def calculateFrequencyGap(self): freq={} size=len(self.frame) if size<2: print("not enough frame") return freq prev=self.frame[0] for i in range(1,size): a=self.frame[i]-prev freq[a]=freq.get(a,0)+1 prev=self.frame[i] self.frequency_of_gap=freq utils.normalize_dict(self.frequency_of_gap)
def calculateFrequencyGap(self): freq = {} size = len(self.frame) if size < 2: print("not enough frame") return freq prev = self.frame[0] for i in range(1, size): a = self.frame[i] - prev freq[a] = freq.get(a, 0) + 1 prev = self.frame[i] self.frequency_of_gap = freq utils.normalize_dict(self.frequency_of_gap)
def test_network(self, test_cues, depth, resp_lang, resps_gold=None, verbose=True, log_file=None): resps_predicted = {} for cue in test_cues: if cue not in self.G.vs['name']: resps_clean = {} else: starting_vertex = {cue: 1.0} if self.lang == "en": resps = self.spread_activation_ucs(starting_vertex, depth) elif self.lang == "nl-en": if self.model_type == "cs": resps = self.spread_activation_cs( starting_vertex, parameters["spreading depth"], parameters["spreading depth"]) else: resps = self.spread_activation_ucs( starting_vertex, parameters["spreading depth"]) else: sys.exit("Unknown network language.") if None in resps: del resps[None] if cue in resps: del resps[cue] resps_clean = normalize_dict( {r: p for r, p in resps.items() if r[-2:] == resp_lang}) resps_predicted[cue] = resps_clean if verbose: log_file.write("\tCUE: %s\n" % cue) if resps_gold: log_file.write("\t\tGOLD\n") gold_top = sorted(resps_gold[cue].items(), key=lambda x: (-x[1], x[0])) for k, v in gold_top[:10]: log_file.write("\t\t\t%s\t\t%.3f\n" % (k, v)) pred_top = sorted(resps_predicted[cue].items(), key=lambda x: (-x[1], x[0])) log_file.write("\t\tPREDICTED\n") for k, v in pred_top[:10]: log_file.write("\t\t\t%s\t\t%.3f\n" % (k, v)) log_file.flush() ups_max, ups_n, rhos_max, rhos_n = compute_differences( resps_gold, resps_predicted, test_cues) return ups_max, ups_n, rhos_max, rhos_n
def read_bilingual_data(condition): # Reads test data of Van Hell & De Groot (1998). CSV format needed. condition_lang = condition.split("-")[0] if condition_lang == "nl": DD_DE_DD_test_dict = read_bilingual_file("./data/bilingual/DD1-DE2-DD3.lemmas.csv") DE_DD_test_dict = read_bilingual_file("./data/bilingual/DE1-DD2.lemmas.csv") test_lists = [DD_DE_DD_test_dict['D0']['D0'], DD_DE_DD_test_dict['D2']['D2'], DE_DD_test_dict['D1']['D1']] elif condition_lang == "en": EE_ED_EE_test_dict = read_bilingual_file("./data/bilingual/EE1-ED2-EE3.lemmas.csv") ED_EE_test_dict = read_bilingual_file("./data/bilingual/ED1-EE2.lemmas.csv") test_lists = [EE_ED_EE_test_dict['E0']['E0'], EE_ED_EE_test_dict['E2']['E2'], ED_EE_test_dict['E1']['E1']] else: sys.exit("Condition unknown!") gold_dict = {} for session in range(len(test_lists)): for cue_resp, freq in Counter(test_lists[session]).items(): cue = cue_resp[0] + ":" + condition_lang resp = cue_resp[1] + ":" + condition_lang if session not in gold_dict: gold_dict[session] = {} if cue not in gold_dict[session]: gold_dict[session][cue] = {} gold_dict[session][cue][resp] = freq if session < 2: if "aggregated" not in gold_dict: gold_dict["aggregated"] = {} if cue not in gold_dict["aggregated"]: gold_dict["aggregated"][cue] = {} if resp not in gold_dict["aggregated"][cue]: gold_dict["aggregated"][cue][resp] = 0 gold_dict["aggregated"][cue][resp] += freq for session in list(range(len(test_lists))) + ["aggregated"]: gold_dict[session] = filter_dict(gold_dict[session]) for cue in gold_dict[session]: gold_dict[session][cue] = utils.normalize_dict(gold_dict[session][cue]) return gold_dict
def get_cue_resp_dict_en(dn): filenames = [dn + fn for fn in ["Cue_Target_Pairs.A-B", "Cue_Target_Pairs.C", "Cue_Target_Pairs.D-F", "Cue_Target_Pairs.G-K", "Cue_Target_Pairs.L-O", "Cue_Target_Pairs.P-R", "Cue_Target_Pairs.S", "Cue_Target_Pairs.T-Z"]] wn_lemmatizer = WordNetLemmatizer() cue_resp_dict = {} for fn in filenames: with open(fn, 'r', encoding='ISO-8859-1') as norms: reader = csv.reader(norms, quotechar='"', quoting=csv.QUOTE_ALL, delimiter=",") next(reader, None) for row in reader: if " " not in row[0].strip() and " " not in row[1].strip(): cue = lemmatize_word(row[0].strip().lower(), "en", wn_lemmatizer) resp = lemmatize_word(row[1].strip().lower(), "en", wn_lemmatizer) freq = float(row[4].strip()) if cue not in cue_resp_dict: cue_resp_dict[cue] = {} if resp not in cue_resp_dict[cue]: cue_resp_dict[cue][resp] = 0.0 cue_resp_dict[cue][resp] += freq cue_resp_dict = filter_dict(cue_resp_dict) for cue in cue_resp_dict: cue_resp_dict[cue] = normalize_dict(cue_resp_dict[cue]) return cue_resp_dict
def get_cue_resp_dict_nl(fn): with open(fn, 'r') as norms: reader = csv.reader(norms, quotechar='"', quoting=csv.QUOTE_ALL, delimiter=";") next(reader, None) cue_resp_dict = {} for row in reader: cue = row[2].strip().lower() resp = row[3].strip().lower() if " " not in cue and " " not in resp and "," not in cue and "," not in resp: if frog_installed: if cue in lemmas_nl: cue = lemmas_nl[cue] else: cue_lemma = lemmatize_word(cue, "nl", frog_lemmatizer) lemmas_nl[cue] = cue_lemma lemmas_nl_file.write("%s,%s\n" % (cue, cue_lemma)) cue = cue_lemma if resp in lemmas_nl: resp = lemmas_nl[resp] else: resp_lemma = lemmatize_word(resp, "nl", frog_lemmatizer) lemmas_nl[resp] = resp_lemma lemmas_nl_file.write("%s,%s\n" % (resp, resp_lemma)) resp = resp_lemma else: cue = lemmas_nl.get(cue) or cue resp = lemmas_nl.get(resp) or resp if cue and resp: if cue not in cue_resp_dict: cue_resp_dict[cue] = {} if resp not in cue_resp_dict[cue]: cue_resp_dict[cue][resp] = 0.0 cue_resp_dict[cue][resp] += 1.0 cue_resp_dict = filter_dict(cue_resp_dict) for cue in cue_resp_dict: cue_resp_dict[cue] = normalize_dict(cue_resp_dict[cue]) return cue_resp_dict
def dict_learn(self, Y, N, n_patch, max_iter, sparsity=6, seed=1, sparse_coder='klimaps', dict_learner='LS', init_method='data'): '''Learn dictionaries Y: gallery images (features pjected in LDA space) N: number of subjects n_patch: number of patches max_iter: maximum number of iterations for dictionary learning sparsity: sparsity level (if a list of values is pecified, one dict for each sparsity level will be learned) seed: for random number generation sparse_coder: "klimaps" dict_learner: "LS" (Least Squares) n_dicts: number of dictionaries init_method: initialization for the dictionaries; "data" or 'rand' ''' vectorized = True k = sparsity if init_method == 'data': D = init_dictionary(Y=Y, k=k, n_subj=N, n_patch=n_patch, method='data', seed=seed) elif init_method == 'random': D = init_dictionary(Y=Y, k=k, n_subj=N, n_patch=n_patch, method='rand', seed=seed) else: raise ValueError('Unrecognized Dictionary Initialization Method!!') D = normalize_dict(D) Dinv = np.linalg.pinv(D) non_zero = np.zeros([max_iter,1]) residual = np.zeros([max_iter,1]) for i in range(max_iter): print('\nIteration: ' + str(i+1) + ' ------------------------------------------------') print('\nComputing Sparse Codes with ' + sparse_coder + '...') if vectorized: X = klimaps_matrix(Y, D, Dinv, k, 1) else: for p in range(Y.shape[1]): y = Y[:,p] x = klimaps(y, D, Dinv, k, 1) if p==0: X = np.expand_dims(x, axis=1) else: X = np.append(X,np.expand_dims(x, axis=1), axis=1) print('...Done') print('\nComputing Dictionary with ' + dict_learner + ' method...') l0 = np.zeros([N,1]) Dict = [] for j in range(N): #for each sbj (gallery img) y_start = j*n_patch y_end = j*n_patch + n_patch Yj = Y[:,y_start:y_end] x_start_row = j*k x_end_row = j*k + k x_start_col = j*n_patch x_end_col = j*n_patch + n_patch Xj = X[x_start_row:x_end_row, x_start_col:x_end_col] X_sign = X[x_start_row:x_end_row, x_end_col:] if dict_learner == 'LS': Dj = np.matmul(Yj,np.linalg.pinv(Xj)) else: raise ValueError('Unrecognized dictionary learner!!') Dict.append(Dj) print('...Done') D = np.squeeze(np.concatenate(Dict, axis=1)) D = normalize_dict(D) Dinv = np.linalg.pinv(D) residual[i] = np.linalg.norm(np.dot(D,X) - Y) print('\nResidual: ' + str(residual[i])) self.learned_dict = D self.inv_dict = Dinv self.k = sparsity