text:
from __future__ import print_function, division, absolute_import

import unittest

import numpy as np

from openmdao.api import Problem, Group, IndepVarComp
from openconcept.utilities.math.combine_split_comp import VectorConcatenateComp, VectorSplitComp
from openmdao.utils.assert_utils import assert_rel_error, assert_check_partials

class TestConcatenateScalars(unittest.TestCase):

    def setUp(self):
        self.nn = 1
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,))
        ivc.add_output(name='b', shape=(self.nn,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b'],vec_sizes=[1,1])

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,)
        self.p['b'] = np.random.rand(self.nn,)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateNx1(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,))
        ivc.add_output(name='b', shape=(self.nn,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b'],vec_sizes=[self.nn,self.nn])

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,)
        self.p['b'] = np.random.rand(self.nn,)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateNx3(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.length = 3
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,self.length))
        ivc.add_output(name='b', shape=(self.nn,self.length))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b'],vec_sizes=[self.nn,self.nn],length=self.length)

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,self.length)
        self.p['b'] = np.random.rand(self.nn,self.length)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateInitMethod(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.length = 3
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,self.length))
        ivc.add_output(name='b', shape=(self.nn,self.length))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp('concat_output',['input_a','input_b'],vec_sizes=[self.nn,self.nn],length=self.length))
        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,self.length)
        self.p['b'] = np.random.rand(self.nn,self.length)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateMultipleSystems(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.length = 3
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,self.length))
        ivc.add_output(name='b', shape=(self.nn,self.length))
        ivc.add_output(name='c', shape=(3,))
        ivc.add_output(name='d', shape=(4,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b','c','d'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output1',['input_a','input_b'],vec_sizes=[self.nn,self.nn],length=self.length)
        combiner.add_relation('concat_output2',['input_c','input_d'],vec_sizes=[3,4])

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')
        self.p.model.connect('c', 'vector_concat_comp.input_c')
        self.p.model.connect('d', 'vector_concat_comp.input_d')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,self.length)
        self.p['b'] = np.random.rand(self.nn,self.length)
        self.p['c'] = np.random.rand(3,)
        self.p['d'] = np.random.rand(4,)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        c = self.p['c']
        d = self.p['d']

        out1 = self.p['vector_concat_comp.concat_output1']
        out2 = self.p['vector_concat_comp.concat_output2']

        expected1 = np.concatenate((a,b))
        expected2 = np.concatenate((c,d))
        assert_rel_error(self, out1, expected1,1e-16)
        assert_rel_error(self, out2, expected2,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateNx3Units(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.length = 3
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,self.length), units='m')
        ivc.add_output(name='b', shape=(self.nn,self.length), units='km')

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b'],vec_sizes=[self.nn,self.nn],length=self.length, units='m')

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,self.length)
        self.p['b'] = np.random.rand(self.nn,self.length)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        b = b*1000.
        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b))
        assert_rel_error(self, out, expected,1e-16)

class TestConcatenate3InputsDiffSizesNx1(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,))
        ivc.add_output(name='b', shape=(self.nn,))
        ivc.add_output(name='c', shape=(3,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b','c'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b','input_c'],vec_sizes=[self.nn,self.nn,3])

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')
        self.p.model.connect('c', 'vector_concat_comp.input_c')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,)
        self.p['b'] = np.random.rand(self.nn,)
        self.p['c'] = np.random.rand(3,)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        c = self.p['c']

        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b,c))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenate3InputsDiffSizesNx3(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.length = 3
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,self.length))
        ivc.add_output(name='b', shape=(self.nn,self.length))
        ivc.add_output(name='c', shape=(3,self.length))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b','c'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b','input_c'],vec_sizes=[self.nn,self.nn,3],length=self.length)

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')
        self.p.model.connect('c', 'vector_concat_comp.input_c')

        self.p.setup()

        self.p['a'] = np.random.rand(self.nn,self.length)
        self.p['b'] = np.random.rand(self.nn,self.length)
        self.p['c'] = np.random.rand(3,self.length)

        self.p.run_model()

    def test_results(self):
        a = self.p['a']
        b = self.p['b']
        c = self.p['c']

        out = self.p['vector_concat_comp.concat_output']
        expected = np.concatenate((a,b,c))
        assert_rel_error(self, out, expected,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestConcatenateWrongVecSizesInputMismatch(unittest.TestCase):
    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='a', shape=(self.nn,))
        ivc.add_output(name='b', shape=(self.nn,))
        ivc.add_output(name='c', shape=(3,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['a', 'b','c'])

        combiner=self.p.model.add_subsystem(name='vector_concat_comp',
                                   subsys=VectorConcatenateComp())
        combiner.add_relation('concat_output',['input_a','input_b','input_c'],vec_sizes=[self.nn,self.nn])

        self.p.model.connect('a', 'vector_concat_comp.input_a')
        self.p.model.connect('b', 'vector_concat_comp.input_b')
        self.p.model.connect('c', 'vector_concat_comp.input_c')

    def test_for_exception(self):
        self.assertRaises(ValueError,self.p.setup)

class TestSplitScalars(unittest.TestCase):

    def setUp(self):
        self.nn = 1
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b'],'input_to_split',vec_sizes=[1,1])

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2,)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']

        expected_a = input_to_split[0]
        expected_b = input_to_split[1]
        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitNx1(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b'],'input_to_split',vec_sizes=[self.nn,self.nn])

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2,)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']

        expected_a = input_to_split[0:self.nn]
        expected_b = input_to_split[self.nn:2*self.nn]
        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitNx3(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,3))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b'],'input_to_split',vec_sizes=[self.nn,self.nn],length=3)

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2,3)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']

        expected_a = input_to_split[0:self.nn,:]
        expected_b = input_to_split[self.nn:2*self.nn,:]
        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitInitMethod(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,3))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp(['output_a','output_b'],'input_to_split',vec_sizes=[self.nn,self.nn],length=3))
        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2,3)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']

        expected_a = input_to_split[0:self.nn,:]
        expected_b = input_to_split[self.nn:2*self.nn,:]
        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitMultipleSystems(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2+2,3))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b','output_c'],'input_to_split',vec_sizes=[self.nn,self.nn,2],length=3)

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2+2,3)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']
        out_c = self.p['vector_split_comp.output_c']

        expected_a = input_to_split[0:self.nn,:]
        expected_b = input_to_split[self.nn:2*self.nn,:]
        expected_c = input_to_split[2*self.nn:2*self.nn+2,:]

        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)
        assert_rel_error(self, out_c, expected_c,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitNx3Units(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,3), units='m')

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b'],'input_to_split',vec_sizes=[self.nn,self.nn],length=3, units='m')

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')
        self.p.setup()
        self.p['input_to_split'] = np.random.rand(self.nn*2,3)
        self.p.run_model()

    def test_results(self):
        input_to_split = self.p['input_to_split']
        out_a = self.p['vector_split_comp.output_a']
        out_b = self.p['vector_split_comp.output_b']

        expected_a = input_to_split[0:self.nn,:]
        expected_b = input_to_split[self.nn:2*self.nn,:]
        assert_rel_error(self, out_a, expected_a,1e-16)
        assert_rel_error(self, out_b, expected_b,1e-16)

    def test_partials(self):
        partials = self.p.check_partials(method='fd', out_stream=None)
        assert_check_partials(partials)

class TestSplitWrongVecSizesOutputMismatch(unittest.TestCase):

    def setUp(self):
        self.nn = 5
        self.p = Problem(model=Group())
        ivc = IndepVarComp()
        ivc.add_output(name='input_to_split', shape=(self.nn*2,3))

        self.p.model.add_subsystem(name='ivc',
                                   subsys=ivc,
                                   promotes_outputs=['input_to_split'])

        splitter=self.p.model.add_subsystem(name='vector_split_comp',
                                   subsys=VectorSplitComp())
        splitter.add_relation(['output_a','output_b'],'input_to_split',vec_sizes=[self.nn],length=3)

        self.p.model.connect('input_to_split', 'vector_split_comp.input_to_split')

    def test_for_exception(self):
        self.assertRaises(ValueError,self.p.setup)

