rithwiks commited on
Commit
6f7b8c4
·
1 Parent(s): e4903da

made utils folder

Browse files
GBI_16_4D.py → GBI-16-4D.py RENAMED
@@ -47,7 +47,7 @@ _REPO_ID = "AstroCompress/GBI-16-4D"
47
  class GBI_16_4D(datasets.GeneratorBasedBuilder):
48
  """GBI-16-4D Dataset"""
49
 
50
- VERSION = datasets.Version("1.0.0")
51
 
52
  BUILDER_CONFIGS = [
53
  datasets.BuilderConfig(
@@ -136,99 +136,4 @@ class GBI_16_4D(datasets.GeneratorBasedBuilder):
136
  task_instance_key = f"{self.config.name}-{split}-{idx}"
137
  with fits.open(filepath, memmap=False, ignore_missing_simple=True) as hdul:
138
  image_data = hdul[0].data.tolist()
139
- yield task_instance_key, {**{"image": image_data}, **item}
140
-
141
-
142
- def get_fits_footprint(fits_path):
143
- """
144
- Process a FITS file to extract WCS information and calculate the footprint.
145
-
146
- Parameters:
147
- fits_path (str): Path to the FITS file.
148
-
149
- Returns:
150
- tuple: A tuple containing the WCS footprint coordinates.
151
- """
152
- with fits.open(fits_path) as hdul:
153
- hdul[0].data = hdul[0].data[0, 0]
154
- wcs = WCS(hdul[0].header)
155
- shape = sorted(tuple(wcs.pixel_shape))[:2]
156
- footprint = wcs.calc_footprint(axes=shape)
157
- coords = list(footprint.flatten())
158
- return coords
159
-
160
- def calculate_pixel_scale(header):
161
- """
162
- Calculate the pixel scale in arcseconds per pixel from a FITS header.
163
-
164
- Parameters:
165
- header (astropy.io.fits.header.Header): The FITS header containing WCS information.
166
-
167
- Returns:
168
- Mean of the pixel scales in x and y.
169
- """
170
- # Extract the CD matrix elements
171
- cd1_1 = header.get('CD1_1', np.nan)
172
- cd1_2 = header.get('CD1_2', np.nan)
173
- cd2_1 = header.get('CD2_1', np.nan)
174
- cd2_2 = header.get('CD2_2', np.nan)
175
-
176
- # Calculate the pixel scales in arcseconds per pixel
177
- pixscale_x = np.sqrt(cd1_1**2 + cd1_2**2) * 3600
178
- pixscale_y = np.sqrt(cd2_1**2 + cd2_2**2) * 3600
179
-
180
- return np.mean([pixscale_x, pixscale_y])
181
-
182
-
183
- def make_split_jsonl_files(config_type="tiny", data_dir="./data",
184
- outdir="./splits", seed=42):
185
- """
186
- Create jsonl files for the GBI-16-4D dataset.
187
-
188
- config_type: str, default="tiny"
189
- The type of split to create. Options are "tiny" and "full".
190
- data_dir: str, default="./data"
191
- The directory where the FITS files are located.
192
- outdir: str, default="./splits"
193
- The directory where the jsonl files will be created.
194
- seed: int, default=42
195
- The seed for the random split.
196
- """
197
- random.seed(seed)
198
- os.makedirs(outdir, exist_ok=True)
199
-
200
- fits_files = glob(os.path.join(data_dir, "*.fits"))
201
- random.shuffle(fits_files)
202
- if config_type == "tiny":
203
- train_files = fits_files[:2]
204
- test_files = fits_files[2:3]
205
- elif config_type == "full":
206
- split_idx = int(0.8 * len(fits_files))
207
- train_files = fits_files[:split_idx]
208
- test_files = fits_files[split_idx:]
209
- else:
210
- raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.")
211
-
212
- def create_jsonl(files, split_name):
213
- output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl")
214
- with open(output_file, "w") as out_f:
215
- for file in tqdm(files):
216
- #print(file, flush=True, end="...")
217
- with fits.open(file, memmap=False, ignore_missing_simple=True) as hdul:
218
- image_id = os.path.basename(file).split(".fits")[0]
219
- ra = hdul[0].header.get('CRVAL1', np.nan)
220
- dec = hdul[0].header.get('CRVAL2', np.nan)
221
- pixscale = calculate_pixel_scale(hdul[0].header)
222
- ntimes = hdul[0].data.shape[0]
223
- nbands = hdul[0].data.shape[1]
224
- footprint = get_fits_footprint(file)
225
- item = {"image_id": image_id, "image": file, "ra": ra, "dec": dec,
226
- "pixscale": pixscale, "ntimes": ntimes, "nbands": nbands, "footprint": footprint}
227
- out_f.write(json.dumps(item) + "\n")
228
-
229
- create_jsonl(train_files, "train")
230
- create_jsonl(test_files, "test")
231
-
232
- if __name__ == "__main__":
233
- make_split_jsonl_files("tiny")
234
- make_split_jsonl_files("full")
 
47
  class GBI_16_4D(datasets.GeneratorBasedBuilder):
48
  """GBI-16-4D Dataset"""
49
 
50
+ VERSION = datasets.Version("1.0.2")
51
 
52
  BUILDER_CONFIGS = [
53
  datasets.BuilderConfig(
 
136
  task_instance_key = f"{self.config.name}-{split}-{idx}"
137
  with fits.open(filepath, memmap=False, ignore_missing_simple=True) as hdul:
138
  image_data = hdul[0].data.tolist()
139
+ yield task_instance_key, {**{"image": image_data}, **item}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -91,7 +91,7 @@ Then `cd GBI-16-4D` and start python like:
91
 
92
  ```python
93
  from datasets import load_dataset
94
- dataset = load_dataset("./GBI_16_4D.py", "tiny", data_dir="./data/")
95
  ds = dataset.with_format("np")
96
  ```
97
 
 
91
 
92
  ```python
93
  from datasets import load_dataset
94
+ dataset = load_dataset("./GBI-16-4D.py", "tiny", data_dir="./data/")
95
  ds = dataset.with_format("np")
96
  ```
97
 
utils/__init__.py ADDED
File without changes
utils/create_splits.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ from glob import glob
4
+ import json
5
+ from huggingface_hub import hf_hub_download
6
+ from tqdm import tqdm
7
+ import numpy as np
8
+
9
+ from astropy.io import fits
10
+ from astropy.wcs import WCS
11
+ import datasets
12
+ from datasets import DownloadManager
13
+ from fsspec.core import url_to_fs
14
+
15
+ def get_fits_footprint(fits_path):
16
+ """
17
+ Process a FITS file to extract WCS information and calculate the footprint.
18
+
19
+ Parameters:
20
+ fits_path (str): Path to the FITS file.
21
+
22
+ Returns:
23
+ tuple: A tuple containing the WCS footprint coordinates.
24
+ """
25
+ with fits.open(fits_path) as hdul:
26
+ hdul[0].data = hdul[0].data[0, 0]
27
+ wcs = WCS(hdul[0].header)
28
+ shape = sorted(tuple(wcs.pixel_shape))[:2]
29
+ footprint = wcs.calc_footprint(axes=shape)
30
+ coords = list(footprint.flatten())
31
+ return coords
32
+
33
+ def calculate_pixel_scale(header):
34
+ """
35
+ Calculate the pixel scale in arcseconds per pixel from a FITS header.
36
+
37
+ Parameters:
38
+ header (astropy.io.fits.header.Header): The FITS header containing WCS information.
39
+
40
+ Returns:
41
+ Mean of the pixel scales in x and y.
42
+ """
43
+ # Extract the CD matrix elements
44
+ cd1_1 = header.get('CD1_1', np.nan)
45
+ cd1_2 = header.get('CD1_2', np.nan)
46
+ cd2_1 = header.get('CD2_1', np.nan)
47
+ cd2_2 = header.get('CD2_2', np.nan)
48
+
49
+ # Calculate the pixel scales in arcseconds per pixel
50
+ pixscale_x = np.sqrt(cd1_1**2 + cd1_2**2) * 3600
51
+ pixscale_y = np.sqrt(cd2_1**2 + cd2_2**2) * 3600
52
+
53
+ return np.mean([pixscale_x, pixscale_y])
54
+
55
+
56
+ def make_split_jsonl_files(config_type="tiny", data_dir="./data",
57
+ outdir="./splits", seed=42):
58
+ """
59
+ Create jsonl files for the GBI-16-4D dataset.
60
+
61
+ config_type: str, default="tiny"
62
+ The type of split to create. Options are "tiny" and "full".
63
+ data_dir: str, default="./data"
64
+ The directory where the FITS files are located.
65
+ outdir: str, default="./splits"
66
+ The directory where the jsonl files will be created.
67
+ seed: int, default=42
68
+ The seed for the random split.
69
+ """
70
+ random.seed(seed)
71
+ os.makedirs(outdir, exist_ok=True)
72
+
73
+ fits_files = glob(os.path.join(data_dir, "*.fits"))
74
+ random.shuffle(fits_files)
75
+ if config_type == "tiny":
76
+ train_files = fits_files[:2]
77
+ test_files = fits_files[2:3]
78
+ elif config_type == "full":
79
+ split_idx = int(0.8 * len(fits_files))
80
+ train_files = fits_files[:split_idx]
81
+ test_files = fits_files[split_idx:]
82
+ else:
83
+ raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.")
84
+
85
+ def create_jsonl(files, split_name):
86
+ output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl")
87
+ with open(output_file, "w") as out_f:
88
+ for file in tqdm(files):
89
+ #print(file, flush=True, end="...")
90
+ with fits.open(file, memmap=False, ignore_missing_simple=True) as hdul:
91
+ image_id = os.path.basename(file).split(".fits")[0]
92
+ ra = hdul[0].header.get('CRVAL1', np.nan)
93
+ dec = hdul[0].header.get('CRVAL2', np.nan)
94
+ pixscale = calculate_pixel_scale(hdul[0].header)
95
+ ntimes = hdul[0].data.shape[0]
96
+ nbands = hdul[0].data.shape[1]
97
+ footprint = get_fits_footprint(file)
98
+ item = {"image_id": image_id, "image": file, "ra": ra, "dec": dec,
99
+ "pixscale": pixscale, "ntimes": ntimes, "nbands": nbands, "footprint": footprint}
100
+ out_f.write(json.dumps(item) + "\n")
101
+
102
+ create_jsonl(train_files, "train")
103
+ create_jsonl(test_files, "test")
104
+
105
+ if __name__ == "__main__":
106
+ make_split_jsonl_files("tiny")
107
+ make_split_jsonl_files("full")