FL::ParseResult AB::analyze( const TimeSeries &ts, Forest &forest, Patterns::Matcher &matcher, PatternsSet &patterns, MetricsSet &metrics) { ParseResult result; try { if (ts.size() < 2) throw EAnalyze(E_INVALID_INPUT); Tree *tree = new Tree(ts); const int up = IDGenerator::idOf("a"); const int down = IDGenerator::idOf("b"); for (int i = 0; i < ts.size()-1; i += 1) { const int &id = (ts.value(i) <= ts.value(i+1)) ? up : down; tree->add(new Node(NULL, id, i, i+1, 0)); } forest.push_back(tree); result.treesAdded = 1; result.nodesAdded = (ts.size() + 1) / 2; } catch (const EAnalyze &e) { m_lastError = e; } return result; }
bool FileCSV::save(const std::string &fileName, TimeSeries &ts) { std::ofstream file(fileName.c_str()); if (!file.is_open()) return false; // Write header for (unsigned int i = 0; i < ts.header().size(); ++i) { file << ts.header()[i]; if (i < ts.header().size() - 1) file << m_separator; } // Write data for (int row = 0; row < ts.size(); ++row) { file << std::endl << ts.time(row) << m_separator << ts.value(row); } file << std::endl; file.close(); return true; }
TEST_F(DataFixture,TimeSeries_AddSubtractSameTimePeriod) { std::string units = "W"; Date startDate(Date(MonthOfYear(MonthOfYear::Feb),21)); DateTime startDateTime(startDate, Time(0,1,0,0)); // interval Time interval = Time(0,1,0,0); Vector intervalValues(3); intervalValues(0) = 0; intervalValues(1) = 1; intervalValues(2) = 2; TimeSeries intervalTimeSeries(startDateTime, interval, intervalValues, units); ASSERT_TRUE(!intervalTimeSeries.values().empty()); // detailed DateTimeVector dateTimes; dateTimes.push_back(startDateTime + Time(0,0,0,0)); dateTimes.push_back(startDateTime + Time(0,0,30,0)); dateTimes.push_back(startDateTime + Time(0,1,0,0)); dateTimes.push_back(startDateTime + Time(0,1,30,0)); dateTimes.push_back(startDateTime + Time(0,2,0,0)); Vector detailedValues(5); detailedValues(0) = 0.0; // 1:00 detailedValues(1) = 0.5; // 1:30 detailedValues(2) = 1.0; // 2:00 detailedValues(3) = 1.5; // 2:30 detailedValues(4) = 2.0; // 3:00 TimeSeries detailedTimeSeries(dateTimes, detailedValues, units); ASSERT_TRUE(!detailedTimeSeries.values().empty()); // sum and difference TimeSeries sum = intervalTimeSeries + detailedTimeSeries; TimeSeries diff1 = intervalTimeSeries - detailedTimeSeries; TimeSeries diff2 = detailedTimeSeries - intervalTimeSeries; ASSERT_TRUE(!sum.values().empty()); ASSERT_TRUE(!diff1.values().empty()); ASSERT_TRUE(!diff2.values().empty()); // EXPECT_EQ((unsigned)5, sum.dateTimes().size()); // EXPECT_EQ((unsigned)5, diff1.dateTimes().size()); // EXPECT_EQ((unsigned)5, diff2.dateTimes().size()); EXPECT_EQ((unsigned)5, sum.daysFromFirstReport().size()); EXPECT_EQ((unsigned)5, diff1.daysFromFirstReport().size()); EXPECT_EQ((unsigned)5, diff2.daysFromFirstReport().size()); // EXPECT_EQ(startDateTime, sum.dateTimes().front()); // EXPECT_EQ(startDateTime, diff1.dateTimes().front()); // EXPECT_EQ(startDateTime, diff2.dateTimes().front()); EXPECT_EQ(startDateTime, sum.firstReportDateTime()); EXPECT_EQ(startDateTime, diff1.firstReportDateTime()); EXPECT_EQ(startDateTime, diff2.firstReportDateTime()); DateTime endDateTime = startDateTime + Time(0,2,0,0); // EXPECT_EQ(endDateTime, sum.dateTimes().back()); // EXPECT_EQ(endDateTime, diff1.dateTimes().back()); // EXPECT_EQ(endDateTime, diff2.dateTimes().back()); EXPECT_EQ(endDateTime, sum.firstReportDateTime() + Time(sum.daysFromFirstReport(sum.daysFromFirstReport().size()-1))); EXPECT_EQ(endDateTime, diff1.firstReportDateTime() + Time(diff1.daysFromFirstReport(diff1.daysFromFirstReport().size()-1))); EXPECT_EQ(endDateTime, diff2.firstReportDateTime() + Time(diff2.daysFromFirstReport(diff2.daysFromFirstReport().size()-1))); // 1:00 EXPECT_EQ(0, sum.value(Time(0,0,0,0))); EXPECT_EQ(0, diff1.value(Time(0,0,0,0))); EXPECT_EQ(0, diff2.value(Time(0,0,0,0))); // 1:30 EXPECT_EQ(1.5, sum.value(Time(0,0,30,0))); EXPECT_EQ(0.5, diff1.value(Time(0,0,30,0))); EXPECT_EQ(-0.5, diff2.value(Time(0,0,30,0))); // 2:00 EXPECT_EQ(2, sum.value(Time(0,1,0,0))); EXPECT_EQ(0.0, diff1.value(Time(0,1,0,0))); EXPECT_EQ(0.0, diff2.value(Time(0,1,0,0))); // 2:30 EXPECT_EQ(3.5, sum.value(Time(0,1,30,0))); EXPECT_EQ(0.5, diff1.value(Time(0,1,30,0))); EXPECT_EQ(-0.5, diff2.value(Time(0,1,30,0))); // Test helper function for summing a vector. TimeSeriesVector sumAndDiffs; sumAndDiffs.push_back(sum); sumAndDiffs.push_back(diff1); sumAndDiffs.push_back(diff2); TimeSeries ans = openstudio::sum(sumAndDiffs); EXPECT_FALSE(ans.values().empty()); // 1:00 EXPECT_DOUBLE_EQ(0, ans.value(Time(0,0,0,0))); // 1:30 EXPECT_DOUBLE_EQ(1.5, ans.value(Time(0,0,30,0))); // 2:00 EXPECT_DOUBLE_EQ(2.0, ans.value(Time(0,1,0,0))); // 2:30 EXPECT_DOUBLE_EQ(3.5, ans.value(Time(0,1,30,0))); // Test multiplication and division with a scalar sumAndDiffs.push_back(sum/2.0); sumAndDiffs.push_back(3.0*diff1); ans = openstudio::sum(sumAndDiffs); EXPECT_FALSE(ans.values().empty()); // 1:00 EXPECT_DOUBLE_EQ(0, ans.value(Time(0,0,0,0))); // 1:30 EXPECT_DOUBLE_EQ(3.75, ans.value(Time(0,0,30,0))); // 2:00 EXPECT_DOUBLE_EQ(3.0, ans.value(Time(0,1,0,0))); // 2:30 EXPECT_DOUBLE_EQ(6.75, ans.value(Time(0,1,30,0))); }