Skip to content

Commit 8b5996a

Browse files
committed
Update dm to use classification
1 parent a47d622 commit 8b5996a

13 files changed

+87
-38
lines changed

api/dao/liststorage.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44

55
from .. import config
6+
from .. import util
67
from . import consistencychecker, containerutil
78
from . import APIStorageException, APIConflictException
89
from .containerstorage import SessionStorage, AcquisitionStorage
@@ -46,7 +47,7 @@ def get_container(self, _id, query_params=None):
4647
log.debug('query {}'.format(query))
4748
return self.dbc.find_one(query, projection)
4849

49-
def exec_op(self, action, _id=None, query_params=None, payload=None, exclude_params=None, replace_classification=False):
50+
def exec_op(self, action, _id=None, query_params=None, payload=None, exclude_params=None):
5051
"""
5152
Generic method to exec an operation.
5253
The request is dispatched to the corresponding private methods.
@@ -63,7 +64,7 @@ def exec_op(self, action, _id=None, query_params=None, payload=None, exclude_par
6364
if action == 'DELETE':
6465
return self._delete_el(_id, query_params)
6566
if action == 'PUT':
66-
return self._update_el(_id, query_params, payload, exclude_params, replace_classification=replace_classification)
67+
return self._update_el(_id, query_params, payload, exclude_params)
6768
if action == 'POST':
6869
return self._create_el(_id, payload, exclude_params)
6970
raise ValueError('action should be one of GET, POST, PUT, DELETE')
@@ -346,20 +347,21 @@ def inflate_job_info(analysis):
346347

347348
class FileStorage(ListStorage):
348349

349-
def _update_el(self, _id, query_params, payload, exclude_params, replace_classification=False):
350+
def update_file(self, _id, query_params, payload, replace_fields=False):
350351
mod_elem = {}
351352
update = {}
352353

353354
# If we want to add to the classification lists rather than replace
354355
# the entirity of the classification map, use $addToSet.
355356
# This allows some endpoints to only make additive changes
356-
if not replace_classification:
357-
classification = payload.get('classification')
357+
if not replace_fields:
358+
classification = payload.pop('classification', None)
358359
if classification:
359360
add_to_set = {}
360361
for k,array in classification.items():
361362
add_to_set[self.list_name + '.$.classification.' + k] = array
362363
update['$addToSet'] = add_to_set
364+
payload = util.mongo_dict(payload)
363365

364366
for k,v in payload.items():
365367
mod_elem[self.list_name + '.$.' + k] = v

api/handlers/listhandler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,11 @@ def put(self, cont_name, list_name, **kwargs):
521521
if not ModalityHandler.check_classification(modality, classification):
522522
self.abort(400, 'Classification does not match allowable values for modality {}.'.format(modality))
523523

524-
rc = self.is_true('replace_classification')
524+
rf = self.is_true('replace_fields')
525525

526526
try:
527-
result = keycheck(mongo_validator(permchecker(storage.exec_op)))('PUT', _id=_id, query_params=kwargs, payload=payload)
527+
keycheck(mongo_validator(permchecker(noop)))('PUT', _id=_id, query_params=kwargs, payload=payload)
528+
result = storage.update_file(_id, kwargs, payload, replace_fields=rf)
528529
except APIStorageException as e:
529530
self.abort(400, e.message)
530531
# abort if the query of the update wasn't able to find any matching documents

api/handlers/modalityhandler.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import datetime as dt
2-
31
from ..web import base
42
from .. import config
5-
from .. import util
6-
from ..auth import require_drone, require_login, require_superuser
3+
from ..auth import require_login, require_superuser
74
from ..dao import containerstorage, APINotFoundException
8-
from ..validators import validate_data
5+
# from ..validators import validate_data
96

107
log = config.log
118

api/placer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,16 @@ def check(self):
242242
job = Job.get(self.context.get('job_id'))
243243
input_names = [{'name': v.name} for v in job.inputs.itervalues()]
244244

245-
measurement = self.metadata.get(self.container_type, {}).pop('measurement', None)
245+
classification = self.metadata.get(self.container_type, {}).pop('measurement', None)
246246
info = self.metadata.get(self.container_type,{}).pop('metadata', None)
247247
modality = self.metadata.get(self.container_type, {}).pop('instrument', None)
248-
if measurement or info or modality:
248+
if classification or info or modality:
249249
files_ = self.metadata[self.container_type].get('files', [])
250250
files_ += input_names
251251
for f in files_:
252-
if measurement:
253-
f['measurements'] = [measurement]
252+
if classification:
253+
custom = {'custom': [classification]}
254+
f['classification'] = custom
254255
if info:
255256
f['info'] = info
256257
if modality:
@@ -502,7 +503,7 @@ def finalize(self):
502503

503504
# OPPORTUNITY: packfile endpoint could be extended someday to take additional metadata.
504505
'modality': None,
505-
'measurements': [],
506+
'classification': {},
506507
'tags': [],
507508
'info': {},
508509

api/upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def process_upload(request, strategy, container_type=None, id_=None, origin=None
119119

120120
'type': None,
121121
'modality': None,
122-
'measurements': [],
122+
'classification': {},
123123
'tags': [],
124124
'info': {}
125125
}

bin/database.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from api.jobs import gears
1515
from api.types import Origin
1616

17-
CURRENT_DATABASE_VERSION = 21 # An int that is bumped when a new schema change is made
17+
CURRENT_DATABASE_VERSION = 22 # An int that is bumped when a new schema change is made
1818

1919
def get_db_version():
2020

@@ -636,6 +636,58 @@ def dm_v2_updates(cont_list, cont_name):
636636
dm_v2_updates(config.db.acquisitions.find(query), 'acquisitions')
637637

638638

639+
def upgrade_to_22():
640+
"""
641+
Change file `measurement` field to `classification` map
642+
Place all measurements in `custom` key on map
643+
"""
644+
645+
def update_project_template(template):
646+
for a in template.get('acquisitions', []):
647+
new_file_templates = []
648+
for f in a.get('files', []):
649+
if f.get('measurement'):
650+
measurements = f.pop('measurement')
651+
f['classification'] = {'custom': measurement}
652+
653+
return template
654+
655+
656+
def change_to_classification(cont_list, cont_name):
657+
for container in cont_list:
658+
659+
if cont_name == 'projects' and container.get('template'):
660+
new_template = update_project_template(json.loads(container.get('template')))
661+
update['$set'] = {'template': new_template}
662+
663+
query = {'_id': container['_id']}
664+
update = {}
665+
666+
files = container.get('files')
667+
if files is not None:
668+
updated_files = []
669+
for file_ in files:
670+
if 'measurements' in file_:
671+
measurements = file_.pop('measurements', [])
672+
custom = {'custom': measurements}
673+
file_['classification'] = custom
674+
675+
updated_files.append(file_)
676+
if update.get('$set'):
677+
update['$set']['files'] = updated_files
678+
else:
679+
update['$set'] = {'files': updated_files}
680+
681+
result = config.db[cont_name].update_one(query, update)
682+
683+
query = {'files.measurements': { '$exists': True}}
684+
685+
change_to_classification(config.db.collections.find(query), 'collections')
686+
change_to_classification(config.db.projects.find(query), 'projects')
687+
change_to_classification(config.db.sessions.find(query), 'sessions')
688+
change_to_classification(config.db.acquisitions.find(query), 'acquisitions')
689+
690+
639691
def upgrade_schema():
640692
"""
641693
Upgrades db to the current schema version

