Beispiel #1
0
#######################
kwargs = dict()  # create dictionnary
try:
    kwargs['nb_pixel_x'] = nb_pixel_x  # fix to declare a known detector but with less pixels (e.g. one tile HS)
except NameError:  # nb_pixel_x not declared
    pass
try:
    kwargs['nb_pixel_y'] = nb_pixel_y  # fix to declare a known detector but with less pixels (e.g. one tile HS)
except NameError:  # nb_pixel_y not declared
    pass
try:
    kwargs['is_series'] = is_series
except NameError:  # is_series not declared
    pass

detector = exp.Detector(name=detector, datadir='', template_imagefile=template_imagefile, roi=roi_detector,
                        binning=binning, **kwargs)

####################
# Initialize setup #
####################
setup = exp.SetupPreprocessing(beamline=beamline, energy=energy, rocking_angle=rocking_angle, distance=sdd,
                               beam_direction=beam_direction, sample_inplane=sample_inplane,
                               sample_outofplane=sample_outofplane, offset_inplane=offset_inplane,
                               custom_scan=custom_scan, custom_images=custom_images, sample_offsets=sample_offsets,
                               custom_monitor=custom_monitor, custom_motors=custom_motors)

#############################################
# Initialize geometry for orthogonalization #
#############################################
if rocking_angle == "energy":
    use_rawdata = False  # you need to interpolate the data in QxQyQz for energy scans
Beispiel #2
0
debug = False  # True to show more plots
save = True  # True to save the prtf figure
##########################
# end of user parameters #
##########################

#################################################
# Initialize paths, detector, setup and logfile #
#################################################
kwargs = dict()  # create dictionnary
try:
    kwargs['is_series'] = is_series
except NameError:  # is_series not declared
    pass

detector = exp.Detector(name=detector, datadir='', template_imagefile=template_imagefile, binning=pre_binning, **kwargs)

setup = exp.SetupPreprocessing(beamline=beamline, rocking_angle=rocking_angle, distance=sdd, energy=energy,
                               beam_direction=beam_direction, sample_inplane=sample_inplane,
                               sample_outofplane=sample_outofplane,
                               offset_inplane=0)  # no need to worry about offsets, work relatively to the Bragg peak

print('\nScan', scan)
print('Setup: ', setup.beamline)
print('Detector: ', detector.name)
print('Pixel sizes after pre_binning (vertical, horizontal): ', detector.pixelsize_y, detector.pixelsize_x, '(m)')
print('Scan type: ', setup.rocking_angle)
print('Sample to detector distance: ', setup.distance, 'm')
print('Energy:', setup.energy, 'ev')

if simulation:
Beispiel #3
0
]
# [Vstart, Vstop, Hstart, Hstop]
binning = [4, 4, 4]  # binning of the detector
###########
# options #
###########
kernel_length = 21  # width of the 3D gaussian window
debug = False  # True to see more plots
##################################
# end of user-defined parameters #
##################################

#######################
# Initialize detector #
#######################
detector = exp.Detector(name=detector, binning=binning, roi=roi_detector)

nbz, nby, nbx = int(np.floor((detector.roi[3] - detector.roi[2]) / detector.binning[2])), \
                   int(np.floor((detector.roi[1] - detector.roi[0]) / detector.binning[1])), \
                   int(np.floor((detector.roi[3] - detector.roi[2]) / detector.binning[2]))
# for P10 data the rotation is around y vertical, hence gridded data range & binning in z and x are identical

###################
# define colormap #
###################
bad_color = '1.0'  # white background
colormap = gu.Colormap(bad_color=bad_color)
my_cmap = colormap.cmap
plt.ion()

######################
directbeam_x = 476  # x horizontal,  cch2 in xrayutilities
directbeam_y = 1375  # y vertical,  cch1 in xrayutilities
direct_inplane = -2.0  # outer angle in xrayutilities
direct_outofplane = 0.8
sdd = 1.839  # sample to detector distance in m
energy = 10300  # in eV, offset of 6eV at ID01
##########################################################
# end of user parameters
##########################################################

