Initial test coverage for new configuration system

This commit is contained in:
Anne Carlson
2015-02-26 18:50:20 -05:00
committed by Madison May
parent 6b9251f4f5
commit dc47fe5b0a
7 changed files with 192 additions and 112 deletions
-99
View File
@@ -1,99 +0,0 @@
indicoio-python
===============
A wrapper for a series of APIs made by indico.
Check out the main site on:
http://indico.io
Check out our documentation on:
http://indicoiopython.s3-website-us-west-2.amazonaws.com/indicoio.html
Our APIs are totally free to use, and ready to be used in your application. No data or training required.
Current APIs
------------
Right now this wrapper supports the following apps:
- Positive/Negative Sentiment Analysis
- Political Sentiment Analysis
- Image Feature Extraction
- Facial Emotion Recognition
- Facial Feature Extraction
- Language Detection
- Text Topic Tagging
Examples
--------
```
>>> import numpy as np
>>> from indicoio import political, sentiment, fer, facial_features, language
>>> political("Guns don't kill people. People kill people.")
{u'Libertarian': 0.47740164630834825, u'Green': 0.08454409540443657, u'Liberal': 0.16617097211030055, u'Conservative': 0.2718832861769146}
>>> sentiment('Worst movie ever.')
{u'Sentiment': 0.07062467665597527}
>>> sentiment('Really enjoyed the movie.')
{u'Sentiment': 0.8105182526856075}
>>> test_text = "Facebook blog posts about Android tech make better journalism than most news outlets."
>>> tag_dict = text_tags(test_text)
>>> sorted(tag_dict.keys(), key=lambda x: tag_dict[x], reverse=True)[:3]
[u'startups_and_entrepreneurship', u'investment', u'business']
>>> text_tags(test_text, threshold=0.1) # return only keys with value > 0.1
{u'startups_and_entrepreneurship': 0.21888586688354486}
>>> text_tags(test_text, top_n=1) # return only keys with top_n values
{u'startups_and_entrepreneurship': 0.21888586688354486}
>>> tag_dict
{u'fashion': 0.011450126534350728, u'art': 0.00358698972755963, u'energy': 0.005537894035625527, ...}
>>> test_face = np.linspace(0,50,48*48).reshape(48,48).tolist()
>>> fer(test_face)
{u'Angry': 0.08843749137458341, u'Sad': 0.39091163159204684, u'Neutral': 0.1947947999669361, u'Surprise': 0.03443785859010413, u'Fear': 0.17574534848440568, u'Happy': 0.11567286999192382}
>>> facial_features(test_face)
[0.0, -0.02568680526917187, 0.21645604230056517, -0.1519435786033145, -0.5648621854611555, 3.0607368045577226, 0.11434321880792693, -0.02163810928547493, -0.44224330594186484, 0.3024315632285246, -2.6068048934495276, 2.497798330306638, 3.040558335205844, 0.741045340525325, 0.37198135618478817, -0.33132377802172325, -0.9804190889833034, 0.5046575784709395, -0.5609132323152847, 1.679107064439151, 0.6825037853544341, -1.5977176226648016, 1.8959464303080562, -0.7812860715595836, -2.998394007543733, -0.22637273967347724, -0.9642457010679496, 1.4557274834236749, 2.412244419186633, 2.3151771738421965, 0.7881483386786367, 1.6622850935863422, 0.1304768990234367, 1.9344501393866649, 3.1271558035162914, -0.10250886439220543, 1.4921395116492966, 2.761645355670677, 1.6903473594991179, 1.009209807271491, 0.07273926986120445, -1.4941708135718021, -2.082786362439631, 1.0160924044870847, 2.5326580674673895, -0.8328208491083264, 2.0390177029762935, 3.0342637531932777]
>>> language_dict = language('Quis custodiet ipsos custodes')
>>> sorted(language_dict.keys(), key=lambda x: language_dict[x], reverse=True)[:5]
[u'Latin', u'Dutch', u'Greek', u'Portuguese', u'Spanish']
>>> language_dict
{u'Swedish': 0.00033330636691921914, u'Lithuanian': 0.007328693814717631, u'Vietnamese': 0.0002686116137658802, u'Romanian': 8.133913804076592e-06, ...}
```
If you have a local indico server running, simply import from `indicoio.local`.
```
>>> from indicoio.local import political, sentiment, fer, facial_features, language
```
If you'd like to use our batch api interface, please send an email to contact@indico.io.
```
>>> from indicio import batch_sentiment
batch_sentiment(['Text to analyze', 'More text'], auth=("example@example.com", "********"))
```
Installation
------------
```
pip install indicoio
```
Announcement: Indico has partnered with Experfy, a data science consulting marketplace based in the Harvard Innovation Lab. Through Experfy, we are helping our data science community members find lucrative projects and advance their skills. Please signup for Experfy at https://www.experfy.com/ to get started.
+30 -7
View File
@@ -78,13 +78,6 @@ Examples
>>> language_dict
{u'Swedish': 0.00033330636691921914, u'Lithuanian': 0.007328693814717631, u'Vietnamese': 0.0002686116137658802, u'Romanian': 8.133913804076592e-06, ...}
```
If you have a local indico server running, simply import from `indicoio.local`.
```
>>> from indicoio.local import political, sentiment, fer, facial_features, language
```
Batch API Access
----------------
@@ -95,3 +88,33 @@ If you'd like to use our batch api interface, please send an email to contact@in
>>> from indicio import batch_sentiment
batch_sentiment(['Text to analyze', 'More text'], auth=("example@example.com", "********"))
```
Authentication credentials can also be set as the environment variables "INDICO_USERNAME" and "INDICO_PASSWORD" or as 'username' and 'password' in indicorc
Private Cloud API Access
------------------------
If you'd like to use our private cloud interface, please send an email to contact@indico.io.
```
>>> from indicio import sentiment
sentiment("Text to analyze", hostname="http://exampleprivatecloud.io/", auth=("example@example.com", "********"))
```
Private cloud hostnames can also be set as the environment variable "INDICO_PRIVATE_CLOUD_URL" or as 'hostname' in indicorc
indicorc
------------------------
Indicoio-python will look first for $HOME/.indicorc then ./.indicorc for the optional configuration file. The indicorc can be used to set an authentication username and password as well as the private cloud hostname, so they don't need to be specified for every call. All sectiions are optional.
Here is an example of a valid indicorc file:
```
[auth]
username = test@example.com
password = secret
[private_cloud]
hostname = example.indico.io
```
+123
View File
@@ -0,0 +1,123 @@
indicoio-python
===============
A wrapper for a series of APIs made by indico.
Check out the main site on:
http://indico.io
Our APIs are totally free to use, and ready to be used in your
application. No data or training required.
Installation
------------
::
pip install indicoio
Documentation
-------------
Available at `indico.reame.io <http://indico.readme.io/v1.0/docs>`__
Current APIs
------------
Right now this wrapper supports the following apps:
- Positive/Negative Sentiment Analysis
- Political Sentiment Analysis
- Image Feature Extraction
- Facial Emotion Recognition
- Facial Feature Extraction
- Language Detection
- Text Topic Tagging
Examples
--------
::
>>> import numpy as np
>>> from indicoio import political, sentiment, fer, facial_features, language
>>> political("Guns don't kill people. People kill people.")
{u'Libertarian': 0.47740164630834825, u'Green': 0.08454409540443657, u'Liberal': 0.16617097211030055, u'Conservative': 0.2718832861769146}
>>> sentiment('Worst movie ever.')
{u'Sentiment': 0.07062467665597527}
>>> sentiment('Really enjoyed the movie.')
{u'Sentiment': 0.8105182526856075}
>>> test_text = "Facebook blog posts about Android tech make better journalism than most news outlets."
>>> tag_dict = text_tags(test_text)
>>> sorted(tag_dict.keys(), key=lambda x: tag_dict[x], reverse=True)[:3]
[u'startups_and_entrepreneurship', u'investment', u'business']
>>> text_tags(test_text, threshold=0.1) # return only keys with value > 0.1
{u'startups_and_entrepreneurship': 0.21888586688354486}
>>> text_tags(test_text, top_n=1) # return only keys with top_n values
{u'startups_and_entrepreneurship': 0.21888586688354486}
>>> test_face = np.linspace(0,50,48*48).reshape(48,48).tolist()
>>> fer(test_face)
{u'Angry': 0.08843749137458341, u'Sad': 0.39091163159204684, u'Neutral': 0.1947947999669361, u'Surprise': 0.03443785859010413, u'Fear': 0.17574534848440568, u'Happy': 0.11567286999192382}
>>> facial_features(test_face)
[0.0, -0.02568680526917187, 0.21645604230056517, -0.1519435786033145, -0.5648621854611555, 3.0607368045577226, 0.11434321880792693, -0.02163810928547493, -0.44224330594186484, 0.3024315632285246, -2.6068048934495276, 2.497798330306638, 3.040558335205844, 0.741045340525325, 0.37198135618478817, -0.33132377802172325, -0.9804190889833034, 0.5046575784709395, -0.5609132323152847, 1.679107064439151, 0.6825037853544341, -1.5977176226648016, 1.8959464303080562, -0.7812860715595836, -2.998394007543733, -0.22637273967347724, -0.9642457010679496, 1.4557274834236749, 2.412244419186633, 2.3151771738421965, 0.7881483386786367, 1.6622850935863422, 0.1304768990234367, 1.9344501393866649, 3.1271558035162914, -0.10250886439220543, 1.4921395116492966, 2.761645355670677, 1.6903473594991179, 1.009209807271491, 0.07273926986120445, -1.4941708135718021, -2.082786362439631, 1.0160924044870847, 2.5326580674673895, -0.8328208491083264, 2.0390177029762935, 3.0342637531932777]
>>> language_dict = language('Quis custodiet ipsos custodes')
>>> sorted(language_dict.keys(), key=lambda x: language_dict[x], reverse=True)[:5]
[u'Latin', u'Dutch', u'Greek', u'Portuguese', u'Spanish']
>>> language_dict
{u'Swedish': 0.00033330636691921914, u'Lithuanian': 0.007328693814717631, u'Vietnamese': 0.0002686116137658802, u'Romanian': 8.133913804076592e-06, ...}
Batch API Access
----------------
If you'd like to use our batch api interface, please send an email to contact@indico.io.
from indicio import batch\_sentiment batch\_sentiment(['Text
to analyze', 'More text'], auth=("example@example.com",
"\*\*\*\*\*\*\*\*"))
::
Authentication credentials can also be set as the environment variables "INDICO_USERNAME" and "INDICO_PASSWORD" or as 'username' and 'password' in indicorc
Private Cloud API Access
------------------------
If you'd like to use our private cloud interface, please send an email to contact@indico.io.
from indicio import sentiment sentiment("Text to analyze",
hostname="http://exampleprivatecloud.io/",
auth=("example@example.com", "\*\*\*\*\*\*\*\*"))
::
Private cloud hostnames can also be set as the environment variable "INDICO_PRIVATE_CLOUD_URL" or as 'hostname' in indicorc
indicorc
------------------------
Indicoio-python will look first for $HOME/.indicorc then ./.indicorc for the optional configuration file. The indicorc can be used to set an authentication username and password as well as the private cloud hostname, so they don't need to be specified for every call. All sectiions are optional.
Here is an example of a valid indicorc file:
[auth] username = test@example.com password = secret
[private\_cloud] hostname = example.indico.io \`\`\`
+3 -1
View File
@@ -1,3 +1,5 @@
import os
api_root = os.getenv("INDICO_PRIVATE_CLOUD_URL") or "http://apiv1.indico.io/"
def get_api_root():
return os.environ.get("INDICO_PRIVATE_CLOUD_URL") or "http://apiv1.indico.io/"
api_root = get_api_root()
+2 -2
View File
@@ -120,7 +120,7 @@ def normalize(array, distribution=1, norm_range=(0, 1), **kwargs):
def image_preprocess(image, batch=False):
"""
Takes an image and prepares it for sending to the api including
Takes an image and prepares it for sending to the api including
resizing and image data/structure standardizing.
"""
if batch:
@@ -137,4 +137,4 @@ def image_preprocess(image, batch=False):
image = image[:,:,:3]
image = resize(image,(64,64))
image = image.tolist()
return image
return image
+19 -3
View File
@@ -1,5 +1,6 @@
import unittest
import os
from requests import ConnectionError
import numpy as np
import skimage.io
@@ -49,7 +50,7 @@ class BatchAPIRun(unittest.TestCase):
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)
@@ -63,13 +64,21 @@ class BatchAPIRun(unittest.TestCase):
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)
def test_batch_set_url_root(self):
test_data = ['clearly an english sentence']
self.assertRaises(ConnectionError,
batch_language,
test_data,
auth=self.auth,
url_root='http://not.a.real.url/')
class FullAPIRun(unittest.TestCase):
@@ -156,7 +165,7 @@ class FullAPIRun(unittest.TestCase):
self.assertTrue(isinstance(response, list))
self.assertEqual(len(response), 48)
self.check_range(response)
def test_good_image_features_greyscale(self):
test_image = np.random.rand(64, 64).tolist()
response = image_features(test_image)
@@ -214,6 +223,13 @@ class FullAPIRun(unittest.TestCase):
self.assertEqual(language_set, set(language_dict.keys()))
assert language_dict['English'] > 0.25
def test_set_url_root(self):
test_data = 'clearly an english sentence'
self.assertRaises(ConnectionError,
language,
test_data,
url_root='http://not.a.real.url/')
if __name__ == "__main__":
unittest.main()
+15
View File
@@ -0,0 +1,15 @@
import os
from indicoio import config
def test_batch_set_url_root_as_env_var():
test_data = ['clearly an english sentence']
old_private_cloud_url = os.environ.get("INDICO_PRIVATE_CLOUD_URL")
os.environ["INDICO_PRIVATE_CLOUD_URL"] = "http://not.a.real.url/"
assert config.get_api_root() == "http://not.a.real.url/"
if old_private_cloud_url:
os.environ["INDICO_PRIVATE_CLOUD_URL"] = old_private_cloud_url
else:
del(os.environ["INDICO_PRIVATE_CLOUD_URL"])