本文整理汇总了Python中numpy.ma.masked_array方法的典型用法代码示例。如果您正苦于以下问题:Python ma.masked_array方法的具体用法?Python ma.masked_array怎么用?Python ma.masked_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ma
的用法示例。
在下文中一共展示了ma.masked_array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: objify
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def objify(self, doc, columns=None):
"""
Decode a Pymongo SON object into an Pandas DataFrame
"""
cols = columns or doc[METADATA][COLUMNS]
data = {}
for col in cols:
# if there is missing data in a chunk, we can default to NaN
# and pandas will autofill the missing values to the correct length
if col not in doc[METADATA][LENGTHS]:
d = [np.nan]
else:
d = decompress(doc[DATA][doc[METADATA][LENGTHS][col][0]: doc[METADATA][LENGTHS][col][1] + 1])
# d is ready-only but that's not an issue since DataFrame will copy the data anyway.
d = np.frombuffer(d, doc[METADATA][DTYPE][col])
if MASK in doc[METADATA] and col in doc[METADATA][MASK]:
mask_data = decompress(doc[METADATA][MASK][col])
mask = np.frombuffer(mask_data, 'bool')
d = ma.masked_array(d, mask)
data[col] = d
# Copy into
return pd.DataFrame(data, columns=cols, copy=True)[cols]
示例2: fillValuesToNan
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def fillValuesToNan(masked_array):
""" Replaces the fill_values of the masked array by NaNs
If the array is None or it does not contain floating point values, it cannot contain NaNs.
In that case the original array is returned.
"""
if masked_array is not None and masked_array.dtype.kind == 'f':
check_class(masked_array, ma.masked_array)
logger.debug("Replacing fill_values by NaNs")
masked_array[:] = ma.filled(masked_array, np.nan)
masked_array.set_fill_value(np.nan)
else:
return masked_array
#TODO: does recordMask help here?
# https://docs.scipy.org/doc/numpy/reference/maskedarray.baseclass.html#numpy.ma.MaskedArray.recordmask
示例3: fill_values_to_nan
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def fill_values_to_nan(masked_array):
""" Replaces the fill_values of the masked array by NaNs
If the array is None or it does not contain floating point values, it cannot contain NaNs.
In that case the original array is returned.
"""
if masked_array is not None and masked_array.dtype.kind == 'f':
check_class(masked_array, ma.masked_array)
logger.debug("Replacing fill_values by NaNs")
masked_array[:] = ma.filled(masked_array, np.nan)
masked_array.set_fill_value(np.nan)
else:
return masked_array
# Needed because boolean QSettings in Pyside are converted incorrect the second
# time in Windows (and Linux?) because of a bug in Qt. See:
# https://www.mail-archive.com/pyside@lists.pyside.org/msg00230.html
示例4: init_buffer
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def init_buffer(self):
#we only accept masked files
assert self.video_file.endswith('hdf5')
with tables.File(self.video_file, 'r') as fid:
masks = fid.get_node('/mask')
#here i am using a masked numpy array to deal with the zeroed background
last_frame = self.buff_size*self.frame_gap - 1
self.buffer = masks[:last_frame + 1:self.frame_gap]
self.buffer = ma.masked_array(self.buffer, self.buffer==0)
self.buffer_ind = self.buffer.shape[0] - 1
self.last_frame = last_frame
if '/full_data' in fid:
#here i am using as the canonical background the results of using the reducing function in all the full frames
full_data = fid.get_node('/full_data')
self.full_img = self.reduce_func(full_data, axis=0)
示例5: test_image_mask
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_image_mask(self):
"""Test image mask."""
im1 = image("testimg", shape=[2, 3])
marr = nma.masked_array(numpy.array([[1, 2, 3], [4, 5, 6]]),
mask=[[False, True, False], [False, False, True]])
im1.put(marr)
numpy.testing.assert_equal(im1.getdata(), marr.data)
numpy.testing.assert_equal(im1.getmask(), marr.mask)
im1.putmask(numpy.array([[True, False, True],
[False, False, False]]), (0, 0), (0, 1))
numpy.testing.assert_equal(im1.getmask(),
numpy.array([[True, False, True],
[False, False, False]]))
numpy.testing.assert_equal(im1.getdata(),
numpy.array([[1, 2, 3], [4, 5, 6]]))
im1.putdata(numpy.array([0, 1, 0]), (0, 0), (0, 1))
numpy.testing.assert_equal(im1.getdata(),
numpy.array([[0, 1, 0], [4, 5, 6]]))
示例6: test_simulation_can_recover_from_sucide_move_black
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_simulation_can_recover_from_sucide_move_black(self):
model = self.model
board = self.board
give_two_eyes(board, 'B')
policies, values = model.predict_on_batch(board)
policy = policies[0]
if np.argmax(policy) == PASS:
policy[0], policy[PASS] = policy[PASS], policy[0] # Make best move sucide
mask = legal_moves(board)
policy = ma.masked_array(policy, mask=mask)
self.assertEqual(np.argmax(policy), 0) # Best option in policy is sucide
else:
print("Warning, policy is not great")
self.assertEqual(np.argmax(policy), 0) # Best option in policy is sucide
tree = Tree()
tree.new_tree(policy, board)
chosen_play = select_play(policy, board, mcts_simulations=128, mcts_tree=tree.tree, temperature=0, model=model)
# First simulation chooses pass, second simulation chooses sucide (p is still higher),
# then going deeper it chooses pass again (value is higher)
self.assertEqual(chosen_play, PASS) # Pass move is best option
示例7: test_simulation_can_recover_from_sucide_move_white
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_simulation_can_recover_from_sucide_move_white(self):
model = self.model
board, player = game_init()
give_two_eyes(board, 'W')
policies, values = model.predict_on_batch(board)
policy = policies[0]
if np.argmax(policy) == PASS:
policy[0], policy[PASS] = policy[PASS], policy[0] # Make best move sucide
mask = legal_moves(board)
policy = ma.masked_array(policy, mask=mask)
self.assertEqual(np.argmax(policy), 0) # Best option in policy is sucide
else:
print("Warning, policy is not great")
tree = Tree()
tree.new_tree(policy, board, move=2)
chosen_play = select_play(policy, board, mcts_simulations=128, mcts_tree=tree.tree, temperature=0, model=model)
# First simulation chooses pass, second simulation chooses sucide (p is still higher),
# then going deeper it chooses pass again (value is higher)
self.assertEqual(chosen_play, PASS) # Pass move is best option
示例8: empty
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def empty(self, process_tile):
"""
Return empty data.
Parameters
----------
process_tile : ``BufferedTile``
must be member of process ``TilePyramid``
Returns
-------
empty data : array
empty array with data type provided in output profile
"""
profile = self.profile(process_tile)
return ma.masked_array(
data=np.full(
(profile["count"], ) + process_tile.shape,
profile["nodata"],
dtype=profile["dtype"]
),
mask=True
)
示例9: empty
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def empty(self, process_tile):
"""
Return empty data.
Parameters
----------
process_tile : ``BufferedTile``
must be member of process ``TilePyramid``
Returns
-------
empty data : array
empty array with data type given in output parameters
"""
bands = (
self.output_params["bands"]
if "bands" in self.output_params
else PNG_DEFAULT_PROFILE["count"]
)
return ma.masked_array(
data=ma.zeros((bands, ) + process_tile.shape),
mask=ma.zeros((bands, ) + process_tile.shape),
dtype=PNG_DEFAULT_PROFILE["dtype"]
)
示例10: test_write_raster_window_memory
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_write_raster_window_memory():
"""Basic output format writing."""
path = "memoryfile"
# standard tile
tp = BufferedTilePyramid("geodetic")
tile = tp.tile(5, 5, 5)
data = ma.masked_array(np.ones((2, ) + tile.shape))
for out_profile in [
dict(
driver="GTiff", count=2, dtype="uint8", compress="lzw", nodata=0,
height=tile.height, width=tile.width, affine=tile.affine),
dict(
driver="GTiff", count=2, dtype="uint8", compress="deflate",
nodata=0, height=tile.height, width=tile.width,
affine=tile.affine),
dict(
driver="PNG", count=2, dtype="uint8", nodata=0, height=tile.height,
width=tile.width, compress=None, affine=tile.affine),
]:
with pytest.raises(DeprecationWarning):
write_raster_window(
in_tile=tile, in_data=data, out_profile=out_profile, out_path=path)
示例11: test_prepare_array_maskedarrays
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_prepare_array_maskedarrays():
"""Convert masked array data into a proper array."""
# input is ma.masked_array
data = ma.empty((1, 1, 1))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# input is ma.masked_array with full mask
data = ma.masked_array(data=np.ones((1, 1, 1)), mask=np.ones((1, 1, 1)))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
示例12: test_prepare_array_ndarrays
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_prepare_array_ndarrays():
"""Convert ndarray data into a proper array."""
# input is np.ndarray
data = np.zeros((1, 1, 1))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# input is 2D np.ndarray
data = np.zeros((1, 1))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
示例13: test_read_existing_output
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_read_existing_output(mp_tmpdir, cleantopo_tl):
"""Read existing process output."""
# raster data
with mapchete.open(cleantopo_tl.path) as mp:
tile = mp.config.process_pyramid.tile(5, 0, 0)
# process and save
mp.get_raw_output(tile)
data = mp.config.output.read(tile)
assert data.any()
assert isinstance(data, ma.masked_array)
assert not data.mask.all()
# read data from Mapchete class
data = mp.config.output.read(tile)
assert data.any()
assert isinstance(data, ma.masked_array)
assert not data.mask.all()
示例14: test_record_array_with_object_field
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_record_array_with_object_field():
# Trac #1839
y = ma.masked_array(
[(1, '2'), (3, '4')],
mask=[(0, 0), (0, 1)],
dtype=[('a', int), ('b', object)])
# getting an item used to fail
y[1]
示例15: test_record_array_with_object_field
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import masked_array [as 别名]
def test_record_array_with_object_field():
# Trac #1839
y = ma.masked_array(
[(1, '2'), (3, '4')],
mask=[(0, 0), (0, 1)],
dtype=[('a', int), ('b', np.object)])
# getting an item used to fail
y[1]