ptyrad.params.hypertune_params#

Defines available options and validation rules for the hypertune_params dictionary.

pydantic model ptyrad.params.hypertune_params.HypertuneParams[source]#

Bases: BaseModel

Hypertune optimizable parameters are specified in ‘tune_params’, these will override the corresponding values in ‘exp_params’ but follows the exact same definition and unit. Set ‘state’ to true to enable hypertuning that parameter. ‘min’, ‘max’, and ‘step’ defines the search space and sampling rate. For example, conv_angle with min:24, max:26, step:0.1 would have a search range between 24 and 26 with 0.1 minimal spacing It’s a better practice to limit your hypertune optimization with no more than 4 parameters simultaneously, and some hierachical strategy (i.e., optimizing dz first, then scale with rotation, then all 4 scan_affine, lastly object tilts) could be much faster / stable For typical dataset with sufficient dose, both conv_angle and defocus can be automatically handled by probe optimization. However for low dose dataset (like 1e2 e-/DP), some hypertune of probe parameters could be necessary

Fields:
param if_hypertune: bool = False#

Default is false. Set to true to run PtyRAD in hypertune (hyperparameter optimization) mode. This is the main switch for hypertune mode so none of the settings in ‘hypertune_params’ would take effect if ‘if_hypertune’ is false.

param verbosity: Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] = 'WARNING'#

Default is ‘WARNING’. The logging verbosity level of the inner reconstruction loop during hypertune trials. Set to ‘INFO’ or ‘DEBUG’ if you need to emit core ptyrad logging for each trial. Note: to see ‘DEBUG’-level messages inside each trial, the outer ‘–verbosity’ CLI arg must also be set to ‘DEBUG’, since the log handlers are initialized at the outer verbosity level and act as a secondary filter.

param collate_results: bool = True#

Default is true. Set to true to collect results/figs specified under ‘recon_params’ from each hypertune trial to ‘output_dir/<HYPERTUNE_FOLDER>’, and name them by error metrics followed by trial parameters. This provides a convenient way to quickly examine the hypertune results inside the hypertune folder under the main output directory ‘output_dir’. This is an independent control than ‘SAVE_ITERS’ and will save by the end of the ‘NITER’ or when the trial is pruned.

param append_params: bool = True#

Default is true. Set to true to append the hyperparameter name/values to the file names of collated result. If set to false, only the error, trial ID, and iter would be preserved. Set this to false to reduce the length of the output file name. The exact parameter values are stored in the .sqlite3 database file.

param n_trials: int = 5#

Number of hypertune trials. Each trial is a separate PtyRAD reconstruction with a set of optimizable parameter values (i.e., a configuration). Note that when the hypertune mode is run in parallel (i.e., multiple jobs on multiple GPU nodes accesing the same database/study), each job will run for ‘n_trials’ times. So submiting 5 jobs with ‘n_trials’: 200 will get you a total of 1000 trials in the database/study

param timeout: float | None = None#

Stop “study” after the given number of second(s). Null represents no limit in terms of elapsed time. The study continues to create trials until the number of trials reaches n_trials, timeout period elapses, stop() is called or, a termination signal such as SIGTERM or Ctrl+C is received.

param sampler_params: SamplerParams [Optional]#

Required — a sampler is always needed; omit the field to use the default TPESampler. Explicit null is rejected. configs: null (or omitted) falls back to the algorithm-specific defaults. See https://optuna.readthedocs.io/en/stable/reference/samplers/index.html for more details.

param pruner_params: PrunerParams | None [Optional]#

Pruning is early termination of unpromising trials to save computation budget. Set to pruner_params: null to disable pruning entirely (i.e., no early termination). configs: null (or omitted) falls back to the algorithm-specific defaults. The recommended prunner is HyperbandPruner, see Optuna document for more details.

param storage_path: str = 'sqlite:///hypertune.sqlite3'#

