def draw(path, srv):
filename = os.path.join(path, srv["preprocessed_filename"])
df = pd.read_csv(filename, sep="\t", index_col='time', parse_dates=True)
bins = defaultdict(list)
for i, col in enumerate(df.columns):
serie = df[col].dropna()
if pd.algos.is_monotonic_float64(serie.values, False)[0]:
serie = serie.diff()[1:]
p_value = adfuller(serie, autolag='AIC')[1]
if math.isnan(p_value): continue
nearest = 0.05 * round(p_value/0.05)
bins[nearest].append(serie)
for bin, members in bins.items():
series = [serie.name for serie in members]
if len(members) <= 10:
columns = series
else:
columns = random.sample(series, 10)
subset = df[columns]
name = "%s_adf_confidence_%.2f.png" % (srv["name"], bin)
print(name)
axes = subset.plot(subplots=True)
plt.savefig(os.path.join(path, name))
plt.close("all")
python类sample()的实例源码
def reorder_bpr_loss(re_x, his_x, dynamic_user, item_embedding, config):
'''
loss function for reorder prediction
re_x padded reorder baskets
his_x padded history bought items
'''
nll = 0
ub_seqs = []
for u, h, du in zip(re_x, his_x, dynamic_user):
du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item
nll_u = [] # nll for user
for t, basket_t in enumerate(u):
if basket_t[0] != 0:
pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t)
# Sample negative products
neg = [random.choice(h[t]) for _ in range(len(basket_t))] # replacement
# neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement
neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg)
# Score p(u, t, v > v')
score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx]
# Average Negative log likelihood for basket_t
nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score)))
nll += torch.mean(torch.cat(nll_u))
return nll
def test_uint_multi_port(self, x_series_device, seed):
# Reset the pseudorandom number generator with seed.
random.seed(seed)
do_ports = random.sample(
[d for d in x_series_device.do_ports if d.do_port_width <= 16], 2)
total_port_width = sum([d.do_port_width for d in do_ports])
with nidaqmx.Task() as task:
task.do_channels.add_do_chan(
flatten_channel_string([d.name for d in do_ports]),
line_grouping=LineGrouping.CHAN_FOR_ALL_LINES)
# Generate random values to test.
values_to_test = [int(random.getrandbits(total_port_width))
for _ in range(10)]
values_read = []
for value_to_test in values_to_test:
task.write(value_to_test)
time.sleep(0.001)
values_read.append(task.read())
assert values_read == values_to_test
def test_insufficient_numpy_write_data(self, x_series_device, seed):
# Reset the pseudorandom number generator with seed.
random.seed(seed)
# Randomly select physical channels to test.
number_of_channels = random.randint(
2, len(x_series_device.ao_physical_chans))
channels_to_test = random.sample(
x_series_device.ao_physical_chans, number_of_channels)
with nidaqmx.Task() as task:
task.ao_channels.add_ao_voltage_chan(
flatten_channel_string([c.name for c in channels_to_test]),
max_val=10, min_val=-10)
number_of_samples = random.randint(1, number_of_channels - 1)
values_to_test = numpy.float64([
random.uniform(-10, 10) for _ in range(number_of_samples)])
with pytest.raises(DaqError) as e:
task.write(values_to_test, auto_start=True)
assert e.value.error_code == -200524
def __init__(self, blanca_arriba = True):
"""TODO: to be defined1. """
# Creo una cara de cada color
self.caras = []
if blanca_arriba:
self.caras.append(Cara(COLOR_WHITE))
self.caras.append(Cara(COLOR_BLUE))
self.caras.append(Cara(COLOR_RED))
self.caras.append(Cara(COLOR_GREEN))
self.caras.append(Cara(COLOR_ORANGE))
self.caras.append(Cara(COLOR_YELLOW))
else:
caras = random.sample(range(6),6)
#print caras
for i in xrange(6):
self.caras.append(Cara(caras[i]))
self.configAdyacentes()
def test_bfill(self):
# test ndim=1
N = 100
s = pd.Series(np.random.randn(N))
mask = random.sample(range(N), 10)
s.iloc[mask] = np.nan
correct = s.bfill().values
test = bfill(s.values)
assert_almost_equal(correct, test)
# test ndim=2
df = pd.DataFrame(np.random.randn(N, N))
df.iloc[mask] = np.nan
correct = df.bfill().values
test = bfill(df.values)
assert_almost_equal(correct, test)
def test_ffill(self):
# test ndim=1
N = 100
s = pd.Series(np.random.randn(N))
mask = random.sample(range(N), 10)
s.iloc[mask] = np.nan
correct = s.ffill().values
test = ffill(s.values)
assert_almost_equal(correct, test)
# test ndim=2
df = pd.DataFrame(np.random.randn(N, N))
df.iloc[mask] = np.nan
correct = df.ffill().values
test = ffill(df.values)
assert_almost_equal(correct, test)
def loadLogoSet(path, rows,cols,test_data_rate=0.15):
random.seed(612)
_, imgID = readItems('data.txt')
y, _ = modelDict(path)
nPics = len(y)
faceassset = np.zeros((nPics,rows,cols), dtype = np.uint8) ### gray images
noImg = []
for i in range(nPics):
temp = cv2.imread(path +'logo/'+imgID[i]+'.jpg', 0)
if temp == None:
noImg.append(i)
elif temp.size < 1000:
noImg.append(i)
else:
temp = cv2.resize(temp,(cols, rows), interpolation = cv2.INTER_CUBIC)
faceassset[i,:,:] = temp
y = np.delete(y, noImg,0); faceassset = np.delete(faceassset, noImg, 0)
nPics = len(y)
index = random.sample(np.arange(nPics), int(nPics*test_data_rate))
x_test = faceassset[index,:,:]; x_train = np.delete(faceassset, index, 0)
y_test = y[index]; y_train = np.delete(y, index, 0)
return (x_train, y_train), (x_test, y_test)
def perturb(self):
'''
Peturb the paramter by choosing a random value between val_min and val_max.
Will choose a random number with precision specified by decimals. Will optionally
pick the min or the max value after a specified number of perturb calls
'''
if self.n == self.n_max - 1:
# Choose and extreme value
self.val = random.sample([self.val_min, self.val_max], 1)[0]
self.n = 0
else:
if self.decimals == 0:
self.val = random.randint(self.val_min,self.val_max)
else:
self.val = random.random() * (self.val_max - self.val_min + 10**-self.decimals) + (self.val_min - 0.5 * 10**-self.decimals)
self.val = round(self.val, ndigits=self.decimals)
if self.n_max > 0:
self.n += 1
def plot_labeled_images_random(image_list, label_list, categories, n, title_str, ypixels, xpixels, seed, filename):
random.seed(seed)
index_sample = random.sample(range(len(image_list)), n)
plt.figure(figsize=(2*n, 2))
#plt.suptitle(title_str)
for i, ind in enumerate(index_sample):
ax = plt.subplot(1, n, i + 1)
plt.imshow(image_list[ind].reshape(ypixels, xpixels))
plt.gray()
ax.set_title(categories[label_list[ind]], fontsize=20)
ax.get_xaxis().set_visible(False); ax.get_yaxis().set_visible(False)
if 1:
pylab.savefig(filename, bbox_inches='tight')
else:
plt.show()
# plot_unlabeled_images_random: plots unlabeled images at random
def plot_unlabeled_images_random(image_list, n, title_str, ypixels, xpixels, seed, filename):
random.seed(seed)
index_sample = random.sample(range(len(image_list)), n)
plt.figure(figsize=(2*n, 2))
plt.suptitle(title_str)
for i, ind in enumerate(index_sample):
ax = plt.subplot(1, n, i + 1)
plt.imshow(image_list[ind].reshape(ypixels, xpixels))
plt.gray()
ax.get_xaxis().set_visible(False); ax.get_yaxis().set_visible(False)
if 1:
pylab.savefig(filename, bbox_inches='tight')
else:
plt.show()
# plot_compare: given test images and their reconstruction, we plot them for visual comparison
def startReplay(warcFilename):
global p
pathOfWARC = os.path.join(os.path.dirname(moduleLocation) +
'/samples/warcs/' + warcFilename)
tempFilePath = '/tmp/' + ''.join(random.sample(
string.ascii_uppercase + string.digits * 6, 6)) + '.cdxj'
print('B2' + tempFilePath)
p = Process(target=replay.start, args=[tempFilePath])
p.start()
sleep(5)
cdxjList = indexer.indexFileAt(pathOfWARC, quiet=True)
cdxj = '\n'.join(cdxjList)
with open(tempFilePath, 'w') as f:
f.write(cdxj)
def generate_article_url(self, response):
as_id = ''.join(random.sample(string.ascii_letters + string.digits, 15))
cp_id = ''.join(random.sample(string.ascii_letters + string.digits, 15))
yield scrapy.Request(
"http://www.toutiao.com/api/pc/feed/?category=news_tech&utm_source=toutiao&widen=1&max_behot_time=0" +
"max_behot_time_tmp=" + str(int(time.time())) +
"tadrequire=true&as=" + as_id + "&cp=" + cp_id + "&t=" + str(time.time()),
callback=self.generate_article_url
)
article_list = json.loads(response.body)
if article_list.get("message") != "success":
return
for article_detail in article_list.get('data'):
# wenda gallery ad ?
# news_tech and news_finance
tag_url = article_detail.get('tag_url')
if article_detail.get('article_genre') == 'article'\
and (tag_url == 'news_tech' or tag_url == 'news_finance'):
yield scrapy.Request(
self.toutiao_url_pre + article_detail.get('source_url'),
callback=self.generate_article_content
)
def end_of_episode(self):
'''
Summary:
Performs miscellaneous end of episode tasks (#printing out useful information, saving stuff, etc.)
'''
# self.model = self.weak_learners
self.add_new_weak_learner()
self.most_recent_episode = []
if self.markov_window > 0:
# num_sampled_trees = int(math.ceil(len(self.weak_learners) / 10.0))
# self.model = random.sample(self.weak_learners, num_sampled_trees)
self.model = self.weak_learners[-self.markov_window:]
else:
self.model = self.weak_learners
Agent.end_of_episode(self)
def upload_file(upload_file_name, temp):
# upload_file_name?????
# ??? saveas???
# ?????????,??git???saveas
#key = md5(str(time.time())+''.join(random.sample(string.letters, 12))).hexdigest()
# key ??????
print u"??????: ",
pic_name = raw_input()
uuid_6 = uuid.uuid4().get_hex()[:8] #?????
key = pic_name+"_"+uuid_6+".png"
copyfile(upload_file_name,join(saveas,key))
mime_type = 'image/png'
token = q.upload_token(bucket, key)
ret, info = put_file(token, key, upload_file_name, mime_type=mime_type, check_crc=True)
print 'upload qiniu result:', info
assert ret['key'] == key
assert ret['hash'] == etag(upload_file_name)
os.rename(upload_file_name, upload_file_name+'.old')
return domain+'/'+key
def __init__(self, config):
super(CandidateDataLoader, self).__init__(config)
self.resize_to = config.size[0]
self.diameter_mm = config.diameter_mm
self.batch_size = config.batch
self.validation_rate =config.validation_ratio
self.k = config.top_k
# set data dir and file
self.data_dir = 'data/'
self.pkl_dir = self.data_dir + 'pkl/'
self.stage1_dir = self.data_dir + 'stage1/'
self.sample_dir = self.data_dir + 'sample/'
self.patch_dir = self.data_dir + 'patch/'
self.data_file_name = 'kaggle_stage1.pkl'
self.feature_file_name = 'features_detect_kaggle.pkl'
self.train_ids, self.valid_ids, self.test_ids = self.split_dataset()
# self.train_ids, self.valid_ids, self.test_ids = self.get_ids_from_sample_dataset()
self.current_ids = self.train_ids
self.current_pointer = 0
self.current_set = None
self.random_for_negative_samples = True
self.data = self.build_data_dict(layer_features=['67', '77'], k=self.k)
def reseed(self, netdb):
"""Compress netdb entries and set content"""
zip_file = io.BytesIO()
dat_files = []
for root, dirs, files in os.walk(netdb):
for f in files:
if f.endswith(".dat"):
# TODO check modified time
# may be not older than 10h
dat_files.append(os.path.join(root, f))
if len(dat_files) == 0:
raise PyseederException("Can't get enough netDb entries")
elif len(dat_files) > 75:
dat_files = random.sample(dat_files, 75)
with ZipFile(zip_file, "w", compression=ZIP_DEFLATED) as zf:
for f in dat_files:
zf.write(f, arcname=os.path.split(f)[1])
self.FILE_TYPE = 0x00
self.CONTENT_TYPE = 0x03
self.CONTENT = zip_file.getvalue()
self.CONTENT_LENGTH = len(self.CONTENT)
def genplot(x, y, fit, xdata=None, ydata=None, maxpts=10000):
bin_range = (0, 360)
a = (np.arange(*bin_range))
f_a = nuth_func(a, fit[0], fit[1], fit[2])
nuth_func_str = r'$y=%0.2f*cos(%0.2f-x)+%0.2f$' % tuple(fit)
if xdata.size > maxpts:
import random
idx = random.sample(list(range(xdata.size)), 10000)
else:
idx = np.arange(xdata.size)
f, ax = plt.subplots()
ax.set_xlabel('Aspect (deg)')
ax.set_ylabel('dh/tan(slope) (m)')
ax.plot(xdata[idx], ydata[idx], 'k.', label='Orig pixels')
ax.plot(x, y, 'ro', label='Bin median')
ax.axhline(color='k')
ax.plot(a, f_a, 'b', label=nuth_func_str)
ax.set_xlim(*bin_range)
pad = 0.2 * np.max([np.abs(y.min()), np.abs(y.max())])
ax.set_ylim(y.min() - pad, y.max() + pad)
ax.legend(prop={'size':8})
return f
#Function copied from from openPIV pyprocess
def attack():
ip = socket.gethostbyname( host )
global n
msg=str(string.letters+string.digits+string.punctuation)
data="".join(random.sample(msg,5))
dos = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
n+=1
dos.connect((ip, port))
dos.send( "GET /%s HTTP/1.1\r\n" % data )
print "\n "+time.ctime().split(" ")[3]+" "+"["+str(n)+"] #-#-# Hold Your Tears #-#-#"
except socket.error:
print "\n [ No connection! Server maybe down ] "
dos.close()
def _batch_gen(self):
from random import sample,randint
self.DB.get_split()
epochs = self.flags.epochs
fold = self.flags.fold
if fold>=0:
docs_ids = list(self.DB.split[fold][0])
else:
docs_ids = list(range(self.DB.data['training_text'].shape[0]))
B = min(self.flags.batch_size,len(docs_ids))
batches_per_epoch = len(docs_ids)//B
y = self.DB.y
#print(batches_per_epoch)
for epoch in range(epochs):
for batch in range(batches_per_epoch):
inputs = []
labels = [] # 0 or 1
for idx in sample(docs_ids,B):
inputs.append(idx+1)
labels.append(y[idx])
yield inputs, labels, epoch
def ex3(argv):
password = ''
for i in range(len(argv)):
for j in range(int(argv[i])):
if i == 0:
password += string.uppercase[random.randint(0,len(string.uppercase)-1)]
elif i == 1:
password += string.lowercase[random.randint(0,len(string.lowercase)-1)]
elif i == 2:
password += string.digits[random.randint(0,len(string.digits)-1)]
elif i == 3:
password += string.punctuation[random.randint(0,len(string.punctuation)-1)]
return ''.join(random.sample(password,len(password)))
def compress(string):
# Get the unique characters and numeric base.
unique = set(string)
base = len(unique)
# Create a key that will encode data properly.
key = random.sample(unique, base)
mapping = dict(map(reversed, enumerate(key)))
while not mapping[string[-1]]:
key = random.sample(unique, base)
mapping = dict(map(reversed, enumerate(key)))
# Create a compressed numeric representation.
value = 0
for place, char in enumerate(string):
value += mapping[char] * base ** place
# Return the number as a string with the table.
return decode(value), bytes(key)
def randomly_grouped_by(key_from_example: Callable[[LabeledExample], Any], training_share: float = .9) -> Callable[
[List[LabeledExample]], Tuple[List[LabeledExample], List[LabeledExample]]]:
def split(examples: List[LabeledExample]) -> Tuple[List[LabeledExample], List[LabeledExample]]:
examples_by_directory = group(examples, key=key_from_example)
directories = examples_by_directory.keys()
# split must be the same every time:
random.seed(42)
keys = set(random.sample(directories, int(training_share * len(directories))))
training_examples = [example for example in examples if key_from_example(example) in keys]
test_examples = [example for example in examples if key_from_example(example) not in keys]
return training_examples, test_examples
return split
def handler(event, context):
num_of_players = int(os.environ['NUMBER_OF_PLAYERS'])
num_of_mafia = int(os.environ['NUMBER_OF_MAFIA'])
clear_all()
names = random.sample(all_names, num_of_players)
mafia = random.sample(range(0, num_of_players), num_of_mafia)
for i in range(num_of_players):
player = {
'Name': names[i],
'TrueIdentity': 'Mafia' if i in mafia else 'Innocent',
'Identity': 'Uncovered'
}
save(player)
message = "New game started with {}".format(', '.join(names))
return response( {"message": message}, event )
# deleate all records in the table
def makeQ(self):
shapes = ["Square", "Rectangle", "Triangle", "Octagon", "Pentagon", ]
shape = random.sample(shapes, 1)[0]
q1 = ("How many sides does a {0} have?").format(shape)
options = [3,4,8,5]
if shape == "Square" or shape =="Rectangle":
a1 = 4
elif shape == "Triangle":
a1 = 3
elif shape == "Octagon":
a1=8
elif shape =="Pentagon":
a1=5
print("Choose the correct answer: {0}").format(options)
return q1, a1, options
def gen_pseudo_word(self, L=None):
if not L:
L = random.randint(1, 8)
# generate one word that we hadn't used before
while True:
if self.readable:
# alternating between vowels and consonants, sampled with repl.
_choice, _range = random.choice, range(int(math.ceil(L / 2)))
v = [_choice(self.V) for i in _range]
c = [_choice(self.C) for i in _range]
zipped = zip(v, c) if random.getrandbits(1) else zip(c, v)
pseudo_word = ''.join([a for b in zipped for a in b])[:L]
else:
pseudo_word = ''.join(random.sample(
string.ascii_lowercase, L))
if pseudo_word not in self.inv_word_mapping:
return pseudo_word
def get_task_generator(self):
content = load_dictionary(DICTIONARY_FILE)
vocabulary = content[:200]
mapping = dict(zip(random.sample(vocabulary, self.MAPPING_SIZE),
random.sample(vocabulary, self.MAPPING_SIZE)))
keys = list(mapping.keys())
self.mapping_check = {key: False for key in keys}
def micro6_question(self):
def micro6_feedback(is_correct, question):
reaction = "good job" if is_correct else "wrong"
if not is_correct:
return reaction + '! ' + sentence
else:
return reaction + '! '
self.key_idx = random.randint(0, len(keys) - 1)
word1 = keys[self.key_idx]
word2 = mapping[word1]
question = 'random_map: ' + word1 + '.'
sentence = word2 + '.'
return question, [sentence], micro6_feedback
return TaskGenerator(micro6_question, '', None, ';')
def on_start(self, event):
# choose some random object
self.target_obj, = random.sample(objects, 1)
# find the cell in front of the learner
ws = self.get_world().state
ld = self.get_world().valid_directions[ws.learner_direction]
lp = ws.learner_pos
self.state.initial_count = ws.learner_inventory[self.target_obj]
# place the object there
self.get_world().put_entity(lp + ld, self.target_obj, True, True)
self.add_handler(
on_state_changed(lambda ws, ts:
ws.learner_inventory[self.target_obj] ==
ts.initial_count + 1)
(self.on_object_picked_up))
self.set_message("You have {indef_object} in front of you. "
"Pick up the {object}.".format(
indef_object=msg.indef_article(self.target_obj),
object=self.target_obj))
def on_start(self, event):
# choose a random object
self.target_obj, = random.sample(objects, 1)
# find a random cell around the learner
self.direction = random.choice(list(self.get_world().valid_directions
.keys()))
ws = self.get_world().state
p = ws.learner_pos + self.get_world().valid_directions[self.direction]
self.state.initial_count = ws.learner_inventory[self.target_obj]
# place the object there
self.get_world().put_entity(p, self.target_obj, True, True)
self.add_handler(
on_state_changed(lambda ws, ts:
ws.learner_inventory[self.target_obj] ==
ts.initial_count + 1)
(self.on_object_picked_up))
self.set_message("There is {indef_object} {direction} from you, "
"pick up the {object}.".format(
indef_object=msg.indef_article(self.target_obj),
direction=self.direction,
object=self.target_obj))
def on_start(self, event):
# choose a random object
self.target_obj, = random.sample(objects, 1)
ws = self.get_world().state
# select a random number of steps
self.n = random.randint(1, PickUpInFrontTask.max_steps_forward)
# place the object that number of steps in front of the learner
p = ws.learner_pos + self.n * self.get_world().valid_directions[
ws.learner_direction]
self.state.initial_count = ws.learner_inventory[self.target_obj]
self.get_world().put_entity(p, self.target_obj, True, True)
self.add_handler(
on_state_changed(lambda ws, ts:
ws.learner_inventory[self.target_obj] ==
ts.initial_count + 1)
(self.on_object_picked_up))
self.set_message("There is {indef_object} {n} steps forward, "
"pick up the {object}."
.format(
indef_object=msg.indef_article(self.target_obj),
n=msg.number_to_string(self.n),
object=self.target_obj))