public double sfaError() {

    int nPieceWiseParmsExp = expSpikePatternData.getSpikePatternClass().getnPieceWiseParms();
    int nPieceWiseParmsModel = patternClassifier.getSpikePatternClass().getnPieceWiseParms();
    double possibleWeightForFutureNEAR = nPieceWiseParmsExp - nPieceWiseParmsModel; // coarse!!

    double error = 0;
    double minError = 0;
    double maxMerror = 1;
    double maxCerror = 2;
    double maxNsfaError = expSpikePatternData.getCurrentDuration() * 0.3;

    SolverResultsStat modelStats =
        patternClassifier.getSolverResultsStats()[nPieceWiseParmsExp - 1];
    int modelNsfaISIs1 = modelStats.getBreakPoint();
    int modelNsfaISIs2 = modelSpikePattern.getISIs().length - modelNsfaISIs1;
    double[] model =
        new double[] {
          modelStats.getM1(),
          modelStats.getM2(),
          modelStats.getC1(),
          modelStats.getC2(),
          modelNsfaISIs1,
          modelNsfaISIs2
        };
    double[] exp =
        new double[] {
          expSpikePatternData.getSfaLinearM1().getValue(),
          expSpikePatternData.getSfaLinearM2().getValue(),
          expSpikePatternData.getSfaLinearb1().getValue(),
          expSpikePatternData.getSfaLinearb2().getValue(),
          expSpikePatternData.getNSfaISIs1().getValue(),
          expSpikePatternData.getNSfaISIs2().getValue(),
        };

    for (int i = 0; i < 6; i++) {
      if (i < 2) { // slopes
        error += StatUtil.calculate0to1NormalizedError(exp[i], model[i], minError, maxMerror);
      } else {
        if (i < 4) { // intercepts
          error += StatUtil.calculate0to1NormalizedError(exp[i], model[i], minError, maxCerror);
        } else {
          error += StatUtil.calculate0to1NormalizedError(exp[i], model[i], minError, maxNsfaError);
        }
      }
    }

    error =
        patternClassifier.getDynamicFeatWeightMatrix().get(PatternFeatureID.n_sfa_isis2) * error;

    if (display) {
      displayRoutineForSFA(null, model, error);
    }
    return error;
  }
 private double measureSWA() {
   double swa = 0;
   double[] ISIs = modelSpikePattern.getISIs();
   if (ISIs == null || ISIs.length == 0) {
     return swa;
   }
   double timeMin = modelSpikePattern.getTimeMin() + modelSpikePattern.getFSL();
   double timeMax =
       modelSpikePattern.getTimeMin() + modelSpikePattern.getDurationOfCurrentInjection() - 5;
   double minVolt =
       modelSpikePattern.getModelSpikePatternData().getMinVoltage(timeMin, timeMax, 1);
   swa = modelVmin - minVolt;
   return swa;
 }
  public double sfaErrorObsNormed() {
    // System.out.println("****"+sfa()+"****");
    // int nPieceWiseParmsExp = expSpikePatternData.getSpikePatternClass().getnPieceWiseParms();
    int nPieceWiseParmsModel = patternClassifier.getSpikePatternClass().getnPieceWiseParms();
    double error = 0;
    SolverResultsStat modelStats = null;
    if (nPieceWiseParmsModel > 0) { // ASP, NASP, X
      modelStats = patternClassifier.getSolverResultsStats()[nPieceWiseParmsModel - 1];
    } else { // ASP error for other comps. for example, TSWB
      patternClassifier.calculateAdaptationForNonSP(0);
      modelStats = patternClassifier.getSolverResultsStats()[1]; // idx 1 for linear regression
    }

    int modelNsfaISIs1 = 1 + modelStats.getBreakPoint(); // 0-based idx ; hence +1
    int modelNsfaISIs2 = modelSpikePattern.getISIs().length - modelNsfaISIs1; // remaining
    double[] model =
        new double[] {
          modelStats.getM1(),
          modelStats.getC1(),
          modelNsfaISIs1,
          modelStats.getM2(),
          modelStats.getC2(),
          modelNsfaISIs2
        };
    PatternFeature[] exp =
        new PatternFeature[] {
          expSpikePatternData.getSfaLinearM1(),
          expSpikePatternData.getSfaLinearb1(),
          expSpikePatternData.getNSfaISIs1(),
          expSpikePatternData.getSfaLinearM2(),
          expSpikePatternData.getSfaLinearb2(),
          expSpikePatternData.getNSfaISIs2()
        };

    // System.out.println(patternClassifier.getDynamicFeatWeightMatrix().size());
    float sfa1Weight =
        patternClassifier.getDynamicFeatWeightMatrix().get(PatternFeatureID.n_sfa_isis1);
    float sfa2weight =
        patternClassifier.getDynamicFeatWeightMatrix().get(PatternFeatureID.n_sfa_isis2);

    for (int i = 0; i < 6; i++) {
      // SFA 1
      if (i == 0) { // m1
        error += sfa1Weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
      if (i == 1) { // b1
        error += sfa1Weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
      if (i == 2) { // nsfaisis1		
        error += sfa1Weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
      // SFA 2
      if (i == 3) { // m2
        error += sfa2weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
      if (i == 4) { // b2
        error += sfa2weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
      if (i == 5) { // nsfaisis 2
        error += sfa2weight * NormalizedErrorObsNormed(exp[i], model[i]);
        continue;
      }
    }

    if (display) {
      displayRoutineForSFA(exp, model, error);
    }
    return error;
  }
  public float calculatePatternError() {
    float patternError = Float.MAX_VALUE;
    float patternValidityErrorVmin = 0;
    float patternValidityErrorVrest = 0;

    if (modelSpikePattern == null) {
      if (display) {
        System.out.println(
            "**NULL compartment SPIKE PATTERN** SPEvaluatorV2:calculatePatternError()");
      }
      return patternError;
    }
    // && Math.abs(Float.MAX_VALUE - modelError)>10.0f
    if (checkForPatternValidity) {
      if (expSpikePatternData.getType() == PatternType.SPIKES) {
        // if(!modelSpikePattern.isValidSpikesPattern(modelVmin, modelVr)) { if(display) {
        // System.out.println("**Invalid SPIKEs Pattern**");	   }
        if (!modelSpikePattern.isValidSpikesPattern()) {
          if (display) {
            System.out.println("**Invalid SPIKEs Pattern** SPEvaluatorV2:calculatePatternError()");
          }
          return patternError;
        }
        /*else{
        //	    	if(!expSpikePatternData.getFeaturesToEvaluate().contains(PatternFeatureID.bursts))
        	    	{
        	    		patternValidityErrorVmin = nonBurstBelowVminError(modelVmin);
        	    	}
        	        patternValidityErrorVrest = spikesBelowVrestError(modelVr);
        	    }*/
        if (modelSpikePattern.getISIs() == null) {
          if (display) {
            System.out.println("**Null ISIs** SPEvaluatorV2:calculatePatternError()");
          }
          return patternError;
        }
      }
    } /*else{

        	if(modelSpikePattern.getSpikeTimes().length < 1
        			||
        		(modelSpikePattern.getSpikeTimes()[modelSpikePattern.getSpikeTimes().length-1] - modelSpikePattern.getTimeMin()) > (modelSpikePattern.getDurationOfCurrentInjection()+10))  {
      	return patternError;
      }
        	if(modelSpikePattern.getISIs() == null ) { if(display) {System.out.println("**Null ISIs**");      }
       			return patternError;
        	}
        }*/
    // System.out.println("SPE:: patternWeighted Avg Error will..");

    patternError = patternWeightedAvgError();
    // System.out.println("SPE:: patternWeighted Avg Error done..");

    float avgPatternError;
    if (expSpikePatternData.getType() == PatternType.SPIKES
        || expSpikePatternData.getType() == PatternType.RBASE) {
      avgPatternError =
          (float)
              (patternRepairWeights[0] * patternError
                  + patternRepairWeights[1] * patternValidityErrorVmin
                  + patternRepairWeights[2] * patternValidityErrorVrest);
    } else {
      avgPatternError = patternError;
    }
    if (display) {
      System.out.print(
          "\nPatternFeatError\t"
              + GeneralUtils.formatThreeDecimal(patternError)
              + "\nPatternValidityErrorVmin\t"
              + GeneralUtils.formatThreeDecimal(patternValidityErrorVmin)
              + "\nPatternValidityErrorVrest\t"
              + GeneralUtils.formatThreeDecimal(patternValidityErrorVrest));
    }
    return avgPatternError * expSpikePatternData.getPatternWeight();
  }