class TestForDocs(unittest.TestCase):

    def test(self):
        """
        A simple example to illustrate usage of the concatenate/split components.

        Let's say we have two analysis tools which generate lists of test conditions to analyze.
        For this example, our two tools takeoff analysis and cruise analysis for an airplane.
        We'll generate altitudes and velocity vectors from two (fake) components, then combine them into one
        so we can feed a single vector to some other analysis tool.

        Once the tool is done, we'll split the results back out into takeoff and cruise phases.
        """
        import numpy as np
        from openmdao.api import Problem, Group, IndepVarComp
        from openmdao.utils.assert_utils import assert_rel_error
        from openconcept.utilities.math.combine_split_comp import VectorConcatenateComp, VectorSplitComp

        n_takeoff_pts = 3
        n_cruise_pts = 5

        p = Problem(model=Group())

        takeoff_conditions = IndepVarComp()
        takeoff_conditions.add_output(name='velocity', shape=(n_takeoff_pts,2), units='m/s')
        takeoff_conditions.add_output(name='altitude', shape=(n_takeoff_pts,), units='m')

        cruise_conditions = IndepVarComp()
        cruise_conditions.add_output(name='velocity', shape=(n_cruise_pts,2), units='m/s')
        cruise_conditions.add_output(name='altitude', shape=(n_cruise_pts,), units='m')

        p.model.add_subsystem(name='takeoff_conditions',
                              subsys=takeoff_conditions)
        p.model.add_subsystem(name='cruise_conditions',
                              subsys=cruise_conditions)

        combiner=p.model.add_subsystem(name='combiner',
                                            subsys=VectorConcatenateComp())

        combiner.add_relation('velocity',['to_vel','cruise_vel'],
                              vec_sizes=[3,5],length=2, units='m/s')

        combiner.add_relation('altitude',['to_alt','cruise_alt'],
                              vec_sizes=[3,5], units='m')

        p.model.connect('takeoff_conditions.velocity', 'combiner.to_vel')
        p.model.connect('cruise_conditions.velocity', 'combiner.cruise_vel')
        p.model.connect('takeoff_conditions.altitude', 'combiner.to_alt')
        p.model.connect('cruise_conditions.altitude', 'combiner.cruise_alt')

        divider=p.model.add_subsystem(name='divider',
                                            subsys=VectorSplitComp())
        divider.add_relation(['to_vel','cruise_vel'],'velocity',
                              vec_sizes=[3,5],length=2, units='m/s')
        p.model.connect('combiner.velocity','divider.velocity')

        p.setup()

        #set thrust to exceed drag, weight to equal lift for this scenario
        p['takeoff_conditions.velocity'][:,0] = [30, 40, 50]
        p['takeoff_conditions.velocity'][:,1] = [0, 0, 0]
        p['cruise_conditions.velocity'][:,0] = [60, 60, 60, 60, 60]
        p['cruise_conditions.velocity'][:,1] = [5, 0, 0, 0, -5]
        p['takeoff_conditions.altitude'][:] = [0, 0, 0]
        p['cruise_conditions.altitude'][:] = [6000,7500,8000,8500,5000]

        p.run_model()

        # Verify the results
        expected_vel = np.array([[30, 0], [40, 0], [50, 0], [60, 5], [60, 0], [60, 0], [60, 0], [60, -5]])
        expected_alt = np.array([0, 0, 0, 6000, 7500, 8000, 8500, 5000])
        expected_split_vel = np.array([[60, 5], [60, 0], [60, 0], [60, 0], [60, -5]])
        assert_rel_error(self, p.get_val('combiner.velocity', units='m/s'), expected_vel)
        assert_rel_error(self, p.get_val('combiner.altitude', units='m'), expected_alt)
        assert_rel_error(self, p.get_val('divider.cruise_vel', units='m/s'), expected_split_vel)

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: bbrelje/openconcept

max_issues_repo_name: bbrelje/openconcept

max_forks_repo_name: bbrelje/openconcept


text:
import lenstronomy.Util.util as util
import lenstronomy.Util.simulation_util as sim_util
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
from lenstronomy.LensModel.lens_model_extensions import LensModelExtensions
from lenstronomy.LensModel.Profiles.curved_arc import center_deflector
from lenstronomy.Data.imaging_data import ImageData
from lenstronomy.Plots import plot_util
import scipy.ndimage as ndimage

from lenstronomy.Util.package_util import exporter
export, __all__ = exporter()

#TODO define coordinate grid beforehand, e.g. kwargs_data

@export
def lens_model_plot(ax, lensModel, kwargs_lens, numPix=500, deltaPix=0.01, sourcePos_x=0, sourcePos_y=0,
                    point_source=False, with_caustics=False, coord_center_ra=0, coord_center_dec=0,
                    coord_inverse=False):
    """
    plots a lens model (convergence) and the critical curves and caustics

    :param ax:
    :param kwargs_lens:
    :param numPix:
    :param deltaPix:
    :return:
    """
    kwargs_data = sim_util.data_configure_simple(numPix, deltaPix, center_ra=coord_center_ra, center_dec=coord_center_dec,
                                                 inverse=coord_inverse)
    data = ImageData(**kwargs_data)
    _coords = data
    _frame_size = numPix * deltaPix
    x_grid, y_grid = data.pixel_coordinates
    lensModelExt = LensModelExtensions(lensModel)
    #ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModelExt.critical_curve_caustics(
    #    kwargs_lens, compute_window=_frame_size, grid_scale=deltaPix/2.)
    x_grid1d = util.image2array(x_grid)
    y_grid1d = util.image2array(y_grid)
    kappa_result = lensModel.kappa(x_grid1d, y_grid1d, kwargs_lens)
    kappa_result = util.array2image(kappa_result)
    im = ax.matshow(np.log10(kappa_result), origin='lower', extent=[0, _frame_size, 0, _frame_size], cmap='Greys',
                    vmin=-1, vmax=1) #, cmap=self._cmap, vmin=v_min, vmax=v_max)
    if with_caustics is True:
        ra_crit_list, dec_crit_list = lensModelExt.critical_curve_tiling(kwargs_lens, compute_window=_frame_size,
                                                                         start_scale=deltaPix, max_order=10)
        ra_caustic_list, dec_caustic_list = lensModel.ray_shooting(ra_crit_list, dec_crit_list, kwargs_lens)
        plot_util.plot_line_set(ax, _coords, ra_caustic_list, dec_caustic_list, color='g')
        plot_util.plot_line_set(ax, _coords, ra_crit_list, dec_crit_list, color='r')
    if point_source:
        from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver
        solver = LensEquationSolver(lensModel)
        theta_x, theta_y = solver.image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens,
                                                             min_distance=deltaPix, search_window=deltaPix*numPix)
        mag_images = lensModel.magnification(theta_x, theta_y, kwargs_lens)
        x_image, y_image = _coords.map_coord2pix(theta_x, theta_y)
        abc_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
        for i in range(len(x_image)):
            x_ = (x_image[i] + 0.5) * deltaPix
            y_ = (y_image[i] + 0.5) * deltaPix
            ax.plot(x_, y_, 'dk', markersize=4*(1 + np.log(np.abs(mag_images[i]))), alpha=0.5)
            ax.text(x_, y_, abc_list[i], fontsize=20, color='k')
        x_source, y_source = _coords.map_coord2pix(sourcePos_x, sourcePos_y)
        ax.plot((x_source + 0.5) * deltaPix, (y_source + 0.5) * deltaPix, '*k', markersize=10)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.autoscale(False)
    return ax

