def uniform_dist(min_size, max_size, n_pos, scale_fac, G_nodes, folNm): neg_comp_list_init = [] n_each = int(np_ceil(float(n_pos) / (max_size - min_size + 1))) # Uniform distribution n_each = int(np_ceil(n_each * scale_fac)) neg_comp_list_init_append = neg_comp_list_init.append # Negative complexes - random walks from random seeds with incremental steps from min_size to max_size for n in range(min_size, max_size + 1): for k in range(n_each): # Pick a random seed node neg_comp_list_init_append(random_graph(n, G_nodes, folNm)) return neg_comp_list_init
def parse_matrix_part(matrix, szSub, ovSub): assert matrix.ndim == 3 assert np_ndim(szSub) == 1 assert len(szSub) == 3 assert np_ndim(ovSub) == 1 assert len(ovSub) == 3 matrix_shape = np_asarray(matrix.shape, dtype=int) len_each_section, _, _ = szSub shift_length, _, _ = ovSub len_each_section_range = np_arange(len_each_section) matrix_shape = np_ceil((matrix_shape - szSub + 1)/ovSub).astype(int) num_rows_overlap, num_elements, num_beams = matrix_shape result_matrix = np_zeros((np_prod(szSub), np_prod(matrix_shape))) cnt = 0 for i in range(num_beams): for j in range(num_elements): for k in range(num_rows_overlap): index_1 = len_each_section_range + k * shift_length index_2 = j index_3 = i tmp = matrix[index_1, index_2, index_3] result_matrix[:, cnt] = tmp cnt += 1 return result_matrix
def same_dist(scale_fac, G_nodes, sizes, folNm): neg_comp_list_init = [] ct_sizes = {} for sz in sizes: if sz in ct_sizes: ct_sizes[sz] += 1 else: ct_sizes[sz] = 1 neg_comp_list_init_append = neg_comp_list_init.append wrong_size_ct = 0 for sz in ct_sizes: nk = int(np_ceil(ct_sizes[sz] * scale_fac)) for k in range(nk): neg_comp = random_graph(sz, G_nodes, folNm) neg_comp_sz = len(neg_comp.nodes()) if neg_comp_sz in ct_sizes: if ct_sizes[neg_comp_sz] > 0: ct_sizes[neg_comp_sz] -= 1 else: wrong_size_ct += 1 else: wrong_size_ct += 1 neg_comp_list_init_append(neg_comp) print("Wrong sizes count ", wrong_size_ct) return neg_comp_list_init
def binomial_distribution_two_tailed_range(n: int, p: float, sds: float): """ Calculates range of `x` values that span `sds` standard deviations from the mean of a binomial distribution with parameters `n` and `p`: `Binom(n,p)` """ M = n * p sd = math_sqrt(n * p * (1 - p)) (x_from, x_to) = max(0, np_floor(M - sds * sd) - 1), min(n, np_ceil(M + sds * sd) + 1) # (y_from, y_to) = binomial_distribution.pmf(x_from, n, p), binomial_distribution.pmf(x_to, n, p) return int(x_from), int(x_to)
def test_uccsd_ansatz_circuit(sample_molecule, h2_programs): n_spatial_orbitals = sample_molecule.n_orbitals n_occupied = int(np_ceil(sample_molecule.n_electrons / 2)) n_virtual = n_spatial_orbitals - n_occupied n_single_amplitudes = n_occupied * n_virtual # make a mock-packed_amplitudes of length (2 * n_single_amplitudes) packed_amplitudes = [1]*(2 * n_single_amplitudes + int(comb(n_single_amplitudes, 2))) ansatz = uccsd_ansatz_circuit(packed_amplitudes, sample_molecule.n_orbitals, sample_molecule.n_electrons) assert isinstance(ansatz, Program) assert ansatz.out() == h2_programs.out()
def pick_top_weight_neigs(neig_list, perc, inputs): n_maxs = len(neig_list) if n_maxs == 0: return None if n_maxs > inputs['min_thres_neig_sorted']: n_maxs = int(np_ceil(perc * len(neig_list))) else: return neig_list imp_neigs = dict( sorted(neig_list.items(), key=lambda elem: elem[1]['weight'], reverse=True)[:n_maxs]) return imp_neigs
def find_max_neig(neig_list,g1,perc,model,scaler,inputs): n_maxs = len(neig_list) if n_maxs == 0: return None if n_maxs > 10: # Ascending order n_maxs = int(np_ceil(perc*len(neig_list))) neig_key_list = [k for k in neig_list] neig_wt_list = [float(neig_list[k]['weight']) for k in neig_list] sorted_ind = np_argsort(neig_wt_list) sorted_wts = [{'weight':val} for val in np_sort(neig_wt_list)][-n_maxs:] sorted_neig_keys = [neig_key_list[i] for i in sorted_ind][-n_maxs:] imp_neigs = dict(zip(sorted_neig_keys,sorted_wts)) folNm = inputs['folNm'] if len(imp_neigs) == 1: imp_neig = list(imp_neigs.keys())[0] wt = imp_neigs[imp_neig] wt_edge = wt['weight'] node_to_add = imp_neig #ADD ALL EDGES OF NEW NODE TO ORIG GRAPH with open(folNm+"/"+node_to_add,'rb') as f: its_neig_list = pickle_load(f) orig_nodes = g1.nodes() all_nodesWedges = set(orig_nodes).intersection(its_neig_list) for node in all_nodesWedges: wt = its_neig_list[node] wt_edge = wt['weight'] g1.add_edge(node_to_add,node,weight=wt_edge) (score_imp_neig,comp_bool) = get_score(g1,model,scaler,inputs['model_type']) g1.remove_node(node_to_add) else: scores = {} for neig in imp_neigs: # Add to graph wt = imp_neigs[neig] wt_edge = wt['weight'] node_to_add = neig #ADD ALL EDGES OF NEW NODE TO ORIG GRAPH with open(folNm+"/"+node_to_add,'rb') as f: its_neig_list = pickle_load(f) orig_nodes = g1.nodes() all_nodesWedges = set(orig_nodes).intersection(its_neig_list) for node in all_nodesWedges: wt = its_neig_list[node] wt_edge = wt['weight'] g1.add_edge(node_to_add,node,weight=wt_edge) # Check score (score_curr,comp_bool) = get_score(g1,model,scaler,inputs['model_type']) scores[neig] = score_curr g1.remove_node(node_to_add) imp_neig = max(iter(scores.items()), key=operator_itemgetter(1))[0] score_imp_neig = scores[imp_neig] return(imp_neig,score_imp_neig)
dz.deredden_lines(cHbeta, reduc_lineslog_df) #Measure Hbeta flux Hbeta_wavelengths = reduc_lineslog_df.loc['H1_4861A', ['Wave1', 'Wave2', 'Wave3', 'Wave4', 'Wave5', 'Wave6']].values Hbeta_dist = random.normal(reduc_lineslog_df.loc['H1_4861A'].line_Int.nominal_value, reduc_lineslog_df.loc['H1_4861A'].line_Int.std_dev, MC_length) #Insert new section in pdf with dz.pdfDoc.create(Section('HII Galaxy: {}'.format(objName))): dz.add_page() #------Plot Oxygen lines element_lines = reduc_lineslog_df.loc[(reduc_lineslog_df.index.isin(oxygen_emision))].index.values if len(element_lines) > 0: dz.FigConf(plotStyle='seaborn-colorblind', Figtype = 'grid', plotSize = sizing_dict, n_columns = int(len(element_lines)), n_rows = int(np_ceil(len(element_lines)/n_columns))) for j in range(len(element_lines)): #Define plotting regions regions_wavelengths = reduc_lineslog_df.loc[element_lines[j], ['Wave1', 'Wave2', 'Wave3', 'Wave4', 'Wave5', 'Wave6']].values idcs_obj, idcs_stellar = searchsorted(wave_reduc, regions_wavelengths), searchsorted(wave_stellar, regions_wavelengths) subwave_solar, subFlux_solar = wave_stellar[idcs_stellar[0]:idcs_stellar[5]], flux_stellar[idcs_stellar[0]:idcs_stellar[5]] subWwave, subFlux = wave_reduc[idcs_obj[0]:idcs_obj[5]], flux_reduc[idcs_obj[0]:idcs_obj[5]] subWwave_emis, subFlux_emis = wave_emis[idcs_obj[0]:idcs_obj[5]], flux_emis[idcs_obj[0]:idcs_obj[5]] dz.data_plot(subWwave, subFlux, label='', linestyle='step', graph_axis=dz.Axis[j]) dz.data_plot(subwave_solar, subFlux_solar, label='', linestyle='step', graph_axis=dz.Axis[j]) dz.FigWording(xlabel='', ylabel='', title=element_lines[j], graph_axis=dz.Axis[j]) plt.tight_layout()
def ceil(x: Tensor, inplace=False) -> Tensor: ceiled = x if inplace else deepcopy(x) ceiled.name += ' (ceiled)' ceiled.value = np_ceil(x.value) return ceiled
reduc_lineslog_df.loc['H1_4861A'].line_Int.std_dev, MC_length) #Insert new section in pdf with dz.pdfDoc.create(Section('HII Galaxy: {}'.format(objName))): dz.add_page() #------Plot Oxygen lines element_lines = reduc_lineslog_df.loc[( reduc_lineslog_df.index.isin(oxygen_emision))].index.values if len(element_lines) > 0: dz.FigConf(plotStyle='seaborn-colorblind', Figtype='grid', plotSize=sizing_dict, n_columns=int(len(element_lines)), n_rows=int(np_ceil(len(element_lines) / n_columns))) for j in range(len(element_lines)): #Define plotting regions regions_wavelengths = reduc_lineslog_df.loc[ element_lines[j], ['Wave1', 'Wave2', 'Wave3', 'Wave4', 'Wave5', 'Wave6' ]].values idcs_obj, idcs_stellar = searchsorted( wave_reduc, regions_wavelengths), searchsorted( wave_stellar, regions_wavelengths) subwave_solar, subFlux_solar = wave_stellar[ idcs_stellar[0]:idcs_stellar[5]], flux_stellar[ idcs_stellar[0]:idcs_stellar[5]] subWwave, subFlux = wave_reduc[
def plot_coverage(self, plt_figure_title: str, title: str = "Coverage of {method_name}\nsamples: n1 = {sample_size1}, n2 = {sample_size2}", theme: plot_styles = "default", colors: Tuple[str,str,str,str,str] = ("gray", "purple", "white", "#b8df96", "green") ): """ Plots the `matplotlib.pyplot` figure given the data from previous coverage calculation and some captions and formatting. """ if self.coverage is None: raise NoCoverageException( "you have to calculate coverage first before plotting it") # this unpacked defaultdict trouble allows for optional formatting placeholders title = title.format(**defaultdict(str, method_name = self.method_name, sample_size1 = self.sample_size1, sample_size2 = self.sample_size2 )) plt.style.use(theme) """ Colorbar range depends on confidence level. Sets vmin to a point 10 times farther from 100% than the confidence for confidence=95% show colorbar from 50% to 100%; for confidence=99% show colorbar from 90% to 100%; for confidence=99.9% show colorbar from 99% to 100%; """ vmin = 100 - ( (100-(self.confidence*100))*10 ) vmax = 100 """ In LinearSegmentedColormap specified color points have to span from 0 to 1, where 0 would correspond to vmin, and 1 to vmax. the 5 specified colors will form a gradient by marking at points below. For confidence=95%: (50, 90, 95, 97.5, 100) For confidence=99%: (90, 98, 99, 99.5, 100) But because the following value is constant, visually, the colorbar itself will always have the same gradient regardless of the given `confidence` value """ nodes = (0.0, 0.8, 0.9, 0.95, 1.0) cmap = LinearSegmentedColormap.from_list("", list(zip(nodes, colors))) cmap.set_under(colors[0]) fig, ax = plt.subplots() fig.canvas.set_window_title(plt_figure_title) """ Would be great if matplotlib supported float128/longdouble. Instead, it converts data to float64 with a warning: "UserWarning: Casting input data from 'float128' to 'float64' for imshow" But! float128 precision could possible be utilized while using float64 in this case. If we were to display not the value (the coverage, 0-100), but the difference between the expected coverage (confidence) and the actual coverage, float64 would do a lot better. This can be done, but some adjustments have to be made to colorbar and labels. """ im = ax.imshow(float64(np_array(self.coverage)), cmap=cmap, norm=Normalize(float(vmin), vmax, True) ) # precision of "8" decimal places should be more than enough for colorbar ticks cb = fig.colorbar(im, format=ticker.FuncFormatter(lambda x, pos: (f'%.8f' % x).rstrip('0').rstrip('.'))) # plot a dashed black line over *confidence* point on a colorbar cb.ax.plot([0, 100], [self.confidence*100, self.confidence*100], '_:k') # rewriting autogenerated colorbar ticks by adding one that corresponds to `confidence` colorbar_ticks = cb.ax.get_yticks() colorbar_ticks = np_append(colorbar_ticks, float(self.confidence*100)) cb.set_ticks(colorbar_ticks) plt.title(title, fontsize="large", fontweight="bold") # this is reasonable number of ticks so that tick labels won't overlap max_num_xticks = 7 max_num_yticks = 20 xticks_period = int(np_ceil(len(self.proportions)/max_num_xticks)) yticks_period = int(np_ceil(len(self.proportions)/max_num_yticks)) xperiodic_probs = [float(v) for v in self.proportions[::xticks_period]] yperiodic_probs = [float(v) for v in self.proportions[::yticks_period]] ax.xaxis.set_major_locator(ticker.MultipleLocator(xticks_period)) ax.yaxis.set_major_locator(ticker.MultipleLocator(yticks_period)) ax.tick_params(axis='x', labelsize=8) ax.tick_params(axis='y', labelsize=9) ax.tick_params(top=False) ax.tick_params(right=False) # ax.xaxis.set_tick_params(labeltop=False) # ax.yaxis.set_tick_params(labelright=False) # auto-calculated ticks are fine except for redundant first and last ticks xticks = ax.get_xticks().tolist()[1:-1] yticks = ax.get_yticks().tolist()[1:-1] ax.set_xticks(xticks) ax.set_yticks(yticks) ax.set_xticklabels(xperiodic_probs) ax.set_yticklabels(yperiodic_probs) self.figure = fig return self.figure