def getLowestFiltered(A, A_prime, B, levels, color_model): min_A = A min_A_prime = A_prime min_B = B for i in xrange(levels): min_A = rc.reduce(min_A, kernel, K) min_A_prime = rc.reduce(min_A_prime, kernel, K) min_B = rc.reduce(min_B, kernel, K) output_img = np.copy(min_A) output_img_tolist = output_img.tolist() feature_array_A = get_feature_array(min_A) feature_array_B = get_feature_array(min_B) nbrs = NearestNeighbors(n_neighbors=1, algorithm='brute').fit(feature_array_A) rows, cols, _ = min_A.shape if (color_model == "RGB"): for i in xrange(rows): for j in xrange(cols): feature = feature_array_B[i * cols + j] best_match_index = nbrs.kneighbors([feature])[1][0][0] row_index, col_index = best_match_index / cols, best_match_index % cols output_img_tolist[i][j] = min_A_prime[row_index][col_index] elif (color_model == "YIQ"): for i in xrange(rows): for j in xrange(cols): feature = feature_array_B[i * cols + j] best_match_index = nbrs.kneighbors([feature])[1][0][0] row_index, col_index = best_match_index / cols, best_match_index % cols bgrA = min_A_prime[row_index][col_index] bgrB = min_B[i][j] Y = (0.299 * bgrA[2] + 0.587 * bgrA[1] + 0.114 * bgrA[0]) / 255 I = (0.596 * bgrB[2] - 0.275 * bgrB[1] - 0.321 * bgrB[0]) / 255 Q = (0.212 * bgrB[2] - 0.523 * bgrB[1] + 0.311 * bgrB[0]) / 255 r = (Y + (0.956 * I) + (0.621 * Q)) * 255 g = (Y - (0.272 * I) - (0.647 * Q)) * 255 b = (Y - (1.105 * I) + (1.702 * Q)) * 255 output_img_tolist[i][j] = [b, g, r] return np.array(output_img_tolist)
def make_feature(df): cate_cols = [ 'uid', 'task_id', 'adv_id', 'creat_type_cd', 'adv_prim_id', 'dev_id', 'inter_type_cd', 'slot_id', 'spread_app_id', 'tags', 'app_first_class', 'app_second_class', 'city', 'city_rank', 'device_name', 'device_size', 'career', 'gender', 'net_type', 'residence', 'app_score', 'emui_dev', 'consume_purchase', 'indu_name' ] # 滑窗groupy nunique_group = [] key = 'uid' feature_target = [ 'task_id', 'adv_id', 'dev_id', 'indu_name', 'adv_prim_id', 'slot_id', 'spread_app_id' ] for target in tqdm(feature_target): if key + '_' + target + '_win_nunique' not in nunique_group: nunique_group.append(key + '_' + target + '_nunique') tmp = time_groupby(df, 5, key, target) df = df.merge(tmp, on=[key, 'pt_d'], how='left') df = adjust(df, key, key + '_' + target + '_win_nunique') if target + '_' + key + '_win_nunique' not in nunique_group: nunique_group.append(target + '_' + key + '_win_nunique') tmp = time_groupby(df, 5, target, key) df = df.merge(tmp, on=[target, 'pt_d'], how='left') df = adjust(df, target, target + '_' + key + '_win_nunique') df = reduce(df) # embedding特征 emb_cols = [['uid', 'adv_id']] sort_df = df.sort_values('pt_d').reset_index(drop=True) for f1, f2 in emb_cols: df = df.merge(emb(sort_df, f1, f2), on=f1, how='left') # ctr特征 feature_list = cate_cols for feat_1 in tqdm(feature_list): res = pd.DataFrame() for period in [1, 2, 3, 4, 5, 6, 7, 8, 10]: if period == 1: count = df[df['pt_d'] <= period].groupby( feat_1, as_index=False)['label'].agg({feat_1 + '_rate': 'mean'}) elif period == 10: count = df[df['pt_d'] < 8].groupby( feat_1, as_index=False)['label'].agg({feat_1 + '_rate': 'mean'}) else: count = df[df['pt_d'] < period].groupby( feat_1, as_index=False)['label'].agg({feat_1 + '_rate': 'mean'}) count['pt_d'] = period res = res.append(count, ignore_index=True) df = pd.merge(df, res, how='left', on=[feat_1, 'pt_d'], sort=False) df[feat_1 + '_rate'] = reduce_s(df[feat_1 + '_rate'].fillna(-1)) print(feat_1, ' over') return df
def Head(nouns: [str], verbs: [(str, [str])]) -> str: '''returns a noun from a noun list and a verb list. which comes from a line, that the next line might use it. ''' verb, arguments = verbs[-1] # wikipedia.org/wiki/Argument_(linguistics) if arguments: head = reduce.reduce(verb, arguments) else: head = nouns[0] if nouns else None return head
def reduce_delegate(image, *args): channels = get_channels(Image) new_channels = reduce.reduce(channels, image.size[0], reduce_S) heights = [len(ch) for ch in new_channels] widths = [len(channel[0]) for channel in new_channels] # width of new image flat_channels = [list(it.chain.from_iterable(ch)) for ch in new_channels] flat_channels = [[int(px) for px in ch] for ch in flat_channels] print("Image reduced from {w}x{h} to R:{rw}x{rh}, G:{gw}x{gh}, B:{bw}x{bh}".format( w=image.size[0], h=image.size[1], rw=widths[0], rh = heights[0], gw=widths[1], gh=heights[1], bw=widths[2], bh=heights[2])) return flat_channels, widths, heights
def force(coefList, time): # creates a coef list with length = number of molecules # possible coef include 1-UPPERLIMIT for i in range(1, UPPERLIMIT): coefList.append(i) # if coef list not equal to number of molecule, recurse if len(coefList) < len(reactantMole) + len(productMole): balanced = force(coefList, time + 1) if balanced: return balanced else: # if list is a multiple of another, continue to the next list if reduce(coefList) != 1: coefList = coefList[:time] continue # resets the reactants and products list reactants = startingReactants.copy() products = startingProducts.copy() index = 0 # creates reactant list with proper multiplication according to coef list for i in reactants: reactants[i] = reactants[i] * coefList[index] index = index + 1 # creates product list with proper multiplication according to coef list for i in products: products[i] = products[i] * coefList[index] index = index + 1 # checks if number of elements in reactant = number of elements in product # if so, print solution with correct forman if checkBalanced(reactants, products): solution = "Solution: " index = 0 for i in reactantMole: coef = coefList[index] if coef == 1: coef = "" solution += str(coef) + i + " + " index = index + 1 solution = solution[: len(solution) - 3] + " -> " for i in productMole: coef = coefList[index] if coef == 1: coef = "" solution += str(coef) + i + " + " index = index + 1 print(solution[: len(solution) - 3]) return True # deletes last number in coefList so next recurrsion can append a new number coefList = coefList[:time]
def main(): # 功能选择 print("+---------------请选择操作:------------+\n") print("1.--------入库--------\n") print("2.--------出库--------\n") print("3.--------查询--------\n") print("4.--------统计--------\n") warn.warn() str = int(input("\n请输入:")) #功能实现 if str == 1: store.store() elif str == 2: reduce.reduce() elif str == 3: query.query() elif str == 4: print("------------统计功能---------\n") print("1.----------库元件总价值统计---------------\n") print("2.----------每月出库元件总价值统计---------\n") print("3.----------各类元件本月消耗量-------------\n") first_value = int(input("请选择统计类:")) statistics.statistics(first_value)
def test(self): test_data = ( ([], []), ([1], [1]), ([1, 2], [1, 2]), ([1, 1], [1]), ([1, 1, 2], [1, 2]), ([1, 2, 2], [1, 2]), ([1, 2, 1], [1, 2, 1]), ([None, None, 1], [None, 1]), ) for input_, exp_output in test_data: with self.subTest(input_=input_, exp_output=exp_output): sections = list(reduce(input_)) self.assertEqual(sections, exp_output)
exit(-1) return [minimal, maximal, letters] def process(letters, minimal, maximal): f = open("german.small", "r") letterword = "".join(letters) for line in f: string = line.strip().upper() length = len(string) if length <= len(letters) and length >= minimal and length <= maximal: possible = True for l in list(string): if l not in letters: possible = False break if string.count(l) > letterword.count(l): possible = False break if possible: print(line.strip()) if __name__ == "__main__": [minimal, maximal, letters] = arguments() if not os.path.isfile("german.small"): reduce() process(letters, minimal, maximal)
def getAnalogyResult(A, A_prime, B, red_B_prime, pyramid_level, levels, kappa, method, case): #method is either YIQ or RGB, case is whether approximate or both rows, cols, _ = A.shape output_image_np = np.copy(B) output_image = output_image_np.tolist() synth_tracker = np.zeros((rows, cols)) synth_tracker = synth_tracker.tolist() output_image_lum, A_prime_lum = getLuminanceImages(np.array(output_image), A_prime) S = {} #dictionary for storing indexes temp_dict = {} full_feature_arr_A = get_full_shape_feature_array( A, rc.reduce(A, rc.kernel, rc.k), rc.reduce(A_prime, rc.kernel, rc.k)) full_feature_arr_B = get_full_shape_feature_array( B, rc.reduce(B, rc.kernel, rc.k), red_B_prime) nbrs = NearestNeighbors(n_neighbors=1, algorithm='brute').fit(full_feature_arr_A) B_lum, B_prime_lum = getLuminanceImages(B, np.array(output_image)) red_B_lum, red_B_prime_lum = getLuminanceImages( rc.reduce(B, rc.kernel, rc.k), red_B_prime) A_lum, A_prime_lum = getLuminanceImages(A, A_prime) red_A_lum, red_A_prime_lum = getLuminanceImages( rc.reduce(A, rc.kernel, rc.k), rc.reduce(A_prime, rc.kernel, rc.k)) if (case == "approximate"): if (method == "YIQ"): for i in xrange(rows): for j in xrange(cols): full_feature = full_feature_arr_B[(i) * (cols) + j] try: index = temp_dict[tuple(full_feature)] except: index = nbrs.kneighbors([full_feature])[1][0][0] temp_dict[tuple(full_feature)] = index Papp = (index / cols, index % cols) S[(i, j)] = Papp synth_tracker[i][j] = 1 Y = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] bgr = B[i][j] I = (0.596 * bgr[2] - 0.275 * bgr[1] - 0.321 * bgr[0]) / 255 Q = (0.212 * bgr[2] - 0.523 * bgr[1] + 0.311 * bgr[0]) / 255 r = (Y + (0.956 * I) + (0.621 * Q)) * 255 g = (Y - (0.272 * I) - (0.647 * Q)) * 255 b = (Y - (1.105 * I) + (1.702 * Q)) * 255 # output_image[i][j] = A_prime[S[(i,j)][0]][S[(i,j)][1]] output_image[i][j] = [b, g, r] output_image_lum[i][j] = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] print( "pyramid_level %d, of size (%d,%d)" % (pyramid_level, rows, cols), i, j) elif (method == "RGB"): for i in xrange(rows): for j in xrange(cols): full_feature = full_feature_arr_B[(i) * (cols) + j] try: index = temp_dict[tuple(full_feature)] except: index = nbrs.kneighbors([full_feature])[1][0][0] temp_dict[tuple(full_feature)] = index Papp = (index / cols, index % cols) S[(i, j)] = Papp synth_tracker[i][j] = 1 output_image[i][j] = A_prime[S[(i, j)][0]][S[(i, j)][1]] output_image_lum[i][j] = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] print( "pyramid_level %d, of size (%d,%d)" % (pyramid_level, rows, cols), i, j) elif (case == "both"): if (method == "YIQ"): for i in xrange(rows): for j in xrange(cols): full_feature = full_feature_arr_B[(i) * (cols) + j] try: index = temp_dict[tuple(full_feature)] except: index = nbrs.kneighbors([full_feature])[1][0][0] temp_dict[tuple(full_feature)] = index Papp = (index / cols, index % cols) Pcoh, Fl_q = bestCoherenceMatch( i, j, synth_tracker, B_lum, red_B_lum, output_image_lum, red_B_prime_lum, A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, rows, cols, S) if (Pcoh[0] < 0 or Pcoh[1] < 0 or Pcoh[0] >= rows or Pcoh[1] >= cols): S[(i, j)] = Papp else: Fl_Papp = getF(A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, Papp[0], Papp[1], synth_tracker, rows, cols) d_app = np.linalg.norm( subtr(np.array(Fl_Papp), np.array(Fl_q)))**2 Fl_Pcoh = getF(A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, Pcoh[0], Pcoh[1], synth_tracker, rows, cols) d_coh = np.linalg.norm( subtr(np.array(Fl_Pcoh), np.array(Fl_q)))**2 if (d_coh <= d_app * (1 + kappa * (2**(-pyramid_level)))): S[(i, j)] = Pcoh else: S[(i, j)] = Papp # S[(i,j)] = Papp synth_tracker[i][j] = 1 Y = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] bgr = B[i][j] I = (0.596 * bgr[2] - 0.275 * bgr[1] - 0.321 * bgr[0]) / 255 Q = (0.212 * bgr[2] - 0.523 * bgr[1] + 0.311 * bgr[0]) / 255 r = (Y + (0.956 * I) + (0.621 * Q)) * 255 g = (Y - (0.272 * I) - (0.647 * Q)) * 255 b = (Y - (1.105 * I) + (1.702 * Q)) * 255 # output_image[i][j] = A_prime[S[(i,j)][0]][S[(i,j)][1]] output_image[i][j] = [b, g, r] output_image_lum[i][j] = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] print( "pyramid_level %d, of size (%d,%d)" % (pyramid_level, rows, cols), i, j) elif (method == "RGB"): for i in xrange(rows): for j in xrange(cols): full_feature = full_feature_arr_B[(i) * (cols) + j] try: index = temp_dict[tuple(full_feature)] except: index = nbrs.kneighbors([full_feature])[1][0][0] temp_dict[tuple(full_feature)] = index Papp = (index / cols, index % cols) Pcoh, Fl_q = bestCoherenceMatch( i, j, synth_tracker, B_lum, red_B_lum, output_image_lum, red_B_prime_lum, A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, rows, cols, S) if (Pcoh[0] < 0 or Pcoh[1] < 0 or Pcoh[0] >= rows or Pcoh[1] >= cols): S[(i, j)] = Papp else: Fl_Papp = getF(A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, Papp[0], Papp[1], synth_tracker, rows, cols) d_app = np.linalg.norm( subtr(np.array(Fl_Papp), np.array(Fl_q)))**2 Fl_Pcoh = getF(A_lum, red_A_lum, A_prime_lum, red_A_prime_lum, Pcoh[0], Pcoh[1], synth_tracker, rows, cols) d_coh = np.linalg.norm( subtr(np.array(Fl_Pcoh), np.array(Fl_q)))**2 if (d_coh <= d_app * (1 + kappa * (2**(-pyramid_level)))): S[(i, j)] = Pcoh else: S[(i, j)] = Papp # S[(i,j)] = Papp synth_tracker[i][j] = 1 output_image[i][j] = A_prime[S[(i, j)][0]][S[(i, j)][1]] output_image_lum[i][j] = A_prime_lum[S[(i, j)][0]][S[(i, j)][1]] print( "pyramid_level %d, of size (%d,%d)" % (pyramid_level, rows, cols), i, j) return np.array(output_image)
def gen_data(): scenario = "corridor.cfg" reduce.reduce(scenario, "base_line") reduce.reduce(scenario, "model_final")
def test_one_element(sum): assert reduce(sum, [0]) == 0
def test_one(): ''' Testing for non-lists ''' reduce(lambda x, y: x + y, 'iufydt') == 'iufydt'
method = "both" A = cv2.imread('../input/analogies/A.jpg', 1) A_prime = cv2.imread('../input/analogies/A_prime.jpg', 1) B = cv2.imread('../input/analogies/B.jpg', 1) print('getting base B prime') red_B_prime = pyramid_base.getLowestFiltered(A, A_prime, B, levels, color_model) print('got reduced B prime') A_array = [A] current_image = A x = 1 while (levels - x >= 1): current_image = rc.reduce(current_image, rc.kernel, rc.k) A_array.append(current_image) x += 1 B_array = [B] current_image = B x = 1 while (levels - x >= 1): current_image = rc.reduce(current_image, rc.kernel, rc.k) B_array.append(current_image) x += 1 A_prime_array = [A_prime] current_image = A_prime x = 1 while (levels - x >= 1):
return reconstruction_arr #white portion = image1, which is added to image2 def applyMask(img1, img2, mask): newimg1 = img1 * (mask / 255) mask_inv = 255 - mask newimg2 = img2 * (mask_inv / 255) return newimg1 + newimg2 current_image1 = image1 reduced_arr1 = [image1] while (current_image1.shape[0] > 20): current_image1 = rc.reduce(current_image1, kernel, k) reduced_arr1.append(current_image1) expanded_arr1 = [] for j in xrange(1, len(reduced_arr1)): expanded_image = ec.expand(reduced_arr1[j], kernel, k) expanded_arr1.append(expanded_image) laplacian_arr1 = [] for i in xrange(len(expanded_arr1)): output = difference(reduced_arr1[i], expanded_arr1[i]) laplacian_arr1.append(output / divide_factor) laplacian_arr1.append(reduced_arr1[len(reduced_arr1) - 1] / divide_factor) current_image2 = image2 reduced_arr2 = [image2]
def test_product_zero(product): assert reduce(product, [0,1,2]) == 0
def test_sum_zero(sum): assert reduce(sum, [0,1,2]) == 3
def test_product(product): assert reduce(product, [1,2,3]) == 6
def test_sum(sum): assert reduce(sum, [1,2,3]) == 6
from reduce import reduce from functools import reduce as reduce2 def gcd(a: int, b: int) -> int: if a == 0 and b == 0: raise ValueError("GCD is not defined for a = b = 0") while b != 0: a, b = b, a % b return a if __name__ == '__main__': numbers = [12, 30, 50, 8] print(reduce(numbers, gcd)) print(reduce2(gcd, numbers))
def test_five(): ''' Testing with single value in list ''' print(reduce(lambda x, y: x + y, [360]))
def test_empty_list(sum): assert reduce(sum, []) is None
def test_three(): ''' Testing with valid input and addition function ''' print(reduce(lambda x, y: x + y, [1,2,3,4,5])) == 15
distances = comm.recv(source=MPI.ANY_SOURCE, tag=mapTag, status=status) for d in distances: oldDistances[d] = distances[d] print(oldDistances) stat = comm.recv(source=MPI.ANY_SOURCE, tag=startTag, status=status) if stat == ready: dataToSend = { "NodeID": d, "Structure": { "adjacecyList": adjList[str(d)], "distance": distances[d], "distances": oldDistances } } comm.send(dataToSend, dest=status.source, tag=mapTag) counter += 1 if counter == 4: break else: while (True): data = comm.recv(source=master, tag=mapTag, status=status) precDistances = data["Structure"]["distances"] newDistances = maper(data["NodeID"], data["Structure"]) distances = reduce(data["NodeID"], newDistances, precDistances) comm.send(distances, dest=status.source, tag=mapTag) comm.send(ready, dest=0, tag=startTag) break
def test_four(): ''' Testing with valid input and multiplication function ''' print(reduce(lambda x, y: x * y, [1,2,3,4,5])) == 120
[1,4,6,4,1], \ [4,16,24,16,4], \ [6,24,36,24,6], \ [4,16,24,16,4], \ [1,4,6,4,1] ] #kernel_multiplier k = 1 / 256.0 divide_factor = 1 current_image = image reduced_arr = [image] while (current_image.shape[0] > 20): current_image = rc.reduce(current_image, kernel, k) reduced_arr.append(current_image) expanded_arr = [] for j in xrange(1, len(reduced_arr)): expanded_image = ec.expand(reduced_arr[j], kernel, k) expanded_arr.append(expanded_image) def difference(G_image1, expanded_image2): rows1, cols1, _ = G_image1.shape rows2, cols2, _ = expanded_image2.shape extra_rows = rows2 - rows1 extra_cols = cols2 - cols1 if (extra_rows > 0 and extra_cols > 0): new_expanded_image2 = np.array(
def test_two(): ''' Testing with empty list ''' reduce(lambda x, y: x + y, []) == None
comm.send(ready, dest=0, tag=mapTag) if rank == master: dataDict = getLines(N) for data in dataDict: info = comm.recv(source=MPI.ANY_SOURCE, tag=mapTag, status=status) if info == ready: comm.send(dataDict[data], dest=status.source, tag=mapTag) totalItemSetDict = Dictlist() for i in range(1, N): combinedItemSetDict = comm.recv(source=MPI.ANY_SOURCE, tag=reduceTag, status=status) for item in combinedItemSetDict: totalItemSetDict[item] = combinedItemSetDict[item] minSupport = 100 goodSupport = reduce(totalItemSetDict, minSupport) for i in goodSupport: print(str(i) + ' - Support = ' + str(goodSupport[i])) else: while (condition): data = comm.recv(source=master, tag=mapTag, status=status) maxNrInCombination = 2 if data: combinedItemSetDict = maper(data, maxNrInCombination) comm.send(combinedItemSetDict, dest=master, tag=reduceTag) condition = False