Exemplo n.º 1
0
        def fn():
            cellprofiler.utilities.jutil.attach()
            with self.image_dict_lock:
                generation = self.image_dict_generation

            for k, v in well.iteritems():
                sd = {}
                with self.image_dict_lock:
                    if self.image_dict_generation > generation:
                        return
                    self.image_dict[k] = sd
                for c, fd in enumerate(v):
                    if PlateData.D_CHANNEL in fd:
                        channel = fd[PlateData.D_CHANNEL]
                    else:
                        channel = str(c + 1)
                    try:
                        img = load_using_bioformats_url(fd[PlateData.D_FILENAME])
                        with self.image_dict_lock:
                            if self.image_dict_generation > generation:
                                return
                            sd[channel] = img
                    except:
                        traceback.print_exc()
                        pass
            wx.CallAfter(self.update_figure)
            cellprofiler.utilities.jutil.detach()
Exemplo n.º 2
0
        def fn():
            cellprofiler.utilities.jutil.attach()
            with self.image_dict_lock:
                generation = self.image_dict_generation

            for k, v in well.iteritems():
                sd = {}
                with self.image_dict_lock:
                    if self.image_dict_generation > generation:
                        return
                    self.image_dict[k] = sd
                for c, fd in enumerate(v):
                    if PlateData.D_CHANNEL in fd:
                        channel = fd[PlateData.D_CHANNEL]
                    else:
                        channel = str(c + 1)
                    try:
                        img = load_using_bioformats_url(
                            fd[PlateData.D_FILENAME])
                        with self.image_dict_lock:
                            if self.image_dict_generation > generation:
                                return
                            sd[channel] = img
                    except:
                        traceback.print_exc()
                        pass
            wx.CallAfter(self.update_figure)
            cellprofiler.utilities.jutil.detach()
 def test_03_03_load_using_bioformats_url(self):
     url = "https://github.com/CellProfiler/python-bioformats/raw/1.0.5/bioformats/tests/Channel1-01-A-01.tif"
     try:
         fd = urlopen(url)
         if fd.code < 200 or fd.code >= 300:
             raise OSError("Http error %d" % fd.code)
     except OSError as e:
         def bad_url(e=e):
             raise e
         unittest.expectedFailure(bad_url)()
     
     data = F.load_using_bioformats_url(url, rescale=False)
     self.assertSequenceEqual(data.shape, (640, 640))
    def test_03_03_load_using_bioformats_url(self):
        url = "https://github.com/CellProfiler/python-bioformats/raw/1.0.5/bioformats/tests/Channel1-01-A-01.tif"
        try:
            fd = urlopen(url)
            if fd.code < 200 or fd.code >= 300:
                raise OSError("Http error %d" % fd.code)
        except OSError as e:

            def bad_url(e=e):
                raise e

            unittest.expectedFailure(bad_url)()

        data = F.load_using_bioformats_url(url, rescale=False)
        self.assertSequenceEqual(data.shape, (640, 640))
Exemplo n.º 5
0
 def test_03_03_load_using_bioformats_url(self):
     data = F.load_using_bioformats_url(
         "http://www.cellprofiler.org/linked_files/broad-logo.gif",
         rescale=False)
     self.assertSequenceEqual(data.shape, (38, 150, 3))
     expected_0_10_0_10 = np.array([[
         181, 176, 185, 185, 175, 175, 176, 195, 187, 185
     ], [25, 7, 7, 7, 2, 2, 13, 13, 0, 1], [21, 1, 1, 0, 0, 1, 0, 1, 0, 0],
                                    [64, 13, 1, 1, 12, 12, 2, 1, 1, 1],
                                    [22, 56, 26, 13, 1, 1, 6, 0, 0, 0],
                                    [12, 13, 82, 57, 9, 12, 2, 6, 6, 6],
                                    [12, 13, 20, 89, 89, 21, 11, 12, 1, 0],
                                    [6, 1, 7, 21, 89, 102, 26, 0, 10, 1],
                                    [26, 0, 0, 1, 20, 84, 151, 58, 12, 1],
                                    [23, 6, 1, 1, 0, 1, 55, 166, 100, 12]],
                                   dtype=np.uint8)
     self.assertTrue(np.all(expected_0_10_0_10 == data[:10, :10, 0]))
