How to use the pydra.engine.specs.attr_fields function in pydra

To help you get started, we’ve selected a few pydra examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nipype / pydra / pydra / engine / core.py View on Github external
def _reset(self):
        """Reset the connections between inputs and LazyFields."""
        for field in attr_fields(self.inputs):
            if field.name in self.inp_lf:
                setattr(self.inputs, field.name, self.inp_lf[field.name])
        if is_workflow(self):
            for task in self.graph.nodes:
                task._reset()
github nipype / pydra / pydra / engine / core.py View on Github external
def create_connections(self, task):
        """
        Add and connect a particular task to existing nodes in the workflow.

        Parameters
        ----------
        task : :class:`TaskBase`
            The task to be added.

        """
        other_states = {}
        for field in attr_fields(task.inputs):
            val = getattr(task.inputs, field.name)
            if isinstance(val, LazyField):
                # saving all connections with LazyFields
                task.inp_lf[field.name] = val
                # adding an edge to the graph if task id expecting output from a different task
                if val.name != self.name:
                    # checking if the connection is already in the graph
                    if (getattr(self, val.name), task) in self.graph.edges:
                        continue
                    self.graph.add_edges((getattr(self, val.name), task))
                    logger.debug("Connecting %s to %s", val.name, task.name)

                    if (
                        getattr(self, val.name).state
                        and getattr(self, val.name).state.splitter_rpn_final
                    ):
github nipype / pydra / pydra / engine / core.py View on Github external
def is_lazy(obj):
    """Check whether an object has any field that is a Lazy Field"""
    for f in attr_fields(obj):
        if isinstance(getattr(obj, f.name), LazyField):
            return True
    return False
github nipype / pydra / pydra / engine / helpers_file.py View on Github external
def template_update(inputs, map_copyfiles=None):
    """
    Update all templates that are present in the input spec.

    Should be run when all inputs used in the templates are already set.

    """
    dict_ = attr.asdict(inputs)
    if map_copyfiles is not None:
        dict_.update(map_copyfiles)

    from .specs import attr_fields

    fields_templ = [
        fld for fld in attr_fields(inputs) if fld.metadata.get("output_file_template")
    ]
    for fld in fields_templ:
        if fld.type not in [str, ty.Union[str, bool]]:
            raise Exception(
                f"fields with output_file_template"
                "has to be a string or Union[str, bool]"
            )
        inp_val_set = getattr(inputs, fld.name)
        if inp_val_set is not attr.NOTHING and not isinstance(inp_val_set, (str, bool)):
            raise Exception(f"{fld.name} has to be str or bool, but {inp_val_set} set")
        if isinstance(inp_val_set, bool) and fld.type is str:
            raise Exception(
                f"type of {fld.name} is str, consider using Union[str, bool]"
            )

        if isinstance(inp_val_set, str):
github nipype / pydra / pydra / engine / helpers.py View on Github external
def output_names_from_inputfields(inputs):
    """
    Collect outputs from input fields with output_file_template.

    Parameters
    ----------
    inputs :
        TODO

    """
    output_names = []
    for fld in attr_fields(inputs):
        if "output_file_template" in fld.metadata:
            if "output_field_name" in fld.metadata:
                field_name = fld.metadata["output_field_name"]
            else:
                field_name = fld.name
            output_names.append(field_name)
    return output_names
github nipype / pydra / pydra / engine / helpers.py View on Github external
def copyfile_workflow(wf_path, result):
    """ if file in the wf results, the file will be copied to the workflow directory"""
    for field in attr_fields(result.output):
        value = getattr(result.output, field.name)
        new_value = _copyfile_single_value(wf_path=wf_path, value=value)
        if new_value != value:
            setattr(result.output, field.name, new_value)
    return result
github nipype / pydra / pydra / engine / core.py View on Github external
def create_connections(self, task):
        """
        Add and connect a particular task to existing nodes in the workflow.

        Parameters
        ----------
        task : :class:`TaskBase`
            The task to be added.

        """
        other_states = {}
        for field in attr_fields(task.inputs):
            val = getattr(task.inputs, field.name)
            if isinstance(val, LazyField):
                # saving all connections with LazyFields
                task.inp_lf[field.name] = val
                # adding an edge to the graph if task id expecting output from a different task
                if val.name != self.name:
                    # checking if the connection is already in the graph
                    if (getattr(self, val.name), task) in self.graph.edges:
                        continue
                    self.graph.add_edges((getattr(self, val.name), task))
                    logger.debug("Connecting %s to %s", val.name, task.name)

                    if (
                        getattr(self, val.name).state
                        and getattr(self, val.name).state.splitter_rpn_final
                    ):
github nipype / pydra / pydra / engine / task.py View on Github external
def _command_args_single(self, state_ind, ind=None):
        """Get command line arguments for a single state"""
        pos_args = []  # list for (position, command arg)
        self._positions_provided = []
        for f in attr_fields(self.inputs):
            # these inputs will eb used in container_args
            if isinstance(self, ContainerTask) and f.name in [
                "container",
                "image",
                "container_xargs",
                "bindings",
            ]:
                continue
            elif f.name == "executable":
                pos_args.append(
                    self._command_shelltask_executable(
                        field=f, state_ind=state_ind, ind=ind
                    )
                )
            elif f.name == "args":
                pos_val = self._command_shelltask_args(