%22%22%22Example%20of%20rainflow%20counting.%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%20Rainflow%20counting%0A%0A%20%20%20%20%20%20%20%20Count%20120%20randomly%20generated%20integer%20stresses%20and%20inspect%20the%20full%0A%20%20%20%20%20%20%20%20and%20half%20cycles.%20The%20dashed%20trace%20joins%20the%20residual%20turning%20points%0A%20%20%20%20%20%20%20%20that%20remain%20after%20the%20closed%20cycles%20are%20removed.%0A%20%20%20%20%20%20%20%20Export%20the%20spectrum%20as%20a%20sparse%20dictionary%20with%20%60as_dict()%60%2C%20then%0A%20%20%20%20%20%20%20%20rebuild%20it%20with%20%60CycleCount.from_rainflow()%60.%20Compare%20the%20summed%0A%20%20%20%20%20%20%20%20counts%20by%20range%20before%20and%20after%20binning.%20The%20bars%20show%20the%20cycle%0A%20%20%20%20%20%20%20%20weight%20in%20each%20bin%20and%20the%20lines%20show%20the%20cumulative%20cycle%20weight.%0A%20%20%20%20%20%20%20%20Each%20half%20cycle%20contributes%200.5.%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%20calculate(mo)%3A%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%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%20time%20%3D%20get_sampled_time(fs%3D1%2C%20duration%3D120)%0A%20%20%20%20stress%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%20cycles%20%3D%20CycleCount.from_timeseries(%0A%20%20%20%20%20%20%20%20stress%2C%20unit%3D%22MPa%22%2C%20range_bin_width%3D1.0%2C%20mean_bin_width%3D1.0%0A%20%20%20%20)%0A%20%20%20%20exported%20%3D%20cycles.as_dict()%0A%20%20%20%20restored%20%3D%20CycleCount.from_rainflow(exported%2C%20unit%3Dcycles.unit)%0A%0A%20%20%20%20fig%2C%20(signal%2C%20counted)%20%3D%20plt.subplots(2%2C%201%2C%20layout%3D%22constrained%22)%0A%20%20%20%20_%2C%20residuals%2C%20indices%20%3D%20rainflow(stress)%0A%20%20%20%20signal.plot(time%2C%20stress%2C%20label%3D%22Full%20history%22)%0A%20%20%20%20signal.plot(time%5Bindices%5D%2C%20residuals%2C%20%22s--%22%2C%20label%3D%22Residuals%22)%0A%20%20%20%20signal.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Time%20(s)%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22Stress%20(MPa)%22%2C%0A%20%20%20%20%20%20%20%20title%3Df%22Input%20history%20%C2%B7%20%7Bstress.size%7D%20samples%22%2C%0A%20%20%20%20)%0A%20%20%20%20signal.legend()%0A%0A%20%20%20%20edges%20%3D%20np.arange(0%2C%2013)%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%20cumulative%20%3D%20counted.twinx()%0A%20%20%20%20for%20spectrum%2C%20label%2C%20offset%2C%20color%20in%20(%0A%20%20%20%20%20%20%20%20(cycles%2C%20%22Original%22%2C%20-0.2%2C%20%22C0%22)%2C%0A%20%20%20%20%20%20%20%20(restored%2C%20%22Reloaded%22%2C%200.2%2C%20%22C1%22)%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%20spectrum.stress_range%2C%20bins%3Dedges%2C%20weights%3Dspectrum.count_cycle%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20counted.bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20centres%20%2B%20offset%2C%20counts%2C%20width%3D0.36%2C%20color%3Dcolor%2C%20label%3Dlabel%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20cumulative.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20edges%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20np.r_%5B0%2C%20counts.cumsum()%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22--%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color%3Dcolor%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3Df%22Cumulative%20%C2%B7%20%7Blabel.lower()%7D%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20counted.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Stress%20range%20(MPa)%22%2C%20ylabel%3D%22Cycles%20per%20bin%22%2C%20ylim%3D(0%2C%20None)%0A%20%20%20%20)%0A%20%20%20%20cumulative.set(ylabel%3D%22Cumulative%20cycles%22%2C%20ylim%3D(0%2C%20None))%0A%20%20%20%20counted.legend(%0A%20%20%20%20%20%20%20%20handles%3D%5B*counted.containers%2C%20*cumulative.lines%5D%2C%20loc%3D%22upper%20left%22%0A%20%20%20%20)%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.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Bcycles.count_cycle.sum()%3Ag%7D**%20cycles%20from%20the%20signal%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Brestored.count_cycle.sum()%3Ag%7D**%20after%20reloading.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Binned%20cycles%20are%20represented%20by%20their%20bin%20centres.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.ui.table(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20cycles.to_df()%5B%5B%22stress_range%22%2C%20%22mean_stress%22%2C%20%22count_cycle%22%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%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%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%0A%0A%20%20%20%20peaks%20%3D%20synthetic_peaks()%0A%20%20%20%20waveform%20%3D%20tensile_waveform(peaks)%0A%20%20%20%20original%20%3D%20_CycleCount.from_timeseries(waveform)%0A%20%20%20%20long_restored%20%3D%20_CycleCount.from_rainflow(original.as_dict())%0A%20%20%20%20_edges%20%3D%20_np.histogram_bin_edges(%0A%20%20%20%20%20%20%20%20_np.r_%5Boriginal.stress_range%2C%20long_restored.stress_range%5D%2C%20bins%3D60%0A%20%20%20%20)%0A%20%20%20%20_centres%20%3D%20(_edges%5B%3A-1%5D%20%2B%20_edges%5B1%3A%5D)%20%2F%202%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(%22Rainflow%22%2C%20original)%2C%0A%20%20%20%20%20%20%20%20(%22After%20sparse%20reload%22%2C%20long_restored)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_counts%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%3D_edges%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20weights%3Dlong_spectrum.count_cycle%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20ax.plot(_centres%2C%20_counts%2C%20label%3Dlong_label)%0A%20%20%20%20ax.set(xlabel%3D%22Stress%20range%20(MPa)%22%2C%20ylabel%3D%22Counted%20cycles%20per%20bin%22)%0A%20%20%20%20ax.legend()%0A%20%20%20%20original_count%20%3D%20original.count_cycle.sum()%0A%20%20%20%20restored_count%20%3D%20long_restored.count_cycle.sum()%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%20**%7Bwaveform.size%3A%2C%7D%20samples**%20and%20repeat%20the%20round-trip.%20%22%0A%20%20%20%20%20%20%20%20f%22The%20original%20contains%20**%7Boriginal_count%3A%2Cg%7D%20cycles**%20%22%0A%20%20%20%20%20%20%20%20f%22and%20the%20reloaded%20spectrum%20contains%20**%7Brestored_count%3A%2Cg%7D**.%20%22%0A%20%20%20%20%20%20%20%20%22Compare%20the%20two%20distributions%20on%20the%20same%20display%20bins.%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
6927683bd41aae24f6acc3e82e764d7e