Ejemplo n.º 1
0
def test_memory_leak():
    import resource

    arr = np.arange(1).reshape((1, 1))

    starting = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

    for i in range(1000):
        for axis in [None, 0, 1]:
            bn.nansum(arr, axis=axis)
            bn.nanargmax(arr, axis=axis)
            bn.nanargmin(arr, axis=axis)
            bn.nanmedian(arr, axis=axis)
            bn.nansum(arr, axis=axis)
            bn.nanmean(arr, axis=axis)
            bn.nanmin(arr, axis=axis)
            bn.nanmax(arr, axis=axis)
            bn.nanvar(arr, axis=axis)

    ending = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

    diff = ending - starting
    diff_bytes = diff * resource.getpagesize()
    print(diff_bytes)
    # For 1.3.0 release, this had value of ~100kB
    assert diff_bytes == 0
Ejemplo n.º 2
0
def test_memory_leak() -> None:
    import resource

    arr = np.arange(1).reshape((1, 1))

    n_attempts = 3
    results = []

    for _ in range(n_attempts):
        starting = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

        for _ in range(1000):
            for axis in [None, 0, 1]:
                bn.nansum(arr, axis=axis)
                bn.nanargmax(arr, axis=axis)
                bn.nanargmin(arr, axis=axis)
                bn.nanmedian(arr, axis=axis)
                bn.nansum(arr, axis=axis)
                bn.nanmean(arr, axis=axis)
                bn.nanmin(arr, axis=axis)
                bn.nanmax(arr, axis=axis)
                bn.nanvar(arr, axis=axis)

        ending = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

        diff = ending - starting
        diff_bytes = diff * resource.getpagesize()
        # For 1.3.0 release, this had value of ~100kB
        if diff_bytes:
            results.append(diff_bytes)
        else:
            break

    assert len(results) < n_attempts
Ejemplo n.º 3
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p): 
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5*(a[w, ind] - o[w, ind] + a[w, n+ind] - o[w, n+ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1) 
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w, where(minres_ind<n, minres_ind+n, minres_ind-n)]
        volume = prod(e-y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n+ind] + o[w, n+ind])
        return [si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i], complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i], volume[i], volumeResidual[i]) for i in range(m)]
        
    else:
        
        residual = None
        tmp = asarray(a)-asarray(o)
        tmp[tmp<1e-300] = 1e-300
        nlhf = log2(tmp)#-log2(p.fTol)
#        nlhf[a==inf] = 1e300# to make it not inf and nan
#        nlhf[o==-inf] = 1e300# to make it not inf and nan
        if nlhf.ndim == 3: # in MOP
            nlhf = nlhf.sum(axis=1)
        
        if p.probType == "MOP":
            # make correct o,a wrt each target
            return [si(MOP_Fields, y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          [o[i][k] for k in range(p.nf)], [a[i][k] for k in range(p.nf)], 
                          _s[i]) for i in range(m)]
        else:
            s, q = o[:, 0:n], o[:, n:2*n]
            Tmp = nanmax(where(q<s, q, s), 1)
            
            nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP')
        
#            residual = None

            return [si(Fields, Tmp[i], y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          o[i], a[i], _s[i]) for i in range(m)]
Ejemplo n.º 4
0
 def compute_draw_info(self, x, ys):
     bs = self.compute_baseline(x, ys)
     im = np.array([bottleneck.nanargmin(abs(x - self.limits[0]))])
     dx = [self.limits[0], self.limits[0]]
     dys = np.hstack((bs[:, im], ys[:, im]))
     return [("curve", (dx, dys, INTEGRATE_DRAW_EDGE_PENARGS)),  # line to value
             ("dot", (x[im], ys[:, im]))]
Ejemplo n.º 5
0
def reducepoints(x, y, n=2000, further=True):
    """
    Reduce the total number of x, y coordinates for plotting. The
    algorithm looks windows roughly one pixel wide and plots the
    minimum and maximum point within that window. NOTE: both the min
    and max for each n will be determined. This will yield a length
    of approximately 2n. If further, remove nonessential points.
    """

    # Can only work on blocks
    if len(x) < n*3: return (x, y)

    # Calculate the block size to average over
    block = int(math.floor(float(len(x))/n))
    newn = int(math.ceil(float(len(x))/block))
    ox, oy = np.zeros(2*newn), np.zeros(2*newn)

    # Search over each block for the min and max y value, order
    # correctly, and add to the output
    for i in range(newn):
        # Avoid just adding NaN's for all NaN blocks
        try:
            pmn = nanargmin(y[i*block:(i + 1)*block])
        except ValueError:
            pmn = 0
        try:
            pmx = nanargmax(y[i*block:(i + 1)*block])
        except ValueError:
            pmx = 0

        if pmn < pmx:
            ox[2*i], oy[2*i] = x[i*block + pmn], y[i*block + pmn]
            ox[2*i + 1], oy[2*i + 1] = x[i*block + pmx], y[i*block + pmx]
        else:
            ox[2*i + 1], oy[2*i + 1] = x[i*block + pmn], y[i*block + pmn]
            ox[2*i], oy[2*i] = x[i*block + pmx], y[i*block + pmx]

    if further:
        last = -1
        match = 0

        # Search through all values and set >= triplets to 0
        for i in range(len(ox)):
            if oy[i] != last:
                last = oy[i]
                match = 0
            else:
                match += 1
                if match > 1:
                    ox[i - 1] = np.nan

        # Eliminate those positions where ox is nan
        if np.sum(np.isnan(ox)) > 0:
            oy = oy[np.isfinite(ox)]
            ox = ox[np.isfinite(ox)]

    return ox, oy
