File size: 1,623 Bytes
332cb73
 
 
 
 
 
 
 
 
 
 
 
d29e7cc
 
 
332cb73
d29e7cc
332cb73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150c5ae
d29e7cc
150c5ae
 
 
 
 
 
 
 
 
 
 
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
import ast
import itertools
from collections import Counter
import pandas as pd
import plotly.express as px
from plotly.graph_objs import Figure
import streamlit as st
from typing import Any


def get_elements(x: str, key: str = "elements") -> Any:
    try:
        result = ast.literal_eval(x)[key]
        print(type(result), result)
        if isinstance(result, str):
            return [result]
        return result
    except:
        return []


def get_most_common_elements(df: pd.DataFrame, column: str,  key: str = "elements") -> list:
    built_elements = df[column].apply(get_elements, args=(key,))
    most_common_elements = Counter(itertools.chain.from_iterable(built_elements)).most_common()
    return most_common_elements


def get_plot_from_most_common_elements(df: pd.DataFrame, column: str, key: str = "elements") -> Figure:
    most_common_elements = get_most_common_elements(df, column, key)
    most_common_elements = pd.DataFrame(most_common_elements, columns=[column, "count"])
    return px.bar(
        most_common_elements,
        x=column,
        y="count",
        labels={"count": "# Objects", column: "Built Elements"},
    )


def aggregate_fauna_elements(df: pd.DataFrame) -> dict:
    fauna_elements = {}
    fauna = df["fauna_identification"].apply(lambda x: ast.literal_eval(x)["fauna"])

    for i in range(len(fauna)):
        for element in fauna[i]:
            if element["type"] in fauna_elements:
                fauna_elements[element["type"]] += element["count"]
            else:
                fauna_elements[element["type"]] = element["count"]
    return fauna_elements