File size: 2,709 Bytes
61e8d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json 
import os 
import shutil 
from map_to_hear import LABEL_MAPPING

SAMPLE_RATE=32000
AUDIOS=f"audios_sr={SAMPLE_RATE}"
HEAR_PATH="/projects/0/prjs1338/tasks/starss2023-v1.0.0-full"
META="""{
 "task_name": "starss2023",
 "version": "hear2021",
 "embedding_type": "event",
 "prediction_type": "multilabel",
 "split_mode": "trainvaltest",
 "sample_duration": null,
 "evaluation": [
  "event_onset_200ms_fms",
  "segment_1s_er"
 ],
 "download_urls": [
  {
   "split": "train",
   "name": "dev",
   "url": "https://archive.org/download/dcase2016_task2_train_dev/dcase2016_task2_train_dev.zip",
   "md5": "4e1b5e8887159193e8624dec801eb9e7"
  },
  {
   "split": "train",
   "name": "eval",
   "url": "https://archive.org/download/dcase2016_task2_test_public/dcase2016_task2_test_public.zip",
   "md5": "ac98768b39a08fc0c6c2ddd15a981dd7"
  }
 ],
 "default_mode": "full",
 "max_task_duration_by_split": {
  "train": null,
  "valid": null,
  "test": null
 },
 "tmp_dir": "_workdir",
 "mode": "full",
 "splits": [
  "train",
  "valid",
  "test"
 ]
}"""

def map_labels_to_csv(label_mapping):
    """
    Converts a label mapping dictionary to a list of (index, label) tuples
    and prints them in 'idx,label' CSV format.
    """
    sorted_keys = sorted(label_mapping.keys(), key=lambda x: int(x))
    string = "idx,label\n"
    for key in sorted_keys:
        idx = int(key)
        label = label_mapping[key]
        string = string + f"{idx},{label}\n"
        
    return string


def get_splits():
    json_names = set()
    for dirs in os.listdir("./labels"):
        json_names.add(dirs + ".json")
    return json_names

def move_files(json_names):
    for json_name in json_names:
        with open(json_name, "r") as f:
            data = json.load(f)
        split = json_name.replace(".json", "")
        file_names = data.keys()
        print(len(file_names))
        for file_name in file_names:
            if os.path.exists(os.path.join(AUDIOS, file_name)):
                os.makedirs(os.path.join(HEAR_PATH, str(SAMPLE_RATE), split), exist_ok=True)
                if not os.path.exists(os.path.join(HEAR_PATH, str(SAMPLE_RATE), split, file_name)):
                    destination = os.path.join(HEAR_PATH, str(SAMPLE_RATE), split, file_name)
                    shutil.copy(os.path.join(AUDIOS, file_name), destination)

if __name__ == "__main__":
    json_names = get_splits()
    move_files(json_names)

    csv_file = map_labels_to_csv(LABEL_MAPPING)
    with open(os.path.join(HEAR_PATH,"labelvocabulary.csv"), "w") as f:
        f.write(csv_file)

    meta = json.loads(META)
    with open(os.path.join(HEAR_PATH, "task_metadata.json"), "w") as f:
        json.dump(meta, f, indent=4)