From ee9d140813ea4a3419f7c12ae63e808ceb1ab8a1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 2 Mar 2012 14:17:19 -0500 Subject: [PATCH] Only calculate relative errors if a previous value exists. This avoids: TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' --- calibcant/analyze.py | 114 +++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/calibcant/analyze.py b/calibcant/analyze.py index 1033dcc..6a84000 100644 --- a/calibcant/analyze.py +++ b/calibcant/analyze.py @@ -406,6 +406,7 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5, (calibration_config['num-vibrations'],), dtype=float) changed_bump = changed_temperature = changed_vibration = False for i in range(calibration_config['num-bumps']): + _changed_bump = False bump_group = '%sbump/%d/' % (group, i) (raw_bump,bump_config,z_axis_config, deflection_channel_config,processed_bump) = _bump_load( @@ -415,16 +416,22 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5, z_axis_config=z_axis_config, deflection_channel_config=deflection_channel_config) bumps[i] = sensitivity - rel_error = abs(sensitivity - processed_bump)/processed_bump - if rel_error > maximum_relative_error: - _LOG.warn(("new analysis doesn't match for bump %d: %g -> %g " - "(difference: %g, relative error: %g)") - % (i, processed_bump, sensitivity, - sensitivity-processed_bump, rel_error)) + if processed_bump is None: + _changed_bump = True + _LOG.warn('new analysis for bump %d: %g' % (i, sensitivity)) + else: + rel_error = abs(sensitivity - processed_bump)/processed_bump + if rel_error > maximum_relative_error: + _changed_bump = True + _LOG.warn(("new analysis doesn't match for bump %d: %g -> %g " + "(difference: %g, relative error: %g)") + % (i, processed_bump, sensitivity, + sensitivity-processed_bump, rel_error)) + if _changed_bump and not dry_run: changed_bump = True - if not dry_run: - _bump_save(filename, bump_group, processed_bump=sensitivity) + _bump_save(filename, bump_group, processed_bump=sensitivity) for i in range(calibration_config['num-temperatures']): + _changed_temperature = False temperature_group = '%stemperature/%d/' % (group, i) (raw_temperature,temperature_config,processed_temperature ) = _temperature_load( @@ -432,19 +439,25 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5, temperature = _temperature_analyze( raw_temperature, temperature_config) temperatures[i] = temperature - rel_error = abs(temperature - processed_temperature - )/processed_temperature - if rel_error > maximum_relative_error: - _LOG.warn(("new analysis doesn't match for temperature %d: " - "%g -> %g (difference: %g, relative error: %g)") - % (i, processed_temperature, temperature, - temperature-processed_temperature, rel_error)) + if processed_temperature is None: + _changed_temperature = True + _LOG.warn('new analysis for temperature %d: %g' % (i, temperature)) + else: + rel_error = abs(temperature - processed_temperature + )/processed_temperature + if rel_error > maximum_relative_error: + _changed_temperature = True + _LOG.warn(("new analysis doesn't match for temperature %d: " + "%g -> %g (difference: %g, relative error: %g)") + % (i, processed_temperature, temperature, + temperature-processed_temperature, rel_error)) + if _changed_temperature and not dry_run: changed_temperature = True - if not dry_run: - _temperature_save( - filename, temperature_group, - processed_T=temperature) + _temperature_save( + filename, temperature_group, + processed_T=temperature) for i in range(calibration_config['num-vibrations']): + _changed_vibration = False vibration_group = '%svibration/%d/' % (group, i) (raw_vibration,vibration_config,deflection_channel_config, processed_vibration) = _vibration_load( @@ -453,16 +466,21 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5, deflection=raw_vibration, vibration_config=vibration_config, deflection_channel_config=deflection_channel_config) vibrations[i] = variance - rel_error = abs(variance - processed_vibration)/processed_vibration - if rel_error > maximum_relative_error: - _LOG.warn(("new analysis doesn't match for vibration %d: %g -> %g " - "(difference: %g, relative error: %g)") - % (i, processed_vibration, variance, - variance-processed_vibration, rel_error)) + if processed_vibration is None: + _changed_vibration = True + _LOG.warn('new analysis for vibration %d: %g' % (i, variance)) + else: + rel_error = abs(variance - processed_vibration)/processed_vibration + if rel_error > maximum_relative_error: + _changed_vibration = True + _LOG.warn(("new analysis doesn't match for vibration %d: " + "%g -> %g (difference: %g, relative error: %g)") + % (i, processed_vibration, variance, + variance-processed_vibration, rel_error)) + if _changed_vibration and not dry_run: changed_vibration = True - if not dry_run: - _vibration_save( - filename, vibration_group, processed_vibration=variance) + _vibration_save( + filename, vibration_group, processed_vibration=variance) calib_group = '%scalibration/' % group @@ -475,20 +493,32 @@ def calib_analyze_all(filename, group='/', maximum_relative_error=1e-5, new_k,new_k_s = calib_analyze( bumps=bumps, temperatures=temperatures, vibrations=vibrations) - rel_error = abs(new_k-k)/k - if rel_error > maximum_relative_error: - _LOG.warn(("new analysis doesn't match for k: %g -> %g " - "(difference: %g, relative error: %g)") - % (k, new_k, new_k-k, rel_error)) - if not dry_run: - calib_save(filename, calib_group, k=new_k) - rel_error = abs(new_k_s-k_s)/k_s - if rel_error > maximum_relative_error: - _LOG.warn(("new analysis doesn't match for k_s: %g -> %g " - "(difference: %g, relative error: %g)") - % (k_s, new_k_s, new_k_s-k_s, rel_error)) - if not dry_run: - calib_save(filename, calib_group, k_s=new_k_s) + new_calib_k = False + if k is None: + new_calib_k = True + _LOG.warn('new analysis for k: %g' % new_k) + else: + rel_error = abs(new_k-k)/k + if rel_error > maximum_relative_error: + new_calib_k = True + _LOG.warn(("new analysis doesn't match for k: %g -> %g " + "(difference: %g, relative error: %g)") + % (k, new_k, new_k-k, rel_error)) + if new_calib_k and not dry_run: + calib_save(filename, calib_group, k=new_k) + new_calib_k_s = False + if k_s is None: + new_calib_k_s = True + _LOG.warn('new analysis for k_s: %g' % new_k_s) + else: + rel_error = abs(new_k_s-k_s)/k_s + if rel_error > maximum_relative_error: + new_calib_k_s = True + _LOG.warn(("new analysis doesn't match for k_s: %g -> %g " + "(difference: %g, relative error: %g)") + % (k_s, new_k_s, new_k_s-k_s, rel_error)) + if new_calib_k_s and not dry_run: + calib_save(filename, calib_group, k_s=new_k_s) return (new_k, new_k_s) def calib_plot_all(bumps, bump_details, temperatures, temperature_details, -- 2.26.2