Team Ai
Apppublic

ever-flow/visualization_modules

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
data_utils.py68 linesDownload Raw Back to src
1import ast2from typing import List3import pandas as pd4 5from config import EXCHANGE_RATES6 7 8def convert_to_usd(value, country):9    """Convert local currency to USD using EXCHANGE_RATES"""10    if pd.isna(value) or pd.isna(country):11        return value12    return value / EXCHANGE_RATES.get(country, 1.0)13 14 15def parse_emtec_list(txt: str) -> List[str]:16    """Safely parse the EMTEC list string"""17    try:18        return [] if pd.isna(txt) or txt in ('[]', '') else ast.literal_eval(txt)19    except Exception:20        return []21 22 23def create_multiple_classification_data(df: pd.DataFrame) -> pd.DataFrame:24    """Generate one row per every EMSEC × EMTEC combination (cross‑product)."""25    expanded_rows: List[pd.Series] = []26 27    for _, row in df.iterrows():28        # ── 1) Collect valid EMSEC levels ------------------------------------------------29        emsec_list = []30        for i in range(1, 6):31            emsec_code = row.get(f'EMSEC{i}')32            if pd.notna(emsec_code):33                emsec_list.append({34                    'sector': row.get(f'EMSEC{i}_Sector', 'Unclassified') or 'Unclassified',35                    'industry': row.get(f'EMSEC{i}_Industry', 'Unclassified') or 'Unclassified',36                    'sub_industry': emsec_code37                })38 39        # ── 2) Collect EMTEC hierarchy ---------------------------------------------------40        lvl1 = parse_emtec_list(row.get('EMTEC_LEVEL1'))41        lvl2 = parse_emtec_list(row.get('EMTEC_LEVEL2'))42        lvl3 = parse_emtec_list(row.get('EMTEC_LEVEL3'))43 44        emtec_combos: List[dict] = []45        if lvl1:46            for l1 in lvl1:47                for l2 in (lvl2 or ['Unclassified']):48                    for l3 in (lvl3 or ['Unclassified']):49                        emtec_combos.append({'theme': l1, 'technology': l2, 'sub_technology': l3})50        else:51            emtec_combos.append({'theme': 'Unclassified', 'technology': 'Unclassified', 'sub_technology': 'Unclassified'})52 53        # ── 3) Produce cross‑product rows ----------------------------------------------54        for emsec in emsec_list:55            for emtec in emtec_combos:56                new_row = row.to_dict()57                new_row.update({58                    'Sector': emsec['sector'],59                    'Industry': emsec['industry'],60                    'Sub_industry': emsec['sub_industry'],61                    'Theme': emtec['theme'],62                    'Technology': emtec['technology'],63                    'Sub_Technology': emtec['sub_technology'],64                })65                expanded_rows.append(new_row)66 67    return pd.DataFrame(expanded_rows)68