LazStats: Refactor DataSmoothUnit

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7474 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-05-26 20:58:55 +00:00
parent 748a489574
commit f635c93904
3 changed files with 121 additions and 86 deletions

View File

@ -1602,7 +1602,7 @@ begin
end else end else
if Command = HELP_COMMAND then if Command = HELP_COMMAND then
begin begin
topic := Application.HelpFile + '::/' + PChar(Data); topic := UnicodeString(Application.HelpFile + '::/' + {%H-}PChar(Data));
res := htmlhelp.HtmlHelpW(0, PWideChar(topic), HH_DISPLAY_TOPIC, 0); res := htmlhelp.HtmlHelpW(0, PWideChar(topic), HH_DISPLAY_TOPIC, 0);
end; end;

View File

@ -3,6 +3,7 @@ object SmoothDataForm: TSmoothDataForm
Height = 386 Height = 386
Top = 190 Top = 190
Width = 387 Width = 387
AutoSize = True
Caption = 'Data Smoothing' Caption = 'Data Smoothing'
ClientHeight = 386 ClientHeight = 386
ClientWidth = 387 ClientWidth = 387
@ -86,6 +87,7 @@ object SmoothDataForm: TSmoothDataForm
Anchors = [akTop, akLeft, akRight] Anchors = [akTop, akLeft, akRight]
BorderSpacing.Left = 8 BorderSpacing.Left = 8
BorderSpacing.Right = 8 BorderSpacing.Right = 8
ReadOnly = True
TabOrder = 3 TabOrder = 3
Text = 'VariableEdit' Text = 'VariableEdit'
end end
@ -135,38 +137,23 @@ object SmoothDataForm: TSmoothDataForm
end end
object ResetBtn: TButton object ResetBtn: TButton
AnchorSideTop.Control = ComputeBtn AnchorSideTop.Control = ComputeBtn
AnchorSideRight.Control = CancelBtn AnchorSideRight.Control = ComputeBtn
Left = 163 Left = 174
Height = 25 Height = 25
Top = 353 Top = 353
Width = 54 Width = 54
Anchors = [akTop, akRight] Anchors = [akTop, akRight]
AutoSize = True AutoSize = True
BorderSpacing.Right = 12 BorderSpacing.Right = 8
Caption = 'Reset' Caption = 'Reset'
OnClick = ResetBtnClick OnClick = ResetBtnClick
TabOrder = 5
end
object CancelBtn: TButton
AnchorSideTop.Control = ComputeBtn
AnchorSideRight.Control = ComputeBtn
Left = 229
Height = 25
Top = 353
Width = 62
Anchors = [akTop, akRight]
AutoSize = True
BorderSpacing.Right = 12
Caption = 'Cancel'
ModalResult = 2
TabOrder = 6 TabOrder = 6
end end
object ComputeBtn: TButton object ComputeBtn: TButton
AnchorSideRight.Control = Owner AnchorSideRight.Control = CloseBtn
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom AnchorSideBottom.Side = asrBottom
Left = 303 Left = 236
Height = 25 Height = 25
Top = 353 Top = 353
Width = 76 Width = 76
@ -176,7 +163,6 @@ object SmoothDataForm: TSmoothDataForm
BorderSpacing.Right = 8 BorderSpacing.Right = 8
BorderSpacing.Bottom = 8 BorderSpacing.Bottom = 8
Caption = 'Compute' Caption = 'Compute'
ModalResult = 1
OnClick = ComputeBtnClick OnClick = ComputeBtnClick
TabOrder = 7 TabOrder = 7
end end
@ -184,16 +170,16 @@ object SmoothDataForm: TSmoothDataForm
Tag = 119 Tag = 119
AnchorSideTop.Control = ComputeBtn AnchorSideTop.Control = ComputeBtn
AnchorSideRight.Control = ResetBtn AnchorSideRight.Control = ResetBtn
Left = 100 Left = 115
Height = 25 Height = 25
Top = 353 Top = 353
Width = 51 Width = 51
Anchors = [akTop, akRight] Anchors = [akTop, akRight]
AutoSize = True AutoSize = True
BorderSpacing.Right = 12 BorderSpacing.Right = 8
Caption = 'Help' Caption = 'Help'
OnClick = HelpBtnClick OnClick = HelpBtnClick
TabOrder = 8 TabOrder = 5
end end
object Bevel1: TBevel object Bevel1: TBevel
AnchorSideLeft.Control = InBtn AnchorSideLeft.Control = InBtn
@ -234,4 +220,32 @@ object SmoothDataForm: TSmoothDataForm
ParentColor = False ParentColor = False
WordWrap = True WordWrap = True
end end
object CloseBtn: TButton
AnchorSideRight.Control = Owner
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 320
Height = 25
Top = 353
Width = 55
Anchors = [akRight, akBottom]
BorderSpacing.Right = 12
BorderSpacing.Bottom = 8
Caption = 'Close'
ModalResult = 11
TabOrder = 8
end
object Label4: TLabel
AnchorSideLeft.Control = VariableEdit
AnchorSideBottom.Control = VariableEdit
Left = 215
Height = 15
Top = 114
Width = 128
Anchors = [akLeft, akBottom]
BorderSpacing.Bottom = 2
Caption = 'Variable to be smoothed'
ParentColor = False
end
end end

