B
    }^ÆJ  ã               @   sÜ   d 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	m
Z
 ddlmZ ddlmZmZmZmZmZmZmZ ddlZddlmZ dd	lmZmZ dd
lmZ ddlZddlZddlZddl Z ddl!Z!G dd„ deƒZ"dS )uð  
Created on 12 Apr 2016

@author: Ã‰ric Piel

Gives ability to acquire a set of streams multiple times over time.

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

The software is provided "as is", without warranty of any kind,
express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and non-infringement.
In no event shall the authors be liable for any claim, damages or
other liability, whether in an action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use or
other dealings in the software.
é    )Údivision)ÚOrderedDictN)ÚmodelÚdataioÚacq)Ústream)ÚMonochromatorSettingsStreamÚARStreamÚSpectrumStreamÚUNDEFINED_ROIÚStaticStreamÚ
LiveStreamÚStream)Úget_acqui_conf)ÚPluginÚAcquisitionDialog)Úsplitextc                   sü   e Zd ZdZdZdZdZeddddœfd	d
ej	j
ifdddej	jdœfdej	jddœfdi ffƒZ‡ fdd„Zdd„ Zd8dd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd9d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd:d2d3„Zd4d5„ Zd6d7„ Z‡  ZS );ÚTimelapsePluginZ	Timelapsez2.0u
   Ã‰ric PielzPublic domainÚperiodzTime between each acquisitionÚlog)ÚtooltipZscaleÚnumberOfAcquisitionsÚcontrol_typeÚsemOnlyOnLastzSEM only on the lastz1Acquire SEM images only once, after the timelapse)Úlabelr   r   Úfilenamez=TIFF files (*.tiff, *tif)|*.tiff;*.tif|HDF5 Files (*.h5)|*.h5)r   ZwildcardÚexpectedDurationc                sÎ   t t| ƒ ||¡ |sd S tjddd| jd| _t dd¡| _t 	d¡| _
t d¡| _tjd	dd
d| _| j | j¡ | j | j¡ |jdkr tjj| jd d< d | _|  d| j¡ t ¡ | _g | _d | _d S )Né
   )gü©ñÒMbP?i'  Ús)ÚunitÚsetteréd   )é   i † Fza.h5é   T)r   Úreadonly)ZsecomZdelphir   r   zAcquisition/Timelapse...	Ctrl+T)Úsuperr   Ú__init__r   ZFloatContinuousÚ
_setPeriodr   ZIntContinuousr   Z	BooleanVAr   ZStringVAr   ZVigilantAttributer   Ú	subscribeÚ_update_exp_durÚroleÚodemisÚguiZCONTROL_CHECKÚvaconfÚ_dlgZaddMenuÚstartÚqueueÚQueueÚ	_to_storeÚ	_sthreadsÚ	_exporter)ÚselfZ
microscopeÚmain_app)Ú	__class__© úY/tmp/pip-install-m1v76eul/Odemis/Odemis-3.0.0.data/data/share/odemis/plugins/timelapse.pyr&   T   s$    


zTimelapsePlugin.__init__c             C   s&   t ƒ }tj |jdt d¡|jf ¡S )Nz%s%sz%Y%m%d-%H%M%S)r   ÚosÚpathÚjoinZ	last_pathÚtimeÚstrftimeZlast_extension)r5   Úconfr8   r8   r9   Ú_get_new_filenamep   s    z!TimelapsePlugin._get_new_filenameNc       	      C   sŽ   | j j}| jj}|  ¡ \}}t |¡}t d|t|ƒ¡ t	d|| ƒ}|| ||d   }|rt|t || ¡| 7 }| j
jt |¡dd dS )zN
        Called when VA that affects the expected duration is changed
        z*Estimating %g s acquisition for %d streamsr   r#   T)Zforce_writeN)r   Úvaluer   Ú_get_acq_streamsr   ÚestimateTimeÚloggingÚdebugÚlenÚmaxr   Z
