<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import unittest
import os
import subprocess
import filecmp

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def test_00_if_travis(self):
        if os.environ.get('TRAVIS', None) == "true":
            Test.if_travis = True
        else:
            Test.if_travis = False

    def test_01_get_secret(self):
        if Test.if_travis == False:
            return True

        client_id = os.environ['client_id']
        client_secret = os.environ['client_secret']

        secret_example_file = "./samples/gdcmdtools.secrets.EXAMPLE"

        with open(secret_example_file) as f:
            secret_example = f.read()

        new_secret_1 = re.sub(r'"client_id":"X"',r'"client_id":"%s"' % client_id, secret_example, flags=re.MULTILINE)
        new_secret = re.sub(r'"client_secret":"X"',r'"client_secret":"%s"' % client_secret, new_secret_1, flags=re.MULTILINE)

        Test.secret_file = os.path.expanduser("~/.gdcmdtools.secrets")

        with open(Test.secret_file,'w') as f:
            f.write(new_secret)

        assert True

    def test_01_get_credentials(self):
        if Test.if_travis == False:
            return True

        client_id = os.environ['client_id']
        client_secret = os.environ['client_secret']
        access_token = os.environ['access_token']
        refresh_token = os.environ['refresh_token']

        credentials_example_file = "./samples/gdcmdtools.creds.EXAMPLE"

        with open(credentials_example_file) as f:
            credentials_example = f.read()

        new_credentials_id = re.sub(r'"client_id":"X"',r'"client_id":"%s"' % client_id, credentials_example, flags=re.MULTILINE)
        new_credentials_secret = re.sub(r'"client_secret":"X"',r'"client_secret":"%s"' % client_secret, new_credentials_id, flags=re.MULTILINE)
        new_credentials_token = re.sub(r'"access_token":"X"',r'"access_token":"%s"' % access_token, new_credentials_secret, flags=re.MULTILINE)
        new_credentials = re.sub(r'"refresh_token":"X"',r'"refresh_token":"%s"' % refresh_token, new_credentials_token, flags=re.MULTILINE)

        Test.credentials_file = os.path.expanduser("~/.gdcmdtools.creds")

        with open(Test.credentials_file,'w') as f:
            f.write(new_credentials)

        assert True

    def test_10_raw_put(self):
        file = "./samples/sample.txt"
        cmd = "python ./gdput.py -t raw %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t raw %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        if m:
            Test.raw_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_11_raw_get(self):

        assert Test.raw_file_id

        if Test.raw_file_id:
            file_ori = "./samples/sample.txt"
            file_get = "/tmp/gdcmdtools.tmp"
            cmd_debug = "python ./gdget.py --debug debug -f raw -s %s %s" % (file_get, Test.raw_file_id)

            response = subprocess.check_output(cmd_debug, shell=True)
            result = filecmp.cmp(file_ori, file_get)

            assert result
        else:
            assert False

    def test_12_converted_put(self):
        file = "./samples/sample.txt"
        cmd = "python ./gdput.py -t doc %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t doc %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        if m:
            Test.converted_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_20_gas_put(self):
        file = "./samples/gas/gas.json"
        cmd = "python ./gdput.py -t gas --gas_new %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t gas --gas_new %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        if m:
            Test.gas_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_21_gas_get(self):

        assert Test.gas_file_id

        if Test.gas_file_id:
            file_ori = "./samples/gas/Code.js"
            file_get = "./Code.js"
            cmd_debug = "python ./gdget.py --debug debug -f json %s" % (Test.gas_file_id)

            response = subprocess.check_output(cmd_debug, shell=True)
            result = filecmp.cmp(file_ori, file_get)

            assert result
        else:
            assert False

    def test_99_cleanup(self):
        if Test.if_travis == False:
            return True

        try:
            os.remove(Test.secret_file)
            os.remove(Test.credentials_file)
        except:
            assert False
        else:
            assert True

if __name__ == '__main__':
    unittest.main()
<commit_msg>test for gdmkdir and gdrm<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import unittest
import os
import subprocess
import filecmp

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def test_00_if_travis(self):
        if os.environ.get('TRAVIS', None) == "true":
            Test.if_travis = True
        else:
            Test.if_travis = False

    def test_01_get_secret(self):
        if Test.if_travis == False:
            return True

        client_id = os.environ['client_id']
        client_secret = os.environ['client_secret']

        secret_example_file = "./samples/gdcmdtools.secrets.EXAMPLE"

        with open(secret_example_file) as f:
            secret_example = f.read()

        new_secret_1 = re.sub(r'"client_id":"X"',r'"client_id":"%s"' % client_id, secret_example, flags=re.MULTILINE)
        new_secret = re.sub(r'"client_secret":"X"',r'"client_secret":"%s"' % client_secret, new_secret_1, flags=re.MULTILINE)

        Test.secret_file = os.path.expanduser("~/.gdcmdtools.secrets")

        with open(Test.secret_file,'w') as f:
            f.write(new_secret)

        assert True

    def test_01_get_credentials(self):
        if Test.if_travis == False:
            return True

        client_id = os.environ['client_id']
        client_secret = os.environ['client_secret']
        access_token = os.environ['access_token']
        refresh_token = os.environ['refresh_token']

        credentials_example_file = "./samples/gdcmdtools.creds.EXAMPLE"

        with open(credentials_example_file) as f:
            credentials_example = f.read()

        new_credentials_id = re.sub(r'"client_id":"X"',r'"client_id":"%s"' % client_id, credentials_example, flags=re.MULTILINE)
        new_credentials_secret = re.sub(r'"client_secret":"X"',r'"client_secret":"%s"' % client_secret, new_credentials_id, flags=re.MULTILINE)
        new_credentials_token = re.sub(r'"access_token":"X"',r'"access_token":"%s"' % access_token, new_credentials_secret, flags=re.MULTILINE)
        new_credentials = re.sub(r'"refresh_token":"X"',r'"refresh_token":"%s"' % refresh_token, new_credentials_token, flags=re.MULTILINE)

        Test.credentials_file = os.path.expanduser("~/.gdcmdtools.creds")

        with open(Test.credentials_file,'w') as f:
            f.write(new_credentials)

        assert True

    def test_10_raw_put(self):
        file = "./samples/sample.txt"
        cmd = "python ./gdput.py -t raw %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t raw %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        assert m

        if m:
            Test.raw_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_11_raw_get(self):

        assert Test.raw_file_id

        if Test.raw_file_id:
            file_ori = "./samples/sample.txt"
            file_get = "/tmp/gdcmdtools.tmp"
            cmd_debug = "python ./gdget.py --debug debug -f raw -s %s %s" % (file_get, Test.raw_file_id)

            response = subprocess.check_output(cmd_debug, shell=True)
            result = filecmp.cmp(file_ori, file_get)

            assert result
        else:
            assert False

    def test_12_converted_put(self):
        file = "./samples/sample.txt"
        cmd = "python ./gdput.py -t doc %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t doc %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        assert m
        if m:
            Test.converted_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_20_gas_put(self):
        file = "./samples/gas/gas.json"
        cmd = "python ./gdput.py -t gas --gas_new %s" % file
        cmd_debug = "python ./gdput.py --debug debug -t gas --gas_new %s" % file

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)
        #print response
        #print m.group(1)
        assert m
        if m:
            Test.gas_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_21_gas_get(self):

        assert Test.gas_file_id

        if Test.gas_file_id:
            file_ori = "./samples/gas/Code.js"
            file_get = "./Code.js"
            cmd_debug = "python ./gdget.py --debug debug -f json %s" % (Test.gas_file_id)

            response = subprocess.check_output(cmd_debug, shell=True)
            result = filecmp.cmp(file_ori, file_get)

            assert result
        else:
            assert False

    def test_30_mkdir(self):
        dir_name = "a dir"
        cmd_debug = "python ./gdmkdir.py --debug debug %s" % dir_name

        response = subprocess.check_output(cmd_debug, shell=True)
        m = re.search("id: (.*)", response, re.MULTILINE)

        assert m

        if m:
           Test.dir_file_id = m.group(1)
            assert True
        else:
            assert False

    def test_31_rm(self):
        cmd_debug = "python ./gdrm.py --debug debug %s" % Test.dir_file_id
        response = subprocess.check_output(cmd_debug, shell=True)

        assert True

    def test_99_cleanup(self):
        if Test.if_travis == False:
            return True

        try:
            os.remove(Test.secret_file)
            os.remove(Test.credentials_file)
        except:
            assert False
        else:
            assert True

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>"""Unit Test File"""
import unittest
import os
import warnings
from collections import namedtuple

from logobatch.batches import Batch
from logobatch.batches import SlurmBatch
from logobatch.batches import SshBatch
from logobatch.batches import LocalBatch
from logobatch.batches import ThreadTest
from logobatch.batches import InputsError
from logobatch.batches import InvalidBatchTypeError
from logobatch.batches import MissingAttributeError

from logobatch.logobatch import BatchManager

#LogoBatch Config Testing variables
LB_CONFIG = 'test/t_lbconfig.yml'
LB_CONFIG_BAD_FORMAT = 'test/t_lbconfig_bad_f.yml'
LB_CONFIG_EMPTY = 'test/t_lbconfig_empty'
LBConfigData = namedtuple('LBConfigData',
                          ['compute', 'storage', 'default_email'])

#BBatch testing variables
BATCH_BASE = 'test/test_batch_base'
BBATCH = 'test/t_bbatch.yml'
BAD_FORMAT_BB = 'test/bad_form_t_bbatch.yml'
EMPTY_BBATCH = 'test/empty_t_bbatch.yml'

