Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def write_sorting(sorting, save_path, overwrite=False):
assert HAVE_NIXIO, NIXIOSortingExtractor.installation_mesg
if os.path.exists(save_path) and not overwrite:
raise FileExistsError("File exists: {}".format(save_path))
sfreq = sorting.get_sampling_frequency()
if sfreq is None:
unit = None
elif sfreq == 1:
unit = "s"
else:
unit = "{} s".format(1./sfreq)
nf = nix.File.open(save_path, nix.FileMode.Overwrite)
# use the file name to name the top-level block
fname = os.path.basename(save_path)
block = nf.create_block(fname, "spikeinterface.sorting")
commonmd = nf.create_section(fname, "spikeinterface.sorting.metadata")
if sfreq is not None:
commonmd["sampling_frequency"] = sfreq
spikes_das = list()
for unit_id in sorting.get_unit_ids():
spikes = sorting.get_unit_spike_train(unit_id)
name = "spikes-{}".format(unit_id)
da = block.create_data_array(name, "spikeinterface.spikes",
data=spikes)
da.unit = unit
da.label = str(unit_id)
spikes_das.append(da)