hatebr / testing.py
ruanchaves's picture
feat: introduce train, validation and test splits
68d8703
import json
import pytest
from sampling import generate_stratified_indexes
import numpy as np
import pandas as pd
import os
default_url = "https://raw.githubusercontent.com/franciellevargas/HateBR/2d18c5b9410c2dfdd6d5394caa54d608857dae7c/dataset/HateBR.csv"
DATASET_URL = os.environ.get("DATASET_URL", default_url)
@pytest.fixture(scope='module')
def mock_data():
df = pd.read_csv(DATASET_URL)
df.drop("instagram_comments", axis=1, inplace=True)
df = df.head(100)
return df
@pytest.fixture(scope='module')
def indexes():
with open('indexes.json', 'r') as f:
generated_indexes = json.load(f)
return generated_indexes
def test_generate_stratified_indexes_returns_arrays_of_equal_length(mock_data):
X_train_indexes, X_dev_indexes, X_test_indexes, y_train_indexes, y_dev_indexes, y_test_indexes = generate_stratified_indexes(mock_data)
assert len(X_train_indexes) == len(y_train_indexes)
assert len(X_dev_indexes) == len(y_dev_indexes)
assert len(X_test_indexes) == len(y_test_indexes)
def test_generate_stratified_indexes_returns_equal_arrays(mock_data):
X_train_indexes, X_dev_indexes, X_test_indexes, y_train_indexes, y_dev_indexes, y_test_indexes = generate_stratified_indexes(mock_data)
assert np.array_equal(X_train_indexes, y_train_indexes)
assert np.array_equal(X_dev_indexes, y_dev_indexes)
assert np.array_equal(X_test_indexes, y_test_indexes)
def test_no_repeated_indexes(indexes):
train_set = set(indexes['train'])
val_set = set(indexes['validation'])
test_set = set(indexes['test'])
# Check that there are no repeated indexes between sets
assert len(train_set.union(val_set).union(test_set)) == len(train_set) + len(val_set) + len(test_set)
def test_all_indexes_present(indexes):
total_indexes = indexes['train'] + indexes['validation'] + indexes['test']
max_index = max(total_indexes)
# Check that all indexes in range of total combined length are present
assert set(total_indexes) == set(range(max_index+1))