Package com.mockturtlesolutions.snifflib.datatypes

Examples of com.mockturtlesolutions.snifflib.datatypes.DblMatrix


    {
      String priorname = "mean_"+names[j];
     
      if (priorParameters.hasParameter(priorname))
      {
        DblMatrix mean = (DblMatrix)priorParameters.getParam(priorname);

        priorname = "var_"+names[j];
        DblMatrix var = (DblMatrix)priorParameters.getParam(priorname);

        this.normal.setMean(mean);
        this.normal.setVariance(var);

        //Calculate and set the prior probability for the current value of the parameter.
        DblMatrix VAL = this.getParam(names[j]);
        priors.setParam(names[j],this.normal.pdf(VAL));
      }
      else
      {
        priorname = "mean_log_"+names[j];
        if (priorParameters.hasParameter(priorname)) //Parameter is log-normal...
        {
          DblMatrix mean = (DblMatrix)priorParameters.getParam(priorname);

          priorname = "var_log_"+names[j];
          DblMatrix var = (DblMatrix)priorParameters.getParam(priorname);

          this.normal.setMean(mean);
          this.normal.setVariance(var);

          //Calculate and set the prior probability for the current value of the parameter.
          DblMatrix VAL = this.getParam(names[j]);
          priors.setParam(names[j],this.normal.pdf(DblMatrix.log(VAL)));
        }
        else
        {
          priorname = "mean_log_delta_"+names[j]; //Order restricted parameters.
          if (priorParameters.hasParameter(priorname)) //Parameter is order-restricted...
          {
            DblMatrix mean_log_delta = (DblMatrix)priorParameters.getParam(priorname);

            priorname = "var_log_delta_"+names[j];
            DblMatrix var_log_delta = (DblMatrix)priorParameters.getParam(priorname);

            this.normal.setMean(mean_log_delta);
            this.normal.setVariance(var_log_delta);

            //Calculate delta.
           
            DblMatrix pKak = this.getParam(names[j]);
            //Need to subtract 1 from the index of this pKa/pKb.
            DblMatrix pKakm1 = getpKaLeftOf(names[j]);
           
            DblMatrix delta = pKak.minus(pKakm1);
           
            //Find the probability of the logarithm of this delta.
            priors.setParam(names[j],this.normal.pdf(DblMatrix.log(delta)));
           
          }
View Full Code Here


        for (int i=0;i<Ka.length;i++) //For each ionizable group
        {
          for (int k=0;k<Ka[i].getN();k++)//For each dissociation site on the ith ion.
          {
            String param = buffername+"_pKa_"+i+"_"+k;
            DblMatrix thisKaSum = null;
            DblMatrix thisKaSumSq = null;
            if (OUT.containsKey(param))
            {
              DblMatrix pka = DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0);
              thisKaSum = (DblMatrix)OUT.get(param);
              thisKaSum = thisKaSum.plus(pka);
              OUT.put(param,thisKaSum);
             
              thisKaSumSq = (DblMatrix)SUMSQ.get(param);
              thisKaSumSq = thisKaSumSq.plus(pka.pow(2.0));
              SUMSQ.put(param,thisKaSumSq);
             
              int N = ((Integer)divisors.get(param)).intValue();
              divisors.put(param,new Integer(N+1));
            }
            else
            {
           
              DblMatrix pka = DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0);
             
              thisKaSum = pka;
              OUT.put(param,thisKaSum);
             
              thisKaSumSq = pka.pow(2);
              SUMSQ.put(param,thisKaSumSq);
             
              divisors.put(param,new Integer(1));
            }
          }
        }

        DblMatrix[] Kb = this.parseKa(buffer.getpKb(expname,ssid));
        for (int i=0;i<Kb.length;i++)//For each ionizable group
        {
          for (int k=0;k<Kb[i].getN();k++)//For each dissociation site on the ith ion.
          {
            String param = buffername+"_pKb_"+i+"_"+k;
            DblMatrix thisKbSum = null;
            DblMatrix thisKbSumSq = null;
            if (OUT.containsKey(param))
            {
              DblMatrix pkb = DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0);
              thisKbSum = (DblMatrix)OUT.get(param);
              thisKbSum = thisKbSum.plus(pkb);
              OUT.put(param,thisKbSum);
             
              thisKbSumSq = (DblMatrix)SUMSQ.get(param);
              thisKbSumSq = thisKbSumSq.plus(pkb.pow(2.0));
              SUMSQ.put(param,thisKbSumSq);
             
              int N = ((Integer)divisors.get(param)).intValue();
              divisors.put(param,new Integer(N+1));
            }
            else
            {
              DblMatrix pkb = DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0);
             
              thisKbSum = pkb;
              OUT.put(param,thisKbSum);
             
              thisKbSumSq = pkb.pow(2);
              SUMSQ.put(param,thisKbSumSq);
             
              divisors.put(param,new Integer(1));
            }
          }
        }
        // ** Assuming no errors in SaltType and that SaltType/ion orderings were the same in each subsample ...
      }
    } 
   
    //Need to modify the pKa and pKb so that the parameters are the average (not the sum).
    Set keys = OUT.keySet();
    Iterator iter = keys.iterator();
    String[] params = new String[OUT.size()];
    int k=0;
   
    while (iter.hasNext())
    {
      params[k] = (String)iter.next();
      k++;
    }
   
    //For configuration of PRIOR distributions...
    //Finally we take the average coefficients we've found here and append.
    //Initially we take the buffer coefficient estimates to equal the emperical average.
    //We store the emperical average for the purposes of evaluating the prior as well.
    for (int n=0;n<params.length;n++)
    {
      System.out.println("Setting initial for parameter:"+params[n]);
      DblMatrix pKaSum = (DblMatrix)OUT.get(params[n]);
      this.setParam(params[n],pKaSum.divideBy(((Integer)divisors.get(params[n])).intValue()));
      pKaSum.divideBy(((Integer)divisors.get(params[n])).intValue());
     
      // if (this.avgcoefficients == null)
//       {
//         System.out.println("AVG COEFF IS NULL");
//       }
     
      //this.avgcoefficients.setParam(params[n],pKaSum.divideBy(((Integer)divisors.get(params[n])).intValue()));
     
     
      DblParamSet pparms = this.getPriorParams();
      pparms.setParam(params[n],pKaSum.divideBy(((Integer)divisors.get(params[n])).intValue()));
     
      pKaSum = ((DblMatrix)SUMSQ.get(params[n])).minus((pKaSum.pow(2.0)).divideBy(((Integer)divisors.get(params[n])).intValue()));
      //this.stdcoefficients.setParam(params[n],pKaSum);
      pparms.setParam(params[n]+"_sigma",DblMatrix.sqrt(pKaSum));//Set the standard deviation in the priorParameters.
     
    }   
  }