Path to the SQLite database file (i.e., sotrage) as ‘sqlite:///<DATABASE_FILENAME>.sqlite3’. This database file keeps the record of hypertune runs and will be automatically created with new hypertube run. If you’re doing distributed (e.g. multiple GPU nodes) hypertune by submitting this params file multiple times, then all the GPU worker would be accessing this database to be aware of each other’s progress

param study_name: str = 'study'#

Name of the hypertune record (i.e., study). Hypertune of different dataset or different search space (i.e., different optimizable parameters) are encouraged to use different study name or even separate database file

param error_metric: Literal['loss', 'contrast'] = 'loss'#

Either use ‘loss’ or ‘contrast’. The current suggested approach is to use ‘loss’ for rough optimization, while switch to ‘contrast’ with loaded reconstructed object/probe/pos to refine remaining hyperparameters. ‘contrast’ is simply calculated by std(img)/mean(std) to reflect reconstruction quality of the object because ‘loss’ doesn’t correlate that well. Note that contrast doesn’t necessarily change monotonically with iterations, especially at early iterations so you may want to disable pruning and set NITER carefullly.

param tune_params: TuneParams [Optional]#

See https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.Trial.html#optuna.trial.Trial for the syntax to configure the search space ranges and types

pydantic model ptyrad.params.hypertune_params.SamplerParams[source]#

Bases: BaseModel

Fields:
param name: Literal['RandomSampler', 'TPESampler', 'GPSampler', 'CmaEsSampler', 'NSGAIISampler', 'NSGAIIISampler', 'GridSampler', 'QMCSampler', 'BruteForceSampler'] = 'TPESampler'#
param configs: Dict[str, Any] | None = None#
pydantic model ptyrad.params.hypertune_params.PrunerParams[source]#

Bases: BaseModel

Fields:
param name: Literal['MedianPruner', 'PatientPruner', 'PercentilePruner', 'SuccessiveHalvingPruner', 'HyperbandPruner', 'ThresholdPruner'] = 'HyperbandPruner'#
param configs: Dict[str, Any] | None = None#
pydantic model ptyrad.params.hypertune_params.TuneParams[source]#

Bases: BaseModel

Fields:
param optimizer: TuneParam [Optional]#
param batch_size: TuneParam [Optional]#
param plr: TuneParam [Optional]#
param oalr: TuneParam [Optional]#
param oplr: TuneParam [Optional]#
param slr: TuneParam [Optional]#
param tlr: TuneParam [Optional]#
param dzlr: TuneParam [Optional]#
param dx: TuneParam [Optional]#
param pmode_max: TuneParam [Optional]#
param conv_angle: TuneParam [Optional]#
param C10: TuneParam [Optional]#
param C12: TuneParam [Optional]#
param C21: TuneParam [Optional]#
param C23: TuneParam [Optional]#
param C30: TuneParam [Optional]#
param C32: TuneParam [Optional]#
param C34: TuneParam [Optional]#
param C41: TuneParam [Optional]#
param C43: TuneParam [Optional]#
param C45: TuneParam [Optional]#
param C50: TuneParam [Optional]#
param C52: TuneParam [Optional]#
param C54: TuneParam [Optional]#
param C56: TuneParam [Optional]#
param z_shift: TuneParam [Optional]#
param Nlayer: TuneParam [Optional]#
param dz: TuneParam [Optional]#
param scale: TuneParam [Optional]#
param asymmetry: TuneParam [Optional]#
param rotation: TuneParam [Optional]#
param shear: TuneParam [Optional]#
param tilt_y: TuneParam [Optional]#
param tilt_x: TuneParam [Optional]#
param defocus: TuneParam | None = None#
param c3: TuneParam | None = None#
param c5: TuneParam | None = None#
scrub_legacy_keys(handler)[source]#
Return type:

Dict[str, Any]

pydantic model ptyrad.params.hypertune_params.TuneParam[source]#

Bases: BaseModel

Fields:
param state: bool [Required]#
param suggest: Literal['int', 'float', 'cat'] [Required]#
param kwargs: Dict[str, Any] [Required]#