File size: 2,443 Bytes
1090409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")