Esempio n. 1
0
    def update_mp(self):
        """
        Update memory profile curve
        """
        if not self.do_memprofile:
            return
        self.memory_profile[0].append(time.time() - self.starttime)
        self.memory_profile[1].append(self.get_mem())
        if not self.fig_mp:
            self.fig_mp = pylab.figure()
            self.fig_mp.show()
            self.ax_mp = self.fig_mp.add_subplot(1, 1, 1)
            self.ax_mp.set_autoscale_on(False)
            self.ax_mp.set_xlabel("Run time (s)")
            self.ax_mp.set_xlim(0, 100)
            self.ax_mp.set_ylim(0, 2**10)
            self.ax_mp.set_ylabel("Memory occupancy (MB)")
            self.ax_mp.set_title("Memory leak hunter")
            self.plot_mp = self.ax_mp.plot(*self.memory_profile)[0]
        else:
            self.plot_mp.set_data(*self.memory_profile)
            tmax = self.memory_profile[0][-1]
            mmax = max(self.memory_profile[1])
            if tmax > self.ax_mp.get_xlim()[-1]:
                self.ax_mp.set_xlim(0, tmax)
            if mmax > self.ax_mp.get_ylim()[-1]:
                self.ax_mp.set_ylim(0, mmax)

        if self.fig_mp.canvas:
            update_fig(self.fig_mp)
Esempio n. 2
0
    def update_mp(self):
        """
        Update memory profile curve
        """
        if not self.do_memprofile:
            return
        self.memory_profile[0].append(time.time() - self.starttime)
        self.memory_profile[1].append(self.get_mem())
        if not self.fig_mp:
            self.fig_mp = pylab.figure()
            self.fig_mp.show()
            self.ax_mp = self.fig_mp.add_subplot(1, 1, 1)
            self.ax_mp.set_autoscale_on(False)
            self.ax_mp.set_xlabel("Run time (s)")
            self.ax_mp.set_xlim(0, 100)
            self.ax_mp.set_ylim(0, 2 ** 10)
            self.ax_mp.set_ylabel("Memory occupancy (MB)")
            self.ax_mp.set_title("Memory leak hunter")
            self.plot_mp = self.ax_mp.plot(*self.memory_profile)[0]
        else:
            self.plot_mp.set_data(*self.memory_profile)
            tmax = self.memory_profile[0][-1]
            mmax = max(self.memory_profile[1])
            if tmax > self.ax_mp.get_xlim()[-1]:
                self.ax_mp.set_xlim(0, tmax)
            if mmax > self.ax_mp.get_ylim()[-1]:
                self.ax_mp.set_ylim(0, mmax)

        if self.fig_mp.canvas:
            update_fig(self.fig_mp)
Esempio n. 3
0
 def new_curve(self, results, label):
     """
     Create a new curve within the current graph
     """
     self.update_mp()
     if not self.fig:
         return
     self.plot_x = list(results.keys())
     self.plot_x.sort()
     self.plot_y = [1000.0 / results[i] for i in self.plot_x]
     self.plot = self.ax.plot(self.plot_x, self.plot_y, "o-", label=label)[0]
     self.ax.legend()
     update_fig(self.fig)
Esempio n. 4
0
    def new_point(self, size, exec_time):
        """
        Add new point to current curve

        @param size: of the system
        @parm exec_time: execution time in ms
        """
        self.update_mp()
        if not self.plot:
            return

        self.plot_x.append(size)
        self.plot_y.append(1000.0 / exec_time)
        self.plot.set_data(self.plot_x, self.plot_y)
        update_fig(self.fig)
Esempio n. 5
0
    def new_point(self, size, exec_time):
        """
        Add new point to current curve

        @param size: of the system
        @parm exec_time: execution time in ms
        """
        self.update_mp()
        if not self.plot:
            return

        self.plot_x.append(size)
        self.plot_y.append(1000.0 / exec_time)
        self.plot.set_data(self.plot_x, self.plot_y)
        update_fig(self.fig)
Esempio n. 6
0
 def new_curve(self, results, label, style="-"):
     """
     Create a new curve within the current graph
     
     @param results: dict with execution time in function of size
     @param label: string with the title of the curve
     @param style: the style of the line: "-" for plain line, "--" for dashed
     """
     self.update_mp()
     if not self.fig:
         return
     self.plot_x = list(results.keys())
     self.plot_x.sort()
     self.plot_y = [1000.0 / results[i] for i in self.plot_x]
     self.plot = self.ax.plot(self.plot_x, self.plot_y, "o" + style, label=label)[0]
     self.ax.legend()
     update_fig(self.fig)
Esempio n. 7
0
 def init_curve(self):
     self.update_mp()
     if self.fig:
         print("Already initialized")
         return
     if "DISPLAY" in os.environ:
         self.fig = pylab.figure()
         self.fig.show()
         self.ax = self.fig.add_subplot(1, 1, 1)
         self.ax.set_autoscale_on(False)
         self.ax.set_xlabel("Image size in Mega-Pixels")
         self.ax.set_ylabel("Frames processed per second")
         self.ax.set_yscale("log", basey=2)
         t = [1, 2, 5, 10, 20, 50, 100, 200, 400, 500]
         self.ax.set_yticks([float(i) for i in t])
         self.ax.set_yticklabels([str(i)for i in t])
         self.ax.set_xlim(0.5, 17)
         self.ax.set_ylim(0.5, 500)
         self.ax.set_title(self.get_cpu() + " / " + self.get_gpu())
         update_fig(self.fig)