class TestBatchManager(unittest.TestCase):
    """ Test Class for BatchManager"""
    bm = None

    @classmethod
    def setUpClass(cls):
        cls.bm = BatchManager(BBATCH, BATCH_BASE)

    def test_parse_logobatch_config_namedtuple_type(self):
        """ """
        lbconf_data = self.bm.parse_logobatch_config(config_yml=LB_CONFIG)
        assert(isinstance(lbconf_data, BatchManager.LBConfigData))

    def test_parse_bbatch(self):
        """tests parse_bbatch method"""

        #Test Good BBatch Format
        batches, addresses = self.bm.parse_bbatch(bbatch_yml=BBATCH)
        assert(isinstance(batches, list))
        assert(isinstance(batches[0], SshBatch))
        assert(isinstance(batches[1], SlurmBatch))
        assert(batches[0].name == 'sshb')
        assert(isinstance(batches[0].inputs, list))
        assert(isinstance(addresses, list))
        assert(addresses[0] == 'wpatt2@pdx.edu')

    def test_parse_bbatch_bad_format_error(self):
        """Test Bad formatted BB file"""
        self.assertRaises(AttributeError,
                          lambda: self.bm.parse_bbatch(BAD_FORMAT_BB))

    def test_parse_bbatch_empty_bbatch(self):
        """Test Empty BB file error"""
        self.assertRaises(RuntimeError,
                          lambda: self.bm.parse_bbatch(EMPTY_BBATCH))

TEST_CSV = 'test/input_test.csv'
TEST_SINGLE_FILE = 'test/t_bbatch.yml'
TEST_DIR = 'test/input_dir_test'
INPUTS = ('0', '1', '2', '3')

