def vary_parameters(model, parameters, watch, duration, steps, fromzero=False, layered=False): trials = Trials(model, parameters) X, Y, Z = trials.run(duration, steps, watch) ylabel = model.find_label(trials.parameters[0].parameter[-1]) zlabel = model.find_label(watch) if layered: plot.plot_layered(X, Y, Z, ylabel, zlabel) else: plot.plot_series(np.array(trials.model.records['t']), trials.parameters[0].series(steps), Z, ylabel, zlabel, fromzero)
def run_trials(steps, change, variable, reverse=False, layered=False): trials = HHTrials(steps, change) X, Y, Z = trials.run() if reverse: Z = list(Z) Z.reverse() Z = np.array(Z) if layered: plot.plot_layered(X, Y, Z, variable) else: plot.plot_series(np.array(trials.hh.records['time']), steps, Z, variable)
def get_integrated_flux(pop_seq, rate, time, nodes=None, grid=100, norm=1, plot_details=False, plot_name='', divide=5, y_max=0, x_max=0, legend=True, save_to_file=False, mute=False): """ calculate the time integrated flux :param pop_seq: 1. calculated population dynamics 2. initial population (will call propagate()) :param rate: numpy matrix or pandas dataframe: rate matrix :param time: propagated time :param nodes: node name list (necessary if :param rate is a numpy matrix) :param grid: number of grids on time (if only initial population is provided) :param norm: all flux will multiply this value (default = 1) :param plot_details: plot the flux and integrated flux without subtraction if plot_details asserted, the following parameters control the details: :param plot_name: name suffix of the saved figures :param divide: how many lines per plot (default = 5) :param x_max: set the x limit of the figures :param y_max: set the y limit of the figures :param legend: (default = True) :param save_to_file: save figure or show only :param mute: ignore all output (including plot_details!) :return: n * n numpy matrix: time-integrated flux """ # if only initial population is provided if len(pop_seq.shape) == 1 or pop_seq.shape[1] == 1: time_sequence = np.linspace(0, time, grid) pop_seq = propagate(pop_seq.reshape(-1, 1), rate, time_sequence, option=0, print_pop=plot_details) if isinstance(rate, pd.DataFrame): nodes = rate.keys() rate = rate.values wrap_nodes = [wrap_str(n) for n in nodes] name_len = max([max([len(n) for n in wrap_nodes]) + 1, 10]) size, length = np.shape(pop_seq) # calculate integrated flux: f_tmp = (rate @ np.array([np.diag(p) for p in pop_seq.T]) * norm).T.swapaxes(0, 1) flux = f_tmp - f_tmp.swapaxes(0, 1) flux_long_time = flux[:, :, -1] flux_subtract = flux - flux_long_time.reshape(size, size, 1) integrated_flux = integrate.cumtrapz(flux_subtract, time, initial=0) integrated_flux_wo_subtract = integrate.cumtrapz(flux, time, initial=0) abs_integration = np.trapz(np.abs(flux), time, axis=2) integrated_flux_matrix = np.zeros((size, size)) sum_abs = 0 flux_ls = [] for i, j in permutations(range(size), 2): f = integrated_flux[i, j, -1] if f > 0: sum_abs += abs_integration[i, j] # calculate the population flow delta_flow = integrated_flux[i, j, 1:] - integrated_flux[i, j, :-1] forward = np.sum(delta_flow[delta_flow > 0]) backward = np.abs(np.sum(delta_flow[delta_flow < 0])) integrated_flux_matrix[i, j] = forward integrated_flux_matrix[j, i] = backward # sort by "loading" of the energy channel flux_ls.append([ abs_integration[i, j], integrated_flux[i, j, -1], integrated_flux_wo_subtract[i, j, -1], forward, backward, i, j ]) if mute: return integrated_flux_matrix # print results flux_ls.sort(reverse=True) print( '{{:{}}}{{:{}}}{{:10}}{{:15}}{{:10}}{{:10}}{{:10}}{{:10}}{{:10}}{{:7}}{{:10}}' .format(name_len, name_len).format('source', 'target', 'maximum', 'equilibrium', 'integral', 'int - eq', 'abs sum', 'forward', 'backward', 'rate', 'backrate')) if get_module_logger_level() > 20: print_numbers = 3 elif get_module_logger_level() > 10: print_numbers = 10 else: print_numbers = None for f_abs, f_sub, f, fw, bw, i, j in flux_ls[:print_numbers]: if f_sub > 0: print( '{{:{}}}{{:{}}}{{:<10.3f}}{{:<15.4f}}' '{{:<10.3f}}{{:<10.3f}}{{:<10.3f}}{{:<10.3f}}{{:<10.3f}}{{:<7.2f}}{{:<10.2f}}' .format(name_len, name_len).format(wrap_nodes[j], wrap_nodes[i], max(flux[i, j, :]), flux_long_time[i, j], f, f_sub, f_abs, fw, bw, rate[i, j], rate[j, i])) print_normal('integrate over all abs flux: {:.2f}'.format(sum_abs)) print_normal('flow matrix:') print_normal(integrated_flux_matrix) if plot_details: flux_series = [] int_flux_series = [] flux_names = [] int_flux_names = [] int_flux_wo_series = [] for _, f_sub, _, fw, bw, i, j in flux_ls: name = wrap_nodes[j] + ' \u2192 ' + wrap_nodes[i] int_flux_names.append(name) flux_names.append('{:.2f}: {}'.format(f_sub, name)) flux_series.append(flux[i, j, :]) int_flux_series.append(integrated_flux[i, j, :]) int_flux_wo_series.append(integrated_flux_wo_subtract[i, j, :]) axes_names = ('Time (ps)', 'Time Integrated Flux') plot.plot_series(int_flux_series, time, flux_names, axes_names, plot_name + 'IntegratedFlux', divide=divide, y_max=y_max, x_max=x_max, legend=legend, save_to_file=save_to_file) plot.plot_series(int_flux_wo_series, time, flux_names, axes_names, plot_name + 'IntegratedFluxWithoutSubtract', divide=divide, y_max=y_max, x_max=x_max, legend=legend, save_to_file=save_to_file) axes_names = ('Time (ps)', 'Flow (ps' + r'$^{\mathregular{-1}}$' ')') plot.plot_series(flux_series, time, flux_names, axes_names, plot_name + 'Flux', divide=divide, y_max=y_max, x_max=x_max, legend=legend, zero=True, save_to_file=save_to_file) return integrated_flux_matrix