|
14 | 14 | """ |
15 | 15 | import os |
16 | 16 | import logging |
| 17 | + |
17 | 18 | # `Keras` was hanging when trying to use the model.predict |
18 | 19 | # and swapping the backend from `Tensorflow` to `Theano` |
19 | 20 | # fixed the issue. |
|
22 | 23 | from cssi.config import read_cssi_config |
23 | 24 | from cssi.latency import Latency |
24 | 25 | from cssi.sentiment import Sentiment |
| 26 | +from cssi.questionnaire import SSQ |
| 27 | +from cssi.exceptions import CSSIException |
25 | 28 |
|
26 | 29 | logger = logging.getLogger(__name__) |
27 | 30 |
|
28 | 31 |
|
29 | 32 | class CSSI(object): |
| 33 | + """The main access point for the CSSI library""" |
30 | 34 |
|
31 | 35 | 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` |
32 | 47 | if config_file is None: |
33 | 48 | self.config_file = "config.cssi" |
34 | 49 | self.config_file = config_file |
| 50 | + # Sets the debug mode |
35 | 51 | self.debug = debug |
| 52 | + # Tries to read the config file. |
36 | 53 | self.config = read_cssi_config(filename=self.config_file) |
| 54 | + # Initialize the latency capturing module |
37 | 55 | self.latency = Latency(config=self.config, debug=self.debug, shape_predictor=shape_predictor) |
| 56 | + # Initializing the Sentiment capturing module |
38 | 57 | 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") |
40 | 114 |
|
| 115 | + # Calculating the CSSI score |
| 116 | + cssi = (tl * lw) + (ts * sw) + (tq * qw) + tot_ps |
41 | 117 |
|
| 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") |
42 | 121 |
|
| 122 | + return cssi |
0 commit comments