def test_02_01_load_one(self):
     folder = self.test_folder
     file_name = self.test_filename
     cpprefs.set_default_image_directory(self.test_path)
     workspace, module = self.make_workspace([file_name])
     assert isinstance(module, L.LoadSingleImage)
     module.prepare_run(workspace)
     module.run(workspace)
     m = workspace.measurements
     self.assertTrue(isinstance(m, cpmeas.Measurements))
     self.assertEqual(m.image_set_count, 1)
     f = m.get_all_measurements(
         cpmeas.IMAGE, "_".join((L.C_FILE_NAME, self.get_image_name(0))))
     self.assertEqual(len(f), 1)
     self.assertEqual(f[0], file_name)
     p = m.get_all_measurements(
         cpmeas.IMAGE, "_".join((L.C_PATH_NAME, self.get_image_name(0))))
     self.assertEqual(len(p), 1)
     self.assertEqual(p[0], self.test_path)
     s = m.get_all_measurements(
         cpmeas.IMAGE, "_".join((L.C_SCALING, self.get_image_name(0))))
     self.assertEqual(len(s), 1)
     self.assertEqual(s[0], 4095)
     md = m.get_all_measurements(
         cpmeas.IMAGE, "_".join((L.C_MD5_DIGEST, self.get_image_name(0))))
     self.assertEqual(len(md), 1)
     self.assertEqual(self.test_md5, md[0])
 def test_02_01_load_one(self):
     folder = self.test_folder
     file_name = self.test_filename
     cpprefs.set_default_image_directory(self.test_path)
     workspace, module = self.make_workspace([file_name])
     assert isinstance(module, L.LoadSingleImage)
     module.prepare_run(workspace)
     module.run(workspace)
     m = workspace.measurements
     self.assertTrue(isinstance(m, cpmeas.Measurements))
     self.assertEqual(m.image_set_count, 1)
     f = m.get_all_measurements(
         cpmeas.IMAGE, 
         "_".join((L.C_FILE_NAME, self.get_image_name(0))))
     self.assertEqual(len(f), 1)
     self.assertEqual(f[0], file_name)
     p = m.get_all_measurements(
         cpmeas.IMAGE, 
         "_".join((L.C_PATH_NAME, self.get_image_name(0))))
     self.assertEqual(len(p), 1)
     self.assertEqual(p[0], self.test_path)
     s = m.get_all_measurements(
         cpmeas.IMAGE,
         "_".join((L.C_SCALING, self.get_image_name(0))))
     self.assertEqual(len(s), 1)
     self.assertEqual(s[0], 4095)
     md = m.get_all_measurements(
         cpmeas.IMAGE,
         "_".join((L.C_MD5_DIGEST, self.get_image_name(0))))
     self.assertEqual(len(md), 1)
     self.assertEqual(self.test_md5, md[0])
 def test_02_01_load_one(self):
     path = os.path.join(example_images_directory(), "ExampleSpecklesImages")
     cpprefs.set_default_image_directory(path)
     file_name = "1-162hrh2ax2.tif"
     workspace, module = self.make_workspace([file_name])
     assert isinstance(module, L.LoadSingleImage)
     module.prepare_run(workspace)
     module.run(workspace)
     m = workspace.measurements
     self.assertTrue(isinstance(m, cpmeas.Measurements))
     self.assertEqual(m.image_set_count, 1)
     f = m.get_all_measurements(cpmeas.IMAGE, 
                                "_".join((L.C_FILE_NAME, self.get_image_name(0))))
     self.assertEqual(len(f), 1)
     self.assertEqual(f[0], file_name)
     p = m.get_all_measurements(cpmeas.IMAGE, 
                                "_".join((L.C_PATH_NAME, self.get_image_name(0))))
     self.assertEqual(len(p), 1)
     self.assertEqual(p[0], path)
     s = m.get_all_measurements(cpmeas.IMAGE,
                                "_".join((L.C_SCALING, self.get_image_name(0))))
     self.assertEqual(len(s), 1)
     self.assertEqual(s[0], 4095)
     md = m.get_all_measurements(cpmeas.IMAGE,
                                "_".join((L.C_MD5_DIGEST, self.get_image_name(0))))
     self.assertEqual(len(md), 1)
     md5 = hashlib.md5()
     image = workspace.image_set.get_image(self.get_image_name(0))
     md5.update(np.ascontiguousarray(image.pixel_data).data)
     self.assertEqual(md5.hexdigest(), md[0])
 def test_03_03_measurement_columns(self):
     '''Check that results of get_measurement_columns match the actual column names output'''
     data = 'eJztW91u2zYUph0naFqgSHexBesNd9d2iSA7SZcEQ2vPXjcPsRs0Xn8wbJgi0TEHWjQkKrU39N32GHuEXe4RJtqyJXFyJMuSJ6cSQEiH5seP5/DwHEqyWrXOWe0beCTJsFXr7HcxQfCcKKxLjf4pHFATD/dg3UAKQxqk+ilsUR3+YBFYPoDlw1P55PTgEFZk+QTEOArN1n37tHsMwJZ9vmOXovPTpiMXPIXLF4gxrF+Zm6AEdp36P+3yWjGwcknQa4VYyHQppvVNvUs7o8HspxbVLILaSt/b2D7aVv8SGebL7hTo/HyOh4hc4N+RoMK02St0jU1MdQfv9C/WzngpE3i5HXqfu3YoCHYo2eWhp563/x647UsBdnvgab/jyFjX8DXWLIVA3FeuZqPg/ckh/W34+tsAjXZtjDsOwW0J49ga21klCEfjLfjwBVBxxlsNwe0IvLx00JDtfztUVAb7ClN7SYw/DL8p4LlcR4SYEe0+T/9FcQcRcSUfrgSe7h3KUey9LejJ5Td24DDMHtKc+mX0jWPn5tnZj62IvKJ/v7NXR1y96yNGB0Qx+wvoPW99heGKPlwRtGk0vnm4MH3vCfpy+SUzLfgdoZcKmemblN3C4txnQj9cbqCuYhEGmzzIwQY2kMqoMVrKDxbFlSU58fi4JeCnxxS/7bFbNYQ36jzGyTOyJI+PvbJz4RlX2vEy6flbNF7aupfTjM/z9Et6nhaNH2U5Xnw/WlLvtOYnAHcUR7+nIJrf3wH++eFyvafoOiKVuPG0qTOkm5iNnPoo/dwV+uFyg0KdMmiZyO0nblyKmweT0n9RfjkgDiSpr+gv5Yi4uOtP9Os21VGaeSZoXl7wG03dvv1awk5fxbRT0P5oEX3fhvB9IejL5V+kJ/uPnp9//Yq+fyZ9+Xh8Xafk2U/y/snPf1Q+PF7ADnH3qUH5vvOeQtXeb5nOneAydumF8B8L/FzmdniHFMMxxOGHiWlaVGc91zjjuoYycmuSjGNx8uYbhK96/JnINX8AoKvz/HgR+y2RHyLls7h+E2THF9RAVwa1dG15vcP407q/Cto/xI3raeaDqPf7WdGvGjLOpPJBmnl6nfJB0vl8XeL/qvN+1uJEVtb7qvSTpaP/fZxpP39Jcj+2alxW9lFZm+e0909xcX/turiCgAt637RK+4xfTnEDDaL3E7Se6OVvSGVuR+uyLjzjhljX0CDF/tZlnd12XBWsZp1E7edjs9tt0TercTDXN8fluOzgwvYRnwD/uuIytRjBOvrPRiLN9b0jjIMXN35PRrFOds9a/Lxt+THHZQOXlfiS4/K4m+M+XlwV3Oznef7LcTkux+W424X7u+DixPcbXPa+N+ftf/XwBOWJJ8CfJ7isIkIGBuXfTRlSf/xxjykRqmiTr2ukM/uy6fnQhvMMQniqAk91Hg/WkM5wdzQwbDaL0b7CsCo1ndpzu7Y2reW8vRDeoPfzN/KaSKW6phijGefFtCYKX0Xgq8zj6yPFtAzE0JDZJ6k1ETsT0bWr10+2A/i88120pU8fPrh7k38B4Pcr19/+eR6Hb2OjWLgP/P/zuheCKwG/n4/9Gizm149uaD/VMavt/wXhfSus'
     # alternate data: 1 image, 1 object, tex scale=3, gabor angles=6
     #        data = 'eJztW91u2zYUph0nW1agSHexFesNd9d2iSA7TZMGQ2vPXjcPsWs0Xrti2DBGomMONGlIVGpv6Hvtco+yy13uESbasiVzciTLP7UzCSCkQ/Hjx3N4eA5lWbVS86z0FTzSdFgrNQ9ahGLYoEi0uNU5hV1uk94+LFsYCWxCzk5hjTP4nUNh/hDmj08LJ6eHBVjQ9ScgwZGp1m67p7+OAdhxzx+6Jevd2vbkTKBI+RwLQdilvQ1y4K5X/6dbXiGLoAuKXyHqYNunGNVXWYs3+93xrRo3HYrrqBNs7B51p3OBLftFawT0bjdID9Nz8htWVBg1e4mviE048/Be/2rtmJcLhVfaofGZb4eMYoecW+4F6mX7b4HfPhditzuB9nueTJhJrojpIApJB12ORyH70yP625robwtU6qUB7iQCt6OMY2dgZ4NiEo83M4HPgLw33mIEbk/hlaWJe+Lg6x4yBOwgYbQXMf4o/LaCl3IZU2rHtPs0/WfFHcbE5SZwOfB4/5Eex967ip5Sfu0GDstuY9Orn0ffJHaunp19X4vJq/r3G3d1JNW73Be8S5HdmUHvaesrCpedwGVBncfjm4aL0veWoq+UXwjbgd9QfoHoWN9511VUfPtUwUu5glvIoQJWZXCDFWJhQ3CrP9f8z4rLa/rC1ueOghsdI9yud17kvCXJK7qmD479vHcRGNesdngcE7fq+VLjoqtzfp75SqrfqudHjRN5fT4/Xda8xpmfmLijefSLinvB/d2eJ5fbiDFMC0nzTZUJzGwi+oFxRPXzkdKPlCscMi6gY2O/n1XN8yjfLUr/Wfn1kDiwSH2T7uPirL84fl3nDC8zv4TNy3P5QMncx6w57HSc0E5h+6BZ9P0hgu9zRV8p/6w9PLj/rPHlS/72qfbFg8F1mdOnP+oHT376vfDuwQx2SLofDcvzzbccGu5+1Pae+OaxSzuC/0Thl7K0wxuMLM8Qj94NTVPjTLR94wzqKqjv1ywyjiXJm68xuWzL3z6u5IM+M6b58YryQ6x8ltRvwuz4nFv40uIOM+fXO4p/Wc9RYfuHpHF9mfmgsGH6FSPGuah8sMw8vUn5YNH5fFPi/6rz/rrFiXVZ76vST9eO3vs4l/27yyL3Y6vGrcs+at3medn7p6S4P+76uIyCC3uvtEr7DF5CSQN14/cTtp74xa/YEH5Hm7IuAuOGhJm4u8T+NmWd3XRcEaxmncTt56bYbV3jQopLcSkuxb2vfcfHYDIuSpk7ghKG/7NRWGZ83lPGIYufj4aj2CS7r1v+u2n5PsWtB25d4kuKS+Nuivv/4orgej9P81+KS3EpLsXdLNzfGR+nvr+QcvC9uGz/S4AnLE88BJN5QsoGprRrcfn9k6V1Bh/p2BrlyBx+JaOduZfVwAczkqcbwVNUeIrTeIiJmSCtftdy2RzBO0gQQ6t6tQ23tjSqlbztCN6w9+/X8trY4MxEVn/MeT6qicNXUPgK0/g6GNmOhQXuCfek1YZicyj6dg36yW4IX3C+s670yb07H1znXwBM+pXvb/88S8K3tZXN3AaT/+O6FYHLgUk/H/g1mM2v71/TfqTjurb/F2wyHOE='
     cpprefs.set_default_image_directory(
         os.path.join(example_images_directory(), "ExampleSBSImages"))
     fd = StringIO(zlib.decompress(base64.b64decode(data)))
     pipeline = cpp.Pipeline()
     pipeline.load(fd)
     module = pipeline.modules()[3]
     measurements = pipeline.run(image_set_end=1)
     for x in module.get_measurement_columns(pipeline):
         assert x[1] in measurements.get_feature_names(
             x[0]
         ), '%s does not match any measurement output by pipeline' % (
             str(x))
     for obname in measurements.get_object_names():
         for m in measurements.get_feature_names(obname):
             if m.startswith(M.TEXTURE):
                 assert (
                     obname, m, 'float'
                 ) in module.get_measurement_columns(
                     pipeline
                 ), 'no entry matching %s in get_measurement_columns.' % (
                     (obname, m, 'float'))
