jbloom commited on
Commit
3b9a488
·
2 Parent(s): 586a5f4 084a6a0

fix merge conflict

Browse files
Files changed (2) hide show
  1. utils/__init__.py +0 -0
  2. utils/create_splits.py +105 -0
utils/__init__.py ADDED
File without changes
utils/create_splits.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
16
+ def get_fits_footprint(fits_path):
17
+ """
18
+ Process a FITS file to extract WCS information and calculate the footprint.
19
+
20
+ Parameters:
21
+ fits_path (str): Path to the FITS file.
22
+
23
+ Returns:
24
+ tuple: A tuple containing the WCS footprint coordinates.
25
+ """
26
+ with fits.open(fits_path) as hdul:
27
+ hdul[1].data = hdul[1].data[0, 0]
28
+ wcs = WCS(hdul[1].header)
29
+ shape = sorted(tuple(wcs.pixel_shape))[:2]
30
+ footprint = wcs.calc_footprint(axes=shape)
31
+ coords = list(footprint.flatten())
32
+ return coords
33
+
34
+
35
+ def calculate_pixel_scale(header):
36
+ """
37
+ Calculate the pixel scale in arcseconds per pixel from a FITS header.
38
+
39
+ Parameters:
40
+ header (astropy.io.fits.header.Header): The FITS header containing WCS information.
41
+
42
+ Returns:
43
+ Mean of the pixel scales in x and y.
44
+ """
45
+
46
+ # Calculate the pixel scales in arcseconds per pixel
47
+ pixscale_x = header.get('CDELT1', np.nan)
48
+ pixscale_y = header.get('CDELT2', np.nan)
49
+
50
+ return np.mean([pixscale_x, pixscale_y])
51
+
52
+
53
+ def make_split_jsonl_files(config_type="tiny", data_dir="./data",
54
+ outdir="./splits", seed=42):
55
+ """
56
+ Create jsonl files for the SBI-16-3D dataset.
57
+
58
+ config_type: str, default="tiny"
59
+ The type of split to create. Options are "tiny" and "full".
60
+ data_dir: str, default="./data"
61
+ The directory where the FITS files are located.
62
+ outdir: str, default="./splits"
63
+ The directory where the jsonl files will be created.
64
+ seed: int, default=42
65
+ The seed for the random split.
66
+ """
67
+ random.seed(seed)
68
+ os.makedirs(outdir, exist_ok=True)
69
+
70
+ fits_files = glob(os.path.join(data_dir, "*.fits"))
71
+ random.shuffle(fits_files)
72
+ if config_type == "tiny":
73
+ train_files = fits_files[:2]
74
+ test_files = fits_files[2:3]
75
+ elif config_type == "full":
76
+ split_idx = int(0.8 * len(fits_files))
77
+ train_files = fits_files[:split_idx]
78
+ test_files = fits_files[split_idx:]
79
+ else:
80
+ raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.")
81
+
82
+ def create_jsonl(files, split_name):
83
+ output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl")
84
+ with open(output_file, "w") as out_f:
85
+ for file in tqdm(files):
86
+ #print(file, flush=True, end="...")
87
+ with fits.open(file, memmap=False) as hdul:
88
+ image_id = os.path.basename(file).split(".fits")[0]
89
+ ra = hdul["SCI"].header.get('CRVAL1', 0)
90
+ dec = hdul["SCI"].header.get('CRVAL2', 0)
91
+ pixscale = calculate_pixel_scale(hdul["SCI"].header)
92
+ footprint = get_fits_footprint(file)
93
+ # get the number of groups per int
94
+ ntimes = hdul["SCI"].data.shape[1]
95
+ item = {"image_id": image_id, "image": file, "ra": ra, "dec": dec,
96
+ "pixscale": pixscale, "ntimes": ntimes, "footprint": footprint}
97
+ out_f.write(json.dumps(item) + "\n")
98
+
99
+ create_jsonl(train_files, "train")
100
+ create_jsonl(test_files, "test")
101
+
102
+
103
+ if __name__ == "__main__":
104
+ make_split_jsonl_files("tiny")
105
+ make_split_jsonl_files("full")