class TestBatch(unittest.TestCase):
    """Test Class for Batch"""

    def test__new__factory(self):
        """Test subclass object creation with Batch.__new__"""
        ssh_params = {'batch_type': 'ssh',
                      'hostnames': ['test.local'],
                      'command': 'test'}
        slurm_params = {'batch_type': 'slurm',
                        'command': 'test'}
        tt_params = {'batch_type': 'threadtest',
                     'command': 'test',
                     'upper': 4}
        local_params = {'batch_type': 'local',
                        'command': 'test'}
        typeless_params = {'command': 'test'}

        #Test batch type creation
        assert(isinstance(Batch(**ssh_params), SshBatch))
        assert(isinstance(Batch(**slurm_params), SlurmBatch))
        assert(isinstance(Batch(**tt_params), ThreadTest))
        assert(isinstance(Batch(**local_params), LocalBatch))
        assert(isinstance(Batch(**typeless_params), LocalBatch))

        #Test incorrect batch type error
        bad_type_params = {'batch_type': 'bad',
                           'command': 'test'}
        self.assertRaises(InvalidBatchTypeError,
                          lambda: Batch(**bad_type_params))

    def test__init__no_args(self):
        """No Args"""
        self.assertRaises(AttributeError, lambda: Batch.__init__(Batch))

    def test__init__empty_command(self):
        """Empty command"""
        batch_params = {'command': ''}
        self.assertRaises(ValueError,
                          lambda: Batch.__init__(Batch, **batch_params))

    def test_generate_inputs(self):
        """tests three outcomes of the generate inputs static method"""

        #CSV input parsing
        split_csv = list(Batch.generate_inputs(TEST_CSV))
        assert(split_csv == [('1','2','3','4'),('a','b','c','df')])

        #Single file path yield
        file_path = list(Batch.generate_inputs(TEST_SINGLE_FILE))
        assert(file_path == [os.path.realpath(TEST_SINGLE_FILE)])

        #Direcotry file yield
        dir_list = list(Batch.generate_inputs(TEST_DIR))
        assert(dir_list == [os.path.realpath(os.path.join(TEST_DIR, 't1')),
                            os.path.realpath(os.path.join(TEST_DIR, 't2')),
                            os.path.realpath(os.path.join(TEST_DIR, 't3'))])

        #Test bad input
        self.assertRaises(InputsError,
                          lambda: list(Batch.generate_inputs('asdf')))

    def test_format_command(self):
        """
        Tests format_command
        """

        #Test class attribute input markers
        class_attr_params = {'batch_type': 'ssh',
                             'hostnames': ['test.local'],
                             'command': '{batch_base} {name}'}
        sshb = Batch(**class_attr_params)
        command = sshb.format_command(1)
        assert(command == '{} {}'.format(sshb.batch_base, sshb.name))

        #Test CSV input markers
        sshb.command_base = '{id} {i0} {i1} {i2} {i3}'
        command = sshb.format_command(1, inputs=INPUTS)
        assert(command == '1 0 1 2 3')

        sshb.command_base = '{batch_base} {id} {i0} {i1} {i2} {i3}'
        command = sshb.format_command(1, inputs=INPUTS)
        assert(command == '{} 1 0 1 2 3'.format(sshb.batch_base))

        #Test CSV index exception
        sshb.command_base = '{i10}'
        self.assertRaises(IndexError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

        #Test input markers and no inputs
        sshb.command_base = '{i0} {i1} {i2} {i3}'
        self.assertRaises(InputsError,
                          lambda: sshb.format_command(1))

        #Test input and not input markers
        sshb.command_base = '{batch_base}'
        self.assertRaises(InputsError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

        #Test Index warning: Ignore True
        sshb.command_base = '{i0} {i1} {i2} {i6}'
        with warnings.catch_warnings(record=True) as w:
            command = sshb.format_command(1, inputs=INPUTS, ignore_index=True)
            assert(command == '0 1 2 ')
            assert(len(w) == 1)
            assert(issubclass(w[-1].category, Warning))
            assert('Index' in str(w[-1].message))

        #Test Index warning: Ignore False
        self.assertRaises(IndexError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

class TestSshBatch(unittest.TestCase):
    """ """
    def setUp(self):
        bm = BatchManager(BBATCH, BATCH_BASE)
        self.ssh_batch = bm.batches[0]

class TestSlurmBatch(unittest.TestCase):
    """ """
    def setUp(self):
        bm = BatchManager(BBATCH, BATCH_BASE)
        self.slurm_batch = bm.batches[1]

if __name__ == '__main__':
    test_classes = (TestBatch, TestBatchManager)
    test_suite = unittest.TestSuite()
    for test_class in test_classes:
        test_suite.addTest(test_class())

    runner = unittest.TextTestRunner()
    runner.run(test_suite)
<commit_msg>broke up generate_inputs test<commit_after>"""Unit Test File"""
import unittest
import os
import warnings
from collections import namedtuple

from logobatch.batches import Batch
from logobatch.batches import SlurmBatch
from logobatch.batches import SshBatch
from logobatch.batches import LocalBatch
from logobatch.batches import ThreadTest
from logobatch.batches import InputsError
from logobatch.batches import InvalidBatchTypeError
from logobatch.batches import MissingAttributeError

from logobatch.logobatch import BatchManager

#LogoBatch Config Testing variables
LB_CONFIG = 'test/t_lbconfig.yml'
LB_CONFIG_BAD_FORMAT = 'test/t_lbconfig_bad_f.yml'
LB_CONFIG_EMPTY = 'test/t_lbconfig_empty'
LBConfigData = namedtuple('LBConfigData',
                          ['compute', 'storage', 'default_email'])

#BBatch testing variables
BATCH_BASE = 'test/test_batch_base'
BBATCH = 'test/t_bbatch.yml'
BAD_FORMAT_BB = 'test/bad_form_t_bbatch.yml'
EMPTY_BBATCH = 'test/empty_t_bbatch.yml'

class TestBatchManager(unittest.TestCase):
    """ Test Class for BatchManager"""
    bm = None

    @classmethod
    def setUpClass(cls):
        cls.bm = BatchManager(BBATCH, BATCH_BASE)

    def test_parse_logobatch_config_namedtuple_type(self):
        """ """
        lbconf_data = self.bm.parse_logobatch_config(config_yml=LB_CONFIG)
        assert(isinstance(lbconf_data, BatchManager.LBConfigData))

    def test_parse_bbatch(self):
        """tests parse_bbatch method"""

        #Test Good BBatch Format
        batches, addresses = self.bm.parse_bbatch(bbatch_yml=BBATCH)
        assert(isinstance(batches, list))
        assert(isinstance(batches[0], SshBatch))
        assert(isinstance(batches[1], SlurmBatch))
        assert(batches[0].name == 'sshb')
        assert(isinstance(batches[0].inputs, list))
        assert(isinstance(addresses, list))
        assert(addresses[0] == 'wpatt2@pdx.edu')

    def test_parse_bbatch_bad_format_error(self):
        """Test Bad formatted BB file"""
        self.assertRaises(AttributeError,
                          lambda: self.bm.parse_bbatch(BAD_FORMAT_BB))

    def test_parse_bbatch_empty_bbatch(self):
        """Test Empty BB file error"""
        self.assertRaises(RuntimeError,
                          lambda: self.bm.parse_bbatch(EMPTY_BBATCH))

TEST_CSV = 'test/input_test.csv'
TEST_SINGLE_FILE = 'test/t_bbatch.yml'
TEST_DIR = 'test/input_dir_test'
INPUTS = ('0', '1', '2', '3')

class TestBatch(unittest.TestCase):
    """Test Class for Batch"""

    def test__new__factory(self):
        """Test subclass object creation with Batch.__new__"""
        ssh_params = {'batch_type': 'ssh',
                      'hostnames': ['test.local'],
                      'command': 'test'}
        slurm_params = {'batch_type': 'slurm',
                        'command': 'test'}
        tt_params = {'batch_type': 'threadtest',
                     'command': 'test',
                     'upper': 4}
        local_params = {'batch_type': 'local',
                        'command': 'test'}
        typeless_params = {'command': 'test'}

        #Test batch type creation
        assert(isinstance(Batch(**ssh_params), SshBatch))
        assert(isinstance(Batch(**slurm_params), SlurmBatch))
        assert(isinstance(Batch(**tt_params), ThreadTest))
        assert(isinstance(Batch(**local_params), LocalBatch))
        assert(isinstance(Batch(**typeless_params), LocalBatch))

        #Test incorrect batch type error
        bad_type_params = {'batch_type': 'bad',
                           'command': 'test'}
        self.assertRaises(InvalidBatchTypeError,
                          lambda: Batch(**bad_type_params))

    def test__init__no_args(self):
        """No Args"""
        self.assertRaises(AttributeError, lambda: Batch.__init__(Batch))

    def test__init__empty_command(self):
        """Empty command"""
        batch_params = {'command': ''}
        self.assertRaises(ValueError,
                          lambda: Batch.__init__(Batch, **batch_params))

    def test_generate_inputs_csv_yield(self):
        """CSV input parsing"""
        split_csv = list(Batch.generate_inputs(TEST_CSV))
        assert(split_csv == [('1','2','3','4'),('a','b','c','df')])

    def test_generate_inputs_file_yield(self):
        """Single file path yield"""
        file_path = list(Batch.generate_inputs(TEST_SINGLE_FILE))
        assert(file_path == [os.path.realpath(TEST_SINGLE_FILE)])

    def test_generate_inputs_directory_yield(self):
        """Direcotry file yield"""
        dir_list = list(Batch.generate_inputs(TEST_DIR))
        assert(dir_list == [os.path.realpath(os.path.join(TEST_DIR, 't1')),
                            os.path.realpath(os.path.join(TEST_DIR, 't2')),
                            os.path.realpath(os.path.join(TEST_DIR, 't3'))])

    def test_generate_inputs_bad_input(self):
        """Test bad input"""
        self.assertRaises(InputsError,
                          lambda: list(Batch.generate_inputs('asdf')))

    def test_format_command(self):
        """
        Tests format_command
        """

        #Test class attribute input markers
        class_attr_params = {'batch_type': 'ssh',
                             'hostnames': ['test.local'],
                             'command': '{batch_base} {name}'}
        sshb = Batch(**class_attr_params)
        command = sshb.format_command(1)
        assert(command == '{} {}'.format(sshb.batch_base, sshb.name))

        #Test CSV input markers
        sshb.command_base = '{id} {i0} {i1} {i2} {i3}'
        command = sshb.format_command(1, inputs=INPUTS)
        assert(command == '1 0 1 2 3')

        sshb.command_base = '{batch_base} {id} {i0} {i1} {i2} {i3}'
        command = sshb.format_command(1, inputs=INPUTS)
        assert(command == '{} 1 0 1 2 3'.format(sshb.batch_base))

        #Test CSV index exception
        sshb.command_base = '{i10}'
        self.assertRaises(IndexError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

        #Test input markers and no inputs
        sshb.command_base = '{i0} {i1} {i2} {i3}'
        self.assertRaises(InputsError,
                          lambda: sshb.format_command(1))

        #Test input and not input markers
        sshb.command_base = '{batch_base}'
        self.assertRaises(InputsError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

        #Test Index warning: Ignore True
        sshb.command_base = '{i0} {i1} {i2} {i6}'
        with warnings.catch_warnings(record=True) as w:
            command = sshb.format_command(1, inputs=INPUTS, ignore_index=True)
            assert(command == '0 1 2 ')
            assert(len(w) == 1)
            assert(issubclass(w[-1].category, Warning))
            assert('Index' in str(w[-1].message))

        #Test Index warning: Ignore False
        self.assertRaises(IndexError,
                          lambda: sshb.format_command(1, inputs=INPUTS))

class TestSshBatch(unittest.TestCase):
    """ """
    def setUp(self):
        bm = BatchManager(BBATCH, BATCH_BASE)
        self.ssh_batch = bm.batches[0]

class TestSlurmBatch(unittest.TestCase):
    """ """
    def setUp(self):
        bm = BatchManager(BBATCH, BATCH_BASE)
        self.slurm_batch = bm.batches[1]

if __name__ == '__main__':
    test_classes = (TestBatch, TestBatchManager)
    test_suite = unittest.TestSuite()
    for test_class in test_classes:
        test_suite.addTest(test_class())

    runner = unittest.TextTestRunner()
    runner.run(test_suite)
<|endoftext|>


<commit_before>import json
import random
import numpy as np
import scipy.misc
from moviepy.editor import VideoFileClip
from tqdm import tqdm

def sample_windows(video_dirs, n_samples, timesteps, fps):
    windows = []
    for video_dir in video_dirs:
        video = VideoFileClip(str(video_dir / 'video.mp4'))
        n_frames = int(video.duration) * fps
        info = json.load((video_dir / 'info.json').open())

        label = np.zeros(n_frames, dtype=np.uint8)
        for s, e in zip(info['starts'], info['ends']):
            fs = round(s * fps)
            fe = round(e * fps)
            label[fs:fe] = 1

        cur = [(video, t - timesteps, t, label[t - 1])
               for t in range(timesteps, n_frames)]
        windows.extend(cur)

    return random.sample(windows, k=n_samples)

def window_gen(video_dirs, n_samples, batch_size, timesteps, fps):
    windows = sample_windows(video_dirs, n_samples, timesteps, fps)

    idx = 0
    x_batch = np.zeros((batch_size, timesteps, 224, 224, 3), dtype=np.float32)
    y_batch = np.zeros((batch_size, 1), dtype=np.uint8)

    for (video, s, e, y) in windows:
        for i in range(e - s):
            img = video.get_frame((s + i) / fps)
            x_batch[idx][i] = scipy.misc.imresize(img, (224, 224))
        y_batch[idx] = y

        if idx + 1 == batch_size:
            yield x_batch, y_batch
        idx = (idx + 1) % batch_size

def window_data(video_dirs, n_samples, timesteps, fps):
    windows = sample_windows(video_dirs, n_samples, timesteps, fps)

    x_all = np.zeros((n_samples, timesteps, 224, 224, 3), dtype=np.float32)
    y_all = np.zeros((n_samples, 1), dtype=np.uint8)

    for idx, (video, s, e, y) in enumerate(tqdm(windows)):
        for i in range(e - s):
            img = video.get_frame((s + i) / fps)
            x_all[idx][i] = scipy.misc.imresize(img, (224, 224))
        y_all[idx] = y

    return x_all, y_all

import json
from pathlib import Path

import numpy as np
import pandas as pd

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

from keras.models import Sequential, Model
from keras.preprocessing import image
from keras.layers import *
from keras.optimizers import *

from utils import get_callbacks

def main():
    # with tf.device('/gpu:3'):
    model = Sequential()
    model.add(TimeDistributed(BatchNormalization(), input_shape=(10, 224, 224, 3)))
    model.add(TimeDistributed(Conv2D(4, kernel_size=5, strides=3, activation='relu')))
    model.add(TimeDistributed(Conv2D(8, kernel_size=5, strides=2, activation='relu')))
    model.add(TimeDistributed(Conv2D(12, kernel_size=3, strides=1, activation='relu')))
    model.add(TimeDistributed(MaxPooling2D(pool_size=3)))
    model.add(Conv3D(4, kernel_size=5, strides=1, activation='relu'))
    model.add(BatchNormalization())
    model.add(Flatten())
    model.add(Dense(16))
    model.add(Dropout(0.3))
    model.add(Dense(1, activation='sigmoid'))

    model_arg = {
        'loss': 'binary_crossentropy',
        'optimizer': 'sgd',
        'metrics': ['binary_accuracy']
    }
    model.compile(**model_arg)
    model.summary()

    dataset = Path('~/tthl-dataset/').expanduser()
    video_dirs = sorted(dataset.glob('video*/'))

    # train_gen = window_gen(video_dirs[:3], 1000, 80, 10, 2)
    # val_gen = window_gen(video_dirs[-1:], 200, 80, 10, 2)
    # fit_gen_arg = {
    #     'generator': train_gen,
    #     'steps_per_epoch': 10000 // 40,
    #     'epochs': 30,
    #     'validation_data': val_gen,
    #     'validation_steps': 1000 // 40,
    #     'callbacks': get_callbacks('conv3d')
    # }
    # model.fit_generator(**fit_gen_arg)

    x_train, y_train = window_data(video_dirs[:3], 100, 10, 5)
    x_val, y_val = window_data(video_dirs[-1:], 20, 10, 5)
    fit_arg = {
        'x': x_train,
        'y': y_train,
        'batch_size': 80,
        'epochs': 30,
        'validation_data': (x_val, y_val)
    }
    model.fit(**fit_arg)

if __name__ == '__main__':
    main()
<commit_msg>Update at 2017-07-20 23-22-58<commit_after>import json
import random
import numpy as np
import scipy.misc
from moviepy.editor import VideoFileClip
from tqdm import tqdm

def sample_windows(video_dirs, n_samples, timesteps, fps):
    windows = []
    for video_dir in video_dirs:
        video = VideoFileClip(str(video_dir / 'video.mp4'))
        n_frames = int(video.duration) * fps
        info = json.load((video_dir / 'info.json').open())

        label = np.zeros(n_frames, dtype=np.uint8)
        for s, e in zip(info['starts'], info['ends']):
            fs = round(s * fps)
            fe = round(e * fps)
            label[fs:fe] = 1

        cur = [(video, t - timesteps, t, label[t - 1])
               for t in range(timesteps, n_frames)]
        windows.extend(cur)

    return random.sample(windows, k=n_samples)

def window_gen(video_dirs, n_samples, batch_size, timesteps, fps):
    windows = sample_windows(video_dirs, n_samples, timesteps, fps)

    idx = 0
    x_batch = np.zeros((batch_size, timesteps, 224, 224, 3), dtype=np.float32)
    y_batch = np.zeros((batch_size, 1), dtype=np.uint8)

    for (video, s, e, y) in windows:
        for i in range(e - s):
            img = video.get_frame((s + i) / fps)
            x_batch[idx][i] = scipy.misc.imresize(img, (224, 224))
        y_batch[idx] = y

        if idx + 1 == batch_size:
            yield x_batch, y_batch
        idx = (idx + 1) % batch_size

def window_data(video_dirs, n_samples, timesteps, fps):
    windows = sample_windows(video_dirs, n_samples, timesteps, fps)

    x_all = np.zeros((n_samples, timesteps, 224, 224, 3), dtype=np.float32)
    y_all = np.zeros((n_samples, 1), dtype=np.uint8)

    for idx, (video, s, e, y) in enumerate(tqdm(windows)):
        for i in range(e - s):
            img = video.get_frame((s + i) / fps)
            x_all[idx][i] = scipy.misc.imresize(img, (224, 224))
        y_all[idx] = y

    return x_all, y_all

import json
from pathlib import Path

import numpy as np
import pandas as pd

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))

from keras.models import Sequential, Model
from keras.preprocessing import image
from keras.layers import *
from keras.optimizers import *

from utils import get_callbacks

def main():
    # with tf.device('/gpu:3'):
    model = Sequential()
    model.add(TimeDistributed(BatchNormalization(), input_shape=(10, 224, 224, 3)))
    model.add(TimeDistributed(Conv2D(4, kernel_size=5, strides=3, activation='relu')))
    model.add(TimeDistributed(Conv2D(8, kernel_size=5, strides=2, activation='relu')))
    model.add(TimeDistributed(Conv2D(12, kernel_size=3, strides=1, activation='relu')))
    model.add(TimeDistributed(MaxPooling2D(pool_size=3)))
    model.add(Conv3D(4, kernel_size=5, strides=1, activation='relu'))
    model.add(BatchNormalization())
    model.add(Flatten())
    model.add(Dense(16))
    model.add(Dropout(0.3))
    model.add(Dense(1, activation='sigmoid'))

    model_arg = {
        'loss': 'binary_crossentropy',
        'optimizer': 'sgd',
        'metrics': ['binary_accuracy']
    }
    model.compile(**model_arg)
    model.summary()

    dataset = Path('~/tthl-dataset/').expanduser()
    video_dirs = sorted(dataset.glob('video*/'))

    # train_gen = window_gen(video_dirs[:3], 1000, 80, 10, 2)
    # val_gen = window_gen(video_dirs[-1:], 200, 80, 10, 2)
    # fit_gen_arg = {
    #     'generator': train_gen,
    #     'steps_per_epoch': 10000 // 40,
    #     'epochs': 30,
    #     'validation_data': val_gen,
    #     'validation_steps': 1000 // 40,
    #     'callbacks': get_callbacks('conv3d')
    # }
    # model.fit_generator(**fit_gen_arg)

    x_train, y_train = window_data(video_dirs[:3], 10000, 10, 5)
    x_val, y_val = window_data(video_dirs[-1:], 2000, 10, 5)
    fit_arg = {
        'x': x_train,
        'y': y_train,
        'batch_size': 80,
        'epochs': 30,
        'validation_data': (x_val, y_val)
    }
    model.fit(**fit_arg)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#!/usr/bin/env python
import os
import sys
import urllib
import xml.etree.ElementTree as ET

from url_functions import getOnlineContent

class season():
    def keys(self):
        tempKeys = sorted(self.episodes.keys(), key=lambda key: int(key))
        for key in xrange(len(tempKeys)):
            tempKeys[key] = int(tempKeys[key])
        return tempKeys

    def __getitem__(self, episode):
        if str(episode) in self.episodes.keys():
            return self.episodes[str(episode)]
        else:
            return False

    def __repr__(self):
        return "Season: " + self.episodes[self.episodes.keys()[0]]['SeasonNumber'] + " with " + str(len(self.episodes.keys())) + " episodes.\n"

    def __init__(self, episodes):
        self.episodes = episodes

class show():
    def keys(self):
        tempKeys = sorted(self.seasons.keys(), key=lambda key: int(key))
        for key in xrange(len(tempKeys)):
            tempKeys[key] = int(tempKeys[key])
        return tempKeys

    def showKeys(self):
        return self.seriesInfo.keys()

    def __len__(self):
        return len(self.seasons)

    def __getitem__(self, index):
        if str(index) in self.seasons.keys():
            return self.seasons[str(index)]
        elif index in self.seriesInfo.keys():
            return self.seriesInfo[index]
        elif index == 'topSeason':
            return int(self.keys()[-1])
        elif index == 'bottomSeason':
            return int(self.keys()[0])
        else:
            return []

    def __repr__(self):
        info = self.seriesInfo['SeriesName'] + " with " + str(len(self.seasons.keys())) + " seasons.\n"
        sortedKeys = sorted(self.seasons.keys(), key=lambda key: int(key))
        for key in sortedKeys:
            info += str(self.seasons[str(key)])
        return info

    def __init__(self, seriesInfo, sortedInfo):
        self.seriesInfo = seriesInfo
        self.seasons = {}
        for key in sortedInfo.keys():
            self.seasons[key] = season(sortedInfo[key])

class TVDB:
    def logger(self, data):
        if self.debug:
            fileHandler = open(os.path.join(os.path.dirname(__file__), "history.log"), "a")
            fileHandler.write(str(data)+'\n')
            fileHandler.close()

    def sortEpisodes(self, episodes):
        sortedEpisodes = {}
        if self.debug:
            print "Sorting full list of episode(s) into seasons -> episode(s)."
            print len(episodes)
        for episode in episodes:
            if episode['SeasonNumber'] in sortedEpisodes.keys():
                sortedEpisodes[episode['SeasonNumber']][episode['EpisodeNumber']] = episode
            else:
                sortedEpisodes[episode['SeasonNumber']] = {}
                sortedEpisodes[episode['SeasonNumber']][episode['EpisodeNumber']] = episode
        if self.debug:
            print len(sortedEpisodes)
            print "Done."
        return sortedEpisodes

    def xmlShowToDict(self, xmlString):
        with open("last.xml", "wb") as temp:
            temp.write(xmlString)
        try:
            tree = ET.fromstring(xmlString)
        except Exception as error:
            print "Error: Failed to convert XML data:", error
            return {}
        data = {}
        data["series"] = {}
        data["episodes"] = []
        for item in tree:
            if item.tag == "Series":
                for child in item.getchildren():
                    data["series"][child.tag] = child.text
            elif item.tag == "Episode":
                temp = item.getchildren()
                for child in temp:
                    if child.tag == "SeasonNumber":
                        SeasonNumber = int(child.text)
                    if child.tag == "EpisodeNumber":
                        EpisodeNumber = int(child.text)
                data["episodes"].append({})
                for child in temp:
                    data["episodes"][-1][child.tag] = child.text
        return data

    def xmlSeriesToDict(self, xmlString):
        with open("last.xml", "wb") as temp:
            temp.write(xmlString)
        tree = ET.fromstring(xmlString)
        data = []
        for item in tree:
            temp = {}
            if item.tag == "Series":
                for child in item.getchildren():
                    temp[child.tag] = child.text
                data.append(temp)
        return data

    def getShowInfo(self, seriesID):
        result = False
        if self.debug:
            print "Retriving online info for series ID: " + seriesID
        path = '/series/{}/all/en.xml'.format(seriesID)
        if os.path.exists(os.path.join("/tmp/", path)):
            with open(os.path.join("/tmp/", path), mode='r') as cacheReader:
                rawData = cacheReader.read()
        else:
            rawData = getOnlineContent('http://thetvdb.com/api/' + self.apikey + )
        if rawData:
            with open(os.path.join("/tmp/", path), mode="w", buffering=None) as cacheWriter:
                cacheWriter.write(rawData)
            rawData = rawData.replace("&", "and")
            data = self.xmlShowToDict(rawData)
            if "episodes" in data.keys():
                data["episodes"] = self.sortEpisodes(data["episodes"])
                result = show(data["series"], data["episodes"])
                if self.debug:
                    print result
        return result

    def search(self, showTitle):
        if len(showTitle) > 0:
            URL = "http://thetvdb.com/api/GetSeries.php?seriesname={}".format(urllib.quote_plus(showTitle))
            if self.debug:
                print "Accessing tvdb: " + URL
            rawData = getOnlineContent(URL)
            if rawData:
                rawData = rawData.replace("&", "and")
                if self.debug:
                    print "Get search results."
                shows = []
                if self.debug:
                    print "Parsing search results..."
                shows = self.xmlSeriesToDict(rawData)
                if self.debug:
                    print "Done."
                return shows
            else:
                return False
        else:
            return False

    def askForShow(self, showTitle = False):
        while True:
            if not showTitle:
                showTitle = raw_input("Enter in show title: ")
            if self.debug:
                print "Searching for: " + showTitle
            shows = self.searchForShow(showTitle)
            if shows:
                print "Got search results."
                if len(shows) > 1:
                    for index in xrange(len(shows)):
                        print str(index+1).zfill(2) + ": " + shows[index]['SeriesName']
                    print str(len(shows)+1).zfill(2) + ": Exit"
                    while True:
                        choice = raw_input("Enter in selection: ")
                        if len(choice) > 0:
                            if choice.isdigit():
                                choice = int(choice) - 1
                                if choice >= 0:
                                    if choice == len(shows):
                                        return False
                                    else:
                                        return shows[choice]
                                else:
                                    print "Invalid selection, please try again."
                            else:
                                print "Invalid selection, please try again."
                        else:
                            print "Invalid input, please try again."
                else:
                    print "Retrieved only one show result."
                    return shows[0]
            else:
                print "No shows found, please try again."
                showTitle = False

    def __init__(self, apiKey, debug = True):
        self.debug = debug
        if os.path.exists(os.path.join(os.path.dirname(__file__), "history.log")):
            os.remove(os.path.join(os.path.dirname(__file__), "history.log"))
        self.apikey = apiKey

if __name__ == '__main__':
    debug = True
    tvdbHandler = TVDB('4E7A4FBBC8CF4D74', debug)
    showInfo = tvdbHandler.search("Doctor Who (2005)")
    if showInfo:
        seriesID = showInfo[0]['seriesid']
        showInfo = tvdbHandler.getShowInfo(seriesID)
        if showInfo:
            print showInfo[1][1]
<commit_msg>Adding logging<commit_after>#!/usr/bin/env python
import os
import sys
import urllib
import xml.etree.ElementTree as ET

from url_functions import getOnlineContent

class season():
    def keys(self):
        tempKeys = sorted(self.episodes.keys(), key=lambda key: int(key))
        for key in xrange(len(tempKeys)):
            tempKeys[key] = int(tempKeys[key])
        return tempKeys

    def __getitem__(self, episode):
        if str(episode) in self.episodes.keys():
            return self.episodes[str(episode)]
        else:
            return False

    def __repr__(self):
        return "Season: " + self.episodes[self.episodes.keys()[0]]['SeasonNumber'] + " with " + str(len(self.episodes.keys())) + " episodes.\n"

    def __init__(self, episodes):
        self.episodes = episodes

class show():
    def keys(self):
        tempKeys = sorted(self.seasons.keys(), key=lambda key: int(key))
        for key in xrange(len(tempKeys)):
            tempKeys[key] = int(tempKeys[key])
        return tempKeys

    def showKeys(self):
        return self.seriesInfo.keys()

    def __len__(self):
        return len(self.seasons)

    def __getitem__(self, index):
        if str(index) in self.seasons.keys():
            return self.seasons[str(index)]
        elif index in self.seriesInfo.keys():
            return self.seriesInfo[index]
        elif index == 'topSeason':
            return int(self.keys()[-1])
        elif index == 'bottomSeason':
            return int(self.keys()[0])
        else:
            return []

    def __repr__(self):
        info = self.seriesInfo['SeriesName'] + " with " + str(len(self.seasons.keys())) + " seasons.\n"
        sortedKeys = sorted(self.seasons.keys(), key=lambda key: int(key))
        for key in sortedKeys:
            info += str(self.seasons[str(key)])
        return info

    def __init__(self, seriesInfo, sortedInfo):
        self.seriesInfo = seriesInfo
        self.seasons = {}
        for key in sortedInfo.keys():
            self.seasons[key] = season(sortedInfo[key])

class TVDB:
    def logger(self, data):
        if self.debug:
            fileHandler = open(os.path.join(os.path.dirname(__file__), "history.log"), "a")
            fileHandler.write(str(data)+'\n')
            fileHandler.close()

    def sortEpisodes(self, episodes):
        sortedEpisodes = {}
        if self.debug:
            print "Sorting full list of episode(s) into seasons -> episode(s)."
            print len(episodes)
        for episode in episodes:
            if episode['SeasonNumber'] in sortedEpisodes.keys():
                sortedEpisodes[episode['SeasonNumber']][episode['EpisodeNumber']] = episode
            else:
                sortedEpisodes[episode['SeasonNumber']] = {}
                sortedEpisodes[episode['SeasonNumber']][episode['EpisodeNumber']] = episode
        if self.debug:
            print len(sortedEpisodes)
            print "Done."
        return sortedEpisodes

    def xmlShowToDict(self, xmlString):
        with open("last.xml", "wb") as temp:
            temp.write(xmlString)
        try:
            tree = ET.fromstring(xmlString)
        except Exception as error:
            print "Error: Failed to convert XML data:", error
            return {}
        data = {}
        data["series"] = {}
        data["episodes"] = []
        for item in tree:
            if item.tag == "Series":
                for child in item.getchildren():
                    data["series"][child.tag] = child.text
            elif item.tag == "Episode":
                temp = item.getchildren()
                for child in temp:
                    if child.tag == "SeasonNumber":
                        SeasonNumber = int(child.text)
                    if child.tag == "EpisodeNumber":
                        EpisodeNumber = int(child.text)
                data["episodes"].append({})
                for child in temp:
                    data["episodes"][-1][child.tag] = child.text
        return data

    def xmlSeriesToDict(self, xmlString):
        with open("last.xml", "wb") as temp:
            temp.write(xmlString)
        tree = ET.fromstring(xmlString)
        data = []
        for item in tree:
            temp = {}
            if item.tag == "Series":
                for child in item.getchildren():
                    temp[child.tag] = child.text
                data.append(temp)
        return data

    def getShowInfo(self, seriesID):
        result = False
        if self.debug:
            print "Retriving online info for series ID: " + seriesID
        path = '/series/{}/all/en.xml'.format(seriesID)
        if os.path.exists(os.path.join("/tmp/", path)):
            with open(os.path.join("/tmp/", path), mode='r') as cacheReader:
                rawData = cacheReader.read()
            print "Using cached xml data."
        if not rawData:
            rawData = getOnlineContent('http://thetvdb.com/api/' + self.apikey + )
        if rawData:
            with open(os.path.join("/tmp/", path), mode="w", buffering=None) as cacheWriter:
                cacheWriter.write(rawData)
            rawData = rawData.replace("&", "and")
            data = self.xmlShowToDict(rawData)
            if "episodes" in data.keys():
                data["episodes"] = self.sortEpisodes(data["episodes"])
                result = show(data["series"], data["episodes"])
                if self.debug:
                    print result
        return result

    def search(self, showTitle):
        if len(showTitle) > 0:
            URL = "http://thetvdb.com/api/GetSeries.php?seriesname={}".format(urllib.quote_plus(showTitle))
            if self.debug:
                print "Accessing tvdb: " + URL
            rawData = getOnlineContent(URL)
            if rawData:
                rawData = rawData.replace("&", "and")
                if self.debug:
                    print "Get search results."
                shows = []
                if self.debug:
                    print "Parsing search results..."
                shows = self.xmlSeriesToDict(rawData)
                if self.debug:
                    print "Done."
                return shows
            else:
                return False
        else:
            return False

    def askForShow(self, showTitle = False):
        while True:
            if not showTitle:
                showTitle = raw_input("Enter in show title: ")
            if self.debug:
                print "Searching for: " + showTitle
            shows = self.searchForShow(showTitle)
            if shows:
                print "Got search results."
                if len(shows) > 1:
                    for index in xrange(len(shows)):
                        print str(index+1).zfill(2) + ": " + shows[index]['SeriesName']
                    print str(len(shows)+1).zfill(2) + ": Exit"
                    while True:
                        choice = raw_input("Enter in selection: ")
                        if len(choice) > 0:
                            if choice.isdigit():
                                choice = int(choice) - 1
                                if choice >= 0:
                                    if choice == len(shows):
                                        return False
                                    else:
                                        return shows[choice]
                                else:
                                    print "Invalid selection, please try again."
                            else:
                                print "Invalid selection, please try again."
                        else:
                            print "Invalid input, please try again."
                else:
                    print "Retrieved only one show result."
                    return shows[0]
            else:
                print "No shows found, please try again."
                showTitle = False

    def __init__(self, apiKey, debug = True):
        self.debug = debug
        if os.path.exists(os.path.join(os.path.dirname(__file__), "history.log")):
            os.remove(os.path.join(os.path.dirname(__file__), "history.log"))
        self.apikey = apiKey

if __name__ == '__main__':
    debug = True
    tvdbHandler = TVDB('4E7A4FBBC8CF4D74', debug)
    showInfo = tvdbHandler.search("Doctor Who (2005)")
    if showInfo:
        seriesID = showInfo[0]['seriesid']
        showInfo = tvdbHandler.getShowInfo(seriesID)
        if showInfo:
            print showInfo[1][1]
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest
from subprocess import call

from wonderful_bing.wonderful_bing import WonderfulBing

class WonderfulBingTestCase(unittest.TestCase):
    def setUp(self):
        self.current_dir = os.path.abspath(os.path.dirname(__file__))
        self.current_dir_with_slash = self.current_dir + '/'
        self.not_exist_dir = '/test/'

    def tearDown(self):
        """delete the download picture"""
        for root, dirs, files in os.walk(self.current_dir_with_slash):
            for file in files:
                if file.endswith('.jpg'):
                    os.remove(file)

    def test_initial(self):
        print "\n----------test_initial begin----------"
        wonderful_bing = WonderfulBing({'directory': self.current_dir_with_slash})
        assert wonderful_bing.copyright
        assert wonderful_bing.picture_url
        print "----------test_initial end----------"

    def test_get_picture_name(self):
        print "\n----------test_get_picture_name begin----------"
        wonderful_bing = WonderfulBing({'directory': self.current_dir_with_slash})
        picture_name = wonderful_bing.get_picture_name()
        assert picture_name
        print "----------test_get_picture_name end----------"

    def test_common_command(self):
        print "\n----------test_common_command begin----------"
        help_message_status = call([
            'python', './wonderful_bing/wonderful_bing.py', '-h'])
        version_status = call(['python', './wonderful_bing/wonderful_bing.py',
                               '-V'])
        dir_specified_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.current_dir_with_slash)])
        assert not version_status
        assert not help_message_status
        assert not dir_specified_status
        print "----------test_common_command end----------"

    def test_picture_has_been_downloaded(self):
        print "\n----------test_picture_has_been_downloaded begin----------"
        dir_specified_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.current_dir_with_slash)])
        picture_has_been_downloaded_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.current_dir_with_slash)])
        assert not picture_has_been_downloaded_status
        print "----------test_picture_has_been_downloaded end----------"

    def test_without_directory(self):
        print "\n----------test_without_directory begin----------"
        no_dir_specified_status = call([
            'python', './wonderful_bing/wonderful_bing.py'])
        assert no_dir_specified_status
        print "----------test_without_directory end----------"

    def test_directory_not_exist(self):
        print "\n----------test_directory_not_exist begin----------"
        dir_not_exist_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.not_exist_dir)])
        assert dir_not_exist_status
        print "----------test_directory_not_exist end----------"

