-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_helper.py
43 lines (35 loc) · 1.34 KB
/
file_helper.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
import os
import ndjson
import pandas as pd
from global_static_vars import experiment_dir, category_imitation_last
### File helpers ###
def read_newest_results(category : str, trial : int):
if category == category_imitation_last and trial == 0:
#if category == category_imitation_last:
path_results = "./preset_flower.ndjson"
else:
path_results = experiment_dir + f"raw_{category}.ndjson"
file = pd.read_json(path_results, lines=True)
# This gets the detailed strokes for each iteration of category
strokes = file["strokes"]
data = strokes[len(strokes)-1]
return data
def read_results(category : str, iteration : int):
path_results = experiment_dir + f"raw_{category}.ndjson"
file = pd.read_json(path_results, lines=True)
# This gets the detailed strokes for each iteration of category
strokes = file["strokes"]
data = strokes[iteration]
return data
def append_to_ndjson(file_path, new_data):
# Read existing data if file exists, otherwise start with an empty list
data = []
if os.path.isfile(file_path):
with open(file_path, 'r') as file:
data = ndjson.load(file)
# Append the new data
data.append(new_data)
# Write all data back to the file
with open(file_path, 'w') as file:
ndjson.dump(data, file)
### End of file helpers ###