Ejemplo n.º 6
0
def r2(PointVals, PointCoords, dataType):
    r23 = nanargmin(PointVals)
    if isnan(r23):
        r23 = 0
    # TODO: check it , maybe it can be improved
    #bestCenter = cs[r23]
    #r7 = array([(val[0][r23]+val[1][r23]) / 2 for val in domain.values()], dtype=dataType)
    #r8 = atleast_1d(r3)[r23] if not isnan(r23) else inf
    r7 = array(PointCoords[r23], dtype=dataType)
    r8 = atleast_1d(PointVals)[r23] 
    return r7, r8
Ejemplo n.º 7
0
def r2(PointVals, PointCoords, dataType):
    r23 = nanargmin(PointVals)
    if isnan(r23):
        r23 = 0
    # TODO: check it , maybe it can be improved
    #bestCenter = cs[r23]
    #r7 = array([(val[0][r23]+val[1][r23]) / 2 for val in domain.values()], dtype=dataType)
    #r8 = atleast_1d(r3)[r23] if not isnan(r23) else inf
    r7 = array(PointCoords[r23], dtype=dataType)
    r8 = atleast_1d(PointVals)[r23]
    return r7, r8
Ejemplo n.º 8
0
def find_nearest(array, value):
	"""
	Search array for value and return the index where the value is closest.

	Parameters:
		array (ndarray): Array to search.
		value: Value to search array for.

	Returns:
		int: Index of ``array`` closest to ``value``.

	Raises:
		ValueError: If ``value`` is NaN.

	.. codeauthor:: Rasmus Handberg <*****@*****.**>
	"""
	if np.isnan(value):
		raise ValueError("Invalid search value")
	if np.isposinf(value):
		return nanargmax(array)
	if np.isneginf(value):
		return nanargmin(array)
	return nanargmin(np.abs(array - value))
Ejemplo n.º 9
0
 def time_nanargmin(self, dtype, shape, order, axis):
     bn.nanargmin(self.arr, axis=axis)
Ejemplo n.º 10
0
 def time_nanargmin(self, dtype, shape):
     bn.nanargmin(self.arr)
Ejemplo n.º 11
0
def gas_exchange(p, fw, photo='Farquhar', res='low', dynamic=True, inf_gb=False,
                 iter_max=40, threshold_conv=0.1):

    # initial state
    Cs = p.CO2  # Pa
    Tleaf = p.Tair  # deg C

    # hydraulics
    P, E = hydraulics(p, res=res, kmax=p.kmaxT)

    # initialise gs over A
    g0 = 1.e-9  # g0 ~ 0, removing it entirely introduces errors
    Cs_umol_mol = Cs * conv.MILI / p.Patm  # umol mol-1
    gsoA = g0 + p.g1T * fw / Cs_umol_mol

    # iter on the solution until it is stable enough
    iter = 0

    while True:

        An, Aj, Ac, Ci = calc_photosynthesis(p, 0., Cs, photo, Tleaf=Tleaf,
                                             gs_over_A=gsoA)

        # stomatal conductance, with fwsoil effect
        gs = np.maximum(cst.zero, conv.GwvGc * gsoA * An)

        # calculate new trans, gw, gb, etc.
        trans, real_zero, gw, gb, __ = calc_trans(p, Tleaf, gs, inf_gb=inf_gb)
        new_Tleaf, __ = leaf_temperature(p, trans, Tleaf=Tleaf, inf_gb=inf_gb)
        Pleaf = P[bn.nanargmin(np.abs(E - trans))]

        # update Cs (Pa)
        boundary_CO2 = p.Patm * conv.FROM_MILI * An / (gb * conv.GbcvGb)
        Cs = np.maximum(cst.zero, np.minimum(p.CO2, p.CO2 - boundary_CO2))
        Cs_umol_mol = Cs * conv.MILI / p.Patm

        # update gs over A
        gsoA = g0 + p.g1T * fw / Cs_umol_mol

        # force stop when atm. conditions yield E < 0. (non-physical)
        if (iter < 1) and (not real_zero):
            real_zero = None

        # check for convergence
        if ((real_zero is None) or (iter >= iter_max) or ((iter >= 2) and
            real_zero and (abs(Tleaf - new_Tleaf) <= threshold_conv) and not
            np.isclose(gs, cst.zero, rtol=cst.zero, atol=cst.zero))):
            break

        # no convergence, iterate on leaf temperature
        Tleaf = new_Tleaf
        iter += 1

    if ((np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero) and
        (An > 0.)) or np.isclose(Ci, 0., rtol=cst.zero, atol=cst.zero) or
        (Ci < 0.) or np.isclose(Ci, p.CO2, rtol=cst.zero, atol=cst.zero) or
        (Ci > p.CO2) or (real_zero is None) or (not real_zero) or
       any(np.isnan([An, Ci, trans, gs, Tleaf, Pleaf]))):
        An, Ci, trans, gs, gb, Tleaf, Pleaf = (9999.,) * 7

    return An, Aj, Ac, Ci, trans, gs, gb, new_Tleaf, Pleaf