@export
def distortions(lensModel, kwargs_lens, num_pix=100, delta_pix=0.05, center_ra=0, center_dec=0,
                differential_scale=0.0001, smoothing_scale=None, **kwargs):
    """

    :param lensModel: LensModel instance
    :param kwargs_lens: lens model keyword argument list
    :param num_pix: number of pixels per axis
    :param delta_pix: pixel scale per axis
    :param center_ra: center of the grid
    :param center_dec: center of the grid
    :param differential_scale: scale of the finite derivative length in units of angles
    :param smoothing_scale: float or None, Gaussian FWHM of a smoothing kernel applied before plotting
    :return: matplotlib instance with different panels
    """
    kwargs_grid = sim_util.data_configure_simple(num_pix, delta_pix, center_ra=center_ra, center_dec=center_dec)
    _coords = ImageData(**kwargs_grid)
    _frame_size = num_pix * delta_pix
    ra_grid, dec_grid = _coords.pixel_coordinates

    extensions = LensModelExtensions(lensModel=lensModel)
    ra_grid1d = util.image2array(ra_grid)
    dec_grid1d = util.image2array(dec_grid)
    lambda_rad, lambda_tan, orientation_angle, dlambda_tan_dtan, dlambda_tan_drad, dlambda_rad_drad, dlambda_rad_dtan, dphi_tan_dtan, dphi_tan_drad, dphi_rad_drad, dphi_rad_dtan = extensions.radial_tangential_differentials(
        ra_grid1d, dec_grid1d, kwargs_lens=kwargs_lens, center_x=center_ra, center_y=center_dec, smoothing_3rd=differential_scale, smoothing_2nd=None)

    lambda_rad2d, lambda_tan2d, orientation_angle2d, dlambda_tan_dtan2d, dlambda_tan_drad2d, dlambda_rad_drad2d, dlambda_rad_dtan2d, dphi_tan_dtan2d, dphi_tan_drad2d, dphi_rad_drad2d, dphi_rad_dtan2d = util.array2image(lambda_rad), \
                                            util.array2image(lambda_tan), util.array2image(orientation_angle), util.array2image(dlambda_tan_dtan), util.array2image(dlambda_tan_drad), util.array2image(dlambda_rad_drad), util.array2image(dlambda_rad_dtan), \
                                            util.array2image(dphi_tan_dtan), util.array2image(dphi_tan_drad), util.array2image(dphi_rad_drad), util.array2image(dphi_rad_dtan)

    if smoothing_scale is not None:
        lambda_rad2d = ndimage.gaussian_filter(lambda_rad2d, sigma=smoothing_scale/delta_pix)
        dlambda_rad_drad2d = ndimage.gaussian_filter(dlambda_rad_drad2d, sigma=smoothing_scale/delta_pix)
        lambda_tan2d = np.abs(lambda_tan2d)
        # the magnification cut is made to make a stable integral/convolution
        lambda_tan2d[lambda_tan2d > 100] = 100
        lambda_tan2d = ndimage.gaussian_filter(lambda_tan2d, sigma=smoothing_scale/delta_pix)
        # the magnification cut is made to make a stable integral/convolution
        dlambda_tan_dtan2d[dlambda_tan_dtan2d > 100] = 100
        dlambda_tan_dtan2d[dlambda_tan_dtan2d < -100] = -100
        dlambda_tan_dtan2d = ndimage.gaussian_filter(dlambda_tan_dtan2d, sigma=smoothing_scale/delta_pix)
        orientation_angle2d = ndimage.gaussian_filter(orientation_angle2d, sigma=smoothing_scale/delta_pix)
        dphi_tan_dtan2d = ndimage.gaussian_filter(dphi_tan_dtan2d, sigma=smoothing_scale/delta_pix)

    def _plot_frame(ax, map, vmin, vmax, text_string):
        """

        :param ax: matplotlib.axis instance
        :param map: 2d array
        :param vmin: minimum plotting scale
        :param vmax: maximum plotting scale
        :param text_string: string to describe the label
        :return:
        """
        font_size = 10
        _arrow_size = 0.02
        im = ax.matshow(map, extent=[0, _frame_size, 0, _frame_size], vmin=vmin, vmax=vmax)
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.autoscale(False)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cb = plt.colorbar(im, cax=cax, orientation='vertical')
        #cb.set_label(text_string, fontsize=10)
        #plot_util.scale_bar(ax, _frame_size, dist=1, text='1"', font_size=font_size)
        plot_util.text_description(ax, _frame_size, text=text_string, color="k",
                                   backgroundcolor='w', font_size=font_size)
        #if 'no_arrow' not in kwargs or not kwargs['no_arrow']:
        #    plot_util.coordinate_arrows(ax, _frame_size, _coords,
        #                                color='w', arrow_size=_arrow_size,
        #                                font_size=font_size)

    f, axes = plt.subplots(3, 4, figsize=(12, 8))
    _plot_frame(axes[0, 0], lambda_rad2d, vmin=0.6, vmax=1.4, text_string=r"$\lambda_{rad}$")
    _plot_frame(axes[0, 1], lambda_tan2d, vmin=-20, vmax=20, text_string=r"$\lambda_{tan}$")
    _plot_frame(axes[0, 2], orientation_angle2d, vmin=-np.pi / 10, vmax=np.pi / 10, text_string=r"$\phi$")
    _plot_frame(axes[0, 3], util.array2image(lambda_tan * lambda_rad), vmin=-20, vmax=20, text_string='magnification')
    _plot_frame(axes[1, 0], dlambda_rad_drad2d/lambda_rad2d, vmin=-.1, vmax=.1, text_string='dlambda_rad_drad')
    _plot_frame(axes[1, 1], dlambda_tan_dtan2d/lambda_tan2d, vmin=-20, vmax=20, text_string='dlambda_tan_dtan')
    _plot_frame(axes[1, 2], dlambda_tan_drad2d/lambda_tan2d, vmin=-20, vmax=20, text_string='dlambda_tan_drad')
    _plot_frame(axes[1, 3], dlambda_rad_dtan2d/lambda_rad2d, vmin=-.1, vmax=.1, text_string='dlambda_rad_dtan')

    _plot_frame(axes[2, 0], dphi_rad_drad2d, vmin=-.1, vmax=.1, text_string='dphi_rad_drad')
    _plot_frame(axes[2, 1], dphi_tan_dtan2d, vmin=0, vmax=20, text_string='dphi_tan_dtan: curvature radius')
    _plot_frame(axes[2, 2], dphi_tan_drad2d, vmin=-.1, vmax=.1, text_string='dphi_tan_drad')
    _plot_frame(axes[2, 3], dphi_rad_dtan2d, vmin=0, vmax=20, text_string='dphi_rad_dtan')

    return f, axes

@export
def arrival_time_surface(ax, lensModel, kwargs_lens, numPix=500, deltaPix=0.01, sourcePos_x=0, sourcePos_y=0,
                         with_caustics=False, point_source=False, n_levels=10, kwargs_contours={}, image_color_list=None,
                         letter_font_size=20):
    """

    :param ax:
    :param lensModel:
    :param kwargs_lens:
    :param numPix:
    :param deltaPix:
    :param sourcePos_x:
    :param sourcePos_y:
    :param with_caustics:
    :return:
    """
    kwargs_data = sim_util.data_configure_simple(numPix, deltaPix)
    data = ImageData(**kwargs_data)
    _frame_size = numPix * deltaPix
    _coords = data
    x_grid, y_grid = data.pixel_coordinates
    lensModelExt = LensModelExtensions(lensModel)
    #ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModelExt.critical_curve_caustics(
    #    kwargs_lens, compute_window=_frame_size, grid_scale=deltaPix/2.)
    x_grid1d = util.image2array(x_grid)
    y_grid1d = util.image2array(y_grid)
    fermat_surface = lensModel.fermat_potential(x_grid1d, y_grid1d, kwargs_lens, sourcePos_x, sourcePos_y)
    fermat_surface = util.array2image(fermat_surface)

        #, cmap='Greys', vmin=-1, vmax=1) #, cmap=self._cmap, vmin=v_min, vmax=v_max)
    if with_caustics is True:
        ra_crit_list, dec_crit_list = lensModelExt.critical_curve_tiling(kwargs_lens, compute_window=_frame_size,
                                                                             start_scale=deltaPix/5, max_order=10)
        ra_caustic_list, dec_caustic_list = lensModel.ray_shooting(ra_crit_list, dec_crit_list, kwargs_lens)
        plot_util.plot_line_set(ax, _coords, ra_caustic_list, dec_caustic_list, shift=_frame_size/2., color='g')
        plot_util.plot_line_set(ax, _coords, ra_crit_list, dec_crit_list, shift=_frame_size/2., color='r')
    if point_source is True:
        from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver
        solver = LensEquationSolver(lensModel)
        theta_x, theta_y = solver.image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens,
                                                                 min_distance=deltaPix, search_window=deltaPix*numPix)

        fermat_pot_images = lensModel.fermat_potential(theta_x, theta_y, kwargs_lens)
        im = ax.contour(x_grid, y_grid, fermat_surface, origin='lower',  # extent=[0, _frame_size, 0, _frame_size],
                        levels=np.sort(fermat_pot_images), **kwargs_contours)
        mag_images = lensModel.magnification(theta_x, theta_y, kwargs_lens)
        x_image, y_image = _coords.map_coord2pix(theta_x, theta_y)
        abc_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']

        for i in range(len(x_image)):
            x_ = (x_image[i] + 0.5) * deltaPix - _frame_size/2
            y_ = (y_image[i] + 0.5) * deltaPix - _frame_size/2
            if image_color_list is None:
                color = 'k'
            else:
                color = image_color_list[i]
            ax.plot(x_, y_, 'x', markersize=10, alpha=1, color=color)  # markersize=8*(1 + np.log(np.abs(mag_images[i])))
            ax.text(x_ + deltaPix, y_ + deltaPix, abc_list[i], fontsize=letter_font_size, color='k')
        x_source, y_source = _coords.map_coord2pix(sourcePos_x, sourcePos_y)
        ax.plot((x_source + 0.5) * deltaPix - _frame_size/2, (y_source + 0.5) * deltaPix - _frame_size/2, '*k', markersize=20)
    else:
        vmin = np.min(fermat_surface)
        vmax = np.max(fermat_surface)
        levels = np.linspace(start=vmin, stop=vmax, num=n_levels)
        im = ax.contour(x_grid, y_grid, fermat_surface, origin='lower',  # extent=[0, _frame_size, 0, _frame_size],
                        levels=levels, **kwargs_contours)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.autoscale(False)
    return ax

