Skip to content

settings

Plotting parameters common to all functions/backends.

Source code in navis/plotting/settings.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@dataclass
class BasePlottingSettings(Settings):
    """Plotting parameters common to all functions/backends."""

    _name = "BasePlottingSettings"

    # For TreeNeurons
    soma: bool = True
    radius: bool = False  # True | False | "auto"
    linewidth: float = 1
    linestyle: str = "-"

    # For Dotprops
    dps_scale_vec: float = "auto"

    # All neurons
    connectors: bool = False
    connectors_only: bool = False
    cn_size: Optional[float] = None
    cn_alpha: Optional[float] = None
    cn_layout: dict = field(default_factory=dict)
    cn_colors: dict = field(default_factory=dict)
    cn_mesh_colors: bool = False
    color: Optional[
        Union[
            str,
            Tuple[float, float, float],
            List[Union[str, Tuple[float, float, float]]],
            dict,
        ]
    ] = None
    color_by: Optional[Union[str, np.ndarray, List[np.ndarray]]] = None
    shade_by: Optional[Union[str, np.ndarray, List[np.ndarray]]] = None
    palette: Optional[Union[str, np.ndarray]] = None
    alpha: Optional[float] = None
    vmin: Optional[float] = None
    vmax: Optional[float] = None
    smin: Optional[float] = None
    smax: Optional[float] = None
    norm_global: bool = True

    # Other
    scatter_kws: dict = field(default_factory=dict)

    _synonyms: List[Tuple] = field(
        default_factory=lambda: [
            ("linestyle", "ls"),
            ("linewidth", "lw"),
            ("color", "colors", "c"),
        ]
    )

Class that works a bit like a dictionary but can validate keys and has some extra features.

Source code in navis/plotting/settings.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@dataclass
class Settings:
    """Class that works a bit like a dictionary but can validate keys and has some extra features."""

    # We can define synonyms for arguments, so that they can be used interchangeably
    _synonyms: List[Tuple] = field(default_factory=list)
    _name = "Settings"

    def __setattr__(self, key, value, check_valid=False):
        if check_valid and key not in self.properties:
            raise AttributeError(
                f"The '{key}' argument is invalid for {self._name}. Valid arguments are: {', '.join(self.properties)}"
            )
        self.__dict__[key] = value

    def __contains__(self, key):
        return key in self.properties

    @property
    def properties(self):
        return tuple(
            [
                p
                for p in dir(self)
                if not p.startswith("_")
                and (p != "properties")   # we need this to avoid infinite recursion
                and not callable(getattr(self, p, None))
            ]
        )

    def update_settings(self, **kwargs):
        # Deal with synonyms
        for syn in self._synonyms:
            present = [s for s in syn if s in kwargs]
            if len(present) > 1:
                raise ValueError(f"Multiple synonyms for the same argument: {present}")

            for s in syn[1:]:
                if s in kwargs:
                    kwargs[syn[0]] = kwargs.pop(s)

        for k, v in kwargs.items():
            self.__setattr__(k, v, check_valid=VALIDATE_SETTINGS)

        # For method chaining
        return self

    def to_dict(self):
        return {k: v for k, v in self.__dict__.items() if not k.startswith("_")}

    def get(self, key, default=None):
        value = self.__dict__.get(key, default)
        if value is None:
            value = default
        return value

    def pop(self, key, default=None):
        return self.__dict__.pop(key, default)