Skip to content

Commit

Permalink
Implement some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kif committed Dec 20, 2024
1 parent 1c6f537 commit d9d23c1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
15 changes: 9 additions & 6 deletions src/pyFAI/io/integration_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
5: Migrate to dataclass
Support for integrator_name/integrator_method and extra_options.
rename some attributes
In a similar way, PixelwiseWorkerConfig and DistortionWorkerConfig are dataclasses
to hold parameters for handling PixelwiseWorker and DistortionWorker, respectively.
Expand All @@ -65,6 +66,7 @@
__date__ = "20/12/2024"
__docformat__ = 'restructuredtext'

import sys
import os
import json
import logging
Expand All @@ -80,9 +82,14 @@
from ..integrator import load_engines as load_integrators

_logger = logging.getLogger(__name__)

CURRENT_VERSION = 5

if sys.version_info>=(3,10):
mydataclass = dataclass(slots=True)
else:
mydataclass = dataclass


def _normalize_v1_darkflat_files(config, key):
"""Normalize dark and flat filename list from the version 1 to version 2.
"""
Expand Down Expand Up @@ -411,7 +418,7 @@ def pop_method(self, default=None):
return method


@dataclass(slots=True)
@mydataclass
class WorkerConfig:
"""Class with the configuration from the worker."""
application: str="worker"
Expand Down Expand Up @@ -479,10 +486,6 @@ def from_dict(cls, dico, inplace=False):
return self


def _upgrade(self):
"""Upgrade an elder version of the config ot the latest one"""
pass

def save(self, filename):
"""Dump the content of the dataclass as JSON file"""
with open(filename, "w") as w:
Expand Down
13 changes: 11 additions & 2 deletions src/pyFAI/test/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "04/07/2024"
__date__ = "20/12/2024"

import unittest
import logging
Expand All @@ -46,7 +46,7 @@
from ..integrator.azimuthal import AzimuthalIntegrator
from ..containers import Integrate1dResult
from ..containers import Integrate2dResult
from ..io.integration_config import ConfigurationReader
from ..io.integration_config import ConfigurationReader, WorkerConfig
from ..io.ponifile import PoniFile
from .. import detector_factory
from . import utilstest
Expand Down Expand Up @@ -557,6 +557,15 @@ def test_v3_equal_to_v4(self):
poni_v4_from_config = PoniFile(data=config_v4)
self.assertEqual(poni_v3_from_config.as_dict(), poni_v4_from_config.as_dict(), "PONI dictionaries from config match")

def test_dataclass(self):
test_files = "0.14_verson0.json id11_v0.json id13_v0.json id15_1_v0.json id15_v0.json id16_v3.json id21_v0.json version0.json version3.json version4.json"
for fn in test_files.split():
js = utilstest.UtilsTest.getimage(fn)
with utilstest.TestLogging(logger='pyFAI.io.integrarion_config', warning=0):
# with self.assertLogs('pyFAI.io.integrarion_config', level='WARNING') as cm:
wc = WorkerConfig.load(js)
self.assertEqual(wc, WorkerConfig.from_dict(wc.as_dict()), f"Idempotent {fn}")




Expand Down
13 changes: 7 additions & 6 deletions src/pyFAI/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "03/07/2024"
__date__ = "20/12/2024"
__status__ = "development"

import threading
Expand Down Expand Up @@ -463,10 +463,11 @@ def set_config(self, config, consume_keys=False):
consumed when used.
"""
config = integration_config.normalize(config, inplace=consume_keys, do_raise=False)
print(config)
_init_ai(self.ai, config, consume_keys=True, read_maps=False)

# Do it here before reading the AI to be able to catch the io
filename = config.pop("mask_file", "")
filename = config.pop("mask_image", "")
apply_process = config.pop("do_mask", True)
if filename and apply_process:
try:
Expand All @@ -478,24 +479,24 @@ def set_config(self, config, consume_keys=False):
self.mask_image = filename

# Do it here while we have to store metadata
filename = config.pop("dark_current", "")
filename = config.pop("dark_current_image", "")
apply_process = config.pop("do_dark", True)
if filename and apply_process:
filenames = _normalize_filenames(filename)
method = "mean"
data = _reduce_images(filenames, method=method)
self.ai.detector.set_darkcurrent(data)
self.dark_current_image = "%s(%s)" % (method, ",".join(filenames))
self.dark_current_image = filenames #"%s(%s)" % (method, ",".join(filenames))

# Do it here while we have to store metadata
filename = config.pop("flat_field", "")
filename = config.pop("flat_field_image", "")
apply_process = config.pop("do_flat", True)
if filename and apply_process:
filenames = _normalize_filenames(filename)
method = "mean"
data = _reduce_images(filenames, method=method)
self.ai.detector.set_flatfield(data)
self.flat_field_image = "%s(%s)" % (method, ",".join(filenames))
self.flat_field_image = filenames# "%s(%s)" % (method, ",".join(filenames))

# Uses it anyway in case do_2D is customed after the configuration
value = config.pop("nbpt_azim", None)
Expand Down

0 comments on commit d9d23c1

Please sign in to comment.