def load_csv(self, filename): '''Populates the grid with the the data in a CSV file. filename -- the path to a CSV file to load ''' # # XXX: try using linecache so we don't need to load the whole file. # # infer types r = csv.reader(open(filename)) dtable = dbconnect.get_data_table_from_csv_reader(r) first_row_types = db.InferColTypesFromData([dtable[0]], len(dtable[0])) coltypes = db.InferColTypesFromData(dtable[1:], len(dtable[0])) has_header_row = False if (not all([a == b for a, b in zip(first_row_types, coltypes)]) and all([a.startswith('VARCHAR') for a in first_row_types]) and not all([b.startswith('VARCHAR') for b in coltypes])): has_header_row = True for i in range(len(coltypes)): if coltypes[i] == 'INT': coltypes[i] = int elif coltypes[i] == 'FLOAT': coltypes[i] = np.float32 else: coltypes[i] = str # read data r = csv.reader(open(filename)) if has_header_row: labels = r.next() else: labels = None data = [] for row in r: data += [[coltypes[i](v) for i, v in enumerate(row)]] data = np.array(data, dtype=object) table_base = PlainTable(self, data, labels) self.grid.SetTable(table_base, True) self.grid.Refresh() self.SetTitle(filename) self.RescaleGrid() self.grid.SetSelectionMode(self.grid.wxGridSelectColumns)
def load_csv(self, filename): '''Populates the grid with the the data in a CSV file. filename -- the path to a CSV file to load ''' # # XXX: try using linecache so we don't need to load the whole file. # # infer types r = csv.reader(open(filename)) dtable = dbconnect.get_data_table_from_csv_reader(r) first_row_types = db.InferColTypesFromData([dtable[0]], len(dtable[0])) coltypes = db.InferColTypesFromData(dtable[1:], len(dtable[0])) has_header_row = False if (not all([a == b for a, b in zip(first_row_types, coltypes)]) and all([a.startswith('VARCHAR') for a in first_row_types]) and not all([b.startswith('VARCHAR') for b in coltypes])): has_header_row = True for i in range(len(coltypes)): if coltypes[i] == 'INT': coltypes[i] = int elif coltypes[i] == 'FLOAT': coltypes[i] = np.float32 else: coltypes[i] = str # read data r = csv.reader(open(filename)) if has_header_row: labels = r.next() else: labels = None data = [] for row in r: data += [[coltypes[i](v) for i,v in enumerate(row)]] data = np.array(data, dtype=object) table_base = PlainTable(self, data, labels) self.grid.SetTable(table_base, True) self.grid.Refresh() self.SetTitle(filename) self.RescaleGrid() self.grid.SetSelectionMode(self.grid.wxGridSelectColumns)
def LoadCSV(self, csvfile, group='Image'): try: self.grid.Destroy() except: pass try: # Remove the previous column show/hide menu (should be the third menu) self.GetMenuBar().Remove(2) self.colmenu.Destroy() except: pass r = csv.reader(open(csvfile)) labels = r.next() dtable = dbconnect.get_data_table_from_csv_reader(r) coltypes = db.InferColTypesFromData(dtable, len(labels)) for i in range(len(coltypes)): if coltypes[i] == 'INT': coltypes[i] = int elif coltypes[i] == 'FLOAT': coltypes[i] = float else: coltypes[i] = str r = csv.reader(open(csvfile)) r.next() # skip col-headers data = [] for row in r: data += [[coltypes[i](v) for i,v in enumerate(row)]] data = np.array(data, dtype=object) if group == DO_NOT_LINK_TO_IMAGES: keycols = [] elif group == 'Image': keycols = range(len(dbconnect.image_key_columns())) else: keycols = range(len(dm.GetGroupColumnNames(group))) self.grid = HugeTableGrid(self, data, labels, key_col_indices=keycols, grouping=group, chMap=p.image_channel_colors) self.Title = '%s (%s)'%(csvfile, group) self.file = csvfile self.CreateColumnMenu() self.RescaleGrid()