Skip to content

Commit

Permalink
Make all setters atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushiDaksh committed Jul 18, 2023
1 parent c419634 commit c9921d9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
57 changes: 42 additions & 15 deletions tardis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import numpy as np
import pandas as pd

INVALID_ION_ERROR_MSG = "Atomic number, ion_number pair not present in model"


class LastLineInteraction(object):
@classmethod
Expand Down Expand Up @@ -86,31 +88,53 @@ def atomic_number(self):

@atomic_number.setter
def atomic_number(self, value):
self._atomic_number = value
self.update_last_interaction_filter()
old_atomic_number = self._atomic_number
try:
self._atomic_number = value
self.update_last_interaction_filter()
except:
self._atomic_number = old_atomic_number
raise ValueError(INVALID_ION_ERROR_MSG)

@property
def ion_number(self):
return self._ion_number

@ion_number.setter
def ion_number(self, value):
self._ion_number = value
self.update_last_interaction_filter()
old_ion_number = self._ion_number
try:
self._ion_number = value
self.update_last_interaction_filter()
except:
self._ion_number = old_ion_number
raise ValueError(INVALID_ION_ERROR_MSG)

def set_ion(self, atomic_number, ion_number):
self._atomic_number = atomic_number
self._ion_number = ion_number
self.update_last_interaction_filter()
old_atomic_number = self._atomic_number
old_ion_number = self._ion_number
try:
self._atomic_number = atomic_number
self._ion_number = ion_number
self.update_last_interaction_filter()
except:
self._atomic_number = old_atomic_number
self._ion_number = old_ion_number
raise ValueError(INVALID_ION_ERROR_MSG)

@property
def shell(self):
return self._shell

@shell.setter
def shell(self, value):
self._shell = value
self.update_last_interaction_filter()
old_shell = self._shell
try:
self._shell = value
self.update_last_interaction_filter()
except:
self._shell = old_shell
raise ValueError("Invalid shell number")

def update_last_interaction_filter(self):
if self.packet_filter_mode == "packet_out_nu":
Expand Down Expand Up @@ -142,29 +166,32 @@ def update_last_interaction_filter(self):
self.last_line_interaction_shell_id == self.shell
)

self.last_line_in = self.lines.iloc[
last_line_in = self.lines.iloc[
self.last_line_interaction_in_id[packet_filter]
]
self.last_line_out = self.lines.iloc[
last_line_out = self.lines.iloc[
self.last_line_interaction_out_id[packet_filter]
]

if self.atomic_number is not None:
self.last_line_in = self.last_line_in.xs(
last_line_in = last_line_in.xs(
self.atomic_number, level="atomic_number", drop_level=False
)
self.last_line_out = self.last_line_out.xs(
last_line_out = last_line_out.xs(
self.atomic_number, level="atomic_number", drop_level=False
)

if self.ion_number is not None:
self.last_line_in = self.last_line_in.xs(
last_line_in = last_line_in.xs(
self.ion_number, level="ion_number", drop_level=False
)
self.last_line_out = self.last_line_out.xs(
last_line_out = last_line_out.xs(
self.ion_number, level="ion_number", drop_level=False
)

self.last_line_in = last_line_in
self.last_line_out = last_line_out

last_line_in_count = self.last_line_in.line_id.value_counts()
last_line_out_count = self.last_line_out.line_id.value_counts()

Expand Down
25 changes: 19 additions & 6 deletions tardis/visualization/widgets/grotrian.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,15 @@ def filter_mode(self):
@filter_mode.setter
def filter_mode(self, value):
assert value in self.FILTER_MODES

# Set the atomic_number and ion_number in the appropriate analysis object
self._line_interaction_analysis[value].set_ion(
self.atomic_number, self.ion_number
)
self._line_interaction_analysis[value].shell = self.shell

self._filter_mode = value

self._compute_transitions()

@property
Expand All @@ -310,6 +318,16 @@ def set_ion(self, atomic_number, ion_number):
Sets the atomic number and ion number
"""
assert type(atomic_number) is int and type(ion_number) is int
if (atomic_number, ion_number) not in self._level_energy_data.index or (
atomic_number,
ion_number,
) not in self._level_population_data.index:
raise ValueError(
"The (atomic_number, ion_number) pair doesn't exist in model"
)
self._line_interaction_analysis[self.filter_mode].set_ion(
atomic_number, ion_number
)

self._atomic_number = atomic_number
self._ion_number = ion_number
Expand Down Expand Up @@ -342,6 +360,7 @@ def shell(self):
@shell.setter
def shell(self, value):
assert value is None or type(value) is int
self._line_interaction_analysis[self.filter_mode].shell = value
self._shell = value
self._compute_level_data()
self._compute_transitions()
Expand All @@ -360,12 +379,6 @@ def _compute_transitions(self):
"""
Computes the excitation/de-excitation line transition data for the arrows in the widget
"""
# Get relevant lines for current simulation
self._line_interaction_analysis[self.filter_mode].set_ion(
self.atomic_number, self.ion_number
)
self._line_interaction_analysis[self.filter_mode].shell = self.shell

### Get the excitation/de-excitation transitions from LastLineInteraction object
excite_lines = (
self._line_interaction_analysis[self.filter_mode]
Expand Down

0 comments on commit c9921d9

Please sign in to comment.