Skip to content

Commit

Permalink
Merge pull request #35 from sdevenes/issue_21/enforce_pep
Browse files Browse the repository at this point in the history
Issue 21/enforce pep
  • Loading branch information
sdevenes authored Oct 7, 2020
2 parents 57312e3 + bdf8175 commit 30ea2df
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 207 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ script:
# Build documentation
- sphinx-build docs/source docs/build
- touch docs/build/.nojekyll
# Black lint checking
- black --check scripts/ tests/
after_success:
# update coveralls report
- coveralls
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
:target: https://sdevenes.github.io/M05_MiniProject/index.html
.. image:: https://img.shields.io/badge/github-project-0000c0.svg
:target: https://github.com/sdevenes/M05_MiniProject
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/black

===============================================================
Human Activity Recognition from Continuous Ambient Sensor Data
Expand Down
1 change: 1 addition & 0 deletions build-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ sphinx_rtd_theme
nose
coverage
coveralls
black
28 changes: 14 additions & 14 deletions scripts/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import numpy as np

import logging

logger = logging.getLogger()


class Model:
def __init__(self, nb_tree_per_forest=50, max_depth=10):
"""Create a new ML model (Random forest classifier from scikitlearn)
def __init__(self, nb_tree_per_forest=50, max_depth=10):
"""Create a new ML model (Random forest classifier from scikitlearn)
Args:
nb_tree_per_forest: number of decision trees in the forest
Expand All @@ -18,13 +19,13 @@ def __init__(self, nb_tree_per_forest=50, max_depth=10):
Raises:
None
"""
# Create a random forest model
self.model = RandomForestClassifier(n_estimators=nb_tree_per_forest, max_depth=max_depth,
random_state=0)

# Create a random forest model
self.model = RandomForestClassifier(
n_estimators=nb_tree_per_forest, max_depth=max_depth, random_state=0
)

def train(self, X, y):
"""Train the model using the given data
def train(self, X, y):
"""Train the model using the given data
Args:
X (numpy.ndarray):A NxM 2D-array where each row corresponds to a sample and each column to a feature
Expand All @@ -34,11 +35,10 @@ def train(self, X, y):
Raises:
None
"""
self.model.fit(X, y)

self.model.fit(X, y)

def predict(self, X):
"""Make a prediction on the data using the trained model
def predict(self, X):
"""Make a prediction on the data using the trained model
Args:
X (numpy.ndarray):A NxM 2D-array where each row corresponds to a sample and each column to a feature
Expand All @@ -49,6 +49,6 @@ def predict(self, X):
Raises:
None
"""
prediction = self.model.predict(X)
prediction = self.model.predict(X)

return prediction
return prediction
25 changes: 15 additions & 10 deletions scripts/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import itertools


def get_confusion_matrix(prediction_label, true_label):
"""Get the confusion matrix given the predicted and true labels
Expand All @@ -16,7 +17,10 @@ def get_confusion_matrix(prediction_label, true_label):
"""
return confusion_matrix(true_label, prediction_label)

def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', file_name="graph"):

def plot_confusion_matrix(
cm, classes, normalize=False, title="Confusion matrix", file_name="graph"
):
"""Generate a plot of the given confusion matrix using plotly express
Args:
Expand All @@ -35,13 +39,14 @@ def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix'
cm = cm / np.sum(cm)

# Create confusion matrix graph
fig = px.imshow(cm,
labels=dict(x="Predicted label", y="True label", color="value"),
x=classes,
y=classes,
color_continuous_scale='Blues',
title=title
)
#fig.show()
fig = px.imshow(
cm,
labels=dict(x="Predicted label", y="True label", color="value"),
x=classes,
y=classes,
color_continuous_scale="Blues",
title=title,
)
# fig.show()
# Export graph
fig.write_html("{}.html".format(file_name))
fig.write_html("{}.html".format(file_name))
2 changes: 1 addition & 1 deletion scripts/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data_path = '../data/csh101/csh101.ann.features.csv'
data_path = "../data/csh101/csh101.ann.features.csv"
nb_trees_experiment = {"nb_trees": (1, 2), "tree_depth": 10}
tree_depth_experiment = {"nb_trees": 10, "tree_depth": (1, 2)}
181 changes: 93 additions & 88 deletions scripts/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,91 @@
from sklearn.model_selection import train_test_split

PROTOCOLS = {
'proto1': {'train': 0.8, 'test': 0.2, 'random': 1},
'proto2': {'train': 0.8, 'test': 0.2, 'random': 2},
"proto1": {"train": 0.8, "test": 0.2, "random": 1},
"proto2": {"train": 0.8, "test": 0.2, "random": 2},
}

SUBSETS = [
'train',
'validation',
'test'
]
SUBSETS = ["train", "validation", "test"]