if __name__ == '__main__':
    unittest.main()
<commit_msg>Pep8 check for test.py<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest
from subprocess import call

from wonderful_bing.wonderful_bing import WonderfulBing

class WonderfulBingTestCase(unittest.TestCase):
    def setUp(self):
        self.current_dir = os.path.abspath(os.path.dirname(__file__))
        self.current_dir_with_slash = self.current_dir + '/'
        self.not_exist_dir = '/test/'

    def tearDown(self):
        """delete the download picture"""
        for root, dirs, files in os.walk(self.current_dir_with_slash):
            for file in files:
                if file.endswith('.jpg'):
                    os.remove(file)

    def test_initial(self):
        print "\n----------test_initial begin----------"
        wonderful_bing = WonderfulBing(
            {'directory': self.current_dir_with_slash})
        assert wonderful_bing.copyright
        assert wonderful_bing.picture_url
        print "----------test_initial end----------"

    def test_get_picture_name(self):
        print "\n----------test_get_picture_name begin----------"
        wonderful_bing = WonderfulBing(
            {'directory': self.current_dir_with_slash})
        picture_name = wonderful_bing.get_picture_name()
        assert picture_name
        print "----------test_get_picture_name end----------"

    def test_common_command(self):
        print "\n----------test_common_command begin----------"
        help_message_status = call([
            'python', './wonderful_bing/wonderful_bing.py', '-h'])
        version_status = call(['python', './wonderful_bing/wonderful_bing.py',
                               '-V'])
        dir_specified_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.current_dir_with_slash)])
        assert not version_status
        assert not help_message_status
        assert not dir_specified_status
        print "----------test_common_command end----------"

    def test_picture_has_been_downloaded(self):
        print "\n----------test_picture_has_been_downloaded begin----------"
        call(['python', './wonderful_bing/wonderful_bing.py',
              '-d', '{}'.format(self.current_dir_with_slash)])
        picture_has_been_downloaded_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.current_dir_with_slash)])
        assert not picture_has_been_downloaded_status
        print "----------test_picture_has_been_downloaded end----------"

    def test_without_directory(self):
        print "\n----------test_without_directory begin----------"
        no_dir_specified_status = call([
            'python', './wonderful_bing/wonderful_bing.py'])
        assert no_dir_specified_status
        print "----------test_without_directory end----------"

    def test_directory_not_exist(self):
        print "\n----------test_directory_not_exist begin----------"
        dir_not_exist_status = call([
            'python', './wonderful_bing/wonderful_bing.py',
            '-d', '{}'.format(self.not_exist_dir)])
        assert dir_not_exist_status
        print "----------test_directory_not_exist end----------"

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>import numpy as np, matplotlib as mpl
try:
	import vtk, vtk.util.numpy_support as npsup
	#import SimPEG.visualize.vtk.vtkTools as vtkSP # Always get an error for this import
