forked from ArnoKasper/ProcessSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_manager.py
127 lines (104 loc) · 4.33 KB
/
exp_manager.py
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
"""
Project: ProcessSim
Made By: Arno Kasper
Version: 1.0.0
"""
import pandas as pd
import socket
import random
import warnings
import os
import simulationmodel as sim
class Experiment_Manager(object):
# Creat a batch of experiments with a upper an lower limit
def __init__(self, lower, upper):
"""
initialize experiments integers
:param lower: lower boundary of the exp number
:param upper: upper boundary of the exp number
"""
self.lower = lower
self.upper = upper
self.count_experiment = 0
self.exp_manager()
def exp_manager(self):
"""
define the experiment manager who controls the simulation model
:return: void
"""
# use a loop to illiterate multiple experiments from the exp_dat list
for i in range(self.lower, (self.upper + 1)):
# activate Simulation experiment method
self.sim = sim.SimulationModel(i)
self.sim.sim_function()
# finish the experiment by saving the data and move on to the saving function
exp_variable_list = self.sim.model_panel.project_name
# save the experiment
self.saving_exp(exp_variable_list)
def saving_exp(self, exp_variable_list):
"""
save all the experiment data versions
:param exp_variable_list:
:return:
"""
# initialize params
df = self.sim.data_exp.database
file_version = ".csv" # ".xlsx"#".csv"#
# get file directory
path = self.get_directory()
# create the experimental name
exp_name = self.sim.model_panel.experiment_name
# save file
file = path + exp_name + file_version
try:
# save as csv file
if file_version == ".csv":
self.save_database_csv(file=file, database=df)
# save as excel file
elif file_version == ".xlsx":
self.save_database_xlsx(file=file, database=df)
except PermissionError:
# failed to save, make a random addition to the name to save anyway
random_genetator = random.Random()
random_name = "random_"
strings = ['a', "tgadg", "daf", "da", "gt", "ada", "fs", "dt", "d", "as"]
name_lenght = random_genetator.randint(1, 14)
# build the name
for j in range(0, name_lenght):
random_genetator.shuffle(strings)
value = random_genetator.randint(0, 60000)
random_name += strings[j] + "_" + str(value) + "_"
# save as csv file
if file_version == ".csv":
self.save_database_csv(file=file, database=df)
# save as excel file
elif file_version == ".xlsx":
self.save_database_xlsx(file=file, database=df)
# notify the user
warnings.warn(f"Permission Error, saved with name {exp_name + random_name}", Warning)
# add the experiment number for the next experiment
self.count_experiment += 1
print(f"Simulation data saved with name: {exp_name}")
if self.sim.print_info:
print(f"\tINPUT THIS EXPERIMENT: {self.sim.data_exp.order_input_counter}")
print(f"\tOUTPUT THIS EXPERIMENT: {self.sim.data_exp.order_output_counter}")
def save_database_csv(self, file, database):
database.to_csv(file, index=False)
def save_database_xlsx(self, file, database):
writer = pd.ExcelWriter(file, engine='xlsxwriter')
database.to_excel(writer, sheet_name='name', index=False)
writer.save()
def get_directory(self):
# define different path options
machine_name = socket.gethostname()
path = ""
# find path for specific machine
if machine_name == "LAPTOP-HN4N26LU":
path = "C:/Users/Arno_ZenBook/Dropbox/Professioneel/Research/Results/test/"
elif machine_name[0:7] == "pg-node":
path = "/data/s3178471/"
else:
warnings.warn(f"{machine_name} is an unknown machine name ", Warning)
path = os.path.abspath(os.getcwd())
print(f"files are saved in {path}")
return path