raml/examples/file_info_list.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"hash": "v0-sha384-12188e00a26650b2baa3f0195337dcf504f4362bb2136eef0cdbefb57159356b1355a0402fca0ab5ab081f21c305e5c2",
99
"name": "cortical_surface_right_hemisphere.obj",
1010
"tags": [],
11-
"measurements": [],
11+
"classification": {},
1212
"modified": "2016-10-18T15:26:35.701000+00:00",
1313
"instrument": null,
1414
"size": 21804112,
@@ -23,7 +23,7 @@
2323
"hash": "v0-sha384-12188e00a26650b2baa3f0195337dcf504f4362bb2136eef0cdbefb57159356b1355a0402fca0ab5ab081f21c305e5c2",
2424
"name": "cortical_surface_right_hemisphere.obj",
2525
"tags": [],
26-
"measurements": [],
26+
"classification": {},
2727
"modified": "2016-10-18T17:45:17.776000+00:00",
2828
"instrument": null,
2929
"metadata": {},

raml/examples/output/acquisition-list.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"name": "Admin Import"
1010
},
1111
"mimetype": "application/zip",
12-
"measurements": [],
12+
"classification": {},
1313
"hash": "v0-sha384-dd3c97bfe0ad1fcba75ae6718c6e81038c59af4f447f5db194d52732efa4f955b28455db02eb64cad3e4e55f11e3679f",
1414
"name": "4784_1_1_localizer_dicom.zip",
1515
"tags": [],
@@ -49,7 +49,7 @@
4949
"name": "Admin Import"
5050
},
5151
"mimetype": "application/zip",
52-
"measurements": [],
52+
"classification": {},
5353
"hash": "v0-sha384-ca055fb36845db86e4278cf6e185f8674d11a96f4b29af27e401fc495cc82ef6b53a5729c3757713064649dc71c8c725",
5454
"name": "4784_3_1_t1_dicom.zip",
5555
"tags": [],
@@ -89,7 +89,7 @@
8989
"name": "Admin Import"
9090
},
9191
"mimetype": "application/zip",
92-
"measurements": [],
92+
"classification": {},
9393
"hash": "v0-sha384-537e42b1dd8f1feef9844fbfb4f60461361e71cafa7055556097e9d0b9f7fac68c8f234ed126af9412bd43a548948847",
9494
"name": "4784_5_1_fmri_dicom.zip",
9595
"tags": [],