View File

@ -16,10 +16,11 @@ type
TSmoothDataForm = class(TForm) TSmoothDataForm = class(TForm)
Bevel1: TBevel; Bevel1: TBevel;
Bevel2: TBevel; Bevel2: TBevel;
CancelBtn: TButton; CloseBtn: TButton;
HelpBtn: TButton; HelpBtn: TButton;
Label3: TLabel; Label3: TLabel;
ComputeBtn: TButton; ComputeBtn: TButton;
Label4: TLabel;
Memo1: TLabel; Memo1: TLabel;
RepeatEdit: TEdit; RepeatEdit: TEdit;
Label1: TLabel; Label1: TLabel;
@ -39,6 +40,7 @@ type
private private
{ private declarations } { private declarations }
FAutoSized: Boolean; FAutoSized: Boolean;
procedure UpdateBtnStates;
public public
{ public declarations } { public declarations }
end; end;
@ -54,66 +56,79 @@ uses
{ TSmoothDataForm } { TSmoothDataForm }
procedure TSmoothDataForm.ResetBtnClick(Sender: TObject); procedure TSmoothDataForm.ResetBtnClick(Sender: TObject);
VAR i : integer; var
i: integer;
begin begin
VarList.Clear; VarList.Clear;
for i := 1 to NoVariables do for i := 1 to NoVariables do
VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]); VarList.Items.Add(OS3MainFrm.DataGrid.Cells[i,0]);
RepeatEdit.Text := '1'; RepeatEdit.Text := '1';
VariableEdit.Text := ''; VariableEdit.Text := '';
InBtn.Enabled := true; UpdateBtnStates;
OutBtn.Enabled := false;
end; end;
procedure TSmoothDataForm.InBtnClick(Sender: TObject); procedure TSmoothDataForm.InBtnClick(Sender: TObject);
VAR index : integer; var
index: integer;
begin begin
index := VarList.ItemIndex; index := VarList.ItemIndex;
VariableEdit.Text := VarList.Items.Strings[index]; if (index > -1) and (VariableEdit.Text = '') then
VarList.Items.Delete(index); begin
InBtn.Enabled := false; VariableEdit.Text := VarList.Items.Strings[index];
OutBtn.Enabled := true; VarList.Items.Delete(index);
UpdateBtnStates;
end;
end; end;
procedure TSmoothDataForm.ComputeBtnClick(Sender: TObject); procedure TSmoothDataForm.ComputeBtnClick(Sender: TObject);
VAR var
DataPts, OutPts : DblDyneVec; DataPts, OutPts: DblDyneVec;
value, avg : double; value, avg: double;
VarCol, N, Reps, i, j, col : integer; VarCol, N, Reps, i, j, col: integer;
varlabel, strvalue : string; varlabel, strvalue: string;
begin begin
N := NoCases; N := NoCases;
SetLength(DataPts,N); SetLength(DataPts, N);
SetLength(OutPts,N); SetLength(OutPts, N);
Reps := StrToInt(RepeatEdit.Text); if not TryStrToInt(RepeatEdit.Text, Reps) or (Reps <= 0) then
varlabel := VariableEdit.Text; begin
for i := 1 to NoVariables do RepeatEdit.SetFocus;
if varlabel = OS3MainFrm.DataGrid.Cells[i,0] then VarCol := i; MessageDlg('Valid positive number required.', mtError, [mbOK], 0);
for i := 1 to N do exit;
begin end;
value := StrToFloat(OS3MainFrm.DataGrid.Cells[VarCol,i]);
DataPts[i-1] := value; varlabel := VariableEdit.Text;
end; for i := 1 to NoVariables do
// repeat smoothing for Reps times if varlabel = OS3MainFrm.DataGrid.Cells[i,0] then VarCol := i;
OutPts[0] := DataPts[0];
OutPts[N-1] := DataPts[N-1]; for i := 1 to N do
for j := 1 to Reps do begin
begin value := StrToFloat(OS3MainFrm.DataGrid.Cells[VarCol,i]);
for i := 1 to N-2 do DataPts[i-1] := value;
begin end;
avg := (DataPts[i-1] + DataPts[i] + DataPts[i+1]) / 3.0;
OutPts[i] := avg; // repeat smoothing for Reps times
end; OutPts[0] := DataPts[0];
if j < Reps then OutPts[N-1] := DataPts[N-1];
for i := 0 to N-1 do DataPts[i] := OutPts[i]; for j := 1 to Reps do
end; begin
// Create a new variable and copy smoothed data into it. for i := 1 to N-2 do
strvalue := 'Smoothed'; begin
col := NoVariables + 1; avg := (DataPts[i-1] + DataPts[i] + DataPts[i+1]) / 3.0;
DictionaryFrm.NewVar(NoVariables+1); OutPts[i] := avg;
DictionaryFrm.DictGrid.Cells[1,NoVariables] := strvalue; end;
OS3MainFrm.DataGrid.Cells[NoVariables,0] := strvalue; if j < Reps then
for i := 0 to N-1 do OS3MainFrm.DataGrid.Cells[col,i+1] := FloatToStr(OutPts[i]); for i := 0 to N-1 do DataPts[i] := OutPts[i];
end;
// Create a new variable and copy smoothed data into it.
strvalue := 'Smoothed';
col := NoVariables + 1;
DictionaryFrm.NewVar(NoVariables+1);
DictionaryFrm.DictGrid.Cells[1, NoVariables] := strvalue;
OS3MainFrm.DataGrid.Cells[NoVariables, 0] := strvalue;
for i := 0 to N-1 do
OS3MainFrm.DataGrid.Cells[col, i+1] := FloatToStr(OutPts[i]);
end; end;
procedure TSmoothDataForm.FormActivate(Sender: TObject); procedure TSmoothDataForm.FormActivate(Sender: TObject);
@ -123,13 +138,13 @@ begin
if FAutoSized then if FAutoSized then
exit; exit;
w := MaxValue([HelpBtn.Width, ComputeBtn.Width, ResetBtn.Width, CancelBtn.Width]); w := MaxValue([HelpBtn.Width, ComputeBtn.Width, ResetBtn.Width, CloseBtn.Width]);
HelpBtn.Constraints.MinWidth := w; HelpBtn.Constraints.MinWidth := w;
ComputeBtn.Constraints.MinWidth := w; ComputeBtn.Constraints.MinWidth := w;
ResetBtn.Constraints.MinWidth := w; ResetBtn.Constraints.MinWidth := w;
CancelBtn.Constraints.MinWidth := w; CloseBtn.Constraints.MinWidth := w;
Constraints.MinWidth := Width; Constraints.MinWidth := (Label1.Width + RepeatEdit.Width + Label3.Width) * 2;
Constraints.MinHeight := Height; Constraints.MinHeight := Height;
FAutoSized := true; FAutoSized := true;
@ -139,8 +154,6 @@ procedure TSmoothDataForm.FormCreate(Sender: TObject);
begin begin
Assert(OS3MainFrm <> nil); Assert(OS3MainFrm <> nil);
if ContextHelpForm = nil then
Application.CreateForm(TContextHelpForm, ContextHelpForm);
if DictionaryFrm = nil then if DictionaryFrm = nil then
Application.CreateForm(TDictionaryFrm, DictionaryFrm); Application.CreateForm(TDictionaryFrm, DictionaryFrm);
end; end;
@ -154,10 +167,18 @@ end;
procedure TSmoothDataForm.OutBtnClick(Sender: TObject); procedure TSmoothDataForm.OutBtnClick(Sender: TObject);
begin begin
VarList.Items.Add(VariableEdit.Text); if VariableEdit.Text <> '' then
VariableEdit.Text := ''; begin
OutBtn.Enabled := false; VarList.Items.Add(VariableEdit.Text);
InBtn.Enabled := true; VariableEdit.Text := '';
UpdateBtnStates;
end;
end;
procedure TSmoothDataForm.UpdateBtnStates;
begin
InBtn.Enabled := (VarList.ItemIndex > -1) and (VariableEdit.Text = '');
OutBtn.Enabled := (VariableEdit.Text <> '');
end; end;
initialization initialization