|
import os |
|
import random |
|
from glob import glob |
|
import json |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
from astropy.io import fits |
|
import datasets |
|
from datasets import DownloadManager |
|
from fsspec.core import url_to_fs |
|
from tqdm import tqdm |
|
|
|
|
|
def make_split_jsonl_files(config_type="tiny", data_dir="./data", |
|
telescope_subdirectories=["INT", "JKT","LCO", "TJO", "WHT"], |
|
outdir="./splits", seed=42): |
|
""" |
|
Create jsonl files for the GBI-16-2D-Legacy dataset. |
|
|
|
config_type: str, default="tiny" |
|
The type of split to create. Options are "tiny" and "full". |
|
data_dir: str, default="./data" |
|
The directory where the FITS files are located. |
|
telescope_subdirectories: list, default=["INT", "JKT","LCO", "TJO", "WHT"] |
|
The subdirectories of the data_dir that contain the FITS files for each telescope. |
|
outdir: str, default="./splits" |
|
The directory where the jsonl files will be created. |
|
seed: int, default=42 |
|
The seed for the random split. |
|
""" |
|
random.seed(seed) |
|
os.makedirs(outdir, exist_ok=True) |
|
|
|
fits_files = [] |
|
for subdir in telescope_subdirectories: |
|
fits_files.extend(glob(os.path.join(data_dir, subdir, "*.fits"))) |
|
|
|
random.shuffle(fits_files) |
|
|
|
if config_type == "tiny": |
|
train_files = [] |
|
test_files = [] |
|
for subdir in telescope_subdirectories: |
|
subdir_files = [f for f in fits_files if subdir in f] |
|
train_files.extend(subdir_files[:2]) |
|
test_files.extend(subdir_files[2:3]) |
|
elif config_type == "full": |
|
train_files = [] |
|
test_files = [] |
|
for subdir in telescope_subdirectories: |
|
subdir_files = [f for f in fits_files if subdir in f] |
|
split_idx = int(0.8 * len(subdir_files)) |
|
train_files.extend(subdir_files[:split_idx]) |
|
test_files.extend(subdir_files[split_idx:]) |
|
else: |
|
raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.") |
|
|
|
def create_jsonl(files, split_name): |
|
output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl") |
|
with open(output_file, "w") as out_f: |
|
for file in files: |
|
print(file, flush=True, end="...") |
|
with fits.open(file, memmap=False) as hdul: |
|
image_id = os.path.basename(file).split(".fits")[0] |
|
|
|
telescope = hdul[0].header.get('TELESCOP', 'UNKNOWN') |
|
item = {"image_id": image_id, "image": file, "telescope": telescope} |
|
out_f.write(json.dumps(item) + "\n") |
|
|
|
create_jsonl(train_files, "train") |
|
create_jsonl(test_files, "test") |
|
|
|
|
|
if __name__ == "__main__": |
|
make_split_jsonl_files("tiny") |
|
make_split_jsonl_files("full") |