Ejemplo n.º 12
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p):
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5 * (a[w, ind] - o[w, ind] + a[w, n + ind] -
                              o[w, n + ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1)
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w,
                                      where(minres_ind < n, minres_ind +
                                            n, minres_ind - n)]
        volume = prod(e - y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n + ind] + o[w, n + ind])
        return [
            si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i],
               complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i],
               volume[i], volumeResidual[i]) for i in range(m)
        ]

    else:
        residual = None

        isSNLE = p.probType in ('SNLE', 'NLSP')
        if 1 or not isSNLE:
            o, a = asarray(o), asarray(a)
            a[a == inf] = 1e300
            o[o == -inf] = -1e300
            tmp = a - o
            tmp[tmp < 1e-300] = 1e-300
            #            ind_uf_inf = where(a==inf)[0]
            #            if ind_uf_inf.size:
            #                Tmp = o[ind_uf_inf]
            #                Tmp[Tmp==-inf] = -1e100
            #                M = nanmax(abs(Tmp))
            #                if M is nan or M == 0.0:
            #                    M = 1.0
            #                tmp[ind_uf_inf] = 1e200 * (1.0 + Tmp/M)
            nlhf = log2(tmp)  #-log2(p.fTol)

#        nlhf[a==inf] = 1e300# to make it not inf and nan
#        nlhf[o==-inf] = 1e300# to make it not inf and nan

        if p.probType == "MOP":
            if nlhf.ndim == 3:  # in MOP
                nlhf = nlhf.sum(axis=1)
            else:
                assert 0, 'bug in interalg'
            # make correct o,a wrt each target
            return [
                si(MOP_Fields, y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None,
                   [o[i][k]
                    for k in range(p.nf)], [a[i][k]
                                            for k in range(p.nf)], _s[i])
                for i in range(m)
            ]
        else:
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP',
                                  'QP', 'LP', 'MILP')

            if 0 and isSNLE:
                nlhf = Tmp = o = a = [None] * m
            else:
                s, q = o[:, 0:n], o[:, n:2 * n]
                Tmp = nanmax(where(q < s, q, s), 1)

                nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300

#            residual = None

            return [
                si(Fields, Tmp[i], y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None, o[i], a[i],
                   _s[i]) for i in range(m)
            ]
Ejemplo n.º 13
0
def func1(tnlhf, tnlhf_curr, residual, y, e, o, a, _s_prev, p, indT):
    m, n = y.shape
    w = arange(m)

    if p.probType == 'IP':
        oc_modL, oc_modU = o[:, :n], o[:, n:]
        ac_modL, ac_modU = a[:, :n], a[:, n:]
        #            # TODO: handle nans
        mino = where(oc_modL < oc_modU, oc_modL, oc_modU)
        maxa = where(ac_modL < ac_modU, ac_modU, ac_modL)

        # Prev
        tmp = a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:]
        t = nanargmin(tmp, 1)
        d = 0.5 * tmp[w, t]

        #New
        #        tmp = a - o
        #        t_ = nanargmin(tmp,1)
        #        t = t_% n
        #        d = tmp[w, t_]

        #        ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
        ind = 2**(1.0 / n) * d >= _s_prev
        #new
        #        ind = 2**(1.0/n) * d >= nanmax(maxa-mino, 1)

        #ind = 2**(-n) >= (_s_prev - _s)/asarray(_s, 'float64')

        #s2 = nanmin(maxa - mino, 1)
        #print (abs(s2/_s))

        # Prev
        _s = nanmin(maxa - mino, 1)

        # New
        #_s = nanmax(maxa - mino, 1)
        #        _s = nanmax(a - o, 1)

        #ind = _s_prev  <= _s + ((2**-n / log(2)) if n > 15 else log2(1+2**-n))
        indD = logical_not(ind)
        indD = ind
        indD = None
        #print len(where(indD)[0]), len(where(logical_not(indD))[0])
#    elif p.probType == 'MOP':
#
#        raise 'unimplemented'
    else:
        if p.solver.dataHandling == 'sorted':
            _s = func13(o, a)

            t = nanargmin(a, 1) % n

            d = nanmax([a[w, t] - o[w, t], a[w, n + t] - o[w, n + t]], 0)

            ## !!!! Don't replace it by (_s_prev /d- 1) to omit rounding errors ###
            #ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')

            #NEW
            ind = d >= _s_prev / 2**(1.0e-12 / n)
            #ind = d  >=  _s_prev / 2 ** (1.0/n)
            indD = empty(m, bool)
            indD.fill(True)
            #ind.fill(False)
            ###################################################
        elif p.solver.dataHandling == 'raw':
            if p.probType == 'MOP':
                t = p._t[:m]
                p._t = p._t[m:]
                d = _s = p.__s[:m]
                p.__s = p.__s[m:]
            else:
                #                tnlh_1, tnlh_2 = tnlhf[:, 0:n], tnlhf[:, n:]
                #                TNHLF_min =  where(logical_or(tnlh_1 > tnlh_2, isnan(tnlh_1)), tnlh_2, tnlh_1)
                #               # Set _s
                #                _s = nanmin(TNHLF_min, 1)
                T = tnlhf_curr
                tnlh_curr_1, tnlh_curr_2 = T[:, 0:n], T[:, n:]
                TNHL_curr_min = where(
                    logical_or(tnlh_curr_1 < tnlh_curr_2, isnan(tnlh_curr_2)),
                    tnlh_curr_1, tnlh_curr_2)
                t = nanargmin(TNHL_curr_min, 1)
                T = tnlhf
                d = nanmin(vstack(([T[w, t], T[w, n + t]])), 0)
                _s = d

            #OLD
            #!#!#!#! Don't replace it by _s_prev - d <= ... to omit inf-inf = nan !#!#!#
            #ind = _s_prev  <= d + ((2**-n / log(2)) if n > 15 else log2(1+2**-n))
            #ind = _s_prev - d <= ((2**-n / log(2)) if n > 15 else log2(1+2**-n))

            #NEW
            if any(_s_prev < d):
                pass
            ind = _s_prev <= d + 1.0 / n
            #            T = TNHL_curr_min
            #ind2 = nanmin(TNHL_curr_min, 0)

            indQ = d >= _s_prev - 1.0 / n
            #indQ = logical_and(indQ, False)
            indD = logical_or(indQ, logical_not(indT))


