Exemple #1
0
        /// <summary>
        /// Export the raw data as well as the results of fitting the data.
        /// </summary>
        /// <param name="FullFileName">Name of file to write.</param>
        /// <param name="GCC">Collection of GrowthCurves to export</param>
        public static void ExportData(string FullFileName, GrowthCurveCollection GCC)
        {
            StreamWriter SW = new StreamWriter(FullFileName);

            SW.WriteLine(String.Join(",", OutputColumnCollection.Select((x) => x.Name)));
            foreach (GrowthCurve GR in GCC)
            {
                SW.WriteLine(String.Join(",", OutputColumnCollection.Select((x) => SafeGet(x.outFunc, GR))));
            }
            //Below assumes the time is the same for all of them
            //Below assumes the time is the same for all of them

            SW.WriteLine(Intermissionline);
            SW.Write("DateTime,");
            SW.WriteLine(String.Join(",", GCC.Select((x) => x.DataSetName + " OD,Flag")));
            HashSet <DateTime> dtimes = new HashSet <DateTime>();

            foreach (GrowthCurve gc in GCC)
            {
                foreach (DateTime dt in gc.Select((x) => x.time))
                {
                    dtimes.Add(dt);
                }
            }
            List <DateTime> DateTimesinFile = dtimes.Select((x) => x).ToList();

            DateTimesinFile.Sort();
            foreach (DateTime DT in DateTimesinFile)
            {
                string line = DT.ToString() + ",";
                foreach (GrowthCurve GR in GCC)
                {
                    int        indexPos   = -1;
                    DateTime[] timeValues = GR.Times;
                    if (SimpleFunctions.ValueInArray(timeValues, DT, ref indexPos))
                    {
                        //decide if this timepoint was included
                        line += GR.ODValues[indexPos].ToString() + ",";
                        double DateX = GR[indexPos].time_as_double;
                        if (GR.FittedXValues != null && SimpleFunctions.ValueInArray(GR.FittedXValues, DateX))//now decide if it made it into the fit
                        {
                            line += "0,";
                        }
                        else
                        {
                            line += "1,";
                        }
                    }
                    else
                    {
                        line += "-999,-999,";
                    }
                }
                SW.WriteLine(line);
            }
            SW.Close();
        }
        public GroupFitter(GrowthCurveCollection GCC)
        {
            var tmpHold            = new List <LightWeightGC>();
            HashSet <double> times = new HashSet <double>();
            Random           r     = new Random();

            foreach (GrowthCurve gc in GCC)
            {
                if (gc.ExpFit == null || gc.ExpFit.SuccessfulFit == false)
                {
                    throw new Exception("Curve " + gc.DataSetName + " has not been fit with an exponential yet");
                }
                LightWeightGC tmp = new LightWeightGC()
                {
                    Xvalues = gc.FittedXValues, YValues = gc.FittedYValues, Name = gc.DataSetName
                };
                tmpHold.Add(tmp);
                NamesToCurves[gc.DataSetName] = tmp;
                xl.AddRange(gc.FittedXValues);
                yl.AddRange(gc.FittedYValues);
                gc.FittedXValues.ToList().ForEach(x => times.Add(x));
                gc.GroupFit = this;
            }
            //Now to make the data array
            int ParameterArraySize = times.Count + 2 * tmpHold.Count;

            var t2 = times.ToList();

            data = tmpHold.ToArray();
            t2.Sort();
            Times = t2.ToArray();
            //First items 1-n are the time point offsets, next are the GrowthRate,InitPop for the different guys
            Parameters = new double[ParameterArraySize];
            int ArrayStart = times.Count;

            for (int i = 0; i < data.Length; i++)
            {
                if (i < Times.Length)
                {
                    //Parameters[i] = 1e-3;
                }

                data[i].GrowthParameterStart = ArrayStart + (i * 2);
                double v = r.NextDouble() < .5 ? -1.0:1.0;
                Parameters[data[i].GrowthParameterStart] = GCC[i].ExpFit.GrowthRate;         // +GCC[i].ExpFit.GrowthRate * (r.NextDouble() * .005) * v;
                v = r.NextDouble() < .5 ? -1.0:1.0;
                Parameters[data[i].GrowthParameterStart + 1] = GCC[i].ExpFit.InitialPopSize; // +GCC[i].ExpFit.InitialPopSize * (r.NextDouble() * .005) * v;
                double minTime = data[i].Xvalues[0];
                data[i].timeParameterStart = t2.IndexOf(minTime);
            }
            double val  = ShoNS.Optimizer.GradTester.Test(new DiffFunc(GetDerivatives), Parameters);
            double val3 = val + .01;

            val3++;
            FitModel();
        }
