Beispiel #1
0
    def N_clusters(self, cr_bin, prev_cr_bin, Nprev, cutoff):

        lw, num = measurements.label(cr_bin)

        minLab = np.min(lw)
        maxLab = np.max(lw)
        #print("labels:  min: {}   max: {}".format(minLab, maxLab), flush=True)
        hist = measurements.histogram(lw, minLab + 1, maxLab, maxLab - minLab)

        # remove clusters below cutoff from count
        hist[hist < cutoff] = 0
        hist[hist != 0] = 1

        sum_hist = np.sum(hist[np.nonzero(hist)])

        Ntotal = float(sum_hist)

        NoldMerged = 0
        if np.max(prev_cr_bin) > 0:
            # remove all new crystal pixels
            no_new = np.multiply(lw, prev_cr_bin)
            no_new_hist = measurements.histogram(no_new, minLab + 1, maxLab,
                                                 maxLab - minLab)

            no_new_hist[no_new_hist != 0] = 1
            NoldMerged = np.sum(no_new_hist[np.nonzero(no_new_hist)])

        #print("  ---NoldMerged:  {}".format(NoldMerged))

        N_new = Ntotal - NoldMerged

        N_merged = Nprev - NoldMerged

        return Ntotal, N_new, N_merged
Beispiel #2
0
def image_to_landscape(image, n=2):
    '''Converts an image to a 2d array of integers from 0 to n-1.

    Most image formats (including TIFF) requires PIL.

    Parameters
    ----------
    image : string
        image filename

    Returns
    -------
    landscape : 2-d array
        all values are integers from 0 to n-1, where zeroes correspond to
        lighter shades in the original image, and darker shades to higher
        values, so that each value corresponds to a distinct type of habitat

    '''
    from matplotlib.image import imread
    from scipy.ndimage.measurements import histogram

    M = imread(image).sum(axis=2)[::-1, :]
    L = np.zeros_like(M)
    nbins = max(20, n * 4)
    freq = histogram(M, M.min(), M.max(), nbins)
    bins = np.linspace(M.min(), M.max(), nbins)
    peaks = sorted(freq.argsort()[-n:])
    delims = [bins[0]
              ] + [bins[(peaks[i + 1] + peaks[i]) // 2]
                   for i in range(n - 1)] + [bins[-1] + 1]
    for i in range(n):
        L[(M > delims[i]) & (M < delims[i + 1])] = i
    #M = np.around(M/M.max())
    #return 1 - M.astype(np.int16)
    return L
def calculate_threshold(sweep, trusted_range):
    from scipy.ndimage.measurements import histogram
    from thresholding import maximum_deviation
    import numpy

    threshold_list = []
    for i, flex_image in enumerate(sweep):

        # Get image as numpy array
        image = flex_image.as_numpy_array()

        # Cap pixels to within trusted range
        image.shape = -1
        ind = numpy.where(image < trusted_range[0])
        image[ind] = trusted_range[0]
        ind = numpy.where(image > trusted_range[1])
        image[ind] = trusted_range[1]

        # Histogram the pixels
        histo = histogram(image, trusted_range[0], trusted_range[1],
                          trusted_range[1])
        histo = histo / numpy.sum(histo)

        # Calculate the threshold and add to list
        threshold = maximum_deviation(histo)
        threshold_list.append(threshold)

    # Return mean threshold
    return numpy.mean(threshold_list)
Beispiel #4
0
    def plot_Ncryst_vsPoreSize(self, im_bin_cryst, im_pore):
        #minPore = self.SCALE * np.min(im_pore)
        #maxPore = self.SCALE * np.max(im_pore)
        #minPore = self.SCALE * np.min(im_pore[np.nonzero(im_pore)])
        #maxPore = self.SCALE * np.max(im_pore[np.nonzero(im_pore)])
        minPore = np.min(im_pore[np.nonzero(im_pore)])
        maxPore = np.max(im_pore[np.nonzero(im_pore)])
        num_bin = 200
        
        lw, num = measurements.label(im_bin_cryst)
        minLab = np.min(lw)
        maxLab = np.max(lw)
        print("labels:  min: {}   max: {}".format(minLab, maxLab), flush=True)
        hist = measurements.histogram(lw, minLab + 1, maxLab, maxLab - minLab)
        for i1, size in enumerate(hist):
            if size < 10:
                im_bin_cryst[ im_bin_cryst==i1 ]=0
        
        hist_pores, bin_edges_pores =\
            np.histogram(im_pore, bins=num_bin, range=(minPore, maxPore))
        
        CP = np.multiply(im_bin_cryst, im_pore)

        hist, bin_edges = \
            np.histogram(CP, bins=num_bin, range=(minPore, maxPore))
        
        be = bin_edges[:-1] + (bin_edges[1] - bin_edges[0])/2.0
        
        #ret_hist = np.divide(hist, hist_pores)
        ret_hist = hist_pores

        return be, ret_hist
def calculate_threshold(sweep, trusted_range):
  from scipy.ndimage.measurements import histogram
  from thresholding import maximum_deviation
  import numpy

  threshold_list = []
  for i, flex_image in enumerate(sweep):

    # Get image as numpy array
    image = flex_image.as_numpy_array()

    # Cap pixels to within trusted range
    image.shape = -1
    ind = numpy.where(image < trusted_range[0])
    image[ind] = trusted_range[0]
    ind = numpy.where(image > trusted_range[1])
    image[ind] = trusted_range[1]

    # Histogram the pixels
    histo = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1])
    histo = histo / numpy.sum(histo)

    # Calculate the threshold and add to list
    threshold = maximum_deviation(histo)
    threshold_list.append(threshold)

  # Return mean threshold
  return numpy.mean(threshold_list)