CLASSES = [
'Other_Activity',
'Watch_TV',
'Sleep_Out_Of_Bed',
'Bathe',
'Cook_Breakfast',
'Dress',
'Toilet',
'Personal_Hygiene',
'Sleep',
'Read',
'Relax',
'Cook_Dinner',
'Drink',
'Eat_Breakfast',
'Morning_Meds',
'Evening_Meds',
'Wash_Breakfast_Dishes',
'Cook_Lunch',
'Wash_Dishes',
'Leave_Home',
'Cook',
'Enter_Home',
'Entertain_Guests',
'Wash_Dinner_Dishes',
'Phone',
'Groom',
'Step_Out',
'Eat_Dinner',
'Eat_Lunch',
'Wash_Lunch_Dishes',
'Bed_Toilet_Transition',
'Eat',
'Go_To_Sleep',
'Wake_Up',
'Work_At_Table'
"Other_Activity",
"Watch_TV",
"Sleep_Out_Of_Bed",
"Bathe",
"Cook_Breakfast",
"Dress",
"Toilet",
"Personal_Hygiene",
"Sleep",
"Read",
"Relax",
"Cook_Dinner",
"Drink",
"Eat_Breakfast",
"Morning_Meds",
"Evening_Meds",
"Wash_Breakfast_Dishes",
"Cook_Lunch",
"Wash_Dishes",
"Leave_Home",
"Cook",
"Enter_Home",
"Entertain_Guests",
"Wash_Dinner_Dishes",
"Phone",
"Groom",
"Step_Out",
"Eat_Dinner",
"Eat_Lunch",
"Wash_Lunch_Dishes",
"Bed_Toilet_Transition",
"Eat",
"Go_To_Sleep",
"Wake_Up",
"Work_At_Table",
]

VARIABLES = [
'lastSensorEventHours',
'lastSensorEventSeconds',
'lastSensorDayOfWeek',
'windowDuration',
'timeSinceLastSensorEvent',
'prevDominantSensor1',
'prevDominantSensor2',
'lastSensorID',
'lastSensorLocation',
'lastMotionLocation',
'complexity',
'activityChange',
'areaTransitions',
'numDistinctSensors',
'sensorCount-Bathroom',
'sensorCount-Bedroom',
'sensorCount-Chair',
'sensorCount-DiningRoom',
'sensorCount-Hall',
'sensorCount-Ignore',
'sensorCount-Kitchen',
'sensorCount-LivingRoom',
'sensorCount-Office',
'sensorCount-OutsideDoor',
'sensorCount-WorkArea',
'sensorElTime-Bathroom',
'sensorElTime-Bedroom',
'sensorElTime-Chair',
'sensorElTime-DiningRoom',
'sensorElTime-Hall',
'sensorElTime-Ignore',
'sensorElTime-Kitchen',
'sensorElTime-LivingRoom',
'sensorElTime-Office',
'sensorElTime-OutsideDoor',
'sensorElTime-WorkArea'
"lastSensorEventHours",
"lastSensorEventSeconds",
"lastSensorDayOfWeek",
"windowDuration",
"timeSinceLastSensorEvent",
"prevDominantSensor1",
"prevDominantSensor2",
"lastSensorID",
"lastSensorLocation",
"lastMotionLocation",
"complexity",
"activityChange",
"areaTransitions",
"numDistinctSensors",
"sensorCount-Bathroom",
"sensorCount-Bedroom",
"sensorCount-Chair",
"sensorCount-DiningRoom",
"sensorCount-Hall",
"sensorCount-Ignore",
"sensorCount-Kitchen",
"sensorCount-LivingRoom",
"sensorCount-Office",
"sensorCount-OutsideDoor",
"sensorCount-WorkArea",
"sensorElTime-Bathroom",
"sensorElTime-Bedroom",
"sensorElTime-Chair",
"sensorElTime-DiningRoom",
"sensorElTime-Hall",
"sensorElTime-Ignore",
"sensorElTime-Kitchen",
"sensorElTime-LivingRoom",
"sensorElTime-Office",
"sensorElTime-OutsideDoor",
"sensorElTime-WorkArea",
]


def load(filepath='./data/csh101/csh101.ann.features.csv'):
def load(filepath="./data/csh101/csh101.ann.features.csv"):
"""Loads the dataset
Args:
Expand All @@ -105,8 +101,8 @@ def load(filepath='./data/csh101/csh101.ann.features.csv'):
"""
x = []
y = []
with open(filepath, 'rt') as f:
reader = csv.reader(f, delimiter=',')
with open(filepath, "rt") as f:
reader = csv.reader(f, delimiter=",")
for k, row in enumerate(reader):
if not k:
continue
Expand All @@ -129,16 +125,25 @@ def split_data(x, y, subset, splits):
Raises:
None
"""
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=splits['test'],
train_size=splits['train'],
random_state=splits['random'],
stratify=y)
(x_split, y_split) = (x_train, y_train) if subset == 'train' else (x_test, y_test)
x_train, x_test, y_train, y_test = train_test_split(
x,
y,
test_size=splits["test"],
train_size=splits["train"],
random_state=splits["random"],
stratify=y,
)
(x_split, y_split) = (x_train, y_train) if subset == "train" else (x_test, y_test)
return x_split, y_split


def get(protocol, subset, classes=CLASSES, variables=VARIABLES, filepath='./data/csh101/csh101.ann.features.csv'):
def get(
protocol,
subset,
classes=CLASSES,
variables=VARIABLES,
filepath="./data/csh101/csh101.ann.features.csv",
):
"""Get the desired subset
Args:
Expand Down
Loading

0 comments on commit 30ea2df

Please sign in to comment.