
    UagF                         d dl Z d dlZd dlZd dlZd dlZd dlZda	da
dadad Zd Zd Zd Zd Zd Zd	 Zd
 Zdeej,                  ej,                  f   fdZd Zd Zd Zy)    N c                 <   	 t        d|  d|        | |d}t        dd      5 }t        j                  ||       d d d        t	        j
                  |        y # 1 sw Y   xY w# t        $ r-}t        d|        t	        j
                  |        Y d }~y d }~ww xY w)NzError z: )	errorCodeerrorMessageoutput.jsonwzFailed to log error: )printopenjsondumpsysexit	Exception)
error_codeerror_message
error_dataoutfilees        Z/var/www/dev.workforce-alpha.com/api/wfa-api/forecast-prediction-helpers/predict_helper.pyexitWithErrorr      s    zl"]O45 $)

 -% 	+IIj'*	+ 			+ 	+  %aS)*s-   "A% AA% A"A% %	B.#BBc            	         t        d       t        t        j                        dk7  r t        d       t        j                  d       t        j                  d   } 	 t        j                  |        t               }d|vrt        dd       |d   }|j                  d       d}t        |      }t        ||      \  }}d|v r|d   }nd }|j                  dd        d|v r|d   }	nd}	|j                  dd        d|vrd}
n|d   }
|j                  dd        |	rt!        j"                  |d   dz         |d<   ||||||||	|
f	S # t        $ r( t        d|  d       t        j                  d       Y t        $ r( t        d|  d       t        j                  d	       Y 0t        $ r;}t        d
t        |       d|  d       t        j                  d       Y d }~nd }~ww xY w)Nz"--Initializing prediction module--   z0Error: Missing argument: Path to temp directory.   zError: Directory  does not exist.z-Error: Permission denied to access directory .   An unexpected error occurred: z while accessing the directory    resultId   z'Error: 'resultId' parameter is missing.r   cutoff_minimumlog_transformFanomaly_threshold_n_stddevg      @y)r	   lenr   argvr   oschdirFileNotFoundErrorPermissionErrorr   strpredict_loadConfigr   poppredict_getRegressorspredict_loadDatanplog)input_directoryr   configr   output_data_file
regressorshistData
futureDatar!   r"   r#   s              r   predict_initializer8   (   s   	
./
388}@AhhqkO

!  !FaBCj!H
JJz$&v.J+FJ?Hj6! 01
JJ& & /
JJ% (61%(" &,,H%I"
JJ+T2 x}q01 8-z8ZQ_an  qK  K  Ke  !/!22BCD =o=NaPQ .s1vh6UVeUffghis$   E -G$2,G$!G$)0GG$c            	         t        d       d} 	 t        |       5 }|j                         }d d d        	 t        j                        }|S # 1 sw Y   !xY w# t        $ r t	        dd|  d       Y ?t
        $ r t	        dd|  d       Y Yt        $ r&}t	        d	d
t        |       d|  d       Y d }~d }~ww xY w# t        $ r t	        dd       Y S t        $ r$}t	        dd
t        |       d       Y d }~S d }~ww xY w)Nz--Loading config--zconfig.json
   zFile r      z&Permission denied to open config file r      r   z* when opening and loading the config file    z5Could not decode JSON from the specified config file.   z* while decoding the specified config file.)r	   r
   readr)   r   r*   r   r+   r   loads
ValueError)input_config_filefsinput_config_json_stringr   r3   s        r   r,   r,   i   s2   	
%C#$ 	1')wwy$	1o45 M!	1 	1 GbE"3!44DEF YbBCTBUUVWX Cb:3q6(Blm~l  @A  B  	C  	CC
  TbQS M  ob:3q6(BlmnnMosV   A AA B= AA B:2B:B:B55B:=DDC<<Dc                     t        d       d| vrt        dd       	 | d   }| j                  d       t        d       |D ]  }t        d|         |S # t        $ r t        dd       Y y t        $ r}t        dd	|        Y d }~y d }~ww xY w)
