File size: 6,221 Bytes
f8bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c10b4
f8bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6c10b4
 
b43dfdf
 
a6c10b4
 
 
 
 
 
f8bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
b43dfdf
 
f8bf7d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from typing import List

from PIL import Image
import pandas as pd

WHALE_CLASSES = [
        "beluga",
        "blue_whale",
        "bottlenose_dolphin",
        "brydes_whale",
        "commersons_dolphin",
        "common_dolphin",
        "cuviers_beaked_whale",
        "dusky_dolphin",
        "false_killer_whale",
        "fin_whale",
        "frasiers_dolphin",
        "gray_whale",
        "humpback_whale",
        "killer_whale",
        "long_finned_pilot_whale",
        "melon_headed_whale",
        "minke_whale",
        "pantropic_spotted_dolphin",
        "pygmy_killer_whale",        
        "rough_toothed_dolphin",
        "sei_whale",
        "short_finned_pilot_whale",
        "southern_right_whale",
        "spinner_dolphin",
        "spotted_dolphin",
        "white_sided_dolphin",
    ]

WHALE_IMAGES = [
        "beluga.webp",
        "blue-whale.webp",
        "bottlenose_dolphin.webp",
        "brydes.webp",
        "common_dolphin.webp",
        "common_dolphin.webp",
        "cuviers_beaked_whale.webp",
        "common_dolphin.webp",
        "false-killer-whale.webp",
        "fin-whale.webp",
        "fin-whale.webp",
        "gray-whale.webp",
        "Humpback.webp",
        "killer_whale.webp",
        "640x427-long-finned-pilot-whale.webp",
        "melon.webp",
        "minke-whale.webp",
        "pantropical-spotted-dolphin.webp",
        "pygmy-killer-whale.webp",
        "rough-toothed-dolphin.webp",
        "sei.webp",
        "Whale_Short-Finned_Pilot-markedDW.png",  ## Background
        "640x427-southern-right-whale.jpg",  ## background
        "spinner.webp",
        "pantropical-spotted-dolphin.webp",  ## duplicate also used for
        "640x427-atlantic-white-sided-dolphin.jpg",  ##background
    ]

WHALE_REFERENCES = [
        "https://www.fisheries.noaa.gov/species/beluga-whale",
        "https://www.fisheries.noaa.gov/species/blue-whale",
        "https://www.fisheries.noaa.gov/species/common-bottlenose-dolphin",
        "https://www.fisheries.noaa.gov/species/brydes-whale",
        "https://en.wikipedia.org/wiki/Commerson's_dolphin",
        #"commersons_dolphin - reference missing - classification to be verified",  ## class matching to be verified
        "https://www.fisheries.noaa.gov/species/short-beaked-common-dolphin",
        "https://www.fisheries.noaa.gov/species/cuviers-beaked-whale",
        "https://en.wikipedia.org/wiki/Dusky_dolphin",
        #"dusky_dolphin - reference missing - classification to be verified",  ## class matching to be verified
        "https://www.fisheries.noaa.gov/species/false-killer-whale",
        "https://www.fisheries.noaa.gov/species/fin-whale",
        "https://www.fisheries.noaa.gov/species/frasers-dolphin",
        #"frasiers_dolphin - reference missing - classification to be verified",  ## class matching to be verified
        "https://www.fisheries.noaa.gov/species/gray-whale",
        "https://www.fisheries.noaa.gov/species/humpback-whale",
        "https://www.fisheries.noaa.gov/species/killer-whale",
        "https://www.fisheries.noaa.gov/species/long-finned-pilot-whale",
        "https://www.fisheries.noaa.gov/species/melon-headed-whale",
        "https://www.fisheries.noaa.gov/species/minke-whale",
        "https://www.fisheries.noaa.gov/species/pantropical-spotted-dolphin",
        "https://www.fisheries.noaa.gov/species/pygmy-killer-whale",
        "https://www.fisheries.noaa.gov/species/rough-toothed-dolphin",
        "https://www.fisheries.noaa.gov/species/sei-whale",
        "https://www.fisheries.noaa.gov/species/short-finned-pilot-whale",
        "https://www.fisheries.noaa.gov/species/southern-right-whale",
        "https://www.fisheries.noaa.gov/species/spinner-dolphin",
        "https://www.fisheries.noaa.gov/species/pantropical-spotted-dolphin",
        "https://www.fisheries.noaa.gov/species/atlantic-white-sided-dolphin",
    ]

# Create a dataframe
df_whale_img_ref = pd.DataFrame(
    {
        "WHALE_CLASSES": WHALE_CLASSES,
        "WHALE_IMAGES": WHALE_IMAGES,
        "WHALE_REFERENCES": WHALE_REFERENCES,
    }
).set_index("WHALE_CLASSES")

def format_whale_name(whale_class:str) -> str:
    """
    Formats a whale class name for display 
    
    Args:
        whale_class (str): The class name of the whale, with words separated by underscores.

    Returns:
        str: The formatted whale name with spaces instead of underscores and each word capitalized.
    """
    whale_name = whale_class.replace("_", " ").title()
    return whale_name


def display_whale(whale_classes:List[str], i:int, viewcontainer=None):
    """
    Display whale image and reference to the provided viewcontainer.

    Args:
        whale_classes (List[str]): A list of whale class names.
        i (int): The index of the whale class to display.
        viewcontainer: The container to display the whale information. If 
            not provided, use the current streamlit context (works via 
            'with `container`' syntax)

    Returns:
        None
    
    TODO: how to find the object type of viewcontainer.? they are just "deltagenerators" but 
    we want the result of the generator.. In any case, it works ok with either call signature.
    """
    import streamlit as st
    if viewcontainer is None:
        viewcontainer = st

    # validate the input i should be within the range of the whale_classes
    if i >= len(whale_classes):
        raise ValueError(f"Index {i} is out of range. The whale_classes list has only {len(whale_classes)} elements.")
    
    # validate the existence of the whale class in the dataframe as a row key
    if whale_classes[i] not in df_whale_img_ref.index:
        raise ValueError(f"Whale class {whale_classes[i]} not found in the dataframe.")
    
    
    viewcontainer.markdown(
        "### :whale:  #" + str(i + 1) + ": " + format_whale_name(whale_classes[i])
    )
    image = Image.open("images/references/" + df_whale_img_ref.loc[whale_classes[i], "WHALE_IMAGES"])

    viewcontainer.image(image, caption=df_whale_img_ref.loc[whale_classes[i], "WHALE_REFERENCES"])
    # link st.markdown(f"[{df.loc[whale_classes[i], 'WHALE_REFERENCES']}]({df.loc[whale_classes[i], 'WHALE_REFERENCES']})")