Coverage for python/lsst/sims/maf/db/trackingDb.py : 50%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Define contents and format of run list table.
Table to list all available MAF results, along with their opsim run and some comment info. """ # Define columns in metric list table. "opsimVersion='%s', opsimDate='%s', mafComment='%s', " \ "mafVersion='%s', mafDate='%s', mafDir='%s', dbFile='%s'>" \ % (self.mafRunId, self.opsimGroup, self.opsimRun, self.opsimComment, self.opsimVersion, self.opsimDate, self.mafComment, self.mafVersion, self.mafDate, self.mafDir, self.dbFile)
trackingDbverbose=False): """ Instantiate the results database, creating metrics, plots and summarystats tables. """ # Connect to database # for sqlite, connecting to non-existent database creates it automatically # Default is a file in the current directory. self.database = os.path.join(os.getcwd(), 'trackingDb_sqlite.db') self.driver = 'sqlite' else:
else: dbAddress = url.URL(self.driver, username=DbAuth.username(self.host, str(self.port)), password=DbAuth.password(self.host, str(self.port)), host=self.host, port=self.port, database=self.database)
print('Created or connected to MAF tracking %s database at %s' %(self.driver, self.database)) # Create the tables, if they don't already exist. except DatabaseError: raise DatabaseError("Cannot create a %s database at %s. Check directory exists." %(self.driver, self.database))
mafComment=None, mafVersion=None, mafDate=None, mafDir=None, dbFile=None, mafRunId=None): """Add a run to the tracking database.
Parameters ---------- opsimGroup : str, opt Set a name to group this run with (eg. "Tier 1, 2016"). opsimRun : str, opt Set a name for the opsim run. opsimComment : str, opt Set a comment describing the opsim run. opsimVersion : str, opt Set the version of opsim. opsimDate : str, opt Set the date the opsim run was created. mafComment : str, opt Set a comment to describe the MAF analysis. mafVersion : str, opt Set the version of MAF used for analysis. mafDate : str, opt Set the date the MAF analysis was run. mafDir : str, opt The relative path to the MAF directory. dbFile : str, opt The relative path to the Opsim SQLite database file. mafRunId : int, opt The MafRunID to assign to this record in the database (note this is a primary key!). If this run (ie the mafDir) exists in the database already, this will be ignored.
Returns ------- int The mafRunID stored in the database. """ mafDir = 'NULL' # Test if mafDir already exists in database. % (mafDir, runIds)) opsimComment=opsimComment, opsimVersion=opsimVersion, opsimDate=opsimDate, mafComment=mafComment, mafVersion=mafVersion, mafDate=mafDate, mafDir=mafDir, dbFile=dbFile) else: # Check if mafRunId exists already. existing = self.session.query(RunRow).filter_by(mafRunId=mafRunId).all() if len(existing) > 0: raise ValueError('MafRunId %d already exists in database, for %s. ' \ 'Record must be deleted first.' % (mafRunId, existing[0].mafDir)) runinfo = RunRow(mafRunId=mafRunId, opsimGroup=opsimGroup, opsimRun=opsimRun, opsimComment=opsimComment, opsimVersion=opsimVersion, opsimDate=opsimDate, mafComment=mafComment, mafVersion=mafVersion, mafDate=mafDate, mafDir=mafDir, dbFile=dbFile) else: opsimComment=opsimComment, opsimVersion=opsimVersion, opsimDate=opsimDate, mafComment=mafComment, mafVersion=mafVersion, mafDate=mafDate, mafDir=mafDir, dbFile=dbFile)
""" Remove a run from the tracking database. """ raise Exception('Found more than one run with mafRunId %d' %(runId))
opsimRun=None, opsimComment=None, mafComment=None, dbFile=None): """Adds information about a MAF analysis run to a MAF tracking database.
Parameters ---------- mafDir : str Path to the directory where the MAF results are located. trackingDb : str or lsst.sims.maf.TrackingDb Full filename (+path) to the tracking database storing the MAF run information or a TrackingDb object. opsimGroup: str, opt Name to use to group this run with other opsim runs. Default None. opsimRun : str, opt Name of the opsim run. If not provided, will attempt to use runName from confSummary.txt. opsimComment : str, opt Comment about the opsim run. If not provided, will attempt to use runComment from confSummary.txt. mafComment : str, opt Comment about the MAF analysis. If not provided, no comment will be recorded. dbFile : str, opt Relative path + name of the opsim database file. If not provided, no location will be recorded. """ mafDir = os.path.abspath(mafDir) if not os.path.isdir(mafDir): raise ValueError('There is no directory containing MAF outputs at %s.' % (mafDir))
trackingDb = TrackingDb(database=trackingDbFile) autoOpsimRun = None autoOpsimComment = None opsimVersion = None opsimDate = None mafVersion = None mafDate = None if os.path.isfile(os.path.join(mafDir, 'configSummary.txt')): file = open(os.path.join(mafDir, 'configSummary.txt')) for line in file: tmp = line.split() if tmp[0].startswith('RunName'): autoOpsimRun = ' '.join(tmp[1:]) if tmp[0].startswith('RunComment'): autoOpsimComment = ' '.join(tmp[1:]) # MAF Date may be in a line with "MafDate" (new configs) # or at the end of "MAFVersion" (old configs). if tmp[0].startswith('MAFDate'): mafDate = tmp[-1] if tmp[0].startswith('MAFVersion'): mafVersion = tmp[1] if len(tmp) > 2: mafDate = tmp[-1] if tmp[0].startswith('OpsimDate'): opsimDate = tmp[-1] if len(tmp) > 2: opsimDate = tmp[-2] if tmp[0].startswith('OpsimVersion'): opsimVersion = tmp[1] if len(tmp) > 2: opsimDate = tmp[-2] # And convert formats to '-' (again, multiple versions of configs). if mafDate is not None: if len(mafDate.split('/')) > 1: t = mafDate.split('/') if len(t[2]) == 2: t[2] = '20' + t[2] mafDate = '-'.join([t[2], t[1], t[0]]) if opsimDate is not None: if len(opsimDate.split('/')) > 1: t = opsimDate.split('/') if len(t[2]) == 2: t[2] = '20' + t[2] opsimDate = '-'.join([t[2], t[1], t[0]])
if opsimRun is None: opsimRun = autoOpsimRun if opsimComment is None: opsimComment = autoOpsimComment
print('Adding to tracking database at %s:' % (trackingDbFile)) print(' MafDir = %s' % (mafDir)) print(' MafComment = %s' % (mafComment)) print(' OpsimGroup = %s' % (opsimGroup)) print(' OpsimRun = %s' % (opsimRun)) print(' OpsimComment = %s' % (opsimComment)) print(' OpsimVersion = %s' % (opsimVersion)) print(' OpsimDate = %s' % (opsimDate)) print(' MafVersion = %s' % (mafVersion)) print(' MafDate = %s' % (mafDate)) print(' Opsim dbFile = %s' % (dbFile)) runId = trackingDb.addRun(opsimGroup=opsimGroup, opsimRun=opsimRun, opsimComment=opsimComment, opsimVersion=opsimVersion, opsimDate=opsimDate, mafComment=mafComment, mafVersion=mafVersion, mafDate=mafDate, mafDir=mafDir, dbFile=dbFile) print('Used MAF RunID %d' % (runId)) trackingDb.close() |