Beispiel #6
0
    def histogram(self, min_value=0, max_value=0, bins = 0, labels = None, index = None):
        '''returns a histogram with default arguments'''
        if bins == 0:
            if self.dtype_max() > 1:
                bins = self.dtype_max()
            else:
                bins = len(unique(self))

        if max_value == 0:
            max_value = self.dtype_max()

        return _measurements.histogram(self, min_value, max_value, bins, labels=labels, index=index)
Beispiel #7
0
 def _computeCDF (self,data):
   """Recomputes the CDF using the current data subset and range""";
   dmin,dmax = self.getDataRange(self.subset if self.subset is not None else data);
   if dmin == dmax:
     self._cdf = None;
   else:
     dprint(1,"computing CDF for range",dmin,dmax);
     # make cumulative histogram, normalize to 0...1
     hist = measurements.histogram(self.subset if self.subset is not None else data,dmin,dmax,self._nbins);
     cdf = numpy.cumsum(hist);
     cdf = cdf/float(cdf[-1]);
     # append 0 at beginning, as left side of bin
     self._cdf = numpy.zeros(len(cdf)+1,float);
     self._cdf[1:] = cdf[...];
     # make array of bin edges
     self._bins = dmin + (dmax-dmin)*numpy.arange(self._nbins+1)/float(self._nbins);
Beispiel #8
0
 def _computeCDF(self, data):
     """Recomputes the CDF using the current data subset and range"""
     dmin, dmax = self.getDataRange(self.subset if self.subset is not None else data)
     if dmin == dmax:
         self._cdf = None
     else:
         dprint(1, "computing CDF for range", dmin, dmax)
         # make cumulative histogram, normalize to 0...1
         hist = measurements.histogram(self.subset if self.subset is not None else data, dmin, dmax, self._nbins)
         cdf = numpy.cumsum(hist)
         if not numpy.all(cdf == 0):
             cdf = cdf / float(cdf[-1])
             # append 0 at beginning, as left side of bin
             self._cdf = numpy.zeros(len(cdf) + 1, float)
             self._cdf[1:] = cdf[...]
             # make array of bin edges
             self._bins = dmin + (dmax - dmin) * numpy.arange(self._nbins + 1) / float(self._nbins)
Beispiel #9
0
    def filter_size(self, solid_bin, cutoff):
        lw, num = measurements.label(solid_bin)
        minLab = np.min(lw)
        maxLab = np.max(lw)
        print("labels:  min: {}   max: {}".format(minLab, maxLab), flush=True)
        hist = measurements.histogram(lw, minLab + 1, maxLab, maxLab - minLab)
        print("histogram shape: {}".format(hist.shape))
        for i, hi in enumerate(hist):
            if i % 100000 == 0:
                print("i: {}".format(i))
            if hi < cutoff:
                #print("   hi: {}   i: {}".format(hi, i))
                lw[lw == hi] = 0

        lw[lw != 0] = 1

        return lw, hist