plt.ion()
#######################
# Initialize detector #
#######################
detector = exp.Detector(name=detector,
                        datadir='',
                        template_imagefile=template_imagefile,
                        roi=roi_detector)

####################
# Initialize setup #
####################
setup_pre = exp.SetupPreprocessing(beamline=beamline,
                                   rocking_angle=rocking_angle,
                                   distance=sdd,
                                   energy=energy,
                                   beam_direction=beam_direction)

if setup_pre.beamline != 'P10':
    homedir = root_folder + sample_name + str(scan) + '/'
    detector.datadir = homedir + "data/"
else:
Beispiel #5
0
roi = []  # plot the integrated intensity in this region of interest. Leave it to [] to use the full detector
# [Vstart, Vstop, Hstart, Hstop]
high_threshold = 9  # data points where log10(data) > high_threshold will be masked
# if data is a series, the condition becomes log10(data.sum(axis=0)) > high_threshold
savedir = '/mxn/visitors/dzhigd/NanoMAX_beamtimes/24062020/data_processing/'+str('{:04d}'.format(scan))  # images will be saved here, leave it to '' otherwise (default to data directory's parent)
is_scan = True  # set to True is the measurement is a scan or a time series, False for a single image
compare_ends = True  # set to True to plot the difference between the last frame and the first frame
save_mask = False  # True to save the mask as 'hotpixels.npz'
##########################
# end of user parameters #
##########################

#######################
# Initialize detector #
#######################
detector = exp.Detector(name=detector)
nb_pixel_y, nb_pixel_x = detector.nb_pixel_y, detector.nb_pixel_x

####################
# Initialize paths #
####################
datadir = rootdir + sample_name

if savedir == '':
    savedir = os.path.abspath(os.path.join(datadir, os.pardir)) + '/'

