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

Auto guess the binning from the reading of the first image #2438

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/pyFAI/app/integrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
__contact__ = "[email protected]"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "07/02/2025"
__date__ = "14/02/2025"
__satus__ = "production"

import sys
Expand Down Expand Up @@ -313,6 +313,7 @@ def __init__(self, statistics):
self._items = []
self._statistics = statistics
self._frames_per_items = []
self._shape = None

def append(self, item):
self._items.append(item)
Expand Down Expand Up @@ -464,6 +465,26 @@ def frames(self):
next_id += data_info.frame_id
next_id += 1

@property
def shape(self):
if self._shape is None and self._items:
item = self._items[0]
fabio_image = None
if isinstance(item, (str,)):
with self._statistics.time_reading():
fabio_image = fabio.open(item)
was_openned = True
elif isinstance(item, fabio.fabioimage.FabioImage):
fabio_image = item
was_openned = False
if fabio_image is None:
self._shape = item.shape[-2:]
else:
self._shape = fabio_image.shape
if was_openned:
fabio_image.close()
return self._shape


class MultiFileWriter(io.Writer):
"""Broadcast writing to differnet files for each frames"""
Expand Down Expand Up @@ -640,7 +661,9 @@ def process(input_data, output, config, observer, write_mode=HDF5Writer.MODE_ERR
source.append(item)
else:
logger.warning("Type %s unsopported. Data ignored.", item)

print(f"Initialize worker wih shape {source.shape}")
worker.reconfig(source.shape, sync=True)
print(worker.ai)
observer.processing_started(source.approximate_count())

writer = None
Expand Down
12 changes: 11 additions & 1 deletion src/pyFAI/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from .io.integration_config import WorkerConfig
from .engines.preproc import preproc as preproc_numpy
from .utils.decorators import deprecated_warning
from .utils import binning as rebin
try:
import numexpr
except ImportError as err:
Expand Down Expand Up @@ -284,7 +285,16 @@ def reconfig(self, shape=None, sync=False):
"""
if shape is not None:
self._shape = shape
if not self.ai.detector.force_pixel:
if self.ai.detector.force_pixel:
mask = self.ai.detector.mask
logger.info(f"Before rebin, mask has {(mask!=0).sum()} pixels")
res = self.ai.detector.guess_binning(shape)
if res and mask is not None:
new_shape = numpy.array(self.ai.detector.shape)
if numpy.all(new_shape < numpy.array(mask.shape)):
self.ai.detector.mask = (rebin(mask, self.ai.detector.binning) != 0)
logger.info(f"reconfig: mask has been rebinned from {mask.shape} to {self.ai.detector.mask.shape}. Masking {self.ai.detector.mask.sum()} pixels")
else:
self.ai.detector.shape = shape
self.ai.reset()
self.warmup(sync)
Expand Down