def newGroupByNames(self, groupName, nameList, scanNum, desc=None, accumulateScan=False): """ Initialize a group by a list of names. The scans are generated automatically. scanNum - which scan (first/second/etc) accumulateScan - whether keep former scans in this group """ session = self.db.new_session() group = tables.Group(name=groupName, description=desc) try: session.query(tables.Group).filter_by(name=groupName).one() except sqlalchemy.orm.exc.NoResultFound: for name in nameList: db_person = session.query( tables.Person).filter_by(name=name).one() group.people.append(db_person) if accumulateScan: group.scans += sorted(db_person.mriscans, key=lambda x: x.filename)[:scanNum] else: group.scans.append( sorted(db_person.mriscans, key=lambda x: x.filename)[scanNum - 1]) session.add(group) session.commit() return except sqlalchemy.orm.exc.MultipleResultsFound: # more than one record found raise Exception("More than one %s group found!" % groupName) # found one existing record raise Exception("%s group already exist" % groupName)
def main(): dbfile = 'E:/mmdpdb.db' engine = create_engine('sqlite:///' + dbfile) sessionManager = sessionmaker(bind=engine) session = sessionManager() db_group = tables.Group(name='test', description='test desc') session.add(db_group) names = loadsave.load_txt( 'E:/Changgung works/20180409_tDCS_14/score_info/control_namelist.txt') for name in names: person = session.query(tables.Person).filter_by(name=name).one() db_group.people.append(person) scans = loadsave.load_txt( 'E:/Changgung works/20180409_tDCS_14/score_info/control_scanlist-2.txt' ) for scan in scans: db_scan = session.query(tables.MRIScan).filter_by(filename=scan).one() db_group.scans.append(db_scan) session.commit()
def newGroupByNamesAndScans(self, groupName, nameList, scanList, desc=None): """ Initialize a group by giving both name and scans """ session = self.db.new_session() group = tables.Group(name=groupName, description=desc) for name in nameList: db_person = session.query(tables.Person).filter_by(name=name).one() group.people.append(db_person) for scan in scanList: db_scan = session.query( tables.MRIScan).filter_by(filename=scan).one() group.scans.append(db_scan) session.add(group) session.commit()
def new_group_by_scans(self, group_name, scan_list, desc=None): """ Initialize a group by a list of scans """ group = tables.Group(name=group_name, description=desc) # check if group already exist try: self.session.query(tables.Group).filter_by(name=group_name).one() except NoResultFound: # alright for scan in scan_list: db_scan = self.session.query( tables.MRIScan).filter_by(filename=scan).one() group.scans.append(db_scan) group.people.append(db_scan.person) self.session.add(group) self.session.commit() return except MultipleResultsFound: # more than one record found raise Exception("More than one %s group found!" % group_name) # found one existing record raise Exception("%s group already exist" % group_name)