def test_xtcav_data_access():
    tname = sys.argv[1] if len(sys.argv) > 1 else '0'

    #fig, axim = fig_axis()
    fig, axim, axcb = gr.fig_img_cbar_axes(fig=None,\
             win_axim=(0.05,  0.05, 0.87, 0.93),\
             win_axcb=(0.923, 0.05, 0.02, 0.93)) #, **kwargs)

    ds = DataSource(files=data_file(tname))
    orun = next(ds.runs())
    det = orun.Detector('xtcav')

    print('test_xtcav_data    expt: %s runnum: %d\n' %
          (orun.expt, orun.runnum))

    for nev, evt in enumerate(orun.events()):
        if nev > 10: break
        print('Event %03d' % nev, end='')

        nda = det.raw(evt)
        print_ndarr(nda, '  det.raw(evt):')

        mean, std = nda.mean(), nda.std()
        aran = (mean - 3 * std, mean + 5 * std)

        axim.clear()
        axcb.clear()

        imsh = gr.imshow(axim, nda, amp_range=aran, extent=None, interpolation='nearest',\
                         aspect='auto', origin='upper', orientation='horizontal', cmap='inferno')
        cbar = gr.colorbar(fig,
                           imsh,
                           axcb,
                           orientation='vertical',
                           amp_range=aran)

        gr.set_win_title(fig, 'Event: %d' % nev)
        gr.draw_fig(fig)
        gr.show(mode='non-hold')

    gr.save_fig(
        fig,
        prefix='./img-%s-r%04d-e%06d-' % (orun.expt, orun.runnum, nev),
        suffix='.png',
    )
    gr.show()