Exemplo n.º 6
0
 def test_03_03_load_using_bioformats_url(self):
     data = F.load_using_bioformats_url(
         "http://www.cellprofiler.org/linked_files/broad-logo.gif")
     self.assertSequenceEqual(data.shape, (38, 150, 3))
     expected_0_10_0_10 = np.array([
         [181, 176, 185, 185, 175, 175, 176, 195, 187, 185],
         [ 25,   7,   7,   7,   2,   2,  13,  13,   0,   1],
         [ 21,   1,   1,   0,   0,   1,   0,   1,   0,   0],
         [ 64,  13,   1,   1,  12,  12,   2,   1,   1,   1],
         [ 22,  56,  26,  13,   1,   1,   6,   0,   0,   0],
         [ 12,  13,  82,  57,   9,  12,   2,   6,   6,   6],
         [ 12,  13,  20,  89,  89,  21,  11,  12,   1,   0],
         [  6,   1,   7,  21,  89, 102,  26,   0,  10,   1],
         [ 26,   0,   0,   1,  20,  84, 151,  58,  12,   1],
         [ 23,   6,   1,   1,   0,   1,  55, 166, 100,  12]], 
                                   dtype=np.uint8)
     self.assertTrue(np.all(expected_0_10_0_10 == data[:10,:10, 0]))
Exemplo n.º 7
0
        def fn():
            from bioformats.formatreader import load_using_bioformats_url
            import cellprofiler.utilities.jutil
            from scipy.io.matlab.mio import loadmat
            from cellprofiler.modules.loadimages import url2pathname

            cellprofiler.utilities.jutil.attach()
            with self.image_dict_lock:
                generation = self.image_dict_generation
                
            for k, v in well.iteritems():
                sd = {}
                with self.image_dict_lock:
                    if self.image_dict_generation > generation:
                        return
                    self.image_dict[k] = sd
                for c, fd in enumerate(v):
                    if PlateData.D_CHANNEL in fd:
                        channel = fd[PlateData.D_CHANNEL]
                    else:
                        channel = str(c+1)
                    url = fd[PlateData.D_FILENAME]
                    try:
                        if url.lower().endswith(".mat"):
                            img = loadmat(
                                url2pathname(url), 
                                struct_as_record=True)["Image"]
                        else:
                            img = load_using_bioformats_url(url)
                        with self.image_dict_lock:
                            if self.image_dict_generation > generation:
                                return
                            sd[channel] = img
                    except:
                        traceback.print_exc()
                        pass
            wx.CallAfter(self.update_figure)
            cellprofiler.utilities.jutil.static_call(
                "java/lang/System", "gc", "()V")
            cellprofiler.utilities.jutil.detach()
