def compare_metrics(candidate: Callable, reference: Callable, tests: int = 10) -> bool: """Test if candidate metric is identical within float error to reference metric. Parameters ------------------------------------------- candidate: Callable, The metric to be tested. reference: Callable, The reference metric considered as ground truth. tests: int = 10, Number of random dictionaries (both float and integer) to test. Returns -------------------------------------------- Boolean value with the test result. """ for dict_generator in (random_float_dict, random_int_dict): random.seed(46) for _ in range(tests): a = normalize_dict(deflate(dict_generator(2, 10))) b = normalize_dict(deflate(dict_generator(2, 10))) assert candidate(b, a) == pytest.approx(candidate(a, b)) assert 0 == pytest.approx(candidate(a, a)) assert 0 == pytest.approx(candidate(b, b)) path = "tests/references/{metric}/{sha}.json".format( metric=candidate.__name__, sha=sha256({ "a": a, "b": b })) distance = candidate(a, b) if not os.path.exists(path): os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w") as f: json.dump({"distance": distance}, f) with open(path, "r") as f: assert pytest.approx(distance) == json.load(f)["distance"] if reference is not None: try: assert pytest.approx(distance) == reference( *dict_to_array(a, b)) except AssertionError as e: print( "Candidate {candidate} does not match {reference}: {candidate_value} != {reference_value}." .format( candidate=candidate.__name__, reference=reference.__name__, candidate_value=candidate(a, b), reference_value=reference(*dict_to_array(a, b)))) raise e
def rasterize(self): self._names = [] self._fixed = {} self._space = [] for name, value in deflate(self, sep=self._sep, leave_tuples=True).items(): self._parse(name, value)
def train_test_binary_classifications_metrics( y_true_train: np.ndarray, y_pred_train: np.ndarray, y_true_test: np.ndarray, y_pred_test: np.ndarray) -> Dict[str, float]: """Return dictionary with binary classification metrics for both the training and test set. Parameters ---------- y_true_train: np.ndarray, Array with ground truth labels from the train set. y_pred_train: np.ndarray, Array with predictions of the labels from the train set. y_true_test: np.ndarray, Array with ground truth labels from the test set. y_pred_test: np.ndarray, Array with predictions of the labels from the test set. Returns ------- Dictionary with binary classification metrics. """ return deflate({ "train": binary_classifications_metrics(y_true_train, y_pred_train), "test": binary_classifications_metrics(y_true_test, y_pred_test) })
def test_tuple_keys(): my_dict = { (1,"d"):6, (56, 56,"d"):6, (1,"d", True):6 } assert my_dict == inflate(deflate(my_dict))
def test_inflate_deflate(): dictionaries = [test_dict] + [ random_int_dict(4, 4) for i in range(100) ] + [random_float_dict(4, 4) for i in range(100)] + [random_string_dict(4, 4) for i in range(100)] for d in dictionaries: assert d == inflate(deflate(d)) assert d == inflate(deflate(d, leave_tuples=True), leave_tuples=True) string_dictionaries = [{"test": {"test": {"test": [1, 2, 3, 4]}}}] for d in string_dictionaries: assert d == inflate(deflate(d, type_encode_key=False), type_decode_key=False) assert d == inflate(deflate(d, leave_tuples=True, type_encode_key=False), leave_tuples=True, type_decode_key=False)
def test_list_encoding(): my_dict = { "root": [ 0, 2, { "weigh":67 } ] } assert match(my_dict, inflate(deflate(my_dict)))
def test_non_list_encoding(): my_dict = { "root": { 0: 0, 1: 2, 2: { "weigh":67 } } } assert match(my_dict, inflate(deflate(my_dict)))
def _sanitize(dictionary: Dict) -> str: """Return given dictionary as JSON string. Parameters ------------------- dictionary: Dict, Dictionary to be converted to JSON. Raises ------------------- ValueError, When the given object is not a dictionary. Returns ------------------- JSON string representation of given dictionary. """ if not isinstance(dictionary, (Dict, List)): raise ValueError( ("Given object to hash is not a dictionary nor a List, " "but a {} object, which is not currently supported.").format( dictionary.__class__.__name__)) return json.dumps(deflate(_convert(dictionary), leave_tuples=True), sort_keys=True)