Exemplo n.º 1
0
    def _make_box(self):
        radio_box = make_radio_list([("Image Fields", (self.image_hist, per_image_fields)),
                                     ("Image Data", (self.data_hist, [])),
                                     ("Image Stars", (self.image_stars_hist, per_star_fields)),
                                     ("Star Fields", (self.star_hist, per_image_fields + per_star_fields)),
                                     ("Sky Value", (self.sky_value_hist, []))],
                                    self.radio_callback)

        self.min_spin = make_float_spinner(0, 0, 1, 0.1)
        self.max_spin = make_float_spinner(1, 0, 1, 0.1)

        self.output_box = OutputList(["N: ", "mean: ", "stddev: "])

        button = make_button("set", self.replot)

        self.bin_spin = make_integer_spinner(5, 1000)
        
        radio_box.pack_start(make_box(gtk.HBox, [self.min_spin, self.max_spin, button]), False, False, 0)
        radio_box.pack_start(self.bin_spin, False, False, 0)
        radio_box.pack_start(self.output_box.box, False, False, 0)

        
        hbox = gtk.HBox()
        hbox.pack_start(radio_box, False, False, 0)
        hbox.pack_start(self.image_box)
        return hbox
Exemplo n.º 2
0
    def _make_imshow_box(self):

        self.output_box = OutputList([field.label for field in per_image_fields])
        radio_box = make_radio_list([("Image", (self.imshow,)),
                                     ("Raw Image", (self.raw_imshow,)),
                                     ("X axis spectra", (self.x_spectra,)),
                                     ("Edge", (self.edge,)),
                                     ("B&W", (self.threshold,))],
                                    self.radio_callback)

        marker_box = make_radio_list([("None", (self.noop,)),
                                      ("marker", (self.marker,)),
                                      ("stars", (self.stars,)),
                                      ("threshold", (self.threshold_points,))],
                                     self.marker_callback)
                                     

        button_box = self.make_left_right_arrows(self.parent.image_spin)
        radio_box.pack_start(marker_box, False, False, 0)
        radio_box.pack_start(button_box, False, False, 0)
        
        self.display_button = make_button("Display", self.display)        
        self.display16_button = make_button("Display x16", self.display16)
        self.comment, comment_box = make_entry_button(64, "comment", self.add_comment)

        disp_box = make_box(gtk.HBox, [self.display_button, self.display16_button])
        radio_box.pack_start(disp_box, False, False, 0)
        radio_box.pack_start(comment_box, False, False, 0)
    
        vbox = make_box(gtk.VBox, [self.output_box.box, radio_box])

        box = gtk.HBox()
        box.pack_start(vbox, False, False, 0)
        box.pack_start(self.image_box, True, True, 0)        
        return box