def curved_arc_illustration(ax, lensModel, kwargs_lens):
    """

    :param ax: matplotlib axis instance
    :param lensModel: LensModel() instance
    :param kwargs_lens: list of lens model keyword arguments (only those of CURVED_ARC considered
    :return:
    """

    # loop through lens models
    # check whether curved arc
    lens_model_list = lensModel.lens_model_list
    for i, lens_type in enumerate(lens_model_list):
        if lens_type == 'CURVED_ARC':
            plot_arc(ax, **kwargs_lens[i])

    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax.autoscale(False)

    # plot coordinate frame and scale

def plot_arc(ax, tangential_stretch, radial_stretch, curvature, direction, center_x, center_y, stretch_scale=0.1):
    """

    :param ax:
    :param tangential_stretch: float, stretch of intrinsic source in tangential direction
    :param radial_stretch: float, stretch of intrinsic source in radial direction
    :param curvature: 1/curvature radius
    :param direction: float, angle in radian
    :param center_x: center of source in image plane
    :param center_y: center of source in image plane
    :return:
    """
    # plot line to centroid
    center_x_spp, center_y_spp = center_deflector(curvature, direction, center_x, center_y)
    ax.plot([center_x, center_x_spp], [center_y, center_y_spp], 'k--', alpha=0.5)
    ax.plot([center_x_spp], [center_y_spp], 'k*', alpha=0.5)

    # plot radial and tangential stretch to scale

    x_r = np.cos(direction) * radial_stretch * stretch_scale
    y_r = np.sin(direction) * radial_stretch * stretch_scale
    ax.plot([center_x - x_r, center_x + x_r], [center_y - y_r, center_y + y_r], 'k-')

    # plot curved tangential stretch
    #x_t = np.sin(direction) * tangential_stretch / 2 * stretch_scale
    #y_t = -np.cos(direction) * tangential_stretch / 2 * stretch_scale

    # compute angle of size of the tangential stretch
    r = 1. / curvature
    d_phi = tangential_stretch * stretch_scale / r

    # linearly interpolate angle around center
    phi = np.linspace(-1, 1, 50) * d_phi + direction
    # plot points on circle
    x_curve = r * np.cos(phi) + center_x_spp
    y_curve = r * np.sin(phi) + center_y_spp
    ax.plot(x_curve, y_curve, 'k-')

    # make round circle with start point to end to close the circle
    r_c, t_c = util.points_on_circle(radius=stretch_scale, num_points=50)
    r_c = radial_stretch * r_c + r
    phi_c = t_c * tangential_stretch / r_c + direction
    x_c = r_c * np.cos(phi_c) + center_x_spp
    y_c = r_c * np.sin(phi_c) + center_y_spp
    ax.plot(x_c, y_c, 'k--')

    # TODO add different colors for each quarter to identify parities

ext: py

max_stars_repo_name: swagnercarena/lenstronomy

max_issues_repo_name: swagnercarena/lenstronomy

max_forks_repo_name: swagnercarena/lenstronomy

reason: import numpy,import scipy


text:
from pyomo.environ import Objective, minimize, Constraint
from pyomo.opt import SolverFactory
from pyomo.dataportal.DataPortal import DataPortal
import pandas as pd

#def Model_Instance(model):

def Model_Resolution(model):

    instance = model.create_instance(r'gisele\michele\Inputs\data.json')  # load parameters
    # opt = SolverFactory('cplex',executable=r'C:\Program Files\IBM\ILOG\CPLEX_Studio1210\cplex\bin\x64_win64\cplex')
    opt = SolverFactory('gurobi')  # Solver use during the optimization
    opt.options['mipgap'] = 0.05

    print('Begin Optimization')

    n_ren=3
    print('Solving for '+str(n_ren)+' different values of minimum renewable fraction, ranging from 0% to 100%')

    renfr=-1  # initialize at a value lower than 0

    #instances={}

    for i in range(n_ren):
        min_renfr=i*1/(n_ren-1)
        instance.ren_fraction=min_renfr
        print('Solving for minimum renewable fraction of '+str(min_renfr*100)+'%')
        if renfr>=min_renfr:
            print('Skipping solve as minimum renewable fraction already fulfilled')
            #instances[i]=instances[i-1]
        else:
            results = opt.solve(instance, tee=True)  # Solving a model instance
            instance.solutions.load_from(results)  # Loading solution into instance
            # Compute renewable fraction
            project_duration = int(instance.project_duration.extract_values()[None])
            h_weight = int(instance.h_weight.extract_values()[None])
            dg_power = pd.DataFrame.from_dict(instance.dg_power.get_values(), orient='index')
            dg_tot = 0.001 * h_weight * sum(dg_power.iloc[h, 0] for h in range(project_duration))
            load = pd.DataFrame.from_dict(instance.Load.get_values(), orient='index')
            load_tot = 0.001 * h_weight * sum(load.iloc[h, 0] for h in range(project_duration))
            renfr=1-dg_tot/load_tot
            print('Renewable fraction='+str(renfr*100)+'%')
            #instances[i]=instance
    return instance

reason: from pyomo


text:
import pickle
import matplotlib.pyplot as plt
import numpy as np

from matplotlib.ticker import AutoMinorLocator
minorLocator = AutoMinorLocator()

data_all = pickle.load( open( "save_data_all.p", "rb" ) )
sigma_0 = 0.5
sigma_max = 2.9
epsilon_0 = 8.85E-12
mi=131*1.67e-27
me=9e-31

q=1.6e-19
qe=q

def simpleplot(name,label = None, datadict = data_all, irang = None, mask = None, norm = False):

    toplot = datadict[name].copy()

    if norm:
        toplot /= toplot.max()

    if label is None:
        label = name
    x = datadict["x"]

    plt.plot(x, toplot, label = label)
    plt.xlabel("Radial position $[cm]$")
    plt.legend()
    if irang is not None:
        plt.xlim(irang)

    plt.xlabel("Radial position $r [cm]$")
    plt.gca().xaxis.set_minor_locator(minorLocator)
    plt.tick_params( right = True)
    plt.tick_params(which='minor', length=4, color='r')

def ne_Bolz(phi, n0, T0):
    """Simple model for the electron density
    Bolzmann electrons
    $n_e = n_0 \exp( - \phi/T_e)
    """

    phisift = phi - phi.max()
    return n0*np.exp(phisift/T0)

def Te_eff_calcl(sigma, epsi):
    """inverses the linear calculation of sigma for a Maxwellian
    """
    return (sigma - sigma_0)/(1 - sigma_0) * epsi/2

