lava icon indicating copy to clipboard operation
lava copied to clipboard

Runtime Jupyter Notebook error on Windows operating System

Open ahmetozleraktas opened this issue 2 years ago • 0 comments

Referred to: #257

Objective of issue: Lava codes can not be run on Windows operating system (Jupyter Notebooks do not work, somehow ran as python script but it takes too long). (Development environment: Windows 11 and Windows 10, Vscode Jupyter Notebook extension and Jupyter Notebook itself.)

Lava version:

  • [x] 0.4.0

I'm submitting a ...

  • [x] bug report

Current behavior:

  • On Jupyter Notebook, runtime goes to infinity and can not run LIF tutorial. Also, on a .py, file it takes few minutes for Izhikevich model (single neuron). (EDIT: The codes have been tried on Ubuntu and Windows. In Ubuntu case the code worked fine and results were received quickly. On Windows, the problem persists.)

Expected behavior:

  • Runtime should normally be very short and can be run on Jupyter Notebook.

Steps to reproduce:

  • Following the steps on LIF tutorial and Izhikevich Model code that I added at the end of issue.

Related code:

from re import X
import numpy as np
from lava.magma.core.process.process import AbstractProcess
from lava.magma.core.process.variable import Var
from lava.magma.core.process.ports.ports import InPort, OutPort
from lava.magma.core.sync.protocols.loihi_protocol import LoihiProtocol
from lava.magma.core.model.py.ports import PyInPort, PyOutPort
from lava.magma.core.model.py.type import LavaPyType
from lava.magma.core.resources import CPU
from lava.magma.core.decorator import implements, requires, tag
from lava.magma.core.model.py.model import PyLoihiProcessModel
from lava.magma.core.run_configs import Loihi1SimCfg
from lava.magma.core.run_conditions import RunSteps
from lava.proc.monitor.process import Monitor
import matplotlib.pyplot as plt


#Defining the process
class Izhikevich(AbstractProcess):
    """Izhikevich neuron model"""

    # You can choose different neuron types when you creating the process
    neuron_types = {
        "RS": { # Regular spiking
            "a": 0.02,
            "b": 0.2,
            "c": -65,
            "d": 8,
        },
        "IB": { # Intrinsically bursting
            "a": 0.02,
            "b": 0.2,
            "c": -55,
            "d": 4,
        },
        "CH": { # Chattering
            "a": 0.02,
            "b": 0.2,
            "c": -50,
            "d": 2,
        },
        "FS": { # Fast spiking
            "a": 0.1,
            "b": 0.2,
            "c": -65,
            "d": 2,
        },
        "LTS": {  # Low-threshold spiking
            "a": 0.02,
            "b": 0.25,
            "c": -65,
            "d": 2,
        },
        "TC": { # Thalamo-cortical
            "a": 0.02,
            "b": 0.25,
            "c": -65,
            "d": 0.05,
        },
        "RZ": { # Resonator
            "a": 0.1,
            "b": 0.26,
            "c": -65,
            "d": 2,
        }
    }


    def __init__(self, neuron_type, sim_time, n_neurons, randomize=False,**kwargs):
        super().__init__()

        # accessing to the neuron types dictionary.
        self.param_dict = self.neuron_types[neuron_type]


        shape = kwargs.get("shape", (n_neurons,))
        self.a_in = InPort(shape=shape)
        self.s_out = OutPort(shape=shape)

        # Assigning the parameters of the neuron model according to the state of the randomize, if randomize is true, the parameters are assigned randomly.
        self.a = Var(shape=shape, init=self.param_dict["a"] + 0.02*np.random.uniform(-1,1, (n_neurons,)) if randomize else self.param_dict["a"])
        self.b = Var(shape=shape, init=self.param_dict["b"] + 0.02*np.random.uniform(-1,1, (n_neurons,)) if randomize else self.param_dict["b"])
        self.c = Var(shape=shape, init=self.param_dict["c"] + 0.02*np.random.uniform(-1,1, (n_neurons,)) if randomize else self.param_dict["c"])
        self.d = Var(shape=shape, init=self.param_dict["d"] + 0.02*np.random.uniform(-1,1, (n_neurons,)) if randomize else self.param_dict["d"])

        # Other variables of the neuron model, you can change the initial values of the variables when creating the process.
        self.I = Var(shape=(1,1), init=kwargs.pop("I", 5))
        self.u = Var(shape=shape, init=0)
        self.v = Var(shape=shape, init=-65)
        self.vth = Var(shape=(1,1), init=kwargs.pop("vth", 30))

        # Defining the time step
        self.step_size = Var(shape=(1,1), init=kwargs.pop("step_size", 0.1))

        # Defining the simulation time (miliseconds)
        self.sim_time = sim_time

        

    
    
    # Print function for debugging
    def print_vars(self):
        """Prints all variables of a Izhikevich process and their values."""

        print("Variables of the Izhikevich:")
        print("a: ", str(self.a.get()))
        print("b: ", str(self.b.get()))
        print("c: ", str(self.c.get()))
        print("d: ", str(self.d.get()))
        print("I: ", str(self.I.get()))
        print("u: ", str(self.u.get()))
        print("v: ", str(self.v.get()))
        print("vth: ", str(self.vth.get()))
        
    
        

