<commit_before>import csv
from datetime import datetime
from optparse import make_option
from django.core.management.base import BaseCommand
from corehq.apps.hqcase.utils import get_cases_in_domain
from dimagi.utils.decorators.log_exception import log_exception

class Command(BaseCommand):
    """
    Creates the backlog of repeat records that were dropped when bihar repeater
    infrastructure went down.
    """
    option_list = BaseCommand.option_list + (
        make_option('--cleanup',
                    action='store_true',
                    dest='cleanup',
                    default=False,
                    help="Clean up (delete) the affected cases."),
    )

    @log_exception()
    def handle(self, *args, **options):
        domain = 'care-bihar'
        root_types = ('cc_bihar_pregnancy', 'cc_bihar_newborn')
        TASK_TYPE = 'task'
        # loop through all mother cases, then all child cases
        # for each case get all associated tasks
        # if any duplicates found, clean up / print them
        with open('bihar-duplicate-tasks.csv', 'wb') as f:
            writer = csv.writer(f, dialect=csv.excel)
            _dump_headings(writer)
            for case_type in root_types:
                for parent_case in get_cases_in_domain(domain, case_type):
                    try:
                        tasks = filter(lambda subcase: subcase.type == TASK_TYPE, parent_case.get_subcases())
                        if tasks:
                            types = [_task_id(t) for t in tasks]
                            unique_types = set(types)
                            if len(unique_types) != len(tasks):
                                for type_being_checked in unique_types:
                                    matching_cases = [t for t in tasks if _task_id(t) == type_being_checked]
                                    if len(matching_cases) > 1:
                                        for row, case in _get_rows(parent_case, matching_cases):
                                            keep = row[-1]
                                            writer.writerow(row)
                                            if options['cleanup'] and not keep:
                                                _purge(case)
                    except Exception, e:
                        print 'error with case %s (%s)' % (parent_case._id, e)

def _task_id(task_case):
    id = getattr(task_case, 'task_id', None)
    if id is None:
        print '%s has no task id' % task_case._id
    return id

def _dump_headings(csv_writer):
    csv_writer.writerow([
        'parent case id',
        'task case id',
        'task id',
        'date created',
        'closed?',
        'keep?',
    ])

def _get_rows(parent, tasklist):
    tasklist = sorted(tasklist, key=lambda case: (not case.closed, case.opened_on))
    for i, task in enumerate(tasklist):
        row = [
            parent._id,
            task._id,
            _task_id(task),
            task.opened_on,
            task.closed,
            i==0,
        ]
        yield (row, task)

def _purge(case):
    case.doc_type = 'CommCareCase-Deleted'
    case.bihar_task_deleted = True
    case.bihar_task_deleted_on = datetime.utcnow()
    case.save()
<commit_msg>fix doc string<commit_after>import csv
from datetime import datetime
from optparse import make_option
from django.core.management.base import BaseCommand
from corehq.apps.hqcase.utils import get_cases_in_domain
from dimagi.utils.decorators.log_exception import log_exception

class Command(BaseCommand):
    """
    Cleans up all duplicate task cases in the system created because
    of bugs in MOTECH
    """
    option_list = BaseCommand.option_list + (
        make_option('--cleanup',
                    action='store_true',
                    dest='cleanup',
                    default=False,
                    help="Clean up (delete) the affected cases."),
    )

    @log_exception()
    def handle(self, *args, **options):
        domain = 'care-bihar'
        root_types = ('cc_bihar_pregnancy', 'cc_bihar_newborn')
        TASK_TYPE = 'task'
        # loop through all mother cases, then all child cases
        # for each case get all associated tasks
        # if any duplicates found, clean up / print them
        with open('bihar-duplicate-tasks.csv', 'wb') as f:
            writer = csv.writer(f, dialect=csv.excel)
            _dump_headings(writer)
            for case_type in root_types:
                for parent_case in get_cases_in_domain(domain, case_type):
                    try:
                        tasks = filter(lambda subcase: subcase.type == TASK_TYPE, parent_case.get_subcases())
                        if tasks:
                            types = [_task_id(t) for t in tasks]
                            unique_types = set(types)
                            if len(unique_types) != len(tasks):
                                for type_being_checked in unique_types:
                                    matching_cases = [t for t in tasks if _task_id(t) == type_being_checked]
                                    if len(matching_cases) > 1:
                                        for row, case in _get_rows(parent_case, matching_cases):
                                            keep = row[-1]
                                            writer.writerow(row)
                                            if options['cleanup'] and not keep:
                                                _purge(case)
                    except Exception, e:
                        print 'error with case %s (%s)' % (parent_case._id, e)

def _task_id(task_case):
    id = getattr(task_case, 'task_id', None)
    if id is None:
        print '%s has no task id' % task_case._id
    return id

def _dump_headings(csv_writer):
    csv_writer.writerow([
        'parent case id',
        'task case id',
        'task id',
        'date created',
        'closed?',
        'keep?',
    ])

def _get_rows(parent, tasklist):
    tasklist = sorted(tasklist, key=lambda case: (not case.closed, case.opened_on))
    for i, task in enumerate(tasklist):
        row = [
            parent._id,
            task._id,
            _task_id(task),
            task.opened_on,
            task.closed,
            i==0,
        ]
        yield (row, task)

def _purge(case):
    case.doc_type = 'CommCareCase-Deleted'
    case.bihar_task_deleted = True
    case.bihar_task_deleted_on = datetime.utcnow()
    case.save()
<|endoftext|>


<commit_before>#!/usr/bin/python

import numpy as np

"""
Some dimensional reduction functions for use on distance matricies
"""

def pcoordinate(nparray, *args):
    pass

def svd(nparray, *args):
    X = center_matrix(np.asarray(nparray))
    w1, U = np.linalg.eig(X.dot(X.T))
    U = U[:, w1.argsort()[::-1]]  # Sort vectors (columns) by eigenvalues (high to low)
    w2, V = np.linalg.eig(X.T.dot(X))
    V = V[:, w2.argsort()[::-1]]
    S = np.zeros((len(U), len(V)))
    np.fill_diagonal(S, sorted(np.sqrt(w1), reverse=True))
    return(U, S, V.T)

def center_matrix(nparray):
    rownorm = nparray - nparray.mean(axis=0)
    matrix = rownorm - nparray.mean(axis=1)[:, np.newaxis]
    return(matrix)
<commit_msg>added Gower matrix norm<commit_after>#!/usr/bin/env python

import numpy as np

"""
Some dimensional reduction functions for use on distance matricies
"""

def pcoordinate(nparray, *args):
    pass

def svd(nparray, *args):
    X = double_centre(np.asarray(nparray))
    w1, U = np.linalg.eig(X.dot(X.T))
    U = U[:, w1.argsort()[::-1]]  # Sort vectors (columns) by eigenvalues (high to low)
    w2, V = np.linalg.eig(X.T.dot(X))
    V = V[:, w2.argsort()[::-1]]
    S = np.zeros((len(U), len(V)))
    np.fill_diagonal(S, sorted(np.sqrt(w1), reverse=True))
    return(U, S, V.T)

def double_centre(D, square_input=True):
    """
    Double-centres the input matrix:
    From each element:
    Subtract the row mean
    Subtract the column mean
    Add the grand mean
    Divide by -2
    Method from: Torgerson, W S (1952). Multidimensional scaling: I. Theory and
    method.
    Alternatively M = -0.5 * (I - 1/n)D[^2](I - 1/n) (slow for large D)
    """

    M = (D*D if square_input else D)
    (rows, cols) = M.shape

    cm = np.mean(M, axis=0)  # column means
    rm = np.mean(M, axis=1).reshape((rows, 1))  # row means
    gm = np.mean(cm)  # grand mean
    M -= rm + cm - gm
    M /= -2

def center_matrix(nparray):
    rownorm = nparray - nparray.mean(axis=0)
    matrix = rownorm - nparray.mean(axis=1)[:, np.newaxis]
    return(matrix)
<|endoftext|>


<commit_before>"""
Utility functions to retrieve information about available services and setting up security for the Hops platform.

These utils facilitates development by hiding complexity for programs interacting with Hops services.
"""

import pydoop.hdfs as hdfs
import os
import datetime

def get():
    return hdfs.hdfs('default', 0, user=project_user())

def get_fs():
    return hdfs.fs.hdfs('default', 0, user=project_user())

def project_path():
    hops_user = project_user()
    hops_user_split = hops_user.split("__")
    project = hops_user_split[0]
    return hdfs.path.abspath("/Projects/" + project + "/")

def project_user():
    hops_user = os.environ["HADOOP_USER_NAME"]
    return hops_user

fd = None

def init_logger():
    logfile = os.environ['EXEC_LOGFILE']
    fs_handle = get_fs()
    global fd
    fd = fs_handle.open_file(logfile, flags='w')

def log(string):
    if isinstance(string, basestring):
        fd.write('{0}: {1}'.format(datetime.datetime.now().isoformat(), string) + '\n')
    else:
        fd.write('{0}: {1}'.format(datetime.datetime.now().isoformat(),
        'ERROR! Attempting to write a non-basestring object to logfile') + '\n')

def kill_logger():
    fd.flush()
    fd.close()

