def image_to_hist_arr(arr, vmin=None, vmax=None, nbins=None): amin = math.floor(arr.min() if vmin is None else vmin) amax = math.ceil(arr.max() if vmax is None else vmax) awid = math.fabs(amax - amin) if math.fabs(amin) < 0.01 * awid: amin = -0.01 * awid #mean, std = arr.mean(), arr.std() #amin, amax = mean-2*std, mean+10*std if amin == amax: amax += 1 nhbins = int(amax - amin) if nbins is None else nbins NBINS_MIN = 2 if arr.dtype == np.int else 100 NBINS_MAX = (1 << 15) - 1 #print 'XXX:NBINS_MAX', NBINS_MAX if nhbins > NBINS_MAX: nhbins = NBINS_MAX if nhbins < NBINS_MIN: nhbins = NBINS_MIN #print 'XXX arr.shape:\n', arr.shape #print 'XXX amin, amax, nhbins:\n', amin, amax, nhbins #print 'XXX arr.mean(), arr.std():\n', arr.mean(), arr.std() hb = HBins((amin, amax), nhbins) values = hb.bin_count(arr) return amin, amax, nhbins, values
def add_hist(self, values, edges, pen=QtGui.QPen(Qt.black), brush=QtGui.QBrush(), vtype=np.float) : nbins = len(values) #if isinstance(values, (list,tuple)) else values.size hb = HBins(edges, nbins) #, vtype) binl = hb.binedgesleft() binr = hb.binedgesright() v0 = 0 hb.values = values self.lst_hbins.append(hb) points = [QtCore.QPointF(binl[0], v0),] # first point for bl, br, v in zip(binl, binr, values) : points.append(QtCore.QPointF(bl, v)) points.append(QtCore.QPointF(br, v)) points.append(QtCore.QPointF(binr[-1], v0)) # last point path = QtGui.QPainterPath() polygon = QtGui.QPolygonF(points) path.addPolygon(polygon) self._add_path_to_scene(path, pen, brush) self.set_limits_horizontal(amin=binl[0], amax=binr[-1]) self.set_limits() self.update_my_scene() self.check_axes_limits_changed() self.emit(QtCore.SIGNAL('histogram_updated()'))
def __init__(self, edges, nbins=None, pbits=0): """ Constructor - edges - sequence of bin edges - nbins - number of bins in spectrum, if None - edges are used - pbits - print control bits; =0 - print nothing, 1 - object attributes. """ self.hbins = HBins(edges, nbins, vtype=np.float32) self.pbits = pbits self.is_inited = False if self.pbits: self.print_attrs()
def fraser_bins(fraser_img, dist_pix, dqv=0) : """Returns horizontal and vertical HBins objects for pixels in units of k=1 Fraser imaging array, returned by method fraser(...). Units: sample to detector distance dist_pix given in pixels, dqv - normalized offset for qv (for l=1 etc.) """ from pyimgalgos.HBins import HBins rows,cols = fraser_img.shape # this is how Fraser's image pixel defined relative to the scale factor dist_pix qhmax = 0.5*cols/dist_pix qvmax = 0.5*rows/dist_pix qh_bins = HBins((-qhmax, qhmax), cols, vtype=np.float32) qv_bins = HBins((-qvmax+dqv, qvmax+dqv), rows, vtype=np.float32) return qh_bins, qv_bins
def image_to_hist_arr(arr): amin, amax = math.floor(arr.min()), math.ceil(arr.max()) #mean, std = arr.mean(), arr.std() #amin, amax = mean-2*std, mean+10*std nbins = int(amax - amin) NBINS_MAX = (1 << 15) - 1 print 'XXX:NBINS_MAX', NBINS_MAX if nbins > NBINS_MAX: nbins = NBINS_MAX #print 'XXX arr.shape:\n', arr.shape #print 'XXX amin, amax, nbins:\n', amin, amax, nbins #print 'XXX arr.mean(), arr.std():\n', arr.mean(), arr.std() hb = HBins((amin, amax), nbins) values = hb.bin_count(arr) return amin, amax, nbins, values
class HSpectrum: def __init__(self, edges, nbins=None, pbits=0): """ Constructor - edges - sequence of bin edges - nbins - number of bins in spectrum, if None - edges are used - pbits - print control bits; =0 - print nothing, 1 - object attributes. """ self.hbins = HBins(edges, nbins, vtype=np.float32) self.pbits = pbits self.is_inited = False if self.pbits: self.print_attrs() def print_attrs(self): """ Prints object essential attributes """ hb = self.hbins print 'Class %s object attributes:' % (self.__class__.__name__) print 'Binning mode: %s, where True/False for equal/variable size bins' % ( hb.equalbins()) print 'Number of bins: %d' % hb.nbins() print 'Bin edges: %s' % str(hb.edges()) print 'vmin = %f\nvmax = %f' % (hb.vmin(), hb.vmax()) print 'pbits: %d' % (self.pbits) #self.hbins.print_attrs() #self.hbins.print_attrs_defined() def init_spectrum(self, nda): """ Initialization of the spectral histogram array at 1-st entrance in fill(nda) - nda - numpy n-d array with intensities for spectral histogram. """ self.ashape = nda.shape self.asize = nda.size self.hshape = (self.asize, self.hbins.nbins()) self.histarr = np.zeros(self.hshape, dtype=np.uint16) # huge size array self.pix_inds = np.array(range(self.asize), dtype=np.uint32) if self.pbits & 1: print 'n-d array shape = %s, size = %d, dtype = %s' % (str( self.ashape), self.asize, str(nda.dtype)) print 'histogram shape = %s, size = %d, dtype = %s' % (str( self.hshape), self.histarr.size, str(self.histarr.dtype)) self.is_inited = True def fill(self, nda): """ Fills n-d array spectrum histogram-array - nda - numpy n-d array with intensities for spectral histogram. """ if not self.is_inited: self.init_spectrum(nda) shape_in = nda.shape if len(shape_in) > 1: nda.shape = (self.asize, ) # reshape to 1-d bin_inds = self.hbins.bin_indexes(nda, edgemode=0) self.histarr[self.pix_inds, bin_inds] += 1 if len(shape_in) > 1: nda.shape = shape_in # return original shape def spectrum(self): """ Returns accumulated n-d array spectrum, histogram bin edges, and number of bins """ return self.histarr, self.hbins.edges(), self.hbins.nbins()
def __init__(self): if PLOT_TIME_CH: self.lst_u1 = [] self.lst_u2 = [] self.lst_v1 = [] self.lst_v2 = [] self.lst_w1 = [] self.lst_w2 = [] self.lst_mcp = [] if PLOT_NHITS: self.lst_nhits_u1 = [] self.lst_nhits_u2 = [] self.lst_nhits_v1 = [] self.lst_nhits_v2 = [] self.lst_nhits_w1 = [] self.lst_nhits_w2 = [] self.lst_nhits_mcp = [] if PLOT_UVW or PLOT_CORRELATIONS: self.lst_u_ns = [] self.lst_v_ns = [] self.lst_w_ns = [] self.lst_u = [] self.lst_v = [] self.lst_w = [] if PLOT_TIME_SUMS or PLOT_CORRELATIONS: self.lst_time_sum_u = [] self.lst_time_sum_v = [] self.lst_time_sum_w = [] self.lst_time_sum_u_corr = [] self.lst_time_sum_v_corr = [] self.lst_time_sum_w_corr = [] if PLOT_XY_COMPONENTS: self.lst_Xuv = [] self.lst_Xuw = [] self.lst_Xvw = [] self.lst_Yuv = [] self.lst_Yuw = [] self.lst_Yvw = [] if PLOT_MISC: self.lst_Deviation = [] self.lst_consist_indicator = [] self.lst_rec_method = [] if PLOT_XY_RESOLUTION: self.lst_binx = [] self.lst_biny = [] self.lst_resol_fwhm = [] if PLOT_REFLECTIONS: self.lst_refl_u1 = [] self.lst_refl_u2 = [] self.lst_refl_v1 = [] self.lst_refl_v2 = [] self.lst_refl_w1 = [] self.lst_refl_w2 = [] if PLOT_XY_2D: # images nbins = 360 self.img_x_bins = HBins((-45., 45.), nbins, vtype=np.float32) self.img_y_bins = HBins((-45., 45.), nbins, vtype=np.float32) self.img_xy_uv = np.zeros((nbins, nbins), dtype=np.float32) self.img_xy_uw = np.zeros((nbins, nbins), dtype=np.float32) self.img_xy_vw = np.zeros((nbins, nbins), dtype=np.float32) self.img_xy_1 = np.zeros((nbins, nbins), dtype=np.float32) self.img_xy_2 = np.zeros((nbins, nbins), dtype=np.float32) if PLOT_PHYSICS: t_ns_nbins = 400 self.t_ns_bins = HBins((0., 8000.), t_ns_nbins, vtype=np.float32) self.t1_vs_t0 = np.zeros((t_ns_nbins, t_ns_nbins), dtype=np.float32) x_mm_nbins = 160 y_mm_nbins = 160 self.x_mm_bins = HBins((-20., 20.), x_mm_nbins, vtype=np.float32) self.y_mm_bins = HBins((-20., 20.), y_mm_nbins, vtype=np.float32) self.x_vs_t0 = np.zeros((x_mm_nbins, t_ns_nbins), dtype=np.float32) self.y_vs_t0 = np.zeros((y_mm_nbins, t_ns_nbins), dtype=np.float32)
import sys import os import math import numpy as np from Detector.GlobalUtils import print_ndarr import pyimgalgos.GlobalGraphics as gg #from pyimgalgos.GlobalGraphics import hist1d, show, move_fig, save_fig, move, save, fig_axes, plot_img, plot_peaks_on_img #------------------------------ R_EVALD = 0.484187 # [1/A] sigma_qh = 0.003 * R_EVALD #from pyimgalgos.FiberIndexing import BinPars from pyimgalgos.HBins import HBins bpq = HBins((-0.25, 0.25), 1500) #bpq = HBins((-0.25, 0.25), 500) bpomega = HBins((0., 180.), 360) #------------------------------ #------------------------------ def list_omega_qhrow(): """Returns a test list of parameters [(omega, <1-d-array-of-intensities-for-omega>)] """ from time import time t0_sec = time() lst_omega_qhrow = []