Ejemplo n.º 5
0
 def enter_batch_mode(self, workspace):
     '''Restore the image set list from its setting as we go into batch mode'''
     pipeline = workspace.pipeline
     assert isinstance(pipeline, cpp.Pipeline)
     assert not self.distributed_mode, "Distributed mode no longer supported"
     cpprefs.set_default_output_directory(self.custom_output_directory.value)
     cpprefs.set_default_image_directory(self.default_image_directory.value)
Ejemplo n.º 6
0
 def test_03_02_pipeline_preferences(self):
     #
     # Walk the worker up through pipelines and preferences.
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [1],
                          worker_runs_post_group = False,
                          wants_dictionary = True))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     maybe_download_example_image(["ExampleSBSImages"],
                                  "Channel1-01-A-01.tif")
     maybe_download_example_image(["ExampleHT29"],
                                  "AS_09125_050116030001_D03f00d0.tif")
     input_dir = os.path.normcase(
         os.path.join(example_images_directory(), "ExampleSBSImages"))
     output_dir = os.path.normcase(
         os.path.join(example_images_directory(), "ExampleHT29"))
     cpprefs.set_default_image_directory(input_dir)
     input_dir = cpprefs.get_default_image_directory()
     cpprefs.set_default_output_directory(output_dir)
     output_dir = cpprefs.get_default_output_directory()
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY),
                    cpprefs.DEFAULT_OUTPUT_DIRECTORY:
                    cpprefs.config_read(cpprefs.DEFAULT_OUTPUT_DIRECTORY)}
     cpprefs.set_default_image_directory(example_images_directory())
     cpprefs.set_default_output_directory(example_images_directory())
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(GOOD_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # Get the next request so that we know the worker has
     # processed the preferences.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertEqual(cpprefs.get_default_image_directory(), 
                      input_dir)
     self.assertEqual(cpprefs.get_default_output_directory(),
                      output_dir)
     self.assertIn(self.analysis_id, 
                   self.awthread.aw.pipelines_and_preferences)
     pipe, prefs = self.awthread.aw.pipelines_and_preferences[
         self.analysis_id]
     self.assertEqual(len(pipe.modules()), 7)
     #
     # Cancel and check for exit
     #
     req.reply(cpanalysis.ServerExited())
     self.assertRaises(cpp.CancelledException, self.awthread.ecute)
    def test_03_03_initial_measurements(self):
        #
        # Walk to the initial measurements
        #
        self.awthread = self.AWThread(self.announce_addr)
        self.awthread.start()
        self.set_work_socket()
        self.awthread.ex(
            self.awthread.aw.do_job,
            cpanalysis.WorkReply(image_set_numbers=[1],
                                 worker_runs_post_group=False,
                                 wants_dictionary=True))
        #
        # The worker should ask for the pipeline and preferences next.
        #
        req = self.awthread.recv(self.work_socket)
        self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
        self.assertEqual(req.analysis_id, self.analysis_id)

        input_dir = os.path.join(example_images_directory(),
                                 "ExampleSBSImages")
        cpprefs.set_default_image_directory(input_dir)
        preferences = {
            cpprefs.DEFAULT_IMAGE_DIRECTORY:
            cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY)
        }

        rep = cpanalysis.Reply(pipeline_blob=np.array(GOOD_PIPELINE),
                               preferences=preferences)
        req.reply(rep)
        #
        # The worker asks for the initial measurements.
        #
        req = self.awthread.recv(self.work_socket)
        self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
        self.assertEqual(req.analysis_id, self.analysis_id)
        m = get_measurements_for_good_pipeline()
        try:
            req.reply(cpanalysis.Reply(buf=m.file_contents()))
            req = self.awthread.recv(self.work_socket)
            #
            # Check that they were installed
            #
            self.assertIn(self.analysis_id,
                          self.awthread.aw.initial_measurements)
            cm = self.awthread.aw.initial_measurements[self.analysis_id]
            for object_name in m.get_object_names():
                for feature_name in m.get_feature_names(object_name):
                    self.assertTrue(cm.has_feature(object_name, feature_name))
                    self.assertEqual(cm[object_name, feature_name, 1],
                                     m[object_name, feature_name, 1])
            #
            # Cancel and check for exit
            #
            req.reply(cpanalysis.ServerExited())
            self.assertRaises(cpaw.CancelledException, self.awthread.ecute)
        finally:
            m.close()
Ejemplo n.º 8
0
 def test_01_03_unicode_directory(self):
     old = cpprefs.get_default_image_directory()
     unicode_dir = u'P125 à 144 Crible Chimiothèque HBEC'
     unicode_dir = tempfile.mkdtemp(prefix=unicode_dir)
     cpprefs.set_default_image_directory(unicode_dir)
     self.assertEqual(cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY),
                      unicode_dir)
     self.assertEqual(cpprefs.get_default_image_directory(), unicode_dir)
     cpprefs.set_default_image_directory(old)
Ejemplo n.º 9
0
 def test_01_03_unicode_directory(self):
     old = cpprefs.get_default_image_directory()
     unicode_dir = u'P125 à 144 Crible Chimiothèque HBEC'
     unicode_dir = tempfile.mkdtemp(prefix=unicode_dir)
     cpprefs.set_default_image_directory(unicode_dir)
     self.assertEqual(cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY),
                      unicode_dir)
     self.assertEqual(cpprefs.get_default_image_directory(), unicode_dir)
     cpprefs.set_default_image_directory(old)
