Skip to content

Commit

Permalink
Merge pull request #2240 from EdgarGF93/pyFAI-calib2-json
Browse files Browse the repository at this point in the history
[pyFAI-calib2] new feature: save .json file
  • Loading branch information
kif authored Aug 19, 2024
2 parents fac5815 + 46552e1 commit 32b2dc5
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pyFAI/gui/model/ExperimentSettingsModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self, parent=None):
self.__calibrantModel = CalibrantModel()
self.__detectorModel = DetectorModel()
self.__poniFile = FilenameModel()
self.__jsonFile = FilenameModel()

self.__image.changed.connect(self.wasChanged)
self.__image.filenameChanged.connect(self.wasChanged)
Expand All @@ -72,6 +73,7 @@ def __init__(self, parent=None):
self.__calibrantModel.changed.connect(self.wasChanged)
self.__detectorModel.changed.connect(self.wasChanged)
self.__poniFile.changed.connect(self.wasChanged)
self.__jsonFile.changed.connect(self.wasChanged)

self.__dark.changed.connect(self.wasChanged)
self.__dark.filenameChanged.connect(self.wasChanged)
Expand Down Expand Up @@ -166,6 +168,9 @@ def polarizationFactor(self):
def poniFile(self):
return self.__poniFile

def jsonFile(self):
return self.__jsonFile

def dark(self):
return self.__dark

Expand Down
85 changes: 85 additions & 0 deletions src/pyFAI/gui/tasks/IntegrationTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
from pyFAI import method_registry
from ..dialog import MessageBox
from pyFAI.io import ponifile
from pyFAI.worker import Worker
import pyFAI.geometry
import json

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -952,6 +954,7 @@ def _initGui(self):
self._customMethodButton.clicked.connect(self.__customIntegrationMethod)

self._savePoniButton.clicked.connect(self.__saveAsPoni)
self._saveJsonButton.clicked.connect(self.__saveJsonFile)

super()._initGui()

Expand Down Expand Up @@ -1135,6 +1138,88 @@ def __saveAsPoni(self):
except Exception as e:
MessageBox.exception(self, "Error while saving poni file", e, _logger)

def __saveJsonFile(self):
dialog = createSaveDialog(self, "Save as JSON file", json=True)
# Disable the warning as the data is append to the file
dialog.setOption(qt.QFileDialog.DontConfirmOverwrite, True)
model = self.model()
jsonFile = model.experimentSettingsModel().jsonFile()
previousJsonFile = jsonFile.value()
if previousJsonFile is not None:
dialog.selectFile(previousJsonFile)

result = dialog.exec_()
if not result:
return
filename = dialog.selectedFiles()[0]
nameFilter = dialog.selectedNameFilter()
isJsonFilter = ".json" in nameFilter
if isJsonFilter and not filename.endswith(".json"):
filename = filename + ".json"
with jsonFile.lockContext():
jsonFile.setValue(filename)

geometry = self._geometryTabs.geometryModel()
experimentSettingsModel = model.experimentSettingsModel()
detector = experimentSettingsModel.detector()

ai = AzimuthalIntegrator(
dist=geometry.distance().value(),
poni1=geometry.poni1().value(),
poni2=geometry.poni2().value(),
rot1=geometry.rotation1().value(),
rot2=geometry.rotation2().value(),
rot3=geometry.rotation3().value(),
detector=detector,
wavelength=geometry.wavelength().value(),
)

nbpt_rad = self.model().integrationSettingsModel().nPointsRadial().value()
nbpt_azim = self.model().integrationSettingsModel().nPointsAzimuthal().value()
unit = self.model().integrationSettingsModel().radialUnit().value()
method = method_registry.Method(0, self.__method.split, self.__method.algo, self.__method.impl, None)
if nbpt_azim == 1:
method = method.fixed(dim=1)
else:
method = method.fixed(dim=2)
worker = Worker(azimuthalIntegrator=ai,
shapeOut=(nbpt_azim, nbpt_rad),
unit=unit,
method=method,
)
config_dictionary = worker.get_config()
config_dictionary["application"] = "pyFAI-calib2"
config_dictionary["do_polarization"] = True
config_dictionary["polarization_factor"] = self.model().experimentSettingsModel().polarizationFactor().value()
mask_filename = model.experimentSettingsModel().mask().filename()
flat_filename = model.experimentSettingsModel().flat().filename()
dark_filename = model.experimentSettingsModel().dark().filename()
if mask_filename:
config_dictionary["do_mask"] = True
config_dictionary["mask_file"] = mask_filename
else:
config_dictionary["do_mask"] = False
if flat_filename:
config_dictionary["do_flat"] = True
config_dictionary["flat_field"] = flat_filename
else:
config_dictionary["do_flat"] = False
if dark_filename:
config_dictionary["do_dark"] = True
config_dictionary["dark_current"] = dark_filename
else:
config_dictionary["do_dark"] = False

try:
with open(filename, "wt") as fd:
fd.write(json.dumps(config_dictionary, indent=2))

with jsonFile.lockContext():
jsonFile.setValue(filename)
jsonFile.setSynchronized(True)
except Exception as e:
MessageBox.exception(self, "Error while saving json file", e, _logger)

def __updateDisplayedGeometry(self):
"Called after the fit"
experimentSettingsModel = self.model().experimentSettingsModel()
Expand Down
10 changes: 10 additions & 0 deletions src/pyFAI/resources/gui/calibration-result.ui
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="_saveJsonButton">
<property name="toolTip">
<string>A .json file with PONI and integration parameters.</string>
</property>
<property name="text">
<string>Save integration JSON file</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 32b2dc5

Please sign in to comment.