20
20
#
21
21
# https://www.nipreps.org/community/licensing/
22
22
#
23
- import json
23
+ from pathlib import Path
24
24
25
+ import orjson
25
26
from nipype .interfaces .base import (
26
27
BaseInterfaceInputSpec ,
27
28
Bunch ,
@@ -127,6 +128,7 @@ class UploadIQMsInputSpec(BaseInterfaceInputSpec):
127
128
128
129
class UploadIQMsOutputSpec (TraitedSpec ):
129
130
api_id = traits .Either (None , traits .Str , desc = 'Id for report returned by the web api' )
131
+ payload_file = File (desc = 'Submitted payload (only for debugging)' )
130
132
131
133
132
134
class UploadIQMs (SimpleInterface ):
@@ -153,6 +155,19 @@ def _run_interface(self, runtime):
153
155
modality = self .inputs .modality ,
154
156
)
155
157
158
+
159
+ payload_str = orjson .dumps (
160
+ payload ,
161
+ option = (
162
+ orjson .OPT_SORT_KEYS
163
+ | orjson .OPT_INDENT_2
164
+ | orjson .OPT_APPEND_NEWLINE
165
+ | orjson .OPT_SERIALIZE_NUMPY
166
+ ),
167
+ )
168
+ Path ('payload.json' ).write_bytes (payload_str )
169
+ self ._results ['payload_file' ] = str (Path ('payload.json' ).absolute ())
170
+
156
171
try :
157
172
self ._results ['api_id' ] = response .json ()['_id' ]
158
173
except (AttributeError , KeyError , ValueError ):
@@ -161,7 +176,7 @@ def _run_interface(self, runtime):
161
176
'QC metrics upload failed to create an ID for the record '
162
177
f'uploaded. Response from server follows: { response .text } '
163
178
'\n \n Payload:\n '
164
- f'{ json . dumps ( payload , indent = 2 ) } '
179
+ f'{ payload_str } '
165
180
)
166
181
config .loggers .interface .warning (errmsg )
167
182
@@ -177,7 +192,7 @@ def _run_interface(self, runtime):
177
192
'' ,
178
193
'' ,
179
194
'Payload:' ,
180
- json . dumps ( payload , indent = 2 ) ,
195
+ payload_str ,
181
196
]
182
197
)
183
198
config .loggers .interface .warning (errmsg )
@@ -209,8 +224,6 @@ def upload_qc_metrics(
209
224
210
225
"""
211
226
from copy import deepcopy
212
- from json import dumps , loads
213
- from pathlib import Path
214
227
215
228
import requests
216
229
@@ -219,7 +232,7 @@ def upload_qc_metrics(
219
232
errmsg = 'Unknown API endpoint' if not endpoint else 'Authentication failed.'
220
233
return Bunch (status_code = 1 , text = errmsg )
221
234
222
- in_data = loads (Path (in_iqms ).read_text ())
235
+ in_data = orjson . loads (Path (in_iqms ).read_bytes ())
223
236
224
237
# Extract metadata and provenance
225
238
meta = in_data .pop ('bids_meta' )
@@ -267,17 +280,18 @@ def upload_qc_metrics(
267
280
268
281
start_message = messages .QC_UPLOAD_START .format (url = endpoint )
269
282
config .loggers .interface .info (start_message )
283
+
270
284
try :
271
285
# if the modality is bold, call "bold" endpoint
272
286
response = requests .post (
273
287
f'{ endpoint } /{ modality } ' ,
274
288
headers = headers ,
275
- data = dumps (data ),
289
+ data = orjson . dumps (data , option = orjson . OPT_SERIALIZE_NUMPY ),
276
290
timeout = 15 ,
277
291
)
278
292
except requests .ConnectionError as err :
279
293
errmsg = f'QC metrics failed to upload due to connection error shown below:\n { err } '
280
- return Bunch (status_code = 1 , text = errmsg )
294
+ return Bunch (status_code = 1 , text = errmsg ), data
281
295
282
296
return response , data
283
297
0 commit comments