forked from jvalue/made-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomated_datapipeline.py
231 lines (190 loc) · 9.01 KB
/
automated_datapipeline.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import pandas as pd
import numpy as np
import requests
import zipfile
import io
from sqlalchemy import create_engine
import os
def download_and_extract_in_memory(url):
"""
Downloads a ZIP file from the specified URL and extracts its contents into memory.
Parameters:
url (str): The URL of the ZIP file to be downloaded.
Returns:
dict: A dictionary where the keys are the names of the files within the ZIP archive,
and the values are in-memory file-like objects containing the file contents."""
response = requests.get(url)
with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
return {name: io.BytesIO(zip_ref.read(name)) for name in zip_ref.namelist()}
def load_and_clean_data(file_like, delimiter=','):
"""
Load data from a CSV file-like object into a pandas DataFrame and perform data cleaning.
Parameters:
file_like (file-like object): The file-like object containing the CSV data.
delimiter (str, optional): The delimiter used in the CSV file. Default is ','.
Returns:
pd.DataFrame: A cleaned DataFrame with no missing values.
"""
df = pd.read_csv(file_like, delimiter=delimiter)
df.dropna(inplace=True)
return df
def drop_irrelevant_columns(df, columns_to_drop):
"""
Removes specified columns from a pandas DataFrame.
Args:
df (pd.DataFrame): The Dataframe from which coulumns are to be removed.
columns_to_drop (list): list of strings represents the names of the columns to be removed.
Returns:
pd.DataFrame: Modified DataFrame with specified columns dropped.
"""
df.drop(columns=columns_to_drop, inplace=True)
return df
def rename_columns(df, columns_mapping):
"""
Rename columns in a DataFrame according to a provided mapping.
Args:
df (pd.DataFrame): The Dataframe whose columns are to be renamed
columns_mapping (dict): Dictionary mapping old column names to new column names.
Returns:
pd.DataFrame: DataFrame with columns renamed.
"""
df.rename(columns=columns_mapping, inplace=True)
return df
def drop_rows_with_zeros(df, columns):
"""
Drop rows from a DataFrame that contain zero values in specified columns.
Args:
df (pd.DataFrame): Input DataFrame.
columns (list): List of columns to check for zero values.
Returns:
pd.DataFrame: DataFrame with rows containing zero values in specified columns dropped.
"""
df = df.loc[~(df[columns] == 0).all(axis=1)]
return df
def create_quarterly_column(df):
"""
Creates a new column indicating the corresponding quarter for each month in a DataFrame.
Args:
df (pd.DataFrame): DataFrame containing a 'Month' column.
Returns:
pd.DataFrame: DataFrame with a new 'Quarter' column indicating the corresponding quarter.
"""
month_to_quarter = {
1: 'Q1', 2: 'Q1', 3: 'Q1',
4: 'Q2', 5: 'Q2', 6: 'Q2',
7: 'Q3', 8: 'Q3', 9: 'Q3',
10: 'Q4', 11: 'Q4', 12: 'Q4'
}
df['Quarter'] = df['Month'].map(month_to_quarter)
return df
def create_database(db_dir, db_name):
"""
Create a SQLite database in the specified directory.
Parameters:
db_dir (str): Directory path where the database will be created.
db_name (str): Name of the SQLite database.
Returns:
str: Path of the created SQLite database.
"""
db_path = os.path.join(db_dir, f"{db_name}.db")
if not os.path.exists(db_dir):
os.makedirs(db_dir)
return db_path
def transform_wildfire_data(df):
"""
Transforms Wildfire burned area data in a pandas DataFrame
This function performs several operations on the DataFrame to prepare this data for analysis:
1. Drop the uncessary columns.
2. Drops the zero values from the rows with specified columns.
3. Renames columns for consistency and clarity
4. Creating quaterly column.
5. Reorder the columns.
Args:
df (str): Path to the CSV file containing wildfire data.
Returns:
pd.DataFrame: Transformed wildfire DataFrame.
"""
wildfire_columns_to_drop = ['gid_1']
df = drop_irrelevant_columns(df, wildfire_columns_to_drop).copy()
columns_with_zeros = ["forest", "savannas", "shrublands_grasslands", "croplands", "other"]
df = drop_rows_with_zeros(df, columns_with_zeros).copy()
wildfire_columns_rename = {'year': 'Year',
'month':'Month',
'gid_0':'Country_Code',
'country':'Country_Name',
'region':'Region_Name',
'forest':'Forest_BA',
'savannas':'Savannas_BA',
'shrublands_grasslands':'Shrubs_Grasslands_BA',
'croplands':'Croplands_BA',
'other':'Other_BA'}
df = rename_columns(df, wildfire_columns_rename).copy()
df = create_quarterly_column(df).copy()
return df.reindex(columns=['Year','Month','Quarter','Country_Code','Country_Name',
'Region_Name','Forest_BA','Savannas_BA',
'Shrubs_Grasslands_BA', 'Croplands_BA',
'Other_BA'])
def transform_emissions_data(df):
"""
Transforms Emissions data in a pandas DataFrame
This function performs several operations on the DataFrame to prepare this data for analysis:
1. Drop the uncessary columns.
2. Drops the zero values from the rows with specified columns.
3. Renames columns for consistency and clarity
4. Creating quaterly column.
5. Reorder the columns.
Args:
df (str): Path to the CSV file containing emissions data.
Returns:
pd.DataFrame: Transformed emissions DataFrame.
"""
emissions_columns_to_drop = ['gid_1']
df = drop_irrelevant_columns(df, emissions_columns_to_drop).copy()
columns_with_zeros = ["CO2", "CO", "TPM", "PM25", "TPC", "NMHC", "OC", "CH4", "SO2", "BC", "NOx"]
df = drop_rows_with_zeros(df, columns_with_zeros).copy()
emissions_columns_rename = {'year':'Year',
'month':'Month',
'gid_0':'Country_Code' ,
'country':'Country_Name',
'region':'Region_Name'}
df = rename_columns(df, emissions_columns_rename).copy()
df = create_quarterly_column(df).copy()
return df.reindex(columns=['Year', 'Month', 'Quarter', 'Country_Code', 'Country_Name',
'Region_Name', 'CO2', 'CO', 'TPM', 'PM25', 'TPC', 'NMHC',
'OC', 'CH4', 'SO2', 'BC', 'NOx'])
def main():
"""
Main function to execute the data engineering automated pipeline. This function downloads,
extracts, transforms, and saves wildfire and emissions data.
Steps:
- Download and extract data.
- Transform wildfire and emissions data.
- Save the transformed data to a SQLite database.
Returns:
None
"""
wildfire_url = "https://effis-gwis-cms.s3.eu-west-1.amazonaws.com/apps/country.profile/MCD64A1_burned_area_full_dataset_2002-2023.zip"
emissions_url = "https://effis-gwis-cms.s3.eu-west-1.amazonaws.com/apps/country.profile/emission_gfed_full_2002_2023.zip"
wildfire_files = download_and_extract_in_memory(wildfire_url)
emissions_files = download_and_extract_in_memory(emissions_url)
#File paths for the extracted CSV files
wildfire_file_like = wildfire_files['MCD64A1_burned_area_full_dataset_2002-2023.csv']
emissions_file_like = emissions_files['emission_gfed_full_2002_2023.csv']
wildfire_df = load_and_clean_data(wildfire_file_like)
emissions_df = load_and_clean_data(emissions_file_like)
#Transform the wildfire and emissions data
wildfire_df = transform_wildfire_data(wildfire_df).copy()
emissions_df = transform_emissions_data(emissions_df).copy()
# Merge the wildfire and emission datasets to perform further analysis
merged_df = pd.merge(wildfire_df, emissions_df, how='inner')
#SQLite database path
db_dir = 'data'
db_name = 'wildfire_burnedarea_emissions_data'
db_path = create_database(db_dir, db_name)
engine = create_engine(f'sqlite:///{db_path}')
#Save the processed data to the database
wildfire_df.to_sql('wildfire_burnedarea_data', engine, index=False, if_exists='replace')
emissions_df.to_sql('wildfire_emissions_data', engine, index=False, if_exists='replace')
merged_df.to_sql('wildfire_merged_data', engine, index=False, if_exists='replace')
if __name__ == "__main__":
main()