def calculate_plane_histogram(plane, doseplane, dosegridpoints,
maxdose, dd, id, structure, hist):
"""Calculate the DVH for the given plane in the structure."""
contours = [[x[0:2] for x in c['data']] for c in plane]
# If there is no dose for the current plane, go to the next plane
if not len(doseplane):
return (np.arange(0, maxdose), 0)
# Create a zero valued bool grid
grid = np.zeros((dd['rows'], dd['columns']), dtype=np.uint8)
# Calculate the histogram for each contour in the plane
# and boolean xor to remove holes
for i, contour in enumerate(contours):
m = get_contour_mask(dd, id, dosegridpoints, contour)
grid = np.logical_xor(m.astype(np.uint8), grid).astype(np.bool)
hist, vol = calculate_contour_dvh(
grid, doseplane, maxdose, dd, id, structure)
return (hist, vol)
python类logical_xor()的实例源码
def _gene_signature(self,wm,size,key):
'''????????????????????????'''
wm = cv2.resize(wm,(size,size))
wU,_,wV = np.linalg.svd(np.mat(wm))
sumU = np.sum(np.array(wU),axis=0)
sumV = np.sum(np.array(wV),axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
sqrts = int(np.sqrt(size))
return np.array(signature,dtype=np.int8).reshape((sqrts,sqrts))
def _gene_signature(self,wm,key):
'''????????????????????????'''
wm = cv2.resize(wm,(256,256))
wU,_,wV = np.linalg.svd(np.mat(wm))
sumU = np.sum(np.array(wU),axis=0)
sumV = np.sum(np.array(wV),axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
return np.array(signature,dtype=np.int8)
def _gene_signature(self,wU,wV,key):
'''????????????????????????'''
sumU = np.sum(wU,axis=0)
sumV = np.sum(wV,axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
return np.array(signature,dtype=np.int8)
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def calculate_plane_histogram(plane, doseplane, dosegridpoints, maxdose, dd,
id, structure, hist):
"""Calculate the DVH for the given plane in the structure."""
contours = [[x[0:2] for x in c['data']] for c in plane]
# Create a zero valued bool grid
grid = np.zeros((dd['rows'], dd['columns']), dtype=np.uint8)
# Calculate the dose plane mask for each contour in the plane
# and boolean xor to remove holes
for i, contour in enumerate(contours):
m = get_contour_mask(dd, id, dosegridpoints, contour)
grid = np.logical_xor(m.astype(np.uint8), grid).astype(np.bool)
hist, vol = calculate_contour_dvh(grid, doseplane, maxdose, dd, id,
structure)
return (hist, vol)
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
test_umath.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
test_ufunc.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def data_cleaning(file_path):
data = pd.read_csv(file_path, index_col=False)
data.drop(['Street', 'Utilities', 'Condition2', 'RoofMatl', 'Alley',
'GarageYrBlt', 'GarageCond', 'PoolQC', 'MiscFeature'],
axis=1, inplace=True)
# marked as NA in BsmtExposure and not NA in other Bsmt Attributes
data.loc[np.logical_xor(data['BsmtCond'].isnull(), data['BsmtExposure'].isnull()), 'BsmtExposure'] = 'No'
# LotFrontage's N/A is assigned zero, will it cause problem?
data.fillna(value={'MasVnrType': 'None', 'MasVnrArea': 0, 'BsmtQual': 'NoBsmt', 'BsmtCond': 'NoBsmt',
'BsmtExposure': 'NoBsmt', 'BsmtFinType1': 'NoBsmt', 'BsmtFinType2': 'NoBsmt',
'Electrical': 'SBrkr', 'FireplaceQu': 'NoFP', 'GarageType': 'Noga',
'GarageFinish': 'Noga', 'GarageQual': 'Noga', 'Fence': 'NoFence', 'LotFrontage': 0},
inplace=True)
data.loc[:, 'YrSold'] = 2016 - data.loc[:, 'YrSold']
data.loc[data.loc[:, 'PoolArea'] != 0, 'PoolArea'] = 1
data.loc[:, 'Porch'] = np.sum(data.loc[:, ['EnclosedPorch', '3SsnPorch', 'ScreenPorch']], axis=1)
data.drop(['EnclosedPorch', '3SsnPorch', 'ScreenPorch'], axis=1, inplace=True)
data.replace({'BsmtFullBath': {3: 2},
'LotShape': {'IR3': 'IR2'}},
inplace=True)
data.columns
# examine columns containing NA value
print(data)
print(data.columns[np.sum(data.isnull(), axis=0) != 0])
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
sculpture_gen.py 文件源码
项目:Simple-User-Input-Sculpture-Generation
作者: ClaireKincaid
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def m_menu(self):
"""Runs the Mathmatically defined sculpture menu item."""
sin, cos = np.sin, np.cos
res = raw_input("Enter a functional definition of a volume (x**2+y**2+z**2 < 1) \n")
self.user_text = res
self.volume_data = self.bool_ops()
self.create_iso_surface(.7)
while True:
res = raw_input("Enter another functional definition of a volume (x**2+y**2+z**2 < 1) \n")
self.user_text = res
self.sec_volume_data = self.bool_ops()
self.create_iso_surface(.7, second=True)
res = raw_input("Enter a boolean operation to do with the previous solid (a = and, o = or, n = not, x = xor):\n")
if res == "a":
self.sec_volume_data = 0+ np.logical_and(my_sculpture.volume_data, my_sculpture.bool_ops())
elif res == "o":
self.sec_volume_data = 0+ np.logical_or(my_sculpture.volume_data, my_sculpture.bool_ops())
elif res == "n":
self.sec_volume_data = 0+ np.logical_not(my_sculpture.volume_data, my_sculpture.bool_ops())
elif res == "x":
self.sec_volume_data = 0+ np.logical_xor(my_sculpture.volume_data, my_sculpture.bool_ops())
self.create_iso_surface(.7, second=True)
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def selectShell(ref_coords, coords, R, sw):
"""
Return indices of the particles within the spherical shell of
inner radius (R-sw) and outer radius R, ie the shell.
Parameters
----------
ref_coords : array_like (n_atoms, n_dim)
Reference atoms positions
coords : array_like (n_atoms, n_dim)
atoms positions
R : float
distance to any atoms
Returns
-------
array
particle indices within shell
"""
if R < sw:
raise RuntimeError("selection radius smaller then shell width")
body_query = get_selection(coords, ref_coords, R=R)
core_query = get_selection(coords, ref_coords, R=R - sw)
query = np.logical_xor(body_query, core_query)
return np.where(query)
def test_truth_table_logical(self):
# 2, 3 and 4 serves as true values
input1 = [0, 0, 3, 2]
input2 = [0, 4, 0, 2]
typecodes = (np.typecodes['AllFloat']
+ np.typecodes['AllInteger']
+ '?') # boolean
for dtype in map(np.dtype, typecodes):
arg1 = np.asarray(input1, dtype=dtype)
arg2 = np.asarray(input2, dtype=dtype)
# OR
out = [False, True, True, True]
for func in (np.logical_or, np.maximum):
assert_equal(func(arg1, arg2).astype(bool), out)
# AND
out = [False, False, False, True]
for func in (np.logical_and, np.minimum):
assert_equal(func(arg1, arg2).astype(bool), out)
# XOR
out = [False, True, True, False]
for func in (np.logical_xor, np.not_equal):
assert_equal(func(arg1, arg2).astype(bool), out)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def encodeMask(M):
"""
Encode binary mask M using run-length encoding.
:param M (bool 2D array) : binary mask to encode
:return: R (object RLE) : run-length encoding of binary mask
"""
[h, w] = M.shape
M = M.flatten(order='F')
N = len(M)
counts_list = []
pos = 0
# counts
counts_list.append(1)
diffs = np.logical_xor(M[0:N-1], M[1:N])
for diff in diffs:
if diff:
pos +=1
counts_list.append(1)
else:
counts_list[pos] += 1
# if array starts from 1. start with 0 counts for 0
if M[0] == 1:
counts_list = [0] + counts_list
return {'size': [h, w],
'counts': counts_list ,
}
def _gene_signature(wm,size,key):
'''
????????????????????????
wm : ????
size ??????????
key ????????
'''
wm = cv2.resize(wm,(size,size))
wU,_,wV = np.linalg.svd(np.mat(wm))
sumU = np.sum(np.array(wU),axis=0)
sumV = np.sum(np.array(wV),axis=0)
sumU_mid = np.median(sumU)
sumV_mid = np.median(sumV)
sumU=np.array([1 if sumU[i] >sumU_mid else 0 for i in range(len(sumU)) ])
sumV=np.array([1 if sumV[i] >sumV_mid else 0 for i in range(len(sumV)) ])
uv_xor=np.logical_xor(sumU,sumV)
np.random.seed(key)
seq=np.random.randint(2,size=len(uv_xor))
signature = np.logical_xor(uv_xor, seq)
sqrts = int(np.sqrt(size))
return np.array(signature,dtype=np.int8).reshape((sqrts,sqrts))
def test_logical_and_or_xor(self):
assert_array_equal(self.t | self.t, self.t)
assert_array_equal(self.f | self.f, self.f)
assert_array_equal(self.t | self.f, self.t)
assert_array_equal(self.f | self.t, self.t)
np.logical_or(self.t, self.t, out=self.o)
assert_array_equal(self.o, self.t)
assert_array_equal(self.t & self.t, self.t)
assert_array_equal(self.f & self.f, self.f)
assert_array_equal(self.t & self.f, self.f)
assert_array_equal(self.f & self.t, self.f)
np.logical_and(self.t, self.t, out=self.o)
assert_array_equal(self.o, self.t)
assert_array_equal(self.t ^ self.t, self.f)
assert_array_equal(self.f ^ self.f, self.f)
assert_array_equal(self.t ^ self.f, self.t)
assert_array_equal(self.f ^ self.t, self.t)
np.logical_xor(self.t, self.t, out=self.o)
assert_array_equal(self.o, self.f)
assert_array_equal(self.nm & self.t, self.nm)
assert_array_equal(self.im & self.f, False)
assert_array_equal(self.nm & True, self.nm)
assert_array_equal(self.im & False, self.f)
assert_array_equal(self.nm | self.t, self.t)
assert_array_equal(self.im | self.f, self.im)
assert_array_equal(self.nm | True, self.t)
assert_array_equal(self.im | False, self.im)
assert_array_equal(self.nm ^ self.t, self.im)
assert_array_equal(self.im ^ self.f, self.im)
assert_array_equal(self.nm ^ True, self.im)
assert_array_equal(self.im ^ False, self.im)
def encodeMask(M):
"""
Encode binary mask M using run-length encoding.
:param M (bool 2D array) : binary mask to encode
:return: R (object RLE) : run-length encoding of binary mask
"""
[h, w] = M.shape
M = M.flatten(order='F')
N = len(M)
counts_list = []
pos = 0
# counts
counts_list.append(1)
diffs = np.logical_xor(M[0:N-1], M[1:N])
for diff in diffs:
if diff:
pos +=1
counts_list.append(1)
else:
counts_list[pos] += 1
# if array starts from 1. start with 0 counts for 0
if M[0] == 1:
counts_list = [0] + counts_list
return {'size': [h, w],
'counts': counts_list ,
}
def xor_(a: Bool = True, b: Bool = False) -> Bool:
return np.logical_xor(a, b)
def count_matrices(data, start_state=None, threshold=None, display=False):
num_clusters = 2
if threshold is None:
clust = clusterer(data)
state = clust.fit_predict(data.reshape(-1, 1)).reshape(data.shape)
else:
logger.debug("Cluster data based on threshold = {}".format(threshold))
state = data > threshold
init_state = state[:,:,0]
final_state = state[:,:,1]
switched = np.logical_xor(init_state, final_state)
init_state_frac = [np.mean(init_state == ct) for ct in range(num_clusters)]
for ct, fraction in enumerate(init_state_frac):
logger.debug("Initial fraction of state %d: %f" %(ct, fraction))
if start_state is not None and start_state in range(num_clusters):
start_stt = start_state
else:
start_stt = np.argmax(init_state_frac)
logger.debug("Start state set to state: {}".format(start_stt))
logger.debug("Switched state is state: {}".format(1-start_stt))
# This array contains a 2x2 count_matrix for each coordinate tuple
count_mat = np.zeros((init_state.shape[0], 2, 2))
# count_mat = np.zeros((2,2), dtype=np.int)
count_mat[:,0,0] = np.logical_and(init_state == 0, np.logical_not(switched)).sum(axis=-1)
count_mat[:,0,1] = np.logical_and(init_state == 0, switched).sum(axis=-1)
count_mat[:,1,0] = np.logical_and(init_state == 1, switched).sum(axis=-1)
count_mat[:,1,1] = np.logical_and(init_state == 1, np.logical_not(switched)).sum(axis=-1)
return count_mat, start_stt
def count_matrices_ber(data, start_state=None, threshold=None, display=None):
num_clusters = 2
if threshold is None:
clust = clusterer(data)
state = clust.fit_predict(data.reshape(-1, 1)).reshape((-1,2))
else:
logger.debug("Cluster data based on threshold = {}".format(threshold))
state = data > threshold
state = state.reshape((-1,2))
init_state = state[:,0]
final_state = state[:,1]
switched = np.logical_xor(init_state, final_state)
init_state_frac = [np.mean(init_state == ct) for ct in range(num_clusters)]
for ct, fraction in enumerate(init_state_frac):
logger.debug("Initial fraction of state %d: %f" %(ct, fraction))
if start_state is not None and start_state in range(num_clusters):
start_stt = start_state
else:
start_stt = np.argmax(init_state_frac)
logger.debug("Start state set to state: {}".format(start_stt))
logger.debug("Switched state is state: {}".format(1-start_stt))
# This array contains a 2x2 count_matrix for each coordinate tuple
count_mat = np.zeros((2, 2))
# count_mat = np.zeros((2,2), dtype=np.int)
count_mat[0,0] = np.logical_and(init_state == 0, np.logical_not(switched)).sum()
count_mat[0,1] = np.logical_and(init_state == 0, switched).sum()
count_mat[1,0] = np.logical_and(init_state == 1, switched).sum()
count_mat[1,1] = np.logical_and(init_state == 1, np.logical_not(switched)).sum()
return count_mat, start_stt
def encodeMask(M):
"""
Encode binary mask M using run-length encoding.
:param M (bool 2D array) : binary mask to encode
:return: R (object RLE) : run-length encoding of binary mask
"""
[h, w] = M.shape
M = M.flatten(order='F')
N = len(M)
counts_list = []
pos = 0
# counts
counts_list.append(1)
diffs = np.logical_xor(M[0:N - 1], M[1:N])
for diff in diffs:
if diff:
pos += 1
counts_list.append(1)
else:
counts_list[pos] += 1
# if array starts from 1. start with 0 counts for 0
if M[0] == 1:
counts_list = [0] + counts_list
return {'size': [h, w],
'counts': counts_list,
}