@implements(proc=Izhikevich, protocol=LoihiProtocol) # Defining the protocol
@requires(CPU) # Defining the resources
@tag('floating_pt') # Defining the tag

# Defining ProcessModel (behaviour of Process)
class PyIzhikevichModel(PyLoihiProcessModel):

    # Define types of elements in the process
    a_in: PyInPort = LavaPyType(PyInPort.VEC_DENSE, float)
    s_out: PyOutPort = LavaPyType(PyOutPort.VEC_DENSE, bool, precision=1)
    u: np.ndarray = LavaPyType(np.ndarray, float)
    v: np.ndarray = LavaPyType(np.ndarray, float)
    vth: float = LavaPyType(float, float)
    a: np.ndarray = LavaPyType(np.ndarray, float)
    b: np.ndarray = LavaPyType(np.ndarray, float)
    c: np.ndarray = LavaPyType(np.ndarray, float)
    d: np.ndarray = LavaPyType(np.ndarray, float)
    I: float = LavaPyType(float, float)
    step_size: float = LavaPyType(float, float)
    
    # Define the equations of neuron
    def run_spk(self):
        a_in_data = self.a_in.recv()
        self.v = self.v + (0.04*self.v**2 + 5*self.v + 140 - self.u + self.I)*self.step_size
        self.u = self.u + (self.a)*((self.b*self.v - self.u))*self.step_size
        s_out = self.v >= self.vth # spike condition
        self.v[s_out] = self.c[s_out] # Reset voltage to c value
        self.u[s_out] = self.u[s_out] + self.d[s_out] # Reset u to d value
        self.s_out.send(s_out)





if __name__ == '__main__':        

    n_neurons = 1 # Number of neurons in the network
    neuron_type = "CH" # Type of neuron

    # Creating the process
    izhkievich_neurons = Izhikevich(sim_time=300, n_neurons=n_neurons, neuron_type=neuron_type, I=5,step_size=0.1, vth=30, randomize=True)

    # Access to simulation time and step size for plotting
    sim_time = izhkievich_neurons.sim_time
    step_size = izhkievich_neurons.step_size.get()

    # calculate the number of steps for the simulation
    num_steps = int(sim_time/step_size)

    # Add monitor for the measurement of the voltage
    monitor1 = Monitor()
    monitor1.probe(target=izhkievich_neurons.v, num_steps=num_steps)

    # start the simulation
    izhkievich_neurons.run(condition=RunSteps(num_steps=num_steps), run_cfg=Loihi1SimCfg())

    # collect the data from the monitor

    # stop the simulation
    izhkievich_neurons.stop()
    fig, ax =  plt.subplot(fig_size=(10,10))
    monitor1.plot(ax=ax, target=izhkievich_neurons.v)

ahmetozleraktas avatar Oct 07 '22 22:10 ahmetozleraktas