Diff of /code/utils.py [000000] .. [594161]

Switch to unified view

a b/code/utils.py
1
"""
2
DeepSlide
3
General helper methods used in other functions.
4
5
Authors: Jason Wei, Behnaz Abdollahi, Saeed Hassanpour
6
"""
7
8
import datetime
9
from pathlib import Path
10
from typing import (Dict, List)
11
12
# Valid image extensions.
13
IMAGE_EXTS = [".jpg", ".jpeg", ".png", ".svs", ".tif", ".tiff"]
14
15
16
def get_classes(folder: Path) -> List[str]:
17
    """
18
    Find the classes for classification.
19
20
    Args:
21
        folder: Folder containing the subfolders named by class.
22
23
    Returns:
24
        A list of strings corresponding to the class names.
25
    """
26
    return sorted([f.name for f in folder.iterdir() if
27
                   ((folder.joinpath(f.name).is_dir()) and (".DS_Store" not in f.name))], key=str)
28
29
30
def get_log_csv_name(log_folder: Path) -> Path:
31
    """
32
    Find the name of the CSV file for logging.
33
34
    Args:
35
        log_folder: Folder to save logging CSV file in.
36
37
    Returns:
38
        The path including the filename of the logging CSV file with date information.
39
    """
40
    now = datetime.datetime.now()
41
42
    return log_folder.joinpath(f"log_{now.month}{now.day}{now.year}"
43
                               f"_{now.hour}{now.minute}{now.second}.csv")
44
45
46
def get_image_names(folder: Path) -> List[Path]:
47
    """
48
    Find the names and paths of all of the images in a folder.
49
50
    Args:
51
        folder: Folder containing images (assume folder only contains images).
52
53
    Returns:
54
        A list of the names with paths of the images in a folder.
55
    """
56
    return sorted([Path(f.name) for f in folder.iterdir() if
57
                   ((folder.joinpath(f.name).is_file()) and (".DS_Store" not in f.name) and (f.suffix.casefold() in IMAGE_EXTS))], key=str)
58
59
60
def get_image_paths(folder: Path) -> List[Path]:
61
    """
62
    Find the full paths of the images in a folder.
63
64
    Args:
65
        folder: Folder containing images (assume folder only contains images).
66
67
    Returns:
68
        A list of the full paths to the images in the folder.
69
    """
70
    return sorted([folder.joinpath(f.name) for f in folder.iterdir() if
71
                   ((folder.joinpath(f.name).is_file()) and (".DS_Store" not in f.name) and (f.suffix.casefold() in IMAGE_EXTS))], key=str)
72
73
74
def get_subfolder_paths(folder: Path) -> List[Path]:
75
    """
76
    Find the paths of subfolders.
77
78
    Args:
79
        folder: Folder to look for subfolders in.
80
81
    Returns:
82
        A list containing the paths of the subfolders.
83
    """
84
    return sorted([folder.joinpath(f.name) for f in folder.iterdir() if
85
                   ((folder.joinpath(f.name).is_dir()) and (".DS_Store" not in f.name))], key=str)
86
87
88
def get_all_image_paths(master_folder: Path) -> List[Path]:
89
    """
90
    Finds all image paths in subfolders.
91
92
    Args:
93
        master_folder: Root folder containing subfolders.
94
95
    Returns:
96
        A list of the paths to the images found in the folder.
97
    """
98
    all_paths = []
99
    subfolders = get_subfolder_paths(folder=master_folder)
100
    if len(subfolders) > 1:
101
        for subfolder in subfolders:
102
            all_paths += get_image_paths(folder=subfolder)
103
    else:
104
        all_paths = get_image_paths(folder=master_folder)
105
    return all_paths
106
107
108
def get_csv_paths(folder: Path) -> List[Path]:
109
    """
110
    Find the CSV files contained in a folder.
111
112
    Args:
113
        folder: Folder to search for CSV files.
114
115
    Returns:
116
        A list of the paths to the CSV files in the folder.
117
    """
118
    return sorted([folder.joinpath(f.name) for f in folder.iterdir() if (
119
                (folder.joinpath(f.name).is_file()) and ("csv" in f.name) and (".DS_Store" not in f.name))],
120
                  key=str)
121
122
123
def create_labels(csv_path: Path) -> Dict[str, str]:
124
    """
125
    Read the labels from a CSV file.
126
127
    Args:
128
        csv_path: Path to the CSV file.
129
130
    Returns:
131
        A dictionary mapping string filenames to string labels.
132
    """
133
    with csv_path.open(mode="r") as lines_open:
134
        lines = lines_open.readlines()[1:]
135
136
        file_to_gt_label = {}
137
138
        for line in lines:
139
            if len(line) > 3:
140
                pieces = line[:-1].split(",")
141
                file = pieces[0]
142
                gt_label = pieces[1]
143
                file_to_gt_label[file] = gt_label
144
145
    return file_to_gt_label