#############
# Load data #
#############
plt.ion() # interactive graphics
mask = np.zeros((nb_pixel_y, nb_pixel_x))
Beispiel #6
0
def main(parameters):
    """
    Protection for multiprocessing.

    :param parameters: dictionnary containing input parameters
    """

    def collect_result(result):
        """
        Callback processing the result after asynchronous multiprocessing. Update the global arrays.

        :param result: the output of load_p10_file, containing the 2d data, 2d mask, counter for each frame, and the
         file index
        """
        nonlocal sumdata, mask, counter, nb_files, current_point
        # result is a tuple: data, mask, counter, file_index
        current_point += 1
        sumdata = sumdata + result[0]
        mask[np.nonzero(result[1])] = 1
        counter.append(result[2])

        sys.stdout.write('\rFile {:d} / {:d}'.format(current_point, nb_files))
        sys.stdout.flush()

    ######################################
    # load the dictionnary of parameters #
    ######################################
    scan = parameters['scan']
    samplename = parameters['sample_name']
    rootdir = parameters['rootdir']
    image_nb = parameters['file_list']
    counterroi = parameters['counter_roi']
    savedir = parameters['savedir']
    load_scan = parameters['is_scan']
    compare_end = parameters['compare_ends']
    savemask = parameters['save_mask']
    multiproc = parameters['multiprocessing']
    threshold = parameters['threshold']
    cb_min = parameters['cb_min']
    cb_max = parameters['cb_max']
    grey_bckg = parameters['grey_bckg']
    ###################
    # define colormap #
    ###################
    if grey_bckg:
        bad_color = '0.7'
    else:
        bad_color = '1.0'  # white background
    colormap = gu.Colormap(bad_color=bad_color)
    my_cmap = colormap.cmap

    #######################
    # Initialize detector #
    #######################
    detector = exp.Detector(name=parameters['detector'])
    nb_pixel_y, nb_pixel_x = detector.nb_pixel_y, detector.nb_pixel_x
    sumdata = np.zeros((nb_pixel_y, nb_pixel_x))
    mask = np.zeros((nb_pixel_y, nb_pixel_x))
    counter = []

    ####################
    # Initialize paths #
    ####################
    if type(image_nb) == int:
        image_nb = [image_nb]
    if len(counterroi) == 0:
        counterroi = [0, nb_pixel_y, 0, nb_pixel_x]

    assert (counterroi[0] >= 0
            and counterroi[1] <= nb_pixel_y
            and counterroi[2] >= 0
            and counterroi[3] <= nb_pixel_x), 'counter_roi setting does not match the detector size'
    nb_files = len(image_nb)
    if nb_files == 1:
        multiproc = False

    if load_scan:  # scan or time series
        detector.datadir = rootdir + samplename + '_' + str('{:05d}'.format(scan)) + '/e4m/'
        template_file = detector.datadir + samplename + '_' + str('{:05d}'.format(scan)) + "_data_"
    else:  # single image
        detector.datadir = rootdir + samplename + '/e4m/'
        template_file = detector.datadir + samplename + '_take_' + str('{:05d}'.format(scan)) + "_data_"
        compare_end = False

    detector.savedir = savedir or os.path.abspath(os.path.join(detector.datadir, os.pardir)) + '/'
    print(f'datadir: {detector.datadir}')
    print(f'savedir: {detector.savedir}')

    #############
    # Load data #
    #############
    plt.ion()
    filenames = [template_file + '{:06d}.h5'.format(image_nb[idx]) for idx in range(nb_files)]
    roi_counter = None
    current_point = 0
    start = time.time()

    if multiproc:
        print("\nNumber of processors used: ", min(mp.cpu_count(), len(filenames)))
        mp.freeze_support()
        pool = mp.Pool(processes=min(mp.cpu_count(), len(filenames)))  # use this number of processes

        for file in range(nb_files):
            pool.apply_async(load_p10_file, args=(detector, filenames[file], file, counterroi, threshold),
                             callback=collect_result, error_callback=util.catch_error)

        pool.close()
        pool.join()  # postpones the execution of next line of code until all processes in the queue are done.

        # sort out counter values (we are using asynchronous multiprocessing, order is not preserved)
        roi_counter = sorted(counter, key=lambda x: x[1])

    else:
        for idx in range(nb_files):
            sys.stdout.write('\rLoading file {:d}'.format(idx + 1) + ' / {:d}'.format(nb_files))
            sys.stdout.flush()
            h5file = h5py.File(filenames[idx], 'r')
            data = h5file['entry']['data']['data'][:]
            data[data <= threshold] = 0
            nbz, nby, nbx = data.shape
            [counter.append(data[index, counterroi[0]:counterroi[1], counterroi[2]:counterroi[3]].sum())
                for index in range(nbz)]
            if compare_end and nb_files == 1:
                data_start, _ = detector.mask_detector(data=data[0, :, :], mask=mask)
                data_start = data_start.astype(float)
                data_stop, _ = detector.mask_detector(data=data[-1, :, :], mask=mask)
                data_stop = data_stop.astype(float)

                fig, _, _ = gu.imshow_plot(data_stop - data_start, plot_colorbar=True, scale='log',
                                           title='difference between the last frame and the first frame of the series')
            nb_frames = data.shape[0]  # collect the number of frames in the eventual series
            data, mask = detector.mask_detector(data=data.sum(axis=0), mask=mask, nb_img=nb_frames)
            sumdata = sumdata + data
            roi_counter = [[counter, idx]]

    end = time.time()
    print('\nTime ellapsed for loading data:', str(datetime.timedelta(seconds=int(end - start))))

    frame_per_series = int(len(counter) / nb_files)

    print('')
    if load_scan:
        if nb_files > 1:
            plot_title = 'masked data - sum of ' + str(nb_files)\
                         + ' points with {:d} frames each'.format(frame_per_series)
        else:
            plot_title = 'masked data - sum of ' + str(frame_per_series) + ' frames'
        filename = 'S' + str(scan) + '_scan.png'
    else:  # single image
        plot_title = 'masked data'
        filename = 'S' + str(scan) + '_image_' + str(image_nb[0]) + '.png'

    if savemask:
        fig, _, _ = gu.imshow_plot(mask, plot_colorbar=False, title='mask')
        np.savez_compressed(detector.savedir+'hotpixels.npz', mask=mask)
        fig.savefig(detector.savedir + 'mask.png')

    y0, x0 = np.unravel_index(abs(sumdata).argmax(), sumdata.shape)
    print("Max at (y, x): ", y0, x0, ' Max = ', int(sumdata[y0, x0]))

    np.savez_compressed(detector.savedir + f'{sample_name}_{scan_nb:05d}_sumdata.npz', data=sumdata)
    if save_to_mat:
        savemat(detector.savedir + f'{sample_name}_{scan_nb:05d}_sumdata.mat', {'data': sumdata})

    if len(roi_counter[0][0]) > 1:  # roi_counter[0][0] is the list of counter intensities in a series
        int_roi = []
        [int_roi.append(val[0][idx]) for val in roi_counter for idx in range(frame_per_series)]
        plt.figure()
        plt.plot(np.asarray(int_roi))
        plt.title('Integrated intensity in counter_roi')
        plt.pause(0.1)

    cb_min = cb_min or sumdata.min()
    cb_max = cb_max or sumdata.max()

    fig, _, _ = gu.imshow_plot(sumdata, plot_colorbar=True, title=plot_title, vmin=cb_min, vmax=cb_max, scale='log',
                               cmap=my_cmap)
    np.savez_compressed(detector.savedir + 'hotpixels.npz', mask=mask)
    fig.savefig(detector.savedir + filename)
    plt.show()
