Skip to content

make_style

Signature/Parameters

def make_style(new_style: Mapping[str, Any], baseline: str = 'default') -> dict

Construct a customized, mutable graph style dictionary by applying a set of user-provided overrides (new_style) to a baseline built-in style template.

Parameters:

Name Type Description Default
new_style Mapping[str, Any]

A dictionary describing style modifications. The structure may range from flat (broadcast) updates to arbitrarily nested overrides. The function recursively matches keys against the baseline schema and applies overrides to the appropriate subtrees. Unknown keys or incompatible structures raise informative errors. To see the visual properties available to set use causalinf.gcm.get_styles(which='default'), and see Notes below. The output of this function can be used to set the plot style locally using causalinf.gcm.plot(graph_style=<dict>) or globally using causalinf.options.set_options(graph_style=<dict>). Supported patterns include:

  • Flat parameter override (propagate style option to all nodes). Ex:

  • Scoped override for a specific node type. Ex (propagate to Exposure nodes only): {“Exposure”: {“node_shape”: “.”}}

  • Fully explicit nesting is accepted: {“nodes”: {“Exposure”: {“node_shape”: “.”}}}

  • Edge-level overrides (same as for node options): {“edge_color”: “red”} <= all edges become red {“edges”: {“edge_color”: “red”}} <= all edges become red {“Directed”: {“edge_color”: “red”}} <= only directed edges become red

required
baseline str

Name of the baseline built-in style (see causalinf.gcm.get_styles()).

'default'

Returns:

Type Description
dict

A mutable style dictionary derived from the baseline but modified according to new_style. The returned structure is fully detached from the immutable baseline, so changes to the result do not affect the built-in styles.

Notes
  • This function does not mutate the baseline style.
  • Flat parameters (e.g., {“node_size”: 800}) are automatically broadcast to every location in the schema where that parameter exists.
  • Nested dictionaries target specific nodes/edges or structural locations.
  • The override mechanism is schema-guided: only fields that exist in the baseline structure can be modified.

Examples:

>>> make_style({"node_shape": "."})
# returns a dictionary with node_shape='.' to all node types
>>> make_style({"Exposure": {"node_shape": "."}})
# returns a dictionary with node_shape='.' only for the Exposure node
>>> make_style({"edges": {"edge_color": "red"}})
# returns a dictionary with  all edge colors (directed, bidirected, undirected) to red
Source code in causalinf/gcm.py
def make_style(new_style: Mapping[str, Any], baseline: str = 'default') -> dict:
    """
    Construct a customized, mutable graph style dictionary by applying a set of
    user-provided overrides (`new_style`) to a baseline built-in style template.

    Parameters
    ----------
    new_style : Mapping[str, Any]
        A dictionary describing style modifications. The structure may range from
        flat (broadcast) updates to arbitrarily nested overrides.
        The function recursively matches keys against the baseline schema and applies
        overrides to the appropriate subtrees. Unknown keys or incompatible structures
        raise informative errors. To see the visual properties available to set
        use ``causalinf.gcm.get_styles(which='default')``, and see ``Notes`` below.
        The output of this function can be used to set the plot style locally using
       ``causalinf.gcm.plot(graph_style=<dict>)`` or globally using 
       ``causalinf.options.set_options(graph_style=<dict>)``.
        Supported patterns include:

        * Flat parameter override (propagate style option to all nodes). Ex:
            {"node_shape": "."}

        * Scoped override for a specific node type. Ex (propagate to Exposure nodes only):
            {"Exposure": {"node_shape": "."}}

        * Fully explicit nesting is accepted:
            {"nodes": {"Exposure": {"node_shape": "."}}}

        * Edge-level overrides (same as for node options):
            {"edge_color": "red"}               <= all edges become red
            {"edges": {"edge_color": "red"}}    <= all edges become red
            {"Directed": {"edge_color": "red"}} <= only directed edges become red


    baseline : str, optional
        Name of the baseline built-in style (see ``causalinf.gcm.get_styles()``).

    Returns
    -------
    dict
        A **mutable** style dictionary derived from the baseline but modified according
        to ``new_style``. The returned structure is fully detached from the immutable
        baseline, so changes to the result do not affect the built-in styles.

    Notes
    -----
    * This function does **not** mutate the baseline style.
    * Flat parameters (e.g., {"node_size": 800}) are automatically broadcast to every
      location in the schema where that parameter exists.
    * Nested dictionaries target specific nodes/edges or structural locations.
    * The override mechanism is schema-guided: only fields that exist in the baseline
      structure can be modified.

    Examples
    --------
    >>> make_style({"node_shape": "."})
    # returns a dictionary with node_shape='.' to all node types

    >>> make_style({"Exposure": {"node_shape": "."}})
    # returns a dictionary with node_shape='.' only for the Exposure node

    >>> make_style({"edges": {"edge_color": "red"}})
    # returns a dictionary with  all edge colors (directed, bidirected, undirected) to red
    """
    if not isinstance(new_style, Mapping):
        raise TypeError("new_style must be a dict-like mapping")

    style = copy_style(baseline)
    schema = GRAPH_STYLES[baseline]

    _make_style_apply_style_update(style, new_style, schema)
    return style