repo_name
stringlengths
7
94
repo_path
stringlengths
4
237
repo_head_hexsha
stringlengths
40
40
content
stringlengths
10
680k
apis
stringlengths
2
680k
smdesai/logica
colab_logica.py
ad099bcd6064e38e9c2bc9a99564832857c0768c
#!/usr/bin/python # # Copyright 2020 Google LLC # # 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. """Library for using Logica in CoLab.""" from .common import color from .common import concertina_lib from .compiler import functors from .compiler import rule_translate from .compiler import universe import IPython from IPython.core.magic import register_cell_magic from IPython.display import display import os import pandas from .parser_py import parse from .common import sqlite3_logica BQ_READY = True # By default. try: from google.cloud import bigquery except: BQ_READY = False print('Could not import google.cloud.bigquery.') try: from google.colab import auth except: BQ_READY = False print('Could not import google.cloud.auth.') try: from google.colab import widgets WIDGETS_IMPORTED = True except: WIDGETS_IMPORTED = False print('Could not import google.colab.widgets.') PROJECT = None # TODO: Should this be renamed to PSQL_ENGINE, PSQL_CONNECTION? DB_ENGINE = None DB_CONNECTION = None USER_AUTHENTICATED = False TABULATED_OUTPUT = True SHOW_FULL_QUERY = True PREAMBLE = None def SetPreamble(preamble): global PREAMBLE PREAMBLE = preamble def SetProject(project): global PROJECT PROJECT = project def SetDbConnection(connection): global DB_CONNECTION DB_CONNECTION = connection def EnsureAuthenticatedUser(): global USER_AUTHENTICATED global PROJECT if USER_AUTHENTICATED: return auth.authenticate_user() if PROJECT is None: print("Please enter project_id to use for BigQuery queries.") PROJECT = input() print("project_id is set to %s" % PROJECT) print("You can change it with logica.colab_logica.SetProject command.") USER_AUTHENTICATED = True def SetTabulatedOutput(tabulated_output): global TABULATED_OUTPUT global SHOW_FULL_QUERY TABULATED_OUTPUT = tabulated_output SHOW_FULL_QUERY = TABULATED_OUTPUT if not WIDGETS_IMPORTED: SetTabulatedOutput(False) def TabBar(*args): """Returns a real TabBar or a mock. Useful for UIs that don't support JS.""" if TABULATED_OUTPUT: return widgets.TabBar(*args) class MockTab: def __init__(self): pass def __enter__(self): pass def __exit__(self, *x): pass class MockTabBar: def __init__(self): pass def output_to(self, x): return MockTab() return MockTabBar() @register_cell_magic def logica(line, cell): Logica(line, cell, run_query=True) def ParseList(line): line = line.strip() if not line: predicates = [] else: predicates = [p.strip() for p in line.split(',')] return predicates def RunSQL(sql, engine, connection=None, is_final=False): if engine == 'bigquery': client = bigquery.Client(project=PROJECT) return client.query(sql).to_dataframe() elif engine == 'psql': if is_final: return pandas.read_sql(sql, connection) else: return connection.execute(sql) elif engine == 'sqlite': statements = parse.SplitRaw(sql, ';') connection.executescript(sql) if is_final: return pandas.read_sql(statements[-1], connection) else: pass return None else: raise Exception('Logica only supports BigQuery, PostgreSQL and SQLite ' 'for now.') class SqliteRunner(object): def __init__(self): self.connection = sqlite3_logica.SqliteConnect() # TODO: Sqlite runner should not be accepting an engine. def __call__(self, sql, engine, is_final): return RunSQL(sql, engine, self.connection, is_final) class PostgresRunner(object): def __init__(self): global DB_CONNECTION global DB_ENGINE if DB_CONNECTION: self.engine = DB_ENGINE self.connection = DB_CONNECTION else: (self.engine, self.connection) = PostgresJumpStart() DB_ENGINE = self.engine DB_CONNECTION = self.connection def __call__(self, sql, engine, is_final): return RunSQL(sql, engine, self.connection, is_final) def ShowError(error_text): print(color.Format('[ {error}Error{end} ] ' + error_text)) def Logica(line, cell, run_query): """Running Logica predicates and storing results.""" predicates = ParseList(line) if not predicates: ShowError('No predicates to run.') return try: program = ';\n'.join(s for s in [PREAMBLE, cell] if s) parsed_rules = parse.ParseFile(program)['rule'] except parse.ParsingException as e: e.ShowMessage() return try: program = universe.LogicaProgram(parsed_rules) except functors.FunctorError as e: e.ShowMessage() return engine = program.annotations.Engine() if engine == 'bigquery' and not BQ_READY: ShowError( 'BigQuery client and/or authentification is not installed. \n' 'It is the easiest to run BigQuery requests from Google CoLab:\n' ' https://colab.research.google.com/.\n' 'Note that running Logica on SQLite requires no installation.\n' 'This could be a good fit for working with small data or learning Logica.\n' 'Use {warning}@Engine("sqlite");{end} annotation in your program to use SQLite.') return bar = TabBar(predicates + ['(Log)']) logs_idx = len(predicates) executions = [] sub_bars = [] ip = IPython.get_ipython() for idx, predicate in enumerate(predicates): with bar.output_to(logs_idx): try: sql = program.FormattedPredicateSql(predicate) executions.append(program.execution) ip.push({predicate + '_sql': sql}) except rule_translate.RuleCompileException as e: print('Encountered error when compiling %s.' % predicate) e.ShowMessage() return # Publish output to Colab cell. with bar.output_to(idx): sub_bar = TabBar(['SQL', 'Result']) sub_bars.append(sub_bar) with sub_bar.output_to(0): if SHOW_FULL_QUERY: print( color.Format( 'The following query is stored at {warning}%s{end} ' 'variable.' % ( predicate + '_sql'))) print(sql) else: print('Query is stored at %s variable.' % color.Warn(predicate + '_sql')) with bar.output_to(logs_idx): if engine == 'sqlite': sql_runner = SqliteRunner() elif engine == 'psql': sql_runner = PostgresRunner() elif engine == 'bigquery': EnsureAuthenticatedUser() sql_runner = RunSQL else: raise Exception('Logica only supports BigQuery, PostgreSQL and SQLite ' 'for now.') result_map = concertina_lib.ExecuteLogicaProgram( executions, sql_runner=sql_runner, sql_engine=engine) for idx, predicate in enumerate(predicates): t = result_map[predicate] ip.push({predicate: t}) with bar.output_to(idx): with sub_bars[idx].output_to(1): if run_query: print( color.Format( 'The following table is stored at {warning}%s{end} ' 'variable.' % predicate)) display(t) else: print('The query was not run.') print(' ') # To activate the tabbar. def PostgresJumpStart(): # Install postgresql server. print("Installing and configuring an empty PostgreSQL database.") result = 0 result += os.system('sudo apt-get -y -qq update') result += os.system('sudo apt-get -y -qq install postgresql') result += os.system('sudo service postgresql start') # Ignoring user creation error, as they may already exist. result += 0 * os.system( 'sudo -u postgres psql -c "CREATE USER logica WITH SUPERUSER"') result += os.system( 'sudo -u postgres psql -c "ALTER USER logica PASSWORD \'logica\';"') result += os.system( 'sudo -u postgres psql -U postgres -c \'CREATE DATABASE logica;\'') if result != 0: print("""Installation failed. Please try the following manually: # Install Logica. !pip install logica # Install postgresql server. !sudo apt-get -y -qq update !sudo apt-get -y -qq install postgresql !sudo service postgresql start # Prepare database for Logica. !sudo -u postgres psql -c "CREATE USER logica WITH SUPERUSER" !sudo -u postgres psql -c "ALTER USER logica PASSWORD 'logica';" !sudo -u postgres psql -U postgres -c 'CREATE DATABASE logica;' # Connect to the database. from logica import colab_logica from sqlalchemy import create_engine import pandas engine = create_engine('postgresql+psycopg2://logica:logica@127.0.0.1', pool_recycle=3600); connection = engine.connect(); colab_logica.SetDbConnection(connection)""") return print('Installation succeeded. Connecting...') # Connect to the database. from logica import colab_logica from sqlalchemy import create_engine import pandas engine = create_engine('postgresql+psycopg2://logica:logica@127.0.0.1', pool_recycle=3600) connection = engine.connect() print('Connected.') return engine, connection
[((1956, 1980), 'google.colab.auth.authenticate_user', 'auth.authenticate_user', ([], {}), '()\n', (1978, 1980), False, 'from google.colab import auth\n'), ((5747, 5768), 'IPython.get_ipython', 'IPython.get_ipython', ([], {}), '()\n', (5766, 5768), False, 'import IPython\n'), ((7847, 7886), 'os.system', 'os.system', (['"""sudo apt-get -y -qq update"""'], {}), "('sudo apt-get -y -qq update')\n", (7856, 7886), False, 'import os\n'), ((7899, 7950), 'os.system', 'os.system', (['"""sudo apt-get -y -qq install postgresql"""'], {}), "('sudo apt-get -y -qq install postgresql')\n", (7908, 7950), False, 'import os\n'), ((7963, 8005), 'os.system', 'os.system', (['"""sudo service postgresql start"""'], {}), "('sudo service postgresql start')\n", (7972, 8005), False, 'import os\n'), ((8174, 8252), 'os.system', 'os.system', (['"""sudo -u postgres psql -c "ALTER USER logica PASSWORD \'logica\';\\""""'], {}), '(\'sudo -u postgres psql -c "ALTER USER logica PASSWORD \\\'logica\\\';"\')\n', (8183, 8252), False, 'import os\n'), ((8270, 8345), 'os.system', 'os.system', (['"""sudo -u postgres psql -U postgres -c \'CREATE DATABASE logica;\'"""'], {}), '("sudo -u postgres psql -U postgres -c \'CREATE DATABASE logica;\'")\n', (8279, 8345), False, 'import os\n'), ((9298, 9384), 'sqlalchemy.create_engine', 'create_engine', (['"""postgresql+psycopg2://logica:logica@127.0.0.1"""'], {'pool_recycle': '(3600)'}), "('postgresql+psycopg2://logica:logica@127.0.0.1', pool_recycle\n =3600)\n", (9311, 9384), False, 'from sqlalchemy import create_engine\n'), ((2598, 2619), 'google.colab.widgets.TabBar', 'widgets.TabBar', (['*args'], {}), '(*args)\n', (2612, 2619), False, 'from google.colab import widgets\n'), ((3250, 3282), 'google.cloud.bigquery.Client', 'bigquery.Client', ([], {'project': 'PROJECT'}), '(project=PROJECT)\n', (3265, 3282), False, 'from google.cloud import bigquery\n'), ((8083, 8156), 'os.system', 'os.system', (['"""sudo -u postgres psql -c "CREATE USER logica WITH SUPERUSER\\""""'], {}), '(\'sudo -u postgres psql -c "CREATE USER logica WITH SUPERUSER"\')\n', (8092, 8156), False, 'import os\n'), ((3382, 3414), 'pandas.read_sql', 'pandas.read_sql', (['sql', 'connection'], {}), '(sql, connection)\n', (3397, 3414), False, 'import pandas\n'), ((3595, 3638), 'pandas.read_sql', 'pandas.read_sql', (['statements[-1]', 'connection'], {}), '(statements[-1], connection)\n', (3610, 3638), False, 'import pandas\n'), ((7585, 7595), 'IPython.display.display', 'display', (['t'], {}), '(t)\n', (7592, 7595), False, 'from IPython.display import display\n')]
toddrme2178/pyccel
pyccel/ast/basic.py
deec37503ab0c5d0bcca1a035f7909f7ce8ef653
from sympy.core.basic import Basic as sp_Basic class Basic(sp_Basic): """Basic class for Pyccel AST.""" _fst = None def set_fst(self, fst): """Sets the redbaron fst.""" self._fst = fst @property def fst(self): return self._fst
[]
Clusks/alibi-detect
alibi_detect/utils/tests/test_discretize.py
b39406a6cf88f315f401562d4fea93a42aa6dcc1
from itertools import product import numpy as np import pytest from alibi_detect.utils.discretizer import Discretizer x = np.random.rand(10, 4) n_features = x.shape[1] feature_names = [str(_) for _ in range(n_features)] categorical_features = [[], [1, 3]] percentiles = [list(np.arange(25, 100, 25)), list(np.arange(10, 100, 10))] tests = list(product(categorical_features, percentiles)) n_tests = len(tests) @pytest.fixture def cats_and_percentiles(request): cat, perc = tests[request.param] return cat, perc @pytest.mark.parametrize('cats_and_percentiles', list(range(n_tests)), indirect=True) def test_discretizer(cats_and_percentiles): cat, perc = cats_and_percentiles disc = Discretizer(x, cat, feature_names, perc) to_disc = list(disc.names.keys()) assert len(to_disc) == (x.shape[1] - len(cat)) x_disc = disc.discretize(x) for k, v in disc.names.items(): assert len(v) <= len(perc) + 1 assert callable(disc.lambdas[k]) assert (x_disc[:, k].min() == 0).all() assert (x_disc[:, k].max() == len(perc)).all() for i in range(x.shape[1]): if i not in to_disc: assert (x_disc[:, i] == x[:, i]).all()
[((123, 144), 'numpy.random.rand', 'np.random.rand', (['(10)', '(4)'], {}), '(10, 4)\n', (137, 144), True, 'import numpy as np\n'), ((346, 388), 'itertools.product', 'product', (['categorical_features', 'percentiles'], {}), '(categorical_features, percentiles)\n', (353, 388), False, 'from itertools import product\n'), ((702, 742), 'alibi_detect.utils.discretizer.Discretizer', 'Discretizer', (['x', 'cat', 'feature_names', 'perc'], {}), '(x, cat, feature_names, perc)\n', (713, 742), False, 'from alibi_detect.utils.discretizer import Discretizer\n'), ((278, 300), 'numpy.arange', 'np.arange', (['(25)', '(100)', '(25)'], {}), '(25, 100, 25)\n', (287, 300), True, 'import numpy as np\n'), ((308, 330), 'numpy.arange', 'np.arange', (['(10)', '(100)', '(10)'], {}), '(10, 100, 10)\n', (317, 330), True, 'import numpy as np\n')]
AlloSphere-Research-Group/tinc-python
tinc/tests/parameter_space_test.py
4c3390df9911a391833244de1eb1d33a2e19d330
# -*- coding: utf-8 -*- """ Created on Mon Jun 14 11:49:43 2021 @author: Andres """ import sys,time import unittest from tinc import * class ParameterSpaceTest(unittest.TestCase): def test_parameter(self): p1 = Parameter("param1") p2 = Parameter("param2") ps = ParameterSpace("ps") ps.register_parameters([p1, p2]) def test_process(self): p1 = Parameter("param1") p1.values = [0, 1,2,3,4] p2 = Parameter("param2") p2.values = [-0.3,-0.2, -0.1, 0] ps = ParameterSpace("ps") ps.register_parameters([p1, p2]) def func(param1, param2): return param1 * param2 result = ps.run_process(func) self.assertAlmostEqual(result, p1.value * p2.value) p1.value = 3 p2.value = -0.1 result = ps.run_process(func) self.assertAlmostEqual(result, p1.value * p2.value) p1.value = 3 p2.value = -0.1 def test_sweep_cache(self): p1 = Parameter("param1") p1.values = [0, 1,2,3,4] p2 = Parameter("param2") p2.values = [-0.3,-0.2, -0.1, 0] ps = ParameterSpace("ps") ps.register_parameters([p1, p2]) ps.enable_cache("ps_test") def func(param1, param2): return param1 * param2 ps.sweep(func) def test_data_directories(self): dim1 = Parameter("dim1") dim1.values = [0.1,0.2,0.3,0.4, 0.5] dim2 = Parameter("dim2") dim2.set_space_representation_type(parameter_space_representation_types.INDEX) dim2.values = [0.1,0.2,0.3,0.4, 0.5] dim3 = Parameter("dim3") dim3.set_space_representation_type(parameter_space_representation_types.ID) dim2.values = [0.1,0.2,0.3,0.4, 0.5] ps = ParameterSpace("ps") ps.register_parameters([dim1, dim2, dim3]) ps.set_current_path_template("file_%%dim1%%_%%dim2:INDEX%%") dim1.value=0.2 dim2.value=0.2 self.assertEqual(ps.get_current_relative_path(), 'file_0.2_1') # TODO ML complete tests see C++ tests for parameter space def test_common_id(self): dim1 = Parameter("dim1") dim1.values = [0.1, 0.1, 0.2, 0.2, 0.3, 0.3] dim1.ids = ["0.1_1" ,"0.1_2","0.2_1" ,"0.2_2", "0.3_1" ,"0.3_2"] dim2 = Parameter("dim2") dim2.set_space_representation_type(parameter_space_representation_types.INDEX) dim2.values = [1,1,1,2,2,2] dim2.ids = ["0.1_1", "0.2_1", "0.3_1", "0.1_2", "0.2_2", "0.3_2"] ps = ParameterSpace("ps") ps.register_parameters([dim1, dim2]) dim1.value = 0.1 dim2.value = 1 self.assertEqual(ps.get_common_id([dim1, dim2]), "0.1_1") dim1.value = 0.2 dim2.value = 1 self.assertEqual(ps.get_common_id([dim1, dim2]), "0.2_1") dim1.value = 0.1 dim2.value = 2 self.assertEqual(ps.get_common_id([dim1, dim2]), "0.1_2") dim1.value = 0.2 dim2.value = 2 self.assertEqual(ps.get_common_id([dim1, dim2]), "0.2_2") dim1.value = 0.3 dim2.value = 2 self.assertEqual(ps.get_common_id([dim1, dim2]), "0.3_2") if __name__ == '__main__': unittest.main()
[((3345, 3360), 'unittest.main', 'unittest.main', ([], {}), '()\n', (3358, 3360), False, 'import unittest\n')]
CORE-Robotics-Lab/Interpretable_DDTS_AISTATS2020
interpretable_ddts/runfiles/gym_runner.py
a7fde4d2a7d70477b2e6c96b140f8c6587f78791
# Created by Andrew Silva on 8/28/19 import gym import numpy as np import torch from interpretable_ddts.agents.ddt_agent import DDTAgent from interpretable_ddts.agents.mlp_agent import MLPAgent from interpretable_ddts.opt_helpers.replay_buffer import discount_reward import torch.multiprocessing as mp import argparse import copy import random def run_episode(q, agent_in, ENV_NAME, seed=0): agent = agent_in.duplicate() if ENV_NAME == 'lunar': env = gym.make('LunarLander-v2') elif ENV_NAME == 'cart': env = gym.make('CartPole-v1') else: raise Exception('No valid environment selected') done = False torch.manual_seed(seed) env.seed(seed) np.random.seed(seed) env.action_space.seed(seed) random.seed(seed) state = env.reset() # Reset environment and record the starting state while not done: action = agent.get_action(state) # Step through environment using chosen action state, reward, done, _ = env.step(action) # env.render() # Save reward agent.save_reward(reward) if done: break reward_sum = np.sum(agent.replay_buffer.rewards_list) rewards_list, advantage_list, deeper_advantage_list = discount_reward(agent.replay_buffer.rewards_list, agent.replay_buffer.value_list, agent.replay_buffer.deeper_value_list) agent.replay_buffer.rewards_list = rewards_list agent.replay_buffer.advantage_list = advantage_list agent.replay_buffer.deeper_advantage_list = deeper_advantage_list to_return = [reward_sum, copy.deepcopy(agent.replay_buffer.__getstate__())] if q is not None: try: q.put(to_return) except RuntimeError as e: print(e) return to_return return to_return def main(episodes, agent, ENV_NAME): running_reward_array = [] for episode in range(episodes): reward = 0 returned_object = run_episode(None, agent_in=agent, ENV_NAME=ENV_NAME) reward += returned_object[0] running_reward_array.append(returned_object[0]) agent.replay_buffer.extend(returned_object[1]) if reward >= 499: agent.save('../models/'+str(episode)+'th') agent.end_episode(reward) running_reward = sum(running_reward_array[-100:]) / float(min(100.0, len(running_reward_array))) if episode % 50 == 0: print(f'Episode {episode} Last Reward: {reward} Average Reward: {running_reward}') if episode % 500 == 0: agent.save('../models/'+str(episode)+'th') return running_reward_array if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-a", "--agent_type", help="architecture of agent to run", type=str, default='ddt') parser.add_argument("-e", "--episodes", help="how many episodes", type=int, default=2000) parser.add_argument("-l", "--num_leaves", help="number of leaves for DDT/DRL ", type=int, default=8) parser.add_argument("-n", "--num_hidden", help="number of hidden layers for MLP ", type=int, default=0) parser.add_argument("-env", "--env_type", help="environment to run on", type=str, default='cart') parser.add_argument("-gpu", help="run on GPU?", action='store_true') args = parser.parse_args() AGENT_TYPE = args.agent_type # 'ddt', 'mlp' NUM_EPS = args.episodes # num episodes Default 1000 ENV_TYPE = args.env_type # 'cart' or 'lunar' Default 'cart' USE_GPU = args.gpu # Applies for 'prolo' only. use gpu? Default false if ENV_TYPE == 'lunar': init_env = gym.make('LunarLander-v2') dim_in = init_env.observation_space.shape[0] dim_out = init_env.action_space.n elif ENV_TYPE == 'cart': init_env = gym.make('CartPole-v1') dim_in = init_env.observation_space.shape[0] dim_out = init_env.action_space.n else: raise Exception('No valid environment selected') print(f"Agent {AGENT_TYPE} on {ENV_TYPE} ") # mp.set_start_method('spawn') mp.set_sharing_strategy('file_system') for i in range(5): bot_name = AGENT_TYPE + ENV_TYPE if USE_GPU: bot_name += 'GPU' if AGENT_TYPE == 'ddt': policy_agent = DDTAgent(bot_name=bot_name, input_dim=dim_in, output_dim=dim_out, rule_list=False, num_rules=args.num_leaves) elif AGENT_TYPE == 'mlp': policy_agent = MLPAgent(input_dim=dim_in, bot_name=bot_name, output_dim=dim_out, num_hidden=args.num_hidden) else: raise Exception('No valid network selected') reward_array = main(NUM_EPS, policy_agent, ENV_TYPE)
[((651, 674), 'torch.manual_seed', 'torch.manual_seed', (['seed'], {}), '(seed)\n', (668, 674), False, 'import torch\n'), ((698, 718), 'numpy.random.seed', 'np.random.seed', (['seed'], {}), '(seed)\n', (712, 718), True, 'import numpy as np\n'), ((755, 772), 'random.seed', 'random.seed', (['seed'], {}), '(seed)\n', (766, 772), False, 'import random\n'), ((1146, 1186), 'numpy.sum', 'np.sum', (['agent.replay_buffer.rewards_list'], {}), '(agent.replay_buffer.rewards_list)\n', (1152, 1186), True, 'import numpy as np\n'), ((1245, 1370), 'interpretable_ddts.opt_helpers.replay_buffer.discount_reward', 'discount_reward', (['agent.replay_buffer.rewards_list', 'agent.replay_buffer.value_list', 'agent.replay_buffer.deeper_value_list'], {}), '(agent.replay_buffer.rewards_list, agent.replay_buffer.\n value_list, agent.replay_buffer.deeper_value_list)\n', (1260, 1370), False, 'from interpretable_ddts.opt_helpers.replay_buffer import discount_reward\n'), ((2802, 2827), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (2825, 2827), False, 'import argparse\n'), ((4187, 4225), 'torch.multiprocessing.set_sharing_strategy', 'mp.set_sharing_strategy', (['"""file_system"""'], {}), "('file_system')\n", (4210, 4225), True, 'import torch.multiprocessing as mp\n'), ((469, 495), 'gym.make', 'gym.make', (['"""LunarLander-v2"""'], {}), "('LunarLander-v2')\n", (477, 495), False, 'import gym\n'), ((3743, 3769), 'gym.make', 'gym.make', (['"""LunarLander-v2"""'], {}), "('LunarLander-v2')\n", (3751, 3769), False, 'import gym\n'), ((539, 562), 'gym.make', 'gym.make', (['"""CartPole-v1"""'], {}), "('CartPole-v1')\n", (547, 562), False, 'import gym\n'), ((3913, 3936), 'gym.make', 'gym.make', (['"""CartPole-v1"""'], {}), "('CartPole-v1')\n", (3921, 3936), False, 'import gym\n'), ((4399, 4513), 'interpretable_ddts.agents.ddt_agent.DDTAgent', 'DDTAgent', ([], {'bot_name': 'bot_name', 'input_dim': 'dim_in', 'output_dim': 'dim_out', 'rule_list': '(False)', 'num_rules': 'args.num_leaves'}), '(bot_name=bot_name, input_dim=dim_in, output_dim=dim_out, rule_list\n =False, num_rules=args.num_leaves)\n', (4407, 4513), False, 'from interpretable_ddts.agents.ddt_agent import DDTAgent\n'), ((4714, 4811), 'interpretable_ddts.agents.mlp_agent.MLPAgent', 'MLPAgent', ([], {'input_dim': 'dim_in', 'bot_name': 'bot_name', 'output_dim': 'dim_out', 'num_hidden': 'args.num_hidden'}), '(input_dim=dim_in, bot_name=bot_name, output_dim=dim_out,\n num_hidden=args.num_hidden)\n', (4722, 4811), False, 'from interpretable_ddts.agents.mlp_agent import MLPAgent\n')]
andrewn488/OMSBA-5061
Week 08/tw10_words_by_prefix.py
8e57fff45d8965b0423a6fe338bd74cedfe94ea0
""" TW10: Words by Prefix Team: Tam Tamura, Andrew Nalundasan For: OMSBA 2061, Seattle University Date: 11/3/2020 """ def wordByPrefix(prefix_length, word): my_dict = {} for key in word: for letter in word: prefix_key = letter[:prefix_length] letter = word[:prefix_length] return prefix_key return letter question_2 = ['able', 'ability', 'apple', 'tryst', 'trial', 'tremendous', 'tree'] my_list = [] for elem in question_2: prefix = elem[:2] my_list.append(prefix) print(my_list) def question_3(prefix_length, word): my_list = [] for key in word: prefix = key[:prefix_length] my_list.append(prefix) return my_list def wordByPrefix(prefix_length, word): my_list = [] #count = 0 for key in word: prefix = key[:prefix_length] my_list.append(prefix) count = {} for letter in my_list: if letter.isalpha(): if letter not in count: count[letter] = 0 count[letter] += 1 return count def wordByPrefix(prefix_length, word): my_list = [] #count = 0 for key in word: prefix = key[:prefix_length] my_list.append(prefix) count = {} for letter in my_list: if letter.isalpha(): if letter not in count: letter[count] = [] count.update(letter) return count
[]
openforcefield/bespoke-f
openff/bespokefit/executor/services/qcgenerator/cache.py
27b072bd09610dc8209429118d739e1f453edd61
import hashlib from typing import TypeVar, Union import redis from openff.toolkit.topology import Molecule from openff.bespokefit.executor.services.qcgenerator import worker from openff.bespokefit.schema.tasks import HessianTask, OptimizationTask, Torsion1DTask from openff.bespokefit.utilities.molecule import canonical_order_atoms _T = TypeVar("_T", HessianTask, OptimizationTask, Torsion1DTask) def _canonicalize_task(task: _T) -> _T: task = task.copy(deep=True) # Ensure the SMILES has a canonical ordering to help ensure cache hits. canonical_molecule = canonical_order_atoms( Molecule.from_smiles(task.smiles, allow_undefined_stereo=True) ) if isinstance(task, Torsion1DTask): map_to_atom_index = { j: i for i, j in canonical_molecule.properties["atom_map"].items() } central_atom_indices = sorted( map_to_atom_index[task.central_bond[i]] for i in (0, 1) ) canonical_molecule.properties["atom_map"] = { atom_index: (i + 1) for i, atom_index in enumerate(central_atom_indices) } canonical_smiles = canonical_molecule.to_smiles( isomeric=True, explicit_hydrogens=True, mapped=True ) task.central_bond = (1, 2) else: canonical_smiles = canonical_molecule.to_smiles( isomeric=True, explicit_hydrogens=True, mapped=False ) task.smiles = canonical_smiles return task def cached_compute_task( task: Union[HessianTask, OptimizationTask, Torsion1DTask], redis_connection: redis.Redis, ) -> str: """Checks to see if a QC task has already been executed and if not send it to a worker. """ if isinstance(task, Torsion1DTask): compute = worker.compute_torsion_drive elif isinstance(task, OptimizationTask): compute = worker.compute_optimization elif isinstance(task, HessianTask): compute = worker.compute_hessian else: raise NotImplementedError() # Canonicalize the task to improve the cache hit rate. task = _canonicalize_task(task) task_hash = hashlib.sha512(task.json().encode()).hexdigest() task_id = redis_connection.hget("qcgenerator:task-ids", task_hash) if task_id is not None: return task_id.decode() task_id = compute.delay(task_json=task.json()).id redis_connection.hset("qcgenerator:types", task_id, task.type) # Make sure to only set the hash after the type is set in case the connection # goes down before this information is entered and subsequently discarded. redis_connection.hset("qcgenerator:task-ids", task_hash, task_id) return task_id
[((341, 400), 'typing.TypeVar', 'TypeVar', (['"""_T"""', 'HessianTask', 'OptimizationTask', 'Torsion1DTask'], {}), "('_T', HessianTask, OptimizationTask, Torsion1DTask)\n", (348, 400), False, 'from typing import TypeVar, Union\n'), ((609, 671), 'openff.toolkit.topology.Molecule.from_smiles', 'Molecule.from_smiles', (['task.smiles'], {'allow_undefined_stereo': '(True)'}), '(task.smiles, allow_undefined_stereo=True)\n', (629, 671), False, 'from openff.toolkit.topology import Molecule\n')]
jrzeszutek/cloudify-training-labs
advanced-workflows/task-graphs-lab/exercise/plugins/lab/plugin/workflows.py
5477750d269cb703ce47e35a1c13749fc88f3f6f
'''Copyright Gigaspaces, 2017, All Rights Reserved''' from cloudify.plugins import lifecycle OP_START = 'hacker.interfaces.lifecycle.start' OP_STOP = 'hacker.interfaces.lifecycle.stop' OP_SS_C = 'hacker.interfaces.lifecycle.create_snapshots' OP_SS_D = 'hacker.interfaces.lifecycle.delete_snapshots' REQUIRED_OPS = set([OP_START, OP_SS_C, OP_SS_D, OP_STOP]) def build_instance_sequence(instance, operation, state_start=None, state_end=None): ''' Builds sequenced subgraph tasks for an instance .. note:: The sequence will not be built if the instance provided does not have a node with an operation defined in the operation parameter. :param `CloudifyWorkflowNodeInstance` instance: Node instance to execute tasks against :param str operation: Node (lifecycle) operation to execute :param str state_start: Verb to describe operation start :param str state_stop: Verb to describe operation finish ''' tasks = list() # Only build the sequence if the node operation exists if operation not in instance.node.operations: return tasks # Add task starting state if state_start: tasks.append(instance.send_event('%s host' % state_start)) tasks.append(instance.set_state(state_start.lower())) # Add task operation tasks.append(instance.execute_operation(operation)) # Add task ended state if state_end: tasks.append(instance.send_event('%s host' % state_end)) tasks.append(instance.set_state(state_end.lower())) return tasks def build_instance_subgraph(instance, graph): ''' Builds a subgraph for an instance :param `CloudifyWorkflowNodeInstance` instance: Node instance to execute tasks against :param `TaskDependencyGraph` graph: Task graph to create sequences from ''' # Init a "stop instance" subgraph sg_stop = graph.subgraph('stop_subgraph') seq_stop = sg_stop.sequence() seq_stop.add(*build_instance_sequence( instance, OP_STOP, 'Stopping', 'Stopped')) # Init a "recreate snapshots" subgraph sg_snap = graph.subgraph('snapshot_subgraph') seq_snap = sg_snap.sequence() if OP_SS_D in instance.node.operations: seq_snap.add(*build_instance_sequence(instance, OP_SS_D)) if OP_SS_C in instance.node.operations: seq_snap.add(*build_instance_sequence(instance, OP_SS_C)) # Init a "start instance" subgraph sg_start = graph.subgraph('stop_subgraph') seq_start = sg_start.sequence() seq_start.add(*build_instance_sequence( instance, OP_START, 'Starting', 'Started')) # Create subgraph dependencies graph.add_dependency(sg_snap, sg_stop) graph.add_dependency(sg_start, sg_snap) def refresh_snapshots(ctx, **_): ''' Executes a complex, graph-based set of lifecycle events to stop all host (compute) instances, delete all existing instance snapshots, take new snapshots of all attached volumes, and start the instances back up when complete. ''' graph = ctx.graph_mode() # Find all compute hosts and build a sequence graph for node in ctx.nodes: if not REQUIRED_OPS.issubset(node.operations): ctx.logger.warn( 'Skipping refresh_snapshots workflow for node "%s" because ' 'it does not have all required operations defined' % node.id) continue # Iterate over each node instance for instance in node.instances: if not lifecycle.is_host_node(instance): ctx.logger.warn( 'Skipping refresh_snapshots workflow for node instance ' '"%s" because it is not a compute host' % instance.id) continue build_instance_subgraph(instance, graph) # Execute the sequences return graph.execute()
[((3599, 3631), 'cloudify.plugins.lifecycle.is_host_node', 'lifecycle.is_host_node', (['instance'], {}), '(instance)\n', (3621, 3631), False, 'from cloudify.plugins import lifecycle\n')]
CryptoNyxz/Miscellaneous-Tools
File Transfer/Flyter/flyter.py
797ea04d7c369469ab3d2a1ae2838c4a7b7b9c02
""" Flyter Tool for transferring files on the same network using raw sockets. Doesn't use encryption. """ __version__ = (0, 0, 0) __author__ = "CryptoNyxz" __license__ = """ MIT License Copyright (c) 2021 Jaymund Cyrus F. Floranza Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from argparse import ArgumentParser from base64 import b64encode from datetime import timedelta from math import log from os import altsep, sep, \ mkdir, stat, unlink from os.path import dirname, exists, join from random import randint from secrets import token_bytes from shutil import get_terminal_size from socket import \ socket, error, timeout, \ ntohs, ntohl, htons, htonl, \ gethostname, \ AF_INET, SOCK_STREAM from threading import Thread from time import time from warnings import warn from sys import argv, exit, version_info if version_info < (3, 6): warn('[!] Some features are not be compatible with the version of your ' 'python interpreter') FROMTERMINAL = False # Utility Functions def random_port(host): """Return a random available TCP port.""" while True: port = randint(10_000, 65536) with socket(AF_INET, SOCK_STREAM) as sock: try: sock.bind((host, port)) except error: continue else: return port def printerror(errormsg): """Print an error message.""" global FROMTERMINAL if FROMTERMINAL: print(f'\n[x] {errormsg}') exit(-1) exit(-1) exit(-1) exit(-1) else: warn(errormsg) def printalert(alert): """Print an alert message.""" global FROMTERMINAL print(f'[!] {alert}') def int_to_bytes_s(integer): """Convert 16 - bit integer to bytes for packing.""" res = ntohs(integer) res = hex(res)[2:] res = '0'*(len(res) % 2) + res return bytes.fromhex(res) def bytes_to_int_s(byteseq): """Convert byte sequence to 16 - but integer for unpacking.""" res = bytes.hex(byteseq) res = int(res, 16) return htons(res) def int_to_bytes_l(integer): """Convert 32 - but integer to bytes for packing.""" res = ntohl(integer) res = hex(res)[2:] res = '0'*(len(res) % 2) + res return bytes.fromhex(res) def bytes_to_int_l(byteseq): """Convert byte sequence to 32 - but integer for unpacking.""" res = bytes.hex(byteseq) res = int(res, 16) return htonl(res) def pack_str(string): """Pack a string into a byte sequence.""" return string.encode() def unpack_str(byteseq): """Unpack a byte sequence into a string.""" return byteseq.decode() # Utility Classes class ProgressBar: """ For displaying progress bars. Parameters ---------- max_value : int, float The upper limit of the progress bar. length : :obj:`int`, optional The length of the progress bar. """ @staticmethod def byte_rescale(data, precision=1): scale = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] p = int(log(data, 2)/10) if data else 0 r_bytes = round(data/pow(2, 10*p), precision) return f"{r_bytes}{scale[p]}" def __init__(self, max_value, length=50): self.max_value = max_value self.current_val = 0 self.length = length self.rate = None self.start_time = None self.start_value = None self.stopped = False @property def done(self): """Return if already finished.""" return self.current_val >= self.max_value or self.stopped def start(self): """Start the progress bar.""" self.stopped = False self.start_time = time() self.start_value = self.current_val def stop(self): """Stop the progress bar.""" self.stopped = True def add_progress(self, value): """ Count new progress. Parameter --------- value : int, float Added progress value. """ if self.stopped: return self.current_val += value def display(self): """Display the current progress.""" if self.stopped: return d_value = self.current_val - self.start_value d_max_value = self.max_value - self.start_value d_time = time() - self.start_time per = d_value/d_max_value prog = int(self.length*per) extra = self.length*round(per) > prog prog_bar = '█'*prog + '▌'*extra spaces = ' '*(self.length - (prog + extra)) rate = d_value/d_time if d_time else float('inf') eta_s = round((d_max_value - d_value)/rate) if rate else \ None eta = timedelta(seconds=eta_s) if eta_s is not None else '?' clear_line = " "*(get_terminal_size().columns - 1) print(f"{clear_line}\r" "Progress: " f"|{prog_bar}{spaces}| " f"{100*per:.1f}% " f"({ProgressBar.byte_rescale(d_value)}) " f"[{ProgressBar.byte_rescale(rate)}/s] " f"ETA: {eta}", end="\r") # Flyter Classes class FlyterSender: """ Handles Flyter file sending processes. Note: Sends to FlyterReceiver instances. Parameterss ---------- recver_ip : str The IP address of the receiver. main_port : int The main TCP port of the receiver. """ DEFAULT_PACKET_SIZE = 1024 def __init__(self, recver_ip, main_port): self.recver_ip = recver_ip self.main_port = main_port self.token = token_bytes(6) self._recver_hostname = None self._recver_token = None self._transfer_type = None self._worker_ports = None self._packet_size = FlyterSender.DEFAULT_PACKET_SIZE self._sending_file = False self._workers_active = 0 self._progress_bar = None try: self.socket = socket(AF_INET, SOCK_STREAM) self.socket.settimeout(60) except: printerror('Error initializing sockets') self.param_set = False def __del__(self): if isinstance(self.socket, socket): self.socket.close() def _send_s(self, filepath, file_size): """ Send a file with a single worker. Parameters ---------- filepath : str The filepath to the file to be sent. """ if not self.param_set: return printerror("Not yet set with receiver's parameters") if not exists(filepath): return printerror("File doesn't exist") self._sending_file = True try: fs = file_size with open(filepath, 'br') as f: while self._sending_file and fs: packet = f.read(self._packet_size) if not packet: break self.socket.send(packet) assert self.socket.recv(1) == b'\x06' # ACK self._progress_bar.add_progress(len(packet)) fs -= len(packet) except AssertionError: self._progress_bar.stop() return printerror("Receiver rejected packet") except FileNotFoundError: self._progress_bar.stop() return printerror("Couldn't access file") except PermissionError: self._progress_bar.stop() return printerror("Couldn't access file due to permission error") except timeout: self._progress_bar.stop() return printerror("Operation timed out") except: self._progress_bar.stop() return printerror(f"Error while sending file") else: self._sending_file = False return True def _send_m(self, filepath, file_sizes): """ Send a file with multiple workers. Speeds up transmission rate by using multiple workers. Parameters ---------- filepath : str The filepath to the file to be sent. file_sizes : list(int) The sizes of the split-up file to be sent. """ if not self.param_set: return printerror("Not yet set with receiver's parameters") if not exists(filepath): printerror("File doesn't exist") def threadfunc(worker_num, fpath, start, end): self._workers_active += 1 try: with socket(AF_INET, SOCK_STREAM) as sock: sock.connect( (self.recver_ip, self._worker_ports[worker_num]) ) sock.send(self.token) assert sock.recv(1) == b'\x06' # ACK fs = end - start with open(fpath, 'br') as f: f.seek(start) while self._sending_file and fs: end_size = f.tell() + self._packet_size size = (self._packet_size - max(0, end_size - end)) packet = f.read(size) if not packet: break sock.send(packet) assert sock.recv(1) == b'\x06' # ACK self._progress_bar.add_progress(len(packet)) fs -= len(packet) except KeyboardInterrupt: self._progress_bar.stop() self._sending_file = False return printerror("User aborted operation") except AssertionError: self._progress_bar.stop() self._sending_file = False return printerror(f"Receiver rejected packet") except FileNotFoundError: self._progress_bar.stop() self._sending_file = False return printerror("Couldn't access file") except PermissionError: self._progress_bar.stop() self._sending_file = False return printerror("Couldn't access file due to permission " "error") except timeout: self._progress_bar.stop() self._sending_file = False return printerror("Operation timed out") except: self._progress_bar.stop() self._sending_file = False return printerror(f"Error while sending file") finally: self._workers_active -= 1 num_workers = len(self._worker_ports) self._sending_file = True try: size = 0 for w in range(num_workers): Thread( target=threadfunc, args=( w, filepath, size, size + file_sizes[w] ), ).start() size += file_sizes[w] except FileNotFoundError: return printerror("Couldn't access file") except PermissionError: return printerror("Couldn't access file due to permission error") except: return printerror("Error while starting to send file") while self._workers_active: try: pass except KeyboardInterrupt: self._progress_bar.stop() self._sending_file = False return printerror("User aborted operation") self._sending_file = False return True def send_file(self, filepath): """ Send a file. Parameters ---------- filepath : str The filepath of the file to be sent. """ if not self.param_set: return printerror("Not yet set with receiver's parameters") if not exists(filepath): return printerror("File doesn't exist") # Headers try: tok = self.token num_w = max(1, len(self._worker_ports)) fpath = filepath.replace(altsep, sep) fname = fpath.split(sep)[-1] fsize = stat(fpath).st_size fsizes = [fsize//num_w for w in range(num_w)] fsizes[-1] += fsize - sum(fsizes) fn = pack_str(fname) len_fn = int_to_bytes_s(len(fn)) fs = [int_to_bytes_l(s) for s in fsizes] fs = b''.join(fs) len_fs = int_to_bytes_s(num_w) headers = b''.join([tok, len_fn, fn, len_fs, fs]) except: return printerror("Error while preparing headers") try: b64_tok = b64encode(self._recver_token).decode() printalert(f"Sending to {self._recver_hostname}-{b64_tok}:" f" [ {fname} ]") self.socket.send(headers) print("Waiting for receiver to accept file") assert self.socket.recv(1) == b'\x06' # ACK except KeyboardInterrupt: return printerror("User aborted operation") except AssertionError: return printerror("Receiver rejected") except timeout: return printerror("Operation timed out") except Exception: return printerror("Error while sending headers to receiver") print(f"[ {gethostname()}-{b64encode(self.token).decode()} ] " f"is now sending file ({ProgressBar.byte_rescale(fsize)})") # Progress bar thread self._progress_bar = ProgressBar(fsize, 40) self._progress_bar.start() def progress_thread(): try: # Wait until sending file while not self._sending_file: pass # Display until file is sent while not self._progress_bar.done: self._progress_bar.display() except: return printerror("Error with progress thread") Thread(target=progress_thread).start() # Start sending res = None try: if self._transfer_type == 'S': res = self._send_s(fpath, fsize) elif self._transfer_type == 'M': res = self._send_m(fpath, fsizes) assert self.socket.recv(1) == b'\x06' # ACK except: self._progress_bar.stop() self._sending_file = False return printerror(f"Sending file was unsuccessful") else: # Wait for progress bar while not self._progress_bar.done: pass self._progress_bar.display() print(f"\nSuccessfully sent: {fname}") return res def recv_param_set(self): """ Receive and unpack Receiver's parameter settings. Used to set Sender's parameter settings used during data transmissions. """ try: self.socket.connect((self.recver_ip, self.main_port)) except error: return printerror("Can't connect to " f"{self.recver_ip}:{self.main_port}") try: sender_hn = pack_str(gethostname()) len_sender_hn = int_to_bytes_s(len(sender_hn)) self.socket.send(b''.join([len_sender_hn, sender_hn])) assert self.socket.recv(1) == b'\x06' # ACK except AssertionError: return printerror("Receiver rejected handshake") except timeout: return printerror('Operation timed out') except: return printerror("Error during handshake") try: len_hn = bytes_to_int_s(self.socket.recv(2)) self._recver_hostname = unpack_str(self.socket.recv(len_hn)) self._recver_token = self.socket.recv(6) self._transfer_type = unpack_str(self.socket.recv(1)) len_wp = bytes_to_int_s(self.socket.recv(2)) self._worker_ports = [bytes_to_int_s(self.socket.recv(2)) for w in range(len_wp)] self.socket.send(b'\x06') # ACK except error: return printerror("Error getting connected with socket") except: self.socket.send(b'\x15') # NAK return printerror("Error getting parameters from receiver") else: self.param_set = True class FlyterReciever: """ Handles Flyter file receiving processes. Note: Receives from FlyterSender instances. Parameters ---------- host_ip : str The Host IP address to be used. main_port : int The main TCP port to be used. num_workers : int The amount of workers to be used during transmission. """ @staticmethod def storage_dir(hostname=None): """ Return the path of the storage dir for received files. If storage directory doesn't exist, creates it first. Parameters ---------- hostname : str The name of the subdirectory where that host's sent files are stored. """ app_dirname = dirname(__file__) appfiles_dirname = join(app_dirname, 'Flyter') if not exists(appfiles_dirname): mkdir(appfiles_dirname) storage_dirname = join(appfiles_dirname, 'Received Files') if not exists(storage_dirname): mkdir(storage_dirname) if hostname: host_storage_dirname = join(storage_dirname, hostname) if not exists(host_storage_dirname): mkdir(host_storage_dirname) return host_storage_dirname else: return storage_dirname DEFAULT_PACKET_SIZE = 512 def __init__(self, host_ip, main_port, num_workers): self.host_ip = host_ip self.main_port = main_port self.token = token_bytes(6) self.transfer_type = 'S' if num_workers == 1 else 'M' self.worker_ports = [ random_port(self.host_ip) for w in range(num_workers) ] if num_workers > 1 else [] self._sender_socket = None self._sender_hostname = None self._sender_token = None self._sender_filename = None self._sender_filesizes = None self._packet_size = FlyterSender.DEFAULT_PACKET_SIZE self._recving_file = False self._workers_active = 0 self._progress_bar = ProgressBar(None) try: self.socket = socket(AF_INET, SOCK_STREAM) self.socket.bind((self.host_ip, self.main_port)) self.socket.settimeout(60) self.workers = [ socket(AF_INET, SOCK_STREAM) for w in range(num_workers) ] if num_workers > 1 else [] if self.workers: for w in range(num_workers): self.workers[w].bind((self.host_ip, self.worker_ports[w])) self.workers[w].settimeout(60) except: printerror('Error initializing sockets') self.param_set = False def __del__(self): if isinstance(self.__dict__.get('socket'), socket): self.socket.close() if self.__dict__.get('workers'): for w in self.workers: w.close() def _recv_s(self): """Receive a file with a single worker.""" if not self.param_set: return printerror("Sender not yet set with parameters") try: self._recving_file = True path = join( FlyterReciever.storage_dir(self._sender_hostname), self._sender_filename ) fs = self._sender_filesizes[0] with open(path, 'bw') as f: while self._recving_file and fs: packet = self._sender_socket.recv(self._packet_size) f.write(packet) self._progress_bar.add_progress(len(packet)) fs -= len(packet) self._sender_socket.send(b'\x06') # ACK except timeout: self._progress_bar.stop() return printerror("Operation timed out") except FileNotFoundError: self._progress_bar.stop() return printerror("Downloading file has been deleted") except PermissionError: self._progress_bar.stop() return printerror("Couldn't access storage directory") except error: self._progress_bar.stop() return printerror("Error with socket") except: self._progress_bar.stop() return printerror("Error receiving file") else: self._recving_file = False return True def _recv_m(self): """ Receive a file with multiple workers. Speeds up transmission rate by using multiple workers. """ if not self.param_set: return printerror("Sender not yet set with parameters") def threadfunc(worker_num, fpath): self._workers_active += 1 try: recver_socket = self.workers[worker_num] recver_socket.listen(1) sender_socket, hostaddr = recver_socket.accept() send_tok = sender_socket.recv(6) if send_tok == self._sender_token: sender_socket.send(b'\x06') # ACK else: sender_socket.send(b'\x15') # NAK fs = self._sender_filesizes[worker_num] with open(fpath, 'bw') as f: while self._recving_file and f.writable() and fs: packet = sender_socket.recv(self._packet_size) f.write(packet) self._progress_bar.add_progress(len(packet)) fs -= len(packet) sender_socket.send(b'\x06') # ACK except KeyboardInterrupt: self._progress_bar.stop() self._recving_file = False return printerror("User aborted operation") except timeout: self._progress_bar.stop() self._recving_file = False return printerror("Operation timed out") except error: self._progress_bar.stop() self._recving_file = False return printerror("Error with sockets") except: self._progress_bar.stop() self._recving_file = False return printerror("Error while receiving file") finally: self._workers_active -= 1 num_workers = len(self.workers) self._recving_file = True try: for w in range(len(self.worker_ports)): wpath = join( FlyterReciever.storage_dir(self._sender_hostname), f"{w}_{self._sender_filename}" ) Thread( target=threadfunc, args=(w, wpath), ).start() except FileNotFoundError: return printerror("Couldn't access file") except PermissionError: return printerror("Couldn't access file due to permission error") while self._workers_active: try: pass except KeyboardInterrupt: self._progress_bar.stop() self._recving_file = False printerror("User aborted operation") self._recving_file = False try: # Build the file path = join( FlyterReciever.storage_dir(self._sender_hostname), self._sender_filename ) with open(path, 'bw') as output: for w in range(num_workers): wpath = join( FlyterReciever.storage_dir(self._sender_hostname), f"{w}_{self._sender_filename}" ) with open(wpath, 'br') as temp: packet = True while packet: packet = temp.read(self._packet_size) output.write(packet) # Clear the contents of the temp file open(wpath, 'bw').close() # Delete the temp files for w in range(num_workers): wpath = join( FlyterReciever.storage_dir(self._sender_hostname), f"{w}_{self._sender_filename}" ) unlink(wpath) except PermissionError: self._sender_socket.send(b'\x15') # NAK return printerror("Couldn't save file due to permissions") except error: return printerror("Error with sockets") except: self._sender_socket.send(b'\x15') # NAK return printerror("Error while saving file") else: return True def recv_file(self): """Receive a file.""" if not self.param_set: return printerror("Not yet set with receiver's parameters") # Headers try: tok = self._sender_socket.recv(6) b64_tok = b64encode(tok).decode() len_fn = bytes_to_int_s(self._sender_socket.recv(2)) fn = unpack_str(self._sender_socket.recv(len_fn)) len_fs = bytes_to_int_s(self._sender_socket.recv(2)) fs = [bytes_to_int_l(self._sender_socket.recv(4)) for s in range(len_fs)] fs_all = sum(fs) answer = input(f"{self._sender_hostname}-{b64_tok}" f" wants to send: {fn} " f"({ProgressBar.byte_rescale(fs_all)}). " "Accept? (y/n) ") if answer.lower() == 'y': self._sender_socket.send(b'\x06') # ACK else: self._sender_socket.send(b'\x06') # NAK return printalert("Rejected file transfer") except error: return printerror("Sender isn't available anymore") except: self._sender_socket.send(b'\x15') # NAK return printerror("Error while receiving headers") print(f"[ {gethostname()}-{b64encode(self.token).decode()} ] " f"is now receiving file ({ProgressBar.byte_rescale(fs_all)})") # Progress bar thread self._progress_bar = ProgressBar(fs_all, 35) self._progress_bar.start() def progress_thread(): try: # Wait until receiving file while not self._recving_file: pass # Display until file is received while not self._progress_bar.done: self._progress_bar.display() except: return printerror("Error with progress thread") Thread(target=progress_thread).start() self._sender_token = tok self._sender_filename = fn self._sender_filesizes = fs # Start receiving try: if self.transfer_type == 'S': res = self._recv_s() elif self.transfer_type == 'M': res = self._recv_m() else: res = None except: self._progress_bar.stop() self._recving_file = False return printerror("Receiving file was unsuccessful") else: self._sender_socket.send(b'\x06') # ACK # Wait for progress bar while not self._progress_bar.done: pass self._progress_bar.display() print(f"\nSuccessfully received: {self._sender_filename}") return res def send_param_set(self): """ Pack and send Receiver's parameter settings. Used to set Sender's parameter settings used during data transmissions. """ try: printalert("Waiting for sender") self.socket.listen(1) self._sender_socket, addrport = self.socket.accept() except timeout: return printerror("No sender available") except: return printerror("Error while waiting for sender") try: len_sender_hn = bytes_to_int_s(self._sender_socket.recv(2)) sender_hn = self._sender_socket.recv(len_sender_hn) self._sender_hostname = unpack_str(sender_hn) self._sender_socket.send(b'\x06') # ACK except timeout: return printerror("Operation timed out") except: return printerror("Error during handshake") try: hn = pack_str(gethostname()) len_hn = int_to_bytes_s(len(hn)) tok = self.token tr_type = pack_str(self.transfer_type) len_wp = int_to_bytes_s(len(self.worker_ports)) wp = [int_to_bytes_s(port) for port in self.worker_ports] wp = b''.join(wp) headers = b''.join([len_hn, hn, tok, tr_type, len_wp, wp]) except: return printerror("Error building headers") try: self._sender_socket.send(headers) assert self._sender_socket.recv(1) == b'\x06' # ACK except: return printerror("Error while sending headers to sender") else: self.param_set = True # Simplified Functions def send(ip_address, port, filepath): """ Send file to receiver on the same network. Parameters ---------- ip_address : str The target receiver's IP address. port : int The target receiver's main TCP port. filepath : str The path to the file to be sent. """ sender = FlyterSender(ip_address, port) sender.recv_param_set() return sender.send_file(filepath) def receive(host_ip_address, port, workers=1): """ Receive a file from sender on the same network. Parameters ---------- host_ip_address : str The receiver's host IP address. port : int The receiver's host port to listen on. workers : :obj:`int`, optional The number of workers to use. """ receiver = FlyterReciever(host_ip_address, port, workers) receiver.send_param_set() receiver.recv_file() if __name__ == '__main__': parser = ArgumentParser( prog="Flyter", epilog="See '<command> --help' to read about a specific sub-command." ) subparsers = parser.add_subparsers( dest="action", help="The action to be performed" ) send_parser = subparsers.add_parser("send") recv_parser = subparsers.add_parser("recv") send_parser.add_argument('-i', '--ip', required=True, help="Target receiver's IP address") send_parser.add_argument('-p', '--port', type=int, required=True, help="Target receiver's TCP port number") send_parser.add_argument('-f', '--file', required=True, help="Path to the file to be sent") recv_parser.add_argument('-i', '--ip', required=True, help="Host IP address") recv_parser.add_argument('-p', '--port', type=int, required=True, help="TCP port to listen on") recv_parser.add_argument('-w', '--workers', type=int, default=1, help="TCP port to listen on") if len(argv) > 1: FROMTERMINAL = True args = parser.parse_args() if args.action == "send": send(args.ip, args.port, args.file) elif args.action == "recv": receive(args.ip, args.port, args.workers) else: parser.print_help()
[((1853, 1954), 'warnings.warn', 'warn', (['"""[!] Some features are not be compatible with the version of your python interpreter"""'], {}), "(\n '[!] Some features are not be compatible with the version of your python interpreter'\n )\n", (1857, 1954), False, 'from warnings import warn\n'), ((2782, 2796), 'socket.ntohs', 'ntohs', (['integer'], {}), '(integer)\n', (2787, 2796), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((3046, 3056), 'socket.htons', 'htons', (['res'], {}), '(res)\n', (3051, 3056), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((3155, 3169), 'socket.ntohl', 'ntohl', (['integer'], {}), '(integer)\n', (3160, 3169), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((3419, 3429), 'socket.htonl', 'htonl', (['res'], {}), '(res)\n', (3424, 3429), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((31753, 31858), 'argparse.ArgumentParser', 'ArgumentParser', ([], {'prog': '"""Flyter"""', 'epilog': '"""See \'<command> --help\' to read about a specific sub-command."""'}), '(prog=\'Flyter\', epilog=\n "See \'<command> --help\' to read about a specific sub-command.")\n', (31767, 31858), False, 'from argparse import ArgumentParser\n'), ((2104, 2125), 'random.randint', 'randint', (['(10000)', '(65536)'], {}), '(10000, 65536)\n', (2111, 2125), False, 'from random import randint\n'), ((2482, 2490), 'sys.exit', 'exit', (['(-1)'], {}), '(-1)\n', (2486, 2490), False, 'from sys import argv, exit, version_info\n'), ((2499, 2507), 'sys.exit', 'exit', (['(-1)'], {}), '(-1)\n', (2503, 2507), False, 'from sys import argv, exit, version_info\n'), ((2516, 2524), 'sys.exit', 'exit', (['(-1)'], {}), '(-1)\n', (2520, 2524), False, 'from sys import argv, exit, version_info\n'), ((2533, 2541), 'sys.exit', 'exit', (['(-1)'], {}), '(-1)\n', (2537, 2541), False, 'from sys import argv, exit, version_info\n'), ((2560, 2574), 'warnings.warn', 'warn', (['errormsg'], {}), '(errormsg)\n', (2564, 2574), False, 'from warnings import warn\n'), ((4671, 4677), 'time.time', 'time', ([], {}), '()\n', (4675, 4677), False, 'from time import time\n'), ((6573, 6587), 'secrets.token_bytes', 'token_bytes', (['(6)'], {}), '(6)\n', (6584, 6587), False, 'from secrets import token_bytes\n'), ((18309, 18326), 'os.path.dirname', 'dirname', (['__file__'], {}), '(__file__)\n', (18316, 18326), False, 'from os.path import dirname, exists, join\n'), ((18355, 18382), 'os.path.join', 'join', (['app_dirname', '"""Flyter"""'], {}), "(app_dirname, 'Flyter')\n", (18359, 18382), False, 'from os.path import dirname, exists, join\n'), ((18487, 18527), 'os.path.join', 'join', (['appfiles_dirname', '"""Received Files"""'], {}), "(appfiles_dirname, 'Received Files')\n", (18491, 18527), False, 'from os.path import dirname, exists, join\n'), ((19051, 19065), 'secrets.token_bytes', 'token_bytes', (['(6)'], {}), '(6)\n', (19062, 19065), False, 'from secrets import token_bytes\n'), ((2140, 2168), 'socket.socket', 'socket', (['AF_INET', 'SOCK_STREAM'], {}), '(AF_INET, SOCK_STREAM)\n', (2146, 2168), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((5311, 5317), 'time.time', 'time', ([], {}), '()\n', (5315, 5317), False, 'from time import time\n'), ((5705, 5729), 'datetime.timedelta', 'timedelta', ([], {'seconds': 'eta_s'}), '(seconds=eta_s)\n', (5714, 5729), False, 'from datetime import timedelta\n'), ((6934, 6962), 'socket.socket', 'socket', (['AF_INET', 'SOCK_STREAM'], {}), '(AF_INET, SOCK_STREAM)\n', (6940, 6962), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((7542, 7558), 'os.path.exists', 'exists', (['filepath'], {}), '(filepath)\n', (7548, 7558), False, 'from os.path import dirname, exists, join\n'), ((9313, 9329), 'os.path.exists', 'exists', (['filepath'], {}), '(filepath)\n', (9319, 9329), False, 'from os.path import dirname, exists, join\n'), ((12988, 13004), 'os.path.exists', 'exists', (['filepath'], {}), '(filepath)\n', (12994, 13004), False, 'from os.path import dirname, exists, join\n'), ((18398, 18422), 'os.path.exists', 'exists', (['appfiles_dirname'], {}), '(appfiles_dirname)\n', (18404, 18422), False, 'from os.path import dirname, exists, join\n'), ((18436, 18459), 'os.mkdir', 'mkdir', (['appfiles_dirname'], {}), '(appfiles_dirname)\n', (18441, 18459), False, 'from os import altsep, sep, mkdir, stat, unlink\n'), ((18543, 18566), 'os.path.exists', 'exists', (['storage_dirname'], {}), '(storage_dirname)\n', (18549, 18566), False, 'from os.path import dirname, exists, join\n'), ((18580, 18602), 'os.mkdir', 'mkdir', (['storage_dirname'], {}), '(storage_dirname)\n', (18585, 18602), False, 'from os import altsep, sep, mkdir, stat, unlink\n'), ((18660, 18691), 'os.path.join', 'join', (['storage_dirname', 'hostname'], {}), '(storage_dirname, hostname)\n', (18664, 18691), False, 'from os.path import dirname, exists, join\n'), ((19662, 19690), 'socket.socket', 'socket', (['AF_INET', 'SOCK_STREAM'], {}), '(AF_INET, SOCK_STREAM)\n', (19668, 19690), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((13284, 13295), 'os.stat', 'stat', (['fpath'], {}), '(fpath)\n', (13288, 13295), False, 'from os import altsep, sep, mkdir, stat, unlink\n'), ((15107, 15137), 'threading.Thread', 'Thread', ([], {'target': 'progress_thread'}), '(target=progress_thread)\n', (15113, 15137), False, 'from threading import Thread\n'), ((16307, 16320), 'socket.gethostname', 'gethostname', ([], {}), '()\n', (16318, 16320), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((18711, 18739), 'os.path.exists', 'exists', (['host_storage_dirname'], {}), '(host_storage_dirname)\n', (18717, 18739), False, 'from os.path import dirname, exists, join\n'), ((18757, 18784), 'os.mkdir', 'mkdir', (['host_storage_dirname'], {}), '(host_storage_dirname)\n', (18762, 18784), False, 'from os import altsep, sep, mkdir, stat, unlink\n'), ((25880, 25893), 'os.unlink', 'unlink', (['wpath'], {}), '(wpath)\n', (25886, 25893), False, 'from os import altsep, sep, mkdir, stat, unlink\n'), ((28250, 28280), 'threading.Thread', 'Thread', ([], {'target': 'progress_thread'}), '(target=progress_thread)\n', (28256, 28280), False, 'from threading import Thread\n'), ((30068, 30081), 'socket.gethostname', 'gethostname', ([], {}), '()\n', (30079, 30081), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((4027, 4039), 'math.log', 'log', (['data', '(2)'], {}), '(data, 2)\n', (4030, 4039), False, 'from math import log\n'), ((5787, 5806), 'shutil.get_terminal_size', 'get_terminal_size', ([], {}), '()\n', (5804, 5806), False, 'from shutil import get_terminal_size\n'), ((9509, 9537), 'socket.socket', 'socket', (['AF_INET', 'SOCK_STREAM'], {}), '(AF_INET, SOCK_STREAM)\n', (9515, 9537), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((13792, 13821), 'base64.b64encode', 'b64encode', (['self._recver_token'], {}), '(self._recver_token)\n', (13801, 13821), False, 'from base64 import b64encode\n'), ((14464, 14477), 'socket.gethostname', 'gethostname', ([], {}), '()\n', (14475, 14477), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((19837, 19865), 'socket.socket', 'socket', (['AF_INET', 'SOCK_STREAM'], {}), '(AF_INET, SOCK_STREAM)\n', (19843, 19865), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((26547, 26561), 'base64.b64encode', 'b64encode', (['tok'], {}), '(tok)\n', (26556, 26561), False, 'from base64 import b64encode\n'), ((27597, 27610), 'socket.gethostname', 'gethostname', ([], {}), '()\n', (27608, 27610), False, 'from socket import socket, error, timeout, ntohs, ntohl, htons, htonl, gethostname, AF_INET, SOCK_STREAM\n'), ((11833, 11906), 'threading.Thread', 'Thread', ([], {'target': 'threadfunc', 'args': '(w, filepath, size, size + file_sizes[w])'}), '(target=threadfunc, args=(w, filepath, size, size + file_sizes[w]))\n', (11839, 11906), False, 'from threading import Thread\n'), ((24210, 24252), 'threading.Thread', 'Thread', ([], {'target': 'threadfunc', 'args': '(w, wpath)'}), '(target=threadfunc, args=(w, wpath))\n', (24216, 24252), False, 'from threading import Thread\n'), ((14480, 14501), 'base64.b64encode', 'b64encode', (['self.token'], {}), '(self.token)\n', (14489, 14501), False, 'from base64 import b64encode\n'), ((27613, 27634), 'base64.b64encode', 'b64encode', (['self.token'], {}), '(self.token)\n', (27622, 27634), False, 'from base64 import b64encode\n')]
patelrajnath/transformers
tests/test_modeling_tf_led.py
98afe9d7c94a840d4b30c7eb76f9bfe570d2ed50
# coding=utf-8 # Copyright Iz Beltagy, Matthew E. Peters, Arman Cohan and The HuggingFace Inc. team. All rights reserved. # # 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. import unittest from transformers import LEDConfig, is_tf_available from transformers.testing_utils import require_tf, slow from .test_configuration_common import ConfigTester from .test_modeling_tf_common import TFModelTesterMixin, ids_tensor if is_tf_available(): import tensorflow as tf from transformers import TFLEDForConditionalGeneration, TFLEDModel @require_tf class TFLEDModelTester: config_cls = LEDConfig config_updates = {} hidden_act = "gelu" def __init__( self, parent, batch_size=13, seq_length=7, is_training=True, use_labels=False, vocab_size=99, hidden_size=32, num_hidden_layers=5, num_attention_heads=4, intermediate_size=37, hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=20, eos_token_id=2, pad_token_id=1, bos_token_id=0, attention_window=4, ): self.parent = parent self.batch_size = batch_size self.seq_length = seq_length self.is_training = is_training self.use_labels = use_labels self.vocab_size = vocab_size self.hidden_size = hidden_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.intermediate_size = intermediate_size self.hidden_dropout_prob = hidden_dropout_prob self.attention_probs_dropout_prob = attention_probs_dropout_prob self.max_position_embeddings = max_position_embeddings self.eos_token_id = eos_token_id self.pad_token_id = pad_token_id self.bos_token_id = bos_token_id self.attention_window = attention_window # `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size # [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention # returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1] # because its local attention only attends to `self.attention_window` and one before and one after self.key_length = self.attention_window + 1 # because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for # the `test_attention_outputs` and `test_hidden_states_output` tests self.encoder_seq_length = ( self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window ) def prepare_config_and_inputs_for_common(self): input_ids = ids_tensor([self.batch_size, self.seq_length - 1], self.vocab_size) eos_tensor = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size), 1) input_ids = tf.concat([input_ids, eos_tensor], axis=1) decoder_input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size) config = self.config_cls( vocab_size=self.vocab_size, d_model=self.hidden_size, encoder_layers=self.num_hidden_layers, decoder_layers=self.num_hidden_layers, encoder_attention_heads=self.num_attention_heads, decoder_attention_heads=self.num_attention_heads, encoder_ffn_dim=self.intermediate_size, decoder_ffn_dim=self.intermediate_size, dropout=self.hidden_dropout_prob, attention_dropout=self.attention_probs_dropout_prob, max_position_embeddings=self.max_position_embeddings, eos_token_ids=[2], bos_token_id=self.bos_token_id, pad_token_id=self.pad_token_id, decoder_start_token_id=self.pad_token_id, attention_window=self.attention_window, **self.config_updates, ) inputs_dict = prepare_led_inputs_dict(config, input_ids, decoder_input_ids) global_attention_mask = tf.concat( [tf.zeros_like(input_ids)[:, :-1], tf.ones_like(input_ids)[:, -1:]], axis=-1, ) inputs_dict["global_attention_mask"] = global_attention_mask return config, inputs_dict def check_decoder_model_past_large_inputs(self, config, inputs_dict): model = TFLEDModel(config=config).get_decoder() input_ids = inputs_dict["input_ids"] input_ids = input_ids[:1, :] attention_mask = inputs_dict["attention_mask"][:1, :] self.batch_size = 1 # first forward pass outputs = model(input_ids, attention_mask=attention_mask, use_cache=True) output, past_key_values = outputs.to_tuple() past_key_values = past_key_values[1] # create hypothetical next token and extent to next_input_ids next_tokens = ids_tensor((self.batch_size, 3), config.vocab_size) next_attn_mask = tf.cast(ids_tensor((self.batch_size, 3), 2), tf.int8) # append to next input_ids and next_input_ids = tf.concat([input_ids, next_tokens], axis=-1) next_attention_mask = tf.concat([attention_mask, next_attn_mask], axis=-1) output_from_no_past = model(next_input_ids, attention_mask=next_attention_mask)[0] output_from_past = model(next_tokens, attention_mask=next_attention_mask, past_key_values=past_key_values)[0] self.parent.assertEqual(next_tokens.shape[1], output_from_past.shape[1]) # select random slice random_slice_idx = int(ids_tensor((1,), output_from_past.shape[-1])) output_from_no_past_slice = output_from_no_past[:, -3:, random_slice_idx] output_from_past_slice = output_from_past[:, :, random_slice_idx] # test that outputs are equal for slice tf.debugging.assert_near(output_from_past_slice, output_from_no_past_slice, rtol=1e-3) def prepare_led_inputs_dict( config, input_ids, decoder_input_ids, attention_mask=None, decoder_attention_mask=None, ): if attention_mask is None: attention_mask = tf.cast(tf.math.not_equal(input_ids, config.pad_token_id), tf.int8) if decoder_attention_mask is None: decoder_attention_mask = tf.cast(tf.math.not_equal(decoder_input_ids, config.pad_token_id), tf.int8) return { "input_ids": input_ids, "attention_mask": attention_mask, "decoder_input_ids": decoder_input_ids, "decoder_attention_mask": decoder_attention_mask, } @require_tf class TFLEDModelTest(TFModelTesterMixin, unittest.TestCase): all_model_classes = (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else () all_generative_model_classes = (TFLEDForConditionalGeneration,) if is_tf_available() else () is_encoder_decoder = True test_pruning = False def setUp(self): self.model_tester = TFLEDModelTester(self) self.config_tester = ConfigTester(self, config_class=LEDConfig) def test_config(self): self.config_tester.run_common_tests() def test_decoder_model_past_large_inputs(self): config_and_inputs = self.model_tester.prepare_config_and_inputs_for_common() self.model_tester.check_decoder_model_past_large_inputs(*config_and_inputs) def test_model_common_attributes(self): config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() for model_class in self.all_model_classes: model = model_class(config) assert isinstance(model.get_input_embeddings(), tf.keras.layers.Layer) x = model.get_output_layer_with_bias() assert x is None name = model.get_prefix_bias_name() assert name is None def test_attention_outputs(self): config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common() inputs_dict["global_attention_mask"] = tf.zeros_like(inputs_dict["attention_mask"]) num_global_attn_indices = 2 inputs_dict["global_attention_mask"] = tf.where( tf.range(self.model_tester.seq_length)[None, :] < num_global_attn_indices, 1, inputs_dict["global_attention_mask"], ) config.return_dict = True seq_length = self.model_tester.seq_length encoder_seq_length = self.model_tester.encoder_seq_length def check_decoder_attentions_output(outputs): decoder_attentions = outputs.decoder_attentions self.assertEqual(len(decoder_attentions), self.model_tester.num_hidden_layers) self.assertListEqual( list(decoder_attentions[0].shape[-3:]), [self.model_tester.num_attention_heads, seq_length, seq_length], ) def check_encoder_attentions_output(outputs): attentions = [t.numpy() for t in outputs.encoder_attentions] global_attentions = [t.numpy() for t in outputs.encoder_global_attentions] self.assertEqual(len(attentions), self.model_tester.num_hidden_layers) self.assertEqual(len(global_attentions), self.model_tester.num_hidden_layers) self.assertListEqual( list(attentions[0].shape[-3:]), [self.model_tester.num_attention_heads, encoder_seq_length, seq_length], ) self.assertListEqual( list(global_attentions[0].shape[-3:]), [self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices], ) for model_class in self.all_model_classes: inputs_dict["output_attentions"] = True inputs_dict["use_cache"] = False config.output_hidden_states = False model = model_class(config) outputs = model(self._prepare_for_class(inputs_dict, model_class)) out_len = len(outputs) self.assertEqual(config.output_hidden_states, False) check_encoder_attentions_output(outputs) if self.is_encoder_decoder: model = model_class(config) outputs = model(self._prepare_for_class(inputs_dict, model_class)) self.assertEqual(config.output_hidden_states, False) check_decoder_attentions_output(outputs) # Check that output attentions can also be changed via the config del inputs_dict["output_attentions"] config.output_attentions = True model = model_class(config) outputs = model(self._prepare_for_class(inputs_dict, model_class)) self.assertEqual(config.output_hidden_states, False) check_encoder_attentions_output(outputs) # Check attention is always last and order is fine inputs_dict["output_attentions"] = True config.output_hidden_states = True model = model_class(config) outputs = model(self._prepare_for_class(inputs_dict, model_class)) self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1), len(outputs)) self.assertEqual(model.config.output_hidden_states, True) check_encoder_attentions_output(outputs) @slow def test_saved_model_with_attentions_output(self): # longformer has special attentions which are not # compatible in graph mode pass @slow def test_saved_model_with_hidden_states_output(self): # TODO(JPLU, PVP) this test should pass!!! PVP: # IMO there is a problem with the signature check. # Test passes for TFLEDModel, but not for TFLEDForConditionalGeneration # IMO the reason is that the tensor variable name cannot be changed # from decoder_input_ids -> input_ids, which poses a BIG restrictions pass def _assert_tensors_equal(a, b, atol=1e-12, prefix=""): """If tensors not close, or a and b arent both tensors, raise a nice Assertion error.""" if a is None and b is None: return True try: if tf.debugging.assert_near(a, b, atol=atol): return True raise except Exception: msg = "{} != {}".format(a, b) if prefix: msg = prefix + ": " + msg raise AssertionError(msg) def _long_tensor(tok_lst): return tf.constant(tok_lst, dtype=tf.int32) TOLERANCE = 1e-4 @slow @require_tf class TFLEDModelIntegrationTest(unittest.TestCase): def test_inference_no_head(self): model = TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384").led # change to intended input here input_ids = _long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]) decoder_input_ids = _long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]) inputs_dict = prepare_led_inputs_dict(model.config, input_ids, decoder_input_ids) output = model(**inputs_dict)[0] expected_shape = (1, 1024, 768) self.assertEqual(output.shape, expected_shape) # change to expected output here expected_slice = tf.convert_to_tensor( [[2.3050, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -2.2043]], ) tf.debugging.assert_near(output[:, :3, :3], expected_slice, atol=TOLERANCE) def test_inference_with_head(self): model = TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384") # change to intended input here input_ids = _long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]) decoder_input_ids = _long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]) inputs_dict = prepare_led_inputs_dict(model.config, input_ids, decoder_input_ids) output = model(**inputs_dict)[0] expected_shape = (1, 1024, model.config.vocab_size) self.assertEqual(output.shape, expected_shape) # change to expected output here expected_slice = tf.convert_to_tensor( [[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149, 4.2783]], ) tf.debugging.assert_near(output[:, :3, :3], expected_slice, atol=TOLERANCE)
[((921, 938), 'transformers.is_tf_available', 'is_tf_available', ([], {}), '()\n', (936, 938), False, 'from transformers import LEDConfig, is_tf_available\n'), ((12895, 12931), 'tensorflow.constant', 'tf.constant', (['tok_lst'], {'dtype': 'tf.int32'}), '(tok_lst, dtype=tf.int32)\n', (12906, 12931), True, 'import tensorflow as tf\n'), ((3488, 3530), 'tensorflow.concat', 'tf.concat', (['[input_ids, eos_tensor]'], {'axis': '(1)'}), '([input_ids, eos_tensor], axis=1)\n', (3497, 3530), True, 'import tensorflow as tf\n'), ((5661, 5705), 'tensorflow.concat', 'tf.concat', (['[input_ids, next_tokens]'], {'axis': '(-1)'}), '([input_ids, next_tokens], axis=-1)\n', (5670, 5705), True, 'import tensorflow as tf\n'), ((5736, 5788), 'tensorflow.concat', 'tf.concat', (['[attention_mask, next_attn_mask]'], {'axis': '(-1)'}), '([attention_mask, next_attn_mask], axis=-1)\n', (5745, 5788), True, 'import tensorflow as tf\n'), ((6402, 6493), 'tensorflow.debugging.assert_near', 'tf.debugging.assert_near', (['output_from_past_slice', 'output_from_no_past_slice'], {'rtol': '(0.001)'}), '(output_from_past_slice, output_from_no_past_slice,\n rtol=0.001)\n', (6426, 6493), True, 'import tensorflow as tf\n'), ((7248, 7265), 'transformers.is_tf_available', 'is_tf_available', ([], {}), '()\n', (7263, 7265), False, 'from transformers import LEDConfig, is_tf_available\n'), ((7345, 7362), 'transformers.is_tf_available', 'is_tf_available', ([], {}), '()\n', (7360, 7362), False, 'from transformers import LEDConfig, is_tf_available\n'), ((8507, 8551), 'tensorflow.zeros_like', 'tf.zeros_like', (["inputs_dict['attention_mask']"], {}), "(inputs_dict['attention_mask'])\n", (8520, 8551), True, 'import tensorflow as tf\n'), ((12623, 12664), 'tensorflow.debugging.assert_near', 'tf.debugging.assert_near', (['a', 'b'], {'atol': 'atol'}), '(a, b, atol=atol)\n', (12647, 12664), True, 'import tensorflow as tf\n'), ((13664, 13772), 'tensorflow.convert_to_tensor', 'tf.convert_to_tensor', (['[[2.305, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -\n 2.2043]]'], {}), '([[2.305, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661],\n [-1.0186, 0.4586, -2.2043]])\n', (13684, 13772), True, 'import tensorflow as tf\n'), ((13801, 13876), 'tensorflow.debugging.assert_near', 'tf.debugging.assert_near', (['output[:, :3, :3]', 'expected_slice'], {'atol': 'TOLERANCE'}), '(output[:, :3, :3], expected_slice, atol=TOLERANCE)\n', (13825, 13876), True, 'import tensorflow as tf\n'), ((13934, 14005), 'transformers.TFLEDForConditionalGeneration.from_pretrained', 'TFLEDForConditionalGeneration.from_pretrained', (['"""allenai/led-base-16384"""'], {}), "('allenai/led-base-16384')\n", (13979, 14005), False, 'from transformers import TFLEDForConditionalGeneration, TFLEDModel\n'), ((14537, 14648), 'tensorflow.convert_to_tensor', 'tf.convert_to_tensor', (['[[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149,\n 4.2783]]'], {}), '([[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902\n ], [-3.2139, -4.3149, 4.2783]])\n', (14557, 14648), True, 'import tensorflow as tf\n'), ((14675, 14750), 'tensorflow.debugging.assert_near', 'tf.debugging.assert_near', (['output[:, :3, :3]', 'expected_slice'], {'atol': 'TOLERANCE'}), '(output[:, :3, :3], expected_slice, atol=TOLERANCE)\n', (14699, 14750), True, 'import tensorflow as tf\n'), ((3413, 3463), 'tensorflow.constant', 'tf.constant', (['([self.eos_token_id] * self.batch_size)'], {}), '([self.eos_token_id] * self.batch_size)\n', (3424, 3463), True, 'import tensorflow as tf\n'), ((6695, 6744), 'tensorflow.math.not_equal', 'tf.math.not_equal', (['input_ids', 'config.pad_token_id'], {}), '(input_ids, config.pad_token_id)\n', (6712, 6744), True, 'import tensorflow as tf\n'), ((6835, 6892), 'tensorflow.math.not_equal', 'tf.math.not_equal', (['decoder_input_ids', 'config.pad_token_id'], {}), '(decoder_input_ids, config.pad_token_id)\n', (6852, 6892), True, 'import tensorflow as tf\n'), ((13077, 13148), 'transformers.TFLEDForConditionalGeneration.from_pretrained', 'TFLEDForConditionalGeneration.from_pretrained', (['"""allenai/led-base-16384"""'], {}), "('allenai/led-base-16384')\n", (13122, 13148), False, 'from transformers import TFLEDForConditionalGeneration, TFLEDModel\n'), ((4948, 4973), 'transformers.TFLEDModel', 'TFLEDModel', ([], {'config': 'config'}), '(config=config)\n', (4958, 4973), False, 'from transformers import TFLEDForConditionalGeneration, TFLEDModel\n'), ((4654, 4678), 'tensorflow.zeros_like', 'tf.zeros_like', (['input_ids'], {}), '(input_ids)\n', (4667, 4678), True, 'import tensorflow as tf\n'), ((4688, 4711), 'tensorflow.ones_like', 'tf.ones_like', (['input_ids'], {}), '(input_ids)\n', (4700, 4711), True, 'import tensorflow as tf\n'), ((8657, 8695), 'tensorflow.range', 'tf.range', (['self.model_tester.seq_length'], {}), '(self.model_tester.seq_length)\n', (8665, 8695), True, 'import tensorflow as tf\n')]
plonerma/wann-genetic
src/wann_genetic/individual/numpy/ffnn.py
c4a8a1db81665b2549994d615e1d347dbe00226a
import numpy as np import sklearn import logging from wann_genetic.individual.network_base import BaseFFNN def softmax(x, axis=-1): """Compute softmax values for each sets of scores in x. Returns: softmax - softmax normalized in dim axis """ e_x = np.exp(x - np.expand_dims(np.max(x,axis=axis), axis=axis)) s = (e_x / np.expand_dims(e_x.sum(axis=-1), axis=axis)) return s def apply_act_function(available_funcs, selected_funcs, x=None): """Apply the activation function of the selected nodes to their sums. This fullfils the same function as the :class:`wann_genetic.individual.torch.ffn.MultiActivationModule`. """ if x is not None: result = np.empty(x.shape) for i, func in enumerate(selected_funcs): assert func < len(available_funcs) result[..., i] = available_funcs[func][1](x[..., i]) return result else: return np.array([ # return function names available_funcs[func][0] for func in selected_funcs ]) class Network(BaseFFNN): """Numpy implmentation of a Feed Forward Neural Network For an explanation of how propagation works, see :doc:`numpy_network`. """ # Definition of the activations functions available_act_functions = [ ('relu', lambda x: np.maximum(0, x)), ('sigmoid', lambda x: (np.tanh(x/2.0) + 1.0)/2.0), ('tanh', lambda x: np.tanh(x)), ('gaussian (standard)', lambda x: np.exp(-np.multiply(x, x) / 2.0)), ('step', lambda x: 1.0*(x>0.0)), ('identity', lambda x: x), ('inverse', lambda x: -x), ('squared', lambda x: x**2), # unstable if applied multiple times ('abs', lambda x: np.abs(x)), ('cos', lambda x: np.cos(np.pi*x)), ('sin ', lambda x: np.sin(np.pi*x)), ] enabled_act_functions = available_act_functions def get_measurements(self, weights, x, y_true=None, measures=['predictions']): assert len(x.shape) == 2 # multiple one dimensional input arrays assert isinstance(weights, np.ndarray) # initial activations act_vec = np.empty((weights.shape[0], x.shape[0], self.n_nodes), dtype=float) act_vec[..., :self.n_in] = x[...] act_vec[..., self.n_in] = 1 # bias # propagate signal through all layers for active_nodes in self.layers(): act_vec[..., active_nodes] = self.calc_act(act_vec, active_nodes, weights) # if any node is nan, we cant rely on the result valid = np.all(~np.isnan(act_vec), axis=-1) act_vec[~valid, :] = np.nan y_raw = act_vec[..., -self.n_out:] return self.measurements_from_output(y_raw, y_true, measures) def measurements_from_output(self, y_raw, y_true, measures): return_values = dict() if 'raw' in measures: return_values['raw'] = y_raw y_pred = np.argmax(y_raw, axis=-1) y_prob = softmax(y_raw, axis=-1) if 'probabilities' in measures: return_values['probabilities'] = y_prob if 'predictions' in measures: return_values['predictions'] = y_pred y_raw = y_raw.reshape(y_raw.shape[0], -1, self.n_out) y_prob = y_prob.reshape(y_raw.shape[0], -1, self.n_out) y_pred = y_pred.reshape(y_raw.shape[0], -1) if y_true is not None: y_true = y_true.reshape(-1) if 'log_loss' in measures: # nan is same as maximally falsely predicted y_prob[~np.isfinite(y_prob)] = 0 return_values['log_loss'] = np.array([ sklearn.metrics.log_loss(y_true, prob, labels=np.arange(self.n_out)) for prob in y_prob ]) if 'mse_loss' in measures: return_values['mse_loss'] = np.array([ sklearn.metrics.mean_squared_error(y_true, raw) for raw in y_raw ]) if 'accuracy' in measures: return_values['accuracy'] = np.array([ sklearn.metrics.accuracy_score(y_true, pred) for pred in y_pred ]) if 'kappa' in measures: return_values['kappa'] = np.array([ sklearn.metrics.cohen_kappa_score(y_true, pred) for pred in y_pred ]) return return_values def activation_functions(self, nodes, x=None): funcs = self.nodes['func'][nodes - self.offset] return apply_act_function(self.enabled_act_functions, funcs, x) def calc_act(self, x, active_nodes, base_weights, add_to_sum=0): """Apply updates for active nodes (active nodes can't share edges). """ addend_nodes = active_nodes[0] M = self.weight_matrix[:addend_nodes, active_nodes - self.offset] # x3d: weights, samples, source nodes # M3d: weights, source, target # multiply relevant weight matrix with base weights M3d = M[None, :, :] * base_weights[:, None, None] x3d = x[..., :addend_nodes] act_sums = np.matmul(x3d, M3d) + add_to_sum # apply activation function for active nodes return self.activation_functions(active_nodes, act_sums)
[((709, 726), 'numpy.empty', 'np.empty', (['x.shape'], {}), '(x.shape)\n', (717, 726), True, 'import numpy as np\n'), ((936, 999), 'numpy.array', 'np.array', (['[available_funcs[func][0] for func in selected_funcs]'], {}), '([available_funcs[func][0] for func in selected_funcs])\n', (944, 999), True, 'import numpy as np\n'), ((2146, 2213), 'numpy.empty', 'np.empty', (['(weights.shape[0], x.shape[0], self.n_nodes)'], {'dtype': 'float'}), '((weights.shape[0], x.shape[0], self.n_nodes), dtype=float)\n', (2154, 2213), True, 'import numpy as np\n'), ((2925, 2950), 'numpy.argmax', 'np.argmax', (['y_raw'], {'axis': '(-1)'}), '(y_raw, axis=-1)\n', (2934, 2950), True, 'import numpy as np\n'), ((5087, 5106), 'numpy.matmul', 'np.matmul', (['x3d', 'M3d'], {}), '(x3d, M3d)\n', (5096, 5106), True, 'import numpy as np\n'), ((301, 321), 'numpy.max', 'np.max', (['x'], {'axis': 'axis'}), '(x, axis=axis)\n', (307, 321), True, 'import numpy as np\n'), ((1324, 1340), 'numpy.maximum', 'np.maximum', (['(0)', 'x'], {}), '(0, x)\n', (1334, 1340), True, 'import numpy as np\n'), ((1429, 1439), 'numpy.tanh', 'np.tanh', (['x'], {}), '(x)\n', (1436, 1439), True, 'import numpy as np\n'), ((1731, 1740), 'numpy.abs', 'np.abs', (['x'], {}), '(x)\n', (1737, 1740), True, 'import numpy as np\n'), ((1769, 1786), 'numpy.cos', 'np.cos', (['(np.pi * x)'], {}), '(np.pi * x)\n', (1775, 1786), True, 'import numpy as np\n'), ((1814, 1831), 'numpy.sin', 'np.sin', (['(np.pi * x)'], {}), '(np.pi * x)\n', (1820, 1831), True, 'import numpy as np\n'), ((2559, 2576), 'numpy.isnan', 'np.isnan', (['act_vec'], {}), '(act_vec)\n', (2567, 2576), True, 'import numpy as np\n'), ((3854, 3901), 'sklearn.metrics.mean_squared_error', 'sklearn.metrics.mean_squared_error', (['y_true', 'raw'], {}), '(y_true, raw)\n', (3888, 3901), False, 'import sklearn\n'), ((4053, 4097), 'sklearn.metrics.accuracy_score', 'sklearn.metrics.accuracy_score', (['y_true', 'pred'], {}), '(y_true, pred)\n', (4083, 4097), False, 'import sklearn\n'), ((4245, 4292), 'sklearn.metrics.cohen_kappa_score', 'sklearn.metrics.cohen_kappa_score', (['y_true', 'pred'], {}), '(y_true, pred)\n', (4278, 4292), False, 'import sklearn\n'), ((1374, 1390), 'numpy.tanh', 'np.tanh', (['(x / 2.0)'], {}), '(x / 2.0)\n', (1381, 1390), True, 'import numpy as np\n'), ((3539, 3558), 'numpy.isfinite', 'np.isfinite', (['y_prob'], {}), '(y_prob)\n', (3550, 3558), True, 'import numpy as np\n'), ((1492, 1509), 'numpy.multiply', 'np.multiply', (['x', 'x'], {}), '(x, x)\n', (1503, 1509), True, 'import numpy as np\n'), ((3678, 3699), 'numpy.arange', 'np.arange', (['self.n_out'], {}), '(self.n_out)\n', (3687, 3699), True, 'import numpy as np\n')]
uktrade/tamato
common/tests/util.py
4ba2ffb25eea2887e4e081c81da7634cd7b4f9ca
import contextlib from datetime import date from datetime import datetime from datetime import timezone from functools import wraps from io import BytesIO from itertools import count from typing import Any from typing import Dict from typing import Sequence import pytest from dateutil.parser import parse as parse_date from dateutil.relativedelta import relativedelta from django import forms from django.core.exceptions import ValidationError from django.template.loader import render_to_string from django.urls import reverse from freezegun import freeze_time from lxml import etree from common.models.records import TrackedModel from common.renderers import counter_generator from common.serializers import validate_taric_xml_record_order from common.util import TaricDateRange from common.util import get_accessor from common.util import get_field_tuple INTERDEPENDENT_IMPORT_IMPLEMENTED = True UPDATE_IMPORTER_IMPLEMENTED = True EXPORT_REFUND_NOMENCLATURE_IMPLEMENTED = False COMMODITIES_IMPLEMENTED = True MEURSING_TABLES_IMPLEMENTED = False PARTIAL_TEMPORARY_STOP_IMPLEMENTED = False UTC = timezone.utc requires_commodities = pytest.mark.skipif( not COMMODITIES_IMPLEMENTED, reason="Commodities not implemented", ) requires_export_refund_nomenclature = pytest.mark.skipif( not EXPORT_REFUND_NOMENCLATURE_IMPLEMENTED, reason="Export refund nomenclature not implemented", ) requires_meursing_tables = pytest.mark.skipif( not MEURSING_TABLES_IMPLEMENTED, reason="Meursing tables not implemented", ) requires_partial_temporary_stop = pytest.mark.skipif( not PARTIAL_TEMPORARY_STOP_IMPLEMENTED, reason="Partial temporary stop not implemented", ) requires_interdependent_import = pytest.mark.skipif( not INTERDEPENDENT_IMPORT_IMPLEMENTED, reason="Interdependent imports not implemented", ) requires_update_importer = pytest.mark.skipif( not UPDATE_IMPORTER_IMPLEMENTED, reason="Requires Updating importers to be implemented", ) @contextlib.contextmanager def raises_if(exception, expected): try: yield except exception: if not expected: raise else: if expected: pytest.fail(f"Did not raise {exception}") def check_validator(validate, value, expected_valid): try: validate(value) except ValidationError: if expected_valid: pytest.fail(f'Unexpected validation error for value "{value}"') except Exception: raise else: if not expected_valid: pytest.fail(f'Expected validation error for value "{value}"') def make_duplicate_record(factory, identifying_fields=None): """Creates two records using the passed factory that are duplicates of each other and returns the record created last.""" existing = factory.create() # allow overriding identifying_fields if identifying_fields is None: identifying_fields = list(factory._meta.model.identifying_fields) return factory.create( **dict(get_field_tuple(existing, field) for field in identifying_fields) ) def make_non_duplicate_record(factory, identifying_fields=None): """Creates two records using the passed factory that are not duplicates of each other and returns the record created last.""" existing = factory.create() not_duplicate = factory.create() if identifying_fields is None: identifying_fields = list(factory._meta.model.identifying_fields) assert any( get_field_tuple(existing, f) != get_field_tuple(not_duplicate, f) for f in identifying_fields ) return not_duplicate def get_checkable_data(model: TrackedModel, ignore=frozenset()): """ Returns a dict representing the model's data ignoring any automatically set fields and fields with names passed to `ignore`. The returned data will contain the identifying fields for any linked models rather than internal PKs. For example: get_checkable_data(FootnoteDescriptionFactory(), ignore={"sid"}) # { # "description": "My sample footnote text", # "described_footnote": { # "footnote_type__footnote_type_id": "FN" # "footnote_id": "123", # }, # } """ checked_field_names = {f.name for f in model.copyable_fields} - ignore data = { name: getattr(model, get_accessor(model._meta.get_field(name))) for name in checked_field_names } identifying_fields = { name: data[name].get_identifying_fields() for name in checked_field_names if hasattr(data[name], "get_identifying_fields") } data.update(identifying_fields) return data def assert_records_match( expected: TrackedModel, imported: TrackedModel, ignore=frozenset(), ): """ Asserts that every value for every field in the imported model is the same as the data in the expected model. System fields that will change from model to model are not checked. Any field names given to `ignore` will also not be checked. """ expected_data = get_checkable_data(expected, ignore=ignore) imported_data = get_checkable_data(imported, ignore=ignore) assert expected_data == imported_data def assert_many_records_match( expected: Sequence[TrackedModel], imported: Sequence[TrackedModel], ignore=frozenset(), ): """ Asserts that every value for every field in the imported models is the same as the data in the expected models, and that the count of both is equal. System fields that will change from model to model are not checked. Any field names given to `ignore` will also not be checked. """ expected_data = [get_checkable_data(e, ignore=ignore) for e in expected] imported_data = [get_checkable_data(i, ignore=ignore) for i in imported] assert expected_data == imported_data _transaction_counter = count(start=1) def generate_test_import_xml(obj: dict) -> BytesIO: xml = render_to_string( template_name="workbaskets/taric/transaction_detail.xml", context={ "envelope_id": next(_transaction_counter), "tracked_models": [obj], "transaction_id": next(_transaction_counter), "message_counter": counter_generator(), "counter_generator": counter_generator, }, ) return BytesIO(xml.encode()) def taric_xml_record_codes(xml): """Yields tuples of (record_code, subrecord_code)""" records = xml.xpath(".//*[local-name() = 'record']") codes = etree.XPath( ".//*[local-name()='record.code' or local-name()='subrecord.code']/text()", ) return [tuple(codes(record)) for record in records] def validate_taric_xml( factory=None, instance=None, factory_kwargs=None, check_order=True, ): def decorator(func): def wraps( api_client, taric_schema, approved_transaction, valid_user, *args, **kwargs, ): if not factory and not instance: raise AssertionError( "Either a factory or an object instance need to be provided", ) if factory and instance: raise AssertionError( "Either a factory or an object instance need to be provided - not both.", ) current_instance = instance or factory.create( transaction=approved_transaction, **factory_kwargs or {} ) api_client.force_login(user=valid_user) response = api_client.get( reverse( "workbaskets:workbasket-detail", kwargs={"pk": approved_transaction.workbasket.pk}, ), {"format": "xml"}, ) assert response.status_code == 200 content = response.content xml = etree.XML(content) taric_schema.validate(xml) assert not taric_schema.error_log, f"XML errors: {taric_schema.error_log}" if check_order: validate_taric_xml_record_order(xml) kwargs = {"xml": xml, **kwargs} func( *args, **kwargs, ) return wraps return decorator class Dates: deltas = { "normal": (relativedelta(), relativedelta(months=+1)), "earlier": (relativedelta(years=-1), relativedelta(years=-1, months=+1)), "later": ( relativedelta(years=+1, months=+1, days=+1), relativedelta(years=+1, months=+2), ), "big": (relativedelta(years=-2), relativedelta(years=+2, days=+1)), "adjacent": (relativedelta(days=+1), relativedelta(months=+1)), "adjacent_earlier": (relativedelta(months=-1), relativedelta(days=-1)), "adjacent_later": (relativedelta(months=+1, days=+1), relativedelta(months=+2)), "adjacent_no_end": (relativedelta(months=+1, days=+1), None), "adjacent_even_later": ( relativedelta(months=+2, days=+1), relativedelta(months=+3), ), "adjacent_earlier_big": ( relativedelta(years=-2, months=-2), relativedelta(years=-2), ), "adjacent_later_big": ( relativedelta(months=+1, days=+1), relativedelta(years=+2, months=+2), ), "overlap_normal": ( relativedelta(days=+15), relativedelta(days=+14, months=+1, years=+1), ), "overlap_normal_earlier": ( relativedelta(months=-1, days=+14), relativedelta(days=+14), ), "overlap_normal_same_year": ( relativedelta(days=+15), relativedelta(days=+14, months=+1), ), "overlap_big": (relativedelta(years=+1), relativedelta(years=+3, days=+2)), "after_big": ( relativedelta(years=+3, months=+1), relativedelta(years=+3, months=+2), ), "backwards": (relativedelta(months=+1), relativedelta(days=+1)), "starts_with_normal": (relativedelta(), relativedelta(days=+14)), "ends_with_normal": (relativedelta(days=+14), relativedelta(months=+1)), "current": (relativedelta(weeks=-4), relativedelta(weeks=+4)), "future": (relativedelta(weeks=+10), relativedelta(weeks=+20)), "no_end": (relativedelta(), None), "normal_first_half": (relativedelta(), relativedelta(days=+14)), } @property def now(self): return self.datetime_now.date() @property def datetime_now(self): return datetime.now(tz=UTC).replace(hour=0, minute=0, second=0, microsecond=0) def __getattr__(self, name): if name in self.deltas: start, end = self.deltas[name] start = self.now + start if end is not None: end = self.now + end return TaricDateRange(start, end) raise AttributeError(name) @classmethod def short_before(cls, dt): return TaricDateRange( dt + relativedelta(months=-1), dt + relativedelta(days=-14), ) @classmethod def medium_before(cls, dt): return TaricDateRange( dt + relativedelta(months=-1), dt + relativedelta(days=-1), ) @classmethod def short_after(cls, dt): return TaricDateRange( dt + relativedelta(days=+14), dt + relativedelta(months=+1), ) @classmethod def short_overlap(cls, dt): return TaricDateRange( dt + relativedelta(months=-1), dt + relativedelta(months=+1), ) @classmethod def no_end_before(cls, dt): return TaricDateRange( dt + relativedelta(months=-1), None, ) def only_applicable_after(cutoff): """ Decorator which asserts that a test fails after a specified cutoff date. :param cutoff: A date string, or datetime object before which the test should fail. """ cutoff = parse_date(cutoff) def decorator(fn): @wraps(fn) def do_test(*args, **kwargs): # test should pass normally fn(*args, **kwargs) # test should fail before cutoff with freeze_time(cutoff + relativedelta(days=-1)): try: fn(*args, **kwargs) except pytest.fail.Exception: pass except Exception: raise else: pytest.fail(f"Rule applied before {cutoff:%Y-%m-%d}") return True return do_test return decorator def validity_period_post_data(start: date, end: date) -> Dict[str, int]: """ Construct a POST data fragment for the validity period start and end dates of a ValidityPeriodForm from the given date objects, eg: >>> validity_period_post_data( >>> datetime.date(2021, 1, 2), >>> datetime.date(2022, 3, 4), >>> ) { "start_date_0": 1, "start_date_1": 2, "start_date_2": 2021, "end_date_0": 4, "end_date_1": 3, "end_date_2": 2022, } """ return { f"{name}_{i}": part for name, date in (("start_date", start), ("end_date", end)) for i, part in enumerate([date.day, date.month, date.year]) } def get_form_data(form: forms.ModelForm) -> Dict[str, Any]: """Returns a dictionary of the fields that the form will put onto a page and their current values, taking account of any fields that have sub-fields and hence result in multiple HTML <input> objects.""" data = {**form.initial} for field in form.rendered_fields: value = data[field] if field in data else form.fields[field].initial if hasattr(form.fields[field].widget, "decompress"): # If the widget can be decompressed, then it is not just a simple # value and has some internal structure. So we need to generate one # form item per decompressed value and append the name with _0, _1, # etc. This mirrors the MultiValueWidget in django/forms/widgets.py. if field in data: del data[field] value = form.fields[field].widget.decompress(value) data.update( **{f"{field}_{i}": v for i, v in enumerate(value) if v is not None} ) elif value is not None: data.setdefault(field, value) return data
[((1138, 1228), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not COMMODITIES_IMPLEMENTED)'], {'reason': '"""Commodities not implemented"""'}), "(not COMMODITIES_IMPLEMENTED, reason=\n 'Commodities not implemented')\n", (1156, 1228), False, 'import pytest\n'), ((1274, 1394), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not EXPORT_REFUND_NOMENCLATURE_IMPLEMENTED)'], {'reason': '"""Export refund nomenclature not implemented"""'}), "(not EXPORT_REFUND_NOMENCLATURE_IMPLEMENTED, reason=\n 'Export refund nomenclature not implemented')\n", (1292, 1394), False, 'import pytest\n'), ((1429, 1527), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not MEURSING_TABLES_IMPLEMENTED)'], {'reason': '"""Meursing tables not implemented"""'}), "(not MEURSING_TABLES_IMPLEMENTED, reason=\n 'Meursing tables not implemented')\n", (1447, 1527), False, 'import pytest\n'), ((1569, 1681), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not PARTIAL_TEMPORARY_STOP_IMPLEMENTED)'], {'reason': '"""Partial temporary stop not implemented"""'}), "(not PARTIAL_TEMPORARY_STOP_IMPLEMENTED, reason=\n 'Partial temporary stop not implemented')\n", (1587, 1681), False, 'import pytest\n'), ((1722, 1833), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not INTERDEPENDENT_IMPORT_IMPLEMENTED)'], {'reason': '"""Interdependent imports not implemented"""'}), "(not INTERDEPENDENT_IMPORT_IMPLEMENTED, reason=\n 'Interdependent imports not implemented')\n", (1740, 1833), False, 'import pytest\n'), ((1868, 1980), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not UPDATE_IMPORTER_IMPLEMENTED)'], {'reason': '"""Requires Updating importers to be implemented"""'}), "(not UPDATE_IMPORTER_IMPLEMENTED, reason=\n 'Requires Updating importers to be implemented')\n", (1886, 1980), False, 'import pytest\n'), ((5923, 5937), 'itertools.count', 'count', ([], {'start': '(1)'}), '(start=1)\n', (5928, 5937), False, 'from itertools import count\n'), ((6571, 6663), 'lxml.etree.XPath', 'etree.XPath', (['""".//*[local-name()=\'record.code\' or local-name()=\'subrecord.code\']/text()"""'], {}), '(\n ".//*[local-name()=\'record.code\' or local-name()=\'subrecord.code\']/text()")\n', (6582, 6663), False, 'from lxml import etree\n'), ((12168, 12186), 'dateutil.parser.parse', 'parse_date', (['cutoff'], {}), '(cutoff)\n', (12178, 12186), True, 'from dateutil.parser import parse as parse_date\n'), ((12220, 12229), 'functools.wraps', 'wraps', (['fn'], {}), '(fn)\n', (12225, 12229), False, 'from functools import wraps\n'), ((2183, 2224), 'pytest.fail', 'pytest.fail', (['f"""Did not raise {exception}"""'], {}), "(f'Did not raise {exception}')\n", (2194, 2224), False, 'import pytest\n'), ((2534, 2595), 'pytest.fail', 'pytest.fail', (['f"""Expected validation error for value "{value}\\""""'], {}), '(f\'Expected validation error for value "{value}"\')\n', (2545, 2595), False, 'import pytest\n'), ((7981, 7999), 'lxml.etree.XML', 'etree.XML', (['content'], {}), '(content)\n', (7990, 7999), False, 'from lxml import etree\n'), ((8430, 8445), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {}), '()\n', (8443, 8445), False, 'from dateutil.relativedelta import relativedelta\n'), ((8447, 8471), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (8460, 8471), False, 'from dateutil.relativedelta import relativedelta\n'), ((8494, 8517), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(-1)'}), '(years=-1)\n', (8507, 8517), False, 'from dateutil.relativedelta import relativedelta\n'), ((8519, 8553), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(-1)', 'months': '(+1)'}), '(years=-1, months=+1)\n', (8532, 8553), False, 'from dateutil.relativedelta import relativedelta\n'), ((8587, 8630), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+1)', 'months': '(+1)', 'days': '(+1)'}), '(years=+1, months=+1, days=+1)\n', (8600, 8630), False, 'from dateutil.relativedelta import relativedelta\n'), ((8644, 8678), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+1)', 'months': '(+2)'}), '(years=+1, months=+2)\n', (8657, 8678), False, 'from dateutil.relativedelta import relativedelta\n'), ((8707, 8730), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(-2)'}), '(years=-2)\n', (8720, 8730), False, 'from dateutil.relativedelta import relativedelta\n'), ((8732, 8764), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+2)', 'days': '(+1)'}), '(years=+2, days=+1)\n', (8745, 8764), False, 'from dateutil.relativedelta import relativedelta\n'), ((8788, 8810), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+1)'}), '(days=+1)\n', (8801, 8810), False, 'from dateutil.relativedelta import relativedelta\n'), ((8812, 8836), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (8825, 8836), False, 'from dateutil.relativedelta import relativedelta\n'), ((8868, 8892), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)'}), '(months=-1)\n', (8881, 8892), False, 'from dateutil.relativedelta import relativedelta\n'), ((8894, 8916), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(-1)'}), '(days=-1)\n', (8907, 8916), False, 'from dateutil.relativedelta import relativedelta\n'), ((8946, 8979), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)', 'days': '(+1)'}), '(months=+1, days=+1)\n', (8959, 8979), False, 'from dateutil.relativedelta import relativedelta\n'), ((8981, 9005), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+2)'}), '(months=+2)\n', (8994, 9005), False, 'from dateutil.relativedelta import relativedelta\n'), ((9036, 9069), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)', 'days': '(+1)'}), '(months=+1, days=+1)\n', (9049, 9069), False, 'from dateutil.relativedelta import relativedelta\n'), ((9123, 9156), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+2)', 'days': '(+1)'}), '(months=+2, days=+1)\n', (9136, 9156), False, 'from dateutil.relativedelta import relativedelta\n'), ((9170, 9194), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+3)'}), '(months=+3)\n', (9183, 9194), False, 'from dateutil.relativedelta import relativedelta\n'), ((9253, 9287), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(-2)', 'months': '(-2)'}), '(years=-2, months=-2)\n', (9266, 9287), False, 'from dateutil.relativedelta import relativedelta\n'), ((9301, 9324), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(-2)'}), '(years=-2)\n', (9314, 9324), False, 'from dateutil.relativedelta import relativedelta\n'), ((9381, 9414), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)', 'days': '(+1)'}), '(months=+1, days=+1)\n', (9394, 9414), False, 'from dateutil.relativedelta import relativedelta\n'), ((9428, 9462), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+2)', 'months': '(+2)'}), '(years=+2, months=+2)\n', (9441, 9462), False, 'from dateutil.relativedelta import relativedelta\n'), ((9515, 9538), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+15)'}), '(days=+15)\n', (9528, 9538), False, 'from dateutil.relativedelta import relativedelta\n'), ((9552, 9596), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)', 'months': '(+1)', 'years': '(+1)'}), '(days=+14, months=+1, years=+1)\n', (9565, 9596), False, 'from dateutil.relativedelta import relativedelta\n'), ((9657, 9691), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)', 'days': '(+14)'}), '(months=-1, days=+14)\n', (9670, 9691), False, 'from dateutil.relativedelta import relativedelta\n'), ((9705, 9728), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)'}), '(days=+14)\n', (9718, 9728), False, 'from dateutil.relativedelta import relativedelta\n'), ((9791, 9814), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+15)'}), '(days=+15)\n', (9804, 9814), False, 'from dateutil.relativedelta import relativedelta\n'), ((9828, 9862), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)', 'months': '(+1)'}), '(days=+14, months=+1)\n', (9841, 9862), False, 'from dateutil.relativedelta import relativedelta\n'), ((9899, 9922), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+1)'}), '(years=+1)\n', (9912, 9922), False, 'from dateutil.relativedelta import relativedelta\n'), ((9924, 9956), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+3)', 'days': '(+2)'}), '(years=+3, days=+2)\n', (9937, 9956), False, 'from dateutil.relativedelta import relativedelta\n'), ((9994, 10028), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+3)', 'months': '(+1)'}), '(years=+3, months=+1)\n', (10007, 10028), False, 'from dateutil.relativedelta import relativedelta\n'), ((10042, 10076), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'years': '(+3)', 'months': '(+2)'}), '(years=+3, months=+2)\n', (10055, 10076), False, 'from dateutil.relativedelta import relativedelta\n'), ((10111, 10135), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (10124, 10135), False, 'from dateutil.relativedelta import relativedelta\n'), ((10137, 10159), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+1)'}), '(days=+1)\n', (10150, 10159), False, 'from dateutil.relativedelta import relativedelta\n'), ((10193, 10208), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {}), '()\n', (10206, 10208), False, 'from dateutil.relativedelta import relativedelta\n'), ((10210, 10233), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)'}), '(days=+14)\n', (10223, 10233), False, 'from dateutil.relativedelta import relativedelta\n'), ((10265, 10288), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)'}), '(days=+14)\n', (10278, 10288), False, 'from dateutil.relativedelta import relativedelta\n'), ((10290, 10314), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (10303, 10314), False, 'from dateutil.relativedelta import relativedelta\n'), ((10337, 10360), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'weeks': '(-4)'}), '(weeks=-4)\n', (10350, 10360), False, 'from dateutil.relativedelta import relativedelta\n'), ((10362, 10385), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'weeks': '(+4)'}), '(weeks=+4)\n', (10375, 10385), False, 'from dateutil.relativedelta import relativedelta\n'), ((10407, 10431), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'weeks': '(+10)'}), '(weeks=+10)\n', (10420, 10431), False, 'from dateutil.relativedelta import relativedelta\n'), ((10433, 10457), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'weeks': '(+20)'}), '(weeks=+20)\n', (10446, 10457), False, 'from dateutil.relativedelta import relativedelta\n'), ((10479, 10494), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {}), '()\n', (10492, 10494), False, 'from dateutil.relativedelta import relativedelta\n'), ((10533, 10548), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {}), '()\n', (10546, 10548), False, 'from dateutil.relativedelta import relativedelta\n'), ((10550, 10573), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)'}), '(days=+14)\n', (10563, 10573), False, 'from dateutil.relativedelta import relativedelta\n'), ((11020, 11046), 'common.util.TaricDateRange', 'TaricDateRange', (['start', 'end'], {}), '(start, end)\n', (11034, 11046), False, 'from common.util import TaricDateRange\n'), ((2381, 2444), 'pytest.fail', 'pytest.fail', (['f"""Unexpected validation error for value "{value}\\""""'], {}), '(f\'Unexpected validation error for value "{value}"\')\n', (2392, 2444), False, 'import pytest\n'), ((3493, 3521), 'common.util.get_field_tuple', 'get_field_tuple', (['existing', 'f'], {}), '(existing, f)\n', (3508, 3521), False, 'from common.util import get_field_tuple\n'), ((3525, 3558), 'common.util.get_field_tuple', 'get_field_tuple', (['not_duplicate', 'f'], {}), '(not_duplicate, f)\n', (3540, 3558), False, 'from common.util import get_field_tuple\n'), ((6286, 6305), 'common.renderers.counter_generator', 'counter_generator', ([], {}), '()\n', (6303, 6305), False, 'from common.renderers import counter_generator\n'), ((7673, 7769), 'django.urls.reverse', 'reverse', (['"""workbaskets:workbasket-detail"""'], {'kwargs': "{'pk': approved_transaction.workbasket.pk}"}), "('workbaskets:workbasket-detail', kwargs={'pk': approved_transaction\n .workbasket.pk})\n", (7680, 7769), False, 'from django.urls import reverse\n'), ((8173, 8209), 'common.serializers.validate_taric_xml_record_order', 'validate_taric_xml_record_order', (['xml'], {}), '(xml)\n', (8204, 8209), False, 'from common.serializers import validate_taric_xml_record_order\n'), ((10714, 10734), 'datetime.datetime.now', 'datetime.now', ([], {'tz': 'UTC'}), '(tz=UTC)\n', (10726, 10734), False, 'from datetime import datetime\n'), ((11179, 11203), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)'}), '(months=-1)\n', (11192, 11203), False, 'from dateutil.relativedelta import relativedelta\n'), ((11222, 11245), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(-14)'}), '(days=-14)\n', (11235, 11245), False, 'from dateutil.relativedelta import relativedelta\n'), ((11355, 11379), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)'}), '(months=-1)\n', (11368, 11379), False, 'from dateutil.relativedelta import relativedelta\n'), ((11398, 11420), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(-1)'}), '(days=-1)\n', (11411, 11420), False, 'from dateutil.relativedelta import relativedelta\n'), ((11528, 11551), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(+14)'}), '(days=+14)\n', (11541, 11551), False, 'from dateutil.relativedelta import relativedelta\n'), ((11570, 11594), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (11583, 11594), False, 'from dateutil.relativedelta import relativedelta\n'), ((11704, 11728), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)'}), '(months=-1)\n', (11717, 11728), False, 'from dateutil.relativedelta import relativedelta\n'), ((11747, 11771), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(+1)'}), '(months=+1)\n', (11760, 11771), False, 'from dateutil.relativedelta import relativedelta\n'), ((11881, 11905), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': '(-1)'}), '(months=-1)\n', (11894, 11905), False, 'from dateutil.relativedelta import relativedelta\n'), ((3016, 3048), 'common.util.get_field_tuple', 'get_field_tuple', (['existing', 'field'], {}), '(existing, field)\n', (3031, 3048), False, 'from common.util import get_field_tuple\n'), ((12686, 12739), 'pytest.fail', 'pytest.fail', (['f"""Rule applied before {cutoff:%Y-%m-%d}"""'], {}), "(f'Rule applied before {cutoff:%Y-%m-%d}')\n", (12697, 12739), False, 'import pytest\n'), ((12424, 12446), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'days': '(-1)'}), '(days=-1)\n', (12437, 12446), False, 'from dateutil.relativedelta import relativedelta\n')]
Leeo1124/pythonDemo
src/com/python/email/send_mail.py
72e2209c095301a3f1f61edfe03ea69c3c05be40
''' Created on 2016年8月10日 @author: Administrator ''' from email import encoders from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEBase from email.utils import parseaddr, formataddr import smtplib def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr)) from_addr = 'leeo1124@163.com'#input('From: ') password = input('Password: ') to_addr = '450475851@qq.com'#input('To: ') smtp_server = 'smtp.163.com'#input('SMTP server: ') # 发送纯文本邮件 # msg = MIMEText('hello, send by Python...', 'plain', 'utf-8') # 发送HTML邮件 # msg = MIMEText('<html><body><h1>Hello</h1>' + # '<p>send by <a href="http://www.python.org">Python</a>...</p>' + # '</body></html>', 'html', 'utf-8') # 发送带附件的邮件 # 邮件对象: msg = MIMEMultipart() msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr) msg['To'] = _format_addr('管理员 <%s>' % to_addr) msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode() # 邮件正文是MIMEText: msg.attach(MIMEText('send with file...', 'plain', 'utf-8')) # 添加附件就是加上一个MIMEBase,从本地读取一个图片: with open('D:/pythonWorkspace/pthonDemo/src/com/python/email/test.jpg', 'rb') as f: # 设置附件的MIME和文件名,这里是png类型: mime = MIMEBase('image', 'png', filename='test.png') # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.png') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 把附件的内容读进来: mime.set_payload(f.read()) # 用Base64编码: encoders.encode_base64(mime) # 添加到MIMEMultipart: msg.attach(mime) msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr) msg['To'] = _format_addr('管理员 <%s>' % to_addr) msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode() server = smtplib.SMTP(smtp_server, 25) server.set_debuglevel(1) server.login(from_addr, password) server.sendmail(from_addr, [to_addr], msg.as_string()) server.quit()
[((864, 879), 'email.mime.multipart.MIMEMultipart', 'MIMEMultipart', ([], {}), '()\n', (877, 879), False, 'from email.mime.multipart import MIMEMultipart\n'), ((1820, 1849), 'smtplib.SMTP', 'smtplib.SMTP', (['smtp_server', '(25)'], {}), '(smtp_server, 25)\n', (1832, 1849), False, 'import smtplib\n'), ((339, 351), 'email.utils.parseaddr', 'parseaddr', (['s'], {}), '(s)\n', (348, 351), False, 'from email.utils import parseaddr, formataddr\n'), ((1071, 1118), 'email.mime.text.MIMEText', 'MIMEText', (['"""send with file..."""', '"""plain"""', '"""utf-8"""'], {}), "('send with file...', 'plain', 'utf-8')\n", (1079, 1118), False, 'from email.mime.text import MIMEText\n'), ((1279, 1324), 'email.mime.multipart.MIMEBase', 'MIMEBase', (['"""image"""', '"""png"""'], {'filename': '"""test.png"""'}), "('image', 'png', filename='test.png')\n", (1287, 1324), False, 'from email.mime.multipart import MIMEBase\n'), ((1573, 1601), 'email.encoders.encode_base64', 'encoders.encode_base64', (['mime'], {}), '(mime)\n', (1595, 1601), False, 'from email import encoders\n'), ((1001, 1031), 'email.header.Header', 'Header', (['"""来自SMTP的问候……"""', '"""utf-8"""'], {}), "('来自SMTP的问候……', 'utf-8')\n", (1007, 1031), False, 'from email.header import Header\n'), ((1770, 1800), 'email.header.Header', 'Header', (['"""来自SMTP的问候……"""', '"""utf-8"""'], {}), "('来自SMTP的问候……', 'utf-8')\n", (1776, 1800), False, 'from email.header import Header\n'), ((375, 396), 'email.header.Header', 'Header', (['name', '"""utf-8"""'], {}), "(name, 'utf-8')\n", (381, 396), False, 'from email.header import Header\n')]
aquilesC/aquicarattino
aqui_carattino/blog/migrations/0002_auto_20200424_1452.py
b6d873aea6e3ec9d1b802ea13952746e4fcc22b6
# Generated by Django 3.0.5 on 2020-04-24 12:52 from django.db import migrations, models import django.db.models.deletion import modelcluster.contrib.taggit import modelcluster.fields import wagtail.core.blocks import wagtail.core.fields import wagtail.images.blocks class Migration(migrations.Migration): dependencies = [ ('wagtailimages', '0001_squashed_0021'), ('wagtailcore', '0045_assign_unlock_grouppagepermission'), ('taggit', '0003_taggeditem_add_unique_index'), ('blog', '0001_initial'), ] operations = [ migrations.CreateModel( name='ArticlePage', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('body', wagtail.core.fields.StreamField([('text', wagtail.core.blocks.RichTextBlock(icon='fa fa-paragraph', template='blocks/paragraph_block.html')), ('alert_block', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.RichTextBlock(required=True))], required=False)), ('button_block', wagtail.core.blocks.StructBlock([('url', wagtail.core.blocks.URLBlock(required=True)), ('text', wagtail.core.blocks.CharBlock(required=True))], required=False)), ('carousel_block', wagtail.core.blocks.StructBlock([('image_items', wagtail.core.blocks.ListBlock(wagtail.images.blocks.ImageChooserBlock(required=True), label='Image Item'))])), ('testimonial', wagtail.core.blocks.StructBlock([('text', wagtail.core.blocks.TextBlock(required=True)), ('author', wagtail.core.blocks.CharBlock(label='Who said this?', required=True)), ('author_title', wagtail.core.blocks.CharBlock(label='Role of this person', required=False)), ('image', wagtail.images.blocks.ImageChooserBlock(required=False)), ('link', wagtail.core.blocks.URLBlock(label='Link to the person website', required=False))])), ('code_block', wagtail.core.blocks.StructBlock([('language', wagtail.core.blocks.CharBlock(help_text='In which language this is written', max_length=20, required=True)), ('code', wagtail.core.blocks.TextBlock(help_text='The actual code', required=True))]))])), ('header_image', models.ForeignKey(blank=True, help_text='Image to appear at the top of the page, before author information', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], options={ 'abstract': False, }, bases=('wagtailcore.page',), ), migrations.CreateModel( name='Sidebar', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('title', models.CharField(max_length=50)), ('text', models.TextField()), ('cta', models.CharField(blank=True, max_length=15, null=True, verbose_name='Call to action text')), ('image', models.ForeignKey(blank=True, help_text='Image to appear at the top of the page, before author information', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), ], ), migrations.CreateModel( name='BlogPageTag', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('content_object', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='tagged_items', to='blog.ArticlePage')), ('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_blogpagetag_items', to='taggit.Tag')), ], options={ 'abstract': False, }, ), migrations.AddField( model_name='articlepage', name='tags', field=modelcluster.contrib.taggit.ClusterTaggableManager(blank=True, help_text='A comma-separated list of tags.', through='blog.BlogPageTag', to='taggit.Tag', verbose_name='Tags'), ), ]
[((677, 847), 'django.db.models.OneToOneField', 'models.OneToOneField', ([], {'auto_created': '(True)', 'on_delete': 'django.db.models.deletion.CASCADE', 'parent_link': '(True)', 'primary_key': '(True)', 'serialize': '(False)', 'to': '"""wagtailcore.Page"""'}), "(auto_created=True, on_delete=django.db.models.deletion\n .CASCADE, parent_link=True, primary_key=True, serialize=False, to=\n 'wagtailcore.Page')\n", (697, 847), False, 'from django.db import migrations, models\n'), ((2231, 2454), 'django.db.models.ForeignKey', 'models.ForeignKey', ([], {'blank': '(True)', 'help_text': '"""Image to appear at the top of the page, before author information"""', 'null': '(True)', 'on_delete': 'django.db.models.deletion.SET_NULL', 'related_name': '"""+"""', 'to': '"""wagtailimages.Image"""'}), "(blank=True, help_text=\n 'Image to appear at the top of the page, before author information',\n null=True, on_delete=django.db.models.deletion.SET_NULL, related_name=\n '+', to='wagtailimages.Image')\n", (2248, 2454), False, 'from django.db import migrations, models\n'), ((2686, 2779), 'django.db.models.AutoField', 'models.AutoField', ([], {'auto_created': '(True)', 'primary_key': '(True)', 'serialize': '(False)', 'verbose_name': '"""ID"""'}), "(auto_created=True, primary_key=True, serialize=False,\n verbose_name='ID')\n", (2702, 2779), False, 'from django.db import migrations, models\n'), ((2804, 2835), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (2820, 2835), False, 'from django.db import migrations, models\n'), ((2863, 2881), 'django.db.models.TextField', 'models.TextField', ([], {}), '()\n', (2879, 2881), False, 'from django.db import migrations, models\n'), ((2908, 3003), 'django.db.models.CharField', 'models.CharField', ([], {'blank': '(True)', 'max_length': '(15)', 'null': '(True)', 'verbose_name': '"""Call to action text"""'}), "(blank=True, max_length=15, null=True, verbose_name=\n 'Call to action text')\n", (2924, 3003), False, 'from django.db import migrations, models\n'), ((3027, 3250), 'django.db.models.ForeignKey', 'models.ForeignKey', ([], {'blank': '(True)', 'help_text': '"""Image to appear at the top of the page, before author information"""', 'null': '(True)', 'on_delete': 'django.db.models.deletion.SET_NULL', 'related_name': '"""+"""', 'to': '"""wagtailimages.Image"""'}), "(blank=True, help_text=\n 'Image to appear at the top of the page, before author information',\n null=True, on_delete=django.db.models.deletion.SET_NULL, related_name=\n '+', to='wagtailimages.Image')\n", (3044, 3250), False, 'from django.db import migrations, models\n'), ((3373, 3466), 'django.db.models.AutoField', 'models.AutoField', ([], {'auto_created': '(True)', 'primary_key': '(True)', 'serialize': '(False)', 'verbose_name': '"""ID"""'}), "(auto_created=True, primary_key=True, serialize=False,\n verbose_name='ID')\n", (3389, 3466), False, 'from django.db import migrations, models\n'), ((3655, 3778), 'django.db.models.ForeignKey', 'models.ForeignKey', ([], {'on_delete': 'django.db.models.deletion.CASCADE', 'related_name': '"""blog_blogpagetag_items"""', 'to': '"""taggit.Tag"""'}), "(on_delete=django.db.models.deletion.CASCADE, related_name\n ='blog_blogpagetag_items', to='taggit.Tag')\n", (3672, 3778), False, 'from django.db import migrations, models\n')]
openforcefield/nistdataselection
studies/mixture_feasibility/parsley_benchmark/alcohol_ester/run.py
d797d597f4ff528a7219d58daa8ef6508d438b24
from evaluator import unit from evaluator.backends import QueueWorkerResources from evaluator.backends.dask import DaskLSFBackend from evaluator.client import ConnectionOptions, EvaluatorClient from evaluator.datasets import PhysicalPropertyDataSet from evaluator.forcefield import SmirnoffForceFieldSource from evaluator.server import EvaluatorServer from evaluator.utils import setup_timestamp_logging def main(): setup_timestamp_logging() # Load in the force field force_field_path = "openff-1.0.0.offxml" force_field_source = SmirnoffForceFieldSource.from_path(force_field_path) # Load in the test set. data_set = PhysicalPropertyDataSet.from_json("full_set.json") # Set up a server object to run the calculations using. working_directory = "working_directory" # Set up a backend to run the calculations on. This assume running # on a HPC resources with the LSF queue system installed. queue_resources = QueueWorkerResources( number_of_threads=1, number_of_gpus=1, preferred_gpu_toolkit=QueueWorkerResources.GPUToolkit.CUDA, per_thread_memory_limit=5 * unit.gigabyte, wallclock_time_limit="05:59", ) worker_script_commands = ["conda activate forcebalance", "module load cuda/10.1"] calculation_backend = DaskLSFBackend( minimum_number_of_workers=1, maximum_number_of_workers=50, resources_per_worker=queue_resources, queue_name="gpuqueue", setup_script_commands=worker_script_commands, adaptive_interval="1000ms", ) with calculation_backend: server = EvaluatorServer( calculation_backend=calculation_backend, working_directory=working_directory, port=8004, ) with server: # Request the estimates. client = EvaluatorClient(ConnectionOptions(server_port=8004)) request, _ = client.request_estimate( property_set=data_set, force_field_source=force_field_source, ) # Wait for the results. results, _ = request.results(True, 5) results.json(f"results.json") if __name__ == "__main__": main()
[((423, 448), 'evaluator.utils.setup_timestamp_logging', 'setup_timestamp_logging', ([], {}), '()\n', (446, 448), False, 'from evaluator.utils import setup_timestamp_logging\n'), ((550, 602), 'evaluator.forcefield.SmirnoffForceFieldSource.from_path', 'SmirnoffForceFieldSource.from_path', (['force_field_path'], {}), '(force_field_path)\n', (584, 602), False, 'from evaluator.forcefield import SmirnoffForceFieldSource\n'), ((647, 697), 'evaluator.datasets.PhysicalPropertyDataSet.from_json', 'PhysicalPropertyDataSet.from_json', (['"""full_set.json"""'], {}), "('full_set.json')\n", (680, 697), False, 'from evaluator.datasets import PhysicalPropertyDataSet\n'), ((959, 1159), 'evaluator.backends.QueueWorkerResources', 'QueueWorkerResources', ([], {'number_of_threads': '(1)', 'number_of_gpus': '(1)', 'preferred_gpu_toolkit': 'QueueWorkerResources.GPUToolkit.CUDA', 'per_thread_memory_limit': '(5 * unit.gigabyte)', 'wallclock_time_limit': '"""05:59"""'}), "(number_of_threads=1, number_of_gpus=1,\n preferred_gpu_toolkit=QueueWorkerResources.GPUToolkit.CUDA,\n per_thread_memory_limit=5 * unit.gigabyte, wallclock_time_limit='05:59')\n", (979, 1159), False, 'from evaluator.backends import QueueWorkerResources\n'), ((1313, 1529), 'evaluator.backends.dask.DaskLSFBackend', 'DaskLSFBackend', ([], {'minimum_number_of_workers': '(1)', 'maximum_number_of_workers': '(50)', 'resources_per_worker': 'queue_resources', 'queue_name': '"""gpuqueue"""', 'setup_script_commands': 'worker_script_commands', 'adaptive_interval': '"""1000ms"""'}), "(minimum_number_of_workers=1, maximum_number_of_workers=50,\n resources_per_worker=queue_resources, queue_name='gpuqueue',\n setup_script_commands=worker_script_commands, adaptive_interval='1000ms')\n", (1327, 1529), False, 'from evaluator.backends.dask import DaskLSFBackend\n'), ((1626, 1735), 'evaluator.server.EvaluatorServer', 'EvaluatorServer', ([], {'calculation_backend': 'calculation_backend', 'working_directory': 'working_directory', 'port': '(8004)'}), '(calculation_backend=calculation_backend, working_directory=\n working_directory, port=8004)\n', (1641, 1735), False, 'from evaluator.server import EvaluatorServer\n'), ((1875, 1910), 'evaluator.client.ConnectionOptions', 'ConnectionOptions', ([], {'server_port': '(8004)'}), '(server_port=8004)\n', (1892, 1910), False, 'from evaluator.client import ConnectionOptions, EvaluatorClient\n')]
motional/nuplan-devkit
nuplan/planning/simulation/observation/idm/test/test_profile_idm_observation.py
e39029e788b17f47f2fcadb774098ef8fbdd0d67
import logging import unittest from pyinstrument import Profiler from nuplan.planning.scenario_builder.nuplan_db.test.nuplan_scenario_test_utils import get_test_nuplan_scenario from nuplan.planning.simulation.history.simulation_history_buffer import SimulationHistoryBuffer from nuplan.planning.simulation.observation.idm_agents import IDMAgents from nuplan.planning.simulation.simulation_time_controller.simulation_iteration import SimulationIteration logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) class TestProfileIDM(unittest.TestCase): """ Profiling test for IDM agents. """ def setUp(self) -> None: """ Inherited, see super class. """ self.n_repeat_trials = 1 self.display_results = True self.scenario = get_test_nuplan_scenario() def test_profile_idm_agent_observation(self) -> None: """Profile IDMAgents.""" profiler = Profiler(interval=0.0001) profiler.start() # How many times to repeat runtime test for _ in range(self.n_repeat_trials): observation = IDMAgents( target_velocity=10, min_gap_to_lead_agent=0.5, headway_time=1.5, accel_max=1.0, decel_max=2.0, scenario=self.scenario, ) for step in range(self.scenario.get_number_of_iterations() - 1): iteration = SimulationIteration(time_point=self.scenario.get_time_point(step), index=step) next_iteration = SimulationIteration(time_point=self.scenario.get_time_point(step + 1), index=step + 1) buffer = SimulationHistoryBuffer.initialize_from_list( 1, [self.scenario.get_ego_state_at_iteration(step)], [self.scenario.get_tracked_objects_at_iteration(step)], next_iteration.time_point.time_s - iteration.time_point.time_s, ) observation.update_observation(iteration, next_iteration, buffer) profiler.stop() if self.display_results: logger.info(profiler.output_text(unicode=True, color=True)) if __name__ == "__main__": unittest.main()
[((465, 492), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (482, 492), False, 'import logging\n'), ((493, 532), 'logging.basicConfig', 'logging.basicConfig', ([], {'level': 'logging.INFO'}), '(level=logging.INFO)\n', (512, 532), False, 'import logging\n'), ((2252, 2267), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2265, 2267), False, 'import unittest\n'), ((810, 836), 'nuplan.planning.scenario_builder.nuplan_db.test.nuplan_scenario_test_utils.get_test_nuplan_scenario', 'get_test_nuplan_scenario', ([], {}), '()\n', (834, 836), False, 'from nuplan.planning.scenario_builder.nuplan_db.test.nuplan_scenario_test_utils import get_test_nuplan_scenario\n'), ((948, 973), 'pyinstrument.Profiler', 'Profiler', ([], {'interval': '(0.0001)'}), '(interval=0.0001)\n', (956, 973), False, 'from pyinstrument import Profiler\n'), ((1120, 1252), 'nuplan.planning.simulation.observation.idm_agents.IDMAgents', 'IDMAgents', ([], {'target_velocity': '(10)', 'min_gap_to_lead_agent': '(0.5)', 'headway_time': '(1.5)', 'accel_max': '(1.0)', 'decel_max': '(2.0)', 'scenario': 'self.scenario'}), '(target_velocity=10, min_gap_to_lead_agent=0.5, headway_time=1.5,\n accel_max=1.0, decel_max=2.0, scenario=self.scenario)\n', (1129, 1252), False, 'from nuplan.planning.simulation.observation.idm_agents import IDMAgents\n')]
RDaneelOlivav/baselines
baselines/ddpg/ddpg.py
fea6ba932055bb76d68b4b22e812bab738fc18f8
import os import os.path as osp import time from collections import deque import pickle from baselines.ddpg.ddpg_learner import DDPG from baselines.ddpg.models import Actor, Critic from baselines.ddpg.memory import Memory from baselines.ddpg.noise import AdaptiveParamNoiseSpec, NormalActionNoise, OrnsteinUhlenbeckActionNoise from baselines.common import set_global_seeds from baselines import logger import tensorflow as tf import numpy as np try: from mpi4py import MPI except ImportError: MPI = None def learn(network, env, seed=None, total_timesteps=None, nb_epochs=None, # with default settings, perform 1M steps total nb_epoch_cycles=20, nb_rollout_steps=100, reward_scale=1.0, render=False, render_eval=False, noise_type='adaptive-param_0.2', normalize_returns=False, normalize_observations=True, critic_l2_reg=1e-2, actor_lr=1e-4, critic_lr=1e-3, popart=False, gamma=0.99, clip_norm=None, nb_train_steps=50, # per epoch cycle and MPI worker, nb_eval_steps=100, batch_size=64, # per MPI worker tau=0.01, eval_env=None, param_noise_adaption_interval=50, load_path=None, **network_kwargs): set_global_seeds(seed) if total_timesteps is not None: assert nb_epochs is None nb_epochs = int(total_timesteps) // (nb_epoch_cycles * nb_rollout_steps) else: nb_epochs = 500 if MPI is not None: rank = MPI.COMM_WORLD.Get_rank() else: rank = 0 nb_actions = env.action_space.shape[-1] assert (np.abs(env.action_space.low) == env.action_space.high).all() # we assume symmetric actions. memory = Memory(limit=int(1e6), action_shape=env.action_space.shape, observation_shape=env.observation_space.shape) critic = Critic(nb_actions, ob_shape=env.observation_space.shape, network=network, **network_kwargs) actor = Actor(nb_actions, ob_shape=env.observation_space.shape, network=network, **network_kwargs) action_noise = None param_noise = None if noise_type is not None: for current_noise_type in noise_type.split(','): current_noise_type = current_noise_type.strip() if current_noise_type == 'none': pass elif 'adaptive-param' in current_noise_type: _, stddev = current_noise_type.split('_') param_noise = AdaptiveParamNoiseSpec(initial_stddev=float(stddev), desired_action_stddev=float(stddev)) elif 'normal' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = NormalActionNoise(mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) elif 'ou' in current_noise_type: _, stddev = current_noise_type.split('_') action_noise = OrnsteinUhlenbeckActionNoise(mu=np.zeros(nb_actions), sigma=float(stddev) * np.ones(nb_actions)) else: raise RuntimeError('unknown noise type "{}"'.format(current_noise_type)) max_action = env.action_space.high logger.info('scaling actions by {} before executing in env'.format(max_action)) agent = DDPG(actor, critic, memory, env.observation_space.shape, env.action_space.shape, gamma=gamma, tau=tau, normalize_returns=normalize_returns, normalize_observations=normalize_observations, batch_size=batch_size, action_noise=action_noise, param_noise=param_noise, critic_l2_reg=critic_l2_reg, actor_lr=actor_lr, critic_lr=critic_lr, enable_popart=popart, clip_norm=clip_norm, reward_scale=reward_scale) logger.info('Using agent with the following configuration:') logger.info(str(agent.__dict__.items())) if load_path is not None: load_path = osp.expanduser(load_path) ckpt = tf.train.Checkpoint(model=agent) manager = tf.train.CheckpointManager(ckpt, load_path, max_to_keep=None) ckpt.restore(manager.latest_checkpoint) print("Restoring from {}".format(manager.latest_checkpoint)) eval_episode_rewards_history = deque(maxlen=100) episode_rewards_history = deque(maxlen=100) # Prepare everything. agent.initialize() agent.reset() obs = env.reset() if eval_env is not None: eval_obs = eval_env.reset() nenvs = obs.shape[0] episode_reward = np.zeros(nenvs, dtype = np.float32) #vector episode_step = np.zeros(nenvs, dtype = int) # vector episodes = 0 #scalar t = 0 # scalar epoch = 0 start_time = time.time() epoch_episode_rewards = [] epoch_episode_steps = [] epoch_actions = [] epoch_qs = [] epoch_episodes = 0 for epoch in range(nb_epochs): for cycle in range(nb_epoch_cycles): # Perform rollouts. if nenvs > 1: # if simulating multiple envs in parallel, impossible to reset agent at the end of the episode in each # of the environments, so resetting here instead agent.reset() for t_rollout in range(nb_rollout_steps): # Predict next action. action, q, _, _ = agent.step(tf.constant(obs), apply_noise=True, compute_Q=True) action, q = action.numpy(), q.numpy() # Execute next action. if rank == 0 and render: env.render() # max_action is of dimension A, whereas action is dimension (nenvs, A) - the multiplication gets broadcasted to the batch new_obs, r, done, info = env.step(max_action * action) # scale for execution in env (as far as DDPG is concerned, every action is in [-1, 1]) # note these outputs are batched from vecenv t += 1 if rank == 0 and render: env.render() episode_reward += r episode_step += 1 # Book-keeping. epoch_actions.append(action) epoch_qs.append(q) agent.store_transition(obs, action, r, new_obs, done) #the batched data will be unrolled in memory.py's append. obs = new_obs for d in range(len(done)): if done[d]: # Episode done. epoch_episode_rewards.append(episode_reward[d]) episode_rewards_history.append(episode_reward[d]) epoch_episode_steps.append(episode_step[d]) episode_reward[d] = 0. episode_step[d] = 0 epoch_episodes += 1 episodes += 1 if nenvs == 1: agent.reset() # Train. epoch_actor_losses = [] epoch_critic_losses = [] epoch_adaptive_distances = [] for t_train in range(nb_train_steps): # Adapt param noise, if necessary. if memory.nb_entries >= batch_size and t_train % param_noise_adaption_interval == 0: batch = agent.memory.sample(batch_size=batch_size) obs0 = tf.constant(batch['obs0']) distance = agent.adapt_param_noise(obs0) epoch_adaptive_distances.append(distance) cl, al = agent.train() epoch_critic_losses.append(cl) epoch_actor_losses.append(al) agent.update_target_net() # Evaluate. eval_episode_rewards = [] eval_qs = [] if eval_env is not None: nenvs_eval = eval_obs.shape[0] eval_episode_reward = np.zeros(nenvs_eval, dtype = np.float32) for t_rollout in range(nb_eval_steps): eval_action, eval_q, _, _ = agent.step(eval_obs, apply_noise=False, compute_Q=True) eval_obs, eval_r, eval_done, eval_info = eval_env.step(max_action * eval_action) # scale for execution in env (as far as DDPG is concerned, every action is in [-1, 1]) if render_eval: eval_env.render() eval_episode_reward += eval_r eval_qs.append(eval_q) for d in range(len(eval_done)): if eval_done[d]: eval_episode_rewards.append(eval_episode_reward[d]) eval_episode_rewards_history.append(eval_episode_reward[d]) eval_episode_reward[d] = 0.0 if MPI is not None: mpi_size = MPI.COMM_WORLD.Get_size() else: mpi_size = 1 # Log stats. # XXX shouldn't call np.mean on variable length lists duration = time.time() - start_time stats = agent.get_stats() combined_stats = stats.copy() combined_stats['rollout/return'] = np.mean(epoch_episode_rewards) combined_stats['rollout/return_std'] = np.std(epoch_episode_rewards) combined_stats['rollout/return_history'] = np.mean(episode_rewards_history) combined_stats['rollout/return_history_std'] = np.std(episode_rewards_history) combined_stats['rollout/episode_steps'] = np.mean(epoch_episode_steps) combined_stats['rollout/actions_mean'] = np.mean(epoch_actions) combined_stats['rollout/Q_mean'] = np.mean(epoch_qs) combined_stats['train/loss_actor'] = np.mean(epoch_actor_losses) combined_stats['train/loss_critic'] = np.mean(epoch_critic_losses) combined_stats['train/param_noise_distance'] = np.mean(epoch_adaptive_distances) combined_stats['total/duration'] = duration combined_stats['total/steps_per_second'] = float(t) / float(duration) combined_stats['total/episodes'] = episodes combined_stats['rollout/episodes'] = epoch_episodes combined_stats['rollout/actions_std'] = np.std(epoch_actions) # Evaluation statistics. if eval_env is not None: combined_stats['eval/return'] = eval_episode_rewards combined_stats['eval/return_history'] = np.mean(eval_episode_rewards_history) combined_stats['eval/Q'] = eval_qs combined_stats['eval/episodes'] = len(eval_episode_rewards) def as_scalar(x): if isinstance(x, np.ndarray): assert x.size == 1 return x[0] elif np.isscalar(x): return x else: raise ValueError('expected scalar, got %s'%x) combined_stats_sums = np.array([ np.array(x).flatten()[0] for x in combined_stats.values()]) if MPI is not None: combined_stats_sums = MPI.COMM_WORLD.allreduce(combined_stats_sums) combined_stats = {k : v / mpi_size for (k,v) in zip(combined_stats.keys(), combined_stats_sums)} # Total statistics. combined_stats['total/epochs'] = epoch + 1 combined_stats['total/steps'] = t for key in sorted(combined_stats.keys()): logger.record_tabular(key, combined_stats[key]) if rank == 0: logger.dump_tabular() logger.info('') logdir = logger.get_dir() if rank == 0 and logdir: if hasattr(env, 'get_state'): with open(os.path.join(logdir, 'env_state.pkl'), 'wb') as f: pickle.dump(env.get_state(), f) if eval_env and hasattr(eval_env, 'get_state'): with open(os.path.join(logdir, 'eval_env_state.pkl'), 'wb') as f: pickle.dump(eval_env.get_state(), f) return agent
[((1363, 1385), 'baselines.common.set_global_seeds', 'set_global_seeds', (['seed'], {}), '(seed)\n', (1379, 1385), False, 'from baselines.common import set_global_seeds\n'), ((1948, 2043), 'baselines.ddpg.models.Critic', 'Critic', (['nb_actions'], {'ob_shape': 'env.observation_space.shape', 'network': 'network'}), '(nb_actions, ob_shape=env.observation_space.shape, network=network,\n **network_kwargs)\n', (1954, 2043), False, 'from baselines.ddpg.models import Actor, Critic\n'), ((2052, 2147), 'baselines.ddpg.models.Actor', 'Actor', (['nb_actions'], {'ob_shape': 'env.observation_space.shape', 'network': 'network'}), '(nb_actions, ob_shape=env.observation_space.shape, network=network, **\n network_kwargs)\n', (2057, 2147), False, 'from baselines.ddpg.models import Actor, Critic\n'), ((3339, 3762), 'baselines.ddpg.ddpg_learner.DDPG', 'DDPG', (['actor', 'critic', 'memory', 'env.observation_space.shape', 'env.action_space.shape'], {'gamma': 'gamma', 'tau': 'tau', 'normalize_returns': 'normalize_returns', 'normalize_observations': 'normalize_observations', 'batch_size': 'batch_size', 'action_noise': 'action_noise', 'param_noise': 'param_noise', 'critic_l2_reg': 'critic_l2_reg', 'actor_lr': 'actor_lr', 'critic_lr': 'critic_lr', 'enable_popart': 'popart', 'clip_norm': 'clip_norm', 'reward_scale': 'reward_scale'}), '(actor, critic, memory, env.observation_space.shape, env.action_space.\n shape, gamma=gamma, tau=tau, normalize_returns=normalize_returns,\n normalize_observations=normalize_observations, batch_size=batch_size,\n action_noise=action_noise, param_noise=param_noise, critic_l2_reg=\n critic_l2_reg, actor_lr=actor_lr, critic_lr=critic_lr, enable_popart=\n popart, clip_norm=clip_norm, reward_scale=reward_scale)\n', (3343, 3762), False, 'from baselines.ddpg.ddpg_learner import DDPG\n'), ((3776, 3836), 'baselines.logger.info', 'logger.info', (['"""Using agent with the following configuration:"""'], {}), "('Using agent with the following configuration:')\n", (3787, 3836), False, 'from baselines import logger\n'), ((4240, 4257), 'collections.deque', 'deque', ([], {'maxlen': '(100)'}), '(maxlen=100)\n', (4245, 4257), False, 'from collections import deque\n'), ((4288, 4305), 'collections.deque', 'deque', ([], {'maxlen': '(100)'}), '(maxlen=100)\n', (4293, 4305), False, 'from collections import deque\n'), ((4510, 4543), 'numpy.zeros', 'np.zeros', (['nenvs'], {'dtype': 'np.float32'}), '(nenvs, dtype=np.float32)\n', (4518, 4543), True, 'import numpy as np\n'), ((4573, 4599), 'numpy.zeros', 'np.zeros', (['nenvs'], {'dtype': 'int'}), '(nenvs, dtype=int)\n', (4581, 4599), True, 'import numpy as np\n'), ((4690, 4701), 'time.time', 'time.time', ([], {}), '()\n', (4699, 4701), False, 'import time\n'), ((1611, 1636), 'mpi4py.MPI.COMM_WORLD.Get_rank', 'MPI.COMM_WORLD.Get_rank', ([], {}), '()\n', (1634, 1636), False, 'from mpi4py import MPI\n'), ((3933, 3958), 'os.path.expanduser', 'osp.expanduser', (['load_path'], {}), '(load_path)\n', (3947, 3958), True, 'import os.path as osp\n'), ((3974, 4006), 'tensorflow.train.Checkpoint', 'tf.train.Checkpoint', ([], {'model': 'agent'}), '(model=agent)\n', (3993, 4006), True, 'import tensorflow as tf\n'), ((4025, 4086), 'tensorflow.train.CheckpointManager', 'tf.train.CheckpointManager', (['ckpt', 'load_path'], {'max_to_keep': 'None'}), '(ckpt, load_path, max_to_keep=None)\n', (4051, 4086), True, 'import tensorflow as tf\n'), ((9132, 9162), 'numpy.mean', 'np.mean', (['epoch_episode_rewards'], {}), '(epoch_episode_rewards)\n', (9139, 9162), True, 'import numpy as np\n'), ((9210, 9239), 'numpy.std', 'np.std', (['epoch_episode_rewards'], {}), '(epoch_episode_rewards)\n', (9216, 9239), True, 'import numpy as np\n'), ((9291, 9323), 'numpy.mean', 'np.mean', (['episode_rewards_history'], {}), '(episode_rewards_history)\n', (9298, 9323), True, 'import numpy as np\n'), ((9379, 9410), 'numpy.std', 'np.std', (['episode_rewards_history'], {}), '(episode_rewards_history)\n', (9385, 9410), True, 'import numpy as np\n'), ((9461, 9489), 'numpy.mean', 'np.mean', (['epoch_episode_steps'], {}), '(epoch_episode_steps)\n', (9468, 9489), True, 'import numpy as np\n'), ((9539, 9561), 'numpy.mean', 'np.mean', (['epoch_actions'], {}), '(epoch_actions)\n', (9546, 9561), True, 'import numpy as np\n'), ((9605, 9622), 'numpy.mean', 'np.mean', (['epoch_qs'], {}), '(epoch_qs)\n', (9612, 9622), True, 'import numpy as np\n'), ((9668, 9695), 'numpy.mean', 'np.mean', (['epoch_actor_losses'], {}), '(epoch_actor_losses)\n', (9675, 9695), True, 'import numpy as np\n'), ((9742, 9770), 'numpy.mean', 'np.mean', (['epoch_critic_losses'], {}), '(epoch_critic_losses)\n', (9749, 9770), True, 'import numpy as np\n'), ((9826, 9859), 'numpy.mean', 'np.mean', (['epoch_adaptive_distances'], {}), '(epoch_adaptive_distances)\n', (9833, 9859), True, 'import numpy as np\n'), ((10150, 10171), 'numpy.std', 'np.std', (['epoch_actions'], {}), '(epoch_actions)\n', (10156, 10171), True, 'import numpy as np\n'), ((11395, 11410), 'baselines.logger.info', 'logger.info', (['""""""'], {}), "('')\n", (11406, 11410), False, 'from baselines import logger\n'), ((11428, 11444), 'baselines.logger.get_dir', 'logger.get_dir', ([], {}), '()\n', (11442, 11444), False, 'from baselines import logger\n'), ((8824, 8849), 'mpi4py.MPI.COMM_WORLD.Get_size', 'MPI.COMM_WORLD.Get_size', ([], {}), '()\n', (8847, 8849), False, 'from mpi4py import MPI\n'), ((8992, 9003), 'time.time', 'time.time', ([], {}), '()\n', (9001, 9003), False, 'import time\n'), ((10355, 10392), 'numpy.mean', 'np.mean', (['eval_episode_rewards_history'], {}), '(eval_episode_rewards_history)\n', (10362, 10392), True, 'import numpy as np\n'), ((10945, 10990), 'mpi4py.MPI.COMM_WORLD.allreduce', 'MPI.COMM_WORLD.allreduce', (['combined_stats_sums'], {}), '(combined_stats_sums)\n', (10969, 10990), False, 'from mpi4py import MPI\n'), ((11282, 11329), 'baselines.logger.record_tabular', 'logger.record_tabular', (['key', 'combined_stats[key]'], {}), '(key, combined_stats[key])\n', (11303, 11329), False, 'from baselines import logger\n'), ((11365, 11386), 'baselines.logger.dump_tabular', 'logger.dump_tabular', ([], {}), '()\n', (11384, 11386), False, 'from baselines import logger\n'), ((1721, 1749), 'numpy.abs', 'np.abs', (['env.action_space.low'], {}), '(env.action_space.low)\n', (1727, 1749), True, 'import numpy as np\n'), ((7893, 7931), 'numpy.zeros', 'np.zeros', (['nenvs_eval'], {'dtype': 'np.float32'}), '(nenvs_eval, dtype=np.float32)\n', (7901, 7931), True, 'import numpy as np\n'), ((10660, 10674), 'numpy.isscalar', 'np.isscalar', (['x'], {}), '(x)\n', (10671, 10674), True, 'import numpy as np\n'), ((5317, 5333), 'tensorflow.constant', 'tf.constant', (['obs'], {}), '(obs)\n', (5328, 5333), True, 'import tensorflow as tf\n'), ((7358, 7384), 'tensorflow.constant', 'tf.constant', (["batch['obs0']"], {}), "(batch['obs0'])\n", (7369, 7384), True, 'import tensorflow as tf\n'), ((11546, 11583), 'os.path.join', 'os.path.join', (['logdir', '"""env_state.pkl"""'], {}), "(logdir, 'env_state.pkl')\n", (11558, 11583), False, 'import os\n'), ((11735, 11777), 'os.path.join', 'os.path.join', (['logdir', '"""eval_env_state.pkl"""'], {}), "(logdir, 'eval_env_state.pkl')\n", (11747, 11777), False, 'import os\n'), ((10823, 10834), 'numpy.array', 'np.array', (['x'], {}), '(x)\n', (10831, 10834), True, 'import numpy as np\n'), ((2799, 2819), 'numpy.zeros', 'np.zeros', (['nb_actions'], {}), '(nb_actions)\n', (2807, 2819), True, 'import numpy as np\n'), ((2843, 2862), 'numpy.ones', 'np.ones', (['nb_actions'], {}), '(nb_actions)\n', (2850, 2862), True, 'import numpy as np\n'), ((3030, 3050), 'numpy.zeros', 'np.zeros', (['nb_actions'], {}), '(nb_actions)\n', (3038, 3050), True, 'import numpy as np\n'), ((3074, 3093), 'numpy.ones', 'np.ones', (['nb_actions'], {}), '(nb_actions)\n', (3081, 3093), True, 'import numpy as np\n')]
enwawerueli/footprints
footprints/transaction_details.py
d9b2a0064b21495edfd0563cb521b0675ee4363d
import os from datetime import datetime from PySide2.QtGui import * from PySide2.QtCore import * from PySide2.QtWidgets import * from PySide2.QtPrintSupport import QPrinter, QPrintDialog from jinja2 import TemplateNotFound from .ui.ui_transaction_details import Ui_TransactionDetails from .ui import images_rc from . import jinja_env from .exceptions import PrinterError class TransactionDetails(QDialog, Ui_TransactionDetails): def __init__(self, transaction, parent=None, *args, **kwargs): QDialog.__init__(self, parent, *args, **kwargs) self._transaction = transaction self.setupUi(self) self.setWindowTitle(QApplication.applicationName()) self.print_pb.setIcon(QIcon.fromTheme('document-print-symbolic', QIcon(':/icons/print'))) try: trans = jinja_env.get_template('trans.jinja2.html') except TemplateNotFound: pass else: html = trans.render(transaction=self._transaction, standalone=True) self.statement_tb.setHtml(html) self.print_pb.clicked.connect(self.print_statement) def print_statement(self): printer = QPrinter() printer.setOutputFileName(os.path.join( os.environ.get('HOME'), '%s_%s.pdf' % (self._transaction.created_at.strftime('%Y%m%d'), self._transaction.transaction_code))) if QPrintDialog(printer, self.parentWidget()).exec_() != QDialog.Accepted: return None try: trans = jinja_env.get_template('trans.jinja2.html') except TemplateNotFound as e: raise PrinterError('Printer data source unavailable') from e html = trans.render(transaction=self._transaction, printed_at=datetime.now().strftime('%d/%m/%Y, %I:%M:%S %p')) doc = QTextDocument(self) doc.setHtml(html) doc.print_(printer) return None
[((1157, 1167), 'PySide2.QtPrintSupport.QPrinter', 'QPrinter', ([], {}), '()\n', (1165, 1167), False, 'from PySide2.QtPrintSupport import QPrinter, QPrintDialog\n'), ((1228, 1250), 'os.environ.get', 'os.environ.get', (['"""HOME"""'], {}), "('HOME')\n", (1242, 1250), False, 'import os\n'), ((1731, 1745), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (1743, 1745), False, 'from datetime import datetime\n')]
FeiLi5/git-github.com-yt-project-yt
yt/units/yt_array.py
0c6cf75351b91e4da80f6a0207ebbcb73dd72a59
""" YTArray class. """ from __future__ import print_function #----------------------------------------------------------------------------- # Copyright (c) 2013, yt Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. #----------------------------------------------------------------------------- import copy import numpy as np from distutils.version import LooseVersion from functools import wraps from numpy import \ add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, \ floor_divide, negative, power, remainder, mod, absolute, rint, \ sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, \ reciprocal, sin, cos, tan, arcsin, arccos, arctan, arctan2, \ hypot, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, \ bitwise_and, bitwise_or, bitwise_xor, invert, left_shift, right_shift, \ greater, greater_equal, less, less_equal, not_equal, equal, logical_and, \ logical_or, logical_xor, logical_not, maximum, minimum, fmax, fmin, \ isreal, iscomplex, isfinite, isinf, isnan, signbit, copysign, nextafter, \ modf, ldexp, frexp, fmod, floor, ceil, trunc, fabs, spacing try: # numpy 1.13 or newer from numpy import positive, divmod as divmod_, isnat, heaviside except ImportError: positive, divmod_, isnat, heaviside = (None,)*4 from yt.units.unit_object import Unit, UnitParseError from yt.units.unit_registry import UnitRegistry from yt.units.dimensions import \ angle, \ current_mks, \ dimensionless, \ em_dimensions from yt.utilities.exceptions import \ YTUnitOperationError, YTUnitConversionError, \ YTUfuncUnitError, YTIterableUnitCoercionError, \ YTInvalidUnitEquivalence, YTEquivalentDimsError from yt.utilities.lru_cache import lru_cache from numbers import Number as numeric_type from yt.utilities.on_demand_imports import _astropy from sympy import Rational from yt.units.unit_lookup_table import \ default_unit_symbol_lut from yt.units.equivalencies import equivalence_registry from yt.utilities.logger import ytLogger as mylog from .pint_conversions import convert_pint_units NULL_UNIT = Unit() POWER_SIGN_MAPPING = {multiply: 1, divide: -1} # redefine this here to avoid a circular import from yt.funcs def iterable(obj): try: len(obj) except: return False return True def return_arr(func): @wraps(func) def wrapped(*args, **kwargs): ret, units = func(*args, **kwargs) if ret.shape == (): return YTQuantity(ret, units) else: # This could be a subclass, so don't call YTArray directly. return type(args[0])(ret, units) return wrapped @lru_cache(maxsize=128, typed=False) def sqrt_unit(unit): return unit**0.5 @lru_cache(maxsize=128, typed=False) def multiply_units(unit1, unit2): return unit1 * unit2 def preserve_units(unit1, unit2=None): return unit1 @lru_cache(maxsize=128, typed=False) def power_unit(unit, power): return unit**power @lru_cache(maxsize=128, typed=False) def square_unit(unit): return unit*unit @lru_cache(maxsize=128, typed=False) def divide_units(unit1, unit2): return unit1/unit2 @lru_cache(maxsize=128, typed=False) def reciprocal_unit(unit): return unit**-1 def passthrough_unit(unit, unit2=None): return unit def return_without_unit(unit, unit2=None): return None def arctan2_unit(unit1, unit2): return NULL_UNIT def comparison_unit(unit1, unit2=None): return None def invert_units(unit): raise TypeError( "Bit-twiddling operators are not defined for YTArray instances") def bitop_units(unit1, unit2): raise TypeError( "Bit-twiddling operators are not defined for YTArray instances") def get_inp_u_unary(ufunc, inputs, out_arr=None): inp = inputs[0] u = getattr(inp, 'units', None) if u is None: u = NULL_UNIT if u.dimensions is angle and ufunc in trigonometric_operators: inp = inp.in_units('radian').v if out_arr is not None: out_arr = ufunc(inp).view(np.ndarray) return out_arr, inp, u def get_inp_u_binary(ufunc, inputs): inp1 = coerce_iterable_units(inputs[0]) inp2 = coerce_iterable_units(inputs[1]) unit1 = getattr(inp1, 'units', None) unit2 = getattr(inp2, 'units', None) ret_class = get_binary_op_return_class(type(inp1), type(inp2)) if unit1 is None: unit1 = Unit(registry=getattr(unit2, 'registry', None)) if unit2 is None and ufunc is not power: unit2 = Unit(registry=getattr(unit1, 'registry', None)) elif ufunc is power: unit2 = inp2 if isinstance(unit2, np.ndarray): if isinstance(unit2, YTArray): if unit2.units.is_dimensionless: pass else: raise YTUnitOperationError(ufunc, unit1, unit2) unit2 = 1.0 return (inp1, inp2), (unit1, unit2), ret_class def handle_preserve_units(inps, units, ufunc, ret_class): if units[0] != units[1]: any_nonzero = [np.any(inps[0]), np.any(inps[1])] if any_nonzero[0] == np.bool_(False): units = (units[1], units[1]) elif any_nonzero[1] == np.bool_(False): units = (units[0], units[0]) else: if not units[0].same_dimensions_as(units[1]): raise YTUnitOperationError(ufunc, *units) inps = (inps[0], ret_class(inps[1]).to( ret_class(inps[0]).units)) return inps, units def handle_comparison_units(inps, units, ufunc, ret_class, raise_error=False): if units[0] != units[1]: u1d = units[0].is_dimensionless u2d = units[1].is_dimensionless any_nonzero = [np.any(inps[0]), np.any(inps[1])] if any_nonzero[0] == np.bool_(False): units = (units[1], units[1]) elif any_nonzero[1] == np.bool_(False): units = (units[0], units[0]) elif not any([u1d, u2d]): if not units[0].same_dimensions_as(units[1]): raise YTUnitOperationError(ufunc, *units) else: if raise_error: raise YTUfuncUnitError(ufunc, *units) inps = (inps[0], ret_class(inps[1]).to( ret_class(inps[0]).units)) return inps, units def handle_multiply_divide_units(unit, units, out, out_arr): if unit.is_dimensionless and unit.base_value != 1.0: if not units[0].is_dimensionless: if units[0].dimensions == units[1].dimensions: out_arr = np.multiply(out_arr.view(np.ndarray), unit.base_value, out=out) unit = Unit(registry=unit.registry) return out, out_arr, unit def coerce_iterable_units(input_object): if isinstance(input_object, np.ndarray): return input_object if iterable(input_object): if any([isinstance(o, YTArray) for o in input_object]): ff = getattr(input_object[0], 'units', NULL_UNIT, ) if any([ff != getattr(_, 'units', NULL_UNIT) for _ in input_object]): raise YTIterableUnitCoercionError(input_object) # This will create a copy of the data in the iterable. return YTArray(input_object) return input_object else: return input_object def sanitize_units_mul(this_object, other_object): inp = coerce_iterable_units(this_object) ret = coerce_iterable_units(other_object) # If the other object is a YTArray and has the same dimensions as the object # under consideration, convert so we don't mix units with the same # dimensions. if isinstance(ret, YTArray): if inp.units.same_dimensions_as(ret.units): ret.in_units(inp.units) return ret def sanitize_units_add(this_object, other_object, op_string): inp = coerce_iterable_units(this_object) ret = coerce_iterable_units(other_object) # Make sure the other object is a YTArray before we use the `units` # attribute. if isinstance(ret, YTArray): if not inp.units.same_dimensions_as(ret.units): # handle special case of adding or subtracting with zero or # array filled with zero if not np.any(other_object): return ret.view(np.ndarray) elif not np.any(this_object): return ret raise YTUnitOperationError(op_string, inp.units, ret.units) ret = ret.in_units(inp.units) else: # If the other object is not a YTArray, then one of the arrays must be # dimensionless or filled with zeros if not inp.units.is_dimensionless and np.any(ret): raise YTUnitOperationError(op_string, inp.units, dimensionless) return ret def validate_comparison_units(this, other, op_string): # Check that other is a YTArray. if hasattr(other, 'units'): if this.units.expr is other.units.expr: if this.units.base_value == other.units.base_value: return other if not this.units.same_dimensions_as(other.units): raise YTUnitOperationError(op_string, this.units, other.units) return other.in_units(this.units) return other @lru_cache(maxsize=128, typed=False) def _unit_repr_check_same(my_units, other_units): """ Takes a Unit object, or string of known unit symbol, and check that it is compatible with this quantity. Returns Unit object. """ # let Unit() handle units arg if it's not already a Unit obj. if not isinstance(other_units, Unit): other_units = Unit(other_units, registry=my_units.registry) equiv_dims = em_dimensions.get(my_units.dimensions, None) if equiv_dims == other_units.dimensions: if current_mks in equiv_dims.free_symbols: base = "SI" else: base = "CGS" raise YTEquivalentDimsError(my_units, other_units, base) if not my_units.same_dimensions_as(other_units): raise YTUnitConversionError( my_units, my_units.dimensions, other_units, other_units.dimensions) return other_units unary_operators = ( negative, absolute, rint, sign, conj, exp, exp2, log, log2, log10, expm1, log1p, sqrt, square, reciprocal, sin, cos, tan, arcsin, arccos, arctan, sinh, cosh, tanh, arcsinh, arccosh, arctanh, deg2rad, rad2deg, invert, logical_not, isreal, iscomplex, isfinite, isinf, isnan, signbit, floor, ceil, trunc, modf, frexp, fabs, spacing, positive, isnat, ) binary_operators = ( add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, power, remainder, mod, arctan2, hypot, bitwise_and, bitwise_or, bitwise_xor, left_shift, right_shift, greater, greater_equal, less, less_equal, not_equal, equal, logical_and, logical_or, logical_xor, maximum, minimum, fmax, fmin, copysign, nextafter, ldexp, fmod, divmod_, heaviside ) trigonometric_operators = ( sin, cos, tan, ) class YTArray(np.ndarray): """ An ndarray subclass that attaches a symbolic unit object to the array data. Parameters ---------- input_array : :obj:`!iterable` A tuple, list, or array to attach units to input_units : String unit specification, unit symbol object, or astropy units The units of the array. Powers must be specified using python syntax (cm**3, not cm^3). registry : ~yt.units.unit_registry.UnitRegistry The registry to create units from. If input_units is already associated with a unit registry and this is specified, this will be used instead of the registry associated with the unit object. dtype : data-type The dtype of the array data. Defaults to the dtype of the input data, or, if none is found, uses np.float64 bypass_validation : boolean If True, all input validation is skipped. Using this option may produce corrupted, invalid units or array data, but can lead to significant speedups in the input validation logic adds significant overhead. If set, input_units *must* be a valid unit object. Defaults to False. Examples -------- >>> from yt import YTArray >>> a = YTArray([1, 2, 3], 'cm') >>> b = YTArray([4, 5, 6], 'm') >>> a + b YTArray([ 401., 502., 603.]) cm >>> b + a YTArray([ 4.01, 5.02, 6.03]) m NumPy ufuncs will pass through units where appropriate. >>> import numpy as np >>> a = YTArray(np.arange(8) - 4, 'g/cm**3') >>> np.abs(a) YTArray([4, 3, 2, 1, 0, 1, 2, 3]) g/cm**3 and strip them when it would be annoying to deal with them. >>> np.log10(a) array([ -inf, 0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 , 0.77815125, 0.84509804]) YTArray is tightly integrated with yt datasets: >>> import yt >>> ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030') >>> a = ds.arr(np.ones(5), 'code_length') >>> a.in_cgs() YTArray([ 3.08600000e+24, 3.08600000e+24, 3.08600000e+24, 3.08600000e+24, 3.08600000e+24]) cm This is equivalent to: >>> b = YTArray(np.ones(5), 'code_length', registry=ds.unit_registry) >>> np.all(a == b) True """ _ufunc_registry = { add: preserve_units, subtract: preserve_units, multiply: multiply_units, divide: divide_units, logaddexp: return_without_unit, logaddexp2: return_without_unit, true_divide: divide_units, floor_divide: divide_units, negative: passthrough_unit, power: power_unit, remainder: preserve_units, mod: preserve_units, fmod: preserve_units, absolute: passthrough_unit, fabs: passthrough_unit, rint: return_without_unit, sign: return_without_unit, conj: passthrough_unit, exp: return_without_unit, exp2: return_without_unit, log: return_without_unit, log2: return_without_unit, log10: return_without_unit, expm1: return_without_unit, log1p: return_without_unit, sqrt: sqrt_unit, square: square_unit, reciprocal: reciprocal_unit, sin: return_without_unit, cos: return_without_unit, tan: return_without_unit, sinh: return_without_unit, cosh: return_without_unit, tanh: return_without_unit, arcsin: return_without_unit, arccos: return_without_unit, arctan: return_without_unit, arctan2: arctan2_unit, arcsinh: return_without_unit, arccosh: return_without_unit, arctanh: return_without_unit, hypot: preserve_units, deg2rad: return_without_unit, rad2deg: return_without_unit, bitwise_and: bitop_units, bitwise_or: bitop_units, bitwise_xor: bitop_units, invert: invert_units, left_shift: bitop_units, right_shift: bitop_units, greater: comparison_unit, greater_equal: comparison_unit, less: comparison_unit, less_equal: comparison_unit, not_equal: comparison_unit, equal: comparison_unit, logical_and: comparison_unit, logical_or: comparison_unit, logical_xor: comparison_unit, logical_not: return_without_unit, maximum: preserve_units, minimum: preserve_units, fmax: preserve_units, fmin: preserve_units, isreal: return_without_unit, iscomplex: return_without_unit, isfinite: return_without_unit, isinf: return_without_unit, isnan: return_without_unit, signbit: return_without_unit, copysign: passthrough_unit, nextafter: preserve_units, modf: passthrough_unit, ldexp: bitop_units, frexp: return_without_unit, floor: passthrough_unit, ceil: passthrough_unit, trunc: passthrough_unit, spacing: passthrough_unit, positive: passthrough_unit, divmod_: passthrough_unit, isnat: return_without_unit, heaviside: preserve_units, } __array_priority__ = 2.0 def __new__(cls, input_array, input_units=None, registry=None, dtype=None, bypass_validation=False): if dtype is None: dtype = getattr(input_array, 'dtype', np.float64) if bypass_validation is True: obj = np.asarray(input_array, dtype=dtype).view(cls) obj.units = input_units if registry is not None: obj.units.registry = registry return obj if input_array is NotImplemented: return input_array.view(cls) if registry is None and isinstance(input_units, (str, bytes)): if input_units.startswith('code_'): raise UnitParseError( "Code units used without referring to a dataset. \n" "Perhaps you meant to do something like this instead: \n" "ds.arr(%s, \"%s\")" % (input_array, input_units) ) if isinstance(input_array, YTArray): ret = input_array.view(cls) if input_units is None: if registry is None: ret.units = input_array.units else: units = Unit(str(input_array.units), registry=registry) ret.units = units elif isinstance(input_units, Unit): ret.units = input_units else: ret.units = Unit(input_units, registry=registry) return ret elif isinstance(input_array, np.ndarray): pass elif iterable(input_array) and input_array: if isinstance(input_array[0], YTArray): return YTArray(np.array(input_array, dtype=dtype), input_array[0].units, registry=registry) # Input array is an already formed ndarray instance # We first cast to be our class type obj = np.asarray(input_array, dtype=dtype).view(cls) # Check units type if input_units is None: # Nothing provided. Make dimensionless... units = Unit() elif isinstance(input_units, Unit): if registry and registry is not input_units.registry: units = Unit(str(input_units), registry=registry) else: units = input_units else: # units kwarg set, but it's not a Unit object. # don't handle all the cases here, let the Unit class handle if # it's a str. units = Unit(input_units, registry=registry) # Attach the units obj.units = units return obj def __repr__(self): """ """ return super(YTArray, self).__repr__()+' '+self.units.__repr__() def __str__(self): """ """ return str(self.view(np.ndarray)) + ' ' + str(self.units) # # Start unit conversion methods # def convert_to_units(self, units): """ Convert the array and units to the given units. Parameters ---------- units : Unit object or str The units you want to convert to. """ new_units = _unit_repr_check_same(self.units, units) (conversion_factor, offset) = self.units.get_conversion_factor(new_units) self.units = new_units values = self.d values *= conversion_factor if offset: np.subtract(self, offset*self.uq, self) return self def convert_to_base(self, unit_system="cgs"): """ Convert the array and units to the equivalent base units in the specified unit system. Parameters ---------- unit_system : string, optional The unit system to be used in the conversion. If not specified, the default base units of cgs are used. Examples -------- >>> E = YTQuantity(2.5, "erg/s") >>> E.convert_to_base(unit_system="galactic") """ return self.convert_to_units(self.units.get_base_equivalent(unit_system)) def convert_to_cgs(self): """ Convert the array and units to the equivalent cgs units. """ return self.convert_to_units(self.units.get_cgs_equivalent()) def convert_to_mks(self): """ Convert the array and units to the equivalent mks units. """ return self.convert_to_units(self.units.get_mks_equivalent()) def in_units(self, units, equivalence=None, **kwargs): """ Creates a copy of this array with the data in the supplied units, and returns it. Optionally, an equivalence can be specified to convert to an equivalent quantity which is not in the same dimensions. .. note:: All additional keyword arguments are passed to the equivalency, which should be used if that particular equivalency requires them. Parameters ---------- units : Unit object or string The units you want to get a new quantity in. equivalence : string, optional The equivalence you wish to use. To see which equivalencies are supported for this unitful quantity, try the :meth:`list_equivalencies` method. Default: None Returns ------- YTArray """ if equivalence is None: new_units = _unit_repr_check_same(self.units, units) (conversion_factor, offset) = self.units.get_conversion_factor(new_units) new_array = type(self)(self.ndview * conversion_factor, new_units) if offset: np.subtract(new_array, offset*new_array.uq, new_array) return new_array else: return self.to_equivalent(units, equivalence, **kwargs) def to(self, units, equivalence=None, **kwargs): """ An alias for YTArray.in_units(). See the docstrings of that function for details. """ return self.in_units(units, equivalence=equivalence, **kwargs) def to_value(self, units=None, equivalence=None, **kwargs): """ Creates a copy of this array with the data in the supplied units, and returns it without units. Output is therefore a bare NumPy array. Optionally, an equivalence can be specified to convert to an equivalent quantity which is not in the same dimensions. .. note:: All additional keyword arguments are passed to the equivalency, which should be used if that particular equivalency requires them. Parameters ---------- units : Unit object or string, optional The units you want to get the bare quantity in. If not specified, the value will be returned in the current units. equivalence : string, optional The equivalence you wish to use. To see which equivalencies are supported for this unitful quantity, try the :meth:`list_equivalencies` method. Default: None Returns ------- NumPy array """ if units is None: v = self.value else: v = self.in_units(units, equivalence=equivalence, **kwargs).value if isinstance(self, YTQuantity): return float(v) else: return v def in_base(self, unit_system="cgs"): """ Creates a copy of this array with the data in the specified unit system, and returns it in that system's base units. Parameters ---------- unit_system : string, optional The unit system to be used in the conversion. If not specified, the default base units of cgs are used. Examples -------- >>> E = YTQuantity(2.5, "erg/s") >>> E_new = E.in_base(unit_system="galactic") """ return self.in_units(self.units.get_base_equivalent(unit_system)) def in_cgs(self): """ Creates a copy of this array with the data in the equivalent cgs units, and returns it. Returns ------- Quantity object with data converted to cgs units. """ return self.in_units(self.units.get_cgs_equivalent()) def in_mks(self): """ Creates a copy of this array with the data in the equivalent mks units, and returns it. Returns ------- Quantity object with data converted to mks units. """ return self.in_units(self.units.get_mks_equivalent()) def to_equivalent(self, unit, equiv, **kwargs): """ Convert a YTArray or YTQuantity to an equivalent, e.g., something that is related by only a constant factor but not in the same units. Parameters ---------- unit : string The unit that you wish to convert to. equiv : string The equivalence you wish to use. To see which equivalencies are supported for this unitful quantity, try the :meth:`list_equivalencies` method. Examples -------- >>> a = yt.YTArray(1.0e7,"K") >>> a.to_equivalent("keV", "thermal") """ conv_unit = Unit(unit, registry=self.units.registry) if self.units.same_dimensions_as(conv_unit): return self.in_units(conv_unit) this_equiv = equivalence_registry[equiv]() oneway_or_equivalent = ( conv_unit.has_equivalent(equiv) or this_equiv._one_way) if self.has_equivalent(equiv) and oneway_or_equivalent: new_arr = this_equiv.convert( self, conv_unit.dimensions, **kwargs) if isinstance(new_arr, tuple): try: return type(self)(new_arr[0], new_arr[1]).in_units(unit) except YTUnitConversionError: raise YTInvalidUnitEquivalence(equiv, self.units, unit) else: return new_arr.in_units(unit) else: raise YTInvalidUnitEquivalence(equiv, self.units, unit) def list_equivalencies(self): """ Lists the possible equivalencies associated with this YTArray or YTQuantity. """ self.units.list_equivalencies() def has_equivalent(self, equiv): """ Check to see if this YTArray or YTQuantity has an equivalent unit in *equiv*. """ return self.units.has_equivalent(equiv) def ndarray_view(self): """ Returns a view into the array, but as an ndarray rather than ytarray. Returns ------- View of this array's data. """ return self.view(np.ndarray) def to_ndarray(self): """ Creates a copy of this array with the unit information stripped """ return np.array(self) @classmethod def from_astropy(cls, arr, unit_registry=None): """ Convert an AstroPy "Quantity" to a YTArray or YTQuantity. Parameters ---------- arr : AstroPy Quantity The Quantity to convert from. unit_registry : yt UnitRegistry, optional A yt unit registry to use in the conversion. If one is not supplied, the default one will be used. """ # Converting from AstroPy Quantity u = arr.unit ap_units = [] for base, exponent in zip(u.bases, u.powers): unit_str = base.to_string() # we have to do this because AstroPy is silly and defines # hour as "h" if unit_str == "h": unit_str = "hr" ap_units.append("%s**(%s)" % (unit_str, Rational(exponent))) ap_units = "*".join(ap_units) if isinstance(arr.value, np.ndarray): return YTArray(arr.value, ap_units, registry=unit_registry) else: return YTQuantity(arr.value, ap_units, registry=unit_registry) def to_astropy(self, **kwargs): """ Creates a new AstroPy quantity with the same unit information. """ if _astropy.units is None: raise ImportError("You don't have AstroPy installed, so you can't convert to " + "an AstroPy quantity.") return self.value*_astropy.units.Unit(str(self.units), **kwargs) @classmethod def from_pint(cls, arr, unit_registry=None): """ Convert a Pint "Quantity" to a YTArray or YTQuantity. Parameters ---------- arr : Pint Quantity The Quantity to convert from. unit_registry : yt UnitRegistry, optional A yt unit registry to use in the conversion. If one is not supplied, the default one will be used. Examples -------- >>> from pint import UnitRegistry >>> import numpy as np >>> ureg = UnitRegistry() >>> a = np.random.random(10) >>> b = ureg.Quantity(a, "erg/cm**3") >>> c = yt.YTArray.from_pint(b) """ p_units = [] for base, exponent in arr._units.items(): bs = convert_pint_units(base) p_units.append("%s**(%s)" % (bs, Rational(exponent))) p_units = "*".join(p_units) if isinstance(arr.magnitude, np.ndarray): return YTArray(arr.magnitude, p_units, registry=unit_registry) else: return YTQuantity(arr.magnitude, p_units, registry=unit_registry) def to_pint(self, unit_registry=None): """ Convert a YTArray or YTQuantity to a Pint Quantity. Parameters ---------- arr : YTArray or YTQuantity The unitful quantity to convert from. unit_registry : Pint UnitRegistry, optional The Pint UnitRegistry to use in the conversion. If one is not supplied, the default one will be used. NOTE: This is not the same as a yt UnitRegistry object. Examples -------- >>> a = YTQuantity(4.0, "cm**2/s") >>> b = a.to_pint() """ from pint import UnitRegistry if unit_registry is None: unit_registry = UnitRegistry() powers_dict = self.units.expr.as_powers_dict() units = [] for unit, pow in powers_dict.items(): # we have to do this because Pint doesn't recognize # "yr" as "year" if str(unit).endswith("yr") and len(str(unit)) in [2,3]: unit = str(unit).replace("yr","year") units.append("%s**(%s)" % (unit, Rational(pow))) units = "*".join(units) return unit_registry.Quantity(self.value, units) # # End unit conversion methods # def write_hdf5(self, filename, dataset_name=None, info=None, group_name=None): r"""Writes a YTArray to hdf5 file. Parameters ---------- filename: string The filename to create and write a dataset to dataset_name: string The name of the dataset to create in the file. info: dictionary A dictionary of supplementary info to write to append as attributes to the dataset. group_name: string An optional group to write the arrays to. If not specified, the arrays are datasets at the top level by default. Examples -------- >>> a = YTArray([1,2,3], 'cm') >>> myinfo = {'field':'dinosaurs', 'type':'field_data'} >>> a.write_hdf5('test_array_data.h5', dataset_name='dinosaurs', ... info=myinfo) """ from yt.utilities.on_demand_imports import _h5py as h5py from yt.extern.six.moves import cPickle as pickle if info is None: info = {} info['units'] = str(self.units) info['unit_registry'] = np.void(pickle.dumps(self.units.registry.lut)) if dataset_name is None: dataset_name = 'array_data' f = h5py.File(filename) if group_name is not None: if group_name in f: g = f[group_name] else: g = f.create_group(group_name) else: g = f if dataset_name in g.keys(): d = g[dataset_name] # Overwrite without deleting if we can get away with it. if d.shape == self.shape and d.dtype == self.dtype: d[...] = self for k in d.attrs.keys(): del d.attrs[k] else: del f[dataset_name] d = g.create_dataset(dataset_name, data=self) else: d = g.create_dataset(dataset_name, data=self) for k, v in info.items(): d.attrs[k] = v f.close() @classmethod def from_hdf5(cls, filename, dataset_name=None, group_name=None): r"""Attempts read in and convert a dataset in an hdf5 file into a YTArray. Parameters ---------- filename: string The filename to of the hdf5 file. dataset_name: string The name of the dataset to read from. If the dataset has a units attribute, attempt to infer units as well. group_name: string An optional group to read the arrays from. If not specified, the arrays are datasets at the top level by default. """ import h5py from yt.extern.six.moves import cPickle as pickle if dataset_name is None: dataset_name = 'array_data' f = h5py.File(filename) if group_name is not None: g = f[group_name] else: g = f dataset = g[dataset_name] data = dataset[:] units = dataset.attrs.get('units', '') if 'unit_registry' in dataset.attrs.keys(): unit_lut = pickle.loads(dataset.attrs['unit_registry'].tostring()) else: unit_lut = None f.close() registry = UnitRegistry(lut=unit_lut, add_default_symbols=False) return cls(data, units, registry=registry) # # Start convenience methods # @property def value(self): """Get a copy of the array data as a numpy ndarray""" return np.array(self) v = value @property def ndview(self): """Get a view of the array data.""" return self.ndarray_view() d = ndview @property def unit_quantity(self): """Get a YTQuantity with the same unit as this array and a value of 1.0""" return YTQuantity(1.0, self.units) uq = unit_quantity @property def unit_array(self): """Get a YTArray filled with ones with the same unit and shape as this array""" return np.ones_like(self) ua = unit_array def __getitem__(self, item): ret = super(YTArray, self).__getitem__(item) if ret.shape == (): return YTQuantity(ret, self.units, bypass_validation=True) else: if hasattr(self, 'units'): ret.units = self.units return ret # # Start operation methods # if LooseVersion(np.__version__) < LooseVersion('1.13.0'): def __add__(self, right_object): """ Add this ytarray to the object on the right of the `+` operator. Must check for the correct (same dimension) units. """ ro = sanitize_units_add(self, right_object, "addition") return super(YTArray, self).__add__(ro) def __radd__(self, left_object): """ See __add__. """ lo = sanitize_units_add(self, left_object, "addition") return super(YTArray, self).__radd__(lo) def __iadd__(self, other): """ See __add__. """ oth = sanitize_units_add(self, other, "addition") np.add(self, oth, out=self) return self def __sub__(self, right_object): """ Subtract the object on the right of the `-` from this ytarray. Must check for the correct (same dimension) units. """ ro = sanitize_units_add(self, right_object, "subtraction") return super(YTArray, self).__sub__(ro) def __rsub__(self, left_object): """ See __sub__. """ lo = sanitize_units_add(self, left_object, "subtraction") return super(YTArray, self).__rsub__(lo) def __isub__(self, other): """ See __sub__. """ oth = sanitize_units_add(self, other, "subtraction") np.subtract(self, oth, out=self) return self def __neg__(self): """ Negate the data. """ return super(YTArray, self).__neg__() def __mul__(self, right_object): """ Multiply this YTArray by the object on the right of the `*` operator. The unit objects handle being multiplied. """ ro = sanitize_units_mul(self, right_object) return super(YTArray, self).__mul__(ro) def __rmul__(self, left_object): """ See __mul__. """ lo = sanitize_units_mul(self, left_object) return super(YTArray, self).__rmul__(lo) def __imul__(self, other): """ See __mul__. """ oth = sanitize_units_mul(self, other) np.multiply(self, oth, out=self) return self def __div__(self, right_object): """ Divide this YTArray by the object on the right of the `/` operator. """ ro = sanitize_units_mul(self, right_object) return super(YTArray, self).__div__(ro) def __rdiv__(self, left_object): """ See __div__. """ lo = sanitize_units_mul(self, left_object) return super(YTArray, self).__rdiv__(lo) def __idiv__(self, other): """ See __div__. """ oth = sanitize_units_mul(self, other) np.divide(self, oth, out=self) return self def __truediv__(self, right_object): ro = sanitize_units_mul(self, right_object) return super(YTArray, self).__truediv__(ro) def __rtruediv__(self, left_object): """ See __div__. """ lo = sanitize_units_mul(self, left_object) return super(YTArray, self).__rtruediv__(lo) def __itruediv__(self, other): """ See __div__. """ oth = sanitize_units_mul(self, other) np.true_divide(self, oth, out=self) return self def __floordiv__(self, right_object): ro = sanitize_units_mul(self, right_object) return super(YTArray, self).__floordiv__(ro) def __rfloordiv__(self, left_object): """ See __div__. """ lo = sanitize_units_mul(self, left_object) return super(YTArray, self).__rfloordiv__(lo) def __ifloordiv__(self, other): """ See __div__. """ oth = sanitize_units_mul(self, other) np.floor_divide(self, oth, out=self) return self def __or__(self, right_object): return super(YTArray, self).__or__(right_object) def __ror__(self, left_object): return super(YTArray, self).__ror__(left_object) def __ior__(self, other): np.bitwise_or(self, other, out=self) return self def __xor__(self, right_object): return super(YTArray, self).__xor__(right_object) def __rxor__(self, left_object): return super(YTArray, self).__rxor__(left_object) def __ixor__(self, other): np.bitwise_xor(self, other, out=self) return self def __and__(self, right_object): return super(YTArray, self).__and__(right_object) def __rand__(self, left_object): return super(YTArray, self).__rand__(left_object) def __iand__(self, other): np.bitwise_and(self, other, out=self) return self def __pow__(self, power): """ Raise this YTArray to some power. Parameters ---------- power : float or dimensionless YTArray. The pow value. """ if isinstance(power, YTArray): if not power.units.is_dimensionless: raise YTUnitOperationError('power', power.unit) # Work around a sympy issue (I think?) # # If I don't do this, super(YTArray, self).__pow__ returns a YTArray # with a unit attribute set to the sympy expression 1/1 rather than # a dimensionless Unit object. if self.units.is_dimensionless and power == -1: ret = super(YTArray, self).__pow__(power) return type(self)(ret, input_units='') return super(YTArray, self).__pow__(power) def __abs__(self): """ Return a YTArray with the abs of the data. """ return super(YTArray, self).__abs__() # # Start comparison operators. # def __lt__(self, other): """ Test if this is less than the object on the right. """ # converts if possible oth = validate_comparison_units(self, other, 'less_than') return super(YTArray, self).__lt__(oth) def __le__(self, other): """Test if this is less than or equal to the object on the right. """ oth = validate_comparison_units(self, other, 'less_than or equal') return super(YTArray, self).__le__(oth) def __eq__(self, other): """ Test if this is equal to the object on the right. """ # Check that other is a YTArray. if other is None: # self is a YTArray, so it can't be None. return False oth = validate_comparison_units(self, other, 'equal') return super(YTArray, self).__eq__(oth) def __ne__(self, other): """ Test if this is not equal to the object on the right. """ # Check that the other is a YTArray. if other is None: return True oth = validate_comparison_units(self, other, 'not equal') return super(YTArray, self).__ne__(oth) def __ge__(self, other): """ Test if this is greater than or equal to other. """ # Check that the other is a YTArray. oth = validate_comparison_units( self, other, 'greater than or equal') return super(YTArray, self).__ge__(oth) def __gt__(self, other): """ Test if this is greater than the object on the right. """ # Check that the other is a YTArray. oth = validate_comparison_units(self, other, 'greater than') return super(YTArray, self).__gt__(oth) # # End comparison operators # # # Begin reduction operators # @return_arr def prod(self, axis=None, dtype=None, out=None): if axis is not None: units = self.units**self.shape[axis] else: units = self.units**self.size return super(YTArray, self).prod(axis, dtype, out), units @return_arr def mean(self, axis=None, dtype=None, out=None): return super(YTArray, self).mean(axis, dtype, out), self.units @return_arr def sum(self, axis=None, dtype=None, out=None): return super(YTArray, self).sum(axis, dtype, out), self.units @return_arr def std(self, axis=None, dtype=None, out=None, ddof=0): return super(YTArray, self).std(axis, dtype, out, ddof), self.units def __array_wrap__(self, out_arr, context=None): ret = super(YTArray, self).__array_wrap__(out_arr, context) if isinstance(ret, YTQuantity) and ret.shape != (): ret = ret.view(YTArray) if context is None: if ret.shape == (): return ret[()] else: return ret ufunc = context[0] inputs = context[1] if ufunc in unary_operators: out_arr, inp, u = get_inp_u_unary(ufunc, inputs, out_arr) unit = self._ufunc_registry[context[0]](u) ret_class = type(self) elif ufunc in binary_operators: unit_operator = self._ufunc_registry[context[0]] inps, units, ret_class = get_inp_u_binary(ufunc, inputs) if unit_operator in (preserve_units, comparison_unit, arctan2_unit): inps, units = handle_comparison_units( inps, units, ufunc, ret_class, raise_error=True) unit = unit_operator(*units) if unit_operator in (multiply_units, divide_units): out_arr, out_arr, unit = handle_multiply_divide_units( unit, units, out_arr, out_arr) else: raise RuntimeError( "Support for the %s ufunc has not been added " "to YTArray." % str(context[0])) if unit is None: out_arr = np.array(out_arr, copy=False) return out_arr out_arr.units = unit if out_arr.size == 1: return YTQuantity(np.array(out_arr), unit) else: if ret_class is YTQuantity: # This happens if you do ndarray * YTQuantity. Explicitly # casting to YTArray avoids creating a YTQuantity with # size > 1 return YTArray(np.array(out_arr), unit) return ret_class(np.array(out_arr, copy=False), unit) else: # numpy version equal to or newer than 1.13 def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): func = getattr(ufunc, method) if 'out' in kwargs: out_orig = kwargs.pop('out') out = np.asarray(out_orig[0]) else: out = None if len(inputs) == 1: _, inp, u = get_inp_u_unary(ufunc, inputs) out_arr = func(np.asarray(inp), out=out, **kwargs) if ufunc in (multiply, divide) and method == 'reduce': power_sign = POWER_SIGN_MAPPING[ufunc] if 'axis' in kwargs and kwargs['axis'] is not None: unit = u**(power_sign*inp.shape[kwargs['axis']]) else: unit = u**(power_sign*inp.size) else: unit = self._ufunc_registry[ufunc](u) ret_class = type(self) elif len(inputs) == 2: unit_operator = self._ufunc_registry[ufunc] inps, units, ret_class = get_inp_u_binary(ufunc, inputs) if unit_operator in (comparison_unit, arctan2_unit): inps, units = handle_comparison_units( inps, units, ufunc, ret_class) elif unit_operator is preserve_units: inps, units = handle_preserve_units( inps, units, ufunc, ret_class) unit = unit_operator(*units) out_arr = func(np.asarray(inps[0]), np.asarray(inps[1]), out=out, **kwargs) if unit_operator in (multiply_units, divide_units): out, out_arr, unit = handle_multiply_divide_units( unit, units, out, out_arr) else: raise RuntimeError( "Support for the %s ufunc with %i inputs has not been" "added to YTArray." % (str(ufunc), len(inputs))) if unit is None: out_arr = np.array(out_arr, copy=False) elif ufunc in (modf, divmod_): out_arr = tuple((ret_class(o, unit) for o in out_arr)) elif out_arr.size == 1: out_arr = YTQuantity(np.asarray(out_arr), unit) else: if ret_class is YTQuantity: # This happens if you do ndarray * YTQuantity. Explicitly # casting to YTArray avoids creating a YTQuantity with # size > 1 out_arr = YTArray(np.asarray(out_arr), unit) else: out_arr = ret_class(np.asarray(out_arr), unit) if out is not None: out_orig[0].flat[:] = out.flat[:] if isinstance(out_orig[0], YTArray): out_orig[0].units = unit return out_arr def copy(self, order='C'): return type(self)(np.copy(np.asarray(self)), self.units) def __array_finalize__(self, obj): if obj is None and hasattr(self, 'units'): return self.units = getattr(obj, 'units', NULL_UNIT) def __pos__(self): """ Posify the data. """ # this needs to be defined for all numpy versions, see # numpy issue #9081 return type(self)(super(YTArray, self).__pos__(), self.units) @return_arr def dot(self, b, out=None): return super(YTArray, self).dot(b), self.units*b.units def __reduce__(self): """Pickle reduction method See the documentation for the standard library pickle module: http://docs.python.org/2/library/pickle.html Unit metadata is encoded in the zeroth element of third element of the returned tuple, itself a tuple used to restore the state of the ndarray. This is always defined for numpy arrays. """ np_ret = super(YTArray, self).__reduce__() obj_state = np_ret[2] unit_state = (((str(self.units), self.units.registry.lut),) + obj_state[:],) new_ret = np_ret[:2] + unit_state + np_ret[3:] return new_ret def __setstate__(self, state): """Pickle setstate method This is called inside pickle.read() and restores the unit data from the metadata extracted in __reduce__ and then serialized by pickle. """ super(YTArray, self).__setstate__(state[1:]) try: unit, lut = state[0] except TypeError: # this case happens when we try to load an old pickle file # created before we serialized the unit symbol lookup table # into the pickle file unit, lut = str(state[0]), default_unit_symbol_lut.copy() # need to fix up the lut if the pickle was saved prior to PR #1728 # when the pickle format changed if len(lut['m']) == 2: lut.update(default_unit_symbol_lut) for k, v in [(k, v) for k, v in lut.items() if len(v) == 2]: lut[k] = v + (0.0, r'\rm{' + k.replace('_', '\ ') + '}') registry = UnitRegistry(lut=lut, add_default_symbols=False) self.units = Unit(unit, registry=registry) def __deepcopy__(self, memodict=None): """copy.deepcopy implementation This is necessary for stdlib deepcopy of arrays and quantities. """ if memodict is None: memodict = {} ret = super(YTArray, self).__deepcopy__(memodict) return type(self)(ret, copy.deepcopy(self.units)) class YTQuantity(YTArray): """ A scalar associated with a unit. Parameters ---------- input_scalar : an integer or floating point scalar The scalar to attach units to input_units : String unit specification, unit symbol object, or astropy units The units of the quantity. Powers must be specified using python syntax (cm**3, not cm^3). registry : A UnitRegistry object The registry to create units from. If input_units is already associated with a unit registry and this is specified, this will be used instead of the registry associated with the unit object. dtype : data-type The dtype of the array data. Examples -------- >>> from yt import YTQuantity >>> a = YTQuantity(1, 'cm') >>> b = YTQuantity(2, 'm') >>> a + b 201.0 cm >>> b + a 2.01 m NumPy ufuncs will pass through units where appropriate. >>> import numpy as np >>> a = YTQuantity(12, 'g/cm**3') >>> np.abs(a) 12 g/cm**3 and strip them when it would be annoying to deal with them. >>> print(np.log10(a)) 1.07918124605 YTQuantity is tightly integrated with yt datasets: >>> import yt >>> ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030') >>> a = ds.quan(5, 'code_length') >>> a.in_cgs() 1.543e+25 cm This is equivalent to: >>> b = YTQuantity(5, 'code_length', registry=ds.unit_registry) >>> np.all(a == b) True """ def __new__(cls, input_scalar, input_units=None, registry=None, dtype=np.float64, bypass_validation=False): if not isinstance(input_scalar, (numeric_type, np.number, np.ndarray)): raise RuntimeError("YTQuantity values must be numeric") ret = YTArray.__new__(cls, input_scalar, input_units, registry, dtype=dtype, bypass_validation=bypass_validation) if ret.size > 1: raise RuntimeError("YTQuantity instances must be scalars") return ret def __repr__(self): return str(self) def validate_numpy_wrapper_units(v, arrs): if not any(isinstance(a, YTArray) for a in arrs): return v if not all(isinstance(a, YTArray) for a in arrs): raise RuntimeError("Not all of your arrays are YTArrays.") a1 = arrs[0] if not all(a.units == a1.units for a in arrs[1:]): raise RuntimeError("Your arrays must have identical units.") v.units = a1.units return v def uconcatenate(arrs, axis=0): """Concatenate a sequence of arrays. This wrapper around numpy.concatenate preserves units. All input arrays must have the same units. See the documentation of numpy.concatenate for full details. Examples -------- >>> A = yt.YTArray([1, 2, 3], 'cm') >>> B = yt.YTArray([2, 3, 4], 'cm') >>> uconcatenate((A, B)) YTArray([ 1., 2., 3., 2., 3., 4.]) cm """ v = np.concatenate(arrs, axis=axis) v = validate_numpy_wrapper_units(v, arrs) return v def ucross(arr1, arr2, registry=None, axisa=-1, axisb=-1, axisc=-1, axis=None): """Applies the cross product to two YT arrays. This wrapper around numpy.cross preserves units. See the documentation of numpy.cross for full details. """ v = np.cross(arr1, arr2, axisa=axisa, axisb=axisb, axisc=axisc, axis=axis) units = arr1.units * arr2.units arr = YTArray(v, units, registry=registry) return arr def uintersect1d(arr1, arr2, assume_unique=False): """Find the sorted unique elements of the two input arrays. A wrapper around numpy.intersect1d that preserves units. All input arrays must have the same units. See the documentation of numpy.intersect1d for full details. Examples -------- >>> A = yt.YTArray([1, 2, 3], 'cm') >>> B = yt.YTArray([2, 3, 4], 'cm') >>> uintersect1d(A, B) YTArray([ 2., 3.]) cm """ v = np.intersect1d(arr1, arr2, assume_unique=assume_unique) v = validate_numpy_wrapper_units(v, [arr1, arr2]) return v def uunion1d(arr1, arr2): """Find the union of two arrays. A wrapper around numpy.intersect1d that preserves units. All input arrays must have the same units. See the documentation of numpy.intersect1d for full details. Examples -------- >>> A = yt.YTArray([1, 2, 3], 'cm') >>> B = yt.YTArray([2, 3, 4], 'cm') >>> uunion1d(A, B) YTArray([ 1., 2., 3., 4.]) cm """ v = np.union1d(arr1, arr2) v = validate_numpy_wrapper_units(v, [arr1, arr2]) return v def unorm(data, ord=None, axis=None, keepdims=False): """Matrix or vector norm that preserves units This is a wrapper around np.linalg.norm that preserves units. See the documentation for that function for descriptions of the keyword arguments. The keepdims argument is ignored if the version of numpy installed is older than numpy 1.10.0. """ if LooseVersion(np.__version__) < LooseVersion('1.10.0'): norm = np.linalg.norm(data, ord=ord, axis=axis) else: norm = np.linalg.norm(data, ord=ord, axis=axis, keepdims=keepdims) if norm.shape == (): return YTQuantity(norm, data.units) return YTArray(norm, data.units) def udot(op1, op2): """Matrix or vector dot product that preserves units This is a wrapper around np.dot that preserves units. """ dot = np.dot(op1.d, op2.d) units = op1.units*op2.units if dot.shape == (): return YTQuantity(dot, units) return YTArray(dot, units) def uvstack(arrs): """Stack arrays in sequence vertically (row wise) while preserving units This is a wrapper around np.vstack that preserves units. """ v = np.vstack(arrs) v = validate_numpy_wrapper_units(v, arrs) return v def uhstack(arrs): """Stack arrays in sequence horizontally (column wise) while preserving units This is a wrapper around np.hstack that preserves units. """ v = np.hstack(arrs) v = validate_numpy_wrapper_units(v, arrs) return v def ustack(arrs, axis=0): """Join a sequence of arrays along a new axis while preserving units The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if ``axis=0`` it will be the first dimension and if ``axis=-1`` it will be the last dimension. This is a wrapper around np.stack that preserves units. """ v = np.stack(arrs) v = validate_numpy_wrapper_units(v, arrs) return v def array_like_field(data, x, field): field = data._determine_fields(field)[0] if isinstance(field, tuple): finfo = data.ds._get_field_info(field[0],field[1]) else: finfo = data.ds._get_field_info(field) if finfo.sampling_type == 'particle': units = finfo.output_units else: units = finfo.units if isinstance(x, YTArray): arr = copy.deepcopy(x) arr.convert_to_units(units) return arr if isinstance(x, np.ndarray): return data.ds.arr(x, units) else: return data.ds.quan(x, units) def get_binary_op_return_class(cls1, cls2): if cls1 is cls2: return cls1 if cls1 in (np.ndarray, np.matrix, np.ma.masked_array) or issubclass(cls1, (numeric_type, np.number, list, tuple)): return cls2 if cls2 in (np.ndarray, np.matrix, np.ma.masked_array) or issubclass(cls2, (numeric_type, np.number, list, tuple)): return cls1 if issubclass(cls1, YTQuantity): return cls2 if issubclass(cls2, YTQuantity): return cls1 if issubclass(cls1, cls2): return cls1 if issubclass(cls2, cls1): return cls2 else: raise RuntimeError("Undefined operation for a YTArray subclass. " "Received operand types (%s) and (%s)" % (cls1, cls2)) def loadtxt(fname, dtype='float', delimiter='\t', usecols=None, comments='#'): r""" Load YTArrays with unit information from a text file. Each row in the text file must have the same number of values. Parameters ---------- fname : str Filename to read. dtype : data-type, optional Data-type of the resulting array; default: float. delimiter : str, optional The string used to separate values. By default, this is any whitespace. usecols : sequence, optional Which columns to read, with 0 being the first. For example, ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns. The default, None, results in all columns being read. comments : str, optional The character used to indicate the start of a comment; default: '#'. Examples -------- >>> temp, velx = yt.loadtxt("sphere.dat", usecols=(1,2), delimiter="\t") """ f = open(fname, 'r') next_one = False units = [] num_cols = -1 for line in f.readlines(): words = line.strip().split() if len(words) == 0: continue if line[0] == comments: if next_one: units = words[1:] if len(words) == 2 and words[1] == "Units": next_one = True else: # Here we catch the first line of numbers try: col_words = line.strip().split(delimiter) for word in col_words: float(word) num_cols = len(col_words) break except ValueError: mylog.warning("Unrecognized character at beginning of line: \"%s\"." % line[0]) f.close() if len(units) != num_cols: mylog.warning("Malformed or incomplete units header. Arrays will be " "dimensionless!") units = ["dimensionless"]*num_cols arrays = np.loadtxt(fname, dtype=dtype, comments=comments, delimiter=delimiter, converters=None, unpack=True, usecols=usecols, ndmin=0) if usecols is not None: units = [units[col] for col in usecols] mylog.info("Array units: %s" % ", ".join(units)) return tuple([YTArray(arr, unit) for arr, unit in zip(arrays, units)]) def savetxt(fname, arrays, fmt='%.18e', delimiter='\t', header='', footer='', comments='#'): r""" Write YTArrays with unit information to a text file. Parameters ---------- fname : str The file to write the YTArrays to. arrays : list of YTArrays or single YTArray The array(s) to write to the file. fmt : str or sequence of strs, optional A single format (%10.5f), or a sequence of formats. delimiter : str, optional String or character separating columns. header : str, optional String that will be written at the beginning of the file, before the unit header. footer : str, optional String that will be written at the end of the file. comments : str, optional String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# ', as expected by e.g. ``yt.loadtxt``. Examples -------- >>> sp = ds.sphere("c", (100,"kpc")) >>> a = sp["density"] >>> b = sp["temperature"] >>> c = sp["velocity_x"] >>> yt.savetxt("sphere.dat", [a,b,c], header='My sphere stuff', delimiter="\t") """ if not isinstance(arrays, list): arrays = [arrays] units = [] for array in arrays: if hasattr(array, "units"): units.append(str(array.units)) else: units.append("dimensionless") if header != '': header += '\n' header += " Units\n " + '\t'.join(units) np.savetxt(fname, np.transpose(arrays), header=header, fmt=fmt, delimiter=delimiter, footer=footer, newline='\n', comments=comments)
[((2243, 2249), 'yt.units.unit_object.Unit', 'Unit', ([], {}), '()\n', (2247, 2249), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((2777, 2812), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (2786, 2812), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((2857, 2892), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (2866, 2892), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((3011, 3046), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (3020, 3046), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((3101, 3136), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (3110, 3136), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((3183, 3218), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (3192, 3218), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((3276, 3311), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (3285, 3311), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((9333, 9368), 'yt.utilities.lru_cache.lru_cache', 'lru_cache', ([], {'maxsize': '(128)', 'typed': '(False)'}), '(maxsize=128, typed=False)\n', (9342, 9368), False, 'from yt.utilities.lru_cache import lru_cache\n'), ((2466, 2477), 'functools.wraps', 'wraps', (['func'], {}), '(func)\n', (2471, 2477), False, 'from functools import wraps\n'), ((9764, 9808), 'yt.units.dimensions.em_dimensions.get', 'em_dimensions.get', (['my_units.dimensions', 'None'], {}), '(my_units.dimensions, None)\n', (9781, 9808), False, 'from yt.units.dimensions import angle, current_mks, dimensionless, em_dimensions\n'), ((55134, 55165), 'numpy.concatenate', 'np.concatenate', (['arrs'], {'axis': 'axis'}), '(arrs, axis=axis)\n', (55148, 55165), True, 'import numpy as np\n'), ((55490, 55560), 'numpy.cross', 'np.cross', (['arr1', 'arr2'], {'axisa': 'axisa', 'axisb': 'axisb', 'axisc': 'axisc', 'axis': 'axis'}), '(arr1, arr2, axisa=axisa, axisb=axisb, axisc=axisc, axis=axis)\n', (55498, 55560), True, 'import numpy as np\n'), ((56128, 56183), 'numpy.intersect1d', 'np.intersect1d', (['arr1', 'arr2'], {'assume_unique': 'assume_unique'}), '(arr1, arr2, assume_unique=assume_unique)\n', (56142, 56183), True, 'import numpy as np\n'), ((56672, 56694), 'numpy.union1d', 'np.union1d', (['arr1', 'arr2'], {}), '(arr1, arr2)\n', (56682, 56694), True, 'import numpy as np\n'), ((57601, 57621), 'numpy.dot', 'np.dot', (['op1.d', 'op2.d'], {}), '(op1.d, op2.d)\n', (57607, 57621), True, 'import numpy as np\n'), ((57922, 57937), 'numpy.vstack', 'np.vstack', (['arrs'], {}), '(arrs)\n', (57931, 57937), True, 'import numpy as np\n'), ((58177, 58192), 'numpy.hstack', 'np.hstack', (['arrs'], {}), '(arrs)\n', (58186, 58192), True, 'import numpy as np\n'), ((58639, 58653), 'numpy.stack', 'np.stack', (['arrs'], {}), '(arrs)\n', (58647, 58653), True, 'import numpy as np\n'), ((62003, 62133), 'numpy.loadtxt', 'np.loadtxt', (['fname'], {'dtype': 'dtype', 'comments': 'comments', 'delimiter': 'delimiter', 'converters': 'None', 'unpack': '(True)', 'usecols': 'usecols', 'ndmin': '(0)'}), '(fname, dtype=dtype, comments=comments, delimiter=delimiter,\n converters=None, unpack=True, usecols=usecols, ndmin=0)\n', (62013, 62133), True, 'import numpy as np\n'), ((9700, 9745), 'yt.units.unit_object.Unit', 'Unit', (['other_units'], {'registry': 'my_units.registry'}), '(other_units, registry=my_units.registry)\n', (9704, 9745), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((9982, 10032), 'yt.utilities.exceptions.YTEquivalentDimsError', 'YTEquivalentDimsError', (['my_units', 'other_units', 'base'], {}), '(my_units, other_units, base)\n', (10003, 10032), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((10101, 10194), 'yt.utilities.exceptions.YTUnitConversionError', 'YTUnitConversionError', (['my_units', 'my_units.dimensions', 'other_units', 'other_units.dimensions'], {}), '(my_units, my_units.dimensions, other_units,\n other_units.dimensions)\n', (10122, 10194), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((25654, 25694), 'yt.units.unit_object.Unit', 'Unit', (['unit'], {'registry': 'self.units.registry'}), '(unit, registry=self.units.registry)\n', (25658, 25694), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((27284, 27298), 'numpy.array', 'np.array', (['self'], {}), '(self)\n', (27292, 27298), True, 'import numpy as np\n'), ((32434, 32453), 'h5py.File', 'h5py.File', (['filename'], {}), '(filename)\n', (32443, 32453), False, 'import h5py\n'), ((34022, 34041), 'h5py.File', 'h5py.File', (['filename'], {}), '(filename)\n', (34031, 34041), False, 'import h5py\n'), ((34456, 34509), 'pint.UnitRegistry', 'UnitRegistry', ([], {'lut': 'unit_lut', 'add_default_symbols': '(False)'}), '(lut=unit_lut, add_default_symbols=False)\n', (34468, 34509), False, 'from pint import UnitRegistry\n'), ((34719, 34733), 'numpy.array', 'np.array', (['self'], {}), '(self)\n', (34727, 34733), True, 'import numpy as np\n'), ((35237, 35255), 'numpy.ones_like', 'np.ones_like', (['self'], {}), '(self)\n', (35249, 35255), True, 'import numpy as np\n'), ((35629, 35657), 'distutils.version.LooseVersion', 'LooseVersion', (['np.__version__'], {}), '(np.__version__)\n', (35641, 35657), False, 'from distutils.version import LooseVersion\n'), ((35660, 35682), 'distutils.version.LooseVersion', 'LooseVersion', (['"""1.13.0"""'], {}), "('1.13.0')\n", (35672, 35682), False, 'from distutils.version import LooseVersion\n'), ((51752, 51800), 'pint.UnitRegistry', 'UnitRegistry', ([], {'lut': 'lut', 'add_default_symbols': '(False)'}), '(lut=lut, add_default_symbols=False)\n', (51764, 51800), False, 'from pint import UnitRegistry\n'), ((51822, 51851), 'yt.units.unit_object.Unit', 'Unit', (['unit'], {'registry': 'registry'}), '(unit, registry=registry)\n', (51826, 51851), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((57144, 57172), 'distutils.version.LooseVersion', 'LooseVersion', (['np.__version__'], {}), '(np.__version__)\n', (57156, 57172), False, 'from distutils.version import LooseVersion\n'), ((57175, 57197), 'distutils.version.LooseVersion', 'LooseVersion', (['"""1.10.0"""'], {}), "('1.10.0')\n", (57187, 57197), False, 'from distutils.version import LooseVersion\n'), ((57214, 57254), 'numpy.linalg.norm', 'np.linalg.norm', (['data'], {'ord': 'ord', 'axis': 'axis'}), '(data, ord=ord, axis=axis)\n', (57228, 57254), True, 'import numpy as np\n'), ((57280, 57339), 'numpy.linalg.norm', 'np.linalg.norm', (['data'], {'ord': 'ord', 'axis': 'axis', 'keepdims': 'keepdims'}), '(data, ord=ord, axis=axis, keepdims=keepdims)\n', (57294, 57339), True, 'import numpy as np\n'), ((59106, 59122), 'copy.deepcopy', 'copy.deepcopy', (['x'], {}), '(x)\n', (59119, 59122), False, 'import copy\n'), ((61837, 61926), 'yt.utilities.logger.ytLogger.warning', 'mylog.warning', (['"""Malformed or incomplete units header. Arrays will be dimensionless!"""'], {}), "(\n 'Malformed or incomplete units header. Arrays will be dimensionless!')\n", (61850, 61926), True, 'from yt.utilities.logger import ytLogger as mylog\n'), ((63925, 63945), 'numpy.transpose', 'np.transpose', (['arrays'], {}), '(arrays)\n', (63937, 63945), True, 'import numpy as np\n'), ((5145, 5160), 'numpy.any', 'np.any', (['inps[0]'], {}), '(inps[0])\n', (5151, 5160), True, 'import numpy as np\n'), ((5162, 5177), 'numpy.any', 'np.any', (['inps[1]'], {}), '(inps[1])\n', (5168, 5177), True, 'import numpy as np\n'), ((5208, 5223), 'numpy.bool_', 'np.bool_', (['(False)'], {}), '(False)\n', (5216, 5223), True, 'import numpy as np\n'), ((5815, 5830), 'numpy.any', 'np.any', (['inps[0]'], {}), '(inps[0])\n', (5821, 5830), True, 'import numpy as np\n'), ((5832, 5847), 'numpy.any', 'np.any', (['inps[1]'], {}), '(inps[1])\n', (5838, 5847), True, 'import numpy as np\n'), ((5878, 5893), 'numpy.bool_', 'np.bool_', (['(False)'], {}), '(False)\n', (5886, 5893), True, 'import numpy as np\n'), ((8495, 8548), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['op_string', 'inp.units', 'ret.units'], {}), '(op_string, inp.units, ret.units)\n', (8515, 8548), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((8767, 8778), 'numpy.any', 'np.any', (['ret'], {}), '(ret)\n', (8773, 8778), True, 'import numpy as np\n'), ((8798, 8855), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['op_string', 'inp.units', 'dimensionless'], {}), '(op_string, inp.units, dimensionless)\n', (8818, 8855), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((9214, 9270), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['op_string', 'this.units', 'other.units'], {}), '(op_string, this.units, other.units)\n', (9234, 9270), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((18365, 18371), 'yt.units.unit_object.Unit', 'Unit', ([], {}), '()\n', (18369, 18371), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((19703, 19744), 'numpy.subtract', 'np.subtract', (['self', '(offset * self.uq)', 'self'], {}), '(self, offset * self.uq, self)\n', (19714, 19744), True, 'import numpy as np\n'), ((26463, 26512), 'yt.utilities.exceptions.YTInvalidUnitEquivalence', 'YTInvalidUnitEquivalence', (['equiv', 'self.units', 'unit'], {}), '(equiv, self.units, unit)\n', (26487, 26512), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((30611, 30625), 'pint.UnitRegistry', 'UnitRegistry', ([], {}), '()\n', (30623, 30625), False, 'from pint import UnitRegistry\n'), ((32308, 32345), 'yt.extern.six.moves.cPickle.dumps', 'pickle.dumps', (['self.units.registry.lut'], {}), '(self.units.registry.lut)\n', (32320, 32345), True, 'from yt.extern.six.moves import cPickle as pickle\n'), ((36358, 36385), 'numpy.add', 'np.add', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (36364, 36385), True, 'import numpy as np\n'), ((37090, 37122), 'numpy.subtract', 'np.subtract', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (37101, 37122), True, 'import numpy as np\n'), ((37896, 37928), 'numpy.multiply', 'np.multiply', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (37907, 37928), True, 'import numpy as np\n'), ((38530, 38560), 'numpy.divide', 'np.divide', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (38539, 38560), True, 'import numpy as np\n'), ((39069, 39104), 'numpy.true_divide', 'np.true_divide', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (39083, 39104), True, 'import numpy as np\n'), ((39618, 39654), 'numpy.floor_divide', 'np.floor_divide', (['self', 'oth'], {'out': 'self'}), '(self, oth, out=self)\n', (39633, 39654), True, 'import numpy as np\n'), ((39930, 39966), 'numpy.bitwise_or', 'np.bitwise_or', (['self', 'other'], {'out': 'self'}), '(self, other, out=self)\n', (39943, 39966), True, 'import numpy as np\n'), ((40247, 40284), 'numpy.bitwise_xor', 'np.bitwise_xor', (['self', 'other'], {'out': 'self'}), '(self, other, out=self)\n', (40261, 40284), True, 'import numpy as np\n'), ((40565, 40602), 'numpy.bitwise_and', 'np.bitwise_and', (['self', 'other'], {'out': 'self'}), '(self, other, out=self)\n', (40579, 40602), True, 'import numpy as np\n'), ((52165, 52190), 'copy.deepcopy', 'copy.deepcopy', (['self.units'], {}), '(self.units)\n', (52178, 52190), False, 'import copy\n'), ((5297, 5312), 'numpy.bool_', 'np.bool_', (['(False)'], {}), '(False)\n', (5305, 5312), True, 'import numpy as np\n'), ((5967, 5982), 'numpy.bool_', 'np.bool_', (['(False)'], {}), '(False)\n', (5975, 5982), True, 'import numpy as np\n'), ((6780, 6808), 'yt.units.unit_object.Unit', 'Unit', ([], {'registry': 'unit.registry'}), '(registry=unit.registry)\n', (6784, 6808), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((7217, 7258), 'yt.utilities.exceptions.YTIterableUnitCoercionError', 'YTIterableUnitCoercionError', (['input_object'], {}), '(input_object)\n', (7244, 7258), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((8342, 8362), 'numpy.any', 'np.any', (['other_object'], {}), '(other_object)\n', (8348, 8362), True, 'import numpy as np\n'), ((16956, 17136), 'yt.units.unit_object.UnitParseError', 'UnitParseError', (['("""Code units used without referring to a dataset. \nPerhaps you meant to do something like this instead: \nds.arr(%s, "%s")"""\n % (input_array, input_units))'], {}), '(\n """Code units used without referring to a dataset. \nPerhaps you meant to do something like this instead: \nds.arr(%s, "%s")"""\n % (input_array, input_units))\n', (16970, 17136), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((18184, 18220), 'numpy.asarray', 'np.asarray', (['input_array'], {'dtype': 'dtype'}), '(input_array, dtype=dtype)\n', (18194, 18220), True, 'import numpy as np\n'), ((18797, 18833), 'yt.units.unit_object.Unit', 'Unit', (['input_units'], {'registry': 'registry'}), '(input_units, registry=registry)\n', (18801, 18833), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((21985, 22041), 'numpy.subtract', 'np.subtract', (['new_array', '(offset * new_array.uq)', 'new_array'], {}), '(new_array, offset * new_array.uq, new_array)\n', (21996, 22041), True, 'import numpy as np\n'), ((46015, 46044), 'numpy.array', 'np.array', (['out_arr'], {'copy': '(False)'}), '(out_arr, copy=False)\n', (46023, 46044), True, 'import numpy as np\n'), ((46845, 46868), 'numpy.asarray', 'np.asarray', (['out_orig[0]'], {}), '(out_orig[0])\n', (46855, 46868), True, 'import numpy as np\n'), ((48678, 48707), 'numpy.array', 'np.array', (['out_arr'], {'copy': '(False)'}), '(out_arr, copy=False)\n', (48686, 48707), True, 'import numpy as np\n'), ((5449, 5484), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['ufunc', '*units'], {}), '(ufunc, *units)\n', (5469, 5484), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((8429, 8448), 'numpy.any', 'np.any', (['this_object'], {}), '(this_object)\n', (8435, 8448), True, 'import numpy as np\n'), ((16543, 16579), 'numpy.asarray', 'np.asarray', (['input_array'], {'dtype': 'dtype'}), '(input_array, dtype=dtype)\n', (16553, 16579), True, 'import numpy as np\n'), ((17693, 17729), 'yt.units.unit_object.Unit', 'Unit', (['input_units'], {'registry': 'registry'}), '(input_units, registry=registry)\n', (17697, 17729), False, 'from yt.units.unit_object import Unit, UnitParseError\n'), ((40993, 41034), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['"""power"""', 'power.unit'], {}), "('power', power.unit)\n", (41013, 41034), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((46177, 46194), 'numpy.array', 'np.array', (['out_arr'], {}), '(out_arr)\n', (46185, 46194), True, 'import numpy as np\n'), ((46541, 46570), 'numpy.array', 'np.array', (['out_arr'], {'copy': '(False)'}), '(out_arr, copy=False)\n', (46549, 46570), True, 'import numpy as np\n'), ((47037, 47052), 'numpy.asarray', 'np.asarray', (['inp'], {}), '(inp)\n', (47047, 47052), True, 'import numpy as np\n'), ((49603, 49619), 'numpy.asarray', 'np.asarray', (['self'], {}), '(self)\n', (49613, 49619), True, 'import numpy as np\n'), ((51361, 51391), 'yt.units.unit_lookup_table.default_unit_symbol_lut.copy', 'default_unit_symbol_lut.copy', ([], {}), '()\n', (51389, 51391), False, 'from yt.units.unit_lookup_table import default_unit_symbol_lut\n'), ((61704, 61781), 'yt.utilities.logger.ytLogger.warning', 'mylog.warning', (['(\'Unrecognized character at beginning of line: "%s".\' % line[0])'], {}), '(\'Unrecognized character at beginning of line: "%s".\' % line[0])\n', (61717, 61781), True, 'from yt.utilities.logger import ytLogger as mylog\n'), ((4917, 4958), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['ufunc', 'unit1', 'unit2'], {}), '(ufunc, unit1, unit2)\n', (4937, 4958), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((6139, 6174), 'yt.utilities.exceptions.YTUnitOperationError', 'YTUnitOperationError', (['ufunc', '*units'], {}), '(ufunc, *units)\n', (6159, 6174), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((26317, 26366), 'yt.utilities.exceptions.YTInvalidUnitEquivalence', 'YTInvalidUnitEquivalence', (['equiv', 'self.units', 'unit'], {}), '(equiv, self.units, unit)\n', (26341, 26366), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((28120, 28138), 'sympy.Rational', 'Rational', (['exponent'], {}), '(exponent)\n', (28128, 28138), False, 'from sympy import Rational\n'), ((29632, 29650), 'sympy.Rational', 'Rational', (['exponent'], {}), '(exponent)\n', (29640, 29650), False, 'from sympy import Rational\n'), ((31007, 31020), 'sympy.Rational', 'Rational', (['pow'], {}), '(pow)\n', (31015, 31020), False, 'from sympy import Rational\n'), ((46483, 46500), 'numpy.array', 'np.array', (['out_arr'], {}), '(out_arr)\n', (46491, 46500), True, 'import numpy as np\n'), ((48143, 48162), 'numpy.asarray', 'np.asarray', (['inps[0]'], {}), '(inps[0])\n', (48153, 48162), True, 'import numpy as np\n'), ((48164, 48183), 'numpy.asarray', 'np.asarray', (['inps[1]'], {}), '(inps[1])\n', (48174, 48183), True, 'import numpy as np\n'), ((6251, 6282), 'yt.utilities.exceptions.YTUfuncUnitError', 'YTUfuncUnitError', (['ufunc', '*units'], {}), '(ufunc, *units)\n', (6267, 6282), False, 'from yt.utilities.exceptions import YTUnitOperationError, YTUnitConversionError, YTUfuncUnitError, YTIterableUnitCoercionError, YTInvalidUnitEquivalence, YTEquivalentDimsError\n'), ((17955, 17989), 'numpy.array', 'np.array', (['input_array'], {'dtype': 'dtype'}), '(input_array, dtype=dtype)\n', (17963, 17989), True, 'import numpy as np\n'), ((48895, 48914), 'numpy.asarray', 'np.asarray', (['out_arr'], {}), '(out_arr)\n', (48905, 48914), True, 'import numpy as np\n'), ((49206, 49225), 'numpy.asarray', 'np.asarray', (['out_arr'], {}), '(out_arr)\n', (49216, 49225), True, 'import numpy as np\n'), ((49295, 49314), 'numpy.asarray', 'np.asarray', (['out_arr'], {}), '(out_arr)\n', (49305, 49314), True, 'import numpy as np\n')]
MahmoudMagdi20/django_rest_blog_api
src/posts/api/serializers.py
e1969c75e20b4d807baf26051924a0b99a23b4dc
from rest_framework import serializers from posts.models import Post class PostCreateUpdateSerializer(serializers.ModelSerializer): class Meta: model = Post fields = [ #'id', 'title', #'slug', 'content', 'publish', ] post_detail_url = serializers.HyperlinkedIdentityField( view_name='posts-api:detail', lookup_field='slug', ) class PostDetailSerializer(serializers.ModelSerializer): url = post_detail_url user = serializers.SerializerMethodField() image = serializers.SerializerMethodField() html = serializers.SerializerMethodField() class Meta: model = Post fields = [ 'url', 'id', 'title', 'slug', 'content', 'publish', 'user', 'image', 'html', ] def get_html(self, obj): return obj.get_markdown() def get_user(self, obj): return str(obj.user.username) def get_image(self, obj): try: image = obj.image.url except: image = None return image class PostListSerializer(serializers.ModelSerializer): url = post_detail_url user = serializers.SerializerMethodField() class Meta: model = Post fields = [ 'url', 'user', 'title', 'content', 'publish', ] def get_user(self, obj): return str(obj.user.username)
[((326, 417), 'rest_framework.serializers.HyperlinkedIdentityField', 'serializers.HyperlinkedIdentityField', ([], {'view_name': '"""posts-api:detail"""', 'lookup_field': '"""slug"""'}), "(view_name='posts-api:detail',\n lookup_field='slug')\n", (362, 417), False, 'from rest_framework import serializers\n'), ((524, 559), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (557, 559), False, 'from rest_framework import serializers\n'), ((572, 607), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (605, 607), False, 'from rest_framework import serializers\n'), ((619, 654), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (652, 654), False, 'from rest_framework import serializers\n'), ((1272, 1307), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (1305, 1307), False, 'from rest_framework import serializers\n')]
98llm/tir-script-samples
Protheus_WebApp/Modules/SIGAGTP/GTPA036ETestCase.py
0bff8393b79356aa562e9e6512c11ee6e039b177
from tir import Webapp import unittest class GTPA036E(unittest.TestCase): @classmethod def setUpClass(inst): inst.oHelper = Webapp() inst.oHelper.Setup("SIGAGTP", "05/08/2020", "T1", "D MG 01 ") inst.oHelper.Program('GTPA036') def test_GTPA036E_CT001(self): self.oHelper.SetButton('Avançar') self.oHelper.ClickLabel("Arquivo não formatado") self.oHelper.SetButton('Avançar') self.oHelper.SetValue('XXX_DATADE', '02/08/2020') self.oHelper.SetValue('XXX_DATATE', '07/08/2020') self.oHelper.ScrollGrid(column='Agência', match_value='000048', grid_number=1) '''self.oHelper.ClickGridCell("", row=2, grid_number=1)''' self.oHelper.ClickBox("", contents_list='', select_all=False, grid_number=1) self.oHelper.SetButton('Concluir') self.oHelper.SetButton('Fechar') self.oHelper.AssertTrue() @classmethod def tearDownClass(inst): inst.oHelper.TearDown() if __name__ == '__main__': unittest.main()
[((1069, 1084), 'unittest.main', 'unittest.main', ([], {}), '()\n', (1082, 1084), False, 'import unittest\n'), ((130, 138), 'tir.Webapp', 'Webapp', ([], {}), '()\n', (136, 138), False, 'from tir import Webapp\n')]
lovebirdegg/nnms-server
code_tmpl/views.py
9fd4563ccca9f29add375d346cdd1c2dd636c512
# @Time : {time} # @Author : code_generator from rest_framework.viewsets import ModelViewSet from rest_framework.generics import ListAPIView from rest_framework.filters import SearchFilter, OrderingFilter from rest_framework.response import Response from rest_framework.decorators import api_view,authentication_classes,permission_classes,action from common.custom import CommonPagination, RbacPermission from django_filters.rest_framework import DjangoFilterBackend from django.http import HttpResponse,FileResponse,JsonResponse from rest_framework_jwt.authentication import JSONWebTokenAuthentication from rest_framework.permissions import IsAuthenticated from rest_xops.basic import XopsResponse from rest_xops.code import * from django.db.models import Q from django.apps import apps from ..models import {model_camel_case_name} from django.contrib.contenttypes.models import ContentType from ..serializers.{model_name}_serializers import * class {model_camel_case_name}View(ModelViewSet): queryset = {model_camel_case_name}.objects.all() serializer_class = {model_camel_case_name}Serializer filter_backends = (DjangoFilterBackend, SearchFilter,OrderingFilter) pagination_class = CommonPagination ordering_fields = ('id',) authentication_classes = (JSONWebTokenAuthentication,) permission_classes = (IsAuthenticated,) filter_fields = ({filter_fields}) search_fields = ({search_fields})
[]
art19/netuitive-diamond
src/collectors/heartbeat/heartbeat.py
57f61f2444e6f3d3692b4ee989415939bfaa932e
# coding=utf-8 """ Send a value of 1 as a heartbeat every time this collector is invoked. #### Dependencies None #### Usage Add the collector config as : enabled = True path = netuitive Metrics are collected as : - metrics.heartbeat Netuitive Change History ======================== DVG 2016/11/14 Initial version. """ import diamond.collector from diamond.utils.config import load_config as load_server_config try: import netuitive except ImportError: netuitive = None class HeartbeatCollector(diamond.collector.Collector): def __init__(self, *args, **kwargs): super(HeartbeatCollector, self).__init__(*args, **kwargs) self.hostname = self.get_hostname() self.ttl = self.config['ttl'] self.connection_timeout = 5 if not netuitive: self.log.error('netuitive import failed. Heartbeat collector disabled') self.enabled = False return try: self.version = self._get_version() if 'netuitive_connection_timeout' in self.config: self.connection_timeout = int(self.config['netuitive_connection_timeout']) self.api = netuitive.Client(url=self.config['netuitive_url'], api_key=self.config['netuitive_api_key'], agent=self.version, connection_timeout=self.connection_timeout) except Exception as e: self.log.debug(e) def collect(self): check = netuitive.Check('heartbeat', self.hostname, self.ttl) self.api.post_check(check)
[((1565, 1618), 'netuitive.Check', 'netuitive.Check', (['"""heartbeat"""', 'self.hostname', 'self.ttl'], {}), "('heartbeat', self.hostname, self.ttl)\n", (1580, 1618), False, 'import netuitive\n'), ((1187, 1353), 'netuitive.Client', 'netuitive.Client', ([], {'url': "self.config['netuitive_url']", 'api_key': "self.config['netuitive_api_key']", 'agent': 'self.version', 'connection_timeout': 'self.connection_timeout'}), "(url=self.config['netuitive_url'], api_key=self.config[\n 'netuitive_api_key'], agent=self.version, connection_timeout=self.\n connection_timeout)\n", (1203, 1353), False, 'import netuitive\n')]
vitorebatista/AVEMH
process_script/stat.py
1c0bea3ae6c35729b80ba49b9663ce83ea43922d
import numpy as np import pandas as pd import sys markets = ["hangseng", "dax", "ftse", "sp", "nikkei"] market = markets[int(sys.argv[1])-1] # read GD data file dat = pd.read_csv("./num_res/{}.GD.csv".format(market)) # split into two experiments exp1_GD = dat[dat.columns[:5]] exp2_GD = dat[dat.columns[5:]] # calculate statistics stat1_GD = pd.DataFrame([exp1_GD.min(), exp1_GD.median(), exp1_GD.std()]) stat1_GD.index = ["Best", "Median", "Std."] stat2_GD = pd.DataFrame([exp2_GD.min(), exp2_GD.median(), exp2_GD.std()]) stat2_GD.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_GD = stat1_GD.loc["Median"].sort_values() best1_GD = list(meds1_GD.index[:2]) meds2_GD = stat2_GD.loc["Median"].sort_values() best2_GD = list(meds2_GD.index[:2]) print("{}.GD:".format(market), best1_GD[0], best1_GD[1]) # print("{}.GD:".format(market), best2_GD[0], best2_GD[1]) # TODO: check error # read Spacing data file dat = pd.read_csv("./num_res/{}.Spacing.csv".format(market)) # split into two experiments exp1_Spacing = dat[dat.columns[:5]] exp2_Spacing = dat[dat.columns[5:]] # calculate statistics stat1_Spacing = pd.DataFrame( [exp1_Spacing.min(), exp1_Spacing.median(), exp1_Spacing.std()]) stat1_Spacing.index = ["Best", "Median", "Std."] stat2_Spacing = pd.DataFrame( [exp2_Spacing.min(), exp2_Spacing.median(), exp2_Spacing.std()]) stat2_Spacing.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_Spacing = stat1_Spacing.loc["Median"].sort_values() best1_Spacing = list(meds1_Spacing.index[:2]) meds2_Spacing = stat2_Spacing.loc["Median"].sort_values() best2_Spacing = list(meds2_Spacing.index[:2]) print("{}.Spacing:".format(market), best1_Spacing[0], best1_Spacing[1]) # print("{}.Spacing:".format(market), best2_Spacing[0], best2_Spacing[1]) # TODO: check error # read MaxSpread data file dat = pd.read_csv("./num_res/{}.MaxSpread.csv".format(market)) # split into two experiments exp1_MaxSpread = dat[dat.columns[:5]] exp2_MaxSpread = dat[dat.columns[5:]] # calculate statistics stat1_MaxSpread = pd.DataFrame( [exp1_MaxSpread.max(), exp1_MaxSpread.median(), exp1_MaxSpread.std()]) stat1_MaxSpread.index = ["Best", "Median", "Std."] stat2_MaxSpread = pd.DataFrame( [exp2_MaxSpread.max(), exp2_MaxSpread.median(), exp2_MaxSpread.std()]) stat2_MaxSpread.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_MaxSpread = stat1_MaxSpread.loc["Median"].sort_values(ascending=False) best1_MaxSpread = list(meds1_MaxSpread.index[:2]) meds2_MaxSpread = stat2_MaxSpread.loc["Median"].sort_values(ascending=False) best2_MaxSpread = list(meds2_MaxSpread.index[:2]) print("{}.MaxSpread:".format(market), best1_MaxSpread[0], best1_MaxSpread[1]) # print("{}.MaxSpread:".format(market), best2_MaxSpread[0], best2_MaxSpread[1]) # TODO: check error # read Delta data file dat = pd.read_csv("./num_res/{}.Delta.csv".format(market)) # split into two experiments exp1_Delta = dat[dat.columns[:5]] exp2_Delta = dat[dat.columns[5:]] # calculate statistics stat1_Delta = pd.DataFrame( [exp1_Delta.min(), exp1_Delta.median(), exp1_Delta.std()]) stat1_Delta.index = ["Best", "Median", "Std."] stat2_Delta = pd.DataFrame( [exp2_Delta.min(), exp2_Delta.median(), exp2_Delta.std()]) stat2_Delta.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_Delta = stat1_Delta.loc["Median"].sort_values() best1_Delta = list(meds1_Delta.index[:2]) meds2_Delta = stat2_Delta.loc["Median"].sort_values() best2_Delta = list(meds2_Delta.index[:2]) print("{}.Delta:".format(market), best1_Delta[0], best1_Delta[1]) # print("{}.Delta:".format(market), best2_Delta[0], best2_Delta[1]) # TODO: check error # read IGD data file dat = pd.read_csv("./num_res/{}.IGD.csv".format(market)) # split into two experiments exp1_IGD = dat[dat.columns[:5]] exp2_IGD = dat[dat.columns[5:]] # calculate statistics stat1_IGD = pd.DataFrame([exp1_IGD.min(), exp1_IGD.median(), exp1_IGD.std()]) stat1_IGD.index = ["Best", "Median", "Std."] stat2_IGD = pd.DataFrame([exp2_IGD.min(), exp2_IGD.median(), exp2_IGD.std()]) stat2_IGD.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_IGD = stat1_IGD.loc["Median"].sort_values() best1_IGD = list(meds1_IGD.index[:2]) meds2_IGD = stat2_IGD.loc["Median"].sort_values() best2_IGD = list(meds2_IGD.index[:2]) print("{}.IGD:".format(market), best1_IGD[0], best1_IGD[1]) # print("{}.IGD:".format(market), best2_IGD[0], best2_IGD[1]) # TODO: check error # read Hypervolume data file dat = pd.read_csv("./num_res/{}.Hypervolume.csv".format(market)) # split into two experiments exp1_Hypervolume = dat[dat.columns[:5]] exp2_Hypervolume = dat[dat.columns[5:]] # calculate statistics stat1_Hypervolume = pd.DataFrame( [exp1_Hypervolume.max(), exp1_Hypervolume.median(), exp1_Hypervolume.std()]) stat1_Hypervolume.index = ["Best", "Median", "Std."] stat2_Hypervolume = pd.DataFrame( [exp2_Hypervolume.max(), exp2_Hypervolume.median(), exp2_Hypervolume.std()]) stat2_Hypervolume.index = ["Best", "Median", "Std."] # find best and second best algorithm meds1_Hypervolume = stat1_Hypervolume.loc["Median"].sort_values( ascending=False) best1_Hypervolume = list(meds1_Hypervolume.index[:2]) meds2_Hypervolume = stat2_Hypervolume.loc["Median"].sort_values( ascending=False) best2_Hypervolume = list(meds2_Hypervolume.index[:2]) print("{}.Hypervolume:".format(market), best1_Hypervolume[0], best1_Hypervolume[1]) # print("{}.Hypervolume:".format(market), # best2_Hypervolume[0], best2_Hypervolume[1]) # TODO: check error print("{}\n----------------------------------------------".format(market)) pd.options.display.float_format = '{:.2e}'.format stat1_overall = pd.concat( [stat1_GD, stat1_Spacing, stat1_MaxSpread, stat1_Delta, stat1_IGD, stat1_Hypervolume]) stat2_overall = pd.concat( [stat2_GD, stat2_Spacing, stat2_MaxSpread, stat2_Delta, stat2_IGD, stat2_Hypervolume]) arrays = [["GD", "GD", "GD", "Spacing", "Spacing", "Spacing", "MaxSpread", "MaxSpread", "MaxSpread", "Delta", "Delta", "Delta", "IGD", "IGD", "IGD", "Hypervolume", "Hypervolume", "Hypervolume"], stat1_overall.index ] index = pd.MultiIndex.from_arrays(arrays, names=["Metric", ""]) stat1_overall.index = index stat2_overall.index = index print(stat1_overall) print("----------------------------------------------") print(stat2_overall)
[((5899, 5999), 'pandas.concat', 'pd.concat', (['[stat1_GD, stat1_Spacing, stat1_MaxSpread, stat1_Delta, stat1_IGD,\n stat1_Hypervolume]'], {}), '([stat1_GD, stat1_Spacing, stat1_MaxSpread, stat1_Delta, stat1_IGD,\n stat1_Hypervolume])\n', (5908, 5999), True, 'import pandas as pd\n'), ((6019, 6119), 'pandas.concat', 'pd.concat', (['[stat2_GD, stat2_Spacing, stat2_MaxSpread, stat2_Delta, stat2_IGD,\n stat2_Hypervolume]'], {}), '([stat2_GD, stat2_Spacing, stat2_MaxSpread, stat2_Delta, stat2_IGD,\n stat2_Hypervolume])\n', (6028, 6119), True, 'import pandas as pd\n'), ((6383, 6438), 'pandas.MultiIndex.from_arrays', 'pd.MultiIndex.from_arrays', (['arrays'], {'names': "['Metric', '']"}), "(arrays, names=['Metric', ''])\n", (6408, 6438), True, 'import pandas as pd\n')]
zmlabe/ThicknessSensitivity
Scripts/calc_Utilities.py
6defdd897a61d7d1a02f34a9f4ec92b2b17b3075
""" Functions are useful untilities for SITperturb experiments Notes ----- Author : Zachary Labe Date : 13 August 2017 Usage ----- [1] calcDecJan(varx,vary,lat,lon,level,levsq) [2] calcDecJanFeb(varx,vary,lat,lon,level,levsq) [3] calc_indttest(varx,vary) [4] calc_weightedAve(var,lats) [5] calc_spatialCorr(varx,vary,lats,lons,weight) [6] calc_RMSE(varx,vary,lats,lons,weight) [7] calc_spatialCorrHeight(varx,vary,lats,lons,weight) [8] calc_spatialCorrHeightLev(varx,vary,lats,lons,weight,levelq) """ def calcDecJan(varx,vary,lat,lon,level,levsq): """ Function calculates average for December-January Parameters ---------- varx : 4d array or 5d array [year,month,lat,lon] or [year,month,lev,lat,lon] vary : 4d array or 5d array [year,month,lat,lon] or [year,month,lev,lat,lon] lat : 1d numpy array latitudes lon : 1d numpy array longitudes level : string Height of variable (surface or profile) levsq : integer number of levels Returns ------- varx_dj : 3d array or 4d array [year,lat,lon] or [year,lev,lat,lon] vary_dj : 3d array [year,lat,lon] or [year,lev,lat,lon] Usage ----- varx_dj,vary_dj = calcDecJan(varx,vary,lat,lon,level,levsq) """ print('\n>>> Using calcDecJan function!') ### Import modules import numpy as np ### Reshape for 3d variables if level == 'surface': varxravel = np.reshape(varx.copy(), (int(varx.shape[0]*12), int(lat.shape[0]),int(lon.shape[0]))) varyravel = np.reshape(vary.copy(), (int(vary.shape[0]*12), int(lat.shape[0]),int(lon.shape[0]))) varx_dj = np.empty((varx.shape[0]-1,lat.shape[0],lon.shape[0])) vary_dj = np.empty((vary.shape[0]-1,lat.shape[0],lon.shape[0]) ) for i in range(0,varxravel.shape[0]-12,12): counter = 0 if i >= 12: counter = i//12 djappendh = np.append(varxravel[11+i,:,:],varxravel[12+i,:,:]) djappendf = np.append(varyravel[11+i,:,:],varyravel[12+i,:,:]) varx_dj[counter,:,:] = np.nanmean(np.reshape(djappendh, (2,int(lat.shape[0]),int(lon.shape[0]))), axis=0) vary_dj[counter,:,:] = np.nanmean(np.reshape(djappendf, (2,int(lat.shape[0]),int(lon.shape[0]))), axis=0) ### Reshape for 4d variables elif level == 'profile': varxravel = np.reshape(varx.copy(), (int(varx.shape[0]*12.),levsq, int(lat.shape[0]),int(lon.shape[0]))) varyravel = np.reshape(vary.copy(), (int(vary.shape[0]*12.),levsq, int(lat.shape[0]),int(lon.shape[0]))) varx_dj = np.empty((int(varx.shape[0]-1),levsq, int(lat.shape[0]),int(lon.shape[0]))) vary_dj = np.empty((int(vary.shape[0]-1),levsq, int(lat.shape[0]),int(lon.shape[0])) ) for i in range(0,varxravel.shape[0]-12,12): counter = 0 if i >= 12: counter = i//12 djappendh = np.append(varxravel[11+i,:,:,:], varxravel[12+i,:,:,:]) djappendf = np.append(varyravel[11+i,:,:,:], varyravel[12+i,:,:,:]) varx_dj[counter,:,:] = np.nanmean(np.reshape(djappendh, (2,levsq,int(lat.shape[0]), int(lon.shape[0]))),axis=0) vary_dj[counter,:,:] = np.nanmean(np.reshape(djappendf, (2,levsq,int(lat.shape[0]), int(lon.shape[0]))),axis=0) else: print(ValueError('Selected wrong height - (surface or profile!)!')) print('Completed: Organized data by months (ON,DJ,FM)!') print('*Completed: Finished calcDecJan function!') return varx_dj,vary_dj ############################################################################### ############################################################################### ############################################################################### def calcDecJanFeb(varx,vary,lat,lon,level,levsq): """ Function calculates average for December-January-February Parameters ---------- varx : 4d array or 5d array [year,month,lat,lon] or [year,month,lev,lat,lon] vary : 4d array or 5d array [year,month,lat,lon] or [year,month,lev,lat,lon] lat : 1d numpy array latitudes lon : 1d numpy array longitudes level : string Height of variable (surface or profile) levsq : integer number of levels Returns ------- varx_djf : 3d array or 4d array [year,lat,lon] or [year,lev,lat,lon] vary_djf : 3d array [year,lat,lon] or [year,lev,lat,lon] Usage ----- varx_djf,vary_djf = calcDecJanFeb(varx,vary,lat,lon,level,levsq) """ print('\n>>> Using calcDecJan function!') ### Import modules import numpy as np ### Reshape for 3d variables if level == 'surface': varxravel = np.reshape(varx.copy(), (int(varx.shape[0]*12), int(lat.shape[0]),int(lon.shape[0]))) varyravel = np.reshape(vary.copy(), (int(vary.shape[0]*12), int(lat.shape[0]),int(lon.shape[0]))) varx_djf = np.empty((varx.shape[0]-1,lat.shape[0],lon.shape[0])) vary_djf = np.empty((vary.shape[0]-1,lat.shape[0],lon.shape[0]) ) for i in range(0,varxravel.shape[0]-12,12): counter = 0 if i >= 12: counter = i//12 djfappendh1 = np.append(varxravel[11+i,:,:],varxravel[12+i,:,:]) djfappendf1 = np.append(varyravel[11+i,:,:],varyravel[12+i,:,:]) djfappendh = np.append(djfappendh1,varxravel[13+i,:,:]) djfappendf = np.append(djfappendf1,varyravel[13+i,:,:]) varx_djf[counter,:,:] = np.nanmean(np.reshape(djfappendh, (3,int(lat.shape[0]),int(lon.shape[0]))), axis=0) vary_djf[counter,:,:] = np.nanmean(np.reshape(djfappendf, (3,int(lat.shape[0]),int(lon.shape[0]))), axis=0) ### Reshape for 4d variables elif level == 'profile': varxravel = np.reshape(varx.copy(), (int(varx.shape[0]*12.),levsq, int(lat.shape[0]),int(lon.shape[0]))) varyravel = np.reshape(vary.copy(), (int(vary.shape[0]*12.),levsq, int(lat.shape[0]),int(lon.shape[0]))) varx_djf = np.empty((int(varx.shape[0]-1),levsq, int(lat.shape[0]),int(lon.shape[0]))) vary_djf = np.empty((int(vary.shape[0]-1),levsq, int(lat.shape[0]),int(lon.shape[0])) ) for i in range(0,varxravel.shape[0]-12,12): counter = 0 if i >= 12: counter = i//12 djfappendh1 = np.append(varxravel[11+i,:,:,:], varxravel[12+i,:,:,:]) djfappendf1 = np.append(varyravel[11+i,:,:,:], varyravel[12+i,:,:,:]) djfappendh = np.append(djfappendh1, varxravel[13+i,:,:,:]) djfappendf = np.append(djfappendf1, varyravel[13+i,:,:,:]) varx_djf[counter,:,:] = np.nanmean(np.reshape(djfappendh, (3,levsq,int(lat.shape[0]), int(lon.shape[0]))),axis=0) vary_djf[counter,:,:] = np.nanmean(np.reshape(djfappendf, (3,levsq,int(lat.shape[0]), int(lon.shape[0]))),axis=0) else: print(ValueError('Selected wrong height - (surface or profile!)!')) print('Completed: Organized data by months (DJF)!') print('*Completed: Finished calcDecJanFeb function!') return varx_djf,vary_djf ############################################################################### ############################################################################### ############################################################################### def calc_indttest(varx,vary): """ Function calculates statistical difference for 2 independent sample t-test Parameters ---------- varx : 3d array vary : 3d array Returns ------- stat = calculated t-statistic pvalue = two-tailed p-value Usage ----- stat,pvalue = calc_ttest(varx,vary) """ print('\n>>> Using calc_ttest function!') ### Import modules import numpy as np import scipy.stats as sts ### 2-independent sample t-test stat,pvalue = sts.ttest_ind(varx,vary,nan_policy='omit') ### Significant at 95% confidence level pvalue[np.where(pvalue >= 0.05)] = np.nan pvalue[np.where(pvalue < 0.05)] = 1. print('*Completed: Finished calc_ttest function!') return stat,pvalue ############################################################################### ############################################################################### ############################################################################### def calc_weightedAve(var,lats): """ Area weights sit array 5d [ens,year,month,lat,lon] into [ens,year,month] Parameters ---------- var : 5d,4d,3d array of a gridded variable lats : 2d array of latitudes Returns ------- meanvar : weighted average for 3d,2d,1d array Usage ----- meanvar = calc_weightedAve(var,lats) """ print('\n>>> Using calc_weightedAve function!') ### Import modules import numpy as np ### Calculate weighted average for various dimensional arrays if var.ndim == 5: meanvar = np.empty((var.shape[0],var.shape[1],var.shape[2])) for ens in range(var.shape[0]): for i in range(var.shape[1]): for j in range(var.shape[2]): varq = var[ens,i,j,:,:] mask = np.isfinite(varq) & np.isfinite(lats) varmask = varq[mask] areamask = np.cos(np.deg2rad(lats[mask])) meanvar[ens,i,j] = np.nansum(varmask*areamask) \ /np.sum(areamask) elif var.ndim == 4: meanvar = np.empty((var.shape[0],var.shape[1])) for i in range(var.shape[0]): for j in range(var.shape[1]): varq = var[i,j,:,:] mask = np.isfinite(varq) & np.isfinite(lats) varmask = varq[mask] areamask = np.cos(np.deg2rad(lats[mask])) meanvar[i,j] = np.nansum(varmask*areamask)/np.sum(areamask) elif var.ndim == 3: meanvar = np.empty((var.shape[0])) for i in range(var.shape[0]): varq = var[i,:,:] mask = np.isfinite(varq) & np.isfinite(lats) varmask = varq[mask] areamask = np.cos(np.deg2rad(lats[mask])) meanvar[i] = np.nansum(varmask*areamask)/np.sum(areamask) elif var.ndim == 2: meanvar = np.empty((var.shape[0])) varq = var[:,:] mask = np.isfinite(varq) & np.isfinite(lats) varmask = varq[mask] areamask = np.cos(np.deg2rad(lats[mask])) meanvar = np.nansum(varmask*areamask)/np.sum(areamask) else: print(ValueError('Variable has the wrong dimensions!')) print('Completed: Weighted variable average!') print('*Completed: Finished calc_weightedAve function!') return meanvar ############################################################################### ############################################################################### ############################################################################### def calc_spatialCorr(varx,vary,lats,lons,weight): """ Calculates spatial correlation from pearson correlation coefficient Parameters ---------- varx : 2d array vary : 2d array lats : 1d array lons : 1d array of latitude weight : string (yes or no) Returns ------- corrcoef : 1d array of correlation coefficient (pearson r) Usage ----- corrcoef = calc_spatialCorr(varx,vary,lats,lons) """ print('\n>>> Using calc_spatialCorr function!') ### Import modules import numpy as np if weight == 'yes': # Computed weighted correlation coefficient ### mask mask = 'yes' if mask == 'yes': latq = np.where(lats > 40)[0] lats = lats[latq] varx = varx[latq,:] vary = vary[latq,:] print('MASKING LATITUDES!') ### Create 2d meshgrid for weights lon2,lat2 = np.meshgrid(lons,lats) ### Create 2d array of weights based on latitude gw = np.cos(np.deg2rad(lat2)) def m(x, w): """Weighted Mean""" wave = np.sum(x * w) / np.sum(w) print('Completed: Computed weighted average!') return wave def cov(x, y, w): """Weighted Covariance""" wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w) print('Completed: Computed weighted covariance!') return wcov def corr(x, y, w): """Weighted Correlation""" wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w)) print('Completed: Computed weighted correlation!') return wcor corrcoef = corr(varx,vary,gw) elif weight == 'no': ### Correlation coefficient from numpy function (not weighted) corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1] print('Completed: Computed NON-weighted correlation!') else: ValueError('Wrong weighted arguement in function!') print('*Completed: Finished calc_SpatialCorr function!') return corrcoef ############################################################################### ############################################################################### ############################################################################### def calc_RMSE(varx,vary,lats,lons,weight): """ Calculates root mean square weighted average Parameters ---------- varx : 2d array vary : 2d array lons : 1d array of latitude weight : string (yes or no) Returns ------- rmse : 1d array Usage ----- rmse = calc_RMSE(varx,vary,lats,lons) """ print('\n>>> Using calc_RMSE function!') ### Import modules import numpy as np from sklearn.metrics import mean_squared_error if weight == 'yes': # Computed weighted correlation coefficient ### mask mask = 'yes' if mask == 'yes': latq = np.where(lats > 40)[0] lats = lats[latq] varx = varx[latq,:] vary = vary[latq,:] print('MASKING LATITUDES!') ### Create 2d meshgrid for weights lon2,lat2 = np.meshgrid(lons,lats) ### Create 2d array of weights based on latitude gw = np.cos(np.deg2rad(lat2)) ### Calculate rmse sq_err = (varx - vary)**2 rmse = np.sqrt((np.sum(sq_err*gw))/np.sum(gw)) elif weight == 'no': ### Root mean square error from sklearn (not weighted) rmse = np.sqrt(mean_squared_error(varx.ravel(),vary.ravel())) print('Completed: Computed NON-weighted correlation!') else: ValueError('Wrong weighted arguement in function!') print('*Completed: Finished calc_RMSE function!') return rmse ############################################################################### ############################################################################### ############################################################################### def calc_spatialCorrHeight(varx,vary,levs,lons,weight): """ Calculates spatial correlation from pearson correlation coefficient for grids over vertical height (17 pressure coordinate levels) Parameters ---------- varx : 2d array vary : 2d array levs : 1d array of levels lons : 1d array of latitude weight : string (yes or no) Returns ------- corrcoef : 1d array of correlation coefficient (pearson r) Usage ----- corrcoef = calc_spatialCorrHeight(varx,vary,lats,lons) """ print('\n>>> Using calc_spatialCorrHeight function!') ### Import modules import numpy as np if weight == 'yes': # Computed weighted correlation coefficient ### Create 2d meshgrid for weights lon2,lev2 = np.meshgrid(lons,levs) ### Create 2d array of weights based on latitude gwq = np.array([0.25,0.25,0.25,0.25,0.25,0.25,0.4,0.5,0.5,0.5, 0.5,0.5,0.5,0.7,0.7,0.7,1.]) gw,gw2 = np.meshgrid(lons,gwq) def m(x, w): """Weighted Mean""" wave = np.sum(x * w) / np.sum(w) print('Completed: Computed weighted average (17 P Levels)!') return wave def cov(x, y, w): """Weighted Covariance""" wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w) print('Completed: Computed weighted covariance (17 P Levels)!') return wcov def corr(x, y, w): """Weighted Correlation""" wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w)) print('Completed: Computed weighted correlation (17 P Levels)!') return wcor corrcoef = corr(varx,vary,gw) elif weight == 'no': ### Correlation coefficient from numpy function (not weighted) corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1] print('Completed: Computed NON-weighted correlation!') else: ValueError('Wrong weighted argument in function!') print('*Completed: Finished calc_SpatialCorrHeight function!') return corrcoef ############################################################################### ############################################################################### ############################################################################### def calc_spatialCorrHeightLev(varx,vary,levs,lons,weight,levelq): """ Calculates spatial correlation from pearson correlation coefficient for grids over vertical height (17 pressure coordinate levels). Change the weighting for different level correlations Parameters ---------- varx : 2d array vary : 2d array levs : 1d array of levels lons : 1d array of latitude weight : string (yes or no) levelq : string (all, tropo, strato) Returns ------- corrcoef : 1d array of correlation coefficient (pearson r) Usage ----- corrcoef = calc_spatialCorrHeight(varx,vary,lats,lons,levels) """ print('\n>>> Using calc_spatialCorrHeightLev function!') ### Import modules import numpy as np if weight == 'yes': # Computed weighted correlation coefficient ### Create 2d meshgrid for weights lon2,lev2 = np.meshgrid(lons,levs) if levelq == 'all': ### Create 2d array of weights based on latitude gwq = np.array([0.25,0.25,0.25,0.25,0.25,0.25,0.4,0.5,0.5,0.5, 0.5,0.5,0.5,0.7,0.7,0.7,1.]) gw,gw2 = np.meshgrid(lons,gwq) elif levelq == 'tropo': gwq = np.array([1.0,1.0,1.0,1.0,0.5,0.5,0.5,0.2,0.2,0.,0.,0., 0.,0.,0.,0.,0.]) gw,gw2 = np.meshgrid(lons,gwq) elif levelq == 'strato': gwq = np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.5,1.,1.,1.,1. ,1.,1.]) gw,gw2 = np.meshgrid(lons,gwq) def m(x, w): """Weighted Mean""" wave = np.sum(x * w) / np.sum(w) print('Completed: Computed weighted average (17 P Levels)!') return wave def cov(x, y, w): """Weighted Covariance""" wcov = np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w) print('Completed: Computed weighted covariance (17 P Levels)!') return wcov def corr(x, y, w): """Weighted Correlation""" wcor = cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w)) print('Completed: Computed weighted correlation (17 P Levels)!') return wcor corrcoef = corr(varx,vary,gw) elif weight == 'no': ### Correlation coefficient from numpy function (not weighted) corrcoef= np.corrcoef(varx.ravel(),vary.ravel())[0][1] print('Completed: Computed NON-weighted correlation!') else: ValueError('Wrong weighted argument in function!') print('*Completed: Finished calc_SpatialCorrHeightLev function!') return corrcoef
[((9848, 9892), 'scipy.stats.ttest_ind', 'sts.ttest_ind', (['varx', 'vary'], {'nan_policy': '"""omit"""'}), "(varx, vary, nan_policy='omit')\n", (9861, 9892), True, 'import scipy.stats as sts\n'), ((1888, 1945), 'numpy.empty', 'np.empty', (['(varx.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((varx.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (1896, 1945), True, 'import numpy as np\n'), ((1960, 2017), 'numpy.empty', 'np.empty', (['(vary.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((vary.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (1968, 2017), True, 'import numpy as np\n'), ((6092, 6149), 'numpy.empty', 'np.empty', (['(varx.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((varx.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (6100, 6149), True, 'import numpy as np\n'), ((6165, 6222), 'numpy.empty', 'np.empty', (['(vary.shape[0] - 1, lat.shape[0], lon.shape[0])'], {}), '((vary.shape[0] - 1, lat.shape[0], lon.shape[0]))\n', (6173, 6222), True, 'import numpy as np\n'), ((10947, 10999), 'numpy.empty', 'np.empty', (['(var.shape[0], var.shape[1], var.shape[2])'], {}), '((var.shape[0], var.shape[1], var.shape[2]))\n', (10955, 10999), True, 'import numpy as np\n'), ((13952, 13975), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'lats'], {}), '(lons, lats)\n', (13963, 13975), True, 'import numpy as np\n'), ((16497, 16520), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'lats'], {}), '(lons, lats)\n', (16508, 16520), True, 'import numpy as np\n'), ((18266, 18289), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'levs'], {}), '(lons, levs)\n', (18277, 18289), True, 'import numpy as np\n'), ((18369, 18474), 'numpy.array', 'np.array', (['[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7,\n 0.7, 0.7, 1.0]'], {}), '([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5,\n 0.5, 0.7, 0.7, 0.7, 1.0])\n', (18377, 18474), True, 'import numpy as np\n'), ((18486, 18508), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (18497, 18508), True, 'import numpy as np\n'), ((20869, 20892), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'levs'], {}), '(lons, levs)\n', (20880, 20892), True, 'import numpy as np\n'), ((2188, 2251), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :]', 'varxravel[(12 + i), :, :]'], {}), '(varxravel[(11 + i), :, :], varxravel[(12 + i), :, :])\n', (2197, 2251), True, 'import numpy as np\n'), ((2263, 2326), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :]', 'varyravel[(12 + i), :, :]'], {}), '(varyravel[(11 + i), :, :], varyravel[(12 + i), :, :])\n', (2272, 2326), True, 'import numpy as np\n'), ((6395, 6458), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :]', 'varxravel[(12 + i), :, :]'], {}), '(varxravel[(11 + i), :, :], varxravel[(12 + i), :, :])\n', (6404, 6458), True, 'import numpy as np\n'), ((6472, 6535), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :]', 'varyravel[(12 + i), :, :]'], {}), '(varyravel[(11 + i), :, :], varyravel[(12 + i), :, :])\n', (6481, 6535), True, 'import numpy as np\n'), ((6550, 6599), 'numpy.append', 'np.append', (['djfappendh1', 'varxravel[(13 + i), :, :]'], {}), '(djfappendh1, varxravel[(13 + i), :, :])\n', (6559, 6599), True, 'import numpy as np\n'), ((6618, 6667), 'numpy.append', 'np.append', (['djfappendf1', 'varyravel[(13 + i), :, :]'], {}), '(djfappendf1, varyravel[(13 + i), :, :])\n', (6627, 6667), True, 'import numpy as np\n'), ((9951, 9975), 'numpy.where', 'np.where', (['(pvalue >= 0.05)'], {}), '(pvalue >= 0.05)\n', (9959, 9975), True, 'import numpy as np\n'), ((9997, 10020), 'numpy.where', 'np.where', (['(pvalue < 0.05)'], {}), '(pvalue < 0.05)\n', (10005, 10020), True, 'import numpy as np\n'), ((11509, 11547), 'numpy.empty', 'np.empty', (['(var.shape[0], var.shape[1])'], {}), '((var.shape[0], var.shape[1]))\n', (11517, 11547), True, 'import numpy as np\n'), ((14061, 14077), 'numpy.deg2rad', 'np.deg2rad', (['lat2'], {}), '(lat2)\n', (14071, 14077), True, 'import numpy as np\n'), ((16618, 16634), 'numpy.deg2rad', 'np.deg2rad', (['lat2'], {}), '(lat2)\n', (16628, 16634), True, 'import numpy as np\n'), ((21008, 21113), 'numpy.array', 'np.array', (['[0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7,\n 0.7, 0.7, 1.0]'], {}), '([0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5,\n 0.5, 0.7, 0.7, 0.7, 1.0])\n', (21016, 21113), True, 'import numpy as np\n'), ((21133, 21155), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21144, 21155), True, 'import numpy as np\n'), ((3574, 3643), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :, :]', 'varxravel[(12 + i), :, :, :]'], {}), '(varxravel[(11 + i), :, :, :], varxravel[(12 + i), :, :, :])\n', (3583, 3643), True, 'import numpy as np\n'), ((3688, 3757), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :, :]', 'varyravel[(12 + i), :, :, :]'], {}), '(varyravel[(11 + i), :, :, :], varyravel[(12 + i), :, :, :])\n', (3697, 3757), True, 'import numpy as np\n'), ((7926, 7995), 'numpy.append', 'np.append', (['varxravel[(11 + i), :, :, :]', 'varxravel[(12 + i), :, :, :]'], {}), '(varxravel[(11 + i), :, :, :], varxravel[(12 + i), :, :, :])\n', (7935, 7995), True, 'import numpy as np\n'), ((8042, 8111), 'numpy.append', 'np.append', (['varyravel[(11 + i), :, :, :]', 'varyravel[(12 + i), :, :, :]'], {}), '(varyravel[(11 + i), :, :, :], varyravel[(12 + i), :, :, :])\n', (8051, 8111), True, 'import numpy as np\n'), ((8158, 8210), 'numpy.append', 'np.append', (['djfappendh1', 'varxravel[(13 + i), :, :, :]'], {}), '(djfappendh1, varxravel[(13 + i), :, :, :])\n', (8167, 8210), True, 'import numpy as np\n'), ((8263, 8315), 'numpy.append', 'np.append', (['djfappendf1', 'varyravel[(13 + i), :, :, :]'], {}), '(djfappendf1, varyravel[(13 + i), :, :, :])\n', (8272, 8315), True, 'import numpy as np\n'), ((11937, 11959), 'numpy.empty', 'np.empty', (['var.shape[0]'], {}), '(var.shape[0])\n', (11945, 11959), True, 'import numpy as np\n'), ((13722, 13741), 'numpy.where', 'np.where', (['(lats > 40)'], {}), '(lats > 40)\n', (13730, 13741), True, 'import numpy as np\n'), ((14165, 14178), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (14171, 14178), True, 'import numpy as np\n'), ((14181, 14190), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (14187, 14190), True, 'import numpy as np\n'), ((14424, 14433), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (14430, 14433), True, 'import numpy as np\n'), ((16239, 16258), 'numpy.where', 'np.where', (['(lats > 40)'], {}), '(lats > 40)\n', (16247, 16258), True, 'import numpy as np\n'), ((16747, 16766), 'numpy.sum', 'np.sum', (['(sq_err * gw)'], {}), '(sq_err * gw)\n', (16753, 16766), True, 'import numpy as np\n'), ((16766, 16776), 'numpy.sum', 'np.sum', (['gw'], {}), '(gw)\n', (16772, 16776), True, 'import numpy as np\n'), ((18594, 18607), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (18600, 18607), True, 'import numpy as np\n'), ((18610, 18619), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (18616, 18619), True, 'import numpy as np\n'), ((18867, 18876), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (18873, 18876), True, 'import numpy as np\n'), ((21204, 21304), 'numpy.array', 'np.array', (['[1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\n 0.0, 0.0]'], {}), '([1.0, 1.0, 1.0, 1.0, 0.5, 0.5, 0.5, 0.2, 0.2, 0.0, 0.0, 0.0, 0.0, \n 0.0, 0.0, 0.0, 0.0])\n', (21212, 21304), True, 'import numpy as np\n'), ((21324, 21346), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21335, 21346), True, 'import numpy as np\n'), ((21616, 21629), 'numpy.sum', 'np.sum', (['(x * w)'], {}), '(x * w)\n', (21622, 21629), True, 'import numpy as np\n'), ((21632, 21641), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (21638, 21641), True, 'import numpy as np\n'), ((21889, 21898), 'numpy.sum', 'np.sum', (['w'], {}), '(w)\n', (21895, 21898), True, 'import numpy as np\n'), ((12286, 12308), 'numpy.empty', 'np.empty', (['var.shape[0]'], {}), '(var.shape[0])\n', (12294, 12308), True, 'import numpy as np\n'), ((21396, 21496), 'numpy.array', 'np.array', (['[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0,\n 1.0, 1.0]'], {}), '([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, \n 1.0, 1.0, 1.0, 1.0])\n', (21404, 21496), True, 'import numpy as np\n'), ((21508, 21530), 'numpy.meshgrid', 'np.meshgrid', (['lons', 'gwq'], {}), '(lons, gwq)\n', (21519, 21530), True, 'import numpy as np\n'), ((11197, 11214), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (11208, 11214), True, 'import numpy as np\n'), ((11217, 11234), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (11228, 11234), True, 'import numpy as np\n'), ((11314, 11336), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (11324, 11336), True, 'import numpy as np\n'), ((11377, 11406), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (11386, 11406), True, 'import numpy as np\n'), ((11448, 11464), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (11454, 11464), True, 'import numpy as np\n'), ((11686, 11703), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (11697, 11703), True, 'import numpy as np\n'), ((11706, 11723), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (11717, 11723), True, 'import numpy as np\n'), ((11795, 11817), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (11805, 11817), True, 'import numpy as np\n'), ((11850, 11879), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (11859, 11879), True, 'import numpy as np\n'), ((11878, 11894), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (11884, 11894), True, 'import numpy as np\n'), ((12049, 12066), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (12060, 12066), True, 'import numpy as np\n'), ((12069, 12086), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (12080, 12086), True, 'import numpy as np\n'), ((12150, 12172), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (12160, 12172), True, 'import numpy as np\n'), ((12199, 12228), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (12208, 12228), True, 'import numpy as np\n'), ((12227, 12243), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (12233, 12243), True, 'import numpy as np\n'), ((12350, 12367), 'numpy.isfinite', 'np.isfinite', (['varq'], {}), '(varq)\n', (12361, 12367), True, 'import numpy as np\n'), ((12370, 12387), 'numpy.isfinite', 'np.isfinite', (['lats'], {}), '(lats)\n', (12381, 12387), True, 'import numpy as np\n'), ((12443, 12465), 'numpy.deg2rad', 'np.deg2rad', (['lats[mask]'], {}), '(lats[mask])\n', (12453, 12465), True, 'import numpy as np\n'), ((12485, 12514), 'numpy.nansum', 'np.nansum', (['(varmask * areamask)'], {}), '(varmask * areamask)\n', (12494, 12514), True, 'import numpy as np\n'), ((12513, 12529), 'numpy.sum', 'np.sum', (['areamask'], {}), '(areamask)\n', (12519, 12529), True, 'import numpy as np\n')]
xiebaiyuan/tvm
tests/python/unittest/test_tir_schedule_compute_inline.py
726239d788e3b90cbe4818271ca5361c46d8d246
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you 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. # pylint: disable=missing-function-docstring,missing-module-docstring import pytest import tvm from tvm import tir from tvm.script import ty # pylint: disable=no-member,invalid-name,unused-variable @tvm.script.tir def elementwise(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = B[vi, vj] + 1.0 @tvm.script.tir def elementwise_multi_producer_consumer(a: ty.handle, c: ty.handle, d: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) D = tir.match_buffer(d, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 # B has two consumers with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = B[vi, vj] + 1.0 with tir.block([128, 128], "D") as [vi, vj]: D[vi, vj] = B[vi, vj] + 2.0 + C[vi, vj] # D has two producers @tvm.script.tir def elementwise_multi_consumer_inlined(a: ty.handle, c: ty.handle, d: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) D = tir.match_buffer(d, (128, 128)) with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = A[vi, vj] * 2.0 + 1.0 with tir.block([128, 128], "D") as [vi, vj]: D[vi, vj] = A[vi, vj] * 2.0 + 2.0 + C[vi, vj] @tvm.script.tir def elementwise_standalone(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = A[vi, vj] + 1.0 @tvm.script.tir def elementwise_standalone_dce(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = A[vi, vj] + 1.0 @tvm.script.tir def elementwise_under_loop(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) B = tir.alloc_buffer((128, 128)) for i in tir.serial(0, 128): for j in tir.serial(0, 128): with tir.block([128, 128], "B") as [vi, vj]: tir.bind(vi, i) tir.bind(vj, j) B[vi, vj] = A[vi, vj] * 2.0 for j in tir.serial(0, 128): with tir.block([128, 128], "C") as [vi, vj]: tir.bind(vi, i) tir.bind(vj, j) C[vi, vj] = B[vi, vj] + 1.0 @tvm.script.tir def elementwise_inlined(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = A[vi, vj] * 2.0 + 1.0 @tvm.script.tir def fail_multi_reader_writer(a: ty.handle, d: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.alloc_buffer((128, 128)) D = tir.match_buffer(d, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 C[vi, vj] = A[vi, vj] + 2.0 with tir.block([128, 128], "C") as [vi, vj]: D[vi, vj] = B[vi, vj] + C[vi, vj] @tvm.script.tir def elementwise_multi_reverse_loads(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: C[vi, vj] = (B[vi, vj] + 1.0) * (B[vi, vj] * 2.0) + 3.0 @tvm.script.tir def elementwise_multi_reverse_loads_inlined(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: C[vi, vj] = (A[vi, vj] * 2.0 + 1.0) * (A[vi, vj] * 2.0 * 2.0) + 3.0 @tvm.script.tir def opaque_access_load(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: tir.reads(B[0:128, 0:128]) tir.writes(C[0:128, 0:128]) C[vi, vj] = tir.load("float32", B.data, vi * 128 + vj) + 1.0 @tvm.script.tir def opaque_access_store(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: tir.reads(B[0:128, 0:128]) tir.writes(C[0:128, 0:128]) tir.store(C.data, vi * 128 + vj, B[vi, vj] + 1.0) C[vi, vj] = tir.load("float32", B.data, vi * 16 + vj) + 1.0 @tvm.script.tir def buffer_matched(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 128], "C") as [vi, vj]: Bb = tir.match_buffer(B[vi : vi + 1, vj], (1, 1)) C[vi, vj] = Bb[0, 0] + 1.0 @tvm.script.tir def elementwise_predicate(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 for i, j in tir.grid(128, 128): with tir.block([128, 128], "C") as [vi, vj]: tir.where(B[i, j] < 10.0) C[vi, vj] = B[vi, vj] + 1.0 @tvm.script.tir def elementwise_predicate_inlined(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) for i, j in tir.grid(128, 128): with tir.block([128, 128], "C") as [vi, vj]: tir.where(A[i, j] * 2.0 < 10.0) C[vi, vj] = A[vi, vj] * 2.0 + 1.0 @tvm.script.tir def elementwise_multi_loads(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) B = tir.alloc_buffer((128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 128], "B") as [vi, vj]: B[vi, vj] = A[vi, vj] * 2.0 with tir.block([128, 126], "C") as [vi, vj]: C[vi, vj] = B[vi, vj] + B[vi, vj + 1] + B[vi, vj + 2] @tvm.script.tir def elementwise_multi_loads_inlined(a: ty.handle, c: ty.handle) -> None: A = tir.match_buffer(a, (128, 128)) C = tir.match_buffer(c, (128, 128)) with tir.block([128, 126], "C") as [vi, vj]: C[vi, vj] = A[vi, vj] * 2.0 + A[vi, vj + 1] * 2.0 + A[vi, vj + 2] * 2.0 # pylint: enable=no-member,invalid-name,unused-variable def test_compute_inline_elementwise(): sch = tir.Schedule(elementwise, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_inlined, sch.mod["main"]) assert sch.get(block_c).name_hint == "C" def test_compute_inline_under_loop(): sch = tir.Schedule(elementwise_under_loop, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_inlined, sch.mod["main"]) assert sch.get(block_c).name_hint == "C" def test_compute_inline_as_dce(): sch = tir.Schedule(elementwise_standalone, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_standalone_dce, sch.mod["main"]) assert sch.get(block_c).name_hint == "C" def test_compute_inline_multi_consumer(): sch = tir.Schedule(elementwise_multi_producer_consumer, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") block_d = sch.get_block("D") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_multi_consumer_inlined, sch.mod["main"]) assert sch.get(block_c).name_hint == "C" assert sch.get(block_d).name_hint == "D" def test_compute_inline_fail_multi_writer(): sch = tir.Schedule(fail_multi_reader_writer, debug_mode=True, error_render_level="detail") block_b = sch.get_block("B") with pytest.raises(tvm.tir.ScheduleError): sch.compute_inline(block_b) def test_reverse_compute_inline_elementwise(): sch = tir.Schedule(elementwise, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") sch.reverse_compute_inline(block_c) tvm.ir.assert_structural_equal(elementwise_inlined, sch.mod["main"]) assert sch.get(block_b).name_hint == "B" def test_reverse_compute_inline_under_loop(): sch = tir.Schedule(elementwise_under_loop, debug_mode=True) block_b = sch.get_block("B") block_c = sch.get_block("C") sch.reverse_compute_inline(block_c) tvm.ir.assert_structural_equal(elementwise_inlined, sch.mod["main"]) assert sch.get(block_b).name_hint == "B" def test_reverse_compute_inline_fail_as_dce(): sch = tir.Schedule(elementwise_standalone, debug_mode=True) block_b = sch.get_block("B") with pytest.raises(tvm.tir.ScheduleError): sch.reverse_compute_inline(block_b) def test_reverse_compute_inline_fail_multi_producer(): sch = tir.Schedule(elementwise_multi_producer_consumer, debug_mode=True) block_d = sch.get_block("D") with pytest.raises(tvm.tir.ScheduleError): sch.reverse_compute_inline(block_d) def test_reverse_compute_inline_fail_multi_reader(): sch = tir.Schedule(fail_multi_reader_writer, debug_mode=True) block_c = sch.get_block("C") with pytest.raises(tvm.tir.ScheduleError): sch.reverse_compute_inline(block_c) def test_reverse_compute_multi_reverse_loads(): sch = tir.Schedule(elementwise_multi_reverse_loads, debug_mode=True) block_c = sch.get_block("C") sch.reverse_compute_inline(block_c) tvm.ir.assert_structural_equal(elementwise_multi_reverse_loads_inlined, sch.mod["main"]) def test_reverse_compute_fail_multi_reverse_loads(): sch = tir.Schedule(elementwise_multi_loads, debug_mode=True) block_c = sch.get_block("C") with pytest.raises(tvm.tir.ScheduleError): sch.reverse_compute_inline(block_c) def test_opaque_access_load(): sch = tir.Schedule(opaque_access_load, debug_mode=True) block_b = sch.get_block("B") with pytest.raises(tvm.tir.ScheduleError): sch.compute_inline(block_b) def test_opaque_access_store(): sch = tir.Schedule(opaque_access_store, debug_mode=True) block_b = sch.get_block("B") with pytest.raises(tvm.tir.ScheduleError): sch.compute_inline(block_b) def test_buffer_matched(): sch = tir.Schedule(buffer_matched, debug_mode=True) block_b = sch.get_block("B") with pytest.raises(tvm.tir.ScheduleError): sch.compute_inline(block_b) def test_compute_inline_predicate(): sch = tir.Schedule(elementwise_predicate, debug_mode=True) block_b = sch.get_block("B") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_predicate_inlined, sch.mod["main"]) def test_compute_inline_multi_loads(): sch = tir.Schedule(elementwise_multi_loads, debug_mode=True) block_b = sch.get_block("B") sch.compute_inline(block_b) tvm.ir.assert_structural_equal(elementwise_multi_loads_inlined, sch.mod["main"]) if __name__ == "__main__": test_compute_inline_elementwise() test_compute_inline_under_loop() test_compute_inline_as_dce() test_compute_inline_multi_consumer() test_compute_inline_fail_multi_writer() test_reverse_compute_inline_elementwise() test_reverse_compute_inline_under_loop() test_reverse_compute_inline_fail_as_dce() test_reverse_compute_inline_fail_multi_producer() test_reverse_compute_inline_fail_multi_reader() test_reverse_compute_multi_reverse_loads() test_reverse_compute_fail_multi_reverse_loads() test_opaque_access_load() test_opaque_access_store() test_buffer_matched() test_compute_inline_predicate() test_compute_inline_multi_loads()
[((1063, 1094), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (1079, 1094), False, 'from tvm import tir\n'), ((1103, 1131), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (1119, 1131), False, 'from tvm import tir\n'), ((1140, 1171), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (1156, 1171), False, 'from tvm import tir\n'), ((1459, 1490), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (1475, 1490), False, 'from tvm import tir\n'), ((1499, 1527), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (1515, 1527), False, 'from tvm import tir\n'), ((1536, 1567), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (1552, 1567), False, 'from tvm import tir\n'), ((1576, 1607), 'tvm.tir.match_buffer', 'tir.match_buffer', (['d', '(128, 128)'], {}), '(d, (128, 128))\n', (1592, 1607), False, 'from tvm import tir\n'), ((2037, 2068), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (2053, 2068), False, 'from tvm import tir\n'), ((2077, 2108), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (2093, 2108), False, 'from tvm import tir\n'), ((2117, 2148), 'tvm.tir.match_buffer', 'tir.match_buffer', (['d', '(128, 128)'], {}), '(d, (128, 128))\n', (2133, 2148), False, 'from tvm import tir\n'), ((2433, 2464), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (2449, 2464), False, 'from tvm import tir\n'), ((2473, 2501), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (2489, 2501), False, 'from tvm import tir\n'), ((2510, 2541), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (2526, 2541), False, 'from tvm import tir\n'), ((2806, 2837), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (2822, 2837), False, 'from tvm import tir\n'), ((2846, 2877), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (2862, 2877), False, 'from tvm import tir\n'), ((3053, 3084), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (3069, 3084), False, 'from tvm import tir\n'), ((3093, 3124), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (3109, 3124), False, 'from tvm import tir\n'), ((3133, 3161), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (3149, 3161), False, 'from tvm import tir\n'), ((3175, 3193), 'tvm.tir.serial', 'tir.serial', (['(0)', '(128)'], {}), '(0, 128)\n', (3185, 3193), False, 'from tvm import tir\n'), ((3686, 3717), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (3702, 3717), False, 'from tvm import tir\n'), ((3726, 3757), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (3742, 3757), False, 'from tvm import tir\n'), ((3941, 3972), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (3957, 3972), False, 'from tvm import tir\n'), ((3981, 4009), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (3997, 4009), False, 'from tvm import tir\n'), ((4018, 4046), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (4034, 4046), False, 'from tvm import tir\n'), ((4055, 4086), 'tvm.tir.match_buffer', 'tir.match_buffer', (['d', '(128, 128)'], {}), '(d, (128, 128))\n', (4071, 4086), False, 'from tvm import tir\n'), ((4398, 4429), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (4414, 4429), False, 'from tvm import tir\n'), ((4438, 4466), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (4454, 4466), False, 'from tvm import tir\n'), ((4475, 4506), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (4491, 4506), False, 'from tvm import tir\n'), ((4812, 4843), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (4828, 4843), False, 'from tvm import tir\n'), ((4852, 4883), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (4868, 4883), False, 'from tvm import tir\n'), ((5095, 5126), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (5111, 5126), False, 'from tvm import tir\n'), ((5135, 5163), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (5151, 5163), False, 'from tvm import tir\n'), ((5172, 5203), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (5188, 5203), False, 'from tvm import tir\n'), ((5565, 5596), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (5581, 5596), False, 'from tvm import tir\n'), ((5605, 5633), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (5621, 5633), False, 'from tvm import tir\n'), ((5642, 5673), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (5658, 5673), False, 'from tvm import tir\n'), ((6087, 6118), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (6103, 6118), False, 'from tvm import tir\n'), ((6127, 6155), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (6143, 6155), False, 'from tvm import tir\n'), ((6164, 6195), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (6180, 6195), False, 'from tvm import tir\n'), ((6512, 6543), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (6528, 6543), False, 'from tvm import tir\n'), ((6552, 6580), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (6568, 6580), False, 'from tvm import tir\n'), ((6589, 6620), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (6605, 6620), False, 'from tvm import tir\n'), ((6722, 6740), 'tvm.tir.grid', 'tir.grid', (['(128)', '(128)'], {}), '(128, 128)\n', (6730, 6740), False, 'from tvm import tir\n'), ((6970, 7001), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (6986, 7001), False, 'from tvm import tir\n'), ((7010, 7041), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (7026, 7041), False, 'from tvm import tir\n'), ((7058, 7076), 'tvm.tir.grid', 'tir.grid', (['(128)', '(128)'], {}), '(128, 128)\n', (7066, 7076), False, 'from tvm import tir\n'), ((7312, 7343), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (7328, 7343), False, 'from tvm import tir\n'), ((7352, 7380), 'tvm.tir.alloc_buffer', 'tir.alloc_buffer', (['(128, 128)'], {}), '((128, 128))\n', (7368, 7380), False, 'from tvm import tir\n'), ((7389, 7420), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (7405, 7420), False, 'from tvm import tir\n'), ((7716, 7747), 'tvm.tir.match_buffer', 'tir.match_buffer', (['a', '(128, 128)'], {}), '(a, (128, 128))\n', (7732, 7747), False, 'from tvm import tir\n'), ((7756, 7787), 'tvm.tir.match_buffer', 'tir.match_buffer', (['c', '(128, 128)'], {}), '(c, (128, 128))\n', (7772, 7787), False, 'from tvm import tir\n'), ((8026, 8068), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise'], {'debug_mode': '(True)'}), '(elementwise, debug_mode=True)\n', (8038, 8068), False, 'from tvm import tir\n'), ((8171, 8239), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_inlined', "sch.mod['main']"], {}), "(elementwise_inlined, sch.mod['main'])\n", (8201, 8239), False, 'import tvm\n'), ((8335, 8388), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_under_loop'], {'debug_mode': '(True)'}), '(elementwise_under_loop, debug_mode=True)\n', (8347, 8388), False, 'from tvm import tir\n'), ((8491, 8559), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_inlined', "sch.mod['main']"], {}), "(elementwise_inlined, sch.mod['main'])\n", (8521, 8559), False, 'import tvm\n'), ((8651, 8704), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_standalone'], {'debug_mode': '(True)'}), '(elementwise_standalone, debug_mode=True)\n', (8663, 8704), False, 'from tvm import tir\n'), ((8807, 8882), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_standalone_dce', "sch.mod['main']"], {}), "(elementwise_standalone_dce, sch.mod['main'])\n", (8837, 8882), False, 'import tvm\n'), ((8982, 9048), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_multi_producer_consumer'], {'debug_mode': '(True)'}), '(elementwise_multi_producer_consumer, debug_mode=True)\n', (8994, 9048), False, 'from tvm import tir\n'), ((9184, 9272), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_multi_consumer_inlined', "sch.mod['main']"], {}), "(elementwise_multi_consumer_inlined, sch.mod[\n 'main'])\n", (9214, 9272), False, 'import tvm\n'), ((9415, 9504), 'tvm.tir.Schedule', 'tir.Schedule', (['fail_multi_reader_writer'], {'debug_mode': '(True)', 'error_render_level': '"""detail"""'}), "(fail_multi_reader_writer, debug_mode=True, error_render_level=\n 'detail')\n", (9427, 9504), False, 'from tvm import tir\n'), ((9675, 9717), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise'], {'debug_mode': '(True)'}), '(elementwise, debug_mode=True)\n', (9687, 9717), False, 'from tvm import tir\n'), ((9828, 9896), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_inlined', "sch.mod['main']"], {}), "(elementwise_inlined, sch.mod['main'])\n", (9858, 9896), False, 'import tvm\n'), ((10000, 10053), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_under_loop'], {'debug_mode': '(True)'}), '(elementwise_under_loop, debug_mode=True)\n', (10012, 10053), False, 'from tvm import tir\n'), ((10164, 10232), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_inlined', "sch.mod['main']"], {}), "(elementwise_inlined, sch.mod['main'])\n", (10194, 10232), False, 'import tvm\n'), ((10337, 10390), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_standalone'], {'debug_mode': '(True)'}), '(elementwise_standalone, debug_mode=True)\n', (10349, 10390), False, 'from tvm import tir\n'), ((10582, 10648), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_multi_producer_consumer'], {'debug_mode': '(True)'}), '(elementwise_multi_producer_consumer, debug_mode=True)\n', (10594, 10648), False, 'from tvm import tir\n'), ((10838, 10893), 'tvm.tir.Schedule', 'tir.Schedule', (['fail_multi_reader_writer'], {'debug_mode': '(True)'}), '(fail_multi_reader_writer, debug_mode=True)\n', (10850, 10893), False, 'from tvm import tir\n'), ((11078, 11140), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_multi_reverse_loads'], {'debug_mode': '(True)'}), '(elementwise_multi_reverse_loads, debug_mode=True)\n', (11090, 11140), False, 'from tvm import tir\n'), ((11218, 11311), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_multi_reverse_loads_inlined', "sch.mod['main']"], {}), "(elementwise_multi_reverse_loads_inlined, sch\n .mod['main'])\n", (11248, 11311), False, 'import tvm\n'), ((11372, 11426), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_multi_loads'], {'debug_mode': '(True)'}), '(elementwise_multi_loads, debug_mode=True)\n', (11384, 11426), False, 'from tvm import tir\n'), ((11594, 11643), 'tvm.tir.Schedule', 'tir.Schedule', (['opaque_access_load'], {'debug_mode': '(True)'}), '(opaque_access_load, debug_mode=True)\n', (11606, 11643), False, 'from tvm import tir\n'), ((11804, 11854), 'tvm.tir.Schedule', 'tir.Schedule', (['opaque_access_store'], {'debug_mode': '(True)'}), '(opaque_access_store, debug_mode=True)\n', (11816, 11854), False, 'from tvm import tir\n'), ((12010, 12055), 'tvm.tir.Schedule', 'tir.Schedule', (['buffer_matched'], {'debug_mode': '(True)'}), '(buffer_matched, debug_mode=True)\n', (12022, 12055), False, 'from tvm import tir\n'), ((12221, 12273), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_predicate'], {'debug_mode': '(True)'}), '(elementwise_predicate, debug_mode=True)\n', (12233, 12273), False, 'from tvm import tir\n'), ((12343, 12421), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_predicate_inlined', "sch.mod['main']"], {}), "(elementwise_predicate_inlined, sch.mod['main'])\n", (12373, 12421), False, 'import tvm\n'), ((12473, 12527), 'tvm.tir.Schedule', 'tir.Schedule', (['elementwise_multi_loads'], {'debug_mode': '(True)'}), '(elementwise_multi_loads, debug_mode=True)\n', (12485, 12527), False, 'from tvm import tir\n'), ((12597, 12682), 'tvm.ir.assert_structural_equal', 'tvm.ir.assert_structural_equal', (['elementwise_multi_loads_inlined', "sch.mod['main']"], {}), "(elementwise_multi_loads_inlined, sch.mod['main']\n )\n", (12627, 12682), False, 'import tvm\n'), ((1181, 1207), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (1190, 1207), False, 'from tvm import tir\n'), ((1266, 1292), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (1275, 1292), False, 'from tvm import tir\n'), ((1617, 1643), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (1626, 1643), False, 'from tvm import tir\n'), ((1725, 1751), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (1734, 1751), False, 'from tvm import tir\n'), ((1810, 1836), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""D"""'], {}), "([128, 128], 'D')\n", (1819, 1836), False, 'from tvm import tir\n'), ((2158, 2184), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (2167, 2184), False, 'from tvm import tir\n'), ((2249, 2275), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""D"""'], {}), "([128, 128], 'D')\n", (2258, 2275), False, 'from tvm import tir\n'), ((2551, 2577), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (2560, 2577), False, 'from tvm import tir\n'), ((2636, 2662), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (2645, 2662), False, 'from tvm import tir\n'), ((2887, 2913), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (2896, 2913), False, 'from tvm import tir\n'), ((3212, 3230), 'tvm.tir.serial', 'tir.serial', (['(0)', '(128)'], {}), '(0, 128)\n', (3222, 3230), False, 'from tvm import tir\n'), ((3414, 3432), 'tvm.tir.serial', 'tir.serial', (['(0)', '(128)'], {}), '(0, 128)\n', (3424, 3432), False, 'from tvm import tir\n'), ((3767, 3793), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (3776, 3793), False, 'from tvm import tir\n'), ((4096, 4122), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (4105, 4122), False, 'from tvm import tir\n'), ((4217, 4243), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (4226, 4243), False, 'from tvm import tir\n'), ((4516, 4542), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (4525, 4542), False, 'from tvm import tir\n'), ((4601, 4627), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (4610, 4627), False, 'from tvm import tir\n'), ((4893, 4919), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (4902, 4919), False, 'from tvm import tir\n'), ((5213, 5239), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (5222, 5239), False, 'from tvm import tir\n'), ((5298, 5324), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (5307, 5324), False, 'from tvm import tir\n'), ((5346, 5372), 'tvm.tir.reads', 'tir.reads', (['B[0:128, 0:128]'], {}), '(B[0:128, 0:128])\n', (5355, 5372), False, 'from tvm import tir\n'), ((5381, 5408), 'tvm.tir.writes', 'tir.writes', (['C[0:128, 0:128]'], {}), '(C[0:128, 0:128])\n', (5391, 5408), False, 'from tvm import tir\n'), ((5683, 5709), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (5692, 5709), False, 'from tvm import tir\n'), ((5768, 5794), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (5777, 5794), False, 'from tvm import tir\n'), ((5816, 5842), 'tvm.tir.reads', 'tir.reads', (['B[0:128, 0:128]'], {}), '(B[0:128, 0:128])\n', (5825, 5842), False, 'from tvm import tir\n'), ((5851, 5878), 'tvm.tir.writes', 'tir.writes', (['C[0:128, 0:128]'], {}), '(C[0:128, 0:128])\n', (5861, 5878), False, 'from tvm import tir\n'), ((5887, 5936), 'tvm.tir.store', 'tir.store', (['C.data', '(vi * 128 + vj)', '(B[vi, vj] + 1.0)'], {}), '(C.data, vi * 128 + vj, B[vi, vj] + 1.0)\n', (5896, 5936), False, 'from tvm import tir\n'), ((6205, 6231), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (6214, 6231), False, 'from tvm import tir\n'), ((6290, 6316), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (6299, 6316), False, 'from tvm import tir\n'), ((6343, 6387), 'tvm.tir.match_buffer', 'tir.match_buffer', (['B[vi:vi + 1, (vj)]', '(1, 1)'], {}), '(B[vi:vi + 1, (vj)], (1, 1))\n', (6359, 6387), False, 'from tvm import tir\n'), ((6630, 6656), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (6639, 6656), False, 'from tvm import tir\n'), ((7430, 7456), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (7439, 7456), False, 'from tvm import tir\n'), ((7515, 7541), 'tvm.tir.block', 'tir.block', (['[128, 126]', '"""C"""'], {}), "([128, 126], 'C')\n", (7524, 7541), False, 'from tvm import tir\n'), ((7797, 7823), 'tvm.tir.block', 'tir.block', (['[128, 126]', '"""C"""'], {}), "([128, 126], 'C')\n", (7806, 7823), False, 'from tvm import tir\n'), ((9542, 9578), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (9555, 9578), False, 'import pytest\n'), ((10433, 10469), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (10446, 10469), False, 'import pytest\n'), ((10691, 10727), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (10704, 10727), False, 'import pytest\n'), ((10936, 10972), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (10949, 10972), False, 'import pytest\n'), ((11469, 11505), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (11482, 11505), False, 'import pytest\n'), ((11686, 11722), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (11699, 11722), False, 'import pytest\n'), ((11897, 11933), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (11910, 11933), False, 'import pytest\n'), ((12098, 12134), 'pytest.raises', 'pytest.raises', (['tvm.tir.ScheduleError'], {}), '(tvm.tir.ScheduleError)\n', (12111, 12134), False, 'import pytest\n'), ((5429, 5471), 'tvm.tir.load', 'tir.load', (['"""float32"""', 'B.data', '(vi * 128 + vj)'], {}), "('float32', B.data, vi * 128 + vj)\n", (5437, 5471), False, 'from tvm import tir\n'), ((5957, 5998), 'tvm.tir.load', 'tir.load', (['"""float32"""', 'B.data', '(vi * 16 + vj)'], {}), "('float32', B.data, vi * 16 + vj)\n", (5965, 5998), False, 'from tvm import tir\n'), ((6755, 6781), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (6764, 6781), False, 'from tvm import tir\n'), ((6807, 6832), 'tvm.tir.where', 'tir.where', (['(B[i, j] < 10.0)'], {}), '(B[i, j] < 10.0)\n', (6816, 6832), False, 'from tvm import tir\n'), ((7091, 7117), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (7100, 7117), False, 'from tvm import tir\n'), ((7143, 7174), 'tvm.tir.where', 'tir.where', (['(A[i, j] * 2.0 < 10.0)'], {}), '(A[i, j] * 2.0 < 10.0)\n', (7152, 7174), False, 'from tvm import tir\n'), ((3249, 3275), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""B"""'], {}), "([128, 128], 'B')\n", (3258, 3275), False, 'from tvm import tir\n'), ((3305, 3320), 'tvm.tir.bind', 'tir.bind', (['vi', 'i'], {}), '(vi, i)\n', (3313, 3320), False, 'from tvm import tir\n'), ((3337, 3352), 'tvm.tir.bind', 'tir.bind', (['vj', 'j'], {}), '(vj, j)\n', (3345, 3352), False, 'from tvm import tir\n'), ((3451, 3477), 'tvm.tir.block', 'tir.block', (['[128, 128]', '"""C"""'], {}), "([128, 128], 'C')\n", (3460, 3477), False, 'from tvm import tir\n'), ((3507, 3522), 'tvm.tir.bind', 'tir.bind', (['vi', 'i'], {}), '(vi, i)\n', (3515, 3522), False, 'from tvm import tir\n'), ((3539, 3554), 'tvm.tir.bind', 'tir.bind', (['vj', 'j'], {}), '(vj, j)\n', (3547, 3554), False, 'from tvm import tir\n')]
Sage-Bionetworks/workflow-service
cwl_flask.py
8b5dc0afe9ea0972014cdf48a693ee6f893cfe5e
from flask import Flask, Response, request, redirect import subprocess import tempfile import json import yaml import signal import threading import time import copy app = Flask(__name__) jobs_lock = threading.Lock() jobs = [] class Job(threading.Thread): def __init__(self, jobid, path, inputobj): super(Job, self).__init__() self.jobid = jobid self.path = path self.inputobj = inputobj self.updatelock = threading.Lock() self.begin() def begin(self): loghandle, self.logname = tempfile.mkstemp() with self.updatelock: self.outdir = tempfile.mkdtemp() self.proc = subprocess.Popen(["cwl-runner", self.path, "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=loghandle, close_fds=True, cwd=self.outdir) self.status = { "id": "%sjobs/%i" % (request.url_root, self.jobid), "log": "%sjobs/%i/log" % (request.url_root, self.jobid), "run": self.path, "state": "Running", "input": json.loads(self.inputobj), "output": None} def run(self): self.stdoutdata, self.stderrdata = self.proc.communicate(self.inputobj) if self.proc.returncode == 0: outobj = yaml.load(self.stdoutdata, Loader=yaml.FullLoader) with self.updatelock: self.status["state"] = "Success" self.status["output"] = outobj else: with self.updatelock: self.status["state"] = "Failed" def getstatus(self): with self.updatelock: return self.status.copy() def cancel(self): if self.status["state"] == "Running": self.proc.send_signal(signal.SIGQUIT) with self.updatelock: self.status["state"] = "Canceled" def pause(self): if self.status["state"] == "Running": self.proc.send_signal(signal.SIGTSTP) with self.updatelock: self.status["state"] = "Paused" def resume(self): if self.status["state"] == "Paused": self.proc.send_signal(signal.SIGCONT) with self.updatelock: self.status["state"] = "Running" @app.route("/run", methods=['POST']) def runworkflow(): path = request.args["wf"] with jobs_lock: jobid = len(jobs) job = Job(jobid, path, request.stream.read()) job.start() jobs.append(job) return redirect("/jobs/%i" % jobid, code=303) @app.route("/jobs/<int:jobid>", methods=['GET', 'POST']) def jobcontrol(jobid): with jobs_lock: job = jobs[jobid] if request.method == 'POST': action = request.args.get("action") if action: if action == "cancel": job.cancel() elif action == "pause": job.pause() elif action == "resume": job.resume() status = job.getstatus() return json.dumps(status, indent=4), 200, "" def logspooler(job): with open(job.logname, "r") as f: while True: r = f.read(4096) if r: yield r else: with job.updatelock: if job.status["state"] != "Running": break time.sleep(1) @app.route("/jobs/<int:jobid>/log", methods=['GET']) def getlog(jobid): with jobs_lock: job = jobs[jobid] return Response(logspooler(job)) @app.route("/jobs", methods=['GET']) def getjobs(): with jobs_lock: jobscopy = copy.copy(jobs) def spool(jc): yield "[" first = True for j in jc: if first: yield json.dumps(j.getstatus(), indent=4) first = False else: yield ", " + json.dumps(j.getstatus(), indent=4) yield "]" return Response(spool(jobscopy)) if __name__ == "__main__": # app.debug = True app.run()
[((173, 188), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (178, 188), False, 'from flask import Flask, Response, request, redirect\n'), ((202, 218), 'threading.Lock', 'threading.Lock', ([], {}), '()\n', (216, 218), False, 'import threading\n'), ((2719, 2757), 'flask.redirect', 'redirect', (["('/jobs/%i' % jobid)"], {'code': '(303)'}), "('/jobs/%i' % jobid, code=303)\n", (2727, 2757), False, 'from flask import Flask, Response, request, redirect\n'), ((454, 470), 'threading.Lock', 'threading.Lock', ([], {}), '()\n', (468, 470), False, 'import threading\n'), ((548, 566), 'tempfile.mkstemp', 'tempfile.mkstemp', ([], {}), '()\n', (564, 566), False, 'import tempfile\n'), ((2936, 2962), 'flask.request.args.get', 'request.args.get', (['"""action"""'], {}), "('action')\n", (2952, 2962), False, 'from flask import Flask, Response, request, redirect\n'), ((3217, 3245), 'json.dumps', 'json.dumps', (['status'], {'indent': '(4)'}), '(status, indent=4)\n', (3227, 3245), False, 'import json\n'), ((3829, 3844), 'copy.copy', 'copy.copy', (['jobs'], {}), '(jobs)\n', (3838, 3844), False, 'import copy\n'), ((623, 641), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ([], {}), '()\n', (639, 641), False, 'import tempfile\n'), ((666, 816), 'subprocess.Popen', 'subprocess.Popen', (["['cwl-runner', self.path, '-']"], {'stdin': 'subprocess.PIPE', 'stdout': 'subprocess.PIPE', 'stderr': 'loghandle', 'close_fds': '(True)', 'cwd': 'self.outdir'}), "(['cwl-runner', self.path, '-'], stdin=subprocess.PIPE,\n stdout=subprocess.PIPE, stderr=loghandle, close_fds=True, cwd=self.outdir)\n", (682, 816), False, 'import subprocess\n'), ((1500, 1550), 'yaml.load', 'yaml.load', (['self.stdoutdata'], {'Loader': 'yaml.FullLoader'}), '(self.stdoutdata, Loader=yaml.FullLoader)\n', (1509, 1550), False, 'import yaml\n'), ((2640, 2661), 'flask.request.stream.read', 'request.stream.read', ([], {}), '()\n', (2659, 2661), False, 'from flask import Flask, Response, request, redirect\n'), ((1282, 1307), 'json.loads', 'json.loads', (['self.inputobj'], {}), '(self.inputobj)\n', (1292, 1307), False, 'import json\n'), ((3565, 3578), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (3575, 3578), False, 'import time\n')]
JulyKikuAkita/PythonPrac
cs15211/ReverseBits.py
0ba027d9b8bc7c80bc89ce2da3543ce7a49a403c
__source__ = 'https://leetcode.com/problems/reverse-bits/description/' # https://github.com/kamyu104/LeetCode/blob/master/Python/reverse-bits.py # Time : O(n) # Space: O(1) # Bit Manipulation # # Description: Leetcode # 190. Reverse Bits # # Reverse bits of a given 32 bits unsigned integer. # # For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), # return 964176192 (represented in binary as 00111001011110000010100101000000). # # Follow up: # If this function is called many times, how would you optimize it? # # Companies # Apple Airbnb # Related Topics # Bit Manipulation # Similar Questions # Number of 1 Bits # import unittest class Solution: # @param n, an integer # @return an integer def reverseBits(self, n): result = 0 for i in xrange(32): result <<= 1 result |= n & 1 n >>= 1 return result class TestMethods(unittest.TestCase): def test_Local(self): self.assertEqual(1, 1) print Solution().reverseBits(1) if __name__ == '__main__': unittest.main() Java = ''' # Thought: # 1ms 100% class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int ret = 0; for (int i = 0; i < 32; i++) { if ((n & 1) != 0) { ret |= 1; //same as // res += n & 1 } n >>>= 1; // padding 0 on the left side if (i < 31) { // CATCH: for last digit, don't shift! ret <<= 1; } } return ret; } } We first intitialize result to 0. We then iterate from 0 to 31 (an integer has 32 bits). In each iteration: We first shift result to the left by 1 bit. Then, if the last digit of input n is 1, we add 1 to result. To find the last digit of n, we just do: (n & 1) Example, if n=5 (101), n&1 = 101 & 001 = 001 = 1; however, if n = 2 (10), n&1 = 10 & 01 = 0). Finally, we update n by shifting it to the right by 1 (n >>= 1) At the end of the iteration, we return result. Example, if input n = 13 (represented in binary as 0000_0000_0000_0000_0000_0000_0000_1101, the "_" is for readability), calling reverseBits(13) should return: 1011_0000_0000_0000_0000_0000_0000_0000 Here is how our algorithm would work for input n = 13: Initially, result = 0 = 0000_0000_0000_0000_0000_0000_0000_0000, n = 13 = 0000_0000_0000_0000_0000_0000_0000_1101 Starting for loop: i = 0: result = result << 1 = 0000_0000_0000_0000_0000_0000_0000_0000. n&1 = 0000_0000_0000_0000_0000_0000_0000_1101 & 0000_0000_0000_0000_0000_0000_0000_0001 = 0000_0000_0000_0000_0000_0000_0000_0001 = 1 therefore result = result + 1 = 0000_0000_0000_0000_0000_0000_0000_0000 + 0000_0000_0000_0000_0000_0000_0000_0001 = 0000_0000_0000_0000_0000_0000_0000_0001 = 1 We right shift n by 1 (n >>= 1) to get: n = 0000_0000_0000_0000_0000_0000_0000_0110. We then go to the next iteration. i = 1: result = result << 1 = 0000_0000_0000_0000_0000_0000_0000_0010; n&1 = 0000_0000_0000_0000_0000_0000_0000_0110 & 0000_0000_0000_0000_0000_0000_0000_0001 = 0000_0000_0000_0000_0000_0000_0000_0000 = 0; therefore we don't increment result. We right shift n by 1 (n >>= 1) to get: n = 0000_0000_0000_0000_0000_0000_0000_0011. We then go to the next iteration. i = 2: result = result << 1 = 0000_0000_0000_0000_0000_0000_0000_0100. n&1 = 0000_0000_0000_0000_0000_0000_0000_0011 & 0000_0000_0000_0000_0000_0000_0000_0001 = 0000_0000_0000_0000_0000_0000_0000_0001 = 1 therefore result = result + 1 = 0000_0000_0000_0000_0000_0000_0000_0100 + 0000_0000_0000_0000_0000_0000_0000_0001 = result = 0000_0000_0000_0000_0000_0000_0000_0101 We right shift n by 1 to get: n = 0000_0000_0000_0000_0000_0000_0000_0001. We then go to the next iteration. i = 3: result = result << 1 = 0000_0000_0000_0000_0000_0000_0000_1010. n&1 = 0000_0000_0000_0000_0000_0000_0000_0001 & 0000_0000_0000_0000_0000_0000_0000_0001 = 0000_0000_0000_0000_0000_0000_0000_0001 = 1 therefore result = result + 1 = = 0000_0000_0000_0000_0000_0000_0000_1011 We right shift n by 1 to get: n = 0000_0000_0000_0000_0000_0000_0000_0000 = 0. Now, from here to the end of the iteration, n is 0, so (n&1) will always be 0 and n >>=1 will not change n. The only change will be for result <<=1, i.e. shifting result to the left by 1 digit. Since there we have i=4 to i = 31 iterations left, this will result in padding 28 0's to the right of result. i.e at the end, we get result = 1011_0000_0000_0000_0000_0000_0000_0000 This is exactly what we expected to get # 1ms 100% class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { if (n == 0) return 0; int result = 0; for (int i = 0; i < 32; i++) { result <<= 1; if ((n & 1) == 1) result++; n >>= 1; } return result; } } # 1ms 100% class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { n = ((n & 0x55555555) << 1) | ((n & 0xAAAAAAAA) >>> 1); n = ((n & 0x33333333) << 2) | ((n & 0xCCCCCCCC) >>> 2); n = ((n & 0x0F0F0F0F) << 4) | ((n & 0xF0F0F0F0) >>> 4); n = ((n & 0x00FF00FF) << 8) | ((n & 0xFF00FF00) >>> 8); return (n >>> 16) | (n << 16); } } '''
[]
freezecoder/tibanna
tibanna/_version.py
89b02e95d04b9c630a9786f4d1eb8157c40098e0
"""Version information.""" # The following line *must* be the last in the module, exactly as formatted: __version__ = "0.16.1"
[]
vs-slavchev/selective_merge_pdf
selective_merge_pdf.py
b24b4dbcaf1ffb8dc0924dafec56f94e452c1ebd
from sys import argv from PyPDF2 import PdfFileReader, PdfFileWriter import re range_pattern = re.compile(r'(\d+)(\.\.|-)(\d+)') comma_pattern = re.compile('\d+(,\d+)*') def pages_args_to_array(pages_str): groups = range_pattern.search(pages_str) if groups: start = int(groups.group(1)) end = int(groups.group(3)) return list(range(start, end + 1)) elif comma_pattern.search(pages_str): return [int(d) for d in pages_str.split(',')] else: raise Exception('pages should be like 1,2,3 or 1-3, but was {}' .format(pages_str)) if __name__ == '__main__': assert(len(argv) > 1), "usage examle:\npython3 selective_merge_pdf.py file1.pdf 1-3 file2.pdf 3,4,10 file1.pdf 50" assert(len(argv) % 2 == 1), "invalid arguments; supply page numbers after each pdf name" files_names = argv[1::2] pages_args = argv[2::2] pdf_writer = PdfFileWriter() for file_name, pages in zip(files_names, pages_args): pdf_reader = PdfFileReader(file_name) last_page_index = pdf_reader.getNumPages() pages = pages_args_to_array(pages) pages_to_add = list(filter(lambda i: i >= 0 and i <= last_page_index, pages)) for page in pages_to_add: pdf_writer.addPage(pdf_reader.getPage(page - 1)) with open("merged.pdf", 'wb') as out: pdf_writer.write(out)
[((97, 133), 're.compile', 're.compile', (['"""(\\\\d+)(\\\\.\\\\.|-)(\\\\d+)"""'], {}), "('(\\\\d+)(\\\\.\\\\.|-)(\\\\d+)')\n", (107, 133), False, 'import re\n'), ((147, 173), 're.compile', 're.compile', (['"""\\\\d+(,\\\\d+)*"""'], {}), "('\\\\d+(,\\\\d+)*')\n", (157, 173), False, 'import re\n'), ((845, 860), 'PyPDF2.PdfFileWriter', 'PdfFileWriter', ([], {}), '()\n', (858, 860), False, 'from PyPDF2 import PdfFileReader, PdfFileWriter\n'), ((931, 955), 'PyPDF2.PdfFileReader', 'PdfFileReader', (['file_name'], {}), '(file_name)\n', (944, 955), False, 'from PyPDF2 import PdfFileReader, PdfFileWriter\n')]
romack77/vp-toolbox
vp/scoring.py
2677b78b80d0b4794735f3ee9bd70403c6b884e6
import math from vp import geom_tools def horizon_error(ground_truth_horizon, detected_horizon, image_dims): """Calculates error in a detected horizon. This measures the max distance between the detected horizon line and the ground truth horizon line, within the image's x-axis, and normalized by image height. Args: ground_truth_horizon: Tuple with (slope, intercept) for the GT horizon line. detected_horizon: Tuple with (slope, intercept) for the detected horizon line. image_dims: Tuple of integers, (width, height) of the image, in pixels. Returns: Float, or None if a horizon is missing altogether. """ if ground_truth_horizon is None or detected_horizon is None: return None def gt(x): return ground_truth_horizon[0] * x + ground_truth_horizon[1] def dt(x): return detected_horizon[0] * x + detected_horizon[1] width, height = image_dims return max(abs(gt(0) - dt(0)), abs(gt(width) - dt(width))) / height def vp_direction_error(ground_truth_vps, detected_vps, image_dims): """Measures error in direction from center of detected vanishing points. Each detected VP is matched with its closest unclaimed ground truth VP. Args: ground_truth_vps: List of ground truth VP point tuples. detected_vps: List of detected VP point tuples. image_dims: Tuple of integers, (width, height) of the image, in pixels. Returns: List with float degrees of error for each ground truth VP. Error is None for missing VPs. """ principal_point = (image_dims[0] // 2, image_dims[1] // 2) point_pair_dists = [] for gt_vp in ground_truth_vps: for dt_vp in detected_vps: gt_angle = geom_tools.get_line_angle(( principal_point[0], principal_point[1], gt_vp[0], gt_vp[1])) dt_angle = geom_tools.get_line_angle(( principal_point[0], principal_point[1], dt_vp[0], dt_vp[1])) angle_diff = 180 - abs(abs(gt_angle - dt_angle) - 180) point_pair_dists.append((angle_diff, gt_vp, dt_vp)) point_pair_dists = sorted(point_pair_dists, key=lambda k: k[0]) gt_vp_to_error = {} seen_dt_vps = set() for distance, gt_vp, dt_vp in point_pair_dists: if gt_vp in gt_vp_to_error or dt_vp in seen_dt_vps: continue gt_vp_to_error[gt_vp] = distance seen_dt_vps.add(dt_vp) return [gt_vp_to_error.get(gt, None) for gt in ground_truth_vps] def location_accuracy_error(ground_truth_vps, detected_vps): """Measures average error in the location of detected vanishing points. "Missed" or "extra" VPs do not count against the score. Based on log distance of detected vp from ground truth vp. Args: ground_truth_vps: List of ground truth VP point tuples. detected_vps: List of detected VP point tuples. Returns: Float, error. """ if len(ground_truth_vps) == 0 or len(detected_vps) == 0: return 0 point_pair_dists = [] for gt_vp in ground_truth_vps: for dt_vp in detected_vps: distance = geom_tools.point_to_point_dist(gt_vp, dt_vp) point_pair_dists.append((distance, gt_vp, dt_vp)) sorted(point_pair_dists, key=lambda k: k[0]) seen_gt_vps = set() seen_dt_vps = set() total_error = 0 for distance, gt_vp, dt_vp in point_pair_dists: if gt_vp in seen_gt_vps or dt_vp in seen_dt_vps: continue seen_gt_vps.add(gt_vp) seen_dt_vps.add(dt_vp) if distance > 0: total_error += math.log(distance) return total_error / min(len(detected_vps), len(ground_truth_vps)) def num_model_detection_error(ground_truth_vps, detected_vps): """Measures error in the number of detected vanishing points. Returns: Integer, positive when there are too many VPs, negative when there are too few. """ return len(detected_vps) - len(ground_truth_vps)
[((1771, 1862), 'vp.geom_tools.get_line_angle', 'geom_tools.get_line_angle', (['(principal_point[0], principal_point[1], gt_vp[0], gt_vp[1])'], {}), '((principal_point[0], principal_point[1], gt_vp[0],\n gt_vp[1]))\n', (1796, 1862), False, 'from vp import geom_tools\n'), ((1899, 1990), 'vp.geom_tools.get_line_angle', 'geom_tools.get_line_angle', (['(principal_point[0], principal_point[1], dt_vp[0], dt_vp[1])'], {}), '((principal_point[0], principal_point[1], dt_vp[0],\n dt_vp[1]))\n', (1924, 1990), False, 'from vp import geom_tools\n'), ((3164, 3208), 'vp.geom_tools.point_to_point_dist', 'geom_tools.point_to_point_dist', (['gt_vp', 'dt_vp'], {}), '(gt_vp, dt_vp)\n', (3194, 3208), False, 'from vp import geom_tools\n'), ((3634, 3652), 'math.log', 'math.log', (['distance'], {}), '(distance)\n', (3642, 3652), False, 'import math\n')]
collassubmission91/CompoSuite-Code
compositional-rl-benchmark/composition/spinningup_training/train_mtl_ppo.py
ac544efb68a11ed8a483b0932975c4949f0cec90
import numpy as np import argparse import composition import os import json import torch from spinup.algos.pytorch.ppo.core import MLPActorCritic from spinup.algos.pytorch.ppo.ppo import ppo from spinup.utils.run_utils import setup_logger_kwargs from spinup.utils.mpi_tools import proc_id, num_procs def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--data-dir', default='spinningup_training/logs') parser.add_argument('--load-dir', default=None) parser.add_argument('--gridsearch-id', type=int, default=-1) parser.add_argument('--task-id', type=int, default=-1) parser.add_argument('--hid', type=int, default=256) parser.add_argument('--l', type=int, default=2) parser.add_argument('--gamma', type=float, default=0.99) parser.add_argument('--seed', '-s', type=int, default=4) parser.add_argument('--cpu', type=int, default=4) parser.add_argument('--steps', type=int, default=16000) parser.add_argument('--epochs', type=int, default=625) parser.add_argument('--exp-name', type=str, default='ppo') parser.add_argument('--clip', type=float, default=0.2) parser.add_argument('--pi-lr', type=float, default=1e-4) parser.add_argument('--vf-lr', type=float, default=1e-4) parser.add_argument('--pi-iters', type=int, default=128) parser.add_argument('--vf-iters', type=int, default=128) parser.add_argument('--target-kl', type=float, default=0.02) parser.add_argument('--ent-coef', type=float, default=0.02) parser.add_argument('--log-std-init', type=float, default=0.) parser.add_argument('--controller', type=str, default="joint") parser.add_argument('--robot', type=str, default="IIWA") parser.add_argument('--object', type=str, default="Hollowbox") parser.add_argument('--obstacle', type=str, default=None) parser.add_argument('--task', type=str, default="PickPlace") parser.add_argument('--horizon', type=int, default=500) args = parser.parse_args() np.random.seed(args.seed) task_list = np.random.choice(256, num_procs(), replace=False) args.task_id = int(task_list[proc_id()]) _robots = ["IIWA", "Jaco", "Kinova3", "Panda"] _objects = ["Box", "Dumbbell", "Plate", "Hollowbox"] _objectives = ["PickPlace", "Push", "Shelf", "Trashcan"] _obstacles = ["None", "GoalWall", "ObjectDoor", "ObjectWall"] idx = np.unravel_index(args.task_id, (len(_robots), len(_objects), len(_objectives), len(_obstacles))) args.robot = _robots[idx[0]] args.object = _objects[idx[1]] args.task = _objectives[idx[2]] args.obstacle = _obstacles[idx[3]] # args.exp_name = "t:" + str(args.task_id) + "_name:" + args.exp_name + "_robot:" + str(args.robot) + "_task:" + str(args.task) + "_object:" + str(args.object) + "_obstacle:" + str(args.obstacle) args.exp_name = 'MTL_{}'.format(len(task_list)) return args def main(): torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False torch.set_num_threads(1) args = parse_args() os.makedirs(os.path.join(args.data_dir, args.exp_name), exist_ok=True) with open(os.path.join(args.data_dir, args.exp_name, 'args_{}.json'.format(proc_id())), 'w') as f: json.dump(args.__dict__, f, indent=2) logger_kwargs = setup_logger_kwargs( args.exp_name, data_dir=args.data_dir) checkpoint = None if args.load_dir is not None: checkpoint = torch.load(os.path.join(args.load_dir, 'pyt_save', 'state_dicts.pt')) ppo(lambda: composition.make( args.robot, args.object, args.obstacle, args.task, args.controller, args.horizon, use_task_id_obs=True), actor_critic=MLPActorCritic, ac_kwargs=dict(hidden_sizes=[args.hid]*args.l, log_std_init=args.log_std_init), seed=args.seed, gamma=args.gamma, steps_per_epoch=args.steps, epochs=args.epochs, clip_ratio=args.clip, pi_lr=args.pi_lr, vf_lr=args.vf_lr, train_pi_iters=args.pi_iters, train_v_iters=args.vf_iters, target_kl=args.target_kl, logger_kwargs=logger_kwargs, max_ep_len=args.horizon, ent_coef=args.ent_coef, log_per_proc=True, checkpoint=checkpoint) if __name__ == '__main__': main()
[((335, 360), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (358, 360), False, 'import argparse\n'), ((1997, 2022), 'numpy.random.seed', 'np.random.seed', (['args.seed'], {}), '(args.seed)\n', (2011, 2022), True, 'import numpy as np\n'), ((2998, 3022), 'torch.set_num_threads', 'torch.set_num_threads', (['(1)'], {}), '(1)\n', (3019, 3022), False, 'import torch\n'), ((3293, 3351), 'spinup.utils.run_utils.setup_logger_kwargs', 'setup_logger_kwargs', (['args.exp_name'], {'data_dir': 'args.data_dir'}), '(args.exp_name, data_dir=args.data_dir)\n', (3312, 3351), False, 'from spinup.utils.run_utils import setup_logger_kwargs\n'), ((2061, 2072), 'spinup.utils.mpi_tools.num_procs', 'num_procs', ([], {}), '()\n', (2070, 2072), False, 'from spinup.utils.mpi_tools import proc_id, num_procs\n'), ((3064, 3106), 'os.path.join', 'os.path.join', (['args.data_dir', 'args.exp_name'], {}), '(args.data_dir, args.exp_name)\n', (3076, 3106), False, 'import os\n'), ((3234, 3271), 'json.dump', 'json.dump', (['args.__dict__', 'f'], {'indent': '(2)'}), '(args.__dict__, f, indent=2)\n', (3243, 3271), False, 'import json\n'), ((3450, 3507), 'os.path.join', 'os.path.join', (['args.load_dir', '"""pyt_save"""', '"""state_dicts.pt"""'], {}), "(args.load_dir, 'pyt_save', 'state_dicts.pt')\n", (3462, 3507), False, 'import os\n'), ((3526, 3651), 'composition.make', 'composition.make', (['args.robot', 'args.object', 'args.obstacle', 'args.task', 'args.controller', 'args.horizon'], {'use_task_id_obs': '(True)'}), '(args.robot, args.object, args.obstacle, args.task, args.\n controller, args.horizon, use_task_id_obs=True)\n', (3542, 3651), False, 'import composition\n'), ((2123, 2132), 'spinup.utils.mpi_tools.proc_id', 'proc_id', ([], {}), '()\n', (2130, 2132), False, 'from spinup.utils.mpi_tools import proc_id, num_procs\n'), ((3202, 3211), 'spinup.utils.mpi_tools.proc_id', 'proc_id', ([], {}), '()\n', (3209, 3211), False, 'from spinup.utils.mpi_tools import proc_id, num_procs\n')]
m4sterchain/mesapy
pypy/interpreter/test/test_generator.py
ed546d59a21b36feb93e2309d5c6b75aa0ad95c9
class AppTestGenerator: def test_generator(self): def f(): yield 1 assert f().next() == 1 def test_generator2(self): def f(): yield 1 g = f() assert g.next() == 1 raises(StopIteration, g.next) def test_attributes(self): def f(): yield 1 assert g.gi_running g = f() assert g.gi_code is f.__code__ assert g.__name__ == 'f' assert g.gi_frame is not None assert not g.gi_running g.next() assert not g.gi_running raises(StopIteration, g.next) assert not g.gi_running assert g.gi_frame is None assert g.gi_code is f.__code__ assert g.__name__ == 'f' def test_generator3(self): def f(): yield 1 g = f() assert list(g) == [1] def test_generator4(self): def f(): yield 1 g = f() assert [x for x in g] == [1] def test_generator5(self): d = {} exec """if 1: def f(): v = (yield ) yield v g = f() g.next() """ in d g = d['g'] assert g.send(42) == 42 def test_throw1(self): def f(): yield 2 g = f() # two arguments version raises(NameError, g.throw, NameError, "Error") def test_throw2(self): def f(): yield 2 g = f() # single argument version raises(NameError, g.throw, NameError("Error")) def test_throw3(self): def f(): try: yield 1 yield 2 except: yield 3 g = f() assert g.next() == 1 assert g.throw(NameError("Error")) == 3 raises(StopIteration, g.next) def test_throw4(self): d = {} exec """if 1: def f(): try: yield 1 v = (yield 2) except: yield 3 g = f() """ in d g = d['g'] assert g.next() == 1 assert g.next() == 2 assert g.throw(NameError("Error")) == 3 raises(StopIteration, g.next) def test_throw5(self): def f(): try: yield 1 except: x = 3 try: yield x except: pass g = f() g.next() # String exceptions are not allowed anymore raises(TypeError, g.throw, "Error") assert g.throw(Exception) == 3 raises(StopIteration, g.throw, Exception) def test_throw6(self): def f(): yield 2 g = f() raises(NameError, g.throw, NameError, "Error", None) def test_throw_fail(self): def f(): yield 1 g = f() raises(TypeError, g.throw, NameError("Error"), "error") def test_throw_fail2(self): def f(): yield 1 g = f() raises(TypeError, g.throw, list()) def test_throw_fail3(self): def f(): yield 1 g = f() raises(TypeError, g.throw, NameError("Error"), None, "not tb object") def test_throw_finishes_generator(self): def f(): yield 1 g = f() assert g.gi_frame is not None raises(ValueError, g.throw, ValueError) assert g.gi_frame is None def test_throw_bug(self): def f(): try: x.throw(IndexError) # => "generator already executing" except ValueError: yield 1 x = f() res = list(x) assert res == [1] def test_throw_on_finished_generator(self): def f(): yield 1 g = f() res = g.next() assert res == 1 raises(StopIteration, g.next) raises(NameError, g.throw, NameError) def test_close(self): def f(): yield 1 g = f() assert g.close() is None def test_close2(self): def f(): try: yield 1 except GeneratorExit: raise StopIteration g = f() g.next() assert g.close() is None def test_close3(self): def f(): try: yield 1 except GeneratorExit: raise NameError g = f() g.next() raises(NameError, g.close) def test_close_fail(self): def f(): try: yield 1 except GeneratorExit: yield 2 g = f() g.next() raises(RuntimeError, g.close) def test_close_on_collect(self): ## we need to exec it, else it won't run on python2.4 d = {} exec """ def f(): try: yield finally: f.x = 42 """.strip() in d g = d['f']() g.next() del g import gc gc.collect() assert d['f'].x == 42 def test_generator_raises_typeerror(self): def f(): yield 1 g = f() raises(TypeError, g.send) # one argument required raises(TypeError, g.send, 1) # not started, must send None def test_generator_explicit_stopiteration(self): def f(): yield 1 raise StopIteration g = f() assert [x for x in g] == [1] def test_generator_propagate_stopiteration(self): def f(): it = iter([1]) while 1: yield it.next() g = f() assert [x for x in g] == [1] def test_generator_restart(self): def g(): i = me.next() yield i me = g() raises(ValueError, me.next) def test_generator_expression(self): exec "res = sum(i*i for i in range(5))" assert res == 30 def test_generator_expression_2(self): d = {} exec """ def f(): total = sum(i for i in [x for x in z]) return total, x z = [1, 2, 7] res = f() """ in d assert d['res'] == (10, 7) def test_repr(self): def myFunc(): yield 1 g = myFunc() r = repr(g) assert r.startswith("<generator object myFunc at 0x") assert list(g) == [1] assert repr(g) == r def test_unpackiterable_gen(self): g = (i*i for i in range(-5, 3)) assert set(g) == set([0, 1, 4, 9, 16, 25]) assert set(g) == set() assert set(i for i in range(0)) == set() def test_explicit_stop_iteration_unpackiterable(self): def f(): yield 1 raise StopIteration assert tuple(f()) == (1,) def test_exception_is_cleared_by_yield(self): def f(): try: foobar except NameError: yield 5 raise # should raise "no active exception to re-raise" gen = f() next(gen) # --> 5 try: next(gen) except TypeError: pass def test_multiple_invalid_sends(self): def mygen(): yield 42 g = mygen() raises(TypeError, g.send, 2) raises(TypeError, g.send, 2) def test_should_not_inline(space): from pypy.interpreter.generator import should_not_inline w_co = space.appexec([], '''(): def g(x): yield x + 5 return g.__code__ ''') assert should_not_inline(w_co) == False w_co = space.appexec([], '''(): def g(x): yield x + 5 yield x + 6 return g.__code__ ''') assert should_not_inline(w_co) == True
[]
innogames/igvm
igvm/cli.py
6c4bd98d61ebaf6280698e74d560ea5b3d70cd9e
"""igvm - The command line interface Copyright (c) 2017 InnoGames GmbH """ from __future__ import print_function from argparse import ArgumentParser, _SubParsersAction from logging import StreamHandler, root as root_logger import time from fabric.network import disconnect_all from igvm.commands import ( change_address, disk_set, evacuate, host_info, mem_set, vcpu_set, vm_build, vm_delete, vm_migrate, vm_rename, vm_restart, vm_start, vm_stop, vm_sync, vm_define, ) from igvm.libvirt import close_virtconns class ColorFormatters(): BOLD = '\033[1m{}\033[0m' WARNING = '\033[1;33m{}\033[0m' ERROR = '\033[1;31m{}\033[0m' CRITICAL = '\033[1;41m{}\033[0m' class IGVMArgumentParser(ArgumentParser): def format_help(self): if not any(isinstance(a, _SubParsersAction) for a in self._actions): return super(IGVMArgumentParser, self).format_help() out = [] out.append(ColorFormatters.BOLD.format(__doc__)) out.append('Available commands:\n') subparsers_actions = [ action for action in self._actions if isinstance(action, _SubParsersAction) ] # There will probably only be one subparser_action, but better safe # than sorry. for subparsers_action in subparsers_actions: # Get all subparsers and print help for choice, subparser in subparsers_action.choices.items(): out.append(ColorFormatters.BOLD.format(choice)) if subparser.get_default('func').__doc__: out.append('\n'.join( '\t{}'.format(l.strip()) for l in subparser .get_default('func').__doc__.strip().splitlines() )) out.append('\n\t{}'.format(subparser.format_usage())) return '\n'.join(out) class IGVMLogHandler(StreamHandler): """Extend StreamHandler to format messages short-cutting Formatters""" def __init__(self, *args, **kwargs): super(IGVMLogHandler, self).__init__(*args, **kwargs) self.isatty = self.stream.isatty() def format(self, record): level = record.levelname msg = '{}: {}: {}'.format(level, record.name, record.getMessage()) if self.isatty and level in vars(ColorFormatters): msg = getattr(ColorFormatters, level).format(msg) return msg def parse_args(): top_parser = IGVMArgumentParser('igvm') top_parser.add_argument('--silent', '-s', action='count', default=0) top_parser.add_argument('--verbose', '-v', action='count', default=0) subparsers = top_parser.add_subparsers(help='Actions') subparser = subparsers.add_parser( 'build', description=vm_build.__doc__, ) subparser.set_defaults(func=vm_build) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( '--postboot', metavar='postboot_script', help='Run postboot_script on the guest after first boot', ) subparser.add_argument( '--skip-puppet', action='store_false', dest='run_puppet', help='Skip running puppet in chroot before powering up', ) subparser.add_argument( '--debug-puppet', action='store_true', help='Run puppet in debug mode', ) subparser.add_argument( '--ignore-reserved', dest='allow_reserved_hv', action='store_true', help='Allow building on a Host which has the state online_reserved', ) subparser.add_argument( '--rebuild', dest='rebuild', action='store_true', help='Rebuild already defined VM or build it if not defined', ) subparser.add_argument( '--soft-preferences', dest='soft_preferences', action='store_true', help='Overrules all preferences so that Hypervisors are not excluded. ' 'Use this if igvm fails to find a matching Hypervisor, but you ' 'are in urgent need to do it anyway. Hint: If igvm fails to find ' 'a matching Hypervisor something might be really wrong. Run igvm ' 'with --verbose to check why it fails finding a Hypervisor.', ) subparser = subparsers.add_parser( 'migrate', description=vm_migrate.__doc__, ) subparser.set_defaults(func=vm_migrate) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'hypervisor_hostname', nargs='?', default=None, help='Hostname of destination hypervisor', ) subparser.add_argument( '--run-puppet', action='store_true', help='Run puppet in chroot before powering up', ) subparser.add_argument( '--debug-puppet', action='store_true', help='Run puppet in debug mode', ) subparser.add_argument( '--offline', action='store_true', help='Force offline migration', ) subparser.add_argument( '--ignore-reserved', dest='allow_reserved_hv', action='store_true', help='Allow migration to a Host which has the state online_reserved', ) subparser.add_argument( '--offline-transport', default='drbd', choices=('drbd', 'netcat', 'xfs'), help=( 'Specify drbd (default), netcat or xfs transport to migrate ' 'disk image' ), ) subparser.add_argument( '--no-shutdown', action='store_true', help=( 'Don\'t shutdown VM during offline migration, igvm will wait for' ' operator to shut down VM for 24h.' ), ) subparser.add_argument( '--enforce-vm-env', dest='enforce_vm_env', action='store_true', help='Build or migrate VM only to a HV with the same environment of VM' ) subparser.add_argument( '--disk-size', dest='disk_size', type=int, help='Resize disk of migrated VM. Expects new size in GiB. ' 'Works only with --offline --offline-transport=xfs', ) subparser.add_argument( '--soft-preferences', dest='soft_preferences', action='store_true', help='Overrules all preferences so that Hypervisors are not excluded. ' 'Use this if igvm fails to find a matching Hypervisor, but you ' 'are in urgent need to do it anyway. Hint: If igvm fails to find ' 'a matching Hypervisor something might be really wrong. Run igvm ' 'with --verbose to check why it fails finding a Hypervisor.', ) subparser = subparsers.add_parser( 'change-address', description=disk_set.__doc__, ) subparser.set_defaults(func=change_address) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'new_address', help=( 'New IPv4 address of VM' ) ) subparser.add_argument( '--offline', action='store_true', help='Perform IP address change offline', ) subparser.add_argument( '--migrate', action='store_true', help='Migrate VM to new HV while changing IP address', ) subparser.add_argument( '--ignore-reserved', dest='allow_reserved_hv', action='store_true', help='Allow migration to a Host which has the state online_reserved', ) subparser.add_argument( '--offline-transport', default='drbd', help=( 'Specify drbd (default) or netcat transport to migrate disk image' ), ) subparser = subparsers.add_parser( 'disk-set', description=disk_set.__doc__, ) subparser.set_defaults(func=disk_set) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'size', help=( 'New disk size with an optional unit (default GiB). ' 'Can be specified relative with "+". Only integers are allowed' ) ) subparser = subparsers.add_parser( 'mem-set', description=mem_set.__doc__, ) subparser.set_defaults(func=mem_set) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'size', help=( 'New memory size with optional unit (default is MiB).' 'Only integers are allowed.' ), ) subparser.add_argument( '--offline', action='store_true', help='Shutdown VM, change memory, and restart VM', ) subparser = subparsers.add_parser( 'vcpu-set', description=vcpu_set.__doc__, ) subparser.set_defaults(func=vcpu_set) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'count', type=int, help='New number of CPUs', ) subparser.add_argument( '--offline', action='store_true', help='Shutdown VM, change CPUs, and restart VM', ) subparser = subparsers.add_parser( 'start', description=vm_start.__doc__, ) subparser.set_defaults(func=vm_start) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( '--unretire', nargs='?', const='maintenance', help='Unretire a VM, set it to given state, maintenance by default', ) subparser = subparsers.add_parser( 'stop', description=vm_stop.__doc__, ) subparser.set_defaults(func=vm_stop) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( '--force', action='store_true', help='Do not wait for guest to shutdown gracefully', ) subparser.add_argument( '--retire', action='store_true', help='Retire VM after stopping it', ) subparser = subparsers.add_parser( 'restart', description=vm_restart.__doc__, ) subparser.set_defaults(func=vm_restart) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( '--force', action='store_true', help='Do not wait for guest to shutdown gracefully', ) subparser.add_argument( '--no-redefine', action='store_true', help='Do not redefine the domain to use latest hypervisor settings', ) subparser = subparsers.add_parser( 'delete', description=vm_delete.__doc__, ) subparser.set_defaults(func=vm_delete) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( '--retire', action='store_true', help='Set VM state to "retired" on Serveradmin instead of deleting', ) subparser = subparsers.add_parser( 'info', description=host_info.__doc__, ) subparser.set_defaults(func=host_info) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser = subparsers.add_parser( 'sync', description=vm_sync.__doc__, ) subparser.set_defaults(func=vm_sync) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser = subparsers.add_parser( 'rename', description=vm_rename.__doc__, ) subparser.set_defaults(func=vm_rename) subparser.add_argument( 'vm_hostname', help='Hostname of the guest system', ) subparser.add_argument( 'new_hostname', help='New hostname', ) subparser.add_argument( '--offline', action='store_true', help='Shutdown VM, if running', ) subparser = subparsers.add_parser( 'evacuate', description=evacuate.__doc__, ) subparser.set_defaults(func=evacuate) subparser.add_argument( 'hv_hostname', help='Hostname of the hypervisor', ) subparser.add_argument( 'dst_hv_hostname', nargs='?', default=None, help='Hostname of destination hypervisor', ) subparser.add_argument( '--dry-run', action='store_true', help='Do not migrate but just print what would be done' ) subparser.add_argument( '--offline', nargs='*', help='Migrate VMs matching the given serveradmin function offline', ) subparser.add_argument( '--ignore-reserved', dest='allow_reserved_hv', action='store_true', help='Allow migrating to a host which has the state online_reserved', ) subparser.add_argument( '--soft-preferences', dest='soft_preferences', action='store_true', help='Overrules all preferences so that Hypervisors are not excluded. ' 'Use this if igvm fails to find a matching Hypervisor, but you ' 'are in urgent need to do it anyway. Hint: If igvm fails to find ' 'a matching Hypervisor something might be really wrong. Run igvm ' 'with --verbose to check why it fails finding a Hypervisor.', ) subparser = subparsers.add_parser( 'define', description=vm_define.__doc__, ) subparser.set_defaults(func=vm_define) subparser.add_argument('vm_hostname', help='Hostname of the guest system') return vars(top_parser.parse_args()) def main(): args = parse_args() configure_root_logger(args.pop('silent'), args.pop('verbose')) try: args.pop('func')(**args) finally: # Fabric requires the disconnect function to be called after every # use. We are also taking our chance to disconnect from # the hypervisors. disconnect_all() close_virtconns() # The underlying library of Fabric, Paramiko, raises an error, on # destruction right after the disconnect function is called. We are # sleeping for a little while to avoid this. time.sleep(0.1) def configure_root_logger(silent, verbose): root_logger.addHandler(IGVMLogHandler()) # We are summing up the silent and verbose arguments in here. It # is not really meaningful to use them both, but giving an error is not # better. See Python logging library documentation [1] for the levels. # Paramiko is overly verbose. We configure it for one level higher. # # [1] https://docs.python.org/library/logging.html#logging-levels level = 20 + (silent - verbose) * 10 root_logger.setLevel(level) root_logger.getChild('paramiko').setLevel(level + 10)
[((15035, 15062), 'logging.root.setLevel', 'root_logger.setLevel', (['level'], {}), '(level)\n', (15055, 15062), True, 'from logging import StreamHandler, root as root_logger\n'), ((14255, 14271), 'fabric.network.disconnect_all', 'disconnect_all', ([], {}), '()\n', (14269, 14271), False, 'from fabric.network import disconnect_all\n'), ((14280, 14297), 'igvm.libvirt.close_virtconns', 'close_virtconns', ([], {}), '()\n', (14295, 14297), False, 'from igvm.libvirt import close_virtconns\n'), ((14511, 14526), 'time.sleep', 'time.sleep', (['(0.1)'], {}), '(0.1)\n', (14521, 14526), False, 'import time\n'), ((15067, 15099), 'logging.root.getChild', 'root_logger.getChild', (['"""paramiko"""'], {}), "('paramiko')\n", (15087, 15099), True, 'from logging import StreamHandler, root as root_logger\n')]
puraminy/OpenPrompt
test/test_data_processor/test_condition_generation_dataset.py
49f0ed9719bb6285e94c746de4511991c848492c
import os, sys from os.path import dirname as d from os.path import abspath, join root_dir = d(d(d(abspath(__file__)))) sys.path.append(root_dir) from openprompt.data_utils.conditional_generation_dataset import PROCESSORS base_path = os.path.join(root_dir, "datasets/CondGen") def test_WebNLGProcessor(): dataset_name = "webnlg_2017" dataset_path = os.path.join(base_path, dataset_name) processor = PROCESSORS[dataset_name.lower()]() train_dataset = processor.get_train_examples(dataset_path) valid_dataset = processor.get_train_examples(dataset_path) test_dataset = processor.get_test_examples(dataset_path) assert len(train_dataset) == 18025 assert len(valid_dataset) == 18025 assert len(test_dataset) == 4928 assert test_dataset[0].text_a == " | Abilene_Regional_Airport : cityServed : Abilene,_Texas" assert test_dataset[0].text_b == "" assert test_dataset[0].tgt_text == "Abilene, Texas is served by the Abilene regional airport."
[((120, 145), 'sys.path.append', 'sys.path.append', (['root_dir'], {}), '(root_dir)\n', (135, 145), False, 'import os, sys\n'), ((235, 277), 'os.path.join', 'os.path.join', (['root_dir', '"""datasets/CondGen"""'], {}), "(root_dir, 'datasets/CondGen')\n", (247, 277), False, 'import os, sys\n'), ((359, 396), 'os.path.join', 'os.path.join', (['base_path', 'dataset_name'], {}), '(base_path, dataset_name)\n', (371, 396), False, 'import os, sys\n'), ((99, 116), 'os.path.abspath', 'abspath', (['__file__'], {}), '(__file__)\n', (106, 116), False, 'from os.path import abspath, join\n')]
JayLeeCompal/EDKII_Git
BaseTools/Source/Python/Common/BuildToolError.py
de4800d50e1f357002bf77235d3bebabd0c00007
## @file # Standardized Error Hanlding infrastructures. # # Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR> # This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at # http://opensource.org/licenses/bsd-license.php # # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. # FILE_OPEN_FAILURE = 1 FILE_WRITE_FAILURE = 2 FILE_PARSE_FAILURE = 3 FILE_READ_FAILURE = 4 FILE_CREATE_FAILURE = 5 FILE_CHECKSUM_FAILURE = 6 FILE_COMPRESS_FAILURE = 7 FILE_DECOMPRESS_FAILURE = 8 FILE_MOVE_FAILURE = 9 FILE_DELETE_FAILURE = 10 FILE_COPY_FAILURE = 11 FILE_POSITIONING_FAILURE = 12 FILE_ALREADY_EXIST = 13 FILE_NOT_FOUND = 14 FILE_TYPE_MISMATCH = 15 FILE_CASE_MISMATCH = 16 FILE_DUPLICATED = 17 FILE_UNKNOWN_ERROR = 0x0FFF OPTION_UNKNOWN = 0x1000 OPTION_MISSING = 0x1001 OPTION_CONFLICT = 0x1002 OPTION_VALUE_INVALID = 0x1003 OPTION_DEPRECATED = 0x1004 OPTION_NOT_SUPPORTED = 0x1005 OPTION_UNKNOWN_ERROR = 0x1FFF PARAMETER_INVALID = 0x2000 PARAMETER_MISSING = 0x2001 PARAMETER_UNKNOWN_ERROR =0x2FFF FORMAT_INVALID = 0x3000 FORMAT_NOT_SUPPORTED = 0x3001 FORMAT_UNKNOWN = 0x3002 FORMAT_UNKNOWN_ERROR = 0x3FFF RESOURCE_NOT_AVAILABLE = 0x4000 RESOURCE_ALLOCATE_FAILURE = 0x4001 RESOURCE_FULL = 0x4002 RESOURCE_OVERFLOW = 0x4003 RESOURCE_UNDERRUN = 0x4004 RESOURCE_UNKNOWN_ERROR = 0x4FFF ATTRIBUTE_NOT_AVAILABLE = 0x5000 ATTRIBUTE_GET_FAILURE = 0x5001 ATTRIBUTE_SET_FAILURE = 0x5002 ATTRIBUTE_UPDATE_FAILURE = 0x5003 ATTRIBUTE_ACCESS_DENIED = 0x5004 ATTRIBUTE_UNKNOWN_ERROR = 0x5FFF IO_NOT_READY = 0x6000 IO_BUSY = 0x6001 IO_TIMEOUT = 0x6002 IO_UNKNOWN_ERROR = 0x6FFF COMMAND_FAILURE = 0x7000 PERMISSION_FAILURE = 0x8000 CODE_ERROR = 0xC0DE AUTOGEN_ERROR = 0xF000 PARSER_ERROR = 0xF001 BUILD_ERROR = 0xF002 GENFDS_ERROR = 0xF003 ECC_ERROR = 0xF004 EOT_ERROR = 0xF005 DDC_ERROR = 0xF009 WARNING_AS_ERROR = 0xF006 MIGRATION_ERROR = 0xF010 PCD_VALIDATION_INFO_ERROR = 0xF011 PCD_VARIABLE_ATTRIBUTES_ERROR = 0xF012 PCD_VARIABLE_ATTRIBUTES_CONFLICT_ERROR = 0xF013 ABORT_ERROR = 0xFFFE UNKNOWN_ERROR = 0xFFFF ## Error message of each error code gErrorMessage = { FILE_NOT_FOUND : "File/directory not found in workspace", FILE_OPEN_FAILURE : "File open failure", FILE_WRITE_FAILURE : "File write failure", FILE_PARSE_FAILURE : "File parse failure", FILE_READ_FAILURE : "File read failure", FILE_CREATE_FAILURE : "File create failure", FILE_CHECKSUM_FAILURE : "Invalid checksum of file", FILE_COMPRESS_FAILURE : "File compress failure", FILE_DECOMPRESS_FAILURE : "File decompress failure", FILE_MOVE_FAILURE : "File move failure", FILE_DELETE_FAILURE : "File delete failure", FILE_COPY_FAILURE : "File copy failure", FILE_POSITIONING_FAILURE: "Failed to seeking position", FILE_ALREADY_EXIST : "File or directory already exists", FILE_TYPE_MISMATCH : "Incorrect file type", FILE_CASE_MISMATCH : "File name case mismatch", FILE_DUPLICATED : "Duplicated file found", FILE_UNKNOWN_ERROR : "Unknown error encountered on file", OPTION_UNKNOWN : "Unknown option", OPTION_MISSING : "Missing option", OPTION_CONFLICT : "Conflict options", OPTION_VALUE_INVALID : "Invalid value of option", OPTION_DEPRECATED : "Deprecated option", OPTION_NOT_SUPPORTED : "Unsupported option", OPTION_UNKNOWN_ERROR : "Unknown error when processing options", PARAMETER_INVALID : "Invalid parameter", PARAMETER_MISSING : "Missing parameter", PARAMETER_UNKNOWN_ERROR : "Unknown error in parameters", FORMAT_INVALID : "Invalid syntax/format", FORMAT_NOT_SUPPORTED : "Not supported syntax/format", FORMAT_UNKNOWN : "Unknown format", FORMAT_UNKNOWN_ERROR : "Unknown error in syntax/format ", RESOURCE_NOT_AVAILABLE : "Not available", RESOURCE_ALLOCATE_FAILURE : "Allocate failure", RESOURCE_FULL : "Full", RESOURCE_OVERFLOW : "Overflow", RESOURCE_UNDERRUN : "Underrun", RESOURCE_UNKNOWN_ERROR : "Unknown error", ATTRIBUTE_NOT_AVAILABLE : "Not available", ATTRIBUTE_GET_FAILURE : "Failed to retrieve", ATTRIBUTE_SET_FAILURE : "Failed to set", ATTRIBUTE_UPDATE_FAILURE: "Failed to update", ATTRIBUTE_ACCESS_DENIED : "Access denied", ATTRIBUTE_UNKNOWN_ERROR : "Unknown error when accessing", COMMAND_FAILURE : "Failed to execute command", IO_NOT_READY : "Not ready", IO_BUSY : "Busy", IO_TIMEOUT : "Timeout", IO_UNKNOWN_ERROR : "Unknown error in IO operation", UNKNOWN_ERROR : "Unknown error", } ## Exception indicating a fatal error class FatalError(Exception): pass if __name__ == "__main__": pass
[]
allenai/ViRB
datasets/SUN397EncodbleDataset.py
fbe1c42571ce0994b1e41bc4bdf88cf9658ae48b
import torch import torchvision.transforms as transforms from torch.utils.data import Dataset import glob from PIL import Image import random class SUN397EncodableDataset(Dataset): """SUN397 encodable dataset class""" def __init__(self, train=True): super().__init__() path = 'data/SUN397/train/*/*.jpg' if train else 'data/SUN397/test/*/*.jpg' self.data = list(glob.glob(path)) random.shuffle(self.data) cats = list(set([path.split("/")[3] for path in self.data])) cats.sort() self.labels = torch.LongTensor([cats.index(path.split("/")[3]) for path in self.data]) self.preprocessor = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() if len(self.encoded_data) == 0: return self.preprocessor(Image.open(self.data[idx]).convert('RGB')), self.labels[idx] return self.encoded_data[idx], self.labels[idx] def __len__(self): return len(self.labels) def num_classes(self): return int(max(self.labels) + 1)
[((422, 447), 'random.shuffle', 'random.shuffle', (['self.data'], {}), '(self.data)\n', (436, 447), False, 'import random\n'), ((987, 1007), 'torch.is_tensor', 'torch.is_tensor', (['idx'], {}), '(idx)\n', (1002, 1007), False, 'import torch\n'), ((397, 412), 'glob.glob', 'glob.glob', (['path'], {}), '(path)\n', (406, 412), False, 'import glob\n'), ((693, 722), 'torchvision.transforms.Resize', 'transforms.Resize', (['(224, 224)'], {}), '((224, 224))\n', (710, 722), True, 'import torchvision.transforms as transforms\n'), ((736, 757), 'torchvision.transforms.ToTensor', 'transforms.ToTensor', ([], {}), '()\n', (755, 757), True, 'import torchvision.transforms as transforms\n'), ((771, 846), 'torchvision.transforms.Normalize', 'transforms.Normalize', ([], {'mean': '[0.485, 0.456, 0.406]', 'std': '[0.229, 0.224, 0.225]'}), '(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n', (791, 846), True, 'import torchvision.transforms as transforms\n'), ((905, 930), 'torch.cuda.is_available', 'torch.cuda.is_available', ([], {}), '()\n', (928, 930), False, 'import torch\n'), ((1118, 1144), 'PIL.Image.open', 'Image.open', (['self.data[idx]'], {}), '(self.data[idx])\n', (1128, 1144), False, 'from PIL import Image\n')]
tirkarthi/python-cybox
cybox/common/location.py
a378deb68b3ac56360c5cc35ff5aad1cd3dcab83
# Copyright (c) 2017, The MITRE Corporation. All rights reserved. # See LICENSE.txt for complete terms. from mixbox import entities, fields import cybox import cybox.bindings.cybox_common as common_binding class LocationFactory(entities.EntityFactory): @classmethod def entity_class(cls, key): return cybox.lookup_extension(key, default=Location) class Location(entities.Entity): _binding = common_binding _binding_class = common_binding.LocationType _namespace = 'http://cybox.mitre.org/common-2' _XSI_TYPE = None # overridden by subclasses id_ = fields.IdrefField("id") idref = fields.IdrefField("idref") name = fields.TypedField("Name") def to_dict(self): d = super(Location, self).to_dict() if self._XSI_TYPE: d["xsi:type"] = self._XSI_TYPE return d @staticmethod def lookup_class(xsi_type): return cybox.lookup_extension(xsi_type, default=Location)
[((592, 615), 'mixbox.fields.IdrefField', 'fields.IdrefField', (['"""id"""'], {}), "('id')\n", (609, 615), False, 'from mixbox import entities, fields\n'), ((628, 654), 'mixbox.fields.IdrefField', 'fields.IdrefField', (['"""idref"""'], {}), "('idref')\n", (645, 654), False, 'from mixbox import entities, fields\n'), ((666, 691), 'mixbox.fields.TypedField', 'fields.TypedField', (['"""Name"""'], {}), "('Name')\n", (683, 691), False, 'from mixbox import entities, fields\n'), ((321, 366), 'cybox.lookup_extension', 'cybox.lookup_extension', (['key'], {'default': 'Location'}), '(key, default=Location)\n', (343, 366), False, 'import cybox\n'), ((915, 965), 'cybox.lookup_extension', 'cybox.lookup_extension', (['xsi_type'], {'default': 'Location'}), '(xsi_type, default=Location)\n', (937, 965), False, 'import cybox\n')]
varlociraptor/prosic-evaluation
scripts/bam-stats.py
f4f1950ba5c10bda0f41df2a8f519d98f779d736
#!/usr/bin/env python import sys import numpy as np import pandas as pd import pysam import matplotlib matplotlib.use("agg") import matplotlib.pyplot as plt import seaborn as sns from functools import partial tumor = pysam.AlignmentFile(snakemake.input[0], "rb") normal = pysam.AlignmentFile(snakemake.input[1], "rb") softclips = [] for i, rec in enumerate(normal): if rec.is_supplementary or rec.is_unmapped: continue is_first_read = rec.pos < rec.mpos get_clip = lambda c: c[1] if c[0] == 4 else None clip_left = get_clip(rec.cigartuples[0]) if clip_left is not None: softclips.append([clip_left, True, is_first_read]) clip_right = get_clip(rec.cigartuples[-1]) if clip_right is not None: softclips.append([clip_right, False, is_first_read]) if i == 10000000: break softclips = pd.DataFrame(softclips, columns=["len", "left", "first_in_pair"]) def plot(*args, **kwargs): softclips = args[0] plt.hist(softclips, normed=True) q95 = np.percentile(softclips, 99) plt.plot([q95, q95], [0, 1.0], "--k") m = max(softclips) plt.plot([m, m], [0, 1.0], ":k") plt.text(m, 1, "max={}".format(m), horizontalalignment="right", verticalalignment="top") g = sns.FacetGrid(softclips, col="left", row="first_in_pair") g = g.map(plot, "len") plt.savefig(snakemake.output[0])
[((104, 125), 'matplotlib.use', 'matplotlib.use', (['"""agg"""'], {}), "('agg')\n", (118, 125), False, 'import matplotlib\n'), ((219, 264), 'pysam.AlignmentFile', 'pysam.AlignmentFile', (['snakemake.input[0]', '"""rb"""'], {}), "(snakemake.input[0], 'rb')\n", (238, 264), False, 'import pysam\n'), ((274, 319), 'pysam.AlignmentFile', 'pysam.AlignmentFile', (['snakemake.input[1]', '"""rb"""'], {}), "(snakemake.input[1], 'rb')\n", (293, 319), False, 'import pysam\n'), ((849, 914), 'pandas.DataFrame', 'pd.DataFrame', (['softclips'], {'columns': "['len', 'left', 'first_in_pair']"}), "(softclips, columns=['len', 'left', 'first_in_pair'])\n", (861, 914), True, 'import pandas as pd\n'), ((1244, 1301), 'seaborn.FacetGrid', 'sns.FacetGrid', (['softclips'], {'col': '"""left"""', 'row': '"""first_in_pair"""'}), "(softclips, col='left', row='first_in_pair')\n", (1257, 1301), True, 'import seaborn as sns\n'), ((1326, 1358), 'matplotlib.pyplot.savefig', 'plt.savefig', (['snakemake.output[0]'], {}), '(snakemake.output[0])\n', (1337, 1358), True, 'import matplotlib.pyplot as plt\n'), ((971, 1003), 'matplotlib.pyplot.hist', 'plt.hist', (['softclips'], {'normed': '(True)'}), '(softclips, normed=True)\n', (979, 1003), True, 'import matplotlib.pyplot as plt\n'), ((1014, 1042), 'numpy.percentile', 'np.percentile', (['softclips', '(99)'], {}), '(softclips, 99)\n', (1027, 1042), True, 'import numpy as np\n'), ((1047, 1084), 'matplotlib.pyplot.plot', 'plt.plot', (['[q95, q95]', '[0, 1.0]', '"""--k"""'], {}), "([q95, q95], [0, 1.0], '--k')\n", (1055, 1084), True, 'import matplotlib.pyplot as plt\n'), ((1112, 1144), 'matplotlib.pyplot.plot', 'plt.plot', (['[m, m]', '[0, 1.0]', '""":k"""'], {}), "([m, m], [0, 1.0], ':k')\n", (1120, 1144), True, 'import matplotlib.pyplot as plt\n')]
RSaab/rss-scraper
app/rss_feeder_api/migrations/0003_auto_20200813_1623.py
9bf608878e7d08fea6508ae90b27f1c226b313f1
# Generated by Django 3.1 on 2020-08-13 16:23 from django.db import migrations, models import django.utils.timezone class Migration(migrations.Migration): dependencies = [ ('rss_feeder_api', '0002_feed_subtitle'), ] operations = [ migrations.AlterModelOptions( name='entry', options={'ordering': ('-updated_at',), 'verbose_name_plural': 'entries'}, ), migrations.AlterModelOptions( name='feed', options={'ordering': ('-updated_at',), 'verbose_name': 'Feed', 'verbose_name_plural': 'Feeds'}, ), migrations.AddField( model_name='entry', name='created_at', field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), preserve_default=False, ), migrations.AddField( model_name='entry', name='updated_at', field=models.DateTimeField(auto_now=True), ), migrations.AlterUniqueTogether( name='entry', unique_together={('guid',)}, ), ]
[((264, 385), 'django.db.migrations.AlterModelOptions', 'migrations.AlterModelOptions', ([], {'name': '"""entry"""', 'options': "{'ordering': ('-updated_at',), 'verbose_name_plural': 'entries'}"}), "(name='entry', options={'ordering': (\n '-updated_at',), 'verbose_name_plural': 'entries'})\n", (292, 385), False, 'from django.db import migrations, models\n'), ((425, 567), 'django.db.migrations.AlterModelOptions', 'migrations.AlterModelOptions', ([], {'name': '"""feed"""', 'options': "{'ordering': ('-updated_at',), 'verbose_name': 'Feed',\n 'verbose_name_plural': 'Feeds'}"}), "(name='feed', options={'ordering': (\n '-updated_at',), 'verbose_name': 'Feed', 'verbose_name_plural': 'Feeds'})\n", (453, 567), False, 'from django.db import migrations, models\n'), ((998, 1071), 'django.db.migrations.AlterUniqueTogether', 'migrations.AlterUniqueTogether', ([], {'name': '"""entry"""', 'unique_together': "{('guid',)}"}), "(name='entry', unique_together={('guid',)})\n", (1028, 1071), False, 'from django.db import migrations, models\n'), ((709, 783), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(True)', 'default': 'django.utils.timezone.now'}), '(auto_now_add=True, default=django.utils.timezone.now)\n', (729, 783), False, 'from django.db import migrations, models\n'), ((942, 977), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now': '(True)'}), '(auto_now=True)\n', (962, 977), False, 'from django.db import migrations, models\n')]
MetaMain/BewareAdvML
AdversarialSampleGeneratorV11/AdversarialSampleGeneratorV11/ResNetConstructor.py
52d489b565b0df36cb588b5709c29c2e8e4d3f49
import tensorflow from tensorflow import keras Model = keras.models.Model Dense = keras.layers.Dense Activation = keras.layers.Activation Flatten = keras.layers.Flatten BatchNormalization= keras.layers.BatchNormalization Conv2D = tensorflow.keras.layers.Conv2D AveragePooling2D = keras.layers.AveragePooling2D Input=keras.layers.Input l2=keras.regularizers.l2 from tensorflow.keras import backend def resnet_layer(inputs, num_filters=16, kernel_size=3, strides=1, activation='relu', batch_normalization=True, conv_first=True): """2D Convolution-Batch Normalization-Activation stack builder # Arguments inputs (tensor): input tensor from input image or previous layer num_filters (int): Conv2D number of filters kernel_size (int): Conv2D square kernel dimensions strides (int): Conv2D square stride dimensions activation (string): activation name batch_normalization (bool): whether to include batch normalization conv_first (bool): conv-bn-activation (True) or bn-activation-conv (False) # Returns x (tensor): tensor as input to the next layer """ conv = Conv2D( num_filters, kernel_size=kernel_size, strides=strides, padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(1e-4)) x = inputs if conv_first: x = conv(x) if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = Activation(activation)(x) else: if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = Activation(activation)(x) x = conv(x) return x def resnet_v2(input, complexityParameter, num_classes=10, dataset='cifar10'): depth = complexityParameter * 9 + 2 if (depth - 2) % 9 != 0: raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])') # Start model definition. num_filters_in = 16 num_res_blocks = int((depth - 2) / 9) inputs = input x = resnet_layer(inputs=inputs, num_filters=num_filters_in, conv_first=True) # Instantiate the stack of residual units for stage in range(3): for res_block in range(num_res_blocks): activation = 'relu' batch_normalization = True strides = 1 if stage == 0: num_filters_out = num_filters_in * 4 if res_block == 0: # first layer and first stage activation = None batch_normalization = False else: num_filters_out = num_filters_in * 2 if res_block == 0: # first layer but not first stage strides = 2 # downsample # bottleneck residual unit y = resnet_layer(inputs=x, num_filters=num_filters_in, kernel_size=1, strides=strides, activation=activation, batch_normalization=batch_normalization, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_in, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_out, kernel_size=1, conv_first=False) if res_block == 0: # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters_out, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = tensorflow.keras.layers.add([x, y]) num_filters_in = num_filters_out # Add classifier on top. # v2 has BN-ReLU before Pooling x = BatchNormalization()(x) x = Activation('relu')(x) x = AveragePooling2D(pool_size=8)(x) final_features = Flatten()(x) logits = Dense(num_classes, kernel_initializer='he_normal')(final_features) outputs = Activation('softmax')(logits) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model, inputs, outputs, logits, final_features
[((4292, 4327), 'tensorflow.keras.layers.add', 'tensorflow.keras.layers.add', (['[x, y]'], {}), '([x, y])\n', (4319, 4327), False, 'import tensorflow\n')]
the-norman-sicily-project/genealogical-trees
ttl2json.py
32fa4f25861ae34543b0a6b95e54842c0018331b
#!/usr/bin/env python3 import sys import json import rdflib import rdflib.plugins.sparql as sparql RELS_TO_DRAW = ['isWifeOf', 'isMotherOf', 'isFatherOf', 'isHusbandOf', 'isSpouseOf'] RELS_TO_INFER = ['hasGrandParent', 'isGrandParentOf', 'hasGreatGrandParent', 'isGreatGrandParentOf', 'isUncleOf', 'hasUncle', 'isGreatUncleOf', 'hasGreatUncle', 'isAuntOf', 'hasAunt', 'isGreatAuntOf', 'hasGreatAunt', 'isBrotherOf', 'isSisterOf', 'isSiblingOf', 'isFirstCousinOf', 'isSecondCousinOf', 'isThirdCousinOf'] RELS_OF_INTEREST = RELS_TO_DRAW + RELS_TO_INFER try: workpath = sys.argv[1] except IndexError: sys.exit("No path defined!") try: recursion_limit = int(sys.argv[2]) except IndexError: recursion_limit = 0 if recursion_limit > 0: sys.setrecursionlimit(recursion_limit) g = rdflib.Graph() g.parse(workpath, format="turtle") fhkb_str = "http://www.example.com/genealogy.owl#" schema_str = "https://schema.org/" FHKB = rdflib.Namespace(fhkb_str) SCHEMA_ORG = rdflib.Namespace(schema_str) def dump(uriref): if uriref.__contains__('#'): return uriref.split('#')[-1] return uriref.split('/')[-1] graph = {} graph['nodes'] = [] graph['edges'] = [] nodes = {} q = sparql.prepareQuery( """PREFIX fhkb:<http://www.example.com/genealogy.owl#> SELECT ?person ?pred ?obj WHERE { ?person a fhkb:Person ; ?pred ?obj . } ORDER BY ?person""") for rel in RELS_OF_INTEREST: pred = rdflib.URIRef("{}{}".format(fhkb_str, rel)) relation_query_results = g.query(q, initBindings={'pred': pred}) for (subj, pred, obj) in relation_query_results: graph['edges'].append( { 'data': { 'group': 'edges', 'id': f'{dump(subj)}-{dump(pred)}-{dump(obj)}', 'source': dump(subj), 'target': dump(obj), 'type': dump(pred) } }) q_details = sparql.prepareQuery( """PREFIX fhkb:<http://www.example.com/genealogy.owl#> SELECT ?person ?pred ?obj WHERE { ?person a fhkb:Person ; ?pred ?obj . FILTER NOT EXISTS { ?person ?testPred ?obj . VALUES ?testPred { fhkb:isWifeOf fhkb:isMotherOf fhkb:isFatherOf fhkb:isHusbandOf fhkb:isSpouseOf fhkb:hasGrandParent fhkb:isGrandParentOf fhkb:hasGreatGrandParent fhkb:isGreatGrandParentOf fhkb:isUncleOf fhkb:hasUncle fhkb:isGreatUncleOf fhkb:hasGreatUncle fhkb:isAuntOf fhkb:hasAunt fhkb:isGreatAuntOf fhkb:hasGreatAunt fhkb:isBrotherOf fhkb:isSisterOf fhkb:isSiblingOf fhkb:isFirstCousinOf fhkb:isSecondCousinOf fhkb:isThirdCousinOf fhkb:hasRelation fhkb:isPartnerIn fhkb:isMalePartnerIn fhkb:isFemalePartnerIn fhkb:isBloodrelationOf } } } ORDER BY ?person""" ) person_query_results = g.query(q_details) for (subj, pred, obj) in person_query_results: node = nodes.get(dump(subj), { 'data': { 'label': '', 'degree': 0, 'size': 10, 'alternateNames': [], 'honorificPrefixes': [], 'honorificSuffixes': [], 'images': [], 'id': dump(subj), }}) if pred == FHKB.Sex: node['data'][dump(pred)] = dump(obj) elif pred.startswith(SCHEMA_ORG): if dump(pred) == 'honorificSuffix': node['data']['honorificSuffixes'].append(obj) elif dump(pred) == 'honorificPrefix': node['data']['honorificPrefixes'].append(obj) elif dump(pred) == 'alternateName': node['data']['alternateNames'].append(obj) elif dump(pred) == 'image': node['data']['images'].append(obj) else: node['data'][dump(pred)] = obj elif pred == rdflib.RDFS.label: node['data']['label'] = obj else: continue nodes[dump(subj)] = node graph['nodes'] = list(nodes.values()) print(json.dumps(graph, indent=0)) sys.exit(0)
[((884, 898), 'rdflib.Graph', 'rdflib.Graph', ([], {}), '()\n', (896, 898), False, 'import rdflib\n'), ((1029, 1055), 'rdflib.Namespace', 'rdflib.Namespace', (['fhkb_str'], {}), '(fhkb_str)\n', (1045, 1055), False, 'import rdflib\n'), ((1069, 1097), 'rdflib.Namespace', 'rdflib.Namespace', (['schema_str'], {}), '(schema_str)\n', (1085, 1097), False, 'import rdflib\n'), ((1289, 1499), 'rdflib.plugins.sparql.prepareQuery', 'sparql.prepareQuery', (['"""PREFIX fhkb:<http://www.example.com/genealogy.owl#> \n SELECT ?person ?pred ?obj\n WHERE { \n ?person a fhkb:Person ; \n ?pred ?obj .\n } \n ORDER BY ?person"""'], {}), '(\n """PREFIX fhkb:<http://www.example.com/genealogy.owl#> \n SELECT ?person ?pred ?obj\n WHERE { \n ?person a fhkb:Person ; \n ?pred ?obj .\n } \n ORDER BY ?person"""\n )\n', (1308, 1499), True, 'import rdflib.plugins.sparql as sparql\n'), ((2043, 3171), 'rdflib.plugins.sparql.prepareQuery', 'sparql.prepareQuery', (['"""PREFIX fhkb:<http://www.example.com/genealogy.owl#>\n SELECT ?person ?pred ?obj\n WHERE { \n ?person a fhkb:Person ; \n ?pred ?obj . \n FILTER NOT EXISTS {\n ?person ?testPred ?obj .\n VALUES ?testPred {\n fhkb:isWifeOf\n fhkb:isMotherOf\n fhkb:isFatherOf\n fhkb:isHusbandOf\n fhkb:isSpouseOf\n fhkb:hasGrandParent\n fhkb:isGrandParentOf\n fhkb:hasGreatGrandParent\n fhkb:isGreatGrandParentOf\n fhkb:isUncleOf\n fhkb:hasUncle\n fhkb:isGreatUncleOf\n fhkb:hasGreatUncle\n fhkb:isAuntOf\n fhkb:hasAunt\n fhkb:isGreatAuntOf\n fhkb:hasGreatAunt\n fhkb:isBrotherOf\n fhkb:isSisterOf\n fhkb:isSiblingOf\n fhkb:isFirstCousinOf\n fhkb:isSecondCousinOf\n fhkb:isThirdCousinOf\n\n fhkb:hasRelation\n fhkb:isPartnerIn\n fhkb:isMalePartnerIn\n fhkb:isFemalePartnerIn\n fhkb:isBloodrelationOf\n }\n }\n} \nORDER BY ?person"""'], {}), '(\n """PREFIX fhkb:<http://www.example.com/genealogy.owl#>\n SELECT ?person ?pred ?obj\n WHERE { \n ?person a fhkb:Person ; \n ?pred ?obj . \n FILTER NOT EXISTS {\n ?person ?testPred ?obj .\n VALUES ?testPred {\n fhkb:isWifeOf\n fhkb:isMotherOf\n fhkb:isFatherOf\n fhkb:isHusbandOf\n fhkb:isSpouseOf\n fhkb:hasGrandParent\n fhkb:isGrandParentOf\n fhkb:hasGreatGrandParent\n fhkb:isGreatGrandParentOf\n fhkb:isUncleOf\n fhkb:hasUncle\n fhkb:isGreatUncleOf\n fhkb:hasGreatUncle\n fhkb:isAuntOf\n fhkb:hasAunt\n fhkb:isGreatAuntOf\n fhkb:hasGreatAunt\n fhkb:isBrotherOf\n fhkb:isSisterOf\n fhkb:isSiblingOf\n fhkb:isFirstCousinOf\n fhkb:isSecondCousinOf\n fhkb:isThirdCousinOf\n\n fhkb:hasRelation\n fhkb:isPartnerIn\n fhkb:isMalePartnerIn\n fhkb:isFemalePartnerIn\n fhkb:isBloodrelationOf\n }\n }\n} \nORDER BY ?person"""\n )\n', (2062, 3171), True, 'import rdflib.plugins.sparql as sparql\n'), ((4409, 4420), 'sys.exit', 'sys.exit', (['(0)'], {}), '(0)\n', (4417, 4420), False, 'import sys\n'), ((840, 878), 'sys.setrecursionlimit', 'sys.setrecursionlimit', (['recursion_limit'], {}), '(recursion_limit)\n', (861, 878), False, 'import sys\n'), ((4380, 4407), 'json.dumps', 'json.dumps', (['graph'], {'indent': '(0)'}), '(graph, indent=0)\n', (4390, 4407), False, 'import json\n'), ((695, 723), 'sys.exit', 'sys.exit', (['"""No path defined!"""'], {}), "('No path defined!')\n", (703, 723), False, 'import sys\n')]
BearerPipelineTest/synapse-1
tests/rest/client/test_login.py
78b99de7c206b106340e12cdee0af9aa246bd5ad
# Copyright 2019-2021 The Matrix.org Foundation C.I.C. # # 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. import json import time import urllib.parse from typing import Any, Dict, List, Optional, Union from unittest.mock import Mock from urllib.parse import urlencode import pymacaroons from twisted.test.proto_helpers import MemoryReactor from twisted.web.resource import Resource import synapse.rest.admin from synapse.appservice import ApplicationService from synapse.rest.client import devices, login, logout, register from synapse.rest.client.account import WhoamiRestServlet from synapse.rest.synapse.client import build_synapse_client_resource_tree from synapse.server import HomeServer from synapse.types import create_requester from synapse.util import Clock from tests import unittest from tests.handlers.test_oidc import HAS_OIDC from tests.handlers.test_saml import has_saml2 from tests.rest.client.utils import TEST_OIDC_AUTH_ENDPOINT, TEST_OIDC_CONFIG from tests.server import FakeChannel from tests.test_utils.html_parsers import TestHtmlParser from tests.unittest import HomeserverTestCase, override_config, skip_unless try: import jwt HAS_JWT = True except ImportError: HAS_JWT = False # synapse server name: used to populate public_baseurl in some tests SYNAPSE_SERVER_PUBLIC_HOSTNAME = "synapse" # public_baseurl for some tests. It uses an http:// scheme because # FakeChannel.isSecure() returns False, so synapse will see the requested uri as # http://..., so using http in the public_baseurl stops Synapse trying to redirect to # https://.... BASE_URL = "http://%s/" % (SYNAPSE_SERVER_PUBLIC_HOSTNAME,) # CAS server used in some tests CAS_SERVER = "https://fake.test" # just enough to tell pysaml2 where to redirect to SAML_SERVER = "https://test.saml.server/idp/sso" TEST_SAML_METADATA = """ <md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"> <md:IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"> <md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="%(SAML_SERVER)s"/> </md:IDPSSODescriptor> </md:EntityDescriptor> """ % { "SAML_SERVER": SAML_SERVER, } LOGIN_URL = b"/_matrix/client/r0/login" TEST_URL = b"/_matrix/client/r0/account/whoami" # a (valid) url with some annoying characters in. %3D is =, %26 is &, %2B is + TEST_CLIENT_REDIRECT_URL = 'https://x?<ab c>&q"+%3D%2B"="fö%26=o"' # the query params in TEST_CLIENT_REDIRECT_URL EXPECTED_CLIENT_REDIRECT_URL_PARAMS = [("<ab c>", ""), ('q" =+"', '"fö&=o"')] # (possibly experimental) login flows we expect to appear in the list after the normal # ones ADDITIONAL_LOGIN_FLOWS = [ {"type": "m.login.application_service"}, {"type": "uk.half-shot.msc2778.login.application_service"}, ] class LoginRestServletTestCase(unittest.HomeserverTestCase): servlets = [ synapse.rest.admin.register_servlets_for_client_rest_resource, login.register_servlets, logout.register_servlets, devices.register_servlets, lambda hs, http_server: WhoamiRestServlet(hs).register(http_server), ] def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: self.hs = self.setup_test_homeserver() self.hs.config.registration.enable_registration = True self.hs.config.registration.registrations_require_3pid = [] self.hs.config.registration.auto_join_rooms = [] self.hs.config.captcha.enable_registration_captcha = False return self.hs @override_config( { "rc_login": { "address": {"per_second": 0.17, "burst_count": 5}, # Prevent the account login ratelimiter from raising first # # This is normally covered by the default test homeserver config # which sets these values to 10000, but as we're overriding the entire # rc_login dict here, we need to set this manually as well "account": {"per_second": 10000, "burst_count": 10000}, } } ) def test_POST_ratelimiting_per_address(self) -> None: # Create different users so we're sure not to be bothered by the per-user # ratelimiter. for i in range(0, 6): self.register_user("kermit" + str(i), "monkey") for i in range(0, 6): params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit" + str(i)}, "password": "monkey", } channel = self.make_request(b"POST", LOGIN_URL, params) if i == 5: self.assertEqual(channel.result["code"], b"429", channel.result) retry_after_ms = int(channel.json_body["retry_after_ms"]) else: self.assertEqual(channel.result["code"], b"200", channel.result) # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower # than 1min. self.assertTrue(retry_after_ms < 6000) self.reactor.advance(retry_after_ms / 1000.0 + 1.0) params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit" + str(i)}, "password": "monkey", } channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.result["code"], b"200", channel.result) @override_config( { "rc_login": { "account": {"per_second": 0.17, "burst_count": 5}, # Prevent the address login ratelimiter from raising first # # This is normally covered by the default test homeserver config # which sets these values to 10000, but as we're overriding the entire # rc_login dict here, we need to set this manually as well "address": {"per_second": 10000, "burst_count": 10000}, } } ) def test_POST_ratelimiting_per_account(self) -> None: self.register_user("kermit", "monkey") for i in range(0, 6): params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit"}, "password": "monkey", } channel = self.make_request(b"POST", LOGIN_URL, params) if i == 5: self.assertEqual(channel.result["code"], b"429", channel.result) retry_after_ms = int(channel.json_body["retry_after_ms"]) else: self.assertEqual(channel.result["code"], b"200", channel.result) # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower # than 1min. self.assertTrue(retry_after_ms < 6000) self.reactor.advance(retry_after_ms / 1000.0) params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit"}, "password": "monkey", } channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.result["code"], b"200", channel.result) @override_config( { "rc_login": { # Prevent the address login ratelimiter from raising first # # This is normally covered by the default test homeserver config # which sets these values to 10000, but as we're overriding the entire # rc_login dict here, we need to set this manually as well "address": {"per_second": 10000, "burst_count": 10000}, "failed_attempts": {"per_second": 0.17, "burst_count": 5}, } } ) def test_POST_ratelimiting_per_account_failed_attempts(self) -> None: self.register_user("kermit", "monkey") for i in range(0, 6): params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit"}, "password": "notamonkey", } channel = self.make_request(b"POST", LOGIN_URL, params) if i == 5: self.assertEqual(channel.result["code"], b"429", channel.result) retry_after_ms = int(channel.json_body["retry_after_ms"]) else: self.assertEqual(channel.result["code"], b"403", channel.result) # Since we're ratelimiting at 1 request/min, retry_after_ms should be lower # than 1min. self.assertTrue(retry_after_ms < 6000) self.reactor.advance(retry_after_ms / 1000.0 + 1.0) params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit"}, "password": "notamonkey", } channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.result["code"], b"403", channel.result) @override_config({"session_lifetime": "24h"}) def test_soft_logout(self) -> None: self.register_user("kermit", "monkey") # we shouldn't be able to make requests without an access token channel = self.make_request(b"GET", TEST_URL) self.assertEqual(channel.result["code"], b"401", channel.result) self.assertEqual(channel.json_body["errcode"], "M_MISSING_TOKEN") # log in as normal params = { "type": "m.login.password", "identifier": {"type": "m.id.user", "user": "kermit"}, "password": "monkey", } channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.code, 200, channel.result) access_token = channel.json_body["access_token"] device_id = channel.json_body["device_id"] # we should now be able to make requests with the access token channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 200, channel.result) # time passes self.reactor.advance(24 * 3600) # ... and we should be soft-logouted channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 401, channel.result) self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN") self.assertEqual(channel.json_body["soft_logout"], True) # # test behaviour after deleting the expired device # # we now log in as a different device access_token_2 = self.login("kermit", "monkey") # more requests with the expired token should still return a soft-logout self.reactor.advance(3600) channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 401, channel.result) self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN") self.assertEqual(channel.json_body["soft_logout"], True) # ... but if we delete that device, it will be a proper logout self._delete_device(access_token_2, "kermit", "monkey", device_id) channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 401, channel.result) self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN") self.assertEqual(channel.json_body["soft_logout"], False) def _delete_device( self, access_token: str, user_id: str, password: str, device_id: str ) -> None: """Perform the UI-Auth to delete a device""" channel = self.make_request( b"DELETE", "devices/" + device_id, access_token=access_token ) self.assertEqual(channel.code, 401, channel.result) # check it's a UI-Auth fail self.assertEqual( set(channel.json_body.keys()), {"flows", "params", "session"}, channel.result, ) auth = { "type": "m.login.password", # https://github.com/matrix-org/synapse/issues/5665 # "identifier": {"type": "m.id.user", "user": user_id}, "user": user_id, "password": password, "session": channel.json_body["session"], } channel = self.make_request( b"DELETE", "devices/" + device_id, access_token=access_token, content={"auth": auth}, ) self.assertEqual(channel.code, 200, channel.result) @override_config({"session_lifetime": "24h"}) def test_session_can_hard_logout_after_being_soft_logged_out(self) -> None: self.register_user("kermit", "monkey") # log in as normal access_token = self.login("kermit", "monkey") # we should now be able to make requests with the access token channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 200, channel.result) # time passes self.reactor.advance(24 * 3600) # ... and we should be soft-logouted channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 401, channel.result) self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN") self.assertEqual(channel.json_body["soft_logout"], True) # Now try to hard logout this session channel = self.make_request(b"POST", "/logout", access_token=access_token) self.assertEqual(channel.result["code"], b"200", channel.result) @override_config({"session_lifetime": "24h"}) def test_session_can_hard_logout_all_sessions_after_being_soft_logged_out( self, ) -> None: self.register_user("kermit", "monkey") # log in as normal access_token = self.login("kermit", "monkey") # we should now be able to make requests with the access token channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 200, channel.result) # time passes self.reactor.advance(24 * 3600) # ... and we should be soft-logouted channel = self.make_request(b"GET", TEST_URL, access_token=access_token) self.assertEqual(channel.code, 401, channel.result) self.assertEqual(channel.json_body["errcode"], "M_UNKNOWN_TOKEN") self.assertEqual(channel.json_body["soft_logout"], True) # Now try to hard log out all of the user's sessions channel = self.make_request(b"POST", "/logout/all", access_token=access_token) self.assertEqual(channel.result["code"], b"200", channel.result) def test_login_with_overly_long_device_id_fails(self) -> None: self.register_user("mickey", "cheese") # create a device_id longer than 512 characters device_id = "yolo" * 512 body = { "type": "m.login.password", "user": "mickey", "password": "cheese", "device_id": device_id, } # make a login request with the bad device_id channel = self.make_request( "POST", "/_matrix/client/v3/login", json.dumps(body).encode("utf8"), custom_headers=None, ) # test that the login fails with the correct error code self.assertEqual(channel.code, 400) self.assertEqual(channel.json_body["errcode"], "M_INVALID_PARAM") @skip_unless(has_saml2 and HAS_OIDC, "Requires SAML2 and OIDC") class MultiSSOTestCase(unittest.HomeserverTestCase): """Tests for homeservers with multiple SSO providers enabled""" servlets = [ login.register_servlets, ] def default_config(self) -> Dict[str, Any]: config = super().default_config() config["public_baseurl"] = BASE_URL config["cas_config"] = { "enabled": True, "server_url": CAS_SERVER, "service_url": "https://matrix.goodserver.com:8448", } config["saml2_config"] = { "sp_config": { "metadata": {"inline": [TEST_SAML_METADATA]}, # use the XMLSecurity backend to avoid relying on xmlsec1 "crypto_backend": "XMLSecurity", }, } # default OIDC provider config["oidc_config"] = TEST_OIDC_CONFIG # additional OIDC providers config["oidc_providers"] = [ { "idp_id": "idp1", "idp_name": "IDP1", "discover": False, "issuer": "https://issuer1", "client_id": "test-client-id", "client_secret": "test-client-secret", "scopes": ["profile"], "authorization_endpoint": "https://issuer1/auth", "token_endpoint": "https://issuer1/token", "userinfo_endpoint": "https://issuer1/userinfo", "user_mapping_provider": { "config": {"localpart_template": "{{ user.sub }}"} }, } ] return config def create_resource_dict(self) -> Dict[str, Resource]: d = super().create_resource_dict() d.update(build_synapse_client_resource_tree(self.hs)) return d def test_get_login_flows(self) -> None: """GET /login should return password and SSO flows""" channel = self.make_request("GET", "/_matrix/client/r0/login") self.assertEqual(channel.code, 200, channel.result) expected_flow_types = [ "m.login.cas", "m.login.sso", "m.login.token", "m.login.password", ] + [f["type"] for f in ADDITIONAL_LOGIN_FLOWS] self.assertCountEqual( [f["type"] for f in channel.json_body["flows"]], expected_flow_types ) flows = {flow["type"]: flow for flow in channel.json_body["flows"]} self.assertCountEqual( flows["m.login.sso"]["identity_providers"], [ {"id": "cas", "name": "CAS"}, {"id": "saml", "name": "SAML"}, {"id": "oidc-idp1", "name": "IDP1"}, {"id": "oidc", "name": "OIDC"}, ], ) def test_multi_sso_redirect(self) -> None: """/login/sso/redirect should redirect to an identity picker""" # first hit the redirect url, which should redirect to our idp picker channel = self._make_sso_redirect_request(None) self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers uri = location_headers[0] # hitting that picker should give us some HTML channel = self.make_request("GET", uri) self.assertEqual(channel.code, 200, channel.result) # parse the form to check it has fields assumed elsewhere in this class html = channel.result["body"].decode("utf-8") p = TestHtmlParser() p.feed(html) p.close() # there should be a link for each href returned_idps: List[str] = [] for link in p.links: path, query = link.split("?", 1) self.assertEqual(path, "pick_idp") params = urllib.parse.parse_qs(query) self.assertEqual(params["redirectUrl"], [TEST_CLIENT_REDIRECT_URL]) returned_idps.append(params["idp"][0]) self.assertCountEqual(returned_idps, ["cas", "oidc", "oidc-idp1", "saml"]) def test_multi_sso_redirect_to_cas(self) -> None: """If CAS is chosen, should redirect to the CAS server""" channel = self.make_request( "GET", "/_synapse/client/pick_idp?redirectUrl=" + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL) + "&idp=cas", shorthand=False, ) self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers cas_uri = location_headers[0] cas_uri_path, cas_uri_query = cas_uri.split("?", 1) # it should redirect us to the login page of the cas server self.assertEqual(cas_uri_path, CAS_SERVER + "/login") # check that the redirectUrl is correctly encoded in the service param - ie, the # place that CAS will redirect to cas_uri_params = urllib.parse.parse_qs(cas_uri_query) service_uri = cas_uri_params["service"][0] _, service_uri_query = service_uri.split("?", 1) service_uri_params = urllib.parse.parse_qs(service_uri_query) self.assertEqual(service_uri_params["redirectUrl"][0], TEST_CLIENT_REDIRECT_URL) def test_multi_sso_redirect_to_saml(self) -> None: """If SAML is chosen, should redirect to the SAML server""" channel = self.make_request( "GET", "/_synapse/client/pick_idp?redirectUrl=" + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL) + "&idp=saml", ) self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers saml_uri = location_headers[0] saml_uri_path, saml_uri_query = saml_uri.split("?", 1) # it should redirect us to the login page of the SAML server self.assertEqual(saml_uri_path, SAML_SERVER) # the RelayState is used to carry the client redirect url saml_uri_params = urllib.parse.parse_qs(saml_uri_query) relay_state_param = saml_uri_params["RelayState"][0] self.assertEqual(relay_state_param, TEST_CLIENT_REDIRECT_URL) def test_login_via_oidc(self) -> None: """If OIDC is chosen, should redirect to the OIDC auth endpoint""" # pick the default OIDC provider channel = self.make_request( "GET", "/_synapse/client/pick_idp?redirectUrl=" + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL) + "&idp=oidc", ) self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers oidc_uri = location_headers[0] oidc_uri_path, oidc_uri_query = oidc_uri.split("?", 1) # it should redirect us to the auth page of the OIDC server self.assertEqual(oidc_uri_path, TEST_OIDC_AUTH_ENDPOINT) # ... and should have set a cookie including the redirect url cookie_headers = channel.headers.getRawHeaders("Set-Cookie") assert cookie_headers cookies: Dict[str, str] = {} for h in cookie_headers: key, value = h.split(";")[0].split("=", maxsplit=1) cookies[key] = value oidc_session_cookie = cookies["oidc_session"] macaroon = pymacaroons.Macaroon.deserialize(oidc_session_cookie) self.assertEqual( self._get_value_from_macaroon(macaroon, "client_redirect_url"), TEST_CLIENT_REDIRECT_URL, ) channel = self.helper.complete_oidc_auth(oidc_uri, cookies, {"sub": "user1"}) # that should serve a confirmation page self.assertEqual(channel.code, 200, channel.result) content_type_headers = channel.headers.getRawHeaders("Content-Type") assert content_type_headers self.assertTrue(content_type_headers[-1].startswith("text/html")) p = TestHtmlParser() p.feed(channel.text_body) p.close() # ... which should contain our redirect link self.assertEqual(len(p.links), 1) path, query = p.links[0].split("?", 1) self.assertEqual(path, "https://x") # it will have url-encoded the params properly, so we'll have to parse them params = urllib.parse.parse_qsl( query, keep_blank_values=True, strict_parsing=True, errors="strict" ) self.assertEqual(params[0:2], EXPECTED_CLIENT_REDIRECT_URL_PARAMS) self.assertEqual(params[2][0], "loginToken") # finally, submit the matrix login token to the login API, which gives us our # matrix access token, mxid, and device id. login_token = params[2][1] chan = self.make_request( "POST", "/login", content={"type": "m.login.token", "token": login_token}, ) self.assertEqual(chan.code, 200, chan.result) self.assertEqual(chan.json_body["user_id"], "@user1:test") def test_multi_sso_redirect_to_unknown(self) -> None: """An unknown IdP should cause a 400""" channel = self.make_request( "GET", "/_synapse/client/pick_idp?redirectUrl=http://x&idp=xyz", ) self.assertEqual(channel.code, 400, channel.result) def test_client_idp_redirect_to_unknown(self) -> None: """If the client tries to pick an unknown IdP, return a 404""" channel = self._make_sso_redirect_request("xxx") self.assertEqual(channel.code, 404, channel.result) self.assertEqual(channel.json_body["errcode"], "M_NOT_FOUND") def test_client_idp_redirect_to_oidc(self) -> None: """If the client pick a known IdP, redirect to it""" channel = self._make_sso_redirect_request("oidc") self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers oidc_uri = location_headers[0] oidc_uri_path, oidc_uri_query = oidc_uri.split("?", 1) # it should redirect us to the auth page of the OIDC server self.assertEqual(oidc_uri_path, TEST_OIDC_AUTH_ENDPOINT) def _make_sso_redirect_request(self, idp_prov: Optional[str] = None) -> FakeChannel: """Send a request to /_matrix/client/r0/login/sso/redirect ... possibly specifying an IDP provider """ endpoint = "/_matrix/client/r0/login/sso/redirect" if idp_prov is not None: endpoint += "/" + idp_prov endpoint += "?redirectUrl=" + urllib.parse.quote_plus(TEST_CLIENT_REDIRECT_URL) return self.make_request( "GET", endpoint, custom_headers=[("Host", SYNAPSE_SERVER_PUBLIC_HOSTNAME)], ) @staticmethod def _get_value_from_macaroon(macaroon: pymacaroons.Macaroon, key: str) -> str: prefix = key + " = " for caveat in macaroon.caveats: if caveat.caveat_id.startswith(prefix): return caveat.caveat_id[len(prefix) :] raise ValueError("No %s caveat in macaroon" % (key,)) class CASTestCase(unittest.HomeserverTestCase): servlets = [ login.register_servlets, ] def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: self.base_url = "https://matrix.goodserver.com/" self.redirect_path = "_synapse/client/login/sso/redirect/confirm" config = self.default_config() config["public_baseurl"] = ( config.get("public_baseurl") or "https://matrix.goodserver.com:8448" ) config["cas_config"] = { "enabled": True, "server_url": CAS_SERVER, } cas_user_id = "username" self.user_id = "@%s:test" % cas_user_id async def get_raw(uri: str, args: Any) -> bytes: """Return an example response payload from a call to the `/proxyValidate` endpoint of a CAS server, copied from https://apereo.github.io/cas/5.0.x/protocol/CAS-Protocol-V2-Specification.html#26-proxyvalidate-cas-20 This needs to be returned by an async function (as opposed to set as the mock's return value) because the corresponding Synapse code awaits on it. """ return ( """ <cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> <cas:authenticationSuccess> <cas:user>%s</cas:user> <cas:proxyGrantingTicket>PGTIOU-84678-8a9d...</cas:proxyGrantingTicket> <cas:proxies> <cas:proxy>https://proxy2/pgtUrl</cas:proxy> <cas:proxy>https://proxy1/pgtUrl</cas:proxy> </cas:proxies> </cas:authenticationSuccess> </cas:serviceResponse> """ % cas_user_id ).encode("utf-8") mocked_http_client = Mock(spec=["get_raw"]) mocked_http_client.get_raw.side_effect = get_raw self.hs = self.setup_test_homeserver( config=config, proxied_http_client=mocked_http_client, ) return self.hs def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: self.deactivate_account_handler = hs.get_deactivate_account_handler() def test_cas_redirect_confirm(self) -> None: """Tests that the SSO login flow serves a confirmation page before redirecting a user to the redirect URL. """ base_url = "/_matrix/client/r0/login/cas/ticket?redirectUrl" redirect_url = "https://dodgy-site.com/" url_parts = list(urllib.parse.urlparse(base_url)) query = dict(urllib.parse.parse_qsl(url_parts[4])) query.update({"redirectUrl": redirect_url}) query.update({"ticket": "ticket"}) url_parts[4] = urllib.parse.urlencode(query) cas_ticket_url = urllib.parse.urlunparse(url_parts) # Get Synapse to call the fake CAS and serve the template. channel = self.make_request("GET", cas_ticket_url) # Test that the response is HTML. self.assertEqual(channel.code, 200, channel.result) content_type_header_value = "" for header in channel.result.get("headers", []): if header[0] == b"Content-Type": content_type_header_value = header[1].decode("utf8") self.assertTrue(content_type_header_value.startswith("text/html")) # Test that the body isn't empty. self.assertTrue(len(channel.result["body"]) > 0) # And that it contains our redirect link self.assertIn(redirect_url, channel.result["body"].decode("UTF-8")) @override_config( { "sso": { "client_whitelist": [ "https://legit-site.com/", "https://other-site.com/", ] } } ) def test_cas_redirect_whitelisted(self) -> None: """Tests that the SSO login flow serves a redirect to a whitelisted url""" self._test_redirect("https://legit-site.com/") @override_config({"public_baseurl": "https://example.com"}) def test_cas_redirect_login_fallback(self) -> None: self._test_redirect("https://example.com/_matrix/static/client/login") def _test_redirect(self, redirect_url: str) -> None: """Tests that the SSO login flow serves a redirect for the given redirect URL.""" cas_ticket_url = ( "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket" % (urllib.parse.quote(redirect_url)) ) # Get Synapse to call the fake CAS and serve the template. channel = self.make_request("GET", cas_ticket_url) self.assertEqual(channel.code, 302) location_headers = channel.headers.getRawHeaders("Location") assert location_headers self.assertEqual(location_headers[0][: len(redirect_url)], redirect_url) @override_config({"sso": {"client_whitelist": ["https://legit-site.com/"]}}) def test_deactivated_user(self) -> None: """Logging in as a deactivated account should error.""" redirect_url = "https://legit-site.com/" # First login (to create the user). self._test_redirect(redirect_url) # Deactivate the account. self.get_success( self.deactivate_account_handler.deactivate_account( self.user_id, False, create_requester(self.user_id) ) ) # Request the CAS ticket. cas_ticket_url = ( "/_matrix/client/r0/login/cas/ticket?redirectUrl=%s&ticket=ticket" % (urllib.parse.quote(redirect_url)) ) # Get Synapse to call the fake CAS and serve the template. channel = self.make_request("GET", cas_ticket_url) # Because the user is deactivated they are served an error template. self.assertEqual(channel.code, 403) self.assertIn(b"SSO account deactivated", channel.result["body"]) @skip_unless(HAS_JWT, "requires jwt") class JWTTestCase(unittest.HomeserverTestCase): servlets = [ synapse.rest.admin.register_servlets_for_client_rest_resource, login.register_servlets, ] jwt_secret = "secret" jwt_algorithm = "HS256" base_config = { "enabled": True, "secret": jwt_secret, "algorithm": jwt_algorithm, } def default_config(self) -> Dict[str, Any]: config = super().default_config() # If jwt_config has been defined (eg via @override_config), don't replace it. if config.get("jwt_config") is None: config["jwt_config"] = self.base_config return config def jwt_encode(self, payload: Dict[str, Any], secret: str = jwt_secret) -> str: # PyJWT 2.0.0 changed the return type of jwt.encode from bytes to str. result: Union[str, bytes] = jwt.encode(payload, secret, self.jwt_algorithm) if isinstance(result, bytes): return result.decode("ascii") return result def jwt_login(self, *args: Any) -> FakeChannel: params = {"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)} channel = self.make_request(b"POST", LOGIN_URL, params) return channel def test_login_jwt_valid_registered(self) -> None: self.register_user("kermit", "monkey") channel = self.jwt_login({"sub": "kermit"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") def test_login_jwt_valid_unregistered(self) -> None: channel = self.jwt_login({"sub": "frog"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@frog:test") def test_login_jwt_invalid_signature(self) -> None: channel = self.jwt_login({"sub": "frog"}, "notsecret") self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Signature verification failed", ) def test_login_jwt_expired(self) -> None: channel = self.jwt_login({"sub": "frog", "exp": 864000}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Signature has expired" ) def test_login_jwt_not_before(self) -> None: now = int(time.time()) channel = self.jwt_login({"sub": "frog", "nbf": now + 3600}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: The token is not yet valid (nbf)", ) def test_login_no_sub(self) -> None: channel = self.jwt_login({"username": "root"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual(channel.json_body["error"], "Invalid JWT") @override_config({"jwt_config": {**base_config, "issuer": "test-issuer"}}) def test_login_iss(self) -> None: """Test validating the issuer claim.""" # A valid issuer. channel = self.jwt_login({"sub": "kermit", "iss": "test-issuer"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") # An invalid issuer. channel = self.jwt_login({"sub": "kermit", "iss": "invalid"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Invalid issuer" ) # Not providing an issuer. channel = self.jwt_login({"sub": "kermit"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], 'JWT validation failed: Token is missing the "iss" claim', ) def test_login_iss_no_config(self) -> None: """Test providing an issuer claim without requiring it in the configuration.""" channel = self.jwt_login({"sub": "kermit", "iss": "invalid"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") @override_config({"jwt_config": {**base_config, "audiences": ["test-audience"]}}) def test_login_aud(self) -> None: """Test validating the audience claim.""" # A valid audience. channel = self.jwt_login({"sub": "kermit", "aud": "test-audience"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") # An invalid audience. channel = self.jwt_login({"sub": "kermit", "aud": "invalid"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Invalid audience" ) # Not providing an audience. channel = self.jwt_login({"sub": "kermit"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], 'JWT validation failed: Token is missing the "aud" claim', ) def test_login_aud_no_config(self) -> None: """Test providing an audience without requiring it in the configuration.""" channel = self.jwt_login({"sub": "kermit", "aud": "invalid"}) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Invalid audience" ) def test_login_default_sub(self) -> None: """Test reading user ID from the default subject claim.""" channel = self.jwt_login({"sub": "kermit"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") @override_config({"jwt_config": {**base_config, "subject_claim": "username"}}) def test_login_custom_sub(self) -> None: """Test reading user ID from a custom subject claim.""" channel = self.jwt_login({"username": "frog"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@frog:test") def test_login_no_token(self) -> None: params = {"type": "org.matrix.login.jwt"} channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual(channel.json_body["error"], "Token field for JWT is missing") # The JWTPubKeyTestCase is a complement to JWTTestCase where we instead use # RSS256, with a public key configured in synapse as "jwt_secret", and tokens # signed by the private key. @skip_unless(HAS_JWT, "requires jwt") class JWTPubKeyTestCase(unittest.HomeserverTestCase): servlets = [ login.register_servlets, ] # This key's pubkey is used as the jwt_secret setting of synapse. Valid # tokens are signed by this and validated using the pubkey. It is generated # with `openssl genrsa 512` (not a secure way to generate real keys, but # good enough for tests!) jwt_privatekey = "\n".join( [ "-----BEGIN RSA PRIVATE KEY-----", "MIIBPAIBAAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7TKO1vSEWdq7u9x8SMFiB", "492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQJAUv7OOSOtiU+wzJq82rnk", "yR4NHqt7XX8BvkZPM7/+EjBRanmZNSp5kYZzKVaZ/gTOM9+9MwlmhidrUOweKfB/", "kQIhAPZwHazbjo7dYlJs7wPQz1vd+aHSEH+3uQKIysebkmm3AiEA1nc6mDdmgiUq", "TpIN8A4MBKmfZMWTLq6z05y/qjKyxb0CIQDYJxCwTEenIaEa4PdoJl+qmXFasVDN", "ZU0+XtNV7yul0wIhAMI9IhiStIjS2EppBa6RSlk+t1oxh2gUWlIh+YVQfZGRAiEA", "tqBR7qLZGJ5CVKxWmNhJZGt1QHoUtOch8t9C4IdOZ2g=", "-----END RSA PRIVATE KEY-----", ] ) # Generated with `openssl rsa -in foo.key -pubout`, with the the above # private key placed in foo.key (jwt_privatekey). jwt_pubkey = "\n".join( [ "-----BEGIN PUBLIC KEY-----", "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM50f1Q5gsdmzifLstzLHb5NhfajiOt7", "TKO1vSEWdq7u9x8SMFiB492RM9W/XFoh8WUfL9uL6Now6tPRDsWv3xsCAwEAAQ==", "-----END PUBLIC KEY-----", ] ) # This key is used to sign tokens that shouldn't be accepted by synapse. # Generated just like jwt_privatekey. bad_privatekey = "\n".join( [ "-----BEGIN RSA PRIVATE KEY-----", "MIIBOgIBAAJBAL//SQrKpKbjCCnv/FlasJCv+t3k/MPsZfniJe4DVFhsktF2lwQv", "gLjmQD3jBUTz+/FndLSBvr3F4OHtGL9O/osCAwEAAQJAJqH0jZJW7Smzo9ShP02L", "R6HRZcLExZuUrWI+5ZSP7TaZ1uwJzGFspDrunqaVoPobndw/8VsP8HFyKtceC7vY", "uQIhAPdYInDDSJ8rFKGiy3Ajv5KWISBicjevWHF9dbotmNO9AiEAxrdRJVU+EI9I", "eB4qRZpY6n4pnwyP0p8f/A3NBaQPG+cCIFlj08aW/PbxNdqYoBdeBA0xDrXKfmbb", "iwYxBkwL0JCtAiBYmsi94sJn09u2Y4zpuCbJeDPKzWkbuwQh+W1fhIWQJQIhAKR0", "KydN6cRLvphNQ9c/vBTdlzWxzcSxREpguC7F1J1m", "-----END RSA PRIVATE KEY-----", ] ) def default_config(self) -> Dict[str, Any]: config = super().default_config() config["jwt_config"] = { "enabled": True, "secret": self.jwt_pubkey, "algorithm": "RS256", } return config def jwt_encode(self, payload: Dict[str, Any], secret: str = jwt_privatekey) -> str: # PyJWT 2.0.0 changed the return type of jwt.encode from bytes to str. result: Union[bytes, str] = jwt.encode(payload, secret, "RS256") if isinstance(result, bytes): return result.decode("ascii") return result def jwt_login(self, *args: Any) -> FakeChannel: params = {"type": "org.matrix.login.jwt", "token": self.jwt_encode(*args)} channel = self.make_request(b"POST", LOGIN_URL, params) return channel def test_login_jwt_valid(self) -> None: channel = self.jwt_login({"sub": "kermit"}) self.assertEqual(channel.result["code"], b"200", channel.result) self.assertEqual(channel.json_body["user_id"], "@kermit:test") def test_login_jwt_invalid_signature(self) -> None: channel = self.jwt_login({"sub": "frog"}, self.bad_privatekey) self.assertEqual(channel.result["code"], b"403", channel.result) self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN") self.assertEqual( channel.json_body["error"], "JWT validation failed: Signature verification failed", ) AS_USER = "as_user_alice" class AppserviceLoginRestServletTestCase(unittest.HomeserverTestCase): servlets = [ login.register_servlets, register.register_servlets, ] def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: self.hs = self.setup_test_homeserver() self.service = ApplicationService( id="unique_identifier", token="some_token", hostname="example.com", sender="@asbot:example.com", namespaces={ ApplicationService.NS_USERS: [ {"regex": r"@as_user.*", "exclusive": False} ], ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES: [], }, ) self.another_service = ApplicationService( id="another__identifier", token="another_token", hostname="example.com", sender="@as2bot:example.com", namespaces={ ApplicationService.NS_USERS: [ {"regex": r"@as2_user.*", "exclusive": False} ], ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES: [], }, ) self.hs.get_datastores().main.services_cache.append(self.service) self.hs.get_datastores().main.services_cache.append(self.another_service) return self.hs def test_login_appservice_user(self) -> None: """Test that an appservice user can use /login""" self.register_appservice_user(AS_USER, self.service.token) params = { "type": login.LoginRestServlet.APPSERVICE_TYPE, "identifier": {"type": "m.id.user", "user": AS_USER}, } channel = self.make_request( b"POST", LOGIN_URL, params, access_token=self.service.token ) self.assertEqual(channel.result["code"], b"200", channel.result) def test_login_appservice_user_bot(self) -> None: """Test that the appservice bot can use /login""" self.register_appservice_user(AS_USER, self.service.token) params = { "type": login.LoginRestServlet.APPSERVICE_TYPE, "identifier": {"type": "m.id.user", "user": self.service.sender}, } channel = self.make_request( b"POST", LOGIN_URL, params, access_token=self.service.token ) self.assertEqual(channel.result["code"], b"200", channel.result) def test_login_appservice_wrong_user(self) -> None: """Test that non-as users cannot login with the as token""" self.register_appservice_user(AS_USER, self.service.token) params = { "type": login.LoginRestServlet.APPSERVICE_TYPE, "identifier": {"type": "m.id.user", "user": "fibble_wibble"}, } channel = self.make_request( b"POST", LOGIN_URL, params, access_token=self.service.token ) self.assertEqual(channel.result["code"], b"403", channel.result) def test_login_appservice_wrong_as(self) -> None: """Test that as users cannot login with wrong as token""" self.register_appservice_user(AS_USER, self.service.token) params = { "type": login.LoginRestServlet.APPSERVICE_TYPE, "identifier": {"type": "m.id.user", "user": AS_USER}, } channel = self.make_request( b"POST", LOGIN_URL, params, access_token=self.another_service.token ) self.assertEqual(channel.result["code"], b"403", channel.result) def test_login_appservice_no_token(self) -> None: """Test that users must provide a token when using the appservice login method """ self.register_appservice_user(AS_USER, self.service.token) params = { "type": login.LoginRestServlet.APPSERVICE_TYPE, "identifier": {"type": "m.id.user", "user": AS_USER}, } channel = self.make_request(b"POST", LOGIN_URL, params) self.assertEqual(channel.result["code"], b"401", channel.result) @skip_unless(HAS_OIDC, "requires OIDC") class UsernamePickerTestCase(HomeserverTestCase): """Tests for the username picker flow of SSO login""" servlets = [login.register_servlets] def default_config(self) -> Dict[str, Any]: config = super().default_config() config["public_baseurl"] = BASE_URL config["oidc_config"] = {} config["oidc_config"].update(TEST_OIDC_CONFIG) config["oidc_config"]["user_mapping_provider"] = { "config": {"display_name_template": "{{ user.displayname }}"} } # whitelist this client URI so we redirect straight to it rather than # serving a confirmation page config["sso"] = {"client_whitelist": ["https://x"]} return config def create_resource_dict(self) -> Dict[str, Resource]: d = super().create_resource_dict() d.update(build_synapse_client_resource_tree(self.hs)) return d def test_username_picker(self) -> None: """Test the happy path of a username picker flow.""" # do the start of the login flow channel = self.helper.auth_via_oidc( {"sub": "tester", "displayname": "Jonny"}, TEST_CLIENT_REDIRECT_URL ) # that should redirect to the username picker self.assertEqual(channel.code, 302, channel.result) location_headers = channel.headers.getRawHeaders("Location") assert location_headers picker_url = location_headers[0] self.assertEqual(picker_url, "/_synapse/client/pick_username/account_details") # ... with a username_mapping_session cookie cookies: Dict[str, str] = {} channel.extract_cookies(cookies) self.assertIn("username_mapping_session", cookies) session_id = cookies["username_mapping_session"] # introspect the sso handler a bit to check that the username mapping session # looks ok. username_mapping_sessions = self.hs.get_sso_handler()._username_mapping_sessions self.assertIn( session_id, username_mapping_sessions, "session id not found in map", ) session = username_mapping_sessions[session_id] self.assertEqual(session.remote_user_id, "tester") self.assertEqual(session.display_name, "Jonny") self.assertEqual(session.client_redirect_url, TEST_CLIENT_REDIRECT_URL) # the expiry time should be about 15 minutes away expected_expiry = self.clock.time_msec() + (15 * 60 * 1000) self.assertApproximates(session.expiry_time_ms, expected_expiry, tolerance=1000) # Now, submit a username to the username picker, which should serve a redirect # to the completion page content = urlencode({b"username": b"bobby"}).encode("utf8") chan = self.make_request( "POST", path=picker_url, content=content, content_is_form=True, custom_headers=[ ("Cookie", "username_mapping_session=" + session_id), # old versions of twisted don't do form-parsing without a valid # content-length header. ("Content-Length", str(len(content))), ], ) self.assertEqual(chan.code, 302, chan.result) location_headers = chan.headers.getRawHeaders("Location") assert location_headers # send a request to the completion page, which should 302 to the client redirectUrl chan = self.make_request( "GET", path=location_headers[0], custom_headers=[("Cookie", "username_mapping_session=" + session_id)], ) self.assertEqual(chan.code, 302, chan.result) location_headers = chan.headers.getRawHeaders("Location") assert location_headers # ensure that the returned location matches the requested redirect URL path, query = location_headers[0].split("?", 1) self.assertEqual(path, "https://x") # it will have url-encoded the params properly, so we'll have to parse them params = urllib.parse.parse_qsl( query, keep_blank_values=True, strict_parsing=True, errors="strict" ) self.assertEqual(params[0:2], EXPECTED_CLIENT_REDIRECT_URL_PARAMS) self.assertEqual(params[2][0], "loginToken") # fish the login token out of the returned redirect uri login_token = params[2][1] # finally, submit the matrix login token to the login API, which gives us our # matrix access token, mxid, and device id. chan = self.make_request( "POST", "/login", content={"type": "m.login.token", "token": login_token}, ) self.assertEqual(chan.code, 200, chan.result) self.assertEqual(chan.json_body["user_id"], "@bobby:test")
[((16039, 16101), 'tests.unittest.skip_unless', 'skip_unless', (['(has_saml2 and HAS_OIDC)', '"""Requires SAML2 and OIDC"""'], {}), "(has_saml2 and HAS_OIDC, 'Requires SAML2 and OIDC')\n", (16050, 16101), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((33304, 33340), 'tests.unittest.skip_unless', 'skip_unless', (['HAS_JWT', '"""requires jwt"""'], {}), "(HAS_JWT, 'requires jwt')\n", (33315, 33340), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((41043, 41079), 'tests.unittest.skip_unless', 'skip_unless', (['HAS_JWT', '"""requires jwt"""'], {}), "(HAS_JWT, 'requires jwt')\n", (41054, 41079), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((49000, 49038), 'tests.unittest.skip_unless', 'skip_unless', (['HAS_OIDC', '"""requires OIDC"""'], {}), "(HAS_OIDC, 'requires OIDC')\n", (49011, 49038), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((4051, 4193), 'tests.unittest.override_config', 'override_config', (["{'rc_login': {'address': {'per_second': 0.17, 'burst_count': 5}, 'account':\n {'per_second': 10000, 'burst_count': 10000}}}"], {}), "({'rc_login': {'address': {'per_second': 0.17, 'burst_count':\n 5}, 'account': {'per_second': 10000, 'burst_count': 10000}}})\n", (4066, 4193), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((5976, 6118), 'tests.unittest.override_config', 'override_config', (["{'rc_login': {'account': {'per_second': 0.17, 'burst_count': 5}, 'address':\n {'per_second': 10000, 'burst_count': 10000}}}"], {}), "({'rc_login': {'account': {'per_second': 0.17, 'burst_count':\n 5}, 'address': {'per_second': 10000, 'burst_count': 10000}}})\n", (5991, 6118), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((7729, 7883), 'tests.unittest.override_config', 'override_config', (["{'rc_login': {'address': {'per_second': 10000, 'burst_count': 10000},\n 'failed_attempts': {'per_second': 0.17, 'burst_count': 5}}}"], {}), "({'rc_login': {'address': {'per_second': 10000,\n 'burst_count': 10000}, 'failed_attempts': {'per_second': 0.17,\n 'burst_count': 5}}})\n", (7744, 7883), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((9520, 9564), 'tests.unittest.override_config', 'override_config', (["{'session_lifetime': '24h'}"], {}), "({'session_lifetime': '24h'})\n", (9535, 9564), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((13069, 13113), 'tests.unittest.override_config', 'override_config', (["{'session_lifetime': '24h'}"], {}), "({'session_lifetime': '24h'})\n", (13084, 13113), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((14134, 14178), 'tests.unittest.override_config', 'override_config', (["{'session_lifetime': '24h'}"], {}), "({'session_lifetime': '24h'})\n", (14149, 14178), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((30948, 31054), 'tests.unittest.override_config', 'override_config', (["{'sso': {'client_whitelist': ['https://legit-site.com/',\n 'https://other-site.com/']}}"], {}), "({'sso': {'client_whitelist': ['https://legit-site.com/',\n 'https://other-site.com/']}})\n", (30963, 31054), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((31373, 31431), 'tests.unittest.override_config', 'override_config', (["{'public_baseurl': 'https://example.com'}"], {}), "({'public_baseurl': 'https://example.com'})\n", (31388, 31431), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((32240, 32315), 'tests.unittest.override_config', 'override_config', (["{'sso': {'client_whitelist': ['https://legit-site.com/']}}"], {}), "({'sso': {'client_whitelist': ['https://legit-site.com/']}})\n", (32255, 32315), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((36647, 36720), 'tests.unittest.override_config', 'override_config', (["{'jwt_config': {**base_config, 'issuer': 'test-issuer'}}"], {}), "({'jwt_config': {**base_config, 'issuer': 'test-issuer'}})\n", (36662, 36720), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((38145, 38230), 'tests.unittest.override_config', 'override_config', (["{'jwt_config': {**base_config, 'audiences': ['test-audience']}}"], {}), "({'jwt_config': {**base_config, 'audiences': ['test-audience']}}\n )\n", (38160, 38230), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((40085, 40162), 'tests.unittest.override_config', 'override_config', (["{'jwt_config': {**base_config, 'subject_claim': 'username'}}"], {}), "({'jwt_config': {**base_config, 'subject_claim': 'username'}})\n", (40100, 40162), False, 'from tests.unittest import HomeserverTestCase, override_config, skip_unless\n'), ((19600, 19616), 'tests.test_utils.html_parsers.TestHtmlParser', 'TestHtmlParser', ([], {}), '()\n', (19614, 19616), False, 'from tests.test_utils.html_parsers import TestHtmlParser\n'), ((23500, 23553), 'pymacaroons.Macaroon.deserialize', 'pymacaroons.Macaroon.deserialize', (['oidc_session_cookie'], {}), '(oidc_session_cookie)\n', (23532, 23553), False, 'import pymacaroons\n'), ((24099, 24115), 'tests.test_utils.html_parsers.TestHtmlParser', 'TestHtmlParser', ([], {}), '()\n', (24113, 24115), False, 'from tests.test_utils.html_parsers import TestHtmlParser\n'), ((29167, 29189), 'unittest.mock.Mock', 'Mock', ([], {'spec': "['get_raw']"}), "(spec=['get_raw'])\n", (29171, 29189), False, 'from unittest.mock import Mock\n'), ((34186, 34233), 'jwt.encode', 'jwt.encode', (['payload', 'secret', 'self.jwt_algorithm'], {}), '(payload, secret, self.jwt_algorithm)\n', (34196, 34233), False, 'import jwt\n'), ((43838, 43874), 'jwt.encode', 'jwt.encode', (['payload', 'secret', '"""RS256"""'], {}), "(payload, secret, 'RS256')\n", (43848, 43874), False, 'import jwt\n'), ((45204, 45495), 'synapse.appservice.ApplicationService', 'ApplicationService', ([], {'id': '"""unique_identifier"""', 'token': '"""some_token"""', 'hostname': '"""example.com"""', 'sender': '"""@asbot:example.com"""', 'namespaces': "{ApplicationService.NS_USERS: [{'regex': '@as_user.*', 'exclusive': False}],\n ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES: []}"}), "(id='unique_identifier', token='some_token', hostname=\n 'example.com', sender='@asbot:example.com', namespaces={\n ApplicationService.NS_USERS: [{'regex': '@as_user.*', 'exclusive': \n False}], ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES:\n []})\n", (45222, 45495), False, 'from synapse.appservice import ApplicationService\n'), ((45681, 45978), 'synapse.appservice.ApplicationService', 'ApplicationService', ([], {'id': '"""another__identifier"""', 'token': '"""another_token"""', 'hostname': '"""example.com"""', 'sender': '"""@as2bot:example.com"""', 'namespaces': "{ApplicationService.NS_USERS: [{'regex': '@as2_user.*', 'exclusive': False}\n ], ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES: []}"}), "(id='another__identifier', token='another_token',\n hostname='example.com', sender='@as2bot:example.com', namespaces={\n ApplicationService.NS_USERS: [{'regex': '@as2_user.*', 'exclusive': \n False}], ApplicationService.NS_ROOMS: [], ApplicationService.NS_ALIASES:\n []})\n", (45699, 45978), False, 'from synapse.appservice import ApplicationService\n'), ((17815, 17858), 'synapse.rest.synapse.client.build_synapse_client_resource_tree', 'build_synapse_client_resource_tree', (['self.hs'], {}), '(self.hs)\n', (17849, 17858), False, 'from synapse.rest.synapse.client import build_synapse_client_resource_tree\n'), ((35961, 35972), 'time.time', 'time.time', ([], {}), '()\n', (35970, 35972), False, 'import time\n'), ((49877, 49920), 'synapse.rest.synapse.client.build_synapse_client_resource_tree', 'build_synapse_client_resource_tree', (['self.hs'], {}), '(self.hs)\n', (49911, 49920), False, 'from synapse.rest.synapse.client import build_synapse_client_resource_tree\n'), ((32723, 32753), 'synapse.types.create_requester', 'create_requester', (['self.user_id'], {}), '(self.user_id)\n', (32739, 32753), False, 'from synapse.types import create_requester\n'), ((51755, 51789), 'urllib.parse.urlencode', 'urlencode', (["{b'username': b'bobby'}"], {}), "({b'username': b'bobby'})\n", (51764, 51789), False, 'from urllib.parse import urlencode\n'), ((3584, 3605), 'synapse.rest.client.account.WhoamiRestServlet', 'WhoamiRestServlet', (['hs'], {}), '(hs)\n', (3601, 3605), False, 'from synapse.rest.client.account import WhoamiRestServlet\n'), ((15777, 15793), 'json.dumps', 'json.dumps', (['body'], {}), '(body)\n', (15787, 15793), False, 'import json\n')]
Kevinskwk/rmf_demos
rmf_demo_tasks/rmf_demo_tasks/request_delivery.py
2d7b9c7c75211b89b91977e5d1a66f440cc5df95
import argparse import sys from time import sleep import uuid import rclpy from rmf_task_msgs.msg import Delivery def main(argv = sys.argv): rclpy.init(args=argv) args_without_ros = rclpy.utilities.remove_ros_args(argv) ''' # Example request: task_id: randomid_001 items: [itemA, itemB....] pickup_place_name: cssd_room pickup_behavior: - name: dispenser - parameters: [request_guid: xxx, target_guid:cssdbot, transporter_type:mir] dropoff_place_name: ot_prep_room dropoff_behavior: - name: dispenser - parameters: [request_guid: yyy, target_guid:otbot, transporter_type:mir] ''' parser = argparse.ArgumentParser() parser.add_argument('-p', '--pickup', default='pantry', help='Start waypoint') parser.add_argument('-d', '--dropoff', default='hardware_2', help='Finish waypoint') parser.add_argument('-i', '--task-id', help='Task ID', default='', type=str) parser.add_argument('-r', '--robot-type', help='Type of robot', default='magni') args = parser.parse_args(args_without_ros[1:]) node = rclpy.create_node('loop_request_publisher') publisher = node.create_publisher(Delivery, 'delivery_requests', 10) sleep(0.5) request = Delivery() if args.task_id: request.task_id = args.task_id else: request.task_id = 'delivery#' + str(uuid.uuid1()) request.pickup_place_name = args.pickup request.dropoff_place_name = args.dropoff for _ in range(5): publisher.publish(request) sleep(0.5) rclpy.shutdown() print(f'Delivery request submitted to {args.robot_type}') if __name__ == '__main__': main(sys.argv)
[((149, 170), 'rclpy.init', 'rclpy.init', ([], {'args': 'argv'}), '(args=argv)\n', (159, 170), False, 'import rclpy\n'), ((194, 231), 'rclpy.utilities.remove_ros_args', 'rclpy.utilities.remove_ros_args', (['argv'], {}), '(argv)\n', (225, 231), False, 'import rclpy\n'), ((659, 684), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (682, 684), False, 'import argparse\n'), ((1087, 1130), 'rclpy.create_node', 'rclpy.create_node', (['"""loop_request_publisher"""'], {}), "('loop_request_publisher')\n", (1104, 1130), False, 'import rclpy\n'), ((1209, 1219), 'time.sleep', 'sleep', (['(0.5)'], {}), '(0.5)\n', (1214, 1219), False, 'from time import sleep\n'), ((1235, 1245), 'rmf_task_msgs.msg.Delivery', 'Delivery', ([], {}), '()\n', (1243, 1245), False, 'from rmf_task_msgs.msg import Delivery\n'), ((1555, 1571), 'rclpy.shutdown', 'rclpy.shutdown', ([], {}), '()\n', (1569, 1571), False, 'import rclpy\n'), ((1531, 1541), 'time.sleep', 'sleep', (['(0.5)'], {}), '(0.5)\n', (1536, 1541), False, 'from time import sleep\n'), ((1360, 1372), 'uuid.uuid1', 'uuid.uuid1', ([], {}), '()\n', (1370, 1372), False, 'import uuid\n')]
BoredManCodes/Dis-Snek
dis_snek/api/http/http_client.py
662dbc3f86c133fd704c22d3d6d55af5ee1f6f5b
"""This file handles the interaction with discords http endpoints.""" import asyncio import logging from typing import Any, Dict, Optional, Union from urllib.parse import quote as _uriquote from weakref import WeakValueDictionary import aiohttp from aiohttp import BaseConnector, ClientSession, ClientWebSocketResponse, FormData from multidict import CIMultiDictProxy from dis_snek.api.http.http_requests import ( BotRequests, ChannelRequests, EmojiRequests, GuildRequests, InteractionRequests, MemberRequests, MessageRequests, ReactionRequests, StickerRequests, ThreadRequests, UserRequests, WebhookRequests, ScheduledEventsRequests, ) from dis_snek.client.const import __py_version__, __repo_url__, __version__, logger_name, MISSING, Absent from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError from dis_snek.client.utils.input_utils import response_decode from dis_snek.client.utils.serializer import dict_filter_missing from dis_snek.models import CooldownSystem from .route import Route __all__ = ["HTTPClient"] log = logging.getLogger(logger_name) class GlobalLock: """Manages the global ratelimit""" def __init__(self) -> None: self.cooldown_system: CooldownSystem = CooldownSystem( 45, 1 ) # global rate-limit is 50 per second, conservatively we use 45 self._lock: asyncio.Lock = asyncio.Lock() async def rate_limit(self) -> None: async with self._lock: while not self.cooldown_system.acquire_token(): await asyncio.sleep(self.cooldown_system.get_cooldown_time()) async def lock(self, delta: float) -> None: """ Lock the global lock for a given duration. Args: delta: The time to keep the lock acquired """ await self._lock.acquire() await asyncio.sleep(delta) self._lock.release() class BucketLock: """Manages the ratelimit for each bucket""" def __init__(self) -> None: self._lock: asyncio.Lock = asyncio.Lock() self.unlock_on_exit: bool = True self.bucket_hash: Optional[str] = None self.limit: int = -1 self.remaining: int = -1 self.delta: float = 0.0 def __repr__(self) -> str: return f"<BucketLock: {self.bucket_hash or 'Generic'}>" @property def locked(self) -> bool: """Return True if lock is acquired.""" return self._lock.locked() def unlock(self) -> None: """Unlock this bucket.""" self._lock.release() def ingest_ratelimit_header(self, header: CIMultiDictProxy) -> None: """ Ingests a discord rate limit header to configure this bucket lock. Args: header: A header from a http response """ self.bucket_hash = header.get("x-ratelimit-bucket") self.limit = int(header.get("x-ratelimit-limit") or -1) self.remaining = int(header.get("x-ratelimit-remaining") or -1) self.delta = float(header.get("x-ratelimit-reset-after", 0.0)) async def blind_defer_unlock(self) -> None: """Unlocks the BucketLock but doesn't wait for completion.""" self.unlock_on_exit = False loop = asyncio.get_running_loop() loop.call_later(self.delta, self.unlock) async def defer_unlock(self) -> None: """Unlocks the BucketLock after a specified delay.""" self.unlock_on_exit = False await asyncio.sleep(self.delta) self.unlock() async def __aenter__(self) -> None: await self._lock.acquire() async def __aexit__(self, *args) -> None: if self.unlock_on_exit and self._lock.locked(): self.unlock() self.unlock_on_exit = True class HTTPClient( BotRequests, ChannelRequests, EmojiRequests, GuildRequests, InteractionRequests, MemberRequests, MessageRequests, ReactionRequests, StickerRequests, ThreadRequests, UserRequests, WebhookRequests, ScheduledEventsRequests, ): """A http client for sending requests to the Discord API.""" def __init__(self, connector: Optional[BaseConnector] = None, loop: Optional[asyncio.AbstractEventLoop] = None): self.connector: Optional[BaseConnector] = connector self.loop = asyncio.get_event_loop() if loop is None else loop self.__session: Absent[Optional[ClientSession]] = MISSING self.token: Optional[str] = None self.global_lock: GlobalLock = GlobalLock() self._max_attempts: int = 3 self.ratelimit_locks: WeakValueDictionary[str, BucketLock] = WeakValueDictionary() self._endpoints = {} self.user_agent: str = ( f"DiscordBot ({__repo_url__} {__version__} Python/{__py_version__}) aiohttp/{aiohttp.__version__}" ) def __del__(self): if self.__session and not self.__session.closed: self.loop.run_until_complete(self.__session.close()) def get_ratelimit(self, route: Route) -> BucketLock: """ Get a route's rate limit bucket. Args: route: The route to fetch the ratelimit bucket for Returns: The BucketLock object for this route """ if bucket_hash := self._endpoints.get(route.rl_bucket): # we have seen this route before, we know which bucket it is associated with lock = self.ratelimit_locks.get(bucket_hash) if lock: # if we have an active lock on this route, it'll still be in the cache # return that lock return lock # if no cached lock exists, return a new lock return BucketLock() def ingest_ratelimit(self, route: Route, header: CIMultiDictProxy, bucket_lock: BucketLock) -> None: """ Ingests a ratelimit header from discord to determine ratelimit. Args: route: The route we're ingesting ratelimit for header: The rate limit header in question bucket_lock: The rate limit bucket for this route """ bucket_lock.ingest_ratelimit_header(header) if bucket_lock.bucket_hash: # We only ever try and cache the bucket if the bucket hash has been set (ignores unlimited endpoints) log.debug(f"Caching ingested rate limit data for: {bucket_lock.bucket_hash}") self._endpoints[route.rl_bucket] = bucket_lock.bucket_hash self.ratelimit_locks[bucket_lock.bucket_hash] = bucket_lock async def request( self, route: Route, data: Absent[Union[dict, FormData]] = MISSING, reason: Absent[str] = MISSING, **kwargs: Dict[str, Any], ) -> Any: """ Make a request to discord. parameters: route: The route to take json: A json payload to send in the request reason: Attach a reason to this request, used for audit logs """ # Assemble headers kwargs["headers"] = {"User-Agent": self.user_agent} if self.token: kwargs["headers"]["Authorization"] = f"Bot {self.token}" if reason not in (None, MISSING): kwargs["headers"]["X-Audit-Log-Reason"] = _uriquote(reason, safe="/ ") if isinstance(data, (list, dict)): kwargs["headers"]["Content-Type"] = "application/json" # sanity check payload if isinstance(data, list): kwargs["json"] = [dict_filter_missing(x) if isinstance(x, dict) else x for x in data] elif isinstance(data, dict): kwargs["json"] = dict_filter_missing(data) elif isinstance(data, FormData): kwargs["data"] = data lock = self.get_ratelimit(route) # this gets a BucketLock for this route. # If this endpoint has been used before, it will get an existing ratelimit for the respective buckethash # otherwise a brand-new bucket lock will be returned for attempt in range(self._max_attempts): async with lock: try: await self.global_lock.rate_limit() # prevent us exceeding the global rate limit by throttling http requests if self.__session.closed: await self.login(self.token) async with self.__session.request(route.method, route.url, **kwargs) as response: result = await response_decode(response) self.ingest_ratelimit(route, response.headers, lock) if response.status == 429: # ratelimit exceeded if result.get("global", False): # if we get a global, that's pretty bad, this would usually happen if the user is hitting the api from 2 clients sharing a token log.error( f"Bot has exceeded global ratelimit, locking REST API for {result.get('retry_after')} seconds" ) await self.global_lock.lock(float(result.get("retry_after"))) continue else: # 429's are unfortunately unavoidable, but we can attempt to avoid them # so long as these are infrequent we're doing well log.warning( f"{route.endpoint} Has exceeded it's ratelimit ({lock.limit})! Reset in {lock.delta} seconds" ) await lock.defer_unlock() # lock this route and wait for unlock continue elif lock.remaining == 0: # Last call available in the bucket, lock until reset log.debug( f"{route.endpoint} Has exhausted its ratelimit ({lock.limit})! Locking route for {lock.delta} seconds" ) await lock.blind_defer_unlock() # lock this route, but continue processing the current response elif response.status in {500, 502, 504}: # Server issues, retry log.warning( f"{route.endpoint} Received {response.status}... retrying in {1 + attempt * 2} seconds" ) await asyncio.sleep(1 + attempt * 2) continue if not 300 > response.status >= 200: await self._raise_exception(response, route, result) log.debug( f"{route.endpoint} Received {response.status} :: [{lock.remaining}/{lock.limit} calls remaining]" ) return result except OSError as e: if attempt < self._max_attempts - 1 and e.errno in (54, 10054): await asyncio.sleep(1 + attempt * 2) continue raise async def _raise_exception(self, response, route, result): log.error(f"{route.method}::{route.url}: {response.status}") if response.status == 403: raise Forbidden(response, response_data=result, route=route) elif response.status == 404: raise NotFound(response, response_data=result, route=route) elif response.status >= 500: raise DiscordError(response, response_data=result, route=route) else: raise HTTPException(response, response_data=result, route=route) async def request_cdn(self, url, asset) -> bytes: log.debug(f"{asset} requests {url} from CDN") async with self.__session.get(url) as response: if response.status == 200: return await response.read() await self._raise_exception(response, asset, await response_decode(response)) async def login(self, token: str) -> dict: """ "Login" to the gateway, basically validates the token and grabs user data. parameters: token: the token to use returns: The currently logged in bot's data """ self.__session = ClientSession(connector=self.connector) self.token = token try: return await self.request(Route("GET", "/users/@me")) except HTTPException as e: if e.status == 401: raise LoginError("An improper token was passed") from e raise async def close(self) -> None: """Close the session.""" if self.__session: await self.__session.close() async def get_gateway(self) -> str: """Get the gateway url.""" try: data: dict = await self.request(Route("GET", "/gateway")) except HTTPException as exc: raise GatewayNotFound from exc return "{0}?encoding={1}&v=9&compress=zlib-stream".format(data["url"], "json") async def websocket_connect(self, url: str) -> ClientWebSocketResponse: """ Connect to the websocket. parameters: url: the url to connect to """ return await self.__session.ws_connect( url, timeout=30, max_msg_size=0, autoclose=False, headers={"User-Agent": self.user_agent}, compress=0 )
[((1138, 1168), 'logging.getLogger', 'logging.getLogger', (['logger_name'], {}), '(logger_name)\n', (1155, 1168), False, 'import logging\n'), ((1308, 1329), 'dis_snek.models.CooldownSystem', 'CooldownSystem', (['(45)', '(1)'], {}), '(45, 1)\n', (1322, 1329), False, 'from dis_snek.models import CooldownSystem\n'), ((1451, 1465), 'asyncio.Lock', 'asyncio.Lock', ([], {}), '()\n', (1463, 1465), False, 'import asyncio\n'), ((2104, 2118), 'asyncio.Lock', 'asyncio.Lock', ([], {}), '()\n', (2116, 2118), False, 'import asyncio\n'), ((3295, 3321), 'asyncio.get_running_loop', 'asyncio.get_running_loop', ([], {}), '()\n', (3319, 3321), False, 'import asyncio\n'), ((4689, 4710), 'weakref.WeakValueDictionary', 'WeakValueDictionary', ([], {}), '()\n', (4708, 4710), False, 'from weakref import WeakValueDictionary\n'), ((12567, 12606), 'aiohttp.ClientSession', 'ClientSession', ([], {'connector': 'self.connector'}), '(connector=self.connector)\n', (12580, 12606), False, 'from aiohttp import BaseConnector, ClientSession, ClientWebSocketResponse, FormData\n'), ((1918, 1938), 'asyncio.sleep', 'asyncio.sleep', (['delta'], {}), '(delta)\n', (1931, 1938), False, 'import asyncio\n'), ((3526, 3551), 'asyncio.sleep', 'asyncio.sleep', (['self.delta'], {}), '(self.delta)\n', (3539, 3551), False, 'import asyncio\n'), ((4373, 4397), 'asyncio.get_event_loop', 'asyncio.get_event_loop', ([], {}), '()\n', (4395, 4397), False, 'import asyncio\n'), ((7324, 7352), 'urllib.parse.quote', '_uriquote', (['reason'], {'safe': '"""/ """'}), "(reason, safe='/ ')\n", (7333, 7352), True, 'from urllib.parse import quote as _uriquote\n'), ((11558, 11612), 'dis_snek.client.errors.Forbidden', 'Forbidden', (['response'], {'response_data': 'result', 'route': 'route'}), '(response, response_data=result, route=route)\n', (11567, 11612), False, 'from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError\n'), ((7695, 7720), 'dis_snek.client.utils.serializer.dict_filter_missing', 'dict_filter_missing', (['data'], {}), '(data)\n', (7714, 7720), False, 'from dis_snek.client.utils.serializer import dict_filter_missing\n'), ((11668, 11721), 'dis_snek.client.errors.NotFound', 'NotFound', (['response'], {'response_data': 'result', 'route': 'route'}), '(response, response_data=result, route=route)\n', (11676, 11721), False, 'from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError\n'), ((7561, 7583), 'dis_snek.client.utils.serializer.dict_filter_missing', 'dict_filter_missing', (['x'], {}), '(x)\n', (7580, 7583), False, 'from dis_snek.client.utils.serializer import dict_filter_missing\n'), ((11777, 11834), 'dis_snek.client.errors.DiscordError', 'DiscordError', (['response'], {'response_data': 'result', 'route': 'route'}), '(response, response_data=result, route=route)\n', (11789, 11834), False, 'from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError\n'), ((11867, 11925), 'dis_snek.client.errors.HTTPException', 'HTTPException', (['response'], {'response_data': 'result', 'route': 'route'}), '(response, response_data=result, route=route)\n', (11880, 11925), False, 'from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError\n'), ((12802, 12844), 'dis_snek.client.errors.LoginError', 'LoginError', (['"""An improper token was passed"""'], {}), "('An improper token was passed')\n", (12812, 12844), False, 'from dis_snek.client.errors import DiscordError, Forbidden, GatewayNotFound, HTTPException, NotFound, LoginError\n'), ((12238, 12263), 'dis_snek.client.utils.input_utils.response_decode', 'response_decode', (['response'], {}), '(response)\n', (12253, 12263), False, 'from dis_snek.client.utils.input_utils import response_decode\n'), ((8553, 8578), 'dis_snek.client.utils.input_utils.response_decode', 'response_decode', (['response'], {}), '(response)\n', (8568, 8578), False, 'from dis_snek.client.utils.input_utils import response_decode\n'), ((11281, 11311), 'asyncio.sleep', 'asyncio.sleep', (['(1 + attempt * 2)'], {}), '(1 + attempt * 2)\n', (11294, 11311), False, 'import asyncio\n'), ((10693, 10723), 'asyncio.sleep', 'asyncio.sleep', (['(1 + attempt * 2)'], {}), '(1 + attempt * 2)\n', (10706, 10723), False, 'import asyncio\n')]
conradsuuna/uac-computer-competency
config.py
40f8b165e5432ca22ab97838f424e26650a3d300
from os import environ import psycopg2 from datetime import timedelta from dotenv import load_dotenv load_dotenv() class Config(object): """ app configuration class """ TESTING = False CSRF_ENABLED = True SECRET_KEY = environ.get('SECRET_KEY') USER = environ.get('DB_USER') PASSWORD = environ.get('DB_PASSWORD') DB_NAME = environ.get('DB_NAME') HOST = environ.get('DB_HOST') SQLALCHEMY_DATABASE_URI = f"postgresql://{USER}:{PASSWORD}@{HOST}/{DB_NAME}" SQLALCHEMY_TRACK_MODIFICATIONS = False # jwt configuarations for the user auth api JWT_SECRET_KEY = environ.get('SECRET_KEY') JWT_ACCESS_TOKEN_EXPIRES = timedelta(days=1) # pagination NUM_OF_ITEMS_PER_PAGE = 18 class DevelopmentConfig(Config): """ app development configuration class """ ENV = "development" DEBUG = True SQLALCHEMY_TRACK_MODIFICATIONS = True class ProductionConfig(Config): DEBUG = False SQLALCHEMY_TRACK_MODIFICATIONS = False
[((101, 114), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (112, 114), False, 'from dotenv import load_dotenv\n'), ((235, 260), 'os.environ.get', 'environ.get', (['"""SECRET_KEY"""'], {}), "('SECRET_KEY')\n", (246, 260), False, 'from os import environ\n'), ((273, 295), 'os.environ.get', 'environ.get', (['"""DB_USER"""'], {}), "('DB_USER')\n", (284, 295), False, 'from os import environ\n'), ((311, 337), 'os.environ.get', 'environ.get', (['"""DB_PASSWORD"""'], {}), "('DB_PASSWORD')\n", (322, 337), False, 'from os import environ\n'), ((352, 374), 'os.environ.get', 'environ.get', (['"""DB_NAME"""'], {}), "('DB_NAME')\n", (363, 374), False, 'from os import environ\n'), ((386, 408), 'os.environ.get', 'environ.get', (['"""DB_HOST"""'], {}), "('DB_HOST')\n", (397, 408), False, 'from os import environ\n'), ((603, 628), 'os.environ.get', 'environ.get', (['"""SECRET_KEY"""'], {}), "('SECRET_KEY')\n", (614, 628), False, 'from os import environ\n'), ((660, 677), 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), '(days=1)\n', (669, 677), False, 'from datetime import timedelta\n')]
c4pt000/electrum-radiocoin
electrum/version.py
7cb5f618a9aa8cd03d60191624a0e57cc24646d2
ELECTRUM_VERSION = '4.1.5-radc' # version of the client package APK_VERSION = '4.1.5.0' # read by buildozer.spec PROTOCOL_VERSION = '1.4' # protocol version requested # The hash of the mnemonic seed must begin with this SEED_PREFIX = '01' # Standard wallet SEED_PREFIX_SW = '100' # Segwit wallet SEED_PREFIX_2FA = '101' # Two-factor authentication SEED_PREFIX_2FA_SW = '102' # Two-factor auth, using segwit def seed_prefix(seed_type): if seed_type == 'standard': return SEED_PREFIX elif seed_type == 'segwit': return SEED_PREFIX_SW elif seed_type == '2fa': return SEED_PREFIX_2FA elif seed_type == '2fa_segwit': return SEED_PREFIX_2FA_SW raise Exception(f"unknown seed_type: {seed_type}")
[]
arleyzhang/object-detection-pytorch
lib/layers/functions/prior_box.py
de96a507e6643a7019b94d92f77219439ccca29f
from __future__ import division from math import sqrt as sqrt from itertools import product as product import torch import numpy as np import cv2 from lib.utils.visualize_utils import TBWriter def vis(func): """tensorboard visualization if has writer as input""" def wrapper(*args, **kw): return func(*args, **kw) if kw['tb_writer'] is not None else None return wrapper class PriorBoxBase(object): """Compute priorbox coordinates in center-offset form for each source feature map. """ def __init__(self, cfg): super(PriorBoxBase, self).__init__() self.image_size = cfg.MODEL.IMAGE_SIZE self._steps = cfg.MODEL.STEPS self._cfg_list = [] self._prior_cfg = {} self._clip = cfg.MODEL.CLIP self._variance = cfg.MODEL.VARIANCE for v in self._variance: if v <= 0: raise ValueError('Variances must be greater than 0') def _setup(self, cfg): num_feat = len(self._steps) for item in self._cfg_list: if item not in cfg.MODEL: raise Exception("wrong anchor config!") if len(cfg.MODEL[item]) != num_feat and len(cfg.MODEL[item]) != 0: raise Exception("config {} length does not match step length!".format(item)) self._prior_cfg[item] = cfg.MODEL[item] @property def num_priors(self): """allow prior num calculation before knowing feature map size""" assert self._prior_cfg is not {} return [int(len(self._create_prior(0, 0, k)) / 4) for k in range(len(self._steps))] def _create_prior(self, cx, cy, k): raise NotImplementedError @vis def _image_proc(self, image=None, tb_writer=None): # TODO test with image if isinstance(image, type(None)): image = np.ones((self.image_size[1], self.image_size[0], 3)) elif isinstance(image, str): image = cv2.imread(image, -1) image = cv2.resize(image, (self.image_size[1], self.image_size[0])) return image @vis def _prior_vis(self, anchor, image_ori, feat_idx, tb_writer=None): # TODO add output path to the signature writer = tb_writer.writer prior_num = self.num_priors[feat_idx] # transform coordinates scale = [self.image_size[1], self.image_size[0], self.image_size[1], self.image_size[0]] bboxs = np.array(anchor).reshape((-1, 4)) box_centers = bboxs[:, :2] * scale[:2] # [x, y] # bboxs: [xmin, ymin, xmax, ymax] bboxs = np.hstack((bboxs[:, :2] - bboxs[:, 2:4] / 2, bboxs[:, :2] + bboxs[:, 2:4] / 2)) * scale box_centers = box_centers.astype(np.int32) bboxs = bboxs.astype(np.int32) # visualize each anchor box on a feature map for prior_idx in range(prior_num): image = image_ori.copy() bboxs_ = bboxs[prior_idx::prior_num, :] box_centers_ = box_centers[4 * prior_idx::prior_num, :] for archor, bbox in zip(box_centers_, bboxs_): cv2.circle(image, (archor[0], archor[1]), 1, (0, 0, 255), -1) if archor[0] == archor[1]: # only show diagnal anchors cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 1) image = image[..., ::-1] image = image.transpose((2,0,1)) writer.add_image('base/feature_map_{}_{}'.format(feat_idx, prior_idx), image, 2) def forward(self, layer_dims, tb_writer=None, image=None): priors = [] image = self._image_proc(image=image, tb_writer=tb_writer) for k in range(len(layer_dims)): prior = [] for i, j in product(range(layer_dims[k][0]), range(layer_dims[k][1])): steps_x = self.image_size[1] / self._steps[k] steps_y = self.image_size[0] / self._steps[k] cx = (j + 0.5) / steps_x # unit center x,y cy = (i + 0.5) / steps_y prior += self._create_prior(cx, cy, k) priors += prior self._prior_vis(prior, image, k, tb_writer=tb_writer) output = torch.Tensor(priors).view(-1, 4) # TODO this clip is meanless, should clip on [xmin, ymin, xmax, ymax] if self._clip: output.clamp_(max=1, min=0) return output class PriorBoxSSD(PriorBoxBase): def __init__(self, cfg): super(PriorBoxSSD, self).__init__(cfg) # self.image_size = cfg['image_size'] self._cfg_list = ['MIN_SIZES', 'MAX_SIZES', 'ASPECT_RATIOS'] self._flip = cfg.MODEL.FLIP self._setup(cfg) def _create_prior(self, cx, cy, k): # as the original paper do prior = [] min_sizes = self._prior_cfg['MIN_SIZES'][k] min_sizes = [min_sizes] if not isinstance(min_sizes, list) else min_sizes for ms in min_sizes: # min square s_i = ms / self.image_size[0] s_j = ms / self.image_size[1] prior += [cx, cy, s_j, s_i] # min max square if len(self._prior_cfg['MAX_SIZES']) != 0: assert type(self._prior_cfg['MAX_SIZES'][k]) is not list # one max size per layer s_i_prime = sqrt(s_i * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[0])) s_j_prime = sqrt(s_j * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[1])) prior += [cx, cy, s_j_prime, s_i_prime] # rectangles by min and aspect ratio for ar in self._prior_cfg['ASPECT_RATIOS'][k]: prior += [cx, cy, s_j * sqrt(ar), s_i / sqrt(ar)] # a vertical box if self._flip: prior += [cx, cy, s_j / sqrt(ar), s_i * sqrt(ar)] return prior # PriorBox = PriorBoxSSD def test_no_vis(cfg, tb_writer): cfg = copy.deepcopy(cfg) cfg['feature_maps'] = [38, 19, 10, 5, 3, 1] cfg['min_sizes'] = [[30], [60], 111, 162, 213, 264] cfg['flip'] = True feat_dim = [list(a) for a in zip(cfg['feature_maps'], cfg['feature_maps'])] p = PriorBoxSSD(cfg) print(p.num_priors) p1 = p.forward(feat_dim) print(p1) def test_filp(cfg, tb_writer): cfg = copy.deepcopy(cfg) cfg['feature_maps'] = [38, 19, 10, 5, 3, 1] cfg['flip'] = True feat_dim = [list(a) for a in zip(cfg['feature_maps'], cfg['feature_maps'])] p = PriorBoxSSD(cfg) p1 = p.forward(feat_dim, tb_writer=tb_writer) cfg['flip'] = False cfg['aspect_ratios'] = [[2, 1 / 2], [2, 1 / 2, 3, 1 / 3], [2, 1 / 2, 3, 1 / 3], [2, 1 / 2, 3, 1 / 3], [2, 1 / 2], [2, 1 / 2]] p = PriorBox(cfg) p2 = p.forward(feat_dim, tb_writer=tb_writer) # print(p2) assert (p2 - p1).sum() < 1e-8 def test_rectangle(cfg, tb_writer): cfg = copy.deepcopy(cfg) cfg['feature_maps'] = [38, 19, 10, 5, 3, 1] cfg['min_sizes'] = [30, 60, 111, 162, 213, 264] cfg['flip'] = True # feat_dim = [list(a) for a in zip(cfg['feature_maps'], cfg['feature_maps'])] # cfg['image_size'] = [300, 300] # feat_dim = [list(a) for a in zip(cfg['feature_maps'], [item * 2 for item in cfg['feature_maps']])] # cfg['image_size'] = [300, 600] feat_dim = [list(a) for a in zip([item * 2 for item in cfg['feature_maps']], cfg['feature_maps'])] cfg['image_size'] = [600, 300] p = PriorBoxSSD(cfg) p1 = p.forward(feat_dim, tb_writer=tb_writer) print(p1.shape) if __name__ == '__main__': import copy # from lib.datasets.config import ssd_voc_vgg as cfg # from lib.utils.visualize_utils import TBWriter # tb_writer = TBWriter(log_dir, {'epoch': 50}) # # test_no_vis(cfg, tb_writer) # test_filp(cfg, tb_writer) # test_rectangle(cfg, tb_writer) print('haha') from lib.utils.config import cfg print(cfg)
[((5886, 5904), 'copy.deepcopy', 'copy.deepcopy', (['cfg'], {}), '(cfg)\n', (5899, 5904), False, 'import copy\n'), ((6247, 6265), 'copy.deepcopy', 'copy.deepcopy', (['cfg'], {}), '(cfg)\n', (6260, 6265), False, 'import copy\n'), ((6845, 6863), 'copy.deepcopy', 'copy.deepcopy', (['cfg'], {}), '(cfg)\n', (6858, 6863), False, 'import copy\n'), ((1993, 2052), 'cv2.resize', 'cv2.resize', (['image', '(self.image_size[1], self.image_size[0])'], {}), '(image, (self.image_size[1], self.image_size[0]))\n', (2003, 2052), False, 'import cv2\n'), ((1845, 1897), 'numpy.ones', 'np.ones', (['(self.image_size[1], self.image_size[0], 3)'], {}), '((self.image_size[1], self.image_size[0], 3))\n', (1852, 1897), True, 'import numpy as np\n'), ((2578, 2657), 'numpy.hstack', 'np.hstack', (['(bboxs[:, :2] - bboxs[:, 2:4] / 2, bboxs[:, :2] + bboxs[:, 2:4] / 2)'], {}), '((bboxs[:, :2] - bboxs[:, 2:4] / 2, bboxs[:, :2] + bboxs[:, 2:4] / 2))\n', (2587, 2657), True, 'import numpy as np\n'), ((1955, 1976), 'cv2.imread', 'cv2.imread', (['image', '(-1)'], {}), '(image, -1)\n', (1965, 1976), False, 'import cv2\n'), ((2429, 2445), 'numpy.array', 'np.array', (['anchor'], {}), '(anchor)\n', (2437, 2445), True, 'import numpy as np\n'), ((3084, 3145), 'cv2.circle', 'cv2.circle', (['image', '(archor[0], archor[1])', '(1)', '(0, 0, 255)', '(-1)'], {}), '(image, (archor[0], archor[1]), 1, (0, 0, 255), -1)\n', (3094, 3145), False, 'import cv2\n'), ((4181, 4201), 'torch.Tensor', 'torch.Tensor', (['priors'], {}), '(priors)\n', (4193, 4201), False, 'import torch\n'), ((5282, 5348), 'math.sqrt', 'sqrt', (["(s_i * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[0]))"], {}), "(s_i * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[0]))\n", (5286, 5348), True, 'from math import sqrt as sqrt\n'), ((5377, 5443), 'math.sqrt', 'sqrt', (["(s_j * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[1]))"], {}), "(s_j * (self._prior_cfg['MAX_SIZES'][k] / self.image_size[1]))\n", (5381, 5443), True, 'from math import sqrt as sqrt\n'), ((3238, 3314), 'cv2.rectangle', 'cv2.rectangle', (['image', '(bbox[0], bbox[1])', '(bbox[2], bbox[3])', '(0, 255, 0)', '(1)'], {}), '(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 1)\n', (3251, 3314), False, 'import cv2\n'), ((5648, 5656), 'math.sqrt', 'sqrt', (['ar'], {}), '(ar)\n', (5652, 5656), True, 'from math import sqrt as sqrt\n'), ((5664, 5672), 'math.sqrt', 'sqrt', (['ar'], {}), '(ar)\n', (5668, 5672), True, 'from math import sqrt as sqrt\n'), ((5767, 5775), 'math.sqrt', 'sqrt', (['ar'], {}), '(ar)\n', (5771, 5775), True, 'from math import sqrt as sqrt\n'), ((5783, 5791), 'math.sqrt', 'sqrt', (['ar'], {}), '(ar)\n', (5787, 5791), True, 'from math import sqrt as sqrt\n')]
cwmartin/gaffer
python/Gaffer/SequencePath.py
1f8a0f75522105c9d5efefac6d55cb61c1038909
########################################################################## # # Copyright (c) 2012-2013, Image Engine Design Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above # copyright notice, this list of conditions and the following # disclaimer. # # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided with # the distribution. # # * Neither the name of John Haddon nor the names of # any other contributors to this software may be used to endorse or # promote products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ########################################################################## import IECore import Gaffer class SequencePath( Gaffer.Path ) : def __init__( self, path, root="/", minSequenceSize=1, filter=None ) : if not isinstance( path, Gaffer.Path ) : path = Gaffer.FileSystemPath( path, root ) Gaffer.Path.__init__( self, path[:], path.root(), filter=filter ) # we use the seed for creating base paths whenever we need them self.__basePathSeed = path self.__minSequenceSize = minSequenceSize def isValid( self ) : for p in self.__basePaths() : if not p.isValid() : return False return True def isLeaf( self ) : for p in self.__basePaths() : if not p.isLeaf() : return False return True def info( self ) : result = Gaffer.Path.info( self ) if result is None : return None def average( values ) : return sum( values ) / len( values ) def mostCommon( values ) : counter = {} for value in values : if value in counter : counter[value] += 1 else : counter[value] = 1 maxCount = 0 mostCommonValue = None for value, count in counter.items() : if count > maxCount : mostCommonValue = value maxCount = count return mostCommonValue combiners = { "fileSystem:owner" : mostCommon, "fileSystem:group" : mostCommon, "fileSystem:modificationTime" : max, "fileSystem:accessTime" : max, "fileSystem:size" : sum, } infos = [ path.info() for path in self.__basePaths() ] if len( infos ) : for key, exampleValue in infos[0].items() : if key in result : continue combiner = combiners.get( key, None ) if combiner is None : if isinstance( exampleValue, ( int, float ) ) : combiner = average elif isinstance( exampleValue, basestring ) : combiner = mostCommon if combiner is not None : values = [ i[key] for i in infos ] result[key] = combiner( values ) return result def _children( self ) : p = self.__basePath( self ) children = p.children() nonLeafPaths = [] leafPathStrings = [] for child in children : if child.isLeaf() : leafPathStrings.append( str( child ) ) else : nonLeafPaths.append( child ) sequences = IECore.findSequences( leafPathStrings, self.__minSequenceSize ) result = [] for path in sequences + nonLeafPaths : result.append( SequencePath( self.__basePath( str( path ) ), minSequenceSize=self.__minSequenceSize, filter = self.getFilter() ) ) return result def copy( self ) : result = SequencePath( self.__basePathSeed, minSequenceSize = self.__minSequenceSize, filter = self.getFilter() ) result.setFromPath( self ) return result def __basePath( self, path ) : result = self.__basePathSeed.copy() if isinstance( path, basestring ) : result.setFromString( path ) else : result.setFromPath( path ) return result def __basePaths( self ) : sequence = None with IECore.IgnoredExceptions( Exception ) : sequence = IECore.FileSequence( str( self ) ) result = [] if sequence : for f in sequence.fileNames() : result.append( self.__basePath( f ) ) else : result.append( self.__basePath( self ) ) return result def __isSequence( self ) : s = str( self ) if IECore.FileSequence.fileNameValidator().match( s ) : return True return False
[((2502, 2524), 'Gaffer.Path.info', 'Gaffer.Path.info', (['self'], {}), '(self)\n', (2518, 2524), False, 'import Gaffer\n'), ((3963, 4024), 'IECore.findSequences', 'IECore.findSequences', (['leafPathStrings', 'self.__minSequenceSize'], {}), '(leafPathStrings, self.__minSequenceSize)\n', (3983, 4024), False, 'import IECore\n'), ((2001, 2034), 'Gaffer.FileSystemPath', 'Gaffer.FileSystemPath', (['path', 'root'], {}), '(path, root)\n', (2022, 2034), False, 'import Gaffer\n'), ((4669, 4704), 'IECore.IgnoredExceptions', 'IECore.IgnoredExceptions', (['Exception'], {}), '(Exception)\n', (4693, 4704), False, 'import IECore\n'), ((4989, 5028), 'IECore.FileSequence.fileNameValidator', 'IECore.FileSequence.fileNameValidator', ([], {}), '()\n', (5026, 5028), False, 'import IECore\n')]
NREL/REopt_API
reo/migrations/0121_merge_20211001_1841.py
fbc70f3b0cdeec9ee220266d6b3b0c5d64f257a6
# Generated by Django 3.1.13 on 2021-10-01 18:41 from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('reo', '0117_financialmodel_generator_fuel_escalation_pct'), ('reo', '0120_auto_20210927_2046'), ('reo', '0121_auto_20211012_0305') ] operations = [ ]
[]
ckamtsikis/cmssw
PhysicsTools/Heppy/python/analyzers/objects/TauAnalyzer.py
ea19fe642bb7537cbf58451dcf73aa5fd1b66250
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle from PhysicsTools.Heppy.physicsobjects.Tau import Tau from PhysicsTools.HeppyCore.utils.deltar import deltaR, matchObjectCollection3 import PhysicsTools.HeppyCore.framework.config as cfg class TauAnalyzer( Analyzer ): def __init__(self, cfg_ana, cfg_comp, looperName ): super(TauAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName) #---------------------------------------- # DECLARATION OF HANDLES OF LEPTONS STUFF #---------------------------------------- def declareHandles(self): super(TauAnalyzer, self).declareHandles() self.handles['taus'] = AutoHandle( ('slimmedTaus',''),'std::vector<pat::Tau>') def beginLoop(self, setup): super(TauAnalyzer,self).beginLoop(setup) self.counters.addCounter('events') count = self.counters.counter('events') count.register('all events') count.register('has >=1 tau at preselection') count.register('has >=1 selected taus') count.register('has >=1 other taus') #------------------ # MAKE LEPTON LISTS #------------------ def makeTaus(self, event): event.inclusiveTaus = [] event.selectedTaus = [] event.otherTaus = [] #get all alltaus = map( Tau, self.handles['taus'].product() ) #make inclusive taus for tau in alltaus: tau.associatedVertex = event.goodVertices[0] if len(event.goodVertices)>0 else event.vertices[0] tau.lepVeto = False tau.idDecayMode = tau.tauID("decayModeFinding") tau.idDecayModeNewDMs = tau.tauID("decayModeFindingNewDMs") if hasattr(self.cfg_ana, 'inclusive_decayModeID') and self.cfg_ana.inclusive_decayModeID and not tau.tauID(self.cfg_ana.inclusive_decayModeID): continue tau.inclusive_lepVeto = False if self.cfg_ana.inclusive_vetoLeptons: for lep in event.selectedLeptons: if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.inclusive_leptonVetoDR: tau.inclusive_lepVeto = True if tau.inclusive_lepVeto: continue if self.cfg_ana.inclusive_vetoLeptonsPOG: if not tau.tauID(self.cfg_ana.inclusive_tauAntiMuonID): tau.inclusive_lepVeto = True if not tau.tauID(self.cfg_ana.inclusive_tauAntiElectronID): tau.inclusive_lepVeto = True if tau.inclusive_lepVeto: continue if tau.pt() < self.cfg_ana.inclusive_ptMin: continue if abs(tau.eta()) > self.cfg_ana.inclusive_etaMax: continue if abs(tau.dxy()) > self.cfg_ana.inclusive_dxyMax or abs(tau.dz()) > self.cfg_ana.inclusive_dzMax: continue def id3(tau,X): """Create an integer equal to 1-2-3 for (loose,medium,tight)""" return tau.tauID(X%"Loose") + tau.tauID(X%"Medium") + tau.tauID(X%"Tight") def id5(tau,X): """Create an integer equal to 1-2-3-4-5 for (very loose, loose, medium, tight, very tight)""" return id3(tau, X) + tau.tauID(X%"VLoose") + tau.tauID(X%"VTight") def id6(tau,X): """Create an integer equal to 1-2-3-4-5-6 for (very loose, loose, medium, tight, very tight, very very tight)""" return id5(tau, X) + tau.tauID(X%"VVTight") tau.idMVA = id6(tau, "by%sIsolationMVArun2v1DBoldDMwLT") tau.idMVANewDM = id6(tau, "by%sIsolationMVArun2v1DBnewDMwLT") tau.idCI3hit = id3(tau, "by%sCombinedIsolationDeltaBetaCorr3Hits") tau.idAntiMu = tau.tauID("againstMuonLoose3") + tau.tauID("againstMuonTight3") tau.idAntiE = id5(tau, "againstElectron%sMVA6") #print "Tau pt %5.1f: idMVA2 %d, idCI3hit %d, %s, %s" % (tau.pt(), tau.idMVA2, tau.idCI3hit, tau.tauID(self.cfg_ana.tauID), tau.tauID(self.cfg_ana.tauLooseID)) if tau.tauID(self.cfg_ana.inclusive_tauID): event.inclusiveTaus.append(tau) for tau in event.inclusiveTaus: tau.loose_lepVeto = False if self.cfg_ana.loose_vetoLeptons: for lep in event.selectedLeptons: if deltaR(lep.eta(), lep.phi(), tau.eta(), tau.phi()) < self.cfg_ana.loose_leptonVetoDR: tau.loose_lepVeto = True if self.cfg_ana.loose_vetoLeptonsPOG: if not tau.tauID(self.cfg_ana.loose_tauAntiMuonID): tau.loose_lepVeto = True if not tau.tauID(self.cfg_ana.loose_tauAntiElectronID): tau.loose_lepVeto = True if tau.tauID(self.cfg_ana.loose_decayModeID) and \ tau.pt() > self.cfg_ana.loose_ptMin and abs(tau.eta()) < self.cfg_ana.loose_etaMax and \ abs(tau.dxy()) < self.cfg_ana.loose_dxyMax and abs(tau.dz()) < self.cfg_ana.loose_dzMax and \ tau.tauID(self.cfg_ana.loose_tauID) and not tau.loose_lepVeto: event.selectedTaus.append(tau) else: event.otherTaus.append(tau) event.inclusiveTaus.sort(key = lambda l : l.pt(), reverse = True) event.selectedTaus.sort(key = lambda l : l.pt(), reverse = True) event.otherTaus.sort(key = lambda l : l.pt(), reverse = True) self.counters.counter('events').inc('all events') if len(event.inclusiveTaus): self.counters.counter('events').inc('has >=1 tau at preselection') if len(event.selectedTaus): self.counters.counter('events').inc('has >=1 selected taus') if len(event.otherTaus): self.counters.counter('events').inc('has >=1 other taus') def matchTaus(self, event): match = matchObjectCollection3(event.inclusiveTaus, event.gentaus, deltaRMax = 0.5) for lep in event.inclusiveTaus: gen = match[lep] lep.mcMatchId = 1 if gen else 0 lep.genp = gen def process(self, event): self.readCollections( event.input ) self.makeTaus(event) if not self.cfg_comp.isMC: return True if hasattr(event, 'gentaus'): self.matchTaus(event) return True # Find the definitions of the tau ID strings here: # http://cmslxr.fnal.gov/lxr/source/PhysicsTools/PatAlgos/python/producersLayer1/tauProducer_cfi.py setattr(TauAnalyzer,"defaultConfig",cfg.Analyzer( class_object = TauAnalyzer, # inclusive very loose hadronic tau selection inclusive_ptMin = 18, inclusive_etaMax = 9999, inclusive_dxyMax = 1000., inclusive_dzMax = 0.4, inclusive_vetoLeptons = False, inclusive_leptonVetoDR = 0.4, inclusive_decayModeID = "decayModeFindingNewDMs", # ignored if not set or "" inclusive_tauID = "decayModeFindingNewDMs", inclusive_vetoLeptonsPOG = False, # If True, the following two IDs are required inclusive_tauAntiMuonID = "", inclusive_tauAntiElectronID = "", # loose hadronic tau selection loose_ptMin = 18, loose_etaMax = 9999, loose_dxyMax = 1000., loose_dzMax = 0.2, loose_vetoLeptons = True, loose_leptonVetoDR = 0.4, loose_decayModeID = "decayModeFindingNewDMs", # ignored if not set or "" loose_tauID = "byLooseCombinedIsolationDeltaBetaCorr3Hits", loose_vetoLeptonsPOG = False, # If True, the following two IDs are required loose_tauAntiMuonID = "againstMuonLoose3", loose_tauAntiElectronID = "againstElectronLooseMVA5" ) )
[((6681, 7442), 'PhysicsTools.HeppyCore.framework.config.Analyzer', 'cfg.Analyzer', ([], {'class_object': 'TauAnalyzer', 'inclusive_ptMin': '(18)', 'inclusive_etaMax': '(9999)', 'inclusive_dxyMax': '(1000.0)', 'inclusive_dzMax': '(0.4)', 'inclusive_vetoLeptons': '(False)', 'inclusive_leptonVetoDR': '(0.4)', 'inclusive_decayModeID': '"""decayModeFindingNewDMs"""', 'inclusive_tauID': '"""decayModeFindingNewDMs"""', 'inclusive_vetoLeptonsPOG': '(False)', 'inclusive_tauAntiMuonID': '""""""', 'inclusive_tauAntiElectronID': '""""""', 'loose_ptMin': '(18)', 'loose_etaMax': '(9999)', 'loose_dxyMax': '(1000.0)', 'loose_dzMax': '(0.2)', 'loose_vetoLeptons': '(True)', 'loose_leptonVetoDR': '(0.4)', 'loose_decayModeID': '"""decayModeFindingNewDMs"""', 'loose_tauID': '"""byLooseCombinedIsolationDeltaBetaCorr3Hits"""', 'loose_vetoLeptonsPOG': '(False)', 'loose_tauAntiMuonID': '"""againstMuonLoose3"""', 'loose_tauAntiElectronID': '"""againstElectronLooseMVA5"""'}), "(class_object=TauAnalyzer, inclusive_ptMin=18, inclusive_etaMax\n =9999, inclusive_dxyMax=1000.0, inclusive_dzMax=0.4,\n inclusive_vetoLeptons=False, inclusive_leptonVetoDR=0.4,\n inclusive_decayModeID='decayModeFindingNewDMs', inclusive_tauID=\n 'decayModeFindingNewDMs', inclusive_vetoLeptonsPOG=False,\n inclusive_tauAntiMuonID='', inclusive_tauAntiElectronID='', loose_ptMin\n =18, loose_etaMax=9999, loose_dxyMax=1000.0, loose_dzMax=0.2,\n loose_vetoLeptons=True, loose_leptonVetoDR=0.4, loose_decayModeID=\n 'decayModeFindingNewDMs', loose_tauID=\n 'byLooseCombinedIsolationDeltaBetaCorr3Hits', loose_vetoLeptonsPOG=\n False, loose_tauAntiMuonID='againstMuonLoose3', loose_tauAntiElectronID\n ='againstElectronLooseMVA5')\n", (6693, 7442), True, 'import PhysicsTools.HeppyCore.framework.config as cfg\n'), ((740, 796), 'PhysicsTools.Heppy.analyzers.core.AutoHandle.AutoHandle', 'AutoHandle', (["('slimmedTaus', '')", '"""std::vector<pat::Tau>"""'], {}), "(('slimmedTaus', ''), 'std::vector<pat::Tau>')\n", (750, 796), False, 'from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle\n'), ((6009, 6082), 'PhysicsTools.HeppyCore.utils.deltar.matchObjectCollection3', 'matchObjectCollection3', (['event.inclusiveTaus', 'event.gentaus'], {'deltaRMax': '(0.5)'}), '(event.inclusiveTaus, event.gentaus, deltaRMax=0.5)\n', (6031, 6082), False, 'from PhysicsTools.HeppyCore.utils.deltar import deltaR, matchObjectCollection3\n')]
omuryorulmaz/kriptografi
vize/170401038.py
04c22e4f05f126b14f41842597a7b27065326670
# İsmail ALTAY 170401038 import math import random r = 3271 def egcd(a,b): if(a == 0): return(b,0,1) else: c,d,e = egcd(b % a, a) return(c, e - (b // a) * d, d) def modInvert(a,b): c,d,e = egcd(a,b) if c != 1: raise Exception('moduler ters bulunamadi') else: return d % b def randomInteger(n): return random.randrange(2 ** (n-1), 2 ** n) | 1 def RabinMiller(f): s = 5 if(f == 2): return 1 if not (f & 1): return 0 p = f-1 u = 0 r = f-1 while (r%2 == 0): r >>= 1 u+=1 def Control(a): z = pow(a, r, f) if z == 1: return 0 for i in range(u): z = pow(a, (2**i) * r, f-1) if z == p: return 0 return 1 for i in range(s): a = random.randrange(2, p-2) if Control(a): return 0 return 1 def Keygen(n): while True: p = randomInteger(n//2) if (p - 1) % r == 0 and RabinMiller(p) and math.gcd(r, int((p - 1) / r)) == 1: break while True: q = randomInteger(n//2) if RabinMiller(q) and math.gcd(r, int(q - 1)) == 1: break N = p * q phi = (p - 1) * (q - 1) while True: y = random.randrange(1, N) if math.gcd(y, N) == 1: x = pow(y, phi * modInvert(r, N) % N, N) if x != 1: break publicKeyFile = open("publickey.txt", "w+") publicKeyFile.write(str(N) + "\n" + str(y)) publicKeyFile.close() privateKeyFile = open("privatekey.txt", "w+") privateKeyFile.write(str(phi) + "\n" + str(x) + "\n" + str(N)) privateKeyFile.close() def encrypt(plaintext, publickeytxt): try: open(publickeytxt, "r") except FileNotFoundError: print("Anahtar çiftleri oluşturulmadan şifrelme işlemi yapılamaz. Lütfen önce Keygen fonksiyonunu çalıştırın.") else: publicKeyFile = open(publickeytxt, "r") N, y = publicKeyFile.read().split("\n") N = int(N) y = int(y) publicKeyFile.close() plainTextFile = open(plaintext, "r") plainCopy = int(plainTextFile.read().split("\n")[0]) plainTextFile.close() while True: u = random.randrange(1, int(N)) if math.gcd(y, N) == 1: break cipherText = pow(y, plainCopy, N) * pow(u, r, N) % N cipherTextFile = open("ciphertext.txt", "w+") cipherTextFile.write(str(cipherText)) cipherTextFile.close() def decrypt(ciphertext, privatekeytxt): try: open(privatekeytxt, "r") except FileNotFoundError: print("Anahtar çiftleri oluşturulmadan deşifreleme işlemi yapılamz. Lütfen önce Keygen fonksiyonunu çalıştırın.") else: privateKeyFile = open(privatekeytxt, "r") phi, x, N = privateKeyFile.read().split("\n") phi, x, N = int(phi), int(x), int(N) privateKeyFile.close() cipherTextFile = open(ciphertext, "r") cipherCopy = int(cipherTextFile.read()) a = pow(cipherCopy, (phi * modInvert(r, N)) % N, N) for i in range(r -1): if(pow(x, i, N) == a): break plainText2File = open("plaintext2.txt", "w+") plainText2File.write(str(i)) plainText2File.close() plain2File = open("plaintext2.txt", "r") plain1File = open("plaintext.txt", "r") plain1 = plain1File.read().split("\n")[0] plain2 = plain2File.read().split("\n")[0] if plain1 == plain2: print("Dosyalar Özdeştir..") else: print("Dosyalar özdeş değildir..") n = int(input("Oluşturulmak istenen anahtar çiftlerinin bit uzunluğunu girin: ")) Keygen(n) encrypt("plaintext.txt","publickey.txt") decrypt("ciphertext.txt", "privatekey.txt")
[((394, 432), 'random.randrange', 'random.randrange', (['(2 ** (n - 1))', '(2 ** n)'], {}), '(2 ** (n - 1), 2 ** n)\n', (410, 432), False, 'import random\n'), ((903, 929), 'random.randrange', 'random.randrange', (['(2)', '(p - 2)'], {}), '(2, p - 2)\n', (919, 929), False, 'import random\n'), ((1375, 1397), 'random.randrange', 'random.randrange', (['(1)', 'N'], {}), '(1, N)\n', (1391, 1397), False, 'import random\n'), ((1410, 1424), 'math.gcd', 'math.gcd', (['y', 'N'], {}), '(y, N)\n', (1418, 1424), False, 'import math\n'), ((2449, 2463), 'math.gcd', 'math.gcd', (['y', 'N'], {}), '(y, N)\n', (2457, 2463), False, 'import math\n')]
xapple/seqenv
seqenv/ontology.py
a898b936b64b51340f439b05fc8909f4ed826247
# Built-in modules # # Internal modules # from seqenv import module_dir from seqenv.common.cache import property_cached # Third party modules # import sh, networkx import matplotlib.colors # A list of envos to help test this module # test_envos = [ "ENVO:00000033", "ENVO:00000043", "ENVO:00000067", "ENVO:00000143", "ENVO:00000210", "ENVO:00000215", "ENVO:00000475", ] ################################################################################ class Ontology(object): """A object that gives you access to the graph (network with nodes and edges) of the ENVO ontology from the OBO file's path. Other libraries not used here that could be added: * graphviz: http://graphviz.readthedocs.org/en/latest/api.html#digraph * pydot: https://github.com/erocarrera/pydot """ def __init__(self, path=None): """Give the path to the OBO file""" if path is None: path = module_dir + 'data_envo/envo.obo' self.path = path # --------------------------- In this section --------------------------- # # orange_obo # goatools # orange_network # pygraphviz # networkx @property_cached def orange_obo(self): """The ontology loaded by the `orange` library. * http://orange.biolab.si * http://orange-bioinformatics.readthedocs.org/en/latest/ * https://github.com/biolab/orange-bio * https://bitbucket.org/biolab/orange-bioinformatics To install: $ pip install Orange-Bioinformatics """ from orangecontrib.bio.ontology import OBOOntology return OBOOntology(self.path) @property_cached def goatools(self): """The network loaded into goatools' format. * https://github.com/tanghaibao/goatools To install: $ pip install goatools """ from goatools import obo_parser return obo_parser.GODag(self.path) @property_cached def orange_network(self): """The network converted to `orange network` format. Doesn't seem to work until they update PyPI. * https://bitbucket.org/biolab/orange-network/ * http://orange-network.readthedocs.org/en/latest/ To install: $ pip install orange-network """ return self.orange_obo.to_network() @property_cached def pygraphviz(self): """The network converted to `pygraphviz` format. * http://pygraphviz.github.io/documentation/pygraphviz-1.3rc1/ To install: $ pip install pygraphviz """ g = self.orange_obo.to_graphviz() assert g.is_directed() assert g.is_strict() return g @property_cached def networkx(self): """The network converted to `networkx` format. Seems like it looses directionality. * https://networkx.readthedocs.org/en/stable/ To install: $ pip install networkx """ g = self.orange_obo.to_networkx() assert networkx.is_directed_acyclic_graph(g) return g # --------------------------- In this section --------------------------- # # test # get_subgraph # add_weights # draw_to_pdf # write_to_dot def get_subgraph(self, envos=None): """Given a list of ENVO terms, get the subgraph that contains them all and all their ancestors, up to the root. Outputs a networkx DiGraph object.""" # Testing mode # if envos is None: envos = test_envos # All nodes # nodes = set(n for e in envos for n in networkx.descendants(self.networkx, e)) nodes.update(envos) nodes = list(nodes) # Return # return self.networkx.subgraph(nodes) def add_weights(self, g, weights=None): """Input a networkx DiGraph object. Outputs a pygraphviz AGraph object.""" g = networkx.nx_agraph.to_agraph(g) if weights is None: return g for envo in weights: node = g.get_node(envo) weight = weights[envo] color = matplotlib.colors.rgb2hex((1.0, 1.0 - weight, 0.0)) node.attr['fillcolor'] = color return g def add_style(self, g): """Input a pygraphviz AGraph object. Outputs a pygraphviz AGraph object.""" for node in g.nodes(): text = node.attr['name'] node.attr['label'] = text.replace(' ','\\n') node.attr['name'] = '' node.attr['shape'] = 'Mrecord' node.attr['style'] = 'filled' # To add the envo id to each node, uncomment: #envo = node.attr['label'] #node.attr['label'] = "{<f0> %s|<f1> %s}" % (envo, text) for edge in g.edges(): if edge.attr['label'] == 'located_in': edge.attr['color'] = 'turquoise4' edge.attr['label'] = '' return g def write_to_dot(self, g, path): """Input a pygraphviz AGraph object.""" with open(path, 'w') as handle: handle.write(g.to_string()) def add_legend(self, path): """Input the path to a dot file.""" legend_txt = """ digraph { rankdir=LR node [shape=plaintext,fontname="helvetica"] subgraph cluster_01 { label = "NB: darker nodes weigh more"; key [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td align="right" port="i1">Is</td></tr> <tr><td align="right" port="i2">Part</td></tr> <tr><td align="right" port="i3">Located</td></tr> </table>>]; key2 [label=<<table border="0" cellpadding="2" cellspacing="0" cellborder="0"> <tr><td port="i1">a</td></tr> <tr><td port="i2">of</td></tr> <tr><td port="i3">in</td></tr> </table>>]; key:i1:e -> key2:i1:w [color=red]; key:i2:e -> key2:i2:w [color=blue]; key:i3:e -> key2:i3:w [color=turquoise4]; }""" orig_txt = [line.rstrip('\n') for line in open(path, 'r') if line] new_text = [line.lstrip() for line in legend_txt.split('\n') if line] new_text = '\n'.join(new_text + orig_txt[2:]) with open(path, 'w') as handle: handle.write(new_text) def draw_to_pdf(self, in_path, out_path): """Input a path to a dot file.""" sh.dot(in_path, '-Tpdf', '-o', out_path) # --------------------------- In this section --------------------------- # # descends def descends(self, e, root): """Does the envo term `e` descend from the node `root`? Returns True or False.""" # Auto conversion # if isinstance(e, int): e = "ENVO:%08d" % e if isinstance(root, int): root = "ENVO:%08d" % root # Return # return e in networkx.ancestors(self.networkx, root) # --------------------------- In this section --------------------------- # # print_test # draw_with_networkx # draw_with_pygraphviz def print_test(self, e=None): """Just a method to see a bit how the different libraries work.""" # Test node # if e is None: e = test_envos[0] # Goa # print "Goa: " print self.goatools[e] # Pygraphviz # print "pygraphviz: " print self.pygraphviz[e] print self.pygraphviz.successors(e) print self.pygraphviz.predecessors(e) print self.pygraphviz.get_node(e) # Networkx # import networkx print "networkx: " print self.networkx[e] print self.networkx.successors(e) print self.networkx.predecessors(e) print networkx.ancestors(self.networkx, e) # same as predecessors print networkx.descendants(self.networkx, e) # almost as child_to_parents def draw_with_networkx(self, g, path): """Input a networkx DiGraph object.""" from matplotlib import pyplot networkx.draw(g) pyplot.savefig(path) pyplot.close() def draw_with_pygraphviz(self, g, path): """Input a pygraphviz AGraph object.""" with open(path, 'w') as handle: handle.write(g.to_string())
[]
adewaleo/azure-sdk-for-python
sdk/iothub/azure-mgmt-iothub/azure/mgmt/iothub/v2019_11_04/models/_models.py
169457edbea5e3c5557246cfcf8bd635d528bae4
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for license information. # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is regenerated. # -------------------------------------------------------------------------- from azure.core.exceptions import HttpResponseError import msrest.serialization class CertificateBodyDescription(msrest.serialization.Model): """The JSON-serialized X509 Certificate. :param certificate: base-64 representation of the X509 leaf certificate .cer file or just .pem file content. :type certificate: str """ _attribute_map = { 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificateBodyDescription, self).__init__(**kwargs) self.certificate = kwargs.get('certificate', None) class CertificateDescription(msrest.serialization.Model): """The X509 Certificate. Variables are only populated by the server, and will be ignored when sending a request. :param properties: The description of an X509 CA Certificate. :type properties: ~azure.mgmt.iothub.v2019_11_04.models.CertificateProperties :ivar id: The resource identifier. :vartype id: str :ivar name: The name of the certificate. :vartype name: str :ivar etag: The entity tag. :vartype etag: str :ivar type: The resource type. :vartype type: str """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True}, 'etag': {'readonly': True}, 'type': {'readonly': True}, } _attribute_map = { 'properties': {'key': 'properties', 'type': 'CertificateProperties'}, 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'etag': {'key': 'etag', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificateDescription, self).__init__(**kwargs) self.properties = kwargs.get('properties', None) self.id = None self.name = None self.etag = None self.type = None class CertificateListDescription(msrest.serialization.Model): """The JSON-serialized array of Certificate objects. :param value: The array of Certificate objects. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.CertificateDescription] """ _attribute_map = { 'value': {'key': 'value', 'type': '[CertificateDescription]'}, } def __init__( self, **kwargs ): super(CertificateListDescription, self).__init__(**kwargs) self.value = kwargs.get('value', None) class CertificateProperties(msrest.serialization.Model): """The description of an X509 CA Certificate. Variables are only populated by the server, and will be ignored when sending a request. :ivar subject: The certificate's subject name. :vartype subject: str :ivar expiry: The certificate's expiration date and time. :vartype expiry: ~datetime.datetime :ivar thumbprint: The certificate's thumbprint. :vartype thumbprint: str :ivar is_verified: Determines whether certificate has been verified. :vartype is_verified: bool :ivar created: The certificate's create date and time. :vartype created: ~datetime.datetime :ivar updated: The certificate's last update date and time. :vartype updated: ~datetime.datetime :param certificate: The certificate content. :type certificate: str """ _validation = { 'subject': {'readonly': True}, 'expiry': {'readonly': True}, 'thumbprint': {'readonly': True}, 'is_verified': {'readonly': True}, 'created': {'readonly': True}, 'updated': {'readonly': True}, } _attribute_map = { 'subject': {'key': 'subject', 'type': 'str'}, 'expiry': {'key': 'expiry', 'type': 'rfc-1123'}, 'thumbprint': {'key': 'thumbprint', 'type': 'str'}, 'is_verified': {'key': 'isVerified', 'type': 'bool'}, 'created': {'key': 'created', 'type': 'rfc-1123'}, 'updated': {'key': 'updated', 'type': 'rfc-1123'}, 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificateProperties, self).__init__(**kwargs) self.subject = None self.expiry = None self.thumbprint = None self.is_verified = None self.created = None self.updated = None self.certificate = kwargs.get('certificate', None) class CertificatePropertiesWithNonce(msrest.serialization.Model): """The description of an X509 CA Certificate including the challenge nonce issued for the Proof-Of-Possession flow. Variables are only populated by the server, and will be ignored when sending a request. :ivar subject: The certificate's subject name. :vartype subject: str :ivar expiry: The certificate's expiration date and time. :vartype expiry: ~datetime.datetime :ivar thumbprint: The certificate's thumbprint. :vartype thumbprint: str :ivar is_verified: Determines whether certificate has been verified. :vartype is_verified: bool :ivar created: The certificate's create date and time. :vartype created: ~datetime.datetime :ivar updated: The certificate's last update date and time. :vartype updated: ~datetime.datetime :ivar verification_code: The certificate's verification code that will be used for proof of possession. :vartype verification_code: str :ivar certificate: The certificate content. :vartype certificate: str """ _validation = { 'subject': {'readonly': True}, 'expiry': {'readonly': True}, 'thumbprint': {'readonly': True}, 'is_verified': {'readonly': True}, 'created': {'readonly': True}, 'updated': {'readonly': True}, 'verification_code': {'readonly': True}, 'certificate': {'readonly': True}, } _attribute_map = { 'subject': {'key': 'subject', 'type': 'str'}, 'expiry': {'key': 'expiry', 'type': 'rfc-1123'}, 'thumbprint': {'key': 'thumbprint', 'type': 'str'}, 'is_verified': {'key': 'isVerified', 'type': 'bool'}, 'created': {'key': 'created', 'type': 'rfc-1123'}, 'updated': {'key': 'updated', 'type': 'rfc-1123'}, 'verification_code': {'key': 'verificationCode', 'type': 'str'}, 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificatePropertiesWithNonce, self).__init__(**kwargs) self.subject = None self.expiry = None self.thumbprint = None self.is_verified = None self.created = None self.updated = None self.verification_code = None self.certificate = None class CertificateVerificationDescription(msrest.serialization.Model): """The JSON-serialized leaf certificate. :param certificate: base-64 representation of X509 certificate .cer file or just .pem file content. :type certificate: str """ _attribute_map = { 'certificate': {'key': 'certificate', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificateVerificationDescription, self).__init__(**kwargs) self.certificate = kwargs.get('certificate', None) class CertificateWithNonceDescription(msrest.serialization.Model): """The X509 Certificate. Variables are only populated by the server, and will be ignored when sending a request. :param properties: The description of an X509 CA Certificate including the challenge nonce issued for the Proof-Of-Possession flow. :type properties: ~azure.mgmt.iothub.v2019_11_04.models.CertificatePropertiesWithNonce :ivar id: The resource identifier. :vartype id: str :ivar name: The name of the certificate. :vartype name: str :ivar etag: The entity tag. :vartype etag: str :ivar type: The resource type. :vartype type: str """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True}, 'etag': {'readonly': True}, 'type': {'readonly': True}, } _attribute_map = { 'properties': {'key': 'properties', 'type': 'CertificatePropertiesWithNonce'}, 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'etag': {'key': 'etag', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, } def __init__( self, **kwargs ): super(CertificateWithNonceDescription, self).__init__(**kwargs) self.properties = kwargs.get('properties', None) self.id = None self.name = None self.etag = None self.type = None class CloudToDeviceProperties(msrest.serialization.Model): """The IoT hub cloud-to-device messaging properties. :param max_delivery_count: The max delivery count for cloud-to-device messages in the device queue. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud-to- device-messages. :type max_delivery_count: int :param default_ttl_as_iso8601: The default time to live for cloud-to-device messages in the device queue. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud- to-device-messages. :type default_ttl_as_iso8601: ~datetime.timedelta :param feedback: The properties of the feedback queue for cloud-to-device messages. :type feedback: ~azure.mgmt.iothub.v2019_11_04.models.FeedbackProperties """ _validation = { 'max_delivery_count': {'maximum': 100, 'minimum': 1}, } _attribute_map = { 'max_delivery_count': {'key': 'maxDeliveryCount', 'type': 'int'}, 'default_ttl_as_iso8601': {'key': 'defaultTtlAsIso8601', 'type': 'duration'}, 'feedback': {'key': 'feedback', 'type': 'FeedbackProperties'}, } def __init__( self, **kwargs ): super(CloudToDeviceProperties, self).__init__(**kwargs) self.max_delivery_count = kwargs.get('max_delivery_count', None) self.default_ttl_as_iso8601 = kwargs.get('default_ttl_as_iso8601', None) self.feedback = kwargs.get('feedback', None) class EndpointHealthData(msrest.serialization.Model): """The health data for an endpoint. :param endpoint_id: Id of the endpoint. :type endpoint_id: str :param health_status: Health statuses have following meanings. The 'healthy' status shows that the endpoint is accepting messages as expected. The 'unhealthy' status shows that the endpoint is not accepting messages as expected and IoT Hub is retrying to send data to this endpoint. The status of an unhealthy endpoint will be updated to healthy when IoT Hub has established an eventually consistent state of health. The 'dead' status shows that the endpoint is not accepting messages, after IoT Hub retried sending messages for the retrial period. See IoT Hub metrics to identify errors and monitor issues with endpoints. The 'unknown' status shows that the IoT Hub has not established a connection with the endpoint. No messages have been delivered to or rejected from this endpoint. Possible values include: "unknown", "healthy", "unhealthy", "dead". :type health_status: str or ~azure.mgmt.iothub.v2019_11_04.models.EndpointHealthStatus """ _attribute_map = { 'endpoint_id': {'key': 'endpointId', 'type': 'str'}, 'health_status': {'key': 'healthStatus', 'type': 'str'}, } def __init__( self, **kwargs ): super(EndpointHealthData, self).__init__(**kwargs) self.endpoint_id = kwargs.get('endpoint_id', None) self.health_status = kwargs.get('health_status', None) class EndpointHealthDataListResult(msrest.serialization.Model): """The JSON-serialized array of EndpointHealthData objects with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: JSON-serialized array of Endpoint health data. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.EndpointHealthData] :ivar next_link: Link to more results. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[EndpointHealthData]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(EndpointHealthDataListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class EnrichmentProperties(msrest.serialization.Model): """The properties of an enrichment that your IoT hub applies to messages delivered to endpoints. All required parameters must be populated in order to send to Azure. :param key: Required. The key or name for the enrichment property. :type key: str :param value: Required. The value for the enrichment property. :type value: str :param endpoint_names: Required. The list of endpoints for which the enrichment is applied to the message. :type endpoint_names: list[str] """ _validation = { 'key': {'required': True}, 'value': {'required': True}, 'endpoint_names': {'required': True, 'min_items': 1}, } _attribute_map = { 'key': {'key': 'key', 'type': 'str'}, 'value': {'key': 'value', 'type': 'str'}, 'endpoint_names': {'key': 'endpointNames', 'type': '[str]'}, } def __init__( self, **kwargs ): super(EnrichmentProperties, self).__init__(**kwargs) self.key = kwargs['key'] self.value = kwargs['value'] self.endpoint_names = kwargs['endpoint_names'] class ErrorDetails(msrest.serialization.Model): """Error details. Variables are only populated by the server, and will be ignored when sending a request. :ivar code: The error code. :vartype code: str :ivar http_status_code: The HTTP status code. :vartype http_status_code: str :ivar message: The error message. :vartype message: str :ivar details: The error details. :vartype details: str """ _validation = { 'code': {'readonly': True}, 'http_status_code': {'readonly': True}, 'message': {'readonly': True}, 'details': {'readonly': True}, } _attribute_map = { 'code': {'key': 'code', 'type': 'str'}, 'http_status_code': {'key': 'httpStatusCode', 'type': 'str'}, 'message': {'key': 'message', 'type': 'str'}, 'details': {'key': 'details', 'type': 'str'}, } def __init__( self, **kwargs ): super(ErrorDetails, self).__init__(**kwargs) self.code = None self.http_status_code = None self.message = None self.details = None class EventHubConsumerGroupInfo(msrest.serialization.Model): """The properties of the EventHubConsumerGroupInfo object. Variables are only populated by the server, and will be ignored when sending a request. :param properties: The tags. :type properties: dict[str, str] :ivar id: The Event Hub-compatible consumer group identifier. :vartype id: str :ivar name: The Event Hub-compatible consumer group name. :vartype name: str :ivar type: the resource type. :vartype type: str :ivar etag: The etag. :vartype etag: str """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True}, 'type': {'readonly': True}, 'etag': {'readonly': True}, } _attribute_map = { 'properties': {'key': 'properties', 'type': '{str}'}, 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, 'etag': {'key': 'etag', 'type': 'str'}, } def __init__( self, **kwargs ): super(EventHubConsumerGroupInfo, self).__init__(**kwargs) self.properties = kwargs.get('properties', None) self.id = None self.name = None self.type = None self.etag = None class EventHubConsumerGroupsListResult(msrest.serialization.Model): """The JSON-serialized array of Event Hub-compatible consumer group names with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: List of consumer groups objects. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.EventHubConsumerGroupInfo] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[EventHubConsumerGroupInfo]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(EventHubConsumerGroupsListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class EventHubProperties(msrest.serialization.Model): """The properties of the provisioned Event Hub-compatible endpoint used by the IoT hub. Variables are only populated by the server, and will be ignored when sending a request. :param retention_time_in_days: The retention time for device-to-cloud messages in days. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#device-to-cloud-messages. :type retention_time_in_days: long :param partition_count: The number of partitions for receiving device-to-cloud messages in the Event Hub-compatible endpoint. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide- messaging#device-to-cloud-messages. :type partition_count: int :ivar partition_ids: The partition ids in the Event Hub-compatible endpoint. :vartype partition_ids: list[str] :ivar path: The Event Hub-compatible name. :vartype path: str :ivar endpoint: The Event Hub-compatible endpoint. :vartype endpoint: str """ _validation = { 'partition_ids': {'readonly': True}, 'path': {'readonly': True}, 'endpoint': {'readonly': True}, } _attribute_map = { 'retention_time_in_days': {'key': 'retentionTimeInDays', 'type': 'long'}, 'partition_count': {'key': 'partitionCount', 'type': 'int'}, 'partition_ids': {'key': 'partitionIds', 'type': '[str]'}, 'path': {'key': 'path', 'type': 'str'}, 'endpoint': {'key': 'endpoint', 'type': 'str'}, } def __init__( self, **kwargs ): super(EventHubProperties, self).__init__(**kwargs) self.retention_time_in_days = kwargs.get('retention_time_in_days', None) self.partition_count = kwargs.get('partition_count', None) self.partition_ids = None self.path = None self.endpoint = None class ExportDevicesRequest(msrest.serialization.Model): """Use to provide parameters when requesting an export of all devices in the IoT hub. All required parameters must be populated in order to send to Azure. :param export_blob_container_uri: Required. The export blob container URI. :type export_blob_container_uri: str :param exclude_keys: Required. The value indicating whether keys should be excluded during export. :type exclude_keys: bool """ _validation = { 'export_blob_container_uri': {'required': True}, 'exclude_keys': {'required': True}, } _attribute_map = { 'export_blob_container_uri': {'key': 'exportBlobContainerUri', 'type': 'str'}, 'exclude_keys': {'key': 'excludeKeys', 'type': 'bool'}, } def __init__( self, **kwargs ): super(ExportDevicesRequest, self).__init__(**kwargs) self.export_blob_container_uri = kwargs['export_blob_container_uri'] self.exclude_keys = kwargs['exclude_keys'] class FailoverInput(msrest.serialization.Model): """Use to provide failover region when requesting manual Failover for a hub. All required parameters must be populated in order to send to Azure. :param failover_region: Required. Region the hub will be failed over to. :type failover_region: str """ _validation = { 'failover_region': {'required': True}, } _attribute_map = { 'failover_region': {'key': 'failoverRegion', 'type': 'str'}, } def __init__( self, **kwargs ): super(FailoverInput, self).__init__(**kwargs) self.failover_region = kwargs['failover_region'] class FallbackRouteProperties(msrest.serialization.Model): """The properties of the fallback route. IoT Hub uses these properties when it routes messages to the fallback endpoint. All required parameters must be populated in order to send to Azure. :param name: The name of the route. The name can only include alphanumeric characters, periods, underscores, hyphens, has a maximum length of 64 characters, and must be unique. :type name: str :param source: Required. The source to which the routing rule is to be applied to. For example, DeviceMessages. Possible values include: "Invalid", "DeviceMessages", "TwinChangeEvents", "DeviceLifecycleEvents", "DeviceJobLifecycleEvents". :type source: str or ~azure.mgmt.iothub.v2019_11_04.models.RoutingSource :param condition: The condition which is evaluated in order to apply the fallback route. If the condition is not provided it will evaluate to true by default. For grammar, See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-query-language. :type condition: str :param endpoint_names: Required. The list of endpoints to which the messages that satisfy the condition are routed to. Currently only 1 endpoint is allowed. :type endpoint_names: list[str] :param is_enabled: Required. Used to specify whether the fallback route is enabled. :type is_enabled: bool """ _validation = { 'source': {'required': True}, 'endpoint_names': {'required': True, 'max_items': 1, 'min_items': 1}, 'is_enabled': {'required': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, 'source': {'key': 'source', 'type': 'str'}, 'condition': {'key': 'condition', 'type': 'str'}, 'endpoint_names': {'key': 'endpointNames', 'type': '[str]'}, 'is_enabled': {'key': 'isEnabled', 'type': 'bool'}, } def __init__( self, **kwargs ): super(FallbackRouteProperties, self).__init__(**kwargs) self.name = kwargs.get('name', None) self.source = kwargs['source'] self.condition = kwargs.get('condition', None) self.endpoint_names = kwargs['endpoint_names'] self.is_enabled = kwargs['is_enabled'] class FeedbackProperties(msrest.serialization.Model): """The properties of the feedback queue for cloud-to-device messages. :param lock_duration_as_iso8601: The lock duration for the feedback queue. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud-to-device-messages. :type lock_duration_as_iso8601: ~datetime.timedelta :param ttl_as_iso8601: The period of time for which a message is available to consume before it is expired by the IoT hub. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide- messaging#cloud-to-device-messages. :type ttl_as_iso8601: ~datetime.timedelta :param max_delivery_count: The number of times the IoT hub attempts to deliver a message on the feedback queue. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging#cloud- to-device-messages. :type max_delivery_count: int """ _validation = { 'max_delivery_count': {'maximum': 100, 'minimum': 1}, } _attribute_map = { 'lock_duration_as_iso8601': {'key': 'lockDurationAsIso8601', 'type': 'duration'}, 'ttl_as_iso8601': {'key': 'ttlAsIso8601', 'type': 'duration'}, 'max_delivery_count': {'key': 'maxDeliveryCount', 'type': 'int'}, } def __init__( self, **kwargs ): super(FeedbackProperties, self).__init__(**kwargs) self.lock_duration_as_iso8601 = kwargs.get('lock_duration_as_iso8601', None) self.ttl_as_iso8601 = kwargs.get('ttl_as_iso8601', None) self.max_delivery_count = kwargs.get('max_delivery_count', None) class ImportDevicesRequest(msrest.serialization.Model): """Use to provide parameters when requesting an import of all devices in the hub. All required parameters must be populated in order to send to Azure. :param input_blob_container_uri: Required. The input blob container URI. :type input_blob_container_uri: str :param output_blob_container_uri: Required. The output blob container URI. :type output_blob_container_uri: str """ _validation = { 'input_blob_container_uri': {'required': True}, 'output_blob_container_uri': {'required': True}, } _attribute_map = { 'input_blob_container_uri': {'key': 'inputBlobContainerUri', 'type': 'str'}, 'output_blob_container_uri': {'key': 'outputBlobContainerUri', 'type': 'str'}, } def __init__( self, **kwargs ): super(ImportDevicesRequest, self).__init__(**kwargs) self.input_blob_container_uri = kwargs['input_blob_container_uri'] self.output_blob_container_uri = kwargs['output_blob_container_uri'] class IotHubCapacity(msrest.serialization.Model): """IoT Hub capacity information. Variables are only populated by the server, and will be ignored when sending a request. :ivar minimum: The minimum number of units. :vartype minimum: long :ivar maximum: The maximum number of units. :vartype maximum: long :ivar default: The default number of units. :vartype default: long :ivar scale_type: The type of the scaling enabled. Possible values include: "Automatic", "Manual", "None". :vartype scale_type: str or ~azure.mgmt.iothub.v2019_11_04.models.IotHubScaleType """ _validation = { 'minimum': {'readonly': True, 'maximum': 1, 'minimum': 1}, 'maximum': {'readonly': True}, 'default': {'readonly': True}, 'scale_type': {'readonly': True}, } _attribute_map = { 'minimum': {'key': 'minimum', 'type': 'long'}, 'maximum': {'key': 'maximum', 'type': 'long'}, 'default': {'key': 'default', 'type': 'long'}, 'scale_type': {'key': 'scaleType', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubCapacity, self).__init__(**kwargs) self.minimum = None self.maximum = None self.default = None self.scale_type = None class Resource(msrest.serialization.Model): """The common properties of an Azure resource. Variables are only populated by the server, and will be ignored when sending a request. All required parameters must be populated in order to send to Azure. :ivar id: The resource identifier. :vartype id: str :ivar name: The resource name. :vartype name: str :ivar type: The resource type. :vartype type: str :param location: Required. The resource location. :type location: str :param tags: A set of tags. The resource tags. :type tags: dict[str, str] """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True, 'pattern': r'^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{2,49}[a-zA-Z0-9]$'}, 'type': {'readonly': True}, 'location': {'required': True}, } _attribute_map = { 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, 'location': {'key': 'location', 'type': 'str'}, 'tags': {'key': 'tags', 'type': '{str}'}, } def __init__( self, **kwargs ): super(Resource, self).__init__(**kwargs) self.id = None self.name = None self.type = None self.location = kwargs['location'] self.tags = kwargs.get('tags', None) class IotHubDescription(Resource): """The description of the IoT hub. Variables are only populated by the server, and will be ignored when sending a request. All required parameters must be populated in order to send to Azure. :ivar id: The resource identifier. :vartype id: str :ivar name: The resource name. :vartype name: str :ivar type: The resource type. :vartype type: str :param location: Required. The resource location. :type location: str :param tags: A set of tags. The resource tags. :type tags: dict[str, str] :param etag: The Etag field is *not* required. If it is provided in the response body, it must also be provided as a header per the normal ETag convention. :type etag: str :param properties: IotHub properties. :type properties: ~azure.mgmt.iothub.v2019_11_04.models.IotHubProperties :param sku: Required. IotHub SKU info. :type sku: ~azure.mgmt.iothub.v2019_11_04.models.IotHubSkuInfo """ _validation = { 'id': {'readonly': True}, 'name': {'readonly': True, 'pattern': r'^(?![0-9]+$)(?!-)[a-zA-Z0-9-]{2,49}[a-zA-Z0-9]$'}, 'type': {'readonly': True}, 'location': {'required': True}, 'sku': {'required': True}, } _attribute_map = { 'id': {'key': 'id', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, 'location': {'key': 'location', 'type': 'str'}, 'tags': {'key': 'tags', 'type': '{str}'}, 'etag': {'key': 'etag', 'type': 'str'}, 'properties': {'key': 'properties', 'type': 'IotHubProperties'}, 'sku': {'key': 'sku', 'type': 'IotHubSkuInfo'}, } def __init__( self, **kwargs ): super(IotHubDescription, self).__init__(**kwargs) self.etag = kwargs.get('etag', None) self.properties = kwargs.get('properties', None) self.sku = kwargs['sku'] class IotHubDescriptionListResult(msrest.serialization.Model): """The JSON-serialized array of IotHubDescription objects with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: The array of IotHubDescription objects. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.IotHubDescription] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[IotHubDescription]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubDescriptionListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class IotHubLocationDescription(msrest.serialization.Model): """Public representation of one of the locations where a resource is provisioned. :param location: The name of the Azure region. :type location: str :param role: The role of the region, can be either primary or secondary. The primary region is where the IoT hub is currently provisioned. The secondary region is the Azure disaster recovery (DR) paired region and also the region where the IoT hub can failover to. Possible values include: "primary", "secondary". :type role: str or ~azure.mgmt.iothub.v2019_11_04.models.IotHubReplicaRoleType """ _attribute_map = { 'location': {'key': 'location', 'type': 'str'}, 'role': {'key': 'role', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubLocationDescription, self).__init__(**kwargs) self.location = kwargs.get('location', None) self.role = kwargs.get('role', None) class IotHubNameAvailabilityInfo(msrest.serialization.Model): """The properties indicating whether a given IoT hub name is available. Variables are only populated by the server, and will be ignored when sending a request. :ivar name_available: The value which indicates whether the provided name is available. :vartype name_available: bool :ivar reason: The reason for unavailability. Possible values include: "Invalid", "AlreadyExists". :vartype reason: str or ~azure.mgmt.iothub.v2019_11_04.models.IotHubNameUnavailabilityReason :param message: The detailed reason message. :type message: str """ _validation = { 'name_available': {'readonly': True}, 'reason': {'readonly': True}, } _attribute_map = { 'name_available': {'key': 'nameAvailable', 'type': 'bool'}, 'reason': {'key': 'reason', 'type': 'str'}, 'message': {'key': 'message', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubNameAvailabilityInfo, self).__init__(**kwargs) self.name_available = None self.reason = None self.message = kwargs.get('message', None) class IotHubProperties(msrest.serialization.Model): """The properties of an IoT hub. Variables are only populated by the server, and will be ignored when sending a request. :param authorization_policies: The shared access policies you can use to secure a connection to the IoT hub. :type authorization_policies: list[~azure.mgmt.iothub.v2019_11_04.models.SharedAccessSignatureAuthorizationRule] :param ip_filter_rules: The IP filter rules. :type ip_filter_rules: list[~azure.mgmt.iothub.v2019_11_04.models.IpFilterRule] :ivar provisioning_state: The provisioning state. :vartype provisioning_state: str :ivar state: The hub state. :vartype state: str :ivar host_name: The name of the host. :vartype host_name: str :param event_hub_endpoints: The Event Hub-compatible endpoint properties. The only possible keys to this dictionary is events. This key has to be present in the dictionary while making create or update calls for the IoT hub. :type event_hub_endpoints: dict[str, ~azure.mgmt.iothub.v2019_11_04.models.EventHubProperties] :param routing: The routing related properties of the IoT hub. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging. :type routing: ~azure.mgmt.iothub.v2019_11_04.models.RoutingProperties :param storage_endpoints: The list of Azure Storage endpoints where you can upload files. Currently you can configure only one Azure Storage account and that MUST have its key as $default. Specifying more than one storage account causes an error to be thrown. Not specifying a value for this property when the enableFileUploadNotifications property is set to True, causes an error to be thrown. :type storage_endpoints: dict[str, ~azure.mgmt.iothub.v2019_11_04.models.StorageEndpointProperties] :param messaging_endpoints: The messaging endpoint properties for the file upload notification queue. :type messaging_endpoints: dict[str, ~azure.mgmt.iothub.v2019_11_04.models.MessagingEndpointProperties] :param enable_file_upload_notifications: If True, file upload notifications are enabled. :type enable_file_upload_notifications: bool :param cloud_to_device: The IoT hub cloud-to-device messaging properties. :type cloud_to_device: ~azure.mgmt.iothub.v2019_11_04.models.CloudToDeviceProperties :param comments: IoT hub comments. :type comments: str :param features: The capabilities and features enabled for the IoT hub. Possible values include: "None", "DeviceManagement". :type features: str or ~azure.mgmt.iothub.v2019_11_04.models.Capabilities :ivar locations: Primary and secondary location for iot hub. :vartype locations: list[~azure.mgmt.iothub.v2019_11_04.models.IotHubLocationDescription] """ _validation = { 'provisioning_state': {'readonly': True}, 'state': {'readonly': True}, 'host_name': {'readonly': True}, 'locations': {'readonly': True}, } _attribute_map = { 'authorization_policies': {'key': 'authorizationPolicies', 'type': '[SharedAccessSignatureAuthorizationRule]'}, 'ip_filter_rules': {'key': 'ipFilterRules', 'type': '[IpFilterRule]'}, 'provisioning_state': {'key': 'provisioningState', 'type': 'str'}, 'state': {'key': 'state', 'type': 'str'}, 'host_name': {'key': 'hostName', 'type': 'str'}, 'event_hub_endpoints': {'key': 'eventHubEndpoints', 'type': '{EventHubProperties}'}, 'routing': {'key': 'routing', 'type': 'RoutingProperties'}, 'storage_endpoints': {'key': 'storageEndpoints', 'type': '{StorageEndpointProperties}'}, 'messaging_endpoints': {'key': 'messagingEndpoints', 'type': '{MessagingEndpointProperties}'}, 'enable_file_upload_notifications': {'key': 'enableFileUploadNotifications', 'type': 'bool'}, 'cloud_to_device': {'key': 'cloudToDevice', 'type': 'CloudToDeviceProperties'}, 'comments': {'key': 'comments', 'type': 'str'}, 'features': {'key': 'features', 'type': 'str'}, 'locations': {'key': 'locations', 'type': '[IotHubLocationDescription]'}, } def __init__( self, **kwargs ): super(IotHubProperties, self).__init__(**kwargs) self.authorization_policies = kwargs.get('authorization_policies', None) self.ip_filter_rules = kwargs.get('ip_filter_rules', None) self.provisioning_state = None self.state = None self.host_name = None self.event_hub_endpoints = kwargs.get('event_hub_endpoints', None) self.routing = kwargs.get('routing', None) self.storage_endpoints = kwargs.get('storage_endpoints', None) self.messaging_endpoints = kwargs.get('messaging_endpoints', None) self.enable_file_upload_notifications = kwargs.get('enable_file_upload_notifications', None) self.cloud_to_device = kwargs.get('cloud_to_device', None) self.comments = kwargs.get('comments', None) self.features = kwargs.get('features', None) self.locations = None class IotHubQuotaMetricInfo(msrest.serialization.Model): """Quota metrics properties. Variables are only populated by the server, and will be ignored when sending a request. :ivar name: The name of the quota metric. :vartype name: str :ivar current_value: The current value for the quota metric. :vartype current_value: long :ivar max_value: The maximum value of the quota metric. :vartype max_value: long """ _validation = { 'name': {'readonly': True}, 'current_value': {'readonly': True}, 'max_value': {'readonly': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, 'current_value': {'key': 'currentValue', 'type': 'long'}, 'max_value': {'key': 'maxValue', 'type': 'long'}, } def __init__( self, **kwargs ): super(IotHubQuotaMetricInfo, self).__init__(**kwargs) self.name = None self.current_value = None self.max_value = None class IotHubQuotaMetricInfoListResult(msrest.serialization.Model): """The JSON-serialized array of IotHubQuotaMetricInfo objects with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: The array of quota metrics objects. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.IotHubQuotaMetricInfo] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[IotHubQuotaMetricInfo]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubQuotaMetricInfoListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class IotHubSkuDescription(msrest.serialization.Model): """SKU properties. Variables are only populated by the server, and will be ignored when sending a request. All required parameters must be populated in order to send to Azure. :ivar resource_type: The type of the resource. :vartype resource_type: str :param sku: Required. The type of the resource. :type sku: ~azure.mgmt.iothub.v2019_11_04.models.IotHubSkuInfo :param capacity: Required. IotHub capacity. :type capacity: ~azure.mgmt.iothub.v2019_11_04.models.IotHubCapacity """ _validation = { 'resource_type': {'readonly': True}, 'sku': {'required': True}, 'capacity': {'required': True}, } _attribute_map = { 'resource_type': {'key': 'resourceType', 'type': 'str'}, 'sku': {'key': 'sku', 'type': 'IotHubSkuInfo'}, 'capacity': {'key': 'capacity', 'type': 'IotHubCapacity'}, } def __init__( self, **kwargs ): super(IotHubSkuDescription, self).__init__(**kwargs) self.resource_type = None self.sku = kwargs['sku'] self.capacity = kwargs['capacity'] class IotHubSkuDescriptionListResult(msrest.serialization.Model): """The JSON-serialized array of IotHubSkuDescription objects with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: The array of IotHubSkuDescription. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.IotHubSkuDescription] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[IotHubSkuDescription]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(IotHubSkuDescriptionListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class IotHubSkuInfo(msrest.serialization.Model): """Information about the SKU of the IoT hub. Variables are only populated by the server, and will be ignored when sending a request. All required parameters must be populated in order to send to Azure. :param name: Required. The name of the SKU. Possible values include: "F1", "S1", "S2", "S3", "B1", "B2", "B3". :type name: str or ~azure.mgmt.iothub.v2019_11_04.models.IotHubSku :ivar tier: The billing tier for the IoT hub. Possible values include: "Free", "Standard", "Basic". :vartype tier: str or ~azure.mgmt.iothub.v2019_11_04.models.IotHubSkuTier :param capacity: The number of provisioned IoT Hub units. See: https://docs.microsoft.com/azure/azure-subscription-service-limits#iot-hub-limits. :type capacity: long """ _validation = { 'name': {'required': True}, 'tier': {'readonly': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, 'tier': {'key': 'tier', 'type': 'str'}, 'capacity': {'key': 'capacity', 'type': 'long'}, } def __init__( self, **kwargs ): super(IotHubSkuInfo, self).__init__(**kwargs) self.name = kwargs['name'] self.tier = None self.capacity = kwargs.get('capacity', None) class IpFilterRule(msrest.serialization.Model): """The IP filter rules for the IoT hub. All required parameters must be populated in order to send to Azure. :param filter_name: Required. The name of the IP filter rule. :type filter_name: str :param action: Required. The desired action for requests captured by this rule. Possible values include: "Accept", "Reject". :type action: str or ~azure.mgmt.iothub.v2019_11_04.models.IpFilterActionType :param ip_mask: Required. A string that contains the IP address range in CIDR notation for the rule. :type ip_mask: str """ _validation = { 'filter_name': {'required': True}, 'action': {'required': True}, 'ip_mask': {'required': True}, } _attribute_map = { 'filter_name': {'key': 'filterName', 'type': 'str'}, 'action': {'key': 'action', 'type': 'str'}, 'ip_mask': {'key': 'ipMask', 'type': 'str'}, } def __init__( self, **kwargs ): super(IpFilterRule, self).__init__(**kwargs) self.filter_name = kwargs['filter_name'] self.action = kwargs['action'] self.ip_mask = kwargs['ip_mask'] class JobResponse(msrest.serialization.Model): """The properties of the Job Response object. Variables are only populated by the server, and will be ignored when sending a request. :ivar job_id: The job identifier. :vartype job_id: str :ivar start_time_utc: The start time of the job. :vartype start_time_utc: ~datetime.datetime :ivar end_time_utc: The time the job stopped processing. :vartype end_time_utc: ~datetime.datetime :ivar type: The type of the job. Possible values include: "unknown", "export", "import", "backup", "readDeviceProperties", "writeDeviceProperties", "updateDeviceConfiguration", "rebootDevice", "factoryResetDevice", "firmwareUpdate". :vartype type: str or ~azure.mgmt.iothub.v2019_11_04.models.JobType :ivar status: The status of the job. Possible values include: "unknown", "enqueued", "running", "completed", "failed", "cancelled". :vartype status: str or ~azure.mgmt.iothub.v2019_11_04.models.JobStatus :ivar failure_reason: If status == failed, this string containing the reason for the failure. :vartype failure_reason: str :ivar status_message: The status message for the job. :vartype status_message: str :ivar parent_job_id: The job identifier of the parent job, if any. :vartype parent_job_id: str """ _validation = { 'job_id': {'readonly': True}, 'start_time_utc': {'readonly': True}, 'end_time_utc': {'readonly': True}, 'type': {'readonly': True}, 'status': {'readonly': True}, 'failure_reason': {'readonly': True}, 'status_message': {'readonly': True}, 'parent_job_id': {'readonly': True}, } _attribute_map = { 'job_id': {'key': 'jobId', 'type': 'str'}, 'start_time_utc': {'key': 'startTimeUtc', 'type': 'rfc-1123'}, 'end_time_utc': {'key': 'endTimeUtc', 'type': 'rfc-1123'}, 'type': {'key': 'type', 'type': 'str'}, 'status': {'key': 'status', 'type': 'str'}, 'failure_reason': {'key': 'failureReason', 'type': 'str'}, 'status_message': {'key': 'statusMessage', 'type': 'str'}, 'parent_job_id': {'key': 'parentJobId', 'type': 'str'}, } def __init__( self, **kwargs ): super(JobResponse, self).__init__(**kwargs) self.job_id = None self.start_time_utc = None self.end_time_utc = None self.type = None self.status = None self.failure_reason = None self.status_message = None self.parent_job_id = None class JobResponseListResult(msrest.serialization.Model): """The JSON-serialized array of JobResponse objects with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: The array of JobResponse objects. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.JobResponse] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[JobResponse]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(JobResponseListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class MatchedRoute(msrest.serialization.Model): """Routes that matched. :param properties: Properties of routes that matched. :type properties: ~azure.mgmt.iothub.v2019_11_04.models.RouteProperties """ _attribute_map = { 'properties': {'key': 'properties', 'type': 'RouteProperties'}, } def __init__( self, **kwargs ): super(MatchedRoute, self).__init__(**kwargs) self.properties = kwargs.get('properties', None) class MessagingEndpointProperties(msrest.serialization.Model): """The properties of the messaging endpoints used by this IoT hub. :param lock_duration_as_iso8601: The lock duration. See: https://docs.microsoft.com/azure/iot- hub/iot-hub-devguide-file-upload. :type lock_duration_as_iso8601: ~datetime.timedelta :param ttl_as_iso8601: The period of time for which a message is available to consume before it is expired by the IoT hub. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-file- upload. :type ttl_as_iso8601: ~datetime.timedelta :param max_delivery_count: The number of times the IoT hub attempts to deliver a message. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-file-upload. :type max_delivery_count: int """ _validation = { 'max_delivery_count': {'maximum': 100, 'minimum': 1}, } _attribute_map = { 'lock_duration_as_iso8601': {'key': 'lockDurationAsIso8601', 'type': 'duration'}, 'ttl_as_iso8601': {'key': 'ttlAsIso8601', 'type': 'duration'}, 'max_delivery_count': {'key': 'maxDeliveryCount', 'type': 'int'}, } def __init__( self, **kwargs ): super(MessagingEndpointProperties, self).__init__(**kwargs) self.lock_duration_as_iso8601 = kwargs.get('lock_duration_as_iso8601', None) self.ttl_as_iso8601 = kwargs.get('ttl_as_iso8601', None) self.max_delivery_count = kwargs.get('max_delivery_count', None) class Name(msrest.serialization.Model): """Name of Iot Hub type. :param value: IotHub type. :type value: str :param localized_value: Localized value of name. :type localized_value: str """ _attribute_map = { 'value': {'key': 'value', 'type': 'str'}, 'localized_value': {'key': 'localizedValue', 'type': 'str'}, } def __init__( self, **kwargs ): super(Name, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.localized_value = kwargs.get('localized_value', None) class Operation(msrest.serialization.Model): """IoT Hub REST API operation. Variables are only populated by the server, and will be ignored when sending a request. :ivar name: Operation name: {provider}/{resource}/{read | write | action | delete}. :vartype name: str :param display: The object that represents the operation. :type display: ~azure.mgmt.iothub.v2019_11_04.models.OperationDisplay """ _validation = { 'name': {'readonly': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, 'display': {'key': 'display', 'type': 'OperationDisplay'}, } def __init__( self, **kwargs ): super(Operation, self).__init__(**kwargs) self.name = None self.display = kwargs.get('display', None) class OperationDisplay(msrest.serialization.Model): """The object that represents the operation. Variables are only populated by the server, and will be ignored when sending a request. :ivar provider: Service provider: Microsoft Devices. :vartype provider: str :ivar resource: Resource Type: IotHubs. :vartype resource: str :ivar operation: Name of the operation. :vartype operation: str :ivar description: Description of the operation. :vartype description: str """ _validation = { 'provider': {'readonly': True}, 'resource': {'readonly': True}, 'operation': {'readonly': True}, 'description': {'readonly': True}, } _attribute_map = { 'provider': {'key': 'provider', 'type': 'str'}, 'resource': {'key': 'resource', 'type': 'str'}, 'operation': {'key': 'operation', 'type': 'str'}, 'description': {'key': 'description', 'type': 'str'}, } def __init__( self, **kwargs ): super(OperationDisplay, self).__init__(**kwargs) self.provider = None self.resource = None self.operation = None self.description = None class OperationInputs(msrest.serialization.Model): """Input values. All required parameters must be populated in order to send to Azure. :param name: Required. The name of the IoT hub to check. :type name: str """ _validation = { 'name': {'required': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, } def __init__( self, **kwargs ): super(OperationInputs, self).__init__(**kwargs) self.name = kwargs['name'] class OperationListResult(msrest.serialization.Model): """Result of the request to list IoT Hub operations. It contains a list of operations and a URL link to get the next set of results. Variables are only populated by the server, and will be ignored when sending a request. :ivar value: List of IoT Hub operations supported by the Microsoft.Devices resource provider. :vartype value: list[~azure.mgmt.iothub.v2019_11_04.models.Operation] :ivar next_link: URL to get the next set of operation list results if there are any. :vartype next_link: str """ _validation = { 'value': {'readonly': True}, 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[Operation]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(OperationListResult, self).__init__(**kwargs) self.value = None self.next_link = None class RegistryStatistics(msrest.serialization.Model): """Identity registry statistics. Variables are only populated by the server, and will be ignored when sending a request. :ivar total_device_count: The total count of devices in the identity registry. :vartype total_device_count: long :ivar enabled_device_count: The count of enabled devices in the identity registry. :vartype enabled_device_count: long :ivar disabled_device_count: The count of disabled devices in the identity registry. :vartype disabled_device_count: long """ _validation = { 'total_device_count': {'readonly': True}, 'enabled_device_count': {'readonly': True}, 'disabled_device_count': {'readonly': True}, } _attribute_map = { 'total_device_count': {'key': 'totalDeviceCount', 'type': 'long'}, 'enabled_device_count': {'key': 'enabledDeviceCount', 'type': 'long'}, 'disabled_device_count': {'key': 'disabledDeviceCount', 'type': 'long'}, } def __init__( self, **kwargs ): super(RegistryStatistics, self).__init__(**kwargs) self.total_device_count = None self.enabled_device_count = None self.disabled_device_count = None class RouteCompilationError(msrest.serialization.Model): """Compilation error when evaluating route. :param message: Route error message. :type message: str :param severity: Severity of the route error. Possible values include: "error", "warning". :type severity: str or ~azure.mgmt.iothub.v2019_11_04.models.RouteErrorSeverity :param location: Location where the route error happened. :type location: ~azure.mgmt.iothub.v2019_11_04.models.RouteErrorRange """ _attribute_map = { 'message': {'key': 'message', 'type': 'str'}, 'severity': {'key': 'severity', 'type': 'str'}, 'location': {'key': 'location', 'type': 'RouteErrorRange'}, } def __init__( self, **kwargs ): super(RouteCompilationError, self).__init__(**kwargs) self.message = kwargs.get('message', None) self.severity = kwargs.get('severity', None) self.location = kwargs.get('location', None) class RouteErrorPosition(msrest.serialization.Model): """Position where the route error happened. :param line: Line where the route error happened. :type line: int :param column: Column where the route error happened. :type column: int """ _attribute_map = { 'line': {'key': 'line', 'type': 'int'}, 'column': {'key': 'column', 'type': 'int'}, } def __init__( self, **kwargs ): super(RouteErrorPosition, self).__init__(**kwargs) self.line = kwargs.get('line', None) self.column = kwargs.get('column', None) class RouteErrorRange(msrest.serialization.Model): """Range of route errors. :param start: Start where the route error happened. :type start: ~azure.mgmt.iothub.v2019_11_04.models.RouteErrorPosition :param end: End where the route error happened. :type end: ~azure.mgmt.iothub.v2019_11_04.models.RouteErrorPosition """ _attribute_map = { 'start': {'key': 'start', 'type': 'RouteErrorPosition'}, 'end': {'key': 'end', 'type': 'RouteErrorPosition'}, } def __init__( self, **kwargs ): super(RouteErrorRange, self).__init__(**kwargs) self.start = kwargs.get('start', None) self.end = kwargs.get('end', None) class RouteProperties(msrest.serialization.Model): """The properties of a routing rule that your IoT hub uses to route messages to endpoints. All required parameters must be populated in order to send to Azure. :param name: Required. The name of the route. The name can only include alphanumeric characters, periods, underscores, hyphens, has a maximum length of 64 characters, and must be unique. :type name: str :param source: Required. The source that the routing rule is to be applied to, such as DeviceMessages. Possible values include: "Invalid", "DeviceMessages", "TwinChangeEvents", "DeviceLifecycleEvents", "DeviceJobLifecycleEvents". :type source: str or ~azure.mgmt.iothub.v2019_11_04.models.RoutingSource :param condition: The condition that is evaluated to apply the routing rule. If no condition is provided, it evaluates to true by default. For grammar, see: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-query-language. :type condition: str :param endpoint_names: Required. The list of endpoints to which messages that satisfy the condition are routed. Currently only one endpoint is allowed. :type endpoint_names: list[str] :param is_enabled: Required. Used to specify whether a route is enabled. :type is_enabled: bool """ _validation = { 'name': {'required': True, 'pattern': r'^[A-Za-z0-9-._]{1,64}$'}, 'source': {'required': True}, 'endpoint_names': {'required': True, 'max_items': 1, 'min_items': 1}, 'is_enabled': {'required': True}, } _attribute_map = { 'name': {'key': 'name', 'type': 'str'}, 'source': {'key': 'source', 'type': 'str'}, 'condition': {'key': 'condition', 'type': 'str'}, 'endpoint_names': {'key': 'endpointNames', 'type': '[str]'}, 'is_enabled': {'key': 'isEnabled', 'type': 'bool'}, } def __init__( self, **kwargs ): super(RouteProperties, self).__init__(**kwargs) self.name = kwargs['name'] self.source = kwargs['source'] self.condition = kwargs.get('condition', None) self.endpoint_names = kwargs['endpoint_names'] self.is_enabled = kwargs['is_enabled'] class RoutingEndpoints(msrest.serialization.Model): """The properties related to the custom endpoints to which your IoT hub routes messages based on the routing rules. A maximum of 10 custom endpoints are allowed across all endpoint types for paid hubs and only 1 custom endpoint is allowed across all endpoint types for free hubs. :param service_bus_queues: The list of Service Bus queue endpoints that IoT hub routes the messages to, based on the routing rules. :type service_bus_queues: list[~azure.mgmt.iothub.v2019_11_04.models.RoutingServiceBusQueueEndpointProperties] :param service_bus_topics: The list of Service Bus topic endpoints that the IoT hub routes the messages to, based on the routing rules. :type service_bus_topics: list[~azure.mgmt.iothub.v2019_11_04.models.RoutingServiceBusTopicEndpointProperties] :param event_hubs: The list of Event Hubs endpoints that IoT hub routes messages to, based on the routing rules. This list does not include the built-in Event Hubs endpoint. :type event_hubs: list[~azure.mgmt.iothub.v2019_11_04.models.RoutingEventHubProperties] :param storage_containers: The list of storage container endpoints that IoT hub routes messages to, based on the routing rules. :type storage_containers: list[~azure.mgmt.iothub.v2019_11_04.models.RoutingStorageContainerProperties] """ _attribute_map = { 'service_bus_queues': {'key': 'serviceBusQueues', 'type': '[RoutingServiceBusQueueEndpointProperties]'}, 'service_bus_topics': {'key': 'serviceBusTopics', 'type': '[RoutingServiceBusTopicEndpointProperties]'}, 'event_hubs': {'key': 'eventHubs', 'type': '[RoutingEventHubProperties]'}, 'storage_containers': {'key': 'storageContainers', 'type': '[RoutingStorageContainerProperties]'}, } def __init__( self, **kwargs ): super(RoutingEndpoints, self).__init__(**kwargs) self.service_bus_queues = kwargs.get('service_bus_queues', None) self.service_bus_topics = kwargs.get('service_bus_topics', None) self.event_hubs = kwargs.get('event_hubs', None) self.storage_containers = kwargs.get('storage_containers', None) class RoutingEventHubProperties(msrest.serialization.Model): """The properties related to an event hub endpoint. All required parameters must be populated in order to send to Azure. :param connection_string: Required. The connection string of the event hub endpoint. :type connection_string: str :param name: Required. The name that identifies this endpoint. The name can only include alphanumeric characters, periods, underscores, hyphens and has a maximum length of 64 characters. The following names are reserved: events, fileNotifications, $default. Endpoint names must be unique across endpoint types. :type name: str :param subscription_id: The subscription identifier of the event hub endpoint. :type subscription_id: str :param resource_group: The name of the resource group of the event hub endpoint. :type resource_group: str """ _validation = { 'connection_string': {'required': True}, 'name': {'required': True, 'pattern': r'^[A-Za-z0-9-._]{1,64}$'}, } _attribute_map = { 'connection_string': {'key': 'connectionString', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'subscription_id': {'key': 'subscriptionId', 'type': 'str'}, 'resource_group': {'key': 'resourceGroup', 'type': 'str'}, } def __init__( self, **kwargs ): super(RoutingEventHubProperties, self).__init__(**kwargs) self.connection_string = kwargs['connection_string'] self.name = kwargs['name'] self.subscription_id = kwargs.get('subscription_id', None) self.resource_group = kwargs.get('resource_group', None) class RoutingMessage(msrest.serialization.Model): """Routing message. :param body: Body of routing message. :type body: str :param app_properties: App properties. :type app_properties: dict[str, str] :param system_properties: System properties. :type system_properties: dict[str, str] """ _attribute_map = { 'body': {'key': 'body', 'type': 'str'}, 'app_properties': {'key': 'appProperties', 'type': '{str}'}, 'system_properties': {'key': 'systemProperties', 'type': '{str}'}, } def __init__( self, **kwargs ): super(RoutingMessage, self).__init__(**kwargs) self.body = kwargs.get('body', None) self.app_properties = kwargs.get('app_properties', None) self.system_properties = kwargs.get('system_properties', None) class RoutingProperties(msrest.serialization.Model): """The routing related properties of the IoT hub. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-messaging. :param endpoints: The properties related to the custom endpoints to which your IoT hub routes messages based on the routing rules. A maximum of 10 custom endpoints are allowed across all endpoint types for paid hubs and only 1 custom endpoint is allowed across all endpoint types for free hubs. :type endpoints: ~azure.mgmt.iothub.v2019_11_04.models.RoutingEndpoints :param routes: The list of user-provided routing rules that the IoT hub uses to route messages to built-in and custom endpoints. A maximum of 100 routing rules are allowed for paid hubs and a maximum of 5 routing rules are allowed for free hubs. :type routes: list[~azure.mgmt.iothub.v2019_11_04.models.RouteProperties] :param fallback_route: The properties of the route that is used as a fall-back route when none of the conditions specified in the 'routes' section are met. This is an optional parameter. When this property is not set, the messages which do not meet any of the conditions specified in the 'routes' section get routed to the built-in eventhub endpoint. :type fallback_route: ~azure.mgmt.iothub.v2019_11_04.models.FallbackRouteProperties :param enrichments: The list of user-provided enrichments that the IoT hub applies to messages to be delivered to built-in and custom endpoints. See: https://aka.ms/telemetryoneventgrid. :type enrichments: list[~azure.mgmt.iothub.v2019_11_04.models.EnrichmentProperties] """ _attribute_map = { 'endpoints': {'key': 'endpoints', 'type': 'RoutingEndpoints'}, 'routes': {'key': 'routes', 'type': '[RouteProperties]'}, 'fallback_route': {'key': 'fallbackRoute', 'type': 'FallbackRouteProperties'}, 'enrichments': {'key': 'enrichments', 'type': '[EnrichmentProperties]'}, } def __init__( self, **kwargs ): super(RoutingProperties, self).__init__(**kwargs) self.endpoints = kwargs.get('endpoints', None) self.routes = kwargs.get('routes', None) self.fallback_route = kwargs.get('fallback_route', None) self.enrichments = kwargs.get('enrichments', None) class RoutingServiceBusQueueEndpointProperties(msrest.serialization.Model): """The properties related to service bus queue endpoint types. All required parameters must be populated in order to send to Azure. :param connection_string: Required. The connection string of the service bus queue endpoint. :type connection_string: str :param name: Required. The name that identifies this endpoint. The name can only include alphanumeric characters, periods, underscores, hyphens and has a maximum length of 64 characters. The following names are reserved: events, fileNotifications, $default. Endpoint names must be unique across endpoint types. The name need not be the same as the actual queue name. :type name: str :param subscription_id: The subscription identifier of the service bus queue endpoint. :type subscription_id: str :param resource_group: The name of the resource group of the service bus queue endpoint. :type resource_group: str """ _validation = { 'connection_string': {'required': True}, 'name': {'required': True, 'pattern': r'^[A-Za-z0-9-._]{1,64}$'}, } _attribute_map = { 'connection_string': {'key': 'connectionString', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'subscription_id': {'key': 'subscriptionId', 'type': 'str'}, 'resource_group': {'key': 'resourceGroup', 'type': 'str'}, } def __init__( self, **kwargs ): super(RoutingServiceBusQueueEndpointProperties, self).__init__(**kwargs) self.connection_string = kwargs['connection_string'] self.name = kwargs['name'] self.subscription_id = kwargs.get('subscription_id', None) self.resource_group = kwargs.get('resource_group', None) class RoutingServiceBusTopicEndpointProperties(msrest.serialization.Model): """The properties related to service bus topic endpoint types. All required parameters must be populated in order to send to Azure. :param connection_string: Required. The connection string of the service bus topic endpoint. :type connection_string: str :param name: Required. The name that identifies this endpoint. The name can only include alphanumeric characters, periods, underscores, hyphens and has a maximum length of 64 characters. The following names are reserved: events, fileNotifications, $default. Endpoint names must be unique across endpoint types. The name need not be the same as the actual topic name. :type name: str :param subscription_id: The subscription identifier of the service bus topic endpoint. :type subscription_id: str :param resource_group: The name of the resource group of the service bus topic endpoint. :type resource_group: str """ _validation = { 'connection_string': {'required': True}, 'name': {'required': True, 'pattern': r'^[A-Za-z0-9-._]{1,64}$'}, } _attribute_map = { 'connection_string': {'key': 'connectionString', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'subscription_id': {'key': 'subscriptionId', 'type': 'str'}, 'resource_group': {'key': 'resourceGroup', 'type': 'str'}, } def __init__( self, **kwargs ): super(RoutingServiceBusTopicEndpointProperties, self).__init__(**kwargs) self.connection_string = kwargs['connection_string'] self.name = kwargs['name'] self.subscription_id = kwargs.get('subscription_id', None) self.resource_group = kwargs.get('resource_group', None) class RoutingStorageContainerProperties(msrest.serialization.Model): """The properties related to a storage container endpoint. All required parameters must be populated in order to send to Azure. :param connection_string: Required. The connection string of the storage account. :type connection_string: str :param name: Required. The name that identifies this endpoint. The name can only include alphanumeric characters, periods, underscores, hyphens and has a maximum length of 64 characters. The following names are reserved: events, fileNotifications, $default. Endpoint names must be unique across endpoint types. :type name: str :param subscription_id: The subscription identifier of the storage account. :type subscription_id: str :param resource_group: The name of the resource group of the storage account. :type resource_group: str :param container_name: Required. The name of storage container in the storage account. :type container_name: str :param file_name_format: File name format for the blob. Default format is {iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm}. All parameters are mandatory but can be reordered. :type file_name_format: str :param batch_frequency_in_seconds: Time interval at which blobs are written to storage. Value should be between 60 and 720 seconds. Default value is 300 seconds. :type batch_frequency_in_seconds: int :param max_chunk_size_in_bytes: Maximum number of bytes for each blob written to storage. Value should be between 10485760(10MB) and 524288000(500MB). Default value is 314572800(300MB). :type max_chunk_size_in_bytes: int :param encoding: Encoding that is used to serialize messages to blobs. Supported values are 'avro', 'avrodeflate', and 'JSON'. Default value is 'avro'. Possible values include: "Avro", "AvroDeflate", "JSON". :type encoding: str or ~azure.mgmt.iothub.v2019_11_04.models.RoutingStorageContainerPropertiesEncoding """ _validation = { 'connection_string': {'required': True}, 'name': {'required': True, 'pattern': r'^[A-Za-z0-9-._]{1,64}$'}, 'container_name': {'required': True}, 'batch_frequency_in_seconds': {'maximum': 720, 'minimum': 60}, 'max_chunk_size_in_bytes': {'maximum': 524288000, 'minimum': 10485760}, } _attribute_map = { 'connection_string': {'key': 'connectionString', 'type': 'str'}, 'name': {'key': 'name', 'type': 'str'}, 'subscription_id': {'key': 'subscriptionId', 'type': 'str'}, 'resource_group': {'key': 'resourceGroup', 'type': 'str'}, 'container_name': {'key': 'containerName', 'type': 'str'}, 'file_name_format': {'key': 'fileNameFormat', 'type': 'str'}, 'batch_frequency_in_seconds': {'key': 'batchFrequencyInSeconds', 'type': 'int'}, 'max_chunk_size_in_bytes': {'key': 'maxChunkSizeInBytes', 'type': 'int'}, 'encoding': {'key': 'encoding', 'type': 'str'}, } def __init__( self, **kwargs ): super(RoutingStorageContainerProperties, self).__init__(**kwargs) self.connection_string = kwargs['connection_string'] self.name = kwargs['name'] self.subscription_id = kwargs.get('subscription_id', None) self.resource_group = kwargs.get('resource_group', None) self.container_name = kwargs['container_name'] self.file_name_format = kwargs.get('file_name_format', None) self.batch_frequency_in_seconds = kwargs.get('batch_frequency_in_seconds', None) self.max_chunk_size_in_bytes = kwargs.get('max_chunk_size_in_bytes', None) self.encoding = kwargs.get('encoding', None) class RoutingTwin(msrest.serialization.Model): """Twin reference input parameter. This is an optional parameter. :param tags: A set of tags. Twin Tags. :type tags: object :param properties: :type properties: ~azure.mgmt.iothub.v2019_11_04.models.RoutingTwinProperties """ _attribute_map = { 'tags': {'key': 'tags', 'type': 'object'}, 'properties': {'key': 'properties', 'type': 'RoutingTwinProperties'}, } def __init__( self, **kwargs ): super(RoutingTwin, self).__init__(**kwargs) self.tags = kwargs.get('tags', None) self.properties = kwargs.get('properties', None) class RoutingTwinProperties(msrest.serialization.Model): """RoutingTwinProperties. :param desired: Twin desired properties. :type desired: object :param reported: Twin desired properties. :type reported: object """ _attribute_map = { 'desired': {'key': 'desired', 'type': 'object'}, 'reported': {'key': 'reported', 'type': 'object'}, } def __init__( self, **kwargs ): super(RoutingTwinProperties, self).__init__(**kwargs) self.desired = kwargs.get('desired', None) self.reported = kwargs.get('reported', None) class SharedAccessSignatureAuthorizationRule(msrest.serialization.Model): """The properties of an IoT hub shared access policy. All required parameters must be populated in order to send to Azure. :param key_name: Required. The name of the shared access policy. :type key_name: str :param primary_key: The primary key. :type primary_key: str :param secondary_key: The secondary key. :type secondary_key: str :param rights: Required. The permissions assigned to the shared access policy. Possible values include: "RegistryRead", "RegistryWrite", "ServiceConnect", "DeviceConnect", "RegistryRead, RegistryWrite", "RegistryRead, ServiceConnect", "RegistryRead, DeviceConnect", "RegistryWrite, ServiceConnect", "RegistryWrite, DeviceConnect", "ServiceConnect, DeviceConnect", "RegistryRead, RegistryWrite, ServiceConnect", "RegistryRead, RegistryWrite, DeviceConnect", "RegistryRead, ServiceConnect, DeviceConnect", "RegistryWrite, ServiceConnect, DeviceConnect", "RegistryRead, RegistryWrite, ServiceConnect, DeviceConnect". :type rights: str or ~azure.mgmt.iothub.v2019_11_04.models.AccessRights """ _validation = { 'key_name': {'required': True}, 'rights': {'required': True}, } _attribute_map = { 'key_name': {'key': 'keyName', 'type': 'str'}, 'primary_key': {'key': 'primaryKey', 'type': 'str'}, 'secondary_key': {'key': 'secondaryKey', 'type': 'str'}, 'rights': {'key': 'rights', 'type': 'str'}, } def __init__( self, **kwargs ): super(SharedAccessSignatureAuthorizationRule, self).__init__(**kwargs) self.key_name = kwargs['key_name'] self.primary_key = kwargs.get('primary_key', None) self.secondary_key = kwargs.get('secondary_key', None) self.rights = kwargs['rights'] class SharedAccessSignatureAuthorizationRuleListResult(msrest.serialization.Model): """The list of shared access policies with a next link. Variables are only populated by the server, and will be ignored when sending a request. :param value: The list of shared access policies. :type value: list[~azure.mgmt.iothub.v2019_11_04.models.SharedAccessSignatureAuthorizationRule] :ivar next_link: The next link. :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[SharedAccessSignatureAuthorizationRule]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(SharedAccessSignatureAuthorizationRuleListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None class StorageEndpointProperties(msrest.serialization.Model): """The properties of the Azure Storage endpoint for file upload. All required parameters must be populated in order to send to Azure. :param sas_ttl_as_iso8601: The period of time for which the SAS URI generated by IoT Hub for file upload is valid. See: https://docs.microsoft.com/azure/iot-hub/iot-hub-devguide-file- upload#file-upload-notification-configuration-options. :type sas_ttl_as_iso8601: ~datetime.timedelta :param connection_string: Required. The connection string for the Azure Storage account to which files are uploaded. :type connection_string: str :param container_name: Required. The name of the root container where you upload files. The container need not exist but should be creatable using the connectionString specified. :type container_name: str """ _validation = { 'connection_string': {'required': True}, 'container_name': {'required': True}, } _attribute_map = { 'sas_ttl_as_iso8601': {'key': 'sasTtlAsIso8601', 'type': 'duration'}, 'connection_string': {'key': 'connectionString', 'type': 'str'}, 'container_name': {'key': 'containerName', 'type': 'str'}, } def __init__( self, **kwargs ): super(StorageEndpointProperties, self).__init__(**kwargs) self.sas_ttl_as_iso8601 = kwargs.get('sas_ttl_as_iso8601', None) self.connection_string = kwargs['connection_string'] self.container_name = kwargs['container_name'] class TagsResource(msrest.serialization.Model): """A container holding only the Tags for a resource, allowing the user to update the tags on an IoT Hub instance. :param tags: A set of tags. Resource tags. :type tags: dict[str, str] """ _attribute_map = { 'tags': {'key': 'tags', 'type': '{str}'}, } def __init__( self, **kwargs ): super(TagsResource, self).__init__(**kwargs) self.tags = kwargs.get('tags', None) class TestAllRoutesInput(msrest.serialization.Model): """Input for testing all routes. :param routing_source: Routing source. Possible values include: "Invalid", "DeviceMessages", "TwinChangeEvents", "DeviceLifecycleEvents", "DeviceJobLifecycleEvents". :type routing_source: str or ~azure.mgmt.iothub.v2019_11_04.models.RoutingSource :param message: Routing message. :type message: ~azure.mgmt.iothub.v2019_11_04.models.RoutingMessage :param twin: Routing Twin Reference. :type twin: ~azure.mgmt.iothub.v2019_11_04.models.RoutingTwin """ _attribute_map = { 'routing_source': {'key': 'routingSource', 'type': 'str'}, 'message': {'key': 'message', 'type': 'RoutingMessage'}, 'twin': {'key': 'twin', 'type': 'RoutingTwin'}, } def __init__( self, **kwargs ): super(TestAllRoutesInput, self).__init__(**kwargs) self.routing_source = kwargs.get('routing_source', None) self.message = kwargs.get('message', None) self.twin = kwargs.get('twin', None) class TestAllRoutesResult(msrest.serialization.Model): """Result of testing all routes. :param routes: JSON-serialized array of matched routes. :type routes: list[~azure.mgmt.iothub.v2019_11_04.models.MatchedRoute] """ _attribute_map = { 'routes': {'key': 'routes', 'type': '[MatchedRoute]'}, } def __init__( self, **kwargs ): super(TestAllRoutesResult, self).__init__(**kwargs) self.routes = kwargs.get('routes', None) class TestRouteInput(msrest.serialization.Model): """Input for testing route. All required parameters must be populated in order to send to Azure. :param message: Routing message. :type message: ~azure.mgmt.iothub.v2019_11_04.models.RoutingMessage :param route: Required. Route properties. :type route: ~azure.mgmt.iothub.v2019_11_04.models.RouteProperties :param twin: Routing Twin Reference. :type twin: ~azure.mgmt.iothub.v2019_11_04.models.RoutingTwin """ _validation = { 'route': {'required': True}, } _attribute_map = { 'message': {'key': 'message', 'type': 'RoutingMessage'}, 'route': {'key': 'route', 'type': 'RouteProperties'}, 'twin': {'key': 'twin', 'type': 'RoutingTwin'}, } def __init__( self, **kwargs ): super(TestRouteInput, self).__init__(**kwargs) self.message = kwargs.get('message', None) self.route = kwargs['route'] self.twin = kwargs.get('twin', None) class TestRouteResult(msrest.serialization.Model): """Result of testing one route. :param result: Result of testing route. Possible values include: "undefined", "false", "true". :type result: str or ~azure.mgmt.iothub.v2019_11_04.models.TestResultStatus :param details: Detailed result of testing route. :type details: ~azure.mgmt.iothub.v2019_11_04.models.TestRouteResultDetails """ _attribute_map = { 'result': {'key': 'result', 'type': 'str'}, 'details': {'key': 'details', 'type': 'TestRouteResultDetails'}, } def __init__( self, **kwargs ): super(TestRouteResult, self).__init__(**kwargs) self.result = kwargs.get('result', None) self.details = kwargs.get('details', None) class TestRouteResultDetails(msrest.serialization.Model): """Detailed result of testing a route. :param compilation_errors: JSON-serialized list of route compilation errors. :type compilation_errors: list[~azure.mgmt.iothub.v2019_11_04.models.RouteCompilationError] """ _attribute_map = { 'compilation_errors': {'key': 'compilationErrors', 'type': '[RouteCompilationError]'}, } def __init__( self, **kwargs ): super(TestRouteResultDetails, self).__init__(**kwargs) self.compilation_errors = kwargs.get('compilation_errors', None) class UserSubscriptionQuota(msrest.serialization.Model): """User subscription quota response. :param id: IotHub type id. :type id: str :param type: Response type. :type type: str :param unit: Unit of IotHub type. :type unit: str :param current_value: Current number of IotHub type. :type current_value: int :param limit: Numerical limit on IotHub type. :type limit: int :param name: IotHub type. :type name: ~azure.mgmt.iothub.v2019_11_04.models.Name """ _attribute_map = { 'id': {'key': 'id', 'type': 'str'}, 'type': {'key': 'type', 'type': 'str'}, 'unit': {'key': 'unit', 'type': 'str'}, 'current_value': {'key': 'currentValue', 'type': 'int'}, 'limit': {'key': 'limit', 'type': 'int'}, 'name': {'key': 'name', 'type': 'Name'}, } def __init__( self, **kwargs ): super(UserSubscriptionQuota, self).__init__(**kwargs) self.id = kwargs.get('id', None) self.type = kwargs.get('type', None) self.unit = kwargs.get('unit', None) self.current_value = kwargs.get('current_value', None) self.limit = kwargs.get('limit', None) self.name = kwargs.get('name', None) class UserSubscriptionQuotaListResult(msrest.serialization.Model): """Json-serialized array of User subscription quota response. Variables are only populated by the server, and will be ignored when sending a request. :param value: :type value: list[~azure.mgmt.iothub.v2019_11_04.models.UserSubscriptionQuota] :ivar next_link: :vartype next_link: str """ _validation = { 'next_link': {'readonly': True}, } _attribute_map = { 'value': {'key': 'value', 'type': '[UserSubscriptionQuota]'}, 'next_link': {'key': 'nextLink', 'type': 'str'}, } def __init__( self, **kwargs ): super(UserSubscriptionQuotaListResult, self).__init__(**kwargs) self.value = kwargs.get('value', None) self.next_link = None
[]
cognifloyd/st2-open-rbac
tests/unit/controllers/v1/test_rbac_for_supported_st2api_endpoints.py
fb3251223743e497267277fe9f5cef91f41ade34
# Licensed to the StackStorm, Inc ('StackStorm') under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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 collections import OrderedDict import six import mock from st2common.services import triggers as trigger_service with mock.patch.object(trigger_service, 'create_trigger_type_db', mock.MagicMock()): from st2api.controllers.v1.webhooks import HooksHolder from st2common.persistence.rbac import UserRoleAssignment from st2common.models.db.rbac import UserRoleAssignmentDB from st2common.service_setup import register_service_in_service_registry from st2common.services import coordination from st2tests import config as tests_config from st2tests.fixturesloader import FixturesLoader from open_rbac.tests import APIControllerWithRBACTestCase from tests.unit.controllers.v1.test_webhooks import DUMMY_TRIGGER_DICT http_client = six.moves.http_client __all__ = [ 'APIControllersRBACTestCase' ] FIXTURES_PACK = 'generic' TEST_FIXTURES = OrderedDict([ ('runners', ['testrunner1.yaml', 'run-local.yaml']), ('sensors', ['sensor1.yaml']), ('actions', ['action1.yaml', 'local.yaml']), ('aliases', ['alias1.yaml']), ('triggers', ['trigger1.yaml', 'cron1.yaml']), ('rules', ['rule1.yaml']), ('triggertypes', ['triggertype1.yaml']), ('executions', ['execution1.yaml']), ('liveactions', ['liveaction1.yaml', 'parentliveaction.yaml', 'childliveaction.yaml']), ('enforcements', ['enforcement1.yaml']), ('apikeys', ['apikey1.yaml']), ('traces', ['trace_for_test_enforce.yaml']) ]) MOCK_RUNNER_1 = { 'name': 'test-runner-1', 'description': 'test', 'enabled': False } MOCK_ACTION_1 = { 'name': 'ma.dummy.action', 'pack': 'examples', 'description': 'test description', 'enabled': True, 'entry_point': '/tmp/test/action2.py', 'runner_type': 'local-shell-script', 'parameters': { 'c': {'type': 'string', 'default': 'C1', 'position': 0}, 'd': {'type': 'string', 'default': 'D1', 'immutable': True} } } MOCK_ACTION_ALIAS_1 = { 'name': 'alias3', 'pack': 'aliases', 'description': 'test description', 'action_ref': 'core.local', 'formats': ['a', 'b'] } MOCK_RULE_1 = { 'enabled': True, 'name': 'st2.test.rule2', 'pack': 'yoyohoneysingh', 'trigger': { 'type': 'wolfpack.triggertype-1' }, 'criteria': { 'trigger.k1': { 'pattern': 't1_p_v', 'type': 'equals' } }, 'action': { 'ref': 'sixpack.st2.test.action', 'parameters': { 'ip2': '{{rule.k1}}', 'ip1': '{{trigger.t1_p}}' } }, 'description': '' } class APIControllersRBACTestCase(APIControllerWithRBACTestCase): """ Test class which hits all the API endpoints which are behind the RBAC wall with a user which has no permissions and makes sure API returns access denied. """ register_packs = True fixtures_loader = FixturesLoader() coordinator = None @classmethod def setUpClass(cls): tests_config.parse_args(coordinator_noop=True) super(APIControllersRBACTestCase, cls).setUpClass() cls.coordinator = coordination.get_coordinator(use_cache=False) # Register mock service in the service registry for testing purposes service = six.binary_type(six.text_type('mock_service').encode('ascii')) register_service_in_service_registry(service=service, capabilities={'key1': 'value1', 'name': 'mock_service'}, start_heart=True) @classmethod def tearDownClass(cls): super(APIControllersRBACTestCase, cls).tearDownClass() coordination.coordinator_teardown(cls.coordinator) def setUp(self): super(APIControllersRBACTestCase, self).setUp() # Register packs if self.register_packs: self._register_packs() # Insert mock objects - those objects are used to test get one, edit and delete operations self.models = self.fixtures_loader.save_fixtures_to_db(fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_FIXTURES) self.role_assignment_db_model = UserRoleAssignmentDB( user='user', role='role', source='assignments/user.yaml') UserRoleAssignment.add_or_update(self.role_assignment_db_model) @mock.patch.object(HooksHolder, 'get_triggers_for_hook', mock.MagicMock( return_value=[DUMMY_TRIGGER_DICT])) def test_api_endpoints_behind_rbac_wall(self): # alias_model = self.models['aliases']['alias1.yaml'] sensor_model = self.models['sensors']['sensor1.yaml'] rule_model = self.models['rules']['rule1.yaml'] enforcement_model = self.models['enforcements']['enforcement1.yaml'] execution_model = self.models['executions']['execution1.yaml'] trace_model = self.models['traces']['trace_for_test_enforce.yaml'] timer_model = self.models['triggers']['cron1.yaml'] supported_endpoints = [ # Runners { 'path': '/v1/runnertypes', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/runnertypes/test-runner-1', 'method': 'GET' }, { 'path': '/v1/runnertypes/test-runner-1', 'method': 'PUT', 'payload': MOCK_RUNNER_1 }, # Packs { 'path': '/v1/packs', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/packs/dummy_pack_1', 'method': 'GET' }, # Pack management { 'path': '/v1/packs/install', 'method': 'POST', 'payload': {'packs': 'libcloud'} }, { 'path': '/v1/packs/uninstall', 'method': 'POST', 'payload': {'packs': 'libcloud'} }, { 'path': '/v1/packs/register', 'method': 'POST', 'payload': {'types': ['actions']} }, { 'path': '/v1/packs/index/search', 'method': 'POST', 'payload': {'query': 'cloud'} }, { 'path': '/v1/packs/index/health', 'method': 'GET' }, # Pack views { 'path': '/v1/packs/views/files/dummy_pack_1', 'method': 'GET' }, # Pack config schemas { 'path': '/v1/config_schemas', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/config_schemas/dummy_pack_1', 'method': 'GET' }, { 'path': '/v1/packs/views/file/dummy_pack_1/pack.yaml', 'method': 'GET' }, # Pack configs { 'path': '/v1/configs', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/configs/dummy_pack_1', 'method': 'GET' }, { 'path': '/v1/configs/dummy_pack_1', 'method': 'PUT', 'payload': { 'foo': 'bar' } }, # Sensors { 'path': '/v1/sensortypes', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/sensortypes/%s' % (sensor_model.ref), 'method': 'GET' }, { 'path': '/v1/sensortypes/%s' % (sensor_model.ref), 'method': 'PUT', 'payload': {'enabled': False} }, # Actions { 'path': '/v1/actions', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/actions/wolfpack.action-1', 'method': 'GET' }, { 'path': '/v1/actions', 'method': 'POST', 'payload': MOCK_ACTION_1 }, { 'path': '/v1/actions/wolfpack.action-1', 'method': 'PUT', 'payload': MOCK_ACTION_1 }, { 'path': '/v1/actions/wolfpack.action-1', 'method': 'DELETE' }, # Action aliases { 'path': '/v1/actionalias', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/actionalias/aliases.alias1', 'method': 'GET' }, { 'path': '/v1/actionalias', 'method': 'POST', 'payload': MOCK_ACTION_ALIAS_1 }, { 'path': '/v1/actionalias/aliases.alias1', 'method': 'PUT', 'payload': MOCK_ACTION_ALIAS_1 }, { 'path': '/v1/actionalias/aliases.alias1', 'method': 'DELETE' }, { 'path': '/v1/actionalias/match', 'method': 'POST', 'payload': {'command': 'test command string'} }, # Rules { 'path': '/v1/rules', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/rules/%s' % (rule_model.ref), 'method': 'GET' }, { 'path': '/v1/rules', 'method': 'POST', 'payload': MOCK_RULE_1 }, { 'path': '/v1/rules/%s' % (rule_model.ref), 'method': 'PUT', 'payload': MOCK_RULE_1 }, { 'path': '/v1/rules/%s' % (rule_model.ref), 'method': 'DELETE' }, # Rule enforcements { 'path': '/v1/ruleenforcements', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/ruleenforcements/%s' % (enforcement_model.id), 'method': 'GET' }, # Action Executions { 'path': '/v1/executions', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/executions/%s' % (execution_model.id), 'method': 'GET' }, { 'path': '/v1/executions/%s/output' % (execution_model.id), 'method': 'GET' }, { 'path': '/v1/executions', 'method': 'POST', 'payload': {'action': 'core.local'} # schedule execution / run action }, { 'path': '/v1/executions/%s' % (execution_model.id), 'method': 'DELETE' # stop execution }, { 'path': '/v1/executions/%s/re_run' % (execution_model.id), 'method': 'POST', # re-run execution 'payload': {'parameters': {}} }, # Action execution nested controllers { 'path': '/v1/executions/%s/attribute/trigger_instance' % (execution_model.id), 'method': 'GET' }, { 'path': '/v1/executions/%s/children' % (execution_model.id), 'method': 'GET' }, # Alias executions { 'path': '/v1/aliasexecution', 'method': 'POST', 'payload': {'name': 'alias1', 'format': 'foo bar ponies', 'command': 'foo bar ponies', 'user': 'channel', 'source_channel': 'bar'} }, # Webhook { 'path': '/v1/webhooks/st2', 'method': 'POST', 'payload': { 'trigger': 'some', 'payload': { 'some': 'thing' } } }, # Traces { 'path': '/v1/traces', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/traces/%s' % (trace_model.id), 'method': 'GET' }, # Timers { 'path': '/v1/timers', 'method': 'GET' }, { 'path': '/v1/timers/%s' % (timer_model.id), 'method': 'GET' }, # Webhooks { 'path': '/v1/webhooks', 'method': 'GET' }, { 'path': '/v1/webhooks/git', 'method': 'GET' }, # RBAC - roles { 'path': '/v1/rbac/roles', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/rbac/roles/admin', 'method': 'GET' }, # RBAC - user role assignments { 'path': '/v1/rbac/role_assignments', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/rbac/role_assignments/%s' % (self.role_assignment_db_model['id']), 'method': 'GET' }, # RBAC - permission types { 'path': '/v1/rbac/permission_types', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/rbac/permission_types/action', 'method': 'GET' }, # Action views { 'path': '/v1/actions/views/overview', 'method': 'GET', 'is_getall': True }, # Rule views { 'path': '/v1/rules/views', 'method': 'GET', 'is_getall': True }, # Service registry { 'path': '/v1/service_registry/groups', 'method': 'GET', 'is_getall': True }, { 'path': '/v1/service_registry/groups/mock_service/members', 'method': 'GET', 'is_getall': True } ] self.use_user(self.users['no_permissions']) for endpoint in supported_endpoints: response = self._perform_request_for_endpoint(endpoint=endpoint) msg = '%s "%s" didn\'t return 403 status code (body=%s)' % (endpoint['method'], endpoint['path'], response.body) self.assertEqual(response.status_code, http_client.FORBIDDEN, msg) # Also test ?limit=-1 - non-admin user self.use_user(self.users['observer']) for endpoint in supported_endpoints: if not endpoint.get('is_getall', False): continue response = self.app.get(endpoint['path'] + '?limit=-1', expect_errors=True) msg = '%s "%s" didn\'t return 403 status code (body=%s)' % (endpoint['method'], endpoint['path'], response.body) self.assertEqual(response.status_code, http_client.FORBIDDEN, msg) # Also test ?limit=-1 - admin user self.use_user(self.users['admin']) for endpoint in supported_endpoints: if not endpoint.get('is_getall', False): continue response = self.app.get(endpoint['path'] + '?limit=-1') self.assertEqual(response.status_code, http_client.OK) def test_icon_png_file_is_whitelisted(self): self.use_user(self.users['no_permissions']) # Test that access to icon.png file doesn't require any permissions response = self.app.get('/v1/packs/views/file/dummy_pack_2/icon.png') self.assertEqual(response.status_code, http_client.OK) # Other files should return forbidden response = self.app.get('/v1/packs/views/file/dummy_pack_2/pack.yaml', expect_errors=True) self.assertEqual(response.status_code, http_client.FORBIDDEN) def _perform_request_for_endpoint(self, endpoint): if endpoint['method'] == 'GET': response = self.app.get(endpoint['path'], expect_errors=True) elif endpoint['method'] == 'POST': return self.app.post_json(endpoint['path'], endpoint['payload'], expect_errors=True) elif endpoint['method'] == 'PUT': return self.app.put_json(endpoint['path'], endpoint['payload'], expect_errors=True) elif endpoint['method'] == 'DELETE': return self.app.delete(endpoint['path'], expect_errors=True) else: raise ValueError('Unsupported method: %s' % (endpoint['method'])) return response
[((1631, 2193), 'collections.OrderedDict', 'OrderedDict', (["[('runners', ['testrunner1.yaml', 'run-local.yaml']), ('sensors', [\n 'sensor1.yaml']), ('actions', ['action1.yaml', 'local.yaml']), (\n 'aliases', ['alias1.yaml']), ('triggers', ['trigger1.yaml',\n 'cron1.yaml']), ('rules', ['rule1.yaml']), ('triggertypes', [\n 'triggertype1.yaml']), ('executions', ['execution1.yaml']), (\n 'liveactions', ['liveaction1.yaml', 'parentliveaction.yaml',\n 'childliveaction.yaml']), ('enforcements', ['enforcement1.yaml']), (\n 'apikeys', ['apikey1.yaml']), ('traces', ['trace_for_test_enforce.yaml'])]"], {}), "([('runners', ['testrunner1.yaml', 'run-local.yaml']), (\n 'sensors', ['sensor1.yaml']), ('actions', ['action1.yaml', 'local.yaml'\n ]), ('aliases', ['alias1.yaml']), ('triggers', ['trigger1.yaml',\n 'cron1.yaml']), ('rules', ['rule1.yaml']), ('triggertypes', [\n 'triggertype1.yaml']), ('executions', ['execution1.yaml']), (\n 'liveactions', ['liveaction1.yaml', 'parentliveaction.yaml',\n 'childliveaction.yaml']), ('enforcements', ['enforcement1.yaml']), (\n 'apikeys', ['apikey1.yaml']), ('traces', ['trace_for_test_enforce.yaml'])])\n", (1642, 2193), False, 'from collections import OrderedDict\n'), ((3630, 3646), 'st2tests.fixturesloader.FixturesLoader', 'FixturesLoader', ([], {}), '()\n', (3644, 3646), False, 'from st2tests.fixturesloader import FixturesLoader\n'), ((966, 982), 'mock.MagicMock', 'mock.MagicMock', ([], {}), '()\n', (980, 982), False, 'import mock\n'), ((3722, 3768), 'st2tests.config.parse_args', 'tests_config.parse_args', ([], {'coordinator_noop': '(True)'}), '(coordinator_noop=True)\n', (3745, 3768), True, 'from st2tests import config as tests_config\n'), ((3857, 3902), 'st2common.services.coordination.get_coordinator', 'coordination.get_coordinator', ([], {'use_cache': '(False)'}), '(use_cache=False)\n', (3885, 3902), False, 'from st2common.services import coordination\n'), ((4070, 4202), 'st2common.service_setup.register_service_in_service_registry', 'register_service_in_service_registry', ([], {'service': 'service', 'capabilities': "{'key1': 'value1', 'name': 'mock_service'}", 'start_heart': '(True)'}), "(service=service, capabilities={'key1':\n 'value1', 'name': 'mock_service'}, start_heart=True)\n", (4106, 4202), False, 'from st2common.service_setup import register_service_in_service_registry\n'), ((4466, 4516), 'st2common.services.coordination.coordinator_teardown', 'coordination.coordinator_teardown', (['cls.coordinator'], {}), '(cls.coordinator)\n', (4499, 4516), False, 'from st2common.services import coordination\n'), ((5013, 5091), 'st2common.models.db.rbac.UserRoleAssignmentDB', 'UserRoleAssignmentDB', ([], {'user': '"""user"""', 'role': '"""role"""', 'source': '"""assignments/user.yaml"""'}), "(user='user', role='role', source='assignments/user.yaml')\n", (5033, 5091), False, 'from st2common.models.db.rbac import UserRoleAssignmentDB\n'), ((5113, 5176), 'st2common.persistence.rbac.UserRoleAssignment.add_or_update', 'UserRoleAssignment.add_or_update', (['self.role_assignment_db_model'], {}), '(self.role_assignment_db_model)\n', (5145, 5176), False, 'from st2common.persistence.rbac import UserRoleAssignment\n'), ((5239, 5288), 'mock.MagicMock', 'mock.MagicMock', ([], {'return_value': '[DUMMY_TRIGGER_DICT]'}), '(return_value=[DUMMY_TRIGGER_DICT])\n', (5253, 5288), False, 'import mock\n'), ((4015, 4044), 'six.text_type', 'six.text_type', (['"""mock_service"""'], {}), "('mock_service')\n", (4028, 4044), False, 'import six\n')]
dtrawins/seldon-core
testing/scripts/test_ksonnet_single_namespace.py
3d8b3791b343118953757a1e787e5919cc64e697
import pytest import time import subprocess from subprocess import run,Popen from seldon_utils import * from k8s_utils import * def wait_for_shutdown(deploymentName): ret = run("kubectl get deploy/"+deploymentName, shell=True) while ret.returncode == 0: time.sleep(1) ret = run("kubectl get deploy/"+deploymentName, shell=True) def wait_for_rollout(deploymentName): ret = run("kubectl rollout status deploy/"+deploymentName, shell=True) while ret.returncode > 0: time.sleep(1) ret = run("kubectl rollout status deploy/"+deploymentName, shell=True) def initial_rest_request(): r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) if not r.status_code == 200: time.sleep(1) r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) if not r.status_code == 200: time.sleep(5) r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) return r @pytest.mark.usefixtures("seldon_java_images") @pytest.mark.usefixtures("single_namespace_seldon_ksonnet") class TestSingleNamespace(object): # Test singe model helm script with 4 API methods def test_single_model(self): run('cd my-model && ks delete default && ks component rm mymodel', shell=True) run('kubectl delete sdep --all', shell=True) run('cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel', shell=True, check=True) wait_for_rollout("mymodel-mymodel-025d03d") r = initial_rest_request() r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = rest_request_ambassador("mymodel",None,API_AMBASSADOR) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = grpc_request_ambassador2("mymodel",None,API_AMBASSADOR) print(r) r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC) print(r) run('cd my-model && ks delete default -c mymodel && ks component rm mymodel', shell=True) # Test AB Test model helm script with 4 API methods def test_abtest_model(self): run('cd my-model && ks delete default && ks component rm mymodel', shell=True) run('kubectl delete sdep --all', shell=True) run('cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c myabtest', shell=True) wait_for_rollout("myabtest-myabtest-41de5b8") wait_for_rollout("myabtest-myabtest-df66c5c") r = initial_rest_request() r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = rest_request_ambassador("myabtest",None,API_AMBASSADOR) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = grpc_request_ambassador2("myabtest",None,API_AMBASSADOR) print(r) r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC) print(r) run('cd my-model && ks delete default -c myabtest && ks component rm myabtest', shell=True) # Test MAB Test model helm script with 4 API methods def test_mab_model(self): run('cd my-model && ks delete default && ks component rm mymab', shell=True) run('kubectl delete sdep --all', shell=True) run('cd my-model && ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymab', shell=True) wait_for_rollout("mymab-mymab-41de5b8") wait_for_rollout("mymab-mymab-b8038b2") wait_for_rollout("mymab-mymab-df66c5c") r = initial_rest_request() r = rest_request_api_gateway("oauth-key","oauth-secret",None,API_GATEWAY_REST) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = rest_request_ambassador("mymab",None,API_AMBASSADOR) res = r.json() print(res) assert r.status_code == 200 assert len(r.json()["data"]["tensor"]["values"]) == 1 r = grpc_request_ambassador2("mymab",None,API_AMBASSADOR) print(r) r = grpc_request_api_gateway2("oauth-key","oauth-secret",None,rest_endpoint=API_GATEWAY_REST,grpc_endpoint=API_GATEWAY_GRPC) print(r) run('cd my-model && ks delete default && ks component rm mymab', shell=True)
[((1033, 1078), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', (['"""seldon_java_images"""'], {}), "('seldon_java_images')\n", (1056, 1078), False, 'import pytest\n'), ((1080, 1138), 'pytest.mark.usefixtures', 'pytest.mark.usefixtures', (['"""single_namespace_seldon_ksonnet"""'], {}), "('single_namespace_seldon_ksonnet')\n", (1103, 1138), False, 'import pytest\n'), ((178, 233), 'subprocess.run', 'run', (["('kubectl get deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl get deploy/' + deploymentName, shell=True)\n", (181, 233), False, 'from subprocess import run, Popen\n'), ((402, 468), 'subprocess.run', 'run', (["('kubectl rollout status deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl rollout status deploy/' + deploymentName, shell=True)\n", (405, 468), False, 'from subprocess import run, Popen\n'), ((271, 284), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (281, 284), False, 'import time\n'), ((299, 354), 'subprocess.run', 'run', (["('kubectl get deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl get deploy/' + deploymentName, shell=True)\n", (302, 354), False, 'from subprocess import run, Popen\n'), ((505, 518), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (515, 518), False, 'import time\n'), ((533, 599), 'subprocess.run', 'run', (["('kubectl rollout status deploy/' + deploymentName)"], {'shell': '(True)'}), "('kubectl rollout status deploy/' + deploymentName, shell=True)\n", (536, 599), False, 'from subprocess import run, Popen\n'), ((763, 776), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (773, 776), False, 'import time\n'), ((1274, 1352), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymodel', shell=True)\n", (1277, 1352), False, 'from subprocess import run, Popen\n'), ((1361, 1405), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (1364, 1405), False, 'from subprocess import run, Popen\n'), ((1414, 1629), 'subprocess.run', 'run', (['"""cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel"""'], {'shell': '(True)', 'check': '(True)'}), "('cd my-model && ks generate seldon-serve-simple-v1alpha2 mymodel --image seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymodel'\n , shell=True, check=True)\n", (1417, 1629), False, 'from subprocess import run, Popen\n'), ((2404, 2497), 'subprocess.run', 'run', (['"""cd my-model && ks delete default -c mymodel && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default -c mymodel && ks component rm mymodel',\n shell=True)\n", (2407, 2497), False, 'from subprocess import run, Popen\n'), ((2600, 2678), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymodel"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymodel', shell=True)\n", (2603, 2678), False, 'from subprocess import run, Popen\n'), ((2687, 2731), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (2690, 2731), False, 'from subprocess import run, Popen\n'), ((2740, 2982), 'subprocess.run', 'run', (['"""cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c myabtest"""'], {'shell': '(True)'}), "('cd my-model && ks generate seldon-abtest-v1alpha2 myabtest --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c myabtest'\n , shell=True)\n", (2743, 2982), False, 'from subprocess import run, Popen\n'), ((3816, 3917), 'subprocess.run', 'run', (['"""cd my-model && ks delete default -c myabtest && ks component rm myabtest"""'], {'shell': '(True)'}), "('cd my-model && ks delete default -c myabtest && ks component rm myabtest'\n , shell=True)\n", (3819, 3917), False, 'from subprocess import run, Popen\n'), ((4009, 4085), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymab"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymab', shell=True)\n", (4012, 4085), False, 'from subprocess import run, Popen\n'), ((4094, 4138), 'subprocess.run', 'run', (['"""kubectl delete sdep --all"""'], {'shell': '(True)'}), "('kubectl delete sdep --all', shell=True)\n", (4097, 4138), False, 'from subprocess import run, Popen\n'), ((4147, 4384), 'subprocess.run', 'run', (['"""cd my-model && ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymab"""'], {'shell': '(True)'}), "('cd my-model && ks generate seldon-mab-v1alpha2 mymab --imageA seldonio/mock_classifier:1.0 --imageB seldonio/mock_classifier:1.0 --oauthKey=oauth-key --oauthSecret=oauth-secret && ks apply default -c mymab'\n , shell=True)\n", (4150, 4384), False, 'from subprocess import run, Popen\n'), ((5257, 5333), 'subprocess.run', 'run', (['"""cd my-model && ks delete default && ks component rm mymab"""'], {'shell': '(True)'}), "('cd my-model && ks delete default && ks component rm mymab', shell=True)\n", (5260, 5333), False, 'from subprocess import run, Popen\n'), ((913, 926), 'time.sleep', 'time.sleep', (['(5)'], {}), '(5)\n', (923, 926), False, 'import time\n')]
enthought/etsproxy
enthought/envisage/safeweakref.py
4aafd628611ebf7fe8311c9d1a0abcf7f7bb5347
# proxy module from __future__ import absolute_import from envisage.safeweakref import *
[]
alissonmuller/django-group-by
test_app/models.py
645c36ad2c3ab1f4691de6fcc04fed8b5d7ef78d
from django.db import models from .query import BookQuerySet class Book(models.Model): objects = BookQuerySet.as_manager() title = models.CharField(max_length=50) publication_date = models.DateTimeField() author = models.ForeignKey('Author') genres = models.ManyToManyField('Genre') class Author(models.Model): name = models.CharField(max_length=50) nationality = models.ForeignKey('Nation', null=True) class Genre(models.Model): name = models.CharField(max_length=50) class Nation(models.Model): name = models.CharField(max_length=50) demonym = models.CharField(max_length=50)
[((143, 174), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (159, 174), False, 'from django.db import models\n'), ((198, 220), 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {}), '()\n', (218, 220), False, 'from django.db import models\n'), ((234, 261), 'django.db.models.ForeignKey', 'models.ForeignKey', (['"""Author"""'], {}), "('Author')\n", (251, 261), False, 'from django.db import models\n'), ((275, 306), 'django.db.models.ManyToManyField', 'models.ManyToManyField', (['"""Genre"""'], {}), "('Genre')\n", (297, 306), False, 'from django.db import models\n'), ((348, 379), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (364, 379), False, 'from django.db import models\n'), ((398, 436), 'django.db.models.ForeignKey', 'models.ForeignKey', (['"""Nation"""'], {'null': '(True)'}), "('Nation', null=True)\n", (415, 436), False, 'from django.db import models\n'), ((477, 508), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (493, 508), False, 'from django.db import models\n'), ((550, 581), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (566, 581), False, 'from django.db import models\n'), ((596, 627), 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)'}), '(max_length=50)\n', (612, 627), False, 'from django.db import models\n')]
whj363636/CamDrop
ResNet/dropblock.py
f8af8c200665145f112b59348f60fc4cf80f04ec
# -*- coding: utf-8 -*- # File: dropblock.py from __future__ import absolute_import from __future__ import division from __future__ import print_function import re import six # from tensorpack.tfutils.compat import tfv1 as tf # this should be avoided first in model code from tensorpack.tfutils.tower import get_current_tower_context from tensorpack.models import GlobalAvgPooling, FullyConnected import tensorflow as tf __all__ = ['dropblock', 'dropblock2','dropblock3','dropblock4'] # 1: paper baseline; 2: group dropout; 3: group soft-dropout; 4: Uout group dropout def dropblock(net, keep_prob, dropblock_size, gap_w=None, label=None, G=None, CG=None, data_format='channels_first'): """DropBlock: a regularization method for convolutional neural networks. DropBlock is a form of structured dropout, where units in a contiguous region of a feature map are dropped together. DropBlock works better than dropout on convolutional layers due to the fact that activation units in convolutional layers are spatially correlated. See https://arxiv.org/pdf/1810.12890.pdf for details. Args: net: `Tensor` input tensor. is_training: `bool` for whether the model is training. keep_prob: `float` or `Tensor` keep_prob parameter of DropBlock. "None" means no DropBlock. dropblock_size: `int` size of blocks to be dropped by DropBlock. data_format: `str` either "channels_first" for `[batch, channels, height, width]` or "channels_last for `[batch, height, width, channels]`. Returns: A version of input tensor with DropBlock applied. Raises: if width and height of the input tensor are not equal. """ ctx = get_current_tower_context() is_training = bool(ctx.is_training) if not is_training or keep_prob is None: return net tf.logging.info('Applying DropBlock: dropblock_size {}, net.shape {}'.format(dropblock_size, net.shape)) if data_format == 'channels_last': _, width, height, _ = net.get_shape().as_list() else: _, _, width, height = net.get_shape().as_list() if width != height: raise ValueError('Input tensor with width!=height is not supported.') dropblock_size = min(dropblock_size, width) # seed_drop_rate is the gamma parameter of DropBlcok. seed_drop_rate = (1.0 - keep_prob) * width**2 / dropblock_size**2 / ( width - dropblock_size + 1)**2 # Forces the block to be inside the feature map. w_i, h_i = tf.meshgrid(tf.range(width), tf.range(width)) valid_block_center = tf.logical_and( tf.logical_and(w_i >= int(dropblock_size // 2), w_i < width - (dropblock_size - 1) // 2), tf.logical_and(h_i >= int(dropblock_size // 2), h_i < width - (dropblock_size - 1) // 2)) valid_block_center = tf.expand_dims(valid_block_center, 0) valid_block_center = tf.expand_dims( valid_block_center, -1 if data_format == 'channels_last' else 0) randnoise = tf.random_uniform(tf.shape(net), dtype=tf.float32) block_pattern = (1 - tf.cast(valid_block_center, dtype=tf.float32) + tf.cast( (1 - seed_drop_rate), dtype=tf.float32) + randnoise) >= 1 block_pattern = tf.cast(block_pattern, dtype=tf.float32) if dropblock_size == width: block_pattern = tf.reduce_min( block_pattern, axis=[1, 2] if data_format == 'channels_last' else [2, 3], keepdims=True) else: if data_format == 'channels_last': ksize = [1, dropblock_size, dropblock_size, 1] else: ksize = [1, 1, dropblock_size, dropblock_size] block_pattern = -tf.nn.max_pool( -block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding='SAME', data_format='NHWC' if data_format == 'channels_last' else 'NCHW') percent_ones = tf.cast(tf.reduce_sum((block_pattern)), tf.float32) / tf.cast( tf.size(block_pattern), tf.float32) net = net / tf.cast(percent_ones, net.dtype) * tf.cast( block_pattern, net.dtype) return net def dropblock2(net, keep_prob, dropblock_size, G=None, CG=None, data_format='channels_first'): """ mimic GN """ ctx = get_current_tower_context() is_training = bool(ctx.is_training) if not is_training or keep_prob is None: return net tf.logging.info('Applying DropBlock: dropblock_size {}, net.shape {}'.format(dropblock_size, net.shape)) if data_format == 'channels_last': N, height, width, C = net.get_shape().as_list() else: N, C, height, width = net.get_shape().as_list() N = tf.shape(net)[0] if width != height: raise ValueError('Input tensor with width!=height is not supported.') if G == None: G = C // CG if CG == None: CG = C // G net = tf.reshape(net, [N, G, CG, height, width]) dropblock_size = min(dropblock_size, width) # seed_drop_rate is the gamma parameter of DropBlcok. # seed_drop_rate = (1.0 - keep_prob) * width**2 * G**2 / (C * dropblock_size**2) / (C * (width - dropblock_size + 1)**2) seed_drop_rate = (1.0 - keep_prob) * width**2 / dropblock_size**2 / (width - dropblock_size + 1)**2 # Forces the block to be inside the feature map. w_i, h_i = tf.meshgrid(tf.range(width), tf.range(width)) valid_block_center = tf.logical_and( tf.logical_and(w_i >= int(dropblock_size // 2), w_i < width - (dropblock_size - 1) // 2), tf.logical_and(h_i >= int(dropblock_size // 2), h_i < width - (dropblock_size - 1) // 2)) valid_block_center = tf.expand_dims(valid_block_center, 0) # for depth valid_block_center = tf.expand_dims(valid_block_center, 0) # for batch valid_block_center = tf.expand_dims(valid_block_center, 0) # for channel randnoise = tf.random_uniform([N, G, 1, width, height], dtype=tf.float32) block_pattern = (1 - tf.cast(valid_block_center, dtype=tf.float32) + tf.cast( (1 - seed_drop_rate), dtype=tf.float32) + randnoise) >= 1 block_pattern = tf.cast(block_pattern, dtype=tf.float32) if dropblock_size == width: block_pattern = tf.reduce_min(block_pattern, axis=[2, 3, 4], keepdims=True) else: ksize = [1, 1, dropblock_size, dropblock_size] block_pattern = tf.reduce_max(-block_pattern, reduction_indices=[2]) block_pattern = -tf.nn.max_pool(block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding='SAME', data_format='NCHW') block_pattern = tf.expand_dims(block_pattern, 2) percent_ones = tf.cast(tf.reduce_sum((block_pattern)), tf.float32) / tf.cast(tf.size(block_pattern), tf.float32) net = net / tf.cast(percent_ones, net.dtype) * tf.cast(block_pattern, net.dtype) net = tf.reshape(net, [N, height, width, C]) if data_format == 'channels_last' else tf.reshape(net, [N, C, height, width]) return net def CamDrop(net, keep_prob, dropblock_size, flag=None, label=None, G=None, CG=None, data_format='channels_first'): '''CamDrop''' def _get_cam(net, label, flag, dropblock_size, data_format='channels_first'): ''' net: [N, C, H, W] gap_w : [gap_C, num_of_class] ''' if data_format == 'channels_last': N, height, width, C = net.get_shape().as_list() else: N, C, height, width = net.get_shape().as_list() N = tf.shape(net)[0] gap_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'linear/W') if flag > 0 else None if not gap_w is None: gap_w = tf.convert_to_tensor(gap_w, tf.float32) gap_C, num = tf.squeeze(gap_w, 0).get_shape().as_list() # [gap_C, num] gap_w = tf.reshape(gap_w, [C, gap_C//C, num]) gap_w = tf.reduce_mean(gap_w, reduction_indices=[1]) # [C, num] label = tf.gather(tf.transpose(gap_w), label) # [N, C] # spatial weights = tf.expand_dims(label, 2) # [N, C, 1] net = tf.reshape(net, [N, height*width, C]) if data_format == 'channels_last' else tf.reshape(net, [N, C, height*width]) cam = tf.matmul(weights, net, transpose_a=True) # [N, 1, width*height] # spt_mask = tf.not_equal(cam, tf.reduce_max(cam, reduction_indices=[2], keepdims=True)) # cam = tf.reshape(cam, [N, height, width, 1]) if data_format == 'channels_last' else tf.reshape(cam, [N, 1, height, width]) # cam = tf.nn.avg_pool(cam, ksize=[1, 1, dropblock_size, dropblock_size], strides=[1, 1, 1, 1], padding='VALID', data_format='NCHW') # left_or_top = (dropblock_size-1) // 2 # right_or_bot = left_or_top if dropblock_size % 2 == 1 else dropblock_size-left_or_top-1 # cam = tf.pad(cam, [[0, 0], [0, 0], [left_or_top, right_or_bot], [left_or_top, right_or_bot]]) # cam = tf.reshape(cam, [N, height*width, 1]) if data_format == 'channels_last' else tf.reshape(cam, [N, 1, height*width]) k = tf.cast(height*width/dropblock_size**2, tf.int32) topk, _ = tf.math.top_k(cam, k=k) # [N, 1, k] topk = tf.gather(topk, indices=[k-1], axis=-1) # [N, 1, 1] spt_mask = (cam < topk) spt_mask = tf.reshape(spt_mask, [N, height, width, 1]) if data_format == 'channels_last' else tf.reshape(spt_mask, [N, 1, height, width]) # channel k = tf.cast(C/8, tf.int32) topk, _ = tf.math.top_k(label, k=k+1) # [N, k] topk = tf.gather(topk, indices=k, axis=1) # [N, 1] topk = tf.expand_dims(topk, 1) # [N, C, 1] chan_mask = (label < topk) chan_mask = tf.expand_dims(chan_mask, 2) # [N, C, 1] chan_mask = tf.expand_dims(chan_mask, 2) # [N, C, 1, 1] cam_mask = tf.logical_or(spt_mask, chan_mask) # chan_mask = tf.reshape(tf.nn.softmax(cam), [N*C, height*width]) if data_format == 'channels_last' else tf.reshape(tf.nn.softmax(cam), [N*C, height*width]) # chan_mask = tf.reshape(cam, [N*C, height*width]) if data_format == 'channels_last' else tf.reshape(cam, [N*C, height*width]) # chan_mask = tf.reshape(tf.nn.sigmoid(cam), [N, height, width, 1]) if data_format == 'channels_last' else tf.reshape(tf.nn.sigmoid(cam), [N, 1, height, width]) else: cam_mask = False return cam_mask # def _get_gradcam(net, cost=None, gap_w=None, data_format='channels_first'): # # Conv layer tensor [?,2048,10,10] # def _compute_gradients(tensor, var_list): # grads = tf.gradients(tensor, var_list) # return [grad if grad is not None else tf.zeros_like(var) # for var, grad in zip(var_list, grads)] # # grads = tf.gradients(cost, net)[0] # if not gap_w is None: # # Normalizing the gradients # if data_format == 'channels_last': # N, height, width, C = net.get_shape().as_list() # else: # N, C, height, width = net.get_shape().as_list() # N = tf.shape(net)[0] # grads = _compute_gradients(cost, [net])[0] # norm_grads = tf.divide(grads, tf.sqrt(tf.reduce_mean(tf.square(grads), reduction_indices=[2,3], keepdims=True)) + tf.constant(1e-5)) # weights = tf.reduce_mean(norm_grads, reduction_indices=[2,3]) # [N, C] # weights = tf.expand_dims(weights, 2) # [N, C, 1] # net = tf.reshape(net, [N, height*width, C]) if data_format == 'channels_last' else tf.reshape(net, [N, C, height*width]) # # cam_mean = 1 + tf.matmul(net, weights, transpose_a=True) # [N, width*height, 1] # cam_mean = tf.maximum(tf.matmul(weights, net, transpose_a=True), 0) # [N, 1, width*height] # cam_chan = tf.maximum(tf.multiply(net, weights), 0) # [N, C, width*height] # cam = cam_mean*cam_chan # # Passing through ReLU # cam = cam / tf.reduce_max(cam, reduction_indices=[1,2], keepdims=True) # cam = tf.reshape(cam, [N, height, width, C]) if data_format == 'channels_last' else tf.reshape(cam, [N, C, height, width]) # else: # cam = 0. # return cam # def _gumbel_softmax(logits, tau, shape, seed_drop_rate, eps=1e-20): # if logits == False: # return logits # U = tf.random_uniform(tf.shape(logits), minval=0, maxval=1) # y = logits - tf.log(-tf.log(U + eps) + eps) # cam_mask = tf.nn.softmax(y / tau) # topk, _ = tf.math.top_k(cam_mask, k=tf.cast(seed_drop_rate*shape[-1], tf.int32)) # [N, 1] # topk = tf.gather(topk, indices=tf.cast(seed_drop_rate*shape[-1], tf.int32)-1, axis=1) # topk = tf.expand_dims(topk, 1) # [N, C, 1] # cam_mask = (cam_mask < topk) # # cam_mask = tf.cast(tf.equal(cam_mask, tf.reduce_max(cam_mask, reduction_indices=[1], keepdims=True)), tf.float32) # cam_mask = tf.expand_dims(cam_mask, 2) # [N, C, 1] # cam_mask = tf.expand_dims(cam_mask, 2) # [N, C, 1, 1] # return cam_mask ctx = get_current_tower_context() is_training = bool(ctx.is_training) if not is_training or keep_prob is None: return net tf.logging.info('Applying DropBlock: dropblock_size {}, net.shape {}'.format(dropblock_size, net.shape)) if data_format == 'channels_last': _, width, height, C = net.get_shape().as_list() else: _, C, width, height = net.get_shape().as_list() if width != height: raise ValueError('Input tensor with width!=height is not supported.') N = tf.shape(net)[0] dropblock_size = min(dropblock_size, width) # seed_drop_rate is the gamma parameter of DropBlcok. seed_drop_rate = (1.0 - keep_prob) * width**2 / dropblock_size**2 / (width - dropblock_size + 1)**2 cam_mask = _get_cam(net, label, flag, dropblock_size, data_format) # Forces the block to be inside the feature map. w_i, h_i = tf.meshgrid(tf.range(width), tf.range(width)) valid_block_center = tf.logical_and( tf.logical_and(w_i >= int(dropblock_size // 2), w_i < width - (dropblock_size - 1) // 2), tf.logical_and(h_i >= int(dropblock_size // 2), h_i < width - (dropblock_size - 1) // 2)) valid_block_center = tf.expand_dims(valid_block_center, 0) valid_block_center = tf.expand_dims(valid_block_center, -1 if data_format == 'channels_last' else 0) randnoise = tf.random_uniform(tf.shape(net), dtype=tf.float32) block_pattern = (1 - tf.cast(valid_block_center, dtype=tf.float32) + tf.cast((1 - seed_drop_rate), dtype=tf.float32) + randnoise) >= 1 block_pattern = tf.logical_or(block_pattern, cam_mask) block_pattern = tf.cast(block_pattern, dtype=tf.float32) if dropblock_size == width: block_pattern = tf.reduce_min( block_pattern, axis=[1, 2] if data_format == 'channels_last' else [2, 3], keepdims=True) else: if data_format == 'channels_last': ksize = [1, dropblock_size, dropblock_size, 1] else: ksize = [1, 1, dropblock_size, dropblock_size] block_pattern = -tf.nn.max_pool( -block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding='SAME', data_format='NHWC' if data_format == 'channels_last' else 'NCHW') percent_ones = tf.cast(tf.reduce_sum((block_pattern)), tf.float32) / tf.cast(tf.size(block_pattern), tf.float32) net = net / tf.cast(percent_ones, net.dtype) * tf.cast(block_pattern, net.dtype) return net
[((1674, 1701), 'tensorpack.tfutils.tower.get_current_tower_context', 'get_current_tower_context', ([], {}), '()\n', (1699, 1701), False, 'from tensorpack.tfutils.tower import get_current_tower_context\n'), ((2773, 2810), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', '(0)'], {}), '(valid_block_center, 0)\n', (2787, 2810), True, 'import tensorflow as tf\n'), ((2834, 2913), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', "(-1 if data_format == 'channels_last' else 0)"], {}), "(valid_block_center, -1 if data_format == 'channels_last' else 0)\n", (2848, 2913), True, 'import tensorflow as tf\n'), ((3149, 3189), 'tensorflow.cast', 'tf.cast', (['block_pattern'], {'dtype': 'tf.float32'}), '(block_pattern, dtype=tf.float32)\n', (3156, 3189), True, 'import tensorflow as tf\n'), ((4075, 4102), 'tensorpack.tfutils.tower.get_current_tower_context', 'get_current_tower_context', ([], {}), '()\n', (4100, 4102), False, 'from tensorpack.tfutils.tower import get_current_tower_context\n'), ((4644, 4686), 'tensorflow.reshape', 'tf.reshape', (['net', '[N, G, CG, height, width]'], {}), '(net, [N, G, CG, height, width])\n', (4654, 4686), True, 'import tensorflow as tf\n'), ((5424, 5461), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', '(0)'], {}), '(valid_block_center, 0)\n', (5438, 5461), True, 'import tensorflow as tf\n'), ((5497, 5534), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', '(0)'], {}), '(valid_block_center, 0)\n', (5511, 5534), True, 'import tensorflow as tf\n'), ((5570, 5607), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', '(0)'], {}), '(valid_block_center, 0)\n', (5584, 5607), True, 'import tensorflow as tf\n'), ((5636, 5697), 'tensorflow.random_uniform', 'tf.random_uniform', (['[N, G, 1, width, height]'], {'dtype': 'tf.float32'}), '([N, G, 1, width, height], dtype=tf.float32)\n', (5653, 5697), True, 'import tensorflow as tf\n'), ((5861, 5901), 'tensorflow.cast', 'tf.cast', (['block_pattern'], {'dtype': 'tf.float32'}), '(block_pattern, dtype=tf.float32)\n', (5868, 5901), True, 'import tensorflow as tf\n'), ((12391, 12418), 'tensorpack.tfutils.tower.get_current_tower_context', 'get_current_tower_context', ([], {}), '()\n', (12416, 12418), False, 'from tensorpack.tfutils.tower import get_current_tower_context\n'), ((13577, 13614), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', '(0)'], {}), '(valid_block_center, 0)\n', (13591, 13614), True, 'import tensorflow as tf\n'), ((13638, 13717), 'tensorflow.expand_dims', 'tf.expand_dims', (['valid_block_center', "(-1 if data_format == 'channels_last' else 0)"], {}), "(valid_block_center, -1 if data_format == 'channels_last' else 0)\n", (13652, 13717), True, 'import tensorflow as tf\n'), ((13939, 13977), 'tensorflow.logical_or', 'tf.logical_or', (['block_pattern', 'cam_mask'], {}), '(block_pattern, cam_mask)\n', (13952, 13977), True, 'import tensorflow as tf\n'), ((13996, 14036), 'tensorflow.cast', 'tf.cast', (['block_pattern'], {'dtype': 'tf.float32'}), '(block_pattern, dtype=tf.float32)\n', (14003, 14036), True, 'import tensorflow as tf\n'), ((2442, 2457), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (2450, 2457), True, 'import tensorflow as tf\n'), ((2459, 2474), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (2467, 2474), True, 'import tensorflow as tf\n'), ((2954, 2967), 'tensorflow.shape', 'tf.shape', (['net'], {}), '(net)\n', (2962, 2967), True, 'import tensorflow as tf\n'), ((3241, 3347), 'tensorflow.reduce_min', 'tf.reduce_min', (['block_pattern'], {'axis': "([1, 2] if data_format == 'channels_last' else [2, 3])", 'keepdims': '(True)'}), "(block_pattern, axis=[1, 2] if data_format == 'channels_last' else\n [2, 3], keepdims=True)\n", (3254, 3347), True, 'import tensorflow as tf\n'), ((3891, 3924), 'tensorflow.cast', 'tf.cast', (['block_pattern', 'net.dtype'], {}), '(block_pattern, net.dtype)\n', (3898, 3924), True, 'import tensorflow as tf\n'), ((4464, 4477), 'tensorflow.shape', 'tf.shape', (['net'], {}), '(net)\n', (4472, 4477), True, 'import tensorflow as tf\n'), ((5093, 5108), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (5101, 5108), True, 'import tensorflow as tf\n'), ((5110, 5125), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (5118, 5125), True, 'import tensorflow as tf\n'), ((5953, 6012), 'tensorflow.reduce_min', 'tf.reduce_min', (['block_pattern'], {'axis': '[2, 3, 4]', 'keepdims': '(True)'}), '(block_pattern, axis=[2, 3, 4], keepdims=True)\n', (5966, 6012), True, 'import tensorflow as tf\n'), ((6097, 6149), 'tensorflow.reduce_max', 'tf.reduce_max', (['(-block_pattern)'], {'reduction_indices': '[2]'}), '(-block_pattern, reduction_indices=[2])\n', (6110, 6149), True, 'import tensorflow as tf\n'), ((6292, 6324), 'tensorflow.expand_dims', 'tf.expand_dims', (['block_pattern', '(2)'], {}), '(block_pattern, 2)\n', (6306, 6324), True, 'import tensorflow as tf\n'), ((6490, 6523), 'tensorflow.cast', 'tf.cast', (['block_pattern', 'net.dtype'], {}), '(block_pattern, net.dtype)\n', (6497, 6523), True, 'import tensorflow as tf\n'), ((6532, 6570), 'tensorflow.reshape', 'tf.reshape', (['net', '[N, height, width, C]'], {}), '(net, [N, height, width, C])\n', (6542, 6570), True, 'import tensorflow as tf\n'), ((6610, 6648), 'tensorflow.reshape', 'tf.reshape', (['net', '[N, C, height, width]'], {}), '(net, [N, C, height, width])\n', (6620, 6648), True, 'import tensorflow as tf\n'), ((12876, 12889), 'tensorflow.shape', 'tf.shape', (['net'], {}), '(net)\n', (12884, 12889), True, 'import tensorflow as tf\n'), ((13246, 13261), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (13254, 13261), True, 'import tensorflow as tf\n'), ((13263, 13278), 'tensorflow.range', 'tf.range', (['width'], {}), '(width)\n', (13271, 13278), True, 'import tensorflow as tf\n'), ((13751, 13764), 'tensorflow.shape', 'tf.shape', (['net'], {}), '(net)\n', (13759, 13764), True, 'import tensorflow as tf\n'), ((14088, 14194), 'tensorflow.reduce_min', 'tf.reduce_min', (['block_pattern'], {'axis': "([1, 2] if data_format == 'channels_last' else [2, 3])", 'keepdims': '(True)'}), "(block_pattern, axis=[1, 2] if data_format == 'channels_last' else\n [2, 3], keepdims=True)\n", (14101, 14194), True, 'import tensorflow as tf\n'), ((14731, 14764), 'tensorflow.cast', 'tf.cast', (['block_pattern', 'net.dtype'], {}), '(block_pattern, net.dtype)\n', (14738, 14764), True, 'import tensorflow as tf\n'), ((3553, 3705), 'tensorflow.nn.max_pool', 'tf.nn.max_pool', (['(-block_pattern)'], {'ksize': 'ksize', 'strides': '[1, 1, 1, 1]', 'padding': '"""SAME"""', 'data_format': "('NHWC' if data_format == 'channels_last' else 'NCHW')"}), "(-block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding=\n 'SAME', data_format='NHWC' if data_format == 'channels_last' else 'NCHW')\n", (3567, 3705), True, 'import tensorflow as tf\n'), ((3744, 3772), 'tensorflow.reduce_sum', 'tf.reduce_sum', (['block_pattern'], {}), '(block_pattern)\n', (3757, 3772), True, 'import tensorflow as tf\n'), ((3805, 3827), 'tensorflow.size', 'tf.size', (['block_pattern'], {}), '(block_pattern)\n', (3812, 3827), True, 'import tensorflow as tf\n'), ((3856, 3888), 'tensorflow.cast', 'tf.cast', (['percent_ones', 'net.dtype'], {}), '(percent_ones, net.dtype)\n', (3863, 3888), True, 'import tensorflow as tf\n'), ((6171, 6276), 'tensorflow.nn.max_pool', 'tf.nn.max_pool', (['block_pattern'], {'ksize': 'ksize', 'strides': '[1, 1, 1, 1]', 'padding': '"""SAME"""', 'data_format': '"""NCHW"""'}), "(block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding=\n 'SAME', data_format='NCHW')\n", (6185, 6276), True, 'import tensorflow as tf\n'), ((6351, 6379), 'tensorflow.reduce_sum', 'tf.reduce_sum', (['block_pattern'], {}), '(block_pattern)\n', (6364, 6379), True, 'import tensorflow as tf\n'), ((6405, 6427), 'tensorflow.size', 'tf.size', (['block_pattern'], {}), '(block_pattern)\n', (6412, 6427), True, 'import tensorflow as tf\n'), ((6455, 6487), 'tensorflow.cast', 'tf.cast', (['percent_ones', 'net.dtype'], {}), '(percent_ones, net.dtype)\n', (6462, 6487), True, 'import tensorflow as tf\n'), ((7112, 7125), 'tensorflow.shape', 'tf.shape', (['net'], {}), '(net)\n', (7120, 7125), True, 'import tensorflow as tf\n'), ((7143, 7206), 'tensorflow.get_collection', 'tf.get_collection', (['tf.GraphKeys.TRAINABLE_VARIABLES', '"""linear/W"""'], {}), "(tf.GraphKeys.TRAINABLE_VARIABLES, 'linear/W')\n", (7160, 7206), True, 'import tensorflow as tf\n'), ((7270, 7309), 'tensorflow.convert_to_tensor', 'tf.convert_to_tensor', (['gap_w', 'tf.float32'], {}), '(gap_w, tf.float32)\n', (7290, 7309), True, 'import tensorflow as tf\n'), ((7401, 7440), 'tensorflow.reshape', 'tf.reshape', (['gap_w', '[C, gap_C // C, num]'], {}), '(gap_w, [C, gap_C // C, num])\n', (7411, 7440), True, 'import tensorflow as tf\n'), ((7453, 7497), 'tensorflow.reduce_mean', 'tf.reduce_mean', (['gap_w'], {'reduction_indices': '[1]'}), '(gap_w, reduction_indices=[1])\n', (7467, 7497), True, 'import tensorflow as tf\n'), ((7604, 7628), 'tensorflow.expand_dims', 'tf.expand_dims', (['label', '(2)'], {}), '(label, 2)\n', (7618, 7628), True, 'import tensorflow as tf\n'), ((7780, 7821), 'tensorflow.matmul', 'tf.matmul', (['weights', 'net'], {'transpose_a': '(True)'}), '(weights, net, transpose_a=True)\n', (7789, 7821), True, 'import tensorflow as tf\n'), ((8597, 8652), 'tensorflow.cast', 'tf.cast', (['(height * width / dropblock_size ** 2)', 'tf.int32'], {}), '(height * width / dropblock_size ** 2, tf.int32)\n', (8604, 8652), True, 'import tensorflow as tf\n'), ((8663, 8686), 'tensorflow.math.top_k', 'tf.math.top_k', (['cam'], {'k': 'k'}), '(cam, k=k)\n', (8676, 8686), True, 'import tensorflow as tf\n'), ((8712, 8753), 'tensorflow.gather', 'tf.gather', (['topk'], {'indices': '[k - 1]', 'axis': '(-1)'}), '(topk, indices=[k - 1], axis=-1)\n', (8721, 8753), True, 'import tensorflow as tf\n'), ((8966, 8990), 'tensorflow.cast', 'tf.cast', (['(C / 8)', 'tf.int32'], {}), '(C / 8, tf.int32)\n', (8973, 8990), True, 'import tensorflow as tf\n'), ((9005, 9034), 'tensorflow.math.top_k', 'tf.math.top_k', (['label'], {'k': '(k + 1)'}), '(label, k=k + 1)\n', (9018, 9034), True, 'import tensorflow as tf\n'), ((9055, 9089), 'tensorflow.gather', 'tf.gather', (['topk'], {'indices': 'k', 'axis': '(1)'}), '(topk, indices=k, axis=1)\n', (9064, 9089), True, 'import tensorflow as tf\n'), ((9112, 9135), 'tensorflow.expand_dims', 'tf.expand_dims', (['topk', '(1)'], {}), '(topk, 1)\n', (9126, 9135), True, 'import tensorflow as tf\n'), ((9199, 9227), 'tensorflow.expand_dims', 'tf.expand_dims', (['chan_mask', '(2)'], {}), '(chan_mask, 2)\n', (9213, 9227), True, 'import tensorflow as tf\n'), ((9258, 9286), 'tensorflow.expand_dims', 'tf.expand_dims', (['chan_mask', '(2)'], {}), '(chan_mask, 2)\n', (9272, 9286), True, 'import tensorflow as tf\n'), ((9320, 9354), 'tensorflow.logical_or', 'tf.logical_or', (['spt_mask', 'chan_mask'], {}), '(spt_mask, chan_mask)\n', (9333, 9354), True, 'import tensorflow as tf\n'), ((14400, 14552), 'tensorflow.nn.max_pool', 'tf.nn.max_pool', (['(-block_pattern)'], {'ksize': 'ksize', 'strides': '[1, 1, 1, 1]', 'padding': '"""SAME"""', 'data_format': "('NHWC' if data_format == 'channels_last' else 'NCHW')"}), "(-block_pattern, ksize=ksize, strides=[1, 1, 1, 1], padding=\n 'SAME', data_format='NHWC' if data_format == 'channels_last' else 'NCHW')\n", (14414, 14552), True, 'import tensorflow as tf\n'), ((14591, 14619), 'tensorflow.reduce_sum', 'tf.reduce_sum', (['block_pattern'], {}), '(block_pattern)\n', (14604, 14619), True, 'import tensorflow as tf\n'), ((14645, 14667), 'tensorflow.size', 'tf.size', (['block_pattern'], {}), '(block_pattern)\n', (14652, 14667), True, 'import tensorflow as tf\n'), ((14696, 14728), 'tensorflow.cast', 'tf.cast', (['percent_ones', 'net.dtype'], {}), '(percent_ones, net.dtype)\n', (14703, 14728), True, 'import tensorflow as tf\n'), ((3058, 3103), 'tensorflow.cast', 'tf.cast', (['(1 - seed_drop_rate)'], {'dtype': 'tf.float32'}), '(1 - seed_drop_rate, dtype=tf.float32)\n', (3065, 3103), True, 'import tensorflow as tf\n'), ((5770, 5815), 'tensorflow.cast', 'tf.cast', (['(1 - seed_drop_rate)'], {'dtype': 'tf.float32'}), '(1 - seed_drop_rate, dtype=tf.float32)\n', (5777, 5815), True, 'import tensorflow as tf\n'), ((7533, 7552), 'tensorflow.transpose', 'tf.transpose', (['gap_w'], {}), '(gap_w)\n', (7545, 7552), True, 'import tensorflow as tf\n'), ((7653, 7692), 'tensorflow.reshape', 'tf.reshape', (['net', '[N, height * width, C]'], {}), '(net, [N, height * width, C])\n', (7663, 7692), True, 'import tensorflow as tf\n'), ((7730, 7769), 'tensorflow.reshape', 'tf.reshape', (['net', '[N, C, height * width]'], {}), '(net, [N, C, height * width])\n', (7740, 7769), True, 'import tensorflow as tf\n'), ((8812, 8855), 'tensorflow.reshape', 'tf.reshape', (['spt_mask', '[N, height, width, 1]'], {}), '(spt_mask, [N, height, width, 1])\n', (8822, 8855), True, 'import tensorflow as tf\n'), ((8895, 8938), 'tensorflow.reshape', 'tf.reshape', (['spt_mask', '[N, 1, height, width]'], {}), '(spt_mask, [N, 1, height, width])\n', (8905, 8938), True, 'import tensorflow as tf\n'), ((13855, 13900), 'tensorflow.cast', 'tf.cast', (['(1 - seed_drop_rate)'], {'dtype': 'tf.float32'}), '(1 - seed_drop_rate, dtype=tf.float32)\n', (13862, 13900), True, 'import tensorflow as tf\n'), ((3010, 3055), 'tensorflow.cast', 'tf.cast', (['valid_block_center'], {'dtype': 'tf.float32'}), '(valid_block_center, dtype=tf.float32)\n', (3017, 3055), True, 'import tensorflow as tf\n'), ((5722, 5767), 'tensorflow.cast', 'tf.cast', (['valid_block_center'], {'dtype': 'tf.float32'}), '(valid_block_center, dtype=tf.float32)\n', (5729, 5767), True, 'import tensorflow as tf\n'), ((13807, 13852), 'tensorflow.cast', 'tf.cast', (['valid_block_center'], {'dtype': 'tf.float32'}), '(valid_block_center, dtype=tf.float32)\n', (13814, 13852), True, 'import tensorflow as tf\n'), ((7329, 7349), 'tensorflow.squeeze', 'tf.squeeze', (['gap_w', '(0)'], {}), '(gap_w, 0)\n', (7339, 7349), True, 'import tensorflow as tf\n')]
Purple-PI/rlstructures
tutorial/deprecated/tutorial_recurrent_policy/main_a2c.py
9b201b083715bbda2f3534b010c84e11dfc0a1c7
# # Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # from rlstructures import logging from rlstructures.env_wrappers import GymEnv, GymEnvInf from rlstructures.tools import weight_init import torch.nn as nn import copy import torch import time import numpy as np import torch.nn.functional as F from tutorial.tutorial_recurrent_policy.agent import RecurrentAgent from tutorial.tutorial_recurrent_policy.a2c import A2C import gym from gym.wrappers import TimeLimit # We write the 'create_env' and 'create_agent' function in the main file to allow these functions to be used with pickle when creating the batcher processes def create_gym_env(env_name): return gym.make(env_name) def create_env(n_envs, env_name=None, max_episode_steps=None, seed=None): envs = [] for k in range(n_envs): e = create_gym_env(env_name) e = TimeLimit(e, max_episode_steps=max_episode_steps) envs.append(e) return GymEnv(envs, seed) def create_train_env(n_envs, env_name=None, max_episode_steps=None, seed=None): envs = [] for k in range(n_envs): e = create_gym_env(env_name) e = TimeLimit(e, max_episode_steps=max_episode_steps) envs.append(e) return GymEnvInf(envs, seed) def create_agent(model, n_actions=1): return RecurrentAgent(model=model, n_actions=n_actions) class Experiment(A2C): def __init__(self, config, create_env, create_train_env, create_agent): super().__init__(config, create_env, create_train_env, create_agent) if __name__ == "__main__": # We use spawn mode such that most of the environment will run in multiple processes import torch.multiprocessing as mp mp.set_start_method("spawn") config = { "env_name": "CartPole-v0", "a2c_timesteps": 3, "n_envs": 4, "max_episode_steps": 100, "env_seed": 42, "n_threads": 4, "n_evaluation_threads": 2, "n_evaluation_episodes": 256, "time_limit": 3600, "lr": 0.001, "discount_factor": 0.95, "critic_coef": 1.0, "entropy_coef": 0.01, "a2c_coef": 1.0, "logdir": "./results", } exp = Experiment(config, create_env, create_train_env, create_agent) exp.run()
[((793, 811), 'gym.make', 'gym.make', (['env_name'], {}), '(env_name)\n', (801, 811), False, 'import gym\n'), ((1063, 1081), 'rlstructures.env_wrappers.GymEnv', 'GymEnv', (['envs', 'seed'], {}), '(envs, seed)\n', (1069, 1081), False, 'from rlstructures.env_wrappers import GymEnv, GymEnvInf\n'), ((1339, 1360), 'rlstructures.env_wrappers.GymEnvInf', 'GymEnvInf', (['envs', 'seed'], {}), '(envs, seed)\n', (1348, 1360), False, 'from rlstructures.env_wrappers import GymEnv, GymEnvInf\n'), ((1412, 1460), 'tutorial.tutorial_recurrent_policy.agent.RecurrentAgent', 'RecurrentAgent', ([], {'model': 'model', 'n_actions': 'n_actions'}), '(model=model, n_actions=n_actions)\n', (1426, 1460), False, 'from tutorial.tutorial_recurrent_policy.agent import RecurrentAgent\n'), ((1801, 1829), 'torch.multiprocessing.set_start_method', 'mp.set_start_method', (['"""spawn"""'], {}), "('spawn')\n", (1820, 1829), True, 'import torch.multiprocessing as mp\n'), ((979, 1028), 'gym.wrappers.TimeLimit', 'TimeLimit', (['e'], {'max_episode_steps': 'max_episode_steps'}), '(e, max_episode_steps=max_episode_steps)\n', (988, 1028), False, 'from gym.wrappers import TimeLimit\n'), ((1255, 1304), 'gym.wrappers.TimeLimit', 'TimeLimit', (['e'], {'max_episode_steps': 'max_episode_steps'}), '(e, max_episode_steps=max_episode_steps)\n', (1264, 1304), False, 'from gym.wrappers import TimeLimit\n')]
buchmuseum/GND_Dashboard
dashboard/gnd-app.py
c8c039bc8c09c480fc5ab8a0b186cd9dc37d7423
from matplotlib.pyplot import title import streamlit as st import pandas as pd import altair as alt import pydeck as pdk import os import glob from wordcloud import WordCloud import streamlit_analytics path = os.path.dirname(__file__) streamlit_analytics.start_tracking() @st.cache def load_gnd_top_daten(typ): gnd_top_df = pd.DataFrame() for file in glob.glob(f'{path}/../stats/title_gnd_{typ}_*.csv'): gnd_top_df = gnd_top_df.append(pd.read_csv(file, index_col=None)) return gnd_top_df def sachbegriff_cloud(): #wordcloud der top 100 sachbegriffe eines auszuwählenden tages der letzten 10 werktage st.header('TOP 100 Sachbegriffe pro Tag') st.write('Wählen Sie ein Datum aus den letzten 10 Werktagen vor der letzten Aktualisierung der Daten des Dashboards und sehen Sie eine Wordcloud der 100 meistverwendeten GND-Sachbegriffe dieses Tages. Die Größe des Begriffes entspricht der Häufigkeit des Sachbegriffs.') files = glob.glob(f'{path}/../stats/*Ts-count.csv') daten = [x[-23:-13] for x in files] daten.sort() daten_filter = st.select_slider('Wählen Sie ein Datum', options=daten, value=daten[-1]) df = pd.read_csv(f'{path}/../stats/{daten_filter}-Ts-count.csv') dict = df.to_dict(orient='records') worte = {} for record in dict: worte.update({record['sachbegriff']:record['count']}) wc = WordCloud(background_color="white", max_words=100, width=2000, height=800, colormap='tab20') wc.generate_from_frequencies(worte) return st.image(wc.to_array()) def wirkungsorte(): #ranking und karte der meistverwendeten wirkungsorte aller personen in der gnd df = pd.read_csv(f'{path}/wirkungsorte-top50.csv') df.drop(columns=['id'], inplace=True) df.rename(columns={'name': 'Name', 'count': 'Anzahl'}, inplace=True) st.header('TOP Wirkungsorte von GND-Personen') st.markdown('Von allen Personensätzen (Tp) weisen 782.682 Angaben zum Wirkungsort der jeweiligen Person auf.') #Balkendiagramm orte_filt = st.slider('Zeige Top …', min_value=3, max_value=len(df), value=10, step=1) graph_count = alt.Chart(df.nlargest(orte_filt, 'Anzahl', keep='all')).mark_bar().encode( alt.X('Name:N', sort='y'), alt.Y('Anzahl'), alt.Color('Name:N', legend=alt.Legend(columns=2)), tooltip=[alt.Tooltip('Name:N', title='Ort'), alt.Tooltip('Anzahl:Q', title='Anzahl')] ) st.altair_chart(graph_count, use_container_width=True) #Karte INITIAL_VIEW_STATE = pdk.ViewState( latitude=50.67877877706058, longitude=8.129981238464392, zoom=4.5, max_zoom=16, bearing=0 ) scatterplotlayer = pdk.Layer( "ScatterplotLayer", df, pickable=True, opacity=0.5, stroked=True, filled=True, radius_min_pixels=1, radius_max_pixels=100, line_width_min_pixels=1, get_position='[lon, lat]', get_radius="Anzahl", get_fill_color=[255, 140, 0], get_line_color=[0, 0, 0] ) st.pydeck_chart(pdk.Deck( scatterplotlayer, initial_view_state=INITIAL_VIEW_STATE, map_style=pdk.map_styles.LIGHT, tooltip={"html": "<b>{Name}</b><br \>Wirkungsort von {Anzahl} Personen"})) def wirkungsorte_musik(): #nach jahrzehnten zwischen 1400 und 2010 gefilterte auswertung der GND-Musikwerke, Musik-Personen und Wikrungsorte und daraus abgeleitete Zentren der Musikkultur, dargestellt auf einer Karte musiker_orte = pd.read_csv(f'{path}/musiker_orte.csv', sep='\t', index_col='idn') st.header('Wirkungszentren der Musik 1400–2010') st.write('Eine Auswertung der veröffentlichten Titel von Musikern und deren Wirkungszeiten erlaubt Rückschlüsse auf die musikalischen Zentren, wie sie im Bestand der DNB repräsentiert sind.') limiter = st.slider('Jahresfilter', min_value=1400, max_value=int(musiker_orte['jahrzehnt'].max()), value=(1900), step=10) musik_filt= musiker_orte.loc[(musiker_orte['jahrzehnt'] == limiter)] musik_filt['norm']=(musik_filt['count']-musik_filt['count'].min())/(musik_filt['count'].max()-musik_filt['count'].min()) #Karte INITIAL_VIEW_STATE = pdk.ViewState( latitude=50.67877877706058, longitude=8.129981238464392, zoom=4.5, max_zoom=16, bearing=0 ) musiker_scatter = pdk.Layer( "ScatterplotLayer", musik_filt, opacity=0.8, get_position='[lon, lat]', pickable=True, stroked=True, filled=True, radius_min_pixels=1, radius_max_pixels=100, radiusscale=100, line_width_min_pixels=1, get_radius="norm*50000", get_fill_color=[50, 168, 92], get_line_color=[39, 71, 51] ) st.pydeck_chart(pdk.Deck( musiker_scatter, initial_view_state=INITIAL_VIEW_STATE, map_style=pdk.map_styles.LIGHT, tooltip={"html": "<b>{name}</b>"})) st.subheader(f'TOP 10 Wirkungszentren der {limiter}er') col1, col2 = st.beta_columns(2) i = 1 for index, row in musik_filt.nlargest(10, 'norm').iterrows(): if i <= 5: with col1: st.write(f'{i}. {row["name"]}') elif i > 5: with col2: st.write(f'{i}. {row["name"]}') i += 1 def gesamt_entity_count(): #Gesamtzahl der GND-Entitäten with open(f"{path}/../stats/gnd_entity_count.csv", "r") as f: entities = f'{int(f.read()):,}' return st.write(f"GND-Entitäten gesamt: {entities.replace(',','.')}") def relationen(): #Top 10 der GND-Relationierungscodes rels = pd.read_csv(f'{path}/../stats/gnd_codes_all.csv', index_col=False) st.subheader('Relationen') st.write('GND-Datensätze können mit anderen Datensätzen verlinkt (»relationiert«) werden. Die Art der Verlinkung wird über einen Relationierungscode beschrieben. Hier sind die am häufigsten verwendeten Relationierungscodes zu sehen. Die Auflösung der wichtigsten Codes gibt es [hier](https://wiki.dnb.de/download/attachments/51283696/Codeliste_ABCnachCode_Webseite_2012-07.pdf).') rels_filt = st.slider('Zeige Top ...', 5, len(rels), 10, 1) relation_count = alt.Chart(rels.nlargest(rels_filt, 'count', keep='all')).mark_bar().encode( alt.X('code', title='Relationierungs-Code', sort='-y'), alt.Y('count', title='Anzahl'), alt.Color('code', sort='-y', title='Relationierungscode'), tooltip=[alt.Tooltip('count', title='Anzahl'), alt.Tooltip('code', title='Code')] ) st.altair_chart(relation_count, use_container_width=True) with open(f"{path}/../stats/gnd_relation_count.csv", "r") as f: relations = f'{int(f.read()):,}' st.write(f"Relationen zwischen Entitäten gesamt: {relations.replace(',','.')}") def systematik(): #Ranking der meistverwendeten GND-Systematik-Notationen classification = pd.read_csv(f'{path}/../stats/gnd_classification_all.csv', index_col=False) st.subheader('Systematik') st.write('Die Entitäten der GND können in eine Systematik eingeordnet werden. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst).') class_filt = st.slider('Zeige Top …', 5, len(classification), 10, 1) classification_count = alt.Chart(classification.nlargest(class_filt, 'count', keep='all')).mark_bar().encode( alt.X('id', title='Notation', sort='-y'), alt.Y('count', title='Anzahl'), alt.Color('name', sort='-y', title="Bezeichnung"), tooltip=[alt.Tooltip('id', title='Notation'), alt.Tooltip('name', title='Bezeichnung'), alt.Tooltip('count', title='Anzahl')] ) return st.altair_chart(classification_count, use_container_width=True) def systematik_ts(): #Ranking der Systematik von Ts-Sätzen classification_ts = pd.read_csv(f'{path}/../stats/gnd_classification_Ts_all.csv', index_col=False) st.subheader('Systematik der Sachbegriffe') st.write('Die Entitäten der GND können in eine Systematik eingeordnet werden. Hier sind die Systematik-Notationen der Sachbegriffe (Ts) aufgetragen. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst).') class_ts_filt = st.slider('Zeige TOP …', min_value=5, max_value=len(classification_ts), value=10, step=1) classification_ts_count = alt.Chart(classification_ts.nlargest(class_ts_filt, 'count', keep='all')).mark_bar().encode( alt.X('id:N', title='Notation', sort='-y'), alt.Y('count:Q', title='Anzahl'), alt.Color('name:N', sort='-y', title='Bezeichnung'), tooltip = [alt.Tooltip('id', title='Notation'), alt.Tooltip('name', title='Bezeichnung'), alt.Tooltip('count', title='Anzahl')] ) return st.altair_chart(classification_ts_count, use_container_width=True) def zeitverlauf(): #zeitverlauf der erstellung der GND-Sätze ab Januar 1972 created_at = pd.read_csv(f'{path}/../stats/gnd_created_at.csv', index_col='created_at', parse_dates=True, header=0, names=['created_at', 'count']) st.subheader('Zeitverlauf der GND-Datensatzerstellung') st.write('Auf einer Zeitleiste wird die Anzahl der monatlich erstellten GND-Sätze aufgetragen. Die ersten Sätze stammen aus dem Januar 1972') created_filt = st.slider('Zeitraum', 1972, 2021, (1972,2021), 1) created = alt.Chart(created_at[f'{created_filt[0]}':f'{created_filt[1]}'].reset_index()).mark_line().encode( alt.X('created_at:T', title='Erstelldatum'), alt.Y('count:Q', title='Sätze pro Monat'), tooltip=['count'] ) return st.altair_chart(created, use_container_width=True) def entities(): #GND-Entitäten nach Satzart und Katalogisierungslevel df = pd.read_csv(f'{path}/../stats/gnd_entity_types.csv', index_col=False, names=['entity','count']) df['level'] = df.entity.str[2:3] df.entity = df.entity.str[:2] if satzart == 'alle': entity_count = alt.Chart(df).mark_bar().encode( alt.X('sum(count)', title='Datensätze pro Katalogisierungslevel'), alt.Y('entity', title='Satzart'), alt.Color('level', title='Katalogisierungslevel'), tooltip=[alt.Tooltip('entity', title='Satzart'), alt.Tooltip( 'level', title='Katalogisierungslevel'), alt.Tooltip('count', title='Anzahl')] ) st.subheader('Entitäten und Katalogisierungslevel') else: entity_count = alt.Chart(df.loc[df['entity'].str.startswith(satzart[:2])]).mark_bar().encode( alt.X('sum(count)', title='Datensätze pro Katalogisierungslevel'), alt.Y('entity', title='Satzart'), alt.Color('level', title='Katalogisierungslevel'), tooltip=[alt.Tooltip( 'level', title='Katalogisierungslevel'), alt.Tooltip('count', title='Anzahl')] ) st.subheader(f'Katalogisierungslevel in Satzart {satzart}') st.write('Alle GND-Entitäten können in verschiedenen Katalogisierungsleveln (1-7) angelegt werden. Je niedriger das Katalogisierungslevel, desto verlässlicher die Daten, weil Sie dann von qualifizierten Personen erstellt bzw. überprüft wurden.') return st.altair_chart(entity_count, use_container_width=True) def newcomer(): #TOP 10 der Entitäten, die in den letzten 365 Tagen erstellt wurden if satzart == 'alle': st.subheader(f'TOP 10 GND-Newcomer') st.write('TOP 10 der GND-Entitäten, die in den letzten 365 Tagen angelegt wurden.') newcomer_daten = pd.read_csv(f'{path}/../stats/title_gnd_newcomer_top10.csv', index_col=None) newcomer = alt.Chart(newcomer_daten).mark_bar().encode( alt.X('gnd_id', title='Entitäten', sort='-y'), alt.Y('count', title='Anzahl'), alt.Color('name', sort='-y', title='Entität'), tooltip=[alt.Tooltip('name:N', title='Entität'), alt.Tooltip('bbg:N', title='Satzart'), alt.Tooltip('gnd_id:N', title='IDN'), alt.Tooltip('count:Q', title='Anzahl')] ) else: st.subheader(f'TOP 10 {satzart} GND-Newcomer') st.write(f'TOP 10 der {satzart} Sätze, die in den letzten 365 Tagen angelegt wurden.') newcomer_daten = load_gnd_top_daten('newcomer_top10') newcomer = alt.Chart(newcomer_daten.loc[newcomer_daten['bbg'].str.startswith(satzart[:2], na=False)]).mark_bar().encode( alt.X('gnd_id:O', title='Entitäten', sort='-y'), alt.Y('count', title='Anzahl'), alt.Color('name', sort='-y', title='Entität'), tooltip=[alt.Tooltip('name:N', title='Entität'), alt.Tooltip('gnd_id:N', title='IDN'), alt.Tooltip('count:Q', title='Anzahl')] ) st.altair_chart(newcomer, use_container_width=True) def gnd_top(): #TOP 10 GND-Entitäten in DNB-Titeldaten, nach Satzart gefiltert if satzart == 'alle': st.subheader(f'TOP 10 GND-Entitäten in DNB-Titeldaten') top_daten = pd.read_csv(f'{path}/../stats/title_gnd_top10.csv', index_col=None) gnd_top = alt.Chart(top_daten).mark_bar().encode( alt.X('gnd_id:N', title='Entitäten', sort='-y'), alt.Y('count:Q', title='Anzahl'), alt.Color('name:N', sort='-y', title='Entität'), tooltip=[alt.Tooltip('name:N', title='Entität'), alt.Tooltip('gnd_id:N', title='IDN'), alt.Tooltip('bbg:N', title='Satzart'), alt.Tooltip('count:Q', title='Anzahl')] ) else: st.subheader(f'TOP 10 {satzart} in DNB-Titeldaten') top_daten = load_gnd_top_daten('top10') gnd_top = alt.Chart(top_daten.loc[top_daten['bbg'].str.startswith(satzart[:2], na=False)]).mark_bar().encode( alt.X('gnd_id:N', title='Entitäten', sort='-y'), alt.Y('count:Q', title='Anzahl'), alt.Color('name:N', sort='-y', title='Entität'), tooltip=[alt.Tooltip('name:N', title='Entität'), alt.Tooltip('gnd_id:N', title='IDN'), alt.Tooltip('count:Q', title='Anzahl')] ) st.write('Verknüpfungen, die maschinell erzeugt wurden, aus Fremddaten stammen oder verwaist sind, wurden nicht in die Auswertung einbezogen. Eine detaillierte Auflistung der ausgewerteten Felder ist im [GitHub-Repository](https://git.io/JG5vN) dieses Dashboards dokumentiert.') st.altair_chart(gnd_top, use_container_width=True) def dnb_links(): #GND-Verknüpfungen in DNB Titeldaten if satzart == 'alle': #Anzahl GND-Verknüpfungen in DNB-Titeldaten with open(f"{path}/../stats/title_gnd_links.csv", "r") as f: links = f'{int(f.read()):,}' #GND-Entitäten maschinell verknüpft with open(f"{path}/../stats/title_gnd_links_auto.csv", "r") as f: auto_entites = int(f.read()) #GND-Entitäten aus Fremddaten with open(f"{path}/../stats/title_gnd_links_ext.csv", "r") as f: fremd_entities = int(f.read()) #Anzahl der intellktuell verknüpften GND-Entitäten in DNB-Titeldaten with open(f"{path}/../stats/title_gnd_links_unique.csv", "r") as f: uniques = int(f.read()) uniques_str = f'{uniques:,}' #Durchschnittliche Anzahl an GND-Verknüpfungen pro DNB-Titeldatensatz with open(f"{path}/../stats/title_gnd_mean.csv", "r") as f: mean = str(round(float(f.read()),2)).replace('.',',') st.write(f"{links.replace(',','.')} intellektuell vergebene Verknüpfungen zu {uniques_str.replace(',','.')} GND-Entitäten in den DNB-Titeldaten. Durchschnittlich {mean} GND-Verknüpfungen pro DNB-Titeldatensatz") entity_df = pd.DataFrame.from_dict({"intellektuell verknüpfte Entitäten": uniques, "Entitäten aus automatischen Prozessen": auto_entites, "Entitäten aus Fremddaten": fremd_entities}, orient = "index").reset_index() entity_df = entity_df.rename(columns={"index":"Datenart", 0:"Anzahl"}) st.subheader('Datenherkunft der GND-Entitäten in DNB-Titeldaten') st.write('Weniger als ein Drittel der GND-Entitäten in DNB-Titeldaten wurde in intellektuellen Erschließungsprozessen vergeben. Jeweils ca. ein weiteres Drittel wurde in maschinellen Erschließungsprozessen vergeben, ca. ein Drittel stammt aus Fremddaten.') entities = alt.Chart(entity_df).mark_bar().encode( alt.X('sum(Datenart):N', title='Datenart'), alt.Y('sum(Anzahl):Q', title='Anzahl'), color='Datenart', tooltip='Anzahl:N' ) st.altair_chart(entities, use_container_width=True) else: with open(f"{path}/../stats/title_gnd_mean_{satzart[:2]}.csv", "r") as f: mean = str(round(float(f.read()),2)).replace('.',',') st.write(f'Durchschnittlich {mean} Verknüpfungen zu {satzart}-Sätzen pro DNB-Titeldatensatz') #main st.title('GND-Dashboard') #infoebereich oben with st.beta_container(): st.info('Hier finden Sie statistische Auswertungen der GND und ihrer Verknüpfungen mit den Titeldaten der Deutschen Nationalbibliothek (Stand der Daten: Juli 2021). Wählen Sie links die Satzart, die Sie interessiert, und Sie erhalten die verfügbaren Auswertungen und Statstiken. Verwenden Sie einen auf Chromium basierenden Browser.') with st.beta_expander("Methodik und Datenherkunft"): st.markdown(''' Datengrundlage ist ein Gesamtabzug der Daten der Gemeinsamen Normadatei (GND) sowie der Titeldaten der Deutschen Nationalbibliothek (DNB) inkl. Zeitschriftendatenbank (ZDB), sofern sich Exemplare der Zeitschrift im Bestand der DNB befinden. In den Titeldaten ist auch der Tonträger- und Notenbestand des Deutschen Musikarchivs (DMA) sowie der Buch- und Objektbestand des Deutschen Buch- und Schriftmuseums (DBSM) nachgewiesen. Der Gesamtabzug liegt im OCLC-Format PICA+ vor. Die Daten werden mithilfe des Pica-Parsers [pica.rs](https://github.com/deutsche-nationalbibliothek/pica-rs) gefiltert. Dieses Tool produziert aus dem sehr großen Gesamtabzug (~ 31 GB) kleinere CSV-Dateien, die mit Python weiterverarbeitet werden. Das Dashboard ist mit dem Python-Framework [Streamlit](https://streamlit.io/) geschrieben. Die Skripte sowie die gefilterten CSV-Rohdaten sind auf [Github](https://github.com/buchmuseum/GND_Dashboard) zu finden. Die Diagramme wurden mit [Altair](https://altair-viz.github.io/index.html) erstellt, die Karten mit [Deck GL](https://deck.gl/) (via [Pydeck](https://deckgl.readthedocs.io/en/latest/#)), die Wordcloud mit [wordcloud](https://amueller.github.io/word_cloud/index.html). Für grundlegende Zugriffsstatistik verwenden wir [streamlit-analytics](https://pypi.org/project/streamlit-analytics/). Dabei werden keine personenbezogenen Daten gespeichert. Alle Skripte und Daten stehen unter CC0 Lizenz und können frei weitergenutzt werden. Die Daten werden monatlich aktualisiert. ''') #sidebar mit satzartenfilter st.sidebar.header("Satzart wählen") satzart = st.sidebar.selectbox( "Über welche GND-Satzart möchten Sie etwas erfahren?", ('alle', "Tp - Personen", "Tb - Körperschaften", "Tg - Geografika", "Ts - Sachbegriffe", "Tu - Werke", "Tf - Veranstaltungen") ) st.sidebar.info('Diese Widgets haben die GitHub-User [niko2342](https://github.com/niko2342/), [ramonvoges](https://github.com/ramonvoges), [a-wendler](https://github.com/a-wendler/) sowie Christian Baumann geschrieben. Sie gehören zur Python Community der Deutschen Nationalbibliothek.') gnd_allgemein = st.beta_container() with gnd_allgemein: st.header('GND Statistik allgemein') #allgemeine statistiken in abhängigkeit der satzart if satzart == 'alle': gesamt_entity_count() entities() newcomer() zeitverlauf() relationen() systematik() else: entities() newcomer() #besondere widgets für einzelne satzarten if satzart == "Tp - Personen": wirkungsorte() elif satzart == "Tg - Geografika": wirkungsorte_musik() wirkungsorte() elif satzart == "Ts - Sachbegriffe": sachbegriff_cloud() systematik_ts() dnb = st.beta_container() with dnb: st.header('GND in der Deutschen Nationalbibliothek') gnd_top() dnb_links() streamlit_analytics.stop_tracking()
[((210, 235), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (225, 235), False, 'import os\n'), ((237, 273), 'streamlit_analytics.start_tracking', 'streamlit_analytics.start_tracking', ([], {}), '()\n', (271, 273), False, 'import streamlit_analytics\n'), ((16582, 16607), 'streamlit.title', 'st.title', (['"""GND-Dashboard"""'], {}), "('GND-Dashboard')\n", (16590, 16607), True, 'import streamlit as st\n'), ((18618, 18653), 'streamlit.sidebar.header', 'st.sidebar.header', (['"""Satzart wählen"""'], {}), "('Satzart wählen')\n", (18635, 18653), True, 'import streamlit as st\n'), ((18664, 18875), 'streamlit.sidebar.selectbox', 'st.sidebar.selectbox', (['"""Über welche GND-Satzart möchten Sie etwas erfahren?"""', "('alle', 'Tp - Personen', 'Tb - Körperschaften', 'Tg - Geografika',\n 'Ts - Sachbegriffe', 'Tu - Werke', 'Tf - Veranstaltungen')"], {}), "('Über welche GND-Satzart möchten Sie etwas erfahren?',\n ('alle', 'Tp - Personen', 'Tb - Körperschaften', 'Tg - Geografika',\n 'Ts - Sachbegriffe', 'Tu - Werke', 'Tf - Veranstaltungen'))\n", (18684, 18875), True, 'import streamlit as st\n'), ((18878, 19176), 'streamlit.sidebar.info', 'st.sidebar.info', (['"""Diese Widgets haben die GitHub-User [niko2342](https://github.com/niko2342/), [ramonvoges](https://github.com/ramonvoges), [a-wendler](https://github.com/a-wendler/) sowie Christian Baumann geschrieben. Sie gehören zur Python Community der Deutschen Nationalbibliothek."""'], {}), "(\n 'Diese Widgets haben die GitHub-User [niko2342](https://github.com/niko2342/), [ramonvoges](https://github.com/ramonvoges), [a-wendler](https://github.com/a-wendler/) sowie Christian Baumann geschrieben. Sie gehören zur Python Community der Deutschen Nationalbibliothek.'\n )\n", (18893, 19176), True, 'import streamlit as st\n'), ((19184, 19203), 'streamlit.beta_container', 'st.beta_container', ([], {}), '()\n', (19201, 19203), True, 'import streamlit as st\n'), ((19827, 19846), 'streamlit.beta_container', 'st.beta_container', ([], {}), '()\n', (19844, 19846), True, 'import streamlit as st\n'), ((19945, 19980), 'streamlit_analytics.stop_tracking', 'streamlit_analytics.stop_tracking', ([], {}), '()\n', (19978, 19980), False, 'import streamlit_analytics\n'), ((331, 345), 'pandas.DataFrame', 'pd.DataFrame', ([], {}), '()\n', (343, 345), True, 'import pandas as pd\n'), ((362, 413), 'glob.glob', 'glob.glob', (['f"""{path}/../stats/title_gnd_{typ}_*.csv"""'], {}), "(f'{path}/../stats/title_gnd_{typ}_*.csv')\n", (371, 413), False, 'import glob\n'), ((632, 673), 'streamlit.header', 'st.header', (['"""TOP 100 Sachbegriffe pro Tag"""'], {}), "('TOP 100 Sachbegriffe pro Tag')\n", (641, 673), True, 'import streamlit as st\n'), ((678, 958), 'streamlit.write', 'st.write', (['"""Wählen Sie ein Datum aus den letzten 10 Werktagen vor der letzten Aktualisierung der Daten des Dashboards und sehen Sie eine Wordcloud der 100 meistverwendeten GND-Sachbegriffe dieses Tages. Die Größe des Begriffes entspricht der Häufigkeit des Sachbegriffs."""'], {}), "(\n 'Wählen Sie ein Datum aus den letzten 10 Werktagen vor der letzten Aktualisierung der Daten des Dashboards und sehen Sie eine Wordcloud der 100 meistverwendeten GND-Sachbegriffe dieses Tages. Die Größe des Begriffes entspricht der Häufigkeit des Sachbegriffs.'\n )\n", (686, 958), True, 'import streamlit as st\n'), ((961, 1004), 'glob.glob', 'glob.glob', (['f"""{path}/../stats/*Ts-count.csv"""'], {}), "(f'{path}/../stats/*Ts-count.csv')\n", (970, 1004), False, 'import glob\n'), ((1081, 1153), 'streamlit.select_slider', 'st.select_slider', (['"""Wählen Sie ein Datum"""'], {'options': 'daten', 'value': 'daten[-1]'}), "('Wählen Sie ein Datum', options=daten, value=daten[-1])\n", (1097, 1153), True, 'import streamlit as st\n'), ((1164, 1223), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/{daten_filter}-Ts-count.csv"""'], {}), "(f'{path}/../stats/{daten_filter}-Ts-count.csv')\n", (1175, 1223), True, 'import pandas as pd\n'), ((1380, 1476), 'wordcloud.WordCloud', 'WordCloud', ([], {'background_color': '"""white"""', 'max_words': '(100)', 'width': '(2000)', 'height': '(800)', 'colormap': '"""tab20"""'}), "(background_color='white', max_words=100, width=2000, height=800,\n colormap='tab20')\n", (1389, 1476), False, 'from wordcloud import WordCloud\n'), ((1661, 1706), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/wirkungsorte-top50.csv"""'], {}), "(f'{path}/wirkungsorte-top50.csv')\n", (1672, 1706), True, 'import pandas as pd\n'), ((1827, 1873), 'streamlit.header', 'st.header', (['"""TOP Wirkungsorte von GND-Personen"""'], {}), "('TOP Wirkungsorte von GND-Personen')\n", (1836, 1873), True, 'import streamlit as st\n'), ((1878, 1998), 'streamlit.markdown', 'st.markdown', (['"""Von allen Personensätzen (Tp) weisen 782.682 Angaben zum Wirkungsort der jeweiligen Person auf."""'], {}), "(\n 'Von allen Personensätzen (Tp) weisen 782.682 Angaben zum Wirkungsort der jeweiligen Person auf.'\n )\n", (1889, 1998), True, 'import streamlit as st\n'), ((2417, 2471), 'streamlit.altair_chart', 'st.altair_chart', (['graph_count'], {'use_container_width': '(True)'}), '(graph_count, use_container_width=True)\n', (2432, 2471), True, 'import streamlit as st\n'), ((2509, 2618), 'pydeck.ViewState', 'pdk.ViewState', ([], {'latitude': '(50.67877877706058)', 'longitude': '(8.129981238464392)', 'zoom': '(4.5)', 'max_zoom': '(16)', 'bearing': '(0)'}), '(latitude=50.67877877706058, longitude=8.129981238464392, zoom\n =4.5, max_zoom=16, bearing=0)\n', (2522, 2618), True, 'import pydeck as pdk\n'), ((2664, 2937), 'pydeck.Layer', 'pdk.Layer', (['"""ScatterplotLayer"""', 'df'], {'pickable': '(True)', 'opacity': '(0.5)', 'stroked': '(True)', 'filled': '(True)', 'radius_min_pixels': '(1)', 'radius_max_pixels': '(100)', 'line_width_min_pixels': '(1)', 'get_position': '"""[lon, lat]"""', 'get_radius': '"""Anzahl"""', 'get_fill_color': '[255, 140, 0]', 'get_line_color': '[0, 0, 0]'}), "('ScatterplotLayer', df, pickable=True, opacity=0.5, stroked=True,\n filled=True, radius_min_pixels=1, radius_max_pixels=100,\n line_width_min_pixels=1, get_position='[lon, lat]', get_radius='Anzahl',\n get_fill_color=[255, 140, 0], get_line_color=[0, 0, 0])\n", (2673, 2937), True, 'import pydeck as pdk\n'), ((3432, 3498), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/musiker_orte.csv"""'], {'sep': '"""\t"""', 'index_col': '"""idn"""'}), "(f'{path}/musiker_orte.csv', sep='\\t', index_col='idn')\n", (3443, 3498), True, 'import pandas as pd\n'), ((3503, 3551), 'streamlit.header', 'st.header', (['"""Wirkungszentren der Musik 1400–2010"""'], {}), "('Wirkungszentren der Musik 1400–2010')\n", (3512, 3551), True, 'import streamlit as st\n'), ((3556, 3757), 'streamlit.write', 'st.write', (['"""Eine Auswertung der veröffentlichten Titel von Musikern und deren Wirkungszeiten erlaubt Rückschlüsse auf die musikalischen Zentren, wie sie im Bestand der DNB repräsentiert sind."""'], {}), "(\n 'Eine Auswertung der veröffentlichten Titel von Musikern und deren Wirkungszeiten erlaubt Rückschlüsse auf die musikalischen Zentren, wie sie im Bestand der DNB repräsentiert sind.'\n )\n", (3564, 3757), True, 'import streamlit as st\n'), ((4109, 4218), 'pydeck.ViewState', 'pdk.ViewState', ([], {'latitude': '(50.67877877706058)', 'longitude': '(8.129981238464392)', 'zoom': '(4.5)', 'max_zoom': '(16)', 'bearing': '(0)'}), '(latitude=50.67877877706058, longitude=8.129981238464392, zoom\n =4.5, max_zoom=16, bearing=0)\n', (4122, 4218), True, 'import pydeck as pdk\n'), ((4283, 4594), 'pydeck.Layer', 'pdk.Layer', (['"""ScatterplotLayer"""', 'musik_filt'], {'opacity': '(0.8)', 'get_position': '"""[lon, lat]"""', 'pickable': '(True)', 'stroked': '(True)', 'filled': '(True)', 'radius_min_pixels': '(1)', 'radius_max_pixels': '(100)', 'radiusscale': '(100)', 'line_width_min_pixels': '(1)', 'get_radius': '"""norm*50000"""', 'get_fill_color': '[50, 168, 92]', 'get_line_color': '[39, 71, 51]'}), "('ScatterplotLayer', musik_filt, opacity=0.8, get_position=\n '[lon, lat]', pickable=True, stroked=True, filled=True,\n radius_min_pixels=1, radius_max_pixels=100, radiusscale=100,\n line_width_min_pixels=1, get_radius='norm*50000', get_fill_color=[50, \n 168, 92], get_line_color=[39, 71, 51])\n", (4292, 4594), True, 'import pydeck as pdk\n'), ((4870, 4925), 'streamlit.subheader', 'st.subheader', (['f"""TOP 10 Wirkungszentren der {limiter}er"""'], {}), "(f'TOP 10 Wirkungszentren der {limiter}er')\n", (4882, 4925), True, 'import streamlit as st\n'), ((4943, 4961), 'streamlit.beta_columns', 'st.beta_columns', (['(2)'], {}), '(2)\n', (4958, 4961), True, 'import streamlit as st\n'), ((5547, 5613), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/gnd_codes_all.csv"""'], {'index_col': '(False)'}), "(f'{path}/../stats/gnd_codes_all.csv', index_col=False)\n", (5558, 5613), True, 'import pandas as pd\n'), ((5618, 5644), 'streamlit.subheader', 'st.subheader', (['"""Relationen"""'], {}), "('Relationen')\n", (5630, 5644), True, 'import streamlit as st\n'), ((5649, 6039), 'streamlit.write', 'st.write', (['"""GND-Datensätze können mit anderen Datensätzen verlinkt (»relationiert«) werden. Die Art der Verlinkung wird über einen Relationierungscode beschrieben. Hier sind die am häufigsten verwendeten Relationierungscodes zu sehen. Die Auflösung der wichtigsten Codes gibt es [hier](https://wiki.dnb.de/download/attachments/51283696/Codeliste_ABCnachCode_Webseite_2012-07.pdf)."""'], {}), "(\n 'GND-Datensätze können mit anderen Datensätzen verlinkt (»relationiert«) werden. Die Art der Verlinkung wird über einen Relationierungscode beschrieben. Hier sind die am häufigsten verwendeten Relationierungscodes zu sehen. Die Auflösung der wichtigsten Codes gibt es [hier](https://wiki.dnb.de/download/attachments/51283696/Codeliste_ABCnachCode_Webseite_2012-07.pdf).'\n )\n", (5657, 6039), True, 'import streamlit as st\n'), ((6462, 6519), 'streamlit.altair_chart', 'st.altair_chart', (['relation_count'], {'use_container_width': '(True)'}), '(relation_count, use_container_width=True)\n', (6477, 6519), True, 'import streamlit as st\n'), ((6814, 6889), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/gnd_classification_all.csv"""'], {'index_col': '(False)'}), "(f'{path}/../stats/gnd_classification_all.csv', index_col=False)\n", (6825, 6889), True, 'import pandas as pd\n'), ((6894, 6920), 'streamlit.subheader', 'st.subheader', (['"""Systematik"""'], {}), "('Systematik')\n", (6906, 6920), True, 'import streamlit as st\n'), ((6925, 7092), 'streamlit.write', 'st.write', (['"""Die Entitäten der GND können in eine Systematik eingeordnet werden. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst)."""'], {}), "(\n 'Die Entitäten der GND können in eine Systematik eingeordnet werden. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst).'\n )\n", (6933, 7092), True, 'import streamlit as st\n'), ((7570, 7633), 'streamlit.altair_chart', 'st.altair_chart', (['classification_count'], {'use_container_width': '(True)'}), '(classification_count, use_container_width=True)\n', (7585, 7633), True, 'import streamlit as st\n'), ((7722, 7800), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/gnd_classification_Ts_all.csv"""'], {'index_col': '(False)'}), "(f'{path}/../stats/gnd_classification_Ts_all.csv', index_col=False)\n", (7733, 7800), True, 'import pandas as pd\n'), ((7805, 7848), 'streamlit.subheader', 'st.subheader', (['"""Systematik der Sachbegriffe"""'], {}), "('Systematik der Sachbegriffe')\n", (7817, 7848), True, 'import streamlit as st\n'), ((7853, 8091), 'streamlit.write', 'st.write', (['"""Die Entitäten der GND können in eine Systematik eingeordnet werden. Hier sind die Systematik-Notationen der Sachbegriffe (Ts) aufgetragen. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst)."""'], {}), "(\n 'Die Entitäten der GND können in eine Systematik eingeordnet werden. Hier sind die Systematik-Notationen der Sachbegriffe (Ts) aufgetragen. Die Liste der möglichen Notationen gibt es [hier](http://www.dnb.de/gndsyst).'\n )\n", (7861, 8091), True, 'import streamlit as st\n'), ((8623, 8689), 'streamlit.altair_chart', 'st.altair_chart', (['classification_ts_count'], {'use_container_width': '(True)'}), '(classification_ts_count, use_container_width=True)\n', (8638, 8689), True, 'import streamlit as st\n'), ((8788, 8925), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/gnd_created_at.csv"""'], {'index_col': '"""created_at"""', 'parse_dates': '(True)', 'header': '(0)', 'names': "['created_at', 'count']"}), "(f'{path}/../stats/gnd_created_at.csv', index_col='created_at',\n parse_dates=True, header=0, names=['created_at', 'count'])\n", (8799, 8925), True, 'import pandas as pd\n'), ((8931, 8986), 'streamlit.subheader', 'st.subheader', (['"""Zeitverlauf der GND-Datensatzerstellung"""'], {}), "('Zeitverlauf der GND-Datensatzerstellung')\n", (8943, 8986), True, 'import streamlit as st\n'), ((8991, 9142), 'streamlit.write', 'st.write', (['"""Auf einer Zeitleiste wird die Anzahl der monatlich erstellten GND-Sätze aufgetragen. Die ersten Sätze stammen aus dem Januar 1972"""'], {}), "(\n 'Auf einer Zeitleiste wird die Anzahl der monatlich erstellten GND-Sätze aufgetragen. Die ersten Sätze stammen aus dem Januar 1972'\n )\n", (8999, 9142), True, 'import streamlit as st\n'), ((9152, 9202), 'streamlit.slider', 'st.slider', (['"""Zeitraum"""', '(1972)', '(2021)', '(1972, 2021)', '(1)'], {}), "('Zeitraum', 1972, 2021, (1972, 2021), 1)\n", (9161, 9202), True, 'import streamlit as st\n'), ((9462, 9512), 'streamlit.altair_chart', 'st.altair_chart', (['created'], {'use_container_width': '(True)'}), '(created, use_container_width=True)\n', (9477, 9512), True, 'import streamlit as st\n'), ((9597, 9698), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/gnd_entity_types.csv"""'], {'index_col': '(False)', 'names': "['entity', 'count']"}), "(f'{path}/../stats/gnd_entity_types.csv', index_col=False, names\n =['entity', 'count'])\n", (9608, 9698), True, 'import pandas as pd\n'), ((10756, 11011), 'streamlit.write', 'st.write', (['"""Alle GND-Entitäten können in verschiedenen Katalogisierungsleveln (1-7) angelegt werden. Je niedriger das Katalogisierungslevel, desto verlässlicher die Daten, weil Sie dann von qualifizierten Personen erstellt bzw. überprüft wurden."""'], {}), "(\n 'Alle GND-Entitäten können in verschiedenen Katalogisierungsleveln (1-7) angelegt werden. Je niedriger das Katalogisierungslevel, desto verlässlicher die Daten, weil Sie dann von qualifizierten Personen erstellt bzw. überprüft wurden.'\n )\n", (10764, 11011), True, 'import streamlit as st\n'), ((11013, 11068), 'streamlit.altair_chart', 'st.altair_chart', (['entity_count'], {'use_container_width': '(True)'}), '(entity_count, use_container_width=True)\n', (11028, 11068), True, 'import streamlit as st\n'), ((12508, 12559), 'streamlit.altair_chart', 'st.altair_chart', (['newcomer'], {'use_container_width': '(True)'}), '(newcomer, use_container_width=True)\n', (12523, 12559), True, 'import streamlit as st\n'), ((13796, 14084), 'streamlit.write', 'st.write', (['"""Verknüpfungen, die maschinell erzeugt wurden, aus Fremddaten stammen oder verwaist sind, wurden nicht in die Auswertung einbezogen. Eine detaillierte Auflistung der ausgewerteten Felder ist im [GitHub-Repository](https://git.io/JG5vN) dieses Dashboards dokumentiert."""'], {}), "(\n 'Verknüpfungen, die maschinell erzeugt wurden, aus Fremddaten stammen oder verwaist sind, wurden nicht in die Auswertung einbezogen. Eine detaillierte Auflistung der ausgewerteten Felder ist im [GitHub-Repository](https://git.io/JG5vN) dieses Dashboards dokumentiert.'\n )\n", (13804, 14084), True, 'import streamlit as st\n'), ((14079, 14129), 'streamlit.altair_chart', 'st.altair_chart', (['gnd_top'], {'use_container_width': '(True)'}), '(gnd_top, use_container_width=True)\n', (14094, 14129), True, 'import streamlit as st\n'), ((16633, 16652), 'streamlit.beta_container', 'st.beta_container', ([], {}), '()\n', (16650, 16652), True, 'import streamlit as st\n'), ((16658, 17002), 'streamlit.info', 'st.info', (['"""Hier finden Sie statistische Auswertungen der GND und ihrer Verknüpfungen mit den Titeldaten der Deutschen Nationalbibliothek (Stand der Daten: Juli 2021). Wählen Sie links die Satzart, die Sie interessiert, und Sie erhalten die verfügbaren Auswertungen und Statstiken. Verwenden Sie einen auf Chromium basierenden Browser."""'], {}), "(\n 'Hier finden Sie statistische Auswertungen der GND und ihrer Verknüpfungen mit den Titeldaten der Deutschen Nationalbibliothek (Stand der Daten: Juli 2021). Wählen Sie links die Satzart, die Sie interessiert, und Sie erhalten die verfügbaren Auswertungen und Statstiken. Verwenden Sie einen auf Chromium basierenden Browser.'\n )\n", (16665, 17002), True, 'import streamlit as st\n'), ((19228, 19264), 'streamlit.header', 'st.header', (['"""GND Statistik allgemein"""'], {}), "('GND Statistik allgemein')\n", (19237, 19264), True, 'import streamlit as st\n'), ((19861, 19913), 'streamlit.header', 'st.header', (['"""GND in der Deutschen Nationalbibliothek"""'], {}), "('GND in der Deutschen Nationalbibliothek')\n", (19870, 19913), True, 'import streamlit as st\n'), ((2202, 2227), 'altair.X', 'alt.X', (['"""Name:N"""'], {'sort': '"""y"""'}), "('Name:N', sort='y')\n", (2207, 2227), True, 'import altair as alt\n'), ((2237, 2252), 'altair.Y', 'alt.Y', (['"""Anzahl"""'], {}), "('Anzahl')\n", (2242, 2252), True, 'import altair as alt\n'), ((3001, 3182), 'pydeck.Deck', 'pdk.Deck', (['scatterplotlayer'], {'initial_view_state': 'INITIAL_VIEW_STATE', 'map_style': 'pdk.map_styles.LIGHT', 'tooltip': "{'html': '<b>{Name}</b><br \\\\>Wirkungsort von {Anzahl} Personen'}"}), "(scatterplotlayer, initial_view_state=INITIAL_VIEW_STATE, map_style\n =pdk.map_styles.LIGHT, tooltip={'html':\n '<b>{Name}</b><br \\\\>Wirkungsort von {Anzahl} Personen'})\n", (3009, 3182), True, 'import pydeck as pdk\n'), ((4716, 4852), 'pydeck.Deck', 'pdk.Deck', (['musiker_scatter'], {'initial_view_state': 'INITIAL_VIEW_STATE', 'map_style': 'pdk.map_styles.LIGHT', 'tooltip': "{'html': '<b>{name}</b>'}"}), "(musiker_scatter, initial_view_state=INITIAL_VIEW_STATE, map_style=\n pdk.map_styles.LIGHT, tooltip={'html': '<b>{name}</b>'})\n", (4724, 4852), True, 'import pydeck as pdk\n'), ((6199, 6253), 'altair.X', 'alt.X', (['"""code"""'], {'title': '"""Relationierungs-Code"""', 'sort': '"""-y"""'}), "('code', title='Relationierungs-Code', sort='-y')\n", (6204, 6253), True, 'import altair as alt\n'), ((6263, 6293), 'altair.Y', 'alt.Y', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (6268, 6293), True, 'import altair as alt\n'), ((6303, 6360), 'altair.Color', 'alt.Color', (['"""code"""'], {'sort': '"""-y"""', 'title': '"""Relationierungscode"""'}), "('code', sort='-y', title='Relationierungscode')\n", (6312, 6360), True, 'import altair as alt\n'), ((7278, 7318), 'altair.X', 'alt.X', (['"""id"""'], {'title': '"""Notation"""', 'sort': '"""-y"""'}), "('id', title='Notation', sort='-y')\n", (7283, 7318), True, 'import altair as alt\n'), ((7328, 7358), 'altair.Y', 'alt.Y', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (7333, 7358), True, 'import altair as alt\n'), ((7368, 7417), 'altair.Color', 'alt.Color', (['"""name"""'], {'sort': '"""-y"""', 'title': '"""Bezeichnung"""'}), "('name', sort='-y', title='Bezeichnung')\n", (7377, 7417), True, 'import altair as alt\n'), ((8323, 8365), 'altair.X', 'alt.X', (['"""id:N"""'], {'title': '"""Notation"""', 'sort': '"""-y"""'}), "('id:N', title='Notation', sort='-y')\n", (8328, 8365), True, 'import altair as alt\n'), ((8375, 8407), 'altair.Y', 'alt.Y', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (8380, 8407), True, 'import altair as alt\n'), ((8417, 8468), 'altair.Color', 'alt.Color', (['"""name:N"""'], {'sort': '"""-y"""', 'title': '"""Bezeichnung"""'}), "('name:N', sort='-y', title='Bezeichnung')\n", (8426, 8468), True, 'import altair as alt\n'), ((9323, 9366), 'altair.X', 'alt.X', (['"""created_at:T"""'], {'title': '"""Erstelldatum"""'}), "('created_at:T', title='Erstelldatum')\n", (9328, 9366), True, 'import altair as alt\n'), ((9376, 9417), 'altair.Y', 'alt.Y', (['"""count:Q"""'], {'title': '"""Sätze pro Monat"""'}), "('count:Q', title='Sätze pro Monat')\n", (9381, 9417), True, 'import altair as alt\n'), ((10207, 10258), 'streamlit.subheader', 'st.subheader', (['"""Entitäten und Katalogisierungslevel"""'], {}), "('Entitäten und Katalogisierungslevel')\n", (10219, 10258), True, 'import streamlit as st\n'), ((10692, 10751), 'streamlit.subheader', 'st.subheader', (['f"""Katalogisierungslevel in Satzart {satzart}"""'], {}), "(f'Katalogisierungslevel in Satzart {satzart}')\n", (10704, 10751), True, 'import streamlit as st\n'), ((11192, 11228), 'streamlit.subheader', 'st.subheader', (['f"""TOP 10 GND-Newcomer"""'], {}), "(f'TOP 10 GND-Newcomer')\n", (11204, 11228), True, 'import streamlit as st\n'), ((11237, 11325), 'streamlit.write', 'st.write', (['"""TOP 10 der GND-Entitäten, die in den letzten 365 Tagen angelegt wurden."""'], {}), "(\n 'TOP 10 der GND-Entitäten, die in den letzten 365 Tagen angelegt wurden.')\n", (11245, 11325), True, 'import streamlit as st\n'), ((11346, 11422), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/title_gnd_newcomer_top10.csv"""'], {'index_col': 'None'}), "(f'{path}/../stats/title_gnd_newcomer_top10.csv', index_col=None)\n", (11357, 11422), True, 'import pandas as pd\n'), ((11857, 11903), 'streamlit.subheader', 'st.subheader', (['f"""TOP 10 {satzart} GND-Newcomer"""'], {}), "(f'TOP 10 {satzart} GND-Newcomer')\n", (11869, 11903), True, 'import streamlit as st\n'), ((11912, 12008), 'streamlit.write', 'st.write', (['f"""TOP 10 der {satzart} Sätze, die in den letzten 365 Tagen angelegt wurden."""'], {}), "(\n f'TOP 10 der {satzart} Sätze, die in den letzten 365 Tagen angelegt wurden.'\n )\n", (11920, 12008), True, 'import streamlit as st\n'), ((12678, 12733), 'streamlit.subheader', 'st.subheader', (['f"""TOP 10 GND-Entitäten in DNB-Titeldaten"""'], {}), "(f'TOP 10 GND-Entitäten in DNB-Titeldaten')\n", (12690, 12733), True, 'import streamlit as st\n'), ((12754, 12821), 'pandas.read_csv', 'pd.read_csv', (['f"""{path}/../stats/title_gnd_top10.csv"""'], {'index_col': 'None'}), "(f'{path}/../stats/title_gnd_top10.csv', index_col=None)\n", (12765, 12821), True, 'import pandas as pd\n'), ((13256, 13307), 'streamlit.subheader', 'st.subheader', (['f"""TOP 10 {satzart} in DNB-Titeldaten"""'], {}), "(f'TOP 10 {satzart} in DNB-Titeldaten')\n", (13268, 13307), True, 'import streamlit as st\n'), ((15676, 15741), 'streamlit.subheader', 'st.subheader', (['"""Datenherkunft der GND-Entitäten in DNB-Titeldaten"""'], {}), "('Datenherkunft der GND-Entitäten in DNB-Titeldaten')\n", (15688, 15741), True, 'import streamlit as st\n'), ((15750, 16016), 'streamlit.write', 'st.write', (['"""Weniger als ein Drittel der GND-Entitäten in DNB-Titeldaten wurde in intellektuellen Erschließungsprozessen vergeben. Jeweils ca. ein weiteres Drittel wurde in maschinellen Erschließungsprozessen vergeben, ca. ein Drittel stammt aus Fremddaten."""'], {}), "(\n 'Weniger als ein Drittel der GND-Entitäten in DNB-Titeldaten wurde in intellektuellen Erschließungsprozessen vergeben. Jeweils ca. ein weiteres Drittel wurde in maschinellen Erschließungsprozessen vergeben, ca. ein Drittel stammt aus Fremddaten.'\n )\n", (15758, 16016), True, 'import streamlit as st\n'), ((16253, 16304), 'streamlit.altair_chart', 'st.altair_chart', (['entities'], {'use_container_width': '(True)'}), '(entities, use_container_width=True)\n', (16268, 16304), True, 'import streamlit as st\n'), ((16481, 16584), 'streamlit.write', 'st.write', (['f"""Durchschnittlich {mean} Verknüpfungen zu {satzart}-Sätzen pro DNB-Titeldatensatz"""'], {}), "(\n f'Durchschnittlich {mean} Verknüpfungen zu {satzart}-Sätzen pro DNB-Titeldatensatz'\n )\n", (16489, 16584), True, 'import streamlit as st\n'), ((17002, 17048), 'streamlit.beta_expander', 'st.beta_expander', (['"""Methodik und Datenherkunft"""'], {}), "('Methodik und Datenherkunft')\n", (17018, 17048), True, 'import streamlit as st\n'), ((17058, 18597), 'streamlit.markdown', 'st.markdown', (['"""\nDatengrundlage ist ein Gesamtabzug der Daten der Gemeinsamen Normadatei (GND) sowie der Titeldaten der Deutschen Nationalbibliothek (DNB) inkl. Zeitschriftendatenbank (ZDB), sofern sich Exemplare der Zeitschrift im Bestand der DNB befinden. In den Titeldaten ist auch der Tonträger- und Notenbestand des Deutschen Musikarchivs (DMA) sowie der Buch- und Objektbestand des Deutschen Buch- und Schriftmuseums (DBSM) nachgewiesen.\n\nDer Gesamtabzug liegt im OCLC-Format PICA+ vor. Die Daten werden mithilfe des Pica-Parsers [pica.rs](https://github.com/deutsche-nationalbibliothek/pica-rs) gefiltert. Dieses Tool produziert aus dem sehr großen Gesamtabzug (~ 31 GB) kleinere CSV-Dateien, die mit Python weiterverarbeitet werden.\n\nDas Dashboard ist mit dem Python-Framework [Streamlit](https://streamlit.io/) geschrieben. Die Skripte sowie die gefilterten CSV-Rohdaten sind auf [Github](https://github.com/buchmuseum/GND_Dashboard) zu finden. Die Diagramme wurden mit [Altair](https://altair-viz.github.io/index.html) erstellt, die Karten mit [Deck GL](https://deck.gl/) (via [Pydeck](https://deckgl.readthedocs.io/en/latest/#)), die Wordcloud mit [wordcloud](https://amueller.github.io/word_cloud/index.html).\n\nFür grundlegende Zugriffsstatistik verwenden wir [streamlit-analytics](https://pypi.org/project/streamlit-analytics/). Dabei werden keine personenbezogenen Daten gespeichert.\n\nAlle Skripte und Daten stehen unter CC0 Lizenz und können frei weitergenutzt werden.\n\nDie Daten werden monatlich aktualisiert.\n"""'], {}), '(\n """\nDatengrundlage ist ein Gesamtabzug der Daten der Gemeinsamen Normadatei (GND) sowie der Titeldaten der Deutschen Nationalbibliothek (DNB) inkl. Zeitschriftendatenbank (ZDB), sofern sich Exemplare der Zeitschrift im Bestand der DNB befinden. In den Titeldaten ist auch der Tonträger- und Notenbestand des Deutschen Musikarchivs (DMA) sowie der Buch- und Objektbestand des Deutschen Buch- und Schriftmuseums (DBSM) nachgewiesen.\n\nDer Gesamtabzug liegt im OCLC-Format PICA+ vor. Die Daten werden mithilfe des Pica-Parsers [pica.rs](https://github.com/deutsche-nationalbibliothek/pica-rs) gefiltert. Dieses Tool produziert aus dem sehr großen Gesamtabzug (~ 31 GB) kleinere CSV-Dateien, die mit Python weiterverarbeitet werden.\n\nDas Dashboard ist mit dem Python-Framework [Streamlit](https://streamlit.io/) geschrieben. Die Skripte sowie die gefilterten CSV-Rohdaten sind auf [Github](https://github.com/buchmuseum/GND_Dashboard) zu finden. Die Diagramme wurden mit [Altair](https://altair-viz.github.io/index.html) erstellt, die Karten mit [Deck GL](https://deck.gl/) (via [Pydeck](https://deckgl.readthedocs.io/en/latest/#)), die Wordcloud mit [wordcloud](https://amueller.github.io/word_cloud/index.html).\n\nFür grundlegende Zugriffsstatistik verwenden wir [streamlit-analytics](https://pypi.org/project/streamlit-analytics/). Dabei werden keine personenbezogenen Daten gespeichert.\n\nAlle Skripte und Daten stehen unter CC0 Lizenz und können frei weitergenutzt werden.\n\nDie Daten werden monatlich aktualisiert.\n"""\n )\n', (17069, 18597), True, 'import streamlit as st\n'), ((454, 487), 'pandas.read_csv', 'pd.read_csv', (['file'], {'index_col': 'None'}), '(file, index_col=None)\n', (465, 487), True, 'import pandas as pd\n'), ((9860, 9925), 'altair.X', 'alt.X', (['"""sum(count)"""'], {'title': '"""Datensätze pro Katalogisierungslevel"""'}), "('sum(count)', title='Datensätze pro Katalogisierungslevel')\n", (9865, 9925), True, 'import altair as alt\n'), ((9939, 9971), 'altair.Y', 'alt.Y', (['"""entity"""'], {'title': '"""Satzart"""'}), "('entity', title='Satzart')\n", (9944, 9971), True, 'import altair as alt\n'), ((9985, 10034), 'altair.Color', 'alt.Color', (['"""level"""'], {'title': '"""Katalogisierungslevel"""'}), "('level', title='Katalogisierungslevel')\n", (9994, 10034), True, 'import altair as alt\n'), ((10385, 10450), 'altair.X', 'alt.X', (['"""sum(count)"""'], {'title': '"""Datensätze pro Katalogisierungslevel"""'}), "('sum(count)', title='Datensätze pro Katalogisierungslevel')\n", (10390, 10450), True, 'import altair as alt\n'), ((10464, 10496), 'altair.Y', 'alt.Y', (['"""entity"""'], {'title': '"""Satzart"""'}), "('entity', title='Satzart')\n", (10469, 10496), True, 'import altair as alt\n'), ((10510, 10559), 'altair.Color', 'alt.Color', (['"""level"""'], {'title': '"""Katalogisierungslevel"""'}), "('level', title='Katalogisierungslevel')\n", (10519, 10559), True, 'import altair as alt\n'), ((11500, 11545), 'altair.X', 'alt.X', (['"""gnd_id"""'], {'title': '"""Entitäten"""', 'sort': '"""-y"""'}), "('gnd_id', title='Entitäten', sort='-y')\n", (11505, 11545), True, 'import altair as alt\n'), ((11559, 11589), 'altair.Y', 'alt.Y', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (11564, 11589), True, 'import altair as alt\n'), ((11603, 11648), 'altair.Color', 'alt.Color', (['"""name"""'], {'sort': '"""-y"""', 'title': '"""Entität"""'}), "('name', sort='-y', title='Entität')\n", (11612, 11648), True, 'import altair as alt\n'), ((12203, 12250), 'altair.X', 'alt.X', (['"""gnd_id:O"""'], {'title': '"""Entitäten"""', 'sort': '"""-y"""'}), "('gnd_id:O', title='Entitäten', sort='-y')\n", (12208, 12250), True, 'import altair as alt\n'), ((12264, 12294), 'altair.Y', 'alt.Y', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (12269, 12294), True, 'import altair as alt\n'), ((12308, 12353), 'altair.Color', 'alt.Color', (['"""name"""'], {'sort': '"""-y"""', 'title': '"""Entität"""'}), "('name', sort='-y', title='Entität')\n", (12317, 12353), True, 'import altair as alt\n'), ((12893, 12940), 'altair.X', 'alt.X', (['"""gnd_id:N"""'], {'title': '"""Entitäten"""', 'sort': '"""-y"""'}), "('gnd_id:N', title='Entitäten', sort='-y')\n", (12898, 12940), True, 'import altair as alt\n'), ((12954, 12986), 'altair.Y', 'alt.Y', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (12959, 12986), True, 'import altair as alt\n'), ((13000, 13047), 'altair.Color', 'alt.Color', (['"""name:N"""'], {'sort': '"""-y"""', 'title': '"""Entität"""'}), "('name:N', sort='-y', title='Entität')\n", (13009, 13047), True, 'import altair as alt\n'), ((13487, 13534), 'altair.X', 'alt.X', (['"""gnd_id:N"""'], {'title': '"""Entitäten"""', 'sort': '"""-y"""'}), "('gnd_id:N', title='Entitäten', sort='-y')\n", (13492, 13534), True, 'import altair as alt\n'), ((13548, 13580), 'altair.Y', 'alt.Y', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (13553, 13580), True, 'import altair as alt\n'), ((13594, 13641), 'altair.Color', 'alt.Color', (['"""name:N"""'], {'sort': '"""-y"""', 'title': '"""Entität"""'}), "('name:N', sort='-y', title='Entität')\n", (13603, 13641), True, 'import altair as alt\n'), ((16078, 16120), 'altair.X', 'alt.X', (['"""sum(Datenart):N"""'], {'title': '"""Datenart"""'}), "('sum(Datenart):N', title='Datenart')\n", (16083, 16120), True, 'import altair as alt\n'), ((16134, 16172), 'altair.Y', 'alt.Y', (['"""sum(Anzahl):Q"""'], {'title': '"""Anzahl"""'}), "('sum(Anzahl):Q', title='Anzahl')\n", (16139, 16172), True, 'import altair as alt\n'), ((2289, 2310), 'altair.Legend', 'alt.Legend', ([], {'columns': '(2)'}), '(columns=2)\n', (2299, 2310), True, 'import altair as alt\n'), ((2330, 2364), 'altair.Tooltip', 'alt.Tooltip', (['"""Name:N"""'], {'title': '"""Ort"""'}), "('Name:N', title='Ort')\n", (2341, 2364), True, 'import altair as alt\n'), ((2366, 2405), 'altair.Tooltip', 'alt.Tooltip', (['"""Anzahl:Q"""'], {'title': '"""Anzahl"""'}), "('Anzahl:Q', title='Anzahl')\n", (2377, 2405), True, 'import altair as alt\n'), ((5096, 5127), 'streamlit.write', 'st.write', (['f"""{i}. {row[\'name\']}"""'], {}), '(f"{i}. {row[\'name\']}")\n', (5104, 5127), True, 'import streamlit as st\n'), ((6379, 6415), 'altair.Tooltip', 'alt.Tooltip', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (6390, 6415), True, 'import altair as alt\n'), ((6417, 6450), 'altair.Tooltip', 'alt.Tooltip', (['"""code"""'], {'title': '"""Code"""'}), "('code', title='Code')\n", (6428, 6450), True, 'import altair as alt\n'), ((7436, 7471), 'altair.Tooltip', 'alt.Tooltip', (['"""id"""'], {'title': '"""Notation"""'}), "('id', title='Notation')\n", (7447, 7471), True, 'import altair as alt\n'), ((7473, 7513), 'altair.Tooltip', 'alt.Tooltip', (['"""name"""'], {'title': '"""Bezeichnung"""'}), "('name', title='Bezeichnung')\n", (7484, 7513), True, 'import altair as alt\n'), ((7515, 7551), 'altair.Tooltip', 'alt.Tooltip', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (7526, 7551), True, 'import altair as alt\n'), ((8489, 8524), 'altair.Tooltip', 'alt.Tooltip', (['"""id"""'], {'title': '"""Notation"""'}), "('id', title='Notation')\n", (8500, 8524), True, 'import altair as alt\n'), ((8526, 8566), 'altair.Tooltip', 'alt.Tooltip', (['"""name"""'], {'title': '"""Bezeichnung"""'}), "('name', title='Bezeichnung')\n", (8537, 8566), True, 'import altair as alt\n'), ((8568, 8604), 'altair.Tooltip', 'alt.Tooltip', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (8579, 8604), True, 'import altair as alt\n'), ((15386, 15580), 'pandas.DataFrame.from_dict', 'pd.DataFrame.from_dict', (["{'intellektuell verknüpfte Entitäten': uniques,\n 'Entitäten aus automatischen Prozessen': auto_entites,\n 'Entitäten aus Fremddaten': fremd_entities}"], {'orient': '"""index"""'}), "({'intellektuell verknüpfte Entitäten': uniques,\n 'Entitäten aus automatischen Prozessen': auto_entites,\n 'Entitäten aus Fremddaten': fremd_entities}, orient='index')\n", (15408, 15580), True, 'import pandas as pd\n'), ((5187, 5218), 'streamlit.write', 'st.write', (['f"""{i}. {row[\'name\']}"""'], {}), '(f"{i}. {row[\'name\']}")\n', (5195, 5218), True, 'import streamlit as st\n'), ((10057, 10095), 'altair.Tooltip', 'alt.Tooltip', (['"""entity"""'], {'title': '"""Satzart"""'}), "('entity', title='Satzart')\n", (10068, 10095), True, 'import altair as alt\n'), ((10097, 10148), 'altair.Tooltip', 'alt.Tooltip', (['"""level"""'], {'title': '"""Katalogisierungslevel"""'}), "('level', title='Katalogisierungslevel')\n", (10108, 10148), True, 'import altair as alt\n'), ((10151, 10187), 'altair.Tooltip', 'alt.Tooltip', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (10162, 10187), True, 'import altair as alt\n'), ((10582, 10633), 'altair.Tooltip', 'alt.Tooltip', (['"""level"""'], {'title': '"""Katalogisierungslevel"""'}), "('level', title='Katalogisierungslevel')\n", (10593, 10633), True, 'import altair as alt\n'), ((10636, 10672), 'altair.Tooltip', 'alt.Tooltip', (['"""count"""'], {'title': '"""Anzahl"""'}), "('count', title='Anzahl')\n", (10647, 10672), True, 'import altair as alt\n'), ((11671, 11709), 'altair.Tooltip', 'alt.Tooltip', (['"""name:N"""'], {'title': '"""Entität"""'}), "('name:N', title='Entität')\n", (11682, 11709), True, 'import altair as alt\n'), ((11712, 11749), 'altair.Tooltip', 'alt.Tooltip', (['"""bbg:N"""'], {'title': '"""Satzart"""'}), "('bbg:N', title='Satzart')\n", (11723, 11749), True, 'import altair as alt\n'), ((11751, 11787), 'altair.Tooltip', 'alt.Tooltip', (['"""gnd_id:N"""'], {'title': '"""IDN"""'}), "('gnd_id:N', title='IDN')\n", (11762, 11787), True, 'import altair as alt\n'), ((11789, 11827), 'altair.Tooltip', 'alt.Tooltip', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (11800, 11827), True, 'import altair as alt\n'), ((12376, 12414), 'altair.Tooltip', 'alt.Tooltip', (['"""name:N"""'], {'title': '"""Entität"""'}), "('name:N', title='Entität')\n", (12387, 12414), True, 'import altair as alt\n'), ((12417, 12453), 'altair.Tooltip', 'alt.Tooltip', (['"""gnd_id:N"""'], {'title': '"""IDN"""'}), "('gnd_id:N', title='IDN')\n", (12428, 12453), True, 'import altair as alt\n'), ((12455, 12493), 'altair.Tooltip', 'alt.Tooltip', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (12466, 12493), True, 'import altair as alt\n'), ((13070, 13108), 'altair.Tooltip', 'alt.Tooltip', (['"""name:N"""'], {'title': '"""Entität"""'}), "('name:N', title='Entität')\n", (13081, 13108), True, 'import altair as alt\n'), ((13111, 13147), 'altair.Tooltip', 'alt.Tooltip', (['"""gnd_id:N"""'], {'title': '"""IDN"""'}), "('gnd_id:N', title='IDN')\n", (13122, 13147), True, 'import altair as alt\n'), ((13149, 13186), 'altair.Tooltip', 'alt.Tooltip', (['"""bbg:N"""'], {'title': '"""Satzart"""'}), "('bbg:N', title='Satzart')\n", (13160, 13186), True, 'import altair as alt\n'), ((13188, 13226), 'altair.Tooltip', 'alt.Tooltip', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (13199, 13226), True, 'import altair as alt\n'), ((13664, 13702), 'altair.Tooltip', 'alt.Tooltip', (['"""name:N"""'], {'title': '"""Entität"""'}), "('name:N', title='Entität')\n", (13675, 13702), True, 'import altair as alt\n'), ((13705, 13741), 'altair.Tooltip', 'alt.Tooltip', (['"""gnd_id:N"""'], {'title': '"""IDN"""'}), "('gnd_id:N', title='IDN')\n", (13716, 13741), True, 'import altair as alt\n'), ((13743, 13781), 'altair.Tooltip', 'alt.Tooltip', (['"""count:Q"""'], {'title': '"""Anzahl"""'}), "('count:Q', title='Anzahl')\n", (13754, 13781), True, 'import altair as alt\n'), ((9815, 9828), 'altair.Chart', 'alt.Chart', (['df'], {}), '(df)\n', (9824, 9828), True, 'import altair as alt\n'), ((11443, 11468), 'altair.Chart', 'alt.Chart', (['newcomer_daten'], {}), '(newcomer_daten)\n', (11452, 11468), True, 'import altair as alt\n'), ((12841, 12861), 'altair.Chart', 'alt.Chart', (['top_daten'], {}), '(top_daten)\n', (12850, 12861), True, 'import altair as alt\n'), ((16026, 16046), 'altair.Chart', 'alt.Chart', (['entity_df'], {}), '(entity_df)\n', (16035, 16046), True, 'import altair as alt\n')]
amandalynne/Seattle-Mobility-Index
seamo/support/seamo_exceptions.py
f21d2fa6913ce9474aedc298e9e4a6e7c9390e64
""" Class for all excpetions used in following scripts - geocoder.py - geocoder_input.py """ class OverlappingGeographyError(Exception): def __init__(self, message): self.message = message # msg: geodataframe has overlapping polygons representing geographic features # please how shapefiles are processed. class NoOverlapSpatialJoinError(Exception): def __init__(self, message): self.message = message # msg: geodataframe has overlapping polygons representing geographic features # please how shapefiles are processed. class NoParkingAvailableError(NoOverlapSpatialJoinError): def __init__(self, message): self.message = message class NoUrbanVillageError(Exception): def __init__(self, message): self.message = message class NotInSeattleError(Exception): def __init__(self, message): self.message = message
[]
hasan-se/blm304
vize/150401052/sunucu.py
893d15282497a426ff96b0c8b6c77d57c406742e
#Erdin Alhas 150401052 import os import sys import time from socket import * from os import system, name ip = '127.0.0.1' port = 42 s_soket = socket(AF_INET, SOCK_DGRAM) s_soket.bind((ip, port)) print("\nSunucu Hazir\n") kontrol, istemciAdres = s_soket.recvfrom(4096) s_soket.sendto(bytes("Sunucu hazir", encoding='utf-8'), istemciAdres) i, istemciAdres = s_soket.recvfrom(4096) if(i.decode("utf-8") == "listeleme yap"): dosyalar = "\n".join(os.listdir()) s_soket.sendto(bytes(dosyalar, encoding='utf-8'), istemciAdres) sys.exit() elif(i.decode("utf-8") == "put yap"): cevap = s_soket.recvfrom(4096) if(cevap[0].decode("utf-8") == "mevcut"): dosyaIsmi, istemciAdres = s_soket.recvfrom(4096) dosyaIcerigi = s_soket.recvfrom(4096) if(os.path.exists(dosyaIsmi.decode("utf-8")) == True): s_soket.sendto(bytes("aynisi mevcut", encoding='utf-8'), istemciAdres) karar = s_soket.recvfrom(4096) if(karar[0].decode("utf-8") == "1"): yeniAd = dosyaIsmi.decode("utf-8")[:-4] + " (kopya)" + ".txt" dosyaYeni = open(yeniAd, "wb") dosyaYeni.write(dosyaIcerigi[0]) dosyaYeni.close() print("\nPUT islemi basariyla gerceklesti..") else: dosyaYeni = open(dosyaIsmi, "wb") dosyaYeni.write(dosyaIcerigi[0]) dosyaYeni.close() s_soket.sendto(bytes("tamam", encoding='utf-8'), istemciAdres) print("\nPUT islemi basariyla gerceklesti..") else: print("\nGirilen adda bir dosya istemcide bulunamadi..") elif(i.decode("utf-8") == "get yap"): dosyaIsmi, istemciAdres = s_soket.recvfrom(4096) if (os.path.exists(dosyaIsmi.decode("utf-8")) == True): dosya = open(dosyaIsmi.decode("utf-8"), "rb") s_soket.sendto(bytes("dosya mevcut", encoding='utf-8'), istemciAdres) dosyaIcerik = dosya.read() dosya.close() s_soket.sendto(dosyaIcerik, istemciAdres) kontrol = s_soket.recvfrom(4096) print("\nGET islemi basariyla gerceklesti..") sys.exit() else: print("\n! Bu isimde bir dosya sunucuda mevcut değil") sys.exit() elif(i.decode("utf-8") == "bitir"): s_soket.close() print("\nSunucu kapandi") sys.exit()
[((707, 717), 'sys.exit', 'sys.exit', ([], {}), '()\n', (715, 717), False, 'import sys\n'), ((566, 578), 'os.listdir', 'os.listdir', ([], {}), '()\n', (576, 578), False, 'import os\n'), ((2836, 2846), 'sys.exit', 'sys.exit', ([], {}), '()\n', (2844, 2846), False, 'import sys\n'), ((3010, 3020), 'sys.exit', 'sys.exit', ([], {}), '()\n', (3018, 3020), False, 'import sys\n'), ((3179, 3189), 'sys.exit', 'sys.exit', ([], {}), '()\n', (3187, 3189), False, 'import sys\n')]
panyuan209/httprunner
httprunner/compat.py
d90f2b9ab06963e8efa1c327975fca5296d6bc39
""" This module handles compatibility issues between testcase format v2 and v3. 解决httprunner2 和 3 之间测试用例兼容性问题 """ import os import sys from typing import List, Dict, Text, Union, Any from loguru import logger from httprunner import exceptions from httprunner.loader import load_project_meta, convert_relative_project_root_dir from httprunner.parser import parse_data from httprunner.utils import sort_dict_by_custom_order def convert_variables( raw_variables: Union[Dict, List, Text], test_path: Text ) -> Dict[Text, Any]: if isinstance(raw_variables, Dict): return raw_variables if isinstance(raw_variables, List): # [{"var1": 1}, {"var2": 2}] variables: Dict[Text, Any] = {} for var_item in raw_variables: if not isinstance(var_item, Dict) or len(var_item) != 1: raise exceptions.TestCaseFormatError( f"Invalid variables format: {raw_variables}" ) variables.update(var_item) return variables elif isinstance(raw_variables, Text): # get variables by function, e.g. ${get_variables()} project_meta = load_project_meta(test_path) variables = parse_data(raw_variables, {}, project_meta.functions) return variables else: raise exceptions.TestCaseFormatError( f"Invalid variables format: {raw_variables}" ) def _convert_jmespath(raw: Text) -> Text: if not isinstance(raw, Text): raise exceptions.TestCaseFormatError(f"Invalid jmespath extractor: {raw}") # content.xx/json.xx => body.xx if raw.startswith("content"): raw = f"body{raw[len('content'):]}" elif raw.startswith("json"): raw = f"body{raw[len('json'):]}" raw_list = [] for item in raw.split("."): if "-" in item: # add quotes for field with separator # e.g. headers.Content-Type => headers."Content-Type" item = item.strip('"') raw_list.append(f'"{item}"') elif item.isdigit(): # convert lst.0.name to lst[0].name if len(raw_list) == 0: logger.error(f"Invalid jmespath: {raw}") sys.exit(1) last_item = raw_list.pop() item = f"{last_item}[{item}]" raw_list.append(item) else: raw_list.append(item) return ".".join(raw_list) def _convert_extractors(extractors: Union[List, Dict]) -> Dict: """ convert extract list(v2) to dict(v3) Args: extractors: [{"varA": "content.varA"}, {"varB": "json.varB"}] Returns: {"varA": "body.varA", "varB": "body.varB"} """ v3_extractors: Dict = {} if isinstance(extractors, List): # [{"varA": "content.varA"}, {"varB": "json.varB"}] for extractor in extractors: if not isinstance(extractor, Dict): logger.error(f"Invalid extractor: {extractors}") sys.exit(1) for k, v in extractor.items(): v3_extractors[k] = v elif isinstance(extractors, Dict): # {"varA": "body.varA", "varB": "body.varB"} v3_extractors = extractors else: logger.error(f"Invalid extractor: {extractors}") sys.exit(1) for k, v in v3_extractors.items(): v3_extractors[k] = _convert_jmespath(v) return v3_extractors def _convert_validators(validators: List) -> List: for v in validators: if "check" in v and "expect" in v: # format1: {"check": "content.abc", "assert": "eq", "expect": 201} v["check"] = _convert_jmespath(v["check"]) elif len(v) == 1: # format2: {'eq': ['status_code', 201]} comparator = list(v.keys())[0] v[comparator][0] = _convert_jmespath(v[comparator][0]) return validators def _sort_request_by_custom_order(request: Dict) -> Dict: custom_order = [ "method", "url", "params", "headers", "cookies", "data", "json", "files", "timeout", "allow_redirects", "proxies", "verify", "stream", "auth", "cert", ] return sort_dict_by_custom_order(request, custom_order) def _sort_step_by_custom_order(step: Dict) -> Dict: custom_order = [ "name", "variables", "request", "testcase", "setup_hooks", "teardown_hooks", "extract", "validate", "validate_script", ] return sort_dict_by_custom_order(step, custom_order) def _ensure_step_attachment(step: Dict) -> Dict: test_dict = { "name": step["name"], } if "variables" in step: test_dict["variables"] = step["variables"] if "setup_hooks" in step: test_dict["setup_hooks"] = step["setup_hooks"] if "teardown_hooks" in step: test_dict["teardown_hooks"] = step["teardown_hooks"] if "extract" in step: test_dict["extract"] = _convert_extractors(step["extract"]) if "export" in step: test_dict["export"] = step["export"] if "validate" in step: if not isinstance(step["validate"], List): raise exceptions.TestCaseFormatError( f'Invalid teststep validate: {step["validate"]}' ) test_dict["validate"] = _convert_validators(step["validate"]) if "validate_script" in step: test_dict["validate_script"] = step["validate_script"] return test_dict def ensure_testcase_v3_api(api_content: Dict) -> Dict: logger.info("convert api in v2 to testcase format v3") teststep = { "request": _sort_request_by_custom_order(api_content["request"]), } teststep.update(_ensure_step_attachment(api_content)) teststep = _sort_step_by_custom_order(teststep) config = {"name": api_content["name"]} extract_variable_names: List = list(teststep.get("extract", {}).keys()) if extract_variable_names: config["export"] = extract_variable_names return { "config": config, "teststeps": [teststep], } def ensure_testcase_v3(test_content: Dict) -> Dict: logger.info("ensure compatibility with testcase format v2") v3_content = {"config": test_content["config"], "teststeps": []} if "teststeps" not in test_content: logger.error(f"Miss teststeps: {test_content}") sys.exit(1) if not isinstance(test_content["teststeps"], list): logger.error( f'teststeps should be list type, got {type(test_content["teststeps"])}: {test_content["teststeps"]}' ) sys.exit(1) for step in test_content["teststeps"]: teststep = {} if "request" in step: teststep["request"] = _sort_request_by_custom_order(step.pop("request")) elif "api" in step: teststep["testcase"] = step.pop("api") elif "testcase" in step: teststep["testcase"] = step.pop("testcase") else: raise exceptions.TestCaseFormatError(f"Invalid teststep: {step}") teststep.update(_ensure_step_attachment(step)) teststep = _sort_step_by_custom_order(teststep) v3_content["teststeps"].append(teststep) return v3_content def ensure_cli_args(args: List) -> List: """ ensure compatibility with deprecated cli args in v2 """ # remove deprecated --failfast if "--failfast" in args: logger.warning(f"remove deprecated argument: --failfast") args.pop(args.index("--failfast")) # convert --report-file to --html if "--report-file" in args: logger.warning(f"replace deprecated argument --report-file with --html") index = args.index("--report-file") args[index] = "--html" args.append("--self-contained-html") # keep compatibility with --save-tests in v2 if "--save-tests" in args: logger.warning( f"generate conftest.py keep compatibility with --save-tests in v2" ) args.pop(args.index("--save-tests")) _generate_conftest_for_summary(args) return args def _generate_conftest_for_summary(args: List): for arg in args: if os.path.exists(arg): test_path = arg # FIXME: several test paths maybe specified break else: logger.error(f"No valid test path specified! \nargs: {args}") sys.exit(1) conftest_content = '''# NOTICE: Generated By HttpRunner. import json import os import time import pytest from loguru import logger from httprunner.utils import get_platform, ExtendJSONEncoder @pytest.fixture(scope="session", autouse=True) def session_fixture(request): """setup and teardown each task""" logger.info(f"start running testcases ...") start_at = time.time() yield logger.info(f"task finished, generate task summary for --save-tests") summary = { "success": True, "stat": { "testcases": {"total": 0, "success": 0, "fail": 0}, "teststeps": {"total": 0, "failures": 0, "successes": 0}, }, "time": {"start_at": start_at, "duration": time.time() - start_at}, "platform": get_platform(), "details": [], } for item in request.node.items: testcase_summary = item.instance.get_summary() summary["success"] &= testcase_summary.success summary["stat"]["testcases"]["total"] += 1 summary["stat"]["teststeps"]["total"] += len(testcase_summary.step_datas) if testcase_summary.success: summary["stat"]["testcases"]["success"] += 1 summary["stat"]["teststeps"]["successes"] += len( testcase_summary.step_datas ) else: summary["stat"]["testcases"]["fail"] += 1 summary["stat"]["teststeps"]["successes"] += ( len(testcase_summary.step_datas) - 1 ) summary["stat"]["teststeps"]["failures"] += 1 testcase_summary_json = testcase_summary.dict() testcase_summary_json["records"] = testcase_summary_json.pop("step_datas") summary["details"].append(testcase_summary_json) summary_path = r"{{SUMMARY_PATH_PLACEHOLDER}}" summary_dir = os.path.dirname(summary_path) os.makedirs(summary_dir, exist_ok=True) with open(summary_path, "w", encoding="utf-8") as f: json.dump(summary, f, indent=4, ensure_ascii=False, cls=ExtendJSONEncoder) logger.info(f"generated task summary: {summary_path}") ''' project_meta = load_project_meta(test_path) project_root_dir = project_meta.RootDir conftest_path = os.path.join(project_root_dir, "conftest.py") test_path = os.path.abspath(test_path) logs_dir_path = os.path.join(project_root_dir, "logs") test_path_relative_path = convert_relative_project_root_dir(test_path) if os.path.isdir(test_path): file_foder_path = os.path.join(logs_dir_path, test_path_relative_path) dump_file_name = "all.summary.json" else: file_relative_folder_path, test_file = os.path.split(test_path_relative_path) file_foder_path = os.path.join(logs_dir_path, file_relative_folder_path) test_file_name, _ = os.path.splitext(test_file) dump_file_name = f"{test_file_name}.summary.json" summary_path = os.path.join(file_foder_path, dump_file_name) conftest_content = conftest_content.replace( "{{SUMMARY_PATH_PLACEHOLDER}}", summary_path ) dir_path = os.path.dirname(conftest_path) if not os.path.exists(dir_path): os.makedirs(dir_path) with open(conftest_path, "w", encoding="utf-8") as f: f.write(conftest_content) logger.info("generated conftest.py to generate summary.json") def ensure_path_sep(path: Text) -> Text: """ ensure compatibility with different path separators of Linux and Windows """ if "/" in path: path = os.sep.join(path.split("/")) if "\\" in path: path = os.sep.join(path.split("\\")) return path
[((4234, 4282), 'httprunner.utils.sort_dict_by_custom_order', 'sort_dict_by_custom_order', (['request', 'custom_order'], {}), '(request, custom_order)\n', (4259, 4282), False, 'from httprunner.utils import sort_dict_by_custom_order\n'), ((4566, 4611), 'httprunner.utils.sort_dict_by_custom_order', 'sort_dict_by_custom_order', (['step', 'custom_order'], {}), '(step, custom_order)\n', (4591, 4611), False, 'from httprunner.utils import sort_dict_by_custom_order\n'), ((5603, 5657), 'loguru.logger.info', 'logger.info', (['"""convert api in v2 to testcase format v3"""'], {}), "('convert api in v2 to testcase format v3')\n", (5614, 5657), False, 'from loguru import logger\n'), ((6205, 6264), 'loguru.logger.info', 'logger.info', (['"""ensure compatibility with testcase format v2"""'], {}), "('ensure compatibility with testcase format v2')\n", (6216, 6264), False, 'from loguru import logger\n'), ((10603, 10631), 'httprunner.loader.load_project_meta', 'load_project_meta', (['test_path'], {}), '(test_path)\n', (10620, 10631), False, 'from httprunner.loader import load_project_meta, convert_relative_project_root_dir\n'), ((10696, 10741), 'os.path.join', 'os.path.join', (['project_root_dir', '"""conftest.py"""'], {}), "(project_root_dir, 'conftest.py')\n", (10708, 10741), False, 'import os\n'), ((10759, 10785), 'os.path.abspath', 'os.path.abspath', (['test_path'], {}), '(test_path)\n', (10774, 10785), False, 'import os\n'), ((10806, 10844), 'os.path.join', 'os.path.join', (['project_root_dir', '"""logs"""'], {}), "(project_root_dir, 'logs')\n", (10818, 10844), False, 'import os\n'), ((10875, 10919), 'httprunner.loader.convert_relative_project_root_dir', 'convert_relative_project_root_dir', (['test_path'], {}), '(test_path)\n', (10908, 10919), False, 'from httprunner.loader import load_project_meta, convert_relative_project_root_dir\n'), ((10928, 10952), 'os.path.isdir', 'os.path.isdir', (['test_path'], {}), '(test_path)\n', (10941, 10952), False, 'import os\n'), ((11388, 11433), 'os.path.join', 'os.path.join', (['file_foder_path', 'dump_file_name'], {}), '(file_foder_path, dump_file_name)\n', (11400, 11433), False, 'import os\n'), ((11558, 11588), 'os.path.dirname', 'os.path.dirname', (['conftest_path'], {}), '(conftest_path)\n', (11573, 11588), False, 'import os\n'), ((11754, 11815), 'loguru.logger.info', 'logger.info', (['"""generated conftest.py to generate summary.json"""'], {}), "('generated conftest.py to generate summary.json')\n", (11765, 11815), False, 'from loguru import logger\n'), ((1502, 1570), 'httprunner.exceptions.TestCaseFormatError', 'exceptions.TestCaseFormatError', (['f"""Invalid jmespath extractor: {raw}"""'], {}), "(f'Invalid jmespath extractor: {raw}')\n", (1532, 1570), False, 'from httprunner import exceptions\n'), ((6384, 6431), 'loguru.logger.error', 'logger.error', (['f"""Miss teststeps: {test_content}"""'], {}), "(f'Miss teststeps: {test_content}')\n", (6396, 6431), False, 'from loguru import logger\n'), ((6440, 6451), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (6448, 6451), False, 'import sys\n'), ((6662, 6673), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (6670, 6673), False, 'import sys\n'), ((7484, 7541), 'loguru.logger.warning', 'logger.warning', (['f"""remove deprecated argument: --failfast"""'], {}), "(f'remove deprecated argument: --failfast')\n", (7498, 7541), False, 'from loguru import logger\n'), ((7664, 7736), 'loguru.logger.warning', 'logger.warning', (['f"""replace deprecated argument --report-file with --html"""'], {}), "(f'replace deprecated argument --report-file with --html')\n", (7678, 7736), False, 'from loguru import logger\n'), ((7946, 8033), 'loguru.logger.warning', 'logger.warning', (['f"""generate conftest.py keep compatibility with --save-tests in v2"""'], {}), "(\n f'generate conftest.py keep compatibility with --save-tests in v2')\n", (7960, 8033), False, 'from loguru import logger\n'), ((8241, 8260), 'os.path.exists', 'os.path.exists', (['arg'], {}), '(arg)\n', (8255, 8260), False, 'import os\n'), ((8382, 8446), 'loguru.logger.error', 'logger.error', (['f"""No valid test path specified! \nargs: {args}"""'], {}), '(f"""No valid test path specified! \nargs: {args}""")\n', (8394, 8446), False, 'from loguru import logger\n'), ((8452, 8463), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (8460, 8463), False, 'import sys\n'), ((10980, 11032), 'os.path.join', 'os.path.join', (['logs_dir_path', 'test_path_relative_path'], {}), '(logs_dir_path, test_path_relative_path)\n', (10992, 11032), False, 'import os\n'), ((11134, 11172), 'os.path.split', 'os.path.split', (['test_path_relative_path'], {}), '(test_path_relative_path)\n', (11147, 11172), False, 'import os\n'), ((11199, 11253), 'os.path.join', 'os.path.join', (['logs_dir_path', 'file_relative_folder_path'], {}), '(logs_dir_path, file_relative_folder_path)\n', (11211, 11253), False, 'import os\n'), ((11282, 11309), 'os.path.splitext', 'os.path.splitext', (['test_file'], {}), '(test_file)\n', (11298, 11309), False, 'import os\n'), ((11600, 11624), 'os.path.exists', 'os.path.exists', (['dir_path'], {}), '(dir_path)\n', (11614, 11624), False, 'import os\n'), ((11634, 11655), 'os.makedirs', 'os.makedirs', (['dir_path'], {}), '(dir_path)\n', (11645, 11655), False, 'import os\n'), ((1157, 1185), 'httprunner.loader.load_project_meta', 'load_project_meta', (['test_path'], {}), '(test_path)\n', (1174, 1185), False, 'from httprunner.loader import load_project_meta, convert_relative_project_root_dir\n'), ((1206, 1259), 'httprunner.parser.parse_data', 'parse_data', (['raw_variables', '{}', 'project_meta.functions'], {}), '(raw_variables, {}, project_meta.functions)\n', (1216, 1259), False, 'from httprunner.parser import parse_data\n'), ((1311, 1387), 'httprunner.exceptions.TestCaseFormatError', 'exceptions.TestCaseFormatError', (['f"""Invalid variables format: {raw_variables}"""'], {}), "(f'Invalid variables format: {raw_variables}')\n", (1341, 1387), False, 'from httprunner import exceptions\n'), ((3215, 3263), 'loguru.logger.error', 'logger.error', (['f"""Invalid extractor: {extractors}"""'], {}), "(f'Invalid extractor: {extractors}')\n", (3227, 3263), False, 'from loguru import logger\n'), ((3272, 3283), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (3280, 3283), False, 'import sys\n'), ((5241, 5326), 'httprunner.exceptions.TestCaseFormatError', 'exceptions.TestCaseFormatError', (['f"""Invalid teststep validate: {step[\'validate\']}"""'], {}), '(f"Invalid teststep validate: {step[\'validate\']}"\n )\n', (5271, 5326), False, 'from httprunner import exceptions\n'), ((849, 925), 'httprunner.exceptions.TestCaseFormatError', 'exceptions.TestCaseFormatError', (['f"""Invalid variables format: {raw_variables}"""'], {}), "(f'Invalid variables format: {raw_variables}')\n", (879, 925), False, 'from httprunner import exceptions\n'), ((2913, 2961), 'loguru.logger.error', 'logger.error', (['f"""Invalid extractor: {extractors}"""'], {}), "(f'Invalid extractor: {extractors}')\n", (2925, 2961), False, 'from loguru import logger\n'), ((2978, 2989), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (2986, 2989), False, 'import sys\n'), ((2155, 2195), 'loguru.logger.error', 'logger.error', (['f"""Invalid jmespath: {raw}"""'], {}), "(f'Invalid jmespath: {raw}')\n", (2167, 2195), False, 'from loguru import logger\n'), ((2212, 2223), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (2220, 2223), False, 'import sys\n'), ((7056, 7115), 'httprunner.exceptions.TestCaseFormatError', 'exceptions.TestCaseFormatError', (['f"""Invalid teststep: {step}"""'], {}), "(f'Invalid teststep: {step}')\n", (7086, 7115), False, 'from httprunner import exceptions\n')]
ContinuumIO/chaco
examples/demo/basic/scatter.py
e4a42b91cb25ef7191fd465caaef2c3256fc668e
""" Scatter plot with panning and zooming Shows a scatter plot of a set of random points, with basic Chaco panning and zooming. Interacting with the plot: - Left-mouse-drag pans the plot. - Mouse wheel up and down zooms the plot in and out. - Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow and alt-right-arrow moves you forwards and backwards through the "zoom history". """ # Major library imports from numpy import sort from numpy.random import random # Enthought library imports from enable.api import Component, ComponentEditor from traits.api import HasTraits, Instance from traitsui.api import Item, Group, View # Chaco imports from chaco.api import ArrayPlotData, Plot from chaco.tools.api import PanTool, ZoomTool #=============================================================================== # # Create the Chaco plot. #=============================================================================== def _create_plot_component(): # Create some data numpts = 5000 x = sort(random(numpts)) y = random(numpts) # Create a plot data obect and give it this data pd = ArrayPlotData() pd.set_data("index", x) pd.set_data("value", y) # Create the plot plot = Plot(pd) plot.plot(("index", "value"), type="scatter", marker="circle", index_sort="ascending", color="orange", marker_size=3, bgcolor="white") # Tweak some of the plot properties plot.title = "Scatter Plot" plot.line_width = 0.5 plot.padding = 50 # Attach some tools to the plot plot.tools.append(PanTool(plot, constrain_key="shift")) zoom = ZoomTool(component=plot, tool_mode="box", always_on=False) plot.overlays.append(zoom) return plot #=============================================================================== # Attributes to use for the plot view. size = (650, 650) title = "Basic scatter plot" bg_color="lightgray" #=============================================================================== # # Demo class that is used by the demo.py application. #=============================================================================== class Demo(HasTraits): plot = Instance(Component) traits_view = View( Group( Item('plot', editor=ComponentEditor(size=size, bgcolor=bg_color), show_label=False), orientation = "vertical"), resizable=True, title=title ) def _plot_default(self): return _create_plot_component() demo = Demo() if __name__ == "__main__": demo.configure_traits() #--EOF---
[((1150, 1164), 'numpy.random.random', 'random', (['numpts'], {}), '(numpts)\n', (1156, 1164), False, 'from numpy.random import random\n'), ((1228, 1243), 'chaco.api.ArrayPlotData', 'ArrayPlotData', ([], {}), '()\n', (1241, 1243), False, 'from chaco.api import ArrayPlotData, Plot\n'), ((1334, 1342), 'chaco.api.Plot', 'Plot', (['pd'], {}), '(pd)\n', (1338, 1342), False, 'from chaco.api import ArrayPlotData, Plot\n'), ((1795, 1853), 'chaco.tools.api.ZoomTool', 'ZoomTool', ([], {'component': 'plot', 'tool_mode': '"""box"""', 'always_on': '(False)'}), "(component=plot, tool_mode='box', always_on=False)\n", (1803, 1853), False, 'from chaco.tools.api import PanTool, ZoomTool\n'), ((2344, 2363), 'traits.api.Instance', 'Instance', (['Component'], {}), '(Component)\n', (2352, 2363), False, 'from traits.api import HasTraits, Instance\n'), ((1126, 1140), 'numpy.random.random', 'random', (['numpts'], {}), '(numpts)\n', (1132, 1140), False, 'from numpy.random import random\n'), ((1746, 1782), 'chaco.tools.api.PanTool', 'PanTool', (['plot'], {'constrain_key': '"""shift"""'}), "(plot, constrain_key='shift')\n", (1753, 1782), False, 'from chaco.tools.api import PanTool, ZoomTool\n'), ((2460, 2504), 'enable.api.ComponentEditor', 'ComponentEditor', ([], {'size': 'size', 'bgcolor': 'bg_color'}), '(size=size, bgcolor=bg_color)\n', (2475, 2504), False, 'from enable.api import Component, ComponentEditor\n')]
fbalak/webstr
webstr/core/config.py
7c7e552fb9943bf664b94ca75a88747c0b243722
""" Central configuration module of webstr selenium tests. This module provides configuration options along with default values and function to redefine values. """ # Copyright 2016 Red Hat # # 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. import logging import sys SELENIUM_LOG_LEVEL = logging.INFO SCHEME = 'https' PORT = 443 BROWSER = 'Firefox' BROWSER_VERSION = '' BROWSER_PLATFORM = 'ANY' SELENIUM_SERVER = None SELENIUM_PORT = 4444 BROWSER_WIDTH = 1280 BROWSER_HEIGHT = 1024 def update_value(key_name, value, force=False): """ Update single value of this config module. """ this_module = sys.modules[__name__] key_name = key_name.upper() # raise AttributeError if we try to define new value (unless force is used) if not force: getattr(this_module, key_name) setattr(this_module, key_name, value)
[]
PiotrBosowski/feat-genes
operations/mutations/mutation.py
8e6604fd4e121022f8ac988d9b56985de01b8331
import random class Mutation: def __init__(self, chrom_mut_chance, gen_mut_chance): self.chrom_mut_chance = chrom_mut_chance self.gen_mut_chance = gen_mut_chance def __call__(self, population): chroms_to_mutate = random.sample( population, round(self.chrom_mut_chance * len(population))) for chrom in chroms_to_mutate: genes_to_mutate = random.sample( range(len(chrom)), round(self.gen_mut_chance * len(chrom))) for gt in genes_to_mutate: chrom[gt] = int(not bool(chrom[gt])) return population
[]
Ellis0817/Introduction-to-Programming-Using-Python
examples/CountLettersInList.py
1882a2a846162d5ff56d4d56c3940b638ef408bd
import RandomCharacter # Defined in Listing 6.9 def main(): """Main.""" # Create a list of characters chars = createList() # Display the list print("The lowercase letters are:") displayList(chars) # Count the occurrences of each letter counts = countLetters(chars) # Display counts print("The occurrences of each letter are:") displayCounts(counts) def createList(): """Create a list of characters.""" # Create an empty list chars = [] # Create lowercase letters randomly and add them to the list for i in range(100): chars.append(RandomCharacter.getRandomLowerCaseLetter()) # Return the list return chars def displayList(chars): """Display the list of characters.""" # Display the characters in the list 20 on each line for i in range(len(chars)): if (i + 1) % 20 == 0: print(chars[i]) else: print(chars[i], end=' ') def countLetters(chars): """Count the occurrences of each letter.""" # Create a list of 26 integers with initial value 0 counts = 26 * [0] # For each lowercase letter in the list, count it for i in range(len(chars)): counts[ord(chars[i]) - ord('a')] += 1 return counts def displayCounts(counts): """Display counts.""" for i in range(len(counts)): if (i + 1) % 10 == 0: print(counts[i], chr(i + ord('a'))) else: print(counts[i], chr(i + ord('a')), end=' ') print() main() # Call the main function
[((612, 654), 'RandomCharacter.getRandomLowerCaseLetter', 'RandomCharacter.getRandomLowerCaseLetter', ([], {}), '()\n', (652, 654), False, 'import RandomCharacter\n')]
lightstep/dd-trace-py
ddtrace/contrib/vertica/__init__.py
9108cbf54ff31f803eac735507ae6d2a87b9b45f
""" The Vertica integration will trace queries made using the vertica-python library. Vertica will be automatically instrumented with ``patch_all``, or when using the ``ls-trace-run`` command. Vertica is instrumented on import. To instrument Vertica manually use the ``patch`` function. Note the ordering of the following statements:: from ddtrace import patch patch(vertica=True) import vertica_python # use vertica_python like usual To configure the Vertica integration globally you can use the ``Config`` API:: from ddtrace import config, patch patch(vertica=True) config.vertica['service_name'] = 'my-vertica-database' To configure the Vertica integration on an instance-per-instance basis use the ``Pin`` API:: from ddtrace import Pin, patch, Tracer patch(vertica=True) import vertica_python custom_tracer = Tracer() conn = vertica_python.connect(**YOUR_VERTICA_CONFIG) # override the service and tracer to be used Pin.override(conn, service='myverticaservice', tracer=custom_tracer) """ from ...utils.importlib import require_modules required_modules = ['vertica_python'] with require_modules(required_modules) as missing_modules: if not missing_modules: from .patch import patch, unpatch __all__ = [patch, unpatch]
[]
kokosing/hue
desktop/core/ext-py/python-openid-2.2.5/openid/test/test_htmldiscover.py
2307f5379a35aae9be871e836432e6f45138b3d9
from openid.consumer.discover import OpenIDServiceEndpoint import datadriven class BadLinksTestCase(datadriven.DataDrivenTestCase): cases = [ '', "http://not.in.a.link.tag/", '<link rel="openid.server" href="not.in.html.or.head" />', ] def __init__(self, data): datadriven.DataDrivenTestCase.__init__(self, data) self.data = data def runOneTest(self): actual = OpenIDServiceEndpoint.fromHTML('http://unused.url/', self.data) expected = [] self.failUnlessEqual(expected, actual) def pyUnitTests(): return datadriven.loadTests(__name__)
[((596, 626), 'datadriven.loadTests', 'datadriven.loadTests', (['__name__'], {}), '(__name__)\n', (616, 626), False, 'import datadriven\n'), ((312, 362), 'datadriven.DataDrivenTestCase.__init__', 'datadriven.DataDrivenTestCase.__init__', (['self', 'data'], {}), '(self, data)\n', (350, 362), False, 'import datadriven\n'), ((432, 495), 'openid.consumer.discover.OpenIDServiceEndpoint.fromHTML', 'OpenIDServiceEndpoint.fromHTML', (['"""http://unused.url/"""', 'self.data'], {}), "('http://unused.url/', self.data)\n", (462, 495), False, 'from openid.consumer.discover import OpenIDServiceEndpoint\n')]
chiehtu/kissaten
project/settings/production.py
a7aad01de569107d5fd5ed2cd781bca6e5750871
from .base import * SECRET_KEY = get_env_var('SECRET_KEY') CSRF_COOKIE_SECURE = True SESSION_COOKIE_SECURE = True TEMPLATE_LOADERS = ( ('django.template.loaders.cached.Loader', ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )), ) EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_HOST_USER = get_env_var('EMAIL_HOST_USER') EMAIL_HOST_PASSWORD = get_env_var('EMAIL_HOST_PASSWORD') EMAIL_PORT = 587 EMAIL_USE_TLS = True DEFAULT_FROM_EMAIL = '' USERENA_USE_HTTPS = True
[]
ModelFlow/modelflow
modelflow/graph_viz_from_outputs.py
c2b720b2da8bb17462baff5c00bbe942644474b0
import pandas as pd import argparse import json try: from graphviz import Digraph except: print("Note: Optional graphviz not installed") def generate_graph(df, graph_format='pdf'): g = Digraph('ModelFlow', filename='modelflow.gv', engine='neato', format=graph_format) g.attr(overlap='false') g.attr(splines='true') column_names = df.columns states = [] g.attr('node', shape='ellipse') for column_name in column_names: if column_name[:6] == 'state_': states.append((column_name[6:], column_name)) g.node(column_name[6:]) models = [] g.attr('node', shape='box') for column_name in column_names: if column_name[:6] != 'state_': models.append((column_name.split('_')[0], column_name)) g.node(column_name.split('_')[0]) for column_name in column_names: if column_name[:6] != 'state_': parts = column_name.split('_') state = '_'.join(parts[1:])[6:-7] print(parts[0], state, df[column_name].min(), df[column_name].max()) if df[column_name].min() < 0 and df[column_name].max() <= 0: g.edge(state, parts[0]) elif df[column_name].min() >= 0 and df[column_name].max() > 0: g.edge(parts[0], state) else: g.edge(parts[0], state) g.edge(state, parts[0]) if graph_format == 'json': # TODO: THIS DOES NOT WORK FOR MULTIPLE MODELFLOWS with open('modelflow.gv.json', 'r') as f: return json.load(f) else: g.view() def generate_react_flow_chart(outputs): df = pd.DataFrame() for key, value in outputs['output_states'].items(): df[key] = value['data'] return generate_react_flow_chart_from_df(df) def generate_react_flow_chart_from_df(df): column_names = df.columns nodes = {} # Elipses for column_name in column_names: if column_name[:6] == 'state_': nodes[column_name[6:]] = dict(name=column_name[6:], kind='elipse') # Boxes for column_name in column_names: if column_name[:6] != 'state_': nodes[column_name.split('_')[0]] = dict(name=column_name.split('_')[0], kind='box') edges = [] for column_name in column_names: if column_name[:6] != 'state_': parts = column_name.split('_') name1 = parts[0] state = '_'.join(parts[1:])[6:-7] # print(name1, state, df[column_name].min(), # df[column_name].max()) if df[column_name].min() < 0 and df[column_name].max() <= 0: edges.append([state, name1, 'one_way']) elif df[column_name].min() >= 0 and df[column_name].max() > 0: edges.append([name1, state, 'one_way']) else: edges.append([name1, state, 'both']) return dict(nodes=list(nodes.values()), edges=edges) def main(args): df = pd.read_csv(args.output_file) # generate_graph(df) generate_react_flow_chart_from_df(df) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Generate Graph Viz') parser.add_argument('-f', '--output_file', type=str, help='The output file to generate a graph of', required=True) args = parser.parse_args() main(args)
[((199, 286), 'graphviz.Digraph', 'Digraph', (['"""ModelFlow"""'], {'filename': '"""modelflow.gv"""', 'engine': '"""neato"""', 'format': 'graph_format'}), "('ModelFlow', filename='modelflow.gv', engine='neato', format=\n graph_format)\n", (206, 286), False, 'from graphviz import Digraph\n'), ((1681, 1695), 'pandas.DataFrame', 'pd.DataFrame', ([], {}), '()\n', (1693, 1695), True, 'import pandas as pd\n'), ((3012, 3041), 'pandas.read_csv', 'pd.read_csv', (['args.output_file'], {}), '(args.output_file)\n', (3023, 3041), True, 'import pandas as pd\n'), ((3152, 3209), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""Generate Graph Viz"""'}), "(description='Generate Graph Viz')\n", (3175, 3209), False, 'import argparse\n'), ((1582, 1594), 'json.load', 'json.load', (['f'], {}), '(f)\n', (1591, 1594), False, 'import json\n')]
link1345/Vol-GameClanTools-DiscordBot
src/command/voice_log/chart.py
c3349f38d59cba59161b8c54c172e39ba873c53d
import discord import os import json import datetime import pandas as pd from dateutil.relativedelta import relativedelta from pprint import pprint import base.ColorPrint as CPrint import command.voice_log.Config_Main as CSetting def most_old_Month() : old_month = 1 labels = [] fileNameList = [] while True : filetime = datetime.datetime.today() - relativedelta(months=old_month) m_month = datetime.datetime.strftime(filetime,'%m') m_year = datetime.datetime.strftime(filetime,'%Y') filename = CSetting.baseLogFolder + CSetting.JSONPATH_row + m_year + m_month + ".json" if not os.path.exists( filename ) : old_month -= 1 # 調査用に+1してあるので、実際の値は、これにold_monthに-1したものとなる。 break labels.append( m_year + "/" + m_month ) fileNameList.append( filename ) old_month += 1 return old_month , labels , fileNameList async def makeOldTimeList( client: discord.Client, MonthFileList:list[str] , IndexLabel:list[str], RoleList: list[int] = CSetting.OneMonthOutput_RoleID ): all_df = None for fileName in MonthFileList : df = await makeTimeList( client, Datafile_path=fileName , RoleList=RoleList) #print( "test1" ) pprint( df ) if df is None : break labelname = IndexLabel[MonthFileList.index(fileName)] df = df.rename(columns={'time': labelname }) if MonthFileList.index(fileName) == 0 : all_df = df else : df = df.drop(columns=['name']) all_df = pd.merge(all_df, df , left_index=True, right_index=True) #all_df = pd.merge(all_df, df , left_index=True) #df.loc[:,[labelname]] #pprint(all_df) return all_df async def UserRoleMember( client: discord.Client, RoleList: list[int] ) : """ [VC] 指定ロールに参加しているメンバーを抽出する Args: client (discord.Client): クライアント RoleList (list[int]): 役職ID return: list[discord.Member]: 指定ロールに参加しているメンバー """ data = [] for guild_item in client.guilds : # ギルドデータ更新 await guild_item.chunk() # ロール制限がなければ、全員分を取ってくる if len(RoleList) == 0 : data += guild_item.members continue # ロール制限がなければ、該当ロール部を取ってくる for role_item in guild_item.roles : if role_item.id in RoleList : data += role_item.members return data async def makeTimeList( client: discord.Client, Datafile_path: str , RoleList: list[int]): """ [VC] 生のログデータを計算して、表にして返す。 Args: client (discord.Client): クライアント RoleList (list[int]): 役職ID mode (string): ユーザーを示すものは、何か?(UserName or ID) return: pd.DataFrame: 計算済みデータ """ # ユーザーリスト取得 members = await UserRoleMember(client, RoleList) # IDだけ抽出 def getID(members: list[discord.Member]): IDlist = [] Namelist = [] for member in members : IDlist.append( member.id ) Namelist.append( member.name + "#" + member.discriminator ) return IDlist , Namelist members_IDlist , members_Namelist = getID(members=members) if members_IDlist is None or members_IDlist == [] : return None # JSON取得 orig_TimeData : dict try : with open( Datafile_path ) as f: orig_TimeData = json.load(f) except : CPrint.error_print("JSONではありません") import traceback traceback.print_exc() return None if orig_TimeData is None : return None #df = pd.DataFrame({ # 'start': [None, None], # 'end': [None, None], # 'time': [13, 23]}, # index=['ONE', 'TWO'] #) df_dict = { 'name': members_Namelist, 'start': [None] * len(members), 'exit': [None] * len(members), 'time': [0.0] * len(members), } # 計算 for item in orig_TimeData : try : indexNum = members_IDlist.index(item["member.id"]) except ValueError as error : # 現在の鯖に、存在しない人は処理しない。 continue if item["Flag"] == "entry" : df_dict["start"][indexNum] = item["time"] if item["Flag"] == "exit" : # スタートがないのに、エンドがある場合 if df_dict["start"][indexNum] is None : # とりあえず、月初めに入室した扱いにする(他の方法も検討中。そもそも入室してない扱いetc..) tmp_startTime = datetime.now().strftime("%Y/%m/01 00:00:00") df_dict["start"][indexNum] = tmp_startTime # -- df_dict["exit"][indexNum] = item["time"] # 差分計算 a_time = datetime.datetime.strptime( df_dict["start"][indexNum] , '%Y/%m/%d %H:%M:%S') b_time = datetime.datetime.strptime( df_dict["exit"][indexNum] , '%Y/%m/%d %H:%M:%S') time : float = (b_time - a_time).total_seconds() #print( "time : " + str(time) ) if time < 0.0 : df_dict["time"][indexNum] += 0.0 else : df_dict["time"][indexNum] += time # DataFrameに変更 df = pd.DataFrame(df_dict, index=members_IDlist ) # 作業用の"start"と"end"を削除 df = df.drop(columns=['start','exit']) # 計算 df["time"] = df["time"] / 60 / 60 #pprint(df) return df
[((4326, 4369), 'pandas.DataFrame', 'pd.DataFrame', (['df_dict'], {'index': 'members_IDlist'}), '(df_dict, index=members_IDlist)\n', (4338, 4369), True, 'import pandas as pd\n'), ((404, 446), 'datetime.datetime.strftime', 'datetime.datetime.strftime', (['filetime', '"""%m"""'], {}), "(filetime, '%m')\n", (430, 446), False, 'import datetime\n'), ((457, 499), 'datetime.datetime.strftime', 'datetime.datetime.strftime', (['filetime', '"""%Y"""'], {}), "(filetime, '%Y')\n", (483, 499), False, 'import datetime\n'), ((1146, 1156), 'pprint.pprint', 'pprint', (['df'], {}), '(df)\n', (1152, 1156), False, 'from pprint import pprint\n'), ((332, 357), 'datetime.datetime.today', 'datetime.datetime.today', ([], {}), '()\n', (355, 357), False, 'import datetime\n'), ((360, 391), 'dateutil.relativedelta.relativedelta', 'relativedelta', ([], {'months': 'old_month'}), '(months=old_month)\n', (373, 391), False, 'from dateutil.relativedelta import relativedelta\n'), ((597, 621), 'os.path.exists', 'os.path.exists', (['filename'], {}), '(filename)\n', (611, 621), False, 'import os\n'), ((1403, 1458), 'pandas.merge', 'pd.merge', (['all_df', 'df'], {'left_index': '(True)', 'right_index': '(True)'}), '(all_df, df, left_index=True, right_index=True)\n', (1411, 1458), True, 'import pandas as pd\n'), ((2933, 2945), 'json.load', 'json.load', (['f'], {}), '(f)\n', (2942, 2945), False, 'import json\n'), ((2958, 2991), 'base.ColorPrint.error_print', 'CPrint.error_print', (['"""JSONではありません"""'], {}), "('JSONではありません')\n", (2976, 2991), True, 'import base.ColorPrint as CPrint\n'), ((3013, 3034), 'traceback.print_exc', 'traceback.print_exc', ([], {}), '()\n', (3032, 3034), False, 'import traceback\n'), ((3943, 4018), 'datetime.datetime.strptime', 'datetime.datetime.strptime', (["df_dict['start'][indexNum]", '"""%Y/%m/%d %H:%M:%S"""'], {}), "(df_dict['start'][indexNum], '%Y/%m/%d %H:%M:%S')\n", (3969, 4018), False, 'import datetime\n'), ((4033, 4107), 'datetime.datetime.strptime', 'datetime.datetime.strptime', (["df_dict['exit'][indexNum]", '"""%Y/%m/%d %H:%M:%S"""'], {}), "(df_dict['exit'][indexNum], '%Y/%m/%d %H:%M:%S')\n", (4059, 4107), False, 'import datetime\n'), ((3775, 3789), 'datetime.now', 'datetime.now', ([], {}), '()\n', (3787, 3789), False, 'import datetime\n')]
e1r0nd/codewars
5kyu/(5 kyu) Count IP Addresses/(5 kyu) Count IP Addresses.py
9b05e32a26ee5f36a4b3f1e76a71e0c79b3c865b
def ips_between(start, end): calc = lambda n, m: (int(end.split(".")[n]) - int(start.split(".")[n])) * m return calc(0, 256 * 256 * 256) + calc(1, 256 * 256) + calc(2, 256) + calc(3, 1)
[]
jayholman/vmaf
python/src/vmaf/core/feature_extractor.py
0bba4faf68ab89e38314cc596e6908b4fb83984d
from abc import ABCMeta, abstractmethod import os from vmaf.tools.misc import make_absolute_path, run_process from vmaf.tools.stats import ListStats __copyright__ = "Copyright 2016-2018, Netflix, Inc." __license__ = "Apache, Version 2.0" import re import numpy as np import ast from vmaf import ExternalProgramCaller, to_list from vmaf.config import VmafConfig, VmafExternalConfig from vmaf.core.executor import Executor from vmaf.core.result import Result from vmaf.tools.reader import YuvReader class FeatureExtractor(Executor): """ FeatureExtractor takes in a list of assets, and run feature extraction on them, and return a list of corresponding results. A FeatureExtractor must specify a unique type and version combination (by the TYPE and VERSION attribute), so that the Result generated by it can be identified. A derived class of FeatureExtractor must: 1) Override TYPE and VERSION 2) Override _generate_result(self, asset), which call a command-line executable and generate feature scores in a log file. 3) Override _get_feature_scores(self, asset), which read the feature scores from the log file, and return the scores in a dictionary format. For an example, follow VmafFeatureExtractor. """ __metaclass__ = ABCMeta @property @abstractmethod def ATOM_FEATURES(self): raise NotImplementedError def _read_result(self, asset): result = {} result.update(self._get_feature_scores(asset)) executor_id = self.executor_id return Result(asset, executor_id, result) @classmethod def get_scores_key(cls, atom_feature): return "{type}_{atom_feature}_scores".format( type=cls.TYPE, atom_feature=atom_feature) @classmethod def get_score_key(cls, atom_feature): return "{type}_{atom_feature}_score".format( type=cls.TYPE, atom_feature=atom_feature) def _get_feature_scores(self, asset): # routine to read the feature scores from the log file, and return # the scores in a dictionary format. log_file_path = self._get_log_file_path(asset) atom_feature_scores_dict = {} atom_feature_idx_dict = {} for atom_feature in self.ATOM_FEATURES: atom_feature_scores_dict[atom_feature] = [] atom_feature_idx_dict[atom_feature] = 0 with open(log_file_path, 'rt') as log_file: for line in log_file.readlines(): for atom_feature in self.ATOM_FEATURES: re_template = "{af}: ([0-9]+) ([a-zA-Z0-9.-]+)".format(af=atom_feature) mo = re.match(re_template, line) if mo: cur_idx = int(mo.group(1)) assert cur_idx == atom_feature_idx_dict[atom_feature] # parse value, allowing NaN and inf val = float(mo.group(2)) if np.isnan(val) or np.isinf(val): val = None atom_feature_scores_dict[atom_feature].append(val) atom_feature_idx_dict[atom_feature] += 1 continue len_score = len(atom_feature_scores_dict[self.ATOM_FEATURES[0]]) assert len_score != 0 for atom_feature in self.ATOM_FEATURES[1:]: assert len_score == len(atom_feature_scores_dict[atom_feature]), \ "Feature data possibly corrupt. Run cleanup script and try again." feature_result = {} for atom_feature in self.ATOM_FEATURES: scores_key = self.get_scores_key(atom_feature) feature_result[scores_key] = atom_feature_scores_dict[atom_feature] return feature_result class VmafFeatureExtractor(FeatureExtractor): TYPE = "VMAF_feature" # VERSION = '0.1' # vmaf_study; Anush's VIF fix # VERSION = '0.2' # expose vif_num, vif_den, adm_num, adm_den, anpsnr # VERSION = '0.2.1' # expose vif num/den of each scale # VERSION = '0.2.2' # adm abs-->fabs, corrected border handling, uniform reading with option of offset for input YUV, updated VIF corner case # VERSION = '0.2.2b' # expose adm_den/num_scalex # VERSION = '0.2.3' # AVX for VMAF convolution; update adm features by folding noise floor into per coef # VERSION = '0.2.4' # Fix a bug in adm feature passing scale into dwt_quant_step # VERSION = '0.2.4b' # Modify by adding ADM noise floor outside cube root; add derived feature motion2 VERSION = '0.2.4c' # Modify by moving motion2 to c code ATOM_FEATURES = ['vif', 'adm', 'ansnr', 'motion', 'motion2', 'vif_num', 'vif_den', 'adm_num', 'adm_den', 'anpsnr', 'vif_num_scale0', 'vif_den_scale0', 'vif_num_scale1', 'vif_den_scale1', 'vif_num_scale2', 'vif_den_scale2', 'vif_num_scale3', 'vif_den_scale3', 'adm_num_scale0', 'adm_den_scale0', 'adm_num_scale1', 'adm_den_scale1', 'adm_num_scale2', 'adm_den_scale2', 'adm_num_scale3', 'adm_den_scale3', ] DERIVED_ATOM_FEATURES = ['vif_scale0', 'vif_scale1', 'vif_scale2', 'vif_scale3', 'vif2', 'adm2', 'adm3', 'adm_scale0', 'adm_scale1', 'adm_scale2', 'adm_scale3', ] ADM2_CONSTANT = 0 ADM_SCALE_CONSTANT = 0 def _generate_result(self, asset): # routine to call the command-line executable and generate feature # scores in the log file. quality_width, quality_height = asset.quality_width_height log_file_path = self._get_log_file_path(asset) yuv_type=self._get_workfile_yuv_type(asset) ref_path=asset.ref_workfile_path dis_path=asset.dis_workfile_path w=quality_width h=quality_height logger = self.logger ExternalProgramCaller.call_vmaf_feature(yuv_type, ref_path, dis_path, w, h, log_file_path, logger) @classmethod def _post_process_result(cls, result): # override Executor._post_process_result result = super(VmafFeatureExtractor, cls)._post_process_result(result) # adm2 = # (adm_num + ADM2_CONSTANT) / (adm_den + ADM2_CONSTANT) adm2_scores_key = cls.get_scores_key('adm2') adm_num_scores_key = cls.get_scores_key('adm_num') adm_den_scores_key = cls.get_scores_key('adm_den') result.result_dict[adm2_scores_key] = list( (np.array(result.result_dict[adm_num_scores_key]) + cls.ADM2_CONSTANT) / (np.array(result.result_dict[adm_den_scores_key]) + cls.ADM2_CONSTANT) ) # vif_scalei = vif_num_scalei / vif_den_scalei, i = 0, 1, 2, 3 vif_num_scale0_scores_key = cls.get_scores_key('vif_num_scale0') vif_den_scale0_scores_key = cls.get_scores_key('vif_den_scale0') vif_num_scale1_scores_key = cls.get_scores_key('vif_num_scale1') vif_den_scale1_scores_key = cls.get_scores_key('vif_den_scale1') vif_num_scale2_scores_key = cls.get_scores_key('vif_num_scale2') vif_den_scale2_scores_key = cls.get_scores_key('vif_den_scale2') vif_num_scale3_scores_key = cls.get_scores_key('vif_num_scale3') vif_den_scale3_scores_key = cls.get_scores_key('vif_den_scale3') vif_scale0_scores_key = cls.get_scores_key('vif_scale0') vif_scale1_scores_key = cls.get_scores_key('vif_scale1') vif_scale2_scores_key = cls.get_scores_key('vif_scale2') vif_scale3_scores_key = cls.get_scores_key('vif_scale3') result.result_dict[vif_scale0_scores_key] = list( (np.array(result.result_dict[vif_num_scale0_scores_key]) / np.array(result.result_dict[vif_den_scale0_scores_key])) ) result.result_dict[vif_scale1_scores_key] = list( (np.array(result.result_dict[vif_num_scale1_scores_key]) / np.array(result.result_dict[vif_den_scale1_scores_key])) ) result.result_dict[vif_scale2_scores_key] = list( (np.array(result.result_dict[vif_num_scale2_scores_key]) / np.array(result.result_dict[vif_den_scale2_scores_key])) ) result.result_dict[vif_scale3_scores_key] = list( (np.array(result.result_dict[vif_num_scale3_scores_key]) / np.array(result.result_dict[vif_den_scale3_scores_key])) ) # vif2 = # ((vif_num_scale0 / vif_den_scale0) + (vif_num_scale1 / vif_den_scale1) + # (vif_num_scale2 / vif_den_scale2) + (vif_num_scale3 / vif_den_scale3)) / 4.0 vif_scores_key = cls.get_scores_key('vif2') result.result_dict[vif_scores_key] = list( ( (np.array(result.result_dict[vif_num_scale0_scores_key]) / np.array(result.result_dict[vif_den_scale0_scores_key])) + (np.array(result.result_dict[vif_num_scale1_scores_key]) / np.array(result.result_dict[vif_den_scale1_scores_key])) + (np.array(result.result_dict[vif_num_scale2_scores_key]) / np.array(result.result_dict[vif_den_scale2_scores_key])) + (np.array(result.result_dict[vif_num_scale3_scores_key]) / np.array(result.result_dict[vif_den_scale3_scores_key])) ) / 4.0 ) # adm_scalei = adm_num_scalei / adm_den_scalei, i = 0, 1, 2, 3 adm_num_scale0_scores_key = cls.get_scores_key('adm_num_scale0') adm_den_scale0_scores_key = cls.get_scores_key('adm_den_scale0') adm_num_scale1_scores_key = cls.get_scores_key('adm_num_scale1') adm_den_scale1_scores_key = cls.get_scores_key('adm_den_scale1') adm_num_scale2_scores_key = cls.get_scores_key('adm_num_scale2') adm_den_scale2_scores_key = cls.get_scores_key('adm_den_scale2') adm_num_scale3_scores_key = cls.get_scores_key('adm_num_scale3') adm_den_scale3_scores_key = cls.get_scores_key('adm_den_scale3') adm_scale0_scores_key = cls.get_scores_key('adm_scale0') adm_scale1_scores_key = cls.get_scores_key('adm_scale1') adm_scale2_scores_key = cls.get_scores_key('adm_scale2') adm_scale3_scores_key = cls.get_scores_key('adm_scale3') result.result_dict[adm_scale0_scores_key] = list( (np.array(result.result_dict[adm_num_scale0_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale0_scores_key]) + cls.ADM_SCALE_CONSTANT) ) result.result_dict[adm_scale1_scores_key] = list( (np.array(result.result_dict[adm_num_scale1_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale1_scores_key]) + cls.ADM_SCALE_CONSTANT) ) result.result_dict[adm_scale2_scores_key] = list( (np.array(result.result_dict[adm_num_scale2_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale2_scores_key]) + cls.ADM_SCALE_CONSTANT) ) result.result_dict[adm_scale3_scores_key] = list( (np.array(result.result_dict[adm_num_scale3_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale3_scores_key]) + cls.ADM_SCALE_CONSTANT) ) # adm3 = \ # (((adm_num_scale0 + ADM_SCALE_CONSTANT) / (adm_den_scale0 + ADM_SCALE_CONSTANT)) # + ((adm_num_scale1 + ADM_SCALE_CONSTANT) / (adm_den_scale1 + ADM_SCALE_CONSTANT)) # + ((adm_num_scale2 + ADM_SCALE_CONSTANT) / (adm_den_scale2 + ADM_SCALE_CONSTANT)) # + ((adm_num_scale3 + ADM_SCALE_CONSTANT) / (adm_den_scale3 + ADM_SCALE_CONSTANT))) / 4.0 adm3_scores_key = cls.get_scores_key('adm3') result.result_dict[adm3_scores_key] = list( ( ((np.array(result.result_dict[adm_num_scale0_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale0_scores_key]) + cls.ADM_SCALE_CONSTANT)) + ((np.array(result.result_dict[adm_num_scale1_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale1_scores_key]) + cls.ADM_SCALE_CONSTANT)) + ((np.array(result.result_dict[adm_num_scale2_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale2_scores_key]) + cls.ADM_SCALE_CONSTANT)) + ((np.array(result.result_dict[adm_num_scale3_scores_key]) + cls.ADM_SCALE_CONSTANT) / (np.array(result.result_dict[adm_den_scale3_scores_key]) + cls.ADM_SCALE_CONSTANT)) ) / 4.0 ) # validate for feature in cls.DERIVED_ATOM_FEATURES: assert cls.get_scores_key(feature) in result.result_dict return result class VifFrameDifferenceFeatureExtractor(FeatureExtractor): TYPE = "VifDiff_feature" VERSION = '0.1' ATOM_FEATURES = ['vifdiff', 'vifdiff_num', 'vifdiff_den', 'vifdiff_num_scale0', 'vifdiff_den_scale0', 'vifdiff_num_scale1', 'vifdiff_den_scale1', 'vifdiff_num_scale2', 'vifdiff_den_scale2', 'vifdiff_num_scale3', 'vifdiff_den_scale3', ] DERIVED_ATOM_FEATURES = ['vifdiff_scale0', 'vifdiff_scale1', 'vifdiff_scale2', 'vifdiff_scale3', ] ADM2_CONSTANT = 0 ADM_SCALE_CONSTANT = 0 def _generate_result(self, asset): # routine to call the command-line executable and generate feature # scores in the log file. quality_width, quality_height = asset.quality_width_height log_file_path = self._get_log_file_path(asset) yuv_type=self._get_workfile_yuv_type(asset) ref_path=asset.ref_workfile_path dis_path=asset.dis_workfile_path w=quality_width h=quality_height logger = self.logger ExternalProgramCaller.call_vifdiff_feature(yuv_type, ref_path, dis_path, w, h, log_file_path, logger) @classmethod def _post_process_result(cls, result): # override Executor._post_process_result result = super(VifFrameDifferenceFeatureExtractor, cls)._post_process_result(result) # vifdiff_scalei = vifdiff_num_scalei / vifdiff_den_scalei, i = 0, 1, 2, 3 vifdiff_num_scale0_scores_key = cls.get_scores_key('vifdiff_num_scale0') vifdiff_den_scale0_scores_key = cls.get_scores_key('vifdiff_den_scale0') vifdiff_num_scale1_scores_key = cls.get_scores_key('vifdiff_num_scale1') vifdiff_den_scale1_scores_key = cls.get_scores_key('vifdiff_den_scale1') vifdiff_num_scale2_scores_key = cls.get_scores_key('vifdiff_num_scale2') vifdiff_den_scale2_scores_key = cls.get_scores_key('vifdiff_den_scale2') vifdiff_num_scale3_scores_key = cls.get_scores_key('vifdiff_num_scale3') vifdiff_den_scale3_scores_key = cls.get_scores_key('vifdiff_den_scale3') vifdiff_scale0_scores_key = cls.get_scores_key('vifdiff_scale0') vifdiff_scale1_scores_key = cls.get_scores_key('vifdiff_scale1') vifdiff_scale2_scores_key = cls.get_scores_key('vifdiff_scale2') vifdiff_scale3_scores_key = cls.get_scores_key('vifdiff_scale3') result.result_dict[vifdiff_scale0_scores_key] = list( (np.array(result.result_dict[vifdiff_num_scale0_scores_key]) / np.array(result.result_dict[vifdiff_den_scale0_scores_key])) ) result.result_dict[vifdiff_scale1_scores_key] = list( (np.array(result.result_dict[vifdiff_num_scale1_scores_key]) / np.array(result.result_dict[vifdiff_den_scale1_scores_key])) ) result.result_dict[vifdiff_scale2_scores_key] = list( (np.array(result.result_dict[vifdiff_num_scale2_scores_key]) / np.array(result.result_dict[vifdiff_den_scale2_scores_key])) ) result.result_dict[vifdiff_scale3_scores_key] = list( (np.array(result.result_dict[vifdiff_num_scale3_scores_key]) / np.array(result.result_dict[vifdiff_den_scale3_scores_key])) ) # validate for feature in cls.DERIVED_ATOM_FEATURES: assert cls.get_scores_key(feature) in result.result_dict return result class PsnrFeatureExtractor(FeatureExtractor): TYPE = "PSNR_feature" VERSION = "1.0" ATOM_FEATURES = ['psnr'] def _generate_result(self, asset): # routine to call the command-line executable and generate quality # scores in the log file. quality_width, quality_height = asset.quality_width_height log_file_path = self._get_log_file_path(asset) yuv_type=self._get_workfile_yuv_type(asset) ref_path=asset.ref_workfile_path dis_path=asset.dis_workfile_path w=quality_width h=quality_height logger = self.logger ExternalProgramCaller.call_psnr(yuv_type, ref_path, dis_path, w, h, log_file_path, logger) class MomentFeatureExtractor(FeatureExtractor): TYPE = "Moment_feature" # VERSION = "1.0" # call executable VERSION = "1.1" # python only ATOM_FEATURES = ['ref1st', 'ref2nd', 'dis1st', 'dis2nd', ] DERIVED_ATOM_FEATURES = ['refvar', 'disvar', ] def _generate_result(self, asset): # routine to call the command-line executable and generate feature # scores in the log file. quality_w, quality_h = asset.quality_width_height ref_scores_mtx = None with YuvReader(filepath=asset.ref_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type(asset)) as ref_yuv_reader: scores_mtx_list = [] i = 0 for ref_yuv in ref_yuv_reader: ref_y = ref_yuv[0] firstm = ref_y.mean() secondm = ref_y.var() + firstm**2 scores_mtx_list.append(np.hstack(([firstm], [secondm]))) i += 1 ref_scores_mtx = np.vstack(scores_mtx_list) dis_scores_mtx = None with YuvReader(filepath=asset.dis_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type(asset)) as dis_yuv_reader: scores_mtx_list = [] i = 0 for dis_yuv in dis_yuv_reader: dis_y = dis_yuv[0] firstm = dis_y.mean() secondm = dis_y.var() + firstm**2 scores_mtx_list.append(np.hstack(([firstm], [secondm]))) i += 1 dis_scores_mtx = np.vstack(scores_mtx_list) assert ref_scores_mtx is not None and dis_scores_mtx is not None log_dict = {'ref_scores_mtx': ref_scores_mtx.tolist(), 'dis_scores_mtx': dis_scores_mtx.tolist()} log_file_path = self._get_log_file_path(asset) with open(log_file_path, 'wt') as log_file: log_file.write(str(log_dict)) def _get_feature_scores(self, asset): # routine to read the feature scores from the log file, and return # the scores in a dictionary format. log_file_path = self._get_log_file_path(asset) with open(log_file_path, 'rt') as log_file: log_str = log_file.read() log_dict = ast.literal_eval(log_str) ref_scores_mtx = np.array(log_dict['ref_scores_mtx']) dis_scores_mtx = np.array(log_dict['dis_scores_mtx']) _, num_ref_features = ref_scores_mtx.shape assert num_ref_features == 2 # ref1st, ref2nd _, num_dis_features = dis_scores_mtx.shape assert num_dis_features == 2 # dis1st, dis2nd feature_result = {} feature_result[self.get_scores_key('ref1st')] = list(ref_scores_mtx[:, 0]) feature_result[self.get_scores_key('ref2nd')] = list(ref_scores_mtx[:, 1]) feature_result[self.get_scores_key('dis1st')] = list(dis_scores_mtx[:, 0]) feature_result[self.get_scores_key('dis2nd')] = list(dis_scores_mtx[:, 1]) return feature_result @classmethod def _post_process_result(cls, result): # override Executor._post_process_result result = super(MomentFeatureExtractor, cls)._post_process_result(result) # calculate refvar and disvar from ref1st, ref2nd, dis1st, dis2nd refvar_scores_key = cls.get_scores_key('refvar') ref1st_scores_key = cls.get_scores_key('ref1st') ref2nd_scores_key = cls.get_scores_key('ref2nd') disvar_scores_key = cls.get_scores_key('disvar') dis1st_scores_key = cls.get_scores_key('dis1st') dis2nd_scores_key = cls.get_scores_key('dis2nd') get_var = lambda m: m[1] - m[0] * m[0] result.result_dict[refvar_scores_key] = \ to_list(map(get_var, zip(result.result_dict[ref1st_scores_key], result.result_dict[ref2nd_scores_key]))) result.result_dict[disvar_scores_key] = \ to_list(map(get_var, zip(result.result_dict[dis1st_scores_key], result.result_dict[dis2nd_scores_key]))) # validate for feature in cls.DERIVED_ATOM_FEATURES: assert cls.get_scores_key(feature) in result.result_dict return result class SsimFeatureExtractor(FeatureExtractor): TYPE = "SSIM_feature" # VERSION = "1.0" VERSION = "1.1" # fix OPT_RANGE_PIXEL_OFFSET = 0 ATOM_FEATURES = ['ssim', 'ssim_l', 'ssim_c', 'ssim_s'] def _generate_result(self, asset): # routine to call the command-line executable and generate quality # scores in the log file. quality_width, quality_height = asset.quality_width_height log_file_path = self._get_log_file_path(asset) yuv_type=self._get_workfile_yuv_type(asset) ref_path=asset.ref_workfile_path dis_path=asset.dis_workfile_path w=quality_width h=quality_height logger = self.logger ExternalProgramCaller.call_ssim(yuv_type, ref_path, dis_path, w, h, log_file_path, logger) class MsSsimFeatureExtractor(FeatureExtractor): TYPE = "MS_SSIM_feature" # VERSION = "1.0" VERSION = "1.1" # fix OPT_RANGE_PIXEL_OFFSET = 0 ATOM_FEATURES = ['ms_ssim', 'ms_ssim_l_scale0', 'ms_ssim_c_scale0', 'ms_ssim_s_scale0', 'ms_ssim_l_scale1', 'ms_ssim_c_scale1', 'ms_ssim_s_scale1', 'ms_ssim_l_scale2', 'ms_ssim_c_scale2', 'ms_ssim_s_scale2', 'ms_ssim_l_scale3', 'ms_ssim_c_scale3', 'ms_ssim_s_scale3', 'ms_ssim_l_scale4', 'ms_ssim_c_scale4', 'ms_ssim_s_scale4', ] def _generate_result(self, asset): # routine to call the command-line executable and generate quality # scores in the log file. quality_width, quality_height = asset.quality_width_height log_file_path = self._get_log_file_path(asset) yuv_type=self._get_workfile_yuv_type(asset) ref_path=asset.ref_workfile_path dis_path=asset.dis_workfile_path w=quality_width h=quality_height logger = self.logger ExternalProgramCaller.call_ms_ssim(yuv_type, ref_path, dis_path, w, h, log_file_path, logger)
[((1573, 1607), 'vmaf.core.result.Result', 'Result', (['asset', 'executor_id', 'result'], {}), '(asset, executor_id, result)\n', (1579, 1607), False, 'from vmaf.core.result import Result\n'), ((6044, 6146), 'vmaf.ExternalProgramCaller.call_vmaf_feature', 'ExternalProgramCaller.call_vmaf_feature', (['yuv_type', 'ref_path', 'dis_path', 'w', 'h', 'log_file_path', 'logger'], {}), '(yuv_type, ref_path, dis_path, w, h,\n log_file_path, logger)\n', (6083, 6146), False, 'from vmaf import ExternalProgramCaller, to_list\n'), ((14143, 14248), 'vmaf.ExternalProgramCaller.call_vifdiff_feature', 'ExternalProgramCaller.call_vifdiff_feature', (['yuv_type', 'ref_path', 'dis_path', 'w', 'h', 'log_file_path', 'logger'], {}), '(yuv_type, ref_path, dis_path, w,\n h, log_file_path, logger)\n', (14185, 14248), False, 'from vmaf import ExternalProgramCaller, to_list\n'), ((17137, 17231), 'vmaf.ExternalProgramCaller.call_psnr', 'ExternalProgramCaller.call_psnr', (['yuv_type', 'ref_path', 'dis_path', 'w', 'h', 'log_file_path', 'logger'], {}), '(yuv_type, ref_path, dis_path, w, h,\n log_file_path, logger)\n', (17168, 17231), False, 'from vmaf import ExternalProgramCaller, to_list\n'), ((19599, 19635), 'numpy.array', 'np.array', (["log_dict['ref_scores_mtx']"], {}), "(log_dict['ref_scores_mtx'])\n", (19607, 19635), True, 'import numpy as np\n'), ((19661, 19697), 'numpy.array', 'np.array', (["log_dict['dis_scores_mtx']"], {}), "(log_dict['dis_scores_mtx'])\n", (19669, 19697), True, 'import numpy as np\n'), ((22214, 22308), 'vmaf.ExternalProgramCaller.call_ssim', 'ExternalProgramCaller.call_ssim', (['yuv_type', 'ref_path', 'dis_path', 'w', 'h', 'log_file_path', 'logger'], {}), '(yuv_type, ref_path, dis_path, w, h,\n log_file_path, logger)\n', (22245, 22308), False, 'from vmaf import ExternalProgramCaller, to_list\n'), ((23415, 23512), 'vmaf.ExternalProgramCaller.call_ms_ssim', 'ExternalProgramCaller.call_ms_ssim', (['yuv_type', 'ref_path', 'dis_path', 'w', 'h', 'log_file_path', 'logger'], {}), '(yuv_type, ref_path, dis_path, w, h,\n log_file_path, logger)\n', (23449, 23512), False, 'from vmaf import ExternalProgramCaller, to_list\n'), ((18258, 18284), 'numpy.vstack', 'np.vstack', (['scores_mtx_list'], {}), '(scores_mtx_list)\n', (18267, 18284), True, 'import numpy as np\n'), ((18837, 18863), 'numpy.vstack', 'np.vstack', (['scores_mtx_list'], {}), '(scores_mtx_list)\n', (18846, 18863), True, 'import numpy as np\n'), ((19548, 19573), 'ast.literal_eval', 'ast.literal_eval', (['log_str'], {}), '(log_str)\n', (19564, 19573), False, 'import ast\n'), ((7803, 7858), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale0_scores_key]'], {}), '(result.result_dict[vif_num_scale0_scores_key])\n', (7811, 7858), True, 'import numpy as np\n'), ((7874, 7929), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale0_scores_key]'], {}), '(result.result_dict[vif_den_scale0_scores_key])\n', (7882, 7929), True, 'import numpy as np\n'), ((8012, 8067), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale1_scores_key]'], {}), '(result.result_dict[vif_num_scale1_scores_key])\n', (8020, 8067), True, 'import numpy as np\n'), ((8083, 8138), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale1_scores_key]'], {}), '(result.result_dict[vif_den_scale1_scores_key])\n', (8091, 8138), True, 'import numpy as np\n'), ((8221, 8276), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale2_scores_key]'], {}), '(result.result_dict[vif_num_scale2_scores_key])\n', (8229, 8276), True, 'import numpy as np\n'), ((8292, 8347), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale2_scores_key]'], {}), '(result.result_dict[vif_den_scale2_scores_key])\n', (8300, 8347), True, 'import numpy as np\n'), ((8430, 8485), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale3_scores_key]'], {}), '(result.result_dict[vif_num_scale3_scores_key])\n', (8438, 8485), True, 'import numpy as np\n'), ((8501, 8556), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale3_scores_key]'], {}), '(result.result_dict[vif_den_scale3_scores_key])\n', (8509, 8556), True, 'import numpy as np\n'), ((15548, 15607), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_num_scale0_scores_key]'], {}), '(result.result_dict[vifdiff_num_scale0_scores_key])\n', (15556, 15607), True, 'import numpy as np\n'), ((15623, 15682), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_den_scale0_scores_key]'], {}), '(result.result_dict[vifdiff_den_scale0_scores_key])\n', (15631, 15682), True, 'import numpy as np\n'), ((15769, 15828), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_num_scale1_scores_key]'], {}), '(result.result_dict[vifdiff_num_scale1_scores_key])\n', (15777, 15828), True, 'import numpy as np\n'), ((15844, 15903), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_den_scale1_scores_key]'], {}), '(result.result_dict[vifdiff_den_scale1_scores_key])\n', (15852, 15903), True, 'import numpy as np\n'), ((15990, 16049), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_num_scale2_scores_key]'], {}), '(result.result_dict[vifdiff_num_scale2_scores_key])\n', (15998, 16049), True, 'import numpy as np\n'), ((16065, 16124), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_den_scale2_scores_key]'], {}), '(result.result_dict[vifdiff_den_scale2_scores_key])\n', (16073, 16124), True, 'import numpy as np\n'), ((16211, 16270), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_num_scale3_scores_key]'], {}), '(result.result_dict[vifdiff_num_scale3_scores_key])\n', (16219, 16270), True, 'import numpy as np\n'), ((16286, 16345), 'numpy.array', 'np.array', (['result.result_dict[vifdiff_den_scale3_scores_key]'], {}), '(result.result_dict[vifdiff_den_scale3_scores_key])\n', (16294, 16345), True, 'import numpy as np\n'), ((2665, 2692), 're.match', 're.match', (['re_template', 'line'], {}), '(re_template, line)\n', (2673, 2692), False, 'import re\n'), ((6651, 6699), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scores_key]'], {}), '(result.result_dict[adm_num_scores_key])\n', (6659, 6699), True, 'import numpy as np\n'), ((6736, 6784), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scores_key]'], {}), '(result.result_dict[adm_den_scores_key])\n', (6744, 6784), True, 'import numpy as np\n'), ((10492, 10547), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale0_scores_key]'], {}), '(result.result_dict[adm_num_scale0_scores_key])\n', (10500, 10547), True, 'import numpy as np\n'), ((10589, 10644), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale0_scores_key]'], {}), '(result.result_dict[adm_den_scale0_scores_key])\n', (10597, 10644), True, 'import numpy as np\n'), ((10752, 10807), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale1_scores_key]'], {}), '(result.result_dict[adm_num_scale1_scores_key])\n', (10760, 10807), True, 'import numpy as np\n'), ((10849, 10904), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale1_scores_key]'], {}), '(result.result_dict[adm_den_scale1_scores_key])\n', (10857, 10904), True, 'import numpy as np\n'), ((11012, 11067), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale2_scores_key]'], {}), '(result.result_dict[adm_num_scale2_scores_key])\n', (11020, 11067), True, 'import numpy as np\n'), ((11109, 11164), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale2_scores_key]'], {}), '(result.result_dict[adm_den_scale2_scores_key])\n', (11117, 11164), True, 'import numpy as np\n'), ((11272, 11327), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale3_scores_key]'], {}), '(result.result_dict[adm_num_scale3_scores_key])\n', (11280, 11327), True, 'import numpy as np\n'), ((11369, 11424), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale3_scores_key]'], {}), '(result.result_dict[adm_den_scale3_scores_key])\n', (11377, 11424), True, 'import numpy as np\n'), ((18172, 18204), 'numpy.hstack', 'np.hstack', (['([firstm], [secondm])'], {}), '(([firstm], [secondm]))\n', (18181, 18204), True, 'import numpy as np\n'), ((18751, 18783), 'numpy.hstack', 'np.hstack', (['([firstm], [secondm])'], {}), '(([firstm], [secondm]))\n', (18760, 18783), True, 'import numpy as np\n'), ((9343, 9398), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale3_scores_key]'], {}), '(result.result_dict[vif_num_scale3_scores_key])\n', (9351, 9398), True, 'import numpy as np\n'), ((9418, 9473), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale3_scores_key]'], {}), '(result.result_dict[vif_den_scale3_scores_key])\n', (9426, 9473), True, 'import numpy as np\n'), ((2987, 3000), 'numpy.isnan', 'np.isnan', (['val'], {}), '(val)\n', (2995, 3000), True, 'import numpy as np\n'), ((3004, 3017), 'numpy.isinf', 'np.isinf', (['val'], {}), '(val)\n', (3012, 3017), True, 'import numpy as np\n'), ((9192, 9247), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale2_scores_key]'], {}), '(result.result_dict[vif_num_scale2_scores_key])\n', (9200, 9247), True, 'import numpy as np\n'), ((9267, 9322), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale2_scores_key]'], {}), '(result.result_dict[vif_den_scale2_scores_key])\n', (9275, 9322), True, 'import numpy as np\n'), ((12610, 12665), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale3_scores_key]'], {}), '(result.result_dict[adm_num_scale3_scores_key])\n', (12618, 12665), True, 'import numpy as np\n'), ((12712, 12767), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale3_scores_key]'], {}), '(result.result_dict[adm_den_scale3_scores_key])\n', (12720, 12767), True, 'import numpy as np\n'), ((8890, 8945), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale0_scores_key]'], {}), '(result.result_dict[vif_num_scale0_scores_key])\n', (8898, 8945), True, 'import numpy as np\n'), ((8965, 9020), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale0_scores_key]'], {}), '(result.result_dict[vif_den_scale0_scores_key])\n', (8973, 9020), True, 'import numpy as np\n'), ((9041, 9096), 'numpy.array', 'np.array', (['result.result_dict[vif_num_scale1_scores_key]'], {}), '(result.result_dict[vif_num_scale1_scores_key])\n', (9049, 9096), True, 'import numpy as np\n'), ((9116, 9171), 'numpy.array', 'np.array', (['result.result_dict[vif_den_scale1_scores_key]'], {}), '(result.result_dict[vif_den_scale1_scores_key])\n', (9124, 9171), True, 'import numpy as np\n'), ((12405, 12460), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale2_scores_key]'], {}), '(result.result_dict[adm_num_scale2_scores_key])\n', (12413, 12460), True, 'import numpy as np\n'), ((12507, 12562), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale2_scores_key]'], {}), '(result.result_dict[adm_den_scale2_scores_key])\n', (12515, 12562), True, 'import numpy as np\n'), ((11995, 12050), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale0_scores_key]'], {}), '(result.result_dict[adm_num_scale0_scores_key])\n', (12003, 12050), True, 'import numpy as np\n'), ((12097, 12152), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale0_scores_key]'], {}), '(result.result_dict[adm_den_scale0_scores_key])\n', (12105, 12152), True, 'import numpy as np\n'), ((12200, 12255), 'numpy.array', 'np.array', (['result.result_dict[adm_num_scale1_scores_key]'], {}), '(result.result_dict[adm_num_scale1_scores_key])\n', (12208, 12255), True, 'import numpy as np\n'), ((12302, 12357), 'numpy.array', 'np.array', (['result.result_dict[adm_den_scale1_scores_key]'], {}), '(result.result_dict[adm_den_scale1_scores_key])\n', (12310, 12357), True, 'import numpy as np\n')]
rprops/Python_DS-WS
notebooks/_solutions/pandas_02_basic_operations28.py
b2fc449a74be0c82863e5fcf1ddbe7d64976d530
df['Age'].hist() #bins=30, log=True
[]
oopsteams/pansite
controller/base_service.py
11896842da66efc72c26eab071f7f802b982f435
# -*- coding: utf-8 -*- """ Created by susy at 2019/11/8 """ from dao.dao import DataDao import pytz from dao.models import PanAccounts from cfg import PAN_SERVICE, MASTER_ACCOUNT_ID class BaseService: def __init__(self): self.default_tz = pytz.timezone('Asia/Chongqing') # self.pan_acc: PanAccounts = DataDao.pan_account_list(MASTER_ACCOUNT_ID, False)
[((255, 286), 'pytz.timezone', 'pytz.timezone', (['"""Asia/Chongqing"""'], {}), "('Asia/Chongqing')\n", (268, 286), False, 'import pytz\n')]
StateOfTheArt-quant/transformerquant
transformerquant/modules/attention/multi_head.py
f6775d7aa920b84908b0a09d9ba098b1fe87bdff
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import torch.nn as nn from .single import attention class MultiHeadedAttention(nn.Module): def __init__(self, d_model, nhead, dropout=0.1): super().__init__() assert d_model % nhead ==0 # we assume d_v always equal d_k self.d_k = d_model // nhead self.nhead = nhead self.linear_layers = nn.ModuleList([nn.Linear(d_model, d_model) for _ in range(3)]) self.output_linear = nn.Linear(d_model, d_model) self.dropout = nn.Dropout(p=dropout) def forward(self, query, key, value, mask=None): if mask is not None: mask = mask.unsqueeze(1) batch_size = query.size(0) # 1) Do all the linear projections in batch from d_model => h x d_k query, key, value = [l(x).view(batch_size, -1, self.nhead, self.d_k).transpose(1, 2) for l, x in zip(self.linear_layers, (query, key, value))] # 2) Apply attention on all the projected vectors in batch. x, attn = attention(query, key, value, mask=mask, dropout=self.dropout) # 3) "Concat" using a view and apply a final linear. x = x.transpose(1, 2).contiguous().view(batch_size, -1, self.nhead * self.d_k) context = self.output_linear(x) return context#, attn
[((504, 531), 'torch.nn.Linear', 'nn.Linear', (['d_model', 'd_model'], {}), '(d_model, d_model)\n', (513, 531), True, 'import torch.nn as nn\n'), ((555, 576), 'torch.nn.Dropout', 'nn.Dropout', ([], {'p': 'dropout'}), '(p=dropout)\n', (565, 576), True, 'import torch.nn as nn\n'), ((427, 454), 'torch.nn.Linear', 'nn.Linear', (['d_model', 'd_model'], {}), '(d_model, d_model)\n', (436, 454), True, 'import torch.nn as nn\n')]
Julian-Theis/AVATAR
avatar/generalization.py
24fcd6eaa26f413be528a160d865d5d7e49a780b
import os, time, argparse from datetime import datetime from pm4py.objects.log.importer.csv import factory as csv_importer from pm4py.objects.log.exporter.xes import factory as xes_exporter from pm4py.objects.log.importer.xes import factory as xes_importer from pm4py.objects.petri.importer import pnml as pnml_importer from pm4py.evaluation.replay_fitness import factory as replay_factory from pm4py.evaluation.precision import factory as precision_factory from conf.settings import DATA_PATH WORK_PATH = os.path.abspath(os.getcwd()) def readFile(f_name1, f_name2, unique=False): traces = [] skipped = 0 with open(f_name1) as file: file_contents = file.read() file_contents = file_contents.split("\n") print("Number of train traces are:", str(len(file_contents))) for row in file_contents: if unique: if row not in traces: traces.append(row) else: skipped += 1 else: traces.append(row) with open(f_name2) as file: file_contents = file.read() file_contents = file_contents.split("\n") print("Number of generated traces are:", str(len(file_contents))) for row in file_contents: if unique: if row not in traces: traces.append(row) else: skipped += 1 else: traces.append(row) f_traces = [] for trace in traces: f_trace = [] t = trace.split(" ") for i in t: if i != "" and "<" not in i: f_trace.append(i) if len(f_trace) > 0: f_traces.append(f_trace) print("Number of traces are:", str(len(f_traces))) print("Number of skipped traces are:", str(skipped)) return f_traces def writeToFile(file, lst): with open(file, 'w') as outfile: for entry in lst: outfile.write(str(entry) + "\n") def convertToCsv(traces, to_path): lines = [] case = 0 timestamp = 0 line = "concept:name,case:concept:name,time:timestamp" lines.append(line) for trace in traces: for event in trace: timestamp = timestamp + 1 dt_object = datetime.fromtimestamp(timestamp) line = str(event) + "_" + "," + str(case) + "," + str(dt_object) lines.append(line) case = case + 1 writeToFile(str(to_path), lines) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-s', '--system', help='Which system (e.g. pb_system_5_3)', required=True) parser.add_argument('-sfx', '--suffix', help='Suffix (chosen epoch, e.g. 1981)', required=True) parser.add_argument('-j', '--job', help='Job (0/1)', required=True) parser.add_argument('-pn', '--pn', help='Petri net file to evaluate', required=True) parser.add_argument('-strategy', '--strategy', help='naive/mh', required=True) args = parser.parse_args() system = args.system suffix = int(args.suffix) job = args.job pn = args.pn strategy = args.strategy if DATA_PATH is None: train_file = os.path.join(WORK_PATH, "data", "variants", system + "_train.txt") gen_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + ".txt") csv_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.csv") xes_file = os.path.join(WORK_PATH, "data", "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.xes") pn_file = os.path.join(WORK_PATH, "data", "pns", system, pn) else: train_file = os.path.join(DATA_PATH, "variants", system + "_train.txt") gen_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + ".txt") csv_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.csv") xes_file = os.path.join(DATA_PATH, "avatar", "variants", system + "_relgan_" + str(suffix) + "_j" + str(job) + "_" + strategy + "_generalization.xes") pn_file = os.path.join(DATA_PATH, "pns", system, pn) """ READ FILES AND CONVERT TO XES """ traces = readFile(train_file,gen_file, unique=True) convertToCsv(traces=traces, to_path=csv_file) time.sleep(1) log = csv_importer.import_event_log(csv_file) xes_exporter.export_log(log, xes_file) time.sleep(1) """ PERFORM MEASUREMENT ON PN AND XES""" log = xes_importer.import_log(xes_file) net, initial_marking, final_marking = pnml_importer.import_net(pn_file) fitness = replay_factory.apply(log, net, initial_marking, final_marking) print("Fitness=", fitness) precision = precision_factory.apply(log, net, initial_marking, final_marking) print("Precision=", precision) fitness = fitness["log_fitness"] generalization = 2 * ((fitness * precision) / (fitness + precision)) if strategy == "mh": print("**** ", str(system), " Job ", str(job), " on PN ", str(pn_file), " using MH SAMPLING on suffix ", str(suffix)," ***") elif strategy == "naive": print("**** ", str(system), " Job ", str(job), " on PN ", str(pn_file), " using NAIVE SAMPLING on suffix ", str(suffix), " ***") else: raise ValueError("Unknown strategy.") print("AVATAR Generalization=", generalization)
[((526, 537), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (535, 537), False, 'import os, time, argparse\n'), ((2537, 2562), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (2560, 2562), False, 'import os, time, argparse\n'), ((4595, 4608), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (4605, 4608), False, 'import os, time, argparse\n'), ((4620, 4659), 'pm4py.objects.log.importer.csv.factory.import_event_log', 'csv_importer.import_event_log', (['csv_file'], {}), '(csv_file)\n', (4649, 4659), True, 'from pm4py.objects.log.importer.csv import factory as csv_importer\n'), ((4664, 4702), 'pm4py.objects.log.exporter.xes.factory.export_log', 'xes_exporter.export_log', (['log', 'xes_file'], {}), '(log, xes_file)\n', (4687, 4702), True, 'from pm4py.objects.log.exporter.xes import factory as xes_exporter\n'), ((4707, 4720), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (4717, 4720), False, 'import os, time, argparse\n'), ((4777, 4810), 'pm4py.objects.log.importer.xes.factory.import_log', 'xes_importer.import_log', (['xes_file'], {}), '(xes_file)\n', (4800, 4810), True, 'from pm4py.objects.log.importer.xes import factory as xes_importer\n'), ((4853, 4886), 'pm4py.objects.petri.importer.pnml.import_net', 'pnml_importer.import_net', (['pn_file'], {}), '(pn_file)\n', (4877, 4886), True, 'from pm4py.objects.petri.importer import pnml as pnml_importer\n'), ((4902, 4964), 'pm4py.evaluation.replay_fitness.factory.apply', 'replay_factory.apply', (['log', 'net', 'initial_marking', 'final_marking'], {}), '(log, net, initial_marking, final_marking)\n', (4922, 4964), True, 'from pm4py.evaluation.replay_fitness import factory as replay_factory\n'), ((5013, 5078), 'pm4py.evaluation.precision.factory.apply', 'precision_factory.apply', (['log', 'net', 'initial_marking', 'final_marking'], {}), '(log, net, initial_marking, final_marking)\n', (5036, 5078), True, 'from pm4py.evaluation.precision import factory as precision_factory\n'), ((3206, 3272), 'os.path.join', 'os.path.join', (['WORK_PATH', '"""data"""', '"""variants"""', "(system + '_train.txt')"], {}), "(WORK_PATH, 'data', 'variants', system + '_train.txt')\n", (3218, 3272), False, 'import os, time, argparse\n'), ((3777, 3827), 'os.path.join', 'os.path.join', (['WORK_PATH', '"""data"""', '"""pns"""', 'system', 'pn'], {}), "(WORK_PATH, 'data', 'pns', system, pn)\n", (3789, 3827), False, 'import os, time, argparse\n'), ((3859, 3917), 'os.path.join', 'os.path.join', (['DATA_PATH', '"""variants"""', "(system + '_train.txt')"], {}), "(DATA_PATH, 'variants', system + '_train.txt')\n", (3871, 3917), False, 'import os, time, argparse\n'), ((4398, 4440), 'os.path.join', 'os.path.join', (['DATA_PATH', '"""pns"""', 'system', 'pn'], {}), "(DATA_PATH, 'pns', system, pn)\n", (4410, 4440), False, 'import os, time, argparse\n'), ((2290, 2323), 'datetime.datetime.fromtimestamp', 'datetime.fromtimestamp', (['timestamp'], {}), '(timestamp)\n', (2312, 2323), False, 'from datetime import datetime\n')]
uqtimes/Rust-SampleCodes
Introductions/The Rust Programming Language/embed/bindings/embed.py
f9d7a040d8198acd30bf3423e7c6cf52bc9c7b6e
# $ python embed.py from ctypes import cdll lib = cdll.LoadLibrary("../target/release/libembed.dylib") #=> for Mac #lib = cdll.LoadLibrary("../target/release/libembed.so") #=> for Linux lib.process() print("done!")
[((52, 104), 'ctypes.cdll.LoadLibrary', 'cdll.LoadLibrary', (['"""../target/release/libembed.dylib"""'], {}), "('../target/release/libembed.dylib')\n", (68, 104), False, 'from ctypes import cdll\n')]
handsome-baby/huaweicloud-sdk-python-v3
huaweicloud-sdk-image/huaweicloudsdkimage/v1/image_client.py
6cdcf1da8b098427e58fc3335a387c14df7776d0
# coding: utf-8 from __future__ import absolute_import import datetime import re import importlib import six from huaweicloudsdkcore.client import Client, ClientBuilder from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkcore.utils import http_utils from huaweicloudsdkcore.sdk_stream_request import SdkStreamRequest class ImageClient(Client): """ :param configuration: .Configuration object for this client :param pool_threads: The number of threads to use for async requests to the API. More threads means more concurrent API requests. """ PRIMITIVE_TYPES = (float, bool, bytes, six.text_type) + six.integer_types NATIVE_TYPES_MAPPING = { 'int': int, 'long': int if six.PY3 else long, 'float': float, 'str': str, 'bool': bool, 'date': datetime.date, 'datetime': datetime.datetime, 'object': object, } def __init__(self): super(ImageClient, self).__init__() self.model_package = importlib.import_module("huaweicloudsdkimage.v1.model") self.preset_headers = {'User-Agent': 'HuaweiCloud-SDK-Python'} @classmethod def new_builder(cls, clazz=None): if clazz is None: return ClientBuilder(cls) if clazz.__name__ != "ImageClient": raise TypeError("client type error, support client type is ImageClient") return ClientBuilder(clazz) def run_celebrity_recognition(self, request): """名人识别 分析并识别图片中包含的政治人物、明星及网红人物,返回人物信息及人脸坐标。 :param RunCelebrityRecognitionRequest request :return: RunCelebrityRecognitionResponse """ return self.run_celebrity_recognition_with_http_info(request) def run_celebrity_recognition_with_http_info(self, request): """名人识别 分析并识别图片中包含的政治人物、明星及网红人物,返回人物信息及人脸坐标。 :param RunCelebrityRecognitionRequest request :return: RunCelebrityRecognitionResponse """ all_params = ['body'] local_var_params = {} for attr in request.attribute_map: if hasattr(request, attr): local_var_params[attr] = getattr(request, attr) collection_formats = {} path_params = {} query_params = [] header_params = {} form_params = {} body_params = None if 'body' in local_var_params: body_params = local_var_params['body'] if isinstance(request, SdkStreamRequest): body_params = request.get_file_stream() response_headers = [] header_params['Content-Type'] = http_utils.select_header_content_type( ['application/json;charset=UTF-8']) auth_settings = [] return self.call_api( resource_path='/v1.0/image/celebrity-recognition', method='POST', path_params=path_params, query_params=query_params, header_params=header_params, body=body_params, post_params=form_params, response_type='RunCelebrityRecognitionResponse', response_headers=response_headers, auth_settings=auth_settings, collection_formats=collection_formats, request_type=request.__class__.__name__) def run_image_tagging(self, request): """图像标签 自然图像的语义内容非常丰富,一个图像包含多个标签内容,图像标签服务准确识别自然图片中数百种场景、上千种通用物体及其属性,让智能相册管理、照片检索和分类、基于场景内容或者物体的广告推荐等功能更加直观。使用时用户发送待处理图片,返回图片标签内容及相应置信度。 :param RunImageTaggingRequest request :return: RunImageTaggingResponse """ return self.run_image_tagging_with_http_info(request) def run_image_tagging_with_http_info(self, request): """图像标签 自然图像的语义内容非常丰富,一个图像包含多个标签内容,图像标签服务准确识别自然图片中数百种场景、上千种通用物体及其属性,让智能相册管理、照片检索和分类、基于场景内容或者物体的广告推荐等功能更加直观。使用时用户发送待处理图片,返回图片标签内容及相应置信度。 :param RunImageTaggingRequest request :return: RunImageTaggingResponse """ all_params = ['body'] local_var_params = {} for attr in request.attribute_map: if hasattr(request, attr): local_var_params[attr] = getattr(request, attr) collection_formats = {} path_params = {} query_params = [] header_params = {} form_params = {} body_params = None if 'body' in local_var_params: body_params = local_var_params['body'] if isinstance(request, SdkStreamRequest): body_params = request.get_file_stream() response_headers = [] header_params['Content-Type'] = http_utils.select_header_content_type( ['application/json;charset=UTF-8']) auth_settings = [] return self.call_api( resource_path='/v1.0/image/tagging', method='POST', path_params=path_params, query_params=query_params, header_params=header_params, body=body_params, post_params=form_params, response_type='RunImageTaggingResponse', response_headers=response_headers, auth_settings=auth_settings, collection_formats=collection_formats, request_type=request.__class__.__name__) def call_api(self, resource_path, method, path_params=None, query_params=None, header_params=None, body=None, post_params=None, response_type=None, response_headers=None, auth_settings=None, collection_formats=None, request_type=None): """Makes the HTTP request and returns deserialized data. :param resource_path: Path to method endpoint. :param method: Method to call. :param path_params: Path parameters in the url. :param query_params: Query parameters in the url. :param header_params: Header parameters to be placed in the request header. :param body: Request body. :param post_params dict: Request post form parameters, for `application/x-www-form-urlencoded`, `multipart/form-data`. :param auth_settings list: Auth Settings names for the request. :param response_type: Response data type. :param response_headers: Header should be added to response data. :param collection_formats: dict of collection formats for path, query, header, and post parameters. :param request_type: Request data type. :return: Return the response directly. """ return self.do_http_request( method=method, resource_path=resource_path, path_params=path_params, query_params=query_params, header_params=header_params, body=body, post_params=post_params, response_type=response_type, response_headers=response_headers, collection_formats=collection_formats, request_type=request_type)
[((1028, 1083), 'importlib.import_module', 'importlib.import_module', (['"""huaweicloudsdkimage.v1.model"""'], {}), "('huaweicloudsdkimage.v1.model')\n", (1051, 1083), False, 'import importlib\n'), ((1421, 1441), 'huaweicloudsdkcore.client.ClientBuilder', 'ClientBuilder', (['clazz'], {}), '(clazz)\n', (1434, 1441), False, 'from huaweicloudsdkcore.client import Client, ClientBuilder\n'), ((2624, 2697), 'huaweicloudsdkcore.utils.http_utils.select_header_content_type', 'http_utils.select_header_content_type', (["['application/json;charset=UTF-8']"], {}), "(['application/json;charset=UTF-8'])\n", (2661, 2697), False, 'from huaweicloudsdkcore.utils import http_utils\n'), ((4606, 4679), 'huaweicloudsdkcore.utils.http_utils.select_header_content_type', 'http_utils.select_header_content_type', (["['application/json;charset=UTF-8']"], {}), "(['application/json;charset=UTF-8'])\n", (4643, 4679), False, 'from huaweicloudsdkcore.utils import http_utils\n'), ((1256, 1274), 'huaweicloudsdkcore.client.ClientBuilder', 'ClientBuilder', (['cls'], {}), '(cls)\n', (1269, 1274), False, 'from huaweicloudsdkcore.client import Client, ClientBuilder\n')]
ebezzam/PolyatomicFW_SPL
frank_wolfe.py
7fbbead5a642915c4bb4d061006b7dac8f6af788
import numpy as np from typing import Optional, Any from pandas import DataFrame from copy import deepcopy from abc import abstractmethod from utils import TimedGenericIterativeAlgorithm import pycsou.core as pcore import pycsou.linop as pl from pycsou.func.penalty import L1Norm from pycsou.func.loss import SquaredL2Loss from pycsou.opt.proxalgs import APGD class GenericFWSolverForLasso(TimedGenericIterativeAlgorithm): def __init__(self, data: np.ndarray, forwardOp: pcore.linop.LinearOperator, lambda_: Optional[float] = None, lambda_factor: Optional[float] = 0.1, min_iter: int = 10, max_iter: int = 500, stopping_strategy: str = 'certificate', accuracy_threshold: float = 1e-4, verbose: Optional[int] = 10, remove_positions: bool = False, remember_iterand: bool = False, decreasing: bool = False, multi_spikes_threshold: float = .7, multi_spikes: bool = True, reweighting: str = 'ista', t_max: float = None): self.data = data self.forwardOp = forwardOp self.stopping_strategy = stopping_strategy self.accuracy_threshold = accuracy_threshold self.multi_spikes = multi_spikes self.multi_spikes_threshold = multi_spikes_threshold self.reweighting = reweighting self.remove_positions = remove_positions self.decreasing = decreasing self.dim = self.forwardOp.shape[1] self.x0 = np.zeros(self.dim) self.dual_certificate_value = 1 / lambda_factor self.new_ind = None self.epsilon = None self.remember_iterand = remember_iterand self.iterand_history = [] init_iterand = {'iterand': self.x0, 'positions': np.array([], dtype=int)} l22_loss = (1 / 2) * SquaredL2Loss(dim=self.forwardOp.shape[0], data=self.data) self.data_fidelity = l22_loss * self.forwardOp if lambda_ is None: lambda_ = lambda_factor * np.abs(self.forwardOp.adjoint(self.data)).max() self.lambda_ = lambda_ self.penalty = self.lambda_ * L1Norm(dim=self.dim) objective_functional = self.data_fidelity + self.penalty self.bound = np.linalg.norm(self.data) ** 2 / (2 * self.lambda_) self.start = None if verbose is not None: self.candidate_new = [] self.actual_new = [] super(GenericFWSolverForLasso, self).__init__(objective_functional=objective_functional, init_iterand=init_iterand, max_iter=max_iter, min_iter=min_iter, accuracy_threshold=accuracy_threshold, verbose=verbose, t_max=t_max) def update_iterand(self) -> Any: self.compute_new_impulse() res = self.combine_new_impulse() return res def compute_new_impulse(self): dual_certificate = - self.data_fidelity.gradient(self.old_iterand['iterand']) / self.lambda_ d = np.abs(dual_certificate) if self.multi_spikes: maxi = np.max(d) if self.iter == 0: threshold = self.multi_spikes_threshold * maxi self.epsilon = (1 - self.multi_spikes_threshold) * maxi else: threshold = maxi - (1 / (self.iter + 2)) * self.epsilon indices = np.where(d > max(threshold, 1.))[0] # print("Threshold: {} / {}".format(threshold, maxi)) # print('Candidate indices: {}\n'.format(indices.shape)) self.new_ind = np.setdiff1d(indices, self.old_iterand['positions'], assume_unique=True) if self.verbose is not None: self.candidate_new.append(indices.shape[0]) self.actual_new.append(self.new_ind.size) if len(self.new_ind) == 0: self.new_ind = None self.dual_certificate_value = max(dual_certificate.min(), dual_certificate.max(), key=abs) else: self.new_ind = np.argmax(d) self.dual_certificate_value = dual_certificate[self.new_ind] if self.new_ind in self.old_iterand['positions']: self.new_ind = None # already present position if abs(self.dual_certificate_value) < 1.: if self.verbose is not None: print('Warning, dual certificate lower than 1 at iteration {}'.format(self.iter)) @abstractmethod def combine_new_impulse(self) -> Any: pass def update_diagnostics(self): """ Dual ceritificate value is computed after iteration Returns ------- """ if self.iter == 0: self.diagnostics = DataFrame( columns=['Iter', 'Relative Improvement Objective', 'Relative Improvement Iterand', 'Dual Certificate Value', 'Objective Function']) self.diagnostics.loc[self.iter, 'Iter'] = self.iter if np.linalg.norm(self.old_iterand['iterand']) == 0: self.diagnostics.loc[self.iter, 'Relative Improvement Iterand'] = np.infty else: self.diagnostics.loc[self.iter, 'Relative Improvement Iterand'] = np.linalg.norm( self.old_iterand['iterand'] - self.iterand['iterand']) / np.linalg.norm( self.old_iterand['iterand']) self.diagnostics.loc[self.iter, 'Dual Certificate Value'] = self.dual_certificate_value # before iteration self.diagnostics.loc[self.iter, 'Objective Function'] = self.objective_functional(self.iterand['iterand']) if self.iter == 0: self.diagnostics.loc[self.iter, 'Relative Improvement Objective'] = np.infty else: self.diagnostics.loc[self.iter, 'Relative Improvement Objective'] = (self.diagnostics.loc[ self.iter - 1, 'Objective Function'] - self.diagnostics.loc[ self.iter, 'Objective Function']) / \ self.diagnostics.loc[ self.iter - 1, 'Objective Function'] if self.remember_iterand: self.iterand_history.append(self.iterand['iterand']) def print_diagnostics(self): print(dict(self.diagnostics.loc[self.iter])) def stopping_metric(self): if self.iter == 0: return np.infty elif self.stopping_strategy == 'relative_improvement': return abs(self.diagnostics.loc[self.iter - 1, 'Relative Improvement Objective']) elif self.stopping_strategy == 'certificate': value = self.diagnostics.loc[self.iter - 1, 'Dual Certificate Value'] return abs(abs(value) - 1) else: raise ValueError('Stopping strategy must be in ["relative_improvement", "certificate"]') def restricted_support_lasso(self, active_indices: np.ndarray, accuracy: float, x0: np.ndarray = None, d: float = 75.): if x0 is None: x0 = np.zeros(active_indices.shape) injection = pl.sampling.SubSampling(self.dim, active_indices, dtype=float).get_adjointOp() restricted_forward = pl.DenseLinearOperator( self.forwardOp.mat[:, active_indices]) restricted_forward.compute_lipschitz_cst(tol=1e-3) restricted_data_fidelity = (1 / 2) * SquaredL2Loss(dim=restricted_forward.shape[0], data=self.data) \ * restricted_forward # restricted_data_fidelity.lipschitz_cst = self.data_fidelity.lipschitz_cst # restricted_data_fidelity.diff_lipschitz_cst = self.data_fidelity.diff_lipschitz_cst restricted_regularization = self.lambda_ * L1Norm(dim=restricted_data_fidelity.shape[1]) if self.reweighting == 'fista': acceleration = 'CD' tau = None elif self.reweighting == 'ista': tau = 1.9 / restricted_data_fidelity.diff_lipschitz_cst acceleration = None else: raise ValueError('Reweighting strategy must be in ["fista", "ista"]') solver = APGD(dim=restricted_data_fidelity.shape[1], F=restricted_data_fidelity, G=restricted_regularization, x0=x0, tau=tau, acceleration=acceleration, verbose=None, accuracy_threshold=accuracy, d=d, max_iter=2000, min_iter=1) return injection(solver.iterate()[0]['iterand']) class VanillaFWSolverForLasso(GenericFWSolverForLasso): def __init__(self, data: np.ndarray, forwardOp: pcore.linop.LinearOperator, lambda_: Optional[float] = None, lambda_factor: Optional[float] = 0.1, min_iter: int = 10, max_iter: int = 500, stopping_strategy: str = 'certificate', accuracy_threshold: float = 1e-4, verbose: Optional[int] = 10, remember_iterand: bool = False, step_size: str = 'optimal', t_max: float = None): if step_size in ['optimal', 'regular']: self.step_size = step_size else: raise ValueError("Step size strategy must be in ['optimal', 'regular']") super(VanillaFWSolverForLasso, self).__init__(data, forwardOp, lambda_=lambda_, lambda_factor=lambda_factor, min_iter=min_iter, max_iter=max_iter, stopping_strategy=stopping_strategy, accuracy_threshold=accuracy_threshold, verbose=verbose, remember_iterand=remember_iterand, multi_spikes=False, t_max=t_max) def combine_new_impulse(self) -> Any: iterand = deepcopy(self.old_iterand['iterand']) if self.new_ind is not None: new_positions = np.hstack([self.old_iterand['positions'], self.new_ind]) if self.step_size == 'optimal': gamma = np.dot(self.data_fidelity.gradient(iterand), iterand) + self.lambda_ * ( 1. * np.linalg.norm(iterand, 1) + (np.abs(self.dual_certificate_value) - 1.) * self.bound) gamma /= np.linalg.norm(self.forwardOp.mat[:, self.new_ind] * self.bound * np.sign( self.dual_certificate_value) - self.forwardOp @ iterand, 2) ** 2 else: gamma = 2/(self.iter + 3) else: new_positions = self.old_iterand['positions'] if self.step_size == 'optimal': gamma = np.dot(self.data_fidelity.gradient(iterand), iterand) + self.lambda_ * np.linalg.norm(iterand, 1) gamma /= np.linalg.norm(self.forwardOp @ iterand, 2) ** 2 else: gamma = 2/(self.iter + 3) if not 0 < gamma < 1: gamma = np.clip(gamma, 0., 1.) iterand *= (1 - gamma) if self.new_ind is not None: iterand[self.new_ind] += gamma * np.sign(self.dual_certificate_value) * self.bound return {'iterand': iterand, 'positions': new_positions} class FullyCorrectiveFWSolverForLasso(VanillaFWSolverForLasso): def __init__(self, data: np.ndarray, forwardOp: pcore.linop.LinearOperator, lambda_: Optional[float] = None, lambda_factor: Optional[float] = 0.1, min_iter: int = 10, max_iter: int = 500, stopping_strategy: str = 'certificate', accuracy_threshold: float = 1e-4, verbose: Optional[int] = 10, remember_iterand: bool = False, remove_positions: bool = False, reweighting_prec: float = 1e-4, reweighting: str = 'fista', t_max: float = None): self.remove_positions = remove_positions self.reweighting_prec = reweighting_prec super(FullyCorrectiveFWSolverForLasso, self).__init__(data, forwardOp, lambda_=lambda_, lambda_factor=lambda_factor, min_iter=min_iter, max_iter=max_iter, stopping_strategy=stopping_strategy, accuracy_threshold=accuracy_threshold, verbose=verbose, remember_iterand=remember_iterand, t_max=t_max) self.reweighting = reweighting self.last_weight = self.bound def combine_new_impulse(self) -> Any: iterand = deepcopy(self.old_iterand['iterand']) if self.new_ind is not None: new_positions = np.unique(np.hstack([self.old_iterand['positions'], self.new_ind])) if self.iter > 0 and self.remove_positions: active_indices = np.unique(np.hstack([iterand.nonzero()[0], self.new_ind])) else: active_indices = new_positions else: new_positions = self.old_iterand['positions'] if self.iter > 0 and self.remove_positions: active_indices = np.unique(iterand.nonzero()[0]) else: active_indices = new_positions if active_indices.shape[0] > 1: iterand[self.new_ind] = np.sign(self.dual_certificate_value) * self.last_weight x0 = iterand[active_indices] iterand = self.restricted_support_lasso(active_indices, self.reweighting_prec, x0=x0) if self.new_ind is not None: self.last_weight = iterand[self.new_ind] else: tmp = np.zeros(self.dim) tmp[active_indices] = 1. column = self.forwardOp(tmp) iterand[active_indices] = np.dot(self.data, column) / (np.linalg.norm(column, 2) ** 2) self.last_weight = iterand[active_indices] overvalue = np.abs(iterand) > self.bound if overvalue.sum() > 0: print("Overvalue at coordinates {}".format(np.arange(overvalue.shape[0])[overvalue])) iterand[overvalue] = np.sign(iterand[overvalue]) * self.bound return {'iterand': iterand, 'positions': new_positions} class PolyatomicFWSolverForLasso(GenericFWSolverForLasso): def __init__(self, data: np.ndarray, forwardOp: pcore.linop.LinearOperator, lambda_: Optional[float] = None, lambda_factor: Optional[float] = 0.1, min_iter: int = 10, max_iter: int = 500, stopping_strategy: str = 'certificate', accuracy_threshold: float = 1e-4, verbose: Optional[int] = 10, remove_positions: bool = False, remember_iterand: bool = False, final_reweighting_prec: float = 1e-4, init_reweighting_prec: float = .2, decreasing: bool = False, multi_spikes_threshold: float = .7, t_max: float = None): self.remove_positions = remove_positions self.reweighting_prec = init_reweighting_prec self.init_reweighting_prec = init_reweighting_prec self.decreasing = decreasing self.final_reweighting_prec = final_reweighting_prec super(PolyatomicFWSolverForLasso, self).__init__(data, forwardOp, lambda_=lambda_, lambda_factor=lambda_factor, min_iter=min_iter, max_iter=max_iter, stopping_strategy=stopping_strategy, accuracy_threshold=accuracy_threshold, verbose=verbose, remember_iterand=remember_iterand, multi_spikes=True, multi_spikes_threshold=multi_spikes_threshold, reweighting='ista', t_max=t_max) def combine_new_impulse(self): iterand = deepcopy(self.old_iterand['iterand']) if self.new_ind is not None: new_positions = np.unique(np.hstack([self.old_iterand['positions'], self.new_ind])) if self.iter > 0 and self.remove_positions: active_indices = np.unique(np.hstack([iterand.nonzero()[0], self.new_ind])) else: active_indices = new_positions else: new_positions = self.old_iterand['positions'] if self.iter > 0 and self.remove_positions: active_indices = np.unique(iterand.nonzero()[0]) else: active_indices = new_positions if active_indices.shape[0] > 1: x0 = iterand[active_indices] iterand = self.restricted_support_lasso(active_indices, self.reweighting_prec, x0=x0) else: tmp = np.zeros(self.dim) tmp[active_indices] = 1. column = self.forwardOp(tmp) iterand[active_indices] = np.dot(self.data, column) / (np.linalg.norm(column, 2) ** 2) overvalue = np.abs(iterand) > self.bound if overvalue.sum() > 0: #Sanity check, never been triggered in practice print("Overvalue at coordinates {}".format(np.arange(overvalue.shape[0])[overvalue])) iterand[overvalue] = np.sign(iterand[overvalue]) * self.bound if self.decreasing: self.reweighting_prec = self.init_reweighting_prec / (self.iter + 1) self.reweighting_prec = max(self.reweighting_prec, self.final_reweighting_prec) return {'iterand': iterand, 'positions': new_positions}
[((1448, 1466), 'numpy.zeros', 'np.zeros', (['self.dim'], {}), '(self.dim)\n', (1456, 1466), True, 'import numpy as np\n'), ((3088, 3112), 'numpy.abs', 'np.abs', (['dual_certificate'], {}), '(dual_certificate)\n', (3094, 3112), True, 'import numpy as np\n'), ((7848, 7911), 'pycsou.linop.DenseLinearOperator', 'pl.DenseLinearOperator', (['self.forwardOp.mat[:, (active_indices)]'], {}), '(self.forwardOp.mat[:, (active_indices)])\n', (7870, 7911), True, 'import pycsou.linop as pl\n'), ((8772, 8999), 'pycsou.opt.proxalgs.APGD', 'APGD', ([], {'dim': 'restricted_data_fidelity.shape[1]', 'F': 'restricted_data_fidelity', 'G': 'restricted_regularization', 'x0': 'x0', 'tau': 'tau', 'acceleration': 'acceleration', 'verbose': 'None', 'accuracy_threshold': 'accuracy', 'd': 'd', 'max_iter': '(2000)', 'min_iter': '(1)'}), '(dim=restricted_data_fidelity.shape[1], F=restricted_data_fidelity, G=\n restricted_regularization, x0=x0, tau=tau, acceleration=acceleration,\n verbose=None, accuracy_threshold=accuracy, d=d, max_iter=2000, min_iter=1)\n', (8776, 8999), False, 'from pycsou.opt.proxalgs import APGD\n'), ((10381, 10418), 'copy.deepcopy', 'deepcopy', (["self.old_iterand['iterand']"], {}), "(self.old_iterand['iterand'])\n", (10389, 10418), False, 'from copy import deepcopy\n'), ((13137, 13174), 'copy.deepcopy', 'deepcopy', (["self.old_iterand['iterand']"], {}), "(self.old_iterand['iterand'])\n", (13145, 13174), False, 'from copy import deepcopy\n'), ((16673, 16710), 'copy.deepcopy', 'deepcopy', (["self.old_iterand['iterand']"], {}), "(self.old_iterand['iterand'])\n", (16681, 16710), False, 'from copy import deepcopy\n'), ((1719, 1742), 'numpy.array', 'np.array', (['[]'], {'dtype': 'int'}), '([], dtype=int)\n', (1727, 1742), True, 'import numpy as np\n'), ((1774, 1832), 'pycsou.func.loss.SquaredL2Loss', 'SquaredL2Loss', ([], {'dim': 'self.forwardOp.shape[0]', 'data': 'self.data'}), '(dim=self.forwardOp.shape[0], data=self.data)\n', (1787, 1832), False, 'from pycsou.func.loss import SquaredL2Loss\n'), ((2071, 2091), 'pycsou.func.penalty.L1Norm', 'L1Norm', ([], {'dim': 'self.dim'}), '(dim=self.dim)\n', (2077, 2091), False, 'from pycsou.func.penalty import L1Norm\n'), ((3162, 3171), 'numpy.max', 'np.max', (['d'], {}), '(d)\n', (3168, 3171), True, 'import numpy as np\n'), ((3648, 3720), 'numpy.setdiff1d', 'np.setdiff1d', (['indices', "self.old_iterand['positions']"], {'assume_unique': '(True)'}), "(indices, self.old_iterand['positions'], assume_unique=True)\n", (3660, 3720), True, 'import numpy as np\n'), ((4191, 4203), 'numpy.argmax', 'np.argmax', (['d'], {}), '(d)\n', (4200, 4203), True, 'import numpy as np\n'), ((4879, 5028), 'pandas.DataFrame', 'DataFrame', ([], {'columns': "['Iter', 'Relative Improvement Objective', 'Relative Improvement Iterand',\n 'Dual Certificate Value', 'Objective Function']"}), "(columns=['Iter', 'Relative Improvement Objective',\n 'Relative Improvement Iterand', 'Dual Certificate Value',\n 'Objective Function'])\n", (4888, 5028), False, 'from pandas import DataFrame\n'), ((5134, 5177), 'numpy.linalg.norm', 'np.linalg.norm', (["self.old_iterand['iterand']"], {}), "(self.old_iterand['iterand'])\n", (5148, 5177), True, 'import numpy as np\n'), ((7689, 7719), 'numpy.zeros', 'np.zeros', (['active_indices.shape'], {}), '(active_indices.shape)\n', (7697, 7719), True, 'import numpy as np\n'), ((8377, 8422), 'pycsou.func.penalty.L1Norm', 'L1Norm', ([], {'dim': 'restricted_data_fidelity.shape[1]'}), '(dim=restricted_data_fidelity.shape[1])\n', (8383, 8422), False, 'from pycsou.func.penalty import L1Norm\n'), ((10484, 10540), 'numpy.hstack', 'np.hstack', (["[self.old_iterand['positions'], self.new_ind]"], {}), "([self.old_iterand['positions'], self.new_ind])\n", (10493, 10540), True, 'import numpy as np\n'), ((11460, 11484), 'numpy.clip', 'np.clip', (['gamma', '(0.0)', '(1.0)'], {}), '(gamma, 0.0, 1.0)\n', (11467, 11484), True, 'import numpy as np\n'), ((14181, 14199), 'numpy.zeros', 'np.zeros', (['self.dim'], {}), '(self.dim)\n', (14189, 14199), True, 'import numpy as np\n'), ((14452, 14467), 'numpy.abs', 'np.abs', (['iterand'], {}), '(iterand)\n', (14458, 14467), True, 'import numpy as np\n'), ((17528, 17546), 'numpy.zeros', 'np.zeros', (['self.dim'], {}), '(self.dim)\n', (17536, 17546), True, 'import numpy as np\n'), ((17744, 17759), 'numpy.abs', 'np.abs', (['iterand'], {}), '(iterand)\n', (17750, 17759), True, 'import numpy as np\n'), ((2178, 2203), 'numpy.linalg.norm', 'np.linalg.norm', (['self.data'], {}), '(self.data)\n', (2192, 2203), True, 'import numpy as np\n'), ((5363, 5432), 'numpy.linalg.norm', 'np.linalg.norm', (["(self.old_iterand['iterand'] - self.iterand['iterand'])"], {}), "(self.old_iterand['iterand'] - self.iterand['iterand'])\n", (5377, 5432), True, 'import numpy as np\n'), ((5452, 5495), 'numpy.linalg.norm', 'np.linalg.norm', (["self.old_iterand['iterand']"], {}), "(self.old_iterand['iterand'])\n", (5466, 5495), True, 'import numpy as np\n'), ((7740, 7802), 'pycsou.linop.sampling.SubSampling', 'pl.sampling.SubSampling', (['self.dim', 'active_indices'], {'dtype': 'float'}), '(self.dim, active_indices, dtype=float)\n', (7763, 7802), True, 'import pycsou.linop as pl\n'), ((8027, 8089), 'pycsou.func.loss.SquaredL2Loss', 'SquaredL2Loss', ([], {'dim': 'restricted_forward.shape[0]', 'data': 'self.data'}), '(dim=restricted_forward.shape[0], data=self.data)\n', (8040, 8089), False, 'from pycsou.func.loss import SquaredL2Loss\n'), ((13250, 13306), 'numpy.hstack', 'np.hstack', (["[self.old_iterand['positions'], self.new_ind]"], {}), "([self.old_iterand['positions'], self.new_ind])\n", (13259, 13306), True, 'import numpy as np\n'), ((13856, 13892), 'numpy.sign', 'np.sign', (['self.dual_certificate_value'], {}), '(self.dual_certificate_value)\n', (13863, 13892), True, 'import numpy as np\n'), ((14316, 14341), 'numpy.dot', 'np.dot', (['self.data', 'column'], {}), '(self.data, column)\n', (14322, 14341), True, 'import numpy as np\n'), ((14644, 14671), 'numpy.sign', 'np.sign', (['iterand[overvalue]'], {}), '(iterand[overvalue])\n', (14651, 14671), True, 'import numpy as np\n'), ((16787, 16843), 'numpy.hstack', 'np.hstack', (["[self.old_iterand['positions'], self.new_ind]"], {}), "([self.old_iterand['positions'], self.new_ind])\n", (16796, 16843), True, 'import numpy as np\n'), ((17663, 17688), 'numpy.dot', 'np.dot', (['self.data', 'column'], {}), '(self.data, column)\n', (17669, 17688), True, 'import numpy as np\n'), ((17987, 18014), 'numpy.sign', 'np.sign', (['iterand[overvalue]'], {}), '(iterand[overvalue])\n', (17994, 18014), True, 'import numpy as np\n'), ((11301, 11344), 'numpy.linalg.norm', 'np.linalg.norm', (['(self.forwardOp @ iterand)', '(2)'], {}), '(self.forwardOp @ iterand, 2)\n', (11315, 11344), True, 'import numpy as np\n'), ((11596, 11632), 'numpy.sign', 'np.sign', (['self.dual_certificate_value'], {}), '(self.dual_certificate_value)\n', (11603, 11632), True, 'import numpy as np\n'), ((14345, 14370), 'numpy.linalg.norm', 'np.linalg.norm', (['column', '(2)'], {}), '(column, 2)\n', (14359, 14370), True, 'import numpy as np\n'), ((17692, 17717), 'numpy.linalg.norm', 'np.linalg.norm', (['column', '(2)'], {}), '(column, 2)\n', (17706, 17717), True, 'import numpy as np\n'), ((11249, 11275), 'numpy.linalg.norm', 'np.linalg.norm', (['iterand', '(1)'], {}), '(iterand, 1)\n', (11263, 11275), True, 'import numpy as np\n'), ((14568, 14597), 'numpy.arange', 'np.arange', (['overvalue.shape[0]'], {}), '(overvalue.shape[0])\n', (14577, 14597), True, 'import numpy as np\n'), ((17911, 17940), 'numpy.arange', 'np.arange', (['overvalue.shape[0]'], {}), '(overvalue.shape[0])\n', (17920, 17940), True, 'import numpy as np\n'), ((10707, 10733), 'numpy.linalg.norm', 'np.linalg.norm', (['iterand', '(1)'], {}), '(iterand, 1)\n', (10721, 10733), True, 'import numpy as np\n'), ((10884, 10920), 'numpy.sign', 'np.sign', (['self.dual_certificate_value'], {}), '(self.dual_certificate_value)\n', (10891, 10920), True, 'import numpy as np\n'), ((10737, 10772), 'numpy.abs', 'np.abs', (['self.dual_certificate_value'], {}), '(self.dual_certificate_value)\n', (10743, 10772), True, 'import numpy as np\n')]
suzuken/xbrlparser
lib/rdflib-3.1.0/test/test_trix_serialize.py
d9309081b8d21113ebb7a0983c677bee971af0a1
#!/usr/bin/env python import unittest from rdflib.graph import ConjunctiveGraph from rdflib.term import URIRef, Literal from rdflib.graph import Graph class TestTrixSerialize(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def testSerialize(self): s1 = URIRef('store:1') r1 = URIRef('resource:1') r2 = URIRef('resource:2') label = URIRef('predicate:label') g1 = Graph(identifier = s1) g1.add((r1, label, Literal("label 1", lang="en"))) g1.add((r1, label, Literal("label 2"))) s2 = URIRef('store:2') g2 = Graph(identifier = s2) g2.add((r2, label, Literal("label 3"))) g = ConjunctiveGraph() for s,p,o in g1.triples((None, None, None)): g.addN([(s,p,o,g1)]) for s,p,o in g2.triples((None, None, None)): g.addN([(s,p,o,g2)]) r3 = URIRef('resource:3') g.add((r3, label, Literal(4))) r = g.serialize(format='trix') g3 = ConjunctiveGraph() from StringIO import StringIO g3.parse(StringIO(r), format='trix') for q in g3.quads((None,None,None)): # TODO: Fix once getGraph/getContext is in conjunctive graph if isinstance(q[3].identifier, URIRef): tg=Graph(store=g.store, identifier=q[3].identifier) else: # BNode, this is a bit ugly # we cannot match the bnode to the right graph automagically # here I know there is only one anonymous graph, # and that is the default one, but this is not always the case tg=g.default_context self.assertTrue(q[0:3] in tg) if __name__=='__main__': unittest.main()
[((1684, 1699), 'unittest.main', 'unittest.main', ([], {}), '()\n', (1697, 1699), False, 'import unittest\n'), ((314, 331), 'rdflib.term.URIRef', 'URIRef', (['"""store:1"""'], {}), "('store:1')\n", (320, 331), False, 'from rdflib.term import URIRef, Literal\n'), ((343, 363), 'rdflib.term.URIRef', 'URIRef', (['"""resource:1"""'], {}), "('resource:1')\n", (349, 363), False, 'from rdflib.term import URIRef, Literal\n'), ((375, 395), 'rdflib.term.URIRef', 'URIRef', (['"""resource:2"""'], {}), "('resource:2')\n", (381, 395), False, 'from rdflib.term import URIRef, Literal\n'), ((411, 436), 'rdflib.term.URIRef', 'URIRef', (['"""predicate:label"""'], {}), "('predicate:label')\n", (417, 436), False, 'from rdflib.term import URIRef, Literal\n'), ((449, 469), 'rdflib.graph.Graph', 'Graph', ([], {'identifier': 's1'}), '(identifier=s1)\n', (454, 469), False, 'from rdflib.graph import Graph\n'), ((587, 604), 'rdflib.term.URIRef', 'URIRef', (['"""store:2"""'], {}), "('store:2')\n", (593, 604), False, 'from rdflib.term import URIRef, Literal\n'), ((616, 636), 'rdflib.graph.Graph', 'Graph', ([], {'identifier': 's2'}), '(identifier=s2)\n', (621, 636), False, 'from rdflib.graph import Graph\n'), ((696, 714), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ([], {}), '()\n', (712, 714), False, 'from rdflib.graph import ConjunctiveGraph\n'), ((886, 906), 'rdflib.term.URIRef', 'URIRef', (['"""resource:3"""'], {}), "('resource:3')\n", (892, 906), False, 'from rdflib.term import URIRef, Literal\n'), ((1006, 1024), 'rdflib.graph.ConjunctiveGraph', 'ConjunctiveGraph', ([], {}), '()\n', (1022, 1024), False, 'from rdflib.graph import ConjunctiveGraph\n'), ((1077, 1088), 'StringIO.StringIO', 'StringIO', (['r'], {}), '(r)\n', (1085, 1088), False, 'from StringIO import StringIO\n'), ((497, 526), 'rdflib.term.Literal', 'Literal', (['"""label 1"""'], {'lang': '"""en"""'}), "('label 1', lang='en')\n", (504, 526), False, 'from rdflib.term import URIRef, Literal\n'), ((554, 572), 'rdflib.term.Literal', 'Literal', (['"""label 2"""'], {}), "('label 2')\n", (561, 572), False, 'from rdflib.term import URIRef, Literal\n'), ((664, 682), 'rdflib.term.Literal', 'Literal', (['"""label 3"""'], {}), "('label 3')\n", (671, 682), False, 'from rdflib.term import URIRef, Literal\n'), ((931, 941), 'rdflib.term.Literal', 'Literal', (['(4)'], {}), '(4)\n', (938, 941), False, 'from rdflib.term import URIRef, Literal\n'), ((1280, 1328), 'rdflib.graph.Graph', 'Graph', ([], {'store': 'g.store', 'identifier': 'q[3].identifier'}), '(store=g.store, identifier=q[3].identifier)\n', (1285, 1328), False, 'from rdflib.graph import Graph\n')]
leschzinerlab/myami-3.2-freeHand
dbschema/revertDBinstall.py
974b8a48245222de0d9cfb0f433533487ecce60d
#!/usr/bin/env python from sinedon import dbupgrade, dbconfig import updatelib project_dbupgrade = dbupgrade.DBUpgradeTools('projectdata', drop=True) if __name__ == "__main__": updatelib_inst = updatelib.UpdateLib(project_dbupgrade) checkout_version = raw_input('Revert to checkout version, for example, 2.1 -->') if checkout_version != 'trunk': try: map((lambda x:int(x)),checkout_version.split('.')[:2]) except: print "valid versions are 'trunk', '2.1', or '2.1.2' etc" raise checkout_revision = int(raw_input('Revert to checkout revision, for example, 16500 -->')) updatelib_inst.updateDatabaseVersion(checkout_version) print "\033[35mVersion Updated in the database %s\033[0m" % checkout_version updatelib_inst.updateDatabaseRevision(checkout_revision) print "\033[35mRevision Updated in the database as %d\033[0m" % checkout_revision
[]
guy4261/fight-churn
fightchurn/listings/chap9/listing_9_4_regression_cparam.py
f3820edd6d4af5e0bd625434d3ad4236aa781ef4
from sklearn.linear_model import LogisticRegression from fightchurn.listings.chap8.listing_8_2_logistic_regression import prepare_data, save_regression_model from fightchurn.listings.chap8.listing_8_2_logistic_regression import save_regression_summary, save_dataset_predictions def regression_cparam(data_set_path, C_param): X,y = prepare_data(data_set_path) retain_reg = LogisticRegression( C=C_param, penalty='l1', solver='liblinear', fit_intercept=True) retain_reg.fit(X, y) c_ext = '_c{:.3f}'.format(C_param) save_regression_summary(data_set_path,retain_reg,ext=c_ext) save_regression_model(data_set_path,retain_reg,ext=c_ext) save_dataset_predictions(data_set_path,retain_reg,X,ext=c_ext)
[((336, 363), 'fightchurn.listings.chap8.listing_8_2_logistic_regression.prepare_data', 'prepare_data', (['data_set_path'], {}), '(data_set_path)\n', (348, 363), False, 'from fightchurn.listings.chap8.listing_8_2_logistic_regression import prepare_data, save_regression_model\n'), ((381, 468), 'sklearn.linear_model.LogisticRegression', 'LogisticRegression', ([], {'C': 'C_param', 'penalty': '"""l1"""', 'solver': '"""liblinear"""', 'fit_intercept': '(True)'}), "(C=C_param, penalty='l1', solver='liblinear',\n fit_intercept=True)\n", (399, 468), False, 'from sklearn.linear_model import LogisticRegression\n'), ((534, 595), 'fightchurn.listings.chap8.listing_8_2_logistic_regression.save_regression_summary', 'save_regression_summary', (['data_set_path', 'retain_reg'], {'ext': 'c_ext'}), '(data_set_path, retain_reg, ext=c_ext)\n', (557, 595), False, 'from fightchurn.listings.chap8.listing_8_2_logistic_regression import save_regression_summary, save_dataset_predictions\n'), ((598, 657), 'fightchurn.listings.chap8.listing_8_2_logistic_regression.save_regression_model', 'save_regression_model', (['data_set_path', 'retain_reg'], {'ext': 'c_ext'}), '(data_set_path, retain_reg, ext=c_ext)\n', (619, 657), False, 'from fightchurn.listings.chap8.listing_8_2_logistic_regression import prepare_data, save_regression_model\n'), ((660, 725), 'fightchurn.listings.chap8.listing_8_2_logistic_regression.save_dataset_predictions', 'save_dataset_predictions', (['data_set_path', 'retain_reg', 'X'], {'ext': 'c_ext'}), '(data_set_path, retain_reg, X, ext=c_ext)\n', (684, 725), False, 'from fightchurn.listings.chap8.listing_8_2_logistic_regression import save_regression_summary, save_dataset_predictions\n')]
prise6/smart-iss-posts
notebooks/classical_clustering.py
fc913078e7fbe6343fd36ec6ca9852322247da5d
#%% [markdown] # # Clustering classique #%% [markdown] # ## import classique import os #%% %load_ext autoreload %autoreload 2 os.chdir('/home/jovyan/work') #%% [markdown] # ## Import iss #%% from iss.tools import Config from iss.tools import Tools from iss.models import SimpleConvAutoEncoder from iss.clustering import ClassicalClustering from iss.clustering import AdvancedClustering from dotenv import find_dotenv, load_dotenv import numpy as np #%% [markdown] # ## Chargement de la config #%% load_dotenv(find_dotenv()) cfg = Config(project_dir = os.getenv("PROJECT_DIR"), mode = os.getenv("MODE")) #%% [markdown] # ## Chargement du modèle #%% ## charger le modèle model_type = 'simple_conv' cfg.get('models')[model_type]['model_name'] = 'model_colab' model = SimpleConvAutoEncoder(cfg.get('models')[model_type]) #%% [markdown] ## Chargement des images #%% filenames = Tools.list_directory_filenames('data/processed/models/autoencoder/train/k/') generator_imgs = Tools.generator_np_picture_from_filenames(filenames, target_size = (27, 48), batch = 496, nb_batch = 10) #%% pictures_id, pictures_preds = Tools.encoded_pictures_from_generator(generator_imgs, model) #%% intermediate_output = pictures_preds.reshape((pictures_preds.shape[0], 3*6*16)) #%% [markdown] # ## ACP # Réduction de la dimension #%% clustering = ClassicalClustering(cfg.get('clustering')['classical'], pictures_id, intermediate_output) #%% clustering.compute_pca() #%% [markdown] # ## Kmeans # Premiers clusters #%% clustering.compute_kmeans() clustering.compute_kmeans_centers() #%% [markdown] # ## CAH # Seconds clusters #%% clustering.compute_cah() clustering.compute_cah_labels() #%% [markdown] # ## Résultats #%% [markdown] # ### Clusters intermediaires #%% fig = plt.figure(1, figsize=(12, 7)) plt.scatter(clustering.pca_reduction[:, 0], clustering.pca_reduction[:, 1], c = clustering.kmeans_labels) #%% [markdown] # ### Clusters finaux #%% plt.scatter(clustering.pca_reduction[:, 0], clustering.pca_reduction[:, 1], c = clustering.final_labels) #%% [markdown] # ### Sauvegarde des modèles #%% clustering.save() #%% # clustering = ClassicalClustering(cfg.get('clustering')['classical']) clustering.load() #%% [markdown] # ## Visualisation des clusters #%% def select_cluster(clustering, id_cluster): return [os.path.join('data/processed/models/autoencoder/train/k/', res[0] + '.jpg') for res in clustering.get_zip_results() if res[2] == id_cluster] #%% from IPython.display import Image #%% for cl in range(0,19): print("Cluster %s" % (cl)) res_tmp = select_cluster(clustering, cl) print(len(res_tmp)) image_array = [Tools.read_np_picture(f, target_size = (54, 96)) for f in res_tmp[:100]] # img = Tools.display_mosaic(image_array, nrow = 10) # fig = plt.figure(1, figsize=(12, 7)) # plt.imshow(img, aspect = 'auto') # plt.show() #%% [markdown] # ## Zoom sur le cluster 0 #%% res_tmp = select_cluster(clustering, 1) #%% print(len(res_tmp)) image_array = [Tools.read_np_picture(f, target_size = (54, 96)) for f in res_tmp] #%% Tools.display_mosaic(image_array, nrow = 18) #%% col = [1 if l == 1 else 0 for l in clustering.kmeans_labels] plt.scatter(clustering.pca_reduction[:, 0], clustering.pca_reduction[:, 1], c = col) #%% plt.scatter(clustering.pca_reduction[np.array(col) == 1, 0], clustering.pca_reduction[np.array(col) == 1, 1])
[]
kaulketh/stepper-motor-stuff
SM_28BYJ48/logger/logger.py
ca7cc78279b378e5ad8e19f9c77b794a43d9a07e
#!/usr/bin/python3 # -*- coding: utf-8 -*- # ----------------------------------------------------------- # created 02.02.2021, tkaulke # Thomas Kaulke, kaulkth@gmail.com # https://github.com/kaulketh # ----------------------------------------------------------- __author__ = "Thomas Kaulke" __email__ = "kaulketh@gmail.com" import errno import logging import os from logging.config import fileConfig # runtime location this_folder = os.path.dirname(os.path.abspath(__file__)) # define log folder related to location log_folder = os.path.join(this_folder, '../logs') # define ini and log files ini_file = 'debug.ini' info_log_file = log_folder + '/info.log' error_log_file = log_folder + '/error.log' # check if exists or create log folder try: os.makedirs(log_folder, exist_ok=True) # Python>3.2 except TypeError: try: os.makedirs(log_folder) except OSError as exc: # Python >2.5 if exc.errno == errno.EEXIST and os.path.isdir(log_folder): pass else: raise # setup configuration config_file = os.path.join(this_folder, ini_file) fileConfig(config_file, disable_existing_loggers=True) # create handlers handler_info = logging.FileHandler(os.path.join(this_folder, info_log_file)) handler_error = logging.FileHandler(os.path.join(this_folder, error_log_file)) # set levels handler_info.setLevel(logging.INFO) handler_error.setLevel(logging.ERROR) # create formatters and add to handlers format_info = \ logging.Formatter('%(asctime)s %(levelname)s ' '[ %(module)s.%(funcName)s linenr.%(lineno)s ] ' '%(message).180s', datefmt='%Y-%m-%d %H:%M:%S') format_error = \ logging.Formatter( '%(asctime)s %(levelname)s ' '[ %(module)s.%(funcName)s linenr.%(lineno)s ] ' '[ thread: %(threadName)s ] %(message)s') handler_info.setFormatter(format_info) handler_error.setFormatter(format_error) def get_logger(name: str = __name__): logger = logging.getLogger(name) # add handler logger.addHandler(handler_info) logger.addHandler(handler_error) return logger if __name__ == '__main__': pass
[((533, 569), 'os.path.join', 'os.path.join', (['this_folder', '"""../logs"""'], {}), "(this_folder, '../logs')\n", (545, 569), False, 'import os\n'), ((1062, 1097), 'os.path.join', 'os.path.join', (['this_folder', 'ini_file'], {}), '(this_folder, ini_file)\n', (1074, 1097), False, 'import os\n'), ((1098, 1152), 'logging.config.fileConfig', 'fileConfig', (['config_file'], {'disable_existing_loggers': '(True)'}), '(config_file, disable_existing_loggers=True)\n', (1108, 1152), False, 'from logging.config import fileConfig\n'), ((1476, 1625), 'logging.Formatter', 'logging.Formatter', (['"""%(asctime)s %(levelname)s [ %(module)s.%(funcName)s linenr.%(lineno)s ] %(message).180s"""'], {'datefmt': '"""%Y-%m-%d %H:%M:%S"""'}), "(\n '%(asctime)s %(levelname)s [ %(module)s.%(funcName)s linenr.%(lineno)s ] %(message).180s'\n , datefmt='%Y-%m-%d %H:%M:%S')\n", (1493, 1625), False, 'import logging\n'), ((1687, 1830), 'logging.Formatter', 'logging.Formatter', (['"""%(asctime)s %(levelname)s [ %(module)s.%(funcName)s linenr.%(lineno)s ] [ thread: %(threadName)s ] %(message)s"""'], {}), "(\n '%(asctime)s %(levelname)s [ %(module)s.%(funcName)s linenr.%(lineno)s ] [ thread: %(threadName)s ] %(message)s'\n )\n", (1704, 1830), False, 'import logging\n'), ((453, 478), 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), '(__file__)\n', (468, 478), False, 'import os\n'), ((754, 792), 'os.makedirs', 'os.makedirs', (['log_folder'], {'exist_ok': '(True)'}), '(log_folder, exist_ok=True)\n', (765, 792), False, 'import os\n'), ((1207, 1247), 'os.path.join', 'os.path.join', (['this_folder', 'info_log_file'], {}), '(this_folder, info_log_file)\n', (1219, 1247), False, 'import os\n'), ((1285, 1326), 'os.path.join', 'os.path.join', (['this_folder', 'error_log_file'], {}), '(this_folder, error_log_file)\n', (1297, 1326), False, 'import os\n'), ((1985, 2008), 'logging.getLogger', 'logging.getLogger', (['name'], {}), '(name)\n', (2002, 2008), False, 'import logging\n'), ((842, 865), 'os.makedirs', 'os.makedirs', (['log_folder'], {}), '(log_folder)\n', (853, 865), False, 'import os\n'), ((949, 974), 'os.path.isdir', 'os.path.isdir', (['log_folder'], {}), '(log_folder)\n', (962, 974), False, 'import os\n')]
Ibotta/mr_uplift
tests/test_mr_uplift.py
e1facd39a87683dfdeaf7b08336e0ce781ff87cf
import numpy as np import pytest from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1 from mr_uplift.mr_uplift import MRUplift, get_t_data from mr_uplift.keras_model_functionality import prepare_data_optimized_loss import sys import pandas as pd class TestMRUplift(object): def test_get_t_data(self): num_obs_1 = 10 num_obs_2 = 3 test_1 = get_t_data(0, num_obs_1) test_2 = get_t_data(np.array([0, 1]), num_obs_2) test_1_values = np.zeros(num_obs_1).reshape(-1, 1) test_2_values = np.concatenate([np.zeros(num_obs_2).reshape(-1, 1), np.ones(num_obs_2).reshape(-1, 1)], axis=1) assert np.mean(test_1 == test_1_values) == 1 assert np.mean(test_2 == test_2_values) == 1 def test_model_mean_outputs(self): true_ATE = np.array([[0, 0], [1, .5]]) rmse_tolerance = .05 num_obs = 10000 y_no_noise, x_no_noise, tmt_no_noise = get_no_noise_data(num_obs) uplift_model = MRUplift() uplift_model.fit(x_no_noise, y_no_noise, tmt_no_noise.reshape(-1, 1), n_jobs=1) oos_ice = uplift_model.predict_ice(response_transformer = True) assert np.sqrt(np.mean((oos_ice.mean(axis=1) -true_ATE)**2)) < rmse_tolerance def test_model_pred_oos_shapes(self): num_obs = 1000 y, x, t = get_simple_uplift_data(num_obs) t = np.concatenate([t.reshape(-1, 1), np.random.binomial(1, .5, num_obs).reshape(-1, 1)], axis=1) param_grid = dict(num_nodes=[8], dropout=[.1], activation=[ 'relu'], num_layers=[1], epochs=[1], batch_size=[1000]) uplift_model = MRUplift() uplift_model.fit(x, y, t, param_grid = param_grid, n_jobs=1) x_1 = x.copy() x_1 = pd.DataFrame(x_1) x_1.columns = ['var_'+str(x) for x in range(x.shape[1])] y_1 = y.copy() y_1 = pd.DataFrame(y_1) y_1.columns = ['var_'+str(x) for x in range(y.shape[1])] uplift_model_named = MRUplift() uplift_model_named.fit(x_1, y_1, t, param_grid = param_grid, n_jobs=1) assert uplift_model.predict_ice().shape == ( np.unique(t, axis=0).shape[0], num_obs * .7, y.shape[1]) assert uplift_model.predict_ice(x=x).shape == (np.unique(t,axis=0).shape[0], num_obs, y.shape[1]) assert uplift_model.get_erupt_curves() assert uplift_model.get_erupt_curves(x = x, y = y, t = t) assert uplift_model_named.get_erupt_curves() def test_model_pred_oos_shapes_single_col_tmt(self): num_obs = 1000 y, x, t = get_simple_uplift_data(num_obs) t = t.reshape(-1, 1) param_grid = dict(num_nodes=[8], dropout=[.1], activation=[ 'relu'], num_layers=[1], epochs=[1], batch_size=[1000]) uplift_model = MRUplift() uplift_model.fit(x, y, t, param_grid = param_grid, n_jobs=1) assert uplift_model.predict_ice().shape == ( np.unique(t, axis=0).shape[0], num_obs * .7, y.shape[1]) assert uplift_model.predict_ice(x=x).shape == (np.unique(t,axis=0).shape[0], num_obs, y.shape[1]) assert uplift_model.get_erupt_curves() assert uplift_model.get_erupt_curves(x = x, y = y, t = t) def test_model_pred_oos_shapes_single_col_tmt_propensity(self): num_obs = 1000 y, x, t = get_simple_uplift_data(num_obs) t = t.reshape(-1, 1) param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[2], epochs=[1], batch_size=[100], alpha = [.5], copy_several_times = [1]) uplift_model = MRUplift() uplift_model.fit(x, y, t, param_grid = param_grid, n_jobs=1, optimized_loss = True, use_propensity = True) assert uplift_model.predict_ice().shape == ( np.unique(t, axis=0).shape[0], num_obs * .7, y.shape[1]) assert uplift_model.predict_ice(x=x).shape == (np.unique(t,axis=0).shape[0], num_obs, y.shape[1]) assert uplift_model.get_erupt_curves() assert uplift_model.get_erupt_curves(x = x, y = y, t = t) def test_prepare_data_optimized_loss_one_col_tmt(self): num_obs = 1000 y, x, t = get_simple_uplift_data(num_obs) t = t.reshape(len(t),1) unique_treatments = np.unique(t, axis = 0) masks = np.ones(num_obs).reshape(num_obs,1) x, utility_weights, missing_utility, missing_y_mat, masks, weights = prepare_data_optimized_loss(x,y,t, masks ,unique_treatments) assert(utility_weights.shape == (num_obs, y.shape[1])) assert(missing_y_mat.shape == (num_obs, unique_treatments.shape[0], y.shape[1])) for q in range(unique_treatments.shape[0]): assert( ((missing_utility[:,q]==0) == (missing_y_mat[:,q,0] == -999)).mean() ==1 ) def test_prepare_data_optimized_loss_two_col_tmt(self): num_obs = 1000 y, x, t = get_simple_uplift_data(num_obs) t = np.concatenate([t.reshape(-1, 1), np.random.binomial(1, .5, num_obs).reshape(-1, 1)], axis=1) unique_treatments = np.unique(t, axis = 0) masks = np.ones(num_obs*len(unique_treatments)).reshape(num_obs,len(unique_treatments)) x, utility_weights, missing_utility, missing_y_mat, masks, weights = prepare_data_optimized_loss(x,y,t,masks, unique_treatments) assert(utility_weights.shape == (num_obs, y.shape[1])) assert(missing_y_mat.shape == (num_obs, unique_treatments.shape[0], y.shape[1])) for q in range(unique_treatments.shape[0]): assert( ((missing_utility[:,q]==0) == (missing_y_mat[:,q,0] == -999)).mean() ==1 ) def test_model_optim_mean_outputs(self): true_ATE = np.array([[0, 0], [1, .5]]) rmse_tolerance = .05 num_obs = 10000 param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[2], epochs=[30], batch_size=[100]) y_no_noise, x_no_noise, tmt_no_noise = get_no_noise_data(num_obs) uplift_model = MRUplift() uplift_model.fit(x_no_noise, y_no_noise, tmt_no_noise.reshape(-1, 1), n_jobs=1, param_grid = param_grid, optimized_loss = False) oos_ice = uplift_model.predict_ice(response_transformer = True) assert np.sqrt(np.mean((oos_ice.mean(axis=1) - true_ATE)**2)) < rmse_tolerance def test_model_get_random_erupts(self): true_ATE = np.array([[0, 0], [1, .5]]) rmse_tolerance = .05 num_obs = 10000 param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[2], epochs=[30], batch_size=[100], alpha = [.5], copy_several_times = [2]) y_no_noise, x_no_noise, tmt_no_noise = get_no_noise_data(num_obs) uplift_model = MRUplift() uplift_model.fit(x_no_noise, y_no_noise, tmt_no_noise.reshape(-1, 1), n_jobs=1, param_grid = param_grid, optimized_loss = True) oos_re = uplift_model.get_random_erupts() uplift_model_propensity = MRUplift() uplift_model_propensity.fit(x_no_noise, y_no_noise, tmt_no_noise.reshape(-1, 1), n_jobs=1, param_grid = param_grid, optimized_loss = True, use_propensity = True) oos_re_propensity = uplift_model_propensity.get_random_erupts() assert oos_re['mean'].iloc[0] > 0 assert oos_re_propensity['mean'].iloc[0] > 0 def test_varimp(self): num_obs = 10000 param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[2], epochs=[30], batch_size=[100]) y, x, t = get_simple_uplift_data(num_obs) uplift_model = MRUplift() uplift_model.fit(x, y, t.reshape(-1, 1), n_jobs=1, param_grid = param_grid) varimp = uplift_model.permutation_varimp(objective_weights = np.array([.7,-.3,0]).reshape(1,-1)) param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[2], epochs=[30], batch_size=[100], alpha = [.5], copy_several_times = [2]) uplift_model_propensity = MRUplift() uplift_model_propensity.fit(x, y, t.reshape(-1, 1), n_jobs=1, param_grid = param_grid, optimized_loss = True, use_propensity = True) varimp_propensity = uplift_model_propensity.permutation_varimp(objective_weights = np.array([.7,-.3,0]).reshape(1,-1)) assert varimp['permutation_varimp_metric'].iloc[0]>varimp['permutation_varimp_metric'].iloc[1] assert varimp_propensity['permutation_varimp_metric'].iloc[0]>varimp_propensity['permutation_varimp_metric'].iloc[1] def test_model_propensity(self): num_obs = 10000 TOLERANCE = .98 y, x, t, rule_assignment = get_observational_uplift_data_1(num_obs) param_grid = dict(num_nodes=[8], dropout=[.1], activation=['relu'], num_layers=[1], epochs=[20], batch_size=[512], alpha = [.9999,.99], copy_several_times = [1]) uplift_model = MRUplift() uplift_model.fit(x, y[:,0].reshape(-1,1), t, param_grid = param_grid, n_jobs=1, optimized_loss = True, use_propensity = True, test_size = 0) uplift_model.best_params_net y_test, x_test, t_test, rule_assignment_test = get_observational_uplift_data_1(num_obs) experiment_groups = np.zeros(num_obs)+2 experiment_groups[np.where(x_test[:,-2]<.5)[0]] = 1 experiment_groups[np.where(x_test[:,-2]<.33)[0]] = 0 experiment_groups[np.where(x_test[:,-1]>.8)[0]] = 3 optim_treatments_no_cuttoff = uplift_model.predict_optimal_treatments(x = x_test, use_propensity_score_cutoff = False) optim_treatments_cuttoff = uplift_model.predict_optimal_treatments(x = x_test, use_propensity_score_cutoff = True) optim_treatments_cuttoff_cat = optim_treatments_cuttoff.argmax(axis = 1) optim_treatments_no_cuttoff_cat = optim_treatments_no_cuttoff.argmax(axis = 1) correct_tmts_1 = np.array([x in [0,1] for x in optim_treatments_cuttoff_cat[np.where(experiment_groups == 0)[0]] ]).mean() correct_tmts_2 = np.array([x in [1,2] for x in optim_treatments_cuttoff_cat[np.where(experiment_groups == 1)[0]] ]).mean() correct_tmts_3 = np.array([x in [0,2] for x in optim_treatments_cuttoff_cat[np.where(experiment_groups == 2)[0]] ]).mean() correct_tmts_4 = np.array([x in [0] for x in optim_treatments_cuttoff_cat[np.where(experiment_groups == 3)[0]] ]).mean() correct_tmts_experiment_groups_1 = ((optim_treatments_cuttoff_cat[np.where(experiment_groups == 1)[0]] == 1) == x_test[np.where(experiment_groups == 1)[0],0]).mean() correct_tmts_no_cutoff = np.mean((optim_treatments_no_cuttoff_cat==1 ) == x_test[:,0]) assert correct_tmts_1>TOLERANCE assert correct_tmts_2>TOLERANCE assert correct_tmts_3>TOLERANCE assert correct_tmts_4>TOLERANCE assert correct_tmts_experiment_groups_1>TOLERANCE assert np.array_equal(optim_treatments_cuttoff_cat,optim_treatments_no_cuttoff_cat) is False assert correct_tmts_no_cutoff>TOLERANCE
[((440, 464), 'mr_uplift.mr_uplift.get_t_data', 'get_t_data', (['(0)', 'num_obs_1'], {}), '(0, num_obs_1)\n', (450, 464), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((908, 936), 'numpy.array', 'np.array', (['[[0, 0], [1, 0.5]]'], {}), '([[0, 0], [1, 0.5]])\n', (916, 936), True, 'import numpy as np\n'), ((1037, 1063), 'mr_uplift.dataset.data_simulation.get_no_noise_data', 'get_no_noise_data', (['num_obs'], {}), '(num_obs)\n', (1054, 1063), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((1088, 1098), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (1096, 1098), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((1456, 1487), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (1478, 1487), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((1768, 1778), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (1776, 1778), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((1886, 1903), 'pandas.DataFrame', 'pd.DataFrame', (['x_1'], {}), '(x_1)\n', (1898, 1903), True, 'import pandas as pd\n'), ((2007, 2024), 'pandas.DataFrame', 'pd.DataFrame', (['y_1'], {}), '(y_1)\n', (2019, 2024), True, 'import pandas as pd\n'), ((2120, 2130), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (2128, 2130), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((2733, 2764), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (2755, 2764), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((2978, 2988), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (2986, 2988), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((3538, 3569), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (3560, 3569), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((3853, 3863), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (3861, 3863), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((4464, 4495), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (4486, 4495), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((4557, 4577), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (4566, 4577), True, 'import numpy as np\n'), ((4711, 4773), 'mr_uplift.keras_model_functionality.prepare_data_optimized_loss', 'prepare_data_optimized_loss', (['x', 'y', 't', 'masks', 'unique_treatments'], {}), '(x, y, t, masks, unique_treatments)\n', (4738, 4773), False, 'from mr_uplift.keras_model_functionality import prepare_data_optimized_loss\n'), ((5175, 5206), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (5197, 5206), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((5350, 5370), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (5359, 5370), True, 'import numpy as np\n'), ((5547, 5609), 'mr_uplift.keras_model_functionality.prepare_data_optimized_loss', 'prepare_data_optimized_loss', (['x', 'y', 't', 'masks', 'unique_treatments'], {}), '(x, y, t, masks, unique_treatments)\n', (5574, 5609), False, 'from mr_uplift.keras_model_functionality import prepare_data_optimized_loss\n'), ((5972, 6000), 'numpy.array', 'np.array', (['[[0, 0], [1, 0.5]]'], {}), '([[0, 0], [1, 0.5]])\n', (5980, 6000), True, 'import numpy as np\n'), ((6224, 6250), 'mr_uplift.dataset.data_simulation.get_no_noise_data', 'get_no_noise_data', (['num_obs'], {}), '(num_obs)\n', (6241, 6250), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((6275, 6285), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (6283, 6285), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((6672, 6700), 'numpy.array', 'np.array', (['[[0, 0], [1, 0.5]]'], {}), '([[0, 0], [1, 0.5]])\n', (6680, 6700), True, 'import numpy as np\n'), ((6998, 7024), 'mr_uplift.dataset.data_simulation.get_no_noise_data', 'get_no_noise_data', (['num_obs'], {}), '(num_obs)\n', (7015, 7024), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((7049, 7059), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (7057, 7059), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((7306, 7316), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (7314, 7316), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((7900, 7931), 'mr_uplift.dataset.data_simulation.get_simple_uplift_data', 'get_simple_uplift_data', (['num_obs'], {}), '(num_obs)\n', (7922, 7931), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((7956, 7966), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (7964, 7966), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((8414, 8424), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (8422, 8424), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((9094, 9134), 'mr_uplift.dataset.data_simulation.get_observational_uplift_data_1', 'get_observational_uplift_data_1', (['num_obs'], {}), '(num_obs)\n', (9125, 9134), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((9396, 9406), 'mr_uplift.mr_uplift.MRUplift', 'MRUplift', ([], {}), '()\n', (9404, 9406), False, 'from mr_uplift.mr_uplift import MRUplift, get_t_data\n'), ((9660, 9700), 'mr_uplift.dataset.data_simulation.get_observational_uplift_data_1', 'get_observational_uplift_data_1', (['num_obs'], {}), '(num_obs)\n', (9691, 9700), False, 'from mr_uplift.dataset.data_simulation import get_no_noise_data, get_simple_uplift_data, get_observational_uplift_data_1\n'), ((11086, 11151), 'numpy.mean', 'np.mean', (['((optim_treatments_no_cuttoff_cat == 1) == x_test[:, (0)])'], {}), '((optim_treatments_no_cuttoff_cat == 1) == x_test[:, (0)])\n', (11093, 11151), True, 'import numpy as np\n'), ((493, 509), 'numpy.array', 'np.array', (['[0, 1]'], {}), '([0, 1])\n', (501, 509), True, 'import numpy as np\n'), ((758, 790), 'numpy.mean', 'np.mean', (['(test_1 == test_1_values)'], {}), '(test_1 == test_1_values)\n', (765, 790), True, 'import numpy as np\n'), ((811, 843), 'numpy.mean', 'np.mean', (['(test_2 == test_2_values)'], {}), '(test_2 == test_2_values)\n', (818, 843), True, 'import numpy as np\n'), ((9731, 9748), 'numpy.zeros', 'np.zeros', (['num_obs'], {}), '(num_obs)\n', (9739, 9748), True, 'import numpy as np\n'), ((11387, 11464), 'numpy.array_equal', 'np.array_equal', (['optim_treatments_cuttoff_cat', 'optim_treatments_no_cuttoff_cat'], {}), '(optim_treatments_cuttoff_cat, optim_treatments_no_cuttoff_cat)\n', (11401, 11464), True, 'import numpy as np\n'), ((547, 566), 'numpy.zeros', 'np.zeros', (['num_obs_1'], {}), '(num_obs_1)\n', (555, 566), True, 'import numpy as np\n'), ((4597, 4613), 'numpy.ones', 'np.ones', (['num_obs'], {}), '(num_obs)\n', (4604, 4613), True, 'import numpy as np\n'), ((9777, 9808), 'numpy.where', 'np.where', (['(x_test[:, (-2)] < 0.5)'], {}), '(x_test[:, (-2)] < 0.5)\n', (9785, 9808), True, 'import numpy as np\n'), ((9837, 9869), 'numpy.where', 'np.where', (['(x_test[:, (-2)] < 0.33)'], {}), '(x_test[:, (-2)] < 0.33)\n', (9845, 9869), True, 'import numpy as np\n'), ((9898, 9929), 'numpy.where', 'np.where', (['(x_test[:, (-1)] > 0.8)'], {}), '(x_test[:, (-1)] > 0.8)\n', (9906, 9929), True, 'import numpy as np\n'), ((622, 641), 'numpy.zeros', 'np.zeros', (['num_obs_2'], {}), '(num_obs_2)\n', (630, 641), True, 'import numpy as np\n'), ((698, 716), 'numpy.ones', 'np.ones', (['num_obs_2'], {}), '(num_obs_2)\n', (705, 716), True, 'import numpy as np\n'), ((1543, 1578), 'numpy.random.binomial', 'np.random.binomial', (['(1)', '(0.5)', 'num_obs'], {}), '(1, 0.5, num_obs)\n', (1561, 1578), True, 'import numpy as np\n'), ((2276, 2296), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (2285, 2296), True, 'import numpy as np\n'), ((2389, 2409), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (2398, 2409), True, 'import numpy as np\n'), ((3124, 3144), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (3133, 3144), True, 'import numpy as np\n'), ((3237, 3257), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (3246, 3257), True, 'import numpy as np\n'), ((4057, 4077), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (4066, 4077), True, 'import numpy as np\n'), ((4170, 4190), 'numpy.unique', 'np.unique', (['t'], {'axis': '(0)'}), '(t, axis=0)\n', (4179, 4190), True, 'import numpy as np\n'), ((5262, 5297), 'numpy.random.binomial', 'np.random.binomial', (['(1)', '(0.5)', 'num_obs'], {}), '(1, 0.5, num_obs)\n', (5280, 5297), True, 'import numpy as np\n'), ((8145, 8169), 'numpy.array', 'np.array', (['[0.7, -0.3, 0]'], {}), '([0.7, -0.3, 0])\n', (8153, 8169), True, 'import numpy as np\n'), ((8707, 8731), 'numpy.array', 'np.array', (['[0.7, -0.3, 0]'], {}), '([0.7, -0.3, 0])\n', (8715, 8731), True, 'import numpy as np\n'), ((10951, 10983), 'numpy.where', 'np.where', (['(experiment_groups == 1)'], {}), '(experiment_groups == 1)\n', (10959, 10983), True, 'import numpy as np\n'), ((11005, 11037), 'numpy.where', 'np.where', (['(experiment_groups == 1)'], {}), '(experiment_groups == 1)\n', (11013, 11037), True, 'import numpy as np\n'), ((10438, 10470), 'numpy.where', 'np.where', (['(experiment_groups == 0)'], {}), '(experiment_groups == 0)\n', (10446, 10470), True, 'import numpy as np\n'), ((10569, 10601), 'numpy.where', 'np.where', (['(experiment_groups == 1)'], {}), '(experiment_groups == 1)\n', (10577, 10601), True, 'import numpy as np\n'), ((10700, 10732), 'numpy.where', 'np.where', (['(experiment_groups == 2)'], {}), '(experiment_groups == 2)\n', (10708, 10732), True, 'import numpy as np\n'), ((10829, 10861), 'numpy.where', 'np.where', (['(experiment_groups == 3)'], {}), '(experiment_groups == 3)\n', (10837, 10861), True, 'import numpy as np\n')]
Full-Data-Alchemist/lambdata-Mani-alch
lambdataalchemani/lambda_test.py
90dcbc091d8f9841d5a1046e64437058a4156dc5
""" """ import unittest from example_module import COLORS, increment class ExampleTest(unittest.TestCase): """ #TODO """ def test_increment(self): x0 = 0 y0 = increment(x0) #y0 == 1 self.assertEqual(y0, 1) x1 = 100 y1 = increment(x1) #y1 == 101 self.assertEqual(y1, 101)
[((195, 208), 'example_module.increment', 'increment', (['x0'], {}), '(x0)\n', (204, 208), False, 'from example_module import COLORS, increment\n'), ((285, 298), 'example_module.increment', 'increment', (['x1'], {}), '(x1)\n', (294, 298), False, 'from example_module import COLORS, increment\n')]
bopopescu/hue-5
desktop/core/src/desktop/auth/views.py
665c275d0c0570b1a4a34a293503cc72ec35695c
#!/usr/bin/env python # Licensed to Cloudera, Inc. under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. Cloudera, Inc. licenses this file # to you 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. try: import oauth2 as oauth except: oauth = None import cgi import logging import urllib from datetime import datetime from axes.decorators import watch_login import django.contrib.auth.views from django.core import urlresolvers from django.core.exceptions import SuspiciousOperation from django.contrib.auth import login, get_backends, authenticate from django.contrib.auth.models import User from django.contrib.sessions.models import Session from django.http import HttpResponseRedirect from django.utils.translation import ugettext as _ from desktop.auth import forms as auth_forms from desktop.lib.django_util import render from desktop.lib.django_util import login_notrequired from desktop.lib.django_util import JsonResponse from desktop.log.access import access_warn, last_access_map from desktop.conf import LDAP, OAUTH, DEMO_ENABLED from hadoop.fs.exceptions import WebHdfsException from useradmin.models import get_profile from useradmin.views import ensure_home_directory, require_change_password LOG = logging.getLogger(__name__) def get_current_users(): """Return dictionary of User objects and a dictionary of the user's IP address and last access time""" current_users = { } for session in Session.objects.all(): try: uid = session.get_decoded().get(django.contrib.auth.SESSION_KEY) except SuspiciousOperation: # If secret_key changed, this resolution won't work. uid = None if uid is not None: try: userobj = User.objects.get(pk=uid) current_users[userobj] = last_access_map.get(userobj.username, { }) except User.DoesNotExist: LOG.debug("User with id=%d does not exist" % uid) return current_users def first_login_ever(): backends = get_backends() for backend in backends: if hasattr(backend, 'is_first_login_ever') and backend.is_first_login_ever(): return True return False def get_backend_names(): return get_backends and [backend.__class__.__name__ for backend in get_backends()] @login_notrequired @watch_login def dt_login(request, from_modal=False): redirect_to = request.REQUEST.get('next', '/') is_first_login_ever = first_login_ever() backend_names = get_backend_names() is_active_directory = 'LdapBackend' in backend_names and ( bool(LDAP.NT_DOMAIN.get()) or bool(LDAP.LDAP_SERVERS.get()) ) if is_active_directory: UserCreationForm = auth_forms.LdapUserCreationForm AuthenticationForm = auth_forms.LdapAuthenticationForm else: UserCreationForm = auth_forms.UserCreationForm AuthenticationForm = auth_forms.AuthenticationForm if request.method == 'POST': request.audit = { 'operation': 'USER_LOGIN', 'username': request.POST.get('username') } # For first login, need to validate user info! first_user_form = is_first_login_ever and UserCreationForm(data=request.POST) or None first_user = first_user_form and first_user_form.is_valid() if first_user or not is_first_login_ever: auth_form = AuthenticationForm(data=request.POST) if auth_form.is_valid(): # Must login by using the AuthenticationForm. # It provides 'backends' on the User object. user = auth_form.get_user() userprofile = get_profile(user) login(request, user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() auto_create_home_backends = ['AllowAllBackend', 'LdapBackend', 'SpnegoDjangoBackend'] if is_first_login_ever or any(backend in backend_names for backend in auto_create_home_backends): # Create home directory for first user. try: ensure_home_directory(request.fs, user.username) except (IOError, WebHdfsException), e: LOG.error(_('Could not create home directory.'), exc_info=e) request.error(_('Could not create home directory.')) if require_change_password(userprofile): return HttpResponseRedirect(urlresolvers.reverse('useradmin.views.edit_user', kwargs={'username': user.username})) userprofile.first_login = False userprofile.last_activity = datetime.now() userprofile.save() msg = 'Successful login for user: %s' % user.username request.audit['operationText'] = msg access_warn(request, msg) if from_modal or request.REQUEST.get('fromModal', 'false') == 'true': return JsonResponse({'auth': True}) else: return HttpResponseRedirect(redirect_to) else: request.audit['allowed'] = False msg = 'Failed login for user: %s' % request.POST.get('username') request.audit['operationText'] = msg access_warn(request, msg) if from_modal or request.REQUEST.get('fromModal', 'false') == 'true': return JsonResponse({'auth': False}) else: first_user_form = None auth_form = AuthenticationForm() if DEMO_ENABLED.get() and not 'admin' in request.REQUEST: user = authenticate(username=request.user.username, password='HueRocks') login(request, user) ensure_home_directory(request.fs, user.username) return HttpResponseRedirect(redirect_to) if not from_modal: request.session.set_test_cookie() renderable_path = 'login.mako' if from_modal: renderable_path = 'login_modal.mako' return render(renderable_path, request, { 'action': urlresolvers.reverse('desktop.auth.views.dt_login'), 'form': first_user_form or auth_form, 'next': redirect_to, 'first_login_ever': is_first_login_ever, 'login_errors': request.method == 'POST', 'backend_names': backend_names, 'active_directory': is_active_directory }) def dt_logout(request, next_page=None): """Log out the user""" username = request.user.get_username() request.audit = { 'username': username, 'operation': 'USER_LOGOUT', 'operationText': 'Logged out user: %s' % username } backends = get_backends() if backends: for backend in backends: if hasattr(backend, 'logout'): response = backend.logout(request, next_page) if response: return response return django.contrib.auth.views.logout(request, next_page) def profile(request): """ Dumps JSON for user-profile information. """ return render(None, request, _profile_dict(request.user)) def _profile_dict(user): return dict( username=user.username, first_name=user.first_name, last_name=user.last_name, last_login=str(user.last_login), # datetime object needs to be converted email=user.email) # OAuth is based on Twitter as example. @login_notrequired def oauth_login(request): assert oauth is not None consumer = oauth.Consumer(OAUTH.CONSUMER_KEY.get(), OAUTH.CONSUMER_SECRET.get()) client = oauth.Client(consumer) resp, content = client.request(OAUTH.REQUEST_TOKEN_URL.get(), "POST", body=urllib.urlencode({ 'oauth_callback': 'http://' + request.get_host() + '/login/oauth_authenticated/' })) if resp['status'] != '200': raise Exception(_("Invalid response from OAuth provider: %s") % resp) request.session['request_token'] = dict(cgi.parse_qsl(content)) url = "%s?oauth_token=%s" % (OAUTH.AUTHENTICATE_URL.get(), request.session['request_token']['oauth_token']) return HttpResponseRedirect(url) @login_notrequired def oauth_authenticated(request): consumer = oauth.Consumer(OAUTH.CONSUMER_KEY.get(), OAUTH.CONSUMER_SECRET.get()) token = oauth.Token(request.session['request_token']['oauth_token'], request.session['request_token']['oauth_token_secret']) client = oauth.Client(consumer, token) resp, content = client.request(OAUTH.ACCESS_TOKEN_URL.get(), "GET") if resp['status'] != '200': raise Exception(_("Invalid response from OAuth provider: %s") % resp) access_token = dict(cgi.parse_qsl(content)) user = authenticate(access_token=access_token) login(request, user) redirect_to = request.REQUEST.get('next', '/') return HttpResponseRedirect(redirect_to)
[]
AlonsoReyes/t-intersection-graph
models/node.py
68bab234cd6e334edcec27bfee3e019f08997945
class Node(object): def __init__(self, name, follow_list, intention, lane): self.name = name self.follow_list = follow_list self.intention = intention self.lane = lane def __eq__(self, other): if isinstance(other, Node): if self.name == other.get_name() and self.follow_list == other.get_follow_list() \ and self.intention == other.get_intention() and self.lane == other.get_lane(): return True return False def get_name(self): return self.name def set_name(self, name): self.name = name def get_follow_list(self): return self.follow_list def set_follow_list(self, follow_list): self.follow_list = follow_list def get_intention(self): return self.intention def set_intention(self, intention): self.intention = intention def get_lane(self): return self.lane def set_lane(self, lane): self.lane = lane
[]
tim-werner/gsheets-db-api
gsheetsdb/url.py
12f2a4fbe1bd5aa36781226759326ce782b08a91
from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals from collections import OrderedDict from moz_sql_parser import parse as parse_sql import pyparsing import re from six.moves.urllib import parse FROM_REGEX = re.compile(' from ("http.*?")', re.IGNORECASE) def get_url(url, headers=0, gid=0, sheet=None): parts = parse.urlparse(url) if parts.path.endswith('/edit'): path = parts.path[:-len('/edit')] else: path = parts.path path = '/'.join((path.rstrip('/'), 'gviz/tq')) qs = parse.parse_qs(parts.query) if 'headers' in qs: headers = int(qs['headers'][-1]) if 'gid' in qs: gid = qs['gid'][-1] if 'sheet' in qs: sheet = qs['sheet'][-1] if parts.fragment.startswith('gid='): gid = parts.fragment[len('gid='):] args = OrderedDict() if headers > 0: args['headers'] = headers if sheet is not None: args['sheet'] = sheet else: args['gid'] = gid params = parse.urlencode(args) return parse.urlunparse( (parts.scheme, parts.netloc, path, None, params, None)) def extract_url(sql): try: return parse_sql(sql)['from'] except pyparsing.ParseException: # fallback to regex to extract from match = FROM_REGEX.search(sql) if match: return match.group(1).strip('"')
[((310, 356), 're.compile', 're.compile', (['""" from ("http.*?")"""', 're.IGNORECASE'], {}), '(\' from ("http.*?")\', re.IGNORECASE)\n', (320, 356), False, 'import re\n'), ((419, 438), 'six.moves.urllib.parse.urlparse', 'parse.urlparse', (['url'], {}), '(url)\n', (433, 438), False, 'from six.moves.urllib import parse\n'), ((615, 642), 'six.moves.urllib.parse.parse_qs', 'parse.parse_qs', (['parts.query'], {}), '(parts.query)\n', (629, 642), False, 'from six.moves.urllib import parse\n'), ((908, 921), 'collections.OrderedDict', 'OrderedDict', ([], {}), '()\n', (919, 921), False, 'from collections import OrderedDict\n'), ((1081, 1102), 'six.moves.urllib.parse.urlencode', 'parse.urlencode', (['args'], {}), '(args)\n', (1096, 1102), False, 'from six.moves.urllib import parse\n'), ((1115, 1187), 'six.moves.urllib.parse.urlunparse', 'parse.urlunparse', (['(parts.scheme, parts.netloc, path, None, params, None)'], {}), '((parts.scheme, parts.netloc, path, None, params, None))\n', (1131, 1187), False, 'from six.moves.urllib import parse\n'), ((1245, 1259), 'moz_sql_parser.parse', 'parse_sql', (['sql'], {}), '(sql)\n', (1254, 1259), True, 'from moz_sql_parser import parse as parse_sql\n')]
joyjeni/detr-fine
detr/datasets/construction_panoptic.py
dfc0f4abc2579a2b3ef4527904af3345c7a9de4d
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved import json from pathlib import Path import numpy as np import torch from PIL import Image from panopticapi.utils import rgb2id # from util.box_ops import masks_to_boxes from .construction import make_construction_transforms import logging def box_xywh_to_xyxy(x): xs, ys, w, h = x.unbind(-1) b = [xs, ys, (xs + w), (ys + h)] return torch.stack(b, dim=-1) def masks_to_boxes(segments): boxes = [] labels = [] iscrowd = [] area = [] for ann in segments: if len(ann["bbox"]) == 4: boxes.append(ann["bbox"]) area.append(ann['area']) else: boxes.append([0, 0, 2, 2]) area.append(4) labels.append(ann["category_id"]) iscrowd.append(ann['iscrowd']) if len(boxes) == 0 and len(labels) == 0: boxes.append([0, 0, 2, 2]) labels.append(1) area.append(4) iscrowd.append(0) boxes = torch.tensor(boxes, dtype=torch.int64) labels = torch.tensor(labels, dtype=torch.int64) iscrowd = torch.tensor(iscrowd) area = torch.tensor(area) boxes = box_xywh_to_xyxy(boxes) return boxes, labels, iscrowd, area class ConstructionPanoptic: def __init__(self, img_folder, ann_folder, ann_file, transforms=None, return_masks=True): with open(ann_file, "r") as f: self.coco = json.load(f) # sort 'images' field so that they are aligned with 'annotations' # i.e., in alphabetical order self.coco["images"] = sorted(self.coco["images"], key=lambda x: x["id"]) # sanity check if "annotations" in self.coco: for img, ann in zip(self.coco["images"], self.coco["annotations"]): assert img["file_name"][:-4] == ann["file_name"][:-4] self.img_folder = img_folder self.ann_folder = ann_folder self.ann_file = ann_file self.transforms = transforms self.return_masks = return_masks def __getitem__(self, idx): try: ann_info = ( self.coco["annotations"][idx] if "annotations" in self.coco else self.coco["images"][idx] ) img_path = Path(self.img_folder) / ann_info["file_name"].replace(".png", ".jpg") ann_path = Path(self.ann_folder) / ann_info["file_name"] img = Image.open(img_path).convert("RGB") w, h = img.size if "segments_info" in ann_info: masks = np.asarray(Image.open(ann_path), dtype=np.uint32) masks = rgb2id(masks) ids = np.array([ann["id"] for ann in ann_info["segments_info"]]) masks = masks == ids[:, None, None] masks = torch.as_tensor(masks, dtype=torch.uint8) # labels = torch.tensor( # [ann["category_id"] for ann in ann_info["segments_info"]], # dtype=torch.int64, # ) target = {} target['image_id'] = torch.tensor([ann_info['image_id'] if "image_id" in ann_info else ann_info["id"]]) if self.return_masks: target['masks'] = masks boxes, labels, iscrowd, area = masks_to_boxes(ann_info["segments_info"]) target['labels'] = labels # Instead of finding boxes, just take the one from json info available # target["boxes"] = masks_to_boxes(ann_info["segments_info"]) target["boxes"] = boxes target['size'] = torch.as_tensor([int(h), int(w)]) target['orig_size'] = torch.as_tensor([int(h), int(w)]) target['iscrowd'] = iscrowd target['area'] = area # if "segments_info" in ann_info: # for name in ['iscrowd', 'area']: # target[name] = torch.tensor([ann[name] for ann in ann_info['segments_info']]) if self.transforms is not None: img, target = self.transforms(img, target) return img, target except Exception as e: logging.error(ann_info) raise e def __len__(self): return len(self.coco['images']) def get_height_and_width(self, idx): img_info = self.coco['images'][idx] height = img_info['height'] width = img_info['width'] return height, width def build(image_set, args): root = Path(args.data_path) assert ( root.exists() ), f"provided Panoptic path {root} does not exist" mode = "panoptic" PATHS = { "train": ("images", f"{mode}", f"{mode}.json"), "val": ("images", f"val_{mode}", f"val_{mode}.json"), } img_folder, ann_folder, ann_file = PATHS[image_set] img_folder_path = root / img_folder ann_folder_path = root / ann_folder ann_file = root / ann_file dataset = ConstructionPanoptic( img_folder_path, ann_folder_path, ann_file, transforms=make_construction_transforms(image_set), return_masks=args.masks, ) return dataset
[((422, 444), 'torch.stack', 'torch.stack', (['b'], {'dim': '(-1)'}), '(b, dim=-1)\n', (433, 444), False, 'import torch\n'), ((1007, 1045), 'torch.tensor', 'torch.tensor', (['boxes'], {'dtype': 'torch.int64'}), '(boxes, dtype=torch.int64)\n', (1019, 1045), False, 'import torch\n'), ((1059, 1098), 'torch.tensor', 'torch.tensor', (['labels'], {'dtype': 'torch.int64'}), '(labels, dtype=torch.int64)\n', (1071, 1098), False, 'import torch\n'), ((1113, 1134), 'torch.tensor', 'torch.tensor', (['iscrowd'], {}), '(iscrowd)\n', (1125, 1134), False, 'import torch\n'), ((1146, 1164), 'torch.tensor', 'torch.tensor', (['area'], {}), '(area)\n', (1158, 1164), False, 'import torch\n'), ((4513, 4533), 'pathlib.Path', 'Path', (['args.data_path'], {}), '(args.data_path)\n', (4517, 4533), False, 'from pathlib import Path\n'), ((1427, 1439), 'json.load', 'json.load', (['f'], {}), '(f)\n', (1436, 1439), False, 'import json\n'), ((3116, 3203), 'torch.tensor', 'torch.tensor', (["[ann_info['image_id'] if 'image_id' in ann_info else ann_info['id']]"], {}), "([ann_info['image_id'] if 'image_id' in ann_info else ann_info[\n 'id']])\n", (3128, 3203), False, 'import torch\n'), ((2278, 2299), 'pathlib.Path', 'Path', (['self.img_folder'], {}), '(self.img_folder)\n', (2282, 2299), False, 'from pathlib import Path\n'), ((2371, 2392), 'pathlib.Path', 'Path', (['self.ann_folder'], {}), '(self.ann_folder)\n', (2375, 2392), False, 'from pathlib import Path\n'), ((2642, 2655), 'panopticapi.utils.rgb2id', 'rgb2id', (['masks'], {}), '(masks)\n', (2648, 2655), False, 'from panopticapi.utils import rgb2id\n'), ((2679, 2737), 'numpy.array', 'np.array', (["[ann['id'] for ann in ann_info['segments_info']]"], {}), "([ann['id'] for ann in ann_info['segments_info']])\n", (2687, 2737), True, 'import numpy as np\n'), ((2815, 2856), 'torch.as_tensor', 'torch.as_tensor', (['masks'], {'dtype': 'torch.uint8'}), '(masks, dtype=torch.uint8)\n', (2830, 2856), False, 'import torch\n'), ((4178, 4201), 'logging.error', 'logging.error', (['ann_info'], {}), '(ann_info)\n', (4191, 4201), False, 'import logging\n'), ((2436, 2456), 'PIL.Image.open', 'Image.open', (['img_path'], {}), '(img_path)\n', (2446, 2456), False, 'from PIL import Image\n'), ((2579, 2599), 'PIL.Image.open', 'Image.open', (['ann_path'], {}), '(ann_path)\n', (2589, 2599), False, 'from PIL import Image\n')]
diyarkudrat/CS-1.3-Core-Data-Structures
Code/all-starter-code/search.py
7d7d48ad7913cded7b0ea75ced144d0a08989924
#!python """ ANNOTATE FUNCTIONS WITH TIME AND SPACE COMPLEXITY!!!!! """ def linear_search(array, item): """return the first index of item in array or None if item is not found""" return linear_search_iterative(array, item) # return linear_search_recursive(array, item) def linear_search_iterative(array, item): """Time complexity: O(n) because you iterate through n amount of items in array Space Complexity: O(n) because there are n amount of items""" # loop over all array values until item is found for index, value in enumerate(array): #O(n) if item == value: #O(1) return index # found O(1) return None # not found O(1) def linear_search_recursive(array, item, index=0): """Time complexity: O(n) because you are returning the function continuously until index equals to nth-item """ if len(array) <= index: return index if array[index] == item: return index else: return linear_search_recursive(array, item, index + 1) def binary_search(array, item): """return the index of item in sorted array or None if item is not found""" return binary_search_iterative(array, item) # return binary_search_recursive(array, item) def binary_search_iterative(array, item): """Time Complexity: O(log*n) because you are constantly dividing the length of array by 2 until array length is 1 Space Complexity: O(1) """ left, right = 0, len(array) - 1 if len(array) == 0: return None while left <= right: middle = left + (right - left) // 2 if item == array[middle]: return middle elif item > array[middle]: left = middle + 1 else: right = middle - 1 return None def binary_search_recursive(array, item, left=None, right=None): """Time Complexity: O(log*n) Space Complexity: 0(log*n) recursion call stack space""" # TODO: implement binary search recursively here if left is None and right is None: left, right = 0, len(array) - 1 middle = left + (right - left) // 2 if left > right: return None if array[middle] == item: return middle elif item > array[middle]: return binary_search_recursive(array, item, middle + 1, right) else: return binary_search_recursive(array, item, left, middle - 1)
[]
mat-heim/max_ros
max_ai/src/max_ai/mem_db.py
e01e4f5b2db96d94865d80452d41b8dcf1412232
#!/usr/bin/python ''' memory class stored in sqlite data base holds raw input and memories in parse taged columns ''' import sys import re import sqlite3 import os from datetime import date, datetime from pattern.en import parse from pattern.en import pprint from pattern.en import parsetree from pattern.en import wordnet from pattern.en import pluralize, singularize from pattern.en import conjugate, lemma, lexeme #dir = os.path.dirname(os.path.abspath(__file__)) dir = '/home/erni/catkin_ws/src/max_ros/max_ai/src/max_ai/' RM = sqlite3.connect(dir +'robbie_memory.sqlite') #RM = sqlite3.connect(dir + '/data/robbie_memory.db') cursor = RM.cursor() # Information about a single concept class conceptClass: def __init__(self, state='none', locality='none'): self.state = state # what/how is 'concept' self.reference = 'none' # unused self.locality = locality # where is 'concept' self.person = '3sg' # e.g. a thing is 3rd-person, singular self.isProperNoun = False # True if proper noun: e.g. Robert self.properties = {} # Dict of custom properties, e.g. 'age' = 39, 'color' = 'blue' # Robbie memory class. Collection of concepts class memoryClass(): def __init__(self): self.concepts = {} self.person = {'I': '1sg', 'you': '2sg' } self.posessivePronouns = {'1sg': 'my', '2sg': 'your', '3sg': 'its' } # Add a concept to memory def add(self, c): # add oncept to raw_input table in robbie_memory # x= # dt = datetime.now() # RM.execute("insert into RAW_INPUT (RAW, DATE) values (?, ?)",(c, dt)) # RM.commit() self.concepts[c] = conceptClass() if c in self.person: self.concepts[c].person = self.person[c] else: self.concepts[c].person = '3sg' # Return True if concept 'c' (string) is in memory def known(self, c): cursor.execute('''SELECT concept, location FROM memory WHERE concept =?''', (c,)) user = cursor.fetchone() # if user == 'None': return user def add_memory(self, a, b): c = '3sg' dt = datetime.now() RM.execute("insert into memory (concept, location, person,DATE) values (?, ?, ?, ?)", (a, b, c, dt)) RM.commit() def update_memory(self, a, b): cursor.execute('''UPDATE memory SET location = ? WHERE concept = ? ''', (b, a)) RM.commit() def search_memory(self, a): cursor.execute('''SELECT concept,location, person FROM memory WHERE concept =?''', (a,)) user = cursor.fetchone() return user def search_profile(self, a): cursor.execute('''SELECT value FROM profile WHERE item =?''', (a,)) user = cursor.fetchone() return user def Dump(self): return (self.concepts.state)
[((537, 582), 'sqlite3.connect', 'sqlite3.connect', (["(dir + 'robbie_memory.sqlite')"], {}), "(dir + 'robbie_memory.sqlite')\n", (552, 582), False, 'import sqlite3\n'), ((2301, 2315), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (2313, 2315), False, 'from datetime import date, datetime\n')]
pierre-haessig/matplotlib
examples/pylab_examples/image_masked.py
0d945044ca3fbf98cad55912584ef80911f330c6
#!/usr/bin/env python '''imshow with masked array input and out-of-range colors. The second subplot illustrates the use of BoundaryNorm to get a filled contour effect. ''' from pylab import * from numpy import ma import matplotlib.colors as colors delta = 0.025 x = y = arange(-3.0, 3.0, delta) X, Y = meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = 10 * (Z2-Z1) # difference of Gaussians # Set up a colormap: palette = cm.gray palette.set_over('r', 1.0) palette.set_under('g', 1.0) palette.set_bad('b', 1.0) # Alternatively, we could use # palette.set_bad(alpha = 0.0) # to make the bad region transparent. This is the default. # If you comment out all the palette.set* lines, you will see # all the defaults; under and over will be colored with the # first and last colors in the palette, respectively. Zm = ma.masked_where(Z > 1.2, Z) # By setting vmin and vmax in the norm, we establish the # range to which the regular palette color scale is applied. # Anything above that range is colored based on palette.set_over, etc. subplot(1,2,1) im = imshow(Zm, interpolation='bilinear', cmap=palette, norm = colors.Normalize(vmin = -1.0, vmax = 1.0, clip = False), origin='lower', extent=[-3,3,-3,3]) title('Green=low, Red=high, Blue=bad') colorbar(im, extend='both', orientation='horizontal', shrink=0.8) subplot(1,2,2) im = imshow(Zm, interpolation='nearest', cmap=palette, norm = colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1], ncolors=256, clip = False), origin='lower', extent=[-3,3,-3,3]) title('With BoundaryNorm') colorbar(im, extend='both', spacing='proportional', orientation='horizontal', shrink=0.8) show()
[((887, 914), 'numpy.ma.masked_where', 'ma.masked_where', (['(Z > 1.2)', 'Z'], {}), '(Z > 1.2, Z)\n', (902, 914), False, 'from numpy import ma\n'), ((1192, 1241), 'matplotlib.colors.Normalize', 'colors.Normalize', ([], {'vmin': '(-1.0)', 'vmax': '(1.0)', 'clip': '(False)'}), '(vmin=-1.0, vmax=1.0, clip=False)\n', (1208, 1241), True, 'import matplotlib.colors as colors\n'), ((1480, 1558), 'matplotlib.colors.BoundaryNorm', 'colors.BoundaryNorm', (['[-1, -0.5, -0.2, 0, 0.2, 0.5, 1]'], {'ncolors': '(256)', 'clip': '(False)'}), '([-1, -0.5, -0.2, 0, 0.2, 0.5, 1], ncolors=256, clip=False)\n', (1499, 1558), True, 'import matplotlib.colors as colors\n')]
d3vzer0/reternal-backend
app/schemas/socket.py
aeeb613c820759212e7aef9150738a66b2882d50
from pydantic import BaseModel, validator, Field from typing import List, Dict from datetime import datetime class Authenticate(BaseModel): access_token: str
[]
aniket091/modmail-plugins-1
meme/meme.py
4360ff885f27e5c9488ea5cf9431aff20435209b
import discord from discord.ext import commands import requests import random from box import Box class WildMemes(commands.Cog): """ Randomly spawns memes. """ subreddits = [ "dankmemes", "wholesomememes", "memes", "terriblefacebookmemes", "historymemes", "me_irl", "2meirl4meirl", "fellowkids", "tumblr" ] def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_message(self, message): if message.author == self.bot.user: return elif random.randint(0, 100) < 25: async with message.channel.typing(): chosen_sub = random.choice(self.subreddits) r = requests.get(f"https://api.reddit.com/r/{chosen_sub}/top.json?sort=top&t=day&limit=500", headers={'User-agent': 'Super Bot 9000'}) r = r.json() boxed = Box(r) data = (random.choice(boxed.data.children)).data image = data.url upvotes = data.ups title = data.title subreddit = data.subreddit_name_prefixed embed = discord.Embed(title=f'Meme Title: {title}', color=0x6bdcd7) embed.set_author(name="A wild meme has appeared!") embed.set_image(url=image) embed.set_footer(text=f"On {subreddit} with {upvotes} upvotes.") await message.channel.send(embed=embed) def setup(bot): bot.add_cog(WildMemes(bot))
[((423, 446), 'discord.ext.commands.Cog.listener', 'commands.Cog.listener', ([], {}), '()\n', (444, 446), False, 'from discord.ext import commands\n'), ((565, 587), 'random.randint', 'random.randint', (['(0)', '(100)'], {}), '(0, 100)\n', (579, 587), False, 'import random\n'), ((672, 702), 'random.choice', 'random.choice', (['self.subreddits'], {}), '(self.subreddits)\n', (685, 702), False, 'import random\n'), ((723, 862), 'requests.get', 'requests.get', (['f"""https://api.reddit.com/r/{chosen_sub}/top.json?sort=top&t=day&limit=500"""'], {'headers': "{'User-agent': 'Super Bot 9000'}"}), "(\n f'https://api.reddit.com/r/{chosen_sub}/top.json?sort=top&t=day&limit=500',\n headers={'User-agent': 'Super Bot 9000'})\n", (735, 862), False, 'import requests\n'), ((936, 942), 'box.Box', 'Box', (['r'], {}), '(r)\n', (939, 942), False, 'from box import Box\n'), ((1192, 1250), 'discord.Embed', 'discord.Embed', ([], {'title': 'f"""Meme Title: {title}"""', 'color': '(7068887)'}), "(title=f'Meme Title: {title}', color=7068887)\n", (1205, 1250), False, 'import discord\n'), ((967, 1001), 'random.choice', 'random.choice', (['boxed.data.children'], {}), '(boxed.data.children)\n', (980, 1001), False, 'import random\n')]
phnomcobra/PCAT2PY
pcat2py/class/20bdcef0-5cc5-11e4-af55-00155d01fe08.py
937c3b365cdc5ac69b78f59070be0a21bdb53db0
#!/usr/bin/python ################################################################################ # 20bdcef0-5cc5-11e4-af55-00155d01fe08 # # Justin Dierking # justindierking@hardbitsolutions.com # phnomcobra@gmail.com # # 10/24/2014 Original Construction ################################################################################ class Finding: def __init__(self): self.output = [] self.is_compliant = False self.uuid = "20bdcef0-5cc5-11e4-af55-00155d01fe08" def check(self, cli): # Initialize Compliance self.is_compliant = True # Get Registry MultiSZ multi_sz = cli.get_reg_multi_sz(r'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers\winreg\allowedExactPaths', 'Machine') # Output Lines self.output = [r'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers\winreg\allowedExactPaths', ('Machine=')] + multi_sz # Recommended MultiSZ rec_multi_sz = ("System\CurrentControlSet\Control\ProductOptions,System\CurrentControlSet\Control\Server Applications,Software\Microsoft\Windows NT\CurrentVersion") for sz in multi_sz: if sz.lower() not in rec_multi_sz.lower(): self.is_compliant = False return self.is_compliant def fix(self, cli): cli.powershell(r"New-Item -path 'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers'") cli.powershell(r"New-Item -path 'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers\winreg'") cli.powershell(r"New-Item -path 'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers\winreg\allowedExactPaths'") cli.powershell(r"Set-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\control\SecurePipeServers\winreg\allowedExactPaths' -name 'Machine' -Type MultiString -value System\CurrentControlSet\Control\ProductOptions,System\CurrentControlSet\Control\Server Applications,Software\Microsoft\Windows NT\CurrentVersion")
[]
dzzhvks94vd2/mikan
mikan/exceptions.py
569b331cff02a089721fd6d0a430d5c2812b4934
class MikanException(Exception): """Generic Mikan exception""" class ConversionError(MikanException, ValueError): """Cannot convert a string"""
[]