def test_06_get_settings_list(self): module1 = instantiate_module("IdentifyPrimaryObjects") module1.module_num = 1 module2 = instantiate_module("Smooth") module2.module_num = 2 module3 = instantiate_module("Resize") module3.module_num = 3 module4 = self.make_instance() module4.module_num = 4 module4.parameters[0].module_names.value = "IdentifyPrimaryObjects #1" module4.add_parameter() module4.parameters[1].module_names.value = "Smooth #2" module4.add_parameter() module4.parameters[2].module_names.value = "Resize #3" module4.add_parameter() module4.parameters[3].module_names.value = "BayesianOptimisation #4" pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module1) pipeline.add_module(module2) pipeline.add_module(module3) pipeline.add_module(module4) settings_list = module4.get_settings_from_modules(pipeline) self.assertTrue("Use advanced settings?" in settings_list) self.assertTrue("Select smoothing method" in settings_list) self.assertTrue("Resizing factor" in settings_list) self.assertTrue("No. of settings to be adjusted" in settings_list)
def test_01_04_instantiate_all(self): '''Instantiate each module and make sure none import wx''' import cellprofiler.modules as M for name in M.get_module_names(): try: M.instantiate_module(name) except: print "Module %s probably imports wx" % name traceback.print_exc()
def print_code_statistics(): '''Print # lines of code, # modules, etc to console This is the official source of code statistics for things like grants. ''' from cellprofiler.modules import builtin_modules, all_modules, instantiate_module import subprocess print "\n\n\n**** CellProfiler code statistics ****" print "# of built-in modules: %d" % len(builtin_modules) setting_count = 0 for module in all_modules.values(): if module.__module__.find(".") < 0: continue mn = module.__module__.rsplit(".", 1)[1] if mn not in builtin_modules: continue module_instance = instantiate_module(module.module_name) setting_count += len(module_instance.help_settings()) print "# of settings: %d" % setting_count filelist = subprocess.Popen(["git", "ls-files"], stdout=subprocess.PIPE).communicate()[0].split("\n") linecount = 0 for filename in filelist: if (os.path.exists(filename) and any([filename.endswith(x) for x in ".py", ".c", ".pyx", ".java"])): with open(filename, "r") as fd: linecount += len(fd.readlines())
def output_module_html(webpage_path): '''Output an HTML page for each module''' icons_relpath = relpath(cellprofiler.icons.__path__[0]) all_png_icons = glob(os.path.join(icons_relpath, "*.png")) icon_names = [os.path.basename(f)[:-4] for f in all_png_icons] help_text = """ <h2>Help for CellProfiler Modules</a></h2> <ul>\n""" d = {} module_path = webpage_path if not (os.path.exists(module_path) and os.path.isdir(module_path)): try: os.mkdir(module_path) except IOError: raise ValueError("Could not create directory %s" % module_path) for module_name in sorted(get_module_names()): module = instantiate_module(module_name) location = os.path.split( module.create_settings.im_func.func_code.co_filename)[0] if location == cpprefs.get_plugin_directory(): continue if isinstance(module.category, (str,unicode)): module.category = [module.category] for category in module.category: if not d.has_key(category): d[category] = {} d[category][module_name] = module result = module.get_help() if result is None: continue result = result.replace('<body><h1>','<body><h1>Module: ') # Replace refs to icons in memory with the relative path to the image dir (see above) result = re.sub("memory:",os.path.join("images","").encode('string-escape'),result) # Check if a corresponding image exists for the module if module_name in icon_names: # Strip out end html tags so I can add more stuff result = result.replace('</body>','').replace('</html>','') # Include images specific to the module, relative to html files ('images' dir) LOCATION_MODULE_IMAGES = os.path.join('images','%s.png'%(module_name)) result += '\n\n<div><p><img src="%s", width="50%%"></p></div>\n'%LOCATION_MODULE_IMAGES # Now end the help text result += '</body></html>' fd = open(os.path.join(module_path,"%s.html" % module_name), "w") fd.write(result) fd.close() for category in sorted(d.keys()): sub_d = d[category] help_text += "<li><b>%s</b><br><ul>\n"%category for module_name in sorted(sub_d.keys()): help_text += "<li><a href='%s.html'>%s</a></li>\n" % (module_name, module_name) help_text += "</ul></li>\n" help_text += "</ul>\n" return help_text
def print_code_statistics(): '''Print # lines of code, # modules, etc to console This is the official source of code statistics for things like grants. ''' from cellprofiler.modules import builtin_modules, all_modules, instantiate_module import subprocess print "\n\n\n**** CellProfiler code statistics ****" print "# of built-in modules: %d" % len(builtin_modules) setting_count = 0 for module in all_modules.values(): if module.__module__.find(".") < 0: continue mn = module.__module__.rsplit(".", 1)[1] if mn not in builtin_modules: continue module_instance = instantiate_module(module.module_name) setting_count += len(module_instance.help_settings()) print "# of settings: %d" % setting_count filelist = subprocess.Popen( ["git", "ls-files"], stdout=subprocess.PIPE).communicate()[0].split("\n") linecount = 0 for filename in filelist: if (os.path.exists(filename) and any( [filename.endswith(x) for x in ".py", ".c", ".pyx", ".java"])): with open(filename, "r") as fd: linecount += len(fd.readlines())
def print_code_statistics(): """Print # lines of code, # modules, etc to console This is the official source of code statistics for things like grants. """ from cellprofiler.modules import builtin_modules, all_modules, instantiate_module import subprocess print "\n\n\n**** CellProfiler code statistics ****" print "# of built-in modules: %d" % len(builtin_modules) setting_count = 0 for module in all_modules.values(): if module.__module__.find(".") < 0: continue mn = module.__module__.rsplit(".", 1)[1] if mn not in builtin_modules: continue module_instance = instantiate_module(module.module_name) setting_count += len(module_instance.help_settings()) directory = os.path.abspath(os.path.split(sys.argv[0])[0]) try: filelist = ( subprocess.Popen(["git", "ls-files"], stdout=subprocess.PIPE, cwd=directory).communicate()[0].split("\n") ) except: filelist = [] for root, dirs, files in os.walk(directory): filelist += [os.path.join(root, f) for f in files] linecount = 0 for filename in filelist: if os.path.exists(filename) and any([filename.endswith(x) for x in ".py", ".c", ".pyx", ".java"]):
def setUpClass(self): self.module = instantiate_module(MODULE_NAME) self.module.input_image_name.value = INPUT_IMAGE_NAME self.module.output_image_name.value = OUTPUT_IMAGE_NAME self.module.module_num = 1 self.pipeline = cpp.Pipeline() self.pipeline.add_module(self.module) self.package = __import__(self.module.__class__.__module__)
def test_02_01_maybe_you_implemented_get_categories(self): module = instantiate_module(MODULE_NAME) module.output_objects_name.value = OUTPUT_OBJECTS_NAME if module.get_categories.im_func is not cpm.CPModule.get_categories.im_func: c_image = module.get_categories(None, cpmeas.IMAGE) self.assertTrue(I.C_COUNT in c_image) c_objects = module.get_categories(None, OUTPUT_OBJECTS_NAME) self.assertTrue(I.C_LOCATION in c_objects) print "+3 for you!"
def test_02_02_maybe_you_implemented_get_measurements(self): module = instantiate_module(MODULE_NAME) module.output_objects_name.value = OUTPUT_OBJECTS_NAME if module.get_measurements.im_func is not cpm.CPModule.get_measurements.im_func: ftr_image = module.get_measurements(None, cpmeas.IMAGE, I.C_COUNT) self.assertTrue(OUTPUT_OBJECTS_NAME in ftr_image) ftr_objects = module.get_measurements(None, OUTPUT_OBJECTS_NAME, I.C_LOCATION) self.assertTrue(I.FTR_CENTER_X in ftr_objects) self.assertTrue(I.FTR_CENTER_Y in ftr_objects) print "+3 for you!"
def setUpClass(cls): cls.tempdir = tempfile.mkdtemp() cls.module = instantiate_module(MODULE_NAME) cls.module.image_name.value = IMAGE_NAME cls.module.folder.set_dir_choice(cps.ABSOLUTE_FOLDER_NAME) cls.module.folder.set_custom_path(cls.tempdir) cls.module.module_num = 1 cls.pipeline = cpp.Pipeline() cls.pipeline.add_module(cls.module) for name, data in ((IMAGE_1_NAME, TIF_1), (IMAGE_2_NAME, TIF_2)): fd = open(os.path.join(cls.tempdir, name), "wb") fd.write(data) fd.close()
def test_05_get_module_list(self): module1 = instantiate_module("IdentifyPrimaryObjects") module1.module_num = 1 module2 = instantiate_module("Smooth") module2.module_num = 2 module3 = instantiate_module("Resize") module3.module_num = 3 module4 = self.make_instance() module4.module_num = 4 pipeline = cellprofiler.pipeline.Pipeline() pipeline.add_module(module1) pipeline.add_module(module2) pipeline.add_module(module3) pipeline.add_module(module4) module_list = module4.get_module_list(pipeline) self.assertEqual(len(module_list), 4) self.assertEqual(module_list[0], "IdentifyPrimaryObjects #1") self.assertEqual(module_list[1], "Smooth #2") self.assertEqual(module_list[2], "Resize #3") self.assertEqual(module_list[3], "BayesianOptimisation #4")
def test_01_02_run(self): module = instantiate_module(MODULE_NAME) module.input_objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() # # Pick a bunch of random points, dilate them using the distance # transform and then label the result. # r = np.random.RandomState() r.seed(12) bimg = np.ones((100, 100), bool) bimg[r.randint(0,100, 50), r.randint(0, 100, 50)] = False labels, count = label(distance_transform_edt(bimg) <= 5) # # Make the input objects # input_objects = cpo.Objects() input_objects.segmented = labels expected = skeletonize_labels(labels) object_set.add_objects(input_objects, INPUT_OBJECTS_NAME) # # Make the workspace # workspace = cpw.Workspace(pipeline, module, None, object_set, cpmeas.Measurements(), None) module.run(workspace) self.assertTrue(OUTPUT_OBJECTS_NAME in object_set.object_names, "Could not find the output objects in the object set") output_objects = object_set.get_objects(OUTPUT_OBJECTS_NAME) np.testing.assert_array_equal(expected, output_objects.segmented)
def run_tteesstt(self, objects, level): module = instantiate_module(MODULE_NAME) module.objects_name.value = OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() object_set.add_objects(objects, OBJECTS_NAME) # # Make the workspace # measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, object_set, measurements, None) module.run(workspace) values = measurements.get_measurement(OBJECTS_NAME, "Example5_MeanDistance") self.assertEqual(len(values), objects.count) for labels, indices in objects.get_labels(): for l in np.unique(labels): # # Test very slowly = 1 object per pass # if l == 0: continue d = scipy.ndimage.distance_transform_edt(labels == l) value = np.sum(d) / np.sum(labels == l) if abs(value - values[l-1]) > .0001: if level == 1: self.fail("You got the wrong answer (label = %d, mine = %f, yours = %f" % l, value, values[l-1]) else: sys.stderr.write("Oh too bad, did not pass level %d\n" % level) return if level > 1: print "+%d for you!" % level
def print_code_statistics(): '''Print # lines of code, # modules, etc to console This is the official source of code statistics for things like grants. ''' from cellprofiler.modules import builtin_modules, all_modules, instantiate_module import subprocess print "\n\n\n**** CellProfiler code statistics ****" print "# of built-in modules: %d" % len(builtin_modules) setting_count = 0 for module in all_modules.values(): if module.__module__.find(".") < 0: continue mn = module.__module__.rsplit(".", 1)[1] if mn not in builtin_modules: continue module_instance = instantiate_module(module.module_name) setting_count += len(module_instance.help_settings()) directory = os.path.abspath(os.path.split(sys.argv[0])[0]) try: filelist = subprocess.Popen(["git", "ls-files"], stdout=subprocess.PIPE, cwd=directory).communicate()[0].split("\n") except: filelist = [] for root, dirs, files in os.walk(directory): filelist += [os.path.join(root, f) for f in files] linecount = 0 for filename in filelist: if (os.path.exists(filename) and any( [filename.endswith(x) for x in ".py", ".c", ".pyx", ".java"])): if filename.endswith(".c") and os.path.exists(filename[:-1] + "pyx"): continue with open(filename, "r") as fd: linecount += len(fd.readlines())
import cellprofiler.cpimage as cpi import cellprofiler.measurements as cpm import cellprofiler.modules.run_imagej as R import cellprofiler.objects as cpo import cellprofiler.pipeline as cpp import cellprofiler.settings as cps import cellprofiler.workspace as cpw import imagej.imagej2 as ij2 INPUT_IMAGE_NAME = "inputimage" OUTPUT_IMAGE_NAME = "outputimage" from cellprofiler.modules import instantiate_module try: instantiate_module("RunImageJ") skip_tests = False except: skip_tests = True @unittest.skipIf(skip_tests, "RunImageJ did not load (headless?)") class TestRunImageJ(unittest.TestCase): def test_01_01_load_v1(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9954 RunImageJ:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Command or macro?:Command
def test_01_01_output_module_html(self): from cellprofiler.modules import get_module_names, instantiate_module M.output_module_html(self.temp_dir) for module_name in sorted(get_module_names()): fd = None try: fd = open(os.path.join(self.temp_dir, module_name + ".html")) except: module = instantiate_module(module_name) location = os.path.split( module.create_settings.im_func.func_code.co_filename)[0] if location == cpprefs.get_plugin_directory(): continue traceback.print_exc() self.assert_("Failed to open %s.html" %module_name) data = fd.read() fd.close() # # Make sure that some nesting rules are obeyed. # tags_we_care_about = ("i","b","ul","ol","li","table","tr","td","th", "h1","h2","h3","html","head", "body") pattern = r"<\s*([a-zA-Z0-9]+).[^>]*>" anti_pattern = r"</\s*([a-zA-Z0-9]+)[^>]*>" d = {} anti_d = {} COUNT = 0 LIST = 1 for tag in tags_we_care_about: for dd in (d, anti_d): dd[tag] = [0, []] for p, dd in ((pattern, d), (anti_pattern, anti_d)): pos = 0 while(True): m = re.search(p, data[pos:]) if m is None: break tag = m.groups()[0].lower() pos = pos + m.start(1)+1 if dd.has_key(tag): dd[tag][COUNT] += 1 dd[tag][LIST].append(pos) # # Check table nesting rules # T_TABLE = 0 T_ANTI_TABLE = 1 T_TR = 2 T_ANTI_TR = 3 T_TH = 4 T_ANTI_TH = 5 T_TD = 6 T_ANTI_TD = 7 T_UL = 8 T_ANTI_UL = 9 T_OL = 10 T_ANTI_OL = 11 T_LI = 12 T_ANTI_LI = 13 T_I = 14 T_ANTI_I = 15 T_B = 16 T_ANTI_B = 17 tokens = [] for tag, token, anti_token in ( ('table', T_TABLE, T_ANTI_TABLE), ('tr', T_TR, T_ANTI_TR), ('td', T_TD, T_ANTI_TD), ('th', T_TH, T_ANTI_TH), ('ul', T_UL, T_ANTI_UL), ('ol', T_OL, T_ANTI_OL), ('li', T_LI, T_ANTI_LI), ('i', T_I, T_ANTI_I), ('b', T_B, T_ANTI_B) ): tokens += [(pos, token) for pos in d[tag][LIST]] tokens += [(pos, anti_token) for pos in anti_d[tag][LIST]] tokens = sorted(tokens) S_INIT = 0 S_AFTER_TABLE = 1 S_AFTER_TR = 2 S_AFTER_TD = 3 S_AFTER_TH = 4 S_AFTER_OL = 5 S_AFTER_UL = 6 S_AFTER_LI = 7 S_AFTER_I = 8 S_AFTER_B = 9 state_transitions = { S_INIT: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_I: S_AFTER_I, T_B: S_AFTER_B }, S_AFTER_TABLE: { T_ANTI_TABLE: S_INIT, T_TR: S_AFTER_TR }, S_AFTER_TR: { T_ANTI_TR: S_INIT, T_TD: S_AFTER_TD, T_TH: S_AFTER_TH }, S_AFTER_TD: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I, T_ANTI_TD: S_INIT }, S_AFTER_TH: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I, T_ANTI_TH: S_INIT }, S_AFTER_OL: { T_LI: S_AFTER_LI, T_ANTI_OL: S_INIT }, S_AFTER_UL: { T_LI: S_AFTER_LI, T_ANTI_UL: S_INIT }, S_AFTER_LI: { T_ANTI_LI: S_INIT, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I }, S_AFTER_I: { T_ANTI_I: S_INIT, T_I: S_AFTER_I, # Stupid but legal <i><i>Foo</i></i> T_B: S_AFTER_B, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL }, S_AFTER_B: { T_ANTI_B: S_INIT, T_B: S_AFTER_B, T_I: S_AFTER_I, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL } } state = [] for pos, token in tokens: self.assertTrue( len(state) >= 0, "Error in %s near position %d (%s)" % (module_name, pos, data[max(0,pos - 30): max(pos + 30, len(data))]) ) top_state, start_pos = (S_INIT,0) if len(state) == 0 else state[-1] self.assertTrue( state_transitions[top_state].has_key(token), "Nesting error in %s near position %d (%s)" % (module_name, pos, data[max(0,pos - 50):pos]+"^^^"+ data[pos:min(pos + 50, len(data))])) next_state = state_transitions[top_state][token] if next_state == S_INIT: state.pop() else: state.append((next_state, pos)) if len(state) > 0: self.assertEqual( len(state), 0, "Couldn't find last closing tag in %s. Last tag position = %d (%s)" % (module_name, state[-1][1], data[(state[-1][1] - 30): (state[-1][1] + 30)])) # # Check begin/end tag counts # for tag in tags_we_care_about: if d.has_key(tag): self.assertTrue(anti_d.has_key(tag), "Missing closing </%s> tag in %s" % (tag, module_name)) self.assertEqual( d[tag][COUNT], anti_d[tag][COUNT], "Found %d <%s>, != %d </%s> in %s" % (d[tag][COUNT], tag, anti_d[tag][COUNT], tag, module_name)) else: self.assertFalse(anti_d.has_key(tag), "Missing opening <%s> tag in %s" % (tag, module_name))
def make_instance(self): '''Return an instance of example1 this way because it's not on classpath''' return instantiate_module("Example1b")
def make_instance(self): return instantiate_module("Example2a")
def test_01_01_instantiate(self): try: instantiate_module(MODULE_NAME) except: self.fail("CellProfiler could not create your module. " "Is it named, " + MODULE_NAME + "?")
def __init__(self, *args, **kwds): '''Instantiate a data tool frame module_name: name of module to instantiate measurements_file_name: name of measurements file ''' assert kwds.has_key("module_name"), "DataToolFrame() needs a module_name argument" assert kwds.has_key("measurements_file_name"), "DataToolFrame() needs a measurements_file_name argument" module_name = kwds["module_name"] measurements_file_name = kwds["measurements_file_name"] kwds_copy = kwds.copy() del kwds_copy["module_name"] del kwds_copy["measurements_file_name"] kwds_copy["title"]="%s data tool"%module_name wx.Frame.__init__(self, *args, **kwds_copy) self.module = instantiate_module(module_name) self.pipeline = cpp.Pipeline() if h5py.is_hdf5(measurements_file_name): self.workspace = cpw.Workspace(self.pipeline, self.module, None, None, None, None) self.workspace.load(measurements_file_name, True) self.measurements = self.workspace.measurements else: self.pipeline.load(measurements_file_name) self.load_measurements(measurements_file_name) self.workspace = cpw.Workspace(self.pipeline, self.module, None, None, self.measurements, None) self.module.module_num = len(self.pipeline.modules())+1 self.pipeline.add_module(self.module) self.sizer = wx.BoxSizer(wx.VERTICAL) module_panel = wx.lib.scrolledpanel.ScrolledPanel(self,-1,style=wx.SUNKEN_BORDER) module_panel.BackgroundColour = cpprefs.get_background_color() self.BackgroundColour = cpprefs.get_background_color() self.module_view = ModuleView(module_panel, self.workspace, True) self.module_view.set_selection(self.module.module_num) def on_change(caller, event): setting = event.get_setting() proposed_value = event.get_proposed_value() setting.value = proposed_value self.pipeline.edit_module(event.get_module().module_num, False) self.module_view.reset_view() self.module_view.add_listener(on_change) # # Add a panel for the "run" button # panel = wx.Panel(self) panel_sizer = wx.BoxSizer(wx.HORIZONTAL) button = wx.Button(panel, label = "Run") self.sizer.Add(module_panel, 1, wx.EXPAND) self.sizer.Add(panel, 0, wx.EXPAND) panel_sizer.AddStretchSpacer() panel_sizer.Add(button, 0, wx.RIGHT, button.Size[1]) panel.SetSizer(panel_sizer) wx.EVT_BUTTON(self, button.Id, self.on_run) # # Add a file menu # file_menu = wx.Menu() file_menu.Append(ID_FILE_LOAD_MEASUREMENTS, "&Load measurements") file_menu.Append(ID_FILE_SAVE_MEASUREMENTS, "&Save measurements") file_menu.Append(ID_FILE_EXIT, "E&xit") self.MenuBar = wx.MenuBar() self.MenuBar.Append(file_menu, "&File") self.Bind(wx.EVT_MENU, self.on_load_measurements, id=ID_FILE_LOAD_MEASUREMENTS) self.Bind(wx.EVT_MENU, self.on_save_measurements, id=ID_FILE_SAVE_MEASUREMENTS) self.Bind(wx.EVT_MENU, self.on_exit, id=ID_FILE_EXIT) accelerators = wx.AcceleratorTable([ (wx.ACCEL_CMD, ord("W"), ID_FILE_EXIT), (wx.ACCEL_CMD, ord("O"), ID_FILE_LOAD_MEASUREMENTS), (wx.ACCEL_CMD, ord("S"), ID_FILE_SAVE_MEASUREMENTS)]) self.SetAcceleratorTable(accelerators) # # Add an image menu # image_menu = wx.Menu() image_menu.Append(ID_IMAGE_CHOOSE, "&Choose") self.MenuBar.Append(image_menu, "&Image") self.Bind(wx.EVT_MENU, self.on_image_choose, id=ID_IMAGE_CHOOSE) self.SetSizer(self.sizer) self.Size = (self.module_view.get_max_width(), self.Size[1]) module_panel.Layout() self.Show() self.tbicon = wx.TaskBarIcon() self.tbicon.SetIcon(get_cp_icon(), "CellProfiler2.0") self.SetIcon(get_cp_icon())
def __init__(self, *args, **kwds): '''Instantiate a data tool frame module_name: name of module to instantiate measurements_file_name: name of measurements file ''' assert kwds.has_key( "module_name"), "DataToolFrame() needs a module_name argument" assert kwds.has_key( "measurements_file_name" ), "DataToolFrame() needs a measurements_file_name argument" module_name = kwds["module_name"] measurements_file_name = kwds["measurements_file_name"] kwds_copy = kwds.copy() del kwds_copy["module_name"] del kwds_copy["measurements_file_name"] kwds_copy["title"] = "%s data tool" % module_name wx.Frame.__init__(self, *args, **kwds_copy) self.module = instantiate_module(module_name) self.module.use_as_data_tool = True self.pipeline = cpp.Pipeline() if h5py.is_hdf5(measurements_file_name): self.workspace = cpw.Workspace(self.pipeline, self.module, None, None, None, None) self.workspace.load(measurements_file_name, True) self.measurements = self.workspace.measurements else: self.pipeline.load(measurements_file_name) self.load_measurements(measurements_file_name) self.workspace = cpw.Workspace(self.pipeline, self.module, None, None, self.measurements, None) self.module.module_num = len(self.pipeline.modules()) + 1 self.pipeline.add_module(self.module) self.sizer = wx.BoxSizer(wx.VERTICAL) module_panel = wx.lib.scrolledpanel.ScrolledPanel( self, -1, style=wx.SUNKEN_BORDER) module_panel.BackgroundColour = cpprefs.get_background_color() self.BackgroundColour = cpprefs.get_background_color() self.module_view = ModuleView(module_panel, self.workspace, True) self.module_view.set_selection(self.module.module_num) def on_change(caller, event): setting = event.get_setting() proposed_value = event.get_proposed_value() setting.value = proposed_value self.pipeline.edit_module(event.get_module().module_num, False) self.module_view.reset_view() self.module_view.request_validation() self.module_view.add_listener(on_change) # # Add a panel for the "run" button # panel = wx.Panel(self) panel_sizer = wx.BoxSizer(wx.HORIZONTAL) button = wx.Button(panel, label="Run") self.sizer.Add(module_panel, 1, wx.EXPAND) self.sizer.Add(panel, 0, wx.EXPAND) panel_sizer.AddStretchSpacer() panel_sizer.Add(button, 0, wx.RIGHT, button.Size[1]) panel.SetSizer(panel_sizer) wx.EVT_BUTTON(self, button.Id, self.on_run) # # Add a file menu # file_menu = wx.Menu() file_menu.Append(ID_FILE_LOAD_MEASUREMENTS, "&Load measurements") file_menu.Append(ID_FILE_SAVE_MEASUREMENTS, "&Save measurements") file_menu.Append(ID_FILE_EXIT, "E&xit") self.MenuBar = wx.MenuBar() self.MenuBar.Append(file_menu, "&File") self.Bind(wx.EVT_MENU, self.on_load_measurements, id=ID_FILE_LOAD_MEASUREMENTS) self.Bind(wx.EVT_MENU, self.on_save_measurements, id=ID_FILE_SAVE_MEASUREMENTS) self.Bind(wx.EVT_MENU, self.on_exit, id=ID_FILE_EXIT) accelerators = wx.AcceleratorTable([ (wx.ACCEL_CMD, ord("W"), ID_FILE_EXIT), (wx.ACCEL_CMD, ord("O"), ID_FILE_LOAD_MEASUREMENTS), (wx.ACCEL_CMD, ord("S"), ID_FILE_SAVE_MEASUREMENTS) ]) self.SetAcceleratorTable(accelerators) # # Add an image menu # image_menu = wx.Menu() image_menu.Append(ID_IMAGE_CHOOSE, "&Choose") self.MenuBar.Append(image_menu, "&Image") self.Bind(wx.EVT_MENU, self.on_image_choose, id=ID_IMAGE_CHOOSE) self.SetSizer(self.sizer) self.Size = (self.module_view.get_max_width(), self.Size[1]) module_panel.Layout() self.Show() self.tbicon = wx.TaskBarIcon() self.tbicon.SetIcon(get_cp_icon(), "CellProfiler2.0") self.SetIcon(get_cp_icon())
def test_00_00_instantiate(self): instantiate_module(MODULE_NAME)
def make_instance(self): '''Return an instance of example1 this way because it's not on classpath''' return instantiate_module("Example1a")
def make_instance(self): '''Return an instance of of the module''' return instantiate_module("BayesianOptimisation")
def test_01_02_run(self): module = instantiate_module(MODULE_NAME) module.input_objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() # # Pick a bunch of random points, dilate them using the distance # transform and then label the result. # r = np.random.RandomState() r.seed(12) bimg = np.ones((100, 100), bool) bimg[r.randint(0,100, 50), r.randint(0, 100, 50)] = False labels, count = label(distance_transform_edt(bimg) <= 5) # # Make the input objects # input_objects = cpo.Objects() input_objects.segmented = labels expected = skeletonize_labels(labels) object_set.add_objects(input_objects, INPUT_OBJECTS_NAME) # # Make the workspace # measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, object_set, measurements, None) module.run(workspace) # # Calculate the centers using Numpy. Scipy can do this too. # But maybe it's instructive to show you how to go at the labels # matrix using Numpy. # # We're going to get the centroids by taking the average value # of x and y per object. # y, x = np.mgrid[0:labels.shape[0], 0:labels.shape[1]].astype(float) # # np.bincount counts the number of occurrences of each integer value. # You need to operate on a 1d array - if you flatten the labels # and weights, their pixels still align. # # We do [1:] to discard the background which is labeled 0 # # The optional second argument to np.bincount is the "weight". For # each label value, maintain a running sum of the weights. # areas = np.bincount(expected.flatten())[1:] total_x = np.bincount(expected.flatten(), weights=x.flatten())[1:] total_y = np.bincount(expected.flatten(), weights=y.flatten())[1:] expected_location_x = total_x / areas expected_location_y = total_y / areas # # Now check against the measurements. # count_feature = I.C_COUNT + "_" + OUTPUT_OBJECTS_NAME self.assertTrue(measurements.has_feature(cpmeas.IMAGE, count_feature), "Your module did not produce a %s measurement" % count_feature) count = measurements.get_measurement(cpmeas.IMAGE, count_feature) self.assertEqual(count, len(areas)) for ftr, expected in ((I.M_LOCATION_CENTER_X, expected_location_x), (I.M_LOCATION_CENTER_Y, expected_location_y)): self.assertTrue(measurements.has_feature( OUTPUT_OBJECTS_NAME, ftr)) location = measurements.get_measurement(OUTPUT_OBJECTS_NAME, ftr) np.testing.assert_almost_equal(location, expected)
def setUp(self): # # Get the module and class using CellProfiler's loader # self.module = instantiate_module("Example1f") self.E = __import__(self.module.__class__.__module__)
def make_instance(self): '''Return an instance of of the module''' return instantiate_module("ManualEvaluation")
def test_01_01_output_module_html(self): from cellprofiler.modules import get_module_names, instantiate_module M.output_module_html(self.temp_dir) for module_name in sorted(get_module_names()): fd = None try: fd = open(os.path.join(self.temp_dir, module_name + ".html")) except: module = instantiate_module(module_name) location = os.path.split( module.create_settings.im_func.func_code.co_filename)[0] if location == cpprefs.get_plugin_directory(): continue traceback.print_exc() self.assert_("Failed to open %s.html" % module_name) data = fd.read() fd.close() # # Make sure that some nesting rules are obeyed. # tags_we_care_about = ("i", "b", "ul", "ol", "li", "table", "tr", "td", "th", "h1", "h2", "h3", "html", "head", "body") pattern = r"<\s*([a-zA-Z0-9]+).[^>]*>" anti_pattern = r"</\s*([a-zA-Z0-9]+)[^>]*>" d = {} anti_d = {} COUNT = 0 LIST = 1 for tag in tags_we_care_about: for dd in (d, anti_d): dd[tag] = [0, []] for p, dd in ((pattern, d), (anti_pattern, anti_d)): pos = 0 while (True): m = re.search(p, data[pos:]) if m is None: break tag = m.groups()[0].lower() pos = pos + m.start(1) + 1 if dd.has_key(tag): dd[tag][COUNT] += 1 dd[tag][LIST].append(pos) # # Check table nesting rules # T_TABLE = 0 T_ANTI_TABLE = 1 T_TR = 2 T_ANTI_TR = 3 T_TH = 4 T_ANTI_TH = 5 T_TD = 6 T_ANTI_TD = 7 T_UL = 8 T_ANTI_UL = 9 T_OL = 10 T_ANTI_OL = 11 T_LI = 12 T_ANTI_LI = 13 T_I = 14 T_ANTI_I = 15 T_B = 16 T_ANTI_B = 17 tokens = [] for tag, token, anti_token in (('table', T_TABLE, T_ANTI_TABLE), ('tr', T_TR, T_ANTI_TR), ('td', T_TD, T_ANTI_TD), ('th', T_TH, T_ANTI_TH), ('ul', T_UL, T_ANTI_UL), ('ol', T_OL, T_ANTI_OL), ('li', T_LI, T_ANTI_LI), ('i', T_I, T_ANTI_I), ('b', T_B, T_ANTI_B)): tokens += [(pos, token) for pos in d[tag][LIST]] tokens += [(pos, anti_token) for pos in anti_d[tag][LIST]] tokens = sorted(tokens) S_INIT = 0 S_AFTER_TABLE = 1 S_AFTER_TR = 2 S_AFTER_TD = 3 S_AFTER_TH = 4 S_AFTER_OL = 5 S_AFTER_UL = 6 S_AFTER_LI = 7 S_AFTER_I = 8 S_AFTER_B = 9 state_transitions = { S_INIT: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_I: S_AFTER_I, T_B: S_AFTER_B }, S_AFTER_TABLE: { T_ANTI_TABLE: S_INIT, T_TR: S_AFTER_TR }, S_AFTER_TR: { T_ANTI_TR: S_INIT, T_TD: S_AFTER_TD, T_TH: S_AFTER_TH }, S_AFTER_TD: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I, T_ANTI_TD: S_INIT }, S_AFTER_TH: { T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I, T_ANTI_TH: S_INIT }, S_AFTER_OL: { T_LI: S_AFTER_LI, T_ANTI_OL: S_INIT }, S_AFTER_UL: { T_LI: S_AFTER_LI, T_ANTI_UL: S_INIT }, S_AFTER_LI: { T_ANTI_LI: S_INIT, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL, T_B: S_AFTER_B, T_I: S_AFTER_I }, S_AFTER_I: { T_ANTI_I: S_INIT, T_I: S_AFTER_I, # Stupid but legal <i><i>Foo</i></i> T_B: S_AFTER_B, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL }, S_AFTER_B: { T_ANTI_B: S_INIT, T_B: S_AFTER_B, T_I: S_AFTER_I, T_TABLE: S_AFTER_TABLE, T_OL: S_AFTER_OL, T_UL: S_AFTER_UL } } state = [] for pos, token in tokens: self.assertTrue( len(state) >= 0, "Error in %s near position %d (%s)" % (module_name, pos, data[max(0, pos - 30):max(pos + 30, len(data))])) top_state, start_pos = (S_INIT, 0) if len(state) == 0 else state[-1] self.assertTrue( state_transitions[top_state].has_key(token), "Nesting error in %s near position %d (%s)" % (module_name, pos, data[max(0, pos - 50):pos] + "^^^" + data[pos:min(pos + 50, len(data))])) next_state = state_transitions[top_state][token] if next_state == S_INIT: state.pop() else: state.append((next_state, pos)) if len(state) > 0: self.assertEqual( len(state), 0, "Couldn't find last closing tag in %s. Last tag position = %d (%s)" % (module_name, state[-1][1], data[(state[-1][1] - 30):(state[-1][1] + 30)])) # # Check begin/end tag counts # for tag in tags_we_care_about: if d.has_key(tag): self.assertTrue( anti_d.has_key(tag), "Missing closing </%s> tag in %s" % (tag, module_name)) self.assertEqual( d[tag][COUNT], anti_d[tag][COUNT], "Found %d <%s>, != %d </%s> in %s" % (d[tag][COUNT], tag, anti_d[tag][COUNT], tag, module_name)) else: self.assertFalse( anti_d.has_key(tag), "Missing opening <%s> tag in %s" % (tag, module_name))
def test_01_02_run(self): module = instantiate_module(MODULE_NAME) module.input_objects_name.value = INPUT_OBJECTS_NAME module.output_objects_name.value = OUTPUT_OBJECTS_NAME module.module_num = 1 pipeline = cpp.Pipeline() pipeline.add_module(module) object_set = cpo.ObjectSet() # # Pick a bunch of random points, dilate them using the distance # transform and then label the result. # r = np.random.RandomState() r.seed(12) bimg = np.ones((100, 100), bool) bimg[r.randint(0, 100, 50), r.randint(0, 100, 50)] = False labels, count = label(distance_transform_edt(bimg) <= 5) # # Make the input objects # input_objects = cpo.Objects() input_objects.segmented = labels expected = skeletonize_labels(labels) object_set.add_objects(input_objects, INPUT_OBJECTS_NAME) # # Make the workspace # measurements = cpmeas.Measurements() workspace = cpw.Workspace(pipeline, module, None, object_set, measurements, None) module.run(workspace) # # Calculate the centers using Numpy. Scipy can do this too. # But maybe it's instructive to show you how to go at the labels # matrix using Numpy. # # We're going to get the centroids by taking the average value # of x and y per object. # y, x = np.mgrid[0:labels.shape[0], 0:labels.shape[1]].astype(float) # # np.bincount counts the number of occurrences of each integer value. # You need to operate on a 1d array - if you flatten the labels # and weights, their pixels still align. # # We do [1:] to discard the background which is labeled 0 # # The optional second argument to np.bincount is the "weight". For # each label value, maintain a running sum of the weights. # areas = np.bincount(expected.flatten())[1:] total_x = np.bincount(expected.flatten(), weights=x.flatten())[1:] total_y = np.bincount(expected.flatten(), weights=y.flatten())[1:] expected_location_x = total_x / areas expected_location_y = total_y / areas # # Now check against the measurements. # count_feature = I.C_COUNT + "_" + OUTPUT_OBJECTS_NAME self.assertTrue( measurements.has_feature(cpmeas.IMAGE, count_feature), "Your module did not produce a %s measurement" % count_feature) count = measurements.get_measurement(cpmeas.IMAGE, count_feature) self.assertEqual(count, len(areas)) for ftr, expected in ((I.M_LOCATION_CENTER_X, expected_location_x), (I.M_LOCATION_CENTER_Y, expected_location_y)): self.assertTrue(measurements.has_feature(OUTPUT_OBJECTS_NAME, ftr)) location = measurements.get_measurement(OUTPUT_OBJECTS_NAME, ftr) np.testing.assert_almost_equal(location, expected)
def make_instance(self): return instantiate_module("Example3a")
import cellprofiler.objects as cpo import cellprofiler.measurements as cpm import cellprofiler.pipeline as cpp import cellprofiler.settings as cps import cellprofiler.workspace as cpw import cellprofiler.modules.run_imagej as R import imagej.imagej2 as ij2 INPUT_IMAGE_NAME = "inputimage" OUTPUT_IMAGE_NAME = "outputimage" from cellprofiler.modules import instantiate_module try: instantiate_module("RunImageJ") skip_tests = False except: skip_tests = True @unittest.skipIf(skip_tests, "RunImageJ did not load (headless?)") class TestRunImageJ(unittest.TestCase): def test_01_01_load_v1(self): data = r"""CellProfiler Pipeline: http://www.cellprofiler.org Version:1 SVNRevision:9954 RunImageJ:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:1|show_window:True|notes:\x5B\x5D] Command or macro?:Command Command\x3A:Sharpen
def search_module_help(text): '''Search the help for a string text - find text in the module help using case-insensitive matching returns an html document of all the module help pages that matched or None if no match found. ''' matching_help = [] for item in MAIN_HELP: matching_help += __search_menu_helper( item, lambda x:__search_fn(x, text)) count = sum([len(x[2]) for x in matching_help]) for module_name in get_module_names(): module = instantiate_module(module_name) location = os.path.split( module.create_settings.im_func.func_code.co_filename)[0] if location == cpprefs.get_plugin_directory(): continue help_text = module.get_help() matches = __search_fn(help_text, text) if len(matches) > 0: matching_help.append((module_name, help_text, matches)) count += len(matches) if len(matching_help) == 0: return None top = """<html style="font-family:arial"> <head><title>%s found</title></head> <body><h1>Matches found</h1><br><ul> """ % ("1 match" if len(matching_help) == 1 else "%d matches" % len(matching_help)) body = "<br>" match_num = 1 prev_link = ( '<a href="#match%d" title="Previous match">' '<img src="memory:previous.png" alt="previous match"></a>') anchor = '<a name="match%d"><u>%s</u></a>' next_link = ('<a href="#match%d" title="Next match">' '<img src="memory:next.png" alt="next match"></a>') for title, help_text, pairs in matching_help: top += """<li><a href="#match%d">%s</a></li>\n""" % ( match_num, title) if help_text.find("<h1>") == -1: body += "<h1>%s</h1>" % title start_match = re.search(r"<\s*body[^>]*?>", help_text, re.IGNORECASE) if start_match is None: start = 0 else: start = start_match.end() end_match = re.search(r"<\\\s*body", help_text, re.IGNORECASE) if end_match is None: end = len(help_text) else: end = end_match.start() for begin_pos, end_pos in pairs: body += help_text[start:begin_pos] if match_num > 1: body += prev_link % (match_num - 1) body += anchor % (match_num, help_text[begin_pos:end_pos]) if match_num != count: body += next_link % (match_num + 1) start = end_pos match_num += 1 body += help_text[start:end] + "<br>" result = "%s</ul><br>\n%s</body></html>" % (top, body) return result
def setUp(self): self.module = instantiate_module("Example3b") self.module.input_image_name.value = INPUT_IMAGE_NAME self.E = __import__(self.module.__class__.__module__)