Beispiel #1
0
 def _showMeanStd(self, busy=True):
     if busy:
         busy = BusyIndicator()
     dmin, dmax = self._subset_range
     subset, mask = self.image.optimalRavel(self._subset)
     dprint(5, "computing mean")
     mean = measurements.mean(subset, labels=mask, index=None if mask is None else False)
     dprint(5, "computing std")
     std = measurements.standard_deviation(subset, labels=mask, index=None if mask is None else False)
     dprint(5, "done")
     text = "  ".join([("%s: " + DataValueFormat) % (name, value) for name, value in
                       ("min", dmin), ("max", dmax), ("mean", mean), ("std", std)] + ["np: %d" % self._subset.size])
     self._wlab_stats.setText(text)
     self._wmore_stats.hide()
     # update markers
     ypos = 0.3
     self._line_mean.line.setData([mean, mean], [0, 1])
     self._line_mean.marker.setValue(mean, ypos)
     self._line_mean.setText(("\u03BC=" + DataValueFormat) % mean)
     self._line_mean.show()
     self._line_std.line.setData([mean - std, mean + std], [ypos, ypos])
     self._line_std.marker.setValue(mean, ypos)
     self._line_std.setText(("\u03C3=" + DataValueFormat) % std)
     self._line_std.show()
     self._histplot.replot()
Beispiel #2
0
    def measure_expression(self, im, labels, segment_ids):
        """
        Measure expression levels.

        Args:

            im (np.ndarray[float]) - 3D array of pixel values

            labels (np.ndarray[int]) - cell segment labels

            segment_ids (np.ndarray[int]) - ordered segment IDs

        """

        # split R/G/B image channels
        drop = lambda x: x.reshape(*x.shape[:2])
        channels = [drop(x) for x in np.split(im, self.colordepth, axis=-1)]

        # compute means
        means = [mean(channel, labels, segment_ids) for channel in channels]

        # compute std
        with catch_warnings():
            filterwarnings('ignore')
            evaluate_std = lambda x: standard_deviation(x, labels, segment_ids)
            stds = [evaluate_std(channel) for channel in channels]

        # compile dictionaries
        self.levels = dict(enumerate(means))
        self.std = dict(enumerate(stds))
Beispiel #3
0
 def getLMRectStats(self, rect):
     xx1, xx2, yy1, yy2 = self._lmRectToPix(rect)
     if xx1 is not None:
         subset = self.image.image()[xx1:xx2, yy1:yy2]
         subset, mask = self.image.optimalRavel(subset)
         mmin, mmax = measurements.extrema(subset, labels=mask, index=None if mask is None else False)[:2]
         mean = measurements.mean(subset, labels=mask, index=None if mask is None else False)
         std = measurements.standard_deviation(subset, labels=mask, index=None if mask is None else False)
         ssum = measurements.sum(subset, labels=mask, index=None if mask is None else False)
         return xx1, xx2, yy1, yy2, mmin, mmax, mean, std, ssum, subset.size
     return None
Beispiel #4
0
def get_superpixel_stds_band(label_array: np.ndarray,
                             band: np.ndarray) -> np.ndarray:
    """Assume labels in label_array are 0, 1, 2, ..., n

    Returns array of shape = (len(np.unique(labels)) x 1)
    """
    labels_ = label_array + 1  # scipy wants labels to begin at 1 and transforms to 1, 2, ..., n+1
    labels_unique = np.unique(labels_)
    means = measurements.standard_deviation(band,
                                            labels=labels_,
                                            index=labels_unique)
    return means.reshape((-1, 1))
