forked from oist/optinist
-
Notifications
You must be signed in to change notification settings - Fork 3
/
outputs.py
153 lines (117 loc) · 4.68 KB
/
outputs.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
import os
from glob import glob
from typing import Optional
import pandas as pd
from fastapi import APIRouter
from studio.app.common.core.utils.file_reader import JsonReader, Reader
from studio.app.common.core.utils.filepath_creater import (
create_directory,
join_filepath,
)
from studio.app.common.core.utils.json_writer import JsonWriter, save_tiff2json
from studio.app.common.schemas.outputs import JsonTimeSeriesData, OutputData
from studio.app.const import ACCEPT_TIFF_EXT
from studio.app.dir_path import DIRPATH
router = APIRouter(prefix="/outputs", tags=["outputs"])
def get_initial_timeseries_data(dirpath) -> JsonTimeSeriesData:
plot_meta_path = f"{dirpath}.plot-meta.json"
plot_meta = JsonReader.read_as_plot_meta(plot_meta_path)
return JsonTimeSeriesData(
xrange=[],
data={},
std={},
meta=plot_meta,
)
@router.get("/inittimedata/{dirpath:path}", response_model=JsonTimeSeriesData)
async def get_inittimedata(dirpath: str):
file_numbers = sorted(
[
os.path.splitext(os.path.basename(x))[0]
for x in glob(join_filepath([dirpath, "*.json"]))
]
)
index = file_numbers[0]
str_index = str(index)
json_data = JsonReader.read_as_timeseries(
join_filepath([dirpath, f"{str(index)}.json"])
)
data = {
str(i): {json_data.xrange[0]: json_data.data[json_data.xrange[0]]}
for i in file_numbers
}
if json_data.std is not None:
std = {
str(i): {json_data.xrange[0]: json_data.data[json_data.xrange[0]]}
for i in file_numbers
}
return_data = get_initial_timeseries_data(dirpath)
return_data.xrange = json_data.xrange
if json_data.std is not None:
return_data.std = std
return_data.data = data
return_data.data[str_index] = json_data.data
if json_data.std is not None:
return_data.std[str_index] = json_data.std
return return_data
@router.get("/timedata/{dirpath:path}", response_model=JsonTimeSeriesData)
async def get_timedata(dirpath: str, index: int):
json_data = JsonReader.read_as_timeseries(
join_filepath([dirpath, f"{str(index)}.json"])
)
return_data = get_initial_timeseries_data(dirpath)
str_index = str(index)
return_data.data[str_index] = json_data.data
if json_data.std is not None:
return_data.std[str_index] = json_data.std
return return_data
@router.get("/alltimedata/{dirpath:path}", response_model=JsonTimeSeriesData)
async def get_alltimedata(dirpath: str):
return_data = get_initial_timeseries_data(dirpath)
for i, path in enumerate(glob(join_filepath([dirpath, "*.json"]))):
str_idx = str(os.path.splitext(os.path.basename(path))[0])
json_data = JsonReader.read_as_timeseries(path)
if i == 0:
return_data.xrange = json_data.xrange
return_data.data[str_idx] = json_data.data
if json_data.std is not None:
return_data.std[str_idx] = json_data.std
return return_data
@router.get("/data/{filepath:path}", response_model=OutputData)
async def get_file(filepath: str):
return JsonReader.read_as_output(filepath)
@router.get("/html/{filepath:path}", response_model=OutputData)
async def get_html(filepath: str):
return Reader.read_as_output(filepath)
@router.get("/image/{filepath:path}", response_model=OutputData)
async def get_image(
filepath: str,
workspace_id: str,
start_index: Optional[int] = 0,
end_index: Optional[int] = 1,
):
filename, ext = os.path.splitext(os.path.basename(filepath))
if ext in ACCEPT_TIFF_EXT:
filepath = join_filepath([DIRPATH.INPUT_DIR, workspace_id, filepath])
save_dirpath = join_filepath(
[
os.path.dirname(filepath),
filename,
]
)
json_filepath = join_filepath(
[save_dirpath, f"{filename}_{str(start_index)}_{str(end_index)}.json"]
)
if not os.path.exists(json_filepath):
save_tiff2json(filepath, save_dirpath, start_index, end_index)
else:
json_filepath = filepath
return JsonReader.read_as_output(json_filepath)
@router.get("/csv/{filepath:path}", response_model=OutputData)
async def get_csv(filepath: str, workspace_id: str):
filepath = join_filepath([DIRPATH.INPUT_DIR, workspace_id, filepath])
filename, _ = os.path.splitext(os.path.basename(filepath))
save_dirpath = join_filepath([os.path.dirname(filepath), filename])
create_directory(save_dirpath)
json_filepath = join_filepath([save_dirpath, f"{filename}.json"])
JsonWriter.write_as_split(json_filepath, pd.read_csv(filepath, header=None))
return JsonReader.read_as_output(json_filepath)