repo_name
stringlengths 7
94
| repo_path
stringlengths 4
237
| repo_head_hexsha
stringlengths 40
40
| content
stringlengths 10
680k
| apis
stringlengths 2
680k
|
---|---|---|---|---|
hhhameem/CV-Recommender | cv_recommender/account/urls.py | b85d53934f0d888835ab8201be388d7d69f0693d | from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('register/', views.register, name='register'),
path('login/', views.userlogin, name='login'),
path('logout/', views.userlogout, name='logout'),
path('password_change/', auth_views.PasswordChangeView.as_view(),
name='password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(),
name='password_change_done'),
path('password_reset/', auth_views.PasswordResetView.as_view(),
name='password_reset'),
path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(),
name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(),
name='password_reset_complete'),
path('applicantdashboard/', views.applicantdashboard,
name='applicantdashboard'),
path('recruiterdashboard/', views.recruiterdashboard,
name='recruiterdashboard'),
path('applicantdashboard/profile-edit/', views.applicantedit,
name='editapplicantprofile'),
path('recruiterdashboard/profile-edit/', views.recruiteredit,
name='editrecruiterprofile'),
]
| [((122, 172), 'django.urls.path', 'path', (['"""register/"""', 'views.register'], {'name': '"""register"""'}), "('register/', views.register, name='register')\n", (126, 172), False, 'from django.urls import path\n'), ((178, 223), 'django.urls.path', 'path', (['"""login/"""', 'views.userlogin'], {'name': '"""login"""'}), "('login/', views.userlogin, name='login')\n", (182, 223), False, 'from django.urls import path\n'), ((229, 277), 'django.urls.path', 'path', (['"""logout/"""', 'views.userlogout'], {'name': '"""logout"""'}), "('logout/', views.userlogout, name='logout')\n", (233, 277), False, 'from django.urls import path\n'), ((959, 1044), 'django.urls.path', 'path', (['"""applicantdashboard/"""', 'views.applicantdashboard'], {'name': '"""applicantdashboard"""'}), "('applicantdashboard/', views.applicantdashboard, name='applicantdashboard'\n )\n", (963, 1044), False, 'from django.urls import path\n'), ((1054, 1139), 'django.urls.path', 'path', (['"""recruiterdashboard/"""', 'views.recruiterdashboard'], {'name': '"""recruiterdashboard"""'}), "('recruiterdashboard/', views.recruiterdashboard, name='recruiterdashboard'\n )\n", (1058, 1139), False, 'from django.urls import path\n'), ((1149, 1244), 'django.urls.path', 'path', (['"""applicantdashboard/profile-edit/"""', 'views.applicantedit'], {'name': '"""editapplicantprofile"""'}), "('applicantdashboard/profile-edit/', views.applicantedit, name=\n 'editapplicantprofile')\n", (1153, 1244), False, 'from django.urls import path\n'), ((1254, 1349), 'django.urls.path', 'path', (['"""recruiterdashboard/profile-edit/"""', 'views.recruiteredit'], {'name': '"""editrecruiterprofile"""'}), "('recruiterdashboard/profile-edit/', views.recruiteredit, name=\n 'editrecruiterprofile')\n", (1258, 1349), False, 'from django.urls import path\n'), ((308, 347), 'django.contrib.auth.views.PasswordChangeView.as_view', 'auth_views.PasswordChangeView.as_view', ([], {}), '()\n', (345, 347), True, 'from django.contrib.auth import views as auth_views\n'), ((417, 460), 'django.contrib.auth.views.PasswordChangeDoneView.as_view', 'auth_views.PasswordChangeDoneView.as_view', ([], {}), '()\n', (458, 460), True, 'from django.contrib.auth import views as auth_views\n'), ((529, 567), 'django.contrib.auth.views.PasswordResetView.as_view', 'auth_views.PasswordResetView.as_view', ([], {}), '()\n', (565, 567), True, 'from django.contrib.auth import views as auth_views\n'), ((635, 677), 'django.contrib.auth.views.PasswordResetDoneView.as_view', 'auth_views.PasswordResetDoneView.as_view', ([], {}), '()\n', (675, 677), True, 'from django.contrib.auth import views as auth_views\n'), ((753, 798), 'django.contrib.auth.views.PasswordResetConfirmView.as_view', 'auth_views.PasswordResetConfirmView.as_view', ([], {}), '()\n', (796, 798), True, 'from django.contrib.auth import views as auth_views\n'), ((865, 911), 'django.contrib.auth.views.PasswordResetCompleteView.as_view', 'auth_views.PasswordResetCompleteView.as_view', ([], {}), '()\n', (909, 911), True, 'from django.contrib.auth import views as auth_views\n')] |
nii-gakunin-cloud/ocs-templates | Moodle/scripts/edit_conf.py | a2a39bb8824d489488af3c3972007317bb1ef6a2 | from datetime import datetime
from difflib import unified_diff
from logging import basicConfig, getLogger, INFO
import os
from pathlib import Path
import shutil
import subprocess
import sys
import yaml
from urllib.parse import urlparse
from notebook import notebookapp
from IPython.core.display import HTML
WORKDIR = 'edit'
META_YML = '.vcp-meta.yml'
MOODLE_DIR = '/opt/moodle'
CONF_RELATIVE = '/etc'
ENV_INHERIT = ['VAULT_ADDR', 'VAULT_TOKEN', 'PATH', 'REQUESTS_CA_BUNDLE']
logger = getLogger(__name__)
basicConfig(level=INFO, format='%(message)s')
def generate_local_path(host, conf_path, version=None):
ret = Path(WORKDIR).absolute() / host
if version is None:
ret /= datetime.now().strftime("%Y%m%d%H%M%S%f")
else:
ret /= version
ret /= Path(conf_path).name
return ret
def generate_remote_path(container, conf_path, relative_to=CONF_RELATIVE):
return (Path(MOODLE_DIR) / container / 'conf' /
Path(conf_path).relative_to(relative_to))
def get_local_path(host, container, conf_path, version=None):
if version is None:
version = find_latest_version(host, container, conf_path)
return generate_local_path(host, conf_path, version)
def _match_metainfo(parent, container, conf_path):
p = parent / META_YML
if not p.exists():
return False
with p.open() as f:
params = yaml.safe_load(f)
return (
isinstance(params, dict) and
'container' in params and
'container_path' in params and
params['container'] == container and
params['container_path'] == conf_path)
def _match_metainfo_by_remote_path(parent, remote_path):
p = parent / META_YML
if not p.exists():
return False
with p.open() as f:
params = yaml.safe_load(f)
return (
isinstance(params, dict) and
'remote_path' in params and
params['remote_path'] == remote_path)
def get_versions(host, *args, match=_match_metainfo):
pdir = Path(WORKDIR).absolute() / host
return sorted([
x.name for x in pdir.glob('*')
if x.is_dir() and match(x, *args)])
def find_latest_version(host, container, conf_path):
return get_versions(host, container, conf_path)[-1]
def find_latest_version_by_remote_path(host, remote_path):
return get_versions(
host, remote_path, match=_match_metainfo_by_remote_path)[-1]
def download_file(host, remote_path, conf_path=None):
if conf_path is None:
conf_path = Path(remote_path).name
dest = generate_local_path(host, conf_path)
ansible_arg = f'src={remote_path} dest={dest} flat=yes'
out = subprocess.check_output(
['ansible', host, '-m', 'fetch', '-a', ansible_arg])
host_1 = out.decode('utf-8').split("\n")[0].split()[0]
logger.info(f'Downloading {remote_path} from {host_1} to {dest}')
return dest
def download_conf_file(host, container, conf_path, relative_to=CONF_RELATIVE):
src = generate_remote_path(container, conf_path, relative_to)
return download_file(host, src, conf_path)
def create_conf_file(host, conf_path):
dest = generate_local_path(host, conf_path)
dest.parent.mkdir(parents=True, exist_ok=True)
dest.touch()
return dest
def _to_backup(conf):
return conf.parent / (conf.name + '.orig')
def make_backup(conf, quiet=False):
org = _to_backup(conf)
if not quiet:
logger.info(f'Copy {conf} {org}')
shutil.copy2(conf, org)
def make_metainfo(local_path, container, conf_path, relative_to=CONF_RELATIVE):
params = {
'container': container,
'container_path': conf_path,
'remote_path':
str(generate_remote_path(container, conf_path, relative_to)),
'version': list(local_path.parts)[-2],
}
with (local_path.parent / META_YML).open(mode='w') as f:
yaml.safe_dump(params, stream=f, default_flow_style=False)
def make_simple_metainfo(local_path, remote_path):
params = {
'remote_path': remote_path,
'version': list(local_path.parts)[-2],
}
with (local_path.parent / META_YML).open(mode='w') as f:
yaml.safe_dump(params, stream=f, default_flow_style=False)
def generate_edit_link(conf):
nb_conf = list(notebookapp.list_running_servers())[0]
p = (Path(nb_conf['base_url']) / 'edit' /
conf.absolute().relative_to(nb_conf['notebook_dir']))
return HTML(f'<a href={p} target="_blank">{p.name}</a>')
def show_diff(path_a, path_b):
lines_a = []
lines_b = []
with path_a.open() as f:
lines_a = f.readlines()
with path_b.open() as f:
lines_b = f.readlines()
diff = list(unified_diff(
lines_a, lines_b, fromfile=path_a.name, tofile=path_b.name))
sys.stdout.writelines(diff)
return len(diff)
def upload_conf_file(src, host, container, conf_path,
relative_to=CONF_RELATIVE):
dest = generate_remote_path(container, conf_path, relative_to)
ansible_arg = f'mkdir -p {dest.parent}'
subprocess.run(
['ansible', host, '-a', ansible_arg])
ansible_arg = f'dest={dest} src={src} backup=yes'
out = subprocess.check_output(
['ansible', host, '-m', 'copy', '-b', '-a', ansible_arg])
host_1 = out.decode('utf-8').split("\n")[0].split()[0]
logger.info(f'Uploading {dest} from {src} to {host_1}')
def restart_container(host, container):
cmd = f'chdir={MOODLE_DIR} docker-compose restart {container}'
logger.info(f'Restart container {container}')
subprocess.check_call(['ansible', host, '-a', cmd])
def fetch_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, create=False):
local_path = download_conf_file(host, container, conf_path, relative_to)
make_backup(local_path)
make_metainfo(local_path, container, conf_path, relative_to)
return generate_edit_link(local_path)
def create_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, create=False):
local_path = create_conf_file(host, conf_path)
make_backup(local_path, quiet=True)
make_metainfo(local_path, container, conf_path, relative_to)
return generate_edit_link(local_path)
def apply_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, version=None, restart=True):
diff = show_local_conf_diff(host, container, conf_path, version)
local_path = get_local_path(host, container, conf_path, version)
upload_conf_file(local_path, host, container, conf_path, relative_to)
if restart:
restart_container(host, container)
def revert_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, version=None):
local_path = get_local_path(host, container, conf_path, version)
backup_path = _to_backup(local_path)
show_diff(local_path, backup_path)
upload_conf_file(backup_path, host, container, conf_path, relative_to)
restart_container(host, container)
local_path.rename(local_path.parent / (local_path.name + '.revert'))
def show_local_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, version=None):
conf = get_local_path(host, container, conf_path, version)
with conf.open() as f:
print(f.read())
def edit_local_conf(host, container, conf_path,
relative_to=CONF_RELATIVE, version=None):
conf = get_local_path(host, container, conf_path, version)
return generate_edit_link(conf)
def show_local_conf_diff(host, container, conf_path, version=None):
local_path = get_local_path(host, container, conf_path, version)
show_diff(_to_backup(local_path), local_path)
def save_shibboleth_part(conf_path):
with conf_path.open() as f:
data = yaml.safe_load(f)
params = {}
if 'shibboleth' in data['services']:
params['shibboleth_container'] = yaml.safe_dump(
data['services']['shibboleth'])
vars_path = conf_path.parent / 'extra_vars.yml'
with vars_path.open(mode='w') as f:
yaml.safe_dump(params, f)
return vars_path
def init_shibboleth_part(conf_dir, hostname, volumes):
shibboleth_volumes = ['/sys/fs/cgroup:/sys/fs/cgroup']
shibboleth_volumes.extend(volumes)
params = {
'shibboleth_container': yaml.safe_dump({
'image': 'harbor.vcloud.nii.ac.jp/vcp/moodle:shibboleth-3.0.4',
'privileged': True,
'ports': ['443:443'],
'volumes': shibboleth_volumes,
'container_name': 'shibboleth',
'hostname': hostname,
}),
}
vars_path = conf_dir / 'shibboleth.yml'
with vars_path.open(mode='w') as f:
yaml.safe_dump(params, f)
return vars_path
def setup_shibboleth_part(local_path, **params):
if params is None or len(params) == 0:
return save_shibboleth_part(local_path)
else:
return init_shibboleth_part(local_path.parent, **params)
def generate_docker_compose(host, conf_path, extra_vars, extra_vars_file):
template = 'template/docker/compose/docker-compose.yml'
ansible_arg = f'src={template} dest={conf_path.parent}/'
env = dict([(x, os.environ[x]) for x in ENV_INHERIT])
args = ['ansible', host, '-m', 'template', '-c', 'local',
'-a', ansible_arg]
for k, v in extra_vars.items():
args.extend(['-e', f'{k}={v}'])
for x in extra_vars_file:
args.extend(['-e', f'@{str(x)}'])
subprocess.run(args=args, env=env, check=True)
def update_docker_compose(host, extra_vars={}, shibboleth_params={}):
remote_path = MOODLE_DIR + '/docker-compose.yml'
local_path = download_file(host, remote_path)
make_backup(local_path)
make_simple_metainfo(local_path, remote_path)
shibboleth_vars = setup_shibboleth_part(local_path, **shibboleth_params)
generate_docker_compose(host, local_path, extra_vars, [shibboleth_vars])
show_diff(_to_backup(local_path), local_path)
return generate_edit_link(local_path)
def append_shibboleth_container(host, moodle_url, volumes=[], extra_vars={}):
hostname = urlparse(moodle_url).netloc
return update_docker_compose(
host, extra_vars,
shibboleth_params={'hostname': hostname, 'volumes': volumes},
)
def upload_docker_compose(host, version=None, apply=False):
remote_path = MOODLE_DIR + '/docker-compose.yml'
if version is None:
version = find_latest_version_by_remote_path(host, remote_path)
local_path = (
Path(WORKDIR).absolute() / host / version / 'docker-compose.yml')
ansible_arg = f'dest={remote_path} src={local_path} backup=yes'
out = subprocess.check_output(
['ansible', host, '-m', 'copy', '-b', '-a', ansible_arg])
host_1 = out.decode('utf-8').split("\n")[0].split()[0]
logger.info(f'Uploading {remote_path} from {local_path} to {host_1}')
if not apply:
return
ansible_arg = f'chdir=/opt/moodle docker-compose up -d --remove-orphans'
args = ['ansible', host, '-a', ansible_arg]
logger.info('Apply the changes in docker-compose.yml.')
subprocess.run(args=args, check=True)
def generate_proxy_conf(host, conf_path, extra_vars):
template = 'template/docker/compose/moodle-proxy.conf.template'
ansible_arg = f'src={template} dest={conf_path.parent}/moodle-proxy.conf'
env = dict([(x, os.environ[x]) for x in ENV_INHERIT])
args = [
'ansible', host, '-m', 'template', '-c', 'local', '-a', ansible_arg]
for k, v in extra_vars.items():
args.extend(['-e', f'{k}={v}'])
subprocess.run(args=args, env=env, check=True)
def update_proxy_conf(host, extra_vars={}):
conf_path = Path('/usr/local/apache2/conf/moodle-proxy.conf')
container = 'proxy'
link = fetch_conf(host, container, str(conf_path), str(conf_path.parent))
version = find_latest_version(host, container, str(conf_path))
local_path = generate_local_path(host, conf_path, version)
generate_proxy_conf(host, local_path, extra_vars)
show_local_conf_diff(host, container, conf_path, version)
return link
def apply_proxy_conf(host, version=None, restart=True):
conf_path = Path('/usr/local/apache2/conf/moodle-proxy.conf')
apply_conf(host, 'proxy', str(conf_path), str(conf_path.parent),
version, restart)
| [((487, 506), 'logging.getLogger', 'getLogger', (['__name__'], {}), '(__name__)\n', (496, 506), False, 'from logging import basicConfig, getLogger, INFO\n'), ((507, 552), 'logging.basicConfig', 'basicConfig', ([], {'level': 'INFO', 'format': '"""%(message)s"""'}), "(level=INFO, format='%(message)s')\n", (518, 552), False, 'from logging import basicConfig, getLogger, INFO\n'), ((2636, 2712), 'subprocess.check_output', 'subprocess.check_output', (["['ansible', host, '-m', 'fetch', '-a', ansible_arg]"], {}), "(['ansible', host, '-m', 'fetch', '-a', ansible_arg])\n", (2659, 2712), False, 'import subprocess\n'), ((3434, 3457), 'shutil.copy2', 'shutil.copy2', (['conf', 'org'], {}), '(conf, org)\n', (3446, 3457), False, 'import shutil\n'), ((4397, 4446), 'IPython.core.display.HTML', 'HTML', (['f"""<a href={p} target="_blank">{p.name}</a>"""'], {}), '(f\'<a href={p} target="_blank">{p.name}</a>\')\n', (4401, 4446), False, 'from IPython.core.display import HTML\n'), ((4739, 4766), 'sys.stdout.writelines', 'sys.stdout.writelines', (['diff'], {}), '(diff)\n', (4760, 4766), False, 'import sys\n'), ((5008, 5060), 'subprocess.run', 'subprocess.run', (["['ansible', host, '-a', ansible_arg]"], {}), "(['ansible', host, '-a', ansible_arg])\n", (5022, 5060), False, 'import subprocess\n'), ((5134, 5219), 'subprocess.check_output', 'subprocess.check_output', (["['ansible', host, '-m', 'copy', '-b', '-a', ansible_arg]"], {}), "(['ansible', host, '-m', 'copy', '-b', '-a',\n ansible_arg])\n", (5157, 5219), False, 'import subprocess\n'), ((5507, 5558), 'subprocess.check_call', 'subprocess.check_call', (["['ansible', host, '-a', cmd]"], {}), "(['ansible', host, '-a', cmd])\n", (5528, 5558), False, 'import subprocess\n'), ((9394, 9440), 'subprocess.run', 'subprocess.run', ([], {'args': 'args', 'env': 'env', 'check': '(True)'}), '(args=args, env=env, check=True)\n', (9408, 9440), False, 'import subprocess\n'), ((10582, 10667), 'subprocess.check_output', 'subprocess.check_output', (["['ansible', host, '-m', 'copy', '-b', '-a', ansible_arg]"], {}), "(['ansible', host, '-m', 'copy', '-b', '-a',\n ansible_arg])\n", (10605, 10667), False, 'import subprocess\n'), ((11028, 11065), 'subprocess.run', 'subprocess.run', ([], {'args': 'args', 'check': '(True)'}), '(args=args, check=True)\n', (11042, 11065), False, 'import subprocess\n'), ((11497, 11543), 'subprocess.run', 'subprocess.run', ([], {'args': 'args', 'env': 'env', 'check': '(True)'}), '(args=args, env=env, check=True)\n', (11511, 11543), False, 'import subprocess\n'), ((11606, 11655), 'pathlib.Path', 'Path', (['"""/usr/local/apache2/conf/moodle-proxy.conf"""'], {}), "('/usr/local/apache2/conf/moodle-proxy.conf')\n", (11610, 11655), False, 'from pathlib import Path\n'), ((12094, 12143), 'pathlib.Path', 'Path', (['"""/usr/local/apache2/conf/moodle-proxy.conf"""'], {}), "('/usr/local/apache2/conf/moodle-proxy.conf')\n", (12098, 12143), False, 'from pathlib import Path\n'), ((778, 793), 'pathlib.Path', 'Path', (['conf_path'], {}), '(conf_path)\n', (782, 793), False, 'from pathlib import Path\n'), ((1372, 1389), 'yaml.safe_load', 'yaml.safe_load', (['f'], {}), '(f)\n', (1386, 1389), False, 'import yaml\n'), ((1775, 1792), 'yaml.safe_load', 'yaml.safe_load', (['f'], {}), '(f)\n', (1789, 1792), False, 'import yaml\n'), ((3843, 3901), 'yaml.safe_dump', 'yaml.safe_dump', (['params'], {'stream': 'f', 'default_flow_style': '(False)'}), '(params, stream=f, default_flow_style=False)\n', (3857, 3901), False, 'import yaml\n'), ((4128, 4186), 'yaml.safe_dump', 'yaml.safe_dump', (['params'], {'stream': 'f', 'default_flow_style': '(False)'}), '(params, stream=f, default_flow_style=False)\n', (4142, 4186), False, 'import yaml\n'), ((4652, 4724), 'difflib.unified_diff', 'unified_diff', (['lines_a', 'lines_b'], {'fromfile': 'path_a.name', 'tofile': 'path_b.name'}), '(lines_a, lines_b, fromfile=path_a.name, tofile=path_b.name)\n', (4664, 4724), False, 'from difflib import unified_diff\n'), ((7714, 7731), 'yaml.safe_load', 'yaml.safe_load', (['f'], {}), '(f)\n', (7728, 7731), False, 'import yaml\n'), ((7830, 7876), 'yaml.safe_dump', 'yaml.safe_dump', (["data['services']['shibboleth']"], {}), "(data['services']['shibboleth'])\n", (7844, 7876), False, 'import yaml\n'), ((7990, 8015), 'yaml.safe_dump', 'yaml.safe_dump', (['params', 'f'], {}), '(params, f)\n', (8004, 8015), False, 'import yaml\n'), ((8239, 8459), 'yaml.safe_dump', 'yaml.safe_dump', (["{'image': 'harbor.vcloud.nii.ac.jp/vcp/moodle:shibboleth-3.0.4',\n 'privileged': True, 'ports': ['443:443'], 'volumes': shibboleth_volumes,\n 'container_name': 'shibboleth', 'hostname': hostname}"], {}), "({'image':\n 'harbor.vcloud.nii.ac.jp/vcp/moodle:shibboleth-3.0.4', 'privileged': \n True, 'ports': ['443:443'], 'volumes': shibboleth_volumes,\n 'container_name': 'shibboleth', 'hostname': hostname})\n", (8253, 8459), False, 'import yaml\n'), ((8629, 8654), 'yaml.safe_dump', 'yaml.safe_dump', (['params', 'f'], {}), '(params, f)\n', (8643, 8654), False, 'import yaml\n'), ((10035, 10055), 'urllib.parse.urlparse', 'urlparse', (['moodle_url'], {}), '(moodle_url)\n', (10043, 10055), False, 'from urllib.parse import urlparse\n'), ((2495, 2512), 'pathlib.Path', 'Path', (['remote_path'], {}), '(remote_path)\n', (2499, 2512), False, 'from pathlib import Path\n'), ((4238, 4272), 'notebook.notebookapp.list_running_servers', 'notebookapp.list_running_servers', ([], {}), '()\n', (4270, 4272), False, 'from notebook import notebookapp\n'), ((4286, 4311), 'pathlib.Path', 'Path', (["nb_conf['base_url']"], {}), "(nb_conf['base_url'])\n", (4290, 4311), False, 'from pathlib import Path\n'), ((621, 634), 'pathlib.Path', 'Path', (['WORKDIR'], {}), '(WORKDIR)\n', (625, 634), False, 'from pathlib import Path\n'), ((692, 706), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (704, 706), False, 'from datetime import datetime\n'), ((903, 919), 'pathlib.Path', 'Path', (['MOODLE_DIR'], {}), '(MOODLE_DIR)\n', (907, 919), False, 'from pathlib import Path\n'), ((955, 970), 'pathlib.Path', 'Path', (['conf_path'], {}), '(conf_path)\n', (959, 970), False, 'from pathlib import Path\n'), ((1992, 2005), 'pathlib.Path', 'Path', (['WORKDIR'], {}), '(WORKDIR)\n', (1996, 2005), False, 'from pathlib import Path\n'), ((10437, 10450), 'pathlib.Path', 'Path', (['WORKDIR'], {}), '(WORKDIR)\n', (10441, 10450), False, 'from pathlib import Path\n')] |
newvicklee/nlp_algorithms | other/minimum_edit_distance.py | d2812398d96d345dcb50970bae6ebbf666ea5380 | """
Minimum edit distance computes the cost it takes to get from one string to another string.
This implementation uses the Levenshtein distance with a cost of 1 for insertions or deletions and a cost of 2 for substitutions.
Resource: https://en.wikipedia.org/wiki/Edit_distance
For example, getting from "intention" to "execution" is a cost of 8.
minimum_edit_distance("intention", "execution")
# 8
"""
def minimum_edit_distance(source, target):
n = len(source)
m = len(target)
D = {}
# Initialization
for i in range(0, n+1):
D[i,0] = i
for j in range(0, m+1):
D[0,j] = j
for i in range(1, n+1):
for j in range(1, m+1):
if source[i-1] == target[j-1]:
D[i,j] = D[i-1, j-1]
else:
D[i,j] = min(
D[i-1, j] + 1,
D[i, j-1] + 1,
D[i-1, j-1] + 2
)
return D[n-1, m-1]
| [] |
iqbal-lab-org/varifier | varifier/dnadiff.py | 718a787fd8490ea33a79b5095884e66e12106399 | from operator import attrgetter
import logging
import os
import shutil
import subprocess
import pyfastaq
import pymummer
from cluster_vcf_records import vcf_record
from varifier import utils
# We only want the .snps file from the dnadiff script from MUMmer. From reading
# the docs inspecting that script, we need to run these commands:
#
# nucmer --maxmatch --delta out.delta ref.fasta query.fasta
# delta-filter -1 out.delta > out.1delta
# show-snps -rlTHC out.1delta > out.snps
#
# This is instead of just running show-snps, which runs several other commands
# in addition to making the snps file.
def _run_dnadiff_one_split(ref_fasta, query_fasta, outfile, threads=1, maxmatch=True):
delta = f"{outfile}.tmp.delta"
delta_1 = f"{outfile}.tmp.1delta"
subprocess.check_output(f"rm -f {delta} {delta_1}", shell=True)
maxmatch_opt = "--maxmatch" if maxmatch else ""
commands = [
f"nucmer --threads {threads} {maxmatch_opt} --delta {delta} {ref_fasta} {query_fasta}",
f"delta-filter -1 {delta} > {delta_1}",
f"show-snps -rlTHC {delta_1} > {outfile}",
]
for command in commands:
logging.info("Start run command: " + command)
subprocess.check_output(command, shell=True)
logging.info("Finish run command: " + command)
os.unlink(delta)
os.unlink(delta_1)
def _run_dnadiff(
ref_fasta,
query_fasta,
outfile,
split_query=False,
debug=False,
threads=1,
maxmatch=True,
):
if not split_query:
_run_dnadiff_one_split(
ref_fasta, query_fasta, outfile, threads=threads, maxmatch=maxmatch
)
else:
tmp_snp_files = []
seq_reader = pyfastaq.sequences.file_reader(query_fasta)
for seq in seq_reader:
prefix = f"{outfile}.tmp.split.{len(tmp_snp_files)}"
tmp_fasta = f"{prefix}.fasta"
with open(tmp_fasta, "w") as f:
print(seq, file=f)
snp_file = f"{prefix}.snps"
_run_dnadiff_one_split(
ref_fasta, tmp_fasta, snp_file, threads=threads, maxmatch=maxmatch
)
os.unlink(tmp_fasta)
tmp_snp_files.append(snp_file)
with open(outfile, "wb") as f_out:
for snp_file in tmp_snp_files:
with open(snp_file, "rb") as f_in:
shutil.copyfileobj(f_in, f_out)
if not debug:
os.unlink(snp_file)
def _snps_file_to_vcf(snps_file, query_fasta, outfile):
"""Loads the .snps file made by dnadiff.
query_fasta = fasta file of query sequences.
Writes a new VCF file unmerged records."""
vcf_records = {}
variants = pymummer.snp_file.get_all_variants(snps_file)
query_seqs = utils.file_to_dict_of_seqs(query_fasta)
for variant in variants:
# If the variant is reversed, it means that either the ref or query had to be
# reverse complemented when aligned by mummer. Need to do the appropriate
# reverse (complement) fixes so the VCF has the correct REF and ALT sequences
if variant.reverse:
qry_seq = pyfastaq.sequences.Fasta("x", variant.qry_base)
qry_seq.revcomp()
variant.qry_base = "".join(reversed(qry_seq.seq))
ref_seq = pyfastaq.sequences.Fasta("x", variant.ref_base)
ref_seq.revcomp()
variant.ref_base = ref_seq.seq
if variant.var_type == pymummer.variant.SNP:
new_record = vcf_record.VcfRecord(
"\t".join(
[
variant.qry_name,
str(variant.qry_start + 1),
".",
variant.qry_base,
variant.ref_base,
".",
".",
"SVTYPE=DNADIFF_SNP",
"GT",
"1/1",
]
)
)
elif variant.var_type == pymummer.variant.DEL:
# The query has sequence missing, compared to the
# reference. We're making VCF records w.r.t. the
# query, so this is an insertion. So need to
# get the nucleotide before the insertion as well.
new_record = vcf_record.VcfRecord(
"\t".join(
[
variant.qry_name,
str(variant.qry_start + 1),
".",
query_seqs[variant.qry_name][variant.qry_start],
query_seqs[variant.qry_name][variant.qry_start]
+ variant.ref_base,
".",
".",
"SVTYPE=DNADIFF_INS",
"GT",
"1/1",
]
)
)
elif variant.var_type == pymummer.variant.INS:
# The ref has sequence missing, compared to the
# query. We're making VCF records w.r.t. the
# query, so this is a deletion. So need to
# get the nucleotide before the deletion as well.
new_record = vcf_record.VcfRecord(
"\t".join(
[
variant.qry_name,
str(variant.qry_start),
".",
query_seqs[variant.qry_name][variant.qry_start - 1]
+ variant.qry_base,
query_seqs[variant.qry_name][variant.qry_start - 1],
".",
".",
"SVTYPE=DNADIFF_DEL",
"GT",
"1/1",
]
)
)
else:
raise Exception("Unknown variant type: " + str(variant))
assert (
new_record.REF
== query_seqs[new_record.CHROM][
new_record.POS : new_record.POS + len(new_record.REF)
]
)
if new_record.CHROM not in vcf_records:
vcf_records[new_record.CHROM] = []
vcf_records[new_record.CHROM].append(new_record)
for vcf_list in vcf_records.values():
vcf_list.sort(key=attrgetter("POS"))
with open(outfile, "w") as f:
print("##fileformat=VCFv4.2", file=f)
for seq in query_seqs.values():
print(f"##contig=<ID={seq.id},length={len(seq)}>", file=f)
print("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tsample", file=f)
for key, vcf_list in sorted(vcf_records.items()):
for record in vcf_list:
print(record, file=f)
def make_truth_vcf(
ref_fasta,
truth_fasta,
outfile,
debug=False,
split_ref=False,
threads=1,
maxmatch=True,
):
snps_file = f"{outfile}.tmp.snps"
_run_dnadiff(
truth_fasta,
ref_fasta,
snps_file,
split_query=split_ref,
debug=debug,
threads=threads,
maxmatch=maxmatch,
)
_snps_file_to_vcf(snps_file, ref_fasta, outfile)
if not debug:
os.unlink(snps_file)
| [((768, 831), 'subprocess.check_output', 'subprocess.check_output', (['f"""rm -f {delta} {delta_1}"""'], {'shell': '(True)'}), "(f'rm -f {delta} {delta_1}', shell=True)\n", (791, 831), False, 'import subprocess\n'), ((1299, 1315), 'os.unlink', 'os.unlink', (['delta'], {}), '(delta)\n', (1308, 1315), False, 'import os\n'), ((1320, 1338), 'os.unlink', 'os.unlink', (['delta_1'], {}), '(delta_1)\n', (1329, 1338), False, 'import os\n'), ((2690, 2735), 'pymummer.snp_file.get_all_variants', 'pymummer.snp_file.get_all_variants', (['snps_file'], {}), '(snps_file)\n', (2724, 2735), False, 'import pymummer\n'), ((2753, 2792), 'varifier.utils.file_to_dict_of_seqs', 'utils.file_to_dict_of_seqs', (['query_fasta'], {}), '(query_fasta)\n', (2779, 2792), False, 'from varifier import utils\n'), ((1140, 1185), 'logging.info', 'logging.info', (["('Start run command: ' + command)"], {}), "('Start run command: ' + command)\n", (1152, 1185), False, 'import logging\n'), ((1194, 1238), 'subprocess.check_output', 'subprocess.check_output', (['command'], {'shell': '(True)'}), '(command, shell=True)\n', (1217, 1238), False, 'import subprocess\n'), ((1247, 1293), 'logging.info', 'logging.info', (["('Finish run command: ' + command)"], {}), "('Finish run command: ' + command)\n", (1259, 1293), False, 'import logging\n'), ((1685, 1728), 'pyfastaq.sequences.file_reader', 'pyfastaq.sequences.file_reader', (['query_fasta'], {}), '(query_fasta)\n', (1715, 1728), False, 'import pyfastaq\n'), ((7198, 7218), 'os.unlink', 'os.unlink', (['snps_file'], {}), '(snps_file)\n', (7207, 7218), False, 'import os\n'), ((2131, 2151), 'os.unlink', 'os.unlink', (['tmp_fasta'], {}), '(tmp_fasta)\n', (2140, 2151), False, 'import os\n'), ((3127, 3174), 'pyfastaq.sequences.Fasta', 'pyfastaq.sequences.Fasta', (['"""x"""', 'variant.qry_base'], {}), "('x', variant.qry_base)\n", (3151, 3174), False, 'import pyfastaq\n'), ((3289, 3336), 'pyfastaq.sequences.Fasta', 'pyfastaq.sequences.Fasta', (['"""x"""', 'variant.ref_base'], {}), "('x', variant.ref_base)\n", (3313, 3336), False, 'import pyfastaq\n'), ((6321, 6338), 'operator.attrgetter', 'attrgetter', (['"""POS"""'], {}), "('POS')\n", (6331, 6338), False, 'from operator import attrgetter\n'), ((2353, 2384), 'shutil.copyfileobj', 'shutil.copyfileobj', (['f_in', 'f_out'], {}), '(f_in, f_out)\n', (2371, 2384), False, 'import shutil\n'), ((2435, 2454), 'os.unlink', 'os.unlink', (['snp_file'], {}), '(snp_file)\n', (2444, 2454), False, 'import os\n')] |
sbj-ss/github-watcher | modules/models.py | 7d7c4d2a0a6a014b93a2168dc6e508b2b867a414 | from dataclasses import asdict, dataclass
from typing import Any, Dict, List, Type
@dataclass(frozen=True)
class StatsBaseModel:
"""Base model for various reports"""
@classmethod
def key(cls: Type) -> str:
name = cls.__name__
return name[0].lower() + name[1:]
def to_table(self) -> List[str]:
raise NotImplementedError
def to_dict(self) -> Dict[str, Any]:
return asdict(self)
@dataclass(frozen=True)
class Contributor:
name: str
commit_count: int
@dataclass(frozen=True)
class ContributorStats(StatsBaseModel):
contributors: List[Contributor]
def to_table(self) -> List[str]:
return [
'Most active contributors:',
'-------------------------',
'Name' + (' ' * 20) + 'Commits',
] + [f'{c.name.ljust(24)}{c.commit_count}' for c in self.contributors]
@dataclass(frozen=True)
class PullRequestStats(StatsBaseModel):
open_count: int
closed_count: int
old_count: int
def to_table(self) -> List[str]:
return [
'Pull requests:',
'--------------',
'Open Closed Old',
f'{str(self.open_count).ljust(8)}{str(self.closed_count).ljust(8)}{str(self.old_count).ljust(8)}'
]
@dataclass(frozen=True)
class IssueStats(StatsBaseModel):
open_count: int
closed_count: int
old_count: int
def to_table(self) -> List[str]:
return [
'Issues:',
'-------',
'Open Closed Old',
f'{str(self.open_count).ljust(8)}{str(self.closed_count).ljust(8)}{str(self.old_count).ljust(8)}'
]
| [((86, 108), 'dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (95, 108), False, 'from dataclasses import asdict, dataclass\n'), ((435, 457), 'dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (444, 457), False, 'from dataclasses import asdict, dataclass\n'), ((516, 538), 'dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (525, 538), False, 'from dataclasses import asdict, dataclass\n'), ((879, 901), 'dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (888, 901), False, 'from dataclasses import asdict, dataclass\n'), ((1276, 1298), 'dataclasses.dataclass', 'dataclass', ([], {'frozen': '(True)'}), '(frozen=True)\n', (1285, 1298), False, 'from dataclasses import asdict, dataclass\n'), ((419, 431), 'dataclasses.asdict', 'asdict', (['self'], {}), '(self)\n', (425, 431), False, 'from dataclasses import asdict, dataclass\n')] |
iCHEF/queryfilter | queryfilter/datetimefilter.py | 0ae4faf525e162d2720d328b96fa179d68277f1e | from __future__ import absolute_import
import datetime
from dateutil import parser
import pytz
from .base import FieldFilter, DictFilterMixin, DjangoQueryFilterMixin
from .queryfilter import QueryFilter
WHOLE_DAY = datetime.timedelta(days=1)
ONE_SECOND = datetime.timedelta(seconds=1)
@QueryFilter.register_type_condition('datetime')
class DatetimeRangeFilter(DjangoQueryFilterMixin, DictFilterMixin,
FieldFilter):
@property
def start(self):
return get_start(self.filter_args.get("start"))
@property
def end(self):
end_datetime = get_end(self.filter_args.get("end"))
if not end_datetime:
return None
if _has_no_time_info(end_datetime):
end_datetime = end_datetime + WHOLE_DAY - ONE_SECOND
return end_datetime
def on_dicts(self, dicts):
def in_range(datum):
datetime_string = self.get(datum, self.field_name)
if isinstance(datetime_string, datetime.datetime):
to_compare = datetime_string
else:
to_compare = parse(datetime_string)
if not self.start and not self.end:
return False
if self.start and (to_compare < self.start):
return False
if self.end and (self.end < to_compare):
return False
return True
return list(filter(in_range, dicts))
@property
def query_params(self):
if not any((self.start, self.end)):
return None
query_params = dict()
if self.start:
query_params["{}__gte".format(self.field_name)] = self.start
if self.end:
query_params["{}__lte".format(self.field_name)] = self.end
return query_params
def _do_django_query(self, queryset):
query_params = self.query_params
if query_params:
return queryset.filter(**query_params)
else:
return queryset.none()
min_datetime = datetime.datetime.min.replace(tzinfo=pytz.utc)
max_datetime = datetime.datetime.max.replace(tzinfo=pytz.utc)
def get_start(start_date_str):
if not start_date_str:
return None
return parse(start_date_str)
def get_end(end_date_str):
if not end_date_str:
return None
return parse(end_date_str)
def parse(datetime_string):
return make_time_aware(parser.parse(datetime_string))
def make_time_aware(datetime_data):
if not datetime_data.tzinfo:
datetime_data = datetime_data.replace(tzinfo=pytz.utc)
return datetime_data
def _has_no_time_info(value):
return value.hour == 0 and \
value.minute == 0 and \
value.second == 0 and \
value.microsecond == 0
| [((220, 246), 'datetime.timedelta', 'datetime.timedelta', ([], {'days': '(1)'}), '(days=1)\n', (238, 246), False, 'import datetime\n'), ((260, 289), 'datetime.timedelta', 'datetime.timedelta', ([], {'seconds': '(1)'}), '(seconds=1)\n', (278, 289), False, 'import datetime\n'), ((2032, 2078), 'datetime.datetime.min.replace', 'datetime.datetime.min.replace', ([], {'tzinfo': 'pytz.utc'}), '(tzinfo=pytz.utc)\n', (2061, 2078), False, 'import datetime\n'), ((2094, 2140), 'datetime.datetime.max.replace', 'datetime.datetime.max.replace', ([], {'tzinfo': 'pytz.utc'}), '(tzinfo=pytz.utc)\n', (2123, 2140), False, 'import datetime\n'), ((2416, 2445), 'dateutil.parser.parse', 'parser.parse', (['datetime_string'], {}), '(datetime_string)\n', (2428, 2445), False, 'from dateutil import parser\n')] |
Huawei-Ascend/modelzoo | built-in/TensorFlow/Research/cv/image_classification/Cars_for_TensorFlow/automl/vega/search_space/networks/pytorch/operator/rpn.py | df51ed9c1d6dbde1deef63f2a037a369f8554406 | # -*- coding: utf-8 -*-
# Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the MIT License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# MIT License for more details.
"""Import all torch operators."""
import torch.nn.functional as F
import torch.nn as nn
import torch
from vega.search_space.networks.network_factory import NetworkFactory
from vega.search_space.networks.net_utils import NetTypes
from vega.search_space.networks.pytorch.utils.anchor_utils.anchor_target import AnchorTarget
from vega.search_space.networks.pytorch.utils.bbox_utils.anchor_generator import AnchorGenerator
from vega.core.common.config import Config
from functools import partial
import numpy as np
from six.moves import map, zip
from vega.search_space.networks.pytorch.losses.reduce_loss import weighted_loss
@NetworkFactory.register(NetTypes.Operator)
class RpnClsLossInput(nn.Module):
"""Rpn input."""
def __init__(self):
super(RpnClsLossInput, self).__init__()
def forward(self, x):
"""Get cls score and bbox preds."""
cls_scores = x[0]
bbox_preds = x[1]
return cls_scores, bbox_preds
@NetworkFactory.register(NetTypes.Operator)
class RpnLossInput(nn.Module):
"""Rpn loss input."""
def __init__(self):
super(RpnLossInput, self).__init__()
def forward(self, x):
"""Get cls score."""
cls_scores = x[2][0]
bbox_preds = x[2][1]
gt_bboxes = x[0]['gt_bboxes'].cuda()
img_metas = [x[0]['img_meta']]
gt_bboxes_ignore = x[0]['gt_bboxes_ignore'].cuda()
return cls_scores, bbox_preds, gt_bboxes, img_metas, gt_bboxes_ignore
@NetworkFactory.register(NetTypes.Operator)
class AnchorTargetOp(nn.Module):
"""Anchor Target."""
def __init__(self, target_means=None, target_stds=None, num_classes=2, use_sigmoid_cls=False, cfg=None,
sampling=True):
self.target_means = target_means or (.0, .0, .0, .0)
self.target_stds = target_stds or (1.0, 1.0, 1.0, 1.0)
self.label_channels = num_classes if use_sigmoid_cls else 1
self.cfg = Config({'assigner': {'name': 'MaxIoUAllNegAssigner', 'pos_iou_thr': 0.7,
'neg_iou_thr': tuple([-1, 0.3]), 'min_pos_iou': 0.3, 'ignore_iof_thr': 0.5},
'sampler': {'name': 'RandomSampler', 'num': 256, 'pos_fraction': 0.5, 'neg_pos_ub': -1,
'add_gt_as_proposals': False}, 'allowed_border': 0, 'pos_weight': -1,
'debug': False})
self.sampling = sampling
super(AnchorTargetOp, self).__init__()
def forward(self, x):
"""Create X=(anchor_list,valid_flag_list,gt_bboxes,img_metas,)."""
anchor_list, valid_flag_list, original_anchors, gt_bboxes, img_metas, gt_bboxes_ignore = x
# out=(labels_list, label_weights_list, bbox_targets_list, bbox_weights_list, num_total_pos,num_total_neg).
return AnchorTarget(anchor_list, valid_flag_list, gt_bboxes, img_metas, self.target_means,
self.target_stds,
self.cfg, gt_bboxes_ignore_list=gt_bboxes_ignore,
gt_labels_list=None,
label_channels=self.label_channels,
sampling=self.sampling)
@NetworkFactory.register(NetTypes.Operator)
class Anchors(nn.Module):
"""Get anchors according to feature map sizes."""
def __init__(self, anchor_base_sizes_cfg=None, anchor_scales=None, anchor_ratios=None, anchor_strides=None):
self.anchor_base_sizes_cfg = anchor_base_sizes_cfg
self.anchor_scales = anchor_scales or [8, 16, 32]
self.anchor_ratios = anchor_ratios or [0.5, 1.0, 2.0]
self.anchor_strides = anchor_strides or [4, 8, 16, 32, 64]
self.anchor_base_sizes = list(
self.anchor_strides) if self.anchor_base_sizes_cfg is None else self.anchor_base_sizes_cfg
super(Anchors, self).__init__()
def forward(self, x):
"""Create anchor."""
cls_scores, bbox_preds, gt_bboxes, img_metas, gt_bboxes_ignore = x
featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
anchor_generators = []
for anchor_base in self.anchor_base_sizes:
anchor_generators.append(AnchorGenerator(anchor_base, self.anchor_scales, self.anchor_ratios))
num_imgs = len(img_metas)
num_levels = len(featmap_sizes)
multi_level_anchors = []
for i in range(num_levels):
anchors = anchor_generators[i].grid_anchors(featmap_sizes[i], self.anchor_strides[i])
multi_level_anchors.append(anchors)
anchor_list = [multi_level_anchors for _ in range(num_imgs)]
valid_flag_list = []
for img_id, img_meta in enumerate(img_metas):
multi_level_flags = []
for i in range(num_levels):
anchor_stride = self.anchor_strides[i]
feat_h, feat_w = featmap_sizes[i]
h, w, _ = img_meta['pad_shape']
valid_feat_h = min(int(np.ceil(h / anchor_stride)), feat_h)
valid_feat_w = min(int(np.ceil(w / anchor_stride)), feat_w)
flags = anchor_generators[i].valid_flags((feat_h, feat_w), (valid_feat_h, valid_feat_w))
multi_level_flags.append(flags)
valid_flag_list.append(multi_level_flags)
return anchor_list, valid_flag_list, multi_level_anchors, gt_bboxes, img_metas, gt_bboxes_ignore
def multi_apply(func, *args, **kwargs):
"""Multi apply.
:param func: function
:param args: args of function
:return: result
"""
pfunc = partial(func, **kwargs) if kwargs else func
map_results = map(pfunc, *args)
return tuple(map(list, zip(*map_results)))
@NetworkFactory.register(NetTypes.Operator)
class RpnClsLoss(nn.Module):
"""Rpn Class Loss."""
def __init__(self, out_channels=2):
super(RpnClsLoss, self).__init__()
self.loss_cls = CustomCrossEntropyLoss()
self.loss_bbox = CustomSmoothL1Loss()
self.out_channels = out_channels
def forward(self, x):
"""Get x."""
(cls_score, bbox_pred, labels, label_weights, bbox_targets, bbox_weights, num_total_pos, num_total_neg,
num_total_samples) = x
losses_cls, losses_bbox = multi_apply(self.loss, cls_score, bbox_pred, labels, label_weights, bbox_targets,
bbox_weights, num_total_samples=num_total_samples)
return losses_cls, losses_bbox
def loss(self, cls_score, bbox_pred, labels, label_weights, bbox_targets, bbox_weights, num_total_samples):
"""Get loss."""
labels = labels.reshape(-1)
label_weights = label_weights.reshape(-1)
cls_score = cls_score.permute(0, 2, 3, 1).reshape(-1, self.out_channels)
loss_cls = self.loss_cls(cls_score, labels, label_weights, avg_factor=num_total_samples)
bbox_targets = bbox_targets.reshape(-1, 4)
bbox_weights = bbox_weights.reshape(-1, 4)
bbox_pred = bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
loss_bbox = self.loss_bbox(bbox_pred, bbox_targets, bbox_weights, avg_factor=num_total_samples)
return loss_cls, loss_bbox
@NetworkFactory.register(NetTypes.Operator)
class CustomCrossEntropyLoss(nn.Module):
"""Cross Entropy Loss."""
def __init__(self, use_sigmoid=False, use_mask=False, reduction='mean',
loss_weight=1.0):
"""Init Cross Entropy loss.
:param desc: config dict
"""
super(CustomCrossEntropyLoss, self).__init__()
self.use_sigmoid = use_sigmoid
self.use_mask = use_mask
self.reduction = reduction
self.loss_weight = loss_weight
if self.use_sigmoid:
self.loss_function = binary_cross_entropy
elif self.use_mask:
self.loss_function = mask_cross_entropy
else:
self.loss_function = cross_entropy
def forward(self, cls_score, label, weight, avg_factor, reduction_override=None, **kwargs):
"""Forward compute."""
assert reduction_override in (None, 'none', 'mean', 'sum')
reduction = (
reduction_override if reduction_override else self.reduction)
loss_cls = self.loss_weight * self.loss_function(cls_score, label, weight, reduction=reduction,
avg_factor=avg_factor, **kwargs)
return loss_cls
@NetworkFactory.register(NetTypes.Operator)
class CustomSmoothL1Loss(nn.Module):
"""Smooth L1 Loss."""
def __init__(self, beta=1.0, reduction='mean', loss_weight=1.0):
"""Init smooth l1 loss."""
super(CustomSmoothL1Loss, self).__init__()
self.beta = beta
self.reduction = reduction
self.loss_weight = loss_weight
def forward(self, pred, target, weight=None, avg_factor=None, reduction_override=None, **kwargs):
"""Forward compute.
:param pred: predict
:param target: target
:param weight: weight
:param avg_factor: avg factor
:param reduction_override: reduce override
:return: loss
"""
reduction = (
reduction_override if reduction_override else self.reduction)
if target.numel() > 0:
loss_bbox = self.loss_weight * smooth_l1_loss(
pred,
target,
weight,
beta=self.beta,
reduction=reduction,
avg_factor=avg_factor,
**kwargs)
return loss_bbox
else:
return torch.FloatTensor([0.0]).cuda()
@weighted_loss
def smooth_l1_loss(pred, target, beta=1.0):
"""Smooth l1 loss.
:param pred: predict
:param target: target
:param beta: beta
:return: loss
"""
assert beta > 0
assert pred.size() == target.size() and target.numel() > 0
diff = torch.abs(pred - target)
loss = torch.where(diff < beta, 0.5 * diff * diff / beta, diff - 0.5 * beta)
return loss
def cross_entropy(pred, label, weight=None, reduction='mean', avg_factor=None):
"""Cross entropy losses.
:param pred: predict result
:param label: gt label
:param weight: weight
:param reduction: reduce function
:param avg_factor: avg factor
:return: loss
"""
loss = F.cross_entropy(pred, label, reduction='none')
if weight is not None:
weight = weight.float()
loss = weight_reduce_loss(loss, weight=weight, reduction=reduction, avg_factor=avg_factor)
return loss
def _expand_binary_labels(labels, label_weights, label_channels):
"""Expand binary labels.
:param labels: labels
:param label_weights: label weights
:param label_channels: label channels
:return: binary label and label weights
"""
bin_labels = labels.new_full((labels.size(0), label_channels), 0)
inds = torch.nonzero(labels >= 1).squeeze()
if inds.numel() > 0:
bin_labels[inds, labels[inds] - 1] = 1
if label_weights is None:
bin_label_weights = None
else:
bin_label_weights = label_weights.view(-1, 1).expand(label_weights.size(0), label_channels)
return bin_labels, bin_label_weights
def binary_cross_entropy(pred, label, weight=None, reduction='mean', avg_factor=None):
"""Binary cross entropy loss.
:param pred: predict result
:param label: gt label
:param weight: weight
:param reduction: reduce function
:param avg_factor: avg factor
:return: loss
"""
if pred.dim() != label.dim():
label, weight = _expand_binary_labels(label, weight, pred.size(-1))
if weight is not None:
weight = weight.float()
loss = F.binary_cross_entropy_with_logits(
pred, label.float(), weight, reduction='none')
loss = weight_reduce_loss(loss, reduction=reduction, avg_factor=avg_factor)
return loss
def mask_cross_entropy(pred, target, label, reduction='mean', avg_factor=None):
"""Mask cross entropy loss.
:param pred: predict result
:param target: target
:param label: gt label
:param reduction: reduce function
:param avg_factor: avg factor
:return: loss
"""
assert reduction == 'mean' and avg_factor is None
num_rois = pred.size()[0]
inds = torch.arange(0, num_rois, dtype=torch.long, device=pred.device)
pred_slice = pred[inds, label].squeeze(1)
return F.binary_cross_entropy_with_logits(pred_slice, target, reduction='mean')[None]
def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None):
"""Weight reduce loss.
:param loss: losses
:param weight: weight
:param reduction: reduce function
:param avg_factor: avg factor
:return: loss
"""
if weight is not None:
loss = loss * weight
if avg_factor is None:
loss = reduce_loss(loss, reduction)
else:
if reduction == 'mean':
loss = loss.sum() / avg_factor
elif reduction != 'none':
raise ValueError('avg_factor can not be used with reduction="sum"')
return loss
def reduce_loss(loss, reduction):
"""Reduce loss compute.
:param loss: losses
:param reduction: reduce funtion
:return: loss
"""
reduction_function = F._Reduction.get_enum(reduction)
if reduction_function == 0:
return loss
elif reduction_function == 1:
return loss.mean()
elif reduction_function == 2:
return loss.sum()
| [((1062, 1104), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (1085, 1104), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((1397, 1439), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (1420, 1439), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((1905, 1947), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (1928, 1947), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((3606, 3648), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (3629, 3648), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((6089, 6131), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (6112, 6131), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((7561, 7603), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (7584, 7603), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((8806, 8848), 'vega.search_space.networks.network_factory.NetworkFactory.register', 'NetworkFactory.register', (['NetTypes.Operator'], {}), '(NetTypes.Operator)\n', (8829, 8848), False, 'from vega.search_space.networks.network_factory import NetworkFactory\n'), ((6021, 6038), 'six.moves.map', 'map', (['pfunc', '*args'], {}), '(pfunc, *args)\n', (6024, 6038), False, 'from six.moves import map, zip\n'), ((10273, 10297), 'torch.abs', 'torch.abs', (['(pred - target)'], {}), '(pred - target)\n', (10282, 10297), False, 'import torch\n'), ((10309, 10378), 'torch.where', 'torch.where', (['(diff < beta)', '(0.5 * diff * diff / beta)', '(diff - 0.5 * beta)'], {}), '(diff < beta, 0.5 * diff * diff / beta, diff - 0.5 * beta)\n', (10320, 10378), False, 'import torch\n'), ((10701, 10747), 'torch.nn.functional.cross_entropy', 'F.cross_entropy', (['pred', 'label'], {'reduction': '"""none"""'}), "(pred, label, reduction='none')\n", (10716, 10747), True, 'import torch.nn.functional as F\n'), ((12648, 12711), 'torch.arange', 'torch.arange', (['(0)', 'num_rois'], {'dtype': 'torch.long', 'device': 'pred.device'}), '(0, num_rois, dtype=torch.long, device=pred.device)\n', (12660, 12711), False, 'import torch\n'), ((13623, 13655), 'torch.nn.functional._Reduction.get_enum', 'F._Reduction.get_enum', (['reduction'], {}), '(reduction)\n', (13644, 13655), True, 'import torch.nn.functional as F\n'), ((3230, 3477), 'vega.search_space.networks.pytorch.utils.anchor_utils.anchor_target.AnchorTarget', 'AnchorTarget', (['anchor_list', 'valid_flag_list', 'gt_bboxes', 'img_metas', 'self.target_means', 'self.target_stds', 'self.cfg'], {'gt_bboxes_ignore_list': 'gt_bboxes_ignore', 'gt_labels_list': 'None', 'label_channels': 'self.label_channels', 'sampling': 'self.sampling'}), '(anchor_list, valid_flag_list, gt_bboxes, img_metas, self.\n target_means, self.target_stds, self.cfg, gt_bboxes_ignore_list=\n gt_bboxes_ignore, gt_labels_list=None, label_channels=self.\n label_channels, sampling=self.sampling)\n', (3242, 3477), False, 'from vega.search_space.networks.pytorch.utils.anchor_utils.anchor_target import AnchorTarget\n'), ((5959, 5982), 'functools.partial', 'partial', (['func'], {}), '(func, **kwargs)\n', (5966, 5982), False, 'from functools import partial\n'), ((12769, 12841), 'torch.nn.functional.binary_cross_entropy_with_logits', 'F.binary_cross_entropy_with_logits', (['pred_slice', 'target'], {'reduction': '"""mean"""'}), "(pred_slice, target, reduction='mean')\n", (12803, 12841), True, 'import torch.nn.functional as F\n'), ((6066, 6083), 'six.moves.zip', 'zip', (['*map_results'], {}), '(*map_results)\n', (6069, 6083), False, 'from six.moves import map, zip\n'), ((11257, 11283), 'torch.nonzero', 'torch.nonzero', (['(labels >= 1)'], {}), '(labels >= 1)\n', (11270, 11283), False, 'import torch\n'), ((4593, 4661), 'vega.search_space.networks.pytorch.utils.bbox_utils.anchor_generator.AnchorGenerator', 'AnchorGenerator', (['anchor_base', 'self.anchor_scales', 'self.anchor_ratios'], {}), '(anchor_base, self.anchor_scales, self.anchor_ratios)\n', (4608, 4661), False, 'from vega.search_space.networks.pytorch.utils.bbox_utils.anchor_generator import AnchorGenerator\n'), ((9963, 9987), 'torch.FloatTensor', 'torch.FloatTensor', (['[0.0]'], {}), '([0.0])\n', (9980, 9987), False, 'import torch\n'), ((5371, 5397), 'numpy.ceil', 'np.ceil', (['(h / anchor_stride)'], {}), '(h / anchor_stride)\n', (5378, 5397), True, 'import numpy as np\n'), ((5447, 5473), 'numpy.ceil', 'np.ceil', (['(w / anchor_stride)'], {}), '(w / anchor_stride)\n', (5454, 5473), True, 'import numpy as np\n')] |
joybhallaa/scrapy | scrapy/http/request/__init__.py | e4750f2fbdacbeb7a20ae7c6b13bba3fb0f7ad54 | """
This module implements the Request class which is used to represent HTTP
requests in Scrapy.
See documentation in docs/topics/request-response.rst
"""
from w3lib.url import safe_url_string
from scrapy.http.headers import Headers
from scrapy.utils.python import to_bytes
from scrapy.utils.trackref import object_ref
from scrapy.utils.url import escape_ajax
from scrapy.http.common import obsolete_setter
from scrapy.utils.curl import curl_to_request_kwargs
class Request(object_ref):
def __init__(self, url, callback=None, method='GET', headers=None, body=None,
cookies=None, meta=None, encoding='utf-8', priority=0,
dont_filter=False, errback=None, flags=None, cb_kwargs=None):
self._encoding = encoding # this one has to be set first
self.method = str(method).upper()
self._set_url(url)
self._set_body(body)
assert isinstance(priority, int), "Request priority not an integer: %r" % priority
self.priority = priority
if callback is not None and not callable(callback):
raise TypeError('callback must be a callable, got %s' % type(callback).__name__)
if errback is not None and not callable(errback):
raise TypeError('errback must be a callable, got %s' % type(errback).__name__)
self.callback = callback
self.errback = errback
self.cookies = cookies or {}
self.headers = Headers(headers or {}, encoding=encoding)
self.dont_filter = dont_filter
self._meta = dict(meta) if meta else None
self._cb_kwargs = dict(cb_kwargs) if cb_kwargs else None
self.flags = [] if flags is None else list(flags)
@property
def cb_kwargs(self):
if self._cb_kwargs is None:
self._cb_kwargs = {}
return self._cb_kwargs
@property
def meta(self):
if self._meta is None:
self._meta = {}
return self._meta
def _get_url(self):
return self._url
def _set_url(self, url):
if not isinstance(url, str):
raise TypeError('Request url must be str or unicode, got %s:' % type(url).__name__)
s = safe_url_string(url, self.encoding)
self._url = escape_ajax(s)
if ('://' not in self._url) and (not self._url.startswith('data:')):
raise ValueError('Missing scheme in request url: %s' % self._url)
url = property(_get_url, obsolete_setter(_set_url, 'url'))
def _get_body(self):
return self._body
def _set_body(self, body):
if body is None:
self._body = b''
else:
self._body = to_bytes(body, self.encoding)
body = property(_get_body, obsolete_setter(_set_body, 'body'))
@property
def encoding(self):
return self._encoding
def __str__(self):
return "<%s %s>" % (self.method, self.url)
__repr__ = __str__
def copy(self):
"""Return a copy of this Request"""
return self.replace()
def replace(self, *args, **kwargs):
"""Create a new Request with the same attributes except for those
given new values.
"""
for x in ['url', 'method', 'headers', 'body', 'cookies', 'meta', 'flags',
'encoding', 'priority', 'dont_filter', 'callback', 'errback', 'cb_kwargs']:
kwargs.setdefault(x, getattr(self, x))
cls = kwargs.pop('cls', self.__class__)
return cls(*args, **kwargs)
@classmethod
def from_curl(cls, curl_command, ignore_unknown_options=True, **kwargs):
"""Create a Request object from a string containing a `cURL
<https://curl.haxx.se/>`_ command. It populates the HTTP method, the
URL, the headers, the cookies and the body. It accepts the same
arguments as the :class:`Request` class, taking preference and
overriding the values of the same arguments contained in the cURL
command.
Unrecognized options are ignored by default. To raise an error when
finding unknown options call this method by passing
``ignore_unknown_options=False``.
.. caution:: Using :meth:`from_curl` from :class:`~scrapy.http.Request`
subclasses, such as :class:`~scrapy.http.JSONRequest`, or
:class:`~scrapy.http.XmlRpcRequest`, as well as having
:ref:`downloader middlewares <topics-downloader-middleware>`
and
:ref:`spider middlewares <topics-spider-middleware>`
enabled, such as
:class:`~scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware`,
:class:`~scrapy.downloadermiddlewares.useragent.UserAgentMiddleware`,
or
:class:`~scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware`,
may modify the :class:`~scrapy.http.Request` object.
To translate a cURL command into a Scrapy request,
you may use `curl2scrapy <https://michael-shub.github.io/curl2scrapy/>`_.
"""
request_kwargs = curl_to_request_kwargs(curl_command, ignore_unknown_options)
request_kwargs.update(kwargs)
return cls(**request_kwargs)
| [((1442, 1483), 'scrapy.http.headers.Headers', 'Headers', (['(headers or {})'], {'encoding': 'encoding'}), '(headers or {}, encoding=encoding)\n', (1449, 1483), False, 'from scrapy.http.headers import Headers\n'), ((2183, 2218), 'w3lib.url.safe_url_string', 'safe_url_string', (['url', 'self.encoding'], {}), '(url, self.encoding)\n', (2198, 2218), False, 'from w3lib.url import safe_url_string\n'), ((2239, 2253), 'scrapy.utils.url.escape_ajax', 'escape_ajax', (['s'], {}), '(s)\n', (2250, 2253), False, 'from scrapy.utils.url import escape_ajax\n'), ((2440, 2472), 'scrapy.http.common.obsolete_setter', 'obsolete_setter', (['_set_url', '"""url"""'], {}), "(_set_url, 'url')\n", (2455, 2472), False, 'from scrapy.http.common import obsolete_setter\n'), ((2713, 2747), 'scrapy.http.common.obsolete_setter', 'obsolete_setter', (['_set_body', '"""body"""'], {}), "(_set_body, 'body')\n", (2728, 2747), False, 'from scrapy.http.common import obsolete_setter\n'), ((5156, 5216), 'scrapy.utils.curl.curl_to_request_kwargs', 'curl_to_request_kwargs', (['curl_command', 'ignore_unknown_options'], {}), '(curl_command, ignore_unknown_options)\n', (5178, 5216), False, 'from scrapy.utils.curl import curl_to_request_kwargs\n'), ((2651, 2680), 'scrapy.utils.python.to_bytes', 'to_bytes', (['body', 'self.encoding'], {}), '(body, self.encoding)\n', (2659, 2680), False, 'from scrapy.utils.python import to_bytes\n')] |
akaeme/BlackJackBot | game.py | 04970107202a24059f8da933233fba7df9f3a0ef | #encoding: utf8
__author__ = 'Diogo Gomes'
__email__ = 'dgomes@ua.pt'
__license__ = "GPL"
__version__ = "0.1"
import copy
import card
from shoe import Shoe
from dealer import Dealer
from player import Player
BET_MULTIPLIER = 2
class Game(object):
class Rules():
def __init__(self, shoe_size=4, min_bet=1, max_bet=10):
self.shoe_size = shoe_size
self.min_bet = min_bet
self.max_bet = max_bet
self.bet_multiplier = BET_MULTIPLIER
def __str__(self):
return "RULES\tMin bet: {}, Max bet: {}, Shoe size: {}, Bet multiplier: {}".format(self.min_bet, self.max_bet, self.shoe_size, self.bet_multiplier)
class PlayerState():
def __init__(self, p):
self.player = p
self.bet = 0
self.hand = []
self.bust = False
self.done = False
self.watch = False
def copy(self):
return copy.deepcopy(self)
def __str__(self):
if isinstance(self.player, Dealer):
return "{}".format(self.hand)
return "{} ({}€)".format(self.hand, self.bet)
def __repr__(self):
return "{}".format(self.player.name)
def hide_card(self):
h = self.copy()
h.hand = h.hand[1:]
return h
def want_to_play(self, rules):
return self.player.want_to_play(rules)
def take_bet(self, state, rules):
bet = 0
while (bet!=self.bet and self.bet!=0) or not (rules.min_bet <= bet <= rules.max_bet) : #bets can't be 0 and double down means double down
bet = self.player.bet(state[0].hide_card(), state[1:])
self.bet += bet
def __init__(self, players, shoe_size=4, debug=False, verbose=True, min_bet=1, max_bet=10, shoe=None):
if verbose:
# print(chr(27) + "[2J")
print("-"*80)
self.verbose = verbose
self.debug = debug
self.rules = self.Rules(shoe_size=shoe_size, min_bet=min_bet, max_bet=max_bet)
self.shoe = Shoe(shoe_size)
if shoe != None:
self.shoe = shoe
self.shoe.shuffle()
self.state = [self.PlayerState(Dealer())] + [self.PlayerState(p) for p in players]
self.done = False
def str_players_hands(self):
o = ""
for p in self.state[1:]:
o+="{!s:^45}".format(p)
return o
def str_players_names(self):
o = ""
for p in self.state[1:]:
o+="{!s:^35}".format(p.player)
return o
def __str__(self):
return (\
"{:^30}\n"\
"╔"+"═══════════════════════════════"*(len(self.state)-1)+"╗\n"\
"{!s:^45}\n"\
" \n"\
" \n"\
" \n"\
" \n"\
" \n"\
"{!s}\n"\
"╚"+"═══════════════════════════════"*(len(self.state)-1)+"╝\n"\
"{}\n"\
).format(self.state[0].player.name, self.state[0].hand if self.done else (["**"]+self.state[0].hide_card().hand if len(self.state[0].hand) else []), self.str_players_hands(), self.str_players_names())
def deal(self, num):
return self.shoe.deal_cards(1)
def take_bets(self):
if self.debug:
print(self)
for p in self.state[1:]:
if p.want_to_play(self.rules):
p.take_bet(self.state, self.rules)
else:
p.watch = True
def loop(self):
#deal initial cards
self.state[0].hand += self.shoe.deal_cards(2)
for p in self.state[1:]:
if not p.watch:
p.hand += self.shoe.deal_cards(2)
turn = 0
if card.blackjack(self.state[0].hand): #if the dealer has blackjack there is no point in playing...
self.done = True
return [p for p in self.state[1:] if card.blackjack(p.hand)]
#lets play
while not self.done:
turn += 1
hits = 0
for p in self.state[::-1]:
if p.watch or p.bust or p.done or card.value(p.hand) == 21: #skip players watching, bust players, players who have double down and players who already have blackjack!
continue
if self.debug:
print("TURN {}: {}".format(turn, p.player.name))
print(self)
action = ""
while action not in ["h", "s", "d", "u"]:
if isinstance(p.player, Dealer):
action = p.player.play(self.state[0], self.state[1:])
else:
action = p.player.play(self.state[0].hide_card(), self.state[1:])
if action == "d" and turn != 1:
print("YOU CAN'T DOUBLE DOWN!!! double down is only available on the 1st turn")
action = ""
if action == "u":
p.watch = True
continue
if action == "d":
p.take_bet(self.state,self.rules)
p.done = True
if action in ["h", "d"]:
p.hand+=self.deal(1)
hits +=1
if card.value(p.hand) >= 21:
if card.value(p.hand) > 21:
p.bust = True
else:
p.done = True #already has blackjack
if isinstance(p.player, Dealer):
self.done = True #game is over we already have a blackjack
if hits == 0:
self.done = True
self.done = True
return [p for p in self.state if
not isinstance(p.player, Dealer) and #Dealer is not really a winner
not card.blackjack(self.state[0].hand) and #If dealer gets blackjack no one wins
not p.watch and #players watching can't win :)
not p.bust and #bust players can't win :)
(card.value(p.hand) >= card.value(self.state[0].hand) or self.state[0].bust) #winners have more points then the dealer or the dealer has gone bust
]
def show_table(self):
for p in self.state[1:]:
p.player.show(self.state)
def payback(self, winners):
for p in self.state[1:]:
if p.watch:
#check if player surrendered
if p.bet > 0:
p.player.payback(-p.bet//2) #this means the player lost half his bet
#skip watchers
continue
if p in winners and card.value(self.state[0].hand) == card.value(p.hand):
p.player.payback(0) #bet is returned
elif p in winners:
p.player.payback(-p.bet + p.bet*BET_MULTIPLIER)
else:
p.player.payback(-p.bet) #this means the player lost
def run(self):
self.take_bets()
winners = self.loop()
self.show_table()
self.payback(winners)
if self.verbose:
print(self)
print("🏆 Winners: "+str(winners))
| [((2094, 2109), 'shoe.Shoe', 'Shoe', (['shoe_size'], {}), '(shoe_size)\n', (2098, 2109), False, 'from shoe import Shoe\n'), ((3813, 3847), 'card.blackjack', 'card.blackjack', (['self.state[0].hand'], {}), '(self.state[0].hand)\n', (3827, 3847), False, 'import card\n'), ((948, 967), 'copy.deepcopy', 'copy.deepcopy', (['self'], {}), '(self)\n', (961, 967), False, 'import copy\n'), ((2231, 2239), 'dealer.Dealer', 'Dealer', ([], {}), '()\n', (2237, 2239), False, 'from dealer import Dealer\n'), ((3989, 4011), 'card.blackjack', 'card.blackjack', (['p.hand'], {}), '(p.hand)\n', (4003, 4011), False, 'import card\n'), ((5369, 5387), 'card.value', 'card.value', (['p.hand'], {}), '(p.hand)\n', (5379, 5387), False, 'import card\n'), ((6741, 6771), 'card.value', 'card.value', (['self.state[0].hand'], {}), '(self.state[0].hand)\n', (6751, 6771), False, 'import card\n'), ((6775, 6793), 'card.value', 'card.value', (['p.hand'], {}), '(p.hand)\n', (6785, 6793), False, 'import card\n'), ((4194, 4212), 'card.value', 'card.value', (['p.hand'], {}), '(p.hand)\n', (4204, 4212), False, 'import card\n'), ((5418, 5436), 'card.value', 'card.value', (['p.hand'], {}), '(p.hand)\n', (5428, 5436), False, 'import card\n'), ((5932, 5966), 'card.blackjack', 'card.blackjack', (['self.state[0].hand'], {}), '(self.state[0].hand)\n', (5946, 5966), False, 'import card\n'), ((6138, 6156), 'card.value', 'card.value', (['p.hand'], {}), '(p.hand)\n', (6148, 6156), False, 'import card\n'), ((6160, 6190), 'card.value', 'card.value', (['self.state[0].hand'], {}), '(self.state[0].hand)\n', (6170, 6190), False, 'import card\n')] |
camipozas/python-exercises | loops/for/for3.py | c8c02d2b9ff77f21592c99038e10434aba08dbc7 | # Escribir un programa que muestre la sumatoria de todos los múltiplos de 7 encontrados entre el 0 y el 100.
# Summing all the multiples of 7 from 0 to 100.
total = 0
for i in range(101):
if i % 7 == 0:
total = total+i
print("Sumatoria de los múltiplos de 7:", total)
| [] |
Saftwerk/TWCManager | lib/TWCManager/Status/HASSStatus.py | 9b17c063ada80fc159db82fe6e3ad8c4ca071a1a | # HomeAssistant Status Output
# Publishes the provided sensor key and value pair to a HomeAssistant instance
import logging
import time
from ww import f
logger = logging.getLogger(__name__.rsplit(".")[-1])
class HASSStatus:
import threading
import requests
apiKey = None
config = None
configConfig = None
configHASS = None
master = None
msgRateInSeconds = 60
resendRateInSeconds = 3600
retryRateInSeconds = 60
msgQueue = {}
status = False
serverIP = None
serverPort = 8123
useHttps = False
timeout = 2
backgroundTasksLock = threading.Lock()
backgroundTasksThread = None
def __init__(self, master):
self.config = master.config
self.master = master
try:
self.configConfig = self.config["config"]
except KeyError:
self.configConfig = {}
try:
self.configHASS = self.config["status"]["HASS"]
except KeyError:
self.configHASS = {}
self.status = self.configHASS.get("enabled", False)
self.serverIP = self.configHASS.get("serverIP", None)
self.serverPort = self.configHASS.get("serverPort", 8123)
self.useHttps = self.configHASS.get("useHttps", False)
self.apiKey = self.configHASS.get("apiKey", None)
self.msgRateInSeconds = self.configHASS.get("msgRateInSeconds", 60)
self.resendRateInSeconds = self.configHASS.get("resendRateInSeconds", 3600)
self.retryRateInSeconds = self.configHASS.get("retryRateInSeconds", 60)
# Unload if this module is disabled or misconfigured
if (
(not self.status)
or (not self.serverIP)
or (int(self.serverPort) < 1)
or (not self.apiKey)
):
self.master.releaseModule("lib.TWCManager.Status", "HASSStatus")
else:
self.backgroundTasksThread = self.threading.Thread(
target=self.background_task_thread, args=()
)
self.backgroundTasksThread.daemon = True
self.backgroundTasksThread.start()
def getTwident(self, twcid):
# Format TWCID nicely
if len(twcid) == 2:
return "%02X%02X" % (twcid[0], twcid[1])
else:
return str(twcid.decode("utf-8"))
def background_task_thread(self):
while True:
time.sleep(self.msgRateInSeconds)
self.backgroundTasksLock.acquire()
for msgKey in self.msgQueue:
msg = self.msgQueue[msgKey]
if msg.elapsingTime < time.time():
self.sendingStatusToHASS(msg)
self.backgroundTasksLock.release()
def getSensorName(self, twcid, key_underscore):
return "sensor.twcmanager_" + str(self.getTwident(twcid)) + "_" + key_underscore
def setStatus(self, twcid, key_underscore, key_camelcase, value, unit):
self.backgroundTasksLock.acquire()
sensor = self.getSensorName(twcid, key_underscore)
if (sensor not in self.msgQueue) or (self.msgQueue[sensor].value != value):
self.msgQueue[sensor] = HASSMessage(
time.time(),
sensor,
twcid,
key_underscore,
key_camelcase,
value,
unit,
)
self.backgroundTasksLock.release()
def sendingStatusToHASS(self, msg):
http = "http://" if not (self.useHttps) else "https://"
url = http + self.serverIP + ":" + self.serverPort
url = url + "/api/states/" + msg.sensor
headers = {
"Authorization": "Bearer " + self.apiKey,
"content-type": "application/json",
}
try:
logger.log(
logging.INFO8,
f(
"Sending POST request to HomeAssistant for sensor {msg.sensor} (value {msg.value})."
),
)
devclass = ""
if str.upper(msg.unit) in ["W", "A", "V", "KWH"]:
devclass = "power"
if len(msg.unit) > 0:
self.requests.post(
url,
json={
"state": msg.value,
"attributes": {
"unit_of_measurement": msg.unit,
"device_class": devclass,
"friendly_name": "TWC "
+ str(self.getTwident(msg.twcid))
+ " "
+ msg.key_camelcase,
},
},
timeout=self.timeout,
headers=headers,
)
else:
self.requests.post(
url,
json={
"state": msg.value,
"attributes": {
"friendly_name": "TWC "
+ str(self.getTwident(msg.twcid))
+ " "
+ msg.key_camelcase
},
},
timeout=self.timeout,
headers=headers,
)
# Setting elapsing time to now + resendRateInSeconds
self.msgQueue[msg.sensor].elapsingTime = (
time.time() + self.resendRateInSeconds
)
except self.requests.exceptions.ConnectionError as e:
logger.log(
logging.INFO4,
"Error connecting to HomeAssistant to publish sensor values",
)
logger.debug(str(e))
self.settingRetryRate(msg)
return False
except self.requests.exceptions.ReadTimeout as e:
logger.log(
logging.INFO4,
"Error connecting to HomeAssistant to publish sensor values",
)
logger.debug(str(e))
self.settingRetryRate(msg)
return False
except Exception as e:
logger.log(
logging.INFO4, "Error during publishing HomeAssistant sensor values"
)
logger.debug(str(e))
self.settingRetryRate(msg)
return False
def settingRetryRate(self, msg):
# Setting elapsing time to now + retryRateInSeconds
self.msgQueue[msg.sensor].elapsingTime = (
time.time() + self.retryRateInSeconds
)
class HASSMessage:
elapsingTime = 0
sensor = ""
twcid = ""
key_underscore = ""
key_camelcase = ""
value = None
unit = ""
def __init__(
self, elapsingTime, sensor, twcid, key_underscore, key_camelcase, value, unit
):
self.elapsingTime = elapsingTime
self.sensor = sensor
self.twcid = twcid
self.key_underscore = key_underscore
self.key_camelcase = key_camelcase
self.value = value
self.unit = unit
| [((599, 615), 'threading.Lock', 'threading.Lock', ([], {}), '()\n', (613, 615), False, 'import threading\n'), ((2385, 2418), 'time.sleep', 'time.sleep', (['self.msgRateInSeconds'], {}), '(self.msgRateInSeconds)\n', (2395, 2418), False, 'import time\n'), ((6495, 6506), 'time.time', 'time.time', ([], {}), '()\n', (6504, 6506), False, 'import time\n'), ((3169, 3180), 'time.time', 'time.time', ([], {}), '()\n', (3178, 3180), False, 'import time\n'), ((3822, 3914), 'ww.f', 'f', (['"""Sending POST request to HomeAssistant for sensor {msg.sensor} (value {msg.value})."""'], {}), "('Sending POST request to HomeAssistant for sensor {msg.sensor} (value {msg.value}).'\n )\n", (3823, 3914), False, 'from ww import f\n'), ((5422, 5433), 'time.time', 'time.time', ([], {}), '()\n', (5431, 5433), False, 'import time\n'), ((2589, 2600), 'time.time', 'time.time', ([], {}), '()\n', (2598, 2600), False, 'import time\n')] |
taycurran/TwitOff | Archive/routes/home_routes.py | 6e2ee13f83fa86c80988a91b3b41ed0958688c3c |
from flask import Blueprint, jsonify, request, render_template
home_routes = Blueprint("home_routes", __name__)
@home_routes.route("/")
def index():
users = User.query.all()
return render_template('base.html', title='Home',
users=users)
@home_routes.route("/about")
def about():
return "About Me"
@home_routes.route('/reset')
def reset():
DB.drop_all()
DB.create_all()
return render_template('base.html', title='Reset', users=[])
# # Add config for database
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
# # stop tracking modifications on sqlalchemy config
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# # ? app.config["TWITTER_API_CLIENT"] = twitter
# # Have the database know about the app
# DB.init_app(app) | [((79, 113), 'flask.Blueprint', 'Blueprint', (['"""home_routes"""', '__name__'], {}), "('home_routes', __name__)\n", (88, 113), False, 'from flask import Blueprint, jsonify, request, render_template\n'), ((197, 252), 'flask.render_template', 'render_template', (['"""base.html"""'], {'title': '"""Home"""', 'users': 'users'}), "('base.html', title='Home', users=users)\n", (212, 252), False, 'from flask import Blueprint, jsonify, request, render_template\n'), ((442, 495), 'flask.render_template', 'render_template', (['"""base.html"""'], {'title': '"""Reset"""', 'users': '[]'}), "('base.html', title='Reset', users=[])\n", (457, 495), False, 'from flask import Blueprint, jsonify, request, render_template\n')] |
rgeorgi/intent | intent/scripts/classification/ctn_to_classifier.py | 9920798c126f6d354029f7bb0a345e7cdb649f3a | from argparse import ArgumentParser
from collections import defaultdict
import glob
import os
import pickle
from random import shuffle, seed
import sys
from tempfile import mkdtemp
import shutil
import logging
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
CTN_LOG = logging.getLogger('CTN_CLASS')
CTN_LOG.setLevel(logging.DEBUG)
logging.basicConfig()
from intent.igt.metadata import set_intent_method, get_intent_method
from intent.interfaces.stanford_tagger import StanfordPOSTagger
from intent.pos.TagMap import TagMap
from intent.utils.env import tagger_model, proj_root
from xigt.codecs import xigtxml
from xigt.consts import ALIGNMENT
from intent.eval.pos_eval import poseval
from intent.igt.consts import GLOSS_WORD_ID, POS_TIER_TYPE, LANG_WORD_ID, GLOSS_WORD_TYPE, POS_TIER_ID, \
INTENT_TOKEN_TYPE, INTENT_POS_PROJ, LANG_WORD_TYPE, TRANS_WORD_TYPE, TRANS_WORD_ID, MANUAL_POS, INTENT_POS_CLASS
from intent.igt.rgxigt import RGCorpus, strip_pos, RGIgt, RGTokenTier, RGTier, gen_tier_id, RGToken, \
ProjectionTransGlossException, word_align
from intent.interfaces.mallet_maxent import MalletMaxent
from intent.scripts.classification.xigt_to_classifier import instances_to_classifier
from intent.utils.token import POSToken, GoldTagPOSToken
from intent.igt.igtutils import rgp
__author__ = 'rgeorgi'
"""
The purpose of this module is to evaluate the POS-line classifiers trained on
"""
def eval_classifier(c, inst_list, context_feats=False, posdict=None):
"""
:param c: The classifier
:param inst_list: A list of Igt instances to test against. Must already have POS tags.
"""
gold_sents = []
eval_sents = []
to_dump = RGCorpus()
for inst in inst_list:
to_tag = inst.copy()
strip_pos(to_tag)
# Do the classification.
to_tag.classify_gloss_pos(c, lowercase=True,
feat_next_gram=context_feats,
feat_prev_gram=context_feats,
posdict=posdict)
to_dump.append(to_tag)
# Fix the tags...
# fix_ctn_gloss_line(to_tag, tag_method=INTENT_POS_CLASS)
# Now, retrieve eval/gold.
eval_tags = [v.value() for v in to_tag.get_pos_tags(GLOSS_WORD_ID, tag_method=INTENT_POS_CLASS)]
gold_tags = [v.value() for v in inst.get_pos_tags(GLOSS_WORD_ID, tag_method=MANUAL_POS)]
tag_tokens = [POSToken('a', label=l) for l in eval_tags]
gold_tokens= [POSToken('a', label=l) for l in gold_tags]
if not len(tag_tokens) == len(gold_tokens):
print("LENGTH OF SEQUENCE IS MISMATCHED")
continue
gold_sents.append(gold_tokens)
eval_sents.append(tag_tokens)
xigtxml.dump(open('./enriched_ctn_dev.xml', 'w'), to_dump)
return poseval(eval_sents, gold_sents, details=True,csv=True, matrix=True)
def eval_proj(xc):
prj_sents = []
sup_sents = []
for inst in xc:
fix_ctn_gloss_line(inst, tag_method=INTENT_POS_PROJ)
# Do the projection comparison
sup = inst.get_pos_tags(GLOSS_WORD_ID, tag_method=MANUAL_POS)
prj = inst.get_pos_tags(GLOSS_WORD_ID, tag_method=INTENT_POS_PROJ)
sup_tags = []
prj_tags = []
for s in sup:
sup_tags.append(POSToken(s.value(), label=s.value()))
# If the same tag occurs in the projections...
if not prj:
prj_tags.append(POSToken('UNALIGNED', label='UNALIGNED'))
continue
proj_tag = prj.find(alignment=s.attributes[ALIGNMENT])
if proj_tag:
prj_tags.append(POSToken(proj_tag.value(), label=proj_tag.value()))
else:
prj_tags.append(POSToken('UNALIGNED', label='UNALIGNED'))
sup_sents.append(sup_tags)
prj_sents.append(prj_tags)
poseval(prj_sents, sup_sents, details=True)
def fix_ctn_gloss_line(inst, tag_method=None):
"""
Given a CTN gloss line, do some specific fixes to attempt to fix the CTN tag mapping.
:param inst:
:type inst:RGIgt
"""
gpos_tier = inst.get_pos_tags(GLOSS_WORD_ID, tag_method=tag_method)
# Get the gloss words
for gw in inst.gloss:
new_tag = None
if gw.value().lower() in ['foc','top','seq','add','emph','cit','rep']:
new_tag = 'PRT'
elif gw.value().lower() in ['but','and','or']:
new_tag = 'CONJ'
elif 'dem' in gw.value().lower():
new_tag = 'PRON'
elif gw.value().lower() in ['for','in']:
new_tag = 'ADP'
elif gw.value().lower() in ['the']:
new_tag = 'DET'
if new_tag:
gpos = gpos_tier.find(alignment=gw.id)
if not gpos:
gpt = RGToken(id=gpos_tier.askItemId(), alignment=gw.id, text=new_tag)
gpos_tier.add(gpt)
else:
gpos.text = new_tag
if __name__ == '__main__':
ctn_train = './data/xml-files/ctn/ctn_train.xml'
ctn_dev = './data/xml-files/ctn/ctn_dev.xml'
ctn_dev_processed = './data/xml-files/ctn/ctn_dev_processed.xml'
ctn_train_processed = './data/xml-files/ctn/ctn_train_processed.xml'
posdict = pickle.load(open('./data/dictionaries/CTN.dict', 'rb'))
# print("Loading CTN Dev Corpus...", end=" ", flush=True)
# dev_xc = RGCorpus.load(ctn_dev)
# print("Done.")
#
# print("Loading CTN Train corpus...", end=" ", flush=True)
# train_xc = RGCorpus.load(ctn_train)
# print("Done.")
print("Initializing tagger...", end=" ", flush=True)
tagger = StanfordPOSTagger(tagger_model)
print("Done.")
# =============================================================================
# 1) Start by projecting the language line to the gloss line in the dev set,
# remapping it from the CTN tagset to the universal tagset along the way.
# =============================================================================
#
# print("Processing DEV corpus...", end=' ', flush=True)
# for inst in dev_xc:
# word_align(inst.gloss, inst.lang)
# inst.project_lang_to_gloss(tagmap = './data/tagset_mappings/ctn.txt')
# fix_ctn_gloss_line(inst, tag_method=MANUAL_POS)
# inst.tag_trans_pos(tagger)
# inst.heur_align() # Align trans/gloss lines heuristically
# inst.project_trans_to_gloss() # Now, project heuristically.
# print('done.')
#
# xigtxml.dump(open(ctn_dev_processed, 'w', encoding='utf-8'), dev_xc)
#
#
# print("Processing TRAIN Corpus...", end=' ', flush=True)
# # Get the language line words projected onto the gloss...
# for inst in train_xc:
# word_align(inst.gloss, inst.lang)
# inst.project_lang_to_gloss(tagmap = './data/tagset_mappings/ctn.txt')
# inst.tag_trans_pos(tagger)
# inst.heur_align()
# inst.project_trans_to_gloss()
# fix_ctn_gloss_line(inst, tag_method=INTENT_POS_PROJ)
#
# print("Done.")
#
# xigtxml.dump(open(ctn_train_processed, 'w', encoding='utf-8'), train_xc)
# sys.exit()
print("Loading Processed CTN Train corpus...", end=" ", flush=True)
train_xc = RGCorpus.load(ctn_train_processed)
print("Done.")
print("Loading Processed CTN Dev corpus...", end=" ", flush=True)
dev_xc = RGCorpus.load(ctn_dev_processed)
print("Done.")
#
# # =============================================================================
# # 2) Train a classifier based on the projected gloss line.
# # =============================================================================
#
index_list = [35,70,106,141,284,569,854,1139,1424,1708,1993,7120]
for train_stop_index in index_list:
train_instances = list(train_xc)[0:train_stop_index]
print('* '*50)
tokens = 0
for inst in train_instances:
tokens += len(inst.gloss)
print("Now training with {} tokens, {} instances.".format(tokens, train_stop_index))
print("Training Classifier...", end=" ", flush=True)
c = instances_to_classifier(train_instances, './ctn-train.class',
tag_method=MANUAL_POS,
posdict=posdict,
context_feats=True,
feat_path='./ctn-train_feats.txt')
print("Done.")
# c = MalletMaxent('/Users/rgeorgi/Documents/code/dissertation/gc.classifier')
# c = MalletMaxent('./ctn_class.class.classifier')
print("Evaluating classifier...", end=" ", flush=True)
eval_classifier(c, dev_xc, posdict=posdict, context_feats=True)
print("Done.")
# eval_proj(dev_xc)
| [((225, 244), 'logging.getLogger', 'logging.getLogger', ([], {}), '()\n', (242, 244), False, 'import logging\n'), ((292, 322), 'logging.getLogger', 'logging.getLogger', (['"""CTN_CLASS"""'], {}), "('CTN_CLASS')\n", (309, 322), False, 'import logging\n'), ((355, 376), 'logging.basicConfig', 'logging.basicConfig', ([], {}), '()\n', (374, 376), False, 'import logging\n'), ((1698, 1708), 'intent.igt.rgxigt.RGCorpus', 'RGCorpus', ([], {}), '()\n', (1706, 1708), False, 'from intent.igt.rgxigt import RGCorpus, strip_pos, RGIgt, RGTokenTier, RGTier, gen_tier_id, RGToken, ProjectionTransGlossException, word_align\n'), ((2836, 2904), 'intent.eval.pos_eval.poseval', 'poseval', (['eval_sents', 'gold_sents'], {'details': '(True)', 'csv': '(True)', 'matrix': '(True)'}), '(eval_sents, gold_sents, details=True, csv=True, matrix=True)\n', (2843, 2904), False, 'from intent.eval.pos_eval import poseval\n'), ((3891, 3934), 'intent.eval.pos_eval.poseval', 'poseval', (['prj_sents', 'sup_sents'], {'details': '(True)'}), '(prj_sents, sup_sents, details=True)\n', (3898, 3934), False, 'from intent.eval.pos_eval import poseval\n'), ((5651, 5682), 'intent.interfaces.stanford_tagger.StanfordPOSTagger', 'StanfordPOSTagger', (['tagger_model'], {}), '(tagger_model)\n', (5668, 5682), False, 'from intent.interfaces.stanford_tagger import StanfordPOSTagger\n'), ((7282, 7316), 'intent.igt.rgxigt.RGCorpus.load', 'RGCorpus.load', (['ctn_train_processed'], {}), '(ctn_train_processed)\n', (7295, 7316), False, 'from intent.igt.rgxigt import RGCorpus, strip_pos, RGIgt, RGTokenTier, RGTier, gen_tier_id, RGToken, ProjectionTransGlossException, word_align\n'), ((7423, 7455), 'intent.igt.rgxigt.RGCorpus.load', 'RGCorpus.load', (['ctn_dev_processed'], {}), '(ctn_dev_processed)\n', (7436, 7455), False, 'from intent.igt.rgxigt import RGCorpus, strip_pos, RGIgt, RGTokenTier, RGTier, gen_tier_id, RGToken, ProjectionTransGlossException, word_align\n'), ((1775, 1792), 'intent.igt.rgxigt.strip_pos', 'strip_pos', (['to_tag'], {}), '(to_tag)\n', (1784, 1792), False, 'from intent.igt.rgxigt import RGCorpus, strip_pos, RGIgt, RGTokenTier, RGTier, gen_tier_id, RGToken, ProjectionTransGlossException, word_align\n'), ((8186, 8352), 'intent.scripts.classification.xigt_to_classifier.instances_to_classifier', 'instances_to_classifier', (['train_instances', '"""./ctn-train.class"""'], {'tag_method': 'MANUAL_POS', 'posdict': 'posdict', 'context_feats': '(True)', 'feat_path': '"""./ctn-train_feats.txt"""'}), "(train_instances, './ctn-train.class', tag_method=\n MANUAL_POS, posdict=posdict, context_feats=True, feat_path=\n './ctn-train_feats.txt')\n", (8209, 8352), False, 'from intent.scripts.classification.xigt_to_classifier import instances_to_classifier\n'), ((2446, 2468), 'intent.utils.token.POSToken', 'POSToken', (['"""a"""'], {'label': 'l'}), "('a', label=l)\n", (2454, 2468), False, 'from intent.utils.token import POSToken, GoldTagPOSToken\n'), ((2511, 2533), 'intent.utils.token.POSToken', 'POSToken', (['"""a"""'], {'label': 'l'}), "('a', label=l)\n", (2519, 2533), False, 'from intent.utils.token import POSToken, GoldTagPOSToken\n'), ((3479, 3519), 'intent.utils.token.POSToken', 'POSToken', (['"""UNALIGNED"""'], {'label': '"""UNALIGNED"""'}), "('UNALIGNED', label='UNALIGNED')\n", (3487, 3519), False, 'from intent.utils.token import POSToken, GoldTagPOSToken\n'), ((3773, 3813), 'intent.utils.token.POSToken', 'POSToken', (['"""UNALIGNED"""'], {'label': '"""UNALIGNED"""'}), "('UNALIGNED', label='UNALIGNED')\n", (3781, 3813), False, 'from intent.utils.token import POSToken, GoldTagPOSToken\n')] |
Havana3351/Low-cost-remote-monitor | watchdog/back-end/v0.3.0/watchdog/app/resource/video.py | 9f86a62b8515c0f9fddda31f25548680f0ad8e2f | from flask_restful import Resource
from flask import Response
import os
import cv2
picturecounter = 1 # 防止过多记录的标识
class Video(Resource):
#如果方法为get 调用该方法
def get(self):
global picturecounter
# username = (request.get_json())['username']
# db = pymysql.connect("rm-2ze61i7u6d7a3fwp9yo.mysql.rds.aliyuncs.com", "team", "Aaa5225975", "pidata")
# cursor = db.cursor()
#
# sql = "select rpiname from user where username=\'" + username + "\'" # 可能存在类型问题
# cursor.execute(sql)
# row = cursor.fetchone()
#
# if not row:
# rpiname = None
# rpiname = str(row[0])
#覆盖取值
rpiname = 'raspberrypi'
# 获取指针并赋值
path = r'/root/video/realtime/%s' % (rpiname)
picnames = []
for filenames in os.walk(path):
picnames = filenames
print(picnames)
pointer = int(((picnames[2])[0].split('.'))[0])
picturecounter = pointer
picpath = r'/root/video/realtime/%s/%s.jpg' % (rpiname, picturecounter)
image = cv2.imread(picpath)
bs = cv2.imencode(".jpg", image)[1].tobytes()
picturecounter += 1
if(picturecounter > 5):
picturecounter = 1
return Response(bs, mimetype='image/jpeg')
def post(self):
print("post") | [((834, 847), 'os.walk', 'os.walk', (['path'], {}), '(path)\n', (841, 847), False, 'import os\n'), ((1098, 1117), 'cv2.imread', 'cv2.imread', (['picpath'], {}), '(picpath)\n', (1108, 1117), False, 'import cv2\n'), ((1280, 1315), 'flask.Response', 'Response', (['bs'], {'mimetype': '"""image/jpeg"""'}), "(bs, mimetype='image/jpeg')\n", (1288, 1315), False, 'from flask import Response\n'), ((1132, 1159), 'cv2.imencode', 'cv2.imencode', (['""".jpg"""', 'image'], {}), "('.jpg', image)\n", (1144, 1159), False, 'import cv2\n')] |
valgur/LEOGPS | codes/ambfix.py | f289f279ef55980a0e3fd82b3b3686e41c474a2e | #!/usr/bin/env python3
'''
###############################################################################
###############################################################################
## ##
## _ ___ ___ ___ ___ ___ ##
## | | | __ / \ / __| _ | __| ##
## | |__| __ ( ) | (_ | _|__ \ ##
## |____|___ \___/ \___|_| \___/ ##
## v 1.0 (Stable) ##
## ##
## FILE DESCRIPTION: ##
## ##
## This is the classical LAMBDA method that was originally authored by ##
## Teunissen, Jonge, and Tiberius (1993). The code was later written in ##
## MATLAB by Dr Sandra Verhagen and Dr Bofeng Li. It takes in a vector of ##
## float ambiguities to the integer least-squares problem, and covariance ##
## of the float ambiguities. It then runs the LAMBDA's ILS search-&-shrink ##
## and spits out the ambiguity integers. The other 5 methods in original ##
## LAMBDA MATLAB code are not supported here (feel free to edit the code ##
## and implement it youself!). The default ncands = 2, as per original code. ##
## All support functions from the original MATLAB code (decorrel, ldldecom) ##
## have been nested within the main function as sub functions. ##
## ##
## INPUTS: ##
## ##
## - ahat : numpy array of float ambiguities ##
## - Qahat : numpy covariance matrix for float ambiguities ##
## - ncands : number of candidates (optional parameter, default = 2) ##
## ##
## OUTPUT: ##
## ##
## - afixed : Array of size (n x ncands) with the estimated integer ##
## candidates, sorted according to the corresponding squared ##
## norms, best candidate first. ##
## - sqnorm : Distance between integer candidate and float ambiguity ##
## vectors in the metric of the variance-covariance matrix. ##
## ##
## REMARKS: ##
## ##
## Besides above changes, mostly syntax changes to this Python version: ##
## - Everything is identical EXCEPT MATLAB is ones-based indexing. ##
## - Python is zeros-based indexing, and range function does not ##
## include the upper limit index. Thus, only indices have changed. ##
## - Example in MATLAB: for i = 1:5 => {1,2,3,4,5} ##
## - Equivalently in Python: for i in range(0,5) => {0,1,2,3,4} ##
## - Indices are thus updated accordingly. ##
## ##
## DEVELOPER: Professor Peter Teunissen (TU Delft) ##
## ORIGINAL AUTHOR: Sandra Verhagen and Bofeng Li (TU Delft) ##
## AUTHOR MODIFIED: 26-07-2019, by Samuel Y.W. Low, with permissions. ##
## ##
###############################################################################
###############################################################################
'''
import numpy as np
def LAMBDA( ahat, Qahat, ncands = 2 ):
###########################################################################
###########################################################################
# [afixed, sqnorm] = LAMBDA( ahat, Qahat, ncands )
#
# This is the main routine of the LAMBDA software package. By default the
# ILS method will be used for integer estimation based on the provided
# float ambiguity vector ahat and associated variance-covariance matrix
# Qahat. In this Pythonic version (modified by Samuel Low, 2019), only
# the ILS method is implemented. For other techniques: integer rounding,
# bootstrapping or Partial Ambiguity Resolution (PAR), the user is free
# to modify this code and adapt it to their own needs.
#
# NOTE 1: LAMBDA always first applies a decorrelation before the integer
# estimation (for ILS this is required to guarantee an efficient search,
# for rounding and bootstrapping it is required in order to get higher
# success rates).
#
# INPUTS:
#
# ahat: Float ambiguities (must be a column!)
# Qahat: Variance/covariance matrix of ambiguities
# ncands: number of candidates (optional parameter, default = 2)
#
# OUTPUTS:
#
# afixed: Array of size (n x ncands) with the estimated integer
# candidates, sorted according to the corresponding squared
# norms, best candidate first.
# sqnorm: Distance between integer candidate and float ambiguity vectors
# in the metric of the variance-covariance matrix Qahat.
# Only available for ILS.
#
# -------------------------------------------------------------------------
# Release date : 1-SEPT-2012
# Authors : Bofeng LI and Sandra VERHAGEN
#
# GNSS Research Centre, Curtin University
# Mathematical Geodesy and Positioning, Delft University of Technology
# -------------------------------------------------------------------------
#
# REFERENCES:
# 1. LAMBDA Software Package: Matlab implementation, Version 3.0.
# Documentation provided with this software package.
# 2. Teunissen P (1993) Least-squares estimation of the integer GPS
# ambiguities. In: Invited lecture, section IV theory and methodology,
# IAG General Meeting, Beijing, China
# 3. Teunissen P (1995) The least-squares ambiguity decorrelation
# adjustment: a method for fast GPS ambiguity estitmation. J Geod
# 70:651-7
# 4. De Jonge P, Tiberius C (1996) The LAMBDA method of intger ambiguity
# estimation:implementation aspects.
# 5. Chang X ,Yang X, Zhou T (2005) MLAMBDA: a modified LAMBDA method for
# integer least-squares estimation
###########################################################################
###########################################################################
''' A function for obtaining the decimals only from float arrays '''
def floatrem( fltarray ):
# This function is NECESSARY because of the differences between:
# MATLAB's rem function
# (computes the true mathematical remainder)
# And Python's modulo % operator
# (computes remainder complementary to the floor_divide function)
fltarray = np.array(fltarray)
fltarray = fltarray + 0.000001
intarray = fltarray.astype(int)
decarray = fltarray - intarray
return decarray, intarray
###########################################################################
###########################################################################
''' A function to perform LtDL decomposition of the covariance matrix '''
def ldldecom( Qahat1 ):
# This routine finds the LtDL decomposition of a given variance or
# covariance matrix.
#
# Input arguments:
# Qahat: Symmetric n by n matrix to be factored
#
# Output arguments:
# L: n by n factor matrix (strict lower triangular)
# D: Diagonal n-vector
# ------------------------------------------------------------------
# File.....: ldldecom
# Date.....: 19-MAY-1999
# Author...: Peter Joosten
# Mathematical Geodesy and Positioning
# Delft University of Technology
# ------------------------------------------------------------------
Qahat2 = Qahat1.copy()
# If we do not use copy, we will overwrite the original Qahat...
# ... even the one outside the function! This doesn't occur in MATLAB.
n = len(Qahat2)
D = np.zeros((n))
L = np.zeros((n,n))
for i in range(n-1,-1,-1):
D[i] = Qahat2[i][i]
L[i,0:i+1] = Qahat2[i,0:i+1] / ((Qahat2[i][i])**0.5)
for j in range(0,i):
Qahat2[j,0:j+1] = Qahat2[j,0:j+1] - L[i,0:j+1]*L[i][j]
L[i,0:i+1] = L[i,0:i+1] / L[i][i]
return L,D
###########################################################################
###########################################################################
''' Decorrelation function for LAMBDA '''
def decorrel( ahat, Qahat ):
# function [Qzhat,Z,L,D,zhat,iZt] = decorrel (Qahat,ahat)
# DECORREL: Decorrelate a (co)variance matrix of ambiguities
#
# [Qzhat,Z,L,D,zhat] = decorrel (Qahat,ahat)
#
# This routine creates a decorrelated Q-matrix, by finding the
# Z-matrix and performing the corresponding transformation.
#
# The method is described in:
# The routine is based on Fortran routines written by Paul de Jonge
# and on Matlab-routines written by Kai Borre.
# The resulting Z-matrix can be used as follows:
# zhat = Zt * ahat; \hat(z) = Z' * \hat(a);
# Q_\hat(z) = Z' * Q_\hat(a) * Z
#
# Input arguments:
# Qahat: Variance-covariance matrix of ambiguities (original)
# ahat: Original ambiguities (optional)
#
# Output arguments:
# Qzhat: Variance-covariance matrix of decorrelated ambiguities
# Z: Z-transformation matrix
# L: L matrix (from LtDL-decomposition of Qzhat)
# D: D matrix (from LtDL-decomposition of Qzhat)
# zhat: Transformed ambiguities (optional)
# iZt: inv(Z')-transformation matrix
#
# ------------------------------------------------------------------
# Function.: decorrel
# Date.....: 19-MAY-1999 / modified 12-APRIL-2012
# Author...: Peter Joosten / Sandra Verhagen
# Mathematical Geodesy and Positioning
# Delft University of Technology
# Modified.: Samuel Low, July 2019, DSO National Laboratories
# ------------------------------------------------------------------
# Initialisations
n = len(Qahat)
iZt = np.identity(n)
i1 = n - 1
sw = True
# LtDL decomposition
L, D = ldldecom(Qahat)
while sw == 1:
i = n # Loop for column from n to 1
sw = 0
while sw == 0 and i > 1:
i = i - 1 # The i-th column
if i <= i1:
for j in range(i,n):
# We have to do some manual coding here, as python's
# rounding for .5's are different from MATLAB's
mu = L[j,i-1] # Get the float mu
mu_dec = mu%1 # Get the decimal float of mu
if mu_dec == 0.5:
mu += 0.01 # Just to make it round up properly.
mu = round(mu)
if mu != 0.0:
L[j:n,i-1] = L[j:n,i-1] - mu * L[j:n,j]
iZt[:,j] = iZt[:,j] + mu * iZt[:,i-1]
delta = D[i-1] + (L[i,i-1]**2) * D[i]
if delta < D[i]:
lam = D[i] * L[i,i-1] / delta
eta = D[i-1] / delta
D[i-1] = eta * D[i]
D[i] = delta
mult1 = np.array([-1*L[i,i-1], 1])
mult2 = np.array([eta,lam])
mult3 = np.stack((mult1,mult2))
L[i-1:i+1,0:i-1] = np.matmul(mult3,L[i-1:i+1,0:i-1])
L[i,i-1] = lam
# Flip rows i and i+1
L[i+1:n,i-1:i+1] = np.flip(L[i+1:n,i-1:i+1], axis=0)
iZt[:,i-1:i+1] = np.flip(iZt[:,i-1:i+1], axis=0)
i1 = i
sw = 1
iZt = iZt + 0.000001 # Resolves Python 3's rounding definition
Z = np.round(np.linalg.inv(iZt.transpose()))
Qzhat = np.matmul( Qahat, Z )
Qzhat = np.matmul( Z.transpose(), Qzhat )
zhat = np.matmul(Z.transpose(),ahat)
iZt = np.round(iZt)
return Qzhat, Z, L, D, zhat, iZt
###########################################################################
###########################################################################
def ssearch( ahat, L, D, ncands):
#------------------------------------------------------------------|
#
# Integer ambiguity vector search via search-and-shrink technique.
#
# INPUTS:
#
# ahat : Float ambiguities (should be decorrelated for
# computational efficiency)
# L,D : LtDL-decomposition of the variance-covariance matrix
# of the float ambiguities ahat
# ncands: Number of requested candidates
#
# OUTPUTS:
#
# afixed: estimated integers (n, x, ncands)
# sqnorm: corresponding squared norms (n-vector, ascending order)
#
#------------------------------------------------------------------|
# Date : 02-SEPT-2010 |
# Author : Bofeng LI |
# GNSS Research Center, Department of Spatial Sciences |
# Curtin University of Technology |
# E-mail : bofeng.li@curtin.edu.au |
#------------------------------------------------------------------|
# First, check that float ambiguity and D have same length
if len(ahat) != len(D):
print('Error! Float ambiguity vector must be a column vector!')
print('It must also have the same dimension as D')
return None
# Initialising outputs
n = len(ahat)
afixed = np.zeros((n, ncands))
sqnorm = np.zeros(ncands)
# Initializing the variables for searching
Chi2 = 1.0e+18 # Start search with an infinite chi-square
dist = np.zeros(n) # MATLAB distance function
endsearch = False # Search trigger
count = 0 # Count the number of candidates
acond = np.zeros(n)
acond[n-1] = ahat[n-1]
zcond = np.zeros(n)
zcond[n-1] = np.round(acond[n-1]+0.000001)
left = acond[n-1] - zcond[n-1]
step = np.zeros(n)
step[n-1] = np.sign(left)
if step[n-1] == 0:
step[n-1] = 1 # Give a positive step.
imax = ncands - 1 # Initially, the maximum F(z) is at ncands
S = np.zeros((n,n)) # Used to compute conditional ambiguities
k = n
# Now we start the main search loop.
while endsearch == False:
newdist = dist[k-1] + (left**2) / D[k-1]
if newdist < Chi2:
if k != 1: # Case 1: move down
k -= 1
dist[k-1] = newdist
S[k-1,0:k] = S[k,0:k] + (zcond[k] - acond[k])*L[k,0:k]
acond[k-1] = ahat[k-1] + S[k-1,k-1]
zcond[k-1] = np.round(acond[k-1]+0.000001)
left = acond[k-1] - zcond[k-1]
step[k-1] = np.sign(left)
if step[k-1] == 0: # Very rarely would this happen...
step[k-1] = 1 # ... but just in case, you know.
else: # Case 2: store the found candidate and try the next.
if count < (ncands - 1):
# Store the 1st ncands-1 initial points as candidates
count += 1
afixed[:,count-1] = zcond[0:n];
sqnorm[count-1] = newdist # Store F(zcond)
else:
afixed[:,imax] = zcond[0:n]
sqnorm[imax] = newdist
Chi2 = max(sqnorm)
imax = np.argmax(sqnorm) # No need to add '-1' to imax
zcond[0] = zcond[0] + step[0]
left = acond[0] - zcond[0]
step[0] = -1*step[0] - np.sign(step[0])
else: # Case 3: exit or move up
if k == n:
endsearch = True
else:
k += 1 # Move up
zcond[k-1] = zcond[k-1] + step[k-1]
left = acond[k-1] - zcond[k-1]
step[k-1] = -1*step[k-1] - np.sign(step[k-1])
order = np.argsort(sqnorm) # Get an array of INDICES for a sort.
sqnormf = np.sort(sqnorm) # Get an array of ACTUAL SORTS for sqnorm.
afixedf = np.copy(afixed)
for k in range(0,len(order)):
afixedf[:,k] = afixed[:,order[k]]
return afixedf, sqnormf
###########################################################################
###########################################################################
''' Initialisation and some initial sanity checks... '''
# Initialise all output variables
sqnorm = np.array([])
# Test inputs: Is the Q-matrix symmetric?
if np.array_equal(Qahat,Qahat.transpose()) == False:
print('Variance-covariance matrix is not symmetric!')
return None
# Test inputs: Is the Q-matrix positive-definite?
if np.sum(np.linalg.eig(Qahat)[0] > 0.0) != len(Qahat):
print('Variance-covariance matrix is not positive definite!')
return None
# Test inputs: Does Q-matrix and amb vector have identical dimensions?
if len(ahat) != len(Qahat):
print('Variance-covariance matrix and vector of ambiguities...')
print('... do not have identical dimensions!')
return None
###########################################################################
###########################################################################
''' Begin least-squares ambiguity decorrelation adjustment! '''
# Remove integer numbers from float solution, so that all values are
# between -1 and 1 (for computational convenience only)
ahat, incr = floatrem( ahat )
# Compute Z matrix based on the decomposition Q=L^T*D*L;
Qzhat, Z, L, D, zhat, iZt = decorrel( ahat, Qahat )
# Integer ambiguity vector search via search-and-shrink
zfixedff, sqnormff = ssearch( zhat, L, D, ncands )
# Perform the back-transformation and add the increments
afixed = np.matmul(iZt,zfixedff)
repmat = np.repeat(np.array([incr]),ncands,axis=0)
repmat = repmat.transpose()
afixed = afixed + repmat
afixed = afixed.transpose()
###########################################################################
###########################################################################
''' Returns best amb-fix, second best amb-fix, and the square norm '''
return afixed, sqnorm
###########################################################################
###########################################################################
| [((20371, 20383), 'numpy.array', 'np.array', (['[]'], {}), '([])\n', (20379, 20383), True, 'import numpy as np\n'), ((21776, 21800), 'numpy.matmul', 'np.matmul', (['iZt', 'zfixedff'], {}), '(iZt, zfixedff)\n', (21785, 21800), True, 'import numpy as np\n'), ((7722, 7740), 'numpy.array', 'np.array', (['fltarray'], {}), '(fltarray)\n', (7730, 7740), True, 'import numpy as np\n'), ((9173, 9184), 'numpy.zeros', 'np.zeros', (['n'], {}), '(n)\n', (9181, 9184), True, 'import numpy as np\n'), ((9208, 9224), 'numpy.zeros', 'np.zeros', (['(n, n)'], {}), '((n, n))\n', (9216, 9224), True, 'import numpy as np\n'), ((11652, 11666), 'numpy.identity', 'np.identity', (['n'], {}), '(n)\n', (11663, 11666), True, 'import numpy as np\n'), ((14206, 14225), 'numpy.matmul', 'np.matmul', (['Qahat', 'Z'], {}), '(Qahat, Z)\n', (14215, 14225), True, 'import numpy as np\n'), ((14364, 14377), 'numpy.round', 'np.round', (['iZt'], {}), '(iZt)\n', (14372, 14377), True, 'import numpy as np\n'), ((16259, 16280), 'numpy.zeros', 'np.zeros', (['(n, ncands)'], {}), '((n, ncands))\n', (16267, 16280), True, 'import numpy as np\n'), ((16298, 16314), 'numpy.zeros', 'np.zeros', (['ncands'], {}), '(ncands)\n', (16306, 16314), True, 'import numpy as np\n'), ((16456, 16467), 'numpy.zeros', 'np.zeros', (['n'], {}), '(n)\n', (16464, 16467), True, 'import numpy as np\n'), ((16607, 16618), 'numpy.zeros', 'np.zeros', (['n'], {}), '(n)\n', (16615, 16618), True, 'import numpy as np\n'), ((16675, 16686), 'numpy.zeros', 'np.zeros', (['n'], {}), '(n)\n', (16683, 16686), True, 'import numpy as np\n'), ((16708, 16738), 'numpy.round', 'np.round', (['(acond[n - 1] + 1e-06)'], {}), '(acond[n - 1] + 1e-06)\n', (16716, 16738), True, 'import numpy as np\n'), ((16810, 16821), 'numpy.zeros', 'np.zeros', (['n'], {}), '(n)\n', (16818, 16821), True, 'import numpy as np\n'), ((16842, 16855), 'numpy.sign', 'np.sign', (['left'], {}), '(left)\n', (16849, 16855), True, 'import numpy as np\n'), ((17045, 17061), 'numpy.zeros', 'np.zeros', (['(n, n)'], {}), '((n, n))\n', (17053, 17061), True, 'import numpy as np\n'), ((19742, 19760), 'numpy.argsort', 'np.argsort', (['sqnorm'], {}), '(sqnorm)\n', (19752, 19760), True, 'import numpy as np\n'), ((19826, 19841), 'numpy.sort', 'np.sort', (['sqnorm'], {}), '(sqnorm)\n', (19833, 19841), True, 'import numpy as np\n'), ((19912, 19927), 'numpy.copy', 'np.copy', (['afixed'], {}), '(afixed)\n', (19919, 19927), True, 'import numpy as np\n'), ((21823, 21839), 'numpy.array', 'np.array', (['[incr]'], {}), '([incr])\n', (21831, 21839), True, 'import numpy as np\n'), ((13377, 13408), 'numpy.array', 'np.array', (['[-1 * L[i, i - 1], 1]'], {}), '([-1 * L[i, i - 1], 1])\n', (13385, 13408), True, 'import numpy as np\n'), ((13453, 13473), 'numpy.array', 'np.array', (['[eta, lam]'], {}), '([eta, lam])\n', (13461, 13473), True, 'import numpy as np\n'), ((13522, 13546), 'numpy.stack', 'np.stack', (['(mult1, mult2)'], {}), '((mult1, mult2))\n', (13530, 13546), True, 'import numpy as np\n'), ((13606, 13647), 'numpy.matmul', 'np.matmul', (['mult3', 'L[i - 1:i + 1, 0:i - 1]'], {}), '(mult3, L[i - 1:i + 1, 0:i - 1])\n', (13615, 13647), True, 'import numpy as np\n'), ((13819, 13859), 'numpy.flip', 'np.flip', (['L[i + 1:n, i - 1:i + 1]'], {'axis': '(0)'}), '(L[i + 1:n, i - 1:i + 1], axis=0)\n', (13826, 13859), True, 'import numpy as np\n'), ((13911, 13947), 'numpy.flip', 'np.flip', (['iZt[:, i - 1:i + 1]'], {'axis': '(0)'}), '(iZt[:, i - 1:i + 1], axis=0)\n', (13918, 13947), True, 'import numpy as np\n'), ((17745, 17775), 'numpy.round', 'np.round', (['(acond[k - 1] + 1e-06)'], {}), '(acond[k - 1] + 1e-06)\n', (17753, 17775), True, 'import numpy as np\n'), ((17900, 17913), 'numpy.sign', 'np.sign', (['left'], {}), '(left)\n', (17907, 17913), True, 'import numpy as np\n'), ((20652, 20672), 'numpy.linalg.eig', 'np.linalg.eig', (['Qahat'], {}), '(Qahat)\n', (20665, 20672), True, 'import numpy as np\n'), ((18933, 18950), 'numpy.argmax', 'np.argmax', (['sqnorm'], {}), '(sqnorm)\n', (18942, 18950), True, 'import numpy as np\n'), ((19188, 19204), 'numpy.sign', 'np.sign', (['step[0]'], {}), '(step[0])\n', (19195, 19204), True, 'import numpy as np\n'), ((19690, 19710), 'numpy.sign', 'np.sign', (['step[k - 1]'], {}), '(step[k - 1])\n', (19697, 19710), True, 'import numpy as np\n')] |
bmcilw1/text-summary | summarizer/test_summarizer.py | f594fd4f41279a6e11262ac859cfbdad6aaf1703 | from summarizer.summarizer import summarize
def test_summarize_whenPassedEmptyString_ReturnsEmpty():
assert summarize("") == "" | [((114, 127), 'summarizer.summarizer.summarize', 'summarize', (['""""""'], {}), "('')\n", (123, 127), False, 'from summarizer.summarizer import summarize\n')] |
KarthikGandrala/DataEncryption | XORCipher/XOREncrypt.py | 6ed4dffead345bc9f7010ac2ea9afbff958c85af | #!/usr/bin/python
# -*- coding: utf-8 -*-
# Function to encrypt message using key is defined
def encrypt(msg, key):
# Defining empty strings and counters
hexadecimal = ''
iteration = 0
# Running for loop in the range of MSG and comparing the BITS
for i in range(len(msg)):
temp = ord(msg[i]) ^ ord(key[iteration])
# zfill will pad a single letter hex with 0, to make it two letter pair
hexadecimal += hex(temp)[2:].zfill(2)
# Checking if the iterations of the key are 1
iteration += 1
if iteration >= len(key):
# once all of the key's letters are used, repeat the key
iteration = 0
# Returning the final value
return hexadecimal
def decrypt(msg, key):
# Defining hex to uni string to store
hex_to_uni = ''
# Running for loop to the length of message
for i in range(0, len(msg), 2):
# Decoding each individual bytes from hex
hex_to_uni += bytes.fromhex(msg[i:i + 2]).decode('utf-8')
decryp_text = ''
iteration = 0
# For loop running for the length of the hex to unicode string
for i in range(len(hex_to_uni)):
# Comparing each individual bit
temp = ord(hex_to_uni[i]) ^ ord(key[iteration])
# zfill will pad a single letter hex with 0, to make it two letter pair
decryp_text += chr(temp)
iteration += 1
if iteration >= len(key):
# once all of the key's letters are used, repeat the key
iteration = 0
# FInally return the decrypted text string
return decryp_text
| [] |
francisrod01/udacity_python_foundations | 02-Use-functions/21-Opening_a_file/secret_message.py | 2a384cf35ce7eff547c88097cdc45cc4e8fc6041 | #!/usr/bin/python3
import os
import random
def rename_files(path):
file_list = os.listdir(path)
print(file_list)
for file_name in file_list:
# Remove numbers from filename.
# new_file_name file_name.translation(None, "0123456789")
# Add random numbers to beginning of filename.
new_file_name = str(random.randint(1, 99)) + file_name
print("Renaming " + file_name + " to " + new_file_name)
os.rename(os.path.join(path, file_name), os.path.join(path, new_file_name))
print("# Python program - Adding random numbers to beginning of filename.")
rename_files("./prank")
| [((86, 102), 'os.listdir', 'os.listdir', (['path'], {}), '(path)\n', (96, 102), False, 'import os\n'), ((465, 494), 'os.path.join', 'os.path.join', (['path', 'file_name'], {}), '(path, file_name)\n', (477, 494), False, 'import os\n'), ((496, 529), 'os.path.join', 'os.path.join', (['path', 'new_file_name'], {}), '(path, new_file_name)\n', (508, 529), False, 'import os\n'), ((347, 368), 'random.randint', 'random.randint', (['(1)', '(99)'], {}), '(1, 99)\n', (361, 368), False, 'import random\n')] |
timgates42/xarray | xarray/core/variable.py | bf0fe2caca1d2ebc4f1298f019758baa12f68b94 | import copy
import functools
import itertools
import numbers
import warnings
from collections import defaultdict
from datetime import timedelta
from distutils.version import LooseVersion
from typing import (
Any,
Dict,
Hashable,
Mapping,
Optional,
Sequence,
Tuple,
TypeVar,
Union,
)
import numpy as np
import pandas as pd
import xarray as xr # only for Dataset and DataArray
from . import arithmetic, common, dtypes, duck_array_ops, indexing, nputils, ops, utils
from .indexing import (
BasicIndexer,
OuterIndexer,
PandasIndexAdapter,
VectorizedIndexer,
as_indexable,
)
from .npcompat import IS_NEP18_ACTIVE
from .options import _get_keep_attrs
from .pycompat import (
cupy_array_type,
dask_array_type,
integer_types,
is_duck_dask_array,
)
from .utils import (
OrderedSet,
_default,
decode_numpy_dict_values,
drop_dims_from_indexers,
either_dict_or_kwargs,
ensure_us_time_resolution,
infix_dims,
is_duck_array,
)
NON_NUMPY_SUPPORTED_ARRAY_TYPES = (
(
indexing.ExplicitlyIndexed,
pd.Index,
)
+ dask_array_type
+ cupy_array_type
)
# https://github.com/python/mypy/issues/224
BASIC_INDEXING_TYPES = integer_types + (slice,) # type: ignore
VariableType = TypeVar("VariableType", bound="Variable")
"""Type annotation to be used when methods of Variable return self or a copy of self.
When called from an instance of a subclass, e.g. IndexVariable, mypy identifies the
output as an instance of the subclass.
Usage::
class Variable:
def f(self: VariableType, ...) -> VariableType:
...
"""
class MissingDimensionsError(ValueError):
"""Error class used when we can't safely guess a dimension name."""
# inherits from ValueError for backward compatibility
# TODO: move this to an xarray.exceptions module?
def as_variable(obj, name=None) -> "Union[Variable, IndexVariable]":
"""Convert an object into a Variable.
Parameters
----------
obj : object
Object to convert into a Variable.
- If the object is already a Variable, return a shallow copy.
- Otherwise, if the object has 'dims' and 'data' attributes, convert
it into a new Variable.
- If all else fails, attempt to convert the object into a Variable by
unpacking it into the arguments for creating a new Variable.
name : str, optional
If provided:
- `obj` can be a 1D array, which is assumed to label coordinate values
along a dimension of this given name.
- Variables with name matching one of their dimensions are converted
into `IndexVariable` objects.
Returns
-------
var : Variable
The newly created variable.
"""
from .dataarray import DataArray
# TODO: consider extending this method to automatically handle Iris and
if isinstance(obj, DataArray):
# extract the primary Variable from DataArrays
obj = obj.variable
if isinstance(obj, Variable):
obj = obj.copy(deep=False)
elif isinstance(obj, tuple):
try:
obj = Variable(*obj)
except (TypeError, ValueError) as error:
# use .format() instead of % because it handles tuples consistently
raise error.__class__(
"Could not convert tuple of form "
"(dims, data[, attrs, encoding]): "
"{} to Variable.".format(obj)
)
elif utils.is_scalar(obj):
obj = Variable([], obj)
elif isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None:
obj = Variable(obj.name, obj)
elif isinstance(obj, (set, dict)):
raise TypeError("variable {!r} has invalid type {!r}".format(name, type(obj)))
elif name is not None:
data = as_compatible_data(obj)
if data.ndim != 1:
raise MissingDimensionsError(
"cannot set variable %r with %r-dimensional data "
"without explicit dimension names. Pass a tuple of "
"(dims, data) instead." % (name, data.ndim)
)
obj = Variable(name, data, fastpath=True)
else:
raise TypeError(
"unable to convert object into a variable without an "
"explicit list of dimensions: %r" % obj
)
if name is not None and name in obj.dims:
# convert the Variable into an Index
if obj.ndim != 1:
raise MissingDimensionsError(
"%r has more than 1-dimension and the same name as one of its "
"dimensions %r. xarray disallows such variables because they "
"conflict with the coordinates used to label "
"dimensions." % (name, obj.dims)
)
obj = obj.to_index_variable()
return obj
def _maybe_wrap_data(data):
"""
Put pandas.Index and numpy.ndarray arguments in adapter objects to ensure
they can be indexed properly.
NumpyArrayAdapter, PandasIndexAdapter and LazilyOuterIndexedArray should
all pass through unmodified.
"""
if isinstance(data, pd.Index):
return PandasIndexAdapter(data)
return data
def _possibly_convert_objects(values):
"""Convert arrays of datetime.datetime and datetime.timedelta objects into
datetime64 and timedelta64, according to the pandas convention. Also used for
validating that datetime64 and timedelta64 objects are within the valid date
range for ns precision, as pandas will raise an error if they are not.
"""
return np.asarray(pd.Series(values.ravel())).reshape(values.shape)
def as_compatible_data(data, fastpath=False):
"""Prepare and wrap data to put in a Variable.
- If data does not have the necessary attributes, convert it to ndarray.
- If data has dtype=datetime64, ensure that it has ns precision. If it's a
pandas.Timestamp, convert it to datetime64.
- If data is already a pandas or xarray object (other than an Index), just
use the values.
Finally, wrap it up with an adapter if necessary.
"""
if fastpath and getattr(data, "ndim", 0) > 0:
# can't use fastpath (yet) for scalars
return _maybe_wrap_data(data)
if isinstance(data, Variable):
return data.data
if isinstance(data, NON_NUMPY_SUPPORTED_ARRAY_TYPES):
return _maybe_wrap_data(data)
if isinstance(data, tuple):
data = utils.to_0d_object_array(data)
if isinstance(data, pd.Timestamp):
# TODO: convert, handle datetime objects, too
data = np.datetime64(data.value, "ns")
if isinstance(data, timedelta):
data = np.timedelta64(getattr(data, "value", data), "ns")
# we don't want nested self-described arrays
data = getattr(data, "values", data)
if isinstance(data, np.ma.MaskedArray):
mask = np.ma.getmaskarray(data)
if mask.any():
dtype, fill_value = dtypes.maybe_promote(data.dtype)
data = np.asarray(data, dtype=dtype)
data[mask] = fill_value
else:
data = np.asarray(data)
if not isinstance(data, np.ndarray):
if hasattr(data, "__array_function__"):
if IS_NEP18_ACTIVE:
return data
else:
raise TypeError(
"Got an NumPy-like array type providing the "
"__array_function__ protocol but NEP18 is not enabled. "
"Check that numpy >= v1.16 and that the environment "
'variable "NUMPY_EXPERIMENTAL_ARRAY_FUNCTION" is set to '
'"1"'
)
# validate whether the data is valid data types.
data = np.asarray(data)
if isinstance(data, np.ndarray):
if data.dtype.kind == "O":
data = _possibly_convert_objects(data)
elif data.dtype.kind == "M":
data = _possibly_convert_objects(data)
elif data.dtype.kind == "m":
data = _possibly_convert_objects(data)
return _maybe_wrap_data(data)
def _as_array_or_item(data):
"""Return the given values as a numpy array, or as an individual item if
it's a 0d datetime64 or timedelta64 array.
Importantly, this function does not copy data if it is already an ndarray -
otherwise, it will not be possible to update Variable values in place.
This function mostly exists because 0-dimensional ndarrays with
dtype=datetime64 are broken :(
https://github.com/numpy/numpy/issues/4337
https://github.com/numpy/numpy/issues/7619
TODO: remove this (replace with np.asarray) once these issues are fixed
"""
if isinstance(data, cupy_array_type):
data = data.get()
else:
data = np.asarray(data)
if data.ndim == 0:
if data.dtype.kind == "M":
data = np.datetime64(data, "ns")
elif data.dtype.kind == "m":
data = np.timedelta64(data, "ns")
return data
class Variable(
common.AbstractArray, arithmetic.SupportsArithmetic, utils.NdimSizeLenMixin
):
"""A netcdf-like variable consisting of dimensions, data and attributes
which describe a single Array. A single Variable object is not fully
described outside the context of its parent Dataset (if you want such a
fully described object, use a DataArray instead).
The main functional difference between Variables and numpy arrays is that
numerical operations on Variables implement array broadcasting by dimension
name. For example, adding an Variable with dimensions `('time',)` to
another Variable with dimensions `('space',)` results in a new Variable
with dimensions `('time', 'space')`. Furthermore, numpy reduce operations
like ``mean`` or ``sum`` are overwritten to take a "dimension" argument
instead of an "axis".
Variables are light-weight objects used as the building block for datasets.
They are more primitive objects, so operations with them provide marginally
higher performance than using DataArrays. However, manipulating data in the
form of a Dataset or DataArray should almost always be preferred, because
they can use more complete metadata in context of coordinate labels.
"""
__slots__ = ("_dims", "_data", "_attrs", "_encoding")
def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False):
"""
Parameters
----------
dims : str or sequence of str
Name(s) of the the data dimension(s). Must be either a string (only
for 1D data) or a sequence of strings with length equal to the
number of dimensions.
data : array_like
Data array which supports numpy-like data access.
attrs : dict_like or None, optional
Attributes to assign to the new variable. If None (default), an
empty attribute dictionary is initialized.
encoding : dict_like or None, optional
Dictionary specifying how to encode this array's data into a
serialized format like netCDF4. Currently used keys (for netCDF)
include '_FillValue', 'scale_factor', 'add_offset' and 'dtype'.
Well-behaved code to serialize a Variable should ignore
unrecognized encoding items.
"""
self._data = as_compatible_data(data, fastpath=fastpath)
self._dims = self._parse_dimensions(dims)
self._attrs = None
self._encoding = None
if attrs is not None:
self.attrs = attrs
if encoding is not None:
self.encoding = encoding
@property
def dtype(self):
return self._data.dtype
@property
def shape(self):
return self._data.shape
@property
def nbytes(self):
return self.size * self.dtype.itemsize
@property
def _in_memory(self):
return isinstance(self._data, (np.ndarray, np.number, PandasIndexAdapter)) or (
isinstance(self._data, indexing.MemoryCachedArray)
and isinstance(self._data.array, indexing.NumpyIndexingAdapter)
)
@property
def data(self):
if is_duck_array(self._data):
return self._data
else:
return self.values
@data.setter
def data(self, data):
data = as_compatible_data(data)
if data.shape != self.shape:
raise ValueError(
f"replacement data must match the Variable's shape. "
f"replacement data has shape {data.shape}; Variable has shape {self.shape}"
)
self._data = data
def astype(
self: VariableType,
dtype,
*,
order=None,
casting=None,
subok=None,
copy=None,
keep_attrs=True,
) -> VariableType:
"""
Copy of the Variable object, with data cast to a specified type.
Parameters
----------
dtype : str or dtype
Typecode or data-type to which the array is cast.
order : {'C', 'F', 'A', 'K'}, optional
Controls the memory layout order of the result. ‘C’ means C order,
‘F’ means Fortran order, ‘A’ means ‘F’ order if all the arrays are
Fortran contiguous, ‘C’ order otherwise, and ‘K’ means as close to
the order the array elements appear in memory as possible.
casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
Controls what kind of data casting may occur.
* 'no' means the data types should not be cast at all.
* 'equiv' means only byte-order changes are allowed.
* 'safe' means only casts which can preserve values are allowed.
* 'same_kind' means only safe casts or casts within a kind,
like float64 to float32, are allowed.
* 'unsafe' means any data conversions may be done.
subok : bool, optional
If True, then sub-classes will be passed-through, otherwise the
returned array will be forced to be a base-class array.
copy : bool, optional
By default, astype always returns a newly allocated array. If this
is set to False and the `dtype` requirement is satisfied, the input
array is returned instead of a copy.
keep_attrs : bool, optional
By default, astype keeps attributes. Set to False to remove
attributes in the returned object.
Returns
-------
out : same as object
New object with data cast to the specified type.
Notes
-----
The ``order``, ``casting``, ``subok`` and ``copy`` arguments are only passed
through to the ``astype`` method of the underlying array when a value
different than ``None`` is supplied.
Make sure to only supply these arguments if the underlying array class
supports them.
See also
--------
numpy.ndarray.astype
dask.array.Array.astype
sparse.COO.astype
"""
from .computation import apply_ufunc
kwargs = dict(order=order, casting=casting, subok=subok, copy=copy)
kwargs = {k: v for k, v in kwargs.items() if v is not None}
return apply_ufunc(
duck_array_ops.astype,
self,
dtype,
kwargs=kwargs,
keep_attrs=keep_attrs,
dask="allowed",
)
def load(self, **kwargs):
"""Manually trigger loading of this variable's data from disk or a
remote source into memory and return this variable.
Normally, it should not be necessary to call this method in user code,
because all xarray functions should either work on deferred data or
load data automatically.
Parameters
----------
**kwargs : dict
Additional keyword arguments passed on to ``dask.array.compute``.
See Also
--------
dask.array.compute
"""
if is_duck_dask_array(self._data):
self._data = as_compatible_data(self._data.compute(**kwargs))
elif not is_duck_array(self._data):
self._data = np.asarray(self._data)
return self
def compute(self, **kwargs):
"""Manually trigger loading of this variable's data from disk or a
remote source into memory and return a new variable. The original is
left unaltered.
Normally, it should not be necessary to call this method in user code,
because all xarray functions should either work on deferred data or
load data automatically.
Parameters
----------
**kwargs : dict
Additional keyword arguments passed on to ``dask.array.compute``.
See Also
--------
dask.array.compute
"""
new = self.copy(deep=False)
return new.load(**kwargs)
def __dask_tokenize__(self):
# Use v.data, instead of v._data, in order to cope with the wrappers
# around NetCDF and the like
from dask.base import normalize_token
return normalize_token((type(self), self._dims, self.data, self._attrs))
def __dask_graph__(self):
if is_duck_dask_array(self._data):
return self._data.__dask_graph__()
else:
return None
def __dask_keys__(self):
return self._data.__dask_keys__()
def __dask_layers__(self):
return self._data.__dask_layers__()
@property
def __dask_optimize__(self):
return self._data.__dask_optimize__
@property
def __dask_scheduler__(self):
return self._data.__dask_scheduler__
def __dask_postcompute__(self):
array_func, array_args = self._data.__dask_postcompute__()
return (
self._dask_finalize,
(array_func, array_args, self._dims, self._attrs, self._encoding),
)
def __dask_postpersist__(self):
array_func, array_args = self._data.__dask_postpersist__()
return (
self._dask_finalize,
(array_func, array_args, self._dims, self._attrs, self._encoding),
)
@staticmethod
def _dask_finalize(results, array_func, array_args, dims, attrs, encoding):
data = array_func(results, *array_args)
return Variable(dims, data, attrs=attrs, encoding=encoding)
@property
def values(self):
"""The variable's data as a numpy.ndarray"""
return _as_array_or_item(self._data)
@values.setter
def values(self, values):
self.data = values
def to_base_variable(self):
"""Return this variable as a base xarray.Variable"""
return Variable(
self.dims, self._data, self._attrs, encoding=self._encoding, fastpath=True
)
to_variable = utils.alias(to_base_variable, "to_variable")
def to_index_variable(self):
"""Return this variable as an xarray.IndexVariable"""
return IndexVariable(
self.dims, self._data, self._attrs, encoding=self._encoding, fastpath=True
)
to_coord = utils.alias(to_index_variable, "to_coord")
def to_index(self):
"""Convert this variable to a pandas.Index"""
return self.to_index_variable().to_index()
def to_dict(self, data=True):
"""Dictionary representation of variable."""
item = {"dims": self.dims, "attrs": decode_numpy_dict_values(self.attrs)}
if data:
item["data"] = ensure_us_time_resolution(self.values).tolist()
else:
item.update({"dtype": str(self.dtype), "shape": self.shape})
return item
@property
def dims(self):
"""Tuple of dimension names with which this variable is associated."""
return self._dims
@dims.setter
def dims(self, value):
self._dims = self._parse_dimensions(value)
def _parse_dimensions(self, dims):
if isinstance(dims, str):
dims = (dims,)
dims = tuple(dims)
if len(dims) != self.ndim:
raise ValueError(
"dimensions %s must have the same length as the "
"number of data dimensions, ndim=%s" % (dims, self.ndim)
)
return dims
def _item_key_to_tuple(self, key):
if utils.is_dict_like(key):
return tuple(key.get(dim, slice(None)) for dim in self.dims)
else:
return key
def _broadcast_indexes(self, key):
"""Prepare an indexing key for an indexing operation.
Parameters
-----------
key: int, slice, array-like, dict or tuple of integer, slice and array-like
Any valid input for indexing.
Returns
-------
dims : tuple
Dimension of the resultant variable.
indexers : IndexingTuple subclass
Tuple of integer, array-like, or slices to use when indexing
self._data. The type of this argument indicates the type of
indexing to perform, either basic, outer or vectorized.
new_order : Optional[Sequence[int]]
Optional reordering to do on the result of indexing. If not None,
the first len(new_order) indexing should be moved to these
positions.
"""
key = self._item_key_to_tuple(key) # key is a tuple
# key is a tuple of full size
key = indexing.expanded_indexer(key, self.ndim)
# Convert a scalar Variable to an integer
key = tuple(
k.data.item() if isinstance(k, Variable) and k.ndim == 0 else k for k in key
)
# Convert a 0d-array to an integer
key = tuple(
k.item() if isinstance(k, np.ndarray) and k.ndim == 0 else k for k in key
)
if all(isinstance(k, BASIC_INDEXING_TYPES) for k in key):
return self._broadcast_indexes_basic(key)
self._validate_indexers(key)
# Detect it can be mapped as an outer indexer
# If all key is unlabeled, or
# key can be mapped as an OuterIndexer.
if all(not isinstance(k, Variable) for k in key):
return self._broadcast_indexes_outer(key)
# If all key is 1-dimensional and there are no duplicate labels,
# key can be mapped as an OuterIndexer.
dims = []
for k, d in zip(key, self.dims):
if isinstance(k, Variable):
if len(k.dims) > 1:
return self._broadcast_indexes_vectorized(key)
dims.append(k.dims[0])
elif not isinstance(k, integer_types):
dims.append(d)
if len(set(dims)) == len(dims):
return self._broadcast_indexes_outer(key)
return self._broadcast_indexes_vectorized(key)
def _broadcast_indexes_basic(self, key):
dims = tuple(
dim for k, dim in zip(key, self.dims) if not isinstance(k, integer_types)
)
return dims, BasicIndexer(key), None
def _validate_indexers(self, key):
""" Make sanity checks """
for dim, k in zip(self.dims, key):
if isinstance(k, BASIC_INDEXING_TYPES):
pass
else:
if not isinstance(k, Variable):
k = np.asarray(k)
if k.ndim > 1:
raise IndexError(
"Unlabeled multi-dimensional array cannot be "
"used for indexing: {}".format(k)
)
if k.dtype.kind == "b":
if self.shape[self.get_axis_num(dim)] != len(k):
raise IndexError(
"Boolean array size {:d} is used to index array "
"with shape {:s}.".format(len(k), str(self.shape))
)
if k.ndim > 1:
raise IndexError(
"{}-dimensional boolean indexing is "
"not supported. ".format(k.ndim)
)
if getattr(k, "dims", (dim,)) != (dim,):
raise IndexError(
"Boolean indexer should be unlabeled or on the "
"same dimension to the indexed array. Indexer is "
"on {:s} but the target dimension is {:s}.".format(
str(k.dims), dim
)
)
def _broadcast_indexes_outer(self, key):
dims = tuple(
k.dims[0] if isinstance(k, Variable) else dim
for k, dim in zip(key, self.dims)
if not isinstance(k, integer_types)
)
new_key = []
for k in key:
if isinstance(k, Variable):
k = k.data
if not isinstance(k, BASIC_INDEXING_TYPES):
k = np.asarray(k)
if k.size == 0:
# Slice by empty list; numpy could not infer the dtype
k = k.astype(int)
elif k.dtype.kind == "b":
(k,) = np.nonzero(k)
new_key.append(k)
return dims, OuterIndexer(tuple(new_key)), None
def _nonzero(self):
""" Equivalent numpy's nonzero but returns a tuple of Varibles. """
# TODO we should replace dask's native nonzero
# after https://github.com/dask/dask/issues/1076 is implemented.
nonzeros = np.nonzero(self.data)
return tuple(Variable((dim), nz) for nz, dim in zip(nonzeros, self.dims))
def _broadcast_indexes_vectorized(self, key):
variables = []
out_dims_set = OrderedSet()
for dim, value in zip(self.dims, key):
if isinstance(value, slice):
out_dims_set.add(dim)
else:
variable = (
value
if isinstance(value, Variable)
else as_variable(value, name=dim)
)
if variable.dtype.kind == "b": # boolean indexing case
(variable,) = variable._nonzero()
variables.append(variable)
out_dims_set.update(variable.dims)
variable_dims = set()
for variable in variables:
variable_dims.update(variable.dims)
slices = []
for i, (dim, value) in enumerate(zip(self.dims, key)):
if isinstance(value, slice):
if dim in variable_dims:
# We only convert slice objects to variables if they share
# a dimension with at least one other variable. Otherwise,
# we can equivalently leave them as slices aknd transpose
# the result. This is significantly faster/more efficient
# for most array backends.
values = np.arange(*value.indices(self.sizes[dim]))
variables.insert(i - len(slices), Variable((dim,), values))
else:
slices.append((i, value))
try:
variables = _broadcast_compat_variables(*variables)
except ValueError:
raise IndexError(f"Dimensions of indexers mismatch: {key}")
out_key = [variable.data for variable in variables]
out_dims = tuple(out_dims_set)
slice_positions = set()
for i, value in slices:
out_key.insert(i, value)
new_position = out_dims.index(self.dims[i])
slice_positions.add(new_position)
if slice_positions:
new_order = [i for i in range(len(out_dims)) if i not in slice_positions]
else:
new_order = None
return out_dims, VectorizedIndexer(tuple(out_key)), new_order
def __getitem__(self: VariableType, key) -> VariableType:
"""Return a new Variable object whose contents are consistent with
getting the provided key from the underlying data.
NB. __getitem__ and __setitem__ implement xarray-style indexing,
where if keys are unlabeled arrays, we index the array orthogonally
with them. If keys are labeled array (such as Variables), they are
broadcasted with our usual scheme and then the array is indexed with
the broadcasted key, like numpy's fancy indexing.
If you really want to do indexing like `x[x > 0]`, manipulate the numpy
array `x.values` directly.
"""
dims, indexer, new_order = self._broadcast_indexes(key)
data = as_indexable(self._data)[indexer]
if new_order:
data = duck_array_ops.moveaxis(data, range(len(new_order)), new_order)
return self._finalize_indexing_result(dims, data)
def _finalize_indexing_result(self: VariableType, dims, data) -> VariableType:
"""Used by IndexVariable to return IndexVariable objects when possible."""
return type(self)(dims, data, self._attrs, self._encoding, fastpath=True)
def _getitem_with_mask(self, key, fill_value=dtypes.NA):
"""Index this Variable with -1 remapped to fill_value."""
# TODO(shoyer): expose this method in public API somewhere (isel?) and
# use it for reindex.
# TODO(shoyer): add a sanity check that all other integers are
# non-negative
# TODO(shoyer): add an optimization, remapping -1 to an adjacent value
# that is actually indexed rather than mapping it to the last value
# along each axis.
if fill_value is dtypes.NA:
fill_value = dtypes.get_fill_value(self.dtype)
dims, indexer, new_order = self._broadcast_indexes(key)
if self.size:
if is_duck_dask_array(self._data):
# dask's indexing is faster this way; also vindex does not
# support negative indices yet:
# https://github.com/dask/dask/pull/2967
actual_indexer = indexing.posify_mask_indexer(indexer)
else:
actual_indexer = indexer
data = as_indexable(self._data)[actual_indexer]
mask = indexing.create_mask(indexer, self.shape, data)
# we need to invert the mask in order to pass data first. This helps
# pint to choose the correct unit
# TODO: revert after https://github.com/hgrecco/pint/issues/1019 is fixed
data = duck_array_ops.where(np.logical_not(mask), data, fill_value)
else:
# array cannot be indexed along dimensions of size 0, so just
# build the mask directly instead.
mask = indexing.create_mask(indexer, self.shape)
data = np.broadcast_to(fill_value, getattr(mask, "shape", ()))
if new_order:
data = duck_array_ops.moveaxis(data, range(len(new_order)), new_order)
return self._finalize_indexing_result(dims, data)
def __setitem__(self, key, value):
"""__setitem__ is overloaded to access the underlying numpy values with
orthogonal indexing.
See __getitem__ for more details.
"""
dims, index_tuple, new_order = self._broadcast_indexes(key)
if not isinstance(value, Variable):
value = as_compatible_data(value)
if value.ndim > len(dims):
raise ValueError(
"shape mismatch: value array of shape %s could not be "
"broadcast to indexing result with %s dimensions"
% (value.shape, len(dims))
)
if value.ndim == 0:
value = Variable((), value)
else:
value = Variable(dims[-value.ndim :], value)
# broadcast to become assignable
value = value.set_dims(dims).data
if new_order:
value = duck_array_ops.asarray(value)
value = value[(len(dims) - value.ndim) * (np.newaxis,) + (Ellipsis,)]
value = duck_array_ops.moveaxis(value, new_order, range(len(new_order)))
indexable = as_indexable(self._data)
indexable[index_tuple] = value
@property
def attrs(self) -> Dict[Hashable, Any]:
"""Dictionary of local attributes on this variable."""
if self._attrs is None:
self._attrs = {}
return self._attrs
@attrs.setter
def attrs(self, value: Mapping[Hashable, Any]) -> None:
self._attrs = dict(value)
@property
def encoding(self):
"""Dictionary of encodings on this variable."""
if self._encoding is None:
self._encoding = {}
return self._encoding
@encoding.setter
def encoding(self, value):
try:
self._encoding = dict(value)
except ValueError:
raise ValueError("encoding must be castable to a dictionary")
def copy(self, deep=True, data=None):
"""Returns a copy of this object.
If `deep=True`, the data array is loaded into memory and copied onto
the new object. Dimensions, attributes and encodings are always copied.
Use `data` to create a new object with the same structure as
original but entirely new data.
Parameters
----------
deep : bool, optional
Whether the data array is loaded into memory and copied onto
the new object. Default is True.
data : array_like, optional
Data to use in the new object. Must have same shape as original.
When `data` is used, `deep` is ignored.
Returns
-------
object : Variable
New object with dimensions, attributes, encodings, and optionally
data copied from original.
Examples
--------
Shallow copy versus deep copy
>>> var = xr.Variable(data=[1, 2, 3], dims="x")
>>> var.copy()
<xarray.Variable (x: 3)>
array([1, 2, 3])
>>> var_0 = var.copy(deep=False)
>>> var_0[0] = 7
>>> var_0
<xarray.Variable (x: 3)>
array([7, 2, 3])
>>> var
<xarray.Variable (x: 3)>
array([7, 2, 3])
Changing the data using the ``data`` argument maintains the
structure of the original object, but with the new data. Original
object is unaffected.
>>> var.copy(data=[0.1, 0.2, 0.3])
<xarray.Variable (x: 3)>
array([0.1, 0.2, 0.3])
>>> var
<xarray.Variable (x: 3)>
array([7, 2, 3])
See Also
--------
pandas.DataFrame.copy
"""
if data is None:
data = self._data
if isinstance(data, indexing.MemoryCachedArray):
# don't share caching between copies
data = indexing.MemoryCachedArray(data.array)
if deep:
data = copy.deepcopy(data)
else:
data = as_compatible_data(data)
if self.shape != data.shape:
raise ValueError(
"Data shape {} must match shape of object {}".format(
data.shape, self.shape
)
)
# note:
# dims is already an immutable tuple
# attributes and encoding will be copied when the new Array is created
return self._replace(data=data)
def _replace(
self, dims=_default, data=_default, attrs=_default, encoding=_default
) -> "Variable":
if dims is _default:
dims = copy.copy(self._dims)
if data is _default:
data = copy.copy(self.data)
if attrs is _default:
attrs = copy.copy(self._attrs)
if encoding is _default:
encoding = copy.copy(self._encoding)
return type(self)(dims, data, attrs, encoding, fastpath=True)
def __copy__(self):
return self.copy(deep=False)
def __deepcopy__(self, memo=None):
# memo does nothing but is required for compatibility with
# copy.deepcopy
return self.copy(deep=True)
# mutable objects should not be hashable
# https://github.com/python/mypy/issues/4266
__hash__ = None # type: ignore
@property
def chunks(self):
"""Block dimensions for this array's data or None if it's not a dask
array.
"""
return getattr(self._data, "chunks", None)
_array_counter = itertools.count()
def chunk(self, chunks={}, name=None, lock=False):
"""Coerce this array's data into a dask arrays with the given chunks.
If this variable is a non-dask array, it will be converted to dask
array. If it's a dask array, it will be rechunked to the given chunk
sizes.
If neither chunks is not provided for one or more dimensions, chunk
sizes along that dimension will not be updated; non-dask arrays will be
converted into dask arrays with a single block.
Parameters
----------
chunks : int, tuple or dict, optional
Chunk sizes along each dimension, e.g., ``5``, ``(5, 5)`` or
``{'x': 5, 'y': 5}``.
name : str, optional
Used to generate the name for this array in the internal dask
graph. Does not need not be unique.
lock : optional
Passed on to :py:func:`dask.array.from_array`, if the array is not
already as dask array.
Returns
-------
chunked : xarray.Variable
"""
import dask
import dask.array as da
if chunks is None:
warnings.warn(
"None value for 'chunks' is deprecated. "
"It will raise an error in the future. Use instead '{}'",
category=FutureWarning,
)
chunks = {}
if utils.is_dict_like(chunks):
chunks = {self.get_axis_num(dim): chunk for dim, chunk in chunks.items()}
data = self._data
if is_duck_dask_array(data):
data = data.rechunk(chunks)
else:
if isinstance(data, indexing.ExplicitlyIndexed):
# Unambiguously handle array storage backends (like NetCDF4 and h5py)
# that can't handle general array indexing. For example, in netCDF4 you
# can do "outer" indexing along two dimensions independent, which works
# differently from how NumPy handles it.
# da.from_array works by using lazy indexing with a tuple of slices.
# Using OuterIndexer is a pragmatic choice: dask does not yet handle
# different indexing types in an explicit way:
# https://github.com/dask/dask/issues/2883
data = indexing.ImplicitToExplicitIndexingAdapter(
data, indexing.OuterIndexer
)
if LooseVersion(dask.__version__) < "2.0.0":
kwargs = {}
else:
# All of our lazily loaded backend array classes should use NumPy
# array operations.
kwargs = {"meta": np.ndarray}
else:
kwargs = {}
if utils.is_dict_like(chunks):
chunks = tuple(chunks.get(n, s) for n, s in enumerate(self.shape))
data = da.from_array(data, chunks, name=name, lock=lock, **kwargs)
return type(self)(self.dims, data, self._attrs, self._encoding, fastpath=True)
def _as_sparse(self, sparse_format=_default, fill_value=dtypes.NA):
"""
use sparse-array as backend.
"""
import sparse
# TODO: what to do if dask-backended?
if fill_value is dtypes.NA:
dtype, fill_value = dtypes.maybe_promote(self.dtype)
else:
dtype = dtypes.result_type(self.dtype, fill_value)
if sparse_format is _default:
sparse_format = "coo"
try:
as_sparse = getattr(sparse, f"as_{sparse_format.lower()}")
except AttributeError:
raise ValueError(f"{sparse_format} is not a valid sparse format")
data = as_sparse(self.data.astype(dtype), fill_value=fill_value)
return self._replace(data=data)
def _to_dense(self):
"""
Change backend from sparse to np.array
"""
if hasattr(self._data, "todense"):
return self._replace(data=self._data.todense())
return self.copy(deep=False)
def isel(
self: VariableType,
indexers: Mapping[Hashable, Any] = None,
missing_dims: str = "raise",
**indexers_kwargs: Any,
) -> VariableType:
"""Return a new array indexed along the specified dimension(s).
Parameters
----------
**indexers : {dim: indexer, ...}
Keyword arguments with names matching dimensions and values given
by integers, slice objects or arrays.
missing_dims : {"raise", "warn", "ignore"}, default: "raise"
What to do if dimensions that should be selected from are not present in the
DataArray:
- "raise": raise an exception
- "warning": raise a warning, and ignore the missing dimensions
- "ignore": ignore the missing dimensions
Returns
-------
obj : Array object
A new Array with the selected data and dimensions. In general,
the new variable's data will be a view of this variable's data,
unless numpy fancy indexing was triggered by using an array
indexer, in which case the data will be a copy.
"""
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
indexers = drop_dims_from_indexers(indexers, self.dims, missing_dims)
key = tuple(indexers.get(dim, slice(None)) for dim in self.dims)
return self[key]
def squeeze(self, dim=None):
"""Return a new object with squeezed data.
Parameters
----------
dim : None or str or tuple of str, optional
Selects a subset of the length one dimensions. If a dimension is
selected with length greater than one, an error is raised. If
None, all length one dimensions are squeezed.
Returns
-------
squeezed : same type as caller
This object, but with with all or a subset of the dimensions of
length 1 removed.
See Also
--------
numpy.squeeze
"""
dims = common.get_squeeze_dims(self, dim)
return self.isel({d: 0 for d in dims})
def _shift_one_dim(self, dim, count, fill_value=dtypes.NA):
axis = self.get_axis_num(dim)
if count > 0:
keep = slice(None, -count)
elif count < 0:
keep = slice(-count, None)
else:
keep = slice(None)
trimmed_data = self[(slice(None),) * axis + (keep,)].data
if fill_value is dtypes.NA:
dtype, fill_value = dtypes.maybe_promote(self.dtype)
else:
dtype = self.dtype
width = min(abs(count), self.shape[axis])
dim_pad = (width, 0) if count >= 0 else (0, width)
pads = [(0, 0) if d != dim else dim_pad for d in self.dims]
data = duck_array_ops.pad(
trimmed_data.astype(dtype),
pads,
mode="constant",
constant_values=fill_value,
)
if is_duck_dask_array(data):
# chunked data should come out with the same chunks; this makes
# it feasible to combine shifted and unshifted data
# TODO: remove this once dask.array automatically aligns chunks
data = data.rechunk(self.data.chunks)
return type(self)(self.dims, data, self._attrs, fastpath=True)
def shift(self, shifts=None, fill_value=dtypes.NA, **shifts_kwargs):
"""
Return a new Variable with shifted data.
Parameters
----------
shifts : mapping of the form {dim: offset}
Integer offset to shift along each of the given dimensions.
Positive offsets shift to the right; negative offsets shift to the
left.
fill_value: scalar, optional
Value to use for newly missing values
**shifts_kwargs
The keyword arguments form of ``shifts``.
One of shifts or shifts_kwargs must be provided.
Returns
-------
shifted : Variable
Variable with the same dimensions and attributes but shifted data.
"""
shifts = either_dict_or_kwargs(shifts, shifts_kwargs, "shift")
result = self
for dim, count in shifts.items():
result = result._shift_one_dim(dim, count, fill_value=fill_value)
return result
def _pad_options_dim_to_index(
self,
pad_option: Mapping[Hashable, Union[int, Tuple[int, int]]],
fill_with_shape=False,
):
if fill_with_shape:
return [
(n, n) if d not in pad_option else pad_option[d]
for d, n in zip(self.dims, self.data.shape)
]
return [(0, 0) if d not in pad_option else pad_option[d] for d in self.dims]
def pad(
self,
pad_width: Mapping[Hashable, Union[int, Tuple[int, int]]] = None,
mode: str = "constant",
stat_length: Union[
int, Tuple[int, int], Mapping[Hashable, Tuple[int, int]]
] = None,
constant_values: Union[
int, Tuple[int, int], Mapping[Hashable, Tuple[int, int]]
] = None,
end_values: Union[
int, Tuple[int, int], Mapping[Hashable, Tuple[int, int]]
] = None,
reflect_type: str = None,
**pad_width_kwargs: Any,
):
"""
Return a new Variable with padded data.
Parameters
----------
pad_width : mapping of hashable to tuple of int
Mapping with the form of {dim: (pad_before, pad_after)}
describing the number of values padded along each dimension.
{dim: pad} is a shortcut for pad_before = pad_after = pad
mode : str, default: "constant"
See numpy / Dask docs
stat_length : int, tuple or mapping of hashable to tuple
Used in 'maximum', 'mean', 'median', and 'minimum'. Number of
values at edge of each axis used to calculate the statistic value.
constant_values : scalar, tuple or mapping of hashable to tuple
Used in 'constant'. The values to set the padded values for each
axis.
end_values : scalar, tuple or mapping of hashable to tuple
Used in 'linear_ramp'. The values used for the ending value of the
linear_ramp and that will form the edge of the padded array.
reflect_type : {"even", "odd"}, optional
Used in "reflect", and "symmetric". The "even" style is the
default with an unaltered reflection around the edge value. For
the "odd" style, the extended part of the array is created by
subtracting the reflected values from two times the edge value.
**pad_width_kwargs
One of pad_width or pad_width_kwargs must be provided.
Returns
-------
padded : Variable
Variable with the same dimensions and attributes but padded data.
"""
pad_width = either_dict_or_kwargs(pad_width, pad_width_kwargs, "pad")
# change default behaviour of pad with mode constant
if mode == "constant" and (
constant_values is None or constant_values is dtypes.NA
):
dtype, constant_values = dtypes.maybe_promote(self.dtype)
else:
dtype = self.dtype
# create pad_options_kwargs, numpy requires only relevant kwargs to be nonempty
if isinstance(stat_length, dict):
stat_length = self._pad_options_dim_to_index(
stat_length, fill_with_shape=True
)
if isinstance(constant_values, dict):
constant_values = self._pad_options_dim_to_index(constant_values)
if isinstance(end_values, dict):
end_values = self._pad_options_dim_to_index(end_values)
# workaround for bug in Dask's default value of stat_length https://github.com/dask/dask/issues/5303
if stat_length is None and mode in ["maximum", "mean", "median", "minimum"]:
stat_length = [(n, n) for n in self.data.shape] # type: ignore
# change integer values to a tuple of two of those values and change pad_width to index
for k, v in pad_width.items():
if isinstance(v, numbers.Number):
pad_width[k] = (v, v)
pad_width_by_index = self._pad_options_dim_to_index(pad_width)
# create pad_options_kwargs, numpy/dask requires only relevant kwargs to be nonempty
pad_option_kwargs = {}
if stat_length is not None:
pad_option_kwargs["stat_length"] = stat_length
if constant_values is not None:
pad_option_kwargs["constant_values"] = constant_values
if end_values is not None:
pad_option_kwargs["end_values"] = end_values
if reflect_type is not None:
pad_option_kwargs["reflect_type"] = reflect_type # type: ignore
array = duck_array_ops.pad(
self.data.astype(dtype, copy=False),
pad_width_by_index,
mode=mode,
**pad_option_kwargs,
)
return type(self)(self.dims, array)
def _roll_one_dim(self, dim, count):
axis = self.get_axis_num(dim)
count %= self.shape[axis]
if count != 0:
indices = [slice(-count, None), slice(None, -count)]
else:
indices = [slice(None)]
arrays = [self[(slice(None),) * axis + (idx,)].data for idx in indices]
data = duck_array_ops.concatenate(arrays, axis)
if is_duck_dask_array(data):
# chunked data should come out with the same chunks; this makes
# it feasible to combine shifted and unshifted data
# TODO: remove this once dask.array automatically aligns chunks
data = data.rechunk(self.data.chunks)
return type(self)(self.dims, data, self._attrs, fastpath=True)
def roll(self, shifts=None, **shifts_kwargs):
"""
Return a new Variable with rolld data.
Parameters
----------
shifts : mapping of hashable to int
Integer offset to roll along each of the given dimensions.
Positive offsets roll to the right; negative offsets roll to the
left.
**shifts_kwargs
The keyword arguments form of ``shifts``.
One of shifts or shifts_kwargs must be provided.
Returns
-------
shifted : Variable
Variable with the same dimensions and attributes but rolled data.
"""
shifts = either_dict_or_kwargs(shifts, shifts_kwargs, "roll")
result = self
for dim, count in shifts.items():
result = result._roll_one_dim(dim, count)
return result
def transpose(self, *dims) -> "Variable":
"""Return a new Variable object with transposed dimensions.
Parameters
----------
*dims : str, optional
By default, reverse the dimensions. Otherwise, reorder the
dimensions to this order.
Returns
-------
transposed : Variable
The returned object has transposed data and dimensions with the
same attributes as the original.
Notes
-----
This operation returns a view of this variable's data. It is
lazy for dask-backed Variables but not for numpy-backed Variables.
See Also
--------
numpy.transpose
"""
if len(dims) == 0:
dims = self.dims[::-1]
dims = tuple(infix_dims(dims, self.dims))
axes = self.get_axis_num(dims)
if len(dims) < 2 or dims == self.dims:
# no need to transpose if only one dimension
# or dims are in same order
return self.copy(deep=False)
data = as_indexable(self._data).transpose(axes)
return type(self)(dims, data, self._attrs, self._encoding, fastpath=True)
@property
def T(self) -> "Variable":
return self.transpose()
def set_dims(self, dims, shape=None):
"""Return a new variable with given set of dimensions.
This method might be used to attach new dimension(s) to variable.
When possible, this operation does not copy this variable's data.
Parameters
----------
dims : str or sequence of str or dict
Dimensions to include on the new variable. If a dict, values are
used to provide the sizes of new dimensions; otherwise, new
dimensions are inserted with length 1.
Returns
-------
Variable
"""
if isinstance(dims, str):
dims = [dims]
if shape is None and utils.is_dict_like(dims):
shape = dims.values()
missing_dims = set(self.dims) - set(dims)
if missing_dims:
raise ValueError(
"new dimensions %r must be a superset of "
"existing dimensions %r" % (dims, self.dims)
)
self_dims = set(self.dims)
expanded_dims = tuple(d for d in dims if d not in self_dims) + self.dims
if self.dims == expanded_dims:
# don't use broadcast_to unless necessary so the result remains
# writeable if possible
expanded_data = self.data
elif shape is not None:
dims_map = dict(zip(dims, shape))
tmp_shape = tuple(dims_map[d] for d in expanded_dims)
expanded_data = duck_array_ops.broadcast_to(self.data, tmp_shape)
else:
expanded_data = self.data[(None,) * (len(expanded_dims) - self.ndim)]
expanded_var = Variable(
expanded_dims, expanded_data, self._attrs, self._encoding, fastpath=True
)
return expanded_var.transpose(*dims)
def _stack_once(self, dims, new_dim):
if not set(dims) <= set(self.dims):
raise ValueError("invalid existing dimensions: %s" % dims)
if new_dim in self.dims:
raise ValueError(
"cannot create a new dimension with the same "
"name as an existing dimension"
)
if len(dims) == 0:
# don't stack
return self.copy(deep=False)
other_dims = [d for d in self.dims if d not in dims]
dim_order = other_dims + list(dims)
reordered = self.transpose(*dim_order)
new_shape = reordered.shape[: len(other_dims)] + (-1,)
new_data = reordered.data.reshape(new_shape)
new_dims = reordered.dims[: len(other_dims)] + (new_dim,)
return Variable(new_dims, new_data, self._attrs, self._encoding, fastpath=True)
def stack(self, dimensions=None, **dimensions_kwargs):
"""
Stack any number of existing dimensions into a single new dimension.
New dimensions will be added at the end, and the order of the data
along each new dimension will be in contiguous (C) order.
Parameters
----------
dimensions : mapping of hashable to tuple of hashable
Mapping of form new_name=(dim1, dim2, ...) describing the
names of new dimensions, and the existing dimensions that
they replace.
**dimensions_kwargs
The keyword arguments form of ``dimensions``.
One of dimensions or dimensions_kwargs must be provided.
Returns
-------
stacked : Variable
Variable with the same attributes but stacked data.
See also
--------
Variable.unstack
"""
dimensions = either_dict_or_kwargs(dimensions, dimensions_kwargs, "stack")
result = self
for new_dim, dims in dimensions.items():
result = result._stack_once(dims, new_dim)
return result
def _unstack_once(self, dims, old_dim):
new_dim_names = tuple(dims.keys())
new_dim_sizes = tuple(dims.values())
if old_dim not in self.dims:
raise ValueError("invalid existing dimension: %s" % old_dim)
if set(new_dim_names).intersection(self.dims):
raise ValueError(
"cannot create a new dimension with the same "
"name as an existing dimension"
)
if np.prod(new_dim_sizes) != self.sizes[old_dim]:
raise ValueError(
"the product of the new dimension sizes must "
"equal the size of the old dimension"
)
other_dims = [d for d in self.dims if d != old_dim]
dim_order = other_dims + [old_dim]
reordered = self.transpose(*dim_order)
new_shape = reordered.shape[: len(other_dims)] + new_dim_sizes
new_data = reordered.data.reshape(new_shape)
new_dims = reordered.dims[: len(other_dims)] + new_dim_names
return Variable(new_dims, new_data, self._attrs, self._encoding, fastpath=True)
def unstack(self, dimensions=None, **dimensions_kwargs):
"""
Unstack an existing dimension into multiple new dimensions.
New dimensions will be added at the end, and the order of the data
along each new dimension will be in contiguous (C) order.
Parameters
----------
dimensions : mapping of hashable to mapping of hashable to int
Mapping of the form old_dim={dim1: size1, ...} describing the
names of existing dimensions, and the new dimensions and sizes
that they map to.
**dimensions_kwargs
The keyword arguments form of ``dimensions``.
One of dimensions or dimensions_kwargs must be provided.
Returns
-------
unstacked : Variable
Variable with the same attributes but unstacked data.
See also
--------
Variable.stack
"""
dimensions = either_dict_or_kwargs(dimensions, dimensions_kwargs, "unstack")
result = self
for old_dim, dims in dimensions.items():
result = result._unstack_once(dims, old_dim)
return result
def fillna(self, value):
return ops.fillna(self, value)
def where(self, cond, other=dtypes.NA):
return ops.where_method(self, cond, other)
def reduce(
self,
func,
dim=None,
axis=None,
keep_attrs=None,
keepdims=False,
**kwargs,
):
"""Reduce this array by applying `func` along some dimension(s).
Parameters
----------
func : callable
Function which can be called in the form
`func(x, axis=axis, **kwargs)` to return the result of reducing an
np.ndarray over an integer valued axis.
dim : str or sequence of str, optional
Dimension(s) over which to apply `func`.
axis : int or sequence of int, optional
Axis(es) over which to apply `func`. Only one of the 'dim'
and 'axis' arguments can be supplied. If neither are supplied, then
the reduction is calculated over the flattened array (by calling
`func(x)` without an axis argument).
keep_attrs : bool, optional
If True, the variable's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
object will be returned without attributes.
keepdims : bool, default: False
If True, the dimensions which are reduced are left in the result
as dimensions of size one
**kwargs : dict
Additional keyword arguments passed on to `func`.
Returns
-------
reduced : Array
Array with summarized data and the indicated dimension(s)
removed.
"""
if dim == ...:
dim = None
if dim is not None and axis is not None:
raise ValueError("cannot supply both 'axis' and 'dim' arguments")
if dim is not None:
axis = self.get_axis_num(dim)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", r"Mean of empty slice", category=RuntimeWarning
)
if axis is not None:
data = func(self.data, axis=axis, **kwargs)
else:
data = func(self.data, **kwargs)
if getattr(data, "shape", ()) == self.shape:
dims = self.dims
else:
removed_axes = (
range(self.ndim) if axis is None else np.atleast_1d(axis) % self.ndim
)
if keepdims:
# Insert np.newaxis for removed dims
slices = tuple(
np.newaxis if i in removed_axes else slice(None, None)
for i in range(self.ndim)
)
if getattr(data, "shape", None) is None:
# Reduce has produced a scalar value, not an array-like
data = np.asanyarray(data)[slices]
else:
data = data[slices]
dims = self.dims
else:
dims = [
adim for n, adim in enumerate(self.dims) if n not in removed_axes
]
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
attrs = self._attrs if keep_attrs else None
return Variable(dims, data, attrs=attrs)
@classmethod
def concat(cls, variables, dim="concat_dim", positions=None, shortcut=False):
"""Concatenate variables along a new or existing dimension.
Parameters
----------
variables : iterable of Variable
Arrays to stack together. Each variable is expected to have
matching dimensions and shape except for along the stacked
dimension.
dim : str or DataArray, optional
Name of the dimension to stack along. This can either be a new
dimension name, in which case it is added along axis=0, or an
existing dimension name, in which case the location of the
dimension is unchanged. Where to insert the new dimension is
determined by the first variable.
positions : None or list of array-like, optional
List of integer arrays which specifies the integer positions to
which to assign each dataset along the concatenated dimension.
If not supplied, objects are concatenated in the provided order.
shortcut : bool, optional
This option is used internally to speed-up groupby operations.
If `shortcut` is True, some checks of internal consistency between
arrays to concatenate are skipped.
Returns
-------
stacked : Variable
Concatenated Variable formed by stacking all the supplied variables
along the given dimension.
"""
if not isinstance(dim, str):
(dim,) = dim.dims
# can't do this lazily: we need to loop through variables at least
# twice
variables = list(variables)
first_var = variables[0]
arrays = [v.data for v in variables]
if dim in first_var.dims:
axis = first_var.get_axis_num(dim)
dims = first_var.dims
data = duck_array_ops.concatenate(arrays, axis=axis)
if positions is not None:
# TODO: deprecate this option -- we don't need it for groupby
# any more.
indices = nputils.inverse_permutation(np.concatenate(positions))
data = duck_array_ops.take(data, indices, axis=axis)
else:
axis = 0
dims = (dim,) + first_var.dims
data = duck_array_ops.stack(arrays, axis=axis)
attrs = dict(first_var.attrs)
encoding = dict(first_var.encoding)
if not shortcut:
for var in variables:
if var.dims != first_var.dims:
raise ValueError(
f"Variable has dimensions {list(var.dims)} but first Variable has dimensions {list(first_var.dims)}"
)
return cls(dims, data, attrs, encoding)
def equals(self, other, equiv=duck_array_ops.array_equiv):
"""True if two Variables have the same dimensions and values;
otherwise False.
Variables can still be equal (like pandas objects) if they have NaN
values in the same locations.
This method is necessary because `v1 == v2` for Variables
does element-wise comparisons (like numpy.ndarrays).
"""
other = getattr(other, "variable", other)
try:
return self.dims == other.dims and (
self._data is other._data or equiv(self.data, other.data)
)
except (TypeError, AttributeError):
return False
def broadcast_equals(self, other, equiv=duck_array_ops.array_equiv):
"""True if two Variables have the values after being broadcast against
each other; otherwise False.
Variables can still be equal (like pandas objects) if they have NaN
values in the same locations.
"""
try:
self, other = broadcast_variables(self, other)
except (ValueError, AttributeError):
return False
return self.equals(other, equiv=equiv)
def identical(self, other, equiv=duck_array_ops.array_equiv):
"""Like equals, but also checks attributes."""
try:
return utils.dict_equiv(self.attrs, other.attrs) and self.equals(
other, equiv=equiv
)
except (TypeError, AttributeError):
return False
def no_conflicts(self, other, equiv=duck_array_ops.array_notnull_equiv):
"""True if the intersection of two Variable's non-null data is
equal; otherwise false.
Variables can thus still be equal if there are locations where either,
or both, contain NaN values.
"""
return self.broadcast_equals(other, equiv=equiv)
def quantile(
self, q, dim=None, interpolation="linear", keep_attrs=None, skipna=True
):
"""Compute the qth quantile of the data along the specified dimension.
Returns the qth quantiles(s) of the array elements.
Parameters
----------
q : float or sequence of float
Quantile to compute, which must be between 0 and 1
inclusive.
dim : str or sequence of str, optional
Dimension(s) over which to apply quantile.
interpolation : {"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:
* linear: ``i + (j - i) * fraction``, where ``fraction`` is
the fractional part of the index surrounded by ``i`` and
``j``.
* lower: ``i``.
* higher: ``j``.
* nearest: ``i`` or ``j``, whichever is nearest.
* midpoint: ``(i + j) / 2``.
keep_attrs : bool, optional
If True, the variable's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
object will be returned without attributes.
Returns
-------
quantiles : Variable
If `q` is a single quantile, then the result
is a scalar. If multiple percentiles are given, first axis of
the result corresponds to the quantile and a quantile dimension
is added to the return array. The other dimensions are the
dimensions that remain after the reduction of the array.
See Also
--------
numpy.nanquantile, pandas.Series.quantile, Dataset.quantile,
DataArray.quantile
"""
from .computation import apply_ufunc
_quantile_func = np.nanquantile if skipna else np.quantile
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
scalar = utils.is_scalar(q)
q = np.atleast_1d(np.asarray(q, dtype=np.float64))
if dim is None:
dim = self.dims
if utils.is_scalar(dim):
dim = [dim]
def _wrapper(npa, **kwargs):
# move quantile axis to end. required for apply_ufunc
return np.moveaxis(_quantile_func(npa, **kwargs), 0, -1)
axis = np.arange(-1, -1 * len(dim) - 1, -1)
result = apply_ufunc(
_wrapper,
self,
input_core_dims=[dim],
exclude_dims=set(dim),
output_core_dims=[["quantile"]],
output_dtypes=[np.float64],
dask_gufunc_kwargs=dict(output_sizes={"quantile": len(q)}),
dask="parallelized",
kwargs={"q": q, "axis": axis, "interpolation": interpolation},
)
# for backward compatibility
result = result.transpose("quantile", ...)
if scalar:
result = result.squeeze("quantile")
if keep_attrs:
result.attrs = self._attrs
return result
def rank(self, dim, pct=False):
"""Ranks the data.
Equal values are assigned a rank that is the average of the ranks that
would have been otherwise assigned to all of the values within that
set. Ranks begin at 1, not 0. If `pct`, computes percentage ranks.
NaNs in the input array are returned as NaNs.
The `bottleneck` library is required.
Parameters
----------
dim : str
Dimension over which to compute rank.
pct : bool, optional
If True, compute percentage ranks, otherwise compute integer ranks.
Returns
-------
ranked : Variable
See Also
--------
Dataset.rank, DataArray.rank
"""
import bottleneck as bn
data = self.data
if is_duck_dask_array(data):
raise TypeError(
"rank does not work for arrays stored as dask "
"arrays. Load the data via .compute() or .load() "
"prior to calling this method."
)
elif not isinstance(data, np.ndarray):
raise TypeError(
"rank is not implemented for {} objects.".format(type(data))
)
axis = self.get_axis_num(dim)
func = bn.nanrankdata if self.dtype.kind == "f" else bn.rankdata
ranked = func(data, axis=axis)
if pct:
count = np.sum(~np.isnan(data), axis=axis, keepdims=True)
ranked /= count
return Variable(self.dims, ranked)
def rolling_window(
self, dim, window, window_dim, center=False, fill_value=dtypes.NA
):
"""
Make a rolling_window along dim and add a new_dim to the last place.
Parameters
----------
dim : str
Dimension over which to compute rolling_window.
For nd-rolling, should be list of dimensions.
window : int
Window size of the rolling
For nd-rolling, should be list of integers.
window_dim : str
New name of the window dimension.
For nd-rolling, should be list of integers.
center : bool, default: False
If True, pad fill_value for both ends. Otherwise, pad in the head
of the axis.
fill_value
value to be filled.
Returns
-------
Variable that is a view of the original array with a added dimension of
size w.
The return dim: self.dims + (window_dim, )
The return shape: self.shape + (window, )
Examples
--------
>>> v = Variable(("a", "b"), np.arange(8).reshape((2, 4)))
>>> v.rolling_window("b", 3, "window_dim")
<xarray.Variable (a: 2, b: 4, window_dim: 3)>
array([[[nan, nan, 0.],
[nan, 0., 1.],
[ 0., 1., 2.],
[ 1., 2., 3.]],
<BLANKLINE>
[[nan, nan, 4.],
[nan, 4., 5.],
[ 4., 5., 6.],
[ 5., 6., 7.]]])
>>> v.rolling_window("b", 3, "window_dim", center=True)
<xarray.Variable (a: 2, b: 4, window_dim: 3)>
array([[[nan, 0., 1.],
[ 0., 1., 2.],
[ 1., 2., 3.],
[ 2., 3., nan]],
<BLANKLINE>
[[nan, 4., 5.],
[ 4., 5., 6.],
[ 5., 6., 7.],
[ 6., 7., nan]]])
"""
if fill_value is dtypes.NA: # np.nan is passed
dtype, fill_value = dtypes.maybe_promote(self.dtype)
array = self.astype(dtype, copy=False).data
else:
dtype = self.dtype
array = self.data
if isinstance(dim, list):
assert len(dim) == len(window)
assert len(dim) == len(window_dim)
assert len(dim) == len(center)
else:
dim = [dim]
window = [window]
window_dim = [window_dim]
center = [center]
axis = [self.get_axis_num(d) for d in dim]
new_dims = self.dims + tuple(window_dim)
return Variable(
new_dims,
duck_array_ops.rolling_window(
array, axis=axis, window=window, center=center, fill_value=fill_value
),
)
def coarsen(
self, windows, func, boundary="exact", side="left", keep_attrs=None, **kwargs
):
"""
Apply reduction function.
"""
windows = {k: v for k, v in windows.items() if k in self.dims}
if not windows:
return self.copy()
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
if keep_attrs:
_attrs = self.attrs
else:
_attrs = None
reshaped, axes = self._coarsen_reshape(windows, boundary, side)
if isinstance(func, str):
name = func
func = getattr(duck_array_ops, name, None)
if func is None:
raise NameError(f"{name} is not a valid method.")
return self._replace(data=func(reshaped, axis=axes, **kwargs), attrs=_attrs)
def _coarsen_reshape(self, windows, boundary, side):
"""
Construct a reshaped-array for coarsen
"""
if not utils.is_dict_like(boundary):
boundary = {d: boundary for d in windows.keys()}
if not utils.is_dict_like(side):
side = {d: side for d in windows.keys()}
# remove unrelated dimensions
boundary = {k: v for k, v in boundary.items() if k in windows}
side = {k: v for k, v in side.items() if k in windows}
for d, window in windows.items():
if window <= 0:
raise ValueError(f"window must be > 0. Given {window}")
variable = self
for d, window in windows.items():
# trim or pad the object
size = variable.shape[self._get_axis_num(d)]
n = int(size / window)
if boundary[d] == "exact":
if n * window != size:
raise ValueError(
"Could not coarsen a dimension of size {} with "
"window {}".format(size, window)
)
elif boundary[d] == "trim":
if side[d] == "left":
variable = variable.isel({d: slice(0, window * n)})
else:
excess = size - window * n
variable = variable.isel({d: slice(excess, None)})
elif boundary[d] == "pad": # pad
pad = window * n - size
if pad < 0:
pad += window
if side[d] == "left":
pad_width = {d: (0, pad)}
else:
pad_width = {d: (pad, 0)}
variable = variable.pad(pad_width, mode="constant")
else:
raise TypeError(
"{} is invalid for boundary. Valid option is 'exact', "
"'trim' and 'pad'".format(boundary[d])
)
shape = []
axes = []
axis_count = 0
for i, d in enumerate(variable.dims):
if d in windows:
size = variable.shape[i]
shape.append(int(size / windows[d]))
shape.append(windows[d])
axis_count += 1
axes.append(i + axis_count)
else:
shape.append(variable.shape[i])
return variable.data.reshape(shape), tuple(axes)
def isnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is a missing value.
Returns
-------
isnull : Variable
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.isnull
Examples
--------
>>> var = xr.Variable("x", [1, np.nan, 3])
>>> var
<xarray.Variable (x: 3)>
array([ 1., nan, 3.])
>>> var.isnull()
<xarray.Variable (x: 3)>
array([False, True, False])
"""
from .computation import apply_ufunc
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
return apply_ufunc(
duck_array_ops.isnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)
def notnull(self, keep_attrs: bool = None):
"""Test each value in the array for whether it is not a missing value.
Returns
-------
notnull : Variable
Same type and shape as object, but the dtype of the data is bool.
See Also
--------
pandas.notnull
Examples
--------
>>> var = xr.Variable("x", [1, np.nan, 3])
>>> var
<xarray.Variable (x: 3)>
array([ 1., nan, 3.])
>>> var.notnull()
<xarray.Variable (x: 3)>
array([ True, False, True])
"""
from .computation import apply_ufunc
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
return apply_ufunc(
duck_array_ops.notnull,
self,
dask="allowed",
keep_attrs=keep_attrs,
)
@property
def real(self):
return type(self)(self.dims, self.data.real, self._attrs)
@property
def imag(self):
return type(self)(self.dims, self.data.imag, self._attrs)
def __array_wrap__(self, obj, context=None):
return Variable(self.dims, obj)
@staticmethod
def _unary_op(f):
@functools.wraps(f)
def func(self, *args, **kwargs):
keep_attrs = kwargs.pop("keep_attrs", None)
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)
with np.errstate(all="ignore"):
result = self.__array_wrap__(f(self.data, *args, **kwargs))
if keep_attrs:
result.attrs = self.attrs
return result
return func
@staticmethod
def _binary_op(f, reflexive=False, **ignored_kwargs):
@functools.wraps(f)
def func(self, other):
if isinstance(other, (xr.DataArray, xr.Dataset)):
return NotImplemented
self_data, other_data, dims = _broadcast_compat_data(self, other)
keep_attrs = _get_keep_attrs(default=False)
attrs = self._attrs if keep_attrs else None
with np.errstate(all="ignore"):
new_data = (
f(self_data, other_data)
if not reflexive
else f(other_data, self_data)
)
result = Variable(dims, new_data, attrs=attrs)
return result
return func
@staticmethod
def _inplace_binary_op(f):
@functools.wraps(f)
def func(self, other):
if isinstance(other, xr.Dataset):
raise TypeError("cannot add a Dataset to a Variable in-place")
self_data, other_data, dims = _broadcast_compat_data(self, other)
if dims != self.dims:
raise ValueError("dimensions cannot change for in-place operations")
with np.errstate(all="ignore"):
self.values = f(self_data, other_data)
return self
return func
def _to_numeric(self, offset=None, datetime_unit=None, dtype=float):
"""A (private) method to convert datetime array to numeric dtype
See duck_array_ops.datetime_to_numeric
"""
numeric_array = duck_array_ops.datetime_to_numeric(
self.data, offset, datetime_unit, dtype
)
return type(self)(self.dims, numeric_array, self._attrs)
def _unravel_argminmax(
self,
argminmax: str,
dim: Union[Hashable, Sequence[Hashable], None],
axis: Union[int, None],
keep_attrs: Optional[bool],
skipna: Optional[bool],
) -> Union["Variable", Dict[Hashable, "Variable"]]:
"""Apply argmin or argmax over one or more dimensions, returning the result as a
dict of DataArray that can be passed directly to isel.
"""
if dim is None and axis is None:
warnings.warn(
"Behaviour of argmin/argmax with neither dim nor axis argument will "
"change to return a dict of indices of each dimension. To get a "
"single, flat index, please use np.argmin(da.data) or "
"np.argmax(da.data) instead of da.argmin() or da.argmax().",
DeprecationWarning,
stacklevel=3,
)
argminmax_func = getattr(duck_array_ops, argminmax)
if dim is ...:
# In future, should do this also when (dim is None and axis is None)
dim = self.dims
if (
dim is None
or axis is not None
or not isinstance(dim, Sequence)
or isinstance(dim, str)
):
# Return int index if single dimension is passed, and is not part of a
# sequence
return self.reduce(
argminmax_func, dim=dim, axis=axis, keep_attrs=keep_attrs, skipna=skipna
)
# Get a name for the new dimension that does not conflict with any existing
# dimension
newdimname = "_unravel_argminmax_dim_0"
count = 1
while newdimname in self.dims:
newdimname = f"_unravel_argminmax_dim_{count}"
count += 1
stacked = self.stack({newdimname: dim})
result_dims = stacked.dims[:-1]
reduce_shape = tuple(self.sizes[d] for d in dim)
result_flat_indices = stacked.reduce(argminmax_func, axis=-1, skipna=skipna)
result_unravelled_indices = duck_array_ops.unravel_index(
result_flat_indices.data, reduce_shape
)
result = {
d: Variable(dims=result_dims, data=i)
for d, i in zip(dim, result_unravelled_indices)
}
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
if keep_attrs:
for v in result.values():
v.attrs = self.attrs
return result
def argmin(
self,
dim: Union[Hashable, Sequence[Hashable]] = None,
axis: int = None,
keep_attrs: bool = None,
skipna: bool = None,
) -> Union["Variable", Dict[Hashable, "Variable"]]:
"""Index or indices of the minimum of the Variable over one or more dimensions.
If a sequence is passed to 'dim', then result returned as dict of Variables,
which can be passed directly to isel(). If a single str is passed to 'dim' then
returns a Variable with dtype int.
If there are multiple minima, the indices of the first one found will be
returned.
Parameters
----------
dim : hashable, sequence of hashable or ..., optional
The dimensions over which to find the minimum. By default, finds minimum over
all dimensions - for now returning an int for backward compatibility, but
this is deprecated, in future will return a dict with indices for all
dimensions; to return a dict with all dimensions now, pass '...'.
axis : int, optional
Axis over which to apply `argmin`. Only one of the 'dim' and 'axis' arguments
can be supplied.
keep_attrs : bool, optional
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
skipna : bool, optional
If True, skip missing values (as marked by NaN). By default, only
skips missing values for float dtypes; other dtypes either do not
have a sentinel missing value (int) or skipna=True has not been
implemented (object, datetime64 or timedelta64).
Returns
-------
result : Variable or dict of Variable
See also
--------
DataArray.argmin, DataArray.idxmin
"""
return self._unravel_argminmax("argmin", dim, axis, keep_attrs, skipna)
def argmax(
self,
dim: Union[Hashable, Sequence[Hashable]] = None,
axis: int = None,
keep_attrs: bool = None,
skipna: bool = None,
) -> Union["Variable", Dict[Hashable, "Variable"]]:
"""Index or indices of the maximum of the Variable over one or more dimensions.
If a sequence is passed to 'dim', then result returned as dict of Variables,
which can be passed directly to isel(). If a single str is passed to 'dim' then
returns a Variable with dtype int.
If there are multiple maxima, the indices of the first one found will be
returned.
Parameters
----------
dim : hashable, sequence of hashable or ..., optional
The dimensions over which to find the maximum. By default, finds maximum over
all dimensions - for now returning an int for backward compatibility, but
this is deprecated, in future will return a dict with indices for all
dimensions; to return a dict with all dimensions now, pass '...'.
axis : int, optional
Axis over which to apply `argmin`. Only one of the 'dim' and 'axis' arguments
can be supplied.
keep_attrs : bool, optional
If True, the attributes (`attrs`) will be copied from the original
object to the new one. If False (default), the new object will be
returned without attributes.
skipna : bool, optional
If True, skip missing values (as marked by NaN). By default, only
skips missing values for float dtypes; other dtypes either do not
have a sentinel missing value (int) or skipna=True has not been
implemented (object, datetime64 or timedelta64).
Returns
-------
result : Variable or dict of Variable
See also
--------
DataArray.argmax, DataArray.idxmax
"""
return self._unravel_argminmax("argmax", dim, axis, keep_attrs, skipna)
ops.inject_all_ops_and_reduce_methods(Variable)
class IndexVariable(Variable):
"""Wrapper for accommodating a pandas.Index in an xarray.Variable.
IndexVariable preserve loaded values in the form of a pandas.Index instead
of a NumPy array. Hence, their values are immutable and must always be one-
dimensional.
They also have a name property, which is the name of their sole dimension
unless another name is given.
"""
__slots__ = ()
def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False):
super().__init__(dims, data, attrs, encoding, fastpath)
if self.ndim != 1:
raise ValueError("%s objects must be 1-dimensional" % type(self).__name__)
# Unlike in Variable, always eagerly load values into memory
if not isinstance(self._data, PandasIndexAdapter):
self._data = PandasIndexAdapter(self._data)
def __dask_tokenize__(self):
from dask.base import normalize_token
# Don't waste time converting pd.Index to np.ndarray
return normalize_token((type(self), self._dims, self._data.array, self._attrs))
def load(self):
# data is already loaded into memory for IndexVariable
return self
# https://github.com/python/mypy/issues/1465
@Variable.data.setter # type: ignore
def data(self, data):
raise ValueError(
f"Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
)
@Variable.values.setter # type: ignore
def values(self, values):
raise ValueError(
f"Cannot assign to the .values attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
)
def chunk(self, chunks={}, name=None, lock=False):
# Dummy - do not chunk. This method is invoked e.g. by Dataset.chunk()
return self.copy(deep=False)
def _as_sparse(self, sparse_format=_default, fill_value=_default):
# Dummy
return self.copy(deep=False)
def _to_dense(self):
# Dummy
return self.copy(deep=False)
def _finalize_indexing_result(self, dims, data):
if getattr(data, "ndim", 0) != 1:
# returns Variable rather than IndexVariable if multi-dimensional
return Variable(dims, data, self._attrs, self._encoding)
else:
return type(self)(dims, data, self._attrs, self._encoding, fastpath=True)
def __setitem__(self, key, value):
raise TypeError("%s values cannot be modified" % type(self).__name__)
@classmethod
def concat(cls, variables, dim="concat_dim", positions=None, shortcut=False):
"""Specialized version of Variable.concat for IndexVariable objects.
This exists because we want to avoid converting Index objects to NumPy
arrays, if possible.
"""
if not isinstance(dim, str):
(dim,) = dim.dims
variables = list(variables)
first_var = variables[0]
if any(not isinstance(v, cls) for v in variables):
raise TypeError(
"IndexVariable.concat requires that all input "
"variables be IndexVariable objects"
)
indexes = [v._data.array for v in variables]
if not indexes:
data = []
else:
data = indexes[0].append(indexes[1:])
if positions is not None:
indices = nputils.inverse_permutation(np.concatenate(positions))
data = data.take(indices)
attrs = dict(first_var.attrs)
if not shortcut:
for var in variables:
if var.dims != first_var.dims:
raise ValueError("inconsistent dimensions")
utils.remove_incompatible_items(attrs, var.attrs)
return cls(first_var.dims, data, attrs)
def copy(self, deep=True, data=None):
"""Returns a copy of this object.
`deep` is ignored since data is stored in the form of
pandas.Index, which is already immutable. Dimensions, attributes
and encodings are always copied.
Use `data` to create a new object with the same structure as
original but entirely new data.
Parameters
----------
deep : bool, optional
Deep is ignored when data is given. Whether the data array is
loaded into memory and copied onto the new object. Default is True.
data : array_like, optional
Data to use in the new object. Must have same shape as original.
Returns
-------
object : Variable
New object with dimensions, attributes, encodings, and optionally
data copied from original.
"""
if data is None:
data = self._data.copy(deep=deep)
else:
data = as_compatible_data(data)
if self.shape != data.shape:
raise ValueError(
"Data shape {} must match shape of object {}".format(
data.shape, self.shape
)
)
return type(self)(self.dims, data, self._attrs, self._encoding, fastpath=True)
def equals(self, other, equiv=None):
# if equiv is specified, super up
if equiv is not None:
return super().equals(other, equiv)
# otherwise use the native index equals, rather than looking at _data
other = getattr(other, "variable", other)
try:
return self.dims == other.dims and self._data_equals(other)
except (TypeError, AttributeError):
return False
def _data_equals(self, other):
return self.to_index().equals(other.to_index())
def to_index_variable(self):
"""Return this variable as an xarray.IndexVariable"""
return self
to_coord = utils.alias(to_index_variable, "to_coord")
def to_index(self):
"""Convert this variable to a pandas.Index"""
# n.b. creating a new pandas.Index from an old pandas.Index is
# basically free as pandas.Index objects are immutable
assert self.ndim == 1
index = self._data.array
if isinstance(index, pd.MultiIndex):
# set default names for multi-index unnamed levels so that
# we can safely rename dimension / coordinate later
valid_level_names = [
name or "{}_level_{}".format(self.dims[0], i)
for i, name in enumerate(index.names)
]
index = index.set_names(valid_level_names)
else:
index = index.set_names(self.name)
return index
@property
def level_names(self):
"""Return MultiIndex level names or None if this IndexVariable has no
MultiIndex.
"""
index = self.to_index()
if isinstance(index, pd.MultiIndex):
return index.names
else:
return None
def get_level_variable(self, level):
"""Return a new IndexVariable from a given MultiIndex level."""
if self.level_names is None:
raise ValueError("IndexVariable %r has no MultiIndex" % self.name)
index = self.to_index()
return type(self)(self.dims, index.get_level_values(level))
@property
def name(self):
return self.dims[0]
@name.setter
def name(self, value):
raise AttributeError("cannot modify name of IndexVariable in-place")
# for backwards compatibility
Coordinate = utils.alias(IndexVariable, "Coordinate")
def _unified_dims(variables):
# validate dimensions
all_dims = {}
for var in variables:
var_dims = var.dims
if len(set(var_dims)) < len(var_dims):
raise ValueError(
"broadcasting cannot handle duplicate "
"dimensions: %r" % list(var_dims)
)
for d, s in zip(var_dims, var.shape):
if d not in all_dims:
all_dims[d] = s
elif all_dims[d] != s:
raise ValueError(
"operands cannot be broadcast together "
"with mismatched lengths for dimension %r: %s"
% (d, (all_dims[d], s))
)
return all_dims
def _broadcast_compat_variables(*variables):
"""Create broadcast compatible variables, with the same dimensions.
Unlike the result of broadcast_variables(), some variables may have
dimensions of size 1 instead of the the size of the broadcast dimension.
"""
dims = tuple(_unified_dims(variables))
return tuple(var.set_dims(dims) if var.dims != dims else var for var in variables)
def broadcast_variables(*variables):
"""Given any number of variables, return variables with matching dimensions
and broadcast data.
The data on the returned variables will be a view of the data on the
corresponding original arrays, but dimensions will be reordered and
inserted so that both broadcast arrays have the same dimensions. The new
dimensions are sorted in order of appearance in the first variable's
dimensions followed by the second variable's dimensions.
"""
dims_map = _unified_dims(variables)
dims_tuple = tuple(dims_map)
return tuple(
var.set_dims(dims_map) if var.dims != dims_tuple else var for var in variables
)
def _broadcast_compat_data(self, other):
if all(hasattr(other, attr) for attr in ["dims", "data", "shape", "encoding"]):
# `other` satisfies the necessary Variable API for broadcast_variables
new_self, new_other = _broadcast_compat_variables(self, other)
self_data = new_self.data
other_data = new_other.data
dims = new_self.dims
else:
# rely on numpy broadcasting rules
self_data = self.data
other_data = other
dims = self.dims
return self_data, other_data, dims
def concat(variables, dim="concat_dim", positions=None, shortcut=False):
"""Concatenate variables along a new or existing dimension.
Parameters
----------
variables : iterable of Variable
Arrays to stack together. Each variable is expected to have
matching dimensions and shape except for along the stacked
dimension.
dim : str or DataArray, optional
Name of the dimension to stack along. This can either be a new
dimension name, in which case it is added along axis=0, or an
existing dimension name, in which case the location of the
dimension is unchanged. Where to insert the new dimension is
determined by the first variable.
positions : None or list of array-like, optional
List of integer arrays which specifies the integer positions to which
to assign each dataset along the concatenated dimension. If not
supplied, objects are concatenated in the provided order.
shortcut : bool, optional
This option is used internally to speed-up groupby operations.
If `shortcut` is True, some checks of internal consistency between
arrays to concatenate are skipped.
Returns
-------
stacked : Variable
Concatenated Variable formed by stacking all the supplied variables
along the given dimension.
"""
variables = list(variables)
if all(isinstance(v, IndexVariable) for v in variables):
return IndexVariable.concat(variables, dim, positions, shortcut)
else:
return Variable.concat(variables, dim, positions, shortcut)
def assert_unique_multiindex_level_names(variables):
"""Check for uniqueness of MultiIndex level names in all given
variables.
Not public API. Used for checking consistency of DataArray and Dataset
objects.
"""
level_names = defaultdict(list)
all_level_names = set()
for var_name, var in variables.items():
if isinstance(var._data, PandasIndexAdapter):
idx_level_names = var.to_index_variable().level_names
if idx_level_names is not None:
for n in idx_level_names:
level_names[n].append(f"{n!r} ({var_name})")
if idx_level_names:
all_level_names.update(idx_level_names)
for k, v in level_names.items():
if k in variables:
v.append("(%s)" % k)
duplicate_names = [v for v in level_names.values() if len(v) > 1]
if duplicate_names:
conflict_str = "\n".join(", ".join(v) for v in duplicate_names)
raise ValueError("conflicting MultiIndex level name(s):\n%s" % conflict_str)
# Check confliction between level names and dimensions GH:2299
for k, v in variables.items():
for d in v.dims:
if d in all_level_names:
raise ValueError(
"conflicting level / dimension names. {} "
"already exists as a level name.".format(d)
)
| [((1294, 1335), 'typing.TypeVar', 'TypeVar', (['"""VariableType"""'], {'bound': '"""Variable"""'}), "('VariableType', bound='Variable')\n", (1301, 1335), False, 'from typing import Any, Dict, Hashable, Mapping, Optional, Sequence, Tuple, TypeVar, Union\n'), ((7749, 7765), 'numpy.asarray', 'np.asarray', (['data'], {}), '(data)\n', (7759, 7765), True, 'import numpy as np\n'), ((36561, 36578), 'itertools.count', 'itertools.count', ([], {}), '()\n', (36576, 36578), False, 'import itertools\n'), ((100638, 100655), 'collections.defaultdict', 'defaultdict', (['list'], {}), '(list)\n', (100649, 100655), False, 'from collections import defaultdict\n'), ((6610, 6641), 'numpy.datetime64', 'np.datetime64', (['data.value', '"""ns"""'], {}), "(data.value, 'ns')\n", (6623, 6641), True, 'import numpy as np\n'), ((6896, 6920), 'numpy.ma.getmaskarray', 'np.ma.getmaskarray', (['data'], {}), '(data)\n', (6914, 6920), True, 'import numpy as np\n'), ((8788, 8804), 'numpy.asarray', 'np.asarray', (['data'], {}), '(data)\n', (8798, 8804), True, 'import numpy as np\n'), ((25611, 25632), 'numpy.nonzero', 'np.nonzero', (['self.data'], {}), '(self.data)\n', (25621, 25632), True, 'import numpy as np\n'), ((79843, 79861), 'functools.wraps', 'functools.wraps', (['f'], {}), '(f)\n', (79858, 79861), False, 'import functools\n'), ((80387, 80405), 'functools.wraps', 'functools.wraps', (['f'], {}), '(f)\n', (80402, 80405), False, 'import functools\n'), ((81115, 81133), 'functools.wraps', 'functools.wraps', (['f'], {}), '(f)\n', (81130, 81133), False, 'import functools\n'), ((7028, 7057), 'numpy.asarray', 'np.asarray', (['data'], {'dtype': 'dtype'}), '(data, dtype=dtype)\n', (7038, 7057), True, 'import numpy as np\n'), ((7127, 7143), 'numpy.asarray', 'np.asarray', (['data'], {}), '(data)\n', (7137, 7143), True, 'import numpy as np\n'), ((8882, 8907), 'numpy.datetime64', 'np.datetime64', (['data', '"""ns"""'], {}), "(data, 'ns')\n", (8895, 8907), True, 'import numpy as np\n'), ((35671, 35692), 'copy.copy', 'copy.copy', (['self._dims'], {}), '(self._dims)\n', (35680, 35692), False, 'import copy\n'), ((35741, 35761), 'copy.copy', 'copy.copy', (['self.data'], {}), '(self.data)\n', (35750, 35761), False, 'import copy\n'), ((35812, 35834), 'copy.copy', 'copy.copy', (['self._attrs'], {}), '(self._attrs)\n', (35821, 35834), False, 'import copy\n'), ((35891, 35916), 'copy.copy', 'copy.copy', (['self._encoding'], {}), '(self._encoding)\n', (35900, 35916), False, 'import copy\n'), ((37746, 37890), 'warnings.warn', 'warnings.warn', (['"""None value for \'chunks\' is deprecated. It will raise an error in the future. Use instead \'{}\'"""'], {'category': 'FutureWarning'}), '(\n "None value for \'chunks\' is deprecated. It will raise an error in the future. Use instead \'{}\'"\n , category=FutureWarning)\n', (37759, 37890), False, 'import warnings\n'), ((39504, 39563), 'dask.array.from_array', 'da.from_array', (['data', 'chunks'], {'name': 'name', 'lock': 'lock'}), '(data, chunks, name=name, lock=lock, **kwargs)\n', (39517, 39563), True, 'import dask.array as da\n'), ((56994, 57016), 'numpy.prod', 'np.prod', (['new_dim_sizes'], {}), '(new_dim_sizes)\n', (57001, 57016), True, 'import numpy as np\n'), ((60768, 60793), 'warnings.catch_warnings', 'warnings.catch_warnings', ([], {}), '()\n', (60791, 60793), False, 'import warnings\n'), ((60807, 60893), 'warnings.filterwarnings', 'warnings.filterwarnings', (['"""ignore"""', '"""Mean of empty slice"""'], {'category': 'RuntimeWarning'}), "('ignore', 'Mean of empty slice', category=\n RuntimeWarning)\n", (60830, 60893), False, 'import warnings\n'), ((69068, 69099), 'numpy.asarray', 'np.asarray', (['q'], {'dtype': 'np.float64'}), '(q, dtype=np.float64)\n', (69078, 69099), True, 'import numpy as np\n'), ((82520, 82821), 'warnings.warn', 'warnings.warn', (['"""Behaviour of argmin/argmax with neither dim nor axis argument will change to return a dict of indices of each dimension. To get a single, flat index, please use np.argmin(da.data) or np.argmax(da.data) instead of da.argmin() or da.argmax()."""', 'DeprecationWarning'], {'stacklevel': '(3)'}), "(\n 'Behaviour of argmin/argmax with neither dim nor axis argument will change to return a dict of indices of each dimension. To get a single, flat index, please use np.argmin(da.data) or np.argmax(da.data) instead of da.argmin() or da.argmax().'\n , DeprecationWarning, stacklevel=3)\n", (82533, 82821), False, 'import warnings\n'), ((8964, 8990), 'numpy.timedelta64', 'np.timedelta64', (['data', '"""ns"""'], {}), "(data, 'ns')\n", (8978, 8990), True, 'import numpy as np\n'), ((16262, 16284), 'numpy.asarray', 'np.asarray', (['self._data'], {}), '(self._data)\n', (16272, 16284), True, 'import numpy as np\n'), ((25034, 25047), 'numpy.asarray', 'np.asarray', (['k'], {}), '(k)\n', (25044, 25047), True, 'import numpy as np\n'), ((30583, 30603), 'numpy.logical_not', 'np.logical_not', (['mask'], {}), '(mask)\n', (30597, 30603), True, 'import numpy as np\n'), ((35009, 35028), 'copy.deepcopy', 'copy.deepcopy', (['data'], {}), '(data)\n', (35022, 35028), False, 'import copy\n'), ((80070, 80095), 'numpy.errstate', 'np.errstate', ([], {'all': '"""ignore"""'}), "(all='ignore')\n", (80081, 80095), True, 'import numpy as np\n'), ((80744, 80769), 'numpy.errstate', 'np.errstate', ([], {'all': '"""ignore"""'}), "(all='ignore')\n", (80755, 80769), True, 'import numpy as np\n'), ((81504, 81529), 'numpy.errstate', 'np.errstate', ([], {'all': '"""ignore"""'}), "(all='ignore')\n", (81515, 81529), True, 'import numpy as np\n'), ((23355, 23368), 'numpy.asarray', 'np.asarray', (['k'], {}), '(k)\n', (23365, 23368), True, 'import numpy as np\n'), ((39039, 39069), 'distutils.version.LooseVersion', 'LooseVersion', (['dask.__version__'], {}), '(dask.__version__)\n', (39051, 39069), False, 'from distutils.version import LooseVersion\n'), ((61260, 61279), 'numpy.atleast_1d', 'np.atleast_1d', (['axis'], {}), '(axis)\n', (61273, 61279), True, 'import numpy as np\n'), ((64333, 64358), 'numpy.concatenate', 'np.concatenate', (['positions'], {}), '(positions)\n', (64347, 64358), True, 'import numpy as np\n'), ((71530, 71544), 'numpy.isnan', 'np.isnan', (['data'], {}), '(data)\n', (71538, 71544), True, 'import numpy as np\n'), ((92294, 92319), 'numpy.concatenate', 'np.concatenate', (['positions'], {}), '(positions)\n', (92308, 92319), True, 'import numpy as np\n'), ((25262, 25275), 'numpy.nonzero', 'np.nonzero', (['k'], {}), '(k)\n', (25272, 25275), True, 'import numpy as np\n'), ((61715, 61734), 'numpy.asanyarray', 'np.asanyarray', (['data'], {}), '(data)\n', (61728, 61734), True, 'import numpy as np\n')] |
zubtsov/competitive-programming | codeforces.com/1186A/solution.py | 919d63130144347d7f6eddcf8f5bc2afb85fddf3 | number_of_participants, number_of_pens, number_of_notebooks = map(int, input().split())
if number_of_pens >= number_of_participants and number_of_notebooks >= number_of_participants:
print('Yes')
else:
print('No')
| [] |
StanczakDominik/DLA | DLA/__main__.py | bf63592a5ac96ffef639e7a0c80d7d52ff776322 | from DLA import main_single
d = main_single(1, gotosize=[1e4, 5e4])
d.plot_particles()
d.plot_mass_distribution()
| [((32, 75), 'DLA.main_single', 'main_single', (['(1)'], {'gotosize': '[10000.0, 50000.0]'}), '(1, gotosize=[10000.0, 50000.0])\n', (43, 75), False, 'from DLA import main_single\n')] |
bulutistan/Py3AMF | pyamf/tests/test_util.py | 3de53095b52fe2bf82b69ba5ad0b894b53045f7e | # -*- coding: utf-8 -*-
#
# Copyright (c) The PyAMF Project.
# See LICENSE.txt for details.
"""
Tests for AMF utilities.
@since: 0.1.0
"""
import unittest
from datetime import datetime
from io import BytesIO
import pyamf
from pyamf import util
from pyamf.tests.util import replace_dict
PosInf = 1e300000
NegInf = -1e300000
NaN = PosInf / PosInf
def isNaN(val):
return str(float(val)) == str(NaN)
def isPosInf(val):
return str(float(val)) == str(PosInf)
def isNegInf(val):
return str(float(val)) == str(NegInf)
class TimestampTestCase(unittest.TestCase):
"""
Test UTC timestamps.
"""
def test_get_timestamp(self):
self.assertEqual(
util.get_timestamp(datetime(2007, 11, 12)),
1194825600
)
def test_get_datetime(self):
self.assertEqual(util.get_datetime(1194825600), datetime(2007, 11, 12))
def test_get_negative_datetime(self):
self.assertEqual(util.get_datetime(-31536000), datetime(1969, 1, 1))
def test_preserved_microseconds(self):
dt = datetime(2009, 3, 8, 23, 30, 47, 770122)
ts = util.get_timestamp(dt)
self.assertEqual(util.get_datetime(ts), dt)
class StringIOTestCase(unittest.TestCase):
def test_create(self):
sp = util.BufferedByteStream()
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
self.assertEqual(sp.getvalue(), b'')
sp = util.BufferedByteStream(None)
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
sp = util.BufferedByteStream('')
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
sp = util.BufferedByteStream('spam')
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'spam')
self.assertEqual(len(sp), 4)
sp = util.BufferedByteStream(BytesIO('this is a test'.encode()))
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'this is a test')
self.assertEqual(len(sp), 14)
self.assertRaises(TypeError, util.BufferedByteStream, self)
def test_getvalue(self):
sp = util.BufferedByteStream()
sp.write('asdfasdf')
self.assertEqual(sp.getvalue(), b'asdfasdf')
sp.write('spam')
self.assertEqual(sp.getvalue(), b'asdfasdfspam')
def test_read(self):
sp = util.BufferedByteStream('this is a test')
self.assertEqual(len(sp), 14)
self.assertEqual(sp.read(1), b't')
self.assertEqual(sp.getvalue(), b'this is a test')
self.assertEqual(len(sp), 14)
self.assertEqual(sp.read(10), b'his is a t')
self.assertEqual(sp.read(), b'est')
def test_seek(self):
sp = util.BufferedByteStream('abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.tell(), 0)
# Relative to the beginning of the stream
sp.seek(0, 0)
self.assertEqual(sp.tell(), 0)
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.read(1), b'a')
self.assertEqual(len(sp), 26)
sp.seek(10, 0)
self.assertEqual(sp.tell(), 10)
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.read(1), b'k')
self.assertEqual(len(sp), 26)
sp.seek(-5, 1)
self.assertEqual(sp.tell(), 6)
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.read(1), b'g')
self.assertEqual(len(sp), 26)
sp.seek(-3, 2)
self.assertEqual(sp.tell(), 23)
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.read(1), b'x')
self.assertEqual(len(sp), 26)
def test_tell(self):
sp = util.BufferedByteStream('abcdefghijklmnopqrstuvwxyz')
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(len(sp), 26)
self.assertEqual(sp.tell(), 0)
sp.read(1)
self.assertEqual(sp.tell(), 1)
self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz')
self.assertEqual(len(sp), 26)
sp.read(5)
self.assertEqual(sp.tell(), 6)
def test_truncate(self):
sp = util.BufferedByteStream('abcdef')
self.assertEqual(sp.getvalue(), b'abcdef')
self.assertEqual(len(sp), 6)
sp.truncate()
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
sp = util.BufferedByteStream('hello')
self.assertEqual(sp.getvalue(), b'hello')
self.assertEqual(len(sp), 5)
sp.truncate(3)
self.assertEqual(sp.getvalue(), b'hel')
self.assertEqual(len(sp), 3)
def test_write(self):
sp = util.BufferedByteStream()
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
self.assertEqual(sp.tell(), 0)
sp.write('hello')
self.assertEqual(sp.getvalue(), b'hello')
self.assertEqual(len(sp), 5)
self.assertEqual(sp.tell(), 5)
sp = util.BufferedByteStream(b'xyz')
self.assertEqual(sp.getvalue(), b'xyz')
self.assertEqual(len(sp), 3)
self.assertEqual(sp.tell(), 0)
sp.write('abc')
self.assertEqual(sp.getvalue(), b'abc')
self.assertEqual(len(sp), 3)
self.assertEqual(sp.tell(), 3)
def test_len(self):
sp = util.BufferedByteStream()
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(len(sp), 0)
self.assertEqual(sp.tell(), 0)
sp.write('xyz')
self.assertEqual(len(sp), 3)
sp = util.BufferedByteStream('foo')
self.assertEqual(len(sp), 3)
sp.seek(0, 2)
sp.write('xyz')
self.assertEqual(len(sp), 6)
def test_consume(self):
sp = util.BufferedByteStream()
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(sp.tell(), 0)
sp.consume()
self.assertEqual(sp.getvalue(), b'')
self.assertEqual(sp.tell(), 0)
sp = util.BufferedByteStream('foobar')
self.assertEqual(sp.getvalue(), b'foobar')
self.assertEqual(sp.tell(), 0)
sp.seek(3)
self.assertEqual(sp.tell(), 3)
sp.consume()
self.assertEqual(sp.getvalue(), b'bar')
self.assertEqual(sp.tell(), 0)
# from ticket 451 - http://pyamf.org/ticket/451
sp = util.BufferedByteStream('abcdef')
# move the stream pos to the end
sp.read()
self.assertEqual(len(sp), 6)
sp.consume()
self.assertEqual(len(sp), 0)
sp = util.BufferedByteStream('abcdef')
sp.seek(6)
sp.consume()
self.assertEqual(sp.getvalue(), b'')
class DataTypeMixInTestCase(unittest.TestCase):
endians = ('>', '<') # big, little
def _write_endian(self, obj, func, args, expected):
old_endian = obj.endian
for x in range(2):
obj.truncate()
obj.endian = self.endians[x]
func(*args)
self.assertEqual(obj.getvalue(), expected[x])
obj.endian = old_endian
def _read_endian(self, data, func, args, expected):
for x in range(2):
obj = util.BufferedByteStream(data[x])
obj.endian = self.endians[x]
result = getattr(obj, func)(*args)
self.assertEqual(result, expected)
def test_read_uchar(self):
x = util.BufferedByteStream(b'\x00\xff')
self.assertEqual(x.read_uchar(), 0)
self.assertEqual(x.read_uchar(), 255)
def test_write_uchar(self):
x = util.BufferedByteStream()
x.write_uchar(0)
self.assertEqual(x.getvalue(), b'\x00')
x.write_uchar(255)
self.assertEqual(x.getvalue(), b'\x00\xff')
self.assertRaises(OverflowError, x.write_uchar, 256)
self.assertRaises(OverflowError, x.write_uchar, -1)
self.assertRaises(TypeError, x.write_uchar, 'f')
def test_read_char(self):
x = util.BufferedByteStream(b'\x00\x7f\xff\x80')
self.assertEqual(x.read_char(), 0)
self.assertEqual(x.read_char(), 127)
self.assertEqual(x.read_char(), -1)
self.assertEqual(x.read_char(), -128)
def test_write_char(self):
x = util.BufferedByteStream()
x.write_char(0)
x.write_char(-128)
x.write_char(127)
self.assertEqual(x.getvalue(), b'\x00\x80\x7f')
self.assertRaises(OverflowError, x.write_char, 128)
self.assertRaises(OverflowError, x.write_char, -129)
self.assertRaises(TypeError, x.write_char, 'f')
def test_write_ushort(self):
x = util.BufferedByteStream()
self._write_endian(x, x.write_ushort, (0,), (b'\x00\x00', b'\x00\x00'))
self._write_endian(x, x.write_ushort, (12345,), (b'09', b'90'))
self._write_endian(
x,
x.write_ushort,
(65535,),
(b'\xff\xff', b'\xff\xff')
)
self.assertRaises(OverflowError, x.write_ushort, 65536)
self.assertRaises(OverflowError, x.write_ushort, -1)
self.assertRaises(TypeError, x.write_ushort, 'aa')
def test_read_ushort(self):
self._read_endian([b'\x00\x00', b'\x00\x00'], 'read_ushort', (), 0)
self._read_endian(['09', '90'], 'read_ushort', (), 12345)
self._read_endian([b'\xff\xff', b'\xff\xff'], 'read_ushort', (), 65535)
def test_write_short(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_short,
(-5673,),
(b'\xe9\xd7', b'\xd7\xe9')
)
self._write_endian(
x, x.write_short,
(32767,),
(b'\x7f\xff', b'\xff\x7f')
)
self.assertRaises(OverflowError, x.write_ushort, 65537)
self.assertRaises(OverflowError, x.write_ushort, -1)
self.assertRaises(TypeError, x.write_short, '\x00\x00')
def test_read_short(self):
self._read_endian([b'\xe9\xd7', b'\xd7\xe9'], 'read_short', (), -5673)
self._read_endian([b'\x7f\xff', b'\xff\x7f'], 'read_short', (), 32767)
def test_write_ulong(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_ulong,
(0,),
(b'\x00\x00\x00\x00', b'\x00\x00\x00\x00')
)
self._write_endian(
x,
x.write_ulong,
(16810049,),
(b'\x01\x00\x80A', b'A\x80\x00\x01')
)
self._write_endian(
x,
x.write_ulong,
(4294967295,),
(b'\xff\xff\xff\xff', b'\xff\xff\xff\xff')
)
self.assertRaises(OverflowError, x.write_ulong, 4294967296)
self.assertRaises(OverflowError, x.write_ulong, -1)
self.assertRaises(TypeError, x.write_ulong, '\x00\x00\x00\x00')
def test_read_ulong(self):
self._read_endian(
[b'\x00\x00\x00\x00', b'\x00\x00\x00\x00'],
'read_ulong',
(),
0
)
self._read_endian(
[b'\x01\x00\x80A', b'A\x80\x00\x01'],
'read_ulong',
(),
16810049
)
self._read_endian(
[b'\xff\xff\xff\xff', b'\xff\xff\xff\xff'],
'read_ulong',
(),
4294967295
)
def test_write_long(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_long,
(0,),
(b'\x00\x00\x00\x00', b'\x00\x00\x00\x00')
)
self._write_endian(
x,
x.write_long,
(16810049,),
(b'\x01\x00\x80A', b'A\x80\x00\x01')
)
self._write_endian(
x,
x.write_long,
(2147483647,),
(b'\x7f\xff\xff\xff', b'\xff\xff\xff\x7f')
)
self._write_endian(
x,
x.write_long,
(-2147483648,),
(b'\x80\x00\x00\x00', b'\x00\x00\x00\x80')
)
self.assertRaises(OverflowError, x.write_long, 2147483648)
self.assertRaises(OverflowError, x.write_long, -2147483649)
self.assertRaises(TypeError, x.write_long, '\x00\x00\x00\x00')
def test_read_long(self):
self._read_endian(
[b'\xff\xff\xcf\xc7', b'\xc7\xcf\xff\xff'],
'read_long',
(),
-12345
)
self._read_endian(
[b'\x00\x00\x00\x00', b'\x00\x00\x00\x00'],
'read_long',
(),
0
)
self._read_endian(
[b'\x01\x00\x80A', b'A\x80\x00\x01'],
'read_long',
(),
16810049
)
self._read_endian(
[b'\x7f\xff\xff\xff', b'\xff\xff\xff\x7f'],
'read_long',
(),
2147483647
)
def test_write_u24bit(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_24bit_uint,
(0,),
(b'\x00\x00\x00', b'\x00\x00\x00')
)
self._write_endian(
x,
x.write_24bit_uint,
(4292609,),
(b'A\x80\x01', b'\x01\x80A')
)
self._write_endian(
x,
x.write_24bit_uint,
(16777215,),
(b'\xff\xff\xff', b'\xff\xff\xff')
)
self.assertRaises(OverflowError, x.write_24bit_uint, 16777216)
self.assertRaises(OverflowError, x.write_24bit_uint, -1)
self.assertRaises(TypeError, x.write_24bit_uint, '\x00\x00\x00')
def test_read_u24bit(self):
self._read_endian(
[b'\x00\x00\x00', b'\x00\x00\x00'], 'read_24bit_uint', (), 0
)
self._read_endian(
[b'\x00\x00\x80', b'\x80\x00\x00'], 'read_24bit_uint', (), 128
)
self._read_endian(
[b'\x80\x00\x00', b'\x00\x00\x80'], 'read_24bit_uint', (), 8388608
)
self._read_endian(
[b'\xff\xff\x7f', b'\x7f\xff\xff'], 'read_24bit_uint', (), 16777087
)
self._read_endian(
[b'\x7f\xff\xff', b'\xff\xff\x7f'], 'read_24bit_uint', (), 8388607
)
def test_write_24bit(self):
x = util.BufferedByteStream()
self._write_endian(
x, x.write_24bit_int, (0,), (b'\x00\x00\x00', b'\x00\x00\x00')
)
self._write_endian(
x, x.write_24bit_int, (128,), (b'\x00\x00\x80', b'\x80\x00\x00')
)
self._write_endian(
x, x.write_24bit_int, (8388607,), (b'\x7f\xff\xff', b'\xff\xff\x7f')
)
self._write_endian(
x, x.write_24bit_int, (-1,), (b'\xff\xff\xff', b'\xff\xff\xff')
)
self._write_endian(
x, x.write_24bit_int, (-8388608,), (b'\x80\x00\x00', b'\x00\x00\x80')
)
self.assertRaises(OverflowError, x.write_24bit_int, 8388608)
self.assertRaises(OverflowError, x.write_24bit_int, -8388609)
self.assertRaises(TypeError, x.write_24bit_int, '\x00\x00\x00')
def test_read_24bit(self):
self._read_endian(
[b'\x00\x00\x00', b'\x00\x00\x00'], 'read_24bit_int', (), 0
)
self._read_endian(
[b'\x00\x00\x80', b'\x80\x00\x00'], 'read_24bit_int', (), 128
)
self._read_endian(
[b'\x80\x00\x00', b'\x00\x00\x80'], 'read_24bit_int', (), -8388608
)
self._read_endian(
[b'\xff\xff\x7f', b'\x7f\xff\xff'], 'read_24bit_int', (), -129
)
self._read_endian(
[b'\x7f\xff\xff', b'\xff\xff\x7f'], 'read_24bit_int', (), 8388607
)
def test_write_float(self):
x = util.BufferedByteStream()
self._write_endian(
x, x.write_float, (0.2,), (b'>L\xcc\xcd', b'\xcd\xccL>')
)
self.assertRaises(TypeError, x.write_float, 'foo')
def test_read_float(self):
self._read_endian(
[b'?\x00\x00\x00', b'\x00\x00\x00?'], 'read_float', (), 0.5
)
def test_write_double(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_double,
(0.2,),
(b'?\xc9\x99\x99\x99\x99\x99\x9a', b'\x9a\x99\x99\x99\x99\x99\xc9?')
)
self.assertRaises(TypeError, x.write_double, 'foo')
def test_read_double(self):
self._read_endian(
[b'?\xc9\x99\x99\x99\x99\x99\x9a', b'\x9a\x99\x99\x99\x99\x99\xc9?'],
'read_double',
(),
0.2
)
def test_write_utf8_string(self):
x = util.BufferedByteStream()
self._write_endian(
x,
x.write_utf8_string,
(u'ᚠᛇᚻ',),
[b'\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2
)
self.assertRaises(TypeError, x.write_utf8_string, 1)
self.assertRaises(TypeError, x.write_utf8_string, 1.0)
self.assertRaises(TypeError, x.write_utf8_string, object())
x.write_utf8_string('\xff')
def test_read_utf8_string(self):
self._read_endian(
[b'\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2,
'read_utf8_string',
(9,),
u'ᚠᛇᚻ'
)
def test_nan(self):
x = util.BufferedByteStream(b'\xff\xf8\x00\x00\x00\x00\x00\x00')
self.assertTrue(isNaN(x.read_double()))
x = util.BufferedByteStream(b'\xff\xf0\x00\x00\x00\x00\x00\x00')
self.assertTrue(isNegInf(x.read_double()))
x = util.BufferedByteStream(b'\x7f\xf0\x00\x00\x00\x00\x00\x00')
self.assertTrue(isPosInf(x.read_double()))
# now test little endian
x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf8\xff')
x.endian = '<'
self.assertTrue(isNaN(x.read_double()))
x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf0\xff')
x.endian = '<'
self.assertTrue(isNegInf(x.read_double()))
x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf0\x7f')
x.endian = '<'
self.assertTrue(isPosInf(x.read_double()))
def test_write_infinites(self):
x = util.BufferedByteStream()
self._write_endian(x, x.write_double, (NaN,), (
b'\xff\xf8\x00\x00\x00\x00\x00\x00',
b'\x00\x00\x00\x00\x00\x00\xf8\xff'
))
self._write_endian(x, x.write_double, (PosInf,), (
b'\x7f\xf0\x00\x00\x00\x00\x00\x00',
b'\x00\x00\x00\x00\x00\x00\xf0\x7f'
))
self._write_endian(x, x.write_double, (NegInf,), (
b'\xff\xf0\x00\x00\x00\x00\x00\x00',
b'\x00\x00\x00\x00\x00\x00\xf0\xff'
))
class BufferedByteStreamTestCase(unittest.TestCase):
"""
Tests for L{BufferedByteStream<util.BufferedByteStream>}
"""
def test_create(self):
x = util.BufferedByteStream()
self.assertEqual(x.getvalue(), b'')
self.assertEqual(x.tell(), 0)
x = util.BufferedByteStream('abc')
self.assertEqual(x.getvalue(), b'abc')
self.assertEqual(x.tell(), 0)
def test_read(self):
x = util.BufferedByteStream()
self.assertEqual(x.tell(), 0)
self.assertEqual(len(x), 0)
self.assertRaises(IOError, x.read)
self.assertRaises(IOError, x.read, 10)
x.write('hello')
x.seek(0)
self.assertRaises(IOError, x.read, 10)
self.assertEqual(x.read(), b'hello')
def test_read_negative(self):
"""
@see: #799
"""
x = util.BufferedByteStream()
x.write('*' * 6000)
x.seek(100)
self.assertRaises(IOError, x.read, -345)
def test_peek(self):
x = util.BufferedByteStream('abcdefghijklmnopqrstuvwxyz')
self.assertEqual(x.tell(), 0)
self.assertEqual(x.peek(), b'a')
self.assertEqual(x.peek(5), b'abcde')
self.assertEqual(x.peek(-1), b'abcdefghijklmnopqrstuvwxyz')
x.seek(10)
self.assertEqual(x.peek(50), b'klmnopqrstuvwxyz')
def test_eof(self):
x = util.BufferedByteStream()
self.assertTrue(x.at_eof())
x.write('hello')
x.seek(0)
self.assertFalse(x.at_eof())
x.seek(0, 2)
self.assertTrue(x.at_eof())
def test_remaining(self):
x = util.BufferedByteStream('spameggs')
self.assertEqual(x.tell(), 0)
self.assertEqual(x.remaining(), 8)
x.seek(2)
self.assertEqual(x.tell(), 2)
self.assertEqual(x.remaining(), 6)
def test_add(self):
a = util.BufferedByteStream('a')
b = util.BufferedByteStream('b')
c = a + b
self.assertTrue(isinstance(c, util.BufferedByteStream))
self.assertEqual(c.getvalue(), b'ab')
self.assertEqual(c.tell(), 0)
def test_add_pos(self):
a = util.BufferedByteStream(b'abc')
b = util.BufferedByteStream(b'def')
a.seek(1)
b.seek(0, 2)
self.assertEqual(a.tell(), 1)
self.assertEqual(b.tell(), 3)
self.assertEqual(a.tell(), 1)
self.assertEqual(b.tell(), 3)
def test_append_types(self):
# test non string types
a = util.BufferedByteStream()
self.assertRaises(TypeError, a.append, 234234)
self.assertRaises(TypeError, a.append, 234.0)
self.assertRaises(TypeError, a.append, 234234)
self.assertRaises(TypeError, a.append, [])
self.assertRaises(TypeError, a.append, {})
self.assertRaises(TypeError, a.append, lambda _: None)
self.assertRaises(TypeError, a.append, ())
self.assertRaises(TypeError, a.append, object())
def test_append_string(self):
"""
Test L{util.BufferedByteStream.append} with C{str} objects.
"""
# test empty
a = util.BufferedByteStream()
self.assertEqual(a.getvalue(), b'')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 0)
a.append('foo')
self.assertEqual(a.getvalue(), b'foo')
self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved
self.assertEqual(len(a), 3)
# test pointer beginning, some data
a = util.BufferedByteStream('bar')
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
# test pointer middle, some data
a = util.BufferedByteStream('bar')
a.seek(2)
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 2)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 2) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
# test pointer end, some data
a = util.BufferedByteStream('bar')
a.seek(0, 2)
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 3)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 3) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
class Foo(object):
def getvalue(self):
return b'foo'
def __str__(self):
raise AttributeError()
a = util.BufferedByteStream()
self.assertEqual(a.getvalue(), b'')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 0)
a.append(Foo())
self.assertEqual(a.getvalue(), b'foo')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 3)
def test_append_unicode(self):
"""
Test L{util.BufferedByteStream.append} with C{unicode} objects.
"""
# test empty
a = util.BufferedByteStream()
self.assertEqual(a.getvalue(), b'')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 0)
a.append('foo')
self.assertEqual(a.getvalue(), b'foo')
self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved
self.assertEqual(len(a), 3)
# test pointer beginning, some data
a = util.BufferedByteStream('bar')
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
# test pointer middle, some data
a = util.BufferedByteStream('bar')
a.seek(2)
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 2)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 2) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
# test pointer end, some data
a = util.BufferedByteStream('bar')
a.seek(0, 2)
self.assertEqual(a.getvalue(), b'bar')
self.assertEqual(a.tell(), 3)
self.assertEqual(len(a), 3)
a.append('gak')
self.assertEqual(a.getvalue(), b'bargak')
self.assertEqual(a.tell(), 3) # <-- pointer hasn't moved
self.assertEqual(len(a), 6)
class Foo(object):
def getvalue(self):
return u'foo'
def __str__(self):
raise AttributeError()
a = util.BufferedByteStream()
self.assertEqual(a.getvalue(), b'')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 0)
a.append(Foo())
self.assertEqual(a.getvalue(), b'foo')
self.assertEqual(a.tell(), 0)
self.assertEqual(len(a), 3)
class DummyAlias(pyamf.ClassAlias):
pass
class AnotherDummyAlias(pyamf.ClassAlias):
pass
class YADummyAlias(pyamf.ClassAlias):
pass
class ClassAliasTestCase(unittest.TestCase):
def setUp(self):
self.old_aliases = pyamf.ALIAS_TYPES.copy()
def tearDown(self):
replace_dict(self.old_aliases, pyamf.ALIAS_TYPES)
def test_simple(self):
class A(object):
pass
pyamf.register_alias_type(DummyAlias, A)
self.assertEqual(util.get_class_alias(A), DummyAlias)
def test_nested(self):
class A(object):
pass
class B(object):
pass
class C(object):
pass
pyamf.register_alias_type(DummyAlias, A, B, C)
self.assertEqual(util.get_class_alias(B), DummyAlias)
def test_multiple(self):
class A(object):
pass
class B(object):
pass
class C(object):
pass
pyamf.register_alias_type(DummyAlias, A)
pyamf.register_alias_type(AnotherDummyAlias, B)
pyamf.register_alias_type(YADummyAlias, C)
self.assertEqual(util.get_class_alias(B), AnotherDummyAlias)
self.assertEqual(util.get_class_alias(C), YADummyAlias)
self.assertEqual(util.get_class_alias(A), DummyAlias)
def test_none_existant(self):
self.assertEqual(util.get_class_alias(self.__class__), None)
def test_subclass(self):
class A(object):
pass
class B(A):
pass
pyamf.register_alias_type(DummyAlias, A)
self.assertEqual(util.get_class_alias(B), DummyAlias)
class IsClassSealedTestCase(unittest.TestCase):
"""
Tests for L{util.is_class_sealed}
"""
def test_new_mixed(self):
class A(object):
__slots__ = ['foo', 'bar']
class B(A):
pass
class C(B):
__slots__ = ('spam', 'eggs')
self.assertTrue(util.is_class_sealed(A))
self.assertFalse(util.is_class_sealed(B))
self.assertFalse(util.is_class_sealed(C))
def test_deep(self):
class A(object):
__slots__ = ['foo', 'bar']
class B(A):
__slots__ = ('gak',)
class C(B):
pass
self.assertTrue(util.is_class_sealed(A))
self.assertTrue(util.is_class_sealed(B))
self.assertFalse(util.is_class_sealed(C))
class GetClassMetaTestCase(unittest.TestCase):
"""
Tests for L{util.get_class_meta}
"""
def test_types(self):
class A:
pass
class B(object):
pass
for t in ['', u'', 1, 1.0, 1, [], {}, object, object(), A(), B()]:
self.assertRaises(TypeError, util.get_class_meta, t)
def test_no_meta(self):
class A:
pass
class B(object):
pass
empty = {
'readonly_attrs': None,
'static_attrs': None,
'synonym_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'exclude_attrs': None,
'proxy_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), empty)
self.assertEqual(util.get_class_meta(B), empty)
def test_alias(self):
class A:
class __amf__:
alias = 'foo.bar.Spam'
class B(object):
class __amf__:
alias = 'foo.bar.Spam'
meta = {
'readonly_attrs': None,
'static_attrs': None,
'synonym_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': 'foo.bar.Spam',
'amf3': None,
'proxy_attrs': None,
'exclude_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_static(self):
class A:
class __amf__:
static = ['foo', 'bar']
class B(object):
class __amf__:
static = ['foo', 'bar']
meta = {
'readonly_attrs': None,
'static_attrs': ['foo', 'bar'],
'synonym_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'exclude_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_exclude(self):
class A:
class __amf__:
exclude = ['foo', 'bar']
class B(object):
class __amf__:
exclude = ['foo', 'bar']
meta = {
'readonly_attrs': None,
'exclude_attrs': ['foo', 'bar'],
'synonym_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'static_attrs': None,
'proxy_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_readonly(self):
class A:
class __amf__:
readonly = ['foo', 'bar']
class B(object):
class __amf__:
readonly = ['foo', 'bar']
meta = {
'exclude_attrs': None,
'readonly_attrs': ['foo', 'bar'],
'synonym_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'static_attrs': None,
'external': None,
'proxy_attrs': None,
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_amf3(self):
class A:
class __amf__:
amf3 = True
class B(object):
class __amf__:
amf3 = True
meta = {
'exclude_attrs': None,
'proxy_attrs': None,
'synonym_attrs': None,
'readonly_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': True,
'static_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_dynamic(self):
class A:
class __amf__:
dynamic = False
class B(object):
class __amf__:
dynamic = False
meta = {
'exclude_attrs': None,
'proxy_attrs': None,
'synonym_attrs': None,
'readonly_attrs': None,
'proxy_attrs': None,
'dynamic': False,
'alias': None,
'amf3': None,
'static_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_external(self):
class A:
class __amf__:
external = True
class B(object):
class __amf__:
external = True
meta = {
'exclude_attrs': None,
'proxy_attrs': None,
'synonym_attrs': None,
'readonly_attrs': None,
'proxy_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'static_attrs': None,
'external': True
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_dict(self):
meta = {
'exclude': ['foo'],
'readonly': ['bar'],
'dynamic': False,
'alias': 'spam.eggs',
'proxy_attrs': None,
'synonym_attrs': None,
'amf3': True,
'static': ['baz'],
'external': True
}
class A:
__amf__ = meta
class B(object):
__amf__ = meta
ret = {
'readonly_attrs': ['bar'],
'static_attrs': ['baz'],
'proxy_attrs': None,
'dynamic': False,
'alias': 'spam.eggs',
'amf3': True,
'exclude_attrs': ['foo'],
'synonym_attrs': None,
'proxy_attrs': None,
'external': True
}
self.assertEqual(util.get_class_meta(A), ret)
self.assertEqual(util.get_class_meta(B), ret)
def test_proxy(self):
class A:
class __amf__:
proxy = ['foo', 'bar']
class B(object):
class __amf__:
proxy = ['foo', 'bar']
meta = {
'exclude_attrs': None,
'readonly_attrs': None,
'proxy_attrs': ['foo', 'bar'],
'synonym_attrs': None,
'dynamic': None,
'alias': None,
'amf3': None,
'static_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
def test_synonym(self):
class A:
class __amf__:
synonym = {'foo': 'bar'}
class B(object):
class __amf__:
synonym = {'foo': 'bar'}
meta = {
'exclude_attrs': None,
'readonly_attrs': None,
'proxy_attrs': None,
'synonym_attrs': {'foo': 'bar'},
'dynamic': None,
'alias': None,
'amf3': None,
'static_attrs': None,
'external': None
}
self.assertEqual(util.get_class_meta(A), meta)
self.assertEqual(util.get_class_meta(B), meta)
| [((1062, 1102), 'datetime.datetime', 'datetime', (['(2009)', '(3)', '(8)', '(23)', '(30)', '(47)', '(770122)'], {}), '(2009, 3, 8, 23, 30, 47, 770122)\n', (1070, 1102), False, 'from datetime import datetime\n'), ((1116, 1138), 'pyamf.util.get_timestamp', 'util.get_timestamp', (['dt'], {}), '(dt)\n', (1134, 1138), False, 'from pyamf import util\n'), ((1277, 1302), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (1300, 1302), False, 'from pyamf import util\n'), ((1484, 1513), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['None'], {}), '(None)\n', (1507, 1513), False, 'from pyamf import util\n'), ((1650, 1677), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['""""""'], {}), "('')\n", (1673, 1677), False, 'from pyamf import util\n'), ((1814, 1845), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""spam"""'], {}), "('spam')\n", (1837, 1845), False, 'from pyamf import util\n'), ((2294, 2319), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (2317, 2319), False, 'from pyamf import util\n'), ((2524, 2565), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""this is a test"""'], {}), "('this is a test')\n", (2547, 2565), False, 'from pyamf import util\n'), ((2881, 2934), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdefghijklmnopqrstuvwxyz"""'], {}), "('abcdefghijklmnopqrstuvwxyz')\n", (2904, 2934), False, 'from pyamf import util\n'), ((3996, 4049), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdefghijklmnopqrstuvwxyz"""'], {}), "('abcdefghijklmnopqrstuvwxyz')\n", (4019, 4049), False, 'from pyamf import util\n'), ((4470, 4503), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdef"""'], {}), "('abcdef')\n", (4493, 4503), False, 'from pyamf import util\n'), ((4712, 4744), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""hello"""'], {}), "('hello')\n", (4735, 4744), False, 'from pyamf import util\n'), ((4983, 5008), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (5006, 5008), False, 'from pyamf import util\n'), ((5298, 5329), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'xyz'"], {}), "(b'xyz')\n", (5321, 5329), False, 'from pyamf import util\n'), ((5642, 5667), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (5665, 5667), False, 'from pyamf import util\n'), ((5867, 5897), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""foo"""'], {}), "('foo')\n", (5890, 5897), False, 'from pyamf import util\n'), ((6063, 6088), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (6086, 6088), False, 'from pyamf import util\n'), ((6295, 6328), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""foobar"""'], {}), "('foobar')\n", (6318, 6328), False, 'from pyamf import util\n'), ((6659, 6692), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdef"""'], {}), "('abcdef')\n", (6682, 6692), False, 'from pyamf import util\n'), ((6862, 6895), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdef"""'], {}), "('abcdef')\n", (6885, 6895), False, 'from pyamf import util\n'), ((7689, 7725), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x00\\xff'"], {}), "(b'\\x00\\xff')\n", (7712, 7725), False, 'from pyamf import util\n'), ((7862, 7887), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (7885, 7887), False, 'from pyamf import util\n'), ((8263, 8307), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x00\\x7f\\xff\\x80'"], {}), "(b'\\x00\\x7f\\xff\\x80')\n", (8286, 8307), False, 'from pyamf import util\n'), ((8531, 8556), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (8554, 8556), False, 'from pyamf import util\n'), ((8916, 8941), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (8939, 8941), False, 'from pyamf import util\n'), ((9722, 9747), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (9745, 9747), False, 'from pyamf import util\n'), ((10444, 10469), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (10467, 10469), False, 'from pyamf import util\n'), ((11674, 11699), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (11697, 11699), False, 'from pyamf import util\n'), ((13220, 13245), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (13243, 13245), False, 'from pyamf import util\n'), ((14563, 14588), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (14586, 14588), False, 'from pyamf import util\n'), ((16023, 16048), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (16046, 16048), False, 'from pyamf import util\n'), ((16403, 16428), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (16426, 16428), False, 'from pyamf import util\n'), ((16934, 16959), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (16957, 16959), False, 'from pyamf import util\n'), ((17596, 17656), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00'"], {}), "(b'\\xff\\xf8\\x00\\x00\\x00\\x00\\x00\\x00')\n", (17619, 17656), False, 'from pyamf import util\n'), ((17718, 17778), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\xff\\xf0\\x00\\x00\\x00\\x00\\x00\\x00'"], {}), "(b'\\xff\\xf0\\x00\\x00\\x00\\x00\\x00\\x00')\n", (17741, 17778), False, 'from pyamf import util\n'), ((17843, 17903), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x7f\\xf0\\x00\\x00\\x00\\x00\\x00\\x00'"], {}), "(b'\\x7f\\xf0\\x00\\x00\\x00\\x00\\x00\\x00')\n", (17866, 17903), False, 'from pyamf import util\n'), ((18001, 18061), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf8\\xff'"], {}), "(b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf8\\xff')\n", (18024, 18061), False, 'from pyamf import util\n'), ((18146, 18206), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf0\\xff'"], {}), "(b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf0\\xff')\n", (18169, 18206), False, 'from pyamf import util\n'), ((18294, 18354), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf0\\x7f'"], {}), "(b'\\x00\\x00\\x00\\x00\\x00\\x00\\xf0\\x7f')\n", (18317, 18354), False, 'from pyamf import util\n'), ((18478, 18503), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (18501, 18503), False, 'from pyamf import util\n'), ((19177, 19202), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (19200, 19202), False, 'from pyamf import util\n'), ((19299, 19329), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abc"""'], {}), "('abc')\n", (19322, 19329), False, 'from pyamf import util\n'), ((19454, 19479), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (19477, 19479), False, 'from pyamf import util\n'), ((19872, 19897), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (19895, 19897), False, 'from pyamf import util\n'), ((20034, 20087), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""abcdefghijklmnopqrstuvwxyz"""'], {}), "('abcdefghijklmnopqrstuvwxyz')\n", (20057, 20087), False, 'from pyamf import util\n'), ((20398, 20423), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (20421, 20423), False, 'from pyamf import util\n'), ((20641, 20676), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""spameggs"""'], {}), "('spameggs')\n", (20664, 20676), False, 'from pyamf import util\n'), ((20896, 20924), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""a"""'], {}), "('a')\n", (20919, 20924), False, 'from pyamf import util\n'), ((20937, 20965), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""b"""'], {}), "('b')\n", (20960, 20965), False, 'from pyamf import util\n'), ((21175, 21206), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'abc'"], {}), "(b'abc')\n", (21198, 21206), False, 'from pyamf import util\n'), ((21219, 21250), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (["b'def'"], {}), "(b'def')\n", (21242, 21250), False, 'from pyamf import util\n'), ((21523, 21548), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (21546, 21548), False, 'from pyamf import util\n'), ((22147, 22172), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (22170, 22172), False, 'from pyamf import util\n'), ((22525, 22555), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (22548, 22555), False, 'from pyamf import util\n'), ((22911, 22941), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (22934, 22941), False, 'from pyamf import util\n'), ((23312, 23342), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (23335, 23342), False, 'from pyamf import util\n'), ((23838, 23863), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (23861, 23863), False, 'from pyamf import util\n'), ((24295, 24320), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (24318, 24320), False, 'from pyamf import util\n'), ((24673, 24703), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (24696, 24703), False, 'from pyamf import util\n'), ((25059, 25089), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (25082, 25089), False, 'from pyamf import util\n'), ((25460, 25490), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['"""bar"""'], {}), "('bar')\n", (25483, 25490), False, 'from pyamf import util\n'), ((25986, 26011), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', ([], {}), '()\n', (26009, 26011), False, 'from pyamf import util\n'), ((26523, 26547), 'pyamf.ALIAS_TYPES.copy', 'pyamf.ALIAS_TYPES.copy', ([], {}), '()\n', (26545, 26547), False, 'import pyamf\n'), ((26581, 26630), 'pyamf.tests.util.replace_dict', 'replace_dict', (['self.old_aliases', 'pyamf.ALIAS_TYPES'], {}), '(self.old_aliases, pyamf.ALIAS_TYPES)\n', (26593, 26630), False, 'from pyamf.tests.util import replace_dict\n'), ((26710, 26750), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['DummyAlias', 'A'], {}), '(DummyAlias, A)\n', (26735, 26750), False, 'import pyamf\n'), ((26979, 27025), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['DummyAlias', 'A', 'B', 'C'], {}), '(DummyAlias, A, B, C)\n', (27004, 27025), False, 'import pyamf\n'), ((27256, 27296), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['DummyAlias', 'A'], {}), '(DummyAlias, A)\n', (27281, 27296), False, 'import pyamf\n'), ((27305, 27352), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['AnotherDummyAlias', 'B'], {}), '(AnotherDummyAlias, B)\n', (27330, 27352), False, 'import pyamf\n'), ((27361, 27403), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['YADummyAlias', 'C'], {}), '(YADummyAlias, C)\n', (27386, 27403), False, 'import pyamf\n'), ((27823, 27863), 'pyamf.register_alias_type', 'pyamf.register_alias_type', (['DummyAlias', 'A'], {}), '(DummyAlias, A)\n', (27848, 27863), False, 'import pyamf\n'), ((830, 859), 'pyamf.util.get_datetime', 'util.get_datetime', (['(1194825600)'], {}), '(1194825600)\n', (847, 859), False, 'from pyamf import util\n'), ((861, 883), 'datetime.datetime', 'datetime', (['(2007)', '(11)', '(12)'], {}), '(2007, 11, 12)\n', (869, 883), False, 'from datetime import datetime\n'), ((953, 981), 'pyamf.util.get_datetime', 'util.get_datetime', (['(-31536000)'], {}), '(-31536000)\n', (970, 981), False, 'from pyamf import util\n'), ((983, 1003), 'datetime.datetime', 'datetime', (['(1969)', '(1)', '(1)'], {}), '(1969, 1, 1)\n', (991, 1003), False, 'from datetime import datetime\n'), ((1164, 1185), 'pyamf.util.get_datetime', 'util.get_datetime', (['ts'], {}), '(ts)\n', (1181, 1185), False, 'from pyamf import util\n'), ((7475, 7507), 'pyamf.util.BufferedByteStream', 'util.BufferedByteStream', (['data[x]'], {}), '(data[x])\n', (7498, 7507), False, 'from pyamf import util\n'), ((26777, 26800), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['A'], {}), '(A)\n', (26797, 26800), False, 'from pyamf import util\n'), ((27052, 27075), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['B'], {}), '(B)\n', (27072, 27075), False, 'from pyamf import util\n'), ((27430, 27453), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['B'], {}), '(B)\n', (27450, 27453), False, 'from pyamf import util\n'), ((27499, 27522), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['C'], {}), '(C)\n', (27519, 27522), False, 'from pyamf import util\n'), ((27563, 27586), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['A'], {}), '(A)\n', (27583, 27586), False, 'from pyamf import util\n'), ((27660, 27696), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['self.__class__'], {}), '(self.__class__)\n', (27680, 27696), False, 'from pyamf import util\n'), ((27890, 27913), 'pyamf.util.get_class_alias', 'util.get_class_alias', (['B'], {}), '(B)\n', (27910, 27913), False, 'from pyamf import util\n'), ((28251, 28274), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['A'], {}), '(A)\n', (28271, 28274), False, 'from pyamf import util\n'), ((28301, 28324), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['B'], {}), '(B)\n', (28321, 28324), False, 'from pyamf import util\n'), ((28351, 28374), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['C'], {}), '(C)\n', (28371, 28374), False, 'from pyamf import util\n'), ((28583, 28606), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['A'], {}), '(A)\n', (28603, 28606), False, 'from pyamf import util\n'), ((28632, 28655), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['B'], {}), '(B)\n', (28652, 28655), False, 'from pyamf import util\n'), ((28682, 28705), 'pyamf.util.is_class_sealed', 'util.is_class_sealed', (['C'], {}), '(C)\n', (28702, 28705), False, 'from pyamf import util\n'), ((29532, 29554), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (29551, 29554), False, 'from pyamf import util\n'), ((29588, 29610), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (29607, 29610), False, 'from pyamf import util\n'), ((30202, 30224), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (30221, 30224), False, 'from pyamf import util\n'), ((30257, 30279), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (30276, 30279), False, 'from pyamf import util\n'), ((30840, 30862), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (30859, 30862), False, 'from pyamf import util\n'), ((30895, 30917), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (30914, 30917), False, 'from pyamf import util\n'), ((31514, 31536), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (31533, 31536), False, 'from pyamf import util\n'), ((31569, 31591), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (31588, 31591), False, 'from pyamf import util\n'), ((32192, 32214), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (32211, 32214), False, 'from pyamf import util\n'), ((32247, 32269), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (32266, 32269), False, 'from pyamf import util\n'), ((32827, 32849), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (32846, 32849), False, 'from pyamf import util\n'), ((32882, 32904), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (32901, 32904), False, 'from pyamf import util\n'), ((33474, 33496), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (33493, 33496), False, 'from pyamf import util\n'), ((33529, 33551), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (33548, 33551), False, 'from pyamf import util\n'), ((34121, 34143), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (34140, 34143), False, 'from pyamf import util\n'), ((34176, 34198), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (34195, 34198), False, 'from pyamf import util\n'), ((35027, 35049), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (35046, 35049), False, 'from pyamf import util\n'), ((35081, 35103), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (35100, 35103), False, 'from pyamf import util\n'), ((35660, 35682), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (35679, 35682), False, 'from pyamf import util\n'), ((35715, 35737), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (35734, 35737), False, 'from pyamf import util\n'), ((36301, 36323), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['A'], {}), '(A)\n', (36320, 36323), False, 'from pyamf import util\n'), ((36356, 36378), 'pyamf.util.get_class_meta', 'util.get_class_meta', (['B'], {}), '(B)\n', (36375, 36378), False, 'from pyamf import util\n'), ((713, 735), 'datetime.datetime', 'datetime', (['(2007)', '(11)', '(12)'], {}), '(2007, 11, 12)\n', (721, 735), False, 'from datetime import datetime\n')] |
keocol/e-valuator | e-valuator.py | c2bab22e3debf08263fef57ee4135312a2bb2b0d | import dns.resolver
import sys
import colorama
import platform
from colorama import init, Fore, Back, Style
import re
# pip install -r requirements.txt (colorama)
os = platform.platform()
if os.find('Windows')!= (-1):
init(convert=True)
print("""
███████╗░░░░░░██╗░░░██╗░█████╗░██╗░░░░░██╗░░░██╗░█████╗░████████╗░█████╗░██████╗░
██╔════╝░░░░░░██║░░░██║██╔══██╗██║░░░░░██║░░░██║██╔══██╗╚══██╔══╝██╔══██╗██╔══██╗
█████╗░░█████╗╚██╗░██╔╝███████║██║░░░░░██║░░░██║███████║░░░██║░░░██║░░██║██████╔╝
██╔══╝░░╚════╝░╚████╔╝░██╔══██║██║░░░░░██║░░░██║██╔══██║░░░██║░░░██║░░██║██╔══██╗
███████╗░░░░░░░░╚██╔╝░░██║░░██║███████╗╚██████╔╝██║░░██║░░░██║░░░╚█████╔╝██║░░██║
╚══════╝░░░░░░░░░╚═╝░░░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═╝░░╚═╝░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝
\x1B[3mSimple Python3 Script for Checking SPF & DMARC Records.\x1B[0m
""" + '\n')
Domain = input('Domain: ')
# Checking SPF
print ('\n[+] Checking SPF Record...')
try:
obj_answer = dns.resolver.resolve(Domain, 'TXT')
except:
sys.exit(Fore.RED + "\n[+] Domain can't be resolved! Check the domain name and try again..")
answer = str(obj_answer.response)
cond = answer.find("v=spf")
if cond != -1:
print ('[+] SPF Record Found!')
spf_pos= answer.find("v=spf")
spf_end_tmp= (answer[spf_pos:].find("\n"))-1
spf_end= answer[spf_pos:spf_pos+spf_end_tmp]
print (Fore.GREEN + '[+] Domain: ' + Domain)
print (Fore.GREEN + '[+] SPF Record: ' +spf_end)
neutral_check = answer.find('?all')
fail_check = answer.find('-all')
soft_check = answer.find('~all')
pass_check = answer.find('+all')
if neutral_check != -1:
print (Fore.RED +'[+] Result: ?all IS FOUND!! Domain emails can be spoofed!')
elif fail_check != -1:
print (Fore.GREEN +'[+] Result: -all is found. SPF is correctly configured.')
elif soft_check != -1:
print (Fore.GREEN +'[+] Result: ~all is found. SPF is correctly configured.')
elif pass_check != -1:
print (Fore.RED +'[+] Result: +all DOMAIN IS VERY BADLY CONFIGURED! Domain emails can be spoofed!')
else:
print (Fore.RED +'[+] Result: No condition is set for "all"! Domain emails can be spoofed!')
else:
print (Fore.RED +'[+] No SPF Record Found!!')
# Checking DMARC
print (Fore.WHITE + '\n\n[+] Checking DMARC Policy..')
try:
obj2_answer = dns.resolver.resolve('_dmarc.'+ Domain, 'TXT')
except:
sys.exit(Fore.RED + "[+] The domain doesn't have DMARC policy configured!")
answer2 = str(obj2_answer.response)
print (Fore.WHITE + '[+] DMARC Policy Found!')
none_check = re.search("[\;\s]p\=none\;", answer2)
reject_check = re.search("[\;\s]p\=reject\;", answer2)
quarantine_check = re.search("[\;\s]p\=quarantine\;", answer2)
if none_check:
print (Fore.RED + '[+] Result: DMARC Policy is set as none! Domain emails can be spoofed!')
if reject_check:
print (Fore.GREEN + '[+] Result: DMARC Policy is set as reject! Domain emails are safe from spoofing.')
if quarantine_check:
print (Fore.GREEN + '[+] Result: DMARC Policy is set as quarantine! Domain emails are safe from spoofing.') | [((179, 198), 'platform.platform', 'platform.platform', ([], {}), '()\n', (196, 198), False, 'import platform\n'), ((2558, 2599), 're.search', 're.search', (['"""[\\\\;\\\\s]p\\\\=none\\\\;"""', 'answer2'], {}), "('[\\\\;\\\\s]p\\\\=none\\\\;', answer2)\n", (2567, 2599), False, 'import re\n'), ((2612, 2655), 're.search', 're.search', (['"""[\\\\;\\\\s]p\\\\=reject\\\\;"""', 'answer2'], {}), "('[\\\\;\\\\s]p\\\\=reject\\\\;', answer2)\n", (2621, 2655), False, 'import re\n'), ((2672, 2719), 're.search', 're.search', (['"""[\\\\;\\\\s]p\\\\=quarantine\\\\;"""', 'answer2'], {}), "('[\\\\;\\\\s]p\\\\=quarantine\\\\;', answer2)\n", (2681, 2719), False, 'import re\n'), ((232, 250), 'colorama.init', 'init', ([], {'convert': '(True)'}), '(convert=True)\n', (236, 250), False, 'from colorama import init, Fore, Back, Style\n'), ((1015, 1114), 'sys.exit', 'sys.exit', (['(Fore.RED +\n """\n[+] Domain can\'t be resolved! Check the domain name and try again..""")'], {}), '(Fore.RED +\n """\n[+] Domain can\'t be resolved! Check the domain name and try again..""")\n', (1023, 1114), False, 'import sys\n'), ((2376, 2451), 'sys.exit', 'sys.exit', (['(Fore.RED + "[+] The domain doesn\'t have DMARC policy configured!")'], {}), '(Fore.RED + "[+] The domain doesn\'t have DMARC policy configured!")\n', (2384, 2451), False, 'import sys\n')] |
qh73xe/HowAboutNatume | api/server.py | 8d994a1e16e2153dc200097d8f8b43713d76a3d5 | # -*- coding: utf-8 -*
"""トルネードを使用した ask.api を作成します."""
from json import dumps
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import parse_command_line
from tornado.web import Application, RequestHandler
from tornado.options import define, options
from tokenizer import get_entity
from logger import getLogger
LOGGER = getLogger('API_MODULE')
define("port", default=8000, help="run on the given port", type=int)
class AskHandler(RequestHandler):
"""question に get された文章と親密度の高い語を返します."""
def get(self):
"""Question に答えます."""
from ask import ask
author = self.get_argument('author')
question = self.get_argument('question')
answers = {
'answers': ask(author, get_entity(question))
}
self.finish(
dumps(
answers,
ensure_ascii=False,
indent=4,
sort_keys=True,
separators=(',', ': ')
)
)
def post(self):
"""Action on google の web フック用レスポンス"""
from ask import ask
import json
data = json.loads(self.request.body)
LOGGER.info('input: {data}'.format(data=data))
author = data.get('author', '夏目漱石')
question = data.get('question')
answers = ask(author, get_entity(question))
if answers:
adjective = answers.get('adjective', None)
nouns = answers.get('nouns')
if adjective:
speech = '。'.join([
'それは {adjective} 質問ですね'.format(adjective=adjective[0]),
'きっと, {0} や {1} あるいは {2} のことです'.format(*nouns)
])
else:
speech = 'それはきっと, {0} や {1} あるいは {2} のことです'.format(*nouns)
else:
speech = '。'.join([
'{q} についてですか'.format(q=question),
'難しいことを聞きますね',
'私にはわからないです'
])
displayText = speech
respose = {
'speech': speech,
'displayText': displayText,
'data': answers,
'contextOut': [answers],
'source': 'how-about-natume'
}
self.finish(
dumps(
respose,
ensure_ascii=False,
indent=4,
sort_keys=True,
separators=(',', ': ')
)
)
if __name__ == "__main__":
parse_command_line()
app = Application(handlers=[(r"/", AskHandler)])
http_server = HTTPServer(app)
http_server.listen(options.port)
IOLoop.instance().start()
| [((373, 396), 'logger.getLogger', 'getLogger', (['"""API_MODULE"""'], {}), "('API_MODULE')\n", (382, 396), False, 'from logger import getLogger\n'), ((397, 465), 'tornado.options.define', 'define', (['"""port"""'], {'default': '(8000)', 'help': '"""run on the given port"""', 'type': 'int'}), "('port', default=8000, help='run on the given port', type=int)\n", (403, 465), False, 'from tornado.options import define, options\n'), ((2480, 2500), 'tornado.options.parse_command_line', 'parse_command_line', ([], {}), '()\n', (2498, 2500), False, 'from tornado.options import parse_command_line\n'), ((2511, 2552), 'tornado.web.Application', 'Application', ([], {'handlers': "[('/', AskHandler)]"}), "(handlers=[('/', AskHandler)])\n", (2522, 2552), False, 'from tornado.web import Application, RequestHandler\n'), ((2572, 2587), 'tornado.httpserver.HTTPServer', 'HTTPServer', (['app'], {}), '(app)\n', (2582, 2587), False, 'from tornado.httpserver import HTTPServer\n'), ((1159, 1188), 'json.loads', 'json.loads', (['self.request.body'], {}), '(self.request.body)\n', (1169, 1188), False, 'import json\n'), ((839, 928), 'json.dumps', 'dumps', (['answers'], {'ensure_ascii': '(False)', 'indent': '(4)', 'sort_keys': '(True)', 'separators': "(',', ': ')"}), "(answers, ensure_ascii=False, indent=4, sort_keys=True, separators=(\n ',', ': '))\n", (844, 928), False, 'from json import dumps\n'), ((1359, 1379), 'tokenizer.get_entity', 'get_entity', (['question'], {}), '(question)\n', (1369, 1379), False, 'from tokenizer import get_entity\n'), ((2257, 2346), 'json.dumps', 'dumps', (['respose'], {'ensure_ascii': '(False)', 'indent': '(4)', 'sort_keys': '(True)', 'separators': "(',', ': ')"}), "(respose, ensure_ascii=False, indent=4, sort_keys=True, separators=(\n ',', ': '))\n", (2262, 2346), False, 'from json import dumps\n'), ((2629, 2646), 'tornado.ioloop.IOLoop.instance', 'IOLoop.instance', ([], {}), '()\n', (2644, 2646), False, 'from tornado.ioloop import IOLoop\n'), ((774, 794), 'tokenizer.get_entity', 'get_entity', (['question'], {}), '(question)\n', (784, 794), False, 'from tokenizer import get_entity\n')] |
GDGSNF/proxy.py | proxy/http/chunk_parser.py | 3ee2824217286df3c108beadf3185eee35c28b49 | # -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
from typing import NamedTuple, Tuple, List, Optional
from ..common.utils import bytes_, find_http_line
from ..common.constants import CRLF, DEFAULT_BUFFER_SIZE
ChunkParserStates = NamedTuple(
'ChunkParserStates', [
('WAITING_FOR_SIZE', int),
('WAITING_FOR_DATA', int),
('COMPLETE', int),
],
)
chunkParserStates = ChunkParserStates(1, 2, 3)
class ChunkParser:
"""HTTP chunked encoding response parser."""
def __init__(self) -> None:
self.state = chunkParserStates.WAITING_FOR_SIZE
self.body: bytes = b'' # Parsed chunks
self.chunk: bytes = b'' # Partial chunk received
# Expected size of next following chunk
self.size: Optional[int] = None
def parse(self, raw: bytes) -> bytes:
more = len(raw) > 0
while more and self.state != chunkParserStates.COMPLETE:
more, raw = self.process(raw)
return raw
def process(self, raw: bytes) -> Tuple[bool, bytes]:
if self.state == chunkParserStates.WAITING_FOR_SIZE:
# Consume prior chunk in buffer
# in case chunk size without CRLF was received
raw = self.chunk + raw
self.chunk = b''
# Extract following chunk data size
line, raw = find_http_line(raw)
# CRLF not received or Blank line was received.
if line is None or line.strip() == b'':
self.chunk = raw
raw = b''
else:
self.size = int(line, 16)
self.state = chunkParserStates.WAITING_FOR_DATA
elif self.state == chunkParserStates.WAITING_FOR_DATA:
assert self.size is not None
remaining = self.size - len(self.chunk)
self.chunk += raw[:remaining]
raw = raw[remaining:]
if len(self.chunk) == self.size:
raw = raw[len(CRLF):]
self.body += self.chunk
if self.size == 0:
self.state = chunkParserStates.COMPLETE
else:
self.state = chunkParserStates.WAITING_FOR_SIZE
self.chunk = b''
self.size = None
return len(raw) > 0, raw
@staticmethod
def to_chunks(raw: bytes, chunk_size: int = DEFAULT_BUFFER_SIZE) -> bytes:
chunks: List[bytes] = []
for i in range(0, len(raw), chunk_size):
chunk = raw[i: i + chunk_size]
chunks.append(bytes_('{:x}'.format(len(chunk))))
chunks.append(chunk)
chunks.append(bytes_('{:x}'.format(0)))
chunks.append(b'')
return CRLF.join(chunks) + CRLF
| [((526, 637), 'typing.NamedTuple', 'NamedTuple', (['"""ChunkParserStates"""', "[('WAITING_FOR_SIZE', int), ('WAITING_FOR_DATA', int), ('COMPLETE', int)]"], {}), "('ChunkParserStates', [('WAITING_FOR_SIZE', int), (\n 'WAITING_FOR_DATA', int), ('COMPLETE', int)])\n", (536, 637), False, 'from typing import NamedTuple, Tuple, List, Optional\n')] |
10088/nova | nova/pci/stats.py | 972c06c608f0b00e9066d7f581fd81197065cf49 | # Copyright (c) 2013 Intel, Inc.
# Copyright (c) 2013 OpenStack Foundation
# 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 copy
import typing as ty
from oslo_config import cfg
from oslo_log import log as logging
from oslo_utils import strutils
from nova import exception
from nova import objects
from nova.objects import fields
from nova.objects import pci_device_pool
from nova.pci.request import PCI_REMOTE_MANAGED_TAG
from nova.pci import utils
from nova.pci import whitelist
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
# TODO(stephenfin): We might want to use TypedDict here. Refer to
# https://mypy.readthedocs.io/en/latest/kinds_of_types.html#typeddict for
# more information.
Pool = ty.Dict[str, ty.Any]
class PciDeviceStats(object):
"""PCI devices summary information.
According to the PCI SR-IOV spec, a PCI physical function can have up to
256 PCI virtual functions, thus the number of assignable PCI functions in
a cloud can be big. The scheduler needs to know all device availability
information in order to determine which compute hosts can support a PCI
request. Passing individual virtual device information to the scheduler
does not scale, so we provide summary information.
Usually the virtual functions provided by a host PCI device have the same
value for most properties, like vendor_id, product_id and class type.
The PCI stats class summarizes this information for the scheduler.
The pci stats information is maintained exclusively by compute node
resource tracker and updated to database. The scheduler fetches the
information and selects the compute node accordingly. If a compute
node is selected, the resource tracker allocates the devices to the
instance and updates the pci stats information.
This summary information will be helpful for cloud management also.
"""
pool_keys = ['product_id', 'vendor_id', 'numa_node', 'dev_type']
def __init__(
self,
numa_topology: 'objects.NUMATopology',
stats: 'objects.PCIDevicePoolList' = None,
dev_filter: whitelist.Whitelist = None,
) -> None:
self.numa_topology = numa_topology
self.pools = (
[pci_pool.to_dict() for pci_pool in stats] if stats else []
)
self.pools.sort(key=lambda item: len(item))
self.dev_filter = dev_filter or whitelist.Whitelist(
CONF.pci.passthrough_whitelist)
def _equal_properties(
self, dev: Pool, entry: Pool, matching_keys: ty.List[str],
) -> bool:
return all(dev.get(prop) == entry.get(prop)
for prop in matching_keys)
def _find_pool(self, dev_pool: Pool) -> ty.Optional[Pool]:
"""Return the first pool that matches dev."""
for pool in self.pools:
pool_keys = pool.copy()
del pool_keys['count']
del pool_keys['devices']
if (len(pool_keys.keys()) == len(dev_pool.keys()) and
self._equal_properties(dev_pool, pool_keys, list(dev_pool))):
return pool
return None
@staticmethod
def _ensure_remote_managed_tag(
dev: 'objects.PciDevice', pool: Pool):
"""Add a remote_managed tag depending on a device type if needed.
Network devices may be managed remotely, e.g. by a SmartNIC DPU. If
a tag has not been explicitly provided, populate it by assuming that
a device is not remote managed by default.
"""
if dev.dev_type not in (fields.PciDeviceType.SRIOV_VF,
fields.PciDeviceType.SRIOV_PF,
fields.PciDeviceType.VDPA):
return
# A tag is added here rather than at the client side to avoid an
# issue with having objects without this tag specified during an
# upgrade to the first version that supports handling this tag.
if pool.get(PCI_REMOTE_MANAGED_TAG) is None:
# NOTE: tags are compared as strings case-insensitively, see
# pci_device_prop_match in nova/pci/utils.py.
pool[PCI_REMOTE_MANAGED_TAG] = 'false'
def _create_pool_keys_from_dev(
self, dev: 'objects.PciDevice',
) -> ty.Optional[Pool]:
"""Create a stats pool dict that this dev is supposed to be part of
Note that this pool dict contains the stats pool's keys and their
values. 'count' and 'devices' are not included.
"""
# Don't add a device that doesn't have a matching device spec.
# This can happen during initial sync up with the controller
devspec = self.dev_filter.get_devspec(dev)
if not devspec:
return None
tags = devspec.get_tags()
pool = {k: getattr(dev, k) for k in self.pool_keys}
if tags:
pool.update(tags)
# NOTE(gibi): parent_ifname acts like a tag during pci claim but
# not provided as part of the whitelist spec as it is auto detected
# by the virt driver.
# This key is used for match InstancePciRequest backed by neutron ports
# that has resource_request and therefore that has resource allocation
# already in placement.
if dev.extra_info.get('parent_ifname'):
pool['parent_ifname'] = dev.extra_info['parent_ifname']
self._ensure_remote_managed_tag(dev, pool)
return pool
def _get_pool_with_device_type_mismatch(
self, dev: 'objects.PciDevice',
) -> ty.Optional[ty.Tuple[Pool, 'objects.PciDevice']]:
"""Check for device type mismatch in the pools for a given device.
Return (pool, device) if device type does not match or a single None
if the device type matches.
"""
for pool in self.pools:
for device in pool['devices']:
if device.address == dev.address:
if dev.dev_type != pool["dev_type"]:
return pool, device
return None
return None
def update_device(self, dev: 'objects.PciDevice') -> None:
"""Update a device to its matching pool."""
pool_device_info = self._get_pool_with_device_type_mismatch(dev)
if pool_device_info is None:
return None
pool, device = pool_device_info
pool['devices'].remove(device)
self._decrease_pool_count(self.pools, pool)
self.add_device(dev)
def add_device(self, dev: 'objects.PciDevice') -> None:
"""Add a device to its matching pool."""
dev_pool = self._create_pool_keys_from_dev(dev)
if dev_pool:
pool = self._find_pool(dev_pool)
if not pool:
dev_pool['count'] = 0
dev_pool['devices'] = []
self.pools.append(dev_pool)
self.pools.sort(key=lambda item: len(item))
pool = dev_pool
pool['count'] += 1
pool['devices'].append(dev)
@staticmethod
def _decrease_pool_count(
pool_list: ty.List[Pool], pool: Pool, count: int = 1,
) -> int:
"""Decrement pool's size by count.
If pool becomes empty, remove pool from pool_list.
"""
if pool['count'] > count:
pool['count'] -= count
count = 0
else:
count -= pool['count']
pool_list.remove(pool)
return count
def remove_device(self, dev: 'objects.PciDevice') -> None:
"""Remove one device from the first pool that it matches."""
dev_pool = self._create_pool_keys_from_dev(dev)
if dev_pool:
pool = self._find_pool(dev_pool)
if not pool:
raise exception.PciDevicePoolEmpty(
compute_node_id=dev.compute_node_id, address=dev.address)
pool['devices'].remove(dev)
self._decrease_pool_count(self.pools, pool)
def get_free_devs(self) -> ty.List['objects.PciDevice']:
free_devs: ty.List[objects.PciDevice] = []
for pool in self.pools:
free_devs.extend(pool['devices'])
return free_devs
def consume_requests(
self,
pci_requests: 'objects.InstancePCIRequests',
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']] = None,
) -> ty.Optional[ty.List['objects.PciDevice']]:
alloc_devices: ty.List[objects.PciDevice] = []
for request in pci_requests:
count = request.count
pools = self._filter_pools(self.pools, request, numa_cells)
# Failed to allocate the required number of devices. Return the
# devices already allocated during previous iterations back to
# their pools
if not pools:
LOG.error("Failed to allocate PCI devices for instance. "
"Unassigning devices back to pools. "
"This should not happen, since the scheduler "
"should have accurate information, and allocation "
"during claims is controlled via a hold "
"on the compute node semaphore.")
for d in range(len(alloc_devices)):
self.add_device(alloc_devices.pop())
return None
for pool in pools:
if pool['count'] >= count:
num_alloc = count
else:
num_alloc = pool['count']
count -= num_alloc
pool['count'] -= num_alloc
for d in range(num_alloc):
pci_dev = pool['devices'].pop()
self._handle_device_dependents(pci_dev)
pci_dev.request_id = request.request_id
alloc_devices.append(pci_dev)
if count == 0:
break
return alloc_devices
def _handle_device_dependents(self, pci_dev: 'objects.PciDevice') -> None:
"""Remove device dependents or a parent from pools.
In case the device is a PF, all of it's dependent VFs should
be removed from pools count, if these are present.
When the device is a VF, or a VDPA device, it's parent PF
pool count should be decreased, unless it is no longer in a pool.
"""
if pci_dev.dev_type == fields.PciDeviceType.SRIOV_PF:
vfs_list = pci_dev.child_devices
if vfs_list:
free_devs = self.get_free_devs()
for vf in vfs_list:
# NOTE(gibi): do not try to remove a device that are
# already removed
if vf in free_devs:
self.remove_device(vf)
elif pci_dev.dev_type in (
fields.PciDeviceType.SRIOV_VF,
fields.PciDeviceType.VDPA,
):
try:
parent = pci_dev.parent_device
# Make sure not to decrease PF pool count if this parent has
# been already removed from pools
if parent in self.get_free_devs():
self.remove_device(parent)
except exception.PciDeviceNotFound:
return
def _filter_pools_for_spec(
self, pools: ty.List[Pool], request: 'objects.InstancePCIRequest',
) -> ty.List[Pool]:
"""Filter out pools that don't match the request's device spec.
Exclude pools that do not match the specified ``vendor_id``,
``product_id`` and/or ``device_type`` field, or any of the other
arbitrary tags such as ``physical_network``, specified in the request.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:returns: A list of pools that can be used to support the request if
this is possible.
"""
request_specs = request.spec
return [
pool for pool in pools
if utils.pci_device_prop_match(pool, request_specs)
]
def _filter_pools_for_numa_cells(
self,
pools: ty.List[Pool],
request: 'objects.InstancePCIRequest',
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']],
) -> ty.List[Pool]:
"""Filter out pools with the wrong NUMA affinity, if required.
Exclude pools that do not have *suitable* PCI NUMA affinity.
``numa_policy`` determines what *suitable* means, being one of
PREFERRED (nice-to-have), LEGACY (must-have-if-available) and REQUIRED
(must-have). We iterate through the various policies in order of
strictness. This means that even if we only *prefer* PCI-NUMA affinity,
we will still attempt to provide it if possible.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:param numa_cells: A list of InstanceNUMACell objects whose ``id``
corresponds to the ``id`` of host NUMACells.
:returns: A list of pools that can, together, provide at least
``requested_count`` PCI devices with the level of NUMA affinity
required by ``numa_policy``, else all pools that can satisfy this
policy even if it's not enough.
"""
if not numa_cells:
return pools
# we default to the 'legacy' policy for...of course...legacy reasons
requested_policy = fields.PCINUMAAffinityPolicy.LEGACY
if 'numa_policy' in request:
requested_policy = request.numa_policy or requested_policy
requested_count = request.count
numa_cell_ids = [cell.id for cell in numa_cells]
# filter out pools which numa_node is not included in numa_cell_ids
filtered_pools = [
pool for pool in pools if any(utils.pci_device_prop_match(
pool, [{'numa_node': cell}]) for cell in numa_cell_ids)]
# we can't apply a less strict policy than the one requested, so we
# need to return if we've demanded a NUMA affinity of REQUIRED.
# However, NUMA affinity is a good thing. If we can get enough devices
# with the stricter policy then we will use them.
if requested_policy == fields.PCINUMAAffinityPolicy.REQUIRED or sum(
pool['count'] for pool in filtered_pools) >= requested_count:
return filtered_pools
# the SOCKET policy is a bit of a special case. It's less strict than
# REQUIRED (so REQUIRED will automatically fulfil SOCKET, at least
# with our assumption of never having multiple sockets per NUMA node),
# but not always more strict than LEGACY: a PCI device with no NUMA
# affinity will fulfil LEGACY but not SOCKET. If we have SOCKET,
# process it here and don't continue.
if requested_policy == fields.PCINUMAAffinityPolicy.SOCKET:
return self._filter_pools_for_socket_affinity(pools, numa_cells)
# some systems don't report NUMA node info for PCI devices, in which
# case None is reported in 'pci_device.numa_node'. The LEGACY policy
# allows us to use these devices so we include None in the list of
# suitable NUMA cells.
numa_cell_ids.append(None)
# filter out pools which numa_node is not included in numa_cell_ids
filtered_pools = [
pool for pool in pools if any(utils.pci_device_prop_match(
pool, [{'numa_node': cell}]) for cell in numa_cell_ids)]
# once again, we can't apply a less strict policy than the one
# requested, so we need to return if we've demanded a NUMA affinity of
# LEGACY. Similarly, we will also return if we have enough devices to
# satisfy this somewhat strict policy.
if requested_policy == fields.PCINUMAAffinityPolicy.LEGACY or sum(
pool['count'] for pool in filtered_pools) >= requested_count:
return filtered_pools
# if we've got here, we're using the PREFERRED policy and weren't able
# to provide anything with stricter affinity. Use whatever devices you
# can, folks.
return sorted(
pools, key=lambda pool: pool.get('numa_node') not in numa_cell_ids)
def _filter_pools_for_socket_affinity(
self,
pools: ty.List[Pool],
numa_cells: ty.List['objects.InstanceNUMACell'],
) -> ty.List[Pool]:
host_cells = self.numa_topology.cells
# bail early if we don't have socket information for all host_cells.
# This could happen if we're running on an weird older system with
# multiple sockets per NUMA node, which is a configuration that we
# explicitly chose not to support.
if any(cell.socket is None for cell in host_cells):
LOG.debug('No socket information in host NUMA cell(s).')
return []
# get a set of host sockets that the guest cells are in. Since guest
# cell IDs map to host cell IDs, we can just lookup the latter's
# socket.
socket_ids = set()
for guest_cell in numa_cells:
for host_cell in host_cells:
if guest_cell.id == host_cell.id:
socket_ids.add(host_cell.socket)
# now get a set of host NUMA nodes that are in the above sockets
allowed_numa_nodes = set()
for host_cell in host_cells:
if host_cell.socket in socket_ids:
allowed_numa_nodes.add(host_cell.id)
# filter out pools that are not in one of the correct host NUMA nodes.
return [
pool for pool in pools if any(
utils.pci_device_prop_match(pool, [{'numa_node': numa_node}])
for numa_node in allowed_numa_nodes
)
]
def _filter_pools_for_unrequested_pfs(
self, pools: ty.List[Pool], request: 'objects.InstancePCIRequest',
) -> ty.List[Pool]:
"""Filter out pools with PFs, unless these are required.
This is necessary in cases where PFs and VFs have the same product_id
and generally useful elsewhere.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:returns: A list of pools that can be used to support the request if
this is possible.
"""
if all(
spec.get('dev_type') != fields.PciDeviceType.SRIOV_PF
for spec in request.spec
):
pools = [
pool for pool in pools
if not pool.get('dev_type') == fields.PciDeviceType.SRIOV_PF
]
return pools
def _filter_pools_for_unrequested_vdpa_devices(
self,
pools: ty.List[Pool],
request: 'objects.InstancePCIRequest',
) -> ty.List[Pool]:
"""Filter out pools with VDPA devices, unless these are required.
This is necessary as vdpa devices require special handling and
should not be allocated to generic pci device requests.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:returns: A list of pools that can be used to support the request if
this is possible.
"""
if all(
spec.get('dev_type') != fields.PciDeviceType.VDPA
for spec in request.spec
):
pools = [
pool for pool in pools
if not pool.get('dev_type') == fields.PciDeviceType.VDPA
]
return pools
def _filter_pools_for_unrequested_remote_managed_devices(
self, pools: ty.List[Pool], request: 'objects.InstancePCIRequest',
) -> ty.List[Pool]:
"""Filter out pools with remote_managed devices, unless requested.
Remote-managed devices are not usable for legacy SR-IOV or hardware
offload scenarios and must be excluded from allocation.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:returns: A list of pools that can be used to support the request if
this is possible.
"""
if all(not strutils.bool_from_string(spec.get(PCI_REMOTE_MANAGED_TAG))
for spec in request.spec):
pools = [pool for pool in pools
if not strutils.bool_from_string(
pool.get(PCI_REMOTE_MANAGED_TAG))]
return pools
def _filter_pools(
self,
pools: ty.List[Pool],
request: 'objects.InstancePCIRequest',
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']],
) -> ty.Optional[ty.List[Pool]]:
"""Determine if an individual PCI request can be met.
Filter pools, which are collections of devices with similar traits, to
identify those that can support the provided PCI request.
If ``numa_cells`` is provided then NUMA locality may be taken into
account, depending on the value of ``request.numa_policy``.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:param numa_cells: A list of InstanceNUMACell objects whose ``id``
corresponds to the ``id`` of host NUMACell objects.
:returns: A list of pools that can be used to support the request if
this is possible, else None.
"""
# NOTE(vladikr): This code may be open to race conditions.
# Two concurrent requests may succeed when called support_requests
# because this method does not remove related devices from the pools
# Firstly, let's exclude all devices that don't match our spec (e.g.
# they've got different PCI IDs or something)
before_count = sum([pool['count'] for pool in pools])
pools = self._filter_pools_for_spec(pools, request)
after_count = sum([pool['count'] for pool in pools])
if after_count < before_count:
LOG.debug(
'Dropped %d device(s) due to mismatched PCI attribute(s)',
before_count - after_count
)
if after_count < request.count:
LOG.debug('Not enough PCI devices left to satisfy request')
return None
# Next, let's exclude all devices that aren't on the correct NUMA node
# or socket, *assuming* we have devices and care about that, as
# determined by policy
before_count = after_count
pools = self._filter_pools_for_numa_cells(pools, request, numa_cells)
after_count = sum([pool['count'] for pool in pools])
if after_count < before_count:
LOG.debug(
'Dropped %d device(s) as they are on the wrong NUMA node(s)',
before_count - after_count
)
if after_count < request.count:
LOG.debug('Not enough PCI devices left to satisfy request')
return None
# If we're not requesting PFs then we should not use these.
# Exclude them.
before_count = after_count
pools = self._filter_pools_for_unrequested_pfs(pools, request)
after_count = sum([pool['count'] for pool in pools])
if after_count < before_count:
LOG.debug(
'Dropped %d device(s) as they are PFs which we have not '
'requested',
before_count - after_count
)
if after_count < request.count:
LOG.debug('Not enough PCI devices left to satisfy request')
return None
# If we're not requesting VDPA devices then we should not use these
# either. Exclude them.
before_count = after_count
pools = self._filter_pools_for_unrequested_vdpa_devices(pools, request)
after_count = sum([pool['count'] for pool in pools])
if after_count < before_count:
LOG.debug(
'Dropped %d device(s) as they are VDPA devices which we have '
'not requested',
before_count - after_count
)
# If we're not requesting remote_managed devices then we should not
# use these either. Exclude them.
before_count = after_count
pools = self._filter_pools_for_unrequested_remote_managed_devices(
pools, request)
after_count = sum([pool['count'] for pool in pools])
if after_count < before_count:
LOG.debug(
'Dropped %d device(s) as they are remote-managed devices which'
'we have not requested',
before_count - after_count
)
if after_count < request.count:
LOG.debug('Not enough PCI devices left to satisfy request')
return None
return pools
def support_requests(
self,
requests: ty.List['objects.InstancePCIRequest'],
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']] = None,
) -> bool:
"""Determine if the PCI requests can be met.
Determine, based on a compute node's PCI stats, if an instance can be
scheduled on the node. **Support does not mean real allocation**.
If ``numa_cells`` is provided then NUMA locality may be taken into
account, depending on the value of ``numa_policy``.
:param requests: A list of InstancePCIRequest object describing the
types, quantities and required NUMA affinities of devices we want.
:type requests: nova.objects.InstancePCIRequests
:param numa_cells: A list of InstanceNUMACell objects whose ``id``
corresponds to the ``id`` of host NUMACells, or None.
:returns: Whether this compute node can satisfy the given request.
"""
# NOTE(yjiang5): this function has high possibility to fail,
# so no exception should be triggered for performance reason.
return all(
self._filter_pools(self.pools, r, numa_cells) for r in requests
)
def _apply_request(
self,
pools: ty.List[Pool],
request: 'objects.InstancePCIRequest',
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']] = None,
) -> bool:
"""Apply an individual PCI request.
Apply a PCI request against a given set of PCI device pools, which are
collections of devices with similar traits.
If ``numa_cells`` is provided then NUMA locality may be taken into
account, depending on the value of ``request.numa_policy``.
:param pools: A list of PCI device pool dicts
:param request: An InstancePCIRequest object describing the type,
quantity and required NUMA affinity of device(s) we want.
:param numa_cells: A list of InstanceNUMACell objects whose ``id``
corresponds to the ``id`` of host NUMACell objects.
:returns: True if the request was applied against the provided pools
successfully, else False.
"""
# NOTE(vladikr): This code maybe open to race conditions.
# Two concurrent requests may succeed when called support_requests
# because this method does not remove related devices from the pools
filtered_pools = self._filter_pools(pools, request, numa_cells)
if not filtered_pools:
return False
count = request.count
for pool in filtered_pools:
count = self._decrease_pool_count(pools, pool, count)
if not count:
break
return True
def apply_requests(
self,
requests: ty.List['objects.InstancePCIRequest'],
numa_cells: ty.Optional[ty.List['objects.InstanceNUMACell']] = None,
) -> None:
"""Apply PCI requests to the PCI stats.
This is used in multiple instance creation, when the scheduler has to
maintain how the resources are consumed by the instances.
If ``numa_cells`` is provided then NUMA locality may be taken into
account, depending on the value of ``numa_policy``.
:param requests: A list of InstancePCIRequest object describing the
types, quantities and required NUMA affinities of devices we want.
:type requests: nova.objects.InstancePCIRequests
:param numa_cells: A list of InstanceNUMACell objects whose ``id``
corresponds to the ``id`` of host NUMACells, or None.
:raises: exception.PciDeviceRequestFailed if this compute node cannot
satisfy the given request.
"""
if not all(
self._apply_request(self.pools, r, numa_cells) for r in requests
):
raise exception.PciDeviceRequestFailed(requests=requests)
def __iter__(self) -> ty.Iterator[Pool]:
pools: ty.List[Pool] = []
for pool in self.pools:
pool = copy.deepcopy(pool)
# 'devices' shouldn't be part of stats
if 'devices' in pool:
del pool['devices']
pools.append(pool)
return iter(pools)
def clear(self) -> None:
"""Clear all the stats maintained."""
self.pools = []
def __eq__(self, other: object) -> bool:
if not isinstance(other, PciDeviceStats):
return NotImplemented
return self.pools == other.pools
def to_device_pools_obj(self) -> 'objects.PciDevicePoolList':
"""Return the contents of the pools as a PciDevicePoolList object."""
stats = [x for x in self]
return pci_device_pool.from_pci_stats(stats)
def has_remote_managed_device_pools(self) -> bool:
"""Determine whether remote managed device pools are present on a host.
The check is pool-based, not free device-based and is NUMA cell
agnostic.
"""
dummy_req = objects.InstancePCIRequest(
count=0,
spec=[{'remote_managed': True}]
)
pools = self._filter_pools_for_spec(self.pools, dummy_req)
return bool(pools)
| [((1061, 1088), 'oslo_log.log.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (1078, 1088), True, 'from oslo_log import log as logging\n'), ((30746, 30783), 'nova.objects.pci_device_pool.from_pci_stats', 'pci_device_pool.from_pci_stats', (['stats'], {}), '(stats)\n', (30776, 30783), False, 'from nova.objects import pci_device_pool\n'), ((31043, 31111), 'nova.objects.InstancePCIRequest', 'objects.InstancePCIRequest', ([], {'count': '(0)', 'spec': "[{'remote_managed': True}]"}), "(count=0, spec=[{'remote_managed': True}])\n", (31069, 31111), False, 'from nova import objects\n'), ((2940, 2991), 'nova.pci.whitelist.Whitelist', 'whitelist.Whitelist', (['CONF.pci.passthrough_whitelist'], {}), '(CONF.pci.passthrough_whitelist)\n', (2959, 2991), False, 'from nova.pci import whitelist\n'), ((29899, 29950), 'nova.exception.PciDeviceRequestFailed', 'exception.PciDeviceRequestFailed', ([], {'requests': 'requests'}), '(requests=requests)\n', (29931, 29950), False, 'from nova import exception\n'), ((30082, 30101), 'copy.deepcopy', 'copy.deepcopy', (['pool'], {}), '(pool)\n', (30095, 30101), False, 'import copy\n'), ((8300, 8391), 'nova.exception.PciDevicePoolEmpty', 'exception.PciDevicePoolEmpty', ([], {'compute_node_id': 'dev.compute_node_id', 'address': 'dev.address'}), '(compute_node_id=dev.compute_node_id, address=\n dev.address)\n', (8328, 8391), False, 'from nova import exception\n'), ((12685, 12733), 'nova.pci.utils.pci_device_prop_match', 'utils.pci_device_prop_match', (['pool', 'request_specs'], {}), '(pool, request_specs)\n', (12712, 12733), False, 'from nova.pci import utils\n'), ((14626, 14682), 'nova.pci.utils.pci_device_prop_match', 'utils.pci_device_prop_match', (['pool', "[{'numa_node': cell}]"], {}), "(pool, [{'numa_node': cell}])\n", (14653, 14682), False, 'from nova.pci import utils\n'), ((16218, 16274), 'nova.pci.utils.pci_device_prop_match', 'utils.pci_device_prop_match', (['pool', "[{'numa_node': cell}]"], {}), "(pool, [{'numa_node': cell}])\n", (16245, 16274), False, 'from nova.pci import utils\n'), ((18483, 18544), 'nova.pci.utils.pci_device_prop_match', 'utils.pci_device_prop_match', (['pool', "[{'numa_node': numa_node}]"], {}), "(pool, [{'numa_node': numa_node}])\n", (18510, 18544), False, 'from nova.pci import utils\n')] |
Codingprivacy/Multiple-Rename | Use.py | 486289e8158487dad058cd8f781ac27bc9a5fc02 | import multiple
multiple.rename("C:/Users/Username/Desktop",'new_name',33,'.exe')
"""this above lines renames all the files of the folder Desktop to 'new_name' and
count starts from 33 to further (we can also provide 1 to start it from 1) and
extension is given '.exe'
hence the files will be renamed like :
1. new_name33.exe
2. new_name34.exe and so on
""" | [((17, 85), 'multiple.rename', 'multiple.rename', (['"""C:/Users/Username/Desktop"""', '"""new_name"""', '(33)', '""".exe"""'], {}), "('C:/Users/Username/Desktop', 'new_name', 33, '.exe')\n", (32, 85), False, 'import multiple\n')] |
ohel/pyorbital-gizmod-tweaks | ReadSymLink.py | 4c02783d1c6287df508351467a5c203a11430b07 | import os
def readlinkabs(l):
"""
Return an absolute path for the destination
of a symlink
"""
if not (os.path.islink(l)):
return None
p = os.readlink(l)
if os.path.isabs(p):
return p
return os.path.join(os.path.dirname(l), p)
| [((173, 187), 'os.readlink', 'os.readlink', (['l'], {}), '(l)\n', (184, 187), False, 'import os\n'), ((195, 211), 'os.path.isabs', 'os.path.isabs', (['p'], {}), '(p)\n', (208, 211), False, 'import os\n'), ((125, 142), 'os.path.islink', 'os.path.islink', (['l'], {}), '(l)\n', (139, 142), False, 'import os\n'), ((254, 272), 'os.path.dirname', 'os.path.dirname', (['l'], {}), '(l)\n', (269, 272), False, 'import os\n')] |
IanTBlack/picamera2 | examples/capture_circular.py | 4d31a56cdb0d8360e71927e754fc6bef50bec360 | #!/usr/bin/python3
import time
import numpy as np
from picamera2.encoders import H264Encoder
from picamera2.outputs import CircularOutput
from picamera2 import Picamera2
lsize = (320, 240)
picam2 = Picamera2()
video_config = picam2.video_configuration(main={"size": (1280, 720), "format": "RGB888"},
lores={"size": lsize, "format": "YUV420"})
picam2.configure(video_config)
picam2.start_preview()
encoder = H264Encoder(1000000, repeat=True)
encoder.output = CircularOutput()
picam2.encoder = encoder
picam2.start()
picam2.start_encoder()
w, h = lsize
prev = None
encoding = False
ltime = 0
while True:
cur = picam2.capture_buffer("lores")
cur = cur[:w * h].reshape(h, w)
if prev is not None:
# Measure pixels differences between current and
# previous frame
mse = np.square(np.subtract(cur, prev)).mean()
if mse > 7:
if not encoding:
epoch = int(time.time())
encoder.output.fileoutput = "{}.h264".format(epoch)
encoder.output.start()
encoding = True
print("New Motion", mse)
ltime = time.time()
else:
if encoding and time.time() - ltime > 5.0:
encoder.output.stop()
encoding = False
prev = cur
picam2.stop_encoder()
| [((201, 212), 'picamera2.Picamera2', 'Picamera2', ([], {}), '()\n', (210, 212), False, 'from picamera2 import Picamera2\n'), ((452, 485), 'picamera2.encoders.H264Encoder', 'H264Encoder', (['(1000000)'], {'repeat': '(True)'}), '(1000000, repeat=True)\n', (463, 485), False, 'from picamera2.encoders import H264Encoder\n'), ((503, 519), 'picamera2.outputs.CircularOutput', 'CircularOutput', ([], {}), '()\n', (517, 519), False, 'from picamera2.outputs import CircularOutput\n'), ((1178, 1189), 'time.time', 'time.time', ([], {}), '()\n', (1187, 1189), False, 'import time\n'), ((857, 879), 'numpy.subtract', 'np.subtract', (['cur', 'prev'], {}), '(cur, prev)\n', (868, 879), True, 'import numpy as np\n'), ((965, 976), 'time.time', 'time.time', ([], {}), '()\n', (974, 976), False, 'import time\n'), ((1232, 1243), 'time.time', 'time.time', ([], {}), '()\n', (1241, 1243), False, 'import time\n')] |
barendt/biopython | Bio/NeuralNetwork/Gene/Pattern.py | 391bcdbee7f821bff3e12b75c635a06bc1b2dcea | """Generic functionality useful for all gene representations.
This module contains classes which can be used for all the different
types of patterns available for representing gene information (ie. motifs,
signatures and schemas). These are the general classes which should be
handle any of the different specific patterns.
"""
# standard library
import random
# biopython
from Bio import utils
from Bio.Seq import Seq, MutableSeq
class PatternIO:
"""Allow reading and writing of patterns to files.
This just defines a simple persistance class for patterns, making
it easy to write them to a file and read 'em back.
"""
def __init__(self, alphabet = None):
"""Intialize the reader and writer class.
Arguments:
o alphabet - An optional argument specifying the alphabet
which patterns should follow. If an alphabet is set it'll be used
to verify that all patterns follow it.
Attributes:
o separator - A character to use in separating items in a signature
when it is written to a file and read back. This character should
not be in the possible alphabet of the sequences, or there will
be trouble.
"""
self._alphabet = alphabet
self.separator = ";"
def write(self, pattern_list, output_handle):
"""Write a list of patterns to the given handle.
"""
for pattern in pattern_list:
# deal with signatures, concatentate them with the separator
if (type(pattern) == type([]) or
type(pattern) == type(tuple([]))):
string_pattern = self.separator.join(pattern)
# deal with the normal cases
else:
string_pattern = pattern
output_handle.write("%s\n" % string_pattern)
def write_seq(self, seq_pattern_list, output_handle):
"""Convenience function to write Seq objects to a file.
This can take Seqs and MutableSeqs, and write them to a file
as strings.
"""
# convert the seq patterns into just string patterns
all_patterns = []
for seq_pattern in seq_pattern_list:
if isinstance(seq_pattern, MutableSeq):
seq = seq_pattern.toseq()
all_patterns.append(seq.data)
elif isinstance(seq_pattern, Seq):
all_patterns.append(seq_pattern.data)
else:
raise ValueError("Unexpected pattern type %r" % seq_pattern)
self.write(all_patterns, output_handle)
def read(self, input_handle):
"""Read patterns from the specified handle.
"""
all_patterns = []
while 1:
cur_line = input_handle.readline()
if not(cur_line):
break
cur_pattern = cur_line.rstrip()
# split up signatures
if cur_pattern.find(self.separator) >= 0:
cur_pattern = tuple(cur_pattern.split(self.separator))
if self._alphabet is not None:
# make single patterns (not signatures) into lists, so we
# can check signatures and single patterns the same
if type(cur_pattern) != type(tuple([])):
test_pattern = [cur_pattern]
else:
test_pattern = cur_pattern
for pattern_item in test_pattern:
pattern_seq = Seq(pattern_item, self._alphabet)
if not(utils.verify_alphabet(pattern_seq)):
raise ValueError("Pattern %s not matching alphabet %s"
% (cur_pattern, self._alphabet))
all_patterns.append(cur_pattern)
return all_patterns
class PatternRepository:
"""This holds a list of specific patterns found in sequences.
This is designed to be a general holder for a set of patterns and
should be subclassed for specific implementations (ie. holding Motifs
or Signatures.
"""
def __init__(self, pattern_info):
"""Initialize a repository with patterns,
Arguments:
o pattern_info - A representation of all of the patterns found in
a *Finder search. This should be a dictionary, where the keys
are patterns, and the values are the number of times a pattern is
found.
The patterns are represented interally as a list of two
tuples, where the first element is the number of times a pattern
occurs, and the second is the pattern itself. This makes it easy
to sort the list and return the top N patterns.
"""
self._pattern_dict = pattern_info
# create the list representation
self._pattern_list = []
for pattern_name in self._pattern_dict.keys():
self._pattern_list.append((self._pattern_dict[pattern_name],
pattern_name))
self._pattern_list.sort()
self._pattern_list.reverse()
def get_all(self):
"""Retrieve all of the patterns in the repository.
"""
patterns = []
for pattern_info in self._pattern_list:
patterns.append(pattern_info[1])
return patterns
def get_random(self, num_patterns):
"""Retrieve the specified number of patterns randomly.
Randomly selects patterns from the list and returns them.
Arguments:
o num_patterns - The total number of patterns to return.
"""
all_patterns = []
while len(all_patterns) < num_patterns:
# pick a pattern, and only add it if it is not already present
new_pattern_info = random.choice(self._pattern_list)
if new_pattern_info[1] not in all_patterns:
all_patterns.append(new_pattern_info[1])
return all_patterns
def get_top_percentage(self, percent):
"""Return a percentage of the patterns.
This returns the top 'percent' percentage of the patterns in the
repository.
"""
all_patterns = self.get_all()
num_to_return = int(len(all_patterns) * percent)
return all_patterns[:num_to_return]
def get_top(self, num_patterns):
"""Return the specified number of most frequently occurring patterns
Arguments:
o num_patterns - The number of patterns to return.
"""
all_patterns = []
for pattern_info in self._pattern_list[:num_patterns]:
all_patterns.append(pattern_info[1])
return all_patterns
def get_differing(self, top_num, bottom_num):
"""Retrieve patterns that are at the extreme ranges.
This returns both patterns at the top of the list (ie. the same as
returned by get_top) and at the bottom of the list. This
is especially useful for patterns that are the differences between
two sets of patterns.
Arguments:
o top_num - The number of patterns to take from the top of the list.
o bottom_num - The number of patterns to take from the bottom of
the list.
"""
all_patterns = []
# first get from the top of the list
for pattern_info in self._pattern_list[:top_num]:
all_patterns.append(pattern_info[1])
# then from the bottom
for pattern_info in self._pattern_list[-bottom_num:]:
all_patterns.append(pattern_info[1])
return all_patterns
def remove_polyA(self, at_percentage = .9):
"""Remove patterns which are likely due to polyA tails from the lists.
This is just a helper function to remove pattenrs which are likely
just due to polyA tails, and thus are not really great motifs.
This will also get rid of stuff like ATATAT, which might be a
useful motif, so use at your own discretion.
XXX Could we write a more general function, based on info content
or something like that?
Arguments:
o at_percentage - The percentage of A and T residues in a pattern
that qualifies it for being removed.
"""
remove_list = []
# find all of the really AT rich patterns
for pattern_info in self._pattern_list:
pattern_at = float(pattern_info[1].count('A') + pattern_info[1].count('T')) / len(pattern_info[1])
if pattern_at > at_percentage:
remove_list.append(pattern_info)
# now remove them from the master list
for to_remove in remove_list:
self._pattern_list.remove(to_remove)
def count(self, pattern):
"""Return the number of times the specified pattern is found.
"""
try:
return self._pattern_dict[pattern]
except KeyError:
return 0
| [((5776, 5809), 'random.choice', 'random.choice', (['self._pattern_list'], {}), '(self._pattern_list)\n', (5789, 5809), False, 'import random\n'), ((3497, 3530), 'Bio.Seq.Seq', 'Seq', (['pattern_item', 'self._alphabet'], {}), '(pattern_item, self._alphabet)\n', (3500, 3530), False, 'from Bio.Seq import Seq, MutableSeq\n'), ((3558, 3592), 'Bio.utils.verify_alphabet', 'utils.verify_alphabet', (['pattern_seq'], {}), '(pattern_seq)\n', (3579, 3592), False, 'from Bio import utils\n')] |
WHOIGit/nes-lter-ims | neslter/parsing/nut/__init__.py | d4cc96c10da56ca33286af84d669625b67170522 | from .nut import parse_nut, format_nut, merge_nut_bottles
| [] |
jpieper/legtool | legtool/tabs/servo_tab.py | ab3946051bd16817b61d3073ce7be8bd27af90d0 | # Copyright 2014 Josh Pieper, jjp@pobox.com.
#
# 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 functools
import trollius as asyncio
from trollius import Task, From, Return
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
from ..servo import selector
from .common import BoolContext
from . import gazebo_config_dialog
def spawn(callback):
def start():
Task(callback())
return start
class ServoTab(object):
def __init__(self, ui, status):
self.ui = ui
self.status = status
self.servo_controls = []
self.monitor_thread = None
self.servo_model = ''
self.servo_name_map = {}
self.ui.statusText.setText('not connected')
self.ui.connectButton.clicked.connect(
spawn(self.handle_connect_clicked))
self.ui.typeCombo.currentIndexChanged.connect(self.handle_type_change)
self.handle_type_change()
self.ui.configureGazeboButton.clicked.connect(
self.handle_configure_gazebo)
servo_layout = QtGui.QVBoxLayout()
servo_layout.setSpacing(0)
servo_layout.setContentsMargins(0, 0, 0, 0)
self.ui.scrollContents.setLayout(servo_layout)
self.ui.servoCountSpin.valueChanged.connect(self.handle_servo_count)
self.handle_servo_count()
self.ui.powerCombo.currentIndexChanged.connect(
spawn(self.handle_power))
self.ui.captureCurrentButton.clicked.connect(
spawn(self.handle_capture_current))
self.update_connected(False)
self.ui.addPoseButton.clicked.connect(self.handle_add_pose)
self.ui.removePoseButton.clicked.connect(self.handle_remove_pose)
self.ui.moveToPoseButton.clicked.connect(
spawn(self.handle_move_to_pose))
self.ui.updatePoseButton.clicked.connect(self.handle_update_pose)
self.ui.poseList.currentItemChanged.connect(
self.handle_poselist_current_changed)
self.controller = None
self.servo_update = BoolContext()
def resizeEvent(self, event):
pass
def poses(self):
result = []
for i in range(self.ui.poseList.count()):
result.append(self.ui.poseList.item(i).text())
return result
def pose(self, name):
for i in range(self.ui.poseList.count()):
if self.ui.poseList.item(i).text() == name:
return self.ui.poseList.item(i).data(QtCore.Qt.UserRole)
return dict([(i, 0.0) for i in range(self.ui.servoCountSpin.value())])
@asyncio.coroutine
def handle_connect_clicked(self):
val = self.ui.typeCombo.currentText().lower()
try:
self.controller = yield From(
selector.select_servo(
val,
serial_port=self.ui.serialPortCombo.currentText(),
model_name=self.servo_model,
servo_name_map=self.servo_name_map))
self.ui.statusText.setText('connected')
self.update_connected(True)
except Exception as e:
self.ui.statusText.setText('error: %s' % str(e))
self.update_connected(False)
def handle_type_change(self):
val = self.ui.typeCombo.currentText().lower()
self.ui.serialPortCombo.setEnabled(val == 'herkulex')
self.ui.configureGazeboButton.setEnabled(val == 'gazebo')
def handle_configure_gazebo(self):
servo_name_map = self.servo_name_map.copy()
for x in range(self.ui.servoCountSpin.value()):
if not x in servo_name_map:
servo_name_map[x] = ''
dialog = gazebo_config_dialog.GazeboConfigDialog(
self.servo_model, servo_name_map)
dialog.setModal(True)
result = dialog.exec_()
if result == QtGui.QDialog.Rejected:
return
self.servo_model = dialog.model_name()
self.servo_name_map = dialog.servo_name_map()
def handle_servo_count(self):
count = self.ui.servoCountSpin.value()
while len(self.servo_controls) > count:
# Remove the last one
last = self.servo_controls[-1]
widget = last['widget']
self.ui.scrollContents.layout().removeWidget(widget)
widget.deleteLater()
self.servo_controls = self.servo_controls[:-1]
while len(self.servo_controls) < count:
# Add a new one.
servo_id = len(self.servo_controls)
label = QtGui.QLabel()
label.setText('ID %d:' % servo_id)
slider = QtGui.QSlider(QtCore.Qt.Horizontal)
slider.setRange(-180, 180)
doublespin = QtGui.QDoubleSpinBox()
doublespin.setRange(-180, 180)
doublespin.setDecimals(1)
save = QtGui.QPushButton()
save.setText("Save")
move = QtGui.QPushButton()
move.setText("Move")
current = QtGui.QLabel()
current.setText('N/A')
current.setMinimumWidth(60)
widget = QtGui.QWidget()
layout = QtGui.QHBoxLayout(widget)
layout.addWidget(label)
layout.addWidget(slider)
layout.addWidget(doublespin)
layout.addWidget(save)
layout.addWidget(move)
layout.addWidget(current)
slider.valueChanged.connect(
functools.partial(self.handle_servo_slider, servo_id))
doublespin.valueChanged.connect(
functools.partial(self.handle_servo_spin, servo_id))
save.clicked.connect(
functools.partial(self.handle_servo_save, servo_id))
move.clicked.connect(
functools.partial(self.handle_servo_move, servo_id))
self.ui.scrollContents.layout().addWidget(widget)
self.servo_controls.append({
'widget': widget,
'label': label,
'slider': slider,
'doublespin': doublespin,
'save': save,
'move': move,
'current': current})
@asyncio.coroutine
def handle_power(self):
text = self.ui.powerCombo.currentText().lower()
value = None
if text == 'free':
value = selector.POWER_FREE
elif text == 'brake':
value = selector.POWER_BRAKE
elif text == 'drive':
value = selector.POWER_ENABLE
else:
raise NotImplementedError()
yield From(self.controller.enable_power(value))
def update_connected(self, value):
self.ui.controlGroup.setEnabled(value)
self.ui.posesGroup.setEnabled(value)
if self.monitor_thread is not None:
self.monitor_thread.cancel()
self.monitor_thread = None
if value:
self.handle_power()
self.monitor_thread = Task(self.monitor_status())
@asyncio.coroutine
def monitor_status(self):
voltages = {}
temperatures = {}
ident = 0
while True:
if (self.controller is not None and
hasattr(self.controller, 'get_voltage')):
try:
ident = (ident + 1) % len(self.servo_controls)
this_voltage = yield From(
self.controller.get_voltage([ident]))
voltages.update(this_voltage)
# Get all temperatures.
this_temp = yield From(
self.controller.get_temperature([ident]))
temperatures.update(this_temp)
def non_None(value):
return [x for x in value if x is not None]
message = "Servo status: "
if len(non_None(voltages.values())):
message += "%.1f/%.1fV" % (
min(non_None(voltages.values())),
max(non_None(voltages.values())))
if len(non_None(temperatures.values())):
message += " %.1f/%.1fC" % (
min(non_None(temperatures.values())),
max(non_None(temperatures.values())))
self.status.showMessage(message, 10000)
except Exception as e:
traceback.print_exc()
print "Error reading servo:", type(e), e
yield From(asyncio.sleep(2.0))
@asyncio.coroutine
def set_single_pose(self, servo_id, value):
yield From(
self.controller.set_single_pose(servo_id, value, pose_time=0.2))
def handle_servo_slider(self, servo_id, event):
if self.servo_update.value:
return
with self.servo_update:
control = self.servo_controls[servo_id]
value = control['slider'].value()
control['doublespin'].setValue(value)
Task(self.set_single_pose(servo_id, value))
def handle_servo_spin(self, servo_id, event):
if self.servo_update.value:
return
with self.servo_update:
control = self.servo_controls[servo_id]
value = control['doublespin'].value()
control['slider'].setSliderPosition(int(value))
Task(self.set_single_pose(servo_id, value))
def handle_servo_save(self, servo_id):
if self.ui.poseList.currentRow() < 0:
return
current_data = self.ui.poseList.currentItem().data(
QtCore.Qt.UserRole)
current_data[servo_id] = (
self.servo_controls[servo_id]['doublespin'].value())
self.ui.poseList.currentItem().setData(
QtCore.Qt.UserRole, current_data)
self.handle_poselist_current_changed(None, None)
def handle_servo_move(self, servo_id):
if self.ui.poseList.currentRow() < 0:
return
data = self.ui.poseList.currentItem().data(QtCore.Qt.UserRole)
self.servo_controls[servo_id]['doublespin'].setValue(data[servo_id])
@asyncio.coroutine
def handle_capture_current(self):
with self.servo_update:
results = yield From(
self.controller.get_pose(range(len(self.servo_controls))))
for ident, angle in results.iteritems():
if angle is None:
continue
control = self.servo_controls[ident]
control['slider'].setSliderPosition(int(angle))
control['doublespin'].setValue(angle)
def add_list_pose(self, name):
self.ui.poseList.addItem(name)
item = self.ui.poseList.item(self.ui.poseList.count() - 1)
item.setFlags(QtCore.Qt.ItemIsEnabled |
QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsEditable |
QtCore.Qt.ItemIsSelectable)
return item
def get_new_pose_name(self):
poses = set([self.ui.poseList.item(x).text()
for x in range(self.ui.poseList.count())])
count = 0
while True:
name = 'new_pose_%d' % count
if name not in poses:
return name
count += 1
def generate_pose_data(self):
return dict(
[ (i, control['doublespin'].value())
for i, control in enumerate(self.servo_controls) ])
def handle_add_pose(self):
pose_name = self.get_new_pose_name()
item = self.add_list_pose(pose_name)
item.setData(QtCore.Qt.UserRole, self.generate_pose_data())
self.ui.poseList.editItem(item)
def handle_remove_pose(self):
if self.ui.poseList.currentRow() < 0:
return
pose_name = self.ui.poseList.currentItem().text()
del self.poses[pose_name]
self.ui.poseList.takeItem(self.ui.poseList.currentRow())
@asyncio.coroutine
def handle_move_to_pose(self):
if self.ui.poseList.currentRow() < 0:
return
values = self.ui.poseList.currentItem().data(QtCore.Qt.UserRole)
yield From(self.controller.set_pose(values, pose_time=1.0))
with self.servo_update:
for ident, angle_deg in values.iteritems():
control = self.servo_controls[ident]
control['slider'].setSliderPosition(int(angle_deg))
control['doublespin'].setValue(angle_deg)
def handle_update_pose(self):
if self.ui.poseList.currentRow() < 0:
return
self.ui.poseList.currentItem().setData(
QtCore.Qt.UserRole, self.generate_pose_data())
self.handle_poselist_current_changed(None, None)
def handle_poselist_current_changed(self, current, previous):
if self.ui.poseList.currentRow() < 0:
return
data = self.ui.poseList.currentItem().data(QtCore.Qt.UserRole)
for i, control in enumerate(self.servo_controls):
control['current'].setText('%.1f' % data[i])
def read_settings(self, config):
if not config.has_section('servo'):
return
self.ui.typeCombo.setCurrentIndex(config.getint('servo', 'type'))
self.ui.serialPortCombo.setEditText(config.get('servo', 'port'))
self.ui.servoCountSpin.setValue(config.getint('servo', 'count'))
self.servo_model = config.get('servo', 'model')
if config.has_section('servo.names'):
self.servo_name_map = {}
for name, value in config.items('servo.names'):
self.servo_name_map[int(name)] = value
if config.has_section('servo.poses'):
for name, value in config.items('servo.poses'):
this_data = {}
for element in value.split(','):
ident, angle_deg = element.split('=')
this_data[int(ident)] = float(angle_deg)
item = self.add_list_pose(name)
item.setData(QtCore.Qt.UserRole, this_data)
def write_settings(self, config):
config.add_section('servo')
config.add_section('servo.poses')
config.add_section('servo.names')
config.set('servo', 'type', self.ui.typeCombo.currentIndex())
config.set('servo', 'port', self.ui.serialPortCombo.currentText())
config.set('servo', 'count', self.ui.servoCountSpin.value())
config.set('servo', 'model', self.servo_model)
for key, value in self.servo_name_map.iteritems():
config.set('servo.names', str(key), value)
for row in range(self.ui.poseList.count()):
item = self.ui.poseList.item(row)
pose_name = item.text()
values = item.data(QtCore.Qt.UserRole)
config.set(
'servo.poses', pose_name,
','.join(['%d=%.2f' % (ident, angle_deg)
for ident, angle_deg in values.iteritems()]))
| [] |
twisted/epsilon | epsilon/juice.py | 783910e1829688e95719a7d3151ec3e2cbb101fd | # -*- test-case-name: epsilon.test.test_juice -*-
# Copyright 2005 Divmod, Inc. See LICENSE file for details
import warnings, pprint
import keyword
import io
import six
from twisted.internet.main import CONNECTION_LOST
from twisted.internet.defer import Deferred, maybeDeferred, fail
from twisted.internet.protocol import ServerFactory, ClientFactory
from twisted.internet.ssl import Certificate
from twisted.python.failure import Failure
from twisted.python import log, filepath
from epsilon.liner import LineReceiver
from epsilon.compat import long
from epsilon import extime
ASK = '_ask'
ANSWER = '_answer'
COMMAND = '_command'
ERROR = '_error'
ERROR_CODE = '_error_code'
ERROR_DESCRIPTION = '_error_description'
LENGTH = '_length'
BODY = 'body'
debug = False
class JuiceBox(dict):
""" I am a packet in the JUICE protocol. """
def __init__(self, __body='', **kw):
self.update(kw)
if __body:
assert isinstance(__body, str), "body must be a string: %r" % ( repr(__body),)
self['body'] = __body
def body():
def get(self):
warnings.warn("body attribute of boxes is now just a regular field",
stacklevel=2)
return self['body']
def set(self, newbody):
warnings.warn("body attribute of boxes is now just a regular field",
stacklevel=2)
self['body'] = newbody
return get,set
body = property(*body())
def copy(self):
newBox = self.__class__()
newBox.update(self)
return newBox
def serialize(self,
delimiter=b'\r\n',
escaped=b'\r\n '):
assert LENGTH not in self
delimiter = six.ensure_binary(delimiter)
escaped = six.ensure_binary(escaped)
L = []
for (k, v) in six.viewitems(self):
if k == BODY:
k = LENGTH
v = str(len(self[BODY]))
L.append(six.ensure_binary(k).replace(b'_', b'-').title())
L.append(b': ')
L.append(six.ensure_binary(v).replace(delimiter, escaped))
L.append(delimiter)
L.append(delimiter)
if BODY in self:
L.append(six.ensure_binary(self[BODY]))
return b''.join(L)
def sendTo(self, proto):
"""
Serialize and send this box to a Juice instance. By the time it is
being sent, several keys are required. I must have exactly ONE of::
-ask
-answer
-error
If the '-ask' header is set, then the '-command' header must also be
set.
"""
proto.sendPacket(self)
# juice.Box => JuiceBox
Box = JuiceBox
class TLSBox(JuiceBox):
def __repr__(self):
return 'TLS(**%s)' % (super(TLSBox, self).__repr__(),)
def __init__(self, __certificate, __verify=None, __sslstarted=None, **kw):
super(TLSBox, self).__init__(**kw)
self.certificate = __certificate
self.verify = __verify
self.sslstarted = __sslstarted
def sendTo(self, proto):
super(TLSBox, self).sendTo(proto)
if self.verify is None:
proto.startTLS(self.certificate)
else:
proto.startTLS(self.certificate, self.verify)
if self.sslstarted is not None:
self.sslstarted()
class QuitBox(JuiceBox):
def __repr__(self):
return 'Quit(**%s)' % (super(QuitBox, self).__repr__(),)
def sendTo(self, proto):
super(QuitBox, self).sendTo(proto)
proto.transport.loseConnection()
class _SwitchBox(JuiceBox):
def __repr__(self):
return 'Switch(**%s)' % (super(_SwitchBox, self).__repr__(),)
def __init__(self, __proto, **kw):
super(_SwitchBox, self).__init__(**kw)
self.innerProto = __proto
def sendTo(self, proto):
super(_SwitchBox, self).sendTo(proto)
proto._switchTo(self.innerProto)
class NegotiateBox(JuiceBox):
def __repr__(self):
return 'Negotiate(**%s)' % (super(NegotiateBox, self).__repr__(),)
def sendTo(self, proto):
super(NegotiateBox, self).sendTo(proto)
proto._setProtocolVersion(int(self['version']))
class JuiceError(Exception):
pass
class RemoteJuiceError(JuiceError):
"""
This error indicates that something went wrong on the remote end of the
connection, and the error was serialized and transmitted to you.
"""
def __init__(self, errorCode, description, fatal=False):
"""Create a remote error with an error code and description.
"""
Exception.__init__(self, "Remote[%s]: %s" % (errorCode, description))
self.errorCode = errorCode
self.description = description
self.fatal = fatal
class UnhandledRemoteJuiceError(RemoteJuiceError):
def __init__(self, description):
errorCode = b"UNHANDLED"
RemoteJuiceError.__init__(self, errorCode, description)
class JuiceBoxError(JuiceError):
pass
class MalformedJuiceBox(JuiceBoxError):
pass
class UnhandledCommand(JuiceError):
pass
class IncompatibleVersions(JuiceError):
pass
class _Transactor:
def __init__(self, store, callable):
self.store = store
self.callable = callable
def __call__(self, box):
return self.store.transact(self.callable, box)
def __repr__(self):
return '<Transaction in: %s of: %s>' % (self.store, self.callable)
class DispatchMixin:
baseDispatchPrefix = 'juice_'
autoDispatchPrefix = 'command_'
wrapper = None
def _auto(self, aCallable, proto, namespace=None):
if aCallable is None:
return None
command = aCallable.command
if namespace not in command.namespaces:
# if you're in the wrong namespace, you are very likely not allowed
# to invoke the command you are trying to invoke. some objects
# have commands exposed in a separate namespace for security
# reasons, since the security model is a role : namespace mapping.
log.msg('WRONG NAMESPACE: %r, %r' % (namespace, command.namespaces))
return None
def doit(box):
kw = stringsToObjects(box, command.arguments, proto)
for name, extraArg in command.extra:
kw[name] = extraArg.fromTransport(proto.transport)
# def checkIsDict(result):
# if not isinstance(result, dict):
# raise RuntimeError("%r returned %r, not dictionary" % (
# aCallable, result))
# return result
def checkKnownErrors(error):
key = error.trap(*command.allErrors)
code = command.allErrors[key]
desc = str(error.value)
return Failure(RemoteJuiceError(
code, desc, error in command.fatalErrors))
return maybeDeferred(aCallable, **kw).addCallback(
command.makeResponse, proto).addErrback(
checkKnownErrors)
return doit
def _wrap(self, aCallable):
if aCallable is None:
return None
wrap = self.wrapper
if wrap is not None:
return wrap(aCallable)
else:
return aCallable
def normalizeCommand(self, cmd):
"""Return the canonical form of a command.
"""
return cmd.upper().strip().replace('-', '_')
def lookupFunction(self, proto, name, namespace):
"""Return a callable to invoke when executing the named command.
"""
# Try to find a method to be invoked in a transaction first
# Otherwise fallback to a "regular" method
fName = self.autoDispatchPrefix + name
fObj = getattr(self, fName, None)
if fObj is not None:
# pass the namespace along
return self._auto(fObj, proto, namespace)
assert namespace is None, 'Old-style parsing'
# Fall back to simplistic command dispatching - we probably want to get
# rid of this eventually, there's no reason to do extra work and write
# fewer docs all the time.
fName = self.baseDispatchPrefix + name
return getattr(self, fName, None)
def dispatchCommand(self, proto, cmd, box, namespace=None):
fObj = self.lookupFunction(proto, self.normalizeCommand(cmd), namespace)
if fObj is None:
return fail(UnhandledCommand(cmd))
return maybeDeferred(self._wrap(fObj), box)
def normalizeKey(key):
lkey = six.ensure_str(key).lower().replace('-', '_')
if keyword.iskeyword(lkey):
return lkey.title()
return lkey
def parseJuiceHeaders(lines):
"""
Create a JuiceBox from a list of header lines.
@param lines: a list of lines.
@type lines: a list of L{bytes}
"""
b = JuiceBox()
key = None
for L in lines:
if L[0:1] == b' ':
# continuation
assert key is not None
b[key] += six.ensure_str(b'\r\n' + L[1:])
continue
parts = L.split(b': ', 1)
if len(parts) != 2:
raise MalformedJuiceBox("Wrong number of parts: %r" % (L,))
key, value = parts
key = normalizeKey(key)
b[key] = six.ensure_str(value)
return int(b.pop(LENGTH, 0)), b
class JuiceParserBase(DispatchMixin):
def __init__(self):
self._outstandingRequests = {}
def _puke(self, failure):
log.msg("Juice server or network failure "
"unhandled by client application:")
log.err(failure)
log.msg(
"Dropping connection! "
"To avoid, add errbacks to ALL remote commands!")
if self.transport is not None:
self.transport.loseConnection()
_counter = long(0)
def _nextTag(self):
self._counter += 1
return '%x' % (self._counter,)
def failAllOutgoing(self, reason):
OR = self._outstandingRequests.items()
self._outstandingRequests = None # we can never send another request
for key, value in OR:
value.errback(reason)
def juiceBoxReceived(self, box):
if debug:
log.msg("Juice receive: %s" % pprint.pformat(dict(six.viewitems(box))))
if ANSWER in box:
question = self._outstandingRequests.pop(box[ANSWER])
question.addErrback(self._puke)
self._wrap(question.callback)(box)
elif ERROR in box:
question = self._outstandingRequests.pop(box[ERROR])
question.addErrback(self._puke)
self._wrap(question.errback)(
Failure(RemoteJuiceError(box[ERROR_CODE],
box[ERROR_DESCRIPTION])))
elif COMMAND in box:
cmd = box[COMMAND]
def sendAnswer(answerBox):
if ASK not in box:
return
if self.transport is None:
return
answerBox[ANSWER] = box[ASK]
answerBox.sendTo(self)
def sendError(error):
if ASK not in box:
return error
if error.check(RemoteJuiceError):
code = error.value.errorCode
desc = error.value.description
if error.value.fatal:
errorBox = QuitBox()
else:
errorBox = JuiceBox()
else:
errorBox = QuitBox()
log.err(error) # here is where server-side logging happens
# if the error isn't handled
code = 'UNHANDLED'
desc = "Unhandled Remote System Exception "
errorBox[ERROR] = box[ASK]
errorBox[ERROR_DESCRIPTION] = desc
errorBox[ERROR_CODE] = code
if self.transport is not None:
errorBox.sendTo(self)
return None # intentionally stop the error here: don't log the
# traceback if it's handled, do log it (earlier) if
# it isn't
self.dispatchCommand(self, cmd, box).addCallbacks(sendAnswer, sendError
).addErrback(self._puke)
else:
raise RuntimeError(
"Empty packet received over connection-oriented juice: %r" % (box,))
def sendBoxCommand(self, command, box, requiresAnswer=True):
"""
Send a command across the wire with the given C{juice.Box}.
Returns a Deferred which fires with the response C{juice.Box} when it
is received, or fails with a C{juice.RemoteJuiceError} if an error is
received.
If the Deferred fails and the error is not handled by the caller of
this method, the failure will be logged and the connection dropped.
"""
if self._outstandingRequests is None:
return fail(CONNECTION_LOST)
box[COMMAND] = command
tag = self._nextTag()
if requiresAnswer:
box[ASK] = tag
result = self._outstandingRequests[tag] = Deferred()
else:
result = None
box.sendTo(self)
return result
class Argument:
optional = False
def __init__(self, optional=False):
self.optional = optional
def retrieve(self, d, name):
if self.optional:
value = d.get(name)
if value is not None:
del d[name]
else:
value = d.pop(name)
return value
def fromBox(self, name, strings, objects, proto):
st = self.retrieve(strings, name)
if self.optional and st is None:
objects[name] = None
else:
objects[name] = self.fromStringProto(st, proto)
def toBox(self, name, strings, objects, proto):
obj = self.retrieve(objects, name)
if self.optional and obj is None:
# strings[name] = None
return
else:
strings[name] = self.toStringProto(obj, proto)
def fromStringProto(self, inString, proto):
return self.fromString(inString)
def toStringProto(self, inObject, proto):
return self.toString(inObject)
def fromString(self, inString):
raise NotImplementedError()
def toString(self, inObject):
raise NotImplementedError()
class JuiceList(Argument):
def __init__(self, subargs):
self.subargs = subargs
def fromStringProto(self, inString, proto):
boxes = parseString(six.ensure_binary(inString))
values = [stringsToObjects(box, self.subargs, proto)
for box in boxes]
return values
def toStringProto(self, inObject, proto):
return b''.join([
objectsToStrings(objects, self.subargs, Box(), proto).serialize()
for objects in inObject
])
class ListOf(Argument):
def __init__(self, subarg, delimiter=', '):
self.subarg = subarg
self.delimiter = delimiter
def fromStringProto(self, inString, proto):
strings = inString.split(self.delimiter)
L = [self.subarg.fromStringProto(string, proto)
for string in strings]
return L
def toStringProto(self, inObject, proto):
L = []
for inSingle in inObject:
outString = self.subarg.toStringProto(inSingle, proto)
assert self.delimiter not in outString
L.append(outString)
return self.delimiter.join(L)
class Integer(Argument):
fromString = int
def toString(self, inObject):
return str(int(inObject))
class String(Argument):
def toString(self, inObject):
return inObject
def fromString(self, inString):
return inString
class EncodedString(Argument):
def __init__(self, encoding):
self.encoding = encoding
def toString(self, inObject):
return inObject.encode(self.encoding)
def fromString(self, inString):
return inString.decode(self.encoding)
# Temporary backwards compatibility for Exponent
Body = String
class Unicode(String):
def toString(self, inObject):
# assert isinstance(inObject, unicode)
return String.toString(self, inObject.encode('utf-8'))
def fromString(self, inString):
# assert isinstance(inString, str)
return String.fromString(self, inString).decode('utf-8')
class Path(Unicode):
def fromString(self, inString):
return filepath.FilePath(Unicode.fromString(self, inString))
def toString(self, inObject):
return Unicode.toString(self, inObject.path)
class Float(Argument):
fromString = float
toString = str
class Base64Binary(Argument):
def toString(self, inObject):
return inObject.encode('base64').replace('\n', '')
def fromString(self, inString):
return inString.decode('base64')
class Time(Argument):
def toString(self, inObject):
return inObject.asISO8601TimeAndDate()
def fromString(self, inString):
return extime.Time.fromISO8601TimeAndDate(inString)
class ExtraArg:
def fromTransport(self, inTransport):
raise NotImplementedError()
class Peer(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QPeer()
class PeerDomain(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QPeer().domain
class PeerUser(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QPeer().resource
class Host(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QHost()
class HostDomain(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QHost().domain
class HostUser(ExtraArg):
def fromTransport(self, inTransport):
return inTransport.getQ2QHost().resource
class Boolean(Argument):
def fromString(self, inString):
if inString == 'True':
return True
elif inString == 'False':
return False
else:
raise RuntimeError("Bad boolean value: %r" % (inString,))
def toString(self, inObject):
if inObject:
return 'True'
else:
return 'False'
class _CommandMeta(type):
def __new__(cls, name, bases, attrs):
re = attrs['reverseErrors'] = {}
er = attrs['allErrors'] = {}
for v, k in six.viewitems(attrs.get('errors',{})):
re[k] = v
er[v] = k
for v, k in six.viewitems(attrs.get('fatalErrors',{})):
re[k] = v
er[v] = k
return type.__new__(cls, name, bases, attrs)
@six.add_metaclass(_CommandMeta)
class Command:
arguments = []
response = []
extra = []
namespaces = [None] # This is set to [None] on purpose: None means
# "no namespace", not "empty list". "empty
# list" will make your command invalid in _all_
# namespaces, effectively uncallable.
errors = {}
fatalErrors = {}
commandType = Box
responseType = Box
def commandName():
def get(self):
return self.__class__.__name__
raise NotImplementedError("Missing command name")
return get,
commandName = property(*commandName())
def __init__(self, **kw):
self.structured = kw
givenArgs = [normalizeKey(k) for k in kw.keys()]
forgotten = []
for name, arg in self.arguments:
if normalizeKey(name) not in givenArgs and not arg.optional:
forgotten.append(normalizeKey(name))
# for v in kw.itervalues():
# if v is None:
# from pprint import pformat
# raise RuntimeError("ARGH: %s" % pformat(kw))
if forgotten:
if len(forgotten) == 1:
plural = 'an argument'
else:
plural = 'some arguments'
raise RuntimeError("You forgot %s to %r: %s" % (
plural, self.commandName, ', '.join(forgotten)))
forgotten = []
def makeResponse(cls, objects, proto):
try:
return objectsToStrings(objects, cls.response, cls.responseType(), proto)
except:
log.msg("Exception in %r.makeResponse" % (cls,))
raise
makeResponse = classmethod(makeResponse)
def do(self, proto, namespace=None, requiresAnswer=True):
if namespace is not None:
cmd = namespace + ":" + self.commandName
else:
cmd = self.commandName
def _massageError(error):
error.trap(RemoteJuiceError)
rje = error.value
return Failure(self.reverseErrors.get(rje.errorCode, UnhandledRemoteJuiceError)(rje.description))
d = proto.sendBoxCommand(
cmd, objectsToStrings(self.structured, self.arguments, self.commandType(),
proto),
requiresAnswer)
if requiresAnswer:
d.addCallback(stringsToObjects, self.response, proto)
d.addCallback(self.addExtra, proto.transport)
d.addErrback(_massageError)
return d
def addExtra(self, d, transport):
for name, extraArg in self.extra:
d[name] = extraArg.fromTransport(transport)
return d
class ProtocolSwitchCommand(Command):
"""Use this command to switch from something Juice-derived to a different
protocol mid-connection. This can be useful to use juice as the
connection-startup negotiation phase. Since TLS is a different layer
entirely, you can use Juice to negotiate the security parameters of your
connection, then switch to a different protocol, and the connection will
remain secured.
"""
def __init__(self, __protoToSwitchToFactory, **kw):
self.protoToSwitchToFactory = __protoToSwitchToFactory
super(ProtocolSwitchCommand, self).__init__(**kw)
def makeResponse(cls, innerProto, proto):
return _SwitchBox(innerProto)
makeResponse = classmethod(makeResponse)
def do(self, proto, namespace=None):
d = super(ProtocolSwitchCommand, self).do(proto)
proto._lock()
def switchNow(ign):
innerProto = self.protoToSwitchToFactory.buildProtocol(proto.transport.getPeer())
proto._switchTo(innerProto, self.protoToSwitchToFactory)
return ign
def die(ign):
proto.transport.loseConnection()
return ign
def handle(ign):
self.protoToSwitchToFactory.clientConnectionFailed(None, Failure(CONNECTION_LOST))
return ign
return d.addCallbacks(switchNow, handle).addErrback(die)
class Negotiate(Command):
commandName = 'Negotiate'
arguments = [('versions', ListOf(Integer()))]
response = [('version', Integer())]
responseType = NegotiateBox
class Juice(LineReceiver, JuiceParserBase, object):
"""
JUICE (JUice Is Concurrent Events) is a simple connection-oriented
request/response protocol. Packets, or "boxes", are collections of
RFC2822-inspired headers, plus a body. Note that this is NOT a literal
interpretation of any existing RFC, 822, 2822 or otherwise, but a simpler
version that does not do line continuations, does not specify any
particular format for header values, dispatches semantic meanings of most
headers on the -Command header rather than giving them global meaning, and
allows multiple sets of headers (messages, or JuiceBoxes) on a connection.
All headers whose names begin with a dash ('-') are reserved for use by the
protocol. All others are for application use - their meaning depends on
the value of the "-Command" header.
"""
protocolName = b'juice-base'
hostCertificate = None
MAX_LENGTH = 1024 * 1024
isServer = property(lambda self: self._issueGreeting,
doc="""
True if this is a juice server, e.g. it is going to
issue or has issued a server greeting upon
connection.
""")
isClient = property(lambda self: not self._issueGreeting,
doc="""
True if this is a juice server, e.g. it is not going to
issue or did not issue a server greeting upon
connection.
""")
def __init__(self, issueGreeting):
"""
@param issueGreeting: whether to issue a greeting when connected. This
should be set on server-side Juice protocols.
"""
JuiceParserBase.__init__(self)
self._issueGreeting = issueGreeting
def __repr__(self):
return '<%s %s/%s at 0x%x>' % (self.__class__.__name__, self.isClient and 'client' or 'server', self.innerProtocol, id(self))
__locked = False
def _lock(self):
""" Lock this Juice instance so that no further Juice traffic may be sent.
This is used when sending a request to switch underlying protocols.
You probably want to subclass ProtocolSwitchCommand rather than calling
this directly.
"""
self.__locked = True
innerProtocol = None
def _switchTo(self, newProto, clientFactory=None):
""" Switch this Juice instance to a new protocol. You need to do this
'simultaneously' on both ends of a connection; the easiest way to do
this is to use a subclass of ProtocolSwitchCommand.
"""
assert self.innerProtocol is None, "Protocol can only be safely switched once."
self.setRawMode()
self.innerProtocol = newProto
self.innerProtocolClientFactory = clientFactory
newProto.makeConnection(self.transport)
innerProtocolClientFactory = None
def juiceBoxReceived(self, box):
if self.__locked and COMMAND in box and ASK in box:
# This is a command which will trigger an answer, and we can no
# longer answer anything, so don't bother delivering it.
return
return super(Juice, self).juiceBoxReceived(box)
def sendPacket(self, completeBox):
"""
Send a juice.Box to my peer.
Note: transport.write is never called outside of this method.
"""
assert not self.__locked, "You cannot send juice packets when a connection is locked"
if self._startingTLSBuffer is not None:
self._startingTLSBuffer.append(completeBox)
else:
if debug:
log.msg("Juice send: %s" % pprint.pformat(dict(six.viewitems(completeBox))))
result = completeBox.serialize()
self.transport.write(result)
def sendCommand(self, command, __content='', __answer=True, **kw):
box = JuiceBox(__content, **kw)
return self.sendBoxCommand(command, box, requiresAnswer=__answer)
_outstandingRequests = None
_justStartedTLS = False
def makeConnection(self, transport):
self._transportPeer = transport.getPeer()
self._transportHost = transport.getHost()
log.msg("%s %s connection established (HOST:%s PEER:%s)" % (self.isClient and "client" or "server",
self.__class__.__name__,
self._transportHost,
self._transportPeer))
self._outstandingRequests = {}
self._requestBuffer = []
LineReceiver.makeConnection(self, transport)
_startingTLSBuffer = None
def prepareTLS(self):
self._startingTLSBuffer = []
def startTLS(self, certificate, *verifyAuthorities):
if self.hostCertificate is None:
self.hostCertificate = certificate
self._justStartedTLS = True
self.transport.startTLS(certificate.options(*verifyAuthorities))
stlsb = self._startingTLSBuffer
if stlsb is not None:
self._startingTLSBuffer = None
for box in stlsb:
self.sendPacket(box)
else:
raise RuntimeError(
"Previously authenticated connection between %s and %s "
"is trying to re-establish as %s" % (
self.hostCertificate,
Certificate.peerFromTransport(self.transport),
(certificate, verifyAuthorities)))
def dataReceived(self, data):
# If we successfully receive any data after TLS has been started, that
# means the connection was secured properly. Make a note of that fact.
if self._justStartedTLS:
self._justStartedTLS = False
return LineReceiver.dataReceived(self, data)
def connectionLost(self, reason):
log.msg("%s %s connection lost (HOST:%s PEER:%s)" % (
self.isClient and 'client' or 'server',
self.__class__.__name__,
self._transportHost,
self._transportPeer))
self.failAllOutgoing(reason)
if self.innerProtocol is not None:
self.innerProtocol.connectionLost(reason)
if self.innerProtocolClientFactory is not None:
self.innerProtocolClientFactory.clientConnectionLost(None, reason)
def lineReceived(self, line):
if line:
self._requestBuffer.append(line)
else:
buf = self._requestBuffer
self._requestBuffer = []
bodylen, b = parseJuiceHeaders(buf)
if bodylen:
self._bodyRemaining = bodylen
self._bodyBuffer = []
self._pendingBox = b
self.setRawMode()
else:
self.juiceBoxReceived(b)
def rawDataReceived(self, data):
if self.innerProtocol is not None:
self.innerProtocol.dataReceived(data)
return
self._bodyRemaining -= len(data)
if self._bodyRemaining <= 0:
if self._bodyRemaining < 0:
self._bodyBuffer.append(data[:self._bodyRemaining])
extraData = data[self._bodyRemaining:]
else:
self._bodyBuffer.append(data)
extraData = ''
self._pendingBox['body'] = six.ensure_str(b''.join(six.ensure_binary(each) for each in self._bodyBuffer))
self._bodyBuffer = None
b, self._pendingBox = self._pendingBox, None
self.juiceBoxReceived(b)
if self.innerProtocol is not None:
self.innerProtocol.makeConnection(self.transport)
if extraData:
self.innerProtocol.dataReceived(extraData)
else:
self.setLineMode(extraData)
else:
self._bodyBuffer.append(data)
protocolVersion = 0
def _setProtocolVersion(self, version):
# if we ever want to actually mangle encodings, this is the place to do
# it!
self.protocolVersion = version
return version
def renegotiateVersion(self, newVersion):
assert newVersion in VERSIONS, (
"This side of the connection doesn't support version %r"
% (newVersion,))
v = VERSIONS[:]
v.remove(newVersion)
return Negotiate(versions=[newVersion]).do(self).addCallback(
lambda ver: self._setProtocolVersion(ver['version']))
def command_NEGOTIATE(self, versions):
for version in versions:
if version in VERSIONS:
return dict(version=version)
raise IncompatibleVersions()
command_NEGOTIATE.command = Negotiate
VERSIONS = [1]
class _ParserHelper(Juice):
def __init__(self):
Juice.__init__(self, False)
self.boxes = []
self.results = Deferred()
def getPeer(self):
return 'string'
def getHost(self):
return 'string'
disconnecting = False
def juiceBoxReceived(self, box):
self.boxes.append(box)
# Synchronous helpers
def parse(cls, fileObj):
p = cls()
p.makeConnection(p)
p.dataReceived(fileObj.read())
return p.boxes
parse = classmethod(parse)
def parseString(cls, data):
with io.BytesIO(data) as f:
return cls.parse(f)
parseString = classmethod(parseString)
parse = _ParserHelper.parse
parseString = _ParserHelper.parseString
def stringsToObjects(strings, arglist, proto):
objects = {}
myStrings = strings.copy()
for argname, argparser in arglist:
argparser.fromBox(argname, myStrings, objects, proto)
return objects
def objectsToStrings(objects, arglist, strings, proto):
myObjects = {}
for (k, v) in objects.items():
myObjects[normalizeKey(k)] = v
for argname, argparser in arglist:
argparser.toBox(argname, strings, myObjects, proto)
return strings
class JuiceServerFactory(ServerFactory):
protocol = Juice
def buildProtocol(self, addr):
prot = self.protocol(True)
prot.factory = self
return prot
class JuiceClientFactory(ClientFactory):
protocol = Juice
def buildProtocol(self, addr):
prot = self.protocol(False)
prot.factory = self
return prot
| [((18926, 18957), 'six.add_metaclass', 'six.add_metaclass', (['_CommandMeta'], {}), '(_CommandMeta)\n', (18943, 18957), False, 'import six\n'), ((8656, 8679), 'keyword.iskeyword', 'keyword.iskeyword', (['lkey'], {}), '(lkey)\n', (8673, 8679), False, 'import keyword\n'), ((9859, 9866), 'epsilon.compat.long', 'long', (['(0)'], {}), '(0)\n', (9863, 9866), False, 'from epsilon.compat import long\n'), ((1743, 1771), 'six.ensure_binary', 'six.ensure_binary', (['delimiter'], {}), '(delimiter)\n', (1760, 1771), False, 'import six\n'), ((1790, 1816), 'six.ensure_binary', 'six.ensure_binary', (['escaped'], {}), '(escaped)\n', (1807, 1816), False, 'import six\n'), ((1855, 1874), 'six.viewitems', 'six.viewitems', (['self'], {}), '(self)\n', (1868, 1874), False, 'import six\n'), ((9324, 9345), 'six.ensure_str', 'six.ensure_str', (['value'], {}), '(value)\n', (9338, 9345), False, 'import six\n'), ((9524, 9599), 'twisted.python.log.msg', 'log.msg', (['"""Juice server or network failure unhandled by client application:"""'], {}), "('Juice server or network failure unhandled by client application:')\n", (9531, 9599), False, 'from twisted.python import log, filepath\n'), ((9627, 9643), 'twisted.python.log.err', 'log.err', (['failure'], {}), '(failure)\n', (9634, 9643), False, 'from twisted.python import log, filepath\n'), ((9652, 9731), 'twisted.python.log.msg', 'log.msg', (['"""Dropping connection! To avoid, add errbacks to ALL remote commands!"""'], {}), "('Dropping connection! To avoid, add errbacks to ALL remote commands!')\n", (9659, 9731), False, 'from twisted.python import log, filepath\n'), ((17264, 17308), 'epsilon.extime.Time.fromISO8601TimeAndDate', 'extime.Time.fromISO8601TimeAndDate', (['inString'], {}), '(inString)\n', (17298, 17308), False, 'from epsilon import extime\n'), ((27501, 27676), 'twisted.python.log.msg', 'log.msg', (["('%s %s connection established (HOST:%s PEER:%s)' % (self.isClient and\n 'client' or 'server', self.__class__.__name__, self._transportHost,\n self._transportPeer))"], {}), "('%s %s connection established (HOST:%s PEER:%s)' % (self.isClient and\n 'client' or 'server', self.__class__.__name__, self._transportHost,\n self._transportPeer))\n", (27508, 27676), False, 'from twisted.python import log, filepath\n'), ((27953, 27997), 'epsilon.liner.LineReceiver.makeConnection', 'LineReceiver.makeConnection', (['self', 'transport'], {}), '(self, transport)\n', (27980, 27997), False, 'from epsilon.liner import LineReceiver\n'), ((29176, 29213), 'epsilon.liner.LineReceiver.dataReceived', 'LineReceiver.dataReceived', (['self', 'data'], {}), '(self, data)\n', (29201, 29213), False, 'from epsilon.liner import LineReceiver\n'), ((29261, 29429), 'twisted.python.log.msg', 'log.msg', (["('%s %s connection lost (HOST:%s PEER:%s)' % (self.isClient and 'client' or\n 'server', self.__class__.__name__, self._transportHost, self.\n _transportPeer))"], {}), "('%s %s connection lost (HOST:%s PEER:%s)' % (self.isClient and\n 'client' or 'server', self.__class__.__name__, self._transportHost,\n self._transportPeer))\n", (29268, 29429), False, 'from twisted.python import log, filepath\n'), ((32285, 32295), 'twisted.internet.defer.Deferred', 'Deferred', ([], {}), '()\n', (32293, 32295), False, 'from twisted.internet.defer import Deferred, maybeDeferred, fail\n'), ((1104, 1190), 'warnings.warn', 'warnings.warn', (['"""body attribute of boxes is now just a regular field"""'], {'stacklevel': '(2)'}), "('body attribute of boxes is now just a regular field',\n stacklevel=2)\n", (1117, 1190), False, 'import warnings, pprint\n'), ((1289, 1375), 'warnings.warn', 'warnings.warn', (['"""body attribute of boxes is now just a regular field"""'], {'stacklevel': '(2)'}), "('body attribute of boxes is now just a regular field',\n stacklevel=2)\n", (1302, 1375), False, 'import warnings, pprint\n'), ((6097, 6165), 'twisted.python.log.msg', 'log.msg', (["('WRONG NAMESPACE: %r, %r' % (namespace, command.namespaces))"], {}), "('WRONG NAMESPACE: %r, %r' % (namespace, command.namespaces))\n", (6104, 6165), False, 'from twisted.python import log, filepath\n'), ((9061, 9092), 'six.ensure_str', 'six.ensure_str', (["(b'\\r\\n' + L[1:])"], {}), "(b'\\r\\n' + L[1:])\n", (9075, 9092), False, 'import six\n'), ((13126, 13147), 'twisted.internet.defer.fail', 'fail', (['CONNECTION_LOST'], {}), '(CONNECTION_LOST)\n', (13130, 13147), False, 'from twisted.internet.defer import Deferred, maybeDeferred, fail\n'), ((13317, 13327), 'twisted.internet.defer.Deferred', 'Deferred', ([], {}), '()\n', (13325, 13327), False, 'from twisted.internet.defer import Deferred, maybeDeferred, fail\n'), ((14752, 14779), 'six.ensure_binary', 'six.ensure_binary', (['inString'], {}), '(inString)\n', (14769, 14779), False, 'import six\n'), ((32729, 32745), 'io.BytesIO', 'io.BytesIO', (['data'], {}), '(data)\n', (32739, 32745), False, 'import io\n'), ((2247, 2276), 'six.ensure_binary', 'six.ensure_binary', (['self[BODY]'], {}), '(self[BODY])\n', (2264, 2276), False, 'import six\n'), ((20588, 20636), 'twisted.python.log.msg', 'log.msg', (["('Exception in %r.makeResponse' % (cls,))"], {}), "('Exception in %r.makeResponse' % (cls,))\n", (20595, 20636), False, 'from twisted.python import log, filepath\n'), ((22941, 22965), 'twisted.python.failure.Failure', 'Failure', (['CONNECTION_LOST'], {}), '(CONNECTION_LOST)\n', (22948, 22965), False, 'from twisted.python.failure import Failure\n'), ((8603, 8622), 'six.ensure_str', 'six.ensure_str', (['key'], {}), '(key)\n', (8617, 8622), False, 'import six\n'), ((2090, 2110), 'six.ensure_binary', 'six.ensure_binary', (['v'], {}), '(v)\n', (2107, 2110), False, 'import six\n'), ((28791, 28836), 'twisted.internet.ssl.Certificate.peerFromTransport', 'Certificate.peerFromTransport', (['self.transport'], {}), '(self.transport)\n', (28820, 28836), False, 'from twisted.internet.ssl import Certificate\n'), ((30785, 30808), 'six.ensure_binary', 'six.ensure_binary', (['each'], {}), '(each)\n', (30802, 30808), False, 'import six\n'), ((6959, 6989), 'twisted.internet.defer.maybeDeferred', 'maybeDeferred', (['aCallable'], {}), '(aCallable, **kw)\n', (6972, 6989), False, 'from twisted.internet.defer import Deferred, maybeDeferred, fail\n'), ((10304, 10322), 'six.viewitems', 'six.viewitems', (['box'], {}), '(box)\n', (10317, 10322), False, 'import six\n'), ((11622, 11636), 'twisted.python.log.err', 'log.err', (['error'], {}), '(error)\n', (11629, 11636), False, 'from twisted.python import log, filepath\n'), ((1991, 2011), 'six.ensure_binary', 'six.ensure_binary', (['k'], {}), '(k)\n', (2008, 2011), False, 'import six\n'), ((26988, 27014), 'six.viewitems', 'six.viewitems', (['completeBox'], {}), '(completeBox)\n', (27001, 27014), False, 'import six\n')] |
kirillpol-ms/bonsai3-py | bonsai3/simulator_client.py | ede9c2c1d25d784d61b7cbf1438a257b5d592274 | """
Client for simulator requests
"""
__copyright__ = "Copyright 2020, Microsoft Corp."
# pyright: strict
from random import uniform
import time
from typing import Union
import jsons
import requests
from .exceptions import RetryTimeoutError, ServiceError
from .logger import Logger
from .simulator_protocol import (
ServiceConfig,
SimulatorEvent,
SimulatorEventRequest,
SimulatorInterface,
)
log = Logger()
_RETRYABLE_ERROR_CODES = {502, 503, 504}
_MAXIMUM_BACKOFF_SECONDS = 60
_BACKOFF_BASE_MULTIPLIER_MILLISECONDS = 50
class SimulatorClient:
def __init__(self, config: ServiceConfig):
self._config = config
self._retry_attempts = 0
self._retry_timeout = None
self._session = requests.session()
self._session.headers.update(
{"Authorization": config.access_key, "Content-type": "application/json"}
)
def register_simulator(self, interface: SimulatorInterface) -> SimulatorEvent:
return self._http_request(interface, self._config)
def get_next_event(self, event_request: SimulatorEventRequest) -> SimulatorEvent:
return self._http_request(event_request, self._config)
def unregister_simulator(self, session_id: str):
url = "{}/v2/workspaces/{}/simulatorSessions/{}".format(
self._config.server, self._config.workspace, session_id
)
log.debug("Sending unregister request to {}".format(url))
return self._session.delete(url, timeout=self._config.network_timeout_seconds)
def _http_request(
self,
payload: Union[SimulatorInterface, SimulatorEventRequest],
config: ServiceConfig,
) -> SimulatorEvent:
res = None
if self._retry_attempts >= 1:
self._handle_retry()
try:
# NOTE: we assert these for the user here to allow the config object to be partially initialized before use.
assert len(
config.access_key
), "Environment variable SIM_ACCESS_KEY is unset or access_key is empty."
assert len(
config.workspace
), "Environment variable SIM_WORKSPACE is unset or workspace is empty."
assert len(
config.server
), "Environment variable SIM_API_HOST is unset or server is empty."
# Register request
if isinstance(payload, SimulatorInterface):
reg_url = "{}/v2/workspaces/{}/simulatorSessions".format(
config.server, config.workspace
)
log.debug("Sending registration to {}".format(reg_url))
log.debug("Registration payload: {}".format(jsons.dumps(payload)))
res = self._session.post(
reg_url,
json=jsons.loads(payload.json),
headers={
"Authorization": config.access_key,
"Content-type": "application/json",
},
timeout=self._config.network_timeout_seconds,
)
log.debug("Response to registration received.")
# Get next event request
if isinstance(payload, SimulatorEventRequest):
log.network("Sending get next event request.")
res = self._session.post(
"{}/v2/workspaces/{}/simulatorSessions/{}/advance".format(
config.server, config.workspace, payload.sessionId
),
json=jsons.loads(jsons.dumps(payload)),
headers={
"Authorization": config.access_key,
"Content-type": "application/json",
},
timeout=self._config.network_timeout_seconds,
)
log.network("Response to get next event request received.")
except requests.exceptions.Timeout as err:
log.error(err)
self._retry_attempts += 1
return self._http_request(payload, config)
except requests.exceptions.RequestException as err:
if res is not None:
log.error(res.text)
log.error(err)
raise
if res is not None:
if res.status_code in _RETRYABLE_ERROR_CODES:
log.debug(
"Service returned {}, a retryable response error code."
" Retrying request.".format(res.status_code)
)
self._retry_attempts += 1
return self._http_request(payload, config)
# bail on error
if res.status_code != 200 and res.status_code != 201:
log.error(
"Received response with {} http status code. "
"Raising exception.".format(res.status_code)
)
if res.text:
log.error(res.text)
raise ServiceError(
"Unable to get next event for simulator, "
"received {} http status code".format(res.status_code)
)
# TODO estee: this needs validation
# SimulatorEvent
self._retry_attempts = 0
self._retry_timeout = None
return self._event_from_json(res.text)
raise RuntimeError(
"Usage error: Somehow http response ended up as none. "
"Check arguments to _http_request and ensure the payload "
"is either of type SimulatorInterface or SimulatorEventRequest"
)
def _event_from_json(self, json_text: str) -> SimulatorEvent:
"""Converts a json string into a SimulatorEvent."""
event_dict = jsons.loads(json_text)
log.debug("Event Response: {}".format(event_dict))
return SimulatorEvent(event_dict)
def _handle_retry(self):
log.network("handling retry.")
if (
self._retry_timeout and time.time() > self._retry_timeout
) or self._config.retry_timeout_seconds == 0:
raise RetryTimeoutError("Simulator Retry time exceeded.")
if self._config.retry_timeout_seconds > 0 and self._retry_timeout is None:
self._retry_timeout = time.time() + self._config.retry_timeout_seconds
log.info(
"Simulator will timeout in {} seconds if it is not able "
"to connect to the platform.".format(self._retry_timeout - time.time())
)
self._backoff()
log.network("retry handled.")
def _backoff(self):
"""
Implements Exponential backoff algorithm with full jitter
Check the following url for more information
https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
"""
power_of_two = 2 ** self._retry_attempts
max_sleep = min(
power_of_two * _BACKOFF_BASE_MULTIPLIER_MILLISECONDS / 1000.0,
_MAXIMUM_BACKOFF_SECONDS,
)
sleep = uniform(0, max_sleep)
log.debug(
"Retry attempt: {}, backing off for {} seconds".format(
self._retry_attempts, sleep
)
)
time.sleep(sleep)
| [((737, 755), 'requests.session', 'requests.session', ([], {}), '()\n', (753, 755), False, 'import requests\n'), ((5796, 5818), 'jsons.loads', 'jsons.loads', (['json_text'], {}), '(json_text)\n', (5807, 5818), False, 'import jsons\n'), ((7087, 7108), 'random.uniform', 'uniform', (['(0)', 'max_sleep'], {}), '(0, max_sleep)\n', (7094, 7108), False, 'from random import uniform\n'), ((7272, 7289), 'time.sleep', 'time.sleep', (['sleep'], {}), '(sleep)\n', (7282, 7289), False, 'import time\n'), ((6314, 6325), 'time.time', 'time.time', ([], {}), '()\n', (6323, 6325), False, 'import time\n'), ((6038, 6049), 'time.time', 'time.time', ([], {}), '()\n', (6047, 6049), False, 'import time\n'), ((2700, 2720), 'jsons.dumps', 'jsons.dumps', (['payload'], {}), '(payload)\n', (2711, 2720), False, 'import jsons\n'), ((2819, 2844), 'jsons.loads', 'jsons.loads', (['payload.json'], {}), '(payload.json)\n', (2830, 2844), False, 'import jsons\n'), ((6534, 6545), 'time.time', 'time.time', ([], {}), '()\n', (6543, 6545), False, 'import time\n'), ((3582, 3602), 'jsons.dumps', 'jsons.dumps', (['payload'], {}), '(payload)\n', (3593, 3602), False, 'import jsons\n')] |
gwxie/Synthesize-Distorted-Image-and-Its-Control-Points | perturbed_images_generation_multiProcess.py | ed6de3e05a7ee1f3aecf65fcbb87c11d2ede41e7 | '''
GuoWang xie
set up :2020-1-9
intergrate img and label into one file
-- fiducial1024_v1
'''
import argparse
import sys, os
import pickle
import random
import collections
import json
import numpy as np
import scipy.io as io
import scipy.misc as m
import matplotlib.pyplot as plt
import glob
import math
import time
import threading
import multiprocessing as mp
from multiprocessing import Pool
import re
import cv2
# sys.path.append('/lustre/home/gwxie/hope/project/dewarp/datasets/') # /lustre/home/gwxie/program/project/unwarp/perturbed_imgaes/GAN
import utils
def getDatasets(dir):
return os.listdir(dir)
class perturbed(utils.BasePerturbed):
def __init__(self, path, bg_path, save_path, save_suffix):
self.path = path
self.bg_path = bg_path
self.save_path = save_path
self.save_suffix = save_suffix
def save_img(self, m, n, fold_curve='fold', repeat_time=4, fiducial_points = 16, relativeShift_position='relativeShift_v2'):
origin_img = cv2.imread(self.path, flags=cv2.IMREAD_COLOR)
save_img_shape = [512*2, 480*2] # 320
# reduce_value = np.random.choice([2**4, 2**5, 2**6, 2**7, 2**8], p=[0.01, 0.1, 0.4, 0.39, 0.1])
reduce_value = np.random.choice([2*2, 4*2, 8*2, 16*2, 24*2, 32*2, 40*2, 48*2], p=[0.02, 0.18, 0.2, 0.3, 0.1, 0.1, 0.08, 0.02])
# reduce_value = np.random.choice([8*2, 16*2, 24*2, 32*2, 40*2, 48*2], p=[0.01, 0.02, 0.2, 0.4, 0.19, 0.18])
# reduce_value = np.random.choice([16, 24, 32, 40, 48, 64], p=[0.01, 0.1, 0.2, 0.4, 0.2, 0.09])
base_img_shrink = save_img_shape[0] - reduce_value
# enlarge_img_shrink = [1024, 768]
# enlarge_img_shrink = [896, 672] # 420
enlarge_img_shrink = [512*4, 480*4] # 420
# enlarge_img_shrink = [896*2, 768*2] # 420
# enlarge_img_shrink = [896, 768] # 420
# enlarge_img_shrink = [768, 576] # 420
# enlarge_img_shrink = [640, 480] # 420
''''''
im_lr = origin_img.shape[0]
im_ud = origin_img.shape[1]
reduce_value_v2 = np.random.choice([2*2, 4*2, 8*2, 16*2, 24*2, 28*2, 32*2, 48*2], p=[0.02, 0.18, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1])
# reduce_value_v2 = np.random.choice([16, 24, 28, 32, 48, 64], p=[0.01, 0.1, 0.2, 0.3, 0.25, 0.14])
if im_lr > im_ud:
im_ud = min(int(im_ud / im_lr * base_img_shrink), save_img_shape[1] - reduce_value_v2)
im_lr = save_img_shape[0] - reduce_value
else:
base_img_shrink = save_img_shape[1] - reduce_value
im_lr = min(int(im_lr / im_ud * base_img_shrink), save_img_shape[0] - reduce_value_v2)
im_ud = base_img_shrink
if round(im_lr / im_ud, 2) < 0.5 or round(im_ud / im_lr, 2) < 0.5:
repeat_time = min(repeat_time, 8)
edge_padding = 3
im_lr -= im_lr % (fiducial_points-1) - (2*edge_padding) # im_lr % (fiducial_points-1) - 1
im_ud -= im_ud % (fiducial_points-1) - (2*edge_padding) # im_ud % (fiducial_points-1) - 1
im_hight = np.linspace(edge_padding, im_lr - edge_padding, fiducial_points, dtype=np.int64)
im_wide = np.linspace(edge_padding, im_ud - edge_padding, fiducial_points, dtype=np.int64)
# im_lr -= im_lr % (fiducial_points-1) - (1+2*edge_padding) # im_lr % (fiducial_points-1) - 1
# im_ud -= im_ud % (fiducial_points-1) - (1+2*edge_padding) # im_ud % (fiducial_points-1) - 1
# im_hight = np.linspace(edge_padding, im_lr - (1+edge_padding), fiducial_points, dtype=np.int64)
# im_wide = np.linspace(edge_padding, im_ud - (1+edge_padding), fiducial_points, dtype=np.int64)
im_x, im_y = np.meshgrid(im_hight, im_wide)
segment_x = (im_lr) // (fiducial_points-1)
segment_y = (im_ud) // (fiducial_points-1)
# plt.plot(im_x, im_y,
# color='limegreen',
# marker='.',
# linestyle='')
# plt.grid(True)
# plt.show()
self.origin_img = cv2.resize(origin_img, (im_ud, im_lr), interpolation=cv2.INTER_CUBIC)
perturbed_bg_ = getDatasets(self.bg_path)
perturbed_bg_img_ = self.bg_path+random.choice(perturbed_bg_)
perturbed_bg_img = cv2.imread(perturbed_bg_img_, flags=cv2.IMREAD_COLOR)
mesh_shape = self.origin_img.shape[:2]
self.synthesis_perturbed_img = np.full((enlarge_img_shrink[0], enlarge_img_shrink[1], 3), 256, dtype=np.float32)#np.zeros_like(perturbed_bg_img)
# self.synthesis_perturbed_img = np.full((enlarge_img_shrink[0], enlarge_img_shrink[1], 3), 0, dtype=np.int16)#np.zeros_like(perturbed_bg_img)
self.new_shape = self.synthesis_perturbed_img.shape[:2]
perturbed_bg_img = cv2.resize(perturbed_bg_img, (save_img_shape[1], save_img_shape[0]), cv2.INPAINT_TELEA)
origin_pixel_position = np.argwhere(np.zeros(mesh_shape, dtype=np.uint32) == 0).reshape(mesh_shape[0], mesh_shape[1], 2)
pixel_position = np.argwhere(np.zeros(self.new_shape, dtype=np.uint32) == 0).reshape(self.new_shape[0], self.new_shape[1], 2)
self.perturbed_xy_ = np.zeros((self.new_shape[0], self.new_shape[1], 2))
# self.perturbed_xy_ = pixel_position.copy().astype(np.float32)
# fiducial_points_grid = origin_pixel_position[im_x, im_y]
self.synthesis_perturbed_label = np.zeros((self.new_shape[0], self.new_shape[1], 2))
x_min, y_min, x_max, y_max = self.adjust_position_v2(0, 0, mesh_shape[0], mesh_shape[1], save_img_shape)
origin_pixel_position += [x_min, y_min]
x_min, y_min, x_max, y_max = self.adjust_position(0, 0, mesh_shape[0], mesh_shape[1])
x_shift = random.randint(-enlarge_img_shrink[0]//16, enlarge_img_shrink[0]//16)
y_shift = random.randint(-enlarge_img_shrink[1]//16, enlarge_img_shrink[1]//16)
x_min += x_shift
x_max += x_shift
y_min += y_shift
y_max += y_shift
'''im_x,y'''
im_x += x_min
im_y += y_min
self.synthesis_perturbed_img[x_min:x_max, y_min:y_max] = self.origin_img
self.synthesis_perturbed_label[x_min:x_max, y_min:y_max] = origin_pixel_position
synthesis_perturbed_img_map = self.synthesis_perturbed_img.copy()
synthesis_perturbed_label_map = self.synthesis_perturbed_label.copy()
foreORbackground_label = np.full((mesh_shape), 1, dtype=np.int16)
foreORbackground_label_map = np.full((self.new_shape), 0, dtype=np.int16)
foreORbackground_label_map[x_min:x_max, y_min:y_max] = foreORbackground_label
# synthesis_perturbed_img_map = self.pad(self.synthesis_perturbed_img.copy(), x_min, y_min, x_max, y_max)
# synthesis_perturbed_label_map = self.pad(synthesis_perturbed_label_map, x_min, y_min, x_max, y_max)
'''*****************************************************************'''
is_normalizationFun_mixture = self.is_perform(0.2, 0.8)
# if not is_normalizationFun_mixture:
normalizationFun_0_1 = False
# normalizationFun_0_1 = self.is_perform(0.5, 0.5)
if fold_curve == 'fold':
fold_curve_random = True
# is_normalizationFun_mixture = False
normalizationFun_0_1 = self.is_perform(0.2, 0.8)
if is_normalizationFun_mixture:
alpha_perturbed = random.randint(80, 120) / 100
else:
if normalizationFun_0_1 and repeat_time < 8:
alpha_perturbed = random.randint(50, 70) / 100
else:
alpha_perturbed = random.randint(70, 130) / 100
else:
fold_curve_random = self.is_perform(0.1, 0.9) # False # self.is_perform(0.01, 0.99)
alpha_perturbed = random.randint(80, 160) / 100
# is_normalizationFun_mixture = False # self.is_perform(0.01, 0.99)
synthesis_perturbed_img = np.full_like(self.synthesis_perturbed_img, 256)
# synthesis_perturbed_img = np.full_like(self.synthesis_perturbed_img, 0, dtype=np.int16)
synthesis_perturbed_label = np.zeros_like(self.synthesis_perturbed_label)
alpha_perturbed_change = self.is_perform(0.5, 0.5)
p_pp_choice = self.is_perform(0.8, 0.2) if fold_curve == 'fold' else self.is_perform(0.1, 0.9)
for repeat_i in range(repeat_time):
if alpha_perturbed_change:
if fold_curve == 'fold':
if is_normalizationFun_mixture:
alpha_perturbed = random.randint(80, 120) / 100
else:
if normalizationFun_0_1 and repeat_time < 8:
alpha_perturbed = random.randint(50, 70) / 100
else:
alpha_perturbed = random.randint(70, 130) / 100
else:
alpha_perturbed = random.randint(80, 160) / 100
''''''
linspace_x = [0, (self.new_shape[0] - im_lr) // 2 - 1,
self.new_shape[0] - (self.new_shape[0] - im_lr) // 2 - 1, self.new_shape[0] - 1]
linspace_y = [0, (self.new_shape[1] - im_ud) // 2 - 1,
self.new_shape[1] - (self.new_shape[1] - im_ud) // 2 - 1, self.new_shape[1] - 1]
linspace_x_seq = [1, 2, 3]
linspace_y_seq = [1, 2, 3]
r_x = random.choice(linspace_x_seq)
r_y = random.choice(linspace_y_seq)
perturbed_p = np.array(
[random.randint(linspace_x[r_x-1] * 10, linspace_x[r_x] * 10),
random.randint(linspace_y[r_y-1] * 10, linspace_y[r_y] * 10)])/10
if ((r_x == 1 or r_x == 3) and (r_y == 1 or r_y == 3)) and p_pp_choice:
linspace_x_seq.remove(r_x)
linspace_y_seq.remove(r_y)
r_x = random.choice(linspace_x_seq)
r_y = random.choice(linspace_y_seq)
perturbed_pp = np.array(
[random.randint(linspace_x[r_x-1] * 10, linspace_x[r_x] * 10),
random.randint(linspace_y[r_y-1] * 10, linspace_y[r_y] * 10)])/10
# perturbed_p, perturbed_pp = np.array(
# [random.randint(0, self.new_shape[0] * 10) / 10,
# random.randint(0, self.new_shape[1] * 10) / 10]) \
# , np.array([random.randint(0, self.new_shape[0] * 10) / 10,
# random.randint(0, self.new_shape[1] * 10) / 10])
# perturbed_p, perturbed_pp = np.array(
# [random.randint((self.new_shape[0]-im_lr)//2*10, (self.new_shape[0]-(self.new_shape[0]-im_lr)//2) * 10) / 10,
# random.randint((self.new_shape[1]-im_ud)//2*10, (self.new_shape[1]-(self.new_shape[1]-im_ud)//2) * 10) / 10]) \
# , np.array([random.randint((self.new_shape[0]-im_lr)//2*10, (self.new_shape[0]-(self.new_shape[0]-im_lr)//2) * 10) / 10,
# random.randint((self.new_shape[1]-im_ud)//2*10, (self.new_shape[1]-(self.new_shape[1]-im_ud)//2) * 10) / 10])
''''''
perturbed_vp = perturbed_pp - perturbed_p
perturbed_vp_norm = np.linalg.norm(perturbed_vp)
perturbed_distance_vertex_and_line = np.dot((perturbed_p - pixel_position), perturbed_vp) / perturbed_vp_norm
''''''
# perturbed_v = np.array([random.randint(-3000, 3000) / 100, random.randint(-3000, 3000) / 100])
# perturbed_v = np.array([random.randint(-4000, 4000) / 100, random.randint(-4000, 4000) / 100])
if fold_curve == 'fold' and self.is_perform(0.6, 0.4): # self.is_perform(0.3, 0.7):
# perturbed_v = np.array([random.randint(-9000, 9000) / 100, random.randint(-9000, 9000) / 100])
perturbed_v = np.array([random.randint(-10000, 10000) / 100, random.randint(-10000, 10000) / 100])
# perturbed_v = np.array([random.randint(-11000, 11000) / 100, random.randint(-11000, 11000) / 100])
else:
# perturbed_v = np.array([random.randint(-9000, 9000) / 100, random.randint(-9000, 9000) / 100])
# perturbed_v = np.array([random.randint(-16000, 16000) / 100, random.randint(-16000, 16000) / 100])
perturbed_v = np.array([random.randint(-8000, 8000) / 100, random.randint(-8000, 8000) / 100])
# perturbed_v = np.array([random.randint(-3500, 3500) / 100, random.randint(-3500, 3500) / 100])
# perturbed_v = np.array([random.randint(-600, 600) / 10, random.randint(-600, 600) / 10])
''''''
if fold_curve == 'fold':
if is_normalizationFun_mixture:
if self.is_perform(0.5, 0.5):
perturbed_d = np.abs(self.get_normalize(perturbed_distance_vertex_and_line))
else:
perturbed_d = self.get_0_1_d(np.abs(perturbed_distance_vertex_and_line), random.randint(1, 2))
else:
if normalizationFun_0_1:
perturbed_d = self.get_0_1_d(np.abs(perturbed_distance_vertex_and_line), 2)
else:
perturbed_d = np.abs(self.get_normalize(perturbed_distance_vertex_and_line))
else:
if is_normalizationFun_mixture:
if self.is_perform(0.5, 0.5):
perturbed_d = np.abs(self.get_normalize(perturbed_distance_vertex_and_line))
else:
perturbed_d = self.get_0_1_d(np.abs(perturbed_distance_vertex_and_line), random.randint(1, 2))
else:
if normalizationFun_0_1:
perturbed_d = self.get_0_1_d(np.abs(perturbed_distance_vertex_and_line), 2)
else:
perturbed_d = np.abs(self.get_normalize(perturbed_distance_vertex_and_line))
''''''
if fold_curve_random:
# omega_perturbed = (alpha_perturbed+0.2) / (perturbed_d + alpha_perturbed)
# omega_perturbed = alpha_perturbed**perturbed_d
omega_perturbed = alpha_perturbed / (perturbed_d + alpha_perturbed)
else:
omega_perturbed = 1 - perturbed_d ** alpha_perturbed
'''shadow'''
if self.is_perform(0.6, 0.4):
synthesis_perturbed_img_map[x_min:x_max, y_min:y_max] = np.minimum(np.maximum(synthesis_perturbed_img_map[x_min:x_max, y_min:y_max] - np.int16(np.round(omega_perturbed[x_min:x_max, y_min:y_max].repeat(3).reshape(x_max-x_min, y_max-y_min, 3) * abs(np.linalg.norm(perturbed_v//2))*np.array([0.4-random.random()*0.1, 0.4-random.random()*0.1, 0.4-random.random()*0.1]))), 0), 255)
''''''
if relativeShift_position in ['position', 'relativeShift_v2']:
self.perturbed_xy_ += np.array([omega_perturbed * perturbed_v[0], omega_perturbed * perturbed_v[1]]).transpose(1, 2, 0)
else:
print('relativeShift_position error')
exit()
'''
flat_position = np.argwhere(np.zeros(self.new_shape, dtype=np.uint32) == 0).reshape(
self.new_shape[0] * self.new_shape[1], 2)
vtx, wts = self.interp_weights(self.perturbed_xy_.reshape(self.new_shape[0] * self.new_shape[1], 2), flat_position)
wts_sum = np.abs(wts).sum(-1)
# flat_img.reshape(flat_shape[0] * flat_shape[1], 3)[:] = interpolate(pixel, vtx, wts)
wts = wts[wts_sum <= 1, :]
vtx = vtx[wts_sum <= 1, :]
synthesis_perturbed_img.reshape(self.new_shape[0] * self.new_shape[1], 3)[wts_sum <= 1,
:] = self.interpolate(synthesis_perturbed_img_map.reshape(self.new_shape[0] * self.new_shape[1], 3), vtx, wts)
synthesis_perturbed_label.reshape(self.new_shape[0] * self.new_shape[1], 2)[wts_sum <= 1,
:] = self.interpolate(synthesis_perturbed_label_map.reshape(self.new_shape[0] * self.new_shape[1], 2), vtx, wts)
foreORbackground_label = np.zeros(self.new_shape)
foreORbackground_label.reshape(self.new_shape[0] * self.new_shape[1], 1)[wts_sum <= 1, :] = self.interpolate(foreORbackground_label_map.reshape(self.new_shape[0] * self.new_shape[1], 1), vtx, wts)
foreORbackground_label[foreORbackground_label < 0.99] = 0
foreORbackground_label[foreORbackground_label >= 0.99] = 1
# synthesis_perturbed_img = np.around(synthesis_perturbed_img).astype(np.uint8)
synthesis_perturbed_label[:, :, 0] *= foreORbackground_label
synthesis_perturbed_label[:, :, 1] *= foreORbackground_label
synthesis_perturbed_img[:, :, 0] *= foreORbackground_label
synthesis_perturbed_img[:, :, 1] *= foreORbackground_label
synthesis_perturbed_img[:, :, 2] *= foreORbackground_label
self.synthesis_perturbed_img = synthesis_perturbed_img
self.synthesis_perturbed_label = synthesis_perturbed_label
'''
'''perspective'''
perspective_shreshold = random.randint(26, 36)*10 # 280
x_min_per, y_min_per, x_max_per, y_max_per = self.adjust_position(perspective_shreshold, perspective_shreshold, self.new_shape[0]-perspective_shreshold, self.new_shape[1]-perspective_shreshold)
pts1 = np.float32([[x_min_per, y_min_per], [x_max_per, y_min_per], [x_min_per, y_max_per], [x_max_per, y_max_per]])
e_1_ = x_max_per - x_min_per
e_2_ = y_max_per - y_min_per
e_3_ = e_2_
e_4_ = e_1_
perspective_shreshold_h = e_1_*0.02
perspective_shreshold_w = e_2_*0.02
a_min_, a_max_ = 70, 110
# if self.is_perform(1, 0):
if fold_curve == 'curve' and self.is_perform(0.5, 0.5):
if self.is_perform(0.5, 0.5):
while True:
pts2 = np.around(
np.float32([[x_min_per - (random.random()) * perspective_shreshold, y_min_per + (random.random()) * perspective_shreshold],
[x_max_per - (random.random()) * perspective_shreshold, y_min_per - (random.random()) * perspective_shreshold],
[x_min_per + (random.random()) * perspective_shreshold, y_max_per + (random.random()) * perspective_shreshold],
[x_max_per + (random.random()) * perspective_shreshold, y_max_per - (random.random()) * perspective_shreshold]])) # right
e_1 = np.linalg.norm(pts2[0]-pts2[1])
e_2 = np.linalg.norm(pts2[0]-pts2[2])
e_3 = np.linalg.norm(pts2[1]-pts2[3])
e_4 = np.linalg.norm(pts2[2]-pts2[3])
if e_1_+perspective_shreshold_h > e_1 and e_2_+perspective_shreshold_w > e_2 and e_3_+perspective_shreshold_w > e_3 and e_4_+perspective_shreshold_h > e_4 and \
e_1_ - perspective_shreshold_h < e_1 and e_2_ - perspective_shreshold_w < e_2 and e_3_ - perspective_shreshold_w < e_3 and e_4_ - perspective_shreshold_h < e_4 and \
abs(e_1-e_4) < perspective_shreshold_h and abs(e_2-e_3) < perspective_shreshold_w:
a0_, a1_, a2_, a3_ = self.get_angle_4(pts2)
if (a0_ > a_min_ and a0_ < a_max_) or (a1_ > a_min_ and a1_ < a_max_) or (a2_ > a_min_ and a2_ < a_max_) or (a3_ > a_min_ and a3_ < a_max_):
break
else:
while True:
pts2 = np.around(
np.float32([[x_min_per + (random.random()) * perspective_shreshold, y_min_per - (random.random()) * perspective_shreshold],
[x_max_per + (random.random()) * perspective_shreshold, y_min_per + (random.random()) * perspective_shreshold],
[x_min_per - (random.random()) * perspective_shreshold, y_max_per - (random.random()) * perspective_shreshold],
[x_max_per - (random.random()) * perspective_shreshold, y_max_per + (random.random()) * perspective_shreshold]]))
e_1 = np.linalg.norm(pts2[0]-pts2[1])
e_2 = np.linalg.norm(pts2[0]-pts2[2])
e_3 = np.linalg.norm(pts2[1]-pts2[3])
e_4 = np.linalg.norm(pts2[2]-pts2[3])
if e_1_+perspective_shreshold_h > e_1 and e_2_+perspective_shreshold_w > e_2 and e_3_+perspective_shreshold_w > e_3 and e_4_+perspective_shreshold_h > e_4 and \
e_1_ - perspective_shreshold_h < e_1 and e_2_ - perspective_shreshold_w < e_2 and e_3_ - perspective_shreshold_w < e_3 and e_4_ - perspective_shreshold_h < e_4 and \
abs(e_1-e_4) < perspective_shreshold_h and abs(e_2-e_3) < perspective_shreshold_w:
a0_, a1_, a2_, a3_ = self.get_angle_4(pts2)
if (a0_ > a_min_ and a0_ < a_max_) or (a1_ > a_min_ and a1_ < a_max_) or (a2_ > a_min_ and a2_ < a_max_) or (a3_ > a_min_ and a3_ < a_max_):
break
else:
while True:
pts2 = np.around(np.float32([[x_min_per+(random.random()-0.5)*perspective_shreshold, y_min_per+(random.random()-0.5)*perspective_shreshold],
[x_max_per+(random.random()-0.5)*perspective_shreshold, y_min_per+(random.random()-0.5)*perspective_shreshold],
[x_min_per+(random.random()-0.5)*perspective_shreshold, y_max_per+(random.random()-0.5)*perspective_shreshold],
[x_max_per+(random.random()-0.5)*perspective_shreshold, y_max_per+(random.random()-0.5)*perspective_shreshold]]))
e_1 = np.linalg.norm(pts2[0]-pts2[1])
e_2 = np.linalg.norm(pts2[0]-pts2[2])
e_3 = np.linalg.norm(pts2[1]-pts2[3])
e_4 = np.linalg.norm(pts2[2]-pts2[3])
if e_1_+perspective_shreshold_h > e_1 and e_2_+perspective_shreshold_w > e_2 and e_3_+perspective_shreshold_w > e_3 and e_4_+perspective_shreshold_h > e_4 and \
e_1_ - perspective_shreshold_h < e_1 and e_2_ - perspective_shreshold_w < e_2 and e_3_ - perspective_shreshold_w < e_3 and e_4_ - perspective_shreshold_h < e_4 and \
abs(e_1-e_4) < perspective_shreshold_h and abs(e_2-e_3) < perspective_shreshold_w:
a0_, a1_, a2_, a3_ = self.get_angle_4(pts2)
if (a0_ > a_min_ and a0_ < a_max_) or (a1_ > a_min_ and a1_ < a_max_) or (a2_ > a_min_ and a2_ < a_max_) or (a3_ > a_min_ and a3_ < a_max_):
break
M = cv2.getPerspectiveTransform(pts1, pts2)
one = np.ones((self.new_shape[0], self.new_shape[1], 1), dtype=np.int16)
matr = np.dstack((pixel_position, one))
new = np.dot(M, matr.reshape(-1, 3).T).T.reshape(self.new_shape[0], self.new_shape[1], 3)
x = new[:, :, 0]/new[:, :, 2]
y = new[:, :, 1]/new[:, :, 2]
perturbed_xy_ = np.dstack((x, y))
# perturbed_xy_round_int = np.around(cv2.bilateralFilter(perturbed_xy_round_int, 9, 75, 75))
# perturbed_xy_round_int = np.around(cv2.blur(perturbed_xy_, (17, 17)))
# perturbed_xy_round_int = cv2.blur(perturbed_xy_round_int, (17, 17))
# perturbed_xy_round_int = cv2.GaussianBlur(perturbed_xy_round_int, (7, 7), 0)
perturbed_xy_ = perturbed_xy_-np.min(perturbed_xy_.T.reshape(2, -1), 1)
# perturbed_xy_round_int = np.around(perturbed_xy_round_int-np.min(perturbed_xy_round_int.T.reshape(2, -1), 1)).astype(np.int16)
self.perturbed_xy_ += perturbed_xy_
'''perspective end'''
'''to img'''
flat_position = np.argwhere(np.zeros(self.new_shape, dtype=np.uint32) == 0).reshape(
self.new_shape[0] * self.new_shape[1], 2)
# self.perturbed_xy_ = cv2.blur(self.perturbed_xy_, (7, 7))
self.perturbed_xy_ = cv2.GaussianBlur(self.perturbed_xy_, (7, 7), 0)
'''get fiducial points'''
fiducial_points_coordinate = self.perturbed_xy_[im_x, im_y]
vtx, wts = self.interp_weights(self.perturbed_xy_.reshape(self.new_shape[0] * self.new_shape[1], 2), flat_position)
wts_sum = np.abs(wts).sum(-1)
# flat_img.reshape(flat_shape[0] * flat_shape[1], 3)[:] = interpolate(pixel, vtx, wts)
wts = wts[wts_sum <= 1, :]
vtx = vtx[wts_sum <= 1, :]
synthesis_perturbed_img.reshape(self.new_shape[0] * self.new_shape[1], 3)[wts_sum <= 1,
:] = self.interpolate(synthesis_perturbed_img_map.reshape(self.new_shape[0] * self.new_shape[1], 3), vtx, wts)
synthesis_perturbed_label.reshape(self.new_shape[0] * self.new_shape[1], 2)[wts_sum <= 1,
:] = self.interpolate(synthesis_perturbed_label_map.reshape(self.new_shape[0] * self.new_shape[1], 2), vtx, wts)
foreORbackground_label = np.zeros(self.new_shape)
foreORbackground_label.reshape(self.new_shape[0] * self.new_shape[1], 1)[wts_sum <= 1, :] = self.interpolate(foreORbackground_label_map.reshape(self.new_shape[0] * self.new_shape[1], 1), vtx, wts)
foreORbackground_label[foreORbackground_label < 0.99] = 0
foreORbackground_label[foreORbackground_label >= 0.99] = 1
self.synthesis_perturbed_img = synthesis_perturbed_img
self.synthesis_perturbed_label = synthesis_perturbed_label
self.foreORbackground_label = foreORbackground_label
'''draw fiducial points
stepSize = 0
fiducial_points_synthesis_perturbed_img = self.synthesis_perturbed_img.copy()
for l in fiducial_points_coordinate.astype(np.int64).reshape(-1,2):
cv2.circle(fiducial_points_synthesis_perturbed_img, (l[1] + math.ceil(stepSize / 2), l[0] + math.ceil(stepSize / 2)), 5, (0, 0, 255), -1)
cv2.imwrite('/lustre/home/gwxie/program/project/unwarp/unwarp_perturbed/TPS/img/cv_TPS_large.jpg', fiducial_points_synthesis_perturbed_img)
'''
'''clip'''
perturbed_x_min, perturbed_y_min, perturbed_x_max, perturbed_y_max = -1, -1, self.new_shape[0], self.new_shape[1]
for x in range(self.new_shape[0] // 2, perturbed_x_max):
if np.sum(self.synthesis_perturbed_img[x, :]) == 768 * self.new_shape[1] and perturbed_x_max - 1 > x:
perturbed_x_max = x
break
for x in range(self.new_shape[0] // 2, perturbed_x_min, -1):
if np.sum(self.synthesis_perturbed_img[x, :]) == 768 * self.new_shape[1] and x > 0:
perturbed_x_min = x
break
for y in range(self.new_shape[1] // 2, perturbed_y_max):
if np.sum(self.synthesis_perturbed_img[:, y]) == 768 * self.new_shape[0] and perturbed_y_max - 1 > y:
perturbed_y_max = y
break
for y in range(self.new_shape[1] // 2, perturbed_y_min, -1):
if np.sum(self.synthesis_perturbed_img[:, y]) == 768 * self.new_shape[0] and y > 0:
perturbed_y_min = y
break
if perturbed_x_min == 0 or perturbed_x_max == self.new_shape[0] or perturbed_y_min == self.new_shape[1] or perturbed_y_max == self.new_shape[1]:
raise Exception('clip error')
if perturbed_x_max - perturbed_x_min < im_lr//2 or perturbed_y_max - perturbed_y_min < im_ud//2:
raise Exception('clip error')
perfix_ = self.save_suffix+'_'+str(m)+'_'+str(n)
is_shrink = False
if perturbed_x_max - perturbed_x_min > save_img_shape[0] or perturbed_y_max - perturbed_y_min > save_img_shape[1]:
is_shrink = True
synthesis_perturbed_img = cv2.resize(self.synthesis_perturbed_img[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max, :].copy(), (im_ud, im_lr), interpolation=cv2.INTER_LINEAR)
synthesis_perturbed_label = cv2.resize(self.synthesis_perturbed_label[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max, :].copy(), (im_ud, im_lr), interpolation=cv2.INTER_LINEAR)
foreORbackground_label = cv2.resize(self.foreORbackground_label[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max].copy(), (im_ud, im_lr), interpolation=cv2.INTER_LINEAR)
foreORbackground_label[foreORbackground_label < 0.99] = 0
foreORbackground_label[foreORbackground_label >= 0.99] = 1
'''shrink fiducial points'''
center_x_l, center_y_l = perturbed_x_min + (perturbed_x_max - perturbed_x_min) // 2, perturbed_y_min + (perturbed_y_max - perturbed_y_min) // 2
fiducial_points_coordinate_copy = fiducial_points_coordinate.copy()
shrink_x = im_lr/(perturbed_x_max - perturbed_x_min)
shrink_y = im_ud/(perturbed_y_max - perturbed_y_min)
fiducial_points_coordinate *= [shrink_x, shrink_y]
center_x_l *= shrink_x
center_y_l *= shrink_y
# fiducial_points_coordinate[1:, 1:] *= [shrink_x, shrink_y]
# fiducial_points_coordinate[1:, :1, 0] *= shrink_x
# fiducial_points_coordinate[:1, 1:, 1] *= shrink_y
# perturbed_x_min_copy, perturbed_y_min_copy, perturbed_x_max_copy, perturbed_y_max_copy = perturbed_x_min, perturbed_y_min, perturbed_x_max, perturbed_y_max
perturbed_x_min, perturbed_y_min, perturbed_x_max, perturbed_y_max = self.adjust_position_v2(0, 0, im_lr, im_ud, self.new_shape)
self.synthesis_perturbed_img = np.full_like(self.synthesis_perturbed_img, 256)
self.synthesis_perturbed_label = np.zeros_like(self.synthesis_perturbed_label)
self.foreORbackground_label = np.zeros_like(self.foreORbackground_label)
self.synthesis_perturbed_img[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max, :] = synthesis_perturbed_img
self.synthesis_perturbed_label[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max, :] = synthesis_perturbed_label
self.foreORbackground_label[perturbed_x_min:perturbed_x_max, perturbed_y_min:perturbed_y_max] = foreORbackground_label
center_x, center_y = perturbed_x_min + (perturbed_x_max - perturbed_x_min) // 2, perturbed_y_min + (perturbed_y_max - perturbed_y_min) // 2
if is_shrink:
fiducial_points_coordinate += [center_x-center_x_l, center_y-center_y_l]
'''draw fiducial points
stepSize = 0
fiducial_points_synthesis_perturbed_img = self.synthesis_perturbed_img.copy()
for l in fiducial_points_coordinate.astype(np.int64).reshape(-1, 2):
cv2.circle(fiducial_points_synthesis_perturbed_img,
(l[1] + math.ceil(stepSize / 2), l[0] + math.ceil(stepSize / 2)), 5, (0, 0, 255), -1)
cv2.imwrite('/lustre/home/gwxie/program/project/unwarp/unwarp_perturbed/TPS/img/cv_TPS_small.jpg',fiducial_points_synthesis_perturbed_img)
'''
self.new_shape = save_img_shape
self.synthesis_perturbed_img = self.synthesis_perturbed_img[
center_x - self.new_shape[0] // 2:center_x + self.new_shape[0] // 2,
center_y - self.new_shape[1] // 2:center_y + self.new_shape[1] // 2,
:].copy()
self.synthesis_perturbed_label = self.synthesis_perturbed_label[
center_x - self.new_shape[0] // 2:center_x + self.new_shape[0] // 2,
center_y - self.new_shape[1] // 2:center_y + self.new_shape[1] // 2,
:].copy()
self.foreORbackground_label = self.foreORbackground_label[
center_x - self.new_shape[0] // 2:center_x + self.new_shape[0] // 2,
center_y - self.new_shape[1] // 2:center_y + self.new_shape[1] // 2].copy()
perturbed_x_ = max(self.new_shape[0] - (perturbed_x_max - perturbed_x_min), 0)
perturbed_x_min = perturbed_x_ // 2
perturbed_x_max = self.new_shape[0] - perturbed_x_ // 2 if perturbed_x_%2 == 0 else self.new_shape[0] - (perturbed_x_ // 2 + 1)
perturbed_y_ = max(self.new_shape[1] - (perturbed_y_max - perturbed_y_min), 0)
perturbed_y_min = perturbed_y_ // 2
perturbed_y_max = self.new_shape[1] - perturbed_y_ // 2 if perturbed_y_%2 == 0 else self.new_shape[1] - (perturbed_y_ // 2 + 1)
'''clip
perturbed_x_min, perturbed_y_min, perturbed_x_max, perturbed_y_max = -1, -1, self.new_shape[0], self.new_shape[1]
for x in range(self.new_shape[0] // 2, perturbed_x_max):
if np.sum(self.synthesis_perturbed_img[x, :]) == 768 * self.new_shape[1] and perturbed_x_max - 1 > x:
perturbed_x_max = x
break
for x in range(self.new_shape[0] // 2, perturbed_x_min, -1):
if np.sum(self.synthesis_perturbed_img[x, :]) == 768 * self.new_shape[1] and x > 0:
perturbed_x_min = x
break
for y in range(self.new_shape[1] // 2, perturbed_y_max):
if np.sum(self.synthesis_perturbed_img[:, y]) == 768 * self.new_shape[0] and perturbed_y_max - 1 > y:
perturbed_y_max = y
break
for y in range(self.new_shape[1] // 2, perturbed_y_min, -1):
if np.sum(self.synthesis_perturbed_img[:, y]) == 768 * self.new_shape[0] and y > 0:
perturbed_y_min = y
break
center_x, center_y = perturbed_x_min+(perturbed_x_max - perturbed_x_min)//2, perturbed_y_min+(perturbed_y_max - perturbed_y_min)//2
perfix_ = self.save_suffix+'_'+str(m)+'_'+str(n)
self.new_shape = save_img_shape
perturbed_x_ = max(self.new_shape[0] - (perturbed_x_max - perturbed_x_min), 0)
perturbed_x_min = perturbed_x_ // 2
perturbed_x_max = self.new_shape[0] - perturbed_x_ // 2 if perturbed_x_%2 == 0 else self.new_shape[0] - (perturbed_x_ // 2 + 1)
perturbed_y_ = max(self.new_shape[1] - (perturbed_y_max - perturbed_y_min), 0)
perturbed_y_min = perturbed_y_ // 2
perturbed_y_max = self.new_shape[1] - perturbed_y_ // 2 if perturbed_y_%2 == 0 else self.new_shape[1] - (perturbed_y_ // 2 + 1)
self.synthesis_perturbed_img = self.synthesis_perturbed_img[center_x-self.new_shape[0]//2:center_x+self.new_shape[0]//2, center_y-self.new_shape[1]//2:center_y+self.new_shape[1]//2, :].copy()
self.synthesis_perturbed_label = self.synthesis_perturbed_label[center_x-self.new_shape[0]//2:center_x+self.new_shape[0]//2, center_y-self.new_shape[1]//2:center_y+self.new_shape[1]//2, :].copy()
self.foreORbackground_label = self.foreORbackground_label[center_x-self.new_shape[0]//2:center_x+self.new_shape[0]//2, center_y-self.new_shape[1]//2:center_y+self.new_shape[1]//2].copy()
'''
'''save'''
pixel_position = np.argwhere(np.zeros(self.new_shape, dtype=np.uint32) == 0).reshape(self.new_shape[0], self.new_shape[1], 2)
if relativeShift_position == 'relativeShift_v2':
self.synthesis_perturbed_label -= pixel_position
fiducial_points_coordinate -= [center_x - self.new_shape[0] // 2, center_y - self.new_shape[1] // 2]
self.synthesis_perturbed_label[:, :, 0] *= self.foreORbackground_label
self.synthesis_perturbed_label[:, :, 1] *= self.foreORbackground_label
self.synthesis_perturbed_img[:, :, 0] *= self.foreORbackground_label
self.synthesis_perturbed_img[:, :, 1] *= self.foreORbackground_label
self.synthesis_perturbed_img[:, :, 2] *= self.foreORbackground_label
'''
synthesis_perturbed_img_filter = self.synthesis_perturbed_img.copy()
synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (3, 3), 0)
# if self.is_perform(0.9, 0.1) or repeat_time > 5:
# # if self.is_perform(0.1, 0.9) and repeat_time > 9:
# # synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (7, 7), 0)
# # else:
# synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (5, 5), 0)
# else:
# synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (3, 3), 0)
self.synthesis_perturbed_img[self.foreORbackground_label == 1] = synthesis_perturbed_img_filter[self.foreORbackground_label == 1]
'''
'''
perturbed_bg_img = perturbed_bg_img.astype(np.float32)
perturbed_bg_img[:, :, 0] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 1] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 2] *= 1 - self.foreORbackground_label
self.synthesis_perturbed_img += perturbed_bg_img
HSV
perturbed_bg_img = perturbed_bg_img.astype(np.float32)
if self.is_perform(0.1, 0.9):
if self.is_perform(0.2, 0.8):
synthesis_perturbed_img_clip_HSV = self.synthesis_perturbed_img.copy()
synthesis_perturbed_img_clip_HSV = cv2.cvtColor(synthesis_perturbed_img_clip_HSV, cv2.COLOR_RGB2HSV)
H_, S_, V_ = (random.random()-0.2)*20, (random.random()-0.2)/8, (random.random()-0.2)*20
synthesis_perturbed_img_clip_HSV[:, :, 0], synthesis_perturbed_img_clip_HSV[:, :, 1], synthesis_perturbed_img_clip_HSV[:, :, 2] = synthesis_perturbed_img_clip_HSV[:, :, 0]-H_, synthesis_perturbed_img_clip_HSV[:, :, 1]-S_, synthesis_perturbed_img_clip_HSV[:, :, 2]-V_
synthesis_perturbed_img_clip_HSV = cv2.cvtColor(synthesis_perturbed_img_clip_HSV, cv2.COLOR_HSV2RGB)
perturbed_bg_img[:, :, 0] *= 1-self.foreORbackground_label
perturbed_bg_img[:, :, 1] *= 1-self.foreORbackground_label
perturbed_bg_img[:, :, 2] *= 1-self.foreORbackground_label
synthesis_perturbed_img_clip_HSV += perturbed_bg_img
self.synthesis_perturbed_img = synthesis_perturbed_img_clip_HSV
else:
perturbed_bg_img_HSV = perturbed_bg_img
perturbed_bg_img_HSV = cv2.cvtColor(perturbed_bg_img_HSV, cv2.COLOR_RGB2HSV)
H_, S_, V_ = (random.random()-0.5)*20, (random.random()-0.5)/8, (random.random()-0.2)*20
perturbed_bg_img_HSV[:, :, 0], perturbed_bg_img_HSV[:, :, 1], perturbed_bg_img_HSV[:, :, 2] = perturbed_bg_img_HSV[:, :, 0]-H_, perturbed_bg_img_HSV[:, :, 1]-S_, perturbed_bg_img_HSV[:, :, 2]-V_
perturbed_bg_img_HSV = cv2.cvtColor(perturbed_bg_img_HSV, cv2.COLOR_HSV2RGB)
perturbed_bg_img_HSV[:, :, 0] *= 1-self.foreORbackground_label
perturbed_bg_img_HSV[:, :, 1] *= 1-self.foreORbackground_label
perturbed_bg_img_HSV[:, :, 2] *= 1-self.foreORbackground_label
self.synthesis_perturbed_img += perturbed_bg_img_HSV
# self.synthesis_perturbed_img[np.sum(self.synthesis_perturbed_img, 2) == 771] = perturbed_bg_img_HSV[np.sum(self.synthesis_perturbed_img, 2) == 771]
else:
synthesis_perturbed_img_clip_HSV = self.synthesis_perturbed_img.copy()
perturbed_bg_img[:, :, 0] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 1] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 2] *= 1 - self.foreORbackground_label
synthesis_perturbed_img_clip_HSV += perturbed_bg_img
# synthesis_perturbed_img_clip_HSV[np.sum(self.synthesis_perturbed_img, 2) == 771] = perturbed_bg_img[np.sum(self.synthesis_perturbed_img, 2) == 771]
synthesis_perturbed_img_clip_HSV = cv2.cvtColor(synthesis_perturbed_img_clip_HSV, cv2.COLOR_RGB2HSV)
H_, S_, V_ = (random.random()-0.5)*20, (random.random()-0.5)/10, (random.random()-0.4)*20
synthesis_perturbed_img_clip_HSV[:, :, 0], synthesis_perturbed_img_clip_HSV[:, :, 1], synthesis_perturbed_img_clip_HSV[:, :, 2] = synthesis_perturbed_img_clip_HSV[:, :, 0]-H_, synthesis_perturbed_img_clip_HSV[:, :, 1]-S_, synthesis_perturbed_img_clip_HSV[:, :, 2]-V_
synthesis_perturbed_img_clip_HSV = cv2.cvtColor(synthesis_perturbed_img_clip_HSV, cv2.COLOR_HSV2RGB)
self.synthesis_perturbed_img = synthesis_perturbed_img_clip_HSV
'''
'''HSV_v2'''
perturbed_bg_img = perturbed_bg_img.astype(np.float32)
# if self.is_perform(1, 0):
# if self.is_perform(1, 0):
if self.is_perform(0.1, 0.9):
if self.is_perform(0.2, 0.8):
synthesis_perturbed_img_clip_HSV = self.synthesis_perturbed_img.copy()
synthesis_perturbed_img_clip_HSV = self.HSV_v1(synthesis_perturbed_img_clip_HSV)
perturbed_bg_img[:, :, 0] *= 1-self.foreORbackground_label
perturbed_bg_img[:, :, 1] *= 1-self.foreORbackground_label
perturbed_bg_img[:, :, 2] *= 1-self.foreORbackground_label
synthesis_perturbed_img_clip_HSV += perturbed_bg_img
self.synthesis_perturbed_img = synthesis_perturbed_img_clip_HSV
else:
perturbed_bg_img_HSV = perturbed_bg_img
perturbed_bg_img_HSV = self.HSV_v1(perturbed_bg_img_HSV)
perturbed_bg_img_HSV[:, :, 0] *= 1-self.foreORbackground_label
perturbed_bg_img_HSV[:, :, 1] *= 1-self.foreORbackground_label
perturbed_bg_img_HSV[:, :, 2] *= 1-self.foreORbackground_label
self.synthesis_perturbed_img += perturbed_bg_img_HSV
# self.synthesis_perturbed_img[np.sum(self.synthesis_perturbed_img, 2) == 771] = perturbed_bg_img_HSV[np.sum(self.synthesis_perturbed_img, 2) == 771]
else:
synthesis_perturbed_img_clip_HSV = self.synthesis_perturbed_img.copy()
perturbed_bg_img[:, :, 0] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 1] *= 1 - self.foreORbackground_label
perturbed_bg_img[:, :, 2] *= 1 - self.foreORbackground_label
synthesis_perturbed_img_clip_HSV += perturbed_bg_img
synthesis_perturbed_img_clip_HSV = self.HSV_v1(synthesis_perturbed_img_clip_HSV)
self.synthesis_perturbed_img = synthesis_perturbed_img_clip_HSV
''''''
# cv2.imwrite(self.save_path+'clip/'+perfix_+'_'+fold_curve+str(perturbed_time)+'-'+str(repeat_time)+'.png', synthesis_perturbed_img_clip)
self.synthesis_perturbed_img[self.synthesis_perturbed_img < 0] = 0
self.synthesis_perturbed_img[self.synthesis_perturbed_img > 255] = 255
self.synthesis_perturbed_img = np.around(self.synthesis_perturbed_img).astype(np.uint8)
label = np.zeros_like(self.synthesis_perturbed_img, dtype=np.float32)
label[:, :, :2] = self.synthesis_perturbed_label
label[:, :, 2] = self.foreORbackground_label
# grey = np.around(self.synthesis_perturbed_img[:, :, 0] * 0.2989 + self.synthesis_perturbed_img[:, :, 1] * 0.5870 + self.synthesis_perturbed_img[:, :, 0] * 0.1140).astype(np.int16)
# synthesis_perturbed_grey = np.concatenate((grey.reshape(self.new_shape[0], self.new_shape[1], 1), label), axis=2)
synthesis_perturbed_color = np.concatenate((self.synthesis_perturbed_img, label), axis=2)
self.synthesis_perturbed_color = np.zeros_like(synthesis_perturbed_color, dtype=np.float32)
# self.synthesis_perturbed_grey = np.zeros_like(synthesis_perturbed_grey, dtype=np.float32)
reduce_value_x = int(round(min((random.random() / 2) * (self.new_shape[0] - (perturbed_x_max - perturbed_x_min)), min(reduce_value, reduce_value_v2))))
reduce_value_y = int(round(min((random.random() / 2) * (self.new_shape[1] - (perturbed_y_max - perturbed_y_min)), min(reduce_value, reduce_value_v2))))
perturbed_x_min = max(perturbed_x_min - reduce_value_x, 0)
perturbed_x_max = min(perturbed_x_max + reduce_value_x, self.new_shape[0])
perturbed_y_min = max(perturbed_y_min - reduce_value_y, 0)
perturbed_y_max = min(perturbed_y_max + reduce_value_y, self.new_shape[1])
if im_lr >= im_ud:
self.synthesis_perturbed_color[:, perturbed_y_min:perturbed_y_max, :] = synthesis_perturbed_color[:, perturbed_y_min:perturbed_y_max, :]
# self.synthesis_perturbed_grey[:, perturbed_y_min:perturbed_y_max, :] = synthesis_perturbed_grey[:, perturbed_y_min:perturbed_y_max, :]
else:
self.synthesis_perturbed_color[perturbed_x_min:perturbed_x_max, :, :] = synthesis_perturbed_color[perturbed_x_min:perturbed_x_max, :, :]
# self.synthesis_perturbed_grey[perturbed_x_min:perturbed_x_max, :, :] = synthesis_perturbed_grey[perturbed_x_min:perturbed_x_max, :, :]
'''blur'''
if self.is_perform(0.1, 0.9):
synthesis_perturbed_img_filter = self.synthesis_perturbed_color[:, :, :3].copy()
if self.is_perform(0.1, 0.9):
synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (5, 5), 0)
else:
synthesis_perturbed_img_filter = cv2.GaussianBlur(synthesis_perturbed_img_filter, (3, 3), 0)
if self.is_perform(0.5, 0.5):
self.synthesis_perturbed_color[:, :, :3][self.synthesis_perturbed_color[:, :, 5] == 1] = synthesis_perturbed_img_filter[self.synthesis_perturbed_color[:, :, 5] == 1]
else:
self.synthesis_perturbed_color[:, :, :3] = synthesis_perturbed_img_filter
fiducial_points_coordinate = fiducial_points_coordinate[:, :, ::-1]
'''draw fiducial points'''
stepSize = 0
fiducial_points_synthesis_perturbed_img = self.synthesis_perturbed_color[:, :, :3].copy()
for l in fiducial_points_coordinate.astype(np.int64).reshape(-1, 2):
cv2.circle(fiducial_points_synthesis_perturbed_img, (l[0] + math.ceil(stepSize / 2), l[1] + math.ceil(stepSize / 2)), 2, (0, 0, 255), -1)
cv2.imwrite(self.save_path + 'fiducial_points/' + perfix_ + '_' + fold_curve + '.png', fiducial_points_synthesis_perturbed_img)
cv2.imwrite(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png', self.synthesis_perturbed_color[:, :, :3])
'''forward-begin'''
self.forward_mapping = np.full((save_img_shape[0], save_img_shape[1], 2), 0, dtype=np.float32)
forward_mapping = np.full((save_img_shape[0], save_img_shape[1], 2), 0, dtype=np.float32)
forward_position = (self.synthesis_perturbed_color[:, :, 3:5] + pixel_position)[self.synthesis_perturbed_color[:, :, 5] != 0, :]
flat_position = np.argwhere(np.zeros(save_img_shape, dtype=np.uint32) == 0)
vtx, wts = self.interp_weights(forward_position, flat_position)
wts_sum = np.abs(wts).sum(-1)
wts = wts[wts_sum <= 1, :]
vtx = vtx[wts_sum <= 1, :]
flat_position_forward = flat_position.reshape(save_img_shape[0], save_img_shape[1], 2)[self.synthesis_perturbed_color[:, :, 5] != 0, :]
forward_mapping.reshape(save_img_shape[0] * save_img_shape[1], 2)[wts_sum <= 1, :] = self.interpolate(flat_position_forward, vtx, wts)
forward_mapping = forward_mapping.reshape(save_img_shape[0], save_img_shape[1], 2)
mapping_x_min_, mapping_y_min_, mapping_x_max_, mapping_y_max_ = self.adjust_position_v2(0, 0, im_lr, im_ud, self.new_shape)
shreshold_zoom_out = 2
mapping_x_min = mapping_x_min_ + shreshold_zoom_out
mapping_y_min = mapping_y_min_ + shreshold_zoom_out
mapping_x_max = mapping_x_max_ - shreshold_zoom_out
mapping_y_max = mapping_y_max_ - shreshold_zoom_out
self.forward_mapping[mapping_x_min:mapping_x_max, mapping_y_min:mapping_y_max] = forward_mapping[mapping_x_min:mapping_x_max, mapping_y_min:mapping_y_max]
self.scan_img = np.full((save_img_shape[0], save_img_shape[1], 3), 0, dtype=np.float32)
self.scan_img[mapping_x_min_:mapping_x_max_, mapping_y_min_:mapping_y_max_] = self.origin_img
self.origin_img = self.scan_img
# flat_img = np.full((save_img_shape[0], save_img_shape[1], 3), 0, dtype=np.float32)
# cv2.remap(self.synthesis_perturbed_color[:, :, :3], self.forward_mapping[:, :, 1], self.forward_mapping[:, :, 0], cv2.INTER_LINEAR, flat_img)
# cv2.imwrite(self.save_path + 'outputs/1.jpg', flat_img)
'''forward-end'''
synthesis_perturbed_data = {
'fiducial_points': fiducial_points_coordinate,
'segment': np.array((segment_x, segment_y))
}
cv2.imwrite(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png', self.synthesis_perturbed_color[:, :, :3])
with open(self.save_path+'color/'+perfix_+'_'+fold_curve+'.gw', 'wb') as f:
pickle_perturbed_data = pickle.dumps(synthesis_perturbed_data)
f.write(pickle_perturbed_data)
# with open(self.save_path+'grey/'+perfix_+'_'+fold_curve+'.gw', 'wb') as f:
# pickle_perturbed_data = pickle.dumps(self.synthesis_perturbed_grey)
# f.write(pickle_perturbed_data)
# cv2.imwrite(self.save_path+'grey_im/'+perfix_+'_'+fold_curve+'.png', self.synthesis_perturbed_color[:, :, :1])
# cv2.imwrite(self.save_path + 'scan/' + self.save_suffix + '_' + str(m) + '.png', self.origin_img)
trian_t = time.time() - begin_train
mm, ss = divmod(trian_t, 60)
hh, mm = divmod(mm, 60)
print(str(m)+'_'+str(n)+'_'+fold_curve+' '+str(repeat_time)+" Time : %02d:%02d:%02d\n" % (hh, mm, ss))
def multiThread(m, n, img_path_, bg_path_, save_path, save_suffix):
saveFold = perturbed(img_path_, bg_path_, save_path, save_suffix)
saveCurve = perturbed(img_path_, bg_path_, save_path, save_suffix)
repeat_time = min(max(round(np.random.normal(10, 3)), 5), 16)
fold = threading.Thread(target=saveFold.save_img, args=(m, n, 'fold', repeat_time, 'relativeShift_v2'), name='fold')
curve = threading.Thread(target=saveCurve.save_img, args=(m, n, 'curve', repeat_time, 'relativeShift_v2'), name='curve')
fold.start()
curve.start()
curve.join()
fold.join()
def xgw(args):
path = args.path
bg_path = args.bg_path
if args.output_path is None:
save_path = '/lustre/home/gwxie/data/unwarp_new/train/general1024/general1024_v1/'
else:
save_path = args.output_path
# if not os.path.exists(save_path + 'grey/'):
# os.makedirs(save_path + 'grey/')
if not os.path.exists(save_path + 'color/'):
os.makedirs(save_path + 'color/')
if not os.path.exists(save_path + 'fiducial_points/'):
os.makedirs(save_path + 'fiducial_points/')
if not os.path.exists(save_path + 'png/'):
os.makedirs(save_path + 'png/')
if not os.path.exists(save_path + 'scan/'):
os.makedirs(save_path + 'scan/')
if not os.path.exists(save_path + 'outputs/'):
os.makedirs(save_path + 'outputs/')
save_suffix = str.split(args.path, '/')[-2]
all_img_path = getDatasets(path)
all_bgImg_path = getDatasets(bg_path)
global begin_train
begin_train = time.time()
fiducial_points = 61 # 31
process_pool = Pool(2)
for m, img_path in enumerate(all_img_path):
for n in range(args.sys_num):
img_path_ = path+img_path
bg_path_ = bg_path+random.choice(all_bgImg_path)+'/'
for m_n in range(10):
try:
saveFold = perturbed(img_path_, bg_path_, save_path, save_suffix)
saveCurve = perturbed(img_path_, bg_path_, save_path, save_suffix)
repeat_time = min(max(round(np.random.normal(12, 4)), 1), 18)
# repeat_time = min(max(round(np.random.normal(8, 4)), 1), 12) # random.randint(1, 2) # min(max(round(np.random.normal(8, 4)), 1), 12)
process_pool.apply_async(func=saveFold.save_img, args=(m, n, 'fold', repeat_time, fiducial_points, 'relativeShift_v2'))
repeat_time = min(max(round(np.random.normal(8, 4)), 1), 13)
# repeat_time = min(max(round(np.random.normal(6, 4)), 1), 10)
process_pool.apply_async(func=saveCurve.save_img, args=(m, n, 'curve', repeat_time, fiducial_points, 'relativeShift_v2'))
except BaseException as err:
print(err)
continue
break
# print('end')
process_pool.close()
process_pool.join()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Hyperparams')
parser.add_argument('--path',
default='./scan/new/', type=str,
help='the path of origin img.')
parser.add_argument('--bg_path',
default='./background/', type=str,
help='the path of bg img.')
parser.add_argument('--output_path',
default='./output/', type=str,
help='the path of origin img.')
# parser.set_defaults(output_path='test')
parser.add_argument('--count_from', '-p', default=0, type=int,
metavar='N', help='print frequency (default: 10)') # print frequency
parser.add_argument('--repeat_T', default=0, type=int)
parser.add_argument('--sys_num', default=6, type=int)
args = parser.parse_args()
xgw(args)
| [((600, 615), 'os.listdir', 'os.listdir', (['dir'], {}), '(dir)\n', (610, 615), False, 'import sys, os\n'), ((44024, 44137), 'threading.Thread', 'threading.Thread', ([], {'target': 'saveFold.save_img', 'args': "(m, n, 'fold', repeat_time, 'relativeShift_v2')", 'name': '"""fold"""'}), "(target=saveFold.save_img, args=(m, n, 'fold', repeat_time,\n 'relativeShift_v2'), name='fold')\n", (44040, 44137), False, 'import threading\n'), ((44143, 44259), 'threading.Thread', 'threading.Thread', ([], {'target': 'saveCurve.save_img', 'args': "(m, n, 'curve', repeat_time, 'relativeShift_v2')", 'name': '"""curve"""'}), "(target=saveCurve.save_img, args=(m, n, 'curve',\n repeat_time, 'relativeShift_v2'), name='curve')\n", (44159, 44259), False, 'import threading\n'), ((45196, 45207), 'time.time', 'time.time', ([], {}), '()\n', (45205, 45207), False, 'import time\n'), ((45251, 45258), 'multiprocessing.Pool', 'Pool', (['(2)'], {}), '(2)\n', (45255, 45258), False, 'from multiprocessing import Pool\n'), ((46370, 46420), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {'description': '"""Hyperparams"""'}), "(description='Hyperparams')\n", (46393, 46420), False, 'import argparse\n'), ((965, 1010), 'cv2.imread', 'cv2.imread', (['self.path'], {'flags': 'cv2.IMREAD_COLOR'}), '(self.path, flags=cv2.IMREAD_COLOR)\n', (975, 1010), False, 'import cv2\n'), ((1169, 1300), 'numpy.random.choice', 'np.random.choice', (['[2 * 2, 4 * 2, 8 * 2, 16 * 2, 24 * 2, 32 * 2, 40 * 2, 48 * 2]'], {'p': '[0.02, 0.18, 0.2, 0.3, 0.1, 0.1, 0.08, 0.02]'}), '([2 * 2, 4 * 2, 8 * 2, 16 * 2, 24 * 2, 32 * 2, 40 * 2, 48 *\n 2], p=[0.02, 0.18, 0.2, 0.3, 0.1, 0.1, 0.08, 0.02])\n', (1185, 1300), True, 'import numpy as np\n'), ((1935, 2064), 'numpy.random.choice', 'np.random.choice', (['[2 * 2, 4 * 2, 8 * 2, 16 * 2, 24 * 2, 28 * 2, 32 * 2, 48 * 2]'], {'p': '[0.02, 0.18, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1]'}), '([2 * 2, 4 * 2, 8 * 2, 16 * 2, 24 * 2, 28 * 2, 32 * 2, 48 *\n 2], p=[0.02, 0.18, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1])\n', (1951, 2064), True, 'import numpy as np\n'), ((2807, 2892), 'numpy.linspace', 'np.linspace', (['edge_padding', '(im_lr - edge_padding)', 'fiducial_points'], {'dtype': 'np.int64'}), '(edge_padding, im_lr - edge_padding, fiducial_points, dtype=np.int64\n )\n', (2818, 2892), True, 'import numpy as np\n'), ((2900, 2985), 'numpy.linspace', 'np.linspace', (['edge_padding', '(im_ud - edge_padding)', 'fiducial_points'], {'dtype': 'np.int64'}), '(edge_padding, im_ud - edge_padding, fiducial_points, dtype=np.int64\n )\n', (2911, 2985), True, 'import numpy as np\n'), ((3389, 3419), 'numpy.meshgrid', 'np.meshgrid', (['im_hight', 'im_wide'], {}), '(im_hight, im_wide)\n', (3400, 3419), True, 'import numpy as np\n'), ((3656, 3725), 'cv2.resize', 'cv2.resize', (['origin_img', '(im_ud, im_lr)'], {'interpolation': 'cv2.INTER_CUBIC'}), '(origin_img, (im_ud, im_lr), interpolation=cv2.INTER_CUBIC)\n', (3666, 3725), False, 'import cv2\n'), ((3856, 3909), 'cv2.imread', 'cv2.imread', (['perturbed_bg_img_'], {'flags': 'cv2.IMREAD_COLOR'}), '(perturbed_bg_img_, flags=cv2.IMREAD_COLOR)\n', (3866, 3909), False, 'import cv2\n'), ((3986, 4072), 'numpy.full', 'np.full', (['(enlarge_img_shrink[0], enlarge_img_shrink[1], 3)', '(256)'], {'dtype': 'np.float32'}), '((enlarge_img_shrink[0], enlarge_img_shrink[1], 3), 256, dtype=np.\n float32)\n', (3993, 4072), True, 'import numpy as np\n'), ((4324, 4416), 'cv2.resize', 'cv2.resize', (['perturbed_bg_img', '(save_img_shape[1], save_img_shape[0])', 'cv2.INPAINT_TELEA'], {}), '(perturbed_bg_img, (save_img_shape[1], save_img_shape[0]), cv2.\n INPAINT_TELEA)\n', (4334, 4416), False, 'import cv2\n'), ((4687, 4738), 'numpy.zeros', 'np.zeros', (['(self.new_shape[0], self.new_shape[1], 2)'], {}), '((self.new_shape[0], self.new_shape[1], 2))\n', (4695, 4738), True, 'import numpy as np\n'), ((4902, 4953), 'numpy.zeros', 'np.zeros', (['(self.new_shape[0], self.new_shape[1], 2)'], {}), '((self.new_shape[0], self.new_shape[1], 2))\n', (4910, 4953), True, 'import numpy as np\n'), ((5204, 5277), 'random.randint', 'random.randint', (['(-enlarge_img_shrink[0] // 16)', '(enlarge_img_shrink[0] // 16)'], {}), '(-enlarge_img_shrink[0] // 16, enlarge_img_shrink[0] // 16)\n', (5218, 5277), False, 'import random\n'), ((5286, 5359), 'random.randint', 'random.randint', (['(-enlarge_img_shrink[1] // 16)', '(enlarge_img_shrink[1] // 16)'], {}), '(-enlarge_img_shrink[1] // 16, enlarge_img_shrink[1] // 16)\n', (5300, 5359), False, 'import random\n'), ((5808, 5846), 'numpy.full', 'np.full', (['mesh_shape', '(1)'], {'dtype': 'np.int16'}), '(mesh_shape, 1, dtype=np.int16)\n', (5815, 5846), True, 'import numpy as np\n'), ((5880, 5922), 'numpy.full', 'np.full', (['self.new_shape', '(0)'], {'dtype': 'np.int16'}), '(self.new_shape, 0, dtype=np.int16)\n', (5887, 5922), True, 'import numpy as np\n'), ((7131, 7178), 'numpy.full_like', 'np.full_like', (['self.synthesis_perturbed_img', '(256)'], {}), '(self.synthesis_perturbed_img, 256)\n', (7143, 7178), True, 'import numpy as np\n'), ((7301, 7346), 'numpy.zeros_like', 'np.zeros_like', (['self.synthesis_perturbed_label'], {}), '(self.synthesis_perturbed_label)\n', (7314, 7346), True, 'import numpy as np\n'), ((15100, 15212), 'numpy.float32', 'np.float32', (['[[x_min_per, y_min_per], [x_max_per, y_min_per], [x_min_per, y_max_per], [\n x_max_per, y_max_per]]'], {}), '([[x_min_per, y_min_per], [x_max_per, y_min_per], [x_min_per,\n y_max_per], [x_max_per, y_max_per]])\n', (15110, 15212), True, 'import numpy as np\n'), ((19566, 19605), 'cv2.getPerspectiveTransform', 'cv2.getPerspectiveTransform', (['pts1', 'pts2'], {}), '(pts1, pts2)\n', (19593, 19605), False, 'import cv2\n'), ((19614, 19680), 'numpy.ones', 'np.ones', (['(self.new_shape[0], self.new_shape[1], 1)'], {'dtype': 'np.int16'}), '((self.new_shape[0], self.new_shape[1], 1), dtype=np.int16)\n', (19621, 19680), True, 'import numpy as np\n'), ((19690, 19722), 'numpy.dstack', 'np.dstack', (['(pixel_position, one)'], {}), '((pixel_position, one))\n', (19699, 19722), True, 'import numpy as np\n'), ((19897, 19914), 'numpy.dstack', 'np.dstack', (['(x, y)'], {}), '((x, y))\n', (19906, 19914), True, 'import numpy as np\n'), ((20739, 20786), 'cv2.GaussianBlur', 'cv2.GaussianBlur', (['self.perturbed_xy_', '(7, 7)', '(0)'], {}), '(self.perturbed_xy_, (7, 7), 0)\n', (20755, 20786), False, 'import cv2\n'), ((21617, 21641), 'numpy.zeros', 'np.zeros', (['self.new_shape'], {}), '(self.new_shape)\n', (21625, 21641), True, 'import numpy as np\n'), ((37471, 37532), 'numpy.zeros_like', 'np.zeros_like', (['self.synthesis_perturbed_img'], {'dtype': 'np.float32'}), '(self.synthesis_perturbed_img, dtype=np.float32)\n', (37484, 37532), True, 'import numpy as np\n'), ((37964, 38025), 'numpy.concatenate', 'np.concatenate', (['(self.synthesis_perturbed_img, label)'], {'axis': '(2)'}), '((self.synthesis_perturbed_img, label), axis=2)\n', (37978, 38025), True, 'import numpy as np\n'), ((38062, 38120), 'numpy.zeros_like', 'np.zeros_like', (['synthesis_perturbed_color'], {'dtype': 'np.float32'}), '(synthesis_perturbed_color, dtype=np.float32)\n', (38075, 38120), True, 'import numpy as np\n'), ((40466, 40597), 'cv2.imwrite', 'cv2.imwrite', (["(self.save_path + 'fiducial_points/' + perfix_ + '_' + fold_curve + '.png')", 'fiducial_points_synthesis_perturbed_img'], {}), "(self.save_path + 'fiducial_points/' + perfix_ + '_' +\n fold_curve + '.png', fiducial_points_synthesis_perturbed_img)\n", (40477, 40597), False, 'import cv2\n'), ((40597, 40717), 'cv2.imwrite', 'cv2.imwrite', (["(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png')", 'self.synthesis_perturbed_color[:, :, :3]'], {}), "(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png',\n self.synthesis_perturbed_color[:, :, :3])\n", (40608, 40717), False, 'import cv2\n'), ((40762, 40833), 'numpy.full', 'np.full', (['(save_img_shape[0], save_img_shape[1], 2)', '(0)'], {'dtype': 'np.float32'}), '((save_img_shape[0], save_img_shape[1], 2), 0, dtype=np.float32)\n', (40769, 40833), True, 'import numpy as np\n'), ((40854, 40925), 'numpy.full', 'np.full', (['(save_img_shape[0], save_img_shape[1], 2)', '(0)'], {'dtype': 'np.float32'}), '((save_img_shape[0], save_img_shape[1], 2), 0, dtype=np.float32)\n', (40861, 40925), True, 'import numpy as np\n'), ((42195, 42266), 'numpy.full', 'np.full', (['(save_img_shape[0], save_img_shape[1], 3)', '(0)'], {'dtype': 'np.float32'}), '((save_img_shape[0], save_img_shape[1], 3), 0, dtype=np.float32)\n', (42202, 42266), True, 'import numpy as np\n'), ((42845, 42965), 'cv2.imwrite', 'cv2.imwrite', (["(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png')", 'self.synthesis_perturbed_color[:, :, :3]'], {}), "(self.save_path + 'png/' + perfix_ + '_' + fold_curve + '.png',\n self.synthesis_perturbed_color[:, :, :3])\n", (42856, 42965), False, 'import cv2\n'), ((44617, 44653), 'os.path.exists', 'os.path.exists', (["(save_path + 'color/')"], {}), "(save_path + 'color/')\n", (44631, 44653), False, 'import sys, os\n'), ((44657, 44690), 'os.makedirs', 'os.makedirs', (["(save_path + 'color/')"], {}), "(save_path + 'color/')\n", (44668, 44690), False, 'import sys, os\n'), ((44700, 44746), 'os.path.exists', 'os.path.exists', (["(save_path + 'fiducial_points/')"], {}), "(save_path + 'fiducial_points/')\n", (44714, 44746), False, 'import sys, os\n'), ((44750, 44793), 'os.makedirs', 'os.makedirs', (["(save_path + 'fiducial_points/')"], {}), "(save_path + 'fiducial_points/')\n", (44761, 44793), False, 'import sys, os\n'), ((44803, 44837), 'os.path.exists', 'os.path.exists', (["(save_path + 'png/')"], {}), "(save_path + 'png/')\n", (44817, 44837), False, 'import sys, os\n'), ((44841, 44872), 'os.makedirs', 'os.makedirs', (["(save_path + 'png/')"], {}), "(save_path + 'png/')\n", (44852, 44872), False, 'import sys, os\n'), ((44882, 44917), 'os.path.exists', 'os.path.exists', (["(save_path + 'scan/')"], {}), "(save_path + 'scan/')\n", (44896, 44917), False, 'import sys, os\n'), ((44921, 44953), 'os.makedirs', 'os.makedirs', (["(save_path + 'scan/')"], {}), "(save_path + 'scan/')\n", (44932, 44953), False, 'import sys, os\n'), ((44963, 45001), 'os.path.exists', 'os.path.exists', (["(save_path + 'outputs/')"], {}), "(save_path + 'outputs/')\n", (44977, 45001), False, 'import sys, os\n'), ((45005, 45040), 'os.makedirs', 'os.makedirs', (["(save_path + 'outputs/')"], {}), "(save_path + 'outputs/')\n", (45016, 45040), False, 'import sys, os\n'), ((3806, 3834), 'random.choice', 'random.choice', (['perturbed_bg_'], {}), '(perturbed_bg_)\n', (3819, 3834), False, 'import random\n'), ((8306, 8335), 'random.choice', 'random.choice', (['linspace_x_seq'], {}), '(linspace_x_seq)\n', (8319, 8335), False, 'import random\n'), ((8345, 8374), 'random.choice', 'random.choice', (['linspace_y_seq'], {}), '(linspace_y_seq)\n', (8358, 8374), False, 'import random\n'), ((8686, 8715), 'random.choice', 'random.choice', (['linspace_x_seq'], {}), '(linspace_x_seq)\n', (8699, 8715), False, 'import random\n'), ((8725, 8754), 'random.choice', 'random.choice', (['linspace_y_seq'], {}), '(linspace_y_seq)\n', (8738, 8754), False, 'import random\n'), ((9806, 9834), 'numpy.linalg.norm', 'np.linalg.norm', (['perturbed_vp'], {}), '(perturbed_vp)\n', (9820, 9834), True, 'import numpy as np\n'), ((14863, 14885), 'random.randint', 'random.randint', (['(26)', '(36)'], {}), '(26, 36)\n', (14877, 14885), False, 'import random\n'), ((25710, 25757), 'numpy.full_like', 'np.full_like', (['self.synthesis_perturbed_img', '(256)'], {}), '(self.synthesis_perturbed_img, 256)\n', (25722, 25757), True, 'import numpy as np\n'), ((25794, 25839), 'numpy.zeros_like', 'np.zeros_like', (['self.synthesis_perturbed_label'], {}), '(self.synthesis_perturbed_label)\n', (25807, 25839), True, 'import numpy as np\n'), ((25873, 25915), 'numpy.zeros_like', 'np.zeros_like', (['self.foreORbackground_label'], {}), '(self.foreORbackground_label)\n', (25886, 25915), True, 'import numpy as np\n'), ((42805, 42837), 'numpy.array', 'np.array', (['(segment_x, segment_y)'], {}), '((segment_x, segment_y))\n', (42813, 42837), True, 'import numpy as np\n'), ((43067, 43105), 'pickle.dumps', 'pickle.dumps', (['synthesis_perturbed_data'], {}), '(synthesis_perturbed_data)\n', (43079, 43105), False, 'import pickle\n'), ((43559, 43570), 'time.time', 'time.time', ([], {}), '()\n', (43568, 43570), False, 'import time\n'), ((7001, 7024), 'random.randint', 'random.randint', (['(80)', '(160)'], {}), '(80, 160)\n', (7015, 7024), False, 'import random\n'), ((9876, 9926), 'numpy.dot', 'np.dot', (['(perturbed_p - pixel_position)', 'perturbed_vp'], {}), '(perturbed_p - pixel_position, perturbed_vp)\n', (9882, 9926), True, 'import numpy as np\n'), ((18770, 18803), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[1])'], {}), '(pts2[0] - pts2[1])\n', (18784, 18803), True, 'import numpy as np\n'), ((18812, 18845), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[2])'], {}), '(pts2[0] - pts2[2])\n', (18826, 18845), True, 'import numpy as np\n'), ((18854, 18887), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[1] - pts2[3])'], {}), '(pts2[1] - pts2[3])\n', (18868, 18887), True, 'import numpy as np\n'), ((18896, 18929), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[2] - pts2[3])'], {}), '(pts2[2] - pts2[3])\n', (18910, 18929), True, 'import numpy as np\n'), ((21010, 21021), 'numpy.abs', 'np.abs', (['wts'], {}), '(wts)\n', (21016, 21021), True, 'import numpy as np\n'), ((37404, 37443), 'numpy.around', 'np.around', (['self.synthesis_perturbed_img'], {}), '(self.synthesis_perturbed_img)\n', (37413, 37443), True, 'import numpy as np\n'), ((39589, 39648), 'cv2.GaussianBlur', 'cv2.GaussianBlur', (['synthesis_perturbed_img_filter', '(5, 5)', '(0)'], {}), '(synthesis_perturbed_img_filter, (5, 5), 0)\n', (39605, 39648), False, 'import cv2\n'), ((39695, 39754), 'cv2.GaussianBlur', 'cv2.GaussianBlur', (['synthesis_perturbed_img_filter', '(3, 3)', '(0)'], {}), '(synthesis_perturbed_img_filter, (3, 3), 0)\n', (39711, 39754), False, 'import cv2\n'), ((41087, 41128), 'numpy.zeros', 'np.zeros', (['save_img_shape'], {'dtype': 'np.uint32'}), '(save_img_shape, dtype=np.uint32)\n', (41095, 41128), True, 'import numpy as np\n'), ((41213, 41224), 'numpy.abs', 'np.abs', (['wts'], {}), '(wts)\n', (41219, 41224), True, 'import numpy as np\n'), ((43982, 44005), 'numpy.random.normal', 'np.random.normal', (['(10)', '(3)'], {}), '(10, 3)\n', (43998, 44005), True, 'import numpy as np\n'), ((6680, 6703), 'random.randint', 'random.randint', (['(80)', '(120)'], {}), '(80, 120)\n', (6694, 6703), False, 'import random\n'), ((16076, 16109), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[1])'], {}), '(pts2[0] - pts2[1])\n', (16090, 16109), True, 'import numpy as np\n'), ((16119, 16152), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[2])'], {}), '(pts2[0] - pts2[2])\n', (16133, 16152), True, 'import numpy as np\n'), ((16162, 16195), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[1] - pts2[3])'], {}), '(pts2[1] - pts2[3])\n', (16176, 16195), True, 'import numpy as np\n'), ((16205, 16238), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[2] - pts2[3])'], {}), '(pts2[2] - pts2[3])\n', (16219, 16238), True, 'import numpy as np\n'), ((17428, 17461), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[1])'], {}), '(pts2[0] - pts2[1])\n', (17442, 17461), True, 'import numpy as np\n'), ((17471, 17504), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[0] - pts2[2])'], {}), '(pts2[0] - pts2[2])\n', (17485, 17504), True, 'import numpy as np\n'), ((17514, 17547), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[1] - pts2[3])'], {}), '(pts2[1] - pts2[3])\n', (17528, 17547), True, 'import numpy as np\n'), ((17557, 17590), 'numpy.linalg.norm', 'np.linalg.norm', (['(pts2[2] - pts2[3])'], {}), '(pts2[2] - pts2[3])\n', (17571, 17590), True, 'import numpy as np\n'), ((22812, 22856), 'numpy.sum', 'np.sum', (['self.synthesis_perturbed_img[(x), :]'], {}), '(self.synthesis_perturbed_img[(x), :])\n', (22818, 22856), True, 'import numpy as np\n'), ((23014, 23058), 'numpy.sum', 'np.sum', (['self.synthesis_perturbed_img[(x), :]'], {}), '(self.synthesis_perturbed_img[(x), :])\n', (23020, 23058), True, 'import numpy as np\n'), ((23194, 23238), 'numpy.sum', 'np.sum', (['self.synthesis_perturbed_img[:, (y)]'], {}), '(self.synthesis_perturbed_img[:, (y)])\n', (23200, 23238), True, 'import numpy as np\n'), ((23396, 23440), 'numpy.sum', 'np.sum', (['self.synthesis_perturbed_img[:, (y)]'], {}), '(self.synthesis_perturbed_img[:, (y)])\n', (23402, 23440), True, 'import numpy as np\n'), ((45387, 45416), 'random.choice', 'random.choice', (['all_bgImg_path'], {}), '(all_bgImg_path)\n', (45400, 45416), False, 'import random\n'), ((4451, 4488), 'numpy.zeros', 'np.zeros', (['mesh_shape'], {'dtype': 'np.uint32'}), '(mesh_shape, dtype=np.uint32)\n', (4459, 4488), True, 'import numpy as np\n'), ((4567, 4608), 'numpy.zeros', 'np.zeros', (['self.new_shape'], {'dtype': 'np.uint32'}), '(self.new_shape, dtype=np.uint32)\n', (4575, 4608), True, 'import numpy as np\n'), ((6791, 6813), 'random.randint', 'random.randint', (['(50)', '(70)'], {}), '(50, 70)\n', (6805, 6813), False, 'import random\n'), ((6853, 6876), 'random.randint', 'random.randint', (['(70)', '(130)'], {}), '(70, 130)\n', (6867, 6876), False, 'import random\n'), ((7903, 7926), 'random.randint', 'random.randint', (['(80)', '(160)'], {}), '(80, 160)\n', (7917, 7926), False, 'import random\n'), ((8407, 8469), 'random.randint', 'random.randint', (['(linspace_x[r_x - 1] * 10)', '(linspace_x[r_x] * 10)'], {}), '(linspace_x[r_x - 1] * 10, linspace_x[r_x] * 10)\n', (8421, 8469), False, 'import random\n'), ((8474, 8536), 'random.randint', 'random.randint', (['(linspace_y[r_y - 1] * 10)', '(linspace_y[r_y] * 10)'], {}), '(linspace_y[r_y - 1] * 10, linspace_y[r_y] * 10)\n', (8488, 8536), False, 'import random\n'), ((8788, 8850), 'random.randint', 'random.randint', (['(linspace_x[r_x - 1] * 10)', '(linspace_x[r_x] * 10)'], {}), '(linspace_x[r_x - 1] * 10, linspace_x[r_x] * 10)\n', (8802, 8850), False, 'import random\n'), ((8855, 8917), 'random.randint', 'random.randint', (['(linspace_y[r_y - 1] * 10)', '(linspace_y[r_y] * 10)'], {}), '(linspace_y[r_y - 1] * 10, linspace_y[r_y] * 10)\n', (8869, 8917), False, 'import random\n'), ((12917, 12995), 'numpy.array', 'np.array', (['[omega_perturbed * perturbed_v[0], omega_perturbed * perturbed_v[1]]'], {}), '([omega_perturbed * perturbed_v[0], omega_perturbed * perturbed_v[1]])\n', (12925, 12995), True, 'import numpy as np\n'), ((20552, 20593), 'numpy.zeros', 'np.zeros', (['self.new_shape'], {'dtype': 'np.uint32'}), '(self.new_shape, dtype=np.uint32)\n', (20560, 20593), True, 'import numpy as np\n'), ((30537, 30578), 'numpy.zeros', 'np.zeros', (['self.new_shape'], {'dtype': 'np.uint32'}), '(self.new_shape, dtype=np.uint32)\n', (30545, 30578), True, 'import numpy as np\n'), ((40386, 40409), 'math.ceil', 'math.ceil', (['(stepSize / 2)'], {}), '(stepSize / 2)\n', (40395, 40409), False, 'import math\n'), ((40418, 40441), 'math.ceil', 'math.ceil', (['(stepSize / 2)'], {}), '(stepSize / 2)\n', (40427, 40441), False, 'import math\n'), ((7657, 7680), 'random.randint', 'random.randint', (['(80)', '(120)'], {}), '(80, 120)\n', (7671, 7680), False, 'import random\n'), ((10375, 10404), 'random.randint', 'random.randint', (['(-10000)', '(10000)'], {}), '(-10000, 10000)\n', (10389, 10404), False, 'import random\n'), ((10412, 10441), 'random.randint', 'random.randint', (['(-10000)', '(10000)'], {}), '(-10000, 10000)\n', (10426, 10441), False, 'import random\n'), ((10798, 10825), 'random.randint', 'random.randint', (['(-8000)', '(8000)'], {}), '(-8000, 8000)\n', (10812, 10825), False, 'import random\n'), ((10833, 10860), 'random.randint', 'random.randint', (['(-8000)', '(8000)'], {}), '(-8000, 8000)\n', (10847, 10860), False, 'import random\n'), ((11303, 11345), 'numpy.abs', 'np.abs', (['perturbed_distance_vertex_and_line'], {}), '(perturbed_distance_vertex_and_line)\n', (11309, 11345), True, 'import numpy as np\n'), ((11347, 11367), 'random.randint', 'random.randint', (['(1)', '(2)'], {}), '(1, 2)\n', (11361, 11367), False, 'import random\n'), ((11444, 11486), 'numpy.abs', 'np.abs', (['perturbed_distance_vertex_and_line'], {}), '(perturbed_distance_vertex_and_line)\n', (11450, 11486), True, 'import numpy as np\n'), ((11795, 11837), 'numpy.abs', 'np.abs', (['perturbed_distance_vertex_and_line'], {}), '(perturbed_distance_vertex_and_line)\n', (11801, 11837), True, 'import numpy as np\n'), ((11839, 11859), 'random.randint', 'random.randint', (['(1)', '(2)'], {}), '(1, 2)\n', (11853, 11859), False, 'import random\n'), ((11936, 11978), 'numpy.abs', 'np.abs', (['perturbed_distance_vertex_and_line'], {}), '(perturbed_distance_vertex_and_line)\n', (11942, 11978), True, 'import numpy as np\n'), ((38249, 38264), 'random.random', 'random.random', ([], {}), '()\n', (38262, 38264), False, 'import random\n'), ((38403, 38418), 'random.random', 'random.random', ([], {}), '()\n', (38416, 38418), False, 'import random\n'), ((7774, 7796), 'random.randint', 'random.randint', (['(50)', '(70)'], {}), '(50, 70)\n', (7788, 7796), False, 'import random\n'), ((7840, 7863), 'random.randint', 'random.randint', (['(70)', '(130)'], {}), '(70, 130)\n', (7854, 7863), False, 'import random\n'), ((45633, 45656), 'numpy.random.normal', 'np.random.normal', (['(12)', '(4)'], {}), '(12, 4)\n', (45649, 45656), True, 'import numpy as np\n'), ((45967, 45989), 'numpy.random.normal', 'np.random.normal', (['(8)', '(4)'], {}), '(8, 4)\n', (45983, 45989), True, 'import numpy as np\n'), ((15594, 15609), 'random.random', 'random.random', ([], {}), '()\n', (15607, 15609), False, 'import random\n'), ((15649, 15664), 'random.random', 'random.random', ([], {}), '()\n', (15662, 15664), False, 'import random\n'), ((15715, 15730), 'random.random', 'random.random', ([], {}), '()\n', (15728, 15730), False, 'import random\n'), ((15770, 15785), 'random.random', 'random.random', ([], {}), '()\n', (15783, 15785), False, 'import random\n'), ((15836, 15851), 'random.random', 'random.random', ([], {}), '()\n', (15849, 15851), False, 'import random\n'), ((15891, 15906), 'random.random', 'random.random', ([], {}), '()\n', (15904, 15906), False, 'import random\n'), ((15957, 15972), 'random.random', 'random.random', ([], {}), '()\n', (15970, 15972), False, 'import random\n'), ((16012, 16027), 'random.random', 'random.random', ([], {}), '()\n', (16025, 16027), False, 'import random\n'), ((16954, 16969), 'random.random', 'random.random', ([], {}), '()\n', (16967, 16969), False, 'import random\n'), ((17009, 17024), 'random.random', 'random.random', ([], {}), '()\n', (17022, 17024), False, 'import random\n'), ((17075, 17090), 'random.random', 'random.random', ([], {}), '()\n', (17088, 17090), False, 'import random\n'), ((17130, 17145), 'random.random', 'random.random', ([], {}), '()\n', (17143, 17145), False, 'import random\n'), ((17196, 17211), 'random.random', 'random.random', ([], {}), '()\n', (17209, 17211), False, 'import random\n'), ((17251, 17266), 'random.random', 'random.random', ([], {}), '()\n', (17264, 17266), False, 'import random\n'), ((17317, 17332), 'random.random', 'random.random', ([], {}), '()\n', (17330, 17332), False, 'import random\n'), ((17372, 17387), 'random.random', 'random.random', ([], {}), '()\n', (17385, 17387), False, 'import random\n'), ((18295, 18310), 'random.random', 'random.random', ([], {}), '()\n', (18308, 18310), False, 'import random\n'), ((18350, 18365), 'random.random', 'random.random', ([], {}), '()\n', (18363, 18365), False, 'import random\n'), ((18416, 18431), 'random.random', 'random.random', ([], {}), '()\n', (18429, 18431), False, 'import random\n'), ((18471, 18486), 'random.random', 'random.random', ([], {}), '()\n', (18484, 18486), False, 'import random\n'), ((18537, 18552), 'random.random', 'random.random', ([], {}), '()\n', (18550, 18552), False, 'import random\n'), ((18592, 18607), 'random.random', 'random.random', ([], {}), '()\n', (18605, 18607), False, 'import random\n'), ((18658, 18673), 'random.random', 'random.random', ([], {}), '()\n', (18671, 18673), False, 'import random\n'), ((18713, 18728), 'random.random', 'random.random', ([], {}), '()\n', (18726, 18728), False, 'import random\n'), ((12684, 12716), 'numpy.linalg.norm', 'np.linalg.norm', (['(perturbed_v // 2)'], {}), '(perturbed_v // 2)\n', (12698, 12716), True, 'import numpy as np\n'), ((12730, 12745), 'random.random', 'random.random', ([], {}), '()\n', (12743, 12745), False, 'import random\n'), ((12755, 12770), 'random.random', 'random.random', ([], {}), '()\n', (12768, 12770), False, 'import random\n'), ((12780, 12795), 'random.random', 'random.random', ([], {}), '()\n', (12793, 12795), False, 'import random\n')] |
tw-ddis/Gnip-Tweet-Evaluation | tweet_evaluator.py | c5c847698bd6deb891870e5cf2514dfe78caa1c2 | #!/usr/bin/env python
import argparse
import logging
try:
import ujson as json
except ImportError:
import json
import sys
import datetime
import os
import importlib
from gnip_tweet_evaluation import analysis,output
"""
Perform audience and/or conversation analysis on a set of Tweets.
"""
logger = logging.getLogger('analysis')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-n","--identifier",dest="unique_identifier", default='0',type=str,
help="a unique name to identify the conversation/audience; default is '%(default)s'")
parser.add_argument("-c","--do-conversation-analysis",dest="do_conversation_analysis",action="store_true",default=False,
help="do conversation analysis on Tweets")
parser.add_argument("-a","--do-audience-analysis",dest="do_audience_analysis",action="store_true",default=False,
help="do audience analysis on users")
parser.add_argument("-i","--input-file-name",dest="input_file_name",default=None,
help="file containing Tweet data; take input from stdin if not present")
parser.add_argument('-o','--output-dir',dest='output_directory',default=os.environ['HOME'] + '/tweet_evaluation/',
help='directory for output files; default is %(default)s')
parser.add_argument('-b','--baseline-input-file',dest='baseline_input_name',default=None,
help='Tweets against which to run a relative analysis')
args = parser.parse_args()
# get the time right now, to use in output naming
time_now = datetime.datetime.now()
output_directory = '{0}/{1:04d}/{2:02d}/{3:02d}/'.format(args.output_directory.rstrip('/')
,time_now.year
,time_now.month
,time_now.day
)
# get the empty results object, which defines the measurements to be run
results = analysis.setup_analysis(do_conversation = args.do_conversation_analysis, do_audience = args.do_audience_analysis)
baseline_results = None
if args.baseline_input_name is not None:
baseline_results = analysis.setup_analysis(do_conversation = args.do_conversation_analysis, do_audience = args.do_audience_analysis)
# manage input sources, file opening, and deserialization
if args.input_file_name is not None:
tweet_generator = analysis.deserialize_tweets(open(args.input_file_name))
else:
tweet_generator = analysis.deserialize_tweets(sys.stdin)
# run analysis
analysis.analyze_tweets(tweet_generator, results)
# run baseline analysis, if requests
if baseline_results is not None:
baseline_tweet_generator = analysis.deserialize_tweets(open(args.baseline_input_name))
analysis.analyze_tweets(baseline_tweet_generator, baseline_results)
results = analysis.compare_results(results,baseline_results)
# dump the output
output.dump_results(results, output_directory, args.unique_identifier)
| [((311, 340), 'logging.getLogger', 'logging.getLogger', (['"""analysis"""'], {}), "('analysis')\n", (328, 340), False, 'import logging\n'), ((390, 413), 'logging.StreamHandler', 'logging.StreamHandler', ([], {}), '()\n', (411, 413), False, 'import logging\n'), ((461, 486), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (484, 486), False, 'import argparse\n'), ((1650, 1673), 'datetime.datetime.now', 'datetime.datetime.now', ([], {}), '()\n', (1671, 1673), False, 'import datetime\n'), ((1955, 2068), 'gnip_tweet_evaluation.analysis.setup_analysis', 'analysis.setup_analysis', ([], {'do_conversation': 'args.do_conversation_analysis', 'do_audience': 'args.do_audience_analysis'}), '(do_conversation=args.do_conversation_analysis,\n do_audience=args.do_audience_analysis)\n', (1978, 2068), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2570, 2619), 'gnip_tweet_evaluation.analysis.analyze_tweets', 'analysis.analyze_tweets', (['tweet_generator', 'results'], {}), '(tweet_generator, results)\n', (2593, 2619), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2971, 3041), 'gnip_tweet_evaluation.output.dump_results', 'output.dump_results', (['results', 'output_directory', 'args.unique_identifier'], {}), '(results, output_directory, args.unique_identifier)\n', (2990, 3041), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2171, 2284), 'gnip_tweet_evaluation.analysis.setup_analysis', 'analysis.setup_analysis', ([], {'do_conversation': 'args.do_conversation_analysis', 'do_audience': 'args.do_audience_analysis'}), '(do_conversation=args.do_conversation_analysis,\n do_audience=args.do_audience_analysis)\n', (2194, 2284), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2507, 2545), 'gnip_tweet_evaluation.analysis.deserialize_tweets', 'analysis.deserialize_tweets', (['sys.stdin'], {}), '(sys.stdin)\n', (2534, 2545), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2803, 2870), 'gnip_tweet_evaluation.analysis.analyze_tweets', 'analysis.analyze_tweets', (['baseline_tweet_generator', 'baseline_results'], {}), '(baseline_tweet_generator, baseline_results)\n', (2826, 2870), False, 'from gnip_tweet_evaluation import analysis, output\n'), ((2889, 2940), 'gnip_tweet_evaluation.analysis.compare_results', 'analysis.compare_results', (['results', 'baseline_results'], {}), '(results, baseline_results)\n', (2913, 2940), False, 'from gnip_tweet_evaluation import analysis, output\n')] |
admiral-aokiji/whatsapp-bot | app.py | 5a0b0d4afddc679cda3670771934cb472629587a | from flask import Flask, request
import os
from twilio.twiml.messaging_response import MessagingResponse
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=chrome_options)
app = Flask(__name__)
import utils
@app.route("/")
def hello():
return "Hello World!"
@app.route('/bot', methods=['POST'])
def bot():
incoming_msg = request.values.get('Body', '')
print(incoming_msg)
resp = MessagingResponse()
msg = resp.message()
responded = False
if incoming_msg in ['Hi', 'Hey', 'Menu']:
text = f'Hello\n For any suggestions or requests 👇 \n 📞 : 9537701631 \n ✉ : rohit.saxena.met17@itbhu.ac.in \n\n Please enter one of the following option 👇 \n *TPC*. TPC portal willingness \n *B*. __________. '
msg.body(text)
responded = True
elif 'TPC' in incoming_msg:
if incoming_msg == 'TPC':
text = 'Menu of options for TPC command'
msg.body(text)
h = 7
responded = True
utils.portalLogin(os.environ.get('TPC_EMAIL'),os.environ.get('TPC_PWD'))
if incoming_msg == 'TPC -willingness -short' or incoming_msg == 'TPC -w -s':
utils.getWillingness()
utils.shortenWillingness()
elif incoming_msg == 'TPC -willingness -details' or incoming_msg == 'TPC -w -d':
utils.portalLogin(os.environ.get('TPC_EMAIL'),os.environ.get('TPC_PWD'))
utils.getWillingness()
elif incoming_msg == 'TPC -willingness -details' or incoming_msg == 'TPC -w -d':
utils.portalLogin(os.environ.get('TPC_EMAIL'),os.environ.get('TPC_PWD'))
utils.getWillingness()
elif incoming_msg[:15] == 'TPC -experience' or (incoming_msg[:7] == 'TPC - e ' and len(incoming_msg)>8):
utils.portalLogin(os.environ.get('TPC_EMAIL'),os.environ.get('TPC_PWD'))
companyName = incoming_msg.split(' ')[2]
print(companyName)
utils.getInterviewExperience(companyName)
else:
# send custom error msg for TPC commands
pass
else:
# Checking for formality
if responded == False:
msg.body('Please enter valid commands')
return str(resp)
if __name__ == "__main__":
app.run(host="localhost", port=5000, debug=True)
| [((153, 178), 'selenium.webdriver.ChromeOptions', 'webdriver.ChromeOptions', ([], {}), '()\n', (176, 178), False, 'from selenium import webdriver\n'), ((212, 247), 'os.environ.get', 'os.environ.get', (['"""GOOGLE_CHROME_BIN"""'], {}), "('GOOGLE_CHROME_BIN')\n", (226, 247), False, 'import os\n'), ((505, 520), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (510, 520), False, 'from flask import Flask, request\n'), ((658, 688), 'flask.request.values.get', 'request.values.get', (['"""Body"""', '""""""'], {}), "('Body', '')\n", (676, 688), False, 'from flask import Flask, request\n'), ((724, 743), 'twilio.twiml.messaging_response.MessagingResponse', 'MessagingResponse', ([], {}), '()\n', (741, 743), False, 'from twilio.twiml.messaging_response import MessagingResponse\n'), ((431, 466), 'os.environ.get', 'os.environ.get', (['"""CHROMEDRIVER_PATH"""'], {}), "('CHROMEDRIVER_PATH')\n", (445, 466), False, 'import os\n'), ((1338, 1365), 'os.environ.get', 'os.environ.get', (['"""TPC_EMAIL"""'], {}), "('TPC_EMAIL')\n", (1352, 1365), False, 'import os\n'), ((1366, 1391), 'os.environ.get', 'os.environ.get', (['"""TPC_PWD"""'], {}), "('TPC_PWD')\n", (1380, 1391), False, 'import os\n'), ((1490, 1512), 'utils.getWillingness', 'utils.getWillingness', ([], {}), '()\n', (1510, 1512), False, 'import utils\n'), ((1525, 1551), 'utils.shortenWillingness', 'utils.shortenWillingness', ([], {}), '()\n', (1549, 1551), False, 'import utils\n'), ((1738, 1760), 'utils.getWillingness', 'utils.getWillingness', ([], {}), '()\n', (1758, 1760), False, 'import utils\n'), ((1671, 1698), 'os.environ.get', 'os.environ.get', (['"""TPC_EMAIL"""'], {}), "('TPC_EMAIL')\n", (1685, 1698), False, 'import os\n'), ((1699, 1724), 'os.environ.get', 'os.environ.get', (['"""TPC_PWD"""'], {}), "('TPC_PWD')\n", (1713, 1724), False, 'import os\n'), ((1947, 1969), 'utils.getWillingness', 'utils.getWillingness', ([], {}), '()\n', (1967, 1969), False, 'import utils\n'), ((1880, 1907), 'os.environ.get', 'os.environ.get', (['"""TPC_EMAIL"""'], {}), "('TPC_EMAIL')\n", (1894, 1907), False, 'import os\n'), ((1908, 1933), 'os.environ.get', 'os.environ.get', (['"""TPC_PWD"""'], {}), "('TPC_PWD')\n", (1922, 1933), False, 'import os\n'), ((2264, 2305), 'utils.getInterviewExperience', 'utils.getInterviewExperience', (['companyName'], {}), '(companyName)\n', (2292, 2305), False, 'import utils\n'), ((2113, 2140), 'os.environ.get', 'os.environ.get', (['"""TPC_EMAIL"""'], {}), "('TPC_EMAIL')\n", (2127, 2140), False, 'import os\n'), ((2141, 2166), 'os.environ.get', 'os.environ.get', (['"""TPC_PWD"""'], {}), "('TPC_PWD')\n", (2155, 2166), False, 'import os\n')] |
hchaozhe/nflfastpy | nflfastpy/errors.py | 11e4894d7fee4ff8baac2c08b000a39308b41143 | """
Custom exceptions for nflfastpy module
"""
class SeasonNotFoundError(Exception):
pass | [] |
zeevikal/CS231n-spring2018 | assignment1/cs231n/classifiers/neural_net.py | 50691a947b877047099e7a1fe99a3fdea4a4fcf8 | from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
class TwoLayerNet(object):
"""
A two-layer fully-connected neural network. The net has an input dimension
of N, a hidden layer dimension of H, and performs classification over C
classes.
We train the network with a softmax loss function and L2 regularization on
the weight matrices. The network uses a ReLU nonlinearity after the first
fully connected layer.
In other words, the network has the following architecture:
input - fully connected layer - ReLU - fully connected layer - softmax
The outputs of the second fully-connected layer are the scores for each
class.
"""
def __init__(self, input_size, hidden_size, output_size, std=1e-4):
"""
Initialize the model. Weights are initialized to small random values
and biases are initialized to zero. Weights and biases are stored in
the variable self.params, which is a dictionary with the following keys
W1: First layer weights; has shape (D, H)
b1: First layer biases; has shape (H,)
W2: Second layer weights; has shape (H, C)
b2: Second layer biases; has shape (C,)
Inputs:
- input_size: The dimension D of the input data.
- hidden_size: The number of neurons H in the hidden layer.
- output_size: The number of classes C.
"""
self.params = {}
self.params['W1'] = std * np.random.randn(input_size, hidden_size)
self.params['b1'] = np.zeros(hidden_size)
self.params['W2'] = std * np.random.randn(hidden_size, output_size)
self.params['b2'] = np.zeros(output_size)
def loss(self, X, y=None, reg=0.0):
"""
Compute the loss and gradients for a two layer fully connected neural
network.
Inputs:
- X: Input data of shape (N, D). Each X[i] is a training sample.
- y: Vector of training labels. y[i] is the label for X[i], and each
y[i] is an integer in the range 0 <= y[i] < C. This parameter is
optional; if it is not passed then we only return scores, and if it
is passed then we instead return the loss and gradients.
- reg: Regularization strength.
Returns:
If y is None, return a matrix scores of shape (N, C) where scores[i, c]
is the score for class c on input X[i].
If y is not None, instead return a tuple of:
- loss: Loss (data loss and regularization loss) for this batch of
training samples.
- grads: Dictionary mapping parameter names to gradients of those
parameters with respect to the loss function; has the same keys as
self.params.
"""
# Unpack variables from the params dictionary
W1, b1 = self.params['W1'], self.params['b1']
W2, b2 = self.params['W2'], self.params['b2']
N, D = X.shape
# Compute the forward pass
scores = None
#######################################################################
# TODO: Perform the forward pass, computing the class scores for the #
# input. Store the result in the scores variable, which should be an #
# array of shape (N, C). #
#######################################################################
scores1 = X.dot(W1) + b1 # FC1
X2 = np.maximum(0, scores1) # ReLU FC1
scores = X2.dot(W2) + b2 # FC2
#######################################################################
# END OF YOUR CODE #
#######################################################################
# If the targets are not given then jump out, we're done
if y is None:
return scores
scores -= np.max(scores) # Fix Number instability
scores_exp = np.exp(scores)
probs = scores_exp / np.sum(scores_exp, axis=1, keepdims=True)
# Compute the loss
loss = None
#######################################################################
# TODO: Finish the forward pass, and compute the loss. This should #
# include both the data loss and L2 regularization for W1 and W2. #
# Store the result in the variable loss, which should be a scalar. Use#
# the Softmax classifier loss. #
#######################################################################
correct_probs = -np.log(probs[np.arange(N), y])
# L_i = -log(e^correct_score/sum(e^scores))) = -log(correct_probs)
loss = np.sum(correct_probs)
loss /= N
# L2 regularization WRT W1 and W2
loss += reg * (np.sum(W1 * W1) + np.sum(W2 * W2))
#######################################################################
# END OF YOUR CODE #
#######################################################################
# Backward pass: compute gradients
grads = {}
#############################################################################
# TODO: Compute the backward pass, computing the derivatives of the weights #
# and biases. Store the results in the grads dictionary. For example, #
# grads['W1'] should store the gradient on W1, and be a matrix of same size #
#############################################################################
# gradient of loss_i WRT scores_k
# dL_i/ds_k = probs_k-1(y_i == k)
# this means the gradient is the score for "other" classes and score-1
# for the target class
d_scores = probs.copy()
d_scores[np.arange(N), y] -= 1
d_scores /= N
# W2 were multiplied with X2, by chain rule and multiplication
# derivative, WRT W2 we need to multiply downstream derivative by X2
d_W2 = X2.T.dot(d_scores)
# b2 was added, so it's d is 1 but we must multiply it with chain rule
# (downstream), in this case d_scores
d_b2 = np.sum(d_scores, axis=0)
# W1 is upstream of X2, so we continue this way
d_X2 = d_scores.dot(W2.T)
# ReLU derivative is 1 for > 0, else 0
d_scores1 = d_X2 * (scores1 > 0)
d_W1 = X.T.dot(d_scores1)
# b1 gradient
d_b1 = d_scores1.sum(axis=0)
# regularization gradient (reg*W2^2)
d_W2 += reg * 2 * W2
d_W1 += reg * 2 * W1
grads['W1'] = d_W1
grads['b1'] = d_b1
grads['W2'] = d_W2
grads['b2'] = d_b2
#######################################################################
# END OF YOUR CODE #
#######################################################################
return loss, grads
def train(self, X, y, X_val, y_val,
learning_rate=1e-3, learning_rate_decay=0.95,
reg=5e-6, num_iters=100,
batch_size=200, verbose=False):
"""
Train this neural network using stochastic gradient descent.
Inputs:
- X: A numpy array of shape (N, D) giving training data.
- y: A numpy array f shape (N,) giving training labels; y[i] = c means
that X[i] has label c, where 0 <= c < C.
- X_val: A numpy array of shape (N_val, D) giving validation data.
- y_val: A numpy array of shape (N_val,) giving validation labels.
- learning_rate: Scalar giving learning rate for optimization.
- learning_rate_decay: Scalar giving factor used to decay the learning
rate after each epoch.
- reg: Scalar giving regularization strength.
- num_iters: Number of steps to take when optimizing.
- batch_size: Number of training examples to use per step.
- verbose: boolean; if true print progress during optimization.
"""
num_train = X.shape[0]
iterations_per_epoch = max(num_train / batch_size, 1)
# Use SGD to optimize the parameters in self.model
loss_history = []
train_acc_history = []
val_acc_history = []
for it in range(num_iters):
X_batch = None
y_batch = None
###################################################################
# TODO: Create a random minibatch of training data and labels, #
# storing them in X_batch and y_batch respectively. #
###################################################################
# random indexes to sample training data/labels
sample_idx = np.random.choice(num_train, batch_size, replace=True)
X_batch = X[sample_idx]
y_batch = y[sample_idx]
###################################################################
# END OF YOUR CODE #
###################################################################
# Compute loss and gradients using the current minibatch
loss, grads = self.loss(X_batch, y=y_batch, reg=reg)
loss_history.append(loss)
###################################################################
# TODO: Use the gradients in the grads dictionary to update the #
# parameters of the network (stored in the dictionary self.params)#
# using stochastic gradient descent. You'll need to use the #
# gradients stored in the grads dictionary defined above. #
###################################################################
# For each weight in network parameters, update it with the
# corresponding calculated gradient
for key in self.params:
self.params[key] -= learning_rate * grads[key]
###################################################################
# END OF YOUR CODE #
###################################################################
if verbose and it % 100 == 0:
print('iteration %d / %d: loss %f' % (it, num_iters, loss))
# Every epoch, check train and val accuracy and decay learning rate
if it % iterations_per_epoch == 0:
# Check accuracy
train_acc = (self.predict(X_batch) == y_batch).mean()
val_acc = (self.predict(X_val) == y_val).mean()
train_acc_history.append(train_acc)
val_acc_history.append(val_acc)
# Decay learning rate
learning_rate *= learning_rate_decay
return {
'loss_history': loss_history,
'train_acc_history': train_acc_history,
'val_acc_history': val_acc_history,
}
def predict(self, X):
"""
Use the trained weights of this two-layer network to predict labels for
data points. For each data point we predict scores for each of the C
classes, and assign each data point to the class with the highest score
Inputs:
- X: A numpy array of shape (N, D) giving N D-dimensional data points
to classify.
Returns:
- y_pred: A numpy array of shape (N,) giving predicted labels for each
of the elements of X. For all i, y_pred[i] = c means that X[i] is
predicted to have class c, where 0 <= c < C.
"""
y_pred = None
#######################################################################
# TODO: Implement this function; it should be VERY simple! #
#######################################################################
y_pred = np.argmax(self.loss(X), axis=1)
#######################################################################
# END OF YOUR CODE #
#######################################################################
return y_pred | [((1558, 1579), 'numpy.zeros', 'np.zeros', (['hidden_size'], {}), '(hidden_size)\n', (1566, 1579), True, 'import numpy as np\n'), ((1684, 1705), 'numpy.zeros', 'np.zeros', (['output_size'], {}), '(output_size)\n', (1692, 1705), True, 'import numpy as np\n'), ((3464, 3486), 'numpy.maximum', 'np.maximum', (['(0)', 'scores1'], {}), '(0, scores1)\n', (3474, 3486), True, 'import numpy as np\n'), ((3914, 3928), 'numpy.max', 'np.max', (['scores'], {}), '(scores)\n', (3920, 3928), True, 'import numpy as np\n'), ((3976, 3990), 'numpy.exp', 'np.exp', (['scores'], {}), '(scores)\n', (3982, 3990), True, 'import numpy as np\n'), ((4746, 4767), 'numpy.sum', 'np.sum', (['correct_probs'], {}), '(correct_probs)\n', (4752, 4767), True, 'import numpy as np\n'), ((6231, 6255), 'numpy.sum', 'np.sum', (['d_scores'], {'axis': '(0)'}), '(d_scores, axis=0)\n', (6237, 6255), True, 'import numpy as np\n'), ((1489, 1529), 'numpy.random.randn', 'np.random.randn', (['input_size', 'hidden_size'], {}), '(input_size, hidden_size)\n', (1504, 1529), True, 'import numpy as np\n'), ((1614, 1655), 'numpy.random.randn', 'np.random.randn', (['hidden_size', 'output_size'], {}), '(hidden_size, output_size)\n', (1629, 1655), True, 'import numpy as np\n'), ((4020, 4061), 'numpy.sum', 'np.sum', (['scores_exp'], {'axis': '(1)', 'keepdims': '(True)'}), '(scores_exp, axis=1, keepdims=True)\n', (4026, 4061), True, 'import numpy as np\n'), ((8825, 8878), 'numpy.random.choice', 'np.random.choice', (['num_train', 'batch_size'], {'replace': '(True)'}), '(num_train, batch_size, replace=True)\n', (8841, 8878), True, 'import numpy as np\n'), ((4851, 4866), 'numpy.sum', 'np.sum', (['(W1 * W1)'], {}), '(W1 * W1)\n', (4857, 4866), True, 'import numpy as np\n'), ((4869, 4884), 'numpy.sum', 'np.sum', (['(W2 * W2)'], {}), '(W2 * W2)\n', (4875, 4884), True, 'import numpy as np\n'), ((5864, 5876), 'numpy.arange', 'np.arange', (['N'], {}), '(N)\n', (5873, 5876), True, 'import numpy as np\n'), ((4637, 4649), 'numpy.arange', 'np.arange', (['N'], {}), '(N)\n', (4646, 4649), True, 'import numpy as np\n')] |
koralarts/django-dynamic-settings | dynamic_setting/tests/test_models.py | 8a3c5f44ad71f6d8fb78af9e7a3f5a380dd3d318 | from django.test import TestCase
from dynamic_setting.models import Setting
class SettingTestCase(TestCase):
def _create_setting(self, name, **kwargs):
return Setting.objects.create(name=name, **kwargs)
def test_create_setting(self):
""" Test Creating a new Setting. """
name = 'TEST_SETTING'
data = 'Setting Data'
setting = self._create_setting(name, data=data)
self.assertEqual(setting.name, name)
self.assertEqual(setting.__str__(), name)
self.assertEqual(setting.data, data)
def test_create_setting_no_data(self):
""" Test Creating a new setting without Data. """
name = 'TEST_SETTING'
data = '-'
setting = self._create_setting(name)
self.assertEqual(setting.name, name)
self.assertEqual(setting.__str__(), name)
self.assertEqual(setting.data, data)
def test_delete_setting(self):
""" Test Deleting a setting object. """
name = 'TEST_SETTING'
setting = self._create_setting(name)
setting_pk = setting.pk
setting.delete()
try:
Setting.objects.get(pk=setting_pk)
except Setting.DoesNotExist:
pass
else:
self.fail('Setting with ID {} should not exist.'.format(setting_pk))
def test_get_setting(self):
""" Test Getting a setting object. """
name = 'TEST_SETTING'
data = 'Setting data'
setting = self._create_setting(name, data=data)
try:
setting2 = Setting.objects.get(pk=setting.pk)
except Setting.DoesNotExist:
self.fail('Setting with ID {} should exist'.format(setting.pk))
self.assertEqual(setting.name, setting2.name)
self.assertEqual(setting.__str__(), setting2.__str__())
self.assertEqual(setting.data, setting2.data)
self.assertEqual(setting.pk, setting2.pk)
def test_update_setting(self):
""" Test Updating a setting object. """
name = 'TEST_SETTING'
data = 'Setting data'
data2 = 'New Setting Data'
setting = self._create_setting(name, data=data)
setting.data = data2
setting.save()
setting2 = Setting.objects.get(pk=setting.pk)
self.assertEqual(setting2.data, data2) | [((178, 221), 'dynamic_setting.models.Setting.objects.create', 'Setting.objects.create', ([], {'name': 'name'}), '(name=name, **kwargs)\n', (200, 221), False, 'from dynamic_setting.models import Setting\n'), ((2228, 2262), 'dynamic_setting.models.Setting.objects.get', 'Setting.objects.get', ([], {'pk': 'setting.pk'}), '(pk=setting.pk)\n', (2247, 2262), False, 'from dynamic_setting.models import Setting\n'), ((1136, 1170), 'dynamic_setting.models.Setting.objects.get', 'Setting.objects.get', ([], {'pk': 'setting_pk'}), '(pk=setting_pk)\n', (1155, 1170), False, 'from dynamic_setting.models import Setting\n'), ((1552, 1586), 'dynamic_setting.models.Setting.objects.get', 'Setting.objects.get', ([], {'pk': 'setting.pk'}), '(pk=setting.pk)\n', (1571, 1586), False, 'from dynamic_setting.models import Setting\n')] |
MrDelik/core | homeassistant/components/zamg/weather.py | 93a66cc357b226389967668441000498a10453bb | """Sensor for data from Austrian Zentralanstalt für Meteorologie."""
from __future__ import annotations
import logging
import voluptuous as vol
from homeassistant.components.weather import (
ATTR_WEATHER_HUMIDITY,
ATTR_WEATHER_PRESSURE,
ATTR_WEATHER_TEMPERATURE,
ATTR_WEATHER_WIND_BEARING,
ATTR_WEATHER_WIND_SPEED,
PLATFORM_SCHEMA,
WeatherEntity,
)
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
# Reuse data and API logic from the sensor implementation
from .sensor import (
ATTRIBUTION,
CONF_STATION_ID,
ZamgData,
closest_station,
zamg_stations,
)
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_STATION_ID): cv.string,
vol.Inclusive(
CONF_LATITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.latitude,
vol.Inclusive(
CONF_LONGITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.longitude,
}
)
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the ZAMG weather platform."""
name = config.get(CONF_NAME)
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
station_id = config.get(CONF_STATION_ID) or closest_station(
latitude, longitude, hass.config.config_dir
)
if station_id not in zamg_stations(hass.config.config_dir):
_LOGGER.error(
"Configured ZAMG %s (%s) is not a known station",
CONF_STATION_ID,
station_id,
)
return
probe = ZamgData(station_id=station_id)
try:
probe.update()
except (ValueError, TypeError) as err:
_LOGGER.error("Received error from ZAMG: %s", err)
return
add_entities([ZamgWeather(probe, name)], True)
class ZamgWeather(WeatherEntity):
"""Representation of a weather condition."""
def __init__(self, zamg_data, stationname=None):
"""Initialise the platform with a data instance and station name."""
self.zamg_data = zamg_data
self.stationname = stationname
@property
def name(self):
"""Return the name of the sensor."""
return (
self.stationname
or f"ZAMG {self.zamg_data.data.get('Name') or '(unknown station)'}"
)
@property
def condition(self):
"""Return the current condition."""
return None
@property
def attribution(self):
"""Return the attribution."""
return ATTRIBUTION
@property
def temperature(self):
"""Return the platform temperature."""
return self.zamg_data.get_data(ATTR_WEATHER_TEMPERATURE)
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property
def pressure(self):
"""Return the pressure."""
return self.zamg_data.get_data(ATTR_WEATHER_PRESSURE)
@property
def humidity(self):
"""Return the humidity."""
return self.zamg_data.get_data(ATTR_WEATHER_HUMIDITY)
@property
def wind_speed(self):
"""Return the wind speed."""
return self.zamg_data.get_data(ATTR_WEATHER_WIND_SPEED)
@property
def wind_bearing(self):
"""Return the wind bearing."""
return self.zamg_data.get_data(ATTR_WEATHER_WIND_BEARING)
def update(self):
"""Update current conditions."""
self.zamg_data.update()
| [((897, 924), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (914, 924), False, 'import logging\n'), ((982, 1005), 'voluptuous.Optional', 'vol.Optional', (['CONF_NAME'], {}), '(CONF_NAME)\n', (994, 1005), True, 'import voluptuous as vol\n'), ((1026, 1055), 'voluptuous.Optional', 'vol.Optional', (['CONF_STATION_ID'], {}), '(CONF_STATION_ID)\n', (1038, 1055), True, 'import voluptuous as vol\n'), ((1076, 1169), 'voluptuous.Inclusive', 'vol.Inclusive', (['CONF_LATITUDE', '"""coordinates"""', '"""Latitude and longitude must exist together"""'], {}), "(CONF_LATITUDE, 'coordinates',\n 'Latitude and longitude must exist together')\n", (1089, 1169), True, 'import voluptuous as vol\n'), ((1210, 1304), 'voluptuous.Inclusive', 'vol.Inclusive', (['CONF_LONGITUDE', '"""coordinates"""', '"""Latitude and longitude must exist together"""'], {}), "(CONF_LONGITUDE, 'coordinates',\n 'Latitude and longitude must exist together')\n", (1223, 1304), True, 'import voluptuous as vol\n')] |
martasls/rasa | rasa/model.py | 6e535a847f6be0c05e7b89208f16a53d2c478629 | import copy
import glob
import hashlib
import logging
import os
import shutil
from subprocess import CalledProcessError, DEVNULL, check_output # skipcq:BAN-B404
import tempfile
import typing
from pathlib import Path
from typing import Any, Text, Tuple, Union, Optional, List, Dict, NamedTuple
from packaging import version
from rasa.constants import MINIMUM_COMPATIBLE_VERSION
import rasa.shared.utils.io
import rasa.utils.io
from rasa.cli.utils import create_output_path
from rasa.shared.utils.cli import print_success
from rasa.shared.constants import (
CONFIG_KEYS_CORE,
CONFIG_KEYS_NLU,
CONFIG_KEYS,
DEFAULT_DOMAIN_PATH,
DEFAULT_MODELS_PATH,
DEFAULT_CORE_SUBDIRECTORY_NAME,
DEFAULT_NLU_SUBDIRECTORY_NAME,
)
from rasa.exceptions import ModelNotFound
from rasa.utils.common import TempDirectoryPath
if typing.TYPE_CHECKING:
from rasa.shared.importers.importer import TrainingDataImporter
logger = logging.getLogger(__name__)
# Type alias for the fingerprint
Fingerprint = Dict[Text, Union[Text, List[Text], int, float]]
FINGERPRINT_FILE_PATH = "fingerprint.json"
FINGERPRINT_CONFIG_KEY = "config"
FINGERPRINT_CONFIG_CORE_KEY = "core-config"
FINGERPRINT_CONFIG_NLU_KEY = "nlu-config"
FINGERPRINT_CONFIG_WITHOUT_EPOCHS_KEY = "config-without-epochs"
FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY = "domain"
FINGERPRINT_NLG_KEY = "nlg"
FINGERPRINT_RASA_VERSION_KEY = "version"
FINGERPRINT_STORIES_KEY = "stories"
FINGERPRINT_NLU_DATA_KEY = "messages"
FINGERPRINT_NLU_LABELS_KEY = "nlu_labels"
FINGERPRINT_PROJECT = "project"
FINGERPRINT_TRAINED_AT_KEY = "trained_at"
class Section(NamedTuple):
"""Specifies which fingerprint keys decide whether this sub-model is retrained."""
name: Text
relevant_keys: List[Text]
SECTION_CORE = Section(
name="Core model",
relevant_keys=[
FINGERPRINT_CONFIG_KEY,
FINGERPRINT_CONFIG_CORE_KEY,
FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY,
FINGERPRINT_STORIES_KEY,
FINGERPRINT_RASA_VERSION_KEY,
],
)
SECTION_NLU = Section(
name="NLU model",
relevant_keys=[
FINGERPRINT_CONFIG_KEY,
FINGERPRINT_CONFIG_NLU_KEY,
FINGERPRINT_NLU_DATA_KEY,
FINGERPRINT_RASA_VERSION_KEY,
],
)
SECTION_NLG = Section(name="NLG responses", relevant_keys=[FINGERPRINT_NLG_KEY])
class FingerprintComparisonResult:
"""Container for the results of a fingerprint comparison."""
def __init__(
self,
nlu: bool = True,
core: bool = True,
nlg: bool = True,
force_training: bool = False,
):
"""Creates a `FingerprintComparisonResult` instance.
Args:
nlu: `True` if the NLU model should be retrained.
core: `True` if the Core model should be retrained.
nlg: `True` if the responses in the domain should be updated.
force_training: `True` if a training of all parts is forced.
"""
self.nlu = nlu
self.core = core
self.nlg = nlg
self.force_training = force_training
def is_training_required(self) -> bool:
"""Check if anything has to be retrained."""
return any([self.nlg, self.nlu, self.core, self.force_training])
def should_retrain_core(self) -> bool:
"""Check if the Core model has to be updated."""
return self.force_training or self.core
def should_retrain_nlg(self) -> bool:
"""Check if the responses have to be updated."""
return self.should_retrain_core() or self.nlg
def should_retrain_nlu(self) -> bool:
"""Check if the NLU model has to be updated."""
return self.force_training or self.nlu
def get_model(model_path: Text = DEFAULT_MODELS_PATH) -> TempDirectoryPath:
"""Get a model and unpack it. Raises a `ModelNotFound` exception if
no model could be found at the provided path.
Args:
model_path: Path to the zipped model. If it's a directory, the latest
trained model is returned.
Returns:
Path to the unpacked model.
"""
if not model_path:
raise ModelNotFound("No path specified.")
elif not os.path.exists(model_path):
raise ModelNotFound(f"No file or directory at '{model_path}'.")
if os.path.isdir(model_path):
model_path = get_latest_model(model_path)
if not model_path:
raise ModelNotFound(
f"Could not find any Rasa model files in '{model_path}'."
)
elif not model_path.endswith(".tar.gz"):
raise ModelNotFound(f"Path '{model_path}' does not point to a Rasa model file.")
try:
model_relative_path = os.path.relpath(model_path)
except ValueError:
model_relative_path = model_path
logger.info(f"Loading model {model_relative_path}...")
return unpack_model(model_path)
def get_latest_model(model_path: Text = DEFAULT_MODELS_PATH) -> Optional[Text]:
"""Get the latest model from a path.
Args:
model_path: Path to a directory containing zipped models.
Returns:
Path to latest model in the given directory.
"""
if not os.path.exists(model_path) or os.path.isfile(model_path):
model_path = os.path.dirname(model_path)
list_of_files = glob.glob(os.path.join(model_path, "*.tar.gz"))
if len(list_of_files) == 0:
return None
return max(list_of_files, key=os.path.getctime)
def unpack_model(
model_file: Text, working_directory: Optional[Union[Path, Text]] = None
) -> TempDirectoryPath:
"""Unpack a zipped Rasa model.
Args:
model_file: Path to zipped model.
working_directory: Location where the model should be unpacked to.
If `None` a temporary directory will be created.
Returns:
Path to unpacked Rasa model.
"""
import tarfile
if working_directory is None:
working_directory = tempfile.mkdtemp()
# All files are in a subdirectory.
try:
with tarfile.open(model_file, mode="r:gz") as tar:
tar.extractall(working_directory)
logger.debug(f"Extracted model to '{working_directory}'.")
except Exception as e:
logger.error(f"Failed to extract model at {model_file}. Error: {e}")
raise
return TempDirectoryPath(working_directory)
def get_model_subdirectories(
unpacked_model_path: Text,
) -> Tuple[Optional[Text], Optional[Text]]:
"""Return paths for Core and NLU model directories, if they exist.
If neither directories exist, a `ModelNotFound` exception is raised.
Args:
unpacked_model_path: Path to unpacked Rasa model.
Returns:
Tuple (path to Core subdirectory if it exists or `None` otherwise,
path to NLU subdirectory if it exists or `None` otherwise).
"""
core_path = os.path.join(unpacked_model_path, DEFAULT_CORE_SUBDIRECTORY_NAME)
nlu_path = os.path.join(unpacked_model_path, DEFAULT_NLU_SUBDIRECTORY_NAME)
if not os.path.isdir(core_path):
core_path = None
if not os.path.isdir(nlu_path):
nlu_path = None
if not core_path and not nlu_path:
raise ModelNotFound(
"No NLU or Core data for unpacked model at: '{}'.".format(
unpacked_model_path
)
)
return core_path, nlu_path
def create_package_rasa(
training_directory: Text,
output_filename: Text,
fingerprint: Optional[Fingerprint] = None,
) -> Text:
"""Create a zipped Rasa model from trained model files.
Args:
training_directory: Path to the directory which contains the trained
model files.
output_filename: Name of the zipped model file to be created.
fingerprint: A unique fingerprint to identify the model version.
Returns:
Path to zipped model.
"""
import tarfile
if fingerprint:
persist_fingerprint(training_directory, fingerprint)
output_directory = os.path.dirname(output_filename)
if not os.path.exists(output_directory):
os.makedirs(output_directory)
with tarfile.open(output_filename, "w:gz") as tar:
for elem in os.scandir(training_directory):
tar.add(elem.path, arcname=elem.name)
shutil.rmtree(training_directory)
return output_filename
def project_fingerprint() -> Optional[Text]:
"""Create a hash for the project in the current working directory.
Returns:
project hash
"""
try:
remote = check_output( # skipcq:BAN-B607,BAN-B603
["git", "remote", "get-url", "origin"], stderr=DEVNULL
)
return hashlib.sha256(remote).hexdigest()
except (CalledProcessError, OSError):
return None
async def model_fingerprint(file_importer: "TrainingDataImporter") -> Fingerprint:
"""Create a model fingerprint from its used configuration and training data.
Args:
file_importer: File importer which provides the training data and model config.
Returns:
The fingerprint.
"""
import time
config = await file_importer.get_config()
domain = await file_importer.get_domain()
stories = await file_importer.get_stories()
nlu_data = await file_importer.get_nlu_data()
responses = domain.responses
# Do a copy of the domain to not change the actual domain (shallow is enough)
domain = copy.copy(domain)
# don't include the response texts in the fingerprint.
# Their fingerprint is separate.
domain.responses = {}
return {
FINGERPRINT_CONFIG_KEY: _get_fingerprint_of_config(
config, exclude_keys=CONFIG_KEYS
),
FINGERPRINT_CONFIG_CORE_KEY: _get_fingerprint_of_config(
config, include_keys=CONFIG_KEYS_CORE
),
FINGERPRINT_CONFIG_NLU_KEY: _get_fingerprint_of_config(
config, include_keys=CONFIG_KEYS_NLU
),
FINGERPRINT_CONFIG_WITHOUT_EPOCHS_KEY: _get_fingerprint_of_config_without_epochs(
config
),
FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY: domain.fingerprint(),
FINGERPRINT_NLG_KEY: rasa.shared.utils.io.deep_container_fingerprint(responses),
FINGERPRINT_PROJECT: project_fingerprint(),
FINGERPRINT_NLU_DATA_KEY: nlu_data.fingerprint(),
FINGERPRINT_NLU_LABELS_KEY: nlu_data.label_fingerprint(),
FINGERPRINT_STORIES_KEY: stories.fingerprint(),
FINGERPRINT_TRAINED_AT_KEY: time.time(),
FINGERPRINT_RASA_VERSION_KEY: rasa.__version__,
}
def _get_fingerprint_of_config(
config: Optional[Dict[Text, Any]],
include_keys: Optional[List[Text]] = None,
exclude_keys: Optional[List[Text]] = None,
) -> Text:
if not config:
return ""
keys = include_keys or list(filter(lambda k: k not in exclude_keys, config.keys()))
sub_config = {k: config[k] for k in keys if k in config}
return rasa.shared.utils.io.deep_container_fingerprint(sub_config)
def _get_fingerprint_of_config_without_epochs(
config: Optional[Dict[Text, Any]],
) -> Text:
if not config:
return ""
copied_config = copy.deepcopy(config)
for key in ["pipeline", "policies"]:
if copied_config.get(key):
for p in copied_config[key]:
if "epochs" in p:
del p["epochs"]
return rasa.shared.utils.io.deep_container_fingerprint(copied_config)
def fingerprint_from_path(model_path: Text) -> Fingerprint:
"""Load a persisted fingerprint.
Args:
model_path: Path to directory containing the fingerprint.
Returns:
The fingerprint or an empty dict if no fingerprint was found.
"""
if not model_path or not os.path.exists(model_path):
return {}
fingerprint_path = os.path.join(model_path, FINGERPRINT_FILE_PATH)
if os.path.isfile(fingerprint_path):
return rasa.shared.utils.io.read_json_file(fingerprint_path)
else:
return {}
def persist_fingerprint(output_path: Text, fingerprint: Fingerprint):
"""Persist a model fingerprint.
Args:
output_path: Directory in which the fingerprint should be saved.
fingerprint: The fingerprint to be persisted.
"""
path = os.path.join(output_path, FINGERPRINT_FILE_PATH)
rasa.shared.utils.io.dump_obj_as_json_to_file(path, fingerprint)
def did_section_fingerprint_change(
fingerprint1: Fingerprint, fingerprint2: Fingerprint, section: Section
) -> bool:
"""Check whether the fingerprint of a section has changed."""
for k in section.relevant_keys:
if fingerprint1.get(k) != fingerprint2.get(k):
logger.info(f"Data ({k}) for {section.name} section changed.")
return True
return False
def move_model(source: Text, target: Text) -> bool:
"""Move two model directories.
Args:
source: The original folder which should be merged in another.
target: The destination folder where it should be moved to.
Returns:
`True` if the merge was successful, else `False`.
"""
try:
shutil.move(source, target)
return True
except Exception as e:
logging.debug(f"Could not merge model: {e}")
return False
def should_retrain(
new_fingerprint: Fingerprint,
old_model: Text,
train_path: Text,
has_e2e_examples: bool = False,
force_training: bool = False,
) -> FingerprintComparisonResult:
"""Check which components of a model should be retrained.
Args:
new_fingerprint: The fingerprint of the new model to be trained.
old_model: Path to the old zipped model file.
train_path: Path to the directory in which the new model will be trained.
has_e2e_examples: Whether the new training data contains e2e examples.
force_training: Indicates if the model needs to be retrained even if the data
has not changed.
Returns:
A FingerprintComparisonResult object indicating whether Rasa Core and/or Rasa
NLU needs to be retrained or not.
"""
fingerprint_comparison = FingerprintComparisonResult()
if old_model is None or not os.path.exists(old_model):
return fingerprint_comparison
with unpack_model(old_model) as unpacked:
last_fingerprint = fingerprint_from_path(unpacked)
old_core, old_nlu = get_model_subdirectories(unpacked)
fingerprint_comparison = FingerprintComparisonResult(
core=did_section_fingerprint_change(
last_fingerprint, new_fingerprint, SECTION_CORE
),
nlu=did_section_fingerprint_change(
last_fingerprint, new_fingerprint, SECTION_NLU
),
nlg=did_section_fingerprint_change(
last_fingerprint, new_fingerprint, SECTION_NLG
),
force_training=force_training,
)
# We should retrain core if nlu data changes and there are e2e stories.
if has_e2e_examples and fingerprint_comparison.should_retrain_nlu():
fingerprint_comparison.core = True
core_merge_failed = False
if not fingerprint_comparison.should_retrain_core():
target_path = os.path.join(train_path, DEFAULT_CORE_SUBDIRECTORY_NAME)
core_merge_failed = not move_model(old_core, target_path)
fingerprint_comparison.core = core_merge_failed
if not fingerprint_comparison.should_retrain_nlg() and core_merge_failed:
# If moving the Core model failed, we should also retrain NLG
fingerprint_comparison.nlg = True
if not fingerprint_comparison.should_retrain_nlu():
target_path = os.path.join(train_path, "nlu")
fingerprint_comparison.nlu = not move_model(old_nlu, target_path)
return fingerprint_comparison
def can_finetune(
last_fingerprint: Fingerprint,
new_fingerprint: Fingerprint,
core: bool = False,
nlu: bool = False,
) -> bool:
"""Checks if components of a model can be finetuned with incremental training.
Args:
last_fingerprint: The fingerprint of the old model to potentially be fine-tuned.
new_fingerprint: The fingerprint of the new model.
core: Check sections for finetuning a core model.
nlu: Check sections for finetuning an nlu model.
Returns:
`True` if the old model can be finetuned, `False` otherwise.
"""
section_keys = [
FINGERPRINT_CONFIG_WITHOUT_EPOCHS_KEY,
]
if core:
section_keys.append(FINGERPRINT_DOMAIN_WITHOUT_NLG_KEY)
if nlu:
section_keys.append(FINGERPRINT_NLU_LABELS_KEY)
fingerprint_changed = did_section_fingerprint_change(
last_fingerprint,
new_fingerprint,
Section(name="finetune", relevant_keys=section_keys),
)
old_model_above_min_version = version.parse(
last_fingerprint.get(FINGERPRINT_RASA_VERSION_KEY)
) >= version.parse(MINIMUM_COMPATIBLE_VERSION)
return old_model_above_min_version and not fingerprint_changed
def package_model(
fingerprint: Fingerprint,
output_directory: Text,
train_path: Text,
fixed_model_name: Optional[Text] = None,
model_prefix: Text = "",
) -> Text:
"""
Compress a trained model.
Args:
fingerprint: fingerprint of the model
output_directory: path to the directory in which the model should be stored
train_path: path to uncompressed model
fixed_model_name: name of the compressed model file
model_prefix: prefix of the compressed model file
Returns: path to 'tar.gz' model file
"""
output_directory = create_output_path(
output_directory, prefix=model_prefix, fixed_name=fixed_model_name
)
create_package_rasa(train_path, output_directory, fingerprint)
print_success(
"Your Rasa model is trained and saved at '{}'.".format(
os.path.abspath(output_directory)
)
)
return output_directory
async def update_model_with_new_domain(
importer: "TrainingDataImporter", unpacked_model_path: Union[Path, Text]
) -> None:
"""Overwrites the domain of an unpacked model with a new domain.
Args:
importer: Importer which provides the new domain.
unpacked_model_path: Path to the unpacked model.
"""
model_path = Path(unpacked_model_path) / DEFAULT_CORE_SUBDIRECTORY_NAME
domain = await importer.get_domain()
domain.persist(model_path / DEFAULT_DOMAIN_PATH)
def get_model_for_finetuning(
previous_model_file: Optional[Union[Path, Text]]
) -> Optional[Text]:
"""Gets validated path for model to finetune.
Args:
previous_model_file: Path to model file which should be used for finetuning or
a directory in case the latest trained model should be used.
Returns:
Path to model archive. `None` if there is no model.
"""
if Path(previous_model_file).is_dir():
logger.debug(
f"Trying to load latest model from '{previous_model_file}' for "
f"finetuning."
)
return get_latest_model(previous_model_file)
if Path(previous_model_file).is_file():
return previous_model_file
logger.debug(
"No valid model for finetuning found as directory either "
"contains no model or model file cannot be found."
)
return None
| [((937, 964), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (954, 964), False, 'import logging\n'), ((4264, 4289), 'os.path.isdir', 'os.path.isdir', (['model_path'], {}), '(model_path)\n', (4277, 4289), False, 'import os\n'), ((6295, 6331), 'rasa.utils.common.TempDirectoryPath', 'TempDirectoryPath', (['working_directory'], {}), '(working_directory)\n', (6312, 6331), False, 'from rasa.utils.common import TempDirectoryPath\n'), ((6841, 6906), 'os.path.join', 'os.path.join', (['unpacked_model_path', 'DEFAULT_CORE_SUBDIRECTORY_NAME'], {}), '(unpacked_model_path, DEFAULT_CORE_SUBDIRECTORY_NAME)\n', (6853, 6906), False, 'import os\n'), ((6922, 6986), 'os.path.join', 'os.path.join', (['unpacked_model_path', 'DEFAULT_NLU_SUBDIRECTORY_NAME'], {}), '(unpacked_model_path, DEFAULT_NLU_SUBDIRECTORY_NAME)\n', (6934, 6986), False, 'import os\n'), ((7995, 8027), 'os.path.dirname', 'os.path.dirname', (['output_filename'], {}), '(output_filename)\n', (8010, 8027), False, 'import os\n'), ((8274, 8307), 'shutil.rmtree', 'shutil.rmtree', (['training_directory'], {}), '(training_directory)\n', (8287, 8307), False, 'import shutil\n'), ((9403, 9420), 'copy.copy', 'copy.copy', (['domain'], {}), '(domain)\n', (9412, 9420), False, 'import copy\n'), ((11136, 11157), 'copy.deepcopy', 'copy.deepcopy', (['config'], {}), '(config)\n', (11149, 11157), False, 'import copy\n'), ((11788, 11835), 'os.path.join', 'os.path.join', (['model_path', 'FINGERPRINT_FILE_PATH'], {}), '(model_path, FINGERPRINT_FILE_PATH)\n', (11800, 11835), False, 'import os\n'), ((11844, 11876), 'os.path.isfile', 'os.path.isfile', (['fingerprint_path'], {}), '(fingerprint_path)\n', (11858, 11876), False, 'import os\n'), ((12242, 12290), 'os.path.join', 'os.path.join', (['output_path', 'FINGERPRINT_FILE_PATH'], {}), '(output_path, FINGERPRINT_FILE_PATH)\n', (12254, 12290), False, 'import os\n'), ((17669, 17760), 'rasa.cli.utils.create_output_path', 'create_output_path', (['output_directory'], {'prefix': 'model_prefix', 'fixed_name': 'fixed_model_name'}), '(output_directory, prefix=model_prefix, fixed_name=\n fixed_model_name)\n', (17687, 17760), False, 'from rasa.cli.utils import create_output_path\n'), ((4107, 4142), 'rasa.exceptions.ModelNotFound', 'ModelNotFound', (['"""No path specified."""'], {}), "('No path specified.')\n", (4120, 4142), False, 'from rasa.exceptions import ModelNotFound\n'), ((4663, 4690), 'os.path.relpath', 'os.path.relpath', (['model_path'], {}), '(model_path)\n', (4678, 4690), False, 'import os\n'), ((5168, 5194), 'os.path.isfile', 'os.path.isfile', (['model_path'], {}), '(model_path)\n', (5182, 5194), False, 'import os\n'), ((5217, 5244), 'os.path.dirname', 'os.path.dirname', (['model_path'], {}), '(model_path)\n', (5232, 5244), False, 'import os\n'), ((5276, 5312), 'os.path.join', 'os.path.join', (['model_path', '"""*.tar.gz"""'], {}), "(model_path, '*.tar.gz')\n", (5288, 5312), False, 'import os\n'), ((5921, 5939), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ([], {}), '()\n', (5937, 5939), False, 'import tempfile\n'), ((6999, 7023), 'os.path.isdir', 'os.path.isdir', (['core_path'], {}), '(core_path)\n', (7012, 7023), False, 'import os\n'), ((7062, 7085), 'os.path.isdir', 'os.path.isdir', (['nlu_path'], {}), '(nlu_path)\n', (7075, 7085), False, 'import os\n'), ((8039, 8071), 'os.path.exists', 'os.path.exists', (['output_directory'], {}), '(output_directory)\n', (8053, 8071), False, 'import os\n'), ((8081, 8110), 'os.makedirs', 'os.makedirs', (['output_directory'], {}), '(output_directory)\n', (8092, 8110), False, 'import os\n'), ((8121, 8158), 'tarfile.open', 'tarfile.open', (['output_filename', '"""w:gz"""'], {}), "(output_filename, 'w:gz')\n", (8133, 8158), False, 'import tarfile\n'), ((8187, 8217), 'os.scandir', 'os.scandir', (['training_directory'], {}), '(training_directory)\n', (8197, 8217), False, 'import os\n'), ((8522, 8590), 'subprocess.check_output', 'check_output', (["['git', 'remote', 'get-url', 'origin']"], {'stderr': 'DEVNULL'}), "(['git', 'remote', 'get-url', 'origin'], stderr=DEVNULL)\n", (8534, 8590), False, 'from subprocess import CalledProcessError, DEVNULL, check_output\n'), ((10466, 10477), 'time.time', 'time.time', ([], {}), '()\n', (10475, 10477), False, 'import time\n'), ((13094, 13121), 'shutil.move', 'shutil.move', (['source', 'target'], {}), '(source, target)\n', (13105, 13121), False, 'import shutil\n'), ((16957, 16998), 'packaging.version.parse', 'version.parse', (['MINIMUM_COMPATIBLE_VERSION'], {}), '(MINIMUM_COMPATIBLE_VERSION)\n', (16970, 16998), False, 'from packaging import version\n'), ((18362, 18387), 'pathlib.Path', 'Path', (['unpacked_model_path'], {}), '(unpacked_model_path)\n', (18366, 18387), False, 'from pathlib import Path\n'), ((4156, 4182), 'os.path.exists', 'os.path.exists', (['model_path'], {}), '(model_path)\n', (4170, 4182), False, 'import os\n'), ((4198, 4255), 'rasa.exceptions.ModelNotFound', 'ModelNotFound', (['f"""No file or directory at \'{model_path}\'."""'], {}), '(f"No file or directory at \'{model_path}\'.")\n', (4211, 4255), False, 'from rasa.exceptions import ModelNotFound\n'), ((4386, 4458), 'rasa.exceptions.ModelNotFound', 'ModelNotFound', (['f"""Could not find any Rasa model files in \'{model_path}\'."""'], {}), '(f"Could not find any Rasa model files in \'{model_path}\'.")\n', (4399, 4458), False, 'from rasa.exceptions import ModelNotFound\n'), ((4548, 4622), 'rasa.exceptions.ModelNotFound', 'ModelNotFound', (['f"""Path \'{model_path}\' does not point to a Rasa model file."""'], {}), '(f"Path \'{model_path}\' does not point to a Rasa model file.")\n', (4561, 4622), False, 'from rasa.exceptions import ModelNotFound\n'), ((5138, 5164), 'os.path.exists', 'os.path.exists', (['model_path'], {}), '(model_path)\n', (5152, 5164), False, 'import os\n'), ((6002, 6039), 'tarfile.open', 'tarfile.open', (['model_file'], {'mode': '"""r:gz"""'}), "(model_file, mode='r:gz')\n", (6014, 6039), False, 'import tarfile\n'), ((11718, 11744), 'os.path.exists', 'os.path.exists', (['model_path'], {}), '(model_path)\n', (11732, 11744), False, 'import os\n'), ((13177, 13221), 'logging.debug', 'logging.debug', (['f"""Could not merge model: {e}"""'], {}), "(f'Could not merge model: {e}')\n", (13190, 13221), False, 'import logging\n'), ((14164, 14189), 'os.path.exists', 'os.path.exists', (['old_model'], {}), '(old_model)\n', (14178, 14189), False, 'import os\n'), ((15221, 15277), 'os.path.join', 'os.path.join', (['train_path', 'DEFAULT_CORE_SUBDIRECTORY_NAME'], {}), '(train_path, DEFAULT_CORE_SUBDIRECTORY_NAME)\n', (15233, 15277), False, 'import os\n'), ((15698, 15729), 'os.path.join', 'os.path.join', (['train_path', '"""nlu"""'], {}), "(train_path, 'nlu')\n", (15710, 15729), False, 'import os\n'), ((17933, 17966), 'os.path.abspath', 'os.path.abspath', (['output_directory'], {}), '(output_directory)\n', (17948, 17966), False, 'import os\n'), ((18931, 18956), 'pathlib.Path', 'Path', (['previous_model_file'], {}), '(previous_model_file)\n', (18935, 18956), False, 'from pathlib import Path\n'), ((19164, 19189), 'pathlib.Path', 'Path', (['previous_model_file'], {}), '(previous_model_file)\n', (19168, 19189), False, 'from pathlib import Path\n'), ((8656, 8678), 'hashlib.sha256', 'hashlib.sha256', (['remote'], {}), '(remote)\n', (8670, 8678), False, 'import hashlib\n')] |
CatalaniCD/quantitative_finance | algorithmic_trading/backester_framework_test.py | c752516a43cd80914dcc8411aadd7b15a258d6a4 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 16 11:20:01 2021
@author: q
GOAL : develop a backtester from a .py framework / library
# installation :
pip install backtesting
# Documentation
Index :
- Manuals
- Tutorials
- Example Strategies
- FAQ
- License
- API Reference Documentation
source : https://kernc.github.io/backtesting.py/doc/backtesting/
# Features
* Simple, well-documented API
* Blazing fast execution
* Built-in optimizer
* Library of composable base strategies and utilities
* Indicator-library-agnostic
* Supports any financial instrument with candlestick data
* Detailed results
* Interactive visualizations
"""
# =============================================================================
# imports and settings
# =============================================================================
# data handling
import pandas as pd
import numpy as np
# import backtesting and set options
import backtesting
# Set notebook False
backtesting.set_bokeh_output(notebook=False)
from backtesting import Backtest, Strategy
from backtesting.lib import crossover, cross
from backtesting.test import SMA, GOOG
# =============================================================================
# strategy definition
# =============================================================================
class PriceAboveSMA(Strategy):
_ma_period = 21 # Moving Average
def init(self):
# compute momentum
""" Simple Moving Average Calc"""
self.sma = self.I(SMA, self.data.Close, self._ma_period)
def next(self):
price = self.data.Close[-1]
if not self.position and price > self.sma[-1]:
# market entry
self.buy()
elif self.position and price < self.sma[-1]:
# market exit
self.position.close()
# =============================================================================
# Program Execution
# =============================================================================
if __name__ == '__main__':
""" Instantiate the Backtester """
backtester = Backtest(GOOG, PriceAboveSMA, commission=.002,
exclusive_orders=True, cash = 10000)
PLOT = True
""" Run a Single Backtest """
stats = backtester.run()
print(stats)
if PLOT: backtester.plot()
| [((1079, 1123), 'backtesting.set_bokeh_output', 'backtesting.set_bokeh_output', ([], {'notebook': '(False)'}), '(notebook=False)\n', (1107, 1123), False, 'import backtesting\n'), ((2231, 2318), 'backtesting.Backtest', 'Backtest', (['GOOG', 'PriceAboveSMA'], {'commission': '(0.002)', 'exclusive_orders': '(True)', 'cash': '(10000)'}), '(GOOG, PriceAboveSMA, commission=0.002, exclusive_orders=True, cash\n =10000)\n', (2239, 2318), False, 'from backtesting import Backtest, Strategy\n')] |
PauloAlexSilva/Python | Sec_10_expr_lambdas_fun_integradas/f_generators.py | 690913cdcfd8bde52d9ddd15e3c838e6aef27730 | """"
Generator Expression
Em aulas anteriores foi abordado:
- List Comprehension;
- Dictionary Comprehension;
- Set Comprehension.
Não foi abordado:
- Tuple Comprehension ... porque elas se chamam Generators
nomes = ['Carlos', 'Camila', 'Carla', 'Cristiana', 'Cristina', 'Vanessa']
print(any8[nomes[0] == 'C' for nome in nomes])
# Poderia ter sido feito usando os Generators
nomes = ['Carlos', 'Camila', 'Carla', 'Cristiana', 'Cristina', 'Vanessa']
print(any(nome[0] == 'C' for nome in nomes))
# List Comprehension
res = [nome[0] == 'C' for nome in nomes]
print(type(res))
print(res) # [True, True, True, True, True, False]
# Generator - mais efeciente
res2 = (nome[0] == 'C' for nome in nomes)
print(type(res2))
print(res2)
# O que faz a função de getsizeof()? -> retorna a quantidade de bytes em memória do elemento
# passado como parâmetro
from sys import getsizeof
# Mostra quantos bytes a string 'Paulo' está ocupando em memória.
# Quanto maior a string mais espaço ocupa.
print(getsizeof('Paulo'))
print(getsizeof('Quanto maior a string mais espaço ocupa.'))
print(getsizeof(9))
print(getsizeof(91))
print(getsizeof(12345667890))
print(getsizeof(True))
from sys import getsizeof
# Gerando uma lista de números com List Comprehension
list_comp = getsizeof([x * 10 for x in range(1000)])
# Gerando uma lista de números com Set Comprehension
set_comp = getsizeof({x * 10 for x in range(1000)})
# Gerando uma lista de números com Dictionary Comprehension
dic_comp = getsizeof({x: x * 10 for x in range(1000)})
# Gerando uma lista de números com Generator
gen = getsizeof(x * 10 for x in range(1000))
print('Para fazer a mesma gastamos em memória: ')
print(f'List Comprehension: {list_comp} bytes!')
print(f'Set Comprehension: {set_comp} bytes!')
print(f'Dictionary Comprehension: {dic_comp} bytes!')
print(f'Generator Expression: {gen} bytes!')
Para fazer a mesma gastamos em memória:
List Comprehension: 8856 bytes!
Set Comprehension: 32984 bytes!
Dictionary Comprehension: 36960 bytes!
Generator Expression: 112 bytes!
"""
# Posso iterar no Generator Expression? Sim
gen = (x * 10 for x in range(1000))
print(gen)
print(type(gen))
for num in gen:
print(num)
| [] |
valdirsjr/learning.data | python/ordenacao.py | a4b72dfd27f55f2f04120644b73232bf343f71e3 | numero1 = int(input("Digite o primeiro número: "))
numero2 = int(input("Digite o segundo número: "))
numero3 = int(input("Digite o terceiro número: "))
if (numero1 < numero2 and numero2 < numero3):
print("crescente")
else:
print("não está em ordem crescente") | [] |
kooi/ippt-od | _sources/5-extra/opg-parameters-sneeuwvlok_solution.py | f1ba44ccfb72e6fcdfdc392fbfbec3e37c47b354 | import turtle
tina = turtle.Turtle()
tina.shape("turtle")
tina.speed(10)
def parallellogram(lengte):
for i in range(2):
tina.forward(lengte)
tina.right(60)
tina.forward(lengte)
tina.right(120)
def sneeuwvlok(lengte, num):
for i in range(num):
parallellogram(lengte)
tina.right(360.0/num) # 360.0 zorgt voor cast van int naar float
sneeuwvlok(30, 6)
| [((21, 36), 'turtle.Turtle', 'turtle.Turtle', ([], {}), '()\n', (34, 36), False, 'import turtle\n')] |
servalproject/nikola | nikola/plugins/task_render_listings.py | 4d78504d93597894f3da4a434dfafdec907601a7 | # Copyright (c) 2012 Roberto Alsina y otros.
# 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 __future__ import unicode_literals, print_function
import os
from pygments import highlight
from pygments.lexers import get_lexer_for_filename, TextLexer
from pygments.formatters import HtmlFormatter
from nikola.plugin_categories import Task
from nikola import utils
class Listings(Task):
"""Render pretty listings."""
name = "render_listings"
def gen_tasks(self):
"""Render pretty code listings."""
kw = {
"default_lang": self.site.config["DEFAULT_LANG"],
"listings_folder": self.site.config["LISTINGS_FOLDER"],
"output_folder": self.site.config["OUTPUT_FOLDER"],
"index_file": self.site.config["INDEX_FILE"],
}
# Things to ignore in listings
ignored_extensions = (".pyc",)
def render_listing(in_name, out_name, folders=[], files=[]):
if in_name:
with open(in_name, 'r') as fd:
try:
lexer = get_lexer_for_filename(in_name)
except:
lexer = TextLexer()
code = highlight(fd.read(), lexer,
HtmlFormatter(cssclass='code',
linenos="table", nowrap=False,
lineanchors=utils.slugify(f),
anchorlinenos=True))
title = os.path.basename(in_name)
else:
code = ''
title = ''
crumbs = utils.get_crumbs(os.path.relpath(out_name,
kw['output_folder']),
is_file=True)
context = {
'code': code,
'title': title,
'crumbs': crumbs,
'lang': kw['default_lang'],
'folders': folders,
'files': files,
'description': title,
}
self.site.render_template('listing.tmpl', out_name,
context)
flag = True
template_deps = self.site.template_system.template_deps('listing.tmpl')
for root, dirs, files in os.walk(kw['listings_folder']):
flag = False
# Render all files
out_name = os.path.join(
kw['output_folder'],
root, kw['index_file']
)
yield {
'basename': self.name,
'name': out_name,
'file_dep': template_deps,
'targets': [out_name],
'actions': [(render_listing, [None, out_name, dirs, files])],
# This is necessary to reflect changes in blog title,
# sidebar links, etc.
'uptodate': [utils.config_changed(
self.site.config['GLOBAL_CONTEXT'])],
'clean': True,
}
for f in files:
ext = os.path.splitext(f)[-1]
if ext in ignored_extensions:
continue
in_name = os.path.join(root, f)
out_name = os.path.join(
kw['output_folder'],
root,
f) + '.html'
yield {
'basename': self.name,
'name': out_name,
'file_dep': template_deps + [in_name],
'targets': [out_name],
'actions': [(render_listing, [in_name, out_name])],
# This is necessary to reflect changes in blog title,
# sidebar links, etc.
'uptodate': [utils.config_changed(
self.site.config['GLOBAL_CONTEXT'])],
'clean': True,
}
if flag:
yield {
'basename': self.name,
'actions': [],
}
| [((3399, 3429), 'os.walk', 'os.walk', (["kw['listings_folder']"], {}), "(kw['listings_folder'])\n", (3406, 3429), False, 'import os\n'), ((3510, 3567), 'os.path.join', 'os.path.join', (["kw['output_folder']", 'root', "kw['index_file']"], {}), "(kw['output_folder'], root, kw['index_file'])\n", (3522, 3567), False, 'import os\n'), ((2582, 2607), 'os.path.basename', 'os.path.basename', (['in_name'], {}), '(in_name)\n', (2598, 2607), False, 'import os\n'), ((2717, 2763), 'os.path.relpath', 'os.path.relpath', (['out_name', "kw['output_folder']"], {}), "(out_name, kw['output_folder'])\n", (2732, 2763), False, 'import os\n'), ((4304, 4325), 'os.path.join', 'os.path.join', (['root', 'f'], {}), '(root, f)\n', (4316, 4325), False, 'import os\n'), ((4179, 4198), 'os.path.splitext', 'os.path.splitext', (['f'], {}), '(f)\n', (4195, 4198), False, 'import os\n'), ((4353, 4395), 'os.path.join', 'os.path.join', (["kw['output_folder']", 'root', 'f'], {}), "(kw['output_folder'], root, f)\n", (4365, 4395), False, 'import os\n'), ((2096, 2127), 'pygments.lexers.get_lexer_for_filename', 'get_lexer_for_filename', (['in_name'], {}), '(in_name)\n', (2118, 2127), False, 'from pygments.lexers import get_lexer_for_filename, TextLexer\n'), ((4004, 4060), 'nikola.utils.config_changed', 'utils.config_changed', (["self.site.config['GLOBAL_CONTEXT']"], {}), "(self.site.config['GLOBAL_CONTEXT'])\n", (4024, 4060), False, 'from nikola import utils\n'), ((2188, 2199), 'pygments.lexers.TextLexer', 'TextLexer', ([], {}), '()\n', (2197, 2199), False, 'from pygments.lexers import get_lexer_for_filename, TextLexer\n'), ((4895, 4951), 'nikola.utils.config_changed', 'utils.config_changed', (["self.site.config['GLOBAL_CONTEXT']"], {}), "(self.site.config['GLOBAL_CONTEXT'])\n", (4915, 4951), False, 'from nikola import utils\n'), ((2468, 2484), 'nikola.utils.slugify', 'utils.slugify', (['f'], {}), '(f)\n', (2481, 2484), False, 'from nikola import utils\n')] |
jsyony37/csld | csld/phonon/head.py | b0e6d5845d807174f24ca7b591bc164c608c99c8 | # to include all module here in order to cite
from numpy import *
from numpy.linalg import *
import string
import os
import scipy
import scipy.sparse
#import rwposcar
#import anaxdat
import math
#define touch file
def touch(file):#input string
if os.path.isfile(file):
os.system(str("rm"+" "+file))
os.system(str("touch"+" "+file))
else:
os.system(str("touch"+" "+file))
def mkdir(dir):
if os.path.isdir(dir):
os.system(str("rm"+" -r "+dir))
os.system(str("mkdir"+" "+dir))
else:
os.system(str("mkdir"+" "+dir))
if False:
mkdir("xixi/")
#define rm file
def rm(file):
if os.path.isfile(file):
os.system(str("rm"+" "+file))
else:
print("No file found, dont need to rm")
#define check file(1 exist; else0)
def check(file):
if os.path.isfile(file):
return int(1)
else:
return int(0)
#define check the file status (print the status)
def checkfile(file):
if os.path.isfile(file):
print(str(file)+" exists :)")
else:
print(str(file)+" not found :(")
#define readallline function
def readinline(file):
dataout=[]
if check(file):
fin=open(file,"r")
for line in fin:
dataout.append(line.split())#map(float,line.split()))
fin.close()
else:
print(str(file)+" not found :(")
return array(dataout)
#define write1dmat
def write1dmat(datain, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file, "w")
fout.writelines("\n".join(map(str,datain)))
fout.close()
#define one number to file
def writenumber(datain, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file,"w")
fout.writelines(str(datain))
fout.close()
#define write2dmat
def write2dmat(datain, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file, "w")
#cout line number
fout.writelines(str(len(datain))+"\n")
for i in datain:
fout.writelines(" ".join(map(str,i))+"\n")
fout.close()
#define write2dMTX
def write2dMTX(datain, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file, "w")
fout.writelines("%%MatrixMarket matrix coordinate real general\n")
fout.writelines("%Created by Wolfram Mathematica 9.0 : www.wolfram.com\n")
print("Transfering to sparse matrix----")
#get rid of small numbers
#for i in range(len(datain)):
# for j in range(len(datain[i])):
# datain[i][j]=round(datain[i][j],3)
BB=scipy.sparse.coo_matrix(datain)
print("Spare matrix obtained!")
# print BB.row
# print BB.col
# print BB.data
fout.writelines(str(len(datain))+" "+str(len(datain[0]))+" "+str(len(BB.data))+"\n")
for i in range(len(BB.data)):
fout.writelines(str(BB.row[i]+1)+" "+str(BB.col[i]+1)+" "+str(BB.data[i])+"\n")
#for i in range(len(datain)):
#for j in range(len(datain[0])):
#fout.writelines(str(i+1)+" "+str(j+1)+" "+str(datain[i][j])+"\n")
fout.close()
def read2dMTX(file):
if check(file):
counter=0
for line in open(file):
counter=counter+1
if counter <=2:
continue
if counter ==3:
inlist=list(map(int,line.split()))
nrow=inlist[0]
ncol=inlist[1]
dataout=array([[0.0]*ncol]*nrow)
continue
if counter >=4:
tmp=line.split()
#print str(tmp)+", "+str(tmp[2])
dataout[int(tmp[0])-1][int(tmp[1])-1]=float(tmp[2])
#print "\n"
return dataout.tolist()
else:
print(str(file)+" not found :(")
#test
if False:
Amat=[[0,1],[2,0],[0,0],[0,16]]
print(Amat)
write2dMTX(Amat, "test.mtx")
print(read2dMTX("test.mtx"))
#define read1dmat
#read float
def read1dmat(file):
mat=[]
if check(file):
for line in open(file):
mat.append(float(line))
return mat
else:
print(str(file)+" not found :(")
if False:
haha=[1,2,3,4,5]
write1dmat(haha, "haha")
xixi=read1dmat("haha")
print(xixi)
#define read2dmat (this is a relatively fast way: iter or chunck read)
def read2dmat(file,icomplex=False):
mat=[]
if check(file):
print("Read matrix start")
for line in open(file):
if not icomplex:
mat.append(list(map(float,line.split())))
else:
mat.append(list(map(complex,line.split())))
print("Read matrix end")
#delete line counter
del mat[0]
return mat
else:
print(str(file)+" not found :(")
#test
#mat=read2dmat("C-isoo.mat")
#print len(mat)
#print len(mat[0])
def clusstr(clus):
dataout=""
for item in clus:
dataout=dataout+str(item[0])+" "+str(item[1])+" "+str(item[2])+"\n"
return dataout
def lptstr(lpt):
dataout=""
for item in lpt:
dataout=dataout+str(item[0][0])+" "+str(item[0][1])+" "+str(item[0][2])+" "+str(item[1])+"\n"
return dataout
#define writeorb(orb)
def writeorb(orbset, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file, "w")
fout.write(str(len(orbset))+"\n\n")
for orb in orbset:
fout.write(str(len(orb))+"\n\n")
for item in orb:
npt=len(item[0])
fout.write(str(npt)+"\n")
fout.write(clusstr(item[0]))
fout.write(str(item[1])+"\n")
fout.write(str(item[2])+"\n")
fout.write(lptstr(item[3]))
fout.write("\n")
fout.close()
def writeclus(clus, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file,"w")
fout.write(str(len(clus))+"\n\n")
for item in clus:
fout.write(str(len(item))+"\n")
fout.write(clusstr(item))
fout.write("\n")
fout.close()
def writeSCinfo(SCinfo, file):
if check(file):
rm(file)
touch(file)
else:
touch(file)
fout=open(file, "w")
tmp=[SCinfo['SC'], SCinfo['invSC'], SCinfo['SCref'], SCinfo['SCpos'], SCinfo['SCmat'], SCinfo['invSCmat'], SCinfo['order']]
lentmp=[len(i) for i in tmp]
fout.write(" ".join(map(str,lentmp))+"\n")
for i in tmp:
if i==SCinfo['order']:
fout.write("\n".join(map(str,i))+"\n")
else:
for j in i:
fout.write(" ".join(map(str,j))+"\n")
fout.close()
def readSCinfo(file):
SCinfo={}
if check(file):
fin=open(file, "r")
lenlist=list(map(int,(fin.readline()).split()))
# tmp=[SCinfo['SC'], SCinfo['invSC'], SCinfo['SCref'], SCinfo['SCpos'], SCinfo['SCmat'], SCinfo['invSCmat'], SCinfo['order']]
tmp=[]
for i in range(7):
tmp1=[]
for j in range(lenlist[i]):
if i in [0,1,3,4,5]:
tmp1.append(list(map(float,(fin.readline()).split())))
elif i in [2]:
tmp1.append(list(map(int,(fin.readline()).split())))
else:
tmp1.append(list(map(int,(fin.readline()).split()))[0])
tmp.append(tmp1)
SCinfo['SC']=tmp[0]
SCinfo['invSC']=tmp[1]
SCinfo['SCref']=tmp[2]
SCinfo['SCpos']=tmp[3]
SCinfo['SCmat']=tmp[4]
SCinfo['invSCmat']=tmp[5]
SCinfo['order']=tmp[6]
else:
print(str(file)+" not found :(")
return SCinfo
#test
if False:
SCinfo={'invSCmat': [[-0.25, 0.25, 0.25], [0.25, -0.25, 0.25], [0.25, 0.25, -0.25]], 'SCmat': [[0.0, 2.0, 2.0], [2.0, 0.0, 2.0], [2.0, 2.0, 0.0]], 'SCref': [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 2], [3, 2, 2], [3, 3, 3]], 'SCpos': [[0.75, 0.25, 0.5], [0.25, 0.75, 0.5], [0.5, 0.25, 0.75], [0.5, 0.75, 0.25], [0.25, 0.5, 0.75], [0.75, 0.5, 0.25], [0.785, 0.785, 0.0], [0.215, 0.215, 0.0], [0.0, 0.215, 0.215], [0.0, 0.785, 0.785], [0.785, 0.0, 0.785], [0.215, 0.0, 0.215], [0.5239, 0.0, 0.7543], [0.7543, 0.0, 0.5239], [0.4761, 0.2304, 0.4761], [0.2457, 0.7696, 0.2457], [0.5239, 0.7543, 0.0], [0.7543, 0.5239, 0.0], [0.2457, 0.2457, 0.7696], [0.4761, 0.4761, 0.2304], [0.7696, 0.2457, 0.2457], [0.2304, 0.4761, 0.4761], [0.0, 0.5239, 0.7543], [0.0, 0.7543, 0.5239], [0.0, 0.0, 0.0], [0.4636, 0.0, 0.0], [0.0, 0.0, 0.4636], [0.5364, 0.5364, 0.5364], [0.0, 0.4636, 0.0], [0.75, 1.25, 1.5], [0.25, 1.75, 1.5], [0.5, 1.25, 1.75], [0.5, 1.75, 1.25], [0.25, 1.5, 1.75], [0.75, 1.5, 1.25], [0.785, 1.785, 1.0], [0.215, 1.215, 1.0], [0.0, 1.215, 1.215], [0.0, 1.785, 1.785], [0.785, 1.0, 1.785], [0.215, 1.0, 1.215], [0.5239, 1.0, 1.7543], [0.7543, 1.0, 1.5239], [0.4761, 1.2304, 1.4761], [0.2457, 1.7696, 1.2457], [0.5239, 1.7543, 1.0], [0.7543, 1.5239, 1.0], [0.2457, 1.2457, 1.7696], [0.4761, 1.4761, 1.2304], [0.7696, 1.2457, 1.2457], [0.2304, 1.4761, 1.4761], [0.0, 1.5239, 1.7543], [0.0, 1.7543, 1.5239], [0.0, 1.0, 1.0], [0.4636, 1.0, 1.0], [0.0, 1.0, 1.4636], [0.5364, 1.5364, 1.5364], [0.0, 1.4636, 1.0], [1.75, 0.25, 1.5], [1.25, 0.75, 1.5], [1.5, 0.25, 1.75], [1.5, 0.75, 1.25], [1.25, 0.5, 1.75], [1.75, 0.5, 1.25], [1.785, 0.785, 1.0], [1.215, 0.215, 1.0], [1.0, 0.215, 1.215], [1.0, 0.785, 1.785], [1.785, 0.0, 1.785], [1.215, 0.0, 1.215], [1.5239, 0.0, 1.7543], [1.7543, 0.0, 1.5239], [1.4761, 0.2304, 1.4761], [1.2457, 0.7696, 1.2457], [1.5239, 0.7543, 1.0], [1.7543, 0.5239, 1.0], [1.2457, 0.2457, 1.7696], [1.4761, 0.4761, 1.2304], [1.7696, 0.2457, 1.2457], [1.2304, 0.4761, 1.4761], [1.0, 0.5239, 1.7543], [1.0, 0.7543, 1.5239], [1.0, 0.0, 1.0], [1.4636, 0.0, 1.0], [1.0, 0.0, 1.4636], [1.5364, 0.5364, 1.5364], [1.0, 0.4636, 1.0], [1.75, 1.25, 0.5], [1.25, 1.75, 0.5], [1.5, 1.25, 0.75], [1.5, 1.75, 0.25], [1.25, 1.5, 0.75], [1.75, 1.5, 0.25], [1.785, 1.785, 0.0], [1.215, 1.215, 0.0], [1.0, 1.215, 0.215], [1.0, 1.785, 0.785], [1.785, 1.0, 0.785], [1.215, 1.0, 0.215], [1.5239, 1.0, 0.7543], [1.7543, 1.0, 0.5239], [1.4761, 1.2304, 0.4761], [1.2457, 1.7696, 0.2457], [1.5239, 1.7543, 0.0], [1.7543, 1.5239, 0.0], [1.2457, 1.2457, 0.7696], [1.4761, 1.4761, 0.2304], [1.7696, 1.2457, 0.2457], [1.2304, 1.4761, 0.4761], [1.0, 1.5239, 0.7543], [1.0, 1.7543, 0.5239], [1.0, 1.0, 0.0], [1.4636, 1.0, 0.0], [1.0, 1.0, 0.4636], [1.5364, 1.5364, 0.5364], [1.0, 1.4636, 0.0], [1.75, 1.25, 1.5], [1.25, 1.75, 1.5], [1.5, 1.25, 1.75], [1.5, 1.75, 1.25], [1.25, 1.5, 1.75], [1.75, 1.5, 1.25], [1.785, 1.785, 1.0], [1.215, 1.215, 1.0], [1.0, 1.215, 1.215], [1.0, 1.785, 1.785], [1.785, 1.0, 1.785], [1.215, 1.0, 1.215], [1.5239, 1.0, 1.7543], [1.7543, 1.0, 1.5239], [1.4761, 1.2304, 1.4761], [1.2457, 1.7696, 1.2457], [1.5239, 1.7543, 1.0], [1.7543, 1.5239, 1.0], [1.2457, 1.2457, 1.7696], [1.4761, 1.4761, 1.2304], [1.7696, 1.2457, 1.2457], [1.2304, 1.4761, 1.4761], [1.0, 1.5239, 1.7543], [1.0, 1.7543, 1.5239], [1.0, 1.0, 1.0], [1.4636, 1.0, 1.0], [1.0, 1.0, 1.4636], [1.5364, 1.5364, 1.5364], [1.0, 1.4636, 1.0], [1.75, 1.25, 2.5], [1.25, 1.75, 2.5], [1.5, 1.25, 2.75], [1.5, 1.75, 2.25], [1.25, 1.5, 2.75], [1.75, 1.5, 2.25], [1.785, 1.785, 2.0], [1.215, 1.215, 2.0], [1.0, 1.215, 2.215], [1.0, 1.785, 2.785], [1.785, 1.0, 2.785], [1.215, 1.0, 2.215], [1.5239, 1.0, 2.7543], [1.7543, 1.0, 2.5239], [1.4761, 1.2304, 2.4761], [1.2457, 1.7696, 2.2457], [1.5239, 1.7543, 2.0], [1.7543, 1.5239, 2.0], [1.2457, 1.2457, 2.7696], [1.4761, 1.4761, 2.2304], [1.7696, 1.2457, 2.2457], [1.2304, 1.4761, 2.4761], [1.0, 1.5239, 2.7543], [1.0, 1.7543, 2.5239], [1.0, 1.0, 2.0], [1.4636, 1.0, 2.0], [1.0, 1.0, 2.4636], [1.5364, 1.5364, 2.5364], [1.0, 1.4636, 2.0], [1.75, 2.25, 1.5], [1.25, 2.75, 1.5], [1.5, 2.25, 1.75], [1.5, 2.75, 1.25], [1.25, 2.5, 1.75], [1.75, 2.5, 1.25], [1.785, 2.785, 1.0], [1.215, 2.215, 1.0], [1.0, 2.215, 1.215], [1.0, 2.785, 1.785], [1.785, 2.0, 1.785], [1.215, 2.0, 1.215], [1.5239, 2.0, 1.7543], [1.7543, 2.0, 1.5239], [1.4761, 2.2304, 1.4761], [1.2457, 2.7696, 1.2457], [1.5239, 2.7543, 1.0], [1.7543, 2.5239, 1.0], [1.2457, 2.2457, 1.7696], [1.4761, 2.4761, 1.2304], [1.7696, 2.2457, 1.2457], [1.2304, 2.4761, 1.4761], [1.0, 2.5239, 1.7543], [1.0, 2.7543, 1.5239], [1.0, 2.0, 1.0], [1.4636, 2.0, 1.0], [1.0, 2.0, 1.4636], [1.5364, 2.5364, 1.5364], [1.0, 2.4636, 1.0], [1.75, 2.25, 2.5], [1.25, 2.75, 2.5], [1.5, 2.25, 2.75], [1.5, 2.75, 2.25], [1.25, 2.5, 2.75], [1.75, 2.5, 2.25], [1.785, 2.785, 2.0], [1.215, 2.215, 2.0], [1.0, 2.215, 2.215], [1.0, 2.785, 2.785], [1.785, 2.0, 2.785], [1.215, 2.0, 2.215], [1.5239, 2.0, 2.7543], [1.7543, 2.0, 2.5239], [1.4761, 2.2304, 2.4761], [1.2457, 2.7696, 2.2457], [1.5239, 2.7543, 2.0], [1.7543, 2.5239, 2.0], [1.2457, 2.2457, 2.7696], [1.4761, 2.4761, 2.2304], [1.7696, 2.2457, 2.2457], [1.2304, 2.4761, 2.4761], [1.0, 2.5239, 2.7543], [1.0, 2.7543, 2.5239], [1.0, 2.0, 2.0], [1.4636, 2.0, 2.0], [1.0, 2.0, 2.4636], [1.5364, 2.5364, 2.5364], [1.0, 2.4636, 2.0], [2.75, 1.25, 1.5], [2.25, 1.75, 1.5], [2.5, 1.25, 1.75], [2.5, 1.75, 1.25], [2.25, 1.5, 1.75], [2.75, 1.5, 1.25], [2.785, 1.785, 1.0], [2.215, 1.215, 1.0], [2.0, 1.215, 1.215], [2.0, 1.785, 1.785], [2.785, 1.0, 1.785], [2.215, 1.0, 1.215], [2.5239, 1.0, 1.7543], [2.7543, 1.0, 1.5239], [2.4761, 1.2304, 1.4761], [2.2457, 1.7696, 1.2457], [2.5239, 1.7543, 1.0], [2.7543, 1.5239, 1.0], [2.2457, 1.2457, 1.7696], [2.4761, 1.4761, 1.2304], [2.7696, 1.2457, 1.2457], [2.2304, 1.4761, 1.4761], [2.0, 1.5239, 1.7543], [2.0, 1.7543, 1.5239], [2.0, 1.0, 1.0], [2.4636, 1.0, 1.0], [2.0, 1.0, 1.4636], [2.5364, 1.5364, 1.5364], [2.0, 1.4636, 1.0], [2.75, 1.25, 2.5], [2.25, 1.75, 2.5], [2.5, 1.25, 2.75], [2.5, 1.75, 2.25], [2.25, 1.5, 2.75], [2.75, 1.5, 2.25], [2.785, 1.785, 2.0], [2.215, 1.215, 2.0], [2.0, 1.215, 2.215], [2.0, 1.785, 2.785], [2.785, 1.0, 2.785], [2.215, 1.0, 2.215], [2.5239, 1.0, 2.7543], [2.7543, 1.0, 2.5239], [2.4761, 1.2304, 2.4761], [2.2457, 1.7696, 2.2457], [2.5239, 1.7543, 2.0], [2.7543, 1.5239, 2.0], [2.2457, 1.2457, 2.7696], [2.4761, 1.4761, 2.2304], [2.7696, 1.2457, 2.2457], [2.2304, 1.4761, 2.4761], [2.0, 1.5239, 2.7543], [2.0, 1.7543, 2.5239], [2.0, 1.0, 2.0], [2.4636, 1.0, 2.0], [2.0, 1.0, 2.4636], [2.5364, 1.5364, 2.5364], [2.0, 1.4636, 2.0], [2.75, 2.25, 1.5], [2.25, 2.75, 1.5], [2.5, 2.25, 1.75], [2.5, 2.75, 1.25], [2.25, 2.5, 1.75], [2.75, 2.5, 1.25], [2.785, 2.785, 1.0], [2.215, 2.215, 1.0], [2.0, 2.215, 1.215], [2.0, 2.785, 1.785], [2.785, 2.0, 1.785], [2.215, 2.0, 1.215], [2.5239, 2.0, 1.7543], [2.7543, 2.0, 1.5239], [2.4761, 2.2304, 1.4761], [2.2457, 2.7696, 1.2457], [2.5239, 2.7543, 1.0], [2.7543, 2.5239, 1.0], [2.2457, 2.2457, 1.7696], [2.4761, 2.4761, 1.2304], [2.7696, 2.2457, 1.2457], [2.2304, 2.4761, 1.4761], [2.0, 2.5239, 1.7543], [2.0, 2.7543, 1.5239], [2.0, 2.0, 1.0], [2.4636, 2.0, 1.0], [2.0, 2.0, 1.4636], [2.5364, 2.5364, 1.5364], [2.0, 2.4636, 1.0], [2.75, 2.25, 2.5], [2.25, 2.75, 2.5], [2.5, 2.25, 2.75], [2.5, 2.75, 2.25], [2.25, 2.5, 2.75], [2.75, 2.5, 2.25], [2.785, 2.785, 2.0], [2.215, 2.215, 2.0], [2.0, 2.215, 2.215], [2.0, 2.785, 2.785], [2.785, 2.0, 2.785], [2.215, 2.0, 2.215], [2.5239, 2.0, 2.7543], [2.7543, 2.0, 2.5239], [2.4761, 2.2304, 2.4761], [2.2457, 2.7696, 2.2457], [2.5239, 2.7543, 2.0], [2.7543, 2.5239, 2.0], [2.2457, 2.2457, 2.7696], [2.4761, 2.4761, 2.2304], [2.7696, 2.2457, 2.2457], [2.2304, 2.4761, 2.4761], [2.0, 2.5239, 2.7543], [2.0, 2.7543, 2.5239], [2.0, 2.0, 2.0], [2.4636, 2.0, 2.0], [2.0, 2.0, 2.4636], [2.5364, 2.5364, 2.5364], [2.0, 2.4636, 2.0], [2.75, 2.25, 3.5], [2.25, 2.75, 3.5], [2.5, 2.25, 3.75], [2.5, 2.75, 3.25], [2.25, 2.5, 3.75], [2.75, 2.5, 3.25], [2.785, 2.785, 3.0], [2.215, 2.215, 3.0], [2.0, 2.215, 3.215], [2.0, 2.785, 3.785], [2.785, 2.0, 3.785], [2.215, 2.0, 3.215], [2.5239, 2.0, 3.7543], [2.7543, 2.0, 3.5239], [2.4761, 2.2304, 3.4761], [2.2457, 2.7696, 3.2457], [2.5239, 2.7543, 3.0], [2.7543, 2.5239, 3.0], [2.2457, 2.2457, 3.7696], [2.4761, 2.4761, 3.2304], [2.7696, 2.2457, 3.2457], [2.2304, 2.4761, 3.4761], [2.0, 2.5239, 3.7543], [2.0, 2.7543, 3.5239], [2.0, 2.0, 3.0], [2.4636, 2.0, 3.0], [2.0, 2.0, 3.4636], [2.5364, 2.5364, 3.5364], [2.0, 2.4636, 3.0], [2.75, 3.25, 2.5], [2.25, 3.75, 2.5], [2.5, 3.25, 2.75], [2.5, 3.75, 2.25], [2.25, 3.5, 2.75], [2.75, 3.5, 2.25], [2.785, 3.785, 2.0], [2.215, 3.215, 2.0], [2.0, 3.215, 2.215], [2.0, 3.785, 2.785], [2.785, 3.0, 2.785], [2.215, 3.0, 2.215], [2.5239, 3.0, 2.7543], [2.7543, 3.0, 2.5239], [2.4761, 3.2304, 2.4761], [2.2457, 3.7696, 2.2457], [2.5239, 3.7543, 2.0], [2.7543, 3.5239, 2.0], [2.2457, 3.2457, 2.7696], [2.4761, 3.4761, 2.2304], [2.7696, 3.2457, 2.2457], [2.2304, 3.4761, 2.4761], [2.0, 3.5239, 2.7543], [2.0, 3.7543, 2.5239], [2.0, 3.0, 2.0], [2.4636, 3.0, 2.0], [2.0, 3.0, 2.4636], [2.5364, 3.5364, 2.5364], [2.0, 3.4636, 2.0], [3.75, 2.25, 2.5], [3.25, 2.75, 2.5], [3.5, 2.25, 2.75], [3.5, 2.75, 2.25], [3.25, 2.5, 2.75], [3.75, 2.5, 2.25], [3.785, 2.785, 2.0], [3.215, 2.215, 2.0], [3.0, 2.215, 2.215], [3.0, 2.785, 2.785], [3.785, 2.0, 2.785], [3.215, 2.0, 2.215], [3.5239, 2.0, 2.7543], [3.7543, 2.0, 2.5239], [3.4761, 2.2304, 2.4761], [3.2457, 2.7696, 2.2457], [3.5239, 2.7543, 2.0], [3.7543, 2.5239, 2.0], [3.2457, 2.2457, 2.7696], [3.4761, 2.4761, 2.2304], [3.7696, 2.2457, 2.2457], [3.2304, 2.4761, 2.4761], [3.0, 2.5239, 2.7543], [3.0, 2.7543, 2.5239], [3.0, 2.0, 2.0], [3.4636, 2.0, 2.0], [3.0, 2.0, 2.4636], [3.5364, 2.5364, 2.5364], [3.0, 2.4636, 2.0], [3.75, 3.25, 3.5], [3.25, 3.75, 3.5], [3.5, 3.25, 3.75], [3.5, 3.75, 3.25], [3.25, 3.5, 3.75], [3.75, 3.5, 3.25], [3.785, 3.785, 3.0], [3.215, 3.215, 3.0], [3.0, 3.215, 3.215], [3.0, 3.785, 3.785], [3.785, 3.0, 3.785], [3.215, 3.0, 3.215], [3.5239, 3.0, 3.7543], [3.7543, 3.0, 3.5239], [3.4761, 3.2304, 3.4761], [3.2457, 3.7696, 3.2457], [3.5239, 3.7543, 3.0], [3.7543, 3.5239, 3.0], [3.2457, 3.2457, 3.7696], [3.4761, 3.4761, 3.2304], [3.7696, 3.2457, 3.2457], [3.2304, 3.4761, 3.4761], [3.0, 3.5239, 3.7543], [3.0, 3.7543, 3.5239], [3.0, 3.0, 3.0], [3.4636, 3.0, 3.0], [3.0, 3.0, 3.4636], [3.5364, 3.5364, 3.5364], [3.0, 3.4636, 3.0]], 'SC': [[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]], 'order': [81, 33, 1, 65, 49, 17, 137, 129, 97, 105, 121, 113, 274, 285, 257, 363, 219, 213, 298, 193, 333, 225, 250, 243, 385, 461, 442, 401, 451, 85, 37, 5, 69, 53, 21, 141, 133, 101, 109, 125, 117, 278, 281, 261, 367, 223, 209, 302, 197, 329, 229, 254, 247, 389, 457, 446, 405, 455, 83, 35, 3, 67, 51, 19, 139, 131, 99, 107, 123, 115, 276, 287, 259, 361, 217, 215, 300, 195, 335, 227, 252, 241, 387, 463, 444, 403, 449, 82, 34, 2, 66, 50, 18, 138, 130, 98, 106, 122, 114, 273, 286, 258, 364, 220, 214, 297, 194, 334, 226, 249, 244, 386, 462, 441, 402, 452, 43, 93, 75, 10, 29, 58, 186, 177, 145, 157, 171, 161, 371, 379, 353, 265, 314, 306, 201, 289, 233, 321, 349, 341, 393, 425, 409, 433, 417, 87, 39, 7, 71, 55, 23, 143, 135, 103, 111, 127, 119, 280, 283, 263, 365, 221, 211, 304, 199, 331, 231, 256, 245, 391, 459, 448, 407, 453, 86, 38, 6, 70, 54, 22, 142, 134, 102, 110, 126, 118, 277, 282, 262, 368, 224, 210, 301, 198, 330, 230, 253, 248, 390, 458, 445, 406, 456, 47, 89, 79, 14, 25, 62, 190, 181, 149, 153, 175, 165, 375, 383, 357, 269, 318, 310, 205, 293, 237, 325, 345, 337, 397, 429, 413, 437, 421, 84, 36, 4, 68, 52, 20, 140, 132, 100, 108, 124, 116, 275, 288, 260, 362, 218, 216, 299, 196, 336, 228, 251, 242, 388, 464, 443, 404, 450, 41, 95, 73, 12, 31, 60, 188, 179, 147, 159, 169, 163, 369, 377, 355, 267, 316, 308, 203, 291, 235, 323, 351, 343, 395, 427, 411, 435, 419, 44, 94, 76, 9, 30, 57, 185, 178, 146, 158, 172, 162, 372, 380, 354, 266, 313, 305, 202, 290, 234, 322, 350, 342, 394, 426, 410, 434, 418, 88, 40, 8, 72, 56, 24, 144, 136, 104, 112, 128, 120, 279, 284, 264, 366, 222, 212, 303, 200, 332, 232, 255, 246, 392, 460, 447, 408, 454, 45, 91, 77, 16, 27, 64, 192, 183, 151, 155, 173, 167, 373, 381, 359, 271, 320, 312, 207, 295, 239, 327, 347, 339, 399, 431, 415, 439, 423, 48, 90, 80, 13, 26, 61, 189, 182, 150, 154, 176, 166, 376, 384, 358, 270, 317, 309, 206, 294, 238, 326, 346, 338, 398, 430, 414, 438, 422, 42, 96, 74, 11, 32, 59, 187, 180, 148, 160, 170, 164, 370, 378, 356, 268, 315, 307, 204, 292, 236, 324, 352, 344, 396, 428, 412, 436, 420, 46, 92, 78, 15, 28, 63, 191, 184, 152, 156, 174, 168, 374, 382, 360, 272, 319, 311, 208, 296, 240, 328, 348, 340, 400, 432, 416, 440, 424], 'invSC': [[0.5, 0.0, 0.0], [0.0, 0.5, 0.0], [0.0, 0.0, 0.5]]}
writeSCinfo(SCinfo, "SCinfo")
haha=readSCinfo("SCinfo")
print(haha['SC'])
print(haha['invSC'])
print(haha['SCref'])
print(haha['SCpos'])
print(haha['SCmat'])
print(haha['invSCmat'])
print(haha['order'])
def readclus(file):
if check(file):
fin=open(file, "r")
nclus=int(fin.readline())
clus=[]
for i in range(nclus):
item=[]
fin.readline()
npt=int(fin.readline())
for j in range(npt):
item.append(list(map(float, fin.readline().split())))
clus.append(item)
return clus
else:
print(str(file)+" not found :(")
#writeclus(clus,"uniqueC")
#print "\n".join(map(str, readclus("uniqueC")))
def readorb(file):
if check(file):
orbset=[]
fin=open(file, "r")
Norb=int(fin.readline())
for i in range(Norb):
orb=[]
fin.readline()
nitem=int(fin.readline())
fin.readline()
for j in range(nitem):
item=[]
npt=int(fin.readline())
clus=[]
lpt=[]
for k in range(npt):
line=fin.readline()
clus.append(list(map(float,line.split())))
item.append(clus)
item.append(int(fin.readline()))
item.append(int(fin.readline()))
for k in range(npt):
line=fin.readline()
tmp=list(map(float,line.split()))
tmp=list(map(int, tmp))
lpt.append([[tmp[0],tmp[1],tmp[2]],tmp[3]])
item.append(lpt)
orb.append(item)
orbset.append(orb)
fin.close()
return orbset
else:
print(str(file)+" not found :(")
#test
if False:
orbset=[[[[[0.75, 0.25, 0.5]], 1, 1, [[[0.0, 0.0, 0.0], 1]]], [[[0.75, 0.5, 0.25]], 1, 2, [[[0.0, 0.0, 0.0], 6]]], [[[0.5, 0.25, -0.25]], 1, 3, [[[0.0, 0.0, -1.0], 3]]], [[[0.25, -0.25, -0.5]], 1, 4, [[[0.0, -1.0, -1.0], 2]]], [[[0.5, -0.25, 0.25]], 1, 5, [[[0.0, -1.0, 0.0], 4]]], [[[0.25, -0.5, -0.25]], 1, 6, [[[0.0, -1.0, -1.0], 5]]]],[[[[0.7696, 0.2457, 0.2457], [0.0, -0.215, -0.215]], 42, 1, [[[0.0, 0.0, 0.0], 21], [[0.0, -1.0, -1.0], 10]]], [[[0.5238999999999999, 0.0, -0.2457], [0.215, 0.0, 0.215]], 42, 3, [[[-0.0, 0.0, -1.0], 13], [[0.0, 0.0, 0.0], 12]]], [[[0.5238999999999999, -0.2457, 0.0], [0.215, 0.215, 0.0]], 42, 5, [[[-0.0, -1.0, 0.0], 17], [[0.0, 0.0, 0.0], 8]]], [[[-0.2457, 0.0, 0.5238999999999999], [0.215, 0.0, 0.215]], 42, 7, [[[-1.0, 0.0, -0.0], 14], [[0.0, 0.0, 0.0], 12]]], [[[0.2457, 0.2457, 0.7696], [-0.215, -0.215, 0.0]], 42, 9, [[[0.0, 0.0, 0.0], 19], [[-1.0, -1.0, 0.0], 7]]], [[[0.0, -0.2457, 0.5238999999999999], [0.0, 0.215, 0.215]], 42, 11, [[[0.0, -1.0, -0.0], 24], [[0.0, 0.0, 0.0], 9]]], [[[-0.7696, -0.5238999999999999, -0.5238999999999999], [0.0, -0.215, -0.215]], 42, 13, [[[-1.0, -1.0, -1.0], 22], [[0.0, -1.0, -1.0], 10]]], [[[-0.5238999999999999, -0.5238999999999999, -0.7696], [-0.215, -0.215, 0.0]], 42, 15, [[[-1.0, -1.0, -1.0], 20], [[-1.0, -1.0, 0.0], 7]]], [[[-0.5238999999999999, -0.7696, -0.5238999999999999], [-0.215, 0.0, -0.215]], 42, 17, [[[-1.0, -1.0, -1.0], 15], [[-1.0, 0.0, -1.0], 11]]], [[[-0.2457, 0.5238999999999999, 0.0], [0.215, 0.215, 0.0]], 42, 19, [[[-1.0, -0.0, 0.0], 18], [[0.0, 0.0, 0.0], 8]]], [[[0.2457, 0.7696, 0.2457], [-0.215, 0.0, -0.215]], 42, 21, [[[0.0, 0.0, 0.0], 16], [[-1.0, 0.0, -1.0], 11]]], [[[0.0, 0.5238999999999999, -0.2457], [0.0, 0.215, 0.215]], 42, 23, [[[0.0, -0.0, -1.0], 23], [[0.0, 0.0, 0.0], 9]]]]]
print("\n".join(map(str,orbset)))
writeorb(orbset,"test-orb")
print("\n")
print("\n".join(map(str,readorb("test-orb"))))
#def read fit.ou
def readfit(file):
if check(file):
counter=0
readflag=False
for line in open(file):
counter=counter+1
if counter==1:
nstruc=list(map(int, line.split()))[1]
fitlist=[0.0]*nstruc
if len(line.split())>=1 and (line.split())[0]=="found":
readflag=True
continue
if readflag:
index=int((line.split())[0])
resl=float((line.split())[1])
fitlist[index-1]=resl
print("Fit.our read successfully and length: "+str(len(fitlist)))
return fitlist
else:
print(str(file)+" not found :(")
#test:
if False:
print(readfit("fit.out-mu1"))
| [((252, 272), 'os.path.isfile', 'os.path.isfile', (['file'], {}), '(file)\n', (266, 272), False, 'import os\n'), ((428, 446), 'os.path.isdir', 'os.path.isdir', (['dir'], {}), '(dir)\n', (441, 446), False, 'import os\n'), ((644, 664), 'os.path.isfile', 'os.path.isfile', (['file'], {}), '(file)\n', (658, 664), False, 'import os\n'), ((826, 846), 'os.path.isfile', 'os.path.isfile', (['file'], {}), '(file)\n', (840, 846), False, 'import os\n'), ((980, 1000), 'os.path.isfile', 'os.path.isfile', (['file'], {}), '(file)\n', (994, 1000), False, 'import os\n'), ((2674, 2705), 'scipy.sparse.coo_matrix', 'scipy.sparse.coo_matrix', (['datain'], {}), '(datain)\n', (2697, 2705), False, 'import scipy\n')] |
sebtelko/pulumi-azure-native | sdk/python/pulumi_azure_native/storage/storage_account_static_website.py | 711ec021b5c73da05611c56c8a35adb0ce3244e4 | # coding=utf-8
# *** WARNING: this file was generated by the Pulumi SDK Generator. ***
# *** Do not edit by hand unless you're certain you know what you are doing! ***
import warnings
import pulumi
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
from .. import _utilities
__all__ = ['StorageAccountStaticWebsiteArgs', 'StorageAccountStaticWebsite']
@pulumi.input_type
class StorageAccountStaticWebsiteArgs:
def __init__(__self__, *,
account_name: pulumi.Input[str],
resource_group_name: pulumi.Input[str],
error404_document: Optional[pulumi.Input[str]] = None,
index_document: Optional[pulumi.Input[str]] = None):
"""
The set of arguments for constructing a StorageAccountStaticWebsite resource.
:param pulumi.Input[str] account_name: The name of the storage account within the specified resource group.
:param pulumi.Input[str] resource_group_name: The name of the resource group within the user's subscription. The name is case insensitive.
:param pulumi.Input[str] error404_document: The absolute path to a custom webpage that should be used when a request is made which does not correspond to an existing file.
:param pulumi.Input[str] index_document: The webpage that Azure Storage serves for requests to the root of a website or any sub-folder. For example, 'index.html'. The value is case-sensitive.
"""
pulumi.set(__self__, "account_name", account_name)
pulumi.set(__self__, "resource_group_name", resource_group_name)
if error404_document is not None:
pulumi.set(__self__, "error404_document", error404_document)
if index_document is not None:
pulumi.set(__self__, "index_document", index_document)
@property
@pulumi.getter(name="accountName")
def account_name(self) -> pulumi.Input[str]:
"""
The name of the storage account within the specified resource group.
"""
return pulumi.get(self, "account_name")
@account_name.setter
def account_name(self, value: pulumi.Input[str]):
pulumi.set(self, "account_name", value)
@property
@pulumi.getter(name="resourceGroupName")
def resource_group_name(self) -> pulumi.Input[str]:
"""
The name of the resource group within the user's subscription. The name is case insensitive.
"""
return pulumi.get(self, "resource_group_name")
@resource_group_name.setter
def resource_group_name(self, value: pulumi.Input[str]):
pulumi.set(self, "resource_group_name", value)
@property
@pulumi.getter(name="error404Document")
def error404_document(self) -> Optional[pulumi.Input[str]]:
"""
The absolute path to a custom webpage that should be used when a request is made which does not correspond to an existing file.
"""
return pulumi.get(self, "error404_document")
@error404_document.setter
def error404_document(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "error404_document", value)
@property
@pulumi.getter(name="indexDocument")
def index_document(self) -> Optional[pulumi.Input[str]]:
"""
The webpage that Azure Storage serves for requests to the root of a website or any sub-folder. For example, 'index.html'. The value is case-sensitive.
"""
return pulumi.get(self, "index_document")
@index_document.setter
def index_document(self, value: Optional[pulumi.Input[str]]):
pulumi.set(self, "index_document", value)
class StorageAccountStaticWebsite(pulumi.CustomResource):
@overload
def __init__(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
account_name: Optional[pulumi.Input[str]] = None,
error404_document: Optional[pulumi.Input[str]] = None,
index_document: Optional[pulumi.Input[str]] = None,
resource_group_name: Optional[pulumi.Input[str]] = None,
__props__=None):
"""
Enables the static website feature of a storage account.
:param str resource_name: The name of the resource.
:param pulumi.ResourceOptions opts: Options for the resource.
:param pulumi.Input[str] account_name: The name of the storage account within the specified resource group.
:param pulumi.Input[str] error404_document: The absolute path to a custom webpage that should be used when a request is made which does not correspond to an existing file.
:param pulumi.Input[str] index_document: The webpage that Azure Storage serves for requests to the root of a website or any sub-folder. For example, 'index.html'. The value is case-sensitive.
:param pulumi.Input[str] resource_group_name: The name of the resource group within the user's subscription. The name is case insensitive.
"""
...
@overload
def __init__(__self__,
resource_name: str,
args: StorageAccountStaticWebsiteArgs,
opts: Optional[pulumi.ResourceOptions] = None):
"""
Enables the static website feature of a storage account.
:param str resource_name: The name of the resource.
:param StorageAccountStaticWebsiteArgs args: The arguments to use to populate this resource's properties.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
...
def __init__(__self__, resource_name: str, *args, **kwargs):
resource_args, opts = _utilities.get_resource_args_opts(StorageAccountStaticWebsiteArgs, pulumi.ResourceOptions, *args, **kwargs)
if resource_args is not None:
__self__._internal_init(resource_name, opts, **resource_args.__dict__)
else:
__self__._internal_init(resource_name, *args, **kwargs)
def _internal_init(__self__,
resource_name: str,
opts: Optional[pulumi.ResourceOptions] = None,
account_name: Optional[pulumi.Input[str]] = None,
error404_document: Optional[pulumi.Input[str]] = None,
index_document: Optional[pulumi.Input[str]] = None,
resource_group_name: Optional[pulumi.Input[str]] = None,
__props__=None):
if opts is None:
opts = pulumi.ResourceOptions()
if not isinstance(opts, pulumi.ResourceOptions):
raise TypeError('Expected resource options to be a ResourceOptions instance')
if opts.version is None:
opts.version = _utilities.get_version()
if opts.id is None:
if __props__ is not None:
raise TypeError('__props__ is only valid when passed in combination with a valid opts.id to get an existing resource')
__props__ = StorageAccountStaticWebsiteArgs.__new__(StorageAccountStaticWebsiteArgs)
if account_name is None and not opts.urn:
raise TypeError("Missing required property 'account_name'")
__props__.__dict__["account_name"] = account_name
__props__.__dict__["error404_document"] = error404_document
__props__.__dict__["index_document"] = index_document
if resource_group_name is None and not opts.urn:
raise TypeError("Missing required property 'resource_group_name'")
__props__.__dict__["resource_group_name"] = resource_group_name
__props__.__dict__["container_name"] = None
super(StorageAccountStaticWebsite, __self__).__init__(
'azure-native:storage:StorageAccountStaticWebsite',
resource_name,
__props__,
opts)
@staticmethod
def get(resource_name: str,
id: pulumi.Input[str],
opts: Optional[pulumi.ResourceOptions] = None) -> 'StorageAccountStaticWebsite':
"""
Get an existing StorageAccountStaticWebsite resource's state with the given name, id, and optional extra
properties used to qualify the lookup.
:param str resource_name: The unique name of the resulting resource.
:param pulumi.Input[str] id: The unique provider ID of the resource to lookup.
:param pulumi.ResourceOptions opts: Options for the resource.
"""
opts = pulumi.ResourceOptions.merge(opts, pulumi.ResourceOptions(id=id))
__props__ = StorageAccountStaticWebsiteArgs.__new__(StorageAccountStaticWebsiteArgs)
__props__.__dict__["container_name"] = None
__props__.__dict__["error404_document"] = None
__props__.__dict__["index_document"] = None
return StorageAccountStaticWebsite(resource_name, opts=opts, __props__=__props__)
@property
@pulumi.getter(name="containerName")
def container_name(self) -> pulumi.Output[str]:
"""
The name of the container to upload blobs to.
"""
return pulumi.get(self, "container_name")
@property
@pulumi.getter(name="error404Document")
def error404_document(self) -> pulumi.Output[Optional[str]]:
"""
The absolute path to a custom webpage that should be used when a request is made which does not correspond to an existing file.
"""
return pulumi.get(self, "error404_document")
@property
@pulumi.getter(name="indexDocument")
def index_document(self) -> pulumi.Output[Optional[str]]:
"""
The webpage that Azure Storage serves for requests to the root of a website or any sub-folder. For example, 'index.html'. The value is case-sensitive.
"""
return pulumi.get(self, "index_document")
| [((1858, 1891), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""accountName"""'}), "(name='accountName')\n", (1871, 1891), False, 'import pulumi\n'), ((2238, 2277), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""resourceGroupName"""'}), "(name='resourceGroupName')\n", (2251, 2277), False, 'import pulumi\n'), ((2683, 2721), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""error404Document"""'}), "(name='error404Document')\n", (2696, 2721), False, 'import pulumi\n'), ((3172, 3207), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""indexDocument"""'}), "(name='indexDocument')\n", (3185, 3207), False, 'import pulumi\n'), ((8885, 8920), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""containerName"""'}), "(name='containerName')\n", (8898, 8920), False, 'import pulumi\n'), ((9121, 9159), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""error404Document"""'}), "(name='error404Document')\n", (9134, 9159), False, 'import pulumi\n'), ((9458, 9493), 'pulumi.getter', 'pulumi.getter', ([], {'name': '"""indexDocument"""'}), "(name='indexDocument')\n", (9471, 9493), False, 'import pulumi\n'), ((1493, 1543), 'pulumi.set', 'pulumi.set', (['__self__', '"""account_name"""', 'account_name'], {}), "(__self__, 'account_name', account_name)\n", (1503, 1543), False, 'import pulumi\n'), ((1552, 1616), 'pulumi.set', 'pulumi.set', (['__self__', '"""resource_group_name"""', 'resource_group_name'], {}), "(__self__, 'resource_group_name', resource_group_name)\n", (1562, 1616), False, 'import pulumi\n'), ((2057, 2089), 'pulumi.get', 'pulumi.get', (['self', '"""account_name"""'], {}), "(self, 'account_name')\n", (2067, 2089), False, 'import pulumi\n'), ((2178, 2217), 'pulumi.set', 'pulumi.set', (['self', '"""account_name"""', 'value'], {}), "(self, 'account_name', value)\n", (2188, 2217), False, 'import pulumi\n'), ((2474, 2513), 'pulumi.get', 'pulumi.get', (['self', '"""resource_group_name"""'], {}), "(self, 'resource_group_name')\n", (2484, 2513), False, 'import pulumi\n'), ((2616, 2662), 'pulumi.set', 'pulumi.set', (['self', '"""resource_group_name"""', 'value'], {}), "(self, 'resource_group_name', value)\n", (2626, 2662), False, 'import pulumi\n'), ((2961, 2998), 'pulumi.get', 'pulumi.get', (['self', '"""error404_document"""'], {}), "(self, 'error404_document')\n", (2971, 2998), False, 'import pulumi\n'), ((3107, 3151), 'pulumi.set', 'pulumi.set', (['self', '"""error404_document"""', 'value'], {}), "(self, 'error404_document', value)\n", (3117, 3151), False, 'import pulumi\n'), ((3467, 3501), 'pulumi.get', 'pulumi.get', (['self', '"""index_document"""'], {}), "(self, 'index_document')\n", (3477, 3501), False, 'import pulumi\n'), ((3604, 3645), 'pulumi.set', 'pulumi.set', (['self', '"""index_document"""', 'value'], {}), "(self, 'index_document', value)\n", (3614, 3645), False, 'import pulumi\n'), ((9066, 9100), 'pulumi.get', 'pulumi.get', (['self', '"""container_name"""'], {}), "(self, 'container_name')\n", (9076, 9100), False, 'import pulumi\n'), ((9400, 9437), 'pulumi.get', 'pulumi.get', (['self', '"""error404_document"""'], {}), "(self, 'error404_document')\n", (9410, 9437), False, 'import pulumi\n'), ((9754, 9788), 'pulumi.get', 'pulumi.get', (['self', '"""index_document"""'], {}), "(self, 'index_document')\n", (9764, 9788), False, 'import pulumi\n'), ((1671, 1731), 'pulumi.set', 'pulumi.set', (['__self__', '"""error404_document"""', 'error404_document'], {}), "(__self__, 'error404_document', error404_document)\n", (1681, 1731), False, 'import pulumi\n'), ((1783, 1837), 'pulumi.set', 'pulumi.set', (['__self__', '"""index_document"""', 'index_document'], {}), "(__self__, 'index_document', index_document)\n", (1793, 1837), False, 'import pulumi\n'), ((6485, 6509), 'pulumi.ResourceOptions', 'pulumi.ResourceOptions', ([], {}), '()\n', (6507, 6509), False, 'import pulumi\n'), ((8490, 8519), 'pulumi.ResourceOptions', 'pulumi.ResourceOptions', ([], {'id': 'id'}), '(id=id)\n', (8512, 8519), False, 'import pulumi\n')] |
onehitcombo/aws-doc-sdk-examples | python/example_code/s3/s3-python-example-get-bucket-policy.py | 03e2e0c5dee75c5decbbb99e849c51417521fd82 | # Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file 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 boto3
# Create an S3 client
s3 = boto3.client('s3')
# Call to S3 to retrieve the policy for the given bucket
result = s3.get_bucket_policy(Bucket='my-bucket')
print(result)
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
# snippet-sourcedescription:[s3-python-example-get-bucket-policy.py demonstrates how to list the Amazon S3 Buckets in your account.]
# snippet-keyword:[Python]
# snippet-keyword:[AWS SDK for Python (Boto3)]
# snippet-keyword:[Code Sample]
# snippet-keyword:[Amazon S3]
# snippet-service:[s3]
# snippet-sourcetype:[full-example]
# snippet-sourcedate:[2018-06-25]
# snippet-sourceauthor:[jschwarzwalder (AWS)]
| [((587, 605), 'boto3.client', 'boto3.client', (['"""s3"""'], {}), "('s3')\n", (599, 605), False, 'import boto3\n')] |
AIngleLab/aae | lang/py/aingle/test/gen_interop_data.py | 6e95f89fad60e62bb5305afe97c72f3278d8e04b | #!/usr/bin/env python3
##
# 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
#
#
#
# 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 os
import sys
import aingle.codecs
import aingle.datafile
import aingle.io
import aingle.schema
NULL_CODEC = "null"
CODECS_TO_VALIDATE = aingle.codecs.KNOWN_CODECS.keys()
DATUM = {
"intField": 12,
"longField": 15234324,
"stringField": "hey",
"boolField": True,
"floatField": 1234.0,
"doubleField": -1234.0,
"bytesField": b"12312adf",
"nullField": None,
"arrayField": [5.0, 0.0, 12.0],
"mapField": {"a": {"label": "a"}, "bee": {"label": "cee"}},
"unionField": 12.0,
"enumField": "C",
"fixedField": b"1019181716151413",
"recordField": {"label": "blah", "children": [{"label": "inner", "children": []}]},
}
def generate(schema_path, output_path):
with open(schema_path) as schema_file:
interop_schema = aingle.schema.parse(schema_file.read())
for codec in CODECS_TO_VALIDATE:
filename = output_path
if codec != NULL_CODEC:
base, ext = os.path.splitext(output_path)
filename = base + "_" + codec + ext
with aingle.datafile.DataFileWriter(open(filename, "wb"), aingle.io.DatumWriter(), interop_schema, codec=codec) as dfw:
dfw.append(DATUM)
if __name__ == "__main__":
generate(sys.argv[1], sys.argv[2])
| [((1716, 1745), 'os.path.splitext', 'os.path.splitext', (['output_path'], {}), '(output_path)\n', (1732, 1745), False, 'import os\n')] |
Rekrau/PyGreentea | data_io/util/value_blob_erosion.py | 457d7dc5be12b15c3c7663ceaf6d74301de56e43 | import numpy as np
from scipy import ndimage
def erode_value_blobs(array, steps=1, values_to_ignore=tuple(), new_value=0):
unique_values = list(np.unique(array))
all_entries_to_keep = np.zeros(shape=array.shape, dtype=np.bool)
for unique_value in unique_values:
entries_of_this_value = array == unique_value
if unique_value in values_to_ignore:
all_entries_to_keep = np.logical_or(entries_of_this_value, all_entries_to_keep)
else:
eroded_unique_indicator = ndimage.binary_erosion(entries_of_this_value, iterations=steps)
all_entries_to_keep = np.logical_or(eroded_unique_indicator, all_entries_to_keep)
result = array * all_entries_to_keep
if new_value != 0:
eroded_entries = np.logical_not(all_entries_to_keep)
new_values = new_value * eroded_entries
result += new_values
return result
| [((194, 236), 'numpy.zeros', 'np.zeros', ([], {'shape': 'array.shape', 'dtype': 'np.bool'}), '(shape=array.shape, dtype=np.bool)\n', (202, 236), True, 'import numpy as np\n'), ((150, 166), 'numpy.unique', 'np.unique', (['array'], {}), '(array)\n', (159, 166), True, 'import numpy as np\n'), ((766, 801), 'numpy.logical_not', 'np.logical_not', (['all_entries_to_keep'], {}), '(all_entries_to_keep)\n', (780, 801), True, 'import numpy as np\n'), ((409, 466), 'numpy.logical_or', 'np.logical_or', (['entries_of_this_value', 'all_entries_to_keep'], {}), '(entries_of_this_value, all_entries_to_keep)\n', (422, 466), True, 'import numpy as np\n'), ((519, 582), 'scipy.ndimage.binary_erosion', 'ndimage.binary_erosion', (['entries_of_this_value'], {'iterations': 'steps'}), '(entries_of_this_value, iterations=steps)\n', (541, 582), False, 'from scipy import ndimage\n'), ((617, 676), 'numpy.logical_or', 'np.logical_or', (['eroded_unique_indicator', 'all_entries_to_keep'], {}), '(eroded_unique_indicator, all_entries_to_keep)\n', (630, 676), True, 'import numpy as np\n')] |
EnjoyLifeFund/macHighSierra-py36-pkgs | astropy/units/tests/test_logarithmic.py | 5668b5785296b314ea1321057420bcd077dba9ea | # coding: utf-8
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Test the Logarithmic Units and Quantities
"""
from __future__ import (absolute_import, unicode_literals, division,
print_function)
from ...extern import six
from ...extern.six.moves import zip
import pickle
import itertools
import pytest
import numpy as np
from numpy.testing.utils import assert_allclose
from ...tests.helper import assert_quantity_allclose
from ... import units as u, constants as c
lu_units = [u.dex, u.mag, u.decibel]
lu_subclasses = [u.DexUnit, u.MagUnit, u.DecibelUnit]
lq_subclasses = [u.Dex, u.Magnitude, u.Decibel]
pu_sample = (u.dimensionless_unscaled, u.m, u.g/u.s**2, u.Jy)
class TestLogUnitCreation(object):
def test_logarithmic_units(self):
"""Check logarithmic units are set up correctly."""
assert u.dB.to(u.dex) == 0.1
assert u.dex.to(u.mag) == -2.5
assert u.mag.to(u.dB) == -4
@pytest.mark.parametrize('lu_unit, lu_cls', zip(lu_units, lu_subclasses))
def test_callable_units(self, lu_unit, lu_cls):
assert isinstance(lu_unit, u.UnitBase)
assert callable(lu_unit)
assert lu_unit._function_unit_class is lu_cls
@pytest.mark.parametrize('lu_unit', lu_units)
def test_equality_to_normal_unit_for_dimensionless(self, lu_unit):
lu = lu_unit()
assert lu == lu._default_function_unit # eg, MagUnit() == u.mag
assert lu._default_function_unit == lu # and u.mag == MagUnit()
@pytest.mark.parametrize('lu_unit, physical_unit',
itertools.product(lu_units, pu_sample))
def test_call_units(self, lu_unit, physical_unit):
"""Create a LogUnit subclass using the callable unit and physical unit,
and do basic check that output is right."""
lu1 = lu_unit(physical_unit)
assert lu1.physical_unit == physical_unit
assert lu1.function_unit == lu1._default_function_unit
def test_call_invalid_unit(self):
with pytest.raises(TypeError):
u.mag([])
with pytest.raises(ValueError):
u.mag(u.mag())
@pytest.mark.parametrize('lu_cls, physical_unit', itertools.product(
lu_subclasses + [u.LogUnit], pu_sample))
def test_subclass_creation(self, lu_cls, physical_unit):
"""Create a LogUnit subclass object for given physical unit,
and do basic check that output is right."""
lu1 = lu_cls(physical_unit)
assert lu1.physical_unit == physical_unit
assert lu1.function_unit == lu1._default_function_unit
lu2 = lu_cls(physical_unit,
function_unit=2*lu1._default_function_unit)
assert lu2.physical_unit == physical_unit
assert lu2.function_unit == u.Unit(2*lu2._default_function_unit)
with pytest.raises(ValueError):
lu_cls(physical_unit, u.m)
def test_predefined_magnitudes():
assert_quantity_allclose((-21.1*u.STmag).physical,
1.*u.erg/u.cm**2/u.s/u.AA)
assert_quantity_allclose((-48.6*u.ABmag).physical,
1.*u.erg/u.cm**2/u.s/u.Hz)
assert_quantity_allclose((0*u.M_bol).physical, c.L_bol0)
assert_quantity_allclose((0*u.m_bol).physical,
c.L_bol0/(4.*np.pi*(10.*c.pc)**2))
def test_predefined_reinitialisation():
assert u.mag('ST') == u.STmag
assert u.mag('AB') == u.ABmag
assert u.mag('Bol') == u.M_bol
assert u.mag('bol') == u.m_bol
def test_predefined_string_roundtrip():
"""Ensure roundtripping; see #5015"""
with u.magnitude_zero_points.enable():
assert u.Unit(u.STmag.to_string()) == u.STmag
assert u.Unit(u.ABmag.to_string()) == u.ABmag
assert u.Unit(u.M_bol.to_string()) == u.M_bol
assert u.Unit(u.m_bol.to_string()) == u.m_bol
def test_inequality():
"""Check __ne__ works (regresssion for #5342)."""
lu1 = u.mag(u.Jy)
lu2 = u.dex(u.Jy)
lu3 = u.mag(u.Jy**2)
lu4 = lu3 - lu1
assert lu1 != lu2
assert lu1 != lu3
assert lu1 == lu4
class TestLogUnitStrings(object):
def test_str(self):
"""Do some spot checks that str, repr, etc. work as expected."""
lu1 = u.mag(u.Jy)
assert str(lu1) == 'mag(Jy)'
assert repr(lu1) == 'Unit("mag(Jy)")'
assert lu1.to_string('generic') == 'mag(Jy)'
with pytest.raises(ValueError):
lu1.to_string('fits')
lu2 = u.dex()
assert str(lu2) == 'dex'
assert repr(lu2) == 'Unit("dex(1)")'
assert lu2.to_string() == 'dex(1)'
lu3 = u.MagUnit(u.Jy, function_unit=2*u.mag)
assert str(lu3) == '2 mag(Jy)'
assert repr(lu3) == 'MagUnit("Jy", unit="2 mag")'
assert lu3.to_string() == '2 mag(Jy)'
lu4 = u.mag(u.ct)
assert lu4.to_string('generic') == 'mag(ct)'
assert lu4.to_string('latex') == ('$\\mathrm{mag}$$\\mathrm{\\left( '
'\\mathrm{ct} \\right)}$')
assert lu4._repr_latex_() == lu4.to_string('latex')
class TestLogUnitConversion(object):
@pytest.mark.parametrize('lu_unit, physical_unit',
itertools.product(lu_units, pu_sample))
def test_physical_unit_conversion(self, lu_unit, physical_unit):
"""Check various LogUnit subclasses are equivalent and convertible
to their non-log counterparts."""
lu1 = lu_unit(physical_unit)
assert lu1.is_equivalent(physical_unit)
assert lu1.to(physical_unit, 0.) == 1.
assert physical_unit.is_equivalent(lu1)
assert physical_unit.to(lu1, 1.) == 0.
pu = u.Unit(8.*physical_unit)
assert lu1.is_equivalent(physical_unit)
assert lu1.to(pu, 0.) == 0.125
assert pu.is_equivalent(lu1)
assert_allclose(pu.to(lu1, 0.125), 0., atol=1.e-15)
# Check we round-trip.
value = np.linspace(0., 10., 6)
assert_allclose(pu.to(lu1, lu1.to(pu, value)), value, atol=1.e-15)
# And that we're not just returning True all the time.
pu2 = u.g
assert not lu1.is_equivalent(pu2)
with pytest.raises(u.UnitsError):
lu1.to(pu2)
assert not pu2.is_equivalent(lu1)
with pytest.raises(u.UnitsError):
pu2.to(lu1)
@pytest.mark.parametrize('lu_unit', lu_units)
def test_container_unit_conversion(self, lu_unit):
"""Check that conversion to logarithmic units (u.mag, u.dB, u.dex)
is only possible when the physical unit is dimensionless."""
values = np.linspace(0., 10., 6)
lu1 = lu_unit(u.dimensionless_unscaled)
assert lu1.is_equivalent(lu1.function_unit)
assert_allclose(lu1.to(lu1.function_unit, values), values)
lu2 = lu_unit(u.Jy)
assert not lu2.is_equivalent(lu2.function_unit)
with pytest.raises(u.UnitsError):
lu2.to(lu2.function_unit, values)
@pytest.mark.parametrize(
'flu_unit, tlu_unit, physical_unit',
itertools.product(lu_units, lu_units, pu_sample))
def test_subclass_conversion(self, flu_unit, tlu_unit, physical_unit):
"""Check various LogUnit subclasses are equivalent and convertible
to each other if they correspond to equivalent physical units."""
values = np.linspace(0., 10., 6)
flu = flu_unit(physical_unit)
tlu = tlu_unit(physical_unit)
assert flu.is_equivalent(tlu)
assert_allclose(flu.to(tlu), flu.function_unit.to(tlu.function_unit))
assert_allclose(flu.to(tlu, values),
values * flu.function_unit.to(tlu.function_unit))
tlu2 = tlu_unit(u.Unit(100.*physical_unit))
assert flu.is_equivalent(tlu2)
# Check that we round-trip.
assert_allclose(flu.to(tlu2, tlu2.to(flu, values)), values, atol=1.e-15)
tlu3 = tlu_unit(physical_unit.to_system(u.si)[0])
assert flu.is_equivalent(tlu3)
assert_allclose(flu.to(tlu3, tlu3.to(flu, values)), values, atol=1.e-15)
tlu4 = tlu_unit(u.g)
assert not flu.is_equivalent(tlu4)
with pytest.raises(u.UnitsError):
flu.to(tlu4, values)
def test_unit_decomposition(self):
lu = u.mag(u.Jy)
assert lu.decompose() == u.mag(u.Jy.decompose())
assert lu.decompose().physical_unit.bases == [u.kg, u.s]
assert lu.si == u.mag(u.Jy.si)
assert lu.si.physical_unit.bases == [u.kg, u.s]
assert lu.cgs == u.mag(u.Jy.cgs)
assert lu.cgs.physical_unit.bases == [u.g, u.s]
def test_unit_multiple_possible_equivalencies(self):
lu = u.mag(u.Jy)
assert lu.is_equivalent(pu_sample)
class TestLogUnitArithmetic(object):
def test_multiplication_division(self):
"""Check that multiplication/division with other units is only
possible when the physical unit is dimensionless, and that this
turns the unit into a normal one."""
lu1 = u.mag(u.Jy)
with pytest.raises(u.UnitsError):
lu1 * u.m
with pytest.raises(u.UnitsError):
u.m * lu1
with pytest.raises(u.UnitsError):
lu1 / lu1
for unit in (u.dimensionless_unscaled, u.m, u.mag, u.dex):
with pytest.raises(u.UnitsError):
lu1 / unit
lu2 = u.mag(u.dimensionless_unscaled)
with pytest.raises(u.UnitsError):
lu2 * lu1
with pytest.raises(u.UnitsError):
lu2 / lu1
# But dimensionless_unscaled can be cancelled.
assert lu2 / lu2 == u.dimensionless_unscaled
# With dimensionless, normal units are OK, but we return a plain unit.
tf = lu2 * u.m
tr = u.m * lu2
for t in (tf, tr):
assert not isinstance(t, type(lu2))
assert t == lu2.function_unit * u.m
with u.set_enabled_equivalencies(u.logarithmic()):
with pytest.raises(u.UnitsError):
t.to(lu2.physical_unit)
# Now we essentially have a LogUnit with a prefactor of 100,
# so should be equivalent again.
t = tf / u.cm
with u.set_enabled_equivalencies(u.logarithmic()):
assert t.is_equivalent(lu2.function_unit)
assert_allclose(t.to(u.dimensionless_unscaled, np.arange(3.)/100.),
lu2.to(lu2.physical_unit, np.arange(3.)))
# If we effectively remove lu1, a normal unit should be returned.
t2 = tf / lu2
assert not isinstance(t2, type(lu2))
assert t2 == u.m
t3 = tf / lu2.function_unit
assert not isinstance(t3, type(lu2))
assert t3 == u.m
# For completeness, also ensure non-sensical operations fail
with pytest.raises(TypeError):
lu1 * object()
with pytest.raises(TypeError):
slice(None) * lu1
with pytest.raises(TypeError):
lu1 / []
with pytest.raises(TypeError):
1 / lu1
@pytest.mark.parametrize('power', (2, 0.5, 1, 0))
def test_raise_to_power(self, power):
"""Check that raising LogUnits to some power is only possible when the
physical unit is dimensionless, and that conversion is turned off when
the resulting logarithmic unit (such as mag**2) is incompatible."""
lu1 = u.mag(u.Jy)
if power == 0:
assert lu1 ** power == u.dimensionless_unscaled
elif power == 1:
assert lu1 ** power == lu1
else:
with pytest.raises(u.UnitsError):
lu1 ** power
# With dimensionless, though, it works, but returns a normal unit.
lu2 = u.mag(u.dimensionless_unscaled)
t = lu2**power
if power == 0:
assert t == u.dimensionless_unscaled
elif power == 1:
assert t == lu2
else:
assert not isinstance(t, type(lu2))
assert t == lu2.function_unit**power
# also check we roundtrip
t2 = t**(1./power)
assert t2 == lu2.function_unit
with u.set_enabled_equivalencies(u.logarithmic()):
assert_allclose(t2.to(u.dimensionless_unscaled, np.arange(3.)),
lu2.to(lu2.physical_unit, np.arange(3.)))
@pytest.mark.parametrize('other', pu_sample)
def test_addition_subtraction_to_normal_units_fails(self, other):
lu1 = u.mag(u.Jy)
with pytest.raises(u.UnitsError):
lu1 + other
with pytest.raises(u.UnitsError):
lu1 - other
with pytest.raises(u.UnitsError):
other - lu1
def test_addition_subtraction_to_non_units_fails(self):
lu1 = u.mag(u.Jy)
with pytest.raises(TypeError):
lu1 + 1.
with pytest.raises(TypeError):
lu1 - [1., 2., 3.]
@pytest.mark.parametrize(
'other', (u.mag, u.mag(), u.mag(u.Jy), u.mag(u.m),
u.Unit(2*u.mag), u.MagUnit('', 2.*u.mag)))
def test_addition_subtraction(self, other):
"""Check physical units are changed appropriately"""
lu1 = u.mag(u.Jy)
other_pu = getattr(other, 'physical_unit', u.dimensionless_unscaled)
lu_sf = lu1 + other
assert lu_sf.is_equivalent(lu1.physical_unit * other_pu)
lu_sr = other + lu1
assert lu_sr.is_equivalent(lu1.physical_unit * other_pu)
lu_df = lu1 - other
assert lu_df.is_equivalent(lu1.physical_unit / other_pu)
lu_dr = other - lu1
assert lu_dr.is_equivalent(other_pu / lu1.physical_unit)
def test_complicated_addition_subtraction(self):
"""for fun, a more complicated example of addition and subtraction"""
dm0 = u.Unit('DM', 1./(4.*np.pi*(10.*u.pc)**2))
lu_dm = u.mag(dm0)
lu_absST = u.STmag - lu_dm
assert lu_absST.is_equivalent(u.erg/u.s/u.AA)
def test_neg_pos(self):
lu1 = u.mag(u.Jy)
neg_lu = -lu1
assert neg_lu != lu1
assert neg_lu.physical_unit == u.Jy**-1
assert -neg_lu == lu1
pos_lu = +lu1
assert pos_lu is not lu1
assert pos_lu == lu1
def test_pickle():
lu1 = u.dex(u.cm/u.s**2)
s = pickle.dumps(lu1)
lu2 = pickle.loads(s)
assert lu1 == lu2
def test_hashable():
lu1 = u.dB(u.mW)
lu2 = u.dB(u.m)
lu3 = u.dB(u.mW)
assert hash(lu1) != hash(lu2)
assert hash(lu1) == hash(lu3)
luset = {lu1, lu2, lu3}
assert len(luset) == 2
class TestLogQuantityCreation(object):
@pytest.mark.parametrize('lq, lu', zip(lq_subclasses + [u.LogQuantity],
lu_subclasses + [u.LogUnit]))
def test_logarithmic_quantities(self, lq, lu):
"""Check logarithmic quantities are all set up correctly"""
assert lq._unit_class == lu
assert type(lu()._quantity_class(1.)) is lq
@pytest.mark.parametrize('lq_cls, physical_unit',
itertools.product(lq_subclasses, pu_sample))
def test_subclass_creation(self, lq_cls, physical_unit):
"""Create LogQuantity subclass objects for some physical units,
and basic check on transformations"""
value = np.arange(1., 10.)
log_q = lq_cls(value * physical_unit)
assert log_q.unit.physical_unit == physical_unit
assert log_q.unit.function_unit == log_q.unit._default_function_unit
assert_allclose(log_q.physical.value, value)
with pytest.raises(ValueError):
lq_cls(value, physical_unit)
@pytest.mark.parametrize(
'unit', (u.mag, u.mag(), u.mag(u.Jy), u.mag(u.m),
u.Unit(2*u.mag), u.MagUnit('', 2.*u.mag),
u.MagUnit(u.Jy, -1*u.mag), u.MagUnit(u.m, -2.*u.mag)))
def test_different_units(self, unit):
q = u.Magnitude(1.23, unit)
assert q.unit.function_unit == getattr(unit, 'function_unit', unit)
assert q.unit.physical_unit is getattr(unit, 'physical_unit',
u.dimensionless_unscaled)
@pytest.mark.parametrize('value, unit', (
(1.*u.mag(u.Jy), None),
(1.*u.dex(u.Jy), None),
(1.*u.mag(u.W/u.m**2/u.Hz), u.mag(u.Jy)),
(1.*u.dex(u.W/u.m**2/u.Hz), u.mag(u.Jy))))
def test_function_values(self, value, unit):
lq = u.Magnitude(value, unit)
assert lq == value
assert lq.unit.function_unit == u.mag
assert lq.unit.physical_unit == getattr(unit, 'physical_unit',
value.unit.physical_unit)
@pytest.mark.parametrize(
'unit', (u.mag(), u.mag(u.Jy), u.mag(u.m), u.MagUnit('', 2.*u.mag),
u.MagUnit(u.Jy, -1*u.mag), u.MagUnit(u.m, -2.*u.mag)))
def test_indirect_creation(self, unit):
q1 = 2.5 * unit
assert isinstance(q1, u.Magnitude)
assert q1.value == 2.5
assert q1.unit == unit
pv = 100. * unit.physical_unit
q2 = unit * pv
assert q2.unit == unit
assert q2.unit.physical_unit == pv.unit
assert q2.to_value(unit.physical_unit) == 100.
assert (q2._function_view / u.mag).to_value(1) == -5.
q3 = unit / 0.4
assert q3 == q1
def test_from_view(self):
# Cannot view a physical quantity as a function quantity, since the
# values would change.
q = [100., 1000.] * u.cm/u.s**2
with pytest.raises(TypeError):
q.view(u.Dex)
# But fine if we have the right magnitude.
q = [2., 3.] * u.dex
lq = q.view(u.Dex)
assert isinstance(lq, u.Dex)
assert lq.unit.physical_unit == u.dimensionless_unscaled
assert np.all(q == lq)
def test_using_quantity_class(self):
"""Check that we can use Quantity if we have subok=True"""
# following issue #5851
lu = u.dex(u.AA)
with pytest.raises(u.UnitTypeError):
u.Quantity(1., lu)
q = u.Quantity(1., lu, subok=True)
assert type(q) is lu._quantity_class
def test_conversion_to_and_from_physical_quantities():
"""Ensures we can convert from regular quantities."""
mst = [10., 12., 14.] * u.STmag
flux_lambda = mst.physical
mst_roundtrip = flux_lambda.to(u.STmag)
# check we return a logquantity; see #5178.
assert isinstance(mst_roundtrip, u.Magnitude)
assert mst_roundtrip.unit == mst.unit
assert_allclose(mst_roundtrip.value, mst.value)
wave = [4956.8, 4959.55, 4962.3] * u.AA
flux_nu = mst.to(u.Jy, equivalencies=u.spectral_density(wave))
mst_roundtrip2 = flux_nu.to(u.STmag, u.spectral_density(wave))
assert isinstance(mst_roundtrip2, u.Magnitude)
assert mst_roundtrip2.unit == mst.unit
assert_allclose(mst_roundtrip2.value, mst.value)
def test_quantity_decomposition():
lq = 10.*u.mag(u.Jy)
assert lq.decompose() == lq
assert lq.decompose().unit.physical_unit.bases == [u.kg, u.s]
assert lq.si == lq
assert lq.si.unit.physical_unit.bases == [u.kg, u.s]
assert lq.cgs == lq
assert lq.cgs.unit.physical_unit.bases == [u.g, u.s]
class TestLogQuantityViews(object):
def setup(self):
self.lq = u.Magnitude(np.arange(10.) * u.Jy)
self.lq2 = u.Magnitude(np.arange(5.))
def test_value_view(self):
lq_value = self.lq.value
assert type(lq_value) is np.ndarray
lq_value[2] = -1.
assert np.all(self.lq.value == lq_value)
def test_function_view(self):
lq_fv = self.lq._function_view
assert type(lq_fv) is u.Quantity
assert lq_fv.unit is self.lq.unit.function_unit
lq_fv[3] = -2. * lq_fv.unit
assert np.all(self.lq.value == lq_fv.value)
def test_quantity_view(self):
# Cannot view as Quantity, since the unit cannot be represented.
with pytest.raises(TypeError):
self.lq.view(u.Quantity)
# But a dimensionless one is fine.
q2 = self.lq2.view(u.Quantity)
assert q2.unit is u.mag
assert np.all(q2.value == self.lq2.value)
lq3 = q2.view(u.Magnitude)
assert type(lq3.unit) is u.MagUnit
assert lq3.unit.physical_unit == u.dimensionless_unscaled
assert np.all(lq3 == self.lq2)
class TestLogQuantitySlicing(object):
def test_item_get_and_set(self):
lq1 = u.Magnitude(np.arange(1., 11.)*u.Jy)
assert lq1[9] == u.Magnitude(10.*u.Jy)
lq1[2] = 100.*u.Jy
assert lq1[2] == u.Magnitude(100.*u.Jy)
with pytest.raises(u.UnitsError):
lq1[2] = 100.*u.m
with pytest.raises(u.UnitsError):
lq1[2] = 100.*u.mag
with pytest.raises(u.UnitsError):
lq1[2] = u.Magnitude(100.*u.m)
assert lq1[2] == u.Magnitude(100.*u.Jy)
def test_slice_get_and_set(self):
lq1 = u.Magnitude(np.arange(1., 10.)*u.Jy)
lq1[2:4] = 100.*u.Jy
assert np.all(lq1[2:4] == u.Magnitude(100.*u.Jy))
with pytest.raises(u.UnitsError):
lq1[2:4] = 100.*u.m
with pytest.raises(u.UnitsError):
lq1[2:4] = 100.*u.mag
with pytest.raises(u.UnitsError):
lq1[2:4] = u.Magnitude(100.*u.m)
assert np.all(lq1[2] == u.Magnitude(100.*u.Jy))
class TestLogQuantityArithmetic(object):
def test_multiplication_division(self):
"""Check that multiplication/division with other quantities is only
possible when the physical unit is dimensionless, and that this turns
the result into a normal quantity."""
lq = u.Magnitude(np.arange(1., 11.)*u.Jy)
with pytest.raises(u.UnitsError):
lq * (1.*u.m)
with pytest.raises(u.UnitsError):
(1.*u.m) * lq
with pytest.raises(u.UnitsError):
lq / lq
for unit in (u.m, u.mag, u.dex):
with pytest.raises(u.UnitsError):
lq / unit
lq2 = u.Magnitude(np.arange(1, 11.))
with pytest.raises(u.UnitsError):
lq2 * lq
with pytest.raises(u.UnitsError):
lq2 / lq
with pytest.raises(u.UnitsError):
lq / lq2
# but dimensionless_unscaled can be cancelled
r = lq2 / u.Magnitude(2.)
assert r.unit == u.dimensionless_unscaled
assert np.all(r.value == lq2.value/2.)
# with dimensionless, normal units OK, but return normal quantities
tf = lq2 * u.m
tr = u.m * lq2
for t in (tf, tr):
assert not isinstance(t, type(lq2))
assert t.unit == lq2.unit.function_unit * u.m
with u.set_enabled_equivalencies(u.logarithmic()):
with pytest.raises(u.UnitsError):
t.to(lq2.unit.physical_unit)
t = tf / (50.*u.cm)
# now we essentially have the same quantity but with a prefactor of 2
assert t.unit.is_equivalent(lq2.unit.function_unit)
assert_allclose(t.to(lq2.unit.function_unit), lq2._function_view*2)
@pytest.mark.parametrize('power', (2, 0.5, 1, 0))
def test_raise_to_power(self, power):
"""Check that raising LogQuantities to some power is only possible when
the physical unit is dimensionless, and that conversion is turned off
when the resulting logarithmic unit (say, mag**2) is incompatible."""
lq = u.Magnitude(np.arange(1., 4.)*u.Jy)
if power == 0:
assert np.all(lq ** power == 1.)
elif power == 1:
assert np.all(lq ** power == lq)
else:
with pytest.raises(u.UnitsError):
lq ** power
# with dimensionless, it works, but falls back to normal quantity
# (except for power=1)
lq2 = u.Magnitude(np.arange(10.))
t = lq2**power
if power == 0:
assert t.unit is u.dimensionless_unscaled
assert np.all(t.value == 1.)
elif power == 1:
assert np.all(t == lq2)
else:
assert not isinstance(t, type(lq2))
assert t.unit == lq2.unit.function_unit ** power
with u.set_enabled_equivalencies(u.logarithmic()):
with pytest.raises(u.UnitsError):
t.to(u.dimensionless_unscaled)
def test_error_on_lq_as_power(self):
lq = u.Magnitude(np.arange(1., 4.)*u.Jy)
with pytest.raises(TypeError):
lq ** lq
@pytest.mark.parametrize('other', pu_sample)
def test_addition_subtraction_to_normal_units_fails(self, other):
lq = u.Magnitude(np.arange(1., 10.)*u.Jy)
q = 1.23 * other
with pytest.raises(u.UnitsError):
lq + q
with pytest.raises(u.UnitsError):
lq - q
with pytest.raises(u.UnitsError):
q - lq
@pytest.mark.parametrize(
'other', (1.23 * u.mag, 2.34 * u.mag(),
u.Magnitude(3.45 * u.Jy), u.Magnitude(4.56 * u.m),
5.67 * u.Unit(2*u.mag), u.Magnitude(6.78, 2.*u.mag)))
def test_addition_subtraction(self, other):
"""Check that addition/subtraction with quantities with magnitude or
MagUnit units works, and that it changes the physical units
appropriately."""
lq = u.Magnitude(np.arange(1., 10.)*u.Jy)
other_physical = other.to(getattr(other.unit, 'physical_unit',
u.dimensionless_unscaled),
equivalencies=u.logarithmic())
lq_sf = lq + other
assert_allclose(lq_sf.physical, lq.physical * other_physical)
lq_sr = other + lq
assert_allclose(lq_sr.physical, lq.physical * other_physical)
lq_df = lq - other
assert_allclose(lq_df.physical, lq.physical / other_physical)
lq_dr = other - lq
assert_allclose(lq_dr.physical, other_physical / lq.physical)
@pytest.mark.parametrize('other', pu_sample)
def test_inplace_addition_subtraction_unit_checks(self, other):
lu1 = u.mag(u.Jy)
lq1 = u.Magnitude(np.arange(1., 10.), lu1)
with pytest.raises(u.UnitsError):
lq1 += other
assert np.all(lq1.value == np.arange(1., 10.))
assert lq1.unit == lu1
with pytest.raises(u.UnitsError):
lq1 -= other
assert np.all(lq1.value == np.arange(1., 10.))
assert lq1.unit == lu1
@pytest.mark.parametrize(
'other', (1.23 * u.mag, 2.34 * u.mag(),
u.Magnitude(3.45 * u.Jy), u.Magnitude(4.56 * u.m),
5.67 * u.Unit(2*u.mag), u.Magnitude(6.78, 2.*u.mag)))
def test_inplace_addition_subtraction(self, other):
"""Check that inplace addition/subtraction with quantities with
magnitude or MagUnit units works, and that it changes the physical
units appropriately."""
lq = u.Magnitude(np.arange(1., 10.)*u.Jy)
other_physical = other.to(getattr(other.unit, 'physical_unit',
u.dimensionless_unscaled),
equivalencies=u.logarithmic())
lq_sf = lq.copy()
lq_sf += other
assert_allclose(lq_sf.physical, lq.physical * other_physical)
lq_df = lq.copy()
lq_df -= other
assert_allclose(lq_df.physical, lq.physical / other_physical)
def test_complicated_addition_subtraction(self):
"""For fun, a more complicated example of addition and subtraction."""
dm0 = u.Unit('DM', 1./(4.*np.pi*(10.*u.pc)**2))
DMmag = u.mag(dm0)
m_st = 10. * u.STmag
dm = 5. * DMmag
M_st = m_st - dm
assert M_st.unit.is_equivalent(u.erg/u.s/u.AA)
assert np.abs(M_st.physical /
(m_st.physical*4.*np.pi*(100.*u.pc)**2) - 1.) < 1.e-15
class TestLogQuantityComparisons(object):
def test_comparison_to_non_quantities_fails(self):
lq = u.Magnitude(np.arange(1., 10.)*u.Jy)
# On python2, ordering operations always succeed, given essentially
# meaningless results.
if not six.PY2:
with pytest.raises(TypeError):
lq > 'a'
assert not (lq == 'a')
assert lq != 'a'
def test_comparison(self):
lq1 = u.Magnitude(np.arange(1., 4.)*u.Jy)
lq2 = u.Magnitude(2.*u.Jy)
assert np.all((lq1 > lq2) == np.array([True, False, False]))
assert np.all((lq1 == lq2) == np.array([False, True, False]))
lq3 = u.Dex(2.*u.Jy)
assert np.all((lq1 > lq3) == np.array([True, False, False]))
assert np.all((lq1 == lq3) == np.array([False, True, False]))
lq4 = u.Magnitude(2.*u.m)
assert not (lq1 == lq4)
assert lq1 != lq4
with pytest.raises(u.UnitsError):
lq1 < lq4
q5 = 1.5 * u.Jy
assert np.all((lq1 > q5) == np.array([True, False, False]))
assert np.all((q5 < lq1) == np.array([True, False, False]))
with pytest.raises(u.UnitsError):
lq1 >= 2.*u.m
with pytest.raises(u.UnitsError):
lq1 <= lq1.value * u.mag
# For physically dimensionless, we can compare with the function unit.
lq6 = u.Magnitude(np.arange(1., 4.))
fv6 = lq6.value * u.mag
assert np.all(lq6 == fv6)
# but not some arbitrary unit, of course.
with pytest.raises(u.UnitsError):
lq6 < 2.*u.m
class TestLogQuantityMethods(object):
def setup(self):
self.mJy = np.arange(1., 5.).reshape(2, 2) * u.mag(u.Jy)
self.m1 = np.arange(1., 5.5, 0.5).reshape(3, 3) * u.mag()
self.mags = (self.mJy, self.m1)
@pytest.mark.parametrize('method', ('mean', 'min', 'max', 'round', 'trace',
'std', 'var', 'ptp', 'diff', 'ediff1d'))
def test_always_ok(self, method):
for mag in self.mags:
res = getattr(mag, method)()
assert np.all(res.value ==
getattr(mag._function_view, method)().value)
if method in ('std', 'ptp', 'diff', 'ediff1d'):
assert res.unit == u.mag()
elif method == 'var':
assert res.unit == u.mag**2
else:
assert res.unit == mag.unit
def test_clip(self):
for mag in self.mags:
assert np.all(mag.clip(2. * mag.unit, 4. * mag.unit).value ==
mag.value.clip(2., 4.))
@pytest.mark.parametrize('method', ('sum', 'cumsum', 'nansum'))
def test_only_ok_if_dimensionless(self, method):
res = getattr(self.m1, method)()
assert np.all(res.value ==
getattr(self.m1._function_view, method)().value)
assert res.unit == self.m1.unit
with pytest.raises(TypeError):
getattr(self.mJy, method)()
def test_dot(self):
assert np.all(self.m1.dot(self.m1).value ==
self.m1.value.dot(self.m1.value))
@pytest.mark.parametrize('method', ('prod', 'cumprod'))
def test_never_ok(self, method):
with pytest.raises(ValueError):
getattr(self.mJy, method)()
with pytest.raises(ValueError):
getattr(self.m1, method)()
class TestLogQuantityUfuncs(object):
"""Spot checks on ufuncs."""
def setup(self):
self.mJy = np.arange(1., 5.).reshape(2, 2) * u.mag(u.Jy)
self.m1 = np.arange(1., 5.5, 0.5).reshape(3, 3) * u.mag()
self.mags = (self.mJy, self.m1)
def test_power(self):
assert np.all(np.power(self.mJy, 0.) == 1.)
assert np.all(np.power(self.m1, 1.) == self.m1)
assert np.all(np.power(self.mJy, 1.) == self.mJy)
assert np.all(np.power(self.m1, 2.) == self.m1 ** 2)
with pytest.raises(u.UnitsError):
np.power(self.mJy, 2.)
def test_not_implemented_with_physical_unit(self):
with pytest.raises(u.UnitsError):
np.square(self.mJy)
assert np.all(np.square(self.m1) == self.m1 ** 2)
| [((1241, 1285), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""lu_unit"""', 'lu_units'], {}), "('lu_unit', lu_units)\n", (1264, 1285), False, 'import pytest\n'), ((6355, 6399), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""lu_unit"""', 'lu_units'], {}), "('lu_unit', lu_units)\n", (6378, 6399), False, 'import pytest\n'), ((11055, 11103), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""power"""', '(2, 0.5, 1, 0)'], {}), "('power', (2, 0.5, 1, 0))\n", (11078, 11103), False, 'import pytest\n'), ((12360, 12403), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""other"""', 'pu_sample'], {}), "('other', pu_sample)\n", (12383, 12403), False, 'import pytest\n'), ((14287, 14304), 'pickle.dumps', 'pickle.dumps', (['lu1'], {}), '(lu1)\n', (14299, 14304), False, 'import pickle\n'), ((14315, 14330), 'pickle.loads', 'pickle.loads', (['s'], {}), '(s)\n', (14327, 14330), False, 'import pickle\n'), ((18491, 18538), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['mst_roundtrip.value', 'mst.value'], {}), '(mst_roundtrip.value, mst.value)\n', (18506, 18538), False, 'from numpy.testing.utils import assert_allclose\n'), ((18815, 18863), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['mst_roundtrip2.value', 'mst.value'], {}), '(mst_roundtrip2.value, mst.value)\n', (18830, 18863), False, 'from numpy.testing.utils import assert_allclose\n'), ((23059, 23107), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""power"""', '(2, 0.5, 1, 0)'], {}), "('power', (2, 0.5, 1, 0))\n", (23082, 23107), False, 'import pytest\n'), ((24457, 24500), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""other"""', 'pu_sample'], {}), "('other', pu_sample)\n", (24480, 24500), False, 'import pytest\n'), ((25923, 25966), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""other"""', 'pu_sample'], {}), "('other', pu_sample)\n", (25946, 25966), False, 'import pytest\n'), ((29671, 29790), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""method"""', "('mean', 'min', 'max', 'round', 'trace', 'std', 'var', 'ptp', 'diff', 'ediff1d'\n )"], {}), "('method', ('mean', 'min', 'max', 'round', 'trace',\n 'std', 'var', 'ptp', 'diff', 'ediff1d'))\n", (29694, 29790), False, 'import pytest\n'), ((30475, 30537), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""method"""', "('sum', 'cumsum', 'nansum')"], {}), "('method', ('sum', 'cumsum', 'nansum'))\n", (30498, 30537), False, 'import pytest\n'), ((30996, 31050), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""method"""', "('prod', 'cumprod')"], {}), "('method', ('prod', 'cumprod'))\n", (31019, 31050), False, 'import pytest\n'), ((1611, 1649), 'itertools.product', 'itertools.product', (['lu_units', 'pu_sample'], {}), '(lu_units, pu_sample)\n', (1628, 1649), False, 'import itertools\n'), ((2210, 2267), 'itertools.product', 'itertools.product', (['(lu_subclasses + [u.LogUnit])', 'pu_sample'], {}), '(lu_subclasses + [u.LogUnit], pu_sample)\n', (2227, 2267), False, 'import itertools\n'), ((5952, 5977), 'numpy.linspace', 'np.linspace', (['(0.0)', '(10.0)', '(6)'], {}), '(0.0, 10.0, 6)\n', (5963, 5977), True, 'import numpy as np\n'), ((5226, 5264), 'itertools.product', 'itertools.product', (['lu_units', 'pu_sample'], {}), '(lu_units, pu_sample)\n', (5243, 5264), False, 'import itertools\n'), ((6616, 6641), 'numpy.linspace', 'np.linspace', (['(0.0)', '(10.0)', '(6)'], {}), '(0.0, 10.0, 6)\n', (6627, 6641), True, 'import numpy as np\n'), ((7355, 7380), 'numpy.linspace', 'np.linspace', (['(0.0)', '(10.0)', '(6)'], {}), '(0.0, 10.0, 6)\n', (7366, 7380), True, 'import numpy as np\n'), ((7064, 7112), 'itertools.product', 'itertools.product', (['lu_units', 'lu_units', 'pu_sample'], {}), '(lu_units, lu_units, pu_sample)\n', (7081, 7112), False, 'import itertools\n'), ((15283, 15303), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (15292, 15303), True, 'import numpy as np\n'), ((15490, 15534), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['log_q.physical.value', 'value'], {}), '(log_q.physical.value, value)\n', (15505, 15534), False, 'from numpy.testing.utils import assert_allclose\n'), ((15043, 15086), 'itertools.product', 'itertools.product', (['lq_subclasses', 'pu_sample'], {}), '(lq_subclasses, pu_sample)\n', (15060, 15086), False, 'import itertools\n'), ((17775, 17790), 'numpy.all', 'np.all', (['(q == lq)'], {}), '(q == lq)\n', (17781, 17790), True, 'import numpy as np\n'), ((19493, 19526), 'numpy.all', 'np.all', (['(self.lq.value == lq_value)'], {}), '(self.lq.value == lq_value)\n', (19499, 19526), True, 'import numpy as np\n'), ((19749, 19785), 'numpy.all', 'np.all', (['(self.lq.value == lq_fv.value)'], {}), '(self.lq.value == lq_fv.value)\n', (19755, 19785), True, 'import numpy as np\n'), ((20099, 20133), 'numpy.all', 'np.all', (['(q2.value == self.lq2.value)'], {}), '(q2.value == self.lq2.value)\n', (20105, 20133), True, 'import numpy as np\n'), ((20293, 20316), 'numpy.all', 'np.all', (['(lq3 == self.lq2)'], {}), '(lq3 == self.lq2)\n', (20299, 20316), True, 'import numpy as np\n'), ((22360, 22394), 'numpy.all', 'np.all', (['(r.value == lq2.value / 2.0)'], {}), '(r.value == lq2.value / 2.0)\n', (22366, 22394), True, 'import numpy as np\n'), ((25561, 25622), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_sf.physical', '(lq.physical * other_physical)'], {}), '(lq_sf.physical, lq.physical * other_physical)\n', (25576, 25622), False, 'from numpy.testing.utils import assert_allclose\n'), ((25659, 25720), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_sr.physical', '(lq.physical * other_physical)'], {}), '(lq_sr.physical, lq.physical * other_physical)\n', (25674, 25720), False, 'from numpy.testing.utils import assert_allclose\n'), ((25757, 25818), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_df.physical', '(lq.physical / other_physical)'], {}), '(lq_df.physical, lq.physical / other_physical)\n', (25772, 25818), False, 'from numpy.testing.utils import assert_allclose\n'), ((25855, 25916), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_dr.physical', '(other_physical / lq.physical)'], {}), '(lq_dr.physical, other_physical / lq.physical)\n', (25870, 25916), False, 'from numpy.testing.utils import assert_allclose\n'), ((27188, 27249), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_sf.physical', '(lq.physical * other_physical)'], {}), '(lq_sf.physical, lq.physical * other_physical)\n', (27203, 27249), False, 'from numpy.testing.utils import assert_allclose\n'), ((27308, 27369), 'numpy.testing.utils.assert_allclose', 'assert_allclose', (['lq_df.physical', '(lq.physical / other_physical)'], {}), '(lq_df.physical, lq.physical / other_physical)\n', (27323, 27369), False, 'from numpy.testing.utils import assert_allclose\n'), ((29297, 29315), 'numpy.all', 'np.all', (['(lq6 == fv6)'], {}), '(lq6 == fv6)\n', (29303, 29315), True, 'import numpy as np\n'), ((2040, 2064), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (2053, 2064), False, 'import pytest\n'), ((2101, 2126), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (2114, 2126), False, 'import pytest\n'), ((2848, 2873), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (2861, 2873), False, 'import pytest\n'), ((4414, 4439), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (4427, 4439), False, 'import pytest\n'), ((6187, 6214), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (6200, 6214), False, 'import pytest\n'), ((6296, 6323), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (6309, 6323), False, 'import pytest\n'), ((6905, 6932), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (6918, 6932), False, 'import pytest\n'), ((8165, 8192), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (8178, 8192), False, 'import pytest\n'), ((9043, 9070), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9056, 9070), False, 'import pytest\n'), ((9108, 9135), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9121, 9135), False, 'import pytest\n'), ((9173, 9200), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9186, 9200), False, 'import pytest\n'), ((9426, 9453), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9439, 9453), False, 'import pytest\n'), ((9491, 9518), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9504, 9518), False, 'import pytest\n'), ((10808, 10832), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (10821, 10832), False, 'import pytest\n'), ((10874, 10898), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (10887, 10898), False, 'import pytest\n'), ((10943, 10967), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (10956, 10967), False, 'import pytest\n'), ((11003, 11027), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (11016, 11027), False, 'import pytest\n'), ((12513, 12540), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (12526, 12540), False, 'import pytest\n'), ((12580, 12607), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (12593, 12607), False, 'import pytest\n'), ((12647, 12674), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (12660, 12674), False, 'import pytest\n'), ((12800, 12824), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (12813, 12824), False, 'import pytest\n'), ((12861, 12885), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (12874, 12885), False, 'import pytest\n'), ((15548, 15573), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (15561, 15573), False, 'import pytest\n'), ((17499, 17523), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (17512, 17523), False, 'import pytest\n'), ((17970, 18000), 'pytest.raises', 'pytest.raises', (['u.UnitTypeError'], {}), '(u.UnitTypeError)\n', (17983, 18000), False, 'import pytest\n'), ((19328, 19342), 'numpy.arange', 'np.arange', (['(5.0)'], {}), '(5.0)\n', (19337, 19342), True, 'import numpy as np\n'), ((19907, 19931), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (19920, 19931), False, 'import pytest\n'), ((20580, 20607), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (20593, 20607), False, 'import pytest\n'), ((20652, 20679), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (20665, 20679), False, 'import pytest\n'), ((20726, 20753), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (20739, 20753), False, 'import pytest\n'), ((21036, 21063), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21049, 21063), False, 'import pytest\n'), ((21110, 21137), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21123, 21137), False, 'import pytest\n'), ((21186, 21213), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21199, 21213), False, 'import pytest\n'), ((21667, 21694), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21680, 21694), False, 'import pytest\n'), ((21736, 21763), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21749, 21763), False, 'import pytest\n'), ((21805, 21832), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21818, 21832), False, 'import pytest\n'), ((21995, 22013), 'numpy.arange', 'np.arange', (['(1)', '(11.0)'], {}), '(1, 11.0)\n', (22004, 22013), True, 'import numpy as np\n'), ((22028, 22055), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (22041, 22055), False, 'import pytest\n'), ((22092, 22119), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (22105, 22119), False, 'import pytest\n'), ((22156, 22183), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (22169, 22183), False, 'import pytest\n'), ((23478, 23504), 'numpy.all', 'np.all', (['(lq ** power == 1.0)'], {}), '(lq ** power == 1.0)\n', (23484, 23504), True, 'import numpy as np\n'), ((23794, 23809), 'numpy.arange', 'np.arange', (['(10.0)'], {}), '(10.0)\n', (23803, 23809), True, 'import numpy as np\n'), ((23930, 23952), 'numpy.all', 'np.all', (['(t.value == 1.0)'], {}), '(t.value == 1.0)\n', (23936, 23952), True, 'import numpy as np\n'), ((24404, 24428), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (24417, 24428), False, 'import pytest\n'), ((24659, 24686), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (24672, 24686), False, 'import pytest\n'), ((24721, 24748), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (24734, 24748), False, 'import pytest\n'), ((24783, 24810), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (24796, 24810), False, 'import pytest\n'), ((26087, 26107), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (26096, 26107), True, 'import numpy as np\n'), ((26125, 26152), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (26138, 26152), False, 'import pytest\n'), ((26280, 26307), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (26293, 26307), False, 'import pytest\n'), ((27734, 27819), 'numpy.abs', 'np.abs', (['(M_st.physical / (m_st.physical * 4.0 * np.pi * (100.0 * u.pc) ** 2) - 1.0)'], {}), '(M_st.physical / (m_st.physical * 4.0 * np.pi * (100.0 * u.pc) ** 2) -\n 1.0)\n', (27740, 27819), True, 'import numpy as np\n'), ((28768, 28795), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (28781, 28795), False, 'import pytest\n'), ((28992, 29019), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (29005, 29019), False, 'import pytest\n'), ((29060, 29087), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (29073, 29087), False, 'import pytest\n'), ((29231, 29250), 'numpy.arange', 'np.arange', (['(1.0)', '(4.0)'], {}), '(1.0, 4.0)\n', (29240, 29250), True, 'import numpy as np\n'), ((29379, 29406), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (29392, 29406), False, 'import pytest\n'), ((30791, 30815), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (30804, 30815), False, 'import pytest\n'), ((31101, 31126), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (31114, 31126), False, 'import pytest\n'), ((31182, 31207), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (31195, 31207), False, 'import pytest\n'), ((31780, 31807), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (31793, 31807), False, 'import pytest\n'), ((31821, 31844), 'numpy.power', 'np.power', (['self.mJy', '(2.0)'], {}), '(self.mJy, 2.0)\n', (31829, 31844), True, 'import numpy as np\n'), ((31913, 31940), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (31926, 31940), False, 'import pytest\n'), ((31954, 31973), 'numpy.square', 'np.square', (['self.mJy'], {}), '(self.mJy)\n', (31963, 31973), True, 'import numpy as np\n'), ((9309, 9336), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9322, 9336), False, 'import pytest\n'), ((19274, 19289), 'numpy.arange', 'np.arange', (['(10.0)'], {}), '(10.0)\n', (19283, 19289), True, 'import numpy as np\n'), ((20420, 20440), 'numpy.arange', 'np.arange', (['(1.0)', '(11.0)'], {}), '(1.0, 11.0)\n', (20429, 20440), True, 'import numpy as np\n'), ((20911, 20931), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (20920, 20931), True, 'import numpy as np\n'), ((21628, 21648), 'numpy.arange', 'np.arange', (['(1.0)', '(11.0)'], {}), '(1.0, 11.0)\n', (21637, 21648), True, 'import numpy as np\n'), ((21913, 21940), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (21926, 21940), False, 'import pytest\n'), ((23411, 23430), 'numpy.arange', 'np.arange', (['(1.0)', '(4.0)'], {}), '(1.0, 4.0)\n', (23420, 23430), True, 'import numpy as np\n'), ((23548, 23573), 'numpy.all', 'np.all', (['(lq ** power == lq)'], {}), '(lq ** power == lq)\n', (23554, 23573), True, 'import numpy as np\n'), ((23996, 24012), 'numpy.all', 'np.all', (['(t == lq2)'], {}), '(t == lq2)\n', (24002, 24012), True, 'import numpy as np\n'), ((24367, 24386), 'numpy.arange', 'np.arange', (['(1.0)', '(4.0)'], {}), '(1.0, 4.0)\n', (24376, 24386), True, 'import numpy as np\n'), ((24596, 24616), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (24605, 24616), True, 'import numpy as np\n'), ((25295, 25315), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (25304, 25315), True, 'import numpy as np\n'), ((26215, 26235), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (26224, 26235), True, 'import numpy as np\n'), ((26370, 26390), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (26379, 26390), True, 'import numpy as np\n'), ((26901, 26921), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (26910, 26921), True, 'import numpy as np\n'), ((27958, 27978), 'numpy.arange', 'np.arange', (['(1.0)', '(10.0)'], {}), '(1.0, 10.0)\n', (27967, 27978), True, 'import numpy as np\n'), ((28131, 28155), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (28144, 28155), False, 'import pytest\n'), ((28297, 28316), 'numpy.arange', 'np.arange', (['(1.0)', '(4.0)'], {}), '(1.0, 4.0)\n', (28306, 28316), True, 'import numpy as np\n'), ((28393, 28423), 'numpy.array', 'np.array', (['[True, False, False]'], {}), '([True, False, False])\n', (28401, 28423), True, 'import numpy as np\n'), ((28463, 28493), 'numpy.array', 'np.array', (['[False, True, False]'], {}), '([False, True, False])\n', (28471, 28493), True, 'import numpy as np\n'), ((28561, 28591), 'numpy.array', 'np.array', (['[True, False, False]'], {}), '([True, False, False])\n', (28569, 28591), True, 'import numpy as np\n'), ((28631, 28661), 'numpy.array', 'np.array', (['[False, True, False]'], {}), '([False, True, False])\n', (28639, 28661), True, 'import numpy as np\n'), ((28879, 28909), 'numpy.array', 'np.array', (['[True, False, False]'], {}), '([True, False, False])\n', (28887, 28909), True, 'import numpy as np\n'), ((28947, 28977), 'numpy.array', 'np.array', (['[True, False, False]'], {}), '([True, False, False])\n', (28955, 28977), True, 'import numpy as np\n'), ((31562, 31585), 'numpy.power', 'np.power', (['self.mJy', '(0.0)'], {}), '(self.mJy, 0.0)\n', (31570, 31585), True, 'import numpy as np\n'), ((31614, 31636), 'numpy.power', 'np.power', (['self.m1', '(1.0)'], {}), '(self.m1, 1.0)\n', (31622, 31636), True, 'import numpy as np\n'), ((31670, 31693), 'numpy.power', 'np.power', (['self.mJy', '(1.0)'], {}), '(self.mJy, 1.0)\n', (31678, 31693), True, 'import numpy as np\n'), ((31728, 31750), 'numpy.power', 'np.power', (['self.m1', '(2.0)'], {}), '(self.m1, 2.0)\n', (31736, 31750), True, 'import numpy as np\n'), ((31996, 32014), 'numpy.square', 'np.square', (['self.m1'], {}), '(self.m1)\n', (32005, 32014), True, 'import numpy as np\n'), ((9984, 10011), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (9997, 10011), False, 'import pytest\n'), ((10436, 10450), 'numpy.arange', 'np.arange', (['(3.0)'], {}), '(3.0)\n', (10445, 10450), True, 'import numpy as np\n'), ((11585, 11612), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (11598, 11612), False, 'import pytest\n'), ((22732, 22759), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (22745, 22759), False, 'import pytest\n'), ((23605, 23632), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (23618, 23632), False, 'import pytest\n'), ((29513, 29532), 'numpy.arange', 'np.arange', (['(1.0)', '(5.0)'], {}), '(1.0, 5.0)\n', (29522, 29532), True, 'import numpy as np\n'), ((29577, 29601), 'numpy.arange', 'np.arange', (['(1.0)', '(5.5)', '(0.5)'], {}), '(1.0, 5.5, 0.5)\n', (29586, 29601), True, 'import numpy as np\n'), ((31361, 31380), 'numpy.arange', 'np.arange', (['(1.0)', '(5.0)'], {}), '(1.0, 5.0)\n', (31370, 31380), True, 'import numpy as np\n'), ((31425, 31449), 'numpy.arange', 'np.arange', (['(1.0)', '(5.5)', '(0.5)'], {}), '(1.0, 5.5, 0.5)\n', (31434, 31449), True, 'import numpy as np\n'), ((10361, 10375), 'numpy.arange', 'np.arange', (['(3.0)'], {}), '(3.0)\n', (10370, 10375), True, 'import numpy as np\n'), ((24220, 24247), 'pytest.raises', 'pytest.raises', (['u.UnitsError'], {}), '(u.UnitsError)\n', (24233, 24247), False, 'import pytest\n'), ((12264, 12278), 'numpy.arange', 'np.arange', (['(3.0)'], {}), '(3.0)\n', (12273, 12278), True, 'import numpy as np\n'), ((12338, 12352), 'numpy.arange', 'np.arange', (['(3.0)'], {}), '(3.0)\n', (12347, 12352), True, 'import numpy as np\n')] |
kti-sam/django-connectwise | djconnectwise/tests/mocks.py | 28484faad9435892a46b8ce4a3c957f64c299971 | import os
from mock import patch
from datetime import datetime, date, time
import json
import responses
from . import fixtures
from django.utils import timezone
CW_MEMBER_IMAGE_FILENAME = 'AnonymousMember.png'
def create_mock_call(method_name, return_value, side_effect=None):
"""Utility function for mocking the specified function or method"""
_patch = patch(method_name, side_effect=side_effect)
mock_get_call = _patch.start()
if not side_effect:
mock_get_call.return_value = return_value
return mock_get_call, _patch
def company_info_get_company_info_call(return_value):
method_name = 'djconnectwise.api.CompanyInfoManager.get_company_info'
return create_mock_call(method_name, return_value)
def company_api_get_call(return_value):
method_name = 'djconnectwise.api.CompanyAPIClient.get_companies'
return create_mock_call(method_name, return_value)
def company_api_by_id_call(return_value, raised=None):
method_name = 'djconnectwise.api.CompanyAPIClient.by_id'
return create_mock_call(method_name, return_value, side_effect=raised)
def company_api_get_company_statuses_call(return_value, raised=None):
method_name = 'djconnectwise.api.CompanyAPIClient.get_company_statuses'
return create_mock_call(method_name, return_value, side_effect=raised)
def company_api_get_company_types_call(return_value, raised=None):
method_name = 'djconnectwise.api.CompanyAPIClient.get_company_types'
return create_mock_call(method_name, return_value, side_effect=raised)
def projects_api_get_project_statuses_call(return_value, raised=None):
method_name = 'djconnectwise.api.ProjectAPIClient.get_project_statuses'
return create_mock_call(method_name, return_value, side_effect=raised)
def projects_api_get_project_types_call(return_value, raised=None):
method_name = 'djconnectwise.api.ProjectAPIClient.get_project_types'
return create_mock_call(method_name, return_value, side_effect=raised)
def projects_api_get_project_phases_call(return_value, raised=None):
method_name = 'djconnectwise.api.ProjectAPIClient.get_project_phases'
return create_mock_call(method_name, return_value, side_effect=raised)
def project_api_get_projects_call(return_value):
method_name = 'djconnectwise.api.ProjectAPIClient.get_projects'
return create_mock_call(method_name, return_value)
def project_api_get_project_call(return_value, raised=None):
method_name = 'djconnectwise.api.ProjectAPIClient.get_project'
return create_mock_call(method_name, return_value, side_effect=raised)
def _project_api_tickets_call(page=1, page_size=25, conditions=[]):
return_value = []
test_date = date(1948, 5, 14)
test_time = time(12, 0, 0, tzinfo=timezone.get_current_timezone())
test_datetime = datetime.combine(test_date, test_time)
conditions.append('lastUpdated>' + timezone.localtime(
value=test_datetime).isoformat()
)
if page == 1:
return_value = [fixtures.API_PROJECT_TICKET]
return return_value
def project_api_tickets_call():
method_name = 'djconnectwise.api.TicketAPIMixin.get_tickets'
mock_call, _patch = create_mock_call(
method_name,
None,
side_effect=_project_api_tickets_call)
return mock_call, _patch
def project_api_tickets_test_command(return_value):
method_name = 'djconnectwise.api.TicketAPIMixin.get_tickets'
mock_call, _patch = create_mock_call(method_name, return_value)
return mock_call, _patch
def sales_api_by_id_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.by_id'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_opportunities_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_opportunities'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_opportunity_statuses_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_opportunity_statuses'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_opportunity_types_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_opportunity_types'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_opportunity_stages_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_opportunity_stages'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_sales_probabilities_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_probabilities'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_schedule_types_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_schedule_types'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_schedule_statuses_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_schedule_statuses'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_schedule_entries_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_schedule_entries'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_schedule_entry_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_schedule_entry'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_calendars_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_calendars'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_holidays_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_holidays'
return create_mock_call(method_name, return_value, side_effect=raised)
def schedule_api_get_holiday_lists_call(return_value, raised=None):
method_name = 'djconnectwise.api.ScheduleAPIClient.get_holiday_lists'
return create_mock_call(method_name, return_value, side_effect=raised)
def time_api_get_time_entries_call(return_value, raised=None):
method_name = 'djconnectwise.api.TimeAPIClient.get_time_entries'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_activities_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_activities'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_activities_statuses_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_activity_statuses'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_activities_types_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_activity_types'
return create_mock_call(method_name, return_value, side_effect=raised)
def sales_api_get_single_activity_call(return_value, raised=None):
method_name = 'djconnectwise.api.SalesAPIClient.get_single_activity'
return create_mock_call(method_name, return_value, side_effect=raised)
def _service_api_tickets_call(page=1, page_size=25, conditions=[]):
return_value = []
test_date = date(1948, 5, 14)
test_time = time(12, 0, 0, tzinfo=timezone.get_current_timezone())
test_datetime = datetime.combine(test_date, test_time)
conditions.append('lastUpdated>' + timezone.localtime(
value=test_datetime).isoformat()
)
if page == 1:
return_value = [fixtures.API_SERVICE_TICKET]
return return_value
def service_api_tickets_call():
method_name = 'djconnectwise.api.TicketAPIMixin.get_tickets'
mock_call, _patch = create_mock_call(
method_name,
None,
side_effect=_service_api_tickets_call)
return mock_call, _patch
def _service_api_get_ticket_call(ticket_id):
return fixtures.API_SERVICE_TICKET_MAP.get(ticket_id)
def service_api_get_ticket_call(raised=None):
method_name = 'djconnectwise.api.TicketAPIMixin.get_ticket'
mock_call, _patch = create_mock_call(
method_name,
None,
side_effect=raised if raised else _service_api_get_ticket_call)
return mock_call, _patch
def service_api_get_boards_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_boards'
return create_mock_call(method_name, return_value)
def service_api_update_ticket_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.update_ticket'
return create_mock_call(method_name, return_value)
def service_api_get_statuses_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_statuses'
return create_mock_call(method_name, return_value)
def service_api_get_priorities_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_priorities'
return create_mock_call(method_name, return_value)
def service_api_get_teams_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_teams'
return create_mock_call(method_name, return_value)
def service_api_get_notes_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_notes'
return create_mock_call(method_name, return_value)
def service_api_get_slas_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_slas'
return create_mock_call(method_name, return_value)
def service_api_get_sla_priorities_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_slapriorities'
return create_mock_call(method_name, return_value)
def service_api_get_types_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_types'
return create_mock_call(method_name, return_value)
def service_api_get_subtypes_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_subtypes'
return create_mock_call(method_name, return_value)
def service_api_get_items_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_items'
return create_mock_call(method_name, return_value)
def sales_api_get_opportunity_notes_call(return_value):
method_name = 'djconnectwise.api.SalesAPIClient.get_notes'
return create_mock_call(method_name, return_value)
def service_api_get_locations_call(return_value):
method_name = 'djconnectwise.api.ServiceAPIClient.get_locations'
return create_mock_call(method_name, return_value)
def system_api_get_connectwise_version_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_connectwise_version'
return create_mock_call(method_name, return_value)
def system_api_get_members_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_members'
return create_mock_call(method_name, return_value)
def system_api_get_member_image_by_photo_id_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.' \
+ 'get_member_image_by_photo_id'
return create_mock_call(method_name, return_value)
def system_api_get_member_count_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_members'
return create_mock_call(method_name, return_value)
def system_api_create_callback_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.create_callback'
return create_mock_call(method_name, return_value)
def system_api_delete_callback_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.delete_callback'
return create_mock_call(method_name, return_value)
def system_api_get_callbacks_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_callbacks'
return create_mock_call(method_name, return_value)
def system_api_get_territories_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_territories'
return create_mock_call(method_name, return_value)
def system_api_get_other_call(return_value):
method_name = 'djconnectwise.api.SystemAPIClient.get_mycompanyother'
return create_mock_call(method_name, return_value)
def cw_api_fetch_resource_call(return_value):
method_name = 'djconnectwise.api.ConnectWiseAPIClient.fetch_resource'
return create_mock_call(method_name, return_value)
def get(url, data, headers=None, status=200):
"""Set up requests mock for given URL and JSON-serializable data."""
get_raw(url, json.dumps(data), "application/json", headers, status=status)
def time_api_get_work_types_call(return_value):
method_name = 'djconnectwise.api.TimeAPIClient.get_work_types'
return create_mock_call(method_name, return_value)
def time_api_get_work_roles_call(return_value):
method_name = 'djconnectwise.api.TimeAPIClient.get_work_roles'
return create_mock_call(method_name, return_value)
def finance_api_get_agreements_call(return_value):
method_name = 'djconnectwise.api.FinanceAPIClient.get_agreements'
return create_mock_call(method_name, return_value)
def get_raw(url, data, content_type="application/octet-stream", headers=None,
status=200):
"""Set up requests mock for given URL."""
responses.add(
responses.GET,
url,
body=data,
status=status,
content_type=content_type,
adding_headers=headers,
)
def get_member_avatar():
"""Return the avatar image data in the tests directory."""
cw_member_image_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
CW_MEMBER_IMAGE_FILENAME
)
with open(cw_member_image_path, 'rb') as anonymous_image_file:
return anonymous_image_file.read()
| [((367, 410), 'mock.patch', 'patch', (['method_name'], {'side_effect': 'side_effect'}), '(method_name, side_effect=side_effect)\n', (372, 410), False, 'from mock import patch\n'), ((2688, 2705), 'datetime.date', 'date', (['(1948)', '(5)', '(14)'], {}), '(1948, 5, 14)\n', (2692, 2705), False, 'from datetime import datetime, date, time\n'), ((2797, 2835), 'datetime.datetime.combine', 'datetime.combine', (['test_date', 'test_time'], {}), '(test_date, test_time)\n', (2813, 2835), False, 'from datetime import datetime, date, time\n'), ((7530, 7547), 'datetime.date', 'date', (['(1948)', '(5)', '(14)'], {}), '(1948, 5, 14)\n', (7534, 7547), False, 'from datetime import datetime, date, time\n'), ((7639, 7677), 'datetime.datetime.combine', 'datetime.combine', (['test_date', 'test_time'], {}), '(test_date, test_time)\n', (7655, 7677), False, 'from datetime import datetime, date, time\n'), ((13483, 13598), 'responses.add', 'responses.add', (['responses.GET', 'url'], {'body': 'data', 'status': 'status', 'content_type': 'content_type', 'adding_headers': 'headers'}), '(responses.GET, url, body=data, status=status, content_type=\n content_type, adding_headers=headers)\n', (13496, 13598), False, 'import responses\n'), ((12744, 12760), 'json.dumps', 'json.dumps', (['data'], {}), '(data)\n', (12754, 12760), False, 'import json\n'), ((2744, 2775), 'django.utils.timezone.get_current_timezone', 'timezone.get_current_timezone', ([], {}), '()\n', (2773, 2775), False, 'from django.utils import timezone\n'), ((7586, 7617), 'django.utils.timezone.get_current_timezone', 'timezone.get_current_timezone', ([], {}), '()\n', (7615, 7617), False, 'from django.utils import timezone\n'), ((13804, 13829), 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), '(__file__)\n', (13819, 13829), False, 'import os\n'), ((2875, 2914), 'django.utils.timezone.localtime', 'timezone.localtime', ([], {'value': 'test_datetime'}), '(value=test_datetime)\n', (2893, 2914), False, 'from django.utils import timezone\n'), ((7717, 7756), 'django.utils.timezone.localtime', 'timezone.localtime', ([], {'value': 'test_datetime'}), '(value=test_datetime)\n', (7735, 7756), False, 'from django.utils import timezone\n')] |
vineeths96/Visual-Odometry | visual_odometry/visual_odometry.py | 88d96a23a0bde9c05de1f4dddcca8b6c4bd817e7 | from .monovideoodometry import MonoVideoOdometry
from .parameters import *
def visual_odometry(
image_path="./input/sequences/10/image_0/",
pose_path="./input/poses/10.txt",
fivepoint=False,
):
"""
Plots the estimated odometry path using either five point estimation or eight point estimation
:param image_path: Path to the directory of camera images
:param pose_path: Path to the directory of pose file
:param fivepoint: Whether to use five point or eight point method
:return: None
"""
vo = MonoVideoOdometry(image_path, pose_path, FOCAL, PP, K, LUCAS_KANADE_PARAMS, fivepoint)
trajectory = np.zeros(shape=(800, 1200, 3))
frame_count = 0
while vo.hasNextFrame():
frame_count += 1
frame = vo.current_frame
cv2.imshow("Frame", frame)
k = cv2.waitKey(1)
if k == 27:
break
vo.process_frame()
estimated_coordinates = vo.get_mono_coordinates()
true_coordinates = vo.get_true_coordinates()
print("MSE Error: ", np.linalg.norm(estimated_coordinates - true_coordinates))
print("x: {}, y: {}, z: {}".format(*[str(pt) for pt in estimated_coordinates]))
print("True_x: {}, True_y: {}, True_z: {}".format(*[str(pt) for pt in true_coordinates]))
draw_x, draw_y, draw_z = [int(round(x)) for x in estimated_coordinates]
true_x, true_y, true_z = [int(round(x)) for x in true_coordinates]
trajectory = cv2.circle(trajectory, (true_x + 400, true_z + 100), 1, list((0, 0, 255)), 4)
trajectory = cv2.circle(trajectory, (draw_x + 400, draw_z + 100), 1, list((0, 255, 0)), 4)
cv2.putText(
trajectory,
"Actual Position:",
(140, 90),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1,
)
cv2.putText(trajectory, "Red", (270, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
cv2.putText(
trajectory,
"Estimated Odometry Position:",
(30, 120),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1,
)
cv2.putText(
trajectory,
"Green",
(270, 120),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 255, 0),
1,
)
cv2.imshow("trajectory", trajectory)
if frame_count % 5 == 0:
cv2.imwrite(f"./results/trajectory/trajectory_{frame_count}.png", trajectory)
cv2.imwrite(f"./results/trajectory.png", trajectory)
cv2.destroyAllWindows()
| [] |
Amirosimani/amazon-sagemaker-script-mode | tf-2-data-parallelism/src/utils.py | ea8d7d6b1b0613dffa793c9ae247cfd8868034ec | import os
import numpy as np
import tensorflow as tf
def get_train_data(train_dir, batch_size):
train_images = np.load(os.path.join(train_dir, 'train_images.npy'))
train_labels = np.load(os.path.join(train_dir, 'train_labels.npy'))
print('train_images', train_images.shape, 'train_labels', train_labels.shape)
dataset_train = tf.data.Dataset.from_tensor_slices((train_images, train_labels))
dataset_train = dataset_train.repeat().shuffle(10000).batch(batch_size)
return dataset_train
def get_val_data(val_dir):
test_images = np.load(os.path.join(val_dir, 'validation_images.npy'))
test_labels = np.load(os.path.join(val_dir, 'validation_labels.npy'))
print('validation_images', test_images.shape, 'validation_labels', test_labels.shape)
dataset_test = tf.data.Dataset.from_tensor_slices((test_images, test_labels))
return dataset_test
| [((345, 409), 'tensorflow.data.Dataset.from_tensor_slices', 'tf.data.Dataset.from_tensor_slices', (['(train_images, train_labels)'], {}), '((train_images, train_labels))\n', (379, 409), True, 'import tensorflow as tf\n'), ((799, 861), 'tensorflow.data.Dataset.from_tensor_slices', 'tf.data.Dataset.from_tensor_slices', (['(test_images, test_labels)'], {}), '((test_images, test_labels))\n', (833, 861), True, 'import tensorflow as tf\n'), ((125, 168), 'os.path.join', 'os.path.join', (['train_dir', '"""train_images.npy"""'], {}), "(train_dir, 'train_images.npy')\n", (137, 168), False, 'import os\n'), ((197, 240), 'os.path.join', 'os.path.join', (['train_dir', '"""train_labels.npy"""'], {}), "(train_dir, 'train_labels.npy')\n", (209, 240), False, 'import os\n'), ((567, 613), 'os.path.join', 'os.path.join', (['val_dir', '"""validation_images.npy"""'], {}), "(val_dir, 'validation_images.npy')\n", (579, 613), False, 'import os\n'), ((641, 687), 'os.path.join', 'os.path.join', (['val_dir', '"""validation_labels.npy"""'], {}), "(val_dir, 'validation_labels.npy')\n", (653, 687), False, 'import os\n')] |
CaptainCandy/influence-release | scripts/run_rbf_comparison_car_air_top5.py | a152486a1c130fb5f907259c6692b9fe0d2ef6d0 | # -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 16:26:35 2019
@author: Administrator
"""
# Forked from run_rbf_comparison.py
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
import math
import copy
import numpy as np
import pandas as pd
import sklearn.linear_model as linear_model
import sklearn.preprocessing as preprocessing
import scipy
import scipy.linalg as slin
import scipy.sparse.linalg as sparselin
import scipy.sparse as sparse
import random
import sys
sys.path.append("C:/Tang/influence-release-master") #设置自定义包的搜索路径
from load_vehicles import load_vehicles
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets import base
from sklearn.metrics.pairwise import rbf_kernel
from influence.inceptionModel import BinaryInceptionModel
from influence.smooth_hinge import SmoothHinge
from influence.binaryLogisticRegressionWithLBFGS import BinaryLogisticRegressionWithLBFGS
import influence.dataset as dataset
from influence.dataset import DataSet
from influence.dataset_poisoning import generate_inception_features
#%%
def get_Y_pred_correct_inception(model):
Y_test = model.data_sets.test.labels
if np.min(Y_test) < -0.5:
Y_test = (np.copy(Y_test) + 1) / 2
Y_pred = model.sess.run(model.preds, feed_dict=model.all_test_feed_dict)
Y_pred_correct = np.zeros([len(Y_test)])
for idx, label in enumerate(Y_test):
Y_pred_correct[idx] = Y_pred[idx, int(label)]
return Y_pred_correct
num_classes = 2
num_train_ex_per_class = 40
num_test_ex_per_class = 300
dataset_name = 'carair_%s_%s' % (num_train_ex_per_class, num_test_ex_per_class)
image_data_sets = load_vehicles(
num_train_ex_per_class=num_train_ex_per_class,
num_test_ex_per_class=num_test_ex_per_class)
weight_decay = 0.001
initial_learning_rate = 0.001
keep_probs = None
decay_epochs = [1000, 10000]
#%%
### Generate kernelized feature vectors
X_train = image_data_sets.train.x
X_test = image_data_sets.test.x
Y_train = np.copy(image_data_sets.train.labels) * 2 - 1
Y_test = np.copy(image_data_sets.test.labels) * 2 - 1
num_train = X_train.shape[0]
num_test = X_test.shape[0]
X_stacked = np.vstack((X_train, X_test))
gamma = 0.05
weight_decay = 0.0001
K = rbf_kernel(X_stacked, gamma = gamma / num_train)
# =============================================================================
# L = slin.cholesky(K, lower=True)
# L_train = L[:num_train, :num_train]
# L_test = L[num_train:, :num_train]
# =============================================================================
K_train = K[:num_train, :num_train]
K_test = K[num_train:, :num_train]
### Compare top 5 influential examples from each network
test_idx = 0
## RBF
input_channels = 1
weight_decay = 0.001
batch_size = num_train
initial_learning_rate = 0.001
keep_probs = None
max_lbfgs_iter = 1000
use_bias = False
decay_epochs = [1000, 10000]
tf.reset_default_graph()
X_train = image_data_sets.train.x
Y_train = image_data_sets.train.labels * 2 - 1
train = DataSet(K_train, Y_train)
test = DataSet(K_test, Y_test)
data_sets = base.Datasets(train=train, validation=None, test=test)
input_dim = data_sets.train.x.shape[1]
# Train with hinge
print('Train rbf with hinge...')
rbf_model = SmoothHinge(
temp=0,
use_bias=use_bias,
input_dim=input_dim,
weight_decay=weight_decay,
num_classes=num_classes,
batch_size=batch_size,
data_sets=data_sets,
initial_learning_rate=initial_learning_rate,
keep_probs=keep_probs,
decay_epochs=decay_epochs,
mini_batch=False,
train_dir='output7',
log_dir='log',
model_name='carair_rbf_hinge_t-0')
rbf_model.train()
hinge_W = rbf_model.sess.run(rbf_model.params)[0]
# Then load weights into smoothed version
print('Load weights into smoothed version...')
tf.reset_default_graph()
rbf_model = SmoothHinge(
temp=0.001,
use_bias=use_bias,
input_dim=input_dim,
weight_decay=weight_decay,
num_classes=num_classes,
batch_size=batch_size,
data_sets=data_sets,
initial_learning_rate=initial_learning_rate,
keep_probs=keep_probs,
decay_epochs=decay_epochs,
mini_batch=False,
train_dir='output7',
log_dir='log',
model_name='car_air_rbf_hinge_t-0.001')
params_feed_dict = {}
params_feed_dict[rbf_model.W_placeholder] = hinge_W
rbf_model.sess.run(rbf_model.set_params_op, feed_dict=params_feed_dict)
rbf_predicted_loss_diffs = rbf_model.get_influence_on_test_loss(
[test_idx],
np.arange(len(rbf_model.data_sets.train.labels)),
force_refresh=True)
#%%
## Inception
dataset_name = 'carair_40_300'
test_idx = 0
# Generate inception features
print('Generate inception features...')
img_side = 299
num_channels = 3
num_train_ex_per_class = 40
num_test_ex_per_class = 300
batch_size = 20 #TODO: 需要根据配置修改
# reset_default_graph大概就是重置当前线程,让tf session里定义的东西都失效,重来。就是重开一个神经网络session
tf.reset_default_graph()
full_model_name = '%s_inception' % dataset_name
# 下面的语句是定义一个inception双分类器
full_model = BinaryInceptionModel(
img_side=img_side,
num_channels=num_channels,
weight_decay=weight_decay,
num_classes=num_classes,
batch_size=batch_size,
data_sets=image_data_sets,
initial_learning_rate=initial_learning_rate,
keep_probs=keep_probs,
decay_epochs=decay_epochs,
mini_batch=True,
train_dir='output9',
log_dir='log',
model_name=full_model_name)
# 下面的代码是在使用inception的卷积层生成特征
train_inception_features_val = generate_inception_features(
full_model,
image_data_sets.train.x,
image_data_sets.train.labels,
batch_size=batch_size)
test_inception_features_val = generate_inception_features(
full_model,
image_data_sets.test.x,
image_data_sets.test.labels,
batch_size=batch_size)
train = DataSet(
train_inception_features_val,
image_data_sets.train.labels)
test = DataSet(
test_inception_features_val,
image_data_sets.test.labels)
# train_f = np.load('G:/output/%s_inception_features_new_train.npz' % dataset_name)
# train = DataSet(train_f['inception_features_val'], train_f['labels'])
# test_f = np.load('G:/output/%s_inception_features_new_test.npz' % dataset_name)
# test = DataSet(test_f['inception_features_val'], test_f['labels'])
validation = None
# 上面的代码是训练了inception模型的全连接层前面的部分,因此输出的feature有2048个维度
data_sets = base.Datasets(train=train, validation=validation, test=test)
# train_f = np.load('G:/output/%s_inception_features_new_train.npz' % dataset_name)
# train = DataSet(train_f['inception_features_val'], train_f['labels'])
# test_f = np.load('G:/output/%s_inception_features_new_test.npz' % dataset_name)
# test = DataSet(test_f['inception_features_val'], test_f['labels'])
# validation = None
# data_sets = base.Datasets(train=train, validation=validation, test=test)
# 下面的代码利用从inception卷积层训练完成后的feature进行一个二分类逻辑回归,取消卷积层后面的FC全连接层
print('Train logistic regression after inception...')
input_dim = 2048
weight_decay = 0.001
batch_size = 20
initial_learning_rate = 0.001
keep_probs = None
decay_epochs = [1000, 10000]
max_lbfgs_iter = 1000
num_classes = 2
tf.reset_default_graph()
inception_model = BinaryLogisticRegressionWithLBFGS(
input_dim=input_dim,
weight_decay=weight_decay,
max_lbfgs_iter=max_lbfgs_iter,
num_classes=num_classes,
batch_size=batch_size,
data_sets=data_sets,
initial_learning_rate=initial_learning_rate,
keep_probs=keep_probs,
decay_epochs=decay_epochs,
mini_batch=False,
train_dir='output9',
log_dir='log',
model_name='%s_inception_onlytop' % dataset_name)
inception_model.train()
# =============================================================================
# inception_predicted_loss_diffs = inception_model.get_influence_on_test_loss(
# [test_idx],
# np.arange(len(inception_model.data_sets.train.labels)),
# force_refresh=True)
#
# x_test = X_test[test_idx, :]
# y_test = Y_test[test_idx]
#
#
# distances = dataset.find_distances(x_test, X_train)
# flipped_idx = Y_train != y_test
# rbf_margins_test = rbf_model.sess.run(rbf_model.margin, feed_dict=rbf_model.all_test_feed_dict)
# rbf_margins_train = rbf_model.sess.run(rbf_model.margin, feed_dict=rbf_model.all_train_feed_dict)
# inception_Y_pred_correct = get_Y_pred_correct_inception(inception_model)
#
#
# np.savez(
# 'output7/rbf_carair_results_%s' % test_idx,
# test_idx=test_idx,
# distances=distances,
# flipped_idx=flipped_idx,
# rbf_margins_test=rbf_margins_test,
# rbf_margins_train=rbf_margins_train,
# inception_Y_pred_correct=inception_Y_pred_correct,
# rbf_predicted_loss_diffs=rbf_predicted_loss_diffs,
# inception_predicted_loss_diffs=inception_predicted_loss_diffs
# )
# =============================================================================
#%%
print('Save results...')
#rand_test = random.sample(range(1, 600),50)
#np.savez('output7/rand_test_point', rand_test=rand_test)
for test_idx in range(1, 600):
rbf_predicted_loss_diffs = rbf_model.get_influence_on_test_loss(
[test_idx],
np.arange(len(rbf_model.data_sets.train.labels)),
force_refresh=True)
inception_predicted_loss_diffs = inception_model.get_influence_on_test_loss(
[test_idx],
np.arange(len(inception_model.data_sets.train.labels)),
force_refresh=True)
x_test = X_test[test_idx, :]
y_test = Y_test[test_idx]
distances = dataset.find_distances(x_test, X_train)
flipped_idx = Y_train != y_test
rbf_margins_test = rbf_model.sess.run(rbf_model.margin, feed_dict=rbf_model.all_test_feed_dict)
rbf_margins_train = rbf_model.sess.run(rbf_model.margin, feed_dict=rbf_model.all_train_feed_dict)
inception_Y_pred_correct = get_Y_pred_correct_inception(inception_model)
np.savez(
'output9/rbf_carair_results_%s' % test_idx,
test_idx=test_idx,
distances=distances,
flipped_idx=flipped_idx,
rbf_margins_test=rbf_margins_test,
rbf_margins_train=rbf_margins_train,
inception_Y_pred_correct=inception_Y_pred_correct,
rbf_predicted_loss_diffs=rbf_predicted_loss_diffs,
inception_predicted_loss_diffs=inception_predicted_loss_diffs
)
| [((571, 622), 'sys.path.append', 'sys.path.append', (['"""C:/Tang/influence-release-master"""'], {}), "('C:/Tang/influence-release-master')\n", (586, 622), False, 'import sys\n'), ((1737, 1846), 'load_vehicles.load_vehicles', 'load_vehicles', ([], {'num_train_ex_per_class': 'num_train_ex_per_class', 'num_test_ex_per_class': 'num_test_ex_per_class'}), '(num_train_ex_per_class=num_train_ex_per_class,\n num_test_ex_per_class=num_test_ex_per_class)\n', (1750, 1846), False, 'from load_vehicles import load_vehicles\n'), ((2245, 2273), 'numpy.vstack', 'np.vstack', (['(X_train, X_test)'], {}), '((X_train, X_test))\n', (2254, 2273), True, 'import numpy as np\n'), ((2315, 2361), 'sklearn.metrics.pairwise.rbf_kernel', 'rbf_kernel', (['X_stacked'], {'gamma': '(gamma / num_train)'}), '(X_stacked, gamma=gamma / num_train)\n', (2325, 2361), False, 'from sklearn.metrics.pairwise import rbf_kernel\n'), ((2969, 2993), 'tensorflow.reset_default_graph', 'tf.reset_default_graph', ([], {}), '()\n', (2991, 2993), True, 'import tensorflow as tf\n'), ((3084, 3109), 'influence.dataset.DataSet', 'DataSet', (['K_train', 'Y_train'], {}), '(K_train, Y_train)\n', (3091, 3109), False, 'from influence.dataset import DataSet\n'), ((3117, 3140), 'influence.dataset.DataSet', 'DataSet', (['K_test', 'Y_test'], {}), '(K_test, Y_test)\n', (3124, 3140), False, 'from influence.dataset import DataSet\n'), ((3154, 3208), 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets', 'base.Datasets', ([], {'train': 'train', 'validation': 'None', 'test': 'test'}), '(train=train, validation=None, test=test)\n', (3167, 3208), False, 'from tensorflow.contrib.learn.python.learn.datasets import base\n'), ((3313, 3672), 'influence.smooth_hinge.SmoothHinge', 'SmoothHinge', ([], {'temp': '(0)', 'use_bias': 'use_bias', 'input_dim': 'input_dim', 'weight_decay': 'weight_decay', 'num_classes': 'num_classes', 'batch_size': 'batch_size', 'data_sets': 'data_sets', 'initial_learning_rate': 'initial_learning_rate', 'keep_probs': 'keep_probs', 'decay_epochs': 'decay_epochs', 'mini_batch': '(False)', 'train_dir': '"""output7"""', 'log_dir': '"""log"""', 'model_name': '"""carair_rbf_hinge_t-0"""'}), "(temp=0, use_bias=use_bias, input_dim=input_dim, weight_decay=\n weight_decay, num_classes=num_classes, batch_size=batch_size, data_sets\n =data_sets, initial_learning_rate=initial_learning_rate, keep_probs=\n keep_probs, decay_epochs=decay_epochs, mini_batch=False, train_dir=\n 'output7', log_dir='log', model_name='carair_rbf_hinge_t-0')\n", (3324, 3672), False, 'from influence.smooth_hinge import SmoothHinge\n'), ((3873, 3897), 'tensorflow.reset_default_graph', 'tf.reset_default_graph', ([], {}), '()\n', (3895, 3897), True, 'import tensorflow as tf\n'), ((3910, 4281), 'influence.smooth_hinge.SmoothHinge', 'SmoothHinge', ([], {'temp': '(0.001)', 'use_bias': 'use_bias', 'input_dim': 'input_dim', 'weight_decay': 'weight_decay', 'num_classes': 'num_classes', 'batch_size': 'batch_size', 'data_sets': 'data_sets', 'initial_learning_rate': 'initial_learning_rate', 'keep_probs': 'keep_probs', 'decay_epochs': 'decay_epochs', 'mini_batch': '(False)', 'train_dir': '"""output7"""', 'log_dir': '"""log"""', 'model_name': '"""car_air_rbf_hinge_t-0.001"""'}), "(temp=0.001, use_bias=use_bias, input_dim=input_dim,\n weight_decay=weight_decay, num_classes=num_classes, batch_size=\n batch_size, data_sets=data_sets, initial_learning_rate=\n initial_learning_rate, keep_probs=keep_probs, decay_epochs=decay_epochs,\n mini_batch=False, train_dir='output7', log_dir='log', model_name=\n 'car_air_rbf_hinge_t-0.001')\n", (3921, 4281), False, 'from influence.smooth_hinge import SmoothHinge\n'), ((4953, 4977), 'tensorflow.reset_default_graph', 'tf.reset_default_graph', ([], {}), '()\n', (4975, 4977), True, 'import tensorflow as tf\n'), ((5065, 5432), 'influence.inceptionModel.BinaryInceptionModel', 'BinaryInceptionModel', ([], {'img_side': 'img_side', 'num_channels': 'num_channels', 'weight_decay': 'weight_decay', 'num_classes': 'num_classes', 'batch_size': 'batch_size', 'data_sets': 'image_data_sets', 'initial_learning_rate': 'initial_learning_rate', 'keep_probs': 'keep_probs', 'decay_epochs': 'decay_epochs', 'mini_batch': '(True)', 'train_dir': '"""output9"""', 'log_dir': '"""log"""', 'model_name': 'full_model_name'}), "(img_side=img_side, num_channels=num_channels,\n weight_decay=weight_decay, num_classes=num_classes, batch_size=\n batch_size, data_sets=image_data_sets, initial_learning_rate=\n initial_learning_rate, keep_probs=keep_probs, decay_epochs=decay_epochs,\n mini_batch=True, train_dir='output9', log_dir='log', model_name=\n full_model_name)\n", (5085, 5432), False, 'from influence.inceptionModel import BinaryInceptionModel\n'), ((5525, 5646), 'influence.dataset_poisoning.generate_inception_features', 'generate_inception_features', (['full_model', 'image_data_sets.train.x', 'image_data_sets.train.labels'], {'batch_size': 'batch_size'}), '(full_model, image_data_sets.train.x,\n image_data_sets.train.labels, batch_size=batch_size)\n', (5552, 5646), False, 'from influence.dataset_poisoning import generate_inception_features\n'), ((5700, 5819), 'influence.dataset_poisoning.generate_inception_features', 'generate_inception_features', (['full_model', 'image_data_sets.test.x', 'image_data_sets.test.labels'], {'batch_size': 'batch_size'}), '(full_model, image_data_sets.test.x,\n image_data_sets.test.labels, batch_size=batch_size)\n', (5727, 5819), False, 'from influence.dataset_poisoning import generate_inception_features\n'), ((5847, 5914), 'influence.dataset.DataSet', 'DataSet', (['train_inception_features_val', 'image_data_sets.train.labels'], {}), '(train_inception_features_val, image_data_sets.train.labels)\n', (5854, 5914), False, 'from influence.dataset import DataSet\n'), ((5931, 5996), 'influence.dataset.DataSet', 'DataSet', (['test_inception_features_val', 'image_data_sets.test.labels'], {}), '(test_inception_features_val, image_data_sets.test.labels)\n', (5938, 5996), False, 'from influence.dataset import DataSet\n'), ((6400, 6460), 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets', 'base.Datasets', ([], {'train': 'train', 'validation': 'validation', 'test': 'test'}), '(train=train, validation=validation, test=test)\n', (6413, 6460), False, 'from tensorflow.contrib.learn.python.learn.datasets import base\n'), ((7153, 7177), 'tensorflow.reset_default_graph', 'tf.reset_default_graph', ([], {}), '()\n', (7175, 7177), True, 'import tensorflow as tf\n'), ((7197, 7600), 'influence.binaryLogisticRegressionWithLBFGS.BinaryLogisticRegressionWithLBFGS', 'BinaryLogisticRegressionWithLBFGS', ([], {'input_dim': 'input_dim', 'weight_decay': 'weight_decay', 'max_lbfgs_iter': 'max_lbfgs_iter', 'num_classes': 'num_classes', 'batch_size': 'batch_size', 'data_sets': 'data_sets', 'initial_learning_rate': 'initial_learning_rate', 'keep_probs': 'keep_probs', 'decay_epochs': 'decay_epochs', 'mini_batch': '(False)', 'train_dir': '"""output9"""', 'log_dir': '"""log"""', 'model_name': "('%s_inception_onlytop' % dataset_name)"}), "(input_dim=input_dim, weight_decay=\n weight_decay, max_lbfgs_iter=max_lbfgs_iter, num_classes=num_classes,\n batch_size=batch_size, data_sets=data_sets, initial_learning_rate=\n initial_learning_rate, keep_probs=keep_probs, decay_epochs=decay_epochs,\n mini_batch=False, train_dir='output9', log_dir='log', model_name=\n '%s_inception_onlytop' % dataset_name)\n", (7230, 7600), False, 'from influence.binaryLogisticRegressionWithLBFGS import BinaryLogisticRegressionWithLBFGS\n'), ((9472, 9511), 'influence.dataset.find_distances', 'dataset.find_distances', (['x_test', 'X_train'], {}), '(x_test, X_train)\n', (9494, 9511), True, 'import influence.dataset as dataset\n'), ((9836, 10211), 'numpy.savez', 'np.savez', (["('output9/rbf_carair_results_%s' % test_idx)"], {'test_idx': 'test_idx', 'distances': 'distances', 'flipped_idx': 'flipped_idx', 'rbf_margins_test': 'rbf_margins_test', 'rbf_margins_train': 'rbf_margins_train', 'inception_Y_pred_correct': 'inception_Y_pred_correct', 'rbf_predicted_loss_diffs': 'rbf_predicted_loss_diffs', 'inception_predicted_loss_diffs': 'inception_predicted_loss_diffs'}), "('output9/rbf_carair_results_%s' % test_idx, test_idx=test_idx,\n distances=distances, flipped_idx=flipped_idx, rbf_margins_test=\n rbf_margins_test, rbf_margins_train=rbf_margins_train,\n inception_Y_pred_correct=inception_Y_pred_correct,\n rbf_predicted_loss_diffs=rbf_predicted_loss_diffs,\n inception_predicted_loss_diffs=inception_predicted_loss_diffs)\n", (9844, 10211), True, 'import numpy as np\n'), ((1248, 1262), 'numpy.min', 'np.min', (['Y_test'], {}), '(Y_test)\n', (1254, 1262), True, 'import numpy as np\n'), ((2075, 2112), 'numpy.copy', 'np.copy', (['image_data_sets.train.labels'], {}), '(image_data_sets.train.labels)\n', (2082, 2112), True, 'import numpy as np\n'), ((2130, 2166), 'numpy.copy', 'np.copy', (['image_data_sets.test.labels'], {}), '(image_data_sets.test.labels)\n', (2137, 2166), True, 'import numpy as np\n'), ((1289, 1304), 'numpy.copy', 'np.copy', (['Y_test'], {}), '(Y_test)\n', (1296, 1304), True, 'import numpy as np\n')] |
Keesiu/meta-kaggle | data/external/repositories_2to3/145085/kaggle_Microsoft_Malware-master/kaggle_Microsoft_malware_small/find_4g.py | 87de739aba2399fd31072ee81b391f9b7a63f540 | import sys
import pickle
##########################################################
# usage
# pypy find_4g.py xid_train.p ../../data/train
# xid_train.p is a list like ['loIP1tiwELF9YNZQjSUO',''....] to specify
# the order of samples in traing data
# ../../data/train is the path of original train data
##########################################################
xid_name=sys.argv[1]
data_path=sys.argv[2]
xid=pickle.load(open(xid_name)) #xid_train.p or xid_test.p
newc=pickle.load(open('newc.p'))
newc2=pickle.load(open('cutcmd3g_for_4g.p'))
cmd4g={}
for i in newc2:
for j in newc:
cmd4g[(i[0],i[1],i[2],j)]=0
print(newc)
for c,f in enumerate(xid):
count={}
fo=open(data_path+'/'+f+'.asm')
tot=0
a=-1
b=-1
d=-1
e=-1
for line in fo:
xx=line.split()
for x in xx:
if x in newc:
a=b
b=d
d=e
e=x
if (a,b,d,e) in cmd4g:
if (a,b,d,e) not in count:
count[(a,b,d,e)]=0
count[(a,b,d,e)]+=1
tot+=1
fo.close()
if True:#c%10000==0:
print(c*1.0/len(xid),tot)
for i in count:
cmd4g[i]=count[i]+cmd4g[i]
del count
cmd4gx={}
for i in cmd4g:
if cmd4g[i]>0:
cmd4gx[i]=cmd4g[i]
print(len(cmd4gx))
pickle.dump(cmd4gx,open('cmd4g.p','w'))
| [] |
Antonio-Gabriel/easepay_backend | src/domain/enums/__init__.py | 9aaf4de27c9cc906911ae46ee61c75c6d92dc826 | from .months import Months
from .sizes import Size | [] |
blu-base/pygments | pygments/lexers/trafficscript.py | da799d14818ed538bf937684a19ce779ddde9446 | """
pygments.lexers.trafficscript
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Lexer for RiverBed's TrafficScript (RTS) language.
:copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from pygments.lexer import RegexLexer
from pygments.token import String, Number, Name, Keyword, Operator, Text, Comment
__all__ = ['RtsLexer']
class RtsLexer(RegexLexer):
"""
For `Riverbed Stingray Traffic Manager <http://www.riverbed.com/stingray>`_
.. versionadded:: 2.1
"""
name = 'TrafficScript'
aliases = ['trafficscript', 'rts']
filenames = ['*.rts']
tokens = {
'root' : [
(r"'(\\\\|\\[^\\]|[^'\\])*'", String),
(r'"', String, 'escapable-string'),
(r'(0x[0-9a-fA-F]+|\d+)', Number),
(r'\d+\.\d+', Number.Float),
(r'\$[a-zA-Z](\w|_)*', Name.Variable),
(r'(if|else|for(each)?|in|while|do|break|sub|return|import)', Keyword),
(r'[a-zA-Z][\w.]*', Name.Function),
(r'[-+*/%=,;(){}<>^.!~|&\[\]\?\:]', Operator),
(r'(>=|<=|==|!=|'
r'&&|\|\||'
r'\+=|.=|-=|\*=|/=|%=|<<=|>>=|&=|\|=|\^=|'
r'>>|<<|'
r'\+\+|--|=>)', Operator),
(r'[ \t\r]+', Text),
(r'#[^\n]*', Comment),
],
'escapable-string' : [
(r'\\[tsn]', String.Escape),
(r'[^"]', String),
(r'"', String, '#pop'),
],
}
| [] |
dimithras/pandas | pandas/tests/indexes/test_common.py | d321be6e2a43270625abf671d9e59f16529c4b48 | """
Collection of tests asserting things that should be true for
any index subclass. Makes use of the `indices` fixture defined
in pandas/tests/indexes/conftest.py.
"""
import re
import numpy as np
import pytest
from pandas._libs.tslibs import iNaT
from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion
import pandas as pd
from pandas import (
CategoricalIndex,
DatetimeIndex,
MultiIndex,
PeriodIndex,
RangeIndex,
TimedeltaIndex,
)
import pandas._testing as tm
class TestCommon:
def test_droplevel(self, index):
# GH 21115
if isinstance(index, MultiIndex):
# Tested separately in test_multi.py
return
assert index.droplevel([]).equals(index)
for level in index.name, [index.name]:
if isinstance(index.name, tuple) and level is index.name:
# GH 21121 : droplevel with tuple name
continue
with pytest.raises(ValueError):
index.droplevel(level)
for level in "wrong", ["wrong"]:
with pytest.raises(
KeyError,
match=r"'Requested level \(wrong\) does not match index name \(None\)'",
):
index.droplevel(level)
def test_constructor_non_hashable_name(self, index):
# GH 20527
if isinstance(index, MultiIndex):
pytest.skip("multiindex handled in test_multi.py")
message = "Index.name must be a hashable type"
renamed = [["1"]]
# With .rename()
with pytest.raises(TypeError, match=message):
index.rename(name=renamed)
# With .set_names()
with pytest.raises(TypeError, match=message):
index.set_names(names=renamed)
def test_constructor_unwraps_index(self, index):
if isinstance(index, pd.MultiIndex):
raise pytest.skip("MultiIndex has no ._data")
a = index
b = type(a)(a)
tm.assert_equal(a._data, b._data)
@pytest.mark.parametrize("itm", [101, "no_int"])
# FutureWarning from non-tuple sequence of nd indexing
@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_getitem_error(self, index, itm):
with pytest.raises(IndexError):
index[itm]
@pytest.mark.parametrize(
"fname, sname, expected_name",
[
("A", "A", "A"),
("A", "B", None),
("A", None, None),
(None, "B", None),
(None, None, None),
],
)
def test_corner_union(self, index, fname, sname, expected_name):
# GH 9943 9862
# Test unions with various name combinations
# Do not test MultiIndex or repeats
if isinstance(index, MultiIndex) or not index.is_unique:
pytest.skip("Not for MultiIndex or repeated indices")
# Test copy.union(copy)
first = index.copy().set_names(fname)
second = index.copy().set_names(sname)
union = first.union(second)
expected = index.copy().set_names(expected_name)
tm.assert_index_equal(union, expected)
# Test copy.union(empty)
first = index.copy().set_names(fname)
second = index.drop(index).set_names(sname)
union = first.union(second)
expected = index.copy().set_names(expected_name)
tm.assert_index_equal(union, expected)
# Test empty.union(copy)
first = index.drop(index).set_names(fname)
second = index.copy().set_names(sname)
union = first.union(second)
expected = index.copy().set_names(expected_name)
tm.assert_index_equal(union, expected)
# Test empty.union(empty)
first = index.drop(index).set_names(fname)
second = index.drop(index).set_names(sname)
union = first.union(second)
expected = index.drop(index).set_names(expected_name)
tm.assert_index_equal(union, expected)
@pytest.mark.parametrize(
"fname, sname, expected_name",
[
("A", "A", "A"),
("A", "B", None),
("A", None, None),
(None, "B", None),
(None, None, None),
],
)
def test_union_unequal(self, index, fname, sname, expected_name):
if isinstance(index, MultiIndex) or not index.is_unique:
pytest.skip("Not for MultiIndex or repeated indices")
# test copy.union(subset) - need sort for unicode and string
first = index.copy().set_names(fname)
second = index[1:].set_names(sname)
union = first.union(second).sort_values()
expected = index.set_names(expected_name).sort_values()
tm.assert_index_equal(union, expected)
@pytest.mark.parametrize(
"fname, sname, expected_name",
[
("A", "A", "A"),
("A", "B", None),
("A", None, None),
(None, "B", None),
(None, None, None),
],
)
def test_corner_intersect(self, index, fname, sname, expected_name):
# GH35847
# Test intersections with various name combinations
if isinstance(index, MultiIndex) or not index.is_unique:
pytest.skip("Not for MultiIndex or repeated indices")
# Test copy.intersection(copy)
first = index.copy().set_names(fname)
second = index.copy().set_names(sname)
intersect = first.intersection(second)
expected = index.copy().set_names(expected_name)
tm.assert_index_equal(intersect, expected)
# Test copy.intersection(empty)
first = index.copy().set_names(fname)
second = index.drop(index).set_names(sname)
intersect = first.intersection(second)
expected = index.drop(index).set_names(expected_name)
tm.assert_index_equal(intersect, expected)
# Test empty.intersection(copy)
first = index.drop(index).set_names(fname)
second = index.copy().set_names(sname)
intersect = first.intersection(second)
expected = index.drop(index).set_names(expected_name)
tm.assert_index_equal(intersect, expected)
# Test empty.intersection(empty)
first = index.drop(index).set_names(fname)
second = index.drop(index).set_names(sname)
intersect = first.intersection(second)
expected = index.drop(index).set_names(expected_name)
tm.assert_index_equal(intersect, expected)
@pytest.mark.parametrize(
"fname, sname, expected_name",
[
("A", "A", "A"),
("A", "B", None),
("A", None, None),
(None, "B", None),
(None, None, None),
],
)
def test_intersect_unequal(self, index, fname, sname, expected_name):
if isinstance(index, MultiIndex) or not index.is_unique:
pytest.skip("Not for MultiIndex or repeated indices")
# test copy.intersection(subset) - need sort for unicode and string
first = index.copy().set_names(fname)
second = index[1:].set_names(sname)
intersect = first.intersection(second).sort_values()
expected = index[1:].set_names(expected_name).sort_values()
tm.assert_index_equal(intersect, expected)
def test_to_flat_index(self, index):
# 22866
if isinstance(index, MultiIndex):
pytest.skip("Separate expectation for MultiIndex")
result = index.to_flat_index()
tm.assert_index_equal(result, index)
def test_set_name_methods(self, index):
new_name = "This is the new name for this index"
# don't tests a MultiIndex here (as its tested separated)
if isinstance(index, MultiIndex):
pytest.skip("Skip check for MultiIndex")
original_name = index.name
new_ind = index.set_names([new_name])
assert new_ind.name == new_name
assert index.name == original_name
res = index.rename(new_name, inplace=True)
# should return None
assert res is None
assert index.name == new_name
assert index.names == [new_name]
# FIXME: dont leave commented-out
# with pytest.raises(TypeError, match="list-like"):
# # should still fail even if it would be the right length
# ind.set_names("a")
with pytest.raises(ValueError, match="Level must be None"):
index.set_names("a", level=0)
# rename in place just leaves tuples and other containers alone
name = ("A", "B")
index.rename(name, inplace=True)
assert index.name == name
assert index.names == [name]
def test_copy_and_deepcopy(self, index):
from copy import copy, deepcopy
if isinstance(index, MultiIndex):
pytest.skip("Skip check for MultiIndex")
for func in (copy, deepcopy):
idx_copy = func(index)
assert idx_copy is not index
assert idx_copy.equals(index)
new_copy = index.copy(deep=True, name="banana")
assert new_copy.name == "banana"
def test_unique(self, index):
# don't test a MultiIndex here (as its tested separated)
# don't test a CategoricalIndex because categories change (GH 18291)
if isinstance(index, (MultiIndex, CategoricalIndex)):
pytest.skip("Skip check for MultiIndex/CategoricalIndex")
# GH 17896
expected = index.drop_duplicates()
for level in 0, index.name, None:
result = index.unique(level=level)
tm.assert_index_equal(result, expected)
msg = "Too many levels: Index has only 1 level, not 4"
with pytest.raises(IndexError, match=msg):
index.unique(level=3)
msg = (
fr"Requested level \(wrong\) does not match index name "
fr"\({re.escape(index.name.__repr__())}\)"
)
with pytest.raises(KeyError, match=msg):
index.unique(level="wrong")
def test_get_unique_index(self, index):
# MultiIndex tested separately
if not len(index) or isinstance(index, MultiIndex):
pytest.skip("Skip check for empty Index and MultiIndex")
idx = index[[0] * 5]
idx_unique = index[[0]]
# We test against `idx_unique`, so first we make sure it's unique
# and doesn't contain nans.
assert idx_unique.is_unique is True
try:
assert idx_unique.hasnans is False
except NotImplementedError:
pass
for dropna in [False, True]:
result = idx._get_unique_index(dropna=dropna)
tm.assert_index_equal(result, idx_unique)
# nans:
if not index._can_hold_na:
pytest.skip("Skip na-check if index cannot hold na")
if is_period_dtype(index.dtype):
vals = index[[0] * 5]._data
vals[0] = pd.NaT
elif needs_i8_conversion(index.dtype):
vals = index.asi8[[0] * 5]
vals[0] = iNaT
else:
vals = index.values[[0] * 5]
vals[0] = np.nan
vals_unique = vals[:2]
if index.dtype.kind in ["m", "M"]:
# i.e. needs_i8_conversion but not period_dtype, as above
vals = type(index._data)._simple_new(vals, dtype=index.dtype)
vals_unique = type(index._data)._simple_new(vals_unique, dtype=index.dtype)
idx_nan = index._shallow_copy(vals)
idx_unique_nan = index._shallow_copy(vals_unique)
assert idx_unique_nan.is_unique is True
assert idx_nan.dtype == index.dtype
assert idx_unique_nan.dtype == index.dtype
for dropna, expected in zip([False, True], [idx_unique_nan, idx_unique]):
for i in [idx_nan, idx_unique_nan]:
result = i._get_unique_index(dropna=dropna)
tm.assert_index_equal(result, expected)
def test_mutability(self, index):
if not len(index):
pytest.skip("Skip check for empty Index")
msg = "Index does not support mutable operations"
with pytest.raises(TypeError, match=msg):
index[0] = index[0]
def test_view(self, index):
assert index.view().name == index.name
def test_searchsorted_monotonic(self, index):
# GH17271
# not implemented for tuple searches in MultiIndex
# or Intervals searches in IntervalIndex
if isinstance(index, (MultiIndex, pd.IntervalIndex)):
pytest.skip("Skip check for MultiIndex/IntervalIndex")
# nothing to test if the index is empty
if index.empty:
pytest.skip("Skip check for empty Index")
value = index[0]
# determine the expected results (handle dupes for 'right')
expected_left, expected_right = 0, (index == value).argmin()
if expected_right == 0:
# all values are the same, expected_right should be length
expected_right = len(index)
# test _searchsorted_monotonic in all cases
# test searchsorted only for increasing
if index.is_monotonic_increasing:
ssm_left = index._searchsorted_monotonic(value, side="left")
assert expected_left == ssm_left
ssm_right = index._searchsorted_monotonic(value, side="right")
assert expected_right == ssm_right
ss_left = index.searchsorted(value, side="left")
assert expected_left == ss_left
ss_right = index.searchsorted(value, side="right")
assert expected_right == ss_right
elif index.is_monotonic_decreasing:
ssm_left = index._searchsorted_monotonic(value, side="left")
assert expected_left == ssm_left
ssm_right = index._searchsorted_monotonic(value, side="right")
assert expected_right == ssm_right
else:
# non-monotonic should raise.
with pytest.raises(ValueError):
index._searchsorted_monotonic(value, side="left")
def test_pickle(self, index):
original_name, index.name = index.name, "foo"
unpickled = tm.round_trip_pickle(index)
assert index.equals(unpickled)
index.name = original_name
def test_drop_duplicates(self, index, keep):
if isinstance(index, MultiIndex):
pytest.skip("MultiIndex is tested separately")
if isinstance(index, RangeIndex):
pytest.skip(
"RangeIndex is tested in test_drop_duplicates_no_duplicates "
"as it cannot hold duplicates"
)
if len(index) == 0:
pytest.skip(
"empty index is tested in test_drop_duplicates_no_duplicates "
"as it cannot hold duplicates"
)
# make unique index
holder = type(index)
unique_values = list(set(index))
unique_idx = holder(unique_values)
# make duplicated index
n = len(unique_idx)
duplicated_selection = np.random.choice(n, int(n * 1.5))
idx = holder(unique_idx.values[duplicated_selection])
# Series.duplicated is tested separately
expected_duplicated = (
pd.Series(duplicated_selection).duplicated(keep=keep).values
)
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected_duplicated)
# Series.drop_duplicates is tested separately
expected_dropped = holder(pd.Series(idx).drop_duplicates(keep=keep))
tm.assert_index_equal(idx.drop_duplicates(keep=keep), expected_dropped)
def test_drop_duplicates_no_duplicates(self, index):
if isinstance(index, MultiIndex):
pytest.skip("MultiIndex is tested separately")
# make unique index
if isinstance(index, RangeIndex):
# RangeIndex cannot have duplicates
unique_idx = index
else:
holder = type(index)
unique_values = list(set(index))
unique_idx = holder(unique_values)
# check on unique index
expected_duplicated = np.array([False] * len(unique_idx), dtype="bool")
tm.assert_numpy_array_equal(unique_idx.duplicated(), expected_duplicated)
result_dropped = unique_idx.drop_duplicates()
tm.assert_index_equal(result_dropped, unique_idx)
# validate shallow copy
assert result_dropped is not unique_idx
def test_drop_duplicates_inplace(self, index):
msg = r"drop_duplicates\(\) got an unexpected keyword argument"
with pytest.raises(TypeError, match=msg):
index.drop_duplicates(inplace=True)
def test_has_duplicates(self, index):
holder = type(index)
if not len(index) or isinstance(index, (MultiIndex, RangeIndex)):
# MultiIndex tested separately in:
# tests/indexes/multi/test_unique_and_duplicates.
# RangeIndex is unique by definition.
pytest.skip("Skip check for empty Index, MultiIndex, and RangeIndex")
idx = holder([index[0]] * 5)
assert idx.is_unique is False
assert idx.has_duplicates is True
@pytest.mark.parametrize(
"dtype",
["int64", "uint64", "float64", "category", "datetime64[ns]", "timedelta64[ns]"],
)
def test_astype_preserves_name(self, index, dtype):
# https://github.com/pandas-dev/pandas/issues/32013
if isinstance(index, MultiIndex):
index.names = ["idx" + str(i) for i in range(index.nlevels)]
else:
index.name = "idx"
try:
# Some of these conversions cannot succeed so we use a try / except
result = index.astype(dtype)
except (ValueError, TypeError, NotImplementedError, SystemError):
return
if isinstance(index, MultiIndex):
assert result.names == index.names
else:
assert result.name == index.name
def test_ravel_deprecation(self, index):
# GH#19956 ravel returning ndarray is deprecated
with tm.assert_produces_warning(FutureWarning):
index.ravel()
@pytest.mark.parametrize("na_position", [None, "middle"])
def test_sort_values_invalid_na_position(index_with_missing, na_position):
if isinstance(index_with_missing, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# datetime-like indices will get na_position kwarg as part of
# synchronizing duplicate-sorting behavior, because we currently expect
# them, other indices, and Series to sort differently (xref 35922)
pytest.xfail("sort_values does not support na_position kwarg")
elif isinstance(index_with_missing, (CategoricalIndex, MultiIndex)):
pytest.xfail("missing value sorting order not defined for index type")
if na_position not in ["first", "last"]:
with pytest.raises(ValueError, match=f"invalid na_position: {na_position}"):
index_with_missing.sort_values(na_position=na_position)
@pytest.mark.parametrize("na_position", ["first", "last"])
def test_sort_values_with_missing(index_with_missing, na_position):
# GH 35584. Test that sort_values works with missing values,
# sort non-missing and place missing according to na_position
if isinstance(index_with_missing, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
# datetime-like indices will get na_position kwarg as part of
# synchronizing duplicate-sorting behavior, because we currently expect
# them, other indices, and Series to sort differently (xref 35922)
pytest.xfail("sort_values does not support na_position kwarg")
elif isinstance(index_with_missing, (CategoricalIndex, MultiIndex)):
pytest.xfail("missing value sorting order not defined for index type")
missing_count = np.sum(index_with_missing.isna())
not_na_vals = index_with_missing[index_with_missing.notna()].values
sorted_values = np.sort(not_na_vals)
if na_position == "first":
sorted_values = np.concatenate([[None] * missing_count, sorted_values])
else:
sorted_values = np.concatenate([sorted_values, [None] * missing_count])
expected = type(index_with_missing)(sorted_values)
result = index_with_missing.sort_values(na_position=na_position)
tm.assert_index_equal(result, expected)
| [((18137, 18193), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""na_position"""', "[None, 'middle']"], {}), "('na_position', [None, 'middle'])\n", (18160, 18193), False, 'import pytest\n'), ((19004, 19061), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""na_position"""', "['first', 'last']"], {}), "('na_position', ['first', 'last'])\n", (19027, 19061), False, 'import pytest\n'), ((2026, 2073), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""itm"""', "[101, 'no_int']"], {}), "('itm', [101, 'no_int'])\n", (2049, 2073), False, 'import pytest\n'), ((2138, 2189), 'pytest.mark.filterwarnings', 'pytest.mark.filterwarnings', (['"""ignore::FutureWarning"""'], {}), "('ignore::FutureWarning')\n", (2164, 2189), False, 'import pytest\n'), ((2305, 2459), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""fname, sname, expected_name"""', "[('A', 'A', 'A'), ('A', 'B', None), ('A', None, None), (None, 'B', None), (\n None, None, None)]"], {}), "('fname, sname, expected_name', [('A', 'A', 'A'), (\n 'A', 'B', None), ('A', None, None), (None, 'B', None), (None, None, None)])\n", (2328, 2459), False, 'import pytest\n'), ((3969, 4123), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""fname, sname, expected_name"""', "[('A', 'A', 'A'), ('A', 'B', None), ('A', None, None), (None, 'B', None), (\n None, None, None)]"], {}), "('fname, sname, expected_name', [('A', 'A', 'A'), (\n 'A', 'B', None), ('A', None, None), (None, 'B', None), (None, None, None)])\n", (3992, 4123), False, 'import pytest\n'), ((4741, 4895), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""fname, sname, expected_name"""', "[('A', 'A', 'A'), ('A', 'B', None), ('A', None, None), (None, 'B', None), (\n None, None, None)]"], {}), "('fname, sname, expected_name', [('A', 'A', 'A'), (\n 'A', 'B', None), ('A', None, None), (None, 'B', None), (None, None, None)])\n", (4764, 4895), False, 'import pytest\n'), ((6465, 6619), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""fname, sname, expected_name"""', "[('A', 'A', 'A'), ('A', 'B', None), ('A', None, None), (None, 'B', None), (\n None, None, None)]"], {}), "('fname, sname, expected_name', [('A', 'A', 'A'), (\n 'A', 'B', None), ('A', None, None), (None, 'B', None), (None, None, None)])\n", (6488, 6619), False, 'import pytest\n'), ((17159, 17276), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""dtype"""', "['int64', 'uint64', 'float64', 'category', 'datetime64[ns]', 'timedelta64[ns]']"], {}), "('dtype', ['int64', 'uint64', 'float64', 'category',\n 'datetime64[ns]', 'timedelta64[ns]'])\n", (17182, 17276), False, 'import pytest\n'), ((19942, 19962), 'numpy.sort', 'np.sort', (['not_na_vals'], {}), '(not_na_vals)\n', (19949, 19962), True, 'import numpy as np\n'), ((20293, 20332), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result', 'expected'], {}), '(result, expected)\n', (20314, 20332), True, 'import pandas._testing as tm\n'), ((1986, 2019), 'pandas._testing.assert_equal', 'tm.assert_equal', (['a._data', 'b._data'], {}), '(a._data, b._data)\n', (2001, 2019), True, 'import pandas._testing as tm\n'), ((3097, 3135), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['union', 'expected'], {}), '(union, expected)\n', (3118, 3135), True, 'import pandas._testing as tm\n'), ((3369, 3407), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['union', 'expected'], {}), '(union, expected)\n', (3390, 3407), True, 'import pandas._testing as tm\n'), ((3641, 3679), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['union', 'expected'], {}), '(union, expected)\n', (3662, 3679), True, 'import pandas._testing as tm\n'), ((3924, 3962), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['union', 'expected'], {}), '(union, expected)\n', (3945, 3962), True, 'import pandas._testing as tm\n'), ((4696, 4734), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['union', 'expected'], {}), '(union, expected)\n', (4717, 4734), True, 'import pandas._testing as tm\n'), ((5513, 5555), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['intersect', 'expected'], {}), '(intersect, expected)\n', (5534, 5555), True, 'import pandas._testing as tm\n'), ((5812, 5854), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['intersect', 'expected'], {}), '(intersect, expected)\n', (5833, 5854), True, 'import pandas._testing as tm\n'), ((6111, 6153), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['intersect', 'expected'], {}), '(intersect, expected)\n', (6132, 6153), True, 'import pandas._testing as tm\n'), ((6416, 6458), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['intersect', 'expected'], {}), '(intersect, expected)\n', (6437, 6458), True, 'import pandas._testing as tm\n'), ((7218, 7260), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['intersect', 'expected'], {}), '(intersect, expected)\n', (7239, 7260), True, 'import pandas._testing as tm\n'), ((7472, 7508), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result', 'index'], {}), '(result, index)\n', (7493, 7508), True, 'import pandas._testing as tm\n'), ((10810, 10838), 'pandas.core.dtypes.common.is_period_dtype', 'is_period_dtype', (['index.dtype'], {}), '(index.dtype)\n', (10825, 10838), False, 'from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion\n'), ((14146, 14173), 'pandas._testing.round_trip_pickle', 'tm.round_trip_pickle', (['index'], {}), '(index)\n', (14166, 14173), True, 'import pandas._testing as tm\n'), ((16294, 16343), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result_dropped', 'unique_idx'], {}), '(result_dropped, unique_idx)\n', (16315, 16343), True, 'import pandas._testing as tm\n'), ((18587, 18649), 'pytest.xfail', 'pytest.xfail', (['"""sort_values does not support na_position kwarg"""'], {}), "('sort_values does not support na_position kwarg')\n", (18599, 18649), False, 'import pytest\n'), ((19580, 19642), 'pytest.xfail', 'pytest.xfail', (['"""sort_values does not support na_position kwarg"""'], {}), "('sort_values does not support na_position kwarg')\n", (19592, 19642), False, 'import pytest\n'), ((20018, 20073), 'numpy.concatenate', 'np.concatenate', (['[[None] * missing_count, sorted_values]'], {}), '([[None] * missing_count, sorted_values])\n', (20032, 20073), True, 'import numpy as np\n'), ((20108, 20163), 'numpy.concatenate', 'np.concatenate', (['[sorted_values, [None] * missing_count]'], {}), '([sorted_values, [None] * missing_count])\n', (20122, 20163), True, 'import numpy as np\n'), ((1402, 1452), 'pytest.skip', 'pytest.skip', (['"""multiindex handled in test_multi.py"""'], {}), "('multiindex handled in test_multi.py')\n", (1413, 1452), False, 'import pytest\n'), ((1574, 1613), 'pytest.raises', 'pytest.raises', (['TypeError'], {'match': 'message'}), '(TypeError, match=message)\n', (1587, 1613), False, 'import pytest\n'), ((1696, 1735), 'pytest.raises', 'pytest.raises', (['TypeError'], {'match': 'message'}), '(TypeError, match=message)\n', (1709, 1735), False, 'import pytest\n'), ((1897, 1936), 'pytest.skip', 'pytest.skip', (['"""MultiIndex has no ._data"""'], {}), "('MultiIndex has no ._data')\n", (1908, 1936), False, 'import pytest\n'), ((2249, 2274), 'pytest.raises', 'pytest.raises', (['IndexError'], {}), '(IndexError)\n', (2262, 2274), False, 'import pytest\n'), ((2816, 2869), 'pytest.skip', 'pytest.skip', (['"""Not for MultiIndex or repeated indices"""'], {}), "('Not for MultiIndex or repeated indices')\n", (2827, 2869), False, 'import pytest\n'), ((4360, 4413), 'pytest.skip', 'pytest.skip', (['"""Not for MultiIndex or repeated indices"""'], {}), "('Not for MultiIndex or repeated indices')\n", (4371, 4413), False, 'import pytest\n'), ((5214, 5267), 'pytest.skip', 'pytest.skip', (['"""Not for MultiIndex or repeated indices"""'], {}), "('Not for MultiIndex or repeated indices')\n", (5225, 5267), False, 'import pytest\n'), ((6860, 6913), 'pytest.skip', 'pytest.skip', (['"""Not for MultiIndex or repeated indices"""'], {}), "('Not for MultiIndex or repeated indices')\n", (6871, 6913), False, 'import pytest\n'), ((7373, 7423), 'pytest.skip', 'pytest.skip', (['"""Separate expectation for MultiIndex"""'], {}), "('Separate expectation for MultiIndex')\n", (7384, 7423), False, 'import pytest\n'), ((7732, 7772), 'pytest.skip', 'pytest.skip', (['"""Skip check for MultiIndex"""'], {}), "('Skip check for MultiIndex')\n", (7743, 7772), False, 'import pytest\n'), ((8341, 8394), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""Level must be None"""'}), "(ValueError, match='Level must be None')\n", (8354, 8394), False, 'import pytest\n'), ((8790, 8830), 'pytest.skip', 'pytest.skip', (['"""Skip check for MultiIndex"""'], {}), "('Skip check for MultiIndex')\n", (8801, 8830), False, 'import pytest\n'), ((9337, 9394), 'pytest.skip', 'pytest.skip', (['"""Skip check for MultiIndex/CategoricalIndex"""'], {}), "('Skip check for MultiIndex/CategoricalIndex')\n", (9348, 9394), False, 'import pytest\n'), ((9559, 9598), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result', 'expected'], {}), '(result, expected)\n', (9580, 9598), True, 'import pandas._testing as tm\n'), ((9676, 9712), 'pytest.raises', 'pytest.raises', (['IndexError'], {'match': 'msg'}), '(IndexError, match=msg)\n', (9689, 9712), False, 'import pytest\n'), ((9912, 9946), 'pytest.raises', 'pytest.raises', (['KeyError'], {'match': 'msg'}), '(KeyError, match=msg)\n', (9925, 9946), False, 'import pytest\n'), ((10144, 10200), 'pytest.skip', 'pytest.skip', (['"""Skip check for empty Index and MultiIndex"""'], {}), "('Skip check for empty Index and MultiIndex')\n", (10155, 10200), False, 'import pytest\n'), ((10639, 10680), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result', 'idx_unique'], {}), '(result, idx_unique)\n', (10660, 10680), True, 'import pandas._testing as tm\n'), ((10745, 10797), 'pytest.skip', 'pytest.skip', (['"""Skip na-check if index cannot hold na"""'], {}), "('Skip na-check if index cannot hold na')\n", (10756, 10797), False, 'import pytest\n'), ((10922, 10954), 'pandas.core.dtypes.common.needs_i8_conversion', 'needs_i8_conversion', (['index.dtype'], {}), '(index.dtype)\n', (10941, 10954), False, 'from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion\n'), ((11984, 12025), 'pytest.skip', 'pytest.skip', (['"""Skip check for empty Index"""'], {}), "('Skip check for empty Index')\n", (11995, 12025), False, 'import pytest\n'), ((12097, 12132), 'pytest.raises', 'pytest.raises', (['TypeError'], {'match': 'msg'}), '(TypeError, match=msg)\n', (12110, 12132), False, 'import pytest\n'), ((12497, 12551), 'pytest.skip', 'pytest.skip', (['"""Skip check for MultiIndex/IntervalIndex"""'], {}), "('Skip check for MultiIndex/IntervalIndex')\n", (12508, 12551), False, 'import pytest\n'), ((12637, 12678), 'pytest.skip', 'pytest.skip', (['"""Skip check for empty Index"""'], {}), "('Skip check for empty Index')\n", (12648, 12678), False, 'import pytest\n'), ((14352, 14398), 'pytest.skip', 'pytest.skip', (['"""MultiIndex is tested separately"""'], {}), "('MultiIndex is tested separately')\n", (14363, 14398), False, 'import pytest\n'), ((14453, 14565), 'pytest.skip', 'pytest.skip', (['"""RangeIndex is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicates"""'], {}), "(\n 'RangeIndex is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicates'\n )\n", (14464, 14565), False, 'import pytest\n'), ((14645, 14758), 'pytest.skip', 'pytest.skip', (['"""empty index is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicates"""'], {}), "(\n 'empty index is tested in test_drop_duplicates_no_duplicates as it cannot hold duplicates'\n )\n", (14656, 14758), False, 'import pytest\n'), ((15701, 15747), 'pytest.skip', 'pytest.skip', (['"""MultiIndex is tested separately"""'], {}), "('MultiIndex is tested separately')\n", (15712, 15747), False, 'import pytest\n'), ((16561, 16596), 'pytest.raises', 'pytest.raises', (['TypeError'], {'match': 'msg'}), '(TypeError, match=msg)\n', (16574, 16596), False, 'import pytest\n'), ((16965, 17034), 'pytest.skip', 'pytest.skip', (['"""Skip check for empty Index, MultiIndex, and RangeIndex"""'], {}), "('Skip check for empty Index, MultiIndex, and RangeIndex')\n", (16976, 17034), False, 'import pytest\n'), ((18065, 18106), 'pandas._testing.assert_produces_warning', 'tm.assert_produces_warning', (['FutureWarning'], {}), '(FutureWarning)\n', (18091, 18106), True, 'import pandas._testing as tm\n'), ((18731, 18801), 'pytest.xfail', 'pytest.xfail', (['"""missing value sorting order not defined for index type"""'], {}), "('missing value sorting order not defined for index type')\n", (18743, 18801), False, 'import pytest\n'), ((18861, 18931), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': 'f"""invalid na_position: {na_position}"""'}), "(ValueError, match=f'invalid na_position: {na_position}')\n", (18874, 18931), False, 'import pytest\n'), ((19724, 19794), 'pytest.xfail', 'pytest.xfail', (['"""missing value sorting order not defined for index type"""'], {}), "('missing value sorting order not defined for index type')\n", (19736, 19794), False, 'import pytest\n'), ((961, 986), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (974, 986), False, 'import pytest\n'), ((1086, 1190), 'pytest.raises', 'pytest.raises', (['KeyError'], {'match': '"""\'Requested level \\\\(wrong\\\\) does not match index name \\\\(None\\\\)\'"""'}), '(KeyError, match=\n "\'Requested level \\\\(wrong\\\\) does not match index name \\\\(None\\\\)\'")\n', (1099, 1190), False, 'import pytest\n'), ((11866, 11905), 'pandas._testing.assert_index_equal', 'tm.assert_index_equal', (['result', 'expected'], {}), '(result, expected)\n', (11887, 11905), True, 'import pandas._testing as tm\n'), ((13944, 13969), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (13957, 13969), False, 'import pytest\n'), ((15222, 15253), 'pandas.Series', 'pd.Series', (['duplicated_selection'], {}), '(duplicated_selection)\n', (15231, 15253), True, 'import pandas as pd\n'), ((15466, 15480), 'pandas.Series', 'pd.Series', (['idx'], {}), '(idx)\n', (15475, 15480), True, 'import pandas as pd\n')] |
leasanchez/BiorbdOptim | tests/test_dynamics.py | 28fac818af031668ecd82bc1929f78303c5d58d2 | import pytest
import numpy as np
from casadi import MX, SX
import biorbd_casadi as biorbd
from bioptim.dynamics.configure_problem import ConfigureProblem
from bioptim.dynamics.dynamics_functions import DynamicsFunctions
from bioptim.interfaces.biorbd_interface import BiorbdInterface
from bioptim.misc.enums import ControlType
from bioptim.optimization.non_linear_program import NonLinearProgram
from bioptim.optimization.optimization_vector import OptimizationVector
from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics
from .utils import TestUtils
class OptimalControlProgram:
def __init__(self, nlp):
self.n_phases = 1
self.nlp = [nlp]
self.v = OptimizationVector(self)
@pytest.mark.parametrize("cx", [MX, SX])
@pytest.mark.parametrize("with_external_force", [False, True])
@pytest.mark.parametrize("with_contact", [False, True])
def test_torque_driven(with_contact, with_external_force, cx):
# Prepare the program
nlp = NonLinearProgram()
nlp.model = biorbd.Model(
TestUtils.bioptim_folder() + "/examples/getting_started/models/2segments_4dof_2contacts.bioMod"
)
nlp.ns = 5
nlp.cx = cx
nlp.x_bounds = np.zeros((nlp.model.nbQ() * 3, 1))
nlp.u_bounds = np.zeros((nlp.model.nbQ(), 1))
ocp = OptimalControlProgram(nlp)
nlp.control_type = ControlType.CONSTANT
NonLinearProgram.add(ocp, "dynamics_type", Dynamics(DynamicsFcn.TORQUE_DRIVEN, with_contact=with_contact), False)
np.random.seed(42)
if with_external_force:
external_forces = [np.random.rand(6, nlp.model.nbSegment(), nlp.ns)]
nlp.external_forces = BiorbdInterface.convert_array_to_external_forces(external_forces)[0]
# Prepare the dynamics
ConfigureProblem.initialize(ocp, nlp)
# Test the results
states = np.random.rand(nlp.states.shape, nlp.ns)
controls = np.random.rand(nlp.controls.shape, nlp.ns)
params = np.random.rand(nlp.parameters.shape, nlp.ns)
x_out = np.array(nlp.dynamics_func(states, controls, params))
if with_contact:
contact_out = np.array(nlp.contact_forces_func(states, controls, params))
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.8631034, 0.3251833, 0.1195942, 0.4937956, -7.7700092, -7.5782306, 21.7073786, -16.3059315],
)
np.testing.assert_almost_equal(contact_out[:, 0], [-47.8131136, 111.1726516, -24.4449121])
else:
np.testing.assert_almost_equal(
x_out[:, 0], [0.6118529, 0.785176, 0.6075449, 0.8083973, -0.3214905, -0.1912131, 0.6507164, -0.2359716]
)
np.testing.assert_almost_equal(contact_out[:, 0], [-2.444071, 128.8816865, 2.7245124])
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.86310343, 0.32518332, 0.11959425, 0.4937956, 0.30731739, -9.97912778, 1.15263778, 36.02430956],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.30241366, -10.38503791, 1.60445173, 35.80238642],
)
@pytest.mark.parametrize("cx", [MX, SX])
@pytest.mark.parametrize("with_external_force", [False, True])
@pytest.mark.parametrize("with_contact", [False, True])
def test_torque_derivative_driven(with_contact, with_external_force, cx):
# Prepare the program
nlp = NonLinearProgram()
nlp.model = biorbd.Model(
TestUtils.bioptim_folder() + "/examples/getting_started/models/2segments_4dof_2contacts.bioMod"
)
nlp.ns = 5
nlp.cx = cx
nlp.x_bounds = np.zeros((nlp.model.nbQ() * 3, 1))
nlp.u_bounds = np.zeros((nlp.model.nbQ(), 1))
ocp = OptimalControlProgram(nlp)
nlp.control_type = ControlType.CONSTANT
NonLinearProgram.add(
ocp, "dynamics_type", Dynamics(DynamicsFcn.TORQUE_DERIVATIVE_DRIVEN, with_contact=with_contact), False
)
np.random.seed(42)
if with_external_force:
external_forces = [np.random.rand(6, nlp.model.nbSegment(), nlp.ns)]
nlp.external_forces = BiorbdInterface.convert_array_to_external_forces(external_forces)[0]
# Prepare the dynamics
ConfigureProblem.initialize(ocp, nlp)
# Test the results
states = np.random.rand(nlp.states.shape, nlp.ns)
controls = np.random.rand(nlp.controls.shape, nlp.ns)
params = np.random.rand(nlp.parameters.shape, nlp.ns)
x_out = np.array(nlp.dynamics_func(states, controls, params))
if with_contact:
contact_out = np.array(nlp.contact_forces_func(states, controls, params))
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.8631034,
0.3251833,
0.1195942,
0.4937956,
-7.7700092,
-7.5782306,
21.7073786,
-16.3059315,
0.8074402,
0.4271078,
0.417411,
0.3232029,
],
)
np.testing.assert_almost_equal(contact_out[:, 0], [-47.8131136, 111.1726516, -24.4449121])
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.61185289,
0.78517596,
0.60754485,
0.80839735,
-0.32149054,
-0.19121314,
0.65071636,
-0.23597164,
0.38867729,
0.54269608,
0.77224477,
0.72900717,
],
)
np.testing.assert_almost_equal(contact_out[:, 0], [-2.444071, 128.8816865, 2.7245124])
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.86310343,
0.32518332,
0.11959425,
0.4937956,
0.30731739,
-9.97912778,
1.15263778,
36.02430956,
0.80744016,
0.42710779,
0.417411,
0.32320293,
],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.61185289,
0.78517596,
0.60754485,
0.80839735,
-0.30241366,
-10.38503791,
1.60445173,
35.80238642,
0.38867729,
0.54269608,
0.77224477,
0.72900717,
],
)
@pytest.mark.parametrize("cx", [MX, SX])
@pytest.mark.parametrize("with_external_force", [False, True])
@pytest.mark.parametrize("with_contact", [False, True])
def test_torque_activation_driven(with_contact, with_external_force, cx):
# Prepare the program
nlp = NonLinearProgram()
nlp.model = biorbd.Model(
TestUtils.bioptim_folder() + "/examples/getting_started/models/2segments_4dof_2contacts.bioMod"
)
nlp.ns = 5
nlp.cx = cx
nlp.x_bounds = np.zeros((nlp.model.nbQ() * 2, 1))
nlp.u_bounds = np.zeros((nlp.model.nbQ(), 1))
ocp = OptimalControlProgram(nlp)
nlp.control_type = ControlType.CONSTANT
NonLinearProgram.add(
ocp, "dynamics_type", Dynamics(DynamicsFcn.TORQUE_ACTIVATIONS_DRIVEN, with_contact=with_contact), False
)
np.random.seed(42)
if with_external_force:
external_forces = [np.random.rand(6, nlp.model.nbSegment(), nlp.ns)]
nlp.external_forces = BiorbdInterface.convert_array_to_external_forces(external_forces)[0]
# Prepare the dynamics
ConfigureProblem.initialize(ocp, nlp)
# Test the results
states = np.random.rand(nlp.states.shape, nlp.ns)
controls = np.random.rand(nlp.controls.shape, nlp.ns)
params = np.random.rand(nlp.parameters.shape, nlp.ns)
x_out = np.array(nlp.dynamics_func(states, controls, params))
if with_contact:
contact_out = np.array(nlp.contact_forces_func(states, controls, params))
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.8631, 0.32518, 0.11959, 0.4938, 19.01887, 18.51503, -53.08574, 58.48719],
decimal=5,
)
np.testing.assert_almost_equal(contact_out[:, 0], [109.8086936, 3790.3932439, -3571.7858574])
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.61185289, 0.78517596, 0.60754485, 0.80839735, 0.78455384, -0.16844256, -1.56184114, 1.97658587],
decimal=5,
)
np.testing.assert_almost_equal(contact_out[:, 0], [-7.88958997, 329.70828173, -263.55516549])
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
8.63103426e-01,
3.25183322e-01,
1.19594246e-01,
4.93795596e-01,
1.73558072e01,
-4.69891264e01,
1.81396922e02,
3.61170139e03,
],
decimal=5,
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
6.11852895e-01,
7.85175961e-01,
6.07544852e-01,
8.08397348e-01,
-2.38262975e01,
-5.82033454e01,
1.27439020e02,
3.66531163e03,
],
decimal=5,
)
@pytest.mark.parametrize("cx", [MX, SX])
@pytest.mark.parametrize("with_external_force", [False, True])
@pytest.mark.parametrize("with_contact", [False, True])
@pytest.mark.parametrize("with_torque", [False, True])
@pytest.mark.parametrize("with_excitations", [False, True])
def test_muscle_driven(with_excitations, with_contact, with_torque, with_external_force, cx):
# Prepare the program
nlp = NonLinearProgram()
nlp.model = biorbd.Model(
TestUtils.bioptim_folder() + "/examples/muscle_driven_ocp/models/arm26_with_contact.bioMod"
)
nlp.ns = 5
nlp.cx = cx
nlp.x_bounds = np.zeros((nlp.model.nbQ() * 2 + nlp.model.nbMuscles(), 1))
nlp.u_bounds = np.zeros((nlp.model.nbMuscles(), 1))
ocp = OptimalControlProgram(nlp)
nlp.control_type = ControlType.CONSTANT
NonLinearProgram.add(
ocp,
"dynamics_type",
Dynamics(
DynamicsFcn.MUSCLE_DRIVEN,
with_torque=with_torque,
with_excitations=with_excitations,
with_contact=with_contact,
),
False,
)
np.random.seed(42)
if with_external_force:
external_forces = [np.random.rand(6, nlp.model.nbSegment(), nlp.ns)]
nlp.external_forces = BiorbdInterface.convert_array_to_external_forces(external_forces)[0]
# Prepare the dynamics
ConfigureProblem.initialize(ocp, nlp)
# Test the results
states = np.random.rand(nlp.states.shape, nlp.ns)
controls = np.random.rand(nlp.controls.shape, nlp.ns)
params = np.random.rand(nlp.parameters.shape, nlp.ns)
x_out = np.array(nlp.dynamics_func(states, controls, params))
if with_contact: # Warning this test is a bit bogus, there since the model does not have contacts
if with_torque:
if with_excitations:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.6158501,
0.50313626,
0.64241928,
1.07179622,
-33.76217857,
36.21815923,
46.87928022,
-1.80189035,
53.3914525,
48.30056919,
63.69373374,
-28.15700995,
],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
1.83404510e-01,
6.11852895e-01,
7.85175961e-01,
-9.29662878e00,
3.00872062e02,
-9.50354903e02,
8.60630831e00,
3.19433638e00,
2.97405608e01,
-2.02754226e01,
-2.32467778e01,
-4.19135012e01,
],
decimal=6,
)
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[6.15850098e-01, 5.03136259e-01, 6.42419278e-01, -8.06478367e00, 2.42279101e02, -7.72114103e02],
decimal=6,
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[1.83404510e-01, 6.11852895e-01, 7.85175961e-01, -3.80892207e00, 1.20476051e02, -4.33291346e02],
decimal=6,
)
else:
if with_excitations:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.6158501,
0.50313626,
0.64241928,
0.91952705,
-39.04876174,
45.31837288,
55.65557816,
50.47052688,
0.36025589,
58.92377491,
29.70094194,
-15.13534937,
],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
1.83404510e-01,
6.11852895e-01,
7.85175961e-01,
-9.72712350e00,
3.10866170e02,
-9.82725656e02,
-7.72228930e00,
-1.13759732e01,
9.51906209e01,
4.45077128e00,
-5.20261014e00,
-2.80864106e01,
],
decimal=6,
)
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[1.83404510e-01, 6.11852895e-01, 7.85175961e-01, -9.72712350e00, 3.10866170e02, -9.82725656e02],
decimal=6,
)
else:
if with_torque:
if with_excitations:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.6158501,
0.50313626,
0.64241928,
1.07179622,
-33.76217857,
36.21815923,
46.87928022,
-1.80189035,
53.3914525,
48.30056919,
63.69373374,
-28.15700995,
],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
1.83404510e-01,
6.11852895e-01,
7.85175961e-01,
-9.29662878e00,
3.00872062e02,
-9.50354903e02,
8.60630831e00,
3.19433638e00,
2.97405608e01,
-2.02754226e01,
-2.32467778e01,
-4.19135012e01,
],
decimal=6,
)
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[6.15850098e-01, 5.03136259e-01, 6.42419278e-01, -8.06478367e00, 2.42279101e02, -7.72114103e02],
decimal=6,
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[1.83404510e-01, 6.11852895e-01, 7.85175961e-01, -3.80892207e00, 1.20476051e02, -4.33291346e02],
decimal=6,
)
else:
if with_excitations:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[
0.6158501,
0.50313626,
0.64241928,
0.91952705,
-39.04876174,
45.31837288,
55.65557816,
50.47052688,
0.36025589,
58.92377491,
29.70094194,
-15.13534937,
],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[
1.83404510e-01,
6.11852895e-01,
7.85175961e-01,
-9.72712350e00,
3.10866170e02,
-9.82725656e02,
-7.72228930e00,
-1.13759732e01,
9.51906209e01,
4.45077128e00,
-5.20261014e00,
-2.80864106e01,
],
decimal=6,
)
else:
if with_external_force:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288],
)
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[1.83404510e-01, 6.11852895e-01, 7.85175961e-01, -9.72712350e00, 3.10866170e02, -9.82725656e02],
decimal=6,
)
@pytest.mark.parametrize("with_contact", [False, True])
def test_custom_dynamics(with_contact):
def custom_dynamic(states, controls, parameters, nlp, with_contact=False) -> tuple:
DynamicsFunctions.apply_parameters(parameters, nlp)
q = DynamicsFunctions.get(nlp.states["q"], states)
qdot = DynamicsFunctions.get(nlp.states["qdot"], states)
tau = DynamicsFunctions.get(nlp.controls["tau"], controls)
dq = DynamicsFunctions.compute_qdot(nlp, q, qdot)
ddq = DynamicsFunctions.forward_dynamics(nlp, q, qdot, tau, with_contact)
return dq, ddq
def configure(ocp, nlp, with_contact=None):
ConfigureProblem.configure_q(nlp, True, False)
ConfigureProblem.configure_qdot(nlp, True, False)
ConfigureProblem.configure_tau(nlp, False, True)
ConfigureProblem.configure_dynamics_function(ocp, nlp, custom_dynamic, with_contact=with_contact)
if with_contact:
ConfigureProblem.configure_contact_function(ocp, nlp, DynamicsFunctions.forces_from_torque_driven)
# Prepare the program
nlp = NonLinearProgram()
nlp.model = biorbd.Model(
TestUtils.bioptim_folder() + "/examples/getting_started/models/2segments_4dof_2contacts.bioMod"
)
nlp.ns = 5
nlp.cx = MX
nlp.x_bounds = np.zeros((nlp.model.nbQ() * 3, 1))
nlp.u_bounds = np.zeros((nlp.model.nbQ(), 1))
ocp = OptimalControlProgram(nlp)
nlp.control_type = ControlType.CONSTANT
NonLinearProgram.add(
ocp, "dynamics_type", Dynamics(configure, dynamic_function=custom_dynamic, with_contact=with_contact), False
)
np.random.seed(42)
# Prepare the dynamics
ConfigureProblem.initialize(ocp, nlp)
# Test the results
states = np.random.rand(nlp.states.shape, nlp.ns)
controls = np.random.rand(nlp.controls.shape, nlp.ns)
params = np.random.rand(nlp.parameters.shape, nlp.ns)
x_out = np.array(nlp.dynamics_func(states, controls, params))
if with_contact:
contact_out = np.array(nlp.contact_forces_func(states, controls, params))
np.testing.assert_almost_equal(
x_out[:, 0], [0.6118529, 0.785176, 0.6075449, 0.8083973, -0.3214905, -0.1912131, 0.6507164, -0.2359716]
)
np.testing.assert_almost_equal(contact_out[:, 0], [-2.444071, 128.8816865, 2.7245124])
else:
np.testing.assert_almost_equal(
x_out[:, 0],
[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.30241366, -10.38503791, 1.60445173, 35.80238642],
)
| [((724, 763), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""cx"""', '[MX, SX]'], {}), "('cx', [MX, SX])\n", (747, 763), False, 'import pytest\n'), ((765, 826), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_external_force"""', '[False, True]'], {}), "('with_external_force', [False, True])\n", (788, 826), False, 'import pytest\n'), ((828, 882), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_contact"""', '[False, True]'], {}), "('with_contact', [False, True])\n", (851, 882), False, 'import pytest\n'), ((3229, 3268), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""cx"""', '[MX, SX]'], {}), "('cx', [MX, SX])\n", (3252, 3268), False, 'import pytest\n'), ((3270, 3331), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_external_force"""', '[False, True]'], {}), "('with_external_force', [False, True])\n", (3293, 3331), False, 'import pytest\n'), ((3333, 3387), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_contact"""', '[False, True]'], {}), "('with_contact', [False, True])\n", (3356, 3387), False, 'import pytest\n'), ((7017, 7056), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""cx"""', '[MX, SX]'], {}), "('cx', [MX, SX])\n", (7040, 7056), False, 'import pytest\n'), ((7058, 7119), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_external_force"""', '[False, True]'], {}), "('with_external_force', [False, True])\n", (7081, 7119), False, 'import pytest\n'), ((7121, 7175), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_contact"""', '[False, True]'], {}), "('with_contact', [False, True])\n", (7144, 7175), False, 'import pytest\n'), ((10095, 10134), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""cx"""', '[MX, SX]'], {}), "('cx', [MX, SX])\n", (10118, 10134), False, 'import pytest\n'), ((10136, 10197), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_external_force"""', '[False, True]'], {}), "('with_external_force', [False, True])\n", (10159, 10197), False, 'import pytest\n'), ((10199, 10253), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_contact"""', '[False, True]'], {}), "('with_contact', [False, True])\n", (10222, 10253), False, 'import pytest\n'), ((10255, 10308), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_torque"""', '[False, True]'], {}), "('with_torque', [False, True])\n", (10278, 10308), False, 'import pytest\n'), ((10310, 10368), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_excitations"""', '[False, True]'], {}), "('with_excitations', [False, True])\n", (10333, 10368), False, 'import pytest\n'), ((20157, 20211), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""with_contact"""', '[False, True]'], {}), "('with_contact', [False, True])\n", (20180, 20211), False, 'import pytest\n'), ((982, 1000), 'bioptim.optimization.non_linear_program.NonLinearProgram', 'NonLinearProgram', ([], {}), '()\n', (998, 1000), False, 'from bioptim.optimization.non_linear_program import NonLinearProgram\n'), ((1481, 1499), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (1495, 1499), True, 'import numpy as np\n'), ((1736, 1773), 'bioptim.dynamics.configure_problem.ConfigureProblem.initialize', 'ConfigureProblem.initialize', (['ocp', 'nlp'], {}), '(ocp, nlp)\n', (1763, 1773), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((1811, 1851), 'numpy.random.rand', 'np.random.rand', (['nlp.states.shape', 'nlp.ns'], {}), '(nlp.states.shape, nlp.ns)\n', (1825, 1851), True, 'import numpy as np\n'), ((1867, 1909), 'numpy.random.rand', 'np.random.rand', (['nlp.controls.shape', 'nlp.ns'], {}), '(nlp.controls.shape, nlp.ns)\n', (1881, 1909), True, 'import numpy as np\n'), ((1923, 1967), 'numpy.random.rand', 'np.random.rand', (['nlp.parameters.shape', 'nlp.ns'], {}), '(nlp.parameters.shape, nlp.ns)\n', (1937, 1967), True, 'import numpy as np\n'), ((3498, 3516), 'bioptim.optimization.non_linear_program.NonLinearProgram', 'NonLinearProgram', ([], {}), '()\n', (3514, 3516), False, 'from bioptim.optimization.non_linear_program import NonLinearProgram\n'), ((4022, 4040), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (4036, 4040), True, 'import numpy as np\n'), ((4277, 4314), 'bioptim.dynamics.configure_problem.ConfigureProblem.initialize', 'ConfigureProblem.initialize', (['ocp', 'nlp'], {}), '(ocp, nlp)\n', (4304, 4314), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((4352, 4392), 'numpy.random.rand', 'np.random.rand', (['nlp.states.shape', 'nlp.ns'], {}), '(nlp.states.shape, nlp.ns)\n', (4366, 4392), True, 'import numpy as np\n'), ((4408, 4450), 'numpy.random.rand', 'np.random.rand', (['nlp.controls.shape', 'nlp.ns'], {}), '(nlp.controls.shape, nlp.ns)\n', (4422, 4450), True, 'import numpy as np\n'), ((4464, 4508), 'numpy.random.rand', 'np.random.rand', (['nlp.parameters.shape', 'nlp.ns'], {}), '(nlp.parameters.shape, nlp.ns)\n', (4478, 4508), True, 'import numpy as np\n'), ((7286, 7304), 'bioptim.optimization.non_linear_program.NonLinearProgram', 'NonLinearProgram', ([], {}), '()\n', (7302, 7304), False, 'from bioptim.optimization.non_linear_program import NonLinearProgram\n'), ((7810, 7828), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (7824, 7828), True, 'import numpy as np\n'), ((8065, 8102), 'bioptim.dynamics.configure_problem.ConfigureProblem.initialize', 'ConfigureProblem.initialize', (['ocp', 'nlp'], {}), '(ocp, nlp)\n', (8092, 8102), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((8140, 8180), 'numpy.random.rand', 'np.random.rand', (['nlp.states.shape', 'nlp.ns'], {}), '(nlp.states.shape, nlp.ns)\n', (8154, 8180), True, 'import numpy as np\n'), ((8196, 8238), 'numpy.random.rand', 'np.random.rand', (['nlp.controls.shape', 'nlp.ns'], {}), '(nlp.controls.shape, nlp.ns)\n', (8210, 8238), True, 'import numpy as np\n'), ((8252, 8296), 'numpy.random.rand', 'np.random.rand', (['nlp.parameters.shape', 'nlp.ns'], {}), '(nlp.parameters.shape, nlp.ns)\n', (8266, 8296), True, 'import numpy as np\n'), ((10499, 10517), 'bioptim.optimization.non_linear_program.NonLinearProgram', 'NonLinearProgram', ([], {}), '()\n', (10515, 10517), False, 'from bioptim.optimization.non_linear_program import NonLinearProgram\n'), ((11182, 11200), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (11196, 11200), True, 'import numpy as np\n'), ((11437, 11474), 'bioptim.dynamics.configure_problem.ConfigureProblem.initialize', 'ConfigureProblem.initialize', (['ocp', 'nlp'], {}), '(ocp, nlp)\n', (11464, 11474), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((11512, 11552), 'numpy.random.rand', 'np.random.rand', (['nlp.states.shape', 'nlp.ns'], {}), '(nlp.states.shape, nlp.ns)\n', (11526, 11552), True, 'import numpy as np\n'), ((11568, 11610), 'numpy.random.rand', 'np.random.rand', (['nlp.controls.shape', 'nlp.ns'], {}), '(nlp.controls.shape, nlp.ns)\n', (11582, 11610), True, 'import numpy as np\n'), ((11624, 11668), 'numpy.random.rand', 'np.random.rand', (['nlp.parameters.shape', 'nlp.ns'], {}), '(nlp.parameters.shape, nlp.ns)\n', (11638, 11668), True, 'import numpy as np\n'), ((21255, 21273), 'bioptim.optimization.non_linear_program.NonLinearProgram', 'NonLinearProgram', ([], {}), '()\n', (21271, 21273), False, 'from bioptim.optimization.non_linear_program import NonLinearProgram\n'), ((21785, 21803), 'numpy.random.seed', 'np.random.seed', (['(42)'], {}), '(42)\n', (21799, 21803), True, 'import numpy as np\n'), ((21836, 21873), 'bioptim.dynamics.configure_problem.ConfigureProblem.initialize', 'ConfigureProblem.initialize', (['ocp', 'nlp'], {}), '(ocp, nlp)\n', (21863, 21873), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((21911, 21951), 'numpy.random.rand', 'np.random.rand', (['nlp.states.shape', 'nlp.ns'], {}), '(nlp.states.shape, nlp.ns)\n', (21925, 21951), True, 'import numpy as np\n'), ((21967, 22009), 'numpy.random.rand', 'np.random.rand', (['nlp.controls.shape', 'nlp.ns'], {}), '(nlp.controls.shape, nlp.ns)\n', (21981, 22009), True, 'import numpy as np\n'), ((22023, 22067), 'numpy.random.rand', 'np.random.rand', (['nlp.parameters.shape', 'nlp.ns'], {}), '(nlp.parameters.shape, nlp.ns)\n', (22037, 22067), True, 'import numpy as np\n'), ((696, 720), 'bioptim.optimization.optimization_vector.OptimizationVector', 'OptimizationVector', (['self'], {}), '(self)\n', (714, 720), False, 'from bioptim.optimization.optimization_vector import OptimizationVector\n'), ((1405, 1467), 'bioptim.dynamics.configure_problem.Dynamics', 'Dynamics', (['DynamicsFcn.TORQUE_DRIVEN'], {'with_contact': 'with_contact'}), '(DynamicsFcn.TORQUE_DRIVEN, with_contact=with_contact)\n', (1413, 1467), False, 'from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics\n'), ((3930, 4003), 'bioptim.dynamics.configure_problem.Dynamics', 'Dynamics', (['DynamicsFcn.TORQUE_DERIVATIVE_DRIVEN'], {'with_contact': 'with_contact'}), '(DynamicsFcn.TORQUE_DERIVATIVE_DRIVEN, with_contact=with_contact)\n', (3938, 4003), False, 'from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics\n'), ((7717, 7791), 'bioptim.dynamics.configure_problem.Dynamics', 'Dynamics', (['DynamicsFcn.TORQUE_ACTIVATIONS_DRIVEN'], {'with_contact': 'with_contact'}), '(DynamicsFcn.TORQUE_ACTIVATIONS_DRIVEN, with_contact=with_contact)\n', (7725, 7791), False, 'from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics\n'), ((10973, 11099), 'bioptim.dynamics.configure_problem.Dynamics', 'Dynamics', (['DynamicsFcn.MUSCLE_DRIVEN'], {'with_torque': 'with_torque', 'with_excitations': 'with_excitations', 'with_contact': 'with_contact'}), '(DynamicsFcn.MUSCLE_DRIVEN, with_torque=with_torque,\n with_excitations=with_excitations, with_contact=with_contact)\n', (10981, 11099), False, 'from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics\n'), ((20348, 20399), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.apply_parameters', 'DynamicsFunctions.apply_parameters', (['parameters', 'nlp'], {}), '(parameters, nlp)\n', (20382, 20399), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20412, 20458), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.get', 'DynamicsFunctions.get', (["nlp.states['q']", 'states'], {}), "(nlp.states['q'], states)\n", (20433, 20458), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20474, 20523), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.get', 'DynamicsFunctions.get', (["nlp.states['qdot']", 'states'], {}), "(nlp.states['qdot'], states)\n", (20495, 20523), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20538, 20590), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.get', 'DynamicsFunctions.get', (["nlp.controls['tau']", 'controls'], {}), "(nlp.controls['tau'], controls)\n", (20559, 20590), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20605, 20649), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.compute_qdot', 'DynamicsFunctions.compute_qdot', (['nlp', 'q', 'qdot'], {}), '(nlp, q, qdot)\n', (20635, 20649), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20664, 20731), 'bioptim.dynamics.dynamics_functions.DynamicsFunctions.forward_dynamics', 'DynamicsFunctions.forward_dynamics', (['nlp', 'q', 'qdot', 'tau', 'with_contact'], {}), '(nlp, q, qdot, tau, with_contact)\n', (20698, 20731), False, 'from bioptim.dynamics.dynamics_functions import DynamicsFunctions\n'), ((20813, 20859), 'bioptim.dynamics.configure_problem.ConfigureProblem.configure_q', 'ConfigureProblem.configure_q', (['nlp', '(True)', '(False)'], {}), '(nlp, True, False)\n', (20841, 20859), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((20868, 20917), 'bioptim.dynamics.configure_problem.ConfigureProblem.configure_qdot', 'ConfigureProblem.configure_qdot', (['nlp', '(True)', '(False)'], {}), '(nlp, True, False)\n', (20899, 20917), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((20926, 20974), 'bioptim.dynamics.configure_problem.ConfigureProblem.configure_tau', 'ConfigureProblem.configure_tau', (['nlp', '(False)', '(True)'], {}), '(nlp, False, True)\n', (20956, 20974), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((20983, 21084), 'bioptim.dynamics.configure_problem.ConfigureProblem.configure_dynamics_function', 'ConfigureProblem.configure_dynamics_function', (['ocp', 'nlp', 'custom_dynamic'], {'with_contact': 'with_contact'}), '(ocp, nlp, custom_dynamic,\n with_contact=with_contact)\n', (21027, 21084), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((21687, 21766), 'bioptim.dynamics.configure_problem.Dynamics', 'Dynamics', (['configure'], {'dynamic_function': 'custom_dynamic', 'with_contact': 'with_contact'}), '(configure, dynamic_function=custom_dynamic, with_contact=with_contact)\n', (21695, 21766), False, 'from bioptim.dynamics.configure_problem import DynamicsFcn, Dynamics\n'), ((22246, 22388), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6118529, 0.785176, 0.6075449, 0.8083973, -0.3214905, -0.1912131, \n 0.6507164, -0.2359716]'], {}), '(x_out[:, (0)], [0.6118529, 0.785176, \n 0.6075449, 0.8083973, -0.3214905, -0.1912131, 0.6507164, -0.2359716])\n', (22276, 22388), True, 'import numpy as np\n'), ((22412, 22504), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-2.444071, 128.8816865, 2.7245124]'], {}), '(contact_out[:, (0)], [-2.444071, 128.8816865,\n 2.7245124])\n', (22442, 22504), True, 'import numpy as np\n'), ((22518, 22675), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.30241366, -10.38503791,\n 1.60445173, 35.80238642]'], {}), '(x_out[:, (0)], [0.61185289, 0.78517596, \n 0.60754485, 0.80839735, -0.30241366, -10.38503791, 1.60445173, 35.80238642]\n )\n', (22548, 22675), True, 'import numpy as np\n'), ((1635, 1700), 'bioptim.interfaces.biorbd_interface.BiorbdInterface.convert_array_to_external_forces', 'BiorbdInterface.convert_array_to_external_forces', (['external_forces'], {}), '(external_forces)\n', (1683, 1700), False, 'from bioptim.interfaces.biorbd_interface import BiorbdInterface\n'), ((2182, 2327), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.8631034, 0.3251833, 0.1195942, 0.4937956, -7.7700092, -7.5782306, \n 21.7073786, -16.3059315]'], {}), '(x_out[:, (0)], [0.8631034, 0.3251833, \n 0.1195942, 0.4937956, -7.7700092, -7.5782306, 21.7073786, -16.3059315])\n', (2212, 2327), True, 'import numpy as np\n'), ((2380, 2477), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-47.8131136, 111.1726516, -24.4449121]'], {}), '(contact_out[:, (0)], [-47.8131136, \n 111.1726516, -24.4449121])\n', (2410, 2477), True, 'import numpy as np\n'), ((2497, 2639), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6118529, 0.785176, 0.6075449, 0.8083973, -0.3214905, -0.1912131, \n 0.6507164, -0.2359716]'], {}), '(x_out[:, (0)], [0.6118529, 0.785176, \n 0.6075449, 0.8083973, -0.3214905, -0.1912131, 0.6507164, -0.2359716])\n', (2527, 2639), True, 'import numpy as np\n'), ((2675, 2767), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-2.444071, 128.8816865, 2.7245124]'], {}), '(contact_out[:, (0)], [-2.444071, 128.8816865,\n 2.7245124])\n', (2705, 2767), True, 'import numpy as np\n'), ((2817, 2966), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.86310343, 0.32518332, 0.11959425, 0.4937956, 0.30731739, -9.97912778, \n 1.15263778, 36.02430956]'], {}), '(x_out[:, (0)], [0.86310343, 0.32518332, \n 0.11959425, 0.4937956, 0.30731739, -9.97912778, 1.15263778, 36.02430956])\n', (2847, 2966), True, 'import numpy as np\n'), ((3033, 3190), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.30241366, -10.38503791,\n 1.60445173, 35.80238642]'], {}), '(x_out[:, (0)], [0.61185289, 0.78517596, \n 0.60754485, 0.80839735, -0.30241366, -10.38503791, 1.60445173, 35.80238642]\n )\n', (3063, 3190), True, 'import numpy as np\n'), ((4176, 4241), 'bioptim.interfaces.biorbd_interface.BiorbdInterface.convert_array_to_external_forces', 'BiorbdInterface.convert_array_to_external_forces', (['external_forces'], {}), '(external_forces)\n', (4224, 4241), False, 'from bioptim.interfaces.biorbd_interface import BiorbdInterface\n'), ((4723, 4916), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.8631034, 0.3251833, 0.1195942, 0.4937956, -7.7700092, -7.5782306, \n 21.7073786, -16.3059315, 0.8074402, 0.4271078, 0.417411, 0.3232029]'], {}), '(x_out[:, (0)], [0.8631034, 0.3251833, \n 0.1195942, 0.4937956, -7.7700092, -7.5782306, 21.7073786, -16.3059315, \n 0.8074402, 0.4271078, 0.417411, 0.3232029])\n', (4753, 4916), True, 'import numpy as np\n'), ((5223, 5320), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-47.8131136, 111.1726516, -24.4449121]'], {}), '(contact_out[:, (0)], [-47.8131136, \n 111.1726516, -24.4449121])\n', (5253, 5320), True, 'import numpy as np\n'), ((5340, 5544), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.32149054, -0.19121314, \n 0.65071636, -0.23597164, 0.38867729, 0.54269608, 0.77224477, 0.72900717]'], {}), '(x_out[:, (0)], [0.61185289, 0.78517596, \n 0.60754485, 0.80839735, -0.32149054, -0.19121314, 0.65071636, -\n 0.23597164, 0.38867729, 0.54269608, 0.77224477, 0.72900717])\n', (5370, 5544), True, 'import numpy as np\n'), ((5851, 5943), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-2.444071, 128.8816865, 2.7245124]'], {}), '(contact_out[:, (0)], [-2.444071, 128.8816865,\n 2.7245124])\n', (5881, 5943), True, 'import numpy as np\n'), ((5993, 6192), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.86310343, 0.32518332, 0.11959425, 0.4937956, 0.30731739, -9.97912778, \n 1.15263778, 36.02430956, 0.80744016, 0.42710779, 0.417411, 0.32320293]'], {}), '(x_out[:, (0)], [0.86310343, 0.32518332, \n 0.11959425, 0.4937956, 0.30731739, -9.97912778, 1.15263778, 36.02430956,\n 0.80744016, 0.42710779, 0.417411, 0.32320293])\n', (6023, 6192), True, 'import numpy as np\n'), ((6514, 6719), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.61185289, 0.78517596, 0.60754485, 0.80839735, -0.30241366, -10.38503791,\n 1.60445173, 35.80238642, 0.38867729, 0.54269608, 0.77224477, 0.72900717]'], {}), '(x_out[:, (0)], [0.61185289, 0.78517596, \n 0.60754485, 0.80839735, -0.30241366, -10.38503791, 1.60445173, \n 35.80238642, 0.38867729, 0.54269608, 0.77224477, 0.72900717])\n', (6544, 6719), True, 'import numpy as np\n'), ((7964, 8029), 'bioptim.interfaces.biorbd_interface.BiorbdInterface.convert_array_to_external_forces', 'BiorbdInterface.convert_array_to_external_forces', (['external_forces'], {}), '(external_forces)\n', (8012, 8029), False, 'from bioptim.interfaces.biorbd_interface import BiorbdInterface\n'), ((8511, 8649), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.8631, 0.32518, 0.11959, 0.4938, 19.01887, 18.51503, -53.08574, 58.48719]'], {'decimal': '(5)'}), '(x_out[:, (0)], [0.8631, 0.32518, 0.11959, \n 0.4938, 19.01887, 18.51503, -53.08574, 58.48719], decimal=5)\n', (8541, 8649), True, 'import numpy as np\n'), ((8718, 8818), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[109.8086936, 3790.3932439, -3571.7858574]'], {}), '(contact_out[:, (0)], [109.8086936, \n 3790.3932439, -3571.7858574])\n', (8748, 8818), True, 'import numpy as np\n'), ((8838, 9004), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.61185289, 0.78517596, 0.60754485, 0.80839735, 0.78455384, -0.16844256, -\n 1.56184114, 1.97658587]'], {'decimal': '(5)'}), '(x_out[:, (0)], [0.61185289, 0.78517596, \n 0.60754485, 0.80839735, 0.78455384, -0.16844256, -1.56184114, \n 1.97658587], decimal=5)\n', (8868, 9004), True, 'import numpy as np\n'), ((9068, 9168), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['contact_out[:, (0)]', '[-7.88958997, 329.70828173, -263.55516549]'], {}), '(contact_out[:, (0)], [-7.88958997, \n 329.70828173, -263.55516549])\n', (9098, 9168), True, 'import numpy as np\n'), ((9217, 9386), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.863103426, 0.325183322, 0.119594246, 0.493795596, 17.3558072, -\n 46.9891264, 181.396922, 3611.70139]'], {'decimal': '(5)'}), '(x_out[:, (0)], [0.863103426, 0.325183322, \n 0.119594246, 0.493795596, 17.3558072, -46.9891264, 181.396922, \n 3611.70139], decimal=5)\n', (9247, 9386), True, 'import numpy as np\n'), ((9667, 9836), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.611852895, 0.785175961, 0.607544852, 0.808397348, -23.8262975, -\n 58.2033454, 127.43902, 3665.31163]'], {'decimal': '(5)'}), '(x_out[:, (0)], [0.611852895, 0.785175961, \n 0.607544852, 0.808397348, -23.8262975, -58.2033454, 127.43902, \n 3665.31163], decimal=5)\n', (9697, 9836), True, 'import numpy as np\n'), ((11336, 11401), 'bioptim.interfaces.biorbd_interface.BiorbdInterface.convert_array_to_external_forces', 'BiorbdInterface.convert_array_to_external_forces', (['external_forces'], {}), '(external_forces)\n', (11384, 11401), False, 'from bioptim.interfaces.biorbd_interface import BiorbdInterface\n'), ((21119, 21222), 'bioptim.dynamics.configure_problem.ConfigureProblem.configure_contact_function', 'ConfigureProblem.configure_contact_function', (['ocp', 'nlp', 'DynamicsFunctions.forces_from_torque_driven'], {}), '(ocp, nlp, DynamicsFunctions.\n forces_from_torque_driven)\n', (21162, 21222), False, 'from bioptim.dynamics.configure_problem import ConfigureProblem\n'), ((11956, 12165), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 1.07179622, -33.76217857, 36.21815923, \n 46.87928022, -1.80189035, 53.3914525, 48.30056919, 63.69373374, -\n 28.15700995]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 1.07179622, -33.76217857, 36.21815923, 46.87928022, -\n 1.80189035, 53.3914525, 48.30056919, 63.69373374, -28.15700995])\n', (11986, 12165), True, 'import numpy as np\n'), ((12630, 12849), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.29662878, 300.872062, -950.354903,\n 8.60630831, 3.19433638, 29.7405608, -20.2754226, -23.2467778, -41.9135012]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.29662878, 300.872062, -950.354903, 8.60630831, \n 3.19433638, 29.7405608, -20.2754226, -23.2467778, -41.9135012], decimal=6)\n', (12660, 12849), True, 'import numpy as np\n'), ((13411, 13551), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.615850098, 0.503136259, 0.642419278, -8.06478367, 242.279101, -772.114103]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.615850098, 0.503136259, \n 0.642419278, -8.06478367, 242.279101, -772.114103], decimal=6)\n', (13441, 13551), True, 'import numpy as np\n'), ((13700, 13839), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -3.80892207, 120.476051, -433.291346]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -3.80892207, 120.476051, -433.291346], decimal=6)\n', (13730, 13839), True, 'import numpy as np\n'), ((14055, 14264), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288, \n 55.65557816, 50.47052688, 0.36025589, 58.92377491, 29.70094194, -\n 15.13534937]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 0.91952705, -39.04876174, 45.31837288, 55.65557816, \n 50.47052688, 0.36025589, 58.92377491, 29.70094194, -15.13534937])\n', (14085, 14264), True, 'import numpy as np\n'), ((14729, 14946), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.7271235, 310.86617, -982.725656, \n -7.7222893, -11.3759732, 95.1906209, 4.45077128, -5.20261014, -28.0864106]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.7271235, 310.86617, -982.725656, -7.7222893, -\n 11.3759732, 95.1906209, 4.45077128, -5.20261014, -28.0864106], decimal=6)\n', (14759, 14946), True, 'import numpy as np\n'), ((15511, 15637), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 0.91952705, -39.04876174, 45.31837288])\n', (15541, 15637), True, 'import numpy as np\n'), ((15744, 15881), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.7271235, 310.86617, -982.725656]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.7271235, 310.86617, -982.725656], decimal=6)\n', (15774, 15881), True, 'import numpy as np\n'), ((16119, 16328), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 1.07179622, -33.76217857, 36.21815923, \n 46.87928022, -1.80189035, 53.3914525, 48.30056919, 63.69373374, -\n 28.15700995]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 1.07179622, -33.76217857, 36.21815923, 46.87928022, -\n 1.80189035, 53.3914525, 48.30056919, 63.69373374, -28.15700995])\n', (16149, 16328), True, 'import numpy as np\n'), ((16793, 17012), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.29662878, 300.872062, -950.354903,\n 8.60630831, 3.19433638, 29.7405608, -20.2754226, -23.2467778, -41.9135012]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.29662878, 300.872062, -950.354903, 8.60630831, \n 3.19433638, 29.7405608, -20.2754226, -23.2467778, -41.9135012], decimal=6)\n', (16823, 17012), True, 'import numpy as np\n'), ((17574, 17714), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.615850098, 0.503136259, 0.642419278, -8.06478367, 242.279101, -772.114103]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.615850098, 0.503136259, \n 0.642419278, -8.06478367, 242.279101, -772.114103], decimal=6)\n', (17604, 17714), True, 'import numpy as np\n'), ((17863, 18002), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -3.80892207, 120.476051, -433.291346]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -3.80892207, 120.476051, -433.291346], decimal=6)\n', (17893, 18002), True, 'import numpy as np\n'), ((18218, 18427), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288, \n 55.65557816, 50.47052688, 0.36025589, 58.92377491, 29.70094194, -\n 15.13534937]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 0.91952705, -39.04876174, 45.31837288, 55.65557816, \n 50.47052688, 0.36025589, 58.92377491, 29.70094194, -15.13534937])\n', (18248, 18427), True, 'import numpy as np\n'), ((18892, 19109), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.7271235, 310.86617, -982.725656, \n -7.7222893, -11.3759732, 95.1906209, 4.45077128, -5.20261014, -28.0864106]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.7271235, 310.86617, -982.725656, -7.7222893, -\n 11.3759732, 95.1906209, 4.45077128, -5.20261014, -28.0864106], decimal=6)\n', (18922, 19109), True, 'import numpy as np\n'), ((19674, 19800), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.6158501, 0.50313626, 0.64241928, 0.91952705, -39.04876174, 45.31837288]'], {}), '(x_out[:, (0)], [0.6158501, 0.50313626, \n 0.64241928, 0.91952705, -39.04876174, 45.31837288])\n', (19704, 19800), True, 'import numpy as np\n'), ((19907, 20044), 'numpy.testing.assert_almost_equal', 'np.testing.assert_almost_equal', (['x_out[:, (0)]', '[0.18340451, 0.611852895, 0.785175961, -9.7271235, 310.86617, -982.725656]'], {'decimal': '(6)'}), '(x_out[:, (0)], [0.18340451, 0.611852895, \n 0.785175961, -9.7271235, 310.86617, -982.725656], decimal=6)\n', (19937, 20044), True, 'import numpy as np\n')] |
elyase/polyaxon | polyaxon/event_manager/event_manager.py | 1c19f059a010a6889e2b7ea340715b2bcfa382a0 | from hestia.manager_interface import ManagerInterface
from event_manager import event_actions
class EventManager(ManagerInterface):
def _get_state_data(self, event): # pylint:disable=arguments-differ
return event.event_type, event
def subscribe(self, event): # pylint:disable=arguments-differ
"""
>>> subscribe(SomeEvent)
"""
super().subscribe(obj=event)
def knows(self, event_type): # pylint:disable=arguments-differ
return super().knows(key=event_type)
def get(self, event_type): # pylint:disable=arguments-differ
return super().get(key=event_type)
def user_write_events(self):
"""Return event types where use acted on an object.
The write events are events with actions:
* CREATED
* UPDATED
* DELETED
* RESUMED
* COPIED
* CLONED
* STOPPED
"""
return [event_type for event_type, event in self.items if event.get_event_action()
in event_actions.WRITE_ACTIONS]
def user_view_events(self):
"""Return event types where use viewed a main object."""
return [event_type for event_type, event in self.items if event.get_event_action()
== event_actions.VIEWED]
| [] |
KotoLLC/peacenik-tests | test_f_login_andy.py | 760f7799ab2b9312fe0cce373890195151c48fce | from helpers import *
def test_f_login_andy():
url = "http://central.orbits.local/rpc.AuthService/Login"
raw_payload = {"name": "andy","password": "12345"}
payload = json.dumps(raw_payload)
headers = {'Content-Type': 'application/json'}
# convert dict to json by json.dumps() for body data.
response = requests.request("POST", url, headers=headers, data=payload)
save_cookies(response.cookies,"cookies.txt")
# Validate response headers and body contents, e.g. status code.
assert response.status_code == 200
# print full request and response
pretty_print_request(response.request)
pretty_print_response(response) | [] |
ian-r-rose/visualization | docker/src/clawpack-5.3.1/riemann/src/shallow_1D_py.py | ed6d9fab95eb125e7340ab3fad3ed114ed3214af | #!/usr/bin/env python
# encoding: utf-8
r"""
Riemann solvers for the shallow water equations.
The available solvers are:
* Roe - Use Roe averages to caluclate the solution to the Riemann problem
* HLL - Use a HLL solver
* Exact - Use a newton iteration to calculate the exact solution to the
Riemann problem
.. math::
q_t + f(q)_x = 0
where
.. math::
q(x,t) = \left [ \begin{array}{c} h \\ h u \end{array} \right ],
the flux function is
.. math::
f(q) = \left [ \begin{array}{c} h u \\ hu^2 + 1/2 g h^2 \end{array}\right ].
and :math:`h` is the water column height, :math:`u` the velocity and :math:`g`
is the gravitational acceleration.
:Authors:
Kyle T. Mandli (2009-02-05): Initial version
"""
# ============================================================================
# Copyright (C) 2009 Kyle T. Mandli <mandli@amath.washington.edu>
#
# Distributed under the terms of the Berkeley Software Distribution (BSD)
# license
# http://www.opensource.org/licenses/
# ============================================================================
import numpy as np
num_eqn = 2
num_waves = 2
def shallow_roe_1D(q_l,q_r,aux_l,aux_r,problem_data):
r"""
Roe shallow water solver in 1d::
ubar = (sqrt(u_l) + sqrt(u_r)) / (sqrt(h_l) + sqrt(h_r))
cbar = sqrt( 0.5 * g * (h_l + h_r))
W_1 = | 1 | s_1 = ubar - cbar
| ubar - cbar |
W_2 = | 1 | s_1 = ubar + cbar
| ubar + cbar |
a1 = 0.5 * ( - delta_hu + (ubar + cbar) * delta_h ) / cbar
a2 = 0.5 * ( delta_hu - (ubar - cbar) * delta_h ) / cbar
*problem_data* should contain:
- *g* - (float) Gravitational constant
- *efix* - (bool) Boolean as to whether a entropy fix should be used, if
not present, false is assumed
:Version: 1.0 (2009-02-05)
"""
# Array shapes
num_rp = q_l.shape[1]
# Output arrays
wave = np.empty( (num_eqn, num_waves, num_rp) )
s = np.zeros( (num_waves, num_rp) )
amdq = np.zeros( (num_eqn, num_rp) )
apdq = np.zeros( (num_eqn, num_rp) )
# Compute roe-averaged quantities
ubar = ( (q_l[1,:]/np.sqrt(q_l[0,:]) + q_r[1,:]/np.sqrt(q_r[0,:])) /
(np.sqrt(q_l[0,:]) + np.sqrt(q_r[0,:])) )
cbar = np.sqrt(0.5 * problem_data['grav'] * (q_l[0,:] + q_r[0,:]))
# Compute Flux structure
delta = q_r - q_l
a1 = 0.5 * (-delta[1,:] + (ubar + cbar) * delta[0,:]) / cbar
a2 = 0.5 * ( delta[1,:] - (ubar - cbar) * delta[0,:]) / cbar
# Compute each family of waves
wave[0,0,:] = a1
wave[1,0,:] = a1 * (ubar - cbar)
s[0,:] = ubar - cbar
wave[0,1,:] = a2
wave[1,1,:] = a2 * (ubar + cbar)
s[1,:] = ubar + cbar
if problem_data['efix']:
raise NotImplementedError("Entropy fix has not been implemented.")
else:
s_index = np.zeros((2,num_rp))
for m in xrange(num_eqn):
for mw in xrange(num_waves):
s_index[0,:] = s[mw,:]
amdq[m,:] += np.min(s_index,axis=0) * wave[m,mw,:]
apdq[m,:] += np.max(s_index,axis=0) * wave[m,mw,:]
return wave, s, amdq, apdq
def shallow_hll_1D(q_l,q_r,aux_l,aux_r,problem_data):
r"""
HLL shallow water solver ::
W_1 = Q_hat - Q_l s_1 = min(u_l-c_l,u_l+c_l,lambda_roe_1,lambda_roe_2)
W_2 = Q_r - Q_hat s_2 = max(u_r-c_r,u_r+c_r,lambda_roe_1,lambda_roe_2)
Q_hat = ( f(q_r) - f(q_l) - s_2 * q_r + s_1 * q_l ) / (s_1 - s_2)
*problem_data* should contain:
- *g* - (float) Gravitational constant
:Version: 1.0 (2009-02-05)
"""
# Array shapes
num_rp = q_l.shape[1]
num_eqn = 2
num_waves = 2
# Output arrays
wave = np.empty( (num_eqn, num_waves, num_rp) )
s = np.empty( (num_waves, num_rp) )
amdq = np.zeros( (num_eqn, num_rp) )
apdq = np.zeros( (num_eqn, num_rp) )
# Compute Roe and right and left speeds
ubar = ( (q_l[1,:]/np.sqrt(q_l[0,:]) + q_r[1,:]/np.sqrt(q_r[0,:])) /
(np.sqrt(q_l[0,:]) + np.sqrt(q_r[0,:])) )
cbar = np.sqrt(0.5 * problem_data['grav'] * (q_l[0,:] + q_r[0,:]))
u_r = q_r[1,:] / q_r[0,:]
c_r = np.sqrt(problem_data['grav'] * q_r[0,:])
u_l = q_l[1,:] / q_l[0,:]
c_l = np.sqrt(problem_data['grav'] * q_l[0,:])
# Compute Einfeldt speeds
s_index = np.empty((4,num_rp))
s_index[0,:] = ubar+cbar
s_index[1,:] = ubar-cbar
s_index[2,:] = u_l + c_l
s_index[3,:] = u_l - c_l
s[0,:] = np.min(s_index,axis=0)
s_index[2,:] = u_r + c_r
s_index[3,:] = u_r - c_r
s[1,:] = np.max(s_index,axis=0)
# Compute middle state
q_hat = np.empty((2,num_rp))
q_hat[0,:] = ((q_r[1,:] - q_l[1,:] - s[1,:] * q_r[0,:]
+ s[0,:] * q_l[0,:]) / (s[0,:] - s[1,:]))
q_hat[1,:] = ((q_r[1,:]**2/q_r[0,:] + 0.5 * problem_data['grav'] * q_r[0,:]**2
- (q_l[1,:]**2/q_l[0,:] + 0.5 * problem_data['grav'] * q_l[0,:]**2)
- s[1,:] * q_r[1,:] + s[0,:] * q_l[1,:]) / (s[0,:] - s[1,:]))
# Compute each family of waves
wave[:,0,:] = q_hat - q_l
wave[:,1,:] = q_r - q_hat
# Compute variations
s_index = np.zeros((2,num_rp))
for m in xrange(num_eqn):
for mw in xrange(num_waves):
s_index[0,:] = s[mw,:]
amdq[m,:] += np.min(s_index,axis=0) * wave[m,mw,:]
apdq[m,:] += np.max(s_index,axis=0) * wave[m,mw,:]
return wave, s, amdq, apdq
def shallow_fwave_1d(q_l, q_r, aux_l, aux_r, problem_data):
r"""Shallow water Riemann solver using fwaves
Also includes support for bathymetry but be wary if you think you might have
dry states as this has not been tested.
*problem_data* should contain:
- *grav* - (float) Gravitational constant
- *sea_level* - (float) Datum from which the dry-state is calculated.
:Version: 1.0 (2014-09-05)
"""
g = problem_data['grav']
num_rp = q_l.shape[1]
num_eqn = 2
num_waves = 2
# Output arrays
fwave = np.empty( (num_eqn, num_waves, num_rp) )
s = np.empty( (num_waves, num_rp) )
amdq = np.zeros( (num_eqn, num_rp) )
apdq = np.zeros( (num_eqn, num_rp) )
# Extract state
u_l = np.where(q_l[0,:] - problem_data['sea_level'] > 1e-3,
q_l[1,:] / q_l[0,:], 0.0)
u_r = np.where(q_r[0,:] - problem_data['sea_level'] > 1e-3,
q_r[1,:] / q_r[0,:], 0.0)
phi_l = q_l[0,:] * u_l**2 + 0.5 * g * q_l[0,:]**2
phi_r = q_r[0,:] * u_r**2 + 0.5 * g * q_r[0,:]**2
# Speeds
s[0,:] = u_l - np.sqrt(g * q_l[0,:])
s[1,:] = u_r + np.sqrt(g * q_r[0,:])
delta1 = q_r[1,:] - q_l[1,:]
delta2 = phi_r - phi_l + g * 0.5 * (q_r[0,:] + q_l[0,:]) * (aux_r[0,:] - aux_l[0,:])
beta1 = (s[1,:] * delta1 - delta2) / (s[1,:] - s[0,:])
beta2 = (delta2 - s[0,:] * delta1) / (s[1,:] - s[0,:])
fwave[0,0,:] = beta1
fwave[1,0,:] = beta1 * s[0,:]
fwave[0,1,:] = beta2
fwave[1,1,:] = beta2 * s[1,:]
for m in xrange(num_eqn):
for mw in xrange(num_waves):
amdq[m,:] += (s[mw,:] < 0.0) * fwave[m,mw,:]
apdq[m,:] += (s[mw,:] >= 0.0) * fwave[m,mw,:]
return fwave, s, amdq, apdq
def shallow_exact_1D(q_l,q_r,aux_l,aux_r,problem_data):
r"""
Exact shallow water Riemann solver
.. warning::
This solver has not been implemented.
"""
raise NotImplementedError("The exact swe solver has not been implemented.")
| [((2045, 2083), 'numpy.empty', 'np.empty', (['(num_eqn, num_waves, num_rp)'], {}), '((num_eqn, num_waves, num_rp))\n', (2053, 2083), True, 'import numpy as np\n'), ((2094, 2123), 'numpy.zeros', 'np.zeros', (['(num_waves, num_rp)'], {}), '((num_waves, num_rp))\n', (2102, 2123), True, 'import numpy as np\n'), ((2137, 2164), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (2145, 2164), True, 'import numpy as np\n'), ((2178, 2205), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (2186, 2205), True, 'import numpy as np\n'), ((2381, 2446), 'numpy.sqrt', 'np.sqrt', (["(0.5 * problem_data['grav'] * (q_l[(0), :] + q_r[(0), :]))"], {}), "(0.5 * problem_data['grav'] * (q_l[(0), :] + q_r[(0), :]))\n", (2388, 2446), True, 'import numpy as np\n'), ((3913, 3951), 'numpy.empty', 'np.empty', (['(num_eqn, num_waves, num_rp)'], {}), '((num_eqn, num_waves, num_rp))\n', (3921, 3951), True, 'import numpy as np\n'), ((3962, 3991), 'numpy.empty', 'np.empty', (['(num_waves, num_rp)'], {}), '((num_waves, num_rp))\n', (3970, 3991), True, 'import numpy as np\n'), ((4005, 4032), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (4013, 4032), True, 'import numpy as np\n'), ((4046, 4073), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (4054, 4073), True, 'import numpy as np\n'), ((4255, 4320), 'numpy.sqrt', 'np.sqrt', (["(0.5 * problem_data['grav'] * (q_l[(0), :] + q_r[(0), :]))"], {}), "(0.5 * problem_data['grav'] * (q_l[(0), :] + q_r[(0), :]))\n", (4262, 4320), True, 'import numpy as np\n'), ((4355, 4398), 'numpy.sqrt', 'np.sqrt', (["(problem_data['grav'] * q_r[(0), :])"], {}), "(problem_data['grav'] * q_r[(0), :])\n", (4362, 4398), True, 'import numpy as np\n'), ((4436, 4479), 'numpy.sqrt', 'np.sqrt', (["(problem_data['grav'] * q_l[(0), :])"], {}), "(problem_data['grav'] * q_l[(0), :])\n", (4443, 4479), True, 'import numpy as np\n'), ((4522, 4543), 'numpy.empty', 'np.empty', (['(4, num_rp)'], {}), '((4, num_rp))\n', (4530, 4543), True, 'import numpy as np\n'), ((4672, 4695), 'numpy.min', 'np.min', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (4678, 4695), True, 'import numpy as np\n'), ((4766, 4789), 'numpy.max', 'np.max', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (4772, 4789), True, 'import numpy as np\n'), ((4829, 4850), 'numpy.empty', 'np.empty', (['(2, num_rp)'], {}), '((2, num_rp))\n', (4837, 4850), True, 'import numpy as np\n'), ((5365, 5386), 'numpy.zeros', 'np.zeros', (['(2, num_rp)'], {}), '((2, num_rp))\n', (5373, 5386), True, 'import numpy as np\n'), ((6240, 6278), 'numpy.empty', 'np.empty', (['(num_eqn, num_waves, num_rp)'], {}), '((num_eqn, num_waves, num_rp))\n', (6248, 6278), True, 'import numpy as np\n'), ((6289, 6318), 'numpy.empty', 'np.empty', (['(num_waves, num_rp)'], {}), '((num_waves, num_rp))\n', (6297, 6318), True, 'import numpy as np\n'), ((6332, 6359), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (6340, 6359), True, 'import numpy as np\n'), ((6373, 6400), 'numpy.zeros', 'np.zeros', (['(num_eqn, num_rp)'], {}), '((num_eqn, num_rp))\n', (6381, 6400), True, 'import numpy as np\n'), ((6434, 6528), 'numpy.where', 'np.where', (["(q_l[(0), :] - problem_data['sea_level'] > 0.001)", '(q_l[(1), :] / q_l[(0), :])', '(0.0)'], {}), "(q_l[(0), :] - problem_data['sea_level'] > 0.001, q_l[(1), :] / q_l\n [(0), :], 0.0)\n", (6442, 6528), True, 'import numpy as np\n'), ((6544, 6638), 'numpy.where', 'np.where', (["(q_r[(0), :] - problem_data['sea_level'] > 0.001)", '(q_r[(1), :] / q_r[(0), :])', '(0.0)'], {}), "(q_r[(0), :] - problem_data['sea_level'] > 0.001, q_r[(1), :] / q_r\n [(0), :], 0.0)\n", (6552, 6638), True, 'import numpy as np\n'), ((2991, 3012), 'numpy.zeros', 'np.zeros', (['(2, num_rp)'], {}), '((2, num_rp))\n', (2999, 3012), True, 'import numpy as np\n'), ((6785, 6809), 'numpy.sqrt', 'np.sqrt', (['(g * q_l[(0), :])'], {}), '(g * q_l[(0), :])\n', (6792, 6809), True, 'import numpy as np\n'), ((6826, 6850), 'numpy.sqrt', 'np.sqrt', (['(g * q_r[(0), :])'], {}), '(g * q_r[(0), :])\n', (6833, 6850), True, 'import numpy as np\n'), ((2329, 2349), 'numpy.sqrt', 'np.sqrt', (['q_l[(0), :]'], {}), '(q_l[(0), :])\n', (2336, 2349), True, 'import numpy as np\n'), ((2349, 2369), 'numpy.sqrt', 'np.sqrt', (['q_r[(0), :]'], {}), '(q_r[(0), :])\n', (2356, 2369), True, 'import numpy as np\n'), ((4203, 4223), 'numpy.sqrt', 'np.sqrt', (['q_l[(0), :]'], {}), '(q_l[(0), :])\n', (4210, 4223), True, 'import numpy as np\n'), ((4223, 4243), 'numpy.sqrt', 'np.sqrt', (['q_r[(0), :]'], {}), '(q_r[(0), :])\n', (4230, 4243), True, 'import numpy as np\n'), ((2270, 2290), 'numpy.sqrt', 'np.sqrt', (['q_l[(0), :]'], {}), '(q_l[(0), :])\n', (2277, 2290), True, 'import numpy as np\n'), ((2299, 2319), 'numpy.sqrt', 'np.sqrt', (['q_r[(0), :]'], {}), '(q_r[(0), :])\n', (2306, 2319), True, 'import numpy as np\n'), ((4144, 4164), 'numpy.sqrt', 'np.sqrt', (['q_l[(0), :]'], {}), '(q_l[(0), :])\n', (4151, 4164), True, 'import numpy as np\n'), ((4173, 4193), 'numpy.sqrt', 'np.sqrt', (['q_r[(0), :]'], {}), '(q_r[(0), :])\n', (4180, 4193), True, 'import numpy as np\n'), ((5513, 5536), 'numpy.min', 'np.min', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (5519, 5536), True, 'import numpy as np\n'), ((5576, 5599), 'numpy.max', 'np.max', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (5582, 5599), True, 'import numpy as np\n'), ((3155, 3178), 'numpy.min', 'np.min', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (3161, 3178), True, 'import numpy as np\n'), ((3222, 3245), 'numpy.max', 'np.max', (['s_index'], {'axis': '(0)'}), '(s_index, axis=0)\n', (3228, 3245), True, 'import numpy as np\n')] |
juanfra684/Nuitka | nuitka/Constants.py | 0e276895fadabefb598232f2ccf8cc7736c9a85b | # Copyright 2020, Kay Hayen, mailto:kay.hayen@gmail.com
#
# Part of "Nuitka", an optimizing Python compiler that is compatible and
# integrates with CPython, but also works on its own.
#
# 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.
#
""" Module for constants in Nuitka.
This contains tools to compare, classify and test constants.
"""
import math
from types import BuiltinFunctionType
from nuitka.Builtins import builtin_type_names
from nuitka.PythonVersions import python_version
from .__past__ import ( # pylint: disable=I0021,redefined-builtin
iterItems,
long,
unicode,
xrange,
)
from .Builtins import (
builtin_anon_names,
builtin_anon_value_list,
builtin_exception_values_list,
builtin_named_values_list,
)
NoneType = type(None)
def compareConstants(a, b):
# Many many cases to deal with, pylint: disable=too-many-branches,too-many-return-statements
# Supposed fast path for comparison.
if type(a) is not type(b):
return False
# Now it's either not the same, or it is a container that contains NaN or it
# is a complex or float that is NaN, the other cases can use == at the end.
if type(a) is complex:
return compareConstants(a.imag, b.imag) and compareConstants(a.real, b.real)
if type(a) is float:
# Check sign first, -0.0 is not 0.0, or -nan is not nan, it has a
# different sign for a start.
if math.copysign(1.0, a) != math.copysign(1.0, b):
return False
if math.isnan(a) and math.isnan(b):
return True
return a == b
if type(a) in (tuple, list):
if len(a) != len(b):
return False
for ea, eb in zip(a, b):
if not compareConstants(ea, eb):
return False
return True
if type(a) is dict:
if len(a) != len(b):
return False
for ea1, ea2 in iterItems(a):
for eb1, eb2 in iterItems(b):
if compareConstants(ea1, eb1) and compareConstants(ea2, eb2):
break
else:
return False
return True
if type(a) in (frozenset, set):
if len(a) != len(b):
return False
for ea in a:
if ea not in b:
# Due to NaN values, we need to compare each set element with
# all the other set to be really sure.
for eb in b:
if compareConstants(ea, eb):
break
else:
return False
return True
if type(a) is xrange:
return str(a) == str(b)
# The NaN values of float and complex may let this fail, even if the
# constants are built in the same way, therefore above checks.
return a == b
# These built-in type references are kind of constant too. The list should be
# complete.
constant_builtin_types = (
int,
str,
float,
list,
tuple,
set,
dict,
slice,
complex,
xrange,
NoneType,
)
if python_version >= 300:
constant_builtin_types += (bytes,)
else:
constant_builtin_types += (
unicode,
long,
# This has no name in Python, but the natural one in C-API.
builtin_anon_names["instance"],
)
def isConstant(constant):
# Too many cases and all return, that is how we do it here,
# pylint: disable=too-many-branches,too-many-return-statements
constant_type = type(constant)
if constant_type is dict:
for key, value in iterItems(constant):
if not isConstant(key):
return False
if not isConstant(value):
return False
return True
elif constant_type in (tuple, list):
for element_value in constant:
if not isConstant(element_value):
return False
return True
elif constant_type is slice:
if (
not isConstant(constant.start)
or not isConstant(constant.stop)
or not isConstant(constant.step)
):
return False
return True
elif constant_type in (
str,
unicode,
complex,
int,
long,
bool,
float,
NoneType,
range,
bytes,
set,
frozenset,
xrange,
bytearray,
):
return True
elif constant in (Ellipsis, NoneType, NotImplemented):
return True
elif constant in builtin_anon_value_list:
return True
elif constant_type is type:
# Maybe pre-build this as a set for quicker testing.
return (
constant.__name__ in builtin_type_names
or constant in builtin_exception_values_list
)
elif constant_type is BuiltinFunctionType and constant in builtin_named_values_list:
# TODO: Some others could also be usable and even interesting, but
# then probably should go into other node types, e.g. str.join is
# a candidate.
return True
else:
return False
def isMutable(constant):
""" Is a constant mutable
That means a user of a reference to it, can modify it. Strings are
a prime example of immutable, dictionaries are mutable.
"""
# Many cases and all return, that is how we do it here,
# pylint: disable=too-many-return-statements
constant_type = type(constant)
if constant_type in (
str,
unicode,
complex,
int,
long,
bool,
float,
NoneType,
range,
bytes,
slice,
xrange,
type,
BuiltinFunctionType,
):
return False
elif constant_type in (dict, list, set, bytearray):
return True
elif constant_type is tuple:
for value in constant:
if isMutable(value):
return True
return False
elif constant_type is frozenset:
for value in constant:
if isMutable(value):
return True
return False
elif constant is Ellipsis:
return False
elif constant is NotImplemented:
return False
else:
assert False, repr(constant)
def isHashable(constant):
""" Is a constant hashable
That means a user of a reference to it, can use it for dicts and set
keys. This is distinct from mutable, there is one types that is not
mutable, and still not hashable: slices.
"""
# Many cases and all return, that is how we do it here,
# pylint: disable=too-many-return-statements
constant_type = type(constant)
if constant_type in (
str,
unicode,
complex,
int,
long,
bool,
float,
NoneType,
xrange,
bytes,
type,
BuiltinFunctionType,
):
return True
elif constant_type in (dict, list, set, slice, bytearray):
return False
elif constant_type is tuple:
for value in constant:
if not isHashable(value):
return False
return True
elif constant_type is frozenset:
for value in constant:
if not isHashable(value):
return False
return True
elif constant is Ellipsis:
return True
else:
assert False, constant_type
def getUnhashableConstant(constant):
# Too many cases and all return, that is how we do it here,
# pylint: disable=too-many-return-statements
constant_type = type(constant)
if constant_type in (
str,
unicode,
complex,
int,
long,
bool,
float,
NoneType,
xrange,
bytes,
type,
BuiltinFunctionType,
):
return None
elif constant_type in (dict, list, set):
return constant
elif constant_type is tuple:
for value in constant:
res = getUnhashableConstant(value)
if res is not None:
return res
return None
elif constant is Ellipsis:
return None
elif constant in constant_builtin_types:
return None
elif constant_type is slice:
return None
else:
assert False, constant_type
def isIterableConstant(constant):
return type(constant) in (
str,
unicode,
list,
tuple,
set,
frozenset,
dict,
xrange,
bytes,
bytearray,
)
def getConstantIterationLength(constant):
assert isIterableConstant(constant)
return len(constant)
def isNumberConstant(constant):
return type(constant) in (int, long, float, bool)
def isIndexConstant(constant):
return type(constant) in (int, long, bool)
def createConstantDict(keys, values):
# Create it proper size immediately.
constant_value = dict.fromkeys(keys, None)
for key, value in zip(keys, values):
constant_value[key] = value
return constant_value
def getConstantWeight(constant):
constant_type = type(constant)
if constant_type is dict:
result = 0
for key, value in iterItems(constant):
result += getConstantWeight(key)
result += getConstantWeight(value)
return result
elif constant_type in (tuple, list, set, frozenset):
result = 0
for element_value in constant:
result += getConstantWeight(element_value)
return result
else:
return 1
def isCompileTimeConstantValue(value):
""" Determine if a value will be usable at compile time.
"""
# This needs to match code in makeCompileTimeConstantReplacementNode
if isConstant(value):
return True
elif type(value) is type:
return True
else:
return False
| [((1962, 1983), 'math.copysign', 'math.copysign', (['(1.0)', 'a'], {}), '(1.0, a)\n', (1975, 1983), False, 'import math\n'), ((1987, 2008), 'math.copysign', 'math.copysign', (['(1.0)', 'b'], {}), '(1.0, b)\n', (2000, 2008), False, 'import math\n'), ((2047, 2060), 'math.isnan', 'math.isnan', (['a'], {}), '(a)\n', (2057, 2060), False, 'import math\n'), ((2065, 2078), 'math.isnan', 'math.isnan', (['b'], {}), '(b)\n', (2075, 2078), False, 'import math\n')] |
chriscoombs/aws-comparing-algorithms-performance-mlops-cdk | functions/predictionLambda/botocore/endpoint.py | 6d3888f3ecd667ee76dc473edba37a608786ed2e | # Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
# Copyright 2012-2014 Amazon.com, Inc. or its affiliates. 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. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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 os
import logging
import time
import threading
from botocore.vendored import six
from botocore.awsrequest import create_request_object
from botocore.exceptions import HTTPClientError
from botocore.httpsession import URLLib3Session
from botocore.utils import is_valid_endpoint_url, get_environ_proxies
from botocore.hooks import first_non_none_response
from botocore.history import get_global_history_recorder
from botocore.response import StreamingBody
from botocore import parsers
logger = logging.getLogger(__name__)
history_recorder = get_global_history_recorder()
DEFAULT_TIMEOUT = 60
MAX_POOL_CONNECTIONS = 10
def convert_to_response_dict(http_response, operation_model):
"""Convert an HTTP response object to a request dict.
This converts the requests library's HTTP response object to
a dictionary.
:type http_response: botocore.vendored.requests.model.Response
:param http_response: The HTTP response from an AWS service request.
:rtype: dict
:return: A response dictionary which will contain the following keys:
* headers (dict)
* status_code (int)
* body (string or file-like object)
"""
response_dict = {
'headers': http_response.headers,
'status_code': http_response.status_code,
'context': {
'operation_name': operation_model.name,
}
}
if response_dict['status_code'] >= 300:
response_dict['body'] = http_response.content
elif operation_model.has_event_stream_output:
response_dict['body'] = http_response.raw
elif operation_model.has_streaming_output:
length = response_dict['headers'].get('content-length')
response_dict['body'] = StreamingBody(http_response.raw, length)
else:
response_dict['body'] = http_response.content
return response_dict
class Endpoint(object):
"""
Represents an endpoint for a particular service in a specific
region. Only an endpoint can make requests.
:ivar service: The Service object that describes this endpoints
service.
:ivar host: The fully qualified endpoint hostname.
:ivar session: The session object.
"""
def __init__(self, host, endpoint_prefix, event_emitter,
response_parser_factory=None, http_session=None):
self._endpoint_prefix = endpoint_prefix
self._event_emitter = event_emitter
self.host = host
self._lock = threading.Lock()
if response_parser_factory is None:
response_parser_factory = parsers.ResponseParserFactory()
self._response_parser_factory = response_parser_factory
self.http_session = http_session
if self.http_session is None:
self.http_session = URLLib3Session()
def __repr__(self):
return '%s(%s)' % (self._endpoint_prefix, self.host)
def make_request(self, operation_model, request_dict):
logger.debug("Making request for %s with params: %s",
operation_model, request_dict)
return self._send_request(request_dict, operation_model)
def create_request(self, params, operation_model=None):
request = create_request_object(params)
if operation_model:
request.stream_output = any([
operation_model.has_streaming_output,
operation_model.has_event_stream_output
])
service_id = operation_model.service_model.service_id.hyphenize()
event_name = 'request-created.{service_id}.{op_name}'.format(
service_id=service_id,
op_name=operation_model.name)
self._event_emitter.emit(event_name, request=request,
operation_name=operation_model.name)
prepared_request = self.prepare_request(request)
return prepared_request
def _encode_headers(self, headers):
# In place encoding of headers to utf-8 if they are unicode.
for key, value in headers.items():
if isinstance(value, six.text_type):
headers[key] = value.encode('utf-8')
def prepare_request(self, request):
self._encode_headers(request.headers)
return request.prepare()
def _send_request(self, request_dict, operation_model):
attempts = 1
request = self.create_request(request_dict, operation_model)
context = request_dict['context']
success_response, exception = self._get_response(
request, operation_model, context)
while self._needs_retry(attempts, operation_model, request_dict,
success_response, exception):
attempts += 1
# If there is a stream associated with the request, we need
# to reset it before attempting to send the request again.
# This will ensure that we resend the entire contents of the
# body.
request.reset_stream()
# Create a new request when retried (including a new signature).
request = self.create_request(
request_dict, operation_model)
success_response, exception = self._get_response(
request, operation_model, context)
if success_response is not None and \
'ResponseMetadata' in success_response[1]:
# We want to share num retries, not num attempts.
total_retries = attempts - 1
success_response[1]['ResponseMetadata']['RetryAttempts'] = \
total_retries
if exception is not None:
raise exception
else:
return success_response
def _get_response(self, request, operation_model, context):
# This will return a tuple of (success_response, exception)
# and success_response is itself a tuple of
# (http_response, parsed_dict).
# If an exception occurs then the success_response is None.
# If no exception occurs then exception is None.
success_response, exception = self._do_get_response(
request, operation_model)
kwargs_to_emit = {
'response_dict': None,
'parsed_response': None,
'context': context,
'exception': exception,
}
if success_response is not None:
http_response, parsed_response = success_response
kwargs_to_emit['parsed_response'] = parsed_response
kwargs_to_emit['response_dict'] = convert_to_response_dict(
http_response, operation_model)
service_id = operation_model.service_model.service_id.hyphenize()
self._event_emitter.emit(
'response-received.%s.%s' % (
service_id, operation_model.name), **kwargs_to_emit)
return success_response, exception
def _do_get_response(self, request, operation_model):
try:
logger.debug("Sending http request: %s", request)
history_recorder.record('HTTP_REQUEST', {
'method': request.method,
'headers': request.headers,
'streaming': operation_model.has_streaming_input,
'url': request.url,
'body': request.body
})
service_id = operation_model.service_model.service_id.hyphenize()
event_name = 'before-send.%s.%s' % (service_id, operation_model.name)
responses = self._event_emitter.emit(event_name, request=request)
http_response = first_non_none_response(responses)
if http_response is None:
http_response = self._send(request)
except HTTPClientError as e:
return (None, e)
except Exception as e:
logger.debug("Exception received when sending HTTP request.",
exc_info=True)
return (None, e)
# This returns the http_response and the parsed_data.
response_dict = convert_to_response_dict(http_response, operation_model)
http_response_record_dict = response_dict.copy()
http_response_record_dict['streaming'] = \
operation_model.has_streaming_output
history_recorder.record('HTTP_RESPONSE', http_response_record_dict)
protocol = operation_model.metadata['protocol']
parser = self._response_parser_factory.create_parser(protocol)
parsed_response = parser.parse(
response_dict, operation_model.output_shape)
# Do a second parsing pass to pick up on any modeled error fields
# NOTE: Ideally, we would push this down into the parser classes but
# they currently have no reference to the operation or service model
# The parsers should probably take the operation model instead of
# output shape but we can't change that now
if http_response.status_code >= 300:
self._add_modeled_error_fields(
response_dict, parsed_response,
operation_model, parser,
)
history_recorder.record('PARSED_RESPONSE', parsed_response)
return (http_response, parsed_response), None
def _add_modeled_error_fields(
self, response_dict, parsed_response,
operation_model, parser,
):
error_code = parsed_response.get("Error", {}).get("Code")
if error_code is None:
return
service_model = operation_model.service_model
error_shape = service_model.shape_for_error_code(error_code)
if error_shape is None:
return
modeled_parse = parser.parse(response_dict, error_shape)
# TODO: avoid naming conflicts with ResponseMetadata and Error
parsed_response.update(modeled_parse)
def _needs_retry(self, attempts, operation_model, request_dict,
response=None, caught_exception=None):
service_id = operation_model.service_model.service_id.hyphenize()
event_name = 'needs-retry.%s.%s' % (
service_id,
operation_model.name)
responses = self._event_emitter.emit(
event_name, response=response, endpoint=self,
operation=operation_model, attempts=attempts,
caught_exception=caught_exception, request_dict=request_dict)
handler_response = first_non_none_response(responses)
if handler_response is None:
return False
else:
# Request needs to be retried, and we need to sleep
# for the specified number of times.
logger.debug("Response received to retry, sleeping for "
"%s seconds", handler_response)
time.sleep(handler_response)
return True
def _send(self, request):
return self.http_session.send(request)
class EndpointCreator(object):
def __init__(self, event_emitter):
self._event_emitter = event_emitter
def create_endpoint(self, service_model, region_name, endpoint_url,
verify=None, response_parser_factory=None,
timeout=DEFAULT_TIMEOUT,
max_pool_connections=MAX_POOL_CONNECTIONS,
http_session_cls=URLLib3Session,
proxies=None,
socket_options=None,
client_cert=None):
if not is_valid_endpoint_url(endpoint_url):
raise ValueError("Invalid endpoint: %s" % endpoint_url)
if proxies is None:
proxies = self._get_proxies(endpoint_url)
endpoint_prefix = service_model.endpoint_prefix
logger.debug('Setting %s timeout as %s', endpoint_prefix, timeout)
http_session = http_session_cls(
timeout=timeout,
proxies=proxies,
verify=self._get_verify_value(verify),
max_pool_connections=max_pool_connections,
socket_options=socket_options,
client_cert=client_cert,
)
return Endpoint(
endpoint_url,
endpoint_prefix=endpoint_prefix,
event_emitter=self._event_emitter,
response_parser_factory=response_parser_factory,
http_session=http_session
)
def _get_proxies(self, url):
# We could also support getting proxies from a config file,
# but for now proxy support is taken from the environment.
return get_environ_proxies(url)
def _get_verify_value(self, verify):
# This is to account for:
# https://github.com/kennethreitz/requests/issues/1436
# where we need to honor REQUESTS_CA_BUNDLE because we're creating our
# own request objects.
# First, if verify is not None, then the user explicitly specified
# a value so this automatically wins.
if verify is not None:
return verify
# Otherwise use the value from REQUESTS_CA_BUNDLE, or default to
# True if the env var does not exist.
return os.environ.get('REQUESTS_CA_BUNDLE', True)
| [((1129, 1156), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (1146, 1156), False, 'import logging\n'), ((1176, 1205), 'botocore.history.get_global_history_recorder', 'get_global_history_recorder', ([], {}), '()\n', (1203, 1205), False, 'from botocore.history import get_global_history_recorder\n'), ((3075, 3091), 'threading.Lock', 'threading.Lock', ([], {}), '()\n', (3089, 3091), False, 'import threading\n'), ((3802, 3831), 'botocore.awsrequest.create_request_object', 'create_request_object', (['params'], {}), '(params)\n', (3823, 3831), False, 'from botocore.awsrequest import create_request_object\n'), ((10980, 11014), 'botocore.hooks.first_non_none_response', 'first_non_none_response', (['responses'], {}), '(responses)\n', (11003, 11014), False, 'from botocore.hooks import first_non_none_response\n'), ((13095, 13119), 'botocore.utils.get_environ_proxies', 'get_environ_proxies', (['url'], {}), '(url)\n', (13114, 13119), False, 'from botocore.utils import is_valid_endpoint_url, get_environ_proxies\n'), ((13681, 13723), 'os.environ.get', 'os.environ.get', (['"""REQUESTS_CA_BUNDLE"""', '(True)'], {}), "('REQUESTS_CA_BUNDLE', True)\n", (13695, 13723), False, 'import os\n'), ((3174, 3205), 'botocore.parsers.ResponseParserFactory', 'parsers.ResponseParserFactory', ([], {}), '()\n', (3203, 3205), False, 'from botocore import parsers\n'), ((3381, 3397), 'botocore.httpsession.URLLib3Session', 'URLLib3Session', ([], {}), '()\n', (3395, 3397), False, 'from botocore.httpsession import URLLib3Session\n'), ((8174, 8208), 'botocore.hooks.first_non_none_response', 'first_non_none_response', (['responses'], {}), '(responses)\n', (8197, 8208), False, 'from botocore.hooks import first_non_none_response\n'), ((11342, 11370), 'time.sleep', 'time.sleep', (['handler_response'], {}), '(handler_response)\n', (11352, 11370), False, 'import time\n'), ((12043, 12078), 'botocore.utils.is_valid_endpoint_url', 'is_valid_endpoint_url', (['endpoint_url'], {}), '(endpoint_url)\n', (12064, 12078), False, 'from botocore.utils import is_valid_endpoint_url, get_environ_proxies\n'), ((2342, 2382), 'botocore.response.StreamingBody', 'StreamingBody', (['http_response.raw', 'length'], {}), '(http_response.raw, length)\n', (2355, 2382), False, 'from botocore.response import StreamingBody\n')] |
RayshineRen/Introduction_to_Data_Science_in_Python | week2/Assignment2Answer.py | b19aa781a8f8d0e25853c4e86dadd4c9bebbcd71 | # -*- coding: utf-8 -*-
"""
Created on Fri Sep 18 21:56:15 2020
@author: Ray
@email: 1324789704@qq.com
@wechat: RayTing0305
"""
'''
Question 1
Write a function called proportion_of_education which returns the proportion of children in the dataset who had a mother with the education levels equal to less than high school (<12), high school (12), more than high school but not a college graduate (>12) and college degree.
This function should return a dictionary in the form of (use the correct numbers, do not round numbers):
{"less than high school":0.2,
"high school":0.4,
"more than high school but not college":0.2,
"college":0.2}
'''
import scipy.stats as stats
import numpy as np
import pandas as pd
df = pd.read_csv("./assets/NISPUF17.csv")
def proportion_of_education():
# your code goes here
# YOUR CODE HERE
df_edu = df.EDUC1
edu_list = [1, 2, 3, 4]
zero_df = pd.DataFrame(np.zeros((df_edu.shape[0], len(edu_list))), columns=edu_list)
for edu in edu_list:
zero_df[edu][df_edu==edu]=1
#zero_df
sum_ret = zero_df.sum(axis=0)
name_l = ["less than high school", "high school", "more than high school but not college", "college"]
rat = sum_ret.values/sum(sum_ret.values)
dic = dict()
for i in range(4):
dic[name_l[i]] = rat[i]
return dic
raise NotImplementedError()
assert type(proportion_of_education())==type({}), "You must return a dictionary."
assert len(proportion_of_education()) == 4, "You have not returned a dictionary with four items in it."
assert "less than high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "more than high school but not college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct"
'''
Question 2
Let's explore the relationship between being fed breastmilk as a child and getting a seasonal influenza vaccine from a healthcare provider. Return a tuple of the average number of influenza vaccines for those children we know received breastmilk as a child and those who know did not.
This function should return a tuple in the form (use the correct numbers:
(2.5, 0.1)
'''
def average_influenza_doses():
# YOUR CODE HERE
#是否喂养母乳
fed_breastmilk = list(df.groupby(by='CBF_01'))
be_fed_breastmilk = fed_breastmilk[0][1]
not_fed_breastmilk = fed_breastmilk[1][1]
#喂养母乳的influenza数目
be_fed_breastmilk_influenza = be_fed_breastmilk.P_NUMFLU
num_be_fed_breastmilk_influenza = be_fed_breastmilk_influenza.dropna().mean()
#未喂养母乳的influenza数目
not_be_fed_breastmilk_influenza = not_fed_breastmilk.P_NUMFLU
num_not_be_fed_breastmilk_influenza = not_be_fed_breastmilk_influenza.dropna().mean()
return num_be_fed_breastmilk_influenza, num_not_be_fed_breastmilk_influenza
raise NotImplementedError()
assert len(average_influenza_doses())==2, "Return two values in a tuple, the first for yes and the second for no."
'''
Question 3
It would be interesting to see if there is any evidence of a link between vaccine effectiveness and sex of the child. Calculate the ratio of the number of children who contracted chickenpox but were vaccinated against it (at least one varicella dose) versus those who were vaccinated but did not contract chicken pox. Return results by sex.
This function should return a dictionary in the form of (use the correct numbers):
{"male":0.2,
"female":0.4}
Note: To aid in verification, the chickenpox_by_sex()['female'] value the autograder is looking for starts with the digits 0.0077.
'''
def chickenpox_by_sex():
# YOUR CODE HERE
#是否感染Varicella
cpox = df.HAD_CPOX
#cpox.value_counts()
cpox_group = list(df.groupby(by='HAD_CPOX'))
have_cpox = cpox_group[0][1]
not_have_cpox = cpox_group[1][1]
#男女分开
have_cpox_group = list(have_cpox.groupby(by='SEX'))
not_have_cpox_group = list(not_have_cpox.groupby(by='SEX'))
have_cpox_boy = have_cpox_group[0][1]
have_cpox_girl = have_cpox_group[1][1]
not_have_cpox_boy = not_have_cpox_group[0][1]
not_have_cpox_girl = not_have_cpox_group[1][1]
#接种感染
#have_cpox_boy_injected = have_cpox_boy[(have_cpox_boy['P_NUMMMR']>0) | (have_cpox_boy['P_NUMVRC']>0)]
have_cpox_boy_injected = have_cpox_boy[(have_cpox_boy['P_NUMVRC']>0)]
num_have_cpox_boy_injected = have_cpox_boy_injected.count()['SEQNUMC']
have_cpox_girl_injected = have_cpox_girl[(have_cpox_girl['P_NUMVRC']>0)]
num_have_cpox_girl_injected = have_cpox_girl_injected.count()['SEQNUMC']
#接种未感染
not_have_cpox_boy_injected = not_have_cpox_boy[(not_have_cpox_boy['P_NUMVRC']>0)]
num_not_have_cpox_boy_injected = not_have_cpox_boy_injected.count()['SEQNUMC']
not_have_cpox_girl_injected = not_have_cpox_girl[(not_have_cpox_girl['P_NUMVRC']>0)]
num_not_have_cpox_girl_injected = not_have_cpox_girl_injected.count()['SEQNUMC']
#计算比例
ratio_boy = num_have_cpox_boy_injected / num_not_have_cpox_boy_injected
ratio_girl = num_have_cpox_girl_injected / num_not_have_cpox_girl_injected
dic = {}
dic['male'] = ratio_boy
dic['female'] = ratio_girl
return dic
raise NotImplementedError()
assert len(chickenpox_by_sex())==2, "Return a dictionary with two items, the first for males and the second for females."
'''
Question 4
A correlation is a statistical relationship between two variables. If we wanted to know if vaccines work, we might look at the correlation between the use of the vaccine and whether it results in prevention of the infection or disease [1]. In this question, you are to see if there is a correlation between having had the chicken pox and the number of chickenpox vaccine doses given (varicella).
Some notes on interpreting the answer. The had_chickenpox_column is either 1 (for yes) or 2 (for no), and the num_chickenpox_vaccine_column is the number of doses a child has been given of the varicella vaccine. A positive correlation (e.g., corr > 0) means that an increase in had_chickenpox_column (which means more no’s) would also increase the values of num_chickenpox_vaccine_column (which means more doses of vaccine). If there is a negative correlation (e.g., corr < 0), it indicates that having had chickenpox is related to an increase in the number of vaccine doses.
Also, pval is the probability that we observe a correlation between had_chickenpox_column and num_chickenpox_vaccine_column which is greater than or equal to a particular value occurred by chance. A small pval means that the observed correlation is highly unlikely to occur by chance. In this case, pval should be very small (will end in e-18 indicating a very small number).
[1] This isn’t really the full picture, since we are not looking at when the dose was given. It’s possible that children had chickenpox and then their parents went to get them the vaccine. Does this dataset have the data we would need to investigate the timing of the dose?
'''
def corr_chickenpox():
cpox = df[(df.P_NUMVRC).notnull()]
have_cpox = cpox[(cpox.HAD_CPOX==1) | (cpox.HAD_CPOX==2)]
df1=pd.DataFrame({"had_chickenpox_column":have_cpox.HAD_CPOX,
"num_chickenpox_vaccine_column":have_cpox.P_NUMVRC})
corr, pval=stats.pearsonr(df1["had_chickenpox_column"],df1["num_chickenpox_vaccine_column"])
return corr
raise NotImplementedError()
| [((758, 794), 'pandas.read_csv', 'pd.read_csv', (['"""./assets/NISPUF17.csv"""'], {}), "('./assets/NISPUF17.csv')\n", (769, 794), True, 'import pandas as pd\n'), ((7478, 7594), 'pandas.DataFrame', 'pd.DataFrame', (["{'had_chickenpox_column': have_cpox.HAD_CPOX,\n 'num_chickenpox_vaccine_column': have_cpox.P_NUMVRC}"], {}), "({'had_chickenpox_column': have_cpox.HAD_CPOX,\n 'num_chickenpox_vaccine_column': have_cpox.P_NUMVRC})\n", (7490, 7594), True, 'import pandas as pd\n'), ((7628, 7715), 'scipy.stats.pearsonr', 'stats.pearsonr', (["df1['had_chickenpox_column']", "df1['num_chickenpox_vaccine_column']"], {}), "(df1['had_chickenpox_column'], df1[\n 'num_chickenpox_vaccine_column'])\n", (7642, 7715), True, 'import scipy.stats as stats\n')] |
accordinglyto/dferte | backup/26.py | d4b8449c1633973dc538c9e72aca5d37802a4ee4 | from numpy import genfromtxt
import matplotlib.pyplot as plt
import mpl_finance
import numpy as np
import uuid
import matplotlib
# Input your csv file here with historical data
ad = genfromtxt(f"../financial_data/SM.csv", delimiter=",", dtype=str)
def convolve_sma(array, period):
return np.convolve(array, np.ones((period,)) / period, mode="valid")
def graphwerk(start, finish):
open = []
high = []
low = []
close = []
volume = []
# decision = []
date = []
c_open = []
c_high = []
c_low = []
c_close = []
c_volume = []
c_date = []
c_start = start + 12
for x in range(finish - start):
c_open.append(float(pd[c_start][1]))
c_high.append(float(pd[c_start][2]))
c_low.append(float(pd[c_start][3]))
c_close.append(float(pd[c_start][4]))
c_volume.append(float(pd[c_start][5]))
c_date.append(pd[c_start][0])
c_start = c_start + 1
for x in range(finish - start):
# Below filtering is valid for eurusd.csv file. Other financial data files have different orders so you need to find out
# what means open, high and close in their respective order.
open.append(float(pd[start][1]))
high.append(float(pd[start][2]))
low.append(float(pd[start][3]))
close.append(float(pd[start][4]))
volume.append(float(pd[start][5]))
# decision.append(str(pd[start][6]))
date.append(pd[start][0])
start = start + 1
decision = "sell"
min_forecast = min(c_low)
max_forecast = max(c_high)
if close[-1] * 1.03 < max_forecast:
decision = "buy"
# for z in all_prices:
# if close[-1] * 1.03 < z:
# decision = "buy"
sma = convolve_sma(close, 5)
smb = list(sma)
diff = sma[-1] - sma[-2]
for x in range(len(close) - len(smb)):
smb.append(smb[-1] + diff)
fig = plt.figure(num=1, figsize=(3, 3), dpi=50, facecolor="w", edgecolor="k")
dx = fig.add_subplot(111)
# mpl_finance.volume_overlay(ax, open, close, volume, width=0.4, colorup='b', colordown='b', alpha=1)
mpl_finance.candlestick2_ochl(
dx, open, close, high, low, width=1.5, colorup="g", colordown="r", alpha=0.5
)
plt.autoscale()
# plt.plot(smb, color="blue", linewidth=10, alpha=0.5)
plt.axis("off")
if decision == "sell":
print("last value: " + str(close[-1]))
print(
"range of values in next 13 bars: "
+ str(min_forecast)
+ "-"
+ str(max_forecast)
)
print("sell")
plt.savefig(sell_dir + str(uuid.uuid4()) + ".jpg", bbox_inches="tight")
else:
print("last value: " + str(close[-1]))
print(
"range of values in next 13 bars: "
+ str(min_forecast)
+ "-"
+ str(max_forecast)
)
print("buy")
plt.savefig(buy_dir + str(uuid.uuid4()) + ".jpg", bbox_inches="tight")
# if close[-1] >= close_next:
# print('previous value is bigger')
# print('last value: ' + str(close[-1]))
# print('next value: ' + str(close_next))
# print('sell')
# plt.savefig(sell_dir + str(uuid.uuid4()) +'.jpg', bbox_inches='tight')
# else:
# print('previous value is smaller')
# print('last value: '+ str(close[-1]))
# print('next value: ' + str(close_next))
# print('buy')
# plt.savefig(buy_dir + str(uuid.uuid4())+'.jpg', bbox_inches='tight')
# plt.show()
open.clear()
close.clear()
volume.clear()
high.clear()
low.clear()
plt.cla()
plt.clf()
# output = []
# with open("STOCKbluechip.csv") as f:
# output = [str(s) for line in f.readlines() for s in line[:-1].split(",")]
# for stock in output:
pd = ad
buy_dir = "../data/train/buy/"
sell_dir = "../data/train/sell/"
iter = 0
for x in range(len(pd)):
graphwerk(iter, iter + 12)
iter = iter + 2
| [((184, 249), 'numpy.genfromtxt', 'genfromtxt', (['f"""../financial_data/SM.csv"""'], {'delimiter': '""","""', 'dtype': 'str'}), "(f'../financial_data/SM.csv', delimiter=',', dtype=str)\n", (194, 249), False, 'from numpy import genfromtxt\n'), ((1909, 1980), 'matplotlib.pyplot.figure', 'plt.figure', ([], {'num': '(1)', 'figsize': '(3, 3)', 'dpi': '(50)', 'facecolor': '"""w"""', 'edgecolor': '"""k"""'}), "(num=1, figsize=(3, 3), dpi=50, facecolor='w', edgecolor='k')\n", (1919, 1980), True, 'import matplotlib.pyplot as plt\n'), ((2121, 2232), 'mpl_finance.candlestick2_ochl', 'mpl_finance.candlestick2_ochl', (['dx', 'open', 'close', 'high', 'low'], {'width': '(1.5)', 'colorup': '"""g"""', 'colordown': '"""r"""', 'alpha': '(0.5)'}), "(dx, open, close, high, low, width=1.5,\n colorup='g', colordown='r', alpha=0.5)\n", (2150, 2232), False, 'import mpl_finance\n'), ((2247, 2262), 'matplotlib.pyplot.autoscale', 'plt.autoscale', ([], {}), '()\n', (2260, 2262), True, 'import matplotlib.pyplot as plt\n'), ((2326, 2341), 'matplotlib.pyplot.axis', 'plt.axis', (['"""off"""'], {}), "('off')\n", (2334, 2341), True, 'import matplotlib.pyplot as plt\n'), ((3674, 3683), 'matplotlib.pyplot.cla', 'plt.cla', ([], {}), '()\n', (3681, 3683), True, 'import matplotlib.pyplot as plt\n'), ((3688, 3697), 'matplotlib.pyplot.clf', 'plt.clf', ([], {}), '()\n', (3695, 3697), True, 'import matplotlib.pyplot as plt\n'), ((315, 333), 'numpy.ones', 'np.ones', (['(period,)'], {}), '((period,))\n', (322, 333), True, 'import numpy as np\n'), ((2629, 2641), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (2639, 2641), False, 'import uuid\n'), ((2941, 2953), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (2951, 2953), False, 'import uuid\n')] |
JanSurft/tornado | streams/readers/arff_reader.py | 2c07686c5358d2bcb15d6edac3126ad9346c3c76 | """
The Tornado Framework
By Ali Pesaranghader
University of Ottawa, Ontario, Canada
E-mail: apesaran -at- uottawa -dot- ca / alipsgh -at- gmail -dot- com
"""
import re
from data_structures.attribute import Attribute
from dictionary.tornado_dictionary import TornadoDic
class ARFFReader:
"""This class is used to read a .arff file."""
@staticmethod
def read(file_path):
labels = []
attributes = []
attributes_min_max = []
records = []
data_flag = False
reader = open(file_path, "r")
for line in reader:
if line.strip() == '':
continue
if line.startswith("@attribute") or line.startswith("@ATTRIBUTE"):
line = line.strip('\n\r\t')
line = line.split(' ')
attribute_name = line[1]
attribute_value_range = line[2]
attribute = Attribute()
attribute.set_name(attribute_name)
if attribute_value_range.lower() in ['numeric', 'real', 'integer']:
attribute_type = TornadoDic.NUMERIC_ATTRIBUTE
attribute_value_range = []
attributes_min_max.append([0, 0])
else:
attribute_type = TornadoDic.NOMINAL_ATTRIBUTE
attribute_value_range = attribute_value_range.strip('{}').replace("'", "")
attribute_value_range = attribute_value_range.split(',')
attributes_min_max.append([None, None])
attribute.set_type(attribute_type)
attribute.set_possible_values(attribute_value_range)
attributes.append(attribute)
elif line.startswith("@data") or line.startswith("@DATA"):
data_flag = True
labels = attributes[len(attributes) - 1].POSSIBLE_VALUES
attributes.pop(len(attributes) - 1)
continue
elif data_flag is True:
line = re.sub('\s+', '', line)
elements = line.split(',')
for i in range(0, len(elements) - 1):
if attributes[i].TYPE == TornadoDic.NUMERIC_ATTRIBUTE:
elements[i] = float(elements[i])
min_value = attributes_min_max[i][0]
max_value = attributes_min_max[i][1]
if elements[i] < min_value:
min_value = elements[i]
elif elements[i] > max_value:
max_value = elements[i]
attributes_min_max[i] = [min_value, max_value]
records.append(elements)
for i in range(0, len(attributes)):
if attributes[i].TYPE == TornadoDic.NUMERIC_ATTRIBUTE:
attributes[i].set_bounds_values(attributes_min_max[i][0], attributes_min_max[i][1])
return labels, attributes, records
| [((981, 992), 'data_structures.attribute.Attribute', 'Attribute', ([], {}), '()\n', (990, 992), False, 'from data_structures.attribute import Attribute\n'), ((2119, 2143), 're.sub', 're.sub', (['"""\\\\s+"""', '""""""', 'line'], {}), "('\\\\s+', '', line)\n", (2125, 2143), False, 'import re\n')] |
MichaelSchwabe/conv-ebnas-abgabe | Experimente/Experiment ID 8/run-cifar10-v7.py | f463d7bbd9b514597e19d25007913f7994cbbf7c | from __future__ import print_function
from keras.datasets import mnist
from keras.datasets import cifar10
from keras.utils.np_utils import to_categorical
import numpy as np
from keras import backend as K
from evolution import Evolution
from genome_handler import GenomeHandler
import tensorflow as tf
#import mlflow.keras
#import mlflow
#import mlflow.tensorflow
#mlflow.tensorflow.autolog()
#mlflow.keras.autolog()
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
K.set_image_data_format("channels_last")
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2],x_train.shape[3]).astype('float32') / 255
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], x_test.shape[3]).astype('float32') / 255
# nCLasses
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
#y_train.shape
dataset = ((x_train, y_train), (x_test, y_test))
genome_handler = GenomeHandler(max_conv_layers=4,
max_dense_layers=2, # includes final dense layer
max_filters=512,
max_dense_nodes=1024,
input_shape=x_train.shape[1:],
n_classes=10)
evo = Evolution(genome_handler, data_path="log/evo_cifar10_gen40_pop10_e20.csv")
model = evo.run(dataset=dataset,
num_generations=40,
pop_size=10,
epochs=20,metric='acc')
#epochs=10,metric='loss')
print(model.summary()) | [((494, 534), 'keras.backend.set_image_data_format', 'K.set_image_data_format', (['"""channels_last"""'], {}), "('channels_last')\n", (517, 534), True, 'from keras import backend as K\n'), ((633, 652), 'keras.datasets.cifar10.load_data', 'cifar10.load_data', ([], {}), '()\n', (650, 652), False, 'from keras.datasets import cifar10\n'), ((912, 935), 'keras.utils.np_utils.to_categorical', 'to_categorical', (['y_train'], {}), '(y_train)\n', (926, 935), False, 'from keras.utils.np_utils import to_categorical\n'), ((945, 967), 'keras.utils.np_utils.to_categorical', 'to_categorical', (['y_test'], {}), '(y_test)\n', (959, 967), False, 'from keras.utils.np_utils import to_categorical\n'), ((1050, 1190), 'genome_handler.GenomeHandler', 'GenomeHandler', ([], {'max_conv_layers': '(4)', 'max_dense_layers': '(2)', 'max_filters': '(512)', 'max_dense_nodes': '(1024)', 'input_shape': 'x_train.shape[1:]', 'n_classes': '(10)'}), '(max_conv_layers=4, max_dense_layers=2, max_filters=512,\n max_dense_nodes=1024, input_shape=x_train.shape[1:], n_classes=10)\n', (1063, 1190), False, 'from genome_handler import GenomeHandler\n'), ((1379, 1453), 'evolution.Evolution', 'Evolution', (['genome_handler'], {'data_path': '"""log/evo_cifar10_gen40_pop10_e20.csv"""'}), "(genome_handler, data_path='log/evo_cifar10_gen40_pop10_e20.csv')\n", (1388, 1453), False, 'from evolution import Evolution\n'), ((452, 490), 'tensorflow.config.list_physical_devices', 'tf.config.list_physical_devices', (['"""GPU"""'], {}), "('GPU')\n", (483, 490), True, 'import tensorflow as tf\n')] |
JaredFG/Multiagentes-Unity | CarModel.py | 37f7ec5c0588865ef08b50df83566a43d817bebf | '''
Autores:Eduardo Rodríguez López A01749381
Rebeca Rojas Pérez A01751192
Jared Abraham Flores Guarneros A01379868
Eduardo Aguilar Chías A01749375
'''
from random import random
from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
from mesa.batchrunner import BatchRunner
from mesa.datacollection import DataCollector
from mesa.space import MultiGrid
from mesa import Agent , Model
from mesa.time import RandomActivation
#Clase para crear a los agentes automóviles
class CarAgent(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.next_cell = None
self.direction = None
self.agent_type = 0
#Función para validar si la posición es válida, en caso de que sea válida regresa True, en caso contrario
#regresa False.
def is_valid(self, position):
if position[0] < self.model.width and position[1] < self.model.height and position[0] >= 0 and position[1] >= 0:
if not self.model.grid.is_cell_empty(position):
return True
return False
#Función para recibir las posibles celdas a dónde moverse, regresa la posición de la calle.
def get_poss_cell(self):
neighborhood = self.model.grid.get_neighborhood(self.pos, moore=False, include_center=False)
for cell in neighborhood:
for agent in self.model.grid.get_cell_list_contents(cell):
if agent.agent_type == 2:
next_dir = (self.pos[0] - agent.pos[0], self.pos[1] - agent.pos[1])
if next_dir[0] * -1 != self.direction[0] and next_dir[1] * -1 != self.direction[1]:
return agent.pos
#Función para avanzar hacia el frente, regresa el valor de la variable move que son coordenadas.
def get_nextcell(self):
move = (self.pos[0] + self.direction[0], self.pos[1] + self.direction[1])
return move
#Función para obtener la dirección hacia donde debe moverse el automóvil, regresa la dirección
# de la calle.
def get_nextdirect(self, position):
for agent in self.model.grid.get_cell_list_contents(position):
if agent.agent_type == 2:
return agent.direction
#Función para dar vuelta, regresa la dirección de la calle.
def turn(self):
for cell in self.model.grid.get_neighborhood(self.pos, moore=False, include_center=False):
for agent in self.model.grid.get_cell_list_contents(cell):
if agent.agent_type == 2:
if agent.direction != self.direction:
return agent.direction
return None
#Función para revisar la luz de los semáforos, regresa la luz del semáforo en caso
# de que el automóvil tenga uno de vecino. En caso contrario regresa True.
def check_light(self):
for agent in self.model.grid.get_cell_list_contents(self.next_cell):
if agent.agent_type == 1:
return agent.light
return True
#Función para checar si hay otro automovil enfrente, regresa un valor booleano.
def check_car(self):
for agent in self.model.grid.get_cell_list_contents(self.next_cell):
if agent.agent_type == 0:
return False
return True
def step(self):
#Variable para guardar el resultado de la función get_nextcell().
next_cell = self.get_nextcell()
#Condición, si la siguiente celda es válida, se guarda en el automóvil y se cambia su dirección.
if self.is_valid(next_cell):
self.next_cell = next_cell
self.direction = self.get_nextdirect(self.next_cell)
#En caso contrario una varible guarda el resultado de la función turn().
else:
direction = self.turn()
#Condición, si la variable direction es verdadera se cambia la dirección del automóvil.
if direction:
self.direction = direction
#En caso contrario una variable guarda el resultado de la función get_poss_cell().
#La siguiente celda del automóvil cambia al valor de la variable.
else:
poss = self.get_poss_cell()
self.next_cell = poss
if self.check_car():
if self.check_light():
self.model.grid.move_agent(self, self.next_cell)
#Clase para crear a los agentes semáforos.
class TrafficLightAgent(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.agent_type = 1
self.light = False
#Función para cambiar la luz de los semáforos.
def change(self):
self.light = not self.light
#Función para contar el número de automóviles que hay en un semáforo,
# regresa el contador con el número de automóviles.
def count_cars(self):
counter = 0
neighborhood = self.model.grid.get_neighborhood(self.pos, moore=False, include_center=True)
for cell in neighborhood:
for agent in self.model.grid.get_cell_list_contents(cell):
if agent.agent_type == 0:
counter += 1
return counter
#Clase para crear a los agentes calle.
class StreetAgent(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.direction = None
self.agent_type = 2
#Clase para crear el modelo.
class CarModel(Model):
def __init__(self, N: int, width: int, height: int):
self.num_agents = N
self.running = True
self.grid = MultiGrid(width, height, False)
self.schedule = RandomActivation(self)
self.uids = 0
self.lights_ids = 0
self.width = width
self.height = height
street_pos = []
self.lights = 4
#Loop para crear la parte interior de las calles, donde está el cruce.
for row in range(height):
for col in range(width):
agent = StreetAgent(self.uids, self)
self.uids += 1
flag = True
if col > width // 2 - 2 and col < width // 2 + 1 and col > 1 and col < height - 1:
if row >= height // 2:
agent.direction = (0, 1)
else:
agent.direction = (0, -1)
elif row > height // 2 - 2 and row < height // 2 + 1 and row > 1 and row < width - 1:
if col > width // 2:
agent.direction = (-1, 0)
else:
agent.direction = (1, 0)
else:
flag = False
if flag:
self.grid.place_agent(agent, (col, row))
street_pos.append((col, row))
#Loop para crear la parte exterior de las calles, donde NO está el cruce.
for row in range(height):
for col in range(width):
agent = StreetAgent(self.uids, self)
self.uids += 1
flag = True
if row < 2:
if col < width - 2:
agent.direction = (1, 0)
else:
agent.direction = (0, 1)
elif row >= 2 and row < height - 2:
if col < 2:
agent.direction = (0, -1)
elif col >= width - 2 and col < width:
agent.direction = (0, 1)
else:
flag = False
elif row >= height -2 and row < height:
if col < width - 2:
agent.direction = (-1, 0)
else:
agent.direction = (0, 1)
else:
flag = False
if flag:
self.grid.place_agent(agent, (col, row))
street_pos.append((col, row))
#Loop para crear los automóviles en posiciones random donde hay calle.
for i in range(self.num_agents):
a = CarAgent(self.uids, self)
self.uids += 1
pos_index = self.random.randint(0, len(street_pos) - 1)
pos = street_pos.pop(pos_index)
a.direction = self.grid.get_cell_list_contents(pos)[0].direction
self.grid.place_agent(a, pos)
self.schedule.add(a)
#Crear los semáforos
for i in range(self.lights):
alight = TrafficLightAgent(self.lights_ids, self)
self.lights_ids += 1
self.schedule.add(alight)
x = 8
y = 9
if i == 0:
alight.light = True
self.grid.place_agent(alight, (x, y))
elif i == 1:
x = 8
y = 10
alight.light = True
self.grid.place_agent(alight, (x, y))
elif i == 2:
x = 11
y = 9
alight.light = False
self.grid.place_agent(alight, (x, y))
else:
x = 11
y = 10
alight.light = False
self.grid.place_agent(alight, (x, y))
def step(self):
#Contadores para saber cuáles semáforos tienen más automóviles.
count_left = 0
count_right = 0
#Loop para añadir a los contadores la cantidad de automóviles que hay en cada lado.
for agent in self.schedule.agents:
if agent.agent_type == 1:
if agent.unique_id == 0:
count_left += agent.count_cars()
elif agent.unique_id == 1:
count_left += agent.count_cars()
elif agent.unique_id == 2:
count_right += agent.count_cars()
elif agent.unique_id == 3:
count_right += agent.count_cars()
#Condición, si el lado izquierdo tiene más automóviles, los semáforos del lado izquierdo
#dan luz verde y los semáforos del lado derecho dan luz roja.
if count_left >= count_right:
for agent in self.schedule.agents:
if agent.agent_type == 1:
if agent.unique_id == 0:
agent.light = True
elif agent.unique_id == 1:
agent.light = True
elif agent.unique_id == 2:
agent.light = False
else:
agent.light = False
#En caso contrario los semáforos del lado derecho dan luz verde y los semáforos del lado
#izquierdo dan luz roja.
else:
for agent in self.schedule.agents:
if agent.agent_type == 1:
if agent.unique_id == 0:
agent.light = False
elif agent.unique_id == 1:
agent.light = False
elif agent.unique_id == 2:
agent.light = True
else:
agent.light = True
self.schedule.step() | [((5808, 5839), 'mesa.space.MultiGrid', 'MultiGrid', (['width', 'height', '(False)'], {}), '(width, height, False)\n', (5817, 5839), False, 'from mesa.space import MultiGrid\n'), ((5864, 5886), 'mesa.time.RandomActivation', 'RandomActivation', (['self'], {}), '(self)\n', (5880, 5886), False, 'from mesa.time import RandomActivation\n')] |
nulinspiratie/Qcodes | qcodes/widgets/display.py | d050d38ac83f532523a39549c3247dfa6096a36e | """Helper for adding content stored in a file to a jupyter notebook."""
import os
from pkg_resources import resource_string
from IPython.display import display, Javascript, HTML
# Originally I implemented this using regular open() and read(), so it
# could use relative paths from the importing file.
#
# But for distributable packages, pkg_resources.resource_string is the
# best way to load data files, because it works even if the package is
# in an egg or zip file. See:
# http://pythonhosted.org/setuptools/setuptools.html#accessing-data-files-at-runtime
def display_auto(qcodes_path, file_type=None):
"""
Display some javascript, css, or html content in a jupyter notebook.
Content comes from a package-relative file path. Will use the file
extension to determine file type unless overridden by file_type
Args:
qcodes_path (str): the path to the target file within the qcodes
package, like 'widgets/widgets.js'
file_type (Optional[str]): Override the file extension to determine
what type of file this is. Case insensitive, supported values
are 'js', 'css', and 'html'
"""
contents = resource_string('qcodes', qcodes_path).decode('utf-8')
if file_type is None:
ext = os.path.splitext(qcodes_path)[1].lower()
elif 'js' in file_type.lower():
ext = '.js'
elif 'css' in file_type.lower():
ext = '.css'
else:
ext = '.html'
if ext == '.js':
display(Javascript(contents))
elif ext == '.css':
display(HTML('<style>' + contents + '</style>'))
else:
# default to html. Anything else?
display(HTML(contents))
| [((1176, 1214), 'pkg_resources.resource_string', 'resource_string', (['"""qcodes"""', 'qcodes_path'], {}), "('qcodes', qcodes_path)\n", (1191, 1214), False, 'from pkg_resources import resource_string\n'), ((1497, 1517), 'IPython.display.Javascript', 'Javascript', (['contents'], {}), '(contents)\n', (1507, 1517), False, 'from IPython.display import display, Javascript, HTML\n'), ((1559, 1598), 'IPython.display.HTML', 'HTML', (["('<style>' + contents + '</style>')"], {}), "('<style>' + contents + '</style>')\n", (1563, 1598), False, 'from IPython.display import display, Javascript, HTML\n'), ((1668, 1682), 'IPython.display.HTML', 'HTML', (['contents'], {}), '(contents)\n', (1672, 1682), False, 'from IPython.display import display, Javascript, HTML\n'), ((1272, 1301), 'os.path.splitext', 'os.path.splitext', (['qcodes_path'], {}), '(qcodes_path)\n', (1288, 1301), False, 'import os\n')] |
smr99/lego-hub-tk | hubcontrol.py | d3b86847873fa80deebf993ccd44b4d3d8f9bf40 | #! /usr/bin/python3
import base64
from data.ProgramHubLogger import ProgramHubLogger
from datetime import datetime
import logging
import os
import sys
from ui.MotionSensor import MotionSensorWidget
from ui.PositionStatus import PositionStatusWidget
from ui.DevicePortWidget import DevicePortWidget
from ui.ConnectionWidget import ConnectionWidget
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
from comm.HubClient import ConnectionState, HubClient
from data.HubMonitor import HubMonitor
from data.HubStatus import HubStatus
from ui.DeviceStatusWidget import DeviceStatusWidget
from utils.setup import setup_logging
logger = logging.getLogger("App")
log_filename = os.path.dirname(__file__) + "/logs/hubcontrol.log"
setup_logging(log_filename)
def list_programs(info):
storage = info['storage']
slots = info['slots']
print("%4s %-40s %6s %-20s %-12s %-10s" % ("Slot", "Decoded Name", "Size", "Last Modified", "Project_id", "Type"))
for i in range(20):
if str(i) in slots:
sl = slots[str(i)]
modified = datetime.utcfromtimestamp(sl['modified']/1000).strftime('%Y-%m-%d %H:%M:%S')
try:
decoded_name = base64.b64decode(sl['name']).decode('utf-8')
except:
decoded_name = sl['name']
try:
project = sl['project_id']
except:
project = " "
try:
type = sl['type']
except:
type = " "
print("%4s %-40s %5db %-20s %-12s %-10s" % (i, decoded_name, sl['size'], modified, project, type))
print(("Storage free %s%s of total %s%s" % (storage['free'], storage['unit'], storage['total'], storage['unit'])))
class ConsoleWidget(QTextEdit):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setReadOnly(True)
self.setLineWrapMode(QTextEdit.NoWrap)
def append(self, text):
self.moveCursor(QTextCursor.End)
self.insertPlainText(text)
sb = self.verticalScrollBar()
sb.setValue(sb.maximum())
def append_line(self, text): self.append(text + '\n')
class ProgramWidget(QWidget):
def __init__(self, hub_client : HubClient, hub_monitor : HubMonitor, *args, **kwargs):
super().__init__(*args, **kwargs)
self._client = hub_client
self._monitor = hub_monitor
self._executing_program_label = QLabel()
self._slot_spinbox = QSpinBox()
self._run_button = QPushButton('Run')
self._run_button.clicked.connect(self.run_program)
self._stop_button = QPushButton('Stop')
self._stop_button.clicked.connect(self.stop_program)
runstop_widget = QWidget()
layout = QHBoxLayout(runstop_widget)
layout.addWidget(QLabel('Slot:'))
layout.addWidget(self._slot_spinbox)
layout.addWidget(self._run_button)
layout.addWidget(self._stop_button)
box = QGroupBox('Program Execution')
layout = QFormLayout(box)
layout.addRow('Executing Program ID:', self._executing_program_label)
layout.addRow(runstop_widget)
layout = QVBoxLayout()
layout.addWidget(box)
self.setLayout(layout)
def refresh(self):
is_connected = self._client.state == ConnectionState.TELEMETRY
self._executing_program_label.setText(self._monitor.execution_status[0])
self._run_button.setEnabled(is_connected)
self._stop_button.setEnabled(is_connected)
def run_program(self):
slot = self._slot_spinbox.value()
r = self._client.program_execute(slot)
logger.debug('Program execute returns: %s', r)
def stop_program(self):
r = self._client.program_terminate()
logger.debug('Program terminate returns: %s', r)
class MainWindow(QMainWindow):
def __init__(self, hub_client, hub_monitor, *args, **kwargs):
super().__init__(*args, **kwargs)
status = hub_monitor.status
self._client = hub_client
self._hub_monitor = hub_monitor
self.position_widget = PositionStatusWidget(status)
self.motion_widget = MotionSensorWidget(status)
self.program_widget = ProgramWidget(hub_client, hub_monitor)
self.port_widget = DevicePortWidget(status)
self.console = ConsoleWidget()
self.list_button = QPushButton('List')
self.list_button.clicked.connect(self.list_programs)
# Top row (status)
top_box = QWidget()
layout = QHBoxLayout(top_box)
layout.addWidget(ConnectionWidget(hub_client))
layout.addWidget(self.position_widget)
layout.addWidget(self.motion_widget)
# Button bar
buttons = QWidget()
layout = QHBoxLayout(buttons)
layout.addWidget(self.list_button)
mw = QWidget()
layout = QVBoxLayout(mw)
layout.addWidget(top_box)
layout.addWidget(buttons)
layout.addWidget(self.program_widget)
layout.addWidget(self.port_widget)
layout.addWidget(self.console)
self.setCentralWidget(mw)
hub_monitor.events.console_print += self.console.append
# Timer refresh trick from https://github.com/Taar2/pyqt5-modelview-tutorial/blob/master/modelview_3.py
# this trick is used to work around the issue of updating UI from background threads -- i.e. events
# raised by HubClient.
timer = QtCore.QTimer(self)
timer.setInterval(200)
timer.timeout.connect(self.refresh)
timer.start()
def refresh(self):
is_connected = self._client.state == ConnectionState.TELEMETRY
is_connected_usb = is_connected and self._hub_monitor.status.is_usb_connected
self.list_button.setEnabled(is_connected_usb)
self.position_widget.refresh()
self.motion_widget.refresh()
self.port_widget.refresh()
self.program_widget.refresh()
def list_programs(self):
storage_status = self._client.get_storage_status()
if storage_status is not None:
list_programs(storage_status)
def run_program(self):
slot = 4
r = self._client.program_execute(slot)
print('Program execute returns: ', r)
logger.info("LEGO status app starting up")
hc = HubClient()
monitor = HubMonitor(hc)
monitor.logger = ProgramHubLogger('logs/program')
app = QApplication(sys.argv)
window = MainWindow(hc, monitor)
window.setWindowTitle('LEGO Hub Status')
window.show()
hc.start()
sys.exit(app.exec_())
| [((770, 794), 'logging.getLogger', 'logging.getLogger', (['"""App"""'], {}), "('App')\n", (787, 794), False, 'import logging\n'), ((862, 889), 'utils.setup.setup_logging', 'setup_logging', (['log_filename'], {}), '(log_filename)\n', (875, 889), False, 'from utils.setup import setup_logging\n'), ((6458, 6469), 'comm.HubClient.HubClient', 'HubClient', ([], {}), '()\n', (6467, 6469), False, 'from comm.HubClient import ConnectionState, HubClient\n'), ((6480, 6494), 'data.HubMonitor.HubMonitor', 'HubMonitor', (['hc'], {}), '(hc)\n', (6490, 6494), False, 'from data.HubMonitor import HubMonitor\n'), ((6512, 6544), 'data.ProgramHubLogger.ProgramHubLogger', 'ProgramHubLogger', (['"""logs/program"""'], {}), "('logs/program')\n", (6528, 6544), False, 'from data.ProgramHubLogger import ProgramHubLogger\n'), ((6552, 6574), 'PyQt5.QtWidgets.QApplication', 'QApplication', (['sys.argv'], {}), '(sys.argv)\n', (6564, 6574), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((811, 836), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (826, 836), False, 'import os\n'), ((2657, 2675), 'PyQt5.QtWidgets.QPushButton', 'QPushButton', (['"""Run"""'], {}), "('Run')\n", (2668, 2675), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((2763, 2782), 'PyQt5.QtWidgets.QPushButton', 'QPushButton', (['"""Stop"""'], {}), "('Stop')\n", (2774, 2782), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((2870, 2879), 'PyQt5.QtWidgets.QWidget', 'QWidget', ([], {}), '()\n', (2877, 2879), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((4253, 4281), 'ui.PositionStatus.PositionStatusWidget', 'PositionStatusWidget', (['status'], {}), '(status)\n', (4273, 4281), False, 'from ui.PositionStatus import PositionStatusWidget\n'), ((4311, 4337), 'ui.MotionSensor.MotionSensorWidget', 'MotionSensorWidget', (['status'], {}), '(status)\n', (4329, 4337), False, 'from ui.MotionSensor import MotionSensorWidget\n'), ((4435, 4459), 'ui.DevicePortWidget.DevicePortWidget', 'DevicePortWidget', (['status'], {}), '(status)\n', (4451, 4459), False, 'from ui.DevicePortWidget import DevicePortWidget\n'), ((4527, 4546), 'PyQt5.QtWidgets.QPushButton', 'QPushButton', (['"""List"""'], {}), "('List')\n", (4538, 4546), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((4654, 4663), 'PyQt5.QtWidgets.QWidget', 'QWidget', ([], {}), '()\n', (4661, 4663), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((4889, 4898), 'PyQt5.QtWidgets.QWidget', 'QWidget', ([], {}), '()\n', (4896, 4898), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((4994, 5003), 'PyQt5.QtWidgets.QWidget', 'QWidget', ([], {}), '()\n', (5001, 5003), False, 'from PyQt5.QtWidgets import QApplication, QPushButton, QWidget\n'), ((5600, 5619), 'PyQt5.QtCore.QTimer', 'QtCore.QTimer', (['self'], {}), '(self)\n', (5613, 5619), False, 'from PyQt5 import QtCore, QtGui, QtWidgets\n'), ((4727, 4755), 'ui.ConnectionWidget.ConnectionWidget', 'ConnectionWidget', (['hub_client'], {}), '(hub_client)\n', (4743, 4755), False, 'from ui.ConnectionWidget import ConnectionWidget\n'), ((1199, 1247), 'datetime.datetime.utcfromtimestamp', 'datetime.utcfromtimestamp', (["(sl['modified'] / 1000)"], {}), "(sl['modified'] / 1000)\n", (1224, 1247), False, 'from datetime import datetime\n'), ((1324, 1352), 'base64.b64decode', 'base64.b64decode', (["sl['name']"], {}), "(sl['name'])\n", (1340, 1352), False, 'import base64\n')] |
shagun-agrawal/To-Do-App | To-D0-App-main/base/views.py | 083081690fe9d291f13c0452a695a092b7544ab2 | from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib.auth.views import LoginView
from .models import Task
# Create your views here.
class CustomLoginView(LoginView):
template_name='base/login.html'
fiels='__all__'
redirect_auhenticated_user = True
def get_success_url(self):
return reverse_lazy('tasks')
class TaskList(ListView):
model = Task
context_object_name = 'tasks'
class TaskDetail(DetailView):
model = Task
context_object_name = 'task'
class TaskCreate(CreateView):
model = Task
fields = '__all__'
success_url = reverse_lazy('tasks')
class TaskUpdate(UpdateView):
model = Task
fields = '__all__'
success_url = reverse_lazy('tasks')
class TaskDelete(DeleteView):
model = Task
context_object_name='Task'
success_url = reverse_lazy('tasks')
| [((823, 844), 'django.urls.reverse_lazy', 'reverse_lazy', (['"""tasks"""'], {}), "('tasks')\n", (835, 844), False, 'from django.urls import reverse_lazy\n'), ((939, 960), 'django.urls.reverse_lazy', 'reverse_lazy', (['"""tasks"""'], {}), "('tasks')\n", (951, 960), False, 'from django.urls import reverse_lazy\n'), ((1063, 1084), 'django.urls.reverse_lazy', 'reverse_lazy', (['"""tasks"""'], {}), "('tasks')\n", (1075, 1084), False, 'from django.urls import reverse_lazy\n'), ((540, 561), 'django.urls.reverse_lazy', 'reverse_lazy', (['"""tasks"""'], {}), "('tasks')\n", (552, 561), False, 'from django.urls import reverse_lazy\n')] |
samysweb/dnnv | tests/unit_tests/test_nn/test_converters/test_tensorflow/test_Dropout.py | 58fb95b7300914d9da28eed86c39eca473b1aaef | import numpy as np
from dnnv.nn.converters.tensorflow import *
from dnnv.nn.operations import *
TOL = 1e-6
def test_Dropout_consts():
x = np.array([3, 4]).astype(np.float32)
op = Dropout(x)
tf_op = TensorflowConverter().visit(op)
result_ = tf_op()
assert isinstance(result_, tuple)
assert len(result_) == 2
result, none = result_
assert none is None
y = np.array([3, 4]).astype(np.float32)
assert np.all(result >= (y - TOL))
assert np.all(result <= (y + TOL))
def test_Dropout_x_is_op():
x = np.array([3, 4]).astype(np.float32)
input_op = Input((2,), np.dtype(np.float32))
op = Dropout(input_op)
tf_op = TensorflowConverter().visit(op)
result_ = tf_op(x)
assert isinstance(result_, tuple)
assert len(result_) == 2
result, none = result_
assert none is None
y = np.array([3, 4]).astype(np.float32)
assert np.all(result >= (y - TOL))
assert np.all(result <= (y + TOL))
| [((442, 467), 'numpy.all', 'np.all', (['(result >= y - TOL)'], {}), '(result >= y - TOL)\n', (448, 467), True, 'import numpy as np\n'), ((481, 506), 'numpy.all', 'np.all', (['(result <= y + TOL)'], {}), '(result <= y + TOL)\n', (487, 506), True, 'import numpy as np\n'), ((899, 924), 'numpy.all', 'np.all', (['(result >= y - TOL)'], {}), '(result >= y - TOL)\n', (905, 924), True, 'import numpy as np\n'), ((938, 963), 'numpy.all', 'np.all', (['(result <= y + TOL)'], {}), '(result <= y + TOL)\n', (944, 963), True, 'import numpy as np\n'), ((610, 630), 'numpy.dtype', 'np.dtype', (['np.float32'], {}), '(np.float32)\n', (618, 630), True, 'import numpy as np\n'), ((147, 163), 'numpy.array', 'np.array', (['[3, 4]'], {}), '([3, 4])\n', (155, 163), True, 'import numpy as np\n'), ((395, 411), 'numpy.array', 'np.array', (['[3, 4]'], {}), '([3, 4])\n', (403, 411), True, 'import numpy as np\n'), ((547, 563), 'numpy.array', 'np.array', (['[3, 4]'], {}), '([3, 4])\n', (555, 563), True, 'import numpy as np\n'), ((852, 868), 'numpy.array', 'np.array', (['[3, 4]'], {}), '([3, 4])\n', (860, 868), True, 'import numpy as np\n')] |
idsc-frazzoli/SMARTS | smarts/zoo/worker.py | bae0a6ea160330921edc94a7161a4e8cf72a1974 | # Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
#
# 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.
"""
Run an agent in it's own (independent) process.
What Agent code does is out of our direct control, we want to avoid any interactions with global state that might be present in the SMARTS process.
To protect and isolate Agents from any pollution of global state in the main SMARTS process, we spawn Agents in their fresh and independent python process.
This script is called from within SMARTS to instantiate a remote agent.
The protocal is as follows:
1. SMARTS calls: worker.py --port 5467 # sets a unique port per agent
2. worker.py will begin listening on port 5467.
3. SMARTS connects to (ip, 5467) as a client.
4. SMARTS calls `build()` rpc with `AgentSpec` as input.
5. worker.py recieves the `AgentSpec` instances and builds the Agent.
6. SMARTS calls `act()` rpc with observation as input and receives the actions as response from worker.py.
"""
import argparse
import importlib
import logging
import os
import signal
import sys
from concurrent import futures
import grpc
from smarts.zoo import worker_pb2_grpc, worker_servicer
# Front-load some expensive imports as to not block the simulation
modules = [
"smarts.core.utils.pybullet",
"smarts.core.utils.sumo",
"smarts.core.sumo_road_network",
"numpy",
"sklearn",
"shapely",
"scipy",
"trimesh",
"panda3d",
"gym",
"ray",
]
for mod in modules:
try:
importlib.import_module(mod)
except ImportError:
if mod == "ray":
print(
"You need to install the ray dependency using pip install -e .[train] first"
)
if mod == "panda3d":
print(
"You need to install the panda3d dependency using pip install -e .[camera-obs] first"
)
pass
# End front-loaded imports
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(f"worker.py - pid({os.getpid()})")
def serve(port):
ip = "[::]"
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
worker_pb2_grpc.add_WorkerServicer_to_server(
worker_servicer.WorkerServicer(), server
)
server.add_insecure_port(f"{ip}:{port}")
server.start()
log.debug(f"Worker - ip({ip}), port({port}), pid({os.getpid()}): Started serving.")
def stop_server(unused_signum, unused_frame):
server.stop(0)
log.debug(
f"Worker - ip({ip}), port({port}), pid({os.getpid()}): Received interrupt signal."
)
# Catch keyboard interrupt and terminate signal
signal.signal(signal.SIGINT, stop_server)
signal.signal(signal.SIGTERM, stop_server)
# Wait to receive server termination signal
server.wait_for_termination()
log.debug(f"Worker - ip({ip}), port({port}), pid({os.getpid()}): Server exited")
if __name__ == "__main__":
parser = argparse.ArgumentParser("Run an agent in an independent process.")
parser.add_argument(
"--port",
type=int,
required=True,
help="Port to listen for remote client connections.",
)
args = parser.parse_args()
serve(args.port)
| [((2917, 2956), 'logging.basicConfig', 'logging.basicConfig', ([], {'level': 'logging.INFO'}), '(level=logging.INFO)\n', (2936, 2956), False, 'import logging\n'), ((3631, 3672), 'signal.signal', 'signal.signal', (['signal.SIGINT', 'stop_server'], {}), '(signal.SIGINT, stop_server)\n', (3644, 3672), False, 'import signal\n'), ((3677, 3719), 'signal.signal', 'signal.signal', (['signal.SIGTERM', 'stop_server'], {}), '(signal.SIGTERM, stop_server)\n', (3690, 3719), False, 'import signal\n'), ((3930, 3996), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (['"""Run an agent in an independent process."""'], {}), "('Run an agent in an independent process.')\n", (3953, 3996), False, 'import argparse\n'), ((2506, 2534), 'importlib.import_module', 'importlib.import_module', (['mod'], {}), '(mod)\n', (2529, 2534), False, 'import importlib\n'), ((3076, 3117), 'concurrent.futures.ThreadPoolExecutor', 'futures.ThreadPoolExecutor', ([], {'max_workers': '(1)'}), '(max_workers=1)\n', (3102, 3117), False, 'from concurrent import futures\n'), ((3177, 3209), 'smarts.zoo.worker_servicer.WorkerServicer', 'worker_servicer.WorkerServicer', ([], {}), '()\n', (3207, 3209), False, 'from smarts.zoo import worker_pb2_grpc, worker_servicer\n'), ((3000, 3011), 'os.getpid', 'os.getpid', ([], {}), '()\n', (3009, 3011), False, 'import os\n'), ((3342, 3353), 'os.getpid', 'os.getpid', ([], {}), '()\n', (3351, 3353), False, 'import os\n'), ((3857, 3868), 'os.getpid', 'os.getpid', ([], {}), '()\n', (3866, 3868), False, 'import os\n'), ((3521, 3532), 'os.getpid', 'os.getpid', ([], {}), '()\n', (3530, 3532), False, 'import os\n')] |
Nburkhal/mit-cs250 | week2/problems/problem2.py | a3d32a217deb2cfa1b94d8188bef73c0742b1245 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Now write a program that calculates the minimum fixed monthly payment needed
in order pay off a credit card balance within 12 months.
By a fixed monthly payment, we mean a single number which does not change each month,
but instead is a constant amount that will be paid each month.
In this problem, we will not be dealing with a minimum monthly payment rate.
The following variables contain values as described below:
balance - the outstanding balance on the credit card
annualInterestRate - annual interest rate as a decimal
The program should print out one line: the lowest monthly payment
that will pay off all debt in under 1 year, for example:
Lowest Payment: 180
Assume that the interest is compounded monthly according to
the balance at the end of the month (after the payment for that month is made).
The monthly payment must be a multiple of $10 and is the same for all months.
Notice that it is possible for the balance to become negative
using this payment scheme, which is okay. A summary of the required math is found below:
Monthly interest rate = (Annual interest rate) / 12.0
Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
Test Case 1:
balance = 3329
annualInterestRate = 0.2
Result Your Code Should Generate:
-------------------
Lowest Payment: 310
Test Case 2:
balance = 4773
annualInterestRate = 0.2
Result Your Code Should Generate:
-------------------
Lowest Payment: 440
Test Case 3:
balance = 3926
annualInterestRate = 0.2
Result Your Code Should Generate:
-------------------
Lowest Payment: 360
"""
# Establish variables that we know / needed for the evaluation.
# Counter optional
balance = 3329
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12
monthlyPayment = 0
updatedBalance = balance
counter = 0
# Will loop through everything until we find a rate that will reduce updatedBalance to 0.
while updatedBalance > 0:
# Was stated that payments needed to happen in increments of $10
monthlyPayment += 10
# To reset balance back to actual balance when loop inevitably fails.
updatedBalance = balance
month = 1
# For 12 months and while balance is not 0...
while month <= 12 and updatedBalance > 0:
# Subtract the ($10*n) amount
updatedBalance -= monthlyPayment
# Compound the interest AFTER making monthly payment
interest = monthlyInterestRate * updatedBalance
updatedBalance += interest
# Increase month counter
month += 1
counter += 1
print("Lowest Payment: ", monthlyPayment)
print("Number of iterations: ", counter)
| [] |
yennanliu/CS_basics | leetcode_python/Sort/sort-characters-by-frequency.py | 3c50c819897a572ff38179bfb0083a19b2325fde | # V0
import collections
class Solution(object):
def frequencySort(self, s):
count = collections.Counter(s)
count_dict = dict(count)
count_tuple_sorted = sorted(count_dict.items(), key=lambda kv : -kv[1])
res = ''
for item in count_tuple_sorted:
res += item[0] * item[1]
return res
# V0'
# IDEA : collections.Counter(s).most_common
class Solution(object):
def frequencySort(self, s):
return ''.join(c * t for c, t in collections.Counter(s).most_common())
# V1
# IDEA : SORT
# https://blog.csdn.net/fuxuemingzhu/article/details/79437548
import collections
class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
count = collections.Counter(s).most_common()
res = ''
for c, v in count:
res += c * v
return res
### Test case:
s=Solution()
assert s.frequencySort(['a','b','c','c']) == 'ccab'
assert s.frequencySort(['a']) == 'a'
assert s.frequencySort(['a','A','c','c']) == 'ccaA'
assert s.frequencySort(['c','c','c']) == 'ccc'
assert s.frequencySort([]) == ''
assert s.frequencySort(['','','']) == ''
# V1'
# http://bookshadow.com/weblog/2016/11/02/leetcode-sort-characters-by-frequency/
class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
return ''.join(c * t for c, t in collections.Counter(s).most_common())
# V2
import collections
class Solution(object):
def frequencySort(self, s):
# sort Counter by value
# https://stackoverflow.com/questions/20950650/how-to-sort-counter-by-value-python
s_freq_dict = collections.Counter(s).most_common()
output = ''
for i in range(len(s_freq_dict)):
output = output + (s_freq_dict[i][0]*s_freq_dict[i][1])
return output
# V2'
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
freq = collections.defaultdict(int)
for c in s:
freq[c] += 1
counts = [""] * (len(s)+1)
for c in freq:
counts[freq[c]] += c
result = ""
for count in reversed(range(len(counts)-1)):
for c in counts[count]:
result += c * count
return result
| [((96, 118), 'collections.Counter', 'collections.Counter', (['s'], {}), '(s)\n', (115, 118), False, 'import collections\n'), ((2076, 2104), 'collections.defaultdict', 'collections.defaultdict', (['int'], {}), '(int)\n', (2099, 2104), False, 'import collections\n'), ((771, 793), 'collections.Counter', 'collections.Counter', (['s'], {}), '(s)\n', (790, 793), False, 'import collections\n'), ((1696, 1718), 'collections.Counter', 'collections.Counter', (['s'], {}), '(s)\n', (1715, 1718), False, 'import collections\n'), ((493, 515), 'collections.Counter', 'collections.Counter', (['s'], {}), '(s)\n', (512, 515), False, 'import collections\n'), ((1437, 1459), 'collections.Counter', 'collections.Counter', (['s'], {}), '(s)\n', (1456, 1459), False, 'import collections\n')] |
chateval/chatevalv2 | eval/scripts/human/html_gen.py | 7ba96d81842db00427a6d6351d5cea76a8766450 | """Stores all the helper functions that generate html"""
import random
def generate_2choice_html(example):
'''Makes html for ranking form for the specified row index.
Returns the HTML for a table of radio buttons used for ranking,
as well as a count of the total number of radio buttons.
'''
# Check for duplicates.
if example.target_lines[0] == example.target_lines[1]:
return "", 0
# Find all the non-duplicate target indices.
target_indices = [0, 1]
# Randomize the order targets are shown in.
random.shuffle(target_indices)
num_targets = len(target_indices)
source_html = ''
speaker = 'A'
for utterance in example.source_line_utterances():
source_html += '<h4>Speaker %s: %s</h4>' % (speaker, utterance)
speaker = 'A' if speaker == 'B' else 'B'
html = """
<br/>
<div class="panel panel-default btn-group">
%s
<br/>
<table>
""" % (source_html)
html += """
<tr>
<td>Speaker %s: %s</td>""" % (speaker, example.target_lines[target_indices[0]])
html += """
<td>
<label class="btn">
<input type="radio" class="%s" name="%s-target-%s" data-col="1" value="1"/>
</label>
</td>
</tr>""" % (example.key, example.key, target_indices[0])
html += """
<tr>
<td>Speaker %s: %s</td>""" % (speaker, example.target_lines[target_indices[1]])
html += """
<td>
<label class="btn">
<input type="radio" class="%s" name="%s-target-%s" data-col="1" value="1"/>
</label>
</td>
</tr>""" % (example.key, example.key, target_indices[1])
html += """
<tr>
<td>It's a tie.</td>
<td>
<label class="btn">
<input type="radio" class="%s" name="%s-target-tie" data-col="1" value="1"/>
</label>
</td>
</tr>""" % (example.key, example.key)
html += """
</table>
</div>
"""
return html, 1
def generate_ranking_tables_html(example):
'''Makes html for ranking form for the specified row index.
Returns the HTML for a table of radio buttons used for ranking,
as well as a count of the total number of radio buttons.
'''
# Find all the non-duplicate target indices.
target_indices = []
for idx in range(len(example.target_lines)):
current = example.target_lines[idx]
if current not in example.target_lines[0:idx] or idx == 0:
target_indices.append(idx)
# Randomize the order targets are shown in.
random.shuffle(target_indices)
num_targets = len(target_indices)
html = """
<br/>
<div class="panel panel-default btn-group">
<h4>Speaker A: %s</h4>
<table>
<tr>
<th></th>
""" % example.source_line
for idx in range(num_targets):
if idx == 0:
tag = 'best'
elif idx == num_targets - 1:
tag = 'worst'
else:
tag = ''
html += '<th align="center">%s<br>%s</th>' % (tag, idx+1)
html += "</tr>"
for idx in target_indices:
html += """
<tr>
<td>Speaker B: %s</td>""" % (example.target_lines[idx])
# Add a row of radio buttons whose length is the number of options.
for jdx in range(num_targets):
html += """
<td>
<label class="btn">
<input type="radio" class="%s" name="%s-target-%s" data-col="%s" value="%s"/>
</label>
</td>""" % (example.key, example.key, idx, jdx, jdx)
html += "</tr>"
html += """
</table>
</div>
"""
return html, num_targets
def generate_2choice_instructions():
return """
<p>Consider the following exchange between two speakers.</p>
<p>Your task is to decide which response sounds better given the previous things said.</p>
<p>If both responses are equally good, click "It's a tie."<p>
<p><b>Example:</b><br/>Speaker A: can i get you something from the cafe?</p>
<table>
<tr><td>Speaker B: coffee would be great</td></tr>
<tr><td>Speaker B: I don't know what to say.</td></tr>
</table>
<br/>
<p>In this case, the first response is better as it directly answers Speaker A's question, so you should click the bubble next to it.</p>
<h3>You must click the Submit button when you are finished. You must complete every question before you can click Submit.</h3>
"""
def generate_multuchoice_instructions():
return """
<p>Consider the following Twitter exchanges between Speakers A and B.</p>
<p>Your task is to rank the possible responses by Speaker B from best to worst, where the best response should get the lowest ranking.</p>
<br/>
<p><b>Example:</b><br/>Speaker A: can i get you something from the cafe?</p>
<table>
<tr><td>Speaker B: coffee would be great</td></tr>
<tr><td>Speaker B: can you believe he missed the shot?</td></tr>
<tr><td>Speaker B: I don't know what to say.</td></tr>
</table>
<br/>
<p>In this case, the first response should be given rank 1, the second rank 2, and the third rank 3.</p>
<h3>You must click the Submit button when you are finished. You must complete every question before you can click Submit.</h3>
"""
def generate_HIT_html(num_required, tables_html, instructions):
question_html_value = """
<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
<HTMLContent><![CDATA[
<!DOCTYPE html>
<html>
<head>
<link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" integrity="sha384-IS73LIqjtYesmURkDE9MXKbXqYA8rvKEp/ghicjem7Vc3mGRdQRptJSz60tvrB6+" rel="stylesheet" /><!-- The following snippet enables the 'responsive' behavior on smaller screens -->
<style>
table {
border-collapse: collapse;
display: block;
}
td, th {
border: 1px solid #ccc;
}
th:empty {
border: 0;
}
#collapseTrigger{
color:#fff;
display: block;
text-decoration: none;
}
* {
margin: 0; padding: 0;
}
tr td:nth-child(1) {
padding-left: 10px;
padding-right: 10px;
}
.panel {
padding: 10px
}
</style>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js' type='text/javascript'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function() {
var col, el;
$("input[type=radio]").click(function() {
// Make sure only one radio button is enabled per column.
el = $(this);
col = el.data("col");
cl = el.attr("class");
//if cl.includes("ex-") {
$("input." + cl + "[data-col=" + col + "]").prop("checked", false);
//}
el.prop("checked", true);
console.log("Here!")
// Only enable submit if enough radio buttons are checked.
if ($('input:radio:checked').length >= """ + str(num_required) + """ ) {
$("input[type=submit]").removeAttr("disabled");
} else {
$("input[type=submit]").attr("disabled", "disabled");
}
});
});
$(document).ready(function() {
// Instructions expand/collapse
var content = $('#instructionBody');
var trigger = $('#collapseTrigger');
content.show();
$('.collapse-text').text('(Click to collapse)');
trigger.click(function(){
content.toggle();
var isVisible = content.is(':visible');
if(isVisible){
$('.collapse-text').text('(Click to collapse)');
}else{
$('.collapse-text').text('(Click to expand)');
}
});
// end expand/collapse
});
</script>
<title>Chatbot Evaluation Task</title>
</head>
<body>
<div class="col-xs-12 col-md-12"><!-- Instructions -->
<div class="panel panel-primary">
<!-- WARNING: the ids "collapseTrigger" and "instructionBody" are being used to enable expand/collapse feature -->
<a class="panel-heading" href="javascript:void(0);" id="collapseTrigger"><strong>Rate the Chatbot's Responses</strong> <span class="collapse-text">(Click to expand)</span> </a>
<div class="panel-body" id="instructionBody">
""" + instructions + """
</div>
</div>
</div>
<!-- HTML to handle creating the HIT form -->
<form name='mturk_form' method='post' id='mturk_form' action='https://workersandbox.mturk.com/mturk/externalSubmit'>
<input type='hidden' value='' name='assignmentId' id='assignmentId'/>
<!-- This is where you define your question(s) -->
""" + tables_html + """
<!-- HTML to handle submitting the HIT -->
<p><input type='submit' id='submitButton' value='Submit' /></p></form>
<h4>You must fill out rankings for every question before you can submit.</h4>
<script language='Javascript'>turkSetAssignmentID();</script>
</body>
</html>
]]>
</HTMLContent>
<FrameHeight>600</FrameHeight>
</HTMLQuestion>
"""
return question_html_value | [((535, 565), 'random.shuffle', 'random.shuffle', (['target_indices'], {}), '(target_indices)\n', (549, 565), False, 'import random\n'), ((2496, 2526), 'random.shuffle', 'random.shuffle', (['target_indices'], {}), '(target_indices)\n', (2510, 2526), False, 'import random\n')] |
sdpython/python_project_template | python3_module_template/subproject/myexample.py | e365b29ba9a7dfd2688f68eb7ff2b84a6a82cb57 | # -*- coding: utf-8 -*-
"""
@file
@brief This the documentation of this module (myexampleb).
"""
class myclass:
"""
This is the documentation for this class.
**example with a sphinx directives**
It works everywhere in the documentation.
.. exref::
:title: an example of use
Just for documentation purpose.
::
m = myclass(0)
The old way:
@example(an old example of use)
This only works from the code,
not inserted in a RST file. The source
documentation is parsed and every such example is
collected and placed in a page ``all_examples.rst``
(look at the source).
@code
m = myclass(0)
@endcode
@endexample
**FAQ**
.. faqref::
:title: How to add a question ?
Just look a this section.
Look also :ref:`l-FAQ2`.
.. faqref::
:title: Add a label
:lid: label1
Look also :ref:`l-FAQ2`.
**BLOC**
.. blocref::
:title: How to add a bloc
:tag: aaaa
Just look a this bloc.
Look also :ref:`l-FAQ2`.
An accent, é, to check it is working.
A link to github source: :githublink:`source|py`.
"""
def __init__(self, pa):
"""
documentation for the constructor
@param pa first parameter
"""
self.pa = pa
def get_value(self, mul):
"""
returns the parameter multiplied by a value
@param mul a float
@return a float
"""
return self.pa * mul
| [] |
eperrier/QDataSet | simulation/dataset_G_1q_X_Z_N1.py | 383b38b9b4166848f72fac0153800525e66b477b | ##############################################
"""
This module generate a dataset
"""
##############################################
# preample
import numpy as np
from utilites import Pauli_operators, simulate, CheckNoise
################################################
# meta parameters
name = "G_1q_X_Z_N1"
################################################
# quantum parameters
dim = 2 # dimension of the system
Omega = 12 # qubit energy gap
static_operators = [0.5*Pauli_operators[3]*Omega] # drift Hamiltonian
dynamic_operators = [0.5*Pauli_operators[1]] # control Hamiltonian
noise_operators = [0.5*Pauli_operators[3]] # noise Hamiltonian
initial_states = [
np.array([[0.5,0.5],[0.5,0.5]]), np.array([[0.5,-0.5],[-0.5,0.5]]),
np.array([[0.5,-0.5j],[0.5j,0.5]]),np.array([[0.5,0.5j],[-0.5j,0.5]]),
np.array([[1,0],[0,0]]), np.array([[0,0],[0,1]])
] # intial state of qubit
measurement_operators = Pauli_operators[1:] # measurement operators
##################################################
# simulation parameters
T = 1 # Evolution time
M = 1024 # Number of time steps
num_ex = 10000 # Number of examples
batch_size = 50 # batch size for TF
##################################################
# noise parameters
K = 2000 # Number of realzations
noise_profile = [1] # Noise type
###################################################
# control parameters
pulse_shape = "Gaussian" # Control pulse shape
num_pulses = 5 # Number of pulses per sequence
####################################################
# Generate the dataset
sim_parameters = dict( [(k,eval(k)) for k in ["name", "dim", "Omega", "static_operators", "dynamic_operators", "noise_operators", "measurement_operators", "initial_states", "T", "M", "num_ex", "batch_size", "K", "noise_profile", "pulse_shape", "num_pulses"] ])
CheckNoise(sim_parameters)
simulate(sim_parameters)
#################################################### | [((2428, 2454), 'utilites.CheckNoise', 'CheckNoise', (['sim_parameters'], {}), '(sim_parameters)\n', (2438, 2454), False, 'from utilites import Pauli_operators, simulate, CheckNoise\n'), ((2456, 2480), 'utilites.simulate', 'simulate', (['sim_parameters'], {}), '(sim_parameters)\n', (2464, 2480), False, 'from utilites import Pauli_operators, simulate, CheckNoise\n'), ((829, 863), 'numpy.array', 'np.array', (['[[0.5, 0.5], [0.5, 0.5]]'], {}), '([[0.5, 0.5], [0.5, 0.5]])\n', (837, 863), True, 'import numpy as np\n'), ((862, 898), 'numpy.array', 'np.array', (['[[0.5, -0.5], [-0.5, 0.5]]'], {}), '([[0.5, -0.5], [-0.5, 0.5]])\n', (870, 898), True, 'import numpy as np\n'), ((919, 956), 'numpy.array', 'np.array', (['[[0.5, -0.5j], [0.5j, 0.5]]'], {}), '([[0.5, -0.5j], [0.5j, 0.5]])\n', (927, 956), True, 'import numpy as np\n'), ((954, 991), 'numpy.array', 'np.array', (['[[0.5, 0.5j], [-0.5j, 0.5]]'], {}), '([[0.5, 0.5j], [-0.5j, 0.5]])\n', (962, 991), True, 'import numpy as np\n'), ((1012, 1038), 'numpy.array', 'np.array', (['[[1, 0], [0, 0]]'], {}), '([[1, 0], [0, 0]])\n', (1020, 1038), True, 'import numpy as np\n'), ((1037, 1063), 'numpy.array', 'np.array', (['[[0, 0], [0, 1]]'], {}), '([[0, 0], [0, 1]])\n', (1045, 1063), True, 'import numpy as np\n')] |
zhiqwang/mmdeploy | configs/mmdet/detection/detection_tensorrt_static-300x300.py | 997d111a6f4ca9624ab3b36717748e6ce002037d | _base_ = ['../_base_/base_tensorrt_static-300x300.py']
| [] |
Ziang-Lu/Flask-Blog | user_service/user_service/api.py | 8daf901a0ea0e079ad24a61fd7f16f1298514d4c | # -*- coding: utf-8 -*-
"""
API definition module.
"""
from flask import Blueprint
from flask_restful import Api
from .resources.user import UserAuth, UserItem, UserList, UserFollow
# Create an API-related blueprint
api_bp = Blueprint(name='api', import_name=__name__)
api = Api(api_bp)
api.add_resource(UserList, '/users')
api.add_resource(UserItem, '/users/<int:id>')
api.add_resource(UserAuth, '/user-auth')
api.add_resource(
UserFollow, '/user-follow/<int:follower_id>/<followed_username>'
)
| [((229, 272), 'flask.Blueprint', 'Blueprint', ([], {'name': '"""api"""', 'import_name': '__name__'}), "(name='api', import_name=__name__)\n", (238, 272), False, 'from flask import Blueprint\n'), ((280, 291), 'flask_restful.Api', 'Api', (['api_bp'], {}), '(api_bp)\n', (283, 291), False, 'from flask_restful import Api\n')] |
ariannasg/python3-essential-training | advanced/itertools_funcs.py | 9b52645f5ccb57d2bda5d5f4a3053681a026450a | #!usr/bin/env python3
import itertools
# itertools is a module that's not technically a set of built-in functions but
# it is part of the standard library that comes with python.
# it's useful for for creating and using iterators.
def main():
print('some infinite iterators')
# cycle iterator can be used to cycle over a collection over and over
seq1 = ["Joe", "John", "Mike"]
cycle1 = itertools.cycle(seq1)
print(next(cycle1))
print(next(cycle1))
print(next(cycle1))
print(next(cycle1))
print(next(cycle1))
# use count to create a simple counter
count1 = itertools.count(100, 3)
print(next(count1))
print(next(count1))
print(next(count1))
print('some non-infinite iterators')
values = [10, 5, 20, 30, 40, 50, 40, 30]
# accumulate creates an iterator that accumulates/aggregates values
print(list(itertools.accumulate(values))) # this defaults to addition
print(list(itertools.accumulate(values, max)))
print(list(itertools.accumulate(values, min)))
# use chain to connect sequences together
x = itertools.chain('ABCD', '1234')
print(list(x))
# dropwhile and takewhile will return values until
# a certain condition is met that stops them. they are similar to the
# filter built-in function.
# dropwhile will drop the values from the sequence as long as the
# condition of the function is true and then returns the rest of values
print(list(itertools.dropwhile(is_less_than_forty, values)))
# takewhile will keep the values from the sequence as long as the
# condition of the function is true and then stops giving data
print(list(itertools.takewhile(is_less_than_forty, values)))
def is_less_than_forty(x):
return x < 40
if __name__ == "__main__":
main()
# CONSOLE OUTPUT:
# some infinite iterators
# Joe
# John
# Mike
# Joe
# John
# 100
# 103
# 106
# some non-infinite iterators
# [10, 15, 35, 65, 105, 155, 195, 225]
# [10, 10, 20, 30, 40, 50, 50, 50]
# [10, 5, 5, 5, 5, 5, 5, 5]
# ['A', 'B', 'C', 'D', '1', '2', '3', '4']
# [40, 50, 40, 30]
# [10, 5, 20, 30]
| [((404, 425), 'itertools.cycle', 'itertools.cycle', (['seq1'], {}), '(seq1)\n', (419, 425), False, 'import itertools\n'), ((603, 626), 'itertools.count', 'itertools.count', (['(100)', '(3)'], {}), '(100, 3)\n', (618, 626), False, 'import itertools\n'), ((1091, 1122), 'itertools.chain', 'itertools.chain', (['"""ABCD"""', '"""1234"""'], {}), "('ABCD', '1234')\n", (1106, 1122), False, 'import itertools\n'), ((874, 902), 'itertools.accumulate', 'itertools.accumulate', (['values'], {}), '(values)\n', (894, 902), False, 'import itertools\n'), ((949, 982), 'itertools.accumulate', 'itertools.accumulate', (['values', 'max'], {}), '(values, max)\n', (969, 982), False, 'import itertools\n'), ((1000, 1033), 'itertools.accumulate', 'itertools.accumulate', (['values', 'min'], {}), '(values, min)\n', (1020, 1033), False, 'import itertools\n'), ((1465, 1512), 'itertools.dropwhile', 'itertools.dropwhile', (['is_less_than_forty', 'values'], {}), '(is_less_than_forty, values)\n', (1484, 1512), False, 'import itertools\n'), ((1667, 1714), 'itertools.takewhile', 'itertools.takewhile', (['is_less_than_forty', 'values'], {}), '(is_less_than_forty, values)\n', (1686, 1714), False, 'import itertools\n')] |
Azenha/AlgProg2 | aula 05/model/Pessoa.py | 062b5caac24435717074a18a7499f80130489a46 | class Pessoa:
def __init__(self, codigo, nome, endereco, telefone):
self.__codigo = int(codigo)
self.nome = str(nome)
self._endereco = str(endereco)
self.__telefone = str(telefone)
def imprimeNome(self):
print(f"Você pode chamar essa pessoa de {self.nome}.")
def __imprimeTelefone(self):
print(f"Você pode ligar para esta pessoa no número {self.__telefone}.") | [] |
lukefx/stardust | examples/plain_text_response.py | 4d9e399ffba9d4a47a2f428b59b5abf4c5bd41ad | from starlette.responses import PlainTextResponse
async def serve(req):
return PlainTextResponse("Hello World!")
| [((85, 118), 'starlette.responses.PlainTextResponse', 'PlainTextResponse', (['"""Hello World!"""'], {}), "('Hello World!')\n", (102, 118), False, 'from starlette.responses import PlainTextResponse\n')] |
t3eHawk/pypyrus_logbook | pypyrus_logbook/logger.py | bd647a1c355b07e8df28c0d7298fcfe68cd9572e | import atexit
import datetime as dt
import os
import platform
import pypyrus_logbook as logbook
import sys
import time
import traceback
from .conf import all_loggers
from .formatter import Formatter
from .header import Header
from .output import Root
from .record import Record
from .sysinfo import Sysinfo
class Logger():
"""This class represents a single logger.
Logger by it self is a complex set of methods, items and commands that
together gives funcionality for advanced logging in different outputs:
console, file, email, database table, HTML document - and using information
from diffrent inputs: user messages, traceback, frames, user parameters,
execution arguments and systems descriptors.
Each logger must have an unique name which will help to identify it.
Main application logger will have the same name as a python script file.
It can be accessed by native logbook methods or by calling `getlogger()`
method with no name.
Parameters
----------
name : str, optional
The argument is used te define `name` attribute
app : str, optional
The argument is used to set the `app` attribute.
desc : str, optional
The argument is used to set the `desc` attribute.
version : str, optional
The argument is used to set the `version` attribute.
status : bool, optional
The argument is used to open or close output `root`.
console : bool, optional
The argument is used to open or close output `console`.
file : bool, optional
The argument is used to open or close output `file`.
email : bool, optional
The argument is used to open or close output `email`.
html : bool, optional
The argument is used to open or close output `html`.
table : bool, optional
The argument is used to open or close output `table`.
directory : str, optional
The argument is used to set logging file folder.
filename : str, optional
The argument is used to set logging file name.
extension : str, optional
The argument is used to set logging file extension.
smtp : dict, optional
The argument is used to configure SMTP connection.
db : dict, optional
The argument is used to configure DB connection.
format : str, optional
The argument is used to set record template.
info : bool, optional
The argument is used to filter info records. The default is True.
debug : bool, optional
The argument is used to filter debug records. The default is False.
warning : bool, optional
The argument is used to filter warning records. The default is True.
error : bool, optional
The argument is used to filter error records. The default is True.
critical : bool, optional
The argument is used to filter critical records. The default is True.
alarming : bool, optional
The argument is used to enable or disable alarming mechanism. The
default is True.
control : bool, optional
The argument is used to enable or disable execution break in case
on error. The default is True.
maxsize : int or bool, optional
The argument is used to define maximum size of output file. Must be
presented as number of bytes. The default is 10 Mb.
maxdays : int or bool, optional
The argument is used to define maximum number of days that will be
logged to same file. The default is 1 which means that new output file
will be opened at each 00:00:00.
maxlevel : int or bool, optional
The argument is used to define the break error level (WARNING = 0,
ERRROR = 1, CRITICAL = 2). All that higher the break level will
interrupt application execution. The default is 1.
maxerrors : int or bool, optional
The argument is used to define maximun number of errors. The default
is False which means it is disabled.
Attributes
----------
name : str
Name of the logger.
app : str
Name of the application that we are logging.
desc : str
Description of the application that we are logging.
version : str
Version of the application that we are logging.
start_date : datetime.datetime
Date when logging was started.
rectypes : dict
All available record types. Keys are used in `Logger` write methods as
`rectype` argument. Values are used in formatting. So if you wish to
modify `rectype` form then edit appropriate one here. If you wish to
use own record types then just add it to that dictinary. By default we
provide the next few record types:
+---------+---------+
| Key | Value |
+=========+=========+
|none |NONE |
+---------+---------+
|info |INFO |
+---------+---------+
|debug |DEBUG |
+---------+---------+
|warning |WARNING |
+---------+---------+
|error |ERROR |
+---------+---------+
|critical |CRITICAL |
+---------+---------+
messages : dict
Messages that are printed with some `Logger` methods like `ok()`,
`success()`, `fail()`. If you wish to modify the text of this messages
just edit the value of appropriate item.
with_errors : int
The flag shows that logger catched errors in the application during its
execution.
count_errors : int
Number of errors that logger catched in the application during its
execution.
filters : dict
Record types filters. To filter record type just set corresponding
item value to False.
root : pypyrus_logbook.output.Root
The output `Root` object.
console : pypyrus_logbook.output.Console
The output `Console` object. Shortcut for `Logger.root.console`.
file : pypyrus_logbook.output.File
The output file. Shortcut for `Logger.output.file`.
email : pypyrus_logbook.output.Email
The output email. Shortcut for `Logger.output.email`.
html: pypyrus_logbook.output.HTML
The output HTML document. Shortcut for `Logger.output.html`.
table: pypyrus_logbook.output.Table
The output table. Shortcut for `Logger.output.table`.
formatter : pypyrus_logbook.formatter.Formatter
Logger formatter which sets all formatting configuration like
record template, error message template, line length etc.
sysinfo : pypyrus_logbook.sysinfo.Sysinfo
Special input object which parse different inputs includeing system
specifications, flag arguments, execution parameters, user parameters
and environment variables and transforms all of that to `Dataset`
object. Through the `Dataset` object data can be easily accessed by
get item operation or by point like `sysinfo.desc['hostname']` or
`sysinfo.desc.hostname`.
header : pypyrus_logbook.header.Header
The header that can be printed to the writable output.
"""
def __init__(self, name=None, app=None, desc=None, version=None,
status=True, console=True, file=True, email=False, html=False,
table=False, directory=None, filename=None, extension=None,
smtp=None, db=None, format=None, info=True, debug=False,
warning=True, error=True, critical=True, alarming=True,
control=True, maxsize=(1024*1024*10), maxdays=1, maxlevel=2,
maxerrors=False):
# Unique name of the logger.
self._name = name
# Attributes describing the application.
self.app = None
self.desc = None
self.version = None
# Some logger important attributes
self._start_date = dt.datetime.now()
self.rectypes = {'none': 'NONE', 'info': 'INFO', 'debug': 'DEBUG',
'warning': 'WARNING', 'error': 'ERROR',
'critical': 'CRITICAL'}
self.messages = {'ok': 'OK', 'success': 'SUCCESS', 'fail': 'FAIL'}
self._with_error = False
self._count_errors = 0
# Complete the initial configuration.
self.configure(app=app, desc=desc, version=version, status=status,
console=console, file=file, email=email, html=html,
table=table, directory=directory, filename=filename,
extension=extension, smtp=smtp, db=db, format=format,
info=info, debug=debug, warning=warning, error=error,
critical=critical, alarming=alarming, control=control,
maxsize=maxsize, maxdays=maxdays, maxlevel=maxlevel,
maxerrors=maxerrors)
# Output shortcuts.
self.console = self.root.console
self.file = self.root.file
self.email = self.root.email
self.html = self.root.html
self.table = self.root.table
# Set exit function.
atexit.register(self._exit)
# Add creating logger to special all_loggers dictinary.
all_loggers[self._name] = self
pass
def __str__(self):
return f'<Logger object "{self._name}">'
__repr__ = __str__
@property
def name(self):
"""Unique logger name."""
return self._name
@property
def start_date(self):
"""Logging start date."""
return self._start_date
@property
def with_error(self):
"""Flag that shows was an error or not."""
return self._with_error
@property
def count_errors(self):
"""The number of occured errors."""
return self._count_errors
def configure(self, app=None, desc=None, version=None, status=None,
console=None, file=None, email=None, html=None, table=None,
directory=None, filename=None, extension=None, smtp=None,
db=None, format=None, info=None, debug=None, warning=None,
error=None, critical=None, alarming=None, control=None,
maxsize=None, maxdays=None, maxlevel=None, maxerrors=None):
"""Main method to configure the logger and all its attributes.
This is an only one right way to customize logger. Parameters are the
same as for creatrion.
Parameters
----------
app : str, optional
The argument is used to set the `app` attribute.
desc : str, optional
The argument is used to set the `desc` attribute.
version : str, optional
The argument is used to set the `version` attribute.
status : bool, optional
The argument is used to open or close output `root`.
console : bool, optional
The argument is used to open or close output `console`.
file : bool, optional
The argument is used to open or close output `file`.
email : bool, optional
The argument is used to open or close output `email`.
html : bool, optional
The argument is used to open or close output `html`.
table : bool, optional
The argument is used to open or close output `table`.
directory : str, optional
The argument is used to set logging file folder.
filename : str, optional
The argument is used to set logging file name.
extension : str, optional
The argument is used to set logging file extension.
smtp : dict, optional
The argument is used to configure SMTP connection.
db : dict, optional
The argument is used to configure DB connection.
format : str, optional
The argument is used to set record template.
info : bool, optional
The argument is used to filter info records.
debug : bool, optional
The argument is used to filter debug records.
warning : bool, optional
The argument is used to filter warning records.
error : bool, optional
The argument is used to filter error records.
critical : bool, optional
The argument is used to filter critical records.
alarming : bool, optional
The argument is used to enable or disable alarming mechanism.
control : bool, optional
The argument is used to enable or disable execution break in case
on error.
maxsize : int or bool, optional
The argument is used to define maximum size of output file.
maxdays : int or bool, optional
The argument is used to define maximum number of days that will be
logged to same file.
maxlevel : int or bool, optional
The argument is used to define the break error level.
maxerrors : int or bool, optional
The argument is used to define maximun number of errors.
"""
if isinstance(app, str) is True: self.app = app
if isinstance(desc, str) is True: self.desc = desc
if isinstance(version, (str, int, float)) is True:
self.version = version
# Build the output root if it is not exists. In other case modify
# existing output if it is requested.
if hasattr(self, 'root') is False:
self.root = Root(self, console=console, file=file, email=email,
html=html, table=table, status=status,
directory=directory, filename=filename,
extension=extension, smtp=smtp, db=db)
else:
for key, value in {'console': console, 'file': file,
'email': email, 'html': html,
'table': table}.items():
if value is True:
getattr(self.root, key).open()
if key == 'file':
getattr(self.root, key).new()
elif value is False:
getattr(self.root, key).close()
# Customize output file path.
path = {}
if directory is not None: path['dir'] = directory
if filename is not None: path['name'] = filename
if extension is not None: path['ext'] = extension
if len(path) > 0:
self.root.file.configure(**path)
# Customize SMTP server.
if isinstance(smtp, dict) is True:
self.root.email.configure(**smtp)
# Customize database connection.
if isinstance(db, dict) is True:
self.root.table.configure(**db)
# Create formatter in case it is not exists yet or just customize it.
# Parameter format can be either string or dictionary.
# When it is string then it must describe records format.
# When it is dictionary it can contaion any parameter of formatter
# that must be customized.
if isinstance(format, str) is True:
format = {'record': format}
if hasattr(self, 'formatter') is False:
format = {} if isinstance(format, dict) is False else format
self.formatter = Formatter(**format)
elif isinstance(format, dict) is True:
self.formatter.configure(**format)
# Create or customize record type filters.
if hasattr(self, 'filters') is False:
self.filters = {}
for key, value in {'info': info, 'debug': debug, 'error': error,
'warning': warning, 'critical': critical}.items():
if isinstance(value, bool) is True:
self.filters[key] = value
# Customize limits and parameters of execution behaviour.
if isinstance(maxsize, (int, float, bool)) is True:
self._maxsize = maxsize
if isinstance(maxdays, (int, float, bool)) is True:
self._maxdays = maxdays
self.__calculate_restart_date()
if isinstance(maxlevel, (int, float, bool)) is True:
self._maxlevel = maxlevel
if isinstance(maxerrors, (int, float, bool)) is True:
self._maxerrors = maxerrors
if isinstance(alarming, bool) is True:
self._alarming = alarming
if isinstance(control, bool) is True:
self._control = control
# Initialize sysinfo instance when not exists.
if hasattr(self, 'sysinfo') is False:
self.sysinfo = Sysinfo(self)
# Initialize header instance when not exists.
if hasattr(self, 'header') is False:
self.header = Header(self)
pass
def write(self, record):
"""Direct write to the output.
Parameters
----------
record : Record
The argument is used to send it to the output `root`.
"""
self.__check_file_stats()
self.root.write(record)
pass
def record(self, rectype, message, error=False, **kwargs):
"""Basic method to write records.
Parameters
----------
rectype : str
By default method creates the record with the type NONE.
That can be changed but depends on available record types.
All registered record types are stored in the instance attribute
rectypes. If you wish to use own record type or change the
presentaion of exeisting one then edit this dictinary.
message : str
The message that must be written.
error : bool, optional
If record is error then set that parameter to `True`.
**kwargs
The keyword arguments used for additional forms (variables) for
record and message formatting.
"""
if self.filters.get(rectype, True) is True:
record = Record(self, rectype, message, error=error, **kwargs)
self.write(record)
pass
def info(self, message, **kwargs):
"""Send INFO record to output."""
rectype = 'info'
self.record(rectype, message, **kwargs)
pass
def debug(self, message, **kwargs):
"""Send DEBUG record to the output."""
rectype = 'debug'
self.record(rectype, message, **kwargs)
pass
def error(self, message=None, rectype='error', format=None, alarming=False,
level=1, **kwargs):
"""Send ERROR record to the output.
If exception in current traceback exists then method will format the
exception according to `formatter.error` string presentation. If
`formatter.error` is set to `False` the exception will be just printed
in original Python style.
Also method will send an alarm if alarming attribute is `True`, email
output is enabled and SMTP server is configurated.
If one of the limit triggers worked then application will be aborted.
Parameters
----------
message : str, optional
The message that must be written instead of exception.
rectype : str, optional
The type of error according to `rectypes` dictionary.
format : str, optional
The format of the error message.
alarming : bool
The argument is used to enable or disable the alarming mechanism
for this certain call.
level : int
The argument is used to describe the error level.
**kwargs
The keyword arguments used for additional forms (variables) for
record and message formatting.
"""
self._with_error = True
self._count_errors += 1
format = self.formatter.error if format is None else format
# Parse the error.
err_type, err_value, err_tb = sys.exc_info()
if message is None and err_type is not None:
if isinstance(format, str) is True:
err_name = err_type.__name__
err_value = err_value
for tb in traceback.walk_tb(err_tb):
f_code = tb[0].f_code
err_file = os.path.abspath(f_code.co_filename)
err_line = tb[1]
err_obj = f_code.co_name
self.record(rectype, message, error=True,
err_name=err_name, err_value=err_value,
err_file=err_file, err_line=err_line,
err_obj=err_obj, **kwargs)
elif format is False:
exception = traceback.format_exception(err_type, err_value,
err_tb)
message = '\n'
message += ''.join(exception)
self.record(rectype, message, **kwargs)
else:
message = message or ''
self.record(rectype, message, **kwargs)
# Break execution in case of critical error if permitted.
# The alarm will be generated at exit if it is configured.
if self._control is True:
if level >= self._maxlevel:
sys.exit()
if self._maxerrors is not False:
if self._count_errors > self._maxerrors:
sys.exit()
# Send alarm if execution was not aborted but alarm is needed.
if alarming is True:
self.root.email.alarm()
pass
def warning(self, message=None, **kwargs):
"""Send WARNING error record to the output."""
self.error(message, rectype='warning', level=0, **kwargs)
pass
def critical(self, message=None, **kwargs):
"""Send CRITICAL error record to the output."""
self.error(message, rectype='critical', level=2, **kwargs)
pass
def head(self):
"""Send header to the output."""
string = self.header.create()
self.write(string)
pass
def subhead(self, string):
"""Send subheader as upper-case text between two border lines to the
output.
Parameters
----------
string : str
The text that will be presented as subheader.
"""
bound = f'{self.formatter.div*self.formatter.length}\n'
string = f'{bound}\t{string}\n{bound}'.upper()
self.write(string)
pass
def line(self, message):
"""Send raw text with the new line to the output.
Parameters
----------
message : str
The message that must be written.
"""
self.write(f'{message}\n')
pass
def bound(self, div=None, length=None):
"""Write horizontal border in the output. Useful when need to separate
different blocks of information.
Parameters
----------
div : str, optional
Symbol that is used to bulid the bound.
length : int, optional
Lenght of the bound.
"""
border = self.formatter.div * self.formatter.length
self.write(border + '\n')
pass
def blank(self, number=1):
"""Write blank lines in the output.
Parameters
----------
number : int, optional
The number of the blank lines that must be written.
"""
string = '\n'*number
self.write(string)
pass
def ok(self, **kwargs):
"""Print INFO message with OK."""
rectype = 'info'
message = self.messages['ok']
self.record(rectype, message, **kwargs)
pass
def success(self, **kwargs):
"""Print INFO message with SUCCESS."""
rectype = 'info'
message = self.messages['success']
self.record(rectype, message, **kwargs)
pass
def fail(self, **kwargs):
"""Print INFO message with FAIL."""
rectype = 'info'
message = self.messages['fail']
self.record(rectype, message, **kwargs)
pass
def restart(self):
"""Restart logging. Will open new file."""
self._start_date = dt.datetime.now()
self.__calculate_restart_date()
if self.root.file.status is True:
self.root.file.new()
if self.header.used is True:
self.head()
pass
def send(self, *args, **kwargs):
"""Send email message. Note that SMTP server connection must be
configured.
"""
self.root.email.send(*args, **kwargs)
pass
def set(self, **kwargs):
"""Update values in table. Note that DB connection must be
configured.
"""
self.root.table.write(**kwargs)
pass
def _exit(self):
# Inform about the error.
if self._alarming is True and self._with_error is True:
self.root.email.alarm()
pass
def __calculate_restart_date(self):
"""Calculate the date when logger must be restarted according to
maxdays parameter.
"""
self.__restart_date = (self._start_date
+ dt.timedelta(days=self._maxdays))
pass
def __check_file_stats(self):
"""Check the output file statistics to catch when current file must be
closed and new one must be opened.
"""
if self.root.file.status is True:
if self._maxsize is not False:
if self.root.file.size is not None:
if self.root.file.size > self._maxsize:
self.restart()
return
if self._maxdays is not False:
if self.__restart_date.day == dt.datetime.now().day:
self.restart()
return
| [((7866, 7883), 'datetime.datetime.now', 'dt.datetime.now', ([], {}), '()\n', (7881, 7883), True, 'import datetime as dt\n'), ((9089, 9116), 'atexit.register', 'atexit.register', (['self._exit'], {}), '(self._exit)\n', (9104, 9116), False, 'import atexit\n'), ((19925, 19939), 'sys.exc_info', 'sys.exc_info', ([], {}), '()\n', (19937, 19939), False, 'import sys\n'), ((24202, 24219), 'datetime.datetime.now', 'dt.datetime.now', ([], {}), '()\n', (24217, 24219), True, 'import datetime as dt\n'), ((25195, 25227), 'datetime.timedelta', 'dt.timedelta', ([], {'days': 'self._maxdays'}), '(days=self._maxdays)\n', (25207, 25227), True, 'import datetime as dt\n'), ((20151, 20176), 'traceback.walk_tb', 'traceback.walk_tb', (['err_tb'], {}), '(err_tb)\n', (20168, 20176), False, 'import traceback\n'), ((21266, 21276), 'sys.exit', 'sys.exit', ([], {}), '()\n', (21274, 21276), False, 'import sys\n'), ((20252, 20287), 'os.path.abspath', 'os.path.abspath', (['f_code.co_filename'], {}), '(f_code.co_filename)\n', (20267, 20287), False, 'import os\n'), ((20696, 20751), 'traceback.format_exception', 'traceback.format_exception', (['err_type', 'err_value', 'err_tb'], {}), '(err_type, err_value, err_tb)\n', (20722, 20751), False, 'import traceback\n'), ((21399, 21409), 'sys.exit', 'sys.exit', ([], {}), '()\n', (21407, 21409), False, 'import sys\n'), ((25767, 25784), 'datetime.datetime.now', 'dt.datetime.now', ([], {}), '()\n', (25782, 25784), True, 'import datetime as dt\n')] |
revnav/sandbox | darling_ansible/python_venv/lib/python3.7/site-packages/oci/object_storage/transfer/constants.py | f9c8422233d093b76821686b6c249417502cf61d | # coding: utf-8
# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
MEBIBYTE = 1024 * 1024
STREAMING_DEFAULT_PART_SIZE = 10 * MEBIBYTE
DEFAULT_PART_SIZE = 128 * MEBIBYTE
OBJECT_USE_MULTIPART_SIZE = 128 * MEBIBYTE
| [] |
ZendaInnocent/news-api | scraper/news/spiders/millardayo.py | 71465aea50e0b1cea08a421d72156cbe7ed8a952 | # Spider for MillardAyo.com
import scrapy
from bs4 import BeautifulSoup
class MillardAyoSpider(scrapy.Spider):
name = 'millardayo'
allowed_urls = ['www.millardayo.com']
start_urls = [
'https://millardayo.com',
]
def parse(self, response, **kwargs):
# extracting data - link, image, title, excerpt
soup = BeautifulSoup(response.body, 'lxml')
posts = soup.find_all('li', {'class': 'infinite-post'})
for post in posts:
try:
yield {
'image_url': post.find('img').get('src'),
'link': post.find('a').get('href'),
'title': post.find('a').get('title'),
'excerpt': post.find('p').get_text(),
'source': 'millardayo',
}
except AttributeError:
pass
next_page = soup.find('a', text='Next ›').get('href')
if next_page:
yield response.follow(next_page, callback=self.parse)
| [((353, 389), 'bs4.BeautifulSoup', 'BeautifulSoup', (['response.body', '"""lxml"""'], {}), "(response.body, 'lxml')\n", (366, 389), False, 'from bs4 import BeautifulSoup\n')] |
stephenoken/beam | sdks/python/apache_beam/runners/portability/expansion_service_test.py | 4797f310b6671de6fd703502520f4b012b655c82 | #
# 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.
#
# pytype: skip-file
from __future__ import absolute_import
import argparse
import logging
import signal
import sys
import typing
import grpc
from past.builtins import unicode
import apache_beam as beam
import apache_beam.transforms.combiners as combine
from apache_beam.coders import StrUtf8Coder
from apache_beam.pipeline import PipelineOptions
from apache_beam.portability.api import beam_expansion_api_pb2_grpc
from apache_beam.portability.api.external_transforms_pb2 import ExternalConfigurationPayload
from apache_beam.runners.portability import expansion_service
from apache_beam.transforms import ptransform
from apache_beam.transforms.external import ImplicitSchemaPayloadBuilder
from apache_beam.utils.thread_pool_executor import UnboundedThreadPoolExecutor
# This script provides an expansion service and example ptransforms for running
# external transform test cases. See external_test.py for details.
_LOGGER = logging.getLogger(__name__)
TEST_PREFIX_URN = "beam:transforms:xlang:test:prefix"
TEST_MULTI_URN = "beam:transforms:xlang:test:multi"
TEST_GBK_URN = "beam:transforms:xlang:test:gbk"
TEST_CGBK_URN = "beam:transforms:xlang:test:cgbk"
TEST_COMGL_URN = "beam:transforms:xlang:test:comgl"
TEST_COMPK_URN = "beam:transforms:xlang:test:compk"
TEST_FLATTEN_URN = "beam:transforms:xlang:test:flatten"
TEST_PARTITION_URN = "beam:transforms:xlang:test:partition"
@ptransform.PTransform.register_urn('beam:transforms:xlang:count', None)
class CountPerElementTransform(ptransform.PTransform):
def expand(self, pcoll):
return pcoll | combine.Count.PerElement()
def to_runner_api_parameter(self, unused_context):
return 'beam:transforms:xlang:count', None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return CountPerElementTransform()
@ptransform.PTransform.register_urn(
'beam:transforms:xlang:filter_less_than_eq', bytes)
class FilterLessThanTransform(ptransform.PTransform):
def __init__(self, payload):
self._payload = payload
def expand(self, pcoll):
return (
pcoll | beam.Filter(
lambda elem, target: elem <= target, int(ord(self._payload[0]))))
def to_runner_api_parameter(self, unused_context):
return (
'beam:transforms:xlang:filter_less_than', self._payload.encode('utf8'))
@staticmethod
def from_runner_api_parameter(unused_ptransform, payload, unused_context):
return FilterLessThanTransform(payload.decode('utf8'))
@ptransform.PTransform.register_urn(TEST_PREFIX_URN, None)
@beam.typehints.with_output_types(unicode)
class PrefixTransform(ptransform.PTransform):
def __init__(self, payload):
self._payload = payload
def expand(self, pcoll):
return pcoll | 'TestLabel' >> beam.Map(
lambda x: '{}{}'.format(self._payload, x))
def to_runner_api_parameter(self, unused_context):
return TEST_PREFIX_URN, ImplicitSchemaPayloadBuilder(
{'data': self._payload}).payload()
@staticmethod
def from_runner_api_parameter(unused_ptransform, payload, unused_context):
return PrefixTransform(parse_string_payload(payload)['data'])
@ptransform.PTransform.register_urn(TEST_MULTI_URN, None)
class MutltiTransform(ptransform.PTransform):
def expand(self, pcolls):
return {
'main': (pcolls['main1'], pcolls['main2'])
| beam.Flatten()
| beam.Map(lambda x, s: x + s, beam.pvalue.AsSingleton(
pcolls['side'])).with_output_types(unicode),
'side': pcolls['side']
| beam.Map(lambda x: x + x).with_output_types(unicode),
}
def to_runner_api_parameter(self, unused_context):
return TEST_MULTI_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return MutltiTransform()
@ptransform.PTransform.register_urn(TEST_GBK_URN, None)
class GBKTransform(ptransform.PTransform):
def expand(self, pcoll):
return pcoll | 'TestLabel' >> beam.GroupByKey()
def to_runner_api_parameter(self, unused_context):
return TEST_GBK_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return GBKTransform()
@ptransform.PTransform.register_urn(TEST_CGBK_URN, None)
class CoGBKTransform(ptransform.PTransform):
class ConcatFn(beam.DoFn):
def process(self, element):
(k, v) = element
return [(k, v['col1'] + v['col2'])]
def expand(self, pcoll):
return pcoll \
| beam.CoGroupByKey() \
| beam.ParDo(self.ConcatFn()).with_output_types(
typing.Tuple[int, typing.Iterable[unicode]])
def to_runner_api_parameter(self, unused_context):
return TEST_CGBK_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return CoGBKTransform()
@ptransform.PTransform.register_urn(TEST_COMGL_URN, None)
class CombineGloballyTransform(ptransform.PTransform):
def expand(self, pcoll):
return pcoll \
| beam.CombineGlobally(sum).with_output_types(int)
def to_runner_api_parameter(self, unused_context):
return TEST_COMGL_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return CombineGloballyTransform()
@ptransform.PTransform.register_urn(TEST_COMPK_URN, None)
class CombinePerKeyTransform(ptransform.PTransform):
def expand(self, pcoll):
return pcoll \
| beam.CombinePerKey(sum).with_output_types(
typing.Tuple[unicode, int])
def to_runner_api_parameter(self, unused_context):
return TEST_COMPK_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return CombinePerKeyTransform()
@ptransform.PTransform.register_urn(TEST_FLATTEN_URN, None)
class FlattenTransform(ptransform.PTransform):
def expand(self, pcoll):
return pcoll.values() | beam.Flatten().with_output_types(int)
def to_runner_api_parameter(self, unused_context):
return TEST_FLATTEN_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return FlattenTransform()
@ptransform.PTransform.register_urn(TEST_PARTITION_URN, None)
class PartitionTransform(ptransform.PTransform):
def expand(self, pcoll):
col1, col2 = pcoll | beam.Partition(
lambda elem, n: 0 if elem % 2 == 0 else 1, 2)
typed_col1 = col1 | beam.Map(lambda x: x).with_output_types(int)
typed_col2 = col2 | beam.Map(lambda x: x).with_output_types(int)
return {'0': typed_col1, '1': typed_col2}
def to_runner_api_parameter(self, unused_context):
return TEST_PARTITION_URN, None
@staticmethod
def from_runner_api_parameter(
unused_ptransform, unused_parameter, unused_context):
return PartitionTransform()
@ptransform.PTransform.register_urn('payload', bytes)
class PayloadTransform(ptransform.PTransform):
def __init__(self, payload):
self._payload = payload
def expand(self, pcoll):
return pcoll | beam.Map(lambda x, s: x + s, self._payload)
def to_runner_api_parameter(self, unused_context):
return b'payload', self._payload.encode('ascii')
@staticmethod
def from_runner_api_parameter(unused_ptransform, payload, unused_context):
return PayloadTransform(payload.decode('ascii'))
@ptransform.PTransform.register_urn('fib', bytes)
class FibTransform(ptransform.PTransform):
def __init__(self, level):
self._level = level
def expand(self, p):
if self._level <= 2:
return p | beam.Create([1])
else:
a = p | 'A' >> beam.ExternalTransform(
'fib',
str(self._level - 1).encode('ascii'),
expansion_service.ExpansionServiceServicer())
b = p | 'B' >> beam.ExternalTransform(
'fib',
str(self._level - 2).encode('ascii'),
expansion_service.ExpansionServiceServicer())
return ((a, b)
| beam.Flatten()
| beam.CombineGlobally(sum).without_defaults())
def to_runner_api_parameter(self, unused_context):
return 'fib', str(self._level).encode('ascii')
@staticmethod
def from_runner_api_parameter(unused_ptransform, level, unused_context):
return FibTransform(int(level.decode('ascii')))
def parse_string_payload(input_byte):
payload = ExternalConfigurationPayload()
payload.ParseFromString(input_byte)
coder = StrUtf8Coder()
return {
k: coder.decode_nested(v.payload)
for k,
v in payload.configuration.items()
}
server = None
def cleanup(unused_signum, unused_frame):
_LOGGER.info('Shutting down expansion service.')
server.stop(None)
def main(unused_argv):
parser = argparse.ArgumentParser()
parser.add_argument(
'-p', '--port', type=int, help='port on which to serve the job api')
options = parser.parse_args()
global server
server = grpc.server(UnboundedThreadPoolExecutor())
beam_expansion_api_pb2_grpc.add_ExpansionServiceServicer_to_server(
expansion_service.ExpansionServiceServicer(
PipelineOptions(
["--experiments", "beam_fn_api", "--sdk_location", "container"])),
server)
server.add_insecure_port('localhost:{}'.format(options.port))
server.start()
_LOGGER.info('Listening for expansion requests at %d', options.port)
signal.signal(signal.SIGTERM, cleanup)
signal.signal(signal.SIGINT, cleanup)
# blocking main thread forever.
signal.pause()
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
main(sys.argv)
| [((1714, 1741), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (1731, 1741), False, 'import logging\n'), ((2170, 2241), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['"""beam:transforms:xlang:count"""', 'None'], {}), "('beam:transforms:xlang:count', None)\n", (2204, 2241), False, 'from apache_beam.transforms import ptransform\n'), ((2622, 2712), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['"""beam:transforms:xlang:filter_less_than_eq"""', 'bytes'], {}), "('beam:transforms:xlang:filter_less_than_eq',\n bytes)\n", (2656, 2712), False, 'from apache_beam.transforms import ptransform\n'), ((3278, 3335), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_PREFIX_URN', 'None'], {}), '(TEST_PREFIX_URN, None)\n', (3312, 3335), False, 'from apache_beam.transforms import ptransform\n'), ((3337, 3378), 'apache_beam.typehints.with_output_types', 'beam.typehints.with_output_types', (['unicode'], {}), '(unicode)\n', (3369, 3378), True, 'import apache_beam as beam\n'), ((3925, 3981), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_MULTI_URN', 'None'], {}), '(TEST_MULTI_URN, None)\n', (3959, 3981), False, 'from apache_beam.transforms import ptransform\n'), ((4595, 4649), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_GBK_URN', 'None'], {}), '(TEST_GBK_URN, None)\n', (4629, 4649), False, 'from apache_beam.transforms import ptransform\n'), ((4995, 5050), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_CGBK_URN', 'None'], {}), '(TEST_CGBK_URN, None)\n', (5029, 5050), False, 'from apache_beam.transforms import ptransform\n'), ((5650, 5706), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_COMGL_URN', 'None'], {}), '(TEST_COMGL_URN, None)\n', (5684, 5706), False, 'from apache_beam.transforms import ptransform\n'), ((6107, 6163), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_COMPK_URN', 'None'], {}), '(TEST_COMPK_URN, None)\n', (6141, 6163), False, 'from apache_beam.transforms import ptransform\n'), ((6597, 6655), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_FLATTEN_URN', 'None'], {}), '(TEST_FLATTEN_URN, None)\n', (6631, 6655), False, 'from apache_beam.transforms import ptransform\n'), ((7027, 7087), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['TEST_PARTITION_URN', 'None'], {}), '(TEST_PARTITION_URN, None)\n', (7061, 7087), False, 'from apache_beam.transforms import ptransform\n'), ((7678, 7730), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['"""payload"""', 'bytes'], {}), "('payload', bytes)\n", (7712, 7730), False, 'from apache_beam.transforms import ptransform\n'), ((8185, 8233), 'apache_beam.transforms.ptransform.PTransform.register_urn', 'ptransform.PTransform.register_urn', (['"""fib"""', 'bytes'], {}), "('fib', bytes)\n", (8219, 8233), False, 'from apache_beam.transforms import ptransform\n'), ((9170, 9200), 'apache_beam.portability.api.external_transforms_pb2.ExternalConfigurationPayload', 'ExternalConfigurationPayload', ([], {}), '()\n', (9198, 9200), False, 'from apache_beam.portability.api.external_transforms_pb2 import ExternalConfigurationPayload\n'), ((9249, 9263), 'apache_beam.coders.StrUtf8Coder', 'StrUtf8Coder', ([], {}), '()\n', (9261, 9263), False, 'from apache_beam.coders import StrUtf8Coder\n'), ((9540, 9565), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (9563, 9565), False, 'import argparse\n'), ((10163, 10201), 'signal.signal', 'signal.signal', (['signal.SIGTERM', 'cleanup'], {}), '(signal.SIGTERM, cleanup)\n', (10176, 10201), False, 'import signal\n'), ((10204, 10241), 'signal.signal', 'signal.signal', (['signal.SIGINT', 'cleanup'], {}), '(signal.SIGINT, cleanup)\n', (10217, 10241), False, 'import signal\n'), ((10278, 10292), 'signal.pause', 'signal.pause', ([], {}), '()\n', (10290, 10292), False, 'import signal\n'), ((9735, 9764), 'apache_beam.utils.thread_pool_executor.UnboundedThreadPoolExecutor', 'UnboundedThreadPoolExecutor', ([], {}), '()\n', (9762, 9764), False, 'from apache_beam.utils.thread_pool_executor import UnboundedThreadPoolExecutor\n'), ((2343, 2369), 'apache_beam.transforms.combiners.Count.PerElement', 'combine.Count.PerElement', ([], {}), '()\n', (2367, 2369), True, 'import apache_beam.transforms.combiners as combine\n'), ((7189, 7249), 'apache_beam.Partition', 'beam.Partition', (['(lambda elem, n: 0 if elem % 2 == 0 else 1)', '(2)'], {}), '(lambda elem, n: 0 if elem % 2 == 0 else 1, 2)\n', (7203, 7249), True, 'import apache_beam as beam\n'), ((7884, 7927), 'apache_beam.Map', 'beam.Map', (['(lambda x, s: x + s)', 'self._payload'], {}), '(lambda x, s: x + s, self._payload)\n', (7892, 7927), True, 'import apache_beam as beam\n'), ((9896, 9981), 'apache_beam.pipeline.PipelineOptions', 'PipelineOptions', (["['--experiments', 'beam_fn_api', '--sdk_location', 'container']"], {}), "(['--experiments', 'beam_fn_api', '--sdk_location', 'container']\n )\n", (9911, 9981), False, 'from apache_beam.pipeline import PipelineOptions\n'), ((10324, 10343), 'logging.getLogger', 'logging.getLogger', ([], {}), '()\n', (10341, 10343), False, 'import logging\n'), ((4754, 4771), 'apache_beam.GroupByKey', 'beam.GroupByKey', ([], {}), '()\n', (4769, 4771), True, 'import apache_beam as beam\n'), ((5282, 5301), 'apache_beam.CoGroupByKey', 'beam.CoGroupByKey', ([], {}), '()\n', (5299, 5301), True, 'import apache_beam as beam\n'), ((8396, 8412), 'apache_beam.Create', 'beam.Create', (['[1]'], {}), '([1])\n', (8407, 8412), True, 'import apache_beam as beam\n'), ((3689, 3742), 'apache_beam.transforms.external.ImplicitSchemaPayloadBuilder', 'ImplicitSchemaPayloadBuilder', (["{'data': self._payload}"], {}), "({'data': self._payload})\n", (3717, 3742), False, 'from apache_beam.transforms.external import ImplicitSchemaPayloadBuilder\n'), ((4130, 4144), 'apache_beam.Flatten', 'beam.Flatten', ([], {}), '()\n', (4142, 4144), True, 'import apache_beam as beam\n'), ((5821, 5846), 'apache_beam.CombineGlobally', 'beam.CombineGlobally', (['sum'], {}), '(sum)\n', (5841, 5846), True, 'import apache_beam as beam\n'), ((6276, 6299), 'apache_beam.CombinePerKey', 'beam.CombinePerKey', (['sum'], {}), '(sum)\n', (6294, 6299), True, 'import apache_beam as beam\n'), ((6758, 6772), 'apache_beam.Flatten', 'beam.Flatten', ([], {}), '()\n', (6770, 6772), True, 'import apache_beam as beam\n'), ((7283, 7304), 'apache_beam.Map', 'beam.Map', (['(lambda x: x)'], {}), '(lambda x: x)\n', (7291, 7304), True, 'import apache_beam as beam\n'), ((7352, 7373), 'apache_beam.Map', 'beam.Map', (['(lambda x: x)'], {}), '(lambda x: x)\n', (7360, 7373), True, 'import apache_beam as beam\n'), ((8792, 8806), 'apache_beam.Flatten', 'beam.Flatten', ([], {}), '()\n', (8804, 8806), True, 'import apache_beam as beam\n'), ((4307, 4332), 'apache_beam.Map', 'beam.Map', (['(lambda x: x + x)'], {}), '(lambda x: x + x)\n', (4315, 4332), True, 'import apache_beam as beam\n'), ((8543, 8587), 'apache_beam.runners.portability.expansion_service.ExpansionServiceServicer', 'expansion_service.ExpansionServiceServicer', ([], {}), '()\n', (8585, 8587), False, 'from apache_beam.runners.portability import expansion_service\n'), ((8709, 8753), 'apache_beam.runners.portability.expansion_service.ExpansionServiceServicer', 'expansion_service.ExpansionServiceServicer', ([], {}), '()\n', (8751, 8753), False, 'from apache_beam.runners.portability import expansion_service\n'), ((8823, 8848), 'apache_beam.CombineGlobally', 'beam.CombineGlobally', (['sum'], {}), '(sum)\n', (8843, 8848), True, 'import apache_beam as beam\n'), ((4184, 4223), 'apache_beam.pvalue.AsSingleton', 'beam.pvalue.AsSingleton', (["pcolls['side']"], {}), "(pcolls['side'])\n", (4207, 4223), True, 'import apache_beam as beam\n')] |
RunnerPro/RunnerProApi | db.py | 2e0aba17cba2a019b6d102bc4eac2fd60f164156 | from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from settings import DB_URI
Session = sessionmaker(autocommit=False, autoflush=False, bind=create_engine(DB_URI))
session = scoped_session(Session)
| [((244, 267), 'sqlalchemy.orm.scoped_session', 'scoped_session', (['Session'], {}), '(Session)\n', (258, 267), False, 'from sqlalchemy.orm import scoped_session\n'), ((211, 232), 'sqlalchemy.create_engine', 'create_engine', (['DB_URI'], {}), '(DB_URI)\n', (224, 232), False, 'from sqlalchemy import create_engine\n')] |
ntanhbk44/tvm | python/tvm/contrib/nvcc.py | f89a929f09f7a0b0ccd0f4d46dc2b1c562839087 | # 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=invalid-name
"""Utility to invoke nvcc compiler in the system"""
from __future__ import absolute_import as _abs
import subprocess
import os
import warnings
import tvm._ffi
from tvm.runtime import ndarray as nd
from . import utils
from .._ffi.base import py_str
def compile_cuda(code, target="ptx", arch=None, options=None, path_target=None):
"""Compile cuda code with NVCC from env.
Parameters
----------
code : str
The cuda code.
target : str
The target format
arch : str
The architecture
options : str or list of str
The additional options
path_target : str, optional
Output file.
Return
------
cubin : bytearray
The bytearray of the cubin
"""
temp = utils.tempdir()
if target not in ["cubin", "ptx", "fatbin"]:
raise ValueError("target must be in cubin, ptx, fatbin")
temp_code = temp.relpath("my_kernel.cu")
temp_target = temp.relpath("my_kernel.%s" % target)
with open(temp_code, "w") as out_file:
out_file.write(code)
if arch is None:
if nd.gpu(0).exist:
# auto detect the compute arch argument
arch = "sm_" + "".join(nd.gpu(0).compute_version.split("."))
else:
raise ValueError("arch(sm_xy) is not passed, and we cannot detect it from env")
file_target = path_target if path_target else temp_target
cmd = ["nvcc"]
cmd += ["--%s" % target, "-O3"]
if isinstance(arch, list):
cmd += arch
else:
cmd += ["-arch", arch]
if options:
if isinstance(options, str):
cmd += [options]
elif isinstance(options, list):
cmd += options
else:
raise ValueError("options must be str or list of str")
cmd += ["-o", file_target]
cmd += [temp_code]
# NOTE: ccbin option can be used to tell nvcc where to find the c++ compiler
# just in case it is not in the path. On Windows it is not in the path by default.
# However, we cannot use TVM_CXX_COMPILER_PATH because the runtime env.
# Because it is hard to do runtime compiler detection, we require nvcc is configured
# correctly by default.
# if cxx_compiler_path != "":
# cmd += ["-ccbin", cxx_compiler_path]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(out, _) = proc.communicate()
if proc.returncode != 0:
msg = code
msg += "\nCompilation error:\n"
msg += py_str(out)
raise RuntimeError(msg)
data = bytearray(open(file_target, "rb").read())
if not data:
raise RuntimeError("Compilation error: empty result is generated")
return data
def find_cuda_path():
"""Utility function to find cuda path
Returns
-------
path : str
Path to cuda root.
"""
if "CUDA_PATH" in os.environ:
return os.environ["CUDA_PATH"]
cmd = ["which", "nvcc"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(out, _) = proc.communicate()
out = py_str(out)
if proc.returncode == 0:
return os.path.realpath(os.path.join(str(out).strip(), "../.."))
cuda_path = "/usr/local/cuda"
if os.path.exists(os.path.join(cuda_path, "bin/nvcc")):
return cuda_path
raise RuntimeError("Cannot find cuda path")
def get_cuda_version(cuda_path):
"""Utility function to get cuda version
Parameters
----------
cuda_path : str
Path to cuda root.
Returns
-------
version : float
The cuda version
"""
version_file_path = os.path.join(cuda_path, "version.txt")
if not os.path.exists(version_file_path):
# Debian/Ubuntu repackaged CUDA path
version_file_path = os.path.join(cuda_path, "lib", "cuda", "version.txt")
try:
with open(version_file_path) as f:
version_str = f.readline().replace("\n", "").replace("\r", "")
return float(version_str.split(" ")[2][:2])
except FileNotFoundError:
pass
cmd = [os.path.join(cuda_path, "bin", "nvcc"), "--version"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(out, _) = proc.communicate()
out = py_str(out)
if proc.returncode == 0:
release_line = [l for l in out.split("\n") if "release" in l][0]
release_fields = [s.strip() for s in release_line.split(",")]
release_version = [f[1:] for f in release_fields if f.startswith("V")][0]
major_minor = ".".join(release_version.split(".")[:2])
return float(major_minor)
raise RuntimeError("Cannot read cuda version file")
@tvm._ffi.register_func("tvm_callback_libdevice_path")
def find_libdevice_path(arch):
"""Utility function to find libdevice
Parameters
----------
arch : int
The compute architecture in int
Returns
-------
path : str
Path to libdevice.
"""
cuda_path = find_cuda_path()
lib_path = os.path.join(cuda_path, "nvvm/libdevice")
if not os.path.exists(lib_path):
# Debian/Ubuntu repackaged CUDA path
lib_path = os.path.join(cuda_path, "lib/nvidia-cuda-toolkit/libdevice")
selected_ver = 0
selected_path = None
cuda_ver = get_cuda_version(cuda_path)
if cuda_ver in (9.0, 9.1, 10.0, 10.1, 10.2, 11.0, 11.1, 11.2):
path = os.path.join(lib_path, "libdevice.10.bc")
else:
for fn in os.listdir(lib_path):
if not fn.startswith("libdevice"):
continue
ver = int(fn.split(".")[-3].split("_")[-1])
if selected_ver < ver <= arch:
selected_ver = ver
selected_path = fn
if selected_path is None:
raise RuntimeError("Cannot find libdevice for arch {}".format(arch))
path = os.path.join(lib_path, selected_path)
return path
def callback_libdevice_path(arch):
try:
return find_libdevice_path(arch)
except RuntimeError:
warnings.warn("Cannot find libdevice path")
return ""
def get_target_compute_version(target=None):
"""Utility function to get compute capability of compilation target.
Looks for the arch in three different places, first in the target attributes, then the global
scope, and finally the GPU device (if it exists).
Parameters
----------
target : tvm.target.Target, optional
The compilation target
Returns
-------
compute_version : str
compute capability of a GPU (e.g. "8.0")
"""
# 1. Target
if target:
if "arch" in target.attrs:
compute_version = target.attrs["arch"]
major, minor = compute_version.split("_")[1]
return major + "." + minor
# 2. Global scope
from tvm.autotvm.env import AutotvmGlobalScope # pylint: disable=import-outside-toplevel
if AutotvmGlobalScope.current.cuda_target_arch:
major, minor = AutotvmGlobalScope.current.cuda_target_arch.split("_")[1]
return major + "." + minor
# 3. GPU
if tvm.gpu(0).exist:
return tvm.gpu(0).compute_version
warnings.warn(
"No CUDA architecture was specified or GPU detected."
"Try specifying it by adding '-arch=sm_xx' to your target."
)
return None
def parse_compute_version(compute_version):
"""Parse compute capability string to divide major and minor version
Parameters
----------
compute_version : str
compute capability of a GPU (e.g. "6.0")
Returns
-------
major : int
major version number
minor : int
minor version number
"""
split_ver = compute_version.split(".")
try:
major = int(split_ver[0])
minor = int(split_ver[1])
return major, minor
except (IndexError, ValueError) as err:
# pylint: disable=raise-missing-from
raise RuntimeError("Compute version parsing error: " + str(err))
def have_fp16(compute_version):
"""Either fp16 support is provided in the compute capability or not
Parameters
----------
compute_version: str
compute capability of a GPU (e.g. "6.0")
"""
major, minor = parse_compute_version(compute_version)
# fp 16 support in reference to:
# https://docs.nvidia.com/cuda/cuda-c-programming-guide/#arithmetic-instructions
if major == 5 and minor == 3:
return True
if major >= 6:
return True
return False
def have_int8(compute_version):
"""Either int8 support is provided in the compute capability or not
Parameters
----------
compute_version : str
compute capability of a GPU (e.g. "6.1")
"""
major, _ = parse_compute_version(compute_version)
if major >= 6:
return True
return False
def have_tensorcore(compute_version=None, target=None):
"""Either TensorCore support is provided in the compute capability or not
Parameters
----------
compute_version : str, optional
compute capability of a GPU (e.g. "7.0").
target : tvm.target.Target, optional
The compilation target, will be used to determine arch if compute_version
isn't specified.
"""
if compute_version is None:
if tvm.gpu(0).exist:
compute_version = tvm.gpu(0).compute_version
else:
if target is None or "arch" not in target.attrs:
warnings.warn(
"Tensorcore will be disabled due to no CUDA architecture specified."
"Try specifying it by adding '-arch=sm_xx' to your target."
)
return False
compute_version = target.attrs["arch"]
# Compute version will be in the form "sm_{major}{minor}"
major, minor = compute_version.split("_")[1]
compute_version = major + "." + minor
major, _ = parse_compute_version(compute_version)
if major >= 7:
return True
return False
def have_cudagraph():
"""Either CUDA Graph support is provided"""
try:
cuda_path = find_cuda_path()
cuda_ver = get_cuda_version(cuda_path)
if cuda_ver < 10.0:
return False
return True
except RuntimeError:
return False
def have_bf16(compute_version):
"""Either bf16 support is provided in the compute capability or not
Parameters
----------
compute_version : str
compute capability of a GPU (e.g. "8.0")
"""
major, _ = parse_compute_version(compute_version)
if major >= 8:
return True
return False
| [((3102, 3173), 'subprocess.Popen', 'subprocess.Popen', (['cmd'], {'stdout': 'subprocess.PIPE', 'stderr': 'subprocess.STDOUT'}), '(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n', (3118, 3173), False, 'import subprocess\n'), ((3772, 3843), 'subprocess.Popen', 'subprocess.Popen', (['cmd'], {'stdout': 'subprocess.PIPE', 'stderr': 'subprocess.STDOUT'}), '(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n', (3788, 3843), False, 'import subprocess\n'), ((4428, 4466), 'os.path.join', 'os.path.join', (['cuda_path', '"""version.txt"""'], {}), "(cuda_path, 'version.txt')\n", (4440, 4466), False, 'import os\n'), ((4942, 5013), 'subprocess.Popen', 'subprocess.Popen', (['cmd'], {'stdout': 'subprocess.PIPE', 'stderr': 'subprocess.STDOUT'}), '(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n', (4958, 5013), False, 'import subprocess\n'), ((5816, 5857), 'os.path.join', 'os.path.join', (['cuda_path', '"""nvvm/libdevice"""'], {}), "(cuda_path, 'nvvm/libdevice')\n", (5828, 5857), False, 'import os\n'), ((7959, 8094), 'warnings.warn', 'warnings.warn', (['"""No CUDA architecture was specified or GPU detected.Try specifying it by adding \'-arch=sm_xx\' to your target."""'], {}), '(\n "No CUDA architecture was specified or GPU detected.Try specifying it by adding \'-arch=sm_xx\' to your target."\n )\n', (7972, 8094), False, 'import warnings\n'), ((4058, 4093), 'os.path.join', 'os.path.join', (['cuda_path', '"""bin/nvcc"""'], {}), "(cuda_path, 'bin/nvcc')\n", (4070, 4093), False, 'import os\n'), ((4478, 4511), 'os.path.exists', 'os.path.exists', (['version_file_path'], {}), '(version_file_path)\n', (4492, 4511), False, 'import os\n'), ((4586, 4639), 'os.path.join', 'os.path.join', (['cuda_path', '"""lib"""', '"""cuda"""', '"""version.txt"""'], {}), "(cuda_path, 'lib', 'cuda', 'version.txt')\n", (4598, 4639), False, 'import os\n'), ((4878, 4916), 'os.path.join', 'os.path.join', (['cuda_path', '"""bin"""', '"""nvcc"""'], {}), "(cuda_path, 'bin', 'nvcc')\n", (4890, 4916), False, 'import os\n'), ((5869, 5893), 'os.path.exists', 'os.path.exists', (['lib_path'], {}), '(lib_path)\n', (5883, 5893), False, 'import os\n'), ((5959, 6019), 'os.path.join', 'os.path.join', (['cuda_path', '"""lib/nvidia-cuda-toolkit/libdevice"""'], {}), "(cuda_path, 'lib/nvidia-cuda-toolkit/libdevice')\n", (5971, 6019), False, 'import os\n'), ((6191, 6232), 'os.path.join', 'os.path.join', (['lib_path', '"""libdevice.10.bc"""'], {}), "(lib_path, 'libdevice.10.bc')\n", (6203, 6232), False, 'import os\n'), ((6261, 6281), 'os.listdir', 'os.listdir', (['lib_path'], {}), '(lib_path)\n', (6271, 6281), False, 'import os\n'), ((6654, 6691), 'os.path.join', 'os.path.join', (['lib_path', 'selected_path'], {}), '(lib_path, selected_path)\n', (6666, 6691), False, 'import os\n'), ((1904, 1913), 'tvm.runtime.ndarray.gpu', 'nd.gpu', (['(0)'], {}), '(0)\n', (1910, 1913), True, 'from tvm.runtime import ndarray as nd\n'), ((6828, 6871), 'warnings.warn', 'warnings.warn', (['"""Cannot find libdevice path"""'], {}), "('Cannot find libdevice path')\n", (6841, 6871), False, 'import warnings\n'), ((7780, 7834), 'tvm.autotvm.env.AutotvmGlobalScope.current.cuda_target_arch.split', 'AutotvmGlobalScope.current.cuda_target_arch.split', (['"""_"""'], {}), "('_')\n", (7829, 7834), False, 'from tvm.autotvm.env import AutotvmGlobalScope\n'), ((10244, 10394), 'warnings.warn', 'warnings.warn', (['"""Tensorcore will be disabled due to no CUDA architecture specified.Try specifying it by adding \'-arch=sm_xx\' to your target."""'], {}), '(\n "Tensorcore will be disabled due to no CUDA architecture specified.Try specifying it by adding \'-arch=sm_xx\' to your target."\n )\n', (10257, 10394), False, 'import warnings\n'), ((2008, 2017), 'tvm.runtime.ndarray.gpu', 'nd.gpu', (['(0)'], {}), '(0)\n', (2014, 2017), True, 'from tvm.runtime import ndarray as nd\n')] |
dhruvshah1996/Project3 | calc/history/calculations.py | d87ad37f6cf2de0d3402c71d21b25258946aad69 | """Calculation history Class"""
from calc.calculations.addition import Addition
from calc.calculations.subtraction import Subtraction
from calc.calculations.multiplication import Multiplication
from calc.calculations.division import Division
class Calculations:
"""Calculations class manages the history of calculations"""
history = []
# pylint: disable=too-few-public-methods
@staticmethod
def clear_history():
"""clear the history of calculations"""
Calculations.history.clear()
return True
@staticmethod
def count_history():
"""get number of items in history"""
return len(Calculations.history)
@staticmethod
def get_last_calculation_object():
"""get last calculation"""
return Calculations.history[-1]
@staticmethod
def get_last_calculation_result_value():
"""get last calculation"""
calculation = Calculations.get_last_calculation_object()
return calculation.get_result()
@staticmethod
def get_first_calculation():
"""get first calculation"""
return Calculations.history[0]
@staticmethod
def get_calculation(num):
""" get a specific calculation from history"""
return Calculations.history[num]
@staticmethod
def add_calculation(calculation):
""" get a generic calculation from history"""
return Calculations.history.append(calculation)
@staticmethod
def add_addition_calculation_to_history(values):
"""create an addition and add object to history using factory method create"""
Calculations.add_calculation(Addition.create(values))
#Get the result of the calculation
return True
@staticmethod
def add_subtraction_calculation_to_history(values):
"""create a subtraction object to history using factory method create"""
Calculations.add_calculation(Subtraction.create(values))
return True
@staticmethod
def add_multiplication_calculation_to_history(values):
"""Add a multiplication object to history using factory method create"""
Calculations.add_calculation(Multiplication.create(values))
return True
@staticmethod
def add_division_calculation_to_history(values):
"Add a division object to history using factory method create"
Calculations.add_calculation(Division.create(values))
return True | [((1632, 1655), 'calc.calculations.addition.Addition.create', 'Addition.create', (['values'], {}), '(values)\n', (1647, 1655), False, 'from calc.calculations.addition import Addition\n'), ((1912, 1938), 'calc.calculations.subtraction.Subtraction.create', 'Subtraction.create', (['values'], {}), '(values)\n', (1930, 1938), False, 'from calc.calculations.subtraction import Subtraction\n'), ((2155, 2184), 'calc.calculations.multiplication.Multiplication.create', 'Multiplication.create', (['values'], {}), '(values)\n', (2176, 2184), False, 'from calc.calculations.multiplication import Multiplication\n'), ((2385, 2408), 'calc.calculations.division.Division.create', 'Division.create', (['values'], {}), '(values)\n', (2400, 2408), False, 'from calc.calculations.division import Division\n')] |
matheusguerreiro/python | Python/17 - 081 - extraindo dados de uma lista.py | f39a1b92409f11cbe7fef5d9261f863f9e0fac0d | # Aula 17 (Listas (Parte 1))
valores = []
while True:
valor = int(input('Digite um Valor ou -1 para Finalizar: '))
if valor < 0:
print('\nFinalizando...')
break
else:
valores.append(valor)
print(f'Foram digitados {len(valores)} números')
valores.sort(reverse=True)
print(f'Lista ordenada de forma decrescente: {valores}')
if 5 in valores:
valores.reverse()
print(f'O valor 5 foi digitado e está na {valores.index(5)} posição.')
else:
print('Valor 5 não encontrado na lista.')
| [] |
maryam-afzp/django-yekpay | yekpay/migrations/0014_auto_20181120_1453.py | f7b9d7914035ea4f27238eba9e0c70227cc65046 | # Generated by Django 2.0.9 on 2018-11-20 11:23
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('yekpay', '0013_auto_20181030_1911'),
]
operations = [
migrations.RenameField(
model_name='transaction',
old_name='authorityStart',
new_name='authority_start',
),
migrations.RenameField(
model_name='transaction',
old_name='authorityVerify',
new_name='authority_verify',
),
migrations.RenameField(
model_name='transaction',
old_name='failureReason',
new_name='failure_reason',
),
migrations.RenameField(
model_name='transaction',
old_name='firstName',
new_name='first_name',
),
migrations.RenameField(
model_name='transaction',
old_name='fromCurrencyCode',
new_name='from_currency_code',
),
migrations.RenameField(
model_name='transaction',
old_name='lastName',
new_name='last_name',
),
migrations.RenameField(
model_name='transaction',
old_name='orderNumber',
new_name='order_number',
),
migrations.RenameField(
model_name='transaction',
old_name='postalCode',
new_name='postal_code',
),
migrations.RenameField(
model_name='transaction',
old_name='toCurrencyCode',
new_name='to_currency_code',
),
migrations.AddField(
model_name='transaction',
name='simulation',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='transaction',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
| [((227, 284), 'django.db.migrations.swappable_dependency', 'migrations.swappable_dependency', (['settings.AUTH_USER_MODEL'], {}), '(settings.AUTH_USER_MODEL)\n', (258, 284), False, 'from django.db import migrations, models\n'), ((367, 474), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""authorityStart"""', 'new_name': '"""authority_start"""'}), "(model_name='transaction', old_name='authorityStart',\n new_name='authority_start')\n", (389, 474), False, 'from django.db import migrations, models\n'), ((527, 636), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""authorityVerify"""', 'new_name': '"""authority_verify"""'}), "(model_name='transaction', old_name='authorityVerify',\n new_name='authority_verify')\n", (549, 636), False, 'from django.db import migrations, models\n'), ((689, 794), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""failureReason"""', 'new_name': '"""failure_reason"""'}), "(model_name='transaction', old_name='failureReason',\n new_name='failure_reason')\n", (711, 794), False, 'from django.db import migrations, models\n'), ((847, 944), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""firstName"""', 'new_name': '"""first_name"""'}), "(model_name='transaction', old_name='firstName',\n new_name='first_name')\n", (869, 944), False, 'from django.db import migrations, models\n'), ((997, 1110), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""fromCurrencyCode"""', 'new_name': '"""from_currency_code"""'}), "(model_name='transaction', old_name=\n 'fromCurrencyCode', new_name='from_currency_code')\n", (1019, 1110), False, 'from django.db import migrations, models\n'), ((1162, 1257), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""lastName"""', 'new_name': '"""last_name"""'}), "(model_name='transaction', old_name='lastName',\n new_name='last_name')\n", (1184, 1257), False, 'from django.db import migrations, models\n'), ((1310, 1411), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""orderNumber"""', 'new_name': '"""order_number"""'}), "(model_name='transaction', old_name='orderNumber',\n new_name='order_number')\n", (1332, 1411), False, 'from django.db import migrations, models\n'), ((1464, 1563), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""postalCode"""', 'new_name': '"""postal_code"""'}), "(model_name='transaction', old_name='postalCode',\n new_name='postal_code')\n", (1486, 1563), False, 'from django.db import migrations, models\n'), ((1616, 1724), 'django.db.migrations.RenameField', 'migrations.RenameField', ([], {'model_name': '"""transaction"""', 'old_name': '"""toCurrencyCode"""', 'new_name': '"""to_currency_code"""'}), "(model_name='transaction', old_name='toCurrencyCode',\n new_name='to_currency_code')\n", (1638, 1724), False, 'from django.db import migrations, models\n'), ((1885, 1919), 'django.db.models.BooleanField', 'models.BooleanField', ([], {'default': '(False)'}), '(default=False)\n', (1904, 1919), False, 'from django.db import migrations, models\n'), ((2042, 2148), 'django.db.models.ForeignKey', 'models.ForeignKey', ([], {'null': '(True)', 'on_delete': 'django.db.models.deletion.CASCADE', 'to': 'settings.AUTH_USER_MODEL'}), '(null=True, on_delete=django.db.models.deletion.CASCADE,\n to=settings.AUTH_USER_MODEL)\n', (2059, 2148), False, 'from django.db import migrations, models\n')] |
nishaq503/polus-plugins-dl | polus-cell-nuclei-segmentation/src/dsb2018_topcoders/albu/src/pytorch_zoo/inplace_abn/modules/__init__.py | 511689e82eb29a84761538144277d1be1af7aa44 | from .bn import ABN, InPlaceABN, InPlaceABNWrapper, InPlaceABNSync, InPlaceABNSyncWrapper
from .misc import GlobalAvgPool2d
from .residual import IdentityResidualBlock
from .dense import DenseModule
| [] |
kylebrandt/arrow | python/pyarrow/tests/test_compute.py | 515197dfe6e83d6fa6fe82bfec134f41b222b748 | # 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.
import numpy as np
import pytest
import pyarrow as pa
import pyarrow.compute
all_array_types = [
('bool', [True, False, False, True, True]),
('uint8', np.arange(5)),
('int8', np.arange(5)),
('uint16', np.arange(5)),
('int16', np.arange(5)),
('uint32', np.arange(5)),
('int32', np.arange(5)),
('uint64', np.arange(5, 10)),
('int64', np.arange(5, 10)),
('float', np.arange(0, 0.5, 0.1)),
('double', np.arange(0, 0.5, 0.1)),
('string', ['a', 'b', None, 'ddd', 'ee']),
('binary', [b'a', b'b', b'c', b'ddd', b'ee']),
(pa.binary(3), [b'abc', b'bcd', b'cde', b'def', b'efg']),
(pa.list_(pa.int8()), [[1, 2], [3, 4], [5, 6], None, [9, 16]]),
(pa.large_list(pa.int16()), [[1], [2, 3, 4], [5, 6], None, [9, 16]]),
(pa.struct([('a', pa.int8()), ('b', pa.int8())]), [
{'a': 1, 'b': 2}, None, {'a': 3, 'b': 4}, None, {'a': 5, 'b': 6}]),
]
numerical_arrow_types = [
pa.int8(),
pa.int16(),
pa.int64(),
pa.uint8(),
pa.uint16(),
pa.uint64(),
pa.float32(),
pa.float64()
]
@pytest.mark.parametrize('arrow_type', numerical_arrow_types)
def test_sum_array(arrow_type):
arr = pa.array([1, 2, 3, 4], type=arrow_type)
assert arr.sum() == 10
assert pa.compute.sum(arr) == 10
arr = pa.array([], type=arrow_type)
assert arr.sum() == None # noqa: E711
assert pa.compute.sum(arr) == None # noqa: E711
@pytest.mark.parametrize('arrow_type', numerical_arrow_types)
def test_sum_chunked_array(arrow_type):
arr = pa.chunked_array([pa.array([1, 2, 3, 4], type=arrow_type)])
assert pa.compute.sum(arr) == 10
arr = pa.chunked_array([
pa.array([1, 2], type=arrow_type), pa.array([3, 4], type=arrow_type)
])
assert pa.compute.sum(arr) == 10
arr = pa.chunked_array([
pa.array([1, 2], type=arrow_type),
pa.array([], type=arrow_type),
pa.array([3, 4], type=arrow_type)
])
assert pa.compute.sum(arr) == 10
arr = pa.chunked_array((), type=arrow_type)
print(arr, type(arr))
assert arr.num_chunks == 0
assert pa.compute.sum(arr) == None # noqa: E711
@pytest.mark.parametrize(('ty', 'values'), all_array_types)
def test_take(ty, values):
arr = pa.array(values, type=ty)
for indices_type in [pa.int8(), pa.int64()]:
indices = pa.array([0, 4, 2, None], type=indices_type)
result = arr.take(indices)
result.validate()
expected = pa.array([values[0], values[4], values[2], None], type=ty)
assert result.equals(expected)
# empty indices
indices = pa.array([], type=indices_type)
result = arr.take(indices)
result.validate()
expected = pa.array([], type=ty)
assert result.equals(expected)
indices = pa.array([2, 5])
with pytest.raises(IndexError):
arr.take(indices)
indices = pa.array([2, -1])
with pytest.raises(IndexError):
arr.take(indices)
def test_take_indices_types():
arr = pa.array(range(5))
for indices_type in ['uint8', 'int8', 'uint16', 'int16',
'uint32', 'int32', 'uint64', 'int64']:
indices = pa.array([0, 4, 2, None], type=indices_type)
result = arr.take(indices)
result.validate()
expected = pa.array([0, 4, 2, None])
assert result.equals(expected)
for indices_type in [pa.float32(), pa.float64()]:
indices = pa.array([0, 4, 2], type=indices_type)
with pytest.raises(NotImplementedError):
arr.take(indices)
@pytest.mark.parametrize('ordered', [False, True])
def test_take_dictionary(ordered):
arr = pa.DictionaryArray.from_arrays([0, 1, 2, 0, 1, 2], ['a', 'b', 'c'],
ordered=ordered)
result = arr.take(pa.array([0, 1, 3]))
result.validate()
assert result.to_pylist() == ['a', 'b', 'a']
assert result.dictionary.to_pylist() == ['a', 'b', 'c']
assert result.type.ordered is ordered
@pytest.mark.parametrize(('ty', 'values'), all_array_types)
def test_filter(ty, values):
arr = pa.array(values, type=ty)
mask = pa.array([True, False, False, True, None])
result = arr.filter(mask, null_selection_behavior='drop')
result.validate()
assert result.equals(pa.array([values[0], values[3]], type=ty))
result = arr.filter(mask, null_selection_behavior='emit_null')
result.validate()
assert result.equals(pa.array([values[0], values[3], None], type=ty))
# non-boolean dtype
mask = pa.array([0, 1, 0, 1, 0])
with pytest.raises(NotImplementedError):
arr.filter(mask)
# wrong length
mask = pa.array([True, False, True])
with pytest.raises(ValueError, match="must all be the same length"):
arr.filter(mask)
def test_filter_chunked_array():
arr = pa.chunked_array([["a", None], ["c", "d", "e"]])
expected_drop = pa.chunked_array([["a"], ["e"]])
expected_null = pa.chunked_array([["a"], [None, "e"]])
for mask in [
# mask is array
pa.array([True, False, None, False, True]),
# mask is chunked array
pa.chunked_array([[True, False, None], [False, True]]),
# mask is python object
[True, False, None, False, True]
]:
result = arr.filter(mask)
assert result.equals(expected_drop)
result = arr.filter(mask, null_selection_behavior="emit_null")
assert result.equals(expected_null)
def test_filter_record_batch():
batch = pa.record_batch(
[pa.array(["a", None, "c", "d", "e"])], names=["a'"])
# mask is array
mask = pa.array([True, False, None, False, True])
result = batch.filter(mask)
expected = pa.record_batch([pa.array(["a", "e"])], names=["a'"])
assert result.equals(expected)
result = batch.filter(mask, null_selection_behavior="emit_null")
expected = pa.record_batch([pa.array(["a", None, "e"])], names=["a'"])
assert result.equals(expected)
def test_filter_table():
table = pa.table([pa.array(["a", None, "c", "d", "e"])], names=["a"])
expected_drop = pa.table([pa.array(["a", "e"])], names=["a"])
expected_null = pa.table([pa.array(["a", None, "e"])], names=["a"])
for mask in [
# mask is array
pa.array([True, False, None, False, True]),
# mask is chunked array
pa.chunked_array([[True, False], [None, False, True]]),
# mask is python object
[True, False, None, False, True]
]:
result = table.filter(mask)
assert result.equals(expected_drop)
result = table.filter(mask, null_selection_behavior="emit_null")
assert result.equals(expected_null)
def test_filter_errors():
arr = pa.chunked_array([["a", None], ["c", "d", "e"]])
batch = pa.record_batch(
[pa.array(["a", None, "c", "d", "e"])], names=["a'"])
table = pa.table([pa.array(["a", None, "c", "d", "e"])], names=["a"])
for obj in [arr, batch, table]:
# non-boolean dtype
mask = pa.array([0, 1, 0, 1, 0])
with pytest.raises(NotImplementedError):
obj.filter(mask)
# wrong length
mask = pa.array([True, False, True])
with pytest.raises(pa.ArrowInvalid,
match="must all be the same length"):
obj.filter(mask)
@pytest.mark.parametrize("typ", ["array", "chunked_array"])
def test_compare_array(typ):
if typ == "array":
def con(values): return pa.array(values)
else:
def con(values): return pa.chunked_array([values])
arr1 = con([1, 2, 3, 4, None])
arr2 = con([1, 1, 4, None, 4])
result = arr1 == arr2
assert result.equals(con([True, False, False, None, None]))
result = arr1 != arr2
assert result.equals(con([False, True, True, None, None]))
result = arr1 < arr2
assert result.equals(con([False, False, True, None, None]))
result = arr1 <= arr2
assert result.equals(con([True, False, True, None, None]))
result = arr1 > arr2
assert result.equals(con([False, True, False, None, None]))
result = arr1 >= arr2
assert result.equals(con([True, True, False, None, None]))
@pytest.mark.parametrize("typ", ["array", "chunked_array"])
def test_compare_scalar(typ):
if typ == "array":
def con(values): return pa.array(values)
else:
def con(values): return pa.chunked_array([values])
arr = con([1, 2, 3, None])
# TODO this is a hacky way to construct a scalar ..
scalar = pa.array([2]).sum()
result = arr == scalar
assert result.equals(con([False, True, False, None]))
result = arr != scalar
assert result.equals(con([True, False, True, None]))
result = arr < scalar
assert result.equals(con([True, False, False, None]))
result = arr <= scalar
assert result.equals(con([True, True, False, None]))
result = arr > scalar
assert result.equals(con([False, False, True, None]))
result = arr >= scalar
assert result.equals(con([False, True, True, None]))
def test_compare_chunked_array_mixed():
arr = pa.array([1, 2, 3, 4, None])
arr_chunked = pa.chunked_array([[1, 2, 3], [4, None]])
arr_chunked2 = pa.chunked_array([[1, 2], [3, 4, None]])
expected = pa.chunked_array([[True, True, True, True, None]])
for result in [
arr == arr_chunked,
arr_chunked == arr,
arr_chunked == arr_chunked2,
]:
assert result.equals(expected)
def test_arithmetic_add():
left = pa.array([1, 2, 3, 4, 5])
right = pa.array([0, -1, 1, 2, 3])
result = pa.compute.add(left, right)
expected = pa.array([1, 1, 4, 6, 8])
assert result.equals(expected)
def test_arithmetic_subtract():
left = pa.array([1, 2, 3, 4, 5])
right = pa.array([0, -1, 1, 2, 3])
result = pa.compute.subtract(left, right)
expected = pa.array([1, 3, 2, 2, 2])
assert result.equals(expected)
def test_arithmetic_multiply():
left = pa.array([1, 2, 3, 4, 5])
right = pa.array([0, -1, 1, 2, 3])
result = pa.compute.multiply(left, right)
expected = pa.array([0, -2, 3, 8, 15])
assert result.equals(expected)
| [((1853, 1913), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""arrow_type"""', 'numerical_arrow_types'], {}), "('arrow_type', numerical_arrow_types)\n", (1876, 1913), False, 'import pytest\n'), ((2200, 2260), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""arrow_type"""', 'numerical_arrow_types'], {}), "('arrow_type', numerical_arrow_types)\n", (2223, 2260), False, 'import pytest\n'), ((2919, 2977), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (["('ty', 'values')", 'all_array_types'], {}), "(('ty', 'values'), all_array_types)\n", (2942, 2977), False, 'import pytest\n'), ((4326, 4375), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""ordered"""', '[False, True]'], {}), "('ordered', [False, True])\n", (4349, 4375), False, 'import pytest\n'), ((4766, 4824), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (["('ty', 'values')", 'all_array_types'], {}), "(('ty', 'values'), all_array_types)\n", (4789, 4824), False, 'import pytest\n'), ((8090, 8148), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""typ"""', "['array', 'chunked_array']"], {}), "('typ', ['array', 'chunked_array'])\n", (8113, 8148), False, 'import pytest\n'), ((8934, 8992), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""typ"""', "['array', 'chunked_array']"], {}), "('typ', ['array', 'chunked_array'])\n", (8957, 8992), False, 'import pytest\n'), ((1720, 1729), 'pyarrow.int8', 'pa.int8', ([], {}), '()\n', (1727, 1729), True, 'import pyarrow as pa\n'), ((1735, 1745), 'pyarrow.int16', 'pa.int16', ([], {}), '()\n', (1743, 1745), True, 'import pyarrow as pa\n'), ((1751, 1761), 'pyarrow.int64', 'pa.int64', ([], {}), '()\n', (1759, 1761), True, 'import pyarrow as pa\n'), ((1767, 1777), 'pyarrow.uint8', 'pa.uint8', ([], {}), '()\n', (1775, 1777), True, 'import pyarrow as pa\n'), ((1783, 1794), 'pyarrow.uint16', 'pa.uint16', ([], {}), '()\n', (1792, 1794), True, 'import pyarrow as pa\n'), ((1800, 1811), 'pyarrow.uint64', 'pa.uint64', ([], {}), '()\n', (1809, 1811), True, 'import pyarrow as pa\n'), ((1817, 1829), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (1827, 1829), True, 'import pyarrow as pa\n'), ((1835, 1847), 'pyarrow.float64', 'pa.float64', ([], {}), '()\n', (1845, 1847), True, 'import pyarrow as pa\n'), ((1956, 1995), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4]'], {'type': 'arrow_type'}), '([1, 2, 3, 4], type=arrow_type)\n', (1964, 1995), True, 'import pyarrow as pa\n'), ((2071, 2100), 'pyarrow.array', 'pa.array', (['[]'], {'type': 'arrow_type'}), '([], type=arrow_type)\n', (2079, 2100), True, 'import pyarrow as pa\n'), ((2768, 2805), 'pyarrow.chunked_array', 'pa.chunked_array', (['()'], {'type': 'arrow_type'}), '((), type=arrow_type)\n', (2784, 2805), True, 'import pyarrow as pa\n'), ((3015, 3040), 'pyarrow.array', 'pa.array', (['values'], {'type': 'ty'}), '(values, type=ty)\n', (3023, 3040), True, 'import pyarrow as pa\n'), ((3562, 3578), 'pyarrow.array', 'pa.array', (['[2, 5]'], {}), '([2, 5])\n', (3570, 3578), True, 'import pyarrow as pa\n'), ((3656, 3673), 'pyarrow.array', 'pa.array', (['[2, -1]'], {}), '([2, -1])\n', (3664, 3673), True, 'import pyarrow as pa\n'), ((4421, 4510), 'pyarrow.DictionaryArray.from_arrays', 'pa.DictionaryArray.from_arrays', (['[0, 1, 2, 0, 1, 2]', "['a', 'b', 'c']"], {'ordered': 'ordered'}), "([0, 1, 2, 0, 1, 2], ['a', 'b', 'c'], ordered\n =ordered)\n", (4451, 4510), True, 'import pyarrow as pa\n'), ((4864, 4889), 'pyarrow.array', 'pa.array', (['values'], {'type': 'ty'}), '(values, type=ty)\n', (4872, 4889), True, 'import pyarrow as pa\n'), ((4902, 4944), 'pyarrow.array', 'pa.array', (['[True, False, False, True, None]'], {}), '([True, False, False, True, None])\n', (4910, 4944), True, 'import pyarrow as pa\n'), ((5296, 5321), 'pyarrow.array', 'pa.array', (['[0, 1, 0, 1, 0]'], {}), '([0, 1, 0, 1, 0])\n', (5304, 5321), True, 'import pyarrow as pa\n'), ((5423, 5452), 'pyarrow.array', 'pa.array', (['[True, False, True]'], {}), '([True, False, True])\n', (5431, 5452), True, 'import pyarrow as pa\n'), ((5596, 5644), 'pyarrow.chunked_array', 'pa.chunked_array', (["[['a', None], ['c', 'd', 'e']]"], {}), "([['a', None], ['c', 'd', 'e']])\n", (5612, 5644), True, 'import pyarrow as pa\n'), ((5665, 5697), 'pyarrow.chunked_array', 'pa.chunked_array', (["[['a'], ['e']]"], {}), "([['a'], ['e']])\n", (5681, 5697), True, 'import pyarrow as pa\n'), ((5718, 5756), 'pyarrow.chunked_array', 'pa.chunked_array', (["[['a'], [None, 'e']]"], {}), "([['a'], [None, 'e']])\n", (5734, 5756), True, 'import pyarrow as pa\n'), ((6378, 6420), 'pyarrow.array', 'pa.array', (['[True, False, None, False, True]'], {}), '([True, False, None, False, True])\n', (6386, 6420), True, 'import pyarrow as pa\n'), ((7482, 7530), 'pyarrow.chunked_array', 'pa.chunked_array', (["[['a', None], ['c', 'd', 'e']]"], {}), "([['a', None], ['c', 'd', 'e']])\n", (7498, 7530), True, 'import pyarrow as pa\n'), ((9848, 9876), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4, None]'], {}), '([1, 2, 3, 4, None])\n', (9856, 9876), True, 'import pyarrow as pa\n'), ((9895, 9935), 'pyarrow.chunked_array', 'pa.chunked_array', (['[[1, 2, 3], [4, None]]'], {}), '([[1, 2, 3], [4, None]])\n', (9911, 9935), True, 'import pyarrow as pa\n'), ((9955, 9995), 'pyarrow.chunked_array', 'pa.chunked_array', (['[[1, 2], [3, 4, None]]'], {}), '([[1, 2], [3, 4, None]])\n', (9971, 9995), True, 'import pyarrow as pa\n'), ((10012, 10062), 'pyarrow.chunked_array', 'pa.chunked_array', (['[[True, True, True, True, None]]'], {}), '([[True, True, True, True, None]])\n', (10028, 10062), True, 'import pyarrow as pa\n'), ((10263, 10288), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4, 5]'], {}), '([1, 2, 3, 4, 5])\n', (10271, 10288), True, 'import pyarrow as pa\n'), ((10301, 10327), 'pyarrow.array', 'pa.array', (['[0, -1, 1, 2, 3]'], {}), '([0, -1, 1, 2, 3])\n', (10309, 10327), True, 'import pyarrow as pa\n'), ((10341, 10368), 'pyarrow.compute.add', 'pa.compute.add', (['left', 'right'], {}), '(left, right)\n', (10355, 10368), True, 'import pyarrow as pa\n'), ((10384, 10409), 'pyarrow.array', 'pa.array', (['[1, 1, 4, 6, 8]'], {}), '([1, 1, 4, 6, 8])\n', (10392, 10409), True, 'import pyarrow as pa\n'), ((10490, 10515), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4, 5]'], {}), '([1, 2, 3, 4, 5])\n', (10498, 10515), True, 'import pyarrow as pa\n'), ((10528, 10554), 'pyarrow.array', 'pa.array', (['[0, -1, 1, 2, 3]'], {}), '([0, -1, 1, 2, 3])\n', (10536, 10554), True, 'import pyarrow as pa\n'), ((10568, 10600), 'pyarrow.compute.subtract', 'pa.compute.subtract', (['left', 'right'], {}), '(left, right)\n', (10587, 10600), True, 'import pyarrow as pa\n'), ((10616, 10641), 'pyarrow.array', 'pa.array', (['[1, 3, 2, 2, 2]'], {}), '([1, 3, 2, 2, 2])\n', (10624, 10641), True, 'import pyarrow as pa\n'), ((10722, 10747), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4, 5]'], {}), '([1, 2, 3, 4, 5])\n', (10730, 10747), True, 'import pyarrow as pa\n'), ((10760, 10786), 'pyarrow.array', 'pa.array', (['[0, -1, 1, 2, 3]'], {}), '([0, -1, 1, 2, 3])\n', (10768, 10786), True, 'import pyarrow as pa\n'), ((10800, 10832), 'pyarrow.compute.multiply', 'pa.compute.multiply', (['left', 'right'], {}), '(left, right)\n', (10819, 10832), True, 'import pyarrow as pa\n'), ((10848, 10875), 'pyarrow.array', 'pa.array', (['[0, -2, 3, 8, 15]'], {}), '([0, -2, 3, 8, 15])\n', (10856, 10875), True, 'import pyarrow as pa\n'), ((948, 960), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (957, 960), True, 'import numpy as np\n'), ((976, 988), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (985, 988), True, 'import numpy as np\n'), ((1006, 1018), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (1015, 1018), True, 'import numpy as np\n'), ((1035, 1047), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (1044, 1047), True, 'import numpy as np\n'), ((1065, 1077), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (1074, 1077), True, 'import numpy as np\n'), ((1094, 1106), 'numpy.arange', 'np.arange', (['(5)'], {}), '(5)\n', (1103, 1106), True, 'import numpy as np\n'), ((1124, 1140), 'numpy.arange', 'np.arange', (['(5)', '(10)'], {}), '(5, 10)\n', (1133, 1140), True, 'import numpy as np\n'), ((1157, 1173), 'numpy.arange', 'np.arange', (['(5)', '(10)'], {}), '(5, 10)\n', (1166, 1173), True, 'import numpy as np\n'), ((1190, 1212), 'numpy.arange', 'np.arange', (['(0)', '(0.5)', '(0.1)'], {}), '(0, 0.5, 0.1)\n', (1199, 1212), True, 'import numpy as np\n'), ((1230, 1252), 'numpy.arange', 'np.arange', (['(0)', '(0.5)', '(0.1)'], {}), '(0, 0.5, 0.1)\n', (1239, 1252), True, 'import numpy as np\n'), ((1358, 1370), 'pyarrow.binary', 'pa.binary', (['(3)'], {}), '(3)\n', (1367, 1370), True, 'import pyarrow as pa\n'), ((2034, 2053), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2048, 2053), True, 'import pyarrow as pa\n'), ((2155, 2174), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2169, 2174), True, 'import pyarrow as pa\n'), ((2382, 2401), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2396, 2401), True, 'import pyarrow as pa\n'), ((2533, 2552), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2547, 2552), True, 'import pyarrow as pa\n'), ((2731, 2750), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2745, 2750), True, 'import pyarrow as pa\n'), ((2874, 2893), 'pyarrow.compute.sum', 'pa.compute.sum', (['arr'], {}), '(arr)\n', (2888, 2893), True, 'import pyarrow as pa\n'), ((3066, 3075), 'pyarrow.int8', 'pa.int8', ([], {}), '()\n', (3073, 3075), True, 'import pyarrow as pa\n'), ((3077, 3087), 'pyarrow.int64', 'pa.int64', ([], {}), '()\n', (3085, 3087), True, 'import pyarrow as pa\n'), ((3108, 3152), 'pyarrow.array', 'pa.array', (['[0, 4, 2, None]'], {'type': 'indices_type'}), '([0, 4, 2, None], type=indices_type)\n', (3116, 3152), True, 'import pyarrow as pa\n'), ((3233, 3291), 'pyarrow.array', 'pa.array', (['[values[0], values[4], values[2], None]'], {'type': 'ty'}), '([values[0], values[4], values[2], None], type=ty)\n', (3241, 3291), True, 'import pyarrow as pa\n'), ((3374, 3405), 'pyarrow.array', 'pa.array', (['[]'], {'type': 'indices_type'}), '([], type=indices_type)\n', (3382, 3405), True, 'import pyarrow as pa\n'), ((3486, 3507), 'pyarrow.array', 'pa.array', (['[]'], {'type': 'ty'}), '([], type=ty)\n', (3494, 3507), True, 'import pyarrow as pa\n'), ((3588, 3613), 'pytest.raises', 'pytest.raises', (['IndexError'], {}), '(IndexError)\n', (3601, 3613), False, 'import pytest\n'), ((3683, 3708), 'pytest.raises', 'pytest.raises', (['IndexError'], {}), '(IndexError)\n', (3696, 3708), False, 'import pytest\n'), ((3942, 3986), 'pyarrow.array', 'pa.array', (['[0, 4, 2, None]'], {'type': 'indices_type'}), '([0, 4, 2, None], type=indices_type)\n', (3950, 3986), True, 'import pyarrow as pa\n'), ((4067, 4092), 'pyarrow.array', 'pa.array', (['[0, 4, 2, None]'], {}), '([0, 4, 2, None])\n', (4075, 4092), True, 'import pyarrow as pa\n'), ((4158, 4170), 'pyarrow.float32', 'pa.float32', ([], {}), '()\n', (4168, 4170), True, 'import pyarrow as pa\n'), ((4172, 4184), 'pyarrow.float64', 'pa.float64', ([], {}), '()\n', (4182, 4184), True, 'import pyarrow as pa\n'), ((4205, 4243), 'pyarrow.array', 'pa.array', (['[0, 4, 2]'], {'type': 'indices_type'}), '([0, 4, 2], type=indices_type)\n', (4213, 4243), True, 'import pyarrow as pa\n'), ((4569, 4588), 'pyarrow.array', 'pa.array', (['[0, 1, 3]'], {}), '([0, 1, 3])\n', (4577, 4588), True, 'import pyarrow as pa\n'), ((5054, 5095), 'pyarrow.array', 'pa.array', (['[values[0], values[3]]'], {'type': 'ty'}), '([values[0], values[3]], type=ty)\n', (5062, 5095), True, 'import pyarrow as pa\n'), ((5211, 5258), 'pyarrow.array', 'pa.array', (['[values[0], values[3], None]'], {'type': 'ty'}), '([values[0], values[3], None], type=ty)\n', (5219, 5258), True, 'import pyarrow as pa\n'), ((5331, 5365), 'pytest.raises', 'pytest.raises', (['NotImplementedError'], {}), '(NotImplementedError)\n', (5344, 5365), False, 'import pytest\n'), ((5462, 5524), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""must all be the same length"""'}), "(ValueError, match='must all be the same length')\n", (5475, 5524), False, 'import pytest\n'), ((5808, 5850), 'pyarrow.array', 'pa.array', (['[True, False, None, False, True]'], {}), '([True, False, None, False, True])\n', (5816, 5850), True, 'import pyarrow as pa\n'), ((5892, 5946), 'pyarrow.chunked_array', 'pa.chunked_array', (['[[True, False, None], [False, True]]'], {}), '([[True, False, None], [False, True]])\n', (5908, 5946), True, 'import pyarrow as pa\n'), ((7027, 7069), 'pyarrow.array', 'pa.array', (['[True, False, None, False, True]'], {}), '([True, False, None, False, True])\n', (7035, 7069), True, 'import pyarrow as pa\n'), ((7111, 7165), 'pyarrow.chunked_array', 'pa.chunked_array', (['[[True, False], [None, False, True]]'], {}), '([[True, False], [None, False, True]])\n', (7127, 7165), True, 'import pyarrow as pa\n'), ((7776, 7801), 'pyarrow.array', 'pa.array', (['[0, 1, 0, 1, 0]'], {}), '([0, 1, 0, 1, 0])\n', (7784, 7801), True, 'import pyarrow as pa\n'), ((7919, 7948), 'pyarrow.array', 'pa.array', (['[True, False, True]'], {}), '([True, False, True])\n', (7927, 7948), True, 'import pyarrow as pa\n'), ((1429, 1438), 'pyarrow.int8', 'pa.int8', ([], {}), '()\n', (1436, 1438), True, 'import pyarrow as pa\n'), ((1502, 1512), 'pyarrow.int16', 'pa.int16', ([], {}), '()\n', (1510, 1512), True, 'import pyarrow as pa\n'), ((2329, 2368), 'pyarrow.array', 'pa.array', (['[1, 2, 3, 4]'], {'type': 'arrow_type'}), '([1, 2, 3, 4], type=arrow_type)\n', (2337, 2368), True, 'import pyarrow as pa\n'), ((2446, 2479), 'pyarrow.array', 'pa.array', (['[1, 2]'], {'type': 'arrow_type'}), '([1, 2], type=arrow_type)\n', (2454, 2479), True, 'import pyarrow as pa\n'), ((2481, 2514), 'pyarrow.array', 'pa.array', (['[3, 4]'], {'type': 'arrow_type'}), '([3, 4], type=arrow_type)\n', (2489, 2514), True, 'import pyarrow as pa\n'), ((2597, 2630), 'pyarrow.array', 'pa.array', (['[1, 2]'], {'type': 'arrow_type'}), '([1, 2], type=arrow_type)\n', (2605, 2630), True, 'import pyarrow as pa\n'), ((2640, 2669), 'pyarrow.array', 'pa.array', (['[]'], {'type': 'arrow_type'}), '([], type=arrow_type)\n', (2648, 2669), True, 'import pyarrow as pa\n'), ((2679, 2712), 'pyarrow.array', 'pa.array', (['[3, 4]'], {'type': 'arrow_type'}), '([3, 4], type=arrow_type)\n', (2687, 2712), True, 'import pyarrow as pa\n'), ((4257, 4291), 'pytest.raises', 'pytest.raises', (['NotImplementedError'], {}), '(NotImplementedError)\n', (4270, 4291), False, 'import pytest\n'), ((6293, 6329), 'pyarrow.array', 'pa.array', (["['a', None, 'c', 'd', 'e']"], {}), "(['a', None, 'c', 'd', 'e'])\n", (6301, 6329), True, 'import pyarrow as pa\n'), ((6485, 6505), 'pyarrow.array', 'pa.array', (["['a', 'e']"], {}), "(['a', 'e'])\n", (6493, 6505), True, 'import pyarrow as pa\n'), ((6659, 6685), 'pyarrow.array', 'pa.array', (["['a', None, 'e']"], {}), "(['a', None, 'e'])\n", (6667, 6685), True, 'import pyarrow as pa\n'), ((6786, 6822), 'pyarrow.array', 'pa.array', (["['a', None, 'c', 'd', 'e']"], {}), "(['a', None, 'c', 'd', 'e'])\n", (6794, 6822), True, 'import pyarrow as pa\n'), ((6868, 6888), 'pyarrow.array', 'pa.array', (["['a', 'e']"], {}), "(['a', 'e'])\n", (6876, 6888), True, 'import pyarrow as pa\n'), ((6934, 6960), 'pyarrow.array', 'pa.array', (["['a', None, 'e']"], {}), "(['a', None, 'e'])\n", (6942, 6960), True, 'import pyarrow as pa\n'), ((7569, 7605), 'pyarrow.array', 'pa.array', (["['a', None, 'c', 'd', 'e']"], {}), "(['a', None, 'c', 'd', 'e'])\n", (7577, 7605), True, 'import pyarrow as pa\n'), ((7644, 7680), 'pyarrow.array', 'pa.array', (["['a', None, 'c', 'd', 'e']"], {}), "(['a', None, 'c', 'd', 'e'])\n", (7652, 7680), True, 'import pyarrow as pa\n'), ((7815, 7849), 'pytest.raises', 'pytest.raises', (['NotImplementedError'], {}), '(NotImplementedError)\n', (7828, 7849), False, 'import pytest\n'), ((7962, 8029), 'pytest.raises', 'pytest.raises', (['pa.ArrowInvalid'], {'match': '"""must all be the same length"""'}), "(pa.ArrowInvalid, match='must all be the same length')\n", (7975, 8029), False, 'import pytest\n'), ((8233, 8249), 'pyarrow.array', 'pa.array', (['values'], {}), '(values)\n', (8241, 8249), True, 'import pyarrow as pa\n'), ((8292, 8318), 'pyarrow.chunked_array', 'pa.chunked_array', (['[values]'], {}), '([values])\n', (8308, 8318), True, 'import pyarrow as pa\n'), ((9078, 9094), 'pyarrow.array', 'pa.array', (['values'], {}), '(values)\n', (9086, 9094), True, 'import pyarrow as pa\n'), ((9137, 9163), 'pyarrow.chunked_array', 'pa.chunked_array', (['[values]'], {}), '([values])\n', (9153, 9163), True, 'import pyarrow as pa\n'), ((9265, 9278), 'pyarrow.array', 'pa.array', (['[2]'], {}), '([2])\n', (9273, 9278), True, 'import pyarrow as pa\n'), ((1579, 1588), 'pyarrow.int8', 'pa.int8', ([], {}), '()\n', (1586, 1588), True, 'import pyarrow as pa\n'), ((1597, 1606), 'pyarrow.int8', 'pa.int8', ([], {}), '()\n', (1604, 1606), True, 'import pyarrow as pa\n')] |
ashwinath/merlin | python/sdk/client/api/log_api.py | 087a7fa6fb21e4c771d64418bd58873175226ca1 | # coding: utf-8
"""
Merlin
API Guide for accessing Merlin's model management, deployment, and serving functionalities # noqa: E501
OpenAPI spec version: 0.7.0
Generated by: https://github.com/swagger-api/swagger-codegen.git
"""
from __future__ import absolute_import
import re # noqa: F401
# python 2 and python 3 compatibility library
import six
from client.api_client import ApiClient
class LogApi(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
Ref: https://github.com/swagger-api/swagger-codegen
"""
def __init__(self, api_client=None):
if api_client is None:
api_client = ApiClient()
self.api_client = api_client
def logs_get(self, name, pod_name, namespace, cluster, **kwargs): # noqa: E501
"""Retrieve log from a container # noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.logs_get(name, pod_name, namespace, cluster, async_req=True)
>>> result = thread.get()
:param async_req bool
:param str name: (required)
:param str pod_name: (required)
:param str namespace: (required)
:param str cluster: (required)
:param str follow:
:param str limit_bytes:
:param str pretty:
:param str previous:
:param str since_seconds:
:param str since_time:
:param str tail_lines:
:param str timestamps:
:return: None
If the method is called asynchronously,
returns the request thread.
"""
kwargs['_return_http_data_only'] = True
if kwargs.get('async_req'):
return self.logs_get_with_http_info(name, pod_name, namespace, cluster, **kwargs) # noqa: E501
else:
(data) = self.logs_get_with_http_info(name, pod_name, namespace, cluster, **kwargs) # noqa: E501
return data
def logs_get_with_http_info(self, name, pod_name, namespace, cluster, **kwargs): # noqa: E501
"""Retrieve log from a container # noqa: E501
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True
>>> thread = api.logs_get_with_http_info(name, pod_name, namespace, cluster, async_req=True)
>>> result = thread.get()
:param async_req bool
:param str name: (required)
:param str pod_name: (required)
:param str namespace: (required)
:param str cluster: (required)
:param str follow:
:param str limit_bytes:
:param str pretty:
:param str previous:
:param str since_seconds:
:param str since_time:
:param str tail_lines:
:param str timestamps:
:return: None
If the method is called asynchronously,
returns the request thread.
"""
all_params = ['name', 'pod_name', 'namespace', 'cluster', 'follow', 'limit_bytes', 'pretty', 'previous', 'since_seconds', 'since_time', 'tail_lines', 'timestamps'] # noqa: E501
all_params.append('async_req')
all_params.append('_return_http_data_only')
all_params.append('_preload_content')
all_params.append('_request_timeout')
params = locals()
for key, val in six.iteritems(params['kwargs']):
if key not in all_params:
raise TypeError(
"Got an unexpected keyword argument '%s'"
" to method logs_get" % key
)
params[key] = val
del params['kwargs']
# verify the required parameter 'name' is set
if ('name' not in params or
params['name'] is None):
raise ValueError("Missing the required parameter `name` when calling `logs_get`") # noqa: E501
# verify the required parameter 'pod_name' is set
if ('pod_name' not in params or
params['pod_name'] is None):
raise ValueError("Missing the required parameter `pod_name` when calling `logs_get`") # noqa: E501
# verify the required parameter 'namespace' is set
if ('namespace' not in params or
params['namespace'] is None):
raise ValueError("Missing the required parameter `namespace` when calling `logs_get`") # noqa: E501
# verify the required parameter 'cluster' is set
if ('cluster' not in params or
params['cluster'] is None):
raise ValueError("Missing the required parameter `cluster` when calling `logs_get`") # noqa: E501
collection_formats = {}
path_params = {}
query_params = []
if 'name' in params:
query_params.append(('name', params['name'])) # noqa: E501
if 'pod_name' in params:
query_params.append(('pod_name', params['pod_name'])) # noqa: E501
if 'namespace' in params:
query_params.append(('namespace', params['namespace'])) # noqa: E501
if 'cluster' in params:
query_params.append(('cluster', params['cluster'])) # noqa: E501
if 'follow' in params:
query_params.append(('follow', params['follow'])) # noqa: E501
if 'limit_bytes' in params:
query_params.append(('limit_bytes', params['limit_bytes'])) # noqa: E501
if 'pretty' in params:
query_params.append(('pretty', params['pretty'])) # noqa: E501
if 'previous' in params:
query_params.append(('previous', params['previous'])) # noqa: E501
if 'since_seconds' in params:
query_params.append(('since_seconds', params['since_seconds'])) # noqa: E501
if 'since_time' in params:
query_params.append(('since_time', params['since_time'])) # noqa: E501
if 'tail_lines' in params:
query_params.append(('tail_lines', params['tail_lines'])) # noqa: E501
if 'timestamps' in params:
query_params.append(('timestamps', params['timestamps'])) # noqa: E501
header_params = {}
form_params = []
local_var_files = {}
body_params = None
# Authentication setting
auth_settings = ['Bearer'] # noqa: E501
return self.api_client.call_api(
'/logs', 'GET',
path_params,
query_params,
header_params,
body=body_params,
post_params=form_params,
files=local_var_files,
response_type=None, # noqa: E501
auth_settings=auth_settings,
async_req=params.get('async_req'),
_return_http_data_only=params.get('_return_http_data_only'),
_preload_content=params.get('_preload_content', True),
_request_timeout=params.get('_request_timeout'),
collection_formats=collection_formats)
| [((3490, 3521), 'six.iteritems', 'six.iteritems', (["params['kwargs']"], {}), "(params['kwargs'])\n", (3503, 3521), False, 'import six\n'), ((722, 733), 'client.api_client.ApiClient', 'ApiClient', ([], {}), '()\n', (731, 733), False, 'from client.api_client import ApiClient\n')] |
bollwyvl/OpenMDAO | openmdao/solvers/nonlinear/nonlinear_block_jac.py | 4d7a31b2bb39674e2be0d6a13cbe22de3f5353af | """Define the NonlinearBlockJac class."""
from openmdao.recorders.recording_iteration_stack import Recording
from openmdao.solvers.solver import NonlinearSolver
from openmdao.utils.mpi import multi_proc_fail_check
class NonlinearBlockJac(NonlinearSolver):
"""
Nonlinear block Jacobi solver.
"""
SOLVER = 'NL: NLBJ'
def _single_iteration(self):
"""
Perform the operations in the iteration loop.
"""
system = self._system
self._solver_info.append_subsolver()
system._transfer('nonlinear', 'fwd')
with Recording('NonlinearBlockJac', 0, self) as rec:
# If this is a parallel group, check for analysis errors and reraise.
if len(system._subsystems_myproc) != len(system._subsystems_allprocs):
with multi_proc_fail_check(system.comm):
for subsys in system._subsystems_myproc:
subsys._solve_nonlinear()
else:
for subsys in system._subsystems_myproc:
subsys._solve_nonlinear()
system._check_child_reconf()
rec.abs = 0.0
rec.rel = 0.0
self._solver_info.pop()
def _mpi_print_header(self):
"""
Print header text before solving.
"""
if (self.options['iprint'] > 0):
pathname = self._system.pathname
if pathname:
nchar = len(pathname)
prefix = self._solver_info.prefix
header = prefix + "\n"
header += prefix + nchar * "=" + "\n"
header += prefix + pathname + "\n"
header += prefix + nchar * "="
print(header)
def _run_apply(self):
"""
Run the apply_nonlinear method on the system.
"""
system = self._system
# If this is a parallel group, check for analysis errors and reraise.
if len(system._subsystems_myproc) != len(system._subsystems_allprocs):
with multi_proc_fail_check(system.comm):
super(NonlinearBlockJac, self)._run_apply()
else:
super(NonlinearBlockJac, self)._run_apply()
| [((580, 619), 'openmdao.recorders.recording_iteration_stack.Recording', 'Recording', (['"""NonlinearBlockJac"""', '(0)', 'self'], {}), "('NonlinearBlockJac', 0, self)\n", (589, 619), False, 'from openmdao.recorders.recording_iteration_stack import Recording\n'), ((2041, 2075), 'openmdao.utils.mpi.multi_proc_fail_check', 'multi_proc_fail_check', (['system.comm'], {}), '(system.comm)\n', (2062, 2075), False, 'from openmdao.utils.mpi import multi_proc_fail_check\n'), ((815, 849), 'openmdao.utils.mpi.multi_proc_fail_check', 'multi_proc_fail_check', (['system.comm'], {}), '(system.comm)\n', (836, 849), False, 'from openmdao.utils.mpi import multi_proc_fail_check\n')] |
Subsets and Splits