def phi_drop(Te, sigma):
    """Sheath and pre-sheath drop for SEE"""
    phi = Te * np.log((1. - sigma) * np.sqrt(mi / (2. * np.pi * me))
                      ) + Te * 0.5  # [V] plasma potential
    return phi

def sigma_maxw_unsat(Te, epsi):
    return sigma_0 + 2.0*(Te)*(1.0 - sigma_0)/epsi

def sigma_maxw_sat(Te, epsi):
    "SEE rate with saturation"
    AR2    = (sigma_max - sigma_0)/(1- sigma_0) *epsi/Te
    # AR2    = ( (2.0*(sigma_max-sigma_0)*epsi/(1.0-sigma_0))**2) / (2.0*me*Te)
    sigma  = sigma_0 + 2.0*Te /epsi* (1.-sigma_0) + (
        sigma_max-sigma_0)*np.exp(-AR2)*(AR2+1.0) + (
        sigma_0-1.0) *Te* np.exp(-AR2)*(AR2*(AR2+2.0)+2.0)/epsi #SEE yield

    return sigma

def newSigma_maxw(Te, dphi,epsi,alpha):
    """Using the Non-isothermal model
    Unfortunatly, we need $\Delta \phi$ with is a function of sigma"""

    sigma = sigma_0 + (1 - sigma_0)/(epsi)*(2*Te - dphi/alpha)

    return sigma

def maxwE(etab, Te, normed = False):
    """return the Maxwellian distribution in energy"""

    m = 2/np.sqrt(np.pi)*np.sqrt(etab/Te)*np.exp( -0.5* etab/(Te))
    if normed:
        m /= m.max()
    return m

reason: import numpy


text:
import unittest
import inspect

import numpy as np

from qupulse.utils.types import (HashableNumpyArray, SequenceProxy, _FrozenDictByWrapping,
                                 _FrozenDictByInheritance)

class HashableNumpyArrayTest(unittest.TestCase):
    def test_hash(self):

        with self.assertWarns(DeprecationWarning):
            a = np.array([1, 2, 3]).view(HashableNumpyArray)

            b = np.array([3, 4, 5]).view(HashableNumpyArray)

            c = np.array([1, 2, 3]).view(HashableNumpyArray)

        self.assertNotEqual(hash(a), hash(b))
        self.assertEqual(hash(a), hash(c))

class SequenceProxyTest(unittest.TestCase):
    def test_sequence_proxy(self):
        l = [1, 2, 3, 4, 1]
        p = SequenceProxy(l)
        self.assertEqual(l, list(iter(p)))
        self.assertEqual(5, len(p))
        self.assertEqual(3, p[2])
        self.assertEqual(list(reversed(l)), list(reversed(p)))
        self.assertEqual(2, p.index(3))
        self.assertEqual(2, p.count(1))
        self.assertIn(3, p)
        self.assertNotIn(5, p)

        with self.assertRaises(TypeError):
            p[1] = 7

class FrozenDictTests(unittest.TestCase):
    FrozenDictType = _FrozenDictByWrapping

    """This class can test general non mutable mappings"""
    def setUp(self) -> None:
        self.d = {'a': 1, 'b': 2}
        self.f = self.FrozenDictType(self.d)
        self.prev_state = dict(self.f)

    def tearDown(self) -> None:
        self.assertEqual(self.prev_state, dict(self.f))

    def test_init(self):
        d = {'a': 1, 'b': 2}

        f1 = self.FrozenDictType(d)
        f2 = self.FrozenDictType(**d)
        f3 = self.FrozenDictType(d.items())

        self.assertEqual(d, f1)
        self.assertEqual(d, f2)
        self.assertEqual(d, f3)

        self.assertEqual(d.keys(), f1.keys())
        self.assertEqual(d.keys(), f2.keys())
        self.assertEqual(d.keys(), f3.keys())

        self.assertEqual(set(d.items()), set(f1.items()))
        self.assertEqual(set(d.items()), set(f2.items()))
        self.assertEqual(set(d.items()), set(f3.items()))

    def test_mapping(self):
        d = {'a': 1, 'b': 2}
        f = self.FrozenDictType(d)

        self.assertEqual(len(d), len(f))
        self.assertIn('a', f)
        self.assertIn('b', f)
        self.assertNotIn('c', f)

        self.assertEqual(1, f['a'])
        self.assertEqual(2, f['b'])

        with self.assertRaisesRegex(KeyError, 'c'):
            _ = f['c']

        with self.assertRaises(TypeError):
            f['a'] = 9

        with self.assertRaises(TypeError):
            del f['a']

    def test_copy(self):
        d = {'a': 1, 'b': 2}
        f = self.FrozenDictType(d)
        self.assertIs(f, f.copy())

    def test_eq_and_hash(self):
        d = {'a': 1, 'b': 2}

        f1 = self.FrozenDictType(d)
        f2 = self.FrozenDictType({'a': 1, 'b': 2})
        f3 = self.FrozenDictType({'a': 1, 'c': 3})

        self.assertEqual(f1, f2)
        self.assertEqual(hash(f1), hash(f2))

        self.assertNotEqual(f1, f3)

class FrozenDictByInheritanceTests(FrozenDictTests):
    FrozenDictType = _FrozenDictByInheritance

    def test_update(self):
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.update(d=5)

    def test_setdefault(self):
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.setdefault('c', 3)
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.setdefault('a', 2)

    def test_clear(self):
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.clear()

    def test_pop(self):
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.pop()
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.pop('a')

    def test_popitem(self):
        with self.assertRaisesRegex(TypeError, 'immutable'):
            self.f.popitem()

max_stars_repo_name: qutech-lab/qc-toolkit

max_issues_repo_name: qutech-lab/qc-toolkit

max_forks_repo_name: qutech-lab/qc-toolkit


text:
from __future__ import division
import os,cv2,helper,time,scipy.io
import tensorflow as tf
import tensorflow.contrib.slim as slim
from tensorflow.contrib.layers.python.layers import initializers
from spatial_transformer import transformer
import numpy as np

training_phase = True
if(training_phase):
    rgb_path = "../traindata/RGB512Full/"
    mat_path = "../traindata/transform/transform_512/"
    label_path = "../traindata/label_refine_512/"
    save_model_path = "../trainedmodels/transform"
    num_folder = 2
else:
    save_model_path = "../trainedmodels/transform"
    mat_path = "../testdata/transform/transform_512/"
    save_result_path = "../result/transform/transform_512/"
    label_path = "../testdata/label_refine_512/"

os.system('nvidia-smi -q -d Memory |grep -A4 GPU|grep Free >tmp')
os.environ['CUDA_VISIBLE_DEVICES']=str(np.argmax([int(x.split()[2]) for x in open('tmp','r').readlines()]))
os.system('rm tmp')

batch_size = 1
num_proposal = 10

crop_size_h = 512
crop_size_w = 1024
sp = 512

#resolution 256 x 512
'''
crop_size_h = 512
crop_size_w = 1024
sp = 512
'''
# resolution 1024 x 2048
'''
crop_size_h = 1024
crop_size_w = 2048
sp = 1024
'''

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

def lrelu(x):
    return tf.maximum(0.2*x,x)

MEAN_VALUES = np.array([123.6800, 116.7790, 103.9390]).reshape((1,1,1,3))

def build_net(ntype,nin,nwb=None,name=None):
    if ntype=='conv':
        return tf.nn.relu(tf.nn.conv2d(nin,nwb[0],strides=[1,1,1,1],padding='SAME',name=name)+nwb[1])
    elif ntype=='pool':
        return tf.nn.avg_pool(nin,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
def one_hot(label):

    output=np.zeros((1,100,label.shape[0],label.shape[1]),dtype=np.float32);
    for i in range(label.shape[0]):
        for j in range(label.shape[1]):
            if label[i,j]>0:
                output[0,label[i,j]-1,i,j]=1
    return output

def get_weight_bias(vgg_layers,i):
    weights=vgg_layers[i][0][0][2][0][0]
    weights=tf.constant(weights)
    bias=vgg_layers[i][0][0][2][0][1]
    bias=tf.constant(np.reshape(bias,(bias.size)))
    return weights,bias

def build_vgg19(input,reuse=False):
    if reuse:
        tf.get_variable_scope().reuse_variables()
    net={}
    vgg_rawnet=scipy.io.loadmat('Models/imagenet-vgg-verydeep-19.mat')
    vgg_layers=vgg_rawnet['layers'][0]
    net['input']=input-MEAN_VALUES
    net['conv1_1']=build_net('conv',net['input'],get_weight_bias(vgg_layers,0),name='vgg_conv1_1')
    net['conv1_2']=build_net('conv',net['conv1_1'],get_weight_bias(vgg_layers,2),name='vgg_conv1_2')
    net['pool1']=build_net('pool',net['conv1_2'])
    net['conv2_1']=build_net('conv',net['pool1'],get_weight_bias(vgg_layers,5),name='vgg_conv2_1')
    net['conv2_2']=build_net('conv',net['conv2_1'],get_weight_bias(vgg_layers,7),name='vgg_conv2_2')
    net['pool2']=build_net('pool',net['conv2_2'])
    net['conv3_1']=build_net('conv',net['pool2'],get_weight_bias(vgg_layers,10),name='vgg_conv3_1')
    net['conv3_2']=build_net('conv',net['conv3_1'],get_weight_bias(vgg_layers,12),name='vgg_conv3_2')
    net['conv3_3']=build_net('conv',net['conv3_2'],get_weight_bias(vgg_layers,14),name='vgg_conv3_3')
    net['conv3_4']=build_net('conv',net['conv3_3'],get_weight_bias(vgg_layers,16),name='vgg_conv3_4')
    net['pool3']=build_net('pool',net['conv3_4'])
    net['conv4_1']=build_net('conv',net['pool3'],get_weight_bias(vgg_layers,19),name='vgg_conv4_1')
    net['conv4_2']=build_net('conv',net['conv4_1'],get_weight_bias(vgg_layers,21),name='vgg_conv4_2')
    net['conv4_3']=build_net('conv',net['conv4_2'],get_weight_bias(vgg_layers,23),name='vgg_conv4_3')
    net['conv4_4']=build_net('conv',net['conv4_3'],get_weight_bias(vgg_layers,25),name='vgg_conv4_4')
    net['pool4']=build_net('pool',net['conv4_4'])
    net['conv5_1']=build_net('conv',net['pool4'],get_weight_bias(vgg_layers,28),name='vgg_conv5_1')
    net['conv5_2']=build_net('conv',net['conv5_1'],get_weight_bias(vgg_layers,30),name='vgg_conv5_2')
    net['conv5_3']=build_net('conv',net['conv5_2'],get_weight_bias(vgg_layers,32),name='vgg_conv5_3')
    net['conv5_4']=build_net('conv',net['conv5_3'],get_weight_bias(vgg_layers,34),name='vgg_conv5_4')
    net['pool5']=build_net('pool',net['conv5_4'])
    return net
def proposal_transform(proposal,mask,label):

    input = tf.concat([proposal,mask,tf.tile(label,[num_proposal,1,1,1])],axis = 3)

    if(sp==512):
        input = tf.image.resize_bilinear(input,[crop_size_h//2, crop_size_w//2], align_corners = True)
    elif(sp==1024):
        input = tf.image.resize_bilinear(input,[crop_size_h//4, crop_size_w//4], align_corners = True)

    conv1_transform = slim.repeat(input, 2, slim.conv2d, 64, [3, 3], rate=1, normalizer_fn=slim.layer_norm,
                                  activation_fn=lrelu, scope='g_transform_conv1')
    pool1_transform = slim.avg_pool2d(conv1_transform, [3, 3], stride=2, padding='SAME', scope='g_transform_pool1')

    conv2_transform = slim.repeat(pool1_transform, 2, slim.conv2d, 128, [3, 3], rate=1, normalizer_fn=slim.layer_norm,
                                  activation_fn=lrelu, scope='g_transfrom_conv2')
    pool2_transform = slim.max_pool2d(conv2_transform, [3, 3], stride=2, padding='SAME', scope='g_transform_pool2')

    conv3_transform = slim.repeat(pool2_transform, 3, slim.conv2d, 256, [3, 3], rate=1, normalizer_fn=slim.layer_norm,
                                  activation_fn=lrelu, scope='g_transform_conv3')
    pool3_transform = slim.max_pool2d(conv3_transform, [3, 3], stride=2, padding='SAME', scope='g_transform_pool3')

    conv4_transform = slim.repeat(pool3_transform, 3, slim.conv2d, 512, [3, 3], rate=1, normalizer_fn=slim.layer_norm,
                                  activation_fn=lrelu,
                                  scope='g_transform_conv4')
    pool4_transform = slim.max_pool2d(conv4_transform, [3, 3], stride=2, padding='SAME', scope='g_transform_pool4')

    conv5_transform = slim.repeat(pool4_transform, 3, slim.conv2d, 512, [3, 3], rate=1, normalizer_fn=slim.layer_norm,
                                  activation_fn=lrelu,
                                  scope='g_transform_conv5')
    pool5_transform = slim.max_pool2d(conv5_transform, [3, 3], stride=2, padding='SAME', scope='g_transform_pool5')

    pool6_transform = slim.avg_pool2d(pool5_transform, [8, 16], stride=[8, 16], padding='VALID',
                                      scope='g_transform_pool6')

    pool6_reshape = tf.reshape(pool6_transform,[batch_size*num_proposal,512])
    initial_W = np.zeros((512,6),dtype = np.float32)
    W_fc1 = tf.Variable(initial_value = initial_W, name='g_transform_param')

    initial = np.array([[1.0, 0., 0.], [0., 1.0, 0.]])
    initial = initial.astype('float32')
    initial = initial.flatten()

    b_fc1 = tf.Variable(initial_value=initial, name='g_b_fc1')
    transform_param = tf.matmul(pool6_reshape, W_fc1) + b_fc1
    # This is to avoid bilinear intepolation to corrupt the original pixel in cityscape dataset.
    # Since cityscape has very similar viewpoint patterns, spatial transformer can only slightly influence the result
    # The following two lines can be comment if on other datasets like NYU and ADE20k
    if(not training_phase):
        transform_param = tf.round(transform_param)

    proposal_refine = transformer(proposal,transform_param,[crop_size_h,crop_size_w])

    proposal_refine_new = tf.multiply(proposal_refine,tf.tile(mask,[1,1,1,3]))
    proposal_refine_new = tf.reduce_sum(proposal_refine_new,axis = 0, keep_dims = True)

    return proposal_refine,proposal_refine_new

def compute_error(real,fake,label,mask,flag = True):
    if (flag):
        return tf.reduce_sum(tf.reduce_mean(label*tf.expand_dims(tf.reduce_mean(tf.abs(fake-real),reduction_indices=[0,3]),-1),reduction_indices=[0,1,2]))

    loss = tf.reduce_mean(tf.abs(fake-real),reduction_indices =[3],keep_dims = True)
    loss = tf.multiply(loss,mask)
    loss = tf.reduce_sum(loss)/(tf.reduce_sum(mask)+1.0)

    return loss

with tf.variable_scope(tf.get_variable_scope()) as scope:
    label=tf.placeholder(tf.float32,[batch_size,None,None,20])
    real_image=tf.placeholder(tf.float32,[batch_size,None,None,3])
    fake_image=tf.placeholder(tf.float32,[batch_size,None,None,3])

    mask = tf.placeholder(tf.float32,[batch_size*num_proposal,crop_size_h,crop_size_w,1])
    proposal = tf.placeholder(tf.float32,[batch_size*num_proposal,crop_size_h,crop_size_w,3])

    proposal_transfer,proposal_new = proposal_transform(proposal,mask,label)

    real_B,real_G,real_R = tf.split(real_image,num_or_size_splits = 3,axis = 3)
    real_image_new = tf.concat([real_R, real_G,real_B],axis = 3)
    weight=tf.placeholder(tf.float32)

    mask_all = tf.reduce_sum(mask, axis=0, keep_dims = True)
    p_transformer = compute_error(real_image_new,proposal_new*255.0,label,mask_all, False)

    G_loss = p_transformer*1.0

lr=tf.placeholder(tf.float32)

t_vars = tf.trainable_variables()
for var in t_vars:
    if var.name.startswith('g_'):
        print var.name
if(training_phase):
    G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(loss = G_loss,var_list=[var for var in t_vars if var.name.startswith('g_')])

saver=tf.train.Saver(max_to_keep=1000)
sess.run(tf.global_variables_initializer())
ckpt=tf.train.get_checkpoint_state(save_model_path+ "/0100")

if ckpt:
   print('loaded '+ckpt.model_checkpoint_path)
   saver.restore(sess,ckpt.model_checkpoint_path)
if(training_phase):
    best=np.inf
    all_loss=np.zeros(3000,dtype=float)
    g_loss=np.zeros(3000,dtype=float)
    all_c_loss=np.zeros(3000,dtype=float)
    input_images=[None]*3000
    label_images=[None]*3000

    tmp_label = np.zeros((batch_size,19,crop_size_h,crop_size_w),dtype = float)
    tmp_image = np.zeros((batch_size, crop_size_h,crop_size_w, 3), dtype = float)
    tmp_mask = np.zeros((batch_size*num_proposal,crop_size_h, crop_size_w,1), dtype =float)
    #tmp_mask_all = np.zeros((batch_size,crop_size_h,crop_size_w,1),dtype = float)
    tmp_proposal = np.zeros((batch_size*num_proposal,crop_size_h,crop_size_w,3),dtype = float)
    total_number = 2975
    base_lr = 1e-4
    for epoch in range(1,101):
        if os.path.isdir(save_model_path+"/%04d"%epoch):
            continue
        cnt=0

        tmp_list = np.random.permutation(total_number) + 1
        global_ind = 0
        ind = 0

        while(global_ind<total_number):
            st=time.time()
            tmp_label = np.zeros((batch_size, 19, crop_size_h, crop_size_w), dtype=float)
            tmp_image = np.zeros((batch_size, crop_size_h, crop_size_w, 3), dtype=float)
            tmp_mask = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 1), dtype=float)

            tmp_proposal = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 3), dtype=float)
            for batch_count in range(0, batch_size):
                if(ind>=total_number):
                    ind = 0

                rand_folder = np.random.permutation(num_folder) + 1
                try:
                    dic = scipy.io.loadmat(mat_path +"%02d"%rand_folder[0]+ "/%08d.mat"%tmp_list[ind])
                except:
                    print("cannot load" + "%08d.mat" % tmp_list[ind])
                    print(mat_path +"%02d"%rand_folder[0]+ "/%08d.mat"%tmp_list[ind])
                    ind = ind + 1
                    global_ind = global_ind + 1
                    continue
                cnt += 1
                num = dic['proposal'].shape[0]

                tmp_label[:, :, :, :] = helper.get_semantic_map(label_path+ "/%08d.png" % tmp_list[ind])
                tmp_image[:, :, :, :] = np.expand_dims(np.float32(cv2.imread(rgb_path + "/%08d.png"%tmp_list[ind])),axis=0)
                if(num<=num_proposal):
                    tmp_mask[0:num, :, :, :] = np.expand_dims(dic['mask'], axis=3).astype(np.float32)
                    tmp_proposal[0:num, :, :, :] = (dic['proposal']).astype(np.float32)/ 255.0
                if(num>num_proposal):
                    d = np.random.permutation(num)
                    tmp_mask[:,:,:,:] = np.expand_dims(dic['mask'],axis=3).astype(np.float32)[d[0:num_proposal],:,:,:]#+np.expand_dims(np.expand_dims(dic2['mask'],axis=0),axis=3)
                    tmp_proposal[:,:,:,:] = (dic['proposal']).astype(np.float32)[d[0:num_proposal],:,:,:]/255.0 #+ np.expand_dims(dic2['proposal'],axis = 0)

                z = np.concatenate((tmp_label, np.expand_dims(1 - np.sum(tmp_label, axis=1), axis=1)), axis=1)
                z = z.transpose([0,2,3,1])
               # print z.shape
                ind = ind + 1
                global_ind = global_ind + 1
            _,G_current,l6,lr_value=sess.run([G_opt,G_loss,p_transformer,lr],feed_dict={label:z,real_image:tmp_image,mask: tmp_mask, proposal:tmp_proposal,lr:min(base_lr*np.power(1.1,epoch-1),1e-5 if epoch>112 else 1e-5)})
            g_loss[cnt]=G_current

            print("%d %d %.2f %.2f %.2f %.6f"%(epoch,cnt,np.mean(g_loss[np.where(g_loss)]),np.mean(l6),time.time()-st, lr_value))

        if epoch%1==0:
            os.makedirs(save_model_path+"/%04d" % epoch)
            target = open(save_model_path+"/%04d/score.txt" % epoch, 'w')
            target.write("%f" % np.mean(g_loss[np.where(g_loss)]))
            target.close()
            saver.save(sess,save_model_path+"/model.ckpt")
        if epoch%20==0:
            saver.save(sess,save_model_path+"/%04d/model.ckpt"%epoch)
        cnt=3000
        tmp_list = range(2926,2976) + range(100001,100051)
        #tmp_list = range(1100,1200)
        global_count = 0
        semantic = np.zeros((batch_size,19,crop_size_h,crop_size_w),dtype = float)
        proposal_val = np.zeros((batch_size*num_proposal,crop_size_h,crop_size_w,3),dtype = float)
        proposal_mask = np.zeros((batch_size*num_proposal,crop_size_h,crop_size_w,1),dtype = float)

        while (global_count < 99):
            semantic = np.zeros((batch_size, 19, crop_size_h, crop_size_w), dtype=float)
            proposal_val = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 3), dtype=float)
            proposal_mask = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 1), dtype=float)
            for local_count in range(0,batch_size):
                semantic[:,:,:,:]= helper.get_semantic_map(label_path + "/%08d.png" % tmp_list[global_count])

                dic = scipy.io.loadmat(mat_path + "/01/"+"%08d.mat"%tmp_list[global_count])
                num = dic['proposal'].shape[0]
                if (num <= num_proposal):
                    proposal_mask[0:num, :, :, :] = np.expand_dims(dic['mask'], axis=3).astype(np.float32)
                    proposal_val[0:num, :, :, :] = (dic['proposal']).astype(np.float32) / 255.0
                if (num > num_proposal):
                    d = np.random.permutation(num)
                    proposal_mask[:, :, :, :] = np.expand_dims(dic['mask'], axis=3).astype(np.float32)[d[0:num_proposal], :, :,:]  # +np.expand_dims(np.expand_dims(dic2['mask'],axis=0),axis=3)
                    proposal_val[:, :, :, :] = (dic['proposal']).astype(np.float32)[d[0:num_proposal], :, :,:] / 255.0  # + np.expand_dims(dic2['proposal'],axis = 0)

                global_count = global_count +1
            z = np.concatenate((semantic, np.expand_dims(1 - np.sum(semantic, axis=1), axis=1)), axis=1)
            z = z.transpose([0, 2, 3, 1])

            proposal_output=sess.run(proposal_new,feed_dict={label:z,proposal:proposal_val,mask:proposal_mask})

            proposal_output = np.minimum(np.maximum(proposal_output, 0.0), 1.0)
            proposal_output = proposal_output * 255.0
            proposal_output_final = np.concatenate((np.expand_dims(proposal_output[:, :, :, 2], axis=3),
                                                    np.expand_dims(proposal_output[:, :, :, 1], axis=3),
                                                    np.expand_dims(proposal_output[:, :, :, 0], axis=3)), axis=3)

            cv2.imwrite(save_model_path + "/%04d/%06d_output_proposal.png" % (epoch, cnt), np.uint8(proposal_output_final[0, :, :, :]))

            print(cnt)
            cnt = cnt + batch_size