Ejemplo n.º 10
0
 def test_03_03_initial_measurements(self):
     #
     # Walk to the initial measurements
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [1],
                          worker_runs_post_group = False,
                          wants_dictionary = True))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(GOOD_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline()
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
         req = self.awthread.recv(self.work_socket)
         #
         # Check that they were installed
         #
         self.assertIn(self.analysis_id, 
                       self.awthread.aw.initial_measurements)
         cm = self.awthread.aw.initial_measurements[self.analysis_id]
         for object_name in m.get_object_names():
             for feature_name in m.get_feature_names(object_name):
                 self.assertTrue(cm.has_feature(object_name, feature_name))
                 self.assertEqual(cm[object_name, feature_name, 1], 
                                  m[object_name, feature_name, 1])
         #
         # Cancel and check for exit
         #
         req.reply(cpanalysis.ServerExited())
         self.assertRaises(cpaw.CancelledException, self.awthread.ecute)
     finally:
         m.close()
Ejemplo n.º 11
0
    def test_01_06_run_pipeline(self):
        import cellprofiler.pipeline as cpp
        import cellprofiler.cpmodule as cpm
        import os
        from cellprofiler.preferences import \
             set_default_image_directory, set_default_output_directory
        maybe_download_fly()
        example_fly_dir = os.path.join(self.example_dir(), "ExampleFlyImages")
        set_default_image_directory(example_fly_dir)
        output_dir = tempfile.mkdtemp()
        set_default_output_directory(output_dir)
        try:
            pipeline_file = os.path.join(example_fly_dir,
                                         "ExampleFlyURL.cppipe")
            if not os.path.exists(pipeline_file):
                pipeline_file = os.path.join(example_fly_dir, "ExampleFly.cp")
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(
                    isinstance(
                        event,
                        (cpp.LoadExceptionEvent, cpp.RunExceptionEvent)))

            pipeline.add_listener(callback)
            pipeline.load(pipeline_file)
            while True:
                removed_something = False
                for module in reversed(pipeline.modules()):
                    self.assertTrue(isinstance(module, cpm.CPModule))
                    if module.module_name in ("SaveImages",
                                              "CalculateStatistics",
                                              "ExportToSpreadsheet",
                                              "ExportToDatabase"):
                        pipeline.remove_module(module.module_num)
                        removed_something = True
                        break
                if not removed_something:
                    break
            for module in pipeline.modules():
                module.show_window = False
            m = pipeline.run(image_set_end=1)
            del m
        finally:
            for file_name in os.listdir(output_dir):
                try:
                    os.remove(os.path.join(output_dir, file_name))
                except Exception, e:
                    print "Failed to remove %s" % os.path.join(
                        output_dir, file_name), e
                    traceback.print_exc()
            try:
                os.rmdir(output_dir)
            except:
                print "Failed to remove %s" % output_dir
                traceback.print_exc()
Ejemplo n.º 12
0
    def test_04_01_load_objects(self):
        r = np.random.RandomState()
        r.seed(41)
        labels = np.random.randint(0, 10, size=(30, 40))
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                      m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            o = object_set.get_objects(OBJECTS_NAME)
            np.testing.assert_equal(labels, o.segmented)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_COUNT, OBJECTS_NAME))), 9)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_OBJECTS_FILE_NAME, OBJECTS_NAME))), filename)
            self.assertEqual(
                m.get_current_image_measurement("_".join(
                    (L.C_OBJECTS_PATH_NAME, OBJECTS_NAME))), directory)
            for feature in (M_LOCATION_CENTER_X, M_LOCATION_CENTER_Y,
                            M_NUMBER_OBJECT_NUMBER):
                values = m.get_current_measurement(OBJECTS_NAME, feature)
                self.assertEqual(len(values), 9)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
Ejemplo n.º 13
0
    def test_01_06_run_pipeline(self):
        import cellprofiler.pipeline as cpp
        import cellprofiler.cpmodule as cpm
        import os
        from cellprofiler.preferences import set_default_image_directory, set_default_output_directory

        maybe_download_fly()
        example_fly_dir = os.path.join(self.example_dir(), "ExampleFlyImages")
        set_default_image_directory(example_fly_dir)
        output_dir = tempfile.mkdtemp()
        set_default_output_directory(output_dir)
        try:
            pipeline_file = os.path.join(example_fly_dir, "ExampleFlyURL.cppipe")
            if not os.path.exists(pipeline_file):
                pipeline_file = os.path.join(example_fly_dir, "ExampleFly.cp")
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, (cpp.LoadExceptionEvent, cpp.RunExceptionEvent)))

            pipeline.add_listener(callback)
            pipeline.load(pipeline_file)
            while True:
                removed_something = False
                for module in reversed(pipeline.modules()):
                    self.assertTrue(isinstance(module, cpm.CPModule))
                    if module.module_name in (
                        "SaveImages",
                        "CalculateStatistics",
                        "ExportToSpreadsheet",
                        "ExportToDatabase",
                    ):
                        pipeline.remove_module(module.module_num)
                        removed_something = True
                        break
                if not removed_something:
                    break
            for module in pipeline.modules():
                module.show_window = False
            m = pipeline.run(image_set_end=1)
            del m
        finally:
            for file_name in os.listdir(output_dir):
                try:
                    os.remove(os.path.join(output_dir, file_name))
                except Exception, e:
                    print "Failed to remove %s" % os.path.join(output_dir, file_name), e
                    traceback.print_exc()
            try:
                os.rmdir(output_dir)
            except:
                print "Failed to remove %s" % output_dir
                traceback.print_exc()
Ejemplo n.º 14
0
 def test_02_02_scale(self):
     '''Load an image twice, as scaled and unscaled'''
     path = os.path.join(example_images_directory(), "ExampleSpecklesImages")
     cpprefs.set_default_image_directory(path)
     file_names = ["1-162hrh2ax2.tif", "1-162hrh2ax2.tif"]
     workspace, module = self.make_workspace(file_names)
     self.assertTrue(isinstance(module, L.LoadSingleImage))
     module.file_settings[0].rescale.value = False
     module.file_settings[1].rescale.value = True
     module.run(workspace)
     unscaled, scaled = [workspace.image_set.get_image(self.get_image_name(i)).pixel_data
                         for i in range(2)]
     np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
Ejemplo n.º 15
0
 def setUp(self):
     #
     # Make a temporary directory structure
     #
     cpprefs.set_headless()
     directory = self.directory = tempfile.mkdtemp()
     for i in range(3):
         os.mkdir(os.path.join(directory, str(i)))
         for j in range(3):
             os.mkdir(os.path.join(directory, str(i), str(j)))
             for k in range(3):
                 os.mkdir(os.path.join(directory, str(i), str(j), str(k)))
     cpprefs.set_default_image_directory(os.path.join(self.directory, "1"))
 def test_02_02_scale(self):
     '''Load an image twice, as scaled and unscaled'''
     path = os.path.join(example_images_directory(), "ExampleSpecklesImages")
     cpprefs.set_default_image_directory(path)
     file_names = ["1-162hrh2ax2.tif", "1-162hrh2ax2.tif"]
     workspace, module = self.make_workspace(file_names)
     self.assertTrue(isinstance(module, L.LoadSingleImage))
     module.file_settings[0].rescale.value = False
     module.file_settings[1].rescale.value = True
     module.run(workspace)
     unscaled, scaled = [workspace.image_set.get_image(self.get_image_name(i)).pixel_data
                         for i in range(2)]
     np.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
Ejemplo n.º 17
0
 def setUp(self):
     #
     # Make a temporary directory structure
     #
     cpprefs.set_headless()
     directory = self.directory = tempfile.mkdtemp()
     for i in range(3):
         os.mkdir(os.path.join(directory, str(i)))
         for j in range(3):
             os.mkdir(os.path.join(directory, str(i), str(j)))
             for k in range(3):
                 os.mkdir(os.path.join(directory, str(i), str(j), str(k)))
     cpprefs.set_default_image_directory(os.path.join(self.directory, "1"))
Ejemplo n.º 18
0
    def test_04_01_load_objects(self):
        r = np.random.RandomState()
        r.seed(41)
        labels = np.random.randint(0, 10, size=(30, 40))
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(
                    pipeline, module, image_set, object_set, m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            o = object_set.get_objects(OBJECTS_NAME)
            np.testing.assert_equal(labels, o.segmented)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((L.C_COUNT, OBJECTS_NAME))), 9)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((L.C_OBJECTS_FILE_NAME, OBJECTS_NAME))), filename)
            self.assertEqual(m.get_current_image_measurement(
                    "_".join((L.C_OBJECTS_PATH_NAME, OBJECTS_NAME))), directory)
            for feature in (M_LOCATION_CENTER_X, M_LOCATION_CENTER_Y, M_NUMBER_OBJECT_NUMBER):
                values = m.get_current_measurement(OBJECTS_NAME, feature)
                self.assertEqual(len(values), 9)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
Ejemplo n.º 19
0
 def enter_batch_mode(self, workspace):
     '''Restore the image set list from its setting as we go into batch mode'''
     image_set_list = workspace.image_set_list
     pipeline = workspace.pipeline
     assert isinstance(image_set_list, cpi.ImageSetList)
     assert isinstance(pipeline, cpp.Pipeline)
     if self.distributed_mode:
         import tempfile
         try:
             cpprefs.set_default_output_directory(tempfile.gettempdir())
             cpprefs.set_default_image_directory(tempfile.gettempdir())
         except:
             pass
     else:
         cpprefs.set_default_output_directory(self.custom_output_directory.value)
         cpprefs.set_default_image_directory(self.default_image_directory.value)