_set_valueÚmathÚceil)	r5   Ú_ÚnbÚpÚssÚlast_ssÚsacqtÚintpÚdurr8   r8   r9   r)   w   s    
zTimelapsePlugin._update_exp_durc             C   sR   d}x4|   ¡ d D ]$}| ¡ }td|tj ƒ}||7 }qW tt||ƒ| jjd ƒS )Nr   gü©ñÒMbP?r#   )rB   ÚestimateAcquisitionTimerG   r   ÚSETUP_OVERHEADÚminr   Úrange)r5   r   Ztot_timer   Zacqtr8   r8   r9   r'   Š   s    zTimelapsePlugin._setPeriodc             C   sb   t |jjƒ}t|dƒr<y| |j¡ W n tk
r:   Y nX x |D ]}t|tƒrB| |¡ qBW |S )zF
        Return all the live streams present in the given tab
        Ú
spotStream)	ÚlistÚstreamsrA   ÚhasattrÚremoverV   Ú
ValueErrorÚ
isinstancer   )r5   Útab_datarM   r   r8   r8   r9   Ú_get_live_streams˜   s    


z!TimelapsePlugin._get_live_streamsc                sà   | j sg g fS | j j ¡ | j j ¡  ‰ t dtˆ ƒ¡ | jjj	j
j}t|dƒr¬|j}g }xV|D ]H}t|tjƒr”t‡ fdd„|jD ƒƒr¦| |¡ P q^|ˆ kr^| |¡ q^W nˆ }g }| jj
rØdd„ |D ƒ}dd„ |D ƒ}||fS )zô
        Return the streams that should be used for acquisition
        return:
           acq_st (list of streams): the streams to be acquired at every repetition
           last_st (list of streams): streams to be acquired at the end
        zView has %d streamsÚacquisitionStreamsc             3   s   | ]}|ˆ kV  qd S )Nr8   )Ú.0Úsubs)Úlive_str8   r9   ú	<genexpr>Á   s    z3TimelapsePlugin._get_acq_streams.<locals>.<genexpr>c             S   s   g | ]}t |tjƒr|‘qS r8   )r\   r   ÚEMStream)r`   r   r8   r8   r9   ú
<listcomp>Ì   s    z4TimelapsePlugin._get_acq_streams.<locals>.<listcomp>c             S   s   g | ]}t |tjƒs|‘qS r8   )r\   r   rd   )r`   r   r8   r8   r9   re   Í   s    )r.   ÚviewZ
getStreamsÚhidden_viewrD   rE   rF   r6   Ú	main_dataÚtabrA   Útab_data_modelrY   r_   r\   r   ZMultipleDetectorStreamÚanyrX   Úappendr   )r5   r]   Zacq_strM   ZacsrN   r8   )rb   r9   rB   ª   s,    



z TimelapsePlugin._get_acq_streamsc             C   sÚ  | j jjj}|jdkrFt | j jddtjtj	B ¡}| 
¡  | ¡  d S yD|jjjjtkrˆt | j jddtjtj	B ¡}| 
¡  | ¡  d S W n tk
rž   Y nX |j ¡  |  ¡ | j_t| ddƒ}|| _| | | j¡ |  |j¡}xD|D ]<}t|tttfƒrt d|¡ |j |d d qè|  |¡ qèW | !d	¡ |j!d
| j"dd | j#j| j#_|j$j%j&j'| j(dd |j)j%j&j'| j(dd | 
¡ }|dkršt d¡ n"|dkr°t d¡ nt *d|¡ |j$j%j& +| j(¡ | ¡  d S )N)Z
secom_liveZsparc_acquiz?Timelapse acquisition must be done from the acquisition stream.z"Timelapse acquisition not possiblez+You need to select a region of acquisition.zTimelapse acquisitionzœThe same streams will be acquired multiple times, defined by the 'number of acquisitions'.
The time separating each acquisition is defined by the 'period'.
z>Not showing stream %s, for which the live image is not spatial)ÚindexZCancelZAcquireZblue)Zface_colourT)Úinitr   zAcquisition cancelledr#   zAcquisition completedzGot unknown return code %s),r6   rh   ri   rA   ÚnameÚwxZMessageDialogZ
main_frameÚOKZ	ICON_STOPZ	ShowModalZDestroyrj   Z	semStreamZroir   ÚAttributeErrorÚstreambar_controllerÚpauseStreamsr@   r   r   r.   ZaddSettingsr-   r^   r\   r	   r
   r   rD   ÚinfoZ	addStreamZ	addButtonÚacquirer   rf   Zstream_treeZflatr(   r)   rg   ÚwarningZunsubscribe)r5   ri   ZboxÚdlgrM   r   Zansr8   r8   r9   r/   Ñ   sV    