else:

    if not os.path.isdir(save_result_path):
        os.makedirs(save_result_path)

    semantic = np.zeros((batch_size,19,crop_size_h,crop_size_w),dtype = float)
    proposal_val = np.zeros((batch_size*num_proposal,crop_size_h,crop_size_w,3),dtype = float)
    proposal_mask = np.zeros((batch_size*num_proposal,crop_size_h,crop_size_w,1),dtype = float)
    for ind in range(100001,100501):
        print(ind)
        semantic = np.zeros((batch_size, 19, crop_size_h, crop_size_w), dtype=float)
        proposal_val = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 3), dtype=float)
        proposal_mask = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 1), dtype=float)
        semantic[:, :, :, :] = helper.get_semantic_map(label_path + "/%08d.png" % ind)

        dic = scipy.io.loadmat(mat_path + "/%08d.mat" % (ind))

        proposals = dic['proposal'].astype(np.float32) / 255.0

        query_mask = dic['mask'].astype(np.float32)

        if (proposals.ndim == 0):
            continue
        if (proposals.ndim == 3 and query_mask.ndim == 2):
            proposals = np.expand_dims(proposals, axis=0)
            query_mask = np.expand_dims(query_mask, axis=0)
        proposal_transfered_result = np.zeros((proposals.shape[0],proposals.shape[1],proposals.shape[2],proposals.shape[3]),dtype=np.float32)
        total_proposal = proposals.shape[0]
        print((np.floor(total_proposal / num_proposal) + 1).astype(np.int32))
        for n in range(0,(np.floor(total_proposal/num_proposal)+1).astype(np.int32)):
            proposal_val = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 3), dtype=float)
            proposal_mask = np.zeros((batch_size * num_proposal, crop_size_h, crop_size_w, 1), dtype=float)
            if(total_proposal<=num_proposal):
                proposal_val[0:total_proposal, :, :, :] = proposals[:,:,:,:]
                proposal_mask[0:total_proposal, :, :, :] = np.expand_dims(query_mask,axis = 3)
                proposal_transfer_data=sess.run(proposal_transfer,feed_dict={label:np.concatenate((semantic,np.expand_dims(1-np.sum(semantic,axis=1),axis=1)),axis=1).transpose([0,2,3,1]),proposal:proposal_val,mask:proposal_mask})
                proposal_transfered_result[0:total_proposal,:,:,:] = proposal_transfer_data[0:total_proposal,:,:,:]
            elif(n==np.floor(total_proposal/num_proposal)):
                residual_num = total_proposal - num_proposal*n
                start_num = num_proposal*n
                proposal_val[0:residual_num,:,:,:] = proposals[start_num:total_proposal,:,:,:]
                proposal_mask[0:residual_num,:,:,:] = np.expand_dims(query_mask[start_num:total_proposal,:,:],axis=3)
                proposal_transfer_data = sess.run(proposal_transfer, feed_dict={label: np.concatenate((semantic, np.expand_dims(1 - np.sum(semantic, axis=1), axis=1)),axis=1).transpose([0, 2, 3, 1]), proposal: proposal_val, mask: proposal_mask})
                proposal_transfered_result[start_num:total_proposal, :, :,:] = proposal_transfer_data[0:residual_num, :, :, :]
            else:
                start_num = n*num_proposal
                end_num = (n+1)*num_proposal

                proposal_val = proposals[start_num:end_num,:,:,:]
                proposal_mask = np.expand_dims(query_mask[start_num:end_num,:,:],axis=3)
                proposal_transfer_data = sess.run(proposal_transfer, feed_dict={label: np.concatenate((semantic, np.expand_dims(1 - np.sum(semantic, axis=1), axis=1)),axis=1).transpose([0, 2, 3, 1]), proposal: proposal_val, mask: proposal_mask})

                proposal_transfered_result[start_num:end_num, :, :, :] = proposal_transfer_data
        proposal_transfered_result = np.uint8(proposal_transfered_result*255.0)
        scipy.io.savemat(save_result_path+"/%08d.mat"%(ind),{'transferred':proposal_transfered_result})

