Пример #1
0
    def __init__(self, network, theory_spec):
        self.params = copy(theory_params)
        check_custom_params(theory_spec, self.params)
        self.custom_params = theory_spec
        nested_update(self.params, self.custom_params)

        self.network = network
        E_L = self.params['neuron_params']['single_neuron_dict']['E_L']
        self.NP = {
            'theta':
            self.params['neuron_params']['single_neuron_dict']['V_th'] - E_L,
            'V_reset':
            self.params['neuron_params']['single_neuron_dict']['V_reset'] -
            E_L,
            'tau_m':
            self.params['neuron_params']['single_neuron_dict']['tau_m'],
            # assumes that tau_syn_ex = tau_syn_in in LIF neuron
            'tau_syn':
            self.params['neuron_params']['single_neuron_dict']['tau_syn_ex'],
            't_ref':
            self.params['neuron_params']['single_neuron_dict']['t_ref'],
            'tau':
            1.
        }
        self.label = dicthash.generate_hash_from_dict({
            'params':
            self.params,
            'network_label':
            self.network.label
        })
    def __init__(self, network, sim_spec):
        """
        Simulation class.
        An instance of the simulation class with the given parameters.
        Can be created as a member class of a multiarea_model instance
        or standalone.

        Parameters
        ----------
        network : multiarea_model
            An instance of the multiarea_model class that specifies
            the network to be simulated.
        params : dict
            custom simulation parameters that overwrite the
            default parameters defined in default_params.py
        """
        self.params = deepcopy(sim_params)
        if isinstance(sim_spec, dict):
            check_custom_params(sim_spec, self.params)
            self.custom_params = sim_spec
        else:
            fn = os.path.join(data_path, sim_spec, '_'.join(
                ('custom_params', sim_spec)))
            with open(fn, 'r') as f:
                self.custom_params = json.load(f)['sim_params']

        nested_update(self.params, self.custom_params)

        self.network = network
        self.label = dicthash.generate_hash_from_dict({
            'params':
            self.params,
            'network_label':
            self.network.label
        })

        print("Simulation label: {}".format(self.label))
        self.data_dir = os.path.join(data_path, self.label)
        try:
            os.mkdir(self.data_dir)
            os.mkdir(os.path.join(self.data_dir, 'recordings'))
        except OSError:
            pass
        self.copy_files()
        print("Copied files.")
        d = {
            'sim_params': self.custom_params,
            'network_params': self.network.custom_params,
            'network_label': self.network.label
        }
        with open(
                os.path.join(self.data_dir, '_'.join(
                    ('custom_params', self.label))), 'w') as f:
            json.dump(d, f)
        print("Initialized simulation class.")

        self.areas_simulated = self.params['areas_simulated']
        self.areas_recorded = self.params['recording_dict']['areas_recorded']
        self.T = self.params['t_sim']
        self.extra_global_param_bytes = 0