#            print '------'
#            print indQ[:10]
#            print indD[:10]
#            print _s_prev[:2], d[:2]
#print len(where(indD)[0]), len(where(indQ)[0]), len(where(indT)[0])
#print _s_prev - d
###################################################
#d = ((tnlh[w, t]* tnlh[w, n+t])**0.5)
        else:
            assert 0

    if any(ind):
        r10 = where(ind)[0]
        #print('r10:', r10)
        #        print _s_prev
        #        print ((_s_prev -d)*n)[r10]
        #        print('ind length: %d' % len(where(ind)[0]))
        #        print where(ind)[0].size
        #bs = e[ind] - y[ind]
        #t[ind] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well
        bs = e[r10] - y[r10]
        t[r10] = nanargmax(bs, 1)  # ordinary numpy.argmax can be used as well

    return t, _s, indD
def mtx_minimise(p,
                 trans,
                 all_Cis,
                 photo,
                 Vmax25=None,
                 all_Ccs=None,
                 inf_gb=False):
    """
    Uses matrices to find each value of Ci for which An(supply) ~
    An(demand) on the transpiration stream.

    Arguments:
    ----------
    p: recarray object or pandas series or class containing the data
        time step's met data & params

    trans: array
        transpiration [mol m-2 s-1], values depending on the possible
        leaf water potentials (P) and the Weibull parameters b, c

    all_Cis: array
        all potential Ci values over the transpiration stream (for each
        water potential, Ci values can be anywhere between a lower bound
        and Cs)

    photo: string
        either the Farquhar model for photosynthesis, or the Collatz
        model

    inf_gb: bool
        if True, gb is prescrived and very large

    Returns:
    --------
    The value of Ci for which An(supply) is the closest to An(demand)
    (e.g. An(supply) - An(demand) closest to zero).

    """

    if Vmax25 is not None:
        demand, __, __ = calc_photosynthesis(p,
                                             np.expand_dims(trans, axis=1),
                                             all_Cis,
                                             photo,
                                             Vmax25=np.expand_dims(Vmax25,
                                                                   axis=1),
                                             inf_gb=inf_gb)

    elif all_Ccs is not None:
        demand, __, __ = calc_photosynthesis(p,
                                             np.expand_dims(trans, axis=1),
                                             all_Ccs,
                                             photo,
                                             inf_gb=inf_gb)

    else:
        demand, __, __ = calc_photosynthesis(p,
                                             np.expand_dims(trans, axis=1),
                                             all_Cis,
                                             photo,
                                             inf_gb=inf_gb)

    supply = A_trans(p, np.expand_dims(trans, axis=1), all_Cis, inf_gb=inf_gb)

    # find the meeting point between demand and supply
    idx = bn.nanargmin(np.abs(supply - demand), axis=1)  # closest ~0

    if all_Ccs is not None:
        all_Cis = all_Ccs

    # each Ci on the transpiration stream
    Ci = np.asarray([all_Cis[e, idx[e]] for e in range(len(trans))])
    Ci = np.ma.masked_where(idx == 0, Ci)
    Ci = np.ma.masked_where(idx == all_Cis.shape[1] - 1, Ci)

    return Ci
Ejemplo n.º 15
0
 def argf(self, *args, **kwargs):
     return bn.nanargmin(*args, **kwargs)
Ejemplo n.º 16
0
def func1(tnlhf, tnlhf_curr, residual, y, e, o, a, _s_prev, p, indT):
    m, n = y.shape
    w = arange(m)
    
    if p.probType == 'IP':
        oc_modL, oc_modU = o[:, :n], o[:, n:]
        ac_modL, ac_modU = a[:, :n], a[:, n:]
#            # TODO: handle nans
        mino = where(oc_modL < oc_modU, oc_modL, oc_modU)
        maxa = where(ac_modL < ac_modU, ac_modU, ac_modL)
    
        # Prev
        tmp = a[:, 0:n]-o[:, 0:n]+a[:, n:]-o[:, n:]
        t = nanargmin(tmp,1)
        d = 0.5*tmp[w, t]
        
        
        #New
#        tmp = a - o
#        t_ = nanargmin(tmp,1)
#        t = t_% n
#        d = tmp[w, t_]

#        ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
        ind = 2**(1.0/n) * d >= _s_prev
        #new
