Skip to content

Commit

Permalink
improve ptychoshelves product reader; add cSAXS diffraction reader
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehenke committed Dec 6, 2024
1 parent b1a292d commit 1fb3722
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 137 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.rst"
requires-python = ">=3.10"
license = {file = "LICENSE.txt"}
dependencies = [
"h5py",
"h5py>=3",
"hdf5plugin",
"matplotlib",
"numpy",
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h5py
h5py>=3
hdf5plugin
matplotlib
mypy
Expand Down
10 changes: 2 additions & 8 deletions src/ptychodus/model/patterns/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@ def __init__(self, registry: SettingsRegistry) -> None:
self._settingsGroup.addObserver(self)

self.widthInPixels = self._settingsGroup.createIntegerParameter(
'WidthInPixels', 1024, minimum=0
'WidthInPixels', 1024, minimum=1
)
self.pixelWidthInMeters = self._settingsGroup.createRealParameter(
'PixelWidthInMeters', 75e-6, minimum=0.0
)
self.heightInPixels = self._settingsGroup.createIntegerParameter(
'HeightInPixels', 1024, minimum=0
'HeightInPixels', 1024, minimum=1
)
self.pixelHeightInMeters = self._settingsGroup.createRealParameter(
'PixelHeightInMeters', 75e-6, minimum=0.0
)
self.bitDepth = self._settingsGroup.createIntegerParameter('BitDepth', 8, minimum=1)

def getImageExtent(self) -> ImageExtent:
return ImageExtent(
widthInPixels=self.widthInPixels.getValue(),
heightInPixels=self.heightInPixels.getValue(),
)

def setImageExtent(self, imageExtent: ImageExtent) -> None:
self.widthInPixels.setValue(imageExtent.widthInPixels)
self.heightInPixels.setValue(imageExtent.heightInPixels)
Expand Down
34 changes: 16 additions & 18 deletions src/ptychodus/model/patterns/sizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ def setCropEnabled(self, value: bool) -> None:
self._settings.cropEnabled.setValue(value)

def getWidthLimitsInPixels(self) -> Interval[int]:
return Interval[int](1, self._detector.getImageExtent().widthInPixels)
return Interval[int](1, self._detector.widthInPixels.getValue())

def getWidthInPixels(self) -> int:
limitsInPixels = self.getWidthLimitsInPixels()
return (
limitsInPixels.clamp(self._settings.cropWidthInPixels.getValue())
if self.isCropEnabled()
else limitsInPixels.upper
)
if self.isCropEnabled():
limits = self.getWidthLimitsInPixels()
return limits.clamp(self._settings.cropWidthInPixels.getValue())

return self._detector.widthInPixels.getValue()

def getCenterXLimitsInPixels(self) -> Interval[int]:
return Interval[int](0, self._detector.getImageExtent().widthInPixels)
return Interval[int](0, self._detector.widthInPixels.getValue())

def getCenterXInPixels(self) -> int:
limitsInPixels = self.getCenterXLimitsInPixels()
Expand All @@ -50,7 +49,7 @@ def getCenterXInPixels(self) -> int:

def _getSafeCenterXInPixels(self) -> int:
lower = self.getWidthInPixels() // 2
upper = self._detector.getImageExtent().widthInPixels - 1 - lower
upper = self._detector.widthInPixels.getValue() - 1 - lower
limits = Interval[int](lower, upper)
return limits.clamp(self.getCenterXInPixels())

Expand All @@ -61,18 +60,17 @@ def getWidthInMeters(self) -> float:
return self.getWidthInPixels() * self.getPixelWidthInMeters()

def getHeightLimitsInPixels(self) -> Interval[int]:
return Interval[int](1, self._detector.getImageExtent().heightInPixels)
return Interval[int](1, self._detector.heightInPixels.getValue())

def getHeightInPixels(self) -> int:
limitsInPixels = self.getHeightLimitsInPixels()
return (
limitsInPixels.clamp(self._settings.cropHeightInPixels.getValue())
if self.isCropEnabled()
else limitsInPixels.upper
)
if self.isCropEnabled():
limits = self.getHeightLimitsInPixels()
return limits.clamp(self._settings.cropHeightInPixels.getValue())

return self._detector.heightInPixels.getValue()

def getCenterYLimitsInPixels(self) -> Interval[int]:
return Interval[int](0, self._detector.getImageExtent().heightInPixels)
return Interval[int](0, self._detector.heightInPixels.getValue())

def getCenterYInPixels(self) -> int:
limitsInPixels = self.getCenterYLimitsInPixels()
Expand All @@ -84,7 +82,7 @@ def getCenterYInPixels(self) -> int:

def _getSafeCenterYInPixels(self) -> int:
lower = self.getHeightInPixels() // 2
upper = self._detector.getImageExtent().heightInPixels - 1 - lower
upper = self._detector.heightInPixels.getValue() - 1 - lower
limits = Interval[int](lower, upper)
return limits.clamp(self.getCenterYInPixels())

Expand Down
36 changes: 24 additions & 12 deletions src/ptychodus/plugins/h5DiffractionFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ def _addAttributes(
itemDetails = f'STRING = "{value}"'
elif isinstance(value, h5py.Empty):
logger.debug(f'Skipping empty attribute {name}.')
elif isinstance(value, numpy.ndarray):
itemDetails = f'ARRAY = {value}'
else:
stringInfo = h5py.check_string_dtype(value.dtype)
itemDetails = (
f'STRING = "{value.decode(stringInfo.encoding)}"'
if stringInfo
else f'SCALAR {value.dtype} = {value}'
)

if stringInfo:
itemDetails = f'STRING = "{value.decode(stringInfo.encoding)}"'
else:
itemDetails = f'SCALAR {value.dtype} = {value}'

treeNode.createChild([str(name), 'Attribute', itemDetails])

Expand Down Expand Up @@ -112,13 +114,18 @@ def build(self, h5File: h5py.File) -> SimpleTreeNode:

if isinstance(value, bytes):
itemDetails = value.decode()
elif isinstance(value, numpy.ndarray):
itemDetails = f'STRING = {h5Item.asstr()}'
else:
stringInfo = h5py.check_string_dtype(value.dtype)
itemDetails = (
f'STRING = "{value.decode(stringInfo.encoding)}"'
if stringInfo
else f'SCALAR {value.dtype} = {value}'
)

if stringInfo:
itemDetails = f'STRING = "{value.decode(stringInfo.encoding)}"'
else:
itemDetails = f'SCALAR {value.dtype} = {value}'
elif h5Item.size == 1:
value = h5Item[()]
itemDetails = f'DATASET {value.dtype} = {value}'
else:
itemDetails = f'{h5Item.shape} {h5Item.dtype}'
elif isinstance(h5Item, h5py.SoftLink):
Expand Down Expand Up @@ -194,10 +201,15 @@ def registerPlugins(registry: PluginRegistry) -> None:
simpleName='APS_HXN',
displayName='CNM/APS HXN Diffraction Files (*.h5 *.hdf5)',
)
registry.diffractionFileReaders.registerPlugin(
H5DiffractionFileReader(dataPath='/entry/data/data'),
simpleName='SLS_cSAXS',
displayName='SLS cSAXS Diffraction Files (*.h5 *.hdf5)',
)
registry.diffractionFileReaders.registerPlugin(
H5DiffractionFileReader(dataPath='/entry/measurement/Eiger/data'),
simpleName='NanoMax',
displayName='NanoMax Diffraction Files (*.h5 *.hdf5)',
simpleName='MAX_IV_NanoMax',
displayName='MAX IV NanoMax Diffraction Files (*.h5 *.hdf5)',
)
registry.diffractionFileReaders.registerPlugin(
H5DiffractionFileReader(dataPath='/dp'),
Expand Down
4 changes: 2 additions & 2 deletions src/ptychodus/plugins/lynxDiffractionFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ def read(self, filePath: Path) -> DiffractionDataset:
def registerPlugins(registry: PluginRegistry) -> None:
registry.diffractionFileReaders.registerPlugin(
LYNXDiffractionFileReader(),
simpleName='LYNX',
displayName='LYNX Diffraction Files (*.h5 *.hdf5)',
simpleName='APS_LYNX',
displayName='APS LYNX Diffraction Files (*.h5 *.hdf5)',
)
117 changes: 22 additions & 95 deletions src/ptychodus/plugins/ptychoShelvesProductFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import scipy.io

from ptychodus.api.geometry import PixelGeometry
from ptychodus.api.object import Object, ObjectFileReader, ObjectFileWriter
from ptychodus.api.object import Object
from ptychodus.api.plugins import PluginRegistry
from ptychodus.api.probe import Probe, ProbeFileReader, ProbeFileWriter
from ptychodus.api.probe import Probe
from ptychodus.api.product import (
ELECTRON_VOLT_J,
LIGHT_SPEED_M_PER_S,
Expand All @@ -19,7 +19,7 @@
from ptychodus.api.scan import Scan, ScanPoint


class PtychoShelvesProductFileIO(ProductFileReader):
class PtychoShelvesProductFileReader(ProductFileReader):
SIMPLE_NAME: Final[str] = 'PtychoShelves'
DISPLAY_NAME: Final[str] = 'PtychoShelves Files (*.mat)'

Expand Down Expand Up @@ -56,13 +56,23 @@ def read(self, filePath: Path) -> Product:
)
scanPointList.append(point)

probe = Probe(
# probeMatrix[width, height, num_shared_modes, num_varying_modes]
matDict['probe'].transpose(),
pixel_geometry,
)
probe_array = matDict['probe']

if probe_array.ndim == 3:
# probe_array[height, width, num_shared_modes]
probe_array = probe_array.transpose(2, 0, 1)
elif probe_array.ndim == 4:
# probe_array[height, width, num_shared_modes, num_varying_modes]
probe_array = probe_array.transpose(3, 2, 0, 1)

probe = Probe(array=probe_array, pixelGeometry=pixel_geometry)

object_array = matDict['object']

if object_array.ndim == 3:
# object_array[height, width, num_layers]
object_array = object_array.transpose(2, 0, 1)

object_array = matDict['object'].transpose()
layer_distance_m: Sequence[float] = list()

try:
Expand All @@ -75,7 +85,6 @@ def read(self, filePath: Path) -> Product:
layer_distance_m = numpy.squeeze(z_distance)[:num_spaces]

object_ = Object(
# object[width, height, num_layers]
array=object_array,
pixelGeometry=pixel_geometry,
center=None,
Expand All @@ -92,91 +101,9 @@ def read(self, filePath: Path) -> Product:
)


class PtychoShelvesProbeFileReader(ProbeFileReader):
def read(self, filePath: Path) -> Probe:
matDict = scipy.io.loadmat(filePath)

# array[width, height, num_shared_modes, num_varying_modes]
array = matDict['probe']
p_struct = matDict['p']
dx_spec = p_struct['dx_spec']
pixel_width_m = dx_spec[0]
pixel_height_m = dx_spec[1]
pixel_geometry = PixelGeometry(widthInMeters=pixel_width_m, heightInMeters=pixel_height_m)

return Probe(array=array.transpose(), pixelGeometry=pixel_geometry)


class PtychoShelvesProbeFileWriter(ProbeFileWriter):
def write(self, filePath: Path, probe: Probe) -> None:
array = probe.getArray()
matDict = {'probe': array.transpose()}
scipy.io.savemat(filePath, matDict)


class PtychoShelvesObjectFileReader(ObjectFileReader):
def read(self, filePath: Path) -> Object:
matDict = scipy.io.loadmat(filePath)

# array[width, height, num_layers]
p_struct = matDict['p']
dx_spec = p_struct['dx_spec']
pixel_width_m = dx_spec[0]
pixel_height_m = dx_spec[1]
pixel_geometry = PixelGeometry(widthInMeters=pixel_width_m, heightInMeters=pixel_height_m)

object_array = matDict['object'].transpose()
layer_distance_m: Sequence[float] = list()

try:
multi_slice_param = p_struct['multi_slice_param']
z_distance = multi_slice_param['z_distance']
except KeyError:
pass
else:
num_spaces = object_array.shape[-3] - 1
layer_distance_m = numpy.squeeze(z_distance)[:num_spaces]

return Object(
# object[width, height, num_layers]
array=object_array,
pixelGeometry=pixel_geometry,
center=None,
layerDistanceInMeters=layer_distance_m,
)


class PtychoShelvesObjectFileWriter(ObjectFileWriter):
def write(self, filePath: Path, object_: Object) -> None:
array = object_.getArray()
matDict = {'object': array.transpose()}
# TODO layer distance to p.z_distance
scipy.io.savemat(filePath, matDict)


def registerPlugins(registry: PluginRegistry) -> None:
registry.productFileReaders.registerPlugin(
PtychoShelvesProductFileIO(),
simpleName=PtychoShelvesProductFileIO.SIMPLE_NAME,
displayName=PtychoShelvesProductFileIO.DISPLAY_NAME,
)
registry.probeFileReaders.registerPlugin(
PtychoShelvesProbeFileReader(),
simpleName=PtychoShelvesProductFileIO.SIMPLE_NAME,
displayName=PtychoShelvesProductFileIO.DISPLAY_NAME,
)
registry.probeFileWriters.registerPlugin(
PtychoShelvesProbeFileWriter(),
simpleName=PtychoShelvesProductFileIO.SIMPLE_NAME,
displayName=PtychoShelvesProductFileIO.DISPLAY_NAME,
)
registry.objectFileReaders.registerPlugin(
PtychoShelvesObjectFileReader(),
simpleName=PtychoShelvesProductFileIO.SIMPLE_NAME,
displayName=PtychoShelvesProductFileIO.DISPLAY_NAME,
)
registry.objectFileWriters.registerPlugin(
PtychoShelvesObjectFileWriter(),
simpleName=PtychoShelvesProductFileIO.SIMPLE_NAME,
displayName=PtychoShelvesProductFileIO.DISPLAY_NAME,
PtychoShelvesProductFileReader(),
simpleName=PtychoShelvesProductFileReader.SIMPLE_NAME,
displayName=PtychoShelvesProductFileReader.DISPLAY_NAME,
)

0 comments on commit 1fb3722

Please sign in to comment.