Ejemplo n.º 2
0
    def __init__(self, args):
        """
        """
        #self.args = args

        fname = getattr(
            args, 'fname',
            '/reg/g/psdm/detector/data2_test/xtc/data-amox23616-r0131-e000200-xtcav-v2.xtc2'
        )
        experiment = getattr(args, 'experiment', 'amox23616')
        run_number = getattr(args, 'run_number', 131)
        max_shots = getattr(args, 'max_shots',
                            401)  #Maximum number of shots to process
        validity_range = getattr(args, 'validity_range', None)
        save_to_file = getattr(args, 'save_to_file', True)
        calibration_path = getattr(args, 'calibration_path', '')
        start_image = getattr(args, 'start_image', 0)
        dark_reference_path = getattr(args, 'dark_reference_path',
                                      None)  #Dark reference information
        num_bunches = getattr(args, 'num_bunches', 1)  #Number of bunches
        num_groups = getattr(args, 'num_groups',
                             None)  #Number of profiles to average together
        snr_filter = getattr(args, 'snr_filter',
                             10)  #Number of sigmas for the noise threshold
        roi_expand = getattr(args, 'roi_expand',
                             1)  #Parameter for the roi location
        roi_fraction = getattr(args, 'roi_fraction', cons.ROI_PIXEL_FRACTION)
        island_split_method = getattr(
            args, 'island_split_method',
            cons.DEFAULT_SPLIT_METHOD)  #Method for island splitting
        island_split_par1 = getattr(
            args, 'island_split_par1', 3.0
        )  #Ratio between number of pixels between largest and second largest groups when calling scipy.label
        island_split_par2 = getattr(
            args, 'island_split_par2', 5.
        )  #Ratio between number of pixels between second/third largest groups when calling scipy.label
        PLOT_IMAGE = getattr(args, 'plot_image', False)

        if PLOT_IMAGE:
            self.fig, self.axim, self.axcb = gr.fig_img_cbar_axes(fig=None,\
            win_axim=(0.05,  0.05, 0.87, 0.93),\
            win_axcb=(0.923, 0.05, 0.02, 0.93)) #, **kwargs)

        #if type(run_number) == int:
        #    run_number = str(run_number)

        self.parameters = LasingOffParameters(
            experiment=experiment,
            max_shots=max_shots,
            run_number=run_number,
            start_image=start_image,
            validity_range=validity_range,
            dark_reference_path=dark_reference_path,
            num_bunches=num_bunches,
            num_groups=num_groups,
            snr_filter=snr_filter,
            roi_expand=roi_expand,
            roi_fraction=roi_fraction,
            island_split_method=island_split_method,
            island_split_par2=island_split_par2,
            island_split_par1=island_split_par1,
            calibration_path=calibration_path,
            fname=fname,
            version=1)

        if rank == 0:
            print('Lasing off reference')
            print('\t File name: %s' % self.parameters.fname)
            print('\t Experiment: %s' % self.parameters.experiment)
            print('\t Runs: %s' % self.parameters.run_number)
            print('\t Number of bunches: %d' % self.parameters.num_bunches)
            print('\t Valid shots to process: %d' % self.parameters.max_shots)
            print('\t Dark reference run: %s' %
                  self.parameters.dark_reference_path)

        #Loading the data, this way of working should be compatible with both xtc and hdf5 files

        #ds = psana.DataSource("exp=%s:run=%s:idx" % (self.parameters.experiment, self.parameters.run_number))

        ds = DataSource(files=fname)
        run = next(ds.runs())  # run = ds.runs().next()
        #env = SimulatorEnvironment() # ds.env()

        #Camera for the xtcav images, Ebeam type, eventid, gas detectors
        camera = run.Detector(cons.DETNAME)  # psana.Detector(cons.DETNAME)
        ebeam = run.Detector(
            cons.EBEAM)  #SimulatorEBeam() # psana.Detector(cons.EBEAM)
        eventid = run.Detector(
            cons.EVENTID)  #SimulatorEventId() # evt.get(psana.EventId)
        gasdetector = run.Detector(
            cons.GAS_DETECTOR
        )  #SimulatorGasDetector() # psana.Detector(cons.GAS_DETECTOR)
        xtcavpars = run.Detector(cons.XTCAVPARS)

        # Empty list for the statistics obtained from each image, the shot to shot properties,
        # and the ROI of each image (although this ROI is initially the same for each shot,
        # it becomes different when the image is cropped around the trace)
        list_image_profiles = []

        #dark_background = self._getDarkBackground(env)

        dark_data, dark_meta = xtup.get_calibconst(camera, 'xtcav_pedestals',
                                                   cons.DETNAME, experiment,
                                                   run_number)

        logger.debug('==== dark_meta:\n%s' % str(dark_meta))

        dark_background = xtu.xtcav_calib_object_from_dict(dark_data)
        logger.debug('==== dir(dark_background):\n%s' %
                     str(dir(dark_background)))
        logger.debug('==== dark_background.ROI:\n%s' %
                     str(dark_background.ROI))
        logger.debug(
            info_ndarr(dark_background.image, '==== dark_background.image:'))

        print('\n', 100 * '_', '\n')

        camraw = xtup.get_attribute(camera, 'raw')
        valsxtp = xtup.get_attribute(xtcavpars, 'valsxtp')
        valsebm = xtup.get_attribute(ebeam, 'valsebm')
        valseid = xtup.get_attribute(eventid, 'valseid')
        valsgd = xtup.get_attribute(gasdetector, 'valsgd')

        if None in (camraw, valsxtp, valsebm, eventid, valsgd):
            sys.error(
                'FATAL ERROR IN THE DETECTOR INTERFACE: MISSING ATTRIBUTE MUST BE IMPLEMENTED'
            )

        #times = run.times()
        #image_numbers = xtup.divideImageTasks(first_event, len(times), rank, size)

        roi_xtcav, global_calibration, saturation_value = None, None, None
        num_processed = 0  #Counter for the total number of xtcav images processed within the run

        for nev, evt in enumerate(run.events()):
            #logger.info('Event %03d'%nev)
            img = camraw(evt)
            if img is None: continue

            if roi_xtcav is None:
                # get calibration values needed to process images.
                resp = self._getCalibrationValues(nev, evt, camraw, valsxtp)
                if resp is None: continue
                roi_xtcav, global_calibration, saturation_value = resp

            #Obtain the shot to shot parameters necessary for the retrieval of the x and y axis in time and energy units
            shot_to_shot = xtup.getShotToShotParameters(
                evt, valsebm, valsgd, valseid)
            #logger.debug('shot_to_shot: %s' % str(shot_to_shot))

            if not shot_to_shot.valid: continue

            image_profile, _ = xtu.processImage(img, self.parameters,
                                                dark_background,
                                                global_calibration,
                                                saturation_value, roi_xtcav,
                                                shot_to_shot)

            #logger.debug(info_ndarr(image_profile, 'LasingOffReference image_profile'))

            if not image_profile:
                continue

            #Append only image profile, omit processed image
            list_image_profiles.append(image_profile)
            num_processed += 1

            self._printProgressStatements(num_processed)

            if num_processed >= np.ceil(
                    self.parameters.max_shots / float(size)):
                break

            if PLOT_IMAGE:

                nda = img

                mean, std = nda.mean(), nda.std()
                aran = (mean - 3 * std, mean + 5 * std)

                self.axim.clear()
                self.axcb.clear()
                imsh = gr.imshow(self.axim, nda, amp_range=aran, extent=None, interpolation='nearest',\
                                 aspect='auto', origin='upper', orientation='horizontal', cmap='inferno')
                cbar = gr.colorbar(self.fig,
                                   imsh,
                                   self.axcb,
                                   orientation='vertical',
                                   amp_range=aran)

                gr.set_win_title(self.fig, 'Event: %d' % nev)
                gr.draw_fig(self.fig)
                gr.show(mode='non-hold')

        # here gather all shots in one core, add all lists
        #image_profiles = comm.gather(list_image_profiles, root=0)
        image_profiles = list_image_profiles

        if rank != 0: return

        sys.stdout.write('\n')
        # Flatten gathered arrays
        #image_profiles = [item for sublist in image_profiles for item in sublist]

        #for i,ipf in enumerate(image_profiles) :
        #  print('XXX image_profiles %d:\n  %s'%(i,str(ipf)))

        #Since there are 12 cores it is possible that there are more references than needed. In that case we discard some
        if len(image_profiles) > self.parameters.max_shots:
            image_profiles = image_profiles[0:self.parameters.max_shots]

        #At the end, all the reference profiles are converted to Physical units, grouped and averaged together
        averaged_profiles = xtu.averageXTCAVProfilesGroups(
            image_profiles, self.parameters.num_groups)

        self.averaged_profiles, num_groups = averaged_profiles
        self.n = num_processed
        self.parameters = self.parameters._replace(num_groups=num_groups)

        logger.debug('self.parameters.validity_range: %s  type: %s' %
                     (self.parameters.validity_range,
                      type(self.parameters.validity_range)))
        logger.debug(
            'self.parameters.run_number: %d  type: %s' %
            (self.parameters.run_number, type(self.parameters.run_number)))

        # Set validity range, replace 'end' -> 9999 othervise save does not work...
        if not self.parameters.validity_range or not type(
                self.parameters.validity_range) == tuple:
            self.parameters = self.parameters._replace(
                validity_range=(self.parameters.run_number,
                                9999))  # IT WAS 'end'))
        elif len(self.parameters.validity_range) == 1:
            self.parameters = self.parameters._replace(
                validity_range=(self.parameters.validity_range[0],
                                9999))  # 'end'))

        #=====================
        #sys.exit('TEST EXIT')
        #=====================

        if save_to_file:
            #cp = CalibrationPaths(env, self.parameters.calibration_path)
            #file = cp.newCalFileName(cons.LOR_FILE_NAME, self.parameters.validity_range[0], self.parameters.validity_range[1])
            fname = 'cons-%s-%04d-xtcav-lasingoff.data' % (run.expt, run.runnum
                                                           )  # , cons.DETNAME)
            self.save(fname)