Skip to content

Commit 19e9ade

Browse files
authored
feat: switch timestamps (sim BroadbandSource) to nanoseconds (#49)
BREAKING CHANGE: switch generated timestamps from us to ns
1 parent 9f926c6 commit 19e9ade

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

synapse/simulator/nodes/broadband_source.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,23 @@ async def run(self):
5353
bit_width = c.bit_width if c.bit_width else 4
5454
sample_rate_hz = c.sample_rate_hz if c.sample_rate_hz else 16000
5555

56-
t0 = time.time_ns() // 1000
56+
t_last_ns = time.time_ns()
5757
while self.running:
58-
now = time.time_ns() // 1000
59-
elapsed = now - t0
60-
n_samples = int(sample_rate_hz * elapsed / 1e6)
58+
await asyncio.sleep(0.01)
59+
60+
now = time.time_ns()
61+
elapsed_ns = now - t_last_ns
62+
n_samples = int(sample_rate_hz * elapsed_ns / 1e9)
63+
64+
samples = [[ch.id, [r_sample(bit_width) for _ in range(n_samples)]] for ch in channels]
6165

6266
data = ElectricalBroadbandData(
6367
bit_width=bit_width,
6468
is_signed=False,
6569
sample_rate=sample_rate_hz,
66-
t0=t0,
67-
samples=[[ch.id, [r_sample(bit_width) for _ in range(n_samples)]] for ch in channels]
70+
t0=t_last_ns,
71+
samples=samples
6872
)
6973

7074
await self.emit_data(data)
71-
72-
t0 = now
73-
74-
await asyncio.sleep(0.100)
75+
t_last_ns = now

synapse/simulator/nodes/spike_source.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ async def run(self):
5656
min_spikes = max(0, int(1 * window_s))
5757
max_spikes = min(15, int(200 * window_s))
5858

59-
t0 = time.time_ns() // 1000
59+
t0 = time.time_ns()
6060
while self.running:
61-
now = time.time_ns() // 1000
61+
now = time.time_ns()
6262

6363
spike_counts = [random.randint(min_spikes, max_spikes) for _ in channels]
6464
data = SpiketrainData(
@@ -67,7 +67,6 @@ async def run(self):
6767
spike_counts=spike_counts
6868
)
6969

70-
7170
await self.emit_data(data)
7271

7372
t0 = now

synapse/utils/ndtp_types.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@ def chunk_channel_data(bit_width: int, ch_data: List[float], max_payload_size_by
2424
yield ch_data[start_idx:end_idx]
2525

2626
class ElectricalBroadbandData:
27+
"""Electrical broadband data from neural recordings.
28+
29+
Attributes:
30+
t0 (int): Start timestamp in nanoseconds
31+
is_signed (bool): Whether the data is represented using signed integers
32+
bit_width (int): Number of bits used to represent each sample
33+
samples (Tuple[int, List[float]]): Tuple of (channel_id, data_samples)
34+
sample_rate (float): Sample rate in Hz
35+
"""
2736
__slots__ = ["data_type", "t0", "is_signed", "bit_width", "samples", "sample_rate"]
2837

2938
def __init__(self, t0, bit_width, samples: Tuple[int, List[float]], sample_rate, is_signed=True):
3039
self.data_type = DataType.kBroadband
31-
self.t0 = t0
40+
41+
self.t0 = t0 # ns
3242
self.is_signed = is_signed
3343
self.bit_width = bit_width
3444
self.samples = samples
@@ -104,13 +114,19 @@ def to_list(self):
104114
],
105115
]
106116

107-
108117
class SpiketrainData:
118+
"""Binned spike train data from neural recordings.
119+
120+
Attributes:
121+
t0 (int): Start timestamp in nanoseconds
122+
bin_size_ms (float): Size of each time bin in milliseconds
123+
spike_counts (List[int]): Number of spikes in each time bin
124+
"""
109125
__slots__ = ["data_type", "t0", "bin_size_ms", "spike_counts"]
110126

111127
def __init__(self, t0, bin_size_ms, spike_counts):
112128
self.data_type = DataType.kSpiketrain
113-
self.t0 = t0
129+
self.t0 = t0 # ns
114130
self.bin_size_ms = bin_size_ms
115131
self.spike_counts = spike_counts
116132

0 commit comments

Comments
 (0)