Exemplo n.º 1
0
        //
        // Neuro Temp Data Processing:
        // Look for the time series of data, Starting after a line that starts with 'Scan,'
        // For each time series (in odd columns):
        //    start_val = mean(first 5 elements)
        // start_elem_ix = index of ANY channel that is 0.2 higher than the start_val
        // stop_elem_ix = start_elem_ix+interval
        // analysis_range = [stop_elem_ix-10:stop_elem_ix-1]         // This is 10 elements!
        // output_value = mean(analysis_range) - start_val

        public static void ProcessCRMTempData(string[] infiles, string outfile, double interval,
                                              bool doPlots = true)
        {
            using (StreamWriter tw = new StreamWriter(outfile))
            {
                string[] headers = new string[] {
                    "" /* line number */,
                    "Measurement Filename",
                    String.Join(",",
                                Enumerable.Range(1, 4).Select(x => "Probe " + x.ToString())
                                )
                };
                tw.WriteLine(String.Join(",", headers));
                int i = 0;

                foreach (string fn in infiles)
                {
                    StreamReader    sr       = new StreamReader(fn);
                    string          namepart = Path.GetFileName(fn);
                    string          line;
                    List <double>[] values = new List <double> [4];
                    for (int j = 0; j < 4; j++)
                    {
                        values[j] = new List <double>();
                    }
                    // Skip header line
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line == "")
                        {
                            continue;
                        }
                        string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 5)
                        {
                            throw new FormatException("Wrong number of element in data row of: " + fn);
                        }
                        for (int j = 1; j <= 4; j++) // skip first element
                        {
                            values[j - 1].Add(Double.Parse(parts[j]));
                        }
                    }
                    ;
                    if (values[0].Count == 0)
                    {
                        throw new FormatException("Data file is too short. No data samples" + fn);
                    }
                    var startvals = values.Select(
                        v => v.GetRange(0, 5).Mean()
                        ).ToArray();
                    int start_elem_ix = -1;
                    for (int j = 0; j < 4; j++)
                    {
                        int ix2 = values[j].FindIndex(x => x > startvals[j] + 0.2);

                        if (ix2 < values[j].Count)
                        {
                            if (ix2 >= 0 && (start_elem_ix == -1 || start_elem_ix > ix2) && values[j][ix2 + 1] > values[j][ix2] + 0.2)
                            {
                                start_elem_ix = ix2;
                            }
                        }
                    }
                    if (start_elem_ix < 6 || start_elem_ix == -1)
                    {
                        MessageBox.Show("Start element was found to be invalid [<=10 || not found], ix=" +
                                        start_elem_ix.ToString() + ", in file " +
                                        fn);
                        continue;
                    }
                    //throw new FormatException("Start element was found to be invalid [<=10 || not found], ix=" +
                    //    start_elem_ix.ToString() + ", in file " +
                    //    fn);
                    int end_elem_ix = start_elem_ix + ((int)interval);
                    var endvals     = values.Select(
                        v => v.GetRange(end_elem_ix - 10, 10).Mean()
                        ).ToArray();
                    var deltaT    = startvals.Zip(endvals, (x1, x2) => (x2 - x1));
                    var lineparts = new string[]
                    {
                        i.ToString(), // line number
                        namepart,     // filename
                        String.Join(",", deltaT.Select(x => x.ToString()))
                    };
                    tw.WriteLine(
                        String.Join(",", lineparts)
                        );
                    if (doPlots)
                    {
                        PlotTempSeriesForm ptsf = new PlotTempSeriesForm(
                            title: namepart, data: values, startvals: startvals,
                            startx2: start_elem_ix,
                            endx1: end_elem_ix - 10, endx2: end_elem_ix - 1
                            );
                        ptsf.Show();
                        i++;
                    }
                }
            }
        }