Beispiel #5
0
    def phot(self, im, showmask=True):
        # TODO if we switch to astropy.photometry then we can have that
        # do the work with subpixels properly, but for now they don't
        # do rms of the bg correctly so we can't use their stuff yet.

        mask = self.setmask(im)

        if showmask:
            cmap1 = pl.matplotlib.colors.LinearSegmentedColormap.from_list(
                'my_cmap', ["black", "blue"], 2)
            cmap1._init()
            cmap1._lut[:, -1] = pl.array([0, 0.5, 0, 0, 0])
            pl.imshow(mask > 0,
                      origin="bottom",
                      interpolation="nearest",
                      cmap=cmap1)

        from scipy import ndimage
        from scipy.ndimage import measurements as m
        nin = len(pl.where(mask == 1)[0])
        nout = len(pl.where(mask == 2)[0])

        floor = pl.nanmin(im.data)
        if floor < 0: floor = 0
        raw = m.sum(im.data, mask, 1) - floor * nin

        #bg=m.mean(im.data,mask,2)
        #bgsig=m.standard_deviation(im.data,mask,2)

        #        from astropy.stats import sigma_clip
        #        clipped = sigma_clip(im.data,sig=3,iters=2)
        #        # http://astropy.readthedocs.org/en/latest/api/astropy.stats.sigma_clip.html#astropy.stats.sigma_clip
        #        # TODO what we really want is to sigma-clip only the BG array/mask
        #        # because including the source will probably just be domimated by the
        #        # source...
        #        bg   =m.mean(              clipped,mask,2)-floor
        #        bgsig=m.standard_deviation(clipped,mask,2)

        # sigma_clip doesn't handle nans
        from scipy import stats

        def mymode(x):
            return stats.mode(x, axis=None)[0][0]

#        pdb.set_trace()
#        xx=stats.mode(im.data,axis=None)
#        print xx

        bg = ndimage.labeled_comprehension(im.data, mask, 2, mymode, "float",
                                           0) - floor
        #        bg = ndimage.labeled_comprehension(im.data,mask,2,pl.mean,"float",0)
        bgsig = m.standard_deviation(im.data, mask, 2)

        # assume uncert dominated by BG level.
        # TODO add sqrt(cts in source) Poisson - need gain or explicit err/pix
        uncert = bgsig * nin / pl.sqrt(nout)

        results = raw, bg, raw - bg * nin, uncert

        f = self.photfactor(im)
        if f:
            if self.debug: print "phot factor = ", f
            results = pl.array(results) * f

        if self.debug:
            #            print "max=", m.maximum(im.data,mask,1), m.maximum(im.data,mask,2)
            #            print "nin,nout=",nin,nout
            print "raw, bg, bgsubbed, uncert=", results
            pdb.set_trace()

        return results
