fermipy.jobs subpackage

fermipy.jobs.chain module

The Chain class inherits from Link and allow chaining together several applications into a single object.

class fermipy.jobs.chain.Chain(**kwargs)[source]

Bases: fermipy.jobs.link.Link

An object tying together a series of applications into a single application.

This class keep track of the arguments to pass to the applications as well as input and output files.

Note that this class is itself a Link. This allows you to write a python module that implements a chain and also has a __main__ function to allow it to be called from the shell.

“Check the status of all the jobs run from the Link objects in this Chain and return a status flag that summarizes that.

Parameters:
  • fail_running (bool) – If True, consider running jobs as failed
  • fail_pending (bool) – If True, consider pending jobs as failed
Returns:

status – Job status flag that summarizes the status of all the jobs,

Return type:

JobStatus

clear_jobs(recursive=True)[source]

Clear a dictionary with all the jobs

If recursive is True this will include jobs from all internal Link

get_jobs(recursive=True)[source]

Return a dictionary with all the jobs

If recursive is True this will include jobs from all internal Link

linknames

Return the name of the Link objects owned by this Chain

Return the OrderedDict of Link objects owned by this Chain

classmethod main()[source]

Hook to run this Chain from the command line

missing_input_files()[source]

Make and return a dictionary of the missing input files.

This returns a dictionary mapping filepath to list of Link that use the file as input.

missing_output_files()[source]

Make and return a dictionary of the missing output files.

This returns a dictionary mapping filepath to list of links that produce the file as output.

print_status(indent='', recurse=False)[source]

Print a summary of the job status for each Link in this Chain

print_summary(stream=<open file '<stdout>', mode 'w'>, indent='', recurse_level=2)[source]

Print a summary of the activity done by this Chain.

Parameters:
  • stream (file) – Stream to print to, must have ‘write’ method.
  • indent (str) – Indentation at start of line
  • recurse_level (int) – Number of recursion levels to print
run(stream=<open file '<stdout>', mode 'w'>, dry_run=False, stage_files=True, resubmit_failed=False)[source]

Runs this Chain.

Parameters:
  • stream (file) – Stream that this Link will print to, Must have ‘write’ function
  • dry_run (bool) – Print command but do not run it.
  • stage_files (bool) – Copy files to and from scratch staging area.
  • resubmit_failed (bool) – Flag for sub-classes to resubmit failed jobs.
run_analysis(argv)[source]

Implemented by sub-classes to run a particular analysis

update_args(override_args)[source]

Update the argument used to invoke the application

Note that this will also update the dictionary of input and output files.

Parameters:override_args (dict) – dictionary passed to the links
fermipy.jobs.chain.purge_dict(idict)[source]

Remove null items from a dictionary

fermipy.jobs.file_archive module

Classes and utilites to keep track of files associated to an analysis.

The main class is FileArchive, which keep track of all the files associated to an analysis.

The FileHandle helper class encapsulates information on a particular file.

class fermipy.jobs.file_archive.FileArchive(**kwargs)[source]

Bases: object

Class that keeps track of the status of files used in an analysis

Parameters:
base_path

Return the base file path for all files in this FileArchive

classmethod build_archive(**kwargs)[source]

Return the singleton FileArchive instance, building it if needed

cache

Return the transiet representation of this FileArchive

classmethod get_archive()[source]

Return the singleton FileArchive instance

get_file_ids(file_list, creator=None, status=0, file_dict=None)[source]

Get or create a list of file ids based on file names

Parameters:
  • file_list (list) – The paths to the file
  • creatror (int) – A unique key for the job that created these files
  • status (FileStatus) – Enumeration giving current status of files
  • file_dict (FileDict) – Mask giving flags set on this file
  • list of integers (Returns) –
get_file_paths(id_list)[source]

Get a list of file paths based of a set of ids

Parameters:
  • id_list (list) – List of integer file keys
  • list of file paths (Returns) –
get_handle(filepath)[source]

Get the FileHandle object associated to a particular file

register_file(filepath, creator, status=0, flags=0)[source]

Register a file in the archive.

If the file already exists, this raises a KeyError

Parameters:
  • filepath (str) – The path to the file
  • creatror (int) – A unique key for the job that created this file
  • status (FileStatus) – Enumeration giving current status of file
  • flags (FileFlags) – Enumeration giving flags set on this file
  • FileHandle (Returns) –
table

Return the persistent representation of this FileArchive

table_file

Return the path to the file used to persist this FileArchive

update_file(filepath, creator, status)[source]

Update a file in the archive

If the file does not exists, this raises a KeyError

Parameters:
  • filepath (str) – The path to the file
  • creatror (int) – A unique key for the job that created this file
  • status (FileStatus) – Enumeration giving current status of file
  • FileHandle (Returns) –
update_file_status()[source]

Update the status of all the files in the archive

write_table_file(table_file=None)[source]

Write the table to self._table_file

class fermipy.jobs.file_archive.FileDict(**kwargs)[source]

Bases: object

Small class to keep track of files used & createed by a link.

Parameters:
  • file_args (dict) – Dictionary mapping argument to FileFlags enum
  • file_dict (dict) – Dictionary mapping file path to FileFlags enum
chain_input_files

Return a list of the input files needed by this chain.

For Link sub-classes this will return only those files that were not created by any internal Link

chain_output_files

Return a list of the all the output files produced by this link.

For Link sub-classes this will return only those files that were not marked as internal files or marked for removal.

gzip_files

Return a list of the files compressed by this link.

This returns all files that were explicitly marked for compression.

input_files

Return a list of the input files needed by this link.

For Link sub-classes this will return the union of all the input files of each internal Link.

That is to say this will include files produced by one Link in a Chain and used as input to another Link in the Chain

input_files_to_stage

Return a list of the input files needed by this link.

For Link sub-classes this will return the union of all the input files of each internal Link.

That is to say this will include files produced by one Link in a Chain and used as input to another Link in the Chain

internal_files

Return a list of the intermediate files produced by this link.

This returns all files that were explicitly marked as internal files.

items()[source]

Return iterator over self.file_dict

latch_file_info(args)[source]

Extract the file paths from a set of arguments

output_files

Return a list of the output files produced by this link.

For Link sub-classes this will return the union of all the output files of each internal Link.

That is to say this will include files produced by one Link in a Chain and used as input to another Link in the Chain

output_files_to_stage

Return a list of the input files needed by this link.

For Link sub-classes this will return the union of all the input files of each internal Link.

That is to say this will include files produced by one Link in a Chain and used as input to another Link in the Chain

print_chain_summary(stream=<open file '<stdout>', mode 'w'>, indent='')[source]

Print a summary of the files in this file dict.

This version uses chain_input_files and chain_output_files to count the input and output files.

print_summary(stream=<open file '<stdout>', mode 'w'>, indent='')[source]

Print a summary of the files in this file dict.

This version explictly counts the union of all input and output files.

temp_files

Return a list of the temporary files produced by this link.

This returns all files that were explicitly marked for removal.

update(file_dict)[source]

Update self with values from a dictionary mapping file path [str] to FileFlags enum

class fermipy.jobs.file_archive.FileFlags[source]

Bases: object

Bit masks to indicate file types

gz_mask = 8
in_ch_mask = 23
in_stage_mask = 33
input_mask = 1
internal_mask = 16
no_flags = 0
out_ch_mask = 22
out_stage_mask = 34
output_mask = 2
rm_mask = 4
rmint_mask = 20
stageable = 32
class fermipy.jobs.file_archive.FileHandle(**kwargs)[source]

Bases: object

Class to keep track of infomration about a file file.

Parameters:
  • key (int) – Unique id for this particular file
  • creator (int) – Unique id for the job that created this file
  • timestamp (int) – File creation time cast as an int
  • status (FileStatus) – Enum giving current status of file
  • flags (FileFlags) – Mask giving flags set on this file
  • path (str) – Path to file
append_to_table(table)[source]

Add this instance as a row on a astropy.table.Table

check_status(basepath=None)[source]

Check on the status of this particular file

classmethod create_from_row(table_row)[source]

Build and return a FileHandle from an astropy.table.row.Row

classmethod make_dict(table)[source]

Build and return a dict of FileHandle from an astropy.table.Table

The dictionary is keyed by FileHandle.key, which is a unique integer for each file

static make_table(file_dict)[source]

Build and return an astropy.table.Table to store FileHandle

update_table_row(table, row_idx)[source]

Update the values in an astropy.table.Table for this instances

class fermipy.jobs.file_archive.FileStageManager(scratchdir, workdir)[source]

Bases: object

Small class to deal with staging files to and from a scratch area

construct_scratch_path(dirname, basename)[source]

Construct and return a path in the scratch area.

This will be <self.scratchdir>/<dirname>/<basename>

static copy_from_scratch(file_mapping, dry_run=True)[source]

Copy output files from scratch area

static copy_to_scratch(file_mapping, dry_run=True)[source]

Copy input files to scratch area

get_scratch_path(local_file)[source]

Construct and return a path in the scratch area from a local file.

static make_scratch_dirs(file_mapping, dry_run=True)[source]

Make any directories need in the scratch area

map_files(local_files)[source]

Build a dictionary mapping local paths to scratch paths.

Parameters:
  • local_files (list) – List of filenames to be mapped to scratch area
  • dict (Returns) – Mapping local_file : fullpath of scratch file
split_local_path(local_file)[source]

Split the local path into a directory name and a file name

If local_file is in self.workdir or a subdirectory of it, the directory will consist of the relative path from workdir.

If local_file is not in self.workdir, directory will be empty.

Returns (dirname, basename)

class fermipy.jobs.file_archive.FileStatus[source]

Bases: object

Enumeration of file status types

exists = 2
expected = 1
missing = 3
no_file = 0
superseded = 4
temp_removed = 5
fermipy.jobs.file_archive.get_timestamp()[source]

Get the current time as an integer

fermipy.jobs.file_archive.get_unique_match(table, colname, value)[source]

Get the row matching value for a particular column. If exactly one row matchs, return index of that row, Otherwise raise KeyError.

fermipy.jobs.file_archive.main_browse()[source]

Entry point for command line use for browsing a FileArchive

fermipy.jobs.job_archive module

Classes and utilites to keep track the various jobs that are running in an analysis pipeline.

The main class is JobArchive, which keep track of all the jobs associated to an analysis.

The JobDetails helper class encapsulates information on a instance of running a job.

class fermipy.jobs.job_archive.JobArchive(**kwargs)[source]

Bases: object

Class that keeps of all the jobs associated to an analysis.

Parameters:
  • table_file (str) – Path to the file used to persist this JobArchive
  • table (astropy.table.Table) – Persistent representation of this JobArchive
  • table_ids (astropy.table.Table) – Ancillary table with information about file ids
  • file_archive (FileArchive) – Archive with infomation about all this files used and produced by this analysis
classmethod build_archive(**kwargs)[source]

Return the singleton JobArchive instance, building it if needed

classmethod build_temp_job_archive()[source]

Build and return a JobArchive using defualt locations of persistent files.

cache

Return the transiet representation of this JobArchive

file_archive

Return the FileArchive with infomation about all the files used and produced by this analysis

classmethod get_archive()[source]

Return the singleton JobArchive instance

get_details(jobname, jobkey)[source]

Get the JobDetails associated to a particular job instance

make_job_details(row_idx)[source]

Create a JobDetails from an astropy.table.row.Row

register_job(job_details)[source]

Register a job in this JobArchive

Register a job in the JobArchive from a Link object

register_jobs(job_dict)[source]

Register a bunch of jobs in this archive

remove_jobs(mask)[source]

Mark all jobs that match a mask as ‘removed’

table

Return the persistent representation of this JobArchive

table_file

Return the path to the file used to persist this JobArchive

table_ids

Return the rpersistent epresentation of the ancillary info of this JobArchive

update_job(job_details)[source]

Update a job in the JobArchive

update_job_status(checker_func)[source]

Update the status of all the jobs in the archive

write_table_file(job_table_file=None, file_table_file=None)[source]

Write the table to self._table_file

class fermipy.jobs.job_archive.JobDetails(**kwargs)[source]

Bases: object

A simple structure to keep track of the details of each of the sub-proccess jobs.

