db_query / apps /ciq_4g_generator.py
DavMelchi's picture
Add site profile bands for CIQ 3G and 4G
ad9d9f8
from pathlib import Path
import pandas as pd
import streamlit as st
from queries.process_ciq_4g import generate_ciq_4g_excel
from utils.ciq_band_profiles import (
parse_band_profile_definitions,
read_site_profile_mapping,
)
from utils.ciq_excel import get_ciq_sheet_selection_details
st.title(":material/description: CIQ 4G Generator")
st.markdown(
"""
Genere les exports CIQ 4G a partir du CIQ brut.
- **CIQ brut 4G** : obligatoire (format Excel)
"""
)
samples_dir = Path(__file__).resolve().parents[1] / "samples"
sample_ciq_4g = samples_dir / "CIQ_LTE.xlsx"
sample_profile_mapping = samples_dir / "CIQ_BAND_PROFILE_MAPPING.csv"
if sample_ciq_4g.exists() or sample_profile_mapping.exists():
sample_col1, sample_col2 = st.columns(2)
with sample_col1:
if sample_ciq_4g.exists():
st.download_button(
label="Download CIQ 4G sample",
data=sample_ciq_4g.read_bytes(),
file_name=sample_ciq_4g.name,
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
with sample_col2:
if sample_profile_mapping.exists():
st.download_button(
label="Download band profile mapping sample",
data=sample_profile_mapping.read_bytes(),
file_name=sample_profile_mapping.name,
mime="text/csv",
key="ciq4g_profile_mapping_sample",
)
ciq_file = st.file_uploader(
"Upload CIQ brut 4G (Excel)", type=["xlsx", "xls"], key="ciq4g_ciq"
)
ciq_sheet_name = None
if ciq_file is not None:
try:
auto_sheet, sheet_names = get_ciq_sheet_selection_details(ciq_file, "4g")
ciq_sheet_name = st.selectbox(
"Sheet CIQ 4G",
options=sheet_names,
index=sheet_names.index(auto_sheet),
key="ciq4g_sheet_name",
)
if len(sheet_names) == 1:
st.info(f"Classeur a une seule feuille: utilisation de `{ciq_sheet_name}`.")
elif ciq_sheet_name == auto_sheet:
st.info(f"Feuille detectee pour la 4G: `{ciq_sheet_name}`.")
else:
st.info(
f"Feuille 4G choisie manuellement: `{ciq_sheet_name}` (auto: `{auto_sheet}`)."
)
except Exception as exc:
st.warning(f"Detection de feuille 4G impossible: {exc}")
col1, col2 = st.columns(2)
with col1:
year_suffix = st.text_input("Year suffix", value="26", key="ciq4g_year")
with col2:
bands_mode = st.selectbox(
"Bands mode",
options=["Single", "Profiles"],
key="ciq4g_bands_mode",
)
default_bands_value = "G9G18U9U21L8L18L26"
profile_definitions = None
site_profile_mapping = None
if bands_mode == "Single":
bands = st.text_input(
"Bands string",
value=default_bands_value,
key="ciq4g_bands",
)
else:
bands = st.text_input(
"Default bands string",
value=default_bands_value,
key="ciq4g_default_bands",
)
profile_text = st.text_area(
"Band profiles",
value=(
"densif = G9G18U9U21L7L8L18L26\n"
"rural = G9U9L7L8"
),
key="ciq4g_band_profiles",
height=120,
)
site_profile_file = st.file_uploader(
"Upload site profile mapping CSV (site_code,profile)",
type=["csv"],
key="ciq4g_site_profile_map",
)
st.caption("Example mapping: `site_code,profile` then `0273,densif` or `0660,rural`.")
col3, col4 = st.columns(2)
with col3:
mcc = st.number_input("MCC", value=610, step=1, min_value=0, key="ciq4g_mcc")
with col4:
mnc = st.number_input("MNC", value=2, step=1, min_value=0, key="ciq4g_mnc")
if ciq_file is None:
st.info("Upload CIQ brut 4G Excel to generate CIQ 4G export.")
st.stop()
if st.button("Generate", type="primary"):
try:
if bands_mode == "Profiles":
profile_definitions = parse_band_profile_definitions(profile_text)
site_profile_mapping = read_site_profile_mapping(site_profile_file)
with st.spinner("Generating CIQ 4G..."):
sheets, excel_bytes = generate_ciq_4g_excel(
ciq_file,
year_suffix=year_suffix.strip(),
bands=bands.strip(),
mcc=int(mcc),
mnc=int(mnc),
ciq_sheet_name=ciq_sheet_name,
profile_definitions=profile_definitions,
site_profile_mapping=site_profile_mapping,
)
st.session_state["ciq4g_sheets"] = sheets
st.session_state["ciq4g_excel_bytes"] = excel_bytes
st.success("CIQ 4G generated")
except Exception as e:
st.error(f"Error: {e}")
sheets = st.session_state.get("ciq4g_sheets")
excel_bytes = st.session_state.get("ciq4g_excel_bytes")
if sheets:
tab_names = list(sheets.keys())
tabs = st.tabs(tab_names)
for t, name in zip(tabs, tab_names):
with t:
df: pd.DataFrame = sheets[name]
st.dataframe(df, use_container_width=True)
if excel_bytes:
st.download_button(
label="Download CIQ 4G Excel",
data=excel_bytes,
file_name="CIQ_4G.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
type="primary",
)