Ejemplo n.º 20
0
 def setUp(self):
     #
     # Make three temporary directory structures
     #
     cpprefs.set_headless()
     self.directories = [tempfile.mkdtemp() for i in range(3)]
     for directory in self.directories:
         for i in range(3):
             os.mkdir(os.path.join(directory, str(i)))
             for j in range(3):
                 os.mkdir(os.path.join(directory, str(i), str(j)))
                 for k in range(3):
                     os.mkdir(os.path.join(directory, str(i), str(j), str(k)))
     cpprefs.set_default_image_directory(os.path.join(self.directories[0], "1"))
     cpprefs.set_default_output_directory(os.path.join(self.directories[1], "1"))
     self.root_directory = os.path.join(self.directories[2], "1")
Ejemplo n.º 21
0
 def enter_batch_mode(self, workspace):
     '''Restore the image set list from its setting as we go into batch mode'''
     image_set_list = workspace.image_set_list
     pipeline = workspace.pipeline
     assert isinstance(image_set_list, cpi.ImageSetList)
     assert isinstance(pipeline, cpp.Pipeline)
     if self.distributed_mode:
         import tempfile
         try:
             cpprefs.set_default_output_directory(tempfile.gettempdir())
             cpprefs.set_default_image_directory(tempfile.gettempdir())
         except:
             pass
     else:
         cpprefs.set_default_output_directory(self.custom_output_directory.value)
         cpprefs.set_default_image_directory(self.default_image_directory.value)
Ejemplo n.º 22
0
    def test_03_03_measurement_columns(self):
        '''Check that results of get_measurement_columns match the actual column names output'''
        data = 'eJztW91u2zYUph0naFqgSHexBesNd9d2iSA7SZcEQ2vPXjcPsRs0Xn8wbJgi0TEHWjQkKrU39N32GHuEXe4RJtqyJXFyJMuSJ6cSQEiH5seP5/DwHEqyWrXOWe0beCTJsFXr7HcxQfCcKKxLjf4pHFATD/dg3UAKQxqk+ilsUR3+YBFYPoDlw1P55PTgEFZk+QTEOArN1n37tHsMwJZ9vmOXovPTpiMXPIXLF4gxrF+Zm6AEdp36P+3yWjGwcknQa4VYyHQppvVNvUs7o8HspxbVLILaSt/b2D7aVv8SGebL7hTo/HyOh4hc4N+RoMK02St0jU1MdQfv9C/WzngpE3i5HXqfu3YoCHYo2eWhp563/x647UsBdnvgab/jyFjX8DXWLIVA3FeuZqPg/ckh/W34+tsAjXZtjDsOwW0J49ga21klCEfjLfjwBVBxxlsNwe0IvLx00JDtfztUVAb7ClN7SYw/DL8p4LlcR4SYEe0+T/9FcQcRcSUfrgSe7h3KUey9LejJ5Td24DDMHtKc+mX0jWPn5tnZj62IvKJ/v7NXR1y96yNGB0Qx+wvoPW99heGKPlwRtGk0vnm4MH3vCfpy+SUzLfgdoZcKmemblN3C4txnQj9cbqCuYhEGmzzIwQY2kMqoMVrKDxbFlSU58fi4JeCnxxS/7bFbNYQ36jzGyTOyJI+PvbJz4RlX2vEy6flbNF7aupfTjM/z9Et6nhaNH2U5Xnw/WlLvtOYnAHcUR7+nIJrf3wH++eFyvafoOiKVuPG0qTOkm5iNnPoo/dwV+uFyg0KdMmiZyO0nblyKmweT0n9RfjkgDiSpr+gv5Yi4uOtP9Os21VGaeSZoXl7wG03dvv1awk5fxbRT0P5oEX3fhvB9IejL5V+kJ/uPnp9//Yq+fyZ9+Xh8Xafk2U/y/snPf1Q+PF7ADnH3qUH5vvOeQtXeb5nOneAydumF8B8L/FzmdniHFMMxxOGHiWlaVGc91zjjuoYycmuSjGNx8uYbhK96/JnINX8AoKvz/HgR+y2RHyLls7h+E2THF9RAVwa1dG15vcP407q/Cto/xI3raeaDqPf7WdGvGjLOpPJBmnl6nfJB0vl8XeL/qvN+1uJEVtb7qvSTpaP/fZxpP39Jcj+2alxW9lFZm+e0909xcX/turiCgAt637RK+4xfTnEDDaL3E7Se6OVvSGVuR+uyLjzjhljX0CDF/tZlnd12XBWsZp1E7edjs9tt0TercTDXN8fluOzgwvYRnwD/uuIytRjBOvrPRiLN9b0jjIMXN35PRrFOds9a/Lxt+THHZQOXlfiS4/K4m+M+XlwV3Oznef7LcTkux+W424X7u+DixPcbXPa+N+ftf/XwBOWJJ8CfJ7isIkIGBuXfTRlSf/xxjykRqmiTr2ukM/uy6fnQhvMMQniqAk91Hg/WkM5wdzQwbDaL0b7CsCo1ndpzu7Y2reW8vRDeoPfzN/KaSKW6phijGefFtCYKX0Xgq8zj6yPFtAzE0JDZJ6k1ETsT0bWr10+2A/i88120pU8fPrh7k38B4Pcr19/+eR6Hb2OjWLgP/P/zuheCKwG/n4/9Gizm149uaD/VMavt/wXhfSus'
        # alternate data: 1 image, 1 object, tex scale=3, gabor angles=6
#        data = 'eJztW91u2zYUph0nW1agSHexFesNd9d2iSA7TZMGQ2vPXjcPsWs0Xrti2DBGomMONGlIVGpv6Hvtco+yy13uESbasiVzciTLP7UzCSCkQ/Hjx3N4eA5lWbVS86z0FTzSdFgrNQ9ahGLYoEi0uNU5hV1uk94+LFsYCWxCzk5hjTP4nUNh/hDmj08LJ6eHBVjQ9ScgwZGp1m67p7+OAdhxzx+6Jevd2vbkTKBI+RwLQdilvQ1y4K5X/6dbXiGLoAuKXyHqYNunGNVXWYs3+93xrRo3HYrrqBNs7B51p3OBLftFawT0bjdID9Nz8htWVBg1e4mviE048/Be/2rtmJcLhVfaofGZb4eMYoecW+4F6mX7b4HfPhditzuB9nueTJhJrojpIApJB12ORyH70yP625robwtU6qUB7iQCt6OMY2dgZ4NiEo83M4HPgLw33mIEbk/hlaWJe+Lg6x4yBOwgYbQXMf4o/LaCl3IZU2rHtPs0/WfFHcbE5SZwOfB4/5Eex967ip5Sfu0GDstuY9Orn0ffJHaunp19X4vJq/r3G3d1JNW73Be8S5HdmUHvaesrCpedwGVBncfjm4aL0veWoq+UXwjbgd9QfoHoWN9511VUfPtUwUu5glvIoQJWZXCDFWJhQ3CrP9f8z4rLa/rC1ueOghsdI9yud17kvCXJK7qmD479vHcRGNesdngcE7fq+VLjoqtzfp75SqrfqudHjRN5fT4/Xda8xpmfmLijefSLinvB/d2eJ5fbiDFMC0nzTZUJzGwi+oFxRPXzkdKPlCscMi6gY2O/n1XN8yjfLUr/Wfn1kDiwSH2T7uPirL84fl3nDC8zv4TNy3P5QMncx6w57HSc0E5h+6BZ9P0hgu9zRV8p/6w9PLj/rPHlS/72qfbFg8F1mdOnP+oHT376vfDuwQx2SLofDcvzzbccGu5+1Pae+OaxSzuC/0Thl7K0wxuMLM8Qj94NTVPjTLR94wzqKqjv1ywyjiXJm68xuWzL3z6u5IM+M6b58YryQ6x8ltRvwuz4nFv40uIOM+fXO4p/Wc9RYfuHpHF9mfmgsGH6FSPGuah8sMw8vUn5YNH5fFPi/6rz/rrFiXVZ76vST9eO3vs4l/27yyL3Y6vGrcs+at3medn7p6S4P+76uIyCC3uvtEr7DF5CSQN14/cTtp74xa/YEH5Hm7IuAuOGhJm4u8T+NmWd3XRcEaxmncTt56bYbV3jQopLcSkuxb2vfcfHYDIuSpk7ghKG/7NRWGZ83lPGIYufj4aj2CS7r1v+u2n5PsWtB25d4kuKS+Nuivv/4orgej9P81+KS3EpLsXdLNzfGR+nvr+QcvC9uGz/S4AnLE88BJN5QsoGprRrcfn9k6V1Bh/p2BrlyBx+JaOduZfVwAczkqcbwVNUeIrTeIiJmSCtftdy2RzBO0gQQ6t6tQ23tjSqlbztCN6w9+/X8trY4MxEVn/MeT6qicNXUPgK0/g6GNmOhQXuCfek1YZicyj6dg36yW4IX3C+s670yb07H1znXwBM+pXvb/88S8K3tZXN3AaT/+O6FYHLgUk/H/g1mM2v71/TfqTjurb/F2wyHOE='
        cpprefs.set_default_image_directory(os.path.join(example_images_directory(),"ExampleSBSImages"))
        fd = StringIO(zlib.decompress(base64.b64decode(data)))
        pipeline = cpp.Pipeline()
        pipeline.load(fd)
        module = pipeline.modules()[3]
        measurements = pipeline.run(image_set_end=1)
        for x in module.get_measurement_columns(pipeline):
            assert x[1] in measurements.get_feature_names(x[0]), '%s does not match any measurement output by pipeline'%(str(x))
        for obname in measurements.get_object_names():
            for m in measurements.get_feature_names(obname):
                if m.startswith(M.TEXTURE):
                    assert (obname, m, 'float') in module.get_measurement_columns(pipeline), 'no entry matching %s in get_measurement_columns.'%((obname, m, 'float'))
