-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyutils.py
More file actions
88 lines (68 loc) · 2.64 KB
/
Copy pathmyutils.py
File metadata and controls
88 lines (68 loc) · 2.64 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
import os
import numpy as np
import json
import csv
def clear_folder_except_files(folder_path, files_to_keep):
try:
if not os.path.exists(folder_path) or not os.path.isdir(folder_path):
raise ValueError(f"The folder '{folder_path}' does not exist.")
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path) and file_name not in files_to_keep:
os.remove(file_path)
except Exception as e:
print(f"Error: {e}")
def clear_folder(folder_path):
try:
if not os.path.exists(folder_path) or not os.path.isdir(folder_path):
raise ValueError(f"The folder '{folder_path}' does not exist.")
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
print(f"Error: {e}")
def writeStringToFile(file_path, content):
with open(file_path, 'w') as file:
file.write(content)
def saveGenotyp(genotyp, path):
genotyp_json = list(genotyp)
with open(path+".json", "w") as datei:
json.dump(genotyp_json, datei)
def loadGenotyp(path, generation = -1):
if generation > -1:
path = path + str(generation) + ".json"
with open(path, "r") as json_datei:
geladener_json = json.load(json_datei)
genotyp = np.array(geladener_json)
return genotyp
def getIntervalls(genotyp):
intervalls = np.zeros(1)
pitches = genotyp[0]
for i in range(len(pitches)-1):
#a
a = pitches[i]
if a > -1 :
b = pitches[i+1]
if b >-1:
diff = int(np.abs(a-b))
if diff>len(intervalls)-1:
intervalls = np.pad(intervalls,(0,diff-len(intervalls)+1),'constant',constant_values=0)
intervalls[diff] += 1
return intervalls
def writeIntervalls(individuum, path):
intervalls = getIntervalls(individuum.genotyp)
with open(path, 'a', newline='') as csv_datei:
wrt = csv.writer(csv_datei)
for i in range(len(intervalls)):
wrt.writerow((i,intervalls[i]))
def addToCSV(individuum, stayCounter, gen, path):
#filename
with open(path, 'a', newline='') as csv_datei:
wrt = csv.writer(csv_datei)
wrt.writerow((gen,individuum.fitness,stayCounter))
def to_tuple(array):
if isinstance(array, np.ndarray):
return tuple(map(to_tuple, array))
else:
return array