#        ind = 2**(1.0/n) * d >= nanmax(maxa-mino, 1)
        
        #ind = 2**(-n) >= (_s_prev - _s)/asarray(_s, 'float64')
    
        #s2 = nanmin(maxa - mino, 1)
        #print (abs(s2/_s))
        
        # Prev
        _s = nanmin(maxa - mino, 1)
        
        # New
        #_s = nanmax(maxa - mino, 1)
#        _s = nanmax(a - o, 1)
        
        #ind = _s_prev  <= _s + ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
        indD = logical_not(ind)
        indD = ind
        indD = None
        #print len(where(indD)[0]), len(where(logical_not(indD))[0])
#    elif p.probType == 'MOP':
#
#        raise 'unimplemented'
    else:
        if p.solver.dataHandling == 'sorted':
            _s = func13(o, a)
            t = nanargmin(a, 1) % n
            d = nanmax([a[w, t] - o[w, t], 
                    a[w, n+t] - o[w, n+t]], 0)
            
            ## !!!! Don't replace it by (_s_prev /d- 1) to omit rounding errors ###
            #ind = 2**(-n) >= (_s_prev - d)/asarray(d, 'float64')
            
            #NEW
            ind = d  >=  _s_prev / 2 ** (1.0e-12/n)
            #ind = d  >=  _s_prev / 2 ** (1.0/n)
            indD = empty(m, bool)
            indD.fill(True)
            #ind.fill(False)
            ###################################################
        elif p.solver.dataHandling == 'raw':
            if p.probType == 'MOP':
                t = p._t[:m]
                p._t = p._t[m:]
                d = _s = p.__s[:m]
                p.__s = p.__s[m:]
            else:
#                tnlh_1, tnlh_2 = tnlhf[:, 0:n], tnlhf[:, n:]
#                TNHLF_min =  where(logical_or(tnlh_1 > tnlh_2, isnan(tnlh_1)), tnlh_2, tnlh_1)
#               # Set _s
#                _s = nanmin(TNHLF_min, 1)
                T = tnlhf_curr
                tnlh_curr_1, tnlh_curr_2 = T[:, 0:n], T[:, n:]
                TNHL_curr_min =  where(logical_or(tnlh_curr_1 < tnlh_curr_2, isnan(tnlh_curr_2)), tnlh_curr_1, tnlh_curr_2)
                t = nanargmin(TNHL_curr_min, 1)
                T = tnlhf
                d = nanmin(vstack(([T[w, t], T[w, n+t]])), 0)
                _s = d

            #OLD
            #!#!#!#! Don't replace it by _s_prev - d <= ... to omit inf-inf = nan !#!#!#
            #ind = _s_prev  <= d + ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
            #ind = _s_prev - d <= ((2**-n / log(2)) if n > 15 else log2(1+2**-n)) 
            
            #NEW
            if any(_s_prev < d):
                pass
            ind = _s_prev  <= d + 1.0/n
#            T = TNHL_curr_min
            #ind2 = nanmin(TNHL_curr_min, 0)
            
            indQ = d >= _s_prev - 1.0/n 
            #indQ = logical_and(indQ, False)
            indD = logical_or(indQ, logical_not(indT))
#            print _s_prev[:2], d[:2]
            #print len(where(indD)[0]), len(where(indQ)[0]), len(where(indT)[0])
            #print _s_prev - d
            ###################################################
            #d = ((tnlh[w, t]* tnlh[w, n+t])**0.5)
        else:
            assert 0

    if any(ind):
        r10 = where(ind)[0]
        #print('r10:', r10)
#        print _s_prev
#        print ((_s_prev -d)*n)[r10]
#        print('ind length: %d' % len(where(ind)[0]))
#        print where(ind)[0].size
        #bs = e[ind] - y[ind]
        #t[ind] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well
        bs = e[r10] - y[r10]
        t[r10] = nanargmax(bs, 1) # ordinary numpy.argmax can be used as well

    return t, _s, indD
