Skip to content
Snippets Groups Projects
Commit 71f80d27 authored by dlandre2's avatar dlandre2
Browse files

title + xlabel + print in perdiodograms.py

parent 6f6039fd
Branches
No related tags found
No related merge requests found
characterizationUseCase.py periodograms.py
\ No newline at end of file \ No newline at end of file
...@@ -20,7 +20,7 @@ def filterTime(series, filterTimeInSec): ...@@ -20,7 +20,7 @@ def filterTime(series, filterTimeInSec):
return seriesFiltered return seriesFiltered
def differentiateSeries(series): # Differentiating a non-stationary time series with no trend def differentiateSeries(series): # Differentiating a non-stationary time series with no trend
print("Differentiation") print("A differentiation of the time series is necessary. There is no trend\n")
seriesDiff = [] seriesDiff = []
for i in range(len(series) - 1): for i in range(len(series) - 1):
seriesDiff.append(series[i] - series[i + 1]) seriesDiff.append(series[i] - series[i + 1])
...@@ -28,10 +28,13 @@ def differentiateSeries(series): # Differentiating a non-stationary time series ...@@ -28,10 +28,13 @@ def differentiateSeries(series): # Differentiating a non-stationary time series
def printPeriodogram(series): # Show periodogram and top 8 frequencies in time series. def printPeriodogram(series): # Show periodogram and top 8 frequencies in time series.
print("Displaying the periodogram of the time series\n")
# Periodogram # Periodogram
freqencies, spectrum = periodogram(series) freqencies, spectrum = periodogram(series)
plt.plot(freqencies, spectrum, color='blue') plt.plot(freqencies, spectrum, color='blue')
plt.grid(True, linestyle='-', which='major', alpha=0.5, axis='both') plt.grid(True, linestyle='-', which='major', alpha=0.5, axis='both')
plt.title("Periodogram of the time series")
plt.xlabel('Frequency (Hz)', fontsize=45) plt.xlabel('Frequency (Hz)', fontsize=45)
plt.ylabel('Power spectral density', fontsize=45) plt.ylabel('Power spectral density', fontsize=45)
plt.xticks(fontsize=40) plt.xticks(fontsize=40)
...@@ -50,6 +53,8 @@ def printPeriodogram(series): # Show periodogram and top 8 frequencies in time s ...@@ -50,6 +53,8 @@ def printPeriodogram(series): # Show periodogram and top 8 frequencies in time s
print(val) print(val)
def stationaryTestsResults(series, alpha): # Assessing the stationarity of a time series using ADF and KPSS tests def stationaryTestsResults(series, alpha): # Assessing the stationarity of a time series using ADF and KPSS tests
print("Testing the stationarity of the time series")
# ADF test # ADF test
testAdf = adfuller(series) testAdf = adfuller(series)
# p-value # p-value
...@@ -61,35 +66,47 @@ def stationaryTestsResults(series, alpha): # Assessing the stationarity of a tim ...@@ -61,35 +66,47 @@ def stationaryTestsResults(series, alpha): # Assessing the stationarity of a tim
print("The p-value KPSS:", testKpss[1]) print("The p-value KPSS:", testKpss[1])
if testAdf[1] <= alpha <= testKpss[1]: if testAdf[1] <= alpha <= testKpss[1]:
print("The series is stationary") print("The time series is stationary\n")
else: else:
print("The series is not stationary") print("The time series is not stationary\n")
def removeTrend(series, granularity): # Removing the trend component from the time series if it is present def removeTrend(series, granularity): # Removing the trend component from the time series if it is present
print("A trend component is present")
print("Displaying the time series with trend\n")
x_abs = [i for i in range(len(series))] x_abs = [i for i in range(len(series))]
low = lowess(series, x_abs, frac=granularity) low = lowess(series, x_abs, frac=granularity)
low = [v[1] for v in low] low = [v[1] for v in low]
plt.plot(series) plt.plot(series)
plt.title("Time series with trend")
plt.plot(low) plt.plot(low)
plt.xlabel("time (hour)")
plt.show() plt.show()
series_without_trend = [val - tr for val, tr in zip(series, low)] series_without_trend = [val - tr for val, tr in zip(series, low)]
print("Displaying the time series without its trend\n")
plt.plot(series_without_trend) plt.plot(series_without_trend)
plt.title("Time series without trend")
plt.xlabel("time (hour)")
plt.show() plt.show()
return series_without_trend return series_without_trend
def plotSeries(series): def plotSeries(series):
plt.plot(series) plt.plot(series)
plt.title("Time series")
plt.xlabel("time (hour)")
plt.show() plt.show()
def execution(List): def execution(List):
for l in List: for l in List:
print("######################################")
print(l[2]) print(l[2])
X = filterTime(l[0], l[1]) X = filterTime(l[0], l[1])
print("Displaying the time series\n")
plotSeries(X) plotSeries(X)
stationaryTestsResults(X, alphaUsed) stationaryTestsResults(X, alphaUsed)
if l[3] == "T": if l[3] == "T":
...@@ -97,6 +114,7 @@ def execution(List): ...@@ -97,6 +114,7 @@ def execution(List):
stationaryTestsResults(X, alphaUsed) stationaryTestsResults(X, alphaUsed)
elif l[3] == "D": elif l[3] == "D":
X = differentiateSeries(X) X = differentiateSeries(X)
print("Displaying the differentiate time series\n")
plotSeries(X) plotSeries(X)
stationaryTestsResults(X, alphaUsed) stationaryTestsResults(X, alphaUsed)
printPeriodogram(X) printPeriodogram(X)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment