def kernel(self, X, Y=None):
"""
Computes the hypercube kerpy k(x,y)=tanh(gamma)^d(x,y), where d is the
Hamming distance between x and y
X - 2d numpy.bool8 array, samples on right left side
Y - 2d numpy.bool8 array, samples on left hand side.
Can be None in which case its replaced by X
"""
if not type(X) is numpy.ndarray:
raise TypeError("X must be numpy array")
if not len(X.shape) == 2:
raise ValueError("X must be 2D numpy array")
if not X.dtype == numpy.bool8:
raise ValueError("X must be boolean numpy array")
if not Y is None:
if not type(Y) is numpy.ndarray:
raise TypeError("Y must be None or numpy array")
if not len(Y.shape) == 2:
raise ValueError("Y must be None or 2D numpy array")
if not Y.dtype == numpy.bool8:
raise ValueError("Y must be boolean numpy array")
if not X.shape[1] == Y.shape[1]:
raise ValueError("X and Y must have same dimension if Y is not None")
# un-normalise normalised hamming distance in both cases
if Y is None:
K = tanh(self.gamma) ** squareform(pdist(X, 'hamming') * X.shape[1])
else:
K = tanh(self.gamma) ** (cdist(X, Y, 'hamming') * X.shape[1])
return K
python类bool8()的实例源码
test_excel.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def test_bool_types(self):
_skip_if_no_xlrd()
for np_type in (np.bool8, np.bool_):
with ensure_clean(self.ext) as path:
# Test np.bool values read come back as float.
frame = (DataFrame([1, 0, True, False], dtype=np_type))
frame.to_excel(path, 'test1')
reader = ExcelFile(path)
recons = read_excel(reader, 'test1').astype(np_type)
tm.assert_frame_equal(frame, recons)
def __read_segment_list_v9(self):
"""
Read a list of Segments with comments.
This is version 9 of the data sequence.
This is the same as __read_segment_list_v8, but contains some
additional annotations. These annotations are added to the Segment.
--------------------------------------------------------
Returns a list of the Segments created with this method.
The returned objects are already added to the Block.
ID: 29120
"""
# segment_collection_v8 -- this is based off a segment_collection_v8
segments = self.__read_segment_list_v8()
# uint8
feature_type = np.fromfile(self._fsrc, dtype=np.uint8,
count=1)[0]
# uint8
go_by_closest_unit_center = np.fromfile(self._fsrc, dtype=np.bool8,
count=1)[0]
# uint8
include_unit_bounds = np.fromfile(self._fsrc, dtype=np.bool8,
count=1)[0]
# create a dictionary of the annotations
annotations = {'feature_type': feature_type,
'go_by_closest_unit_center': go_by_closest_unit_center,
'include_unit_bounds': include_unit_bounds}
# add the annotations to each Segment
for segment in segments:
segment.annotations.update(annotations)
return segments
def __read_segment_list_v9(self):
"""
Read a list of Segments with comments.
This is version 9 of the data sequence.
This is the same as __read_segment_list_v8, but contains some
additional annotations. These annotations are added to the Segment.
--------------------------------------------------------
Returns a list of the Segments created with this method.
The returned objects are already added to the Block.
ID: 29120
"""
# segment_collection_v8 -- this is based off a segment_collection_v8
segments = self.__read_segment_list_v8()
# uint8
feature_type = np.fromfile(self._fsrc, dtype=np.uint8,
count=1)[0]
# uint8
go_by_closest_unit_center = np.fromfile(self._fsrc, dtype=np.bool8,
count=1)[0]
# uint8
include_unit_bounds = np.fromfile(self._fsrc, dtype=np.bool8,
count=1)[0]
# create a dictionary of the annotations
annotations = {'feature_type': feature_type,
'go_by_closest_unit_center': go_by_closest_unit_center,
'include_unit_bounds': include_unit_bounds}
# add the annotations to each Segment
for segment in segments:
segment.annotations.update(annotations)
return segments
def omit_hsp_points(self, distance=0, reset=False):
"""Exclude head shape points that are far away from the MRI head
Parameters
----------
distance : float
Exclude all points that are further away from the MRI head than
this distance. Previously excluded points are still excluded unless
reset=True is specified. A value of distance <= 0 excludes nothing.
reset : bool
Reset the filter before calculating new omission (default is
False).
"""
distance = float(distance)
if reset:
logger.info("Coregistration: Reset excluded head shape points")
with warnings.catch_warnings(record=True): # Traits None comp
self.hsp.points_filter = None
if distance <= 0:
return
# find the new filter
hsp_pts = self.transformed_hsp_points
mri_pts = self.transformed_mri_points
point_distance = _point_cloud_error(hsp_pts, mri_pts)
new_sub_filter = point_distance <= distance
n_excluded = np.sum(new_sub_filter == False) # noqa
logger.info("Coregistration: Excluding %i head shape points with "
"distance >= %.3f m.", n_excluded, distance)
# combine the new filter with the previous filter
old_filter = self.hsp.points_filter
if old_filter is None:
new_filter = new_sub_filter
else:
new_filter = np.ones(len(self.hsp.raw_points), np.bool8)
new_filter[old_filter] = new_sub_filter
# set the filter
with warnings.catch_warnings(record=True): # comp to None in Traits
self.hsp.points_filter = new_filter
def execute(self):
self.resulting_image = None
f_first = True
resimg = self.images_iterator.read_reference_image()
shape = resimg.shape
resimg.image[:] = 2**resimg.color_depth / 2
avrimg = Image(ishape=shape, dtype=resimg.dtype)
std = np.zeros(shape[:2], dtype=resimg.dtype) + 2**resimg.color_depth
dist = np.zeros(shape[:2], dtype=resimg.dtype)
flags = np.zeros(shape[:2], dtype=np.bool8)
iter_cnt = 5
for itr in range(iter_cnt):
invalid_imgs = []
img_cnt = 0.0
for imgarr in self.images_iterator:
if shape != imgarr.shape:
self.images_iterator.discard_image()
continue
img_cnt += 1
dist[:] = np.sqrt(
np.power(resimg.image[:, :, 0] - imgarr.image[:, :, 0], 2) +
np.power(resimg.image[:, :, 1] - imgarr.image[:, :, 1], 2) +
np.power(resimg.image[:, :, 2] - imgarr.image[:, :, 2], 2))
ca = time.clock()
flags[:] = False
flags[:] = dist[:] < std[:] / np.exp(np.float(itr) / 10.0)
avrimg.image[flags] = avrimg.image[flags] + imgarr.image[flags]
flags[:] = np.logical_not(flags)
avrimg.image[flags] = avrimg.image[flags] + resimg.image[flags]
cb = time.clock()
print(cb - ca)
resimg.image[:] = avrimg.image[:] / img_cnt
std[:] = 0.0
for imgarr in self.images_iterator:
std[:] = (std[:] +
(np.power(resimg.image[:, :, 0] - imgarr.image[:, :, 0], 2) +
np.power(resimg.image[:, :, 1] - imgarr.image[:, :, 1], 2) +
np.power(resimg.image[:, :, 2] - imgarr.image[:, :, 2], 2)))
std[:] = np.sqrt(std[:] / img_cnt)
avrimg.image[:] = 0.0
self.resulting_image = resimg