@Override
  public String toString() {
    StringBuilder sb = new StringBuilder();

    sb.append("GeneratedElecInputs with " + this.getNumberSingleInputs() + " inputs in total\n");

    Enumeration keys = this.myElecInputs.keys();

    while (keys.hasMoreElements()) {
      String input = (String) keys.nextElement();
      ArrayList<SingleElectricalInput> singleInputList = myElecInputs.get(input);
      StimulationSettings ss = project.elecInputInfo.getStim(input);
      sb.append(
          input + " (" + ss.toLongString() + ") has " + singleInputList.size() + " entries.\n");
      for (int i = 0; (i < singleInputList.size() && i < 9); i++) {
        SingleElectricalInput sei = singleInputList.get(i);
        sb.append("   Input " + i + ": " + sei.toString());
        if (sei.getInstanceProps() != null)
          sb.append(" (" + sei.getInstanceProps().details(false) + ")");

        sb.append("\n");
      }
    }
    return sb.toString();
  }
  public int getNumberSingleInputs(String inputRef) {
    if (!myElecInputs.containsKey(inputRef)) return 0;

    ArrayList inputArrayList = myElecInputs.get(inputRef);

    return inputArrayList.size();
  }
Example #3
0
  private ArrayList<String> getConnectionSynTypes() {
    ArrayList<String> a = new ArrayList<String>();

    for (ConnSpecificProps c : globConnProps) {
      a.add(c.synapseType);
    }
    return a;
  }
  public void addSingleInput(String inputReference, SingleElectricalInput oneInput) {
    if (!myElecInputs.containsKey(inputReference)) {
      ArrayList<SingleElectricalInput> newInputArrayList = new ArrayList<SingleElectricalInput>();
      myElecInputs.put(inputReference, newInputArrayList);
    }
    ArrayList<SingleElectricalInput> inputVector = myElecInputs.get(inputReference);

    inputVector.add(oneInput);
  }
  public int getNumberSingleInputs() {
    int totalCount = 0;

    Enumeration keys = this.myElecInputs.keys();

    while (keys.hasMoreElements()) {
      ArrayList inputArrayList = myElecInputs.get((String) keys.nextElement());
      totalCount = totalCount + inputArrayList.size();
    }

    return totalCount;
  }
  public ArrayList<String> getNonEmptyInputRefs() {
    ArrayList<String> neir = new ArrayList<String>();

    Enumeration<String> keys = this.myElecInputs.keys();

    while (keys.hasMoreElements()) {
      String ref = keys.nextElement();
      ArrayList inputArrayList = myElecInputs.get(ref);
      if (inputArrayList.size() > 0) neir.add(ref);
    }

    return neir;
  }
  public void saveToFile(File inputsFile) throws java.io.IOException {
    logger.logComment(
        "Saving "
            + this.getNumberSingleInputs()
            + " inputs to file: "
            + inputsFile.getAbsolutePath());

    // will create the parent dir if it doesn't exist.
    if (!inputsFile.exists()) {
      logger.logComment("File: " + inputsFile + " doesn't exist.");
      if (!inputsFile.getParentFile().exists()) {
        logger.logComment("Parent dir: " + inputsFile.getParentFile() + " doesn't exist.");
        // String parentDirName = inputsFile.getParentFile().getCanonicalPath();
        File projectDir = inputsFile.getParentFile().getParentFile();

        if (!projectDir.exists()) {
          throw new FileNotFoundException(
              "Project dir doesn't exist: " + projectDir.getAbsolutePath());
        }
        // logger.logComment("Going to create dir: "+ parentDirName +" in dir :"+ projectDir);

        logger.logComment("Going to create dir: " + inputsFile.getParentFile());

        inputsFile.getParentFile().mkdir();

        logger.logComment("Success? " + inputsFile.getParentFile().exists());
      }
    }

    FileWriter fw = new FileWriter(inputsFile);

    Enumeration keys = this.myElecInputs.keys();

    while (keys.hasMoreElements()) {
      String input = (String) keys.nextElement();
      ArrayList<SingleElectricalInput> inputs = getInputLocations(input);

      fw.write(input + ":\n");

      for (int i = 0; i < inputs.size(); i++) {
        fw.write(inputs.get(i) + "\n");
      }
    }
    logger.logComment("Finished saving data to file: " + inputsFile.getAbsolutePath());
    fw.flush();
    fw.close();
  }
  public void addSingleInput(
      String inputReference,
      String inputType,
      String cellGroup,
      int cellNumber,
      int segmentId,
      float fractionAlong,
      InputInstanceProps ip) {
    if (!myElecInputs.containsKey(inputReference)) {
      ArrayList<SingleElectricalInput> newInputArrayList = new ArrayList<SingleElectricalInput>();
      myElecInputs.put(inputReference, newInputArrayList);
    }
    ArrayList<SingleElectricalInput> inputVector = myElecInputs.get(inputReference);

    inputVector.add(
        new SingleElectricalInput(inputType, cellGroup, cellNumber, segmentId, fractionAlong, ip));
  }
  public String details(boolean html) {
    StringBuilder sb = new StringBuilder();

    sb.append(
        "Network contains "
            + GeneralUtils.getBold(this.getNumberSingleInputs(), html)
            + " inputs in total"
            + GeneralUtils.getEndLine(html)
            + GeneralUtils.getEndLine(html));

    Enumeration keys = this.myElecInputs.keys();

    String indent = "    ";
    if (html) indent = "&nbsp;&nbsp;&nbsp;&nbsp;";

    while (keys.hasMoreElements()) {
      String input = (String) keys.nextElement();
      ArrayList<SingleElectricalInput> singleInputList = myElecInputs.get(input);
      sb.append(
          "Input: "
              + GeneralUtils.getBold(input, html)
              + " has "
              + GeneralUtils.getBold(singleInputList.size(), html)
              + " entries"
              + GeneralUtils.getEndLine(html));
      for (int i = 0; (i < singleInputList.size()); i++) {
        sb.append(
            "Input "
                + i
                + ": "
                + singleInputList.get(i).details(html)
                + GeneralUtils.getEndLine(html));
        if (singleInputList.get(i).getInstanceProps() != null) {
          sb.append(
              indent
                  + "Input specific properties: "
                  + singleInputList.get(i).getInstanceProps().details(html)
                  + GeneralUtils.getEndLine(html));
        }
      }
      sb.append(GeneralUtils.getEndLine(html));
    }
    return sb.toString();
  }