Ejemplo n.º 23
0
    def test_04_02_object_outlines(self):
        labels = np.zeros((30, 40), int)
        labels[10:15, 20:30] = 1
        expected_outlines = labels != 0
        expected_outlines[11:14, 21:29] = False
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            fs.wants_outlines.value = True
            fs.outlines_name.value = OUTLINES_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(pipeline, module, image_set, object_set,
                                      m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            outlines = image_set.get_image(OUTLINES_NAME)
            np.testing.assert_equal(outlines.pixel_data, expected_outlines)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
Ejemplo n.º 24
0
    def test_04_02_object_outlines(self):
        labels = np.zeros((30, 40), int)
        labels[10:15, 20:30] = 1
        expected_outlines = labels != 0
        expected_outlines[11:14, 21:29] = False
        filename = "myobjects.png"
        directory = tempfile.mkdtemp()
        cpprefs.set_default_image_directory(directory)
        pilimage = PIL.Image.fromarray(labels.astype(np.uint8), "L")
        pilimage.save(os.path.join(directory, filename))
        del pilimage
        try:
            module = L.LoadSingleImage()
            module.module_num = 1
            module.directory.set_dir_choice(L.DEFAULT_INPUT_FOLDER_NAME)
            fs = module.file_settings[0]
            fs.file_name.value = filename
            fs.image_objects_choice.value = L.IO_OBJECTS
            fs.objects_name.value = OBJECTS_NAME
            fs.wants_outlines.value = True
            fs.outlines_name.value = OUTLINES_NAME
            pipeline = cpp.Pipeline()

            def callback(caller, event):
                self.assertFalse(isinstance(event, cpp.RunExceptionEvent))

            pipeline.add_listener(callback)
            pipeline.add_module(module)
            m = cpmeas.Measurements()
            object_set = cpo.ObjectSet()
            image_set_list = cpi.ImageSetList()
            image_set = image_set_list.get_image_set(0)
            workspace = cpw.Workspace(
                    pipeline, module, image_set, object_set, m, image_set_list)
            module.prepare_run(workspace)
            module.run(workspace)

            outlines = image_set.get_image(OUTLINES_NAME)
            np.testing.assert_equal(outlines.pixel_data, expected_outlines)
        finally:
            try:
                os.remove(os.path.join(directory, filename))
                os.rmdir(directory)
            except:
                print "Failed to delete directory " + directory
Ejemplo n.º 25
0
 def enter_batch_mode(self, workspace):
     '''Restore the image set list from its setting as we go into batch mode'''
     pipeline = workspace.pipeline
     assert isinstance(pipeline, cpp.Pipeline)
     assert not self.distributed_mode, "Distributed mode no longer supported"
     default_output_directory = self.custom_output_directory.value
     default_image_directory = self.default_image_directory.value
     if os.path.isdir(default_output_directory):
         cpprefs.set_default_output_directory(default_output_directory)
     else:
         logger.info(
                 "Batch file default output directory, \"%s\", does not exist" %
                 default_output_directory)
     if os.path.isdir(default_image_directory):
         cpprefs.set_default_image_directory(default_image_directory)
     else:
         logger.info(
                 "Batch file default input directory \"%s\", does not exist" %
                 default_image_directory)
Ejemplo n.º 26
0
 def enter_batch_mode(self, workspace):
     '''Restore the image set list from its setting as we go into batch mode'''
     pipeline = workspace.pipeline
     assert isinstance(pipeline, cpp.Pipeline)
     assert not self.distributed_mode, "Distributed mode no longer supported"
     default_output_directory = self.custom_output_directory.value
     default_image_directory = self.default_image_directory.value
     if os.path.isdir(default_output_directory):
         cpprefs.set_default_output_directory(default_output_directory)
     else:
         logger.info(
             "Batch file default output directory, \"%s\", does not exist" %
             default_output_directory)
     if os.path.isdir(default_image_directory):
         cpprefs.set_default_image_directory(default_image_directory)
     else:
         logger.info(
             "Batch file default input directory \"%s\", does not exist" %
             default_image_directory)
Ejemplo n.º 27
0
 def check_preferences(self):
     """Return True if preferences are OK (e.g. directories exist)"""
     path = self.__image_edit_box.Value
     if not os.path.isdir(path):
         if (
             wx.MessageBox(
                 (
                     'The Default Input Folder is "%s", but '
                     "the directory does not exist. Do you want to "
                     "create it?"
                 )
                 % path,
                 "Warning, cannot run pipeline",
                 style=wx.YES_NO,
             )
             == wx.NO
         ):
             return False, "Image directory does not exist"
         os.makedirs(path)
         cpprefs.set_default_image_directory(path)
     path = self.__output_edit_box.Value
     if not os.path.isdir(path):
         if (
             wx.MessageBox(
                 (
                     'The Default Output Folder is "%s", but '
                     "the directory does not exist. Do you want to "
                     "create it?"
                 )
                 % path,
                 "Warning, cannot run pipeline",
                 style=wx.YES_NO,
             )
             == wx.NO
         ):
             return False, "Output directory does not exist"
         os.makedirs(path)
         cpprefs.set_default_output_directory(path)
     return True, "OK"
Ejemplo n.º 28
0
 def check_preferences(self):
     '''Return True if preferences are OK (e.g. directories exist)'''
     path = self.__image_edit_box.Value
     if not os.path.isdir(path):
         if wx.MessageBox(('The Default Input Folder is "%s", but '
                           'the directory does not exist. Do you want to '
                           'create it?') % path,
                          "Warning, cannot run pipeline",
                          style=wx.YES_NO) == wx.NO:
             return False, "Image directory does not exist"
         os.makedirs(path)
         cpprefs.set_default_image_directory(path)
     path = self.__output_edit_box.Value
     if not os.path.isdir(path):
         if wx.MessageBox(('The Default Output Folder is "%s", but '
                           'the directory does not exist. Do you want to '
                           'create it?') % path,
                          "Warning, cannot run pipeline",
                          style=wx.YES_NO) == wx.NO:
             return False, "Output directory does not exist"
         os.makedirs(path)
         cpprefs.set_default_output_directory(path)
     return True, "OK"
Ejemplo n.º 29
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)

    options, args = parse_args(args)
    set_log_level(options)

    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.run_ilastik:
        run_ilastik()
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(
            options.overwrite_external_dependencies)

    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return

    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return

    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None
                              and options.workspace_filename is None
                              and (not options.new_workspace)
                              and options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0,
                check_for_new_version=(options.pipeline_filename is None),
                show_splashbox=show_splashbox,
                workspace_path=workspace_path)

        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup
            options.run_pipeline = True

        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"'
                        % options.pipeline_filename,
                        "Error loading pipeline",
                        style=wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline",
                                       exc_info=True)
            App.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        raise
