def test_10_01_scaling(self):
        '''Test loading an image scaled and unscaled'''
        folder = "loaddata"
        file_name = "1-162hrh2ax2.tif"
        path = tests.modules.make_12_bit_image(folder, file_name, (22, 18))
        csv_text = ("Image_PathName_MyFile,Image_FileName_MyFile\n"
                    "%s,%s\n" % os.path.split(path))
        c0_image = []
        for rescale in (False, True):
            pipeline, module, filename = self.make_pipeline(csv_text)
            try:
                module.rescale.value = rescale

                def callback(workspace):
                    imgset = workspace.image_set
                    image = imgset.get_image("MyFile")
                    pixels = image.pixel_data
                    c0_image.append(pixels.copy())

                c0 = C0()
                c0.callback = callback
                c0.module_num = 2
                pipeline.add_module(c0)
                pipeline.run()
            finally:
                os.remove(filename)
        unscaled, scaled = c0_image
        numpy.testing.assert_almost_equal(unscaled * 65535. / 4095., scaled)
Beispiel #2
0
def test_dont_load_file():
    csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
        test_filename,
        test_path,
    )
    pipeline, module, filename = make_pipeline(csv_text)
    c0_ran = [False]

    def callback(workspace):
        imgset = workspace.image_set
        assert len(imgset.names) == 0
        c0_ran[0] = True

    c0 = C0()
    c0.callback = callback
    c0.set_module_num(1)
    pipeline.add_module(c0)
    try:
        module.wants_images.value = False
        pipeline.run()
        assert c0_ran[0]
    finally:
        os.remove(filename)
    def test_04_02_dont_load_file(self):
        csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
            self.test_filename,
            self.test_path,
        )
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            self.assertEqual(len(imgset.names), 0)
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 1
        pipeline.add_module(c0)
        try:
            module.wants_images.value = False
            pipeline.run()
            self.assertTrue(c0_ran[0])
        finally:
            os.remove(filename)
    def test_10_01_scaling(self):
        """Test loading an image scaled and unscaled"""
        folder = "loaddata"
        file_name = "1-162hrh2ax2.tif"
        path = tests.modules.make_12_bit_image(folder, file_name, (22, 18))
        csv_text = (
            "Image_PathName_MyFile,Image_FileName_MyFile\n"
            "%s,%s\n" % os.path.split(path)
        )
        c0_image = []
        for rescale in (False, True):
            pipeline, module, filename = self.make_pipeline(csv_text)
            try:
                module.rescale.value = rescale

                def callback(workspace):
                    imgset = workspace.image_set
                    image = imgset.get_image("MyFile")
                    pixels = image.pixel_data
                    c0_image.append(pixels.copy())

                c0 = C0()
                c0.callback = callback
                c0.module_num = 2
                pipeline.add_module(c0)
                pipeline.run()
            finally:
                os.remove(filename)
        unscaled, scaled = c0_image
        numpy.testing.assert_almost_equal(unscaled * 65535.0 / 4095.0, scaled)
    def test_03_03_metadata_row_and_column_and_well(self):
        csv_text = '''"Metadata_Row","Metadata_Column","Metadata_Well"
"C","03","B14"
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        columns = module.get_measurement_columns(pipeline)
        self.assertTrue(
            any([
                c[0] == cellprofiler.measurement.IMAGE
                and c[1] == "Metadata_Row" and c[2] == "varchar(1)"
                for c in columns
            ]))
        self.assertTrue(
            any([
                c[0] == cellprofiler.measurement.IMAGE
                and c[1] == "Metadata_Column" and c[2] == "varchar(2)"
                for c in columns
            ]))
        self.assertTrue(
            any([
                c[0] == cellprofiler.measurement.IMAGE
                and c[1] == "Metadata_Well" and c[2] == "varchar(3)"
                for c in columns
            ]))
        m = pipeline.run()
        features = module.get_measurements(pipeline,
                                           cellprofiler.measurement.IMAGE,
                                           cellprofiler.measurement.C_METADATA)
        for feature, expected in (("Row", "C"), ("Column", "03"), ("Well",
                                                                   "B14")):
            self.assertTrue(feature in features)
            value = m.get_current_image_measurement('_'.join(
                (cellprofiler.measurement.C_METADATA, feature)))
            self.assertEqual(value, expected)
Beispiel #6
0
def test_metadata_row_and_column_and_well():
    csv_text = """"Metadata_Row","Metadata_Column","Metadata_Well"
"C","03","B14"
"""
    pipeline, module, filename = make_pipeline(csv_text)
    columns = module.get_measurement_columns(pipeline)
    assert any([
        c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Row"
        and c[2] == "varchar(1)" for c in columns
    ])
    assert any([
        c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Column"
        and c[2] == "varchar(2)" for c in columns
    ])
    assert any([
        c[0] == cellprofiler.measurement.IMAGE and c[1] == "Metadata_Well"
        and c[2] == "varchar(3)" for c in columns
    ])
    m = pipeline.run()
    features = module.get_measurements(pipeline,
                                       cellprofiler.measurement.IMAGE,
                                       cellprofiler.measurement.C_METADATA)
    for feature, expected in (("Row", "C"), ("Column", "03"), ("Well", "B14")):
        assert feature in features
        value = m.get_current_image_measurement("_".join(
            (cellprofiler.measurement.C_METADATA, feature)))
        assert value == expected
    def test_05_02_img_717(self):
        """Regression test of img-717, column without underbar"""
        csv_text = """"Image","Test_Measurement"
"foo",1
"foo",2
"foo",3
"foo",4
"foo",5
"foo",6
"foo",7
"foo",8
"foo",9
"foo",10
"""
        pipeline, module, filename = self.make_pipeline(csv_text)
        module.wants_rows.value = True
        module.row_range.min = 4
        module.row_range.max = 6
        m = pipeline.run()
        self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
        data = m.get_all_measurements(
            cellprofiler.measurement.IMAGE, "Test_Measurement"
        )
        self.assertTrue(numpy.all(data == numpy.arange(4, 7)))
        os.remove(filename)
    def test_04_01_load_file(self):
        csv_text = '''"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
