| 1 |
from django.db.models import Aggregate, FloatField, IntegerField |
| 2 |
|
| 3 |
__all__ = [ |
| 4 |
"CovarPop", |
| 5 |
"Corr", |
| 6 |
"RegrAvgX", |
| 7 |
"RegrAvgY", |
| 8 |
"RegrCount", |
| 9 |
"RegrIntercept", |
| 10 |
"RegrR2", |
| 11 |
"RegrSlope", |
| 12 |
"RegrSXX", |
| 13 |
"RegrSXY", |
| 14 |
"RegrSYY", |
| 15 |
"StatAggregate", |
| 16 |
] |
| 17 |
|
| 18 |
|
| 19 |
class StatAggregate(Aggregate): |
| 20 |
output_field = FloatField() |
| 21 |
|
| 22 |
def __init__(self, y, x, output_field=None, filter=None, default=None): |
| 23 |
if not x or not y: |
| 24 |
raise ValueError("Both y and x must be provided.") |
| 25 |
super().__init__( |
| 26 |
y, x, output_field=output_field, filter=filter, default=default |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
class Corr(StatAggregate): |
| 31 |
function = "CORR" |
| 32 |
|
| 33 |
|
| 34 |
class CovarPop(StatAggregate): |
| 35 |
def __init__(self, y, x, sample=False, filter=None, default=None): |
| 36 |
self.function = "COVAR_SAMP" if sample else "COVAR_POP" |
| 37 |
super().__init__(y, x, filter=filter, default=default) |
| 38 |
|
| 39 |
|
| 40 |
class RegrAvgX(StatAggregate): |
| 41 |
function = "REGR_AVGX" |
| 42 |
|
| 43 |
|
| 44 |
class RegrAvgY(StatAggregate): |
| 45 |
function = "REGR_AVGY" |
| 46 |
|
| 47 |
|
| 48 |
class RegrCount(StatAggregate): |
| 49 |
function = "REGR_COUNT" |
| 50 |
output_field = IntegerField() |
| 51 |
empty_result_set_value = 0 |
| 52 |
|
| 53 |
|
| 54 |
class RegrIntercept(StatAggregate): |
| 55 |
function = "REGR_INTERCEPT" |
| 56 |
|
| 57 |
|
| 58 |
class RegrR2(StatAggregate): |
| 59 |
function = "REGR_R2" |
| 60 |
|
| 61 |
|
| 62 |
class RegrSlope(StatAggregate): |
| 63 |
function = "REGR_SLOPE" |
| 64 |
|
| 65 |
|
| 66 |
class RegrSXX(StatAggregate): |
| 67 |
function = "REGR_SXX" |
| 68 |
|
| 69 |
|
| 70 |
class RegrSXY(StatAggregate): |
| 71 |
function = "REGR_SXY" |
| 72 |
|
| 73 |
|
| 74 |
class RegrSYY(StatAggregate): |
| 75 |
function = "REGR_SYY" |