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

ENH: chxtools package update for 2.0.x #16

Merged
merged 8 commits into from
Dec 20, 2017
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
2 changes: 1 addition & 1 deletion chxtools/Time_Correlation_Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import sys
import time
import skxray.core.roi as roi
import skbeam.core.roi as roi
from matplotlib import gridspec

import itertools
Expand Down
238 changes: 126 additions & 112 deletions chxtools/chx_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
by LW 03/20/2015
some wrapper functions for ophyd
"""
import Tkinter, tkFileDialog
from __future__ import print_function


def det_select(det):
"""
Expand All @@ -16,19 +17,19 @@ def det_select(det):
calling sequence: det_select(det)
"""
try:
rm_det=ascan.user_detectors[0].name
ascan.user_detectors.remove(session_mgr[rm_det])
rm_det=ascan.user_detectors[0].name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using very old ophyd API that definitely no longer works. It took me a minute to even remember .user_detectors; I think it goes back to the days when the RunEngine was part of ophyd and there was no such thing as plans. @mrakitin Can you open an issue to revisit this function?

ascan.user_detectors.remove(session_mgr[rm_det])
ascan.default_triggers = []
ascan.default_detectors = []
print 'removed previous default detector: ',rm_det
except: print 'list of detectors appeared to be empty...'

print('removed previous default detector: {}'.format(rm_det))
except:
print('list of detectors appeared to be empty...')

ascan.user_detectors.append(det) # add detector
new_def_det=ascan.user_detectors[0].name

print ''
print 'new default detector: ',new_def_det

new_def_det=ascan.user_detectors[0].name

print('\nnew default detector: {}'.format(new_def_det))


def cw_ascan(mot,xmin,xmax,npoints,acqt='default',pos_ret=True):
"""
Expand All @@ -43,30 +44,32 @@ def cw_ascan(mot,xmin,xmax,npoints,acqt='default',pos_ret=True):
WILL NOT WORK FOR A LIST OF DETECTORS!
"""
# gather beamline information prior to starting the scan:
ini_motpos=caget(mot.record+'.RBV')
ini_motpos = caget(mot.record+'.RBV')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

caget shouldn't be used in a plan. @mrakitin Can you open an issue for this too?

# current detector:
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname

# set different exposure time for the scan, if requested:
if acqt!='default':
if acqt != 'default':
try:
ini_expt=caget(acq_pv[2]) # initial exposure time
ini_expt = caget(acq_pv[2]) # initial exposure time
session_mgr[ascan.user_detectors[0].name].acquire_time = acqt
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt

print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))

# execute the scan
ascan(mot,xmin,xmax,npoints)

# put beamline back into initial state
if pos_ret==True:
if pos_ret is True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pos_ret: is sufficient here.

caput(mot.record+'.VAL',ini_motpos)
print 'returned axes to: ',ini_motpos
if acqt!='default':
print('returned axes to: {}'.format(ini_motpos))
if acqt != 'default':
try:
session_mgr[ascan.user_detectors[0].name].acquire_time = ini_expt
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))

def cw_dscan(mot,mdx,pdx,npoints,acqt='default',pos_ret=True):
"""
Expand All @@ -78,25 +81,27 @@ def cw_dscan(mot,mdx,pdx,npoints,acqt='default',pos_ret=True):
WILL NOT WORK FOR A LIST OF DETECTORS!
"""
# current detector:
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

session_mgr is long-gone too. I guess we should just make a note to revisit this entire file. I suspect it hasn't been used in years, so we need to decide whether to delete it or overhaul it.


# set different exposure time for the scan, if requested:
if acqt!='default':
if acqt != 'default':
try:
ini_expt=caget(acq_pv[2]) # initial exposure time
session_mgr[ascan.user_detectors[0].name].acquire_time = acqt
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt

print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))

# execute the scan
dscan(mot,mdx,pdx,npoints)
#print 'finished scan'
if acqt!='default':
#print('finished scan')

if acqt != 'default':
try:
session_mgr[ascan.user_detectors[0].name].acquire_time = ini_expt
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))

def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default',acqperiod='default'):
"""
Expand All @@ -118,7 +123,7 @@ def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default
# get the detector name::
detector.pvname.split('}')[0]+'}'
#get folder interactively:

if folder == 'ia':
root=Tkinter.Tk()
root.withdraw()
Expand All @@ -127,116 +132,125 @@ def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default
# check whether target directory exists and create it, if it doesn't
if not os.path.exists(folder):
os.mkdir(folder)
os.chmod(folder,436) #make sure everybody can read an write
os.chmod(folder,436) #make sure everybody can read an write
os.chmod(folder,stat.S_IRWXO)
print 'successfully created new directory: ',folder