Esempio n. 8
0
    def new_curve(self, results, label, style="-"):
        """
        Create a new curve within the current graph

        @param results: dict with execution time in function of size
        @param label: string with the title of the curve
        @param style: the style of the line: "-" for plain line, "--" for dashed
        """
        self.update_mp()
        if not self.fig:
            return
        self.plot_x = list(results.keys())
        self.plot_x.sort()
        self.plot_y = [1000.0 / results[i] for i in self.plot_x]
        self.plot = self.ax.plot(self.plot_x,
                                 self.plot_y,
                                 "o" + style,
                                 label=label)[0]
        self.ax.legend()
        update_fig(self.fig)
Esempio n. 9
0
 def init_curve(self):
     self.update_mp()
     if self.fig:
         print("Already initialized")
         return
     if (sys.platform in ["win32", "darwin"]) or ("DISPLAY" in os.environ):
         self.fig = pylab.figure()
         self.fig.show()
         self.ax = self.fig.add_subplot(1, 1, 1)
         self.ax.set_autoscale_on(False)
         self.ax.set_xlabel("Image size in Mega-Pixels")
         self.ax.set_ylabel("Frames processed per second")
         self.ax.set_yscale("log", basey=2)
         t = [1, 2, 5, 10, 20, 50, 100, 200, 400, 500]
         self.ax.set_yticks([float(i) for i in t])
         self.ax.set_yticklabels([str(i) for i in t])
         self.ax.set_xlim(0.5, 17)
         self.ax.set_ylim(0.5, 500)
         self.ax.set_title(self.get_cpu() + " / " + self.get_gpu())
         update_fig(self.fig)
Esempio n. 10
0
def calibration(img, calibrant_file=None, wavelength=None,
                calib_collection_uid=None, save_file_name=None,
                detector=None, gaussian=None):
    """ run calibration process on a image with geometry correction
    software

    current backend is ``pyFAI``.

    Parameters
    ----------
    img : ndarray
        image to be calibrated
    calibrant_file : str, optional
        calibrant file being used, default is 'Ni.D' under 
        xpdUser/userAnalysis/
    wavelength : flot, optional
        current of x-ray wavelength, in angstrom. Default value is 
        read out from existing xpdacq.Beamtime object
    calibration_collection_uid : str, optional
        uid of calibration collection. default is generated from run
        calibration
    save_file_name : str, optional
        file name for yaml that carries resultant calibration parameters
    detector : pyfai.detector.Detector, optional.
        instance of detector which defines pixel size in x- and
        y-direction. Default is set to Perkin Elmer detector
    gaussian : int, optional
        gaussian width between rings, Default is 100.
    """
    # default params
    interactive = True
    dist = 0.1

    _check_obj(_REQUIRED_OBJ_LIST)
    ips = get_ipython()
    bto = ips.ns_table['user_global']['bt']
    xrun = ips.ns_table['user_global']['xrun']

    calibrant = Calibrant()
    # d-spacing
    if calibrant_file is not None:
        calibrant.load_file(calibrant_file)
        calibrant_name = os.path.split(calibrant_file)[1]
        calibrant_name = os.path.splitext(calibrant_name)[0]
    else:
        calibrant.load_file(os.path.join(glbl.usrAnalysis_dir, 'Ni.D'))
        calibrant_name = 'Ni'
    # wavelength
    if wavelength is None:
        _wavelength = bto['bt_wavelength']
    else:
        _wavelength = wavelength
    calibrant.wavelength = _wavelength * 10 ** (-10)
    # detector
    if detector is None:
        detector = Perkin()
    # calibration
    timestr = _timestampstr(time.time())
    basename = '_'.join(['pyFAI_calib', calibrant_name, timestr])
    w_name = os.path.join(glbl.config_base, basename)  # poni name
    c = Calibration(wavelength=calibrant.wavelength,
                    detector=detector,
                    calibrant=calibrant,
                    gaussianWidth=gaussian)
    c.gui = interactive
    c.basename = w_name
    c.pointfile = w_name + ".npt"
    c.ai = AzimuthalIntegrator(dist=dist, detector=detector,
                               wavelength=calibrant.wavelength)
    c.peakPicker = PeakPicker(img, reconst=True, mask=detector.mask,
                              pointfile=c.pointfile, calibrant=calibrant,
                              wavelength=calibrant.wavelength)
    # method=method)
    if gaussian is not None:
        c.peakPicker.massif.setValleySize(gaussian)
    else:
        c.peakPicker.massif.initValleySize()

    if interactive:
        c.peakPicker.gui(log=True, maximize=True, pick=True)
        update_fig(c.peakPicker.fig)
    c.gui_peakPicker()
    c.ai.setPyFAI(**c.geoRef.getPyFAI())
    c.ai.wavelength = c.geoRef.wavelength

    return c.ai
