Skip to content

Commit

Permalink
Merge pull request #5 from CerebusOSS/cboulay/spkhps
Browse files Browse the repository at this point in the history
Enable setting spike hoops from API
  • Loading branch information
cboulay authored Mar 10, 2024
2 parents 5af9dd3 + 6bf46a4 commit 8a711b4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
32 changes: 31 additions & 1 deletion pycbsdk/cbhw/device/nsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
CBNPlayMode,
CBNPlayFlag,
CBSpecialChan,
CBHoop
)
from pycbsdk.cbhw.params import Params
from pycbsdk.cbhw.consts import CBError
Expand Down Expand Up @@ -528,6 +529,29 @@ def _configure_channel_autothreshold(self, chid: int, attr_value: int):
pkt.spkopts |= CBAInpSpk.THRAUTO.value if attr_value else 0
self._send_packet(pkt)

def _configure_channel_hoops(self, chid: int, attr_value: dict):
"""
Args:
chid: 1-based channel index
attr_value: a dictionary of dictionaries of dictionaries.
The outer dictionary keys are the 1-based unit ids.
The middle dictionary keys are the 1-based hoop ids.
The inner dictionary has fields 'time', 'min', and 'max'.
e.g. attr_value = {1: {
1: {'time': 13, 'min': -975, 'max': -646},
2: {'time': 6, 'min': 108, 'max': 342}
}
This will set the first two hoops for unit-1.
"""
pkt = copy.copy(self._config["channel_infos"][chid])
pkt.header.type = CBPacketType.CHANSETSPKHPS
for un_id, hoop_dicts in attr_value.items():
for hp_id, hp in hoop_dicts.items():
pkt.spkhoops[un_id-1][hp_id-1] = CBHoop(
valid=1, time=hp["time"], min=hp["min"], max=hp["max"]
)
self._send_packet(pkt)

def _configure_channel_label(self, chid: int, attr_value: str):
pkt = copy.copy(self._config["channel_infos"][chid])
pkt.header.type = CBPacketType.CHANSETLABEL
Expand Down Expand Up @@ -556,7 +580,11 @@ def _configure_channel_enable_spike(self, chid: int, attr_value: bool):
pkt = copy.copy(self._config["channel_infos"][chid])
pkt.header.type = CBPacketType.CHANSETSPK
pkt.spkopts &= ~CBAInpSpk.EXTRACT.value
pkt.spkopts |= CBAInpSpk.EXTRACT.value if attr_value else 0
if attr_value:
pkt.spkopts |= CBAInpSpk.EXTRACT.value if attr_value else 0
# Also reset sorting. Enable hoops by default.
pkt.spkopts &= ~CBAInpSpk.ALLSORT.value
pkt.spkopts |= CBAInpSpk.HOOPSORT.value
self._send_packet(pkt, self._config_events["chaninfo"])

def configure_channel(self, chid: int, attr_name: str, attr_value):
Expand All @@ -582,6 +610,8 @@ def configure_channel_spike(self, chid: int, attr_name: str, attr_value):
self._configure_channel_enable_spike(chid, attr_value)
elif attr_name.lower().startswith("autothresh"):
self._configure_channel_autothreshold(chid, attr_value)
elif attr_name.lower().startswith("hoops"):
self._configure_channel_hoops(chid, attr_value)
# self._config_events["chaninfo"].wait(timeout=0.02)

def configure_all_channels_spike(
Expand Down
2 changes: 1 addition & 1 deletion pycbsdk/cbhw/packet/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class CBPacketNTrodeInfo(CBPacketConfigFixed):
"_label",
c_char * 16,
), # Label of the Ntrode (null terminated if < 16 characters)
("ellipses", CBManualUnitMapping * 6 * 5),
("ellipses", CBManualUnitMapping * 5 * 6),
(
"nSite",
c_uint16,
Expand Down
2 changes: 1 addition & 1 deletion pycbsdk/cbhw/packet/v311.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class CBPacketChanInfo(CBPacketConfigFixed):
("amplrejneg", c_int16), # Amplitude rejection negative value
("refelecchan", c_uint32), # Software reference electrode channel
("unitmapping", CBManualUnitMapping * 5), # manual unit mapping
("spkhoops", CBHoop * 5 * 4), # spike hoop sorting set
("spkhoops", CBHoop * 4 * 5), # spike hoop sorting set
]

@property
Expand Down
2 changes: 1 addition & 1 deletion pycbsdk/cbhw/packet/v41.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class CBPacketChanInfo(CBPacketConfigFixed):
("amplrejneg", c_int16), # Amplitude rejection negative value
("refelecchan", c_uint32), # Software reference electrode channel
("unitmapping", CBManualUnitMapping * 5), # manual unit mapping
("spkhoops", CBHoop * 5 * 4), # spike hoop sorting set
("spkhoops", CBHoop * 4 * 5), # spike hoop sorting set
]

@property
Expand Down
19 changes: 19 additions & 0 deletions pycbsdk/examples/print_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def main(
loglevel: str = "debug",
skip_startup: bool = False,
update_interval: float = 1.0,
set_hoops: bool = False
):
"""
Run the application:
Expand All @@ -87,6 +88,7 @@ def main(
:param loglevel: debug, info, or warning
:param skip_startup: Skip the initial handshake as well as the attempt to set the device to RUNNING.
:param update_interval: Interval between updates. This determines how big the queues can grow.
:param set_hoops: set True to enable hoop-based sorting on channel 2.
:return:
"""
# Handle logger arguments
Expand Down Expand Up @@ -152,6 +154,23 @@ def main(
cbsdk.set_all_channels_disable(nsp_obj, ch_type)
cbsdk.set_all_channels_spk_config(nsp_obj, ch_type, "enable", True)

if set_hoops:
spk_hoops = {
1: {
1: {"time": 13, "min": -975, "max": -646},
2: {"time": 6, "min": 108, "max": 342},
},
2: {
1: {"time": 21, "min": 675, "max": 1033},
2: {"time": 31, "min": -538, "max": -185},
},
3: {
1: {"time": 17, "min": 481, "max": 820},
2: {"time": 35, "min": -23, "max": 262},
},
}
cbsdk.set_channel_spk_config(nsp_obj, 2, "hoops", spk_hoops)

# Count the number of FrontEnd | AnalogIn channels.
b_spk = [
_ in [CBChannelType.FrontEnd, CBChannelType.AnalogIn]
Expand Down

0 comments on commit 8a711b4

Please sign in to comment.