def create_directories(app_id, run_id, executor_num, param_string=None):
    if param_string == None:
        param_string = 'no_args'

    pyhdfs_handle = get()
    #Create output directory for TensorBoard events for this executor
    #REMOVE THIS LATER!!!!!!!!!! Folder should be created automatically
    hdfs_events_parent_dir = project_path() + "/Logs/TensorFlow"
    #if not pyhdfs_handle.exists(hdfs_events_parent_dir):
    #pyhdfs_handle.create_directory(hdfs_events_parent_dir)

    hdfs_appid_logdir = hdfs_events_parent_dir + "/" + app_id
    #if not pyhdfs_handle.exists(hdfs_appid_logdir):
    #pyhdfs_handle.create_directory(hdfs_appid_logdir)

    hdfs_run_id_logdir = hdfs_appid_logdir + "/" + "runId." + str(run_id)
    #if not pyhdfs_handle.exists(hdfs_run_id_logdir):
    #pyhdfs_handle.create_directory(hdfs_run_id_logdir)

    logfile = hdfs_run_id_logdir + '/executor.' + param_string + '.log'
    os.environ['EXEC_LOGFILE'] = logfile

    hdfs_exec_logdir = hdfs_run_id_logdir + "/executor." + param_string
    #if not pyhdfs_handle.exists(hdfs_exec_logdir):
    pyhdfs_handle.create_directory(hdfs_exec_logdir)

    return hdfs_exec_logdir, hdfs_appid_logdir
<commit_msg>Only attempt to log if fd is not None<commit_after>"""
Utility functions to retrieve information about available services and setting up security for the Hops platform.

These utils facilitates development by hiding complexity for programs interacting with Hops services.
"""

import pydoop.hdfs as hdfs
import os
import datetime

def get():
    return hdfs.hdfs('default', 0, user=project_user())

def get_fs():
    return hdfs.fs.hdfs('default', 0, user=project_user())

def project_path():
    hops_user = project_user()
    hops_user_split = hops_user.split("__")
    project = hops_user_split[0]
    return hdfs.path.abspath("/Projects/" + project + "/")

def project_user():
    hops_user = os.environ["HADOOP_USER_NAME"]
    return hops_user

fd = None

def init_logger():
    logfile = os.environ['EXEC_LOGFILE']
    fs_handle = get_fs()
    global fd
    fd = fs_handle.open_file(logfile, flags='w')

def log(string):
    if fd:
        if isinstance(string, basestring):
            fd.write('{0}: {1}'.format(datetime.datetime.now().isoformat(), string) + '\n')
        else:
            fd.write('{0}: {1}'.format(datetime.datetime.now().isoformat(),
            'ERROR! Attempting to write a non-basestring object to logfile') + '\n')

def kill_logger():
    fd.flush()
    fd.close()

def create_directories(app_id, run_id, executor_num, param_string=None):
    if param_string == None:
        param_string = 'no_args'

    pyhdfs_handle = get()
    #Create output directory for TensorBoard events for this executor
    #REMOVE THIS LATER!!!!!!!!!! Folder should be created automatically
    hdfs_events_parent_dir = project_path() + "/Logs/TensorFlow"
    #if not pyhdfs_handle.exists(hdfs_events_parent_dir):
    #pyhdfs_handle.create_directory(hdfs_events_parent_dir)

    hdfs_appid_logdir = hdfs_events_parent_dir + "/" + app_id
    #if not pyhdfs_handle.exists(hdfs_appid_logdir):
    #pyhdfs_handle.create_directory(hdfs_appid_logdir)

    hdfs_run_id_logdir = hdfs_appid_logdir + "/" + "runId." + str(run_id)
    #if not pyhdfs_handle.exists(hdfs_run_id_logdir):
    #pyhdfs_handle.create_directory(hdfs_run_id_logdir)

    logfile = hdfs_run_id_logdir + '/executor.' + param_string + '.log'
    os.environ['EXEC_LOGFILE'] = logfile

    hdfs_exec_logdir = hdfs_run_id_logdir + "/executor." + param_string
    #if not pyhdfs_handle.exists(hdfs_exec_logdir):
    pyhdfs_handle.create_directory(hdfs_exec_logdir)

    return hdfs_exec_logdir, hdfs_appid_logdir
<|endoftext|>


<commit_before>from unittest import TestCase
from unittest import SkipTest
from chatterbot.adapters.storage import MongoDatabaseAdapter
from chatterbot.conversation import Statement, Response

class MongoAdapterTestCase(TestCase):

    def setUp(self):
        """
        Instantiate the adapter.
        """
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        database_name = "test_db"

        # Skip these tests if a mongo client is not running.
        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.2
            )
            client.server_info()

            self.adapter = MongoDatabaseAdapter(database=database_name)

        except ServerSelectionTimeoutError:
            raise SkipTest("Unable to connect to mongo database.")

    def tearDown(self):
        """
        Remove the test database.
        """
        self.adapter.drop()

class MongoDatabaseAdapterTestCase(MongoAdapterTestCase):

    def test_count_returns_zero(self):
        """
        The count method should return a value of 0
        when nothing has been saved to the database.
        """
        self.assertEqual(self.adapter.count(), 0)

    def test_count_returns_value(self):
        """
        The count method should return a value of 1
        when one item has been saved to the database.
        """
        statement = Statement("Test statement")
        self.adapter.update(statement)
        self.assertEqual(self.adapter.count(), 1)

    def test_statement_not_found(self):
        """
        Test that None is returned by the find method
        when a matching statement is not found.
        """
        self.assertEqual(self.adapter.find("Non-existant"), None)

    def test_statement_found(self):
        """
        Test that a matching statement is returned
        when it exists in the database.
        """
        statement = Statement("New statement")
        self.adapter.update(statement)

        found_statement = self.adapter.find("New statement")
        self.assertNotEqual(found_statement, None)
        self.assertEqual(found_statement.text, statement.text)

    def test_update_adds_new_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertNotEqual(statement_found, None)
        self.assertEqual(statement_found.text, statement.text)

    def test_update_modifies_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        # Check the initial values
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(
            len(found_statement.in_response_to), 0
        )

        # Update the statement value
        statement.add_response(
            Response("New response")
        )
        self.adapter.update(statement)

        # Check that the values have changed
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(
            len(found_statement.in_response_to), 1
        )

    def test_get_random_returns_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        random_statement = self.adapter.get_random()
        self.assertEqual(random_statement.text, statement.text)

    def test_find_returns_nested_responses(self):
        response_list = [
            Response("Yes"),
            Response("No")
        ]
        statement = Statement(
            "Do you like this?",
            in_response_to=response_list
        )
        self.adapter.update(statement)

        result = self.adapter.find(statement.text)

        self.assertIn("Yes", result.in_response_to)
        self.assertIn("No", result.in_response_to)

    def test_multiple_responses_added_on_update(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thank you."),
                Response("Thanks.")
            ]
        )
        self.adapter.update(statement)
        result = self.adapter.find(statement.text)

        self.assertEqual(len(result.in_response_to), 2)
        self.assertIn(statement.in_response_to[0], result.in_response_to)
        self.assertIn(statement.in_response_to[1], result.in_response_to)

    def test_update_saves_statement_with_multiple_responses(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thanks."),
                Response("Thank you.")
            ]
        )
        self.adapter.update(statement)
        response = self.adapter.find(statement.text)

        self.assertEqual(len(response.in_response_to), 2)

    def test_getting_and_updating_statement(self):
        statement = Statement("Hi")
        self.adapter.update(statement)

        statement.add_response(Response("Hello"))
        statement.add_response(Response("Hello"))
        self.adapter.update(statement)

        response = self.adapter.find(statement.text)

        self.assertEqual(len(response.in_response_to), 1)
        self.assertEqual(response.in_response_to[0].occurrence, 2)

    def test_deserialize_responses(self):
        response_list = [
            {"text": "Test", "occurrence": 3},
            {"text": "Testing", "occurrence": 1},
        ]
        results = self.adapter.deserialize_responses(response_list)

        self.assertEqual(len(results), 2)

    def test_remove(self):
        text = "Sometimes you have to run before you can walk."
        statement = Statement(text)
        self.adapter.update(statement)
        self.adapter.remove(statement.text)
        result = self.adapter.find(text)

        self.assertIsNone(result)

    def test_remove_response(self):
        text = "Sometimes you have to run before you can walk."
        statement = Statement(
            "A test flight is not recommended at this design phase.",
            in_response_to=[Response(text)]
        )
        self.adapter.update(statement)
        self.adapter.remove(statement.text)
        results = self.adapter.filter(in_response_to__contains=text)

        self.assertEqual(results, [])

    def test_get_response_statements(self):
        """
        Test that we are able to get a list of only statements
        that are known to be in response to another statement.
        """
        statement_list = [
            Statement("What... is your quest?"),
            Statement("This is a phone."),
            Statement("A what?", in_response_to=[Response("This is a phone.")]),
            Statement("A phone.", in_response_to=[Response("A what?")])
        ]

        for statement in statement_list:
            self.adapter.update(statement)

        responses = self.adapter.get_response_statements()

        self.assertEqual(len(responses), 2)
        self.assertIn("This is a phone.", responses)
        self.assertIn("A what?", responses)

