-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
145 lines (120 loc) · 4.95 KB
/
Copy pathbasic.py
File metadata and controls
145 lines (120 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
"""
Python version of basic.cpp - Single Hodgkin-Huxley neuron simulation
This example demonstrates:
- Creating a Hodgkin-Huxley neuron with specific parameters
- Setting initial conditions for variables
- Running a simulation loop
- Accessing neuron state variables
Usage:
python3 basic.py # Print to stdout
python3 basic.py --output file.dat # Save data to file.dat
python3 basic.py --plot file.pdf # Save plot to file.pdf
python3 basic.py --output data.dat --plot sim.pdf # Both options
python3 basic.py --help # Show help
"""
import neun_py
import matplotlib.pyplot as plt
import numpy as np
import argparse
import sys
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(
description='Single Hodgkin-Huxley neuron simulation',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python3 basic.py # Print to stdout
python3 basic.py --output data.dat # Save data to file
python3 basic.py --plot sim.pdf # Save plot to PDF
python3 basic.py --output data.dat --plot sim.pdf # Both options
""")
parser.add_argument('--plot', metavar='file.pdf',
help='Plot simulation results to PDF file')
parser.add_argument('--output', metavar='file.dat',
help='Save voltage data to file')
args = parser.parse_args()
# Create constructor arguments for the neuron
neuron_args = neun_py.HHDoubleConstructorArgs()
# Create a Hodgkin-Huxley neuron with double precision and RungeKutta4 integrator
neuron = neun_py.HHDoubleRK4(neuron_args)
# Set the parameter values (equivalent to C++ version)
neuron.set_param(neun_py.HHDoubleParameter.cm, 1 * 7.854e-3)
neuron.set_param(neun_py.HHDoubleParameter.vna, 50)
neuron.set_param(neun_py.HHDoubleParameter.vk, -77)
neuron.set_param(neun_py.HHDoubleParameter.vl, -54.387)
neuron.set_param(neun_py.HHDoubleParameter.gna, 120 * 7.854e-3)
neuron.set_param(neun_py.HHDoubleParameter.gk, 36 * 7.854e-3)
neuron.set_param(neun_py.HHDoubleParameter.gl, 0.3 * 7.854e-3)
# Set initial conditions for the neuron variables
neuron.set(neun_py.HHDoubleVariable.v, -80)
neuron.set(neun_py.HHDoubleVariable.m, 0.1)
neuron.set(neun_py.HHDoubleVariable.n, 0.7)
neuron.set(neun_py.HHDoubleVariable.h, 0.01)
# Simulation parameters
step = 0.001 # Integration step
simulation_time = 100 # Total simulation time
# Storage for results
times = []
voltages = []
# Open output file if specified
output_file = None
if args.output:
try:
output_file = open(args.output, 'w')
output_file.write("# Time(ms) Voltage(mV)\n")
print(f"Saving voltage data to: {args.output}")
except IOError as e:
print(f"Error: Could not open output file '{args.output}': {e}", file=sys.stderr)
sys.exit(1)
# Perform the simulation
time = 0.0
while time < simulation_time:
# Step the neuron forward
neuron.add_synaptic_input(0.1)
neuron.step(step)
# Get current voltage
voltage = neuron.get(neun_py.HHDoubleVariable.v)
# Store results for plotting
times.append(time)
voltages.append(voltage)
# Output data
output_line = f"{time:.6f} {voltage:.6f}"
if output_file:
# Write to file
output_file.write(output_line + "\n")
else:
# Print to stdout (default behavior)
print(output_line)
time += step
# Close output file
if output_file:
output_file.close()
print(f"Data successfully written to: {args.output}")
# Generate plot if requested
if args.plot:
try:
plt.figure(figsize=(10, 6))
plt.plot(times, voltages, linewidth=1.5, color='blue')
plt.xlabel('Time (ms)')
plt.ylabel('Membrane Potential (mV)')
plt.title('Hodgkin-Huxley Neuron - Basic Simulation')
plt.grid(True, alpha=0.3)
plt.tight_layout()
# Save to PDF
plt.savefig(args.plot, format='pdf', dpi=300, bbox_inches='tight')
print(f"Plot successfully saved to: {args.plot}")
# Don't show interactive plot when saving to file
plt.close()
except Exception as e:
print(f"Error: Could not save plot to '{args.plot}': {e}", file=sys.stderr)
sys.exit(1)
# Show summary
if args.output or args.plot:
print(f"Simulation completed:")
print(f" - Duration: {simulation_time} ms")
print(f" - Time step: {step} ms")
print(f" - Data points: {len(times)}")
if __name__ == "__main__":
main()