Ejemplo n.º 17
0
def r14MOP(p, nlhc, residual, definiteRange, y, e, vv, asdf1, C, r40, g, nNodes,  \
         r41, fTol, Solutions, varTols, _in, dataType, \
         maxNodes, _s, indTC, xRecord):

    assert p.probType == 'MOP'
    
    if len(p._discreteVarsNumList):
        y, e = adjustDiscreteVarBounds(y, e, p)
    
    
    if p.nProc != 1 and getattr(p, 'pool', None) is None:
        p.pool = Pool(processes = p.nProc)
    elif p.nProc == 1:
        p.pool = None
    
    ol, al = [], []
    targets = p.targets # TODO: check it
    m, n = y.shape
    ol, al = [[] for k in range(m)], [[] for k in range(m)]
    for i, t in enumerate(targets):
        o, a, definiteRange = func82(y, e, vv, t.func, dataType, p)
        o, a = o.reshape(2*n, m).T, a.reshape(2*n, m).T
        for j in range(m):
            ol[j].append(o[j])
            al[j].append(a[j])
        #ol.append(o.reshape(2*n, m).T.tolist())
        #al.append(a.reshape(2*n, m).T.tolist())

    nlhf = r43(targets, Solutions.F, ol, al, p.pool, p.nProc)
    
    fo_prev = 0
    # TODO: remove NaN nodes here

    if y.size == 0:
        return _in, g, fo_prev, _s, Solutions, xRecord, r41, r40
    
    nodes = func11(y, e, nlhc, indTC, residual, ol, al, _s, p)
    
    #y, e = func4(y, e, o, a, fo)
    
    
    assert p.solver.dataHandling == 'raw', '"sorted" mode is unimplemented for MOP yet'
    
    if nlhf is None:
        new_nodes_tnlh_all = nlhc
    elif nlhc is None: 
        new_nodes_tnlh_all = nlhf
    else:
        new_nodes_tnlh_all = nlhf + nlhc

    asdf1 = [t.func for t in p.targets]
    r5F, r5Coords = getr4Values(vv, y, e, new_nodes_tnlh_all, asdf1, C, p.contol, dataType, p) 
    
    nIncome, nOutcome = r44(Solutions, r5Coords, r5F, targets, p.solver.sigma)
    fo = 0 # unused for MOP
    
    # TODO: better of nlhc for unconstrained probs

    if len(_in) != 0:
        an = hstack((nodes,  _in))
    else:
        an = atleast_1d(nodes)

    hasNewParetoNodes = False if nIncome == 0 else True
    if hasNewParetoNodes:
        ol2 = [node.o for node in an]
        al2 = [node.a for node in an]
        nlhc2 = [node.nlhc for node in an]
        nlhf2 = r43(targets, Solutions.F, ol2, al2, p.pool, p.nProc)
        tnlh_all = asarray(nlhc2) if nlhf2 is None else nlhf2 if nlhc2[0] is None else asarray(nlhc2) + nlhf2
    else:
        tnlh_all = vstack([new_nodes_tnlh_all] + [node.tnlh_all for node in _in]) if len(_in) != 0 else new_nodes_tnlh_all
        
    for i, node in enumerate(nodes):
        node.tnlh_all = tnlh_all[i]

    r10 = logical_not(any(isfinite(tnlh_all), 1))
    if any(r10):
        ind = where(logical_not(r10))[0]
        #an = take(an, ind, axis=0, out=an[:ind.size])
        an = asarray(an[ind])
        tnlh_all = take(tnlh_all, ind, axis=0, out=tnlh_all[:ind.size])
    
#    else:
#        tnlh_all = hstack([node.tnlh_all for node in an])
        
    T1, T2 = tnlh_all[:, :tnlh_all.shape[1]/2], tnlh_all[:, tnlh_all.shape[1]/2:]
    T = where(logical_or(T1 < T2, isnan(T2)), T1, T2)
    t = nanargmin(T, 1)
    
    w = arange(t.size)
    NN = T[w, t].flatten()

    for i, node in enumerate(an):
        node.tnlh_all = tnlh_all[i]
        node.tnlh_curr_best = NN[i]
        
    astnlh = argsort(NN)
    an = an[astnlh]

    p._t = t
    
    # TODO: form _s in other level (for active nodes only), to reduce calculations
    if len(an) != 0:
        nlhf_fixed = asarray([node.nlhf for node in an])
        nlhc_fixed = asarray([node.nlhc for node in an])
        T = nlhf_fixed + nlhc_fixed if nlhc_fixed[0] is not None else nlhf_fixed 
        p.__s = \
        nanmin(vstack(([T[w, t], T[w, n+t]])), 0)
    else:
        p.__s = array([])

#        p._nObtainedSolutions = len(solutions)
#        if p._nObtainedSolutions > maxSolutions:
#            solutions = solutions[:maxSolutions]
#            p.istop = 0
#            p.msg = 'user-defined maximal number of solutions (p.maxSolutions = %d) has been exeeded' % p.maxSolutions
#            return an, g, fo, None, solutions, coords, xRecord, r41, r40
    
    
    # TODO: fix it
    p._frontLength = len(Solutions.F)
    p._nIncome = nIncome
    p._nOutcome = nOutcome
    p.iterfcn(p.x0)
    #print('iter: %d (%d) frontLenght: %d' %(p.iter, itn, len(Solutions.coords)))
    
    if p.istop != 0: 
        return an, g, fo, None, Solutions, xRecord, r41, r40
        
    #an, g = func9(an, fo, g, p)

    nn = maxNodes#1 if asdf1.isUncycled and all(isfinite(o)) and p._isOnlyBoxBounded and not p.probType.startswith('MI') else maxNodes
    
    an, g = func5(an, nn, g, p)
    nNodes.append(len(an))
    return an, g, fo, _s, Solutions, xRecord, r41, r40