except Exception, e:
	print 'VTK import error. Please ensure you have VTK installed to use this visualization package.'
import SimPEG as simpeg

class vtkView(object):
	"""
	Class for storing and view of SimPEG models in VTK (visualization toolkit).

	Inputs:
	:param mesh, SimPEG mesh.
	:param propdict, dictionary of property models.
		Can have these dictionary names:
		'C' - cell model; 'F' - face model; 'E' - edge model; ('V' - vector field : NOT SUPPORTED)
		The dictionary values are given as dictionaries with:
		{'NameOfThePropertyModel': np.array of the properties}.
		The property np.array has to be ordered in compliance with SimPEG standards.

	::
	Example of usages.

		ToDo

	"""

	def __init__(self,mesh,propdict):
		"""
		"""

		# Setup hidden properties, used for the visualization
		self._ren = None
		self._iren = None
		self._renwin = None
		self._core = None
		self._viewobj = None
		self._plane = None
		self._clipper = None
		self._widget = None
		self._actor = None
		self._lut = None
		# Set vtk object containers
		self._cells = None
		self._faces = None
		self._edges = None
		self._vectors = None # Not implemented
		# Set default values
		self.name = 'VTK figure of SimPEG model'

		# Error check the input mesh
		if type(mesh).__name__ != 'TensorMesh':
			raise Exception('The input {:s} to vtkView has to be a TensorMesh object'.format(mesh))
		# Set the mesh
		self._mesh = mesh

		# Read the property dictionary
		self._readPropertyDictionary(propdict)

	# Set/Get properties
	@property
	def cmap(self):
		''' Colormap to use in vtkView. Colormap is a matplotlib cmap(cm) array, has to be uint8(use flag bytes=True during cmap generation).'''
		if getattr(self,'_cmap',None) is None:
			# Set default
			self._cmap = mpl.cm.hsv(np.arange(0.,1.,0.05),bytes=True)
		return self._cmap
	@cmap.setter
	def cmap(self,value):
		if value.min() > 0 or value.max() < 255 or value.shape[1] != 4 or value.dtype != np.uint8:
			raise Exception('Input not an allowed array.\n Use matplotlib.cm to generate an array of size [nrColors,4] and dtype = uint8(flag bytes=True).')
		self._cmap = value

	@property
	def range(self):
		''' Range of the colors in vtkView.'''
		if getattr(self,'_range',None) is None:
			self._range = np.array(self._getActiveVTKobj().GetArray(self.viewprop.values()[0]).GetRange())
		return self._range
	@range.setter
	def range(self,value):
		if type(value) not in [tuple, list, np.ndarray] or len(value) != 2 or np.array(value).dtype is not np.dtype('float'):
			raise Exception('Input not in correct format. \n Has to be a list, tuple or np.arry of 2 floats.')
		self._range = np.array(value)

	@property
	def extent(self):
		''' Extent of the sub-domain of the model to view'''
		if getattr(self,'_extent',None) is None:
			self._extent = [0,self._mesh.nCx-1,0,self._mesh.nCy-1,0,self._mesh.nCz-1]
		return self._extent
	@extent.setter
	def extent(self,value):

		import warnings
		# Error check
		valnp = np.array(value,dtype=int)
		if valnp.dtype != int or len(valnp) != 6:
			raise Exception('.extent has to be list or nparray of 6 integers.')
		# Test the range of the values
		loB = np.zeros(3,dtype=int)
		upB = np.array(self._mesh.nCv - np.ones(3),dtype=int)
		# Test the bounds
		change = 0
		# Test for lower bounds, can't be smaller the 0
		tlb = valnp[::2] < loB
		if tlb.any():
			valnp[::2][tlb] = loB[tlb]
			change = 1
			warnings.warn('Lower bounds smaller then 0')
		# Test for lower bounds, can't be larger then upB
		tlub = valnp[::2] > upB
		if tlub.any():
			valnp[::2][tlub] = upB[tlub] - 1
			change = 1
			warnings.warn('Lower bounds larger then uppermost bounds')
		# Test for upper bounds, can't be larger the extent of the mesh
		tub = valnp[1::2] > upB
		if tub.any():
			valnp[1::2][tub] = upB[tub]
			change = 1
			warnings.warn('Upper bounds greater then number of cells')
		# Test if lower is smaller the upper
		tgt = valnp[::2] > valnp[1::2]
		if tgt.any():
			valnp[1::2][tgt] = valnp[::2][tgt] + 1
			change = 1
			warnings.warn('Lower bounds greater the Upper bounds')
		# Print a warning
		if change:
			warnings.warn('Changed given extent from {:s} to {:s}'.format(value,valnp.tolist()))

		# Set extent
		self._extent = valnp

	@property
	def limits(self):
		''' Lower and upper limits (cutoffs) of the values to view. '''
		return getattr(self,'_limits',None)
	@limits.setter
	def limits(self,value):
		if value is None:
			self._limits = None
		else:
			valnp = np.array(value)
			if valnp.dtype != float or len(valnp) != 2:
				raise Exception('.limits has to be list or numpy array of 2 floats.')
			self._limits = valnp

	@property
	def viewprop(self):
		''' Controls the property that will be viewed.'''

		if getattr(self,'_viewprop',None) is None:
			self._viewprop = {'C':0} # Name of the type and Int order of the array or name of the vector.
		return self._viewprop
	@viewprop.setter
	def viewprop(self,value):
		if type(value) != dict:
			raise Exception('{:s} has to be a python dictionary containing property type and name index. ')
		if len(value) > 1:
			raise Exception('Too many input items in the viewprop dictionary')
		if value.keys()[0] not in ['C','F','E']:
			raise Exception('\"{:s}\" is not allowed as a dictionary key. Can be \'C\',\'F\',\'E\'.'.format(propitem[0]))
		if not(type(self.viewprop.values()[0]) is int or type(self.viewprop.values()[0]) is str):
			raise Exception('The vtkView.viewprop.values()[0] has the wrong format. Has to be integer or a string with the index.')

		self._viewprop = value

	def _getActiveVTKobj(self):
		"""
		Finds the active VTK object.
		"""

		if self.viewprop.keys()[0] is 'C':
			vtkCellData = self._cells.GetCellData()
		elif self.viewprop.keys()[0] is 'F':
			vtkCellData = self._faces.GetCellData()
		elif self.viewprop.keys()[0] is 'E':
			vtkCellData = self._edges.GetCellData()

		return vtkCellData

	def _readPropertyDictionary(self,propdict):
		"""
		Reads the property and assigns to the object
		"""
		import SimPEG.visualize.vtk.vtkTools as vtkSP

		# Test the property dictionary
		if type(propdict) != dict:
			raise Exception('{:s} has to be a python dictionary containing property models. ')
		if len(propdict) > 4:
			raise Exception('Too many input items in the property dictionary')
		for propitem in propdict.iteritems():
			if propitem[0] in ['C','F','E']:
				if propitem[0] == 'C':
					self._cells = vtkSP.makeCellVTKObject(self._mesh,propitem[1])
				if propitem[0] == 'F':
					self._faces = vtkSP.makeFaceVTKObject(self._mesh,propitem[1])
				if propitem[0] == 'E':
					self._edges = vtkSP.makeEdgeVTKObject(self._mesh,propitem[1])
			else:
				raise Exception('\"{:s}\" is not allowed as a dictionary key. Can be \'C\',\'F\',\'E\'.'.format(propitem[0]))

	def Show(self):
		"""
		Open the VTK figure window and show the mesh.
		"""
		#vtkSP = simpeg.visualize.vtk.vtkTools
		import SimPEG.visualize.vtk.vtkTools as vtkSP

		# Make a renderer
		self._ren = vtk.vtkRenderer()
		# Make renderwindow. Returns the interactor.
		self._iren, self._renwin = vtkSP.makeRenderWindow(self._ren)

		# Set the active scalar.
		if type(self.viewprop.values()[0]) == int:
			actScalar = self._getActiveVTKobj().GetArrayName(self.viewprop.values()[0])
		elif type(self.viewprop.values()[0]) == str:
			actScalar = self.viewprop.values()[0]
		else :
			raise Exception('The vtkView.viewprop.values()[0] has the wrong format. Has to be interger or a string.')
		self._getActiveVTKobj().SetActiveScalars(actScalar)
		# Sort out the actor
		imageType = self.viewprop.keys()[0]
		if imageType == 'C':
			if self.limits is None:
				self.limits = self._cells.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			self._vtkobj, self._core = vtkSP.makeRectiVTKVOIThres(self._cells,self.extent,self.limits)
		elif imageType == 'F':
			if self.limits is None:
				self.limits = self._faces.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			extent = [self._mesh.vectorNx[self.extent[0]], self._mesh.vectorNx[self.extent[1]], self._mesh.vectorNy[self.extent[2]], self._mesh.vectorNy[self.extent[3]], self._mesh.vectorNz[self.extent[4]], self._mesh.vectorNz[self.extent[5]] ]
			self._vtkobj, self._core = vtkSP.makeUnstructVTKVOIThres(self._faces,extent,self.limits)
		elif imageType == 'E':
			if self.limits is None:
				self.limits = self._edges.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			extent = [self._mesh.vectorNx[self.extent[0]], self._mesh.vectorNx[self.extent[1]], self._mesh.vectorNy[self.extent[2]], self._mesh.vectorNy[self.extent[3]], self._mesh.vectorNz[self.extent[4]], self._mesh.vectorNz[self.extent[5]] ]
			self._vtkobj, self._core = vtkSP.makeUnstructVTKVOIThres(self._edges,extent,self.limits)
		else:
			raise Exception("{:s} is not a valid viewprop. Has to be 'C':'F':'E'".format(imageType))
		#self._vtkobj.GetCellData().SetActiveScalars(actScalar)
		# Set up the plane, clipper and the user interaction.
		global intPlane, intActor
		self._clipper, intPlane = vtkSP.makePlaneClipper(self._vtkobj)
		intActor = vtkSP.makeVTKLODActor(self._vtkobj,self._clipper)
		self._widget = vtkSP.makePlaneWidget(self._vtkobj,self._iren,self._clipper.GetClipFunction(),self._actor)
		# Callback function
		self._plane = intPlane
		self._actor = intActor
		def movePlane(obj, events):
			global intPlane, intActor
			obj.GetPlane(intPlane)
			intActor.VisibilityOn()

		self._widget.AddObserver("InteractionEvent",movePlane)
		lut = vtk.vtkLookupTable()
		lut.SetNumberOfColors(len(self.cmap))
		lut.SetTable(npsup.numpy_to_vtk(self.cmap))
		lut.Build()
		self._lut = lut
		self._actor.GetMapper().SetScalarRange(self.range)
		self._actor.GetMapper().SetLookupTable(lut)

		# Set renderer options
		self._ren.SetBackground(.5,.5,.5)
		self._ren.AddActor(self._actor)

		# Start the render Window
		vtkSP.startRenderWindow(self._iren)
		# Close the window when exited
		vtkSP.closeRenderWindow(self._iren)
		del self._iren, self._renwin