zTimelapsePlugin.startc          
   C   sŒ   zxyTxN| j  ¡ \}}|d kr*| j  ¡  d S t d||¡ | j ||¡ | j  ¡  qW W n tk
rt   t d¡ Y nX W d t 	d|¡ X d S )NzSaving data %s in thread %dzFailure in the saving threadzSaving thread %d done)
r2   ÚgetÚ	task_donerD   ru   r4   ÚexportÚ	ExceptionÚ	exceptionrE   )r5   ÚiÚfnÚdasr8   r8   r9   Ú_saving_thread  s    
zTimelapsePlugin._saving_threadé   c             C   sT   | j rt d¡ |  ¡  x6t|ƒD ]*}tj| j|fd}| ¡  | j  	|¡ q"W dS )z1
        n (int >= 1): number of threads
        z:The previous saving threads were not stopped, stopping now)ÚtargetÚargsN)
r3   rD   rw   Ú_stop_saving_threadsrU   Ú	threadingÚThreadr   r/   rl   )r5   Únr~   Útr8   r8   r9   Ú_start_saving_threads'  s    
z%TimelapsePlugin._start_saving_threadsc             C   sH   x| j D ]}| j d¡ qW | j ¡  x| j D ]}| ¡  q.W g | _ dS )zi
        Blocks until all the data has been stored
        Can be called multiple times in a row
        )NNN)r3   r2   Úputr<   )r5   rJ   r‰   r8   r8   r9   r…   4  s    
z$TimelapsePlugin._stop_saving_threadsc             C   s   | j  ||f¡ dS )zO
        Queue the requested DataArrays to be stored in the given file
        N)r2   r‹   )r5   r   r€   r8   r8   r9   Ú
_save_dataC  s    zTimelapsePlugin._save_datac       
      C   sÜ   | j j}|jjj}| ¡ }| ¡  |  d¡ |  ¡ \}}t	 
|¡}| jj}| jj}	zht|ƒdkrªt|d tƒrª|	dkrª|dk rª||tj k rªt d¡ |  ||d |¡ n|  |||¡ W d |  ¡  X t d¡ | ¡  d S )Nr‚   r#   r   r"   é   z9Fast timelapse detected, will acquire as fast as possiblezClosing dialog)r6   rh   ri   rA   rs   rt   ZpauseSettingsrŠ   rB   r   rC   r   r   rF   r\   r   r   rS   rD   ru   Ú_fast_acquire_oneÚ_acquire_multir…   rE   ÚClose)
r5   rx   rh   Zstr_ctrlZstream_pausedrM   rN   rO   rL   rK   r8   r8   r9   rv   I  s&    





zTimelapsePlugin.acquirec          
   C   sT  | j j}| jj}t |¡| _t|ƒ\}}|d | }t ¡ | _	t
 ¡ }	| j|	_||	_|rr|d8 }t |g| ¡}
nd}
|  ||	|||
¡ z<|	 ¡  | |	¡ d|j_| j	 ¡  |	 ¡ rÂ| ¡  dS W dd|j_t d|¡ |  |¡ X |r>t d¡ |g| }|	jt ¡ t |¡ d	 t |¡ ¡ \}}|  ||f |¡ |   ¡  |	 !d¡ dS )
a  
        Acquires one stream, *as fast as possible* (ie, the period is not used).
        Only works with LiveStreams (and not with MDStreams)
        st (LiveStream)
        last_ss (list of Streams): all the streams to be acquire on the last time
        z-%.5dr#   r   TNFzRestoring stream %sz0Acquiring last acquisition, with all the streams)Úend)"r   rA   r   r   Úfind_fittest_converterr4   r   r†   ÚEventÚ_acq_completedr   ÚProgressiveFutureÚ_cancel_fast_acquireÚtask_cancellerÚ_streamr   rC   Ú_hijack_live_streamÚset_running_or_notify_cancelÚshowProgressÚ	is_activeÚwaitÚ	cancelledÚresumeSettingsrD   rE   Ú_restore_live_streamÚset_progressr=   rv   ÚresultrŒ   r…   Ú
set_result)r5   rx   ÚstrN   rK   r   ÚbsÚextÚfn_patÚfÚ	extra_durrM   r€   Úer8   r8   r9   rŽ   j  sB    