def calculate_threshold(image, trusted_range):
  from scipy.ndimage.measurements import histogram
  from thresholding import maximum_deviation
  import numpy

  # Cap pixels to within trusted range
  image.shape = -1
  ind = numpy.where(image < trusted_range[0])
  image[ind] = trusted_range[0]
  ind = numpy.where(image > trusted_range[1])
  image[ind] = trusted_range[1]

  # Histogram the pixels
  histo = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1])
  histo = histo / numpy.sum(histo)

  # Calculate the threshold and add to list
  return maximum_deviation(histo)
def select_contigious(index, shape, conn):
    """ Filter the indices to only contain contigious pixels

  Params:
      index The input array of indices
      shape The shape of the mask
      conn The connectivity (4 or 8)

  Returns:
      The filtered indices

  """
    from scipy.ndimage.measurements import label, histogram
    from numpy import zeros, int32, argmax, where
    from operator import mul

    # Set the label structure
    if conn == 8:
        structure = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    else:
        structure = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]

    # Create a mask of the roi size
    mask = zeros(reduce(mul, shape), dtype=int32)

    # Set all the points at the indices to 1
    for i in index:
        mask[i] = 1

    # Reshape the mask
    mask.shape = shape

    # Label the indices in the mask
    regions, nregions = label(mask, structure)

    # Histogram the regions to get the largest
    histo = histogram(regions, 0, nregions + 1, nregions + 1)

    # Find largest region
    max_index = argmax(histo[1:]) + 1

    # Return only those region indices that are equal to max_index
    regions.shape = -1
    return flex.size_t(where(regions == max_index)[0])
def select_contigious(index, shape, conn):
    """Filter the indices to only contain contigious pixels

    Params:
        index The input array of indices
        shape The shape of the mask
        conn The connectivity (4 or 8)

    Returns:
        The filtered indices

    """
    from scipy.ndimage.measurements import label, histogram
    from numpy import zeros, int32, argmax, where
    from operator import mul

    # Set the label structure
    if conn == 8:
        structure = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    else:
        structure = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]

    # Create a mask of the roi size
    mask = zeros(reduce(mul, shape), dtype=int32)

    # Set all the points at the indices to 1
    for i in index:
        mask[i] = 1

    # Reshape the mask
    mask.shape = shape

    # Label the indices in the mask
    regions, nregions = label(mask, structure)

    # Histogram the regions to get the largest
    histo = histogram(regions, 0, nregions + 1, nregions + 1)

    # Find largest region
    max_index = argmax(histo[1:]) + 1

    # Return only those region indices that are equal to max_index
    regions.shape = -1
    return flex.size_t(where(regions == max_index)[0])
Beispiel #13
0
def gen_feature_mfcc(x):
	v, asp = x
	try:
		if np.isnan(v).any() or np.isinf(v).any():
			raise Exception('MFCC contains Nan of Inf')
		xn, (xmin, xmax), xmean, xvar, xskew, xkurt =  stats.describe(v)
		d = np.diff(v, 1, 0)
		dmean = np.mean(d, 0)
		feat = np.reshape((measurements.center_of_mass(v)[0],
			measurements.center_of_mass(asp)[0], measurements.maximum_position(asp)[0]),(3,))
		hist = 1.0*measurements.histogram(asp, 0, 1, 4) / asp.shape[0]/asp.shape[1]
		x = np.concatenate((xmean,xmin,xmax,xvar,dmean,feat,hist[-1:]))
		if np.isnan(x).any() or np.isinf(x).any():
			raise Exception('Feature vector contains Nan of Inf')
	except:
		#print d, dd
		print xmin.shape
		raise
	return x.T
Beispiel #14
0
def calculate_threshold(image, trusted_range):
    from scipy.ndimage.measurements import histogram
    from thresholding import maximum_deviation
    import numpy

    # Cap pixels to within trusted range
    image.shape = -1
    ind = numpy.where(image < trusted_range[0])
    image[ind] = trusted_range[0]
    ind = numpy.where(image > trusted_range[1])
    image[ind] = trusted_range[1]

    # Histogram the pixels
    histo = histogram(image, trusted_range[0], trusted_range[1],
                      trusted_range[1])
    histo = histo / numpy.sum(histo)

    # Calculate the threshold and add to list
    return maximum_deviation(histo)
