%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%20a%20short%20stress%20history%20and%20inspect%20the%20full%20and%20half%20cycles.%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.%20Each%20half%20cycle%20has%0A%20%20%20%20%20%20%20%20weight%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%20_examples.charts%20import%20plot_views%0A%0A%20%20%20%20stress%20%3D%20np.array(%5B4%2C%207%2C%202%2C%2010%2C%205%2C%209%2C%203%2C%204%2C%202%2C%2012%5D%2C%20dtype%3Dfloat)%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%20signal.plot(np.arange(stress.size)%2C%20stress%2C%20%22o-%22)%0A%20%20%20%20signal.set(xlabel%3D%22Sample%22%2C%20ylabel%3D%22Stress%20(MPa)%22%2C%20title%3D%22Input%20history%22)%0A%0A%20%20%20%20for%20spectrum%2C%20label%2C%20marker%20in%20(%0A%20%20%20%20%20%20%20%20(cycles%2C%20%22From%20signal%22%2C%20%22o%22)%2C%0A%20%20%20%20%20%20%20%20(restored%2C%20%22From%20sparse%20dictionary%22%2C%20%22D%22)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20ranges%2C%20groups%20%3D%20np.unique(spectrum.stress_range%2C%20return_inverse%3DTrue)%0A%20%20%20%20%20%20%20%20counted.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20ranges%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20np.bincount(groups%2C%20weights%3Dspectrum.count_cycle)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20linestyle%3D%22none%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Dmarker%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3Dlabel%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20counted.set(xlabel%3D%22Stress%20range%20(MPa)%22%2C%20ylabel%3D%22Total%20cycle%20count%22)%0A%20%20%20%20counted.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.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%20edges%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%20centres%20%3D%20(edges%5B%3A-1%5D%20%2B%20edges%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%20counts%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%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%20counts%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
193adfcc5b2aa59df215dcbbe02ca8d4