text:
#
# Copyright 2014 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest import TestCase

from nose_parameterized import parameterized
import numpy as np
import pandas as pd
from pandas.util.testing import assert_frame_equal

from zipline.history import history
from zipline.history.history_container import HistoryContainer
from zipline.protocol import BarData
import zipline.utils.factory as factory
from zipline import TradingAlgorithm
from zipline.finance.trading import SimulationParameters, TradingEnvironment

from zipline.sources import RandomWalkSource, DataFrameSource

from .history_cases import (
    HISTORY_CONTAINER_TEST_CASES,
)

# Cases are over the July 4th holiday, to ensure use of trading calendar.

#      March 2013
# Su Mo Tu We Th Fr Sa
#                 1  2
#  3  4  5  6  7  8  9
# 10 11 12 13 14 15 16
# 17 18 19 20 21 22 23
# 24 25 26 27 28 29 30
# 31
#      April 2013
# Su Mo Tu We Th Fr Sa
#     1  2  3  4  5  6
#  7  8  9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30
#
#       May 2013
# Su Mo Tu We Th Fr Sa
#           1  2  3  4
#  5  6  7  8  9 10 11
# 12 13 14 15 16 17 18
# 19 20 21 22 23 24 25
# 26 27 28 29 30 31
#
#      June 2013
# Su Mo Tu We Th Fr Sa
#                    1
#  2  3  4  5  6  7  8
#  9 10 11 12 13 14 15
# 16 17 18 19 20 21 22
# 23 24 25 26 27 28 29
# 30
#      July 2013
# Su Mo Tu We Th Fr Sa
#     1  2  3  4  5  6
#  7  8  9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31
#
# Times to be converted via:
# pd.Timestamp('2013-07-05 9:31', tz='US/Eastern').tz_convert('UTC')},

INDEX_TEST_CASES_RAW = {
    'week of daily data': {
        'input': {'bar_count': 5,
                  'frequency': '1d',
                  'algo_dt': '2013-07-05 9:31AM'},
        'expected': [
            '2013-06-28 4:00PM',
            '2013-07-01 4:00PM',
            '2013-07-02 4:00PM',
            '2013-07-03 1:00PM',
            '2013-07-05 9:31AM',
        ]
    },
    'five minutes on july 5th open': {
        'input': {'bar_count': 5,
                  'frequency': '1m',
                  'algo_dt': '2013-07-05 9:31AM'},
        'expected': [
            '2013-07-03 12:57PM',
            '2013-07-03 12:58PM',
            '2013-07-03 12:59PM',
            '2013-07-03 1:00PM',
            '2013-07-05 9:31AM',
        ]
    },
}

def to_timestamp(dt_str):
    return pd.Timestamp(dt_str, tz='US/Eastern').tz_convert('UTC')

def convert_cases(cases):
    """
    Convert raw strings to values comparable with system data.
    """
    cases = cases.copy()
    for case in cases.values():
        case['input']['algo_dt'] = to_timestamp(case['input']['algo_dt'])
        case['expected'] = pd.DatetimeIndex([to_timestamp(dt_str) for dt_str
                                             in case['expected']])
    return cases

INDEX_TEST_CASES = convert_cases(INDEX_TEST_CASES_RAW)

def get_index_at_dt(case_input):
    history_spec = history.HistorySpec(
        case_input['bar_count'],
        case_input['frequency'],
        None,
        False,
        daily_at_midnight=False
    )
    return history.index_at_dt(history_spec, case_input['algo_dt'])

class TestHistoryIndex(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.environment = TradingEnvironment.instance()

    @parameterized.expand(
        [(name, case['input'], case['expected'])
         for name, case in INDEX_TEST_CASES.items()]
    )
    def test_index_at_dt(self, name, case_input, expected):
        history_index = get_index_at_dt(case_input)

        history_series = pd.Series(index=history_index)
        expected_series = pd.Series(index=expected)

        pd.util.testing.assert_series_equal(history_series, expected_series)

class TestHistoryContainer(TestCase):

    @classmethod
    def setUpClass(cls):
        cls.env = TradingEnvironment.instance()

    def bar_data_dt(self, bar_data, require_unique=True):
        """
        Get a dt to associate with the given BarData object.

        If require_unique == True, throw an error if multiple unique dt's are
        encountered.  Otherwise, return the earliest dt encountered.
        """
        dts = {sid_data['dt'] for sid_data in bar_data.values()}
        if require_unique and len(dts) > 1:
            self.fail("Multiple unique dts ({0}) in {1}".format(dts, bar_data))

        return sorted(dts)[0]

    @parameterized.expand(
        [(name,
          case['specs'],
          case['sids'],
          case['dt'],
          case['updates'],
          case['expected'])
         for name, case in HISTORY_CONTAINER_TEST_CASES.items()]
    )
    def test_history_container(self,
                               name,
                               specs,
                               sids,
                               dt,
                               updates,
                               expected):

        for spec in specs:
            # Sanity check on test input.
            self.assertEqual(len(expected[spec.key_str]), len(updates))

        container = HistoryContainer(
            {spec.key_str: spec for spec in specs}, sids, dt
        )

        for update_count, update in enumerate(updates):

            bar_dt = self.bar_data_dt(update)
            container.update(update, bar_dt)

            for spec in specs:
                pd.util.testing.assert_frame_equal(
                    container.get_history(spec, bar_dt),
                    expected[spec.key_str][update_count],
                    check_dtype=False,
                    check_column_type=True,
                    check_index_type=True,
                    check_frame_type=True,
                )

    def test_container_nans_and_daily_roll(self):

        spec = history.HistorySpec(
            bar_count=3,
            frequency='1d',
            field='price',
            ffill=True,
            daily_at_midnight=False
        )
        specs = {spec.key_str: spec}
        initial_sids = [1, ]
        initial_dt = pd.Timestamp(
            '2013-06-28 9:31AM', tz='US/Eastern').tz_convert('UTC')

        container = HistoryContainer(
            specs, initial_sids, initial_dt)

        bar_data = BarData()
        container.update(bar_data, initial_dt)
        # Since there was no backfill because of no db.
        # And no first bar of data, so all values should be nans.
        prices = container.get_history(spec, initial_dt)
        nan_values = np.isnan(prices[1])
        self.assertTrue(all(nan_values), nan_values)

        # Add data on bar two of first day.
        second_bar_dt = pd.Timestamp(
            '2013-06-28 9:32AM', tz='US/Eastern').tz_convert('UTC')

        bar_data[1] = {
            'price': 10,
            'dt': second_bar_dt
        }
        container.update(bar_data, second_bar_dt)

        prices = container.get_history(spec, second_bar_dt)
        # Prices should be
        #                             1
        # 2013-06-26 20:00:00+00:00 NaN
        # 2013-06-27 20:00:00+00:00 NaN
        # 2013-06-28 13:32:00+00:00  10

        self.assertTrue(np.isnan(prices[1].ix[0]))
        self.assertTrue(np.isnan(prices[1].ix[1]))
        self.assertEqual(prices[1].ix[2], 10)

        third_bar_dt = pd.Timestamp(
            '2013-06-28 9:33AM', tz='US/Eastern').tz_convert('UTC')

        del bar_data[1]

        container.update(bar_data, third_bar_dt)

        prices = container.get_history(spec, third_bar_dt)
        # The one should be forward filled

        # Prices should be
        #                             1
        # 2013-06-26 20:00:00+00:00 NaN
        # 2013-06-27 20:00:00+00:00 NaN
        # 2013-06-28 13:33:00+00:00  10

        self.assertEquals(prices[1][third_bar_dt], 10)

        # Note that we did not fill in data at the close.
        # There was a bug where a nan was being introduced because of the
        # last value of 'raw' data was used, instead of a ffilled close price.

        day_two_first_bar_dt = pd.Timestamp(
            '2013-07-01 9:31AM', tz='US/Eastern').tz_convert('UTC')

        bar_data[1] = {
            'price': 20,
            'dt': day_two_first_bar_dt
        }

        container.update(bar_data, day_two_first_bar_dt)

        prices = container.get_history(spec, day_two_first_bar_dt)

        # Prices Should Be

        #                              1
        # 2013-06-27 20:00:00+00:00  nan
        # 2013-06-28 20:00:00+00:00   10
        # 2013-07-01 13:31:00+00:00   20

        self.assertTrue(np.isnan(prices[1].ix[0]))
        self.assertEqual(prices[1].ix[1], 10)
        self.assertEqual(prices[1].ix[2], 20)

        # Clear out the bar data

        del bar_data[1]

        day_three_first_bar_dt = pd.Timestamp(
            '2013-07-02 9:31AM', tz='US/Eastern').tz_convert('UTC')

        container.update(bar_data, day_three_first_bar_dt)

        prices = container.get_history(spec, day_three_first_bar_dt)

        #                             1
        # 2013-06-28 20:00:00+00:00  10
        # 2013-07-01 20:00:00+00:00  20
        # 2013-07-02 13:31:00+00:00  20

        self.assertTrue(prices[1].ix[0], 10)
        self.assertTrue(prices[1].ix[1], 20)
        self.assertTrue(prices[1].ix[2], 20)

        day_four_first_bar_dt = pd.Timestamp(
            '2013-07-03 9:31AM', tz='US/Eastern').tz_convert('UTC')

        container.update(bar_data, day_four_first_bar_dt)

        prices = container.get_history(spec, day_four_first_bar_dt)

        #                             1
        # 2013-07-01 20:00:00+00:00  20
        # 2013-07-02 20:00:00+00:00  20
        # 2013-07-03 13:31:00+00:00  20

        self.assertEqual(prices[1].ix[0], 20)
        self.assertEqual(prices[1].ix[1], 20)
        self.assertEqual(prices[1].ix[2], 20)

class TestHistoryAlgo(TestCase):
    def setUp(self):
        np.random.seed(123)

    def test_history_daily(self):
        bar_count = 3
        algo_text = """
from zipline.api import history, add_history
from copy import deepcopy

def initialize(context):
    add_history(bar_count={bar_count}, frequency='1d', field='price')
    context.history_trace = []

def handle_data(context, data):
    prices = history(bar_count={bar_count}, frequency='1d', field='price')
    context.history_trace.append(deepcopy(prices))
""".format(bar_count=bar_count).strip()

        #      March 2006
        # Su Mo Tu We Th Fr Sa
        #          1  2  3  4
        #  5  6  7  8  9 10 11
        # 12 13 14 15 16 17 18
        # 19 20 21 22 23 24 25
        # 26 27 28 29 30 31

        start = pd.Timestamp('2006-03-20', tz='UTC')
        end = pd.Timestamp('2006-03-30', tz='UTC')

        sim_params = factory.create_simulation_parameters(
            start=start, end=end)

        _, df = factory.create_test_df_source(sim_params)
        df = df.astype(np.float64)
        source = DataFrameSource(df, sids=[0])

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='daily',
            sim_params=sim_params
        )

        output = test_algo.run(source)
        self.assertIsNotNone(output)

        history_trace = test_algo.history_trace

        for i, received in enumerate(history_trace[bar_count - 1:]):
            expected = df.iloc[i:i + bar_count]
            assert_frame_equal(expected, received)

    def test_basic_history(self):
        algo_text = """
from zipline.api import history, add_history

def initialize(context):
    add_history(bar_count=2, frequency='1d', field='price')

def handle_data(context, data):
    prices = history(bar_count=2, frequency='1d', field='price')
    prices['prices_times_two'] = prices[1] * 2
    context.last_prices = prices
""".strip()

        #      March 2006
        # Su Mo Tu We Th Fr Sa
        #          1  2  3  4
        #  5  6  7  8  9 10 11
        # 12 13 14 15 16 17 18
        # 19 20 21 22 23 24 25
        # 26 27 28 29 30 31
        start = pd.Timestamp('2006-03-20', tz='UTC')
        end = pd.Timestamp('2006-03-21', tz='UTC')

        sim_params = factory.create_simulation_parameters(
            start=start, end=end)

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)
        self.assertIsNotNone(output)

        last_prices = test_algo.last_prices[0]
        oldest_dt = pd.Timestamp(
            '2006-03-20 4:00 PM', tz='US/Eastern').tz_convert('UTC')
        newest_dt = pd.Timestamp(
            '2006-03-21 4:00 PM', tz='US/Eastern').tz_convert('UTC')

        self.assertEquals(oldest_dt, last_prices.index[0])
        self.assertEquals(newest_dt, last_prices.index[-1])

        # Random, depends on seed
        self.assertEquals(139.36946942498648, last_prices[oldest_dt])
        self.assertEquals(180.15661995395106, last_prices[newest_dt])

    def test_basic_history_one_day(self):
        algo_text = """
from zipline.api import history, add_history

def initialize(context):
    add_history(bar_count=1, frequency='1d', field='price')

def handle_data(context, data):
    prices = history(bar_count=1, frequency='1d', field='price')
    context.last_prices = prices
""".strip()

        #      March 2006
        # Su Mo Tu We Th Fr Sa
        #          1  2  3  4
        #  5  6  7  8  9 10 11
        # 12 13 14 15 16 17 18
        # 19 20 21 22 23 24 25
        # 26 27 28 29 30 31

        start = pd.Timestamp('2006-03-20', tz='UTC')
        end = pd.Timestamp('2006-03-21', tz='UTC')

        sim_params = factory.create_simulation_parameters(
            start=start, end=end)

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        self.assertIsNotNone(output)

        last_prices = test_algo.last_prices[0]
        # oldest and newest should be the same if there is only 1 bar
        oldest_dt = pd.Timestamp(
            '2006-03-21 4:00 PM', tz='US/Eastern').tz_convert('UTC')
        newest_dt = pd.Timestamp(
            '2006-03-21 4:00 PM', tz='US/Eastern').tz_convert('UTC')

        self.assertEquals(oldest_dt, last_prices.index[0])
        self.assertEquals(newest_dt, last_prices.index[-1])

        # Random, depends on seed
        self.assertEquals(180.15661995395106, last_prices[oldest_dt])
        self.assertEquals(180.15661995395106, last_prices[newest_dt])

    def test_basic_history_positional_args(self):
        """
        Ensure that positional args work.
        """
        algo_text = """
import copy
from zipline.api import history, add_history

def initialize(context):
    add_history(2, '1d', 'price')

def handle_data(context, data):

    prices = history(2, '1d', 'price')
    context.last_prices = copy.deepcopy(prices)
""".strip()

        #      March 2006
        # Su Mo Tu We Th Fr Sa
        #          1  2  3  4
        #  5  6  7  8  9 10 11
        # 12 13 14 15 16 17 18
        # 19 20 21 22 23 24 25
        # 26 27 28 29 30 31

        start = pd.Timestamp('2006-03-20', tz='UTC')
        end = pd.Timestamp('2006-03-21', tz='UTC')

        sim_params = factory.create_simulation_parameters(
            start=start, end=end)

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)
        self.assertIsNotNone(output)

        last_prices = test_algo.last_prices[0]
        oldest_dt = pd.Timestamp(
            '2006-03-20 4:00 PM', tz='US/Eastern').tz_convert('UTC')
        newest_dt = pd.Timestamp(
            '2006-03-21 4:00 PM', tz='US/Eastern').tz_convert('UTC')

        self.assertEquals(oldest_dt, last_prices.index[0])
        self.assertEquals(newest_dt, last_prices.index[-1])

        self.assertEquals(139.36946942498648, last_prices[oldest_dt])
        self.assertEquals(180.15661995395106, last_prices[newest_dt])

    def test_history_with_volume(self):
        algo_text = """
from zipline.api import history, add_history, record

def initialize(context):
    add_history(3, '1d', 'volume')

def handle_data(context, data):
    volume = history(3, '1d', 'volume')

    record(current_volume=volume[0].ix[-1])
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        start = pd.Timestamp('2007-04-10', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='minute'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        np.testing.assert_equal(output.ix[0, 'current_volume'],
                                212218404.0)

    def test_history_with_high(self):
        algo_text = """