def gen_feature_mfcc(x):
    v, asp = x
    try:
        if np.isnan(v).any() or np.isinf(v).any():
            raise Exception('MFCC contains Nan of Inf')
        xn, (xmin, xmax), xmean, xvar, xskew, xkurt = stats.describe(v)
        d = np.diff(v, 1, 0)
        dmean = np.mean(d, 0)
        feat = np.reshape((measurements.center_of_mass(v)[0],
                           measurements.center_of_mass(asp)[0],
                           measurements.maximum_position(asp)[0]), (3, ))
        hist = 1.0 * measurements.histogram(asp, 0, 1,
                                            4) / asp.shape[0] / asp.shape[1]
        x = np.concatenate((xmean, xmin, xmax, xvar, dmean, feat, hist[-1:]))
        if np.isnan(x).any() or np.isinf(x).any():
            raise Exception('Feature vector contains Nan of Inf')
    except:
        #print d, dd
        print xmin.shape
        raise
    return x.T
Beispiel #16
0
 def clean_not_attached(self, bin_image):
     sh = bin_image.shape
     sh = np.asarray(sh)
     sh = sh + 2
 
     aux = np.ones(shape=(sh))
     aux[1:-1, 1:-1, 1:-1] = bin_image
 
     lw, num = measurements.label(aux)
 
     # get a label of the biggest cluster
     minLab = np.min(lw)
     maxLab = np.max(lw)
     print("labels:  min: {}   max: {}".format(minLab, maxLab), flush=True)
 
     hist = measurements.histogram(lw, minLab + 1, maxLab, maxLab - minLab)
 
     # maxCl = np.max(hist)
     maxClLab = np.argmax(hist) + 1
     print("label of a biggest cluster: {}".format(maxClLab), flush=True)
     aux[lw != maxClLab] = 0
     return aux[1:-1, 1:-1, 1:-1]
Beispiel #17
0
 def _updateHistogram (self,hmin=None,hmax=None):
   """Recomputes histogram. If no arguments, computes full histogram for
   data subset. If hmin/hmax is specified, computes zoomed-in histogram.""";
   busy = BusyIndicator();
   self._prev_range = self._display_range;
   dmin,dmax = self._subset_range;
   hmin0,hmax0 = dmin,dmax;
   if hmin0 >= hmax0:
     hmax0 = hmin0+1;
   subset,mask = self.image.optimalRavel(self._subset);
   # compute full-subset hi-res histogram, if we don't have one (for percentile stats)
   if self._hist_hires is None:
     dprint(1,"computing histogram for full subset range",hmin0,hmax0);
     self._hist_hires = measurements.histogram(subset,hmin0,hmax0,self.NumHistBinsHi,labels=mask,index=None if mask is None else False);
     self._hist_bins_hires = hmin0 + (hmax0-hmin0)*(numpy.arange(self.NumHistBinsHi)+0.5)/float(self.NumHistBinsHi);
     self._hist_binsize_hires =  (hmax0-hmin0)/self.NumHistBins;
   # if hist limits not specified, then compute lo-res histogram based on the hi-res one
   if hmin is None:
     hmin,hmax = hmin0,hmax0;
     # downsample to low-res histogram
     self._hist = self._hist_hires.reshape((self.NumHistBins,self.NumHistBinsHi/self.NumHistBins)).sum(1);
   else:
     # zoomed-in low-res histogram
     # bracket limits at subset range
     hmin,hmax = max(hmin,dmin),min(hmax,dmax);
     if hmin >= hmax:
       hmax = hmin+1;
     dprint(1,"computing histogram for",self._subset.shape,self._subset.dtype,hmin,hmax);
     self._hist = measurements.histogram(subset,hmin,hmax,self.NumHistBins,labels=mask,index=None if mask is None else False);
   dprint(1,"histogram computed");
   # compute bins
   self._itf_bins = hmin + (hmax-hmin)*(numpy.arange(self.NumItfBins))/(float(self.NumItfBins)-1);
   self._hist_bins = hmin + (hmax-hmin)*(numpy.arange(self.NumHistBins)+0.5)/float(self.NumHistBins);
   # histogram range and position of peak
   self._hist_range = hmin,hmax;
   self._hist_min,self._hist_max,self._hist_imin,self._hist_imax = measurements.extrema(self._hist);
   self._hist_peak = self._hist_bins[self._hist_imax];
   # set controls accordingly
   if dmin >= dmax:
     dmax = dmin+1;
   zoom = math.log10((dmax-dmin)/(hmax-hmin));
   self._whistzoom.setValue(zoom);
   self._whistunzoom.setEnabled(zoom>0);
   self._whistzoomout.setEnabled(zoom>0);
   # reset scales
   self._histplot.setAxisScale(QwtPlot.xBottom,hmin,hmax);
   self._histplot.setAxisScale(QwtPlot.yRight,0,1+self.ColorBarHeight);
   # update curves
   # call _setHistLogScale() (with current setting) to update axis scales and set data
   self._setHistLogScale(self._ylogscale,replot=False);
   # set plot lines
   self._line_0.line.setData([0,0],[0,1]);
   self._line_0.marker.setValue(0,0);
   self._line_maxbin.line.setData([self._hist_peak,self._hist_peak],[0,1]);
   self._line_maxbin.marker.setValue(self._hist_peak,0);
   self._line_maxbin.setText(("max bin:"+DataValueFormat)%self._hist_peak);
   # set half-max line
   self._line_halfmax.line.setData(self._hist_range,[self._hist_max/2,self._hist_max/2]);
   self._line_halfmax.marker.setValue(hmin,self._hist_max/2);
   # update ITF
   self._updateITF();