Beispiel #7
0
# define colormap #
###################
colormap = gu.Colormap()
my_cmap = colormap.cmap
plt.rcParams["keymap.fullscreen"] = [""]

#######################
# Initialize detector #
#######################
kwargs = dict()  # create dictionnary
kwargs['is_series'] = is_series
kwargs['preprocessing_binning'] = preprocessing_binning
kwargs['nb_pixel_x'] = nb_pixel_x  # fix to declare a known detector but with less pixels (e.g. one tile HS)
kwargs['nb_pixel_y'] = nb_pixel_y  # fix to declare a known detector but with less pixels (e.g. one tile HS)

detector = exp.Detector(name=detector, roi=roi_detector, sum_roi=normalize_roi, binning=binning, **kwargs)

####################
# Initialize setup #
####################
setup = exp.Setup(beamline=beamline, energy=energy, rocking_angle=rocking_angle, distance=sdd, direct_beam=direct_beam,
                  custom_scan=custom_scan, custom_images=custom_images, custom_monitor=custom_monitor,
                  custom_motors=custom_motors)

########################################
# print the current setup and detector #
########################################
print('\n##############\nSetup instance\n##############')
print(setup)
print('\n#################\nDetector instance\n#################')
print(detector)
Beispiel #8
0
my_cmap = colormap.cmap

#######################
# Initialize detector #
#######################
kwargs = dict()  # create dictionnary
kwargs['preprocessing_binning'] = preprocessing_binning
if nb_pixel_x:
    kwargs[
        'nb_pixel_x'] = nb_pixel_x  # fix to declare a known detector but with less pixels (e.g. one tile HS)
if nb_pixel_y:
    kwargs[
        'nb_pixel_y'] = nb_pixel_y  # fix to declare a known detector but with less pixels (e.g. one tile HS)

detector = exp.Detector(name=detector,
                        template_imagefile=template_imagefile,
                        binning=phasing_binning,
                        **kwargs)

