<commit_before>#!/usr/bin/env python

# s2p (Satellite Stereo Pipeline) testing module
# Copyright (C) 2017, <NAME> <<EMAIL>>

from __future__ import print_function
import numpy as np
from osgeo import gdal
import argparse
import os

import s2p
import s2plib

def unit_image_keypoints():
    try:
        os.mkdir('s2p_tmp')
    except:
        pass
    kpts = s2plib.sift.image_keypoints('testdata/input_triplet/img_01.tif',100,100,200,200)

    test_kpts = np.loadtxt(kpts)
    ref_kpts  = np.loadtxt('testdata/expected_output/units/unit_image_keypoints.txt')

    # Check that the number of keypoints is the same
    np.testing.assert_equal(test_kpts.shape[0],ref_kpts.shape[0])

    # Check that all keypoints are the same
    np.testing.assert_allclose(test_kpts, ref_kpts, rtol=.01, atol=1)

def end2end_pair():
    s2p.main('testdata/input_pair/config.json')
    computed = gdal.Open('test_pair/dsm.tif').ReadAsArray()
    expected = gdal.Open('testdata/expected_output/pair/dsm.tif').ReadAsArray()
    
    # compare shapes
    np.testing.assert_equal(computed.shape, expected.shape)
    # compare number of valid pixels
    n_computed = np.count_nonzero(np.isfinite(computed))
    n_expected = np.count_nonzero(np.isfinite(expected))
    np.testing.assert_allclose(n_computed, n_expected, rtol=.01, atol=100)
    
    # check largest difference
    print('99th percentile abs difference', 
          np.nanpercentile(np.abs(computed - expected), 99))
    assert(np.nanpercentile(np.abs(computed - expected), 99) < 1)

def end2end_triplet():
    s2p.main('testdata/input_triplet/config.json')
    computed = gdal.Open('test_triplet/dsm.tif').ReadAsArray()
    expected = gdal.Open('testdata/expected_output/triplet/dsm.tif').ReadAsArray()
    
    # compare shapes
    np.testing.assert_equal(computed.shape, expected.shape)
    # compare number of valid pixels
    n_computed = np.count_nonzero(np.isfinite(computed))
    n_expected = np.count_nonzero(np.isfinite(expected))
    np.testing.assert_allclose(n_computed, n_expected, rtol=.01, atol=100)
    
    # check largest difference
    print('99th percentile abs difference', 
          np.nanpercentile(np.abs(computed - expected), 99))
    assert(np.nanpercentile(np.abs(computed - expected), 99) < 1)

# Register tests
registered_tests = { 'unit_image_keypoints' : (unit_image_keypoints,[]),
                     'end2end_pair' : (end2end_pair, []),
                     'end2end_triplet' : (end2end_triplet, [])}

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=('S2P: Satellite Stereo '
                                                  'Pipeline tests suite'))
    
    parser.add_argument('--tests',nargs='+',metavar='test', help='(List of tests to run)')
    parser.add_argument('--all',help=('Run all tests'),action='store_true')

    args = parser.parse_args()

    # If nothing provided, display list of tests
    if not args.tests and not args.all:
        parser.print_help()
        
        print(os.linesep+'available tests:')
        for test,commands in registered_tests.iteritems():
            print('\t'+test)
        exit(1)

    tests_to_run = args.tests

    if args.all:
        tests_to_run = registered_tests.keys()

    print('The following tests will be run: '+str(tests_to_run))
        
    for test in tests_to_run:
        if test in registered_tests:
            print('Running test '+test+'...'+os.linesep)
            command,args = registered_tests[test]
            command(*args)
            print('Success.'+os.linesep)
        else:
            print('Test '+test+' not found')
            exit(1)
    exit(0)
            <commit_msg>Ensure all tests are run even if one fails, and display a summary of failing tests<commit_after>#!/usr/bin/env python

# s2p (Satellite Stereo Pipeline) testing module
# Copyright (C) 2017, <NAME> <<EMAIL>>

