Class TFitService

Unit

Declaration

type TFitService = class(TInterfacedObject, IClientCallback, IFitService)

Description

The component which implements all server logic. It divides the task of profile fitting on a few subtasks of fitting on intervals. The intervals should be defined before manually or automatically. All interface methods should allow calling in arbitrary state, this should not be considered as inadmissible situation, but corresponding error code should be returned. In fitting knowledge of wavelength is not required. This is why all components TNeutronPointsSet were changed on TPointsSet. This also allows to abstract from tasks of neutron diffraction and to come to tasks from arbitrary field in which data have form of profile. It is impossible simply to replace TNeutronPointsSet on TPointsSet because at the server side methods of TFitViewer are called which require TTitlePointsSet. Moreover graphics is still implemented at server side. This requires usage of TNeutronPointsSet which supports argument recalculation. Interface methods results shows is requested operation allowed or not. Instead of EAssertionFailed the EUserException is used because it is impossible to guarantee that exception will not be thrown from library. This is acceptable only for classes which process user commands. Rest of the classes should use EAssertionFailed in the case of inadmissible state. Boundary class shoud interpret such exception as fatal error but EUserException as state errors. This implementation performs all operations in the thread of caller. It should store all the data necessary for operations including selected intervals because client can be unable to store data.

Hierarchy

  • TInterfacedObject
  • TFitService

Overview

Fields

Protected FBackFactor: double;
Protected FBackgroundPoints: TTitlePointsSet;
Protected FBackgroundVariationEnabled: boolean;
Protected FCalcProfile: TTitlePointsSet;
Protected FCurrentMinimum: double;
Protected FCurveAttributes: TMSCRCurveList;
Protected FCurveExpr: string;
Protected FCurvePositions: TTitlePointsSet;
Protected FCurves: TSelfCopiedCompList;
Protected FCurveScalingEnabled: boolean;
Protected FCurveThresh: double;
Protected FCurveTypeSelector: ICurveTypeSelector;
Protected FDeltaProfile: TTitlePointsSet;
Protected FDoneDisabled: boolean;
Protected FExpProfile: TTitlePointsSet;
Protected FFitDone: boolean;
Protected FMaxRFactor: double;
Protected FParams: Curve_parameters;
Protected FRFactorBounds: TTitlePointsSet;
Protected FSavedState: TFitServerState;
Protected FSelectedArea: TTitlePointsSet;
Protected FSelectedAreaMode: boolean;
Protected FStartTime: TDateTime;
Protected FState: TFitServerState;
Protected FTaskList: TComponentList;
Protected FWaveLength: double;

Methods

