Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 committed Sep 18, 2024
1 parent a22e056 commit a17f279
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/.vscode
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"brenth",
"proportion",
"nullproportion",
"ospp"
"ospp",
"unpooled"
]
}
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pystatpower.basic import *


class TestEnum(Enum):
Test1 = 1


Alternative(TestEnum.Test1)
148 changes: 148 additions & 0 deletions src/pystatpower/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from abc import ABC, abstractmethod
from enum import Enum
from math import inf
from numbers import Real

from pystatpower.interval import Interval


class Param(ABC):
"""抽象参数基类"""

domain = None

@abstractmethod
def __init__(self, value):
pass

@classmethod
@abstractmethod
def _check(cls, domain, value):
pass


class NumericParam(Param):
"""数值参数基类"""

domain = Interval(-inf, inf)

def __init__(self, value: Real):
cls = type(self)
cls._check(value)
self._value = value

@property
def value(self):
return self._value

@classmethod
def _check(cls, value: Real):
domain = cls.domain
if not isinstance(value, Real):
raise TypeError(f"{value} is not a real number")
if value not in domain:
raise ValueError(f"{value} is not in {domain}")


class OptionalParam(Param):
"""选项参数基类"""

class EmptyEnum(Enum):
pass

domain = EmptyEnum

def __init__(self, value: Enum | str):
cls = type(self)
self._value = cls._check(value)

@property
def value(self):
return self._value

@classmethod
def _check(cls, value: Enum | str) -> Enum:
domain = cls.domain

if isinstance(value, str):
try:
value = domain[value.upper()]
except KeyError:
raise ValueError(f"No such option '{value}' in {domain.__name__}")
elif not isinstance(value, domain):
raise TypeError(f"{value} is not a {domain.__name__}")

return value


class Alpha(NumericParam):
"""显著性水平"""

domain = Interval(0, 1)


class Power(NumericParam):
"""检验效能"""

domain = Interval(0, 1)


class Mean(NumericParam):
"""均值"""

domain = Interval(-inf, inf)


class STD(NumericParam):
"""标准差"""

domain = Interval(0, inf)


class Proportion(NumericParam):
"""率"""

domain = Interval(0, 1)


class Percent(NumericParam):
"""百分比"""

domain = Interval(0, 1)


class Ratio(NumericParam):
"""比例"""

domain = Interval(0, inf)


class Size(NumericParam):
"""样本量"""

domain = Interval(0, inf)


class DropOutRate(NumericParam):
"""脱落率"""

domain = Interval(0, 1, lower_inclusive=True)


class Alternative(OptionalParam):
"""备择假设类型
Attributes
----------
TWO_SIDED : (int)
双侧检验
ONE_SIDED : (int)
单侧检验
"""

class AlternativeEnum(Enum):

TWO_SIDED = 1
ONE_SIDED = 2

domain = AlternativeEnum
Loading

0 comments on commit a17f279

Please sign in to comment.