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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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
from .population import Population
from .molecule import Molecule
from core.predictors.pure_component.property_predictor import PropertyPredictor
from core.config import EvolutionConfig
from crem.crem import mutate_mol
from rdkit import Chem
import pandas as pd
import numpy as np
import random
from typing import List, Tuple, Dict
from core.data_prep import df
from pathlib import Path
from joblib import Parallel, delayed
from .anomaly_detection import EnsembleUncertaintyFilter

class MolecularEvolution:
    """Main evolutionary algorithm coordinator with uncertainty filtering."""
    BASE_DIR = Path(__file__).resolve().parent.parent.parent
    REP_DB_PATH = BASE_DIR / "data" / "fragments" / "diesel_fragments.db"

    # Calibrated uncertainty thresholds β€” shared across all instances in the same process
    _cached_uncertainty_thresholds: dict = {}

    def __init__(self, config: EvolutionConfig):
        self.config = config
        self.predictor = PropertyPredictor(config)
        self.population = Population(config)

        # Initialize and calibrate uncertainty filters
        self._setup_uncertainty_filters()
    
    def _setup_uncertainty_filters(self):
        """Setup and calibrate uncertainty filters for CN and YSI."""
        from core.shared_features import featurize_df

        cache_key = (
            id(self.predictor.predictors.get('cn')),
            id(self.predictor.predictors.get('ysi')),
            self.config.minimize_ysi,
        )
        if cache_key in MolecularEvolution._cached_uncertainty_thresholds:
            self.uncertainty_filters = MolecularEvolution._cached_uncertainty_thresholds[cache_key]
            print("βœ“ Uncertainty filters loaded from cache\n")
            return

        print("\nCalibrating ensemble uncertainty filters...")
        
        val_df = df.sample(200, random_state=42)  # Sample DataFrame, not just SMILES
        val_smiles = val_df["SMILES"].tolist()    # Extract SMILES
        y_val_cn = val_df["cn"].values

        X_val = featurize_df(val_smiles, return_df=False)
        
        if X_val is None:
            print("⚠ Warning: Could not featurize validation set, uncertainty filtering disabled")
            self.uncertainty_filters = {}
            return
        
        self.uncertainty_filters = {}
        
        # Calibrate CN uncertainty filter
        if 'cn' in self.predictor.predictors:
            print("\n  Calibrating CN uncertainty filter...")
            
            
            # Get predictions with uncertainty
            cn_predictor = self.predictor.predictors['cn']
            X_cn = cn_predictor.selector.transform(X_val)
            
            all_tree_preds = np.array(
                Parallel(n_jobs=-1, prefer="threads")(
                    delayed(tree.predict)(X_cn)
                    for tree in cn_predictor.model.estimators_
                )
            )
            cn_variances = np.std(all_tree_preds, axis=0)
            
            # Calculate errors for analysis
            cn_preds = np.mean(all_tree_preds, axis=0)
            cn_errors = np.abs(cn_preds - y_val_cn)
            
            print(f"    Mean prediction error: {cn_errors.mean():.3f}")
            print(f"    Mean variance: {cn_variances.mean():.4f}")
            
            # Calibrate filter
            self.uncertainty_filters['cn'] = EnsembleUncertaintyFilter(percentile_threshold=90)
            self.uncertainty_filters['cn'].calibrate(cn_variances)
            
            # Show effectiveness
            filtered_mask = cn_variances > self.uncertainty_filters['cn'].variance_threshold
            if filtered_mask.sum() > 0:
                print(f"    Will filter: {filtered_mask.sum()}/{len(cn_variances)} ({filtered_mask.mean()*100:.1f}%)")
                print(f"    Filtered have error: {cn_errors[filtered_mask].mean():.3f}")
                print(f"    Kept have error: {cn_errors[~filtered_mask].mean():.3f}")
        
        # Calibrate YSI uncertainty filter (if used)
        if self.config.minimize_ysi and 'ysi' in self.predictor.predictors:
            print("\n  Calibrating YSI uncertainty filter...")
            y_val_ysi = val_df["YSI_Unified_Measured"].values

            
            # Get predictions with uncertainty
            ysi_predictor = self.predictor.predictors['ysi']
            X_ysi = ysi_predictor.selector.transform(X_val)
            
            all_tree_preds = np.array(
                Parallel(n_jobs=-1, prefer="threads")(
                    delayed(tree.predict)(X_ysi)
                    for tree in ysi_predictor.model.estimators_
                )
            )
            ysi_variances = np.std(all_tree_preds, axis=0)
            
            # Calculate errors for analysis
            ysi_preds = np.mean(all_tree_preds, axis=0)
            ysi_errors = np.abs(ysi_preds - y_val_ysi)
            
            print(f"    Mean prediction error: {ysi_errors.mean():.3f}")
            print(f"    Mean variance: {ysi_variances.mean():.4f}")
            
            # Calibrate filter
            self.uncertainty_filters['ysi'] = EnsembleUncertaintyFilter(percentile_threshold=90)
            self.uncertainty_filters['ysi'].calibrate(ysi_variances)
            
            # Show effectiveness
            filtered_mask = ysi_variances > self.uncertainty_filters['ysi'].variance_threshold
            if filtered_mask.sum() > 0:
                print(f"    Will filter: {filtered_mask.sum()}/{len(ysi_variances)} ({filtered_mask.mean()*100:.1f}%)")
                print(f"    Filtered have error: {ysi_errors[filtered_mask].mean():.3f}")
                print(f"    Kept have error: {ysi_errors[~filtered_mask].mean():.3f}")
        
        MolecularEvolution._cached_uncertainty_thresholds[cache_key] = self.uncertainty_filters
        print("\nβœ“ Uncertainty filtering ready\n")

    def _mutate_molecule(self, mol: Chem.Mol) -> List[str]:
        """Generate mutations for a molecule using CREM."""
        try:
            mutants = list(mutate_mol(
                mol,
                db_name=str(self.REP_DB_PATH),
                max_size=self.config.max_size,
                max_replacements=100,
                min_freq=self.config.min_freq,
                return_mol=False
            ))
            return [m for m in mutants if m and m not in self.population.seen_smiles]
        except Exception:
            return []
    
    def _predict_with_uncertainty(self, smiles_list: List[str]) -> Tuple[Dict, Dict]:
        """

        Predict all properties with uncertainty estimates.

        

        Returns:

            predictions: Dict of property predictions

            uncertainties: Dict of uncertainty estimates (std across trees)

        """
        from core.shared_features import featurize_df
        
        if not smiles_list:
            return {}, {}
        
        # Featurize once
        X_full = featurize_df(smiles_list, return_df=False)
        
        if X_full is None:
            return {}, {}
        
        predictions = {}
        uncertainties = {}
        
        # Predict each property with uncertainty
        for prop_name, predictor_obj in self.predictor.predictors.items():
            X_selected = predictor_obj.selector.transform(X_full)

            # Thread-parallel over trees (sklearn trees release the GIL in C)
            all_tree_preds = np.array(
                Parallel(n_jobs=-1, prefer="threads")(
                    delayed(tree.predict)(X_selected)
                    for tree in predictor_obj.model.estimators_
                )
            )

            mean_preds = np.mean(all_tree_preds, axis=0)
            if predictor_obj.uses_log_transform:
                mean_preds = np.power(10.0, mean_preds)
            predictions[prop_name] = mean_preds
            uncertainties[prop_name] = np.std(all_tree_preds, axis=0)
        
        # Add Tanimoto (no uncertainty)
        predictions["tanimoto"] = self.predictor.compute_tanimoto(smiles_list)
        
        return predictions, uncertainties
    
    def _create_molecules(self, smiles_list: List[str]) -> Tuple[List[Molecule], Dict]:
        """Create Molecule objects with UNCERTAINTY FILTERING."""
        if not smiles_list:
            return [], {}
        
        # Track filtering stats
        filter_stats = {
            'total': len(smiles_list),
            'cn_none': 0,
            'ysi_none': 0,
            'tanimoto_fail': 0,
            'cn_uncertainty_fail': 0,
            'ysi_uncertainty_fail': 0,
            'passed': 0
        }
        
        # Predict with uncertainty
        predictions, uncertainties = self._predict_with_uncertainty(smiles_list)
        
        molecules = []
        
        for i, smiles in enumerate(smiles_list):
            # Extract predictions
            props = {k: predictions[k][i] for k in predictions}

            uncerts = {k: v[i] for k, v in uncertainties.items() if k in uncertainties}
            
            # Validate CN exists
            if props.get('cn') is None or not np.isfinite(props['cn']):
                filter_stats['cn_none'] += 1
                continue
            
            # NEW: Filter by CN uncertainty
            if 'cn' in self.uncertainty_filters and 'cn' in uncerts:
                cn_variance = uncerts['cn']
                is_reliable = self.uncertainty_filters['cn'].is_reliable(
                    np.array([cn_variance])
                )[0]
                
                if not is_reliable:
                    filter_stats['cn_uncertainty_fail'] += 1
                    continue
            
            # YSI validation
            if self.config.minimize_ysi:
                if props.get('ysi') is None or not np.isfinite(props['ysi']):
                    filter_stats['ysi_none'] += 1
                    continue
                
                # NEW: Filter by YSI uncertainty
                if 'ysi' in self.uncertainty_filters and 'ysi' in uncerts:
                    ysi_variance = uncerts['ysi']
                    is_reliable = self.uncertainty_filters['ysi'].is_reliable(
                        np.array([ysi_variance])
                    )[0]
                    
                    if not is_reliable:
                        filter_stats['ysi_uncertainty_fail'] += 1
                        continue
            
            # Tanimoto filter
            tanimoto = props.get("tanimoto")
            if tanimoto is None or tanimoto < 0.7:
                filter_stats['tanimoto_fail'] += 1
                continue

            filter_stats['passed'] += 1
            
            mol = Molecule(
            smiles=smiles,
            cn=float(props['cn']),
            cn_error=abs(float(props['cn']) - self.config.target_cn),
            cn_score=0.0,  # placeholder, set below
            bp=float(props.get('bp')) if props.get('bp') is not None else None,
            ysi=float(props.get('ysi')) if props.get('ysi') is not None else None,
            density=float(props.get('density')) if props.get('density') is not None else None,
            lhv=float(props.get('lhv')) if props.get('lhv') is not None else None,
            dynamic_viscosity=float(props.get('dynamic_viscosity')) if props.get('dynamic_viscosity') is not None else None
        )
            mol.cn_score = mol.fitness(self.config)
            molecules.append(mol)
        
        return molecules, filter_stats
    
    def initialize_population(self, initial_smiles: List[str]) -> int:
        """Initialize the population from initial SMILES."""
        print("Predicting properties for initial population...")
        molecules, filter_stats = self._create_molecules(initial_smiles)
        
        # Log initial filtering
        if filter_stats['total'] > 0:
            print(f"\n  Initial filtering: {filter_stats['total']} β†’ {filter_stats['passed']} passed")
            print(f"    Tanimoto: {filter_stats['tanimoto_fail']} | "
                  f"CN Uncertainty: {filter_stats['cn_uncertainty_fail']} | "
                  f"YSI Uncertainty: {filter_stats['ysi_uncertainty_fail']}")
        
        return self.population.add_molecules(molecules)
    
    def _log_generation_stats(self, generation: int):
        """Log statistics for the current generation."""
        mols = self.population.molecules
        if not mols:
            print(f"Gen {generation}: Population empty after filtering.")
            return

        
        if self.config.maximize_cn:
            best_cn = max(mols, key=lambda m: m.cn)
            avg_cn = np.mean([m.cn for m in mols])
            
            print_msg = (f"Gen {generation}/{self.config.generations} | "
                        f"Pop {len(mols)} | "
                        f"Best CN: {best_cn.cn:.3f} | "
                        f"Avg CN: {avg_cn:.3f}")
        else:
            best_cn = min(mols, key=lambda m: m.cn_error)
            avg_cn_err = np.mean([m.cn_error for m in mols])
            
            print_msg = (f"Gen {generation}/{self.config.generations} | "
                        f"Pop {len(mols)} | "
                        f"Best CN err: {best_cn.cn_error:.3f} | "
                        f"Avg CN err: {avg_cn_err:.3f}")
        
        if self.config.minimize_ysi:
            front = self.population.pareto_front()
            best_ysi = min(mols, key=lambda m: m.ysi)
            avg_ysi = np.mean([m.ysi for m in mols])
            
            print_msg += (f" | Best YSI: {best_ysi.ysi:.3f} | "
                         f"Avg YSI: {avg_ysi:.3f} | "
                         f"Pareto: {len(front)}")
        
        print(print_msg)
    
    def _generate_offspring(self, survivors: List[Molecule]) -> Tuple[List[Molecule], Dict]:
        """Generates offspring from survivors with filtering stats."""
        target_count = self.config.population_size - len(survivors)
        max_attempts = target_count * self.config.max_offspring_attempts
        
        all_children = []
        new_molecules = []
        cumulative_stats = {
            'total': 0,
            'cn_none': 0,
            'ysi_none': 0,
            'tanimoto_fail': 0,
            'cn_uncertainty_fail': 0,
            'ysi_uncertainty_fail': 0,
            'passed': 0
        }
        
        print(f"  β†’ Generating offspring (target: {target_count})...")
        
        for attempt in range(max_attempts):
            if len(new_molecules) >= target_count:
                break
            
            # Generate mutations
            # AFTER β€” fitness-proportionate (roulette wheel) selection
            weights = np.array([m.fitness(self.config) for m in survivors])
            weights /= weights.sum()
            parent = survivors[np.random.choice(len(survivors), p=weights)]
            mol = Chem.MolFromSmiles(parent.smiles)
            if mol is None:
                continue
            
            children = self._mutate_molecule(mol)
            all_children.extend(children[:self.config.mutations_per_parent])
            
            # Process in batches
            if len(all_children) >= self.config.batch_size:
                batch_mols, batch_stats = self._create_molecules(all_children)
                new_molecules.extend(batch_mols)
                
                # Accumulate stats
                for key in cumulative_stats:
                    cumulative_stats[key] += batch_stats[key]
                
                all_children = []
        
        # Process remaining children
        if all_children:
            batch_mols, batch_stats = self._create_molecules(all_children)
            new_molecules.extend(batch_mols)
            
            for key in cumulative_stats:
                cumulative_stats[key] += batch_stats[key]
        
        print(f"  βœ“ Generated {len(new_molecules)} valid offspring")
        print(f"    Filtering: {cumulative_stats['total']} β†’ {cumulative_stats['passed']} | "
              f"Tanimoto: {cumulative_stats['tanimoto_fail']} | "
              f"CN Unc: {cumulative_stats['cn_uncertainty_fail']} | "
              f"YSI Unc: {cumulative_stats['ysi_uncertainty_fail']} "
              f"(property constraints applied at end)")
        
        return new_molecules, cumulative_stats
    
    def _run_evolution_loop(self):
        """Run the main evolution loop."""
        for gen in range(1, self.config.generations + 1):
            self._log_generation_stats(gen)
            
            survivors = self.population.get_survivors()
            offspring, _ = self._generate_offspring(survivors)
            
            # Create new population
            new_pop = Population(self.config)
            new_pop.add_molecules(survivors + offspring)
            self.population = new_pop
    
    def _apply_property_filters(self, df: pd.DataFrame) -> pd.DataFrame:
        """Apply bp, density, lhv, dynamic_viscosity constraints to a DataFrame."""
        mask = pd.Series(True, index=df.index)
        for prop, (lo, hi) in self.config.filters.items():
            if prop not in df.columns:
                continue
            col = df[prop]
            if lo is not None:
                mask &= col >= lo
            if hi is not None:
                mask &= col <= hi
        filtered = df[mask]
        removed = len(df) - len(filtered)
        if removed > 0:
            print(f"  Property filters removed {removed}/{len(df)} molecules")
        return filtered

    def _sort_df(self, df: pd.DataFrame) -> pd.DataFrame:
        if self.config.maximize_cn:
            if self.config.minimize_ysi and "ysi" in df.columns:
                candidates = df[df["cn"] > 30] if len(df[df["cn"] > 50]) < 10 else df[df["cn"] > 50]
                return candidates.sort_values(["cn", "ysi"], ascending=[False, True])
            else:
                candidates = df[df["cn"] > 30] if len(df[df["cn"] > 50]) < 10 else df[df["cn"] > 50]
                return candidates.sort_values("cn", ascending=False)
        else:
            if self.config.minimize_ysi and "ysi" in df.columns:
                return df[df["cn_error"] < 15].sort_values(["cn_error", "ysi"], ascending=True)
            else:
                return df[df["cn_error"] < 5].sort_values("cn_error", ascending=True)

    def _generate_results(self) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
        """Generate final results DataFrames (filtered, unfiltered, pareto)."""
        raw_df = self.population.to_dataframe()

        # Unfiltered: only CN/YSI objectives applied, no property constraints
        unfiltered_df = self._sort_df(raw_df).copy()
        unfiltered_df["rank"] = range(1, len(unfiltered_df) + 1)

        # Filtered: property constraints (bp, density, lhv, dynamic_viscosity) applied first
        final_df = self._sort_df(self._apply_property_filters(raw_df)).copy()
        final_df["rank"] = range(1, len(final_df) + 1)

        if self.config.minimize_ysi:
            pareto_mols = self.population.pareto_front()

            pareto_df = pd.DataFrame([m.to_dict() for m in pareto_mols])
            if not pareto_df.empty:
                pareto_df = self._sort_df(pareto_df).copy()
                pareto_df.insert(0, 'rank', range(1, len(pareto_df) + 1))
        else:
            pareto_df = pd.DataFrame()

        return final_df, pareto_df, unfiltered_df
    
    def evolve(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
        """Run the evolutionary algorithm."""
        # Initialize
        df_bins = pd.qcut(df["cn"], q=30)
        initial_smiles = (
            df.groupby(df_bins, observed=False)
            .apply(lambda x: x.sample(20, random_state=42))
            .reset_index(drop=True)["SMILES"]
            .tolist()
        )
        init_count = self.initialize_population(initial_smiles)

        if init_count == 0:
            print("No valid initial molecules")
            return pd.DataFrame(), pd.DataFrame(), pd.DataFrame()

        print(f"βœ“ Initial population size: {init_count}\n")

        # Evolution
        self._run_evolution_loop()

        # Results
        return self._generate_results()