diff --git a/lhcsmapi/api/analysis/output/qh/quench_heater_ccc_output.py b/lhcsmapi/api/analysis/output/qh/quench_heater_ccc_output.py index 3973936835546e33416f7a30cde7da7ac658c3d7..d315586d474d68fa4de43b11c2481e8b1c5f9f4d 100644 --- a/lhcsmapi/api/analysis/output/qh/quench_heater_ccc_output.py +++ b/lhcsmapi/api/analysis/output/qh/quench_heater_ccc_output.py @@ -2,15 +2,8 @@ from typing import List -import pandas as pd from lhcsmapi.api.analysis.output import output_types -from lhcsmapi.api.analysis.qh import ( - QHCCCAnalysis, - VoltageCurrentEvent, - VoltageCurrentResult, - VoltageEvent, - VoltageResult, -) +from lhcsmapi.api.analysis.qh import QHCCCAnalysis from lhcsmapi.api.analysis.output.qh import _common as output_common @@ -30,16 +23,10 @@ def get_summary(analysis: QHCCCAnalysis) -> List[output_types.Output]: Returns: A list of Output instances with only one HTMLOutput.""" - voltage_current_events: List[VoltageCurrentEvent] = analysis.get_result("voltage_current_events") - voltage_current_results: List[VoltageCurrentResult] = analysis.get_result("voltage_current_results") - - voltage_events: List[VoltageEvent] = analysis.get_result("voltage_events") - voltage_results: List[VoltageResult] = analysis.get_result("voltage_results") - - events = voltage_current_events + voltage_events - results = voltage_current_results + voltage_results - - return output_common.get_summary_output(events, results) + return output_common.get_summary_output( + analysis.voltage_current_events + analysis.voltage_events, + analysis.voltage_current_results + analysis.voltage_results, + ) def get_output(analysis: QHCCCAnalysis) -> List[output_types.Output]: @@ -54,20 +41,10 @@ def get_output(analysis: QHCCCAnalysis) -> List[output_types.Output]: output_list: List[output_types.Output] = [] if analysis.initial_charge_check: - low_charge_events: pd.DataFrame = analysis.get_result("low_charge_events") - - df_html = low_charge_events.to_html() - output_list.append(output_types.TextOutput("Low level charge events:")) - output_list.append(output_types.HTMLOutput(df_html)) - - voltage_current_events: List[VoltageCurrentEvent] = analysis.get_result("voltage_current_events") - voltage_current_results: List[VoltageCurrentResult] = analysis.get_result("voltage_current_results") + output_list.append(output_types.HTMLOutput(analysis.low_charge_events.to_html())) - voltage_events: List[VoltageEvent] = analysis.get_result("voltage_events") - voltage_results: List[VoltageResult] = analysis.get_result("voltage_results") - - if len(voltage_events) + len(voltage_current_events) == 0: + if len(analysis.voltage_events) + len(analysis.voltage_current_events) == 0: output_list.append( output_types.HTMLOutput( '<h2><span style="background-color: #3DCF1A">   ' @@ -77,9 +54,11 @@ def get_output(analysis: QHCCCAnalysis) -> List[output_types.Output]: return output_list output_list.extend( - output_common.map_two_arg_func(output_common.get_voltage_output, voltage_events, voltage_results) + output_common.map_two_arg_func( + output_common.get_voltage_output, analysis.voltage_events, analysis.voltage_results + ) + output_common.map_two_arg_func( - output_common.get_voltage_current_output, voltage_current_events, voltage_current_results + output_common.get_voltage_current_output, analysis.voltage_current_events, analysis.voltage_current_results ) ) diff --git a/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_current_output.py b/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_current_output.py index 0c58d651349caf17a2923f66d50ea1812a962cd8..af61154bf6d50b4dfb1cb770bcfbd8129b1aabcf 100644 --- a/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_current_output.py +++ b/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_current_output.py @@ -1,13 +1,11 @@ """Module containing functions presenting the results of the QHDA for RB/RQ circuits.""" -from typing import List - from lhcsmapi.api.analysis.output import output_types -from lhcsmapi.api.analysis.qh import QuenchHeaterVoltageCurrentAnalysis, VoltageCurrentEvent, VoltageCurrentResult +from lhcsmapi.api.analysis.qh import QuenchHeaterVoltageCurrentAnalysis from lhcsmapi.api.analysis.output.qh import _common as output_common -def get_summary(analysis: QuenchHeaterVoltageCurrentAnalysis) -> List[output_types.Output]: +def get_summary(analysis: QuenchHeaterVoltageCurrentAnalysis) -> list[output_types.Output]: """Returns the summary of the analysis as an HTML table with each row being an analyzed event. Example: @@ -23,13 +21,12 @@ def get_summary(analysis: QuenchHeaterVoltageCurrentAnalysis) -> List[output_typ Returns: A list of Output instances with only one HTMLOutput.""" - voltage_current_events: List[VoltageCurrentEvent] = analysis.get_result("voltage_current_events") - voltage_current_results: List[VoltageCurrentResult] = analysis.get_result("voltage_current_results") - - return output_common.get_summary_with_links_output(voltage_current_events, voltage_current_results) + return output_common.get_summary_with_links_output( + analysis.voltage_current_events, analysis.voltage_current_results + ) -def get_output(analysis: QuenchHeaterVoltageCurrentAnalysis) -> List[output_types.Output]: +def get_output(analysis: QuenchHeaterVoltageCurrentAnalysis) -> list[output_types.Output]: """Returns the output of the analysis as a list of Output instances. Args: @@ -48,9 +45,6 @@ def get_output(analysis: QuenchHeaterVoltageCurrentAnalysis) -> List[output_type ) outputs.extend(output_common.get_voltage_current_output(event, result)) - voltage_current_events: List[VoltageCurrentEvent] = analysis.get_result("voltage_current_events") - voltage_current_results: List[VoltageCurrentResult] = analysis.get_result("voltage_current_results") - return output_common.get_output_wrapper( - analysis.get_analysis_output, voltage_current_events, voltage_current_results, add_outputs + analysis.get_analysis_output, analysis.voltage_current_events, analysis.voltage_current_results, add_outputs ) diff --git a/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_output.py b/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_output.py index 9318013e7701cbfc313d86cda3269e573c8827d9..1e00bbe65d49527679917b32480a63db3f70831d 100644 --- a/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_output.py +++ b/lhcsmapi/api/analysis/output/qh/quench_heater_voltage_output.py @@ -3,7 +3,7 @@ from typing import List from lhcsmapi.api.analysis.output import output_types -from lhcsmapi.api.analysis.qh import QuenchHeaterVoltageAnalysis, VoltageEvent, VoltageResult +from lhcsmapi.api.analysis.qh import QuenchHeaterVoltageAnalysis from lhcsmapi.api.analysis.output.qh import _common as output_common @@ -23,10 +23,7 @@ def get_summary(analysis: QuenchHeaterVoltageAnalysis) -> List[output_types.Outp Returns: A list of Output instances with only one HTMLOutput.""" - voltage_events: List[VoltageEvent] = analysis.get_result("voltage_events") - voltage_results: List[VoltageResult] = analysis.get_result("voltage_results") - - return output_common.get_summary_with_links_output(voltage_events, voltage_results) + return output_common.get_summary_with_links_output(analysis.voltage_events, analysis.voltage_results) def get_output(analysis: QuenchHeaterVoltageAnalysis) -> List[output_types.Output]: @@ -48,9 +45,6 @@ def get_output(analysis: QuenchHeaterVoltageAnalysis) -> List[output_types.Outpu ) outputs.extend(output_common.get_voltage_output(event, result)) - voltage_events: List[VoltageEvent] = analysis.get_result("voltage_events") - voltage_results: List[VoltageResult] = analysis.get_result("voltage_results") - return output_common.get_output_wrapper( - analysis.get_analysis_output, voltage_events, voltage_results, add_outputs_with_links + analysis.get_analysis_output, analysis.voltage_events, analysis.voltage_results, add_outputs_with_links ) diff --git a/lhcsmapi/api/analysis/qh/quench_heater_ccc.py b/lhcsmapi/api/analysis/qh/quench_heater_ccc.py index 0b5d006963e7b164d5876c2d53b052db9ee065a9..aef26d9b087e86e3ace298d55c3a8b2412749df5 100644 --- a/lhcsmapi/api/analysis/qh/quench_heater_ccc.py +++ b/lhcsmapi/api/analysis/qh/quench_heater_ccc.py @@ -2,7 +2,7 @@ from __future__ import annotations import datetime -from typing import Union, List +from typing import Optional, Union from lhcsmapi.api.analysis.qh import _common @@ -53,6 +53,12 @@ class QHCCCAnalysis(analysis.Analysis): if initial_charge_check: self._charge_check_level = 800 + self.voltage_current_events: list[_common.VoltageCurrentEvent] = [] + self.voltage_current_results: list[_common.VoltageCurrentResult] = [] + self.voltage_events: list[_common.VoltageEvent] = [] + self.voltage_results: list[_common.VoltageResult] = [] + self.low_charge_events: Optional[pd.DataFrame] = None + @property def initial_charge_check(self): """Returns whether if the initial charge check has been enabled.""" @@ -148,15 +154,15 @@ class QHCCCAnalysis(analysis.Analysis): meta_circuit_type, circuit_name, source_timestamp_df_i, "I_HDS", True ) - def _analyze_voltage(self, circuit_type, circuit_name, source_timestamp_df_i): - result: list[_common.VoltageResult] = [] + def _analyze_voltage(self, circuit_type, circuit_name, source_timestamp_df_i) -> list[_common.VoltageResult]: + result = [] u_signals = self._u_signals_dfs[circuit_name] u_signals_ref = self._u_signals_dfs_ref[circuit_name] for i, row in source_timestamp_df_i.iterrows(): timestamp = row["timestamp"] if u_signals[i]: - self._voltage_events.append( + self.voltage_events.append( _common.VoltageEvent( row["source"], timestamp, circuit_type, circuit_name, u_signals[i], u_signals_ref[i] ) @@ -172,7 +178,7 @@ class QHCCCAnalysis(analysis.Analysis): _common.VoltageResult(row["source"], timestamp, first_last_u_comp_df, tau_u_comp_df, is_qh_ok) ) else: - self._voltage_events.append( + self.voltage_events.append( _common.VoltageEvent(row["source"], timestamp, circuit_type, circuit_name, None, None) ) result.append(_common.VoltageResult(row["source"], timestamp, None, None, True)) @@ -211,7 +217,7 @@ class QHCCCAnalysis(analysis.Analysis): i_signals_ref[i], 0.085 if circuit_type == "RB" else 0.025, ) - self._voltage_currents_events.append( + self.voltage_current_events.append( _common.VoltageCurrentEvent( row["source"], row["timestamp"], @@ -247,7 +253,7 @@ class QHCCCAnalysis(analysis.Analysis): result.append( _common.VoltageCurrentResult(row["source"], row["timestamp"], None, None, None, None, True) ) - self._voltage_currents_events.append( + self.voltage_current_events.append( _common.VoltageCurrentEvent( row["source"], row["timestamp"], circuit_type, circuit_name, None, None, None, None, None, None ) @@ -257,48 +263,31 @@ class QHCCCAnalysis(analysis.Analysis): def analyze(self): if self._initial_charge_check and "max_charge" in self._source_timestamp_qds_df.columns: - low_charge_events = self._source_timestamp_qds_df[ + self.low_charge_events = self._source_timestamp_qds_df[ self._source_timestamp_qds_df["max_charge"] < self._charge_check_level ] - self.register_result("low_charge_events", low_charge_events) # We only analyze high charge events self._source_timestamp_qds_df = self._source_timestamp_qds_df[ self._source_timestamp_qds_df["max_charge"] >= self._charge_check_level ] - r_result: List[_common.VoltageCurrentResult] = [] - i_result: List[_common.VoltageResult] = [] - self._voltage_currents_events: List[_common.VoltageCurrentEvent] = [] - self._voltage_events: List[_common.VoltageEvent] = [] - if not self._source_timestamp_qds_df.empty: for circuit_name, source_timestamp_df_i in self._groups: circuit_type = source_timestamp_df_i["circuit_type"].values[0] if circuit_type in ("RB", "RQ"): - r_result.extend(self._analyze_voltage_current(circuit_type, circuit_name, source_timestamp_df_i)) + self.voltage_current_results.extend( + self._analyze_voltage_current(circuit_type, circuit_name, source_timestamp_df_i) + ) else: - i_result.extend(self._analyze_voltage(circuit_type, circuit_name, source_timestamp_df_i)) - - self.register_result("voltage_current_results", r_result) - self.register_result("voltage_results", i_result) - self.register_result("voltage_current_events", self._voltage_currents_events) - self.register_result("voltage_events", self._voltage_events) - self.register_result( - "events_number", (len(self._voltage_currents_events) or 0) + (len(self._voltage_events) or 0) - ) - - def get_analysis_output(self) -> bool: - voltage_current_results: list[_common.VoltageCurrentResult] = self.get_result("voltage_current_results") - voltage_results: list[_common.VoltageResult] = self.get_result("voltage_results") - - for voltage_current_result in voltage_current_results: - if not voltage_current_result.is_qh_ok: - return False + self.voltage_results.extend( + self._analyze_voltage(circuit_type, circuit_name, source_timestamp_df_i) + ) - for voltage_result in voltage_results: - if not voltage_result.is_qh_ok: - return False + self.events_number = len(self.voltage_current_events) + len(self.voltage_events) - return True + def get_analysis_output(self) -> bool: + return all(voltage_current_result.is_qh_ok for voltage_current_result in self.voltage_current_results) and all( + voltage_result.is_qh_ok for voltage_result in self.voltage_results + ) diff --git a/lhcsmapi/api/analysis/qh/quench_heater_voltage_analysis.py b/lhcsmapi/api/analysis/qh/quench_heater_voltage_analysis.py index e5f84781d71a2b3425572db40370a256733f508e..e8407d733a50a44a69f2d6fc68e739138dd7463d 100644 --- a/lhcsmapi/api/analysis/qh/quench_heater_voltage_analysis.py +++ b/lhcsmapi/api/analysis/qh/quench_heater_voltage_analysis.py @@ -1,7 +1,7 @@ """Module containing the class to analyze QH events for IPD/IPQ/IT circuits.""" from __future__ import annotations -from typing import List, Union +from typing import Union import datetime import pandas as pd @@ -40,7 +40,8 @@ class QuenchHeaterVoltageAnalysis(analysis.Analysis): self._start_time = Time.to_unix_timestamp(start_time) self._stop_time = Time.to_unix_timestamp(stop_time) self._discharge_level = discharge_level - + self.voltage_events: list[_common.VoltageEvent] = [] + self.voltage_results: list[_common.VoltageResult] = [] super().__init__(identifier) def search_discharges(self) -> pd.DataFrame: @@ -55,14 +56,9 @@ class QuenchHeaterVoltageAnalysis(analysis.Analysis): self._signals_dfs_ref = _common.query_qh_pm(self._circuit_type, self._circuit_name, self._events, "U_HDS", True) def get_analysis_output(self) -> bool: - voltage_results: List[_common.VoltageResult] = self.get_result("voltage_results") - - return all(voltage_result.is_qh_ok for voltage_result in voltage_results) + return all(voltage_result.is_qh_ok for voltage_result in self.voltage_results) def analyze(self): - voltage_results = [] - voltage_events = [] - for index, (u_dfs, u_dfs_ref) in enumerate(zip(self._signals_dfs, self._signals_dfs_ref)): # logging here? # the notebook prints some timestamp @@ -75,12 +71,12 @@ class QuenchHeaterVoltageAnalysis(analysis.Analysis): source_event = self._events.at[index, "source"] if not u_dfs: - voltage_events.append( + self.voltage_events.append( _common.VoltageEvent( source_event, timestamp_event, self._circuit_type, self._circuit_name, None, None ) ) - voltage_results.append(_common.VoltageResult(source_event, timestamp_event, None, None, True)) + self.voltage_results.append(_common.VoltageResult(source_event, timestamp_event, None, None, True)) continue first_last_u_comp_df, tau_u_comp_df = _common.analyze_single_qh_voltage_with_ref( @@ -89,14 +85,11 @@ class QuenchHeaterVoltageAnalysis(analysis.Analysis): is_qh_ok = first_last_u_comp_df["result"].all() and tau_u_comp_df["result"].all() - voltage_events.append( + self.voltage_events.append( _common.VoltageEvent( source_event, timestamp_event, self._circuit_type, self._circuit_name, u_dfs, u_dfs_ref ) ) - voltage_results.append( + self.voltage_results.append( _common.VoltageResult(source_event, timestamp_event, first_last_u_comp_df, tau_u_comp_df, is_qh_ok) ) - - self.register_result("voltage_results", voltage_results) - self.register_result("voltage_events", voltage_events) diff --git a/lhcsmapi/api/analysis/qh/quench_heater_voltage_current_analysis.py b/lhcsmapi/api/analysis/qh/quench_heater_voltage_current_analysis.py index 61e5ec3f31d41dadc4a14187ca839c0f17244ee5..904346f1f653d536dfe37dac0a345460d938a340 100644 --- a/lhcsmapi/api/analysis/qh/quench_heater_voltage_current_analysis.py +++ b/lhcsmapi/api/analysis/qh/quench_heater_voltage_current_analysis.py @@ -7,7 +7,7 @@ from typing import List, Union import pandas as pd from lhcsmapi.api.analysis.qh import _common - +from lhcsmapi.api.analysis.qh import VoltageCurrentEvent, VoltageCurrentResult from lhcsmapi.api import analysis from lhcsmapi.Time import Time @@ -42,6 +42,8 @@ class QuenchHeaterVoltageCurrentAnalysis(analysis.Analysis): self._discharge_level = discharge_level self._signals_dfs = None self._signals_dfs_ref = None + self.voltage_current_events: list[VoltageCurrentEvent] = [] + self.voltage_current_results: list[VoltageCurrentResult] = [] super().__init__(identifier) @@ -81,8 +83,6 @@ class QuenchHeaterVoltageCurrentAnalysis(analysis.Analysis): self._signals_dfs_ref = {(source, timestamp): dfs for (source, timestamp, dfs) in qh_source_timestamp_dfs_ref} def analyze(self): - voltage_current_events = [] - voltage_current_results = [] for _, row in self._events.iterrows(): source = row["source"] @@ -92,12 +92,12 @@ class QuenchHeaterVoltageCurrentAnalysis(analysis.Analysis): i_hds_dfs = list(filter(lambda df: "I_HDS" in df.columns.values[0], self._signals_dfs[(source, timestamp)])) if not u_hds_dfs or not i_hds_dfs: - voltage_current_events.append( + self.voltage_current_events.append( _common.VoltageCurrentEvent( source, timestamp, self._circuit_type, self._circuit_name, None, None, None, None, None, None ) ) - voltage_current_results.append( + self.voltage_current_results.append( _common.VoltageCurrentResult(source, timestamp, None, None, None, None, True) ) continue @@ -141,7 +141,7 @@ class QuenchHeaterVoltageCurrentAnalysis(analysis.Analysis): and tau_comp_df["result"].all() and capacitance_comp_df["result"].all() ) - voltage_current_events.append( + self.voltage_current_events.append( _common.VoltageCurrentEvent( source, timestamp, @@ -155,29 +155,18 @@ class QuenchHeaterVoltageCurrentAnalysis(analysis.Analysis): r_hds_ref_dfs, ) ) - voltage_current_results.append( + self.voltage_current_results.append( _common.VoltageCurrentResult( source, timestamp, first_last_u_comp_df, first_r_comp_df, tau_comp_df, capacitance_comp_df, is_qh_ok ) ) - self.register_result("voltage_current_events", voltage_current_events) - self.register_result("voltage_current_results", voltage_current_results) - def get_analysis_output(self) -> bool: - voltage_current_results: List[_common.VoltageCurrentResult] = self.get_result("voltage_current_results") - - for voltage_current_result in voltage_current_results: - if not voltage_current_result.is_qh_ok: - return False - - return True + return all(voltage_current_result.is_qh_ok for voltage_current_result in self.voltage_current_results) def _clean_query_results(self): - if "voltage_current_events" in self._context: - del self._context["voltage_current_events"] - if "voltage_current_results" in self._context: - del self._context["voltage_current_results"] + self.voltage_current_events = [] + self.voltage_current_results = [] if self._signals_dfs is not None: del self._signals_dfs if self._signals_dfs_ref is not None: diff --git a/test/test_output.py b/test/test_output.py index 5b843d0f13f069c111e5daf14e97ea2182ecf65c..187d6660f5ba0817c29da0c05bf4b09bf83d8927 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -63,12 +63,8 @@ def test_qh_voltage_current_summary(): def _mock_results(analysis): files_directory = path.join(path.dirname(path.realpath(__file__)), "ccc_20220721-20220721-nonxcals-success") - voltage_current_events = pickle.load(open(f"{files_directory}/voltage_current_events.pkl", "rb")) - voltage_current_results = pickle.load(open(f"{files_directory}/voltage_current_results.pkl", "rb")) - voltage_events = pickle.load(open(f"{files_directory}/voltage_events.pkl", "rb")) - voltage_results = pickle.load(open(f"{files_directory}/voltage_results.pkl", "rb")) - - analysis.register_result("voltage_current_events", voltage_current_events) - analysis.register_result("voltage_current_results", voltage_current_results) - analysis.register_result("voltage_events", voltage_events) - analysis.register_result("voltage_results", voltage_results) + + analysis.voltage_current_events = pickle.load(open(f"{files_directory}/voltage_current_events.pkl", "rb")) + analysis.voltage_current_results = pickle.load(open(f"{files_directory}/voltage_current_results.pkl", "rb")) + analysis.voltage_events = pickle.load(open(f"{files_directory}/voltage_events.pkl", "rb")) + analysis.voltage_results = pickle.load(open(f"{files_directory}/voltage_results.pkl", "rb")) diff --git a/test/test_qh_ccc.py b/test/test_qh_ccc.py index 9a9d6a17517db89a1ceb997fc56aaa375591c9d5..30baa9a8d3baa6525d5dbf34e819e4b4d5db5478 100644 --- a/test/test_qh_ccc.py +++ b/test/test_qh_ccc.py @@ -40,9 +40,9 @@ def test_acceptance_ccc(directory_name, output): qhccc.analyze() - assert voltage_current_events == qhccc.get_result("voltage_current_events") - assert voltage_current_results == qhccc.get_result("voltage_current_results") - assert voltage_events == qhccc.get_result("voltage_events") - assert voltage_results == qhccc.get_result("voltage_results") - assert events_number == qhccc.get_result("events_number") + assert voltage_current_events == qhccc.voltage_current_events + assert voltage_current_results == qhccc.voltage_current_results + assert voltage_events == qhccc.voltage_events + assert voltage_results == qhccc.voltage_results + assert events_number == qhccc.events_number assert qhccc.get_analysis_output() is output diff --git a/test/test_qh_voltage.py b/test/test_qh_voltage.py index 77791fc0db9ae386a1439de9fd2a10be7df65b8c..59697caa81c4a5a3529adb7f3d9576aa8b306d04 100644 --- a/test/test_qh_voltage.py +++ b/test/test_qh_voltage.py @@ -31,6 +31,6 @@ def test_acceptance_voltage(directory_name, circuit_type, circuit_name, discharg qhv.analyze() - assert voltage_events == qhv.get_result("voltage_events") - assert voltage_results == qhv.get_result("voltage_results") + assert voltage_events == qhv.voltage_events + assert voltage_results == qhv.voltage_results assert qhv.get_analysis_output() is output diff --git a/test/test_qh_voltage_current.py b/test/test_qh_voltage_current.py index 730d6b1adfa185e1bc00684299c65647a9c9d611..c3ccbd547f55d81fdb2e24ea51c0fee09bc76480 100644 --- a/test/test_qh_voltage_current.py +++ b/test/test_qh_voltage_current.py @@ -28,6 +28,6 @@ def test_acceptance_voltage_current(directory_name, circuit_type, circuit_name, qhvi.analyze() - assert voltage_current_events == qhvi.get_result("voltage_current_events") - assert voltage_current_results == qhvi.get_result("voltage_current_results") + assert voltage_current_events == qhvi.voltage_current_events + assert voltage_current_results == qhvi.voltage_current_results assert qhvi.get_analysis_output() is output