''' % (self.test_filename, self.test_path)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DNA")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement('MD5Digest_DNA')
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue('PathName_DNA' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('PathName_DNA'),
                             self.test_path)
            self.assertTrue('FileName_DNA' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('FileName_DNA'),
                             self.test_filename)
        finally:
            os.remove(filename)
    def test_03_01_metadata(self):
        csv_text = '''"Metadata_Plate"
"P-12345"
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Metadata_Plate")
        self.assertEqual(data, "P-12345")
        os.remove(filename)
Beispiel #10
0
    def test_03_01_metadata(self):
        csv_text = """"Metadata_Plate"
"P-12345"
"""
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Metadata_Plate")
        self.assertEqual(data, "P-12345")
        os.remove(filename)
Beispiel #11
0
def test_metadata():
    csv_text = """"Metadata_Plate"
"P-12345"
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run()
    data = m.get_current_image_measurement("Metadata_Plate")
    assert data == "P-12345"
    os.remove(filename)
    def test_02_04_long_int_image_measurement(self):
        csv_text = '''"Test_Measurement"
1234567890123
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(isinstance(data, six.text_type), "Expected <type 'six.text_type'> got %s" % type(data))
        self.assertEqual(data, "1234567890123")
        os.remove(filename)
    def test_02_03_int_image_measurement(self):
        csv_text = '''"Test_Measurement"
1
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(isinstance(data, numpy.int), "data is type %s, not np.int" % (type(data)))
        self.assertEqual(data, 1)
        os.remove(filename)
    def test_02_02_float_image_measurement(self):
        csv_text = '''"Test_Measurement"
1.5
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(numpy.isreal(data))
        self.assertAlmostEqual(data, 1.5)
        os.remove(filename)
    def test_02_01_string_image_measurement(self):
        csv_text = '''"Test_Measurement"
"Hello, world"
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertFalse(numpy.isreal(data))
        self.assertEqual(data, "Hello, world")
        os.remove(filename)
Beispiel #16
0
def test_string_image_measurement():
    csv_text = """"Test_Measurement"
