Correcting poor code design
Posted: Mon May 03, 2021 10:39 am
Hi there!
First off, thank you for taking time to look into my question.
I started my project working mostly with the gui, and am now transitioning to working largely with python. When I was building my gui projects, I saved all my items (rig, UI, CellBuilder, etc) in one session file. I saved a new file multiple times a day, so I have a 100+ .ses files. I am now trying to correct that mistake. I wrote a quick python script to isolate the cell builder files, but when I run it through my jupyter notebook, my kernel consistently dies. This is a "load dependent" effect - if the file list is short or if I have less if statements, the kernel is fine. I'm not sure if this is a question you could answer, as it might be completely unrelated to neuron. However, I suspect there is a better way to clean up messy combined gui files, and I would appreciate any direction you might be able to provide. I have attached my code below. Please let me know if you need to see more for proper troubleshooting.
Many thanks again,
Alia
First off, thank you for taking time to look into my question.
I started my project working mostly with the gui, and am now transitioning to working largely with python. When I was building my gui projects, I saved all my items (rig, UI, CellBuilder, etc) in one session file. I saved a new file multiple times a day, so I have a 100+ .ses files. I am now trying to correct that mistake. I wrote a quick python script to isolate the cell builder files, but when I run it through my jupyter notebook, my kernel consistently dies. This is a "load dependent" effect - if the file list is short or if I have less if statements, the kernel is fine. I'm not sure if this is a question you could answer, as it might be completely unrelated to neuron. However, I suspect there is a better way to clean up messy combined gui files, and I would appreciate any direction you might be able to provide. I have attached my code below. Please let me know if you need to see more for proper troubleshooting.
Many thanks again,
Alia
Code: Select all
#imports
import os
import efel
import numpy as np
from neuron import h, nrn, gui
from neuron.units import ms, mV
import csv
directory_in_str ='/Users/pedersonam/ModelMOC/Alia/Today/AMP_model'
#Sim run protocol
def make_trace(h, stim, hold):
trace = {}
v = h.Vector().record(h.soma(0.5)._ref_v)# Membrane potential vector
t = h.Vector().record(h._ref_t)# Time stamp vector
h.finitialize(-60 * mV)
h.continuerun(1000 * ms)
# Set the 'V' (=voltage) key of the trace
trace['V'] = v
trace['T'] = t
# Set the 'stim_start' (time at which a stimulus starts, in ms)
# key of the trace
# Warning: this need to be a list (with one element)
trace['stim_start'] = [300]
# Set the 'stim_end' (time at which a stimulus end) key of the trace
# Warning: this need to be a list (with one element)
trace['stim_end'] = [stim.dur + stim.delay]
return trace
# globals
traces = []
filenames = []
directory = os.fsencode(directory_in_str)
#cleaning files - this is the problem area
def file_clean():
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith(".ses") and (not filename.endswith('cell.ses')):
h.load_file(filename)
p = h.PWManager()
count = p.count()
index = 0
while index < count:
if p.name(index).startswith('Point') :
p.close(index)
count = count-1
index = index - 1
elif p.name(index).startswith('I/V') :
p.close(index)
count = count-1
index = index - 1
elif p.name(index).startswith('Gr') :
p.close(index)
count = count-1
index = index - 1
else :
index = index + 1
h.save_session(filename.replace('.ses', '_cell.ses'))
else :
continue
#creating the rig
def rig_spec(h, traces):
soma = h.soma
if hasattr(soma, 'eh'):
for sec in h.allsec():
sec.eh = -38
# # Current clamp
stim = h.IClamp(h.soma(0.5))
stim.delay = 300 * ms
stim.dur = 500 * ms
stim.amp = 0.07
tstop = stim.delay+stim.dur+250
hold = h.IClamp(soma(0.5))
hold.delay = 0
hold.dur = tstop
hold.amp = 0.0121695
traces.append(make_trace(h, stim, hold))
return h
# kernel dies here
file_clean()
#actually running the simulation
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith("cell.ses") and filename.startswith('2'):
filenames.append(filename)
h.load_file(filename)
rig_spec(h, traces)