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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
import os
import glob
import json
import argparse
from typing import List, Dict, Any, Union
from collections import Counter
import numpy as np

def get_temp_qa(question_type, paraphrased_path):
    """

    Load all question data and return a list of filtered template IDs based on question_type.



    Args:

        question_type (str): Type of question to filter by ('single-query', 'single-verify', etc., or 'all').

        paraphrased_path (str): Base path for the dataset.



    Returns:

        List[int]: A list of unique template IDs matching the filter criteria.

    """
    data = []
    
    for split in ["train", "valid", "test"]:
        file_pattern = os.path.join(paraphrased_path, split, "*.json")
        for fname in sorted(glob.glob(file_pattern)):
            with open(fname, "r") as f:
                records = json.load(f)
                data.extend(records)

    print(f"Loaded {len(data)} samples!")
    
    temp_list = []
    if question_type != "all":
        for item in data:
            if item['question_type'] == question_type:
                temp_list.append(item['template_id'])
    else:
        for item in data:
            if "single-" in item['question_type']:
                temp_list.append(item['template_id'])
    
    temp_ids = list(set(temp_list))
    
    # Filter template IDs to avoid conflict class
    temp_ids = [i for i in temp_ids if i not in [17, 14, 26, 28, 4, 40, 31]]
    return temp_ids

def prepare_ecg_qa_data(args):
    if args.question_type == "all":
        class_qa = get_class_qa("single-verify")
        ecg_qa_list_1 = change_ecg_to_qa(
            class_qa, "single-verify",
            paraphrased_path=args.paraphrased_path,
            dif_exp=args.dif_exp
        )

        class_qa += get_class_qa("single-choose")
        ecg_qa_list_2 = change_ecg_to_qa(
            class_qa, "single-choose",
            paraphrased_path=args.paraphrased_path,
            dif_exp=args.dif_exp
        )

        class_qa += get_class_qa("single-query")
        ecg_qa_list_3 = change_ecg_to_qa(
            class_qa, "single-query",
            paraphrased_path=args.paraphrased_path,
            dif_exp=args.dif_exp
        )

        ecg_qa_list = {**ecg_qa_list_1, **ecg_qa_list_2, **ecg_qa_list_3}
    else:
        class_qa = get_temp_qa(args.question_type, args.paraphrased_path)
        ecg_qa_list = change_ecg_to_qa(
            class_qa, args.question_type,
            paraphrased_path=args.paraphrased_path,
            dif_exp=args.dif_exp
        )

    all_ecg_qa_temp = list(ecg_qa_list.keys())
    sample_size = int(len(all_ecg_qa_temp) * 0.8)
    train_temp = np.random.choice(all_ecg_qa_temp, sample_size, replace=False).tolist()
    test_temp = [i for i in all_ecg_qa_temp if i not in train_temp]

    return class_qa, train_temp, test_temp

def change_ecg_to_qa(

    sample_ids, 

    question_type, 

    paraphrased_path,

    in_template=None, 

    dif_exp=1, 

    attr="",

    test_dataset="ptb-xl"

):
    """

    Process ECG data to create a dictionary of question-answer pairs categorized by template.

    

    Args:

        sample_ids (List[int]): List of template IDs to include.

        question_type (str): Type of question to filter.

        paraphrased_path (str): Base path for all data.

        data_root_path (str, optional): Path to the data root directory.

        in_template (List, optional): List of templates to include.

        dif_exp (int, optional): Controls return format. Default is 1.

        attr (str, optional): Attribute type filter.

        test_dataset (str, optional): Dataset to use ('ptb-xl' or 'mimic'). Default is 'ptb-xl'.

        

    Returns:

        Dict: Dictionary of ECG QA data categorized by template.

    """
    data = []
    
        
    # Define subdirectories to process
    subdirectories = ["train", "valid", "test"]
    
    # Load data from all subdirectories
    for subdir in subdirectories:
        directory_path = os.path.join(paraphrased_path, subdir)
        if not os.path.exists(directory_path):
            print(f"Warning: Directory {directory_path} does not exist!")
            continue
            
        file_pattern = os.path.join(directory_path, "*.json")
        files = sorted(glob.glob(file_pattern))
        
        if not files:
            print(f"Warning: No JSON files found in {directory_path}")
            continue
            
        for fname in files:
            with open(fname, "r") as f:
                json_data = json.load(f)
                data.extend(json_data)
    
    # Filter by attribute type if specified
    if attr != "":
        sample_data = [item for item in data if item['attribute_type'] == attr]
    else:
        sample_data = data
    
    # Filter samples by file existence if using MIMIC dataset
    if test_dataset == "mimic":
        ecg_data_path = os.path.join(paraphrased_path, "mimic_iv_ecg/processed_test_30k/")
        if not os.path.exists(ecg_data_path):
            print(f"Warning: MIMIC ECG data path {ecg_data_path} does not exist!")
        sample_data = [sample for sample in sample_data if os.path.isfile(os.path.join(ecg_data_path, f"{sample['ecg_id'][0]}.mat"))]

    ecg_qa_dict = {}
    
    if len(sample_data) == 0:
        print(f"Cannot find template_id == {sample_ids} or no data available")
    else:
        for sample in sample_data:
            template_id = sample['template_id']
            if template_id in sample_ids:
                process_sample_by_type(sample, template_id, ecg_qa_dict, in_template)
    
    filter_ecg_qa_dict_by_question_type(ecg_qa_dict, question_type)
    
    if dif_exp == 1:
        return {key: [value] for key, value in ecg_qa_dict.items()}
    
    return filter_by_question_frequency(ecg_qa_dict)


