poc: portable network state serialization (.brian format)#1799
Draft
Sanchit2662 wants to merge 1 commit into
Draft
poc: portable network state serialization (.brian format)#1799Sanchit2662 wants to merge 1 commit into
Sanchit2662 wants to merge 1 commit into
Conversation
Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
Member
|
Hi @Sanchit2662, given that this project is not part of this year's GSoC I'll assume that we can close the PR for now? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
This is a proof-of-concept for the GSoC 2026 project on serialization/deserialization for Brian2.
I'm submitting it early to get feedback on the core approach before building the full implementation on top of it.
What I built
Three new files:
brian2/serialization/brian_format.pyThe core module. Two functions:
serialize_network_state(net, filepath), takes a running Network, walks all its objects, and writes a.brianZIP archive to diskrestore_network_state(net, filepath), reads that archive and restores every object back to the exact state it was inexamples/serialization/brian_format_demo.pyA runnable end-to-end demo.
It builds a LIF network (NeuronGroup + Synapses with per-neuron delays + StateMonitor + SpikeMonitor), runs 5 ms, serializes the state, resets the network to t=0 using the existing pickle store/restore, restores from the
.brianarchive to t=5 ms, runs another 5 ms, and checks that the final traces are numerically identical to a reference run that ran straight through to 10 ms.Max error is
0.00e+00.README_POC.mdExplains what the PoC covers, what it doesn't, and how to run it.
How it works
The existing
Network.store()already has a method_full_state()that walks all objects in the network and collects their array state. The current code just pickles that and writes it to disk.I'm calling the same
_full_state()but writing the output differently.The
.brianarchive is a ZIP with three files inside:metadata.jsonHuman-readable.
Contains the simulation time, and for every object and every variable:
Dimension._dimsthe 7 SI base units)This means the archive carries unit information that pickle never exposed.
arrays.npzAll the actual numerical data.
"ObjectName__varname"spikequeues.jsonThis one is important.
SynapticPathway._full_state()returns the Cython SpikeQueue state as(offset, list_of_lists)the in-flight spikes that are mid-delay at the moment of serialization.I convert this to a JSON dict so it survives the round-trip without pickle.
Without this, restoring a network mid-simulation would silently drop spikes that haven't been delivered yet.
For restore:
_restore_from_full_state()expects_full_state()doesobj._restore_from_full_state()on each oneNo changes to any existing Brian2 internals needed.
What this isn't
This is only the RuntimeDevice path.
It does not yet handle
CPPStandaloneDevicethat's where the realNotImplementedErrorlives.The plan for that is the same format, but arrays are read from:
via
get_array_filename(var)after the binary has run, instead of from live NumPy arrays.The archive layout stays identical.
This also does not store model structure equations, thresholds, connectivity conditions.
The network objects have to already exist before you can restore into them.
That's the same contract as the existing
Network.store/restore.The structural side (BrianExporter, BrianImporter) is a separate deliverable.
Questions for reviewers
Is
brian2/serialization/the right place for this, or should it live insidebrian2/importexport/next to the existingImportExportregistry?I'm reading
Dimension._dimsdirectly to serialize unit information.Is there a more stable public-facing API I should use instead?
For the CPP standalone path is reading from
results/afterhas_been_runis set the right hook point, or is there somewhere better in the device lifecycle to intercept this?