class MongoAdapterFilterTestCase(MongoAdapterTestCase):

    def setUp(self):
        super(MongoAdapterFilterTestCase, self).setUp()

        self.statement1 = Statement(
            "Testing...",
            in_response_to=[
                Response("Why are you counting?")
            ]
        )
        self.statement2 = Statement(
            "Testing one, two, three.",
            in_response_to=[
                Response("Testing...")
            ]
        )

    def test_filter_text_no_matches(self):
        self.adapter.update(self.statement1)
        results = self.adapter.filter(text="Howdy")

        self.assertEqual(len(results), 0)

    def test_filter_in_response_to_no_matches(self):
        self.adapter.update(self.statement1)

        results = self.adapter.filter(
            in_response_to=[Response("Maybe")]
        )
        self.assertEqual(len(results), 0)

    def test_filter_equal_results(self):
        statement1 = Statement(
            "Testing...",
            in_response_to=[]
        )
        statement2 = Statement(
            "Testing one, two, three.",
            in_response_to=[]
        )
        self.adapter.update(statement1)
        self.adapter.update(statement2)

        results = self.adapter.filter(in_response_to=[])
        self.assertEqual(len(results), 2)
        self.assertIn(statement1, results)
        self.assertIn(statement2, results)

    def test_filter_contains_result(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            in_response_to__contains="Why are you counting?"
        )
        self.assertEqual(len(results), 1)
        self.assertIn(self.statement1, results)

    def test_filter_contains_no_result(self):
        self.adapter.update(self.statement1)

        results = self.adapter.filter(
            in_response_to__contains="How do you do?"
        )
        self.assertEqual(results, [])

    def test_filter_multiple_parameters(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            text="Testing...",
            in_response_to__contains="Why are you counting?"
        )

        self.assertEqual(len(results), 1)
        self.assertIn(self.statement1, results)

    def test_filter_multiple_parameters_no_results(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            text="Test",
            in_response_to__contains="Not an existing response."
        )

        self.assertEqual(len(results), 0)

    def test_filter_no_parameters(self):
        """
        If no parameters are passed to the filter,
        then all statements should be returned.
        """
        statement1 = Statement("Testing...")
        statement2 = Statement("Testing one, two, three.")
        self.adapter.update(statement1)
        self.adapter.update(statement2)

        results = self.adapter.filter()

        self.assertEqual(len(results), 2)

    def test_filter_returns_statement_with_multiple_responses(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thanks."),
                Response("Thank you.")
            ]
        )
        self.adapter.update(statement)
        response = self.adapter.filter(
            in_response_to__contains="Thanks."
        )

        # Get the first response
        response = response[0]

        self.assertEqual(len(response.in_response_to), 2)

    def test_response_list_in_results(self):
        """
        If a statement with response values is found using
        the filter method, they should be returned as
        response objects.
        """
        statement = Statement(
            "The first is to help yourself, the second is to help others.",
            in_response_to=[
                Response("Why do people have two hands?")
            ]
        )
        self.adapter.update(statement)
        found = self.adapter.filter(text=statement.text)

        self.assertEqual(len(found[0].in_response_to), 1)
        self.assertEqual(type(found[0].in_response_to[0]), Response)

class ReadOnlyMongoDatabaseAdapterTestCase(MongoAdapterTestCase):

    def test_update_does_not_add_new_statement(self):
        self.adapter.read_only = True

        statement = Statement("New statement")
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(statement_found, None)

    def test_update_does_not_modify_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        self.adapter.read_only = True

        statement.add_response(
            Response("New response")
        )
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(
            statement_found.text, statement.text
        )
        self.assertEqual(
            len(statement_found.in_response_to), 0
        )
<commit_msg>Add test case for new mongo_to_object method.<commit_after>from unittest import TestCase
from unittest import SkipTest
from chatterbot.adapters.storage import MongoDatabaseAdapter
from chatterbot.conversation import Statement, Response

class MongoAdapterTestCase(TestCase):

    def setUp(self):
        """
        Instantiate the adapter.
        """
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        database_name = "test_db"

        # Skip these tests if a mongo client is not running.
        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.2
            )
            client.server_info()

            self.adapter = MongoDatabaseAdapter(database=database_name)

        except ServerSelectionTimeoutError:
            raise SkipTest("Unable to connect to mongo database.")

    def tearDown(self):
        """
        Remove the test database.
        """
        self.adapter.drop()

class MongoDatabaseAdapterTestCase(MongoAdapterTestCase):

    def test_count_returns_zero(self):
        """
        The count method should return a value of 0
        when nothing has been saved to the database.
        """
        self.assertEqual(self.adapter.count(), 0)

    def test_count_returns_value(self):
        """
        The count method should return a value of 1
        when one item has been saved to the database.
        """
        statement = Statement("Test statement")
        self.adapter.update(statement)
        self.assertEqual(self.adapter.count(), 1)

    def test_statement_not_found(self):
        """
        Test that None is returned by the find method
        when a matching statement is not found.
        """
        self.assertEqual(self.adapter.find("Non-existant"), None)

    def test_statement_found(self):
        """
        Test that a matching statement is returned
        when it exists in the database.
        """
        statement = Statement("New statement")
        self.adapter.update(statement)

        found_statement = self.adapter.find("New statement")
        self.assertNotEqual(found_statement, None)
        self.assertEqual(found_statement.text, statement.text)

    def test_update_adds_new_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertNotEqual(statement_found, None)
        self.assertEqual(statement_found.text, statement.text)

    def test_update_modifies_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        # Check the initial values
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(
            len(found_statement.in_response_to), 0
        )

        # Update the statement value
        statement.add_response(
            Response("New response")
        )
        self.adapter.update(statement)

        # Check that the values have changed
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(
            len(found_statement.in_response_to), 1
        )

    def test_get_random_returns_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        random_statement = self.adapter.get_random()
        self.assertEqual(random_statement.text, statement.text)

    def test_find_returns_nested_responses(self):
        response_list = [
            Response("Yes"),
            Response("No")
        ]
        statement = Statement(
            "Do you like this?",
            in_response_to=response_list
        )
        self.adapter.update(statement)

        result = self.adapter.find(statement.text)

        self.assertIn("Yes", result.in_response_to)
        self.assertIn("No", result.in_response_to)

    def test_multiple_responses_added_on_update(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thank you."),
                Response("Thanks.")
            ]
        )
        self.adapter.update(statement)
        result = self.adapter.find(statement.text)

        self.assertEqual(len(result.in_response_to), 2)
        self.assertIn(statement.in_response_to[0], result.in_response_to)
        self.assertIn(statement.in_response_to[1], result.in_response_to)

    def test_update_saves_statement_with_multiple_responses(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thanks."),
                Response("Thank you.")
            ]
        )
        self.adapter.update(statement)
        response = self.adapter.find(statement.text)

        self.assertEqual(len(response.in_response_to), 2)

    def test_getting_and_updating_statement(self):
        statement = Statement("Hi")
        self.adapter.update(statement)

        statement.add_response(Response("Hello"))
        statement.add_response(Response("Hello"))
        self.adapter.update(statement)

        response = self.adapter.find(statement.text)

        self.assertEqual(len(response.in_response_to), 1)
        self.assertEqual(response.in_response_to[0].occurrence, 2)

    def test_deserialize_responses(self):
        response_list = [
            {"text": "Test", "occurrence": 3},
            {"text": "Testing", "occurrence": 1},
        ]
        results = self.adapter.deserialize_responses(response_list)

        self.assertEqual(len(results), 2)

    def test_mongo_to_object(self):
        self.adapter.update(
            Statement('Hello',
                in_response_to=[
                    Response('Hi', occurrence=3),
                    Response('Hey', occurrence=6)
                ]
            )
        )
        statement_data = self.adapter.statements.find_one({'text': 'Hello'})

        obj = self.adapter.mongo_to_object(statement_data)

        self.assertEqual(type(obj), Statement)
        self.assertEqual(len(obj.in_response_to), 2)
        self.assertEqual(type(obj.in_response_to[0]), Response)
        self.assertEqual(type(obj.in_response_to[1]), Response)
        self.assertEqual(obj.in_response_to[0].text, 'Hi')
        self.assertEqual(obj.in_response_to[0].occurrence, 3)
        self.assertEqual(obj.in_response_to[1].text, 'Hey')
        self.assertEqual(obj.in_response_to[1].occurrence, 6)

    def test_remove(self):
        text = "Sometimes you have to run before you can walk."
        statement = Statement(text)
        self.adapter.update(statement)
        self.adapter.remove(statement.text)
        result = self.adapter.find(text)

        self.assertIsNone(result)

    def test_remove_response(self):
        text = "Sometimes you have to run before you can walk."
        statement = Statement(
            "A test flight is not recommended at this design phase.",
            in_response_to=[Response(text)]
        )
        self.adapter.update(statement)
        self.adapter.remove(statement.text)
        results = self.adapter.filter(in_response_to__contains=text)

        self.assertEqual(results, [])

    def test_get_response_statements(self):
        """
        Test that we are able to get a list of only statements
        that are known to be in response to another statement.
        """
        statement_list = [
            Statement("What... is your quest?"),
            Statement("This is a phone."),
            Statement("A what?", in_response_to=[Response("This is a phone.")]),
            Statement("A phone.", in_response_to=[Response("A what?")])
        ]

        for statement in statement_list:
            self.adapter.update(statement)

        responses = self.adapter.get_response_statements()

        self.assertEqual(len(responses), 2)
        self.assertIn("This is a phone.", responses)
        self.assertIn("A what?", responses)

