Skip to content

Saving / Loading#

Saving / loading the state of the network#

To save or load the network state you can use the following methods:

ANNarchy.save(filename, populations=True, projections=True, net_id=0) #

Save the current network state (parameters and variables) to a file.

  • If the extension is '.npz', the data will be saved and compressed using np.savez_compressed (recommended).

  • If the extension is '.mat', the data will be saved as a Matlab 7.2 file. Scipy must be installed.

  • If the extension ends with '.gz', the data will be pickled into a binary file and compressed using gzip.

  • Otherwise, the data will be pickled into a simple binary text file using cPickle.

Warning: The '.mat' data will not be loadable by ANNarchy, it is only for external analysis purpose.

Example:

save('results/init.npz')

save('results/init.data')

save('results/init.txt.gz')

save('1000_trials.mat')

Parameters:

  • filename

    filename, may contain relative or absolute path.

  • populations

    if True, population data will be saved (by default True)

  • projections

    if True, projection data will be saved (by default True)

Source code in ANNarchy/core/IO.py
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def save(filename, populations=True, projections=True, net_id=0):#, pure_data=True):
    """
    Save the current network state (parameters and variables) to a file.

    * If the extension is '.npz', the data will be saved and compressed using `np.savez_compressed` (recommended).

    * If the extension is '.mat', the data will be saved as a Matlab 7.2 file. Scipy must be installed.

    * If the extension ends with '.gz', the data will be pickled into a binary file and compressed using gzip.

    * Otherwise, the data will be pickled into a simple binary text file using cPickle.

    **Warning:** The '.mat' data will not be loadable by ANNarchy, it is only for external analysis purpose.

    Example:

    ```python
    save('results/init.npz')

    save('results/init.data')

    save('results/init.txt.gz')

    save('1000_trials.mat')
    ```

    :param filename: filename, may contain relative or absolute path.
    :param populations: if True, population data will be saved (by default True)
    :param projections: if True, projection data will be saved (by default True)

    """
    data = _net_description(populations, projections, net_id)
    _save_data(filename, data)

ANNarchy.load(filename, populations=True, projections=True, pickle_encoding=None, net_id=0) #

Loads a saved state of the network.

Warning: Matlab data can not be loaded.

Example:

load('results/network.npz')

Parameters:

  • filename

    the filename with relative or absolute path.

  • populations

    if True, population data will be loaded (by default True)

  • projections

    if True, projection data will be loaded (by default True)

  • pickle_encoding

    optional parameter provided to the pickle.load() method. If set to None the default is used.

Source code in ANNarchy/core/IO.py
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
def load(filename, populations=True, projections=True, pickle_encoding=None, net_id=0):
    """
    Loads a saved state of the network.

    **Warning:** Matlab data can not be loaded.

    Example:

    ```python
    load('results/network.npz')
    ```


    :param filename: the filename with relative or absolute path.
    :param populations: if True, population data will be loaded (by default True)
    :param projections: if True, projection data will be loaded (by default True)
    :param pickle_encoding: optional parameter provided to the pickle.load() method. If set to None the default is used.
    """

    desc = _load_data(filename, pickle_encoding)
    if desc is None:
        return

    if 'time_step' in desc.keys():
        Global.set_current_step(desc['time_step'], net_id)

    if populations:
        # Over all populations
        for pop in Global._network[net_id]['populations']:
            # check if the population is contained in save file
            if pop.name in desc.keys():
                pop._load_pop_data(desc[pop.name])

    if projections:
        for proj in Global._network[net_id]['projections'] :
            if proj.name in desc.keys():
                proj._load_proj_data(desc[proj.name])

Please note that these functions are only usable after the call to ANNarchy.compile().

Saving / loading the parameters of the network#

ANNarchy.save_parameters(filename, net_id=0) #

Saves the global parameters of a network (flag population for neurons, projection for synapses) to a JSON file.

Parameters:

  • filename

    path to the JSON file.

  • net_id

    ID of the network (default: 0, the global network).

Source code in ANNarchy/core/IO.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def save_parameters(filename, net_id=0):
    """
    Saves the global parameters of a network (flag ``population`` for neurons, ``projection`` for synapses) to a JSON file.

    :param filename: path to the JSON file.
    :param net_id: ID of the network (default: 0, the global network).
    """
    import json

    # Get the netowrk description
    network = Global._network[net_id]

    # Dictionary of parameters
    description = {
        'populations' : {},
        'projections' : {},
        'network' : {},
        'constants' : {},
    }

    # Constants
    for constant in Global._objects['constants']:
        description['constants'][constant.name] = constant.value

    # Populations
    for pop in network['populations']:

        # Get the neuron description
        neuron = pop.neuron_type

        pop_description = {}

        for param in neuron.description['global']:
            pop_description[param] = pop.init[param]

        description['populations'][pop.name] = pop_description

    # Projections
    for proj in network['projections']:

        # Get the synapse description
        synapse = proj.synapse_type

        proj_description = {}

        for param in synapse.description['global']:
            proj_description[param] = proj.init[param]

        description['projections'][proj.name] = proj_description

    # Save the description in a json file
    try:
        with open(filename, 'w') as wfile:
            json.dump(description, wfile, indent=4)
    except IOError as error:
        Global._error("save_parameters(): cannot write the json file. Make sure the subfolders already exist.")

