def run(function, queue, idx, rnd, *args): """ A helper function for handling return values. Takes a function and its arguments and stores its result in a queue. @type function: function @param function: handle to function that will be called @type queue: Queue @param queue: stores returned function values @type idx: integer @param idx: index used to identify return values @type rnd: float @param rnd: a random number to seed random number generator """ # compute random seed rnd_seed = int(1e6 * rnd + 1e6 * time()) # without it, different processes are likely to use the same seed numpy_seed(rnd_seed) py_seed(rnd_seed) # evaluate function queue.put((idx, function(*args)))
def sample_times(node, num_times): if not hasattr(GC,"NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True assert hasattr(GC,'transmissions'), "No transmission network found in global context! Run this after the transmission network simulation is done" first_time = node.get_first_infection_time() if first_time is None: return [] windows = [] last_time = first_time for u,v,t in GC.transmissions: if u == node and v == node: if last_time is not None and t > last_time: windows.append((last_time,t)) last_time = None elif last_time is None and v == node: last_time = t if last_time is not None and t > last_time: windows.append((last_time, GC.time)) if len(windows) == 0: windows.append((first_time, GC.time)) loc = (GC.ts_truncnorm_loc-0.5)*GC.ts_truncnorm_scale scale = 1. a = (-0.5 + (0.5-GC.ts_truncnorm_loc))*GC.ts_truncnorm_scale b = (0.5 + (0.5-GC.ts_truncnorm_loc))*GC.ts_truncnorm_scale truncnorm_variates = (truncnorm.rvs(a,b,loc=loc,scale=scale,size=num_times)/GC.ts_truncnorm_scale)+0.5 out = [] for i in range(num_times): start,end = choice(windows) out.append((truncnorm_variates[i]*(end-start))+start) return out
def sample_times(node, num_times): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True assert hasattr( GC, 'transmissions' ), "No transmission network found in global context! Run this after the transmission network simulation is done" first_time = node.get_first_infection_time() if first_time is None: return [] windows = [] last_time = first_time for u, v, t in GC.transmissions: if u == node and v == node: if last_time is not None and t > last_time: windows.append((last_time, t)) last_time = None elif last_time is None and v == node: last_time = t if last_time is not None and t > last_time: windows.append((last_time, GC.time)) if len(windows) == 0: windows.append((first_time, GC.time)) out = [] for i in range(num_times): start, end = choice(windows) length = end - start delta = float('inf') while delta > length: delta = gamma(GC.ts_gamma_shape, scale=GC.ts_gamma_scale) out.append(delta + start) return out
def sample_nodes(time): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True assert GC.next_trans is not None, "Must call TransmissionTimeSample_SI before TransmissionNodeSample_SI" u, v, t = GC.next_trans assert not v.is_infected(), "Destination virus is already infected" GC.next_trans = None for edge in GC.contact_network.get_edges_from(v): neighbor = edge.get_to() if not neighbor.is_infected(): infected_neighbors = [ edge.get_from() for edge in GC.contact_network.get_edges_to(neighbor) if edge.get_from().is_infected() ] infected_neighbors.append(v) infector = choice(infected_neighbors) time = t + exponential( scale=1 / (GC.infection_rate * len(infected_neighbors)) ) # min of exponentials is exponential with sum of rates if neighbor in GC.trans_pq_v2trans: GC.trans_pq.removeFirst(neighbor) GC.trans_pq.put(neighbor, time) GC.trans_pq_v2trans[neighbor] = (infector, neighbor, time) return u, v
def initialize_with_keras_hdf5(keras_model, dict_map, torch_model, model_path=None, seed=None): """ :param keras_model: a keras model created by keras.models.Sequential :param dict_map: a dictionary maps keys from Kera => PyTorch :param torch_model: a PyTorch network :param model_path: path where h5 file located, if None, than keras will initialize a new network :return: PyTorch StateDict """ if model_path: weight_dict = load_weights_from_hdf5(model_path, keras_model) else: if seed: numpy_seed(seed) set_random_seed(seed) keras_model.compile(keras.optimizers.adam()) weight_dict = {} for layer in keras_model.layers: weight_dict.update({layer.name: layer.get_weights()}) state_dict = torch_model.state_dict() for key in weight_dict.keys(): destiny = dict_map[key] for i, item in enumerate(destiny): if len(weight_dict[key][i].shape) == 4: # Convolutional Layer tensor = np.transpose(weight_dict[key][i], (3, 2, 0, 1)) elif len(weight_dict[key][i].shape) == 2: # Full Connection Layer tensor = np.transpose(weight_dict[key][i], (1, 0)) else: tensor = weight_dict[key][i] state_dict[item] = torch.tensor(tensor) torch_model.load_state_dict(state_dict) return torch_model
def sample_time(): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True return GC.time + exponential(scale=1 / (float(GC.time_sample_rate)))
def time_to_mutation_rate(tree): if not hasattr(GC,"NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.edge_length is not None: node.edge_length *= noncentral_f(dfnum=GC.tree_rate_dfnum,dfden=GC.tree_rate_dfden,nonc=GC.tree_rate_lambda) return str(t)
def sample_num_times(node): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True if node.get_first_infection_time( ) is not None and node.get_first_infection_time() != GC.time: return poisson(lam=GC.num_time_sample_lambda) else: return 0
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.edge_length is not None: node.edge_length *= pareto(a=GC.tree_rate_shape) return str(t)
def make_deterministic(seed): random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) torch.backends.cudnn.deterministic = True numpy_seed(seed) os.environ["PYTHONHASHSEED"] = str(seed) warnings.warn("You have chosen to seed training. " "This will turn on the CUDNN deterministic setting, " "which can slow down your training considerably! " "You may see unexpected behavior when restarting " "from checkpoints.")
def post_fork(arbiter, worker): """ A Gunicorn hook which initializes the worker. """ # Each subprocess needs to have the random number generator re-seeded. numpy_seed() worker.app.zato_wsgi_app.startup_callable_tool.invoke(SERVER_STARTUP.PHASE.BEFORE_POST_FORK, kwargs={ 'arbiter': arbiter, 'worker': worker, }) worker.app.zato_wsgi_app.worker_pid = worker.pid ParallelServer.start_server(worker.app.zato_wsgi_app, arbiter.zato_deployment_key)
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.is_root(): node.rate = GC.tree_rate_R0 else: node.rate = exponential(scale=node.parent.rate) if node.edge_length is not None: node.edge_length *= node.rate return str(t)
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.edge_length is not None: node.edge_length *= truncnorm.rvs(a=GC.tree_rate_min, b=GC.tree_rate_max, loc=GC.tree_rate_loc, scale=GC.tree_rate_scale, size=1)[0] return str(t)
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.is_root(): node.rate = GC.tree_rate_R0 else: assert node.edge_length is not None and node.edge_length > 0, "All edges must have positive lengths for TreeUnit_AutocorrelatedLogNormal" node.rate = lognormal(mean=node.parent.rate, sigma=GC.tree_rate_v * node.edge_length) if node.edge_length is not None: # root node might not have incident edge node.edge_length *= node.rate return str(t)
def random_Filter(n=10, p=5, q=5, seed=None, name=None): """ Generate a n-th order stable filter, with q inputs and p outputs Parameters ---------- - n: number of states (default: 10) - p: number of outputs (default: 5) - q: number of inputs (default: 5) - seed: if not None, indicates the seed toi use for the random part (in order to be reproductible, the seed is stored in the name of the filter) - name: used to build a random filter from a string (a name of a filter previously built with random_Filter) -> should be of the form 'RandomFilter-7/1/4-12345678' Returns a Filter object """ if name: m = regRF.match(name) if m: res = tuple(map(int, m.groups())) n = res[0] p = res[1] q = res[2] seed = res[3] else: raise ValueError( "randomFilter: the string should be a valid string, ie be like 'RandomFilter-7/1/4-12345678'" ) # change the seed if asked if seed: numpy_seed(seed) name = 'RandomFilter-%d/%d/%d-%d' % ( n, p, q, seed ) # for example 'RandomFilter-(5,10)/(1,2)/(1,10)-12345678' for a MISO filter (#states between 5 and 10, #inputs between 1 and 10), seed=12345678) else: name = 'RandomFilter' # return a Filter from a random dSS return Filter(ss=random_dSS(n, p, q), name=name, stable=True)
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.edge_length is None: if node.is_root(): node.rate = GC.tree_rate_R0 else: node.rate = node.parent.rate else: if node.is_root(): parent_rate = GC.tree_rate_R0 else: parent_rate = node.parent.rate node.rate = noncentral_chisquare( df=GC.tree_rate_df, nonc=nonc(GC.tree_rate_b, GC.tree_rate_sigma, parent_rate, node.edge_length)) node.edge_length *= node.rate return str(t)
def make_results_reproducible(model_is_convolutional: bool = False) -> None: """ Make the subsequent instructions produce purely deterministic outputs by fixing all the relevant seeds: """ random_seed(0) _ = numpy_seed(0) _ = torch_manual_seed(0) # since GPU computations may introduce additional stochasticity with # their convolutional operation optimizations: if model_is_convolutional: if cuda_is_available(): # disabling benchmarking and choosing among convolution operation # implementation alternatives, which is stochastic based due to # noise and hardware: cudnn.benchmark = False # ansuring deterministic algorithms for colvolutional operations # are employed: cudnn.deterministic = True