class MongoAdapterFilterTestCase(MongoAdapterTestCase):

    def setUp(self):
        super(MongoAdapterFilterTestCase, self).setUp()

        self.statement1 = Statement(
            "Testing...",
            in_response_to=[
                Response("Why are you counting?")
            ]
        )
        self.statement2 = Statement(
            "Testing one, two, three.",
            in_response_to=[
                Response("Testing...")
            ]
        )

    def test_filter_text_no_matches(self):
        self.adapter.update(self.statement1)
        results = self.adapter.filter(text="Howdy")

        self.assertEqual(len(results), 0)

    def test_filter_in_response_to_no_matches(self):
        self.adapter.update(self.statement1)

        results = self.adapter.filter(
            in_response_to=[Response("Maybe")]
        )
        self.assertEqual(len(results), 0)

    def test_filter_equal_results(self):
        statement1 = Statement(
            "Testing...",
            in_response_to=[]
        )
        statement2 = Statement(
            "Testing one, two, three.",
            in_response_to=[]
        )
        self.adapter.update(statement1)
        self.adapter.update(statement2)

        results = self.adapter.filter(in_response_to=[])
        self.assertEqual(len(results), 2)
        self.assertIn(statement1, results)
        self.assertIn(statement2, results)

    def test_filter_contains_result(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            in_response_to__contains="Why are you counting?"
        )
        self.assertEqual(len(results), 1)
        self.assertIn(self.statement1, results)

    def test_filter_contains_no_result(self):
        self.adapter.update(self.statement1)

        results = self.adapter.filter(
            in_response_to__contains="How do you do?"
        )
        self.assertEqual(results, [])

    def test_filter_multiple_parameters(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            text="Testing...",
            in_response_to__contains="Why are you counting?"
        )

        self.assertEqual(len(results), 1)
        self.assertIn(self.statement1, results)

    def test_filter_multiple_parameters_no_results(self):
        self.adapter.update(self.statement1)
        self.adapter.update(self.statement2)

        results = self.adapter.filter(
            text="Test",
            in_response_to__contains="Not an existing response."
        )

        self.assertEqual(len(results), 0)

    def test_filter_no_parameters(self):
        """
        If no parameters are passed to the filter,
        then all statements should be returned.
        """
        statement1 = Statement("Testing...")
        statement2 = Statement("Testing one, two, three.")
        self.adapter.update(statement1)
        self.adapter.update(statement2)

        results = self.adapter.filter()

        self.assertEqual(len(results), 2)

    def test_filter_returns_statement_with_multiple_responses(self):
        statement = Statement(
            "You are welcome.",
            in_response_to=[
                Response("Thanks."),
                Response("Thank you.")
            ]
        )
        self.adapter.update(statement)
        response = self.adapter.filter(
            in_response_to__contains="Thanks."
        )

        # Get the first response
        response = response[0]

        self.assertEqual(len(response.in_response_to), 2)

    def test_response_list_in_results(self):
        """
        If a statement with response values is found using
        the filter method, they should be returned as
        response objects.
        """
        statement = Statement(
            "The first is to help yourself, the second is to help others.",
            in_response_to=[
                Response("Why do people have two hands?")
            ]
        )
        self.adapter.update(statement)
        found = self.adapter.filter(text=statement.text)

        self.assertEqual(len(found[0].in_response_to), 1)
        self.assertEqual(type(found[0].in_response_to[0]), Response)

class ReadOnlyMongoDatabaseAdapterTestCase(MongoAdapterTestCase):

    def test_update_does_not_add_new_statement(self):
        self.adapter.read_only = True

        statement = Statement("New statement")
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(statement_found, None)

    def test_update_does_not_modify_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        self.adapter.read_only = True

        statement.add_response(
            Response("New response")
        )
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(
            statement_found.text, statement.text
        )
        self.assertEqual(
            len(statement_found.in_response_to), 0
        )
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  hoststool.py : Launch the utility.
#
# Copyleft (C) 2014 - huhamhire hosts team <hosts@huhamhire.com>
# =====================================================================
# Licensed under the GNU General Public License, version 3. You should
# have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
# THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE.
# =====================================================================

__author__ = "huhamhire <me@huhamhire.com>"

import os

from optparse import OptionParser

import gui
import tui

from __version__ import __version__

import sys
sys.path.append("..")
from util import CommonUtil

class UtilLauncher(object):
    """
    HostsUtil class is the entrance to launch `Hosts Setup Utility`. This
    class contains methods for user to decide whether to start a session in
    Graphical User Interface (GUI) mode or in Text-based User Interface (TUI)
    mode.

    Usage can be printed to screen via terminal by using ``-h`` or ``--help``
    argument. The help message would be like this::

        Usage: hoststool.py [-g] [-t] [-h] [--version]

        Options:
          --version        show program's version number and exit
          -h, --help       show this help message and exit
          -g, --graphicui  launch in GUI(QT) mode
          -t, --textui     launch in TUI(Curses) mode

    .. seealso:: :ref:`Get Started <intro-get-started>`.

    .. note:: All methods from this class are declared as `classmethod`.
    """

    @classmethod
    def launch(cls):
        """
        Launch `Hosts Setup Utility`.

        .. note:: This is a `classmethod`.
        """
        options, args = cls.set_commands()
        if options.tui:
            cls.launch_tui()
        elif options.gui:
            cls.launch_gui()

    @classmethod
    def set_commands(cls):
        """
        Set the command options and arguments to launch `Hosts Setup Utility`.

        .. note:: This is a `classmethod`.

        :return: :meth:`hoststool.UtilLauncher.set_commands` returns two
            values:

            (:attr:`options`, :attr:`args`)

            * options(`optparse.Values`): An instance of
              :class:`optparse.Values` containing values for all of your
              options—e.g. if --file takes a single string argument, then
              options.file will be the filename supplied by the user, or None
              if the user did not supply that option args, the list of
              positional arguments leftover after parsing options.
            * args(`list`): Positional arguments leftover after parsing
              options.

            .. seealso:: `OptionParser
                <http://docs.python.org/2/library/optparse.html>`_.

        :rtype: :class:`optparse.Values`, list
        """
        usage = "usage: %prog [-g] [-t] [-h] [--version]"
        version = "Hosts Setup Utility v" + __version__
        parser = OptionParser(usage=usage, version=version)
        parser.add_option("-g", "--graphicui", dest="gui",
                          default=True, action="store_true",
                          help="launch in GUI(QT) mode")
        parser.add_option("-t", "--textui", dest="tui",
                          default=False, action="store_true",
                          help="launch in TUI(Curses) mode ")
        return parser.parse_args()

    @classmethod
    def launch_gui(cls):
        """
        Start a Graphical User Interface (GUI) session of
        `Hosts Setup Utility`.

        .. note:: This is a `classmethod`.
        """
        main = gui.HostsUtil()
        main.start()

    @classmethod
    def launch_tui(cls):
        """
        Start a Text-based User Interface (TUI) session of
        `Hosts Setup Utility`.

        .. note:: This is a `classmethod`.
        """

        # Set code page for Windows
        system = CommonUtil.check_platform()[0]
        cp = "437"
        if system == "Windows":
            chcp = os.popen("chcp")
            cp = chcp.read().split()[-1]
            chcp.close()
            os.popen("chcp 437")

        main = tui.HostsUtil()
        main.start()

        # Restore the default code page for Windows
        if system == "Windows":
            os.popen("chcp " + cp)

if __name__ == "__main__":
    UtilLauncher.launch()<commit_msg>remove hoststool.py for test the git rm command<commit_after><|endoftext|>


<commit_before>#imports Stanford’s Dependency Parser and sets up environment.

print('Before starting, the script will help you set up the paths to the Stanford dependency parser components needed to run the raw text parser. As the system is set up, it will not print out the whole sentence as part of the array. You can change this by deleting the hashtag indicating that this was a note in the corpus_callosum() module prior to running.')
print('=========')
print('')
print('This script is designed to look for keywords that can be construed as the target domain in metaphor research.')
print('=========')
print('')
print('To run the program through a corpus downloaded on your computer, run the function pvc(), with the corpus file location on your computer for the first variable, and a list of search terms for the second variable. If you want the system to print out a .csv with your collected data, enter str(build) in as the third variable.')
print('')
print('')
print('=========')
print('')
from nltk.parse.stanford import StanfordDependencyParser as sparse
pathmodelsjar = input('path to the LANGUAGE MODELS you intend to use: ')
pathjar = input('path to the .jar for Stanford dependency parser--the file should be stanford-parser.jar: ')
depparse = sparse(path_to_jar=pathjar, path_to_models_jar=pathmodelsjar)

directory = '/Users/' + input('the computer user name: ') + '/Desktop/'
builderfile = directory + input('filename for csv output: ') + '.csv'

#Limits analysis to solely the dependency tree level of interest
def lmtc_pfc(ventral_stream, v2, litc_lmtc, litc_pfc, dobj):
	for tuple in ventral_stream:
		if tuple[0] == v2:
			litc_lmtc.append(tuple)
			if tuple[1] == 'dobj':
				dobj.append(tuple[2][0])
	for tuple in litc_lmtc:
		litc_pfc.append(tuple[1])

#Finds and arranges relevant oblique data
def oblique(ventral_stream, v3, met_case_marker, search):
	for tuple in ventral_stream:
		if search in tuple[2][0] and 'JJ' in tuple[2][1]:
			v3.append(tuple[2][0])
			met_case_marker.append(tuple[0][0])
		elif 'mod' in tuple[1] and search in (tuple[2][0] or tuple[0][0]):
			v3.append(tuple[2])
			for tuple in ventral_stream:
				if tuple[0] == v3[0] and tuple[1] == 'case':
					met_case_marker.append(tuple[2])
				elif search in tuple[2][0] and 'RB' in tuple[0][1]:
					met_case_marker.append(tuple[0])

#Generates the array that will be passed to KellyGEN
def corpus_callosum(corpus_callosum1, met_case_marker, adv_nmod, dobj, v1, litc_pfc, litc_lmtc, v3, sentence, TEST):
	corpus_callosum1.append(v1[0][2][0])
	corpus_callosum1.append(dobj[0]) if len(dobj) != 0 else corpus_callosum1.append('0')
	corpus_callosum1.append(str(litc_pfc).replace(',', ' '))
	corpus_callosum1.append(litc_lmtc[0][0][0])
	if len(met_case_marker) != 0:
		corpus_callosum1.append(met_case_marker[0][0])
		corpus_callosum1.append(v3[0][0])
	elif len(met_case_marker) == 0 and len(adv_nmod) == 0:
		corpus_callosum1.append(int(0))
		corpus_callosum1.append(int(0))
	#corpus_callosum1.append(sentence)
	brocas(sentence, litc_pfc, litc_lmtc, corpus_callosum1, TEST)

#Prints and sends array to relevant next steps
def brocas(sentence, litc_pfc, litc_lmtc, array, TEST='non'):
	print('==============')
	if TEST == 'non':
		print(sentence)
		print('')
	if TEST == 'test':
		print('Grammatical Structure:')
		print(litc_pfc)
		print(' ')
		print('Lexical Items:')
		print(litc_lmtc)
		print(' ')
	#Builds the training data sheet for you, if selected.
	if TEST == 'build':
		Training_Data_Builder(array)
	print('Array: ')
	print(array)
	print('==============')