Exemple #3
0
        /// <summary>
        /// Exports data as a CSV file with the first column being the date/time encoded as a double.
        /// Useful for loading data in Matlab.
        /// </summary>
        /// <param name="FullFileName"></param>
        /// <param name="GCC"></param>
        public static void ExportMatlabData(string FullFileName, GrowthCurveCollection GCC)
        {
            StreamWriter     SW        = new StreamWriter(FullFileName);
            string           TitleLine = "Time,";//this will hold the titles for everything below
            HashSet <double> Times     = new HashSet <double>();

            foreach (GrowthCurve GC in GCC)
            {
                foreach (double d in GC.Select((x) => x.time_as_double))
                {
                    Times.Add(d);
                }
                TitleLine += GC.ToString() + ",";
            }
            //Below assumes the time is the same for all of them
            SW.WriteLine(TitleLine);
            List <double> times = Times.ToList();

            times.Sort();
            foreach (double time in times)
            {
                string line = time.ToString() + ",";
                foreach (GrowthCurve GR in GCC)
                {
                    int      indexPos = -1;
                    double[] timesGC  = GR.TimeValues_As_Double;

                    if (SimpleFunctions.ValueInArray(timesGC, time, ref indexPos))
                    {
                        //decide if this timepoint was included
                        line += GR.ODValues[indexPos].ToString() + ",";
                        double DateX = GR[indexPos].time_as_double;
                    }
                    else
                    {
                        line += "-999,";
                    }
                }
                SW.WriteLine(line);
            }
            SW.Close();
        }