ANNarchy.load_parameters(filename, global_only=True, verbose=False, net_id=0) #

Loads the global parameters of a network (flag population for neurons, projection for synapses) from a JSON file.

It is advised to generate the JSON file first with save_parameters() and later edit it manually.

A strong restriction is that population/projection names cannot change between saving and loading. By default, they take names such as pop0 or proj2, we advise setting explicitly a name in their constructor for readability.

If you add a parameter name to the JSON file but it does not exist in te neuron/synapse, it will be silently skipped. Enable verbose=True to see which parameters are effectively changed.

If you set global_only to True, you will be able to set values for non-global parameters (e.g. synapse-specific), but a single value will be loaded for all. The JSON file cannot contain arrays.

If you want to save/load the value of variables after a simulation, please refer to save() or load().

Parameters:

  • filename

    path to the JSON file.

  • global_only

    True if only global parameters (flags population and projection) should be loaded, the other values are ignored. (default: True)

  • verbose

    True if the old and new values of the parameters should be printed (default: False).

  • net_id

    ID of the network (default: 0, the global network).

Returns:

  • a dictionary of additional parameters not related to populations or projections (keyword network in the JSON file).

Source code in ANNarchy/core/IO.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def load_parameters(filename, global_only=True, verbose=False, net_id=0):
    """
    Loads the global parameters of a network (flag ``population`` for neurons, ``projection`` for synapses) from a JSON file.

    It is advised to generate the JSON file first with ``save_parameters()`` and later edit it manually.

    A strong restriction is that population/projection names cannot change between saving and loading.
    By default, they take names such as ``pop0`` or ``proj2``, we advise setting explicitly a name in their constructor for readability.

    If you add a parameter name to the JSON file but it does not exist in te neuron/synapse, it will be silently skipped.
    Enable ``verbose=True`` to see which parameters are effectively changed.

    If you set ``global_only`` to True, you will be able to set values for non-global parameters (e.g. synapse-specific), but a single value will be loaded for all.
    The JSON file cannot contain arrays.

    If you want to save/load the value of variables after a simulation, please refer to ``save()`` or ``load()``.

    :param filename: path to the JSON file.
    :param global_only: True if only global parameters (flags ``population`` and ``projection``) should be loaded, the other values are ignored. (default: True)
    :param verbose: True if the old and new values of the parameters should be printed (default: False).
    :param net_id: ID of the network (default: 0, the global network).
    :return: a dictionary of additional parameters not related to populations or projections (keyword ``network`` in the JSON file).

    """
    import json
    try:
        with open(filename, 'r') as rfile:
            desc = json.load(rfile)
    except IOError as error:
        print(error)
        Global._error("load_parameters(): the json file does not exist")   

    if verbose:
        Global._print('Loading parameters from file', filename)
        Global._print('-'*40)

    # Populations
    try:
        populations = desc['populations']
    except:
        populations = {}
        if verbose:
            Global._print('load_parameters(): no population parameters.')
    for name, parameters in populations.items():
        # Get the population
        for pop in Global._network[net_id]['populations']:
            if pop.name == name:
                population = pop
                break
        else:
            Global._warning('The population', name, 'defined in the file', filename, 'does not exist in the current network.')

        if verbose:
            Global._print('Population', name)

        # Set the parameters
        for name, val in parameters.items():
            # Check that the variable indeed exists
            if not name in population.parameters:
                Global._print('  ', name, 'is not a global parameter of', population.name, ', skipping.')
                continue
            if global_only and not name in population.neuron_type.description['global']:
                Global._print('  ', name, 'is not a global parameter of', population.name, ', skipping.')
                continue

            if verbose:
                Global._print('  ', name, ':', population.get(name), '->', val)

            population.set({name: float(val)})

    # Projections
    try:
        projections = desc['projections']
    except:
        projections = {}
        if verbose:
            Global._print('load_parameters(): no projection parameters.')
    for name, parameters in projections.items():
        # Get the projection
        for proj in Global._network[net_id]['projections']:
            if proj.name == name:
                projection = proj
                break
        else:
            Global._warning('The projection', name, 'defined in the file', filename, 'does not exist in the current network.')

        if verbose:
            Global._print('Projection', name)

        # Set the parameters
        for name, val in parameters.items():
            # Check that the variable indeed exists
            if not name in projection.parameters:
                Global._print('  ', name, 'is not a global parameter of', population.name, ', skipping.')
                continue
            if global_only and not name in projection.synapse_type.description['global']:
                Global._print('  ', name, 'is not a global parameter of', population.name, ', skipping.')
                continue

            if verbose:
                Global._print('  ', name, ':', projection.get(name), '->', val)

            projection.set({name: float(val)})

    # Constants
    try:
        constants = desc['constants']
    except:
        constants = {}
        if verbose:
            Global._print('load_parameters(): no constants.')
    for name, value in constants.items():
        if name in Global.list_constants(): # modify it
            Global.get_constant(name).value = value
        else: # create it
            _ = Global.Constant(name, value)

    # Global user-defined parameters
    try:
        network_parameters = {}
        for name, val in desc['network'].items():
            network_parameters[name] = float(val)
    except:
        network_parameters = {}

    return network_parameters