Skip to content

Commit

Permalink
misc requested fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehenke committed Dec 12, 2024
1 parent 36d66a9 commit 41ddb17
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/ptychodus/controller/ptychi/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ def __init__(
layout.addRow('Batch Size:', self._batchSizeViewController.getWidget())
layout.addRow('Batch Mode:', self._batchingModeViewController.getWidget())
layout.addRow('Batch Stride:', self._batchStride.getWidget())
layout.addRow('Precision:', self._precisionViewController.getWidget())
layout.addRow(self._useLowMemoryViewController.getWidget())

if repository:
layout.addRow(self._deviceViewController.getWidget())

layout.addRow('Precision:', self._precisionViewController.getWidget())
layout.addRow(self._useLowMemoryViewController.getWidget())

self._widget.setLayout(layout)

def getWidget(self) -> QWidget:
Expand Down
4 changes: 4 additions & 0 deletions src/ptychodus/model/ptychi/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Sequence
from importlib.metadata import version
from typing import overload
import logging

Expand All @@ -15,6 +16,9 @@ def __init__(self, *, isDeveloperModeEnabled: bool) -> None:
if isDeveloperModeEnabled:
self._devices.extend(f'gpu:{n}' for n in range(4))
else:
ptychiVersion = version('ptychi')
logger.info(f'\tPty-Chi {ptychiVersion}')

for device in ptychi.list_available_devices():
logger.info(device)
self._devices.append(f'{device.name} ({device.torch_device})')
Expand Down
Empty file.
88 changes: 88 additions & 0 deletions src/ptychodus/plugins/csaxsDiffractionFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from pathlib import Path
from typing import Final
import logging

import h5py

from ptychodus.api.geometry import ImageExtent, PixelGeometry
from ptychodus.api.patterns import (
DiffractionDataset,
DiffractionFileReader,
DiffractionMetadata,
SimpleDiffractionDataset,
)
from ptychodus.api.plugins import PluginRegistry

from .h5DiffractionFile import H5DiffractionPatternArray, H5DiffractionFileTreeBuilder

logger = logging.getLogger(__name__)


class CSAXSDiffractionFileReader(DiffractionFileReader):
SIMPLE_NAME: Final[str] = 'SLS_cSAXS'
DISPLAY_NAME: Final[str] = 'SLS cSAXS Diffraction Files (*.h5 *.hdf5)'
ONE_MICRON_M: Final[float] = 1e-6
ONE_MILLIMETER_M: Final[float] = 1e-3

def __init__(self) -> None:
self._dataPath = '/entry/data/data'
self._treeBuilder = H5DiffractionFileTreeBuilder()

def read(self, filePath: Path) -> DiffractionDataset:
dataset = SimpleDiffractionDataset.createNullInstance(filePath)

try:
with h5py.File(filePath, 'r') as h5File:
contentsTree = self._treeBuilder.build(h5File)

try:
data = h5File[self._dataPath]
x_pixel_size_um = h5File['/entry/instrument/eiger_4/x_pixel_size']
y_pixel_size_um = h5File['/entry/instrument/eiger_4/y_pixel_size']
distance_mm = h5File['/entry/instrument/monochromator/distance']
energy_keV = h5File['/entry/instrument/monochromator/energy']
except KeyError:
logger.warning('Unable to load data.')
else:
numberOfPatterns, detectorHeight, detectorWidth = data.shape
detectorDistanceInMeters = float(distance_mm[()]) * self.ONE_MILLIMETER_M
detectorPixelGeometry = PixelGeometry(
widthInMeters=float(x_pixel_size_um[()]) * self.ONE_MICRON_M,
heightInMeters=float(y_pixel_size_um[()]) * self.ONE_MICRON_M,
)
probeEnergyInElectronVolts = 1000 * float(energy_keV[()])

metadata = DiffractionMetadata(
numberOfPatternsPerArray=numberOfPatterns,
numberOfPatternsTotal=numberOfPatterns,
patternDataType=data.dtype,
detectorDistanceInMeters=abs(detectorDistanceInMeters),
detectorExtent=ImageExtent(detectorWidth, detectorHeight),
detectorPixelGeometry=detectorPixelGeometry,
probeEnergyInElectronVolts=probeEnergyInElectronVolts,
filePath=filePath,
)

print(metadata.detectorDistanceInMeters) # FIXME
print(repr(metadata)) # FIXME

array = H5DiffractionPatternArray(
label=filePath.stem,
index=0,
filePath=filePath,
dataPath=self._dataPath,
)

dataset = SimpleDiffractionDataset(metadata, contentsTree, [array])
except OSError:
logger.warning(f'Unable to read file "{filePath}".')

return dataset


def registerPlugins(registry: PluginRegistry) -> None:
registry.diffractionFileReaders.registerPlugin(
CSAXSDiffractionFileReader(),
simpleName=CSAXSDiffractionFileReader.SIMPLE_NAME,
displayName=CSAXSDiffractionFileReader.DISPLAY_NAME,
)
5 changes: 0 additions & 5 deletions src/ptychodus/plugins/h5DiffractionFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,6 @@ 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='MAX_IV_NanoMax',
Expand Down
9 changes: 8 additions & 1 deletion src/ptychodus/plugins/h5ProductFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ def read(self, filePath: Path) -> Product:
scanPointList: list[ScanPoint] = list()

with h5py.File(filePath, 'r') as h5File:
probePhotonCount = 0.0

try:
probePhotonCount = float(h5File.attrs[self.PROBE_PHOTON_COUNT])
except KeyError:
logger.debug('Probe photon count not found.')

metadata = ProductMetadata(
name=str(h5File.attrs[self.NAME]),
comments=str(h5File.attrs[self.COMMENTS]),
detectorDistanceInMeters=float(h5File.attrs[self.DETECTOR_OBJECT_DISTANCE]),
probeEnergyInElectronVolts=float(h5File.attrs[self.PROBE_ENERGY]),
probePhotonCount=float(h5File.attrs[self.PROBE_PHOTON_COUNT]),
probePhotonCount=probePhotonCount,
exposureTimeInSeconds=float(h5File.attrs[self.EXPOSURE_TIME]),
)

Expand Down
12 changes: 11 additions & 1 deletion src/ptychodus/plugins/npzProductFile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Any, Final
import logging

import numpy

Expand All @@ -15,6 +16,8 @@
)
from ptychodus.api.scan import Scan, ScanFileReader, ScanPoint

logger = logging.getLogger(__name__)


class NPZProductFileIO(ProductFileReader, ProductFileWriter):
SIMPLE_NAME: Final[str] = 'NPZ'
Expand Down Expand Up @@ -45,12 +48,19 @@ class NPZProductFileIO(ProductFileReader, ProductFileWriter):

def read(self, filePath: Path) -> Product:
with numpy.load(filePath) as npzFile:
probePhotonCount = 0.0

try:
probePhotonCount = float(npzFile[self.PROBE_PHOTON_COUNT])
except KeyError:
logger.debug('Probe photon count not found.')

metadata = ProductMetadata(
name=str(npzFile[self.NAME]),
comments=str(npzFile[self.COMMENTS]),
detectorDistanceInMeters=float(npzFile[self.DETECTOR_OBJECT_DISTANCE]),
probeEnergyInElectronVolts=float(npzFile[self.PROBE_ENERGY]),
probePhotonCount=float(npzFile[self.PROBE_PHOTON_COUNT]),
probePhotonCount=probePhotonCount,
exposureTimeInSeconds=float(npzFile[self.EXPOSURE_TIME]),
)

Expand Down

0 comments on commit 41ddb17

Please sign in to comment.