def occipital(sentence, search1=None, TEST='non'):
	#Resets triggers and data failsafes
	v1 = []
	v2 = ''
	v3 = []
	met_case_marker = []
	dobj = []
	adv_nmod = []
	ventral_stream = []
	litc_lmtc = []
	litc_pfc = []
	corpus_callosum1 = []
	#components from Stanford's Dependency parser to create dep. tree.
	res = depparse.raw_parse(sentence)
	dep = res.__next__()
	ventral_stream = list(dep.triples())
	for tuple in ventral_stream:
		if tuple[1] == 'nsubj' or tuple[1] == 'nsubjpass':
			v1.append(tuple)
	if len(v1) > int(0):
		v2 = v1[0][0]
		lmtc_pfc(ventral_stream, v2, litc_lmtc, litc_pfc, dobj)
		oblique(ventral_stream, v3, met_case_marker, search1)
		corpus_callosum(corpus_callosum1, met_case_marker, adv_nmod, dobj, v1, litc_pfc, litc_lmtc, v3, sentence, TEST)
		#print(corpus_callosum1)
	elif len(v1) == 0:
		print('==============')
		print("No dependency structure to analyze")
		print(sentence)
		print('==============')

import codecs
import csv

#takes data and saves it to a CSV to build training file.
def Training_Data_Builder(array):
	with codecs.open(builderfile, 'a', 'utf-8') as csvfile:
		databuilder = csv.writer(csvfile, delimiter=',',
				quotechar='|',
				quoting=csv.QUOTE_MINIMAL)
		databuilder.writerow(array)
	csvfile.close()

#Builds the training data from a printed corpus. Designate your corpus file, the search term(s) you’re looking for, and it’ll feed each one to occipital() in build mode.

def pvc(file, search, function='non'):
	doc = codecs.open(file, 'r', 'utf-8')
	searchlines = doc.readlines()
	doc.close()
	for i, line in enumerate(searchlines):
		for item in search:
			if item in line:
				line2 = line.replace('\\', '').replace('}', '').replace('uc0u8232', '').replace('\'92', '\'').replace('a0', '').replace('\'93', '\"').replace('\'94', '\"').replace('\'96', ',').replace('\'97', ',').replace('f0fs24 ', '').replace('cf0 ', '')
				occipital(line2, item, function)

def test(sentence):
	res = depparse.raw_parse(sentence)
	dep = res.__next__()
	ventral_stream = list(dep.triples())
	for tuple in ventral_stream:
		print(tuple)
<commit_msg>Update KellyGEN.py<commit_after>#imports Stanford’s Dependency Parser and sets up environment.

print('Before starting, the script will help you set up the paths to the Stanford dependency parser components needed to run the raw text parser. As the system is set up, it will not print out the whole sentence as part of the array. You can change this by deleting the hashtag indicating that this was a note in the corpus_callosum() module prior to running.')
print('=========')
print('')
print('This script is designed to look for keywords that can be construed as the target domain in metaphor research.')
print('=========')
print('')
print('To run the program through a corpus downloaded on your computer, run the function pvc(), with the corpus file location on your computer for the first variable, and a list of search terms for the second variable. If you want the system to print out a .csv with your collected data, enter str(build) in as the third variable.')
print('')
print('')
print('=========')
print('')
from nltk.parse.stanford import StanfordDependencyParser as sparse
pathmodelsjar = input('path to the LANGUAGE MODELS you intend to use: ')
pathjar = input('path to the .jar for Stanford dependency parser--the file should be stanford-parser.jar: ')
depparse = sparse(path_to_jar=pathjar, path_to_models_jar=pathmodelsjar)

directory = '/Users/' + input('the computer user name: ') + '/Desktop/'
builderfile = directory + input('filename for csv output: ') + '.csv'

#Limits analysis to solely the dependency tree level of interest
def lmtc_pfc(ventral_stream, v2, litc_lmtc, litc_pfc, dobj):
	for tuple in ventral_stream:
		if tuple[0] == v2:
			litc_lmtc.append(tuple)
			if tuple[1] == 'dobj':
				dobj.append(tuple[2][0])
	for tuple in litc_lmtc:
		litc_pfc.append(tuple[1])

#Finds and arranges relevant oblique data
def oblique(ventral_stream, v3, met_case_marker, search):
	for tuple in ventral_stream:
		if search in tuple[2][0] and 'JJ' in tuple[2][1]:
			v3.append(tuple[2][0])
			met_case_marker.append(tuple[0][0])
		elif 'mod' in tuple[1] and search in (tuple[2][0] or tuple[0][0]):
			v3.append(tuple[2])
			for tuple in ventral_stream:
				if tuple[0] == v3[0] and tuple[1] == 'case':
					met_case_marker.append(tuple[2])
				elif search in tuple[2][0] and 'RB' in tuple[0][1]:
					met_case_marker.append(tuple[0])

#Generates the array that will be passed to KellyGEN
def corpus_callosum(corpus_callosum1, met_case_marker, adv_nmod, dobj, v1, litc_pfc, litc_lmtc, v3, sentence, TEST):
	corpus_callosum1.append(v1[0][2][0])
	corpus_callosum1.append(dobj[0]) if len(dobj) != 0 else corpus_callosum1.append('0')
	corpus_callosum1.append(str(litc_pfc).replace(',', ' '))
	corpus_callosum1.append(litc_lmtc[0][0][0])
	if len(met_case_marker) != 0:
		corpus_callosum1.append(met_case_marker[0][0])
		corpus_callosum1.append(v3[0][0])
	elif len(met_case_marker) == 0 and len(adv_nmod) == 0:
		corpus_callosum1.append(int(0))
		corpus_callosum1.append(int(0))
	#corpus_callosum1.append(sentence)
	brocas(sentence, litc_pfc, litc_lmtc, corpus_callosum1, TEST)

#Prints and sends array to relevant next steps
def brocas(sentence, litc_pfc, litc_lmtc, array, TEST='non'):
	print('==============')
	if TEST == 'non':
		print(sentence)
		print('')
	if TEST == 'test':
		print('Grammatical Structure:')
		print(litc_pfc)
		print(' ')
		print('Lexical Items:')
		print(litc_lmtc)
		print(' ')
	#Builds the training data sheet for you, if selected.
	if TEST == 'build':
		Training_Data_Builder(array)
	print('Array: ')
	print(array)
	print('==============')

def occipital(sentence, search1=None, TEST='non'):
	#Resets triggers and data failsafes
	v1 = []
	v2 = ''
	v3 = []
	met_case_marker = []
	dobj = []
	adv_nmod = []
	ventral_stream = []
	litc_lmtc = []
	litc_pfc = []
	corpus_callosum1 = []
	#components from Stanford's Dependency parser to create dep. tree.
	res = depparse.raw_parse(sentence)
	dep = res.__next__()
	ventral_stream = list(dep.triples())
	for tuple in ventral_stream:
		if tuple[1] == 'nsubj' or tuple[1] == 'nsubjpass':
			v1.append(tuple)
	if len(v1) > int(0):
		v2 = v1[0][0]
		lmtc_pfc(ventral_stream, v2, litc_lmtc, litc_pfc, dobj)
		oblique(ventral_stream, v3, met_case_marker, search1)
		corpus_callosum(corpus_callosum1, met_case_marker, adv_nmod, dobj, v1, litc_pfc, litc_lmtc, v3, sentence, TEST)
		#print(corpus_callosum1)
	elif len(v1) == 0:
		print('==============')
		print("No dependency structure to analyze")
		print(sentence)
		print('==============')

import codecs
import csv

#takes data and saves it to a CSV to build training file.
def Training_Data_Builder(builder=builderfile, array):
	with codecs.open(builder, 'a', 'utf-8') as csvfile:
		databuilder = csv.writer(csvfile, delimiter=',',
				quotechar='|',
				quoting=csv.QUOTE_MINIMAL)
		databuilder.writerow(array)
	csvfile.close()

#Builds the training data from a printed corpus. Designate your corpus file, the search term(s) you’re looking for, and it’ll feed each one to occipital() in build mode.

def pvc(file, search, function='non'):
	doc = codecs.open(file, 'r', 'utf-8')
	searchlines = doc.readlines()
	doc.close()
	for i, line in enumerate(searchlines):
		for item in search:
			if item in line:
				line2 = line.replace('\\', '').replace('}', '').replace('uc0u8232', '').replace('\'92', '\'').replace('a0', '').replace('\'93', '\"').replace('\'94', '\"').replace('\'96', ',').replace('\'97', ',').replace('f0fs24 ', '').replace('cf0 ', '')
				occipital(line2, item, function)

def test(sentence):
	res = depparse.raw_parse(sentence)
	dep = res.__next__()
	ventral_stream = list(dep.triples())
	for tuple in ventral_stream:
		print(tuple)
<|endoftext|>


<commit_before>import os
from . import pre, csr
import imageio
from tqdm import tqdm
import numpy as np
from skimage import morphology
import pandas as pd
from . import draw
from .image_stats import image_summary
from .vendored.shape import shape_index
import matplotlib.pyplot as plt

def _get_scale(image, md_path_or_scale):
    """Get a valid scale from an image and a metadata path or scale."""
    scale = None
    try:
        scale = float(md_path_or_scale)
    except ValueError:
        pass
    if md_path_or_scale is not None and scale is None:
        md_path = md_path_or_scale.split(sep='/')
        meta = image.meta
        for key in md_path:
            meta = meta[key]
        scale = float(meta)
    else:
        if scale is None:
            scale = 1  # measurements will be in pixel units
    return scale

