File size: 67,263 Bytes
b4123b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
"""
Main pipeline class for the Sorghum Plant Phenotyping Pipeline.

This module orchestrates the entire pipeline from data loading
to feature extraction and result output.
"""

import os
import subprocess
import logging
from pathlib import Path
from typing import Dict, Any, Optional, List, Set
import numpy as np
import cv2
import torch
from torchvision import transforms
from transformers import AutoModelForImageSegmentation
from sklearn.decomposition import PCA
try:
    from tqdm import tqdm
except Exception:
    tqdm = None

from .config import Config
from .data import DataLoader, ImagePreprocessor, MaskHandler
from .features import TextureExtractor, VegetationIndexExtractor, MorphologyExtractor
from .output import OutputManager
from .segmentation import SegmentationManager
# Make occlusion handling optional if the module is not present
try:
    from .segmentation.occlusion_handler import OcclusionHandler  # type: ignore
except Exception:
    OcclusionHandler = None  # type: ignore


class SorghumPipeline:
    """
    Main pipeline class for sorghum plant phenotyping.
    
    This class orchestrates the entire pipeline from data loading
    to feature extraction and result output.
    """
    
    def __init__(self, config_path: Optional[str] = None, config: Optional[Config] = None, include_ignored: bool = False, enable_occlusion_handling: bool = False, enable_instance_integration: bool = False, strict_loader: bool = False, excluded_dates: Optional[List[str]] = None):
        """
        Initialize the pipeline.
        
        Args:
            config_path: Path to configuration file
            config: Configuration object (if not using file)
            include_ignored: Whether to include ignored plants
            enable_occlusion_handling: Whether to enable SAM2Long occlusion handling
        """
        # Setup logging
        self._setup_logging()
        
        # Load configuration
        if config is not None:
            self.config = config
        elif config_path is not None:
            self.config = Config(config_path)
        else:
            raise ValueError("Either config_path or config must be provided")
        
        # Validate configuration
        self.config.validate()
        
        # Store settings
        self.enable_occlusion_handling = enable_occlusion_handling
        self.enable_instance_integration = enable_instance_integration
        self.strict_loader = strict_loader
        self.excluded_dates = excluded_dates or []
        
        # Initialize components
        self._initialize_components(include_ignored)
        
        logger.info("Sorghum Pipeline initialized successfully")
    
    def _setup_logging(self):
        """Setup logging configuration."""
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[
                logging.StreamHandler(),
                logging.FileHandler('sorghum_pipeline.log')
            ]
        )
        global logger
        logger = logging.getLogger(__name__)
    
    def _initialize_components(self, include_ignored: bool = False):
        """Initialize all pipeline components."""
        # Data components
        self.data_loader = DataLoader(
            input_folder=self.config.paths.input_folder,
            debug=True,
            include_ignored=include_ignored,
            strict_loader=self.strict_loader,
            excluded_dates=self.excluded_dates,
        )
        self.preprocessor = ImagePreprocessor(
            target_size=self.config.processing.target_size
        )
        self.mask_handler = MaskHandler(
            min_area=self.config.processing.min_component_area,
            kernel_size=self.config.processing.morphology_kernel_size
        )
        
        # Feature extractors
        self.texture_extractor = TextureExtractor(
            lbp_points=self.config.processing.lbp_points,
            lbp_radius=self.config.processing.lbp_radius,
            hog_orientations=self.config.processing.hog_orientations,
            hog_pixels_per_cell=self.config.processing.hog_pixels_per_cell,
            hog_cells_per_block=self.config.processing.hog_cells_per_block,
            lacunarity_window=self.config.processing.lacunarity_window,
            ehd_threshold=self.config.processing.ehd_threshold,
            angle_resolution=self.config.processing.angle_resolution
        )
        
        self.vegetation_extractor = VegetationIndexExtractor(
            epsilon=self.config.processing.epsilon,
            soil_factor=self.config.processing.soil_factor
        )
        
        self.morphology_extractor = MorphologyExtractor(
            pixel_to_cm=self.config.processing.pixel_to_cm,
            prune_sizes=self.config.processing.prune_sizes
        )
        
        # Segmentation
        self.segmentation_manager = SegmentationManager(
            model_name=self.config.model.model_name,
            device=self.config.get_device(),
            threshold=self.config.processing.segmentation_threshold,
            trust_remote_code=self.config.model.trust_remote_code,
            cache_dir=self.config.model.cache_dir if getattr(self.config.model, 'cache_dir', '') else None,
            local_files_only=getattr(self.config.model, 'local_files_only', False),
        )
        
        # Occlusion handling (optional)
        self.occlusion_handler = None
        if self.enable_occlusion_handling and OcclusionHandler is not None:
            try:
                self.occlusion_handler = OcclusionHandler(
                    device=self.config.get_device(),
                    model="tiny",  # Can be made configurable
                    confidence_threshold=0.5,
                    iou_threshold=0.1
                )
                logger.info("Occlusion handler initialized successfully")
            except Exception as e:
                logger.warning(f"Failed to initialize occlusion handler: {e}")
                logger.warning("Continuing without occlusion handling")
                self.occlusion_handler = None
        elif self.enable_occlusion_handling and OcclusionHandler is None:
            logger.warning("Occlusion handler module not found; continuing without occlusion handling")
        
        # Output manager
        self.output_manager = OutputManager(
            output_folder=self.config.paths.output_folder,
            settings=self.config.output
        )

    def _free_gpu_memory_before_instance(self) -> None:
        """Attempt to free GPU memory prior to running SAM2Long in a subprocess.

        - Moves BRIA segmentation model to CPU if present
        - Deletes the model reference to release VRAM
        - Calls torch.cuda.empty_cache()
        """
        try:
            import torch as _torch  # type: ignore
            # Move BRIA model to CPU and drop reference
            try:
                if getattr(self, 'segmentation_manager', None) is not None:
                    mdl = getattr(self.segmentation_manager, 'model', None)
                    if mdl is not None:
                        try:
                            mdl.to('cpu')
                        except Exception:
                            pass
                        try:
                            delattr(self.segmentation_manager, 'model')
                        except Exception:
                            pass
                        # Ensure attribute exists but is None for future checks
                        try:
                            self.segmentation_manager.model = None  # type: ignore
                        except Exception:
                            pass
            except Exception:
                pass
            # Free CUDA cache
            try:
                if _torch.cuda.is_available():
                    _torch.cuda.empty_cache()
            except Exception:
                pass
            logger.info("Freed GPU memory before SAM2Long invocation (moved BRIA to CPU and emptied cache)")
        except Exception as e:
            logger.warning(f"Failed to free GPU memory before instance segmentation: {e}")
    
    def run(self, load_all_frames: bool = False, segmentation_only: bool = False, filter_plants: Optional[List[str]] = None, filter_frames: Optional[List[str]] = None, run_instance_segmentation: bool = False, features_frame_only: Optional[int] = None, reuse_instance_results: bool = False, instance_mapping_path: Optional[str] = None, force_reprocess: bool = False, respect_instance_frame_rules_for_features: bool = False, substitute_feature_image_from_instance_src: bool = False) -> Dict[str, Any]:
        """
        Run the complete pipeline.
        
        Args:
            load_all_frames: Whether to load all frames or selected frames
            segmentation_only: If True, run segmentation only and skip feature extraction
            
        Returns:
            Dictionary containing all results
        """
        logger.info("Starting Sorghum Pipeline...")
        
        try:
            import time
            total_start = time.perf_counter()
            # Step 1: Load data
            logger.info("Step 1/6: Loading data...")
            # In reuse mode we need all frames to select the mapped frame per plant
            if reuse_instance_results:
                plants = self.data_loader.load_all_frames()
            else:
                # If specific frames are requested, we must load all frames to filter correctly
                if load_all_frames or (filter_frames is not None and len(filter_frames) > 0):
                    plants = self.data_loader.load_all_frames()
                else:
                    plants = self.data_loader.load_selected_frames()
            
            # Optional filter by specific plant names (e.g., ["plant1"]) 
            if filter_plants:
                allowed = set(filter_plants)
                plants = {
                    key: pdata for key, pdata in plants.items()
                    if len(key.split('_')) > 3 and key.split('_')[3] in allowed
                }

            # Optional filter by specific frame numbers (e.g., ["9"] or ["frame9"]) 
            if filter_frames:
                # Normalize to 'frameX' tokens
                wanted = set(
                    [f if str(f).startswith('frame') else f"frame{str(f)}" for f in filter_frames]
                )
                plants = {
                    key: pdata for key, pdata in plants.items()
                    if key.split('_')[-1] in wanted
                }

            if not plants:
                raise ValueError("No plant data loaded")
            
            logger.info(f"Loaded {len(plants)} plants")

            # If reusing instance results with mapping, restrict to exactly the mapped frame per plant (default frame8)
            if reuse_instance_results:
                try:
                    import json as _json
                    if instance_mapping_path is None:
                        raise ValueError("instance_mapping_path is required in reuse mode")
                    _map = _json.load(open(instance_mapping_path, 'r'))
                    # Normalize mapping plant keys and compute target frame (default 8)
                    target_frame_by_plant = {}
                    for pk, pv in _map.items():
                        k_norm = pk if str(pk).startswith('plant') else f"plant{int(pk)}" if str(pk).isdigit() else str(pk)
                        try:
                            target_frame_by_plant[k_norm] = int(pv.get('frame', 8))
                        except Exception:
                            target_frame_by_plant[k_norm] = 8
                    before = len(plants)
                    plants = {
                        key: pdata for key, pdata in plants.items()
                        if (len(key.split('_')) > 3 and key.split('_')[3] in target_frame_by_plant
                            and key.split('_')[-1] == f"frame{target_frame_by_plant[key.split('_')[3]]}")
                    }
                    logger.info(f"Restricted loaded data by mapping frames: {before} -> {len(plants)} items")
                except Exception as e:
                    logger.warning(f"Failed to restrict loaded data by mapping frames: {e}")

            # Skip plants that already have saved results (unless force_reprocess)
            if not force_reprocess:
                try:
                    before = len(plants)
                    filtered = {}
                    for key, pdata in plants.items():
                        parts = key.split('_')
                        if len(parts) < 5:
                            filtered[key] = pdata
                            continue
                        date_key = "_".join(parts[:3])
                        plant_name = parts[3]
                        plant_dir = Path(self.config.paths.output_folder) / date_key / plant_name
                        meta_ok = (plant_dir / 'metadata.json').exists()
                        seg_mask_ok = (plant_dir / self.config.output.segmentation_dir / 'mask.png').exists()
                        if meta_ok or seg_mask_ok:
                            continue
                        filtered[key] = pdata
                    plants = filtered
                    logger.info(f"Skip-existing filter: {before} -> {len(plants)} items to process")
                except Exception as e:
                    logger.warning(f"Skip-existing filter failed: {e}")
            
            # Pre-segmentation borrowing: use plant12 images for plant13 from the start
            try:
                rewired = 0
                borrow_map: Dict[str, str] = {
                    'plant13': 'plant12',
                    'plant14': 'plant13',
                    'plant15': 'plant14',
                    'plant16': 'plant15',
                }
                for _k in list(plants.keys()):
                    _parts = _k.split('_')
                    # Expect keys like YYYY_MM_DD_plantX_frameY
                    if len(_parts) < 5:
                        continue
                    _date_key = "_".join(_parts[:3])
                    _plant_name = _parts[3]
                    _frame_token = _parts[4]
                    # Do NOT borrow on 2025_05_08
                    if _date_key == '2025_05_08':
                        continue
                    if _plant_name not in borrow_map:
                        continue
                    _src_plant = borrow_map[_plant_name]
                    _src_key = f"{_date_key}_{_src_plant}_{_frame_token}"
                    _src = plants.get(_src_key)
                    if not _src:
                        # Fallback: load raw image for source plant directly from disk
                        try:
                            from PIL import Image as _Image
                            _date_folder = _date_key.replace('_', '-')
                            _frame_num = int(_frame_token.replace('frame', ''))
                            _date_dir = Path(self.config.paths.input_folder)
                            # If input folder is a parent of dates, append date folder
                            if _date_dir.name != _date_folder:
                                _date_dir = _date_dir / _date_folder
                            _frame_path = _date_dir / _src_plant / f"{_src_plant}_frame{_frame_num}.tif"
                            if _frame_path.exists():
                                _img = _Image.open(str(_frame_path))
                                _src = {"raw_image": (_img, _frame_path.name), "plant_name": _plant_name, "file_path": str(_frame_path)}
                            else:
                                _src = None
                        except Exception:
                            _src = None
                    if not _src:
                        continue
                    _tgt = plants[_k]
                    # Preserve original raw image once
                    if 'raw_image' in _tgt and 'raw_image_original' not in _tgt:
                        _tgt['raw_image_original'] = _tgt['raw_image']
                    if 'raw_image' in _src:
                        _tgt['raw_image'] = _src['raw_image']
                        _tgt['borrowed_from'] = _src_plant
                        rewired += 1
                if rewired > 0:
                    logger.info(f"Pre-seg borrowing applied: rewired {rewired} frames for plants 13/14/15/16")
            except Exception as e:
                logger.warning(f"Pre-seg borrowing failed: {e}")

            # Step 2: Create composites
            logger.info("Step 2/6: Creating composites...")
            step_start = time.perf_counter()
            plants = self.preprocessor.create_composites(plants)
            logger.info(f"Composites done in {(time.perf_counter()-step_start):.2f}s")
            
            # Step 3: Segment plants (optionally with bounding boxes)
            logger.info("Step 3/6: Segmenting plants...")
            step_start = time.perf_counter()
            bbox_lookup = None
            try:
                bbox_dir = getattr(self.config.paths, 'boundingbox_dir', None)
                # Default to project BoundingBox dir if unset or falsy
                if not bbox_dir:
                    try:
                        self.config.paths.boundingbox_dir = "/home/grads/f/fahimehorvatinia/Documents/my_full_project/BoundingBox"
                        bbox_dir = self.config.paths.boundingbox_dir
                    except Exception:
                        bbox_dir = None
                if bbox_dir:
                    bbox_lookup = self.data_loader.load_bounding_boxes(bbox_dir)
                    logger.info(f"Loaded bounding boxes from {bbox_dir}")
            except Exception as e:
                logger.warning(f"Failed to load bounding boxes: {e}")
                bbox_lookup = None
            plants = self._segment_plants(plants, bbox_lookup)
            logger.info(f"Segmentation done in {(time.perf_counter()-step_start):.2f}s")
            
            # Step 3.5: Handle occlusion if enabled
            if self.enable_occlusion_handling and self.occlusion_handler is not None:
                logger.info("Step 3.5/6: Handling occlusion with SAM2Long...")
                step_start = time.perf_counter()
                plants = self._handle_occlusion(plants)
                logger.info(f"Occlusion handling done in {(time.perf_counter()-step_start):.2f}s")

            # Optional: Export RMBG maskouts with white background and run instance segmentation
            if (run_instance_segmentation or self.enable_instance_integration) and not reuse_instance_results:
                if not load_all_frames:
                    logger.warning("Instance segmentation expects all 13 frames; consider running with load_all_frames=True.")
                logger.info("Step 3.6: Exporting white-background RMBG images for instance segmentation...")
                # Derive date-specific export/result directories when a single date is present
                date_keys = set()
                try:
                    for _k in plants.keys():
                        _p = _k.split('_')
                        if len(_p) >= 3:
                            date_keys.add("_".join(_p[:3]))
                except Exception:
                    pass
                if len(date_keys) == 1:
                    date_key = next(iter(date_keys))
                    base_dir = Path(self.config.paths.output_folder) / date_key
                    export_dir = base_dir / "instance_input_maskouts"
                    instance_results_dir = base_dir / "instance_results"
                else:
                    export_dir = Path(self.config.paths.output_folder) / "instance_input_maskouts"
                    instance_results_dir = Path(self.config.paths.output_folder) / "instance_results"
                export_dir.mkdir(parents=True, exist_ok=True)
                instance_results_dir.mkdir(parents=True, exist_ok=True)
                self._export_white_background_maskouts(plants, export_dir)

                logger.info("Invoking final SAM2Long instance segmentation on exported images...")
                # Free GPU memory before launching SAM2Long to avoid CUDA OOM
                self._free_gpu_memory_before_instance()
                env = os.environ.copy()
                env["SAM2LONG_IMAGES_DIR"] = str(export_dir)
                env["SAM2LONG_RESULTS_DIR"] = str(instance_results_dir)
                # Ensure instance outputs include all frames for all dates
                try:
                    env.pop("INSTANCE_OUTPUT_FRAMES", None)
                except Exception:
                    pass
                script_path = "/home/grads/f/fahimehorvatinia/Documents/my_full_project/Experiments3_code/sam2long_instance_integration.py"
                try:
                    subprocess.run(["python", script_path], check=True, env=env)
                except subprocess.CalledProcessError as e:
                    logger.error(f"Instance segmentation failed: {e}")
                else:
                    # Integrate instance masks (track_0 as target) into pdata before feature extraction
                    try:
                        self._apply_instance_masks(plants, instance_results_dir)
                        logger.info("Applied instance segmentation masks to pipeline data")
                    except Exception as e:
                        logger.warning(f"Failed to apply instance masks: {e}")
            elif reuse_instance_results:
                # Reuse existing instance masks from mapping file
                if instance_mapping_path is None:
                    raise ValueError("reuse_instance_results=True requires instance_mapping_path to be provided")
                try:
                    self._apply_instance_masks_from_mapping(plants, Path(instance_mapping_path))
                    logger.info("Applied instance masks from mapping file")
                except Exception as e:
                    logger.error(f"Failed to apply instance masks from mapping: {e}")
            
            if not segmentation_only:
                # If reusing instance results with a mapping, restrict features to mapped frames per plant
                if reuse_instance_results and instance_mapping_path is not None:
                    try:
                        import json as _json
                        _map = _json.load(open(instance_mapping_path, 'r'))
                        # Normalize map
                        _norm = {}
                        for pk, pv in _map.items():
                            k_norm = pk if str(pk).startswith('plant') else f"plant{int(pk)}" if str(pk).isdigit() else str(pk)
                            _norm[k_norm] = int(pv.get('frame', 8))
                        before = len(plants)
                        plants = {
                            k: v for k, v in plants.items()
                            if len(k.split('_')) > 3 and k.split('_')[3] in _norm and k.split('_')[-1] == f"frame{_norm[k.split('_')[3]]}"
                        }
                        logger.info(f"Restricted feature extraction by mapping: {before} -> {len(plants)} items")
                    except Exception as e:
                        logger.warning(f"Failed to restrict by mapping frames: {e}")
                # Optional: restrict features to per-plant preferred frame using internal frame rules
                if respect_instance_frame_rules_for_features:
                    try:
                        # Keep this in sync with _apply_instance_masks frame_rules
                        frame_rules: Dict[str, int] = {
                            "plant33": 2,
                            "plant16": 4,
                            "plant19": 5,
                            "plant26": 8,
                            "plant27": 8,
                            "plant29": 8,
                            "plant35": 7,
                            "plant36": 6,
                            "plant37": 2,
                            "plant45": 5,
                        }
                        before = len(plants)
                        def _keep(k: str) -> bool:
                            parts = k.split('_')
                            if len(parts) < 2:
                                return False
                            plant_name = parts[-2]
                            frame_token = parts[-1]
                            if not (plant_name.startswith('plant') and frame_token.startswith('frame')):
                                return False
                            desired = frame_rules.get(plant_name, 8)
                            return frame_token == f"frame{desired}"
                        plants = {k: v for k, v in plants.items() if _keep(k)}
                        logger.info(f"Restricted feature extraction by per-plant frame rules: {before} -> {len(plants)} items")
                    except Exception as e:
                        logger.warning(f"Failed to apply per-plant frame restriction for features: {e}")

                # Optional: if features_frame_only set, keep only that frame's entries (global single frame)
                if features_frame_only is not None:
                    frame_token = f"frame{features_frame_only}"
                    plants = {k: v for k, v in plants.items() if k.split('_')[-1] == frame_token}
                    logger.info(f"Restricted feature extraction to {len(plants)} items for {frame_token}")

                # Optional: substitute feature input image from instance src_rules mapping (e.g., plant14 <- plant13)
                if substitute_feature_image_from_instance_src:
                    try:
                        src_rules: Dict[str, str] = {
                            "plant13": "plant12",
                            "plant14": "plant13",
                            "plant15": "plant14",
                            "plant16": "plant15",
                        }
                        switched = 0
                        for key in list(plants.keys()):
                            parts = key.split('_')
                            if len(parts) < 5:
                                continue
                            date_key = "_".join(parts[:3])
                            plant_name = parts[3]
                            frame_token = parts[-1]
                            if plant_name not in src_rules:
                                continue
                            src_plant = src_rules[plant_name]
                            src_key = f"{date_key}_{src_plant}_{frame_token}"
                            if src_key not in plants:
                                continue
                            src_pdata = plants[src_key]
                            tgt_pdata = plants[key]
                            # Preserve the original composite used for segmentation for correct overlays later
                            try:
                                if 'composite' in tgt_pdata and 'segmentation_composite' not in tgt_pdata:
                                    tgt_pdata['segmentation_composite'] = tgt_pdata['composite']
                            except Exception:
                                pass
                            # Swap feature inputs: composite and spectral bands
                            if 'composite' in src_pdata:
                                tgt_pdata['composite'] = src_pdata['composite']
                            if 'spectral_stack' in src_pdata:
                                tgt_pdata['spectral_stack'] = src_pdata['spectral_stack']
                            # Ensure mask aligns with substituted composite; resize if needed
                            try:
                                import cv2 as _cv2
                                import numpy as _np
                                comp = tgt_pdata.get('composite')
                                msk = tgt_pdata.get('mask')
                                if comp is not None and msk is not None:
                                    ch, cw = comp.shape[:2]
                                    mh, mw = msk.shape[:2]
                                    if (mh, mw) != (ch, cw):
                                        resized = _cv2.resize(msk.astype('uint8'), (cw, ch), interpolation=_cv2.INTER_NEAREST)
                                        tgt_pdata['mask'] = resized
                                        if 'soft_mask' in tgt_pdata and isinstance(tgt_pdata['soft_mask'], _np.ndarray):
                                            tgt_pdata['soft_mask'] = (resized > 0).astype(_np.float32)
                                    # Precompute masked composite with white background for saving
                                    white = _np.full_like(comp, 255, dtype=_np.uint8)
                                    result = white.copy()
                                    result[tgt_pdata['mask'] > 0] = comp[tgt_pdata['mask'] > 0]
                                    tgt_pdata['masked_composite'] = result
                            except Exception:
                                pass
                            switched += 1
                        if switched > 0:
                            logger.info(f"Substituted feature images from src_rules for {switched} items")
                    except Exception as e:
                        logger.warning(f"Failed feature-image substitution via src_rules: {e}")
                # Step 4: Extract features
                logger.info("Step 4/6: Extracting features...")
                step_start = time.perf_counter()
                # Stream-save mode: save outputs immediately after each plant's features when fast output is enabled
                stream_save = False
                try:
                    import os as _os
                    stream_save = bool(int(_os.environ.get('STREAM_SAVE', '0'))) or bool(getattr(self.output_manager, 'fast_mode', False))
                except Exception:
                    stream_save = False

                plants = self._extract_features(plants, stream_save=stream_save)
                logger.info(f"Features done in {(time.perf_counter()-step_start):.2f}s")
                
                # Step 5: Generate outputs (skip if already stream-saved)
                if not stream_save:
                    logger.info("Step 5/6: Generating outputs...")
                    step_start = time.perf_counter()
                    self._generate_outputs(plants)
                    logger.info(f"Outputs done in {(time.perf_counter()-step_start):.2f}s")
                
                # Step 6: Create summary
                logger.info("Step 6/6: Creating summary...")
                summary = self._create_summary(plants)
            else:
                logger.info("Segmentation-only mode: skipping texture/vegetation/morphology features and plots")
                # Segmentation-only: generate only segmentation outputs and a minimal summary
                logger.info("Step 4/4: Generating segmentation outputs (segmentation-only mode)...")
                self._generate_outputs(plants)
                summary = {
                    "total_plants": len(plants),
                    "successful_plants": len(plants),
                    "failed_plants": 0,
                    "features_extracted": {
                        "texture": 0,
                        "vegetation": 0,
                        "morphology": 0
                    }
                }
            
            total_time = time.perf_counter() - total_start
            logger.info(f"Pipeline completed successfully in {total_time:.2f}s!")
            return {
                "plants": plants,
                "summary": summary,
                "config": self.config,
                "timing_seconds": total_time
            }
            
        except Exception as e:
            logger.error(f"Pipeline failed: {e}")
            raise

    def _export_white_background_maskouts(self, plants: Dict[str, Any], out_dir: Path) -> None:
        """Export RMBG composites with white background using the soft/binary masks.

        Filenames follow: plantX_plantX_frameY_maskout.png so the final instance script can detect plants.
        """
        # Clear any previous maskouts to avoid processing stale plants
        try:
            if out_dir.exists():
                for p in out_dir.glob("*_maskout.png"):
                    try:
                        p.unlink()
                    except Exception:
                        pass
        except Exception:
            pass
        count = 0
        # Per-plant rule: use bbox-only (skip SAM2Long) for these plants on all dates except 2025_05_08
        bbox_only_plants: Set[str] = {"plant19", "plant20", "plant27", "plant33", "plant39", "plant42", "plant44", "plant46"}
        date_exception = "2025_05_08"
        for key, pdata in plants.items():
            try:
                # key format: "YYYY_MM_DD_plantX_frameY"
                parts = key.split('_')
                if len(parts) < 3:
                    continue
                plant_name = parts[-2]
                frame_token = parts[-1]  # e.g., frame8
                if not plant_name.startswith('plant') or not frame_token.startswith('frame'):
                    continue
                date_key = "_".join(parts[:3])
                if (plant_name in bbox_only_plants) and (date_key != date_exception):
                    # Skip exporting maskouts for bbox-only plants so SAM2Long does not run on them
                    continue
                # Extract frame number
                frame_num = int(frame_token.replace('frame', ''))
                composite = pdata.get('composite')
                mask = pdata.get('mask')
                if composite is None or mask is None:
                    continue
                # Ensure 3-channel BGR
                if len(composite.shape) == 2:
                    composite_bgr = cv2.cvtColor(composite, cv2.COLOR_GRAY2BGR)
                else:
                    composite_bgr = composite
                out_img = composite_bgr.copy()
                # Set background to white where mask == 0
                out_img[mask == 0] = (255, 255, 255)
                out_path = out_dir / f"{plant_name}_{plant_name}_{frame_token}_maskout.png"
                cv2.imwrite(str(out_path), out_img)
                count += 1
            except Exception as e:
                logger.warning(f"Failed to export maskout for {key}: {e}")
        logger.info(f"Exported {count} white-background maskouts to {out_dir}")
    
    def _segment_plants(self, plants: Dict[str, Any], 
                       bbox_lookup: Optional[Dict[str, tuple]]) -> Dict[str, Any]:
        """Segment plants using BRIA model.

        If bbox_lookup is provided and contains an entry for the plant (e.g., 'plant1'),
        the image is cropped/masked to the bounding box region before segmentation and the
        predicted mask is mapped back to the full image size. In bbox mode a largest
        connected component post-processing is applied to obtain a clean target mask.
        """
        total = len(plants)
        iterator = plants.items()
        if tqdm is not None:
            iterator = tqdm(list(plants.items()), desc="Segmenting", total=total, unit="img", leave=False)
        for idx, (key, pdata) in enumerate(iterator):
            try:
                # Get composite image
                composite = pdata['composite']
                h, w = composite.shape[:2]
                
                # Determine bbox for this plant if available
                parts = key.split('_')
                plant_name = parts[-2] if len(parts) >= 2 else None
                date_key = "_".join(parts[:3]) if len(parts) >= 3 else None  # e.g., 2025_04_16
                bbox = None
                if bbox_lookup is not None and plant_name is not None:
                    # keys in bbox_lookup are typically like 'plant1'
                    bbox = bbox_lookup.get(plant_name)
                # For plant33, ignore any bbox and run full-image segmentation on all dates except the exception
                if plant_name == 'plant33' and date_key != '2025_05_08':
                    bbox = None
                
                # Plants that should use the bounding box itself as the mask (skip model)
                bbox_only_plants: Set[str] = {"plant19", "plant20", "plant27", "plant39", "plant42", "plant44", "plant46"}
                use_bbox_only = (plant_name in bbox_only_plants)

                # Do not use bounding boxes for date 2025_05_08
                if date_key == '2025_05_08':
                    bbox = None

                if bbox is not None:
                    # Clamp bbox to image
                    x1, y1, x2, y2 = bbox
                    x1 = max(0, min(w, int(x1)))
                    x2 = max(0, min(w, int(x2)))
                    y1 = max(0, min(h, int(y1)))
                    y2 = max(0, min(h, int(y2)))
                    if x2 <= x1 or y2 <= y1:
                        x1, y1, x2, y2 = 0, 0, w, h

                    if use_bbox_only:
                        # Use the bbox as the mask directly (255 inside, 0 outside)
                        soft_full = np.zeros((h, w), dtype=np.float32)
                        soft_full[y1:y2, x1:x2] = 1.0
                        bin_full = np.zeros((h, w), dtype=np.uint8)
                        bin_full[y1:y2, x1:x2] = 255
                        pdata['soft_mask'] = soft_full
                        pdata['mask'] = bin_full
                    else:
                        # Segment inside the bbox region and map back
                        crop = composite[y1:y2, x1:x2]
                        soft_mask_crop = self.segmentation_manager.segment_image_soft(crop)
                        soft_full = np.zeros((h, w), dtype=np.float32)
                        soft_resized = cv2.resize(soft_mask_crop, (x2 - x1, y2 - y1), interpolation=cv2.INTER_LINEAR)
                        soft_full[y1:y2, x1:x2] = soft_resized
                        bin_full = (soft_full > 0.5).astype(np.uint8) * 255
                        try:
                            n_lbl, labels, stats, _ = cv2.connectedComponentsWithStats(bin_full, 8)
                            if n_lbl > 1:
                                largest = 1 + int(np.argmax(stats[1:, cv2.CC_STAT_AREA]))
                                bin_full = (labels == largest).astype(np.uint8) * 255
                        except Exception:
                            pass
                        pdata['soft_mask'] = soft_full.astype(np.float32)
                        pdata['mask'] = bin_full.astype(np.uint8)
                else:
                    # Full-image segmentation (no bbox)
                    soft_mask = self.segmentation_manager.segment_image_soft(composite)
                    pdata['soft_mask'] = soft_mask
                    pdata['mask'] = (soft_mask * 255.0).astype(np.uint8)
                
                # Progress log every 25 items and for first/last
                if tqdm is None and (idx == 0 or (idx + 1) % 25 == 0 or (idx + 1) == total):
                    logger.info(f"Segmented {idx + 1}/{total}: {key}")
                
            except Exception as e:
                logger.error(f"Segmentation failed for {key}: {e}")
                pdata['soft_mask'] = np.zeros(composite.shape[:2], dtype=np.float32)
                pdata['mask'] = np.zeros(composite.shape[:2], dtype=np.uint8)
        
        return plants
    
    def _handle_occlusion(self, plants: Dict[str, Any]) -> Dict[str, Any]:
        """
        Handle occlusion problems using SAM2Long.
        
        This method groups plants by their base plant ID and processes
        each plant's 13-frame sequence to differentiate target plant
        from neighboring plants.
        
        Args:
            plants: Dictionary of plant data
            
        Returns:
            Updated plant data with occlusion handling results
        """
        if self.occlusion_handler is None:
            logger.warning("Occlusion handler not available, skipping occlusion handling")
            return plants
        
        # Group plants by base plant ID (e.g., "plant1" from "plant1_plant1_frame1")
        plant_groups = {}
        for key, pdata in plants.items():
            # Extract plant ID from key like "plant1_plant1_frame1"
            parts = key.split('_')
            if len(parts) >= 3:
                plant_id = parts[0]  # e.g., "plant1"
                if plant_id not in plant_groups:
                    plant_groups[plant_id] = []
                plant_groups[plant_id].append((key, pdata))
        
        logger.info(f"Processing {len(plant_groups)} plant groups for occlusion handling")
        
        # Process each plant group
        for plant_id, plant_frames in plant_groups.items():
            try:
                # Sort frames by frame number
                plant_frames.sort(key=lambda x: int(x[0].split('_')[-1].replace('frame', '')))
                
                if len(plant_frames) < 2:
                    logger.warning(f"Plant {plant_id} has only {len(plant_frames)} frames, skipping")
                    continue
                
                # Extract frames and keys
                frame_keys = [x[0] for x in plant_frames]
                frames = [x[1]['composite'] for x in plant_frames]
                
                logger.info(f"Processing plant {plant_id} with {len(frames)} frames")
                
                # Process with SAM2Long
                occlusion_results = self.occlusion_handler.segment_plant_sequence(
                    frames=frames,
                    target_plant_id=plant_id
                )
                
                # Update plant data with occlusion results
                target_masks = occlusion_results['target_masks']
                neighbor_masks = occlusion_results['neighbor_masks']
                
                for i, (key, pdata) in enumerate(plant_frames):
                    if i < len(target_masks):
                        # Update mask with target plant only
                        pdata['original_mask'] = pdata.get('mask', np.zeros_like(target_masks[i]))
                        pdata['mask'] = target_masks[i]
                        pdata['neighbor_mask'] = neighbor_masks[i]
                        pdata['occlusion_handled'] = True
                        
                        # Update soft mask as well
                        pdata['original_soft_mask'] = pdata.get('soft_mask', np.zeros_like(target_masks[i], dtype=np.float32))
                        pdata['soft_mask'] = (target_masks[i] / 255.0).astype(np.float32)
                
                # Calculate and store occlusion metrics
                metrics = self.occlusion_handler.get_occlusion_metrics(occlusion_results)
                for key, pdata in plant_frames:
                    pdata['occlusion_metrics'] = metrics
                
                logger.info(f"Plant {plant_id} occlusion handling completed")
                logger.info(f"  - Average occlusion ratio: {metrics['average_occlusion_ratio']:.3f}")
                logger.info(f"  - Frames with occlusion: {metrics['frames_with_occlusion']}")
                
            except Exception as e:
                logger.error(f"Occlusion handling failed for plant {plant_id}: {e}")
                # Mark as failed but continue
                for key, pdata in plant_frames:
                    pdata['occlusion_handled'] = False
                    pdata['occlusion_error'] = str(e)
        
        return plants
    
    def _extract_features(self, plants: Dict[str, Any], stream_save: bool = False) -> Dict[str, Any]:
        """Extract all features from plants.
        
        If stream_save is True, save outputs for each plant immediately after
        its features are computed to improve throughput and reduce peak memory.
        """
        total = len(plants)
        logger.info(f"Extracting features for {total} plants...")
        iterator = plants.items()
        if tqdm is not None:
            iterator = tqdm(list(plants.items()), desc="Extracting features", total=total, unit="img", leave=False)

        # Prepare output directories once if we're streaming saves
        if stream_save:
            try:
                self.output_manager.create_output_directories()
            except Exception:
                pass

        for idx, (key, pdata) in enumerate(iterator):
            try:
                logger.debug(f"Extracting features for {key}")
                
                # Extract texture features
                pdata['texture_features'] = self._extract_texture_features(pdata)
                
                # Extract vegetation indices
                pdata['vegetation_indices'] = self._extract_vegetation_indices(pdata)
                
                # Extract morphological features
                pdata['morphology_features'] = self._extract_morphology_features(pdata)
                
                # Immediately save outputs for this plant if streaming is enabled
                if stream_save:
                    try:
                        self.output_manager.save_plant_results(key, pdata)
                    except Exception as _e:
                        logger.error(f"Stream-save failed for {key}: {_e}")
                
                logger.debug(f"Features extracted for {key}")
                if tqdm is None and (idx == 0 or (idx + 1) % 25 == 0 or (idx + 1) == total):
                    logger.info(f"Extracted features for {idx + 1}/{total}: {key}")
                
            except Exception as e:
                logger.error(f"Feature extraction failed for {key}: {e}")
                # Add empty features
                pdata['texture_features'] = {}
                pdata['vegetation_indices'] = {}
                pdata['morphology_features'] = {}
        
        return plants
    
    def _extract_texture_features(self, pdata: Dict[str, Any]) -> Dict[str, Any]:
        """Extract texture features for a single plant."""
        features = {}
        
        # Get bands to process
        bands = ['color', 'nir', 'red_edge', 'red', 'green', 'pca']
        
        for band in bands:
            try:
                # Prepare grayscale image
                gray_image = self._prepare_band_image(pdata, band)
                
                # Extract texture features
                band_features = self.texture_extractor.extract_all_texture_features(gray_image)
                
                # Compute statistics using mask3 β†’ features_mask β†’ mask
                mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
                stats = self.texture_extractor.compute_texture_statistics(band_features, mask)
                
                features[band] = {
                    'features': band_features,
                    'statistics': stats
                }
                
            except Exception as e:
                logger.error(f"Texture extraction failed for band {band}: {e}")
                features[band] = {'features': {}, 'statistics': {}}
        
        return features
    
    def _extract_vegetation_indices(self, pdata: Dict[str, Any]) -> Dict[str, Any]:
        """Extract vegetation indices for a single plant."""
        try:
            spectral_stack = pdata.get('spectral_stack', {})
            # Prefer mask3 β†’ features_mask β†’ mask
            mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
            
            if not spectral_stack or mask is None:
                return {}
            
            return self.vegetation_extractor.compute_vegetation_indices(
                spectral_stack, mask
            )
            
        except Exception as e:
            logger.error(f"Vegetation index extraction failed: {e}")
            return {}
    
    def _extract_morphology_features(self, pdata: Dict[str, Any]) -> Dict[str, Any]:
        """Extract morphological features for a single plant."""
        try:
            composite = pdata.get('composite')
            # Prefer mask3 β†’ features_mask β†’ mask
            mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
            
            if composite is None or mask is None:
                return {}
            
            return self.morphology_extractor.extract_morphology_features(
                composite, mask
            )
            
        except Exception as e:
            logger.error(f"Morphology feature extraction failed: {e}")
            return {}
    
    def _prepare_band_image(self, pdata: Dict[str, Any], band: str) -> np.ndarray:
        """Prepare grayscale image for a specific band."""
        if band == 'color':
            composite = pdata['composite']
            # Prefer mask3 β†’ features_mask β†’ mask
            mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
            if mask is not None:
                masked = self.mask_handler.apply_mask_to_image(composite, mask)
                return cv2.cvtColor(masked, cv2.COLOR_BGR2GRAY)
            else:
                return cv2.cvtColor(composite, cv2.COLOR_BGR2GRAY)
        
        elif band == 'pca':
            # Create PCA from spectral bands
            spectral_stack = pdata.get('spectral_stack', {})
            # Prefer mask3 β†’ features_mask β†’ mask
            mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
            
            if not spectral_stack:
                return np.zeros((512, 512), dtype=np.uint8)
            
            # Stack bands
            bands_data = []
            for b in ['nir', 'red_edge', 'red', 'green']:
                if b in spectral_stack:
                    arr = spectral_stack[b].squeeze(-1).astype(float)
                    if mask is not None:
                        arr = np.where(mask > 0, arr, np.nan)
                    bands_data.append(arr)
            
            if not bands_data:
                return np.zeros((512, 512), dtype=np.uint8)
            
            # Create PCA
            full_stack = np.stack(bands_data, axis=-1)
            h, w, c = full_stack.shape
            flat = full_stack.reshape(-1, c)
            valid = ~np.isnan(flat).any(axis=1)
            
            if valid.sum() == 0:
                return np.zeros((h, w), dtype=np.uint8)
            
            vec = np.zeros(h * w)
            vec[valid] = PCA(n_components=1, whiten=True).fit_transform(
                flat[valid]
            ).squeeze()
            
            gray_f = vec.reshape(h, w)
            if mask is not None:
                m, M = gray_f[mask > 0].min(), gray_f[mask > 0].max()
            else:
                m, M = gray_f.min(), gray_f.max()
            
            if M > m:
                gray = ((gray_f - m) / (M - m) * 255).astype(np.uint8)
            else:
                gray = np.zeros_like(gray_f, dtype=np.uint8)
            
            return gray
        
        else:
            # Individual spectral band
            spectral_stack = pdata.get('spectral_stack', {})
            # Prefer mask3 β†’ features_mask β†’ mask
            mask = pdata.get('mask3', pdata.get('features_mask', pdata.get('mask')))
            
            if band not in spectral_stack:
                return np.zeros((512, 512), dtype=np.uint8)
            
            arr = spectral_stack[band].squeeze(-1).astype(float)
            if mask is not None:
                arr = np.where(mask > 0, arr, np.nan)
            
            if mask is not None:
                m, M = np.nanmin(arr), np.nanmax(arr)
            else:
                m, M = arr.min(), arr.max()
            
            if M > m:
                gray = ((np.nan_to_num(arr, nan=m) - m) / (M - m) * 255).astype(np.uint8)
            else:
                gray = np.zeros_like(arr, dtype=np.uint8)
            
            return gray
    
    def _generate_outputs(self, plants: Dict[str, Any]) -> None:
        """Generate all output files and visualizations."""
        self.output_manager.create_output_directories()
        
        for key, pdata in plants.items():
            try:
                logger.debug(f"Generating outputs for {key}")
                self.output_manager.save_plant_results(key, pdata)
            except Exception as e:
                logger.error(f"Output generation failed for {key}: {e}")
    
    def _create_summary(self, plants: Dict[str, Any]) -> Dict[str, Any]:
        """Create summary of pipeline results."""
        summary = {
            "total_plants": len(plants),
            "successful_plants": 0,
            "failed_plants": 0,
            "features_extracted": {
                "texture": 0,
                "vegetation": 0,
                "morphology": 0
            }
        }
        
        for key, pdata in plants.items():
            try:
                # Check if features were extracted
                if pdata.get('texture_features'):
                    summary["features_extracted"]["texture"] += 1
                if pdata.get('vegetation_indices'):
                    summary["features_extracted"]["vegetation"] += 1
                if pdata.get('morphology_features'):
                    summary["features_extracted"]["morphology"] += 1
                
                summary["successful_plants"] += 1
                
            except Exception:
                summary["failed_plants"] += 1
        
        return summary

    def _apply_instance_masks(self, plants: Dict[str, Any], instance_results_dir: Path) -> None:
        """Replace segmentation masks with SAM2Long instance masks using track_1.

        Expects files under instance_results_dir/plantX/track_1/frame_YY_mask.png.
        """
        # Default and per-plant overrides for source plant, track and preferred frame
        default_track = "track_0"
        src_rules: Dict[str, str] = {
            "plant13": "plant12",
            "plant14": "plant13",
            "plant15": "plant14",
            "plant16": "plant15",
        }
        track_rules: Dict[str, str] = {
            # explicit track rules
            "plant1": "track_0",
            "plant4": "track_0",
            "plant9": "track_3",
            "plant13": "track_1",
            "plant14": "track_0",
            "plant15": "track_0",
            "plant16": "track_0",
            "plant18": "track_0",
            "plant19": "track_0",
            "plant23": "track_1",
            "plant26": "track_0",
            "plant27": "track_0",
            "plant29": "track_0",
            "plant31": "track_1",
            "plant34": "track_1",
            "plant35": "track_1",
            "plant36": "track_0",
            "plant37": "track_1",
            "plant38": "track_0",
            "plant39": "track_1",
            "plant40": "track_0",
            "plant41": "track_1",
            "plant42": "track_0",
            "plant43": "track_0",
            "plant45": "track_0",
        }
        frame_rules: Dict[str, int] = {
            # preferred frame overrides (1-based)
            "plant13": 8,
            "plant14": 8,
            "plant15": 8,
            "plant33": 2,
            "plant16": 4,
            "plant19": 5,
            "plant26": 8,
            "plant27": 8,
            "plant29": 8,
            "plant35": 7,
            "plant36": 6,
            "plant37": 2,
            "plant45": 5,
        }
        # Per-plant rule: skip applying instance masks (keep bbox/BRIA mask) on all dates except 2025_05_08
        bbox_only_plants: Set[str] = {"plant19", "plant20", "plant27", "plant33", "plant39", "plant42", "plant44", "plant46"}
        date_exception = "2025_05_08"

        for key, pdata in plants.items():
            try:
                parts = key.split('_')
                if len(parts) < 3:
                    continue
                plant_name = parts[-2]
                frame_token = parts[-1]  # frame8
                if not (plant_name.startswith('plant') and frame_token.startswith('frame')):
                    continue
                date_key = "_".join(parts[:3])
                if (plant_name in bbox_only_plants) and (date_key != date_exception):
                    # Do not override masks for bbox-only plants
                    continue
                frame_num = int(frame_token.replace('frame', ''))
                # Resolve source plant, track and desired frame
                src_plant = src_rules.get(plant_name, plant_name)
                track_name = track_rules.get(plant_name, default_track)
                desired_frame = frame_rules.get(plant_name, frame_num)
                plant_dir = Path(instance_results_dir) / src_plant / track_name
                mask_path = plant_dir / f"frame_{desired_frame:02d}_mask.png"
                if not mask_path.exists():
                    # Fallback to current frame if override not found
                    fallback = plant_dir / f"frame_{frame_num:02d}_mask.png"
                    if fallback.exists():
                        mask_path = fallback
                    else:
                        # Last-resort: pick any available frame mask in the track directory
                        try:
                            candidates = sorted(plant_dir.glob("frame_*_mask.png"))
                            if len(candidates) > 0:
                                mask_path = candidates[0]
                            else:
                                continue
                        except Exception:
                            continue
                inst_mask = cv2.imread(str(mask_path), cv2.IMREAD_GRAYSCALE)
                if inst_mask is None:
                    continue
                # Ensure binary uint8 0/255
                inst_mask_bin = (inst_mask > 0).astype(np.uint8) * 255
                pdata['original_mask'] = pdata.get('mask', inst_mask_bin.copy())
                pdata['mask'] = inst_mask_bin
                pdata['original_soft_mask'] = pdata.get('soft_mask', (inst_mask_bin / 255.0).astype(np.float32))
                pdata['soft_mask'] = (inst_mask_bin / 255.0).astype(np.float32)
                pdata['instance_applied'] = True

                # Build mask3 = external(mask) AND BRIA(original_mask)
                try:
                    _m1 = pdata.get('mask')
                    _m2 = pdata.get('original_mask')
                    if isinstance(_m1, np.ndarray) and isinstance(_m2, np.ndarray):
                        _m1b = (_m1.astype(np.uint8) > 0)
                        _m2b = (_m2.astype(np.uint8) > 0)
                        mask3 = (_m1b & _m2b).astype(np.uint8) * 255
                        pdata['mask3'] = mask3
                        pdata['features_mask'] = mask3
                except Exception:
                    pass

                # After applying instance masks, also overwrite the composite and spectral stack
                # with the source plant's raw image (desired frame preferred) so that
                # feature extraction and saved originals/overlays are consistent with the mask source.
                try:
                    if plant_name in src_rules:
                        date_key = "_".join(parts[:3])
                        src_key_desired = f"{date_key}_{src_plant}_frame{desired_frame}"
                        src_key_same = f"{date_key}_{src_plant}_{frame_token}"
                        copy_from = plants.get(src_key_desired) or plants.get(src_key_same)
                        if copy_from is None:
                            # Fallback: load source composite from filesystem if not present in plants dict
                            try:
                                from PIL import Image as _Image
                                _date_folder = date_key.replace('_', '-')
                                _date_dir = Path(self.config.paths.input_folder)
                                if _date_dir.name != _date_folder:
                                    _date_dir = _date_dir / _date_folder
                                _frame_path = _date_dir / src_plant / f"{src_plant}_frame{desired_frame}.tif"
                                if not _frame_path.exists():
                                    _frame_path = _date_dir / src_plant / f"{src_plant}_frame{frame_num}.tif"
                                if _frame_path.exists():
                                    _img = _Image.open(str(_frame_path))
                                    # Process to composite using preprocessor
                                    comp, spec = self.preprocessor.process_raw_image(_img)
                                    copy_from = {"composite": comp, "spectral_stack": spec}
                            except Exception:
                                copy_from = None
                        if copy_from is not None:
                            # Preserve the segmentation-time composite once
                            if 'composite' in pdata and 'segmentation_composite' not in pdata:
                                pdata['segmentation_composite'] = pdata['composite']
                            if 'composite' in copy_from:
                                pdata['composite'] = copy_from['composite']
                            if 'spectral_stack' in copy_from:
                                pdata['spectral_stack'] = copy_from['spectral_stack']
                            # Ensure mask size matches the copied composite
                            ch, cw = pdata['composite'].shape[:2]
                            mh, mw = pdata['mask'].shape[:2]
                            if (mh, mw) != (ch, cw):
                                pdata['mask'] = cv2.resize(pdata['mask'].astype('uint8'), (cw, ch), interpolation=cv2.INTER_NEAREST)
                                pdata['soft_mask'] = (pdata['mask'] > 0).astype(np.float32)
                except Exception:
                    pass
            except Exception as e:
                logger.debug(f"Instance mask apply failed for {key}: {e}")

    def _apply_instance_masks_from_mapping(self, plants: Dict[str, Any], mapping_file: Path) -> None:
        """Apply instance masks using an explicit mapping file with absolute paths.

        mapping JSON structure:
        {
          "plant1": {"frame": 8, "mask_path": "/abs/path/to/plant1/track_X/frame_08_mask.png"},
          "plant2": {"frame": 8, "mask_path": "/abs/path/.../frame_08_mask.png"},
          ...
        }
        If a plant's mapping specifies a different frame, only entries matching that frame are updated.
        """
        import json
        if not mapping_file.exists():
            raise FileNotFoundError(f"Mapping file not found: {mapping_file}")
        with open(mapping_file, "r") as f:
            mapping = json.load(f)
        # Normalize mapping plant keys to names like 'plantX'
        norm_map = {}
        for k, v in mapping.items():
            k_norm = k if str(k).startswith("plant") else f"plant{int(k)}" if str(k).isdigit() else str(k)
            norm_map[k_norm] = v

        for key, pdata in plants.items():
            try:
                parts = key.split('_')
                if len(parts) < 3:
                    continue
                plant_name = parts[-2]
                frame_token = parts[-1]
                if not (plant_name.startswith('plant') and frame_token.startswith('frame')):
                    continue
                frame_num = int(frame_token.replace('frame', ''))
                if plant_name not in norm_map:
                    continue
                entry = norm_map[plant_name]
                target_frame = int(entry.get("frame", frame_num))
                if frame_num != target_frame:
                    # Only update the designated frame for this plant
                    continue
                mask_path_str = entry.get("mask_path")
                if not mask_path_str:
                    continue
                mask_path = Path(mask_path_str)
                if not mask_path.exists():
                    logger.warning(f"Mask path not found for {plant_name} {frame_token}: {mask_path}")
                    continue
                inst_mask = cv2.imread(str(mask_path), cv2.IMREAD_GRAYSCALE)
                if inst_mask is None:
                    continue
                inst_mask_bin = (inst_mask > 0).astype(np.uint8) * 255
                pdata['original_mask'] = pdata.get('mask', inst_mask_bin.copy())
                pdata['mask'] = inst_mask_bin
                pdata['original_soft_mask'] = pdata.get('soft_mask', (inst_mask_bin / 255.0).astype(np.float32))
                pdata['soft_mask'] = (inst_mask_bin / 255.0).astype(np.float32)
                pdata['instance_applied'] = True

                # Build mask3 = external(mask) AND BRIA(original_mask)
                try:
                    _m1 = pdata.get('mask')
                    _m2 = pdata.get('original_mask')
                    if isinstance(_m1, np.ndarray) and isinstance(_m2, np.ndarray):
                        _m1b = (_m1.astype(np.uint8) > 0)
                        _m2b = (_m2.astype(np.uint8) > 0)
                        mask3 = (_m1b & _m2b).astype(np.uint8) * 255
                        pdata['mask3'] = mask3
                        pdata['features_mask'] = mask3
                except Exception:
                    pass
            except Exception as e:
                logger.debug(f"Instance mapping apply failed for {key}: {e}")


def run_pipeline(config_path: str, load_all_frames: bool = False, segmentation_only: bool = False, filter_plants: Optional[List[str]] = None) -> Dict[str, Any]:
    """
    Convenience function to run the pipeline.
    
    Args:
        config_path: Path to configuration file
        load_all_frames: Whether to load all frames or selected frames
        segmentation_only: If True, run segmentation only and skip feature extraction
        
    Returns:
        Pipeline results
    """
    pipeline = SorghumPipeline(config_path)
    return pipeline.run(load_all_frames, segmentation_only, filter_plants)


if __name__ == "__main__":
    import sys
    
    config_path = sys.argv[1] if len(sys.argv) > 1 else "config.yml"
    load_all = "--all" in sys.argv
    seg_only = "--seg-only" in sys.argv
    # Basic arg parse for --plant=<name>
    plant_filter = None
    for arg in sys.argv[1:]:
        if arg.startswith("--plant="):
            plant_filter = [arg.split("=", 1)[1]]
    
    try:
        results = run_pipeline(config_path, load_all, seg_only, plant_filter)
        print("Pipeline completed successfully!")
        print(f"Processed {results['summary']['total_plants']} plants")
    except Exception as e:
        print(f"Pipeline failed: {e}")
        sys.exit(1)