def asciiHistogram(histogram, log=False, width=60, label='length', maxLabelWidth=10): (values,edges)=histogram[:2] maxValue=max(values) centers=[int(float(sum(edges[i:i+2]))/2.) for i in range(len(values))] largestLabel = max(max([len(str(c)) for c in centers]),len(label)) if largestLabel<6: largestLabel=6 elif largestLabel>maxLabelWidth: largestLabel=maxLabelWidth plotWidth=width-largestLabel+1 midPoint = npexp((nplog(maxValue)-nplog(.5))/2) if log else maxValue/2 output="%s|count%s%s|%s%s|\n" % (rightPad(label,largestLabel), "".join([" " for i in range(plotWidth/2 - len(str(int(midPoint))) - len("count"))]), str(int(midPoint)), "".join([" " for i in range(int(ceil(plotWidth/2.)) - 1 - len(str(int(maxValue))))]), str(int(maxValue)), ) #output+="%s|%s\n" % ("".join(["_" for i in range(largestLabel)]), # "".join(["_" for i in range(plotWidth)]), # ) for i, v in enumerate(values): output+="%s|%s\n" % (rightPad(str(centers[i]),largestLabel),getBarString(v, maxValue, plotWidth, log)) return output
def log_return(close, length=None, cumulative=None, offset=None, **kwargs): """Indicator: Log Return""" # Validate Arguments length = int(length) if length and length > 0 else 1 cumulative = bool( cumulative) if cumulative is not None and cumulative else False close = verify_series(close, length) offset = get_offset(offset) if close is None: return # Calculate Result if cumulative: # log_return = nplog(close).diff(length).cumsum() log_return = nplog(close / close.iloc[0]) else: log_return = nplog(close / close.shift(length)) # nplog(close).diff(length) # Offset if offset != 0: log_return = log_return.shift(offset) # Handle fills if "fillna" in kwargs: log_return.fillna(kwargs["fillna"], inplace=True) if "fill_method" in kwargs: log_return.fillna(method=kwargs["fill_method"], inplace=True) # Name & Category log_return.name = f"{'CUM' if cumulative else ''}LOGRET_{length}" log_return.category = "performance" return log_return
def fisher(High, Low, length=None): ''' Fisher Transform Indicator https://www.tradingview.com/script/og7JPrRA-CM-Williams-Vix-Fix-Finds-Market-Bottoms/ ''' length = int(length) if length and length > 0 else 5 # Calculate Result m = High.size hl2_ = (High + Low) / 2 max_high = hl2_.rolling(length).max() min_low = hl2_.rolling(length).min() hl2_range = max_high - min_low hl2_range[hl2_range < 1e-5] = 0.001 position = (hl2_ - min_low) / hl2_range v = 0 fish = 0 result = [npNaN for _ in range(0, length - 1)] for i in range(length - 1, m): v = 0.66 * (position[i] - 0.5) + 0.67 * v if v > 0.99: v = 0.999 if v < -0.99: v = -0.999 fish = 0.5 * (fish + nplog((1 + v) / (1 - v))) result.append(fish) fisher = Series(result) return fisher
def log_return(close, length=None, cumulative=False, offset=None, **kwargs): """Indicator: Log Return""" # Validate Arguments close = verify_series(close) length = int(length) if length and length > 0 else 1 offset = get_offset(offset) # Calculate Result log_return = nplog(close).diff(periods=length) if cumulative: log_return = log_return.cumsum() # Offset if offset != 0: log_return = log_return.shift(offset) # Handle fills if "fillna" in kwargs: log_return.fillna(kwargs["fillna"], inplace=True) if "fill_method" in kwargs: log_return.fillna(method=kwargs["fill_method"], inplace=True) # Name & Category log_return.name = f"{'CUM' if cumulative else ''}LOGRET_{length}" log_return.category = "performance" return log_return
def softmax_cost( theta, nclasses, dim, wdecay, data, labels ): # unroll parameters from theta theta = reshape(theta,(dim, nclasses)) # This was wrong theta= theta.T nsamp = data.shape[1] # generate ground truth matrix onevals = squeeze(ones((1,nsamp))) rows = squeeze(labels)-1 # This was wrong cols = arange(nsamp) ground_truth = csr_matrix((onevals,(rows,cols))).todense() # compute hypothesis; use some in-place computations theta_dot_prod = dot(theta,data) theta_dot_prod = theta_dot_prod - numpy.amax(theta_dot_prod, axis=0) # This was wrong soft_theta = npexp(theta_dot_prod) soft_theta_sum = npsum(soft_theta,axis=0) soft_theta_sum = tile(soft_theta_sum,(nclasses,1)) hyp = soft_theta/soft_theta_sum # compute cost log_hyp = nplog(hyp) temp = array(multiply(ground_truth,log_hyp)) temp = npsum(npsum(temp,axis=1),axis=0) cost = (-1.0/nsamp)*temp + 0.5*wdecay*pow(norm(theta,'fro'),2) return cost
def fisher(high, low, length=None, signal=None, offset=None, **kwargs): """Indicator: Fisher Transform (FISHT)""" # Validate Arguments length = int(length) if length and length > 0 else 9 signal = int(signal) if signal and signal > 0 else 1 _length = max(length, signal) high = verify_series(high, _length) low = verify_series(low, _length) offset = get_offset(offset) if high is None or low is None: return # Calculate Result hl2_ = hl2(high, low) highest_hl2 = hl2_.rolling(length).max() lowest_hl2 = hl2_.rolling(length).min() hlr = high_low_range(highest_hl2, lowest_hl2) hlr[hlr < 0.001] = 0.001 position = ((hl2_ - lowest_hl2) / hlr) - 0.5 v = 0 m = high.size result = [npNaN for _ in range(0, length - 1)] + [0] for i in range(length, m): v = 0.66 * position.iloc[i] + 0.67 * v if v < -0.99: v = -0.999 if v > 0.99: v = 0.999 result.append(0.5 * (nplog((1 + v) / (1 - v)) + result[i - 1])) fisher = Series(result, index=high.index) signalma = fisher.shift(signal) # Offset if offset != 0: fisher = fisher.shift(offset) signalma = signalma.shift(offset) # Handle fills if "fillna" in kwargs: fisher.fillna(kwargs["fillna"], inplace=True) signalma.fillna(kwargs["fillna"], inplace=True) if "fill_method" in kwargs: fisher.fillna(method=kwargs["fill_method"], inplace=True) signalma.fillna(method=kwargs["fill_method"], inplace=True) # Name and Categorize it _props = f"_{length}_{signal}" fisher.name = f"FISHERT{_props}" signalma.name = f"FISHERTs{_props}" fisher.category = signalma.category = "momentum" # Prepare DataFrame to return data = {fisher.name: fisher, signalma.name: signalma} df = DataFrame(data) df.name = f"FISHERT{_props}" df.category = fisher.category return df
def drawdown(close, offset=None, **kwargs) -> DataFrame: """Indicator: Drawdown (DD)""" # Validate Arguments close = verify_series(close) offset = get_offset(offset) # Calculate Result max_close = close.cummax() dd = max_close - close dd_pct = 1 - (close / max_close) _np_err = seterr() seterr(divide="ignore", invalid="ignore") dd_log = nplog(max_close) - nplog(close) seterr(divide=_np_err["divide"], invalid=_np_err["invalid"]) # Offset if offset != 0: dd = dd.shift(offset) dd_pct = dd_pct.shift(offset) dd_log = dd_log.shift(offset) # Handle fills if "fillna" in kwargs: dd.fillna(kwargs["fillna"], inplace=True) dd_pct.fillna(kwargs["fillna"], inplace=True) dd_log.fillna(kwargs["fillna"], inplace=True) if "fill_method" in kwargs: dd.fillna(method=kwargs["fill_method"], inplace=True) dd_pct.fillna(method=kwargs["fill_method"], inplace=True) dd_log.fillna(method=kwargs["fill_method"], inplace=True) # Name and Categorize it dd.name = "DD" dd_pct.name = f"{dd.name}_PCT" dd_log.name = f"{dd.name}_LOG" dd.category = dd_pct.category = dd_log.category = "performance" # Prepare DataFrame to return data = {dd.name: dd, dd_pct.name: dd_pct, dd_log.name: dd_log} df = DataFrame(data) df.name = dd.name df.category = dd.category return df
def getBarString(value, maxValue, maxWidth, log): """ return string of various signs (-,~,=,#) based on value and scale """ if log: value=nplog(value)-nplog(.5) if value>0 else 0 maxValue=nplog(maxValue)-nplog(.5) width=maxWidth*(value/float(maxValue)) if width<1: return '' char=logChars[0] s=char while len(s)<width: if log: #print "s: %s, mw: %s, w: %s" % (s, maxWidth, width) char=logChars[int(ceil(len(logChars)*len(s)/float(maxWidth))-1)] s+=char return s
def vec_survives(t_beg, t_end, count_, λ, μ, ρ): f = nplog(2) * count_ for n in range(count_.size): for hs in range(int(count_[n])): t = uniform(t_end, t_beg) if survives(t, λ[n], μ[n], ρ): f[n] = -inf break return f
def fisher(high, low, length=None, signal=None, offset=None, **kwargs): """Indicator: Fisher Transform (FISHT)""" # Validate Arguments high = verify_series(high) low = verify_series(low) length = int(length) if length and length > 0 else 9 signal = int(signal) if signal and signal > 0 else 5 offset = get_offset(offset) # Calculate Result m = high.size hl2_ = hl2(high, low) max_high = hl2_.rolling(length).max() min_low = hl2_.rolling(length).min() hl2_range = max_high - min_low hl2_range[hl2_range < 1e-5] = 0.001 position = (hl2_ - min_low) / hl2_range v = 0 fish = 0 result = [npNaN for _ in range(0, length - 1)] for i in range(length - 1, m): v = 0.66 * (position[i] - 0.5) + 0.67 * v if v > 0.99: v = 0.999 if v < -0.99: v = -0.999 fish = 0.5 * (fish + nplog((1 + v) / (1 - v))) result.append(fish) fisher = Series(result, index=high.index) signalma = ema(fisher, length=signal) # Offset if offset != 0: fisher = fisher.shift(offset) signalma = signalma.shift(offset) # Handle fills if "fillna" in kwargs: fisher.fillna(kwargs["fillna"], inplace=True) signalma.fillna(kwargs["fillna"], inplace=True) if "fill_method" in kwargs: fisher.fillna(method=kwargs["fill_method"], inplace=True) signalma.fillna(method=kwargs["fill_method"], inplace=True) # Name and Categorize it _props = f"_{length}_{signal}" fisher.name = f"FISHERT{_props}" signalma.name = f"FISHERTs{_props}" fisher.category = signalma.category = "momentum" # Prepare DataFrame to return data = {fisher.name: fisher, signalma.name: signalma} df = DataFrame(data) df.name = f"FISHERT{_props}" df.category = fisher.category return df
def recordHz(self): chunk = self.stream.read(self.CHUNK_SIZE) npdata = fromstring(chunk, dtype=int16) #sample_width = p.get_sample_size(FORMAT) npdata = self.silence(npdata) Y = fft.rfft(npdata, self.CHUNK_SIZE) Y_abs = nplog(absolute(Y) + 1) return Y_abs
def getBarString(value, maxValue, maxWidth, log): """ return string of various signs (-,~,=,#) based on value and scale """ if log: value = nplog(value) - nplog(.5) if value > 0 else 0 maxValue = nplog(maxValue) - nplog(.5) width = maxWidth * (value / float(maxValue)) if width < 1: return '' char = logChars[0] s = char while len(s) < width: if log: # print "s: %s, mw: %s, w: %s" % (s, maxWidth, width) char = logChars[int( ceil(len(logChars) * len(s) / float(maxWidth)) - 1)] s += char return s
def vec_survives(t_beg, t_end, count_, λ_α, λ_β, μ_α, μ_β, ρ): f = nplog(2) * count_ for n in prange(count_.size): for hs in range(int(count_[n])): t = uniform(t_end, t_beg) if survives(t, λ_α[n:n + 1], λ_β[n:n + 1], μ_α[n:n + 1], μ_β[n:n + 1], ρ): f[n] = -inf break return f
def ascii_histogram(histogram, log=False, width=60, label='length', maxLabelWidth=10): (values, edges) = histogram[:2] maxValue = max(values) centers = [ int(float(sum(edges[i:i + 2])) / 2.) for i in range(len(values)) ] largestLabel = max(max([len(str(c)) for c in centers]), len(label)) if largestLabel < 6: largestLabel = 6 elif largestLabel > maxLabelWidth: largestLabel = maxLabelWidth plotWidth = width - largestLabel + 1 midPoint = npexp((nplog(maxValue) - nplog(.5)) / 2) \ if log else maxValue / 2 midPointStr = str(int(midPoint)) padding_a_width = int(plotWidth / 2) - len(midPointStr) - len("count") padding_a = "".join([" " for i in range(padding_a_width)]) maxValueStr = str(int(maxValue)) padding_b_width = int(ceil(plotWidth / 2.)) - 1 - len(maxValueStr) padding_b = "".join([" " for i in range(padding_b_width)]) output = "%s|count%s%s|%s%s|\n" % ( rightPad(label, largestLabel), padding_a, midPointStr, padding_b, maxValueStr, ) # output+="%s|%s\n" % ("".join(["_" for i in range(largestLabel)]), # "".join(["_" for i in range(plotWidth)]), # ) for i, v in enumerate(values): output += "%s|%s\n" % (rightPad(str(centers[i]), largestLabel), getBarString(v, maxValue, plotWidth, log)) return output
def entropy(close, length=None, offset=None, **kwargs): """Indicator: Shannon Entropy""" # Validate Arguments close = verify_series(close) length = int(length) if length and length > 0 else 10 offset = get_offset(offset) src = close # Calculate Result p = src / src.rolling(length).sum() ep = -(p * nplog(p) / nplog(2)).rolling(length).sum() # Offset if offset != 0: ep = ep.shift(offset) # Name & Category ep.name = f"ENTROPY_{length}" ep.category = 'statistics' return ep
def tiehom2020(im, plan, nr_threads=2): """Process a tomographic projection image with the Generalized TIE-HOM (Paganin et al 2020) phase retrieval algorithm. Parameters ---------- im : array_like Flat corrected image data as numpy array. plan : structure Structure with pre-computed data (see tiehom_plan function). nr_threads : int Number of threads to be used in the computation of FFT by PyFFTW (default = 2). """ # Extract plan values: dim0_o = plan['dim0'] dim1_o = plan['dim1'] n_pad0 = plan['npad0'] n_pad1 = plan['npad1'] marg0 = (n_pad0 - dim0_o) / 2 marg1 = (n_pad1 - dim1_o) / 2 den = plan['den'] mu = plan['mu'] # Pad image (if required): im = padImage(im, n_pad0, n_pad1) # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = rfft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = rfft2(im) # Apply Paganin's (pre-computed) formula: im = im / den # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = irfft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = irfft2(im) im = im.astype(float32) im = -1 / mu * nplog(im) # Return cropped output: return im[marg0:dim0_o + marg0, marg1:dim1_o + marg1]
def tiehom(im, plan, nr_threads=2): """Process a tomographic projection image with the TIE-HOM (Paganin's) phase retrieval algorithm. Parameters ---------- im : array_like Flat corrected image data as numpy array. plan : structure Structure with pre-computed data (see tiehom_plan function). nr_threads : int Number of threads to be used in the computation of FFT by PyFFTW (default = 2). """ # Extract plan values: dim0_o = plan['dim0'] dim1_o = plan['dim1'] n_pad0 = plan['npad0'] n_pad1 = plan['npad1'] marg0 = (n_pad0 - dim0_o) / 2 marg1 = (n_pad1 - dim1_o) / 2 den = plan['den'] mu = plan['mu'] # Pad image (if required): im = padImage(im, n_pad0, n_pad1) # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = rfft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = rfft2(im) # Apply Paganin's (pre-computed) formula: im = im / den # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = irfft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = irfft2(im) im = im.astype(float32) im = -1 / mu * nplog(im) # Return cropped output: return im[marg0:dim0_o + marg0, marg1:dim1_o + marg1]
def fisher(high, low, length=None, offset=None, **kwargs): """Indicator: Fisher Transform (FISHT)""" # Validate Arguments high = verify_series(high) low = verify_series(low) length = int(length) if length and length > 0 else 5 offset = get_offset(offset) # Calculate Result m = high.size hl2_ = hl2(high, low) max_high = hl2_.rolling(length).max() min_low = hl2_.rolling(length).min() hl2_range = max_high - min_low hl2_range[hl2_range < 1e-5] = 0.001 position = (hl2_ - min_low) / hl2_range v = 0 fish = 0 result = [npNaN for _ in range(0, length - 1)] for i in range(length - 1, m): v = 0.66 * (position[i] - 0.5) + 0.67 * v if v > 0.99: v = 0.999 if v < -0.99: v = -0.999 fish = 0.5 * (fish + nplog((1 + v) / (1 - v))) result.append(fish) fisher = Series(result) # Offset if offset != 0: fisher = fisher.shift(offset) # Handle fills if 'fillna' in kwargs: fisher.fillna(kwargs['fillna'], inplace=True) if 'fill_method' in kwargs: fisher.fillna(method=kwargs['fill_method'], inplace=True) # Name and Categorize it fisher.name = f"FISHERT_{length}" fisher.category = 'momentum' return fisher
def Compute_SigmaLevel(self, trace1, trace2, nbins=20): # From a set of traces, bin by number of standard deviations L, xbins, ybins = histogram2d(trace1, trace2, nbins) L[L == 0] = 1E-16 logL = nplog(L) shape = L.shape L = L.ravel() #Obtain the indices to sort and unsort the flattened array i_sort = argsort(L)[::-1] i_unsort = argsort(i_sort) L_cumsum = L[i_sort].cumsum() L_cumsum /= L_cumsum[-1] xbins = 0.5 * (xbins[1:] + xbins[:-1]) ybins = 0.5 * (ybins[1:] + ybins[:-1]) return xbins, ybins, L_cumsum[i_unsort].reshape(shape)
def log_return(close, length=None, cumulative=False, offset=None, **kwargs): """Indicator: Log Return""" # Validate Arguments close = verify_series(close) length = int(length) if length and length > 0 else 1 offset = get_offset(offset) # Calculate Result log_return = nplog(close).diff(periods=length) if cumulative: log_return = log_return.cumsum() # Offset if offset != 0: log_return = log_return.shift(offset) # Name & Category log_return.name = f"{'CUM' if cumulative else ''}LOGRET_{length}" log_return.category = 'performance' return log_return
def cost(self, theta): """ This function computes the cost associated to the softmax classifier. :param data: data matrix :math:`D_1\in\mathbb{R}^{d \\times n}`, where :math:`d` is the dimensionality and :math:`n` is the number of training samples :type data: numpy array """ # unroll parameters from theta theta = reshape(theta, (self.dim, self.nclasses)) theta = theta.T nsamp = self.data.shape[1] # generate ground truth matrix onevals = squeeze(ones((1, nsamp))) rows = squeeze(self.labels) - 1 cols = arange(nsamp) ground_truth = csr_matrix((onevals, (rows, cols))).todense() # compute hypothesis; use some in-place computations theta_dot_prod = dot(theta, self.data) theta_dot_prod = theta_dot_prod - numpy.amax(theta_dot_prod, axis=0) soft_theta = npexp(theta_dot_prod) soft_theta_sum = npsum(soft_theta, axis=0) soft_theta_sum = tile(soft_theta_sum, (self.nclasses, 1)) hyp = soft_theta / soft_theta_sum # compute cost log_hyp = nplog(hyp) temp = array(multiply(ground_truth, log_hyp)) temp = npsum(npsum(temp, axis=1), axis=0) cost = (-1.0 / nsamp) * temp + 0.5 * self.wdecay * pow(norm(theta, "fro"), 2) thetagrad = (-1.0 / nsamp) * dot(ground_truth - hyp, transpose(self.data)) + self.wdecay * theta thetagrad = thetagrad.flatten(1) return cost, thetagrad
def lnprob_gaussCurve(p, x, y, zerolev, err): resid = residual_gauss(p, x, y, err, zerolev) return -0.5 * sum(((resid - y) / err)**2 + nplog(2 * pi * err**2))
def slope_and_intercept(slope = m_0): prob_slope = nplog(1. / (1. + slope ** 2)) return prob_slope
def log_posterior_likelihood_of_outlier(y_with_outlier, mu, spread_vector, inlier, mean_outliers, spread_outliers): inlier_posterior = sum(inlier * (nplog(2 * pi * spread_vector ** 2) + (y_with_outlier - mu) ** 2 / (spread_vector ** 2))) outlier_posterior = sum((1 - inlier) * (nplog(2 * pi * ((spread_vector ** 2) + (spread_outliers ** 2))) + (y_with_outlier - mean_outliers) ** 2 / ((spread_vector ** 2) + (spread_outliers ** 2)))) return -0.5 * (inlier_posterior + outlier_posterior)
def logmeanexp_preds(output): output = torch.stack(output, dim=0) n_models = len(output) output = log_softmax(output, dim=-1) output = torch.logsumexp(output, dim=0) - nplog(n_models) # [k*n, n] return output
def reconstruct(im, angles, offset, logtransform, recpar, circle, scale, pad, method, zerone_mode, dset_min, dset_max, corr_offset, rolling, roll_shift, tmppath): """Reconstruct a sinogram with the specified reconstruction method (or algorithm). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). """ offset = int(round(offset)) # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im.shape[1] im = imresize(im, (im.shape[0], int(round(scale * im.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Apply transformation for changes in the center of rotation: if (offset != 0): if (offset >= 0): im = im[:,:-offset] tmp = im[:,0] # Get first column tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times im = concatenate((tmp.T,im), axis=1) # Concatenate tmp before the image else: im = im[:,abs(offset):] tmp = im[:,im.shape[1] - 1] # Get last column tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times im = concatenate((im,tmp.T), axis=1) # Concatenate tmp after the image # Sinogram rolling (if required). It doesn't make sense in limited angle tomography, so check if 180 or 360: if ((rolling == True) and (roll_shift > 0)): if ( (angles - pi) < finfo(float32).eps ): # Flip the last rows: im[-roll_shift:,:] = im[-roll_shift:,::-1] # Now roll the sinogram: im = roll(im, roll_shift, axis=0) elif ((angles - pi*2.0) < finfo(float32).eps): # Only roll the sinogram: im = roll(im, roll_shift, axis=0) # Scale image to [0,1] range (if required): if (zerone_mode): #im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: im = (im - numpy.amin(im[:])) / (numpy.amax(im[:]) - numpy.amin(im[:])) # Apply log transform: im[im <= finfo(float32).eps] = finfo(float32).eps if (logtransform == True): im = -nplog(im + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im.shape[1] n_pad = im.shape[1] + im.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: im = padSmoothWidth(im, n_pad) # Perform the actual reconstruction: if (method.startswith('FBP')): im = recon_astra_fbp(im, angles, method, recpar) elif (method == 'MR-FBP_CUDA'): im = recon_mr_fbp(im, angles) elif (method == 'FISTA-TV_CUDA'): lam, fgpiter, tviter = recpar.split(":") lam = float32(lam) fgpiter = int(fgpiter) tviter = int(tviter) im = recon_fista_tv(im, angles, lam, fgpiter, tviter) #elif (method == 'SIRT-FBP_CUDA'): # im = recon_sirt_fbp(im, angles, int(recpar), tmppath ) # # Clean SIRT-FBP cache: # elif (method == 'GRIDREC'): [im, im] = recon_gridrec(im, im, angles, recpar) else: im = recon_astra_iterative(im, angles, method, recpar, zerone_mode) # Crop: if (pad): im = im[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im = imresize(im, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Return output: return im.astype(float32)
def reconstruct(im, angles, angles_projfrom, angles_projto, offset, logtransform, param1, circle, scale, pad, method, zerone_mode, dset_min, dset_max, corr_offset, postprocess_required, convert_opt, crop_opt, start, end, outpath, sino_idx, downsc_factor, logfilename, lock, slice_prefix): """Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). """ # Copy required due to multithreading: im_f = im # Decimate projections if required: #if decim_factor > 1: # im = im[::decim_factor,:] # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im_f.shape[1] im_f = imresize(im_f, (im_f.shape[0], int(round(scale * im_f.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Loop for all the required offsets for the center of rotation: for i in range(int(round(start)), int(round(end)) + 1, downsc_factor): offset = int(round(i / downsc_factor)) # Apply transformation for changes in the center of rotation: if (offset != 0): if (offset >= 0): im_f = im_f[:, :-offset] tmp = im_f[:, 0] # Get first column tmp = tile(tmp, ( offset, 1)) # Replicate the first column the right number of times im_f = concatenate((tmp.T, im_f), axis=1) # Concatenate tmp before the image else: im_f = im_f[:, abs(offset):] tmp = im_f[:, im_f.shape[1] - 1] # Get last column tmp = tile( tmp, (abs(offset), 1)) # Replicate the last column the right number of times im_f = concatenate((im_f, tmp.T), axis=1) # Concatenate tmp after the image # Downscale projections (without pixel averaging): #if downsc_factor > 1: # im = im[:,::downsc_factor] # Scale image to [0,1] range (if required): if (zerone_mode): #print dset_min #print dset_max #print numpy.amin(im_f[:]) #print numpy.amax(im_f[:]) #im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: im_f = (im_f - numpy.amin(im_f[:])) / (numpy.amax(im_f[:]) - numpy.amin(im_f[:])) # Apply log transform: if (logtransform == True): im_f[im_f <= finfo(float32).eps] = finfo(float32).eps im_f = -nplog(im_f + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im_f.shape[1] n_pad = im_f.shape[1] + im_f.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: #im_f = padSmoothWidth(im_f, n_pad) im_f = padImage(im_f, im_f.shape[0], n_pad) # Perform the actual reconstruction: if (method.startswith('FBP')): im_f = recon_astra_fbp(im_f, angles, method, param1) elif (method == 'MR-FBP_CUDA'): im_f = recon_mr_fbp(im_f, angles) elif (method == 'FISTA-TV_CUDA'): im_f = recon_fista_tv(im_f, angles, param1, param1) elif (method == 'MLEM'): im_f = recon_tomopy_iterative(im_f, angles, method, param1) elif (method == 'GRIDREC'): [im_f, im_f] = recon_gridrec(im_f, im_f, angles, param1) else: im_f = recon_astra_iterative(im_f, angles, method, param1, zerone_mode) # Crop: if (pad): im_f = im_f[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im_f = imresize(im_f, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Apply post-processing (if required): if postprocess_required: im_f = croprescale(im_f, convert_opt, crop_opt) else: # Create the circle mask for fancy output: if (circle == True): siz = im_f.shape[1] if siz % 2: rang = arange(-siz / 2 + 1, siz / 2 + 1) else: rang = arange(-siz / 2, siz / 2) x, y = meshgrid(rang, rang) z = x**2 + y**2 a = (z < (siz / 2 - int(round(abs(offset) / downsc_factor)))**2) im_f = im_f * a # Write down reconstructed image (file name modified with metadata): if (i >= 0): fname = outpath + slice_prefix + '_' + str(sino_idx).zfill( 4) + '_proj=' + str( angles_projto - angles_projfrom) + '_col=' + str( (im_f.shape[1] + offset) * downsc_factor).zfill(4) + '_off=+' + str( abs(offset * downsc_factor)).zfill(4) + '.tif' else: fname = outpath + slice_prefix + '_' + str(sino_idx).zfill( 4) + '_proj=' + str( angles_projto - angles_projfrom) + '_col=' + str( (im_f.shape[1] + offset) * downsc_factor).zfill(4) + '_off=-' + str( abs(offset * downsc_factor)).zfill(4) + '.tif' imsave(fname, im_f) # Restore original image for next step: im_f = im # Write log (atomic procedure - lock used): write_log(lock, fname, logfilename)
def invsp(x): return nplog(npexp(x) - 1)
def lnprob_gaussMix(p, x, y, zerolev, err, components_index): resid = residual_gaussMix(p, x, y, err, zerolev, components_index) return -0.5 * sum(((resid - y) / err)**2 + nplog(2 * pi * err**2))
def pre_processing(dset, rebinning, flatfielding_window, despeckle_thresh, \ output_low, output_high, output_diff, output_sum, mode, \ crop, proj_avg_mode, proj_avg_alpha, dering_thresh): """ Perform pre-processing composed of the following steps: - Crop (at first to speed-up everything else) - Projection averaging (if 4D-data with Nan compensation) - Removal of the first column (Pixirad has a bad first column) - Create energy integrated image (if required) - Flat fielding - Rebinning (if required) - Despeckle with NaNs and Infs removal - Ring removal """ # Init low = None high = None diff = None sum = None # Get the portion of the image to process: lim = [dset.low.shape[0] - crop[1], dset.low.shape[1] - crop[3]] # Crop (check if 4D-data or 3D-data): if (dset.low.ndim == 4): low = dset.low[crop[0]:lim[0], crop[2]:lim[1], :, :] flat_low = dset.flat_low[crop[0]:lim[0], crop[2]:lim[1], :, :] else: low = dset.low[crop[0]:lim[0], crop[2]:lim[1], :] flat_low = dset.flat_low[crop[0]:lim[0], crop[2]:lim[1], :] # Projection averaging (only for 4D-data): if (low.ndim == 4): low = kst_matrix_manipulation.proj_averaging(low, proj_avg_mode, proj_avg_alpha) flat_low = kst_matrix_manipulation.proj_averaging( flat_low, proj_avg_mode, proj_avg_alpha) # Remove first column (of each image): low[:, 0, :] = low[:, 1, :] flat_low[:, 0, :] = flat_low[:, 1, :] if dset.high is not None: # Crop (check if 4D-data or 3D-data): if (dset.high.ndim == 4): high = dset.high[crop[0]:lim[0], crop[2]:lim[1], :, :] flat_high = dset.flat_high[crop[0]:lim[0], crop[2]:lim[1], :, :] else: high = dset.high[crop[0]:lim[0], crop[2]:lim[1], :] flat_high = dset.flat_high[crop[0]:lim[0], crop[2]:lim[1], :] # Projection averaging (only for 4D-data): if (high.ndim == 4): high = kst_matrix_manipulation.proj_averaging( high, proj_avg_mode, proj_avg_alpha) flat_high = kst_matrix_manipulation.proj_averaging( flat_high, proj_avg_mode, proj_avg_alpha) # Remove first column (of each image): high[:, 0, :] = high[:, 1, :] flat_high[:, 0, :] = flat_high[:, 1, :] # Create energy-integration image (if required): if (output_sum): sum = low + high flat_sum = flat_low + flat_high # Apply flat fielding: low = kst_flat_fielding.flat_fielding(low, flat_low, flatfielding_window) if dset.high is not None: high = kst_flat_fielding.flat_fielding(high, flat_high, flatfielding_window) if (output_sum): sum = kst_flat_fielding.flat_fielding(sum, flat_sum, flatfielding_window) # Apply rebinning (if required): if (rebinning): # Get new size by calling code once: new_im = kst_matrix_manipulation.rebinning2x2(low[:, :, 0]) # Prepare output matrix: new_low = zeros((new_im.shape[0], new_im.shape[1], low.shape[2]), dtype=low.dtype) if dset.high is not None: new_high = zeros((new_im.shape[0], new_im.shape[1], high.shape[2]), dtype=high.dtype) # Do the job: for i in range(0, low.shape[2]): new_low[:, :, i] = kst_matrix_manipulation.rebinning2x2(low[:, :, i]) if dset.high is not None: for i in range(0, high.shape[2]): new_high[:, :, i] = kst_matrix_manipulation.rebinning2x2(high[:, :, i]) # Re-assign: low = new_low if dset.high is not None: high = new_high # Do-it also for the sum image (if required): if (output_sum): new_sum = zeros((new_im.shape[0], new_im.shape[1], sum.shape[2]), dtype=sum.dtype) for i in range(0, sum.shape[2]): new_sum[:, :, i] = kst_matrix_manipulation.rebinning2x2(sum[:, :, i]) sum = new_sum # Correct outliers: low = kst_remove_outliers.despeckle(low, despeckle_thresh, True) # Apply ring removal: #for j in range(0, low.shape[0]): # low[j,:,:] = kst_ring_removal.boinhaibel(low[j,:,:].T, dering_thresh).T if dset.high is not None: # Correct outliers: high = kst_remove_outliers.despeckle(high, despeckle_thresh, True) # Apply ring removal: #for j in range(0, low.shape[0]): # high[j,:,:] = kst_ring_removal.boinhaibel(high[j,:,:].T, dering_thresh).T if (output_sum): # Correct outliers: sum = kst_remove_outliers.despeckle(sum, despeckle_thresh, True) # Apply ring removal: #for j in range(0, low.shape[0]): # sum[j,:,:] = kst_ring_removal.boinhaibel(sum[j,:,:].T, dering_thresh).T # Compute the subtraction image (if required): if (output_diff): diff = nplog(high) - nplog(low) # Correct outliers: #diff = kst_remove_outliers.despeckle(diff, despeckle_thresh, False) # Apply ring removal: #for j in range(0, diff.shape[0]): # diff[j,:,:] = kst_ring_removal.boinhaibel(diff[j,:,:].T, dering_thresh).T # Apply log transform (if required): if (output_low): low = -nplog(low) if high is not None: high = -nplog(high) if (output_sum): sum = -nplog(sum) # Return: return low, high, diff, sum
def phase_retrieval(im, plan, method=1, nr_threads=2): """Process a tomographic projection image with the selected phase retrieval algorithm. Parameters ---------- im : array_like Flat corrected image data as numpy array. plan : structure Structure with pre-computed data (see prepare_plan function) method : int Phase retrieval algorithm {1 = TIE (default), 2 = Born, 3 = Rytov, 4 = Wu} nr_threads : int Number of threads to be used in the computation of FFT by PyFFTW """ # Extract plan values: dim0_o = plan['dim0'] dim1_o = plan['dim1'] n_pad0 = plan['npad0'] n_pad1 = plan['npad1'] marg0 = (n_pad0 - dim0_o) / 2 marg1 = (n_pad1 - dim1_o) / 2 den = plan['den'] mu = plan['mu'] # Pad image (if required): im = padImage(im, n_pad0, n_pad1) # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = fft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = fft2(im) # Apply formula: if method == 1: im = im / den # (Un)comment the following two lines to use PyFFTW: n_byte_align(im, simd_alignment) im = ifft2(im, threads=nr_threads) # (Un)comment the following line to use NumPy: #im = ifft2(im) im = im.astype(complex64) im = real(im) im = im.astype(float32) im = -1 / mu * nplog(im) # # WARNING: The following methods are not tested # elif method == 2: im = real(ifft2((im - 1.0) / 2.0) / den) elif method == 3: im = real(ifft2(nplog(im) / 2.0) / den) elif method == 4: im = real(ifft2(im / den)) im = -1 / 2 * (delta / beta) * nplog(im) # Return cropped output: return im[marg0:dim0_o + marg0, marg1:dim1_o + marg1]
def doornik_hansen(data): """ Perform the Doornik-Hansen test (https://doi.org/10.1111/j.1468-0084.2008.00537.x) This computes and transforms multivariate variants of the skewness and kurtosis, then computes a chi-square statistic on the results. """ data = pandas.DataFrame(data) data = deepcopy(data) n = len(data) p = len(data.columns) # R is the correlation matrix, a scaling of the covariance matrix # R has dimensions dim * dim R = corrcoef(data.transpose()) L, V = eigh(R) for i in range(p): if (L[i] <= 1e-12): L[i] = 0 if (L[i] > 1e-12): L[i] = 1 / sqrt(L[i]) L = diag(L) if (matrix_rank(R) < p): V = pandas.DataFrame(V) G = V.loc[:, (L != 0).any(axis=0)] data = data.dot(G) ppre = p p = data.size / len(data) raise ValueError( "NOTE:Due that some eigenvalue resulted zero, \ a new data matrix was created. Initial number \ of variables = ", ppre, ", were reduced to = ", p) R = corrcoef(data.transpose()) L, V = eigh(R) L = diag(L) means = [list(data.mean())] * n stddev = [list(data.std(ddof=0))] * n Z = (data - pandas.DataFrame(means)) / pandas.DataFrame(stddev) Zp = Z.dot(V) Zpp = Zp.dot(L) st = Zpp.dot(transpose(V)) # skew is the multivariate skewness (dimension dim) # kurt is the multivariate kurtosis (dimension dim) skew = mean(power(st, 3), axis=0) kurt = mean(power(st, 4), axis=0) # Transform the skewness into a standard normal z1 n2 = n * n b = 3 * (n2 + 27 * n - 70) * (n + 1) * (n + 3) b /= (n - 2) * (n + 5) * (n + 7) * (n + 9) w2 = -1 + sqrt(2 * (b - 1)) d = 1 / sqrt(log(sqrt(w2))) y = skew * sqrt((w2 - 1) * (n + 1) * (n + 3) / (12 * (n - 2))) # Use numpy log/sqrt as math versions dont have array input z1 = d * nplog(y + npsqrt(y * y + 1)) # Transform the kurtosis into a standard normal z2 d = (n - 3) * (n + 1) * (n2 + 15 * n - 4) a = (n - 2) * (n + 5) * (n + 7) * (n2 + 27 * n - 70) / (6 * d) c = (n - 7) * (n + 5) * (n + 7) * (n2 + 2 * n - 5) / (6 * d) k = (n + 5) * (n + 7) * (n * n2 + 37 * n2 + 11 * n - 313) / (12 * d) al = a + (skew**2) * c chi = (kurt - 1 - (skew**2)) * k * 2 z2 = (((chi / (2 * al))**(1 / 3)) - 1 + 1 / (9 * al)) * npsqrt(9 * al) kurt -= 3 # omnibus normality statistic DH = z1.dot(z1.transpose()) + z2.dot(z2.transpose()) AS = n / 6 * skew.dot(skew.transpose()) + n / 24 * kurt.dot( kurt.transpose()) # degrees of freedom v = 2 * p # p-values PO = 1 - chi2.cdf(DH, v) PA = 1 - chi2.cdf(AS, v) return DH, AS, PO, PA
stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE) plot_range = CHUNK_SIZE / 2 + 1 # RECORD print 'recording started' while record: chunk = stream.read(CHUNK_SIZE) npdata = fromstring(chunk, dtype=int16) sample_width = p.get_sample_size(FORMAT) #print npdata, sample_width # silence check npdata = silence(npdata) Y = fft.rfft(npdata, CHUNK_SIZE) #print X, len(X) Y_abs = nplog(absolute(Y) + 1) #print Y_abs, len(Y_abs) #print max(Y_abs) record = quit()
# INIT p = PyAudio() stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE) plot_range = CHUNK_SIZE/2 + 1 # RECORD print 'recording started' while record: chunk = stream.read(CHUNK_SIZE) npdata = fromstring(chunk, dtype=int16) sample_width = p.get_sample_size(FORMAT) #print npdata, sample_width # silence check npdata = silence(npdata) Y = fft.rfft(npdata, CHUNK_SIZE) #print X, len(X) Y_abs = nplog(absolute(Y) + 1) #print Y_abs, len(Y_abs) #print max(Y_abs) record = quit()
def log(x): r"""(Natural) log """ return nplog(x)
def log(x): return nplog(x)
def reconstruct_gridrec(im1, im2, angles, offset, logtransform, param1, circle, scale, pad, rolling, roll_shift, zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset): """Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). Example (using tiffile.py) -------------------------- >>> # Read input (uncorrected) sinogram >>> sino_im1 = imread('sino_0050.tif') >>> >>> # Get flat and dark correction images: >>> im_dark = medianize("\project\tomo", "dark*.tif") >>> im_flat = medianize("\project\tomo", "flat*.tif") >>> >>> # Perform flat fielding and normalization: >>> sino_im = normalize(sino_im1, (10,10), (0,0), im_dark, im_flat, 50) >>> >>> # Actual reconstruction: >>> out = reconstruct_fbp(sino_im, -3.0) >>> >>> # Save output slice: >>> imsave('slice_0050.tif', out) """ # Ensure images are of type float32: im_f1 = im1.astype(float32) im_f2 = im2.astype(float32) # Decimate projections if required: #if decim_factor > 1: # im_f1 = im_f1[::decim_factor,:] # im_f2 = im_f2[::decim_factor,:] # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im_f.shape[1] im_f1 = imresize(im_f1, (im_f1.shape[0], int(round(scale * im_f1.shape[1]))), interp='bicubic', mode='F') im_f2 = imresize(im_f2, (im_f2.shape[0], int(round(scale * im_f2.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Apply transformation for changes in the center of rotation: if (offset != 0): if (offset >= 0): im_f1 = im_f1[:,:-offset] tmp = im_f1[:,0] # Get first column tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times im_f1 = concatenate((tmp.T,im_f1), axis=1) # Concatenate tmp before the image im_f2 = im_f2[:,:-offset] tmp = im_f2[:,0] # Get first column tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times im_f2 = concatenate((tmp.T,im_f2), axis=1) # Concatenate tmp before the image else: im_f1 = im_f1[:,abs(offset):] tmp = im_f1[:,im_f1.shape[1] - 1] # Get last column tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times im_f1 = concatenate((im_f1,tmp.T), axis=1) # Concatenate tmp after the image im_f2 = im_f2[:,abs(offset):] tmp = im_f2[:,im_f2.shape[1] - 1] # Get last column tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times im_f2 = concatenate((im_f2,tmp.T), axis=1) # Concatenate tmp after the image # Downscale projections (without pixel averaging): #if downsc_factor > 1: # im_f1 = im_f1[:,::downsc_factor] # im_f2 = im_f2[:,::downsc_factor] # Sinogram rolling (if required). It doesn't make sense in limited angle tomography, so check if 180 or 360: if ((rolling == True) and (roll_shift > 0)): if ( (angles - pi) < finfo(float32).eps ): # Flip the last rows: im_f1[-roll_shift:,:] = im_f1[-roll_shift:,::-1] im_f2[-roll_shift:,:] = im_f2[-roll_shift:,::-1] # Now roll the sinogram: im_f1 = roll(im_f1, roll_shift, axis=0) im_f2 = roll(im_f2, roll_shift, axis=0) elif ((angles - pi*2.0) < finfo(float32).eps): # Only roll the sinogram: im_f1 = roll(im_f1, roll_shift, axis=0) im_f2 = roll(im_f2, roll_shift, axis=0) # Scale image to [0,1] range (if required): if (zerone_mode): #print dset_min #print dset_max #print numpy.amin(im_f[:]) #print numpy.amax(im_f[:]) #im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: im_f1 = (im_f1 - numpy.amin(im_f1[:])) / (numpy.amax(im_f1[:]) - numpy.amin(im_f1[:])) im_f2 = (im_f2 - numpy.amin(im_f2[:])) / (numpy.amax(im_f2[:]) - numpy.amin(im_f2[:])) # Apply log transform: if (logtransform == True): im_f1[im_f1 <= finfo(float32).eps] = finfo(float32).eps im_f1 = -nplog(im_f1 + corr_offset) im_f2[im_f2 <= finfo(float32).eps] = finfo(float32).eps im_f2 = -nplog(im_f2 + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im_f1.shape[1] n_pad = im_f1.shape[1] + im_f1.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: im_f1 = padSmoothWidth(im_f1, n_pad) im_f2 = padSmoothWidth(im_f2, n_pad) # Perform the actual reconstruction: [im_f1, im_f2] = recon_gridrec(im_f1, im_f2, angles, param1) # Crop: if (pad): im_f1 = im_f1[marg:dim_o + marg, marg:dim_o + marg] im_f2 = im_f2[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im_f1 = imresize(im_f1, (siz_orig1, siz_orig1), interp='nearest', mode='F') im_f2 = imresize(im_f2, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Return output: return [im_f1.astype(float32), im_f2.astype(float32)]
def reconstruct(im, angles, offset, logtransform, param1, circle, scale, pad, method, zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset): """Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). Example (using tiffile.py) -------------------------- >>> # Read input (uncorrected) sinogram >>> sino_im1 = imread('sino_0050.tif') >>> >>> # Get flat and dark correction images: >>> im_dark = medianize("\project\tomo", "dark*.tif") >>> im_flat = medianize("\project\tomo", "flat*.tif") >>> >>> # Perform flat fielding and normalization: >>> sino_im = normalize(sino_im1, (10,10), (0,0), im_dark, im_flat, 50) >>> >>> # Actual reconstruction: >>> out = reconstruct_fbp(sino_im, -3.0) >>> >>> # Save output slice: >>> imsave('slice_0050.tif', out) """ # Copy images and ensure they are of type float32: #im_f = copy(im.astype(float32)) im_f = im.astype(float32) # Decimate projections if required: if decim_factor > 1: im_f = im_f[::decim_factor,:] # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im_f.shape[1] im_f = imresize(im_f, (im_f.shape[0], int(round(scale * im_f.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Apply transformation for changes in the center of rotation: if (offset != 0): if (offset >= 0): im_f = im_f[:,:-offset] tmp = im_f[:,0] # Get first column tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times im_f = concatenate((tmp.T,im_f), axis=1) # Concatenate tmp before the image else: im_f = im_f[:,abs(offset):] tmp = im_f[:,im_f.shape[1] - 1] # Get last column tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times im_f = concatenate((im_f,tmp.T), axis=1) # Concatenate tmp after the image # Downscale projections (without pixel averaging): if downsc_factor > 1: im_f = im_f[:,::downsc_factor] # Scale image to [0,1] range (if required): if (zerone_mode): #print dset_min #print dset_max #print numpy.amin(im_f[:]) #print numpy.amax(im_f[:]) #im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: im_f = (im_f - numpy.amin(im_f[:])) / (numpy.amax(im_f[:]) - numpy.amin(im_f[:])) # Apply log transform: if (logtransform == True): im_f[im_f <= finfo(float32).eps] = finfo(float32).eps im_f = -nplog(im_f + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im_f.shape[1] n_pad = im_f.shape[1] + im_f.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: im_f = padSino(im_f, n_pad) # Perform the actual reconstruction: if (method.startswith('FBP')): im_f = recon_astra_fbp(im_f, angles, method, param1) elif (method == 'MR-FBP_CUDA'): im_f = recon_mr_fbp(im_f, angles) elif (method == 'FISTA-TV_CUDA'): im_f = recon_fista_tv(im_f, angles, param1, param1) elif (method == 'MLEM'): im_f = recon_tomopy_iterative(im_f, angles, method, param1) elif (method == 'SCIKIT-FBP'): im_f = recon_scikit_fbp(im_f, angles, param1) elif (method == 'SCIKIT-SART'): im_f = recon_scikit_sart(im_f, angles, param1) else: im_f = recon_astra_iterative(im_f, angles, method, param1, zerone_mode) # Crop: if (pad): im_f = im_f[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im_f = imresize(im_f, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Return output: return im_f.astype(float32)
def homomorphic(im, args): """Process the input image with an homomorphic filtering. Parameters ---------- im : array_like Image data as numpy array. d0 : float Cutoff in the range [0.01, 0.99] of the high pass Gaussian filter. Higher values means more high pass effect. [Suggested for default: 0.80]. alpha : float Offset to preserve the zero frequency where. Higher values means less high pass effect. [Suggested for default: 0.2] (Parameters d0 and alpha have to passed as a string separated by ;) Example (using tiffile.py) -------------------------- >>> im = imread('im_orig.tif') >>> im = homomorphic(im, '0.5;0.2') >>> imsave('im_flt.tif', im) References ---------- """ # Get args: param1, param2 = args.split(";") d0 = (1.0 - float(param1)) # Simpler for user alpha = float(param2) # Internal parameters for Gaussian low-pass filtering: d0 = d0 * (im.shape[1] / 2.0) # Take the log: im = nplog(1 + im) # Compute FFT: n_byte_align(im, simd_alignment) im = rfft2(im, threads=2) # Prepare the frequency coordinates: u = arange(0, im.shape[0], dtype=float32) v = arange(0, im.shape[1], dtype=float32) # Compute the indices for meshgrid: u[(u > im.shape[0] / 2.0)] = u[(u > im.shape[0] / 2.0)] - im.shape[0] v[(v > im.shape[1] / 2.0)] = v[(v > im.shape[1] / 2.0)] - im.shape[1] # Compute the meshgrid arrays: V, U = meshgrid(v, u) # Compute the distances D(U, V): D = sqrt(U ** 2 + V ** 2) # Prepare Guassian filter: H = npexp(-(D ** 2) / (2 * (d0 ** 2))) H = (1 - H) + alpha # Do the filtering: im = H * im # Compute inverse FFT of the filtered data: n_byte_align(im, simd_alignment) #im = real(irfft2(im, threads=2)) im = irfft2(im, threads=2) # Take the exp: im = npexp(im) - 1 # Return image according to input type: return im.astype(float32)
def reconstruct(im, angles, offset, logtransform, recpar, circle, scale, pad, method, zerone_mode, dset_min, dset_max, corr_offset, rolling, roll_shift, tmppath): """Reconstruct a sinogram with the specified reconstruction method (or algorithm). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). """ # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im.shape[1] im = imresize(im, (im.shape[0], int(round(scale * im.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Apply transformation for changes in the center of rotation: if ((method == 'GRIDREC') or (method == 'MR-FBP_CUDA') or (method == 'FISTA-TV_CUDA')): offset = int(round(offset)) if (offset != 0): if (offset >= 0): im = im[:, :-offset] tmp = im[:, 0] # Get first column tmp = tile(tmp, ( offset, 1)) # Replicate the first column the right number of times im = concatenate((tmp.T, im), axis=1) # Concatenate tmp before the image else: im = im[:, abs(offset):] tmp = im[:, im.shape[1] - 1] # Get last column tmp = tile( tmp, (abs(offset), 1)) # Replicate the last column the right number of times im = concatenate((im, tmp.T), axis=1) # Concatenate tmp after the image # Sinogram rolling (if required). It doesn't make sense in limited angle tomography, so check if 180 or 360: if ((rolling == True) and (roll_shift > 0)): if ((angles - pi) < finfo(float32).eps): # Flip the last rows: im[-roll_shift:, :] = im[-roll_shift:, ::-1] # Now roll the sinogram: im = roll(im, roll_shift, axis=0) elif ((angles - pi * 2.0) < finfo(float32).eps): # Only roll the sinogram: im = roll(im, roll_shift, axis=0) # Scale image to [0,1] range (if required): if (zerone_mode): im = (im - dset_min) / (dset_max - dset_min) # Cheating the whole process: #im = (im - numpy.amin(im[:])) / (numpy.amax(im[:]) - numpy.amin(im[:])) # Apply log transform: im[im <= finfo(float32).eps] = finfo(float32).eps if (logtransform == True): im = -nplog(im + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im.shape[1] n_pad = im.shape[1] + im.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: if (method == 'GRIDREC'): im = padSmoothWidth(im, n_pad) else: im = padImage(im, im.shape[0], n_pad) # Perform the actual reconstruction: if (method.startswith('FBP')): im = recon_astra_fbp(im, angles, method, recpar, offset) elif (method == 'MR-FBP_CUDA'): im = recon_mr_fbp(im, angles, offset) elif (method == 'FISTA-TV_CUDA'): lam, fgpiter, tviter = recpar.split(":") lam = float32(lam) fgpiter = int(fgpiter) tviter = int(tviter) im = recon_fista_tv(im, angles, lam, fgpiter, tviter, offset) #elif (method == 'SIRT-FBP_CUDA'): # im = recon_sirt_fbp(im, angles, int(recpar), tmppath ) # # Clean SIRT-FBP cache: # elif (method == 'GRIDREC'): [im, im] = recon_gridrec(im, im, angles, recpar) else: im = recon_astra_iterative(im, angles, method, recpar, zerone_mode, offset) # Crop: if (pad): im = im[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im = imresize(im, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Return output: return im.astype(float32)
def reconstruct(im, angles, offset, logtransform, param1, circle, scale, pad, method, zerone_mode, dset_min, dset_max, corr_offset, postprocess_required, convert_opt, crop_opt, start, end, outpath, sino_idx, downsc_factor, decim_factor, logfilename, lock, slice_prefix): """Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). """ # Copy required due to multithreading: im_f = im # Decimate projections if required: #if decim_factor > 1: # im = im[::decim_factor,:] # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im_f.shape[1] im_f = imresize(im_f, (im_f.shape[0], int(round(scale * im_f.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) offset = int(round(offset)) # Apply transformation for changes in the center of rotation: if (offset != 0): if (offset >= 0): im_f = im_f[:,:-offset] tmp = im_f[:,0] # Get first column tmp = tile(tmp, (offset,1)) # Replicate the first column the right number of times im_f = concatenate((tmp.T,im_f), axis=1) # Concatenate tmp before the image else: im_f = im_f[:,abs(offset):] tmp = im_f[:,im_f.shape[1] - 1] # Get last column tmp = tile(tmp, (abs(offset),1)) # Replicate the last column the right number of times im_f = concatenate((im_f,tmp.T), axis=1) # Concatenate tmp after the image # Downscale projections (without pixel averaging): #if downsc_factor > 1: # im = im[:,::downsc_factor] # Scale image to [0,1] range (if required): if (zerone_mode): #print dset_min #print dset_max #print numpy.amin(im_f[:]) #print numpy.amax(im_f[:]) #im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: im_f = (im_f - numpy.amin(im_f[:])) / (numpy.amax(im_f[:]) - numpy.amin(im_f[:])) # Apply log transform: if (logtransform == True): im_f[im_f <= finfo(float32).eps] = finfo(float32).eps im_f = -nplog(im_f + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im_f.shape[1] n_pad = im_f.shape[1] + im_f.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: im_f = padSmoothWidth(im_f, n_pad) # Loop for all the required angles: for i in range(int(round(start)), int(round(end)) + 1): # Save image for next step: im = im_f # Apply projection removal (if required): im_f = im_f[0:int(round(i/decim_factor)), :] # Perform the actual reconstruction: if (method.startswith('FBP')): im_f = recon_astra_fbp(im_f, angles, method, param1) elif (method == 'MR-FBP_CUDA'): im_f = recon_mr_fbp(im_f, angles) elif (method == 'FISTA-TV_CUDA'): im_f = recon_fista_tv(im_f, angles, param1, param1) elif (method == 'MLEM'): im_f = recon_tomopy_iterative(im_f, angles, method, param1) elif (method == 'GRIDREC'): [im_f, im_f] = recon_gridrec(im_f, im_f, angles, param1) else: im_f = recon_astra_iterative(im_f, angles, method, param1, zerone_mode) # Crop: if (pad): im_f = im_f[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im_f = imresize(im_f, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Apply post-processing (if required): if postprocess_required: im_f = postprocess(im_f, convert_opt, crop_opt) else: # Create the circle mask for fancy output: if (circle == True): siz = im_f.shape[1] if siz % 2: rang = arange(-siz / 2 + 1, siz / 2 + 1) else: rang = arange(-siz / 2,siz / 2) x,y = meshgrid(rang,rang) z = x ** 2 + y ** 2 a = (z < (siz / 2 - int(round(abs(offset)/downsc_factor)) ) ** 2) im_f = im_f * a # Write down reconstructed image (file name modified with metadata): fname = outpath + slice_prefix + '_' + str(sino_idx).zfill(4) + '_off=' + str(offset*downsc_factor).zfill(4) + '_proj=' + str(i).zfill(4) + '.tif' imsave(fname, im_f) # Restore original image for next step: im_f = im # Write log (atomic procedure - lock used): write_log(lock, fname, logfilename )
def homomorphic(im, args): """Process the input image with an homomorphic filtering. Parameters ---------- im : array_like Image data as numpy array. d0 : float Cutoff in the range [0.01, 0.99] of the high pass Gaussian filter. Higher values means more high pass effect. [Suggested for default: 0.80]. alpha : float Offset to preserve the zero frequency where. Higher values means less high pass effect. [Suggested for default: 0.2] (Parameters d0 and alpha have to passed as a string separated by ;) Example (using tiffile.py) -------------------------- >>> im = imread('im_orig.tif') >>> im = homomorphic(im, '0.5;0.2') >>> imsave('im_flt.tif', im) References ---------- """ # Get args: param1, param2 = args.split(";") d0 = (1.0 - float(param1)) # Simpler for user alpha = float(param2) # Internal parameters for Gaussian low-pass filtering: d0 = d0 * (im.shape[1] / 2.0) # Take the log: im = nplog(1 + im) # Compute FFT: n_byte_align(im, simd_alignment) im = rfft2(im, threads=2) # Prepare the frequency coordinates: u = arange(0, im.shape[0], dtype=float32) v = arange(0, im.shape[1], dtype=float32) # Compute the indices for meshgrid: u[(u > im.shape[0] / 2.0)] = u[(u > im.shape[0] / 2.0)] - im.shape[0] v[(v > im.shape[1] / 2.0)] = v[(v > im.shape[1] / 2.0)] - im.shape[1] # Compute the meshgrid arrays: V, U = meshgrid(v, u) # Compute the distances D(U, V): D = sqrt(U**2 + V**2) # Prepare Guassian filter: H = npexp(-(D**2) / (2 * (d0**2))) H = (1 - H) + alpha # Do the filtering: im = H * im # Compute inverse FFT of the filtered data: n_byte_align(im, simd_alignment) #im = real(irfft2(im, threads=2)) im = irfft2(im, threads=2) # Take the exp: im = npexp(im) - 1 # Return image according to input type: return im.astype(float32)
def reconstruct(im, angles, offset, logtransform, param1, circle, scale, pad, method, rolling, roll_shift, zerone_mode, dset_min, dset_max, decim_factor, downsc_factor, corr_offset): """Reconstruct a sinogram with FBP algorithm (from ASTRA toolbox). Parameters ---------- im1 : array_like Sinogram image data as numpy array. center : float Offset of the center of rotation to use for the tomographic reconstruction with respect to the half of sinogram width (default=0, i.e. half width). logtransform : boolean Apply logarithmic transformation before reconstruction (default=True). filter : string Filter to apply before the application of the reconstruction algorithm. Filter types are: ram-lak, shepp-logan, cosine, hamming, hann, tukey, lanczos, triangular, gaussian, barlett-hann, blackman, nuttall, blackman-harris, blackman-nuttall, flat-top, kaiser, parzen. circle : boolean Create a circle in the reconstructed image and set to zero pixels outside the circle (default=False). Example (using tiffile.py) -------------------------- >>> # Read input (uncorrected) sinogram >>> sino_im1 = imread('sino_0050.tif') >>> >>> # Get flat and dark correction images: >>> im_dark = medianize("\project\tomo", "dark*.tif") >>> im_flat = medianize("\project\tomo", "flat*.tif") >>> >>> # Perform flat fielding and normalization: >>> sino_im = normalize(sino_im1, (10,10), (0,0), im_dark, im_flat, 50) >>> >>> # Actual reconstruction: >>> out = reconstruct_fbp(sino_im, -3.0) >>> >>> # Save output slice: >>> imsave('slice_0050.tif', out) """ # Copy images and ensure they are of type float32: #im_f = copy(im.astype(float32)) im_f = im.astype(float32) # Decimate projections if required: #if decim_factor > 1: # im_f = im_f[::decim_factor,:] # Upscale projections (if required): if (abs(scale - 1.0) > finfo(float32).eps): siz_orig1 = im_f.shape[1] im_f = imresize(im_f, (im_f.shape[0], int(round(scale * im_f.shape[1]))), interp='bicubic', mode='F') offset = int(offset * scale) # Apply transformation for changes in the center of rotation (only for Pelt's code): if ((method == 'MR-FBP_CUDA') or (method == 'FISTA-TV_CUDA')): offset = int(round(offset)) if (offset != 0): if (offset >= 0): im_f = im_f[:, :-offset] tmp = im_f[:, 0] # Get first column tmp = tile(tmp, ( offset, 1)) # Replicate the first column the right number of times im_f = concatenate((tmp.T, im_f), axis=1) # Concatenate tmp before the image else: im_f = im_f[:, abs(offset):] tmp = im_f[:, im_f.shape[1] - 1] # Get last column tmp = tile( tmp, (abs(offset), 1)) # Replicate the last column the right number of times im_f = concatenate((im_f, tmp.T), axis=1) # Concatenate tmp after the image # Downscale projections (without pixel averaging): #if downsc_factor > 1: # im_f = im_f[:,::downsc_factor] # Sinogram rolling (if required). It doesn't make sense in limited angle # tomography, so check if 180 or 360: if ((rolling == True) and (roll_shift > 0)): if ((angles - pi) < finfo(float32).eps): # Flip the last rows: im_f[-roll_shift:, :] = im_f[-roll_shift:, ::-1] # Now roll the sinogram: im_f = roll(im_f, roll_shift, axis=0) elif ((angles - pi * 2.0) < finfo(float32).eps): # Only roll the sinogram: im_f = roll(im_f, roll_shift, axis=0) # Scale image to [0,1] range (if required): if (zerone_mode): im_f = (im_f - dset_min) / (dset_max - dset_min) # Cheating the whole process: #im_f = (im_f - numpy.amin(im_f[:])) / (numpy.amax(im_f[:]) - #numpy.amin(im_f[:])) # Apply log transform: if (logtransform == True): im_f[im_f <= finfo(float32).eps] = finfo(float32).eps im_f = -nplog(im_f + corr_offset) # Replicate pad image to double the width: if (pad): dim_o = im_f.shape[1] n_pad = im_f.shape[1] + im_f.shape[1] / 2 marg = (n_pad - dim_o) / 2 # Pad image: #im_f = padSmoothWidth(im_f, n_pad) im_f = padImage(im_f, im_f.shape[0], n_pad) # Perform the actual reconstruction: if (method.startswith('FBP')): im_f = recon_astra_fbp(im_f, angles, method, param1, offset) elif (method == 'MR-FBP_CUDA'): im_f = recon_mr_fbp(im_f, angles, offset) elif (method == 'FISTA-TV_CUDA'): lam, fgpiter, tviter = param1.split(":") lam = float32(lam) fgpiter = int(fgpiter) tviter = int(tviter) im_f = recon_fista_tv(im_f, angles, lam, fgpiter, tviter, offset) else: im_f = recon_astra_iterative(im_f, angles, method, param1, zerone_mode, offset) # Crop: if (pad): im_f = im_f[marg:dim_o + marg, marg:dim_o + marg] # Resize (if necessary): if (abs(scale - 1.0) > finfo(float32).eps): im_f = imresize(im_f, (siz_orig1, siz_orig1), interp='nearest', mode='F') # Return output: return im_f.astype(float32)