Ejemplo n.º 1
0
    def OnPlot(self, event):
        #r = self.GetData(self._path)
        #if r is None: return
        r = self.data

        xaxis = ""
        for i, button in enumerate(self.PlotOption["x"]):
            if button.GetValue(): xaxis = self._top[i]
    
        yaxis = []
        for i, button in enumerate(self.PlotOption["y"]):
            if button.GetValue(): yaxis.append(self._top[i])

        # Plot
        f = PlotFrame(None, -1, title=self._path)
        fig = f.get_figure()
        a = fig.add_subplot(1, 1, 1)
        for y in yaxis:
            #print r[y]
            a.plot(r[xaxis], r[y], label=y)

        a.legend()
    
        try:
            f.draw()
        except OverflowError, e:
            msg = "Too much data: %s." % e
            widgets.ErrorMessage(msg)
            f.Close()
Ejemplo n.º 2
0
class PlotMap(Plot):
    def LayoutFrame(self):
        self._colour_maps = [(name, cm.get_cmap(name)) \
                                 for name in ("gray", "spectral", "copper",
                                              "coolwarm", "ocean", "terrain",
                                              "bone", "prism", "pink",
                                              "rainbow", "binary", "brg",
                                              "afmhot", "autumn")
                             if cm.get_cmap(name) != None
                             ]
        self._choice = wx.Choice(self.frame.Panel, -1,
                                 choices=[name for name, map_t in \
                                              self._colour_maps])
        self.frame.MainSizer.Insert(1, self._choice, 0, wx.EXPAND|wx.ALL, 10)
    def getData(self, path):
        try:
            f = open(path, "r")
            X, Y, dx, dy = [int(i) for i in f.readline().split()]
            a = np.loadtxt(f)
            f.close()

            # Ensure indexing by zero
            a[:,0] -= 1
            a[:,1] -= 1
            b = np.zeros((Y, X))
            for x, y, z in a:
                x, y = int(x), int(y)
                b[x,y] = z

        except IOError, e:
            widgets.ErrorMessage("IOError for file %s: %s" % (path, e))
            return None
        except (ValueError, IndexError), e:
            widgets.ErrorMessage("Error parsing file %s: %s" % (path, e))
            return None
Ejemplo n.º 3
0
    def OnPlot(self, event):
        errors = []
        # Which columns to plot
        for xcol, button in enumerate(self.PlotOption["x"]):
            if button.GetValue(): break
        ycols = []
        for i, button in enumerate(self.PlotOption["y"]):
            if button.GetValue(): ycols.append(i)

        if not ycols:
            msg = "Please specify a variabele for y!"
            widgets.ErrorMessage(msg)
            return 

        x, ys = self.GetData(self._path, xcol, ycols)

        # Transformations to be applied to the data
        transformations = []
        for tname in self._transformations:
            if self.PlotOption[tname].GetValue():
                transformations.append((tname, Transformations[tname]))

        # Apply transformations
        for tname, t in transformations:
            try:
                xn, ysn = t(x[1], [data for conf, data in ys])
                x = (x[0], xn)
                ys = [(prev[0], data) for prev, data in zip(ys, ysn)]
            except (ValueError, ArithmeticError), e:
                msg = "Error applying transformation %s: %s" % (tname, e)
                errors.append(msg)
Ejemplo n.º 4
0
 def OnPlot(self, event):
     #print type(self._filegetter)
     path = self._filegetter.GetFileValue()
     #print path
     if not path:
         msg = "Please choose a file to plot!"
         widgets.ErrorMessage(msg)
         return
     self._plotter.ShowPlotWindow(self.parent, path)
Ejemplo n.º 5
0
 def GetData(self, path):
     errors = []
     try:
         f = open(path)
         lines = f.readlines()
         f.close()
     except IOError, e:
         msg = "Error reading file '%s'." % (path,)
         widgets.ErrorMessage(msg)
         return