Exemplo n.º 8
0
        def fn():
            from bioformats.formatreader import load_using_bioformats_url
            import cellprofiler.utilities.jutil
            from scipy.io.matlab.mio import loadmat
            from cellprofiler.modules.loadimages import url2pathname

            cellprofiler.utilities.jutil.attach()
            with self.image_dict_lock:
                generation = self.image_dict_generation

            for k, v in well.iteritems():
                sd = {}
                with self.image_dict_lock:
                    if self.image_dict_generation > generation:
                        return
                    self.image_dict[k] = sd
                for c, fd in enumerate(v):
                    if PlateData.D_CHANNEL in fd:
                        channel = fd[PlateData.D_CHANNEL]
                    else:
                        channel = str(c + 1)
                    url = fd[PlateData.D_FILENAME]
                    try:
                        if url.lower().endswith(".mat"):
                            img = loadmat(url2pathname(url),
                                          struct_as_record=True)["Image"]
                        else:
                            img = load_using_bioformats_url(url)
                        with self.image_dict_lock:
                            if self.image_dict_generation > generation:
                                return
                            sd[channel] = img
                    except:
                        traceback.print_exc()
                        pass
            wx.CallAfter(self.update_figure)
            cellprofiler.utilities.jutil.static_call("java/lang/System", "gc",
                                                     "()V")
            cellprofiler.utilities.jutil.detach()
    def test_02_01_compare_to_matlab(self):
        path = os.path.split(__file__)[0]
        png_file = 'Channel2-01-A-01Mask.png'
        mat_file = 'texturemeasurements.mat'
        mask_file = os.path.join(path, png_file)
        if not os.path.isfile(mask_file):
            github_fmt = github_url + "/cellprofiler/modules/tests/%s"
            mask = load_using_bioformats_url(github_fmt % png_file) != 0
            fin = urllib2.urlopen(github_fmt % mat_file)
            fd, tempmat = tempfile.mkstemp(".mat")
            fout = os.fdopen(fd, "wb")
            shutil.copyfileobj(fin, fout)
            fout.close()
            texture_measurements = loadmat(tempmat, struct_as_record=True)
            os.remove(tempmat)
        else:
            mask = load_image(mask_file) != 0
            texture_measurements = loadmat(
                os.path.join(path, mat_file), 
                struct_as_record=True)
                
        texture_measurements = texture_measurements['m'][0,0]
        folder = 'ExampleSBSImages'
        file_name = 'Channel1-01-A-01.tif'
        image_file = os.path.join(
            example_images_directory(), folder, file_name)
        maybe_download_example_image([folder], file_name)
        image = load_image(image_file, rescale=False).astype(float)/255.0
        labels,count = scind.label(mask.astype(bool),np.ones((3,3),bool))
        centers = scind.center_of_mass(np.ones(labels.shape), labels, 
                                       np.arange(count)+1)
        centers = np.array(centers)
        X = 1 # the index of the X coordinate
        Y = 0 # the index of the Y coordinate
        order_python = np.lexsort((centers[:,X],centers[:,Y]))
        workspace, module = self.make_workspace(image, labels, convert = False)
        module.scale_groups[0].scale.value = 3
        my_angle = M.H_HORIZONTAL
        module.scale_groups[0].angles.value = my_angle

        module.run(workspace)
        m = workspace.measurements
        tm_center_x = texture_measurements['Location_Center_X'][0,0][:,0]
        tm_center_y = texture_measurements['Location_Center_Y'][0,0][:,0]
        order_matlab = np.lexsort((tm_center_x,tm_center_y))
            
        for measurement in M.F_HARALICK:
            mname = '%s_%s_%s_%d'%(M.TEXTURE, measurement, INPUT_IMAGE_NAME, 3)
            pymname = mname + "_" + M.H_TO_A[my_angle]
            pytm = m.get_current_measurement(INPUT_OBJECTS_NAME, pymname)
            tm = texture_measurements[mname][0,0][:,]
            error_count = 0
            for i in range(count):
                matlab_val = tm[order_matlab[i]]
                python_val = pytm[order_python[i]]
                self.assertAlmostEqual(tm[order_matlab[i]],
                                       pytm[order_python[i]],7,
                                       "Measurement = %s, Loc=(%.2f,%.2f), Matlab=%f, Python=%f"%
                                       (mname, tm_center_x[order_matlab[i]],
                                        tm_center_y[order_matlab[i]],
                                        tm[order_matlab[i]],
                                        pytm[order_python[i]]))
        image_measurements =\
        (('Texture_AngularSecondMoment_Cytoplasm_3', 0.5412),
         ('Texture_Contrast_Cytoplasm_3',0.1505),
         ('Texture_Correlation_Cytoplasm_3', 0.7740),
         ('Texture_Variance_Cytoplasm_3', 0.3330),
         ('Texture_InverseDifferenceMoment_Cytoplasm_3',0.9321),
         ('Texture_SumAverage_Cytoplasm_3',2.5684),
         ('Texture_SumVariance_Cytoplasm_3',1.1814),
         ('Texture_SumEntropy_Cytoplasm_3',0.9540),
         ('Texture_Entropy_Cytoplasm_3',1.0677),
         ('Texture_DifferenceVariance_Cytoplasm_3',0.1314),
         ('Texture_DifferenceEntropy_Cytoplasm_3',0.4147),
         ('Texture_InfoMeas1_Cytoplasm_3',-0.4236),
         ('Texture_InfoMeas2_Cytoplasm_3',0.6609))
        for feature_name, value in image_measurements:
            py_feature_name = feature_name + "_" + M.H_TO_A[my_angle]
            pytm = m.get_current_image_measurement(py_feature_name)
            self.assertAlmostEqual(pytm, value,3,
                                   "%s failed. Python=%f, Matlab=%f" %
                                   (feature_name, pytm, value))
