diff --git a/pyproject.toml b/pyproject.toml index 882e733..6303040 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "simdata" -version = "0.0.3" +version = "0.0.4" description = "" authors = ["dm "] packages = [{include = "simdata"}] diff --git a/readme.md b/readme.md index 6349696..12207d6 100644 --- a/readme.md +++ b/readme.md @@ -24,6 +24,10 @@ s = simd( print(s.simulate()) # {'a': 59, 'b': 0.46, 'c': 86, 'd': 'A', 'e': 'edward'} + +print(sim.int.create(values=[1, 2, 2, 3, 3, 3, 4, 4, 4, 4])) +# func='int' mean=3.0 std=1.0 min=1.0 max=4.0 precision=3 + ``` ## Pydantic diff --git a/simdata/sim.py b/simdata/sim.py index 7134782..8277be7 100644 --- a/simdata/sim.py +++ b/simdata/sim.py @@ -21,16 +21,20 @@ Float = float from pydantic import BaseModel +from collections import Counter from random import choices as r_choices, seed as r_seed from numpy.random import normal as r_normal, seed as n_seed +from numpy import std -def seed(seed:int): +def seed(seed:Int): n_seed(seed); r_seed(seed) return seed class simd_func (BaseModel): def simulate(self): raise NotImplementedError() + def create(self): + raise NotImplementedError() class float (simd_func): func:Literal["float"]="float" @@ -40,6 +44,18 @@ class float (simd_func): max:Float precision:Optional[Int]=3 + @classmethod + def create(cls, values:list[Float], **kwargs): + _sum = sum(values) + _avg = _sum / len(values) + return cls( + mean=_avg, + std=std(values), + min=min(values), + max=max(values), + **kwargs + ) + def simulate(self): value = r_normal(self.mean, self.std) while value < self.min or value > self.max: @@ -50,9 +66,9 @@ class int (float, simd_func): func:Literal['int']="int" @classmethod - def create(cls, values:list[int]): - f = super().create(values=values) - return cls(mean=f.mean, std=f.std, min=f.min, max=f.max) + def create(cls, values:list[Int], **kwargs): + f = super().create(values=values, **kwargs) + return cls(mean=f.mean, std=f.std, min=f.min, max=f.max, precision=f.precision) def simulate(self): return Int(super().simulate()) @@ -63,7 +79,12 @@ class choice (simd_func): weights:list[Int|Float] n:Optional[Int]=1 - def simulate(self): + @classmethod + def create(cls, values:list[str], **kwargs): + value_counts = Counter(values) + return cls(choices=values, weights=value_counts.values(), **kwargs) + + def simulate(self, n:Int=None): choice = r_choices(self.choices, weights=self.weights, k=self.n) if self.n == 1: return choice[0] return choice @@ -72,5 +93,9 @@ class literal (simd_func): func:Literal["literal"]="literal" value:Any + @classmethod + def create(cls, value:str): + return cls(value=value) + def simulate(self): return self.value \ No newline at end of file