From 8e3312788e57700dbcb54cd23214749cd1f4619e Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Mon, 12 Jul 2021 22:23:11 +0530 Subject: [PATCH 1/5] Changed to fstring --- tardis/analysis.py | 26 ++++++++------------------ tardis/gui/widgets.py | 27 ++++++++------------------- tardis/io/parsers/csvy.py | 4 ++-- tardis/io/util.py | 2 +- tardis/model/base.py | 5 ++--- 5 files changed, 21 insertions(+), 43 deletions(-) diff --git a/tardis/analysis.py b/tardis/analysis.py index ec8ee20deaa..da2ebc92f41 100644 --- a/tardis/analysis.py +++ b/tardis/analysis.py @@ -192,21 +192,11 @@ def plot_wave_in_out(self, fig, do_clf=True, plot_resonance=True): def onpick(event): print("-" * 80) print( - "Line_in (%d/%d):\n%s" - % ( - len(event.ind), - self.current_no_packets, - self.last_line_list_in.ix[event.ind], - ) + f"Line_in ({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" ) print("\n\n") print( - "Line_out (%d/%d):\n%s" - % ( - len(event.ind), - self.current_no_packets, - self.last_line_list_in.ix[event.ind], - ) + f"Line_out ({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" ) print("^" * 80) @@ -262,7 +252,7 @@ def load_t_inner(self, iterations=None): for iter in iterations: t_inners.append( - hdf_store["model%03d/configuration" % iter].ix["t_inner"] + hdf_store[f"model{iter:003d}/configuration"].ix["t_inner"] ) hdf_store.close() @@ -281,8 +271,8 @@ def load_t_rads(self, iterations=None): iterations = self.iterations[iterations] for iter in iterations: - current_iter = "iter%03d" % iter - t_rads_dict[current_iter] = hdf_store["model%03d/t_rads" % iter] + current_iter = f"iter{iter:003d}" + t_rads_dict[current_iter] = hdf_store[f"model{iter:003d}/t_rads"] t_rads = pd.DataFrame(t_rads_dict) hdf_store.close() @@ -319,7 +309,7 @@ def load_level_populations(self, iterations=None): iterations = self.iterations[iterations] for iter in iterations: - current_iter = "iter%03d" % iter + current_iter = f"iter{iter:003d}" level_populations_dict[current_iter] = hdf_store[ f"model{iter:03d}/level_populations" ] @@ -381,7 +371,7 @@ def load_spectrum(self, iteration, spectrum_keyword="luminosity_density"): hdf_store = pd.HDFStore(self.hdf5_fname, "r") spectrum = hdf_store[ - "model%03d/%s" % (self.iterations[iteration], spectrum_keyword) + f"model{self.iterations[iteration]:003d}/{spectrum_keyword}" ] hdf_store.close() return spectrum @@ -426,7 +416,7 @@ def get_last_line_interaction(self, iteration=-1): self.load_atom_data() hdf_store = pd.HDFStore(self.hdf5_fname, "r") - model_string = "model" + ("%03d" % iteration) + "/%s" + model_string = "model" + (f"{iteration:003d}" ) + "/%s" last_line_interaction_in_id = hdf_store[ model_string % "last_line_interaction_in_id" ].values diff --git a/tardis/gui/widgets.py b/tardis/gui/widgets.py index 5f793642e87..15082a13ff7 100644 --- a/tardis/gui/widgets.py +++ b/tardis/gui/widgets.py @@ -818,7 +818,7 @@ def __init__(self, index, tablecreator, parent=None): self.parent = parent self.shell_index = index self.setGeometry(400, 150, 200, 400) - self.setWindowTitle("Shell %d Abundances" % (self.shell_index + 1)) + self.setWindowTitle(f"Shell {self.shell_index + 1} Abundances" ) self.atomstable = QtWidgets.QTableView() self.ionstable = QtWidgets.QTableView() self.levelstable = QtWidgets.QTableView() @@ -829,7 +829,7 @@ def __init__(self, index, tablecreator, parent=None): self.table1_data = self.parent.model.plasma.abundance[self.shell_index] self.atomsdata = self.createTable( - [["Z = "], ["Count (Shell %d)" % (self.shell_index + 1)]], + [["Z = "], [f"Count (Shell {self.shell_index + 1})" ]], iterate_header=(2, 0), index_info=self.table1_data.index.values.tolist(), ) @@ -855,7 +855,7 @@ def on_atom_header_double_clicked(self, index): self.shell_index ].ix[self.current_atom_index] self.ionsdata = self.createTable( - [["Ion: "], ["Count (Z = %d)" % self.current_atom_index]], + [["Ion: "], [f"Count (Z = {self.current_atom_index})"]], iterate_header=(2, 0), index_info=self.table2_data.index.values.tolist(), ) @@ -889,7 +889,7 @@ def on_ion_header_double_clicked(self, index): self.shell_index ].ix[self.current_atom_index, self.current_ion_index] self.levelsdata = self.createTable( - [["Level: "], ["Count (Ion %d)" % self.current_ion_index]], + [["Level: "], [f"Count (Ion {self.current_ion_index})" ]], iterate_header=(2, 0), index_info=self.table3_data.index.values.tolist(), ) @@ -931,11 +931,7 @@ def __init__(self, parent, wavelength_start, wavelength_end, tablecreator): self.parent = parent self.setGeometry(180 + len(self.parent.line_info) * 20, 150, 250, 400) self.setWindowTitle( - "Line Interaction: %.2f - %.2f (A) " - % ( - wavelength_start, - wavelength_end, - ) + f"Line Interaction: {wavelength_start:.2f} - {wavelength_end:.2f} (A) " ) self.layout = QtWidgets.QVBoxLayout() packet_nu_line_interaction = analysis.LastLineInteraction.from_model( @@ -1010,7 +1006,7 @@ def get_data(self, wavelength_start, wavelength_end): / self.grouped_lines_in.wavelength.count().sum() ).values.tolist() for z, ion in self.ions_in: - self.header_list.append("Z = %d: Ion %d" % (z, ion)) + self.header_list.append(f"Z = {z}: Ion {ion}" ) def get_transition_table(self, lines, atom, ion): """Called by the two methods below to get transition table for @@ -1043,14 +1039,7 @@ def get_transition_table(self, lines, atom, ion): transitions_count[index] /= float(s) for key, value in transitions.items(): transitions_parsed.append( - "%d-%d (%.2f A)" - % ( - key[0], - key[1], - self.parent.model.atom_data.lines.ix[value[0]][ - "wavelength" - ], - ) + f"{key[0]}-{key[1]} {self.parent.model.atom_data.lines.ix[value[0]]["wavelength"]}" ) return transitions_parsed, transitions_count @@ -1225,7 +1214,7 @@ def on_species_clicked(self, index): last_line_in_model = self.createTable( [ last_line_in_string, - ["Num. pkts %d" % current_last_line_in.wavelength.count()], + [f"Num. pkts {current_last_line_in.wavelength.count()}" ], ] ) last_line_in_model.add_data(last_line_count) diff --git a/tardis/io/parsers/csvy.py b/tardis/io/parsers/csvy.py index 15fcdcb2294..e3503eaebfb 100644 --- a/tardis/io/parsers/csvy.py +++ b/tardis/io/parsers/csvy.py @@ -32,7 +32,7 @@ def load_csvy(fname): yaml_end_ind = i break else: - raise ValueError("End %s not found" % (YAML_DELIMITER)) + raise ValueError(f"End {YAML_DELIMITER} not found" ) yaml_dict = yaml.load("".join(yaml_lines[1:-1]), YAMLLoader) try: data = pd.read_csv(fname, skiprows=yaml_end_ind + 1) @@ -67,7 +67,7 @@ def load_yaml_from_csvy(fname): yaml_end_ind = i break else: - raise ValueError("End %s not found" % (YAML_DELIMITER)) + raise ValueError(f"End {YAML_DELIMITER} not found" ) yaml_dict = yaml.load("".join(yaml_lines[1:-1]), YAMLLoader) return yaml_dict diff --git a/tardis/io/util.py b/tardis/io/util.py index e4edcfa500a..87af2b5881d 100644 --- a/tardis/io/util.py +++ b/tardis/io/util.py @@ -393,7 +393,7 @@ def download_from_url(url, dst): first_byte = 0 if first_byte >= file_size: return file_size - header = {"Range": "bytes=%s-%s" % (first_byte, file_size)} + header = {f"Range": "bytes={first_byte}-{file_size}" } pbar = tqdm( total=file_size, initial=first_byte, diff --git a/tardis/model/base.py b/tardis/model/base.py index 555f78759a7..387da98446f 100644 --- a/tardis/model/base.py +++ b/tardis/model/base.py @@ -606,9 +606,8 @@ def from_csvy(cls, config): ), "CSVY field descriptions exist without corresponding csv data" if unsupported_columns != set(): logger.warning( - "The following columns are specified in the csvy" - "model file, but are IGNORED by TARDIS: %s" - % (str(unsupported_columns)) + f"The following columns are specified in the csvy model file, but are IGNORED by TARDIS: {str(unsupported_columns)}" + ) time_explosion = config.supernova.time_explosion.cgs From d522ab14f23cf13a0e8268097369394b83646e81 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 13 Jul 2021 14:57:21 +0530 Subject: [PATCH 2/5] Updated strings --- tardis/analysis.py | 6 ++++-- tardis/gui/widgets.py | 4 ++-- tardis/model/base.py | 7 ++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tardis/analysis.py b/tardis/analysis.py index da2ebc92f41..7fe93855b33 100644 --- a/tardis/analysis.py +++ b/tardis/analysis.py @@ -192,11 +192,13 @@ def plot_wave_in_out(self, fig, do_clf=True, plot_resonance=True): def onpick(event): print("-" * 80) print( - f"Line_in ({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" + "Line_in" + f"({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" ) print("\n\n") print( - f"Line_out ({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" + "Line_out" + f"({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" ) print("^" * 80) diff --git a/tardis/gui/widgets.py b/tardis/gui/widgets.py index 15082a13ff7..3d4dc2f6b30 100644 --- a/tardis/gui/widgets.py +++ b/tardis/gui/widgets.py @@ -1039,7 +1039,7 @@ def get_transition_table(self, lines, atom, ion): transitions_count[index] /= float(s) for key, value in transitions.items(): transitions_parsed.append( - f"{key[0]}-{key[1]} {self.parent.model.atom_data.lines.ix[value[0]]["wavelength"]}" + f"{key[0]}-{key[1]} {self.parent.model.atom_data.lines.ix[value[0]]["wavelength"]:.2f} A" ) return transitions_parsed, transitions_count @@ -1214,7 +1214,7 @@ def on_species_clicked(self, index): last_line_in_model = self.createTable( [ last_line_in_string, - [f"Num. pkts {current_last_line_in.wavelength.count()}" ], + [f"Num. pkts {current_last_line_in.wavelength.count()}"], ] ) last_line_in_model.add_data(last_line_count) diff --git a/tardis/model/base.py b/tardis/model/base.py index 387da98446f..9ebab59a180 100644 --- a/tardis/model/base.py +++ b/tardis/model/base.py @@ -606,9 +606,10 @@ def from_csvy(cls, config): ), "CSVY field descriptions exist without corresponding csv data" if unsupported_columns != set(): logger.warning( - f"The following columns are specified in the csvy model file, but are IGNORED by TARDIS: {str(unsupported_columns)}" - - ) + "The following columns are " + "specified in the csvy model file," + f" but are IGNORED by TARDIS: {str(unsupported_columns)}" + ) time_explosion = config.supernova.time_explosion.cgs From aec2e517b099cf4a8e7eeb5452274fc4d4138a9d Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 13 Jul 2021 20:33:48 +0530 Subject: [PATCH 3/5] 003 typo fixed --- tardis/analysis.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tardis/analysis.py b/tardis/analysis.py index 7fe93855b33..04befeabf85 100644 --- a/tardis/analysis.py +++ b/tardis/analysis.py @@ -254,7 +254,7 @@ def load_t_inner(self, iterations=None): for iter in iterations: t_inners.append( - hdf_store[f"model{iter:003d}/configuration"].ix["t_inner"] + hdf_store[f"model{iter:03d}/configuration"].ix["t_inner"] ) hdf_store.close() @@ -273,8 +273,8 @@ def load_t_rads(self, iterations=None): iterations = self.iterations[iterations] for iter in iterations: - current_iter = f"iter{iter:003d}" - t_rads_dict[current_iter] = hdf_store[f"model{iter:003d}/t_rads"] + current_iter = f"iter{iter:03d}" + t_rads_dict[current_iter] = hdf_store[f"model{iter:03d}/t_rads"] t_rads = pd.DataFrame(t_rads_dict) hdf_store.close() @@ -311,7 +311,7 @@ def load_level_populations(self, iterations=None): iterations = self.iterations[iterations] for iter in iterations: - current_iter = f"iter{iter:003d}" + current_iter = f"iter{iter:03d}" level_populations_dict[current_iter] = hdf_store[ f"model{iter:03d}/level_populations" ] @@ -373,7 +373,7 @@ def load_spectrum(self, iteration, spectrum_keyword="luminosity_density"): hdf_store = pd.HDFStore(self.hdf5_fname, "r") spectrum = hdf_store[ - f"model{self.iterations[iteration]:003d}/{spectrum_keyword}" + f"model{self.iterations[iteration]:03d}/{spectrum_keyword}" ] hdf_store.close() return spectrum @@ -418,7 +418,7 @@ def get_last_line_interaction(self, iteration=-1): self.load_atom_data() hdf_store = pd.HDFStore(self.hdf5_fname, "r") - model_string = "model" + (f"{iteration:003d}" ) + "/%s" + model_string = "model" + (f"{iteration:03d}" ) + "/%s" last_line_interaction_in_id = hdf_store[ model_string % "last_line_interaction_in_id" ].values From 87c198c2169259e4ed23d989a1846a1c4bf86f1b Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Tue, 13 Jul 2021 22:23:50 +0530 Subject: [PATCH 4/5] fixed error --- tardis/analysis.py | 6 ++++-- tardis/gui/widgets.py | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tardis/analysis.py b/tardis/analysis.py index 04befeabf85..655b6187ab2 100644 --- a/tardis/analysis.py +++ b/tardis/analysis.py @@ -193,12 +193,14 @@ def onpick(event): print("-" * 80) print( "Line_in" - f"({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" + f"({len(event.ind)}/{self.current_no_packets})" + f":\n{self.last_line_list_in.ix[event.ind]}" ) print("\n\n") print( "Line_out" - f"({len(event.ind)}/{self.current_no_packets}):\n{self.last_line_list_in.ix[event.ind]}" + f"({len(event.ind)}/{self.current_no_packets})" + f":\n{self.last_line_list_in.ix[event.ind]}" ) print("^" * 80) diff --git a/tardis/gui/widgets.py b/tardis/gui/widgets.py index 3d4dc2f6b30..4811aa7a556 100644 --- a/tardis/gui/widgets.py +++ b/tardis/gui/widgets.py @@ -1039,7 +1039,8 @@ def get_transition_table(self, lines, atom, ion): transitions_count[index] /= float(s) for key, value in transitions.items(): transitions_parsed.append( - f"{key[0]}-{key[1]} {self.parent.model.atom_data.lines.ix[value[0]]["wavelength"]:.2f} A" + f"{key[0]}-{key[1]}" + f"{self.parent.model.atom_data.lines.ix[value[0]]["wavelength"]:.2f} A" ) return transitions_parsed, transitions_count From ae4e954d8037bf2bdd71d03bdb60e681d9851f12 Mon Sep 17 00:00:00 2001 From: Shashank Venkat Date: Thu, 15 Jul 2021 16:53:09 +0530 Subject: [PATCH 5/5] fixed PEP8 violation in line 195 --- tardis/analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tardis/analysis.py b/tardis/analysis.py index 655b6187ab2..32c9ae62d77 100644 --- a/tardis/analysis.py +++ b/tardis/analysis.py @@ -193,7 +193,7 @@ def onpick(event): print("-" * 80) print( "Line_in" - f"({len(event.ind)}/{self.current_no_packets})" + f"({len(event.ind)}/{self.current_no_packets})"\ f":\n{self.last_line_list_in.ix[event.ind]}" ) print("\n\n")