{ "cells": [ { "cell_type": "code", "execution_count": 71, "id": "b4c4c986", "metadata": {}, "outputs": [], "source": [ "import os\n", "from tqdm import tqdm\n", "import glob\n", "from astropy.io import fits\n", "import os\n", "from astropy.io import fits\n", "from astropy.wcs import WCS\n", "from spherical_geometry.polygon import SphericalPolygon\n", "import os\n", "from astropy.io import fits\n", "from astropy.wcs import WCS\n", "from spherical_geometry.polygon import SphericalPolygon\n", "from sklearn.cluster import AgglomerativeClustering\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "from astropy.io import fits\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import shutil\n", "\n", "def get_all_fits_files(root_dir):\n", " # Use glob to recursively find all .fits files\n", " pattern = os.path.join(root_dir, '**', '*LR*.fits')\n", " fits_files = glob.glob(pattern, recursive=True)\n", " return fits_files" ] }, { "cell_type": "code", "execution_count": 56, "id": "ba3bf5f7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1014" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "valid_fits_paths = get_all_fits_files('./GBI-16-2D/prelim_data')\n", "len(valid_fits_paths)" ] }, { "cell_type": "code", "execution_count": 57, "id": "a9a90d18", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1014\n", "861\n" ] } ], "source": [ "df_test = pd.read_json('./GBI-16-2D/splits/full_test.jsonl', lines=True)\n", "df_train = pd.read_json('./GBI-16-2D/splits/full_train.jsonl', lines=True)\n", "\n", "df = pd.concat([df_train, df_test])\n", "\n", "print(len(df))\n", "df = df[df['exposure_time'] >= 30]\n", "print(len(df))" ] }, { "cell_type": "code", "execution_count": 58, "id": "f965da24", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symmetric?\n", "True\n", "(861, 861)\n" ] } ], "source": [ "latitudes = list(df['dec'])\n", "longitudes = list(df['ra'])\n", "\n", "n_points = len(latitudes)\n", "\n", "# Repeat each point n_points times for lat1, lon1\n", "lat1 = np.repeat(latitudes, n_points)\n", "lon1 = np.repeat(longitudes, n_points)\n", "\n", "# Tile the whole array n_points times for lat2, lon2\n", "lat2 = np.tile(latitudes, n_points)\n", "lon2 = np.tile(longitudes, n_points)\n", "\n", "# Calculates angular separation between two spherical coords\n", "# This can be lat/lon or ra/dec\n", "# Taken from astropy\n", "def angular_separation_deg(lon1, lat1, lon2, lat2):\n", " lon1 = np.deg2rad(lon1)\n", " lon2 = np.deg2rad(lon2)\n", " lat1 = np.deg2rad(lat1)\n", " lat2 = np.deg2rad(lat2)\n", " \n", " sdlon = np.sin(lon2 - lon1)\n", " cdlon = np.cos(lon2 - lon1)\n", " slat1 = np.sin(lat1)\n", " slat2 = np.sin(lat2)\n", " clat1 = np.cos(lat1)\n", " clat2 = np.cos(lat2)\n", "\n", " num1 = clat2 * sdlon\n", " num2 = clat1 * slat2 - slat1 * clat2 * cdlon\n", " denominator = slat1 * slat2 + clat1 * clat2 * cdlon\n", "\n", " return np.rad2deg(np.arctan2(np.hypot(num1, num2), denominator))\n", "\n", "# Compute the pairwise angular separations\n", "angular_separations = angular_separation_deg(lon1, lat1, lon2, lat2)\n", "\n", "# Reshape the result into a matrix form\n", "angular_separations_matrix = angular_separations.reshape(n_points, n_points)\n", "\n", "def check_symmetric(a, rtol=1e-05, atol=1e-07):\n", " return np.allclose(a, a.T, rtol=rtol, atol=atol)\n", "\n", "print(\"Symmetric?\")\n", "print(check_symmetric(angular_separations_matrix))\n", "print(angular_separations_matrix.shape)" ] }, { "cell_type": "code", "execution_count": 59, "id": "6670e994", "metadata": {}, "outputs": [], "source": [ "KECK_DEG_PER_PIXEL = 3.75e-5\n", "KECK_FOV = 3768 * KECK_DEG_PER_PIXEL\n", "THRESH = KECK_FOV * 2\n", "\n", "clustering = AgglomerativeClustering(n_clusters=None, metric='precomputed', linkage='single', distance_threshold=THRESH)\n", "labels = clustering.fit_predict(angular_separations_matrix)" ] }, { "cell_type": "code", "execution_count": 60, "id": "ec592fb5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████| 137/137 [00:00<00:00, 1211.58it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Max subset with minimum distance: 137\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "RA_NAME = 'ra'\n", "DEC_NAME = 'dec'\n", "\n", "def max_subset_with_min_distance(points, min_distance):\n", " subset = []\n", " for i, row in points.iterrows():\n", " if all(angular_separation_deg(row[RA_NAME], row[DEC_NAME], existing_point[RA_NAME], existing_point[DEC_NAME]) >= min_distance for existing_point in subset):\n", " subset.append(row)\n", " return subset\n", "\n", "all_subsets = []\n", "\n", "for label in tqdm(np.unique(labels)):\n", " cds = df[labels == label]\n", " subset = max_subset_with_min_distance(cds, THRESH)\n", " all_subsets.extend(subset)\n", "\n", "print(\"Max subset with minimum distance:\", len(all_subsets))\n", "\n", "locations = pd.DataFrame(all_subsets)" ] }, { "cell_type": "code", "execution_count": 74, "id": "b141c2e9", "metadata": {}, "outputs": [], "source": [ "for path in [\"./GBI-16-2D/prelim_data/\" + s.split('/')[-1] for s in locations['image']]:\n", " shutil.move(path, path.replace(\"prelim_data\", \"data\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }