jbloom
commited on
Commit
·
6e2625f
1
Parent(s):
e4d1b39
handle multiextension fits
Browse files- GBI-16-2D.py +484 -16
- splits/tiny_test.jsonl +1 -1
GBI-16-2D.py
CHANGED
|
@@ -2,15 +2,17 @@ import os
|
|
| 2 |
import random
|
| 3 |
from glob import glob
|
| 4 |
import json
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
-
|
| 7 |
|
|
|
|
| 8 |
from astropy.io import fits
|
| 9 |
from astropy.coordinates import Angle
|
| 10 |
from astropy import units as u
|
|
|
|
|
|
|
|
|
|
| 11 |
import datasets
|
| 12 |
from datasets import DownloadManager
|
| 13 |
-
|
| 14 |
|
| 15 |
_DESCRIPTION = (
|
| 16 |
"""SBI-16-2D is a dataset which is part of the AstroCompress project. """
|
|
@@ -159,9 +161,12 @@ class GBI_16_2D(datasets.GeneratorBasedBuilder):
|
|
| 159 |
for idx, (filepath, item) in enumerate(zip(filepaths, data_metadata)):
|
| 160 |
task_instance_key = f"{self.config.name}-{split}-{idx}"
|
| 161 |
with fits.open(filepath, memmap=False) as hdul:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
yield task_instance_key, {**{"image": image_data}, **item}
|
| 166 |
|
| 167 |
|
|
@@ -200,17 +205,30 @@ def make_split_jsonl_files(
|
|
| 200 |
with open(output_file, "w") as out_f:
|
| 201 |
for file in files:
|
| 202 |
print(file, flush=True, end="...")
|
|
|
|
| 203 |
with fits.open(file, memmap=False) as hdul:
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
item = {
|
| 215 |
"image_id": image_id,
|
| 216 |
"image": file,
|
|
@@ -226,3 +244,453 @@ def make_split_json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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import random
|
| 3 |
from glob import glob
|
| 4 |
import json
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
import numpy as np
|
| 7 |
from astropy.io import fits
|
| 8 |
from astropy.coordinates import Angle
|
| 9 |
from astropy import units as u
|
| 10 |
+
from fsspec.core import url_to_fs
|
| 11 |
+
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
import datasets
|
| 14 |
from datasets import DownloadManager
|
| 15 |
+
|
| 16 |
|
| 17 |
_DESCRIPTION = (
|
| 18 |
"""SBI-16-2D is a dataset which is part of the AstroCompress project. """
|
|
|
|
| 161 |
for idx, (filepath, item) in enumerate(zip(filepaths, data_metadata)):
|
| 162 |
task_instance_key = f"{self.config.name}-{split}-{idx}"
|
| 163 |
with fits.open(filepath, memmap=False) as hdul:
|
| 164 |
+
if len(hdul) > 1:
|
| 165 |
+
# multiextension ... paste together the amplifiers
|
| 166 |
+
data, _ = read_lris(filepath)
|
| 167 |
+
else:
|
| 168 |
+
data = hdul[0].data
|
| 169 |
+
image_data = data[:, :]
|
| 170 |
yield task_instance_key, {**{"image": image_data}, **item}
|
| 171 |
|
| 172 |
|
|
|
|
| 205 |
with open(output_file, "w") as out_f:
|
| 206 |
for file in files:
|
| 207 |
print(file, flush=True, end="...")
|
| 208 |
+
image_id = os.path.basename(file).split(".fits")[0]
|
| 209 |
with fits.open(file, memmap=False) as hdul:
|
| 210 |
+
if len(hdul) > 1:
|
| 211 |
+
# multiextension ... paste together
|
| 212 |
+
data, header = read_lris(file)
|
| 213 |
+
dim_1 = data.shape[0]
|
| 214 |
+
dim_2 = data.shape[1]
|
| 215 |
+
header = fits.header.Header(header)
|
| 216 |
+
else:
|
| 217 |
+
dim_1 = hdul[0].header.get("NAXIS1", 0)
|
| 218 |
+
dim_2 = hdul[0].header.get("NAXIS2", 0)
|
| 219 |
+
header = hdul[0].header
|
| 220 |
+
|
| 221 |
+
ras = header.get("RA", "0")
|
| 222 |
+
ra = float(
|
| 223 |
+
Angle(f"{ras} hours").to_string(unit=u.degree, decimal=True)
|
| 224 |
+
)
|
| 225 |
+
decs = header.get("DEC", "0")
|
| 226 |
+
dec = float(
|
| 227 |
+
Angle(f"{decs} degrees").to_string(unit=u.degree, decimal=True)
|
| 228 |
+
)
|
| 229 |
+
pixscale = header.get("CD1_2", 0.135)
|
| 230 |
+
rotation = header.get("ROTPOSN", 0.0)
|
| 231 |
+
exposure_time = header.get("TTIME", 0.0)
|
| 232 |
item = {
|
| 233 |
"image_id": image_id,
|
| 234 |
"image": file,
|
|
|
|
| 244 |
|
| 245 |
create_jsonl(train_files, "train")
|
| 246 |
create_jsonl(test_files, "test")
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def read_lris(raw_file, det=None, TRIM=False):
|
| 250 |
+
"""
|
| 251 |
+
Modified from pypeit.spectrographs.keck_lris.read_lris -- Jon Brown, Josh Bloom
|
| 252 |
+
cf. https://github.com/KerryPaterson/Imaging_pipelines
|
| 253 |
+
|
| 254 |
+
Read a raw LRIS data frame (one or more detectors)
|
| 255 |
+
Packed in a multi-extension HDU
|
| 256 |
+
Based on readmhdufits.pro
|
| 257 |
+
|
| 258 |
+
Parameters
|
| 259 |
+
----------
|
| 260 |
+
raw_file : str
|
| 261 |
+
Filename
|
| 262 |
+
det : int, optional
|
| 263 |
+
Detector number; Default = both
|
| 264 |
+
TRIM : bool, optional
|
| 265 |
+
Trim the image?
|
| 266 |
+
|
| 267 |
+
Returns
|
| 268 |
+
-------
|
| 269 |
+
array : ndarray
|
| 270 |
+
Combined image
|
| 271 |
+
header : FITS header
|
| 272 |
+
sections : list
|
| 273 |
+
List of datasec, oscansec, ampsec sections
|
| 274 |
+
"""
|
| 275 |
+
|
| 276 |
+
hdu = fits.open(raw_file)
|
| 277 |
+
head0 = hdu[0].header
|
| 278 |
+
|
| 279 |
+
# Get post, pre-pix values
|
| 280 |
+
precol = head0["PRECOL"]
|
| 281 |
+
postpix = head0["POSTPIX"]
|
| 282 |
+
preline = head0["PRELINE"]
|
| 283 |
+
postline = head0["POSTLINE"]
|
| 284 |
+
|
| 285 |
+
# get the detector
|
| 286 |
+
# this just checks if its the blue one and assumes red if not
|
| 287 |
+
# note the red fits headers don't even have this keyword???
|
| 288 |
+
if head0["INSTRUME"] == "LRISBLUE":
|
| 289 |
+
redchip = False
|
| 290 |
+
else:
|
| 291 |
+
redchip = True
|
| 292 |
+
|
| 293 |
+
# Setup for datasec, oscansec
|
| 294 |
+
dsec = []
|
| 295 |
+
osec = []
|
| 296 |
+
nxdata_sum = 0
|
| 297 |
+
|
| 298 |
+
# get the x and y binning factors...
|
| 299 |
+
binning = head0["BINNING"]
|
| 300 |
+
xbin, ybin = [int(ibin) for ibin in binning.split(",")]
|
| 301 |
+
|
| 302 |
+
# First read over the header info to determine the size of the output array...
|
| 303 |
+
n_ext = len(hdu) - 1 # Number of extensions (usually 4)
|
| 304 |
+
xcol = []
|
| 305 |
+
xmax = 0
|
| 306 |
+
ymax = 0
|
| 307 |
+
xmin = 10000
|
| 308 |
+
ymin = 10000
|
| 309 |
+
for i in np.arange(1, n_ext + 1):
|
| 310 |
+
theader = hdu[i].header
|
| 311 |
+
detsec = theader["DETSEC"]
|
| 312 |
+
if detsec != "0":
|
| 313 |
+
# parse the DETSEC keyword to determine the size of the array.
|
| 314 |
+
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten()
|
| 315 |
+
|
| 316 |
+
# find the range of detector space occupied by the data
|
| 317 |
+
# [xmin:xmax,ymin:ymax]
|
| 318 |
+
xt = max(x2, x1)
|
| 319 |
+
xmax = max(xt, xmax)
|
| 320 |
+
yt = max(y2, y1)
|
| 321 |
+
ymax = max(yt, ymax)
|
| 322 |
+
|
| 323 |
+
# find the min size of the array
|
| 324 |
+
xt = min(x1, x2)
|
| 325 |
+
xmin = min(xmin, xt)
|
| 326 |
+
yt = min(y1, y2)
|
| 327 |
+
ymin = min(ymin, yt)
|
| 328 |
+
# Save
|
| 329 |
+
xcol.append(xt)
|
| 330 |
+
|
| 331 |
+
# determine the output array size...
|
| 332 |
+
nx = xmax - xmin + 1
|
| 333 |
+
ny = ymax - ymin + 1
|
| 334 |
+
|
| 335 |
+
# change size for binning...
|
| 336 |
+
nx = nx // xbin
|
| 337 |
+
ny = ny // ybin
|
| 338 |
+
|
| 339 |
+
# Update PRECOL and POSTPIX
|
| 340 |
+
precol = precol // xbin
|
| 341 |
+
postpix = postpix // xbin
|
| 342 |
+
|
| 343 |
+
# Deal with detectors
|
| 344 |
+
if det in [1, 2]:
|
| 345 |
+
nx = nx // 2
|
| 346 |
+
n_ext = n_ext // 2
|
| 347 |
+
det_idx = np.arange(n_ext, dtype=np.int) + (det - 1) * n_ext
|
| 348 |
+
elif det is None:
|
| 349 |
+
det_idx = np.arange(n_ext).astype(int)
|
| 350 |
+
else:
|
| 351 |
+
raise ValueError("Bad value for det")
|
| 352 |
+
|
| 353 |
+
# change size for pre/postscan...
|
| 354 |
+
if not TRIM:
|
| 355 |
+
nx += n_ext * (precol + postpix)
|
| 356 |
+
ny += preline + postline
|
| 357 |
+
|
| 358 |
+
# allocate output array...
|
| 359 |
+
array = np.zeros((nx, ny), dtype="uint16")
|
| 360 |
+
gain_array = np.zeros((nx, ny), dtype="uint16")
|
| 361 |
+
order = np.argsort(np.array(xcol))
|
| 362 |
+
|
| 363 |
+
# insert extensions into master image...
|
| 364 |
+
for kk, i in enumerate(order[det_idx]):
|
| 365 |
+
|
| 366 |
+
# grab complete extension...
|
| 367 |
+
data, gaindata, predata, postdata, x1, y1 = lris_read_amp(
|
| 368 |
+
hdu, i + 1, redchip=redchip
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
# insert components into output array...
|
| 372 |
+
if not TRIM:
|
| 373 |
+
# insert predata...
|
| 374 |
+
buf = predata.shape
|
| 375 |
+
nxpre = buf[0]
|
| 376 |
+
xs = kk * precol
|
| 377 |
+
xe = xs + nxpre
|
| 378 |
+
|
| 379 |
+
array[xs:xe, :] = predata
|
| 380 |
+
gain_array[xs:xe, :] = predata
|
| 381 |
+
|
| 382 |
+
# insert data...
|
| 383 |
+
buf = data.shape
|
| 384 |
+
nxdata = buf[0]
|
| 385 |
+
nydata = buf[1]
|
| 386 |
+
|
| 387 |
+
# JB: have to track the number of xpixels
|
| 388 |
+
xs = n_ext * precol + nxdata_sum
|
| 389 |
+
xe = xs + nxdata
|
| 390 |
+
|
| 391 |
+
# now log how many pixels that was
|
| 392 |
+
nxdata_sum += nxdata
|
| 393 |
+
|
| 394 |
+
# Data section
|
| 395 |
+
# section = '[{:d}:{:d},{:d}:{:d}]'.format(preline,nydata-postline, xs, xe) # Eliminate lines
|
| 396 |
+
section = "[{:d}:{:d},{:d}:{:d}]".format(
|
| 397 |
+
preline, nydata, xs, xe
|
| 398 |
+
) # DONT eliminate lines
|
| 399 |
+
|
| 400 |
+
dsec.append(section)
|
| 401 |
+
array[xs:xe, :] = data # Include postlines
|
| 402 |
+
gain_array[xs:xe, :] = gaindata # Include postlines
|
| 403 |
+
|
| 404 |
+
# ; insert postdata...
|
| 405 |
+
buf = postdata.shape
|
| 406 |
+
nxpost = buf[0]
|
| 407 |
+
xs = nx - n_ext * postpix + kk * postpix
|
| 408 |
+
xe = xs + nxpost
|
| 409 |
+
section = "[:,{:d}:{:d}]".format(xs, xe)
|
| 410 |
+
osec.append(section)
|
| 411 |
+
|
| 412 |
+
array[xs:xe, :] = postdata
|
| 413 |
+
gain_array[xs:xe, :] = postdata
|
| 414 |
+
|
| 415 |
+
else:
|
| 416 |
+
buf = data.shape
|
| 417 |
+
nxdata = buf[0]
|
| 418 |
+
nydata = buf[1]
|
| 419 |
+
|
| 420 |
+
xs = (x1 - xmin) // xbin
|
| 421 |
+
xe = xs + nxdata
|
| 422 |
+
ys = (y1 - ymin) // ybin
|
| 423 |
+
ye = ys + nydata - postline
|
| 424 |
+
|
| 425 |
+
yin1 = preline
|
| 426 |
+
yin2 = nydata - postline
|
| 427 |
+
|
| 428 |
+
array[xs:xe, ys:ye] = data[:, yin1:yin2]
|
| 429 |
+
gain_array[xs:xe, ys:ye] = gaindata[:, yin1:yin2]
|
| 430 |
+
|
| 431 |
+
# make sure BZERO is a valid integer for IRAF
|
| 432 |
+
obzero = head0["BZERO"]
|
| 433 |
+
head0["O_BZERO"] = obzero
|
| 434 |
+
head0["BZERO"] = 32768 - obzero
|
| 435 |
+
|
| 436 |
+
# Return, transposing array back to goofy Python indexing
|
| 437 |
+
return array.T, head0
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
def lris_read_amp(inp, ext, redchip=False, applygain=True):
|
| 441 |
+
"""
|
| 442 |
+
Modified from pypeit.spectrographs.keck_lris.lris_read_amp -- Jon Brown, Josh Bloom
|
| 443 |
+
cf. https://github.com/KerryPaterson/Imaging_pipelines
|
| 444 |
+
Read one amplifier of an LRIS multi-extension FITS image
|
| 445 |
+
|
| 446 |
+
Parameters
|
| 447 |
+
----------
|
| 448 |
+
inp: tuple
|
| 449 |
+
(str,int) filename, extension
|
| 450 |
+
(hdu,int) FITS hdu, extension
|
| 451 |
+
|
| 452 |
+
Returns
|
| 453 |
+
-------
|
| 454 |
+
data
|
| 455 |
+
predata
|
| 456 |
+
postdata
|
| 457 |
+
x1
|
| 458 |
+
y1
|
| 459 |
+
|
| 460 |
+
;------------------------------------------------------------------------
|
| 461 |
+
function lris_read_amp, filename, ext, $
|
| 462 |
+
linebias=linebias, nobias=nobias, $
|
| 463 |
+
predata=predata, postdata=postdata, header=header, $
|
| 464 |
+
x1=x1, x2=x2, y1=y1, y2=y2, GAINDATA=gaindata
|
| 465 |
+
;------------------------------------------------------------------------
|
| 466 |
+
; Read one amp from LRIS mHDU image
|
| 467 |
+
;------------------------------------------------------------------------
|
| 468 |
+
"""
|
| 469 |
+
# Parse input
|
| 470 |
+
if isinstance(inp, str):
|
| 471 |
+
hdu = fits.open(inp)
|
| 472 |
+
else:
|
| 473 |
+
hdu = inp
|
| 474 |
+
|
| 475 |
+
# Get the pre and post pix values
|
| 476 |
+
# for LRIS red POSTLINE = 20, POSTPIX = 80, PRELINE = 0, PRECOL = 12
|
| 477 |
+
head0 = hdu[0].header
|
| 478 |
+
precol = head0["precol"]
|
| 479 |
+
postpix = head0["postpix"]
|
| 480 |
+
|
| 481 |
+
# Deal with binning
|
| 482 |
+
binning = head0["BINNING"]
|
| 483 |
+
xbin, ybin = [int(ibin) for ibin in binning.split(",")]
|
| 484 |
+
precol = precol // xbin
|
| 485 |
+
postpix = postpix // xbin
|
| 486 |
+
|
| 487 |
+
# get entire extension...
|
| 488 |
+
temp = hdu[ext].data.transpose() # Silly Python nrow,ncol formatting
|
| 489 |
+
tsize = temp.shape
|
| 490 |
+
nxt = tsize[0]
|
| 491 |
+
|
| 492 |
+
# parse the DETSEC keyword to determine the size of the array.
|
| 493 |
+
header = hdu[ext].header
|
| 494 |
+
detsec = header["DETSEC"]
|
| 495 |
+
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten()
|
| 496 |
+
|
| 497 |
+
# parse the DATASEC keyword to determine the size of the science region (unbinned)
|
| 498 |
+
datasec = header["DATASEC"]
|
| 499 |
+
xdata1, xdata2, ydata1, ydata2 = np.array(
|
| 500 |
+
load_sections(datasec, fmt_iraf=False)
|
| 501 |
+
).flatten()
|
| 502 |
+
|
| 503 |
+
# grab the components...
|
| 504 |
+
predata = temp[0:precol, :]
|
| 505 |
+
# datasec appears to have the x value for the keywords that are zero
|
| 506 |
+
# based. This is only true in the image header extensions
|
| 507 |
+
# not true in the main header. They also appear inconsistent between
|
| 508 |
+
# LRISr and LRISb!
|
| 509 |
+
# data = temp[xdata1-1:xdata2-1,*]
|
| 510 |
+
# data = temp[xdata1:xdata2+1, :]
|
| 511 |
+
|
| 512 |
+
# JB: LRIS-R is windowed differently, so the default pypeit checks fail
|
| 513 |
+
# xshape is calculated from datasec.
|
| 514 |
+
# For blue, its 1024,
|
| 515 |
+
# For red, the chip dimensions are different AND the observations are windowed
|
| 516 |
+
# In windowed mode each amplifier has differently sized data sections
|
| 517 |
+
if not redchip:
|
| 518 |
+
xshape = 1024 // xbin # blue
|
| 519 |
+
else:
|
| 520 |
+
xshape = xdata2 - xdata1 + 1 // xbin # red
|
| 521 |
+
|
| 522 |
+
# do some sanity checks
|
| 523 |
+
if (xdata1 - 1) != precol:
|
| 524 |
+
# msgs.error("Something wrong in LRIS datasec or precol")
|
| 525 |
+
errStr = "Something wrong in LRIS datasec or precol"
|
| 526 |
+
print(errStr)
|
| 527 |
+
|
| 528 |
+
if (xshape + precol + postpix) != temp.shape[0]:
|
| 529 |
+
# msgs.error("Wrong size for in LRIS detector somewhere. Funny binning?")
|
| 530 |
+
errStr = "Wrong size for in LRIS detector somewhere. Funny binning?"
|
| 531 |
+
print(errStr)
|
| 532 |
+
|
| 533 |
+
data = temp[precol : precol + xshape, :]
|
| 534 |
+
postdata = temp[nxt - postpix : nxt, :]
|
| 535 |
+
|
| 536 |
+
# flip in X as needed...
|
| 537 |
+
if x1 > x2:
|
| 538 |
+
xt = x2
|
| 539 |
+
x2 = x1
|
| 540 |
+
x1 = xt
|
| 541 |
+
data = np.flipud(data) # reverse(temporary(data),1)
|
| 542 |
+
|
| 543 |
+
# flip in Y as needed...
|
| 544 |
+
if y1 > y2:
|
| 545 |
+
yt = y2
|
| 546 |
+
y2 = y1
|
| 547 |
+
y1 = yt
|
| 548 |
+
data = np.fliplr(data)
|
| 549 |
+
predata = np.fliplr(predata)
|
| 550 |
+
postdata = np.fliplr(postdata)
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
# dummy gain data since we're keeping as uint16
|
| 554 |
+
gaindata = 0.0 * data + 1.0
|
| 555 |
+
|
| 556 |
+
return data, gaindata, predata, postdata, x1, y1
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
def load_sections(string, fmt_iraf=True):
|
| 560 |
+
"""
|
| 561 |
+
Modified from pypit.core.parse.load_sections -- Jon Brown, Josh Bloom
|
| 562 |
+
cf. https://github.com/KerryPaterson/Imaging_pipelines
|
| 563 |
+
From the input string, return the coordinate sections
|
| 564 |
+
|
| 565 |
+
Parameters
|
| 566 |
+
----------
|
| 567 |
+
string : str
|
| 568 |
+
character string of the form [x1:x2,y1:y2]
|
| 569 |
+
x1 = left pixel
|
| 570 |
+
x2 = right pixel
|
| 571 |
+
y1 = bottom pixel
|
| 572 |
+
y2 = top pixel
|
| 573 |
+
fmt_iraf : bool
|
| 574 |
+
Is the variable string in IRAF format (True) or
|
| 575 |
+
python format (False)
|
| 576 |
+
|
| 577 |
+
Returns
|
| 578 |
+
-------
|
| 579 |
+
sections : list (or None)
|
| 580 |
+
the detector sections
|
| 581 |
+
"""
|
| 582 |
+
xyrng = string.strip("[]()").split(",")
|
| 583 |
+
if xyrng[0] == ":":
|
| 584 |
+
xyarrx = [0, 0]
|
| 585 |
+
else:
|
| 586 |
+
xyarrx = xyrng[0].split(":")
|
| 587 |
+
# If a lower/upper limit on the array slicing is not given (e.g. [:100] has no lower index specified),
|
| 588 |
+
# set the lower/upper limit to be the first/last index.
|
| 589 |
+
if len(xyarrx[0]) == 0:
|
| 590 |
+
xyarrx[0] = 0
|
| 591 |
+
if len(xyarrx[1]) == 0:
|
| 592 |
+
xyarrx[1] = -1
|
| 593 |
+
if xyrng[1] == ":":
|
| 594 |
+
xyarry = [0, 0]
|
| 595 |
+
else:
|
| 596 |
+
xyarry = xyrng[1].split(":")
|
| 597 |
+
# If a lower/upper limit on the array slicing is not given (e.g. [5:] has no upper index specified),
|
| 598 |
+
# set the lower/upper limit to be the first/last index.
|
| 599 |
+
if len(xyarry[0]) == 0:
|
| 600 |
+
xyarry[0] = 0
|
| 601 |
+
if len(xyarry[1]) == 0:
|
| 602 |
+
xyarry[1] = -1
|
| 603 |
+
if fmt_iraf:
|
| 604 |
+
xmin = max(0, int(xyarry[0]) - 1)
|
| 605 |
+
xmax = int(xyarry[1])
|
| 606 |
+
ymin = max(0, int(xyarrx[0]) - 1)
|
| 607 |
+
ymax = int(xyarrx[1])
|
| 608 |
+
else:
|
| 609 |
+
xmin = max(0, int(xyarrx[0]))
|
| 610 |
+
xmax = int(xyarrx[1])
|
| 611 |
+
ymin = max(0, int(xyarry[0]))
|
| 612 |
+
ymax = int(xyarry[1])
|
| 613 |
+
return [[xmin, xmax], [ymin, ymax]]
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
def sec2slice(
|
| 617 |
+
subarray, one_indexed=False, include_end=False, require_dim=None, transpose=False
|
| 618 |
+
):
|
| 619 |
+
"""
|
| 620 |
+
Modified from pypit.core.parse.sec2slice -- Jon Brown
|
| 621 |
+
|
| 622 |
+
Convert a string representation of an array subsection (slice) into
|
| 623 |
+
a list of slice objects.
|
| 624 |
+
|
| 625 |
+
Args:
|
| 626 |
+
subarray (str):
|
| 627 |
+
The string to convert. Should have the form of normal slice
|
| 628 |
+
operation, 'start:stop:step'. The parser ignores whether or
|
| 629 |
+
not the string has the brackets '[]', but the string must
|
| 630 |
+
contain the appropriate ':' and ',' characters.
|
| 631 |
+
one_indexed (:obj:`bool`, optional):
|
| 632 |
+
The string should be interpreted as 1-indexed. Default
|
| 633 |
+
is to assume python indexing.
|
| 634 |
+
include_end (:obj:`bool`, optional):
|
| 635 |
+
**If** the end is defined, adjust the slice such that
|
| 636 |
+
the last element is included. Default is to exclude the
|
| 637 |
+
last element as with normal python slicing.
|
| 638 |
+
require_dim (:obj:`int`, optional):
|
| 639 |
+
Test if the string indicates the slice along the proper
|
| 640 |
+
number of dimensions.
|
| 641 |
+
transpose (:obj:`bool`, optional):
|
| 642 |
+
Transpose the order of the returned slices. The
|
| 643 |
+
following are equivalent::
|
| 644 |
+
|
| 645 |
+
tslices = parse_sec2slice('[:10,10:]')[::-1]
|
| 646 |
+
tslices = parse_sec2slice('[:10,10:]', transpose=True)
|
| 647 |
+
|
| 648 |
+
Returns:
|
| 649 |
+
tuple: A tuple of slice objects, one per dimension of the
|
| 650 |
+
prospective array.
|
| 651 |
+
|
| 652 |
+
Raises:
|
| 653 |
+
TypeError:
|
| 654 |
+
Raised if the input `subarray` is not a string.
|
| 655 |
+
ValueError:
|
| 656 |
+
Raised if the string does not match the required
|
| 657 |
+
dimensionality or if the string does not look like a
|
| 658 |
+
slice.
|
| 659 |
+
"""
|
| 660 |
+
# Check it's a string
|
| 661 |
+
if not isinstance(subarray, (str, bytes)):
|
| 662 |
+
raise TypeError("Can only parse string-based subarray sections.")
|
| 663 |
+
# Remove brackets if they're included
|
| 664 |
+
sections = subarray.strip("[]").split(",")
|
| 665 |
+
# Check the dimensionality
|
| 666 |
+
ndim = len(sections)
|
| 667 |
+
if require_dim is not None and ndim != require_dim:
|
| 668 |
+
raise ValueError(
|
| 669 |
+
"Number of slices ({0}) in {1} does not match ".format(ndim, subarray)
|
| 670 |
+
+ "required dimensions ({0}).".format(require_dim)
|
| 671 |
+
)
|
| 672 |
+
# Convert the slice of each dimension from a string to a slice
|
| 673 |
+
# object
|
| 674 |
+
slices = []
|
| 675 |
+
for s in sections:
|
| 676 |
+
# Must be able to find the colon
|
| 677 |
+
if ":" not in s:
|
| 678 |
+
raise ValueError("Unrecognized slice string: {0}".format(s))
|
| 679 |
+
# Initial conversion
|
| 680 |
+
_s = [None if x == "" else int(x) for x in s.split(":")]
|
| 681 |
+
if len(_s) > 3:
|
| 682 |
+
raise ValueError(
|
| 683 |
+
"String as too many sections. Must have format 'start:stop:step'."
|
| 684 |
+
)
|
| 685 |
+
if len(_s) < 3:
|
| 686 |
+
# Include step
|
| 687 |
+
_s += [None]
|
| 688 |
+
if one_indexed:
|
| 689 |
+
# Decrement to convert from 1- to 0-indexing
|
| 690 |
+
_s = [None if x is None else x - 1 for x in _s]
|
| 691 |
+
if include_end and _s[1] is not None:
|
| 692 |
+
# Increment to include last
|
| 693 |
+
_s[1] += 1
|
| 694 |
+
# Append the new slice
|
| 695 |
+
slices += [slice(*_s)]
|
| 696 |
+
return tuple(slices[::-1] if transpose else slices)
|
splits/tiny_test.jsonl
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"image_id": "LR.20100708.41739", "image": "./data/LR.20100708.41739.fits", "ra": 264.942, "dec": 27.3245, "pixscale": 0.135, "rotation_angle": -89.9999583, "dim_1":
|
|
|
|
| 1 |
+
{"image_id": "LR.20100708.41739", "image": "./data/LR.20100708.41739.fits", "ra": 264.942, "dec": 27.3245, "pixscale": 0.135, "rotation_angle": -89.9999583, "dim_1": 2520, "dim_2": 3768, "exposure_time": 270}
|