figureSavePath = '/media/onerva/KINGSTON/test-data/outcome/random-vs-brainnetome.pdf' equalSized = False nIterations = 10 nROIs = 246 allVoxelTs = io.loadmat(allVoxelTsPath)['roi_voxel_data'][0]['roi_voxel_ts'][0] imgdata = nib.load(imgdataPath).get_data() groupMaskPath = '/media/onerva/KINGSTON/test-data/group_roi_mask-30-4mm_with_subcortl_and_cerebellum.nii' groupMask = nib.load(groupMaskPath).get_data() nTime = imgdata.shape[3] for i in range(nTime): imgdata[:,:,:,i] = imgdata[:,:,:,i] * groupMask _,_,originalVoxelCoordinates,originalROIMaps = cbc.readROICentroids(ROIInfoFile,readVoxels=True,fixCentroids=True) originalROIs = [] for ROIMap in originalROIMaps: ROI = [] for voxel in ROIMap: voxelIndex = np.where((originalVoxelCoordinates == voxel).all(axis=1)==1)[0][0] if len(cbc.findNeighbors(voxel,allVoxels=originalVoxelCoordinates)) > 0: ROI.append(voxelIndex) originalROIs.append(ROI) VIs = np.zeros(nIterations) for i in range(nIterations): ROILabels, voxelCoordinates, radius = cbc.growSphericalROIs('random', imgdata, nROIs=nROIs, template=groupMask, equalSized=equalSized) #import pdb; pdb.set_trace() randomROIs = [list(np.where(ROILabels == j)[0]) for j in range(nROIs)]
import nibabel as nib import clustering_by_consistency as cbc testSubjectFolders = ['/media/onerva/KINGSTON/test-data/010/', '/media/onerva/KINGSTON/test-data/011/'] ROIVoxelTsFileName = 'roi_voxel_ts_all_rois4mm_FWHM0.mat' ROIVoxelTsInfoFileName = 'roi_voxel_ts_all_rois4mm_WHM0_info.mat' allVoxelTsPath = testSubjectFolders[0] + ROIVoxelTsFileName ROIInfoFile = '/media/onerva/KINGSTON/test-data/group_roi_mask-30-4mm_with_subcortl_and_cerebellum.mat' imgdataPath = testSubjectFolders[0] + 'epi_STD_mask_detrend_fullreg.nii' niiSavePath = testSubjectFolders[0] + '/optimized-rois-test-for-Tarmo-mean-weighted-consistency-thresholded-voxel-neighborhood.nii' pickleSavePath = testSubjectFolders[0] + '/optimized-rois-test-for-Tarmo-mean-weighted-consistency-thresholded-voxel-neighborhood.pkl' ROICentroids,_,voxelCoordinates,_ = cbc.readROICentroids(ROIInfoFile,readVoxels=True,fixCentroids=True) allVoxelTs = io.loadmat(allVoxelTsPath)['roi_voxel_data'][0]['roi_voxel_ts'][0] imgdata = nib.load(imgdataPath).get_data() groupMaskPath = '/media/onerva/KINGSTON/test-data/group_roi_mask-30-4mm_with_subcortl_and_cerebellum.nii' groupMask = nib.load(groupMaskPath).get_data() nTime = imgdata.shape[3] for i in range(nTime): imgdata[:,:,:,i] = imgdata[:,:,:,i] * groupMask cfg = {} cfg['ROICentroids'] = ROICentroids cfg['voxelCoordinates'] = voxelCoordinates cfg['names'] = '' cfg['allVoxelTs'] = allVoxelTs cfg['imgdata'] = imgdata
testUpdateROI = False testGrowROIs = False testFindROICentroids = False testRemoveVoxel = False testFindVoxelLabels = False testFindROIBoundary = False testVoxelLabelsToROIInfo = False testVoxelsInClustersToROIInfo = False testFindInROINeighbors = False testIsBoundary = False testGetKendallW = True # testing defSphericalROIs if testDefSphericalROIs: ROIPath = 'atlases/brainnetome/brainnetome_thr25_4mm_rois.mat' centroids, _, voxelCoords, _ = cbc.readROICentroids(ROIPath, readVoxels=True) # testcase 1: ROIs with radius < 1 should contain only ROI centroids spheres = cbc.defineSphericalROIs(centroids, voxelCoords, 0) sphereMaps = [] for sphereMap in spheres['ROIMaps']: sphereMaps.append(sphereMap) sphereMaps = np.array(sphereMaps) diff = np.sum(np.abs(sphereMaps - centroids)) if diff == 0: print("defSphericalROIs: testcase 1/3 OK") # testcase 2: small example space space = np.zeros((4, 4, 4))
for i, (clusteringMethod, tempalateNamesPerMethod, consistencySaveName) in enumerate(zip(clusteringMethods,templateNames,consistencySaveNames)): for j, templateName in enumerate(templateNamesPerMethod): for timeWindow in timeWindows: consistencyPath = consistencySaveName + '_' + timeWindow + '.pkl' f = open(consistencyPath, 'r') data = pickle.load(f) f.close() if 'consistencies' in data.keys(): pooledConsistencies[i][j].extend(data['consistencies']) else: pooledConsistencies[i][j].extend(data['spatialConsistencies']) if not excludeSizes: if 'ROI_sizes' in data.keys(): ROISizes[i][j].extend(data['ROI_sizes']) elif clusteringMethod == 'template': # TODO: this is a hack to be removed later originalCentroids,_,originalVoxelCoordinates,originalROIMaps = cbc.readROICentroids('/media/onerva/KINGSTON/test-data/group_roi_mask-30-4mm_with_subcortl_and_cerebellum.mat',readVoxels=True,fixCentroids=True) ROISizes[i][j].extend([len(ROIMap) for ROIMap in originalROIMaps]) else: for i, (clusteringMethod, templateNamesPerMethod, netIdentificatorsPerMethod) in enumerate(zip(clusteringMethods,templateNames,netIdentificators)): for j, (templateName, netIdentificatorsPerTemplate) in enumerate(zip(templateNamesPerMethod,netIdentificatorsPerMethod)): savePath = subjectFolder + '/spatialConsistency_' + clusteringMethod + '_' + templateName + '.pkl' f = open(savePath,'r') spatialConsistencyData = pickle.load(f) f.close() ROISizes[i][j].extend(spatialConsistencyData['roi_sizes']) pooledConsistencies[i][j].extend(spatialConsistencyData['spatial_consistencies']) # calculating and visualizing consistency distributions consFig = plt.figure(1) consAx = consFig.add_subplot(111)
data, statistic='count', bins=nBins) pdf = count / float(np.sum(count) * (binEdges[1] - binEdges[0])) binCenters = 0.5 * (binEdges[:-1] + binEdges[1:]) return pdf, binCenters fig = plt.figure(1) ax1 = fig.add_subplot(111) sizeFig = plt.figure(2) sizeAx = sizeFig.add_subplot(111) originalCentroids, _, originalVoxelCoordinates, originalROIMaps = cbc.readROICentroids( originalROIInfoFile, readVoxels=True, fixCentroids=True) centroidIndices = np.zeros(len(originalCentroids), dtype=int) for i, originalCentroid in enumerate(originalCentroids): centroidIndices[i] = np.where( (originalVoxelCoordinates == originalCentroid).all(axis=1) == 1)[0][0] for i, subject in enumerate(subjects): optimizedROIInfoPath = subject + optimizedROIInfoFile + '.pkl' tempPath = subject + optimizedROIInfoFile + '_temp.pkl' allVoxelTsPath = subject + allVoxelTsFileName allVoxelTs = io.loadmat( allVoxelTsPath)['roi_voxel_data'][0]['roi_voxel_ts'][0] f = open(optimizedROIInfoPath, "rb") ROIInfo = pickle.load(f) f.close()