def process_sample_by_type(sample, template_id, ecg_qa_dict, in_template):
    """

    Process a sample based on its question type.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

        in_template (List, optional): List of templates to include.

    """
    question_type = sample['question_type']
    
    if question_type == 'single-verify':
        process_single_verify_sample(sample, template_id, ecg_qa_dict, in_template)
    elif question_type == 'single-choose':
        process_single_choose_sample(sample, template_id, ecg_qa_dict, in_template)
    elif question_type == 'single-query':
        process_single_query_sample(sample, template_id, ecg_qa_dict, in_template)


def process_single_verify_sample(sample, template_id, ecg_qa_dict, in_template):
    """

    Process a single-verify type sample.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

        in_template (List, optional): List of templates to include.

    """
    if sample['answer'][0] in ["yes", "no"]:
        answer = sample['answer'][0]
        all_attributes = "_".join(sorted(sample['attribute']))
        dict_key = f"{template_id}_{all_attributes}_{answer}"
        
        if in_template is None or dict_key in in_template:
            if dict_key not in ecg_qa_dict:
                ecg_qa_dict[dict_key] = [sample]
            else:
                ecg_qa_dict[dict_key].append(sample)


def process_single_choose_sample(sample, template_id, ecg_qa_dict, in_template):
    """

    Process a single-choose type sample.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

        in_template (List, optional): List of templates to include.

    """
    if len(sample['answer']) == 1:
        answer = sample['answer'][0]
    elif len(sample['answer']) == 2:
        answer = "both"
        sample['answer'] = ["both"]
    else:
        print("single-choose data have more than 2 answers!")
        return
    
    all_attributes = "_".join(sorted(sample['attribute']))
    
    if in_template is None:
        handle_single_choose_without_template(sample, template_id, answer, all_attributes, ecg_qa_dict)
    else:
        handle_single_choose_with_template(sample, template_id, answer, all_attributes, in_template, ecg_qa_dict)


def handle_single_choose_without_template(sample, template_id, answer, all_attributes, ecg_qa_dict):
    """

    Handle a single-choose type sample when no template is provided.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        answer (str): The answer string.

        all_attributes (str): The joined attributes string.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

    """
    if answer == "both":
        dict_key = f"{template_id}_{all_attributes}_{answer}"
        add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)
    elif answer == "none":
        for attr in sample['attribute']:
            dict_key = f"{template_id}_{attr}_{answer}"
            add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)
    else:
        dict_key = f"{template_id}_{answer}_{answer}"
        add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)


def handle_single_choose_with_template(sample, template_id, answer, all_attributes, in_template, ecg_qa_dict):
    """

    Handle a single-choose type sample when a template is provided.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        answer (str): The answer string.

        all_attributes (str): The joined attributes string.

        in_template (List): List of templates to include.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

    """
    full_key = f"{template_id}_{all_attributes}_{answer}"
    short_key = f"{template_id}_{answer}"
    
    if full_key in in_template or short_key in in_template:
        if answer == "both":
            dict_key = f"{template_id}_{all_attributes}_{answer}"
            add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)
        elif answer == "none":
            for attr in sample['attribute']:
                dict_key = f"{template_id}_{attr}_{answer}"
                add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)
        else:
            dict_key = f"{template_id}_{answer}_{answer}"
            add_to_ecg_qa_dict(dict_key, sample, ecg_qa_dict)


