From 8695da59f886017c5983de6c396e737a6cedffc2 Mon Sep 17 00:00:00 2001 From: bkelle <51238483+bkelle@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:51:27 -0500 Subject: [PATCH 1/6] Update agent.py for bias waves adding in code for bias waves. Essentially copying the bias step code block and modifying for bias wave analysis. --- socs/agents/pysmurf_controller/agent.py | 111 +++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index 0b62ba914..cf7a5dd47 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -19,7 +19,7 @@ from ocs.ocs_agent import log_formatter from ocs.ocs_twisted import TimeoutLock from sodetlib.det_config import DetConfig -from sodetlib.operations import (bias_dets, bias_steps, iv, uxm_relock, +from sodetlib.operations import (bias_dets, bias_steps, bias_wave, iv, uxm_relock, uxm_setup) NBIASLINES = 12 @@ -729,6 +729,7 @@ def take_iv(self, session, params): @ocs_agent.param('kwargs', default=None) @ocs_agent.param('rfrac_range', default=(0.2, 0.9)) @ocs_agent.param('tag', default=None) + def take_bias_steps(self, session, params): """take_bias_steps(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) @@ -833,6 +834,114 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) + + def take_bias_waves(self, session, params): + """take_bias_waves(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) + + **Task** - Takes bias_wave and saves the output filepath to the + session data object. See the `sodetlib bias wave docs page + + NEED TO CREATE DOCS PAGE AND INSERT HERE + + for more information on bias wave and what kwargs can be passed in. + + Args + ---- + kwargs : dict + Additional kwargs to pass to ``take_bias_wave`` function. + rfrac_range : tuple + Range of valid rfracs to check against when determining the number + of good detectors. + tag : Optional[str] + String containing a tag or comma-separated list of tags to attach + to the g3 stream. + + Notes + ------ + The following data will be written to the session.data object:: + + >> response.session['data'] + { + 'filepath': Filepath of saved BiasWaveAnalysis object + 'biased_total': Total number of detectors biased into rfrac_range + 'biased_per_bg': List containing number of biased detectors on each bias line + 'Rtes_quantiles': { + 'Rtes': List of 15%, 25%, 50%, 75%, 85% Rtes quantiles, + 'quantiles': List of quantile labels + 'count': Total count of the distribution + } + 'responsivity_quantiles': Same as above for responsivity + 'Rfrac_quantiles': Same as above for Rfrac + + } + """ + + if params['kwargs'] is None: + params['kwargs'] = {} + + if params['tag'] is not None: + params['kwargs']['g3_tag'] = params['tag'] + + with self.lock.acquire_timeout(0, job='bias_wave') as acquired: + if not acquired: + return False, f"Operation failed: {self.lock.job} is running." + + session.set_status('starting') + S, cfg = self._get_smurf_control(session=session) + bwa = bias_wave.take_bias_waves( + S, cfg, **params['kwargs'] + ) + + biased = np.logical_and.reduce([ + params['rfrac_range'][0] < bwa.Rfrac, + params['rfrac_range'][1] > bwa.Rfrac + ]) + session.data = { + 'filepath': bwa.filepath, + 'biased_total': int(np.sum(biased)), + 'biased_per_bg': [ + int(np.sum(biased[bwa.bgmap == bg])) for bg in range(12) + ], + } + quantiles = np.array([15, 25, 50, 75, 85]) + + def publish_quantile_block(arr, name): + if np.isnan(arr).all(): + return None + labels = [f'{name}_q{q}' for q in quantiles] + qs = [float(np.nanquantile(arr, q / 100)) for q in quantiles] + block = { + k: q + for k, q in zip(labels, qs) + } + count = int(np.sum(~np.isnan(arr))) + block[f'{name}_count'] = count + + session.data[f'{name}_quantiles'] = { + name: qs, + 'quantiles': labels, + 'count': count, + } + + d = { + 'timestamp': time.time(), + 'block_name': f'{name}_quantile', + 'data': block + } + + self.agent.publish_to_feed('bias_wave_quantiles', d) + return d + + # Resistance quantiles + publish_quantile_block(bwa.R0, 'Rtes') + publish_quantile_block(bwa.Si, 'resposnivity') + publish_quantile_block(bwa.Rfrac, 'Rfrac') + + return True, "Finished taking bias waves" + + @ocs_agent.param('bgs', default=None) + @ocs_agent.param('kwargs', default=None) + def overbias_tes(self, session, params): """overbias_tes(bgs=None, kwargs=None) From 748a1ea745c1c87e5473de550bfc355f03dbfbda Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 Feb 2024 18:57:23 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- socs/agents/pysmurf_controller/agent.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index cf7a5dd47..1b967aebb 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -19,8 +19,8 @@ from ocs.ocs_agent import log_formatter from ocs.ocs_twisted import TimeoutLock from sodetlib.det_config import DetConfig -from sodetlib.operations import (bias_dets, bias_steps, bias_wave, iv, uxm_relock, - uxm_setup) +from sodetlib.operations import (bias_dets, bias_steps, bias_wave, iv, + uxm_relock, uxm_setup) NBIASLINES = 12 @@ -729,7 +729,6 @@ def take_iv(self, session, params): @ocs_agent.param('kwargs', default=None) @ocs_agent.param('rfrac_range', default=(0.2, 0.9)) @ocs_agent.param('tag', default=None) - def take_bias_steps(self, session, params): """take_bias_steps(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) @@ -834,15 +833,14 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) - def take_bias_waves(self, session, params): """take_bias_waves(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) **Task** - Takes bias_wave and saves the output filepath to the session data object. See the `sodetlib bias wave docs page - + NEED TO CREATE DOCS PAGE AND INSERT HERE - + for more information on bias wave and what kwargs can be passed in. Args @@ -941,7 +939,6 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) - def overbias_tes(self, session, params): """overbias_tes(bgs=None, kwargs=None) From f9f8c3e78374dd18db1cc0558d3a59f15699c0fd Mon Sep 17 00:00:00 2001 From: bkelle <51238483+bkelle@users.noreply.github.com> Date: Fri, 23 Feb 2024 14:36:16 -0500 Subject: [PATCH 3/6] Update agent.py w/ changes requested by Jack. added in param for "tag" arg for bias wave (and biasstep... seemed to be missing there too). Rmoved docstring for nonexistent documentation. --- socs/agents/pysmurf_controller/agent.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index 1b967aebb..2fc0e9bb1 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -729,6 +729,7 @@ def take_iv(self, session, params): @ocs_agent.param('kwargs', default=None) @ocs_agent.param('rfrac_range', default=(0.2, 0.9)) @ocs_agent.param('tag', default=None) + def take_bias_steps(self, session, params): """take_bias_steps(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) @@ -833,15 +834,13 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) + @ocs_agent.param('tag', default=None) + def take_bias_waves(self, session, params): """take_bias_waves(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) **Task** - Takes bias_wave and saves the output filepath to the - session data object. See the `sodetlib bias wave docs page - - NEED TO CREATE DOCS PAGE AND INSERT HERE - - for more information on bias wave and what kwargs can be passed in. + session data object. Args ---- @@ -939,6 +938,8 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) + @ocs_agent.param('tag', default=None) + def overbias_tes(self, session, params): """overbias_tes(bgs=None, kwargs=None) From 688b21a3dbd5df08adb286ddeb9b761608b41a20 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 Feb 2024 19:37:10 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- socs/agents/pysmurf_controller/agent.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index 2fc0e9bb1..194702bae 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -729,7 +729,6 @@ def take_iv(self, session, params): @ocs_agent.param('kwargs', default=None) @ocs_agent.param('rfrac_range', default=(0.2, 0.9)) @ocs_agent.param('tag', default=None) - def take_bias_steps(self, session, params): """take_bias_steps(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) @@ -835,7 +834,6 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) @ocs_agent.param('tag', default=None) - def take_bias_waves(self, session, params): """take_bias_waves(kwargs=None, rfrac_range=(0.2, 0.9), tag=None) @@ -939,7 +937,6 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) @ocs_agent.param('tag', default=None) - def overbias_tes(self, session, params): """overbias_tes(bgs=None, kwargs=None) From 8a78a703e2b99352a78064f608239806d924626b Mon Sep 17 00:00:00 2001 From: bkelle <51238483+bkelle@users.noreply.github.com> Date: Fri, 23 Feb 2024 14:40:22 -0500 Subject: [PATCH 5/6] Update agent.py registered OCS operation at bottom of file --- socs/agents/pysmurf_controller/agent.py | 1 + 1 file changed, 1 insertion(+) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index 194702bae..783e411ac 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -1150,6 +1150,7 @@ def main(args=None): agent.register_task('bias_dets', controller.bias_dets) agent.register_task('take_iv', controller.take_iv) agent.register_task('take_bias_steps', controller.take_bias_steps) + agent.register_task('take_bias_waves', controller.take_bias_waves) agent.register_task('overbias_tes', controller.overbias_tes) agent.register_task('take_noise', controller.take_noise) agent.register_task('bias_dets', controller.bias_dets) From 0c1e78d191f832a79e0cad81871bab7ec21fb3a1 Mon Sep 17 00:00:00 2001 From: bkelle <51238483+bkelle@users.noreply.github.com> Date: Thu, 29 Feb 2024 07:42:27 -0500 Subject: [PATCH 6/6] removed stray decorator --- socs/agents/pysmurf_controller/agent.py | 1 - 1 file changed, 1 deletion(-) diff --git a/socs/agents/pysmurf_controller/agent.py b/socs/agents/pysmurf_controller/agent.py index 783e411ac..8cac1cbc7 100644 --- a/socs/agents/pysmurf_controller/agent.py +++ b/socs/agents/pysmurf_controller/agent.py @@ -936,7 +936,6 @@ def publish_quantile_block(arr, name): @ocs_agent.param('bgs', default=None) @ocs_agent.param('kwargs', default=None) - @ocs_agent.param('tag', default=None) def overbias_tes(self, session, params): """overbias_tes(bgs=None, kwargs=None)