Ejemplo n.º 1
0
 def __init__(self):
     GllLensConfig.__init__(self, ll.polygonal_lenses)
     self.config_widget = GllConfigBox(
         [("num_stars", "Number of stars", (5, 0, 1000, 1), 0),
          ("total_mass", "Total mass", (1.0, 0.0, 1e10, 0.05), 2),
          ("angle", "Rotation angle", (0.0, -100.0, 100.0, 0.01), 4),
          ("region_radius", "Region radius", (1.5, 0.0, 1000.0, 0.1), 2)])
     self.config_widget.declare_record("region")
Ejemplo n.º 2
0
 def __init__(self):
     GllLensConfig.__init__(self, ll.globular_cluster)
     self.config_widget = GllConfigBox(
         [("num_stars", "Number of stars", (1000, 0, 100000, 1), 0),
          ("random_seed", "Random seed", (42, -1000000, 1000000, 1), 0),
          ("total_mass", "Total mass", (1.0, 0.0, 1e10, 0.05), 2),
          ("log_mass_stddev", "Log(mass) std dev", (0.0, 0.0, 10.0, 0.05), 2),
          ("angle", "Rotation angle", (0.0, -100.0, 100.0, 0.01), 4),
          ("region_radius", "Region radius", (1.0, 0.0, 1000.0, 0.1), 2)])
     self.config_widget.declare_record("region")
Ejemplo n.º 3
0
class GllPolygonalLenses(GllLensConfig):
    name = "Polygonal lens configuration"

    def __init__(self):
        GllLensConfig.__init__(self, ll.polygonal_lenses)
        self.config_widget = GllConfigBox(
            [("num_stars", "Number of stars", (5, 0, 1000, 1), 0),
             ("total_mass", "Total mass", (1.0, 0.0, 1e10, 0.05), 2),
             ("angle", "Rotation angle", (0.0, -100.0, 100.0, 0.01), 4),
             ("region_radius", "Region radius", (1.5, 0.0, 1000.0, 0.1), 2)])
        self.config_widget.declare_record("region")
Ejemplo n.º 4
0
 def __init__(self):
     super(GllLightCurve, self).__init__(ll.light_curve)
     self.main_widget = gtk.DrawingArea()
     self.main_widget.set_size_request(400, 300)
     self.main_widget.show()
     self.main_widget.connect("expose-event", self.draw)
     self.config_widget = GllConfigBox()
     self.config_widget.add_toggle_group(
         "export_max", "Set upper magnification", False,
         ("curve_max_mag", "Upper magnification",
          (10.0, 0.0, 1e10, 1.0), 1))
     self.points = []
Ejemplo n.º 5
0
class GllGlobularCluster(GllLensConfig):
    name = "Globular cluster"

    def __init__(self):
        GllLensConfig.__init__(self, ll.globular_cluster)
        self.config_widget = GllConfigBox(
            [("num_stars", "Number of stars", (1000, 0, 100000, 1), 0),
             ("random_seed", "Random seed", (42, -1000000, 1000000, 1), 0),
             ("total_mass", "Total mass", (1.0, 0.0, 1e10, 0.05), 2),
             ("log_mass_stddev", "Log(mass) std dev", (0.0, 0.0, 10.0, 0.05), 2),
             ("angle", "Rotation angle", (0.0, -100.0, 100.0, 0.01), 4),
             ("region_radius", "Region radius", (1.0, 0.0, 1000.0, 0.1), 2)])
        self.config_widget.declare_record("region")
Ejemplo n.º 6
0
 def __init__(self):
     super(GllSourceProfile, self).__init__(ll.source_profile)
     self.main_widget = GllImageView(self.get_pixbuf)
     self.config_widget = GllConfigBox()
     self.config_widget.add_radio_buttons("profile_type",
                                          "Source profile type", "flat",
                                          [("Flat", "flat"),
                                           ("Gaussian", "gaussian")])
     show_profile = GllConfigCheckButton("Show profile")
     show_profile.set_active(True)
     self.config_widget.add_items([("source_radius", "Source radius",
                                    (0.01, 0.0, 1e10, 0.001), 4),
                                   ("export_kernel", show_profile)])
