Skip to content
Snippets Groups Projects
Commit 72929ee8 authored by suazo2's avatar suazo2
Browse files

Upload New File

parent d0b99554
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:9ef9c1b3 tags:
``` python
import boto3
from botocore.config import Config
import pandas as pd
import matplotlib as mp
import matplotlib.pyplot as plt
```
%% Cell type:code id:d7385261 tags:
``` python
ACCESS_KEY = 'AKIA5II7XXI23QL6ICS5'
SECRET_KEY = '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'
my_config = Config(
region_name = 'us-east-1',
retries = {
'max_attempts': 10,
'mode': 'standard'
}
)
```
%% Cell type:code id:f1ab6dae tags:
``` python
# create IoT Analytics client
client = boto3.client('iotanalytics',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
config=my_config
)
```
%% Cell type:code id:f2bcc76c tags:
``` python
print(client.list_datasets()['datasetSummaries'])
print(client.list_channels()['channelSummaries'])
```
%% Output
[{'datasetName': 'emission_ds', 'status': 'ACTIVE', 'creationTime': datetime.datetime(2023, 4, 2, 23, 11, 0, 5000, tzinfo=tzlocal()), 'lastUpdateTime': datetime.datetime(2023, 4, 3, 2, 38, 32, 191000, tzinfo=tzlocal()), 'triggers': [{'schedule': {'expression': 'cron(10/5 * * * ? *)'}}], 'actions': [{'actionName': 'action_11', 'actionType': 'QUERY'}]}]
[{'channelName': 'emissionchannel', 'channelStorage': {'serviceManagedS3': {}}, 'status': 'ACTIVE', 'creationTime': datetime.datetime(2023, 4, 2, 22, 59, 6, 629000, tzinfo=tzlocal()), 'lastUpdateTime': datetime.datetime(2023, 4, 2, 22, 59, 6, 629000, tzinfo=tzlocal()), 'lastMessageArrivalTime': datetime.datetime(2023, 4, 3, 2, 56, 23, 350000, tzinfo=tzlocal())}]
%% Cell type:code id:e31db523 tags:
``` python
# Load dataset
dataset_name = "emission_ds"
dataset = client.get_dataset_content(datasetName=dataset_name)
# get pandas data frame
df = pd.read_csv(dataset['entries'][0]['dataURI'])
```
%% Cell type:code id:e1c4a0a3 tags:
``` python
df
```
%% Output
device_id max_co2 timestep_time __dt
0 device_0 12353.41 118.0 2023-04-03 00:00:00.000
1 device_3 9638.66 120.0 2023-04-03 00:00:00.000
2 device_2 11307.68 119.0 2023-04-03 00:00:00.000
3 device_1 18960.98 119.0 2023-04-03 00:00:00.000
4 device_4 19466.87 120.0 2023-04-03 00:00:00.000
.. ... ... ... ...
793 device_4 15216.78 19.0 2023-04-03 00:00:00.000
794 device_1 9460.68 18.0 2023-04-03 00:00:00.000
795 device_0 9818.41 17.0 2023-04-03 00:00:00.000
796 device_2 11307.68 18.0 2023-04-03 00:00:00.000
797 device_0 9818.41 18.0 2023-04-03 00:00:00.000
[798 rows x 4 columns]
%% Cell type:code id:756b529a tags:
``` python
# Bar graph max emission per vehicle
max_emission = df.groupby('device_id')['max_co2'].max()
max_emission.plot.bar(x='device_id', y='max_co2', rot=0)
```
%% Output
<AxesSubplot: xlabel='device_id'>
%% Cell type:code id:2b50f3d0 tags:
``` python
vehicle_0 = df[df['device_id'] == "device_0"]
vehicle_0 = vehicle_0.sort_values(by=['timestep_time'])
vehicle_1 = df[df['device_id'] == "device_1"]
vehicle_1 = vehicle_1.sort_values(by=['timestep_time'])
vehicle_2 = df[df['device_id'] == "device_2"]
vehicle_2 = vehicle_2.sort_values(by=['timestep_time'])
vehicle_3 = df[df['device_id'] == "device_3"]
vehicle_3 = vehicle_3.sort_values(by=['timestep_time'])
vehicle_4 = df[df['device_id'] == "device_4"]
vehicle_4 = vehicle_4.sort_values(by=['timestep_time'])
fig, axes = plt.subplots(nrows=3, ncols=2)
vehicle_0.plot(x="timestep_time", y=["max_co2"], ax=axes[0,0])
vehicle_1.plot(x="timestep_time", y=["max_co2"], ax=axes[0,1])
vehicle_2.plot(x="timestep_time", y=["max_co2"], ax=axes[1,0])
vehicle_3.plot(x="timestep_time", y=["max_co2"], ax=axes[1,1])
vehicle_4.plot(x="timestep_time", y=["max_co2"], ax=axes[2,0])
```
%% Output
<AxesSubplot: xlabel='timestep_time'>
%% Cell type:code id:c5e15ebc tags:
``` python
# Average emission over time (All 5 vehicles)
df_mean = df.groupby('timestep_time')['max_co2'].mean()
df_mean.plot(x="timestep_time", y=["max_co2"])
```
%% Output
<AxesSubplot: xlabel='timestep_time'>
%% Cell type:code id:fe3e0b2b tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment