Exemplo n.º 1
0
 def test_get_tract_empty_density_map(self):
     ''' Sometimes a subject might not have a certain tract show up with tractography. So need
     to be able to handle an empty density map. '''
     self.insert_tract_test_data()
     
     def nib_load_patch(file_path):
         if md.s1_subject_id in file_path:
             return md.s1_t1
         elif md.s3_subject_id in file_path:
             return md.empty_nifti
         elif du.TEMPLATE_FILE_NAME in file_path:
             return md.template_nifti
         else:
             raise ValueError('Invalid file path passed to nib_load_patch in test_views.')
         
     def nib_save_patch(img, file_path):
         pass
     
     def send_file_patch(file_path, as_attachment=True, attachment_filename=None, conditional=True, add_etags=True):
         '''Monkey patch flask.send_file with this function to create a response object without needing
         to load a file from file system'''
         global file_path_to_test
         file_path_to_test = file_path
         headers = Headers()
         headers.add('Content-Disposition', 'attachment', filename=attachment_filename)
         return Response(mimetype='application/octet-stream', headers=headers)
     
     with monkey_patch(views, 'send_file', send_file_patch):
         with monkey_patch(du.nib, 'load', nib_load_patch):
             with monkey_patch(du.nib, 'save', nib_save_patch):
                 resp = self.client.get(f'tract/{md.t1_code}?{md.brc_atlas_males_query}&file_type=.nii.gz')
                 
     self.assert200(resp)
     assert md.t1_code in file_path_to_test and 'nii.gz' in file_path_to_test
Exemplo n.º 2
0
 def tract_test_response(self, tract_code, query_string):
     
     def nib_load_patch(file_path):
         if md.s1_subject_id in file_path:
             return md.s1_t1
         elif md.s3_subject_id in file_path:
             return md.s3_t1
         elif du.TEMPLATE_FILE_NAME in file_path:
             return md.template_nifti
         else:
             raise ValueError('Invalid file path passed to nib_load_patch in test_views.')
     
     def nib_save_patch(img, file_path):
         pass
     
     def send_file_patch(file_path, as_attachment=True, attachment_filename=None, conditional=True, add_etags=True):
         '''Monkey patch flask.send_file with this function to create a response object without needing
         to load a file from file system'''
         global file_path_to_test
         file_path_to_test = file_path
         headers = Headers()
         headers.add('Content-Disposition', 'attachment', filename=attachment_filename)
         return Response(mimetype='application/octet-stream', headers=headers)
     
     with monkey_patch(views, 'send_file', send_file_patch):
         with monkey_patch(du.nib, 'load', nib_load_patch):
             with monkey_patch(du.nib, 'save', nib_save_patch):
                 resp = self.client.get(f'tract/{tract_code}?{query_string}&file_type=.nii.gz')
                 
     return resp
Exemplo n.º 3
0
 def test_get_template_file_not_found(self):
     
     def send_file_patch(file_path, as_attachment=True, attachment_filename=None,
                         conditional=True, add_etags=True):
         '''Monkey patch flask.send_file with this function to
         generate FileNotFoundError'''
         file = open('fail.nii.gz', 'rb')
     
     with monkey_patch(views, 'send_file', send_file_patch):
         resp = self.client.get('/get_template')
         
     self.assert500(resp)
Exemplo n.º 4
0
 def test_get_template(self):
     
     file_path_to_test = ''
     
     def send_file_patch(file_path, as_attachment=True, attachment_filename=None,
                         conditional=True, add_etags=True):
         '''Monkey patch flask.send_file with this function to create a
         response object without needing to load a file from file system'''
         nonlocal file_path_to_test
         file_path_to_test = file_path
         headers = Headers()
         headers.add('Content-Disposition', 'attachment', filename=attachment_filename)
         headers['Content-Length'] = 1431363
         return Response(mimetype='application/octet-stream', headers=headers)
     
     with monkey_patch(views, 'send_file', send_file_patch):
         resp = self.client.get('/get_template')
     
     assert file_path_to_test == f'../{current_app.config["DATA_FILE_PATH"]}' \
                                 f'/{du.TEMPLATE_FILE_NAME}'
     assert resp.mimetype == 'application/octet-stream'
     assert resp.headers.get('Content-Disposition') == f'attachment; ' \
                                                       f'filename={du.TEMPLATE_FILE_NAME}'
     assert resp.headers.get('Content-Length') == '1431363'
