def load_single_dynamic_attr(scan, atlasobj, attrname, dynamic_conf, rootFolder = rootconfig.path.feature_root): """ Return a DynamicAttr (attr.data[tickIdx, timeIdx]) """ if type(atlasobj) is str: atlasobj = atlas.get(atlasobj) window_length = dynamic_conf[0] step_size = dynamic_conf[1] # fix dynamic attr feature_name issue if attrname.find('bc') != -1 or attrname.find('BC') != -1: feature_name = 'BOLD.BC.inter' elif attrname.find('ccfs') != -1 or attrname.find('CCFS') != -1: feature_name = 'BOLD.CCFS.inter' elif attrname.find('le') != -1 or attrname.find('LE') != -1: feature_name = 'BOLD.LE.inter' elif attrname.find('wd') != -1 or attrname.find('WD') != -1: feature_name = 'BOLD.WD.inter' else: raise Exception('Unknown feature_name %s' % attrname) dynamic_attr = netattr.DynamicAttr(None, atlasobj, window_length, step_size, scan = scan, feature_name = feature_name) start = 0 dynamic_foler_path = os.path.join(rootFolder, scan, atlasobj.name, 'bold_net_attr', 'dynamic %d %d' % (step_size, window_length)) while True: dynamic_attr_filepath = os.path.join(dynamic_foler_path, '%s-%d.%d.csv' % (attrname, start, start + window_length)) if os.path.exists(dynamic_attr_filepath): dynamic_attr.append_one_slice(load_csvmat(dynamic_attr_filepath)) start += step_size else: # print('loaded %d attrs for %s' % (len(ret[scan]), scan)) break return dynamic_attr
def trans_dynamic_netattr(self, subject_scan, atlas_name, feature_name, window_length, step_size, value): if value.ndim == 2: # 这里要改一下 arr = netattr.DynamicAttr(value.swapaxes(0,1), atlas.get(atlas_name), window_length, step_size, subject_scan, feature_name) return arr else: net = netattr.DynamicNet(value.swapaxes(0,2).swapaxes(0,1), atlas.get(atlas_name), window_length, step_size, subject_scan, feature_name) return net
def get_dynamic_attr(self, scan, atlas_name, feature, window_length, step_size, comment={}): """ Return to dynamic attr object directly """ query = dict(scan=scan, comment=comment) col = self.getcol(atlas_name, feature, window_length, step_size) count = self.dadb[col].find(query) if count == 0: raise NoRecordFoundException(scan + atlas_name + feature) else: records = self.dadb[col].find(query).sort([('slice', pymongo.ASCENDING)]) atlasobj = atlas.get(atlas_name) attr = netattr.DynamicAttr(None, atlasobj, window_length, step_size, scan, feature) for record in records: attr.append_one_slice(pickle.loads(record['value'])) return attr
def run_feature(self, feature_name, feature_config): """ Override super run_feature. Stores csv files to MongoDB directly """ if feature_config['file_type'] != '.csv': # only supports csv features return in_file_list, out_file_list = self.get_feature_file_path(feature_config) if self.is_dynamic and feature_config['modal'] == 'BOLD': if len(in_file_list) < 1: print('==Not Exist:', self.mriscan, self.atlasname, feature_name) return if feature_name.find('net') != -1: feature = netattr.DynamicNet(None, self.atlasname, self.dataconfig['dynamic']['window_length'], self.dataconfig['dynamic']['step_size'], scan = self.mriscan, feature_name = feature_name) for file in in_file_list: feature.append_one_slice(load_csvmat(file)) try: self.mdb.save_dynamic_network(feature) except mongodb_database.MultipleRecordException: if self.force: # delete and overwrite self.mdb.remove_dynamic_network(self.mriscan, self.dataconfig['dynamic']['window_length'], self.dataconfig['dynamic']['step_size'], self.atlasname) self.mdb.save_dynamic_network(feature) else: print('!!!Already Exist: %s %s %s. Skipped' % (self.mriscan, self.atlasname, feature_name)) else: feature = netattr.DynamicAttr(None, self.atlasname, self.dataconfig['dynamic']['window_length'], self.dataconfig['dynamic']['step_size'], scan = self.mriscan, feature_name = feature_name) for file in in_file_list: feature.append_one_slice(load_csvmat(file)) try: self.mdb.save_dynamic_attr(feature) except mongodb_database.MultipleRecordException: if self.force: # delete and overwrite self.mdb.remove_dynamic_attr(self.mriscan, feature_name, self.dataconfig['dynamic']['window_length'], self.dataconfig['dynamic']['step_size'], self.atlasname) self.mdb.save_dynamic_attr(feature) else: print('!!!Already Exist: %s %s %s. Skipped' % (self.mriscan, self.atlasname, feature_name)) elif self.is_dynamic: # dynamic but not BOLD feature return else: # not dynamic for file in in_file_list: if not os.path.isfile(file): print('==Not Exist:', self.mriscan, self.atlasname, feature_name) continue if feature_name.find('net') != -1: feature = netattr.Net(load_csvmat(file), self.atlasname, self.mriscan, feature_name) else: feature = netattr.Attr(load_csvmat(file), self.atlasname, self.mriscan, feature_name) try: self.mdb.save_static_feature(feature) except mongodb_database.MultipleRecordException: if self.force: # delete and overwrite self.mdb.remove_static_feature(self.mriscan, self.atlasname, feature_name) self.mdb.save_static_feature(feature) else: print('!!!Already Exist: %s %s %s. Skipped' % (self.mriscan, self.atlasname, feature_name))