%22%22%22Compare%20whole-signal%20counting%20with%20segmented%20cycle%20count%20aggregation.%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App(css_file%3D%22..%2Fdocs%2Fstylesheets%2Fcustom.css%22)%0A%0A%0A%40app.cell%0Adef%20introduction()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Add%20cycle%20counts%20and%20recover%20residuals%0A%0A%20%20%20%20%20%20%20%20Count%20a%20sampled%20history%20in%20two%20windows%2C%20add%20their%20%60CycleCount%60%20objects%2C%0A%20%20%20%20%20%20%20%20then%20call%20%60solve_lffd()%60%20to%20recount%20the%20turning%20points%20left%20over%20at%0A%20%20%20%20%20%20%20%20the%20window%20boundaries.%20Compare%20with%20counting%20the%20whole%20signal%20at%20once.%0A%0A%20%20%20%20%20%20%20%20The%20example%20uses%20a%20short%20synthetic%20signal.%20Check%20how%20both%20the%20spectrum%0A%20%20%20%20%20%20%20%20and%20Miner%20damage%20change%20when%20the%20residuals%20are%20resolved.%0A%20%20%20%20%20%20%20%20%22%22%22)%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20compare(mo)%3A%0A%20%20%20%20from%20datetime%20import%20datetime%2C%20timedelta%2C%20timezone%0A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20from%20py_fatigue%20import%20CycleCount%2C%20SNCurve%0A%20%20%20%20from%20py_fatigue.damage%20import%20get_pm%0A%20%20%20%20from%20_examples.charts%20import%20plot_views%0A%0A%20%20%20%20stress%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%207%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%2010%2C%0A%20%20%20%20%20%20%20%20%20%20%20%205%2C%0A%20%20%20%20%20%20%20%20%20%20%20%209%2C%0A%20%20%20%20%20%20%20%20%20%20%20%203%2C%0A%20%20%20%20%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%2012%2C%0A%20%20%20%20%20%20%20%20%20%20%20%205%2C%0A%20%20%20%20%20%20%20%20%20%20%20%2011%2C%0A%20%20%20%20%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%203%2C%0A%20%20%20%20%20%20%20%20%20%20%20%2010%2C%0A%20%20%20%20%20%20%20%20%20%20%20%206%2C%0A%20%20%20%20%20%20%20%20%20%20%20%2012%2C%0A%20%20%20%20%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%208%2C%0A%20%20%20%20%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%209%2C%0A%20%20%20%20%20%20%20%20%20%20%20%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%206%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20dtype%3Dfloat%2C%0A%20%20%20%20)%0A%20%20%20%20split%20%3D%20len(stress)%20%2F%2F%202%0A%20%20%20%20start%20%3D%20datetime(2024%2C%201%2C%201%2C%20tzinfo%3Dtimezone.utc)%0A%20%20%20%20first%20%3D%20CycleCount.from_timeseries(stress%5B%3Asplit%5D%2C%20timestamp%3Dstart)%0A%20%20%20%20second%20%3D%20CycleCount.from_timeseries(%0A%20%20%20%20%20%20%20%20stress%5Bsplit%3A%5D%2C%20timestamp%3Dstart%20%2B%20timedelta(seconds%3Dsplit)%0A%20%20%20%20)%0A%20%20%20%20combined%20%3D%20first%20%2B%20second%0A%20%20%20%20recovered%20%3D%20combined.solve_lffd()%0A%20%20%20%20whole%20%3D%20CycleCount.from_timeseries(stress%2C%20timestamp%3Dstart)%0A%20%20%20%20curve%20%3D%20SNCurve(slope%3D3%2C%20intercept%3D12)%0A%0A%20%20%20%20fig%2C%20(signal%2C%20spectrum)%20%3D%20plt.subplots(2%2C%201%2C%20layout%3D%22constrained%22)%0A%20%20%20%20signal.plot(stress%2C%20marker%3D%22o%22)%0A%20%20%20%20signal.axvline(%0A%20%20%20%20%20%20%20%20split%20-%200.5%2C%20color%3D%220.4%22%2C%20linestyle%3D%22--%22%2C%20label%3D%22Window%20split%22%0A%20%20%20%20)%0A%20%20%20%20signal.set(xlabel%3D%22Sample%20at%201%20Hz%22%2C%20ylabel%3D%22Stress%20(MPa)%22)%0A%20%20%20%20signal.legend()%0A%0A%20%20%20%20summary%20%3D%20%5B%5D%0A%20%20%20%20for%20label%2C%20cc%20in%20(%0A%20%20%20%20%20%20%20%20(%22Added%20windows%22%2C%20combined)%2C%0A%20%20%20%20%20%20%20%20(%22Residuals%20resolved%22%2C%20recovered)%2C%0A%20%20%20%20%20%20%20%20(%22Whole%20signal%22%2C%20whole)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20ranges%2C%20bins%20%3D%20np.unique(cc.stress_range%2C%20return_inverse%3DTrue)%0A%20%20%20%20%20%20%20%20counts%20%3D%20np.bincount(bins%2C%20weights%3Dcc.count_cycle)%0A%20%20%20%20%20%20%20%20spectrum.plot(ranges%2C%20np.cumsum(counts%5B%3A%3A-1%5D)%5B%3A%3A-1%5D%2C%20%22o-%22%2C%20label%3Dlabel)%0A%20%20%20%20%20%20%20%20summary.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Spectrum%22%3A%20label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Remaining%20turning%20points%22%3A%20len(cc.residuals_sequence)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Miner%20damage%22%3A%20float(get_pm(cc%2C%20curve).sum())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20spectrum.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Stress%20range%20(MPa)%22%2C%20ylabel%3D%22Cycles%20at%20this%20range%20or%20higher%22%0A%20%20%20%20)%0A%20%20%20%20spectrum.legend()%0A%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20plot_views(fig%2C%20height%3D%22830px%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(summary)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20long_case()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20_plt%0A%20%20%20%20import%20numpy%20as%20_np%0A%20%20%20%20from%20_examples.synthetic%20import%20(%0A%20%20%20%20%20%20%20%20count_windows%2C%0A%20%20%20%20%20%20%20%20long_case_view%2C%0A%20%20%20%20%20%20%20%20synthetic_peaks%2C%0A%20%20%20%20%20%20%20%20tensile_waveform%2C%0A%20%20%20%20)%0A%20%20%20%20from%20py_fatigue%20import%20CycleCount%20as%20_CycleCount%2C%20SNCurve%20as%20_SNCurve%0A%20%20%20%20from%20py_fatigue.damage%20import%20get_pm%20as%20_get_pm%0A%0A%20%20%20%20peaks%20%3D%20synthetic_peaks()%0A%20%20%20%20waveform%20%3D%20tensile_waveform(peaks)%0A%20%20%20%20long_combined%20%3D%20count_windows(waveform)%0A%20%20%20%20resolved%20%3D%20long_combined.solve_lffd()%0A%20%20%20%20reference%20%3D%20_CycleCount.from_timeseries(waveform)%0A%20%20%20%20edges%20%3D%20_np.histogram_bin_edges(reference.stress_range%2C%20bins%3D60)%0A%20%20%20%20centres%20%3D%20(edges%5B%3A-1%5D%20%2B%20edges%5B1%3A%5D)%20%2F%202%0A%20%20%20%20sn%20%3D%20_SNCurve(slope%3D3%2C%20intercept%3D10.493)%0A%20%20%20%20reference_damage%2C%20_%20%3D%20_np.histogram(%0A%20%20%20%20%20%20%20%20reference.stress_range%2C%20bins%3Dedges%2C%20weights%3D_get_pm(reference%2C%20sn)%0A%20%20%20%20)%0A%20%20%20%20long_fig%2C%20ax%20%3D%20_plt.subplots()%0A%20%20%20%20for%20long_label%2C%20long_spectrum%20in%20(%0A%20%20%20%20%20%20%20%20(%22Added%20windows%22%2C%20long_combined)%2C%0A%20%20%20%20%20%20%20%20(%22Residuals%20resolved%22%2C%20resolved)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20damage_by_range%2C%20_%20%3D%20_np.histogram(%0A%20%20%20%20%20%20%20%20%20%20%20%20long_spectrum.stress_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20bins%3Dedges%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20weights%3D_get_pm(long_spectrum%2C%20sn)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20ax.plot(centres%2C%20damage_by_range%20-%20reference_damage%2C%20label%3Dlong_label)%0A%20%20%20%20ax.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Stress%20range%20(MPa)%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22Miner%20damage%20difference%20from%20whole-signal%20count%22%2C%0A%20%20%20%20)%0A%20%20%20%20ax.legend()%0A%20%20%20%20long_case_view(%0A%20%20%20%20%20%20%20%20long_fig%2C%0A%20%20%20%20%20%20%20%20f%22Count%20two%20consecutive%20pieces%20of%20a%20**%7Bpeaks.size%3A%2C%7D-cycle**%20waveform.%20%22%0A%20%20%20%20%20%20%20%20%22Compare%20the%20damage%20in%20each%20range%20bin%20against%20the%20whole-signal%20result.%20%22%0A%20%20%20%20%20%20%20%20f%22The%20whole-signal%20damage%20is%20**%7Breference_damage.sum()%3A.5f%7D**.%22%2C%0A%20%20%20%20)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1860060d2aa2fe4f62628a128bca82e9