Python numpy.nanmin() Examples

The following are 30 code examples of numpy.nanmin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: rafpc.py    From fylearn with MIT License 6 votes vote down vote up
def fuzzify_mean(A):
    # output for fuzzified values
    R = np.zeros((A.shape[0], A.shape[1] * 3))

    cmin, cmax, cmean = np.nanmin(A, 0), np.nanmax(A, 0), np.nanmean(A, 0)
        
    left = np.array([cmin - (cmax - cmin), cmin, cmax]).T
    middle = np.array([cmin, cmean, cmax]).T
    right = np.array([cmin, cmax, cmax + (cmax - cmin)]).T

    mus = []

    for i in range(A.shape[1]):
        f_l = fl.TriangularSet(*left[i])
        f_m = fl.TriangularSet(*middle[i])
        f_r = fl.TriangularSet(*right[i])
        R[:,(i*3)] = f_l(A[:,i])
        R[:,(i*3)+1] = f_m(A[:,i])
        R[:,(i*3)+2] = f_r(A[:,i])
        mus.extend([(i, f_l), (i, f_m), (i, f_r)])

    return 3, R, mus 
Example #2
Source File: LSDMap_Subplots.py    From LSDMappingTools with MIT License 6 votes vote down vote up
def findminval_multirasters(FileList):
    """
    Loops through a list or array of rasters (np arrays)
    and finds the minimum single value in the set of arrays.
    """
    overall_min_val = 0

    for i in range (len(FileList)):

        raster_as_array = LSDMap_IO.ReadRasterArrayBlocks(FileList[i])
        this_min_val = np.nanmin(raster_as_array)

        if this_min_val > overall_min_val:
            overall_min_val = this_min_val
            print(overall_min_val)

    return overall_min_val 
Example #3
Source File: test_frame.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_unsorted_index_lims(self):
        df = DataFrame({'y': [0., 1., 2., 3.]}, index=[1., 0., 3., 2.])
        ax = df.plot()
        xmin, xmax = ax.get_xlim()
        lines = ax.get_lines()
        assert xmin <= np.nanmin(lines[0].get_data()[0])
        assert xmax >= np.nanmax(lines[0].get_data()[0])

        df = DataFrame({'y': [0., 1., np.nan, 3., 4., 5., 6.]},
                       index=[1., 0., 3., 2., np.nan, 3., 2.])
        ax = df.plot()
        xmin, xmax = ax.get_xlim()
        lines = ax.get_lines()
        assert xmin <= np.nanmin(lines[0].get_data()[0])
        assert xmax >= np.nanmax(lines[0].get_data()[0])

        df = DataFrame({'y': [0., 1., 2., 3.], 'z': [91., 90., 93., 92.]})
        ax = df.plot(x='z', y='y')
        xmin, xmax = ax.get_xlim()
        lines = ax.get_lines()
        assert xmin <= np.nanmin(lines[0].get_data()[0])
        assert xmax >= np.nanmax(lines[0].get_data()[0]) 
Example #4
Source File: som.py    From pyERA with MIT License 6 votes vote down vote up
def return_normalized_distance_matrix(self, input_vector):
        """Return the min-max normalized euclidean-distance matrix between the input vector and the SOM weights.

        A value of 0.0 means that the input/weights are equal.
        @param input_vector the vector to use for the comparison.
        """
        output_matrix = np.zeros((self._matrix_size, self._matrix_size))
        it = np.nditer(output_matrix, flags=['multi_index'])
        while not it.finished:
            #print "%d <%s>" % (it[0], it.multi_index),
            dist = self.return_euclidean_distance(input_vector, self._weights_matrix[it.multi_index[0], it.multi_index[1], :])
            output_matrix[it.multi_index[0], it.multi_index[1]] = dist
            it.iternext()
        #min-max normalization
        max_value = np.nanmax(output_matrix)
        min_value = np.nanmin(output_matrix)
        output_matrix = (output_matrix - min_value) / (max_value - min_value)
        return output_matrix 