View Full Code Here

  protected void configureParameters()
  {   
    
    this.rhsSolution = new ComplexBufferPredictor();
   
    this.declareParameter("sigma",new DblMatrix(1.e-3)); // the overall MSE
    //System.out.println("Configuring parameters...");
    Vector exps = this.bufferData.getExperimentNames();
    String expname;
    Vector subsample,obs;
    String ssid;
   
    this.declareParameter("smoothing_param",new DblMatrix(0.2));
   
    this.declareParameter("aliquot_sigma",new DblMatrix(0.0001)); // Assumed to be the std error of measurement on 1 L.
    this.declareParameter("Vo_sigma",new DblMatrix(0.01)); // Assumed to be the std error of measurement on 1 L.
   
    HashMap divisors = new HashMap();
   
    // if (exps.size() > 0)
//     {
//       expname = (String)exps.get(0);
//      
//       subsample = this.bufferData.getBufferCoefficientsSubsampleIDs(expname);
//       //We will accumulate a sum of pKa and pKb.  At the end we'll divide.
//       if (subsample.size() >0) //Buffer coefficients are given for this buffer.
//       {
//         if (this.bufferData.getNickname() == null)
//         {
//           this.bufferData.setNickname("com.root");
//           //System.out.println(this.bufferData.getClass().toString());
//           //throw new RuntimeException("See I told you so!");
//         }
//       }
//     }
   
   
    BufferSolution sol = new BufferSolution();
    sol.setConnection(this.connection);
    sol.setRepository(this.repository);
    sol.initialize();
    System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
   
   
    sol.show("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
   
    System.out.println("NICKNAME IS::"+this.bufferData.getNickname());
   
    sol.add(this.bufferData,new DblMatrix(1.0));
   
    sol.show("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
   
    this.referenceSolution = sol;
   
   
   
    //HashMap containedCnts = sol.getContents();
   
    String[] comps = sol.getComponents();
    this.containedcomponents = comps;
   
    //Set contbuffers = containedCnts.keySet();
    //Iterator iter2 = contbuffers.iterator();
   
   
    //while (iter2.hasNext())
    for (int c=0;c<comps.length;c++)
    {
      //Put in concentration and dissociation constant parameters for every contained buffer.
      //
      //String bname = (String)iter2.next();
      System.out.println("Contained component "+c+"="+comps[c]);
      String bname = comps[c];
     
      if (bname != null)
      {
        bname = bname.trim();
      }
     
      if (!bname.equals(""))
      {
        //DblMatrix avgconc = (DblMatrix)containedCnts.get(bname);
     
        DblMatrix avgconc = sol.getConcentration(bname);
        this.initializeCoefficientsFor(bname,avgconc);
      }
     
      //this.declareParameter(bname+"_C",avgconc);
      //this.avgcoefficients.setParam(bname+"_C",avgconc);
      //this.stdcoefficients.setParam(bname+"_C",new DblMatrix(0.01));
    }
   
   
   
    for (int x=0;x<exps.size();x++)
    {
      expname = (String)exps.get(x);
     
      subsample = this.bufferData.getTitrationSubsampleIDs(expname);
      HashSet seenAlready = new HashSet();
      for (int s=0;s<subsample.size();s++)
      {
        ssid = (String)subsample.get(s);
        obs = this.bufferData.getTitrationObservationNumbers(expname,ssid);
        for (int k=0;k<obs.size();k++) //For each titration
        {
       
          String obsnum = (String)obs.get(k);
          String titrant =  this.bufferData.getTitrant(expname,ssid,obsnum);
          //Only add new parameters once for each titrant that we encounter during a titration.
          if (!seenAlready.contains(titrant))
          {
            //The concentration parameter that gets created here is set to 1
            //and represents the fraction of the declared concentration that
            //is used for the aliquot (i.e. we assume that a single stock of
            //titrant was used).
            this.initializeCoefficientsFor(titrant,new DblMatrix(1.0));
            seenAlready.add(titrant);
          }
           
        }
      }
     
     
      //
      //Set pKa/pKb buffer variance components
      //Here there are possible gotcha's -- A user has a buffer with 3 pKa but they only record 2
      //Then in another experiment another user records 3 -- Solution: These kinds of conflicts will need to
      //be resolved at the database level when creating the experiment and adding it's support.
      //Assume it's been taken care of by the time we get here.  We don't want to use the numerical
      //value of the pKa as this is the thing that is changing over the subsamples.
      //It will be important to have a separate variance component for each pKa.  Consider citric acid
      // where the second pKa was recorded incorrectly by 1 pKa unit.  Alternatively, in brines, this will
      //impart automatic "smoothing" when Davies or other methods don't do a good job at adjusting the pKa/pKb
      // --Let the data speak for themselves!!--
      //
     
      subsample = this.bufferData.getBufferCoefficientsSubsampleIDs(expname);
      //We will accumulate a sum of pKa and pKb.  At the end we'll divide.
     
     
     
      if (subsample.size() >0) //Buffer coefficients are given for this buffer.
      {
     
        //Declare a nominal concentration...
        // The nominal concentration of **this** solution.
        //By default this concentration is 1.0 which is the accepted
        //standard for buffers that are simply mixtures.
        //Tyipcally the user will need to imput the actual value in the
        //edit parameters input.  Probably we will need to make this a standard
        //data entry field. Note that we only create the one parameter.
       
       
        this.declareParameter("C",new DblMatrix(1.0));
       
       
        int s = 0;
        ssid = (String)subsample.get(s);
        DblMatrix[] Ka = this.parseKa(this.bufferData.getpKa(expname,ssid));
       
        //Set the nominal salt type for this.
        this.nominalSaltType = this.parseSaltType(this.bufferData.getSaltType(expname,ssid));
        this.nominalpKa = this.parseSaltType(this.bufferData.getpKa(expname,ssid));
        this.nominalpKb = this.parseSaltType(this.bufferData.getpKa(expname,ssid));
       
       
        for (int i=0;i<Ka.length;i++) //For each ionizable group
        {
          for (int k=0;k<Ka[i].getN();k++)//For each dissociation this.declareParameter(param,thisKaSum); site on the ith ion.
          {
         
            String param = "pKa_"+i+"_"+k;
            DblMatrix thisKaSum = null;
            if (this.hasParameter(param))
            {
               thisKaSum = this.getParam(param);
              thisKaSum = thisKaSum.plus(DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0));
              this.setParam(param,thisKaSum);
              int N = ((Integer)divisors.get(param)).intValue();
              divisors.put(param,new Integer(N+1));
            }
            else
            {
              thisKaSum = DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0);
              this.declareParameter(param,thisKaSum);
              divisors.put(param,new Integer(1));
            }
          }
        }
       
        DblMatrix[] Kb = this.parseKa(this.bufferData.getpKb(expname,ssid));
        for (int i=0;i<Kb.length;i++)//For each ionizable group
        {
          for (int k=0;k<Kb[i].getN();k++)//For each dissociation site on the ith ion.
          {
            String param = "pKb_"+i+"_"+k;
           
            DblMatrix thisKbSum = null;
            if (this.hasParameter(param))
            {
              thisKbSum = this.getParam(param);
              thisKbSum = thisKbSum.plus(DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0));
              this.setParam(param,thisKbSum);
              int N = ((Integer)divisors.get(param)).intValue();
              divisors.put(param,new Integer(N+1));
            }
            else
            {
              thisKbSum = DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0);
              this.declareParameter(param,thisKbSum);
              divisors.put(param,new Integer(1));
            }
          }
        }
        // ** Assuming no errors in SaltType and that SaltType/ion orderings were the same in each subsample ...
      }
      //
         
      subsample = this.bufferData.getTitrationSubsampleIDs(expname);
      for (int s=0;s<subsample.size();s++)
      {
        ssid = (String)subsample.get(s);
        String pHprobe = this.bufferData.getpHProbe(expname,ssid);
       
        if (pHprobe.trim().equals("")) // pH probe was not recorded
        {
           pHprobe = "unknown"; //All empty pHprobe then are assumed to be the "same" probe -- typically this just means that the variance component will be larger than if pHprobes were identified properly.
        }
       
        this.declareParameter("pHProbe_"+pHprobe+"_sigma",new DblMatrix(0.01)); // the measurement error associated with reading pH with a given electrode.
       
        obs = this.bufferData.getTitrationObservationNumbers(expname,ssid);
       
        for (int k=0;k<obs.size();k++) //For each row of titration table...
        {
          String obsnum = (String)obs.get(k);
         
          String titrant = this.bufferData.getTitrant(expname,ssid,obsnum);
          this.declareParameter("titrant_"+titrant+"_sigma",new DblMatrix(0.01)); // Addumed to be the standard deviation of calibration of a 1.00 Molar stock prep.
        }
      }
    }
   
   
    //////  PRIORS //////
   
    //We'll construct standard priors (mean and std) for the parameters.  We can get fancy later on...
    Set params = this.getDeclaredParameters();
    Iterator iter = params.iterator();
    DblParamSet priors = new DblParamSet();
    String meanname = null;
    String varname = null;
   
    DblMatrix var_val;
    DblMatrix mean_val;
   
    while (iter.hasNext())
    {
      String mainparam = (String)iter.next();
     
      if (mainparam.endsWith("sigma")) //Then it is a variance component.
      {
        meanname = "mean_log_"+mainparam;
        varname = "var_log_"+mainparam; 
       
        priors.put(meanname,this.getParam(mainparam));
        priors.put(varname,new DblMatrix(0.1));
      }
      else
      {
        String[] parts = mainparam.split("_");
        if (parts.length >= 3)
        { 
          String r = parts[parts.length-3];
         
          if ((r.equals("pKa")) || (r.equals("pKb")))
          {
            Integer ion = new Integer(parts[parts.length-1]);
           
            if (ion.intValue() > 0) // Order-restriction priors.
            {
              meanname = "mean_log_delta_"+mainparam;
              varname = "var_log_delta_"+mainparam;
             
              DblMatrix delta = this.getParam(mainparam).minus(this.getpKaLeftOf(mainparam));
             
              priors.put(meanname,DblMatrix.log(delta));
              //Set variance to 1/10 the mean.
              priors.put(varname,new DblMatrix(0.01));
            }
            else
            {
              meanname = "mean_"+mainparam;
              varname = "var_"+mainparam;
             
              priors.put(meanname,this.getParam(mainparam));
              priors.put(varname,new DblMatrix(0.01));
            }
           
           
          }
          else
          {
            meanname = "mean_"+mainparam;
            varname = "var_"+mainparam;
           
            priors.put(meanname,this.getParam(mainparam));
            priors.put(varname,new DblMatrix(1.0));
          }
        }
        else
        {
          meanname = "mean_"+mainparam;
          varname = "var_"+mainparam;
         
          priors.put(meanname,this.getParam(mainparam));
          priors.put(varname,new DblMatrix(1.0));
        }
         
      }
     
    }
   
    this.setPriorParams(priors);
   
   
    /// Load titrant's BufferStorage into memory by creating BufferSolution's.  This should speed
    //things up by reducing communication with the repository (file I/O, SQL or whatever the current
    //protocol being used does).  Just do it once here rather than for every line in the titration table.
    //System.out.println("PPPPPPPPPPPPPPPPPPPPPP");
    this.titrant_solutions = new HashMap();
    String[] pname = this.Params.parameterSet();
    for (int m=0;m<pname.length;m++)
    {
      if (pname[m].startsWith("titrant_"))
      {
        //Extract the titrant name from the parameter for the variance component.
        String titrant_name = pname[m].replace("titrant_","");
        titrant_name = titrant_name.replace("_sigma","");

        sol = new BufferSolution();
        sol.setRepository(this.repository);
        sol.setConnection(this.connection);
        sol.initialize();
       
        sol.add(titrant_name,new DblMatrix(1.0));
        this.titrant_solutions.put(titrant_name,sol);
        System.out.println("ADDING THE TITRANT:"+titrant_name);
       
      }
    }