Ejemplo n.º 30
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i+1:
                cpprefs.set_ij_plugin_directory(args[i+1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    if options.jvm_heap_size != None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]
        
        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                filename = path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
            ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            from cellprofiler.workspace import is_workspace_file
            show_splashbox = (options.pipeline_filename is None and
                              (not options.new_project) and
                              options.show_splashbox)
            
            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path,
                pipeline_path = pipeline_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            if not os.path.exists(options.temp_dir):
                os.makedirs(options.temp_dir)
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            if options.run_pipeline:
                App.frame.pipeline_controller.do_analyze_images()
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise
Ejemplo n.º 31
0
def main(args):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if any([arg.startswith('--work-announce') for arg in args]):
        #
        # Go headless ASAP
        #
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.main()
        sys.exit(0)
        
    options, args = parse_args(args)
    set_log_level(options)
    
    if not hasattr(sys, "frozen") and options.code_statistics:
        print_code_statistics()
        return
    
    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return
    
    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return
        
    if options.run_ilastik:
        run_ilastik()
        return
    
    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')
    
    if (not hasattr(sys, 'frozen')) and options.fetch_external_dependencies:
        import external_dependencies
        external_dependencies.fetch_external_dependencies(options.overwrite_external_dependencies)
    
    if (not hasattr(sys, 'frozen')) and options.build_extensions:
        build_extensions()
        if options.build_and_exit:
            return
    
    if options.output_html:
        from cellprofiler.gui.html.manual import generate_html
        webpage_path = options.output_directory if options.output_directory else None
        generate_html(webpage_path)
        return
    if options.print_measurements:
        print_measurements(options)
        return
    
    try:
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.cellprofilerapp import CellProfilerApp
            show_splashbox = (options.pipeline_filename is None and
                              options.workspace_filename is None and
                              (not options.new_workspace) and
                              options.show_splashbox)
            if options.workspace_filename:
                workspace_path = os.path.expanduser(options.workspace_filename)
            elif options.new_workspace:
                workspace_path = False
            else:
                workspace_path = None
            App = CellProfilerApp(
                0, 
                check_for_new_version = (options.pipeline_filename is None),
                show_splashbox = show_splashbox,
                workspace_path = workspace_path)
    
        #
        # Important to go headless ASAP
        #
        # cellprofiler.preferences can't be imported before we have a chance
        # to initialize the wx app.
        #
        import cellprofiler.preferences as cpprefs
        if not options.show_gui:
            cpprefs.set_headless()
            # What's there to do but run if you're running headless?
            # Might want to change later if there's some headless setup 
            options.run_pipeline = True
    
            
        if options.plugins_directory is not None:
            cpprefs.set_plugin_directory(options.plugins_directory)
        if options.ij_plugins_directory is not None:
            cpprefs.set_ij_plugin_directory(options.ij_plugins_directory)
        if options.temp_dir is not None:
            cpprefs.set_temporary_directory(options.temp_dir)
        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))
        if options.image_set_file is not None:
            cpprefs.set_image_set_file(options.image_set_file, False)
            
        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))
    
        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")
    
        if options.output_directory:
            cpprefs.set_default_output_directory(options.output_directory)
        
        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)
    
        if options.show_gui:
            import cellprofiler.gui.cpframe as cpgframe
            if options.pipeline_filename:
                pipeline_path = os.path.expanduser(options.pipeline_filename)
                try:
                    App.frame.pipeline.load(pipeline_path)
                    if options.run_pipeline:
                        App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
                except:
                    import wx
                    wx.MessageBox(
                        'CellProfiler was unable to load the pipeline file, "%s"' %
                        options.pipeline_filename, "Error loading pipeline",
                        style = wx.OK | wx.ICON_ERROR)
                    logging.root.error("Unable to load pipeline", exc_info=True)
            App.MainLoop()
            return
        
        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        raise
Ejemplo n.º 32
0
 def test_03_05_the_happy_path_chapter_1(self):
     #
     # Run the worker clear through to the end
     # for the first imageset
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [1],
                          worker_runs_post_group = False,
                          wants_dictionary = True))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(DISPLAY_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline()
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
     finally:
         m.close()
     #
     # Next, the worker asks for the shared dictionary
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.SharedDictionaryRequest)
     shared_dictionaries = [{ ("foo%d" % i):"bar%d" % i} for i in range(1,8)]
     rep = cpanalysis.SharedDictionaryReply(
         dictionaries = shared_dictionaries)
     req.reply(rep)
     #
     # The worker sends a display request for FlipAndRotate
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.DisplayRequest)
     self.assertEqual(req.image_set_number, 1)
     d = req.display_data_dict
     # Possibly, this will break if someone edits FlipAndRotate. Sorry.
     self.assertItemsEqual(d.keys(), 
                           ['vmax', 'output_image_pixel_data', 
                            'image_pixel_data', 'vmin'])
     self.assertIsInstance(d['output_image_pixel_data'], np.ndarray)
     req.reply(cpanalysis.Ack())
     #
     # The worker sends ImageSetSuccessWithDictionary.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.ImageSetSuccessWithDictionary)
     self.assertEqual(req.image_set_number, 1)
     for expected, actual in zip(shared_dictionaries, req.shared_dicts):
         self.assertDictEqual(expected, actual)
     req.reply(cpanalysis.Ack())
     #
     # The worker sends the measurement report
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.MeasurementsReport)
     self.assertSequenceEqual(req.image_set_numbers, [1])
     m = cpmeas.load_measurements_from_buffer(req.buf)
     #
     # Spot check for some expected stuff
     #
     self.assertTrue(m.has_feature(cpmeas.IMAGE, C_COUNT+"_Nuclei"))
     self.assertTrue(m.has_feature("Nuclei", M_LOCATION_CENTER_X))
     self.assertTrue(m.has_feature("Nuclei", "AreaShape_Area"))
     req.reply(cpanalysis.Ack())
     self.awthread.ecute()
Ejemplo n.º 33
0
 def test_03_06_the_happy_path_chapter_2(self):
     #
     # Give the worker image sets # 2 and 3 and tell it to run post_group
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [2, 3],
                          worker_runs_post_group = True,
                          wants_dictionary = False))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(DISPLAY_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline(nimages=3)
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
     finally:
         m.close()
     #
     # In group mode, the worker issues a display request and constructs
     # its own dictonaries
     #
     for image_number in (2, 3):
         #
         # The worker sends a display request for FlipAndRotate
         #
         req = self.awthread.recv(self.work_socket)
         self.assertIsInstance(req, cpanalysis.DisplayRequest)
         req.reply(cpanalysis.Ack())
         #
         # The worker sends ImageSetSuccess.
         #
         req = self.awthread.recv(self.work_socket)
         self.assertIsInstance(req, cpanalysis.ImageSetSuccess)
         self.assertEqual(req.image_set_number, image_number)
         req.reply(cpanalysis.Ack())
     #
     # The worker sends a DisplayPostGroup request for FlipAndRotate
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.DisplayPostGroupRequest)
     self.assertEqual(req.image_set_number, 3)
     req.reply(cpanalysis.Ack())
     #
     # The worker sends a measurement report for image sets 2 and 3
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.MeasurementsReport)
     self.assertSequenceEqual(req.image_set_numbers, [2, 3])
     m = cpmeas.load_measurements_from_buffer(req.buf)
     #
     # Spot check for some expected stuff
     #
     self.assertTrue(m.has_feature(cpmeas.IMAGE, C_COUNT+"_Nuclei"))
     self.assertTrue(m.has_feature("Nuclei", M_LOCATION_CENTER_X))
     self.assertTrue(m.has_feature("Nuclei", "AreaShape_Area"))
     req.reply(cpanalysis.Ack())
     self.awthread.ecute()
Ejemplo n.º 34
0
 def test_03_07_a_sad_ending(self):
     #
     # Run using the bad pipeline and lead the analysis worker
     # through debug post-mortem
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [2],
                          worker_runs_post_group = False,
                          wants_dictionary = False))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(BAD_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline(nimages=2)
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
     finally:
         m.close()
     #
     # Next, the worker asks for the shared dictionary
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.SharedDictionaryRequest)
     shared_dictionaries = [{ ("foo%d" % i):"bar%d" % i} for i in range(1,8)]
     rep = cpanalysis.SharedDictionaryReply(
         dictionaries = shared_dictionaries)
     req.reply(rep)
     #
     # The worker should choke somewhere in NamesAndTypes
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.ExceptionReport)
     password = "******"
     req.reply(cpanalysis.ExceptionPleaseDebugReply(
         disposition='DEBUG', 
         verification_hash = hashlib.sha1(password).hexdigest()))
     #
     # Next, the worker sends DebugWaiting once it binds to a port
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.DebugWaiting)
     handle = None
     telnet_socket = socket.socket(socket.AF_INET, 
                                   proto = socket.IPPROTO_TCP)
     telnet_socket.connect(("127.0.0.1", req.port))
     try:
         req.reply(cpanalysis.Ack())
         #
         # Should say, "Verification: ", but it could change...
         #
         prompt = telnet_socket.recv(20)
         telnet_socket.send("corned beef\n")
         pdb = telnet_socket.recv(4000)
         telnet_socket.send("exit\n")
     finally:
         telnet_socket.close()
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.DebugComplete)
     req.reply(cpanalysis.ExceptionPleaseDebugReply(disposition = ED_STOP))
Ejemplo n.º 35
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.set_awt_headless(True)
    exit_code = 0
    switches = ('--work-announce', '--knime-bridge-address')
    if any([any([arg.startswith(switch) for switch in switches])
            for arg in args]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.analysis_worker
        cellprofiler.analysis_worker.aw_parse_args()
        cellprofiler.analysis_worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.run_ilastik:
        run_ilastik()
        return

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write("CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n")
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write("    <message> - the message displayed inside the message box\n")
            sys.stderr.write("    <pipeline-or-project> - the path to the pipeline or project file to modify\n")
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurements as cpmeas
            m = cpmeas.Measurements(
                    filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory,
                                     globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0, workspace_path=workspace_path, pipeline_path=pipeline_path)

        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" % (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py", exc_info=True)
        exit_code = -1
