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

Enable manually specifying detector and motor names #189

Merged
merged 1 commit into from
Nov 30, 2023
Merged
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
109 changes: 93 additions & 16 deletions ImageD11/sinograms/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
- indexing and reconstructing grains

"""

# POSSIBLE_DETECTOR_NAMES = ("frelon3", "eiger")

def guess_chunks(name, shape):
if name == 'omega':
Expand All @@ -30,28 +32,36 @@ def guess_chunks(name, shape):


class DataSet:
"""One DataSet instance per detector!"""

# simple strings or ints
ATTRNAMES = ( "dataroot", "analysisroot", "sample", "dset", "shape", "dsname",
"datapath", "analysispath", "masterfile",
"limapath"
ATTRNAMES = ( "dataroot", "analysisroot", "sample", "dset", "shape", "dsname",
"datapath", "analysispath", "masterfile", "limapath",
"detector", "omegamotor", "dtymotor",
"pksfile", "sparsefile", "parfile"
)
STRINGLISTS = ( "scans", "imagefiles", "sparsefiles" )
# sinograms
NDNAMES = ( "omega", "dty", "nnz", "frames_per_file", "nlm", "frames_per_scan" )



def __init__(self,
dataroot = ".",
analysisroot = ".",
sample = "sample",
dset = "dataset"):
dset = "dataset",
detector = "eiger",
omegamotor = "rot_center",
dtymotor = "dty"):
""" The things we need to know to process data """

self.detector = 'eiger' # frelon3

# defaults to eiger and nanoscope station, can be overwritten with init parameters detector, omegamotor and dtymotor
self.detector = detector # frelon3
self.limapath = None # where is the data in the Lima files

self.omegamotor = 'rot_center' # diffrz
self.dtymotor = 'dty' # diffty
self.omegamotor = omegamotor # diffrz
self.dtymotor = dtymotor # diffty

self.dataroot = dataroot # folder to find {sample}/{sample}_{dset}
self.analysisroot = analysisroot # where to write or find sparse data
Expand Down Expand Up @@ -85,7 +95,7 @@ def __repr__(self):
r.append( '# scans %d from %s to %s'%(
len(self.scans), self.scans[0], self.scans[-1] ))
return "\n".join(r)


def compare(self, other):
'''Try to see if the load/save is working'''
Expand Down Expand Up @@ -144,6 +154,8 @@ def import_all(self, scans=None, shape=None):

def import_scans(self, scans=None, hname = None):
""" Reads in the scans from the bliss master file """
# we need to work out what detector we have at this point
# self.guess_detector()
if hname is None:
hname = self.masterfile
frames_per_scan = []
Expand All @@ -155,12 +167,22 @@ def import_scans(self, scans=None, hname = None):
(self.detector in hin[scan]['measurement'])) ]
goodscans = []
for scan in scans:
frames = hin[scan]['measurement'][self.detector]
if len(frames.shape)==3: # need 1D series of frames
goodscans.append(scan)
frames_per_scan.append( frames.shape[0] )
# Make sure that this scan has a measurement from our detector
if self.detector not in hin[scan]['measurement']:
print('Bad scan', scan)
else:
print('Bad scan', scan)
try:
frames = hin[scan]['measurement'][self.detector]
except KeyError as e: # Thrown by h5py
print('Bad scan', scan, ', h5py error follows:')
print(e)
continue
if len(frames.shape)==3: # need 1D series of frames
goodscans.append(scan)
frames_per_scan.append( frames.shape[0] )
else:
print('Bad scan', scan)

self.scans = goodscans
self.frames_per_scan = frames_per_scan

Expand All @@ -170,6 +192,7 @@ def import_scans(self, scans=None, hname = None):

def import_imagefiles(self):
""" Get the Lima file names from the bliss master file, also scan_npoints """
# self.import_scans() should always be called before this function, so we know the detector
npts = None
self.imagefiles = []
self.frames_per_file = []
Expand Down Expand Up @@ -200,6 +223,7 @@ def import_motors_from_master(self): # could also get these from sparse files
you need to import the imagefiles first
these will be the motor positions to accompany the images
"""
# self.guess_motornames()
self.omega = [None,] * len(self.scans)
self.dty = [None,] * len(self.scans)
with h5py.File( self.masterfile, 'r' ) as hin:
Expand Down Expand Up @@ -294,6 +318,57 @@ def guessbins(self):
self.obinedges = np.linspace( self.omin-self.ostep/2, self.omax + self.ostep/2, nomega + 1 )
self.ybinedges = np.linspace( self.ymin-self.ystep/2, self.ymax + self.ystep/2, ny + 1 )

def guess_detector(self):
'''Guess which detector we are using from the masterfile'''

# open the masterfile
hname = self.masterfile
detectors_seen = []
scan = "1.1"

with h5py.File( hname, 'r' ) as hin:
# go through the first scan, and see what detectors we can see
for measurement in list(hin[scan]['measurement']):
if measurement.attrs.get("interpretation") == "image":
detectors_seen.append(measurement)

if len(detectors_seen) != 1:
raise ValueError("More than one detector seen! Can't work out which one to process.")
else:
self.detector = detectors_seen[0]

# def guess_motornames(self):
# '''Guess which station we were using (Nanoscope or 3DXRD) from which motors are in instrument/positioners'''
# from ImageD11.sinograms.assemble_label import HEADERMOTORS_NSCOPE, HEADERMOTORS_TDXRD, HEADERMOTORS
# # open the masterfile
# hname = self.masterfile
# motors_seen = []
# scan = "1.1"

# with h5py.File( hname, 'r' ) as hin:
# # go through the first scan, and see what motors we can see
# for positioner in list(hin[scan]['instrument/positioners']):
# if positioner in HEADERMOTORS:
# motors_seen.append(positioner)

# using_nscope = False
# using_tdxrd = False
# for motor in motors_seen:
# if motor in HEADERMOTORS_NSCOPE:
# using_nscope = True
# elif motor in HEADERMOTORS_TDXRD:
# using_tdxrd = True

# if using_nscope and using_tdxrd:
# raise ValueError("Found both nscope and tdxrd motors in positioners, not sure which one we were using!")

# if using_nscope:
# self.omegamotor = 'rot_center'
# self.dtymotor = 'dty'
# elif using_tdxrd:
# self.omegamotor = 'diffrz'
# self.dtymotor = 'diffty'


def sinohist(self, weights=None, omega=None, dty=None, method='fast'):
""" Bin some data onto the sinogram histogram """
Expand Down Expand Up @@ -446,7 +521,9 @@ def load( self, h5name, h5group = '/' ):


def load( h5name, h5group = '/' ):
return DataSet().load( h5name, h5group )
ds_obj = DataSet().load( h5name, h5group )

return ds_obj

def import_from_sparse( hname,
omegamotor='instrument/positioners/rot',
Expand Down
Loading