Nz--getRegressors--r5      z,Error: 'regressors' key not found in config.zRegressors loaded:z        Runtime error: )r	   r   r-   KeyErrorr   )r3   r5   	regressorr   s       r   r.   r.      s    	
6!bHI1L)


< "## 	$IByk"#	$  JbHI 1bOA3/001s   7A B,B4BBc                    |D ]  }|d   }||dk  r|d   }t        d| d| d       t        j                  t        |       t              }| j
                  | |   dk(     j                         }t        t        |            D ]R  }||   dz   }|dz   t        |      k  r||dz      n
t        |       }	t        ||	|z
        }
t        d|
dz         ||||
z    T || d| <    t        d	       t        | j                  d
             y )NnumDaysSincer   namez.Generating 'days since' column for regressor: , with a maximum of  days.dtypeds_zEFirst 10 rows of the dataframe after generating 'days since' columns:r:   )
r	   r0   zerosr%   intindextolistrangeminhead)dfr5   rK   rM   regressorNamerecords_since_1ones_indicesi	start_idxend_idxlengths              r   #predict_generateRegressorsDaysSincerc      s5    4	 0<!#3!&)>}oMabnaoouvw ((3r7#6 xx= 1Q 67>>@ s<() 	QA$Q!+I-.US5F-Fl1q5)CPRGGw':;F<A!VaZ<POIi&&89		Q %4S !144 

QR	"''"+    c                    |D ]  }|d   }||dk  r|d   }t        d| d| d       t        j                  t        |       t              }| j
                  | |   dk(     j                         }t        t        |            D ]+  }||   }t        d||z
        }	||	z
  }
