Skip to content

Commit

Permalink
Merge pull request TreB1eN#8 from tomasic/feat/cps/log-generator
Browse files Browse the repository at this point in the history
feat/cps/log generator
  • Loading branch information
bogdan-veliscu authored Aug 21, 2020
2 parents 1d33188 + 9a548f7 commit e0437ad
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ dmypy.json

# npm stuff
node_modules/

# ingnore channel log files

autocoder/cps/logs/*.csv
4 changes: 2 additions & 2 deletions autocoder/cps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ pip install -r requirements.txt
Generate Interface

```
usage: generate.py [-h] [-c CONFIG_FILE] [-o OUTPUT_FILE] [-r SEED] [-s SIZE] [-p SHOW_PLOT]
usage: generate.py [-h] [-c CONFIG_FILE] [-o OUTPUT_PATH] [-p SHOW_PLOT]
optional arguments:
-h, --help show this help message and exit
-c CONFIG_FILE, --config CONFIG_FILE set configuration file yaml
-o OUTPUT_FILE, --output OUTPUT_FILE set output channel log file
-o OUTPUT_PATH, --output OUTPUT_PATH set output channel log path
-p SHOW_PLOT, --plot SHOW_PLOT show the distribution histogram plot
```

Expand Down
47 changes: 36 additions & 11 deletions autocoder/cps/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
import time
import random
import argparse
import os


def generate(output_file, show_plot):
loc, scale, size = 20, 5, 1000 # mean , standard deviation and size
measurements = np.random.normal(loc, scale, size)
def generate(output_path, show_plot, name, distribution, interval):
measurements = handle_distribution(**distribution)
intervals = handle_distribution(**interval)

save_to_csv(measurements, output_file)
log_array = np.hstack((intervals, measurements))

output_file = os.path.join(output_path, name + "_log.csv")
save_to_csv(log_array, output_file)
if (show_plot):
scale = distribution["scale"]
loc = distribution["loc"]
import matplotlib.pyplot as plt

count, bins, ignored = plt.hist(measurements, 30, density=True)
Expand All @@ -22,16 +28,33 @@ def generate(output_file, show_plot):
plt.show()


def handle_distribution(distribution_type, arguments):
if hasattr(np.random, distribution_type):
res = (getattr(np.random, distribution_type)(**arguments))

return res.reshape(arguments['size'], 1)


def create_dirs(filepath):
if not os.path.exists(filepath):
try:
os.makedirs(filepath)
except OSError as exc: # Guard against race condition
raise


def save_to_csv(measurements, output_file):
np.savetxt(output_file, measurements, delimiter=",")
fmt = ",".join(["%d"] + ["%10.6e"] * (measurements.shape[1]-1))
np.savetxt(output_file, measurements, delimiter=",",
fmt=fmt, header='Interval, Measurement')


def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", type=str, dest='config_file', help="set configuration file yaml",
default="autocoder/cps/generator.yaml")
parser.add_argument("-o", "--output", type=str, dest='output_file',
help="set output channel log file", default="channel_log.csv")
parser.add_argument("-o", "--output", type=str, dest='output_path',
help="set output channel log path", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs"))
parser.add_argument("-p", "--plot", type=bool, dest='show_plot',
help="show the distribution histogram plot", default=False)

Expand All @@ -40,15 +63,17 @@ def parse_arguments():

def main():
args = parse_arguments()
seed = 0
create_dirs(args.output_path)
with open(args.config_file, 'r') as stream:
config_info = yaml.load_all(stream, Loader=yaml.Loader)
for config in config_info:
print(config["run"]["generator"]["random_seed"])
seed = config["run"]["generator"]["random_seed"]
np.random.seed(seed)
channels = config["run"]["generator"]["facility"]["channels"]

np.random.seed(seed)
generate(args.output_file, args.show_plot)
for channel in channels:
generate(args.output_path, args.show_plot,
**channel["channel"])


if __name__ == "__main__":
Expand Down
40 changes: 20 additions & 20 deletions autocoder/cps/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ run: # the name of the scenario
- channel: # each channel generates measurements with a different distribution
name: facility_temperature
distribution:
-distribution:
type: normal # using the same name for distribution and params as in numpy
arguments: # normal(loc=0.0, scale=1.0, size=None)
loc: 20.0
scale: 5.0
size: 1000
distribution_type: normal # using the same name for distribution and params as in numpy
arguments: # normal(loc=0.0, scale=1.0, size=None)
loc: 20.0
scale: 5.0
size: 1000
interval:
-distribution:
type: choice
options: [1, 2, 3, 4, 5, 6, 7, 8]
probabilities: [0.1, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1]
distribution_type: choice
arguments:
a: [1, 2, 3, 4, 5, 6, 7, 8]
p: [0.1, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1]
size: 1000
- channel: # read data is type converted (Date objects)
name: facility_humidity
distribution:
-distribution:
type: normal
arguments: #normal(loc=0.0, scale=1.0, size=None)
loc: 45.0
scale: 2.0
size: 1000
distribution_type: normal
arguments: #normal(loc=0.0, scale=1.0, size=None)
loc: 45.0
scale: 2.0
size: 1000
interval:
-distribution:
type: choice
options: [1, 2, 3, 4, 5, 6, 7, 8]
probabilities: [0.1, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1]
distribution_type: choice
arguments:
a: [1, 2, 3, 4, 5, 6, 7, 8]
p: [0.1, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1]
size: 1000

test: # the test scenario is the same as the run scenario
generator:
Expand Down

0 comments on commit e0437ad

Please sign in to comment.