File size: 7,490 Bytes
f8c5348 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
{
"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",
"\"\"\"\n",
"Use this code after downloading imagery using\n",
"keck_downloading file.\n",
"\n",
"\"\"\"\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",
"Code to compute all angular separations between pairwise images from single RA DEC\n",
"values.\n",
"\n",
"\"\"\"\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",
"'''\n",
"Initial agglomerative clustering.\n",
"Since we don't have WCS info, the above threshold is very conservative.\n",
"'''\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",
"\"\"\"\n",
"Only select images that are at least THRESH apart from each other.\n",
"\n",
"\"\"\"\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
}
|