File size: 16,917 Bytes
849ca03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import re
import numpy as np
import argparse
from sentence_transformers import SentenceTransformer # pip install sentence-transformers
from sklearn.metrics.pairwise import cosine_similarity # pip install scikit-learn
import random
import sys

# --- 1. Configuration Settings (from original script) ---
# TODO: Fill in the directory containing the 8 metadata JSON files
METADATA_BASE_DIR = 'metadata' # Directory containing these files

# TODO: Fill in the directory containing the 8 metadata JSON files
OUTPUT_DIR = "intra_task"

# --- Hardcoded list of metadata filenames ---
METADATA_FILENAMES = [
    # '2d_Spatial457_SAT_SPAR-7M_80k_metadata.json',
    # '3d_RoboSpatial_PRISM_Spatial457_SAT_SPAR-7M_80.0k_metadata.json',
    # 'dynamic_Spatial457_SAT_SPAR-7M_80k_metadata.json',
    # 'perception_Spatial457_SAT_SPAR-7M_80k_metadata.json',
    # 'real_SPAR-7M_RoboSpatial_80.0k_metadata.json',
    # 'reasoning_RoboSpatial_PRISM_Spatial457_SAT_SPAR-7M_80.0k_metadata.json',
    # 'static_RoboSpatial_PRISM_Spatial457_SAT_SPAR-7M_80.0k_metadata.json',
    # 'synthetic_SAT_Spatial457_PRISM_80.0k_metadata.json'
]

# --- FT files (hardcoded list of FT data) ---
ADDITIONAL_FT_FILES = {
    # RefSpatial
    "RefSpatial_2D_choice_qa": "../RefSpatial_data/2D/choice_qa.json",
    "RefSpatial_2D_reasoning_template_qa": "../RefSpatial_data/2D/reasoning_template_qa.json",
    "RefSpatial_3D_choice_qa": "../RefSpatial_data/3D/choice_qa.json",
    "RefSpatial_3D_multi_view_qa": "../RefSpatial_data/3D/multi_view_qa.json",
    "RefSpatial_3D_reasoning_template_qa": "../RefSpatial_data/3D/reasoning_template_qa.json",
    "RefSpatial_3D_vacant_qa": "../RefSpatial_data/3D/vacant_qa.json",
    "RefSpatial_3D_visual_choice_qa": "../RefSpatial_data/3D/visual_choice_qa.json",
    # Spatial457
    "Spatial457_L1_single": "../Spatial457_data/qwen_data_new/L1_single.json",
    "Spatial457_L2_objects": "../Spatial457_data/qwen_data_new/L2_objects.json",
    "Spatial457_L3_2d_spatial": "../Spatial457_data/qwen_data_new/L3_2d_spatial.json",
    "Spatial457_L4_occ": "../Spatial457_data/qwen_data_new/L4_occ.json",
    "Spatial457_L4_pose": "../Spatial457_data/qwen_data_new/L4_pose.json",
    "Spatial457_L5_6d_spatial": "../Spatial457_data/qwen_data_new/L5_6d_spatial.json",
    "Spatial457_L5_collision": "../Spatial457_data/qwen_data_new/L5_collision.json",
    # SPAR-7M
    "SPAR-7M_obj_count": "../SPAR-7M_data/qwen_data/obj_count.json",
    "SPAR-7M_obj_spatial_relation": "../SPAR-7M_data/qwen_data/obj_spatial_relation.json",
    "SPAR-7M_spatial_imagination": "../SPAR-7M_data/qwen_data/spatial_imagination.json",
    # SAT
    "SAT_action_consequence": "../SAT_data/qwen_data_new/action_consequence.json",
    "SAT_action_sequence": "../SAT_data/qwen_data_new/action_sequence.json",
    "SAT_goal_aim": "../SAT_data/qwen_data_new/goal_aim.json",
    "SAT_obj_movement": "../SAT_data/qwen_data_new/obj_movement.json",
    "SAT_other": "../SAT_data/qwen_data_new/other.json",
    "SAT_perspective": "../SAT_data/qwen_data_new/perspective.json",
    # PRISM
    "PRISM_train_data": "../PRISM_data/qwen_data/train_data.json",
    # RoboSpatial
    "RoboSpatial_compatibility": "../RoboSpatial_data/qwen_data/robospatial_compatibility.json",
    "RoboSpatial_configuration": "../RoboSpatial_data/qwen_data/robospatial_configuration.json",
    "RoboSpatial_context": "../RoboSpatial_data/qwen_data/robospatial_context.json",
}