if __name__ == '__main__':

	#Make a mesh and model
	x0 = np.zeros(3)
	h1 = np.ones(20)*50
	h2 = np.ones(10)*100
	h3 = np.ones(5)*200

	mesh = simpeg.mesh.TensorMesh([h1,h2,h3],x0)

	# Make a models that correspond to the cells, faces and edges.
	models = {'C':{'Test':np.arange(0,mesh.nC),'AllOnce':np.ones(mesh.nC)},'F':{'Test':np.arange(0,mesh.nF),'AllOnce':np.ones(mesh.nF)},'E':{'Test':np.arange(0,mesh.nE),'AllOnce':np.ones(mesh.nE)}}
	# Make the vtk viewer object.
	vtkViewer = simpeg.visualize.vtk.vtkView(mesh,models)
	# Set the .viewprop for which model to view
	vtkViewer.viewprop = {'F':'Test'}
	# Show the image
	vtkViewer.Show()

	# Set subset of the mesh to view (remove padding)
	vtkViewer.extent = [4,14,0,7,0,3]
	vtkViewer.Show()
<commit_msg>Added a colorbar to the figure.<commit_after>import numpy as np, matplotlib as mpl
try:
	import vtk, vtk.util.numpy_support as npsup
	#import SimPEG.visualize.vtk.vtkTools as vtkSP # Always get an error for this import
except Exception, e:
	print 'VTK import error. Please ensure you have VTK installed to use this visualization package.'
import SimPEG as simpeg

class vtkView(object):
	"""
	Class for storing and view of SimPEG models in VTK (visualization toolkit).

	Inputs:
	:param mesh, SimPEG mesh.
	:param propdict, dictionary of property models.
		Can have these dictionary names:
		'C' - cell model; 'F' - face model; 'E' - edge model; ('V' - vector field : NOT SUPPORTED)
		The dictionary values are given as dictionaries with:
		{'NameOfThePropertyModel': np.array of the properties}.
		The property np.array has to be ordered in compliance with SimPEG standards.

	::
	Example of usages.

		ToDo

	"""

	def __init__(self,mesh,propdict):
		"""
		"""

		# Setup hidden properties, used for the visualization
		self._ren = None
		self._iren = None
		self._renwin = None
		self._core = None
		self._viewobj = None
		self._plane = None
		self._clipper = None
		self._widget = None
		self._actor = None
		self._lut = None
		# Set vtk object containers
		self._cells = None
		self._faces = None
		self._edges = None
		self._vectors = None # Not implemented
		# Set default values
		self.name = 'VTK figure of SimPEG model'

		# Error check the input mesh
		if type(mesh).__name__ != 'TensorMesh':
			raise Exception('The input {:s} to vtkView has to be a TensorMesh object'.format(mesh))
		# Set the mesh
		self._mesh = mesh

		# Read the property dictionary
		self._readPropertyDictionary(propdict)

	# Set/Get properties
	@property
	def cmap(self):
		''' Colormap to use in vtkView. Colormap is a matplotlib cmap(cm) array, has to be uint8(use flag bytes=True during cmap generation).'''
		if getattr(self,'_cmap',None) is None:
			# Set default
			self._cmap = mpl.cm.hsv(np.arange(0.,1.,0.05),bytes=True)
		return self._cmap
	@cmap.setter
	def cmap(self,value):
		if value.min() > 0 or value.max() < 255 or value.shape[1] != 4 or value.dtype != np.uint8:
			raise Exception('Input not an allowed array.\n Use matplotlib.cm to generate an array of size [nrColors,4] and dtype = uint8(flag bytes=True).')
		self._cmap = value

	@property
	def range(self):
		''' Range of the colors in vtkView.'''
		if getattr(self,'_range',None) is None:
			self._range = np.array(self._getActiveVTKobj().GetArray(self.viewprop.values()[0]).GetRange())
		return self._range
	@range.setter
	def range(self,value):
		if type(value) not in [tuple, list, np.ndarray] or len(value) != 2 or np.array(value).dtype is not np.dtype('float'):
			raise Exception('Input not in correct format. \n Has to be a list, tuple or np.arry of 2 floats.')
		self._range = np.array(value)

	@property
	def extent(self):
		''' Extent of the sub-domain of the model to view'''
		if getattr(self,'_extent',None) is None:
			self._extent = [0,self._mesh.nCx-1,0,self._mesh.nCy-1,0,self._mesh.nCz-1]
		return self._extent
	@extent.setter
	def extent(self,value):

		import warnings
		# Error check
		valnp = np.array(value,dtype=int)
		if valnp.dtype != int or len(valnp) != 6:
			raise Exception('.extent has to be list or nparray of 6 integers.')
		# Test the range of the values
		loB = np.zeros(3,dtype=int)
		upB = np.array(self._mesh.nCv - np.ones(3),dtype=int)
		# Test the bounds
		change = 0
		# Test for lower bounds, can't be smaller the 0
		tlb = valnp[::2] < loB
		if tlb.any():
			valnp[::2][tlb] = loB[tlb]
			change = 1
			warnings.warn('Lower bounds smaller then 0')
		# Test for lower bounds, can't be larger then upB
		tlub = valnp[::2] > upB
		if tlub.any():
			valnp[::2][tlub] = upB[tlub] - 1
			change = 1
			warnings.warn('Lower bounds larger then uppermost bounds')
		# Test for upper bounds, can't be larger the extent of the mesh
		tub = valnp[1::2] > upB
		if tub.any():
			valnp[1::2][tub] = upB[tub]
			change = 1
			warnings.warn('Upper bounds greater then number of cells')
		# Test if lower is smaller the upper
		tgt = valnp[::2] > valnp[1::2]
		if tgt.any():
			valnp[1::2][tgt] = valnp[::2][tgt] + 1
			change = 1
			warnings.warn('Lower bounds greater the Upper bounds')
		# Print a warning
		if change:
			warnings.warn('Changed given extent from {:s} to {:s}'.format(value,valnp.tolist()))

		# Set extent
		self._extent = valnp

	@property
	def limits(self):
		''' Lower and upper limits (cutoffs) of the values to view. '''
		return getattr(self,'_limits',None)
	@limits.setter
	def limits(self,value):
		if value is None:
			self._limits = None
		else:
			valnp = np.array(value)
			if valnp.dtype != float or len(valnp) != 2:
				raise Exception('.limits has to be list or numpy array of 2 floats.')
			self._limits = valnp

	@property
	def viewprop(self):
		''' Controls the property that will be viewed.'''

		if getattr(self,'_viewprop',None) is None:
			self._viewprop = {'C':0} # Name of the type and Int order of the array or name of the vector.
		return self._viewprop
	@viewprop.setter
	def viewprop(self,value):
		if type(value) != dict:
			raise Exception('{:s} has to be a python dictionary containing property type and name index. ')
		if len(value) > 1:
			raise Exception('Too many input items in the viewprop dictionary')
		if value.keys()[0] not in ['C','F','E']:
			raise Exception('\"{:s}\" is not allowed as a dictionary key. Can be \'C\',\'F\',\'E\'.'.format(propitem[0]))
		if not(type(self.viewprop.values()[0]) is int or type(self.viewprop.values()[0]) is str):
			raise Exception('The vtkView.viewprop.values()[0] has the wrong format. Has to be integer or a string with the index.')

		self._viewprop = value

	def _getActiveVTKobj(self):
		"""
		Finds the active VTK object.
		"""

		if self.viewprop.keys()[0] is 'C':
			vtkCellData = self._cells.GetCellData()
		elif self.viewprop.keys()[0] is 'F':
			vtkCellData = self._faces.GetCellData()
		elif self.viewprop.keys()[0] is 'E':
			vtkCellData = self._edges.GetCellData()

		return vtkCellData

	def _getActiveArrayName(self):
		"""
		Finds the name of the active array.
		"""
		actArr = self.viewprop.values()[0]
		if type(actArr) is str:
			activeName = actArr
		elif type(actArr) is int:
			activeName = self._getActiveVTKobj().GetArrayName(actArr)
		return activeName

	def _readPropertyDictionary(self,propdict):
		"""
		Reads the property and assigns to the object
		"""
		import SimPEG.visualize.vtk.vtkTools as vtkSP

		# Test the property dictionary
		if type(propdict) != dict:
			raise Exception('{:s} has to be a python dictionary containing property models. ')
		if len(propdict) > 4:
			raise Exception('Too many input items in the property dictionary')
		for propitem in propdict.iteritems():
			if propitem[0] in ['C','F','E']:
				if propitem[0] == 'C':
					self._cells = vtkSP.makeCellVTKObject(self._mesh,propitem[1])
				if propitem[0] == 'F':
					self._faces = vtkSP.makeFaceVTKObject(self._mesh,propitem[1])
				if propitem[0] == 'E':
					self._edges = vtkSP.makeEdgeVTKObject(self._mesh,propitem[1])
			else:
				raise Exception('\"{:s}\" is not allowed as a dictionary key. Can be \'C\',\'F\',\'E\'.'.format(propitem[0]))

	def Show(self):
		"""
		Open the VTK figure window and show the mesh.
		"""
		#vtkSP = simpeg.visualize.vtk.vtkTools
		import SimPEG.visualize.vtk.vtkTools as vtkSP

		# Make a renderer
		self._ren = vtk.vtkRenderer()
		# Make renderwindow. Returns the interactor.
		self._iren, self._renwin = vtkSP.makeRenderWindow(self._ren)

		# Set the active scalar.
		if type(self.viewprop.values()[0]) == int:
			actScalar = self._getActiveVTKobj().GetArrayName(self.viewprop.values()[0])
		elif type(self.viewprop.values()[0]) == str:
			actScalar = self.viewprop.values()[0]
		else :
			raise Exception('The vtkView.viewprop.values()[0] has the wrong format. Has to be interger or a string.')
		self._getActiveVTKobj().SetActiveScalars(actScalar)
		# Sort out the actor
		imageType = self.viewprop.keys()[0]
		if imageType == 'C':
			if self.limits is None:
				self.limits = self._cells.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			self._vtkobj, self._core = vtkSP.makeRectiVTKVOIThres(self._cells,self.extent,self.limits)
		elif imageType == 'F':
			if self.limits is None:
				self.limits = self._faces.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			extent = [self._mesh.vectorNx[self.extent[0]], self._mesh.vectorNx[self.extent[1]], self._mesh.vectorNy[self.extent[2]], self._mesh.vectorNy[self.extent[3]], self._mesh.vectorNz[self.extent[4]], self._mesh.vectorNz[self.extent[5]] ]
			self._vtkobj, self._core = vtkSP.makeUnstructVTKVOIThres(self._faces,extent,self.limits)
		elif imageType == 'E':
			if self.limits is None:
				self.limits = self._edges.GetCellData().GetArray(self.viewprop.values()[0]).GetRange()
			extent = [self._mesh.vectorNx[self.extent[0]], self._mesh.vectorNx[self.extent[1]], self._mesh.vectorNy[self.extent[2]], self._mesh.vectorNy[self.extent[3]], self._mesh.vectorNz[self.extent[4]], self._mesh.vectorNz[self.extent[5]] ]
			self._vtkobj, self._core = vtkSP.makeUnstructVTKVOIThres(self._edges,extent,self.limits)
		else:
			raise Exception("{:s} is not a valid viewprop. Has to be 'C':'F':'E'".format(imageType))
		#self._vtkobj.GetCellData().SetActiveScalars(actScalar)
		# Set up the plane, clipper and the user interaction.
		global intPlane, intActor
		self._clipper, intPlane = vtkSP.makePlaneClipper(self._vtkobj)
		intActor = vtkSP.makeVTKLODActor(self._vtkobj,self._clipper)
		self._widget = vtkSP.makePlaneWidget(self._vtkobj,self._iren,self._clipper.GetClipFunction(),self._actor)
		# Callback function
		self._plane = intPlane
		self._actor = intActor
		def movePlane(obj, events):
			global intPlane, intActor
			obj.GetPlane(intPlane)
			intActor.VisibilityOn()

		self._widget.AddObserver("InteractionEvent",movePlane)
		lut = vtk.vtkLookupTable()
		lut.SetNumberOfColors(len(self.cmap))
		lut.SetTable(npsup.numpy_to_vtk(self.cmap))
		lut.Build()
		self._lut = lut
		scalarBar = vtk.vtkScalarBarActor()
  		scalarBar.SetLookupTable(lut)
 		scalarBar.SetTitle(self._getActiveArrayName())
  		scalarBar.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
  		scalarBar.GetPositionCoordinate().SetValue(0.1,0.01)
  		scalarBar.SetOrientationToHorizontal()
  		scalarBar.SetWidth(0.8)
  		scalarBar.SetHeight(0.17)

		self._actor.GetMapper().SetScalarRange(self.range)
		self._actor.GetMapper().SetLookupTable(lut)

		# Set renderer options
		self._ren.SetBackground(.5,.5,.5)
		self._ren.AddActor(self._actor)
		self._ren.AddActor2D(scalarBar)
  		self._renwin.SetSize(450,450)

		# Start the render Window
		vtkSP.startRenderWindow(self._iren)
		# Close the window when exited
		vtkSP.closeRenderWindow(self._iren)
		del self._iren, self._renwin

if __name__ == '__main__':

	#Make a mesh and model
	x0 = np.zeros(3)
	h1 = np.ones(20)*50
	h2 = np.ones(10)*100
	h3 = np.ones(5)*200

	mesh = simpeg.mesh.TensorMesh([h1,h2,h3],x0)

	# Make a models that correspond to the cells, faces and edges.
	models = {'C':{'Test':np.arange(0,mesh.nC),'AllOnce':np.ones(mesh.nC)},'F':{'Test':np.arange(0,mesh.nF),'AllOnce':np.ones(mesh.nF)},'E':{'Test':np.arange(0,mesh.nE),'AllOnce':np.ones(mesh.nE)}}
	# Make the vtk viewer object.
	vtkViewer = simpeg.visualize.vtk.vtkView(mesh,models)
	# Set the .viewprop for which model to view
	vtkViewer.viewprop = {'F':'Test'}
	# Show the image
	vtkViewer.Show()

	# Set subset of the mesh to view (remove padding)
	vtkViewer.extent = [4,14,0,7,0,3]
	vtkViewer.Show()
<|endoftext|>


<commit_before>"""Dependency injector."""

from .catalog import AbstractCatalog
from .catalog import override

from .providers import Provider
from .providers import Delegate
from .providers import Factory
from .providers import Singleton
from .providers import ExternalDependency
from .providers import Class
from .providers import Object
from .providers import Function
from .providers import Value
from .providers import Callable
from .providers import Config

from .injections import KwArg
from .injections import Attribute
from .injections import Method
from .injections import inject

from .errors import Error

__all__ = ('AbstractCatalog',

           # Providers
           'Provider',
           'Delegate',
           'Factory',
           'Singleton',
           'ExternalDependency',
           'Class',
           'Object',
           'Function',
           'Value',
           'Callable',
           'Config',

           # Injections
           'KwArg',
           'Attribute',
           'Method',

           # Decorators
           'override',
           'inject',

           # Errors
           'Error')
<commit_msg>Add additional shortcuts for top level package<commit_after>"""Dependency injector."""

from .catalog import AbstractCatalog
from .catalog import override

from .providers import Provider
from .providers import Delegate
from .providers import Factory
from .providers import Singleton
from .providers import ExternalDependency
from .providers import Class
from .providers import Object
from .providers import Function
from .providers import Value
from .providers import Callable
from .providers import Config

from .injections import Injection
from .injections import KwArg
from .injections import Attribute
from .injections import Method
from .injections import inject

from .utils import is_provider
from .utils import ensure_is_provider
from .utils import is_injection
from .utils import ensure_is_injection
from .utils import is_kwarg_injection
from .utils import is_attribute_injection
from .utils import is_method_injection

from .errors import Error

__all__ = (
    # Catalogs
    'AbstractCatalog',
    'override',

    # Providers
    'Provider',
    'Delegate',
    'Factory',
    'Singleton',
    'ExternalDependency',
    'Class',
    'Object',
    'Function',
    'Value',
    'Callable',
    'Config',

    # Injections
    'KwArg',
    'Attribute',
    'Method',
    'inject',

    # Utils
    'is_provider',
    'ensure_is_provider',
    'is_injection',
    'ensure_is_injection',
    'is_kwarg_injection',
    'is_attribute_injection',
    'is_method_injection',

    # Errors
    'Error',
)
<|endoftext|>