def plot_historical_relevance(self, word, period, method, smooth=None): time_series = TimeSeries(word) series = time_series.get_series() original_series = time_series.get_modified_series(series) if smooth != None: series = time_series.smoothify_series(original_series, smooth) else: series = original_series x0 = [i + self.year_range[0] for i in range(self.max_range)] y0 = self.compute_relevance(original_series) if 'level' in method: level_peak = LevelPeakDetector(series) if method == 'level 1': y = self.compute_relevance(level_peak.get_levels(1)) else: y = self.compute_relevance(level_peak.get_levels(2)) elif 'window' in method: window_peak = WindowPeakDetector(series) if method == 'window 1': y = self.compute_relevance(window_peak.compute_peaks(1)) elif method == 'window 2': y = self.compute_relevance(window_peak.compute_peaks(2)) else: y = self.compute_relevance(window_peak.compute_peaks(3)) elif 'double' in method: double_peak = DoubleChangePeakDetector(series) y = double_peak.compute_relevance(0.1) plotter = Plotter(x0, series, period) plotter.plot_peaks(word, y, method + ' function')
def get_divergence(self, word, series): divergence = [] time_series = TimeSeries(word) series = time_series.get_modified_series(series) y = self.compute_relevance(series) smoothing_series = time_series.smoothify_series(series, 1) ys = self.compute_relevance(smoothing_series) level_peak = LevelPeakDetector(series) y_level_1 = self.compute_relevance(level_peak.get_levels(1)) divergence.append(KullbackLeibler(y, y_level_1).compute_divergence()) y_level_2 = self.compute_relevance(level_peak.get_levels(2)) divergence.append(KullbackLeibler(y, y_level_2).compute_divergence()) window_peak = WindowPeakDetector(series) y_window_1 = self.compute_relevance(window_peak.compute_peaks(1)) divergence.append(KullbackLeibler(y, y_window_1).compute_divergence()) y_window_2 = self.compute_relevance(window_peak.compute_peaks(2)) divergence.append(KullbackLeibler(y, y_window_2).compute_divergence()) y_window_3 = self.compute_relevance(window_peak.compute_peaks(3)) divergence.append(KullbackLeibler(y, y_window_3).compute_divergence()) double_peak = DoubleChangePeakDetector(smoothing_series) y_double = double_peak.compute_relevance(0.1) divergence.append(KullbackLeibler(ys, y_double).compute_divergence()) return divergence
def get_historical_relevance(self, word, word_info): initial_period = word_info[0].split('-') period = [] for p in initial_period: if 'Present' in initial_period: period.append(2014) else: period.append(int(p)) series = word_info[1] time_series = TimeSeries(word) series = time_series.get_modified_series(series) smoothing_series = time_series.smoothify_series(series, 2) relevance = [] level_peak = LevelPeakDetector(smoothing_series) relevance.append(self.is_relevant_in_period(self.compute_relevance(level_peak.get_levels(1)), period)) relevance.append(self.is_relevant_in_period(self.compute_relevance(level_peak.get_levels(2)), period)) window_peak = WindowPeakDetector(series) relevance.append(self.is_relevant_in_period(self.compute_relevance(window_peak.compute_peaks(1)), period)) relevance.append(self.is_relevant_in_period(self.compute_relevance(window_peak.compute_peaks(2)), period)) relevance.append(self.is_relevant_in_period(self.compute_relevance(window_peak.compute_peaks(3)), period)) double_peak = DoubleChangePeakDetector(smoothing_series) relevance.append(self.is_relevant_in_period(double_peak.compute_relevance(0.1), period)) return relevance
def plot_all_methods(self, word, period, smooth=None): time_series = TimeSeries(word) series = time_series.get_series() original_series = time_series.get_modified_series(series) if smooth != None: series = time_series.smoothify_series(original_series, smooth) else: series = original_series x0 = [i + self.year_range[0] for i in range(self.max_range)] y0 = self.compute_relevance(original_series) y = [] level_peak = LevelPeakDetector(series) y.append(self.compute_relevance(level_peak.get_levels(1))) y.append(self.compute_relevance(level_peak.get_levels(2))) window_peak = WindowPeakDetector(series) y.append(self.compute_relevance(window_peak.compute_peaks(1))) y.append(self.compute_relevance(window_peak.compute_peaks(2))) y.append(self.compute_relevance(window_peak.compute_peaks(3))) double_peak = DoubleChangePeakDetector(series) y.append(double_peak.compute_relevance(0.1)) plotter = Plotter(x0, series, period) plotter.plot_multiple_peaks(word, y, period)
def test_bool(self): ''' Verify if bool of 2-norm ''' test_values = [1, 2, 3, 4, 5] test_values1 = [0, 0, 0, 0, 0] ts = TimeSeries(test_values) ts1 = TimeSeries(test_values1) self.assertEqual(bool(ts), True) self.assertEqual(bool(ts1), False)
def plot_keywords(self, event, words, period): time_series = [] for i in range(len(words)): original_series = TimeSeries(words[i]).get_series() original_series = original_series.get_modified_series(original_series) series = original_series.smoothify_series(original_series, 2) time_series.append(series) x0 = [i + self.year_range[0] for i in range(self.max_range)] plotter = Plotter(x0, [], period) plotter.plot_keywords(event, words, time_series)
def test_addition(self): ''' Verify adding two timeseries ''' test_times = [1, 2, 3, 4, 5] test_values = [1, 2, 3, 4, 5] test_times1 = [1, 2, 3, 4, 6] test_values1 = [5, 6, 7, 8, 9] ts = TimeSeries(test_values, test_times) ts0 = TimeSeries(test_values1, test_times) ts1 = TimeSeries(test_values1, test_times1) self.assertEqual((ts + ts0), TimeSeries([6, 8, 10, 12, 14], [1, 2, 3, 4, 5])) with self.assertRaises(ValueError): ts + ts1
def test_equal(self): ''' Verify equating two timeseries ''' test_times = [2.3, 3.4, 4.1, 9, 10] test_values = [5, 8.9, 10.1, 19.8, 2] test_times1 = [2, 3.4, 4.1, 9, 10] test_values1 = [5, 6, 7, 8, 9] ts = TimeSeries(test_values, test_times) ts0 = TimeSeries(test_values1, test_times) ts1 = TimeSeries(test_values1, test_times1) self.assertEqual((ts == ts), True) self.assertEqual((ts == ts0), False) with self.assertRaises(ValueError): ts == ts1
def test_subtraction(self): ''' Verify subtracting two timeseries ''' test_times = [1, 2, 3, 4, 5] test_values = [1, 2, 3, 4, 5] test_times1 = [1, 2, 3, 4, 6] test_values1 = [5, 6, 7, 8, 9] ts = TimeSeries(test_values, test_times) ts0 = TimeSeries(test_values1, test_times) ts1 = TimeSeries(test_values1, test_times1) self.assertEqual((ts0 - ts), TimeSeries([4, 4, 4, 4, 4], [1, 2, 3, 4, 5])) with self.assertRaises(ValueError): ts - ts1
def test_multiplication(self): ''' Verify multiplying two timeseries ''' test_times = [1, 2, 3, 4, 5] test_values = [1, 2, 3, 4, 5] test_times1 = [1, 2, 3, 4, 6] test_values1 = [5, 6, 7, 8, 9] ts = TimeSeries(test_values, test_times) ts0 = TimeSeries(test_values1, test_times) ts1 = TimeSeries(test_values1, test_times1) self.assertEqual((ts * ts0), TimeSeries([5, 12, 21, 32, 45], [1, 2, 3, 4, 5])) with self.assertRaises(ValueError): ts * ts1
def test_range(self): ''' Verify that "unrealized" sequences can be parsed as argument ''' ts = TimeSeries(range(1, 10, 2)) self.assertEqual(ts.timeseries, [(0, 1), (1, 3), (2, 5), (3, 7), (4, 9)])
def test_setItemNonPresent(self): ''' Verify that an IndexError is raised when trying to change the value of an element that is not present in the TimeSeries object ''' ts = TimeSeries([]) with self.assertRaises(IndexError): ts[0] = 10
def test_getItemNotPresent(self): ''' Verify that an IndexError is raised when trying to access an element not contained in the TimeSeries ''' ts = TimeSeries([1, 2, 3, 4, 5]) with self.assertRaises(IndexError): ts[10]
def test_setItemPresent(self): ''' Verify we can change an element in the list ''' ts = TimeSeries([0, 1, 2, 3, 4]) ts[4] = 10 self.assertEqual(ts[4], 10)
def test_neg(self): ''' Verify if negation ''' test_values = [1, 2, 3, 4, 5] ts = TimeSeries(test_values) self.assertEqual(-ts, [-1, -2, -3, -4, -5])
def test_abs(self): ''' Verify 2-norm of time series ''' test_times = [1, 2, 3, 4, 5] test_values = [1, 2, 3, 4, 5] ts = TimeSeries(test_values, test_times) self.assertEqual(abs(ts), math.sqrt(55))
def main(): print('creating checkpoint directory') os.makedirs(args.checkpoint_dir, exist_ok=True) batches_per_epoch = 100 ts_train = TimeSeries('Training', batches_per_epoch) ts_eval = TimeSeries('Evaluation', args.epochs) for epoch in range(args.epochs): print('starting epoch {}'.format(epoch)) metrics = evaluate(epoch) for key, value in metrics.items(): ts_eval.collect(key, value) print(ts_eval) make_video('epoch_{:03d}'.format(epoch)) train(epoch, ts_train, batches_per_epoch) print(ts_train) torch.save(discriminator.state_dict(), os.path.join(args.checkpoint_dir, 'disc_{}'.format(epoch))) torch.save(generator.state_dict(), os.path.join(args.checkpoint_dir, 'gen_{}'.format(epoch))) torch.save(encoder.state_dict(), os.path.join(args.checkpoint_dir, 'enc_{}'.format(epoch)))
def test_contains(self): ''' Verify __contains__ ''' test_times = [2.3, 3.4, 4.1, 9, 10] test_values = [5, 8.9, 10.1, 19.8, 2] ts = TimeSeries(test_values, test_times) self.assertEqual((5 in ts), True) self.assertEqual((5.1 in ts), False)
def test_getItemPresent(self): ''' Test the getItem with an index smaller than the length ''' ts = TimeSeries([1, 2, 3, 4, 5]) self.assertEqual(ts[2], 3)
def test_nonZeroLen(self): ''' Checks we correctly render lengths ''' self.assertEqual(len(TimeSeries(range(10000))), 10000)
def test_zeroLen(self): ''' Checks we correctly render 0 length ''' self.assertEqual(len(TimeSeries([])), 0)
def test_valTimeEqLen(self): ''' Checks that the value sequence and time sequence have the same lengths ''' with self.assertRaises(AssertionError): TimeSeries([1, 2, 3], [1])
def test_emptyList(self): ''' Verify that empty timeseries is constructed when empty sequence is given ''' ts = TimeSeries([]) self.assertEqual(ts.timeseries, [])
from series import TimeSeries # projecteuler.net/problem=1 # Note: this is decidely *not* the intended purpose of this class. threes = TimeSeries(range(0, 1000, 3)) fives = TimeSeries(range(0, 1000, 5)) s = 0 for i in range(0, 1000): if i in threes or i in fives: s += i print("sum", s)
def test_nonNumeric(self): ''' Checks that an Error is raised if the sequence contains non numeric elements ''' with self.assertRaises(AssertionError): TimeSeries(list('abcd'))
def test_noArgument(self): ''' Verify that an error is raised when no values argument is passed ''' with self.assertRaises(TypeError): ts = TimeSeries()