from zipline.api import history, add_history, record

def initialize(context):
    add_history(3, '1d', 'high')

def handle_data(context, data):
    highs = history(3, '1d', 'high')

    record(current_high=highs[0].ix[-1])
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        start = pd.Timestamp('2007-04-10', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='minute'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        np.testing.assert_equal(output.ix[0, 'current_high'],
                                139.5370641791925)

    def test_history_with_low(self):
        algo_text = """
from zipline.api import history, add_history, record

def initialize(context):
    add_history(3, '1d', 'low')

def handle_data(context, data):
    lows = history(3, '1d', 'low')

    record(current_low=lows[0].ix[-1])
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        start = pd.Timestamp('2007-04-10', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='minute'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        np.testing.assert_equal(output.ix[0, 'current_low'],
                                99.891436939669944)

    def test_history_with_open(self):
        algo_text = """
from zipline.api import history, add_history, record

def initialize(context):
    add_history(3, '1d', 'open_price')

def handle_data(context, data):
    opens = history(3, '1d', 'open_price')

    record(current_open=opens[0].ix[-1])
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        start = pd.Timestamp('2007-04-10', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='minute'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        np.testing.assert_equal(output.ix[0, 'current_open'],
                                99.991436939669939)

    def test_history_passed_to_func(self):
        """
        Had an issue where MagicMock was causing errors during validation
        with rolling mean.
        """
        algo_text = """
from zipline.api import history, add_history
import pandas as pd

def initialize(context):
    add_history(2, '1d', 'price')

def handle_data(context, data):
    prices = history(2, '1d', 'price')

    pd.rolling_mean(prices, 2)
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        start = pd.Timestamp('2007-04-10', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='minute'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)

        # At this point, just ensure that there is no crash.
        self.assertIsNotNone(output)

    def test_history_passed_to_talib(self):
        """
        Had an issue where MagicMock was causing errors during validation
        with talib.

        We don't officially support a talib integration, yet.
        But using talib directly should work.
        """
        algo_text = """
import talib
import numpy as np

from zipline.api import history, add_history, record

def initialize(context):
    add_history(2, '1d', 'price')

def handle_data(context, data):
    prices = history(2, '1d', 'price')

    ma_result = talib.MA(np.asarray(prices[0]), timeperiod=2)
    record(ma=ma_result[-1])
""".strip()

        #      April 2007
        # Su Mo Tu We Th Fr Sa
        #  1  2  3  4  5  6  7
        #  8  9 10 11 12 13 14
        # 15 16 17 18 19 20 21
        # 22 23 24 25 26 27 28
        # 29 30

        # Eddie: this was set to 04-10 but I don't see how that makes
        # sense as it does not generate enough data to get at -2 index
        # below.
        start = pd.Timestamp('2007-04-05', tz='UTC')
        end = pd.Timestamp('2007-04-10', tz='UTC')

        sim_params = SimulationParameters(
            period_start=start,
            period_end=end,
            capital_base=float("1.0e5"),
            data_frequency='minute',
            emission_rate='daily'
        )

        test_algo = TradingAlgorithm(
            script=algo_text,
            data_frequency='minute',
            sim_params=sim_params
        )

        source = RandomWalkSource(start=start,
                                  end=end)
        output = test_algo.run(source)
        # At this point, just ensure that there is no crash.
        self.assertIsNotNone(output)

        recorded_ma = output.ix[-2, 'ma']

        self.assertFalse(pd.isnull(recorded_ma))
        # Depends on seed
        np.testing.assert_almost_equal(recorded_ma,
                                       159.76304468946876)


text:
import cv2
import numpy as np
import pyvirtualcam
from pyvirtualcam import PixelFormat

# Invert the feed
MIRROR_CAMERA = False

# If True, enables the virtual camera.
# ! NOTE: Requires something like OBS virtualcam to be
# ! NOTE: on the system.
OUTPUT_VIRTUAL = True
# Outputs the image to its own window. Handy for testing.
OUTPUT_LOCAL = False

# Set frame size. More pixels means more processing.
pref_width = 1920
pref_height = 1080
# Set the desired FPS. 24 is the most cinematic
pref_fps = 24

# Open video feed and configured preffered settings.
vc = cv2.VideoCapture(0)
if not vc.isOpened():
    raise RuntimeError("Could not open video feed.")

vc.set(cv2.CAP_PROP_FRAME_WIDTH, pref_width)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, pref_height)
vc.set(cv2.CAP_PROP_FPS, pref_fps)

# Get the actual settings. Previous might not
# have been OK.
width = int(vc.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = vc.get(cv2.CAP_PROP_FPS)

# ! Try to connect to the virtual camera output
try:
    if OUTPUT_VIRTUAL:
        cam = pyvirtualcam.Camera(width, height, fps, fmt=PixelFormat.BGR)
except Exception as e:
    print("Closing")
    vc.release()
    cv2.destroyAllWindows()
    raise e

while True:
    try:
        # Get if there is a frame, and the frame itself
        success, frame = vc.read()
        if not success:
            # We could not get a frame for some reason.
            continue

        # ! \/\/ Manipulate image here \/\/

        # ! /\/\ Manipulate image here /\/\

        if MIRROR_CAMERA:
            # Flip the frame
            frame = cv2.flip(frame, 1)

        if OUTPUT_LOCAL:
            # Display in window.
            cv2.imshow('Frame', frame)

        if OUTPUT_VIRTUAL:
            # Send to virtual camera
            cam.send(frame)
            cam.sleep_until_next_frame()

        if cv2.waitKey(25) & 0xFF == ord('q'):
            # If we press `q`, close the program
            raise KeyboardInterrupt

    except KeyboardInterrupt:
        break
    except Exception as e:
        vc.release()
        cv2.destroyAllWindows()
        raise e

vc.release()
cv2.destroyAllWindows()

ext: py

reason: import numpy


text:
from decomon.models.models import DecomonModel
from decomon.models.convert import clone as convert
import numpy as np
from .layers.core import Ball, Box
from .wrapper import get_upper_box, get_lower_box, refine_boxes
from .metrics.loss import get_upper_loss
from tensorflow.keras.optimizers import Adam

#### FORMAL BOUNDS ######
def get_upper_box_tuning(model, decomon_model_concat, x_min, x_max, batch_size=1, n_sub_boxes=1, lr=0.1, epochs=100):
    """
    upper bound the maximum of a model in a given box
    :param model: either a Keras model or a Decomon model
    :param x_min: numpy array for the extremal lower corner of the boxes
    :param x_max: numpy array for the extremal upper corner of the boxes
    :param batch_size: for computational efficiency, one can split the calls to minibatches
    :param fast: useful in the forward-backward or in the hybrid-backward mode to optimize the scores
    :return: numpy array, vector with upper bounds for adversarial attacks
    """

    if np.min(x_max - x_min) < 0:
        raise UserWarning("Inconsistency Error: x_max < x_min")

    fast = True
    # check that the model is a DecomonModel, else do the conversion
    # input_dim = 0
    model_=model

    baseline_upper = get_upper_box(model, x_min, x_max, batch_size=batch_size, n_sub_boxes=n_sub_boxes)

    # TO DO
    #if not model.finetune or not model.forward:
    #    return baseline_upper

    n_split = 1
    n_batch = len(x_min)

    if n_sub_boxes > 1:
        x_min, x_max = refine_boxes(x_min, x_max, n_sub_boxes)
        # reshape
        n_split = x_min.shape[1]
        shape = list(x_min.shape[2:])
        x_min = np.reshape(x_min, [-1] + shape)
        x_max = np.reshape(x_max, [-1] + shape)

    # reshape x_mmin, x_max
    input_shape = list(model_.input_shape[2:])
    input_dim = np.prod(input_shape)
    x_ = x_min + 0 * x_min
    x_ = x_.reshape([-1] + input_shape)
    x_min = x_min.reshape((-1, 1, input_dim))
    x_max = x_max.reshape((-1, 1, input_dim))

    z = np.concatenate([x_min, x_max], 1)

    if batch_size > 1:
        # split
        r = 0
        if len(x_) % batch_size > 0:
            r += 1
        X_min_ = [x_min[batch_size * i : batch_size * (i + 1)] for i in range(len(x_) // batch_size + r)]
        X_max_ = [x_max[batch_size * i : batch_size * (i + 1)] for i in range(len(x_) // batch_size + r)]

        results = [get_upper_box_tuning(model_, decomon_model_concat, X_min_[i], X_max_[i], -1, lr=lr, epochs=epochs) for i in range(len(X_min_))]

        u_ = np.concatenate(results)

        return u_
    else:

        # freeze_weights
        model.freeze_weights()

        # create loss
        loss_upper = get_upper_loss(model)
        decomon_model_concat.compile(Adam(lr=lr), loss_upper)

        decomon_model_concat.fit(z, baseline_upper, epochs=epochs, verbose=0)

        upper_ = get_upper_box(model, x_min, x_max, batch_size=batch_size, n_sub_boxes=n_sub_boxes)

        # reset alpha
        model.reset_finetuning()

        return np.minimum(baseline_upper, upper_)

#### FORMAL BOUNDS ######
def get_lower_box_tuning(model, decomon_model_concat, x_min, x_max, batch_size=1, n_sub_boxes=1, lr=0.1, epochs=100):
    """
    upper bound the maximum of a model in a given box
    :param model: either a Keras model or a Decomon model
    :param x_min: numpy array for the extremal lower corner of the boxes
    :param x_max: numpy array for the extremal upper corner of the boxes
    :param batch_size: for computational efficiency, one can split the calls to minibatches
    :param fast: useful in the forward-backward or in the hybrid-backward mode to optimize the scores
    :return: numpy array, vector with upper bounds for adversarial attacks
    """

    if np.min(x_max - x_min) < 0:
        raise UserWarning("Inconsistency Error: x_max < x_min")

    fast = True
    # check that the model is a DecomonModel, else do the conversion
    # input_dim = 0
    model_=model

    baseline_upper = get_lower_box(model, x_min, x_max, batch_size=batch_size, n_sub_boxes=n_sub_boxes)

    # TO DO
    #if not model.finetune or not model.forward:
    #    return baseline_upper

    n_split = 1
    n_batch = len(x_min)

    if n_sub_boxes > 1:
        x_min, x_max = refine_boxes(x_min, x_max, n_sub_boxes)
        # reshape
        n_split = x_min.shape[1]
        shape = list(x_min.shape[2:])
        x_min = np.reshape(x_min, [-1] + shape)
        x_max = np.reshape(x_max, [-1] + shape)

    # reshape x_mmin, x_max
    input_shape = list(model_.input_shape[2:])
    input_dim = np.prod(input_shape)
    x_ = x_min + 0 * x_min
    x_ = x_.reshape([-1] + input_shape)
    x_min = x_min.reshape((-1, 1, input_dim))
    x_max = x_max.reshape((-1, 1, input_dim))

    z = np.concatenate([x_min, x_max], 1)

    if batch_size >0 and batch_size!=len(x_min):
        # split
        r = 0
        if len(x_) % batch_size > 0:
            r += 1
        X_min_ = [x_min[batch_size * i : batch_size * (i + 1)] for i in range(len(x_) // batch_size + r)]
        X_max_ = [x_max[batch_size * i : batch_size * (i + 1)] for i in range(len(x_) // batch_size + r)]

        results = [get_lower_box_tuning(model_, decomon_model_concat, X_min_[i], X_max_[i], -1, lr=lr, epochs=epochs) for i in range(len(X_min_))]

        u_ = np.concatenate(results)
        model.reset_finetuning()

        return u_
    else:

        # freeze_weights
        model.freeze_weights()

        # create loss
        loss_upper = get_upper_loss(model)
        decomon_model_concat.compile(Adam(lr=lr), loss_upper)

        decomon_model_concat.fit(z, baseline_upper, epochs=epochs, verbose=0)

        upper_ = get_lower_box(model, x_min, x_max, batch_size=batch_size, n_sub_boxes=n_sub_boxes)

        # reset alpha
        model.reset_finetuning()

        return np.minimum(baseline_upper, upper_)

max_stars_repo_name: airbus/decomon

max_issues_repo_name: airbus/decomon

max_forks_repo_name: airbus/decomon


text:
# License : https://github.com/slezica/python-frozendict/blob/master/LICENSE.txt

import collections
import operator
import functools

from theano.compat import Mapping

class frozendict(Mapping):
    """
    An immutable wrapper around dictionaries that implements the complete :py:class:`collections.abc.Mapping`
    interface. It can be used as a drop-in replacement for dictionaries where immutability and ordering are desired.
    """

    dict_cls = dict

    def __init__(self, *args, **kwargs):
        self._dict = self.dict_cls(*args, **kwargs)
        self._hash = None

    def __getitem__(self, key):
        return self._dict[key]

    def __contains__(self, key):
        return key in self._dict

    def copy(self, **add_or_replace):
        return self.__class__(self, **add_or_replace)

    def __iter__(self):
        return iter(self._dict)

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

    def __repr__(self):
        return "<%s %r>" % (self.__class__.__name__, self._dict)

    def __hash__(self):
        if self._hash is None:
            hashes = map(hash, self.items())
            self._hash = functools.reduce(operator.xor, hashes, 0)

        return self._hash

class FrozenOrderedDict(frozendict):
    """
    A FrozenDict subclass that maintains key order
    """

    dict_cls = collections.OrderedDict

max_stars_repo_name: bridgeland/Theano-PyMC

max_issues_repo_name: bridgeland/Theano-PyMC

max_forks_repo_name: bridgeland/Theano-PyMC

reason: from theano


text:
import numpy
from typing import Optional
from pydantic import Field, constr
import hashlib
import json

# MM models
from mmelemental.models.base import ProtoModel, Provenance
from cmselemental.types import Array

from mmelemental.util.data import (
    float_prep,
    NUMPY_INT,
    NUMPY_FLOAT,
    NUMPY_UNI,
    CHARGE_NOISE,
    MASS_NOISE,
)

__all__ = ["Topology"]

mmschema_topology_default = "mmschema_topology"

class Topology(ProtoModel):
    """A topological representation of a Molecule in MM. In addition to connectivity, this model contains data for particle
    symbols/labels, masses, and net charge. Useful for creating :class:``Trajectory`` objects.
    """

    schema_name: constr(
        strip_whitespace=True, regex=mmschema_topology_default
    ) = Field(  # type: ignore
        mmschema_topology_default,
        description=(
            f"The MMSchema specification to which this model conforms. Explicitly fixed as {mmschema_topology_default}."
        ),
    )
    schema_version: int = Field(  # type: ignore
        1,
        description="The version number of ``schema_name`` to which this model conforms.",
    )
    name: Optional[str] = Field(  # type: ignore
        None,
        description="Common or human-readable name to assign to this molecule. This field can be arbitrary.",
    )
    comment: Optional[str] = Field(  # type: ignore
        None,
        description="Additional comments for this molecule. Intended for pure human/user consumption and clarity.",
    )
    symbols: Optional[Array[NUMPY_UNI]] = Field(  # type: ignore
        None,
        description="An ordered (natom,) array-like object of particle symbols. The index of "
        "this attribute sets the order for all other per-particle setting like ``geometry`` and the first "
        "dimension of ``geometry``.",
    )
    atom_labels: Optional[Array[NUMPY_UNI]] = Field(  # type: ignore
        None,
        description="Additional per-atom labels as an array of strings. Typical use is in "
        "model conversions, such as Elemental <-> Molpro and not typically something which should be user "
        "assigned. See the ``comments`` field for general human-consumable text to affix to the molecule.",
    )
    atomic_numbers: Optional[Array[numpy.dtype(NUMPY_INT)]] = Field(  # type: ignore
        None,
        description="An optional ordered 1-D array-like object of atomic numbers of shape (nat,). Index "
        "matches the 0-indexed indices of all other per-atom settings like ``symbols`` and ``real``. "
        "Values are inferred from the ``symbols`` list if not explicitly set. "
        "Ghostedness should be indicated through ``real`` field, not zeros here.",
    )
    mass_numbers: Optional[Array[numpy.dtype(NUMPY_INT)]] = Field(  # type: ignore
        None,
        description="An optional ordered 1-D array-like object of atomic *mass* numbers of shape (nat). Index "
        "matches the 0-indexed indices of all other per-atom settings like ``symbols`` and ``real``. "
        "Values are inferred from the most common isotopes of the ``symbols`` list if not explicitly set. "
        "If single isotope not (yet) known for an atom, -1 is placeholder.",
    )
    masses: Optional[Array[numpy.dtype(NUMPY_FLOAT)]] = Field(  # type: ignore
        None,
        description="The ordered array of particle masses. Index order "
        "matches the 0-indexed indices of all other per-atom fields like ``symbols`` and ``real``. If "
        "this is not provided, the mass of each atom is inferred from its most common isotope. If this "
        "is provided, it must be the same length as ``symbols`` but can accept ``None`` entries for "
        "standard masses to infer from the same index in the ``symbols`` field.",
    )
    masses_units: Optional[str] = Field(  # type: ignore
        "amu",
        description="Units for atomic masses. Defaults to unified atomic mass unit.",
    )
    molecular_charge: Optional[float] = Field(  # type: ignore
        0.0,
        description="The net electrostatic charge of the molecule. Default unit is elementary charge.",
    )
    molecular_charge_units: Optional[str] = Field(  # type: ignore
        "e", description="Units for molecular charge. Defaults to elementary charge."
    )
    connectivity: Optional[Array[numpy.dtype(f"{NUMPY_INT}, {NUMPY_INT}, {NUMPY_FLOAT}")]] = Field(  # type: ignore
        None,
        description="A list of bonds within the molecule. Each entry is a tuple "
        "of ``(atom_index_A, atom_index_B, bond_order)`` where the ``atom_index`` "
        "matches the 0-indexed indices of all other per-atom settings like ``symbols`` and ``real``. "
        "Bonds may be freely reordered and inverted.",
    )

    def __repr_args__(self) -> "ReprArgs":
        return [("name", self.name), ("hash", self.get_hash()[:7])]

    def __eq__(self, other):
        """
        Checks if two models are identical. This is a molecular identity defined
        by scientific terms, and not programing terms, so it's less rigorous than
        a programmatic equality or a memory equivalent `is`.
        """

        if isinstance(other, dict):
            other = self.__class__(**other)
        elif isinstance(other, self.__class__):
            pass
        else:
            raise TypeError(
                f"Comparison between {self.__class__} and {type(other)} is not supported."
            )

        return self.get_hash() == other.get_hash()

    @property
    def hash_fields(self):
        return [
            "symbols",
            "masses",
            "masses_units",
            "molecular_charge",
            "molecular_charge_units",
            "connectivity",
        ]

    def get_hash(self):
        """
        Returns the hash of the molecule.
        """

        m = hashlib.sha1()
        concat = ""

        # np.set_printoptions(precision=16)
        for field in self.hash_fields:
            data = getattr(self, field)
            if data is not None:
                if field == "molecular_charge":
                    data = float_prep(data, CHARGE_NOISE)
                elif field == "masses":
                    data = float_prep(data, MASS_NOISE)

                concat += json.dumps(data, default=lambda x: x.ravel().tolist())

        m.update(concat.encode("utf-8"))
        return m.hexdigest()

max_stars_repo_name: RlyehAD/mmelemental

max_issues_repo_name: RlyehAD/mmelemental

max_forks_repo_name: RlyehAD/mmelemental


text:
from math import log, e
from time import process_time # remove this once functions are finished
import numpy as np
import pandas as pd

#%% Define some functions for statisical analysis on model ensembles

# information entropy - this assumes the inputs are labels, but also works with numerics

def entropy_custom(array, base=None):
  """ Computes entropy of label distribution. """

  n_labels = len(array)

  if n_labels <= 1:
    return 0

  value,counts = np.unique(array, return_counts=True)
  probs = counts / n_labels
  n_classes = np.count_nonzero(probs)

  if n_classes <= 1:
    return 0

  ent = 0.

  # Compute entropy
  base = e if base is None else base
  for i in probs:
    ent -= i * log(i, base)

  return ent

#%% calculate the probability (between 0 and 1) of a lithology in a cell

def litho_probabilites(array):
    lithos = np.unique(array)
    litho_prob = pd.DataFrame([])
    for i in range(0, int(lithos.max())+1):
        temp = array[array == i]
        temp_counts = temp.count(axis=1)
        litho_prob = litho_prob.append(temp_counts, ignore_index=True)
    litho_prob = litho_prob / array.shape[1]
    return(litho_prob)

#%%
# group splitter

def split(x, n):
    # If we cannot split the
    # number into exactly 'N' parts
    if (x < n):
        return -1

    # If x % n == 0 then the minimum
    # difference is 0 and all
    # numbers are x / n
    elif (x % n == 0):
        temp = np.zeros(n)
        for i in range(n):
            temp[i] = x // n
        return temp
    else:
        # upto n-(x % n) the values
        # will be x / n
        # after that the values
        # will be x / n + 1
        temp = np.zeros(n)
        zp = n - (x % n)
        pp = x // n
        for i in range(n):
            if (i >= zp):
                temp[i] = pp + 1
            else:
                temp[i] = pp

        return temp


text:
import numpy as np
from numpy import linalg as la
from matrixmath import randn

from ltimultgen import gen_system_mult, gen_system_erdos_renyi
from policygradient import PolicyGradientOptions, run_policy_gradient
from ltimult import dlyap_obj

from matplotlib import pyplot as plt

from time import time,sleep

import os
from utility import create_directory

from pickle_io import pickle_import,pickle_export

def set_initial_gains(SS,K0_method):
    # Initial gains
    if K0_method=='random':
        K0 = randn(*SS.Kare.shape)
        SS.setK(K0)
        while not SS.c < np.inf:
            K0 = randn(*SS.Kare.shape) # May take forever
            SS.setK(K0)
    elif K0_method=='random_olmss':
        K0 = randn(*SS.Kare.shape)
        SS.setK(K0)
        while not SS.c < np.inf:
            K0 = 0.9*K0 # Only guaranteed to work if sys open loop mean-square stable
            SS.setK(K0)
    elif K0_method=='are':
        K0 = SS.Kare
        print('Initializing at the ARE solution')
    elif K0_method=='are_perturbed':
        perturb_scale = 10.0/SS.n  # scale with 1/n as rough heuristic
        safety_scale = 10.0
        Kp = randn(SS.Kare.shape[0],SS.Kare.shape[1])
        K0 = SS.Kare + perturb_scale*Kp
        SS.setK(K0)
        while not SS.c < safety_scale*SS.ccare:
            perturb_scale *= 0.2
            K0 = SS.Kare + perturb_scale*Kp
            SS.setK(K0)
        while not SS.c > safety_scale*SS.ccare:
            perturb_scale *= 1.01
            K0 = SS.Kare + perturb_scale*Kp
            SS.setK(K0)
        perturb_scale /= 1.01
        K0 = SS.Kare + perturb_scale*Kp
        SS.setK(K0)
    elif K0_method=='zero':
        K0 = np.zeros([SS.m,SS.n])
        SS.setK(K0)
        P = dlyap_obj(SS,algo='iterative',show_warn=False)
        if P is not None:
            print('Initializing with zero gain solution')
        else:
            print('System not open-loop mean-square stable, use a different initial gain setting')
    SS.setK(K0)
    return K0

def policy_gradient_setup(SS, eta, step_direction, max_iters, exact, stepsize_method, nr):
    # Gradient descent options
    if step_direction == 'gradient_model_free':
        step_direction = 'gradient'
    PGO = PolicyGradientOptions(epsilon=0,
                                eta=eta,
                                max_iters=max_iters,
                                disp_stride=1,
                                keep_hist=True,
                                opt_method='gradient',
                                keep_opt='last',
                                step_direction=step_direction,
                                stepsize_method=stepsize_method,
                                exact=exact,
                                regularizer=None,
                                regweight=0,
                                stop_crit='fixed',
                                fbest_repeat_max=0,
                                display_output=True,
                                display_inplace=True,
                                slow=False,
                                nt=20,
                                nr=nr,
                                ru=0.1)
    return PGO

def load_system(timestr):
    # Import
    dirname_in = os.path.join('systems_keepers',timestr)
    filename_only = 'system_init.pickle'
    SS = pickle_import(os.path.join(dirname_in,filename_only))
    #Export
    timestr = str(time()).replace('.','p')
    dirname_out = os.path.join('systems',timestr)
    SS.dirname = dirname_out
    filename_out = os.path.join(dirname_out,filename_only)
    pickle_export(dirname_out, filename_out, SS)
    return SS

def plot_data(all_dict, dirname_in):
    img_dirname_out = os.path.join(dirname_in,'analysis_plots')
    create_directory(img_dirname_out)

    nSS = len(all_dict['gradient']['costnorm'])

    lw = 3
    fs = 14
    markerdict = {'gradient': 'o',
                  'gradient_model_free': 's',
                  'natural_gradient': 'v',
                  'gauss_newton': '^'}
    linestyle_dict = {'gradient': ':',
                      'gradient_model_free': '-',
                      'natural_gradient': '-.',
                      'gauss_newton': '--'}
    ms = 8
    pmax = 90
    pmin = 10

    figsize = (6, 3)
    leg_str_dict = {'gradient_model_free': 'Gradient (model-free)',
                    'gradient': 'Gradient',
                    'natural_gradient': 'Natural gradient',
                    'gauss_newton': 'Gauss-Newton'}

    quant_list = ['costnorm','gradnorm']
    for quant in quant_list:
        leg_str_list = []
        xlab = 'Iteration'
        if quant=='costnorm':
            ylab = 'Normalized cost diff.'
        elif quant=='gradnorm':
            ylab = 'Fro. norm of gradient'

        plt.figure(figsize=figsize)
        # plt.xscale('log')
        plt.yscale('log')
        plt.xlabel(xlab,fontsize=fs)
        plt.ylabel(ylab,fontsize=fs)

        for step_direction in list(leg_str_dict.keys()):
            leg_str_list.append(leg_str_dict[step_direction])
            max_iters = all_dict[step_direction][quant][0].size
            quant_all = np.zeros([nSS,max_iters])
            for i in range(nSS):
                quant_all[i] = all_dict[step_direction][quant][i]
            quant_mean = quant_all.mean(axis=0)

            # max_iters_partial = 20
            max_iters_partial = max_iters

            x = np.arange(max_iters_partial)+1
            upr = np.percentile(quant_all,pmax,axis=0)[0:max_iters_partial]
            lwr = np.percentile(quant_all,pmin,axis=0)[0:max_iters_partial]
            mid = quant_mean[0:max_iters_partial]
            plt.fill_between(x,lwr,upr,alpha=0.3)
            plt.plot(x, mid, linewidth=lw, linestyle=linestyle_dict[step_direction])
            if max_iters_partial <= 20:
                plt.xticks(ticks=1+np.arange(max_iters_partial))

        # Position the legend to custom location
        plt.legend(leg_str_list, loc='right', bbox_to_anchor=(0.97, 0.35))
        # plt.legend(leg_str_list, loc='right', bbox_to_anchor=(0.97, 0.65))
        #
        plt.show()
        filename_out = 'plot_'+quant+'_vs_iteration_random'
        path_out = os.path.join(img_dirname_out,filename_out)
        plt.savefig(path_out, dpi=300, bbox_inches='tight')

def routine_gen():
    # folderstr = 'systems'
    # timestr = str(time()).replace('.','p')
    # dirname_in = os.path.join(folderstr,timestr)
    # create_directory(dirname_in)

    nSS = 20 # Number of independent runs

    # Settings for backtracking line search
    stepsize_method = 'backtrack'
    nr = 100000
    PGO_dict = {'gradient_model_free': {'eta': 1e-1, 'max_iters': 20, 'exact': False},
                'gradient': {'eta': 1e-1, 'max_iters': 20, 'exact': True},
                'natural_gradient': {'eta': 1e-1, 'max_iters': 20, 'exact': True},
                'gauss_newton': {'eta': 1/2, 'max_iters': 20, 'exact': True}}

    all_dict = {key: {'costnorm':[],'gradnorm':[]} for key in PGO_dict.keys()}

    # Generate system from scratch
    seed = 1
    # SS = gen_system_erdos_renyi(n=4,
    #                             diffusion_constant=1.0,
    #                             leakiness_constant=0.1,
    #                             time_constant=0.05,
    #                             leaky=True,
    #                             seed=seed)
    # SS = gen_system_erdos_renyi(n=2,
    #                             diffusion_constant=1.0,
    #                             leakiness_constant=0.1,
    #                             time_constant=0.05,
    #                             leaky=True,
    #                             seed=seed)

    # Load system
    folderstr = 'example_systems'
    timestr = '1587086073p9661696_example_network_all_steps_backtrack'
    dirname_in = os.path.join(folderstr, timestr)
    filename_in = os.path.join(dirname_in, 'system_init.pickle')
    SS = pickle_import(filename_in)

    for i in range(nSS):
        # Policy gradient setup
        K0_method = 'zero'
        K0 = set_initial_gains(SS,K0_method=K0_method)

        sleep(0.5)

        for step_direction in PGO_dict:
            SS.setK(K0)
            t_start = time()
            eta = PGO_dict[step_direction]['eta']
            max_iters = PGO_dict[step_direction]['max_iters']
            exact = PGO_dict[step_direction]['exact']
            PGO = policy_gradient_setup(SS, eta, step_direction, max_iters, exact, stepsize_method, nr)
            t_end = time()
            print('Initialization completed after %.3f seconds' % (t_end-t_start))

            SS, histlist = run_policy_gradient(SS,PGO)

            costnorm = (histlist[2]/SS.ccare)-1
            gradnorm = la.norm(histlist[1], ord='fro', axis=(0,1))

            all_dict[step_direction]['costnorm'].append(costnorm)
            all_dict[step_direction]['gradnorm'].append(gradnorm)

    filename_out = 'monte_carlo_all_dict.pickle'
    path_out = os.path.join(dirname_in,filename_out)
    pickle_export(dirname_in,path_out,all_dict)

    plot_data(all_dict,dirname_in)

def routine_load():
    folderstr = 'example_systems'
    timestr = '1587086073p9661696_example_network_all_steps_backtrack'
    dirname_in = os.path.join(folderstr, timestr)
    filename_in = os.path.join(dirname_in, 'monte_carlo_all_dict.pickle')
    all_dict = pickle_import(filename_in)
    plot_data(all_dict,dirname_in)

###############################################################################
if __name__ == "__main__":
    # routine_gen()
   routine_load()

max_stars_repo_name: TSummersLab/polgrad-multinoise

max_issues_repo_name: TSummersLab/polgrad-multinoise

max_forks_repo_name: TSummersLab/polgrad-multinoise

reason: import numpy,from numpy


text:
import functools, hashlib

import numpy as np
from scipy.stats import norm as normal_dbn

import rpy2.robjects as rpy
from rpy2.robjects import numpy2ri
numpy2ri.activate()

from selection.learning.learners import mixture_learner
from selection.learning.utils import naive_partial_model_inference, pivot_plot
from selection.learning.core import random_forest_fit_sk, infer_general_target

def probit_MLE(X, y, formula_terms, truth=None, alpha=0.1):

    numpy2ri.activate()
    rpy.r.assign('X', X)
    rpy.r.assign('y', y)
    rpy.r('D = data.frame(X, y)')
    rpy.r('M = glm(y ~ %s, family=binomial(link="probit"), data=D)' %
          ' + '.join(formula_terms))
    beta_hat = rpy.r('coef(M)')
    target_cov = rpy.r('vcov(M)')

    if truth is None:
        truth = np.zeros_like(beta_hat)
    SE = np.sqrt(np.diag(target_cov))

    Z = (beta_hat - truth) / SE
    Z0 = beta_hat / SE

    pvalues = normal_dbn.cdf(Z0)
    pvalues = 2 * np.minimum(pvalues, 1 - pvalues)

    pivots = normal_dbn.cdf(Z)
    pivots = 2 * np.minimum(pivots, 1 - pivots)

    upper = beta_hat + normal_dbn.ppf(1 - 0.5 * alpha) * SE
    lower = beta_hat - normal_dbn.ppf(1 - 0.5 * alpha) * SE

    covered = (upper > truth) * (lower < truth)

    results_df = pd.DataFrame({'naive_pivot':pivots,
                               'naive_pvalue':pvalues,
                               'naive_coverage':covered,
                               'naive_length':upper - lower,
                               'naive_upper':upper,
                               'naive_lower':lower,
                               'variable':formula_terms,
                               })

    return beta_hat, target_cov, results_df

#### A parametric model will need something like this

class probit_step_learner(mixture_learner):

    def __init__(self,
                 algorithm,
                 observed_selection,
                 target_cov,
                 X,
                 observed_MLE,
                 observed_Y):

        (self.algorithm,
         self.observed_outcome,
         self.target_cov,
         self.X,
         self.observed_MLE,
         self.observed_Y) = (algorithm,
                             observed_selection,
                             target_cov,
                             X,
                             observed_MLE,
                             observed_Y)

        n, p = X.shape
        var_select = ['X%d' % (i+1) in observed_selection for i in range(p)]
        self.X_select = np.hstack([np.ones((n, 1)), X[:,var_select]])

        self.observed_target = observed_MLE
        self._chol = np.linalg.cholesky(self.target_cov)
        self._beta_cov = self.target_cov

    def learning_proposal(self):
        """
        Return perturbed data and perturbed MLE.
        """

        n, s = self.X_select.shape

        beta_hat = self.observed_MLE

        perturbed_beta = beta_hat.copy()
        nidx = np.random.choice(np.arange(s), min(3, s), replace=False)
        for idx in nidx:
            scale = np.random.choice(self.scales, 1)
            perturbed_beta[idx] += (scale * np.random.standard_normal() *
                                    np.sqrt(self._beta_cov[idx, idx]))

        linpred = self.X_select.dot(perturbed_beta)
        prob = normal_dbn.cdf(linpred)
        perturbed_Y = np.random.binomial(1, prob)

        perturbed_MLE = probit_MLE(self.X, perturbed_Y, self.observed_outcome)[0]
        return perturbed_MLE, perturbed_Y

####

def simulate(n=500, p=10, alpha=0.1, B=2000):

    # description of statistical problem

    X = np.random.standard_normal((n, p))
    y = np.random.binomial(1, 0.5, size=(n,))
    truth = np.zeros(p+1)

    def algorithm(X, y):

        numpy2ri.activate()
        rpy.r.assign('X', X)
        rpy.r.assign('y', y)
        rpy.r('''
y = as.matrix(y)
D = data.frame(X, y)
glm(y ~ ., family=binomial(link='probit'), data=D)
M = glm(y ~ ., family=binomial(link='probit'), data=D)
M0 = glm(y ~ 1, family=binomial(link='probit'), data=D)
Mselect = step(M0, direction='forward', scope=list(upper=M, lower=M0), trace=FALSE)
selected_vars = names(coef(Mselect))
''')
        selected_vars = ' + '.join(sorted(list(rpy.r('selected_vars'))))
        selected_vars = selected_vars.replace('(Intercept)', '1')
        numpy2ri.deactivate()
        return tuple(selected_vars.split(' + '))

    # run selection algorithm

    selection_algorithm = functools.partial(algorithm, X)

    instance_hash = hashlib.md5()
    instance_hash.update(X.tobytes())
    instance_hash.update(y.tobytes())
    instance_hash.update(truth.tobytes())
    instance_id = instance_hash.hexdigest()

    observed_model = selection_algorithm(y)
    print(observed_model)

    proj_truth = np.zeros(len(observed_model)) # null simulation here
    MLE, target_cov, naive_df = probit_MLE(X,
                                           y,
                                           observed_model,
                                           truth=proj_truth,
                                           alpha=alpha)

    (pivots,
     covered,
     lengths,
     pvalues,
     lower,
     upper) = [], [], [], [], [], []

    targets = []

    s = len(observed_model)

    learner = probit_step_learner(selection_algorithm,
                                  observed_model,
                                  target_cov,
                                  X,
                                  MLE,
                                  y)

    results = infer_general_target(observed_model,
                                   MLE,
                                   target_cov,
                                   learner,
                                   hypothesis=proj_truth,
                                   fit_probability=random_forest_fit_sk,
                                   fit_args={'n_estimators':5000},
                                   alpha=alpha,
                                   B=B)

    for result, true_target in zip(results, proj_truth):

        (pivot,
         interval,
         pvalue,
         _) = result

        pvalues.append(pvalue)
        pivots.append(pivot)
        covered.append((interval[0] < true_target) * (interval[1] > true_target))
        lengths.append(interval[1] - interval[0])
        lower.append(interval[0])
        upper.append(interval[1])

    df = pd.DataFrame({'pivot':pivots,
                       'pvalue':pvalues,
                       'coverage':covered,
                       'length':lengths,
                       'upper':upper,
                       'lower':lower,
                       'target':proj_truth,
                       'variable':list(observed_model),
                       'id':[instance_id]*len(pivots),
                       })

    df = pd.merge(df, naive_df, on='variable')

    return df

if __name__ == "__main__":
    import statsmodels.api as sm
    import matplotlib.pyplot as plt
    import pandas as pd

    for i in range(2000):
        df = simulate(B=1000)
        csvfile = 'probit_step.csv'
        outbase = csvfile[:-4]

        if df is not None and i > 0:

            try: # concatenate to disk
                df = pd.concat([df, pd.read_csv(csvfile)])
            except FileNotFoundError:
                pass
            df.to_csv(csvfile, index=False)

            if len(df['pivot']) > 0:
                pivot_ax, length_ax = pivot_plot(df, outbase)

reason: import numpy,from scipy,import statsmodels


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

n_groups = 2

# a=(2.751,,,,)
SeqGAN = 2.588
RankGAN = 0.449
LeakGAN = 3.011
RelGAN = 3.407

means_men = 20

means_women = 25

SentiGAN = 3.117
CSGAN = 2.017
CatGAN_s = 3.601
CatGAN_m = 3.411

# plt.figure(figsize=(10, 100))
fig, ax = plt.subplots(figsize=(6, 3))

index = np.arange(n_groups)
bar_width = 0.5

opacity = 1.0
error_config = {'ecolor': '0'}

rects1 = ax.bar(0, CSGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#8e44ad', error_kw=error_config,
                label='CSGAN')

rects2 = ax.bar(bar_width, SentiGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#27ae60', error_kw=error_config,
                label='SentiGAN')

rects3 = ax.bar(0 + 2 * bar_width, CatGAN_m, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#d35400', error_kw=error_config,
                label='CatGAN ($k=2$)')
gap = 1.2
rects4 = ax.bar(3 * bar_width + gap, SeqGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#fd79a8', error_kw=error_config,
                label='SeqGAN')

rects5 = ax.bar(4 * bar_width + gap, RankGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#34495e', error_kw=error_config,
                label='RankGAN')

rects6 = ax.bar(0 + 5 * bar_width + gap, LeakGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#f1c40f', error_kw=error_config,
                label='LeakGAN')
rects7 = ax.bar(6 * bar_width + gap, RelGAN, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#2980b9', error_kw=error_config,
                label='RelGAN')
rects8 = ax.bar(7 * bar_width + gap, CatGAN_s, bar_width, linestyle='-', linewidth=1, edgecolor='black',
                alpha=opacity, color='#c0392b', error_kw=error_config,
                label='CatGAN ($k=1$)')

ax.set_xlabel('Dataset')
ax.set_ylabel('Human Score')
# ax.set_title('Scores by group and gender')
len = ((0 + 3 * bar_width) / 3, 3 * bar_width + gap + 2 * bar_width)
ax.set_xticks(len)
ax.set_xticklabels(('AR', 'EN'))
ax.legend(bbox_to_anchor=(1, 0), loc=3, borderaxespad=0.2)
# plt.legend()
fig.tight_layout()

plt.savefig('savefig/human.pdf')
plt.show()
# plt.savefig('C:/1123.pdf')

max_issues_repo_name: jianqiaoSE/ceGan

max_forks_repo_name: jianqiaoSE/ceGan


text:
# coding = utf-8

# @time    : 2019/6/20 3:52 PM
# @author  : alchemistlee
# @fileName: scan_corpus.py
# @abstract:

import os
from api_utils import get_youdao_res, logging
import Levenshtein
import random
import numpy as np
import time

def load_corpus_file(catalog_file):
  res = dict()
  for line in open(catalog_file,'r'):
    line = line.strip()
    dir = os.path.dirname(line)
    filename = os.path.basename(line)
    suffix = os.path.splitext(filename)
    suffix = suffix[-1].strip('.')
    print('path = {} , filename = {} , suffix = {} '.format(dir,filename,suffix))
    if not dir in res.keys():
      tmp_dict = dict()
      tmp_dict[suffix] = filename
      res[dir] = tmp_dict
    else :
      tmp_dict = res[dir]
      tmp_dict[suffix] = filename
      res[dir] = tmp_dict
  return res

def get_distance(l, r):
  return Levenshtein.ratio(l,r)

def get_baseline(input):
  return get_youdao_res(input,direction='zhen')

def random_sample(en_file, zh_file, rate):
  selected_idx = list()
  zh_sample = list()
  en_sample = list()

  index = 0
  for line in open(zh_file):
    line = line.strip()
    if random.random() <= rate:
      zh_sample.append(line)
      selected_idx.append(index)
    index+=1

  doc_cnt = index+1

  index = 0
  for line in open(en_file):
    line = line.strip()
    if index in selected_idx:
      en_sample.append(line)
    index +=1

  return zh_sample, en_sample, doc_cnt

def summary_score(scores,doc_cnt):
  if len(scores) == 0:
    print('score is empty ! ')
    return 0, 0, 0

  avg_score = np.mean(scores)
  mid_score = np.median(scores)
  good_score_cnt = 0
  for s in scores:
    if s >= 0.6:
      good_score_cnt +=1
  return avg_score, mid_score, good_score_cnt/len(scores)

def main(catalog_file, log_file):
  all_corpus = load_corpus_file(catalog_file)

  with open(log_file,'w') as log_fh:
    for dir in all_corpus.keys():
      tmp_info = all_corpus[dir]
      en_file = tmp_info['en']
      zh_file = tmp_info['zh']

      logging(log_fh, 'dir = {}'.format(dir))
      logging(log_fh, 'en = {} , zh = {}'.format(en_file,zh_file))

      tmp_en = dir + '/'+ en_file
      tmp_zh = dir + '/'+ zh_file

      zh_sample, en_sample, doc_size = random_sample(tmp_en, tmp_zh, 0.001)

      assert len(zh_sample) == len(en_sample)

      scores = []
      logging(log_fh, 'sample-size = {} , doc-size = {} '.format(len(zh_sample),doc_size))
      for i in range(len(zh_sample)):
        zh_line = zh_sample[i]
        en_line = en_sample[i]
        baseline_en = get_youdao_res(zh_line)
        time.sleep(1)
        if baseline_en is None:
          continue

        baseline_en = baseline_en[0]

        sim_score = get_distance(baseline_en, en_line)
        print('base = {} , ori = {}'.format(baseline_en, en_line))
        print('score = {}'.format(str(sim_score)))
        scores.append(sim_score)

      avg, mid, good_per = summary_score(scores, doc_size)
      logging(log_fh, 'avg = {} , mid = {} , good_per = {} '.format(avg, mid, good_per))

if __name__ == '__main__':
  main(catalog_file='/root/workspace/translate_data/corpus_file_list',log_file='/root/workspace/translate_data/corpus.log')

max_stars_repo_name: Tigerye/OpenNMT-py

max_issues_repo_name: Tigerye/OpenNMT-py

max_forks_repo_name: Tigerye/OpenNMT-py


text:
# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest, f_regression
import matplotlib.pyplot as plt

def setMonths2(data):
    """
    data: pandas DataFrame
    Replaces the month feature by 12 features, one for each month, which can only be 1 or 0.
    """
    months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
    for month in months:
        new_column = pd.Series(0, index=data.index)
        new_column[data.month == month] = 1
        data[month] = new_column
    data.drop('month', axis=1, inplace=True)

def setDays2(data):
    """
    data: pandas DataFrame
    Replaces the month feature by 12 features, one for each month, which can only be 1 or 0.
    """
    days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
    for day in days:
        new_column = pd.Series(0, index=data.index)
        new_column[data.day == day] = 1
        data[day] = new_column
    data.drop('day', axis=1, inplace=True)

def setWeekend(data, deleteDay=True):
    """
    data: pandas DataFrame
    Replaces the day feature by a weekend feature, whose value is 1 if the day is Saturday or Sunday, 0 otherwise.
    """
    new_col = pd.Series(0, index=data.index)
    new_col[data['day'].isin(['sat', 'sun'])] = 1
    data['weekend'] = new_col
    if deleteDay:
        data.drop('day', axis=1, inplace=True)

def setDays(data):
    """
    data : pandas DataFrame
    Replaces the name of the day of the week by a number
    """
    days = {'sun':7, 'mon':1, 'tue':2, 'wed':3, 'thu':4, 'fri':5, 'sat':6}
    a = pd.Series(index=data.index)
    for d in days.keys():
        a[data.day == d] = days[d]
    data.day = a

def setMonths(data):
    """
    data : pandas DataFrame
    Replaces the name of the month by a number
    """
    months = {'jan':1, 'feb':2, 'mar':3, 'apr':4, 'may':5, 'jun':6, 'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12}
    a = pd.Series(index=data.index)
    for m in months.keys():
        a[data.month == m] = months[m]
    data.month = a

def scaleData(X):
    """
    X : pandas DataFrame with only numerical values
    This function linearly scales the features of X, to make each value lie between 0 and 1
    """
    M, m = X.max(), X.min()
    for col in X.columns:
        if M[col] != m[col]:
            X[col] = (X[col] - m[col])/(M[col] - m[col])

def normalizeData(X, columns='all'):
    """
    X : pandas DataFrame with only numerical values
    This function linearly scales the features of X, to make it centered with a unitary standard deviation
    """
    M, S = X.mean(), X.std()
    if columns == 'all':
        colList = X.columns
    else:
        colList = columns
    for col in colList:
        if S[col] == 0:
            X[col] = 0
        else:
            X[col] = (X[col] - M[col])/S[col]

def PCA(A, y, k):
    """
    A : Numpy Array, k : integer
    Performs PCA on A
    """
    M = np.tile(np.average(A, axis=0), (A.shape[0], 1)) # Mean of the columns
    C = A - M
    W = np.dot(np.transpose(C), C)
    _, eigvec = np.linalg.eigh(W)
    eigvec = eigvec[:,::-1] # eigenvalues in ascending order : colums of U must be reversed
    Uk = eigvec[:,:k]
    return np.dot(A, Uk)

def featureSelect(X, y, j):
    Xnew = SelectKBest(f_regression, k=j).fit_transform(X, y)
    return Xnew

def plotData(X, y, features=None, sepFig=False, log=False):
    a = np.array(y)
    if features == None:
        colList = X.columns
    else:
        colList = features
    for col in colList:
        if sepFig:
            plt.figure()
            plt.title(col)
        A = np.array(X[col])
        if log:
            a = np.log(a)
        plt.plot(A, a, '+', label=col)
    if not sepFig:
        plt.legend()

def describeData(X, y, features=None, log=False):
    """
    X: pandas DataFrame of the input vectors
    y: pandas DataFrame of the labels
    features: list of the features to describe (they have to be categorical)
    Returns a pandas DataFrame with statistical information about y for each possible value of the feature
    """
    if log:
        a = np.log(y)
    else:
        a = y
    if features == None:
        colList = X.columns
    else:
        colList = features
    D = pd.DataFrame()
    for col in colList:
        values = pd.unique(X[col])
        for val in values:
            D[col + ' = ' + str(val)] = a[X[col] == val].describe()
    return D

def setCategorical(X, col, deleteCol=True):
    """
    X: pandas DataFrame
    col: str
    Generalization of setDays2 and setMonth2
    """
    values = pd.unique(X[col])
    for val in values:
        new_col = pd.Series(0, index=X.index)
        new_col[X[col] == val] = 1
        X[str(val)] = new_col
    if deleteCol:
        X.drop(col, axis=1, inplace=True)

def setSeason(X, deleteMonth=True):
    """
    X: pandas DataFrame
    Creates a new feature: season. It can have four values (win, spr, sum, and aut) depending on the month
    """
    new_col = pd.Series(index=X.index)
    new_col[X['month'].isin(['dec', 'jan', 'feb'])] = 'win'
    new_col[X['month'].isin(['mar', 'apr', 'may'])] = 'spr'
    new_col[X['month'].isin(['jun', 'jul', 'aug'])] = 'sum'
    new_col[X['month'].isin(['sep', 'oct', 'nov'])] = 'aut'
    X['season'] = new_col
    if deleteMonth:
        X.drop('month', axis=1, inplace=True)

def dropConst(X):
    """
    X: pandas DataFrame
    Deletes the columns that have the same value for each input vector
    """
    for col in X.columns:
        if X[col].min() == X[col].max():
            X.drop(col, axis=1, inplace=True)

data = pd.read_csv('data/forestfires.csv') # Load the dataset

# Datasets for regression

X = data.copy()
X = X[X.area > 0] # We only take data where the fire has happened
y = X['area'] # Extract labels from the dataset
X.drop('area', axis=1, inplace=True) # Removing the label column from X
X.drop(['X', 'Y'], axis=1, inplace=True)

X1 = X.copy()
X2 = X.copy()
X3 = X.copy()

# First training set
setDays2(X)
setMonths2(X)
dropConst(X)
scaleData(X)

# Second training set
setWeekend(X1)
X1['hot'] = pd.Series(0, index=X1.index)
X1.loc[X1.month.isin(['may', 'jun', 'jul', 'aug', 'sep']), 'hot'] = 1
X1.drop('month', axis=1, inplace=True)
scaleData(X1)
dropConst(X1)

# Third training set
setSeason(X2)
setCategorical(X2, 'season')
setWeekend(X2)
scaleData(X2)
dropConst(X2)

# Fourth training set
X3.drop(['day', 'month', 'FFMC', 'DMC', 'DC', 'ISI'], axis=1, inplace=True)
scaleData(X3)

# Datasets for classification

C = data.copy()
c = C['area'] # Extract labels from the dataset
c[C.area > 0] = 1
C.drop('area', axis=1, inplace=True) # Removing the label column from C
C.drop(['X', 'Y'], axis=1, inplace=True)

C1 = C.copy()
C2 = C.copy()
C3 = C.copy()

# First training set
setDays2(C)
setMonths2(C)
dropConst(C)
scaleData(C)

# Second training set
setWeekend(C1)
C1['hot'] = pd.Series(0, index=C1.index)
C1.loc[C1.month.isin(['may', 'jun', 'jul', 'aug', 'sep']), 'hot'] = 1
C1.drop('month', axis=1, inplace=True)
scaleData(C1)
dropConst(C1)

# Third training set
setSeason(C2)
setCategorical(C2, 'season')
setWeekend(C2)
scaleData(C2)
dropConst(C2)

# Fourth training set
C3.drop(['day', 'month', 'FFMC', 'DMC', 'DC', 'ISI'], axis=1, inplace=True)
scaleData(C3)

max_stars_repo_name: pietrodll/school-projects

max_issues_repo_name: pietrodll/school-projects

max_forks_repo_name: pietrodll/school-projects


text:
import unittest
import pytest
import gym
import numpy as np
from dacbench.benchmarks import LubyBenchmark, FastDownwardBenchmark, CMAESBenchmark
from dacbench.wrappers import ActionFrequencyWrapper

class TestActionTrackingWrapper(unittest.TestCase):
    def test_init(self):
        bench = LubyBenchmark()
        env = bench.get_environment()
        wrapped = ActionFrequencyWrapper(env)
        self.assertTrue(len(wrapped.overall_actions) == 0)
        self.assertTrue(wrapped.action_interval is None)
        wrapped.instance = [0]
        self.assertTrue(wrapped.instance[0] == 0)

        wrapped2 = ActionFrequencyWrapper(env, 10)
        self.assertTrue(len(wrapped2.overall_actions) == 0)
        self.assertTrue(wrapped2.action_interval == 10)
        self.assertTrue(len(wrapped2.action_intervals) == 0)
        self.assertTrue(len(wrapped2.current_actions) == 0)

    def test_step(self):
        bench = LubyBenchmark()
        env = bench.get_environment()
        wrapped = ActionFrequencyWrapper(env, 10)

        state = wrapped.reset()
        self.assertTrue(len(state) > 1)

        state, reward, done, _ = wrapped.step(1)
        self.assertTrue(len(state) > 1)
        self.assertTrue(reward <= 0)
        self.assertFalse(done)

        self.assertTrue(len(wrapped.overall_actions) == 1)
        self.assertTrue(wrapped.overall_actions[0] == 1)
        self.assertTrue(len(wrapped.current_actions) == 1)
        self.assertTrue(wrapped.current_actions[0] == 1)
        self.assertTrue(len(wrapped.action_intervals) == 0)

    def test_get_actions(self):
        bench = LubyBenchmark()
        env = bench.get_environment()
        wrapped = ActionFrequencyWrapper(env)
        wrapped.reset()
        for i in range(5):
            wrapped.step(i)
        wrapped2 = ActionFrequencyWrapper(env, 2)
        wrapped2.reset()
        for i in range(5):
            wrapped2.step(i)

        overall_actions_only = wrapped.get_actions()
        overall_actions, intervals = wrapped2.get_actions()
        self.assertTrue(np.array_equal(overall_actions, overall_actions_only))
        self.assertTrue(overall_actions_only == [0, 1, 2, 3, 4])

        self.assertTrue(len(intervals) == 3)
        self.assertTrue(len(intervals[0]) == 2)
        self.assertTrue(intervals[0] == [0, 1])
        self.assertTrue(len(intervals[1]) == 2)
        self.assertTrue(intervals[1] == [2, 3])
        self.assertTrue(len(intervals[2]) == 1)
        self.assertTrue(intervals[2] == [4])

    def test_rendering(self):
        bench = FastDownwardBenchmark()
        env = bench.get_environment()
        wrapped = ActionFrequencyWrapper(env, 2)
        wrapped.reset()
        for _ in range(10):
            wrapped.step(1)
        img = wrapped.render_action_tracking()
        self.assertTrue(img.shape[-1] == 3)

        bench = CMAESBenchmark()
        env = bench.get_environment()
        wrapped = ActionFrequencyWrapper(env, 2)
        wrapped.reset()
        wrapped.step(np.ones(10))
        img = wrapped.render_action_tracking()
        self.assertTrue(img.shape[-1] == 3)

        class dict_action_env:
            def __init__(self):
                self.action_space = gym.spaces.Dict(
                    {
                        "one": gym.spaces.Discrete(2),
                        "two": gym.spaces.Box(
                            low=np.array([-1, 1]), high=np.array([1, 5])
                        ),
                    }
                )
                self.observation_space = gym.spaces.Discrete(2)
                self.reward_range = (1, 2)
                self.metadata = {}

            def reset(self):
                return 1

            def step(self, action):
                return 1, 1, 1, 1

        env = dict_action_env()
        wrapped = ActionFrequencyWrapper(env)
        wrapped.reset()
        with pytest.raises(NotImplementedError):
            wrapped.render_action_tracking()

        class tuple_action_env:
            def __init__(self):
                self.action_space = gym.spaces.Tuple(
                    (
                        gym.spaces.Discrete(2),
                        gym.spaces.Box(low=np.array([-1, 1]), high=np.array([1, 5])),
                    )
                )
                self.observation_space = gym.spaces.Discrete(2)
                self.reward_range = (1, 2)
                self.metadata = {}

            def reset(self):
                return 1

            def step(self, action):
                return 1, 1, 1, 1

        env = tuple_action_env()
        wrapped = ActionFrequencyWrapper(env)
        wrapped.reset()
        with pytest.raises(NotImplementedError):
            wrapped.render_action_tracking()

        class multi_discrete_action_env:
            def __init__(self):
                self.action_space = gym.spaces.MultiDiscrete([2, 3])
                self.observation_space = gym.spaces.Discrete(2)
                self.reward_range = (1, 2)
                self.metadata = {}

            def reset(self):
                return 1

            def step(self, action):
                return 1, 1, 1, 1

        env = multi_discrete_action_env()
        wrapped = ActionFrequencyWrapper(env, 5)
        wrapped.reset()
        for _ in range(10):
            wrapped.step([1, 2])
        img = wrapped.render_action_tracking()
        self.assertTrue(img.shape[-1] == 3)

        class multi_binary_action_env:
            def __init__(self):
                self.action_space = gym.spaces.MultiBinary(2)
                self.observation_space = gym.spaces.Discrete(2)
                self.reward_range = (1, 2)
                self.metadata = {}

            def reset(self):
                return 1

            def step(self, action):
                return 1, 1, 1, 1

        env = multi_binary_action_env()
        wrapped = ActionFrequencyWrapper(env)
        wrapped.reset()
        wrapped.step([1, 0])
        img = wrapped.render_action_tracking()
        self.assertTrue(img.shape[-1] == 3)

        class large_action_env:
            def __init__(self):
                self.action_space = gym.spaces.Box(low=np.zeros(15), high=np.ones(15))
                self.observation_space = gym.spaces.Discrete(2)
                self.reward_range = (1, 2)
                self.metadata = {}

            def reset(self):
                return 1

            def step(self, action):
                return 1, 1, 1, 1

        env = large_action_env()
        wrapped = ActionFrequencyWrapper(env)
        wrapped.reset()
        wrapped.step(0.5 * np.ones(15))
        img = wrapped.render_action_tracking()
        self.assertTrue(img.shape[-1] == 3)


text:
"""

Some notes:
    HDI: Highest Density Interval.
    ROPE: Region of Practical Equivalence.

"""
import numpy as np
from matplotlib import pyplot as plt

def ch01_01():
    """

    """
    thetas = np.linspace(0, 1, 1001)
    print(thetas)

    likelihood = lambda r: thetas if r else (1 - thetas)

    def posterior(r, prior):
        lp = likelihood(r) * prior
        return lp / lp.sum()

    p = np.array([1 / len(thetas) for _ in thetas])
    print(p)

    # Bayesian update by click-event(r=1)
    p = posterior(1, p)
    print(p)

    plt.plot(thetas, p)
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.show()

    #
    clicks = 2
    noclicks = 38
    p = np.array([1 / len(thetas) for theta in thetas])
    for _ in range(clicks):
        p = posterior(1, p)
    for _ in range(noclicks):
        p = posterior(0, p)
    print(p)
    plt.plot(thetas, p)
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.show()

def ch01_02():
    """ Binomial distribution
    """
    thetas = np.linspace(0, 1, 1001)
    print(thetas)

    likelihood = lambda a, N: thetas ** a * (1 - thetas) ** (N - a)

    def posterior(a, N, prior):
        lp = likelihood(a, N) * prior
        return lp / lp.sum()

    prior = 1 / len(thetas)
    plt.subplot(2, 1, 1)
    plt.plot(thetas, posterior(2, 40, prior), label='Alice - A')
    plt.plot(thetas, posterior(4, 50, prior), label='Alice - B')
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.xlim(0, 0.2)
    plt.legend()
    plt.subplot(2, 1, 2)
    plt.plot(thetas, posterior(64, 1280, prior), label='Bob - A')
    plt.plot(thetas, posterior(128, 1600, prior), label='Bob - B')
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.xlim(0, 0.2)
    plt.legend()
    plt.tight_layout()
    plt.show()

def ch01_03():
    """
        theta ~ Beta.
        alpha ~ Binomial.
    """
    thetas = np.linspace(0, 1, 1001)
    print(thetas)

    def betaf(alpha, beta):
        numerator = thetas ** (alpha - 1) * (1 - thetas) ** (beta - 1)
        return numerator / numerator.sum()

    def posterior(a, N):
        return betaf(a + 1, N - a + 1)

    plt.subplot(2, 1, 1)
    plt.plot(thetas, posterior(2, 40), label='Alice - A')
    plt.plot(thetas, posterior(4, 50), label='Alice - B')
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.xlim(0, 0.2)
    plt.legend()
    plt.subplot(2, 1, 2)
    plt.plot(thetas, posterior(64, 1280), label='Bob - A')
    plt.plot(thetas, posterior(128, 1600), label='Bob - B')
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.xlim(0, 0.2)
    plt.legend()
    plt.tight_layout()
    plt.show()

def ch01_04():
    """

    """
    def hmv(xs, ps, alpha=0.95):
        """ Highest Mass Value function.
        Parameters:
            xs : Probability variables.
            ps : Probability Mass.
            alpha : threshold.
        Return:

        """
        xps = sorted(zip(xs, ps), key=lambda xp: xp[1], reverse=True)
        xps = np.array(xps)
        xs = xps[:, 0]
        ps = xps[:, 1]
        return np.sort(xs[np.cumsum(ps) <= alpha])

    thetas = np.linspace(0, 1, 1001)

    def posterior(a, N):
        alpha = a + 1
        beta = N - a + 1
        numerator = thetas ** (alpha - 1) * (1 - thetas) ** (beta - 1)
        return numerator / numerator.sum()

    ps = posterior(2, 40)
    hm_thetas = hmv(thetas, ps, alpha=0.95)
    plt.plot(thetas, ps)
    plt.annotate('', xy=(hm_thetas.min(), 0),
                 xytext=(hm_thetas.max(), 0),
                 arrowprops=dict(color='black', shrinkA=0, shrinkB=0,
                                 arrowstyle='<->', linewidth=2))
    plt.annotate('%.3f' % hm_thetas.min(), xy=(hm_thetas.min(), 0),
                 ha='right', va='bottom')
    plt.annotate('%.3f' % hm_thetas.max(), xy=(hm_thetas.max(), 0),
                 ha='left', va='bottom')
    plt.annotate('95% HDI', xy=(hm_thetas.mean(), 0),
                 ha='center', va='bottom')
    hm_region = (hm_thetas.min() < thetas) & (thetas < hm_thetas.max())
    plt.fill_between(thetas[hm_region], ps[hm_region], 0, alpha=0.3)
    plt.xlabel(r'$\theta$')
    plt.ylabel(r'$p(\theta)$')
    plt.xlim(0, 0.3)
    plt.tight_layout()
    plt.show()

    def plot_hdi(ps, label):
        """ """
        hm_thetas = hmv(thetas, ps, 0.95)
        plt.plot(thetas, ps)
        plt.annotate('', xy=(hm_thetas.min(), 0),
                    xytext=(hm_thetas.max(), 0),
                    arrowprops=dict(color='black', shrinkA=0, shrinkB=0,
                                    arrowstyle='<->', linewidth=2))
        plt.annotate('%.3f' % hm_thetas.min(), xy=(hm_thetas.min(), 0),
                    ha='right', va='bottom')
        plt.annotate('%.3f' % hm_thetas.max(), xy=(hm_thetas.max(), 0),
                    ha='left', va='bottom')
        plt.annotate('95% HDI', xy=(hm_thetas.mean(), 0),
                    ha='center', va='bottom')
        hm_region = (hm_thetas.min() < thetas) & (thetas < hm_thetas.max())
        plt.fill_between(thetas[hm_region], ps[hm_region], 0, alpha=0.3)
        plt.xlim(0, 0.3)
        plt.ylabel(label)
        plt.yticks([])

    plt.subplot(4, 1, 1)
    alice_a = posterior(2, 40)
    plot_hdi(alice_a, 'Alice A')
    plt.subplot(4, 1, 2)
    alice_b = posterior(4, 50)
    plot_hdi(alice_b, 'Alice B')
    plt.subplot(4, 1, 3)
    bob_a = posterior(64, 1280)
    plot_hdi(bob_a, 'Bob A')
    plt.subplot(4, 1, 4)
    bob_b = posterior(128, 1600)
    plot_hdi(bob_b, 'Bob B')
    plt.xlabel(r'$\theta$')
    plt.tight_layout()
    plt.show

def ch01_05():
    """
    """
    theta_a = np.random.beta(3, 39, size=100000)
    theta_b = np.random.beta(5, 47, size=100000)
    delta = theta_b - theta_a
    plt.hist(delta, range=(-0.3, 0.3), bins=60)
    plt.xlabel(r'$\delta$')
    plt.ylabel(r'Frequency')
    plt.show()

    print((delta > 0).mean())

max_stars_repo_name: hyoshizawa/playground

max_issues_repo_name: hyoshizawa/playground

max_forks_repo_name: hyoshizawa/playground


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

func_axis = np.arange(-100,100)
u = np.heaviside(func_axis,1)
u_p_4 = np.roll(u,-4) #the end wont be plotted so dont care
u_m_5 = np.roll(u,5)
#starting function
x = func_axis*(u_p_4-u_m_5)
x_graph = x[90:111]
plot_axis = np.arange(-10,11)
plt.figure(1)
plt.stem(plot_axis,x_graph,use_line_collection=True)
plt.savefig('test.png')#just for reference
#1a
g1 = np.roll(x_graph,3)
plt.figure(2)
plt.stem(plot_axis,g1,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g1[n]")
plt.savefig("hw1_1a.png")
#1b
g2 = np.zeros(21,dtype=int)
for i, point in enumerate(g2):
    index = i-10
    g2[i] = x[(2*index-3)+100]
plt.figure(3)
plt.stem(plot_axis,g2,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g2[n]")
plt.savefig("hw1_1b.png")
#1c
plt.figure(4)
g3 = np.flip(x_graph)
g3 = np.roll(g3,1)
plt.stem(plot_axis,g3,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g3[n]")
plt.savefig("hw1_1c.png")
#1d
plt.figure(5)
g4 =np.roll(g3,2)
plt.stem(plot_axis,g4,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g4[n]")
plt.savefig("hw1_1d.png")
#1e
g5 = np.zeros(21, dtype=int)
for i, point in enumerate(g5):
    index  = i-10
    if (int(index/2) == float(index)/2):
        g5[i] = x[int(index/2)+100]
plt.figure(6)
plt.stem(plot_axis,g5,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g5[n]")
plt.savefig("hw1_1e.png")
#1f
delta = np.zeros(21, dtype=int)
delta[10] =1
g6 = x_graph*delta
plt.figure(7)
plt.stem(plot_axis,g6,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("g6[n]")
plt.savefig("hw1_1f.png")

#---------------------------------------------#
#Problem 2A
#---------------------------------------------#
#generate x
x_n = np.zeros(21)
base_x =0.5

for i, value in enumerate(x_n):
    index = i-10
    if(index>=0 and index<=3):
        x_n[i] = np.power(base_x,index)

#generate y
y_n =np.zeros(21)
base_y =0.25
for i, value in enumerate(y_n):
    index = i-10
    if(index>=0 and index<=5):
        y_n[i] = np.power(base_y,index-1)

plt.figure(8)
plt.stem(plot_axis,x_n,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("x[n]")
plt.savefig("hw1_2aX.png")
plt.figure(9)
plt.stem(plot_axis,y_n,use_line_collection=True)
plt.xlabel("n")
plt.ylabel("y[n]")
plt.savefig("hw1_2aY.png")

#---------------------------------------------#
#Problem 2B
#---------------------------------------------#
z_n = x_n*y_n
plt.figure(10)
plt.stem(plot_axis,z_n, use_line_collection=True)
plt.xlabel("n")
plt.ylabel("z[n]")
plt.savefig("hw1_2b.png")