GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Numerics/python/pynumerics/src/pynumerics/functions.py at main · USACE-RMC/Numerics

Expand file tree

/

functions.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
1142 lines (883 loc) · 35.9 KB
/

functions.py

Copy path

File metadata and controls

1142 lines (883 loc) · 35.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
156
157
158
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
290
291
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Univariate functions and link functions from ``Numerics.Functions``.
:class:`LinearFunction`, :class:`PowerFunction`, :class:`SegmentedPowerFunction`,
:class:`TabularFunction` and :class:`CompositeFunction` wrap the ``IUnivariateFunction``
implementations (rating curves and other deterministic or stochastic transfer functions);
:class:`EnsembleFunction` holds an ensemble of parameter sets for one template.
:class:`IdentityLink`, :class:`LogLink`, :class:`LogitLink`, :class:`ProbitLink`,
:class:`ComplementaryLogLogLink`, :class:`FisherZLink` and :class:`YeoJohnsonLink` wrap the
``ILinkFunction`` implementations, and :class:`LinkController` holds one link per parameter.
An array argument crosses once, through the list overloads ``UnivariateFunctionExtensions`` and
``LinkFunctionExtensions``, which give the scalar members' values bit for bit.
"""
from __future__ import annotations
from collections.abc import Sequence
from typing import Any
import numpy as np
from pynumerics._util import evaluate, namespace, net_enum, unwrap
from pynumerics.data import UncertainOrderedPairedData, parse_xml
from pynumerics.enums import Extrapolation, NetEnum, Transform
from pynumerics.errors import translate_public
from pynumerics.marshal import to_net_doubles, to_numpy_doubles, to_numpy_matrix
def _functions() -> Any:
"""Return the ``Numerics.Functions`` namespace after starting the runtime."""
return namespace("Numerics.Functions")
_LIST_OVERLOADS: dict[tuple[str, str], Any] = {}
def _apply(net: Any, member: str, extensions: str, values: Any) -> Any:
"""Evaluate a scalar member of ``net`` on a scalar or an array, the array through its list overload.
The list overload is the static ``extensions.member(net, IList<double>)``, which calls the scalar
member once per element, so both give the scalar member's values bit for bit.
"""
key = (extensions, member)
vector = _LIST_OVERLOADS.get(key)
if vector is None:
vector = _LIST_OVERLOADS[key] = getattr(getattr(_functions(), extensions), member)
return evaluate(values, getattr(net, member), lambda array: vector(net, array))
class FunctionType(NetEnum):
"""Univariate function families (``Numerics.Functions.UnivariateFunctionType``)."""
LINEAR = "Linear"
POWER = "Power"
TABULAR = "Tabular"
SEGMENTED_POWER = "SegmentedPower"
COMPOSITE = "Composite"
class CompositeMode(NetEnum):
"""How a :class:`CompositeFunction` combines its children (``Numerics.Functions.CompositeFunctionMode``)."""
WEIGHTED_AVERAGE = "WeightedAverage"
MIXTURE = "Mixture"
class LinkFunction:
"""A link function ``eta = g(x)`` (``Numerics.Functions.ILinkFunction``).
Parameters
----------
net : Numerics.Functions.ILinkFunction
The .NET link to wrap; the subclasses construct their own.
Attributes
----------
net : Numerics.Functions.ILinkFunction
The wrapped .NET object.
"""
def __init__(self, net: Any) -> None:
self.net = unwrap(net)
def link(self, x: Any) -> Any:
"""Map values to the link space (``Link``).
Parameters
----------
x : float or array_like
Values in the natural space.
Returns
-------
float or numpy.ndarray
``g(x)``, shaped like ``x``.
"""
return _apply(self.net, "Link", "LinkFunctionExtensions", x)
def inverse_link(self, eta: Any) -> Any:
"""Map link-space values back to the natural space (``InverseLink``).
Parameters
----------
eta : float or array_like
Values in the link space.
Returns
-------
float or numpy.ndarray
``g^-1(eta)``, shaped like ``eta``.
"""
return _apply(self.net, "InverseLink", "LinkFunctionExtensions", eta)
def d_link(self, x: Any) -> Any:
"""Return the derivative ``dg/dx`` (``DLink``).
Parameters
----------
x : float or array_like
Values in the natural space.
Returns
-------
float or numpy.ndarray
The derivative, shaped like ``x``.
"""
return _apply(self.net, "DLink", "LinkFunctionExtensions", x)
def to_xml(self) -> str:
"""Return the link's ``ToXElement()`` as XML text.
Returns
-------
str
The element as .NET writes it.
"""
return str(self.net.ToXElement().ToString())
class IdentityLink(LinkFunction):
"""The identity link ``g(x) = x`` (``Numerics.Functions.IdentityLink``)."""
def __init__(self) -> None:
super().__init__(_functions().IdentityLink())
class LogLink(LinkFunction):
"""The log link ``g(x) = ln x`` for positive parameters (``Numerics.Functions.LogLink``)."""
def __init__(self) -> None:
super().__init__(_functions().LogLink())
class LogitLink(LinkFunction):
"""The logit link ``g(x) = ln(x / (1 - x))`` for parameters in (0, 1) (``Numerics.Functions.LogitLink``)."""
def __init__(self) -> None:
super().__init__(_functions().LogitLink())
class ProbitLink(LinkFunction):
"""The probit link ``g(x) = Phi^-1(x)`` for parameters in (0, 1) (``Numerics.Functions.ProbitLink``)."""
def __init__(self) -> None:
super().__init__(_functions().ProbitLink())
class ComplementaryLogLogLink(LinkFunction):
"""The complementary log-log link ``g(x) = ln(-ln(1 - x))`` (``ComplementaryLogLogLink``)."""
def __init__(self) -> None:
super().__init__(_functions().ComplementaryLogLogLink())
class FisherZLink(LinkFunction):
"""The Fisher z link ``g(x) = atanh(x)`` for correlations in (-1, 1) (``Numerics.Functions.FisherZLink``)."""
def __init__(self) -> None:
super().__init__(_functions().FisherZLink())
class YeoJohnsonLink(LinkFunction):
"""The Yeo-Johnson power link (``Numerics.Functions.YeoJohnsonLink``).
Parameters
----------
lambda_ : float, optional
The transformation parameter; default 1 (the identity).
values : array_like, optional
Representative values (at least two, finite) to fit lambda to by maximum likelihood
(``YeoJohnson.FitLambda``) instead of giving ``lambda_``.
"""
def __init__(self, lambda_: float = 1.0, values: Any = None) -> None:
functions = _functions()
if values is not None:
super().__init__(functions.YeoJohnsonLink(to_net_doubles(values)))
else:
super().__init__(functions.YeoJohnsonLink(float(lambda_)))
@property
def lambda_(self) -> float:
"""The transformation parameter."""
return float(self.net.Lambda)
_LINK_CLASSES: dict[str, type[LinkFunction]] = {
cls.__name__: cls for cls in (IdentityLink, LogLink, LogitLink, ProbitLink, ComplementaryLogLogLink,
FisherZLink, YeoJohnsonLink)}
def wrap_link(net: Any) -> LinkFunction | None:
"""Wrap a .NET link function in the matching Python class.
Parameters
----------
net : Numerics.Functions.ILinkFunction or None
The .NET link.
Returns
-------
LinkFunction or None
The specific wrapper class (the generic :class:`LinkFunction` for an unknown type), or
None for a null link.
"""
if net is None:
return None
concrete = unwrap(net)
cls = _LINK_CLASSES.get(str(concrete.GetType().Name), LinkFunction)
instance = cls.__new__(cls)
LinkFunction.__init__(instance, concrete)
return instance
def create_link(link_type: str) -> LinkFunction:
"""Create a link function by type name (``LinkFunctionFactory.Create``).
Parameters
----------
link_type : str
A ``LinkFunctionType`` member name: ``"identity"``, ``"log"``, ``"logit"``, ``"probit"``,
``"complementary_log_log"``, ``"yeo_johnson"`` or ``"fisher_z"``.
Returns
-------
LinkFunction
The wrapped link (a Yeo-Johnson link has lambda 1).
"""
functions = _functions()
return wrap_link(functions.LinkFunctionFactory.Create(net_enum(functions.LinkFunctionType, link_type)))
def link_from_xml(xml: str) -> LinkFunction:
"""Restore a link function from its XML (``LinkFunctionFactory.CreateFromXElement``).
Parameters
----------
xml : str
The text :meth:`LinkFunction.to_xml` returns.
Returns
-------
LinkFunction
The wrapped link.
"""
return wrap_link(_functions().LinkFunctionFactory.CreateFromXElement(parse_xml(xml)))
def _net_link(link: Any) -> Any:
"""Return the .NET link for a wrapper, a ``LinkFunctionType`` name, a .NET link or None."""
if link is None:
return None
if isinstance(link, LinkFunction):
return link.net
if isinstance(link, str):
return create_link(link).net
return link
class LinkController:
"""Per-parameter link functions (``Numerics.Functions.LinkController``).
Maps parameters to and from the link space, with the Jacobian, the log-determinant of the
inverse-link Jacobian, indexing and the XML round trip.
Parameters
----------
links : sequence of LinkFunction, str or None
One link per parameter: a :class:`LinkFunction`, a ``LinkFunctionType`` member name, or
None for a parameter without a link.
Attributes
----------
net : Numerics.Functions.LinkController
The wrapped .NET object.
"""
def __init__(self, links: Sequence[Any]) -> None:
functions = _functions()
import System # noqa: PLC0415 - the runtime is running
self.net = functions.LinkController(System.Array[functions.ILinkFunction]([_net_link(v) for v in links]))
@classmethod
def _from_net(cls, net: Any) -> LinkController:
"""Wrap an existing .NET controller."""
instance = cls.__new__(cls)
instance.net = net
return instance
@classmethod
def for_location_scale_shape(cls, location: Any = None, scale: Any = None, shape: Any = None) -> LinkController:
"""Return a three-parameter controller (``ForLocationScaleShape``).
Parameters
----------
location, scale, shape : LinkFunction, str or None, optional
The link of each parameter; None leaves the parameter unlinked.
Returns
-------
LinkController
The controller.
"""
return cls._from_net(_functions().LinkController.ForLocationScaleShape(
_net_link(location), _net_link(scale), _net_link(shape)))
@classmethod
def from_xml(cls, xml: str) -> LinkController:
"""Restore a controller from its XML (the ``XElement`` constructor).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
LinkController
The controller.
"""
return cls._from_net(_functions().LinkController(parse_xml(xml)))
def __len__(self) -> int:
return int(self.net.Count)
@property
def count(self) -> int:
"""The number of parameter slots."""
return int(self.net.Count)
def __getitem__(self, index: int) -> LinkFunction | None:
return wrap_link(self.net[int(index)])
@property
def links(self) -> list[LinkFunction | None]:
"""The link of each parameter (None where unlinked)."""
return [wrap_link(link) for link in self.net.Links]
def link(self, x: Any) -> np.ndarray:
"""Map parameters to the link space (``Link``).
Parameters
----------
x : array_like
Parameters, one per link.
Returns
-------
numpy.ndarray
The linked values.
"""
return to_numpy_doubles(self.net.Link(to_net_doubles(x)))
def inverse_link(self, eta: Any) -> np.ndarray:
"""Map link-space values back to parameters (``InverseLink``).
Parameters
----------
eta : array_like
Link-space values, one per link.
Returns
-------
numpy.ndarray
The parameters.
"""
return to_numpy_doubles(self.net.InverseLink(to_net_doubles(eta)))
def link_jacobian(self, x: Any) -> np.ndarray:
"""Return the diagonal Jacobian d(eta)/d(x) at the parameters (``LinkJacobian``).
Parameters
----------
x : array_like
Parameters, one per link.
Returns
-------
numpy.ndarray
The square Jacobian matrix.
"""
return to_numpy_matrix(self.net.LinkJacobian(to_net_doubles(x)).ToArray())
def log_det_jacobian(self, phi: Any) -> float:
"""Return ``log |d theta / d phi|`` of the inverse link at link-space values (``LogDetJacobian``).
Parameters
----------
phi : array_like
Link-space values, one per parameter.
Returns
-------
float
The log-determinant, the change-of-variables term of a prior in the link space.
"""
return float(self.net.LogDetJacobian(to_net_doubles(phi)))
def to_xml(self) -> str:
"""Return the controller's ``ToXElement()`` as XML text.
Returns
-------
str
The element as .NET writes it.
"""
return str(self.net.ToXElement().ToString())
class UnivariateFunction:
"""A deterministic or stochastic function ``y = f(x)`` (``Numerics.Functions.IUnivariateFunction``).
A stochastic function evaluates at :attr:`confidence_level` of its residual; with the level
outside [0, 1] or :attr:`is_deterministic` set, it evaluates the mean curve.
Parameters
----------
net : Numerics.Functions.IUnivariateFunction
The .NET function to wrap; the subclasses construct their own.
Attributes
----------
net : Numerics.Functions.IUnivariateFunction
The wrapped .NET object.
"""
def __init__(self, net: Any) -> None:
self.net = unwrap(net)
@property
def number_of_parameters(self) -> int:
"""The length of the parameter vector."""
return int(self.net.NumberOfParameters)
@property
def parameters_valid(self) -> bool:
"""Whether the current parameters are valid."""
return bool(self.net.ParametersValid)
@property
def minimum(self) -> float:
"""The lower bound of the support (settable where the function allows it)."""
return float(self.net.Minimum)
@minimum.setter
def minimum(self, value: float) -> None:
self.net.Minimum = float(value)
@property
def maximum(self) -> float:
"""The upper bound of the support."""
return float(self.net.Maximum)
@maximum.setter
def maximum(self, value: float) -> None:
self.net.Maximum = float(value)
@property
def minimum_of_parameters(self) -> np.ndarray:
"""The lower bound of each parameter."""
return to_numpy_doubles(self.net.MinimumOfParameters)
@property
def maximum_of_parameters(self) -> np.ndarray:
"""The upper bound of each parameter."""
return to_numpy_doubles(self.net.MaximumOfParameters)
@property
def is_deterministic(self) -> bool:
"""Whether the function ignores its residual."""
return bool(self.net.IsDeterministic)
@is_deterministic.setter
def is_deterministic(self, value: bool) -> None:
self.net.IsDeterministic = bool(value)
@property
def confidence_level(self) -> float:
"""The residual quantile evaluated; outside [0, 1] (default -1) the mean curve."""
return float(self.net.ConfidenceLevel)
@confidence_level.setter
def confidence_level(self, value: float) -> None:
self.net.ConfidenceLevel = float(value)
def set_parameters(self, parameters: Any) -> None:
"""Set the parameter vector (``SetParameters``); invalid values are recorded, not refused.
Parameters
----------
parameters : array_like
The parameters in Numerics order.
"""
self.net.SetParameters(to_net_doubles(parameters))
def validate_parameters(self, parameters: Any, throw: bool = False) -> str | None:
"""Check a parameter vector (``ValidateParameters``).
Parameters
----------
parameters : array_like
The parameters in Numerics order.
throw : bool, optional
Whether to raise instead of returning the message; default False.
Returns
-------
str or None
The validation message, or None when the parameters are valid.
Raises
------
ValueError
If ``throw`` is True and the parameters are invalid.
"""
error = self.net.ValidateParameters(to_net_doubles(parameters), bool(throw))
return None if error is None else str(error.Message)
def function(self, x: Any) -> Any:
"""Evaluate ``y = f(x)`` (``Function``).
Parameters
----------
x : float or array_like
Arguments.
Returns
-------
float or numpy.ndarray
The function values, shaped like ``x``.
"""
return _apply(self.net, "Function", "UnivariateFunctionExtensions", x)
def inverse_function(self, y: Any) -> Any:
"""Evaluate ``x = f^-1(y)`` (``InverseFunction``).
Parameters
----------
y : float or array_like
Function values.
Returns
-------
float or numpy.ndarray
The arguments, shaped like ``y``.
"""
return _apply(self.net, "InverseFunction", "UnivariateFunctionExtensions", y)
def __call__(self, x: Any) -> Any:
return self.function(x)
def to_xml(self) -> str:
"""Return the function's ``ToXElement()`` as XML text.
Returns
-------
str
The element as .NET writes it.
"""
return str(self.net.ToXElement().ToString())
@property
def function_type(self) -> FunctionType:
"""The family (``UnivariateFunctionFactory.GetFunctionType``)."""
return FunctionType(str(_functions().UnivariateFunctionFactory.GetFunctionType(self.net)))
class LinearFunction(UnivariateFunction):
"""The linear function ``y = alpha + beta x`` with a Normal residual (``Numerics.Functions.LinearFunction``).
Parameters
----------
alpha, beta : float, optional
Intercept and slope; default 0 and 1.
sigma : float, optional
Residual standard error; when given the function is stochastic, otherwise deterministic.
"""
def __init__(self, alpha: float = 0.0, beta: float = 1.0, sigma: float | None = None) -> None:
functions = _functions()
net = (functions.LinearFunction(float(alpha), float(beta)) if sigma is None
else functions.LinearFunction(float(alpha), float(beta), float(sigma)))
super().__init__(net)
@classmethod
def from_xml(cls, xml: str) -> LinearFunction:
"""Restore the function from its XML (``FromXElement``).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
LinearFunction
The function.
"""
return wrap_function(_functions().LinearFunction.FromXElement(parse_xml(xml)))
@property
def alpha(self) -> float:
"""The intercept."""
return float(self.net.Alpha)
@alpha.setter
def alpha(self, value: float) -> None:
self.net.Alpha = float(value)
@property
def beta(self) -> float:
"""The slope."""
return float(self.net.Beta)
@beta.setter
def beta(self, value: float) -> None:
self.net.Beta = float(value)
@property
def sigma(self) -> float:
"""The residual standard error."""
return float(self.net.Sigma)
@sigma.setter
def sigma(self, value: float) -> None:
self.net.Sigma = float(value)
class PowerFunction(UnivariateFunction):
"""The power function ``y = alpha (x - xi)^beta`` with a log-space Normal residual (``PowerFunction``).
Parameters
----------
alpha, beta : float, optional
Coefficient and exponent; default 1 and 1.5.
xi : float, optional
Location (the lower bound of the support); default 0.
sigma : float, optional
Residual standard error in natural-log space; when given the function is stochastic.
"""
def __init__(self, alpha: float = 1.0, beta: float = 1.5, xi: float = 0.0, sigma: float | None = None) -> None:
functions = _functions()
net = (functions.PowerFunction(float(alpha), float(beta), float(xi)) if sigma is None
else functions.PowerFunction(float(alpha), float(beta), float(xi), float(sigma)))
super().__init__(net)
@classmethod
def from_xml(cls, xml: str) -> PowerFunction:
"""Restore the function from its XML (``FromXElement``).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
PowerFunction
The function.
"""
return wrap_function(_functions().PowerFunction.FromXElement(parse_xml(xml)))
@property
def alpha(self) -> float:
"""The coefficient."""
return float(self.net.Alpha)
@alpha.setter
def alpha(self, value: float) -> None:
self.net.Alpha = float(value)
@property
def beta(self) -> float:
"""The exponent."""
return float(self.net.Beta)
@beta.setter
def beta(self, value: float) -> None:
self.net.Beta = float(value)
@property
def xi(self) -> float:
"""The location (lower bound of the support)."""
return float(self.net.Xi)
@xi.setter
def xi(self, value: float) -> None:
self.net.Xi = float(value)
@property
def sigma(self) -> float:
"""The residual standard error in natural-log space."""
return float(self.net.Sigma)
@sigma.setter
def sigma(self, value: float) -> None:
self.net.Sigma = float(value)
@property
def is_inverse(self) -> bool:
"""Whether :meth:`function` evaluates the inverse power law (x and y swapped)."""
return bool(self.net.IsInverse)
@is_inverse.setter
def is_inverse(self, value: bool) -> None:
self.net.IsInverse = bool(value)
class SegmentedPowerFunction(UnivariateFunction):
"""A sum of power-law controls ``sum_k alpha_k (x - h_k)^beta_k [x > h_k]`` (``SegmentedPowerFunction``).
The parameter layout is ``[h_1, log10 alpha_1, beta_1, ..., h_K, log10 alpha_K, beta_K, sigma]``
with a Gaussian residual in log10 space (Le Coz et al., 2014, J. Hydrol. 509:573-587).
Parameters
----------
number_of_segments : int, optional
Segments of a deterministic function at the default parameters; default 1.
parameters : array_like, optional
A full parameter vector (length ``3 K + 1``) instead; the function is then stochastic.
"""
def __init__(self, number_of_segments: int = 1, parameters: Any = None) -> None:
functions = _functions()
net = (functions.SegmentedPowerFunction(int(number_of_segments)) if parameters is None
else functions.SegmentedPowerFunction(to_net_doubles(parameters)))
super().__init__(net)
@classmethod
def from_xml(cls, xml: str) -> SegmentedPowerFunction:
"""Restore the function from its XML (``FromXElement``).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
SegmentedPowerFunction
The function.
"""
return wrap_function(_functions().SegmentedPowerFunction.FromXElement(parse_xml(xml)))
@property
def number_of_segments(self) -> int:
"""The number of controls."""
return int(self.net.NumberOfSegments)
@property
def sigma(self) -> float:
"""The residual standard error in log10 space."""
return float(self.net.Sigma)
def get_breakpoint(self, segment: int) -> float:
"""Return the activation stage ``h_k`` of a segment (``GetBreakpoint``).
Parameters
----------
segment : int
The one-based segment number.
Returns
-------
float
The breakpoint.
"""
return float(self.net.GetBreakpoint(int(segment)))
def get_log10_alpha(self, segment: int) -> float:
"""Return ``log10 alpha_k`` of a segment (``GetLog10Alpha``).
Parameters
----------
segment : int
The one-based segment number.
Returns
-------
float
The log10 coefficient.
"""
return float(self.net.GetLog10Alpha(int(segment)))
def get_beta(self, segment: int) -> float:
"""Return the exponent ``beta_k`` of a segment (``GetBeta``).
Parameters
----------
segment : int
The one-based segment number.
Returns
-------
float
The exponent.
"""
return float(self.net.GetBeta(int(segment)))
class TabularFunction(UnivariateFunction):
"""A function tabulated by uncertain paired data (``Numerics.Functions.TabularFunction``).
The curve at :attr:`confidence_level` is ``paired_data.curve_sample(level)`` (the means
when the level is negative), interpolated in the transformed spaces.
Parameters
----------
paired_data : UncertainOrderedPairedData
The x values and the distribution of y at each.
"""
def __init__(self, paired_data: UncertainOrderedPairedData) -> None:
super().__init__(_functions().TabularFunction(paired_data.net))
@classmethod
def from_xml(cls, xml: str) -> TabularFunction:
"""Restore the function from its XML (``FromXElement``).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
TabularFunction
The function.
"""
return wrap_function(_functions().TabularFunction.FromXElement(parse_xml(xml)))
@property
def paired_data(self) -> UncertainOrderedPairedData:
"""The uncertain paired data."""
return UncertainOrderedPairedData._from_net(self.net.PairedData)
@property
def x_transform(self) -> Transform:
"""The interpolation space of x."""
return Transform(str(self.net.XTransform))
@x_transform.setter
def x_transform(self, value: Transform | str) -> None:
self.net.XTransform = net_enum(namespace("Numerics.Data").Transform, value)
@property
def y_transform(self) -> Transform:
"""The interpolation space of y."""
return Transform(str(self.net.YTransform))
@y_transform.setter
def y_transform(self, value: Transform | str) -> None:
self.net.YTransform = net_enum(namespace("Numerics.Data").Transform, value)
@property
def extrapolation(self) -> Extrapolation:
"""The sides on which the table extends linearly; default none (end values held)."""
return Extrapolation(str(self.net.Extrapolation))
@extrapolation.setter
def extrapolation(self, value: Extrapolation | str) -> None:
self.net.Extrapolation = net_enum(namespace("Numerics.Data").ExtrapolationSides, value)
@property
def allow_negative_y_values(self) -> bool:
"""Whether negative or NaN values are kept; when False they become 0."""
return bool(self.net.AllowNegativeYValues)
@allow_negative_y_values.setter
def allow_negative_y_values(self, value: bool) -> None:
self.net.AllowNegativeYValues = bool(value)
class CompositeFunction(UnivariateFunction):
"""A weighted combination of univariate functions (``Numerics.Functions.CompositeFunction``).
With :attr:`mode` weighted average, a stochastic evaluation draws every child at the same
confidence level; with mixture, the level selects one child by its weight.
Parameters
----------
functions : sequence of UnivariateFunction
The children (at least one).
weights : array_like, optional
Non-negative weights summing to one, one per child; default equal weights.
"""
def __init__(self, functions: Sequence[UnivariateFunction], weights: Any = None) -> None:
net_functions = _functions()
import System # noqa: PLC0415 - the runtime is running
array = System.Array[net_functions.IUnivariateFunction]([f.net for f in functions])
net = (net_functions.CompositeFunction(array) if weights is None
else net_functions.CompositeFunction(array, to_net_doubles(weights)))
super().__init__(net)
@classmethod
def from_xml(cls, xml: str) -> CompositeFunction:
"""Restore the function from its XML (``FromXElement``).
Parameters
----------
xml : str
The text :meth:`to_xml` returns.
Returns
-------
CompositeFunction
The function.
"""
return wrap_function(_functions().CompositeFunction.FromXElement(parse_xml(xml)))
@property
def mode(self) -> CompositeMode:
"""How the children combine."""
return CompositeMode(str(self.net.Mode))
@mode.setter
def mode(self, value: CompositeMode | str) -> None:
self.net.Mode = net_enum(_functions().CompositeFunctionMode, value)
@property
def functions(self) -> list[UnivariateFunction]:
"""The children."""
return [wrap_function(f) for f in self.net.Functions]
@property
def weights(self) -> np.ndarray:
"""The child weights (the parameter vector)."""
return to_numpy_doubles(unwrap(self.net.Weights))
_FUNCTION_CLASSES: dict[str, type[UnivariateFunction]] = {
cls.__name__: cls for cls in (LinearFunction, PowerFunction, SegmentedPowerFunction, TabularFunction,
CompositeFunction)}
def wrap_function(net: Any) -> UnivariateFunction:
"""Wrap a .NET univariate function in the matching Python class.
Parameters
----------
net : Numerics.Functions.IUnivariateFunction
The .NET function.
Returns
-------
UnivariateFunction
The specific wrapper class (the generic :class:`UnivariateFunction` for an unknown type).
"""
concrete = unwrap(net)
cls = _FUNCTION_CLASSES.get(str(concrete.GetType().Name), UnivariateFunction)
instance = cls.__new__(cls)
UnivariateFunction.__init__(instance, concrete)
return instance
def create_function(function_type: FunctionType | str) -> UnivariateFunction:
"""Create a function at its default parameters (``UnivariateFunctionFactory.CreateFunction``).
Parameters
----------
function_type : FunctionType or str
The family: ``"linear"``, ``"power"`` or ``"segmented_power"`` (a tabular or composite
function needs its data, so the factory refuses them).
Returns
-------
UnivariateFunction
The wrapped function.
"""
functions = _functions()
return wrap_function(functions.UnivariateFunctionFactory.CreateFunction(
net_enum(functions.UnivariateFunctionType, function_type)))
def function_from_xml(xml: str) -> UnivariateFunction:
"""Restore any univariate function from its XML (``UnivariateFunctionFactory.CreateFromXElement``).
View remainder of file in raw view
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.