Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from config import all_training_fames | |
| # ----------------------------- | |
| # Detect unsupported FAME columns | |
| # ----------------------------- | |
| def detect_unsupported_fames(df): | |
| """ | |
| Identify uploaded FAME columns that were not used during model training. | |
| Any column containing ':' is treated as a potential FAME column. | |
| Columns not present in the training set are flagged so users can be warned. | |
| Returns: | |
| List of unsupported FAME column names. | |
| """ | |
| uploaded_fame_cols = [c for c in df.columns if ":" in c] | |
| return [c for c in uploaded_fame_cols if c not in all_training_fames] | |
| # ----------------------------- | |
| # Create CSV template | |
| # ----------------------------- | |
| def make_template_csv(): | |
| """ | |
| Generate a blank CSV template for users. | |
| Columns: | |
| - Name (optional): sample identifier | |
| - FAME columns: composition percentages | |
| - CN (optional): measured cetane number for evaluation | |
| Returns: | |
| CSV file (bytes) for download. | |
| """ | |
| template_df = pd.DataFrame(columns=["Name"] + all_training_fames + ["CN"]) | |
| return template_df.to_csv(index=False).encode("utf-8") | |
| # ----------------------------- | |
| # Create example CSV | |
| # ----------------------------- | |
| def make_example_csv(): | |
| """ | |
| Generate a small example CSV for users to test the app. | |
| Includes: | |
| - Sample names | |
| - Realistic FAME compositions | |
| - Measured CN values for evaluation mode | |
| Missing FAME columns are automatically added as zero so that the | |
| file matches the full training schema. | |
| Returns: | |
| CSV file (bytes) for download. | |
| """ | |
| example_df = pd.DataFrame([ | |
| { | |
| "Name": "Neem", | |
| "C16:0": 16.8, | |
| "C18:0": 19.7, | |
| "C18:1": 44.9, | |
| "C18:2": 18.4, | |
| "C18:3": 0.3, | |
| "CN": 51.3 | |
| }, | |
| { | |
| "Name": "Thevetia", | |
| "C16:0": 15.7, | |
| "C18:0": 10.5, | |
| "C18:1": 61.1, | |
| "C18:2": 5.2, | |
| "C18:3": 7.4, | |
| "CN": 57.5 | |
| } | |
| ]) | |
| # Ensure all training FAME columns exist (fill missing with 0). | |
| for col in all_training_fames: | |
| if col not in example_df.columns: | |
| example_df[col] = 0.0 | |
| # Arrange columns in a consistent and user-friendly order. | |
| example_df = example_df[["Name"] + all_training_fames + ["CN"]] | |
| return example_df.to_csv(index=False).encode("utf-8") |