Exemplo n.º 3
0
class HistTab(Tab):

    label = "Histogram"

    def __init__(self, parent):
        Tab.__init__(self, parent)
        self.fields = per_image_fields + per_star_fields
        self.image_function = self.image_hist
        self.fields = per_image_fields

    def _make_box(self):
        radio_box = make_radio_list([("Image Fields", (self.image_hist, per_image_fields)),
                                     ("Image Data", (self.data_hist, [])),
                                     ("Image Stars", (self.image_stars_hist, per_star_fields)),
                                     ("Star Fields", (self.star_hist, per_image_fields + per_star_fields)),
                                     ("Sky Value", (self.sky_value_hist, []))],
                                    self.radio_callback)

        self.min_spin = make_float_spinner(0, 0, 1, 0.1)
        self.max_spin = make_float_spinner(1, 0, 1, 0.1)

        self.output_box = OutputList(["N: ", "mean: ", "stddev: "])

        button = make_button("set", self.replot)

        self.bin_spin = make_integer_spinner(5, 1000)
        
        radio_box.pack_start(make_box(gtk.HBox, [self.min_spin, self.max_spin, button]), False, False, 0)
        radio_box.pack_start(self.bin_spin, False, False, 0)
        radio_box.pack_start(self.output_box.box, False, False, 0)

        
        hbox = gtk.HBox()
        hbox.pack_start(radio_box, False, False, 0)
        hbox.pack_start(self.image_box)
        return hbox

    def replot(self, button):
        self.plot(use_range=True)

    def plot(self, use_range=False):
        self.set_busy_message("Plotting...")
        x, _, _ = self.parent.get_xyc()
        self.pre_draw()
        self.image_function(x, self.gca(), use_range)
        self.f.canvas.draw()
        self.unset_busy_message()

    def hist(self, x, ax, star_id, image_id, filter, use_range, data=None):
        bins = self.bin_spin.get_value_as_int()
        if data is None:
            data = self.query([x, SimpleField("image.id")], filter=filter, star_id=star_id, image_id=image_id)
            data = x.value(data)

        data = N.asarray(data)
        if use_range:
            min = self.min_spin.get_value()
            max = self.max_spin.get_value()
            data = data[min <= data]
            data = data[data <= max]

        if data.size:
            result = ax.hist(data, bins)
            ax.set_xlabel(str(x))
            if use_range:
                range = min, max, len(result[1])
            else:
                range = data.min(), data.max(), len(result[1])
                self.min_spin.set_range(range[0], range[1])
                self.max_spin.set_range(range[0], range[1])

            self.min_spin.set_value(range[0])
            self.max_spin.set_value(range[1])
            self.min_spin.set_increments((range[1] - range[0])/range[2], 0)
            self.max_spin.set_increments((range[1] - range[0])/range[2], 0)

            self.output_box.update([str(len(data)), str(data.mean()), str(data.std())])
            
            if x == "image.time":
                print "AHA, time!"
                ax.xaxis_date()
                self.f.autofmt_xdate()

        #data.sort()
        #thirty = data[int(0.3*len(data))]
        #fifty = data[int(0.5*len(data))]
        #ymin, ymax = ax.get_ylim()
        #ax.vlines([thirty, fifty], ymin, ymax)
        return data

    def image_hist(self, x, ax, use_range):
        return self.hist(x, ax, False, False, True, use_range)

    def data_hist(self, x, ax, use_range):
        data = get_image_data(self.get_image_id()).flat
        #data = get_raw_image_data(self.get_image_id()).flat
        return self.hist("pixel count", ax, None, None, None, use_range, data)

    def star_hist(self, x, ax, use_range):
        return self.hist(x, ax, True, False, True, use_range)

    def image_stars_hist(self, x, ax, use_range):
        return self.hist(x, ax, False, True, False, use_range)

    def sky_value_hist(self, x, ax, use_range):
        data = get_image_data(self.get_image_id())
        mu = data.flat.mean()
        data = data[600][320:1280]
        ret = self.hist("pixel count", ax, None, None, None, use_range, data)
        data.sort()
        thirty = data[int(0.3*len(data))]
        ymin, ymax = ax.get_ylim()
        ax.vlines([thirty, mu], ymin, ymax)
        return ret

    def radio_callback(self, button, fn, fields):
        self.fields = fields
        self.on_focus()
        self.image_function = fn
    
    def on_focus(self):
        refill_cb(self.parent.x_val_combo, self.fields, self.x)
        refill_cb(self.parent.y_val_combo, [], self.y)
        refill_cb(self.parent.c_val_combo, [], self.c)

    def get_hist_params(self):
        return (self.min_spin.get_value(), self.max_spin.get_value(), self.bin_spin.get_value_as_int())

    def set_hist_params(self, params):
        print "setting", params
        if params is not None:
            min, max, bin = params
            self.min_spin.set_value(float(min))
            self.max_spin.set_value(float(max))
            self.bin_spin.set_value(int(bin))