Example #5
Source File: som.py    From pyERA with MIT License 6 votes vote down vote up
def return_similarity_matrix(self, input_vector):
        """Return a similarity matrix where a value is 1.0 if the distance input/weight is zero.

        @param input_vector the vector to use for the comparison.
        """
        output_matrix = np.zeros((self._matrix_size, self._matrix_size))
        it = np.nditer(output_matrix, flags=['multi_index'])
        while not it.finished:
            #print "%d <%s>" % (it[0], it.multi_index),
            dist = self.return_euclidean_distance(input_vector, self._weights_matrix[it.multi_index[0], it.multi_index[1], :])
            output_matrix[it.multi_index[0], it.multi_index[1]] = dist
            it.iternext()
        #min-max normalization
        max_value = np.nanmax(output_matrix)
        min_value = np.nanmin(output_matrix)
        output_matrix = (output_matrix - min_value) / (max_value - min_value)
        output_matrix = 1.0 - output_matrix
        return output_matrix 
Example #6
Source File: plots.py    From basenji with Apache License 2.0 6 votes vote down vote up
def scatter_lims(vals1, vals2=None, buffer=.05):
  if vals2 is not None:
    vals = np.concatenate((vals1, vals2))
  else:
    vals = vals1
  vmin = np.nanmin(vals)
  vmax = np.nanmax(vals)

  buf = .05 * (vmax - vmin)

  if vmin == 0:
    vmin -= buf / 2
  else:
    vmin -= buf
  vmax += buf

  return vmin, vmax


################################################################################
# nucleotides

# Thanks to Anshul Kundaje, Avanti Shrikumar
# https://github.com/kundajelab/deeplift/tree/master/deeplift/visualization 
Example #7
Source File: bam_cov.py    From basenji with Apache License 2.0 6 votes vote down vote up
def scatter_lims(vals1, vals2=None, buffer=.05):
  if vals2 is not None:
    vals = np.concatenate((vals1, vals2))
  else:
    vals = vals1
  vmin = np.nanmin(vals)
  vmax = np.nanmax(vals)

  buf = .05 * (vmax - vmin)

  if vmin == 0:
    vmin -= buf / 2
  else:
    vmin -= buf
  vmax += buf

  return vmin, vmax

################################################################################
# __main__
################################################################################ 
Example #8
Source File: ImageView.py    From tf-pose with Apache License 2.0 6 votes vote down vote up
def quickMinMax(self, data):
        """
        Estimate the min/max values of *data* by subsampling.
        Returns [(min, max), ...] with one item per channel
        """
        while data.size > 1e6:
            ax = np.argmax(data.shape)
            sl = [slice(None)] * data.ndim
            sl[ax] = slice(None, None, 2)
            data = data[sl]
            
        cax = self.axes['c']
        if cax is None:
            return [(float(nanmin(data)), float(nanmax(data)))]
        else:
            return [(float(nanmin(data.take(i, axis=cax))), 
                     float(nanmax(data.take(i, axis=cax)))) for i in range(data.shape[-1])] 
Example #9
Source File: test_sw.py    From pysat with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_calc_f107a_daily_missing(self):
        """ Test the calc_f107a routine with some daily data missing"""

        self.testInst.data = pds.DataFrame({'f107': np.linspace(70, 200, 160)},
                                           index=[pysat.datetime(2009, 1, 1)
                                                  + pds.DateOffset(days=2*i+1)
                                                  for i in range(160)])
        sw_f107.calc_f107a(self.testInst, f107_name='f107', f107a_name='f107a')

        # Assert that new data and metadata exist
        assert 'f107a' in self.testInst.data.columns
        assert 'f107a' in self.testInst.meta.keys()

        # Assert the finite values have realistic means
        assert(np.nanmin(self.testInst['f107a'])
               > np.nanmin(self.testInst['f107']))
        assert(np.nanmax(self.testInst['f107a'])
               < np.nanmax(self.testInst['f107']))

        # Assert the expected number of fill values
        assert(len(self.testInst['f107a'][np.isnan(self.testInst['f107a'])])
               == 40) 