from __future__ import print_function
import numpy as np
from osgeo import gdal
import argparse
import os

import s2p
import s2plib

def unit_image_keypoints():
    try:
        os.mkdir('s2p_tmp')
    except:
        pass
    kpts = s2plib.sift.image_keypoints('testdata/input_triplet/img_01.tif',100,100,200,200)

    test_kpts = np.loadtxt(kpts)
    ref_kpts  = np.loadtxt('testdata/expected_output/units/unit_image_keypoints.txt')

    # Check that the number of keypoints is the same
    np.testing.assert_equal(test_kpts.shape[0],ref_kpts.shape[0])
    
    # Check that all keypoints are the same
    np.testing.assert_allclose(test_kpts, ref_kpts, rtol=.01, atol=1)

def end2end_pair():
    s2p.main('testdata/input_pair/config.json')
    computed = gdal.Open('test_pair/dsm.tif').ReadAsArray()
    expected = gdal.Open('testdata/expected_output/pair/dsm.tif').ReadAsArray()
    
    # compare shapes
    np.testing.assert_equal(computed.shape, expected.shape)
    # compare number of valid pixels
    n_computed = np.count_nonzero(np.isfinite(computed))
    n_expected = np.count_nonzero(np.isfinite(expected))
    np.testing.assert_allclose(n_computed, n_expected, rtol=.01, atol=100)
    
    # check largest difference
    print('99th percentile abs difference', 
          np.nanpercentile(np.abs(computed - expected), 99))
    assert(np.nanpercentile(np.abs(computed - expected), 99) < 1)

def end2end_triplet():
    s2p.main('testdata/input_triplet/config.json')
    computed = gdal.Open('test_triplet/dsm.tif').ReadAsArray()
    expected = gdal.Open('testdata/expected_output/triplet/dsm.tif').ReadAsArray()
    
    # compare shapes
    np.testing.assert_equal(computed.shape, expected.shape)
    # compare number of valid pixels
    n_computed = np.count_nonzero(np.isfinite(computed))
    n_expected = np.count_nonzero(np.isfinite(expected))
    np.testing.assert_allclose(n_computed, n_expected, rtol=.01, atol=100)
    
    # check largest difference
    print('99th percentile abs difference', 
          np.nanpercentile(np.abs(computed - expected), 99))
    assert(np.nanpercentile(np.abs(computed - expected), 99) < 1)

# Register tests
registered_tests = { 'unit_image_keypoints' : (unit_image_keypoints,[]),
                     'end2end_pair' : (end2end_pair, []),
                     'end2end_triplet' : (end2end_triplet, [])}

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=('S2P: Satellite Stereo '
                                                  'Pipeline tests suite'))
    
    parser.add_argument('--tests',nargs='+',metavar='test', help='(List of tests to run)')
    parser.add_argument('--all',help=('Run all tests'),action='store_true')

    args = parser.parse_args()

    # If nothing provided, display list of tests
    if not args.tests and not args.all:
        parser.print_help()
        
        print(os.linesep+'available tests:')
        for test,commands in registered_tests.iteritems():
            print('\t'+test)
        exit(1)

    tests_to_run = args.tests

    if args.all:
        tests_to_run = registered_tests.keys()

    print('The following tests will be run: '+str(tests_to_run))

    failed = []
    
    for test in tests_to_run:
        if test in registered_tests:
            print('Running test '+test+'...'+os.linesep)
            command,args = registered_tests[test]
            try:
                command(*args)
                print('Success.'+os.linesep)
            except:
                print('Failure.'+os.linesep)
                failed.append(test)
        else:
            print('Test '+test+' not found')
    
    if len(failed)==0:
        print('All tests passes')
        exit(0)
    else:
        print('The following tests failed:')
        for test in failed:
            print('\t'+test)
        exit(1)
{'fraction_non_alphanumeric': 0.12018170019467878, 'fraction_numerical': 0.01674237508111616, 'mean_word_length': 2.970118495620814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3039852', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2481, 'n_words': 493}