Exemplo n.º 4
0
class ImageTab(Tab):

    label = "Image"

    def __init__(self, parent):
        Tab.__init__(self, parent)

        self.image_function = self.imshow
        self.marker_function = self.noop
        self.fields = []
    
    def _make_box(self):
        im_box = self._make_imshow_box()
        ax = self.gca()
        ax.imshow([[1,2],[4,5]])
        return im_box

    def _make_imshow_box(self):

        self.output_box = OutputList([field.label for field in per_image_fields])
        radio_box = make_radio_list([("Image", (self.imshow,)),
                                     ("Raw Image", (self.raw_imshow,)),
                                     ("X axis spectra", (self.x_spectra,)),
                                     ("Edge", (self.edge,)),
                                     ("B&W", (self.threshold,))],
                                    self.radio_callback)

        marker_box = make_radio_list([("None", (self.noop,)),
                                      ("marker", (self.marker,)),
                                      ("stars", (self.stars,)),
                                      ("threshold", (self.threshold_points,))],
                                     self.marker_callback)
                                     

        button_box = self.make_left_right_arrows(self.parent.image_spin)
        radio_box.pack_start(marker_box, False, False, 0)
        radio_box.pack_start(button_box, False, False, 0)
        
        self.display_button = make_button("Display", self.display)        
        self.display16_button = make_button("Display x16", self.display16)
        self.comment, comment_box = make_entry_button(64, "comment", self.add_comment)

        disp_box = make_box(gtk.HBox, [self.display_button, self.display16_button])
        radio_box.pack_start(disp_box, False, False, 0)
        radio_box.pack_start(comment_box, False, False, 0)
    
        vbox = make_box(gtk.VBox, [self.output_box.box, radio_box])

        box = gtk.HBox()
        box.pack_start(vbox, False, False, 0)
        box.pack_start(self.image_box, True, True, 0)        
        return box

    def noop(self, ax, data, new_data):
        pass

    def display(self, button):
        image_id = self.get_image_id()
        self.set_busy_message("Displaying image %d..." % image_id )
        print "display", image_id
        single_display(image_id)
        self.unset_busy_message()
        
    def display16(self, button):
        self.set_busy_message("Displaying multiple images. This may take a while...")
        image_id = self.get_image_id()
        image_ids = [x for x in range(image_id-8, image_id + 8) if x > 0]
        print image_ids
        print len(image_ids)
        multi_display(image_ids)
        self.unset_busy_message()

    def add_comment(self, button):
        comment = self.comment.get_text()
        f = open("comments.txt", "a")
        f.write("%d: %s\n" % (self.get_image_id(), comment))
        f.close()


    def plot(self):
        image_id = self.get_image_id()
        self.update_image(image_id)

    def update_image(self, image_id):
        self.set_busy_message("Processing %d..." % image_id)
        result = self.query(per_image_fields, filter=False, image_id=image_id)

        self.output_box.update([field.str_value(result[0]) for field in per_image_fields])

        data = get_image_data(image_id)

        ax = self.pre_draw()

        new_data = self.image_function(ax, data)
        self.marker_function(ax, data, new_data)

        self.f.canvas.draw()
        self.unset_busy_message()

    def imshow(self, ax, data):
        mu, sd = data.mean(), data.stddev()
        data = clip(data, mu - 3*sd, mu + 3*sd)

        #data = signal.detrend(signal.detrend(data, axis=0), axis=1)
        #data -= data.min()
        #data = signal.spline_filter(data)

        cmap = self.get_cmap()
        im = ax.imshow(data, origin="lower", cmap=cmap, interpolation='nearest')
        self.set_callbacks([("button_release_event", self.imclick, ())])
        self.f.colorbar(im, fraction = 0.08)

        return data

    def raw_imshow(self, ax, data):
        data = get_raw_image_data(self.get_image_id())
        return self.imshow(ax, data)

    def edge(self, ax, data):

        data = N.array(data, dtype=float)
        arr = data.__array_interface__
        print arr['shape'], arr['typestr']

        image = PIL.Image.fromarray(N.array(data, dtype=float))
        print "SIZE", image.size, image.mode
        image = image.filter(PIL.ImageFilter.EDGE_ENHANCE)
        data = N.asarray(image)
        print "shape", data.shape
        #data = data.reshape((1200, 1600))

        cmap = self.get_cmap()
        im = ax.imshow(data, origin="lower", cmap=cmap, interpolation='nearest')
        self.set_callbacks([("button_release_event", self.imclick, ())])
        self.f.colorbar(im, fraction = 0.08)

        return data

    def marker(self, ax, data, new_data, star_id=True, condition=None):
        fields = [SimpleField("phot.X"), SimpleField("phot.Y")]
        data = self.query(fields, filter=False, image_id=True, star_id=star_id, condition=condition)
        if data.size:
            ax.scatter(data['X'] - 1, data['Y'] - 1, s=20, c='r')
        ax.set_xlim(0, 1600)
        ax.set_ylim(0, 1200)

    def stars(self, ax, data, new_data):
        return self.marker(ax, data, new_data, False, "phot.image_id = image.id")


    def imclick(self, event):
        if event.xdata is None or event.ydata is None:
            return
        x, y = event.xdata + 1, event.ydata + 1
        if x and y and self.toolbar._active not in ["ZOOM", "PAN"]:
            self.find_closest_star(x, y, self.get_image_id())
            if self.marker_function == self.marker:
                self.plot()

    def find_closest_star(self, x, y, image_id):
        sql = "select star_id, X, Y from phot where image_id=%d" % image_id
        data = run_sql(sql)
        if len(data):
            i = N.argmin((data['X'] - x)**2 + (data['Y'] - y)**2)
            star_id = data[i][0]
            self.set_busy_message("Processing star %d..." % star_id)
            self.parent.star_spin.set_value(star_id)
            self.unset_busy_message()
        else:
            print "no stars"

    def x_spectra(self, ax, data):

        data = N.array(data)
        fs = []
        for d in data:
            n = d.shape[0]
            x = [n] + [n/float(i) for i in range(1, len(d)/2 + 1)]
            f = N.abs(N.fft.rfft(d))
            fs.append(f)
        f = N.array(fs)
        fav = f.mean(axis=0)
        std = f.std(axis=0)
        print f.shape, fav.shape
        ax.plot(x, fav)
        ax.set_xlim(0, 100)
        ax.set_ylim(0, 20000)



    def threshold_points(self, ax, data, new_data):
        data = blur_image(data, 5) 
        ii, jj, ss = get_points(data, new_data)
        print "POINTS", len(ii)

        for i, j, s in zip(ii, jj, ss):
            if True:
                #print s
                #print "==="
                ax.hlines(i, j-s, j+s, colors='r')
                ax.vlines(j, i-s, i+s, colors='r')

        #ax.scatter(jj, ii, c='y')
        print "COUNT", len(ii)
        ax.set_xlim(0, 1600)
        ax.set_ylim(0, 1200)

        #self.stars(ax, data, new_data)

    def threshold(self, ax, data):
        data = N.asarray(data)
        mu, sd = data.mean(), data.std()

        data = clip(data, mu - 3*sd, mu + 3*sd)
        data = blur_image(data, 5) 


        orig = data.copy()

        f = fft.fft2(data)
        f[0:5,:] = 0
        f[:,0:5] = 0
        data = N.abs(fft.ifft2(f))


        print "mu, std =", orig.mean(), orig.std()

        """
        mu, sigma = data.mean(), data.std()
        mask = data > mu + 2*sigma
        """
        #im = ax.imshow(data, origin="lower", interpolation='nearest')
        im = ax.imshow(orig, origin="lower", interpolation='nearest')
        self.set_callbacks([("button_release_event", self.imclick, ())])
        self.f.colorbar(im, fraction = 0.08)
        return data

        
    def radio_callback(self, button, arg):
        self.image_function = arg

    def marker_callback(self, button, arg):
        self.marker_function = arg