Example #10
Source File: rafpc.py    From fylearn with MIT License 6 votes vote down vote up
def fuzzify_partitions(p):
    def fuzzify_p(A):
        R = np.zeros((A.shape[0], A.shape[1] * p))

        cmin, cmax = np.nanmin(A, 0), np.nanmax(A, 0)
        psize = (cmax - cmin) / (p - 1)

        mus = []
        # iterate features
        for i in range(A.shape[1]):
            # iterate partitions
            mu_i = []
            offset = cmin[i]
            for j in range(p):
                f = fl.TriangularSet(offset - psize[i], offset, offset + psize[i])
                R[:, (i * p) + j] = f(A[:, i])
                mu_i.append(f)
                offset += psize[i]
            mus.append(mu_i)
        return p, R, mus
    return fuzzify_p 
Example #11
Source File: grid.py    From python-control with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __call__(self, transform_xy, x1, y1, x2, y2):
        x_, y_ = np.linspace(x1, x2, self.nx), np.linspace(y1, y2, self.ny)
        x, y = np.meshgrid(x_, y_)
        lon, lat = transform_xy(np.ravel(x), np.ravel(y))

        with np.errstate(invalid='ignore'):
            if self.lon_cycle is not None:
                lon0 = np.nanmin(lon)
                # Changed from 180 to 360 to be able to span only
                # 90-270 (left hand side)
                lon -= 360. * ((lon - lon0) > 360.)
            if self.lat_cycle is not None:
                lat0 = np.nanmin(lat)
                # Changed from 180 to 360 to be able to span only
                # 90-270 (left hand side)
                lat -= 360. * ((lat - lat0) > 360.)

        lon_min, lon_max = np.nanmin(lon), np.nanmax(lon)
        lat_min, lat_max = np.nanmin(lat), np.nanmax(lat)

        lon_min, lon_max, lat_min, lat_max = \
            self._adjust_extremes(lon_min, lon_max, lat_min, lat_max)

        return lon_min, lon_max, lat_min, lat_max 
Example #12
Source File: netcdfhelper.py    From geojsoncontour with MIT License 6 votes vote down vote up
def netcdf_to_geojson(ncfile, var, fourth_dim=None):
    realpath = os.path.realpath(ncfile)
    name, ext = os.path.splitext(realpath)
    X, Y, Z, levels, unit = setup(ncfile, var)
    figure = plt.figure()
    ax = figure.add_subplot(111)
    for t in range(len(Z.time)):
        third = Z.isel(time=t)
        position = 0
        if len(third.dims) == 3:
            position = len(getattr(third, third.dims[0]))-1
            third = third[position, ]
        # local min max
        levels = np.linspace(start=np.nanmin(third),
                             stop=np.nanmax(third), num=20)
        contourf = ax.contourf(X, Y, third, levels=levels, cmap=plt.cm.viridis)
        geojsoncontour.contourf_to_geojson(
            contourf=contourf,
            geojson_filepath='{}_{}_t{}_{}.geojson'.format(name, var,
                                                           t, position),
            ndigits=3,
            min_angle_deg=None,
            unit=unit
        ) 
Example #13
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def apply_cmap(zs, cmap, vmin=None, vmax=None, unit=None, logrescale=False):
    '''
    apply_cmap(z, cmap) applies the given cmap to the values in z; if vmin and/or vmax are passed,
      they are used to scale z.

    Note that this function can automatically rescale data into log-space if the colormap is a
    neuropythy log-space colormap such as log_eccentricity. To enable this behaviour use the
    optional argument logrescale=True.
    '''
    zs = pimms.mag(zs) if unit is None else pimms.mag(zs, unit)
    zs = np.asarray(zs, dtype='float')
    if pimms.is_str(cmap): cmap = matplotlib.cm.get_cmap(cmap)
    if logrescale:
        if vmin is None: vmin = np.log(np.nanmin(zs))
        if vmax is None: vmax = np.log(np.nanmax(zs))
        mn = np.exp(vmin)
        u = zdivide(nanlog(zs + mn) - vmin, vmax - vmin, null=np.nan)
    else:        
        if vmin is None: vmin = np.nanmin(zs)
        if vmax is None: vmax = np.nanmax(zs)
        u = zdivide(zs - vmin, vmax - vmin, null=np.nan)
    u[np.isnan(u)] = -np.inf
    return cmap(u) 