Пример #3
0
    def __init__(self,
                 network_spec,
                 theory=False,
                 simulation=False,
                 analysis=False,
                 *args,
                 **keywords):
        """
        Multiarea model class.
        An instance of the multiarea model with the given parameters.

        Parameters
        ----------
        network_spec : dict or str
            Specify the network. If it is of type dict, the parameters defined
            in the dictionary overwrite the default parameters defined in
            default_params.py.
            If it is of type str, the string defines the label of a previously
            initialized model instance that is now loaded.
        theory : bool
            whether to create an instance of the theory class as member.
        simulation : bool
            whether to create an instance of the simulation class as member.
        analysis : bool
            whether to create an instance of the analysis class as member.

        """
        self.params = deepcopy(network_params)
        if isinstance(network_spec, dict):
            print("Initializing network from dictionary.")
            check_custom_params(network_spec, self.params)
            self.custom_params = network_spec
            p_ = 'multiarea_model/data_multiarea/custom_data_files'
            # Draw random integer label for data script to avoid clashes with
            # parallelly created class instances
            rand_data_label = np.random.randint(10000)
            print("RAND_DATA_LABEL", rand_data_label)
            tmp_parameter_fn = os.path.join(
                base_path, p_,
                'custom_{}_parameter_dict.json'.format(rand_data_label))
            tmp_data_fn = os.path.join(
                base_path, p_,
                'custom_Data_Model_{}.json'.format(rand_data_label))

            with open(tmp_parameter_fn, 'w') as f:
                json.dump(self.custom_params, f)
            # Execute Data script
            compute_Model_params(out_label=str(rand_data_label), mode='custom')
        else:
            print("Initializing network from label.")
            parameter_fn = os.path.join(base_path, 'config_files',
                                        '{}_config'.format(network_spec))
            tmp_data_fn = os.path.join(
                base_path, 'config_files',
                'custom_Data_Model_{}.json'.format(network_spec))
            with open(parameter_fn, 'r') as f:
                self.custom_params = json.load(f)
        nested_update(self.params, self.custom_params)
        with open(tmp_data_fn, 'r') as f:
            dat = json.load(f)

        self.structure = OrderedDict()
        for area in dat['area_list']:
            self.structure[area] = dat['structure'][area]
        self.N = dat['neuron_numbers']
        self.synapses = dat['synapses']
        self.W = dat['synapse_weights_mean']
        self.W_sd = dat['synapse_weights_sd']
        self.area_list = complete_area_list
        self.distances = dat['distances']

        ind, inda, out, outa = load_degree_data(tmp_data_fn)
        # If K_stable is specified in the params, load the stabilized matrix
        # TODO: Extend this by calling the stabilization method
        if self.params['connection_params']['K_stable'] is None:
            self.K = ind
        else:
            if not isinstance(self.params['connection_params']['K_stable'],
                              str):
                raise TypeError("Not supported. Please store the "
                                "matrix in a binary numpy file and define "
                                "the path to the file as the parameter value.")
            # Assume that the parameter defines a filename containing the matrix
            K_stable = np.load(self.params['connection_params']['K_stable'])
            ext = {
                area: {
                    pop: ind[area][pop]['external']
                    for pop in self.structure['V1']
                }
                for area in self.area_list
            }
            self.K = matrix_to_dict(K_stable,
                                    self.area_list,
                                    self.structure,
                                    external=ext)
            self.synapses = indegree_to_synapse_numbers(self.K, self.N)

        self.vectorize()
        if self.params['K_scaling'] != 1. or self.params['N_scaling'] != 1.:
            if self.params['fullscale_rates'] is None:
                raise KeyError('For downscaling, you have to define a file'
                               ' with fullscale rates.')
            self.scale_network()

        self.K_areas = area_level_dict(self.K, self.N)
        self.label = dicthash.generate_hash_from_dict(
            {
                'params': self.params,
                'K': self.K,
                'N': self.N,
                'structure': self.structure
            },
            blacklist=[('params', 'fullscale_rates'),
                       ('params', 'connection_params', 'K_stable'),
                       ('params', 'connection_params',
                        'replace_cc_input_source')])

        if isinstance(network_spec, dict):
            parameter_fn = os.path.join(base_path, 'config_files',
                                        '{}_config'.format(self.label))
            data_fn = os.path.join(
                base_path, 'config_files',
                'custom_Data_Model_{}.json'.format(self.label))

            shutil.move(tmp_parameter_fn, parameter_fn)
            shutil.move(tmp_data_fn, data_fn)

        elif isinstance(network_spec, str):
            assert (network_spec == self.label)

        # Initialize member classes
        if theory:
            if 'theory_spec' not in keywords:
                theory_spec = {}
            else:
                theory_spec = keywords['theory_spec']
            self.init_theory(theory_spec)

        if simulation:
            if 'sim_spec' not in keywords:
                sim_spec = {}
            else:
                sim_spec = keywords['sim_spec']
            self.init_simulation(sim_spec)

        if analysis:
            assert (getattr(self, 'simulation'))
            if 'ana_spec' not in keywords:
                ana_spec = {}
            else:
                ana_spec = keywords['ana_spec']
            self.init_analysis(ana_spec)