print('successfully created new directory: {}'.format(folder))

# put folder:
r=caput(detector.pvname.split('}')[0]+'}TIFF1:FilePath', folder) # have some problem with syntax here...
if r==1:
print 'changed saving directory to: ',folder
else: print 'error: could not change directory for saving data files.'
if r == 1:
print('changed saving directory to: {}'.format(folder))
else:
print('error: could not change directory for saving data files.')
# put the filename:
r=caput(detector.pvname.split('}')[0]+'}TIFF1:FileName', filename) # have some problem with syntax here...
if r==1:
print 'filename for saving: ',filename
else: print 'error: could not change file name for saving.'
if r == 1:
print('filename for saving: {}'.format(filename))
else:
print('error: could not change file name for saving.')


# put start number
caput(detector.pvname.split('}')[0]+'}TIFF1:FileNumber',startn)

#gather information about current camera settings
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
ini_acq=caget(acq_pv.split('}')[0]+'}cam1:Acquire') # initial state: started or stopped
ini_mode=caget(acq_pv.split('}')[0]+'}cam1:ImageMode') # initial image mode: single [0], multiple [1], continous [2]
ini_expt=caget(acq_pv.split('}')[0]+'}cam1:AcquireTime') # initial exposure time
ini_acqperiod=caget(acq_pv.split('}')[0]+'}cam1:AcquirePeriod') # initial acquiring period
ini_imnum=caget(acq_pv.split('}')[0]+'}cam1:NumImages') # initial image number
if acqt!='default':
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname
ini_acq = caget(acq_pv.split('}')[0]+'}cam1:Acquire') # initial state: started or stopped
ini_mode = caget(acq_pv.split('}')[0]+'}cam1:ImageMode') # initial image mode: single [0], multiple [1], continous [2]
ini_expt = caget(acq_pv.split('}')[0]+'}cam1:AcquireTime') # initial exposure time
ini_acqperiod = caget(acq_pv.split('}')[0]+'}cam1:AcquirePeriod') # initial acquiring period
ini_imnum = caget(acq_pv.split('}')[0]+'}cam1:NumImages') # initial image number

if acqt != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquireTime',acqt)
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt
print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))
# stop camara:
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0)
print 'successfully stopped camera'
except: print 'could not stop camera'
print('successfully stopped camera')
except:
print('could not stop camera')
# try to set image mode to multiple
try:
caput(acq_pv.split('}')[0]+'}cam1:ImageMode',1)
print 'successfully set ImageMode to "multiple"'
except: print 'could not set ImageMode to "multiple"'
if acqperiod!='default':
try:
print('successfully set ImageMode to "multiple"')
except:
print('could not set ImageMode to "multiple"')
if acqperiod != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod',acqperiod)
print 'successfully set acquiering period to: ',acqperiod
except: print 'could not set aquireing period to ',acqperiod
print('successfully set acquiering period to: {}'.format(acqperiod))
except:
print('could not set aquireing period to {}'.format(acqperiod))

# set number of images to be taken:
if imnum!='default':
if imnum != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:NumImages',imnum)
print 'successfully set number of images to: ',imnum
except: print 'could not set number of images to ',imnum
print 'going to start the acquisition...'
caput(acq_pv.split('}')[0]+'}cam1:NumImages', imnum)
print('successfully set number of images to: {}'.format(imnum))
except:
print('could not set number of images to {}'.format(imnum))
print('going to start the acquisition...')
time.sleep(1)
# start the acquisition!!!
caput(acq_pv.split('}')[0]+'}cam1:ArrayCounter',0)#set Images acquired (image counter) to 0
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',1) # start auto save
caput(acq_pv.split('}')[0]+'}cam1:Acquire',1) # start series acquisition
caput(acq_pv.split('}')[0]+'}cam1:ArrayCounter',0) # set Images acquired (image counter) to 0

caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',1) # start auto save
caput(acq_pv.split('}')[0]+'}cam1:Acquire',1) # start series acquisition

counting=0
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
while counting==0:
time.sleep(.5)
if caget(acq_pv.split('}')[0]+'}cam1:ArrayCounter_RBV')==imnum: counting=1
if caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)!=current_file:
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
print 'file written: ',current_file
time.sleep(.1)

print 'going to stop the acquisition...'
time.sleep(.5)
if caget(acq_pv.split('}')[0]+'}cam1:ArrayCounter_RBV') == imnum:
counting=1
if caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True) != current_file:
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
print('file written: {}'.format(current_file))
time.sleep(.1)

print('going to stop the acquisition...')
time.sleep(.5)
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0) # stop series acquisition (redundent...should have stopped already
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',0) # stop auto save
print 'going to reset camera...'
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0) # stop series acquisition (redundent...should have stopped already
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',0) # stop auto save