def process_single_query_sample(sample, template_id, ecg_qa_dict, in_template):
    """

    Process a single-query type sample.

    

    Args:

        sample (Dict): The sample data.

        template_id (int): The template ID.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

        in_template (List, optional): List of templates to include.

    """
    if len(sample['answer']) == 1:
        answer = sample['answer'][0]
    elif len(sample['answer']) >= 2:
        answer = ", ".join(sample['answer'])
        sample['answer'] = [answer]
    
    all_attributes = "_".join(sorted(sample['attribute']))
    dict_key = f"{template_id}_{all_attributes}_{answer}"
    
    if in_template is None or dict_key in in_template:
        if dict_key not in ecg_qa_dict:
            ecg_qa_dict[dict_key] = [sample]
        else:
            ecg_qa_dict[dict_key].append(sample)


def add_to_ecg_qa_dict(key, sample, ecg_qa_dict):
    """

    Add a sample to the ECG QA dictionary.

    

    Args:

        key (str): The dictionary key.

        sample (Dict): The sample data.

        ecg_qa_dict (Dict): Dictionary to populate with processed data.

    """
    if key not in ecg_qa_dict:
        ecg_qa_dict[key] = [sample]
    else:
        ecg_qa_dict[key].append(sample)


def filter_ecg_qa_dict_by_question_type(ecg_qa_dict, question_type):
    """

    Filter the ECG QA dictionary by question type and minimum sample counts.

    

    Args:

        ecg_qa_dict (Dict): Dictionary to filter.

        question_type (str): Type of question to filter by.

    """
    for key in list(ecg_qa_dict.keys()):
        if question_type in ["single-verify", "single-choose", "single-query"]:
            qt = question_type
        elif question_type == "all":
            if len(ecg_qa_dict[key]) == 0:
                del ecg_qa_dict[key]
                continue
            qt = ecg_qa_dict[key][0]['question_type']
        else:
            continue
        
        if qt == "single-verify" and len(ecg_qa_dict[key]) < 140:
            del ecg_qa_dict[key]
        elif qt == "single-choose" and len(ecg_qa_dict[key]) < 14:
            del ecg_qa_dict[key]
        elif qt == "single-query" and len(ecg_qa_dict[key]) < 50:
            del ecg_qa_dict[key]


def filter_by_question_frequency(ecg_qa_dict):
    """

    Filter the ECG QA dictionary by question frequency.

    

    Args:

        ecg_qa_dict (Dict): Dictionary to filter.

        

    Returns:

        Dict: Filtered dictionary.

    """
    for key in list(ecg_qa_dict.keys()):
        question_id_counter = Counter(sample['question_id'] for sample in ecg_qa_dict[key])
        frequent_question_ids = [q_id for q_id, count in question_id_counter.items() if count >= 10]
        
        all_samples_by_question = []
        for question_id in frequent_question_ids:
            question_samples = [sample for sample in ecg_qa_dict[key] if sample['question_id'] == question_id]
            all_samples_by_question.append(question_samples)
        
        if len(all_samples_by_question) != 0:
            ecg_qa_dict[key] = all_samples_by_question
        else:
            del ecg_qa_dict[key]
    
    return ecg_qa_dict


def validate_path(path):
    """

    Validate if a path exists.

    

    Args:

        path (str): Path to validate.

        

    Returns:

        bool: True if path exists, False otherwise.

    """
    if not os.path.exists(path):
        return False
    return True


def main():
    """

    Main function to run the ECG data processing.

    """
    parser = argparse.ArgumentParser(description='ECG Data Processor')
    parser.add_argument('--paraphrased_path', type=str, 
                        default='path/to/paraphrased', 
                        help='path to ./paraphrased containing trian/val/test ECG-QA json files')
    parser.add_argument('--test_dataset', type=str, default="ptb-xl", choices=["ptb-xl", "mimic"], 
                        help='Dataset to use (ptb-xl or mimic)')
    
    args = parser.parse_args()
    paraphrased_path = args.paraphrased_path
    
    # Process each question type
    for q_type in ['single-verify', 'single-choose', 'single-query', 'all']:
        print(f"\nProcessing {q_type} question type...")
        
        # Get template IDs for the question type
        temp_ids = get_temp_qa(q_type, paraphrased_path)
        print(f"{q_type} temp_ids: {temp_ids} (total: {len(temp_ids)})")
        
        # Get the QA dictionary
        ecg_qa_dict = change_ecg_to_qa(
            temp_ids,
            q_type,
            paraphrased_path=paraphrased_path,
            test_dataset=args.test_dataset
        )
        print(f"ECG QA dictionary length: {len(ecg_qa_dict)}")
        
        # Calculate total QA count
        total_qa_count = 0
        for category_key in ecg_qa_dict.keys():
            category_values = ecg_qa_dict.get(category_key)
            category_total = sum(len(item) for item in category_values)
            total_qa_count += category_total
            
        print(f"Total QA count for '{q_type}': {total_qa_count}")
        print("-" * 40)


if __name__ == "__main__":
    main()