Exemplo n.º 10
0
    def test_02_01_compare_to_matlab(self):
        path = os.path.split(__file__)[0]
        png_file = 'Channel2-01-A-01Mask.png'
        mat_file = 'texturemeasurements.mat'
        mask_file = os.path.join(path, png_file)
        if not os.path.isfile(mask_file):
            github_fmt = github_url + "/cellprofiler/modules/tests/%s"
            mask = load_using_bioformats_url(github_fmt % png_file) != 0
            fin = urllib2.urlopen(github_fmt % mat_file)
            fd, tempmat = tempfile.mkstemp(".mat")
            fout = os.fdopen(fd, "wb")
            shutil.copyfileobj(fin, fout)
            fout.close()
            texture_measurements = loadmat(tempmat, struct_as_record=True)
            os.remove(tempmat)
        else:
            mask = load_image(mask_file) != 0
            texture_measurements = loadmat(os.path.join(path, mat_file),
                                           struct_as_record=True)

        texture_measurements = texture_measurements['m'][0, 0]
        folder = 'ExampleSBSImages'
        file_name = 'Channel1-01-A-01.tif'
        image_file = os.path.join(example_images_directory(), folder,
                                  file_name)
        maybe_download_example_image([folder], file_name)
        image = load_image(image_file, rescale=False).astype(float) / 255.0
        labels, count = scind.label(mask.astype(bool), np.ones((3, 3), bool))
        centers = scind.center_of_mass(np.ones(labels.shape), labels,
                                       np.arange(count) + 1)
        centers = np.array(centers)
        X = 1  # the index of the X coordinate
        Y = 0  # the index of the Y coordinate
        order_python = np.lexsort((centers[:, X], centers[:, Y]))
        workspace, module = self.make_workspace(image, labels, convert=False)
        module.scale_groups[0].scale.value = 3
        my_angle = M.H_HORIZONTAL
        module.scale_groups[0].angles.value = my_angle

        module.run(workspace)
        m = workspace.measurements
        tm_center_x = texture_measurements['Location_Center_X'][0, 0][:, 0]
        tm_center_y = texture_measurements['Location_Center_Y'][0, 0][:, 0]
        order_matlab = np.lexsort((tm_center_x, tm_center_y))

        for measurement in M.F_HARALICK:
            mname = '%s_%s_%s_%d' % (M.TEXTURE, measurement, INPUT_IMAGE_NAME,
                                     3)
            pymname = mname + "_" + M.H_TO_A[my_angle]
            pytm = m.get_current_measurement(INPUT_OBJECTS_NAME, pymname)
            tm = texture_measurements[mname][0, 0][:, ]
            error_count = 0
            for i in range(count):
                matlab_val = tm[order_matlab[i]]
                python_val = pytm[order_python[i]]
                self.assertAlmostEqual(
                    tm[order_matlab[i]], pytm[order_python[i]], 7,
                    "Measurement = %s, Loc=(%.2f,%.2f), Matlab=%f, Python=%f" %
                    (mname, tm_center_x[order_matlab[i]],
                     tm_center_y[order_matlab[i]], tm[order_matlab[i]],
                     pytm[order_python[i]]))
        image_measurements =\
        (('Texture_AngularSecondMoment_Cytoplasm_3', 0.5412),
         ('Texture_Contrast_Cytoplasm_3',0.1505),
         ('Texture_Correlation_Cytoplasm_3', 0.7740),
         ('Texture_Variance_Cytoplasm_3', 0.3330),
         ('Texture_InverseDifferenceMoment_Cytoplasm_3',0.9321),
         ('Texture_SumAverage_Cytoplasm_3',2.5684),
         ('Texture_SumVariance_Cytoplasm_3',1.1814),
         ('Texture_SumEntropy_Cytoplasm_3',0.9540),
         ('Texture_Entropy_Cytoplasm_3',1.0677),
         ('Texture_DifferenceVariance_Cytoplasm_3',0.1314),
         ('Texture_DifferenceEntropy_Cytoplasm_3',0.4147),
         ('Texture_InfoMeas1_Cytoplasm_3',-0.4236),
         ('Texture_InfoMeas2_Cytoplasm_3',0.6609))
        for feature_name, value in image_measurements:
            py_feature_name = feature_name + "_" + M.H_TO_A[my_angle]
            pytm = m.get_current_image_measurement(py_feature_name)
            self.assertAlmostEqual(
                pytm, value, 3, "%s failed. Python=%f, Matlab=%f" %
                (feature_name, pytm, value))