"Hello, world"
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run()
    data = m.get_current_image_measurement("Test_Measurement")
    assert not numpy.isreal(data)
    assert data == "Hello, world"
    os.remove(filename)
Beispiel #17
0
    def test_02_01_string_image_measurement(self):
        csv_text = """"Test_Measurement"
"Hello, world"
"""
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertFalse(numpy.isreal(data))
        self.assertEqual(data, "Hello, world")
        os.remove(filename)
Beispiel #18
0
def test_float_image_measurement():
    csv_text = """"Test_Measurement"
1.5
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run()
    data = m.get_current_image_measurement("Test_Measurement")
    assert numpy.isreal(data)
    assert round(abs(data - 1.5), 7) == 0
    os.remove(filename)
Beispiel #19
0
    def test_02_02_float_image_measurement(self):
        csv_text = """"Test_Measurement"
1.5
"""
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(numpy.isreal(data))
        self.assertAlmostEqual(data, 1.5)
        os.remove(filename)
Beispiel #20
0
def test_int_image_measurement():
    csv_text = """"Test_Measurement"
1
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run()
    data = m.get_current_image_measurement("Test_Measurement")
    assert isinstance(data,
                      numpy.int), "data is type %s, not np.int" % (type(data))
    assert data == 1
    os.remove(filename)
    def test_02_03_int_image_measurement(self):
        csv_text = '''"Test_Measurement"
1
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(isinstance(data, numpy.int),
                        "data is type %s, not np.int" % (type(data)))
        self.assertEqual(data, 1)
        os.remove(filename)
    def test_02_04_long_int_image_measurement(self):
        csv_text = '''"Test_Measurement"
1234567890123
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run()
        data = m.get_current_image_measurement("Test_Measurement")
        self.assertTrue(isinstance(data, six.text_type),
                        "Expected <type 'six.text_type'> got %s" % type(data))
        self.assertEqual(data, "1234567890123")
        os.remove(filename)
Beispiel #23
0
def test_long_int_image_measurement():
    csv_text = """"Test_Measurement"
1234567890123
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run()
    data = m.get_current_image_measurement("Test_Measurement")
    assert isinstance(
        data,
        six.text_type), "Expected <type 'six.text_type'> got %s" % type(data)
    assert data == "1234567890123"
    os.remove(filename)
    def test_04_02_dont_load_file(self):
        csv_text = '''"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
''' % (self.test_filename, self.test_path)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            self.assertEqual(len(imgset.names), 0)
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 1
        pipeline.add_module(c0)
        try:
            module.wants_images.value = False
            pipeline.run()
            self.assertTrue(c0_ran[0])
        finally:
            os.remove(filename)
Beispiel #25
0
    def test_09_01_load_bcb_file(self):

        csv_text = """ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\
MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\
MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\
CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\
CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES
"4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\
"2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\
"%s","C-4012-00-D80-001_Rev3","N01",\
"BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\
"Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1"
""" % (
            self.test_path,
            self.test_filename,
        )
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DAPI")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement("MD5Digest_DAPI")
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue(
                "PathName_DAPI" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("PathName_DAPI"), self.test_path
            )
            self.assertTrue(
                "FileName_DAPI" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("FileName_DAPI"), self.test_filename
            )
        finally:
            os.remove(filename)
    def test_06_01_alternate_image_start(self):
        csv_text = '''"Metadata_Measurement"
1
2
3
4
5
6
7
8
9
10
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run(image_set_start=2)
        data = m.get_all_measurements(cellprofiler.measurement.IMAGE, "Metadata_Measurement")
        self.assertTrue(all([data[i - 2] == i for i in range(2, 11)]))
        os.remove(filename)
    def test_06_01_alternate_image_start(self):
        csv_text = '''"Metadata_Measurement"
1
2
3
4
5
6
7
8
9
10
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        m = pipeline.run(image_set_start=2)
        data = m.get_all_measurements(cellprofiler.measurement.IMAGE,
                                      "Metadata_Measurement")
        self.assertTrue(all([data[i - 2] == i for i in range(2, 11)]))
        os.remove(filename)
Beispiel #28
0
def test_alternate_image_start():
    csv_text = """"Metadata_Measurement"
1
2
3
4
5
6
7
8
9
10
"""
    pipeline, module, filename = make_pipeline(csv_text)
    m = pipeline.run(image_set_start=2)
    data = m.get_all_measurements(cellprofiler.measurement.IMAGE,
                                  "Metadata_Measurement")
    assert all([data[i - 2] == i for i in range(2, 11)])
    os.remove(filename)
    def test_09_01_load_bcb_file(self):

        csv_text = '''ELN_RUN_ID,CBIP_RUN_ID,ASSAY_PLATE_BARCODE,\
MX_PLATE_ID,ASSAY_WELL_POSITION,ASSAY_WELL_ROLE,SITE_X,SITE_Y,\
MICROSCOPE,SOURCE_DESCRIPTION,DATE_CREATED,FILE_PATH,FILE_NAME,\
CPD_PLATE_MAP_NAME,CPD_WELL_POSITION,BROAD_ID,\
CPD_MMOL_CONC,SOURCE_NAME,SOURCE_COMPOUND_NAME,CPD_SMILES
"4012-10-W01-01-02","4254","BR00021547","20777","N01","COMPOUND",\
"2","2","GS IX Micro","DAPI","2010/03/19 06:01:12","%s",\
"%s","C-4012-00-D80-001_Rev3","N01",\
"BRD-K71194192-001-01-6","2.132352941","ChemBridge","",\
"Oc1ccnc(SCC(=O)Nc2ccc(Oc3ccccc3)cc2)n1"
''' % (self.test_path, self.test_filename)
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DAPI")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement('MD5Digest_DAPI')
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue('PathName_DAPI' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('PathName_DAPI'),
                             self.test_path)
            self.assertTrue('FileName_DAPI' in m.get_feature_names(cellprofiler.measurement.IMAGE))
            self.assertEqual(m.get_current_image_measurement('FileName_DAPI'),
                             self.test_filename)
        finally:
            os.remove(filename)
Beispiel #30
0
    def test_04_01_load_file(self):
        csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
            self.test_filename,
            self.test_path,
        )
        pipeline, module, filename = self.make_pipeline(csv_text)
        c0_ran = [False]

        def callback(workspace):
            imgset = workspace.image_set
            image = imgset.get_image("DNA")
            pixels = image.pixel_data
            self.assertEqual(pixels.shape[0], self.test_shape[0])
            c0_ran[0] = True

        c0 = C0()
        c0.callback = callback
        c0.module_num = 2
        pipeline.add_module(c0)

        try:
            m = pipeline.run()
            self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
            self.assertTrue(c0_ran[0])
            hexdigest = m.get_current_image_measurement("MD5Digest_DNA")
            self.assertEqual(hexdigest, self.test_md5)
            self.assertTrue(
                "PathName_DNA" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("PathName_DNA"), self.test_path
            )
            self.assertTrue(
                "FileName_DNA" in m.get_feature_names(cellprofiler.measurement.IMAGE)
            )
            self.assertEqual(
                m.get_current_image_measurement("FileName_DNA"), self.test_filename
            )
        finally:
            os.remove(filename)
    def test_05_01_some_rows(self):
        csv_text = '''"Test_Measurement"
1
2
3
4
5
6
7
8
9
10
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        module.wants_rows.value = True
        module.row_range.min = 4
        module.row_range.max = 6
        m = pipeline.run()
        self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
        data = m.get_all_measurements(cellprofiler.measurement.IMAGE, "Test_Measurement")
        self.assertTrue(numpy.all(data == numpy.arange(4, 7)))
        os.remove(filename)
    def test_05_01_some_rows(self):
        csv_text = '''"Test_Measurement"
1
2
3
4
5
6
7
8
9
10
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        module.wants_rows.value = True
        module.row_range.min = 4
        module.row_range.max = 6
        m = pipeline.run()
        self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
        data = m.get_all_measurements(cellprofiler.measurement.IMAGE,
                                      "Test_Measurement")
        self.assertTrue(numpy.all(data == numpy.arange(4, 7)))
        os.remove(filename)
Beispiel #33
0
def test_some_rows():
    csv_text = """"Test_Measurement"
