forked from menouarazib/icme-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (101 loc) · 4.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import errno
from datetime import timedelta
import numpy as np
from utils.utils import delimit_data, resampling_data, \
add_extra_features, plot_similarity_prediction, predict_all, get_module_logger, plot_events_icme, \
validate_time_format, get_or_download_data_set, convert_events_to_np_array
from utils.gautierfunctions.process import turn_peaks_to_clouds
import logging.config
import sys
import os
PLOT = False
"""
Plot results or not
"""
DEBUG = False
"""Run in debug mode without passing arguments to the module and using directly START_TIME_DEBUG and STOP_TIME_DEBUG
defined just below """
START_TIME_DEBUG = "2012-7-2T20:30:00"
"""
The start time to use in order to debug, feel free to modify it
"""
STOP_TIME_DEBUG = "2012-7-20T23:59:00"
"""
The stop time to use in order to debug, feel free to modify it
"""
DELTA_TIME = 0
"""
Add a time to the start/stop times, by default is zero
"""
def main():
"""
Returns the predicted ICME events for a given start and stop times,
then saves them into csv file named 'icme_events.csv' and into
image named 'icme_events.png'
if you are in the debug mode (DEBUG == True) then you need to specify the start/stop times manually (see
START_TIME_DEBUG and STOP_TIME_DEBUG), if you are in the production mode (DEBUG == False) then you need to pass
start/stop times as arguments (see readme.md)
if you need to plot the results you need to assign True to PLOT
:return: the predicted ICME events
"""
logger.info("Checking arguments... ")
nb_arguments = len(sys.argv)
destination_folder_path = ""
if not DEBUG and nb_arguments < 3:
message = "X X : Expected at least 3 arguments. You give only: ", nb_arguments, *sys.argv
logger.error(message)
sys.exit(errno.EINVAL)
elif not DEBUG and nb_arguments == 4:
message = "Destination folder is given: ", sys.argv[3]
logger.info(message)
destination_folder_path = sys.argv[3]
elif not DEBUG and nb_arguments == 3:
message = "Destination folder is missing, the output files will be saved in the current folder"
logger.warn(message)
if not DEBUG:
message = "Checking the formats of start time and stop time..."
logger.info(message)
try:
start = validate_time_format(sys.argv[1])
stop = validate_time_format(sys.argv[2])
except ValueError as error:
logger.error('Caught this error: ' + repr(error))
sys.exit(errno.EINVAL)
else:
start = validate_time_format(START_TIME_DEBUG)
stop = validate_time_format(STOP_TIME_DEBUG)
logger.info("Arguments are valid")
if start >= stop:
message = "X X : The start time must be less than the stop time: ", start, stop
logger.error(message)
sys.exit(errno.EINVAL)
logger.info("Preparing data... ")
raw_data = get_or_download_data_set()
add_extra_features(raw_data)
data = delimit_data(raw_data)
data = resampling_data(data)
prediction_start_time = start - timedelta(minutes=DELTA_TIME)
prediction_stop_time = stop + timedelta(minutes=DELTA_TIME)
data_prediction = data[data.index < prediction_stop_time]
data_prediction = data_prediction[data_prediction.index > prediction_start_time]
logger.info("Run predictions... ")
prediction = predict_all(data_prediction)
logger.info("Post processing... ")
integral = prediction.sum(axis=1)
list_icmes = turn_peaks_to_clouds(integral, 12)
path_save = os.path.join(destination_folder_path, "similarity_output.png")
plot_similarity_prediction(prediction, path_save, plot=PLOT)
path_save = os.path.join(destination_folder_path, "events_output.png")
plot_events_icme(integral, list_icmes, path_save, PLOT)
logger.info("Writing results... ")
events_catalog = convert_events_to_np_array(list_icmes)
path_save = os.path.join(destination_folder_path, "events_tt_output.csv")
np.savetxt(path_save, events_catalog, delimiter=" ", fmt='%s')
message = "List of events %s ", *events_catalog
logger.info(message)
logger.info("End")
if __name__ == '__main__':
logger = get_module_logger(__name__)
# run predictions
main()
logging.shutdown()