Esempio n. 11
0
def run_calibration(exposure=60, calibrant_file=None, wavelength=None,
                    detector=None, gaussian=None):
    """ function to run entire calibration process.

    Entire process includes: collect calibration image, trigger pyFAI 
    calibration process, store calibration parameters as a yaml file 
    under xpdUser/config_base/ and inject uid of calibration image to
    following scans, until this function is run again.

    Parameters
    ----------
    exposure : int, optional
        total exposure time in sec. Default is 60s
    calibrant_name : str, optional
        name of calibrant used, different calibrants correspond to 
        different d-spacing profiles. Default is 'Ni'. User can assign 
        different calibrant, given d-spacing file path presents
    wavelength : flot, optional
        current of x-ray wavelength, in angstrom. Default value is 
        read out from existing xpdacq.Beamtime object
    detector : pyfai.detector.Detector, optional.
        instance of detector which defines pxiel size in x- and
        y-direction. Default is set to Perkin Elmer detector
    gaussian : int, optional
        gaussian width between rings, Default is 100.
    """
    # default params
    interactive = True
    dist = 0.1

    _check_obj(_REQUIRED_OBJ_LIST)
    ips = get_ipython()
    bto = ips.ns_table['user_global']['bt']
    prun = ips.ns_table['user_global']['prun']
    # print('*** current beamtime info = {} ***'.format(bto.md))
    calibrant = Calibrant()
    # d-spacing
    if calibrant_file is not None:
        calibrant.load_file(calibrant_file)
        calibrant_name = os.path.split(calibrant_file)[1]
        calibrant_name = os.path.splitext(calibrant_name)[0]
    else:
        calibrant.load_file(os.path.join(glbl.usrAnalysis_dir, 'Ni24.D'))
        calibrant_name = 'Ni'
    # wavelength
    if wavelength is None:
        _wavelength = bto['bt_wavelength']
    else:
        _wavelength = wavelength
    calibrant.wavelength = _wavelength * 10 ** (-10)
    # detector
    if detector is None:
        detector = Perkin()
    # scan
    # simplified version of Sample object
    calib_collection_uid = str(uuid.uuid4())
    calibration_dict = {'sample_name':calibrant_name,
                        'sample_composition':{calibrant_name :1},
                        'is_calibration': True,
                        'calibration_collection_uid': calib_collection_uid}
    prun_uid = prun(calibration_dict, ScanPlan(bto, ct, exposure))
    light_header = glbl.db[prun_uid[-1]]  # last one is always light
    dark_uid = light_header.start['sc_dk_field_uid']
    dark_header = glbl.db[dark_uid]
    # unknown signature of get_images
    dark_img = np.asarray(
        get_images(dark_header, glbl.det_image_field)).squeeze()
    # dark_img = np.asarray(glbl.get_images(dark_header, glbl.det_image_field)).squeeze()
    for ev in glbl.get_events(light_header, fill=True):
        img = ev['data'][glbl.det_image_field]
        img -= dark_img
    # calibration
    timestr = _timestampstr(time.time())
    basename = '_'.join(['pyFAI_calib', calibrant_name, timestr])
    w_name = os.path.join(glbl.config_base, basename)  # poni name
    c = Calibration(wavelength=calibrant.wavelength,
                    detector=detector,
                    calibrant=calibrant,
                    gaussianWidth=gaussian)
    c.gui = interactive
    c.basename = w_name
    c.pointfile = w_name + ".npt"
    c.ai = AzimuthalIntegrator(dist=dist, detector=detector,
                               wavelength=calibrant.wavelength)
    c.peakPicker = PeakPicker(img, reconst=True, mask=detector.mask,
                              pointfile=c.pointfile, calibrant=calibrant,
                              wavelength=calibrant.wavelength)
    # method=method)
    if gaussian is not None:
        c.peakPicker.massif.setValleySize(gaussian)
    else:
        c.peakPicker.massif.initValleySize()

    if interactive:
        c.peakPicker.gui(log=True, maximize=True, pick=True)
        update_fig(c.peakPicker.fig)
    c.gui_peakPicker()
    c.ai.setPyFAI(**c.geoRef.getPyFAI())
    c.ai.wavelength = c.geoRef.wavelength
    # update until next time
    glbl.calib_config_dict = c.ai.getPyFAI()
    Fit2D_dict = c.ai.getFit2D()
    glbl.calib_config_dict.update(Fit2D_dict)
    glbl.calib_config_dict.update({'file_name':basename})
    glbl.calib_config_dict.update({'time':timestr})
    # FIXME: need a solution for selecting desired calibration image
    # based on calibration_collection_uid later
    glbl.calib_config_dict.update({'calibration_collection_uid':
                                   calib_collection_uid})
    # write yaml
    yaml_name = glbl.calib_config_name
    with open(os.path.join(glbl.config_base, yaml_name), 'w') as f:
        yaml.dump(glbl.calib_config_dict, f)

    return c.ai