usansred package
usansred.reduce
- class usansred.reduce.CombinedSample(**data: Any)[source]
Bases:
BaseModelCombine multiple Sample measurements at the raw (X,Y,E) scan level before converting to (Q,I,E).
Attributes
- namestr
Combined sample name.
- experimentExperiment
Experiment this combined sample belongs to.
- thicknessfloat
Sample thickness in cm.
- is_backgroundbool
Whether this combined sample represents a background measurement.
- combined_sampleslist[Sample]
Individual Sample objects whose scans will be combined.
- combined_scanslist[Scan]
Scans produced by the combination.
- combine() None[source]
Sum raw XY data from all combined samples scan-by-scan, then generate IQ data.
For each scan index, the monitor and detector XY data from every sample are accumulated. If a sample has fewer scans than others a warning is logged and it is skipped for that index. After accumulation the XY to IQ conversion is performed on the combined data and ready for reduction.
Raises
- AssertionError
If
combined_samplesis empty or none of them contain scans.
- experiment: Experiment
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class usansred.reduce.Experiment(*, config_file: str, config: dict = <factory>, output_dir: str = '', prim_wave: float = 3.6, v_angle: float = 0.042, log_binning: bool = False, num_of_banks: int = 4, folder: str = '', background: ~usansred.reduce.Sample | None = None, samples: list[~usansred.reduce.Sample] = <factory>)[source]
Bases:
BaseModelExperiment configuration for USANS data reduction
Attributes
- config_filestr
Path to the configuration file
- configdict
Configuration file loaded into a Python dictionary. Populated for both JSON and CSV config files
- output_dirstr | None
Output folder for reduced data, default is current folder
- prim_wavefloat
Primary wavelength in Angstroms, default is 3.6
- v_anglefloat
Vertical angle, default is 0.042
- log_binningbool
Flag for log-binning, default is False
- amend_log_binning(logbin: bool) None[source]
Override the log-binning setting with the command-line –logbin flag.
For backwards compatibility when user enters a CSV file
Parameters
- logbinbool
When True, enables log-binning regardless of what the config file says. When False, the config-file value set during initialisation is preserved.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class usansred.reduce.Sample(**data: Any)[source]
Bases:
BaseModelContainer for sample information, related scans, and data reduction methods
- property config
- property data
Main detector data, currently an alias for detector_data[0]
- data_bg_subtracted: IQData
- data_log_binned: IQData
- property data_reduced
Reduced data, currently an alias for bg_subtracted data
- dump_data_to_csv(filepath: str, data: IQData | XYData, title: str | None = None)[source]
Dump IQ or XY data to a CSV file.
- dump_reduced_data_to_csv(detector_data: bool = True, scaled_data: bool = True, bg_subtracted_data: bool = True, log_binned_data: bool = True)[source]
Dump reduced data to CSV files based on specified flags.
- experiment: Experiment
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- normalize_by_monitor() None[source]
Normalize detector intensities by monitor counts for all scans.
- rescale_data() None[source]
Rescale reflected data by the analyzer’s solid angle acceptance and by sample thickness.
- rocking_curve_centering() float[source]
Center the stitched rocking curves by fitting a Gaussian peak to the rocking curve of the first harmonic.
Notice that the
qvalues of the stitched rocking curves are analyzer motor angles at this stage of reduction, not yet converted to Q values. The first harmonic is fit to a Gaussian over the mostly symmetric angle range[q_min, -q_min], whereq_minis the minimum analyzier motor angle. It will be a negative value. The center of the fitted Gaussian represents the value of the analyzer motor angle at which the analyzer reflects neutrons that have not been scattered by the sample. It should be very close to zero.Returns
- float
Fitted first-harmonic motor-angle center.
- stitch_scans()[source]
Stitch scan data from each detector bank into per-bank intensity curves.
For each detector bank (harmonic), combine all scans in
self.scansonto a single Intensity-versus-Q profile. Detector intensities and errors are expected to already be normalized byScan.normalize_by_monitorbefore being added to the stitched output.Notice that at this stage, the “Q” values are actually analyzer-motor angles, that is, detector bank
iq_data.qvalues are analyzer-motor angles (in arcsec units).If two or more scans contain the same “Q” value, their intensities are combined into one point using inverse-variance weighting, and the combined uncertainty is stored as the square root of the inverse summed weights.
After all scans for a bank are processed, the stitched points are sorted by Q
The method also generates log messages for the raw scan theta ranges and converted Q ranges in
1/angstromfor the first detector bank. Results are stored onself.detector_data; no value is returned.
- class usansred.reduce.Scan(**data: Any)[source]
Bases:
BaseModelA single scan (run) of a sample in an experiment
Attributes
- numberint
Scan (run) number
- experimentExperiment
Experiment this scan belongs to
- monitor_dataMonitorData
Monitor data associated with this scan
- detector_datalist[MonitorData]
Detector data associated with this scan
- load_databool
Whether to load data files during initialization. Set to False when creating placeholder scans (e.g. for CombinedSample).
- convert_xy_to_iq(xy_data: XYData) IQData[source]
Convert XY data to IQ data
Directly copies x to q, y to i, and t. For error, calculates based on a Poisson-like statistical model:
err = sqrt(|y - 0.5| + 0.5)
which ensures a minimum value to avoid zero error for low counts.
- experiment: Experiment
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- monitor_data: MonitorData
- normalize_by_monitor() None[source]
Normalize detector intensities by monitor counts.
Each harmonic in the scan is normalized independently, and within each harmonic, the counts collected at the detector during the time the analyzer-motor remained at a particular angle are divided by the counts collected at the monitor during such time.
- usansred.reduce.horizontal_rocking_width(order: int) float[source]
FWHM (arcs) of the resolution function at the detector for a given reflection order
References
M. Agamalian et al., “Progress on The Time-of-Flight Ultra Small Angle Neutron Scattering Instrument at SNS”, J. Phys.: Conf. Ser. 1021 (2018) 012033.
Parameters
- orderint
Positive reflection order (a.k.a. harmonic or bank)
Returns
- float
Computed horizontal angular resolution.
usansred.reduce_USANS
usansred.summary
summary.py: summary of the reduced data.
- usansred.summary.generate_report(config_file_path: str, data_dir: str | None = None, output_dir: str | None = None)[source]
Generate report from a reduction config file.
Parameters
- config_file_pathstr
Path to the configuration file (CSV or JSON).
- data_dirstr | None
Directory where the reduced data are stored. If None, use the reduced dir in the config file directory.
- output_dirstr | None
Where to save the report. If None, use data_folder/reduced.