max_stars_repo_name: compile-run-Ali/SIMS

max_issues_repo_name: compile-run-Ali/SIMS

max_forks_repo_name: compile-run-Ali/SIMS


text:
import cv2
import queue
import numpy as np
import math

THRESHOLD = 25

FILENAME = 'diff_hue'
image = cv2.imread(FILENAME + '.png')
height = image.shape[0]
width = image.shape[1]

# used for smoothing out image.
kernel = np.ones((5, 5), np.float32) / 25

def RGB_distance_threshold(first_rgb, second_rgb):
    return math.sqrt(np.sum((np.absolute(first_rgb - second_rgb))**2))

x_max = 0
y_max = 0
x_min = width - 1
y_min = height - 1

def flood_fill(image, x_loc, y_loc, target_color, replacement_color):
    image[y_loc, x_loc] = replacement_color
    pixel_queue = queue.Queue()
    pixel_queue.put((x_loc, y_loc))
    width = len(image[0])
    height = len(image)
    while not pixel_queue.empty():
        global x_max, y_max, x_min, y_min
        current_x, current_y = pixel_queue.get()

        if current_x > 0:
            left_rgb = image[current_y][current_x - 1]
            if RGB_distance_threshold(left_rgb, target_color) < THRESHOLD and not np.array_equal(image[current_y][current_x - 1], replacement_color):
                image[current_y][current_x - 1] = replacement_color
                pixel_queue.put((current_x - 1, current_y))
                if (x_min > current_x - 1):
                    x_min = current_x - 1

        if current_x < width - 1:
            right_rgb = image[current_y][current_x + 1]
            if RGB_distance_threshold(right_rgb, target_color) < THRESHOLD and not np.array_equal(image[current_y][current_x + 1], replacement_color):
                image[current_y][current_x + 1] = replacement_color
                pixel_queue.put((current_x + 1, current_y))
                if (x_max < current_x + 1):
                    x_max = current_x + 1

        if current_y < height - 1:
            up_rgb = image[current_y + 1][current_x]
            if RGB_distance_threshold(up_rgb, target_color) < THRESHOLD and not np.array_equal(image[current_y + 1][current_x], replacement_color):
                image[current_y + 1][current_x] = replacement_color
                pixel_queue.put((current_x, current_y + 1))
                if (y_max < current_y + 1):
                    y_max = current_y + 1

        if current_y > 0:
            down_rgb = image[current_y - 1][current_x]
            if RGB_distance_threshold(down_rgb, target_color) < THRESHOLD and not np.array_equal(image[current_y - 1][current_x], replacement_color):
                image[current_y - 1][current_x] = replacement_color
                pixel_queue.put((current_x, current_y - 1))
                if (y_min > current_y - 1):
                    y_min = current_y - 1
    return image

x_global = 0
y_global = 0
def register_click(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        global x_global, y_global
        x_global = int(x)
        y_global = int(y)
        print(x_global, y_global)

        # image indexing returns something weird so this fixed it
        target_color = np.array(image[y_global][x_global].tolist())
        # green color
        replace_color = np.array([0, 255, 0])

        image2 = flood_fill(image, x_global, y_global, target_color, replace_color)

        cv2.imshow('image2', image2)
        cv2.imwrite(FILENAME + 'detected.PNG', image2)

cv2.namedWindow('image')
cv2.setMouseCallback('image', register_click)
cv2.imshow('image', image)

#print(x_global, y_global)

cv2.waitKey(0)
cv2.destroyAllWindows()


text:
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import confusion_matrix, roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd

data = pd.read_csv('balanced_reviews.csv')

data.dropna(inplace=True)

data = data[data['overall'] != 3]

data['Positivity'] = np.where(data['overall'] > 3, 1, 0)

data['Positivity'].value_counts()

features_train, features_test, labels_train, labels_test = train_test_split(
    data['reviewText'], data['Positivity'], random_state=42)

# Using TF-IDF Vectorizer
vect = TfidfVectorizer(min_df=5).fit(features_train)

features_train_vectorized = vect.transform(features_train)

model = LogisticRegression()
model.fit(features_train_vectorized, labels_train)

predictions = model.predict(vect.transform(features_test))

confusion_matrix(labels_test, predictions)

roc_auc_score(labels_test, predictions)

pkl_filename = 'model_files/trained_model.pkl'
vocab_filename = 'model_files/vocab.pkl'

# Writing model to pickle file
with open(pkl_filename, 'wb') as file:
    pickle.dump(model, file)

# Saving TF-IDF vocabulary
with open(vocab_filename, 'wb') as file:
    pickle.dump(vect.vocabulary_, file)