Ejemplo n.º 18
0
def solve_std(p, sw, photo='Farquhar', res='low', iter_max=40,
              threshold_conv=0.1, inf_gb=False):

    """
    Checks the energy balance by looking for convergence of the new leaf
    temperature with the leaf temperature predicted by the previous
    iteration. Then returns the corresponding An, E, Ci, etc.

    Arguments:
    ----------
    p: recarray object or pandas series or class containing the data
        time step's met data & params

    sw: float
        mean volumetric soil moisture content [m3 m-3]

    photo: string
        either the Farquhar model for photosynthesis, or the Collatz
        model

    threshold_conv: float
        convergence threshold for the new leaf temperature to be in
        energy balance

    iter_max: int
        maximum number of iterations allowed on the leaf temperature
        before reaching the conclusion that the system is not energy
        balanced

    inf_gb: bool
        if True, gb is prescrived and very large

    Returns:
    --------
    trans_can: float
        transpiration rate of canopy [mmol m-2 s-1] across leaves

    gs_can: float
        stomatal conductance of canopy [mol m-2 s-1] across leaves

    An_can: float
        C assimilation rate of canopy [umol m-2 s-1] across leaves

    Ci_can: float
        average intercellular CO2 concentration of canopy [Pa] across
        leaves

    rublim_can: string
        'True' if the C assimilation is rubisco limited, 'False'
        otherwise.

    """

    # initial state
    Cs = p.CO2  # Pa
    Tleaf = p.Tair  # deg C
    Dleaf = np.maximum(0.05, p.VPD)  # gs model not valid < 0.05

    # hydraulics
    P, E = hydraulics(p, res=res)

    if sw >= p.fc:
        g1 = p.g1

    else:
        g1 = p.g1 * fwWP(p, p.Ps)

    # initialise gs over A
    g0 = 1.e-9  # g0 ~ 0, removing it entirely introduces errors
    Cs_umol_mol = Cs * conv.MILI / p.Patm  # umol mol-1
    gsoA = g0 + (1. + g1 / (Dleaf ** 0.5)) / Cs_umol_mol

    # iter on the solution until it is stable enough
    iter = 0

    while True:

        An, Aj, Ac, Ci = calc_photosynthesis(p, 0., Cs, photo, Tleaf=Tleaf,
                                             gs_over_A=gsoA)

        # stomatal conductance, with moisture stress effect
        gs = np.maximum(cst.zero, conv.GwvGc * gsoA * An)

        # calculate new trans, gw, gb, mol.m-2.s-1
        trans, real_zero, gw, gb, Dleaf = calc_trans(p, Tleaf, gs,
                                                     inf_gb=inf_gb)
        new_Tleaf, __ = leaf_temperature(p, trans, Tleaf=Tleaf, inf_gb=inf_gb)

        # update Cs (Pa)
        boundary_CO2 = p.Patm * conv.FROM_MILI * An / (gb * conv.GbcvGb)
        Cs = np.maximum(cst.zero, np.minimum(p.CO2, p.CO2 - boundary_CO2))
        Cs_umol_mol = Cs * conv.MILI / p.Patm

        # new leaf-air vpd, kPa
        if (np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero) or
            np.isclose(gw, cst.zero, rtol=cst.zero, atol=cst.zero) or
            np.isclose(gs, cst.zero, rtol=cst.zero, atol=cst.zero)):
            Dleaf = np.maximum(0.05, p.VPD)  # kPa

        # update gs over A
        gsoA = g0 + (1. + g1 / (Dleaf ** 0.5)) / Cs_umol_mol

        # force stop when atm. conditions yield E < 0. (non-physical)
        if (iter < 1) and (not real_zero):
            real_zero = None

        # check for convergence
        if ((real_zero is None) or (iter >= iter_max) or ((iter >= 1) and
            real_zero and (abs(Tleaf - new_Tleaf) <= threshold_conv) and not
            np.isclose(gs, cst.zero, rtol=cst.zero, atol=cst.zero))):
            break

        # no convergence, iterate on leaf temperature
        Tleaf = new_Tleaf
        iter += 1

    Pleaf = P[bn.nanargmin(np.abs(trans - E))]
    rublim = rubisco_limit(Aj, Ac)  # lim?

    if ((np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero) and
        (An > 0.)) or np.isclose(Ci, 0., rtol=cst.zero, atol=cst.zero) or
        (Ci < 0.) or np.isclose(Ci, p.CO2, rtol=cst.zero, atol=cst.zero) or
        (Ci > p.CO2) or (real_zero is None) or (not real_zero) or
       any(np.isnan([An, Ci, trans, gs, new_Tleaf, Pleaf]))):
        An, Ci, trans, gs, gb, new_Tleaf, Pleaf = (9999.,) * 7

    elif not np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero):
        trans *= conv.MILI  # mmol.m-2.s-1

    return An, Ci, rublim, trans, gs, gb, new_Tleaf, Pleaf
Ejemplo n.º 19
0
 def compute_integral(self, x_s, y_s):
     if len(x_s) == 0:
         return np.zeros((y_s.shape[0],)) * np.nan
     closer = bottleneck.nanargmin(abs(x_s - self.limits[0]))
     return y_s[:, closer]
Ejemplo n.º 20
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p):
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5 * (a[w, ind] - o[w, ind] + a[w, n + ind] -
                              o[w, n + ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1)
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w,
                                      where(minres_ind < n, minres_ind +
                                            n, minres_ind - n)]
        volume = prod(e - y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n + ind] + o[w, n + ind])
        return [
            si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i],
               complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i],
               volume[i], volumeResidual[i]) for i in range(m)
        ]

    else:

        residual = None
        tmp = asarray(a) - asarray(o)
        tmp[tmp < 1e-300] = 1e-300
        nlhf = log2(tmp)  #-log2(p.fTol)
        #        nlhf[a==inf] = 1e300# to make it not inf and nan
        #        nlhf[o==-inf] = 1e300# to make it not inf and nan
        if nlhf.ndim == 3:  # in MOP
            nlhf = nlhf.sum(axis=1)

        if p.probType == "MOP":
            # make correct o,a wrt each target
            return [
                si(MOP_Fields, y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None,
                   [o[i][k]
                    for k in range(p.nf)], [a[i][k]
                                            for k in range(p.nf)], _s[i])
                for i in range(m)
            ]
        else:
            s, q = o[:, 0:n], o[:, n:2 * n]
            Tmp = nanmax(where(q < s, q, s), 1)

            nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP')

            #            residual = None

            return [
                si(Fields, Tmp[i], y[i], e[i], nlhf[i],
                   nlhc[i] if nlhc is not None else None,
                   indTC[i] if indTC is not None else None,
                   residual[i] if residual is not None else None, o[i], a[i],
                   _s[i]) for i in range(m)
            ]
