lythonic.exposure¶
Sparse, string-keyed exposure matrices relating subjects to targets.
Sparse, string-keyed exposure matrices.
An exposure matrix relates subjects (the entities that hold exposure) to
targets (the things they are exposed to). Both axes are named by a
Universe of string keys, and storage is sparse because most cells are
empty in practice.
ExposureMatrix is immutable. All growth happens in ExposureMatrixBuilder,
which produces a matrix via build(). See
docs/adr/0001-immutable-matrix-with-builder.md.
>>> b = ExposureMatrixBuilder()
>>> b.set_exposure("acct1", "USD", 0.4)
>>> m = b.build()
>>> m.exposure("acct1", "USD")
0.4
>>> m.exposure("acct1", "EUR")
Traceback (most recent call last):
...
KeyError: "'EUR' not in universe"
ExposureMatrix
¶
Bases: BaseModel
Immutable sparse matrix of exposures, keyed by string on both axes.
Reads by key raise KeyError for a key outside the relevant universe,
and return cell_fill for a key that is in the universe but has no
record, so "unknown key" and "no exposure" stay distinguishable.
Source code in src/lythonic/exposure.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
np = LibAccess(MatrixNpIn, MatrixNpOut)
class-attribute
¶
Numpy facade. Class access gives constructors, instance access conversions.
exposure(subject, target)
¶
Value at one cell, or cell_fill when no record exists for it.
Source code in src/lythonic/exposure.py
exposures_of(subject)
¶
Stored exposures of one subject, keyed by target.
Source code in src/lythonic/exposure.py
exposures_to(target)
¶
Stored exposures to one target, keyed by subject.
cast(subjects=None, targets=None, default_row=None)
¶
A new matrix over the given universes, keeping cell_fill.
None keeps an axis as-is. This is a projection: keys outside the new
universes are dropped along with their exposures. Subjects the cast
introduces take default_row, restricted to the new target universe,
or cell_fill when no default row is given. There is no default
column - targets are the curated axis.
Source code in src/lythonic/exposure.py
to_builder(default_row=None)
¶
A builder seeded with this matrix, with both axes frozen.
Freezing is the safe default for amending an existing matrix; thaw an axis explicitly to grow it.
Source code in src/lythonic/exposure.py
ExposureMatrixBuilder
¶
Mutable accumulator that produces an ExposureMatrix.
Universes grow in first-mention order.
Source code in src/lythonic/exposure.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
np
property
¶
Array-shaped writers, valid only against frozen axes.
subjects
property
¶
The subject universe accumulated so far.
targets
property
¶
The target universe accumulated so far.
subjects_frozen
property
¶
Whether the subject axis rejects keys outside its universe.
targets_frozen
property
¶
Whether the target axis rejects keys outside its universe.
freeze_subjects()
¶
thaw_subjects()
¶
freeze_targets()
¶
thaw_targets()
¶
set_exposure(subject, target, value)
¶
Set one cell, growing either universe if the key is new.
Writing cell_fill removes the record, keeping storage canonical.
Source code in src/lythonic/exposure.py
set_exposures(subject, targets)
¶
Replace a subject's whole row.
An empty mapping models a subject with no exposures, which is not the
same as a subject that was never mentioned. None applies the
configured default row and raises if there is none.
Source code in src/lythonic/exposure.py
exposure(subject, target)
¶
Value at one cell so far, or cell_fill when no record exists for it.
Source code in src/lythonic/exposure.py
exposures_of(subject)
¶
Stored exposures of one subject so far, keyed by target.
Source code in src/lythonic/exposure.py
build()
¶
Snapshot the current state as an immutable matrix.
Source code in src/lythonic/exposure.py
MatrixNpIn
¶
Class-access numpy facade: constructors that take dense arrays.
Source code in src/lythonic/exposure.py
from_matrix(arr, subjects, targets, cell_fill=0.0)
¶
Build a matrix from a dense array, dropping cells equal to cell_fill.
This is how a numpy result comes home. NaN raises rather than being stored, since a NaN in the data is almost always upstream breakage.
Source code in src/lythonic/exposure.py
MatrixNpOut
¶
Instance-access numpy facade: dense views of a matrix.
Source code in src/lythonic/exposure.py
BuilderNpAccess
¶
Array-shaped writers, valid only against frozen axes.
Source code in src/lythonic/exposure.py
set_exposures(subject, values)
¶
Replace a subject's row from an array aligned to the target universe.
Source code in src/lythonic/exposure.py
set_matrix(arr)
¶
Replace every row from a dense array aligned to both universes.