def process_images(filenames, image_format, threshold_radius,
                   smooth_radius, brightness_offset, scale_metadata_path,
                   save_skeleton='', output_folder=None, crop_radius=0,
                   smooth_method='Gaussian'):
    """Full pipeline from images to skeleton stats with local median threshold.

    Parameters
    ----------
    filenames : list of string
        The list of input filenames.
    image_format : string
        The format of the files. 'auto' is automatically determined by the
        imageio library. See imageio documentation for valid image formats.
    threshold_radius : float
        The radius for median thresholding,
    smooth_radius : float in [0, 1]
        The value of sigma with which to Gaussian-smooth the image,
        **relative to `threshold_radius`**.
    brightness_offset : float
        The standard brightness value with which to threshold is the local
        median, `m(x, y)`. Use this value to offset from there: the threshold
        used will be `m(x, y) + brightness_offset`.
    scale_metadata_path : string
        The path in the image dictionary to find the metadata on pixel scale,
        separated by forward slashes ('/').
    save_skeleton : string, optional
        If this is not an empty string, skeleton plots will be saved with
        the given prefix, one per input filename.
    crop_radius : int, optional
        Crop `crop_radius` pixels from each margin of the image before
        processing.
    smooth_method : {'Gaussian', 'TV', 'NL'}, optional
        Which method to use for smoothing.

    Returns
    -------
    result : pandas DataFrame
        Data frame containing all computed statistics on the skeletons found
        in the input image.
    """
    image_format = None if image_format == 'auto' else image_format
    results = []
    image_results = []
    for file in tqdm(filenames):
        image = imageio.imread(file, format=image_format)
        scale = _get_scale(image, scale_metadata_path)
        if crop_radius > 0:
            c = crop_radius
            image = image[c:-c, c:-c]
        pixel_threshold_radius = int(np.ceil(threshold_radius / scale))
        pixel_smoothing_radius = smooth_radius * pixel_threshold_radius
        thresholded = pre.threshold(image, sigma=pixel_smoothing_radius,
                                    radius=pixel_threshold_radius,
                                    offset=brightness_offset)
        quality = shape_index(image, sigma=pixel_smoothing_radius,
                              mode='reflect')
        skeleton = morphology.skeletonize(thresholded) * quality
        framedata = csr.summarise(skeleton, spacing=scale)
        framedata['squiggle'] = np.log2(framedata['branch-distance'] /
                                        framedata['euclidean-distance'])
        framedata['filename'] = file
        framedata['scale'] = scale
        framedata.rename(columns={'mean pixel value': 'mean shape index'},
                         inplace=True)
        results.append(framedata)
        if save_skeleton:
            fig, axes = draw.pipeline_plot(image, sigma=pixel_smoothing_radius,
                                           radius=pixel_threshold_radius,
                                           offset=brightness_offset)
            output_basename = (save_skeleton +
                               os.path.basename(os.path.splitext(file)[0]) +
                               '.png')
            output_filename = os.path.join(output_folder, output_basename)
            fig.savefig(output_filename, dpi=300)
            plt.close(fig)
        image_stats = image_summary(skeleton, spacing=scale)
        image_stats['filename'] = file
        image_stats['branch density'] = (framedata.shape[0] /
                                         image_stats['area'])
        j2j = framedata[framedata['branch-type'] == 2]
        image_stats['mean J2J branch distance'] = j2j['branch-distance'].mean()
        image_results.append(image_stats)

    return pd.concat(results), pd.concat(image_results)
<commit_msg>Don't forget the smoothing method<commit_after>import os
from . import pre, csr
import imageio
from tqdm import tqdm
import numpy as np
from skimage import morphology
import pandas as pd
from . import draw
from .image_stats import image_summary
from .vendored.shape import shape_index
import matplotlib.pyplot as plt

def _get_scale(image, md_path_or_scale):
    """Get a valid scale from an image and a metadata path or scale."""
    scale = None
    try:
        scale = float(md_path_or_scale)
    except ValueError:
        pass
    if md_path_or_scale is not None and scale is None:
        md_path = md_path_or_scale.split(sep='/')
        meta = image.meta
        for key in md_path:
            meta = meta[key]
        scale = float(meta)
    else:
        if scale is None:
            scale = 1  # measurements will be in pixel units
    return scale

def process_images(filenames, image_format, threshold_radius,
                   smooth_radius, brightness_offset, scale_metadata_path,
                   save_skeleton='', output_folder=None, crop_radius=0,
                   smooth_method='Gaussian'):
    """Full pipeline from images to skeleton stats with local median threshold.

    Parameters
    ----------
    filenames : list of string
        The list of input filenames.
    image_format : string
        The format of the files. 'auto' is automatically determined by the
        imageio library. See imageio documentation for valid image formats.
    threshold_radius : float
        The radius for median thresholding,
    smooth_radius : float in [0, 1]
        The value of sigma with which to Gaussian-smooth the image,
        **relative to `threshold_radius`**.
    brightness_offset : float
        The standard brightness value with which to threshold is the local
        median, `m(x, y)`. Use this value to offset from there: the threshold
        used will be `m(x, y) + brightness_offset`.
    scale_metadata_path : string
        The path in the image dictionary to find the metadata on pixel scale,
        separated by forward slashes ('/').
    save_skeleton : string, optional
        If this is not an empty string, skeleton plots will be saved with
        the given prefix, one per input filename.
    crop_radius : int, optional
        Crop `crop_radius` pixels from each margin of the image before
        processing.
    smooth_method : {'Gaussian', 'TV', 'NL'}, optional
        Which method to use for smoothing.

    Returns
    -------
    result : pandas DataFrame
        Data frame containing all computed statistics on the skeletons found
        in the input image.
    """
    image_format = None if image_format == 'auto' else image_format
    results = []
    image_results = []
    for file in tqdm(filenames):
        image = imageio.imread(file, format=image_format)
        scale = _get_scale(image, scale_metadata_path)
        if crop_radius > 0:
            c = crop_radius
            image = image[c:-c, c:-c]
        pixel_threshold_radius = int(np.ceil(threshold_radius / scale))
        pixel_smoothing_radius = smooth_radius * pixel_threshold_radius
        thresholded = pre.threshold(image, sigma=pixel_smoothing_radius,
                                    radius=pixel_threshold_radius,
                                    offset=brightness_offset,
                                    smooth_method=smooth_method)
        quality = shape_index(image, sigma=pixel_smoothing_radius,
                              mode='reflect')
        skeleton = morphology.skeletonize(thresholded) * quality
        framedata = csr.summarise(skeleton, spacing=scale)
        framedata['squiggle'] = np.log2(framedata['branch-distance'] /
                                        framedata['euclidean-distance'])
        framedata['filename'] = file
        framedata['scale'] = scale
        framedata.rename(columns={'mean pixel value': 'mean shape index'},
                         inplace=True)
        results.append(framedata)
        if save_skeleton:
            fig, axes = draw.pipeline_plot(image, sigma=pixel_smoothing_radius,
                                           radius=pixel_threshold_radius,
                                           offset=brightness_offset)
            output_basename = (save_skeleton +
                               os.path.basename(os.path.splitext(file)[0]) +
                               '.png')
            output_filename = os.path.join(output_folder, output_basename)
            fig.savefig(output_filename, dpi=300)
            plt.close(fig)
        image_stats = image_summary(skeleton, spacing=scale)
        image_stats['filename'] = file
        image_stats['branch density'] = (framedata.shape[0] /
                                         image_stats['area'])
        j2j = framedata[framedata['branch-type'] == 2]
        image_stats['mean J2J branch distance'] = j2j['branch-distance'].mean()
        image_results.append(image_stats)

    return pd.concat(results), pd.concat(image_results)
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand
import json
from datasets.models import Dataset, Sound

class Command(BaseCommand):
    help = 'Load examples from json taxonomy file. Use it as python manage.py load_freesound_false_examples ' \
           'DATASET_ID PATH/TO/TAOXNOMY_FILE.json'

    def add_arguments(self, parser):
        parser.add_argument('dataset_id', type=int)
        parser.add_argument('taxonomy_file', type=str)

    def handle(self, *args, **options):
        file_location = options['taxonomy_file']
        dataset_id = options['dataset_id']

        ds = Dataset.objects.get(id=dataset_id)
        taxonomy = ds.taxonomy
        data = json.load(open(file_location))

        for d in data:
            node = taxonomy.get_element_at_id(d['id'])
            for ex_id in d['positive_examples_FS']:
                sound = Sound.objects.get(freesound_id=ex_id)
                node.freesound_examples.add(sound)
            node.save()
<commit_msg>Adapt command load FS examples for verification<commit_after>from django.core.management.base import BaseCommand
import json
from datasets.models import Dataset, Sound

class Command(BaseCommand):
    help = 'Load examples from json taxonomy file. Use it as python manage.py load_freesound_false_examples ' \
           'DATASET_ID PATH/TO/TAOXNOMY_FILE.json'

    def add_arguments(self, parser):
        parser.add_argument('dataset_id', type=int)
        parser.add_argument('taxonomy_file', type=str)

    def handle(self, *args, **options):
        file_location = options['taxonomy_file']
        dataset_id = options['dataset_id']

        ds = Dataset.objects.get(id=dataset_id)
        taxonomy = ds.taxonomy
        data = json.load(open(file_location))

        for d in data:
            node = taxonomy.get_element_at_id(d['id'])
            for ex_id in d['positive_examples_FS'][:2]:
                sound = Sound.objects.get(freesound_id=ex_id)
                node.freesound_examples.add(sound)
            for ex_id in d['positive_examples_FS'][2:]:
                sound = Sound.objects.get(freesound_id=ex_id)
                node.freesound_examples_verification.add(sound)
            node.save()
<|endoftext|>


<commit_before>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                self.log_addition(request, new_user)
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<commit_msg>Added app_label to the context for the add user admin view.<commit_after>
from django.contrib.auth.models import User, Group
from django.core.exceptions import PermissionDenied
from django import template
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.html import escape
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext, ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AdminPasswordChangeForm
from django.contrib import admin

class GroupAdmin(admin.ModelAdmin):
    search_fields = ('name',)
    ordering = ('name',)
    filter_horizontal = ('permissions',)

class UserAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('user_permissions',)

    def __call__(self, request, url):
        # this should not be here, but must be due to the way __call__ routes
        # in ModelAdmin.
        if url is None:
            return self.changelist_view(request)
        if url.endswith('password'):
            return self.user_change_password(request, url.split('/')[0])
        return super(UserAdmin, self).__call__(request, url)

    def add_view(self, request):
        if not self.has_change_permission(request):
            raise PermissionDenied
        if request.method == 'POST':
            form = self.add_form(request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': 'user', 'obj': new_user}
                self.log_addition(request, new_user)
                if "_addanother" in request.POST:
                    request.user.message_set.create(message=msg)
                    return HttpResponseRedirect(request.path)
                else:
                    request.user.message_set.create(message=msg + ' ' + ugettext("You may edit it again below."))
                    return HttpResponseRedirect('../%s/' % new_user.id)
        else:
            form = self.add_form()
        return render_to_response('admin/auth/user/add_form.html', {
            'title': _('Add user'),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_add_permission': True,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_file_field': False,
            'has_absolute_url': False,
            'auto_populated_fields': (),
            'opts': self.model._meta,
            'save_as': False,
            'username_help_text': self.model._meta.get_field('username').help_text,
            'root_path': self.admin_site.root_path,
            'app_label': self.model._meta.app_label,
        }, context_instance=template.RequestContext(request))

    def user_change_password(self, request, id):
        if not request.user.has_perm('auth.change_user'):
            raise PermissionDenied
        user = get_object_or_404(self.model, pk=id)
        if request.method == 'POST':
            form = self.change_password_form(user, request.POST)
            if form.is_valid():
                new_user = form.save()
                msg = ugettext('Password changed successfully.')
                request.user.message_set.create(message=msg)
                return HttpResponseRedirect('..')
        else:
            form = self.change_password_form(user)
        return render_to_response('admin/auth/user/change_password.html', {
            'title': _('Change password: %s') % escape(user.username),
            'form': form,
            'is_popup': '_popup' in request.REQUEST,
            'add': True,
            'change': False,
            'has_delete_permission': False,
            'has_change_permission': True,
            'has_absolute_url': False,
            'opts': self.model._meta,
            'original': user,
            'save_as': False,
            'show_save': True,
            'root_path': self.admin_site.root_path,
        }, context_instance=RequestContext(request))

admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2015, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

from __future__ import division

import numpy as np

from ..visuals import Compound
from ...visuals.mesh import MeshVisual
from ...visuals.transforms import STTransform
from ...visuals.filters import Clipper
from ...util.event import Event
from ...geometry import Rect
from ...color import Color

class Widget(Compound):
    """ A widget takes up a rectangular space, intended for use in
    a 2D pixel coordinate frame.

    The widget is positioned using the transform attribute (as any
    node), and its extent (size) is kept as a separate property.

    Parameters
    ----------
    pos : (x, y)
        A 2-element tuple to specify the top left corner of the widget.
    size : (w, h)
        A 2-element tuple to spicify the size of the widget.
    border_color : color
        The color of the border.
    bgcolor : color
        The background color.
    padding : int
        The amount of padding in the widget (i.e. the space reserved between
        the contents and the border).
    margin : int
        The margin to keep outside the widget's border.
    """

    def __init__(self, pos=(0, 0), size=(10, 10), border_color=None,
                 bgcolor=None, padding=0, margin=0, **kwargs):

        # For drawing border.
        # A mesh is required because GL lines cannot be drawn with predictable
        # shape across all platforms.
        self._mesh = MeshVisual(color=border_color, mode='triangles')
        self._mesh.set_gl_state('translucent', depth_test=False,
                                cull_face=False)
        self._picking_mesh = MeshVisual(mode='triangle_fan')
        self._picking_mesh.set_gl_state(cull_face=False)
        self._picking_mesh.visible = False

        # reserved space inside border
        self._padding = padding

        # reserved space outside border
        self._margin = margin
        self._size = 100, 100

        # layout interaction
        self._fixed_size = (None, None)
        self._stretch = (None, None)

        self._widgets = []

        Compound.__init__(self, [self._mesh, self._picking_mesh], **kwargs)

        self.transform = STTransform()
        self.events.add(resize=Event)
        self.pos = pos
        self._border_color = Color(border_color)
        self._bgcolor = Color(bgcolor)
        self._update_colors()
        self.size = size

    @property
    def pos(self):
        return tuple(self.transform.translate[:2])

    @pos.setter
    def pos(self, p):
        assert isinstance(p, tuple)
        assert len(p) == 2
        if p == self.pos:
            return
        self.transform.translate = p[0], p[1], 0, 0
        self._update_line()

    @property
    def size(self):
        """The size (w, h) of this widget.

        If the widget is a child of another widget, then its size is assigned
        automatically by its parent.
        """
        return self._size

    @size.setter
    def size(self, s):
        assert isinstance(s, tuple)
        assert len(s) == 2
        if self._size == s:
            return
        self._size = s
        self._update_line()
        self._update_child_widgets()
        self._update_clipper()
        self.events.resize()

    @property
    def rect(self):
        return Rect((0, 0), self.size)

    @rect.setter
    def rect(self, r):
        with self.events.resize.blocker():
            self.pos = r.pos
            self.size = r.size
        self.update()
        self.events.resize()

    @property
    def inner_rect(self):
        """The rectangular area inside the margin, border and padding.

        Generally widgets should avoid drawing or placing widgets outside this
        rectangle.
        """
        m = self.margin + self.padding
        if not self.border_color.is_blank:
            m += 1
        return Rect((m, m), (self.size[0]-2*m, self.size[1]-2*m))

    @property
    def stretch(self):
        """Stretch factors (w, h) used when determining how much space to
        allocate to this widget in a layout.

        If either stretch factor is None, then it will be assigned when the
        widget is added to a layout based on the number of columns or rows it
        occupies.
        """
        return self._stretch

    @stretch.setter
    def stretch(self, s):
        self._stretch = s
        self._update_layout()

    @property
    def fixed_size(self):
        """Fixed size (w, h) of the widget.

        Specifying a fixed size for either axis forces the widget to have a
        specific size in a layout. Setting either axis to None allows the
        widget to be resized by the layout.
        """
        return self._fixed_size

    @fixed_size.setter
    def fixed_size(self, s):
        self._fixed_size = s
        self._update_layout()

    def _update_layout(self):
        if isinstance(self.parent, Widget):
            self.parent._update_child_widgets()

    def _update_clipper(self):
        """Called whenever the clipper for this widget may need to be updated.
        """
        if self.clip_children and self._clipper is None:
            self._clipper = Clipper()
        elif not self.clip_children:
            self._clipper = None

        if self._clipper is None:
            return
        self._clipper.rect = self.inner_rect
        self._clipper.transform = self.get_transform('framebuffer', 'visual')

    @property
    def border_color(self):
        """ The color of the border.
        """
        return self._border_color

    @border_color.setter
    def border_color(self, b):
        self._border_color = Color(b)
        self._update_colors()
        self._update_line()
        self.update()

    @property
    def bgcolor(self):
        """ The background color of the Widget.
        """
        return self._bgcolor

    @bgcolor.setter
    def bgcolor(self, value):
        self._bgcolor = Color(value)
        self._update_colors()
        self._update_line()
        self.update()

    @property
    def margin(self):
        return self._margin

    @margin.setter
    def margin(self, m):
        self._margin = m
        self._update_child_widgets()
        self._update_line()
        self.update()
        self.events.resize()

    @property
    def padding(self):
        return self._padding

    @padding.setter
    def padding(self, p):
        self._padding = p
        self._update_child_widgets()
        self.update()

    def _update_line(self):
        """ Update border line to match new shape """
        w = 1  # XXX Eventually this can be a parameter
        m = int(self.margin)
        # border is drawn within the boundaries of the widget:
        #
        #  size = (8, 7)  margin=2
        #  internal rect = (3, 3, 2, 1)
        #  ........
        #  ........
        #  ..BBBB..
        #  ..B  B..
        #  ..BBBB..
        #  ........
        #  ........
        #
        l = b = m
        r = int(self.size[0]) - m
        t = int(self.size[1]) - m
        pos = np.array([
            [l, b], [l+w, b+w],
            [r, b], [r-w, b+w],
            [r, t], [r-w, t-w],
            [l, t], [l+w, t-w],
        ], dtype=np.float32)
        faces = np.array([
            [0, 2, 1],
            [1, 2, 3],
            [2, 4, 3],
            [3, 5, 4],
            [4, 5, 6],
            [5, 7, 6],
            [6, 0, 7],
            [7, 0, 1],
            [5, 3, 1],
            [1, 5, 7],
        ], dtype=np.int32)
        start = 8 if self._border_color.is_blank else 0
        stop = 8 if self._bgcolor.is_blank else 10
        face_colors = None
        if self._face_colors is not None:
            face_colors = self._face_colors[start:stop]
        self._mesh.set_data(vertices=pos, faces=faces[start:stop],
                            face_colors=face_colors)

        # picking mesh covers the entire area
        self._picking_mesh.set_data(vertices=pos[::2])

    def _update_colors(self):
        self._face_colors = np.concatenate(
            (np.tile(self.border_color.rgba, (8, 1)),
             np.tile(self.bgcolor.rgba, (2, 1)))).astype(np.float32)
        self._update_visibility()

    @property
    def picking(self):
        return self._picking

    @picking.setter
    def picking(self, p):
        Compound.picking.fset(self, p)
        self._update_visibility()

    def _update_visibility(self):
        blank = self.border_color.is_blank and self.bgcolor.is_blank
        picking = self.picking
        self._picking_mesh.visible = picking and self.interactive
        self._mesh.visible = not picking and not blank

    def _update_child_widgets(self):
        # Set the position and size of child boxes (only those added
        # using add_widget)
        for ch in self._widgets:
            ch.rect = self.rect.padded(self.padding + self.margin)

    def add_widget(self, widget):
        """
        Add a Widget as a managed child of this Widget.

        The child will be
        automatically positioned and sized to fill the entire space inside
        this Widget (unless _update_child_widgets is redefined).

        Parameters
        ----------
        widget : instance of Widget
            The widget to add.

        Returns
        -------
        widget : instance of Widget
            The widget.
        """
        self._widgets.append(widget)
        widget.parent = self
        self._update_child_widgets()
        return widget

    def add_grid(self, *args, **kwargs):
        """
        Create a new Grid and add it as a child widget.

        All arguments are given to Grid().
        """
        from .grid import Grid
        grid = Grid(*args, **kwargs)
        return self.add_widget(grid)

    def add_view(self, *args, **kwargs):
        """
        Create a new ViewBox and add it as a child widget.

        All arguments are given to ViewBox().
        """
        from .viewbox import ViewBox
        view = ViewBox(*args, **kwargs)
        return self.add_widget(view)

    def remove_widget(self, widget):
        """
        Remove a Widget as a managed child of this Widget.

        Parameters
        ----------
        widget : instance of Widget
            The widget to remove.
        """
        self._widgets.remove(widget)
        widget.remove_parent(self)
        self._update_child_widgets()
<commit_msg>Remove nonfunctional widget.fixed_size<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2015, Vispy Development Team.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

from __future__ import division

import numpy as np

from ..visuals import Compound
from ...visuals.mesh import MeshVisual
from ...visuals.transforms import STTransform
from ...visuals.filters import Clipper
from ...util.event import Event
from ...geometry import Rect
from ...color import Color

class Widget(Compound):
    """ A widget takes up a rectangular space, intended for use in
    a 2D pixel coordinate frame.

    The widget is positioned using the transform attribute (as any
    node), and its extent (size) is kept as a separate property.

    Parameters
    ----------
    pos : (x, y)
        A 2-element tuple to specify the top left corner of the widget.
    size : (w, h)
        A 2-element tuple to spicify the size of the widget.
    border_color : color
        The color of the border.
    bgcolor : color
        The background color.
    padding : int
        The amount of padding in the widget (i.e. the space reserved between
        the contents and the border).
    margin : int
        The margin to keep outside the widget's border.
    """

    def __init__(self, pos=(0, 0), size=(10, 10), border_color=None,
                 bgcolor=None, padding=0, margin=0, **kwargs):

        # For drawing border.
        # A mesh is required because GL lines cannot be drawn with predictable
        # shape across all platforms.
        self._mesh = MeshVisual(color=border_color, mode='triangles')
        self._mesh.set_gl_state('translucent', depth_test=False,
                                cull_face=False)
        self._picking_mesh = MeshVisual(mode='triangle_fan')
        self._picking_mesh.set_gl_state(cull_face=False)
        self._picking_mesh.visible = False

        # reserved space inside border
        self._padding = padding

        # reserved space outside border
        self._margin = margin
        self._size = 100, 100

        # layout interaction
        # todo: use Cassowary; see #277
        self._fixed_size = (None, None)
        self._stretch = (None, None)

        self._widgets = []

        Compound.__init__(self, [self._mesh, self._picking_mesh], **kwargs)

        self.transform = STTransform()
        self.events.add(resize=Event)
        self.pos = pos
        self._border_color = Color(border_color)
        self._bgcolor = Color(bgcolor)
        self._update_colors()
        self.size = size

    @property
    def pos(self):
        return tuple(self.transform.translate[:2])

    @pos.setter
    def pos(self, p):
        assert isinstance(p, tuple)
        assert len(p) == 2
        if p == self.pos:
            return
        self.transform.translate = p[0], p[1], 0, 0
        self._update_line()

    @property
    def size(self):
        """The size (w, h) of this widget.

        If the widget is a child of another widget, then its size is assigned
        automatically by its parent.
        """
        return self._size

    @size.setter
    def size(self, s):
        assert isinstance(s, tuple)
        assert len(s) == 2
        if self._size == s:
            return
        self._size = s
        self._update_line()
        self._update_child_widgets()
        self._update_clipper()
        self.events.resize()

    @property
    def rect(self):
        return Rect((0, 0), self.size)

    @rect.setter
    def rect(self, r):
        with self.events.resize.blocker():
            self.pos = r.pos
            self.size = r.size
        self.update()
        self.events.resize()

    @property
    def inner_rect(self):
        """The rectangular area inside the margin, border and padding.

        Generally widgets should avoid drawing or placing widgets outside this
        rectangle.
        """
        m = self.margin + self.padding
        if not self.border_color.is_blank:
            m += 1
        return Rect((m, m), (self.size[0]-2*m, self.size[1]-2*m))

    @property
    def stretch(self):
        """Stretch factors (w, h) used when determining how much space to
        allocate to this widget in a layout.

        If either stretch factor is None, then it will be assigned when the
        widget is added to a layout based on the number of columns or rows it
        occupies.
        """
        return self._stretch

    @stretch.setter
    def stretch(self, s):
        self._stretch = float(s[0]), float(s[1])
        self._update_layout()

    def _update_layout(self):
        if isinstance(self.parent, Widget):
            self.parent._update_child_widgets()

    def _update_clipper(self):
        """Called whenever the clipper for this widget may need to be updated.
        """
        if self.clip_children and self._clipper is None:
            self._clipper = Clipper()
        elif not self.clip_children:
            self._clipper = None

        if self._clipper is None:
            return
        self._clipper.rect = self.inner_rect
        self._clipper.transform = self.get_transform('framebuffer', 'visual')

    @property
    def border_color(self):
        """ The color of the border.
        """
        return self._border_color

    @border_color.setter
    def border_color(self, b):
        self._border_color = Color(b)
        self._update_colors()
        self._update_line()
        self.update()

    @property
    def bgcolor(self):
        """ The background color of the Widget.
        """
        return self._bgcolor

    @bgcolor.setter
    def bgcolor(self, value):
        self._bgcolor = Color(value)
        self._update_colors()
        self._update_line()
        self.update()

    @property
    def margin(self):
        return self._margin

    @margin.setter
    def margin(self, m):
        self._margin = m
        self._update_child_widgets()
        self._update_line()
        self.update()
        self.events.resize()

    @property
    def padding(self):
        return self._padding

    @padding.setter
    def padding(self, p):
        self._padding = p
        self._update_child_widgets()
        self.update()

    def _update_line(self):
        """ Update border line to match new shape """
        w = 1  # XXX Eventually this can be a parameter
        m = int(self.margin)
        # border is drawn within the boundaries of the widget:
        #
        #  size = (8, 7)  margin=2
        #  internal rect = (3, 3, 2, 1)
        #  ........
        #  ........
        #  ..BBBB..
        #  ..B  B..
        #  ..BBBB..
        #  ........
        #  ........
        #
        l = b = m
        r = int(self.size[0]) - m
        t = int(self.size[1]) - m
        pos = np.array([
            [l, b], [l+w, b+w],
            [r, b], [r-w, b+w],
            [r, t], [r-w, t-w],
            [l, t], [l+w, t-w],
        ], dtype=np.float32)
        faces = np.array([
            [0, 2, 1],
            [1, 2, 3],
            [2, 4, 3],
            [3, 5, 4],
            [4, 5, 6],
            [5, 7, 6],
            [6, 0, 7],
            [7, 0, 1],
            [5, 3, 1],
            [1, 5, 7],
        ], dtype=np.int32)
        start = 8 if self._border_color.is_blank else 0
        stop = 8 if self._bgcolor.is_blank else 10
        face_colors = None
        if self._face_colors is not None:
            face_colors = self._face_colors[start:stop]
        self._mesh.set_data(vertices=pos, faces=faces[start:stop],
                            face_colors=face_colors)

        # picking mesh covers the entire area
        self._picking_mesh.set_data(vertices=pos[::2])

    def _update_colors(self):
        self._face_colors = np.concatenate(
            (np.tile(self.border_color.rgba, (8, 1)),
             np.tile(self.bgcolor.rgba, (2, 1)))).astype(np.float32)
        self._update_visibility()

    @property
    def picking(self):
        return self._picking

    @picking.setter
    def picking(self, p):
        Compound.picking.fset(self, p)
        self._update_visibility()

    def _update_visibility(self):
        blank = self.border_color.is_blank and self.bgcolor.is_blank
        picking = self.picking
        self._picking_mesh.visible = picking and self.interactive
        self._mesh.visible = not picking and not blank

    def _update_child_widgets(self):
        # Set the position and size of child boxes (only those added
        # using add_widget)
        for ch in self._widgets:
            ch.rect = self.rect.padded(self.padding + self.margin)

    def add_widget(self, widget):
        """
        Add a Widget as a managed child of this Widget.

        The child will be
        automatically positioned and sized to fill the entire space inside
        this Widget (unless _update_child_widgets is redefined).

        Parameters
        ----------
        widget : instance of Widget
            The widget to add.

        Returns
        -------
        widget : instance of Widget
            The widget.
        """
        self._widgets.append(widget)
        widget.parent = self
        self._update_child_widgets()
        return widget

    def add_grid(self, *args, **kwargs):
        """
        Create a new Grid and add it as a child widget.

        All arguments are given to Grid().
        """
        from .grid import Grid
        grid = Grid(*args, **kwargs)
        return self.add_widget(grid)

    def add_view(self, *args, **kwargs):
        """
        Create a new ViewBox and add it as a child widget.

        All arguments are given to ViewBox().
        """
        from .viewbox import ViewBox
        view = ViewBox(*args, **kwargs)
        return self.add_widget(view)

    def remove_widget(self, widget):
        """
        Remove a Widget as a managed child of this Widget.

        Parameters
        ----------
        widget : instance of Widget
            The widget to remove.
        """
        self._widgets.remove(widget)
        widget.remove_parent(self)
        self._update_child_widgets()
<|endoftext|>