%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%20first%20window%20draws%20120%20integer%20stresses%20from%201%20to%2012%20MPa.%0A%20%20%20%20%20%20%20%20The%20second%20draws%20120%20from%2021%20to%2032%20MPa.%20The%20fixed%20seeds%20reproduce%0A%20%20%20%20%20%20%20%20the%20Handbook%20example.%20Look%20for%20the%2031%20MPa%20range%20in%20the%20recovered%0A%20%20%20%20%20%20%20%20and%20whole-signal%20spectra.%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(hide_code%3DTrue)%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%20import%20pandas%20as%20pd%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%20py_fatigue.cycle_count.rainflow%20import%20rainflow%0A%20%20%20%20from%20py_fatigue.testing%20import%20get_random_data%2C%20get_sampled_time%0A%20%20%20%20from%20_examples.charts%20import%20plot_views%0A%0A%20%20%20%20split%20%3D%20120%0A%20%20%20%20time%20%3D%20get_sampled_time(fs%3D1%2C%20duration%3Dsplit)%0A%20%20%20%20first_stress%20%3D%20get_random_data(%0A%20%20%20%20%20%20%20%20time%2C%20seed%3D0%2C%20random_type%3D%22integers%22%2C%20low%3D1%2C%20high%3D13%2C%20min_%3D1%2C%20range_%3D11%0A%20%20%20%20)%0A%20%20%20%20second_stress%20%3D%20get_random_data(%0A%20%20%20%20%20%20%20%20time%2C%20seed%3D1%2C%20random_type%3D%22integers%22%2C%20low%3D1%2C%20high%3D13%2C%20min_%3D21%2C%20range_%3D11%0A%20%20%20%20)%0A%20%20%20%20stress%20%3D%20np.concatenate((first_stress%2C%20second_stress))%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(%0A%20%20%20%20%20%20%20%202%2C%201%2C%20figsize%3D(8%2C%208)%2C%20layout%3D%22constrained%22%0A%20%20%20%20)%0A%20%20%20%20for%20label%2C%20window%2C%20offset%20in%20(%0A%20%20%20%20%20%20%20%20(%22Window%201%22%2C%20stress%5B%3Asplit%5D%2C%200)%2C%0A%20%20%20%20%20%20%20%20(%22Window%202%22%2C%20stress%5Bsplit%3A%5D%2C%20split)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20signal.plot(np.arange(len(window))%20%2B%20offset%2C%20window%2C%20%22-%22%2C%20label%3Dlabel)%0A%20%20%20%20%20%20%20%20_%2C%20residual%2C%20indices%20%3D%20rainflow(window)%0A%20%20%20%20%20%20%20%20signal.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20indices%20%2B%20offset%2C%20residual%2C%20%22s--%22%2C%20label%3Df%22%7Blabel%7D%20residual%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20signal.plot(%0A%20%20%20%20%20%20%20%20%5Bsplit%20-%201%2C%20split%5D%2C%0A%20%20%20%20%20%20%20%20stress%5Bsplit%20-%201%20%3A%20split%20%2B%201%5D%2C%0A%20%20%20%20%20%20%20%20%22k%3A%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Join%20in%20full%20record%22%2C%0A%20%20%20%20)%0A%20%20%20%20signal.axvline(split%20-%200.5%2C%20color%3D%220.5%22%2C%20linestyle%3D%22--%22)%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(ncols%3D2)%0A%0A%20%20%20%20summary%20%3D%20%5B%5D%0A%20%20%20%20whole_damage%20%3D%20float(get_pm(whole%2C%20curve).sum())%0A%20%20%20%20short_edges%20%3D%20np.arange(0.5%2C%20stress.max()%20-%20stress.min()%20%2B%201.6)%0A%20%20%20%20centres%20%3D%20(short_edges%5B%3A-1%5D%20%2B%20short_edges%5B1%3A%5D)%20%2F%202%0A%20%20%20%20for%20label%2C%20cc%2C%20offset%20in%20(%0A%20%20%20%20%20%20%20%20(%22Added%20windows%22%2C%20combined%2C%20-0.26)%2C%0A%20%20%20%20%20%20%20%20(%22Residuals%20resolved%22%2C%20recovered%2C%200.0)%2C%0A%20%20%20%20%20%20%20%20(%22Whole%20signal%22%2C%20whole%2C%200.26)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20counts%2C%20_%20%3D%20np.histogram(%0A%20%20%20%20%20%20%20%20%20%20%20%20cc.stress_range%2C%20bins%3Dshort_edges%2C%20weights%3Dcc.count_cycle%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20spectrum.bar(centres%20%2B%20offset%2C%20counts%2C%20width%3D0.25%2C%20label%3Dlabel)%0A%20%20%20%20%20%20%20%20damage%20%3D%20float(get_pm(cc%2C%20curve).sum())%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%20f%22%7Bdamage%3A.6e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Difference%20from%20whole%20(%25)%22%3A%20f%22%7B100%20*%20(damage%20%2F%20whole_damage%20-%201)%3A.2f%7D%22%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(xlabel%3D%22Stress%20range%20(MPa)%22%2C%20ylabel%3D%22Cycle%20count%20per%20bin%22)%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.Html(pd.DataFrame(summary).to_html(index%3DFalse%2C%20border%3D0))%2C%0A%20%20%20%20%20%20%20%20%5D%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
ab0394a8cf9111ab7b1f6afc17df59c9