Ejemplo n.º 6
0
    def LayoutFrame(self):
        GridSizer = wx.GridSizer(len(self.column_names) + 1, 3)
            
        GridSizer.Add((-1, -1))

        self._transformations = "Log", "Log-log", "Normalize"

        for axis in ("x", "y"):
            GridSizer.Add(wx.StaticText(self.frame.Panel, -1, axis),
                          flag=wx.ALIGN_RIGHT)
            self.PlotOption[axis] = []
        
        for name in self.column_names:
            text = wx.StaticText(self.frame.Panel, -1, name)
            GridSizer.Add(text,
                          flag=wx.ALIGN_LEFT)
            for axis, cls in ("x", wx.RadioButton), ("y", wx.CheckBox):
                obj = cls(self.frame.Panel, -1)
                GridSizer.Add(obj, 1, flag=wx.ALIGN_RIGHT)
                self.PlotOption[axis].append(obj)

        self.PlotOption["x"][0].SetValue(True)
        self.frame.MainSizer.Insert(0, (-1, -1), 0, wx.TOP|wx.BOTTOM, 10)
        self.frame.MainSizer.Insert(1, GridSizer, 0, wx.EXPAND|wx.RIGHT|
                                    wx.LEFT, 5)

        # Transformations
        for name in self._transformations:
            obj = wx.CheckBox(self.frame.Panel, -1, name)
            self.PlotOption[name] = obj
            self.frame.MainSizer.Add(obj, 0, wx.EXPAND|wx.RIGHT|wx.LEFT,
                                 border=5)
            self.frame.MainSizer.Add((-1, -1), 0, wx.BOTTOM, border=5)

        # Check valid columns and disable if the file is too short
        try:
            f = open_column_file(self._path)
            l = f.readline()
            for i in xrange(len(l.split()), len(self.PlotOption["x"])):
                self.PlotOption["x"][i].Enable(False)
                self.PlotOption["y"][i].Enable(False)
            f.close()
                
        except (IOError, EOFError), e:
            msg = "IOError: %s" % e
            widgets.ErrorMessage(msg)
            self.frame.PlotButton.Enable(False)
            self.frame.SetTitle("Error: %s" % self._path)
Ejemplo n.º 7
0
    def getData(self, path):
        try:
            f = open(path, "r")
            X, Y, dx, dy = [int(i) for i in f.readline().split()]
            a = np.loadtxt(f)
            f.close()

            # Ensure indexing by zero
            a[:,0] -= 1
            a[:,1] -= 1
            b = np.zeros((Y, X))
            for x, y, z in a:
                x, y = int(x), int(y)
                b[x,y] = z

        except IOError, e:
            widgets.ErrorMessage("IOError for file %s: %s" % (path, e))
            return None
Ejemplo n.º 8
0
    def OnPlotUU(self, event):
        s = self.lambda_choice.GetCurrentSelection()

        f = PlotFrame(None, -1, title="%s (wavelength = %s)" % 
                      (self._path, str(self.data["lambda"][s])))
        fig = f.get_figure()
        a = fig.add_subplot(1, 1, 1)
        
        for i, phi in enumerate(self.data["phi"]):
            a.plot(self.data["umu"], self.data["uu"][s][:,i],
                   label="phi=%f" % phi)

        a.legend()
        try:
            f.draw()
        except OverflowError, e:
            msg = "Too much data: %s." % e
            widgets.ErrorMessage(msg)
            f.Close()
Ejemplo n.º 9
0
 def GetData(self, path, xcol, ycols):
     """
     TODO: Rewrite with numpy
     Returns metadata and data
     """
     variables = [[] for i in xrange(self._l)]
     try:
         lcount = 0
         f = open(path)
         for l in f.readlines():
             lcount += 1
             if l.startswith("#"): continue
             l = l.split()
             for i in xrange(min(len(l), self._l)):
                 variables[i].append(float(l[i]))
         f.close()
         
     except (IOError, ValueError), e:
         msg = "Error in file %s at line %d: %s" % (path, lcount, e)
         widgets.ErrorMessage(msg)