Beispiel #18
0
def get_imgs(path, imdir, job_order, f_job=None, img_save=None, csv_save=None):
    """Function to handle the acquired images, do renaming,
    max projections etc."""
    if f_job is None:
        f_job = 2
    if img_save is None:
        img_save = True
    if csv_save is None:
        csv_save = True
    img_paths = Directory(path).get_all_files('*' + job_order + '*.tif')
    new_paths = []
    metadata_d = {}
    for img_path in img_paths:
        img = File(img_path)
        img_array = img.read_image()
        well = img.get_name('U\d\d--V\d\d')
        job_order = img.get_name('E\d\d')
        job_ord_int = int(re.sub("\D", "", job_order))
        field = img.get_name('X\d\d--Y\d\d')
        z_slice = img.get_name('Z\d\d')
        channel = img.get_name('C\d\d')
        if job_ord_int == f_job:
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice + '--' +
                                                            channel +
                                                            '.ome.tif'
                                                            )
                                                     )
                                        )
        elif job_ord_int == f_job + 1 and channel == 'C00':
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C01.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C01'
        elif job_ord_int == f_job + 1 and channel == 'C01':
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C02.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C02'
        elif job_ord_int == f_job + 2:
            new_name = os.path.normpath(os.path.join(path, (well + '--' +
                                                            job_order + '--' +
                                                            field + '--' +
                                                            z_slice +
                                                            '--C03.ome.tif'
                                                            )
                                                     )
                                        )
            channel = 'C03'
        else:
            new_name = img_path
        if not (len(img_array) == 16 or len(img_array) == 256):
            new_paths.append(new_name)
            metadata_d[well + '--' + field + '--' + channel] = img.meta_data()
        os.rename(img_path, new_name)
    max_projs = make_proj(new_paths)
    new_dir = os.path.normpath(os.path.join(imdir, 'maxprojs'))
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
    if img_save:
        print('Saving images')
    if csv_save:
        print('Calculating histograms')
    for channel, proj in max_projs.iteritems():
        if img_save:
            ptime = time.time()
            p = os.path.normpath(os.path.join(new_dir, 'image--' + well + '--' +
                                 field + '--' + channel + '.ome.tif'))
            metadata = metadata_d[well + '--' + field + '--' + channel]
            File(p).save_image(proj, metadata)
            print('Save image:' + str(time.time()-ptime) + ' secs')
        if csv_save:
            ptime = time.time()
            if proj.dtype.name == 'uint8':
                max_int = 255
            if proj.dtype.name == 'uint16':
                max_int = 65535
            histo = histogram(proj, 0, max_int, 256)
            rows = defaultdict(list)
            for b, count in enumerate(histo):
                rows[b].append(count)
            p = os.path.normpath(os.path.join(new_dir, well + '--' + channel +
                                 '.ome.csv'))
            write_csv(p, rows, ['bin', 'count'])
            print('Save csv:' + str(time.time()-ptime) + ' secs')
    return
