def stats(self, value): if value is None: self._stats = NumericStats() elif isinstance(value, NumericStats): self._stats = value else: raise DataException(f"stats for a NumericComponant should be of type NumericStats, not {type(value)}")
def from_jcr(cls, jcr): classpath = jcr["extractor_class"] extr_class = locate(classpath) if extr_class is None: NameError(f"Could not locate {classpath}") extractor = extr_class.from_jcr(jcr["extractor_state"]) stats = NumericStats.from_jcr(jcr["stats"]) name = jcr["name"] return cls(name=name, extractor=extractor, stats=stats)
def test_schema_buildable(): component = FloatFeature(name="testcomponent") schema = Schema(name="Testing", version="1.0.0", features=[component]) assert not schema.is_built() extractor = FixedSubpatchSimilarity( patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1 ) stats = NumericStats(min=0, max=1) component = FloatFeature(name="testcomponent", extractor=extractor, stats=stats) schema = Schema(name="Testing", version="1.0.0", features=[component]) assert not schema.is_built() extractor = FixedSubpatchSimilarity( patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1 ) stats = NumericStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101), pinv=0) component = FloatFeature(name="testcomponent", extractor=extractor, stats=stats) schema = Schema(name="Testing", version="1.0.0", features=[component, component]) assert schema.is_built()
def test_schema_jcr(): extractor = FixedSubpatchSimilarity( patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1 ) stats = NumericStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101)) component = FloatFeature(name="testcomponent", extractor=extractor, stats=stats) component2 = FloatFeature(name="testcomponent2", extractor=extractor, stats=stats) schema = Schema(name="Testing", version="1.0.0", features=[component, component2]) schema_jcr = schema.to_jcr() assert len(schema_jcr["features"]) == 2 jsonstr = json.dumps(schema_jcr) # Should not throw an error assert len(jsonstr) > 0 schema_restored = Schema.from_jcr(schema_jcr) assert schema.name == schema_restored.name assert schema.version == schema_restored.version assert all([c1 == c2 for (c1, c2) in zip(schema.features.keys(), schema_restored.features.keys())])
def test_feature_buildable(): component = FloatFeature(name="testcomponent") assert not component.is_built() extractor = FixedSubpatchSimilarity( patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1 ) stats = NumericStats(min=0, max=1) component = FloatFeature(name="testcomponent", extractor=extractor, stats=stats) assert not stats.is_built() assert extractor.is_built() assert not component.is_built() extractor = FixedSubpatchSimilarity( patch={"x0": 0, "y0": 0, "x1": 64, "y1": 64}, refs=["adf8d224cb8786cc"], nrefs=1 ) stats = NumericStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101), pinv=0) component = FloatFeature(name="testcomponent", extractor=extractor, stats=stats) assert stats.is_built() assert extractor.is_built() assert component.is_built()
def test_stats_none(): stats = NumericStats() jcr = stats.to_jcr() for attr in NumericStats._attrs: assert jcr[attr] is None
def test_num_stats_build(): stats = NumericStats() assert not stats.is_built() stats = NumericStats( min=0, max=1, mean=0.5, std=0.02, ) assert not stats.is_built() stats = NumericStats(min=0, max=1, mean=0.5, percentiles=range(101), pinv=0) assert not stats.is_built() stats = NumericStats(min=0, max=1, std=0.02, percentiles=range(101), pinv=0) assert not stats.is_built() stats = NumericStats(min=0, mean=0.5, std=0.02, percentiles=range(101), pinv=0) assert not stats.is_built() stats = NumericStats(max=1, mean=0.5, std=0.02, percentiles=range(101), pinv=0) assert not stats.is_built() stats = NumericStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101)) assert not stats.is_built() stats = NumericStats(min=0, max=1, mean=0.5, std=0.02, percentiles=range(101), pinv=0) assert stats.is_built()
def test_stats_partial_none(): params = dict(min=0, max=1, mean=0.5, std=0.02, pinv=0.1, percentiles=list(range(101))) stats = NumericStats(**params) jcr = stats.to_jcr() for attr in NumericStats._attrs: assert jcr[attr] == params[attr]