def on_save_properties(self, evt): if not self.confirm_properties(): return p = Properties() dirname, filename = os.path.split(p._filename) ext = os.path.splitext(p._filename)[-1] dlg = wx.FileDialog(self, message="Save properties as...", defaultDir=dirname, defaultFile=filename, wildcard=ext, style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT | wx.FD_CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: p.save_file(dlg.GetPath())
def Start(self): '''Initialize CPA ''' if hasattr(sys, "frozen") and sys.platform == "darwin": # Some versions of Macos like to put CPA in a sandbox. If we're frozen Java should be packed in, # so let's just figure out the directory on run time. os.environ["CP_JAVA_HOME"] = os.path.abspath( os.path.join(sys.prefix, "..", "Resources/Home")) '''List of tables created by the user during this session''' self.user_tables = [] p = Properties() self.frame = MainGUI(p, None, size=(1000, -1)) self.frame.Show() # Show frame if not p.is_initialized(): from cpa.guiutils import show_load_dialog try: if not show_load_dialog(): example_link_address = 'cellprofileranalyst.org' dlg = wx.MessageDialog( None, 'CellProfiler Analyst requires a properties file. Download an example at %s' % (example_link_address), 'Properties file required', wx.OK) response = dlg.ShowModal() logging.error( 'CellProfiler Analyst requires a properties file.') return False else: db = DBConnect() db.connect() db.register_gui_parent(self.frame) except Exception as e: p._initialized = False # Fully raising the exception during this startup sequence will crash CPA. # So we'll display it manually. show_exception_as_dialog(type(e), e, e.__traceback__, raisefurther=False) logging.critical(e) try: from cpa.updatechecker import check_update check_update(self.frame, event=False) except: logging.warn("CPA was unable to check for updates.") return True
def on_save_workspace(self, evt): p = Properties.getInstance() dlg = wx.FileDialog(self, message="Save workspace as...", defaultDir=os.getcwd(), defaultFile='%s_%s.workspace'%(os.path.splitext(os.path.split(p._filename)[1])[0], p.image_table), style=wx.SAVE|wx.FD_OVERWRITE_PROMPT|wx.FD_CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: wx.GetApp().save_workspace(dlg.GetPath())
def OnInit(self): """Initialize CPA """ """List of tables created by the user during this session""" self.user_tables = [] # splashscreen splashimage = cpa.icons.cpa_splash.ConvertToBitmap() # If the splash image has alpha, it shows up transparently on # windows, so we blend it into a white background. splashbitmap = wx.EmptyBitmapRGBA(splashimage.GetWidth(), splashimage.GetHeight(), 255, 255, 255, 255) dc = wx.MemoryDC() dc.SelectObject(splashbitmap) dc.DrawBitmap(splashimage, 0, 0) dc.Destroy() # necessary to avoid a crash in splashscreen splash = wx.SplashScreen(splashbitmap, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1) self.splash = splash p = Properties.getInstance() if not p.is_initialized(): from cpa.guiutils import show_load_dialog splash.Destroy() if not show_load_dialog(): logging.error("CellProfiler Analyst requires a properties file. Exiting.") return False self.frame = MainGUI(p, None, size=(860, -1)) self.frame.Show(True) db = cpa.dbconnect.DBConnect.getInstance() # Black magic: Bus errors occur on Mac OS X if we wait until # the JVM or the wx event look has started to connect. But it # has to be done after we have read the properties file. So we # do it here. db.connect() db.register_gui_parent(self.frame) # The JVM has to be started after db.connect(), otherwise bus errors # occur on Mac OS X. javabridge.start_vm(class_path=bioformats.JARS, run_headless=False) javabridge.attach() javabridge.activate_awt() try: if __version__ != -1: import cellprofiler.utilities.check_for_updates as cfu cfu.check_for_updates( "http://cellprofiler.org/CPAupdate.html", max(__version__, cpaprefs.get_skip_version()), new_version_cb, user_agent="CPAnalyst/2.0.%d" % (__version__), ) except ImportError: logging.warn( "CPA was unable to check for updates. Could not import cellprofiler.utilities.check_for_updates." ) return True
def on_save_properties(self, evt): p = Properties.getInstance() dirname, filename = os.path.split(p._filename) ext = os.path.splitext(p._filename)[-1] dlg = wx.FileDialog(self, message="Save properties as...", defaultDir=dirname, defaultFile=filename, wildcard=ext, style=wx.SAVE|wx.FD_OVERWRITE_PROMPT|wx.FD_CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: p.save_file(dlg.GetPath())
def on_load_properties(self, evt): # Show confirmation dialog. if self.properties.is_initialized(): dlg = wx.MessageDialog( None, "Loading a new file will close all windows and clear unsaved data. Proceed?", 'Load properties file', wx.OK | wx.CANCEL) response = dlg.ShowModal() if response != wx.ID_OK: return # Close all subwindows for window in self.GetChildren(): if isinstance(window, wx.Frame): window.Destroy() # Shut down existing connections and wipe properties. db = DBConnect() db.Disconnect() p = Properties() p.clear() self.console.Clear() if not p.is_initialized(): from cpa.guiutils import show_load_dialog if not show_load_dialog(): example_link_address = 'cellprofileranalyst.org' dlg = wx.MessageDialog( None, 'CellProfiler Analyst requires a properties file. Download an example at %s' % (example_link_address), 'Properties file required', wx.OK) response = dlg.ShowModal() logging.error( 'CellProfiler Analyst requires a properties file.') else: db.connect() db.register_gui_parent(self)
def clear_link_tables(self, evt=None): p = Properties.getInstance() dlg = wx.MessageDialog(self, 'This will delete the tables ' '"%s" and "%s" from your database. ' 'CPA will automatically recreate these tables as it ' 'discovers how your database is linked. Are you sure you ' 'want to proceed?' %(p.link_tables_table, p.link_columns_table), 'Clear table linking information?', wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION) response = dlg.ShowModal() if response != wx.ID_YES: return db = dbconnect.DBConnect.getInstance() db.execute('DROP TABLE IF EXISTS %s'%(p.link_tables_table)) db.execute('DROP TABLE IF EXISTS %s'%(p.link_columns_table)) db.Commit()
def start(): # Init p = Properties.getInstance() p.LoadFile("/vagrant/data/23_classes/az-dnaonly.properties") # p.LoadFile('/vagrant/data/5_classes/2010_08_21_Malaria_MartiLab_Test_2011_05_27_DIC+Alexa.properties') # p.LoadFile('/vagrant/data/cpa_example/example.properties') db = dbconnect.DBConnect.getInstance() dm = DataModel.getInstance() classifier = Classifier(properties=p) # Create a classifier with p classifier.LoadTrainingSet("/vagrant/data/23_classes/Anne_DNA_66.txt") # classifier.LoadTrainingSet('/vagrant/data/5_classes/MyTrainingSet_AllStages_Half.txt') # classifier.LoadTrainingSet('/vagrant/data/cpa_example/MyTrainingSet.txt') settings["p"] = p settings["db"] = db settings["dm"] = dm settings["classifier"] = classifier
from cpa.properties import Properties from cpa.datamodel import DataModel from cpa.scoreall import score import base64 import zlib import os if __name__ == "__main__": from cpa.trainingset import TrainingSet import wx app = wx.PySimpleApp() os.chdir('/Users/afraser/') p = Properties() db = DBConnect() dm = DataModel() testdata = [ # Test 2 classes filtered by MAPs { 'props': 'CPAnalyst_test_data/nirht_test.properties', 'ts': 'CPAnalyst_test_data/nirht_2class_test.txt', 'nRules': 5, 'filter': 'MAPs', 'group':
def Start(self): '''Initialize CPA ''' '''List of tables created by the user during this session''' self.user_tables = [] # splashscreen splashimage = cpa.icons.cpa_splash.ConvertToBitmap() # If the splash image has alpha, it shows up transparently on # windows, so we blend it into a white background. splashbitmap = wx.EmptyBitmapRGBA(splashimage.GetWidth(), splashimage.GetHeight(), 255, 255, 255, 255) dc = wx.MemoryDC() dc.SelectObject(splashbitmap) dc.DrawBitmap(splashimage, 0, 0) dc.Destroy() # necessary to avoid a crash in splashscreen splash = wx.SplashScreen(splashbitmap, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1) self.splash = splash p = Properties.getInstance() if not p.is_initialized(): from cpa.guiutils import show_load_dialog splash.Destroy() if not show_load_dialog(): splash.Destroy() example_link_address = 'cellprofiler.org' dlg = wx.MessageDialog(None, 'CellProfiler Analyst requires a properties file. Download an example at %s' % ( example_link_address), 'Properties file required', wx.OK) response = dlg.ShowModal() logging.error('CellProfiler Analyst requires a properties file. Exiting.') return False self.frame = MainGUI(p, None, size=(1000,-1)) # def show_frame(): # import time # res = self.frame.Show() # wx.CallAfter(show_frame) db = cpa.dbconnect.DBConnect.getInstance() # Black magic: Bus errors occur on Mac OS X if we wait until # the JVM or the wx event look has started to connect. But it # has to be done after we have read the properties file. So we # do it here. db.connect() db.register_gui_parent(self.frame) # The JVM has to be started after db.connect(), otherwise bus errors # occur on Mac OS X. javabridge.start_vm(class_path=bioformats.JARS, run_headless=True) # removes the log4j warnings from bioformats import log4j log4j.basic_config() javabridge.attach() # javabridge.activate_awt() try: if __version__ != -1: import cpa.util.check_for_updates as cfu cfu.check_for_updates('http://cellprofiler.org/updates/CPA.html', max(__version__, cpa.cpaprefs.get_skip_version()), new_version_cb, user_agent='CPAnalyst/%s'%(__version__)) except ImportError: logging.warn("CPA was unable to check for updates. Could not import cpa.util.check_for_updates.") self.frame.Show() # Show frame return True
def setup_sqlite2(self): self.p = Properties() self.db = DBConnect() self.db.Disconnect() self.p.LoadFile( '../../CPAnalyst_test_data/export_to_db_test.properties')
# send everything to logfile sys.stderr = Stderr() sys.stdout = sys.stderr if hasattr(sys, 'frozen') and sys.platform.startswith('win'): # on windows, log to a file (Mac goes to console) setup_frozen_logging() logging.basicConfig(level=logging.DEBUG) # Handles args to MacOS "Apps" if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'): del sys.argv[1] if len(sys.argv) > 1: # Load a properties file if passed in args p = Properties.getInstance() if sys.argv[1] == '--incell': # GE Incell xml wrapper # LOOP p.LoadIncellFiles(sys.argv[2], sys.argv[3], sys.argv[4:]) else: p.LoadFile(sys.argv[1]) import threading from cpa.classifier import Classifier from cpa.tableviewer import TableViewer from cpa.plateviewer import PlateViewer from cpa.imageviewer import ImageViewer from cpa.boxplot import BoxPlot from cpa.scatter import Scatter from cpa.histogram import Histogram
class TestDBConnect(unittest.TestCase): def setup_mysql(self): self.p = Properties() self.db = DBConnect() self.db.Disconnect() self.p.LoadFile('../../CPAnalyst_test_data/nirht_test.properties') def setup_sqlite(self): self.p = Properties() self.db = DBConnect() self.db.Disconnect() self.p.LoadFile('../../CPAnalyst_test_data/nirht_local.properties') def setup_sqlite2(self): self.p = Properties() self.db = DBConnect() self.db.Disconnect() self.p.LoadFile( '../../CPAnalyst_test_data/export_to_db_test.properties') # # Test module-level functions # def test_clean_up_colnames(self): self.setup_mysql() def test_well_key_columns(self): self.setup_mysql() assert well_key_columns() == ('plate', 'well') self.setup_sqlite() assert well_key_columns() == tuple() def test_image_key_columns(self): self.setup_mysql() assert image_key_columns() == ('ImageNumber', ) self.setup_sqlite() assert image_key_columns() == ('TableNumber', 'ImageNumber') def test_object_key_columns(self): self.setup_mysql() assert object_key_columns() == ('ImageNumber', 'ObjectNumber') self.setup_sqlite() assert object_key_columns() == ('TableNumber', 'ImageNumber', 'ObjectNumber') def test_GetWhereClauseForObjects(self): self.setup_mysql() assert GetWhereClauseForObjects([ (1, 1) ]) == '(ImageNumber=1 AND ObjectNumber=1)' assert GetWhereClauseForObjects( [(1, 1), (2, 1)] ) == '(ImageNumber=1 AND ObjectNumber=1 OR ImageNumber=2 AND ObjectNumber=1)' self.setup_sqlite() assert GetWhereClauseForObjects( [(0, 1, 1), (0, 2, 1)] ) == '(TableNumber=0 AND ImageNumber=1 AND ObjectNumber=1 OR TableNumber=0 AND ImageNumber=2 AND ObjectNumber=1)' def test_GetWhereClauseForImages(self): self.setup_mysql() assert GetWhereClauseForImages([(1, )]) == 'ImageNumber IN (1)' assert GetWhereClauseForImages([(1, ), (2, )]) == 'ImageNumber IN (1,2)' self.setup_sqlite() assert GetWhereClauseForImages([ (0, 1), (0, 2) ]) == '(TableNumber=0 AND ImageNumber IN (1,2))' def test_UniqueObjectClause(self): self.setup_mysql() assert UniqueObjectClause() == 'ImageNumber,ObjectNumber' self.setup_sqlite() assert UniqueObjectClause() == 'TableNumber,ImageNumber,ObjectNumber' def test_UniqueImageClause(self): self.setup_mysql() assert UniqueImageClause() == 'ImageNumber' self.setup_sqlite() assert UniqueImageClause() == 'TableNumber,ImageNumber' # # Test class functions # def test_Connect_Disconnect(self): self.setup_mysql() self.db.connect() assert len(self.db.connections) == 1 assert len(self.db.cursors) == 1 assert len(self.db.connectionInfo) == 1 self.db.connect() assert len(self.db.connections) == 1 assert len(self.db.cursors) == 1 assert len(self.db.connectionInfo) == 1 self.db.Disconnect() assert len(self.db.connections) == 0 assert len(self.db.cursors) == 0 assert len(self.db.connectionInfo) == 0 self.setup_sqlite() assert len(self.db.connections) == 0 assert len(self.db.cursors) == 0 assert len(self.db.connectionInfo) == 0 self.db.GetAllImageKeys() assert len(self.db.connections) == 1 assert len(self.db.cursors) == 1 assert len(self.db.connectionInfo) == 1 self.db.GetAllImageKeys() assert len(self.db.connections) == 1 assert len(self.db.cursors) == 1 assert len(self.db.connectionInfo) == 1 self.db.Disconnect() assert len(self.db.connections) == 0 assert len(self.db.cursors) == 0 assert len(self.db.connectionInfo) == 0 def test_Commit(self): self.setup_mysql() self.db.connect() self.db.execute('DROP TABLE IF EXISTS temp_test') self.db.execute('CREATE TABLE temp_test (id int(11) default NULL)') self.db.execute('INSERT INTO temp_test values(1)') self.db.Commit() self.db.Disconnect() self.db.connect() res = self.db.execute('SELECT id FROM temp_test WHERE id=1') assert res == [(1, )] self.db.execute('DROP TABLE temp_test') def test_execute(self): self.setup_mysql() self.db.execute('SELECT %s FROM %s' % (self.p.image_id, self.p.image_table)) self.setup_sqlite() self.db.execute('SELECT %s FROM %s' % (self.p.image_id, self.p.image_table)) def test_GetObjectIDAtIndex(self): self.setup_mysql() obKey = self.db.GetObjectIDAtIndex(imKey=(1, ), index=94) assert obKey == (1, 94) self.setup_sqlite() obKey = self.db.GetObjectIDAtIndex(imKey=(0, 1), index=94) assert obKey == (0, 1, 94) def test_GetPerImageObjectCounts(self): self.setup_mysql() self.db.GetPerImageObjectCounts() self.setup_sqlite() self.db.GetPerImageObjectCounts() def test_GetObjectCoords(self): self.setup_mysql() xy = self.db.GetObjectCoords((1, 1)) assert xy == (11.4818, 305.06400000000002) self.setup_sqlite() xy = self.db.GetObjectCoords((0, 1, 1)) assert xy == (11.4818, 305.06400000000002) def test_GetObjectNear(self): self.setup_mysql() obKey = self.db.GetObjectNear((1, ), 11, 300) assert obKey == (1, 1) self.setup_sqlite() obKey = self.db.GetObjectNear((0, 1), 11, 300) assert obKey == (0, 1, 1) def test_GetFullChannelPathsForImage(self): self.setup_mysql() paths = self.db.GetFullChannelPathsForImage((1, )) assert paths == [ '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d2.DIB', '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d1.DIB', '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d0.DIB' ] self.setup_sqlite() paths = self.db.GetFullChannelPathsForImage((0, 1)) assert paths == [ '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d2.DIB', '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d1.DIB', '2006_02_15_NIRHT/trcHT29Images/NIRHTa+001/AS_09125_050116000001_A01f00d0.DIB' ] def test_GetGroupMaps(self): self.setup_mysql() groupMaps, colNames = self.db.GetGroupMaps() assert groupMaps['Gene'][(1, )] == ('Gabra3', ) assert groupMaps['Well'][(1, )] == (1, ) assert groupMaps['Well+Gene'][(1, )] == (1, 'Gabra3') assert colNames == { 'Gene': ['gene'], 'Well': ['well'], 'Well+Gene': ['well', 'gene'] } self.setup_sqlite() groupMaps, colNames = self.db.GetGroupMaps() assert groupMaps['96x4'][(0, 1)] == (0, 1) assert colNames == {'96x4': ['T2', 'I2']} def test_GetFilteredImages(self): self.setup_mysql() test = set(self.db.GetFilteredImages('MAPs')) print(test) vals = set([(239, ), (21, ), (32, ), (197, ), (86, ), (23, ), (61, ), (72, ), (213, ), (222, ), (63, ), (229, ), (221, ), (38, ), (224, ), (231, ), (13, ), (24, ), (78, ), (214, ), (15, ), (223, ), (53, ), (64, ), (246, ), (55, ), (93, ), (232, ), (30, ), (206, ), (95, ), (215, ), (5, ), (16, ), (70, ), (7, ), (45, ), (56, ), (238, ), (198, ), (47, ), (207, ), (85, ), (96, ), (22, ), (87, ), (253, ), (8, ), (62, ), (254, ), (255, ), (199, ), (37, ), (48, ), (205, ), (230, ), (208, ), (39, ), (77, ), (88, ), (14, ), (79, ), (245, ), (256, ), (54, ), (247, ), (29, ), (40, ), (94, ), (31, ), (240, ), (69, ), (80, ), (6, ), (216, ), (71, ), (237, ), (248, ), (200, ), (46, )]) assert test == vals assert self.db.GetFilteredImages('IMPOSSIBLE') == [] self.setup_sqlite() assert self.db.GetFilteredImages('FirstTen') == [ (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10) ] assert self.db.GetFilteredImages('IMPOSSIBLE') == [] def test_GetColumnNames(self): self.setup_mysql() cols = self.db.GetColumnNames(self.p.object_table) assert cols[:19] == [ 'ImageNumber', 'ObjectNumber', 'Nuclei_Location_CenterX', 'Nuclei_Location_CenterY', 'Nuclei_Children_Cells_Count', 'Nuclei_Correlation_Correlation_DNA_and_pH3', 'Nuclei_Correlation_Correlation_DNA_and_Actin', 'Nuclei_Correlation_Correlation_pH3_and_Actin', 'Nuclei_AreaShape_Area', 'Nuclei_AreaShape_Eccentricity', 'Nuclei_AreaShape_Solidity', 'Nuclei_AreaShape_Extent', 'Nuclei_AreaShape_Euler_number', 'Nuclei_AreaShape_Perimeter', 'Nuclei_AreaShape_Form_factor', 'Nuclei_AreaShape_MajorAxisLength', 'Nuclei_AreaShape_MinorAxisLength', 'Nuclei_AreaShape_Orientation', 'Nuclei_AreaShape_Zernike0_0' ] assert cols[-20:] == [ 'AreaNormalized_Cytoplasm_AreaShape_Zernike5_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike5_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_0', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_2', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_4', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_6', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_1', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_7', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_0', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_2', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_4', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_6', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_8', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_1', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_7', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_9' ] self.setup_sqlite() cols = self.db.GetColumnNames(self.p.object_table) assert cols[:20] == [ 'TableNumber', 'ImageNumber', 'ObjectNumber', 'Nuclei_Location_CenterX', 'Nuclei_Location_CenterY', 'Nuclei_Children_Cells_Count', 'Nuclei_Correlation_Correlation_DNA_and_pH3', 'Nuclei_Correlation_Correlation_DNA_and_Actin', 'Nuclei_Correlation_Correlation_pH3_and_Actin', 'Nuclei_AreaShape_Area', 'Nuclei_AreaShape_Eccentricity', 'Nuclei_AreaShape_Solidity', 'Nuclei_AreaShape_Extent', 'Nuclei_AreaShape_Euler_number', 'Nuclei_AreaShape_Perimeter', 'Nuclei_AreaShape_Form_factor', 'Nuclei_AreaShape_MajorAxisLength', 'Nuclei_AreaShape_MinorAxisLength', 'Nuclei_AreaShape_Orientation', 'Nuclei_AreaShape_Zernike0_0' ] assert cols[-20:] == [ 'AreaNormalized_Cytoplasm_AreaShape_Zernike5_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike5_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_0', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_2', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_4', 'AreaNormalized_Cytoplasm_AreaShape_Zernike6_6', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_1', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike7_7', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_0', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_2', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_4', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_6', 'AreaNormalized_Cytoplasm_AreaShape_Zernike8_8', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_1', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_3', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_5', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_7', 'AreaNormalized_Cytoplasm_AreaShape_Zernike9_9' ] def test_GetColumnTypes(self): self.setup_mysql() cols = self.db.GetColumnTypes(self.p.object_table) assert cols[:19] == [ int, int, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float ] assert cols[-20:] == [ float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float ] cols = self.db.GetColumnTypes(self.p.image_table) assert cols[:20] == [ int, int, int, str, int, str, str, str, str, str, str, float, float, float, float, float, float, float, float, float ] def test_GetColnamesForClassifier(self): self.setup_mysql() cols = self.db.GetColnamesForClassifier() for c in [ 'ImageNumber', 'ObjectNumber', 'Nuclei_Location_CenterX', 'Nuclei_Location_CenterY' ]: assert c not in cols self.setup_sqlite() cols = self.db.GetColnamesForClassifier() for c in [ 'TableNumber', 'ImageNumber', 'ObjectNumber', 'Nuclei_Location_CenterX', 'Nuclei_Location_CenterY' ]: assert c not in cols def test_ReadExportToDB(self): '''Test reading data from Export to Database.''' self.setup_sqlite2() vals = [(1, ), (2, ), (3, ), (4, ), (5, ), (6, ), (7, ), (8, ), (9, ), (10, ), (11, ), (12, ), (13, ), (14, ), (15, ), (16, ), (17, ), (18, ), (19, ), (20, )] groups = { 'Plate+Well': { ('Week1_22123', 'B05'): [(13, ), (14, ), (15, ), (16, )], ('Week1_22123', 'B02'): [(1, ), (2, ), (3, ), (4, )], ('Week1_22123', 'B04'): [(9, ), (10, ), (11, ), (12, )], ('Week1_22123', 'B06'): [(17, ), (18, ), (19, ), (20, )], ('Week1_22123', 'B03'): [(5, ), (6, ), (7, ), (8, )] } }, { 'Plate+Well': ['Image_Metadata_Plate_DAPI', 'Image_Metadata_Well_DAPI'] } assert len(self.db.GetAllImageKeys()) == 20 assert self.db.GetAllImageKeys() == vals assert self.db.GetGroupMaps(True) == groups def test_CreateMySQLTempTableFromData(self): self.setup_mysql() data = [ ['A01', 1, 1.], ['A02', 1, 2.], ['A03', 1, -np.inf], ['A04', 1, np.inf], ['A04', 1, np.nan], ['A04', 1, 100], ['A04', 1, 200], ] colnames = ['well', 'plate', 'vals'] self.db.CreateTableFromData(data, colnames, '__test_table', temporary=True) res = self.db.execute('select * from __test_table') assert res == [('A01', 1, 1.0), ('A02', 1, 2.0), ('A03', 1, None), ('A04', 1, None), ('A04', 1, None), ('A04', 1, 100.0), ('A04', 1, 200.0)] def test_CreateSQLiteTempTableFromData(self): self.setup_sqlite() data = [ ['A01', 1, 1.], ['A02', 1, 2.], ['A03', 1, -np.inf], ['A04', 1, np.inf], ['A04', 1, np.nan], ['A04', 1, 100], ['A04', 1, 200], ] colnames = ['well', 'plate', 'vals'] self.db.CreateTableFromData(data, colnames, '__test_table', temporary=True) res = self.db.execute('select * from __test_table') assert res == [('A01', 1, 1.0), ('A02', 1, 2.0), ('A03', 1, None), ('A04', 1, None), ('A04', 1, None), ('A04', 1, 100.0), ('A04', 1, 200.0)]
import unittest from cpa.imagereader import ImageReader from cpa.properties import Properties p = Properties() # fake-up some props p._filename = '../../CPAnalyst_test_data/test_images/' p.image_channel_colors = ['red', 'green', 'blue', 'none', 'none', 'none'] p.object_name = ['cell', 'cells'] p.image_names = ['', '', ''] p.image_id = 'ImageNumber' ir = ImageReader() class TestImageReader(unittest.TestCase): def test_tif1(self): # TIF RGB, 8-bit, PackBits encoding fds = ['color.tif'] images = ir.ReadImages(fds) assert len(images) == 3 for im in images: assert 0. <= im.min() <= im.max() <= 1. assert im.shape == (512, 512) def test_tif2(self): # 2 RGB TIFS fds = ['color.tif', 'color.tif'] images = ir.ReadImages(fds) assert len(images) == 6
from cpa.trainingset import TrainingSet from io import StringIO import cpa.fastgentleboostingmulticlass import cpa.imagetools import cpa.multiclasssql import cpa.polyafit import numpy import os import wx from cpa.classifier import * import time if __name__ == "__main__": app = wx.PySimpleApp() p = Properties() db = dbconnect.DBConnect() dm = DataModel() # props = '/Volumes/imaging_analysis/2007_10_19_Gilliland_LeukemiaScreens/Screen3_1Apr09_run3/2007_10_19_Gilliland_LeukemiaScreens_Validation_v2_AllBatches_DuplicatesFiltered_FullBarcode_testSinglePlate.properties' # ts = '/Volumes/imaging_analysis/2007_10_19_Gilliland_LeukemiaScreens/Screen3_1Apr09_run3/trainingvalidation3b.txt' props = '../Properties/nirht_area_test.properties' ts = '/Users/afraser/Desktop/MyTrainingSet.txt' p.LoadFile(props) classifier = Classifier(p) classifier.Show(True) classifier.LoadTrainingSet(ts) time.sleep(3) classifier.TrainClassifier() classifier.ScoreAll()
def Start(self): '''Initialize CPA ''' '''List of tables created by the user during this session''' self.user_tables = [] # splashscreen splashimage = cpa.icons.cpa_splash.ConvertToBitmap() # If the splash image has alpha, it shows up transparently on # windows, so we blend it into a white background. splashbitmap = wx.EmptyBitmapRGBA(splashimage.GetWidth(), splashimage.GetHeight(), 255, 255, 255, 255) dc = wx.MemoryDC() dc.SelectObject(splashbitmap) dc.DrawBitmap(splashimage, 0, 0) dc.Destroy() # necessary to avoid a crash in splashscreen splash = wx.SplashScreen(splashbitmap, wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT, 2000, None, -1) self.splash = splash p = Properties.getInstance() if not p.is_initialized(): from cpa.guiutils import show_load_dialog splash.Destroy() if not show_load_dialog(): splash.Destroy() example_link_address = 'cellprofiler.org' dlg = wx.MessageDialog(None, 'CellProfiler Analyst requires a properties file. Download an example at %s' % ( example_link_address), 'Properties file required', wx.OK) response = dlg.ShowModal() logging.error('CellProfiler Analyst requires a properties file. Exiting.') return False self.frame = MainGUI(p, None, size=(1000,-1)) db = DBConnect.getInstance() # Black magic: Bus errors occur on Mac OS X if we wait until # the JVM or the wx event look has started to connect. But it # has to be done after we have read the properties file. So we # do it here. db.connect() db.register_gui_parent(self.frame) # The JVM has to be started after db.connect(), otherwise bus errors # occur on Mac OS X. javabridge.start_vm(class_path=bioformats.JARS, run_headless=True) # removes the log4j warnings javabridge.attach() # TODO: check for updates try: if __version__ != -1: import cpa.util.check_for_updates as cfu cfu.check_for_updates('http://cellprofiler.org/updates/CPA.html', max(__version__, cpa.cpaprefs.get_skip_version()), new_version_cb, user_agent='CPAnalyst/%s'%(__version__)) except ImportError: logging.warn("CPA was unable to check for updates. Could not import cpa.util.check_for_updates.") self.frame.Show() # Show frame return True
def setup_mysql(self): self.p = Properties() self.db = DBConnect() self.db.Disconnect() self.p.LoadFile('../../CPAnalyst_test_data/nirht_test.properties')
# send everything to logfile sys.stderr = Stderr() sys.stdout = sys.stderr if hasattr(sys, 'frozen') and sys.platform.startswith('win'): # on windows, log to a file (Mac goes to console) setup_frozen_logging() logging.basicConfig(level=logging.DEBUG) # Handles args to MacOS "Apps" if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'): del sys.argv[1] if len(sys.argv) > 1: # Load a properties file if passed in args p = Properties.getInstance() if sys.argv[1] == '--incell': # GE Incell xml wrapper # LOOP p.LoadIncellFiles(sys.argv[2], sys.argv[3], sys.argv[4:]) else: p.LoadFile(sys.argv[1]) import threading from cpa.classifier import Classifier from cpa.tableviewer import TableViewer from cpa.plateviewer import PlateViewer from cpa.imageviewer import ImageViewer from cpa.imagegallery import ImageGallery from cpa.boxplot import BoxPlot from cpa.scatter import Scatter
logging.Handler.__init__(self) self.update = update def emit(self, record): self.update(self.format(record)) logging.basicConfig(level=logging.DEBUG) # Handles args to MacOS "Apps" if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'): del sys.argv[1] if len(sys.argv) > 1: # Load a properties file if passed in args p = Properties() if sys.argv[1] == '--incell': # GE Incell xml wrapper # LOOP p.LoadIncellFiles(sys.argv[2], sys.argv[3], sys.argv[4:]) else: p.LoadFile(sys.argv[1]) from cpa.cpatool import CPATool import inspect import cpa.multiclasssql # --- import wx ID_CLASSIFIER = wx.NewIdRef()