Skip to content

spatial_graph.dtypes

Classes:

  • DType

    A class to represent a data type in C/C++ and Cython/PYX files.

DType

DType(dtype_str: str)

A class to represent a data type in C/C++ and Cython/PYX files.

Parameters:

  • dtype_str

    (str) –

    The data type string in the format "base_type[size]". The base_type must be one of the valid base types defined in VALID_BASE_TYPES, and size is optional.

Methods:

  • to_c_decl

    Convert this dtype to the equivalent C/C++ declaration with the given name.

  • to_pyxtype

    Convert this dtype to the equivalent PYX type.

  • to_rvalue

    Convert this dtype into an r-value to be used in PYX files for assignments.

Attributes:

  • base_c_type (str) –

    Convert the base of this DType into the equivalent C/C++ type.

Source code in src/spatial_graph/dtypes.py
42
43
44
45
46
def __init__(self, dtype_str: str) -> None:
    self.as_string = dtype_str
    self.base, self.size = self.__parse_array_dtype(dtype_str)
    self.is_array = self.size is not None
    self.shape = (self.size,) if self.is_array else ()

base_c_type property

base_c_type: str

Convert the base of this DType into the equivalent C/C++ type.

to_c_decl

to_c_decl(name: str) -> str

Convert this dtype to the equivalent C/C++ declaration with the given name.

"base_c_type name" if not an array "base_c_type name[size]" if an array type

Source code in src/spatial_graph/dtypes.py
70
71
72
73
74
75
76
77
78
79
def to_c_decl(self, name: str) -> str:
    """Convert this dtype to the equivalent C/C++ declaration with the given name.

    "base_c_type name"        if not an array
    "base_c_type name[size]"  if an array type
    """
    if self.is_array:
        return f"{self.base_c_type} {name}[{self.size}]"
    else:
        return f"{self.base_c_type} {name}"

to_pyxtype

to_pyxtype(
    use_memory_view: bool = False, add_dim: bool = False
) -> str

Convert this dtype to the equivalent PYX type.

"base_c_type"
"base_c_type[size]"     if an array type
"base_c_type[::1]"      if an array type and use_memory_view
"base_c_type[::1]"      if not an array type and add_dim
"base_c_type[:, ::1]"   if an array type and add_dim

Args:

use_memory_view:

    If set, will produce "dtype[::1]" instead of "dtype[dim]" for
    array types.

add_dim:

    Append a dim to the type, e.g., "int32_t[::1]" instead of
    "int32_t" for dtype "int32". If this DType is already an array,
    will create a 2D array, e.g., "int32_t[:, ::1]".
Source code in src/spatial_graph/dtypes.py
 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
def to_pyxtype(self, use_memory_view: bool = False, add_dim: bool = False) -> str:
    """Convert this dtype to the equivalent PYX type.

        "base_c_type"
        "base_c_type[size]"     if an array type
        "base_c_type[::1]"      if an array type and use_memory_view
        "base_c_type[::1]"      if not an array type and add_dim
        "base_c_type[:, ::1]"   if an array type and add_dim

    Args:

        use_memory_view:

            If set, will produce "dtype[::1]" instead of "dtype[dim]" for
            array types.

        add_dim:

            Append a dim to the type, e.g., "int32_t[::1]" instead of
            "int32_t" for dtype "int32". If this DType is already an array,
            will create a 2D array, e.g., "int32_t[:, ::1]".
    """
    if self.is_array:
        if add_dim:
            suffix = "[:, ::1]"
        else:
            if use_memory_view:
                suffix = "[::1]"
            else:
                suffix = f"[{self.size}]"
    else:
        suffix = "[::1]" if add_dim else ""

    return self.base_c_type + suffix

to_rvalue

to_rvalue(name: str, array_index: str | None = None) -> str

Convert this dtype into an r-value to be used in PYX files for assignments.

"name" default "name[array_index]" if array_index is given "{name[0], ..., name[size-1]}" if an array type "{name[array_index, 0], ..., name[array_index, size-1]}" if an array type and array_index is given

Source code in src/spatial_graph/dtypes.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def to_rvalue(self, name: str, array_index: str | None = None) -> str:
    """Convert this dtype into an r-value to be used in PYX files for assignments.

    "name"                  default
    "name[array_index]"     if array_index is given
    "{name[0], ..., name[size-1]}"
                            if an array type
    "{name[array_index, 0], ..., name[array_index, size-1]}"
                            if an array type and array_index is given
    """

    if self.size:
        if array_index:
            return (
                "{"
                + ", ".join(
                    [f"{name}[{array_index}, {i}]" for i in range(self.size)]
                )
                + "}"
            )
        else:
            return (
                "{" + ", ".join([name + f"[{i}]" for i in range(self.size)]) + "}"
            )
    else:
        if array_index:
            return f"{name}[{array_index}]"
        else:
            return name