Ejemplo n.º 21
0
        if (iter < 1) and (not real_zero):
            real_zero = None

        # check for convergence
        if ((real_zero is None) or (iter >= iter_max) or
            ((iter >= 1) and real_zero and
             (abs(Tleaf - new_Tleaf) <= threshold_conv)
             and not np.isclose(gs, cst.zero, rtol=cst.zero, atol=cst.zero))):
            break

        # no convergence, iterate on leaf temperature
        Tleaf = new_Tleaf
        iter += 1

    if case == 1:  # infer leaf water potential, MPa
        Pleaf = P[bn.nanargmin(np.abs(E - trans))]

    else:
        Pleaf = P[iopt]
        ksc = p.kmaxS2 * cost[iopt]

    rublim = rubisco_limit(Aj, Ac)  # lim?

    if ((np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero) and
         (An > 0.)) or np.isclose(Ci, 0., rtol=cst.zero, atol=cst.zero)
            or (Ci < 0.) or np.isclose(Ci, p.CO2, rtol=cst.zero, atol=cst.zero)
            or (Ci > p.CO2) or (real_zero is None) or (not real_zero)
            or any(np.isnan([An, Ci, trans, gs, new_Tleaf, Pleaf]))):
        An, Ci, trans, gs, gb, new_Tleaf, Pleaf = (9999., ) * 7

    elif not np.isclose(trans, cst.zero, rtol=cst.zero, atol=cst.zero):
Ejemplo n.º 22
0
def func11(y, e, nlhc, indTC, residual, o, a, _s, p): 
    m, n = y.shape
    if p.probType == "IP":
        w = arange(m)
        # TODO: omit recalculation from func1
        ind = nanargmin(a[:, 0:n] - o[:, 0:n] + a[:, n:] - o[:, n:], 1)
        sup_inf_diff = 0.5*(a[w, ind] - o[w, ind] + a[w, n+ind] - o[w, n+ind])
        diffao = a - o
        minres_ind = nanargmin(diffao, 1) 
        minres = diffao[w, minres_ind]
        complementary_minres = diffao[w, where(minres_ind<n, minres_ind+n, minres_ind-n)]
        volume = prod(e-y, 1)
        volumeResidual = volume * sup_inf_diff
        F = 0.25 * (a[w, ind] + o[w, ind] + a[w, n+ind] + o[w, n+ind])
        return [si(IP_fields, sup_inf_diff[i], minres[i], minres_ind[i], complementary_minres[i], y[i], e[i], o[i], a[i], _s[i], F[i], volume[i], volumeResidual[i]) for i in range(m)]
        
    else:
        residual = None
        
        isSNLE = p.probType in ('SNLE', 'NLSP')
        if 1 or not isSNLE:
            o, a = asarray(o), asarray(a)
            a[a==inf] = 1e300
            o[o==-inf] = -1e300
            tmp = a - o
            tmp[tmp<1e-300] = 1e-300
#            ind_uf_inf = where(a==inf)[0]
#            if ind_uf_inf.size:
#                Tmp = o[ind_uf_inf]
#                Tmp[Tmp==-inf] = -1e100
#                M = nanmax(abs(Tmp))
#                if M is nan or M == 0.0: 
#                    M = 1.0
#                tmp[ind_uf_inf] = 1e200 * (1.0 + Tmp/M)
            nlhf = log2(tmp)#-log2(p.fTol)
            
#        nlhf[a==inf] = 1e300# to make it not inf and nan
#        nlhf[o==-inf] = 1e300# to make it not inf and nan
        
        if p.probType == "MOP":
            if nlhf.ndim == 3: # in MOP
                nlhf = nlhf.sum(axis=1)
            else:
                assert 0, 'bug in interalg'
            # make correct o,a wrt each target
            return [si(MOP_Fields, y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          [o[i][k] for k in range(p.nf)], [a[i][k] for k in range(p.nf)], 
                          _s[i]) for i in range(m)]
        else:
            assert p.probType in ('GLP', 'NLP', 'NSP', 'SNLE', 'NLSP', 'MINLP', 'QP', 'LP', 'MILP')
            
            if 0 and isSNLE:
                nlhf = Tmp = o = a = [None]*m
            else:
                s, q = o[:, 0:n], o[:, n:2*n]
                Tmp = nanmax(where(q<s, q, s), 1)
                
                nlhf[logical_and(isinf(a), isinf(nlhf))] = 1e300
            
#            residual = None

            return [si(Fields, Tmp[i], y[i], e[i], nlhf[i], 
                          nlhc[i] if nlhc is not None else None, 
                          indTC[i] if indTC is not None else None, 
                          residual[i] if residual is not None else None, 
                          o[i], a[i], _s[i]) for i in range(m)]
Ejemplo n.º 23
0
    def argf(self, *args, **kwargs): return bn.nanargmin(*args, **kwargs)

class nanmax(A_extremum):