Exemplo n.º 5
0
 def test_download_tract(self):
     
     # need to mock tract probability map + mean maps
     # mock data in db
     # monkey_patch nib.load, nib.save (for json file, may as well cache it for
     #     downloads with same query but different tract)
     # monkey patch Flask.send_file? (will attempt to create zip file in memory though
     #     so hopefully won't need to do this)
     
     self.insert_tract_test_data()
     
     # insert subject_tract_metrics
     s1t1m = SubjectTractMetrics(md.s1_subject_id,
                               md.m1_code,
                               md.t1_code,
                               0.5,
                               0.05,
                               0.5,
                               0.05,
                               3.)
     db.session.add(s1t1m)
     
     s3t1m = SubjectTractMetrics(md.s3_subject_id,
                               md.m1_code,
                               md.t1_code,
                               0.5,
                               0.05,
                               0.5,
                               0.05,
                               3.)
     db.session.add(s3t1m)
     
     db.session.commit()
     
     # mock cached file paths and added completed jobs to the cache
     prob_map_file_path = f'{md.t1_code}_some_time.nii.gz'
     MD_file_path = 'MD_some_time.nii.gz'
     FA_file_path = 'FA_some_time.nii.gz'
     current_app.cache.set(md.brc_atlas_males_query, {
                                         md.t1_code: {
                                             'status': 'COMPLETE',
                                             'result': prob_map_file_path
                                         },
                                         'mean_maps': {
                                             'status': 'COMPLETE',
                                             'result': {
                                                 'MD': MD_file_path,
                                                 'FA': FA_file_path
                                             }
                                         }
                                     })
     
     def nib_load_patch(file_path):
         if file_path == prob_map_file_path:
             return md.s1_t1
         elif file_path == MD_file_path:
             return md.s1_MD
         elif file_path == FA_file_path:
             return md.sd_FA
         else:
             raise ValueError(f'Unexpected file path {file_path} passed to nib_load_patch!')
     
     def nib_save_patch(img, file_path):
         # pretend to do something with the nifti img
         pass
     
     def zipfile_write_patch(self, file_path, arcname=''):
         pass
     
     with monkey_patch(du.nib, 'load', nib_load_patch), \
             monkey_patch(du.nib, 'save', nib_save_patch), \
                 monkey_patch(zipfile.ZipFile, 'write', zipfile_write_patch):
         resp = self.client.get(f'/download/tract/{md.t1_code}?{md.brc_atlas_males_query}')
         self.assert200(resp)
         
         zipped_bytes = resp.get_data()
         zipped_file = zipfile.ZipFile(io.BytesIO(zipped_bytes), 'r')
         with zipped_file.open('data.json') as data_json:
             data = json.loads(data_json.read())
             assert len(data['subjects']) == 2
             assert len(data['demographic_data']) == 2
Exemplo n.º 6
0
 def test_generate_mean_maps(self):
     s1 = Subject(subject_id=md.s1_subject_id,
                  gender=md.s1_gender,
                  age=md.s1_age,
                  handedness=md.s1_handedness,
                  edinburgh_handedness_raw=md.s1_edinburgh_handedness_raw,
                  ravens_iq_raw=md.s1_ravens_iq_raw,
                  dataset_code=md.s1_dataset_code,
                  file_path=md.s1_file_path,
                  mmse=md.s1_mmse)
     db.session.add(s1)
     
     s2 = Subject(subject_id=md.s2_subject_id,
                  gender=md.s2_gender,
                  age=md.s2_age,
                  handedness=md.s2_handedness,
                  edinburgh_handedness_raw=md.s2_edinburgh_handedness_raw,
                  ravens_iq_raw=md.s2_ravens_iq_raw,
                  dataset_code=md.s2_dataset_code,
                  file_path=md.s2_file_path,
                  mmse=md.s2_mmse)
     db.session.add(s2)
     
     s3 = Subject(subject_id=md.s3_subject_id,
                  gender=md.s3_gender,
                  age=md.s3_age,
                  handedness=md.s3_handedness,
                  edinburgh_handedness_raw=md.s3_edinburgh_handedness_raw,
                  ravens_iq_raw=md.s3_ravens_iq_raw,
                  dataset_code=md.s3_dataset_code,
                  file_path=md.s3_file_path,
                  mmse=md.s3_mmse)
     db.session.add(s3)
     
     s4 = Subject(subject_id=md.s4_subject_id,
                  gender=md.s4_gender,
                  age=md.s4_age,
                  handedness=md.s4_handedness,
                  edinburgh_handedness_raw=md.s4_edinburgh_handedness_raw,
                  ravens_iq_raw=md.s4_ravens_iq_raw,
                  dataset_code=md.s4_dataset_code,
                  file_path=md.s4_file_path,
                  mmse=md.s4_mmse)
     db.session.add(s4)
     
     d1 = Dataset(code=md.d1_code, name=md.d1_name, file_path=md.d1_file_path, query_params=md.d1_query_params)
     db.session.add(d1)
     
     d2 = Dataset(code=md.d2_code, name=md.d2_name, file_path=md.d2_file_path, query_params=md.d1_query_params)
     db.session.add(d2)
     
     db.session.commit()
     
     self.saved_file_path = ''
     self.saved_img = None
     
     def nib_save_patch(img, file_path):
         self.saved_file_path = file_path
         self.saved_img = img
         data = img.get_data()
         assert np.all(data == 1.)
     
     def nib_load_patch(file_path):
         if du.TEMPLATE_FILE_NAME in file_path:
             return md.template_nifti
         elif md.s1_subject_id in file_path:
             return md.s1_MD if 'MD' in file_path else md.s1_FA
         elif md.s3_subject_id in file_path:
             return md.s3_MD if 'MD' in file_path else md.s3_FA
         else:
             print('Unexpected file path passed to nib_load_path in test_views.test_generate_maps')
             print(file_path)
             return
         
     # assert the cache is empty for query string before response
     assert not current_app.cache.get(md.brc_atlas_males_query)
     
     # first request
     with monkey_patch(du.nib, 'save', nib_save_patch):
         with monkey_patch(du.nib, 'load', nib_load_patch):
             resp = self.client.get(f'/generate_mean_maps?{md.brc_atlas_males_query}')
     
     self.assertStatus(resp, 204)
     assert current_app.cache.get(md.brc_atlas_males_query) # check data has been cached