z!TimelapsePlugin._fast_acquire_onec             C   s   d|j j_| j ¡  dS )NFT)r˜   rœ   rA   r”   Úset)r5   r¨   r8   r8   r9   r–   ¢  s    

z$TimelapsePlugin._cancel_fast_acquirer   c                sP   ˆj ˆ_dd„ ˆ_ dˆ_ˆ ¡ tj ‰ ‡ ‡‡‡‡‡‡fdd„}ˆjˆ_|ˆ_d S )Nc               S   s   d S )Nr8   r8   r8   r8   r9   Ú<lambda>©  ó    z5TimelapsePlugin._hijack_live_stream.<locals>.<lambda>r   c                 s¦   ˆj } ˆ j d7  _ t d| ¡ ˆj ˆkrHt d¡ dˆj_ˆj ¡  nˆj ˆkr`t d¡ d S ˆ ˆ| f ˆjd g¡ ˆ|  }ˆ | ˆ }ˆj	t
 
¡ | d d S )Nr#   zReceived data %dzStopping the streamFzSkipping extra datar   )r‘   )Ú_data_receivedrD   rE   rœ   rA   r”   r«   rŒ   Úrawr¡   r=   )r~   ÚleftrQ   )Údur_oner©   r¨   r§   rK   r5   r¤   r8   r9   Ústore_raw_data¯  s    



z;TimelapsePlugin._hijack_live_stream.<locals>.store_raw_data)Ú_shouldUpdateHistogramÚ_old_shouldUpdateHistogramr®   rR   r   rS   Ú_shouldUpdateImageÚ_old_shouldUpdateImage)r5   r¤   r¨   rK   r§   r©   r²   r8   )r±   r©   r¨   r§   rK   r5   r¤   r9   r™   §  s    
z#TimelapsePlugin._hijack_live_streamc             C   s   |j |_|` |j|_|`d S )N)r¶   rµ   r´   r³   )r5   r¤   r8   r8   r9   r    Æ  s    z$TimelapsePlugin._restore_live_streamc             C   s†  | j j}| jj}| jj}t |¡| _t|ƒ\}}|d | }	t 	|¡}
t
d||
 ƒ}||
k rjt d|
|¡ t ¡ }dd„ |_| ¡  | |¡ xàt|ƒD ]Ô}|| }|
| ||d   }|dkrÞ|rÞ||7 }|t 	|¡|
 7 }t ¡ }|j|| d t |¡ ¡ \}}| ¡ r| ¡  d S |  |	|f |¡ |dkr˜|| t ¡  }|dkr^t |¡ q˜t d| ¡ q˜W |  ¡  | d ¡ d S )	Nz-%.5dr   zLAcquisition will take %g s, but period between acquisition must be only %g sc             S   s   dS )NTr8   )Úlr8   r8   r9   r¬   à  r­   z0TimelapsePlugin._acquire_multi.<locals>.<lambda>r#   )r‘   z0Immediately starting next acquisition, %g s late)r   rA   r   r   r   r’   r4   r   r   rC   rG   rD   rw   r   r•   r—   rš   r›   rU   r=   r¡   rv   r¢   rž   rŸ   rŒ   Úsleepru   r…   r£   )r5   rx   rM   rN   rL   rK   r   r¥   r¦   r§   rO   rP   r¨   r~   r°   rQ   Zstarttr€   rª   Zsleeptr8   r8   r9   r   Ì  sH    




zTimelapsePlugin._acquire_multi)N)r‚   )r   ) Ú__name__Ú
__module__Ú__qualname__ro   Ú__version__Ú
__author__Ú__license__r   r+   r,   ZCONTROL_INTZCONTROL_NONEZCONTROL_SAVE_FILEr-   r&   r@   r)   r'   r^   rB   r/   r   rŠ   r…   rŒ   rv   rŽ   r–   r™   r    r   Ú__classcell__r8   r8   )r7   r9   r   5   sF   
'G
!8
r   )#Ú__doc__Ú
__future__r   Úcollectionsr   rD   rH   r+   r   r   r   Z
odemis.acqr   Zodemis.acq.streamr   r	   r
   r   r   r   r   Z
odemis.guiZodemis.gui.confr   Zodemis.gui.pluginr   r   Zodemis.util.dataior   r:   r0   r†   r=   rp   r   r8   r8   r8   r9   Ú<module>   s"   $