def test_classify_image_joblib(): # use our test config file with algo set to joblib config = 'test_config_files/config_test_joblib.ini' # create mineral classifier instance mc = mineral.MineralClassification( config_file=config, library_file_name=test.libraryFilenames[0]) # for each of the test images for image_file_name in test_classifyImage_testFilenames: # classify the test image classified_file_name = image_file_name[:-4] + "_class_test.hdr" mc.classify_image(image_file_name, classified_file_name) actual = spectral.open_image(classified_file_name) # classified image for comparison expected = spectral.open_image(image_file_name[:-4] + "_class.hdr") # validate metadata assert actual.metadata.get( u'description') == 'COAL ' + pycoal.version + ' mineral ' \ 'classified image.' assert expected.metadata.get(u'file type') == actual.metadata.get( u'file type') assert expected.metadata.get(u'map info') == actual.metadata.get( u'map info') assert expected.metadata.get(u'class names') == actual.metadata.get( u'class names') assert expected.metadata.get(u'classes') == actual.metadata.get( u'classes') # verify that every pixel has the same classification assert numpy.array_equal(expected.asarray(), actual.asarray()) # create mineral classifier instance with image loading enabled mc = mineral.MineralClassification( config_filename=config, library_file_name=test.libraryFilenames[0], in_memory=True) # for each of the test images for image_file_name in test_classifyImage_testFilenames: # classify the test image classified_file_name = image_file_name[:-4] + "_class_test.hdr" mc.classify_image(image_file_name, classified_file_name) actual = spectral.open_image(classified_file_name) # classified image for comparison expected = spectral.open_image(image_file_name[:-4] + "_class.hdr") # verify that every pixel has the same classification assert numpy.array_equal(expected.asarray(), actual.asarray())
def test_classify_image(): # create mineral classifier instance mc = mineral.MineralClassification(library_file_name=libraryFilenames[0]) # for each of the test images for image_file_name in test_classifyImage_testFilenames: # classify the test image classified_file_name = image_file_name[:-4] + "_class_test.hdr" mc.classify_image(image_file_name, classified_file_name) actual = spectral.open_image(classified_file_name) # classified image for comparison expected = spectral.open_image(image_file_name[:-4] + "_class.hdr") # validate metadata assert actual.metadata.get( u'description' ) == 'COAL ' + pycoal.version + ' mineral classified image.' assert expected.metadata.get(u'file type') == actual.metadata.get( u'file type') assert expected.metadata.get(u'map info') == actual.metadata.get( u'map info') assert expected.metadata.get(u'class names') == actual.metadata.get( u'class names') assert expected.metadata.get(u'classes') == actual.metadata.get( u'classes') # verify that every pixel has the same classification assert numpy.array_equal(expected.asarray(), actual.asarray())
def test_classify_image_threshold(): # create mineral classification instance with threshold mc = mineral.MineralClassification(library_file_name=libraryFilenames[0], threshold=0.75) # classify image mc.classify_image(test_classifyImage_threshold_subset_imageFilename, \ test_classifyImage_threshold_subset_testFilename) actual = spectral.open_image( test_classifyImage_threshold_subset_testFilename) # compare expected to actual classifications expected = spectral.open_image( test_classifyImage_threshold_subset_classifiedFilename) for x in range(actual.shape[0]): for y in range(actual.shape[1]): actual_class_id = actual[x, y, 0] actual_class_name = actual.metadata.get( 'class names')[actual_class_id] expected_class_id = expected[x, y, 0] expected_class_name = expected.metadata.get( 'class names')[expected_class_id] assert actual_class_name == expected_class_name \ or actual_class_name == 'No data'
def run_mineral(input_filename=constants.INPUT_FILENAME, library_filename=constants.LIBRARY_FILENAME): """ Run mineral classification. :param input_filename: Input file to be processed :param library_filename: Spectral Library filename """ logging.info( "Starting mineral classification with input file '%s' and spectral " "library '%s'." % ( input_filename, library_filename)) # path to save RGB image rgb_filename = constants.INPUT_NAME + "_rgb.hdr" # path to save mineral classified image classified_filename = constants.INPUT_NAME + "_class.hdr" # path to save classification scores image scores_filename = constants.INPUT_NAME + "_scores.hdr" # create a new mineral classification instance (defaults to SAM algorithm) mineral_classification = mineral.MineralClassification( library_file_name=library_filename, scores_file_name=scores_filename, subset_rows=constants.MINERAL_SUBSET_ROWS, subset_cols=constants.MINERAL_SUBSET_COLS, config_file="../pycoal/config.ini") # generate a georeferenced visible-light image mineral_classification.to_rgb(input_filename, rgb_filename) # generate a mineral classified image mineral_classification.classify_image(input_filename, classified_filename)
def run_mineral(input_filename, library_filename): logging.info( "Starting mineral classification with input file '%s' and spectral " "library '%s'." % ( input_filename, library_filename)) # path to save RGB image rgb_filename = constants.INPUT_NAME + "_rgb.hdr" # path to save mineral classified image classified_filename = constants.INPUT_NAME + "_class.hdr" # path to save classification scores image scores_filename = constants.INPUT_NAME + "_scores.hdr" # list containing the names of all considered classification classes class_names = ["Schwertmannite", "non-Schwertmannite"] # create a new mineral classification instance mineral_classification = mineral.MineralClassification( algorithm=mineral.avngDNN, model_file_name=model_filename, class_names=class_names, scores_file_name=scores_filename) # generate a georeferenced visible-light image mineral_classification.to_rgb(input_filename, rgb_filename) # generate a mineral classified image mineral_classification.classify_image(input_filename, classified_filename)
def test_config_file_wrong_impl(): # create mineral classifier instance, should raise keyerror # in __init__ due to no SAM_p function in globals array config = 'test_config_files/config_test_wrong_impl.ini' with assert_raises(KeyError): _mc = mineral.MineralClassification( library_file_name=test.libraryFilenames[0], config_file=config)
def test_classify_image_subset(): # create mineral classification instance with mining subset mc = mineral.MineralClassification(library_file_name=libraryFilenames[0], class_names=mining.proxy_class_names_usgsv6) # classify image mc.classify_image(test_classifyImage_threshold_subset_imageFilename, \ test_classifyImage_threshold_subset_testFilename) actual = spectral.open_image(test_classifyImage_threshold_subset_testFilename) # inspect the classifications for x in range(actual.shape[0]): for y in range(actual.shape[1]): actual_class_id = actual[x,y,0] actual_class_name = actual.metadata.get('class names')[actual_class_id] assert actual_class_name in mining.proxy_class_names_usgsv6 \ or actual_class_name == 'No data'
def test_classify_image_in_memory(): # create mineral classifier instance with image loading enabled mc = mineral.MineralClassification(library_file_name=libraryFilenames[0], in_memory=True) # for each of the test images for image_file_name in test_classifyImage_testFilenames: # classify the test image classified_file_name = image_file_name[:-4] + "_class_test.hdr" mc.classify_image(image_file_name, classified_file_name) actual = spectral.open_image(classified_file_name) # classified image for comparison expected = spectral.open_image(image_file_name[:-4] + "_class.hdr") # verify that every pixel has the same classification assert numpy.array_equal(expected.asarray(), actual.asarray())
def test_classify_image_subset(): # create mineral classification instance with mining subset mc = mineral.MineralClassification( library_file_name=test.libraryFilenames[0], class_names=mining.PROXY_CLASS_NAMES_USGSV6, config_file="test_config_files/config_test_joblib.ini") # classify image mc.classify_image(test_classifyImage_threshold_subset_imageFilename, test_classifyImage_threshold_subset_testFilename) actual = spectral.open_image( test_classifyImage_threshold_subset_testFilename) # inspect the classifications for x in range(actual.shape[0]): for y in range(actual.shape[1]): actual_class_id = actual[x, y, 0] actual_class_name = actual.metadata.get( 'class names')[actual_class_id] assert actual_class_name in mining.PROXY_CLASS_NAMES_USGSV6 or \ actual_class_name == 'No data'