From f5fc5c86c242e923662f3bb039174dd318af6e42 Mon Sep 17 00:00:00 2001 From: d-hoshino2626 Date: Thu, 17 Oct 2024 18:17:48 +0900 Subject: [PATCH 01/43] add time_constant function --- src/sorunlib/wiregrid.py | 183 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index d1ca15e4..cf9780d6 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -198,6 +198,112 @@ def _check_temperature_sensors(): _verify_temp_response(resp, 'AIN2C', 0) +def _check_wiregrid_position(): + """Check the wiregrid position.""" + actuator = run.CLIENTS['wiregrid']['actuator'] + resp = actuator.acq.status() + ls_status = resp.session['data']['fields']['limitswitch'] + if (ls_status['LSR1'] == 1 or ls_status['LSL1'] == 1) and not (ls_status['LSR2'] == 1 and ls_status['LSL2'] == 1): + position = 'outside' + elif (ls_status['LSR2'] == 1 or ls_status['LSL2'] == 1) and not (ls_status['LSR1'] == 1 or ls_status['LSL1'] == 1): + position = 'inside' + else: + raise RuntimeError("The wiregrid position is unknown. The limitswitch response is like this:\n" + + "LSR1: {}\n".format('ON' if ls_status['LSR1'] == 1 else 'OFF') + + "LSL1: {}\n".format('ON' if ls_status['LSL1'] == 1 else 'OFF') + + "LSR2: {}\n".format('ON' if ls_status['LSR2'] == 1 else 'OFF') + + "LSL2: {}\n".format('ON' if ls_status['LSL2'] == 1 else 'OFF') + + "Aborting...") + return position + + +def _stop_hwp_with_wiregrid(): + """Stop the HWP after comfirming the wiregrid is inserted.""" + # Check the wiregrid position + position = _check_wiregrid_position() + if position == 'outside': + raise RuntimeError("The wiregrid is not inserted. Aborting...") + elif position == 'inside': + # Stop the HWP + print("Starting to stop the HWP.") + run.hwp.stop(active=True) + print("The HWP stopped successfully.") + else: + raise RuntimeError("Unknown wiregrid position. Aborting...") + + +def _spin_hwp_with_wiregrid(target_hwp_direction): + """Spin the HWP to the target direction at 2Hz after comfirming the wiregrid is inserted. + + Args: + target_hwp_direction (str): Target HWP direction, 'forward' or 'backward'. + """ + # Check the wiregrid position + position = _check_wiregrid_position() + if position == 'outside': + raise RuntimeError("The wiregrid is not inserted. Aborting...") + elif position == 'inside': + if target_hwp_direction == 'forward': + print("Starting to spin up the HWP in forward.") + run.hwp.set_freq(freq=2.0) + print("The HWP is spinning at 2Hz in forward successfully.") + elif target_hwp_direction == 'backward': + print("Starting to spin up the HWP in backward.") + run.hwp.set_freq(freq=-2.0) + print("The HWP is spinning at 2Hz in backward successfully.") + else: + raise RuntimeError("Unknown HWP target direction. Aborting...") + else: + raise RuntimeError("Unknown wiregrid position. Aborting...") + + +def _reverse_hwp_with_wiregrid(initial_hwp_direction, streaming=False, stepwise_before=False, stepwise_after=False): + """Change the HWP direction after comfirming the wiregrid is inserted. + + Args: + initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. + streaming (bool): Do SMuRF streaming during the HWP direction chaging or not . Default is False. + stepwise_before (bool): Do stepwise rotation before changing HWP direction or not. Default is False. + stepwise_after (bool): Do stepwise rotation after changing HWP direction or not. Default is False. + """ + if initial_hwp_direction not in ['forward', 'backward']: + raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") + + current_hwp_direction = initial_hwp_direction + if current_hwp_direction == 'forward': + target_hwp_direction = 'backward' + elif current_hwp_direction == 'backward': + target_hwp_direction = 'forward' + + # Stop and spin up reversely the HWP + try: + # Enable SMuRF streams + if streaming: + stream_tag = f'wiregrid, wg_time_constant, hwp_change_to_{target_hwp_direction}' + if stepwise_before or stepwise_after: + stream_tag += ', wg_stepwise' + if _check_zenith(): + stream_tag += ', wg_el90' + run.smurf.stream('on', subtype='cal', tag=stream_tag) + # Stepwise rotation before stopping the HWP + if stepwise_before: + rotate(False) + # Stop the HWP + _stop_hwp_with_wiregrid() + # Spin up the HWP in the opposite direction + _spin_hwp_with_wiregrid(target_hwp_direction) + current_hwp_direction = target_hwp_direction + # Stepwise rotation after spinning up the HWP + if stepwise_after: + rotate(False) + finally: + # Stop SMuRF streams + if streaming: + run.smurf.stream('off') + + return current_hwp_direction + + # Public API def insert(): """Insert the wiregrid.""" @@ -305,3 +411,80 @@ def calibrate(continuous=False, elevation_check=True, boresight_check=True, finally: # Stop SMuRF streams run.smurf.stream('off') + + +def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True, stepwise_mid=False, repeat=1): + """ + Run a wiregrid time constant measurement. + + Args: + initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. + stepwise_first (bool): Do stepwise rotation or not before the first HWP speed change. Default is True. + stepwise_last (bool): Do stepwise rotation or not after the last HWP speed change. Default is True. + stepwise_mid (bool): Do stepwise rotation between each HWP speed change. Default is False. + repeat (int): Number of repeats. Default is 1. + If this is odd, the HWP direction will be changed to the opposite of the initial direction. + If this is even, the HWP direction will be the same as the initial direction. + """ + + # Check the initial HWP direction + if initial_hwp_direction not in ['forward', 'backward']: + raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") + current_hwp_direction = initial_hwp_direction + + # Check the repeat + if repeat < 1 or not isinstance(repeat, int): + raise RuntimeError("The repeat should be int and larger than 0. Aborting...") + + _check_agents_online() + _check_motor_on() + _check_telescope_position(elevation_check=True, boresight_check=False) + + if _check_zenith: + el_tag = ', wg_el90' + else: + el_tag = '' + + # Rotate for reference before insertion + rotate(continuous=True, duration=10) + + # Bias step (the wire grid is off the window) + bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.bias_step(tag=bs_tag, concurrent=True) + + # Insert the wiregrid + insert() + + for i in range(repeat): + # Bias step (the wire grid is on the window) + bs_tag = f'biasstep, wg_time_constant, wg_inserted, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.bias_step(tag=bs_tag, concurrent=True) + + stepwise_before = False + stepwise_after = False + if stepwise_first and i == 0: + # Stepwise rotation before the first HWP speed change + stepwise_before = True + if stepwise_mid and i != 0: + # Stepwise rotation between changing HWP speed + stepwise_before = True + if stepwise_last and i == repeat-1: + # Stepwise rotation after the last HWP speed change + stepwise_after = True + + # Spin the HWP in the opposite direction of the initial direction with streaming and stepwise rotation + current_hwp_direction = _reverse_hwp_with_wiregrid(current_hwp_direction, + streaming=True, + stepwise_before=stepwise_before, + stepwise_after=stepwise_after) + + # Bias step (the wire grid is on the window) + bs_tag = f'biasstep, wg_time_constant, wg_inserted, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.bias_step(tag=bs_tag, concurrent=True) + + # Eject the wiregrid + eject() + + # Bias step (the wire grid is off the window) + bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.bias_step(tag=bs_tag, concurrent=True) From f1e11576e9775b17e49ee0f69041bba2d8762dde Mon Sep 17 00:00:00 2001 From: d-hoshino2626 Date: Mon, 21 Oct 2024 15:10:33 +0900 Subject: [PATCH 02/43] add stream during insert/eject and sleep time before and after insert/eject --- src/sorunlib/wiregrid.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index cf9780d6..53d3f482 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -452,8 +452,18 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) - # Insert the wiregrid - insert() + # Insert the wiregrid with streaming + time.sleep(5) + try: + # Enable SMuRF streams + stream_tag = f'wg_time_constant, wg_inserting, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Insert the wiregrid + insert() + finally: + # Stop SMuRF streams + run.smurf.stream('off') + time.sleep(5) for i in range(repeat): # Bias step (the wire grid is on the window) @@ -482,8 +492,18 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True bs_tag = f'biasstep, wg_time_constant, wg_inserted, hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) - # Eject the wiregrid - eject() + # Eject the wiregrid with streaming + time.sleep(5) + try: + # Enable SMuRF streams + stream_tag = f'wg_time_constant, wg_ejecting, hwp_2hz_{current_hwp_direction}' + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Eject the wiregrid + eject() + finally: + # Stop SMuRF streams + run.smurf.stream('off') + time.sleep(5) # Bias step (the wire grid is off the window) bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag From e7610efc91f120896a8ac0c1ce8daf4a11f39193 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Oct 2024 06:11:00 +0000 Subject: [PATCH 03/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sorunlib/wiregrid.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 53d3f482..99138e13 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -209,11 +209,11 @@ def _check_wiregrid_position(): position = 'inside' else: raise RuntimeError("The wiregrid position is unknown. The limitswitch response is like this:\n" + - "LSR1: {}\n".format('ON' if ls_status['LSR1'] == 1 else 'OFF') + - "LSL1: {}\n".format('ON' if ls_status['LSL1'] == 1 else 'OFF') + - "LSR2: {}\n".format('ON' if ls_status['LSR2'] == 1 else 'OFF') + - "LSL2: {}\n".format('ON' if ls_status['LSL2'] == 1 else 'OFF') + - "Aborting...") + "LSR1: {}\n".format('ON' if ls_status['LSR1'] == 1 else 'OFF') + + "LSL1: {}\n".format('ON' if ls_status['LSL1'] == 1 else 'OFF') + + "LSR2: {}\n".format('ON' if ls_status['LSR2'] == 1 else 'OFF') + + "LSL2: {}\n".format('ON' if ls_status['LSL2'] == 1 else 'OFF') + + "Aborting...") return position @@ -234,7 +234,7 @@ def _stop_hwp_with_wiregrid(): def _spin_hwp_with_wiregrid(target_hwp_direction): """Spin the HWP to the target direction at 2Hz after comfirming the wiregrid is inserted. - + Args: target_hwp_direction (str): Target HWP direction, 'forward' or 'backward'. """ @@ -259,14 +259,14 @@ def _spin_hwp_with_wiregrid(target_hwp_direction): def _reverse_hwp_with_wiregrid(initial_hwp_direction, streaming=False, stepwise_before=False, stepwise_after=False): """Change the HWP direction after comfirming the wiregrid is inserted. - + Args: initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. streaming (bool): Do SMuRF streaming during the HWP direction chaging or not . Default is False. stepwise_before (bool): Do stepwise rotation before changing HWP direction or not. Default is False. stepwise_after (bool): Do stepwise rotation after changing HWP direction or not. Default is False. """ - if initial_hwp_direction not in ['forward', 'backward']: + if initial_hwp_direction not in ['forward', 'backward']: raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") current_hwp_direction = initial_hwp_direction @@ -293,7 +293,7 @@ def _reverse_hwp_with_wiregrid(initial_hwp_direction, streaming=False, stepwise_ # Spin up the HWP in the opposite direction _spin_hwp_with_wiregrid(target_hwp_direction) current_hwp_direction = target_hwp_direction - # Stepwise rotation after spinning up the HWP + # Stepwise rotation after spinning up the HWP if stepwise_after: rotate(False) finally: @@ -416,7 +416,7 @@ def calibrate(continuous=False, elevation_check=True, boresight_check=True, def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True, stepwise_mid=False, repeat=1): """ Run a wiregrid time constant measurement. - + Args: initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. stepwise_first (bool): Do stepwise rotation or not before the first HWP speed change. Default is True. @@ -426,7 +426,7 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True If this is odd, the HWP direction will be changed to the opposite of the initial direction. If this is even, the HWP direction will be the same as the initial direction. """ - + # Check the initial HWP direction if initial_hwp_direction not in ['forward', 'backward']: raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") @@ -439,7 +439,7 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True _check_agents_online() _check_motor_on() _check_telescope_position(elevation_check=True, boresight_check=False) - + if _check_zenith: el_tag = ', wg_el90' else: @@ -478,7 +478,7 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True if stepwise_mid and i != 0: # Stepwise rotation between changing HWP speed stepwise_before = True - if stepwise_last and i == repeat-1: + if stepwise_last and i == repeat - 1: # Stepwise rotation after the last HWP speed change stepwise_after = True From 04218dd3bde964de3735772518c033faa6e435bc Mon Sep 17 00:00:00 2001 From: d-hoshino2626 Date: Tue, 25 Mar 2025 12:03:30 +0900 Subject: [PATCH 04/43] edit time constant function and related internal functions --- src/sorunlib/wiregrid.py | 241 +++++++++++++++----------------- tests/test_wiregrid.py | 293 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 400 insertions(+), 134 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 007f73da..095ac737 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -8,8 +8,9 @@ AGENT_TIMEDIFF_THRESHOLD = 5 # sec OP_TIMEOUT = 60 - # Internal Helper Functions + + def _check_process_data(process, last_timestamp): """Check the latest timestamp from a process' session.data is recent enough. @@ -199,108 +200,80 @@ def _check_temperature_sensors(): def _check_wiregrid_position(): - """Check the wiregrid position.""" + """Check the wiregrid position. + + Returns: + str: The wiregrid position, either 'inside' or 'outside'. + + Raises: + RuntimeError: When the wiregrid position is unknown. + + """ actuator = run.CLIENTS['wiregrid']['actuator'] resp = actuator.acq.status() - ls_status = resp.session['data']['fields']['limitswitch'] - if (ls_status['LSR1'] == 1 or ls_status['LSL1'] == 1) and not (ls_status['LSR2'] == 1 and ls_status['LSL2'] == 1): - position = 'outside' - elif (ls_status['LSR2'] == 1 or ls_status['LSL2'] == 1) and not (ls_status['LSR1'] == 1 or ls_status['LSL1'] == 1): - position = 'inside' - else: - raise RuntimeError("The wiregrid position is unknown. The limitswitch response is like this:\n" + - "LSR1: {}\n".format('ON' if ls_status['LSR1'] == 1 else 'OFF') - + "LSL1: {}\n".format('ON' if ls_status['LSL1'] == 1 else 'OFF') - + "LSR2: {}\n".format('ON' if ls_status['LSR2'] == 1 else 'OFF') - + "LSL2: {}\n".format('ON' if ls_status['LSL2'] == 1 else 'OFF') - + "Aborting...") + position = resp.session['data']['fields']['position'] + if position not in ['inside', 'outside']: + raise RuntimeError("The wiregrid position is unknown. Aborting...") return position -def _stop_hwp_with_wiregrid(): - """Stop the HWP after comfirming the wiregrid is inserted.""" - # Check the wiregrid position - position = _check_wiregrid_position() - if position == 'outside': - raise RuntimeError("The wiregrid is not inserted. Aborting...") - elif position == 'inside': - # Stop the HWP - print("Starting to stop the HWP.") - run.hwp.stop(active=True) - print("The HWP stopped successfully.") - else: - raise RuntimeError("Unknown wiregrid position. Aborting...") - +def _check_hwp_direction(): + """Check the HWP direction by referring to the 'direction' in session.data + of the HWP PID Agent via the HWP Supervisor Agent. -def _spin_hwp_with_wiregrid(target_hwp_direction): - """Spin the HWP to the target direction at 2Hz after comfirming the wiregrid is inserted. + Returns: + str: The HWP direction, either 'forward' or 'backward'. - Args: - target_hwp_direction (str): Target HWP direction, 'forward' or 'backward'. """ - # Check the wiregrid position - position = _check_wiregrid_position() - if position == 'outside': - raise RuntimeError("The wiregrid is not inserted. Aborting...") - elif position == 'inside': - if target_hwp_direction == 'forward': - print("Starting to spin up the HWP in forward.") - run.hwp.set_freq(freq=2.0) - print("The HWP is spinning at 2Hz in forward successfully.") - elif target_hwp_direction == 'backward': - print("Starting to spin up the HWP in backward.") - run.hwp.set_freq(freq=-2.0) - print("The HWP is spinning at 2Hz in backward successfully.") - else: - raise RuntimeError("Unknown HWP target direction. Aborting...") + hwp = run.CLIENTS['hwp'] + resp = hwp.monitor.status() + pid_direction = resp.session['data']['hwp_status']['pid_direction'] + if pid_direction == 0: + direction = 'forward' + elif pid_direction == 1: + direction = 'backward' else: - raise RuntimeError("Unknown wiregrid position. Aborting...") + raise RuntimeError("The HWP direction is unknown. Aborting...") + return direction -def _reverse_hwp_with_wiregrid(initial_hwp_direction, streaming=False, stepwise_before=False, stepwise_after=False): - """Change the HWP direction after comfirming the wiregrid is inserted. +def _reverse_hwp_direction(initial_hwp_direction, stepwise_before=False, + stepwise_after=False): + """Reverse the HWP rotation direction from ``initial_hwp_direction``. Args: - initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. - streaming (bool): Do SMuRF streaming during the HWP direction chaging or not . Default is False. - stepwise_before (bool): Do stepwise rotation before changing HWP direction or not. Default is False. - stepwise_after (bool): Do stepwise rotation after changing HWP direction or not. Default is False. - """ - if initial_hwp_direction not in ['forward', 'backward']: - raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") + initial_hwp_direction (str): The initial HWP direction, either 'forward' + or 'backward'. + stepwise_before (bool): Do stepwise rotation before changing HWP direction + or not. Default is False. + stepwise_after (bool): Do stepwise rotation after changing HWP direction + or not. Default is False. + Returns: + str: The current HWP direction after reversing. + """ + # Set the target HWP direction current_hwp_direction = initial_hwp_direction if current_hwp_direction == 'forward': target_hwp_direction = 'backward' elif current_hwp_direction == 'backward': target_hwp_direction = 'forward' - - # Stop and spin up reversely the HWP - try: - # Enable SMuRF streams - if streaming: - stream_tag = f'wiregrid, wg_time_constant, hwp_change_to_{target_hwp_direction}' - if stepwise_before or stepwise_after: - stream_tag += ', wg_stepwise' - if _check_zenith(): - stream_tag += ', wg_el90' - run.smurf.stream('on', subtype='cal', tag=stream_tag) - # Stepwise rotation before stopping the HWP - if stepwise_before: - rotate(False) - # Stop the HWP - _stop_hwp_with_wiregrid() - # Spin up the HWP in the opposite direction - _spin_hwp_with_wiregrid(target_hwp_direction) - current_hwp_direction = target_hwp_direction - # Stepwise rotation after spinning up the HWP - if stepwise_after: - rotate(False) - finally: - # Stop SMuRF streams - if streaming: - run.smurf.stream('off') - + else: + raise RuntimeError("Invalid initial hwp rotation direction. Aborting...") + # Run stepwise rotation before stopping the HWP + if stepwise_before: + rotate(False) + # Stop the HWP + run.hwp.stop(active=True) + # Spin up the HWP reversely + if target_hwp_direction == 'forward': + run.hwp.set_freq(freq=2.0) + elif target_hwp_direction == 'backward': + run.hwp.set_freq(freq=-2.0) + current_hwp_direction = target_hwp_direction + # Run stepwise rotation after spinning up the HWP + if stepwise_after: + rotate(False) return current_hwp_direction @@ -413,50 +386,55 @@ def calibrate(continuous=False, elevation_check=True, boresight_check=True, run.smurf.stream('off') -def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True, stepwise_mid=False, repeat=1): +def time_constant(num_repeats=1): """ Run a wiregrid time constant measurement. Args: - initial_hwp_direction (str): Initial HWP direction, 'forward' or 'backward'. - stepwise_first (bool): Do stepwise rotation or not before the first HWP speed change. Default is True. - stepwise_last (bool): Do stepwise rotation or not after the last HWP speed change. Default is True. - stepwise_mid (bool): Do stepwise rotation between each HWP speed change. Default is False. - repeat (int): Number of repeats. Default is 1. - If this is odd, the HWP direction will be changed to the opposite of the initial direction. - If this is even, the HWP direction will be the same as the initial direction. - """ + num_repeats (int): Number of repeats. Default is 1. + If this is odd, the HWP direction will be changed to the opposite + of the initial direction. If this is even, the HWP direction will be + the same as the initial direction. - # Check the initial HWP direction - if initial_hwp_direction not in ['forward', 'backward']: - raise RuntimeError("Initial HWP direction should be either 'forward' or 'backward'. Aborting...") - current_hwp_direction = initial_hwp_direction - - # Check the repeat - if repeat < 1 or not isinstance(repeat, int): - raise RuntimeError("The repeat should be int and larger than 0. Aborting...") + """ + # Check the number of repeats + if num_repeats < 1 or not isinstance(num_repeats, int): + error = "The ``num_repeats`` should be int and larger than 0." + raise RuntimeError(error) _check_agents_online() _check_motor_on() _check_telescope_position(elevation_check=True, boresight_check=False) + position = _check_wiregrid_position() + print(position) + if _check_wiregrid_position() == 'inside': + error = "The wiregrid is already inserted before the wiregrid time " + \ + "constant measurement. Please inspect wiregrid and HWP " + \ + "before continuing observations." + raise RuntimeError(error) - if _check_zenith: + if _check_zenith(): el_tag = ', wg_el90' else: el_tag = '' + # Check the current HWP direction + current_hwp_direction = _check_hwp_direction() + # Rotate for reference before insertion rotate(continuous=True, duration=10) # Bias step (the wire grid is off the window) - bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag + bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) # Insert the wiregrid with streaming time.sleep(5) try: # Enable SMuRF streams - stream_tag = f'wg_time_constant, wg_inserting, hwp_2hz_{current_hwp_direction}' + el_tag + stream_tag = 'wiregrid, wg_time_constant, wg_inserting, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Insert the wiregrid insert() @@ -465,38 +443,48 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True run.smurf.stream('off') time.sleep(5) - for i in range(repeat): + for i in range(num_repeats): # Bias step (the wire grid is on the window) - bs_tag = f'biasstep, wg_time_constant, wg_inserted, hwp_2hz_{current_hwp_direction}' + el_tag + bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) - stepwise_before = False - stepwise_after = False - if stepwise_first and i == 0: - # Stepwise rotation before the first HWP speed change - stepwise_before = True - if stepwise_mid and i != 0: - # Stepwise rotation between changing HWP speed - stepwise_before = True - if stepwise_last and i == repeat - 1: - # Stepwise rotation after the last HWP speed change - stepwise_after = True - - # Spin the HWP in the opposite direction of the initial direction with streaming and stepwise rotation - current_hwp_direction = _reverse_hwp_with_wiregrid(current_hwp_direction, - streaming=True, - stepwise_before=stepwise_before, - stepwise_after=stepwise_after) + stepwise_before = True if i == 0 else False + stepwise_after = True + try: + if current_hwp_direction == 'forward': + target_hwp_direction = 'backward' + elif current_hwp_direction == 'backward': + target_hwp_direction = 'forward' + stream_tag = 'wiregrid, wg_time_constant, ' + \ + f'hwp_change_to_{target_hwp_direction}' + el_tag + # Enable SMuRF streams + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Reverse the HWP with streaming and a stepwise rotation + current_hwp_direction = \ + _reverse_hwp_direction(current_hwp_direction, + stepwise_before=stepwise_before, + stepwise_after=stepwise_after) + except RuntimeError as e: + error = "The wiregrid time constant measurement failed. " + \ + "Please inspect wiregrid and HWP before continuing " + \ + "observations.\n" + str(e) + raise RuntimeError(error) + finally: + # Stop SMuRF streams + run.smurf.stream('off') # Bias step (the wire grid is on the window) - bs_tag = f'biasstep, wg_time_constant, wg_inserted, hwp_2hz_{current_hwp_direction}' + el_tag + bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) # Eject the wiregrid with streaming time.sleep(5) try: # Enable SMuRF streams - stream_tag = f'wg_time_constant, wg_ejecting, hwp_2hz_{current_hwp_direction}' + el_tag + stream_tag = 'wiregrid, wg_time_constant, wg_ejecting, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Eject the wiregrid eject() @@ -506,5 +494,6 @@ def time_constant(initial_hwp_direction, stepwise_first=True, stepwise_last=True time.sleep(5) # Bias step (the wire grid is off the window) - bs_tag = f'biasstep, wg_time_constant, wg_ejected, hwp_2hz_{current_hwp_direction}' + el_tag + bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ + f'hwp_2hz_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 3c5500d7..f2617e7c 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -4,7 +4,7 @@ import pytest -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, patch, call import ocs from ocs.ocs_client import OCSReply @@ -35,6 +35,27 @@ def create_acu_client(az, el, boresight): return acu_client +def create_hwp_client(pid_direction): + """Create a HWP client with mock acq Process session.data. + + Args: + pid_direction (int): PID direction of the HWP. 0 is forward, 1 is backward. + + """ + client = MagicMock() + session = create_session('acq') + session.data = { + 'hwp_status': { + 'pid_direction': pid_direction, + }, + 'timestamp': time.time(), + } + reply = OCSReply(ocs.OK, 'msg', session.encoded()) + client.monitor.status = MagicMock(return_value=reply) + + return client + + def create_labjack_client(): """Create a labjack client with mock acq Process session.data.""" client = MagicMock() @@ -55,17 +76,23 @@ def create_labjack_client(): return client -def create_actuator_client(motor): +def create_actuator_client(motor, position): """Create an actuator client with mock acq Process session.data. Args: motor (int): Motor state, 0 is off, 1 is on. + position (str): Position of the wiregrid, either 'inside' or 'outside'. """ client = MagicMock() session = create_session('acq') - session.data = {'fields': {'motor': motor}, - 'timestamp': time.time()} + session.data = { + 'fields': { + 'motor': motor, + 'position': position + }, + 'timestamp': time.time(), + } session.set_status('running') reply = OCSReply(ocs.OK, 'msg', session.encoded()) client.acq.status = MagicMock(return_value=reply) @@ -201,7 +228,8 @@ def test__check_telescope_position_invalid(el, boresight): @pytest.mark.parametrize('motor', [(0), (1)]) @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) def test__check_motor_on(motor): - wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(motor=motor) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=motor, position='inside') wiregrid._check_motor_on() if motor == 1: wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() @@ -213,7 +241,8 @@ def test__check_motor_on(motor): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) def test__check_motor_on_invalid_state(): - wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(motor=3) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=3, position='inside') with pytest.raises(RuntimeError): wiregrid._check_motor_on() wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() @@ -221,7 +250,8 @@ def test__check_motor_on_invalid_state(): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) def test__check_agents_online(): - wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(motor=1) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='inside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() @@ -239,7 +269,8 @@ def test__check_agents_online(): def test_calibrate_stepwise(patch_clients, continuous, el, tag): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, el, 0) - wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(motor=1) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='inside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() @@ -262,3 +293,249 @@ def test__check_process_data_stale_data(): with pytest.raises(RuntimeError): stale_time = time.time() - wiregrid.AGENT_TIMEDIFF_THRESHOLD wiregrid._check_process_data('test process', stale_time) + + +@pytest.mark.parametrize('position', [('inside'), ('outside')]) +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__check_wiregrid_position(position): + wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(1, position) + return_position = wiregrid._check_wiregrid_position() + wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() + assert return_position == position + + +@pytest.mark.parametrize('position', [('unknown'), ('')]) +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__check_wiregrid_position_invalid(position): + wiregrid.run.CLIENTS['wiregrid']['actuator'] = create_actuator_client(1, position) + with pytest.raises(RuntimeError): + wiregrid._check_wiregrid_position() + wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() + + +@pytest.mark.parametrize('pid_direction', [(0), (1)]) +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__check_hwp_direction(pid_direction): + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(pid_direction) + hwp_direction = wiregrid._check_hwp_direction() + if pid_direction == 0: + assert hwp_direction == 'forward' + elif pid_direction == 1: + assert hwp_direction == 'backward' + wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__check_hwp_direction_invalid(): + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(pid_direction=3) + with pytest.raises(RuntimeError): + wiregrid._check_hwp_direction() + wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() + + +@pytest.mark.parametrize('initial_hwp_direction', [('forward'), ('backward')]) +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__reverse_hwp_direction_initial_direction(initial_hwp_direction): + with patch('sorunlib.wiregrid.run.hwp.set_freq') as mock_hwp_set_freq: + wiregrid._reverse_hwp_direction(initial_hwp_direction) + if initial_hwp_direction == 'forward': + mock_hwp_set_freq.assert_called_once_with(freq=-2.0) + elif initial_hwp_direction == 'backward': + mock_hwp_set_freq.assert_called_once_with(freq=2.0) + + +@pytest.mark.parametrize('initial_hwp_direction', 'unknown') +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__reverse_hwp_direction_initial_direction_failed(initial_hwp_direction): + with pytest.raises(RuntimeError): + wiregrid._reverse_hwp_direction(initial_hwp_direction, True, True) + + +@pytest.mark.parametrize('stepwise_before, stepwise_after', + [(True, True), + (True, False), + (False, True), + (False, False)]) +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +def test__reverse_hwp_direction_stepwise(stepwise_before, stepwise_after): + with patch('sorunlib.wiregrid.rotate') as mock_rotate: + wiregrid._reverse_hwp_direction('forward', + stepwise_before=stepwise_before, + stepwise_after=stepwise_after) + if stepwise_before and stepwise_after: + assert mock_rotate.call_count == 2 + elif not stepwise_before and not stepwise_after: + assert mock_rotate.call_count == 0 + else: + assert mock_rotate.call_count == 1 + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_forward(): + # Setup all mock clients + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) # forward + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='outside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + + wiregrid.time_constant(num_repeats=1) + + # just make sure bias_steps and streams because other functions are already + # tested separately. + expected_calls_of_bias_steps = [ + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_backward'), + ] + + common_kwargs_of_streams = { + "downsample_factor": None, + "filter_disable": False + } + expected_tags_of_streams = [ + 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_forward', + 'wiregrid, wg_time_constant, hwp_change_to_backward', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_backward', + ] + expected_calls_of_streams = [ + call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) + for stream_tag in expected_tags_of_streams + ] + + for client in wiregrid.run.CLIENTS['smurf']: + assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps + assert client.stream.start.call_args_list == expected_calls_of_streams + assert client.stream.stop.call_count == 3 + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_backward_el90(): + # Setup all mock clients + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 90, 0) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(1) # backward + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='outside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + + wiregrid.time_constant(num_repeats=1) + + # just make sure bias_steps and streams because other functions are already + # tested separately. + expected_calls_of_bias_steps = [ + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_backward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward, wg_el90'), + ] + + common_kwargs_of_streams = { + "downsample_factor": None, + "filter_disable": False + } + expected_tags_of_streams = [ + 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_backward, wg_el90', + 'wiregrid, wg_time_constant, hwp_change_to_forward, wg_el90', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_forward, wg_el90', + ] + expected_calls_of_streams = [ + call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) + for stream_tag in expected_tags_of_streams + ] + + for client in wiregrid.run.CLIENTS['smurf']: + assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps + assert client.stream.start.call_args_list == expected_calls_of_streams + assert client.stream.stop.call_count == 3 + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_repeats(): + # Setup all mock clients + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='outside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + wiregrid.run.wiregrid.rotate = MagicMock() + + wiregrid.time_constant(num_repeats=2) + + # just make sure bias_steps and streams because other functions are already + # tested separately. + expected_calls_of_bias_steps = [ + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), + ] + + common_kwargs_of_streams = { + "downsample_factor": None, + "filter_disable": False + } + expected_tags_of_streams = [ + 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_forward', + 'wiregrid, wg_time_constant, hwp_change_to_backward', + 'wiregrid, wg_time_constant, hwp_change_to_forward', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_forward', + ] + expected_calls_of_streams = [ + call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) + for stream_tag in expected_tags_of_streams + ] + + for client in wiregrid.run.CLIENTS['smurf']: + assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps + assert client.stream.start.call_args_list == expected_calls_of_streams + assert client.stream.stop.call_count == 4 + + assert wiregrid.run.wiregrid.rotate.call_count == 4 + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_num_repeats_failed(): + with pytest.raises(RuntimeError): + wiregrid.time_constant(num_repeats=-2) + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_wiregrid_position_failed(): + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='inside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + + with pytest.raises(RuntimeError): + wiregrid.time_constant(num_repeats=1) + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_reverse_hwp_failed(): + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='outside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + + wiregrid.run.wiregrid._reverse_hwp_direction = MagicMock(side_effect=RuntimeError) + with pytest.raises(RuntimeError): + wiregrid.time_constant(num_repeats=1) From 4116360db8b571f9fb84f87b0f6e24fae77ea841 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 16 Apr 2025 15:52:48 +0900 Subject: [PATCH 05/43] Update wiregrid.py hwp_status --> hwp_state --- src/sorunlib/wiregrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 095ac737..04337007 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -227,7 +227,7 @@ def _check_hwp_direction(): """ hwp = run.CLIENTS['hwp'] resp = hwp.monitor.status() - pid_direction = resp.session['data']['hwp_status']['pid_direction'] + pid_direction = resp.session['data']['hwp_state']['pid_direction'] if pid_direction == 0: direction = 'forward' elif pid_direction == 1: From f51a267fbdbe39b32f72e52341f83d0f3ef0a85b Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 16 Apr 2025 16:05:22 +0900 Subject: [PATCH 06/43] Update wiregrid.py remove print about the wiregrid position (insert or eject) --- src/sorunlib/wiregrid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 04337007..10875d9f 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -406,7 +406,6 @@ def time_constant(num_repeats=1): _check_motor_on() _check_telescope_position(elevation_check=True, boresight_check=False) position = _check_wiregrid_position() - print(position) if _check_wiregrid_position() == 'inside': error = "The wiregrid is already inserted before the wiregrid time " + \ "constant measurement. Please inspect wiregrid and HWP " + \ From 168c5e833e430c0f86469a3f095e1c430e1cb070 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 16 Apr 2025 16:40:08 +0900 Subject: [PATCH 07/43] Update wiregrid.py remove "2hz" from tags. --- src/sorunlib/wiregrid.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 10875d9f..b4e1ed01 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -425,7 +425,7 @@ def time_constant(num_repeats=1): # Bias step (the wire grid is off the window) bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) # Insert the wiregrid with streaming @@ -433,7 +433,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_inserting, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Insert the wiregrid insert() @@ -445,7 +445,7 @@ def time_constant(num_repeats=1): for i in range(num_repeats): # Bias step (the wire grid is on the window) bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) stepwise_before = True if i == 0 else False @@ -475,7 +475,7 @@ def time_constant(num_repeats=1): # Bias step (the wire grid is on the window) bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) # Eject the wiregrid with streaming @@ -483,7 +483,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_ejecting, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Eject the wiregrid eject() @@ -494,5 +494,5 @@ def time_constant(num_repeats=1): # Bias step (the wire grid is off the window) bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ - f'hwp_2hz_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) From 164fd38d58fa9c885e09e61f8a63ba83a647f1db Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 16 Apr 2025 16:49:07 +0900 Subject: [PATCH 08/43] Update tests/test_wiregrid.py Change test__reverse_hwp_direction_initial_direction_failed() Co-authored-by: Brian Koopman --- tests/test_wiregrid.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index f2617e7c..1ed18881 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -344,11 +344,10 @@ def test__reverse_hwp_direction_initial_direction(initial_hwp_direction): mock_hwp_set_freq.assert_called_once_with(freq=2.0) -@pytest.mark.parametrize('initial_hwp_direction', 'unknown') @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__reverse_hwp_direction_initial_direction_failed(initial_hwp_direction): +def test__reverse_hwp_direction_initial_direction_failed(): with pytest.raises(RuntimeError): - wiregrid._reverse_hwp_direction(initial_hwp_direction, True, True) + wiregrid._reverse_hwp_direction('unknown', True, True) @pytest.mark.parametrize('stepwise_before, stepwise_after', From c859f2e3aaf3b73df89f5aa0e9fd6ac4f0462801 Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Wed, 16 Apr 2025 17:30:09 -0400 Subject: [PATCH 09/43] Remove unused variable assignment --- src/sorunlib/wiregrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index b4e1ed01..1a36108e 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -405,7 +405,7 @@ def time_constant(num_repeats=1): _check_agents_online() _check_motor_on() _check_telescope_position(elevation_check=True, boresight_check=False) - position = _check_wiregrid_position() + _check_wiregrid_position() if _check_wiregrid_position() == 'inside': error = "The wiregrid is already inserted before the wiregrid time " + \ "constant measurement. Please inspect wiregrid and HWP " + \ From 52b31f817eb82cbb0c4c6b53496acc8089058c8b Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Wed, 16 Apr 2025 17:33:02 -0400 Subject: [PATCH 10/43] Fix failing tests --- tests/test_wiregrid.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 1ed18881..934b417e 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -45,7 +45,7 @@ def create_hwp_client(pid_direction): client = MagicMock() session = create_session('acq') session.data = { - 'hwp_status': { + 'hwp_state': { 'pid_direction': pid_direction, }, 'timestamp': time.time(), @@ -386,10 +386,10 @@ def test_time_constant_forward(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_backward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_backward'), ] common_kwargs_of_streams = { @@ -397,9 +397,9 @@ def test_time_constant_forward(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_forward', + 'wiregrid, wg_time_constant, wg_inserting, hwp_forward', 'wiregrid, wg_time_constant, hwp_change_to_backward', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_backward', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_backward', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -429,10 +429,10 @@ def test_time_constant_backward_el90(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_backward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_backward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward, wg_el90'), ] common_kwargs_of_streams = { @@ -440,9 +440,9 @@ def test_time_constant_backward_el90(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_backward, wg_el90', + 'wiregrid, wg_time_constant, wg_inserting, hwp_backward, wg_el90', 'wiregrid, wg_time_constant, hwp_change_to_forward, wg_el90', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_forward, wg_el90', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_forward, wg_el90', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -473,11 +473,11 @@ def test_time_constant_repeats(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_backward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_2hz_forward'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_2hz_forward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), ] common_kwargs_of_streams = { @@ -485,10 +485,10 @@ def test_time_constant_repeats(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_2hz_forward', + 'wiregrid, wg_time_constant, wg_inserting, hwp_forward', 'wiregrid, wg_time_constant, hwp_change_to_backward', 'wiregrid, wg_time_constant, hwp_change_to_forward', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_2hz_forward', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_forward', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) From 43ed0576014b4389aa2a8d9a25044844fdeeee72 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Tue, 22 Apr 2025 01:55:54 +0900 Subject: [PATCH 11/43] Update wiregrid.py to split stream before & after hwp stopping To split the stream, I didn't use _reverse_hwp_direction(). Temporarily, that function is commented out. --- src/sorunlib/wiregrid.py | 48 +++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 1a36108e..f4380bd6 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -455,15 +455,53 @@ def time_constant(num_repeats=1): target_hwp_direction = 'backward' elif current_hwp_direction == 'backward': target_hwp_direction = 'forward' + # Run stepwise rotation before stopping the HWP + if stepwise_before: + # Enable SMuRF streams + stream_tag = 'wiregrid, wg_time_constant, ' + \ + f'wg_stepwise_before' + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + rotate(False) + # Stop SMuRF streams + run.smurf.stream('off') + + # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'hwp_change_to_{target_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}_to_0' + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Stop the HWP + run.hwp.stop(active=True) + # Stop SMuRF streams + run.smurf.stream('off') + # Enable SMuRF streams + stream_tag = 'wiregrid, wg_time_constant, ' + \ + f'hwp_0_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Reverse the HWP with streaming and a stepwise rotation - current_hwp_direction = \ - _reverse_hwp_direction(current_hwp_direction, - stepwise_before=stepwise_before, - stepwise_after=stepwise_after) + # Spin up the HWP reversely + if target_hwp_direction == 'forward': + run.hwp.set_freq(freq=2.0) + elif target_hwp_direction == 'backward': + run.hwp.set_freq(freq=-2.0) + current_hwp_direction = target_hwp_direction + # Stop SMuRF streams + run.smurf.stream('off') + + # Run stepwise rotation after spinning up the HWP + if stepwise_after: + # Enable SMuRF streams + stream_tag = 'wiregrid, wg_time_constant, ' + \ + f'wg_stepwise_after' + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + rotate(False) + # Stop SMuRF streams + run.smurf.stream('off') + + #current_hwp_direction = \ + # _reverse_hwp_direction(current_hwp_direction, + # stepwise_before=stepwise_before, + # stepwise_after=stepwise_after) except RuntimeError as e: error = "The wiregrid time constant measurement failed. " + \ "Please inspect wiregrid and HWP before continuing " + \ From c720215414bc5c814a1566bd28a3bbb442271828 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Apr 2025 16:56:00 +0000 Subject: [PATCH 12/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sorunlib/wiregrid.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index f4380bd6..1adbf606 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -459,7 +459,7 @@ def time_constant(num_repeats=1): if stepwise_before: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'wg_stepwise_before' + el_tag + f'wg_stepwise_before' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') rotate(False) # Stop SMuRF streams @@ -492,13 +492,13 @@ def time_constant(num_repeats=1): if stepwise_after: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'wg_stepwise_after' + el_tag + f'wg_stepwise_after' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') rotate(False) # Stop SMuRF streams run.smurf.stream('off') - #current_hwp_direction = \ + # current_hwp_direction = \ # _reverse_hwp_direction(current_hwp_direction, # stepwise_before=stepwise_before, # stepwise_after=stepwise_after) From 70fca8c97a28c65a0728324f39cf24ec7ccdfe97 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Thu, 24 Apr 2025 22:55:39 +0900 Subject: [PATCH 13/43] update streaming (add try & finally to make it sure to stop the streaming) in wiregrid.py --- src/sorunlib/wiregrid.py | 58 +++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 1adbf606..5a857c19 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -427,9 +427,9 @@ def time_constant(num_repeats=1): bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) + time.sleep(5) # Insert the wiregrid with streaming - time.sleep(5) try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_inserting, ' + \ @@ -437,87 +437,85 @@ def time_constant(num_repeats=1): run.smurf.stream('on', tag=stream_tag, subtype='cal') # Insert the wiregrid insert() + time.sleep(5) finally: # Stop SMuRF streams run.smurf.stream('off') - time.sleep(5) for i in range(num_repeats): # Bias step (the wire grid is on the window) bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ - f'hwp_{current_hwp_direction}' + el_tag + f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) stepwise_before = True if i == 0 else False stepwise_after = True - try: - if current_hwp_direction == 'forward': - target_hwp_direction = 'backward' - elif current_hwp_direction == 'backward': - target_hwp_direction = 'forward' - # Run stepwise rotation before stopping the HWP - if stepwise_before: + if current_hwp_direction == 'forward': + target_hwp_direction = 'backward' + elif current_hwp_direction == 'backward': + target_hwp_direction = 'forward' + + # Run stepwise rotation before stopping the HWP + if stepwise_before: + try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'wg_stepwise_before' + el_tag + 'wg_stepwise_before' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Run stepwise rotation rotate(False) + finally: # Stop SMuRF streams run.smurf.stream('off') + # Stop the HWP with streaming + try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'hwp_{current_hwp_direction}_to_0' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Stop the HWP run.hwp.stop(active=True) + finally: # Stop SMuRF streams run.smurf.stream('off') + # Spin up the HWP reversely with streaming + try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'hwp_0_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Reverse the HWP with streaming and a stepwise rotation # Spin up the HWP reversely if target_hwp_direction == 'forward': run.hwp.set_freq(freq=2.0) elif target_hwp_direction == 'backward': run.hwp.set_freq(freq=-2.0) current_hwp_direction = target_hwp_direction + finally: # Stop SMuRF streams run.smurf.stream('off') - # Run stepwise rotation after spinning up the HWP - if stepwise_after: + # Run stepwise rotation after spinning up the HWP + if stepwise_after: + try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'wg_stepwise_after' + el_tag + 'wg_stepwise_after' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Run stepwise rotation rotate(False) + finally: # Stop SMuRF streams run.smurf.stream('off') - # current_hwp_direction = \ - # _reverse_hwp_direction(current_hwp_direction, - # stepwise_before=stepwise_before, - # stepwise_after=stepwise_after) - except RuntimeError as e: - error = "The wiregrid time constant measurement failed. " + \ - "Please inspect wiregrid and HWP before continuing " + \ - "observations.\n" + str(e) - raise RuntimeError(error) - finally: - # Stop SMuRF streams - run.smurf.stream('off') - # Bias step (the wire grid is on the window) bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) + time.sleep(5) # Eject the wiregrid with streaming - time.sleep(5) try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_ejecting, ' + \ @@ -525,10 +523,10 @@ def time_constant(num_repeats=1): run.smurf.stream('on', tag=stream_tag, subtype='cal') # Eject the wiregrid eject() + time.sleep(5) finally: # Stop SMuRF streams run.smurf.stream('off') - time.sleep(5) # Bias step (the wire grid is off the window) bs_tag = 'wiregrid, wg_time_constant, wg_ejected, ' + \ From 857e8c2be46f3c6f29f942b1a7fd625af01c8da8 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Thu, 24 Apr 2025 23:47:55 +0900 Subject: [PATCH 14/43] update hwp.py to add `get_direction()` function. --- src/sorunlib/hwp.py | 30 ++++++++++++++++++++++++++++++ src/sorunlib/wiregrid.py | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 2fda9cec..6c9fe824 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -43,3 +43,33 @@ def stop(active=True, brake_voltage=None): else: resp = hwp.pmx_off(wait_stop=True) check_response(hwp, resp) + + +def get_direction(): + """Return the rotational direction of the HWP. + If True, it is counter-clockwise (CCW) seen from the sky to window. + If False, it is clockwise (CW). + + Args: + None + + .. _docs: https://socs.readthedocs.io/en/main/agents/hwp_supervisor_agent.html + + """ + hwp = run.CLIENTS['hwp'] + + resp = hwp.monitor.status() + pid_direction = resp.session['data']['hwp_state']['pid_direction'] + if pid_direction == 0: + is_forward = True + elif pid_direction == 1: + is_forward = False + else: + raise RuntimeError("the hwp direction is unknown. aborting...") + + forward_is_cw = hwp.forward_is_cw # TODO: Need to check if this is possible + + if is_forward ^ forward_is_cw: + return True + else: + return False diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 5a857c19..bd416f97 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -217,6 +217,7 @@ def _check_wiregrid_position(): return position +# TODO: Should be changed to check CCW or CW def _check_hwp_direction(): """Check the HWP direction by referring to the 'direction' in session.data of the HWP PID Agent via the HWP Supervisor Agent. @@ -233,7 +234,7 @@ def _check_hwp_direction(): elif pid_direction == 1: direction = 'backward' else: - raise RuntimeError("The HWP direction is unknown. Aborting...") + raise RuntimeError("the hwp direction is unknown. aborting...") return direction From c53b903a106b066bbb12d70a19061d28ba2637e2 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Thu, 24 Apr 2025 23:49:19 +0900 Subject: [PATCH 15/43] update hwp.py to add `get_direction()` function. --- src/sorunlib/hwp.py | 2 +- src/sorunlib/wiregrid.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 6c9fe824..e6f7b4df 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -65,7 +65,7 @@ def get_direction(): elif pid_direction == 1: is_forward = False else: - raise RuntimeError("the hwp direction is unknown. aborting...") + raise RuntimeError("The HWP direction is unknown. Aborting...") forward_is_cw = hwp.forward_is_cw # TODO: Need to check if this is possible diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index bd416f97..7726e598 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -234,7 +234,7 @@ def _check_hwp_direction(): elif pid_direction == 1: direction = 'backward' else: - raise RuntimeError("the hwp direction is unknown. aborting...") + raise RuntimeError("The HWP direction is unknown. Aborting...") return direction From d483868ce50abac9d40e8dd32ef23f5708c68464 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Thu, 24 Apr 2025 23:52:26 +0900 Subject: [PATCH 16/43] remove `_reverse_hwp_direction()` function in wiregrid.py --- src/sorunlib/wiregrid.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 7726e598..24849bda 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -238,46 +238,6 @@ def _check_hwp_direction(): return direction -def _reverse_hwp_direction(initial_hwp_direction, stepwise_before=False, - stepwise_after=False): - """Reverse the HWP rotation direction from ``initial_hwp_direction``. - - Args: - initial_hwp_direction (str): The initial HWP direction, either 'forward' - or 'backward'. - stepwise_before (bool): Do stepwise rotation before changing HWP direction - or not. Default is False. - stepwise_after (bool): Do stepwise rotation after changing HWP direction - or not. Default is False. - - Returns: - str: The current HWP direction after reversing. - """ - # Set the target HWP direction - current_hwp_direction = initial_hwp_direction - if current_hwp_direction == 'forward': - target_hwp_direction = 'backward' - elif current_hwp_direction == 'backward': - target_hwp_direction = 'forward' - else: - raise RuntimeError("Invalid initial hwp rotation direction. Aborting...") - # Run stepwise rotation before stopping the HWP - if stepwise_before: - rotate(False) - # Stop the HWP - run.hwp.stop(active=True) - # Spin up the HWP reversely - if target_hwp_direction == 'forward': - run.hwp.set_freq(freq=2.0) - elif target_hwp_direction == 'backward': - run.hwp.set_freq(freq=-2.0) - current_hwp_direction = target_hwp_direction - # Run stepwise rotation after spinning up the HWP - if stepwise_after: - rotate(False) - return current_hwp_direction - - # Public API def insert(): """Insert the wiregrid.""" From 3809f239d378a4efe68e09552e6d151ec9a4e703 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Thu, 24 Apr 2025 23:57:28 +0900 Subject: [PATCH 17/43] remove tests related to ``_reverse_hwp_direction()`` in ``test_wiregrid.py`` --- tests/test_wiregrid.py | 52 ------------------------------------------ 1 file changed, 52 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 934b417e..55c291cb 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -333,42 +333,6 @@ def test__check_hwp_direction_invalid(): wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() -@pytest.mark.parametrize('initial_hwp_direction', [('forward'), ('backward')]) -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__reverse_hwp_direction_initial_direction(initial_hwp_direction): - with patch('sorunlib.wiregrid.run.hwp.set_freq') as mock_hwp_set_freq: - wiregrid._reverse_hwp_direction(initial_hwp_direction) - if initial_hwp_direction == 'forward': - mock_hwp_set_freq.assert_called_once_with(freq=-2.0) - elif initial_hwp_direction == 'backward': - mock_hwp_set_freq.assert_called_once_with(freq=2.0) - - -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__reverse_hwp_direction_initial_direction_failed(): - with pytest.raises(RuntimeError): - wiregrid._reverse_hwp_direction('unknown', True, True) - - -@pytest.mark.parametrize('stepwise_before, stepwise_after', - [(True, True), - (True, False), - (False, True), - (False, False)]) -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__reverse_hwp_direction_stepwise(stepwise_before, stepwise_after): - with patch('sorunlib.wiregrid.rotate') as mock_rotate: - wiregrid._reverse_hwp_direction('forward', - stepwise_before=stepwise_before, - stepwise_after=stepwise_after) - if stepwise_before and stepwise_after: - assert mock_rotate.call_count == 2 - elif not stepwise_before and not stepwise_after: - assert mock_rotate.call_count == 0 - else: - assert mock_rotate.call_count == 1 - - @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_forward(): @@ -522,19 +486,3 @@ def test_time_constant_wiregrid_position_failed(): with pytest.raises(RuntimeError): wiregrid.time_constant(num_repeats=1) - - -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -@patch('sorunlib.wiregrid.time.sleep', MagicMock()) -def test_time_constant_reverse_hwp_failed(): - wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) - wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ - create_actuator_client(motor=1, position='outside') - wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() - wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() - wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() - - wiregrid.run.wiregrid._reverse_hwp_direction = MagicMock(side_effect=RuntimeError) - with pytest.raises(RuntimeError): - wiregrid.time_constant(num_repeats=1) From e7d635bbb06df98b9a7f45eefabd7ded1db08e73 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Fri, 25 Apr 2025 10:18:02 +0900 Subject: [PATCH 18/43] add `get_direction()` in hwp.py and its test function --- src/sorunlib/hwp.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index e6f7b4df..2f8c378a 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -46,9 +46,9 @@ def stop(active=True, brake_voltage=None): def get_direction(): - """Return the rotational direction of the HWP. - If True, it is counter-clockwise (CCW) seen from the sky to window. - If False, it is clockwise (CW). + """Return the rotational direction ('cw' or 'ccw') of the HWP. + 'cw' means clockwise rotation seen from the sky to window. + 'ccw' means counter-clockwise rotation seen from the sky to window. Args: None @@ -57,19 +57,10 @@ def get_direction(): """ hwp = run.CLIENTS['hwp'] - resp = hwp.monitor.status() - pid_direction = resp.session['data']['hwp_state']['pid_direction'] - if pid_direction == 0: - is_forward = True - elif pid_direction == 1: - is_forward = False - else: - raise RuntimeError("The HWP direction is unknown. Aborting...") + direction = resp.session['data']['hwp_state']['direction'] - forward_is_cw = hwp.forward_is_cw # TODO: Need to check if this is possible + if direction not in ['cw', 'ccw']: + raise RuntimeError("The HWP direction is unknown. Aborting...") - if is_forward ^ forward_is_cw: - return True - else: - return False + return direction From 64b4904a79c22b3667a77e36000ba51b991ef04e Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Fri, 25 Apr 2025 10:21:13 +0900 Subject: [PATCH 19/43] add `get_direction()` in hwp.py and its test function --- tests/test_hwp.py | 48 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/tests/test_hwp.py b/tests/test_hwp.py index 4e5ccd24..d8453caf 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -2,15 +2,39 @@ os.environ["OCS_CONFIG_DIR"] = "./test_util/" import pytest +from unittest.mock import MagicMock +import time +import ocs +from ocs.ocs_client import OCSReply from sorunlib import hwp -from util import create_patch_clients - +from util import create_patch_clients, create_session patch_clients_satp = create_patch_clients('satp') +def create_hwp_client(direction): + """Create a HWP client with mock acq Process session.data. + + Args: + direction (str): direction of the HWP. 'cw' (clockwise) or 'ccw' (counter-clockwise). + + """ + client = MagicMock() + session = create_session('acq') + session.data = { + 'hwp_state': { + 'direction': direction, + }, + 'timestamp': time.time(), + } + reply = OCSReply(ocs.OK, 'msg', session.encoded()) + client.monitor.status = MagicMock(return_value=reply) + + return client + + @pytest.mark.parametrize("active", [True, False]) def test_stop(patch_clients_satp, active): hwp.stop(active=active) @@ -29,3 +53,23 @@ def test_stop_brake_voltage(patch_clients_satp): def test_set_freq(patch_clients_satp): hwp.set_freq(freq=2.0) hwp.run.CLIENTS['hwp'].pid_to_freq.assert_called_with(target_freq=2.0) + + +@pytest.mark.parametrize('direction', ['cw', 'ccw']) +def test_get_direction(direction): + hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) + ret = hwp.get_direction() + if direction == 'cw': + assert ret == 'cw' + elif direction == 'ccw': + assert ret == 'ccw' + hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() + + +@pytest.mark.parametrize('direction', [None, '']) +def test_get_direction_invalid(direction): + hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) + with pytest.raises(RuntimeError) as e: + hwp.get_direction() + assert str(e.value) == "The HWP direction is unknown. Aborting..." + hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() From 1cf3292a602712d4e1ecf44ea08a9889e6ed4860 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Fri, 25 Apr 2025 21:02:40 +0900 Subject: [PATCH 20/43] wiregrid.py: use hwp.get_direction() and change hwp direction label from `forward`/`backward` to `ccw`/`cw`. --- src/sorunlib/hwp.py | 6 +++--- src/sorunlib/wiregrid.py | 40 ++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 2f8c378a..7a421ced 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -46,9 +46,9 @@ def stop(active=True, brake_voltage=None): def get_direction(): - """Return the rotational direction ('cw' or 'ccw') of the HWP. - 'cw' means clockwise rotation seen from the sky to window. + """Return the rotational direction ('ccw' or 'cw') of the HWP. 'ccw' means counter-clockwise rotation seen from the sky to window. + 'cw' means clockwise rotation seen from the sky to window. Args: None @@ -60,7 +60,7 @@ def get_direction(): resp = hwp.monitor.status() direction = resp.session['data']['hwp_state']['direction'] - if direction not in ['cw', 'ccw']: + if direction not in ['ccw', 'cw']: raise RuntimeError("The HWP direction is unknown. Aborting...") return direction diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 24849bda..a34313d0 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -217,24 +217,19 @@ def _check_wiregrid_position(): return position -# TODO: Should be changed to check CCW or CW def _check_hwp_direction(): - """Check the HWP direction by referring to the 'direction' in session.data - of the HWP PID Agent via the HWP Supervisor Agent. + """Check the HWP direction. Returns: - str: The HWP direction, either 'forward' or 'backward'. + str: The HWP direction, either 'ccw' or 'cw'. """ - hwp = run.CLIENTS['hwp'] - resp = hwp.monitor.status() - pid_direction = resp.session['data']['hwp_state']['pid_direction'] - if pid_direction == 0: - direction = 'forward' - elif pid_direction == 1: - direction = 'backward' - else: - raise RuntimeError("The HWP direction is unknown. Aborting...") + try: + direction = run.hwp.get_direction() + except RuntimeError as e: + error = "Wiregrid _check_hwp_direction() failed. \n" +\ + + str(e) + raise RuntimeError(error) return direction @@ -379,7 +374,12 @@ def time_constant(num_repeats=1): el_tag = '' # Check the current HWP direction - current_hwp_direction = _check_hwp_direction() + try: + current_hwp_direction = _check_hwp_direction() + except RuntimeError as e: + error = "Wiregrid time constant measurment was failed " + \ + "due to the failure in getting hwp direction. " + str(e) + raise RuntimeError(error) # Rotate for reference before insertion rotate(continuous=True, duration=10) @@ -411,10 +411,10 @@ def time_constant(num_repeats=1): stepwise_before = True if i == 0 else False stepwise_after = True - if current_hwp_direction == 'forward': - target_hwp_direction = 'backward' - elif current_hwp_direction == 'backward': - target_hwp_direction = 'forward' + if current_hwp_direction == 'cw': + target_hwp_direction = 'ccw' + elif current_hwp_direction == 'ccw': + target_hwp_direction = 'cw' # Run stepwise rotation before stopping the HWP if stepwise_before: @@ -448,9 +448,9 @@ def time_constant(num_repeats=1): f'hwp_0_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Spin up the HWP reversely - if target_hwp_direction == 'forward': + if target_hwp_direction == 'ccw': run.hwp.set_freq(freq=2.0) - elif target_hwp_direction == 'backward': + elif target_hwp_direction == 'cw': run.hwp.set_freq(freq=-2.0) current_hwp_direction = target_hwp_direction finally: From b266e633b557d1bbcef6a05b099198c71f52ae2f Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Fri, 25 Apr 2025 21:41:59 +0900 Subject: [PATCH 21/43] update test_wiregrid.py --- tests/test_wiregrid.py | 101 +++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 55c291cb..d8b358b0 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -35,18 +35,18 @@ def create_acu_client(az, el, boresight): return acu_client -def create_hwp_client(pid_direction): +def create_hwp_client(direction): """Create a HWP client with mock acq Process session.data. Args: - pid_direction (int): PID direction of the HWP. 0 is forward, 1 is backward. + direction (str): direction of the HWP. 'cw' (clockwise) or 'ccw' (counter-clockwise). """ client = MagicMock() session = create_session('acq') session.data = { 'hwp_state': { - 'pid_direction': pid_direction, + 'direction': direction, }, 'timestamp': time.time(), } @@ -313,21 +313,21 @@ def test__check_wiregrid_position_invalid(position): wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() -@pytest.mark.parametrize('pid_direction', [(0), (1)]) +@pytest.mark.parametrize('direction', [('cw'), ('ccw')]) @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__check_hwp_direction(pid_direction): - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(pid_direction) +def test__check_hwp_direction(direction): + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(direction) hwp_direction = wiregrid._check_hwp_direction() - if pid_direction == 0: - assert hwp_direction == 'forward' - elif pid_direction == 1: - assert hwp_direction == 'backward' + if direction == 'cw': + assert hwp_direction == 'cw' + elif direction == 'ccw': + assert hwp_direction == 'ccw' wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) def test__check_hwp_direction_invalid(): - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(pid_direction=3) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client(direction=None) with pytest.raises(RuntimeError): wiregrid._check_hwp_direction() wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() @@ -335,10 +335,10 @@ def test__check_hwp_direction_invalid(): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) -def test_time_constant_forward(): +def test_time_constant_cw(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) # forward + wiregrid.run.CLIENTS['hwp'] = create_hwp_client('cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -350,10 +350,10 @@ def test_time_constant_forward(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_backward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_ccw'), ] common_kwargs_of_streams = { @@ -361,9 +361,12 @@ def test_time_constant_forward(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_forward', - 'wiregrid, wg_time_constant, hwp_change_to_backward', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_backward', + 'wiregrid, wg_time_constant, wg_inserting, hwp_cw', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', + 'wiregrid, wg_time_constant, hwp_change_cw_to_0', + 'wiregrid, wg_time_constant, hwp_change_0_to_ccw', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -373,15 +376,17 @@ def test_time_constant_forward(): for client in wiregrid.run.CLIENTS['smurf']: assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps assert client.stream.start.call_args_list == expected_calls_of_streams - assert client.stream.stop.call_count == 3 + assert client.stream.stop.call_count == 6 + + assert wiregrid.run.wiregrid.rotate.call_count == 2 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) -def test_time_constant_backward_el90(): +def test_time_constant_ccw_el90(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 90, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(1) # backward + wiregrid.run.CLIENTS['hwp'] = create_hwp_client('ccw') # ccw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -393,10 +398,10 @@ def test_time_constant_backward_el90(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_backward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_ccw, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw, wg_el90'), ] common_kwargs_of_streams = { @@ -404,9 +409,12 @@ def test_time_constant_backward_el90(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_backward, wg_el90', - 'wiregrid, wg_time_constant, hwp_change_to_forward, wg_el90', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_forward, wg_el90', + 'wiregrid, wg_time_constant, wg_inserting, hwp_ccw, wg_el90', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw, wg_el90', + 'wiregrid, wg_time_constant, hwp_change_ccw_to_0, wg_el90', + 'wiregrid, wg_time_constant, hwp_change_0_to_cw, wg_el90', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw, wg_el90', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw, wg_el90', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -416,7 +424,9 @@ def test_time_constant_backward_el90(): for client in wiregrid.run.CLIENTS['smurf']: assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps assert client.stream.start.call_args_list == expected_calls_of_streams - assert client.stream.stop.call_count == 3 + assert client.stream.stop.call_count == 6 + + assert wiregrid.run.wiregrid.rotate.call_count == 2 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @@ -424,7 +434,7 @@ def test_time_constant_backward_el90(): def test_time_constant_repeats(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(0) + wiregrid.run.CLIENTS['hwp'] = create_hwp_client('cw') wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -437,11 +447,11 @@ def test_time_constant_repeats(): # just make sure bias_steps and streams because other functions are already # tested separately. expected_calls_of_bias_steps = [ - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_backward'), - call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_forward'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_forward'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw'), + call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw'), ] common_kwargs_of_streams = { @@ -449,10 +459,15 @@ def test_time_constant_repeats(): "filter_disable": False } expected_tags_of_streams = [ - 'wiregrid, wg_time_constant, wg_inserting, hwp_forward', - 'wiregrid, wg_time_constant, hwp_change_to_backward', - 'wiregrid, wg_time_constant, hwp_change_to_forward', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_forward', + 'wiregrid, wg_time_constant, wg_inserting, hwp_cw', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', + 'wiregrid, wg_time_constant, hwp_change_cw_to_0', + 'wiregrid, wg_time_constant, hwp_change_0_to_ccw', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw', + 'wiregrid, wg_time_constant, hwp_change_ccw_to_0', + 'wiregrid, wg_time_constant, hwp_change_0_to_cw', + 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw', ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -462,9 +477,9 @@ def test_time_constant_repeats(): for client in wiregrid.run.CLIENTS['smurf']: assert client.take_bias_steps.start.call_args_list == expected_calls_of_bias_steps assert client.stream.start.call_args_list == expected_calls_of_streams - assert client.stream.stop.call_count == 4 + assert client.stream.stop.call_count == 9 - assert wiregrid.run.wiregrid.rotate.call_count == 4 + assert wiregrid.run.wiregrid.rotate.call_count == 3 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) From 5d7c2adbda545cc8c21ecf9485b3e36c80bf80ae Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Fri, 25 Apr 2025 21:42:48 +0900 Subject: [PATCH 22/43] update --- src/sorunlib/hwp.py | 6 +++--- src/sorunlib/wiregrid.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 7a421ced..2f8c378a 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -46,9 +46,9 @@ def stop(active=True, brake_voltage=None): def get_direction(): - """Return the rotational direction ('ccw' or 'cw') of the HWP. - 'ccw' means counter-clockwise rotation seen from the sky to window. + """Return the rotational direction ('cw' or 'ccw') of the HWP. 'cw' means clockwise rotation seen from the sky to window. + 'ccw' means counter-clockwise rotation seen from the sky to window. Args: None @@ -60,7 +60,7 @@ def get_direction(): resp = hwp.monitor.status() direction = resp.session['data']['hwp_state']['direction'] - if direction not in ['ccw', 'cw']: + if direction not in ['cw', 'ccw']: raise RuntimeError("The HWP direction is unknown. Aborting...") return direction diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index a34313d0..a822d995 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -221,7 +221,7 @@ def _check_hwp_direction(): """Check the HWP direction. Returns: - str: The HWP direction, either 'ccw' or 'cw'. + str: The HWP direction, either 'cw' or 'ccw'. """ try: @@ -421,7 +421,8 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise_before' + el_tag + 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation rotate(False) @@ -433,7 +434,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'hwp_{current_hwp_direction}_to_0' + el_tag + f'hwp_change_{current_hwp_direction}_to_0' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Stop the HWP run.hwp.stop(active=True) @@ -445,12 +446,12 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'hwp_0_to_{target_hwp_direction}' + el_tag + f'hwp_change_0_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Spin up the HWP reversely - if target_hwp_direction == 'ccw': + if target_hwp_direction == 'cw': run.hwp.set_freq(freq=2.0) - elif target_hwp_direction == 'cw': + elif target_hwp_direction == 'ccw': run.hwp.set_freq(freq=-2.0) current_hwp_direction = target_hwp_direction finally: @@ -462,7 +463,8 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise_after' + el_tag + 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation rotate(False) From b320d6c7778119c76959ca798072b081d6e79f10 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 11:19:42 +0900 Subject: [PATCH 23/43] update test_wiregrid.py --- tests/test_wiregrid.py | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index d8b358b0..09b29b0c 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -313,26 +313,6 @@ def test__check_wiregrid_position_invalid(position): wiregrid.run.CLIENTS['wiregrid']['actuator'].acq.status.assert_called_once() -@pytest.mark.parametrize('direction', [('cw'), ('ccw')]) -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__check_hwp_direction(direction): - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(direction) - hwp_direction = wiregrid._check_hwp_direction() - if direction == 'cw': - assert hwp_direction == 'cw' - elif direction == 'ccw': - assert hwp_direction == 'ccw' - wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() - - -@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -def test__check_hwp_direction_invalid(): - wiregrid.run.CLIENTS['hwp'] = create_hwp_client(direction=None) - with pytest.raises(RuntimeError): - wiregrid._check_hwp_direction() - wiregrid.run.CLIENTS['hwp'].monitor.status.assert_called_once() - - @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_cw(): @@ -363,8 +343,8 @@ def test_time_constant_cw(): expected_tags_of_streams = [ 'wiregrid, wg_time_constant, wg_inserting, hwp_cw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', - 'wiregrid, wg_time_constant, hwp_change_cw_to_0', - 'wiregrid, wg_time_constant, hwp_change_0_to_ccw', + 'wiregrid, wg_time_constant, hwp_change_cw_to_stop', + 'wiregrid, wg_time_constant, hwp_change_stop_to_ccw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw', 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw', ] @@ -411,8 +391,8 @@ def test_time_constant_ccw_el90(): expected_tags_of_streams = [ 'wiregrid, wg_time_constant, wg_inserting, hwp_ccw, wg_el90', 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw, wg_el90', - 'wiregrid, wg_time_constant, hwp_change_ccw_to_0, wg_el90', - 'wiregrid, wg_time_constant, hwp_change_0_to_cw, wg_el90', + 'wiregrid, wg_time_constant, hwp_change_ccw_to_stop, wg_el90', + 'wiregrid, wg_time_constant, hwp_change_stop_to_cw, wg_el90', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw, wg_el90', 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw, wg_el90', ] @@ -426,7 +406,7 @@ def test_time_constant_ccw_el90(): assert client.stream.start.call_args_list == expected_calls_of_streams assert client.stream.stop.call_count == 6 - assert wiregrid.run.wiregrid.rotate.call_count == 2 + assert wiregrid.run.wiregrid.rotate.call_count == 3 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) @@ -461,11 +441,11 @@ def test_time_constant_repeats(): expected_tags_of_streams = [ 'wiregrid, wg_time_constant, wg_inserting, hwp_cw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', - 'wiregrid, wg_time_constant, hwp_change_cw_to_0', - 'wiregrid, wg_time_constant, hwp_change_0_to_ccw', + 'wiregrid, wg_time_constant, hwp_change_cw_to_stop', + 'wiregrid, wg_time_constant, hwp_change_stop_to_ccw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw', - 'wiregrid, wg_time_constant, hwp_change_ccw_to_0', - 'wiregrid, wg_time_constant, hwp_change_0_to_cw', + 'wiregrid, wg_time_constant, hwp_change_ccw_to_stop', + 'wiregrid, wg_time_constant, hwp_change_stop_to_cw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw', ] @@ -479,7 +459,7 @@ def test_time_constant_repeats(): assert client.stream.start.call_args_list == expected_calls_of_streams assert client.stream.stop.call_count == 9 - assert wiregrid.run.wiregrid.rotate.call_count == 3 + assert wiregrid.run.wiregrid.rotate.call_count == 4 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) From d3c55bd10e30b7b5240ce810c1ead89c0e28d125 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 11:20:08 +0900 Subject: [PATCH 24/43] update hwp.py, wiregrid.py --- src/sorunlib/hwp.py | 4 +- src/sorunlib/wiregrid.py | 91 ++++++++++++++++------------------------ 2 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 2f8c378a..53e545cf 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -47,8 +47,8 @@ def stop(active=True, brake_voltage=None): def get_direction(): """Return the rotational direction ('cw' or 'ccw') of the HWP. - 'cw' means clockwise rotation seen from the sky to window. - 'ccw' means counter-clockwise rotation seen from the sky to window. + 'cw' means clockwise rotation seen from the sky to window. (+Hz) + 'ccw' means counter-clockwise rotation seen from the sky to window. (-Hz) Args: None diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index a822d995..6deab6ce 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -217,22 +217,6 @@ def _check_wiregrid_position(): return position -def _check_hwp_direction(): - """Check the HWP direction. - - Returns: - str: The HWP direction, either 'cw' or 'ccw'. - - """ - try: - direction = run.hwp.get_direction() - except RuntimeError as e: - error = "Wiregrid _check_hwp_direction() failed. \n" +\ - + str(e) - raise RuntimeError(error) - return direction - - # Public API def insert(): """Insert the wiregrid.""" @@ -375,13 +359,14 @@ def time_constant(num_repeats=1): # Check the current HWP direction try: - current_hwp_direction = _check_hwp_direction() + # return 'cw' or 'ccw' + current_hwp_direction = run.hwp.get_direction() except RuntimeError as e: error = "Wiregrid time constant measurment was failed " + \ "due to the failure in getting hwp direction. " + str(e) raise RuntimeError(error) - # Rotate for reference before insertion + # Rotate to get encoder reference before insertion rotate(continuous=True, duration=10) # Bias step (the wire grid is off the window) @@ -404,37 +389,35 @@ def time_constant(num_repeats=1): run.smurf.stream('off') for i in range(num_repeats): - # Bias step (the wire grid is on the window) - bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ - f'hwp_{current_hwp_direction}' + el_tag - run.smurf.bias_step(tag=bs_tag, concurrent=True) - - stepwise_before = True if i == 0 else False - stepwise_after = True if current_hwp_direction == 'cw': target_hwp_direction = 'ccw' elif current_hwp_direction == 'ccw': target_hwp_direction = 'cw' + # Bias step (the wire grid is on the window) + # before stopping the HWP + bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ + f'hwp_{current_hwp_direction}' + el_tag + run.smurf.bias_step(tag=bs_tag, concurrent=True) + # Run stepwise rotation before stopping the HWP - if stepwise_before: - try: - # Enable SMuRF streams - stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_directoin}' + \ - el_tag - run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Run stepwise rotation - rotate(False) - finally: - # Stop SMuRF streams - run.smurf.stream('off') + try: + # Enable SMuRF streams + stream_tag = 'wiregrid, wg_time_constant, ' + \ + 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Run stepwise rotation + rotate(False) + finally: + # Stop SMuRF streams + run.smurf.stream('off') # Stop the HWP with streaming try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'hwp_change_{current_hwp_direction}_to_0' + el_tag + f'hwp_change_{current_hwp_direction}_to_stop' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Stop the HWP run.hwp.stop(active=True) @@ -446,33 +429,33 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - f'hwp_change_0_to_{target_hwp_direction}' + el_tag + f'hwp_change_stop_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Spin up the HWP reversely if target_hwp_direction == 'cw': - run.hwp.set_freq(freq=2.0) - elif target_hwp_direction == 'ccw': run.hwp.set_freq(freq=-2.0) + elif target_hwp_direction == 'ccw': + run.hwp.set_freq(freq=2.0) current_hwp_direction = target_hwp_direction finally: # Stop SMuRF streams run.smurf.stream('off') - # Run stepwise rotation after spinning up the HWP - if stepwise_after: - try: - # Enable SMuRF streams - stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_directoin}' + \ - el_tag - run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Run stepwise rotation - rotate(False) - finally: - # Stop SMuRF streams - run.smurf.stream('off') + # Run stepwise rotation after changing the HWP rotation + try: + # Enable SMuRF streams + stream_tag = 'wiregrid, wg_time_constant, ' + \ + 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + el_tag + run.smurf.stream('on', tag=stream_tag, subtype='cal') + # Run stepwise rotation + rotate(False) + finally: + # Stop SMuRF streams + run.smurf.stream('off') # Bias step (the wire grid is on the window) + # after changing the HWP rotation bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) From 183e5d824a0fef94dce4bb37dedf6669abae2b57 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:01:58 +0900 Subject: [PATCH 25/43] update test_hwp/wiregrid.py --- tests/test_hwp.py | 4 ++-- tests/test_wiregrid.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_hwp.py b/tests/test_hwp.py index d8453caf..6726abbe 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -56,7 +56,7 @@ def test_set_freq(patch_clients_satp): @pytest.mark.parametrize('direction', ['cw', 'ccw']) -def test_get_direction(direction): +def test_get_direction(patch_clients_satp, direction): hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) ret = hwp.get_direction() if direction == 'cw': @@ -67,7 +67,7 @@ def test_get_direction(direction): @pytest.mark.parametrize('direction', [None, '']) -def test_get_direction_invalid(direction): +def test_get_direction_invalid(patch_clients_satp, direction): hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) with pytest.raises(RuntimeError) as e: hwp.get_direction() diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 09b29b0c..4db919ba 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -349,7 +349,7 @@ def test_time_constant_cw(): 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw', ] expected_calls_of_streams = [ - call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) + call(subtype='cal', tag=stream_tag, kwargs=common_kwargs_of_streams) for stream_tag in expected_tags_of_streams ] From 036d41cfe640abd02f1c0ffdf6edb9d63dbd4a6e Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:05:59 +0900 Subject: [PATCH 26/43] fix bug in wiregrid.py --- src/sorunlib/wiregrid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 6deab6ce..92cf3883 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -404,7 +404,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + 'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation @@ -445,7 +445,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_directoin}' + \ + 'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation From 9ebcc0be29363aa3735a8b9cfce60b86d93a475f Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:25:06 +0900 Subject: [PATCH 27/43] update test_wiregrid.py --- tests/test_wiregrid.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 4db919ba..90753812 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -8,7 +8,7 @@ import ocs from ocs.ocs_client import OCSReply -from sorunlib import wiregrid +from sorunlib import wiregrid, hwp from util import create_session, create_patch_clients, mocked_clients @@ -314,11 +314,12 @@ def test__check_wiregrid_position_invalid(position): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.hwp.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_cw(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client('cw') # cw + hwp.run.CLIENTS['hwp'] = create_hwp_client('cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -349,7 +350,7 @@ def test_time_constant_cw(): 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw', ] expected_calls_of_streams = [ - call(subtype='cal', tag=stream_tag, kwargs=common_kwargs_of_streams) + call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) for stream_tag in expected_tags_of_streams ] @@ -410,6 +411,7 @@ def test_time_constant_ccw_el90(): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.hwp.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_repeats(): # Setup all mock clients From ef39b3958ecfa317dd91b8d1bec9bab89b772e30 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:39:58 +0900 Subject: [PATCH 28/43] update test_wiregrid.py --- tests/test_wiregrid.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 90753812..070f327f 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -8,7 +8,7 @@ import ocs from ocs.ocs_client import OCSReply -from sorunlib import wiregrid, hwp +from sorunlib import wiregrid from util import create_session, create_patch_clients, mocked_clients @@ -314,12 +314,11 @@ def test__check_wiregrid_position_invalid(position): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -@patch('sorunlib.hwp.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_cw(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - hwp.run.CLIENTS['hwp'] = create_hwp_client('cw') # cw + wiregrid.run.hwp.get_direction = MagicMock(return_value='cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -367,7 +366,7 @@ def test_time_constant_cw(): def test_time_constant_ccw_el90(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 90, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client('ccw') # ccw + wiregrid.run.hwp.get_direction = MagicMock(return_value='ccw') # ccw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -416,7 +415,7 @@ def test_time_constant_ccw_el90(): def test_time_constant_repeats(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.CLIENTS['hwp'] = create_hwp_client('cw') + wiregrid.run.hwp.get_direction = MagicMock(return_value='cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() From 8cdbbd226a115518bad6c36dc416fd4392a87f99 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:43:04 +0900 Subject: [PATCH 29/43] update test_wiregrid.py --- tests/test_wiregrid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 070f327f..309e10ad 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -410,7 +410,6 @@ def test_time_constant_ccw_el90(): @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) -@patch('sorunlib.hwp.run.CLIENTS', mocked_clients()) @patch('sorunlib.wiregrid.time.sleep', MagicMock()) def test_time_constant_repeats(): # Setup all mock clients From 4d4cdc8269ae969bb4fb25cb9383f7b3d99d28e7 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:47:43 +0900 Subject: [PATCH 30/43] update wiregrid.py --- src/sorunlib/wiregrid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index 92cf3883..c6d0db07 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -404,7 +404,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_direction}' + \ + f'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation @@ -445,7 +445,7 @@ def time_constant(num_repeats=1): try: # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ - 'wg_stepwise, hwp_{current_hwp_direction}' + \ + f'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation From 9db23973ba5c7a223a5805e7012336f9708afb12 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:48:31 +0900 Subject: [PATCH 31/43] update test_wiregrid.py --- tests/test_wiregrid.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 309e10ad..5124bdfb 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -333,7 +333,7 @@ def test_time_constant_cw(): call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_ccw'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_ccw') ] common_kwargs_of_streams = { @@ -346,7 +346,7 @@ def test_time_constant_cw(): 'wiregrid, wg_time_constant, hwp_change_cw_to_stop', 'wiregrid, wg_time_constant, hwp_change_stop_to_ccw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_ccw', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_ccw' ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -381,7 +381,7 @@ def test_time_constant_ccw_el90(): call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_ccw, wg_el90'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw, wg_el90'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw, wg_el90'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw, wg_el90'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw, wg_el90') ] common_kwargs_of_streams = { @@ -394,7 +394,7 @@ def test_time_constant_ccw_el90(): 'wiregrid, wg_time_constant, hwp_change_ccw_to_stop, wg_el90', 'wiregrid, wg_time_constant, hwp_change_stop_to_cw, wg_el90', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw, wg_el90', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw, wg_el90', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw, wg_el90' ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) @@ -431,7 +431,7 @@ def test_time_constant_repeats(): call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_ccw'), call(tag='wiregrid, wg_time_constant, wg_inserted, hwp_cw'), - call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw'), + call(tag='wiregrid, wg_time_constant, wg_ejected, hwp_cw') ] common_kwargs_of_streams = { @@ -447,7 +447,7 @@ def test_time_constant_repeats(): 'wiregrid, wg_time_constant, hwp_change_ccw_to_stop', 'wiregrid, wg_time_constant, hwp_change_stop_to_cw', 'wiregrid, wg_time_constant, wg_stepwise, hwp_cw', - 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw', + 'wiregrid, wg_time_constant, wg_ejecting, hwp_cw' ] expected_calls_of_streams = [ call(tag=stream_tag, subtype='cal', kwargs=common_kwargs_of_streams) From 4820d4a3061d04c2c4ed4b3d9a3cbb58b660b70e Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:55:27 +0900 Subject: [PATCH 32/43] update test_wiregrid.py --- tests/test_wiregrid.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 5124bdfb..8da71cfc 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -324,6 +324,7 @@ def test_time_constant_cw(): wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + wiregrid.run.wiregrid.rotate = MagicMock() wiregrid.time_constant(num_repeats=1) @@ -372,6 +373,7 @@ def test_time_constant_ccw_el90(): wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + wiregrid.run.wiregrid.rotate = MagicMock() wiregrid.time_constant(num_repeats=1) From 2e7e481cd6863804270198824c521c9a5b9b8461 Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Mon, 28 Apr 2025 13:59:14 +0900 Subject: [PATCH 33/43] update test_wiregrid.py --- tests/test_wiregrid.py | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 8da71cfc..e7ebf15a 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -35,27 +35,6 @@ def create_acu_client(az, el, boresight): return acu_client -def create_hwp_client(direction): - """Create a HWP client with mock acq Process session.data. - - Args: - direction (str): direction of the HWP. 'cw' (clockwise) or 'ccw' (counter-clockwise). - - """ - client = MagicMock() - session = create_session('acq') - session.data = { - 'hwp_state': { - 'direction': direction, - }, - 'timestamp': time.time(), - } - reply = OCSReply(ocs.OK, 'msg', session.encoded()) - client.monitor.status = MagicMock(return_value=reply) - - return client - - def create_labjack_client(): """Create a labjack client with mock acq Process session.data.""" client = MagicMock() @@ -359,7 +338,7 @@ def test_time_constant_cw(): assert client.stream.start.call_args_list == expected_calls_of_streams assert client.stream.stop.call_count == 6 - assert wiregrid.run.wiregrid.rotate.call_count == 2 + assert wiregrid.run.wiregrid.rotate.call_count == 3 @patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) From 620866373c8da4c0e1a8d41781d755e9821ca367 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 30 Apr 2025 13:41:31 +0900 Subject: [PATCH 34/43] Update hwp.py Rename and move a function: get_direction() --> _get_direction() --- src/sorunlib/hwp.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 53e545cf..6a5601bb 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -2,6 +2,27 @@ from sorunlib._internal import check_response +def _get_direction(): + """Return the rotational direction ('cw' or 'ccw') of the HWP. + 'cw' means clockwise rotation seen from the sky to window. (+Hz) + 'ccw' means counter-clockwise rotation seen from the sky to window. (-Hz) + + Args: + None + + .. _docs: https://socs.readthedocs.io/en/main/agents/hwp_supervisor_agent.html + + """ + hwp = run.CLIENTS['hwp'] + resp = hwp.monitor.status() + direction = resp.session['data']['hwp_state']['direction'] + + if direction not in ['cw', 'ccw']: + raise RuntimeError("The HWP direction is unknown. Aborting...") + + return direction + + # Public API def set_freq(freq): """Set the rotational frequency of the HWP. @@ -43,24 +64,3 @@ def stop(active=True, brake_voltage=None): else: resp = hwp.pmx_off(wait_stop=True) check_response(hwp, resp) - - -def get_direction(): - """Return the rotational direction ('cw' or 'ccw') of the HWP. - 'cw' means clockwise rotation seen from the sky to window. (+Hz) - 'ccw' means counter-clockwise rotation seen from the sky to window. (-Hz) - - Args: - None - - .. _docs: https://socs.readthedocs.io/en/main/agents/hwp_supervisor_agent.html - - """ - hwp = run.CLIENTS['hwp'] - resp = hwp.monitor.status() - direction = resp.session['data']['hwp_state']['direction'] - - if direction not in ['cw', 'ccw']: - raise RuntimeError("The HWP direction is unknown. Aborting...") - - return direction From a76a4fa48c32d7caed7fcc49ae219e90f38c4c3d Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 30 Apr 2025 13:42:17 +0900 Subject: [PATCH 35/43] Update wiregrid.py Rename hwp.get_direction() --> hwp._get_direction() --- src/sorunlib/wiregrid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index c6d0db07..aa627619 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -360,7 +360,7 @@ def time_constant(num_repeats=1): # Check the current HWP direction try: # return 'cw' or 'ccw' - current_hwp_direction = run.hwp.get_direction() + current_hwp_direction = run.hwp._get_direction() except RuntimeError as e: error = "Wiregrid time constant measurment was failed " + \ "due to the failure in getting hwp direction. " + str(e) From 8a6c3d0cde7f1a28a32d808d73ba24d08d7611f9 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 30 Apr 2025 13:43:43 +0900 Subject: [PATCH 36/43] Update test_wiregrid.py rename: hwp.get_direction() --> hwp._get_direction() --- tests/test_wiregrid.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index e7ebf15a..7bab8d38 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -297,7 +297,7 @@ def test__check_wiregrid_position_invalid(position): def test_time_constant_cw(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.hwp.get_direction = MagicMock(return_value='cw') # cw + wiregrid.run.hwp._get_direction = MagicMock(return_value='cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -346,7 +346,7 @@ def test_time_constant_cw(): def test_time_constant_ccw_el90(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 90, 0) - wiregrid.run.hwp.get_direction = MagicMock(return_value='ccw') # ccw + wiregrid.run.hwp._get_direction = MagicMock(return_value='ccw') # ccw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() @@ -395,7 +395,7 @@ def test_time_constant_ccw_el90(): def test_time_constant_repeats(): # Setup all mock clients wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) - wiregrid.run.hwp.get_direction = MagicMock(return_value='cw') # cw + wiregrid.run.hwp._get_direction = MagicMock(return_value='cw') # cw wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ create_actuator_client(motor=1, position='outside') wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() From 95496f8e6bc6323f353968ac6430fb525f2141d5 Mon Sep 17 00:00:00 2001 From: Shunsuke Adachi <38268229+sadachi5@users.noreply.github.com> Date: Wed, 30 Apr 2025 13:44:59 +0900 Subject: [PATCH 37/43] Update test_hwp.py Rename: hwp.get_direction() --> hwp._get_direction() --- tests/test_hwp.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/test_hwp.py b/tests/test_hwp.py index 6726abbe..4684f4ba 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -33,6 +33,26 @@ def create_hwp_client(direction): client.monitor.status = MagicMock(return_value=reply) return client + + +@pytest.mark.parametrize('direction', ['cw', 'ccw']) +def test_get_direction(patch_clients_satp, direction): + hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) + ret = hwp._get_direction() + if direction == 'cw': + assert ret == 'cw' + elif direction == 'ccw': + assert ret == 'ccw' + hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() + + +@pytest.mark.parametrize('direction', [None, '']) +def test_get_direction_invalid(patch_clients_satp, direction): + hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) + with pytest.raises(RuntimeError) as e: + hwp._get_direction() + assert str(e.value) == "The HWP direction is unknown. Aborting..." + hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() @pytest.mark.parametrize("active", [True, False]) @@ -53,23 +73,3 @@ def test_stop_brake_voltage(patch_clients_satp): def test_set_freq(patch_clients_satp): hwp.set_freq(freq=2.0) hwp.run.CLIENTS['hwp'].pid_to_freq.assert_called_with(target_freq=2.0) - - -@pytest.mark.parametrize('direction', ['cw', 'ccw']) -def test_get_direction(patch_clients_satp, direction): - hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) - ret = hwp.get_direction() - if direction == 'cw': - assert ret == 'cw' - elif direction == 'ccw': - assert ret == 'ccw' - hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() - - -@pytest.mark.parametrize('direction', [None, '']) -def test_get_direction_invalid(patch_clients_satp, direction): - hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) - with pytest.raises(RuntimeError) as e: - hwp.get_direction() - assert str(e.value) == "The HWP direction is unknown. Aborting..." - hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() From 70c9e000a166ca477387c7c12bd1c131667f43bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Apr 2025 04:45:06 +0000 Subject: [PATCH 38/43] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_hwp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hwp.py b/tests/test_hwp.py index 4684f4ba..d533eb4a 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -33,7 +33,7 @@ def create_hwp_client(direction): client.monitor.status = MagicMock(return_value=reply) return client - + @pytest.mark.parametrize('direction', ['cw', 'ccw']) def test_get_direction(patch_clients_satp, direction): From 466b3684c54990d699035f2534a6216b5bcebcdb Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Wed, 30 Apr 2025 13:55:35 +0900 Subject: [PATCH 39/43] update test_hwp.py --- tests/test_hwp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_hwp.py b/tests/test_hwp.py index d533eb4a..4c5472ad 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -36,7 +36,7 @@ def create_hwp_client(direction): @pytest.mark.parametrize('direction', ['cw', 'ccw']) -def test_get_direction(patch_clients_satp, direction): +def test__get_direction(patch_clients_satp, direction): hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) ret = hwp._get_direction() if direction == 'cw': @@ -47,7 +47,7 @@ def test_get_direction(patch_clients_satp, direction): @pytest.mark.parametrize('direction', [None, '']) -def test_get_direction_invalid(patch_clients_satp, direction): +def test__get_direction_invalid(patch_clients_satp, direction): hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) with pytest.raises(RuntimeError) as e: hwp._get_direction() From 809fc1764437bb44a7d0b9048ce7598e14071e6d Mon Sep 17 00:00:00 2001 From: sadachi5 Date: Wed, 30 Apr 2025 14:06:39 +0900 Subject: [PATCH 40/43] update hwp.py, wiregrid.py, and their test scripts --- src/sorunlib/hwp.py | 10 +++++----- src/sorunlib/wiregrid.py | 14 +++++++------- tests/test_hwp.py | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 6a5601bb..541e320a 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -4,11 +4,11 @@ def _get_direction(): """Return the rotational direction ('cw' or 'ccw') of the HWP. - 'cw' means clockwise rotation seen from the sky to window. (+Hz) - 'ccw' means counter-clockwise rotation seen from the sky to window. (-Hz) + 'ccw' means counter-clockwise rotation seen from the sky to window. (+Hz) + 'cw' means clockwise rotation seen from the sky to window. (-Hz) - Args: - None + Returns: + str: The direction of the hwp, either 'ccw' or 'cw' .. _docs: https://socs.readthedocs.io/en/main/agents/hwp_supervisor_agent.html @@ -17,7 +17,7 @@ def _get_direction(): resp = hwp.monitor.status() direction = resp.session['data']['hwp_state']['direction'] - if direction not in ['cw', 'ccw']: + if direction not in ['ccw', 'cw']: raise RuntimeError("The HWP direction is unknown. Aborting...") return direction diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index aa627619..f81f0d72 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -359,7 +359,7 @@ def time_constant(num_repeats=1): # Check the current HWP direction try: - # return 'cw' or 'ccw' + # return 'ccw' or 'cw' current_hwp_direction = run.hwp._get_direction() except RuntimeError as e: error = "Wiregrid time constant measurment was failed " + \ @@ -389,10 +389,10 @@ def time_constant(num_repeats=1): run.smurf.stream('off') for i in range(num_repeats): - if current_hwp_direction == 'cw': - target_hwp_direction = 'ccw' - elif current_hwp_direction == 'ccw': + if current_hwp_direction == 'ccw': target_hwp_direction = 'cw' + elif current_hwp_direction == 'cw': + target_hwp_direction = 'ccw' # Bias step (the wire grid is on the window) # before stopping the HWP @@ -432,10 +432,10 @@ def time_constant(num_repeats=1): f'hwp_change_stop_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Spin up the HWP reversely - if target_hwp_direction == 'cw': - run.hwp.set_freq(freq=-2.0) - elif target_hwp_direction == 'ccw': + if target_hwp_direction == 'ccw': run.hwp.set_freq(freq=2.0) + elif target_hwp_direction == 'cw': + run.hwp.set_freq(freq=-2.0) current_hwp_direction = target_hwp_direction finally: # Stop SMuRF streams diff --git a/tests/test_hwp.py b/tests/test_hwp.py index 4c5472ad..c9e01231 100644 --- a/tests/test_hwp.py +++ b/tests/test_hwp.py @@ -18,7 +18,7 @@ def create_hwp_client(direction): """Create a HWP client with mock acq Process session.data. Args: - direction (str): direction of the HWP. 'cw' (clockwise) or 'ccw' (counter-clockwise). + direction (str): direction of the HWP. 'ccw' (counter-clockwise) or 'cw' (clockwise). """ client = MagicMock() @@ -35,14 +35,14 @@ def create_hwp_client(direction): return client -@pytest.mark.parametrize('direction', ['cw', 'ccw']) +@pytest.mark.parametrize('direction', ['ccw', 'cw']) def test__get_direction(patch_clients_satp, direction): hwp.run.CLIENTS['hwp'] = create_hwp_client(direction) ret = hwp._get_direction() - if direction == 'cw': - assert ret == 'cw' - elif direction == 'ccw': + if direction == 'ccw': assert ret == 'ccw' + elif direction == 'cw': + assert ret == 'cw' hwp.run.CLIENTS['hwp'].monitor.status.assert_called_once() From 36e4085d6292510edb8f7920ed6206608d89a481 Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Tue, 13 May 2025 14:58:35 -0400 Subject: [PATCH 41/43] Add test for failed direction determination --- tests/test_wiregrid.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_wiregrid.py b/tests/test_wiregrid.py index 7bab8d38..ece8abab 100644 --- a/tests/test_wiregrid.py +++ b/tests/test_wiregrid.py @@ -1,6 +1,7 @@ import os import time os.environ["OCS_CONFIG_DIR"] = "./test_util/" +os.environ["SORUNLIB_CONFIG"] = "./data/example_config.yaml" import pytest @@ -462,3 +463,21 @@ def test_time_constant_wiregrid_position_failed(): with pytest.raises(RuntimeError): wiregrid.time_constant(num_repeats=1) + + +@patch('sorunlib.wiregrid.run.CLIENTS', mocked_clients()) +@patch('sorunlib.wiregrid.time.sleep', MagicMock()) +def test_time_constant_hwp_direction_failed(): + wiregrid.run.CLIENTS['acu'] = create_acu_client(180, 50, 0) + wiregrid.run.CLIENTS['wiregrid']['actuator'] = \ + create_actuator_client(motor=1, position='outside') + wiregrid.run.CLIENTS['wiregrid']['kikusui'] = create_kikusui_client() + wiregrid.run.CLIENTS['wiregrid']['encoder'] = create_encoder_client() + wiregrid.run.CLIENTS['wiregrid']['labjack'] = create_labjack_client() + + # Set up expected raise from invalid direction + wiregrid.run.hwp._get_direction = MagicMock(return_value=None) + wiregrid.run.hwp._get_direction.side_effect = RuntimeError("The HWP direction is unknown. Aborting...") + + with pytest.raises(RuntimeError): + wiregrid.time_constant(num_repeats=1) From 3fbf4aaeae72ef2d2b7e3702254a6346bf33fccd Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Tue, 13 May 2025 15:37:45 -0400 Subject: [PATCH 42/43] Clean up docstrings and comments I mostly wanted to remove the implied ordering of 'cw' and 'ccw' and drop the references to the correspondance between direction and +/- frequency. This correspondance can change depending on the hardware or agent configuration. In the future this should be hidden by the agents so that a frequency and direction are given. Then within sorunlib we can avoid needing to know the correspondance internally. Other changes are for readability and consistency with overall style within the repo. --- src/sorunlib/hwp.py | 16 ++++++++++----- src/sorunlib/wiregrid.py | 42 ++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/sorunlib/hwp.py b/src/sorunlib/hwp.py index 541e320a..8b696615 100644 --- a/src/sorunlib/hwp.py +++ b/src/sorunlib/hwp.py @@ -3,12 +3,18 @@ def _get_direction(): - """Return the rotational direction ('cw' or 'ccw') of the HWP. - 'ccw' means counter-clockwise rotation seen from the sky to window. (+Hz) - 'cw' means clockwise rotation seen from the sky to window. (-Hz) + """Get the rotational direction ('cw' or 'ccw') of the HWP. The direction + is determined in part by the configuration of the HWP supervisor agent. For + details see the `HWP Supervisor agent docs `_. + + * 'cw' is clockwise as seen from the sky to window. + * 'ccw' is counter-clockwise as seen from the sky to window. Returns: - str: The direction of the hwp, either 'ccw' or 'cw' + str: The direction of the HWP, either 'cw' or 'ccw'. + + Raises: + RuntimeError: If the direction is not either 'cw' or 'ccw'. .. _docs: https://socs.readthedocs.io/en/main/agents/hwp_supervisor_agent.html @@ -17,7 +23,7 @@ def _get_direction(): resp = hwp.monitor.status() direction = resp.session['data']['hwp_state']['direction'] - if direction not in ['ccw', 'cw']: + if direction not in ['cw', 'ccw']: raise RuntimeError("The HWP direction is unknown. Aborting...") return direction diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index f81f0d72..d3e18d8d 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -8,9 +8,8 @@ AGENT_TIMEDIFF_THRESHOLD = 5 # sec OP_TIMEOUT = 60 -# Internal Helper Functions - +# Internal Helper Functions def _check_process_data(process, last_timestamp): """Check the latest timestamp from a process' session.data is recent enough. @@ -359,11 +358,10 @@ def time_constant(num_repeats=1): # Check the current HWP direction try: - # return 'ccw' or 'cw' - current_hwp_direction = run.hwp._get_direction() + current_hwp_direction = run.hwp._get_direction() # 'cw' or 'ccw' except RuntimeError as e: - error = "Wiregrid time constant measurment was failed " + \ - "due to the failure in getting hwp direction. " + str(e) + error = "Wiregrid time constant measurment has failed " + \ + "due to a failure in getting the HWP direction.\n" + str(e) raise RuntimeError(error) # Rotate to get encoder reference before insertion @@ -375,17 +373,14 @@ def time_constant(num_repeats=1): run.smurf.bias_step(tag=bs_tag, concurrent=True) time.sleep(5) - # Insert the wiregrid with streaming + # Insert the wiregrid while streaming try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_inserting, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Insert the wiregrid insert() time.sleep(5) finally: - # Stop SMuRF streams run.smurf.stream('off') for i in range(num_repeats): @@ -395,14 +390,13 @@ def time_constant(num_repeats=1): target_hwp_direction = 'ccw' # Bias step (the wire grid is on the window) - # before stopping the HWP + # Before stopping the HWP bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) # Run stepwise rotation before stopping the HWP try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag @@ -410,40 +404,36 @@ def time_constant(num_repeats=1): # Run stepwise rotation rotate(False) finally: - # Stop SMuRF streams run.smurf.stream('off') - # Stop the HWP with streaming + # Stop the HWP while streaming try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'hwp_change_{current_hwp_direction}_to_stop' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Stop the HWP run.hwp.stop(active=True) finally: - # Stop SMuRF streams run.smurf.stream('off') - # Spin up the HWP reversely with streaming + # Reverse the HWP while streaming try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'hwp_change_stop_to_{target_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Spin up the HWP reversely + # Note: This is hardcoding the correspondance between direction and + # the sign of the frequency, which is subject to change depending + # on the hardware/agent configuration. This should be removed in + # the future, if possible. if target_hwp_direction == 'ccw': run.hwp.set_freq(freq=2.0) elif target_hwp_direction == 'cw': run.hwp.set_freq(freq=-2.0) current_hwp_direction = target_hwp_direction finally: - # Stop SMuRF streams run.smurf.stream('off') # Run stepwise rotation after changing the HWP rotation try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, ' + \ f'wg_stepwise, hwp_{current_hwp_direction}' + \ el_tag @@ -451,27 +441,23 @@ def time_constant(num_repeats=1): # Run stepwise rotation rotate(False) finally: - # Stop SMuRF streams run.smurf.stream('off') # Bias step (the wire grid is on the window) - # after changing the HWP rotation + # After changing the HWP rotation bs_tag = 'wiregrid, wg_time_constant, wg_inserted, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.bias_step(tag=bs_tag, concurrent=True) time.sleep(5) - # Eject the wiregrid with streaming + # Eject the wiregrid while streaming try: - # Enable SMuRF streams stream_tag = 'wiregrid, wg_time_constant, wg_ejecting, ' + \ f'hwp_{current_hwp_direction}' + el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') - # Eject the wiregrid eject() time.sleep(5) finally: - # Stop SMuRF streams run.smurf.stream('off') # Bias step (the wire grid is off the window) From 2b842f16b2f41c141f22ca56c41ed243445455c4 Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Tue, 13 May 2025 15:41:57 -0400 Subject: [PATCH 43/43] Add argument name when calling rotate function Just for readability. --- src/sorunlib/wiregrid.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sorunlib/wiregrid.py b/src/sorunlib/wiregrid.py index d3e18d8d..f8b993c6 100644 --- a/src/sorunlib/wiregrid.py +++ b/src/sorunlib/wiregrid.py @@ -402,7 +402,7 @@ def time_constant(num_repeats=1): el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation - rotate(False) + rotate(continuous=False) finally: run.smurf.stream('off') @@ -439,7 +439,7 @@ def time_constant(num_repeats=1): el_tag run.smurf.stream('on', tag=stream_tag, subtype='cal') # Run stepwise rotation - rotate(False) + rotate(continuous=False) finally: run.smurf.stream('off')