def technique_simulation(self, techniques, data_dir, results_dir): configurations = self.get_configurations() te_cross_cfgs = util.cartesian( [list(range(len(techniques))), list(range(len(configurations)))]) if not self.meta_only: rdir = os.path.join(results_dir, str(self), 'techniques') self._prep_dir(rdir) X, y = data.load_data(data_dir) batch_args = [] for n, m in te_cross_cfgs: batch_args.append((configurations[m], arr(X), arr(y), techniques[n], self.split, rdir)) self.pool.map(self.evaluate_technique, batch_args) for meta_split in self.meta_splits: meta_rdir = os.path.join( results_dir, str(self), '{}_meta_techniques'.format(int(100 * meta_split))) self._prep_dir(meta_rdir) X, y = data.load_data(data_dir, remove_last=self.split) meta_batch_args = [] for n, m in te_cross_cfgs: meta_batch_args.append((configurations[m], X[:], y[:], techniques[n], meta_split, meta_rdir)) self.pool.map(self.evaluate_technique, meta_batch_args)
def test_cartesian(self): x = [0, 1, 5] y = [0, 1, 10, 12] cart = util.cartesian((x, y)) self.assertEqual( len(x) * len(y), len(cart), msg='cartesian does not produce correct number of combinations')
def _get_all_ra_dec(input_wcs, h, w): # An array of all the possible permutation of (i,j) for i=0..999 and j=0..999 xx = np.arange(0.5, h + 0.5, 1, dtype=np.int16) yy = np.arange(0.5, w + 0.5, 1, dtype=np.int16) _ij_grid = cartesian((xx, yy)) # Convert pixel coordinates to world coordinates world = input_wcs.wcs_pix2world(_ij_grid, 0, ra_dec_order=True) return world[:, 0], world[:, 1]
def get_configurations(): rf_param_max_fts = [('max_features', round(x, 2)) for x in np.arange(0.05, 1.05, 0.05)] rf_parameters = [rf_param_max_fts] rf_param_settings = [] for setting in util.cartesian( list(map(lambda x: range(len(x)), rf_parameters))): rf_param_settings.append({ rf_parameters[n][m][0]: rf_parameters[n][m][1] for n, m in enumerate(setting) }) return rf_param_settings
def make_anchor_lines(self): categories = ["_Toppkjetting", "_Tau", "_Bunnkjetting"] for row in self.anchor_config.iterrows(): anchor_num = row[1][0] corner_num = row[1][1] horizontal_length = float(row[1][2]) bottom_length = float(row[1][6]) top_length = float(row[1][7]) degree = float(row[1][3]) depth = float(row[1][4]) rel_depth = depth - self.frame_depth tot_length = (horizontal_length**2 + rel_depth**2) ** 0.5 mid_length = tot_length - bottom_length # Set nodes cos_elv = horizontal_length / tot_length # cos(elevation angle) lengths = [top_length, mid_length, tot_length] node_names = [corner_num] for length in lengths: node_name = round( anchor_num + (tot_length - length) / 1e3, 3 ) node_names.append(node_name) x, y = cartesian( length * cos_elv, degree, self.nodes[corner_num]["pos"][0], self.nodes[corner_num]["pos"][1] ) z = -length * rel_depth / tot_length - self.frame_depth self.nodes[node_name] = { "pos": (x, y, z), "id": self.node_id, "dof": (length != tot_length) } self.node_id += 1 # Set edges for i, category in enumerate(categories): # .split()-hack to avoid annoying floating points self.edges[str(anchor_num).split(".")[0]+category]= { "edge": (node_names[i], node_names[i+1]), "edge_id": self.edge_id + i * len(self.anchor_config) } self.edge_id += 1 self.edge_id += len(self.anchor_config) * (len(categories) - 1)
def _build_d2d_mtx(dgroups, dlookup): """ Helper function for building the dsrc-to-dsrc association matrix. :param dgroups: dictionary mapping a dsrc pseudonym to a list of observation intervals ({pseduonym: [obi_0, ...], ...}) :param dlookup: dictionary mapping a dsrc pseudonym to its row/column in the matrix. :return: d2d_matrix (the likelihood that d_i = d_j) """ n = len(dlookup) mtx = np.full((n, n), 1 / (n - 1)) # ignore the diagonal np.fill_diagonal(mtx, -1) for r in np.arange(mtx.shape[0]): not_assoc = [] for c in np.where(mtx[r, :] > 0)[0]: a_obis = dgroups[dlookup[r]] b_obis = dgroups[dlookup[c]] for n, m in util.cartesian((a_obis, b_obis), indices=False): dist, intersect = util.obi_dist_and_t_intersect(a_obis[n], b_obis[m]) if intersect: not_assoc.append(c) break for c_na in not_assoc: a_obis = dgroups[dlookup[r]] b_obis = dgroups[dlookup[c_na]] s1, s2 = util.get_subject_ids(a_obis, b_obis) _log.debug('found impossible d2d association: {} and {}'.format(dlookup[r], dlookup[c_na])) if s1 == s2: _log.warning( 'invalid impossible d2d association found: {} and {}'.format(dlookup[r], dlookup[c_na])) # zero out everything we can mtx[[r, c_na], [c_na, r]] = 0 # TODO: is there anything else that we can do here? return mtx
def _build_w2d_mtx(wgroups, dgroups, wlookup, dlookup, d2d_mtx, sigma=0.3): """ Helper function for building wifi-to-dsrc association matrix. :param wgroups: dictionary mapping a wifi pseudonym to a list of observation intervals ({pseduonym: [obi_0, ...], ...}) :param dgroups: dictionary mapping a dsrc pseudonym to "" ({pseduonym: [obi_0, ...], ...}) :param wlookup: dictionary mapping a wifi pseudonym to its row in the matrix :param dlookup: dictionary mapping a dsrc pseudonym to its column in the matrix :param d2d_mtx: the dsrc-to-dsrc association matrix :param sigma: constant redistribution factor :return: w2d matrix (the likelihood that w_i == d_j) """ mtx = np.full((len(wlookup), len(dlookup)), 1 / len(wlookup)) r_not_assoc = [] r_assoc_by_loc = [] for r in np.arange(mtx.shape[0]): not_assoc = set() assoc_by_loc = defaultdict(set) for c in np.arange(mtx.shape[1]): w_obis = wgroups[wlookup[r]] d_obis = dgroups[dlookup[c]] for n, m in util.cartesian((w_obis, d_obis), indices=False): wobi = w_obis[n] dobi = d_obis[m] # coverage is non-overlapping; # if d0 is seen by lx while w0 is seen by ly then d0 != w0 dist, t_inter = util.obi_dist_and_t_intersect(wobi, dobi) if dist and t_inter: if c not in assoc_by_loc[wobi.loc]: # make sure that this dsrc isn't already possibly assoc not_assoc.add(c) # if d0 is seen by lx while w0 is seen by lx then d0 might be equal to w0 elif t_inter: assoc_by_loc[wobi.loc].add(c) if c in not_assoc: # we just found that this dsrc might be associated, so update not assoc not_assoc.remove(c) r_not_assoc.append(arr(list(not_assoc))) r_assoc_by_loc.append([arr(list(a)) for a in assoc_by_loc.values()]) # set things to 0 for r, not_assoc in enumerate(r_not_assoc): not_r = np.setdiff1d(np.arange(mtx.shape[0]), [r]) if not_assoc.size: not_assoc = np.union1d(not_assoc, np.where(d2d_mtx[not_assoc, :] == 0)[0]) for c_na in not_assoc: s1, s2 = util.get_subject_ids(wgroups[wlookup[r]], dgroups[dlookup[c_na]]) _log.debug('w2d impossible association found: {} and {}'.format(s1, s2)) if s1 == s2: _log.warning('invalid impossible w2d association found: {} and {}'.format(s1, s2)) delta = mtx[r, c_na] mtx[r, c_na] = 0 p_assoc = np.where(mtx[not_r, c_na] > 0)[0] if not p_assoc.size: _log.warning('dsrc {} not associated with any wifi?'.format(dlookup[c_na])) mtx[:, c_na] = (1 / mtx.shape[0]) else: mtx[p_assoc, c_na] += (delta / p_assoc.size) # handle possible associations for r, assoc_by_loc in enumerate(r_assoc_by_loc): not_r = np.setdiff1d(np.arange(mtx.shape[0]), [r]) not_assoc = np.where(mtx[r, :] == 0)[0] for assoc in assoc_by_loc: assoc = np.setdiff1d(assoc, not_assoc) if assoc.size == 1: # we win, hopefully c_a = assoc[0] s1, s2 = util.get_subject_ids(wgroups[wlookup[r]], dgroups[dlookup[c_a]]) _log.debug('w2d association found: {} and {}'.format(wlookup[r], dlookup[c_a])) if s1 != s2: _log.warning('invalid w2d association found: Vehicle {} and Vehicle {}'.format(wlookup[r], dlookup[c_a])) # update probas mtx[:, c_a] = 0 for c_na in np.where(d2d_mtx[c_a, :] == 0)[0]: delta = mtx[r, c_na] mtx[r, c_na] = 0 p_assoc = np.where(mtx[:, c_na] > 0)[0] if p_assoc.size: mtx[p_assoc, c_na] += (delta / p_assoc.size) else: _log.warning('dsrc {} not associated with any wifi?'.format(dlookup[c_na])) mtx[:, c_na] = (1 / mtx.shape[0]) mtx[r, c_a] = 1 else: for c in assoc: delta = mtx[not_r, c].sum() mtx[r, c] += (sigma * delta) mtx[not_r, c] *= (1 - sigma) tots = np.round(mtx.sum(axis=0), 2) if tots.all() != 1.0: _log.warning('uh oh: wifi_2_dsrc matrix columns dont sum to 1.') _log.debug('w2d columns neq 1: {}'.format(','.join(tots[tots != 1].astype(str)))) return mtx
def generate_simulation_settings(algNames, datasizes, dims, dists, kvalues): simSettings = util.cartesian((algNames, datasizes, dims, dists, kvalues)) return simSettings