def check_lesion_tract_overlaps(tracts): #cached_data = current_app.cache.get(cache_key) cache = JobCache(current_app.cache, current_app.cache_lock) for tract in tracts: # average density maps for this tract based on current query # save averaged map and cache the file path status = cache.job_status(cache_key, tract.code) if status == 'COMPLETE': # get file path from cache tract_file_path = cache.job_result(cache_key, tract.code) else: # recalculate density map file_path_data = dbu.density_map_file_path_data(request_query) if len(file_path_data) > 0: current_app.logger.info(f'Adding job {tract.code}') cache.add_job(cache_key, tract.code) data_dir = current_app.config[ 'DATA_FILE_PATH'] # file path to data folder tract_file_path = du.generate_average_density_map( data_dir, file_path_data, tract, 'MNI') cache.job_complete(cache_key, tract.code, tract_file_path) current_app.logger.info(f'Job {tract.code} complete') else: current_app.logger.info( f'No subjects returned for query {json.dumps(request_query, indent=4)}' ) return 'No subjects returned for the current query', 404 # # perform weighted overlap: lesion * tract # tract_data = du.get_nifti_data(tract_file_path) # overlap = lesion_data * tract_data # # weighted sum of voxels occupied by overlap # # figure out percentage of tract overlapping with lesion # overlap_sum = np.sum(overlap) # if overlap_sum: # overlap_score = overlap_sum / np.sum(tract_data) # # add dict to intersecting_tracts list # intersecting_tracts.append({"tractCode": tract.code, "overlapScore": overlap_score}) # alternative overlap score tract_data = du.get_nifti_data(tract_file_path) masked_tract_data = ma.masked_where(tract_data < threshold, tract_data) overlap = lesion_data * masked_tract_data over_threshold_count = masked_tract_data.count() over_threshold_overlap_count = len(overlap.nonzero()[0]) if over_threshold_overlap_count: overlap_percent = (over_threshold_overlap_count / over_threshold_count) * 100. # add dict to intersecting_tracts list intersecting_tracts.append({ "tractName": tract.name, "tractCode": tract.code, "overlapScore": overlap_percent, "description": tract.description })
def test_add_job(self): cache = CacheMock() lock = LockMock() job_cache = JobCache(cache, lock) # test when no jobs in cache for key job_cache.add_job(self.TEST_KEY, self.JOB1_KEY) assert cache.get(self.TEST_KEY).get( self.JOB1_KEY).get('status') == 'IN_PROGRESS' # test when jobs already exist for key but job doesn't exist job_cache.add_job(self.TEST_KEY, self.JOB2_KEY) assert cache.get(self.TEST_KEY).get( self.JOB2_KEY).get('status') == 'IN_PROGRESS' # test when completed job already exists (shouldn't change cached job) jobs = cache.get(self.TEST_KEY) jobs[self.JOB2_KEY]['status'] = 'COMPLETE' cache.set(self.TEST_KEY, jobs) job_cache.add_job(self.TEST_KEY, self.JOB2_KEY) assert cache.get(self.TEST_KEY).get( self.JOB2_KEY).get('status') == 'COMPLETE'