📄 np_data.py
字号:
#! /usr/bin/env python################################################################################ ## Copyright 2005 University of Cambridge Computer Laboratory. ## ## This file is part of Nprobe. ## ## Nprobe is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## Nprobe is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Nprobe; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## ################################################################################################################################################################ ## ## Data set class and data loader for np_plot## ################################################################################ ############################################################################import osimport tkSimpleDialog, tkFileDialogfrom Tkinter import *from signal import *from np_widgets import Dialogue, BetterEntryfrom np_drawutil import field_l2str, field_str2l, fields_eqimport mathfrom math import *import re############################################################################ ############################################################################## Basic data set types#DATA_TS = 0x1DATA_PDF = 0x2DATA_CDF = 0x4DATA_HIST = 0x8DATA_SM = 0x10## Histogram data only - denotes bucket ranges rather than discrete values#DATA_RANGES = 0x40############################################################################ ############################################################################## Edit types for data edit#DELETE = 0EDIT = 1############################################################################ ############################################################################## Data format for np_Plot:## DATA := [DataSet 0, DataSet 1, ...]## DataSet := (data, data type, path, tag, callback)## data := [data point 0, data point 1, ...]## statsinfo = (low_outliers, high outliers, bktsize)## outliers := [data]## data point := [xcoord, ycoord, [s.d., s.e.], (int)tag, [tag_list]]# optional s.d. and.s.e. if specified by fields attribute## Setting dataset tag to None will colour each plotted point determined by# point tag value, setting dataset tag to a value will determine uniform# colour for all points in the set#class DataSet: def __init__(self, data, type, path, tag, style=None, callback=None, fields=[], is_filter=0): dlen = len(data) if dlen == 0: raise EmptyDataSetError, path try: if type == DATA_TS: self.tsdata = data self.pdfdata = None self.cdfdata = None self.histdata = None self.tslen = dlen self.pdflen = None self.cdflen = None self.histlen = None elif type == DATA_PDF: self.pdfdata = data self.tsdata = None self.tsdata_sm = None self.cdfdata = None self.histdata = None self.pdflen = dlen self.tslen = None self.cdflen = None self.histlen = None elif type == DATA_CDF: self.cdfdata = data self.tsdata = None self.tsdata_sm = None self.pdfdata = None self.histdata = None self.cdflen = dlen self.tslen = None self.pdflen = None self.histlen = None elif type == DATA_HIST: self.histdata = data self.tsdata = None self.tsdata_sm = None self.pdfdata = None self.cdfdata = None self.histlen = dlen self.tslen = None self.pdflen = None self.cdflen = None except AttributeError: raise WrongDataTypeError self.tsdata_sm = {} self.pdfdata_sm = {} self.cdfdata_sm = {} self.histdata_sm = {} self.bktsz = None self.data = data self.len = dlen self.type = type self.path = path self.opath = '' # addition to label if smoothed self.tag = tag self.style = style self.callback = callback self.is_filter = is_filter if fields == None: self.fields = ['X', 'Y', 'T'] else: self.fields = fields self.edits = [] def printself(self): print 'len=%d type=%d path=%s' % (self.len, self.type, self.path) def initialise(self): #print 'initialising %s' % (self.path) self.data.sort() self.hide = IntVar() self.hidden = [0] self.docol = IntVar() self.docol.set(0) self.reset() def reset(self): #self.printself() self.hide.set(self.hidden[-1]) self.hidden = [self.hide.get()] self.curr_indices = [(0, self.len - 1)] try: #self.ops.configure(fg='black') self.ops.top.configure(state=NORMAL) self.ops.hide.configure(state=NORMAL) self.ops.col.configure(state=NORMAL) except AttributeError: # only one set pass############################################################################## ############################################################################### Empty data set exception#class EmptyDataSetError: def __init__(self, val): self.val = val def __str__(self): return self.val############################################################################## ############################################################################### Wrong data type exception#class WrongDataTypeError: def __init__(self, val): self.value = val def __str__(self): return self.value ############################################################################## ############################################################################### General data exception# class DataError(Exception): def __init__(self, value): self.value = value def __str__(self): return `self.value` ############################################################################## ############################################################################### Decorations for plots#class Plot_Decoration: def __init__(self, type, coords, colour='red', label=''): self.type = type self.coords = coords self.col = colour self.label = label ############################################################################## ############################################################################### Class to read data from file(s)##############################################################################class DataReader: def __init__(self, args, fields=[]): self.args = args self.fields = fields self.gets = [ None, self.get1, self.get2, self.get3, self.get4 ] ############################################################################# def get_float(self, s): try: f = float(s) except ValueError: toks = s.split('E') try: exp = int(toks[1]) except ValueError: # may be comma separated exp = int(toks[1][:-1]) f = float(toks[0])*pow(10, exp) return f ############################################################################## def get_file(self, dir): root = Tk() root.winfo_toplevel().title('Plot:') fnm = tkFileDialog.askopenfilename( \ title='Plot', initialdir=dir, filetypes=[('All files', '.*')]) root.destroy() del root return fnm ############################################################################## def get1(self, ss): return [float(ss[0])] ############################################################################## def get2(self, ss): return [float(ss[0]), float(ss[1])] ############################################################################## def get3(self, ss): return [float(ss[0]), float(ss[1]), float(ss[2])] ############################################################################## def get4(self, ss): return [float(ss[0]), float(ss[1]), float(ss[2]), float(ss[3])] ############################################################################## def get5(self, ss): return [float(ss[0]), float(ss[1]), float(ss[2]), float(ss[3]), float(ss[4])] ############################################################################## def get_values(self, f, getf): f.seek(0) lno = 0 data = [] try: for s in f.readlines(): lno += 1 if not len(s): break s = s.strip() if (not s) or s[0] == ' ' or s[0] =='' or s[0] =='\n' or s[0] == '#': continue ss = s.replace(',', ' ').split() data.append(getf(ss)) except IndexError: raise DataError, 'Malformed data at line %d in %s\'%s\'' % (lno, f.name, s.rstrip()) return data ############################################################################## def get_values_xing(self, f, getf): f.seek(0) lno = 0 data = [] x = 0 try: for s in f: lno += 1 if not len(s): break if (not s) or s[0] == '' or s[0] == ' ' or s[0] =='\n' or s[0] == '#': continue ss = s.split() d = getf(ss) d.insert(0, x) x += 1 data.append(d) except IndexError: raise DataError, 'Malformed data at line %d in %s\'%s\'' % (lno, f.name, s) return data ############################################################################## def get_stuff(self, files): def reslabel(s, key, line): s = s.replace(key, '')[:-1] ## if indata:## raise DataError, 'Plot directive follows data line %s' % (line) return s.replace('\\n', '\n') #print files #path = os.path.split(files[0])[0] title = '' xlab = '' ylab = '' decs = [] path = files[0] tagno = 0 tag = None sets = [] types = [] for fl in files: if fl[-4:] == '.cdf': type = DATA_CDF if fl[-5:] == '.hist': type = DATA_HIST elif fl[-4:] == '.pdf': type = DATA_PDF else: type = DATA_TS types.append(type) data = [] fields = thesefields = self.fields notags = 1 indata = 0 line = 0 xing = 1 f = open(fl, 'r') #for s in f: for s in f.readlines(): #print s line = line + 1 if not len(s): break if s[0] == ' ' or s[0] =='\n': continue if s[0] == '#': try: if s.find('#plot-title=') == 0: title = reslabel(s, '#plot-title=', line) if s.find('#plot-xlab=') == 0: xlab = reslabel(s, '#plot-xlab=', line) if s.find('#plot-ylab=') == 0: ylab = reslabel(s,'#plot-ylab=' , line) if s.find('#fields=') == 0: thesefields = field_str2l(reslabel(s,'#fields=' , line))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -