This is a collection of recipes on how to solve typical problems using signac.

## Migrating (changing) the project schema

Oftentimes, one discovers at a later stage that important parameters are missing from the project schema. For example, in the tutorial we are modeling a gas using the ideal gas law, but we might discover later that important effects are not captured using this overly simplistic model and decide to replace it with the van der Waals equation:

$\left(p + \frac{N^2 a}{V^2}\right) \left(V - Nb \right) = N k_B T$

Since the ideal gas law can be considered a special case of the equation above with $$a=b=0$$, we could migrate all jobs with:

>>> for job in project:
...     job.sp.setdefault("a", 0)
...     job.sp.setdefault("b", 0)
...


The setdefault() function sets the value for $$a$$ and $$b$$ to 0 in case that they are not already present.

• To delete a key use del job.sp['key_to_be_removed'].

• To rename a key, use job.sp.new_name = job.sp.pop('old_name').

Note

The job.sp and job.doc attributes provide all basic functions of a regular Python dict.

### Applying document-wide changes

The safest approach to apply multiple document-wide changes is to replace the document in one operation. Here is an example on how we could recursively replace all dot (.)-characters with the underscore-character in all keys [1]:

import signac
from collections.abc import Mapping

def migrate(doc):
if isinstance(doc, Mapping):
return {k.replace(".", "_"): migrate(v) for k, v in doc.items()}
else:
return doc

for job in signac.get_project():
job.sp = migrate(job.sp)
job.doc = migrate(job.doc)


This approach makes it also easy to compare the pre- and post-migration states before actually applying them.

## Initializing state points with replica indices

We often require multiple jobs with the same state point to collect enough information to make statistical inferences about the data. Instead of creating multiple projects to handle this, we can add a replica_index to the state point. For example, we can use the following code to generate 3 copies of each state point in a workspace:

# init.py
import signac

project = signac.init_project()
num_reps = 3

## Running in containerized environments

Using signac-flow in combination with container systems such as docker or singularity is easily achieved by modifying the executable directive. For example, assuming that we wanted to use a singularity container named software.simg, which is placed within the project root directory, we use the following directive to specify that a given operation is to be executed within then container:

@Project.operation(directives={"executable": "singularity exec software.simg python"})
def containerized_operation(job):
pass


If you are using the run command for execution, simply execute the whole script in the container:

\$ singularity exec software.simg python project.py run


Attention

Many cluster environments will not allow you to submit jobs to the scheduler using the container image. This means that the actual submission, (e.g. python project.py submit or similar) will need to be executed with a local Python executable.

To avoid issues with dependencies that are only available in the container image, move imports into the operation function. Condition functions will be executed during the submission process to determine what to submit, so dependencies for those must be installed into the local environment as well.

Tip

You can define a decorator that can be reused like this:

def on_container(func):
return flow.directives(executable="singularity exec software.simg python")(func)

@on_container
@Project.operation
def containerized_operation(job):
pass


## Using multiple execution environments for operations

Suppose that for a given project you wanted to run jobs on multiple supercomputers, your laptop, and your desktop. On each of these different machines, different operation directives may be needed. The FlowGroup class provides a mechanism to easily specify the different requirements of each different environment.

# project.py
from flow import FlowProject, directives

class Project(FlowProject):
pass

supercomputer = Project.make_group(name="supercomputer")
laptop = Project.make_group(name="laptop")
desktop = Project.make_group(name="desktop")

@supercomputer(
directives={
"ngpu": 4,
"executable": "singularity exec --nv /path/to/container python",
}
)
@laptop(directives={"ngpu": 0})
@desktop(directives={"ngpu": 1})
@Project.operation
def op1(job):
pass

@supercomputer(
directives=dict(nranks=40, executable="singularity exec /path/to/container python")
)
@laptop(directives={"nranks": 4})
@desktop(directives={"nranks": 8})
@Project.operation
def op2(job):
pass

if __name__ == "__main__":
Project().main()


Tip

Sometimes, a machine should only run certain operations. To specify that an operation should only run on certain machines, only decorate the operation with the groups for the ‘right’ machine(s).

Tip

To test operations with a small interactive job, a ‘test’ group can be used to ensure that the operations do not try to run on multiple cores or GPUs.

## Passing command line options to operations run in a container or other environment

When executing an operation in a container (e.g. Singularity or Docker) or a different environment, the operation will not receive command line flags from the submitting process. FlowGroups can be used to pass options to an exec command. This example shows how to use the run_options argument to tell an operation executed in a container to run in debug mode.

# project.py
from flow import FlowProject

class Project(flow.FlowProject):
pass

# Anything in run_options will be passed to the forked exec command when the operation is run.
# Here we just pass the debug flag.
debug = Project.make_group("debug", run_options="--debug")

@debug
@Project.post.isfile("a.txt")
@Project.operation(directives={"executable": "/path/to/container exec python3"})
def op1(job):
with open(job.fn("a.txt"), "w") as fh:
fh.write("hello world")

if __name__ == "__main__":
Project().main()


To run the operation with debugging, run the group called “debug” with python3 project.py run -o debug.