a
    L9›fç%  ã                   @   s€   d dl Zd dlZd dlmZ d dlmZ d dl	Z
d dl Zd dlmZmZ d dlZd dlmZ d dlmZ dZG dd„ deƒZdS )	é    N)ÚHydraHeadApp)ÚStepsÚStep)Úchain)ÚSequencyz{:,.2f}c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚWalshAppÚ c                 K   s   | j  |¡ || _d S )N)Ú__dict__ÚupdateÚtitle)Úselfr   Úkwargs© r   úF/Users/putuwistika/Documents/TRILAK/hydralit-example/apps/walsh_app.pyÚ__init__   s    zWalshApp.__init__c               
   C   sÎ  zxt  d¡ t  d¡ t jddd tjdddgdd	}|  |d
d¡ t  d¡ t  d¡ t j	j
dddddd}t j	j
dddddd}t j	 dd¡}tƒ }tj ddd¡}tƒ jttdt|ƒƒƒ|d}|j| ¡ ||d}|j| ¡ ||d}	tƒ j| ¡ |d}
tƒ j| ¡ |	d}|jdd}|j|dd d!d" |
j|d#d d$d%d& | ¡  t j| ¡ dd' t  d(¡ t  d)¡ tj|ddd*}|jt d+d,d¡d-}|j| ¡ ||d}|j| ¡ ||d}tƒ j|jdd.|d}tƒ j|jdd.|d}|jdd/d0}|j|d1d2d3 |j|d1d4d3 | ¡  t j| ¡ dd' t  d5¡ t  d6¡ tj d,d7d8\}\}}}|j!d9d: tƒ }| "| ¡ ¡\}}}| #d;¡ | $d<¡ | %d=¡ | &||¡ | ¡ j'd> }| (| ¡ d tj) | ¡\}}| #d?¡ | $d@¡ | %d=¡ | &||¡ | (| *¡ d tj) | ¡\}}| #dA¡ | $d@¡ | %d=¡ | &|dd … |dd … ¡ t j|dd' W nN t+yÈ } z4t j,dBddC t  -dD¡ t  -dE .|¡¡ W Y d }~n
d }~0 0 d S )FNzSequency Denoisinga‡  Source for this quick tutorial on using Sequency methods for denoising step change data can be found in the [Hotstepper documentation](https://hotstepper.readthedocs.io/notebooks/sequency_quickstart.html). An example of how easy it is to convert an existing application and use within a Hydralit multi-page application, see the secret saurce [here] (https://github.com/TangleSpace/hydralit).z<br><br>T)Úunsafe_allow_htmlzZhttps://raw.githubusercontent.com/TangleSpace/hotstepper-data/master/data/vessel_queue.csvZenterZleave)Úparse_datesÚdayfirstzdata.csvzDownload Example Dataa<  
                Another typical use case of Fourier transforms is to remove high frequency noise from a single by decomposing it into constituent frequency components and setting frequencies below a threshold value to zero.                 With Sequency analysis, we have a similar functionality, except since we have step data and we are wanting to retain the nature of the step data (as steps) instead of just smoothing away the details,                 we can use the denoise method of the Sequency module to remove the higher sequency number components. An explicit example is shown below, here for now, we pass in the direct steps data for denoising,                 apply a strength parameter to determine how many of the high sequency components are set to zero and then we construct a new Steps object.
                a¥  
denoise_step_changes_strong = seq.denoise(rand_steps.step_changes(),denoise_strength=0.7,denoise_mode='range')
denoise_step_changes = seq.denoise(rand_steps.step_changes(),denoise_strength=0.2)

rand_steps_denoise_strong = Steps().add_direct(data_start=rand_steps.step_keys(),data_weight = denoise_step_changes_strong)
rand_steps_denoise = Steps().add_direct(data_start=rand_steps.step_keys(),data_weight = denoise_step_changes)

ax = rand_steps.plot(label='Original Data')
rand_steps_denoise.plot(ax=ax,color='g',linewidth=2,label='Light Denoise');
rand_steps_denoise_strong.plot(ax=ax,color='r',linewidth=2,linestyle='-',label='Strong Denoise')
ax.legend();
                zStrong Denoise Strengthgffffffæ?g{®Gáz„?g      ð?)ÚvalueÚ	min_valueÚ	max_valueÚstepzLight Denoise Strengthgš™™™™™É?zDenoise Mode)Úranger   iöÿÿÿé   éd   é   )Z
data_startZdata_weight)Zdenoise_strengthZdenoise_modezOriginal Data)ÚlabelÚgé   zLight Denoise)ÚaxÚcolorÚ	linewidthr   ÚrÚ-zStrong Denoise)r   r    r!   Z	linestyler   )Úclear_figureuO  
                As another quick example, we can apply the same technique to one of the HotStepper sample datasets, for this tutorial, weâ€™ll only look at the first two months of data in order to better highlight what the sequency denoising can do to help better show the trend or typical step behaviour of the data.
                aQ  
df_vq_samp = pd.read_csv(r'https://raw.githubusercontent.com/TangleSpace/hotstepper-data/master/data/vessel_queue.csv',parse_dates=['enter','leave'],dayfirst=True)

vq_samp = Steps.read_dataframe(df_vq_samp,start='enter',end='leave')
vq_clip = vq_samp.clip(ubound=pd.Timestamp(2020,3,1))

dn = seq.denoise(vq_clip.step_changes(),denoise_mode='range')
vq_clip_dn = Steps().add_direct(data_start=vq_clip.step_keys(convert_keys=True),data_weight=dn)

ax = vq_clip.plot(label='Original Data',figsize=(14,6))
vq_clip_dn.plot(ax=ax,linewidth=3,label='Sequency Denoise')
ax.legend()
                )ÚstartÚendiä  é   )Zubound)Zconvert_keys)é   é   )r   Úfigsizeg      ø?zStrong Sequency Denoise)r   r!   r   zLight Sequency Denoisea’  
                As the last item, we can take a look at the sequency spectrum for the vessel queue data. This dataset has a large number of changes and therefore the sequency range goes quite high, however it does show a number of peaks that are significantly larger than the others, indicating a number of distinct and repeating step change patterns within the data over this period.
                aÌ  
fig, (ax,ax2,ax3) = plt.subplots(nrows=3,figsize=(12,8))
fig.tight_layout(h_pad=4)

# Sequency object to use for analysis
vq_clip_seq = Sequency()
n,sp,l = vq_clip_seq.sequency_spectrum(vq_clip.step_changes())


ax.set_title('Step Change Sequency Spectrum')
ax.set_xlabel('Sequency Number')
ax.set_ylabel('Amplitude')
ax.stem(n,sp)

# number of data points, needed to create the sampling frequency
no_points = vq_clip.step_changes().shape[0]

fr,fsp = vq_clip_seq.frequency_spectrum(vq_clip.step_changes(),2*np.pi*no_points)

ax2.set_title('Step Change Frequency Spectrum')
ax2.set_xlabel('Frequency')
ax2.set_ylabel('Amplitude')
ax2.stem(fr,fsp)

# FFT the steps values instead of the delta changes to see the difference in the spectrum.
frv,fspv = vq_clip_seq.frequency_spectrum(vq_clip.step_values(),2*np.pi*no_points)

ax3.set_title('Steps Value Frequency Spectrum')
ax3.set_xlabel('Frequency')
ax3.set_ylabel('Amplitude')
ax3.stem(frv[1:],fspv[1:]);
                )r   é   )Únrowsr*   é   )Zh_padzStep Change Sequency SpectrumzSequency NumberZ	Amplituder   zStep Change Frequency SpectrumÚ	FrequencyzSteps Value Frequency Spectrumz./resources/failure.png)ÚwidthzhAn error has occurred, someone will be punished for your inconvenience, we humbly request you try again.zError details: {})/Ústr   Ú	subheaderÚmarkdownÚpdÚread_csvÚdownload_buttonÚwriteÚcodeÚsidebarÚsliderÚradior   ÚnpÚrandomÚrandintr   Z
add_directÚlistr   ÚlenZdenoiseZstep_changesZ	step_keysÚplotÚlegendÚpyplotZ
get_figureZread_dataframeÚclipÚ	TimestampÚpltÚsubplotsZtight_layoutZsequency_spectrumZ	set_titleZ
set_xlabelZ
set_ylabelÚstemÚshapeZfrequency_spectrumÚpiZstep_valuesÚ	ExceptionÚimageÚerrorÚformat) r   Z
df_vq_sampZdenoise_power_strong_selectZdenoise_power_light_selectZdenoise_mode_selectÚseqZsteps_changesZ
rand_stepsZdenoise_step_changes_strongZdenoise_step_changesZrand_steps_denoise_strongZrand_steps_denoiser   Zvq_sampZvq_clipZ	dn_strongZdn_lightZvq_clip_dn_strongZvq_clip_dn_lightÚfigÚax2Zax3Zvq_clip_seqÚnÚspÚlZ	no_pointsÚfrZfspZfrvZfspvÚer   r   r   Úrun   s”    

ÿ	ÿÿÿÿÿ$









zWalshApp.runN)r   )Ú__name__Ú
__module__Ú__qualname__r   rV   r   r   r   r   r      s   
r   )Úpandasr3   Ú	streamlitr0   Úhydralitr   Úmatplotlib.pyplotrB   rE   Únumpyr;   Z
hotstepperr   r   ZhsÚ	itertoolsr   r   ZSIG_FIGURE_FMTr   r   r   r   r   Ú<module>   s   