Ejemplo n.º 7
0
 def __init__(self):
     super(GllSourcePath, self).__init__()
     self.main_widget = GllImageView(self.get_pixbuf)
     self.tool = ImageToolSourcePath()
     self.main_widget.imageview.set_tool(self.tool)
     self.config_widget = GllConfigBox([
         ("curve_x0", "Start x coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
         ("curve_y0", "Start y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
         ("curve_x1", "End x coordinate", (1.0, -1e10, 1e10, 0.01), 4),
         ("curve_y1", "End y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
         ("curve_samples", "Number of samples", (256, 0, 1000000, 16), 0)
     ])
     for key in ["curve_x0", "curve_y0", "curve_x1", "curve_y1"]:
         self.config_widget[key].adjustment.connect("value_changed",
                                                    self.coords_changed)
Ejemplo n.º 8
0
 def __init__(self):
     super(GllRayshooter, self).__init__(ll.Rayshooter)
     self.main_widget = GllImageView(self.get_pixbuf)
     self.imageview = self.main_widget.imageview
     self.imageview.add_events(gtk.gdk.BUTTON_PRESS_MASK |
                               gtk.gdk.POINTER_MOTION_MASK |
                               gtk.gdk.LEAVE_NOTIFY_MASK)
     self.imageview.connect("button-press-event", self.imageview_clicked)
     self.imageview.connect("motion-notify-event", self.imageview_motion)
     self.imageview.connect("leave-notify-event", self.imageview_leave)
     self.main_widget.connect("parent-set", self.imageview_leave)
     self.dragger = self.imageview.get_tool()
     self.selector = gtkimageview.ImageToolSelector(self.imageview)
     self.config_widget = GllConfigBox(
         [("xpixels", "Resolution x", (1024, 0, 16384, 16), 0),
          ("ypixels", "Resolution y", (1024, 0, 16384, 16), 0),
          ("density", "Ray density", (100.0, 0.0, 100000.0, 1.0), 1),
          ("num_threads", "Number of threads", (2, 0, 32, 1), 0)])
     self.config_widget.add_radio_buttons(
         "kernel", "Ray shooting kernel", "triangulated",
         [("Simple", "simple"),
          ("Bilinear", "bilinear"),
          ("Triangulated", "triangulated")])
     self.config_widget.add_toggle_group(
         "export_region", "Magnification pattern region", False,
         [("region_x0", "Left coordinate",  (-1.0, -1e10, 1e10, 0.01), 4),
          ("region_y0", "Lower coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
          ("region_x1", "Right coordinate", ( 1.0, -1e10, 1e10, 0.01), 4),
          ("region_y1", "Upper coordinate", ( 1.0, -1e10, 1e10, 0.01), 4)])
     self.config_widget.declare_record("region", "export_region")
     self.magpat = None
     self.region = None
Ejemplo n.º 9
0
 def __init__(self):
     GllLensConfig.__init__(self)
     self.builder = gtk.Builder()
     self.builder.add_from_file("glllenses.glade")
     self.builder.connect_signals(self)
     self.lens_list = self.builder.get_object("lens_list")
     self.lens_selection = self.builder.get_object("treeview").get_selection()
     self.lens_selection.set_mode(gtk.SELECTION_MULTIPLE)
     self.config_widget = GllConfigBox()
     self.config_widget.add_toggle_group(
         "export_region", "Magnification pattern region", True,
         [("region_x0", "Left coordinate",  (-1.0, -1e10, 1e10, 0.01), 4),
          ("region_y0", "Lower coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
          ("region_x1", "Right coordinate", ( 1.0, -1e10, 1e10, 0.01), 4),
          ("region_y1", "Upper coordinate", ( 1.0, -1e10, 1e10, 0.01), 4)])
     self.config_widget.add(self.builder.get_object("lens_box"))
     self.config_widget.declare_record("region", "export_region")
Ejemplo n.º 10
0
class GllLenses(GllLensConfig):
    name = "Lens configuration"

    def __init__(self):
        GllLensConfig.__init__(self)
        self.builder = gtk.Builder()
        self.builder.add_from_file("glllenses.glade")
        self.builder.connect_signals(self)
        self.lens_list = self.builder.get_object("lens_list")
        self.lens_selection = self.builder.get_object("treeview").get_selection()
        self.lens_selection.set_mode(gtk.SELECTION_MULTIPLE)
        self.config_widget = GllConfigBox()
        self.config_widget.add_toggle_group(
            "export_region", "Magnification pattern region", True,
            [("region_x0", "Left coordinate",  (-1.0, -1e10, 1e10, 0.01), 4),
             ("region_y0", "Lower coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
             ("region_x1", "Right coordinate", ( 1.0, -1e10, 1e10, 0.01), 4),
             ("region_y1", "Upper coordinate", ( 1.0, -1e10, 1e10, 0.01), 4)])
        self.config_widget.add(self.builder.get_object("lens_box"))
        self.config_widget.declare_record("region", "export_region")

    def get_config(self):
        config = self.config_widget.get_config()
        all_lenses = map(tuple, self.lens_list)
        config["all_lenses"] = all_lenses
        config["lenses"] = [lens[1:] for lens in all_lenses if lens[0]]
        return config

    def set_config(self, config):
        self.config_widget.set_config(config)
        all_lenses = config.get("all_lenses")
        if all_lenses is not None:
            self.lens_list.clear()
            for lens in all_lenses:
                self.lens_list.append(lens)

    def toggle_lens(self, cell, path):
        self.lens_list[path][0] ^= True
    def edit_lens_cell1(self, cell, path, new_text):
        self.lens_list[path][1] = float(new_text)
    def edit_lens_cell2(self, cell, path, new_text):
        self.lens_list[path][2] = float(new_text)
    def edit_lens_cell3(self, cell, path, new_text):
        self.lens_list[path][3] = float(new_text)
    def add_lens(self, button):
        self.lens_list.append((True, 0.0, 0.0, 0.0))
    def delete_lens(self, button):
        lens_list, selected = self.lens_selection.get_selected_rows()
        for row_path in reversed(selected):
            lens_list.remove(lens_list.get_iter(row_path))
Ejemplo n.º 11
0
 def __init__(self):
     super(GllLightCurve, self).__init__(ll.light_curve)
     self.main_widget = gtk.DrawingArea()
     self.main_widget.set_size_request(400, 300)
     self.main_widget.show()
     self.main_widget.connect("expose-event", self.draw)
     self.config_widget = GllConfigBox()
     self.config_widget.add_toggle_group(
         "export_max", "Set upper magnification", False,
         ("curve_max_mag", "Upper magnification", (10.0, 0.0, 1e10, 1.0), 1))
     self.points = []
Ejemplo n.º 12
0
class GllSourceProfile(GllPlugin):
    name = "Source profile"

    def __init__(self):
        super(GllSourceProfile, self).__init__(ll.source_profile)
        self.main_widget = GllImageView(self.get_pixbuf)
        self.config_widget = GllConfigBox()
        self.config_widget.add_radio_buttons("profile_type",
                                             "Source profile type", "flat",
                                             [("Flat", "flat"),
                                              ("Gaussian", "gaussian")])
        show_profile = GllConfigCheckButton("Show profile")
        show_profile.set_active(True)
        self.config_widget.add_items([("source_radius", "Source radius",
                                       (0.01, 0.0, 1e10, 0.001), 4),
                                      ("export_kernel", show_profile)])

    def update(self, data):
        self.show = data["export_kernel"]
        if self.show:
            self.source = data["source_profile"]
        self.main_widget.mark_dirty()

    def set_config(self, config):
        config.setdefault("export_kernel", True)
        GllPlugin.set_config(self, config)

    def get_pixbuf(self):
        if self.show:
            source = self.source
        else:
            source = numpy.ones((1, 1))
        ypixels, xpixels = source.shape
        source = numpy.vstack((source[ypixels // 2:], source[:ypixels // 2]))
        source = numpy.hstack(
            (source[:, xpixels // 2:], source[:, :xpixels // 2]))
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, xpixels,
                                ypixels)
        array = numpy.rollaxis(pixbuf.get_pixels_array(), 2, 0)
        array[:] = source * (255 / source.max())
        return pixbuf
Ejemplo n.º 13
0
 def __init__(self):
     super(GllSourceProfile, self).__init__(ll.source_profile)
     self.main_widget = GllImageView(self.get_pixbuf)
     self.config_widget = GllConfigBox()
     self.config_widget.add_radio_buttons(
         "profile_type", "Source profile type", "flat",
         [("Flat", "flat"), ("Gaussian", "gaussian")])
     show_profile = GllConfigCheckButton("Show profile")
     show_profile.set_active(True)
     self.config_widget.add_items(
         [("source_radius", "Source radius", (0.01, 0.0, 1e10, 0.001), 4),
          ("export_kernel", show_profile)])
Ejemplo n.º 14
0
class GllSourceProfile(GllPlugin):
    name = "Source profile"

    def __init__(self):
        super(GllSourceProfile, self).__init__(ll.source_profile)
        self.main_widget = GllImageView(self.get_pixbuf)
        self.config_widget = GllConfigBox()
        self.config_widget.add_radio_buttons(
            "profile_type", "Source profile type", "flat",
            [("Flat", "flat"), ("Gaussian", "gaussian")])
        show_profile = GllConfigCheckButton("Show profile")
        show_profile.set_active(True)
        self.config_widget.add_items(
            [("source_radius", "Source radius", (0.01, 0.0, 1e10, 0.001), 4),
             ("export_kernel", show_profile)])

    def update(self, data):
        self.show = data["export_kernel"]
        if self.show:
            self.source = data["source_profile"]
        self.main_widget.mark_dirty()

    def set_config(self, config):
        config.setdefault("export_kernel", True)
        GllPlugin.set_config(self, config)

    def get_pixbuf(self):
        if self.show:
            source = self.source
        else:
            source = numpy.ones((1, 1))
        ypixels, xpixels = source.shape
        source = numpy.vstack((source[ypixels//2:], source[:ypixels//2]))
        source = numpy.hstack((source[:,xpixels//2:], source[:,:xpixels//2]))
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
                                xpixels, ypixels)
        array = numpy.rollaxis(pixbuf.get_pixels_array(), 2, 0)
        array[:] = source*(255/source.max())
        return pixbuf
Ejemplo n.º 15
0
 def __init__(self):
     super(GllSourcePath, self).__init__()
     self.main_widget = GllImageView(self.get_pixbuf)
     self.tool = ImageToolSourcePath()
     self.main_widget.imageview.set_tool(self.tool)
     self.config_widget = GllConfigBox(
         [("curve_x0", "Start x coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
          ("curve_y0", "Start y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
          ("curve_x1", "End x coordinate", (1.0, -1e10, 1e10, 0.01), 4),
          ("curve_y1", "End y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
          ("curve_samples", "Number of samples", (256, 0, 1000000, 16), 0)])
     for key in ["curve_x0", "curve_y0", "curve_x1", "curve_y1"]:
         self.config_widget[key].adjustment.connect(
             "value_changed", self.coords_changed)
Ejemplo n.º 16
0
class GllSourcePath(GllPlugin):
    name = "Source path"

    def __init__(self):
        super(GllSourcePath, self).__init__()
        self.main_widget = GllImageView(self.get_pixbuf)
        self.tool = ImageToolSourcePath()
        self.main_widget.imageview.set_tool(self.tool)
        self.config_widget = GllConfigBox([
            ("curve_x0", "Start x coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
            ("curve_y0", "Start y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
            ("curve_x1", "End x coordinate", (1.0, -1e10, 1e10, 0.01), 4),
            ("curve_y1", "End y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
            ("curve_samples", "Number of samples", (256, 0, 1000000, 16), 0)
        ])
        for key in ["curve_x0", "curve_y0", "curve_x1", "curve_y1"]:
            self.config_widget[key].adjustment.connect("value_changed",
                                                       self.coords_changed)

    def update(self, data):
        try:
            self.xpixels = data["xpixels"]
            self.ypixels = data["ypixels"]
            self.region = data["magpat"].region
            self.pixbuf = data["magpat_pixbuf"]
        except KeyError:
            return
        self.update_coords(data)

    def update_coords(self, data):
        pixels_per_width = self.xpixels / self.region.width
        pixels_per_height = self.ypixels / self.region.height
        self.tool.coords = (
            (data["curve_x0"] - self.region.x0) * pixels_per_width,
            (self.region.y1 - data["curve_y0"]) * pixels_per_height,
            (data["curve_x1"] - self.region.x0) * pixels_per_width,
            (self.region.y1 - data["curve_y1"]) * pixels_per_height)
        self.main_widget.mark_dirty()

    def get_pixbuf(self):
        return self.pixbuf

    def coords_changed(self, *args):
        if hasattr(self, "pixbuf"):
            self.update_coords(self.config_widget.get_config())

    def save_png(self, *args):
        filename = save_dialog("Save Magnification Pattern Image",
                               [("PNG Image Files", "*.png")])
        if filename is None:
            return
        pixmap = gtk.gdk.Pixmap(None, self.xpixels, self.ypixels, 24)
        cr = pixmap.cairo_create()
        cr.set_source_pixbuf(self.pixbuf, 0, 0)
        cr.paint()
        cr.move_to(*self.tool.coords[:2])
        cr.line_to(*self.tool.coords[2:])
        cr.set_source_rgb(1.0, 1.0, 1.0)
        cr.stroke()
        cr.get_target().write_to_png(filename)

    def get_actions(self):
        return [("Save PNG", self.save_png, hasattr(self, "pixbuf"))]
Ejemplo n.º 17
0
class GllLightCurve(GllPlugin):
    name = "Light curve"

    def __init__(self):
        super(GllLightCurve, self).__init__(ll.light_curve)
        self.main_widget = gtk.DrawingArea()
        self.main_widget.set_size_request(400, 300)
        self.main_widget.show()
        self.main_widget.connect("expose-event", self.draw)
        self.config_widget = GllConfigBox()
        self.config_widget.add_toggle_group(
            "export_max", "Set upper magnification", False,
            ("curve_max_mag", "Upper magnification", (10.0, 0.0, 1e10, 1.0), 1))
        self.points = []

    def update(self, data):
        curve = data["light_curve"]
        self.max_mag = data.get("curve_max_mag")
        disp_max = self.max_mag
        if self.max_mag is None:
            disp_max = curve.max() * 1.01
        factor = 1.0
        while True:
            if disp_max <= 1.2:
                disp_max = 2 * round(0.5*disp_max + 0.05, 1)
                step = 0.2
                break
            if disp_max <= 3.0:
                disp_max = 5 * round(0.2*disp_max + 0.05, 1)
                step = 0.5
                break
            if disp_max <= 6.0:
                disp_max = round(disp_max + 0.5, 0)
                step = 1.0
                break
            factor *= 10.0
            disp_max /= 10.0
        if self.max_mag is None:
            self.max_mag = factor * disp_max
        self.grid_step = factor * step
        self.grid_lines = int(round(self.max_mag/self.grid_step, 10)) + 1
        self.num_samples = len(curve)
        self.points = [(i, mag) for i, mag in enumerate(curve) if mag]

    def draw(self, area, event):
        if not self.points:
            return
        width, height = area.window.get_size()
        cr = area.window.cairo_create()
        cr.rectangle(event.area.x, event.area.y,
                      event.area.width, event.area.height)
        cr.clip()
        pixel_mat = cr.get_matrix()

        # Transform to graph coordinates
        cr.translate(0.1 * width, 0.9 * height)
        cr.scale(0.8*width / (self.num_samples-1), -0.8*height / self.max_mag)
        graph_mat = cr.get_matrix()

        # Render background and frame
        cr.rectangle(0.0, 0.0, self.num_samples-1, self.max_mag)
        rect = cr.copy_path()
        cr.set_source_color(area.style.white)
        cr.fill_preserve()
        cr.set_matrix(pixel_mat)
        cr.set_source_color(area.style.black)
        cr.set_line_width(0.5)
        cr.stroke()

        # render the grid lines
        cr.set_matrix(graph_mat)
        for i in range(1, self.grid_lines):
            y = i * self.grid_step
            cr.move_to(0, y)
            cr.line_to(self.num_samples-1, y)
        cr.set_matrix(pixel_mat)
        cr.set_line_width(0.25)
        cr.stroke()

        # Render the axis marks
        cr.set_font_size(12.0)
        cr.set_source_color(area.style.text[area.state])
        pix_x = 0.1*width - 5.0
        for i in range(self.grid_lines):
            y = i * self.grid_step
            text = "%#.2g" % y
            xbear, ybear, w, h, xadv, yadv = cr.text_extents(text)
            pix_y = (0.9 - 0.8*y / self.max_mag) * height
            cr.move_to(pix_x - w - xbear, pix_y - h/2.0 - ybear)
            cr.show_text(text)

        # Render the curve
        cr.set_matrix(graph_mat)
        cr.append_path(rect)
        cr.clip()
        cr.move_to(*self.points[0])
        for p in self.points[1:]:
            cr.line_to(*p)
        cr.set_matrix(pixel_mat)
        cr.set_source_rgb(0.75, 0.0, 0.0)
        cr.set_line_width(2.0)
        cr.stroke()
Ejemplo n.º 18
0
class GllRayshooter(GllPlugin):
    name = "Magnification pattern"

    def __init__(self):
        super(GllRayshooter, self).__init__(ll.Rayshooter)
        self.main_widget = GllImageView(self.get_pixbuf)
        self.imageview = self.main_widget.imageview
        self.imageview.add_events(gtk.gdk.BUTTON_PRESS_MASK |
                                  gtk.gdk.POINTER_MOTION_MASK |
                                  gtk.gdk.LEAVE_NOTIFY_MASK)
        self.imageview.connect("button-press-event", self.imageview_clicked)
        self.imageview.connect("motion-notify-event", self.imageview_motion)
        self.imageview.connect("leave-notify-event", self.imageview_leave)
        self.main_widget.connect("parent-set", self.imageview_leave)
        self.dragger = self.imageview.get_tool()
        self.selector = gtkimageview.ImageToolSelector(self.imageview)
        self.config_widget = GllConfigBox(
            [("xpixels", "Resolution x", (1024, 0, 16384, 16), 0),
             ("ypixels", "Resolution y", (1024, 0, 16384, 16), 0),
             ("density", "Ray density", (100.0, 0.0, 100000.0, 1.0), 1),
             ("num_threads", "Number of threads", (2, 0, 32, 1), 0)])
        self.config_widget.add_radio_buttons(
            "kernel", "Ray shooting kernel", "triangulated",
            [("Simple", "simple"),
             ("Bilinear", "bilinear"),
             ("Triangulated", "triangulated")])
        self.config_widget.add_toggle_group(
            "export_region", "Magnification pattern region", False,
            [("region_x0", "Left coordinate",  (-1.0, -1e10, 1e10, 0.01), 4),
             ("region_y0", "Lower coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
             ("region_x1", "Right coordinate", ( 1.0, -1e10, 1e10, 0.01), 4),
             ("region_y1", "Upper coordinate", ( 1.0, -1e10, 1e10, 0.01), 4)])
        self.config_widget.declare_record("region", "export_region")
        self.magpat = None
        self.region = None

    def get_config(self):
        config = self.config_widget.get_config()
        if self.region is not None and self.imageview.get_tool() is self.selector:
            rect = self.selector.get_selection()
            width = max(rect.width, rect.height*self.xpixels//self.ypixels)
            x = rect.x + (rect.width - width)//2
            x0 = self.region.x0 + x * self.xfactor
            height = max(rect.height, rect.width*self.ypixels//self.xpixels)
            y = rect.y + (rect.height - height)//2
            y0 = self.region.y1 - y * self.yfactor - height * self.yfactor
            config["region"] = dict(x0=x0, y0=y0,
                                    x1=x0 + width * self.xfactor,
                                    y1=y0 + height * self.yfactor)
        return config

    def update(self, data):
        self.magpat = data["magpat"]
        data["min_mag"] = self.magpat.min()
        data["max_mag"] = self.magpat.max()
        self.ypixels, self.xpixels = self.magpat.shape
        self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
                                     self.xpixels, self.ypixels)
        data["magpat_pixbuf"] = self.pixbuf
        self.magpat.render_gradient(buf=self.pixbuf.get_pixels_array())
        self.imageview.set_tool(self.dragger)
        self.main_widget.mark_dirty()
        self.region = self.magpat.region
        for key in ["x0", "x1", "y0", "y1"]:
            self.config_widget["region_" + key].set_value(
                getattr(self.region, key))
        self.xfactor = self.region.width / self.xpixels
        self.yfactor = self.region.height / self.ypixels
        xlog = log10(abs(self.region.x0) + abs(self.region.x1))
        if -2.0 <= xlog < 0.0:
            xprec = int(-log10(self.xfactor) + 1)
        else:
            xprec = int(xlog - log10(self.xfactor) + 1)
        if xlog < -2.0:
            self.xformat = "%" + (".%ie" % xprec)
        else:
            self.xformat = "%" + (".%if" % xprec)
        ylog = log10(abs(self.region.y0) + abs(self.region.y1))
        if -2.0 <= ylog < 0.0:
            yprec = int(-log10(self.yfactor) + 1)
        else:
            yprec = int(ylog - log10(self.yfactor) + 1)
        if ylog < -2.0:
            self.yformat = "%" + (".%ie" % yprec)
        else:
            self.yformat = "%" + (".%if" % yprec)

    def get_pixbuf(self):
        return self.pixbuf

    def imageview_clicked(self, imageview, event):
        if event.button == 1:
            if event.type == gtk.gdk._2BUTTON_PRESS:
                self.emit("run-pipeline")
        if event.button == 3:
            if imageview.get_tool() is self.dragger:
                imageview.set_tool(self.selector)
                self.config_widget["export_region"].set_active(True)
            else:
                imageview.set_tool(self.dragger)

    def imageview_motion(self, imageview, event):
        draw_rect = self.imageview.get_draw_rect()
        if self.region is None or draw_rect is None:
            return
        zoom = self.imageview.get_zoom()
        viewport = self.imageview.get_viewport()
        mag_x = (event.x - draw_rect.x + viewport.x) / zoom
        mag_y = self.ypixels - (event.y - draw_rect.y + viewport.y + 1) / zoom
        if (mag_x < 0 or mag_x >= self.xpixels or
            mag_y < 0 or mag_y >= self.ypixels):
            self.emit("statusbar-pop")
            return
        x = self.region.x0 + mag_x * self.xfactor
        y = self.region.y0 + mag_y * self.yfactor
        mag = self.magpat[mag_y][mag_x]
        text = (("x: " + self.xformat + ", y: " + self.yformat +
                 ", magnification: %.2f") % (x, y, mag))
        self.emit("statusbar-push", text)

    def imageview_leave(self, imageview, event):
        self.emit("statusbar-pop")

    def save_png(self, *args):
        filename = save_dialog("Save Magnification Pattern Image",
                               [("PNG Image Files", "*.png")])
        if filename is not None:
            self.pixbuf.save(filename, "png")

    def save_fits(self, *args):
        filename = save_dialog("Save Magnification Pattern",
                               [("FITS Image Files", "*.fits")])
        if filename is not None:
            self.magpat.write_fits(filename)

    def get_actions(self):
        save_sensitive = hasattr(self, "pixbuf")
        return [("Save FITS", self.save_fits, save_sensitive),
                ("Save PNG", self.save_png, save_sensitive)]
Ejemplo n.º 19
0
class GllLightCurve(GllPlugin):
    name = "Light curve"

    def __init__(self):
        super(GllLightCurve, self).__init__(ll.light_curve)
        self.main_widget = gtk.DrawingArea()
        self.main_widget.set_size_request(400, 300)
        self.main_widget.show()
        self.main_widget.connect("expose-event", self.draw)
        self.config_widget = GllConfigBox()
        self.config_widget.add_toggle_group(
            "export_max", "Set upper magnification", False,
            ("curve_max_mag", "Upper magnification",
             (10.0, 0.0, 1e10, 1.0), 1))
        self.points = []

    def update(self, data):
        curve = data["light_curve"]
        self.max_mag = data.get("curve_max_mag")
        disp_max = self.max_mag
        if self.max_mag is None:
            disp_max = curve.max() * 1.01
        factor = 1.0
        while True:
            if disp_max <= 1.2:
                disp_max = 2 * round(0.5 * disp_max + 0.05, 1)
                step = 0.2
                break
            if disp_max <= 3.0:
                disp_max = 5 * round(0.2 * disp_max + 0.05, 1)
                step = 0.5
                break
            if disp_max <= 6.0:
                disp_max = round(disp_max + 0.5, 0)
                step = 1.0
                break
            factor *= 10.0
            disp_max /= 10.0
        if self.max_mag is None:
            self.max_mag = factor * disp_max
        self.grid_step = factor * step
        self.grid_lines = int(round(self.max_mag / self.grid_step, 10)) + 1
        self.num_samples = len(curve)
        self.points = [(i, mag) for i, mag in enumerate(curve) if mag]

    def draw(self, area, event):
        if not self.points:
            return
        width, height = area.window.get_size()
        cr = area.window.cairo_create()
        cr.rectangle(event.area.x, event.area.y, event.area.width,
                     event.area.height)
        cr.clip()
        pixel_mat = cr.get_matrix()

        # Transform to graph coordinates
        cr.translate(0.1 * width, 0.9 * height)
        cr.scale(0.8 * width / (self.num_samples - 1),
                 -0.8 * height / self.max_mag)
        graph_mat = cr.get_matrix()

        # Render background and frame
        cr.rectangle(0.0, 0.0, self.num_samples - 1, self.max_mag)
        rect = cr.copy_path()
        cr.set_source_color(area.style.white)
        cr.fill_preserve()
        cr.set_matrix(pixel_mat)
        cr.set_source_color(area.style.black)
        cr.set_line_width(0.5)
        cr.stroke()

        # render the grid lines
        cr.set_matrix(graph_mat)
        for i in range(1, self.grid_lines):
            y = i * self.grid_step
            cr.move_to(0, y)
            cr.line_to(self.num_samples - 1, y)
        cr.set_matrix(pixel_mat)
        cr.set_line_width(0.25)
        cr.stroke()

        # Render the axis marks
        cr.set_font_size(12.0)
        cr.set_source_color(area.style.text[area.state])
        pix_x = 0.1 * width - 5.0
        for i in range(self.grid_lines):
            y = i * self.grid_step
            text = "%#.2g" % y
            xbear, ybear, w, h, xadv, yadv = cr.text_extents(text)
            pix_y = (0.9 - 0.8 * y / self.max_mag) * height
            cr.move_to(pix_x - w - xbear, pix_y - h / 2.0 - ybear)
            cr.show_text(text)

        # Render the curve
        cr.set_matrix(graph_mat)
        cr.append_path(rect)
        cr.clip()
        cr.move_to(*self.points[0])
        for p in self.points[1:]:
            cr.line_to(*p)
        cr.set_matrix(pixel_mat)
        cr.set_source_rgb(0.75, 0.0, 0.0)
        cr.set_line_width(2.0)
        cr.stroke()
Ejemplo n.º 20
0
class GllSourcePath(GllPlugin):
    name = "Source path"

    def __init__(self):
        super(GllSourcePath, self).__init__()
        self.main_widget = GllImageView(self.get_pixbuf)
        self.tool = ImageToolSourcePath()
        self.main_widget.imageview.set_tool(self.tool)
        self.config_widget = GllConfigBox(
            [("curve_x0", "Start x coordinate", (-1.0, -1e10, 1e10, 0.01), 4),
             ("curve_y0", "Start y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
             ("curve_x1", "End x coordinate", (1.0, -1e10, 1e10, 0.01), 4),
             ("curve_y1", "End y coordinate", (0.0, -1e10, 1e10, 0.01), 4),
             ("curve_samples", "Number of samples", (256, 0, 1000000, 16), 0)])
        for key in ["curve_x0", "curve_y0", "curve_x1", "curve_y1"]:
            self.config_widget[key].adjustment.connect(
                "value_changed", self.coords_changed)

    def update(self, data):
        try:
            self.xpixels = data["xpixels"]
            self.ypixels = data["ypixels"]
            self.region = data["magpat"].region
            self.pixbuf = data["magpat_pixbuf"]
        except KeyError:
            return
        self.update_coords(data)

    def update_coords(self, data):
        pixels_per_width = self.xpixels / self.region.width
        pixels_per_height = self.ypixels / self.region.height
        self.tool.coords = (
            (data["curve_x0"] - self.region.x0) * pixels_per_width,
            (self.region.y1 - data["curve_y0"]) * pixels_per_height,
            (data["curve_x1"] - self.region.x0) * pixels_per_width,
            (self.region.y1 - data["curve_y1"]) * pixels_per_height)
        self.main_widget.mark_dirty()

    def get_pixbuf(self):
        return self.pixbuf

    def coords_changed(self, *args):
        if hasattr(self, "pixbuf"):
            self.update_coords(self.config_widget.get_config())

    def save_png(self, *args):
        filename = save_dialog("Save Magnification Pattern Image",
                               [("PNG Image Files", "*.png")])
        if filename is None:
            return
        pixmap = gtk.gdk.Pixmap(None, self.xpixels, self.ypixels, 24)
        cr = pixmap.cairo_create()
        cr.set_source_pixbuf(self.pixbuf, 0, 0)
        cr.paint()
        cr.move_to(*self.tool.coords[:2])
        cr.line_to(*self.tool.coords[2:])
        cr.set_source_rgb(1.0, 1.0, 1.0)
        cr.stroke()
        cr.get_target().write_to_png(filename)

    def get_actions(self):
        return [("Save PNG", self.save_png, hasattr(self, "pixbuf"))]