Beispiel #19
0
    def phase3(self):
        voids_dir = os.path.join(self.__resDir__, 'voids')

        # choose only seg voids to clean
        dirs = [
            d for d in os.listdir(voids_dir)
            if (os.path.isdir(os.path.join(voids_dir, d)) and "seg_void" in d)
        ]
        dirs = sorted(dirs)

        for i, dr in enumerate(dirs):
            curdir = os.path.join(voids_dir, dr)

            tif_files = sorted([f for f in os.listdir(curdir)])
            for ind, filename in enumerate(tif_files):
                print('\n*********************************************',
                      flush=True)
                print('  Processing file {}'.format(filename), flush=True)
                print('  In directory {}'.format(dr), flush=True)
                print('*********************************************',
                      flush=True)
                im_class = io.imread(os.path.join(curdir, filename),
                                     plugin='tifffile')
                im_bin = np.logical_not(im_class.astype(bool)).astype(int)

                slsl = im_bin.astype(np.uint8)

                # clean boundaries in slsl
                cleanCl = True
                while cleanCl:

                    lw, num = measurements.label(slsl)

                    minLab = np.min(lw)
                    maxLab = np.max(lw)
                    print("labels:  min: {}   max: {}".format(minLab, maxLab),
                          flush=True)

                    hist = measurements.histogram(lw, minLab + 1, maxLab,
                                                  maxLab - minLab)

                    maxClLab = np.argmax(hist) + 1

                    print("label of a biggest cluster: {}".format(maxClLab),
                          flush=True)
                    indMaxCl = np.where(lw == maxClLab)

                    ccc = False
                    for ii, indx in enumerate(indMaxCl):
                        minI = min(indx)
                        maxI = max(indx)

                        if minI == 0 or maxI == lw.shape[ii] - 1:
                            ccc = True
                            break

                    if ccc:
                        slsl[lw == maxClLab] = 0
                    else:
                        cleanCl = False

                lw, num = measurements.label(slsl)

                minLab = np.min(lw)
                maxLab = np.max(lw)
                print("labels:  min: {}   max: {}".format(minLab, maxLab),
                      flush=True)

                hist = measurements.histogram(lw, minLab + 1, maxLab,
                                              maxLab - minLab)

                maxClLab = np.argmax(hist) + 1
                slsl[lw != maxClLab] = 0

                inlw = slsl.astype(np.uint8)
                inlw *= 255  # 65535 #256

                io.imsave(os.path.join(curdir, filename),
                          inlw,
                          plugin='tifffile')
Beispiel #20
0
  stop = 3
  for i, flex_image in enumerate(sweep[start:stop]):

    image = flex_image.as_numpy_array()

    image.shape = -1
    ind = numpy.where(image < trusted_range[0])
    image[ind] = trusted_range[0]
    ind = numpy.where(image > trusted_range[1])
    image[ind] = trusted_range[1]

    mean = numpy.mean(image)
    sdev = numpy.std(image)
    var = sdev **2

    temp = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1])
    temp = temp / numpy.sum(temp)
    histo.append(temp)

    #threshold = bhthreshold(temp)
    threshold = maximum_deviation(temp)
    #threshold = percentage_threshold(temp)
#        threshold = normal_threshold(temp)
    threshold_list.append(threshold)


    print "{0} - Mean: {1}, Sdev: {2}, Var: {3}, Thres: {4}".format(
        i, mean, sdev, var, threshold)

  print "Threshold - Mean: {0}, Sdev: {1}".format(
      numpy.mean(threshold_list), numpy.std(threshold_list))
Beispiel #21
0
    stop = 3
    for i, flex_image in enumerate(sweep[start:stop]):

        image = flex_image.as_numpy_array()

        image.shape = -1
        ind = numpy.where(image < trusted_range[0])
        image[ind] = trusted_range[0]
        ind = numpy.where(image > trusted_range[1])
        image[ind] = trusted_range[1]

        mean = numpy.mean(image)
        sdev = numpy.std(image)
        var = sdev ** 2

        temp = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1])
        temp = temp / numpy.sum(temp)
        histo.append(temp)

        # threshold = bhthreshold(temp)
        threshold = maximum_deviation(temp)
        # threshold = percentage_threshold(temp)
        #        threshold = normal_threshold(temp)
        threshold_list.append(threshold)

        print(
            "{0} - Mean: {1}, Sdev: {2}, Var: {3}, Thres: {4}".format(
                i, mean, sdev, var, threshold
            )
        )
