예제 #1
0
        /// <summary>
        /// Loads the inputs.
        /// </summary>
        /// <returns><c>true</c>, if inputs was loaded, <c>false</c> otherwise.</returns>
        public bool LoadInputs()
        {
            bool success = false;

            try
            {
                InputData = new Dictionary <string, IEnumerable <SensorReading> >();
                foreach (var input in Inputs)
                {
                    foreach (var inputFile in input.InputFiles)
                    {
                        // read in the sensor data
                        var sensorReadings = SensorReading.ReadSensorFile(inputFile.Value);

                        // verify input - only take records between the "start" and "quit" flags
                        var startRecord = sensorReadings.FirstOrDefault(x => x.Start);
                        if (startRecord != null)
                        {
                            sensorReadings = sensorReadings.Where(x => x.RecordNum >= startRecord.RecordNum).ToList();
                        }
                        var quitRecord = sensorReadings.FirstOrDefault(x => x.End);
                        if (quitRecord != null)
                        {
                            sensorReadings = sensorReadings.Where(x => x.RecordNum <= quitRecord.RecordNum).ToList();
                        }

                        // add data to collection using file name, not file path
                        InputData.Add(inputFile.Key, sensorReadings);
                    }
                }

                // run any initialization methods for selected modules
                foreach (var summarizer in Summarizers)
                {
                    SummarizerManager.GetSummarizer(summarizer).Initialize(InputData);
                }

                // confirm that everything worked
                LogManager.Debug($"{InputData.Keys.Count} files processed.");
                foreach (var key in InputData.Keys)
                {
                    LogManager.Debug($"Input file: {key} ... contains {InputData[key].Count()} records.");
                }

                success = InputData.Any();
            }
            catch (Exception e)
            {
                LogManager.Error("Fatal error encountered while loading input data.  Exiting program.", e, this);
            }

            return(success);
        }
예제 #2
0
        /// <summary>
        /// Runs the analyzers.
        /// </summary>
        /// <returns><c>true</c>, if analyzers was run, <c>false</c> otherwise.</returns>
        public bool RunAnalyzers()
        {
            if (!Analyzers.Any())
            {
                LogManager.Info("No analysis operations configured.", this);
                return(true);
            }

            bool success = false;

            if (Analyzers?.Count > 0 && InputData?.Keys?.Count >= 1)
            {
                var analyzedData = new Dictionary <string, IEnumerable <ICsvWritable> >();
                foreach (var analyzerCommand in Analyzers)
                {
                    // ---------------------------------------------------------
                    //          Retrieve analysis operation details
                    // ---------------------------------------------------------
                    var analyzerName = analyzerCommand.Name;
                    var analyzer     = AnalyzerManager.GetAnalyzer(analyzerName);
                    if (analyzer == null)
                    {
                        continue;
                    }

                    // Sanity checking ...
                    LogManager.Info($"Running analysis operation:\n\t{analyzer}", this);

                    // ---------------------------------------------------------
                    //      Check to see if user requested a summary
                    // ---------------------------------------------------------
                    bool summaryRequested = Summarizers.Contains(analyzerName);

                    // ---------------------------------------------------------
                    //          Group input data by original file name
                    // ---------------------------------------------------------
                    var consolidatedInputFiles =
                        Inputs.SelectMany(x => x.InputFiles.Select(y => y.Key)).ToList();
                    foreach (var origInputFile in consolidatedInputFiles)
                    {
                        var analysisDataByInputFile = new Dictionary <string, IEnumerable <ICsvWritable> >();
                        var analysisKeysByInputFile = InputData.Keys.Where(x => x.Contains(origInputFile));
                        foreach (var key in analysisKeysByInputFile)
                        {
                            // -------------------------------------------------
                            //      Perform the actual analysis operation
                            // -------------------------------------------------
                            if (analyzerCommand.HasParameters &&
                                FilterManager.ContainsFilter(Filters, typeof(ThresholdCalibrationFilter)))
                            {
                                var calibData = CalibrationData[origInputFile];
                                analyzerCommand.Parameters =
                                    ThresholdCalibrationFilter.CalibrateParameters
                                        (analyzerCommand.Parameters, calibData);
                            }

                            // -------------------------------------------------
                            //      Any required analyzer-specific prep
                            // -------------------------------------------------
                            if (analyzer is Analyzers.SciKitPrepAnalysis skpAnalyzer)
                            {
                                skpAnalyzer.CurrentInput = key;
                            }
                            else if (analyzer is Analyzers.SciKitEvalAnalysis skeAnalyzer)
                            {
                                skeAnalyzer.CurrentInput = key;
                            }
                            // -------------------------------------------------

                            var analysisResult =
                                analyzer.Analyze(InputData[key], analyzerCommand.Parameters);
                            analysisDataByInputFile[key] = analysisResult;

                            // -------------------------------------------------
                            //          Dump output to file if necessary
                            // -------------------------------------------------
                            if (WriteOutputFile)
                            {
                                CsvFileWriter.WriteResultsToFile
                                    (new string[] { OutputDirs.Analyzers, analyzerName },
                                    key, analyzer.HeaderCsv, analysisResult);
                            }

                            // -------------------------------------------------
                            //  If requested, summarize file-specific results
                            // -------------------------------------------------
                            if (summaryRequested)
                            {
                                ISummarizer summarizer =
                                    SummarizerManager.GetSummarizer(analyzerName);
                                var summarizedValues =
                                    summarizer.Summarize(analysisDataByInputFile);

                                if (WriteOutputFile)
                                {
                                    CsvFileWriter.WriteSummaryToFile
                                        (new string[] { OutputDirs.Summarizers, analyzerName },
                                        $"{origInputFile}{Constants.BAT.DEFAULT_INPUT_FILE_EXT}",
                                        summarizer.HeaderCsv, summarizedValues,
                                        summarizer.FooterCsv, summarizer.FooterValues);
                                }
                            }
                        }

                        // consolidate the results by input file
                        var inputFileResults = analyzer.ConsolidateData(analysisDataByInputFile);
                        if (analyzedData.ContainsKey(origInputFile))
                        {
                            analyzedData[origInputFile] = inputFileResults;
                        }
                        else
                        {
                            analyzedData.Add(origInputFile, inputFileResults);
                        }
                    }

                    // -----------------------------------------------------
                    // If requested, aggregate all file-specific summaries
                    // into one high level file in root summarizer directory
                    // -----------------------------------------------------
                    if (summaryRequested)
                    {
                        ISummarizer summarizer       = SummarizerManager.GetSummarizer(analyzerName);
                        var         summarizedValues = summarizer.Summarize(analyzedData);
                        if (WriteOutputFile)
                        {
                            CsvFileWriter.WriteSummaryToFile
                                (new string[] { OutputDirs.Summarizers },
                                $"{analyzerName}Aggregate{Constants.BAT.DEFAULT_INPUT_FILE_EXT}",
                                summarizer.HeaderCsv, summarizedValues,
                                summarizer.FooterCsv, summarizer.FooterValues);
                        }
                    }

                    success = true;
                }

                // use different collection to maintain integrity of original input data
                AnalysisData = analyzedData;
            }
            else
            {
                LogManager.Error("No input data to run analyzers on.", this);
            }

            return(success);
        }