Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralise the reading & WRITING of json config into the io module #2360

Open
kif opened this issue Dec 18, 2024 · 2 comments
Open

Centralise the reading & WRITING of json config into the io module #2360

kif opened this issue Dec 18, 2024 · 2 comments
Assignees

Comments

@kif
Copy link
Member

kif commented Dec 18, 2024

Everything should go to:
src/pyFAI/io/integration_config.py
Attn: @t20100

@kif kif changed the title Centralise the reading & **writing** of json config into the io module Centralise the reading & WRITING of json config into the io module Dec 18, 2024
@kif
Copy link
Member Author

kif commented Dec 19, 2024

One should use a dataclass, since they are mutable, and provide them with method to parse/dump to dicts.

@t20100
Copy link
Member

t20100 commented Dec 19, 2024

That would be great!

Here are the few places I found that deals with exporting the config.
The most different on is the worker get_config (with mask_image instead of do_mask and mask_file).

worker doc:

The configuration of this class is mainly done via a dictionary transmitted as a JSON string:
Here are the valid keys:
- "dist"
- "poni1"
- "poni2"
- "rot1"
- "rot3"
- "rot2"
- "pixel1"
- "pixel2"
- "splineFile"
- "wavelength"
- "poni" #path of the file
- "chi_discontinuity_at_0"
- "do_mask"
- "do_dark"
- "do_azimuthal_range"
- "do_flat"
- "do_2D"
- "azimuth_range_min"
- "azimuth_range_max"
- "polarization_factor"
- "nbpt_rad"
- "do_solid_angle"
- "do_radial_range"
- "error_model"
- "delta_dummy"
- "nbpt_azim"
- "flat_field"
- "radial_range_min"
- "dark_current"
- "do_polarization"
- "mask_file"
- "detector"
- "unit"
- "radial_range_max"
- "val_dummy"
- "do_dummy"
- "method"

worker get_config:

pyFAI/src/pyFAI/worker.py

Lines 590 to 624 in b1446d5

def get_config(self):
"""Returns the configuration as a dictionary.
:return: dict with the config to be de-serialized with set_config/loaded with pyFAI.load
"""
config = {
"version": 4,
"application" : "worker",
"unit": str(self.unit),
}
config["poni"] = dict(self.ai.get_config())
for key in ["nbpt_azim", "nbpt_rad", "polarization_factor", "dummy", "delta_dummy",
"correct_solid_angle", "dark_current_image", "flat_field_image",
"mask_image", "error_model", "shape", "method"]:
try:
config[key] = self.__getattribute__(key)
except Exception:
pass
for key in ["azimuth_range", "radial_range"]:
try:
value = self.__getattribute__(key)
except Exception:
pass
else:
if value is not None:
config["do_" + key] = True
config[key + "_min"] = min(value)
config[key + "_max"] = max(value)
else:
config["do_" + key] = False
return config

ponifile:

def as_integration_config(self):
config = {
"application" : "poni",
"version" : 4,
"poni" : dict(self.as_dict()),
"do_2D" : True,
"nbpt_rad" : 500,
"nbpt_azim" : 360,
"unit" : "q_nm^-1",
"do_mask" : False,
"mask_file" : None,
"do_flat" : False,
"flat_field" : None,
"do_dark" : False,
"dark_current" : None,
"method" : ("bbox", "csr", "cython"),
"do_dummy" : None,
"val_dummy" : None,
"delta_dummy" : None,
"do_solid_angle" : True,
"error_model" : None,
"do_radial_range" : False,
"radial_range_min" : None,
"radial_range_max" : None,
"do_azimuthal_range" : False,
"azimuth_range_min" : None,
"azimuth_range_max" : None,
"chi_discontinuity_at_0" : False,
"do_polarization" : False,
"normalization_factor" : 1.0,
}
return integration_config.normalize(config=config)

WorkerConfigurator widget:

def getConfig(self):
"""Read the configuration of the plugin and returns it as a dictionary
:return: dict with all information.
"""
def splitFiles(filenames):
"""In case files was provided with comma.
The file brower was in this case not working, but the returned
config will be valid.
"""
filenames = filenames.strip()
if filenames == "":
return None
return [name.strip() for name in filenames.split("|")]
config = collections.OrderedDict()
# file-version
config["application"] = "pyfai-integrate"
config["version"] = 4
# geometry
config["poni"] = self.getPoniDict()
# pre-processing
config["do_mask"] = bool(self.do_mask.isChecked())
config["mask_file"] = str_(self.mask_file.text()).strip()
config["do_dark"] = bool(self.do_dark.isChecked())
config["dark_current"] = splitFiles(self.dark_current.text())
config["do_flat"] = bool(self.do_flat.isChecked())
config["flat_field"] = splitFiles(self.flat_field.text())
config["do_polarization"] = bool(self.do_polarization.isChecked())
config["polarization_factor"] = float_(self.polarization_factor.value())
config["do_dummy"] = bool(self.do_dummy.isChecked())
config["val_dummy"] = self._float("val_dummy", None)
config["delta_dummy"] = self._float("delta_dummy", None)
# integration
config["do_2D"] = bool(self.do_2D.isChecked())
value = self.__getRadialNbpt()
if value is not None:
config["nbpt_rad"] = value
value = self.__getAzimuthalNbpt()
if value is not None:
config["nbpt_azim"] = value
config["unit"] = str(self.radial_unit.model().value())
config["do_radial_range"] = bool(self.do_radial_range.isChecked())
config["radial_range_min"] = self._float("radial_range_min", None)
config["radial_range_max"] = self._float("radial_range_max", None)
config["do_azimuthal_range"] = bool(self.do_azimuthal_range.isChecked())
config["azimuth_range_min"] = self._float("azimuth_range_min", None)
config["azimuth_range_max"] = self._float("azimuth_range_max", None)
# processing-config
config["chi_discontinuity_at_0"] = bool(self.chi_discontinuity_at_0.isChecked())
config["do_solid_angle"] = bool(self.do_solid_angle.isChecked())
config["error_model"] = self.error_model.currentData()
method = self.__method
if method is not None:
config["method"] = method.split, method.algo, method.impl
if method.impl == "opencl":
config["opencl_device"] = self.__openclDevice
if self.do_normalization.isChecked():
value = self.normalization_factor.text()
if value != "":
try:
value = float(value)
except ValueError:
value = None
if value not in [1.0, None]:
config["normalization_factor"] = value
value = self.monitor_name.text()
if value != "":
value = str(value)
config["monitor_name"] = value
if self.integrator_name.currentText() == "sigma_clip_ng":
config["do_2D"] = False
config["integrator_name"] = "sigma_clip_ng"
config["extra_options"] = {"thres" : float(self.sigmaclip_threshold.text()),
"max_iter" : float(self.sigmaclip_maxiter.text()),
}
return config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants