Skip to content

Commit

Permalink
fixing various bugs fmod + adding nerve_results.fasc_pties, units MV kV
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas couppey committed Dec 17, 2024
1 parent 6c12f5f commit 27b8296
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion nrv/fmod/FEM/fenics_utils/_FEMSimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def __init_domain(self):
if not self.domain_status:
# RECOVERING THE GEOMETRY
self.domain, self.subdomains, self.boundaries = read_gmsh(
self.mesh, comm=self.comm, rank=self.rank, gdim=3
self.mesh, comm=self.comm, rank=self.rank, gdim=self.D
)
# SPACE FOR INTEGRATION
if self.inbound:
Expand Down
2 changes: 1 addition & 1 deletion nrv/fmod/FEM/mesh_creator/_NerveMshCreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def save(
blacklist += [
"model",
]
if self.is_generated and save:
if self.is_generated and (save or mshfname is not None):
if mshfname is None:
mshfname = rmv_ext(fname) + ".msh"
super().save(fname=mshfname, generate=False)
Expand Down
8 changes: 5 additions & 3 deletions nrv/fmod/_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ def compute_PSA_anisotropic_footprint(
surface = surface = np.pi * (d / cm) * (np.gradient(x_axon) / cm)
d_internode = np.gradient(x_axon)

sx = sigma_yy * sigma_zz
sy = sigma_xx * sigma_zz
sz = sigma_xx * sigma_yy
#!! Modified tc 12/12/24 added **0.5
sx = (sigma_yy * sigma_zz)**.5
sy = (sigma_xx * sigma_zz)**.5
sz = (sigma_xx * sigma_yy)**.5

electrical_distance = (
4
Expand Down Expand Up @@ -670,6 +671,7 @@ def compute_footprints(self, x_axon, y_axon, z_axon, d, ID, myelinated=False):
self.sigma_xx,
self.sigma_yy,
self.sigma_zz,
True,
)
if point.method == "LSA":
if self.isotropic:
Expand Down
22 changes: 22 additions & 0 deletions nrv/nmod/results/_nerve_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ def axons_type(self) -> np.ndarray:
"""
return self.axons_pop_properties[:,[0,2]]

@property
def fasc_properties(self) -> np.ndarray:
"""
Porperties of axons population of each fascicles
Returns
-------
np.ndarray (self.n_ax, 6)
ndarray gathering, for all fascicles in the nerve, fascicle IDs, diameter and y and z positions.
"""
fasc_keys = self.fascicle_keys
_fasc = np.zeros((self.n_fasc, 4))
for i_fasc, key in enumerate(fasc_keys):
fasc_ = self[key]
_fasc[i_fasc, :] = np.array([
fasc_.ID,
fasc_.D,
fasc_.y_grav_center,
fasc_.z_grav_center,
])
return _fasc

@property
def axons_pop_properties(self) -> np.ndarray:
"""
Expand Down
39 changes: 22 additions & 17 deletions nrv/ui/_axon_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ def is_blocked(results:axon_results, AP_start:float|None=None, freq:float|None=N



def sample_keys(results:axon_results, keys_to_sample:str|set[str]={}, t_start_rec:float=0, t_stop_rec:float=-1, sample_dt:None|float=None, x_bounds:None|float|tuple[float]=None, keys_to_remove:str|set[str]={}, keys_to_keep:set[str]={})->axon_results:
def sample_keys(results:axon_results, keys_to_sample:str|set[str]={}, t_start_rec:float=0, t_stop_rec:float=-1, sample_dt:None|float=None, i_sampled_t:None|np.ndarray=None, x_bounds:None|float|tuple[float]=None, keys_to_remove:str|set[str]={}, keys_to_keep:set[str]={})->axon_results:
"""
Undersample the membrane coductivity (``results["g_mem"]``) key and remove most of the `axon_results` keys to alliviate RAM usage.
Expand Down Expand Up @@ -1232,38 +1232,43 @@ def sample_keys(results:axon_results, keys_to_sample:str|set[str]={}, t_start_re
if len(set(keys_to_sample) - set(results.keys())):
rise_error(set(keys_to_sample) - set(results.keys()), "keys are missing to apply postprocessing. Please check simulation parameters")
else:
if t_stop_rec < 0:
t_stop_rec=results.t_sim

if sample_dt is None:
sample_dt=results.dt

# x - sampling array
if x_bounds is None:
x_bounds=(0,results.L)

if np.iterable(x_bounds):
I_x = np.arange(len(results["x_rec"]))
x_bounds = (0, results["x_rec"][-1])
elif np.iterable(x_bounds):
I_x = np.argwhere((results["x_rec"]>x_bounds[0])&(results["x_rec"]<x_bounds[1]))[:,0]
else:
x_bounds = [x_bounds]
I_x = np.array([np.argmin(abs(results["x_rec"]-x_bounds[0]))])

N_x = len(I_x)
i_t_min = np.argwhere(results["t"]>t_start_rec)[0][0]
i_t_max = np.argwhere(results["t"]<t_stop_rec)[-1][0]

t_APs = [k for k in range(i_t_min,i_t_max)]
t_APs = t_APs[::int(sample_dt/results.dt)]
# t - sampling array
if i_sampled_t is not None:
t_APs = i_sampled_t
else:
if t_stop_rec < 0:
i_t_max = len(results["t"])
else:
i_t_max = np.argwhere(results["t"]<=t_stop_rec)[-1][0]
if sample_dt is None:
sample_dt=results.dt
i_t_min = np.argwhere(results["t"]>=t_start_rec)[0][0]

t_APs = [k for k in range(i_t_min,i_t_max)]
t_APs = t_APs[::int(sample_dt/results.dt)]

# Under sampling to reduce memory consumption
results["x_rec"] = results["x_rec"][I_x] - x_bounds[0]
results["t"] = results["t"][t_APs]
if "t" in results:
results["t"] = results["t"][t_APs]
for key in keys_to_sample:
results[key] = results[key][np.ix_(I_x, t_APs)]

###############################
## remove non nevessary data ##
###############################
list_keys = {
"ID",
"x_rec",
"rec",
"Nseg_per_sec",
Expand Down
4 changes: 3 additions & 1 deletion nrv/utils/_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- Existing
* - Voltage
- mV
- uV, V
- uV, V, kV, MV
* - Time
- ms
- us, s, minute, hour, day
Expand Down Expand Up @@ -54,6 +54,8 @@
######################
uV = 1e-3 * mV
V = 1e3 * mV
kV = 1e3 * V
MV = 1e3 * kV

#############################
## time prefixes and units ##
Expand Down

0 comments on commit 27b8296

Please sign in to comment.