From df72e7268ccf522fdedcce21893ca918eb34293b Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 08:58:42 -0400 Subject: [PATCH 1/8] Workaround list access --- .../properties/transition_probabilities.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tardis/plasma/properties/transition_probabilities.py b/tardis/plasma/properties/transition_probabilities.py index 9def7e650df..c5594776b2d 100644 --- a/tardis/plasma/properties/transition_probabilities.py +++ b/tardis/plasma/properties/transition_probabilities.py @@ -330,11 +330,20 @@ def calculate( names=p_deactivation.index.names, ) fb_cooling_probs.insert(0, "lines_idx", continuum_idx) - fb_cooling_probs.insert( - 0, - "transition_type", - level_idxs2transition_idx.at[("k", "bf"), "transition_type"], - ) + # Since 'at' seems to be faster than 'loc' we try it first + # However, if the index is not unique, 'at' will raise a ValueError + try: + fb_cooling_probs.insert( + 0, + "transition_type", + level_idxs2transition_idx.at[("k", "bf"), "transition_type"], + ) + except ValueError: + fb_cooling_probs.insert( + 0, + "transition_type", + level_idxs2transition_idx.loc[("k", "bf"), "transition_type"].values[0], + ) # Check if there are two-photon decays if "two-photon" in p_deactivation.index.get_level_values(1): From a0f278ba03a5ebcb89861a03fb2b568150e21029 Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 09:06:17 -0400 Subject: [PATCH 2/8] Formatting --- tardis/plasma/properties/transition_probabilities.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tardis/plasma/properties/transition_probabilities.py b/tardis/plasma/properties/transition_probabilities.py index c5594776b2d..7b823633aee 100644 --- a/tardis/plasma/properties/transition_probabilities.py +++ b/tardis/plasma/properties/transition_probabilities.py @@ -342,7 +342,9 @@ def calculate( fb_cooling_probs.insert( 0, "transition_type", - level_idxs2transition_idx.loc[("k", "bf"), "transition_type"].values[0], + level_idxs2transition_idx.loc[ + ("k", "bf"), "transition_type" + ].values[0], ) # Check if there are two-photon decays From e77a44b1f555d11a26ac8f492ecaa80d88aa112c Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 09:06:49 -0400 Subject: [PATCH 3/8] Added error if duplicate --- tardis/plasma/properties/atomic.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tardis/plasma/properties/atomic.py b/tardis/plasma/properties/atomic.py index 236edb01ecc..09dee61014d 100644 --- a/tardis/plasma/properties/atomic.py +++ b/tardis/plasma/properties/atomic.py @@ -467,6 +467,14 @@ def calculate(self, atomic_data): level_idxs2line_idx = pd.Series( np.arange(len(index)), index=index, name="lines_idx" ) + + # Check for duplicate indices + if level_idxs2line_idx.index.duplicated().any(): + raise ValueError( + "Duplicate indices in level_idxs2line_idx. " + "This should not happen." + ) + return level_idxs2line_idx From 4dc6fe48105b4b3a0c27ac099380f5e940b8969b Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 14:08:56 -0400 Subject: [PATCH 4/8] Remove duplicates --- tardis/plasma/properties/atomic.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tardis/plasma/properties/atomic.py b/tardis/plasma/properties/atomic.py index 09dee61014d..34368101b08 100644 --- a/tardis/plasma/properties/atomic.py +++ b/tardis/plasma/properties/atomic.py @@ -470,10 +470,15 @@ def calculate(self, atomic_data): # Check for duplicate indices if level_idxs2line_idx.index.duplicated().any(): - raise ValueError( + logger.warn( "Duplicate indices in level_idxs2line_idx. " - "This should not happen." + "Dropping duplicates. " + "This is an issue with the atomic data & carsus. " + "Once fixed upstream, this warning will be removed. " + "This will raise an error in the future instead. " + "See https://github.com/tardis-sn/carsus/issues/384" ) + level_idxs2line_idx = level_idxs2line_idx.drop_duplicates() return level_idxs2line_idx From fcb2f743f7d266d1256a0d607adc36ac50410d85 Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 14:26:22 -0400 Subject: [PATCH 5/8] Fix typo, add comment --- tardis/plasma/properties/atomic.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tardis/plasma/properties/atomic.py b/tardis/plasma/properties/atomic.py index 34368101b08..e8ec384bed2 100644 --- a/tardis/plasma/properties/atomic.py +++ b/tardis/plasma/properties/atomic.py @@ -464,6 +464,7 @@ def calculate(self, atomic_data): ], names=["source_level_idx", "destination_level_idx"], ) + level_idxs2line_idx = pd.Series( np.arange(len(index)), index=index, name="lines_idx" ) @@ -478,7 +479,14 @@ def calculate(self, atomic_data): "This will raise an error in the future instead. " "See https://github.com/tardis-sn/carsus/issues/384" ) - level_idxs2line_idx = level_idxs2line_idx.drop_duplicates() + # This is necessary since pd.DataFrame.drop_duplicates() + # does not remove duplicates if the data is different + # and only the index is duplicated. See the example given + # in the pandas documentation: + # https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop_duplicates.html + level_idxs2line_idx = level_idxs2line_idx[ + ~level_idxs2line_idx.index.duplicated() + ] return level_idxs2line_idx From df3399025a6a11216d243f532e1ff454a0e2a09c Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 14:26:36 -0400 Subject: [PATCH 6/8] Remove bandaid fix --- .../properties/transition_probabilities.py | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/tardis/plasma/properties/transition_probabilities.py b/tardis/plasma/properties/transition_probabilities.py index 7b823633aee..d11cf1fa90d 100644 --- a/tardis/plasma/properties/transition_probabilities.py +++ b/tardis/plasma/properties/transition_probabilities.py @@ -1,4 +1,5 @@ import logging +from operator import le import numpy as np import pandas as pd @@ -330,22 +331,11 @@ def calculate( names=p_deactivation.index.names, ) fb_cooling_probs.insert(0, "lines_idx", continuum_idx) - # Since 'at' seems to be faster than 'loc' we try it first - # However, if the index is not unique, 'at' will raise a ValueError - try: - fb_cooling_probs.insert( - 0, - "transition_type", - level_idxs2transition_idx.at[("k", "bf"), "transition_type"], - ) - except ValueError: - fb_cooling_probs.insert( - 0, - "transition_type", - level_idxs2transition_idx.loc[ - ("k", "bf"), "transition_type" - ].values[0], - ) + fb_cooling_probs.insert( + 0, + "transition_type", + level_idxs2transition_idx.at[("k", "bf"), "transition_type"], + ) # Check if there are two-photon decays if "two-photon" in p_deactivation.index.get_level_values(1): From 0dc0b3dcb68be8355808df197adcc815b449a4e1 Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 14:51:18 -0400 Subject: [PATCH 7/8] Cleanup --- tardis/plasma/properties/transition_probabilities.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tardis/plasma/properties/transition_probabilities.py b/tardis/plasma/properties/transition_probabilities.py index d11cf1fa90d..9def7e650df 100644 --- a/tardis/plasma/properties/transition_probabilities.py +++ b/tardis/plasma/properties/transition_probabilities.py @@ -1,5 +1,4 @@ import logging -from operator import le import numpy as np import pandas as pd From d7ce1e74ff4ce4ad8dd765d9069a3bdf86a4be4b Mon Sep 17 00:00:00 2001 From: Alexander Holas Date: Thu, 12 Oct 2023 14:52:04 -0400 Subject: [PATCH 8/8] More cleanup --- tardis/plasma/properties/atomic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tardis/plasma/properties/atomic.py b/tardis/plasma/properties/atomic.py index e8ec384bed2..6ade1ae1377 100644 --- a/tardis/plasma/properties/atomic.py +++ b/tardis/plasma/properties/atomic.py @@ -464,7 +464,6 @@ def calculate(self, atomic_data): ], names=["source_level_idx", "destination_level_idx"], ) - level_idxs2line_idx = pd.Series( np.arange(len(index)), index=index, name="lines_idx" )