Esempio n. 1
0
class LiveImage(CallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event
    """
    def __init__(self, field, *, fs=None):
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt
        super().__init__()
        self.field = field
        fig = plt.figure()
        self.cs = CrossSection(fig)
        self.cs._fig.show()
        if fs is None:
            import filestore.api as fs
        self.fs = fs

    def event(self, doc):
        uid = doc['data'][self.field]
        data = self.fs.retrieve(uid)
        self.update(data)
        super().event(doc)

    def update(self, data):
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 2
0
class LiveImage(CallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event

    Note
    ----
    Requires a matplotlib fix that is not released as of this writing. The
    relevant commit is a951b7.
    """
    def __init__(self, field):
        super().__init__()
        self.field = field
        fig = plt.figure()
        self.cs = CrossSection(fig)
        self.cs._fig.show()

    def event(self, doc):
        uid = doc['data'][self.field]
        data = fsapi.retrieve(uid)
        self.cs.update_image(data)
        self.cs._fig.canvas.draw()
        self.cs._fig.canvas.flush_events()
Esempio n. 3
0
class SRXLiveImage(CallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string name of data field in an Event

    Note
    ----
    Requires a matplotlib fix that is not released as of this writing. The
    relevant commit is a951b7.
    """
    def __init__(self, field):
        super().__init__()
        self.field = field
        fig = plt.figure()
        self.cs = CrossSection(fig)
        self.cs._fig.show()

    def event(self, doc):
        uid = doc['data'][self.field]
        data = fsapi.retrieve(uid)
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 4
0
 def __init__(self,
              field,
              *,
              cmap=None,
              norm=None,
              limit_func=None,
              auto_redraw=True,
              interpolation=None,
              window_title=None,
              aspect='equal',
              tofile=None):
     from xray_vision.backend.mpl.cross_section_2d import CrossSection
     import matplotlib.pyplot as plt
     self.tofile = tofile
     super().__init__()
     fig = plt.figure()
     self.field = field
     self._plot_norm = norm
     # the None field is supposed to be norm
     self.cs = CrossSection(fig, cmap, None, limit_func, auto_redraw,
                            interpolation)
     # not supported feature but do anyway...
     # need to set aspect ratio!!!!
     # (get rid of xray vision in the future...)
     self.cs._im_ax.set_aspect(aspect)
     if window_title:
         self.cs._fig.canvas.set_window_title(window_title)
     self.cs._fig.show()
Esempio n. 5
0
def _show_image(light, title):
    from xray_vision.backend.mpl.cross_section_2d import CrossSection
    options = dict(cmap='gray', interpolation='nearest')
    fig = plt.figure()
    cs = CrossSection(fig)
    cs.update_image(light)
    #    fig, axes = plt.subplots(1, 1, figsize=(4, 4))
    #    axes.imshow(light, **options)
    cs._ax_h.set_title(title)
Esempio n. 6
0
def _show_image(light, title):
    from xray_vision.backend.mpl.cross_section_2d import CrossSection
    options = dict(cmap='gray', interpolation='nearest')
    fig = plt.figure()
    cs = CrossSection(fig)
    cs.update_image(light)
#    fig, axes = plt.subplots(1, 1, figsize=(4, 4))
#    axes.imshow(light, **options)
    cs._ax_h.set_title(title)
Esempio n. 7
0
 def __init__(self, field, *, fs=None):
     from xray_vision.backend.mpl.cross_section_2d import CrossSection
     import matplotlib.pyplot as plt
     super().__init__()
     self.field = field
     fig = plt.figure()
     self.cs = CrossSection(fig)
     self.cs._fig.show()
     if fs is None:
         import filestore.api as fs
     self.fs = fs
Esempio n. 8
0
class LiveImage(BrokerCallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event
    fs: Registry instance
        The Registry instance to pull the data from
    cmap : str,  colormap, or None
        color map to use.  Defaults to gray
    norm : Normalize or None
       Normalization function to use
    limit_func : callable, optional
        function that takes in the image and returns clim values
    auto_redraw : bool, optional
    interpolation : str, optional
        Interpolation method to use. List of valid options can be found in
        CrossSection2DView.interpolation
    """
    def __init__(self,
                 field,
                 *,
                 db=None,
                 cmap=None,
                 norm=None,
                 limit_func=None,
                 auto_redraw=True,
                 interpolation=None,
                 window_title=None):
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt
        super().__init__((field, ), db=db)
        fig = plt.figure()
        self.field = field
        self.cs = CrossSection(fig, cmap, norm, limit_func, auto_redraw,
                               interpolation)
        if window_title:
            self.cs._fig.canvas.set_window_title(window_title)
        self.cs._fig.show()

    def event(self, doc):
        super().event(doc)
        data = doc['data'][self.field]
        self.update(data)

    def update(self, data):
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 9
0
 def __init__(self, field):
     from xray_vision.backend.mpl.cross_section_2d import CrossSection
     import matplotlib.pyplot as plt
     super().__init__()
     self.field = field
     fig = plt.figure()
     self.cs = CrossSection(fig)
     self.cs._fig.show()
Esempio n. 10
0
class LiveImage(BrokerCallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event
    fs: Registry instance
        The Registry instance to pull the data from
    cmap : str,  colormap, or None
        color map to use.  Defaults to gray
    norm : Normalize or None
       Normalization function to use
    limit_func : callable, optional
        function that takes in the image and returns clim values
    auto_redraw : bool, optional
    interpolation : str, optional
        Interpolation method to use. List of valid options can be found in
        CrossSection2DView.interpolation
    """

    def __init__(self, field, *, db=None, cmap=None, norm=None,
                 limit_func=None, auto_redraw=True, interpolation=None,
                 window_title=None):
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt
        super().__init__((field,), db=db)
        fig = plt.figure()
        self.field = field
        self.cs = CrossSection(fig, cmap, norm,
                               limit_func, auto_redraw, interpolation)
        if window_title:
            self.cs._fig.canvas.set_window_title(window_title)
        self.cs._fig.show()

    def event(self, doc):
        super().event(doc)
        data = doc['data'][self.field]
        self.update(data)

    def update(self, data):
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 11
0
 def __init__(self,
              field,
              *,
              db=None,
              cmap=None,
              norm=None,
              limit_func=None,
              auto_redraw=True,
              interpolation=None,
              window_title=None):
     from xray_vision.backend.mpl.cross_section_2d import CrossSection
     import matplotlib.pyplot as plt
     super().__init__((field, ), db=db)
     fig = plt.figure()
     self.field = field
     self.cs = CrossSection(fig, cmap, norm, limit_func, auto_redraw,
                            interpolation)
     if window_title:
         self.cs._fig.canvas.set_window_title(window_title)
     self.cs._fig.show()
Esempio n. 12
0
 def __init__(self, field, *, db=None, cmap=None, norm=None,
              limit_func=None, auto_redraw=True, interpolation=None,
              window_title=None):
     from xray_vision.backend.mpl.cross_section_2d import CrossSection
     import matplotlib.pyplot as plt
     super().__init__((field,), db=db)
     fig = plt.figure()
     self.field = field
     self.cs = CrossSection(fig, cmap, norm,
                            limit_func, auto_redraw, interpolation)
     if window_title:
         self.cs._fig.canvas.set_window_title(window_title)
     self.cs._fig.show()
Esempio n. 13
0
class LiveImage(CallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event
    """
    def __init__(self, field):
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt
        super().__init__()
        self.field = field
        fig = plt.figure()
        self.cs = CrossSection(fig)
        self.cs._fig.show()

    def event(self, doc):
        import filestore.api as fsapi
        uid = doc['data'][self.field]
        data = fsapi.retrieve(uid)
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 14
0
    def descriptor(self, doc):
        self.fields = [
            k for k, v in doc["data_keys"].items()
            if len(v["shape"]) == 2 or (len(v["shape"]) == 3 and len(
                [dim for dim in v["shape"] if dim > 0]) == 2)
        ]
        data_keys = doc["data_keys"]
        if self.aspect is None:
            aspects = []
            for field in self.fields:
                aspect_ratio = data_keys[field]["shape"][0] / data_keys[field][
                    "shape"][1]
                if any(s == -1 for s in data_keys[field]["shape"]):
                    aspects.append('auto')
                elif aspect_ratio > 1.25 or aspect_ratio < .75:
                    aspects.append('auto')
                else:
                    aspects.append('equal')
        else:
            aspects = [self.aspect] * len(self.fields)
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt

        # only make new figure for new data otherwise use old data
        for field, asp in zip(self.fields, aspects):
            if field in self.cs_dict and plt.fignum_exists(
                    self.cs_dict[field]._fig.number):
                continue
            fig = plt.figure(field)
            cs = CrossSection(
                fig,
                self.cmap,
                self.norm,
                self.limit_func,
                self.auto_redraw,
                self.interpolation,
                aspect=asp,
            )
            cs._fig.canvas.set_window_title(field)
            cs._fig.show()
            self.cs_dict[field] = cs
Esempio n. 15
0
 def __init__(self, field):
     super().__init__()
     self.field = field
     fig = plt.figure()
     self.cs = CrossSection(fig)
     self.cs._fig.show()
Esempio n. 16
0
def cli():
    import argparse

    parser = argparse.ArgumentParser(description='Plot image based on input file')
    parser.add_argument('-d', '--dat_file', dest='dat_file', default='',
                        help ='input .dat file')
    parser.add_argument('-g', '--graph', dest='graph', default='',
                        help='create graph', action='store_true')
    parser.add_argument('-vp', '--vert_pos', dest='vert_pos', default='',
                        help='input vertical slice position', type=str)
    parser.add_argument('-hp', '--horiz_pos', dest='horiz_pos', default='',
                        help='input horizontal slice position', type=str)
    parser.add_argument('-vl', '--vert_label', dest='vert_label', default='',
                        help='input vertical label string', type=str)
    parser.add_argument('-hl', '--horiz_label', dest='horiz_label', default='',
                        help='input horizontal label string', type=str)
    parser.add_argument('-e', '--extent_labels', dest='extent_labels', default=None,
                        help='input extent labels for image ranges', nargs='+', type=int)
    parser.add_argument('-t', '--title', dest='title',default=None,
                        help='input title', type=str)
    args = parser.parse_args()

    initial_data = {'photon': {}, 'horizontal': {}, 'vertical': {},}
    matrix = [[]]
    try:
        get_parameters(initial_data, file_name=args.dat_file)
    except:
        print('no data file found - specify with (-d <filename>)')
        quit()
    horizontal = initial_data['horizontal']['points']
    vertical = initial_data['vertical']['points']
    if args.graph and args.dat_file:

        import matplotlib.pyplot as plt
        from xray_vision.backend.mpl.cross_section_2d import CrossSection

        img = np.loadtxt(args.dat_file)
        img = img.reshape((vertical, horizontal))
        fig = plt.figure()
        kwargs = {'fig': fig}

        if args.vert_pos:
            if args.vert_pos == 'right':
                args.vert_pos = 'right'
            kwargs['vert_loc'] = args.vert_pos

        if args.horiz_pos:
            if args.horiz_pos == 'bottom':
                args.horiz_pos = 'bottom'
            kwargs['horiz_loc'] = args.horiz_pos

        if args.vert_label:
            kwargs['vert_label'] = args.vert_label

        if args.horiz_label:
            kwargs['horiz_label'] = args.horiz_label

        if args.extent_labels:
            kwargs['extent_labels'] = args.extent_labels
        if args.title:
            kwargs['title'] = args.title

        cs = CrossSection(**kwargs)
        cs.update_image(img)
        plt.show()
    else:
        if args.dat_file:
            file = args.dat_file
            matrix = populate_matrix_smart(matrix, horizontal, vertical, data_file=file)
            print(initial_data)
Esempio n. 17
0
class LiveImage(CallbackBase):
    """
    Stream 2D images in a cross-section viewer.

    Parameters
    ----------
    field : string
        name of data field in an Event
    fs: Registry instance
        The Registry instance to pull the data from
    cmap : str,  colormap, or None
        color map to use.  Defaults to gray
    norm : Normalize or None
       Normalization function to use
    limit_func : callable, optional
        function that takes in the image and returns clim values
    auto_redraw : bool, optional
    interpolation : str, optional
        Interpolation method to use. List of valid options can be found in
        CrossSection2DView.interpolation
    """
    def __init__(self,
                 field,
                 *,
                 cmap=None,
                 norm=None,
                 limit_func=None,
                 auto_redraw=True,
                 interpolation=None,
                 window_title=None,
                 aspect='equal',
                 tofile=None):
        from xray_vision.backend.mpl.cross_section_2d import CrossSection
        import matplotlib.pyplot as plt
        self.tofile = tofile
        super().__init__()
        fig = plt.figure()
        self.field = field
        self._plot_norm = norm
        # the None field is supposed to be norm
        self.cs = CrossSection(fig, cmap, None, limit_func, auto_redraw,
                               interpolation)
        # not supported feature but do anyway...
        # need to set aspect ratio!!!!
        # (get rid of xray vision in the future...)
        self.cs._im_ax.set_aspect(aspect)
        if window_title:
            self.cs._fig.canvas.set_window_title(window_title)
        self.cs._fig.show()

    def event(self, doc):
        super().event(doc)
        if self.field in doc['data']:
            print("field : {}".format(self.field))
            data = doc['data'][self.field]
            if self._plot_norm is not None:
                data = self._plot_norm(data)
            self.update(data)
            if self.tofile is not None:
                plt.savefig(self.tofile)

    def update(self, data):
        self.cs.update_image(data)
        self.cs._fig.canvas.draw_idle()
Esempio n. 18
0
 def __init__(self, field):
     super().__init__()
     self.field = field
     fig = plt.figure()
     self.cs = CrossSection(fig)
     self.cs._fig.show()
Esempio n. 19
0
        if args.horiz_pos:
            if args.horiz_pos == 'bottom':
                args.horiz_pos = 'bottom'
            kwargs['horiz_loc'] = args.horiz_pos

        if args.vert_label:
            kwargs['vert_label'] = args.vert_label

        if args.horiz_label:
            kwargs['horiz_label'] = args.horiz_label

        if args.extent_labels:
            kwargs['extent_labels'] = args.extent_labels

        cs = CrossSection(**kwargs)
        cs.update_image(img)
        plt.show()
    else:

        matrix = populate_matrix_smart(matrix, horizontal, vertical)
        fig = plt.figure(figsize=(8, 10))
        title = f'After Propagation (E={initial_data["photon"]["initial_energy"]} {initial_data["photon"]["units"]})'
        axis_data = [
            initial_data['horizontal']['axis_label'] + " [µm]",
            initial_data['vertical']['axis_label'] + " [µm]",
            "Intensity " + initial_data['intensity']
        ]
        cs = CrossSection(fig,
                          horiz_loc='top',
                          vert_loc='right',