Public constructor Create;
Public destructor Destroy; override;
Public function AsyncOper: boolean;
Public function ComputeBackgroundPoints: string; virtual;
Public function ComputeCurveBounds: string; virtual;
Public function ComputeCurvePositions: string; virtual;
Public function DoAllAutomatically: string; virtual;
Public function GetAbsRFactorStr: string;
Public function GetBackFactor: double;
Public function GetBackgroundPoints: TTitlePointsSet;
Public function GetBackgroundVariationEnabled: boolean;
Public function GetCalcProfilePointsSet: TTitlePointsSet;
Public function GetCalcTimeStr: string;
Public function GetCurveAttributes: TMSCRCurveList;
Public function GetCurveCount: longint;
Public function GetCurveParameterCount(SpecIndex: longint): longint;
Public function GetCurvePoints(SpecIndex: longint): TNamedPointsSet;
Public function GetCurvePositions: TTitlePointsSet;
Public function GetCurves: TSelfCopiedCompList;
Public function GetCurveScalingEnabled: boolean;
Public function GetCurveThresh: double;
Public function GetCurveType: TCurveTypeId;
Public function GetDeltaProfilePointsSet: TTitlePointsSet;
Public function GetMaxRFactor: double;
Public function GetProfilePointsSet: TTitlePointsSet;
Public function GetRFactorBounds: TTitlePointsSet;
Public function GetRFactorStr: string;
Public function GetSelectedProfileInterval: TTitlePointsSet;
Public function GetSpecialCurveParameters: Curve_parameters;
Public function GetSqrRFactorStr: string;
Public function GetState: TFitServerState;
Public function GetWaveLength: double;
Public function MinimizeDifference: string; virtual;
Public function MinimizeDifferenceAgain: string; virtual;
Public function MinimizeNumberOfCurves: string; virtual;
Public function SelectAllPointsAsCurvePositions: string; virtual;
Public function SelectEntireProfile: string;
Public function SelectProfileInterval(StartPointIndex, StopPointIndex: longint): string;
Public function SetBackgroundPointsSet(ABackgroundPoints: TTitlePointsSet): string;
Public function SetCurvePositions(ACurvePositions: TPointsSet): string;
Public function SetProfilePointsSet(APointsSet: TTitlePointsSet): string;
Public function SetRFactorBounds(ARFactorBounds: TPointsSet): string;
Public function SmoothProfile: string;
Protected function ComputeBackgroundPointsActual(Data: TPointsSet): TPointsSet;
Protected function ComputeCurvePositionsActual(SearchMinimums: boolean): TTitlePointsSet;
Protected function CreateTaskObject: TFitTask; virtual;
Protected function GetAllInitialized: boolean;
Protected function GetTotalAbsRFactor: double;
Protected function GetTotalRFactor: double;
Protected function GetTotalSqrRFactor: double;
Protected function Integrate(Points: TPointsSet): double;
Protected function IntegrateWithBoundaries(Points: TPointsSet; StartPointIndex, StopPointIndex: longint): double;
Public procedure AbortAsyncOper; virtual; abstract;
Public procedure AddPointToBackground(XValue, YValue: double);
Public procedure AddPointToCurvePositions(XValue, YValue: double);
Public procedure AddPointToProfile(XValue, YValue: double);
Public procedure AddPointToRFactorBounds(XValue, YValue: double);
Public procedure CreateCurveList;
Public procedure GetCurveParameter(SpecIndex: longint; ParamIndex: longint; var Name: string; var Value: double; var Type_: longint);
Public procedure ReplacePointInBackground(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
Public procedure ReplacePointInCurvePositions(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
Public procedure ReplacePointInProfile(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
Public procedure ReplacePointInRFactorBounds(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
Public procedure SetBackFactor(ABackFactor: double);
Public procedure SetBackgroundVariationEnabled(AEnable: boolean);
Public procedure SetCurveParameter(SpecIndex: longint; ParamIndex: longint; Value: double);
Public procedure SetCurveScalingEnabled(AEnabled: boolean);
Public procedure SetCurveThresh(ACurveThresh: double);
Public procedure SetCurveType(ACurveTypeId: TCurveTypeId);
Public procedure SetMaxRFactor(AMaxRFactor: double);
Public procedure SetSpecialCurveParameters(ACurveExpr: string; CP: Curve_parameters);
Public procedure StopAsyncOper; virtual; abstract;
Public procedure SubtractBackground;
Public procedure SubtractBackground(Auto: boolean);
Protected procedure AddCurveToList(Points: TCurvePointsSet; StartPointIndex, StopPointIndex: longint);
Protected procedure AddPoint(var Points: TTitlePointsSet; XValue, YValue: double);
Protected procedure ComputeBackgroundPointsAlg;
Protected procedure ComputeBackgroundPointsDone; virtual;
Protected procedure ComputeBackgroundPointsDoneProcActual;
Protected procedure ComputeCurveBoundsAlg;
Protected procedure ComputeCurveBoundsDone; virtual;
Protected procedure ComputeCurveBoundsDoneProcActual;
Protected procedure ComputeCurvePositionsAlg;
Protected procedure ComputeCurvePositionsDone; virtual;
Protected procedure ComputeCurvePositionsDoneProcActual;
Protected procedure ComputeCurvePositionsForAutoAlg;
Protected procedure CreateDeltaProfile;
Protected procedure CreateParameters(ACurveExpr: string);
Protected procedure CreateResultedCurvePositions;
Protected procedure CreateResultedProfile;
Protected procedure CreateTasks;
Protected procedure DoAllAutomaticallyAlg;
Protected procedure Done; virtual;
Protected procedure DoneProc; virtual;
Protected procedure FillCurveParameterList;
Protected procedure FillCurvesList;
Protected procedure GoToReadyForFit;
Protected procedure InitTasks;
Protected procedure MinimizeDifferenceAgainAlg; virtual;
Protected procedure MinimizeDifferenceAlg; virtual;
Protected procedure MinimizeNumberOfCurvesAlg; virtual;
Protected procedure RecreateMainCalcThread(ACurrentTask: TThreadMethod; ADoneProc: TThreadMethod); virtual;
Protected procedure SelectAllPointsAsCurvePositionsAlg;
Protected procedure SelectProfileIntervalActual(Points: TPointsSet; StartPointIndex, StopPointIndex: longint);
Protected procedure SetState(AState: TFitServerState); virtual;
Protected procedure SetWaveLength(AWaveLength: double);
Protected procedure ShowCurMin(Min: double); virtual;
Protected procedure ShowCurMinInternal; virtual;
Protected procedure ShowProfile; virtual;
Protected procedure SmoothProfileActual(ANeutronPointsSet: TPointsSet);
Protected procedure SubtractBackgroundLinearly(Data: TPointsSet; StartIndex: longint; EndIndex: longint);

Properties

Public property BackFactor: double read GetBackFactor write SetBackFactor;
Public property CurveThresh: double read GetCurveThresh write SetCurveThresh;
Public property CurveTypeId: TCurveTypeId read GetCurveType write SetCurveType ;
Public property MaxRFactor: double read GetMaxRFactor write SetMaxRFactor;
Public property SelectedAreaMode: boolean read FSelectedAreaMode;
Public property State: TFitServerState read GetState;
Public property WaveLength: double read GetWaveLength write SetWaveLength;

Description

Fields

Protected FBackFactor: double;
 
Protected FBackgroundPoints: TTitlePointsSet;

List of background points used in transition between manual and automatic modes of selection.

Protected FBackgroundVariationEnabled: boolean;
 
Protected FCalcProfile: TTitlePointsSet;

The curve obtained by the sum of all curves of adjustable intervals to calculate the total R-factor along the entire profile.

Protected FCurrentMinimum: double;

Current total value of R-factor for all subtasks.

Protected FCurveAttributes: TMSCRCurveList;

Containers of parameters of curves. TODO: change type and remove SetWaveLength.

Protected FCurveExpr: string;

The expression for user defined curve.

Protected FCurvePositions: TTitlePointsSet;

Positions of curves. Only X-coordinates are used.

Protected FCurves: TSelfCopiedCompList;

Contains all curves collected from tasks (for example, for separate intervals). Items are added synchronously to the list.

Protected FCurveScalingEnabled: boolean;
 
Protected FCurveThresh: double;
 
Protected FCurveTypeSelector: ICurveTypeSelector;
 
Protected FDeltaProfile: TTitlePointsSet;

The curve obtained by calculating difference between experimental and calculated profiles.

Protected FDoneDisabled: boolean;
 
Protected FExpProfile: TTitlePointsSet;

Data of full experimental profile.

Protected FFitDone: boolean;

Is set up to True after finishing first cycle of calculation.

Protected FMaxRFactor: double;

Allows to retrieve the value from the client-side.

Protected FParams: Curve_parameters;

Parameters of user defined curve. The object is created by server. It is necessary to provide parameter editing on the client-side.

Protected FRFactorBounds: TTitlePointsSet;

Pairs of points defining intervals of R-factor calculation. Should always be displayed allowing user to see where R-factor is calculated.

Protected FSavedState: TFitServerState;

The state that preceded the transition to AsyncOperation.

Protected FSelectedArea: TTitlePointsSet;

Part of the whole profile with which user works at the given moment.

Protected FSelectedAreaMode: boolean;

Dependent on this flag either data of the selected interval are used or data of the whole profile.

Protected FStartTime: TDateTime;

The starting time of continuous operation.

Protected FState: TFitServerState;
 
Protected FTaskList: TComponentList;

List of subtasks for fitting parts of profile on intervals. By default is active.

Protected FWaveLength: double;
 

Methods

Public constructor Create;
 
Public destructor Destroy; override;
 
Public function AsyncOper: boolean;

Returns True in asynchronous operation mode.

Public function ComputeBackgroundPoints: string; virtual;

Searches for background points.

Public function ComputeCurveBounds: string; virtual;

Searches for intervals of application of curves.

Public function ComputeCurvePositions: string; virtual;

Searches for curve positions.

Public function DoAllAutomatically: string; virtual;

Completely automatic procedure of finding model curves.

Public function GetAbsRFactorStr: string;
 
Public function GetBackFactor: double;
 
Public function GetBackgroundPoints: TTitlePointsSet;
 
Public function GetBackgroundVariationEnabled: boolean;
 
Public function GetCalcProfilePointsSet: TTitlePointsSet;

Methods return copies of objects, they should be free by caller.

Public function GetCalcTimeStr: string;
 
Public function GetCurveAttributes: TMSCRCurveList;

Returns list of parameters of all curves.

Public function GetCurveCount: longint;

These methods check validity of server state and throw EUserException in the case when state is invalid.

Public function GetCurveParameterCount(SpecIndex: longint): longint;
 
Public function GetCurvePoints(SpecIndex: longint): TNamedPointsSet;
 
Public function GetCurvePositions: TTitlePointsSet;
 
Public function GetCurves: TSelfCopiedCompList;

Returns list of components containing sets of points.

Public function GetCurveScalingEnabled: boolean;
 
Public function GetCurveThresh: double;
 
Public function GetCurveType: TCurveTypeId;
 
Public function GetDeltaProfilePointsSet: TTitlePointsSet;
 
Public function GetMaxRFactor: double;
 
Public function GetProfilePointsSet: TTitlePointsSet;

Get experimental profile data.

Public function GetRFactorBounds: TTitlePointsSet;
 
Public function GetRFactorStr: string;
 
Public function GetSelectedProfileInterval: TTitlePointsSet;

Get data for the selected interval.

Public function GetSpecialCurveParameters: Curve_parameters;
 
Public function GetSqrRFactorStr: string;
 
Public function GetState: TFitServerState;
 
Public function GetWaveLength: double;
 
Public function MinimizeDifference: string; virtual;

Performs model fitting (initial or subsequent). Corresponds to MinimizeDifference.

Public function MinimizeDifferenceAgain: string; virtual;

Performs model fitting without initialization of bounds.

Public function MinimizeNumberOfCurves: string; virtual;

Search for model describing experimental data with given accuracy by minimum number of specimens. Sequentially reducing the number of specimens.

Public function SelectAllPointsAsCurvePositions: string; virtual;
 
Public function SelectEntireProfile: string;
 
Public function SelectProfileInterval(StartPointIndex, StopPointIndex: longint): string;

Transfers part of profile data to the list of selected interval.

Public function SetBackgroundPointsSet(ABackgroundPoints: TTitlePointsSet): string;
 
Public function SetCurvePositions(ACurvePositions: TPointsSet): string;
 
Public function SetProfilePointsSet(APointsSet: TTitlePointsSet): string;

Set experimental profile data.

Public function SetRFactorBounds(ARFactorBounds: TPointsSet): string;
 
Public function SmoothProfile: string;

Smoothes experimental data. Returns describing message. TODO: so far is executed synchronously. Refactor to asynchronous processing.

Protected function ComputeBackgroundPointsActual(Data: TPointsSet): TPointsSet;

Calculates reference points for linear cut up the background. The points aren't sorted by X.

Protected function ComputeCurvePositionsActual(SearchMinimums: boolean): TTitlePointsSet;

Searches for peak points and return them.

Protected function CreateTaskObject: TFitTask; virtual;
 
Protected function GetAllInitialized: boolean;
 
Protected function GetTotalAbsRFactor: double;
 
Protected function GetTotalRFactor: double;

Calculates the R-factor for FCalcProfile and SelectProfileInterval by sum for all tasks.

Protected function GetTotalSqrRFactor: double;
 
Protected function Integrate(Points: TPointsSet): double;
 
Protected function IntegrateWithBoundaries(Points: TPointsSet; StartPointIndex, StopPointIndex: longint): double;
 
Public procedure AbortAsyncOper; virtual; abstract;

Stops long-term operation synchronously without calling termination procedure.

Public procedure AddPointToBackground(XValue, YValue: double);
 
Public procedure AddPointToCurvePositions(XValue, YValue: double);
 
Public procedure AddPointToProfile(XValue, YValue: double);

All methods call AddPoint.

Public procedure AddPointToRFactorBounds(XValue, YValue: double);
 
Public procedure CreateCurveList;

Defines starting and finishing point for each curve (specimen), integrates it and puts parameters into resulting list.

Public procedure GetCurveParameter(SpecIndex: longint; ParamIndex: longint; var Name: string; var Value: double; var Type_: longint);
 
Public procedure ReplacePointInBackground(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
 
Public procedure ReplacePointInCurvePositions(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
 
Public procedure ReplacePointInProfile(PrevXValue, PrevYValue, NewXValue, NewYValue: double);

All methods call ReplacePoint.

Public procedure ReplacePointInRFactorBounds(PrevXValue, PrevYValue, NewXValue, NewYValue: double);
 
Public procedure SetBackFactor(ABackFactor: double);

Denominator of ratio of background to maximal intensity.

Public procedure SetBackgroundVariationEnabled(AEnable: boolean);
 
Public procedure SetCurveParameter(SpecIndex: longint; ParamIndex: longint; Value: double);
 
Public procedure SetCurveScalingEnabled(AEnabled: boolean);
 
Public procedure SetCurveThresh(ACurveThresh: double);

The threshold for determination of curve (specimen) boundaries. It is supposed that background was cut out. The curve boundaries are defined by exceeding the threshold by curve function. The same threshold removes instances with too small amplitude.

Public procedure SetCurveType(ACurveTypeId: TCurveTypeId);

https://github.com/dvmorozov/fit/issues/160

Public procedure SetMaxRFactor(AMaxRFactor: double);

Maximum allowed value of R-factor.

Public procedure SetSpecialCurveParameters(ACurveExpr: string; CP: Curve_parameters);
 
Public procedure StopAsyncOper; virtual; abstract;

Stops long-term operation asynchronously. Calls termination procedure.

Public procedure SubtractBackground;

Linearly subtracts background in the SelectProfileInterval and recreates SelectProfileInterval. TODO: unify with SubtractBackground.

Public procedure SubtractBackground(Auto: boolean);

Subtracts the background by linear approximation. When Auto is True then background points selected before (no matter by which way) are dropped out. TODO: when it is called as interface method should return text message.

Protected procedure AddCurveToList(Points: TCurvePointsSet; StartPointIndex, StopPointIndex: longint);

Integrates specimen curve and adds resulting value to the list of results.

Protected procedure AddPoint(var Points: TTitlePointsSet; XValue, YValue: double);

Adds new point to the given point set. Second call with the same coordinates removes point from the list. At this the list object is replaced by new one.

Protected procedure ComputeBackgroundPointsAlg;

Calculates background points.

Protected procedure ComputeBackgroundPointsDone; virtual;

TODO: implement.

Protected procedure ComputeBackgroundPointsDoneProcActual;
 
Protected procedure ComputeCurveBoundsAlg;

Calculates boundaries of R-factor intervals based on data obtained from ComputeCurvePositionsActual.

Protected procedure ComputeCurveBoundsDone; virtual;

TODO: implement.

Protected procedure ComputeCurveBoundsDoneProcActual;
 
Protected procedure ComputeCurvePositionsAlg;

Calculates peak positions which will be taken as specimen positions.

Protected procedure ComputeCurvePositionsDone; virtual;

TODO: implement.

Protected procedure ComputeCurvePositionsDoneProcActual;
 
Protected procedure ComputeCurvePositionsForAutoAlg;

Fills the list of peak positions for automatic fit.

Protected procedure CreateDeltaProfile;

Calculates profile containing differences between calculated and experimental data. In the calculation all curves are included. Will not work properly if curves are overlapped.

Protected procedure CreateParameters(ACurveExpr: string);

Checks expression and fills list of parameters.

Protected procedure CreateResultedCurvePositions;

Collects resulting set of curve positions.

Protected procedure CreateResultedProfile;

Auxiliary methods.

Protected procedure CreateTasks;

Creates subtasks for selected intervals. If the intervals were not selected generates them automatically.

Protected procedure DoAllAutomaticallyAlg;
 
Protected procedure Done; virtual;

TODO: implement.

Protected procedure DoneProc; virtual;

These methods are executed in the separate thread.

Protected procedure FillCurveParameterList;

Iterates through list of curves and creates common list of parameters of all curves complementing them with calculated parameters.

Protected procedure FillCurvesList;
 
Protected procedure GoToReadyForFit;

Prepares intermediate results for user.

Protected procedure InitTasks;
 
Protected procedure MinimizeDifferenceAgainAlg; virtual;
 
Protected procedure MinimizeDifferenceAlg; virtual;
 
Protected procedure MinimizeNumberOfCurvesAlg; virtual;

Wrappers for corresponding methods of TFitTask.

Protected procedure RecreateMainCalcThread(ACurrentTask: TThreadMethod; ADoneProc: TThreadMethod); virtual;

Does not really create any thread. Simply calls methods synchronously.

Protected procedure SelectAllPointsAsCurvePositionsAlg;

Selects all points as specimen positions.

Protected procedure SelectProfileIntervalActual(Points: TPointsSet; StartPointIndex, StopPointIndex: longint);

Copies data from given list to the list of selected interval.

Protected procedure SetState(AState: TFitServerState); virtual;

State changing. The state should not change within additional thread, because the change destroys it.

Protected procedure SetWaveLength(AWaveLength: double);

TODO: remove this.

Protected procedure ShowCurMin(Min: double); virtual;

Regenerates resulting list of curves, recalculates resulting profile and updates current minimum value.

Protected procedure ShowCurMinInternal; virtual;

Methods used by optimization algorithm to update information in achieving of new minimum. Calls IClientCallback's ShowCurMin method.

Protected procedure ShowProfile; virtual;

Updates profile data after background subtraction.

Protected procedure SmoothProfileActual(ANeutronPointsSet: TPointsSet);

Low-level methods used by algorithms.

Protected procedure SubtractBackgroundLinearly(Data: TPointsSet; StartIndex: longint; EndIndex: longint);

Linearly subtracts background at the given interval of points.

Properties

Public property BackFactor: double read GetBackFactor write SetBackFactor;
 
Public property CurveThresh: double read GetCurveThresh write SetCurveThresh;
 
Public property CurveTypeId: TCurveTypeId read GetCurveType write SetCurveType ;
 
Public property MaxRFactor: double read GetMaxRFactor write SetMaxRFactor;
 
Public property SelectedAreaMode: boolean read FSelectedAreaMode;
 
Public property State: TFitServerState read GetState;
 
Public property WaveLength: double read GetWaveLength write SetWaveLength;
 

Generated by PasDoc 0.15.0.