Exemple #1
0
    def initialize_class_vars(cls):

        # Load the icons from a sub-directory -- assumes they are present
        #triangle_icon_template_path = importlib_resources.path("hvf_extraction_script.hvf_data.other_icons", "icon_triangle.PNG");
        #print(triangle_icon_template_path.as_posix());

        # Get resource directory - get loader, then cleave off __init__.py
        resource_module_dir, _ = os.path.split(
            pkgutil.get_loader(
                "hvf_extraction_script.hvf_data.other_icons").get_filename())

        triangle_icon_v1_template_path = os.path.join(resource_module_dir,
                                                      'icon_triangle_v1.PNG')
        triangle_icon_v2_template_path = os.path.join(resource_module_dir,
                                                      'icon_triangle_v2.PNG')

        cls.triangle_icon_template_v1 = cv2.cvtColor(
            File_Utils.read_image_from_file(triangle_icon_v1_template_path),
            cv2.COLOR_BGR2GRAY)
        cls.triangle_icon_template_v2 = cv2.cvtColor(
            File_Utils.read_image_from_file(triangle_icon_v2_template_path),
            cv2.COLOR_BGR2GRAY)

        # Lastly, flip the flag to indicate initialization has been done
        cls.is_initialized = True

        return None
Exemple #2
0
    def initialize_class_vars(cls):

        # Load the perc icons from a sub-directory -- assumes they are present
        resource_module_dir, _ = os.path.split(
            pkgutil.get_loader(
                "hvf_extraction_script.hvf_data.perc_icons").get_filename())

        perc_5_template_path = os.path.join(resource_module_dir, "perc_5.JPG")
        perc_2_template_path = os.path.join(resource_module_dir, "perc_2.JPG")
        perc_1_template_path = os.path.join(resource_module_dir, "perc_1.JPG")
        perc_half_template_path = os.path.join(resource_module_dir,
                                               "perc_half.JPG")

        cls.perc_5_template = cv2.cvtColor(
            File_Utils.read_image_from_file(perc_5_template_path),
            cv2.COLOR_BGR2GRAY)
        cls.perc_2_template = cv2.cvtColor(
            File_Utils.read_image_from_file(perc_2_template_path),
            cv2.COLOR_BGR2GRAY)
        cls.perc_1_template = cv2.cvtColor(
            File_Utils.read_image_from_file(perc_1_template_path),
            cv2.COLOR_BGR2GRAY)
        cls.perc_half_template = cv2.cvtColor(
            File_Utils.read_image_from_file(perc_half_template_path),
            cv2.COLOR_BGR2GRAY)

        #cls.perc_5_template = cv2.cvtColor(File_Utils.read_image_from_file("hvf_extraction_script/hvf_data/perc_icons/perc_5.JPG"), cv2.COLOR_BGR2GRAY);
        #cls.perc_2_template = cv2.cvtColor(File_Utils.read_image_from_file("hvf_extraction_script/hvf_data/perc_icons/perc_2.JPG"), cv2.COLOR_BGR2GRAY);
        #cls.perc_1_template = cv2.cvtColor(File_Utils.read_image_from_file("hvf_extraction_script/hvf_data/perc_icons/perc_1.JPG"), cv2.COLOR_BGR2GRAY);
        #cls.perc_half_template = cv2.cvtColor(File_Utils.read_image_from_file("hvf_extraction_script/hvf_data/perc_icons/perc_half.JPG"), cv2.COLOR_BGR2GRAY);

        # Load them into lists for ease of use:
        cls.template_perc_list = [
            cls.perc_5_template, cls.perc_2_template, cls.perc_1_template,
            cls.perc_half_template
        ]

        # Lastly, flip the flag to indicate initialization has been done
        cls.is_initialized = True

        return None
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_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 initialize_class_vars(cls):

        # Load all the icon images for matching:

        # Declare our template dictionaries:
        cls.value_icon_templates = {}
        cls.minus_icon_templates = {}
        cls.less_than_icon_templates = {}

        # Iterate through the icon folders:

        module_list = [
            "hvf_extraction_script.hvf_data.value_icons.v1",
            "hvf_extraction_script.hvf_data.value_icons.v2"
        ]

        for module in module_list:

            module_dir, _ = os.path.split(
                pkgutil.get_loader(module).get_filename())

            head, dir = os.path.split(module_dir)

            # Assume that names are standardized within the directory:

            # Add number value icons (construct file names):

            for ii in range(10):
                # Construct filename:
                value_icon_file_name = 'value_' + str(ii) + '.PNG'

                # Construct full path:
                value_icon_full_path = os.path.join(module_dir,
                                                    value_icon_file_name)
                icon_template = cv2.cvtColor(
                    File_Utils.read_image_from_file(value_icon_full_path),
                    cv2.COLOR_BGR2GRAY)

                # Add to value icon template dictionary:
                if not (ii in cls.value_icon_templates):
                    cls.value_icon_templates[ii] = {}

                cls.value_icon_templates[ii][dir] = icon_template

            # Add minus template:
            minus_icon_full_path = os.path.join(module_dir, 'value_minus.PNG')
            minus_template = cv2.cvtColor(
                File_Utils.read_image_from_file(minus_icon_full_path),
                cv2.COLOR_BGR2GRAY)

            cls.minus_icon_templates[dir] = minus_template

            # Add less than template:
            less_than_full_path = os.path.join(module_dir,
                                               'value_less_than.PNG')
            less_than_template = cv2.cvtColor(
                File_Utils.read_image_from_file(less_than_full_path),
                cv2.COLOR_BGR2GRAY)

            cls.less_than_icon_templates[dir] = less_than_template

        # Lastly, flip the flag to indicate initialization has been done
        cls.is_initialized = True

        return None
# BULK PROCESSING #############################################################
###############################################################################

Logger.set_logger_level(Logger.DEBUG_FLAG_SYSTEM)

# If flag, then do unit tests:
if (args["image_directory"]):

    # Grab the argument directory for readability
    directory = args["image_directory"]

    dict_of_hvf_objs = get_dict_of_hvf_objs_from_imgs(directory)

    return_string = Hvf_Export.export_hvf_list_to_spreadsheet(dict_of_hvf_objs)

    File_Utils.write_string_to_file(return_string, "output_spreadsheet.tsv")

elif (args["text_directory"]):

    # Grab the argument directory for readability
    directory = args["text_directory"]

    Logger.get_logger().log_msg(
        Logger.DEBUG_FLAG_SYSTEM,
        "========== START READING ALL TEXT FILES ==========")
    dict_of_hvf_objs = get_dict_of_hvf_objs_from_text(directory)

    Logger.get_logger().log_msg(
        Logger.DEBUG_FLAG_SYSTEM,
        "========== FINISHED READING ALL TEXT FILES ==========")
args = vars(ap.parse_args())

# Set up the logger module:
#debug_level = Logger.DEBUG_FLAG_INFO;
debug_level = Logger.DEBUG_FLAG_WARNING
#debug_level = Logger.DEBUG_FLAG_DEBUG;
msg_logger = Logger.get_logger().set_logger_level(debug_level)

###############################################################################
# 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 ##########################################################
###############################################################################