Ejemplo n.º 10
0
    def OnPlot(self, event):
        selected_map = self._colour_maps[self._choice.GetCurrentSelection()][1]

        Z = self.getData(self._path)
        if Z is None: return
        #cm = self._colour_maps()[1]

        # Plot
        f = PlotFrame(None, -1, title=self._path)
        fig = f.get_figure()
        a = fig.add_subplot(1,1,1)

        im = a.imshow(Z, interpolation='bilinear', cmap=selected_map,
                        origin='lower')

        try:
            f.draw()
        except OverflowError, e:
            msg = "Too much data: %s" % e
            f.Close()
            widgets.ErrorMessage(msg)
Ejemplo n.º 11
0
class PlotBlock(Plot):
    def __init__(self, phi, umu):
        Plot.__init__(self)
        self._nphi = len(phi)
        self._numu = len(umu)

        self._top = ["lambda",
                     "edir",
                     "edn",
                     "eup",
                     "uavgdir",
                     "uavgdn",
                     "uavgup"]

        if self._numu > 0 and self._nphi == 0:
            self._top.append(self._numu)

    def ShowPlotWindow(self, parent, path):
        self._path = path
        self.frame = PlotWindow(parent, path)
        
        self.data = self.GetData(self._path)

        if not self.data:
            self.frame.Close()
            return

        GridSizer = wx.GridSizer(len(self._top) + 2, 3)
            
        GridSizer.Add((-1, -1))

        for axis in ("x", "y"):
            GridSizer.Add(wx.StaticText(self.frame.Panel, -1, axis),
                          flag=wx.ALIGN_RIGHT)
            self.PlotOption[axis] = []
        
        for name in self._top:
            text = wx.StaticText(self.frame.Panel, -1, name)
            GridSizer.Add(text,
                          flag=wx.ALIGN_LEFT)
            for axis, cls in ("x", wx.RadioButton), ("y", wx.CheckBox):
                obj = cls(self.frame.Panel, -1)
                GridSizer.Add(obj, 1, flag=wx.ALIGN_RIGHT)
                self.PlotOption[axis].append(obj)

        self.PlotOption["x"][0].SetValue(True)
        self.frame.MainSizer.Insert(0, (-1, -1), 0, wx.TOP|wx.BOTTOM, 10)
        self.frame.MainSizer.Insert(1, GridSizer, 0, wx.EXPAND|wx.RIGHT|
                                    wx.LEFT, 5)

        self.frame.MainSizer.Add((-1, -1), 1)
        # Add umu control
        tmp_sizer = wx.BoxSizer(wx.HORIZONTAL)
        text = wx.StaticText(self.frame.Panel, -1, "uu(lambda) ")
        self.lambda_choice = wx.Choice(self.frame.Panel, -1,
                                  choices=[str(l) for l in self.data["lambda"]])
        
        self.lambda_choice.SetToolTipString("Wavelength")

        tmp_sizer.Add(text, 0, wx.RIGHT, 5)
        tmp_sizer.Add(self.lambda_choice)

        self.frame.MainSizer.Add(tmp_sizer, 0, wx.CENTER|wx.ALL, 5)
        
        plot_uu_button = wx.Button(self.frame.Panel, -1, "Plot")
        self.frame.MainSizer.Add(plot_uu_button, 0, wx.CENTER|wx.BOTTOM, 10)
        
        # Layout
        self.SetPerfectSize()

        self.frame.MainSizer.Layout()
        self.frame.Center()

        self.frame.Show()
        self.frame.Bind(wx.EVT_BUTTON, self.OnPlot,
                        self.frame.PlotButton)
        plot_uu_button.Bind(wx.EVT_BUTTON, self.OnPlotUU, plot_uu_button)

    def SetPerfectSize(self):
        w, h = 0, 0
        for obj in self.frame.MainSizer.GetChildren():
            r = obj.CalcMin()
            w = max(r[0], w)
            if r[1] > 0: h += r[1]
            org = obj.GetWindow() if obj.IsWindow() else obj.GetSizer()

        # Adjust for the title bar
        h += self.frame.GetSize()[1] - self.frame.GetClientSize()[1] \
            + 10

        #print self.frame.GetSize()
        #print self.frame.GetClientSize()
        
        size = (w, h)
        self.frame.SetSizeHints(*size)
        self.frame.SetSize(size)

    def GetNUmu(self):
        return self.numu_ctrl.GetValue()
    def GetNPhi(self):
        return self.nphi_ctrl.GetValue()

    def GetData(self, path):
        errors = []
        try:
            f = open(path)
            lines = f.readlines()
            f.close()
        except IOError, e:
            msg = "Error reading file '%s'." % (path,)
            widgets.ErrorMessage(msg)
            return

        numu = -1
        nphi = -1

        umu = []
        phi = []
        top = [[] for i in xrange(len(self._top))]
        u0u = []
        uu = []
        u0u_block = []
        uu_block = []

        lcount = 1
        n = 0
        dn = 0
        block_size = 0
        first_block = True

        try:
            while n < len(lines):
                line = lines[n]
                if not line.strip() or line.startswith("#"):
                    n += 1
                    continue

                line = np.asarray([float(i) for i in lines[n].split()])

                if (block_size == 0 and dn == 0) or (block_size and \
                                                         (dn % block_size == 0)):
                    # In top
                    assert len(line) == len(top), "Wrong number of values!"
                    for a, b in zip(top, line):
                        a.append(b)
                elif (block_size == 0 and dn == 1) or block_size and \
                        (dn % block_size == 1):
                    # In phi or n * 1 blcok
                    if first_block:
                        nphi = len(line)
                        phi = line
                    elif first_block:
                        assert False, "Not implemented"
                else:
                    # In n*m blcok
                    # Guess end of block assuming wavelengths are above 2 nm
                    if first_block and (line[0] > 2.0 or len(line) != nphi + 2):
                        first_block = False
                        numu = len(umu)
                    
                        block_size = 2 + numu

                        u0u = [np.asarray(u0u_block)]
                        uu = [np.asarray(uu_block)]

                        uu_block = []
                        u0u_block = []

                        continue

                    assert len(line) == nphi + 2, "Wrong number of values."

                    if first_block:
                        umu.append(line[0])

                    u0u_block.append(line[1])
                    uu_block.append(line[2:])
                
                n += 1
                dn += 1
                if not first_block and dn % block_size == 0:
                    u0u.append(np.asarray(u0u_block))
                    uu.append(np.asarray(uu_block))

                    uu_block = []
                    u0u_block = []

        except ValueError, e:
            msg = "Value error: line %d: %s." % (n, repr(e))
            widgets.ErrorMessage(msg)
            self.frame.PlotButton.Enable(False)
            self.frame.SetTitle("Value error: %s" % self._path)
Ejemplo n.º 12
0
                a.plot(xdata, ydata, label=conf["name"])
    
        a.set_xlabel(x[0]["name"])
        a.legend()
        a.axis("tight")

        try:
            f.draw()
        except OverflowError, e:
            msg = "Too much data: %s" % e
            errors.append(msg)
            f.Close()
        else:
            f.Show()
        if errors:
            widgets.ErrorMessage(errors)
            
    

class PlotMap(Plot):
    def LayoutFrame(self):
        self._colour_maps = [(name, cm.get_cmap(name)) \
                                 for name in ("gray", "spectral", "copper",
                                              "coolwarm", "ocean", "terrain",
                                              "bone", "prism", "pink",
                                              "rainbow", "binary", "brg",
                                              "afmhot", "autumn")
                             if cm.get_cmap(name) != None
                             ]
        self._choice = wx.Choice(self.frame.Panel, -1,
                                 choices=[name for name, map_t in \