Exemplo n.º 2
0
        // FIXME: The 0.2 constant threshold for RF on seems perhaps too low
        // FIXME: Assumption that the sample rate is exactly 1 Hz
        //
        // Neuro Temp Data Processing:
        // Look for the time series of data, Starting after a line that starts with 'Scan,'
        // For each time series (in odd columns):
        //    start_val = mean(first 30 elements)           [ or maybe it should be 30???]
        // start_elem_ix = index of first element of probe 1 that is its start_val+0.2
        // stop_elem_ix = start_elem_ix+interval
        // analysis_range = [stop_elem_ix-10:stop_elem_ix-1]         // This is 10 elements!
        // output_value = mean(analysis_range) - start_val

        public static void ProcessNeuroTempData(string[] infiles, string outfile, double interval,
                                                bool doPlots = true)
        {
            using (StreamWriter tw = new StreamWriter(outfile))
            {
                int i = 0;
                foreach (string fn in infiles)
                {
                    StreamReader sr       = new StreamReader(fn);
                    string       namepart = Path.GetFileName(fn);
                    string       line;
                    int          number_channels;
                    do
                    {
                        line = sr.ReadLine();
                    } while (line != null && !line.StartsWith("Total Channels", StringComparison.InvariantCultureIgnoreCase));
                    string[] line_break = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    number_channels = Int32.Parse(line_break[1]);
                    List <double>[] values = new List <double> [number_channels];
                    for (int j = 0; j < number_channels; j++)
                    {
                        values[j] = new List <double>();
                    }
                    do
                    {
                        line = sr.ReadLine();
                    } while (line != null && !line.StartsWith("scan,", StringComparison.InvariantCultureIgnoreCase));

                    // Skip header line
                    if (line == null)
                    {
                        throw new FormatException("Data file is too short. No data samples" + fn);
                    }

                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line == "")
                        {
                            continue;
                        }
                        string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 2 * (number_channels + 1))
                        {
                            throw new FormatException("Wrong number of element in data row of: " + fn);
                        }
                        for (int j = 1; j <= number_channels; j++) // skip first element
                        {
                            values[j - 1].Add(Double.Parse(parts[j * 2]));
                        }
                    }
                    ;
                    if (values[0].Count == 0)
                    {
                        throw new FormatException("Data file is too short. No data samples" + fn);
                    }
                    var startvals = values.Select(
                        v => v.GetRange(0, 30).Mean()
                        ).ToArray();

                    //int start_elem_ix = values[0].Skip(30).ToList().FindIndex(x => x > startvals[0] + 0.2) + 30; // Ignore the first 30 elements
                    int start_elem_ix = -1;

                    for (int j = 0; j < number_channels; j++)
                    {
                        int ix2 = values[j].FindIndex(x => x > startvals[j] + 0.3);
                        if (ix2 >= 30 && (start_elem_ix == -1 || start_elem_ix > ix2))
                        {
                            start_elem_ix = ix2;
                        }
                    }
                    if (start_elem_ix == -1)
                    {
                        MessageBox.Show("Start element was found to be invalid [<=10 || not found], ix=" +
                                        start_elem_ix.ToString() + ", in file " +
                                        fn);
                        continue;
                    }
                    //throw new FormatException("Start element was found to be invalid, ix=" +
                    //    start_elem_ix.ToString() + ", in file " +
                    //    fn);
                    int end_elem_ix = start_elem_ix + ((int)interval);
                    if (end_elem_ix >= values[0].Count)
                    {
                        MessageBox.Show("In file " + fn + ", the RF pulse start + interval position was past the end of the data");
                        continue;
                    }
                    //throw new FormatException("In file " + fn + ", the RF pulse start + interval position was past the end of the data");
                    var endvals = values.Select(
                        v => v.GetRange(end_elem_ix - 10, 10).Mean()
                        ).ToArray();
                    var deltaT    = startvals.Zip(endvals, (x1, x2) => (x2 - x1));
                    var lineparts = new string[]
                    {
                        i.ToString(), // line number
                        namepart,     // filename
                        String.Join(",", deltaT.Select(x => x.ToString()))
                    };
                    string[] headers = new string[] {
                        "" /* line number */,
                        "Filename",
                        String.Join(",",
                                    Enumerable.Range(1, number_channels).Select(x => "Probe " + x.ToString())
                                    )
                    };
                    if (i == 0)
                    {
                        tw.WriteLine(String.Join(",", headers));
                    }
                    tw.WriteLine(
                        String.Join(",", lineparts)
                        );
                    if (doPlots)
                    {
                        PlotTempSeriesForm ptsf = new PlotTempSeriesForm(
                            title: namepart, data: values, startvals: startvals,
                            startx2: start_elem_ix,
                            endx1: end_elem_ix - 10, endx2: end_elem_ix - 1
                            );
                        ptsf.Show();
                    }
                    i++;
                }
            }
        }