Example #10
0
  public void startGroup(Group g) throws Hdf5Exception {
    logger.logComment("-----   Going into a group: " + g.getFullName());

    ArrayList<Attribute> attrs = Hdf5Utils.parseGroupForAttributes(g);

    for (Attribute attribute : attrs) {
      // attribute.
      logger.logComment(
          "Group: "
              + g.getName()
              + " has attribute: "
              + attribute.getName()
              + " = "
              + Hdf5Utils.getFirstStringValAttr(attrs, attribute.getName()));
    }

    if (g.getName().equals(NetworkMLConstants.ROOT_ELEMENT)) {
      logger.logComment("Found the main group");

      String simConfigName =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.NC_SIM_CONFIG);

      if (simConfigName != null) this.foundSimConfig = simConfigName;

      String randomSeed =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.NC_NETWORK_GEN_RAND_SEED);

      if (randomSeed != null) this.foundRandomSeed = Long.parseLong(randomSeed);

    } else if (g.getName().equals(NetworkMLConstants.POPULATIONS_ELEMENT)) {
      logger.logComment("Found the pops group");
      inPopulations = true;

    } else if (g.getName().startsWith(NetworkMLConstants.POPULATION_ELEMENT) && inPopulations) {
      String name = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.POP_NAME_ATTR);

      logger.logComment("Found a population: " + name);
      currentCellGroup = name;
    } else if (g.getName().equals(NetworkMLConstants.PROJECTIONS_ELEMENT)) {
      logger.logComment("Found the projections group");
      inProjections = true;

      String units = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.UNITS_ATTR);

      projUnitSystem = UnitConverter.getUnitSystemIndex(units);

    } else if (g.getName().startsWith(NetworkMLConstants.PROJECTION_ELEMENT) && inProjections) {
      String name = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.PROJ_NAME_ATTR);
      String source = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.SOURCE_ATTR);
      String target = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.TARGET_ATTR);

      logger.logComment("Found a projection: " + name + " from " + source + " to " + target);

      if (!project.morphNetworkConnectionsInfo.isValidSimpleNetConn(name)
          && !project.volBasedConnsInfo.isValidVolBasedConn(name)) {
        throw new Hdf5Exception(
            "Error: there is a network connection with name: "
                + name
                + " specified in "
                + "that file, but no such NetConn exists in the project. Add one to allow import of this file");
      }

      /* TODO: Add checks on source & target!!
       */

      if (project.morphNetworkConnectionsInfo.isValidSimpleNetConn(name)) {
        // if (project.morphNetworkConnectionsInfo)
      }

      currentNetConn = name;
    } else if (g.getName().startsWith(NetworkMLConstants.SYN_PROPS_ELEMENT + "_")
        && inProjections) {
      String name = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.SYN_TYPE_ATTR);

      ConnSpecificProps cp = new ConnSpecificProps(name);

      String internalDelay =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INTERNAL_DELAY_ATTR);
      if (internalDelay != null)
        cp.internalDelay =
            (float)
                UnitConverter.getTime(
                    Float.parseFloat(internalDelay),
                    projUnitSystem,
                    UnitConverter.NEUROCONSTRUCT_UNITS);

      // Lump them in to the internal delay...
      String preDelay = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.PRE_DELAY_ATTR);
      if (preDelay != null)
        cp.internalDelay =
            cp.internalDelay
                + (float)
                    UnitConverter.getTime(
                        Float.parseFloat(preDelay),
                        projUnitSystem,
                        UnitConverter.NEUROCONSTRUCT_UNITS);

      String postDelay = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.POST_DELAY_ATTR);
      if (postDelay != null)
        cp.internalDelay =
            cp.internalDelay
                + (float)
                    UnitConverter.getTime(
                        Float.parseFloat(postDelay),
                        projUnitSystem,
                        UnitConverter.NEUROCONSTRUCT_UNITS);

      cp.weight =
          Float.parseFloat(Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.WEIGHT_ATTR));

      String propDelay = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.PROP_DELAY_ATTR);
      if (propDelay != null)
        globAPDelay =
            (float)
                UnitConverter.getTime(
                    Float.parseFloat(propDelay),
                    projUnitSystem,
                    UnitConverter.NEUROCONSTRUCT_UNITS);

      logger.logComment("Found: " + cp);

      globConnProps.add(cp);
    } else if (g.getName().equals(NetworkMLConstants.INPUTS_ELEMENT)) {
      logger.logComment("Found the Inputs group");
      inInputs = true;

      String units = Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.UNITS_ATTR);

      inputUnitSystem = UnitConverter.getUnitSystemIndex(units);
    } else if (g.getName().startsWith(NetworkMLConstants.INPUT_ELEMENT) && inInputs) {
      // The table of input sites is within the input group so get sites from here

      String inputName = g.getName().substring(6);

      // String inputName = Hdf5Utils.getFirstStringValAttr(attrs,
      // NetworkMLConstants.INPUT_ELEMENT);

      logger.logComment("Found an Input: " + inputName);
      // inInput = true;

      if (project.elecInputInfo.getStim(inputName) == null) {
        throw new Hdf5Exception(
            "Error: there is an electrical input with name: "
                + inputName
                + " specified in "
                + "that file, but no such electrical input exists in the project. Add one to allow import of this file");
      }
      // Get the atributes of the Input and compare them with the attributes within the project
      // Test to find out what type of input this is

    } else if (g.getName().startsWith("IClamp") && inInputs) {
      String inputName = g.getParent().getName().substring(6);
      // Get the input sites from the table

      String cellGroup =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_TARGET_POPULATION_ATTR);
      if (cellGroup == null) {
        cellGroup =
            Hdf5Utils.getFirstStringValAttr(
                attrs, NetworkMLConstants.INPUT_TARGET_CELLGROUP_OLD_ATTR); // check old name
      }

      float readDelay =
          (float)
              UnitConverter.getTime(
                  Float.parseFloat(
                      Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_DELAY_ATTR)),
                  inputUnitSystem,
                  UnitConverter.NEUROCONSTRUCT_UNITS);
      float readDuration =
          (float)
              UnitConverter.getTime(
                  Float.parseFloat(
                      Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_DUR_ATTR)),
                  inputUnitSystem,
                  UnitConverter.NEUROCONSTRUCT_UNITS);
      float readAmp =
          (float)
              UnitConverter.getCurrent(
                  Float.parseFloat(
                      Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_AMP_ATTR)),
                  inputUnitSystem,
                  UnitConverter.NEUROCONSTRUCT_UNITS);

      StimulationSettings nextStim = project.elecInputInfo.getStim(inputName);
      ElectricalInput myElectricalInput = nextStim.getElectricalInput();
      IClamp ic = (IClamp) myElectricalInput;

      logger.logComment("Found an IClamp Input");

      float currDelay = -1, currDur = -1, currAmp = -1;

      /*
      try
      {
          ic.getDelay().reset();
          currDelay = ic.getDelay().getNumber();
          ic.getDuration().reset();
          currDur = ic.getDuration().getNumber();
          ic.getAmplitude().reset();
          currAmp = ic.getAmplitude().getNumber();
      }
      catch (Exception ex)
      {
          logger.logError("Legacy error getting iclamp params!!");
      }*/

      currDelay = ic.getDel().getNominalNumber();
      currDur = ic.getDur().getNominalNumber();
      currAmp = ic.getAmp().getNominalNumber();

      if ((!project.elecInputInfo.getStim(inputName).getCellGroup().equals(cellGroup))
          || (readDelay != currDelay)
          || (readDuration != currDur)
          || (readAmp != currAmp)) {
        throw new Hdf5Exception(
            "Error: the input properties of the file do not match those in the project for input "
                + inputName
                + ""
                + "\nreadDelay: "
                + readDelay
                + ", currDelay: "
                + currDelay
                + "\nreadDuration: "
                + readDuration
                + ", currDur: "
                + currDur
                + "\nreadAmp: "
                + readAmp
                + ", currAmp: "
                + currAmp
                + ", str: "
                + Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_AMP_ATTR));
      }
      currentInput = inputName;
    } else if (g.getName().startsWith("RandomSpikeTrain") && inInputs) {
      String inputName = g.getParent().getName().substring(6);
      // Get the input sites from the table
      String cellGroup =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.INPUT_TARGET_POPULATION_ATTR);
      if (cellGroup == null) {
        cellGroup =
            Hdf5Utils.getFirstStringValAttr(
                attrs, NetworkMLConstants.INPUT_TARGET_CELLGROUP_OLD_ATTR); // check old name
      }

      float frequency =
          (float)
              UnitConverter.getRate(
                  Float.parseFloat(
                      Hdf5Utils.getFirstStringValAttr(
                          attrs, NetworkMLConstants.RND_STIM_FREQ_ATTR)),
                  inputUnitSystem,
                  UnitConverter.NEUROCONSTRUCT_UNITS);
      String mechanism =
          Hdf5Utils.getFirstStringValAttr(attrs, NetworkMLConstants.RND_STIM_MECH_ATTR);

      StimulationSettings nextStim = project.elecInputInfo.getStim(inputName);
      ElectricalInput myElectricalInput = nextStim.getElectricalInput();
      RandomSpikeTrain rs = (RandomSpikeTrain) myElectricalInput;

      logger.logComment("Found an Random Spike Train Input");

      if ((!project.elecInputInfo.getStim(inputName).getCellGroup().equals(cellGroup))
          || frequency != rs.getRate().getFixedNum()
          || !rs.getSynapseType().equals(mechanism)) {
        throw new Hdf5Exception(
            "Error: the input properties of the file do not match those in the project for input "
                + inputName);
      }
      currentInput = inputName;
    }
  }
