refactoring: splitting conftest logic into submodules
This commit is contained in:
parent
e0b9a3f32e
commit
4fa1ab83b8
102
test/conftest.py
102
test/conftest.py
@ -1,36 +1,27 @@
|
|||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
|
||||||
from functools import partial
|
|
||||||
from itertools import starmap
|
|
||||||
from operator import itemgetter
|
|
||||||
|
|
||||||
import fpdf
|
import fpdf
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pytest
|
import pytest
|
||||||
from funcy import rcompose
|
|
||||||
|
|
||||||
from image_prediction.exceptions import (
|
|
||||||
UnknownLabelFormat,
|
|
||||||
)
|
|
||||||
from image_prediction.image_extractor.extractor import ImageMetadataPair
|
|
||||||
from image_prediction.info import Info
|
from image_prediction.info import Info
|
||||||
from image_prediction.label_mapper.mappers.probability import ProbabilityMapperKeys
|
|
||||||
from image_prediction.locations import TEST_DATA_DIR
|
from image_prediction.locations import TEST_DATA_DIR
|
||||||
from image_prediction.pipeline import load_pipeline
|
from image_prediction.pipeline import load_pipeline
|
||||||
from image_prediction.utils import get_logger
|
from image_prediction.utils import get_logger
|
||||||
from test.utils.generation.pdf import add_image, pdf_stream
|
from test.utils.generation.pdf import add_image, pdf_stream
|
||||||
from test.utils.label import map_labels
|
|
||||||
|
|
||||||
pytest_plugins = [
|
pytest_plugins = [
|
||||||
"test.fixtures.image",
|
"test.fixtures.image",
|
||||||
|
"test.fixtures.image_metadata_pair",
|
||||||
"test.fixtures.input",
|
"test.fixtures.input",
|
||||||
"test.fixtures.label",
|
"test.fixtures.label",
|
||||||
"test.fixtures.metadata",
|
"test.fixtures.metadata",
|
||||||
"test.fixtures.model",
|
"test.fixtures.model",
|
||||||
"test.fixtures.model_store",
|
"test.fixtures.model_store",
|
||||||
"test.fixtures.parameters",
|
"test.fixtures.parameters",
|
||||||
|
"test.fixtures.pdf",
|
||||||
|
"test.fixtures.target",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -43,97 +34,16 @@ def mute_logger():
|
|||||||
logger.setLevel(level)
|
logger.setLevel(level)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def expected_predictions_mapped(
|
|
||||||
label_format, batch_of_expected_string_labels, batch_of_expected_label_to_probability_mappings
|
|
||||||
):
|
|
||||||
if label_format == "index":
|
|
||||||
return batch_of_expected_string_labels
|
|
||||||
elif label_format == "probability":
|
|
||||||
return batch_of_expected_label_to_probability_mappings
|
|
||||||
else:
|
|
||||||
raise UnknownLabelFormat(f"No label mapper for label format {label_format} was specified.")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def expected_predictions(label_format, batch_of_expected_numeric_labels, batch_of_expected_probability_arrays):
|
|
||||||
if label_format == "index":
|
|
||||||
return batch_of_expected_numeric_labels
|
|
||||||
elif label_format == "probability":
|
|
||||||
return batch_of_expected_probability_arrays
|
|
||||||
else:
|
|
||||||
raise UnknownLabelFormat(f"No label mapper for label format {label_format} was specified.")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def input_batch(batch_size, input_size):
|
def input_batch(batch_size, input_size):
|
||||||
return np.random.random_sample(size=(batch_size, *input_size))
|
return np.random.random_sample(size=(batch_size, *input_size))
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=[0, 1, 2, 16, 32])
|
|
||||||
def batch_size(request):
|
|
||||||
return request.param
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def batch_of_expected_string_labels(batch_of_expected_numeric_labels, classes):
|
|
||||||
return map_labels(batch_of_expected_numeric_labels, classes)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def batch_of_expected_numeric_labels(batch_size, classes):
|
|
||||||
return random.choices(range(len(classes)), k=batch_size)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def batch_of_expected_label_to_probability_mappings(batch_of_expected_probability_arrays, classes):
|
|
||||||
def map_probabilities(probabilities):
|
|
||||||
lbl2prob = dict(sorted(zip(classes, map(rounder, probabilities)), key=itemgetter(1), reverse=True))
|
|
||||||
most_likely = [*lbl2prob][0]
|
|
||||||
return {ProbabilityMapperKeys.LABEL: most_likely, ProbabilityMapperKeys.PROBABILITIES: lbl2prob}
|
|
||||||
|
|
||||||
rounder = rcompose(partial(np.round, decimals=4), float)
|
|
||||||
return list(map(map_probabilities, batch_of_expected_probability_arrays))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def batch_of_expected_probability_arrays(batch_size, classes):
|
|
||||||
return [np.random.uniform(size=len(classes)) for _ in range(batch_size)]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def output_batch_generator(expected_predictions):
|
|
||||||
return iter(expected_predictions)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def metadata_plus_mapped_prediction(expected_predictions_mapped, metadata):
|
|
||||||
return [{"classification": epm, **mdt} for epm, mdt in zip(expected_predictions_mapped, metadata)]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def metadata_formatted_plus_mapped_prediction_formatted(expected_predictions_mapped_and_formatted, metadata_formatted):
|
|
||||||
return [
|
|
||||||
{"classification": epm, **mdt}
|
|
||||||
for epm, mdt in zip(expected_predictions_mapped_and_formatted, metadata_formatted)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def expected_predictions_mapped_and_formatted(expected_predictions_mapped):
|
|
||||||
return [{k.value: v for k, v in epm.items()} for epm in expected_predictions_mapped]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def info_label_map():
|
def info_label_map():
|
||||||
return Info
|
return Info
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def image_metadata_pairs(images, metadata):
|
|
||||||
return list(starmap(ImageMetadataPair, zip(images, metadata)))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def pdf(image_metadata_pairs):
|
def pdf(image_metadata_pairs):
|
||||||
pdf = fpdf.FPDF(unit="pt")
|
pdf = fpdf.FPDF(unit="pt")
|
||||||
@ -150,12 +60,6 @@ def real_pdf():
|
|||||||
yield f.read()
|
yield f.read()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def real_expected_service_response():
|
|
||||||
with open(os.path.join(TEST_DATA_DIR, "f2dc689ca794fccb8cd38b95f2bf6ba9_predictions.json"), "r") as f:
|
|
||||||
yield json.load(f)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def pipeline():
|
def pipeline():
|
||||||
pipeline = load_pipeline(verbose=False)
|
pipeline = load_pipeline(verbose=False)
|
||||||
|
|||||||
10
test/fixtures/image_metadata_pair.py
vendored
Normal file
10
test/fixtures/image_metadata_pair.py
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from itertools import starmap
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from image_prediction.image_extractor.extractor import ImageMetadataPair
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def image_metadata_pairs(images, metadata):
|
||||||
|
return list(starmap(ImageMetadataPair, zip(images, metadata)))
|
||||||
5
test/fixtures/parameters.py
vendored
5
test/fixtures/parameters.py
vendored
@ -30,4 +30,9 @@ def height(request):
|
|||||||
|
|
||||||
@pytest.fixture(params=[10, 31])
|
@pytest.fixture(params=[10, 31])
|
||||||
def width(request):
|
def width(request):
|
||||||
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(params=[0, 1, 2, 16, 32])
|
||||||
|
def batch_size(request):
|
||||||
return request.param
|
return request.param
|
||||||
0
test/fixtures/pdf.py
vendored
Normal file
0
test/fixtures/pdf.py
vendored
Normal file
91
test/fixtures/target.py
vendored
Normal file
91
test/fixtures/target.py
vendored
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
from functools import partial
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
from funcy import rcompose
|
||||||
|
|
||||||
|
from image_prediction.exceptions import UnknownLabelFormat
|
||||||
|
from image_prediction.label_mapper.mappers.probability import ProbabilityMapperKeys
|
||||||
|
from image_prediction.locations import TEST_DATA_DIR
|
||||||
|
from test.utils.label import map_labels
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def expected_predictions_mapped(
|
||||||
|
label_format, batch_of_expected_string_labels, batch_of_expected_label_to_probability_mappings
|
||||||
|
):
|
||||||
|
if label_format == "index":
|
||||||
|
return batch_of_expected_string_labels
|
||||||
|
elif label_format == "probability":
|
||||||
|
return batch_of_expected_label_to_probability_mappings
|
||||||
|
else:
|
||||||
|
raise UnknownLabelFormat(f"No label mapper for label format {label_format} was specified.")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def expected_predictions(label_format, batch_of_expected_numeric_labels, batch_of_expected_probability_arrays):
|
||||||
|
if label_format == "index":
|
||||||
|
return batch_of_expected_numeric_labels
|
||||||
|
elif label_format == "probability":
|
||||||
|
return batch_of_expected_probability_arrays
|
||||||
|
else:
|
||||||
|
raise UnknownLabelFormat(f"No label mapper for label format {label_format} was specified.")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def batch_of_expected_string_labels(batch_of_expected_numeric_labels, classes):
|
||||||
|
return map_labels(batch_of_expected_numeric_labels, classes)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def batch_of_expected_numeric_labels(batch_size, classes):
|
||||||
|
return random.choices(range(len(classes)), k=batch_size)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def batch_of_expected_label_to_probability_mappings(batch_of_expected_probability_arrays, classes):
|
||||||
|
def map_probabilities(probabilities):
|
||||||
|
lbl2prob = dict(sorted(zip(classes, map(rounder, probabilities)), key=itemgetter(1), reverse=True))
|
||||||
|
most_likely = [*lbl2prob][0]
|
||||||
|
return {ProbabilityMapperKeys.LABEL: most_likely, ProbabilityMapperKeys.PROBABILITIES: lbl2prob}
|
||||||
|
|
||||||
|
rounder = rcompose(partial(np.round, decimals=4), float)
|
||||||
|
return list(map(map_probabilities, batch_of_expected_probability_arrays))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def batch_of_expected_probability_arrays(batch_size, classes):
|
||||||
|
return [np.random.uniform(size=len(classes)) for _ in range(batch_size)]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def output_batch_generator(expected_predictions):
|
||||||
|
return iter(expected_predictions)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def metadata_plus_mapped_prediction(expected_predictions_mapped, metadata):
|
||||||
|
return [{"classification": epm, **mdt} for epm, mdt in zip(expected_predictions_mapped, metadata)]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def metadata_formatted_plus_mapped_prediction_formatted(expected_predictions_mapped_and_formatted, metadata_formatted):
|
||||||
|
return [
|
||||||
|
{"classification": epm, **mdt}
|
||||||
|
for epm, mdt in zip(expected_predictions_mapped_and_formatted, metadata_formatted)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def expected_predictions_mapped_and_formatted(expected_predictions_mapped):
|
||||||
|
return [{k.value: v for k, v in epm.items()} for epm in expected_predictions_mapped]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def real_expected_service_response():
|
||||||
|
with open(os.path.join(TEST_DATA_DIR, "f2dc689ca794fccb8cd38b95f2bf6ba9_predictions.json"), "r") as f:
|
||||||
|
yield json.load(f)
|
||||||
Loading…
x
Reference in New Issue
Block a user