print('going to reset camera...')
#time.sleep(5)
# put camera back into initial state:
if acqt!='default':
if acqt != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquireTime',ini_expt)
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))
try:
caput(acq_pv.split('}')[0]+'}cam1:ImageMode',ini_mode)
print 'successfully reset ImageMode'
except: print 'could not reset ImageMode'
caput(acq_pv.split('}')[0]+'}cam1:ImageMode', ini_mode)
print('successfully reset ImageMode')
except:
print('could not reset ImageMode')
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',ini_acq)
print 'successfully reset camera acqusition mode'
except: print 'could not reset camera acquisition mode'
caput(acq_pv.split('}')[0]+'}cam1:Acquire', ini_acq)
print('successfully reset camera acqusition mode')
except:
print('could not reset camera acquisition mode')
if acqperiod!='default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod',ini_acqperiod)
print 'successfully reset acquisition period to [s]: ',ini_acqperiod
except: print 'could not reset acquisition period to ',ini_acqperiod
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod', ini_acqperiod)
print('successfully reset acquisition period to [s]: {}'.format(ini_acqperiod))
except:
print('could not reset acquisition period to {}'.format(ini_acqperiod))
if imnum!='default':
try:
caput(acq_pv.split('}')[0]+'}cam1:NumImages',ini_imnum)
print 'successfully reset image numbers to: ',ini_imnum
except: print 'could not reset image numbers to ',ini_imnum
caput(acq_pv.split('}')[0]+'}cam1:NumImages', ini_imnum)
print('successfully reset image numbers to: {}'.format(ini_imnum))
except:
print('could not reset image numbers to {}'.format(ini_imnum))
time.sleep(.5)
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',ini_acq) # restart camera if it was running before taking the series
print 'restarted camera'
except: print 'could not restart camera...'





caput(acq_pv.split('}')[0]+'}cam1:Acquire', ini_acq) # restart camera if it was running before taking the series
print('restarted camera')
except:
print('could not restart camera...')

14 changes: 9 additions & 5 deletions chxtools/handlers2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import h5py
from pims import FramesSequence, Frame
from databroker import db
from databroker import Broker
from .handlers import LazyEigerHandler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventually, should be
from eiger_io.fs_handler import EigerHandler
for PIMS version
or
from eiger_io.fs_handler_dask import EigerDaskHandler
for dask version.

But let's keep it as is for now. There are some things I need to check back with Yugang on.

Just to summarize what needs to be fixed with this:

  1. The handlers that treat the AD_EIGER and AD_EIGER2 specs should be combined into one. The only difference between the two is that the keyword argument is a different name. (Other that that API is same as far as I can tell and I've already tested this on all versions of EIGER files, of which there are four in CHX currently)
  2. The EigerImages pims subclass (which is created by the handler per event) contains a lot of metadata which is obtained by reading from the hdf5 file directly. We should discourage that and try to make sure CHX accesses this data directly from metadatastore. Most of this data should be in a descriptor configuration. I want to discuss with Yugang and try my best to make sure we weed this out, and make a long term plan for deprecating this use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



class EigerImages2(FramesSequence):
Expand Down Expand Up @@ -96,8 +97,11 @@ def __call__(self, seq_id):
# TODO Return a multi-dimensional PIMS seq.
return EigerImages2(master_path, self._images_per_file, md=md)

db.fs.deregister_handler('AD_EIGER')
db.fs.deregister_handler('AD_EIGER2')
db = Broker.named('chx')

db.reg.deregister_handler('AD_EIGER')
db.reg.deregister_handler('AD_EIGER2')

db.reg.register_handler('AD_EIGER2', EigerHandler2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could remove the previous two lines and add the overwrite=True flag to this line and bellow. Maybe we should get @danielballan 's blessing first ;-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blessed!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

db.reg.register_handler('AD_EIGER', LazyEigerHandler)

db.fs.register_handler('AD_EIGER2', EigerHandler2)
db.fs.register_handler('AD_EIGER', LazyEigerHandler)
4 changes: 2 additions & 2 deletions chxtools/speckle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import numpy as np
import time

from skxray.core import roi
from skxray.core.utils import bin_edges_to_centers, geometric_series
from skbeam.core import roi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some history: scikit-xray was renamed to scikit-beam a while ago. So just substituting the name is perfect.

from skbeam.core.utils import bin_edges_to_centers, geometric_series

import logging
logger = logging.getLogger(__name__)
Expand Down
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
atom
cython
databroker
enaml
fabio
h5py
matplotlib
numpy
ophyd
pandas
pims
pyepics
scikit-beam
scipy

Loading