From a5d8bcb7aece6476bdb616e05fc93a5c043e9227 Mon Sep 17 00:00:00 2001 From: Tianlu Yuan <5412915+tianluyuan@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:52:10 -0600 Subject: [PATCH 1/4] save contour files to specified output directory --- skyreader/plot/plot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skyreader/plot/plot.py b/skyreader/plot/plot.py index bce980f4..e5ebf7c5 100644 --- a/skyreader/plot/plot.py +++ b/skyreader/plot/plot.py @@ -773,8 +773,8 @@ def bounding_box(ra, dec, theta, phi): tab = {"ra (rad)": ras, "dec (rad)": decs} savename = unique_id + ".contour_" + val + ".txt" try: - LOGGER.info("Dumping to {savename}") - ascii.write(tab, savename, overwrite=True) + LOGGER.info(f"Dumping to {savename}") + ascii.write(tab, self.output_dir / savename, overwrite=True) except OSError: LOGGER.error( "OS Error prevented contours from being written, " @@ -836,7 +836,7 @@ def bounding_box(ra, dec, theta, phi): ) plt.legend(fontsize=6, loc="lower left") # Dump the whole contour - path = unique_id + ".contour.pkl" + path = self.output_dir / f"{unique_id}.contour.pkl" print("Saving contour to", path) with open(path, "wb") as f: pickle.dump(saving_contours, f) From f066d9ee40713cd9f9e93da2803aabfcbe080fa7 Mon Sep 17 00:00:00 2001 From: Tianlu Yuan <5412915+tianluyuan@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:01:44 -0600 Subject: [PATCH 2/4] full path in savename --- skyreader/plot/plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skyreader/plot/plot.py b/skyreader/plot/plot.py index e5ebf7c5..9b11560c 100644 --- a/skyreader/plot/plot.py +++ b/skyreader/plot/plot.py @@ -771,10 +771,10 @@ def bounding_box(ra, dec, theta, phi): ras = list(np.asarray(saving_contours[i][0]).T[0]) decs = list(np.asarray(saving_contours[i][0]).T[1]) tab = {"ra (rad)": ras, "dec (rad)": decs} - savename = unique_id + ".contour_" + val + ".txt" + savename = self.output_dir / f"{unique_id}.contour_{val}.txt" try: LOGGER.info(f"Dumping to {savename}") - ascii.write(tab, self.output_dir / savename, overwrite=True) + ascii.write(tab, savename, overwrite=True) except OSError: LOGGER.error( "OS Error prevented contours from being written, " From 5b844f851cd68d8d78cf4b1dc77ba79537ad62d8 Mon Sep 17 00:00:00 2001 From: Tianlu Yuan <5412915+tianluyuan@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:57:33 -0600 Subject: [PATCH 3/4] move contour saving into private helper function --- skyreader/plot/plot.py | 63 +++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/skyreader/plot/plot.py b/skyreader/plot/plot.py index 9b11560c..58aa35fd 100644 --- a/skyreader/plot/plot.py +++ b/skyreader/plot/plot.py @@ -755,30 +755,6 @@ def bounding_box(ra, dec, theta, phi): linestyle='dashed', label=contour_label ) - # Output contours in RA, dec instead of theta, phi - saving_contours: list = [] - for contours in contours_by_level: - saving_contours.append([]) - for contour in contours: - saving_contours[-1].append([]) - theta, phi = contour.T - ras = phi - decs = np.pi/2 - theta - for tmp_ra, tmp_dec in zip(ras, decs): - saving_contours[-1][-1].append([tmp_ra, tmp_dec]) - # Save the individual contours, send messages - for i, val in enumerate(["50", "90"]): - ras = list(np.asarray(saving_contours[i][0]).T[0]) - decs = list(np.asarray(saving_contours[i][0]).T[1]) - tab = {"ra (rad)": ras, "dec (rad)": decs} - savename = self.output_dir / f"{unique_id}.contour_{val}.txt" - try: - LOGGER.info(f"Dumping to {savename}") - ascii.write(tab, savename, overwrite=True) - except OSError: - LOGGER.error( - "OS Error prevented contours from being written, " - "maybe a memory issue. Error is:\n{err}") uncertainty = [(ra_minus, ra_plus), (dec_minus, dec_plus)] fits_header = format_fits_header( @@ -835,11 +811,6 @@ def bounding_box(ra, dec, theta, phi): linestyle=cont_sty ) plt.legend(fontsize=6, loc="lower left") - # Dump the whole contour - path = self.output_dir / f"{unique_id}.contour.pkl" - print("Saving contour to", path) - with open(path, "wb") as f: - pickle.dump(saving_contours, f) # save flattened map equatorial_map, column_names = prepare_flattened_map( @@ -878,6 +849,40 @@ def bounding_box(ra, dec, theta, phi): bbox_inches='tight' ) + self._save_contours(contours_by_level, unique_id) LOGGER.info("done.") plt.close() + + def _save_contours(self, contours_by_level, unique_id) -> None: + # Output contours in RA, dec instead of theta, phi + saving_contours: list = [] + for contours in contours_by_level: + saving_contours.append([]) + for contour in contours: + saving_contours[-1].append([]) + theta, phi = contour.T + ras = phi + decs = np.pi/2 - theta + for tmp_ra, tmp_dec in zip(ras, decs): + saving_contours[-1][-1].append([tmp_ra, tmp_dec]) + + # Save the individual contours + for i, val in enumerate(["50", "90"]): + ras = list(np.asarray(saving_contours[i][0]).T[0]) + decs = list(np.asarray(saving_contours[i][0]).T[1]) + tab = {"ra (rad)": ras, "dec (rad)": decs} + savepath = self.output_dir / f"{unique_id}.contour_{val}.txt" + try: + LOGGER.info(f"Dumping to {savepath}") + ascii.write(tab, savepath, overwrite=True) + except OSError: + LOGGER.error( + "OS Error prevented contours from being written, " + "maybe a memory issue. Error is:\n{err}") + + # Dump the whole contour + path = self.output_dir / f"{unique_id}.contour.pkl" + print("Saving contour to", path) + with open(path, "wb") as f: + pickle.dump(saving_contours, f) From 6af30673f5458401682ea42bf9f18a42b0546f83 Mon Sep 17 00:00:00 2001 From: Tianlu Yuan <5412915+tianluyuan@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:03:19 -0600 Subject: [PATCH 4/4] use fstrings --- skyreader/plot/plot.py | 5 ++--- skyreader/plot/plotting_tools.py | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/skyreader/plot/plot.py b/skyreader/plot/plot.py index 58aa35fd..3c09e6e0 100644 --- a/skyreader/plot/plot.py +++ b/skyreader/plot/plot.py @@ -626,8 +626,7 @@ def bounding_box(ra, dec, theta, phi): contour_colors, contours_by_level ): - contour_label = contour_label + ' - area: {0:.2f} sqdeg'.format( - contour_area_sqdeg) + contour_label = contour_label + f' - area: {contour_area_sqdeg:.2f} sqdeg' first = True for contour in contours: theta, phi = contour.T @@ -746,7 +745,7 @@ def bounding_box(ra, dec, theta, phi): # convert to square-degrees bounding_contour_area *= (180.*180.)/(np.pi*np.pi) contour_label = r'90% Bounding rectangle' + \ - ' - area: {0:.2f} sqdeg'.format(bounding_contour_area) + f' - area: {bounding_contour_area:.2f} sqdeg' healpy.projplot( bounding_theta, bounding_phi, diff --git a/skyreader/plot/plotting_tools.py b/skyreader/plot/plotting_tools.py index 551cd88e..bf8c7bc8 100644 --- a/skyreader/plot/plotting_tools.py +++ b/skyreader/plot/plotting_tools.py @@ -33,7 +33,7 @@ def format_fits_header( ('EVENTID', event_id), ('SENDER', 'IceCube Collaboration'), ('EventMJD', mjd), - ('I3TYPE', '%s'%event_type,'Alert Type'), + ('I3TYPE', f'{event_type}','Alert Type'), ('RA', np.round(ra,2),'Degree'), ('DEC', np.round(dec,2),'Degree'), ('RA_ERR_PLUS', np.round(uncertainty[0][1],2), @@ -99,12 +99,12 @@ def hp_ticklabels(zoom=False, lonra=None, latra=None, rot=None, bounds=None): pe = [path_effects.Stroke(linewidth=1.5, foreground='white'), path_effects.Normal()] for _ in lats: - healpy.projtext(lon_offset, _, "{:.0f}$^\circ$".format(_), + healpy.projtext(lon_offset, _, f"{_:.0f}$^\\circ$", lonlat=True, path_effects=pe, fontsize=10) if zoom: for _ in lons: healpy.projtext(_, lat_offset, - "{:.0f}$^\circ$".format(_), lonlat=True, + f"{_:.0f}$^\\circ$", lonlat=True, path_effects=pe, fontsize=10) else: ax.annotate(r"$\bf{-180^\circ}$", xy=(1.7, 0.625), size="medium")