Skip to content

Commit

Permalink
Changes towards NeuroML/jNeuroML#54
Browse files Browse the repository at this point in the history
Added support for dimension per_voltage2 in Neuron
  • Loading branch information
pgleeson committed Nov 16, 2017
1 parent 08cb1e0 commit e72b4c8
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/lemsml/export/dlems/DLemsWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,12 @@ public String getMainScript() throws LEMSException, IOException
return sw.toString();
}

private void writeDLemsForComponent(JsonGenerator g, Component comp) throws ContentError, JsonGenerationException, IOException
private void writeDLemsForComponent(JsonGenerator g, Component comp) throws ContentError, JsonGenerationException, IOException, LEMSException
{
writeDLemsForComponent(g, comp, null);
}

private void writeDLemsForComponent(JsonGenerator g, Component comp, HashMap<String, Set<String>> extraParameters) throws ContentError, JsonGenerationException, IOException
private void writeDLemsForComponent(JsonGenerator g, Component comp, HashMap<String, Set<String>> extraParameters) throws ContentError, JsonGenerationException, IOException, LEMSException
{
if (comp==null)
return;
Expand Down Expand Up @@ -674,7 +674,7 @@ private void writeDLemsForComponent(JsonGenerator g, Component comp, HashMap<Str

}

private String convertTime(ParamValue lemsValue) throws ContentError
private String convertTime(ParamValue lemsValue) throws ContentError, LEMSException
{
return unitConverter.convert((float)lemsValue.getDoubleValue(),TIME_DIM)+"";
}
Expand Down Expand Up @@ -924,7 +924,7 @@ private void writeStateFunctions(JsonGenerator g, Component comp) throws Content
}
}

