From 453b9f32574ffabc90e857ec0616a3d1f6b7e85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 28 Feb 2017 04:01:05 +0100 Subject: [PATCH] Fixed bugs in Ekman2VAD --- senpy/extensions.py | 3 +- senpy/plugins/conversion/emotion/ekman2vad.py | 16 ++++--- .../conversion/emotion/ekman2vad.senpy | 4 +- setup.cfg | 4 +- tests/test_extensions.py | 43 ++++++++++++++++++- tests/test_models.py | 4 -- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/senpy/extensions.py b/senpy/extensions.py index b36a57b..861dc89 100644 --- a/senpy/extensions.py +++ b/senpy/extensions.py @@ -13,6 +13,7 @@ from .api import API_PARAMS, NIF_PARAMS, parse_params from threading import Thread import os +import copy import fnmatch import inspect import sys @@ -180,7 +181,7 @@ class Senpy(object): newentries = [] for i in resp.entries: if output == "full": - newemotions = i.emotions.copy() + newemotions = copy.copy(i.emotions) else: newemotions = [] for j in i.emotions: diff --git a/senpy/plugins/conversion/emotion/ekman2vad.py b/senpy/plugins/conversion/emotion/ekman2vad.py index 0fed98d..f38f83d 100644 --- a/senpy/plugins/conversion/emotion/ekman2vad.py +++ b/senpy/plugins/conversion/emotion/ekman2vad.py @@ -10,24 +10,26 @@ import math class WNA2VAD(EmotionConversionPlugin): def _ekman_to_vad(self, ekmanSet): - potency = 0 + """Sum the VAD value of all categories found.""" + valence = 0 arousal = 0 dominance = 0 for e in ekmanSet.onyx__hasEmotion: category = e.onyx__hasEmotionCategory centroid = self.centroids[category] - potency += centroid['V'] + valence += centroid['V'] arousal += centroid['A'] dominance += centroid['D'] - e = Emotion({'emoml:potency': potency, + e = Emotion({'emoml:valence': valence, 'emoml:arousal': arousal, - 'emoml:dominance': dominance}) + 'emoml:potency': dominance}) return e def _vad_to_ekman(self, VADEmotion): + """Find the closest category""" V = VADEmotion['emoml:valence'] - A = VADEmotion['emoml:potency'] - D = VADEmotion['emoml:dominance'] + A = VADEmotion['emoml:arousal'] + D = VADEmotion['emoml:potency'] emotion = '' value = 10000000000000000000000.0 for state in self.centroids: @@ -50,7 +52,7 @@ class WNA2VAD(EmotionConversionPlugin): e.onyx__hasEmotion.append(self._ekman_to_vad(emotionSet)) elif fromModel == 'emoml:fsre-dimensions': for i in emotionSet.onyx__hasEmotion: - e.onyx__hasEmotion.append(self._vad_to_ekman(e)) + e.onyx__hasEmotion.append(self._vad_to_ekman(i)) else: raise Error('EMOTION MODEL NOT KNOWN') yield e diff --git a/senpy/plugins/conversion/emotion/ekman2vad.senpy b/senpy/plugins/conversion/emotion/ekman2vad.senpy index 64c7854..80cb0fc 100644 --- a/senpy/plugins/conversion/emotion/ekman2vad.senpy +++ b/senpy/plugins/conversion/emotion/ekman2vad.senpy @@ -7,7 +7,7 @@ onyx:doesConversion: - onyx:conversionFrom: emoml:big6 onyx:conversionTo: emoml:fsre-dimensions - onyx:conversionFrom: emoml:fsre-dimensions - onyx:conversionTo: wna:WNAModel + onyx:conversionTo: emoml:big6 centroids: emoml:big6anger: A: 6.95 @@ -31,5 +31,5 @@ centroids: V: 2.21 aliases: A: emoml:arousal - V: emoml:potency + V: emoml:valence D: emoml:dominance \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index c1b329b..376ac43 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,4 +9,6 @@ test=pytest ignore = E402 max-line-length = 100 [bdist_wheel] -universal=1 \ No newline at end of file +universal=1 +[tool:pytest] +addopts = --cov=senpy --cov-report term-missing \ No newline at end of file diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 609c29e..0265bf2 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -1,5 +1,6 @@ from __future__ import print_function import os +from copy import deepcopy import logging try: @@ -9,7 +10,7 @@ except ImportError: from functools import partial from senpy.extensions import Senpy -from senpy.models import Error +from senpy.models import Error, Results, Entry, EmotionSet, Emotion from flask import Flask from unittest import TestCase @@ -52,6 +53,7 @@ class ExtensionsTest(TestCase): assert module import noop dir(noop) + self.senpy.install_deps() def test_installing(self): """ Enabling a plugin """ @@ -120,3 +122,42 @@ class ExtensionsTest(TestCase): def test_load_default_plugins(self): senpy = Senpy(plugin_folder=self.dir, default_plugins=True) assert len(senpy.plugins) > 1 + + def test_convert_emotions(self): + self.senpy.activate_all() + plugin = { + 'id': 'imaginary', + 'onyx:usesEmotionModel': 'emoml:fsre-dimensions' + } + eSet1 = EmotionSet() + eSet1['onyx:hasEmotion'].append(Emotion({ + 'emoml:arousal': 1, + 'emoml:potency': 0, + 'emoml:valence': 0 + })) + response = Results({ + 'entries': [Entry({ + 'text': 'much ado about nothing', + 'emotions': [eSet1] + })] + }) + params = {'emotionModel': 'emoml:big6', + 'conversion': 'full'} + r1 = deepcopy(response) + self.senpy.convert_emotions(r1, + plugin, + params) + assert len(r1.entries[0].emotions) == 2 + params['conversion'] = 'nested' + r2 = deepcopy(response) + self.senpy.convert_emotions(r2, + plugin, + params) + assert len(r2.entries[0].emotions) == 1 + assert r2.entries[0].emotions[0]['prov:wasDerivedFrom'] == eSet1 + params['conversion'] = 'filtered' + r3 = deepcopy(response) + self.senpy.convert_emotions(r3, + plugin, + params) + assert len(r3.entries[0].emotions) == 1 diff --git a/tests/test_models.py b/tests/test_models.py index df48c79..3aad3ca 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -143,7 +143,3 @@ class ModelsTest(TestCase): print(t) g = rdflib.Graph().parse(data=t, format='turtle') assert len(g) == len(triples) - - def test_convert_emotions(self): - """It should be possible to convert between different emotion models""" - pass