-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_data_analysis.py
152 lines (133 loc) · 5.41 KB
/
weather_data_analysis.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pandas as pd
import numpy as np
import sys
import glob
import os
import matplotlib.pyplot as plt
import re
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from PIL import Image
from sklearn.pipeline import make_pipeline
from sklearn.decomposition import PCA
weather_data = sys.argv[1]
image_data = sys.argv[2]
path = r'{0}'.format(weather_data)
path_im= r'{0}'.format(image_data)
def filename_to_datetime(s):
pattern = r'^\D*(\d*).jpg'
prog = re.compile(pattern)
string = prog.match(s)
return string[1]
#https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe
def file_to_df(path):
all_files = glob.glob(os.path.join(path, "*.csv"))
df_from_each_file = (pd.read_csv(f, skiprows = 16) for f in all_files)
data = pd.concat(df_from_each_file, ignore_index=True)
return data
def clean_weather_data(data, images):
clean_data = data
clean_data = clean_data.drop(['Hmdx'], axis = 1)
clean_data['Date/Time'] = pd.to_datetime(clean_data['Date/Time'], infer_datetime_format = True, format='%Y%m%d%h')
clean_data['Time'] = clean_data['Date/Time'].map(lambda x: x.hour)
clean_data = clean_data.drop(['Data Quality'], axis = 1)
clean_data = clean_data.rename(index=str, columns={'Temp (°C)': 'Temp',
'Dew Point Temp (°C)': 'Dew Point Temp',
'Wind Dir (10s def)' : 'Wind Dir',
'Time': 'Hour'
})
clean_data = clean_data.dropna(axis = 1, how = 'all')
clean_data = images.join(clean_data.set_index('Date/Time'), on= 'Date/Time')
clean_data = clean_data.dropna()
clean_data['Weather'] = clean_data['Weather'].apply(classify_weather, args = [r'.*(Cloudy)'])
clean_data['Weather'] = clean_data['Weather'].apply(classify_weather, args = [r'.*(Clear)'])
clean_data['Weather'] = clean_data['Weather'].apply(classify_weather, args = [r'.*(Rain|Snow|Fog)'])
# clean_data['Weather'] = clean_data['Weather'].apply(classify_rain, args = [r'(Drizzle|Thunderstorms)'])
clean_data = clean_data.dropna(axis=1, how='all')
return clean_data
def annual_data(data, year):
return data[data['Year'] == year]
def process_images(path):
all_images = glob.glob(os.path.join(path, '*.jpg'))
image_list = pd.DataFrame({'filename': all_images})
image_list['Date/Time'] = image_list['filename'].apply(filename_to_datetime)
image_list['Date/Time'] = pd.to_datetime(image_list['Date/Time'], infer_datetime_format = True, format='%Y%m%d%h')
return image_list
def classify_rain(data, weather):
compiler = re.compile(weather)
string = compiler.search(data)
if string != None:
return 'Rain'
else:
return data
def classify_weather(data, weather):
compiler = re.compile(weather)
string = compiler.search(data)
if string != None:
return string[1]
else:
return data
def scatter_plot(x, y, color):
plt.figure(figsize=(12, 4))
plt.plot(x, y, 'b.', alpha=0.2)
plt.show()
#https://stackoverflow.com/questions/45896800/how-to-convert-image-to-dataset-to-process-machine-learning
def reshape_to_2d(file_list):
X= []
for file in file_list:
im=Image.open(file)
imarr=np.array(im)
flatim=imarr.flatten()
X.append(flatim)
return X
def fit_model(X, y):
model = make_pipeline(StandardScaler(), PCA(75), SVC(kernel='linear', C=2.0))
model.fit(X, y)
return model
data = file_to_df(path)
images = process_images(path_im)
clean_data = clean_weather_data(data, images)
#check how many labels there are
#unique = list(clean_data['Weather'].unique())
file_list = list(clean_data['filename'])
X = reshape_to_2d(file_list)
y = clean_data['Weather']
y_hour = clean_data['Hour']
y_month = clean_data['Month']
jpg_check = r'^.*(\.jpg)'
checker = re.compile(jpg_check)
test_input = sys.argv[3]
#model = make_pipeline(StandardScaler(), PCA(75), SVC(kernel='linear', C=2.0))
if test_input == 'test':
X_train, X_test, y_train, y_test = train_test_split(X, y)
model = fit_model(X_train, y_train)
print('Weather score:')
print(model.score(X_test, y_test))
df = pd.DataFrame({'True': y_test, 'Predicted': model.predict(X_test)})
df.to_csv('test')
X_train, X_test, y_train, y_test = train_test_split(X, y_hour)
model = fit_model(X_train, y_train)
print('Hour score:')
print(model.score(X_test, y_test))
X_train, X_test, y_train, y_test = train_test_split(X, y_month)
model = fit_model(X_train, y_train)
print('Month score:')
print(model.score(X_test, y_test))
else:
if checker.match(test_input) != None:
input_data = Image.open(test_input)
imarr=np.array(input_data)
flatim=imarr.flatten()
input_t = []
input_t.append(flatim)
else:
image_list = process_images(test_input)
input_t = reshape_to_2d(image_list)
model_w = fit_model(X, y)
model_h = fit_model(X, y_hour)
model_m = fit_model(X, y_month)
df = pd.DataFrame({'p_month':model_m.predict(input_t),
'p_hour': model_h.predict(input_t),
'p_weather': model_w.predict(input_t)})
df.to_csv('results.csv')