####################################
# define the experimental geometry #
####################################
# correct the tilt_angle for binning
tilt_angle = tilt_angle * preprocessing_binning[0] * phasing_binning[0]
setup = exp.Setup(beamline=beamline,
                  energy=energy,
                  outofplane_angle=outofplane_angle,
                  inplane_angle=inplane_angle,
                  tilt_angle=tilt_angle,
                  rocking_angle=rocking_angle,
                  distance=sdd,
                  pixel_x=detector.pixelsize_x,
Beispiel #9
0
# options #
###########
normalize_prtf = True  # set to True when the solution is the first mode - then the intensity needs to be normalized
debug = False  # True to show more plots
##########################
# end of user parameters #
##########################

#######################
# Initialize detector #
#######################
kwargs = dict()  # create dictionnary
kwargs['is_series'] = is_series

detector = exp.Detector(name=detector,
                        template_imagefile=template_imagefile,
                        binning=(1, 1, 1),
                        preprocessing_binning=pre_binning)

####################
# Initialize setup #
####################
setup = exp.Setup(beamline=beamline,
                  energy=energy,
                  rocking_angle=rocking_angle,
                  distance=sdd,
                  beam_direction=beam_direction,
                  sample_inplane=sample_inplane,
                  sample_outofplane=sample_outofplane,
                  sample_offsets=sample_offsets,
                  actuators=actuators)
Beispiel #10
0
###################
# define colormap #
###################
bad_color = '1.0'  # white background
colormap = gu.Colormap(bad_color=bad_color)
my_cmap = colormap.cmap
plt.ion()

#################################################
# initialize detector, setup, paths and logfile #
#################################################
kwargs = dict()  # create dictionnary
kwargs['is_series'] = is_series
detector = exp.Detector(name=detector,
                        datadir='',
                        template_imagefile=template_imagefile,
                        sum_roi=sum_roi,
                        binning=[1, binning[0], binning[1]],
                        **kwargs)

setup = exp.SetupPreprocessing(beamline=beamline)

if setup.beamline == 'P10':
    specfile_name = sample_name + '_{:05d}'.format(scan)
    homedir = root_folder + specfile_name + '/'
    detector.datadir = homedir + 'e4m/'
    template_imagefile = specfile_name + template_imagefile
    detector.template_imagefile = template_imagefile
elif setup.beamline == 'SIXS_2018' or setup.beamline == 'SIXS_2019':
    homedir = root_folder
    detector.datadir = homedir + "align/"
else:
Beispiel #11
0
# end of user-defined parameters #
##################################

###################
# define colormap #
###################
bad_color = '1.0'  # white background
colormap = gu.Colormap(bad_color=bad_color)
my_cmap = colormap.cmap
plt.ion()

#################################################
# Initialize detector, setup, paths and logfile #
#################################################
detector = exp.Detector(name=detector,
                        datadir='',
                        template_imagefile=template_imagefile,
                        is_series=is_series)

setup = exp.SetupPreprocessing(beamline=beamline,
                               rocking_angle=rocking_angle,
                               custom_scan=custom_scan,
                               custom_images=custom_images,
                               custom_monitor=custom_monitor,
                               custom_motors=custom_motors)

if setup.beamline == 'P10':
    specfile_name = specfile_name % scan
    homedir = root_folder + specfile_name + '/'
    detector.datadir = homedir + 'e4m/'
    template_imagefile = specfile_name + template_imagefile
    detector.template_imagefile = template_imagefile
Beispiel #12
0
q_com = []  # q value of the center of mass
check_roi = [
]  # a small ROI around the Bragg peak will be stored for each scan, to see if the peak is indeed
# captured by the rocking curve

#######################
# Initialize detector #
#######################
kwargs = dict()  # create dictionnary
kwargs['is_series'] = is_series
kwargs[
    'nb_pixel_x'] = nb_pixel_x  # fix to declare a known detector but with less pixels (e.g. one tile HS)
kwargs[
    'nb_pixel_y'] = nb_pixel_y  # fix to declare a known detector but with less pixels (e.g. one tile HS)
detector = exp.Detector(name=detector,
                        template_imagefile=template_imagefile,
                        roi=roi_detector,
                        **kwargs)

####################
# Initialize setup #
####################
setup = exp.Setup(beamline=beamline,
                  energy=energy,
                  rocking_angle=rocking_angle,
                  distance=sdd,
                  beam_direction=beam_direction,
                  custom_scan=custom_scan,
                  custom_images=custom_images,
                  custom_monitor=custom_monitor,
                  custom_motors=custom_motors,
                  pixel_x=detector.pixelsize_x,