t        |
dd	      ||	| - || d
| <    t        d       t        | j                  d             y )NnumDaysBeforer   rN   z/Generating 'days before' column for regressor: rO   rP   rQ   r   db_zFFirst 10 rows of the dataframe after generating 'days before' columns:r:   )
r	   r0   rT   r%   rU   rV   rW   rX   maxrZ   )r[   r5   rK   rf   r\   records_before_1r^   r_   ra   r`   rb   s              r   $predict_generateRegressorsDaysBeforerk      s    5	!/2 MA$5!&)?Nbcpbqqwxy 88CG37 xx= 1Q 67>>@ s<() 	GA"1oGAw67Iy(F2722FYw/		G %5S !154 

RS	"''"+rd   c                 
   	 g }g }| j                   dd  }|D ]Z  }| |   j                         j                         dkD  s(|j                  |       |D ]  }|d   |k(  s|j                  |        Z \ |r| j	                  |d       |D cg c]	  }||vs| c}|d d  t        d|       t        d|       y c c}w # t        $ r}t        dd	| d
       Y d }~y d }~wt        $ r}	t        dd|	        Y d }	~	y d }	~	ww xY w)Nr   r   rN   T)columnsinplacez Columns dropped from data frame:zRegressors dropped:   z
KeyError: z( - The specified column might not exist.   rI   )	rm   isnasumappenddropr	   rJ   r   r   )
r[   r5   columns_to_dropregressors_to_removecolumns_to_checkcolumnrK   rker   s
             r   #predict_dropRegressorsWithNaNValuesr{      s%   1 !::ab> ' 	F&z $$&*&&v.!+ I (F2,33I>		 GGOTG: %/Pq!;O2OP
1 	0/B#%9:	 Q  UbJrd*RSTT 1bOA3/001sF   <B? B? /B? 	B:B:B? :B? ?	DCD)C==Dc                    t        d| j                         t        d       t        | j                  d             d| j                  v r1| j	                  ddgd       t        d| j                  d	           n/| j	                  dgd       t        d
| j                  d	           t        d       t        j                  | d   dd      | d<   | d   j                         j                         }|d	kD  r#t        d| d       | j	                  dgd       d| j                  v rft        j                  | d   d      | d<   | d   j                         j                         }|d	kD  r#t        d| d       | j	                  dgd       t        d| j                         t        d| j                         j                         j                                 y )NzInitial dataframe shape:zFirst 10 rows of the dataframe:r:   r$   dsT)subsetrn   z3Rows after dropping missing values in 'ds' or 'y': r   z,Rows after dropping missing values in 'ds': z*Converting 'ds' column to datetime format.%Y-%m-%d %H:%M:%Scoerce)formaterrorsz	Warning: z[ values could not be converted to datetime and were set to NaT. These rows will be removed.)r   z> non-numeric values in 'y' column. These rows will be removed.z.Final dataframe shape after all preprocessing:z/Total missing values in the cleaned dataframe: )
r	   shaperZ   rm   dropnapdto_datetimerq   rr   
to_numeric)r[   	nat_countnumeric_y_nan_counts      r   predict_prepareDataframer      s   	
$bhh/	
+,	"''"+ bjj
		$d	3CBHHQK=QR
		$	.<RXXa[MJK 

67~~bh/B8TBtH 4##%I1}	)$  A  	B
		$	. bjj--393 glln002"I122pqrIIcUDI1 

:BHHE	;BGGIMMO<O<O<Q;R
STrd   returnc                 p   d| vrt        dd       | d   a| j                  d       d| vrt        dd       | d   a| j                  d       d| vrt        dd	       | d   a| j                  d       d
| vrt        dd       | d
   a| j                  d
       d| vrt        dd       | d   }| j                  d       d| vrt        dd       | d   }| j                  d       	 t        j                  j                  t        t        t        t
              }	 t        d       d| }t        j                  |      }t        |       t!        ||       t#        ||       t%        ||       |j&                  rt        dd       t        d       d| }t        j                  ||      }t        |       t!        ||       t#        ||       t%        ||       |j&                  rt        dd       ||fS # t        j                  j                  $ r#}t        ddt        |              Y d }~d }~wt        $ r$}t        ddt        |       d       Y d }~Ed }~ww xY w# t(        $ r}	t        d d!|	        Y d }	~	fS d }	~	wt*        $ r}
t        d"d#|
        Y d }
~
fS d }
~
wt,        $ r}t        d$d%|        Y d }~fS d }~wt        j                  j                  $ r%}t        d&d't        |              Y d }~fS d }~wt        $ r}t        d(d| d)       Y d }~fS d }~ww xY w)*Ndb_host2   z&Error: 'db_host' parameter is missing.db_user3   z&Error: 'db_user' parameter is missing.db_password4   z*Error: 'db_password' parameter is missing.db_name5   z&Error: 'db_name' parameter is missing.histViewName6   z+Error: 'histViewName' parameter is missing.futureViewName7   z-Error: 'futureViewName' parameter is missing.hostuserpassworddatabase8   *Error: Could not connect to the database: 9   r   z" while connecting to the database.z--load histData--zSELECT * FROM :   z$Error: The historical data is empty.z--load futureData--;   z Error: The future data is empty.<   zError: a key was not found - =   zError: Value error occurred - >   zError: Index error occurred - ?   z.Error: Could not read data from the database: @   z& while reading data from the database.)r   _dbHostr-   _dbUser_dbPassword_dbNamemysql	connectorconnectErrorr+   r   r	   r   read_sqlr   r{   rc   rk   emptyrJ   rA   
IndexError)r3   r5   r   r   connr   SQLr6   r7   rz   veies               r   r/   r/   !  sQ    bBCYG
JJybBCYG
JJyF"bFG'K
JJ}bBCYG
JJyV#bGH.)L
JJ~v%bIJ,-N
JJ 
g&& 	 ' 
f!"|n-;;sD) *+HjA+HjA,XzB>>"DE#$~./[[d+
 ,+J
C+J
C,ZD"@A ZM ??   QbFs1vhOPP gb:3q6(Bdeffg2  @b9">?? Z  Ab:2$?@@ Z  Ab:2$?@@ Z ??   UbJ3q6(STT Z  fb:1#=cdeeZfsm   &3G8 CI& 8I#H33I#?II#&	L5/JL5J))L55K L5,LL5L00L5c           	         t        d| d       |D cg c]  }|d   	 }}t        d       |D ]  t                t        d       t        d       |j                  D ]  |vst                t        d       	 t        j                  j	                  t
        t        t        t              }|j                         }|j                         D ]  \  }}	|	d   }
g }|D ]2  |	   }t        fd	|D        d       }|j                  | ||
||f       4 d
}|dj                  dgt        |      z        z  }|D cg c]  }|D ]  }|  }}}|j                  ||        |j!                          y c c}w c c}}w # t        j                  j"                  $ r}t%        dd|        Y d }~y d }~wt&        $ r}t%        dd| d       Y d }~y d }~ww xY w)Nz	--Saving z
 results--r   zColumns to be saved:r   zColumns not saved:r   r}   c              3   :   K   | ]  }|d    k(  s|d     yw)r   r   N ).0itemcols     r   	<genexpr>z&predict_saveResults.<locals>.<genexpr>  s!     !Zd4PQ7VY>$q'!Zs   
zIINSERT INTO prediction_results (result_id, result_type, ds, k, v) VALUES z, z(%s, %s, %s, %s, %s)F   r   G   r   z while saving the results)r	   rm   r   r   r   r   r   r   r   cursoriterrowsnextrs   joinr%   executecommitr   r   r   )r   result_typer[   columns_to_insertr   column_namesr   r   _rowds_valuevalues_to_insertvalue	target_idinsert_querysublistflattened_valuesr   r   s                     @r   predict_saveResultsr   s  s   	Ik]*
-.(9:DG:L:	
 ! c
	"I	
zz l"#J 
"I(Y&& 	 ' 
  kkm 	;FAs4yH  "# ]C !Z6G!Z\`a	 '';)UZ([\] gLDII'=&>EUAV&VWWL
 5EYQXYYYY
 NN<)9:-	;2 	a ;P  Z ??   LbFqcJKK Yb:1#=VWXXYs<   E:B?F E?'F ?F G"F66GGGc	                    | rt        ||||||       t        t        t        f      rHF|!|D ]  }	||	   j	                  fd      ||	<    |!|D ]  }	||	   j	                  fd      ||	<    d }
|||d   ||d      z
  }t        j                  t        j                  |            t        j                  |d         z  }
t        d|
       |j                         }||z  }t        |      |kD  j                  t              |d<   |!|d   j                  j                  d      |d<   |!|d   j                  j                  d      |d<   |
S )	Nc                     t        |       S Nri   xr!   s    r   <lambda>z.predict_processAfterForecast.<locals>.<lambda>  s    c.RS>T rd   c                     t        |       S r   r   r   s    r   r   z.predict_processAfterForecast.<locals>.<lambda>  s    N\]H^ rd   r$   r   zwMAPE: anomalyr}   r   )predict_undoLogTransformation
isinstancerU   floatapplyr0   rr   absr	   stdastypedtstrftime)r"   r!   r#   forecastforecast_columnsforecast_pastforecast_past_columnsr6   histDataColumnsr   wMAPE	residualsstddev	thresholds    `            r   predict_processAfterForecastr     s   %h0@-Qfhp  sB  	C .3,/N4N' V ( 3 34T UV$, `%23%7%=%=>^%_c"` E 9SMM2G2J$KK	 rvvi()BFF8C=,AAi  /7	 %(	NY$>#F#Fs#Ki !$**334GH +D144==>QRdLrd   c                     | %|D ]   }t        j                  | |         dz
  | |<   " |%|D ]   }t        j                  ||         dz
  ||<   " |&|D ]   }t        j                  ||         dz
  ||<   " y y )Nr   )r0   exp)r   r   r   r   r6   r   r   s          r   r   r     s     # 	6CFF8C=1A5HSM	6  ( 	@C!#c(:!;a!?M#	@ " 	6CFF8C=1A5HSM	6 rd   )r   r   r'   pandasr   mysql.connectorr   numpyr0   r   r   r   r   r   r8   r,   r.   rc   rk   r{   r   tuple	DataFramer/   r   r   r   r   rd   r   <module>r      s    
  	   
 

.=KB41,@@1F!UJN E",,2L,M N d8Yx%P6rd   