Grid¶
dummypy.grid
¶
The :class:Grid value type for the dummypy analytics library.
Responsibility split:
- :func:
_build_gridsowns the data-generation concern — turning a sizeninto the two coordinate DataFrames. It is a pure function ofn. - :class:
Gridowns the model concern — validatingn, holding the generatedx/yframes, and exposing behaviour (:meth:Grid.diff).
Keeping generation in a standalone function keeps Grid a thin, testable
value type rather than a class that both stores and manufactures its data.
Grid
¶
A grid representing data points for analytics calculations.
Holds two coordinate DataFrames, x and y (with x == y.T),
generated from the grid size n by :func:_build_grids.
Instances are immutable. x and y are derived from n, so
letting any of the three be reassigned would break the invariants the
validator and :func:_build_grids establish at construction: a new x
need not be y.T, and a new n would not rebuild the frames it is
supposed to describe. Build a new :class:Grid instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
Maximum size for the grid (default: 10). Must be a non-negative integer. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Examples:
Both frames are square with side n + 1, and x is the transpose
of y:
Only n is part of the repr, since x and y are derived:
A negative size is rejected, and so is a float or a bool:
>>> Grid(n=-1)
Traceback (most recent call last):
...
ValueError: Grid size n must be non-negative, got -1
>>> Grid(n=2.0)
Traceback (most recent call last):
...
TypeError: Grid size n must be an integer, got float
Instances are immutable — build a new grid rather than reassigning:
Source code in src/dummypy/grid.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
__attrs_post_init__()
¶
Populate the x and y coordinate frames from n.
Uses :func:object.__setattr__ because the class is frozen — the
standard attrs idiom for a derived attribute on an immutable class.
Keeping the single :func:_build_grids call here preserves the
generation-vs-model split described in the module docstring.
Source code in src/dummypy/grid.py
diff()
¶
Returns a grid of differences.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A fresh DataFrame of element-wise differences (x - y), computed |
DataFrame
|
anew on each call. |
Examples:
The value at (i, j) is i - j, so the frame is antisymmetric
and its diagonal is zero:
>>> grid = Grid(n=3)
>>> grid.diff().loc["2", "1"]
np.int64(1)
>>> grid.diff().loc["1", "2"]
np.int64(-1)
Each call returns a fresh frame, so mutating one cannot corrupt the grid it came from: