def ellipseFilterContours(cnts, minAxisMAX=None, minAxisMIN=None, majAxisMIN=None, majAxisMAX=None): ''' this function filter out the contours that, after an ellipse fitting, have a minor axis higher than a threshold ''' l = amap(len, cnts) cnts = compress(l >= 5, cnts, axis=0) #There should be at least 5 points to fit the ellipse in function fitEllipse boxs = amap(cv2.fitEllipse, cnts) minaxs = amap(lambda b: b[1][0], boxs) majaxs = amap(lambda b: b[1][1], boxs) cond1 = ones(len(boxs), dtype=bool) if minAxisMIN: cond2 = minaxs > minAxisMIN cond1 = cond1 * cond2 if minAxisMAX: cond2 = minaxs < minAxisMAX cond1 = cond1 * cond2 if majAxisMIN: cond2 = majaxs > majAxisMIN cond1 = cond1 * cond2 if majAxisMAX: cond2 = majaxs < majAxisMAX cond1 = cond1 * cond2 cnts = compress(cond1, cnts, axis=0) return cnts, boxs
def win_hl_for_img(img, winsize=32, overlap=0.25): hopsize = int(winsize * overlap) width = img.shape[0] height = img.shape[1] return mlab.amap( lambda x: mlab.amap( lambda y: ms_compute_hl(img[(x):(x + winsize), (y): (y + winsize), :], return_mean=True), range( 0, height, hopsize)), range(0, width, hopsize))
def Adiabats(self, pt, pmin, pmax, species = [], num = 200, endpoint = True): plev = logspace(log10(pmin), log10(pmax), num = num, endpoint = endpoint) result = amap(lambda p: newton(lambda v: self.PotentialT(v, p, species) - pt, pt * (p / self.Pref)**(self.Rdry / self.cpdry)), plev) return plev, result
def Adiabats(self, pt, pmin, pmax, species=[], num=200, endpoint=True): plev = logspace(log10(pmin), log10(pmax), num=num, endpoint=endpoint) result = amap( lambda p: newton(lambda v: self.PotentialT(v, p, species) - pt, pt * (p / self.Pref)**(self.Rdry / self.cpdry)), plev) return plev, result
def findContours(tIm, threshArea, pix2mic): #findcontours find white elements on black background _, contours, _ = cv2.findContours(tIm, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) area = amap(lambda cnt: cv2.contourArea(cnt), contours) contours = compress(area > threshArea / pix2mic**2, contours, axis=0) area = compress(area > threshArea / pix2mic**2, area) return contours, area
def SatMixingR(T, P, sp): if not isinstance(T, ndarray): return SatMixingRScalar(T, P, sp) else: dims = T.shape n = size(T) result = amap(lambda x, y: SatMixingRScalar(x, y, sp), T.reshape(n), P.reshape(n)) return result.reshape(dims)
def VirtualT(self, T, X, s): if not isinstance(T, ndarray): result = self.VirtualTScalar(T, X, s) return result else: # ndarray might be zero shape and looks exactly like a float! dims = T.shape n = size(T) result = amap(lambda x, y: self.VirtualT(x, y, s), T.reshape(n), X.reshape(n, 2)) return result.reshape(dims)
def eps(self, sp): if isinstance(sp, list) or isinstance(sp, ndarray): return amap(lambda s: s.mu / self.mu, sp) else: return sp.mu / self.mu
def ms_compute_hl(image, return_mean=True): band_list = range(get_num_bands(image)) return mlab.amap( lambda x: mh.features.haralick(image[:, :, x].astype(int), return_mean=return_mean), band_list)
def SatVaporP(T, sp): if isinstance(sp, list) or isinstance(sp, ndarray): result = amap(lambda s: 10.**(s.al + s.bl / T), sp) return result.T else: return 10.**(sp.al + sp.bl / T)
def EqCondensation(self, T, P, X, sp, pmin, pmax, num=200): sp = array(sp) XS = array([s.mmr for s in sp]) mixr = amap(min, X, XS) sat = IsSat(T, P, mixr, sp) sat0 = sat.copy() mixr = logical_not(sat) * mixr + sat * XS # mixing ratio is the "real mixing ratio" when it is unsaturated. # But it is the "maximum mixing ratio" if it is saturated. plev = logspace(log10(pmin), log10(pmax), num=num) # logspace doesn't behave too well in python, need to force boundary condition plev[0], plev[-1] = pmin, pmax ilev = where(P <= plev)[0][0] # scan upward i, i0, temp, ptol0 = ilev, ilev, T, P sample_p, sample_t, sample_x, cloud = [], [], [[] for j in range(len(sp)) ], [] pt = self.PotentialT(T, P, sp[sat]) while (i >= 0 and sum(logical_not(sat)) > 0): temp = newton(lambda v: self.PotentialT(v, plev[i], sp[sat]) - pt, temp) newsat = IsSat(temp, plev[i], mixr, sp) if all(equal(sat, newsat)): i -= 1 continue j = where(sat != newsat)[0][0] sol = root(lambda v: self._eq1_(v, sp, sat, pt, mixr, j), (temp, plev[i], plev[i])) temp, ptol, pdry = tuple(sol.x) #print "Find Cloud Layer: T = %8.2f, P = %8.2f, Pdry = %8.2f, Species = " % (temp, ptol, pdry), sp[j] xcloud = array([ mixr[ii] if not sat[ii] else SatVaporP(temp, sp[ii]) / pdry for ii in range(len(sp)) ]) cloud = [CloudLayer(temp, ptol, xcloud, sp[j])] + cloud _sample_p, _sample_t = self.Adiabats(pt, ptol, ptol0, species=sp[sat], num=3 * (i0 - i), endpoint=False) sample_p = hstack((_sample_p, sample_p)) sample_t = hstack((_sample_t, sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis=-1) ) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((_sample_x, sample_x)) sat[j] = True pt = self.PotentialT(temp, ptol, sp[sat]) i0, ptol0 = i, ptol if ptol0 > pmin: _sample_p, _sample_t = self.Adiabats(pt, pmin, ptol0, species=sp, num=3 * i0, endpoint=False) sample_p = hstack((_sample_p, sample_p)) sample_t = hstack((_sample_t, sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis=-1) ) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((_sample_x, sample_x)) # scan downward i, i0, temp, ptol0 = ilev, ilev, T, P sat = sat0.copy() id = len(sample_p) - 1 pt = self.PotentialT(T, P, sp[sat]) while (i < num and sum(sat) > 0): temp = newton(lambda v: self.PotentialT(v, plev[i], sp[sat]) - pt, temp) newsat = IsSat(temp, plev[i], mixr, sp) if all(equal(sat, newsat)): i += 1 continue j = where(sat != newsat)[0][0] sol = root(lambda v: self._eq1_(v, sp, sat, pt, mixr, j), (temp, plev[i], plev[i]), tol=1.E-6) temp, ptol, pdry = tuple(sol.x) #print "Find Cloud Layer: T = %8.2f, P = %8.2f, Pdry = %8.2f, Species = " % (temp, ptol, pdry), sp[j] xcloud = array([ mixr[ii] if not sat[ii] else SatVaporP(temp, sp[ii]) / pdry for ii in range(len(sp)) ]) cloud = cloud + [CloudLayer(temp, ptol, xcloud, sp[j])] _sample_p, _sample_t = self.Adiabats(pt, ptol0, ptol, species=sp[sat], num=3 * (i - i0), endpoint=False) sample_p = hstack((sample_p, _sample_p)) sample_t = hstack((sample_t, _sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis=-1) ) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((sample_x, _sample_x)) sat[j] = False pt = self.PotentialT(temp, ptol, sp[sat]) i0, ptol0 = i, ptol if ptol0 < pmax: _sample_p, _sample_t = self.Adiabats(pt, ptol0, pmax, species=sp[sat], num=3 * (num - i0)) sample_p = hstack((sample_p, _sample_p)) sample_t = hstack((sample_t, _sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis=-1) ) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(_sample_p))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((sample_x, _sample_x)) # interpolation function and cell averaging result = CondensationSolution() result.cloud = cloud result.species = sp result.funcT = interp1d(sample_p, sample_t, kind='linear') result.funcX = interp1d(sample_p, sample_x, kind='linear') def cellT(pres): presw = zeros(len(pres) + 1) presw[0] = pres[0] * pres[0] / sqrt(pres[0] * pres[1]) presw[-1] = pres[-1] * pres[-1] / sqrt(pres[-1] * pres[-2]) for i in range(1, len(pres)): presw[i] = sqrt(pres[i] * pres[i - 1]) return array([ quad(result.funcT, presw[i], presw[i + 1])[0] / (presw[i + 1] - presw[i]) for i in range(len(pres)) ]) def cellX(pres): presw = zeros(len(pres) + 1) presw[0] = pres[0] * pres[0] / sqrt(pres[0] * pres[1]) presw[-1] = pres[-1] * pres[-1] / sqrt(pres[-1] * pres[-2]) for i in range(1, len(pres)): presw[i] = sqrt(pres[i] * pres[i - 1]) cellx = zeros((len(sp), len(pres))) for j in range(len(sp)): funcx = interp1d(sample_p, sample_x[j, :], kind='linear') for i in range(len(pres)): cellx[j, i] = quad(funcx, presw[i], presw[i + 1])[0] / ( presw[i + 1] - presw[i]) return cellx result.sample_p = sample_p result.sample_t = sample_t result.sample_x = sample_x result.cellT = cellT result.cellX = cellX result.make() return result
def EqCondensation(self, T, P, X, sp, pmin, pmax, num = 200): sp = array(sp) XS = array([s.mmr for s in sp]) mixr = amap(min, X, XS) sat = IsSat(T, P, mixr, sp) sat0 = sat.copy() mixr = logical_not(sat) * mixr + sat * XS # mixing ratio is the "real mixing ratio" when it is unsaturated. # But it is the "maximum mixing ratio" if it is saturated. plev = logspace(log10(pmin), log10(pmax), num = num) # logspace doesn't behave too well in python, need to force boundary condition plev[0], plev[-1] = pmin, pmax ilev = where(P <= plev)[0][0] # scan upward i, i0, temp, ptol0 = ilev, ilev, T, P sample_p, sample_t, sample_x, cloud = [], [], [[] for j in range(len(sp))], [] pt = self.PotentialT(T, P, sp[sat]) while (i >= 0 and sum(logical_not(sat)) > 0): temp = newton(lambda v: self.PotentialT(v, plev[i], sp[sat]) - pt, temp) newsat = IsSat(temp, plev[i], mixr, sp) if all(equal(sat, newsat)): i -= 1 continue j = where(sat != newsat)[0][0] sol = root(lambda v: self._eq1_(v, sp, sat, pt, mixr, j), (temp, plev[i], plev[i])) temp, ptol, pdry = tuple(sol.x) #print "Find Cloud Layer: T = %8.2f, P = %8.2f, Pdry = %8.2f, Species = " % (temp, ptol, pdry), sp[j] xcloud = array([mixr[ii] if not sat[ii] else SatVaporP(temp, sp[ii]) / pdry for ii in range(len(sp))]) cloud = [CloudLayer(temp, ptol, xcloud, sp[j])] + cloud _sample_p, _sample_t = self.Adiabats(pt, ptol, ptol0, species = sp[sat], num = 3 * (i0 - i), endpoint = False) sample_p = hstack((_sample_p, sample_p)) sample_t = hstack((_sample_t, sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis = -1)) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((_sample_x, sample_x)) sat[j] = True; pt = self.PotentialT(temp, ptol, sp[sat]) i0, ptol0 = i, ptol if ptol0 > pmin: _sample_p, _sample_t = self.Adiabats(pt, pmin, ptol0, species = sp, num = 3 * i0, endpoint = False) sample_p = hstack((_sample_p, sample_p)) sample_t = hstack((_sample_t, sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis = -1)) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((_sample_x, sample_x)) # scan downward i, i0, temp, ptol0 = ilev, ilev, T, P sat = sat0.copy() id = len(sample_p) - 1 pt = self.PotentialT(T, P, sp[sat]) while (i < num and sum(sat) > 0): temp = newton(lambda v: self.PotentialT(v, plev[i], sp[sat]) - pt, temp) newsat = IsSat(temp, plev[i], mixr, sp) if all(equal(sat, newsat)): i += 1 continue j = where(sat != newsat)[0][0] sol = root(lambda v: self._eq1_(v, sp, sat, pt, mixr, j), (temp, plev[i], plev[i]), tol = 1.E-6) temp, ptol, pdry = tuple(sol.x) #print "Find Cloud Layer: T = %8.2f, P = %8.2f, Pdry = %8.2f, Species = " % (temp, ptol, pdry), sp[j] xcloud = array([mixr[ii] if not sat[ii] else SatVaporP(temp, sp[ii]) / pdry for ii in range(len(sp))]) cloud = cloud + [CloudLayer(temp, ptol, xcloud, sp[j])] _sample_p, _sample_t = self.Adiabats(pt, ptol0, ptol, species = sp[sat], num = 3 * (i - i0), endpoint = False) sample_p = hstack((sample_p, _sample_p)) sample_t = hstack((sample_t, _sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis = -1)) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(pdry))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((sample_x, _sample_x)) sat[j] = False; pt = self.PotentialT(temp, ptol, sp[sat]) i0, ptol0 = i, ptol if ptol0 < pmax: _sample_p, _sample_t = self.Adiabats(pt, ptol0, pmax, species = sp[sat], num = 3 * (num - i0)) sample_p = hstack((sample_p, _sample_p)) sample_t = hstack((sample_t, _sample_t)) pdry = (_sample_p - sum(SatVaporP(_sample_t, sp[sat]), axis = -1)) / (1. + sum(mixr[logical_not(sat)])) _sample_x = zeros((len(sp), len(_sample_p))) for k in range(len(sp)): if sat[k]: _sample_x[k, :] = SatVaporP(_sample_t, sp[k]) / pdry else: _sample_x[k, :] = array([mixr[k]] * len(pdry)) sample_x = hstack((sample_x, _sample_x)) # interpolation function and cell averaging result = CondensationSolution() result.cloud = cloud result.species = sp result.funcT = interp1d(sample_p, sample_t, kind = 'linear') result.funcX = interp1d(sample_p, sample_x, kind = 'linear') def cellT(pres): presw = zeros(len(pres) + 1) presw[0] = pres[0] * pres[0] / sqrt(pres[0] * pres[1]) presw[-1] = pres[-1] * pres[-1] / sqrt(pres[-1] * pres[-2]) for i in range(1, len(pres)): presw[i] = sqrt(pres[i] * pres[i - 1]) return array([quad(result.funcT, presw[i], presw[i + 1])[0] / (presw[i + 1] - presw[i]) for i in range(len(pres))]) def cellX(pres): presw = zeros(len(pres) + 1) presw[0] = pres[0] * pres[0] / sqrt(pres[0] * pres[1]) presw[-1] = pres[-1] * pres[-1] / sqrt(pres[-1] * pres[-2]) for i in range(1, len(pres)): presw[i] = sqrt(pres[i] * pres[i - 1]) cellx = zeros((len(sp), len(pres))) for j in range(len(sp)): funcx = interp1d(sample_p, sample_x[j, :], kind = 'linear') for i in range(len(pres)): cellx[j, i] = quad(funcx, presw[i], presw[i + 1])[0] / (presw[i + 1] - presw[i]) return cellx result.sample_p = sample_p result.sample_t = sample_t result.sample_x = sample_x result.cellT = cellT result.cellX = cellX result.make() return result
def SatVaporP(T, sp): if isinstance(sp, list) or isinstance(sp, ndarray): result = amap(lambda s: 10. ** (s.al + s.bl / T), sp) return result.T else: return 10. ** (sp.al + sp.bl / T)
def LatentH(sp, temp=273.15): if isinstance(sp, list) or isinstance(sp, ndarray): return amap(lambda s: s.Lv, sp) #return amap(lambda s: s.Lv0 - s.dLvdt * (temp - 273.15), sp) else: return sp.Lv
def LatentH(sp, temp = 273.15): if isinstance(sp, list) or isinstance(sp, ndarray): return amap(lambda s: s.Lv, sp) #return amap(lambda s: s.Lv0 - s.dLvdt * (temp - 273.15), sp) else: return sp.Lv
def cutOutRects(rects, img): img_cropped = amap(lambda r: cropROI(r, img), rects) return img_cropped