Ejemplo n.º 36
0
 def test_03_08_a_sad_moment(self):
     #
     # Run using the good pipeline, but change one of the URLs so
     # an exception is thrown.
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [2, 3],
                          worker_runs_post_group = False,
                          wants_dictionary = False))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(GOOD_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline(nimages=3)
     m[cpmeas.IMAGE, M_IMAGE_SET, 2] = np.zeros(100, np.uint8)
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
     finally:
         m.close()
     #
     # Next, the worker asks for the shared dictionary
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.SharedDictionaryRequest)
     shared_dictionaries = [{ ("foo%d" % i):"bar%d" % i} for i in range(1,8)]
     rep = cpanalysis.SharedDictionaryReply(
         dictionaries = shared_dictionaries)
     req.reply(rep)
     #
     # The worker should choke somewhere in NamesAndTypes, but we
     # tell the worker to skip the rest of the imageset.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.ExceptionReport)
     req.reply(cpanalysis.ExceptionPleaseDebugReply(disposition = ED_SKIP))
     #
     # The worker should send ImageSetSuccess for image set 2 anyway.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.ImageSetSuccess)
     self.assertEqual(req.image_set_number, 2)
     req.reply(cpanalysis.Ack())
     #
     # And then it tells us about image set 3
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.ImageSetSuccess)
     self.assertEqual(req.image_set_number, 3)
     req.reply(cpanalysis.Ack())
     #
     # The worker should then report the measurements for both 2 and 3
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.MeasurementsReport)
     self.assertSequenceEqual(req.image_set_numbers, [2, 3])
     m = cpmeas.load_measurements_from_buffer(req.buf)
     #
     # Spot check for some expected stuff
     #
     self.assertTrue(m.has_feature(cpmeas.IMAGE, C_COUNT+"_Nuclei"))
     self.assertTrue(m.has_feature("Nuclei", M_LOCATION_CENTER_X))
     self.assertTrue(m.has_feature("Nuclei", "AreaShape_Area"))
     #
     # The count for the skipped image should be None
     #
     count = m[cpmeas.IMAGE, C_COUNT + "_Nuclei", 2]
     self.assertIsNone(count)
     count = m[cpmeas.IMAGE, C_COUNT + "_Nuclei", 3]
     center_x = m["Nuclei", M_LOCATION_CENTER_X, 3]
     self.assertEqual(count, len(center_x))
     req.reply(cpanalysis.Ack())
     self.awthread.ecute()
Ejemplo n.º 37
0
        if not options.last_image_set.isdigit():
            raise ValueError("The --last-image-set option takes a numeric argument")
        else:
            image_set_end = int(options.last_image_set)
            if image_set_start is None:
                image_set_numbers = np.arange(1, image_set_end+1)
            else:
                image_set_numbers = np.arange(image_set_start, image_set_end+1)
    else:
        image_set_end = None
    
    if options.output_directory:
        cpprefs.set_default_output_directory(options.output_directory)
    
    if options.image_directory:
        cpprefs.set_default_image_directory(options.image_directory)

    if options.show_gui:
        import cellprofiler.gui.cpframe as cpgframe
        if options.pipeline_filename:
            try:
                App.frame.pipeline.load(os.path.expanduser(options.pipeline_filename))
                if options.run_pipeline:
                    App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
            except:
                import wx
                wx.MessageBox(
                    'CellProfiler was unable to load the pipeline file, "%s"' %
                    options.pipeline_filename, "Error loading pipeline",
                    style = wx.OK | wx.ICON_ERROR)
                logging.root.error("Unable to load pipeline", exc_info=True)
Ejemplo n.º 38
0
    def test_03_09_flag_image_abort(self):
        #
        # Regression test of issue #1210
        # Make a pipeline that aborts during FlagImage
        #
        data = r"""CellProfiler Pipeline: http://www.cellprofiler.org
Version:3
DateRevision:20140918122611
GitHash:ded6939
ModuleCount:6
HasImagePlaneDetails:False

Images:[module_num:1|svn_version:\'Unknown\'|variable_revision_number:2|show_window:False|notes:\x5B\'To begin creating your project, use the Images module to compile a list of files and/or folders that you want to analyze. You can also specify a set of rules to include only the desired files in your selected folders.\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    :
    Filter images?:Images only
    Select the rule criteria:and (extension does isimage) (directory doesnot containregexp "\x5B\\\\\\\\\\\\\\\\/\x5D\\\\\\\\.")

Metadata:[module_num:2|svn_version:\'Unknown\'|variable_revision_number:4|show_window:False|notes:\x5B\'The Metadata module optionally allows you to extract information describing your images (i.e, metadata) which will be stored along with your measurements. This information can be contained in the file name and/or location, or in an external file.\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    Extract metadata?:No
    Metadata data type:Text
    Metadata types:{}
    Extraction method count:1
    Metadata extraction method:Extract from file/folder names
    Metadata source:File name
    Regular expression:^(?P<Plate>.*)_(?P<Well>\x5BA-P\x5D\x5B0-9\x5D{2})_s(?P<Site>\x5B0-9\x5D)_w(?P<ChannelNumber>\x5B0-9\x5D)
    Regular expression:(?P<Date>\x5B0-9\x5D{4}_\x5B0-9\x5D{2}_\x5B0-9\x5D{2})$
    Extract metadata from:All images
    Select the filtering criteria:and (file does contain "")
    Metadata file location:
    Match file and image metadata:\x5B\x5D
    Use case insensitive matching?:No

NamesAndTypes:[module_num:3|svn_version:\'Unknown\'|variable_revision_number:5|show_window:False|notes:\x5B\'The NamesAndTypes module allows you to assign a meaningful name to each image by which other modules will refer to it.\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    Assign a name to:All images
    Select the image type:Grayscale image
    Name to assign these images:DNA
    Match metadata:\x5B\x5D
    Image set matching method:Order
    Set intensity range from:Image metadata
    Assignments count:1
    Single images count:0
    Select the rule criteria:and (file does contain "")
    Name to assign these images:DNA
    Name to assign these objects:Cell
    Select the image type:Grayscale image
    Set intensity range from:Image metadata
    Retain outlines of loaded objects?:No
    Name the outline image:LoadedOutlines

Groups:[module_num:4|svn_version:\'Unknown\'|variable_revision_number:2|show_window:False|notes:\x5B\'The Groups module optionally allows you to split your list of images into image subsets (groups) which will be processed independently of each other. Examples of groupings include screening batches, microtiter plates, time-lapse movies, etc.\'\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    Do you want to group your images?:No
    grouping metadata count:1
    Metadata category:None

FlagImage:[module_num:5|svn_version:\'Unknown\'|variable_revision_number:4|show_window:False|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    Hidden:1
    Hidden:1
    Name the flag\'s category:Metadata
    Name the flag:QCFlag
    Flag if any, or all, measurement(s) fails to meet the criteria?:Flag if any fail
    Skip image set if flagged?:Yes
    Flag is based on:Whole-image measurement
    Select the object to be used for flagging:None
    Which measurement?:Height_DNA
    Flag images based on low values?:No
    Minimum value:0.0
    Flag images based on high values?:Yes
    Maximum value:1.0
    Rules file location:Elsewhere...\x7C
    Rules file name:rules.txt
    Class number:

MeasureImageIntensity:[module_num:6|svn_version:\'Unknown\'|variable_revision_number:2|show_window:True|notes:\x5B\x5D|batch_state:array(\x5B\x5D, dtype=uint8)|enabled:True|wants_pause:False]
    Select the image to measure:DNA
    Measure the intensity only from areas enclosed by objects?:No
    Select the input objects:None
"""
        self.awthread = self.AWThread(self.announce_addr)
        self.awthread.start()
        self.set_work_socket()
        self.awthread.ex(self.awthread.aw.do_job, 
                         cpanalysis.WorkReply(
                             image_set_numbers = [1],
                             worker_runs_post_group = False,
                             wants_dictionary = True))
        #
        # The worker should ask for the pipeline and preferences next.
        #
        req = self.awthread.recv(self.work_socket)
        self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
        self.assertEqual(req.analysis_id, self.analysis_id)
        
        input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
        cpprefs.set_default_image_directory(input_dir)
        preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                       cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
        
        rep = cpanalysis.Reply(
            pipeline_blob = np.array(data),
            preferences = preferences)
        req.reply(rep)
        #
        # The worker asks for the initial measurements.
        #
        req = self.awthread.recv(self.work_socket)
        self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
        self.assertEqual(req.analysis_id, self.analysis_id)
        m = get_measurements_for_good_pipeline()
        pipeline = cpp.Pipeline()
        pipeline.loadtxt(StringIO(data))
        pipeline.write_pipeline_measurement(m)
        
        try:
            req.reply(cpanalysis.Reply(buf = m.file_contents()))
        finally:
            m.close()
        #
        # Next, the worker asks for the shared dictionary
        #
        req = self.awthread.recv(self.work_socket)
        self.assertIsInstance(req, cpanalysis.SharedDictionaryRequest)
        shared_dictionaries = [{ ("foo%d" % i):"bar%d" % i} for i in range(1,7)]
        rep = cpanalysis.SharedDictionaryReply(
            dictionaries = shared_dictionaries)
        req.reply(rep)
        #
        # MeasureImageIntensity follows FlagImage and it is poised to ask
        # for a display. So if we get that, we know the module has been run
        # and we fail the test.
        #
        req = self.awthread.recv(self.work_socket)
        self.assertFalse(isinstance(req, cpanalysis.DisplayRequest))
        self.assertFalse(isinstance(req, cpanalysis.ExceptionReport))