Example #14
Source File: fpt.py    From fylearn with MIT License 5 votes vote down vote up
def default_fuzzifier(idx, F):
    """Default fuzzifier function.

    Creates three fuzzy sets with triangular membership functions: (low, med, hig) from min and max data points.
    """
    # get min/max from data
    v_min = np.nanmin(F)
    v_max = np.nanmax(F)
    # blarg
    return [ Leaf(idx, "low", fl.TriangularSet(v_min - (v_max - v_min) ** 2, v_min, v_max)),
             Leaf(idx, "med", fl.TriangularSet(v_min, v_min + ((v_max - v_min) / 2), v_max)),
             Leaf(idx, "hig", fl.TriangularSet(v_min, v_max, v_max + (v_max - v_min) ** 2)) ] 
Example #15
Source File: test_series.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_unsorted_index_xlim(self):
        ser = Series([0., 1., np.nan, 3., 4., 5., 6.],
                     index=[1., 0., 3., 2., np.nan, 3., 2.])
        _, ax = self.plt.subplots()
        ax = ser.plot(ax=ax)
        xmin, xmax = ax.get_xlim()
        lines = ax.get_lines()
        assert xmin <= np.nanmin(lines[0].get_data(orig=False)[0])
        assert xmax >= np.nanmax(lines[0].get_data(orig=False)[0]) 
Example #16
Source File: fuzzylogic.py    From fylearn with MIT License 5 votes vote down vote up
def min_max_normalize(X):
    nmin, nmax = np.nanmin(X), np.nanmax(X)
    return (X - nmin) / (nmax - nmin) 
Example #17
Source File: rafpc.py    From fylearn with MIT License 5 votes vote down vote up
def build_memberships(X, factory):
    mins = np.nanmin(X, 0)
    maxs = np.nanmax(X, 0)
    means = np.nanmean(X, 0)
    return [ (i, factory(means[i] - ((maxs[i] - mins[i]) / 2.0),
                         means[i], means[i] + ((maxs[i] - mins[i]) / 2.0))) for i in range(X.shape[1]) ] 
Example #18
Source File: _tools.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _get_xlim(lines):
    left, right = np.inf, -np.inf
    for l in lines:
        x = l.get_xdata(orig=False)
        left = min(np.nanmin(x), left)
        right = max(np.nanmax(x), right)
    return left, right 
Example #19
Source File: frr.py    From fylearn with MIT License 5 votes vote down vote up
def build_memberships(X, factory):
    mins = np.nanmin(X, 0)
    maxs = np.nanmax(X, 0)
    means = np.nanmean(X, 0)
    return [ factory(means[i] - ((maxs[i] - mins[i]) / 2.0),
                     means[i],
                     means[i] + ((maxs[i] - mins[i]) / 2.0))
             for i in range(len(X.T)) ] 
Example #20
Source File: fuzzylogic.py    From fylearn with MIT License 5 votes vote down vote up
def min(X, axis=-1):
    return np.nanmin(X, axis) 
Example #21
Source File: digits_adjust.py    From OpenCV-Python-Tutorial with MIT License 5 votes vote down vote up
def adjust_SVM(self):
        Cs = np.logspace(0, 10, 15, base=2)
        gammas = np.logspace(-7, 4, 15, base=2)
        scores = np.zeros((len(Cs), len(gammas)))
        scores[:] = np.nan

        print('adjusting SVM (may take a long time) ...')
        def f(job):
            i, j = job
            samples, labels = self.get_dataset()
            params = dict(C = Cs[i], gamma=gammas[j])
            score = cross_validate(SVM, params, samples, labels)
            return i, j, score

        ires = self.run_jobs(f, np.ndindex(*scores.shape))
        for count, (i, j, score) in enumerate(ires):
            scores[i, j] = score
            print('%d / %d (best error: %.2f %%, last: %.2f %%)' %
                  (count+1, scores.size, np.nanmin(scores)*100, score*100))
        print(scores)

        print('writing score table to "svm_scores.npz"')
        np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas)

        i, j = np.unravel_index(scores.argmin(), scores.shape)
        best_params = dict(C = Cs[i], gamma=gammas[j])
        print('best params:', best_params)
        print('best error: %.2f %%' % (scores.min()*100))
        return best_params 
