JesseLiu commited on
Commit
9734235
·
verified ·
1 Parent(s): f48e76f

Update MedQA_Maze.py

Browse files
Files changed (1) hide show
  1. MedQA_Maze.py +53 -64
MedQA_Maze.py CHANGED
@@ -1,15 +1,28 @@
1
  import os
2
  import json
3
  import datasets
4
- import logging
5
 
6
- _CITATION = """Your citation here"""
7
- _DESCRIPTION = """Description of your medical QA dataset"""
8
 
9
 
10
- logging.basicConfig(level=logging.INFO)
11
- logger = logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
13
  class MedQaMazeConfig(datasets.BuilderConfig):
14
  def __init__(self, **kwargs):
15
  super().__init__(version=datasets.Version("1.0.0"), **kwargs)
@@ -18,8 +31,8 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
18
  BUILDER_CONFIGS = [
19
  MedQaMazeConfig(name="default", description="A default config"),
20
  MedQaMazeConfig(name="advance", description="Advanced-level test data"),
21
- MedQaMazeConfig(name="all", description="Full dataset with train and test"),
22
- MedQaMazeConfig(name="basic", description="Basic-level test data"),
23
  MedQaMazeConfig(name="challenge", description="Challenge-level test data"),
24
  ]
25
 
@@ -36,74 +49,50 @@ class MedQaMaze(datasets.GeneratorBasedBuilder):
36
  "answer": datasets.Value("string"),
37
  }),
38
  supervised_keys=None,
 
 
39
  )
40
 
41
  def _split_generators(self, dl_manager):
42
  """Returns SplitGenerators."""
43
- # Get the absolute path of the script
44
- base_path = os.path.dirname(os.path.abspath(__file__))
45
- logger.info(f"Base path: {base_path}")
46
 
47
- config_name = self.config.name
48
- logger.info(f"Using config: {config_name}")
 
49
 
50
- # Define file paths
51
- if config_name == "advance":
52
- filepath = os.path.join(base_path, "advance", "test.jsonl")
53
- logger.info(f"Looking for advance test file at: {filepath}")
54
- if not os.path.exists(filepath):
55
- raise ValueError(f"File not found: {filepath}")
56
-
57
- return [
58
- datasets.SplitGenerator(
59
- name=datasets.Split.TEST,
60
- gen_kwargs={"filepath": filepath}
61
- )
62
- ]
63
- elif config_name == "all":
64
- train_path = os.path.join(base_path, "all", "train.jsonl")
65
- test_path = os.path.join(base_path, "all", "test.jsonl")
66
-
67
- logger.info(f"Looking for train file at: {train_path}")
68
- logger.info(f"Looking for test file at: {test_path}")
69
-
70
- if not os.path.exists(train_path):
71
- raise ValueError(f"Train file not found: {train_path}")
72
- if not os.path.exists(test_path):
73
- raise ValueError(f"Test file not found: {test_path}")
74
-
75
- return [
76
  datasets.SplitGenerator(
77
  name=datasets.Split.TRAIN,
78
- gen_kwargs={"filepath": train_path}
79
- ),
 
 
 
 
80
  datasets.SplitGenerator(
81
  name=datasets.Split.TEST,
82
- gen_kwargs={"filepath": test_path}
83
  )
84
- ]
85
- # ... similar checks for other configs ...
 
86
 
87
  def _generate_examples(self, filepath):
88
  """Yields examples."""
89
- logger.info(f"Processing file: {filepath}")
90
-
91
- try:
92
- with open(filepath, "r", encoding="utf-8") as f:
93
- for idx, line in enumerate(f):
94
- try:
95
- data = json.loads(line.strip())
96
- example = {
97
- "context": data.get("context", ""),
98
- "question": data.get("question", ""),
99
- "prerequisit": data.get("prerequisit", ""),
100
- "groundtruth_zoo": data.get("groundtruth_zoo", []),
101
- "answer": data.get("answer", ""),
102
- }
103
- yield idx, example
104
- except json.JSONDecodeError as e:
105
- logger.error(f"Error parsing JSON at line {idx} in {filepath}: {e}")
106
- continue
107
- except Exception as e:
108
- logger.error(f"Error reading file {filepath}: {e}")
109
- raise
 
1
  import os
2
  import json
3
  import datasets
 
4
 
5
+ _CITATION = ""
6
+ _DESCRIPTION = ""
7
 
8
 
9
+ _URLs = {
10
+ "all": {
11
+ "train": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/all/train.jsonl",
12
+ "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/all/test.jsonl"
13
+ },
14
+ "basic": {
15
+ "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/basic/test.jsonl"
16
+ },
17
+ "advance": {
18
+ "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/advance/test.jsonl"
19
+ },
20
+ "challenge": {
21
+ "test": "https://huggingface.co/datasets/JesseLiu/MedQA_Maze/raw/main/challenge/test.jsonl"
22
+ }
23
+ }
24
 
25
+ # Rest of your code remains the same...
26
  class MedQaMazeConfig(datasets.BuilderConfig):
27
  def __init__(self, **kwargs):
28
  super().__init__(version=datasets.Version("1.0.0"), **kwargs)
 
31
  BUILDER_CONFIGS = [
32
  MedQaMazeConfig(name="default", description="A default config"),
33
  MedQaMazeConfig(name="advance", description="Advanced-level test data"),
34
+ MedQaMazeConfig(name="all", description="Full dataset with train and test"),
35
+ MedQaMazeConfig(name="basic", description="Basic-level test data"),
36
  MedQaMazeConfig(name="challenge", description="Challenge-level test data"),
37
  ]
38
 
 
49
  "answer": datasets.Value("string"),
50
  }),
51
  supervised_keys=None,
52
+ homepage="https://huggingface.co/datasets/JesseLiu/MedQA_Maze",
53
+ citation=_CITATION,
54
  )
55
 
56
  def _split_generators(self, dl_manager):
57
  """Returns SplitGenerators."""
58
+ config_name = self.config.name if self.config.name != "default" else "all"
 
 
59
 
60
+ # Download the files
61
+ urls = _URLs[config_name]
62
+ data_files = dl_manager.download_and_extract(urls)
63
 
64
+ splits = []
65
+ if "train" in data_files:
66
+ splits.append(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  datasets.SplitGenerator(
68
  name=datasets.Split.TRAIN,
69
+ gen_kwargs={"filepath": data_files["train"]}
70
+ )
71
+ )
72
+
73
+ if "test" in data_files:
74
+ splits.append(
75
  datasets.SplitGenerator(
76
  name=datasets.Split.TEST,
77
+ gen_kwargs={"filepath": data_files["test"]}
78
  )
79
+ )
80
+
81
+ return splits
82
 
83
  def _generate_examples(self, filepath):
84
  """Yields examples."""
85
+ with open(filepath, encoding="utf-8") as f:
86
+ for idx, line in enumerate(f):
87
+ try:
88
+ data = json.loads(line.strip())
89
+ yield idx, {
90
+ "context": data.get("context", ""),
91
+ "question": data.get("question", ""),
92
+ "prerequisit": data.get("prerequisit", ""),
93
+ "groundtruth_zoo": data.get("groundtruth_zoo", []),
94
+ "answer": data.get("answer", ""),
95
+ }
96
+ except json.JSONDecodeError:
97
+ print(f"Warning: Skipping invalid JSON at line {idx} in {filepath}")
98
+ continue