private void writeParameters(JsonGenerator g, Component comp) throws ContentError, JsonGenerationException, IOException
private void writeParameters(JsonGenerator g, Component comp) throws ContentError, JsonGenerationException, IOException, LEMSException
{
ComponentType ct = comp.getComponentType();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/lemsml/export/dlems/UnitConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
*/
package org.lemsml.export.dlems;

import org.lemsml.jlems.core.sim.LEMSException;

/**
*
* @author padraig
*/
public interface UnitConverter
{
float convert(float siValue, String dimensionName);
float convert(float siValue, String dimensionName) throws LEMSException;
}
24 changes: 17 additions & 7 deletions src/main/java/org/neuroml/export/neuron/NRNUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.lemsml.export.dlems.UnitConverter;
import org.lemsml.jlems.core.expression.ParseError;
import org.lemsml.jlems.core.sim.ContentError;
import org.lemsml.jlems.core.sim.LEMSException;
import org.lemsml.jlems.core.type.Component;
import org.lemsml.jlems.core.type.Dimension;
import org.lemsml.jlems.core.type.DimensionalQuantity;
import org.lemsml.jlems.core.type.Lems;
import org.lemsml.jlems.core.type.QuantityReader;
import org.neuroml.export.utils.Utils;
import org.neuroml.model.util.NeuroMLElements;
import org.neuroml.model.util.NeuroMLException;

/**
* @author Boris Marin & Padraig Gleeson
Expand Down Expand Up @@ -221,7 +223,7 @@ protected static String checkForBinaryOperators(String expr)
return expr.replace("\\.gt\\.", ">").replace("\\.geq\\.", ">=").replace("\\.lt\\.", "<").replace("\\.leq\\.", "<=").replace("\\.and\\.", "&&").replace("\\.neq\\.", "!=");
}

protected static float getThreshold(Component comp, Lems lems) throws ParseError, ContentError
protected static float getThreshold(Component comp, Lems lems) throws ParseError, ContentError, LEMSException
{
float threshold = 0;
if(comp.getComponentType().isOrExtends(NeuroMLElements.BASE_IAF_CAP_CELL) ||
Expand Down Expand Up @@ -324,6 +326,10 @@ else if (dimensionName.equals("per_voltage"))
{
return "(/mV)";
}
else if (dimensionName.equals("per_voltage2"))
{
return "(/mV2)";
}
else if (dimensionName.equals("conductance"))
{
return "(uS)";
Expand Down Expand Up @@ -410,19 +416,19 @@ else if (dimensionName.equals(Dimension.NO_DIMENSION))
}
}

protected static float convertToNeuronUnits(String neuromlQuantity, Lems lems) throws ParseError, ContentError
protected static float convertToNeuronUnits(String neuromlQuantity, Lems lems) throws ParseError, ContentError, LEMSException
{
DimensionalQuantity dq = QuantityReader.parseValue(neuromlQuantity, lems.getUnits());
return convertToNeuronUnits((float)dq.getDoubleValue(), dq.getDimension().getName());
}

@Override
public float convert(float siValue, String dimensionName)
public float convert(float siValue, String dimensionName) throws LEMSException
{
return convertToNeuronUnits(siValue, dimensionName);
}

protected static float convertToNeuronUnits(float siVal, String dimensionName)
protected static float convertToNeuronUnits(float siVal, String dimensionName) throws LEMSException
{
BigDecimal factor = new BigDecimal(getNeuronUnitFactor(dimensionName));
BigDecimal newValB = new BigDecimal(siVal+"");
Expand All @@ -433,7 +439,7 @@ protected static float convertToNeuronUnits(float siVal, String dimensionName)
return newVal;
}

public static float getNeuronUnitFactor(String dimensionName)
public static float getNeuronUnitFactor(String dimensionName) throws LEMSException
{

if (dimensionName.equals("none"))
Expand All @@ -448,6 +454,10 @@ else if (dimensionName.equals("per_voltage"))
{
return 0.001f;
}
else if (dimensionName.equals("per_voltage2"))
{
return 1e-6f;
}
else if (dimensionName.equals("conductance"))
{
return 1e6f;
Expand Down Expand Up @@ -526,7 +536,7 @@ else if (dimensionName.equals("temperature"))
}
else
{
return Float.NaN;
throw new LEMSException("Dimension "+dimensionName+" is not known to NEURON (add to NRNUtils)");
}
}

Expand All @@ -544,7 +554,7 @@ protected static String getDerivativeUnit(String dimensionName)
}


public static void main(String args[])
public static void main(String args[]) throws LEMSException
{
NRNUtils nu = new NRNUtils();
float f = 2.5e-5f;
Expand Down
157 changes: 115 additions & 42 deletions src/main/java/org/neuroml/export/neuron/NeuronWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public NeuronWriter(Lems lems) throws ModelFeatureSupportException, LEMSExceptio
public NeuronWriter(Lems lems, File outputFolder, String outputFileName) throws ModelFeatureSupportException, NeuroMLException, LEMSException
{
super(lems, Format.NEURON, outputFolder, outputFileName);
E.info("Creating NeuronWriter for "+outputFileName);
E.info("Creating NeuronWriter to output files to "+outputFolder.getAbsolutePath());
}

@Override
Expand All @@ -143,44 +143,61 @@ public void setSupportedFeatures()
sli.addSupportInfo(format, ModelFeature.KS_CHANNEL_MODEL, SupportLevelInfo.Level.LOW);
}

public List<File> generateAndRun(boolean nogui, boolean run) throws LEMSException, GenerationException, NeuroMLException, IOException, ModelFeatureSupportException
public List<File> generateAndRun(boolean nogui, boolean compileMods, boolean run) throws LEMSException, GenerationException, NeuroMLException, IOException, ModelFeatureSupportException
{

this.nogui = nogui;
List<File> files = generateMainScriptAndMods();

if(run)
if(compileMods || run)
{
E.info("Trying to compile mods in: " + this.getOutputFolder());
if (compileMods)
{
E.info("Trying to compile mods in: " + this.getOutputFolder());

boolean comp = ProcessManager.compileFileWithNeuron(this.getOutputFolder(), false);

E.info("Success: " + comp);
boolean complied = ProcessManager.compileFileWithNeuron(this.getOutputFolder(), false);

File neuronHome = findNeuronHome();
String nrncmd = nogui ? "nrniv" : "nrngui";
String commandToExecute = neuronHome.getCanonicalPath() + System.getProperty("file.separator") + "bin" + System.getProperty("file.separator") + nrncmd + " -python "
+ new File(this.getOutputFolder(), this.getOutputFileName()).getCanonicalPath();
E.info("Success in compiling mods: " + complied);

Runtime rt = Runtime.getRuntime();
Process currentProcess = rt.exec(commandToExecute, null, this.getOutputFolder());
ProcessOutputWatcher procOutputMain = new ProcessOutputWatcher(currentProcess.getInputStream(), "NRN Output >>");
procOutputMain.start();
if (!complied)
{
String mods = "";
for (File f: files )
{
if (f.getName().endsWith("mod"))
mods+=f.getAbsolutePath()+"; ";
}

ProcessOutputWatcher procOutputError = new ProcessOutputWatcher(currentProcess.getErrorStream(), "NRN Error >>");
procOutputError.start();
throw new NeuroMLException("Error compiling mod files: "+mods);
}
}
if (run)
{
File neuronHome = findNeuronHome();
String nrncmd = nogui ? "nrniv" : "nrngui";
String commandToExecute = neuronHome.getCanonicalPath() + System.getProperty("file.separator") + "bin" + System.getProperty("file.separator") + nrncmd + " -python "
+ new File(this.getOutputFolder(), this.getOutputFileName()).getCanonicalPath();

E.info("Have successfully executed command: " + commandToExecute);
Runtime rt = Runtime.getRuntime();
Process currentProcess = rt.exec(commandToExecute, null, this.getOutputFolder());
ProcessOutputWatcher procOutputMain = new ProcessOutputWatcher(currentProcess.getInputStream(), "NRN Output >>");
procOutputMain.start();

try
{
currentProcess.waitFor();
ProcessOutputWatcher procOutputError = new ProcessOutputWatcher(currentProcess.getErrorStream(), "NRN Error >>");
procOutputError.start();

E.info("Exit value for running NEURON: " + currentProcess.exitValue());
}
catch(InterruptedException e)
{
E.info("Problem executing Neuron " + e);
E.info("Have successfully executed command: " + commandToExecute);

try
{
currentProcess.waitFor();

E.info("Exit value for running NEURON: " + currentProcess.exitValue());
}
catch(InterruptedException e)
{
E.info("Problem executing Neuron " + e);
}
}
}
return files;
Expand Down Expand Up @@ -1588,7 +1605,7 @@ private void processInputLists(StringBuilder main, Component targetComp) throws
}

private void generateHocForInput(StringBuilder main, Component input, Component inputComp, String inputName, int index) throws ContentError,
NeuroMLException, ParseError {
NeuroMLException, ParseError, LEMSException {

String nrnSection = parseInputSecName(input);
float fractionAlong = parseFractionAlong(input);
Expand Down Expand Up @@ -1693,7 +1710,7 @@ private int parseSegmentId(Component input) throws ContentError {
}

private void processTimeDependentLiterals(StringBuilder main, Component input, Component inputComp)
throws ContentError, NeuroMLException, ParseError {
throws ContentError, NeuroMLException, ParseError, LEMSException {
// TODO Auto-generated method stub
String inputName = NRNUtils.getSafeName(inputComp.getID());
addComment(main, "Generating event source for point process " + input, bIndent);
Expand Down Expand Up @@ -2886,7 +2903,7 @@ private static void parseOnEvent(Component comp, StringBuilder blockNetReceive,
}

private static void parseParameters(Component comp, String prefix, String prefixParent, ArrayList<String> rangeVars, ArrayList<String> stateVars, StringBuilder blockNeuron,
StringBuilder blockParameter, LinkedHashMap<String, LinkedHashMap<String, String>> paramMappings)
StringBuilder blockParameter, LinkedHashMap<String, LinkedHashMap<String, String>> paramMappings) throws LEMSException
{

LinkedHashMap<String, String> paramMappingsComp = paramMappings.get(comp.getUniqueID());
Expand Down Expand Up @@ -3537,13 +3554,69 @@ public class CompInfo
StringBuilder eqns = new StringBuilder();
StringBuilder initInfo = new StringBuilder();
}

/*
* Can be used to generate Neuron hoc/mod files for cells (with channels) and synapses
*/
public void generateFilesForNeuroMLElements(boolean compileMods) throws LEMSException, NeuroMLException, IOException
{
boolean foundMods = false;
for (Component comp: lems.getComponents())
{
if (comp.getComponentType().isOrExtends(NeuroMLElements.CELL_COMP_TYPE))
{
convertCellWithMorphology(comp);
}
// extends baseCell but not Cell
else if (comp.getComponentType().isOrExtends(NeuroMLElements.BASE_CELL_COMP_TYPE))
{
generateModForComp(comp);
foundMods = true;
}
else if (comp.getComponentType().isOrExtends(NeuroMLElements.BASE_SYNAPSE_COMP_TYPE))
{
generateModForComp(comp);
foundMods = true;
}
// TODO: more..?

}
if (compileMods && foundMods)
{
E.info("Trying to compile mods in: " + this.getOutputFolder());

boolean complied = ProcessManager.compileFileWithNeuron(this.getOutputFolder(), false);

E.info("Success in compiling mods: " + complied);

if (!complied)
{

throw new NeuroMLException("Error compiling mod files in: "+this.getOutputFolder());
}
}
}

public static void main(String[] args) throws Exception
{

MinimalMessageHandler.setVeryMinimal(true);
E.setDebug(false);


ArrayList<File> nmlFiles = new ArrayList<File>();
nmlFiles.add(new File("../NeuroML2/examples/NML2_SingleCompHHCell.nml"));
nmlFiles.add(new File("../NeuroML2/examples/NML2_SynapseTypes.nml"));
nmlFiles.add(new File("../git/BonoClopath2017/NeuroML2/SimpleNet.net.nml"));

for(File nmlFile : nmlFiles)
{
Lems lems = Utils.readNeuroMLFile(nmlFile.getAbsoluteFile()).getLems();
System.out.println("Loading: "+nmlFile);
NeuronWriter nw = new NeuronWriter(lems, nmlFile.getParentFile(),"");
nw.generateFilesForNeuroMLElements(true);
}
//System.exit(0);

ArrayList<File> lemsFiles = new ArrayList<File>();
//lemsFiles.add(new File("../neuroConstruct/osb/invertebrate/celegans/CElegansNeuroML/CElegans/pythonScripts/c302/examples/LEMS_c302_C1_Oscillator.xml"));

Expand All @@ -3552,7 +3625,7 @@ public static void main(String[] args) throws Exception
//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex16_Inputs.xml"));
//lemsFiles.add(new File("../neuroConstruct/osb/cerebellum/networks/VervaekeEtAl-GolgiCellNetwork/NeuroML2/LEMS_Pacemaking.xml"));
//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex9_FN.xml"));
//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex5_DetCell.xml"));
lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex5_DetCell.xml"));
//lemsFiles.add(new File("../git/TestHippocampalNetworks/NeuroML2/channels/test_Cadynamics/NeuroML2/LEMS_test_Ca.xml"));
//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex20a_AnalogSynapsesHH.xml"));
//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex20_AnalogSynapses.xml"));
Expand All @@ -3570,16 +3643,16 @@ public static void main(String[] args) throws Exception
//lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/neocortical_pyramidal_neuron/SmithEtAl2013-L23DendriticSpikes/NeuroML2/LEMS_L23_Stim.xml"));

//lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex19a_GapJunctionInstances.xml"));

lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/multiple/PospischilEtAl2008/NeuroML2/channels/Na/LEMS_Na.xml"));
lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/multiple/PospischilEtAl2008/NeuroML2/channels/Kd/LEMS_Kd.xml"));
lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/networks/ACnet2/neuroConstruct/generatedNeuroML2/LEMS_MediumNet.xml"));
lemsFiles.add(new File("../OpenCortex/examples/LEMS_ACNet.xml"));

lemsFiles.add(new File("../OpenCortex/examples/LEMS_SpikingNet.xml"));
lemsFiles.add(new File("../OpenCortex/examples/LEMS_SimpleNet.xml"));

lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/networks/IzhikevichModel/NeuroML2/LEMS_2007One.xml"));
//
// lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/multiple/PospischilEtAl2008/NeuroML2/channels/Na/LEMS_Na.xml"));
// lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/multiple/PospischilEtAl2008/NeuroML2/channels/Kd/LEMS_Kd.xml"));
// lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/networks/ACnet2/neuroConstruct/generatedNeuroML2/LEMS_MediumNet.xml"));
// lemsFiles.add(new File("../OpenCortex/examples/LEMS_ACNet.xml"));
//
// lemsFiles.add(new File("../OpenCortex/examples/LEMS_SpikingNet.xml"));
// lemsFiles.add(new File("../OpenCortex/examples/LEMS_SimpleNet.xml"));
//
// lemsFiles.add(new File("../neuroConstruct/osb/cerebral_cortex/networks/IzhikevichModel/NeuroML2/LEMS_2007One.xml"));

// lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex20a_AnalogSynapsesHH.xml"));
// lemsFiles.add(new File("../NeuroML2/LEMSexamples/LEMS_NML2_Ex14_PyNN.xml"));
Expand Down Expand Up @@ -3682,7 +3755,7 @@ public static void main(String[] args) throws Exception
Lems lems = Utils.readLemsNeuroMLFile(lemsFile.getAbsoluteFile()).getLems();
nw = new NeuronWriter(lems, lemsFile.getParentFile(), lemsFile.getName().replaceAll(".xml", "_nrn.py"));

List<File> ff = nw.generateAndRun(false, false);
List<File> ff = nw.generateAndRun(false, false, false);
for(File f : ff)
{
System.out.println("Generated: " + f.getAbsolutePath());
Expand Down
Loading

0 comments on commit e72b4c8

Please sign in to comment.