Example #22
Source File: _core.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _get_ind(self, y):
        if self.ind is None:
            # np.nanmax() and np.nanmin() ignores the missing values
            sample_range = np.nanmax(y) - np.nanmin(y)
            ind = np.linspace(np.nanmin(y) - 0.5 * sample_range,
                              np.nanmax(y) + 0.5 * sample_range, 1000)
        elif is_integer(self.ind):
            sample_range = np.nanmax(y) - np.nanmin(y)
            ind = np.linspace(np.nanmin(y) - 0.5 * sample_range,
                              np.nanmax(y) + 0.5 * sample_range, self.ind)
        else:
            ind = self.ind
        return ind 
Example #23
Source File: test_nanfunctions.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_nanmin(self):
        tgt = np.min(self.mat)
        for mat in self.integer_arrays():
            assert_equal(np.nanmin(mat), tgt) 
Example #24
Source File: test_nanfunctions.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_object_array(self):
        arr = np.array([[1.0, 2.0], [np.nan, 4.0], [np.nan, np.nan]], dtype=object)
        assert_equal(np.nanmin(arr), 1.0)
        assert_equal(np.nanmin(arr, axis=0), [1.0, 2.0])

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # assert_equal does not work on object arrays of nan
            assert_equal(list(np.nanmin(arr, axis=1)), [1.0, 4.0, np.nan])
            assert_(len(w) == 1, 'no warning raised')
            assert_(issubclass(w[0].category, RuntimeWarning)) 
Example #25
Source File: test_nanfunctions.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_masked(self):
        mat = np.ma.fix_invalid(_ndat)
        msk = mat._mask.copy()
        for f in [np.nanmin]:
            res = f(mat, axis=1)
            tgt = f(_ndat, axis=1)
            assert_equal(res, tgt)
            assert_equal(mat._mask, msk)
            assert_(not np.isinf(mat).any()) 
Example #26
Source File: simpletable.py    From TheCannon with MIT License 5 votes vote down vote up
def min(s, v):
        return np.nanmin(v) 
Example #27
Source File: netcdfhelper.py    From geojsoncontour with MIT License 5 votes vote down vote up
def setup(filename, var):
    data = xr.open_dataset(filename)
    lon_range = data.variables['lon'].data
    lat_range = data.variables['lat'].data
    lon_range, lat_range
    X, Y = np.meshgrid(lon_range, lat_range)
    mini = np.nanmin(data.variables[var].data)
    maxi = np.nanmax(data.variables[var].data)
    unit = data.variables[var].attrs['units']
    n_contours = 20
    levels = np.linspace(start=mini, stop=maxi, num=n_contours)
    Z = getattr(data, var)
    return X, Y, Z, levels, unit 
Example #28
Source File: test_nanfunctions.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_nanmin(self):
        tgt = np.min(self.mat)
        for mat in self.integer_arrays():
            assert_equal(np.nanmin(mat), tgt) 
Example #29
Source File: test_nanfunctions.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_masked(self):
        mat = np.ma.fix_invalid(_ndat)
        msk = mat._mask.copy()
        for f in [np.nanmin]:
            res = f(mat, axis=1)
            tgt = f(_ndat, axis=1)
            assert_equal(res, tgt)
            assert_equal(mat._mask, msk)
            assert_(not np.isinf(mat).any()) 
Example #30
Source File: rfsm.py    From pysheds with GNU General Public License v3.0 5 votes vote down vote up
def volume_to_level(self, node, waterlevel):
        if node.current_vol > 0:
            maxelev = node.parent.elev
            if node.elev:
                minelev = node.elev
            else:
                # TODO: This bound could be a lot better
                minelev = np.nanmin(self.dem)
            target_vol = node.current_vol
            elev = optimize.bisect(self.compute_vol, minelev, maxelev,
                                   args=(node, target_vol))
            if node.name:
                mask = self.ws[node.level] == node.name
            else:
                leaves = []
                self.enumerate_leaves(node, level=node.level, stack=leaves)
                mask = np.isin(self.ws[node.level], leaves)
                boundary = list(chain.from_iterable([self.b[node.level].setdefault(pair, [])
                                                     for pair in combinations(leaves, 2)]))
                mask.flat[boundary] = True
            mask = np.flatnonzero(mask & (self.dem < elev))
            waterlevel.flat[mask] = elev
        else:
            if node.l:
                self.volume_to_level(node.l, waterlevel)
            if node.r:
                self.volume_to_level(node.r, waterlevel)