def test_subclass(self): a = array(1).view(PhysicalQuantity2) b = array(7).view(PhysicalQuantity2) gs = geomspace(a, b) assert type(gs) is PhysicalQuantity2 assert_equal(gs, geomspace(1.0, 7.0)) gs = geomspace(a, b, 1) assert type(gs) is PhysicalQuantity2 assert_equal(gs, geomspace(1.0, 7.0, 1))
def test_dtype(self): y = geomspace(1, 1e6, dtype='float32') assert_equal(y.dtype, dtype('float32')) y = geomspace(1, 1e6, dtype='float64') assert_equal(y.dtype, dtype('float64')) y = geomspace(1, 1e6, dtype='int32') assert_equal(y.dtype, dtype('int32')) # Native types y = geomspace(1, 1e6, dtype=float) assert_equal(y.dtype, dtype('float_')) y = geomspace(1, 1e6, dtype=complex) assert_equal(y.dtype, dtype('complex'))
def test_start_stop_array(self): # Try to use all special cases. start = array([1.e0, 32., 1j, -4j, 1+1j, -1]) stop = array([1.e4, 2., 16j, -324j, 10000+10000j, 1]) t1 = geomspace(start, stop, 5) t2 = stack([geomspace(_start, _stop, 5) for _start, _stop in zip(start, stop)], axis=1) assert_equal(t1, t2) t3 = geomspace(start, stop[0], 5) t4 = stack([geomspace(_start, stop[0], 5) for _start in start], axis=1) assert_equal(t3, t4) t5 = geomspace(start, stop, 5, axis=-1) assert_equal(t5, t2.T)
def user_bkg_spec_a(): """Create "user-background" data for testing. Returns ------- m_bkg_spec : `~jwst.datamodels.MultiSpecModel` """ # This data type is used for creating a MultiSpecModel. spec_dtype = datamodels.SpecModel().spec_table.dtype # m_bkg_spec doesn't have to be a MultiSpecModel, but that's an option. m_bkg_spec = datamodels.MultiSpecModel() wavelength = np.geomspace(1.5, 4.5, num=25, endpoint=True, dtype=np.float64) flux = np.linspace(13., 25., num=25, endpoint=True, retstep=False, dtype=np.float64) fl_error = np.ones_like(wavelength) dq = np.zeros(wavelength.shape, dtype=np.uint32) net = np.zeros_like(wavelength) nerror = np.ones_like(wavelength) background = np.ones_like(wavelength) berror = np.ones_like(wavelength) npixels = np.ones_like(wavelength) otab = np.array(list(zip(wavelength, flux, fl_error, dq, net, nerror, background, berror, npixels)), dtype=spec_dtype) spec = datamodels.SpecModel(spec_table=otab) m_bkg_spec.spec.append(spec) return m_bkg_spec
def user_bkg_spec_c(): """Create "user-background" data for testing. Returns ------- m_bkg_spec : `~jwst.datamodels.CombinedSpecModel` """ # This is the data type of an output table from combine_1d. spec_table_dtype = datamodels.CombinedSpecModel().spec_table.dtype wavelength = np.geomspace(1.5, 4.5, num=25, endpoint=True, dtype=np.float64) flux = np.linspace(13., 25., num=25, endpoint=True, retstep=False, dtype=np.float64) fl_error = np.ones_like(wavelength) dq = np.zeros(wavelength.shape, dtype=np.uint32) net = np.zeros_like(wavelength) weight = np.ones_like(wavelength) n_input = np.ones_like(wavelength) # yes, float64 data = np.array(list(zip(wavelength, flux, fl_error, net, dq, weight, n_input)), dtype=spec_table_dtype) m_bkg_spec = datamodels.CombinedSpecModel(spec_table=data) return m_bkg_spec
def bin_dataframe(df, n_bins): """ Assign a "bin" column to the dataframe to indicate which bin the true charges belong to. Bins are assigned in log space. Parameters ---------- df : pd.DataFrame n_bins : int Number of bins to allow in range Returns ------- pd.DataFrame """ true = df['true'].values min_ = true.min() max_ = true.max() bins = np.geomspace(min_, max_, n_bins) bins = np.append(bins, 10**(np.log10(bins[-1]) + np.diff(np.log10(bins))[0])) df['bin'] = np.digitize(true, bins, right=True) - 1 return df
def get_reynolds(self, velocity): Re = self.foil.Reynolds(velocity) re_space = np.round(np.geomspace(30000, 2e6, 20), -4) idx = np.argmin(abs(re_space - Re)) reynolds = re_space[idx] # np.round(Re, -4) # Round to nearest 1000 if (reynolds < 30000.0): reynolds = 30000.0 return reynolds
def geomspace_stepsize(start, stop, dlnx): """ return equally log spaced array from start to stop (sometimes over) with step size dlnx """ num = np.ceil(np.absolute(np.log(stop) - np.log(start))/dlnx) + 1 if stop > start: stop_real = np.exp(np.log(start)+(num-1)*dlnx) else: stop_real = np.exp(np.log(start)-(num-1)*dlnx) return np.geomspace(start, stop_real, num=num, endpoint=True)
def evaluate(self): var_reports = {} for variable in self.variables: initial_value = self.tf_session.run(variable) start_value = max(np.abs(initial_value/100), .01) base_space = np.geomspace(start_value, np.abs(initial_value), self.width // 2) space = np.hstack((initial_value + base_space, [initial_value], initial_value - base_space)) reports = [] for test_value in space: self._assign_value(variable.name, test_value) reports.append(self.evaluator.evaluate()) self._assign_value(variable.name, initial_value) var_reports[variable.name] = reports return SensitivityReport(var_reports)
def test_scott_vs_stone(self): """Verify that Scott's rule and Stone's rule converges for normally distributed data""" def nbins_ratio(seed, size): rng = np.random.RandomState(seed) x = rng.normal(loc=0, scale=2, size=size) a, b = len(np.histogram(x, 'stone')[0]), len(np.histogram(x, 'scott')[0]) return a / (a + b) ll = [[nbins_ratio(seed, size) for size in np.geomspace(start=10, stop=100, num=4).round().astype(int)] for seed in range(10)] # the average difference between the two methods decreases as the dataset size increases. avg = abs(np.mean(ll, axis=0) - 0.5) assert_almost_equal(avg, [0.15, 0.09, 0.08, 0.03], decimal=2)
def __init__(self, f_max, mu_min, population_distribution, delta_fitness, mu_multiple, fraction_beneficial, fraction_accurate, fraction_mu2mu, K): """ The population is described both by its state and the parameters of the model. Population_distribution should be a single number or a numpy array. """ self.population_distribution = \ np.atleast_2d(np.array(population_distribution, dtype='int64')) if np.any(self.population_distribution) < 0: raise ValueError('the population distribution must be nonnegative') pop_shape = self.population_distribution.shape self.delta_fitness = delta_fitness if self.delta_fitness <= 0: raise ValueError('delta fitness must be positive') self.mu_multiple = mu_multiple if self.mu_multiple <= 1: raise ValueError('mu multiple must be greater than one') self.fraction_beneficial = fraction_beneficial if self.fraction_beneficial >= 1 or self.fraction_beneficial < 0: raise ValueError('fraction beneficial must be >= 0 and < 1') self.fraction_accurate = fraction_accurate if self.fraction_accurate >= 1 or self.fraction_accurate < 0: raise ValueError('fraction accurate must be >=0 and < 1') self.fraction_mu2mu = fraction_mu2mu if self.fraction_mu2mu >= 1 or self.fraction_mu2mu < 0: raise ValueError('fraction_mu2mu must be >=0 and < 1') self.pop_cap = K if self.pop_cap < 100: raise ValueError('pop_cap must be greater than or equal to 100') f_min = f_max - delta_fitness*(pop_shape[0]-1) self.fitness_list = np.transpose(np.atleast_2d(np.linspace(f_max, f_min, pop_shape[0]))) self.mutation_list = np.geomspace(mu_min, mu_min*mu_multiple**(pop_shape[1]-1), pop_shape[1])
def __init__( self, start_center_hz, stop_center_hz, bandwidth_ratio, n_bands, always_even=False): self.__bands = [ FrequencyBand.from_center(cf, cf * bandwidth_ratio) for cf in np.geomspace(start_center_hz, stop_center_hz, num=n_bands) ] band = FrequencyBand(self.__bands[0].start_hz, self.__bands[-1].stop_hz) super(GeometricScale, self).__init__( band, n_bands, always_even=always_even) self.start_center_hz = start_center_hz self.stop_center_hz = stop_center_hz self.bandwidth_ratio = bandwidth_ratio
def test_array_scalar(self): lim1 = array([120, 100], dtype="int8") lim2 = array([-120, -100], dtype="int8") lim3 = array([1200, 1000], dtype="uint16") t1 = geomspace(lim1[0], lim1[1], 5) t2 = geomspace(lim2[0], lim2[1], 5) t3 = geomspace(lim3[0], lim3[1], 5) t4 = geomspace(120.0, 100.0, 5) t5 = geomspace(-120.0, -100.0, 5) t6 = geomspace(1200.0, 1000.0, 5) # t3 uses float32, t6 uses float64 assert_allclose(t1, t4, rtol=1e-2) assert_allclose(t2, t5, rtol=1e-2) assert_allclose(t3, t6, rtol=1e-5)
def overview(self): """ Plots an overview figure. """ is_nm = self.freq_unit is "nm" if is_nm: ph.vis_mode() else: ph.ir_mode() ds = self.dataset x = ds.wavelengths if is_nm else ds.wavenumbers fig, axs = plt.subplots( 3, 1, figsize=(5, 12), gridspec_kw=dict(height_ratios=(2, 1, 1)) ) self.map(ax=axs[0]) times = np.hstack((0, np.geomspace(0.1, ds.t.max(), 6))) sp = self.spec(times, ax=axs[1]) freqs = np.unique(np.linspace(x.min(), x.max(), 6)) tr = self.trans(freqs, ax=axs[2]) OverviewPlot = namedtuple("OverviewPlot", "fig axs trans spec") return OverviewPlot(fig, axs, tr, sp)
def user_bkg_spec_b(): """Create "user-background" data for testing. `expand_to_2d` uses `np.interp` for interpolation, and the wavelength array that is passed to `np.interp` must be increasing. `expand_to_2d` is supposed to handle the case that the wavelengths are decreasing. Create data for checking that the results are the same even if the wavelength array in `m_bkg_spec` is reversed so that the values are decreasing, and the corresponding flux array is also reversed to retain the original (wavelength, flux) relation. Returns ------- m_bkg_spec : `~jwst.datamodels.MultiSpecModel` """ # This data type is used for creating a MultiSpecModel. spec_dtype = datamodels.SpecModel().spec_table.dtype m_bkg_spec = datamodels.MultiSpecModel() wavelength = np.geomspace(1.5, 4.5, num=25, endpoint=True, dtype=np.float64)[::-1] flux = np.linspace(13., 25., num=25, endpoint=True, retstep=False, dtype=np.float64)[::-1] fl_error = np.ones_like(wavelength) dq = np.zeros(wavelength.shape, dtype=np.uint32) net = np.zeros_like(wavelength) nerror = np.ones_like(wavelength) background = np.ones_like(wavelength) berror = np.ones_like(wavelength) npixels = np.ones_like(wavelength) otab = np.array(list(zip(wavelength, flux, fl_error, dq, net, nerror, background, berror, npixels)), dtype=spec_dtype) spec = datamodels.SpecModel(spec_table=otab) m_bkg_spec.spec.append(spec) return m_bkg_spec
def test_basic(self): y = geomspace(1, 1e6) assert_(len(y) == 50) y = geomspace(1, 1e6, num=100) assert_(y[-1] == 10 ** 6) y = geomspace(1, 1e6, endpoint=False) assert_(y[-1] < 10 ** 6) y = geomspace(1, 1e6, num=7) assert_array_equal(y, [1, 10, 100, 1e3, 1e4, 1e5, 1e6]) y = geomspace(8, 2, num=3) assert_allclose(y, [8, 4, 2]) assert_array_equal(y.imag, 0) y = geomspace(-1, -100, num=3) assert_array_equal(y, [-1, -10, -100]) assert_array_equal(y.imag, 0) y = geomspace(-100, -1, num=3) assert_array_equal(y, [-100, -10, -1]) assert_array_equal(y.imag, 0)
def __init__(self, f_max, mu_min, population_distribution, delta_fitness, mu_multiple, fraction_beneficial, fraction_accurate, fraction_mu2mu, K): """ The population is described both by its state and the parameters of the model. Population_distribution should be a single number or a numpy array. """ self.population_distribution = \ np.atleast_2d(np.array(population_distribution, dtype='int64')) if np.any(self.population_distribution) < 0: raise ValueError('the population distribution must be nonnegative') pop_shape = self.population_distribution.shape self.delta_fitness = delta_fitness if self.delta_fitness <= 0: raise ValueError('delta fitness must be positive') self.mu_multiple = mu_multiple if self.mu_multiple <= 1: raise ValueError('mu multiple must be greater than one') self.fraction_beneficial = fraction_beneficial if self.fraction_beneficial >= 1 or self.fraction_beneficial < 0: raise ValueError('fraction beneficial must be >= 0 and < 1') self.fraction_accurate = fraction_accurate if self.fraction_accurate >= 1 or self.fraction_accurate < 0: raise ValueError('fraction accurate must be >=0 and < 1') self.fraction_mu2mu = fraction_mu2mu if self.fraction_mu2mu >= 1 or self.fraction_mu2mu < 0: raise ValueError('fraction_mu2mu must be >=0 and < 1') self.pmus = \ np.array([fraction_mu2mu*fraction_accurate, fraction_mu2mu*(1-fraction_accurate), (1-fraction_mu2mu)*fraction_beneficial, (1-fraction_mu2mu)*(1-fraction_beneficial)]).reshape(4, 1, 1) self.pop_cap = K if self.pop_cap < 100: raise ValueError('pop_cap must be greater than or equal to 100') if K <= 10**9: self.wright_fisher = wf.wright_fisher_fitness_update self.multinomial = arrm.array_multinomial else: self.wright_fisher = wf.wright_fisher_fitness_update_bigN self.multinomial = arrm.array_multinomial_int64 f_min = f_max - delta_fitness*(pop_shape[0]-1) self.fitness_list = np.transpose(np.atleast_2d(np.linspace(f_max, f_min, pop_shape[0]))) self.mutation_list = np.minimum(np.geomspace(mu_min, mu_min*mu_multiple**(pop_shape[1]-1), pop_shape[1]),1) if self.mutation_list.size == 1: if self.mutation_list[0] > 1.0: raise ValueError('Your population distribution implies mutation' ' rates exceeding one. This is not possible in' ' this model.') #else: # if self.mutation_list[-2] >= 1.0: # raise ValueError('Your population distribution implies mutation' # ' rates exceeding one. This is not possible in' # ' this model.') self.stencil = np.array([[0, -1], [0, 1], [-1, 0], [1, 0], [0, 0]]) self.summer = stsum.fixedStencilSum(3, 0, (5,), self.stencil)
#cosmo2.props.xb = 4.8e37 cosmo2.props.xb = 1.0e37 cosmo2.props.OmegaL = 1.0e0 cosmo2.props.Omegac = 1.0 cosmo2.props.H0 = 67.8 tau_min = max (cosmo1.tau_min (), cosmo1.tau_xc (1.0e-5)) tau_max = min (cosmo1.tau_max (), -cosmo1.tau_xc (1.0e-5)) k = 1.0e0 xb = cosmo1.xbe () print ("# tau interval: (% 22.15g, % 22.15g)" % (tau_min, tau_max)) Nn = 100000 LS = 1.0e-30 tau_a = np.concatenate ((np.geomspace (tau_min, -LS, Nn/2), np.geomspace (LS, tau_max, Nn/2)), axis = 0) alpha_a = [] nu1_a = [] mnu1_a = [] mnu_gw1_a = [] dlnmnu1_a = [] dlnmnu_gw1_a = [] Eaa01_a = [] x1_a = [] y1_a = [] nu2_a = [] mnu2_a = [] mnu_gw2_a = []
dictionary_type="Toeplitz", n_samples=n_samples, n_features=n_features, n_times=1, n_active=n_active, rho=rho, SNR=SNR, seed=2) X_test_s = csc_matrix(X_test) alpha_max = (X_train.T @ y_train).max() / n_samples p_alpha = 0.7 alpha = p_alpha * alpha_max log_alpha = np.log(alpha) log_alphas = np.log(alpha_max * np.geomspace(1, 0.1)) tol = 1e-16 dict_log_alpha = {} dict_log_alpha["lasso"] = log_alpha tab = np.linspace(1, 1000, n_features) dict_log_alpha["wlasso"] = log_alpha + np.log(tab / tab.max()) models = [Lasso(X_train, y_train, dict_log_alpha["lasso"])] def test_cross_val_criterion(): alpha_min = alpha_max / 10 log_alpha_max = np.log(alpha_max) log_alpha_min = np.log(alpha_min) max_iter = 10000
def add_heatmap(ax, x, y, c, xinterp=None, yinterp=None, kind='linear', xscale='linear', yscale='linear', cscale='linear', xmin=None, xmax=None, ymin=None, ymax=None, cmin=None, cmax=None, colour='viridis', **kwargs): """Adds a heatmap to a set of axes. Formats limits, parses extra colourmap options, makes sure data isn't obscured. Arguments --------- ax : axes axes to plot on. x : array-like x data. y : array-like y data. c : array-like (shape: x, y) colour data. xinterp : int, optional density of interpolation. None turns it off. Default: None. yinterp : int, optional density of interpolation. None turns it off. Default: None. kind : str, optional interpolation kind. Default: linear. xscale : str, optional x scale (linear or log). Default: linear. yscale : str, optional y scale (linear or log). Default: linear cscale : str, optional colour scale (linear or log). Default: linear. xmin : float, optional override x minimum. Default: None. xmax : float, optional override x maximum. Default: None. ymin : float, optional override y minimum. Default: None. ymax : float, optional override y maximum. Default: None. cmin : float, optional override colour scale minimum. Default: None. cmax : float, optional override colour scale maximum. Default: None. colour : colourmap or str or array-like, optional colourmap or colourmap name; or key colour or min and max RGB colours to generate a colour map. Default: viridis. **kwargs keyword arguments passed to matplotlib.pyplot.pcolormesh. Defaults: rasterized: False Returns ------- colourbar colourbar. """ # defaults defkwargs = {'rasterized': False} for key in defkwargs: if key not in kwargs: kwargs[key] = defkwargs[key] # data trimming x = np.array(x) y = np.array(y) c = np.array(c) if xmin is None: xmin = x[0] if xmax is None: xmax = x[-1] if ymin is None: ymin = y[0] if ymax is None: ymax = y[-1] xi = np.where((x >= xmin) & (x <= xmax))[0] yi = np.where((y >= ymin) & (y <= ymax))[0] x = x[xi] y = y[yi] try: c = c[np.ix_(xi, yi)] except IndexError: c = c[np.ix_(xi[:-1], yi[:-1])] # colour extend = 'neither' if cmin is None: cmin = np.amin(c) elif cmin > np.amin(c): extend = 'min' if cmax is None: cmax = np.amax(c) elif cmax < np.amax(c): if extend == 'min': extend = 'both' else: extend = 'max' if cscale == 'linear': cnorm = mpl.colors.Normalize(vmin=cmin, vmax=cmax) elif cscale == 'log': cnorm = mpl.colors.LogNorm(vmin=cmin, vmax=cmax) try: colours = mpl.cm.get_cmap(colour) except Exception: if isinstance(colour, mpl.colors.ListedColormap): colours = colour else: try: colours = tp.plot.colour.uniform(colour) except Exception: colours = tp.plot.colour.linear(colour[1], colour[0]) # data interpolation if xinterp is not None or yinterp is not None: cinterp = interp2d(x, y, np.transpose(c), kind=kind) if xinterp is not None: if xscale == 'linear': x = np.linspace(x[0], x[-1], xinterp) if xscale == 'log': x = np.geomspace(x[0], x[-1], xinterp) if yinterp is not None: if yscale == 'linear': y = np.linspace(y[0], y[-1], yinterp) if yscale == 'log': y = np.geomspace(y[0], y[-1], yinterp) c = cinterp(x, y) # ensure all data is shown if len(x) == len(c[0]): x = list(x) if xscale == 'linear': x.append(2 * x[-1] - x[-2]) elif xscale == 'log': x.append(x[-1]**2 / x[-2]) if len(y) == len(c): y = list(y) if yscale == 'linear': y.append(2 * y[-1] - y[-2]) elif yscale == 'log': y.append(y[-1]**2 / y[-2]) # plotting heat = ax.pcolormesh(x, y, c, cmap=colours, norm=cnorm, **kwargs) cbar = plt.colorbar(heat, extend=extend) # axes formatting tp.plot.utilities.set_locators(ax, x=xscale, y=yscale) tp.plot.utilities.set_locators(cbar.ax, y=cscale) ax.set_xlim(x[0], x[-1]) ax.set_ylim(y[0], y[-1]) return cbar
def main(): logging.basicConfig(level=logging.INFO) logging.getLogger("pyirf").setLevel(logging.DEBUG) for particle_type, p in particles.items(): log.info(f"Simulated {particle_type.title()} Events:") p["events"], p["simulation_info"] = read_eventdisplay_fits(p["file"]) p["events"]["particle_type"] = particle_type p["simulated_spectrum"] = PowerLaw.from_simulation( p["simulation_info"], T_OBS) p["events"]["weight"] = calculate_event_weights( p["events"]["true_energy"], p["target_spectrum"], p["simulated_spectrum"]) for prefix in ('true', 'reco'): k = f"{prefix}_source_fov_offset" p["events"][k] = calculate_source_fov_offset(p["events"], prefix=prefix) # calculate theta / distance between reco and assuemd source positoin # we handle only ON observations here, so the assumed source pos # is the pointing position p["events"]["theta"] = calculate_theta( p["events"], assumed_source_az=p["events"]["pointing_az"], assumed_source_alt=p["events"]["pointing_alt"], ) log.info(p["simulation_info"]) log.info("") gammas = particles["gamma"]["events"] # background table composed of both electrons and protons background = table.vstack( [particles["proton"]["events"], particles["electron"]["events"]]) INITIAL_GH_CUT = np.quantile(gammas['gh_score'], (1 - INITIAL_GH_CUT_EFFICENCY)) log.info( f"Using fixed G/H cut of {INITIAL_GH_CUT} to calculate theta cuts") # event display uses much finer bins for the theta cut than # for the sensitivity theta_bins = add_overflow_bins( create_bins_per_decade( 10**(-1.9) * u.TeV, 10**2.3005 * u.TeV, 50, )) # theta cut is 68 percent containmente of the gammas # for now with a fixed global, unoptimized score cut mask_theta_cuts = gammas["gh_score"] >= INITIAL_GH_CUT theta_cuts = calculate_percentile_cut( gammas["theta"][mask_theta_cuts], gammas["reco_energy"][mask_theta_cuts], bins=theta_bins, min_value=0.05 * u.deg, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, percentile=68, ) # same bins as event display uses sensitivity_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, bins_per_decade=5)) log.info("Optimizing G/H separation cut for best sensitivity") gh_cut_efficiencies = np.arange( GH_CUT_EFFICIENCY_STEP, MAX_GH_CUT_EFFICIENCY + GH_CUT_EFFICIENCY_STEP / 2, GH_CUT_EFFICIENCY_STEP) sensitivity_step_2, gh_cuts = optimize_gh_cut( gammas, background, reco_energy_bins=sensitivity_bins, gh_cut_efficiencies=gh_cut_efficiencies, op=operator.ge, theta_cuts=theta_cuts, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) # now that we have the optimized gh cuts, we recalculate the theta # cut as 68 percent containment on the events surviving these cuts. log.info('Recalculating theta cut for optimized GH Cuts') for tab in (gammas, background): tab["selected_gh"] = evaluate_binned_cut(tab["gh_score"], tab["reco_energy"], gh_cuts, operator.ge) theta_cuts_opt = calculate_percentile_cut( gammas[gammas['selected_gh']]["theta"], gammas[gammas['selected_gh']]["reco_energy"], theta_bins, percentile=68, fill_value=0.32 * u.deg, max_value=0.32 * u.deg, min_value=0.05 * u.deg, ) gammas["selected_theta"] = evaluate_binned_cut(gammas["theta"], gammas["reco_energy"], theta_cuts_opt, operator.le) gammas["selected"] = gammas["selected_theta"] & gammas["selected_gh"] # calculate sensitivity signal_hist = create_histogram_table(gammas[gammas["selected"]], bins=sensitivity_bins) background_hist = estimate_background( background[background["selected_gh"]], reco_energy_bins=sensitivity_bins, theta_cuts=theta_cuts_opt, alpha=ALPHA, background_radius=MAX_BG_RADIUS, ) sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA) # scale relative sensitivity by Crab flux to get the flux sensitivity spectrum = particles['gamma']['target_spectrum'] for s in (sensitivity_step_2, sensitivity): s["flux_sensitivity"] = (s["relative_sensitivity"] * spectrum(s['reco_energy_center'])) log.info('Calculating IRFs') hdus = [ fits.PrimaryHDU(), fits.BinTableHDU(sensitivity, name="SENSITIVITY"), fits.BinTableHDU(sensitivity_step_2, name="SENSITIVITY_STEP_2"), fits.BinTableHDU(theta_cuts, name="THETA_CUTS"), fits.BinTableHDU(theta_cuts_opt, name="THETA_CUTS_OPT"), fits.BinTableHDU(gh_cuts, name="GH_CUTS"), ] masks = { "": gammas["selected"], "_NO_CUTS": slice(None), "_ONLY_GH": gammas["selected_gh"], "_ONLY_THETA": gammas["selected_theta"], } # binnings for the irfs true_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 10)) reco_energy_bins = add_overflow_bins( create_bins_per_decade(10**-1.9 * u.TeV, 10**2.31 * u.TeV, 5)) fov_offset_bins = [0, 0.5] * u.deg source_offset_bins = np.arange(0, 1 + 1e-4, 1e-3) * u.deg energy_migration_bins = np.geomspace(0.2, 5, 200) for label, mask in masks.items(): effective_area = effective_area_per_energy( gammas[mask], particles["gamma"]["simulation_info"], true_energy_bins=true_energy_bins, ) hdus.append( create_aeff2d_hdu( effective_area[..., np.newaxis], # add one dimension for FOV offset true_energy_bins, fov_offset_bins, extname="EFFECTIVE_AREA" + label, )) edisp = energy_dispersion( gammas[mask], true_energy_bins=true_energy_bins, fov_offset_bins=fov_offset_bins, migration_bins=energy_migration_bins, ) hdus.append( create_energy_dispersion_hdu( edisp, true_energy_bins=true_energy_bins, migration_bins=energy_migration_bins, fov_offset_bins=fov_offset_bins, extname="ENERGY_DISPERSION" + label, )) bias_resolution = energy_bias_resolution(gammas[gammas["selected"]], reco_energy_bins, energy_type="reco") ang_res = angular_resolution(gammas[gammas["selected_gh"]], reco_energy_bins, energy_type="reco") psf = psf_table( gammas[gammas["selected_gh"]], true_energy_bins, fov_offset_bins=fov_offset_bins, source_offset_bins=source_offset_bins, ) background_rate = background_2d( background[background['selected_gh']], reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, t_obs=T_OBS, ) hdus.append( create_background_2d_hdu( background_rate, reco_energy_bins, fov_offset_bins=np.arange(0, 11) * u.deg, )) hdus.append( create_psf_table_hdu( psf, true_energy_bins, source_offset_bins, fov_offset_bins, )) hdus.append( create_rad_max_hdu(theta_cuts_opt["cut"][:, np.newaxis], theta_bins, fov_offset_bins)) hdus.append(fits.BinTableHDU(ang_res, name="ANGULAR_RESOLUTION")) hdus.append( fits.BinTableHDU(bias_resolution, name="ENERGY_BIAS_RESOLUTION")) log.info('Writing outputfile') fits.HDUList(hdus).writeto("pyirf_eventdisplay.fits.gz", overwrite=True)
def guess_alphas(self): return numpy.geomspace(0.001, 1000, num=1000)
df2["r0"] = np.round(b / gamma, 2) df_list.append(df2) df = pd.concat(df_list) g = sns.relplot(data=df, x="time", y="value", hue="species", col="r0", kind="line", facet_kws={"sharey": False}) g.set(xlim=[0, 50]) #plt.show() #g.savefig("../figures/antigen_effects/inf_good_range.pdf") # run ode and plot lognorm fit params for different betas beta_arr = np.geomspace(gamma, 10 * gamma, 100) r0_arr = beta_arr / gamma df_list = [] for b in beta_arr: df1, df2 = run_pipeline(y0, t, N, b, gamma) df_list.append(df2) df = pd.concat(df_list) df = df[[ "beta", "SD_lognorm", "mean_lognorm", "CV", "fit_error", "lognorm_shape", "lognorm_scale" ]] df = df.drop_duplicates() df = df.melt(id_vars="beta") df["r0"] = df.beta / gamma
def partition_corpus(min_examples, max_examples, n_partition=5): return np.geomspace(min_examples, max_examples, num=n_partition)
args = parser.parse_args() n_genes = args.n_genes n_drugs = args.n_drugs n_se_combo = args.combo n_se_mono = args.mono # ============================================================================================= # # DATA GENERATION # Adjacency matrix for PPI network b = 10 * np.random.randn(n_genes, n_genes) ppi_adj = sp.csr_matrix(((b + b.T)/2 > 20).astype(int)) ppi_degrees = np.array(ppi_adj.sum(axis=0)).squeeze() # Adjacency matrix for DTI network dti_adj = sp.csr_matrix((10 * np.random.randn(n_genes, n_drugs) > 29).astype(int)) # DDI adjacency matrices t = n_se_combo thresh = np.geomspace(8,20,t) def se_adj_matrix(i): b = 10 * np.random.randn(n_drugs, n_drugs) mat = sp.csr_matrix(((b + b.T)/2 > i).astype(int)) return mat ddi_adj_list = Parallel(n_jobs=16)\ (delayed(se_adj_matrix)(d) for d in thresh[:n_se_combo]) ddi_degrees_list = [np.array(drug_adj.sum(axis=0)).squeeze() for drug_adj in ddi_adj_list] # Drug feature matrix drug_feat = sp.csr_matrix((10 * np.random.randn(n_drugs, n_se_mono) > 19).astype(int)) print('\nAdjacency a feature matrices generated\n') # ============================================================================================= # # CONTROL PRINTING # Interactions (edges) print('==== DATA GENERATED ====') print('Interactions (edges)')
import numpy as np "Input parameters" path = argv[1] # directory path where the traces data files are "Variables" line = 0 # line in data files from which start reading the data xmin = 0.001 # minimum x-axis value to plot xmax = 5.1 # maximum x-axis value to plot cmin = 0 # minimum number of counts cmax = 1000 # maximum number of counts bins = 1000 # number of bins in x and y os.chdir(path) list_dat = [file for file in sorted(glob.glob("*.dat"))] logbins = np.geomspace(xmin, xmax, bins) fig = plt.figure(figsize=(16, 9)) axs = fig.subplots(nrows=1, ncols=3) def column(array, column): """Returns column n from file lines in array""" c = [float(i) for i in [(line.split()[column]) for line in array]] return c def animate(i): with open(list_dat[i]) as g: glines = g.readlines()[line:] x = column(glines, 0) y = column(glines, 1)
import numpy as np import matplotlib.pyplot as plt import exopy def nk_analytic(t, k: int, A: float, initial: float) -> float: f = 1.0 / (1 + A * t * initial / 2) nk = initial * f**2 * (1 - f)**(k - 1) return nk t = np.geomspace(start=0.0001, stop=100.0, num=1000) nk = exopy.compute_nk_approx(steps=t, k_max=200, initial=200.0, A=0.001) n_1 = nk[:, 1 - 1] n_5 = nk[:, 5 - 1] n_10 = nk[:, 10 - 1] n_50 = nk[:, 50 - 1] fig = plt.figure() ax = plt.subplot(111) plt.plot(t, n_1, 'g-', label='Numeric k=1') plt.plot(t, nk_analytic(t, 1, A=0.001, initial=200.0), 'k-', label='Analytic k=1') plt.plot(t, n_5, 'g-.', label='Numeric k=5') plt.plot(t, nk_analytic(t, 5, A=0.001, initial=200.0), 'k-.', label='Analytic k=5')
def plot_ts_profiles( self, ax=None, energy_unit="TeV", add_cbar=True, y_values=None, y_unit=None, sed_type="dnde", **kwargs, ): """Plot fit statistic SED profiles as a density plot. Parameters ---------- ax : `~matplotlib.axes.Axes` Axis object to plot on. energy_unit : str, `~astropy.units.Unit`, optional Unit of the energy axis add_cbar : bool Whether to add a colorbar to the plot. y_values : `astropy.units.Quantity` Array of y-values to use for the fit statistic profile evaluation. y_unit : str or `astropy.units.Unit` Unit to use for the y-axis. sed_type : {"dnde", "flux", "eflux", "e2dnde"} Sed type kwargs : dict Keyword arguments passed to :func:`matplotlib.pyplot.pcolormesh` Returns ------- ax : `~matplotlib.axes.Axes` Axis object """ import matplotlib.pyplot as plt if ax is None: ax = plt.gca() self._validate_data(self.table, sed_type="likelihood", check_scan=True) y_unit = u.Unit(y_unit or DEFAULT_UNIT[sed_type]) if y_values is None: ref_values = getattr(self, sed_type + "_ref") y_values = np.geomspace(0.2 * ref_values.value.min(), 5 * ref_values.value.max(), 500) y_values = u.Quantity(y_values, y_unit, copy=False) x = self.energy_axis.edges.to(energy_unit) # Compute fit statistic "image" one energy bin at a time # by interpolating e2dnde at the log bin centers z = np.empty((len(self.norm), len(y_values))) for idx, row in enumerate(self.table): y_ref = getattr(self, sed_type + "_ref")[idx] norm = (y_values / y_ref).to_value("") norm_scan = row["norm_scan"] ts_scan = row["stat_scan"] - row["stat"] interp = interpolate_profile(norm_scan, ts_scan) z[idx] = interp((norm, )) kwargs.setdefault("vmax", 0) kwargs.setdefault("vmin", -4) kwargs.setdefault("zorder", 0) kwargs.setdefault("cmap", "Blues") kwargs.setdefault("linewidths", 0) # clipped values are set to NaN so that they appear white on the plot z[-z < kwargs["vmin"]] = np.nan caxes = ax.pcolormesh(x.value, y_values.value, -z.T, **kwargs) ax.set_xscale("log", nonpositive="clip") ax.set_yscale("log", nonpositive="clip") ax.set_xlabel(f"Energy ({energy_unit})") ax.set_ylabel(f"{sed_type} ({y_values.unit})") if add_cbar: label = "fit statistic difference" ax.figure.colorbar(caxes, ax=ax, label=label) return ax
u[i] = method(lambda u: f(u, cfg["params"]), u[i - 1], deltaT) if any(u[i] < cfg["min"]) or any(u[i] > cfg["max"]): break # Crop array if necessary and return t = t[:i + 1] u = u[:i + 1] return (t, *[u[:, i] for i in range(len(u0))]) if __name__ == "__main__": # Exercise 1 # a) plt.close() for deltaT in np.geomspace(0.0001, 1, 5): steps = int(100 / deltaT) _, GraphU1, GraphU2 = generate([0, 0], 0, deltaT, explicitEuler, f1, steps=steps) plt.plot(GraphU1, GraphU2, label=r"$\Delta\tau=$" + str(deltaT), linewidth=0.3) plt.legend(loc="best") plt.savefig("sheet01_ex1a.png", dpi=300) # b) Square
def gabor_filter_frequency_domain(f, M, N, plot=False): n_rotation = 8 n_scale = 8 theta = np.linspace(0.0, np.pi, n_rotation) omega = np.geomspace(0.1, 0.9, n_scale) params = [(t, o) for o in omega for t in theta] filterBank = [] fftFilterBank = [] gaborParams = [] gaborFeaturesBank = [] for (theta, omega) in params: gaborParam = {'omega': omega, 'theta': theta, 'sz': (N, M)} gabor = genGabor(func=np.cos, **gaborParam) filterBank.append(gabor) fftFilterBank.append(fftn(gabor)) gaborParams.append(gaborParam) #Imprime os filtros de Gabor Gerados no Dominio Espacial if plot == True: plt.figure() plt.suptitle("Filtros de Gabor no Dominio Espacial", fontsize=16) n = len(filterBank) for i in range(n): plt.subplot(n_scale, n_rotation, i + 1) plt.axis('off') plt.imshow(filterBank[i], cmap='gray') #Imprime os filtros de Gabor Gerados e convertidos para o Dominio de Frequencia if plot == True: plt.figure() plt.suptitle("Filtros de Gabor no Dominio de Frequencia", fontsize=16) n = len(fftFilterBank) for i in range(n): im_fft = fftshift(fftFilterBank[i]) plt.subplot(n_scale, n_rotation, i + 1) plt.axis('off') plt.imshow(np.log(np.abs(im_fft) + 1), cmap='gray') n = len(fftFilterBank) out = np.zeros([M, N]).astype(np.float64) for i in range(n): G = fftFilterBank[i] F = fftn(f) C = np.multiply(F, G) f_rest = np.real(fftshift(ifftn(C))) #De repente converter f_rest para o range -1 a 1 e aplicar a funcao x^3 ou sigmoide (Talvez apareca apenas os valores das transicoes detectados) out += f_rest gaborFeaturesBank.append(f_rest) #show_image(f_rest, out) #Imprime a imagem de entrada filtrada com os filtros de Gabor Gerados if plot == True: plt.figure() plt.suptitle("Mapa de Features da Filtragem de Gabor", fontsize=16) n = len(gaborFeaturesBank) for i in range(n): im_gabor = gaborFeaturesBank[i] plt.subplot(n_scale, n_rotation, i + 1) plt.axis('off') plt.imshow(im_gabor, cmap='gray') out = normalize_values(out) return out
def test_complex(self): # Purely imaginary y = geomspace(1j, 16j, num=5) assert_allclose(y, [1j, 2j, 4j, 8j, 16j]) assert_array_equal(y.real, 0) y = geomspace(-4j, -324j, num=5) assert_allclose(y, [-4j, -12j, -36j, -108j, -324j]) assert_array_equal(y.real, 0) y = geomspace(1+1j, 1000+1000j, num=4) assert_allclose(y, [1+1j, 10+10j, 100+100j, 1000+1000j]) y = geomspace(-1+1j, -1000+1000j, num=4) assert_allclose(y, [-1+1j, -10+10j, -100+100j, -1000+1000j]) # Logarithmic spirals y = geomspace(-1, 1, num=3, dtype=complex) assert_allclose(y, [-1, 1j, +1]) y = geomspace(0+3j, -3+0j, 3) assert_allclose(y, [0+3j, -3/sqrt(2)+3j/sqrt(2), -3+0j]) y = geomspace(0+3j, 3+0j, 3) assert_allclose(y, [0+3j, 3/sqrt(2)+3j/sqrt(2), 3+0j]) y = geomspace(-3+0j, 0-3j, 3) assert_allclose(y, [-3+0j, -3/sqrt(2)-3j/sqrt(2), 0-3j]) y = geomspace(0+3j, -3+0j, 3) assert_allclose(y, [0+3j, -3/sqrt(2)+3j/sqrt(2), -3+0j]) y = geomspace(-2-3j, 5+7j, 7) assert_allclose(y, [-2-3j, -0.29058977-4.15771027j, 2.08885354-4.34146838j, 4.58345529-3.16355218j, 6.41401745-0.55233457j, 6.75707386+3.11795092j, 5+7j]) # Type promotion should prevent the -5 from becoming a NaN y = geomspace(3j, -5, 2) assert_allclose(y, [3j, -5]) y = geomspace(-5, 3j, 2) assert_allclose(y, [-5, 3j])
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D import matplotlib.animation import pandas as pd from pymaniprob.sphere import VonMisesFisher N = 10 a = np.random.rand(2000, 3)*10 t = np.array([np.ones(100)*i for i in range(20)]).flatten() df = pd.DataFrame({"time": t ,"x" : a[:,0], "y" : a[:,1], "z" : a[:,2]}) #kk = np.linspace(0.1, 100.1, 20) kk = np.geomspace(1., 100, 10, endpoint=True) rvs = [VonMisesFisher.rvs(p=3, size=100, k=k) for k in kk] def update_graph(num): # data=df[df['time']==num] data = rvs[num] graph._offsets3d = (data[:, 0], data[:, 1], data[:, 2]) # graph._offsets3d = (data.x, data.y, data.z) title.set_text(r'$\kappa={:0.2g}$'.format(kk[num])) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') title = ax.set_title('3D Test') ax = fig.gca(projection='3d') ax.set_aspect("equal")
x=xlist[i], y=ylist[i], ang=anglist[i], linestyle=lines[i], labeltext=labellist[i]) #Plotting stuff plt.axhspan(1, 1.5, facecolor='grey', alpha=0.5) plt.ylim(1e-3, 1.5) xmin = 1e-12 xmax = 1e4 plt.xlim(xmin, xmax) ticks_minor = np.geomspace(1e-18, 1e4, 23) ticks_minor = ticks_minor[(xmin <= ticks_minor) & (ticks_minor <= xmax)] #print(ticks_minor) ax.set_xticks(ticks_minor, minor=True) ax.set_xticklabels([], minor=True) ax.set_xlabel(r'$M_\mathrm{CO}$ [$M_\odot$]') plt.ylabel(r'$f_\mathrm{CO} = \Omega_\mathrm{CO}/\Omega_\mathrm{DM}$') ax_top = ax.twiny() ax_top.xaxis.tick_top() ax_top.set_xscale('log') ax_top.set_xlim(ax.get_xlim()) ax_top.set_xlabel(r'$M_\mathrm{CO}$ [g]', labelpad=7) g_to_Msun = 1 / 1.989e+33
Z = np.zeros((theta0.shape[0], theta1.shape[0])) # Calculate Z-values (Cost) based on grid of coefficients for (i, j), v in np.ndenumerate(Z): t = np.array([[theta0[i, j], theta1[i, j]]]).T Z[i, j] = computeCost(X, y, t) fig = plt.figure(figsize=(15, 6)) ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122, projection='3d') # Left plot CS = ax1.contour(theta0, theta1, Z, np.geomspace(Z.min(), Z.max(), 10), cmap=plt.cm.jet, color='black') plt.clabel(CS, inline=1, fontsize=10) ax1.scatter(theta_history[0, :], theta_history[1, :], c='r') ax1.grid() # Right plot ax2.plot_surface(theta0, theta1, Z, rstride=1, cstride=1, alpha=0.6, cmap=plt.cm.jet, linewidth=0,
from itertools import product import numpy as np import time dstamp = time.strftime('%Y%m%d') tstamp = time.strftime('%H%M%S') jobname = 'lorentz_mlp_encoding_%s_%s' % (dstamp, tstamp) jobfile = 'Batches/%s.job' % jobname lam_grid = np.append(np.geomspace(1.0, 0.01, num=50), 0) seed_grid = [0] hidden_grid = [10] network_lag_grid = [5] nepoch_grid = [30000] lr_grid = [0.01] wd_grid = [0.01] FC_grid = [10, 40] sd_grid = [2.0] dt_grid = [0.05] p_grid = [10, 20, 30] T_grid = [500, 750, 1000] data_seed_grid = [0, 1, 2, 3, 4] BASECMD = 'python lorentz_mlp_encoding.py' param_grid = product(lam_grid, seed_grid, hidden_grid, network_lag_grid, nepoch_grid, lr_grid, wd_grid, FC_grid, sd_grid, dt_grid, p_grid, T_grid, data_seed_grid)
plt.ylim(1e-4, 1.5) plt.xlim(1e-18, 1e4) ax.set_xticks(np.logspace(-18, 4, 23), minor=True) ax.set_xticklabels([], minor=True) ax.set_xlabel(r'$M_\mathrm{PBH}$ [$M_\odot$]') plt.ylabel(r'$f_\mathrm{PBH} = \Omega_\mathrm{PBH}/\Omega_\mathrm{DM}$') ax_top = ax.twiny() ax_top.xaxis.tick_top() ax_top.set_xscale('log') ax_top.set_xlim(ax.get_xlim()) ax_top.set_xlabel(r'$M_\mathrm{PBH}$ [g]', labelpad=7) g_ticks_minor = np.geomspace(1e15, 1e37, 23) g_ticks = g_ticks_minor[::3] g_to_Msun = 1 / 1.989e+33 g_tick_labels = [r"$10^{" + str(int(np.log10(x))) + "}$" for x in g_ticks] ax_top.set_xticks(g_ticks * g_to_Msun) ax_top.set_xticklabels(g_tick_labels) ax_top.xaxis.set_tick_params(pad=0) ax_top.set_xticks(g_ticks_minor * g_to_Msun, minor=True) ax_top.set_xticklabels([], minor=True) plt.savefig(outfile, bbox_inches='tight') plt.show()
def data(self): """ Generates linear or logarithmic sequence of ``n`` numbers. These numbers are middle points of the bins defined by ``n``, ``min_val`` and ``max_val`` parameters. >>> x = MeshAxis(n=10, min_val=0.0, max_val=10.0, name="X", unit="cm", binning=MeshAxis.BinningType.linear) >>> x.data array([ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]) Binning may also consist of one bin: >>> x = MeshAxis(n=1, min_val=0.0, max_val=5.0, name="X", unit="cm", binning=MeshAxis.BinningType.linear) >>> x.data array([ 2.5]) Logarithmic binning works as well, middle bin points are calculated as geometrical mean. Here we define 3 bins: [1,4], [4,16], [16,64]. >>> x = MeshAxis(n=3, min_val=1.0, max_val=64.0, name="X", unit="cm", binning=MeshAxis.BinningType.logarithmic) >>> x.data array([ 2., 8., 32.]) For the same settings as below linear scale gives as expected different sequence: >>> x = MeshAxis(n=3, min_val=1.0, max_val=64.0, name="X", unit="cm", binning=MeshAxis.BinningType.linear) >>> x.data array([ 11.5, 32.5, 53.5]) For logarithmic axis min_val has to be positive: >>> x = MeshAxis(n=3, min_val=-2.0, max_val=64.0, name="X", unit="cm", binning=MeshAxis.BinningType.logarithmic) >>> x.data Traceback (most recent call last): ... Exception: Left edge of first bin (-2) is not positive :return: """ if self.max_val < self.min_val: raise Exception("Right edge of last bin ({:g}) is smaller than left edge of first bin ({:g})".format( self.max_val, self.min_val )) if self.binning == self.BinningType.linear: bin_width = (self.max_val - self.min_val) / self.n first_bin_mid = self.min_val + bin_width / 2.0 # arithmetic mean last_bin_mid = self.max_val - bin_width / 2.0 # arithmetic mean return np.linspace(start=first_bin_mid, stop=last_bin_mid, num=self.n) elif self.binning == self.BinningType.logarithmic: if self.min_val < 0.0: raise Exception("Left edge of first bin ({:g}) is not positive".format(self.min_val)) q = (self.max_val / self.min_val)**(1.0 / self.n) # an = a0 q^n first_bin_mid = self.min_val * q**0.5 # geometrical mean sqrt(a0 a1) = sqrt( a0 a0 q) = a0 sqrt(q) last_bin_mid = self.max_val / q**0.5 # geometrical mean sqrt(an a(n-1)) = sqrt( an an/q) = an / sqrt(q) try: result = np.geomspace(start=first_bin_mid, stop=last_bin_mid, num=self.n) except AttributeError: # Python3.2 require numpy older than 1.2, such versions of numpy doesn't have geomspace function # in such case we calculate geometrical binning manually result = np.exp(np.linspace(start=np.log(first_bin_mid), stop=np.log(last_bin_mid), num=self.n)) return result else: return None
raise RuntimeError('Test files are different.') df = read_data(ds_test_files, ds_predictions, pn_predictions) for subdir in [ 'distributions', 'flavours', 'response', 'resolution', 'residual' ]: try: os.makedirs(os.path.join(args.outdir, subdir)) except FileExistsError: pass plot_distrs(df, os.path.join(args.outdir, 'distributions')) compare_flavours(df, os.path.join(args.outdir, 'flavours')) binning = np.geomspace(30, 3000, 20) bin_centers = np.sqrt(binning[:-1] * binning[1:]) for (ieta, eta_bin), (flavour_label, flavour_ids) in itertools.product( enumerate([(0, 2.5), (2.5, 5)], start=1), [('uds', {1, 2, 3}), ('b', {5}), ('g', {21}), ('all', {0, 1, 2, 3, 4, 5, 21})]): df_bin = df[(np.abs(df.GenJet_eta) >= eta_bin[0]) & (np.abs(df.GenJet_eta) < eta_bin[1]) & df.flavour.isin(flavour_ids)] bins = df_bin.groupby(pd.cut(df_bin.GenJet_pt, binning)) plot_median_response(os.path.join(args.outdir, 'response'), flavour_label, bins, bin_centers, eta_bin, ieta) plot_resolution(os.path.join(args.outdir, 'resolution'), flavour_label,
def sample(self, bqm, beta_range=None, num_reads=None, num_sweeps=None, num_sweeps_per_beta=1, beta_schedule_type="geometric", seed=None, interrupt_function=None, beta_schedule=None, initial_states=None, initial_states_generator="random", **kwargs): """Sample from a binary quadratic model using an implemented sample method. Args: bqm (:class:`dimod.BinaryQuadraticModel`): The binary quadratic model to be sampled. beta_range (tuple or list, optional, default = None): A 2-tuple or list defining the beginning and end of the beta schedule, where beta is the inverse temperature. The schedule is interpolated within this range according to the value specified by ``beta_schedule_type``. Default range is set based on the total bias associated with each node. num_reads (int, optional, default=len(initial_states) or 1): Number of reads. Each read is generated by one run of the simulated annealing algorithm. If `num_reads` is not explicitly given, it is selected to match the number of initial states given. If initial states are not provided, only one read is performed. num_sweeps (int, optional, default=``len(beta_schedule)*num_sweeps_per_beta`` or 1000): Number of sweeps used in annealing. If no value is provided and ``beta_schedule`` is None the value is defaulted to 1000. num_sweeps_per_beta (int, optional, default=1) Number of sweeps to perform at each beta. One sweep consists of a sequential Metropolis update of all spins. beta_schedule_type (string, optional, default="geometric"): Beta schedule type, or how the beta values are interpolated between the given `beta_range`. Supported values are: * "linear" * "geometric" * "custom" "custom" is recommended for high-performance applications, which typically require optimizing beta schedules beyond those of the "linear" and "geometric" options, with bounds beyond those provided by default. ``num_sweeps_per_beta`` and ``beta_schedule`` fully specify a custom schedule. beta_schedule (array-like, optional, default = None) Sequence of beta values swept. Format compatible with numpy.array(beta_schedule,dtype=float) required. Values should be non-negative. seed (int, optional, default = None): Seed to use for the PRNG. Specifying a particular seed with a constant set of parameters produces identical results. If not provided, a random seed is chosen. initial_states (samples-like, optional, default=None): One or more samples, each defining an initial state for all the problem variables. Initial states are given one per read, but if fewer than ``num_reads`` initial states are defined, additional values are generated as specified by ``initial_states_generator``. See func:``.as_samples`` for a description of "samples-like". initial_states_generator (str, "none"/"tile"/"random", optional, default="random"): Defines the expansion of ``initial_states`` if fewer than ``num_reads`` are specified: * "none": If the number of initial states specified is smaller than ``num_reads``, raises ValueError. * "tile": Reuses the specified initial states if fewer than ``num_reads`` or truncates if greater. * "random": Expands the specified initial states with randomly generated states if fewer than ``num_reads`` or truncates if greater. interrupt_function (function, optional): If provided, interrupt_function is called with no parameters between each sample of simulated annealing. If the function returns True, then simulated annealing will terminate and return with all of the samples and energies found so far. Returns: :obj:``dimod.Response``: A ``dimod`` :obj:``~dimod.Response`` object. Examples: This example runs simulated annealing on a binary quadratic model with some different input parameters. >>> import dimod >>> import neal ... >>> sampler = neal.SimulatedAnnealingSampler() >>> bqm = dimod.BinaryQuadraticModel({'a': .5, 'b': -.5}, ... {('a', 'b'): -1}, 0.0, ... dimod.SPIN) >>> # Run with default parameters >>> sampleset = sampler.sample(bqm) >>> # Run with specified parameters >>> sampleset = sampler.sample(bqm, seed=1234, ... beta_range=[0.1, 4.2], ... num_sweeps=20, ... beta_schedule_type='geometric') >>> # Reuse a seed >>> a1 = next((sampler.sample(bqm, seed=88)).samples())['a'] >>> a2 = next((sampler.sample(bqm, seed=88)).samples())['a'] >>> a1 == a2 True """ # get the original vartype so we can return consistently original_vartype = bqm.vartype # convert to spin (if needed) if bqm.vartype is not dimod.SPIN: bqm = bqm.change_vartype(dimod.SPIN, inplace=False) # parse_initial_states could handle seed generation, but because we're # sharing it with the SA algo, we handle it out here if seed is None: seed = randint(2**32) elif not isinstance(seed, Integral): error_msg = "'seed' should be None or an integer between 0 and 2^32 - 1: value =" + str( seed) raise TypeError(error_msg) elif not (0 <= seed < 2**32): error_msg = "'seed' should be an integer between 0 and 2^32 - 1: value =" + str( seed) raise ValueError(error_msg) # parse the inputs parsed = self.parse_initial_states( bqm, num_reads=num_reads, initial_states=initial_states, initial_states_generator=initial_states_generator, seed=seed) num_reads = parsed.num_reads # read out the initial states and the variable order initial_states_array = np.ascontiguousarray( parsed.initial_states.record.sample) variable_order = parsed.initial_states.variables # read out the BQM ldata, (irow, icol, qdata), off = bqm.to_numpy_vectors( variable_order=variable_order, dtype=np.double, index_dtype=np.intc) if interrupt_function and not callable(interrupt_function): raise TypeError("'interrupt_function' should be a callable") if not isinstance(num_sweeps_per_beta, Integral): error_msg = "'num_sweeps_per_beta' should be a positive integer: value =" + str( num_sweeps_per_beta) raise TypeError(error_msg) elif num_sweeps_per_beta < 1: error_msg = "'num_sweeps_per_beta' should be a positive integer: value = " + str( num_sweeps_per_beta) raise ValueError(error_msg) # handle beta_schedule et al if beta_schedule_type == "custom": if beta_schedule is None: error_msg = "'beta_schedule' must be provided for beta_schedule_type = 'custom': value is None" raise ValueError(error_msg) elif num_sweeps is not None and num_sweeps != len( beta_schedule) * num_sweeps_per_beta: error_msg = "'num_sweeps' should be set to None, or a value consistent with 'beta_schedule' and 'num_sweeps_per_beta' for 'beta_schedule_type' = 'custom': value = " + str( num_sweeps) raise ValueError(error_msg) elif beta_range is not None and ( beta_range[0] != beta_schedule[0] or beta_range[-1] != beta_schedule[-1]): error_msg = "'beta_range' should be set to None, or a value consistent with 'beta_schedule', for 'beta_schedule_type'='custom'." raise ValueError(error_msg) elif min(beta_schedule) < 0: error_msg = "'beta_schedule' cannot include negative values." raise ValueError(error_msg) else: if beta_schedule is not None: error_msg = "'beta_schedule' must be set to None for 'beta_schedule_type' not equal to 'custom'" raise ValueError(error_msg) elif num_sweeps is None: num_sweeps = 1000 num_betas, rem = divmod(num_sweeps, num_sweeps_per_beta) if rem > 0 or num_betas < 1: error_msg = "'num_sweeps' must be a positive value divisible by 'num_sweeps_per_beta'." raise ValueError(error_msg) if beta_range is None: beta_range = _default_ising_beta_range(bqm.linear, bqm.quadratic) elif len(beta_range) != 2 or min(beta_range) < 0: error_msg = "'beta_range' should be a 2-tuple, or 2 element list of positive numbers. The latter value is the target value." raise ValueError(error_msg) if num_betas == 1: #One sweep in the target model beta_schedule = np.array([beta_range[-1]], dtype=float) else: if beta_schedule_type == "linear": # interpolate a linear beta schedule beta_schedule = np.linspace(*beta_range, num=num_betas) elif beta_schedule_type == "geometric": if min(beta_range) <= 0: error_msg = "'beta_range' must contain non-zero values for 'beta_schedule_type' = 'geometric'" raise ValueError(error_msg) # interpolate a geometric beta schedule beta_schedule = np.geomspace(*beta_range, num=num_betas) else: raise ValueError( "Beta schedule type {} not implemented".format( beta_schedule_type)) # run the simulated annealing algorithm samples, energies = sa.simulated_annealing( num_reads, ldata, irow, icol, qdata, num_sweeps_per_beta, beta_schedule, seed, initial_states_array, interrupt_function) info = { "beta_range": beta_range, "beta_schedule_type": beta_schedule_type } response = dimod.SampleSet.from_samples( (samples, variable_order), energy=energies + bqm.offset, # add back in the offset info=info, vartype=dimod.SPIN) response.change_vartype(original_vartype, inplace=True) return response
def plot_median_response(outdir, flavour_label, bins, bin_centers, eta_bin, ieta): """Plot median response as a function of pt.""" ref_median = bins.response.median().to_numpy() ref_median_error = np.empty_like(ref_median) for i, (_, df) in enumerate(bins): ref_median_error[i] = bootstrap_median(df.response.to_numpy()) ds_median = bins.ds_response.median().to_numpy() ds_median_error = np.empty_like(ref_median) for i, (_, df) in enumerate(bins): ds_median_error[i] = bootstrap_median(df.ds_response.to_numpy()) pn_median = bins.pn_response.median().to_numpy() pn_median_error = np.empty_like(ref_median) for i, (_, df) in enumerate(bins): pn_median_error[i] = bootstrap_median(df.pn_response.to_numpy()) fig = plt.figure() fig.suptitle('Median ' + flavour_label + '-jet response w.r.t. gen p$_{T}$') ax = fig.add_subplot() vals = np.geomspace(0.5, 50, 20) shift = np.sqrt(vals[:-1] * vals[1:]) ax.errorbar(bin_centers - shift, ref_median, yerr=ref_median_error, ms=3, fmt='o', elinewidth=0.8, label='Standard') ax.errorbar(bin_centers, ds_median, yerr=ds_median_error, ms=3, fmt='^', elinewidth=0.8, label='Deep Sets') ax.errorbar(bin_centers + shift, pn_median, yerr=pn_median_error, ms=3, fmt='s', elinewidth=0.8, label='ParticleNet') ax.axhline(1, ls='dashed', c='gray', alpha=.7) ax.set_xlabel('$p^\\mathrm{{gen}}_{T}$') ax.set_ylabel('Median response') ax.text(1., 1.002, '{}${:g} < |\\eta^\\mathrm{{gen}}| < {:g}$'.format( f'${flavour_label}$, ' if flavour_label != 'all' else '', eta_bin[0], eta_bin[1]), ha='right', va='bottom', transform=ax.transAxes) ax.legend(loc='upper right') ax.tick_params(axis='both', which='both', direction='in', bottom=True, top=True, left=True, right=True) ax.set_xscale('log') fig.savefig(os.path.join(outdir, f'{flavour_label}_eta{ieta}.png')) plt.close(fig)
# Define fiducial parameters and deviations ################################################ LCDM_params = { r"100\theta_\mathrm{s}": 1.04092, r"\Omega_\mathrm{c}h^2": 0.1200, r"\Omega_\mathrm{b}h^2": 0.02237, r"N_\mathrm{eff}": 2.89, r"Y_\mathrm{He}": 0.246, r"\ln(10^{10}As)": 3.044, r"n_\mathrm{s}": 0.9649, r"\tau": 0.0544 } dh = { r"100\theta_\mathrm{s}": np.geomspace(1e-7, 1e-4, 10), r"\Omega_\mathrm{c}h^2": np.geomspace(1e-6, 1e-3, 10), r"\Omega_\mathrm{b}h^2": np.geomspace(1e-7, 1e-4, 10), r"N_\mathrm{eff}": np.geomspace(1e-4, 3e-1, 10), r"Y_\mathrm{He}": np.geomspace(1e-5, 1e-3, 10), r"\ln(10^{10}As)": np.geomspace(1e-5, 1e-2, 10), r"n_\mathrm{s}": np.geomspace(1e-6, 1e-3, 10), r"\tau": np.geomspace(1e-5, 1e-3, 10) } spectra_names = ["C_\ell^{TT}", "C_\ell^{EE}", "C_\ell^{TE}", "C_\ell^{BB}"] params_names = list(LCDM_params.keys()) len_params = len(params_names) ################################################ # Compute range of parameters
def plot_median_residual(outdir, bin_centers, flavour_labels, bins, eta_bin, ieta): """Plot difference in median response between flavours as a function of pt.""" ref_median_1 = bins[0].response.median().to_numpy() ref_median_error_1 = np.empty_like(ref_median_1) for i, (_, df) in enumerate(bins[0]): ref_median_error_1[i] = bootstrap_median(df.response.to_numpy()) ds_median_1 = bins[0].ds_response.median().to_numpy() ds_median_error_1 = np.empty_like(ref_median_1) for i, (_, df) in enumerate(bins[0]): ds_median_error_1[i] = bootstrap_median(df.ds_response.to_numpy()) pn_median_1 = bins[0].pn_response.median().to_numpy() pn_median_error_1 = np.empty_like(ref_median_1) for i, (_, df) in enumerate(bins[0]): pn_median_error_1[i] = bootstrap_median(df.pn_response.to_numpy()) ref_median_2 = bins[1].response.median().to_numpy() ref_median_error_2 = np.empty_like(ref_median_2) for i, (_, df) in enumerate(bins[1]): ref_median_error_2[i] = bootstrap_median(df.response.to_numpy()) ds_median_2 = bins[1].ds_response.median().to_numpy() ds_median_error_2 = np.empty_like(ref_median_2) for i, (_, df) in enumerate(bins[1]): ds_median_error_2[i] = bootstrap_median(df.ds_response.to_numpy()) pn_median_2 = bins[1].pn_response.median().to_numpy() pn_median_error_2 = np.empty_like(ref_median_2) for i, (_, df) in enumerate(bins[1]): pn_median_error_2[i] = bootstrap_median(df.pn_response.to_numpy()) diff = ref_median_1 - ref_median_2 err = np.sqrt(ref_median_error_1**2 + ref_median_error_2**2) ds_diff = ds_median_1 - ds_median_2 ds_err = np.sqrt(ds_median_error_1**2 + ds_median_error_2**2) pn_diff = pn_median_1 - pn_median_2 pn_err = np.sqrt(pn_median_error_1**2 + pn_median_error_2**2) fig = plt.figure() ax = fig.add_subplot() fig.suptitle('Median response residuals w.r.t. gen p$_{T}$') vals = np.geomspace(0.5, 50, 20) shift = np.sqrt(vals[:-1] * vals[1:]) ax.errorbar(bin_centers - shift, diff, yerr=err, ms=3, fmt='o', elinewidth=0.8, label='Standard') ax.errorbar(bin_centers, ds_diff, yerr=ds_err, ms=3, fmt='^', elinewidth=0.8, label='Deep Sets') ax.errorbar(bin_centers + shift, pn_diff, yerr=pn_err, ms=3, fmt='s', lw=0, elinewidth=0.8, label='ParticleNet') ax.axhline(0, ls='dashed', c='gray', alpha=.7) ax.set_xlabel('$p^\\mathrm{{gen}}_{T}$') ax.set_ylabel('$R_{' + flavour_labels[0] + '}-R_{' + flavour_labels[1] + '}$') ax.text(1., 1.002, '${:g} < |\\eta^\\mathrm{{gen}}| < {:g}$'.format( eta_bin[0], eta_bin[1]), ha='right', va='bottom', transform=ax.transAxes) ax.set_xscale('log') ax.legend(loc='upper right') ax.tick_params(axis='both', which='both', direction='in', bottom=True, top=True, left=True, right=True) fig.savefig( os.path.join(outdir, f'{flavour_labels[0]}-{flavour_labels[1]}_eta{ieta}.png')) plt.close(fig)
def generate_fem_file(): # placeholders # coordinates global x, y, z x = np.zeros((num_node, )) y = np.zeros((num_node, )) z = np.zeros((num_node, )) # struct twist structural_twist = np.zeros((num_elem, num_node_elem)) # beam number beam_number = np.zeros((num_elem, ), dtype=int) # frame of reference delta frame_of_reference_delta = np.zeros((num_elem, num_node_elem, 3)) # connectivities conn = np.zeros((num_elem, num_node_elem), dtype=int) # stiffness num_stiffness = 1 ea = 1e4 ga = 1e4 gj = 1e4 eiy = 2e4 eiz = 5e4 sigma = 1 base_stiffness = sigma * np.diag([ea, ga, ga, gj, eiy, eiz]) stiffness = np.zeros((num_stiffness, 6, 6)) stiffness[0, :, :] = main_sigma * base_stiffness elem_stiffness = np.zeros((num_elem, ), dtype=int) # mass num_mass = 1 m_base = 0.75 j_base = 0.1 base_mass = np.diag([m_base, m_base, m_base, j_base, j_base, j_base]) mass = np.zeros((num_mass, 6, 6)) mass[0, :, :] = np.sqrt(main_sigma) * base_mass elem_mass = np.zeros((num_elem, ), dtype=int) # boundary conditions boundary_conditions = np.zeros((num_node, ), dtype=int) boundary_conditions[0] = 1 # applied forces n_app_forces = 0 node_app_forces = np.zeros((n_app_forces, ), dtype=int) app_forces = np.zeros((n_app_forces, 6)) spacing_param = 3 # right wing (beam 0) -------------------------------------------------------------- working_elem = 0 working_node = 0 beam_number[working_elem:working_elem + num_elem_main] = 0 domain = np.linspace(0, 1.0, num_node_main) # 16 - (np.geomspace(20, 4, 10) - 4) x[working_node:working_node + num_node_main] = np.sin(sweep) * ( main_span - (np.geomspace(main_span + spacing_param, 0 + spacing_param, num_node_main) - spacing_param)) y[working_node:working_node + num_node_main] = np.abs( np.cos(sweep) * (main_span - (np.geomspace(main_span + spacing_param, 0 + spacing_param, num_node_main) - spacing_param))) y[0] = 0 # y[working_node:working_node + num_node_main] = np.cos(sweep)*np.linspace(0.0, main_span, num_node_main) # x[working_node:working_node + num_node_main] = np.sin(sweep)*np.linspace(0.0, main_span, num_node_main) # y[working_node:working_node + num_node_main] = np.cos(sweep)*np.linspace(0.0, main_span, num_node_main) for ielem in range(num_elem_main): for inode in range(num_node_elem): frame_of_reference_delta[working_elem + ielem, inode, :] = [-1, 0, 0] # connectivity for ielem in range(num_elem_main): conn[working_elem + ielem, :] = ((np.ones( (3, )) * (working_elem + ielem) * (num_node_elem - 1)) + [0, 2, 1]) elem_stiffness[working_elem:working_elem + num_elem_main] = 0 elem_mass[working_elem:working_elem + num_elem_main] = 0 boundary_conditions[0] = 1 boundary_conditions[working_node + num_node_main - 1] = -1 working_elem += num_elem_main working_node += num_node_main # left wing (beam 1) -------------------------------------------------------------- beam_number[working_elem:working_elem + num_elem_main] = 1 domain = np.linspace(-1.0, 0.0, num_node_main) tempy = np.linspace(-main_span, 0.0, num_node_main) # x[working_node:working_node + num_node_main - 1] = -np.sin(sweep)*tempy[0:-1] # y[working_node:working_node + num_node_main - 1] = np.cos(sweep)*tempy[0:-1] x[working_node:working_node + num_node_main - 1] = -np.sin(sweep) * ( main_span - (np.geomspace(0 + spacing_param, main_span + spacing_param, num_node_main)[:-1] - spacing_param)) y[working_node:working_node + num_node_main - 1] = -np.abs( np.cos(sweep) * (main_span - (np.geomspace(0 + spacing_param, main_span + spacing_param, num_node_main)[:-1] - spacing_param))) for ielem in range(num_elem_main): for inode in range(num_node_elem): frame_of_reference_delta[working_elem + ielem, inode, :] = [-1, 0, 0] # connectivity for ielem in range(num_elem_main): conn[working_elem + ielem, :] = ((np.ones( (3, )) * (working_elem + ielem) * (num_node_elem - 1)) + [0, 2, 1]) + 1 conn[working_elem + num_elem_main - 1, 1] = 0 elem_stiffness[working_elem:working_elem + num_elem_main] = 0 elem_mass[working_elem:working_elem + num_elem_main] = 0 boundary_conditions[working_node] = -1 working_elem += num_elem_main working_node += num_node_main - 1 with h5.File(route + '/' + case_name + '.fem.h5', 'a') as h5file: coordinates = h5file.create_dataset('coordinates', data=np.column_stack((x, y, z))) conectivities = h5file.create_dataset('connectivities', data=conn) num_nodes_elem_handle = h5file.create_dataset('num_node_elem', data=num_node_elem) num_nodes_handle = h5file.create_dataset('num_node', data=num_node) num_elem_handle = h5file.create_dataset('num_elem', data=num_elem) stiffness_db_handle = h5file.create_dataset('stiffness_db', data=stiffness) stiffness_handle = h5file.create_dataset('elem_stiffness', data=elem_stiffness) mass_db_handle = h5file.create_dataset('mass_db', data=mass) mass_handle = h5file.create_dataset('elem_mass', data=elem_mass) frame_of_reference_delta_handle = h5file.create_dataset( 'frame_of_reference_delta', data=frame_of_reference_delta) structural_twist_handle = h5file.create_dataset('structural_twist', data=structural_twist) bocos_handle = h5file.create_dataset('boundary_conditions', data=boundary_conditions) beam_handle = h5file.create_dataset('beam_number', data=beam_number) app_forces_handle = h5file.create_dataset('app_forces', data=app_forces) node_app_forces_handle = h5file.create_dataset('node_app_forces', data=node_app_forces)
pytorch_data = dict() pytorch_data["trainset"], pytorch_data["testset"] = get_dataset(DATA_PATH, DATASET_NAME, is_numpy=False) class_wise_train_indices = [ np.argwhere(numpy_data["train_labels"] == class_index).flatten() for class_index in CHOOSEN_CLASSES ] total_train_samples = sum([len(ci) for ci in class_wise_train_indices]) MIN_TRAIN_FRACTION = MIN_TRAIN_SAMPLES / total_train_samples MAX_TRAIN_FRACTION = MAX_TRAIN_SAMPLES / total_train_samples fraction_of_train_samples_space = np.geomspace(MIN_TRAIN_FRACTION, MAX_TRAIN_FRACTION, num=10) # progressive sub sampling for each trial, over fraction of train samples # constructs from previously sampled indices and adds on to them as frac progresses train_indices_all_trials = list() for n in range(N_TRIALS): train_indices = list() for frac in fraction_of_train_samples_space: sub_sample = list() for i, class_indices in enumerate(class_wise_train_indices): if not train_indices: num_samples = int(len(class_indices) * frac + 0.5) sub_sample.append( np.random.choice(class_indices, num_samples, replace=False))
filt = myGuassian(mag, 25, 10) #plt.imshow(filt) from scipy import misc im = face = misc.face() im = im[:n,:n, :]/255. im = np.mean(im, -1) im= 1-im plt.imshow(im, cmap='Greys') n_bin_edges = 10 #fewest_freqs = 10. #highest_divisor = np.floor(nyq / fewest_freqs) #bin_edges = nyq/np.logspace(1, np.log2(highest_divisor), num=n_bin_edges, base=2) #need to work on hwo to do spacing a little bit bin_upper_edges = nyq/np.geomspace(1, n/10., num=n_bin_edges) bin_edges = np.append(bin_upper_edges, 0) bin_half_width = np.abs(np.diff(bin_edges)/2) bin_centers = [(bin_edges[i+1] + bin_edges[i]) / 2. for i in range(len(bin_edges))[:-1]] nyq = np.ceil((n/2.)/(bin_centers+bin_half_width*2)) for i in range(len(bin_centers))[:]: filt = myGuassian(mag, bin_centers[i], bin_half_width[i]) plt.figure() plt.subplot(141) plt.imshow(filt) plt.xticks([]);plt.yticks([]) plt.subplot(142) plt.imshow(np.fft.fftshift(np.fft.irfft2(filt)), cmap='Greys') plt.xticks([]);plt.yticks([]) plt.subplot(143)
To run on a standalone cluster: > spark-submit --deploy-mode cluster \ --master spark://192.168.200.230:7077 \ --py-files dist/PedestrianCrowding-2019.6.14-py3.6-macosx-10.7-x86_64.egg \ sample-spark.py ''' ## TODO: set these values as function arguments (model parameters) num_lanes = 2 densities = np.arange(0.02, 1, 0.02) bus_fractions = np.linspace(0, 1 / num_lanes, 11) trials = range(50) alphas = np.geomspace(1e-4, 1, 29) densities = np.arange(0.2, 1, 0.2) bus_fractions = [0.5] trials = range(1) # alphas = np.geomspace(1e-4, 1, 1) alphas = [1] def operation(params): # Output of the mapped function should ideally be a tuple # (creates a CSV) or a string from pickling the object. result = simulate(*params) output = json.dumps({"param_list": params, "param_output": result}) return output
log = get_logger('clusters') parser = argparse.ArgumentParser(description='Save clusters membership in mongodb') parser.add_argument('--num', type=int, default=50, help='Specify the number of cluster levels [default: %(default)s]') args = vars(parser.parse_args()) uri = uri_parser.parse_uri(MONGODB_URL) client = MongoClient(uri['nodelist'][0][0], uri['nodelist'][0][1]) db = client.windmobile now = datetime.now().timestamp() all_stations = list(db.stations.find({ 'status': {'$ne': 'hidden'}, 'last._id': {'$gt': now - 30 * 24 * 3600} })) range_clusters = np.geomspace(20, len(all_stations), num=args['num'], dtype=np.int) ids = np.array([station['_id'] for station in all_stations]) x = [station['loc']['coordinates'][0] for station in all_stations] y = [station['loc']['coordinates'][1] for station in all_stations] X = np.array((x, y)) X = X.T try: mongo_bulk = db.stations.initialize_ordered_bulk_op() mongo_bulk.find({}).update({'$set': {'clusters': []}}) for n_clusters in reversed(range_clusters):
params=params, time=time, virus_model=mdl.vir_model_gamma) mysim4 = exp.Sim(name="timer", params=params, time=time, virus_model=mdl.vir_model_ode) mysims = [mysim1] for mysim in mysims: # run timecourse cells, molecules = mysim.run_sim() # plot timecourse g = pl.plot_timecourse(cells) plt.show() g = pl.plot_timecourse(molecules) plt.show() # get readouts reads = proc.get_readouts(cells) # run pscan pname = "beta" arr = np.geomspace(0.1, 1, 12) myscan1 = proc.pscan(mysim, arr, pname) # plot pscan g = pl.plot_pscan(myscan1) plt.show()
from os.path import exists import numpy as np import matplotlib.pyplot as plt lrs = np.geomspace(1e-7, 1e-4, 15) test_accs = np.zeros(15) index = 0 for lr in lrs: print(lr) print('../generated_data/coarse_learning_rate/testAcc_LR=' + str(lr) + '.npy') if exists('../generated_data/coarse_learning_rate/testAcc_LR=' + str(lr) + '.npy'): test_accs[index] = np.load( '../generated_data/coarse_learning_rate/testAcc_LR=' + str(lr) + '.npy') else: print("not found") index += 1 test_accs_save = [lrs, test_accs] np.save( '../generated_data/coarse_learning_rate/ALL_testAcc_geomspace(1e-7,1e-4,15).npy', test_accs_save) test_accs = np.load( '../generated_data/coarse_learning_rate/ALL_testAcc_geomspace(1e-7,1e-4,15).npy' ) print(test_accs)
def nstep_target(idx, policy_net, target_net, memory, steps=20, device='cpu', BATCH_SIZE=32, GAMMA=0.99, double_dqn=False): range_ = np.arange(0, steps + 1) idx_nReward = idx.reshape(-1, 1) + range_ _batch, _ = memory.sample(idx=idx_nReward.ravel()) n_batch = Transition(*zip(*_batch)) non_final_mask_rewards = torch.tensor( tuple(map(lambda s: s is not None, n_batch.next_state)), device=device, dtype=torch.bool).view(idx_nReward.shape) non_final_mask = torch.prod(non_final_mask_rewards[:, :-1], 1).bool() non_final_mask_r = non_final_mask_rewards[:, :-1] ##### r23 = non_final_mask_r[:, :-1] r23 = r23.t().view(r23.shape[1], r23.shape[0], 1).expand(r23.shape[1], r23.shape[0], r23.shape[1]) r12 = non_final_mask_r[:, 1:] r = torch.prod(r23, 0) * r12.long() r_mask = torch.cat([non_final_mask_rewards[:, 0].view(-1, 1).long(), r], 1) ##### rewards = tuple((map(lambda r: torch.tensor([r], device=device), n_batch.reward))) n_rewards = torch.cat(rewards).view( idx_nReward.shape)[:, 1:] * r_mask.float() gamma_n = np.geomspace(1, GAMMA**(steps - 1), steps) discounted_rewards = n_rewards * torch.from_numpy(gamma_n).float().to( device) discounted_rewards = torch.sum(discounted_rewards, axis=1).to(device) batch_future, _ = memory.sample(idx + steps - 1) batch_ = Transition(*zip(*batch_future)) # non_final_next_states = torch.cat([s for s in batch_.next_state if s is not None]).to(device) next_states_ = [s for s in batch_.next_state] non_final_next_states_mask = torch.tensor(tuple( map(lambda s: s is not None, batch_.next_state)), device=device, dtype=torch.bool) non_final_mask = non_final_next_states_mask * non_final_mask non_final_next_states = torch.cat( itemgetter(*list(torch.where( non_final_mask == 1)[0]))(next_states_)).to(device) next_state_values = torch.zeros(BATCH_SIZE, device=device) if double_dqn: max_action = policy_net(non_final_next_states).max( 1, keepdim=True)[1].detach() next_state_values[non_final_mask] = target_net( non_final_next_states).gather(1, max_action).detach().squeeze(1) else: next_state_values[non_final_mask] = target_net( non_final_next_states, double_dqn=double_dqn).max(1)[0].detach() # next_state_values[non_final_mask] = target_net(non_final_next_states).max(1)[0].detach() expected_state_action_values = (next_state_values * (GAMMA**steps)) + discounted_rewards return expected_state_action_values
def test_physical_quantities(self): a = PhysicalQuantity(1.0) b = PhysicalQuantity(5.0) assert_equal(geomspace(a, b), geomspace(1.0, 5.0))
def findJetBreak(jetType, Y, Z, regime, NU, printMode=None, returnAll=False, ax=None): tN0 = pp.calcTN0(Y) tb = 10 * tN0 thV = Y[0] thC = Y[2] thW = Y[3] chip = 2 * math.sin(0.5 * (thC + thV)) tj_guess = 0.24 * tN0 * math.pow(chip, 8.0 / 3.0) if jetType == -1: if thV <= thC: ta = 1.0e-3 * tj_guess else: tC = 2 * pp.calcTC(jetType, Y, regime) ta = 2 * tC else: if thV <= thC and thV <= thW: ta = 1.0e-3 * tj_guess elif thV <= thW: ta = 1.0e-3 * tj_guess else: tW = pp.calcTW(jetType, Y, regime) ta = 2.0 * tW if ta == 0.0: print(ta, tb, thV, thC, thW) t = np.geomspace(ta, tb, 200) theta = np.zeros(t.shape) phi = np.zeros(t.shape) """ _, _, u, _ = grb.jet.shockVals(theta, phi, t, jetType, *Y, **Z) while u.max() < 1.0: ta = ta / 10.0 print("Retrying with ta = {0:.3e} s".format(ta)) t = np.geomspace(ta, tb, 200) _, _, u, _ = grb.jet.shockVals(theta, phi, t, jetType, *Y, **Z) rel = (u >= 0.5) & (u < 1.0e5) t = t[rel] u = u[rel] theta = theta[rel] phi = phi[rel] """ ta = 5.0e-2 * tj_guess tb = 2.0e1 * tj_guess t = np.geomspace(ta, tb, 200) theta = np.zeros(t.shape) phi = np.zeros(t.shape) _, _, u, _ = grb.jet.shockVals(theta, phi, t, jetType, *Y, **Z) nu = np.empty(t.shape) nu[:] = NU nufac = 1.01 Fnu = grb.fluxDensity(t, nu, jetType, 0, *Y, **Z) Fnua = grb.fluxDensity(t, nu / nufac, jetType, 0, *Y, **Z) Fnub = grb.fluxDensity(t, nu * nufac, jetType, 0, *Y, **Z) beta = np.log(Fnub / Fnua) / np.log(nufac * nufac) right = (np.fabs(beta - betaRegime(regime, Y)) < 0.02)\ & (t > 3*ta) & (t < tb/3) t_fit = t[right] # nu_fit = nu[right] # u_fit = u[right] # theta_fit = theta[right] # phi_fit = phi[right] Fnu_fit = Fnu[right] t_guess = 0.24 * tN0 * np.power(2 * np.sin(0.5 * (thC + thV)), 8.0 / 3.0) i0s = [ 1, len(t_fit) // 4, len(t_fit) // 2, 3 * len(t_fit) // 4, len(t_fit) - 2 ] i0 = np.searchsorted(t_fit, t_guess) if i0 > 1 and i0 < len(t_fit) - 2: i0s.append(i0) bmin = -10 bmax = 10 bounds = [(math.log10(t_fit.min()) - 1, math.log10(t_fit.max()) + 1), (math.log10(Fnu_fit.min()) - 1, math.log10(Fnu_fit.max()) + 1), (bmin, bmax), (bmin, bmax), (-1.5, 1.5)] chi2min = np.inf x_best = None for i0 in i0s: if printMode is 'all': print("i0 = {0:d}".format(i0)) lt0 = math.log10(t_fit[i0]) lF0 = math.log10(Fnu_fit[i0]) ls = 1.0 b0 = math.log(Fnu_fit[i0] / Fnu_fit[0]) / math.log( t_fit[i0] / t_fit[0]) b1 = math.log(Fnu_fit[-1] / Fnu_fit[i0]) / math.log( t_fit[-1] / t_fit[i0]) if b0 < bmin: b0 = bmin + 0.1 if b0 > bmax: b0 = bmax - 0.1 if b1 < bmin: b1 = bmin + 0.1 if b1 > bmax: b1 = bmax - 0.1 x0 = [lt0, lF0, b0, b1, ls] func = lf_spl2 if printMode is 'all': print("chi2(x0) = {0:.3e}".format(chi2(x0, t_fit, Fnu_fit, func))) res = opt.minimize(chi2, x0, (t_fit, Fnu_fit, func), bounds=bounds, method='TNC', options={'maxiter': 8000}) if printMode is 'all': print("chi2(x1) = {0:.3e}".format(chi2(res.x, t_fit, Fnu_fit, func))) if printMode is 'all': print(res) elif printMode is 'summary': print("Success: " + str(res.success) + " chi2={0:.2e}".format(res.fun)) if res.fun < chi2min: chi2min = res.fun x_best = res.x x = x_best if x[3] - x[2] > -0.74: print(" is that even a jetbreak? b0={0:.2f} b1={1:.2f} thV={2:.2f}". format(x[2], x[3], thV)) if ax is not None: ax.plot(t, Fnu) ax.plot(t_fit, func(t_fit, *x0), lw=1, color='k', ls='--') ax.plot(t_fit, func(t_fit, *x), lw=1, color='k') # ax2 = ax.twinx() # ax2.plot(t, u, color='tab:orange') # ax2.set_yscale('log') ax.set_xscale('log') ax.set_yscale('log') if returnAll: return x return math.pow(10.0, x[0])
def plot_scores_per_canopy(allScores, currResultDir): trainScores, devScores, testScores = allScores["train"], allScores["dev"], allScores["test"] Path(currResultDir).mkdir(parents=True, exist_ok=True) # Create resultDir directory if not already present metricList = list(set(list(trainScores.keys()) + list(testScores.keys()) + list(devScores.keys()))) for metric in metricList: print("Plotting metric={}".format(metric)) X,Y = [],[] if metric in trainScores: temp = [(x,trainScores[metric][x]) for x in trainScores[metric]] temp = sorted(temp,key=lambda x:x[1]) X_train = [x for x,y in temp] X += X_train Y += [y for x,y in temp] if metric in testScores: temp = [(x,testScores[metric][x]) for x in testScores[metric]] temp = sorted(temp,key=lambda x:x[1]) X_test = [x for x,y in temp] X += X_test Y += [y for x,y in temp] if metric in devScores: temp = [(x,devScores[metric][x]) for x in devScores[metric]] temp = sorted(temp,key=lambda x:x[1]) X_dev = [x for x,y in temp] X += X_dev Y += [y for x,y in temp] if len(X) == 0: continue plt.clf() plt.rcParams.update({'font.size': 40}) fig, all_ax = plt.subplots(2, 3, sharex=True, sharey=True,figsize=(80,60)) # for ax in all_ax.flat: # ax.set(xlabel='Canopies', ylabel=metric) for m_ctr,measure in enumerate(["avgMents","numMents","numSingletons","stdMents","origin"]): print("Plotting for measure={}".format(measure)) canopy_stats = json.load(open("resources/aminer/aminer_{}.json".format(measure),"r")) p_x,p_y = int(m_ctr/3),m_ctr%3 ax = all_ax[p_x,p_y] barlist = ax.bar(X, Y) vals = sorted(list(set([canopy_stats[key] for key in canopy_stats]))) minVal = min([v for v in vals if v > 0]) maxVal = max(vals) numVals = len(vals) numColors= min(100,numVals) cmap = sns.color_palette("YlOrRd", numColors) if measure in ["avgMents","numSingletons","stdMents","numMents"]: bins = np.geomspace(minVal, maxVal, numColors) else: bins = np.arange(minVal, maxVal, (maxVal-minVal)/numColors) valToIdx = {val:i for i,val in enumerate(vals)} for i, val in enumerate(vals): valToIdx[val] = 0 for j in range(len(bins)): if bins[j] <= val: valToIdx[val] = j for idx,x in enumerate(X): if x in canopy_stats: val = canopy_stats[x] color = cmap[valToIdx[val]] barlist[idx].set_color(color) else: barlist[idx].set_color('r') Z = [canopy_stats[x] for x in X] corrCoeff = np.corrcoef(Y, Z)[0, 1] plt.title("{}".format(metric)) ax.set_title("{}\t{:.3f}".format(measure,corrCoeff)) ax.set_xticks([]) plt.savefig(currResultDir + "/{}.png".format(metric)) plt.close() for m_ctr,measure in enumerate(["avgMents","numMents","numSingletons","stdMents","origin"]): print("Plotting for measure={}".format(measure)) canopy_stats = json.load(open("resources/aminer/aminer_{}.json".format(measure),"r")) plt.clf() matplotlib.rcdefaults() plt.figure(figsize=(16,9)) Z = [canopy_stats[x] for x in canopy_stats] plt.hist(Z,bins=len(set(Z))) plt.savefig(currResultDir + "/distribution_{}.png".format(measure)) plt.close()
Returns ------- y: ndarray(N,M) Folded exponential for given taus. """ ws = w k = 1 / tau k = k.reshape(tau.size, 1, 1) t = (tt + tz).T.reshape(1, tt.shape[1], tt.shape[0]) y = np.exp(k * (ws * ws * k / (4.0) - t)) y *= 0.5 * my_erfc(-t / ws + ws * k / (2.0)) return y.T jit1 = nb.njit(_fold_exp, parallel=True, fastmath=True) jit2 = nb.njit(_fold_exp, parallel=False, fastmath=True) jit3 = nb.njit(_fold_exp, parallel=True, fastmath=False) jit4 = nb.njit(_fold_exp, parallel=False, fastmath=False) import matplotlib.pyplot as plt plt.figure() for i, j in enumerate([jit1, jit2, jit3, jit4]): for N in np.geomspace(10, 1000, 20): res_jit = benchmark(j, ta_shape=(300, N), N=30) plt.plot(np.ones_like(res_jit.all) * N, res_jit.all, 'x', c='C'+str(i)) plt.show()