1
2
3
4
5
6
7
8
9
10
"""
    pipeline, module, filename = make_pipeline(csv_text)
    module.wants_rows.value = True
    module.row_range.min = 4
    module.row_range.max = 6
    m = pipeline.run()
    assert isinstance(m, cellprofiler.measurement.Measurements)
    data = m.get_all_measurements(cellprofiler.measurement.IMAGE,
                                  "Test_Measurement")
    assert numpy.all(data == numpy.arange(4, 7))
    os.remove(filename)
    def test_05_02_img_717(self):
        '''Regression test of img-717, column without underbar'''
        csv_text = '''"Image","Test_Measurement"
"foo",1
"foo",2
"foo",3
"foo",4
"foo",5
"foo",6
"foo",7
"foo",8
"foo",9
"foo",10
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        module.wants_rows.value = True
        module.row_range.min = 4
        module.row_range.max = 6
        m = pipeline.run()
        self.assertTrue(isinstance(m, cellprofiler.measurement.Measurements))
        data = m.get_all_measurements(cellprofiler.measurement.IMAGE, "Test_Measurement")
        self.assertTrue(numpy.all(data == numpy.arange(4, 7)))
        os.remove(filename)
    def test_03_03_metadata_row_and_column_and_well(self):
        csv_text = '''"Metadata_Row","Metadata_Column","Metadata_Well"
"C","03","B14"
'''
        pipeline, module, filename = self.make_pipeline(csv_text)
        columns = module.get_measurement_columns(pipeline)
        self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and
                             c[1] == "Metadata_Row" and
                             c[2] == "varchar(1)" for c in columns]))
        self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and
                             c[1] == "Metadata_Column" and
                             c[2] == "varchar(2)" for c in columns]))
        self.assertTrue(any([c[0] == cellprofiler.measurement.IMAGE and
                             c[1] == "Metadata_Well" and
                             c[2] == "varchar(3)" for c in columns]))
        m = pipeline.run()
        features = module.get_measurements(pipeline, cellprofiler.measurement.IMAGE,
                                           cellprofiler.measurement.C_METADATA)
        for feature, expected in (("Row", "C"),
                                  ("Column", "03"),
                                  ("Well", "B14")):
            self.assertTrue(feature in features)
            value = m.get_current_image_measurement('_'.join((cellprofiler.measurement.C_METADATA, feature)))
            self.assertEqual(value, expected)
Beispiel #36
0
def test_load_file():
    csv_text = """"Image_FileName_DNA","Image_PathName_DNA"
