rithwiks commited on
Commit
e081d27
·
1 Parent(s): 7443663

new utils and splits

Browse files
SBI_16_2D.py → SBI-16-2D.py RENAMED
File without changes
splits/full_test.jsonl CHANGED
The diff for this file is too large to render. See raw diff
 
splits/full_train.jsonl CHANGED
The diff for this file is too large to render. See raw diff
 
splits/tiny_test.jsonl CHANGED
@@ -1 +1 @@
1
- {"ra1": 203.7732409387, "dec1": 13.8489878334, "ra2": 203.7214653161, "dec2": 13.8777594581, "ra3": 203.7050942963, "dec3": 13.8539247351, "ra4": 203.7568666382, "dec4": 13.8251565401, "exposure_time": 390.0, "image": "./data/jdrz89fdq_raw.fits", "image_id": "jdrz89fdq_raw", "ra": 203.7436405194, "dec": 13.83161538103, "pixscale": -1.21332e-05}
 
1
+ {"image_id": "jdrz11n7q_raw", "image": "../data/jdrz11n7q_raw.fits", "ra": 4.608953170523, "dec": 30.08292391635, "pixscale": 0.05008251063628878, "footprint": [[4.596537292754619, 30.114743173326985, 4.551509758403926, 30.071884367648646, 4.574426433594073, 30.05121263351455, 4.619454495882632, 30.09406362790967], [4.57446707043176, 30.093260596142066, 4.531317302276631, 30.051542448993004, 4.553651889505143, 30.030857573994318, 4.596802061267953, 30.072568429035663]]}
splits/tiny_train.jsonl CHANGED
@@ -1,2 +1,2 @@
1
- {"ra1": 260.2513190778, "dec1": -19.6024730349, "ra2": 260.3018066706, "dec2": -19.6355333113, "ra3": 260.3208290377, "dec3": -19.6131799119, "ra4": 260.2703445491, "dec4": -19.5801249252, "exposure_time": 493.0, "image": "./data/jdig01dbq_raw.fits", "image_id": "jdig01dbq_raw", "ra": 260.2833237316, "dec": -19.58768272612, "pixscale": 1.14791e-05}
2
- {"ra1": 11.77879924, "dec1": -21.6292955803, "ra2": 11.7313561082, "dec2": -21.6668467959, "ra3": 11.7496899151, "dec3": -21.6898781547, "ra4": 11.7971358575, "dec4": -21.6523217318, "exposure_time": 279.0, "image": "./data/jeyv02heq_raw.fits", "image_id": "jeyv02heq_raw", "ra": 11.78591952774, "dec": -21.6622203537, "pixscale": -1.06419e-05}
 