Ejemplo n.º 39
0
        image_set_start = None

    if not options.last_image_set is None:
        if not options.last_image_set.isdigit():
            raise ValueError(
                "The --last-image-set option takes a numeric argument")
        else:
            image_set_end = int(options.last_image_set)
    else:
        image_set_end = None

    if options.output_directory:
        cpprefs.set_default_output_directory(options.output_directory)

    if options.image_directory:
        cpprefs.set_default_image_directory(options.image_directory)

    if options.show_gui:
        import cellprofiler.gui.cpframe as cpgframe
        if options.pipeline_filename:
            try:
                App.frame.pipeline.load(
                    os.path.expanduser(options.pipeline_filename))
                if options.run_pipeline:
                    App.frame.Command(cpgframe.ID_FILE_ANALYZE_IMAGES)
            except:
                import wx
                wx.MessageBox(
                    'CellProfiler was unable to load the pipeline file, "%s"' %
                    options.pipeline_filename,
                    "Error loading pipeline",
Ejemplo n.º 40
0
def main(args=None):
    '''Run CellProfiler

    args - command-line arguments, e.g. sys.argv
    '''
    if args is None:
        args = sys.argv
    import cellprofiler.preferences as cpprefs
    cpprefs.set_awt_headless(True)
    exit_code = 0
    switches = ('--work-announce', '--knime-bridge-address')
    if any(
        [any([arg.startswith(switch) for switch in switches])
         for arg in args]):
        #
        # Go headless ASAP
        #
        cpprefs.set_headless()
        for i, arg in enumerate(args):
            if arg == "--ij-plugins-directory" and len(args) > i + 1:
                cpprefs.set_ij_plugin_directory(args[i + 1])
                break
        import cellprofiler.worker
        cellprofiler.worker.aw_parse_args()
        cellprofiler.worker.main()
        sys.exit(exit_code)

    options, args = parse_args(args)
    if options.print_version:
        from cellprofiler.utilities.version import \
            dotted_version, version_string, git_hash, version_number
        print "CellProfiler %s" % dotted_version
        print "Git %s" % git_hash
        print "Version %s" % version_number
        print "Built %s" % version_string.split(" ")[0]
        sys.exit(exit_code)
    #
    # Important to go headless ASAP
    #
    if (not options.show_gui) or options.write_schema_and_exit:
        import cellprofiler.preferences as cpprefs
        cpprefs.set_headless()
        # What's there to do but run if you're running headless?
        # Might want to change later if there's some headless setup
        options.run_pipeline = True

    if options.jvm_heap_size is not None:
        from cellprofiler.preferences import set_jvm_heap_mb
        set_jvm_heap_mb(options.jvm_heap_size, False)
    set_log_level(options)

    if options.print_groups_file is not None:
        print_groups(options.print_groups_file)
        return

    if options.batch_commands_file is not None:
        get_batch_commands(options.batch_commands_file)
        return

    if options.add_message_for_user:
        if len(args) != 3:
            sys.stderr.write("Usage: (for add_message-for-user)\n")
            sys.stderr.write(
                "CellProfiler --add-message-for-user <caption> <message> <pipeline-or-project>\n"
            )
            sys.stderr.write("where:\n")
            sys.stderr.write("    <caption> - the message box caption\n")
            sys.stderr.write(
                "    <message> - the message displayed inside the message box\n"
            )
            sys.stderr.write(
                "    <pipeline-or-project> - the path to the pipeline or project file to modify\n"
            )
            return
        caption = args[0]
        message = args[1]
        path = args[2]

        import h5py
        using_hdf5 = h5py.is_hdf5(path)
        if using_hdf5:
            import cellprofiler.measurement as cpmeas
            m = cpmeas.Measurements(filename=path, mode="r+")
            pipeline_text = m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"]
        else:
            with open(path, "r") as fd:
                pipeline_text = fd.read()
        header, body = pipeline_text.split("\n\n", 1)
        pipeline_text = header + \
                        ("\nMessageForUser:%s|%s\n\n" % (caption, message)) + body
        if using_hdf5:
            m[cpmeas.EXPERIMENT, "Pipeline_Pipeline"] = pipeline_text
            m.close()
        else:
            with open(path, "w") as fd:
                fd.write(pipeline_text)
        print "Message added to %s" % path
        return

    # necessary to prevent matplotlib trying to use Tkinter as its backend.
    # has to be done before CellProfilerApp is imported
    from matplotlib import use as mpluse
    mpluse('WXAgg')

    if options.omero_credentials is not None:
        set_omero_credentials_from_string(options.omero_credentials)
    if options.plugins_directory is not None:
        cpprefs.set_plugin_directory(options.plugins_directory, globally=False)
    if options.ij_plugins_directory is not None:
        cpprefs.set_ij_plugin_directory(options.ij_plugins_directory,
                                        globally=False)
    if options.temp_dir is not None:
        if not os.path.exists(options.temp_dir):
            os.makedirs(options.temp_dir)
        cpprefs.set_temporary_directory(options.temp_dir, globally=False)
    if not options.allow_schema_write:
        cpprefs.set_allow_schema_write(False)
    #
    # After the crucial preferences are established, we can start the VM
    #
    from cellprofiler.utilities.cpjvm import cp_start_vm
    cp_start_vm()
    #
    # Not so crucial preferences...
    #
    if options.image_set_file is not None:
        cpprefs.set_image_set_file(options.image_set_file)
    try:
        # ---------------------------------------
        #
        # Handle command-line tasks that that need to load the modules to run
        #
        if options.output_html:
            from cellprofiler.gui.html.manual import generate_html
            webpage_path = options.output_directory if options.output_directory else None
            generate_html(webpage_path)
            return
        if options.print_measurements:
            print_measurements(options)
            return
        if not hasattr(sys, "frozen") and options.code_statistics:
            print_code_statistics()
            return
        if options.write_schema_and_exit:
            write_schema(options.pipeline_filename)
            return
        #
        # ------------------------------------------
        if options.show_gui:
            import wx
            wx.Log.EnableLogging(False)
            from cellprofiler.gui.app import App
            from cellprofiler.workspace import is_workspace_file

            if options.pipeline_filename:
                if is_workspace_file(options.pipeline_filename):
                    workspace_path = os.path.expanduser(
                        options.pipeline_filename)
                    pipeline_path = None
                else:
                    pipeline_path = os.path.expanduser(
                        options.pipeline_filename)
                    workspace_path = None
            elif options.new_project:
                workspace_path = False
                pipeline_path = None
            else:
                workspace_path = None
                pipeline_path = None

            app = App(0,
                      workspace_path=workspace_path,
                      pipeline_path=pipeline_path)

        if options.data_file is not None:
            cpprefs.set_data_file(os.path.abspath(options.data_file))

        from cellprofiler.utilities.version import version_string, version_number
        logging.root.info("Version: %s / %d" %
                          (version_string, version_number))

        if options.run_pipeline and not options.pipeline_filename:
            raise ValueError("You must specify a pipeline filename to run")

        if options.output_directory:
            if not os.path.exists(options.output_directory):
                os.makedirs(options.output_directory)
            cpprefs.set_default_output_directory(options.output_directory)

        if options.image_directory:
            cpprefs.set_default_image_directory(options.image_directory)

        if options.show_gui:
            if options.run_pipeline:
                app.frame.pipeline_controller.do_analyze_images()
            app.MainLoop()
            return

        elif options.run_pipeline:
            run_pipeline_headless(options, args)
    except Exception, e:
        logging.root.fatal("Uncaught exception in CellProfiler.py",
                           exc_info=True)
        exit_code = -1
Ejemplo n.º 41
0
 def test_03_04_shared_dictionary_request(self):
     #
     # The SharedDictionaryRequest
     #
     self.awthread = self.AWThread(self.announce_addr)
     self.awthread.start()
     self.set_work_socket()
     self.awthread.ex(self.awthread.aw.do_job, 
                      cpanalysis.WorkReply(
                          image_set_numbers = [1],
                          worker_runs_post_group = False,
                          wants_dictionary = True))
     #
     # The worker should ask for the pipeline and preferences next.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.PipelinePreferencesRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     
     input_dir = os.path.join(example_images_directory(), "ExampleSBSImages")
     cpprefs.set_default_image_directory(input_dir)
     preferences = {cpprefs.DEFAULT_IMAGE_DIRECTORY: 
                    cpprefs.config_read(cpprefs.DEFAULT_IMAGE_DIRECTORY) }
     
     rep = cpanalysis.Reply(
         pipeline_blob = np.array(DISPLAY_PIPELINE),
         preferences = preferences)
     req.reply(rep)
     #
     # The worker asks for the initial measurements.
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.InitialMeasurementsRequest)
     self.assertEqual(req.analysis_id, self.analysis_id)
     m = get_measurements_for_good_pipeline()
     try:
         req.reply(cpanalysis.Reply(buf = m.file_contents()))
     finally:
         m.close()
     #
     # Next, the worker asks for the shared dictionary
     #
     req = self.awthread.recv(self.work_socket)
     self.assertIsInstance(req, cpanalysis.SharedDictionaryRequest)
     rep = cpanalysis.SharedDictionaryReply(
         dictionaries = [{ ("foo%d" % i):"bar%d" % i} for i in range(1,8)])
     req.reply(rep)
     #
     # Sneaky way to get pipeline. First, synchronize with the next message
     #
     req = self.awthread.recv(self.work_socket)
     pipe, prefs = self.awthread.aw.pipelines_and_preferences[
         self.analysis_id]
     for d, module in zip(rep.dictionaries, pipe.modules()):
         self.assertDictEqual(module.get_dictionary(), d)
     #
     # Might as well torpedo the app. It should be stalled waiting
     # for the FlipAndRotate display.
     #
     self.cancel()
     self.awthread.ecute()