raml/examples/output/acquisition.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"name": "Admin Import"
99
},
1010
"mimetype": "application/zip",
11-
"measurements": [],
11+
"classification": {},
1212
"hash": "v0-sha384-dd3c97bfe0ad1fcba75ae6718c6e81038c59af4f447f5db194d52732efa4f955b28455db02eb64cad3e4e55f11e3679f",
1313
"name": "4784_1_1_localizer_dicom.zip",
1414
"tags": [],

raml/examples/output/analysis-item.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"hash": "v0-sha384-12188e00a26650b2baa3f0195337dcf504f4362bb2136eef0cdbefb57159356b1355a0402fca0ab5ab081f21c305e5c2",
99
"name": "cortical_surface_right_hemisphere.obj",
1010
"tags": [],
11-
"measurements": [],
11+
"classification": {},
1212
"modified": "2016-10-18T15:26:35.701000+00:00",
1313
"instrument": null,
1414
"input": true,
@@ -24,7 +24,7 @@
2424
"hash": "v0-sha384-12188e00a26650b2baa3f0195337dcf504f4362bb2136eef0cdbefb57159356b1355a0402fca0ab5ab081f21c305e5c2",
2525
"name": "cortical_surface_right_hemisphere.obj",
2626
"tags": [],
27-
"measurements": [],
27+
"classification": {},
2828
"modified": "2016-10-18T17:45:17.776000+00:00",
2929
"instrument": null,
3030
"output": true,

raml/schemas/definitions/analysis.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
{"type":"null"}
5050
]
5151
},
52-
"measurements": {"$ref":"../definitions/file.json#/definitions/measurements"},
52+
"classification": {"$ref":"../definitions/file.json#/definitions/classification"},
5353
"tags": {"$ref":"../definitions/file.json#/definitions/tags"},
5454
"info": {"$ref":"../definitions/file.json#/definitions/info"},
5555
"origin":{"$ref":"../definitions/file.json#/definitions/origin"},

raml/schemas/definitions/file.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
"file-type": { "type": "string" },
66
"mimetype": { "type": "string" },
77
"modality": { "type": "string" },
8-
"measurements": {
9-
"items": { "type": "string"},
10-
"type": "array",
11-
"uniqueItems": true
8+
"classification": {
9+
"type": "object"
1210
},
1311
"tags": {
1412
"items": { "type": "string"},
@@ -37,7 +35,7 @@
3735
"type": {"$ref":"#/definitions/file-type"},
3836
"mimetype": {"$ref":"#/definitions/mimetype"},
3937
"modality": {"$ref":"#/definitions/modality"},
40-
"measurements": {"$ref":"#/definitions/measurements"},
38+
"classification": {"$ref":"#/definitions/classification"},
4139
"tags": {"$ref":"#/definitions/tags"},
4240
"info": {"$ref":"#/definitions/info"}
4341
},
@@ -55,7 +53,7 @@
5553
{"type":"null"}
5654
]
5755
},
58-
"measurements": {"$ref":"#/definitions/measurements"},
56+
"classification": {"$ref":"#/definitions/classification"},
5957
"tags": {"$ref":"#/definitions/tags"},
6058
"info": {"$ref":"#/definitions/info"},
6159
"origin":{"$ref":"#/definitions/origin"},

raml/schemas/mongo/file.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
"size": { "type": "integer" },
1111
"hash": { "type": "string" },
1212
"modality": { "type": "string" },
13-
"measurements": {
14-
"items": { "type": "string"},
15-
"type": "array",
16-
"uniqueItems": true
13+
"classification": {
14+
"type": "object"
1715
},
1816
"tags": {
1917
"items": { "type": "string"},

0 commit comments

Comments
 (0)