Skip to content

Instantly share code, notes, and snippets.

@manthey
Created July 5, 2023 14:42
Show Gist options
  • Save manthey/39f5f37a023e9cd63bcd9d6f473db3f8 to your computer and use it in GitHub Desktop.
Save manthey/39f5f37a023e9cd63bcd9d6f473db3f8 to your computer and use it in GitHub Desktop.
import json
import os
import tempfile
from pathlib import Path
from .datastore import datastore
class TestNucleiDetection:
def _runTest(self, args):
from histomicstk.cli.NucleiDetection import NucleiDetection
from histomicstk.cli.utils import CLIArgumentParser
parentdir = Path(NucleiDetection.__file__).parent
xmlfile = parentdir / 'NucleiDetection.xml'
# In our tox environment, the xml files aren't always copied
while not xmlfile.exists():
if parentdir.parent == parentdir:
break
parentdir = parentdir.parent
xmlfile = parentdir / 'histomicstk/cli/NucleiDetection/NucleiDetection.xml'
with tempfile.TemporaryDirectory() as tmpdirname:
outpath = os.path.join(tmpdirname, 'result.json')
NucleiDetection.main(CLIArgumentParser(xmlfile).parse_args(
args + [outpath, '--scheduler=multithreading']))
return json.load(open(outpath))
def test_rgb_mag(self):
src = datastore.fetch('tcgaextract_rgbmag.tiff')
annot = self._runTest([src])
assert 2500 < len(annot['elements']) < 3000
def test_rgb_mag_roi(self):
src = datastore.fetch('tcgaextract_rgbmag.tiff')
annot = self._runTest([src, '--analysis_roi=1,1,3998,2998'])
assert 2500 < len(annot['elements']) < 3000
def test_inverted_hematoxylin_mag(self):
src = datastore.fetch('tcgaextract_ihergb_labeledmag.tiff')
annot = self._runTest([src])
assert 2500 < len(annot['elements']) < 3000
def test_hematoxylin_mag(self):
src = datastore.fetch('tcgaextract_ihergb_labeledmag.tiff')
annot = self._runTest([src, '--frame=1', '--output_form=No'])
assert 2500 < len(annot['elements']) < 3000
def test_compositied_mag(self):
src = datastore.fetch('tcgaextract_ihergb_labeledmag.tiff')
annot = self._runTest([src, '--style', json.dumps({
'bands': [
{'palette': '#FF0000', 'framedelta': 3},
{'palette': '#00FF00', 'framedelta': 4},
{'palette': '#0000FF', 'framedelta': 5}
]
})])
assert 2500 < len(annot['elements']) < 3000
# We could add similar tests with the files that don't have magnification
# set, and add similar tests where we pass an ROI that is the whole image
@manthey
Copy link
Author

manthey commented Jul 5, 2023

Instead of multiple tests for each file and combination, you make fewer tests and use pytest.mark.parameterize:

    @pytest.mark.parametrize('filename,params', [
        ('tcgaextract_rgb.tiff', []),
        ('tcgaextract_rgbmag.tiff', []),
        ('tcgaextract_ihergb_labeled.tiff', []),
        ('tcgaextract_ihergb_labeled.tiff', ['--frame', '1', '--output_form', 'No']),
        ('tcgaextract_ihergb_labeled.tiff', ['--style', json.dumps({
            'bands': [
                {'palette': '#FF0000', 'framedelta': 3},
                {'palette': '#00FF00', 'framedelta': 4},
                {'palette': '#0000FF', 'framedelta': 5}
            ]
        })]),
        ('tcgaextract_ihergb_labeledmag.tiff', []),
        ('tcgaextract_ihergb_labeledmag.tiff', ['--frame', '1', '--output_form', 'No']),
        ('tcgaextract_ihergb_labeledmag.tiff', ['--style', json.dumps({
            'bands': [
                {'palette': '#FF0000', 'framedelta': 3},
                {'palette': '#00FF00', 'framedelta': 4},
                {'palette': '#0000FF', 'framedelta': 5}
            ]
        })]),
    ])
    @pytest.mark.parametrize('roi', [[], ['--analysis_roi=1,1,3998,2998']])
    def test_detection(self, filename, params, roi):
        src = datastore.fetch(filename)
        annot = self._runTest([src] + params + roi)
        assert 2500 < len(annot['elements']) < 3000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment