def test_binary_repr_0_width(self, level=rlevel):
assert_equal(np.binary_repr(0, width=3), '000')
python类binary_repr()的实例源码
def test_zero(self):
assert_equal(np.binary_repr(0), '0')
def test_large(self):
assert_equal(np.binary_repr(10736848), '101000111101010011010000')
def test_negative(self):
assert_equal(np.binary_repr(-1), '-1')
assert_equal(np.binary_repr(-1, width=8), '11111111')
def Bin(a,N):
a_bin = np.binary_repr(a)
while len(a_bin) < N:
a_bin = '0'+a_bin
return a_bin
def test_binary_repr_0(self,level=rlevel):
# Ticket #151
assert_equal('0', np.binary_repr(0))
def test_binary_repr_0_width(self, level=rlevel):
assert_equal(np.binary_repr(0, width=3), '000')
def test_zero(self):
assert_equal(np.binary_repr(0), '0')
def test_large(self):
assert_equal(np.binary_repr(10736848), '101000111101010011010000')
def test_negative(self):
assert_equal(np.binary_repr(-1), '-1')
assert_equal(np.binary_repr(-1, width=8), '11111111')
def test_binary_repr_0(self, level=rlevel):
# Ticket #151
assert_equal('0', np.binary_repr(0))
def test_binary_repr_0_width(self, level=rlevel):
assert_equal(np.binary_repr(0, width=3), '000')
def test_zero(self):
assert_equal(np.binary_repr(0), '0')
def test_positive(self):
assert_equal(np.binary_repr(10), '1010')
assert_equal(np.binary_repr(12522),
'11000011101010')
assert_equal(np.binary_repr(10736848),
'101000111101010011010000')
def test_negative(self):
assert_equal(np.binary_repr(-1), '-1')
assert_equal(np.binary_repr(-10), '-1010')
assert_equal(np.binary_repr(-12522),
'-11000011101010')
assert_equal(np.binary_repr(-10736848),
'-101000111101010011010000')
def test_insufficient_width_positive(self):
args = (10,)
kwargs = {'width': 2}
self.message = ("Insufficient bit width provided. This behavior "
"will raise an error in the future.")
self.assert_deprecated(np.binary_repr, args=args, kwargs=kwargs)
def test_insufficient_width_negative(self):
args = (-5,)
kwargs = {'width': 2}
self.message = ("Insufficient bit width provided. This behavior "
"will raise an error in the future.")
self.assert_deprecated(np.binary_repr, args=args, kwargs=kwargs)
def _getAllowedShapes(self, shape):
''' Return set of allowed shapes that can be squeezed into given shape.
Examples
--------
>>> PB = ParamBag() # fixing K,D doesn't matter
>>> PB._getAllowedShapes(())
set([()])
>>> PB._getAllowedShapes((1,))
set([(), (1,)])
>>> aSet = PB._getAllowedShapes((23,))
>>> sorted(aSet)
[(23,)]
>>> sorted(PB._getAllowedShapes((3,1)))
[(3,), (3, 1)]
>>> sorted(PB._getAllowedShapes((1,1)))
[(), (1,), (1, 1)]
'''
assert isinstance(shape, tuple)
allowedShapes = set()
if len(shape) == 0:
allowedShapes.add(tuple())
return allowedShapes
shapeVec = np.asarray(shape, dtype=np.int32)
onesMask = shapeVec == 1
keepMask = np.logical_not(onesMask)
nOnes = sum(onesMask)
for b in range(2**nOnes):
bStr = np.binary_repr(b)
bStr = '0' * (nOnes - len(bStr)) + bStr
keepMask[onesMask] = np.asarray([int(x) > 0 for x in bStr])
curShape = shapeVec[keepMask]
allowedShapes.add(tuple(curShape))
return allowedShapes
def _get_contrast_indices(effect_idx, n_factors):
"""Henson's factor coding, see num2binvec"""
binrepr = np.binary_repr(effect_idx, n_factors)
return np.array([int(i) for i in binrepr], dtype=int)
def activate_network(self, num_activations=1):
"""Activates the Markov Network
Parameters
----------
num_activations: int (default: 1)
The number of times the Markov Network should be activated
Returns
-------
None
"""
original_input_values = np.copy(self.states[:self.num_input_states])
for _ in range(num_activations):
for markov_gate, mg_input_ids, mg_output_ids in zip(self.markov_gates, self.markov_gate_input_ids, self.markov_gate_output_ids):
# Determine the input values for this Markov Gate
mg_input_values = self.states[mg_input_ids]
mg_input_index = int(''.join([str(int(val)) for val in mg_input_values]), base=2)
# Determine the corresponding output values for this Markov Gate
roll = np.random.uniform()
mg_output_index = np.where(markov_gate[mg_input_index, :] >= roll)[0][0]
mg_output_values = np.array(list(np.binary_repr(mg_output_index, width=len(mg_output_ids))), dtype=np.uint8)
self.states[mg_output_ids] = np.bitwise_or(self.states[mg_output_ids], mg_output_values)
self.states[:self.num_input_states] = original_input_values