Skip to content

Commit c653429

Browse files
authored
Merge pull request #8 from brionmario/develop
stable version 2019-05-03
2 parents b68daa5 + 192862c commit c653429

14 files changed

+2600
-256
lines changed

cssi/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def read_from_file(self, filename):
8181
('latency_boundary', 'latency:latency_boundary', 'float'),
8282

8383
# [sentiment]
84-
('sentiment', 'sentiment:sentiment_weight', 'float'),
84+
('sentiment_weight', 'sentiment:sentiment_weight', 'float'),
8585

8686
# [questionnaire]
87-
('questionnaire', 'questionnaire:questionnaire_weight', 'float'),
87+
('questionnaire_weight', 'questionnaire:questionnaire_weight', 'float'),
8888
]
8989

9090
def _set_config_attribute_from_option(self, parser, attr, where, type_=''):

cssi/contributors.py renamed to cssi/contributor_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def __init__(self, config, debug=False):
1414
self.debug = debug
1515

1616
@abstractmethod
17-
def generate_score(self, *args):
17+
def generate_final_score(self, *args):
1818
""""""
1919
pass

cssi/core.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515
import os
1616
import logging
17+
1718
# `Keras` was hanging when trying to use the model.predict
1819
# and swapping the backend from `Tensorflow` to `Theano`
1920
# fixed the issue.
@@ -22,21 +23,100 @@
2223
from cssi.config import read_cssi_config
2324
from cssi.latency import Latency
2425
from cssi.sentiment import Sentiment
26+
from cssi.questionnaire import SSQ
27+
from cssi.exceptions import CSSIException
2528

2629
logger = logging.getLogger(__name__)
2730

2831

2932
class CSSI(object):
33+
"""The main access point for the CSSI library"""
3034

3135
def __init__(self, shape_predictor, debug=False, config_file=None):
36+
"""Initializes all the core modules in the CSSI Library.
37+
38+
Args:
39+
shape_predictor (str): Path to the landmark detector.
40+
debug (bool): Boolean indicating if debug mode should be activated or not.
41+
config_file (str): A file containing all the configurations for CSSI.
42+
43+
Examples:
44+
>>> cssi = CSSI(shape_predictor="hmd_face_landmarks.dat", debug=True, config_file="config.cssi")
45+
"""
46+
# If no config file name is passed in, defaults to `config.cssi`
3247
if config_file is None:
3348
self.config_file = "config.cssi"
3449
self.config_file = config_file
50+
# Sets the debug mode
3551
self.debug = debug
52+
# Tries to read the config file.
3653
self.config = read_cssi_config(filename=self.config_file)
54+
# Initialize the latency capturing module
3755
self.latency = Latency(config=self.config, debug=self.debug, shape_predictor=shape_predictor)
56+
# Initializing the Sentiment capturing module
3857
self.sentiment = Sentiment(config=self.config, debug=self.debug)
39-
logger.debug("Initialized CSSI library")
58+
# Initializing the questionnaire module.
59+
self.questionnaire = SSQ(config=self.config, debug=self.debug)
60+
logger.debug("CSSI library initialized......")
61+
62+
def generate_cssi_score(self, tl, ts, tq, plugin_scores=None):
63+
"""Generators the final CSSI score.
64+
65+
This is the core function of the CSSI library and it takes in the scores and
66+
generates the final CSSI score for the test session.
67+
68+
Args:
69+
tl (float): Total latency score
70+
ts (float): Total sentiment score
71+
tq (float): Total questionnaire score
72+
plugin_scores (list): A list of dictionaries containing plugin details.
73+
ex: [{"name": "heartrate.plugin", "score": 40.00}].
74+
Returns:
75+
float: The CSSI score.
76+
Raises:
77+
CSSIException: If the calculations couldn't be completed successfully
78+
this exception will be thrown.
79+
Examples:
80+
>>> cssi.generate_cssi_score(tl, ts, tq, plugin_scores)
81+
"""
82+
tot_ps = 0.0 # Variable to store the sum of the plugin scores
83+
tot_pw = 0 # Variable to keep track total plugin weight
84+
85+
# Checks if any plugins are provided for score calculation.
86+
if plugin_scores is not None:
87+
for plugin in plugin_scores:
88+
plugin_name = plugin["name"]
89+
# Checks if the plugin is registered in the configuration file
90+
# If not, raises an exception.
91+
if plugin_name not in self.config.plugins:
92+
raise CSSIException("The plugin {0} appears to be invalid.".format(plugin_name))
93+
else:
94+
plugin_weight = float(self.config.plugin_options[plugin_name]["weight"]) / 100
95+
plugin_score = plugin["score"]
96+
97+
# Checks if the passed in plugin score is less than 100.
98+
# If not an exception will be thrown.
99+
if plugin_score > 100:
100+
raise CSSIException("Invalid score provided for the plugin: {0}.".format(plugin_name))
101+
102+
# Ads the current plugin score to the total plugin score.
103+
tot_ps += plugin_score * plugin_weight
104+
# Ads the current plugin weight to the total plugin weight percentage.
105+
tot_pw += plugin_weight
106+
107+
lw = float(self.config.latency_weight) / 100 # latency weight percentage
108+
sw = float(self.config.sentiment_weight) / 100 # sentiment weight percentage
109+
qw = float(self.config.questionnaire_weight) / 100 # questionnaire weight percentage
110+
111+
# Checks if the total weight is less than 100 percent.
112+
if (lw + sw + qw + tot_pw) > 1:
113+
raise CSSIException("Invalid weight configuration. Please reconfigure and try again")
40114

115+
# Calculating the CSSI score
116+
cssi = (tl * lw) + (ts * sw) + (tq * qw) + tot_ps
41117

118+
# Double checks if the generated CSSI score is less than 100.
119+
if cssi > 100:
120+
raise CSSIException("Invalid CSSI score was generated. Please try again")
42121

122+
return cssi

0 commit comments

Comments
 (0)