def test_add_DOI(self): collection1 = Collection(name='Collection1', owner=self.u1, private=False) collection1.save() collection2 = Collection(name='Collection2', owner=self.u1, private=False) collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) image1 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/all.nii.gz'), collection=collection1, image_name="image1") image2 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/motor_lips.nii.gz'), collection=collection2, image_name="image2") comparison = Comparison.objects.filter(image1=image1, image2=image2) self.assertEqual(len(comparison), 0) print "without DOI: %s" % Comparison.objects.all().count() collection = Collection.objects.get(pk=collection1.pk) collection.DOI = '10.3389/fninf.2015.00020' collection.save() print "with DOI: %s" % Comparison.objects.all().count() print collection.basecollectionitem_set.instance_of(Image).all() comparison = Comparison.objects.filter(image1=image1, image2=image2) self.assertEqual(len(comparison), 1)
def test_get_similar_images(self): collection1 = Collection(name='Collection1', owner=self.u1, DOI='10.3389/fninf.2015.00099') collection1.save() collection2 = Collection(name='Collection2', owner=self.u1, DOI='10.3389/fninf.2015.00089') collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) image1 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/all.nii.gz'), collection=collection1, image_name="image1") image2 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/all.nii.gz'), collection=collection2, image_name="image2") similar_images = get_similar_images(int(image1.pk)) print "Success for this test means the pandas DataFrame shows the copy in first position with score of 1" self.assertEqual(similar_images['image_id'][0], int(image2.pk)) self.assertEqual(similar_images['score'][0], 1)
def test_private_to_public_switch(self): private_collection1 = Collection(name='privateCollection1',owner=self.u1, private=True, DOI='10.3389/fninf.2015.00099') private_collection1.save() private_collection2 = Collection(name='privateCollection2',owner=self.u1, private=True, DOI='10.3389/fninf.2015.00089') private_collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) private_image1 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/all.nii.gz'), collection=private_collection1, image_name = "image1") private_image2 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/motor_lips.nii.gz'), collection=private_collection2, image_name = "image2") comparison = Comparison.objects.filter(image1=private_image1,image2=private_image2) self.assertEqual(len(comparison), 0) print "before private: %s"%Comparison.objects.all().count() private_collection1 = Collection.objects.get(pk=private_collection1.pk) private_collection1.private = False private_collection1.save() private_collection2 = Collection.objects.get(pk=private_collection2.pk) private_collection2.private = False private_collection2.save() print "after private: %s"%Comparison.objects.all().count() print private_collection1.basecollectionitem_set.instance_of(Image).all() comparison = Comparison.objects.filter(image1=private_image1,image2=private_image2) self.assertEqual(len(comparison), 1)
def setUp(self): print "Preparing to test image comparison..." self.tmpdir = tempfile.mkdtemp() app_path = os.path.abspath(os.path.dirname(__file__)) self.u1 = User.objects.create(username='******') self.comparisonCollection1 = Collection(name='comparisonCollection1', owner=self.u1, DOI='10.3389/fninf.2015.00008') self.comparisonCollection1.save() self.comparisonCollection2 = Collection(name='comparisonCollection2', owner=self.u1, DOI='10.3389/fninf.2015.00009') self.comparisonCollection2.save() self.comparisonCollection3 = Collection(name='comparisonCollection3', owner=self.u1, DOI='10.3389/fninf.2015.00010') self.comparisonCollection3.save() self.comparisonCollection4 = Collection(name='comparisonCollection4', owner=self.u1, DOI='10.3389/fninf.2015.00011') self.comparisonCollection4.save() self.comparisonCollection5 = Collection(name='comparisonCollection5', owner=self.u1, DOI='10.3389/fninf.2015.00012') self.comparisonCollection5.save() image1 = save_statmap_form(image_path=os.path.join(app_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz'), collection=self.comparisonCollection1, image_name = "image1", ignore_file_warning=True) self.pk1 = image1.id # Image 2 is equivalent to 1, so pearson should be 1.0 image2 = save_statmap_form(image_path=os.path.join(app_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz'), collection=self.comparisonCollection2, image_name = "image1_copy", ignore_file_warning=True) self.pk1_copy = image2.id # "Bricks" images bricks = split_4D_to_3D(nibabel.load(os.path.join(app_path,'test_data/TTatlas.nii.gz')),tmp_dir=self.tmpdir) image3 = save_statmap_form(image_path=bricks[0][1],collection=self.comparisonCollection3,image_name="image2",ignore_file_warning=True) self.pk2 = image3.id image4 = save_statmap_form(image_path=bricks[1][1],collection=self.comparisonCollection4,image_name="image3",ignore_file_warning=True) self.pk3 = image4.id # This last image is a statmap with NaNs to test that transformation doesn't eliminate them image_nan = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/motor_lips_nan.nii.gz'), collection=self.comparisonCollection5, image_name = "image_nan", ignore_file_warning=True) self.pknan = image_nan.id Similarity.objects.update_or_create(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL") self.pearson_metric = Similarity.objects.filter(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL")
def setUp(self): self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username='******') self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name='Collection1', owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas(name='unorderedAtlas', description='', collection=self.Collection1) # Save new atlas object, unordered print "Adding unordered and ordered atlas..." nii_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz') xml_path = os.path.join( self.test_path, 'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml' ) self.unorderedAtlas = save_atlas_form(nii_path=nii_path, xml_path=xml_path, collection=self.Collection1, name="unorderedAtlas") # Ordered nii_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz') xml_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml') self.orderedAtlas = save_atlas_form(nii_path=nii_path, xml_path=xml_path, collection=self.Collection1, name="orderedAtlas") # Statistical Map 1 and 2 print "Adding statistical maps..." nii_path = os.path.join(self.test_path, 'test_data/statmaps/motor_lips.nii.gz') self.Image1 = save_statmap_form(image_path=nii_path, collection=self.Collection1) nii_path = os.path.join(self.test_path, 'test_data/statmaps/beta_0001.nii.gz') self.Image2 = save_statmap_form(image_path=nii_path, collection=self.Collection1) # Zip file with nidm results print "Adding nidm results..." zip_file = os.path.join(self.test_path, 'test_data/nidm/spm_example.nidm.zip') self.nidm = save_nidm_form(zip_file=zip_file, collection=self.Collection1)
def handle(self, *args, **options): down_data() clearDB() User.objects.all().delete() app_path = '/code/neurovault/apps/statmaps/tests/bench' u1 = User.objects.create(username='******') num_files = len(os.listdir(os.path.join(app_path, 'images/'))) index_table = np.zeros(num_files) query_table = np.zeros(num_files) for i, file in enumerate(os.listdir(os.path.join(app_path, 'images/'))): #print 'Adding subject ' + file randomCollection = Collection(name='random' + file, owner=u1, DOI='10.3389/fninf.2015.00008' + str(i)) randomCollection.save() t = Timer() with t: image = save_statmap_form( image_path=os.path.join(app_path, 'images/', file), collection=randomCollection, image_name=file, ignore_file_warning=True) index_table[i] = t.interval np.savetxt(os.path.join(app_path, 'results_index'+ str(datetime.datetime.utcnow())[:10] + '.csv'), index_table, delimiter=",") t = Timer() with t: _dummy = get_similar_images(int(image.pk)) query_table[i] = t.interval np.savetxt(os.path.join(app_path, 'results_query' + str(datetime.datetime.utcnow())[:10] + '.csv'), query_table, delimiter=",")
def setUp(self): super(TestStatisticMapChange, self).setUp() self.item = save_statmap_form(image_path=self.abs_file_path( 'test_data/statmaps/motor_lips.nii.gz'), collection=self.coll) self.item_url = '/api/images/%s/' % self.item.pk
def handle(self, *args, **options): down_data() clearDB() app_path = '/code/neurovault/apps/statmaps/tests/bench' u1 = User.objects.create(username='******') num_files = len(os.listdir(os.path.join(app_path, 'images/'))) index_table = np.zeros(num_files) query_table = np.zeros(num_files) for i, file in enumerate(os.listdir(os.path.join(app_path, 'images/'))): #print 'Adding subject ' + file randomCollection = Collection(name='random' + file, owner=u1, DOI='10.3389/fninf.2015.00008' + str(i)) randomCollection.save() t = Timer() with t: image = save_statmap_form( image_path=os.path.join(app_path, 'images/', file), collection=randomCollection, image_name=file, ignore_file_warning=True) index_table[i] = t.interval np.save(os.path.join(app_path, 'results_index'+ str(datetime.datetime.utcnow())[:10]), index_table) factory = RequestFactory() request = factory.get('/images/' + str(i) + '/find_similar') t = Timer() with t: _dummy = find_similar(request, image.pk) query_table[i] = t.interval np.save(os.path.join(app_path, 'results_query' + str(datetime.datetime.utcnow())[:10]), query_table)
def createFeatures(subjects=None, resample_dim=[4, 4, 4]): clearDB() if os.path.isfile('/code/neurovault/apps/statmaps/tests/features'+str(subjects)+str(resample_dim)+'.npy'): #and subjects == None: return np.load('/code/neurovault/apps/statmaps/tests/features'+str(subjects)+str(resample_dim)+'.npy').T, \ pickle.load(open('/code/neurovault/apps/statmaps/tests/dict_feat'+str(subjects)+str(resample_dim)+'.p',"rb" )) else: feature_dimension = get_feature_dimension(resample_dim) features = np.empty([feature_dimension, subjects]) #4*4*4 = 28549 #16*16*16 = 450 dict_feat = {} u1 = User.objects.create(username='******') for i, file in enumerate(os.listdir('/code/neurovault/apps/statmaps/tests/bench/images/')): # print 'Adding subject ' + file print i randomCollection = Collection(name='random' + file, owner=u1, DOI='10.3389/fninf.2015.00008' + str(i)) randomCollection.save() image = save_statmap_form(image_path=os.path.join('/code/neurovault/apps/statmaps/tests/bench/images/', file), collection=randomCollection, image_name=file, ignore_file_warning=True) if not image.reduced_representation or not os.path.exists(image.reduced_representation.path): image = save_resampled_transformation_single(image.pk, resample_dim) features[:, i] = np.load(image.reduced_representation.file) dict_feat[i] = int(file.split(".")[0]) if i == subjects-1: features[np.isnan(features)] = 0 np.save('/code/neurovault/apps/statmaps/tests/features'+str(subjects)+str(resample_dim)+'.npy', features) pickle.dump(dict_feat,open('/code/neurovault/apps/statmaps/tests/dict_feat'+str(subjects)+str(resample_dim)+'.p',"wb" )) return features.T, dict_feat
def setUp(self): self.test_path = os.path.abspath(os.path.dirname(__file__)) self.user = User.objects.create(username='******') self.client = Client() self.client.login(username=self.user) self.Collection1 = Collection(name='Collection1', owner=self.user) self.Collection1.save() self.unorderedAtlas = Atlas( name='unorderedAtlas', description='', collection=self.Collection1) # Save new atlas object, unordered print "Adding unordered and ordered atlas..." nii_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz') xml_path = os.path.join( self.test_path, 'test_data/api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml') self.unorderedAtlas = save_atlas_form(nii_path=nii_path, xml_path=xml_path, collection=self.Collection1, name="unorderedAtlas") # Ordered nii_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz') xml_path = os.path.join( self.test_path, 'test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml') self.orderedAtlas = save_atlas_form(nii_path=nii_path, xml_path=xml_path, collection=self.Collection1, name="orderedAtlas") # Statistical Map 1 and 2 print "Adding statistical maps..." nii_path = os.path.join( self.test_path, 'test_data/statmaps/motor_lips.nii.gz') self.Image1 = save_statmap_form( image_path=nii_path, collection=self.Collection1) nii_path = os.path.join( self.test_path, 'test_data/statmaps/beta_0001.nii.gz') self.Image2 = save_statmap_form( image_path=nii_path, collection=self.Collection1) # Zip file with nidm results print "Adding nidm results..." zip_file = os.path.join( self.test_path, 'test_data/nidm/spm_example.nidm.zip') self.nidm = save_nidm_form( zip_file=zip_file, collection=self.Collection1)
def setUp(self): print "\n#### TESTING THRESHOLDED IMAGES IN COMPARISON\n" self.tmpdir = tempfile.mkdtemp() self.app_path = os.path.abspath(os.path.dirname(__file__)) self.u1 = User.objects.create(username='******') self.comparisonCollection1 = Collection(name='comparisonCollection1', owner=self.u1, DOI='10.3389/fninf.2015.00008') self.comparisonCollection1.save() self.comparisonCollection2 = Collection(name='comparisonCollection2', owner=self.u1, DOI='10.3389/fninf.2015.00009') self.comparisonCollection2.save() self.comparisonCollection3 = Collection(name='comparisonCollection3', owner=self.u1, DOI='10.3389/fninf.2015.00010') self.comparisonCollection3.save() self.comparisonCollection4 = Collection(name='comparisonCollection4', owner=self.u1, DOI='10.3389/fninf.2015.00011') self.comparisonCollection4.save() # Image 1 is an atlas print "Adding atlas image..." nii_path = os.path.join(self.app_path,"test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz") xml_path = os.path.join(self.app_path,"test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml") image1 = save_atlas_form(nii_path=nii_path,xml_path=xml_path,collection = self.comparisonCollection1) self.pk1 = image1.id # Image 2 is a statistical map print "Adding statistical map..." image_path = os.path.join(self.app_path,'test_data/statmaps/beta_0001.nii.gz') image2 = save_statmap_form(image_path=image_path,collection = self.comparisonCollection2) self.pk2 = image2.id # Image 3 is a thresholded statistical map print "Adding thresholded statistical map..." image_paths = [os.path.join(self.app_path,'test_data/statmaps/box_0b_vs_1b.img'), os.path.join(self.app_path,'test_data/statmaps/box_0b_vs_1b.hdr')] image3 = save_statmap_form(image_path=image_paths, collection = self.comparisonCollection3,ignore_file_warning=True) self.pk3 = image3.id # Create similarity object Similarity.objects.update_or_create(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL") self.pearson_metric = Similarity.objects.filter(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL")
def setUp(self): super(TestStatisticMapChange, self).setUp() self.item = save_statmap_form( image_path=self.abs_data_path('statmaps/motor_lips.nii.gz'), collection=self.coll ) self.item_url = '/api/images/%s/' % self.item.pk
def setUp(self): print "Preparing to test image comparison..." self.tmpdir = tempfile.mkdtemp() app_path = os.path.abspath(os.path.dirname(__file__)) self.u1 = User.objects.create(username='******') self.comparisonCollection = Collection(name='comparisonCollection',owner=self.u1) self.comparisonCollection.save() image1 = save_statmap_form(image_path=os.path.join(app_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz'), collection=self.comparisonCollection, image_name = "image1", ignore_file_warning=True) self.pk1 = image1.id # Image 2 is equivalent to 1, so pearson should be 1.0 image2 = save_statmap_form(image_path=os.path.join(app_path,'test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz'), collection=self.comparisonCollection, image_name = "image1_copy", ignore_file_warning=True) self.pk1_copy = image2.id # "Bricks" images bricks = split_afni4D_to_3D(nibabel.load(os.path.join(app_path,'test_data/TTatlas.nii.gz')),tmp_dir=self.tmpdir) image3 = save_statmap_form(image_path=bricks[0][1],collection=self.comparisonCollection,image_name="image2",ignore_file_warning=True) self.pk2 = image3.id image4 = save_statmap_form(image_path=bricks[1][1],collection=self.comparisonCollection,image_name="image3",ignore_file_warning=True) self.pk3 = image4.id # This last image is a statmap with NaNs to test that transformation doesn't eliminate them image_nan = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/motor_lips_nan.nii.gz'), collection=self.comparisonCollection, image_name = "image_nan", ignore_file_warning=True) self.pknan = image_nan.id Similarity.objects.update_or_create(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL") self.pearson_metric = Similarity.objects.filter(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL")
def test_private_to_public_switch(self): private_collection = Collection(name='privateCollection',owner=self.u1, private=True) private_collection.save() app_path = os.path.abspath(os.path.dirname(__file__)) private_image1 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/all.nii.gz'), collection=private_collection, image_name = "image1") private_image2 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/motor_lips.nii.gz'), collection=private_collection, image_name = "image2") comparison = Comparison.objects.filter(image1=private_image1,image2=private_image2) self.assertEqual(len(comparison), 0) print "before private: %s"%Comparison.objects.all().count() private_collection = Collection.objects.get(pk=private_collection.pk) private_collection.private = False private_collection.save() print "after private: %s"%Comparison.objects.all().count() print private_collection.image_set.all() comparison = Comparison.objects.filter(image1=private_image1,image2=private_image2) self.assertEqual(len(comparison), 1)
def test_get_similar_images(self): collection1 = Collection(name='Collection1', owner=self.u1, DOI='10.3389/fninf.2015.00099') collection1.save() collection2 = Collection(name='Collection2', owner=self.u1, DOI='10.3389/fninf.2015.00089') collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) image1 = save_statmap_form(image_path=os.path.join(app_path, 'test_data/statmaps/all.nii.gz'), collection=collection1, image_name="image1") image2 = save_statmap_form(image_path=os.path.join(app_path, 'test_data/statmaps/all.nii.gz'), collection=collection2, image_name="image2") similar_images = get_similar_images(int(image1.pk)) print "Success for this test means the pandas DataFrame shows the copy in first position with score of 1" self.assertEqual(similar_images['image_id'][0], int(image2.pk)) self.assertEqual(similar_images['score'][0], 1)
def test_private_to_public_switch(self): private_collection1 = Collection(name='privateCollection1', owner=self.u1, private=True, DOI='10.3389/fninf.2015.00099') private_collection1.save() private_collection2 = Collection(name='privateCollection2', owner=self.u1, private=True, DOI='10.3389/fninf.2015.00089') private_collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) private_image1 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/all.nii.gz'), collection=private_collection1, image_name="image1") private_image2 = save_statmap_form(image_path=os.path.join( app_path, 'test_data/statmaps/motor_lips.nii.gz'), collection=private_collection2, image_name="image2") comparison = Comparison.objects.filter(image1=private_image1, image2=private_image2) self.assertEqual(len(comparison), 0) print "before private: %s" % Comparison.objects.all().count() private_collection1 = Collection.objects.get(pk=private_collection1.pk) private_collection1.private = False private_collection1.save() private_collection2 = Collection.objects.get(pk=private_collection2.pk) private_collection2.private = False private_collection2.save() print "after private: %s" % Comparison.objects.all().count() print private_collection1.basecollectionitem_set.instance_of( Image).all() comparison = Comparison.objects.filter(image1=private_image1, image2=private_image2) self.assertEqual(len(comparison), 1)
def get_feature_dimension(resample_dim): u1 = User.objects.create(username='******'+str(resample_dim)) for file in os.listdir('/code/neurovault/apps/statmaps/tests/bench/images/'): randomCollection = Collection(name='random' + file, owner=u1, DOI='10.3389/fninf.2015.00008' + file) randomCollection.save() image = save_statmap_form(image_path=os.path.join('/code/neurovault/apps/statmaps/tests/bench/images/', file), collection=randomCollection, image_name=file, ignore_file_warning=True) if not image.reduced_representation or not os.path.exists(image.reduced_representation.path): image = save_resampled_transformation_single(image.pk, resample_dim) feature = np.load(image.reduced_representation.file) dimension = feature.shape[0] clearDB() break return dimension
def setUp(self): self.user = User.objects.create_user('NeuroGuy') self.user.save() self.collection = Collection(owner=self.user, name="Test Collection") self.collection.save() self.unorderedAtlas = save_atlas_form( nii_path=self.abs_data_path( 'api/VentralFrontal_thr75_summaryimage_2mm.nii.gz' ), xml_path=self.abs_data_path( 'api/unordered_VentralFrontal_thr75_summaryimage_2mm.xml' ), collection=self.collection, name="unorderedAtlas" ) self.image1 = save_statmap_form( image_path=self.abs_data_path( 'statmaps/motor_lips.nii.gz' ), collection=self.collection ) self.image2 = save_statmap_form( image_path=self.abs_data_path( 'statmaps/beta_0001.nii.gz' ), collection=self.collection ) self.nidm = save_nidm_form( zip_file=self.abs_data_path( 'nidm/spm_example.nidm.zip' ), collection=self.collection )
def setUp(self): print "\n#### TESTING THRESHOLDED IMAGES IN COMPARISON\n" self.tmpdir = tempfile.mkdtemp() self.app_path = os.path.abspath(os.path.dirname(__file__)) self.u1 = User.objects.create(username='******') self.comparisonCollection = Collection(name='comparisonCollection',owner=self.u1) self.comparisonCollection.save() # Image 1 is an atlas print "Adding atlas image..." nii_path = os.path.join(self.app_path,"test_data/api/VentralFrontal_thr75_summaryimage_2mm.nii.gz") xml_path = os.path.join(self.app_path,"test_data/api/VentralFrontal_thr75_summaryimage_2mm.xml") image1 = save_atlas_form(nii_path=nii_path,xml_path=xml_path,collection = self.comparisonCollection) self.pk1 = image1.id # Image 2 is a statistical map print "Adding statistical map..." image_path = os.path.join(self.app_path,'test_data/statmaps/beta_0001.nii.gz') image2 = save_statmap_form(image_path=image_path,collection = self.comparisonCollection) self.pk2 = image2.id # Image 3 is a thresholded statistical map print "Adding thresholded statistical map..." image_paths = [os.path.join(self.app_path,'test_data/statmaps/box_0b_vs_1b.img'), os.path.join(self.app_path,'test_data/statmaps/box_0b_vs_1b.hdr')] image3 = save_statmap_form(image_path=image_paths, collection = self.comparisonCollection,ignore_file_warning=True) self.pk3 = image3.id # Create similarity object Similarity.objects.update_or_create(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL") self.pearson_metric = Similarity.objects.filter(similarity_metric="pearson product-moment correlation coefficient", transformation="voxelwise", metric_ontology_iri="http://webprotege.stanford.edu/RCS8W76v1MfdvskPLiOdPaA", transformation_ontology_iri="http://webprotege.stanford.edu/R87C6eFjEftkceScn1GblDL")
def setUpClass(cls): cls.test_path = os.path.abspath(os.path.dirname(__file__)) cls.user, _ = User.objects.get_or_create(username='******') cls.client = Client() cls.client.login(username=cls.user) cls.Collection1 = Collection(name='Collection1', owner=cls.user) cls.Collection1.save() nii_path = os.path.join( cls.test_path, cls._map) map = save_statmap_form( image_path=nii_path, collection=cls.Collection1) save_resampled_transformation_single(map.pk) response = json.loads(cls.client.get("/images/%d/gene_expression/json?mask=full" % map.pk, follow=True).content) cls.df = pd.DataFrame(response["data"], columns=response["columns"])
def test_add_DOI(self): collection1 = Collection(name='Collection1', owner=self.u1, private=False) collection1.save() collection2 = Collection(name='Collection2', owner=self.u1, private=False) collection2.save() app_path = os.path.abspath(os.path.dirname(__file__)) image1 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/all.nii.gz'), collection=collection1, image_name = "image1") image2 = save_statmap_form(image_path=os.path.join(app_path,'test_data/statmaps/motor_lips.nii.gz'), collection=collection2, image_name = "image2") comparison = Comparison.objects.filter(image1=image1,image2=image2) self.assertEqual(len(comparison), 0) print "without DOI: %s"%Comparison.objects.all().count() collection = Collection.objects.get(pk=collection1.pk) collection.DOI = '10.3389/fninf.2015.00020' collection.save() print "with DOI: %s"%Comparison.objects.all().count() print collection.basecollectionitem_set.instance_of(Image).all() comparison = Comparison.objects.filter(image1=image1,image2=image2) self.assertEqual(len(comparison), 1)
def setUpClass(cls): cls.test_path = os.path.abspath(os.path.dirname(__file__)) cls.user, _ = User.objects.get_or_create(username='******') cls.client = Client() cls.client.login(username=cls.user) cls.Collection1 = Collection(name='Collection1', owner=cls.user) cls.Collection1.save() nii_path = os.path.join(cls.test_path, cls._map) map = save_statmap_form(image_path=nii_path, collection=cls.Collection1) save_resampled_transformation_single(map.pk) response = json.loads( cls.client.get("/images/%d/gene_expression/json" % map.pk, follow=True).content) cls.df = pd.DataFrame(response["data"], columns=response["columns"])
def test_thresholded_image_comparison(self): # There should be no comparisons for a thresholded image print "testing comparisons for thresholded images" assert_equal(count_existing_comparisons(self.pk3), 0) # There should be no comparisons for an atlas print "testing comparisons for atlases" assert_equal(count_existing_comparisons(self.pk1), 0) # There should be no comparisons for statistical map because no other statistical maps print "testing comparisons for statistical maps" assert_equal(count_existing_comparisons(self.pk2), 0) # Add another statistical map image_path = os.path.join(self.app_path, 'test_data/statmaps/motor_lips.nii.gz') image4 = save_statmap_form(image_path=image_path, collection=self.comparisonCollection4) self.pk4 = image4.id # There should STILL be no comparisons for a thresholded image print "testing comparisons for thresholded images" assert_equal(count_existing_comparisons(self.pk3), 0) # There should STILL be no comparisons for an of the atlas print "testing comparisons for atlases" assert_equal(count_existing_comparisons(self.pk1), 0) # There should now be one comparison for each statistical map, two total print "testing comparisons for statistical maps" print Comparison.objects.all() assert_equal(count_existing_comparisons(self.pk2), 1) assert_equal(count_existing_comparisons(self.pk4), 1) # This is the call that find_similar users to get images comparisons = get_existing_comparisons(self.pk4) for comp in comparisons: pk1 = comp.image1.pk pk2 = comp.image2.pk im1 = Image.objects.get(pk=pk1) im2 = Image.objects.get(pk=pk2) assert_equal(im1.is_thresholded, False) assert_equal(im2.is_thresholded, False)
def testDownloadCollection_NIDM_results(self): collection = Collection(owner=self.user, name="Collection2") collection.save() # Upload NIMDResult zip file zip_file = open( os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'), 'rb') post_dict = { 'name': 'auditory', 'description': '{0} upload test'.format('spm_auditory_v1.2.0'), 'collection': collection.pk, } fname = os.path.basename( os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip')) file_dict = {'zip_file': SimpleUploadedFile(fname, zip_file.read())} form = NIDMResultsForm(post_dict, file_dict) form.save() # Upload Statistic Map image = save_statmap_form(image_path=os.path.join( TEST_PATH, 'test_data/statmaps/all.nii.gz'), collection=collection, image_name="all.nii.gz") factory = RequestFactory() self.client.login(username=self.user) request = factory.get('/collections/%s/download' % collection.pk, {'format': 'img.zip'}) request.user = self.user response = download_collection(request, str(collection.pk)) self.assertTrue(response.streaming_content) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), "attachment; filename=" + collection.name + ".zip") zf = zipfile.ZipFile(io.BytesIO(''.join(response.streaming_content))) self.assertEqual(len(zf.filelist), 2) # 1 NIDMResult, 1 Statmap self.assertIsNone(zf.testzip()) self.assertIn("Collection2/all.nii.gz", zf.namelist()) self.assertIn("Collection2/auditory.nidm.zip", zf.namelist())
def test_thresholded_image_comparison(self): # There should be no comparisons for a thresholded image print "testing comparisons for thresholded images" assert_equal(0,count_existing_comparisons(self.pk3)) # There should be no comparisons for an atlas print "testing comparisons for atlases" assert_equal(0,count_existing_comparisons(self.pk1)) # There should be no comparisons for statistical map because no other statistical maps print "testing comparisons for statistical maps" assert_equal(0,count_existing_comparisons(self.pk2)) # Add another statistical map image_path = os.path.join(self.app_path,'test_data/statmaps/motor_lips.nii.gz') image4 = save_statmap_form(image_path=image_path,collection = self.comparisonCollection) self.pk4 = image4.id # There should STILL be no comparisons for a thresholded image print "testing comparisons for thresholded images" assert_equal(0,count_existing_comparisons(self.pk3)) # There should STILL be no comparisons for an of the atlas print "testing comparisons for atlases" assert_equal(0,count_existing_comparisons(self.pk1)) # There should now be one comparison for each statistical map, two total print "testing comparisons for statistical maps" print Comparison.objects.all() assert_equal(1,count_existing_comparisons(self.pk2)) assert_equal(1,count_existing_comparisons(self.pk4)) assert_equal(1,count_existing_comparisons()) # This is the call that find_similar users to get images comparisons = get_existing_comparisons(self.pk4) for comp in comparisons: pk1=comp.image1.pk pk2=comp.image2.pk im1 = Image.objects.get(pk=pk1) im2 = Image.objects.get(pk=pk2) assert_equal(im1.is_thresholded,False) assert_equal(im2.is_thresholded,False)
def testDownloadCollection_NIDM_results(self): collection = Collection(owner=self.user, name="Collection2") collection.save() # Upload NIMDResult zip file zip_file = open(os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip'), 'rb') post_dict = { 'name': 'auditory', 'description': '{0} upload test'.format('spm_auditory_v1.2.0'), 'collection': collection.pk, } fname = os.path.basename(os.path.join(TEST_PATH, 'test_data/nidm/auditory.nidm.zip')) file_dict = { 'zip_file': SimpleUploadedFile(fname, zip_file.read())} form = NIDMResultsForm(post_dict, file_dict) form.save() # Upload Statistic Map image = save_statmap_form(image_path=os.path.join(TEST_PATH, 'test_data/statmaps/all.nii.gz'), collection=collection, image_name="all.nii.gz") factory = RequestFactory() self.client.login(username=self.user) request = factory.get('/collections/%s/download' % collection.pk, {'format': 'img.zip'}) request.user = self.user response = download_collection(request, str(collection.pk)) self.assertTrue(response.streaming_content) self.assertEqual(response.status_code, 200) self.assertEqual(response.get('Content-Disposition'), "attachment; filename=" + collection.name + ".zip") zf = zipfile.ZipFile(io.BytesIO(''.join(response.streaming_content))) self.assertEqual(len(zf.filelist),2) # 1 NIDMResult, 1 Statmap self.assertIsNone(zf.testzip()) self.assertIn("Collection2/all.nii.gz", zf.namelist()) self.assertIn("Collection2/auditory.nidm.zip", zf.namelist())
def handle(self, *args, **options): clearDB() app_path = '/code/neurovault/apps/statmaps/tests/bench' u1 = User.objects.create(username='******') dict_feat = {} for i, file in enumerate(os.listdir(os.path.join(app_path, 'images/'))): #print 'Adding subject ' + file randomCollection = Collection(name='random' + file, owner=u1, DOI='10.3389/fninf.2015.00008' + str(i)) randomCollection.save() t = Timer() with t: image = save_statmap_form( image_path=os.path.join(app_path, 'images/', file), collection=randomCollection, image_name=file, ignore_file_warning=True) dict_feat[i] = image.pk print i if i == 939: pickle.dump(dict_feat, open('/code/neurovault/apps/statmaps/tests/dict_feat_localhost.p', "wb")) break