Beispiel #22
0
 def _updateHistogram(self, hmin=None, hmax=None):
     """Recomputes histogram. If no arguments, computes full histogram for
     data subset. If hmin/hmax is specified, computes zoomed-in histogram."""
     busy = BusyIndicator()
     self._prev_range = self._display_range
     dmin, dmax = self._subset_range
     hmin0, hmax0 = dmin, dmax
     if hmin0 >= hmax0:
         hmax0 = hmin0 + 1
     subset, mask = self.image.optimalRavel(self._subset)
     # compute full-subset hi-res histogram, if we don't have one (for percentile stats)
     if self._hist_hires is None:
         dprint(1, "computing histogram for full subset range", hmin0, hmax0)
         self._hist_hires = measurements.histogram(subset, hmin0, hmax0, self.NumHistBinsHi, labels=mask,
                                                   index=None if mask is None else False)
         self._hist_bins_hires = hmin0 + (hmax0 - hmin0) * (numpy.arange(self.NumHistBinsHi) + 0.5) / float(
             self.NumHistBinsHi)
         self._hist_binsize_hires = (hmax0 - hmin0) / self.NumHistBins
     # if hist limits not specified, then compute lo-res histogram based on the hi-res one
     if hmin is None:
         hmin, hmax = hmin0, hmax0
         # downsample to low-res histogram
         self._hist = self._hist_hires.reshape((self.NumHistBins, self.NumHistBinsHi / self.NumHistBins)).sum(1)
     else:
         # zoomed-in low-res histogram
         # bracket limits at subset range
         hmin, hmax = max(hmin, dmin), min(hmax, dmax)
         if hmin >= hmax:
             hmax = hmin + 1
         dprint(1, "computing histogram for", self._subset.shape, self._subset.dtype, hmin, hmax)
         self._hist = measurements.histogram(subset, hmin, hmax, self.NumHistBins, labels=mask,
                                             index=None if mask is None else False)
     dprint(1, "histogram computed")
     # compute bins
     self._itf_bins = hmin + (hmax - hmin) * (numpy.arange(self.NumItfBins)) / (float(self.NumItfBins) - 1)
     self._hist_bins = hmin + (hmax - hmin) * (numpy.arange(self.NumHistBins) + 0.5) / float(self.NumHistBins)
     # histogram range and position of peak
     self._hist_range = hmin, hmax
     self._hist_min, self._hist_max, self._hist_imin, self._hist_imax = measurements.extrema(self._hist)
     self._hist_peak = self._hist_bins[self._hist_imax]
     # set controls accordingly
     if dmin >= dmax:
         dmax = dmin + 1
     zoom = math.log10((dmax - dmin) / (hmax - hmin))
     self._whistzoom.setValue(zoom)
     self._whistunzoom.setEnabled(zoom > 0)
     self._whistzoomout.setEnabled(zoom > 0)
     # reset scales
     self._histplot.setAxisScale(QwtPlot.xBottom, hmin, hmax)
     self._histplot.setAxisScale(QwtPlot.yRight, 0, 1 + self.ColorBarHeight)
     # update curves
     # call _setHistLogScale() (with current setting) to update axis scales and set data
     self._setHistLogScale(self._ylogscale, replot=False)
     # set plot lines
     self._line_0.line.setData([0, 0], [0, 1])
     self._line_0.marker.setValue(0, 0)
     self._line_maxbin.line.setData([self._hist_peak, self._hist_peak], [0, 1])
     self._line_maxbin.marker.setValue(self._hist_peak, 0)
     self._line_maxbin.setText(("max bin:" + DataValueFormat) % self._hist_peak)
     # set half-max line
     self._line_halfmax.line.setData(self._hist_range, [self._hist_max / 2, self._hist_max / 2])
     self._line_halfmax.marker.setValue(hmin, self._hist_max / 2)
     # update ITF
     self._updateITF()