Skip to content

Commit

Permalink
minor changes in results and postproc
Browse files Browse the repository at this point in the history
  • Loading branch information
louisreg committed Nov 20, 2024
1 parent 6ec0318 commit 72d7f6f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
65 changes: 45 additions & 20 deletions nrv/nmod/results/_axons_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def rasterize(
if len(t_data) :
return (x_idx_AP, x_data[x_idx_AP], t_idx_AP, t_data[t_idx_AP])
else:
return ([], [], [], [])
return (np.array([]), np.array([]), np.array([]), np.array([]))


def get_first_AP(
Expand Down Expand Up @@ -208,7 +208,7 @@ def get_first_AP(
return (np.array(x_APs), [0], np.array(t_APs), [0], [0])

if len(t_APs) < 1:
return ([], [], [], [], [])
return (np.array([]), np.array([]), np.array([]), np.array([]), np.array([]))

ordered_t_idx = np.argsort(t_APs) # sort time indexes

Expand All @@ -222,8 +222,12 @@ def get_first_AP(
t_min = t_refract
x_ordered_unique = x_ordered[idx_unique]
x_ordered_unique.sort()
if len(x_ordered_unique) < 2:
return (np.array([]), np.array([]), np.array([]), np.array([]), np.array([]))

x_min = np.min(np.abs(np.diff(x_ordered_unique))) * 5


msk_unique = np.arange(len(t_ordered) - 1)
msk_unique = [i for i in msk_unique if i not in idx_unique]

Expand Down Expand Up @@ -387,12 +391,14 @@ def is_blocked(
rise_warning(
f"is_blocked in Axon {self.ID}: No AP detected after the test pulse. Make sure the stimulus is strong enough, attached to the axon or not colliding with onset response. \n ... Fiber considered as blocked."
)
self["blocked"] = True
self["blocked"] = False
return self["blocked"]
if n_APs > 2:
rise_warning(
f"is_blocked in Axon {self.ID}: More than two APs are detected after the test pulse, likely due to onset response. This can cause erroneous block state estimation. \n ... Consider increasing axon's spatial discretization."
)
self["blocked"] = False
return self["blocked"]

if self.has_AP_reached_end(
x_AP_test, t_AP_test
Expand Down Expand Up @@ -459,7 +465,8 @@ def split_APs(
list of the time index of each AP
"""
# if not vm_key + "_raster_x_position" in self:
self.rasterize(vm_key=vm_key, clear_artifacts=False)
if not vm_key + "_raster_x_position" in self:
self.rasterize(vm_key=vm_key, clear_artifacts=False)
x_APs = []
x_idx_APs = []
t_APs = []
Expand All @@ -476,23 +483,33 @@ def split_APs(
x_AP, x_idx_AP, t_AP, t_idx_AP, AP_idx = get_first_AP(
x_raster, x_idx_raster, t_raster, t_idx_raster
)
AP_len = np.max(
[
self.get_AP_upward_len(x_AP, t_AP),
self.get_AP_downward_len(x_AP, t_AP),
]
)
if len(x_AP) == 0:
AP_len = 0
else:
AP_len = np.max(
[
self.get_AP_upward_len(x_AP, t_AP),
self.get_AP_downward_len(x_AP, t_AP),
]
)
#print(f"upward: {self.get_AP_upward_len(x_AP, t_AP)} - downward: {self.get_AP_downward_len(x_AP, t_AP)}")
#plt.scatter(t_AP,x_AP)
if AP_len >= min_size:
x_APs.append(x_AP)
t_APs.append(t_AP)
x_idx_APs.append(x_idx_AP)
t_idx_APs.append(t_idx_AP)
x_raster = np.delete(x_raster, AP_idx)
t_raster = np.delete(t_raster, AP_idx)
x_idx_raster = np.delete(x_idx_raster, AP_idx)
t_idx_raster = np.delete(t_idx_raster, AP_idx)
if len(AP_idx):
x_raster = np.delete(x_raster, AP_idx)
t_raster = np.delete(t_raster, AP_idx)
x_idx_raster = np.delete(x_idx_raster, AP_idx)
t_idx_raster = np.delete(t_idx_raster, AP_idx)
else:
x_raster = np.array([])
t_raster = np.array([])
x_idx_raster = np.array([])
t_idx_raster = np.array([])

#plt.show()
#exit()
return (x_APs, x_idx_APs, t_APs, t_idx_APs)
Expand Down Expand Up @@ -1162,8 +1179,10 @@ def rasterize(

if "threshold" in kwargs:
threshold = kwargs["threshold"]
else:
elif "threshold" in self:
threshold = self["threshold"]
else:
threshold = 0

if "t_min_AP" in kwargs:
t_min_AP = kwargs["t_min_AP"]
Expand Down Expand Up @@ -1505,12 +1524,18 @@ def block_summary(
"""

if {"is_blocked", "has_onset", "n_onset"} not in self:
required_keys = {"V_mem"}
vm_key = "V_mem"
if freq is not None:
vm_key += "_filtered"
required_keys = {"{vm_key}_raster_position",
f"{vm_key}_raster_x_position",
f"{vm_key}_raster_time_index",
f"{vm_key}_raster_time"}
if required_keys in self:
vm_key = "V_mem"
if freq is not None:
self.filter_freq("V_mem", freq, Q=2)
vm_key += "_filtered"
#vm_key = "V_mem"
#if freq is not None:
# self.filter_freq("V_mem", freq, Q=2)
# vm_key += "_filtered"

blocked = self.is_blocked(AP_start, freq, t_refractory)
n_APs = self.count_APs(vm_key) - 1
Expand Down
19 changes: 11 additions & 8 deletions nrv/nmod/results/_fascicles_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def get_axons_key(self, ax_type: str = "all") -> list:
return axon_keys

def get_recruited_axons(
self, ax_type: str = "all", normalize: bool = False
self, ax_type: str = "all", normalize: bool = False,
vm_key: str = "V_mem", t_start: float = None
) -> int | float:
"""
Return the number or the ratio of recruited axons in the fascicle
Expand All @@ -77,14 +78,15 @@ def get_recruited_axons(
axons_keys = self.get_axons_key(ax_type)
n_recr = 0
for axon in axons_keys:
if self[axon].is_recruited():
if self[axon].is_recruited(vm_key, t_start):
n_recr += 1
if normalize:
n_recr /= self.get_n_ax(ax_type)
return n_recr

def get_recruited_axons_greater_than(
self, diam: float, ax_type: str = "all", normalize: bool = False
self, diam: float, ax_type: str = "all", normalize: bool = False,
vm_key: str = "V_mem", t_start: float = None
) -> float:
"""
Return the number or the ratio of recruited axons with a diameter greater than `diam` in the fascicle
Expand All @@ -110,14 +112,15 @@ def get_recruited_axons_greater_than(
for axon in axons_keys:
if self[axon].diameter > diam:
n_tot += 1
if self[axon].is_recruited():
if self[axon].is_recruited(vm_key,t_start):
n_recr += 1
if normalize:
n_recr /= n_tot
return n_recr

def get_recruited_axons_lesser_than(
self, diam: float, ax_type: str = "all", normalize: bool = False
self, diam: float, ax_type: str = "all", normalize: bool = False,
vm_key: str = "V_mem", t_start: float = None
) -> float:
"""
Return the number or the ratio of recruited axons with a diameter smaller than `diam` in the fascicle
Expand All @@ -143,21 +146,21 @@ def get_recruited_axons_lesser_than(
for axon in axons_keys:
if self[axon].diameter < diam:
n_tot += 1
if self[axon].is_recruited():
if self[axon].is_recruited(vm_key,t_start):
n_recr += 1
if normalize:
n_recr /= n_tot
return n_recr

def get_axons(self) -> list:
def get_axons(self,vm_key: str = "V_mem", t_start: float = None) -> list:
axons_keys = self.get_axons_key()
axon_diam = []
axon_type = []
axon_y = []
axon_z = []
axon_recruited = []
for axon in axons_keys:
axon_recruited.append(self[axon].is_recruited())
axon_recruited.append(self[axon].is_recruited(vm_key,t_start))
axon_y.append(self[axon].y)
axon_z.append(self[axon].z)
axon_diam.append(self[axon].diameter)
Expand Down
8 changes: 4 additions & 4 deletions nrv/ui/_axon_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,10 +1166,10 @@ def is_blocked(results:axon_results, AP_start:float|None=None, freq:float|None=N
list_keys = {
"ID",
"L",
#f"{vm_key}_raster_position",
#f"{vm_key}raster_x_position",
#f"{vm_key}_raster_time_index",
#f"{vm_key}_raster_time",
f"{vm_key}_raster_position",
f"{vm_key}_raster_x_position",
f"{vm_key}_raster_time_index",
f"{vm_key}_raster_time",
"myelinated",
"y",
"z",
Expand Down

0 comments on commit 72d7f6f

Please sign in to comment.