Here are the examples of the python api numpy.ma.is_masked taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
52 Examples
3
Source : relational.py
with MIT License
from buds-lab
with MIT License
from buds-lab
def color_lookup(self, key):
"""Return the color corresponding to the hue level."""
if self.hue_type == "numeric":
normed = self.hue_norm(key)
if np.ma.is_masked(normed):
normed = np.nan
return self.cmap(normed)
elif self.hue_type == "categorical":
return self.palette[key]
def size_lookup(self, key):
3
Source : relational.py
with MIT License
from buds-lab
with MIT License
from buds-lab
def size_lookup(self, key):
"""Return the size corresponding to the size level."""
if self.size_type == "numeric":
min_size, max_size = self.size_range
val = self.size_norm(key)
if np.ma.is_masked(val):
return 0
return min_size + val * (max_size - min_size)
elif self.size_type == "categorical":
return self.sizes[key]
def style_to_attributes(self, levels, style, defaults, name):
3
Source : _core.py
with Apache License 2.0
from dashanji
with Apache License 2.0
from dashanji
def _lookup_single(self, key):
try:
value = self.lookup_table[key]
except KeyError:
normed = self.norm(key)
if np.ma.is_masked(normed):
normed = np.nan
size_values = self.lookup_table.values()
size_range = min(size_values), max(size_values)
value = size_range[0] + normed * np.ptp(size_range)
return value
def categorical_mapping(self, data, sizes, order):
3
Source : test_mask.py
with BSD 3-Clause "New" or "Revised" License
from earthlab
with BSD 3-Clause "New" or "Revised" License
from earthlab
def test_masked_arr_returned(im, im_mask):
""" Test for return of masked_array type. """
masked = mask_pixels(im, im_mask, vals=[0])
assert np.ma.is_masked(masked)
def test_vals_as_list(im, im_mask):
3
Source : test_norm_diff.py
with BSD 3-Clause "New" or "Revised" License
from earthlab
with BSD 3-Clause "New" or "Revised" License
from earthlab
def test_normalized_diff_no_mask(b1_b2_arrs):
"""Test that if result does not include nan values,
the array is returned as unmasked."""
# Test data
b1, b2 = b1_b2_arrs
n_diff = es.normalized_diff(b1=b1, b2=b2)
# Output array unmasked
assert not ma.is_masked(n_diff)
def test_normalized_diff_inf(b1_b2_arrs):
3
Source : test_norm_diff.py
with BSD 3-Clause "New" or "Revised" License
from earthlab
with BSD 3-Clause "New" or "Revised" License
from earthlab
def test_normalized_diff_mask(b1_b2_arrs):
"""Test that if result does include nan values,
the array is returned as masked."""
# Test data
b1, b2 = b1_b2_arrs
b2 = b2.astype(float)
b2[1:, 4:] = np.nan
n_diff = es.normalized_diff(b1=b1, b2=b2)
# Output array masked
assert ma.is_masked(n_diff)
3
Source : test_eclrun.py
with GNU Lesser General Public License v3.0
from equinor
with GNU Lesser General Public License v3.0
from equinor
def test_dualperm_soil_property(dual_poro_dual_perm_run):
soil = dual_poro_dual_perm_run.get_property_from_restart("SOIL", date=20170121)
assert soil.values[3, 0, 0] == pytest.approx(0.4452512)
assert soil.values[0, 1, 0] == pytest.approx(0.0)
assert np.ma.is_masked(soil.values[1, 2, 0])
assert soil.values[3, 2, 0] == pytest.approx(0.0)
assert soil.values[4, 2, 0] == pytest.approx(0.4127138)
def test_dualperm_fractured_soil_property(dual_poro_dual_perm_run):
3
Source : _mask.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def _apply_fx_mask(fx_mask, var_data):
"""Apply the fx data extracted mask on the actual processed data."""
# Apply mask across
if np.ma.is_masked(var_data):
fx_mask |= var_data.mask
# Build the new masked data
var_data = np.ma.array(var_data, mask=fx_mask, fill_value=1e+20)
return var_data
def mask_landsea(cube, mask_out, always_use_ne_mask=False):
3
Source : _trend.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def _mask_xy(x_arr, y_arr):
"""Mask X and Y arrays."""
if np.ma.is_masked(y_arr):
x_arr = x_arr[~y_arr.mask]
y_arr = y_arr[~y_arr.mask]
return (x_arr, y_arr)
def _slope(x_arr, y_arr):
3
Source : test_bias.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def assert_array_equal(array_1, array_2):
"""Assert that (masked) array 1 equals (masked) array 2."""
if np.ma.is_masked(array_1) or np.ma.is_masked(array_2):
np.testing.assert_array_equal(np.ma.getmaskarray(array_1),
np.ma.getmaskarray(array_2))
mask = np.ma.getmaskarray(array_1)
np.testing.assert_array_equal(array_1[~mask], array_2[~mask])
else:
np.testing.assert_array_equal(array_1, array_2)
def products_set_to_dict(products):
3
Source : test_co2s.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def test_co2_calculate_masked_cubes(masked_cubes):
"""Test function ``calculate`` with masked cube."""
derived_var = co2s.DerivedVariable()
out_cube = derived_var.calculate(masked_cubes)
assert not np.ma.is_masked(out_cube.data)
np.testing.assert_allclose(out_cube.data,
[[[180.0, 50.0],
[80.0, 10.0]]])
assert out_cube.units == '1e-6'
plev_coord = out_cube.coord('air_pressure')
assert plev_coord.var_name == 'plev'
assert plev_coord.standard_name == 'air_pressure'
assert plev_coord.long_name == 'pressure'
assert plev_coord.units == 'Pa'
np.testing.assert_allclose(plev_coord.points,
[[[105000.0, 50000.0], [95000.0, 60000.0]]])
def test_co2_calculate_unmasked_cubes(unmasked_cubes):
3
Source : test_co2s.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def test_co2_calculate_unmasked_cubes(unmasked_cubes):
"""Test function ``calculate`` with unmasked cube."""
derived_var = co2s.DerivedVariable()
out_cube = derived_var.calculate(unmasked_cubes)
assert not np.ma.is_masked(out_cube.data)
np.testing.assert_allclose(out_cube.data,
[[[2.25, 0.50],
[0.75, 0.02]]])
assert out_cube.units == '1e-6'
plev_coord = out_cube.coord('air_pressure')
assert plev_coord.var_name == 'plev'
assert plev_coord.standard_name == 'air_pressure'
assert plev_coord.long_name == 'pressure'
assert plev_coord.units == 'Pa'
np.testing.assert_allclose(plev_coord.points,
[[[105000.0, 50000.0], [95000.0, 60000.0]]])
3
Source : test_toz.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def test_toz_calculate_masked_cubes(masked_cubes):
"""Test function ``calculate`` with masked cube."""
derived_var = toz.DerivedVariable()
out_cube = derived_var.calculate(masked_cubes)
assert not np.ma.is_masked(out_cube.data)
np.testing.assert_allclose(out_cube.data,
[[[1.2988646378902597, 0.7871906896304607],
[1.6924599827054907, 0.9446288275565529]]])
assert out_cube.units == 'DU'
def test_toz_calculate_unmasked_cubes(unmasked_cubes):
3
Source : test_toz.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def test_toz_calculate_unmasked_cubes(unmasked_cubes):
"""Test function ``calculate`` with unmasked cube."""
derived_var = toz.DerivedVariable()
out_cube = derived_var.calculate(unmasked_cubes)
assert not np.ma.is_masked(out_cube.data)
np.testing.assert_allclose(out_cube.data,
[[[2.65676858, 0.39359534],
[2.04669579, 0.94462883]]])
assert out_cube.units == 'DU'
@pytest.mark.parametrize('project,out', [
3
Source : test_mask_multimodel.py
with Apache License 2.0
from ESMValGroup
with Apache License 2.0
from ESMValGroup
def assert_array_equal(array_1, array_2):
"""Assert that (masked) array 1 equals (masked) array 2."""
if np.ma.is_masked(array_1) or np.ma.is_masked(array_2):
np.testing.assert_array_equal(np.ma.getmaskarray(array_1),
np.ma.getmaskarray(array_2))
mask = np.ma.getmaskarray(array_1)
np.testing.assert_array_equal(array_1[~mask], array_2[~mask])
else:
np.testing.assert_array_equal(array_1, array_2)
def _get_cube(ndim):
3
Source : _core.py
with MIT License
from icaros-usc
with MIT License
from icaros-usc
def _lookup_single(self, key):
try:
value = self.lookup_table[key]
except KeyError:
normed = self.norm(key)
if np.ma.is_masked(normed):
normed = np.nan
value = self.size_range[0] + normed * np.ptp(self.size_range)
return value
def categorical_mapping(self, data, sizes, order):
3
Source : librarian.py
with MIT License
from jvines
with MIT License
from jvines
def _get_radius(res):
rad = res['radius_val'][0]
if np.ma.is_masked(rad):
CatalogWarning('radius', 1).warn()
return 0, 0
lo = res['radius_percentile_lower'][0]
up = res['radius_percentile_upper'][0]
rad_e = max([rad - lo, up - rad])
return rad, 5 * rad_e
@staticmethod
3
Source : librarian.py
with MIT License
from jvines
with MIT License
from jvines
def _get_teff(res):
teff = res['teff_val'][0]
if np.ma.is_masked(teff):
CatalogWarning('teff', 1).warn()
return 0, 0
lo = res['teff_percentile_lower'][0]
up = res['teff_percentile_upper'][0]
teff_e = max([teff - lo, up - teff])
return teff, teff_e
@staticmethod
3
Source : librarian.py
with MIT License
from jvines
with MIT License
from jvines
def _get_lum(res):
lum = res['lum_val'][0]
if np.ma.is_masked(lum):
CatalogWarning('lum', 1).warn()
return 0, 0
lo = res['lum_percentile_lower'][0]
up = res['lum_percentile_upper'][0]
lum_e = max([lum - lo, up - lum])
return lum, lum_e
@staticmethod
3
Source : librarian.py
with MIT License
from jvines
with MIT License
from jvines
def _qc_mags(mag, err, m):
if np.ma.is_masked(mag):
CatalogWarning(m, 2).warn()
return False
if np.ma.is_masked(err):
CatalogWarning(m, 3).warn()
return True
if err == 0:
CatalogWarning(m, 4).warn()
return True
if err > 1:
return False
return True
@staticmethod
3
Source : griddeddata.py
with GNU General Public License v3.0
from metno
with GNU General Public License v3.0
from metno
def _ensure_is_masked_array(self):
"""Make sure underlying data is masked array
Required, e.g. for removal of outliers
Note
----
Will trigger "realisation" of data (i.e. loading of numpy array) in
case data is lazily loaded.
"""
if not np.ma.is_masked(self.cube.data):
self.cube.data = np.ma.masked_array(self.cube.data)
def _resample_time_iris(self, to_ts_type):
3
Source : figures.py
with GNU General Public License v3.0
from noaa-ocs-modeling
with GNU General Public License v3.0
from noaa-ocs-modeling
def __call__(self, value, clip=None):
x, y = [self.vmin, self.sealevel, self.vmax], [0, self.col_val, 1]
if np.ma.is_masked(value) is False:
value = np.ma.masked_invalid(value)
return np.ma.masked_where(value.mask, np.interp(value, x, y))
def figure(f):
3
Source : grid_data.py
with GNU General Public License v3.0
from Sbozzolo
with GNU General Public License v3.0
from Sbozzolo
def is_masked(self):
"""Return whether the data is masked.
:returns: True if the data is masked, false if it is not.
:rtype: bool
"""
return np.ma.is_masked(self.data)
@property
3
Source : geo_tools.py
with MIT License
from system123
with MIT License
from system123
def _get_patches(self, bands, windows, strict=True):
for window, transform in windows:
data = self.read(bands, window=window, masked=True)
# If the patch contains nodata values then don't generate it
if strict and np.ma.is_masked(data):
continue
yield data, transform
# Yields patches from the raster with the defined stride and size, strict means no nodata will exist in thee returned patches
def get_patches(self, bands=1, size=128, stride=64, strict=True):
0
Source : select_model.py
with GNU General Public License v3.0
from acolite
with GNU General Public License v3.0
from acolite
def select_model(metadata, rdark, rsr_file=None, lutdir=None, bestfit='bands', bestfit_bands=None,
force_band=None, pressure=None, model_selection='min_tau', rdark_list_selection='intercept',
lowess_frac = 0.5, lut_data_dict=None,
luts=['PONDER-LUT-201704-MOD1-1013mb', 'PONDER-LUT-201704-MOD2-1013mb', 'PONDER-LUT-201704-MOD3-1013mb']):
import acolite as pp
import os
from numpy import float64, ndarray, arange, nanmin, where, nan, isnan, min
from numpy.ma import MaskedArray,is_masked
from statsmodels.nonparametric.smoothers_lowess import lowess
## get scene geometry and default bands from metadata
try:
if 'SE_DISTANCE' in metadata.keys():
se_distance = metadata['SE_DISTANCE']
else:
se_distance = pp.distance_se(metadata['DOY'])
ths = metadata['THS']
thv = metadata['THV']
azi = metadata['AZI']
if 'LUT_SENSOR' in metadata.keys():
sensor = metadata['LUT_SENSOR']
elif 'SATELLITE_SENSOR' in metadata.keys():
sensor = metadata['SATELLITE_SENSOR']
else:
sensor = metadata['SENSOR']
bestfit_bands_defaults = metadata['BANDS_BESTFIT']
bands_sorted = metadata['BANDS_ALL']
except:
print('Could not get appropriate metadata for model selection for satellite {}'.format(metadata['SATELLITE']))
print(metadata.keys())
return(1)
bestfit_bands_all = rdark.keys()
if bestfit_bands is None: bestfit_bands = bestfit_bands_defaults
if bestfit_bands == 'default': bestfit_bands = bestfit_bands_defaults
if bestfit_bands == 'all': bestfit_bands = bestfit_bands_all
## set LUT dir and rsr_file
pp_path = pp.config['pp_data_dir']
if lutdir is None:
lutdir=pp_path+'/LUT/'
if rsr_file is None:
rsr_file = pp_path+'/RSR/'+sensor+'.txt'
rsr, rsr_bands = pp.shared.rsr_read(file=rsr_file)
## make lut data dictionary that can be reused in next runs
if lut_data_dict is None:
lut_data_dict = {}
for li,lut in enumerate(luts):
## get sensor LUT
lut_sensor, meta_sensor = pp.aerlut.get_sensor_lut(sensor, rsr_file, lutdir=lutdir, lutid=lut, override=0)
lut_data_dict[lut] = {'lut':lut_sensor, 'meta':meta_sensor}
## read luts at other pressures if needed
if pressure is not None:
lut_split = lut.split('-')
lut0 = '-'.join(lut_split[0:-1]+['0500mb'])
lut_sensor, meta_sensor = pp.aerlut.get_sensor_lut(sensor, rsr_file, lutdir=lutdir, lutid=lut0, override=0)
lut_data_dict[lut0] = {'lut':lut_sensor, 'meta':meta_sensor}
lut1 = '-'.join(lut_split[0:-1]+['1100mb'])
lut_sensor, meta_sensor = pp.aerlut.get_sensor_lut(sensor, rsr_file, lutdir=lutdir, lutid=lut1, override=0)
lut_data_dict[lut1] = {'lut':lut_sensor, 'meta':meta_sensor}
## empty dicts
rdark_smooth = {}
rdark_selected = {}
## default is not to use the 'list' type model/rdark selection
## will only be used if the rdark is a list and the rdark_list_selection=="list"
rdark_list = False
## find out what kind of rdark is given and which selection to use
for band in rdark.keys():
## given rdark is a single spectrum
if (type(rdark[band]) == float64):
rdark_selected[band] = rdark[band]
## given rdark is a list of spectra
elif (type(rdark[band]) == ndarray) or (type(rdark[band]) == MaskedArray):
#pixel_range = range(0, len(rdark[band]))
pixel_range = [float(i) for i in range(0, len(rdark[band]))]
#print(pixel_range)
rdark_list = True
## select rdark by lowess smoothing the given spectra
if rdark_list_selection == 'smooth':
rdark_smooth[band] = lowess(rdark[band],pixel_range,frac=lowess_frac)[:,1]
rdark_selected[band] = rdark_smooth[band][0]
rdark_list = False
## select rdark by OLS intercept
elif rdark_list_selection == 'intercept':
for band in rdark.keys():
reg = pp.shared.regression.lsqfity(pixel_range, rdark[band]) # m, b, r, sm, sb
rdark_selected[band] = reg[1]
rdark_list = False
## select rdark from all given pixels according to min rmsd
elif rdark_list_selection == 'list':
rdark_selected[band] = rdark[band]
rdark_list = True
## select rdark from all given pixels according to min rmsd, but first smooth the given rdark
elif rdark_list_selection == 'list_smooth':
rdark_selected[band] = lowess(rdark[band],pixel_range,frac=lowess_frac)[:,1]
rdark_list = True
## fallback selection is darkest pixel in each band
else:
rdark_list_selection = 'darkest'
rdark_selected[band] = nanmin(rdark[band])
## given dark is something else
else:
print('rdark type not recognised')
rdark_selected = (rdark)
if rdark_list:
from numpy import min,max
rdark_len = [len(rdark[b]) for b in rdark]
max_len = max(rdark_len)
min_len = min(rdark_len)
rdark = {b:rdark[b][0:min_len] for b in rdark}
## find best fitting model
sel_rmsd = 1.
sel_tau = 5.
taufits = []
daot_minimum = 5
## run through luts
for li,lut in enumerate(luts):
## get current sensor LUT
if pressure is not None: ## interpolate LUTs to given pressure
lut_sensor, meta_sensor = pp.aerlut.aerlut_pressure(lut, lutdir, pressure, sensor, rsr_file, lut_data_dict=lut_data_dict)
else: ## just the default LUTs
lut_sensor, meta_sensor = (lut_data_dict[lut]["lut"], lut_data_dict[lut]["meta"])
## get tau for selected dark spectrum
tau_550, tau_rmsd, tau_band = pp.aerlut.lut_get_taufit_sensor(lut_sensor, meta_sensor,azi,thv,ths,rdark_selected,
bestfit_bands=bestfit_bands, force_band=force_band)
## if rdark list method do not select tau and model here, just append to 'taufits' list for later processing
if (rdark_list):
taufits.append((tau_550, tau_rmsd, tau_band))
## if single spectra selected before, select tau and model here
else:
## get tau and ac parameters for this model
tmp = pp.aerlut.lut_get_ac_parameters_sensor(lut_sensor,meta_sensor,azi,thv,ths,
rdark_selected, force_band=force_band)
## band index for this model (i.e. band giving lowest tau)
idx = tmp[8]
## selected tau for this model
tau550_cur = tmp[7][idx]
## probably obsolete
if bestfit == 'bands':
rmsd_y = [rdark_selected[band] for band in bestfit_bands]
rmsd_x = [tmp[0][band] for band in bestfit_bands]
rmsd = pp.rmsd(rmsd_x,rmsd_y)
else:
rmsd = tmp[9][idx]
## find best spectral fit
if model_selection == 'min_rmsd':
if is_masked(rmsd):
rmsd = 1.0
if (rmsd < = sel_rmsd) | (li == 0):
sel_rmsd = rmsd
sel_idx = idx
sel_ac_par = tmp
sel_model_lut, sel_model_lut_meta = (lut_sensor, meta_sensor)
pixel_idx = -1
## find lowest tau fit
if model_selection == 'min_tau':
if (tau550_cur < = sel_tau) | (li == 0):
sel_tau = tau550_cur
sel_rmsd = rmsd
sel_idx = idx
sel_ac_par = tmp
sel_model_lut, sel_model_lut_meta = (lut_sensor, meta_sensor)
pixel_idx = -1
## find lowest tau and second band with most similar tau
if model_selection == 'min_dtau':
allt = [tmp[7][b] for i,b in enumerate(tmp[7].keys())]
min_tau = min([t for t in allt if t > 0.001])
allt_diff_sorted = [t-min_tau for t in allt if t > min_tau]
allt_diff_sorted.sort()
if allt_diff_sorted[0] < daot_minimum:
daot_minimum = allt_diff_sorted[0]
daot_second_band = [b for i,b in enumerate(tmp[7].keys()) \
if allt[i] == allt_diff_sorted[0] + min_tau][0]
sel_tau = tau550_cur
sel_rmsd = rmsd
sel_idx = (idx,daot_second_band)
sel_ac_par = tmp
sel_model_lut, sel_model_lut_meta = (lut_sensor, meta_sensor)
pixel_idx = -1
## find lowest tau and minimum rmsd with any other band
if model_selection == 'min_drmsd':
rmsdi = {}
rmsdi_band = ''
rmsdi_min = 5
for i,b in enumerate(tmp[7].keys()):
if b == idx: continue
rmsd_yi = [rdark_selected[b], rdark_selected[idx]]
rmsd_xi = [tmp[0][b],tmp[0][idx]]
rmsdi[b] = pp.rmsd(rmsd_xi,rmsd_yi)
if is_masked(rmsdi[b]): rmsdi[b] = 5
if (rmsdi[b] < rmsdi_min) & (b != idx):
rmsdi_band = b
rmsdi_min = rmsdi[b]
# this happens if no band fits better (e.g. for rhopath==rhorayleigh)
if rmsdi_band == '':
rmsdi_band = b
sel_rmsd = -1
sel_idx = (idx,idx)
sel_ac_par = tmp
sel_model_lut, sel_model_lut_meta = (lut_sensor, meta_sensor)
pixel_idx = -1
elif (rmsdi[rmsdi_band] < = sel_rmsd) | (li == 0):
sel_rmsd = rmsdi[rmsdi_band]
sel_idx = (idx,rmsdi_band)
sel_ac_par = tmp
sel_model_lut, sel_model_lut_meta = (lut_sensor, meta_sensor)
pixel_idx = -1
## get rdark/tau+model according to min rmsd/tau in the given pixel lists
## when rdark_list_selection did not select a single spectrum
if (rdark_list):
## choose best lut for each pixel
tau_550_sel=[]
tau_rmsd_sel=[]
tau_band_sel=[]
tau_model_sel=[]
## get for each pixel the minimum rmsd [1] according to the models
## default select on min tau
list_min_idx = 0
if model_selection == "min_tau":
list_min_idx = 0
elif model_selection == "min_rmsd":
list_min_idx = 1
for i in pixel_range:
val_c = [taufits[j][list_min_idx][i] for j in range(0,len(taufits))] ## selected tau/rmsd per model for this pixel
idx = [i for i,t in enumerate(val_c) if (t == nanmin(val_c))]
if len(idx) == 0:
tau_550_sel.append(nan)
tau_rmsd_sel.append(nan)
tau_band_sel.append(nan)
tau_model_sel.append(nan)
else:
idx = idx[0]
tau_550_sel.append(taufits[idx][0][i])
tau_rmsd_sel.append(taufits[idx][1][i])
tau_band_sel.append(taufits[idx][2][i])
tau_model_sel.append(idx+1)
## select pixel with min rmsd
pixel_idx = where(tau_rmsd_sel == nanmin(tau_rmsd_sel))[0][0]
## select lut and band
lut_sel = luts[tau_model_sel[pixel_idx]-1]
sel_idx = tau_band_sel[pixel_idx]
sel_rmsd = tau_rmsd_sel[pixel_idx]
## construct the selected dark pixel
for band in rdark_selected.keys(): rdark_selected[band] = rdark_selected[band][pixel_idx]
## get selected LUT
if pressure is not None:
sel_model_lut, sel_model_lut_meta = pp.aerlut.aerlut_pressure(lut_sel, lutdir, pressure, sensor, rsr_file, lut_data_dict=lut_data_dict)
else:
sel_model_lut, sel_model_lut_meta = (lut_data_dict[lut_sel]["lut"], lut_data_dict[lut_sel]["meta"])
## get ac parameters for the selected rdark - probably better to use the TAU to retrieve parameters ?
sel_ac_par = pp.aerlut.lut_get_ac_parameters_sensor(sel_model_lut,sel_model_lut_meta,azi,thv,ths,
rdark_selected, force_band=force_band)
## get ac parameters from previously selected model
(ratm_s,rorayl_s,dtotr_s,utotr_s,dtott_s,utott_s,astot_s) = sel_ac_par[0:7]
tau550_all_bands = sel_ac_par[7]
if type(sel_idx) == str:
tau550 =tau550_all_bands[sel_idx]
else:
tau550 =tau550_all_bands[sel_idx[0]]
dark_idx = sel_idx
## if tau is the minimum in the model ratm may be nans
## in that case replace by Rayleigh reflectance
from numpy import isnan
for band in ratm_s.keys():
if isnan(ratm_s[band]): ratm_s[band] = rorayl_s[band]
return (ratm_s,rorayl_s,dtotr_s,utotr_s,dtott_s,utott_s,astot_s, tau550),\
(bands_sorted, tau550_all_bands, dark_idx, sel_rmsd, rdark_selected, pixel_idx), (sel_model_lut, sel_model_lut_meta)
0
Source : pixc_to_shp.py
with GNU Lesser General Public License v3.0
from CNES
with GNU Lesser General Public License v3.0
from CNES
def pixc_to_shp(input_name, output_name, lat_name, lon_name, var_names, group_name=None, progress=False):
pixc = nc.Dataset(input_name, "r")
if group_name is None:
latitude = pixc.variables[lat_name][:]
longitude = my_tools.convert_to_m180_180(pixc.variables[lon_name][:])
variables = [pixc.variables[var_name][:] for var_name in var_names]
else:
latitude = pixc.groups[group_name].variables[lat_name][:]
longitude = my_tools.convert_to_m180_180(pixc.groups[group_name].variables[lon_name][:])
variables = [pixc.groups[group_name].variables[var_name][:] for var_name in var_names]
nb_points = latitude.size
driver = "ESRI Shapefile"
crs = fiona.crs.from_epsg(4326) # WGS84
schema = {'properties': OrderedDict([(lon_name, 'float:24.15'), (lat_name, 'float:24.15')] + [(var_name, 'float:24.15') for var_name in var_names]), 'geometry': 'Point'}
sys.stdout.write("Writing shp points")
with fiona.open(output_name,'w', driver=driver, crs=crs, schema=schema) as c:
for i in range(nb_points):
point = geometry.Point(longitude[i], latitude[i])
prop = {lon_name: float(point.coords.xy[0][0]),
lat_name: float(point.coords.xy[1][0])}
for var_name, var_values in zip(var_names, variables):
if np.ma.is_masked(var_values[i]) or math.isnan(var_values[i]) :
prop[var_name] = float(-9999.)
else:
prop[var_name] = float(var_values[i])
c.write({'geometry': geometry.mapping(point), 'properties': prop})
if progress and i % 100 == 0:
sys.stdout.write("\rWriting shp points: {:.1f}% done ({} / {})".format(100*(i+1)/nb_points, i+1, nb_points))
sys.stdout.write("\n")
pixc.close()
if __name__ == "__main__":
0
Source : colors.py
with Apache License 2.0
from dashanji
with Apache License 2.0
from dashanji
def __call__(self, X, alpha=None, bytes=False):
"""
Parameters
----------
X : float or int, ndarray or scalar
The data value(s) to convert to RGBA.
For floats, X should be in the interval ``[0.0, 1.0]`` to
return the RGBA values ``X*100`` percent along the Colormap line.
For integers, X should be in the interval ``[0, Colormap.N)`` to
return RGBA values *indexed* from the Colormap with index ``X``.
alpha : float, None
Alpha must be a scalar between 0 and 1, or None.
bytes : bool
If False (default), the returned RGBA values will be floats in the
interval ``[0, 1]`` otherwise they will be uint8s in the interval
``[0, 255]``.
Returns
-------
Tuple of RGBA values if X is scalar, otherwise an array of
RGBA values with a shape of ``X.shape + (4, )``.
"""
if not self._isinit:
self._init()
mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X) # Mask nan's.
xa = np.array(X, copy=True)
if not xa.dtype.isnative:
xa = xa.byteswap().newbyteorder() # Native byteorder is faster.
if xa.dtype.kind == "f":
with np.errstate(invalid="ignore"):
xa *= self.N
# Negative values are out of range, but astype(int) would
# truncate them towards zero.
xa[xa < 0] = -1
# xa == 1 (== N after multiplication) is not out of range.
xa[xa == self.N] = self.N - 1
# Avoid converting large positive values to negative integers.
np.clip(xa, -1, self.N, out=xa)
xa = xa.astype(int)
# Set the over-range indices before the under-range;
# otherwise the under-range values get converted to over-range.
xa[xa > self.N - 1] = self._i_over
xa[xa < 0] = self._i_under
xa[mask_bad] = self._i_bad
if bytes:
lut = (self._lut * 255).astype(np.uint8)
else:
lut = self._lut.copy() # Don't let alpha modify original _lut.
if alpha is not None:
alpha = np.clip(alpha, 0, 1)
if bytes:
alpha = int(alpha * 255)
if (lut[-1] == 0).all():
lut[:-1, -1] = alpha
# All zeros is taken as a flag for the default bad
# color, which is no color--fully transparent. We
# don't want to override this.
else:
lut[:, -1] = alpha
# If the bad value is set to have a color, then we
# override its alpha just as for any other value.
rgba = lut[xa]
if not np.iterable(X):
# Return a tuple if the input was a scalar
rgba = tuple(rgba)
return rgba
def __copy__(self):
0
Source : plot.py
with GNU General Public License v3.0
from discsim
with GNU General Public License v3.0
from discsim
def plot_vis_quantity(baselines, vis_quantity, ax, vis_quantity_err=None,
**kwargs):
r"""
Plot a visibility domain quantity (e.g., observed visibilities, a frank fit,
residual visibilities, a power spectrum) as a function of baseline
Parameters
----------
baselines : array
Baseline data coordinates `b`
vis_quantity : array
A generic quantity `Q` to plot as a function of baselines `b`, Q(b)
ax : Matplotlib `~.axes.Axes` class
Axis on which to plot
vis_quantity_err : array, optional, default = None
Uncertainty on vis_quantity values
"""
# If input arrays are masked with invalid values ('--'), replace those
# masked values with NaN
if np.ma.is_masked(baselines):
baselines = np.ma.array(baselines).filled(np.nan)
vis_quantity = np.ma.array(vis_quantity).filled(np.nan)
if vis_quantity_err is not None:
vis_quantity_err = np.ma.array(vis_quantity_err).filled(np.nan)
if vis_quantity_err is not None:
ax.errorbar(baselines, vis_quantity, yerr=vis_quantity_err, **kwargs)
else:
ax.plot(baselines, vis_quantity, **kwargs)
ax.axhline(0, c='c', ls='--', zorder=10)
ax.legend(loc='best')
def plot_vis_hist(binned_vis, ax, **kwargs):
0
Source : colors.py
with GNU General Public License v3.0
from dnn-security
with GNU General Public License v3.0
from dnn-security
def __call__(self, X, alpha=None, bytes=False):
"""
Parameters
----------
X : float or int, ndarray or scalar
The data value(s) to convert to RGBA.
For floats, X should be in the interval ``[0.0, 1.0]`` to
return the RGBA values ``X*100`` percent along the Colormap line.
For integers, X should be in the interval ``[0, Colormap.N)`` to
return RGBA values *indexed* from the Colormap with index ``X``.
alpha : float or array-like or None
Alpha must be a scalar between 0 and 1, a sequence of such
floats with shape matching X, or None.
bytes : bool
If False (default), the returned RGBA values will be floats in the
interval ``[0, 1]`` otherwise they will be uint8s in the interval
``[0, 255]``.
Returns
-------
Tuple of RGBA values if X is scalar, otherwise an array of
RGBA values with a shape of ``X.shape + (4, )``.
"""
if not self._isinit:
self._init()
mask_bad = X.mask if np.ma.is_masked(X) else np.isnan(X) # Mask nan's.
xa = np.array(X, copy=True)
if not xa.dtype.isnative:
xa = xa.byteswap().newbyteorder() # Native byteorder is faster.
if xa.dtype.kind == "f":
with np.errstate(invalid="ignore"):
xa *= self.N
# Negative values are out of range, but astype(int) would
# truncate them towards zero.
xa[xa < 0] = -1
# xa == 1 (== N after multiplication) is not out of range.
xa[xa == self.N] = self.N - 1
# Avoid converting large positive values to negative integers.
np.clip(xa, -1, self.N, out=xa)
xa = xa.astype(int)
# Set the over-range indices before the under-range;
# otherwise the under-range values get converted to over-range.
xa[xa > self.N - 1] = self._i_over
xa[xa < 0] = self._i_under
xa[mask_bad] = self._i_bad
if bytes:
lut = (self._lut * 255).astype(np.uint8)
else:
lut = self._lut.copy() # Don't let alpha modify original _lut.
rgba = np.empty(shape=xa.shape + (4,), dtype=lut.dtype)
lut.take(xa, axis=0, mode='clip', out=rgba)
if alpha is not None:
if np.iterable(alpha):
alpha = np.asarray(alpha)
if alpha.shape != xa.shape:
raise ValueError("alpha is array-like but its shape"
" %s doesn't match that of X %s" %
(alpha.shape, xa.shape))
alpha = np.clip(alpha, 0, 1)
if bytes:
alpha = (alpha * 255).astype(np.uint8)
rgba[..., -1] = alpha
# If the "bad" color is all zeros, then ignore alpha input.
if (lut[-1] == 0).all() and np.any(mask_bad):
if np.iterable(mask_bad) and mask_bad.shape == xa.shape:
rgba[mask_bad] = (0, 0, 0, 0)
else:
rgba[..., :] = (0, 0, 0, 0)
if not np.iterable(X):
rgba = tuple(rgba)
return rgba
def __copy__(self):
0
Source : plot.py
with BSD 3-Clause "New" or "Revised" License
from earthlab
with BSD 3-Clause "New" or "Revised" License
from earthlab
def plot_rgb(
arr,
rgb=(0, 1, 2),
figsize=(10, 10),
str_clip=2,
ax=None,
extent=None,
title="",
stretch=None,
):
"""Plot three bands in a numpy array as a composite RGB image.
Parameters
----------
arr : numpy array
An n-dimensional array in rasterio band order (bands, rows, columns)
containing the layers to plot.
rgb : list (default = (0, 1, 2))
Indices of the three bands to be plotted.
figsize : tuple (default = (10, 10)
The x and y integer dimensions of the output plot.
str_clip: int (default = 2)
The percentage of clip to apply to the stretch. Default = 2 (2 and 98).
ax : object (optional)
The axes object where the ax element should be plotted.
extent : tuple (optional)
The extent object that matplotlib expects (left, right, bottom, top).
title : string (optional)
The intended title of the plot.
stretch : Boolean (optional)
Application of a linear stretch. If set to True, a linear stretch will
be applied.
Returns
----------
ax : axes object
The axes object associated with the 3 band image.
Example
-------
.. plot::
>>> import matplotlib.pyplot as plt
>>> import rasterio as rio
>>> import earthpy.plot as ep
>>> from earthpy.io import path_to_example
>>> with rio.open(path_to_example('rmnp-rgb.tif')) as src:
... img_array = src.read()
>>> # Ensure the input array doesn't have nodata values like -9999
>>> ep.plot_rgb(img_array)
< AxesSubplot:>
"""
if len(arr.shape) != 3:
raise ValueError(
"Input needs to be 3 dimensions and in rasterio "
"order with bands first"
)
# Index bands for plotting and clean up data for matplotlib
rgb_bands = arr[rgb, :, :]
if stretch:
rgb_bands = _stretch_im(rgb_bands, str_clip)
nan_check = np.isnan(rgb_bands)
if np.any(nan_check):
rgb_bands = np.ma.masked_array(rgb_bands, nan_check)
# If type is masked array - add alpha channel for plotting
if ma.is_masked(rgb_bands):
# Build alpha channel
mask = ~(np.ma.getmask(rgb_bands[0])) * 255
# Add the mask to the array & swap the axes order from (bands,
# rows, columns) to (rows, columns, bands) for plotting
rgb_bands = np.vstack(
(es.bytescale(rgb_bands), np.expand_dims(mask, axis=0))
).transpose([1, 2, 0])
else:
# Index bands for plotting and clean up data for matplotlib
rgb_bands = es.bytescale(rgb_bands).transpose([1, 2, 0])
# Then plot. Define ax if it's undefined
show = False
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
show = True
ax.imshow(rgb_bands, extent=extent)
ax.set_title(title)
ax.set(xticks=[], yticks=[])
# Multipanel won't work if plt.show is called prior to second plot def
if show:
plt.show()
return ax
def hist(
0
Source : test_norm_diff.py
with BSD 3-Clause "New" or "Revised" License
from earthlab
with BSD 3-Clause "New" or "Revised" License
from earthlab
def test_normalized_diff_inf(b1_b2_arrs):
"""Test that inf values in result are set to nan and
that array is returned as masked."""
# Test data
b1, b2 = b1_b2_arrs
b2[1:, 4:] = -20
# Check warning
with pytest.warns(
Warning, match="Divide by zero produced infinity values"
):
n_diff = es.normalized_diff(b1=b1, b2=b2)
# Inf values set to nan
assert not np.isinf(n_diff).any()
# Output array masked
assert ma.is_masked(n_diff)
def test_normalized_diff_mask(b1_b2_arrs):
0
Source : rt_match_filter.py
with GNU General Public License v3.0
from eqcorrscan
with GNU General Public License v3.0
from eqcorrscan
def _numpy_len(arr: Union[numpy.ndarray, numpy.ma.MaskedArray]) -> int:
"""
Convenience function to return the length of a numpy array.
If arr is a masked array will return the count of the non-masked elements.
Parameters
----------
arr
Array to get the length of - must be 1D
Returns
-------
Length of non-masked elements
"""
assert arr.ndim == 1, "Only supports 1D arrays."
if numpy.ma.is_masked(arr):
return arr.count()
return arr.shape[0]
if __name__ == "__main__":
0
Source : long_streamer_test.py
with GNU General Public License v3.0
from eqcorrscan
with GNU General Public License v3.0
from eqcorrscan
def run_streamer(self, rt_client, logger):
for net, sta, chan in self.selectors:
rt_client.select_stream(net=net, station=sta, selector=chan)
rt_client.background_run()
sleepy_time = 0
try:
while sleepy_time < = RUN_LENGTH:
time.sleep(SLEEP_INTERVAL)
now = UTCDateTime.now()
st = rt_client.stream.split().merge()
logger.info(f"Currently (at {now}) have the stream: \n{st}")
for tr in st:
if np.ma.is_masked(tr.data):
# Check that the data are not super gappy.
self.assertLess(tr.data.mask.sum(), len(tr.data) / 4)
# Check that data are recent.
self.assertLess(abs(now - rt_client.last_data), 60.0)
self.assertLess(abs(now - tr.stats.endtime), 120.0)
sleepy_time += SLEEP_INTERVAL
finally: # We MUST stop the streamer even if we fail.
rt_client.background_stop()
def test_no_wavebank(self):
0
Source : wps_binary.py
with MIT License
from GIS4WRF
with MIT License
from GIS4WRF
def compute_inv_scale_factor(blocks: BandData) -> Tuple[int,Tuple[float,float]]:
''' Compute optimal (inverse) scale factor by estimating significant digits. '''
# The maximum inverse scale factor across all data blocks.
max_inv_scale_factor = 10000000000
# After the first significant digit, how many extra digits at maximum should be preserved.
# E.g. 0.039859812 would become 0.0398598, whereas 0.950000000001 would become 0.95.
extra_precision = 1e-5
# Maximum total decimals that can be preserved.
# This also sets an upper limit to the possible inverse scale factor.
# E.g. 0.0000000273926 will stop at factor 1e10 and hence become 0.0000000274.
max_precision = 1/max_inv_scale_factor
# The current maximum inverse scale factor across all data blocks. Default is minimum.
inv_scale_factor = 1
# Current maximum magnitude of the block data. Default is minimum.
mag_max = -order_of_magnitude(max_inv_scale_factor)
# Min/max values over all data blocks.
all_min = math.inf
all_max = -math.inf
for data in blocks:
min_ = np.min(data)
if ma.is_masked(min_):
# Ignore blocks where all values are masked.
continue
max_ = np.max(data)
all_min = min(all_min, min_)
all_max = max(all_max, max_)
mag = order_of_magnitude(max(abs(min_), abs(max_)))
if mag > mag_max:
mag_max = mag
inv_scale_factor = 1
target_precision = max(pow(10, mag_max) * extra_precision, max_precision)
# Initial factor. If the data is all integer, then this factor will be returned.
block_inv_scale_factor = 1
while True:
max_diff = np.max(np.abs(data - np.round(data, order_of_magnitude(block_inv_scale_factor))))
if max_diff > target_precision:
block_inv_scale_factor *= 10
else:
break
inv_scale_factor = max(inv_scale_factor, block_inv_scale_factor)
return inv_scale_factor, (all_min, all_max)
def write_index_file(path: str, metadata: dict) -> None:
0
Source : convolve.py
with BSD 3-Clause "New" or "Revised" License
from HEXRD
with BSD 3-Clause "New" or "Revised" License
from HEXRD
def _copy_input_if_needed(input, dtype=float, order='C', nan_treatment=None,
mask=None, fill_value=None):
# strip quantity attributes
if hasattr(input, 'unit'):
input = input.value
output = input
# Copy input
try:
# Anything that's masked must be turned into NaNs for the interpolation.
# This requires copying. A copy is also needed for nan_treatment == 'fill'
# A copy prevents possible function side-effects of the input array.
if nan_treatment == 'fill' or np.ma.is_masked(input) or mask is not None:
if np.ma.is_masked(input):
# ``np.ma.maskedarray.filled()`` returns a copy, however there
# is no way to specify the return type or order etc. In addition
# ``np.nan`` is a ``float`` and there is no conversion to an
# ``int`` type. Therefore, a pre-fill copy is needed for non
# ``float`` masked arrays. ``subok=True`` is needed to retain
# ``np.ma.maskedarray.filled()``. ``copy=False`` allows the fill
# to act as the copy if type and order are already correct.
output = np.array(input, dtype=dtype, copy=False, order=order, subok=True)
output = output.filled(fill_value)
else:
# Since we're making a copy, we might as well use `subok=False` to save,
# what is probably, a negligible amount of memory.
output = np.array(input, dtype=dtype, copy=True, order=order, subok=False)
if mask is not None:
# mask != 0 yields a bool mask for all ints/floats/bool
output[mask != 0] = fill_value
else:
# The call below is synonymous with np.asanyarray(array, ftype=float, order='C')
# The advantage of `subok=True` is that it won't copy when array is an ndarray subclass. If it
# is and `subok=False` (default), then it will copy even if `copy=False`. This uses less memory
# when ndarray subclasses are passed in.
output = np.array(input, dtype=dtype, copy=False, order=order, subok=True)
except (TypeError, ValueError) as e:
raise TypeError('input should be a Numpy array or something '
'convertible into a float array', e)
return output
def convolve(array, kernel, boundary='fill', fill_value=0.,
0
Source : convolve.py
with BSD 3-Clause "New" or "Revised" License
from holzschu
with BSD 3-Clause "New" or "Revised" License
from holzschu
def _copy_input_if_needed(input, dtype=float, order='C', nan_treatment=None,
mask=None, fill_value=None):
# Alias input
input = input.array if isinstance(input, Kernel) else input
# strip quantity attributes
if hasattr(input, 'unit'):
input = input.value
output = input
# Copy input
try:
# Anything that's masked must be turned into NaNs for the interpolation.
# This requires copying. A copy is also needed for nan_treatment == 'fill'
# A copy prevents possible function side-effects of the input array.
if nan_treatment == 'fill' or np.ma.is_masked(input) or mask is not None:
if np.ma.is_masked(input):
# ``np.ma.maskedarray.filled()`` returns a copy, however there
# is no way to specify the return type or order etc. In addition
# ``np.nan`` is a ``float`` and there is no conversion to an
# ``int`` type. Therefore, a pre-fill copy is needed for non
# ``float`` masked arrays. ``subok=True`` is needed to retain
# ``np.ma.maskedarray.filled()``. ``copy=False`` allows the fill
# to act as the copy if type and order are already correct.
output = np.array(input, dtype=dtype, copy=False, order=order, subok=True)
output = output.filled(fill_value)
else:
# Since we're making a copy, we might as well use `subok=False` to save,
# what is probably, a negligible amount of memory.
output = np.array(input, dtype=dtype, copy=True, order=order, subok=False)
if mask is not None:
# mask != 0 yields a bool mask for all ints/floats/bool
output[mask != 0] = fill_value
else:
# The call below is synonymous with np.asanyarray(array, ftype=float, order='C')
# The advantage of `subok=True` is that it won't copy when array is an ndarray subclass. If it
# is and `subok=False` (default), then it will copy even if `copy=False`. This uses less memory
# when ndarray subclasses are passed in.
output = np.array(input, dtype=dtype, copy=False, order=order, subok=True)
except (TypeError, ValueError) as e:
raise TypeError('input should be a Numpy array or something '
'convertible into a float array', e)
return output
@support_nddata(data='array')
0
Source : interpolation.py
with MIT License
from igp-gravity
with MIT License
from igp-gravity
def fill_nans(x, y, v, xp, yp, vp):
""""
Fill in the NaNs or masked values on interpolated points using nearest
neighbors.
.. warning::
Operation is performed in place. Replaces the NaN or masked values of
the original array!
Parameters:
* x, y : 1D arrays
Arrays with the x and y coordinates of the original data points (not
interpolated).
* v : 1D array
Array with the scalar value assigned to the data points (not
interpolated).
* xp, yp : 1D arrays
Points where the data values were interpolated.
* vp : 1D array
Interpolated data values (the one that has NaNs or masked values to
replace).
"""
if np.ma.is_masked(vp):
nans = vp.mask
else:
nans = np.isnan(vp)
vp[nans] = scipy.interpolate.griddata((x, y), v, (xp[nans], yp[nans]),
method='nearest').ravel()
def interp_at(x, y, v, xp, yp, algorithm='cubic', extrapolate=False):
0
Source : grdio.py
with MIT License
from igp-gravity
with MIT License
from igp-gravity
def fill_nulls(self, method='nearest'):
"""
Fill in the NaNs or masked values on interpolated points using nearest
neighbors.
method='nearest' or 'linear' or 'cubic'
"""
if np.ma.is_masked(self.data):
nans = self.data.mask
else:
nans = np.isnan(self.data)
nx,ny = nans.shape
ns = nans.reshape(nx*ny)
shape = (nx, ny)
xmax = self.xmin + (self.cols-1)*self.xdim
ymax = self.ymin + (self.rows-1)*self.ydim
area = (self.xmin, xmax, self.ymin, ymax)
x, y = regular(area, shape)
dtmp = self.data.copy() #数组copy,不改变源数组
dtmp1 = dtmp.reshape(nx*ny)
ns1 = (ns == False)
dtmp1[ns] = interp.griddata((x[ns1], y[ns1]), dtmp1[ns1], (x[ns], y[ns]),
method).ravel()
self.data0 = dtmp1.reshape(nx,ny)
def grd2xyz(self, flag = True):
0
Source : librarian.py
with MIT License
from jvines
with MIT License
from jvines
def _retrieve_from_tess(self):
print('Checking catalog TICv8')
tic = self.get_TIC(self.ra, self.dec, self.radius)
tic.sort('dstArcSec')
mask = tic['GAIA'] == str(self.g_id)
cat = tic[mask]
if len(cat) > 0:
is_star = cat['objType'][0] == 'STAR'
if not is_star:
CatalogWarning('TESS', 8).warn()
return
self.tic = int(cat['ID'][0])
kic = cat['KIC'][0]
self.kic = int(kic) if not np.ma.is_masked(kic) else None
m, e, f = self.catalogs['TESS'][1][0]
filt_idx = np.where(f == self.filter_names)[0]
if self.used_filters[filt_idx] == 1:
CatalogWarning(f, 6).warn()
return
mag = cat[m][0]
err = cat[e][0]
if not self._qc_mags(mag, err, m):
return
self._add_mags(mag, err, f)
else:
CatalogWarning('TIC', 5).warn()
def _retrieve_from_cat(self, cat, name):
0
Source : _validation.py
with MIT License
from ktraunmueller
with MIT License
from ktraunmueller
def validate_graph(csgraph, directed, dtype=DTYPE,
csr_output=True, dense_output=True,
copy_if_dense=False, copy_if_sparse=False,
null_value_in=0, null_value_out=np.inf,
infinity_null=True, nan_null=True):
"""Routine for validation and conversion of csgraph inputs"""
if not (csr_output or dense_output):
raise ValueError("Internal: dense or csr output must be true")
# if undirected and csc storage, then transposing in-place
# is quicker than later converting to csr.
if (not directed) and isspmatrix_csc(csgraph):
csgraph = csgraph.T
if isspmatrix(csgraph):
if csr_output:
csgraph = csr_matrix(csgraph, dtype=DTYPE, copy=copy_if_sparse)
else:
csgraph = csgraph_to_dense(csgraph, null_value=null_value_out)
elif np.ma.is_masked(csgraph):
if dense_output:
mask = csgraph.mask
csgraph = np.array(csgraph.data, dtype=DTYPE, copy=copy_if_dense)
csgraph[mask] = null_value_out
else:
csgraph = csgraph_from_masked(csgraph)
else:
if dense_output:
csgraph = csgraph_masked_from_dense(csgraph,
copy=copy_if_dense,
null_value=null_value_in,
nan_null=nan_null,
infinity_null=infinity_null)
mask = csgraph.mask
csgraph = np.asarray(csgraph.data, dtype=DTYPE)
csgraph[mask] = null_value_out
else:
csgraph = csgraph_from_dense(csgraph, null_value=null_value_in,
infinity_null=infinity_null,
nan_null=nan_null)
if csgraph.ndim != 2:
raise ValueError("compressed-sparse graph must be two dimensional")
if csgraph.shape[0] != csgraph.shape[1]:
raise ValueError("compressed-sparse graph must be shape (N, N)")
return csgraph
0
Source : targets.py
with BSD 3-Clause "New" or "Revised" License
from LSSTDESC
with BSD 3-Clause "New" or "Revised" License
from LSSTDESC
def load(self):
"""Load the coordinates of the target.
Examples
--------
>>> s = Star('3C273')
>>> print(s.radec_position.dec)
2d03m08.598s
"""
Simbad.add_votable_fields('flux(U)', 'flux(B)', 'flux(V)', 'flux(R)', 'flux(I)', 'flux(J)', 'sptype',
'parallax', 'pm', 'z_value')
simbad = Simbad.query_object(self.label)
self.simbad = simbad
if simbad is not None:
if self.verbose or True:
self.my_logger.info(f'\n\tSimbad:\n{simbad}')
self.radec_position = SkyCoord(simbad['RA'][0] + ' ' + simbad['DEC'][0], unit=(u.hourangle, u.deg))
else:
self.my_logger.warning('Target {} not found in Simbad'.format(self.label))
self.get_radec_position_after_pm(date_obs="J2000")
if not np.ma.is_masked(simbad['Z_VALUE']):
self.redshift = float(simbad['Z_VALUE'])
else:
self.redshift = 0
self.load_spectra()
def load_spectra(self):
0
Source : gym_space_management.py
with MIT License
from Mohan-Zhang-u
with MIT License
from Mohan-Zhang-u
def normalize_spaces(space, max_space=None, min_space=None, skip_columns=None, fill_value=0.0):
"""
normalize each column of observation/action space to be in [-1,1] such that it looks like a Box
space can be the whole original space (X by D) or just one row in the original space (D,)
:param space: numpy array
:param max_space: numpy array, the maximum value of each column of the space, normally
we would get this from reading the dataset or prior knowledge
:param min_space: numpy array, the minimum value of each column of the space, normally
we would get this from reading the dataset or prior knowledge
:param skip_columns: numpy array or list, columns to skip from normalization
:param fill_value: float, the value to fill in the normalized space if the original space is masked here.
so, if you don't want a part of the space to be normalized, you can pass in a masked array.
The value will be automatically filled with fill_value in the normalized space. The returned will be
tuple of filled_re_space, max_space, min_space, and the original re_space with mask.
e.g.
a = np.array(range(24), dtype=np.float64).reshape(4,6)
a = np.where(a > 21, np.nan, a)
a = np.ma.array(a, mask=np.isnan(a))
b, max, min = mzutils.normalize_spaces(a)
"""
assert not isinstance(space, list)
if max_space is None:
max_space = space.max(axis=0)
if min_space is None:
min_space = space.min(axis=0)
gap = max_space - min_space
gap += 1e-8 # to avoid div by 0
full_sum = max_space + min_space
re_space = (2 * space - full_sum) / gap
if skip_columns is not None:
if len(space.shape) == 1:
re_space[skip_columns] = space[skip_columns]
else:
re_space[:, skip_columns] = space[:, skip_columns]
if np.ma.is_masked(re_space): # if re_space has all masks is False, this sentence can also be false.
return re_space.filled(fill_value=fill_value), max_space, min_space, re_space
return np.array(re_space), max_space, min_space
def denormalize_spaces(space_normalized, max_space=None, min_space=None, skip_columns=None, fill_value=0.0):
0
Source : gym_space_management.py
with MIT License
from Mohan-Zhang-u
with MIT License
from Mohan-Zhang-u
def denormalize_spaces(space_normalized, max_space=None, min_space=None, skip_columns=None, fill_value=0.0):
"""
same as above, and space_normalized can be the whole normalized original space or just one row in the normalized space
"""
assert not isinstance(space_normalized, list)
if max_space is None:
max_space = space_normalized.max(axis=0)
if min_space is None:
min_space = space_normalized.min(axis=0)
gap = max_space - min_space
gap += 1e-8 # to avoid div by 0
full_sum = max_space + min_space
re_space = (space_normalized * gap + full_sum) / 2
if skip_columns is not None:
if len(space_normalized.shape) == 1:
re_space[skip_columns] = space_normalized[skip_columns]
else:
re_space[:, skip_columns] = space_normalized[:, skip_columns]
if np.ma.is_masked(re_space): # if re_space has all masks is False, this sentence can also be false.
return re_space.filled(fill_value=fill_value), max_space, min_space, re_space
return np.array(re_space), max_space, min_space
def list_of_str_to_numpy_onehot_dict(lst):
0
Source : __init__.py
with GNU General Public License v3.0
from nkarasiak
with GNU General Public License v3.0
from nkarasiak
def _convert_array(self, X):
if self._array_is_customized is True:
X = self.xFunction(X, **self.xKwargs)
if self.standardize:
if np.ma.is_masked(X):
if X.mask.ndim == 1:
X = X.reshape(-1, 1)
tmpMask = X.mask[:, 0]
X = _reshape_ndim(X)
X = self.StandardScaler.transform(X)
if np.ma.is_masked(X):
tmpMask = np.repeat(tmpMask.reshape(-1, 1),
X.shape[-1], axis=1)
X = np.ma.masked_array(X, tmpMask)
X = _reshape_ndim(X)
return X
def predict_array(self, X):
0
Source : quantity.py
with GNU General Public License v3.0
from noam09
with GNU General Public License v3.0
from noam09
def __setitem__(self, key, value):
try:
if np.ma.is_masked(value) or math.isnan(value):
self._magnitude[key] = value
return
except TypeError:
pass
try:
if isinstance(value, self.__class__):
factor = self.__class__(
value.magnitude, value._units / self._units
).to_root_units()
else:
factor = self.__class__(value, self._units ** (-1)).to_root_units()
if isinstance(factor, self.__class__):
if not factor.dimensionless:
raise DimensionalityError(
value,
self.units,
extra_msg=". Assign a quantity with the same dimensionality "
"or access the magnitude directly as "
f"`obj.magnitude[{key}] = {value}`.",
)
self._magnitude[key] = factor.magnitude
else:
self._magnitude[key] = factor
except PintTypeError:
raise
except TypeError as exc:
raise TypeError(
f"Neither Quantity object nor its magnitude ({self._magnitude}) "
"supports indexing"
) from exc
def tolist(self):
0
Source : pfp_gfALT.py
with BSD 3-Clause "New" or "Revised" License
from OzFlux
with BSD 3-Clause "New" or "Revised" License
from OzFlux
def trap_masked_constant(num):
if numpy.ma.is_masked(num):
num = float(c.missing_value)
return num
0
Source : pfp_utils.py
with BSD 3-Clause "New" or "Revised" License
from OzFlux
with BSD 3-Clause "New" or "Revised" License
from OzFlux
def round2significant(x, d, direction='nearest'):
"""
Round to 'd' significant digits with the option to round to
the nearest number, round up or round down.
"""
if numpy.ma.is_masked(x):
y = float(0)
elif numpy.isclose(x, 0.0):
y = float(0)
else:
n = d - numpy.ceil(numpy.log10(abs(x)))
if direction.lower() == 'up':
y = round(numpy.ceil(x*10**n))/float(10**n)
elif direction.lower() == 'down':
y = round(numpy.floor(x*10**n))/float(10**n)
else:
y = round(x*10**n)/float(10**n)
return y
def r(b, p, alpha):
0
Source : isd2nc.py
with BSD 3-Clause "New" or "Revised" License
from OzFlux
with BSD 3-Clause "New" or "Revised" License
from OzFlux
def interpolate_1d(x1, y1, x2):
"""
Purpose:
Interpolate data from one time step to another.
Assumptions:
Usage:
Author: PRI
Date: June 2017
"""
# off we go
if numpy.ma.is_masked(y1):
# check we have at least 2 non-masked points
if numpy.ma.count(y1) >= 2:
# input Y array is a masked array
idx = numpy.where(numpy.ma.getmaskarray(y1) == False)[0]
int_fn = scipy.interpolate.Akima1DInterpolator(x1[idx], y1[idx].data)
y2 = int_fn(x2)
else:
msg = "Not enough points ( < 2) to interpolate"
logger.warning(msg)
y2 = numpy.ma.ones(len(x2))*float(c.missing_value)
else:
int_fn = scipy.interpolate.Akima1DInterpolator(x1, y1)
y2 = int_fn(x2)
return y2
def interpolate_1d_old(x1,y1,x2,k=3,ext=0):
0
Source : isd2nc.py
with BSD 3-Clause "New" or "Revised" License
from OzFlux
with BSD 3-Clause "New" or "Revised" License
from OzFlux
def interpolate_1d_old(x1,y1,x2,k=3,ext=0):
"""
Purpose:
Interpolate data from one time step to another.
Assumptions:
Usage:
Author: PRI
Date: June 2017
"""
# off we go
if numpy.ma.is_masked(y1):
# check we have at least 2 non-masked points
if numpy.ma.count(y1) >= 2:
# input Y array is a masked array
cidx = numpy.zeros(len(y1),dtype=numpy.float64)
idx = numpy.where(numpy.ma.getmaskarray(y1)==True)[0]
cidx[idx] = numpy.float64(1)
idx = numpy.where(numpy.ma.getmaskarray(y1)==False)[0]
spl = InterpolatedUnivariateSpline(x1[idx], y1[idx], k=k, ext=ext)
y2 = spl(x2)
spl = InterpolatedUnivariateSpline(x1, cidx, k=k, ext=ext)
cidxi = spl(x2)
y2 = numpy.ma.masked_where(cidxi!=0,y2)
else:
msg = "Not enough points ( < 2) to interpolate"
logger.warning(msg)
#raise RuntimeError(msg)
y2 = numpy.ma.ones(len(x2))*float(c.missing_value)
else:
spl = InterpolatedUnivariateSpline(x1, y1, k=k, ext=ext)
y2 = spl(x2)
return y2
def perdelta(start, end, delta):
0
Source : model.py
with GNU General Public License v2.0
from tmiasko
with GNU General Public License v2.0
from tmiasko
def dict_to_jags(src):
"""Convert Python dictionary with array like values to format suitable
for use with JAGS.
* Returned arrays have at least one dimension.
* Empty arrays are removed from the dictionary.
* Masked values are replaced with JAGS_NA.
"""
dst = {}
for k, v in src.items():
if np.ma.is_masked(v):
v = np.ma.array(data=v, dtype=np.double, ndmin=1,
fill_value=JAGS_NA)
v = np.ma.filled(v)
else:
v = np.atleast_1d(v)
if not np.size(v):
continue
dst[k] = v
return dst
def dict_from_jags(src):
See More Examples