def avg_ent(filepath, nrange, shuffle_mode=False, min_count=1, backwards=False, ExpectedProbability=False): ''' For each n (Markov order) in the parameter nrange, averages entropy across all n-grams, estimating the entropy rate of the songs in filepath. ''' if shuffle_mode: shuffle.shuffle(filepath) filepath = './output/shuffle.csv' ndct = p_to_ent(filepath, nrange, backwards = backwards, ExpectedProbability = ExpectedProbability) # print(ndct) result = {} for key, value in ndct.items(): n = key ls = [] for jey, ualue in value.items(): if ualue[1]>min_count: for i in range(ualue[1]): ls.append(ualue[0]) if len(ls)>0: result[key] = sum(ls) / len(ls) '''else: result[key]='-''' with open("./output/entropy.csv", 'w') as output_file: writer = csv.writer(output_file) for key, value in result.items(): row = [] row.append(key) row.append(value) writer.writerow(row) return result
def tennis(filename, count, dicts, seed): fh = open(filename, "r") data = [] labels = [] fl = fh.readlines() for line in fl: vals = line.split('\n')[0].rsplit(',') if len(vals) != 5: break element = [] for i in range(4): v = 0 keys = dicts[i].keys() keys.sort() for k in keys: if vals[i] == k: element.append(v) v += 1 data.append(element) labels.append(vals[4]) shuffle.shuffle(data, labels, seed) return numpy.array(data, numpy.int16), labels
def test_type_error_on_py3k(): # type: ignore msg = "abcdefghi" * 1000 if sys.version_info.major > 2: with raises(TypeError): shuffle.shuffle(msg) else: assert shuffle.unshuffle(shuffle.shuffle(msg)) == msg
def build_problem(ES, q0, Lambda, k, phi, alpha, N, order): dim_l = find_dim(N) A = np.zeros((dim_l, dim_l)) b = np.zeros(dim_l) c = 0. keys = get_words(2, order) alphabet = (0, 1) words = itertools.chain( *[itertools.product(alphabet, repeat=i) for i in range(N + 1)]) for w in tqdm(words, total=find_dim(N)): w_idx = keys.index(w) words2 = itertools.chain( *[itertools.product(alphabet, repeat=i) for i in range(N + 1)]) for v in words2: v_idx = keys.index(v) w_shuffle_v = shuffle(w, v) w_shuffle_v_1 = [tuple(list(tau) + [0]) for tau in w_shuffle_v] ES_w_shuffle_v_1 = sum(ES[tau] for tau in w_shuffle_v_1) w1_shuffle_v = shuffle(tuple(list(w) + [0]), v) ES_w1_shuffle_v = sum(ES[tau] for tau in w1_shuffle_v) w1_shuffle_v1 = shuffle(tuple(list(w) + [0]), tuple(list(v) + [0])) ES_w1_shuffle_v1 = sum(ES[tau] for tau in w1_shuffle_v1) w1_shuffle_v1_1 = [tuple(list(tau) + [0]) for tau in w1_shuffle_v1] ES_w1_shuffle_v1_1 = sum(ES[tau] for tau in w1_shuffle_v1_1) A[w_idx, v_idx] = -Lambda * ES_w_shuffle_v_1 + ( k - alpha) * ES_w1_shuffle_v1 - (phi + k) * ES_w1_shuffle_v1_1 ES_w = ES[w] w_shuffle_2 = shuffle(w, (1, )) w_shuffle_21 = [tuple(list(tau) + [0]) for tau in w_shuffle_2] ES_w_shuffle_21 = sum(ES[tau] for tau in w_shuffle_21) w1_shuffle_2 = shuffle(tuple(list(w) + [0]), (1, )) ES_w1_shuffle_2 = sum(ES[tau] for tau in w1_shuffle_2) w1 = tuple(list(w) + [0]) ES_w1 = ES[w1] w11 = tuple(list(w1) + [0]) ES_w11 = ES[w11] b[w_idx] = ES_w_shuffle_21 - ES_w1_shuffle_2 + ( 2 * alpha * q0 - q0 * k) * ES_w1 + 2 * phi * ES_w11 c = q0 * (ES[(1, )] + 1.) - alpha * q0 - q0 * phi * ES[(1, )] return A, b, c
def read_from_archves(comm, path, cnt_classes, size, crop): lst_tarfiles = [] lst_tarfiles_local = [] max_loaders = 500 # ranks_with_files = comm.mpi_comm.allgather(comm.rank if ((comm.intra_rank == 0) and os.path.isdir(path)) else -1) ranks_with_files = comm.mpi_comm.allgather( comm.rank if os.path.isdir(path) else -1) ranks_with_files = [i for i in ranks_with_files if i >= 0] ranks_with_files.sort() # ranks_with_files = ranks_with_files[:max_loaders] if comm.rank == ranks_with_files[0]: print("loading tarfiles from", path) lst_tarfiles = list_dir_absolute(path) lst_tarfiles = [ i for i in lst_tarfiles if int(i.split("_")[-2]) < cnt_classes ] print( f"loaded {len(lst_tarfiles)} filenames, using {len(ranks_with_files)} readers" ) comm.mpi_comm.Barrier() count_me_in = comm.rank in ranks_with_files shuffle(lst_tarfiles, lst_tarfiles_local, comm.mpi_comm, pad=False, count_me_in=count_me_in) #if comm.rank < 6: if comm.rank == 0: print( f"---------------shuffled tarfiles to read, rank {comm.rank} got", len(lst_tarfiles_local)) print("loading tars from storage") time_start = timer() if len(lst_tarfiles_local) > 0: data = read_from_list_of_tars(lst_tarfiles_local, size, crop) else: data = [] comm.mpi_comm.Barrier() time_end = timer() if comm.rank == 0: print(f"done loading tars in {time_end - time_start}, shuffling") time_start = timer() # print(f"{comm.rank}: {lst_tarfiles_local}, {len(data)}") data_local = [] shuffle(data, data_local, comm.mpi_comm, pad=True) comm.mpi_comm.Barrier() time_end = timer() if comm.rank == 0: print("done shuffling in", time_end - time_start) # for i in range(len(data_local)): # if data_local[i][0].shape != (3, 224, 224): # raise RuntimeError("wrong image loaded :", data_local[i][0].shape) return data_local
def main(): if len(sys.argv) != 2: parsing.error(0, "0") inst = parsing.parsing(sys.argv[1]) print_rubik.print_rubik(rubik) shuffle.shuffle(rubik, inst) if utils.check_rubik(rubik) == False: print("the cube is not solved") else: print("the cube is solved")
def test_shuffle_content(self): array = [ 1, 2, 3, 4, 5, 6, 12, 23, 1412, 1, 32, 23, 5, 6, 6, 234, 23, 456, 678 ] array_cp = copy.copy(array) shuffle(array) for i in array_cp: self.assertIn(i, array) for j in array: self.assertIn(j, array_cp)
def _build_row(w, basis, E): Ai = [] for v in basis: z = shuffle(w, v) Ai.append(sum(E[z_] for z_ in z)) return np.array(Ai)
def _build_row(w, basis, E): Ai = [] for j, v in enumerate(basis): z = shuffle(w, v) Ai.append(sum(E[z_] for z_ in z)) return np.array(Ai)
def play(deck,verbose): """Plays a game of patience with the deck entered (represented by numbers 1 to 13) and returns the ammount of cards left in the deck when the game ends. If verbose is True then prints each round. Requires Shuffle function to be imported.""" visible=[] #List visible to represent the table deck=shuffle(deck) #Deck is randomly shuffled by imported shuffle function while len(deck)!=0 and len(visible)<10: #While loop to make sure at begining of round deck isnt empty or there are too many piles x=add_to_11(visible) y=jqk(visible) #gets result of both conditions if y==() and x==(): #if they both return empty tuples then a new pile needs to added visible.append(deck.pop(0)) if verbose and len(visible)<10: #if true is etered the new visible is printed print visible if len(deck)>2 and y!=(): #deck has to be longer than 2 to be able to carry this whole function out when y returns a tuple for m in y: visible[m]=deck.pop(0) #loop so that for ever member of the tuple is found and then a new card put over the top of it (replacing it) if verbose: print visible if len(deck)>=2 and x!=(): #deck has to be longer than 1 for this function to be done when add_to_11 gives a non empty tuple as it needs to cards to place on top for n in x: visible[n]=deck.pop(0) if verbose: print visible if len(deck)<3 and y!=() : #if jqk returns a tuple but less than 3 cards are availible then they cannot all be replaced however you would still win to 0 is returned return 0 if len(deck)<2 and x!=(): #if add_to_11 returns a tuple but there are less than 2 cards left not all can be replced but you would still win so 0 is returned return 0 R=len(deck) if R==0: #if its fallen out of while loop due to deck being all used then its won and 0 is returned return R if R>0: #if its fallen out due to too many piles its because it added a tenth on, this means it needs to be replaced to deck to get remaing cards so its one more than it falls out the loop with return R+1
def action_handler(game, Action): the_action = Action.get('actionType', None) pileId=Action.get('pileId', None) fromPileId=Action.get('fromPileId', None) toPileId=Action.get('toPileId', None) numberCards=Action.get('numberCards', None) fromMethod=Action.get('fromMethod', None) toMethod=Action.get('toMethod', None) key=Action.get('key', None) value=Action.get('value', None) userId=Action.get('userId', None) playerId=Action.get('playerId', None) isExit=Action.get('isExit', None) isRemove=Action.get('isRemove', None) toPermission=Action.get('toPermission', None) actionList=Action.get('actionList', None) if the_action == 0: shuffle(game.piles[pileId]) elif the_action==1: game.piles[fromPileId].pileTransfer(game.piles[toPileId], fromMethod, toMethod, numberCards, None, None) elif the_action==2: if userId != -1: value = game.players[userId].map_change(key) if type(value) is not int: value = int(eval(value)) game.vars[key] = value elif the_action==3: # board -> player player_exit_enter(playerId, isExit) elif the_action==4: # board -> pile pile_creation_removal(game.piles[pileId], isRemove) elif the_action==5: # board -> pile permission_change(game.piles[pileId], toPermission) elif the_action==6: decision = game.players[userId].ask_the_user(actionList) action_handler(game, actionList[decision])
def optdigits(filename): fh = open(filename, "r") data = [] labels = [] fl = fh.readlines() for line in fl: vals = line.split('\n')[0].rsplit(',') if len(vals) != 65: break data.append([int(vals[i]) for i in range(64)]) labels.append(vals[64]) shuffle.shuffle(data, labels, shuffle.generate_seed(len(data))) return numpy.array(data), labels
def glass(filename): fh = open(filename, "r") data = [] labels = [] fl = fh.readlines() for line in fl: vals = line.split('\n')[0].rsplit(',') if len(vals) != 11: break data.append([float(vals[i + 1]) for i in range(9)]) labels.append(tr(vals[10])) shuffle.shuffle(data, labels, shuffle.generate_seed(len(data))) return numpy.array(data), labels
def shuffle_a_file(fn): fid = open(fn, 'r+t') arr = [] for ln in fid: if len(ln.strip()) == 0: continue arr.append(ln) shuffle(arr) fid.seek(0) for ii in arr: fid.write(ii) fid.close()
def encrypt_flow(img, filepath, keypass): """ UI flow for an encryption operation """ shuff = shuff_algo.shuffle(img, keypass) show(shuff) print("Save image? (y/n)") ifsave = input("> ") if ifsave.lower() == 'y': cv2.imwrite(filepath, shuff)
def quick_select(seq, k): assert len(seq) > 0 shuffle(seq) lo = 0 hi = len(seq) while lo < hi: j = partition(seq, lo, hi) if k < j: hi = j elif j < k: lo = j + 1 else: return seq[j] return seq[k]
def test_enqueue_dequeue(pq): assert len(pq) == 0 with pytest.raises(IndexError): pq.pop_min() values = list( zip(np.random.permutation(size), np.random.standard_normal(size))) for k, v in values: pq.push(k, v) shuffle(values) for k, v in values: pq.decrease(k, v - 1.0) assert len(pq) == size values_sorted = [pq.pop_min() for _ in range(size)] assert len(pq) == 0 assert values_sorted == [i for i, _ in sorted(values, key=lambda x: x[1])]
def avg_ent(filepath, nrange, shuffle_mode=False): ''' For each n (Markov order) in the parameter nrange, averages entropy across all n-grams, estimating the entropy rate of the songs in filepath. ''' if shuffle_mode == True: shuffle.shuffle(filepath) filepath = './output/shuffle.csv' idct = index.get_probs(filepath, nrange) ndct = p_to_ent(filepath, nrange) #print(ndct) result = {} for key, value in ndct.items(): n = key ls = [] ngram_count_list = [] nminus1gram_dict = idct[n - 1] for value in nminus1gram_dict.values(): ngram_count_list.append(value[1]) total_ngram_count = sum(ngram_count_list) for jey, ualue in value.items(): #if ualue[1]>1: ''' See equation 2.11 in Elements of Information Theory (Cover & Thomas, 2nd ed.): Add together the conditional entropies for each prefix, multiplied by the marginal entropy of that prefix. ''' prefix_marginal = nminus1gram_dict[key][1] / total_ngram_count ls.append(prefix_marginal * nminus1gram_dict) #if len(ls)>0: result[key] = sum(ls) '''else: result[key]='-''' with open("./output/entropy.csv", 'w') as output_file: writer = csv.writer(output_file) for key, value in result.items(): row = [] row.append(key) row.append(value) writer.writerow(row) return result
def deal_cards(player_count, cards_in_hand, deck): if player_count * cards_in_hand > len(deck): return "error--number of cards requested is too high" else: deck = shuffle.shuffle(deck) player_hands = {} for x in list(range(player_count)): player_hands["player" + str(x)] = [] for card in list(range(cards_in_hand)): for x in player_hands: player_hands[x] = player_hands[x] + deck[0] deck = deck[1:] return ((player_hands), (deck))
def play(deck, verbose): """ play a single game of patience with a deck. Return the number of cards left in the deck at the end of the game, and therefore if 0 is returned, the game has been won. If verbose, prints the visible cards at each stage of the game. keyword arguments: deck -- a list representing a deck. verbose -- Boolean value controlling whether the visible cards are printed at each stage of the game. """ shuffle(deck) visible = [deck.pop(0), deck.pop(0)] #creates list "visible" which contains the face up cards, and makes the top two cards of the deck the first two elements. if verbose: print visible while len(deck) > 0 and len(visible) < 10: #while game winning conditions (the length of deck becoming 0, or more than 9 piles being created) are not met, play the game. if add_to_11(visible) != (): #if add_to_11(visible) does not equal (), it means there are cards to be replaced a, b = add_to_11(visible) #unpack the indices of two cards which add to eleven visible[a] = deck.pop(0) if len(deck) > 0: #for the case where the length of the deck is 1 at the start of the loop, this ensures the computer doesn't try to access an element in a empty list. visible[b] = deck.pop(0) elif 11 in visible and 12 in visible and 13 in visible: #checks for j, q, k in visible a, b, c = jqk(visible) #unpacks the indices of j, q and k. visible[a] = deck.pop(0) if len(deck) > 1: visible[b] = deck.pop(0) if len(deck) > 1: visible[c] = deck.pop(0) else: #if the above two conditions have not been met, a new pile must be created visible.append(deck.pop(0)) if verbose: print visible if len(visible) == 10: #if a tenth pile has to be turned over, the game is lost, so the card in the tenth pile is returned to the deck. deck.append(visible.pop(9)) return len(deck) #returns the length of the deck as described in the docstring
def krkopt(filename, count, dicts, seed): fh = open(filename, "r") data = [] labels = [] fl = fh.readlines() for line in fl: vals = line.split("\n")[0].rsplit(",") if len(vals) != 7: break element = [] for i in range(6): v = 0 for k in dicts[i].keys(): if vals[i] == k: element.append(v) v += 1 data.append(element) labels.append(vals[6]) shuffle.shuffle(data, labels, seed) return numpy.array(data, numpy.int16), labels
def mushroom(filename, count, dicts, seed): fh = open(filename, "r") data = [] labels = [] fl = fh.readlines() for line in fl: vals = line.split('\n')[0].rsplit(',') if len(vals) != 23: break element = [] for i in range(22): v = 0 for k in dicts[i + 1].keys(): if vals[i + 1] == k: element.append(v) v += 1 data.append(element) labels.append(vals[0]) shuffle.shuffle(data, labels, seed) return numpy.array(data, numpy.int16), labels
def shuffle(self, other, max_order=MAX_SHUFFLE_ORDER): order = self.order + other.order tensor = Tensor(self.dim, order) for w1 in self.sigkeys(): self_w1 = self[w1] if self_w1 == 0.: continue for w2 in other.sigkeys(): if len(w1) + len(w2) > max_order: continue other_w2 = other[w2] if other_w2 == 0.: continue shuffled = shuffle.shuffle(w1, w2) for v in shuffled: tensor[v] += self[w1] * other[w2] return tensor
def rev_announcement(self, s, msg_args): """Handles doing a reverse announcement. secret: Secret for ElGamal decryption. server_pub_keys: Public keys of servers. ann_list_pre: Announcement list before the previous server's modifications. ann_list_post: Announcement list after the previous server's modifications. g_, h_: Verifiable shuffle parameters init_id: The id of the server that was the first in the ring. """ (secret, server_pub_keys, ann_list_pre, ann_list_post, g_, h_, init_id) = msg_args ann_list = ann_list_post if init_id != self.server_id: if init_id == Constants.INIT_ID: init_id = self.server_id ann_list = [] ann_list.append([k for k in self.stp_list.keys()]) ann_list.append([(secret, v) for v in self.stp_list.values()]) else: # verify shuffle from prev server if not shuffle.verify(s, ann_list_pre, ann_list_post, g_, h_): eprint(self.name, 'Verifiable shuffle failed.') s.close() return s.close() self.eph_key = randkey() # update announcement list server_pub_keys.append(self.pub_key) self.secret = powm(secret, self.pri_key) secret = (secret * powm(Constants.G, self.eph_key)) % Constants.P ann_list[1] = [(sec, (rep * self.secret) % Constants.P) for sec, rep in ann_list[1]] new_ann_list = self.announcement_bwd(ann_list, secret, server_pub_keys) # shuffle announcement list n = len(new_ann_list[0]) pi = shuffle.generate_permutation(n) new_ann_list = [shuffle.shuffle(elts, pi) for elts in new_ann_list] # update parameters beta = self.eph_key g_ = Constants.G h_ = 1 for server_pub_key in server_pub_keys: h_ = (h_ * server_pub_key) % Constants.P # pass announcement list to next server s = socket.socket() s.connect(self.prev_addr) send(s, [ Constants.REV_ANNOUNCEMENT, secret, server_pub_keys, ann_list, new_ann_list, g_, h_, init_id ]) # prove shuffle to prev server if self.addr != self.prev_addr: shuffle.prove(s, ann_list, new_ann_list, pi, beta, g_, h_) else: # verify shuffle from next server (if more than one server) if self.addr != self.next_addr: if not shuffle.verify(s, ann_list_pre, ann_list_post, g_, h_): eprint(self.name, 'Verifiable shuffle failed.') s.close() return s.close() # initialize add announcement ltp_list = list(zip(ann_list[0], [rep for sec, rep in ann_list[1]])) ann_args = [ltp_list, secret, Constants.INIT_ID] self.replace_ltp(ann_args)
from NSQuestions import NSQuestions #print NSQuestions from FTQuestions import FTQuestions #print FTQuestions from IEQuestions import IEQuestions #print IEQuestions from JPQuestions import JPQuestions #print JPQuestions from MyersBriggsTypes import TYPES # print TYPES from shuffle import shuffle shuffle (NSQuestions) shuffle (FTQuestions) shuffle (IEQuestions) shuffle (JPQuestions) #while loop if user wants to continue the test? #ask for user name? result = { "N":0, "S":0, "I":0, "E":0, "F":0, "T":0, "J":0, "P":0 } def Quizzer (Questions, Type1, Type2, result):
# train process alpha = 0.01 epoch = 148 mini_batch = 80 l = 50 #lamda index = 5 for index in range(6): x_train = list_x_train[index] y_train = list_y_train[index] theta1 = randInitializeWeights(784, hidden_layer) theta2 = randInitializeWeights(hidden_layer, hidden_layer) theta3 = randInitializeWeights(hidden_layer, 10) print 'training... ' + str(index + 1) for i in range(epoch): # mini batch x_train, y_train = shuffle(x_train, y_train) for k in range(len(x_train)/mini_batch): x = x_train[k*mini_batch:(k + 1)*mini_batch] y = y_train[k*mini_batch:(k + 1)*mini_batch] results = nnCostFunction(theta1, theta2, theta3, x, y, lamda=l) grad1 = results[1] grad2 = results[2] grad3 = results[3] #print 'inter ' + str(i) + ' coss = ' + str(results[0]) grad = np.concatenate([unRolling(grad1), unRolling(grad2)]) grad = np.concatenate([grad, unRolling(grad3)]) theta = np.concatenate([unRolling(theta1), unRolling(theta2)]) theta = np.concatenate([theta, unRolling(theta3)]) theta = theta - alpha * grad theta1 = np.reshape(
def test_a_large_array_is_sorted_lowest_to_highest(self): assert sort(shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
if squared == True: x = int(math.sqrt(s)) y = int(math.sqrt(s)) if isPrime == None and squared == None: x, y = fpf.numberFactors(s,primeNumbers) if isPrime != True: break print("No primes") ### make and shuffle the board listOfNumbers = cpl.createList(x,y) correctList = listOfNumbers.copy() done = False while done == False: listOfNumbers = sh.shuffle(x,y,listOfNumbers) fakeList = listOfNumbers.copy() done = sh.checkShuffle(x,y,listOfNumbers) listOfNumbers = fakeList.copy() ### print the board cpl.printList(x,y,listOfNumbers) ### Game game = True while game == True: if listOfNumbers == correctList: break composition = mP.wheresNumber(x,y,listOfNumbers,isPrime) askedNumber = mP.askForInput(x,y)
def train_multiple_outputs(n_images, batch_size, epoch_num, critic_updates=5): g = generator_model() d = discriminator_model() g.load_weights('generator.h5') d.load_weights('discriminator.h5') d_on_g = generator_containing_discriminator_multiple_outputs(g, d) d_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08) d_on_g_opt = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08) d.trainable = True d.compile(optimizer=d_opt, loss=wasserstein_loss) d.trainable = False loss = [perceptual_loss, wasserstein_loss] loss_weights = [100, 1] d_on_g.compile(optimizer=d_on_g_opt, loss=loss, loss_weights=loss_weights) d.trainable = True output_true_batch, output_false_batch = np.ones((batch_size, 1)), np.zeros( (batch_size, 1)) for epoch in range(epoch_num): print('epoch: {}/{}'.format(epoch, epoch_num)) print('batches: {}'.format(batch_size)) start = 0 d_losses = [] d_on_g_losses = [] shuffle() for index in range(int(25000 // batch_size)): data = load_images(start, batch_size) y_train, x_train = data['B'], data['A'] image_blur_batch = x_train image_full_batch = y_train generated_images = g.predict(x=image_blur_batch, batch_size=batch_size) for _ in range(critic_updates): d_loss_real = d.train_on_batch(image_full_batch, output_true_batch) d_loss_fake = d.train_on_batch(generated_images, output_false_batch) d_loss = 0.5 * np.add(d_loss_fake, d_loss_real) d_losses.append(d_loss) print('batch {} d_loss : {}'.format(index + 1, np.mean(d_losses))) d.trainable = False d_on_g_loss = d_on_g.train_on_batch( image_blur_batch, [image_full_batch, output_true_batch]) d_on_g_losses.append(d_on_g_loss) print('batch {} d_on_g_loss : {}'.format(index + 1, d_on_g_loss)) d.trainable = True if (index % 300): save_all_weights(d, g, epoch, int(index * 10)) start += batch_size with open('log.txt', 'a') as f: f.write('{} - {} - {}\n'.format(epoch, np.mean(d_losses), np.mean(d_on_g_losses))) save_all_weights(d, g, epoch, int(np.mean(d_on_g_losses)))
os.mkdir(os.path.join(miss_judge_test_dir, str(j)+'試行目')) for l in range(1, n_epoch+1): os.mkdir(os.path.join(os.path.join(miss_judge_train_dir, str(j)+'試行目'), str(l))) os.mkdir(os.path.join(os.path.join(miss_judge_test_dir, str(j)+'試行目'), str(l))) #ここから学習と判別 for i in range(1, k+1): print(str(i) + '試行目') current_dir = os.path.join(main_dir, str(i)+'試行目') #current_dirがなければ,作成 if not os.path.exists(current_dir): os.mkdir(current_dir) #shuffleにより評価用画像枚数を返す. #N_testはテスト枚数,flagが0ならinputdirからランダムに1枚抜き出し,評価用画像とし,残りを教師用画像とする. #flagが1なら教師用フォルダから教師用に,評価用フォルダから評価用にわける. N_test = shuffle(input_dir, current_dir, flag = 0) train_dir = os.path.join(current_dir, 'training') test_dir = os.path.join(current_dir, 'testing') #data augmentationの実行 N, G = augmentation(os.path.join(train_dir, 'image1'), os.path.join(train_dir, 'image2'), 100, (h,w)) #教師用、評価用ともに画像サイズの変更 resize(os.path.join(train_dir, 'image2'), os.path.join(train_dir, 'image3'), h, w) resize(os.path.join(test_dir, 'image1'), os.path.join(test_dir, 'image2'), h, w) #時間計測 start_time = time.clock() pathsAndLabels = [] paths = [] for j in range(0, G): pathsAndLabels.append(np.asarray([os.path.join(os.path.join(train_dir, 'image3'), 'stage'+str(j)+'\\'), j]))
def test1(self): self.assertEqual(sum(range(5)), sum(shuffle(range(5))))
def test_shuffle(self): inp = [i for i in range(0, 1000)] self.assertNotEqual(inp.copy(), shuffle(inp))
from get_file_list import get_file_list from shuffle import shuffle from fix_window import fix_window import sys if __name__ == "__main__": folder = sys.argv[1] prefix = sys.argv[2] all_filename = "{}.txt".format(prefix) total = get_file_list(folder, all_filename) train_num = round(total * 0.95) train_filename = "{}_train.txt".format(prefix) test_filename = "{}_test.txt".format(prefix) shuffle(all_filename, train_filename, train_num, test_filename) train_fixed_filename = "{}_train_fixed.txt".format(prefix) test_fixed_filename = "{}_test_fixed.txt".format(prefix) fix_window(train_filename, train_fixed_filename) fix_window(test_filename, test_fixed_filename)
from shuffle import shuffle import sys if len(sys.argv) < 2: print 'Usage: ', __file__, '<count>' exit() count = int(sys.argv[1]) print ','.join(str(c) for c in shuffle(range(count)))
import shuffle L = range(20) print L shuffle.shuffle(L) print L
def test_shuffle_returns_the_same_array_for_a_1_size_array(self): assert shuffle([1]) == [1]
def test_shuffle_returns_a_different_array_when_randomness_stubbed(self): assert shuffle([1,2],1) == [2,1]
def test_shuffle(): order = [0, 0, 1, 1, 1, 1, 2, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 10, 11, 11, 12, 12, 13, 14, 14, 15, 16, 16, 16, 16, 16, 17, 18, 18, 19, 19, 19, 19, 20, 20, 20] result = shuffle(order) assert_not_equal(result, order)
def shuff(): shuffle.shuffle()
def new_announcement(self, s, msg_args): """Handles a request to carry out one step of the announcement phase. generator: Generator for this server to use. ann_list_pre: Announcement list before the previous server's modifications. ann_list_post: Announcement list after the previous server's modifications. g_, h_: Verifiable shuffle parameters init_id: The id of the server that was the first in the ring. """ generator, ann_list_pre, ann_list_post, g_, h_, init_id = msg_args ann_list = ann_list_post if init_id != self.server_id: if init_id == Constants.INIT_ID: init_id = self.server_id ann_list = [] ann_list.append(list(self.ltp_list.keys())) ann_list.append([(self.secret, v) for v in self.ltp_list.values()]) else: # verify shuffle from prev server if not shuffle.verify(s, ann_list_pre, ann_list_post, g_, h_): eprint(self.name, 'Verifiable shuffle failed.') s.close() return s.close() # update announcement list secret_inv = modinv(powm(self.secret, self.pri_key)) new_ann_list = self.announcement_fwd(ann_list, secret_inv) # shuffle announcement list n = len(new_ann_list[0]) pi = shuffle.generate_permutation(n) new_ann_list = [shuffle.shuffle(elts, pi) for elts in new_ann_list] # update generator and parameters new_generator = powm(generator, self.eph_key) beta = 1 g_ = 1 h_ = secret_inv # pass announcement list to next server s = socket.socket() s.connect(self.next_addr) send(s, [ Constants.NEW_ANNOUNCEMENT, new_generator, ann_list, new_ann_list, g_, h_, init_id ]) # prove shuffle to next server (if more than one server) if self.addr != self.next_addr: shuffle.prove(s, ann_list, new_ann_list, pi, beta, g_, h_) else: # verify shuffle from prev server (if more than one server) if self.addr != self.prev_addr: if not shuffle.verify(s, ann_list_pre, ann_list_post, g_, h_): eprint(self.name, 'Verifiable shuffle failed.') s.close() return s.close() # initialize add announcement stp_list = list(zip(ann_list[0], [rep for sec, rep in ann_list[1]])) stp_args = [stp_list, generator, Constants.INIT_ID] self.replace_stp(stp_args)
def test_shuffle_length(self): array = [1, 2, 3, 4, 5, 6] array_cp = copy.copy(array) shuffle(array) self.assertEqual(array.__len__(), array_cp.__len__())
assert 1 <= m < n, "m must be smaller than n." edges = list() deg = np.zeros(n) for i in range(m, n): if i == m: for j in range(i): edges.append((j, i)) deg[j] += 1 deg[i] += 1 continue connection = np.random.choice(range(n), size=m, replace=False, p=deg / np.sum(deg)) for cnt in connection: edges.append((cnt, i)) deg[cnt] += 1 deg[i] += 1 edges = shuffle(n, edges) os.makedirs('generated', exist_ok=True) with open(os.path.join('generated', args.out_txt), 'w') as f: f.write(str(n) + '\n') f.write(str(len(edges)) + '\n') for edge in edges: f.write('%d %d\n' % (edge[0], edge[1]))
def test_basic(): # type: ignore msg = b"abcdefghijklm" * 1000 assert shuffle.unshuffle(shuffle.shuffle(msg)) == msg
def three_way_quick_sort(seq): shuffle(seq) quick_subsort(seq, 0, len(seq))
# # Insertion sorting algorithm # from shuffle import gen_list, shuffle def insertion_sort(lst: list): """Insertion sorting algorithm """ for i in range(1, len(lst)): current = lst[i] current_index = i - 1 while (current_index >= 0) and (lst[current_index] > current): lst[current_index + 1] = lst[current_index] current_index = current_index - 1 lst[current_index + 1] = current if __name__ == '__main__': lst = gen_list(20) print('Generate List:', lst) shuffle(lst) print('Shuffled List:', lst) insertion_sort(lst) print('Sorted List: ', lst)