Skip to content

Commit

Permalink
Add functional test using planetary rover sample data. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkiri authored and hannah-rae committed Oct 4, 2021
1 parent 957b675 commit 4110577
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
functional: Functional test
46 changes: 46 additions & 0 deletions test/test_planetary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python
# Functional tests with planetary science data.
# Inspired by Gary's PDSC functional tests, e.g.:
# https://github-fn.jpl.nasa.gov/COSMIC/COSMIC_PDSC/blob/master/test/test_ingest.py
#
# Kiri Wagstaff
# 8/19/21

import os
import shutil
import pytest
from unittest import TestCase
from dora_exp_pipeline.dora_exp import start
from test_utils import check_results

@pytest.mark.functional
class TestPlanetary(TestCase):

def setUp(self):

self.outdir = '/tmp/dora/test/planetary'
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)

def test_run(self):

config_file = 'sample_data/planetary_rover/png/exp/navcam.config'

# Run the experiment; also tests ability to parse config file
start(config_file, self.outdir)

# Check results for each algorithm
for file_base in ['demud-k=5/selections-demud.csv',
'iforest/selections-iforest.csv',
'negative_sampling-percent_increase=20/selections-negative_sampling.csv',
'pca-k=5/selections-pca.csv'
]:
correct_file = 'test/ref-results/planetary_rover/%s' % file_base
output_file = '%s/%s' % (self.outdir, file_base)

# Check results against correct output
assert(check_results(output_file, correct_file))

def tearDown(self):
# Remove any intermediate files
shutil.rmtree(self.outdir)
46 changes: 46 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Helper functions for pytests
import numpy as np

def read_selections(f):

sels = []

with open(f, 'r') as inf:
for l in inf:
if l[0] == '#':
continue
sels += [l.split(',')[2].split('.')[0]] # name excluding extension

return sels


def read_selections_and_scores(f):

sels = []
scores = []

with open(f, 'r') as inf:
for l in inf:
if l[0] == '#':
continue
# Get the image filename and numeric score
vals = l.split(',')[2:4]
sels += [vals[0].split('.')[0]] # exclude extension
scores += [float(vals[1])] # convert score to float

return sels, scores


def check_results(outfile, correct_file):

sels, scores = read_selections_and_scores(outfile)
correct_sels, correct_scores = read_selections_and_scores(correct_file)

# Check the selections by name
if sels != correct_sels:
return False

# Check the numeric scores
return np.isclose(scores, correct_scores, atol=1e-6).all()


0 comments on commit 4110577

Please sign in to comment.