-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeseriesSimulator.py
More file actions
197 lines (153 loc) · 6.59 KB
/
Copy pathtimeseriesSimulator.py
File metadata and controls
197 lines (153 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#! /bin/env python
#Inputs
"""
From and To Timestamp
SendInterval in minutes
Mode:
square;amp:5;freq:20
pulse;amp:-2;freq:0.1
cos;amp:-2;freq:0.1
sin;amp-1;freq:1
randomInt:-25 to:80
randomDouble:0 to 1000
randomBool:80% (likelihood for true)
randomString: [list of strings to chose from]
A dictionary would some of those keys, not all are mandatory
{
from:
to:
sendInterval:
mode:
min:
max:
likelihoodForTrue:
listOfWords
}
"""
# Returns
"""
An Object with the simulated timeseries data, containing tuples with Timestamp, Value
"""
import pandas as pd
import math
import time
import json
import os
import random
import string
import itertools
import traceback
from datetime import datetime
from string import Template
inputDictionary = {
"startDate" : datetime(2021, 1, 15),
"endDate": datetime(2021, 1, 16),
"dataCreationInterval" : "1min",
"seriesDefinitions":
{
"variablename1":
{
"mode": "randomString",
"listOfStrings":["This","is","a","dummy","stringlist","lama"]
},
"variablename2":
{
"mode": "randomBool",
"likelihoodForTrueInPercent": 75,
},
"variablename3":
{
"mode":"randomInt",
"min":2,
"max":15,
},
"variablename4":
{
"mode":"sin",
"waveFormDescription": {"frequencyInHz":0.001, "amplitude":10}
}
}
}
def squareWave(value, mode ="square"): #supports two modes: "square" (dropping down to -1) and "pulse" (dropping down to 0)
if value >= 0:
return 1
elif mode == "pulse":
return -1
else:
return 0
def getSimulatedData(inputDictionary,timestampsAsString = False):
try:
timeTuples = []
startDate = inputDictionary["startDate"]
endDate = inputDictionary["endDate"]
dataCreationInterval = inputDictionary["dataCreationInterval"]
timeRange = pd.date_range(startDate, endDate, freq=dataCreationInterval).to_pydatetime().tolist()
seriesDefinitions = inputDictionary.get("seriesDefinitions")
simulatedData = {}
if not seriesDefinitions:
seriesDefinitions = {}
seriesDefinitions["dummyName"] = inputDictionary
for seriesName in seriesDefinitions:
inputDictionary = seriesDefinitions[seriesName]
timeStampValuePairs = {}
mode = inputDictionary["mode"]
if mode in ("square","pulse","sin","cos"):
waveFormDescription = inputDictionary.get("waveFormDescription")
if waveFormDescription:
frequency = waveFormDescription["frequencyInHz"]
amplitude = waveFormDescription["amplitude"]
firstValue = timeRange[0]
timeStampValuePairs[firstValue] = 1 if "mode" =="cos" else 0
for timestamp in timeRange[1:]:
timePassedInSeconds = (timestamp - firstValue).total_seconds()
if mode == "sin":
simulatedValue = amplitude * math.sin(2 * math.pi * frequency * timePassedInSeconds)
if mode == "cos":
simulatedValue = amplitude * math.sin(2* math.pi * frequency * timePassedInSeconds)
if mode in ("square","pulse"):
simulatedValue = amplitude * squareWave(math.sin(2* math.pi * frequency * timePassedInSeconds), mode = "square")
timeStampValuePairs[timestamp] = simulatedValue
else:
print('No Wave Form description has been found in the input dictionary, though a periodical simulation has been chosen (here: %s).\nPlease add such a definition using this syntax\n"waveFormDescription": {"frequencyInHz":0.001, "amplitude":10}' %mode)
return []
else:
for timestamp in timeRange:
if mode == "randomInt":
min = inputDictionary["min"]
max = inputDictionary["max"]
simulatedValue = random.randint(min, max)
if mode == "randomDouble":
min = inputDictionary["min"]
max = inputDictionary["max"]
simulatedValue = random.uniform(min, max)
if mode == "randomBool":
likelihoodForTrueInPercent = inputDictionary["likelihoodForTrueInPercent"]
simulatedValue = random.random() * 100 <= likelihoodForTrueInPercent
if mode =="randomString":
listOfStrings = inputDictionary["listOfStrings"]
simulatedValue = random.choice(listOfStrings)
timeStampValuePairs[timestamp] = simulatedValue
simulatedData[seriesName] = timeStampValuePairs
#join all series in one dict
resultDictionary = {}
for timestamp in timeRange:
if timestampsAsString:
timestampInTargetFormat = timestamp.isoformat()+"Z"
else:
timestampInTargetFormat = timestamp
currentDict = {}
for seriesName in simulatedData:
currentDict[seriesName] = simulatedData[seriesName][timestamp]
resultDictionary[timestampInTargetFormat] = currentDict
return(timeTuples)
except:
print(f"Something went wrong when trying to simulate data. Input dictionary has been {inputDictionary}:")
traceback.print_exc()
print("Return an empty list now")
return []
#getSimulatedTuples(inputDictionary)
def getXValuesForWaveform(frequency):
period = 1/frequency
stepSize = math.ceil(period*360)
currentWaveGenerationRange = list(range(0,361,stepSize))
return currentWaveGenerationRange