Package: napari-turing
napari-turingA plugin to run simple simulations of Turing patternsThisnapariplugin was generated withCookiecutterusing@napari'scookiecutter-napari-plugintemplate.InstallationYou can installnapari-turingviapipafter downloading the content ofpip install napari-turingTo install latest version :pip install git+https://github.com/leoguignard/napari-turing.gitTroubleshootingIf the installation does not work just with the previous command, it might be useful to first installnaparifor example that way:conda install napariCreating a new modelTo create your own model, you can use the template for modelshere.Note that a bit of knowledge in Python is probably necessary and it might not be completely trivial at first but you'll manage :)First you need to name the concentrations that you will use with the followingline:_concentration_names=["A","I"]Then, you need to declare its variables. For example you can create a parameter namedmu_athe following way (here in the code):mu_a=ModelParameter(name="mu_a",# Name of the parameterdescription="Activator diffusion coefficient (10^-4)",# Description of the parameter for naparivalue=2.8,# Initial and default valuemin=1,# Minimum value the parameter can takemax=5,# Maximum value the parameter can takeexponent=1e-4,# All values given to this instance of the class will but multiplied by this value)Then you need to list the parameters that are necessary to run the model (usually all the paramaters declared previously) and the parameters that you will allow the user to tune (for example, sometimes some of the parameters are co-dependent and there is no point in being able to tune both of them). That should be done the following way (here in the code):# These are the parameters that are necessary to run the equations._necessary_parameters=[tau,k,mu_a,mu_i]# These are the parameters that can be modified via napari_tunable_parameters=_necessary_parametersIf you want, you can specify what the method will return as a string, it will be displayed in the napari viewer (here in the code):# This function allows to display some information about the model# in naparidef__str__(self)->str:return("Equations (FitzHugh-Nagumo model):\n""  Concentration of Activator (a) and Inhibitor (i)\n""    - da/dt = mu_a * diffusion(a) + a - a^3 - i + k\n""    - tau * di/dt = mu_i * diffusion(i) + a - i")Now that the basics are declared, you will need to declare how to initialize your concentrations the following way (here in the code):# The following allows to reset the values of the concentrations.# The function takes the name of the concentration to initialize.# If no name is given or if it is None all the concentrations are# reinitialized.## The reason why this function is useful is that some models# require specific initialisations for them to work correctly# In the following example the concentrations are reintinalized# to a random value between -1 and 1.# This is the default behavior, so if you don't need to change# it you don't have to implement the function.definit_concentrations(self,C:Optional[str]=None)->None:ifCisNone:forciinself.concentration_names():self[ci]=np.random.random((self.size,self.size))*2-1else:self[C]=np.random.random((self.size,self.size))*2-1In the previous example, the all concentrations are initialized the same way. If you need to have different initializations, you can do it the following way for example (from the GrayScott model):definit_concentrations(self,C:Optional[str]=None)->None:ifC=="X"orCisNone:self["X"]=np.ones((self.size,self.size))ifC=="Y"orCisNone:Y=np.zeros((self.size,self.size))nb_pos=20pos=(np.random.random((2,nb_pos))*self.size).astype(int)Y[pos[0],pos[1]]=1self["Y"]=YIn this model, there are two concentrations,XandYwhich are initialized differenty. Note that they can be accessed usingself["X"]orself.X.Finally, you of course have to define the reaction equations and the diffusion equations. The way it is defined is with two functions, one for the reaction and one for the diffusion, that take as an input the name of the concentration to apply the function to and returns the new values. Then for each of your concentrations, their new values will be computed as followed:new_concentration=current_concentration+dt*(reaction+diffusion)Here is an example for the reaction function (here in the code):# This function defines the equations of the reactions.# It takes as an input which concentration to compute# (in this example we have to define how to compute A and I)def_reaction(self,c:str)->np.ndarray:ifc=="A":# Below is the reaction part of the equation (1)returnself.A-self.A**3-self.I+self.kelifc=="I":# Below is the reaction part of the equation (2)return(self.A-self.I)/self.tauOf course, if you have more concentrations, you will need to define more equations.Here is an example for the reaction function (here in the code):# This function defines the equations of the diffusion.# It takes as an input which concentration to compute# (in this example we have to define how to compute A and I)# Here we compute the diffusion as follow:# A cell gives an equal fraction mu of its concentration to its neighbors# A cell recieves an equal fraction mu of concentration from its neighbors# Neighbors = (left, right, above, below)# In the case of oriented diffusion the amount recieved and given to the neighbors# is imbalanced according to the position of the neighbor.def_diffusion(self,c:str)->np.ndarray:ifc=="A":arr=self.A# Define the array of concentrations to diffuse for the reageant Amu=self.mu_a# Define the diffusion coefficient for the reageant Aelifc=="I":arr=self.I# Define the array of concentrations to diffuse for the reageant Imu=self.mu_i# Define the diffusion coefficient for the reageant I# Computes what is recieved from neighboring cellsfrom_cell=convolve(arr,self.kernel.value,mode="constant",cval=0)# Computes what is given to neighboring cellsto_cell=self.nb_neighbs*arr# Computes the diffusionout=mu*(from_cell-to_cell)/(self.dx*self.dy)# In our case, the equation (2), for I specify that it has to be divided by tauifc=="I":out/=self.taureturnoutThe diffusion function is usually a standard one so it might not be necessary to overly change it.You can find other model examples:BrusselatorGrayScottGameOfLifeOnce all that is done, let say you've saved your new model in the folderModelsunder the nameNewModel.pyand the model class created is nameNewModel. Then you need to declare you model in the_model_list.pyfile. To do so you need to add the following lines in the file:fromenumimportEnumfrom.FitzHughNagumoimportFitzHughNagumofrom.BrusselatorimportBrusselatorfrom.GrayScottimportGrayScottfrom.GameOfLifeimportGameOfLifefrom.NewModelimportNewModel## THAT LINE HEREclassAvailableModels(Enum):FitzHughNagumo=FitzHughNagumoBrusselator=BrusselatorGrayScott=GrayScottGameOfLife=GameOfLifeNewModel=NewModel## AND THAT OTHER LINE HEREContributingContributions are very welcome.LicenseDistributed under the terms of theMITlicense,
"napari-turing" is free and open source softwareIssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.