View Full Code Here

    System.out.println("Getting cummulant residual...");
   
    Vector expnames = perturbedStorage.getExperimentNames();
    String exp,ssid;
    DblMatrix[] OUT = new DblMatrix[2]; //This will be [pH][resids]
    DblMatrix resids = new DblMatrix(0);
    DblMatrix pHturb = new DblMatrix(0);
   
    BufferSolution rhsSolution = new BufferSolution();
    String[] names = rhssolution.getComponents();
   
    for (int y=0;y<names.length;y++)
    {
      System.out.println("NAMES=="+names[y]);
    }
   
    rhsSolution.setSummaryBuffer(rhssolution);
    
//    
//     BufferSolution rhsSolution = new BufferSolution();
//     rhsSolution.setRepository(this.repository);
//     rhsSolution.setConnection(this.connection);
//     rhsSolution.initialize();
//    
//    
//     //We will need to create a BufferStorage for this top level buffer (in case it has pKa/pKb)
//     //and a concentration.
//    
//     DblMatrix mean_nominal_conc = null;
//    
//     if (this.hasParameter("C")) // A nominal concentration is being given (different from the identity 1.0).
//     {
//       mean_nominal_conc = this.getParam("C");
//     }
//     else
//     {
//       mean_nominal_conc = new DblMatrix(1.0);
//     }
//    
//     DblMatrix[] OUT = new DblMatrix[2]; //This will be [pH][resids]
//    
//     DblMatrix resids = new DblMatrix(0);
//     DblMatrix pHturb = new DblMatrix(0);
//    
//     Vector expnames = perturbedStorage.getExperimentNames();
//     String exp,ssid;
//    
//    
//     //We need to add in all of the perturbed contained buffers here!!!
//     //Go through the parameters.  We should have a parameter for each concentration
//     //of a contained component as well as any pKa/pKb for that component.
//     //Form up the contained component that way (i.e. by parsing the parameter list).
//    
//     String[] pname = this.Params.parameterSet();
//     for (int m=0;m<pname.length;m++)
//      {
//       if (pname[m].endsWith("_C")) // Concentration parameter.
//       {
//        
//      
//       }
//     }         
//          
//          
//     // /// Load titrant's BufferStorage into memory by creating BufferSolution's.  This should speed
// //     //things up by reducing communication with the repository (file I/O, SQL or whatever the current
// //     //protocol being used does).  Just do it once here rather than for every line in the titration table.
// //     //System.out.println("PPPPPPPPPPPPPPPPPPPPPP");
// //     HashMap titrant_solutions = new HashMap();
// //     String[] pname = this.Params.parameterSet();
// //     for (int m=0;m<pname.length;m++)
// //     {
// //       if (pname[m].startsWith("titrant_"))
// //       {
// //         //Extract the titrant name from the parameter for the variance component.
// //         String titrant_name = pname[m].replace("titrant_","");
// //         titrant_name = titrant_name.replace("_sigma","");
// //
// //         BufferSolution sol = new BufferSolution();
// //         sol.setRepository(this.repository);
// //         sol.setConnection(this.connection);
// //         sol.initialize();
// //        
// //         sol.add(titrant_name,new DblMatrix(1.0));
// //         titrant_solutions.put(titrant_name,sol);
// //       }
// //     }
//    
    ///
    //System.out.println("PPPPPPPPPPPPPPPPPPPPPP");
    for (int i=0;i<expnames.size();i++)
    {
   
      String expname = (String)expnames.get(i);
     
      Vector subsamples = perturbedStorage.getTitrationSubsampleIDs(expname);
     
      for (int j=0;j<subsamples.size();j++)
      {
        ssid = (String)subsamples.get(j);
        Vector obs = perturbedStorage.getTitrationObservationNumbers(expname,ssid);
       
        String val;
        DblMatrix pHi,vi,ci;
        DblMatrix CumNum = new DblMatrix(1);

        DblMatrix Cummulant = new DblMatrix(obs.size());
        DblMatrix current_volume = new DblMatrix(1);
        //System.out.println("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");

        current_volume = new DblMatrix(new Double(perturbedStorage.getInitialVolume(expname,ssid)));
        System.out.println("Initial volume is:"+current_volume.getDoubleAt(0));
       
        long cpu;
        long cpu2;
       
        for (int k=0;k<obs.size();k++) //For each row of titration table...
        {
          cpu = System.nanoTime();
          String obsnum = (String)obs.get(k);
         
          val = perturbedStorage.getpH(expname,ssid,obsnum);
          //System.out.println("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
          try
          {
            pHi = new DblMatrix(new Double(perturbedStorage.getpH(expname,ssid,obsnum)));
          }
          catch (Exception err)
          {
            pHi = new DblMatrix(new Double(Double.NaN));
            throw new RuntimeException("Unable to convert pH String to Double.",err);
          }
         
          cpu2 = System.nanoTime();
          System.out.println("T1="+(cpu2-cpu));
          cpu = cpu2;
         
          //System.out.println("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
          try
          {
            vi = new DblMatrix(new Double(perturbedStorage.getVolumeAdded(expname,ssid,obsnum)));
          }
          catch (Exception err)
          {
            vi = new DblMatrix(new Double(Double.NaN));
            throw new RuntimeException("Unable to convert volume added String to Double.",err);
          }

          //System.out.println("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
          try
          {
            ci = new DblMatrix(new Double(perturbedStorage.getTitrantConcentration(expname,ssid,obsnum)));
          }
          catch (Exception err)
          {
            ci = new DblMatrix(new Double(Double.NaN));
            throw new RuntimeException("Unable to convert titrant concentration String to Double.",err);
          }

          String titrant = perturbedStorage.getTitrant(expname,ssid,obsnum);

          //Update the rhs buffer for the aliquot of titrant added.
          //Do this by making a solution representing the aliquot
          //and then mixing it with current solution in the titration
          //flask...
          //
          if (!this.titrant_solutions.containsKey(titrant))
          {
            throw new RuntimeException("titrant_solutions does not have titrant:"+titrant);
          }
         
          BufferSolution sol = (BufferSolution)this.titrant_solutions.get(titrant);
          if (sol == null)
          {
            System.out.println("sol is null for:"+titrant);
            Iterator iter2 = this.titrant_solutions.keySet().iterator();
            while (iter2.hasNext())
            {
              System.out.println("Titrant is:"+iter2.next());
            }
          }
          sol.setConcentration(titrant,ci);
         
          cpu2 = System.nanoTime();
          System.out.println("T2="+(cpu2-cpu));
          cpu = cpu2;
         
         
         
         
          //Description of bottle neck issue:  In the new approach we're adding
          //again and again usually the same titrant over to the rhs of the
          //Cb equation.  We can not affort to do a database read every bloody time.
          //Got to hash the buffer solution.
         
          //This is the killer here!!!  Need to have this run fast.
          rhsSolution = BufferSolution.mixSol(rhsSolution,current_volume,sol,vi);
         
         
         
         
         
         
          System.out.println("Catch error here!");
          rhsSolution.show("rhsSOlution");
         
         
          cpu2 = System.nanoTime();
          System.out.println("T3="+(cpu2-cpu));
          cpu = cpu2;
         
          current_volume = current_volume.plus(vi);
          //current_volume.show("current_volume");
          CumNum = CumNum.plus(ci.times(vi));
          Cummulant.setDblAt(CumNum.divideBy(current_volume),k);

          //Davies eqn etc.... is completely optional in this approach since
          //priors on pKa/pKb allow us to borrow strength from the data.
          //In the end Davies has 2 parameters and there must be a penalty
          //for estimating those two parmaeters too! Furthermore it is known
          //that Davies and Pitzer type models do quite poorly in many instances
          //using the default or suggested parameters for those models.
          System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
         
         
          rhsSolution.show("FUCKFUCKFUCKFUCKFUCKFUCKFUCKFUCKFUCKFUCKFUCK");
         
         
          HashMap cb = rhsSolution.Cb(pHi,true); //Get the partial Cb's at the pH.
          Set keys = cb.keySet();
          iter = keys.iterator();
          DblMatrix absoluteSumCb = new DblMatrix(0.0);
          while (iter.hasNext())
          {
            String partial =  (String)iter.next();
            if (!partial.equals("total")) //Don't accumulate the total in here.
            {
              System.out.println("partial="+partial);
              absoluteSumCb = absoluteSumCb.plus(DblMatrix.abs((DblMatrix)cb.get(partial)));
            }
          }
          cpu2 = System.nanoTime();
          System.out.println("T4="+(cpu2-cpu));
          cpu = cpu2;
         
         
          System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
          //System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
          resids = resids.concat(Cummulant.getDblAt(k).minus(absoluteSumCb),1);
          pHturb = pHturb.concat(pHi,1);
         
          //System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
         
          cpu2 = System.nanoTime();
          System.out.println("T6="+(cpu2-cpu));
          cpu = cpu2;
          // this.outSet.addColumn("Observed",Double.class);
          // this.outSet.addColumn("Experiment",String.class);      
          // this.outSet.addColumn("Subsample",String.class);       
          // this.outSet.addColumn("Observation",String.class);     
          // this.outSet.addColumn("pH",Double.class);            
          // this.outSet.addColumn("Cbplot",Double.class);       
            
          //System.out.println("YESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS:"+(dataSetStartRow+k));
          plotSet.setValueAt(new Double(0.0),dataSetStartRow+k,0);//Nope, these all simulated (or predicted) data.
                plotSet.setValueAt(expname,dataSetStartRow+k,1);
                 plotSet.setValueAt(ssid,dataSetStartRow+k,2);
                 plotSet.setValueAt(obsnum,dataSetStartRow+k,3);
                 plotSet.setValueAt(pHi.getDoubleAt(0),dataSetStartRow+k,4);
          //System.out.println("Cbplot="+Cummulant.getDoubleAt(k));
                //Probably don't want to plot this but rather the rhs.
                 //plotSet.setValueAt(Cummulant.getDoubleAt(k),dataSetStartRow+k,5);
          plotSet.setValueAt(absoluteSumCb.getDoubleAt(0),dataSetStartRow+k,5);
          System.out.println("FINEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
         
          cpu2 = System.nanoTime();
          System.out.println("T5="+(cpu2-cpu));
          cpu = cpu2;
        }
       
       
      }
    }
   
    //System.out.println("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW");
    OUT[0] = pHturb;
    OUT[1] = resids;
   
    DblMatrix SHOW = pHturb.concat(resids,2);
    //SHOW.show("Cummulant residuals");
    System.out.println("Finished getting cummulant residual.");
    if (plotPanelHandle != null)
    {
View Full Code Here

    Vector Exps =  this.bufferData.getExperimentNames();
    Vector Expsdelete;
    Vector SS,SSdelete,NN,NNdelete;
    String Exp,concentration,val,ssid,tid;
    boolean altered;
    DblMatrix temp;
    Expsdelete = OUT.getExperimentNames();
    //System.out.println("Here are the current experiment names in the destination buffer:");
    // for (int l=0;l<Expsdelete.size();l++)
//     {
//       System.out.println(Expsdelete.get(l));
//     }
   
    // System.out.println("Here are the current experiment names in the source buffer:");
//     for (int l=0;l<Exps.size();l++)
//     {
//       System.out.println(Exps.get(l));
//     }
//    
   
    if (Exps.size()!=0)
    {
      for (int j=0;j<Exps.size();j++)
      {
       
        Exp = (String)Exps.get(j);
        if (!Expsdelete.contains(Exp))
        {
          //System.out.println("Adding the experiment:"+Exp);
          OUT.addExperiment(Exp);
        }
       
        //System.out.println("What went wrong?");
       
        OUT.setExperimentEnabled(Exp,this.bufferData.getExperimentEnabled(Exp));
       
        //System.out.println("Here is description in"+Exp);
        OUT.setDescription(Exp,this.bufferData.getDescription(Exp));
        OUT.setPerformedBy(Exp,this.bufferData.getPerformedBy(Exp));
        OUT.setPerformedOn(Exp,this.bufferData.getPerformedOn(Exp));
       
        //Add the subsample measurements of "static" buffer coefficients (pKa,pKb etc).
        SS =  this.bufferData.getBufferCoefficientsSubsampleIDs(Exp);

        SSdelete = OUT.getBufferCoefficientsSubsampleIDs(Exp);
        if (SS.size()>0)
        {
          for (int k=0;k<SS.size();k++) //Each subsample...
          {
            ssid = (String)SS.get(k);
            //System.out.println("Showing ssid:"+ssid);
            if (!SSdelete.contains(ssid))
            {
              OUT.addBufferCoefficientsSubsample(Exp,ssid);
            }
           
            //System.out.println("Setting pKa Here----"+ssid);
            DblMatrix[] Ka = this.parseKa(this.bufferData.getpKa(Exp,ssid));
            String newpKa = "";
           
            for (int i=0;i<Ka.length;i++) //For each ion...
           
              if (i>0)
              {
                newpKa = newpKa+",[";
              }
              else
              {
                newpKa = newpKa+"[";
              }
             
              for (int m=0;m<Ka[i].getN();m++) //For each pKa
              {
                if (m>0)
                {
                  newpKa = newpKa+",";
                }
               
                String param = "pKa_"+m+"_sigma";
               
                if (this.hasParameter(param))
                {
                  //We model the joint distribution of an ordered list of pKa
                  //for this molecule.  This should work for example
                 
                  DblMatrix pKak =   DblMatrix.log10(Ka[i].getDoubleAt(m)).times(-1.0);
                 
                  if (i > 0//Sample the delta difference between the pKa which
                  {               //will enforce order-restricted inference.
                 
                    DblMatrix pKakm1 =   DblMatrix.log10(Ka[i-1].getDoubleAt(m)).times(-1.0);
                    DblMatrix delta_k = pKak.minus(pKakm1);
                    this.normal.setMean(DblMatrix.log(delta_k));
                    this.normal.setStd(this.getParam(param));
                    temp = pKak.plus(DblMatrix.exp(this.normal.random(1)));
                  }
                  else
                 
                    this.normal.setMean(pKak);
                    this.normal.setStd(this.getParam(param));
                    temp = this.normal.random(1); //Perturbed pKa.
                  }
                   
                  newpKa = newpKa+temp.getDoubleAt(0);
               
                }
                else //We are not to estimate this pKa -- just use the given value.
                {
                  DblMatrix pKa = DblMatrix.log10(Ka[i].getDblAt(m)).times(-1.0);
                  newpKa = newpKa+pKa.getDoubleAt(0).toString();
                }
              }
              newpKa = newpKa+"]";
            }
           
            //System.out.println("Setting pKa Here----"+ssid);
            DblMatrix[] Kb = this.parseKa(this.bufferData.getpKb(Exp,ssid));
            String newpKb = "";
           
            for (int i=0;i<Kb.length;i++)
            { 
              if (i>0)
              {
                newpKb = newpKb+",[";
              }
              else
              {
                newpKb = newpKb+"[";
              }
             
              for (int m=0;m<Kb[i].getN();m++)
              {
                if (m>0)
                {
                  newpKb = newpKb+",";
                }
               
                String param = "pKb_"+m+"_sigma";
               
                if (this.hasParameter(param))
                {
               
                  //We model the joint distribution of an ordered list of pKa
                  //for this molecule.  This should work for example
                 
                  DblMatrix pKbk =   DblMatrix.log10(Kb[i].getDoubleAt(m)).times(-1.0);
                 
                  if (i > 0//Sample the delta difference between the pKb which
                  {               //will enforce order-restricted inference.
                 
                    DblMatrix pKbkm1 =   DblMatrix.log10(Kb[i-1].getDoubleAt(m)).times(-1.0);
                    DblMatrix delta_k = pKbk.minus(pKbkm1);
                    this.normal.setMean(DblMatrix.log(delta_k));
                    this.normal.setStd(this.getParam(param));
                    temp = pKbk.plus(DblMatrix.exp(this.normal.random(1)));
                  }
                  else
                 
                    this.normal.setMean(pKbk);
                    this.normal.setStd(this.getParam(param));
                    temp = this.normal.random(1); //Perturbed pKb.
                  }
                   
                  newpKb = newpKb+temp.getDoubleAt(0);
                 
                 
                 
                }
                else
                {
                  DblMatrix pKb = DblMatrix.log10(Kb[i].getDblAt(m)).times(-1.0);
                  newpKb = newpKb+pKb.getDoubleAt(0).toString();
                }
              }
             
              newpKb = newpKb+"]";
            }
           
            OUT.setpKa(Exp,ssid,newpKa);
            OUT.setpKb(Exp,ssid,newpKb);
            OUT.setSaltType(Exp,ssid,this.bufferData.getSaltType(Exp,ssid));
          }
        }

        //Remove those BufferCoefficientSubsamples not in "that" experiment. 
        //SSdelete = OUT.getBufferCoefficientsSubsampleIDs();
        altered = SSdelete.removeAll(SS);
        if (SSdelete.size()>0)
        {
          for (int m=0;m<SSdelete.size();m++)
          {
            ssid = (String)SSdelete.get(m);
            OUT.removeBufferCoefficientsSubsample(Exp,ssid);
          }
        }

        ///////////////////////////////////////////

        //Add the subsample measurements of contained buffers.
        SS =  this.bufferData.getContainedSubsampleIDs(Exp);
        SSdelete = OUT.getContainedSubsampleIDs(Exp);
        if (SS.size()>0)
        {
          for (int k=0;k<SS.size();k++)
          {
            ssid = (String)SS.get(k);

            if (!SSdelete.contains(ssid))
            {

              OUT.addContainedSubsample(Exp,ssid);
            }

            NN = this.bufferData.getContainedObservationNumbers(Exp,ssid);
            NNdelete = OUT.getContainedObservationNumbers(Exp,ssid);

            for (int l=0;l<NN.size();l++)
            {
                    tid = (String)NN.get(l);

              if ((tid == null) || (tid.equals("")))
              {
                throw new RuntimeException("Observation number can not be empty.");
              }

              if (!NNdelete.contains(tid))
              { 

                OUT.addContainedObservation(Exp,ssid,tid);
              }

                    val = this.bufferData.getContainedNickname(Exp,ssid,tid);
                    OUT.setContainedNickname(Exp,ssid,tid,val);
              String var_comp = new String("contained_"+val+"_sigma");
             
                    val = this.bufferData.getContainedConcentration(Exp,ssid,tid);
              temp = new DblMatrix(new Double(val));
              this.normal.setMean(DblMatrix.log(temp));
              this.normal.setStd(this.getParam(var_comp));
              temp = DblMatrix.exp(this.normal.random(1));
              val = temp.getDoubleAt(0).toString();
                    OUT.setContainedConcentration(Exp,ssid,tid,val);
            }

            ///Remove any observations not in the new data.
            //NNdelete = OUT.getContainedObservationNumbers(ssid);
            altered = NNdelete.removeAll(NN);
            if (NNdelete.size()>0)
            {
              for (int m=0;m<NNdelete.size();m++)
              {
                tid = (String)NNdelete.get(m);
                OUT.removeContainedObservation(Exp,ssid,tid);
              }
            }
          }
        }

        //Remove those ContainedSubsamples not in "that" experiment. 
        //SSdelete = OUT.getContainedSubsampleIDs();
        altered = SSdelete.removeAll(SS);
        if (SSdelete.size()>0)
        {
          for (int m=0;m<SSdelete.size();m++)
          {
            ssid = (String)SSdelete.get(m);
            OUT.removeContainedSubsample(Exp,ssid);
          }
        }

        ///////////////////////////////////////////

        //Add the titration measurements of contained buffers.

        SS = this.bufferData.getTitrationSubsampleIDs(Exp);
        //System.out.println("This is the experiment"+Exp);
        //System.out.println("This is the number of SS"+SS.size());
       
        SSdelete = OUT.getTitrationSubsampleIDs(Exp);
        if (SS.size()>0)
        {
          for (int k=0;k<SS.size();k++)
          {
            ssid = (String)SS.get(k);
            //System.out.println("ssid="+ssid);
            if (!SSdelete.contains(ssid))
            {
              OUT.addTitrationSubsample(Exp,ssid)
            }
           
            OUT.setInitialVolume(Exp,ssid,this.bufferData.getInitialVolume(Exp,ssid));
            OUT.setTemperature(Exp,ssid,this.bufferData.getTemperature(Exp,ssid));
            OUT.setDielectric(Exp,ssid,this.bufferData.getDielectric(Exp,ssid));
            String pHprobe = this.bufferData.getpHProbe(Exp,ssid);
            OUT.setpHProbe(Exp,ssid,pHprobe);
            if (pHprobe.trim().equals("")) // pH probe was not recorded
            {
               pHprobe = "unknown"; //All empty pHprobe then are assumed to be the "same" probe -- typically this just means that the variance component will be larger than if pHprobes were identified properly.
            }
           
            DblMatrix pH_var_comp = this.getParam("pHProbe_"+pHprobe+"_sigma");
           
            pH_var_comp.show("Here is the pH variance component:");
           
            NN = this.bufferData.getTitrationObservationNumbers(Exp,ssid);
            NNdelete = OUT.getTitrationObservationNumbers(Exp,ssid);
            for (int l=0;l<NN.size();l++)
            {
              tid = (String)NN.get(l);
              if (tid == null)
              {
                throw new RuntimeException("Titration observation number can not be null.");
              }
             
              if (!NNdelete.contains(tid))
              {
                OUT.addTitrationPoint(Exp,ssid,tid)
              }

              val = this.bufferData.getTitrant(Exp,ssid,tid);
              OUT.setTitrant(Exp,ssid,tid,val);
             
             
              this.normal.setStd(this.getParam("titrant_"+val+"_sigma"));
               
              val = this.bufferData.getTitrantConcentration(Exp,ssid,tid);
              this.normal.setMean(DblMatrix.log(new DblMatrix(new Double(val))));
              temp = DblMatrix.exp(this.normal.random(1));
              OUT.setTitrantConcentration(Exp,ssid,tid,temp.getDoubleAt(0).toString());
             
              //this.getParam("aliquot_sigma").show("Aliquot sigma:");
              this.normal.setStd(this.getParam("aliquot_sigma"));
             
              val = this.bufferData.getVolumeAdded(Exp,ssid,tid);
              //this.normal.setMean(DblMatrix.log(new DblMatrix(new Double(val))));
              this.normal.setMean(new DblMatrix(new Double(val)));
             
              temp = this.normal.random(1);//Can be + or - due to bubble or drop errors.
             
              OUT.setVolumeAdded(Exp,ssid,tid,temp.getDoubleAt(0).toString());
             
              this.normal.setStd(pH_var_comp);//Depends on the pH probe being used.
             
              val = this.bufferData.getpH(Exp,ssid,tid);
              this.normal.setMean(DblMatrix.log(new DblMatrix(new Double(val))));
              temp = DblMatrix.exp(this.normal.random(1));
             
              OUT.setpH(Exp,ssid,tid,val);

              val = this.bufferData.getRemark(Exp,ssid,tid);
View Full Code Here

   
    BufferStorage perturbedBuffer = this.getPerturbedSupport();
    DblMatrix[] R = this.getCummulantResiduals(perturbedBuffer,true);
   
    DblMatrix[] pHi = new DblMatrix[]{R[0]};
    DblMatrix LOF = R[1]; //LOF trend
   
   
    System.out.println("Instantiating Lpreg...");
    Lpreg lpreg = new Lpreg(pHi,LOF);
    System.out.println("Finished instantiating Lpreg");
   
    System.out.println("Setting smoothing parameter...");
    NNBandwidth BW = (NNBandwidth)lpreg.getBandwidthMethod();
    FixedSmoothMethod SM = (FixedSmoothMethod)BW.getSmoothMethod();
    DblMatrix sp = this.getParam("smoothing_param");
   
    if (DblMatrix.test(sp.lt(0.01))) //Can't let window get so small that the number of data points is less than the model df+1+number of derivatives you want to estimate.
    {
      return(DblMatrix.INF);
    }
   
    SM.setSmoothParameter(sp.getDoubleAt(0)); //The proportion of N that gives K in the NN method.
    System.out.println("Finished setting smoothing parameter...");
   
   
    System.out.println("Lpreg prediction...");
    //DblMatrix LOFhat = lpreg.predict(pHi); //These should be iid Normal
   
    //this.normal.setStd(new DblMatrix(0.1));
    //this.normal.setMean(new DblMatrix(0.0));
    //DblMatrix LOFhat = this.normal.random(LOF.getN());
   
   
    //Debugging!!! Just set LOF to zeros.
    DblMatrix LOFhat = new DblMatrix(R[0].getN());
   
    System.out.println("Finished Lpreg prediction.");
    DblMatrix residuals = LOF.minus(LOFhat);
   
    this.normal.setMean(new DblMatrix(0.0));
    this.normal.setStd(this.getParam("sigma"));
   
    DblMatrix out = DblMatrix.log(this.normal.pdf(residuals)).times(-1.0);       
               
                return(out);
        }
View Full Code Here

       
 
 
        public DblMatrix residuals()
        {
    DblMatrix resids = null;
    DblMatrix OUT = new DblMatrix(0);
    Vector exps = this.bufferData.getExperimentNames();
    String expname;
    Vector subsample;
    String ssid;
   
    for (int x=0;x<exps.size();x++)
    {
      expname = (String)exps.get(x);
      subsample = this.bufferData.getTitrationSubsampleIDs(expname);
      for (int s=0;s<subsample.size();s++)
      {
        ssid = (String)subsample.get(s);
        //DblMatrix cummulant = this.getCummulant(expname,ssid,false);
        DblMatrix cummulant =  new DblMatrix(0.0);
        resids = new  DblMatrix(cummulant.Size);
        OUT = OUT.concat(resids,1)
      }
    }
   
    return(OUT);
View Full Code Here

       
        public DblMatrix getPredictionAt(DblMatrix[] X)
        {
    //Note: probably the X should be the hidden (i.e. randomly drawn pH value rather than the observed pH values).
 
    DblMatrix pH = X[0];
    DblMatrix OUT = new DblMatrix(pH.Size); // the predicted cummulant...
   
                return(OUT);
        }
View Full Code Here

    Vector pKaSet = new Vector();
    int ionStart=0;
    int ionStop=0;
    String ion;
    boolean readingIon=false;
    DblMatrix val;
    int k=0;
    while (k<pKa.length())
    {
      if (pKa.charAt(k)=='[')
      {
        if (readingIon)
        {
          throw new RuntimeException("Encountered extra '[' in buffer coefficients.")
        }
        readingIon = true;
        ionStart = k;
       
      }
      else
      {
        if (pKa.charAt(k)==']')
        {
          if (!readingIon)
          {
            throw new RuntimeException("Encountered extra ']' in buffer coefficients.")
          }
          ionStop = k+1;
          ion = pKa.substring(ionStart,ionStop);
          pKaSet.add(new DblMatrix(ion));
         
          readingIon = false;
        }
      }
      k++;
View Full Code Here

    Vector pKaSet = new Vector();
    int ionStart=0;
    int ionStop=0;
    String ion;
    boolean readingIon=false;
    DblMatrix val;
    int k=0;
    while (k<pKa.length())
    {
      if (pKa.charAt(k)=='[')
      {
        if (readingIon)
        {
          throw new RuntimeException("Encountered extra '[' in buffer coefficients.")
        }
        readingIon = true;
        ionStart = k;
       
      }
      else
      {
        if (pKa.charAt(k)==']')
        {
          if (!readingIon)
          {
            throw new RuntimeException("Encountered extra ']' in buffer coefficients.")
          }
          ionStop = k+1;
          ion = pKa.substring(ionStart,ionStop);
          pKaSet.add(new DblMatrix(ion));
         
          readingIon = false;
        }
      }
      k++;
View Full Code Here

TOP

Related Classes of com.mockturtlesolutions.snifflib.datatypes.DblMatrix

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.