def on_save_table_to_db(self, evt): valid = False while not valid: dlg = wx.TextEntryDialog(self, 'What do you want to name your table?', 'Save table to database', self.Title) if dlg.ShowModal() != wx.ID_OK: return tablename = dlg.GetValue() if not re.match('^[A-Za-z]\w*$', tablename): wx.MessageDialog( self, 'Table name must begin with a letter and may' 'only contain letters, digits and "_"', 'Invalid table name', wx.OK | wx.ICON_INFORMATION).ShowModal() elif db.table_exists(tablename): dlg = wx.MessageDialog( self, 'The table "%s" already exists in the database. Overwrite it?' % (tablename), 'Table already exists', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_WARNING) if dlg.ShowModal() == wx.ID_YES: valid = True else: valid = True dlg = wx.SingleChoiceDialog( self, 'Do you want to be able to access\n' 'this table after you close CPA?', 'Save table to database', ['Store for this session only.', 'Store permanently.'], wx.CHOICEDLG_STYLE) if dlg.ShowModal() != wx.ID_OK: return temporary = (dlg.GetSelection() == 0) colnames = [ self.grid.Table.GetColLabelValueWithoutDecoration(col) for col in range(self.grid.Table.GetNumberCols()) ] data = [[ self.grid.Table.GetValue(row, col) for col in range(self.grid.Table.GetNumberCols()) ] for row in range(self.grid.Table.GetNumberRows())] db.CreateTableFromData(data, dbconnect.clean_up_colnames(colnames), tablename, temporary=temporary) self.Title = tablename try: wx.GetApp().user_tables += [tablename] for plot in wx.GetApp().get_plots(): if plot.tool_name == 'PlateViewer': plot.AddTableChoice(tablename) except AttributeError: # running without main UI user_tables = wx.GetApp().user_tables = []
def OnWriteTempTableToDB(self, evt): from classifier import Classifier db.CreateTempTableFromData(self.grid.GetTable().data, dbconnect.clean_up_colnames(self.grid.GetTable().col_labels), '__Classifier_output') try: if self.GetParent().pmb: self.GetParent().pmb.AddTableChoice('__Classifier_output') except: pass
def on_save_table_to_db(self, evt): valid = False while not valid: dlg = wx.TextEntryDialog(self, 'What do you want to name your table?', 'Save table to database', self.Title) if dlg.ShowModal() != wx.ID_OK: return tablename = dlg.GetValue() if not re.match('^[A-Za-z]\w*$', tablename): wx.MessageDialog(self, 'Table name must begin with a letter and may' 'only contain letters, digits and "_"', 'Invalid table name', wx.OK|wx.ICON_INFORMATION).ShowModal() elif db.table_exists(tablename): dlg = wx.MessageDialog(self, 'The table "%s" already exists in the database. Overwrite it?'%(tablename), 'Table already exists', wx.YES_NO|wx.NO_DEFAULT|wx.ICON_WARNING) if dlg.ShowModal() == wx.ID_YES: valid = True else: valid = True dlg = wx.SingleChoiceDialog(self, 'Do you want to be able to access\n' 'this table after you close CPA?', 'Save table to database', ['Store for this session only.', 'Store permanently.'], wx.CHOICEDLG_STYLE) if dlg.ShowModal() != wx.ID_OK: return temporary = (dlg.GetSelection() == 0) colnames = [self.grid.Table.GetColLabelValueWithoutDecoration(col) for col in range(self.grid.Table.GetNumberCols())] data = [[self.grid.Table.GetValue(row, col) for col in range(self.grid.Table.GetNumberCols())] for row in range(self.grid.Table.GetNumberRows())] db.CreateTableFromData(data, dbconnect.clean_up_colnames(colnames), tablename, temporary=temporary) self.Title = tablename try: wx.GetApp().user_tables += [tablename] for plot in wx.GetApp().get_plots(): if plot.tool_name == 'PlateViewer': plot.AddTableChoice(tablename) except AttributeError: # running without main UI user_tables = wx.GetApp().user_tables = []
def create_per_object_table(well_result_files): '''doc -- measurements_index DOM document object ''' def get_column_names(well_res_file): '''parses reported column names out of the given well_results_file''' ob_col_dict = {} doc = dom.parse(os.path.join(results_dir, well_res_file)) for ref in doc.getElementsByTagName('ParameterAnnotations'): for param in ref.getElementsByTagName('Parameter'): ob_col_dict[param.getAttribute('id')] = dict( param.attributes.items()) params_node = doc.getElementsByTagName( 'ResultTable')[0].getElementsByTagName('Parameters')[0] measurement_ids = [ p.getAttribute('parID') for p in params_node.getElementsByTagName('Parameter') ] return [ob_col_dict[id]['name'] for id in measurement_ids] # Get per-object column names default_columns = ['ImageNumber', 'ObjectNumber', 'Well', 'X', 'Y'] param_columns = get_column_names(well_result_files[0]) colnames = default_columns + param_columns coltypes = ['INTEGER', 'INTEGER', 'VARCHAR(3)', 'REAL', 'REAL'] coltypes += ['REAL'] * len(param_columns) colnames = dbconnect.clean_up_colnames(colnames) db.create_empty_table('per_object', colnames, coltypes) db.create_default_indexes_on_table('per_object') # create object measurement matrix for well_res_file in well_result_files: # # TODO: Object number should change for every image, not every well # object_number = 1 doc = dom.parse(os.path.join(results_dir, well_res_file)) platename = doc.getElementsByTagName('PlateName')[0].firstChild.data r = doc.getElementsByTagName('ResultTable')[0].getAttribute('Row') c = doc.getElementsByTagName('ResultTable')[0].getAttribute('Col') pid = doc.getElementsByTagName('ResultTable')[0].getAttribute( 'PlaneID') tid = doc.getElementsByTagName('ResultTable')[0].getAttribute( 'TimepointID') wellname = convert_rowcol_to_wellid(r, c) rows = [] for tr in doc.getElementsByTagName( 'ResultTable')[0].getElementsByTagName('tr'): fid = tr.getAttribute('FieldID') query = 'SELECT ImageNumber FROM per_image WHERE Plate="%s" AND ' \ 'Well="%s" AND FieldID="%s" AND PlaneID="%s" AND ' \ 'TimepointID="%s"'%(platename, wellname, fid, pid, tid) image_number = db.execute(query)[0][0] row = [ image_number, object_number, wellname, float(tr.getAttribute('x')), float(tr.getAttribute('y')) ] row += [ float(col.firstChild.data) for col in tr.childNodes if col.firstChild ] rows += [row] object_number += 1 # Insert rows from this well result file db.insert_rows_into_table('per_object', colnames, coltypes, rows) db.Commit()
def create_per_object_table(well_result_files): '''doc -- measurements_index DOM document object ''' def get_column_names(well_res_file): '''parses reported column names out of the given well_results_file''' ob_col_dict = {} doc = dom.parse(os.path.join(results_dir, well_res_file)) for ref in doc.getElementsByTagName('ParameterAnnotations'): for param in ref.getElementsByTagName('Parameter'): ob_col_dict[param.getAttribute('id')] = dict(param.attributes.items()) params_node = doc.getElementsByTagName('ResultTable')[0].getElementsByTagName('Parameters')[0] measurement_ids = [p.getAttribute('parID') for p in params_node.getElementsByTagName('Parameter')] return [ob_col_dict[id]['name'] for id in measurement_ids] # Get per-object column names default_columns = ['ImageNumber', 'ObjectNumber', 'Well', 'X', 'Y'] param_columns = get_column_names(well_result_files[0]) colnames = default_columns + param_columns coltypes = ['INTEGER', 'INTEGER', 'VARCHAR(3)', 'REAL', 'REAL'] coltypes += ['REAL'] * len(param_columns) colnames = dbconnect.clean_up_colnames(colnames) db.create_empty_table('per_object', colnames, coltypes) db.create_default_indexes_on_table('per_object') # create object measurement matrix for well_res_file in well_result_files: # # TODO: Object number should change for every image, not every well # object_number = 1 doc = dom.parse(os.path.join(results_dir, well_res_file)) platename = doc.getElementsByTagName('PlateName')[0].firstChild.data r = doc.getElementsByTagName('ResultTable')[0].getAttribute('Row') c = doc.getElementsByTagName('ResultTable')[0].getAttribute('Col') pid = doc.getElementsByTagName('ResultTable')[0].getAttribute('PlaneID') tid = doc.getElementsByTagName('ResultTable')[0].getAttribute('TimepointID') wellname = convert_rowcol_to_wellid(r, c) rows = [] for tr in doc.getElementsByTagName('ResultTable')[0].getElementsByTagName('tr'): fid = tr.getAttribute('FieldID') query = 'SELECT ImageNumber FROM per_image WHERE Plate="%s" AND ' \ 'Well="%s" AND FieldID="%s" AND PlaneID="%s" AND ' \ 'TimepointID="%s"'%(platename, wellname, fid, pid, tid) image_number = db.execute(query)[0][0] row = [image_number, object_number, wellname, float(tr.getAttribute('x')), float(tr.getAttribute('y'))] row += [float(col.firstChild.data) for col in tr.childNodes if col.firstChild] rows += [row] object_number += 1 # Insert rows from this well result file db.insert_rows_into_table('per_object', colnames, coltypes, rows) db.Commit()