"%s","%s"
""" % (
        test_filename,
        test_path,
    )
    pipeline, module, filename = make_pipeline(csv_text)
    c0_ran = [False]

    def callback(workspace):
        imgset = workspace.image_set
        image = imgset.get_image("DNA")
        pixels = image.pixel_data
        assert pixels.shape[0] == test_shape[0]
        c0_ran[0] = True

    c0 = C0()
    c0.callback = callback
    c0.set_module_num(2)
    pipeline.add_module(c0)

    try:
        m = pipeline.run()
        assert isinstance(m, cellprofiler.measurement.Measurements)
        assert c0_ran[0]
        hexdigest = m.get_current_image_measurement("MD5Digest_DNA")
        assert hexdigest == test_md5
        assert "PathName_DNA" in m.get_feature_names(
            cellprofiler.measurement.IMAGE)
        assert m.get_current_image_measurement("PathName_DNA") == test_path
        assert "FileName_DNA" in m.get_feature_names(
            cellprofiler.measurement.IMAGE)
        assert m.get_current_image_measurement("FileName_DNA") == test_filename
    finally:
        os.remove(filename)
Beispiel #37
0
def run_pipeline_headless(options, args):
    """
    Run a CellProfiler pipeline in headless mode
    """

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

    image_set_numbers = None

    if options.last_image_set is not 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)

            if image_set_start is None:
                image_set_numbers = numpy.arange(1, image_set_end + 1)
            else:
                image_set_numbers = numpy.arange(image_set_start, image_set_end + 1)
    else:
        image_set_end = None

    if (options.pipeline_filename is not None) and (not options.pipeline_filename.lower().startswith('http')):
        options.pipeline_filename = os.path.expanduser(options.pipeline_filename)

    pipeline = cellprofiler.pipeline.Pipeline()

    initial_measurements = None

    try:
        if h5py.is_hdf5(options.pipeline_filename):
            initial_measurements = cellprofiler.measurement.load_measurements(options.pipeline_filename, image_numbers=image_set_numbers)
    except:
        logging.root.info("Failed to load measurements from pipeline")

    if initial_measurements is not None:
        pipeline_text = initial_measurements.get_experiment_measurement(cellprofiler.pipeline.M_PIPELINE)

        pipeline_text = pipeline_text.encode('us-ascii')

        pipeline.load(cStringIO.StringIO(pipeline_text))

        if not pipeline.in_batch_mode():
            #
            # Need file list in order to call prepare_run
            #

            with h5py.File(options.pipeline_filename, "r") as src:
                if cellprofiler.utilities.hdf5_dict.HDF5FileList.has_file_list(src):
                    cellprofiler.utilities.hdf5_dict.HDF5FileList.copy(src, initial_measurements.hdf5_dict.hdf5_file)
    else:
        pipeline.load(options.pipeline_filename)

    if options.groups is not None:
        kvs = [x.split('=') for x in options.groups.split(',')]

        groups = dict(kvs)
    else:
        groups = None

    file_list = cellprofiler.preferences.get_image_set_file()

    if file_list is not None:
        pipeline.read_file_list(file_list)

    #
    # Fixup CreateBatchFiles with any command-line input or output directories
    #
    if pipeline.in_batch_mode():
        create_batch_files = [m for m in pipeline.modules() if m.is_create_batch_module()]

        if len(create_batch_files) > 0:
            create_batch_files = create_batch_files[0]

            if options.output_directory is not None:
                create_batch_files.custom_output_directory.value = options.output_directory

            if options.image_directory is not None:
                create_batch_files.default_image_directory.value = options.image_directory

    use_hdf5 = len(args) > 0 and not args[0].lower().endswith(".mat")

    measurements = pipeline.run(
        image_set_start=image_set_start,
        image_set_end=image_set_end,
        grouping=groups,
        measurements_filename=None if not use_hdf5 else args[0],
        initial_measurements=initial_measurements
    )

    if len(args) > 0 and not use_hdf5:
        pipeline.save_measurements(args[0], measurements)

    if options.done_file is not None:
        if measurements is not None and measurements.has_feature(cellprofiler.measurement.EXPERIMENT, cellprofiler.pipeline.EXIT_STATUS):
            done_text = measurements.get_experiment_measurement(cellprofiler.pipeline.EXIT_STATUS)

            exit_code = (0 if done_text == "Complete" else -1)
        else:
            done_text = "Failure"

            exit_code = -1

        fd = open(options.done_file, "wt")
        fd.write("%s\n" % done_text)
        fd.close()
    else:
        exit_code = 0

    if measurements is not None:
        measurements.close()

    return exit_code
Beispiel #38
0
def run_pipeline_headless(options, args):
    """
    Run a CellProfiler pipeline in headless mode
    """

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

    image_set_numbers = None

    if options.last_image_set is not 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)

            if image_set_start is None:
                image_set_numbers = numpy.arange(1, image_set_end + 1)
            else:
                image_set_numbers = numpy.arange(image_set_start,
                                                 image_set_end + 1)
    else:
        image_set_end = None

    if (options.pipeline_filename is not None) and (
            not options.pipeline_filename.lower().startswith('http')):
        options.pipeline_filename = os.path.expanduser(
            options.pipeline_filename)

    pipeline = cellprofiler.pipeline.Pipeline()

    initial_measurements = None

    try:
        if h5py.is_hdf5(options.pipeline_filename):
            initial_measurements = cellprofiler.measurement.load_measurements(
                options.pipeline_filename, image_numbers=image_set_numbers)
    except:
        logging.root.info("Failed to load measurements from pipeline")

    if initial_measurements is not None:
        pipeline_text = initial_measurements.get_experiment_measurement(
            cellprofiler.pipeline.M_PIPELINE)

        pipeline_text = pipeline_text.encode('us-ascii')

        pipeline.load(cStringIO.StringIO(pipeline_text))

        if not pipeline.in_batch_mode():
            #
            # Need file list in order to call prepare_run
            #

            with h5py.File(options.pipeline_filename, "r") as src:
                if cellprofiler.utilities.hdf5_dict.HDF5FileList.has_file_list(
                        src):
                    cellprofiler.utilities.hdf5_dict.HDF5FileList.copy(
                        src, initial_measurements.hdf5_dict.hdf5_file)
    else:
        pipeline.load(options.pipeline_filename)

    if options.groups is not None:
        kvs = [x.split('=') for x in options.groups.split(',')]

        groups = dict(kvs)
    else:
        groups = None

    file_list = cellprofiler.preferences.get_image_set_file()

    if file_list is not None:
        pipeline.read_file_list(file_list)

    #
    # Fixup CreateBatchFiles with any command-line input or output directories
    #
    if pipeline.in_batch_mode():
        create_batch_files = [
            m for m in pipeline.modules() if m.is_create_batch_module()
        ]

        if len(create_batch_files) > 0:
            create_batch_files = create_batch_files[0]

            if options.output_directory is not None:
                create_batch_files.custom_output_directory.value = options.output_directory

            if options.image_directory is not None:
                create_batch_files.default_image_directory.value = options.image_directory

    use_hdf5 = len(args) > 0 and not args[0].lower().endswith(".mat")

    measurements = pipeline.run(
        image_set_start=image_set_start,
        image_set_end=image_set_end,
        grouping=groups,
        measurements_filename=None if not use_hdf5 else args[0],
        initial_measurements=initial_measurements)

    if len(args) > 0 and not use_hdf5:
        pipeline.save_measurements(args[0], measurements)

    if options.done_file is not None:
        if measurements is not None and measurements.has_feature(
                cellprofiler.measurement.EXPERIMENT,
                cellprofiler.pipeline.EXIT_STATUS):
            done_text = measurements.get_experiment_measurement(
                cellprofiler.pipeline.EXIT_STATUS)

            exit_code = (0 if done_text == "Complete" else -1)
        else:
            done_text = "Failure"

            exit_code = -1

        fd = open(options.done_file, "wt")
        fd.write("%s\n" % done_text)
        fd.close()
    else:
        exit_code = 0

    if measurements is not None:
        measurements.close()

    return exit_code