Parameters:
  • dbkey (int) – A unique key to identify this job
  • jobname (str) – A name used to idenfity this job
  • jobkey (str) – A string to identify this instance of the job
  • appname (str) – The executable inovked to run the job
  • logfile (str) – The logfile for this job, may be used to check for success/ failure
  • job_config (dict) – A dictionrary with the arguments for the job
  • parent_id (int) – Unique key identifying the parent job
  • infile_ids (list of int) – Keys to identify input files to this job
  • outfile_ids (list of int) – Keys to identify output files from this job
  • rmfile_ids (list of int) – Keys to identify temporary files removed by this job
  • intfile_ids (list of int) – Keys to identify internal files
  • status (int) – Current job status, one of the enums above
append_to_tables(table, table_ids)[source]

Add this instance as a row on a astropy.table.Table

check_status_logfile(checker_func)[source]

Check on the status of this particular job using the logfile

classmethod create_from_row(table_row)[source]

Create a JobDetails from an astropy.table.row.Row

fullkey

Return the fullkey for this job fullkey = <jobkey>@<jobname>

get_file_ids(file_archive, creator=None, status=0)[source]

Fill the file id arrays from the file lists

Parameters:
  • file_archive (FileArchive) – Used to look up file ids
  • creator (int) – A unique key for the job that created these file
  • status (FileStatus) – Enumeration giving current status thse files
get_file_paths(file_archive, file_id_array)[source]

Get the full paths of the files used by this object from the the id arrays

Parameters:
  • file_archive (FileArchive) – Used to look up file ids
  • file_id_array (numpy.array) – Array that remaps the file indexes
classmethod make_dict(table)[source]

Build a dictionary map int to JobDetails from an astropy.table.Table

static make_fullkey(jobname, jobkey='__top__')[source]

Combine jobname and jobkey to make a unique key fullkey = <jobkey>@<jobname>

static make_tables(job_dict)[source]

Build and return an astropy.table.Table' to store `JobDetails

static split_fullkey(fullkey)[source]

Split fullkey to make extract jobname, jobkey fullkey = <jobkey>@<jobname>

topkey = '__top__'
update_table_row(table, row_idx)[source]

Add this instance as a row on a astropy.table.Table

class fermipy.jobs.job_archive.JobStatus[source]

Bases: object

Enumeration of job status types

done = 5
failed = 6
no_job = -1
not_ready = 1
partial_failed = 7
pending = 3
ready = 2
removed = 8
running = 4
unknown = 0
class fermipy.jobs.job_archive.JobStatusVector[source]

Bases: object

Vector that counts the status of jobs and returns an overall status flag based on those

get_status()[source]

Return an overall status based on the number of jobs in various states.

n_done

Return the number of successfully completed jobs

n_failed

Return the number of failed jobs

n_pending

Return the number jobs submitted to batch, but not yet running

n_running

Return the number of running jobs

n_total

Return the total number of jobs

n_waiting

Return the number of jobs in various waiting states

reset()[source]

Reset the counters

fermipy.jobs.job_archive.main_browse()[source]

Entry point for command line use for browsing a JobArchive

fermipy.jobs.batch module

Factory module to return the default interace to the batch farm

fermipy.jobs.batch.get_batch_job_args(job_time=1500)[source]

Get the correct set of batch jobs arguments.

Parameters:job_time (int) – Expected max length of the job, in seconds. This is used to select the batch queue and set the job_check_sleep parameter that sets how often we check for job completion.
Returns:job_args – Dictionary of arguments used to submit a batch job
Return type:dict
fermipy.jobs.batch.get_batch_job_interface(job_time=1500)[source]

Create a batch job interface object.

Parameters:job_time (int) – Expected max length of the job, in seconds. This is used to select the batch queue and set the job_check_sleep parameter that sets how often we check for job completion.
Returns:job_interfact – Object that manages interactions with batch farm
Return type:SysInterface

fermipy.jobs.scatter_gather module

Abstract interface for parallel execution of multiple jobs.

The main class is ScatterGather, which can submit many instances of a job with different configurations.

class fermipy.jobs.scatter_gather.ScatterGather(link, **kwargs)[source]

Bases: fermipy.jobs.link.Link

Class to dispatch several jobs in parallel and collect and merge the results.

Sub-classes will need to generatare configuration for the jobs that they launch.

Parameters:
  • clientclass (type) – Type of Link object managed by this class.
  • job_time (int) – Estimated maximum time it takes to run a job This is used to manage batch farm scheduling and checking for completion.
appname = 'dummy-sg'
build_job_configs(args)[source]

Hook to build job configurations

Sub-class implementation should return:

job_configs : dict
Dictionary of dictionaries passed to parallel jobs
check_status(stream=<open file '<stdout>', mode 'w'>, check_once=False, fail_pending=False, fail_running=False, no_wait=False, do_print=True, write_status=False)[source]

Loop to check on the status of all the jobs in job dict.

Parameters:
  • stream (file) – Stream that this function will print to, Must have ‘write’ function.
  • check_once (bool) – Check status once and exit loop.
  • fail_pending (bool) – If True, consider pending jobs as failed
  • fail_running (bool) – If True, consider running jobs as failed
  • no_wait (bool) – Do not sleep before checking jobs.
  • do_print (bool) – Print summary stats.
  • write_status (bool) – Write the status the to log file.
Returns:

status_vect – Vector that summarize the number of jobs in various states.

Return type:

JobStatusVector

clean_jobs(recursive=False)[source]

Clean up all the jobs associated with this object.

If recursive is True this also clean jobs dispatch by this object.

clear_jobs(recursive=True)[source]

Clear the self.jobs dictionary that contains information about jobs associated with this ScatterGather

If recursive is True this will include jobs from all internal Link

clientclass = None
classmethod create(**kwargs)[source]

Build and return a ScatterGather object

default_options = {}
default_options_base = {'action': ('run', 'Action to perform', <type 'str'>), 'check_status_once': (False, 'Check status only once before proceeding', <type 'bool'>), 'dry_run': (False, 'Print commands, but do not execute them', <type 'bool'>), 'job_check_sleep': (300, 'Sleep time between checking on job status (s)', <type 'int'>), 'print_update': (False, 'Print summary of job status', <type 'bool'>)}
default_prefix_logfile = 'scatter'
description = 'Run multiple analyses'
get_jobs(recursive=True)[source]

Return a dictionary with all the jobs

If recursive is True this will include jobs from all internal Link

job_time = 1500
classmethod main()[source]

Hook for command line interface to sub-classes

print_failed(stream=<open file '<stderr>', mode 'w'>)[source]

Print list of the failed jobs

print_summary(stream=<open file '<stdout>', mode 'w'>, indent='', recurse_level=2)[source]

Print a summary of the activity done by this Link.

Parameters:
  • stream (file) – Stream to print to
  • indent (str) – Indentation at start of line
  • recurse_level (int) – Number of recursion levels to print
print_update(stream=<open file '<stdout>', mode 'w'>, job_stats=None)[source]

Print an update about the current number of jobs running

resubmit(stream=<open file '<stdout>', mode 'w'>, fail_running=False)[source]

Function to resubmit failed jobs and collect results

Parameters:
  • stream (file) – Stream that this function will print to, Must have ‘write’ function.
  • fail_running (bool) – If True, consider running jobs as failed
Returns:

status_vect – Vector that summarize the number of jobs in various states.

Return type:

JobStatusVector

run(stream=<open file '<stdout>', mode 'w'>, dry_run=False, stage_files=True, resubmit_failed=True)[source]

Runs this Link.

This version is intended to be overwritten by sub-classes so as to provide a single function that behaves the same for all version of Link

Parameters:
  • stream (file) – Stream that this Link will print to, Must have ‘write’ function
  • dry_run (bool) – Print command but do not run it.
  • stage_files (bool) – Copy files to and from scratch staging area.
  • resubmit_failed (bool) – Flag for sub-classes to resubmit failed jobs.
run_analysis(argv)[source]

Implemented by sub-classes to run a particular analysis

run_jobs(stream=<open file '<stdout>', mode 'w'>)[source]

Function to dipatch jobs and collect results

Parameters:stream (file) – Stream that this function will print to, Must have ‘write’ function.
Returns:status_vect – Vector that summarize the number of jobs in various states.
Return type:JobStatusVector

Return the Link object used the scatter phase of processing

update_args(override_args)[source]

Update the arguments used to invoke the application

Note that this will also update the dictionary of input and output files

Parameters:override_args (dict) – dictionary of arguments to override the current values
usage = 'dummy-sg [options]'

Module contents