def get_dict_of_hvf_objs_from_imgs(directory): # Read in images from directory: list_of_image_file_extensions = [".bmp", ".jpg", ".jpeg", ".png"] list_of_img_paths = File_Utils.get_files_within_dir( directory, list_of_image_file_extensions) dict_of_hvf_objs = {} for hvf_img_path in list_of_img_paths: path, filename = os.path.split(hvf_img_path) Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF image " + filename) hvf_img = File_Utils.read_image_from_file(hvf_img_path) hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_img) hvf_obj.release_saved_image() dict_of_hvf_objs[filename] = hvf_obj return dict_of_hvf_objs
def get_hvf_object_from_line(line): line, raw_plot = Hvf_Export.get_hvf_plot_from_line( line, Hvf_Plot_Array.PLOT_RAW, Hvf_Plot_Array.PLOT_VALUE) line, tdv_plot = Hvf_Export.get_hvf_plot_from_line( line, Hvf_Plot_Array.PLOT_TOTAL_DEV, Hvf_Plot_Array.PLOT_VALUE) line, tdp_plot = Hvf_Export.get_hvf_plot_from_line( line, Hvf_Plot_Array.PLOT_TOTAL_DEV, Hvf_Plot_Array.PLOT_PERC) line, pdv_plot = Hvf_Export.get_hvf_plot_from_line( line, Hvf_Plot_Array.PLOT_PATTERN_DEV, Hvf_Plot_Array.PLOT_VALUE) line, pdp_plot = Hvf_Export.get_hvf_plot_from_line( line, Hvf_Plot_Array.PLOT_PATTERN_DEV, Hvf_Plot_Array.PLOT_PERC) # Clean up metadata: for key in line.keys(): line[key] = line.get(key).replace('\"', "").strip() hvf_obj = Hvf_Object(line, raw_plot, tdv_plot, pdv_plot, tdp_plot, pdp_plot, None) return hvf_obj
def get_dict_of_hvf_objs_from_text(directory): # Read in text files from directory: list_of_file_extensions = [".txt"] list_of_txt_paths = File_Utils.get_files_within_dir( directory, list_of_file_extensions) dict_of_hvf_objs = {} for hvf_txt_path in list_of_txt_paths: path, filename = os.path.split(hvf_txt_path) Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF text file " + filename) hvf_txt = File_Utils.read_text_from_file(hvf_txt_path) hvf_obj = Hvf_Object.get_hvf_object_from_text(hvf_txt) dict_of_hvf_objs[filename] = hvf_obj return dict_of_hvf_objs
def test_unit_tests(sub_dir, test_type): # Set up the logger module: debug_level = Logger.DEBUG_FLAG_ERROR msg_logger = Logger.get_logger().set_logger_level(debug_level) # Error check to make sure that sub_dir exists test_dir_path = os.path.join(Hvf_Test.UNIT_TEST_MASTER_PATH, test_type, sub_dir) test_data_path = os.path.join(test_dir_path, Hvf_Test.UNIT_TEST_TEST_DIR) reference_data_path = os.path.join(test_dir_path, Hvf_Test.UNIT_TEST_REFERENCE_DIR) if (not os.path.isdir(test_dir_path)): # This path does not exist! Logger.get_logger().log_msg( Logger.DEBUG_FLAG_ERROR, "Unit test directory \'{}\' does not exist".format( test_dir_path)) return "" if (not os.path.isdir(test_data_path)): # This path does not exist! Logger.get_logger().log_msg( Logger.DEBUG_FLAG_ERROR, "Unit test directory \'{}\' does not exist".format( test_data_path)) return "" if (not os.path.isdir(reference_data_path)): # This path does not exist! Logger.get_logger().log_msg( Logger.DEBUG_FLAG_ERROR, "Unit test directory \'{}\' does not exist".format( reference_data_path)) return "" Logger.get_logger().log_msg( debug_level, "================================================================================" ) Logger.get_logger().log_msg(debug_level, "Starting HVF Unit Testing") Logger.get_logger().log_msg(debug_level, "Test Type: {}".format(test_type)) Logger.get_logger().log_msg(debug_level, "Unit Test Name: {}".format(sub_dir)) # Declare variable to keep track of times, errors, etc # Will be a list of raw data --> we will calculate metrics at the end aggregate_testing_data_dict = {} # For each file in the test folder: for hvf_file in os.listdir(test_data_path): # Skip hidden files: if hvf_file.startswith('.'): continue # Then, find corresponding reference file filename_root, ext = os.path.splitext(hvf_file) reference_hvf_obj = None test_hvf_obj = None # How to generate hvf obj from these files depends on what type of test: if (test_type == Hvf_Test.UNIT_TEST_IMAGE_VS_SERIALIZATION): # Load image, convert to an hvf_obj hvf_image_path = os.path.join(test_data_path, hvf_file) hvf_image = File_Utils.read_image_from_file(hvf_image_path) Logger.get_logger().log_time("Test " + filename_root, Logger.TIME_START) test_hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_image) time_elapsed = Logger.get_logger().log_time( "Test " + filename_root, Logger.TIME_END) serialization_path = os.path.join(reference_data_path, filename_root + ".txt") serialization = File_Utils.read_text_from_file( serialization_path) reference_hvf_obj = Hvf_Object.get_hvf_object_from_text( serialization) elif (test_type == Hvf_Test.UNIT_TEST_IMAGE_VS_DICOM): # Load image, convert to an hvf_obj hvf_image_path = os.path.join(test_data_path, hvf_file) hvf_image = File_Utils.read_image_from_file(hvf_image_path) Logger.get_logger().log_time("Test " + filename_root, Logger.TIME_START) test_hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_image) time_elapsed = Logger.get_logger().log_time( "Test " + filename_root, Logger.TIME_END) dicom_file_path = os.path.join(reference_data_path, filename_root + ".dcm") dicom_ds = File_Utils.read_dicom_from_file(dicom_file_path) reference_hvf_obj = Hvf_Object.get_hvf_object_from_dicom( dicom_ds) elif (test_type == Hvf_Test.UNIT_TEST_SERIALIZATION_VS_DICOM): serialization_file_path = os.path.join(test_data_path, hvf_file) serialization = File_Utils.read_text_from_file( serialization_file_path) test_hvf_obj = Hvf_Object.get_hvf_object_from_text( serialization) dicom_file_path = os.path.join(reference_data_path, filename_root + ".dcm") dicom_ds = File_Utils.read_dicom_from_file(dicom_file_path) reference_hvf_obj = Hvf_Object.get_hvf_object_from_dicom( dicom_ds) time_elapsed = 0 elif (test_type == Hvf_Test.UNIT_TEST_SERIALIZATION_VS_SERIALIZATION): serialization_file_path = os.path.join(test_data_path, hvf_file) serialization = File_Utils.read_text_from_file( serialization_file_path) test_hvf_obj = Hvf_Object.get_hvf_object_from_text( serialization) ref_serialization_path = os.path.join(reference_data_path, filename_root + ".txt") ref_serialization = File_Utils.read_text_from_file( ref_serialization_path) reference_hvf_obj = Hvf_Object.get_hvf_object_from_text( ref_serialization) time_elapsed = 0 else: Logger.get_logger().log_msg( Logger.DEBUG_FLAG_ERROR, "Unrecognized test type \'{}\'".format(test_type)) return "" testing_data_dict, testing_msgs = Hvf_Test.test_hvf_obj( filename_root, reference_hvf_obj, test_hvf_obj, time_elapsed) testing_data_dict["time"] = time_elapsed # Print messages #for msg in testing_msgs: # Logger.get_logger().log_msg(debug_level, msg) aggregate_testing_data_dict[filename_root] = testing_data_dict Hvf_Test.print_unit_test_aggregate_metrics( aggregate_testing_data_dict.values()) metadata_error_header_list = [ "test_name", "field_name", "expected", "actual" ] metadata_error_output = "\t".join(metadata_error_header_list) + "\n" for test in aggregate_testing_data_dict.keys(): for error in aggregate_testing_data_dict[test]["metadata_errors"]: error_string = "\t".join(error.values()) + "\n" metadata_error_output = metadata_error_output + error_string plot_header_list = ["test_name", "location", "expected", "actual"] value_plot_error_output = "\t".join(plot_header_list) + "\n" for test in aggregate_testing_data_dict.keys(): for error in aggregate_testing_data_dict[test][ "value_plot_errors"]: error_string = "\t".join(error.values()) + "\n" value_plot_error_output = value_plot_error_output + error_string perc_plot_error_output = "\t".join(plot_header_list) + "\n" for test in aggregate_testing_data_dict.keys(): for error in aggregate_testing_data_dict[test]["perc_plot_errors"]: error_string = "\t".join(error.values()) + "\n" perc_plot_error_output = perc_plot_error_output + error_string if (True): metadata_error_output = metadata_error_output.encode( 'ascii', 'ignore').decode('unicode_escape') File_Utils.write_string_to_file(metadata_error_output, sub_dir + "_metadata_errors.tsv") File_Utils.write_string_to_file(value_plot_error_output, sub_dir + "_value_plot_errors.tsv") File_Utils.write_string_to_file(perc_plot_error_output, sub_dir + "_perc_plot_errors.tsv") return ""
def test_single_image(hvf_image): # Load image # Set up the logger module: debug_level = Logger.DEBUG_FLAG_SYSTEM #debug_level = Logger.DEBUG_FLAG_INFO; msg_logger = Logger.get_logger().set_logger_level(debug_level) # Instantiate hvf object: Logger.get_logger().log_time("Single HVF image extraction time", Logger.TIME_START) hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_image) debug_level = Logger.DEBUG_FLAG_TIME msg_logger = Logger.get_logger().set_logger_level(debug_level) Logger.get_logger().log_time("Single HVF image extraction time", Logger.TIME_END) # Print the display strings: print(hvf_obj.get_pretty_string()) # Get a serialization string of object: serialization = hvf_obj.serialize_to_json() # Print serialization: print(serialization) # Test to make sure serialization works: hvf_obj2 = Hvf_Object.get_hvf_object_from_text(serialization) serialization2 = hvf_obj2.serialize_to_json() if (serialization == serialization2): Logger.get_logger().log_msg( Logger.DEBUG_FLAG_SYSTEM, "Passed serialization/deserialization consistency") # Check to see if we can release saved images without error: hvf_obj.release_saved_image() hvf_obj2.release_saved_image() Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Passed releasing saved images") else: Logger.get_logger().log_msg( Logger.DEBUG_FLAG_SYSTEM, "FAILED serialization/deserialization consistency ==============" ) print(serialization) Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "=====") print(serialization2) # Check to see if we can release saved images without error: hvf_obj.release_saved_image() hvf_obj2.release_saved_image() #for line in difflib.unified_diff(serialization, serialization2, lineterm=''): # print(line); # Test HVF Metric calculator: Logger.get_logger().log_msg( Logger.DEBUG_FLAG_SYSTEM, "Global CIGTS TDP Score: " + str(Hvf_Metric_Calculator.get_global_cigts_tdp_score(hvf_obj))) if hvf_obj.pat_dev_percentile_array.is_pattern_not_generated(): pdp_cigts = "Cannot calculate as pattern not generated" else: pdp_cigts = str( Hvf_Metric_Calculator.get_global_cigts_pdp_score(hvf_obj)) Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Global CIGTS PDP Score: " + pdp_cigts) # Need to have wait for window instantiation IF the code generates frames - it does # when debugging. Comment for now #cv2.waitKey(0); cv2.destroyAllWindows() return ""
os.mkdir(save_dir) list_of_image_file_extensions = [".bmp", ".jpg", ".jpeg", ".png"] list_of_img_paths = File_Utils.get_files_within_dir( directory, list_of_image_file_extensions) for hvf_img_path in list_of_img_paths: path, filename = os.path.split(hvf_img_path) Logger.get_logger().log_msg(Logger.DEBUG_FLAG_SYSTEM, "Reading HVF image " + filename) hvf_img = File_Utils.read_image_from_file(hvf_img_path) try: hvf_obj = Hvf_Object.get_hvf_object_from_image(hvf_img) file_path = os.path.join(save_dir, str(filename) + ".txt") Logger.get_logger().log_msg( Logger.DEBUG_FLAG_SYSTEM, "Writing text serialization file " + filename) File_Utils.write_string_to_file(hvf_obj.serialize_to_json(), file_path) except: Logger.get_logger().log_msg( Logger.DEBUG_FLAG_SYSTEM, "============= FAILURE on serializing " + filename)
# SINGLE IMAGE TESTING ######################################################## ############################################################################### # If we are passed in an image, read it and show results if (args["image"]): hvf_image = File_Utils.read_image_from_file(args["image"]) Hvf_Test.test_single_image(hvf_image) ############################################################################### # DICOM FILE TESTING ########################################################## ############################################################################### if (args["dicom"]): hvf_dicom = File_Utils.read_dicom_from_file(args["dicom"]) hvf_obj = Hvf_Object.get_hvf_object_from_dicom(hvf_dicom) print(hvf_obj.get_pretty_string()) ############################################################################### # ADD NEW UNIT TESTS ########################################################## ############################################################################### # If given a new file, add to the unit test to the specified collection. This # will use the current version of Hvf_Object to generate the expected result elif (args["add_test_case"]): if not (len(args["add_test_case"]) == 4): Logger.get_logger().log_msg( Logger.DEBUG_FLAG_ERROR, "Incorrect number of arguments, needs 4 (test_name, test_type, ref_data, test_data)" )