# --- Analysis Configuration ---
MODEL_NAME = 'sentence-transformers/all-mpnet-base-v2'
DEFAULT_NUM_ITERATIONS = 20
MAX_SAMPLES_PER_ITERATION = 10000

# --- 2. Helper Function (from original script) ---
def clean_question(text, keep_instructions=False):
    """Cleans question text, optionally removing instructions."""
    if not isinstance(text, str): return ""
    cleaned_text = re.sub(r'^<image>\n?', '', text).strip()
    
    # If we keep instructions, just do the basic clean and return
    if keep_instructions: 
        return cleaned_text
    
    # If we remove instructions, apply all regex rules
    cleaned_text = re.sub(r'Choices:.*?(?=\nPlease answer|\n|$)', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'\n\nChoices:\s*.*?(?=\n|$)', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'\nPlease answer directly.*', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'The options describe the spatial relationship.*?(\nChoose|\nPlease select|\nPick|\nSelect)', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'(\nChoose|\nPlease select|\nPick|\nSelect)\s+the (correct|right|appropriate) (response|option|answer).*?Your answer can only include.*?$', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'\.?\s*Final answer should be.*?$', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'\.?\s*Your final answer should be formatted.*?points\.', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = re.sub(r'\.?\s*Please use the world coordinate system.*?objects\.', '', cleaned_text, flags=re.DOTALL | re.IGNORECASE).strip()
    cleaned_text = cleaned_text.rstrip('?.')
    return cleaned_text.strip()

# --- 3. Main Analysis Function ---
def analyze_ft_intra_similarity(metadata_dir, metadata_files, additional_files, model_name, num_iterations, keep_instructions):
    """
    Calculates intra-task similarity for all specified fine-tuning datasets.
    """
    print(f"Using Sentence Transformer model: {model_name}")
    print(f"Keep instructions: {keep_instructions}")
    print(f"Running {num_iterations} iterations...\n")
    
    try:
        model = SentenceTransformer(model_name)
    except Exception as e:
        print(f"!!! ERROR loading model '{model_name}': {e}")
        return None, None, None

    # --- Load FT Data ---
    print("Processing Fine-tuning source data...")
    ft_questions = {}
    metadata_task_names = []
    additional_task_names = []

    # 1. Load from metadata
    print(f"Reading {len(metadata_files)} metadata files from '{metadata_dir}'...")
    for meta_filename in metadata_files:
        meta_file_path = os.path.join(metadata_dir, meta_filename)
        if not os.path.exists(meta_file_path): 
            print(f"Warning: Meta file not found: {meta_file_path}")
            continue
        try:
            with open(meta_file_path, 'r', encoding='utf-8') as f: 
                metadata = json.load(f)
            
            if 'source_files' in metadata:
                for src in metadata['source_files']:
                    task_name = src.get('file_path') # Use relative path as task name
                    if not task_name: continue
                    
                    full_path = os.path.normpath(os.path.join("..", task_name))
                    metadata_task_names.append(task_name)
                    if task_name not in ft_questions: 
                        ft_questions[task_name] = []
                    
                    if not os.path.exists(full_path):
                        print(f"Warning: Source file not found: {full_path}")
                        continue
                        
                    # Load questions from this source file
                    with open(full_path, 'r', encoding='utf-8') as f_data: 
                        data = json.load(f_data)
                    for item in data:
                        q = None
                        if 'conversations' in item and item['conversations']: q = item['conversations'][0].get('value')
                        elif 'question' in item: q = item.get('question')
                        cleaned_q = clean_question(q, keep_instructions=keep_instructions)
                        if cleaned_q: ft_questions[task_name].append(cleaned_q)

        except Exception as e: 
            print(f"Warning: Could not process meta file {meta_filename}: {e}")

    # 2. Add additional files
    print(f"Adding {len(additional_files)} additional FT files...")
    for task_name, relative_path in additional_files.items():
        full_path = os.path.normpath(relative_path)
        additional_task_names.append(task_name)
        if task_name not in ft_questions: 
            ft_questions[task_name] = []
        
        if not os.path.exists(full_path): 
            print(f"Warning: Additional file not found: {full_path}")
            continue
            
        try:
            # Load questions from this source file
            with open(full_path, 'r', encoding='utf-8') as f_data: 
                data = json.load(f_data)
            for item in data:
                q = None
                if 'conversations' in item and item['conversations']: q = item['conversations'][0].get('value')
                elif 'question' in item: q = item.get('question')
                cleaned_q = clean_question(q, keep_instructions=keep_instructions)
                if cleaned_q: ft_questions[task_name].append(cleaned_q)
        except Exception as e: 
            print(f"Warning: Could not process additional file {full_path}: {e}")

    print(f"Loaded questions from {len(ft_questions)} unique FT tasks.")

    # --- Pre-calculate all embeddings ---
    print("\nCalculating all FT task embeddings (once)...")
    ft_embeddings = {}
    for q_type, questions in ft_questions.items():
        if len(questions) < 2:
            print(f"  - Skipping {q_type} (only {len(questions)} question(s))")
            continue
        print(f"  - Encoding {q_type} ({len(questions)} q's)")
        try:
            ft_embeddings[q_type] = model.encode(questions, show_progress_bar=True)
        except Exception as e:
            print(f"!!! ERROR encoding {q_type}: {e}")
            
    if not ft_embeddings:
        print("!!! ERROR: No tasks with sufficient questions to encode.")
        return None, None, None

    print("\nStarting similarity iterations...")
    task_similarity_scores = {q_type: [] for q_type in ft_embeddings}

    for i in range(num_iterations):
        if (i + 1) % 10 == 0 or i == 0:
            print(f"  Iteration {i+1}/{num_iterations}...")
        
        for q_type, embeddings_list in ft_embeddings.items():
            
            total_count = len(embeddings_list)
            
            k = min(total_count, MAX_SAMPLES_PER_ITERATION)
            
            sampled_indices = random.sample(range(total_count), k)
            
            mid_point = k // 2
            group_a_indices = sampled_indices[:mid_point]
            group_b_indices = sampled_indices[mid_point:]

            group_a_embeds = [embeddings_list[idx] for idx in group_a_indices]
            group_b_embeds = [embeddings_list[idx] for idx in group_b_indices]
            
            avg_a = np.mean(group_a_embeds, axis=0).reshape(1, -1)
            avg_b = np.mean(group_b_embeds, axis=0).reshape(1, -1)
            
            similarity = cosine_similarity(avg_a, avg_b)[0][0]
            # --- FIX: Cast numpy.float32 to a standard python float ---
            task_similarity_scores[q_type].append(float(similarity))
            
    print("Iterations complete.\n")
    
    # --- Calculate Final Statistics ---
    final_stats = {}
    all_scores = []
    
    for q_type, scores in task_similarity_scores.items():
        if scores:
            mean_sim = np.mean(scores)
            std_sim = np.std(scores)
            final_stats[q_type] = {
                'mean': mean_sim,
                'std': std_sim,
                'iterations': len(scores),
                'num_questions': len(ft_embeddings[q_type])
            }
            all_scores.extend(scores)

    # Calculate overall average
    if all_scores:
        overall_mean = np.mean(all_scores)
        overall_std = np.std(all_scores)
        final_stats['--OVERALL--'] = {
            'mean': overall_mean,
            'std': overall_std,
            'iterations': num_iterations,
            'num_questions': 'N/A'
        }

    # Return stats and the lists of task names for grouped printing
    return final_stats, metadata_task_names, additional_task_names

# --- 4. Main Execution Block ---
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Calculate intra-task similarity for all Fine-Tuning source datasets.")
    parser.add_argument('--keep_instructions', action='store_true', 
                        help="Keep instruction text in questions during embedding.")
    parser.add_argument('--num_iterations', type=int, default=DEFAULT_NUM_ITERATIONS, 
                        help=f"Number of random splits to perform (default: {DEFAULT_NUM_ITERATIONS}).")
    args = parser.parse_args()

    if not METADATA_BASE_DIR:
        print("!!! ERROR: Please fill in the METADATA_BASE_DIR variable in the script.")
        sys.exit()
        
    stats, metadata_tasks, additional_tasks = analyze_ft_intra_similarity(
        metadata_dir=METADATA_BASE_DIR,
        metadata_files=METADATA_FILENAMES,
        additional_files=ADDITIONAL_FT_FILES,
        model_name=MODEL_NAME,
        num_iterations=args.num_iterations,
        keep_instructions=args.keep_instructions
    )
    
    if stats:
        print("--- Fine-Tuning Intra-Task Similarity Report ---")
        print(f"(Based on {args.num_iterations} random split iterations)\n")

        valid_tasks = [task for task in stats if task != '--OVERALL--']
        if not valid_tasks:
            print("No valid tasks with scores found.")
            sys.exit()
            
        max_name_len = max(len(t) for t in valid_tasks)
        max_name_len = max(35, max_name_len)
        
        header = f"{'Task Name':<{max_name_len}} | {'Questions':>10} | {'Mean Similarity':>18} | {'Std. Deviation':>18}"
        print(header)
        print("-" * len(header))

        # --- (Console Print) Group 1: Additional FT Tasks ---
        print(f"\n--- Additional FT Tasks (e.g., RefSpatial) ---")
        additional_tasks_found = 0
        for task in sorted(list(set(additional_tasks))):
            if task in stats:
                data = stats[task]
                print(f"{task:<{max_name_len}} | {data['num_questions']:>10} | {data['mean']:>18.4f} | {data['std']:>18.4f}")
                additional_tasks_found += 1
        if additional_tasks_found == 0:
            print("No valid results for Additional FT Tasks.")

        # --- (Console Print) Group 2: Metadata-Discovered Tasks ---
        print(f"\n--- Metadata-Discovered Tasks (e.g., VQA, SPAR) ---")
        metadata_tasks_found = 0
        for task in sorted(list(set(metadata_tasks))):
            if task in stats:
                data = stats[task]
                print(f"{task:<{max_name_len}} | {data['num_questions']:>10} | {data['mean']:>18.4f} | {data['std']:>18.4f}")
                metadata_tasks_found += 1
        if metadata_tasks_found == 0:
            print("No valid results for Metadata-Discovered Tasks.")

        # --- (Console Print) Overall Summary ---
        if '--OVERALL--' in stats:
            data = stats['--OVERALL--']
            print("\n" + "-" * len(header))
            print(f"{'--OVERALL--':<{max_name_len}} | {data['num_questions']:>10} | {data['mean']:>18.4f} | {data['std']:>18.4f}")

        try:
            os.makedirs(OUTPUT_DIR, exist_ok=True)
            suffix = "_with_instructions" if args.keep_instructions else ""
            report_filename = f"fixed_ft_datasets_intra_task_report{suffix}.json"
            report_save_path = os.path.join(OUTPUT_DIR, report_filename)
            
            report_data = {
                "report_summary": {
                    "model_name": MODEL_NAME,
                    "num_iterations": args.num_iterations,
                    "keep_instructions": args.keep_instructions
                },
                "overall_stats": stats.get('--OVERALL--', {}),
                "additional_ft_tasks": {},
                "metadata_discovered_tasks": {}
            }
            
            # 1. Additional FT Tasks (RefSpatial, etc.)
            for task in sorted(list(set(additional_tasks))):
                if task in stats:
                    report_data["additional_ft_tasks"][task] = stats[task]
            
            # 2. Metadata-Discovered Tasks (VQA, SPAR, etc.)
            for task in sorted(list(set(metadata_tasks))):
                if task in stats:
                    report_data["metadata_discovered_tasks"][task] = stats[task]

            with open(report_save_path, 'w', encoding='utf-8') as f:
                json.dump(report_data, f, indent=2)
                
            print(f"\nStructured summary report saved to {report_save_path}")
            
        except Exception as e:
            print(f"\n!!! Could not save final summary report to JSON: {e}")

    print("\n--- Analysis finished ---")