Example #11
0
  public void dataSet(Dataset d) throws Hdf5Exception {
    logger.logComment("-----   Looking through dataset: " + d);

    ArrayList<Attribute> attrs = Hdf5Utils.parseDatasetForAttributes(d);

    for (Attribute attribute : attrs) {
      logger.logComment(
          "Dataset: "
              + d.getName()
              + " has attribute: "
              + attribute.getName()
              + " = "
              + Hdf5Utils.getFirstStringValAttr(attrs, attribute.getName()));
    }

    float[][] data = Hdf5Utils.parse2Ddataset(d);

    logger.logComment("Data has size: (" + data.length + ", " + data[0].length + ")");

    if (inPopulations && currentCellGroup != null) {
      for (int i = 0; i < data.length; i++) {
        int id = (int) data[i][0];
        float x = data[i][1];
        float y = data[i][2];
        float z = data[i][3];

        PositionRecord posRec = new PositionRecord(id, x, y, z);

        if (data[0].length == 5) {
          posRec.setNodeId((int) data[i][4]);
        }

        this.project.generatedCellPositions.addPosition(currentCellGroup, posRec);
      }
    }
    if (inProjections && currentNetConn != null) {
      logger.logComment("Adding info for NetConn: " + currentNetConn);

      int id_col = -1;

      int pre_cell_id_col = -1;
      int pre_segment_id_col = -1;
      int pre_fraction_along_col = -1;

      int post_cell_id_col = -1;
      int post_segment_id_col = -1;
      int post_fraction_along_col = -1;

      int prop_delay_col = -1;

      for (Attribute attribute : attrs) {
        String storedInColumn = Hdf5Utils.getFirstStringValAttr(attrs, attribute.getName());

        if (storedInColumn.equals(NetworkMLConstants.CONNECTION_ID_ATTR)) {
          id_col = Integer.parseInt(attribute.getName().substring("column_".length()));
          logger.logComment("id col: " + id_col);
        } else if (storedInColumn.equals(NetworkMLConstants.PRE_CELL_ID_ATTR)) {
          pre_cell_id_col = Integer.parseInt(attribute.getName().substring("column_".length()));
        } else if (storedInColumn.equals(NetworkMLConstants.PRE_SEGMENT_ID_ATTR)) {
          pre_segment_id_col = Integer.parseInt(attribute.getName().substring("column_".length()));
          logger.logComment("pre_segment_id_col: " + pre_segment_id_col);
        } else if (storedInColumn.equals(NetworkMLConstants.PRE_FRACT_ALONG_ATTR)) {
          pre_fraction_along_col =
              Integer.parseInt(attribute.getName().substring("column_".length()));
          logger.logComment("pre_fraction_along_col: " + pre_fraction_along_col);
        } else if (storedInColumn.equals(NetworkMLConstants.POST_CELL_ID_ATTR)) {
          post_cell_id_col = Integer.parseInt(attribute.getName().substring("column_".length()));
        } else if (storedInColumn.equals(NetworkMLConstants.POST_SEGMENT_ID_ATTR)) {
          post_segment_id_col = Integer.parseInt(attribute.getName().substring("column_".length()));
        } else if (storedInColumn.equals(NetworkMLConstants.POST_FRACT_ALONG_ATTR)) {
          post_fraction_along_col =
              Integer.parseInt(attribute.getName().substring("column_".length()));
        } else if (storedInColumn.startsWith(NetworkMLConstants.PROP_DELAY_ATTR)) {
          prop_delay_col = Integer.parseInt(attribute.getName().substring("column_".length()));
        }

        for (String synType : getConnectionSynTypes()) {
          if (storedInColumn.endsWith(synType)) {
            ConnSpecificProps cp = null;

            for (ConnSpecificProps currCp : localConnProps) {
              if (currCp.synapseType.equals(synType)) cp = currCp;
            }
            if (cp == null) {
              cp = new ConnSpecificProps(synType);
              cp.internalDelay = -1;
              cp.weight = -1;
              localConnProps.add(cp);
            }

            if (storedInColumn.startsWith(NetworkMLConstants.INTERNAL_DELAY_ATTR)) {
              cp.internalDelay =
                  Integer.parseInt(
                      attribute
                          .getName()
                          .substring("column_".length())); // store the col num temporarily..
            }
            if (storedInColumn.startsWith(NetworkMLConstants.WEIGHT_ATTR)) {
              cp.weight =
                  Integer.parseInt(
                      attribute
                          .getName()
                          .substring("column_".length())); // store the col num temporarily..
            }
          }
        }
      }

      for (int i = 0; i < data.length; i++) {
        int pre_seg_id = 0;
        float pre_fract_along = 0.5f;
        int post_seg_id = 0;
        float post_fract_along = 0.5f;

        int id = (int) data[i][id_col];
        int pre_cell_id = (int) data[i][pre_cell_id_col];
        int post_cell_id = (int) data[i][post_cell_id_col];

        float prop_delay = 0;

        if (pre_segment_id_col >= 0) pre_seg_id = (int) data[i][pre_segment_id_col];
        if (pre_fraction_along_col >= 0) pre_fract_along = data[i][pre_fraction_along_col];
        if (post_segment_id_col >= 0) post_seg_id = (int) data[i][post_segment_id_col];
        if (post_fraction_along_col >= 0) post_fract_along = data[i][post_fraction_along_col];

        // (float)UnitConverter.getTime(XXXXXXXXX, UnitConverter.NEUROCONSTRUCT_UNITS,
        // unitSystem)+"";
        if (prop_delay_col >= 0)
          prop_delay =
              (float)
                  UnitConverter.getTime(
                      data[i][prop_delay_col], projUnitSystem, UnitConverter.NEUROCONSTRUCT_UNITS);

        ArrayList<ConnSpecificProps> props = new ArrayList<ConnSpecificProps>();

        if (localConnProps.size() > 0) {
          for (ConnSpecificProps currCp : localConnProps) {
            logger.logComment("Pre cp: " + currCp);
            ConnSpecificProps cp2 = new ConnSpecificProps(currCp.synapseType);

            if (currCp.internalDelay > 0) // index was stored in this val...
            cp2.internalDelay =
                  (float)
                      UnitConverter.getTime(
                          data[i][(int) currCp.internalDelay],
                          projUnitSystem,
                          UnitConverter.NEUROCONSTRUCT_UNITS);
            if (currCp.weight > 0) // index was stored in this val...
            cp2.weight = data[i][(int) currCp.weight];

            logger.logComment("Filled cp: " + cp2);

            props.add(cp2);
          }
        }

        this.project.generatedNetworkConnections.addSynapticConnection(
            currentNetConn,
            GeneratedNetworkConnections.MORPH_NETWORK_CONNECTION,
            pre_cell_id,
            pre_seg_id,
            pre_fract_along,
            post_cell_id,
            post_seg_id,
            post_fract_along,
            prop_delay,
            props);
      }
    }
    if (inInputs && currentInput != null) {
      logger.logComment("Adding info for: " + currentInput);
      StimulationSettings nextStim = project.elecInputInfo.getStim(currentInput);
      ElectricalInput myElectricalInput = nextStim.getElectricalInput();
      String electricalInputType = myElectricalInput.getType();
      String cellGroup = nextStim.getCellGroup();

      for (int i = 0; i < data.length; i++) {
        Float fileCellId = data[i][0];
        Float fileSegmentId = data[i][1];
        Float fractionAlong = data[i][2];
        int cellId = fileCellId.intValue();
        int segmentId = fileSegmentId.intValue();

        SingleElectricalInput singleElectricalInputFromFile =
            new SingleElectricalInput(
                electricalInputType, cellGroup, cellId, segmentId, fractionAlong, null);

        this.project.generatedElecInputs.addSingleInput(
            currentInput, singleElectricalInputFromFile);
      }
    }
  }
  public ArrayList<SimpleXMLEntity> getNetworkMLEntities(
      int unitSystem, NeuroMLConstants.NeuroMLVersion version, SimpleXMLElement topLevelCompElement)
      throws NeuroMLException {
    ArrayList<SimpleXMLEntity> entities = new ArrayList<SimpleXMLEntity>();

    Units timeUnits = UnitConverter.timeUnits[unitSystem];
    Units currentUnits = UnitConverter.currentUnits[unitSystem];

    SimpleXMLElement inputsElement = null;
    try {
      logger.logComment(
          "Going to save file in NeuroML format: "
              + this.getNumberSingleInputs()
              + " inputs in total");

      if (getNumberSingleInputs() == 0) {
        SimpleXMLComment comm =
            new SimpleXMLComment("There are no electrical inputs present in the network");
        entities.add(comm);
        return entities;
      }

      boolean nml2 = version.isVersion2();
      boolean nml2alpha = version.isVersion2alpha();

      if (!nml2) {
        inputsElement = new SimpleXMLElement(NetworkMLConstants.INPUTS_ELEMENT);
        entities.add(inputsElement);

        if (unitSystem == UnitConverter.GENESIS_PHYSIOLOGICAL_UNITS) {
          inputsElement.addAttribute(
              new SimpleXMLAttribute(
                  NetworkMLConstants.UNITS_ATTR, NetworkMLConstants.UNITS_PHYSIOLOGICAL));
        } else if (unitSystem == UnitConverter.GENESIS_SI_UNITS) {
          inputsElement.addAttribute(
              new SimpleXMLAttribute(NetworkMLConstants.UNITS_ATTR, NetworkMLConstants.UNITS_SI));
        }
      }

      Enumeration keys = myElecInputs.keys();

      while (keys.hasMoreElements()) {
        String inputReference = (String) keys.nextElement();
        ArrayList<SingleElectricalInput> inputsHere = getInputLocations(inputReference);

        logger.logComment("Adding " + inputsHere.size() + " inputs");

        StimulationSettings nextStim = project.elecInputInfo.getStim(inputReference);

        ElectricalInput myElectricalInput = nextStim.getElectricalInput();

        SimpleXMLElement inputElement = new SimpleXMLElement(NetworkMLConstants.INPUT_ELEMENT);

        inputElement.addAttribute(
            new SimpleXMLAttribute(NetworkMLConstants.INPUT_NAME_ATTR, inputReference));

        if (myElectricalInput instanceof IClamp) {
          IClamp ic = (IClamp) myElectricalInput;

          float delay = ic.getDel().getNominalNumber();
          float duration = ic.getDur().getNominalNumber();
          float amplitude = ic.getAmp().getNominalNumber();

          SimpleXMLElement inputTypeElement =
              new SimpleXMLElement(NetworkMLConstants.PULSEINPUT_ELEMENT);

          float del =
              (float) UnitConverter.getTime(delay, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);
          float dur =
              (float)
                  UnitConverter.getTime(duration, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);
          float amp =
              (float)
                  UnitConverter.getCurrent(
                      amplitude, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);

          inputTypeElement.addAttribute(
              new SimpleXMLAttribute(NetworkMLConstants.INPUT_DELAY_ATTR, del + ""));

          inputTypeElement.addAttribute(
              new SimpleXMLAttribute(NetworkMLConstants.INPUT_DUR_ATTR, dur + ""));

          inputTypeElement.addAttribute(
              new SimpleXMLAttribute(NetworkMLConstants.INPUT_AMP_ATTR, amp + ""));

          inputElement.addChildElement(inputTypeElement);

          inputElement.addContent("\n        ");

          if (nml2) {
            SimpleXMLElement pulseGenElement =
                new SimpleXMLElement(NetworkMLConstants.NEUROML2_PULSE_GEN_ELEMENT);
            pulseGenElement.addAttribute(NeuroMLConstants.NEUROML_ID_V2, inputReference);
            pulseGenElement.addAttribute(
                NetworkMLConstants.INPUT_DELAY_ATTR, del + timeUnits.getNeuroML2Symbol());
            pulseGenElement.addAttribute(
                NetworkMLConstants.INPUT_DUR_ATTR, dur + timeUnits.getNeuroML2Symbol());
            pulseGenElement.addAttribute(
                NetworkMLConstants.INPUT_AMP_ATTR, amp + currentUnits.getNeuroML2Symbol());

            topLevelCompElement.addContent("\n\n    ");
            topLevelCompElement.addChildElement(pulseGenElement);
            topLevelCompElement.addContent("\n\n    ");
          }

        } else if (myElectricalInput instanceof RandomSpikeTrain) {
          RandomSpikeTrain rst = (RandomSpikeTrain) myElectricalInput;

          float stimFreq = rst.getRate().getNominalNumber();
          String stimMech = rst.getSynapseType();

          SimpleXMLElement inputTypeElement =
              new SimpleXMLElement(NetworkMLConstants.RANDOMSTIM_ELEMENT);
          float rate =
              (float)
                  UnitConverter.getRate(stimFreq, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);
          inputTypeElement.addAttribute(
              new SimpleXMLAttribute(
                  NetworkMLConstants.RND_STIM_FREQ_ATTR,
                  (float)
                          UnitConverter.getRate(
                              stimFreq, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem)
                      + ""));

          inputTypeElement.addAttribute(
              new SimpleXMLAttribute(NetworkMLConstants.RND_STIM_MECH_ATTR, stimMech));
          inputElement.addChildElement(inputTypeElement);
          inputElement.addContent("\n        ");

          if (nml2 && !nml2alpha) {
            SimpleXMLElement spikeGenElement =
                new SimpleXMLElement(NetworkMLConstants.NEUROML2_SPIKE_GEN_POISSON_ELEMENT);
            spikeGenElement.addAttribute(NeuroMLConstants.NEUROML_ID_V2, inputReference);
            spikeGenElement.addAttribute(
                NetworkMLConstants.NEUROML2_SPIKE_GEN_POISSON_RATE_ATTR,
                rate
                    + " "
                    + UnitConverter.rateUnits[UnitConverter.NEUROCONSTRUCT_UNITS]
                        .getNeuroML2Symbol()
                    + "");

            topLevelCompElement.addContent("\n\n    ");
            topLevelCompElement.addChildElement(spikeGenElement);
            topLevelCompElement.addContent("\n\n    ");
          }
        } else {
          throw new NeuroMLException(
              "Error trying to save input "
                  + inputReference
                  + ". Cannot save in NeuroML an input of type: "
                  + myElectricalInput.getType());
        }

        SimpleXMLElement inputTargetElement =
            new SimpleXMLElement(NetworkMLConstants.INPUT_TARGET_ELEMENT);

        inputTargetElement.addAttribute(
            new SimpleXMLAttribute(
                NetworkMLConstants.INPUT_TARGET_POPULATION_ATTR, nextStim.getCellGroup()));

        inputElement.addChildElement(inputTargetElement);
        inputTargetElement.addContent("\n            ");

        SimpleXMLElement inputTargetSitesElement =
            new SimpleXMLElement(NetworkMLConstants.INPUT_TARGET_SITES_ELEMENT);

        inputTargetSitesElement.addAttribute(
            new SimpleXMLAttribute(
                NetworkMLConstants.INPUT_SITES_SIZE_ATTR, inputsHere.size() + ""));

        inputTargetElement.addChildElement(inputTargetSitesElement);

        SimpleXMLElement stimProjElement = null;

        if (version.isVersion2betaOrLater()) {

          if (myElectricalInput instanceof IClamp) {
            SimpleXMLElement inputListElement =
                new SimpleXMLElement(NetworkMLConstants.NEUROML2_INPUT_LIST_ELEMENT);
            entities.add(inputListElement);
            inputListElement.addAttribute(NeuroMLConstants.NEUROML_ID_V2, nextStim.getReference());
            inputListElement.addAttribute(
                NetworkMLConstants.NEUROML2_INPUT_COMPONENT, inputReference);
            inputListElement.addAttribute(
                NetworkMLConstants.NEUROML2_INPUT_POPULATION, nextStim.getCellGroup());

            // inputElement.addContent("\n    ");
            inputTargetSitesElement = inputListElement;

          } else if (myElectricalInput instanceof RandomSpikeTrain) {
            SimpleXMLElement popElement =
                new SimpleXMLElement(NetworkMLConstants.POPULATION_ELEMENT);
            entities.add(0, popElement);
            popElement.addAttribute(
                NeuroMLConstants.NEUROML_ID_V2, nextStim.getReference() + "_population");
            popElement.addAttribute(
                NetworkMLConstants.NEUROML2_POPULATION_COMPONENT,
                nextStim.getReference() + "_population");
            popElement.addAttribute(
                NetworkMLConstants.NEUROML2_POPULATION_SIZE, inputsHere.size() + "");

            stimProjElement = new SimpleXMLElement(NetworkMLConstants.PROJECTION_ELEMENT);
            stimProjElement.addAttribute(
                NeuroMLConstants.NEUROML_ID_V2, nextStim.getReference() + "_projection");
            entities.add(stimProjElement);
          }
        }

        // Iterate around the list of sites
        for (int i = 0; i < inputsHere.size(); i++) {
          inputTargetSitesElement.addContent("\n                ");

          SingleElectricalInput sei = inputsHere.get(i);

          SimpleXMLElement inputTargetSiteElement =
              new SimpleXMLElement(NetworkMLConstants.INPUT_TARGET_SITE_ELEMENT);

          inputTargetSiteElement.addAttribute(
              new SimpleXMLAttribute(
                  NetworkMLConstants.INPUT_SITE_CELLID_ATTR, sei.getCellNumber() + ""));
          inputTargetSiteElement.addAttribute(
              new SimpleXMLAttribute(
                  NetworkMLConstants.INPUT_SITE_SEGID_ATTR, sei.getSegmentId() + ""));
          inputTargetSiteElement.addAttribute(
              new SimpleXMLAttribute(
                  NetworkMLConstants.INPUT_SITE_FRAC_ATTR, sei.getFractionAlong() + ""));

          if (!nml2) inputTargetSitesElement.addChildElement(inputTargetSiteElement);

          if (nml2 && !nml2alpha) {
            if (myElectricalInput instanceof RandomSpikeTrain) {
              String connElName = NetworkMLConstants.CONNECTION_ELEMENT;

              SimpleXMLElement connElement = new SimpleXMLElement(connElName);

              connElement.addAttribute(
                  new SimpleXMLAttribute(NetworkMLConstants.CONNECTION_ID_ATTR, i + ""));
              stimProjElement.addContent("\n    ");
              stimProjElement.addChildElement(connElement);
              stimProjElement.addContent("\n    ");
            }
          }

          if (sei.getInstanceProps() != null) {
            inputTargetSiteElement.addContent("\n                ");
            inputTargetSiteElement.addComment("Adding the site specific props");

            if (sei.getInstanceProps() instanceof IClampInstanceProps) {
              IClampInstanceProps ic = (IClampInstanceProps) sei.getInstanceProps();

              float delay =
                  (float)
                      UnitConverter.getTime(
                          ic.getDelay(), UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);
              float duration =
                  (float)
                      UnitConverter.getTime(
                          ic.getDuration(), UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);
              float amp =
                  (float)
                      UnitConverter.getCurrent(
                          ic.getAmplitude(), UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem);

              if (!nml2) {
                SimpleXMLElement inputTypeElement =
                    new SimpleXMLElement(NetworkMLConstants.PULSEINPUT_INSTANCE_ELEMENT);

                inputTypeElement.addAttribute(
                    new SimpleXMLAttribute(NetworkMLConstants.INPUT_DELAY_ATTR, delay + ""));

                inputTypeElement.addAttribute(
                    new SimpleXMLAttribute(NetworkMLConstants.INPUT_DUR_ATTR, duration + ""));

                // System.out.println("Converted "+amp+" to "+ a);
                inputTypeElement.addAttribute(
                    new SimpleXMLAttribute(NetworkMLConstants.INPUT_AMP_ATTR, amp + ""));

                inputTargetSiteElement.addContent("                    ");
                inputTargetSiteElement.addChildElement(inputTypeElement);

                inputTargetSiteElement.addContent("\n                ");
              } else {
                SimpleXMLElement pulseGenElement =
                    new SimpleXMLElement(NetworkMLConstants.NEUROML2_PULSE_GEN_ELEMENT);
                pulseGenElement.addAttribute(
                    NeuroMLConstants.NEUROML_ID_V2, inputReference + "__" + i);
                pulseGenElement.addAttribute(
                    NetworkMLConstants.INPUT_DELAY_ATTR, delay + timeUnits.getNeuroML2Symbol());
                pulseGenElement.addAttribute(
                    NetworkMLConstants.INPUT_DUR_ATTR, duration + timeUnits.getNeuroML2Symbol());
                pulseGenElement.addAttribute(
                    NetworkMLConstants.INPUT_AMP_ATTR, amp + currentUnits.getNeuroML2Symbol());

                topLevelCompElement.addContent("\n\n    ");
                topLevelCompElement.addChildElement(pulseGenElement);
                topLevelCompElement.addContent("\n\n    ");

                if (version.isVersion2alpha()) {
                  String target = nextStim.getCellGroup() + "[" + sei.getCellNumber() + "]";
                  SimpleXMLElement expInputElement =
                      new SimpleXMLElement(NetworkMLConstants.NEUROML2_EXP_INPUT_ELEMENT);
                  expInputElement.addAttribute(
                      NetworkMLConstants.NEUROML2_EXP_INPUT_TARGET_ATTR, target);
                  expInputElement.addAttribute(
                      NetworkMLConstants.NEUROML2_EXP_INPUT_INPUT_ATTR, inputReference + "__" + i);

                  entities.add(expInputElement);
                } else {
                  String target =
                      "../"
                          + nextStim.getCellGroup()
                          + "/"
                          + sei.getCellNumber()
                          + "/"
                          + project.cellGroupsInfo.getCellType(nextStim.getCellGroup());
                  SimpleXMLElement expInputElement =
                      new SimpleXMLElement(NetworkMLConstants.NEUROML2_INPUT_LIST_ELEMENT);
                  expInputElement.addAttribute(
                      NetworkMLConstants.NEUROML2_EXP_INPUT_TARGET_ATTR, target);
                  expInputElement.addAttribute(
                      NetworkMLConstants.NEUROML2_EXP_INPUT_INPUT_ATTR, inputReference + "__" + i);

                  entities.add(expInputElement);
                }
              }
            } else if (sei.getInstanceProps() instanceof RandomSpikeTrainInstanceProps) {
              RandomSpikeTrainInstanceProps rst =
                  (RandomSpikeTrainInstanceProps) sei.getInstanceProps();

              float stimFreq = rst.getRate();
              // String stimMech = rst.get;

              SimpleXMLElement inputTypeElement =
                  new SimpleXMLElement(NetworkMLConstants.RANDOMSTIM_INSTANCE_ELEMENT);

              inputTypeElement.addAttribute(
                  new SimpleXMLAttribute(
                      NetworkMLConstants.RND_STIM_FREQ_ATTR,
                      (float)
                              UnitConverter.getRate(
                                  stimFreq, UnitConverter.NEUROCONSTRUCT_UNITS, unitSystem)
                          + ""));

              // inputTypeElement.addAttribute(new
              // SimpleXMLAttribute(NetworkMLConstants.RND_STIM_MECH_ATTR, stimMech));

              inputTargetSiteElement.addContent("                    ");
              inputTargetSiteElement.addChildElement(inputTypeElement);
              inputTargetSiteElement.addContent("\n                ");

            } else {
              throw new NeuroMLException(
                  "Error trying to save input "
                      + inputReference
                      + ". Cannot save in NeuroML an input of type: "
                      + myElectricalInput.getType());
            }
          } else {
            if (nml2) {
              if (version.isVersion2alpha()) {
                String target = nextStim.getCellGroup() + "[" + sei.getCellNumber() + "]";
                SimpleXMLElement expInputElement =
                    new SimpleXMLElement(NetworkMLConstants.NEUROML2_EXP_INPUT_ELEMENT);
                expInputElement.addAttribute(
                    NetworkMLConstants.NEUROML2_EXP_INPUT_TARGET_ATTR, target);
                expInputElement.addAttribute(
                    NetworkMLConstants.NEUROML2_EXP_INPUT_INPUT_ATTR, inputReference);

                entities.add(expInputElement);
              } else {
                String target =
                    "../"
                        + nextStim.getCellGroup()
                        + "/"
                        + sei.getCellNumber()
                        + "/"
                        + project.cellGroupsInfo.getCellType(nextStim.getCellGroup());
                SimpleXMLElement expInputElement =
                    new SimpleXMLElement(NetworkMLConstants.NEUROML2_INPUT_ELEMENT);
                expInputElement.addAttribute(NeuroMLConstants.NEUROML_ID_V2, i + "");
                expInputElement.addAttribute(
                    NetworkMLConstants.NEUROML2_EXP_INPUT_TARGET_ATTR, target);
                expInputElement.addAttribute(
                    NetworkMLConstants.NEUROML2_INPUT_DESTINATION,
                    NetworkMLConstants.NEUROML2_INPUT_DESTINATION_DEFAULT);

                inputTargetSitesElement.addChildElement(expInputElement);
              }
            }
          }

          if (i == inputsHere.size() - 1) inputTargetSitesElement.addContent("\n            ");

          // Next Site
        }
        inputTargetElement.addContent("\n        ");

        if (!nml2) {
          inputsElement.addChildElement(inputElement);
          inputElement.addContent("\n    ");
        }
      }
      logger.logComment("Finished saving data to inputs element");

    } catch (Exception ex) {
      ex.printStackTrace();
      throw new NeuroMLException("Problem creating inputs element file", ex);
    }
    return entities;
  }