本文整理汇总了Python中enumerate函数的典型用法代码示例。如果您正苦于以下问题:Python enumerate函数的具体用法?Python enumerate怎么用?Python enumerate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enumerate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculate_zernikes
def calculate_zernikes(self, workspace):
zernike_indexes = cpmz.get_zernike_indexes(self.zernike_degree.value + 1)
meas = workspace.measurements
for o in self.objects:
object_name = o.object_name.value
objects = workspace.object_set.get_objects(object_name)
#
# First, get a table of centers and radii of minimum enclosing
# circles per object
#
ij = np.zeros((objects.count + 1, 2))
r = np.zeros(objects.count + 1)
for labels, indexes in objects.get_labels():
ij_, r_ = minimum_enclosing_circle(labels, indexes)
ij[indexes] = ij_
r[indexes] = r_
#
# Then compute x and y, the position of each labeled pixel
# within a unit circle around the object
#
ijv = objects.ijv
l = ijv[:, 2]
yx = (ijv[:, :2] - ij[l, :]) / r[l, np.newaxis]
z = cpmz.construct_zernike_polynomials(
yx[:, 1], yx[:, 0], zernike_indexes)
for image_group in self.images:
image_name = image_group.image_name.value
image = workspace.image_set.get_image(
image_name, must_be_grayscale=True)
pixels = image.pixel_data
mask = (ijv[:, 0] < pixels.shape[0]) & \
(ijv[:, 1] < pixels.shape[1])
mask[mask] = image.mask[ijv[mask, 0], ijv[mask, 1]]
yx_ = yx[mask, :]
l_ = l[mask]
z_ = z[mask, :]
if len(l_) == 0:
for i, (n, m) in enumerate(zernike_indexes):
ftr = self.get_zernike_magnitude_name(image_name, n, m)
meas[object_name, ftr] = np.zeros(0)
if self.wants_zernikes == Z_MAGNITUDES_AND_PHASE:
ftr = self.get_zernike_phase_name(image_name, n, m)
meas[object_name, ftr] = np.zeros(0)
continue
areas = scind.sum(
np.ones(l_.shape, int), labels=l_, index=objects.indices)
for i, (n, m) in enumerate(zernike_indexes):
vr = scind.sum(
pixels[ijv[mask, 0], ijv[mask, 1]] * z_[:, i].real,
labels=l_, index=objects.indices)
vi = scind.sum(
pixels[ijv[mask, 0], ijv[mask, 1]] * z_[:, i].imag,
labels=l_, index=objects.indices)
magnitude = np.sqrt(vr * vr + vi * vi) / areas
ftr = self.get_zernike_magnitude_name(image_name, n, m)
meas[object_name, ftr] = magnitude
if self.wants_zernikes == Z_MAGNITUDES_AND_PHASE:
phase = np.arctan2(vr, vi)
ftr = self.get_zernike_phase_name(image_name, n, m)
meas[object_name, ftr] = phase
示例2: _distance_to_W
def _distance_to_W(self, ids=None):
allneighbors = {}
weights = {}
if ids:
ids = np.array(ids)
else:
ids = np.arange(len(self._nmat))
if self.binary:
for i, neighbors in enumerate(self._nmat):
ns = [ni for ni in neighbors if ni != i]
neigh = list(ids[ns])
if len(neigh) == 0:
allneighbors[ids[i]] = []
weights[ids[i]] = []
else:
allneighbors[ids[i]] = neigh
weights[ids[i]] = [1] * len(ns)
else:
self.dmat = self.kd.sparse_distance_matrix(
self.kd, max_distance=self.threshold)
for i, neighbors in enumerate(self._nmat):
ns = [ni for ni in neighbors if ni != i]
neigh = list(ids[ns])
if len(neigh) == 0:
allneighbors[ids[i]] = []
weights[ids[i]] = []
else:
try:
allneighbors[ids[i]] = neigh
weights[ids[i]] = [self.dmat[(
i, j)] ** self.alpha for j in ns]
except ZeroDivisionError:
raise Exception, "Cannot compute inverse distance for elements at same location (distance=0)."
return allneighbors, weights
示例3: load_text
def load_text(self):
'''
The text of instances are not stored in the prediction result file,
so you need to call this function to load texts from testing data.
>>> from libshorttext.analyzer import *
>>> insts = InstanceSet('prediction_result_path')
>>> insts.load_text()
This method also load the extra svm features if extra svm files
are used when training.
'''
EMPTY_MESSAGE = '**None**'
sorted_insts = sorted(self.insts, key = lambda inst: inst.idx)
i = 0
for idx, lines in enumerate(izip(*([open(self.filepath, 'r')] + [open(f, 'r') for f in self.extra_svm_files]))):
line = lines[0]
extra_svm_feats = lines[1:]
nr_extra_svm_feats = len(extra_svm_feats)
if idx > sorted_insts[-1].idx:
break
if idx == sorted_insts[i].idx:
try:
sorted_insts[i].text = line.split('\t',1)[1].strip()
except:
sorted_insts[i].text = EMPTY_MESSAGE
sorted_insts[i].extra_svm_feats = [None] * nr_extra_svm_feats
for j, extra_svm_feat in enumerate(extra_svm_feats):
try:
sorted_insts[i].extra_svm_feats[j] = dict(map(lambda t: (int(t[0]), float(t[1])), [feat.split(':') for feat in extra_svm_feat.split(None, 1)[1].split()]))
except:
sorted_insts[i].extra_svm_feats[j] = EMPTY_MESSAGE
i += 1
示例4: recompute_unread
def recompute_unread(min_date = None):
from r2.models import Inbox, Account, Comment, Message
from r2.lib.db import queries
def load_accounts(inbox_rel):
accounts = set()
q = inbox_rel._query(eager_load = False, data = False,
sort = desc("_date"))
if min_date:
q._filter(inbox_rel.c._date > min_date)
for i in fetch_things2(q):
accounts.add(i._thing1_id)
return accounts
accounts_m = load_accounts(Inbox.rel(Account, Message))
for i, a in enumerate(accounts_m):
a = Account._byID(a)
print "%s / %s : %s" % (i, len(accounts_m), a)
queries.get_unread_messages(a).update()
queries.get_unread_comments(a).update()
queries.get_unread_selfreply(a).update()
accounts = load_accounts(Inbox.rel(Account, Comment)) - accounts_m
for i, a in enumerate(accounts):
a = Account._byID(a)
print "%s / %s : %s" % (i, len(accounts), a)
queries.get_unread_comments(a).update()
queries.get_unread_selfreply(a).update()
示例5: knapsack_unbounded_dp
def knapsack_unbounded_dp(items, C):
# order by max value per item size
items = sorted(items, key=lambda item: item[VALUE]/float(item[SIZE]), reverse=True)
# Sack keeps track of max value so far as well as the count of each item in the sack
print('!')
sack = [(0, [0 for i in items]) for i in range(0, C+1)] # value, [item counts]
print('!')
for i,item in enumerate(items):
name, size, value = item
for c in range(size, C+1):
print(sack)
sackwithout = sack[c-size] # previous max sack to try adding this item to
trial = sackwithout[0] + value
used = sackwithout[1][i]
if sack[c][0] < trial:
# old max sack with this added item is better
sack[c] = (trial, sackwithout[1][:])
sack[c][1][i] +=1 # use one more
value, bagged = sack[C]
numbagged = sum(bagged)
size = sum(items[i][1]*n for i,n in enumerate(bagged))
# convert to (iten, count) pairs) in name order
bagged = sorted((items[i][NAME], n) for i,n in enumerate(bagged) if n)
return value, size, numbagged, bagged
示例6: rx_oversampled
def rx_oversampled(frames, ref_frame, modulated_frame, x_preamble, data, rx_kernel, demapper, timeslots, fft_len, cp_len, cs_len):
ref_frame_os = signal.resample(ref_frame, 2 * len(ref_frame))
x_preamble_os = signal.resample(x_preamble, 2 * len(x_preamble))
nyquist_frame_len = cp_len + 2 * fft_len + cs_len + cp_len + timeslots * fft_len + cs_len
n_frames = np.shape(frames)[0]
sync_frames = np.zeros((n_frames, nyquist_frame_len), dtype=np.complex)
print('nyquist sampled frame len', nyquist_frame_len, 'with n_frames', n_frames)
f_start = cp_len + 2 * fft_len + cs_len
d_start = f_start + cp_len
print('data start: ', d_start)
for i, f in enumerate(frames[0:2]):
tf = np.roll(f, 1)
tf[0] = 0
ff = signal.resample(tf, len(f) // 2)
sframe = synchronize_time(ff, ref_frame_os, x_preamble_os, 2 * fft_len, 2 * cp_len)
sframe = signal.resample(sframe, len(sframe) // 2)
sframe = synchronize_freq_offsets(sframe, modulated_frame, x_preamble, fft_len, cp_len, samp_rate=3.125e6)
print(len(sframe), len(ref_frame))
rx_preamble = sframe[cp_len:cp_len + 2 * fft_len]
avg_phase = calculate_avg_phase(rx_preamble, x_preamble)
# m, c = calculate_avg_phase(rx_preamble, x_preamble)
# avg_phase = calculate_avg_phase(sframe, ref_frame)
# phase_eqs = m * np.arange(-cp_len, len(sframe) - cp_len) + c
# sframe *= np.exp(-1j * phase_eqs)
# sframe *= np.exp(-1j * avg_phase)
sync_frames[i] = sframe
rx_data_frame = sframe[d_start:d_start + fft_len * timeslots]
# # rx_data_frame *= np.exp(-1j * avg_phase)
#
demodulate_frame(rx_data_frame, modulated_frame, rx_kernel, demapper, data, timeslots, fft_len)
for i, f in enumerate(sync_frames[0:3]):
rx_data_frame = f[d_start:d_start + fft_len * timeslots]
demodulate_frame(rx_data_frame, modulated_frame, rx_kernel, demapper, data, timeslots, fft_len)
示例7: links
def links(self, data_matrix):
data_size = data_matrix.shape[0]
kernel_matrix = pairwise_kernels(data_matrix, metric=self.metric, **self.kwds)
# compute instance density as average pairwise similarity
density = np.sum(kernel_matrix, 0) / data_size
# compute list of nearest neighbors
kernel_matrix_sorted = np.argsort(-kernel_matrix)
# make matrix of densities ordered by nearest neighbor
density_matrix = density[kernel_matrix_sorted]
# if a denser neighbor cannot be found then assign link to the instance itself
link_ids = list(range(density_matrix.shape[0]))
# for all instances determine link link
for i, row in enumerate(density_matrix):
i_density = row[0]
# for all neighbors from the closest to the furthest
for jj, d in enumerate(row):
# proceed until n_nearest_neighbors have been explored
if self.n_nearest_neighbors is not None and jj > self.n_nearest_neighbors:
break
j = kernel_matrix_sorted[i, jj]
if jj > 0:
j_density = d
# if the density of the neighbor is higher than the density of the instance assign link
if j_density > i_density:
link_ids[i] = j
break
return link_ids
示例8: WorkBook_writeSheet
def WorkBook_writeSheet(self, filename):
columns = ['Date', 'Month', 'ID #', 'Contact ID #', \
'Talked to Person X?', 'Closeness/Trust with X', \
'Connecting ID', 'Connector ID']
# Writes to csv file
with open(filename, 'w') as f:
writer = csv.writer(f)
writer.writerow(columns)
for row in self.sheet:
date = row[DATE_COLUMN]
month = row[MONTH_COLUMN]
connecting = row[CONNECTING_COLUMN]
connector = row[CONNECTOR_COLUMN]
talkVal = row[TALKED_WEIGHT]
closeVal = row[CLOSENESS_WEIGHT]
connectingID = row[CONNECTING_ID_COLUMN]
connectorID = row[CONNECTOR_ID_COLUMN]
row = [date, month, connecting, connector, talkVal, \
closeVal, connectingID, connectorID]
writer.writerow(row)
# Converts from the written csv file to xlsx
for csvfile in glob.glob(os.path.join('.', '*.csv')):
workbook = Workbook(csvfile[0:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'rb') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
sys.exit()
示例9: plots_1d
def plots_1d(self, roots, params=None, legend_labels=None, legend_ncol=None, nx=None,
paramList=None, roots_per_param=False, share_y=None, markers=None, xlims=None):
if roots_per_param:
params = [self.check_param(roots[i][0], param) for i, param in enumerate(params)]
else: params = self.get_param_array(roots[0], params)
if paramList is not None:
wantedParams = self.paramNameListFromFile(paramList)
params = [param for param in params if param.name in wantedParams]
nparam = len(params)
if share_y is None: share_y = self.settings.prob_label is not None and nparam > 1
plot_col, plot_row = self.make_figure(nparam, nx=nx)
plot_roots = roots
for i, param in enumerate(params):
subplot(plot_row, plot_col, i + 1)
if roots_per_param: plot_roots = roots[i]
if markers is not None and i < len(markers): marker = markers[i]
else: marker = None
# self.plot_1d(plot_roots, param, no_ylabel=share_y and i % self.plot_col > 0, marker=marker, prune=(None, 'both')[share_y])
self.plot_1d(plot_roots, param, no_ylabel=share_y and i % self.plot_col > 0, marker=marker)
if xlims is not None: xlim(xlims[i][0], xlims[i][1])
if share_y: self.spaceTicks(gca().xaxis, expand=True)
self.finish_plot([legend_labels, roots][legend_labels is None], legend_ncol=legend_ncol)
if share_y: subplots_adjust(wspace=0)
return plot_col, plot_row
示例10: categorize
def categorize(data, colnum, missingvals, ranges=[]):
categories = set()
for row in data:
if row[colnum] not in missingvals:
categories.add(row[colnum])
catlist = list(categories)
catlist.sort()
# print(', '.join(['%i: %s' % (n, catlist[n]) for n in xrange(len(catlist))]), "(with missing vals:", missingvals, ")")
missing_indices = []
for index, row in enumerate(data):
if row[colnum] in missingvals: # missing data
row[colnum] = 0
missing_indices.append(index)
else: # this row doesn't have missing data.
if len(ranges) > 0: # find val in ranges and use that index.
found = False
for i, r in enumerate(ranges):
if isinstance(r, basestring): # compare strings.
if r in row[colnum]:
row[colnum] = i
found = True
break
elif isinstance(r, ( int, long )) and not re.search('[a-zA-Z]', row[colnum]):
# ref : http://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not
if float(row[colnum]) >= r and len(ranges) > i+1 and isinstance(ranges[i+1], ( int, long )) and float(row[colnum]) < ranges[i+1]:
row[colnum] = i
found = True
break
if not found:
print(row[colnum]) # error here
else: # no ranges given, so just set category of appearance.
row[colnum] = catlist.index(row[colnum])+1
return missing_indices
示例11: validate_label_generation
def validate_label_generation():
mals1_df = pd.read_csv('data/sorted-train-labels-vs251-252.csv')
mals2_df = pd.read_csv('data/sorted-train-labels-vs263-264-apt.csv')
counter = 0
m1_x = np.array(mals1_df['malware_type_x'])
m1_f = np.array(mals1_df['family_name'])
m1_sl = np.array(mals1_df['sample_label'])
m1_fl = np.array(mals1_df['family_label'])
m2_x = np.array(mals2_df['malware_type_x'])
m21_f = np.array(mals2_df['family_name'])
m2_sl = np.array(mals2_df['sample_label'])
m2_fl = np.array(mals2_df['family_label'])
for idx1, mname1 in enumerate(m1_x):
for idx2, mname2 in enumerate(m2_x):
if mname1 == mname2:
if m1_sl[idx1] != m2_sl[idx2]:
print("Sample label incongruence: {:d} {:d}".format(m1_sl[idx1], m2_sl[idx2]))
counter += 1
if (m1_fl[idx1] != m2_fl[idx2]):
print("Family label incongruence: {:d} {:d}".format(m1_fl[idx1], m2_fl[idx2]))
counter += 1
if (idx1 % 1000) == 0:
print("Processed {:d} malware names.".format(idx1))
print("Total Incongruence Errors: {:d}".format(counter))
return
示例12: testPeriodsMonths
def testPeriodsMonths(self):
""" Test iteration over periods (months) """
dt = datetime.datetime
ef = S3TimePlotEventFrame(dt(2011, 1, 5),
dt(2011, 4, 28),
slots="months")
expected = [(dt(2011, 1, 5), dt(2011, 2, 5)),
(dt(2011, 2, 5), dt(2011, 3, 5)),
(dt(2011, 3, 5), dt(2011, 4, 5)),
(dt(2011, 4, 5), dt(2011, 4, 28))]
for i, period in enumerate(ef):
self.assertEqual(period.start, expected[i][0])
self.assertEqual(period.end, expected[i][1])
ef = S3TimePlotEventFrame(dt(2011, 1, 5),
dt(2011, 8, 16),
slots="3 months")
expected = [(dt(2011, 1, 5), dt(2011, 4, 5)),
(dt(2011, 4, 5), dt(2011, 7, 5)),
(dt(2011, 7, 5), dt(2011, 8, 16))]
for i, period in enumerate(ef):
self.assertEqual(period.start, expected[i][0])
self.assertEqual(period.end, expected[i][1])
示例13: testPeriodsWeeks
def testPeriodsWeeks(self):
""" Test iteration over periods (weeks) """
dt = datetime.datetime
ef = S3TimePlotEventFrame(dt(2011, 1, 5),
dt(2011, 1, 28),
slots="weeks")
expected = [(dt(2011, 1, 5), dt(2011, 1, 12)),
(dt(2011, 1, 12), dt(2011, 1, 19)),
(dt(2011, 1, 19), dt(2011, 1, 26)),
(dt(2011, 1, 26), dt(2011, 1, 28))]
for i, period in enumerate(ef):
self.assertEqual(period.start, expected[i][0])
self.assertEqual(period.end, expected[i][1])
ef = S3TimePlotEventFrame(dt(2011, 1, 5),
dt(2011, 2, 16),
slots="2 weeks")
expected = [(dt(2011, 1, 5), dt(2011, 1, 19)),
(dt(2011, 1, 19), dt(2011, 2, 2)),
(dt(2011, 2, 2), dt(2011, 2, 16))]
for i, period in enumerate(ef):
self.assertEqual(period.start, expected[i][0])
self.assertEqual(period.end, expected[i][1])
示例14: lcs_dy_prog
def lcs_dy_prog(s1, s2):
table = np.zeros((len(s1), len(s2)), dtype=np.int)
def lookup(i, j):
if i < 0 or j < 0:
return 0
else:
return table[i, j]
# find length of the lcs
for i, c1 in enumerate(s1):
for j, c2 in enumerate(s2):
if c1 == c2:
table[i, j] = lookup(i - 1, j - 1) + 1
else:
table[i, j] = max(lookup(i - 1, j), lookup(i, j - 1))
# backtrac to find lcs (not unique)
i = len(s1) - 1
j = len(s2) - 1
res = ""
while i >= 0 and j >= 0:
if s1[i] == s2[j]:
res += s1[i]
i -= 1
j -= 1
else:
if lookup(i - 1,j) > lookup(i, j - 1):
i -= 1
else:
j -= 1
res = res[::-1]
return res
示例15: _make_scalar_compound_controller
def _make_scalar_compound_controller(self, fcurves, keyframes, bez_chans, default_xform):
ctrl = plCompoundController()
subctrls = ("X", "Y", "Z")
for i in subctrls:
setattr(ctrl, i, plLeafController())
exported_frames = ([], [], [])
ctrl_fcurves = { i.array_index: i for i in fcurves }
for keyframe in keyframes:
for i, subctrl in enumerate(subctrls):
fval = keyframe.values.get(i, None)
if fval is not None:
keyframe_type = hsKeyFrame.kBezScalarKeyFrame if i in bez_chans else hsKeyFrame.kScalarKeyFrame
exported = hsScalarKey()
exported.frame = keyframe.frame_num
exported.frameTime = keyframe.frame_time
exported.inTan = keyframe.in_tans[i]
exported.outTan = keyframe.out_tans[i]
exported.type = keyframe_type
exported.value = fval
exported_frames[i].append(exported)
for i, subctrl in enumerate(subctrls):
my_keyframes = exported_frames[i]
# ensure this controller has at least ONE keyframe
if not my_keyframes:
hack_frame = hsScalarKey()
hack_frame.frame = 0
hack_frame.frameTime = 0.0
hack_frame.type = hsKeyFrame.kScalarKeyFrame
hack_frame.value = default_xform[i]
my_keyframes.append(hack_frame)
getattr(ctrl, subctrl).keys = (my_keyframes, my_keyframes[0].type)
return ctrl