-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_usefull_functions.py
157 lines (121 loc) · 5.11 KB
/
f_usefull_functions.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
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
import os
import json
########################################################################################################################
# ============================================================================================================
# Usefull functions
# ============================================================================================================
########################################################################################################################
def split_remove_spaces(expr2split, splitCharacter):
""" Splits a given string according to a specified character e.g., ','
also removes the spaces ' '
"""
exprList = []
if not isinstance(expr2split, str) and not isinstance(expr2split, list): # so a float or int just one number
return [float(expr2split)] # if mulptiple prices for utilty you're gona have to change str to floats
elif isinstance(expr2split, list):
for exp in expr2split:
expresions = exp.split(splitCharacter)
for i in expresions:
i = i.replace(' ', '') # remove annoying spaces
exprList.append(i)
return exprList
elif isinstance(expr2split, str):
expresions = expr2split.split(splitCharacter)
for i in expresions:
i = i.replace(' ', '') # remove annoying spaces
exprList.append(i)
return exprList
def stringbounds_2_tuplebounds(stringBound):
""" transforms a bound that is writen as a string (e.g., '[20, 50]') to a tuple
"""
stringBound = stringBound.replace('[', '')
stringBound = stringBound.replace(']', '')
bounds = stringBound.split(',')
boundsList = []
for i in bounds:
boundsList.append(float(i))
return boundsList
def load_objectes_from_dictionary(dict):
""" deleet I think, not used"""
for i in dict:
locals()[i] = dict[i]
def remove_spaces(listOfInterest):
""" removes spaces """
exprList = []
for i in listOfInterest:
i = i.replace(' ', '')
exprList.append(i)
return exprList
def get_connected_intervals(intervalName, conectionMatrix):
"""From the conection matrix (see Excel file) the connected intervals are found which go into the specified
interval (intervalName) is found
"""
conectionCol = conectionMatrix[intervalName]
connectionInfo = conectionCol.where(conectionCol != 0).dropna()
# drop the bool variable (otherwise mixing gets confused)
# if there is a boolean varibale at least
try:
connectionInfo = connectionInfo.drop(intervalName)
except:
pass
nameConnectedIntervals = connectionInfo.index
connectionDict = {nameConnectedIntervals[i]: connectionInfo[i] for i in range(len(connectionInfo))}
# posConnect = conectionCol != 0
# nameConnectedIntervals = list(conectionMatrix['process_intervals'][posConnect])
return connectionDict
def str_2_dict(string, intervalname):
D = eval(string)
inputBoundsDict = {}
for i in D:
inputBoundsDict.update({i + '_' + intervalname: D[i]})
return inputBoundsDict
def get_location(file, case = ''):
""" gets the file location from the Directory 'Excel files'
"""
loc = os.getcwd()
posAlquimia = loc.find('Alquimia')
loc = loc[0:posAlquimia + 8]
if '/' in loc: # in the case of macOS
file = r"/{}".format(file)
if '.xlsx' in file:
if case == 'ML': # if you want to get Exel files for machinelearing go to other location
loc = loc + r'/machine learning models/excel_data' + file
else:
loc = loc + r'/excel files' + file
elif '.xml' in file:
loc = loc + r'/SBML models' + file
elif '.json' in file:
loc = loc + r'/json models' + file
elif "\\" in loc: # in the case of Windows OS
file = r"\{}".format(file)
if '.xlsx' in file:
if case == 'ML': # if you want to get Exel files for machinelearing go to other location
loc = loc + r'\machine learning models\excel_data' + file
else:
loc = loc + r'\excel files' + file
elif '.xml' in file:
loc = loc + r'\SBML models' + file
elif '.json' in file:
loc = loc + r'\json models' + file
return loc
def save_2_json(saveName, saveObject):
""" saves an object or dictionary to a json file """
if not isinstance(saveObject, dict):
saveObject = saveObject.__dict__
loc = os.getcwd()
posAlquimia = loc.find('Alquimia')
loc = loc[0:posAlquimia + 8]
loc = loc + r'\json models' + r'\{}'.format(saveName)
with open(loc, 'w+', encoding='utf-8') as f:
json.dump(saveObject, f, ensure_ascii=False, indent=4)
def linear_aprox(y1, y2, x1, x2, z1):
v = x1 - (x1-x2)*(y1-z1)/(y1-y2)
return v
def transform_dictionary(input_dict):
output_dict = {}
for key1, value1 in input_dict.items():
for key2, value2 in value1.items():
if key2 not in output_dict:
output_dict[key2] = {}
output_dict[key2][key1] = value2
return output_dict