1
+ {"image_id": "jdne07fnq_raw", "image": "../data/jdne07fnq_raw.fits", "ra": 58.15894870986, "dec": 35.69119598576, "pixscale": 0.05011143018356258, "footprint": [[58.14032335605238, 35.66121385345633, 58.211555242689485, 35.66350683314965, 58.212584136362814, 35.69214259312979, 58.14132664729774, 35.68985021861018], [58.17565225163017, 35.66268076088015, 58.24449094130459, 35.6652962486248, 58.24598074938274, 35.6935841665628, 58.17711759640468, 35.690969525771244]]}
2
+ {"image_id": "j9fc57s3q_raw", "image": "../data/j9fc57s3q_raw.fits", "ra": 194.5182600318, "dec": 47.0326648271, "pixscale": 0.050102208663596326, "footprint": [[194.56725398623578, 47.029107445340784, 194.53407080188407, 47.08243223084805, 194.4943150205125, 47.07306667853927, 194.527532100821, 47.01975336809004], [194.5503582969605, 47.05544175524446, 194.5176842457192, 47.10682001922871, 194.47820028351606, 47.097913156369316, 194.51090691984348, 47.04654611406288]]}
utils/__init__.py ADDED
File without changes
utils/create_splits.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ wcs = WCS(hdul[1].header)
27
+ shape = sorted(tuple(wcs.pixel_shape))[:2]
28
+ footprint = wcs.calc_footprint(axes=shape)
29
+ coords1 = list(footprint.flatten())
30
+
31
+ wcs = WCS(hdul[4].header)
32
+ shape = sorted(tuple(wcs.pixel_shape))[:2]
33
+ footprint = wcs.calc_footprint(axes=shape)
34
+ coords2 = list(footprint.flatten())
35
+
36
+ return coords1, coords2
37
+
38
+
39
+ def calculate_pixel_scale(header):
40
+ """
41
+ Calculate the pixel scale separately for X and Y directions and return the mean pixel scale from a FITS header.
42
+
43
+ Args:
44
+ header: A FITS header object containing CD1_1, CD1_2, CD2_1, and CD2_2.
45
+
46
+ Returns:
47
+ mean_pixscale: The mean pixel scale in arcseconds per pixel.
48
+ """
49
+ # Extract CD matrix elements
50
+ CD1_1 = header['CD1_1']
51
+ CD1_2 = header['CD1_2']
52
+ CD2_1 = header['CD2_1']
53
+ CD2_2 = header['CD2_2']
54
+
55
+ # Calculate pixel scales
56
+ pixscale_x = (CD1_1**2 + CD1_2**2)**0.5 * 3600 # Convert from degrees to arcseconds
57
+ pixscale_y = (CD2_1**2 + CD2_2**2)**0.5 * 3600 # Convert from degrees to arcseconds
58
+
59
+ # Calculate mean pixel scale
60
+ mean_pixscale = (pixscale_x + pixscale_y) / 2
61
+
62
+ return mean_pixscale
63
+
64
+
65
+ def make_split_jsonl_files(
66
+ config_type="tiny", data_dir="../data", outdir="../splits", seed=42
67
+ ):
68
+ """
69
+ Create jsonl files for the SBI-16-2D dataset.
70
+
71
+ config_type: str, default="tiny"
72
+ The type of split to create. Options are "tiny" and "full".
73
+ data_dir: str, default="./data"
74
+ The directory where the FITS files are located.
75
+ outdir: str, default="./splits"
76
+ The directory where the jsonl files will be created.
77
+ seed: int, default=42
78
+ The seed for the random split.
79
+ """
80
+ random.seed(seed)
81
+ os.makedirs(outdir, exist_ok=True)
82
+
83
+ fits_files = glob(os.path.join(data_dir, "*.fits"))
84
+ random.shuffle(fits_files)
85
+ if config_type == "tiny":
86
+ train_files = fits_files[:2]
87
+ test_files = fits_files[2:3]
88
+ elif config_type == "full":
89
+ split_idx = int(0.8 * len(fits_files))
90
+ train_files = fits_files[:split_idx]
91
+ test_files = fits_files[split_idx:]
92
+ else:
93
+ raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.")
94
+
95
+ def create_jsonl(files, split_name):
96
+ output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl")
97
+ with open(output_file, "w") as out_f:
98
+ for file in tqdm(files):
99
+ #print(file, flush=True, end="...")
100
+ with fits.open(file, memmap=False) as hdul:
101
+ image_id = os.path.basename(file).split(".fits")[0]
102
+ ra = hdul["SCI"].header.get("CRVAL1", 0)
103
+ dec = hdul["SCI"].header.get("CRVAL2", 0)
104
+ pixscale = calculate_pixel_scale(hdul[1].header)
105
+ footprint = get_fits_footprint(file)
106
+ item = {
107
+ "image_id": image_id,
108
+ "image": file,
109
+ "ra": ra,
110
+ "dec": dec,
111
+ "pixscale": pixscale,
112
+ "footprint": footprint
113
+ }
114
+ out_f.write(json.dumps(item) + "\n")
115
+
116
+ create_jsonl(train_files, "train")
117
+ create_jsonl(test_files, "test")
118
+
119
+ if __name__ == "__main__":
120
+ make_split_jsonl_files("tiny")
121
+ make_split_jsonl_files("full")