Beispiel #6
0
def label_objects(dataset=None, labels=None, out=None, out_features=None,
                  source=None, return_labels=False):
    DM = DataModel.instance()

    log.info('+ Loading data into memory')
    data = DM.load_slices(dataset)
    if labels is None:
        data += 1
        labels = set(np.unique(data)) - set([0])
    else:
        data += 1
        labels = np.asarray(labels) + 1

    obj_labels = []

    log.info('+ Extracting individual objects')
    new_labels = np.zeros(data.shape, np.int32)
    total_labels = 0
    num = 0

    for label in labels:
        mask = (data == label)
        tmp_data = data.copy()
        tmp_data[~mask] = 0
        tmp_labels, num = splabel(tmp_data, structure=octahedron(1))
        mask = (tmp_labels > 0)
        new_labels[mask] = tmp_labels[mask] + total_labels
        total_labels += num
        obj_labels += [label] * num

    log.info('+ {} Objects found'.format(total_labels))
    log.info('+ Saving results')
    DM.create_empty_dataset(out, DM.data_shape, new_labels.dtype)
    DM.write_slices(out, new_labels, params=dict(active=True, num_objects=total_labels))

    log.info('+ Loading source to memory')
    data = DM.load_slices(source)
    objs = new_labels
    objects = new_labels
    num_objects = total_labels
    objlabels = np.arange(1, num_objects+1)

    log.info('+ Computing Average intensity')
    feature = measure.mean(data, objs, index=objlabels)
    DM.create_empty_dataset(out_features[0], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[0], feature, params=dict(active=True))

    """log.info('+ Computing Median intensity')
    objs.shape = -1
    data.shape = -1
    feature = binned_statistic(objs, data, statistic='median',
                               bins=num_objects+1)[0]
    feature = feature[objlabels]
    out_features[1].write_direct(feature)
    out_features[1].attrs['active'] = True
    objs.shape = dataset.shape
    data.shape = dataset.shape"""

    log.info('+ Computing Sum of intensity')
    feature = measure.sum(data, objs, index=objlabels)
    DM.create_empty_dataset(out_features[1], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[1], feature, params=dict(active=True))

    log.info('+ Computing Standard Deviation of intensity')
    feature = measure.standard_deviation(data, objs, index=objlabels)
    DM.create_empty_dataset(out_features[2], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[2], feature, params=dict(active=True))

    log.info('+ Computing Variance of intensity')
    feature = measure.variance(data, objs, index=objlabels)
    DM.create_empty_dataset(out_features[3], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[3], feature, params=dict(active=True))

    log.info('+ Computing Area')
    objs.shape = -1
    feature = np.bincount(objs, minlength=num_objects+1)[1:]
    DM.create_empty_dataset(out_features[4], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[4], feature, params=dict(active=True))
    DM.create_empty_dataset(out_features[5], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[5], np.log10(feature), params=dict(active=True))
    objs.shape = data.shape

    log.info('+ Computing Bounding Box')
    obj_windows = measure.find_objects(objs)
    feature = []; depth = []; height = []; width = [];
    for w in obj_windows:
        feature.append((w[0].stop - w[0].start) *
                       (w[1].stop - w[1].start) *
                       (w[2].stop - w[2].start))
        depth.append(w[0].stop - w[0].start)
        height.append(w[1].stop - w[1].start)
        width.append(w[2].stop - w[2].start)

    feature = np.asarray(feature, np.float32)
    DM.create_empty_dataset(out_features[6], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[6], feature, params=dict(active=True))
    #depth
    depth = np.asarray(depth, np.float32)
    DM.create_empty_dataset(out_features[7], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[7], depth, params=dict(active=True))
    # height
    height = np.asarray(height, np.float32)
    DM.create_empty_dataset(out_features[8], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[8], height, params=dict(active=True))
    # width
    width = np.asarray(width, np.float32)
    DM.create_empty_dataset(out_features[9], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[9], width, params=dict(active=True))
    # log10
    DM.create_empty_dataset(out_features[10], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[10], np.log10(feature), params=dict(active=True))

    log.info('+ Computing Oriented Bounding Box')
    ori_feature = []; ori_depth = []; ori_height = []; ori_width = [];
    for i, w in enumerate(obj_windows):
        z, y, x = np.where(objs[w] == i+1)
        coords = np.c_[z, y, x]
        if coords.shape[0] >= 3:
            coords = PCA(n_components=3).fit_transform(coords)
        cmin, cmax = coords.min(0), coords.max(0)
        zz, yy, xx = (cmax[0] - cmin[0] + 1,
                      cmax[1] - cmin[1] + 1,
                      cmax[2] - cmin[2] + 1)
        ori_feature.append(zz * yy * xx)
        ori_depth.append(zz)
        ori_height.append(yy)
        ori_width.append(xx)

    ori_feature = np.asarray(ori_feature, np.float32)
    DM.create_empty_dataset(out_features[11], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[11], ori_feature, params=dict(active=True))
    #depth
    ori_depth = np.asarray(ori_depth, np.float32)
    DM.create_empty_dataset(out_features[12], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[12], ori_depth, params=dict(active=True))
    # height
    ori_height = np.asarray(ori_height, np.float32)
    DM.create_empty_dataset(out_features[13], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[13], ori_height, params=dict(active=True))
    # width
    ori_width = np.asarray(ori_width, np.float32)
    DM.create_empty_dataset(out_features[14], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[14], ori_width, params=dict(active=True))
    # log10
    DM.create_empty_dataset(out_features[15], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[15], np.log10(ori_feature), params=dict(active=True))

    log.info('+ Computing Positions')
    pos = measure.center_of_mass(objs, labels=objs, index=objlabels)
    pos = np.asarray(pos, dtype=np.float32)
    DM.create_empty_dataset(out_features[16], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[16], pos[:, 2].copy(), params=dict(active=True))
    DM.create_empty_dataset(out_features[17], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[17], pos[:, 1].copy(), params=dict(active=True))
    DM.create_empty_dataset(out_features[18], (num_objects,), np.float32, check=False)
    DM.write_dataset(out_features[18], pos[:, 0].copy(), params=dict(active=True))

    if return_labels:
        return out, total_labels, np.asarray(obj_labels)

    return out, total_labels