From aa63a831c628977c9b73a0bdfa4731fe66bc1602 Mon Sep 17 00:00:00 2001 From: Madison May Date: Sat, 20 Dec 2014 15:34:07 -0500 Subject: [PATCH 1/3] Fix for texttagging batch support, adding in a few more tests --- indicoio/text/tagging.py | 2 +- indicoio/utils/__init__.py | 5 +++-- tests/remote/test_remote.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/indicoio/text/tagging.py b/indicoio/text/tagging.py index b0e8750..6fabd7b 100644 --- a/indicoio/text/tagging.py +++ b/indicoio/text/tagging.py @@ -22,4 +22,4 @@ def text_tags(api_root, text, batch=False, auth=None, **kwargs): :rtype: Dictionary of class probability pairs """ - return api_handler(text, api_root + "texttags", batch=batch, auth=None, **kwargs) + return api_handler(text, api_root + "texttags", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/utils/__init__.py b/indicoio/utils/__init__.py index c04e378..fb45e0c 100644 --- a/indicoio/utils/__init__.py +++ b/indicoio/utils/__init__.py @@ -26,8 +26,9 @@ def api_handler(arg, url, batch=False, auth=None, **kwargs): json_data = json.dumps(data) if batch: url += "/batch" - if not auth: - auth = auth_query() + # if not auth: + # auth = auth_query() + print auth response = requests.post(url, data=json_data, headers=JSON_HEADERS, auth=auth).json() results = response.get('results', False) if results is False: diff --git a/tests/remote/test_remote.py b/tests/remote/test_remote.py index 43f97f7..dd6331d 100644 --- a/tests/remote/test_remote.py +++ b/tests/remote/test_remote.py @@ -3,11 +3,40 @@ import os import numpy as np import skimage.io +from nose.plugins.skip import Skip, SkipTest from indicoio import political, sentiment, fer, facial_features, language, image_features, text_tags +from indicoio import batch_political, batch_sentiment, batch_fer, batch_facial_features +from indicoio import batch_language, batch_image_features, batch_text_tags DIR = os.path.dirname(os.path.realpath(__file__)) +class BatchAPIRun(unittest.TestCase): + + def setUp(self): + self.username = os.getenv("INDICO_USERNAME") + self.password = os.getenv("INDICO_PASSWORD") + self.auth = (self.username, self.password) + + if not self.username or not self.password: + raise SkipTest + + def test_batch_texttags(self): + test_data = ["On Monday, president Barack Obama will be..."] + response = batch_text_tags(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + + def test_batch_posneg(self): + test_data = ['Worst song ever', 'Best song ever'] + response = batch_sentiment(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(response[0] < 0.5) + + def test_batch_political(self): + test_data = ["Guns don't kill people, people kill people."] + response = batch_political(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + class FullAPIRun(unittest.TestCase): From f58643868f5798027e92eec45f51001e06328c8b Mon Sep 17 00:00:00 2001 From: Madison May Date: Sat, 20 Dec 2014 17:16:50 -0500 Subject: [PATCH 2/3] Fix preprocessing for batch image features --- indicoio/images/features.py | 2 +- indicoio/utils/__init__.py | 4 +++- tests/remote/test_remote.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/indicoio/images/features.py b/indicoio/images/features.py index f404994..8ed7903 100644 --- a/indicoio/images/features.py +++ b/indicoio/images/features.py @@ -59,5 +59,5 @@ def image_features(api_root, image, batch=False, auth=None, **kwargs): :type image: numpy.ndarray :rtype: List containing features """ - image = image_preprocess(image) + image = image_preprocess(image, batch=batch) return api_handler(image, api_root + "imagefeatures", batch=batch, auth=auth, **kwargs) diff --git a/indicoio/utils/__init__.py b/indicoio/utils/__init__.py index fb45e0c..a01a3f0 100644 --- a/indicoio/utils/__init__.py +++ b/indicoio/utils/__init__.py @@ -120,11 +120,13 @@ def normalize(array, distribution=1, norm_range=(0, 1), **kwargs): return dict(zip(keys, norm_array)) return norm_array -def image_preprocess(image): +def image_preprocess(image, batch=False): """ Takes an image and prepares it for sending to the api including resizing and image data/structure standardizing. """ + if batch: + return [image_preprocess(img, batch=False) for img in image] if isinstance(image,list): image = np.asarray(image) if type(image).__module__ != np.__name__: diff --git a/tests/remote/test_remote.py b/tests/remote/test_remote.py index dd6331d..bac7fa5 100644 --- a/tests/remote/test_remote.py +++ b/tests/remote/test_remote.py @@ -37,6 +37,39 @@ class BatchAPIRun(unittest.TestCase): response = batch_political(test_data, auth=self.auth) self.assertTrue(isinstance(response, list)) + def test_batch_fer(self): + test_data = [np.random.rand(48, 48).tolist()] + response = batch_fer(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(isinstance(response[0], dict)) + + def test_batch_facial_features(self): + test_data = [np.random.rand(48, 48).tolist()] + response = batch_facial_features(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(isinstance(response[0], list)) + self.assertEqual(len(response[0]), 48) + + def test_batch_image_features_greyscale(self): + test_data = [np.random.rand(64, 64).tolist()] + response = batch_image_features(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(isinstance(response[0], list)) + self.assertEqual(len(response[0]), 2048) + + def test_batch_image_features_rgb(self): + test_data = [np.random.rand(64, 64, 3).tolist()] + response = batch_image_features(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(isinstance(response[0], list)) + self.assertEqual(len(response[0]), 2048) + + def test_batch_language(self): + test_data = ['clearly an english sentence'] + response = batch_language(test_data, auth=self.auth) + self.assertTrue(isinstance(response, list)) + self.assertTrue(response[0]['English'] > 0.25) + class FullAPIRun(unittest.TestCase): From 6fa61ce27bd60017a4dbf3902517d90e0181ec97 Mon Sep 17 00:00:00 2001 From: Madison May Date: Sat, 20 Dec 2014 17:20:44 -0500 Subject: [PATCH 3/3] Updating changes.txt and version numbers --- CHANGES.txt | 7 ++++--- indicoio/__init__.py | 2 +- setup.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 48f6da9..0258a05 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,6 +13,7 @@ v0.4.4, Thu Sep 25 -- Added dependencies installation to setup.py v0.4.5, Thu Sep 25 -- Added interface to local indico server v0.4.6, Fri Oct 27 -- Updated to point to new indico api servers, cleaner REST API v0.4.8, Fri Nov 7 -- Updated API interface to include new text tags API -v0.4.11, Wed Dec 18 -- Updated tests for text tags -v0.4.12, Thu Dec 19 -- Added batch support interface -v0.4.13, Thu Dec 19 -- Added optional arguments to text tags API \ No newline at end of file +v0.4.11, Thu Dec 18 -- Updated tests for text tags +v0.4.12, Fri Dec 19 -- Added batch support interface +v0.4.13, Fri Dec 19 -- Added optional arguments to text tags API +v0.4.14, Sat Dec 20 -- Fix for batch image features preprocessing, increased test coverage diff --git a/indicoio/__init__.py b/indicoio/__init__.py index 17b4702..4bd6e42 100644 --- a/indicoio/__init__.py +++ b/indicoio/__init__.py @@ -3,7 +3,7 @@ import indicoio.config as config JSON_HEADERS = {'Content-type': 'application/json', 'Accept': 'text/plain'} -Version, version, __version__, VERSION = ('0.4.13',) * 4 +Version, version, __version__, VERSION = ('0.4.14',) * 4 from indicoio.text.sentiment import political, posneg from indicoio.text.sentiment import posneg as sentiment diff --git a/setup.py b/setup.py index fc4825b..392f484 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ except ImportError: setup( name="IndicoIo", - version='0.4.13', + version='0.4.14', packages=[ "indicoio", "indicoio.text",