8
8
9
9
from flask import Flask , request , Response , send_file , jsonify
10
10
import numpy as np
11
- # from intern.remote.boss import BossRemote
12
- # from intern.resource.boss.resource import ChannelResource
13
- # from intern.utils.parallel import block_compute
14
- # from requests import codes, post
15
11
16
12
APP = Flask (__name__ )
17
13
18
14
UPLOADS_PATH = "./uploads"
19
15
BLOCK_SIZE = (256 , 256 , 256 )
20
16
21
17
22
- """
23
- https://api.theboss.io/v1/cutout/:collection/:experiment/:channel/:resolution/:x_range/:y_range/:z_range/:time_range/?iso=:iso
24
- """
25
-
26
-
27
18
def file_compute (x_start , x_stop ,
28
19
y_start , y_stop ,
29
20
z_start , z_stop ,
@@ -59,6 +50,20 @@ def file_compute(x_start, x_stop,
59
50
60
51
61
52
def blockfile_indices (xs , ys , zs , origin = (0 , 0 , 0 ), block_size = (256 , 256 , 256 )):
53
+ """
54
+ Returns the indices PER BLOCK for each file required.
55
+
56
+ Returns the indices of the offset PER BLOCK of each file that is
57
+ required in order to construct the final volume.
58
+
59
+ For instance, in 1D:
60
+
61
+ Blocksize is 100, origin is 0.
62
+
63
+ Request of (10, 50) returns (10, 50)
64
+ Request of (10, 150) returns (10, 100), (0, 50)
65
+ Request of (10, 250) returns (10, 100), (0, 100), (100, 50)
66
+ """
62
67
blocks = block_compute (
63
68
xs [0 ], xs [1 ], ys [0 ], ys [1 ], zs [0 ], zs [1 ],
64
69
origin , block_size
@@ -163,7 +168,7 @@ def getdata(self, col, exp, chan, res, xs, ys, zs):
163
168
f [2 ] + i [2 ][0 ]: f [2 ] + i [2 ][1 ],
164
169
] = data_partial
165
170
166
- except :
171
+ except ValueError :
167
172
payload [
168
173
f [0 ] + i [0 ][0 ]: f [0 ] + i [0 ][1 ],
169
174
f [1 ] + i [1 ][0 ]: f [1 ] + i [1 ][1 ],
@@ -189,6 +194,17 @@ def store(self, data, col, exp, chan, res, b):
189
194
return np .save (fname , data )
190
195
191
196
def retrieve (self , col , exp , chan , res , b ):
197
+ if not (
198
+ os .path .isdir ("{}/{}" .format (UPLOADS_PATH , col )) and
199
+ os .path .isdir ("{}/{}/{}" .format (UPLOADS_PATH , col , exp )) and
200
+ os .path .isdir ("{}/{}/{}/{}" .format (
201
+ UPLOADS_PATH , col , exp , chan
202
+ ))
203
+ ):
204
+ raise IOError ("{}/{}/{} not found." .format (
205
+ col , exp , chan
206
+ ))
207
+ # return np.zeros(self.block_size, dtype="uint8")
192
208
fname = "{}/{}/{}/{}/{}-{}-{}-{}.npy" .format (
193
209
UPLOADS_PATH ,
194
210
col , exp , chan ,
@@ -206,8 +222,9 @@ def retrieve(self, col, exp, chan, res, b):
206
222
@APP .route ("/v1/cutout/<collection>/<experiment>/<channel>/<resolution>/<x_range>/<y_range>/<z_range>/" , methods = ["POST" ])
207
223
def upload_cutout_xyz (collection , experiment , channel , resolution , x_range , y_range , z_range ):
208
224
"""
209
- Upload a volume
225
+ Upload a volume.
210
226
227
+ Uses bossURI format.
211
228
"""
212
229
for _ , f in request .files .items ():
213
230
memfile = io .BytesIO ()
@@ -224,25 +241,23 @@ def upload_cutout_xyz(collection, experiment, channel, resolution, x_range, y_ra
224
241
@APP .route ("/v1/cutout/<collection>/<experiment>/<channel>/<resolution>/<x_range>/<y_range>/<z_range>/" , methods = ["GET" ])
225
242
def get_cutout_xyz (collection , experiment , channel , resolution , x_range , y_range , z_range ):
226
243
"""
227
- Download a volume
244
+ Download a volume.
245
+
246
+ Returns 404 if the bossURI is not found.
228
247
"""
229
248
xs = [int (i ) for i in x_range .split (":" )]
230
249
ys = [int (i ) for i in y_range .split (":" )]
231
250
zs = [int (i ) for i in z_range .split (":" )]
232
- if (
233
- os .path .isdir ("{}/{}" .format (UPLOADS_PATH , collection )) and
234
- os .path .isdir ("{}/{}/{}" .format (UPLOADS_PATH , collection , experiment )) and
235
- os .path .isdir ("{}/{}/{}/{}" .format (UPLOADS_PATH , collection , experiment , channel ))
236
- ):
251
+ try :
237
252
data = MANAGER .getdata (collection , experiment , channel , resolution , xs , ys , zs )
238
253
res = {}
239
254
res ["dtype" ] = str (data .dtype )
240
255
res ["data" ] = data .tolist ()
241
256
return jsonify (res )
242
- else :
257
+ except IOError as e :
243
258
return Response (
244
- json .dumps ({"message" : "DNE" }),
245
- status = 402 ,
259
+ json .dumps ({"message" : str ( e ) }),
260
+ status = 404 ,
246
261
mimetype = "application/json"
247
262
)
248
263
0 commit comments