Exemple #4
0
        public static void ExportDataDEPRECATED(string FullFileName, GrowthCurveCollection GCC)
        {
            bool LagData = false;
            //FullFileName = "C:\\FullName.csv";
            StreamWriter SW = new StreamWriter(FullFileName);

            SW.WriteLine("Fitted Data Results");
            SW.WriteLine("Name, Doubling Time(Hrs),Growth Rate, How Determined?,NumPoints,R2,RMSE, Maximum GrowthRate,MaxOD,Notes,Linear-Fit Slope,Reduction in absolute error from ExpFit,LagTime,Reduction in Sum of Squares from Exp Fit,TimeTill_OD_0.02,EndOD,TreatmentGroup");
            string TitleLine = "Time,";//this will hold the titles for everything below

            foreach (GrowthCurve GR in GCC)
            {
                LagData = false;

                TitleLine += GR.ToString() + " OD," + "Flag,";
                string newline      = GR.ToString() + ",";
                double ActualGrowth = Math.Log(2) / GR.GrowthRate.GrowthRate;
                if (GR.ValidDataSet)
                {
                    newline += ActualGrowth.ToString("n5") + "," + GR.GrowthRate.GrowthRate.ToString() + "," + GR.GrowthRate.FittingUsed + "," + GR.GrowthRate.NumPoints + "," + GR.GrowthRate.R2.ToString("n4") + ","
                               + GR.GrowthRate.RMSE.ToString("n5") + "," + GR.MaxGrowthRate.MaxGrowthRate.ToString("n5") + "," + GR.ODValues.Max().ToString("n4") + "," + GR.GrowthRate.Notes;
                    if (GR.LinearModelFitted && GR.ExpModelFitted)
                    {
                        double dif  = GR.LinFit.AbsError - GR.ExpFit.AbsError;
                        double dif2 = GR.LinFit.calculateResidualSumofSquares() - GR.ExpFit.calculateResidualSumofSquares();
                        newline += "," + GR.LinFit.Parameters[1].ToString("n5") + "," + dif.ToString("n5") + ",DEPRECATED," + dif2.ToString("n5") + ",";
                    }//+","+RMSEdiff.ToString("n5")+","+GR.LinFit.RMSE.ToString()+","+GR.ExpFit.RMSE.ToString(); }//report the linear fitted slope if possible
                    else
                    {
                        newline += ",No Exp Fit to Compare Against,";
                    }
                    newline += GR.HoursTillODReached(.02).ToString() + ",";
                    newline += GR.ODValues.Last().ToString() + ",";
                }
                else
                {
                    newline += ",,Weird Data:Blank??,,,,,,,,,,,,,";
                }
                SW.WriteLine(newline);
            }
            //Below assumes the time is the same for all of them
            string Intermissionline = "Complete Data Listing Below";

            if (LagData)
            {
                Intermissionline += "-Initial OD Present";
            }
            ;
            SW.WriteLine(Intermissionline);
            SW.WriteLine(TitleLine);
            HashSet <DateTime> dtimes = new HashSet <DateTime>();

            foreach (GrowthCurve gc in GCC)
            {
                foreach (DateTime dt in gc.Select((x) => x.time))
                {
                    dtimes.Add(dt);
                }
            }
            List <DateTime> DateTimesinFile = dtimes.Select((x) => x).ToList();

            DateTimesinFile.Sort();
            foreach (DateTime DT in DateTimesinFile)
            {
                string line = DT.ToString() + ",";
                foreach (GrowthCurve GR in GCC)
                {
                    int        indexPos   = -1;
                    DateTime[] timeValues = GR.Times;
                    if (SimpleFunctions.ValueInArray(timeValues, DT, ref indexPos))
                    {
                        //decide if this timepoint was included
                        line += GR.ODValues[indexPos].ToString() + ",";
                        double DateX = GR[indexPos].time_as_double;
                        if (GR.FittedXValues != null && SimpleFunctions.ValueInArray(GR.FittedXValues, DateX))    //now decide if it made it into the fit
                        {
                            line += "0,";
                        }
                        else
                        {
                            line += "1,";
                        }
                    }
                    else
                    {
                        line += "-999,-999,";
                    }
                }
                SW.WriteLine(line);
            }
            SW.Close();
        }
        public static GrowthCurveCollection importPreviousDataFile(string file)
        {
            StreamReader          SR  = new StreamReader(file);
            GrowthCurveCollection GCC = new GrowthCurveCollection();

            //drop header lines
            SR.ReadLine();
            string        line            = "";
            List <string> FitSummaryLines = new List <string> ();

            while ((line = SR.ReadLine()) != null)
            {
                if (!line.StartsWith("Complete Data Listing Below"))
                {
                    FitSummaryLines.Add(line);
                }
                else
                {
                    break;
                }
            }
            //now to grab the more important data
            List <string[]> CompleteDataListing = new List <string[]> ();

            SR.ReadLine();             //blow through headerline
            while ((line = SR.ReadLine()) != null)
            {
                if (line.Length > 3)
                {
                    CompleteDataListing.Add(line.Split(','));
                }
            }
            //now to add the datetimes to the file
            DateTime[] AllDateTimes = new DateTime[CompleteDataListing.Count];
            for (int i = 0; i < CompleteDataListing.Count; i++)
            {
                AllDateTimes [i] = (Convert.ToDateTime(CompleteDataListing [i] [0]));
            }
            //now to convert this into something useful
            for (int i = 0; i < FitSummaryLines.Count; i++)
            {
                List <DateTime> DateTimeArray = new List <DateTime> ();
                List <double>   ODValuesArray = new List <double> ();

                string name = (string)FitSummaryLines [i];
                name = name.Split(',') [0];
                string notes = (string)FitSummaryLines [i];
                notes = notes.Split(',') [9];
                int        dataposition = 1 + i * 2;
                int        indexPos     = 0;
                List <int> IndexesToFit = new List <int> ();
                for (int j = 0; j < CompleteDataListing.Count; j++)
                {
                    if ((CompleteDataListing [j] as string[]) [dataposition] != "-999")
                    {
                        DateTimeArray.Add(Convert.ToDateTime(CompleteDataListing [j] [0]));
                        ODValuesArray.Add(Convert.ToDouble(CompleteDataListing [j] [dataposition]));
                        int flag = Convert.ToInt32(CompleteDataListing [j] [dataposition + 1]);
                        if (flag == 0)
                        {
                            IndexesToFit.Add(indexPos);
                        }
                        indexPos++;
                    }
                }
                DateTime[]  Times    = DateTimeArray.ToArray();
                double[]    ODvalues = ODValuesArray.ToArray();
                GrowthCurve GD       = new GrowthCurve(name, Times, ODvalues);
                GD.SetFittedRangeFromIndexes(IndexesToFit);
                GCC.Add(GD);
            }
            return(GCC);
        }