def resample(image, scan, new_spacing=[1,1,1]):
# Determine current pixel spacing
spacing = map(float, ([scan[0].SliceThickness] + scan[0].PixelSpacing))
spacing = np.array(list(spacing))
#scan[2].SliceThickness
resize_factor = spacing / new_spacing
new_real_shape = image.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize_factor = new_shape / image.shape
new_spacing = spacing / real_resize_factor
image = scipy.ndimage.interpolation.zoom(image, real_resize_factor, mode='nearest') ### early orig modified
return image, new_spacing
python类round()的实例源码
def resample(image, scan, new_spacing=[1,1,1]):
# Determine current pixel spacing
spacing = map(float, ([scan[0].SliceThickness] + scan[0].PixelSpacing))
spacing = np.array(list(spacing))
resize_factor = spacing / new_spacing
new_real_shape = image.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize_factor = new_shape / image.shape
new_spacing = spacing / real_resize_factor
#image = scipy.ndimage.interpolation.zoom(image, real_resize_factor) # nor mode= "wrap"/xxx, nor cval=-1024 can ensure that the min and max values are unchanged .... # cval added
image = scipy.ndimage.interpolation.zoom(image, real_resize_factor, mode='nearest') ### early orig modified
#image = scipy.ndimage.zoom(image, real_resize_factor, order=1) # order=1 bilinear , preserves the min and max of the image -- pronbably better for us (also faster than spkine/order=2)
#image = scipy.ndimage.zoom(image, real_resize_factor, mode='nearest', order=1) # order=1 bilinear , preserves the min and max of the image -- pronbably better for us (also faster than spkine/order=2)
return image, new_spacing
def handle_data(self, data):
if self.target_shares == 0:
assert 0 not in self.portfolio.positions
self.order(self.sid(0), 10)
self.target_shares = 10
return
else:
print(self.portfolio)
assert self.portfolio.positions[0]['amount'] == \
self.target_shares, "Orders not filled immediately."
assert self.portfolio.positions[0]['last_sale_price'] == \
data[0].price, "Orders not filled at current price."
self.order_target_value(self.sid(0), 20)
self.target_shares = np.round(20 / data[0].price)
if isinstance(self.sid(0), Equity):
self.target_shares = np.round(20 / data[0].price)
if isinstance(self.sid(0), Future):
self.target_shares = np.round(
20 / (data[0].price * self.sid(0).multiplier))
def round_to_chunk_size(self, chunk_size, offset=Vec(0,0,0, dtype=int)):
"""
Align a potentially non-axis aligned bbox to the grid by rounding it
to the nearest grid lines.
Required:
chunk_size: arraylike (x,y,z), the size of chunks in the
dataset e.g. (64,64,64)
Optional:
offset: arraylike (x,y,z), the starting coordinate of the dataset
"""
chunk_size = np.array(chunk_size, dtype=np.float32)
result = self.clone()
result = result - offset
result.minpt = np.round(result.minpt / chunk_size) * chunk_size
result.maxpt = np.round(result.maxpt / chunk_size) * chunk_size
return result + offset
def draw_bounding_boxes(image, gt_boxes, im_info):
num_boxes = gt_boxes.shape[0]
gt_boxes_new = gt_boxes.copy()
gt_boxes_new[:,:4] = np.round(gt_boxes_new[:,:4].copy() / im_info[2])
disp_image = Image.fromarray(np.uint8(image[0]))
for i in xrange(num_boxes):
this_class = int(gt_boxes_new[i, 4])
disp_image = _draw_single_box(disp_image,
gt_boxes_new[i, 0],
gt_boxes_new[i, 1],
gt_boxes_new[i, 2],
gt_boxes_new[i, 3],
'N%02d-C%02d' % (i, this_class),
FONT,
color=STANDARD_COLORS[this_class % NUM_COLORS])
image[0, :] = np.array(disp_image)
return image
def quantized_forward_pass_cost_and_output(inputs, weights, scales, biases=None, quantization_method='round',
hidden_activations='relu', output_activation = 'relu', computation_calc='adds', seed=None):
"""
Do a forward pass of a discretized network, and return the (pseudo) computational cost and final output.
:param inputs: A (n_samples, n_dims) array of inputs
:param weights: A list of (n_dim_in, n_dim_out) arrays of weights
:param scales: A list of (w[0].shape[0], w[1].shape[0], ...) scales to multiply/divide by before/after the quantization
:param quantization_method: The method of quantization/discretization: 'round', 'uniform', None, ....
:param seed: A random seed or number generator
:return: n_ops, output_activation: Where:
n_ops is the (scalar) number of commputations required in the forward pass (only striclty true if scale is 'round', .. otherwise it's some kind of surrogate.
output_activation: A (n_samples, n_dims) array representing the output activations.
"""
activations = scaled_quantized_forward_pass(inputs= inputs, weights=weights, biases=biases, scales=scales,
hidden_activations=hidden_activations, output_activations=output_activation, quantization_method=quantization_method, rng=seed)
spike_activations = activations[1::3]
n_ops = sparse_nn_flop_count(spike_activations, [w.shape[1] for w in weights], mode=computation_calc) if quantization_method is not None else None
return n_ops, activations[-1]
def resample(patient, new_spacing=[1,1,1]):
scan = get_scan(patient)
image = get_3D_data(patient)
# Determine current pixel spacing
spacing = np.array([scan[0].SliceThickness] + scan[0].PixelSpacing, dtype=np.float32)
resize_factor = spacing / new_spacing
new_real_shape = image.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize_factor = new_shape / image.shape
new_spacing = spacing / real_resize_factor
image = nd.interpolation.zoom(image, real_resize_factor, mode='nearest')
return image
# For the sake of testing the network, we'll be using the sample dataset
# For this, we'll use the maximum size of the image
# and PAD any image with -1000 values which is smaller than that
#PS: only the first dimension is different in sample dataset
#which is not the case in actual dataset
def did_succeed( output_dict, cond_dict ):
'''
Used in rejection sampling:
for each row, determine if cond is satisfied
for every cond in cond_dict
success is hardcoded as round(label) being exactly equal
to the integer in cond_dict
'''
#definition success:
def is_win(key):
#cond=np.squeeze(cond_dict[key])
cond=np.squeeze(cond_dict[key])
val=np.squeeze(output_dict[key])
condition= np.round(val)==cond
return condition
scoreboard=[is_win(key) for key in cond_dict]
#print('scoreboard', scoreboard)
all_victories_bool=np.logical_and.reduce(scoreboard)
return all_victories_bool.flatten()
def func_impl(self, x):
objval, invalid = None, False
for i, t in enumerate(x):
if t < self.p_min[i] or t > self.p_max[i]:
objval = float("inf")
invalid = True
if not invalid:
x = [int(np.round(x_t)) if p_t is "integer" else x_t for p_t, x_t in zip(self.p_types, x)]
objval = self._coef * self.func(x)
print("{:5d} | {} | {:>15.5f}".format(
self.n_eval,
" | ".join(["{:>15.5f}".format(t) for t in x]),
self._coef * objval
))
self.n_eval += 1
return objval
def clean_height(df):
v = df.VALUE.astype(float)
idx = df.VALUEUOM.fillna('').apply(lambda s: 'in' in s.lower()) | df.MIMIC_LABEL.apply(lambda s: 'in' in s.lower())
v.ix[idx] = np.round(v[idx] * 2.54)
return v
# ETCO2: haven't found yet
# Urine output: ambiguous units (raw ccs, ccs/kg/hr, 24-hr, etc.)
# Tidal volume: tried to substitute for ETCO2 but units are ambiguous
# Glascow coma scale eye opening
# Glascow coma scale motor response
# Glascow coma scale total
# Glascow coma scale verbal response
# Heart Rate
# Respiratory rate
# Mean blood pressure
def test_sample_NormalFloatHyperparameter(self):
hp = NormalFloatHyperparameter("nfhp", 0, 1)
def actual_test():
rs = np.random.RandomState(1)
counts_per_bin = [0 for i in range(11)]
for i in range(100000):
value = hp.sample(rs)
index = min(max(int((round(value + 0.5)) + 5), 0), 9)
counts_per_bin[index] += 1
self.assertEqual([0, 4, 138, 2113, 13394, 34104, 34282, 13683,
2136, 146, 0], counts_per_bin)
return counts_per_bin
self.assertEqual(actual_test(), actual_test())
def round_solution_pool(pool, constraints):
pool.distinct().sort()
P = pool.P
L0_reg_ind = np.isnan(constraints['coef_set'].C_0j)
L0_max = constraints['L0_max']
rounded_pool = SolutionPool(P)
for solution in pool.solutions:
# sort from largest to smallest coefficients
feature_order = np.argsort([-abs(x) for x in solution])
rounded_solution = np.zeros(shape=(1, P))
l0_norm_count = 0
for k in range(0, P):
j = feature_order[k]
if not L0_reg_ind[j]:
rounded_solution[0, j] = np.round(solution[j], 0)
elif l0_norm_count < L0_max:
rounded_solution[0, j] = np.round(solution[j], 0)
l0_norm_count += L0_reg_ind[j]
rounded_pool.add(objvals=np.nan, solutions=rounded_solution)
rounded_pool.distinct().sort()
return rounded_pool
def resize(im, target_size, max_size):
"""
only resize input image to target size and return scale
:param im: BGR image input by opencv
:param target_size: one dimensional size (the short side)
:param max_size: one dimensional max size (the long side)
:return:
"""
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
im_scale = float(target_size) / float(im_size_min)
# prevent bigger axis from being more than max_size:
if np.round(im_scale * im_size_max) > max_size:
im_scale = float(max_size) / float(im_size_max)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
return im, im_scale
def resize(im, target_size, max_size):
"""
only resize input image to target size and return scale
:param im: BGR image input by opencv
:param target_size: one dimensional size (the short side)
:param max_size: one dimensional max size (the long side)
:return:
"""
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
im_scale = float(target_size) / float(im_size_min)
if np.round(im_scale * im_size_max) > max_size:
im_scale = float(max_size) / float(im_size_max)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
return im, im_scale
def generateTickText(tickValue, ratio, baseline=False):
multStep = 1000.
multipliers = [
dict(suffix='', mult=pow(multStep, 0)),
dict(suffix='k', mult=pow(multStep, 1)),
dict(suffix='M', mult=pow(multStep, 2)),
dict(suffix='G', mult=pow(multStep, 3)),
]
multiplier = multipliers[0]
for m in multipliers:
if np.round(tickValue / m['mult'], decimals=2) >= 1:
multiplier = m
baseText = float('%.3g' % np.round(tickValue / multiplier['mult'], decimals=2))
baseText = int(baseText) if int(baseText) == baseText else baseText
suffix = multiplier['suffix']
percent = float('%.1f' % (100 * ratio))
percent = int(percent) if percent == int(percent) else percent
return '%s%s [%s%%]' % (baseText, suffix, percent)
def generateTickText(tickValue, ratio, baseline = False):
multStep = 1000.
multipliers = [
dict(suffix='', mult=pow(multStep, 0)),
dict(suffix='k', mult=pow(multStep, 1)),
dict(suffix='M', mult=pow(multStep, 2)),
dict(suffix='G', mult=pow(multStep, 3)),
]
multiplier = multipliers[0]
for m in multipliers:
if np.round(tickValue / m['mult']) >= 1:
multiplier = m
baseText = float('%.3g' % np.round(tickValue / multiplier['mult']))
baseText = int(baseText) if int(baseText) == baseText else baseText
suffix = multiplier['suffix']
percent = float('%.1f' % (100 * ratio))
percent = int(percent) if percent == int(percent) else percent
return '%s%s [%s%%]' % (baseText, suffix, percent)
def apply_regr(x, y, w, h, tx, ty, tw, th):
try:
cx = x + w/2.
cy = y + h/2.
cx1 = tx * w + cx
cy1 = ty * h + cy
w1 = math.exp(tw) * w
h1 = math.exp(th) * h
x1 = cx1 - w1/2.
y1 = cy1 - h1/2.
x1 = int(round(x1))
y1 = int(round(y1))
w1 = int(round(w1))
h1 = int(round(h1))
return x1, y1, w1, h1
except ValueError:
return x, y, w, h
except OverflowError:
return x, y, w, h
except Exception as e:
print(e)
return x, y, w, h
def prep_im_for_blob(im, pixel_means, target_size, max_size):
"""Mean subtract and scale an image for use in a blob."""
im = im.astype(np.float32, copy=False)
im -= pixel_means
im = im / 127.5
im_shape = im.shape
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])
im_scale = float(target_size) / float(im_size_min)
# Prevent the biggest axis from being more than MAX_SIZE
if np.round(im_scale * im_size_max) > max_size:
im_scale = float(max_size) / float(im_size_max)
im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
interpolation=cv2.INTER_LINEAR)
return im, im_scale
def _gene_embed_space(self,vec):
shape = vec.shape
vec = vec.flatten()
combo_neg_idx = np.array([1 if vec[i]<0 else 0 for i in range(len(vec))])
vec_pos = np.abs(vec)
int_part = np.floor(vec_pos)
frac_part = np.round(vec_pos - int_part,2)
bi_int_part=[] #?????????????signature???????
for i in range(len(int_part)):
bi=list(bin(int(int_part[i]))[2:])
bie = [0] * (16 - len(bi))
bie.extend(bi)
bi_int_part.append(np.array(bie,dtype=np.uint16))
bi_int_part = np.array(bi_int_part)
sig = []
for i in range(len(bi_int_part)):
sig.append(bi_int_part[i][10])
sig = np.array(sig).reshape(shape)
return np.array(bi_int_part),frac_part.reshape(shape),combo_neg_idx.reshape(shape),sig
def _gene_embed_space(self,vec):
shape = vec.shape
vec = vec.flatten()
combo_neg_idx = np.array([1 if vec[i]<0 else 0 for i in range(len(vec))])
vec_pos = np.abs(vec)
int_part = np.floor(vec_pos)
frac_part = np.round(vec_pos - int_part,2)
bi_int_part=[] #?????????????signature???????
for i in range(len(int_part)):
bi=list(bin(int(int_part[i]))[2:])
bie = [0] * (16 - len(bi))
bie.extend(bi)
bi_int_part.append(np.array(bie,dtype=np.uint16))
bi_int_part = np.array(bi_int_part)
sig = []
for i in range(len(bi_int_part)):
sig.append(bi_int_part[i][10])
sig = np.array(sig).reshape(shape)
return np.array(bi_int_part),frac_part.reshape(shape),combo_neg_idx.reshape(shape),sig
def crop_pad(image, corner, shape):
ndim = len(corner)
corner = [int(round(c)) for c in corner]
shape = [int(round(s)) for s in shape]
original = image.shape[-ndim:]
zipped = zip(corner, shape, original)
if np.any(c < 0 or c + s > o for (c, s, o) in zipped):
no_padding = [(0, 0)] * (image.ndim - ndim)
padding = [(max(-c, 0), max(c + s - o, 0)) for (c, s, o) in zipped]
corner = [c + max(-c, 0) for c in corner]
image_temp = np.pad(image, no_padding + padding, mode=str('constant'))
else:
image_temp = image
no_crop = [slice(o+1) for o in image.shape[:-ndim]]
crop = [slice(c, c+s) for (c, s) in zip(corner, shape)]
return image_temp[no_crop + crop]
def apply_regr(x, y, w, h, tx, ty, tw, th):
try:
cx = x + w/2.
cy = y + h/2.
cx1 = tx * w + cx
cy1 = ty * h + cy
w1 = math.exp(tw) * w
h1 = math.exp(th) * h
x1 = cx1 - w1/2.
y1 = cy1 - h1/2.
x1 = int(round(x1))
y1 = int(round(y1))
w1 = int(round(w1))
h1 = int(round(h1))
return x1, y1, w1, h1
except ValueError:
return x, y, w, h
except OverflowError:
return x, y, w, h
except Exception as e:
print(e)
return x, y, w, h
def compute_centroids(object_matrix, preserve_ids=False, round_val=False):
# if ids=true, then write a matrix equal to size of maximum
# value, else, order in object label order
# if round = true, round centroid coordinates to nearest integer
# when rounding, TODO: make sure we don't leave the volume
import skimage.measure as measure
centroids = []
# Threshold data
rp = measure.regionprops(object_matrix)
for r in rp:
if round_val > 0:
centroids.append(np.round(r.Centroid, round_val))
else:
centroids.append(r.Centroid)
return centroids
def pareto_front(vals1, vals2, round_val=3):
# butter and guns pareto front. Removes points not on
# the pareto frontier
# round very similar vals
vals1 = round(vals1, round_val)
vals2 = round(vals2, round_val)
v1_out = []
v2_out = []
idx_out = []
for idx in range(0, len(vals1)):
is_better = np.find(vals1 >= vals1[idx] and vals2 >= vals2[idx])
if is_better is None:
v1_out.append(vals1[idx])
v2_out.append(vals2[idx])
idx_out.append(idx)
return v1_out, v2_out, idx_out
def _downsample_mask(X, pct):
""" Create a boolean mask indicating which subset of X should be
evaluated.
"""
if pct < 1.0:
Mask = np.zeros(X.shape, dtype=np.bool)
m = X.shape[-2]
n = X.shape[-1]
nToEval = np.round(pct*m*n).astype(np.int32)
idx = sobol(2, nToEval ,0)
idx[0] = np.floor(m*idx[0])
idx[1] = np.floor(n*idx[1])
idx = idx.astype(np.int32)
Mask[:,:,idx[0], idx[1]] = True
else:
Mask = np.ones(X.shape, dtype=np.bool)
return Mask
def prepare_oae_PU4(known_transisitons):
print("Learn from pre + action label",
"*** INCOMPATIBLE MODEL! ***",
sep="\n")
N = known_transisitons.shape[1] // 2
y = generate_oae_action(known_transisitons)
ind = np.where(np.squeeze(combined(y[:,N:])) > 0.5)[0]
y = y[ind]
actions = oae.encode_action(known_transisitons, batch_size=1000).round()
positive = np.concatenate((known_transisitons[:,:N], np.squeeze(actions)), axis=1)
actions = oae.encode_action(y, batch_size=1000).round()
negative = np.concatenate((y[:,:N], np.squeeze(actions)), axis=1)
# random.shuffle(negative)
# negative = negative[:len(positive)]
# normalize
return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(positive, negative))
def prepare_oae_PU5(known_transisitons):
print("Learn from pre + suc + action label",
"*** INCOMPATIBLE MODEL! ***",
sep="\n")
N = known_transisitons.shape[1] // 2
y = generate_oae_action(known_transisitons)
ind = np.where(np.squeeze(combined(y[:,N:])) > 0.5)[0]
y = y[ind]
actions = oae.encode_action(known_transisitons, batch_size=1000).round()
positive = np.concatenate((known_transisitons, np.squeeze(actions)), axis=1)
actions = oae.encode_action(y, batch_size=1000).round()
negative = np.concatenate((y, np.squeeze(actions)), axis=1)
# random.shuffle(negative)
# negative = negative[:len(positive)]
# normalize
return (default_networks['PUDiscriminator'], *prepare_binary_classification_data(positive, negative))
def puzzle_plot(p):
p.setup()
def name(template):
return template.format(p.__name__)
from itertools import islice
configs = list(islice(p.generate_configs(9), 1000)) # be careful, islice is not immutable!!!
import numpy.random as random
random.shuffle(configs)
configs = configs[:10]
puzzles = p.generate(configs, 3, 3)
print(puzzles.shape, "mean", puzzles.mean(), "stdev", np.std(puzzles))
plot_image(puzzles[-1], name("{}.png"))
plot_image(np.clip(puzzles[-1]+np.random.normal(0,0.1,puzzles[-1].shape),0,1),name("{}+noise.png"))
plot_image(np.round(np.clip(puzzles[-1]+np.random.normal(0,0.1,puzzles[-1].shape),0,1)),name("{}+noise+round.png"))
plot_grid(puzzles, name("{}s.png"))
_transitions = p.transitions(3,3,configs=configs)
print(_transitions.shape)
transitions_for_show = \
np.einsum('ba...->ab...',_transitions) \
.reshape((-1,)+_transitions.shape[2:])
print(transitions_for_show.shape)
plot_grid(transitions_for_show, name("{}_transitions.png"))
def point_trans(ori_point, angle, ori_shape, new_shape):
""" Transfrom the point from original to rotated image.
Args:
ori_point: Point coordinates in original image.
angle: Rotate angle.
ori_shape: The shape of original image.
new_shape: The shape of rotated image.
Returns:
Numpy array of new point coordinates in rotated image.
"""
dx = ori_point[0] - ori_shape[1] / 2.0
dy = ori_point[1] - ori_shape[0] / 2.0
t_x = round(dx * math.cos(angle) - dy * math.sin(angle) + new_shape[1] / 2.0)
t_y = round(dx * math.sin(angle) + dy * math.cos(angle) + new_shape[0] / 2.0)
return np.array((int(t_x), int(t_y)))
def predict_tf_once(day,start_date = '2016-10-1'):
all_dataset = get_dataset(day)
all_dataset = map(lambda x:x.ix[start_date:start_date],all_dataset)
y_p_features = map(lambda user_id:tf_percent_model.resample_x_y_(all_dataset,user_id)[0].reshape(-1),get_full_user_ids())
y_p_features_df = pd.DataFrame(y_p_features,index = get_full_user_ids())
percent = pd.DataFrame.from_csv('./features/tensorflow_model/percent_model/%d.csv'%day)
#percent = pd.DataFrame.from_csv('./features/tensorflow_model/percent_model/%d.csv'%2)
#%%
percent = percent[map(lambda x:'percent#%d'%x,range(_feature_length))]
t = pd.DataFrame(index = percent.index)
t[pd.Timestamp(start_date)+pd.Timedelta('%dd'%(day-1))] = (np.array(y_p_features_df)*percent).sum(axis=1)
t = t.T
t.to_csv('./result/predict_part/%d.csv'%day)
real = int(np.round((np.array(y_p_features_df)*percent).sum().sum()))
print (day,real)
return (day,real)