content
stringlengths 0
1.05M
| origin
stringclasses 2
values | type
stringclasses 2
values |
|---|---|---|
# Copyright 2015, Pinterest, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Constructs shared by multiple tools."""
import abc
from pinball.config.utils import PinballException
__author__ = 'Pawel Garbacki'
__copyright__ = 'Copyright 2015, Pinterest, Inc.'
__credits__ = [__author__]
__license__ = 'Apache'
__version__ = '2.0'
class CommandException(PinballException):
pass
class Command(object):
"""Interface for commands interacting with the master."""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def prepare(self, options):
"""Parse command options."""
return
@abc.abstractmethod
def execute(self, client, store):
"""Execute the command using a provided client.
Args:
client: The client connected to the master.
store: The token store.
Returns:
Output of the command. We return a string rather than printing it
to stdout to make testing easier - mocking 'print' is a pain.
"""
# TODO(pawel): modify this method to return a tuple: (exit_code,
# stdout, stderr)
return
def confirm(prompt='Confirm'):
"""Prompt for yes or no response from the user.
Args:
prompt: A message to show when asking for confirmation.
Returns:
True iff the user approved the action.
"""
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = raw_input(prompt)
if not ans:
return False
if ans not in ['y', 'Y', 'n', 'N']:
print 'please enter y or n.'
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False
|
nilq/baby-python
|
python
|
from datetime import date
import click
@click.group()
def cli():
"Utility for http://b3.com.br datasets"
@cli.command()
@click.option("--date", type=click.DateTime(formats=["%Y-%m-%d"]))
@click.option("--chunk-size", type=int, default=10000)
def download(date, chunk_size):
"""Downloads quotes data"""
if not date:
import subprocess
date = subprocess.getoutput("""echo $(curl --silent 'https://arquivos.b3.com.br/apinegocios/dates') | sed -e 's/"//g' -e 's/\[//g' -e 's/\]//g' | cut -d"," -f 1""")
else:
date = str(date.date())
from b3_data import download
download.download_tickercsv(date, chunk_size)
|
nilq/baby-python
|
python
|
import torch
from torchvision.datasets.folder import default_loader
from torch.utils import data
from tqdm import tqdm
import sys
import numpy as np
import struct
class DatasetBin(data.Dataset):
def __init__(self, meta_filename, bin_filename, meta_columns, transform=None, targets_transform=None, loader=default_loader):
self.loader = loader
self.transform = (lambda x: x) if transform is None else transform
self.targets_transform = lambda x: x if targets_transform is None else targets_transform
self.bin_filename = bin_filename
self.meta_filename = meta_filename
self.samples = []
self.num_subjects = 0
def convert_type(str_value, key):
# print(str_value, type(str_value))
if 'NaN' == str_value:
return -9999
if key == 'SUBJECT_ID':
return int(str_value)
elif key == 'RACE':
return int(str_value)
else: # key == 'PR_MALE' or anything else
return float(str_value)
with open(self.meta_filename, 'r') as f:
keys = f.readline().strip().split(',')
assert all(k in keys for k in meta_columns)
for idx, line in tqdm(enumerate(f), file=sys.stdout, ncols=0, desc=f'Parsing {self.meta_filename}'):
d = dict(zip(keys, line.strip().split(',')))
targets = [convert_type(d[k], k) for k in meta_columns]
if (-9999 not in targets):
self.samples.append((targets, idx))
self.num_subjects = max(self.num_subjects, int(d['SUBJECT_ID']) + 1)
with open(self.bin_filename, 'rb') as fin:
self.bin_rows = struct.unpack('i', fin.read(4))[0]
self.bin_cols = struct.unpack('i', fin.read(4))[0]
self.bin_header_offset = fin.tell()
print('Num Subjects: ', self.num_subjects)
def __getitem__(self, index):
targets, bin_index = self.samples[index]
assert bin_index < self.bin_rows
targets = self.targets_transform(targets)
float32_sz = np.dtype(np.float32).itemsize
with open(self.bin_filename, 'rb') as fin:
fin.seek(self.bin_header_offset + (float32_sz * self.bin_cols * bin_index))
sample = torch.from_numpy(
np.fromfile(fin, dtype=np.float32, count=self.bin_cols))
return sample, targets
def __len__(self):
return len(self.samples)
def __repr__(self):
fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'
fmt_str += ' Number of datapoints: {}\n'.format(self.__len__())
fmt_str += ' Ultraface File: {}\n'.format(self.meta_filename)
fmt_str += ' Bin File: {}\n'.format(self.bin_filename)
tmp = ' Transforms (if any): '
fmt_str += '{0}{1}\n'.format(tmp, self.transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
tmp = ' Targets Transforms (if any): '
fmt_str += '{0}{1}'.format(tmp, self.targets_transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
return fmt_str
|
nilq/baby-python
|
python
|
import streamlit as st
from pdf2docx import Converter
pdf_file = 'pdf文件路径'
docx_file = '输出word文件的路径'
cv = Converter(pdf_file)
cv.convert(docx_file, start=0, end=None)
cv.close()
st.file_uploader(label, type=None, accept_multiple_files=False, key=None, help=None, on_change=None, args=None, kwargs=None)
st.download_button(label, data, file_name=None, mime=None, key=None, help=None, on_click=None, args=None, kwargs=None)
|
nilq/baby-python
|
python
|
import logging
import textwrap
from pathlib import Path
from typing import Optional
import click
from tabulate import tabulate
from bohr import api
from bohr.datamodel.bohrrepo import load_bohr_repo
from bohr.util.logging import verbosity
logger = logging.getLogger(__name__)
@click.group()
def dataset():
pass
@dataset.command()
@click.option("-t", "--task", type=str)
@click.option("-a", "--extended-list", is_flag=True)
def ls(task: Optional[str], extended_list: bool) -> None:
bohr_repo = load_bohr_repo()
if task:
if task not in bohr_repo.tasks:
logger.error(
f"Task not found in the config: {task}. \n"
f"Defined tasks: {list(bohr_repo.tasks.keys())}"
)
exit(404)
datasets = bohr_repo.tasks[task].datasets
else:
datasets = bohr_repo.datasets
if extended_list:
print(
tabulate(
[
[dataset_name, textwrap.fill(dataset.description)]
for dataset_name, dataset in datasets.items()
],
tablefmt="fancy_grid",
)
)
else:
for dataset in datasets:
print(dataset)
@dataset.command()
@click.argument("path", type=str)
@click.option("-t", "--artifact", required=True)
@click.option("-v", "--verbose", is_flag=True, help="Enables verbose mode")
def add(path: str, artifact: str, verbose: bool) -> None:
with verbosity(verbose):
dataset = api.add(Path(path), artifact)
print(f"Dataset {dataset.name} is added.")
|
nilq/baby-python
|
python
|
import requests
import json
from datetime import datetime, timedelta
import logging
bLoadWeatherModelFromFile = False
class WeatherQueryProxy():
def __init__(self, apikey, refreshtimeoutinminutes):
self.queryCache = {}
self.api_key = apikey
self.queryInvalidationTimeout = timedelta(minutes = refreshtimeoutinminutes)
def query(self, lat, lon):
if(bLoadWeatherModelFromFile):
with open("C:\\Users\\IC005540\\Desktop\\Weather.json", "r") as infile:
return json.load(infile)
else:
return self.queryCheckCache(lat,lon)
def queryServer(self, lat, lon):
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={self.api_key}&units=metric"
response = requests.get(url)
data = json.loads(response.text)
return data
def queryServerAndFillCache(self, lat,lon, internalcachekey):
data = self.queryServer(lat,lon)
self.queryCache[internalcachekey][0] = datetime.now()
self.queryCache[internalcachekey][1] = data
def queryCheckCache(self, lat, lon):
internalcachekey = f"{lat}-{lon}"
now = datetime.now()
if internalcachekey in self.queryCache:
cachedvaluesince = now - self.queryCache[internalcachekey][0]
logging.info(f"CachedValueSince {cachedvaluesince}")
if cachedvaluesince > self.queryInvalidationTimeout:
logging.info(f"Cache Period Lapsed - requerrying")
self.queryServerAndFillCache(lat,lon,internalcachekey)
return self.queryCache[internalcachekey][1]
else:
logging.info(f"Querying First time for {internalcachekey}")
self.queryCache[internalcachekey] = []
data = self.queryServer(lat,lon)
self.queryCache[internalcachekey].append(now)
self.queryCache[internalcachekey].append(data)
return self.queryCache[internalcachekey][1]
|
nilq/baby-python
|
python
|
#Exercício Python 096: Faça um programa que tenha uma função chamada área(), que receba as dimensões de um terreno retangular (largura e comprimento) e mostre a área do terreno.
#Funções utilizadas no programa principal
def mostraLinha():
print("-" * 30)
def calculoArea (x, y):
area = x * y
print(f"O terreno com {x}m de lagura por {y}m de comprimento possui uma área de {area} m².")
mostraLinha()
print("--- CÁLCULO DE ÁREA DE UM TERRENO ---")
largura = float(input("Qual a largura (m): "))
comprimento = float(input("Qual o comprimento (m): "))
calculoArea(largura, comprimento)
mostraLinha()
|
nilq/baby-python
|
python
|
# coding=utf-8
# *** WARNING: this file was generated by the Pulumi Terraform Bridge (tfgen) Tool. ***
# *** 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__ = [
'GetGlueRecordResult',
'AwaitableGetGlueRecordResult',
'get_glue_record',
'get_glue_record_output',
]
@pulumi.output_type
class GetGlueRecordResult:
"""
A collection of values returned by getGlueRecord.
"""
def __init__(__self__, id=None, ips=None, name=None, zone=None):
if id and not isinstance(id, str):
raise TypeError("Expected argument 'id' to be a str")
pulumi.set(__self__, "id", id)
if ips and not isinstance(ips, list):
raise TypeError("Expected argument 'ips' to be a list")
pulumi.set(__self__, "ips", ips)
if name and not isinstance(name, str):
raise TypeError("Expected argument 'name' to be a str")
pulumi.set(__self__, "name", name)
if zone and not isinstance(zone, str):
raise TypeError("Expected argument 'zone' to be a str")
pulumi.set(__self__, "zone", zone)
@property
@pulumi.getter
def id(self) -> str:
"""
The provider-assigned unique ID for this managed resource.
"""
return pulumi.get(self, "id")
@property
@pulumi.getter
def ips(self) -> Sequence[str]:
return pulumi.get(self, "ips")
@property
@pulumi.getter
def name(self) -> str:
return pulumi.get(self, "name")
@property
@pulumi.getter
def zone(self) -> str:
return pulumi.get(self, "zone")
class AwaitableGetGlueRecordResult(GetGlueRecordResult):
# pylint: disable=using-constant-test
def __await__(self):
if False:
yield self
return GetGlueRecordResult(
id=self.id,
ips=self.ips,
name=self.name,
zone=self.zone)
def get_glue_record(name: Optional[str] = None,
zone: Optional[str] = None,
opts: Optional[pulumi.InvokeOptions] = None) -> AwaitableGetGlueRecordResult:
"""
Use this data source to access information about an existing resource.
"""
__args__ = dict()
__args__['name'] = name
__args__['zone'] = zone
if opts is None:
opts = pulumi.InvokeOptions()
if opts.version is None:
opts.version = _utilities.get_version()
__ret__ = pulumi.runtime.invoke('gandi:domain/getGlueRecord:getGlueRecord', __args__, opts=opts, typ=GetGlueRecordResult).value
return AwaitableGetGlueRecordResult(
id=__ret__.id,
ips=__ret__.ips,
name=__ret__.name,
zone=__ret__.zone)
@_utilities.lift_output_func(get_glue_record)
def get_glue_record_output(name: Optional[pulumi.Input[str]] = None,
zone: Optional[pulumi.Input[str]] = None,
opts: Optional[pulumi.InvokeOptions] = None) -> pulumi.Output[GetGlueRecordResult]:
"""
Use this data source to access information about an existing resource.
"""
...
|
nilq/baby-python
|
python
|
import re
try:
import json
except ImportError:
import simplejson as json
from rbtools.api.errors import APIError
from rbtools.commands import (Command,
CommandError,
CommandExit,
Option,
ParseError)
class APIGet(Command):
name = 'api-get'
author = 'The Review Board Project'
description = 'Retrieve raw API resource payloads.'
args = '<path> [-- [--<query-arg>=<value> ...]]'
option_list = [
Option("--pretty",
action="store_true",
dest="pretty_print",
config_key="API_GET_PRETTY_PRINT",
default=False,
help="Pretty print output"),
Command.server_options,
]
def _dumps(self, payload):
if self.options.pretty_print:
return json.dumps(payload, sort_keys=True, indent=4)
else:
return json.dumps(payload)
def main(self, path, *args):
query_args = {}
query_arg_re = re.compile('^--(?P<name>.*)=(?P<value>.*)$')
for arg in args:
m = query_arg_re.match(arg)
if m:
query_args[m.group('name')] = m.group('value')
else:
raise ParseError("Unexpected query argument %s" % arg)
self.repository_info, self.tool = self.initialize_scm_tool()
server_url = self.get_server_url(self.repository_info, self.tool)
api_client, api_root = self.get_api(server_url)
try:
if path.startswith('http://') or path.startswith('https://'):
resource = api_client.get_url(path, **query_args)
else:
resource = api_client.get_path(path, **query_args)
except APIError, e:
if e.rsp:
print self._dumps(e.rsp)
raise CommandExit(1)
else:
raise CommandError('Could not retrieve the requested '
'resource: %s' % e)
print self._dumps(resource.rsp)
|
nilq/baby-python
|
python
|
'''
Alena will manage directories
'''
import os
from pathlib import Path
def cleaning_service(pathsToClean, images = False, videos = False):
if images:
if pathsToClean.get('original'):
if Path(os.environ.get('IMAGE_ORIGINAL_LOCAL_PATH') + pathsToClean['original']).is_file():
Path(os.environ.get('IMAGE_ORIGINAL_LOCAL_PATH') + pathsToClean['original']).unlink()
if pathsToClean.get('thumbnail'):
if Path(os.environ.get('IMAGE_THUMBNAIL_LOCAL_PATH') + pathsToClean['thumbnail']).is_file():
Path(os.environ.get('IMAGE_THUMBNAIL_LOCAL_PATH') + pathsToClean['thumbnail']).unlink()
if pathsToClean.get('qrImage'):
if Path(os.environ.get('QR_IMAGE_LOCAL_PATH') + pathsToClean['qrImage']).is_file():
Path(os.environ.get('QR_IMAGE_LOCAL_PATH') + pathsToClean['qrImage']).unlink()
elif videos:
if pathsToClean.get('original'):
if Path(os.environ.get('VIDEO_ORIGINAL_LOCAL_PATH') + pathsToClean['original']).is_file():
Path(os.environ.get('VIDEO_ORIGINAL_LOCAL_PATH') + pathsToClean['original']).unlink()
if pathsToClean.get('optimized'):
if Path(os.environ.get('VIDEO_OPTIMIZED_LOCAL_PATH') + pathsToClean['optimized']).is_file():
Path(os.environ.get('VIDEO_OPTIMIZED_LOCAL_PATH') + pathsToClean['optimized']).unlink()
def local_savings(images = False, videos = False, qrCodes = False):
if images:
Path(os.environ.get('IMAGE_ORIGINAL_LOCAL_PATH')).mkdir(parents=True, exist_ok=True)
Path(os.environ.get('IMAGE_THUMBNAIL_LOCAL_PATH')).mkdir(parents=True, exist_ok=True)
elif videos:
Path(os.environ.get('VIDEO_ORIGINAL_LOCAL_PATH')).mkdir(parents=True, exist_ok=True)
Path(os.environ.get('VIDEO_OPTIMIZED_LOCAL_PATH')).mkdir(parents=True, exist_ok=True)
elif qrCodes:
Path(os.environ.get('QR_IMAGE_LOCAL_PATH')).mkdir(parents=True, exist_ok=True)
|
nilq/baby-python
|
python
|
from .custom_unet import custom_unet
from .custom_vnet import custom_vnet
from .vanilla_unet import vanilla_unet
from .satellite_unet import satellite_unet
|
nilq/baby-python
|
python
|
# Generated by Django 3.1.3 on 2020-12-19 15:03
from django.db import migrations, models
import django.db.models.expressions
class Migration(migrations.Migration):
dependencies = [
('events', '0006_auto_20201215_1622'),
]
operations = [
migrations.RemoveConstraint(
model_name='event',
name='valid_event_start_end_date_time',
),
migrations.AddConstraint(
model_name='event',
constraint=models.CheckConstraint(check=models.Q(start_date_time__lte=django.db.models.expressions.F('end_date_time')), name='event_start_date_time_lte_end_date_time'),
),
]
|
nilq/baby-python
|
python
|
# Create a function named remove_middle which has three parameters named lst, start, and end.
# The function should return a list where all elements in lst with an index between start and end(inclusive) have been removed.
# For example, the following code should return [4, 23, 42] because elements at indices 1, 2, and 3 have been removed:
#remove_middle([4, 8, 15, 16, 23, 42], 1, 3)
def remove_middle(lst, start, end):
list_new = lst[0:start] + lst[1 + end:]
return list_new
def remove_middle(lst, start, end):
return lst[:start] + lst[end+1:]
remove_middle([4, 8, 15, 16, 23, 42], 1, 3)
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
list1 = [4, 8, 15, 16, 23, 42]
list_new = list1[0:1] + list1[4:]
print(list_new)
|
nilq/baby-python
|
python
|
import pytest
from erp import schema
from erp import forms
from erp.models import Accessibilite
from erp.schema import get_help_text_ui, get_help_text_ui_neg
@pytest.fixture
def form_test():
def _factory(name, value):
instance = Accessibilite(**{name: value})
form = forms.ViewAccessibiliteForm(instance=instance)
return form.get_accessibilite_data(flatten=True)
return _factory
def test_ViewAccessibiliteForm_empty():
form = forms.ViewAccessibiliteForm()
data = form.get_accessibilite_data()
assert list(data.keys()) == []
def test_ViewAccessibiliteForm_filled():
form = forms.ViewAccessibiliteForm(
{
"entree_reperage": True,
"transport_station_presence": True,
"stationnement_presence": True,
"cheminement_ext_presence": True,
"accueil_visibilite": True,
"sanitaires_presence": True,
"commentaire": "plop",
}
)
data = form.get_accessibilite_data()
assert list(data.keys()) == [
"Transports en commun",
"Stationnement",
"Chemin extérieur",
"Entrée",
"Accueil",
"Sanitaires",
"Commentaire",
]
def test_ViewAccessibiliteForm_filled_with_comment():
form = forms.ViewAccessibiliteForm({"commentaire": "plop"})
data = form.get_accessibilite_data()
field = data["Commentaire"]["fields"][0]
assert field["value"] == "plop"
assert field["is_comment"] is True
def test_ViewAccessibiliteForm_filled_null_comment():
form = forms.ViewAccessibiliteForm(
{
"sanitaires_presence": True,
"commentaire": "",
}
)
data = form.get_accessibilite_data()
assert list(data.keys()) == ["Sanitaires"]
def test_ViewAccessibiliteForm_serialized():
form = forms.ViewAccessibiliteForm(
{
"entree_reperage": True,
}
)
data = form.get_accessibilite_data()
field = data["Entrée"]["fields"][0]
assert field["name"] == "entree_reperage"
assert field["label"] == schema.get_help_text_ui("entree_reperage")
assert field["value"] is True
assert field["warning"] is False
assert field["is_comment"] is False
def test_ViewAccessibiliteForm_labels(form_test):
def assert_absence(name, value):
assert get_help_text_ui_neg(name) in [
f["label"] for f in form_test(name, value)
]
def assert_presence(name, value):
assert get_help_text_ui(name) in [f["label"] for f in form_test(name, value)]
def assert_missing(name, value):
assert get_help_text_ui(name) not in [
f["label"] for f in form_test(name, value)
]
assert get_help_text_ui_neg(name) not in [
f["label"] for f in form_test(name, value)
]
# boolean fields
assert_presence("sanitaires_presence", True)
assert_absence("sanitaires_presence", False)
assert_missing("sanitaires_presence", None)
# integer fields
assert_presence("sanitaires_adaptes", 1)
assert_presence("sanitaires_adaptes", 2)
assert_absence("sanitaires_adaptes", 0)
assert_missing("sanitaires_adaptes", None)
# single string fields
assert_presence("cheminement_ext_pente_degre_difficulte", schema.PENTE_LEGERE)
assert_missing("cheminement_ext_pente_degre_difficulte", None)
# multiple strings fields
assert_presence(
"entree_dispositif_appel_type",
[schema.DISPOSITIFS_APPEL_BOUTON, schema.DISPOSITIFS_APPEL_INTERPHONE],
)
assert_missing("entree_dispositif_appel_type", [])
assert_missing("entree_dispositif_appel_type", None)
# special cases
assert_presence("cheminement_ext_devers", schema.DEVERS_LEGER)
assert_absence("cheminement_ext_devers", schema.DEVERS_AUCUN)
assert_missing("cheminement_ext_devers", None)
for f in [
"cheminement_ext_rampe",
"entree_marches_rampe",
"accueil_cheminement_rampe",
]:
assert_presence(f, schema.RAMPE_AMOVIBLE)
assert_absence(f, schema.RAMPE_AUCUNE)
assert_missing(f, None)
assert_presence("accueil_personnels", schema.PERSONNELS_FORMES)
assert_absence("accueil_personnels", schema.PERSONNELS_AUCUN)
assert_missing("accueil_personnels", None)
|
nilq/baby-python
|
python
|
import logging
import sys
import os
from tasker.master.Master import Master
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler(sys.stdout)])
def supply():
n = 1
while n <= 5:
task = {
"id": "test-{}".format(n),
"image": "pippo",
"arguments": 'python3 -m autotrader.run.backtest.Trade-MultiSignal coeff="eyJzZV9wc2FyX2ludmVyc2lvbiI6MC40LCJzZV9wc2FyX2xyXzEwIjowLjAyNSwic2VfcHNhcl9zdHJlbmd0aCI6LTAuMDF9"'
}
yield task
n += 1
def reduce(value, accumulator, bar):
# value is a dictionary similar to {'id': 'test-2', 'isSuccess': True, 'payload': '2'}
bar.text("Processing: {task}".format(task=value['id']))
if value['isSuccess']:
accumulator.append(int(value['payload']))
def termination(values):
logging.info("Termination! Values: {}".format(values))
_sum = 0
for v in values:
_sum += v
logging.info("DONE! The sum is {}".format(_sum))
logging.info(_sum)
if __name__ == '__main__':
print("DIOCANE PYTHON")
master = Master(supplier=supply,
reducer=reduce,
terminate=termination)
master.start()
print("### DONE ###")
|
nilq/baby-python
|
python
|
import datetime as dt
from os.path import splitext
def add_ts_to_filename(filepath):
filename, extension = splitext(filepath)
ts = dt.datetime.today().strftime('%Y%m%dT%H%M%S')
filename_with_ts = f"{filename}_{ts}{extension}"
return filename_with_ts
if __name__ == '__main__':
print(add_ts_to_filename(__file__))
|
nilq/baby-python
|
python
|
import json
class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
j = len(nums) - 1
i = 0
while i < j:
if nums[i] == val and nums[j] != val:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1
elif nums[i] != val:
i += 1
elif nums[j] == val:
j -= 1
return j if nums[j] == val else j+1
def stringToIntegerList(input):
return json.loads(input)
def integerListToString(nums, len_of_list=None):
if not len_of_list:
len_of_list = len(nums)
return json.dumps(nums[:len_of_list])
def main():
import sys
def readlines():
for line in sys.stdin:
yield line.strip('\n')
lines = readlines()
while True:
try:
line = next(lines)
nums = stringToIntegerList(line)
line = next(lines)
val = int(line)
ret = Solution().removeElement(nums, val)
out = integerListToString(nums, len_of_list=ret)
print(out)
except StopIteration:
break
if __name__ == '__main__':
main()
|
nilq/baby-python
|
python
|
from enum import IntFlag
from .ProcStates import *
class Process:
def __init__(self, n_cores=4096, time_needed=1000, process_id = 0,state = PROC_STATE.WAITING,
scheduled_start_time=0, name="CIFAR" ):
self.n_cores = n_cores
self.needed_time = time_needed
self.process_id = process_id
if state == PROC_STATE.WAITING and scheduled_start_time > 0:
current_state = PROC_STATE.PRE_WAIT
elif state == PROC_STATE.NO_OP:
if scheduled_start_time > 0:
current_state = PROC_STATE.PRE_WAIT
else:
current_state = PROC_STATE.WAITING
else:
current_state = state
self.waiting_time = 0
self.running_time = 0
self.scheduled_start_time = scheduled_start_time
self.start_time = 0
self.stop_time = 0
self.clock = -1
self.name = name
self.running = False
self.messages = []
self._state = current_state
@property
def state(self):
return self._state
@state.setter
def state(self, value):
self.add_event(value)
self._state = value
def start_proc(self):
assert(self.state == PROC_STATE.WAITING)
self.start_time = self.clock
self.running = True
self.state = PROC_STATE.RUNNING
return self.state
def stop_proc(self):
assert(self.state == PROC_STATE.RUNNING)
#todo: Potential bug - should check if run_time is > than clock
if self.needed_time >= self.running_time:
self.state = PROC_STATE.COMPLETE
self.stop_time = self.clock
else:
self.state = PROC_STATE.WAITING
return self.state
def msg_tmplate(self):
return f"P {self.process_id} clock: {self.clock} "
def add_message(self, proc_message = PROC_MESSAGE.SHOULD_WAIT):
m = f"{self.msg_tmplate()} says {proc_message}"
self.messages.append(m)
def add_event(self, proc_message = PROC_STATE.PRE_WAIT):
m = f"{self.msg_tmplate()} is now {proc_message}"
self.messages.append(m)
def tick(self):
self.clock += 1
ctx = 0
if self.state == PROC_STATE.PRE_WAIT:
if self.scheduled_start_time <= self.clock:
ctx = PROC_MESSAGE.SHOULD_WAIT
self.state = PROC_STATE.WAITING
else:
ctx = PROC_MESSAGE.NO_CHANGE
elif self.state == PROC_STATE.WAITING:
self.waiting_time += 1
ctx = PROC_MESSAGE.SHOULD_START
elif self.state == PROC_STATE.RUNNING:
new_run_time = self.running_time + 1
assert(new_run_time <= self.needed_time)
if new_run_time < self.needed_time:
ctx = PROC_MESSAGE.NO_CHANGE
elif new_run_time == self.needed_time:
ctx = PROC_MESSAGE.SHOULD_END
self.messages.append(
f"Process {self.name} completed with rt:{self.running_time}, wt:{self.waiting_time}")
self.running_time = new_run_time
elif self.state == PROC_STATE.COMPLETE:
ctx = PROC_MESSAGE.NO_CHANGE
self.add_message(ctx)
return ctx.value
def hdr(self):
return "name,scheduled_start_time,wait_time,start_time,running_time,current_state\n"
def __str__(self):
return f"{self.name},{self.scheduled_start_time},{self.waiting_time},{self.start_time},{self.running_time},{self.state}"
|
nilq/baby-python
|
python
|
pas = float(input('Quantos Km você irá percorrer'))
pas1 = pas * 0.45
pas2 = pas * 0.5
if pas > 200:
print('Sua passagem saí por R${:.2f}' .format(pas1))
else:
print('Sua passagem saí por R${:.2f}'.format(pas2))
|
nilq/baby-python
|
python
|
import json
from http.client import HTTPResponse
from urllib.parse import urlencode
from urllib.request import urlopen
class MeasurementUnit:
def __init__(self, name: str, temperature: str, wind_speed: str):
self.name = name
self.temperature = temperature
self.wind_speed = wind_speed
class OpenWeatherMapApi:
def __init__(self, location: str, country: str, api_key: str, unit_system: str):
self.unit_system = unit_system
self.api_key = api_key
self.country = country
self.location = location
self.API_URL = 'https://api.openweathermap.org/data/2.5/weather?'
def fetch(self):
return urlopen(self.build_url())
def build_url(self):
location_and_country = '%s,%s' % (self.location, self.country)
query_params = {'q': location_and_country, 'APPID': self.api_key, 'units': self.unit_system}
return self.API_URL + urlencode(query_params)
class WeatherUpdate:
def __init__(self, area, country, description, temperature, feels_like, wind_speed, humidity, measurement_unit: MeasurementUnit):
self.measurement_unit = measurement_unit
self.description = description
self.humidity = humidity
self.wind_speed = wind_speed
self.feels_like = feels_like
self.temperature = temperature
self.country = country
self.area = area
@staticmethod
def from_response(response: HTTPResponse, measurement_unit: MeasurementUnit):
json_contents = json.loads(response.read())
return WeatherUpdate(
json_contents.get('name'),
json_contents.get('sys').get('country'),
json_contents.get('weather')[0].get('description'),
json_contents.get('main').get('temp'),
json_contents.get('main').get('feels_like'),
json_contents.get('wind').get('speed'),
json_contents.get('main').get('humidity'),
measurement_unit
)
def __str__(self):
header = '🌍 %s, %s: %s\n' % (self.area, self.country, self.description)
return ''.join(
[
header,
'-' * len(header),
'\n🌡️ Temperature: %s %s\n' % (self.temperature, self.measurement_unit.temperature),
'⚖️ Feels like: %s %s\n' % (self.feels_like, self.measurement_unit.temperature),
'💦 Humidity: %s %%\n' % self.humidity,
'🌬 Wind speed: %s %s' % (self.wind_speed, self.measurement_unit.wind_speed)
]
)
class WeatherScript:
def __init__(self, weather_api, measurement_unit: MeasurementUnit):
self.weather_api = weather_api
self.measurement_unit = measurement_unit
def run(self):
print(WeatherUpdate.from_response(self.weather_api.fetch(), self.measurement_unit))
|
nilq/baby-python
|
python
|
from os.path import abspath, dirname, join
from setuptools import find_packages, setup
# Fetches the content from README.md
# This will be used for the "long_description" field.
with open(join(dirname(abspath(__file__)), "README.md"), encoding='utf-8') as f:
README_MD = f.read()
setup(
# The name of your project that we discussed earlier.
# This name will decide what users will type when they install your package.
# In my case it will be:
# pip install pydash-arnu515
# This field is REQUIRED
name="python_ramda",
# The version of your project.
# Usually, it would be in the form of:
# major.minor.patch
# eg: 1.0.0, 1.0.1, 3.0.2, 5.0-beta, etc.
# You CANNOT upload two versions of your package with the same version number
# This field is REQUIRED
version="0.2.2",
# The packages that constitute your project.
# For my project, I have only one - "pydash".
# Either you could write the name of the package, or
# alternatively use setuptools.findpackages()
#
# If you only have one file, instead of a package,
# you can instead use the py_modules field instead.
# EITHER py_modules OR packages should be present.
packages=find_packages(exclude="tests"),
# The description that will be shown on PyPI.
# Keep it short and concise
# This field is OPTIONAL
description="A small clone of ramda",
# The content that will be shown on your project page.
# In this case, we're displaying whatever is there in our README.md file
# This field is OPTIONAL
long_description=README_MD,
# Now, we'll tell PyPI what language our README file is in.
# In my case it is in Markdown, so I'll write "text/markdown"
# Some people use reStructuredText instead, so you should write "text/x-rst"
# If your README is just a text file, you have to write "text/plain"
# This field is OPTIONAL
long_description_content_type="text/markdown",
# The url field should contain a link to a git repository, the project's website
# or the project's documentation. I'll leave a link to this project's Github repository.
# This field is OPTIONAL
url="https://github.com/zydmayday/python_ramda",
# The author name and email fields are self explanatory.
# These fields are OPTIONAL
author_name="zydmayday",
author_email="zydmayday@gmail.com",
# Classifiers help categorize your project.
# For a complete list of classifiers, visit:
# https://pypi.org/classifiers
# This is OPTIONAL
classifiers=[
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.9"
],
# Keywords are tags that identify your project and help searching for it
# This field is OPTIONAL
keywords="functional programming, ramda",
# For additional fields, check:
# https://github.com/pypa/sampleproject/blob/master/setup.py
)
|
nilq/baby-python
|
python
|
import random
import numpy as np
from time import sleep
def AgentModel(Observation,actions,reward):
sleep(0.05)
#action = random.choice(actions)
Observation_size = Observation.size()
action_size = len(actions)
qtable = np.zeros((Observation_size, action_size))
return action
|
nilq/baby-python
|
python
|
#----------------------------------------------------------------------
# Libraries
from PyQt6.QtWidgets import QGridLayout, QDialog, QDialogButtonBox, QLabel, QSizePolicy
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt
from .QBaseApplication import QBaseApplication
from .QGridWidget import QGridWidget
#----------------------------------------------------------------------
# Class
class QAboutBox(QDialog):
def __init__(self, app: QBaseApplication = None, windowTitle: str = '', logo: str = '', texts: list[QLabel] = []):
super().__init__(parent = app.window)
self.setWindowFlag(Qt.WindowType.MSWindowsFixedSizeDialogHint, True)
self.__layout__ = QGridLayout(self)
self.__left__ = QGridWidget()
self.__right__ = QGridWidget()
self.__right__.gridLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.__right__.gridLayout.setSpacing(20)
self.__down__ = QGridWidget()
self.__layout__.addWidget(self.__left__, 0, 0)
self.__layout__.addWidget(self.__right__, 0, 1)
pixmap = QLabel()
pixmap.setPixmap(QPixmap(logo))
#TODO: pixmap.setFixedSize(100, 100)
self.__left__.gridLayout.addWidget(pixmap)
self.__left__.gridLayout.setAlignment(pixmap, Qt.AlignmentFlag.AlignTop)
self.__left__.gridLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
self.setWindowTitle(windowTitle)
text = QLabel(windowTitle)
text.setProperty('class', 'bold')
self.__right__.gridLayout.addWidget(text, 0, 0)
self.__right__.gridLayout.setAlignment(text, Qt.AlignmentFlag.AlignTop)
self.__right__.gridLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
for textID in range(len(texts)):
texts[textID].setOpenExternalLinks(True)
self.__right__.gridLayout.addWidget(texts[textID], textID + 1, 0)
self.__right__.gridLayout.setAlignment(texts[textID], Qt.AlignmentFlag.AlignTop)
QBtn = QDialogButtonBox.StandardButton.Ok
self.__buttonBox__ = QDialogButtonBox(QBtn)
self.__buttonBox__.accepted.connect(self.accept)
self.__buttonBox__.rejected.connect(self.reject)
self.__layout__.addWidget(self.__buttonBox__, 1, 1)
#----------------------------------------------------------------------
|
nilq/baby-python
|
python
|
""" plugin example for autofile template system """
from typing import Iterable, List, Optional
import autofile
# specify which template fields your plugin will provide
FIELDS = {"{foo}": "Returns BAR", "{bar}": "Returns FOO"}
@autofile.hookimpl
def get_template_help() -> Iterable:
"""Specify help text for your plugin; will get displayed with autofile --help
Returns:
Iterable (e.g. list) of help text as str or list of lists
str items may be formatted with markdown
list of lists items can be used for definition lists (e.g. [[key1, value1], [key2, value2]])
"""
text = """
This a useless example plugin that returns the text "FOO" or "BAR".
autofile will correctly format this text for you so don't worry about the spaces preceding each line
in the docstring block quote.
You can use markdown in the docstring to format the text. **This is bold** and *this is italic*.
- You can also use lists
- This is another list item
"""
fields = [["Field", "Description"], *[[k, v] for k, v in FIELDS.items()]]
return ["**FooBar Fields**", fields, text]
@autofile.hookimpl
def get_template_value(
filepath: str, field: str, subfield: str, default: List[str]
) -> Optional[List[Optional[str]]]:
"""lookup value for file dates
Args:
filepath: path to the file being processed
field: template field to find value for
subfield: the subfield provided, if any (e.g. {field:subfield})
default: the default value provided to the template, if any (e.g. {field,default})
options: the render options provided to the template, you likely won't need this
Returns:
The matching template value (which may be None) as a list or None if template field is not handled.
Raises:
ValueError: if the template is not correctly formatted (e.g. plugin expected a subfield but none provided)
"""
# if your plugin does not handle a certain field, return None
if "{" + field + "}" not in FIELDS:
return None
if field == "foo":
return ["BAR"]
elif field == "bar":
return ["FOO"]
else:
return None
|
nilq/baby-python
|
python
|
"""
appends JSON wine records from given data, formats and sort'em,
output is JSON file, contains summary information by built-in parameters
"""
winedata_full = []
avg_wine_price_by_origin = []
ratings_count = []
def string_comb(raw_str):
form_str = ' '.join(
raw_str[1:-1].split()
).replace(
'price": ', 'price": "'
).replace(
', "designation', '", "designation'
).replace(
'null', '"None"'
)
return form_str.split('": "')
def flatten(nonflat_arr):
flat_arr = []
for m in nonflat_arr:
flat_arr.extend(m)
return flat_arr
def parser(inp_json):
json_arr = set()
for s in inp_json[2:-2].split('}, {'):
json_arr.add(s)
for x in json_arr:
temp = flatten([i.split('", "', 2) for i in string_comb(x)])
curr_rec = {
temp[k]: temp[k + 1] for k in range(0, len(temp), 2)
}
winedata_full.append(curr_rec)
def sort_by_price(wine_record):
if wine_record['price'] == '"None"':
return 0
return int(wine_record['price'])
def summary(kind):
all_coinc = []
for w in winedata_full:
if kind in w['variety']:
all_coinc.append(w)
if all_coinc == []:
return f"no records on {kind} were found"
prices_arr = [
int(r['price']) for r in all_coinc if r['price'] != '"None"'
]
avarege_price = round(sum(prices_arr) / len(prices_arr), 1)
min_price = min(prices_arr)
max_price = max(prices_arr)
regions = {}
for i in all_coinc:
if i['region_1'] == 'None':
continue
elif i['region_1'] not in regions:
regions[i['region_1']] = 1
else:
regions[i['region_1']] = +1
if i['region_2'] == 'None':
continue
elif i['region_2'] not in regions:
regions[i['region_2']] = 1
else:
regions[i['region_2']] = +1
regions = [[r, regions[r]] for r in regions]
regions.sort(key=lambda x: x[1])
regions.sort()
most_common_region = [i for i in regions if i[1] == regions[0][1]]
countries = {}
for i in all_coinc:
if i['country'] == 'None':
continue
elif i['country'] not in countries:
countries[i['country']] = 1
else:
countries[i['country']] = +1
countries = [[c, countries[c]] for c in countries]
countries.sort(key=lambda x: x[1])
countries.sort()
most_common_country = [n for n in countries if n[1] == countries[0][1]]
avarage_score = int(
sum([int(i['points']) for i in all_coinc]) / len(all_coinc)
)
return f'''avarege_price: {avarege_price},
min_price: {min_price},
max_price: {max_price},
most_common_region: {most_common_region},
most_common_country: {most_common_country},
avarage_score: {avarage_score}'''
def most_expensive_wine(w_data):
max_price = w_data[0]['price']
most_expensive_arr = []
for r in w_data:
if r['price'] == max_price:
most_expensive_arr.append(r)
else:
break
print(max_price)
print(w_data[0]['price'])
return most_expensive_arr
def cheapest_wine(w_data):
cheap_arr = []
for w in reversed(w_data):
if w['price'] != '"None"':
min_price = w['price']
break
for r in reversed(w_data):
if r['price'] <= min_price:
cheap_arr.append(r)
else:
break
return cheap_arr
def highest_score(w_data):
hi_score = int(w_data[0]['points'])
highest_score_arr = []
for r in w_data:
if int(r['points']) >= hi_score:
highest_score_arr.append(r)
else:
break
return highest_score_arr
def lowest_score(w_data):
lowest_score_arr = []
for w in reversed(w_data):
if w['points'] != '"None"':
low_score = int(w['points'])
break
for r in reversed(w_data):
if int(r['points']) <= low_score:
lowest_score_arr.append(r)
else:
break
return lowest_score_arr
def most_expensive_country(w_data):
unsort_prc = {}
for w in w_data:
if w['price'] == '"None"':
continue
elif w['country'] not in unsort_prc:
w_count = 1
sum_price = int(w['price'])
unsort_prc[w['country']] = [sum_price, w_count]
else:
unsort_prc[w['country']][0] += int(w['price'])
unsort_prc[w['country']][1] += 1
global avg_wine_price_by_origin
avg_wine_price_by_origin = [
[i, int(unsort_prc[i][0] / unsort_prc[i][1])] for i in unsort_prc
]
avg_wine_price_by_origin.sort(key=lambda x: x[1], reverse=True)
return avg_wine_price_by_origin[0]
def cheapest_country(w_data):
return w_data[-1]
def most_rated_country(w_data):
unsort_cnt = {}
for w in w_data:
if w['country'] == 'None':
continue
elif w['country'] not in unsort_cnt:
unsort_cnt[w['country']] = 1
else:
unsort_cnt[w['country']] += 1
global ratings_count
ratings_count = [[i, unsort_cnt[i]] for i in unsort_cnt]
ratings_count.sort(key=lambda x: x[1], reverse=True)
return ratings_count[0]
def most_underrated_country(w_data):
return w_data[-1]
def most_active_commentator(w_data):
commentators = {}
for w in w_data:
if w['taster_name'] == 'None':
continue
elif w['taster_name'] not in commentators:
commentators[w['taster_name']] = 1
else:
commentators[w['taster_name']] += 1
commentators = [[i, commentators[i]] for i in commentators]
commentators.sort(key=lambda x: x[1], reverse=True)
return commentators[0]
with open("./winedata_1.json") as wd_1:
for line in wd_1:
parser(line)
with open("./winedata_2.json") as wd_2:
for line in wd_2:
parser(line)
winedata_full.sort(key=lambda x: x['title'])
winedata_full.sort(key=sort_by_price, reverse=True)
w_data_by_points = winedata_full
w_data_by_points.sort(key=lambda x: int(x['points']), reverse=True)
extr_data = {
"Gewurztraminer_summ": summary('Gewurztraminer'),
"Riesling_summ": summary('Riesling'),
"Merlot_summ": summary('Merlot'),
"Madera_summ": summary('Madera'),
"Tempranillo_summ": summary('Tempranillo'),
"Red_Blend_summ": summary('Red Blend'),
"most_ex_wine": most_expensive_wine(winedata_full),
"chp_wine": cheapest_wine(winedata_full),
"hi_scr": highest_score(w_data_by_points),
"low_scr": lowest_score(w_data_by_points),
"most_cnt": most_expensive_country(winedata_full),
"chp_cnt": cheapest_country(avg_wine_price_by_origin),
"rate_cnt": most_rated_country(winedata_full),
"undr_cnt": most_underrated_country(ratings_count),
"act_cnt": most_active_commentator(winedata_full)
}
extr_str = """{{"statistics": {{
"wine": {{
"Gewurztraminer": {Gewurztraminer_summ}
}},
{{
"Riesling": {Riesling_summ}
}},
{{
"Merlot": {Merlot_summ}
}},
{{
"Madera": {Madera_summ}
}},
{{
"Tempranillo": {Tempranillo_summ}
}},
{{
"Red Blend": {Red_Blend_summ}
}},
"most_expensive_wine": {most_ex_wine},
"cheapest_wine": {chp_wine},
"highest_score": {hi_scr},
"lowest_score": {low_scr},
"most_expensive_country": {most_cnt},
"cheapest_country": {chp_cnt},
"most_rated_country": {rate_cnt},
"underrated_country": {undr_cnt},
"most_active_commentator": {act_cnt}
}}
}}""".format(**extr_data)
# with open('./stats.json', 'a') as ex:
# ex.write(extr_str)
|
nilq/baby-python
|
python
|
#!/usr/bin/env python3
# @generated AUTOGENERATED file. Do not Change!
from dataclasses import dataclass, field as _field
from ...config import custom_scalars, datetime
from gql_client.runtime.variables import encode_variables
from gql import gql, Client
from gql.transport.exceptions import TransportQueryError
from functools import partial
from numbers import Number
from typing import Any, AsyncGenerator, Dict, List, Generator, Optional
from time import perf_counter
from dataclasses_json import DataClassJsonMixin, config
from ..fragment.service import ServiceFragment, QUERY as ServiceFragmentQuery
from ..input.service_create_data import ServiceCreateData
# fmt: off
QUERY: List[str] = ServiceFragmentQuery + ["""
mutation AddServiceMutation($data: ServiceCreateData!) {
addService(data: $data) {
...ServiceFragment
}
}
"""
]
class AddServiceMutation:
@dataclass(frozen=True)
class AddServiceMutationData(DataClassJsonMixin):
@dataclass(frozen=True)
class Service(ServiceFragment):
pass
addService: Service
# fmt: off
@classmethod
def execute(cls, client: Client, data: ServiceCreateData) -> AddServiceMutationData.Service:
variables: Dict[str, Any] = {"data": data}
new_variables = encode_variables(variables, custom_scalars)
response_text = client.execute(
gql("".join(set(QUERY))), variable_values=new_variables
)
res = cls.AddServiceMutationData.from_dict(response_text)
return res.addService
# fmt: off
@classmethod
async def execute_async(cls, client: Client, data: ServiceCreateData) -> AddServiceMutationData.Service:
variables: Dict[str, Any] = {"data": data}
new_variables = encode_variables(variables, custom_scalars)
response_text = await client.execute_async(
gql("".join(set(QUERY))), variable_values=new_variables
)
res = cls.AddServiceMutationData.from_dict(response_text)
return res.addService
|
nilq/baby-python
|
python
|
#!/usr/bin/env python3
# Copyright (c) 2004-present Facebook All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
from gql.transport.exceptions import TransportServerError
from psym.common.constant import __version__
from .api.equipment_type import _populate_equipment_types
from .api.location_type import _populate_location_types
from .api.port_type import _populate_equipment_port_types
from .api.project_type import _populate_project_types
from .api.service_type import _populate_service_types
from .api.work_order_type import _populate_work_order_types
from .client import SymphonyClient
"""Psym is a python package that allows for querying and modifying the Symphony data using graphql queries.
This module contains the client that allows to connect to inventory. The client
allows different kind of operations: querying and creating of locations, equipments,
positions and links.
Example of how to connect:
```
from psym import PsymClient
# since symphony is multi tenant system you will need to insert which
# partner you connect as
client = PsymClient(email, password, "tenant_name")
location = client.add_location(
location_hirerchy=[
("Country", "England"),
("City", "Milton Keynes"),
("Site", "Bletchley Park")
],
properties_dict={
"Date Property": date.today(),
"Lat/Lng Property": (-1.23,9.232),
"E-mail Property": "user@fb.com",
"Number Property": 11,
"String Property": "aa",
"Float Property": 1.23,
},
lat=-11.32,
long=98.32,
external_id=None
)
equipment = client.add_equipment(
name="Router X123",
equipment_type="Router",
location=location,
properties_dict={
"Date Property": date.today(),
"Lat/Lng Property": (-1.23,9.232),
"E-mail Property": "user@fb.com",
"Number Property": 11,
"String Property": "aa",
"Float Property": 1.23,
}
)
```
"""
class UserDeactivatedException(Exception):
pass
class PsymClient(SymphonyClient):
from .api.file import add_location_image, delete_document, add_file, add_files
from .api.document_category import get_document_category_by_names
from .api.location_type import (
add_location_type,
add_property_types_to_location_type,
delete_location_type,
get_location_types,
get_location_type_by_id,
get_location_type_by_name,
edit_location_type,
delete_locations_by_location_type,
delete_location_type_with_locations,
)
from .api.location import (
get_location,
get_location_by_external_id,
get_location_children,
get_location_documents,
delete_location,
add_location,
edit_location,
move_location,
get_locations,
)
from .api.equipment_type import (
copy_equipment_type,
delete_equipment_type_with_equipments,
_add_equipment_type,
add_equipment_type,
add_property_types_to_equipment_type,
get_or_create_equipment_type,
_edit_equipment_type,
edit_equipment_type,
get_equipment_type_property_type,
get_equipment_type_property_type_by_external_id,
edit_equipment_type_property_type,
delete_equipment_type,
get_equipment_type_by_id,
get_equipment_types,
)
from .api.equipment import (
add_equipment,
add_equipment_to_position,
get_equipment,
get_equipment_in_position,
get_equipments,
delete_equipment,
search_for_equipments,
delete_all_equipments,
copy_equipment_in_position,
copy_equipment,
get_equipment_type_of_equipment,
get_or_create_equipment,
get_or_create_equipment_in_position,
edit_equipment,
get_equipment_properties,
get_equipments_by_type,
get_equipments_by_location,
get_equipment_by_external_id,
)
from .api.link import (
add_link,
get_link_in_port_of_equipment,
get_links,
get_all_links_and_port_names_of_equipment,
)
from .api.service import (
add_service,
add_service_endpoint,
add_service_link,
get_service,
)
from .api.service_type import (
add_service_type,
add_property_types_to_service_type,
get_service_type,
get_service_types,
edit_service_type,
delete_service_type,
delete_service_type_with_services,
)
from .api.location_template import (
apply_location_template_to_location,
copy_equipment_with_all_attachments,
)
from .api.customer import add_customer, delete_customer, get_all_customers
from .api.port_type import (
add_equipment_port_type,
get_equipment_port_type,
edit_equipment_port_type,
delete_equipment_port_type,
get_equipment_port_types,
)
from .api.port import (
get_port,
edit_port_properties,
edit_link_properties,
get_ports,
)
from .api.user import (
add_user,
get_user,
edit_user,
deactivate_user,
activate_user,
get_users,
get_active_users,
)
from .api.property_type import (
get_property_type_id,
get_property_types,
get_property_type,
get_property_type_by_external_id,
)
from .api.features import get_enabled_features, set_feature
from .api.work_order_type import (
get_work_order_type_by_name,
get_work_order_type_by_id,
get_work_order_types,
add_work_order_type,
add_property_types_to_work_order_type,
delete_work_order_type,
)
from .api.work_order import (
add_work_order,
get_work_orders,
get_work_order_by_id,
edit_work_order,
delete_work_order,
)
from .api.project_type import (
add_project_type,
add_property_types_to_project_type,
delete_project_type,
edit_project_type,
get_project_type_by_id,
get_project_types,
)
from .api.project import (
add_project,
delete_project,
edit_project,
get_project_by_id,
get_projects,
)
def __init__(
self,
email: str,
password: str,
tenant: str = "fb-test",
is_local_host: bool = False,
is_dev_mode: bool = False,
) -> None:
"""This is the class to use for working with inventory. It contains all
the functions to query and and edit the inventory.
The __init__ method populates the different entity types
for faster run of operations.
Args:
email (str): The email of the user to connect with.
password (str): The password of the user to connect with.
tenant (str, optional): The tenant to connect to -
should be the beginning of "{}.purpleheadband.cloud"
The default is "fb-test" for QA environment
is_local_host (bool, optional): Used for developers to connect to
local inventory. This changes the address and also
disable verification of ssl certificate
is_dev_mode (bool, optional): Used for developers to connect to
local inventory from a container. This changes the
address and also disable verification of ssl
certificate
"""
super().__init__(
email,
password,
tenant,
f"Psym/{__version__}",
is_local_host,
is_dev_mode,
)
try:
self.populate_types()
except TransportServerError as e:
err_msg = str(e.args[0])
if "Forbidden" in err_msg:
raise UserDeactivatedException()
raise
def populate_types(self) -> None:
_populate_location_types(self)
_populate_equipment_types(self)
_populate_service_types(self)
_populate_equipment_port_types(self)
_populate_work_order_types(self)
_populate_project_types(self)
|
nilq/baby-python
|
python
|
import os
import cv2
import numpy as np
import argparse
from SSRNET_model import SSR_net, SSR_net_general
import sys
import timeit
from moviepy.editor import *
from keras import backend as K
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=1, thickness=2):
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
x, y = point
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness)
def draw_results(detected,input_img,faces,ad,img_size,img_w,img_h,model,model_gender,time_detection,time_network,time_plot):
#for i, d in enumerate(detected):
for i, (x,y,w,h) in enumerate(detected):
#x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
x1 = x
y1 = y
x2 = x+w
y2 = y+h
xw1 = max(int(x1 - ad * w), 0)
yw1 = max(int(y1 - ad * h), 0)
xw2 = min(int(x2 + ad * w), img_w - 1)
yw2 = min(int(y2 + ad * h), img_h - 1)
faces[i,:,:,:] = cv2.resize(input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
faces[i,:,:,:] = cv2.normalize(faces[i,:,:,:], None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.rectangle(input_img, (xw1, yw1), (xw2, yw2), (0, 0, 255), 2)
start_time = timeit.default_timer()
if len(detected) > 0:
# predict ages and genders of the detected faces
predicted_ages = model.predict(faces)
predicted_genders = model_gender.predict(faces)
# draw results
for i, (x,y,w,h) in enumerate(detected):
#label = "{}~{}, {}".format(int(predicted_ages[i]*4.54),int((predicted_ages[i]+1)*4.54),
# "F" if predicted_genders[i][0] > 0.5 else "M")
x1 = x
y1 = y
x2 = x+w
y2 = y+h
gender_str = 'male'
if predicted_genders[i]<0.5:
gender_str = 'female'
label = "{},{}".format(int(predicted_ages[i]),gender_str)
draw_label(input_img, (x1, y1), label)
elapsed_time = timeit.default_timer()-start_time
time_network = time_network + elapsed_time
start_time = timeit.default_timer()
#input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
cv2.imshow("result", input_img)
elapsed_time = timeit.default_timer()-start_time
time_plot = time_plot + elapsed_time
return input_img,time_network,time_plot
def main():
K.set_learning_phase(0) # make sure its testing mode
weight_file = "../pre-trained/morph2/ssrnet_3_3_3_64_1.0_1.0/ssrnet_3_3_3_64_1.0_1.0.h5"
weight_file_gender = "../pre-trained/wiki_gender_models/ssrnet_3_3_3_64_1.0_1.0/ssrnet_3_3_3_64_1.0_1.0.h5"
face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface_improved.xml')
try:
os.mkdir('./img')
except OSError:
pass
# load model and weights
img_size = 64
stage_num = [3,3,3]
lambda_local = 1
lambda_d = 1
model = SSR_net(img_size,stage_num, lambda_local, lambda_d)()
model.load_weights(weight_file)
model_gender = SSR_net_general(img_size,stage_num, lambda_local, lambda_d)()
model_gender.load_weights(weight_file_gender)
# capture video
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1024*1)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768*1)
img_idx = 0
detected = '' #make this not local variable
time_detection = 0
time_network = 0
time_plot = 0
skip_frame = 5 # every 5 frame do 1 detection and network forward propagation
ad = 0.5
while True:
# get video frame
ret, input_img = cap.read()
img_idx = img_idx + 1
img_h, img_w, _ = np.shape(input_img)
if img_idx==1 or img_idx%skip_frame == 0:
time_detection = 0
time_network = 0
time_plot = 0
# detect faces using LBP detector
gray_img = cv2.cvtColor(input_img,cv2.COLOR_BGR2GRAY)
start_time = timeit.default_timer()
detected = face_cascade.detectMultiScale(gray_img, 1.1)
elapsed_time = timeit.default_timer()-start_time
time_detection = time_detection + elapsed_time
faces = np.empty((len(detected), img_size, img_size, 3))
input_img,time_network,time_plot = draw_results(detected,input_img,faces,ad,img_size,img_w,img_h,model,model_gender,time_detection,time_network,time_plot)
cv2.imwrite('img/'+str(img_idx)+'.png',input_img)
else:
input_img,time_network,time_plot = draw_results(detected,input_img,faces,ad,img_size,img_w,img_h,model,model_gender,time_detection,time_network,time_plot)
#Show the time cost (fps)
print('avefps_time_detection:',1/time_detection)
print('avefps_time_network:',skip_frame/time_network)
print('avefps_time_plot:',skip_frame/time_plot)
print('===============================')
key = cv2.waitKey(1)
if __name__ == '__main__':
main()
|
nilq/baby-python
|
python
|
#!/usr/bin/env python3
import sys
assert_statement = "Requires Python{mjr}.{mnr} or greater".format(
mjr='3',
mnr='4')
assert sys.version_info >= (3, 4), assert_statement
from groupby import main
if __name__ == '__main__':
main()
|
nilq/baby-python
|
python
|
import importlib
from contextlib import ContextDecorator
from django.conf import settings
## add those to settings.py
##
## MOCKABLE_NAMES = {
## "MockableClassName": {
## "test": 'path.to.some.ServiceMockError',
## "development": 'path.to.some.ServiceMockOk',
## # "production": 'please.dont.mock.Me',
## },
## }
## ENVIRONMENT = "local"
class Mockable:
"""Abstract class for when we need to mock something in test envinronments.
When the class is instanciated, if settings say so, it shall return a mock instead.
"""
__unmocked = []
def __new__(cls, *args, **kwargs):
"returns an instance... probably."
mock = cls.__get_mock()
return object.__new__(mock, *args, **kwargs)
@classmethod
def __get_mock(cls):
'returns the mocked class if it should'
if cls.__name__ in cls.__unmocked:
return cls
full_path = settings.MOCKABLE_NAMES.get(cls.__name__, {}).get(settings.ENVIRONMENT, None)
if full_path is None:
return cls
return cls.__get_module(full_path)
@classmethod
def __get_module(cls, full_path: str):
'get a module from full_path'
path = full_path.split('.')
module = importlib.import_module(".".join(path[:-1]))
return getattr(module, path[-1])
@classmethod
def get_unmocked(cls, *args, **kwargs):
'gets an instance of this classs, ignoring mocks'
with unmock(cls):
return cls(*args, **kwargs)
@classmethod
def unmock(cls):
"Returns the 'unmock' decorator for this class"
return unmock(cls)
class unmock(ContextDecorator):
"""Decorator and context manager for unmocking a Mocked class.
Use this if settings says to mock something, but you really need the real one."""
def __init__(self, cls):
if issubclass(cls, Mockable):
self.class_name = cls.__name__
elif isinstance(cls, str):
self.class_name = cls
else:
raise ValueError("cls param should be a Mockable subclass or string value.")
def __enter__(self):
Mockable.__unmocked.append(self.class_name)
return self
def __exit__(self):
Mockable.__unmocked.remove(self.class_name)
return False
|
nilq/baby-python
|
python
|
import torch
from torch import nn
from PVANet import PVANet
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Encoder(nn.Module):
def __init__(self, in_channels, out_channels,feature_size=20):
super(Encoder, self).__init__()
self.cnn = PVANet(in_channels, out_channels)
self.adaptive_pool = nn.AdaptiveAvgPool2d((feature_size, feature_size))
def forward(self, input):
output = self.cnn(input)
output = self.adaptive_pool(output)
output = output.permute(0, 2, 3, 1)
return output
class Attention(nn.Module):
"""
args:
encoder_dim: encoder output dim
decoder_dim: decoder hidden layers dim
converge_vector_channel: channel of vector channel, always equal size of encoder output
converge_vector_dim: converge vector dim
attention_dim: attention dim
inputs:
encoder_out: encoder output in size(batch size * size^2 * channels)
decoder_hidden: decoder hidden states in size(batch size * channels)
converge_vector_dim: sum of alphas of all past time, equals to 0 at start, in size(batch size * encoder feature size^2)
outputs:
context: attention context in size(batch size * encoder dim)
alpha: softmax of weights of encoder feature insize(batch size * encoder size^2)
"""
def __init__(self, encoder_dim, decoder_dim, converge_vector_channel, converge_vector_dim, attention_dim=256):
super(Attention, self).__init__()
self.encoder_att = nn.Linear(encoder_dim, attention_dim)
self.decoder_att = nn.Linear(decoder_dim, attention_dim)
self.converge = nn.Linear(converge_vector_channel, converge_vector_dim)
self.converge_att = nn.Linear(converge_vector_dim, attention_dim)
self.tanh = nn.Tanh()
self.full_att = nn.Linear(attention_dim, 1)
self.softmax = nn.Softmax(dim=1)
def forward(self, encoder_out, decoder_hidden, converge_vector):
att1 = self.encoder_att(encoder_out)
att2 = self.decoder_att(decoder_hidden)
if sum(sum(converge_vector)).item() != 0:
converge_vector = self.converge(converge_vector)
att3 = self.converge_att(converge_vector)
att = self.full_att(self.tanh(att1 + att2.unsqueeze(1) + att3.unsqueeze(1))).squeeze(2)
else:
att = self.full_att(self.tanh(att1 + att2.unsqueeze(1))).squeeze(2)
# att size (batch_size, encoder_feature_length)
alpha = self.softmax(att)
context = (encoder_out * alpha.unsqueeze(2)).sum(dim=1)
return context, alpha
class Decoder(nn.Module):
def __init__(self, attention_dim, embed_dim, decoder_dim, vocab_size, encoder_dim=512, encoder_fsize = 20,
converge_vector_dim = 256, dropout=0.5, embedding_dropout=0.5):
super(Decoder, self).__init__()
self.encoder_dim = encoder_dim
self.attention_dim = attention_dim
self.embed_dim = embed_dim
self.decoder_dim = decoder_dim
self.vocab_size = vocab_size
self.encoder_fsize = encoder_fsize
self.encoder_fl = encoder_fsize*encoder_fsize
self.converge_vector_dim = converge_vector_dim
self.dropout = dropout
self.embedding_dropout = embedding_dropout
self.attention = Attention(self.encoder_dim, self.decoder_dim, self.encoder_fl, self.converge_vector_dim,
self.attention_dim)
self.embeddimg = nn.Embedding(vocab_size, self.embed_dim)
self.embedding_dropout = nn.Dropout(p=self.embedding_dropout)
self.dropout = nn.Dropout(p=self.dropout)
self.gru1 = nn.GRUCell(self.embed_dim, decoder_dim, bias=True)
self.gru2 = nn.GRUCell(self.encoder_dim, decoder_dim, bias=True)
self.s = nn.Linear(self.encoder_dim, self.decoder_dim)
self.fc = nn.Linear(self.decoder_dim, self.vocab_size)
self.init_weights()
def init_weights(self):
self.embeddimg.weight.data.uniform_(-0.1, 0.1)
self.fc.bias.data.fill_(0)
self.fc.weight.data.uniform_(-0.1,0.1)
def load_pretrained_embeddings(self, embeddings):
self.embedding.weight = nn.Parameter(embeddings)
def init_hidden_state(self, encoder_out):
mean_encoder_out = encoder_out.mean(dim=1)
s = self.s(mean_encoder_out)
return s
def decode_step(self, embedding_word, s, encoder_out, converge_vector):
# gru cell
st_hat = self.gru1(embedding_word, s)
context, alpha = self.attention(encoder_out, s, converge_vector)
st = self.gru2(context, st_hat)
# sum of history converge vector
converge_vector = converge_vector + alpha
# embedding predict word
preds = self.fc(self.dropout(st))
preds_words = preds.topk(1)[1].squeeze()
embedding_word = self.embeddimg(preds_words)
embedding_word = self.embedding_dropout(embedding_word)
embedding_word = embedding_word.view(-1, self.embed_dim)
return embedding_word, st, converge_vector, preds, alpha
def forward(self, encoder_out, encoded_captions, caption_lengths):
batch_size = encoder_out.size(0)
encoder_dim = encoder_out.size(-1)
vocab_size = self.vocab_size
#Flatten image
encoder_out = encoder_out.view(batch_size, -1, encoder_dim)
num_pixels = encoder_out.size(1)
#sort input data by decreasing lengths
# caption_lengths, sort_ind = caption_lengths.squeeze(1).sort(dim=0, descending=True)
caption_lengths, sort_ind = caption_lengths.sort(dim=0, descending=True)
encoder_out = encoder_out[sort_ind]
encoded_captions = encoded_captions[sort_ind]
#embedding
start_word = encoded_captions[:,0]
embedding_word = self.embeddimg(start_word)
embedding_word = self.embedding_dropout(embedding_word)
#initialize GRU state
s = self.init_hidden_state(encoder_out)
#remove <eos> during decoding
# decode_lengths = (caption_lengths -1).tolist()
decode_lengths = caption_lengths.tolist()
#create tensors to hold word prediction scores and alphas
predictions = torch.zeros(batch_size, max(decode_lengths), vocab_size).to(device)
alphas = torch.zeros(batch_size, max(decode_lengths), num_pixels).to(device)
#decode by time t
for t in range(max(decode_lengths)):
batch_size_t = sum([l > t for l in decode_lengths])
embedding_word, s, converge_vector, preds, alpha = self.decode_step(embedding_word[:batch_size_t],
s[:batch_size_t],
encoder_out[:batch_size_t],
converge_vector = torch.zeros(batch_size_t, num_pixels).to(device)
if t==0 else converge_vector[:batch_size_t])
predictions[:batch_size_t, t] = preds
alphas[:batch_size_t, t] = alpha
return predictions, encoded_captions, decode_lengths, alphas, sort_ind
class Model(nn.Module):
def __init__(self, img_channels, vocab_size, encoder_dim=512, encoder_fsize=20, embed_dim=256, decoder_dim=256,
attention_dim=256, converge_vector_dim=256, dropout=0.5, embedding_dropout=0.1):
super(Model, self).__init__()
self.encoder = Encoder(img_channels, encoder_dim, encoder_fsize)
self.decoder = Decoder(attention_dim, embed_dim, decoder_dim, vocab_size, encoder_dim, encoder_fsize,
converge_vector_dim, dropout, embedding_dropout)
self.encoder.apply(self.init_weight)
self.decoder.apply(self.init_weight)
def init_weight(self, m):
# 使用isinstance来判断m属于什么类型
if isinstance(m, nn.Conv2d):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def forward(self, images, encoded_captions, caption_lengths):
encoder_out = self.encoder(images)
predictions, encoded_captions, decode_lengths, alphas, sort_ind = self.decoder(encoder_out, encoded_captions, caption_lengths)
return predictions, encoded_captions, decode_lengths, alphas, sort_ind
if __name__ == "__main__":
#already converge vector
#already not calculate padding loss
#already allow loading pretrained embeddings
#already add embedding dropout
#todo add sigmoid gate to control context
#todo add weights init methods
#todo add gradient clip
#todo try doubly stochastic regularization
#todo add teaching learning
# x = torch.Tensor(16, 3, 224, 224).to(device)
# x = Encoder(3).to(device)(x)
# x.size() #(16,512,20,20)
# encoder_out = torch.Tensor(16, 400, 512).to(device)
# decoder_hidden = torch.Tensor(16, 256).to(device)
# converge_vector = torch.Tensor(16, 400).to(device)
#
# context, alpha = Attention(512, 256, 400, 256).to(device)(encoder_out, decoder_hidden, converge_vector)
# encoder_out = torch.Tensor(16,20,20,512).to(device)
# encoded_captions = torch.ones(16,15).to(device).long()
# caption_lengths = torch.ones(16,1).to(device).long()*15
# predictions, encoded_captions, decode_lengths, alphas, sort_ind = Decoder(256, 256, 256, 13).to(device)(encoder_out, encoded_captions, caption_lengths)
images = torch.Tensor(16, 3, 224, 224).to(device)
encoded_captions = torch.ones(16,15).to(device).long()
caption_lengths = torch.ones(16, 1).to(device).long() * 15
model = Model(3, 13).to(device)
predictions, encoded_captions, decode_lengths, alphas, sort_ind = model(images, encoded_captions, caption_lengths)
|
nilq/baby-python
|
python
|
"""
Functions in this file have all been defined in the notebooks.
This file serves to allow subsequent notebooks to import
functionality and reduce code duplication.
"""
import cv2
import numpy as np
import ipyvolume as ipv
def calibrate_cameras() :
""" Calibrates cameras from chessboard images.
Returns:
images (list[np.ndarray]): Images containing the chessboard.
intrinsics (np.ndarray): An upper triangular 4x4 full-rank matrix containing camera intrinsics.
distortions (np.ndarray): Radial distortion coefficients.
rotation_vectors (list[np.ndarray]): Rodrigues rotation vectors.
translation_vectors (list[np.ndarray]): Translation vectors.
object_points: (np.ndarray): A (4, 54) point array, representing the [x,y,z,w]
of 54 chessboard points (homogenous coordiantes).
"""
images = list()
# Read images
for i in range(11):
img = cv2.imread(f'./images/{i}.jpg')
img = cv2.resize(img, None, fx=0.25, fy=0.25)
images.append(img)
# The default opencv chessboard has 6 rows, 9 columns
shape = (6, 9)
# List to store vectors of 3D world points for every checkerboard image
object_points_all = []
# List to store vectors of 2D projected points for every checkerboard image
image_points_all = []
# Flags for chessboard corner search. Taken from opencv docs.
flags = cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE
# Criteria for termination of the iterative corner refinement. Taken from opencv docs.
refinement_criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# List for images in which chessboard is be found by search
images_filtered = list()
# Object points in a single image. Simply a row iterated list of z=0 3d points.
# E.g. [[0. 0. 0.] [1. 0. 0.] ... [0, 1, 0], [1, 1, 0], ... ]
object_points = np.zeros((1, shape[0] * shape[1], 3), np.float32)
object_points[0, :, :2] = np.mgrid[0:shape[0], 0:shape[1]].T.reshape(-1, 2)
# For each image, store the object points and image points of chessboard corners.
for idx, image in enumerate(images):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
succes, corners = cv2.findChessboardCorners(image=gray,
patternSize=shape,
flags=flags)
if succes:
images_filtered.append(image)
corners = cv2.cornerSubPix(image=gray,
corners=corners,
winSize=(11,11),
zeroZone=(-1,-1),
criteria=refinement_criteria)
object_points_all.append(object_points)
image_points_all.append(corners)
images = images_filtered
# Calibrate the cameras by using the 3D <-> 2D point correspondences.
ret, intrinsics, distortions, rotation_vectors, translation_vectors = cv2.calibrateCamera(object_points_all, image_points_all, gray.shape[::-1], None, None)
# Make intrinsic matrix 4x4 full-rank to ease manipulation.
intrinsics = np.hstack([intrinsics, np.zeros((3, 1))])
intrinsics = np.vstack([intrinsics, [[0, 0, 0, 1]]])
# Convert chessboard object points to homogeneous coordinates to ease later use.
object_points = object_points[0].reshape((-1, 3)).T
object_points = np.vstack([object_points, np.ones((1, object_points.shape[1]))])
return images, intrinsics, distortions, rotation_vectors, translation_vectors, object_points
images, intrinsics, distortions, rotation_vectors, translation_vectors, object_points = calibrate_cameras()
def extrinsics_from_calibration(rotation_vectors, translation_vectors):
""" Calculates extrinsic matrices from calibration output.
Args:
rotation_vectors (list[np.ndarray]): Rodrigues rotation vectors.
translation_vectors (list[np.ndarray]): Translation vectors.
Returns:
extrinsics (list[np.ndarray]): A list of camera extrinsic matrices.
These matrices are 4x4 full-rank.
"""
rotation_matrices = list()
for rot in rotation_vectors:
rotation_matrices.append(cv2.Rodrigues(rot)[0])
extrinsics = list()
for rot, trans in zip(rotation_matrices, translation_vectors):
extrinsic = np.concatenate([rot, trans], axis=1)
extrinsic = np.vstack([extrinsic, [[0,0,0,1]]])
extrinsics.append(extrinsic)
return extrinsics
def camera_centers_from_extrinsics(extrinsics):
""" Calculates camera centers from extrinsic matrices.
Args:
extrinsics (list[np.ndarray]): A list of camera extrinsic matrices.
Returns:
camera_centers (list[np.ndarray]): Homogenous coordinates of camera centers in
3D world coordinate frame.
"""
camera_centers = list()
for extrinsic in extrinsics:
rot = extrinsic[:3, :3]
trans = extrinsic[:3, 3]
center = -rot.T @ trans
center = np.append(center, 1)
camera_centers.append(center)
return camera_centers
extrinsics = extrinsics_from_calibration(rotation_vectors, translation_vectors)
camera_centers = camera_centers_from_extrinsics(extrinsics)
cam_sphere_size = 1
def init_3d_plot():
""" Initializes a ipyvolume 3d plot and centers the
world view around the center of the chessboard. """
chessboard_x_center = 2.5
chessboard_y_center = 4
fig = ipv.pylab.figure(figsize=(15, 15), width=800)
ipv.xlim(2.5 - 30, 2.5 + 30)
ipv.ylim(4 - 30, 4 + 30)
ipv.zlim(-50, 10)
ipv.pylab.view(azimuth=40, elevation=-150)
return fig
def plot_chessboard(object_points):
""" Plots a 3D chessboard and highlights the
objects points with green spheres. """
img = cv2.imread('./images/chessboard.jpg')
img_height, img_width, _ = img.shape
chessboard_rows, chessboard_cols = 7, 10
xx, yy = np.meshgrid(np.linspace(0, chessboard_rows, img_height),
np.linspace(0, chessboard_cols, img_width))
zz = np.zeros_like(yy)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255
# -1 is used as the start of the board images x and y coord,
# such that the first inner corner appear as coord (0, 0, 0)
ipv.plot_surface(xx-1, yy-1, zz, color=cv2.transpose(img))
xs, ys, zs, _ = object_points
ipv.scatter(xs, ys, zs, size=1, marker='sphere', color='lime')
# Visual dimension of the camera in the 3D plot.
height, width, _ = images[0].shape
camera_aspect_ratio = width / height
# A length of 1 corresponds to the length of 1 chessboard cell.
# This is because a chessboard points have been defined as such.
# Set height of camera viewport to 1.
vis_cam_height = 1
vis_cam_width = vis_cam_height * camera_aspect_ratio
wire_frame_depth = 1.2
def plot_camera_wireframe(cam_center, vis_scale, inv_extrinsic, color='blue', cam_sphere_size=1):
""" Plots the 'viewport' or 'wireframe' for a camera. """
x, y, z = cam_center[:3]
# Get left/right top/bottom wireframe coordinates
# Use the inverse of the camera's extrinsic matrix to convert
# coordinates relative to the camera to world coordinates.
lt = inv_extrinsic @ np.array((-vis_cam_width/2, -vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
rt = inv_extrinsic @ np.array((vis_cam_width/2, -vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
lb = inv_extrinsic @ np.array((-vis_cam_width/2, vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
rb = inv_extrinsic @ np.array((vis_cam_width/2, vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
# Connect camera projective center to wireframe extremities
p1 = ipv.plot([x, lt[0]], [y, lt[1]], [z, lt[2]], color=color)
p2 = ipv.plot([x, rt[0]], [y, rt[1]], [z, rt[2]], color=color)
p3 = ipv.plot([x, lb[0]], [y, lb[1]], [z, lb[2]], color=color)
p4 = ipv.plot([x, rb[0]], [y, rb[1]], [z, rb[2]], color=color)
# Connect wireframe corners with a rectangle
p5 = ipv.plot([lt[0], rt[0]], [lt[1], rt[1]], [lt[2], rt[2]], color=color)
p6 = ipv.plot([rt[0], rb[0]], [rt[1], rb[1]], [rt[2], rb[2]], color=color)
p7 = ipv.plot([rb[0], lb[0]], [rb[1], lb[1]], [rb[2], lb[2]], color=color)
p8 = ipv.plot([lb[0], lt[0]], [lb[1], lt[1]], [lb[2], lt[2]], color=color)
p9 = ipv.scatter(np.array([x]), np.array([y]), np.array([z]), size=cam_sphere_size, marker="sphere", color=color)
return [p1, p2, p3, p4, p5, p6, p7, p8, p9]
def plot_picture(image, inv_extrinsic, vis_scale):
""" Plots a real world image its respective 3D camera wireframe. """
image = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, None, fx=0.1, fy=0.1) / 255
img_height, img_width, _ = image.shape
xx, yy = np.meshgrid(np.linspace(-vis_cam_width/2 * vis_scale, vis_cam_width/2 * vis_scale, img_width),
np.linspace(-vis_cam_height/2 * vis_scale, vis_cam_height/2 * vis_scale, img_height))
zz = np.ones_like(yy) * wire_frame_depth * vis_scale
coords = np.stack([xx, yy, zz, np.ones_like(zz)])
coords = coords.reshape(4, -1)
# Convert canera relative coordinates to world relative coordinates
coords = inv_extrinsic @ coords
xx, yy, zz, ones = coords.reshape(4, img_height, img_width)
return ipv.plot_surface(xx, yy, zz, color=image)
def update_camera_wireframe(cam_center, vis_scale, inv_extrinsic, old_plot):
""" Updates the camera wireframe. This allows for animating the wireframe. """
[p1, p2, p3, p4, p5, p6, p7, p8, p9] = old_plot
x, y, z = cam_center[:3]
lt = inv_extrinsic @ np.array((-vis_cam_width/2, -vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
rt = inv_extrinsic @ np.array((vis_cam_width/2, -vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
lb = inv_extrinsic @ np.array((-vis_cam_width/2, vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
rb = inv_extrinsic @ np.array((vis_cam_width/2, vis_cam_height/2, wire_frame_depth, 1/vis_scale)) * vis_scale
p1.x, p1.y, p1.z = [x, lt[0]], [y, lt[1]], [z, lt[2]]
p2.x, p2.y, p2.z = [x, rt[0]], [y, rt[1]], [z, rt[2]]
p3.x, p3.y, p3.z = [x, lb[0]], [y, lb[1]], [z, lb[2]]
p4.x, p4.y, p4.z = [x, rb[0]], [y, rb[1]], [z, rb[2]]
p5.x, p5.y, p5.z = [lt[0], rt[0]], [lt[1], rt[1]], [lt[2], rt[2]]
p6.x, p6.y, p6.z = [rt[0], rb[0]], [rt[1], rb[1]], [rt[2], rb[2]]
p7.x, p7.y, p7.z = [rb[0], lb[0]], [rb[1], lb[1]], [rb[2], lb[2]]
p8.x, p8.y, p8.z = [lb[0], lt[0]], [lb[1], lt[1]], [lb[2], lt[2]]
p9.x, p9.y, p9.z = np.array([x]), np.array([y]), np.array([z])
return [p1, p2, p3, p4, p5, p6, p7, p8, p9]
def dim(x):
""" Determines the dimensionality of an array;
A helper function for update_picture. """
d = 0
el = x
while True:
try:
el = el[0]
d += 1
except:
break
return d
def reshape(ar):
""" Reshapes an array; A helper function for update_picture. """
if dim(ar) == 3:
return [k.reshape(-1) for k in ar]
else:
return ar.reshape(-1)
def update_picture(image, inv_extrinsic, vis_scale, old_plot):
""" Updates the location of pictures within a wireframes.
This allows for animating the pictures. """
image = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, None, fx=0.1, fy=0.1) / 255
img_height, img_width, _ = image.shape
xx, yy = np.meshgrid(np.linspace(-vis_cam_width/2 * vis_scale, vis_cam_width/2 * vis_scale, img_width),
np.linspace(-vis_cam_height/2 * vis_scale, vis_cam_height/2 * vis_scale, img_height))
zz = np.ones_like(yy) * wire_frame_depth * vis_scale
coords = np.stack([xx, yy, zz, np.ones_like(zz)])
coords = coords.reshape(4, -1)
# Convert canera relative coordinates to world relative coordinates
coords = inv_extrinsic @ coords
old_color = old_plot.color.copy()
xx, yy, zz, ones = coords.reshape(4, img_height, img_width)
x = reshape(xx)
y = reshape(yy)
z = reshape(zz)
old_plot.x = x
old_plot.y = y
old_plot.z = z
return old_plot
def project_points_to_picture(image, object_points, intrinsics, extrinsic):
""" Perspective projects points to an image and draws them green. """
image = image.copy()
proj_matrix = intrinsics @ extrinsic
object_points = proj_matrix @ object_points
xs, ys, ones, disparity = object_points / object_points[2]
for idx, (x, y) in enumerate(zip(xs, ys)):
x = round(x)
y = round(y)
if (0 < y < image.shape[0] and
0 < x < image.shape[1]):
# Each point occupies a 20x20 pixel area in the image.
image[y-10:y+10, x-10:x+10] = [0, 255, 0]
return image
def triangulate(p1, p2, p3, p4):
""" Calculates the point triangulated by two lines.
Also returns that points projection onto line 1 and 2,
know as Pa and Pb in the math description in notebook 1.
"""
# Strip potential scale factor of homogenous coord
p1 = p1[:3]
p2 = p2[:3]
p3 = p3[:3]
p4 = p4[:3]
p13 = p1 - p3
p21 = p2 - p1
p43 = p4 - p3
d1321 = np.dot(p13, p21)
d1343 = np.dot(p13, p43)
d2121 = np.dot(p21, p21)
d4321 = np.dot(p43, p21)
d4343 = np.dot(p43, p43)
mu_a = (d1343 * d4321 - d1321 * d4343) / (d2121 * d4343 - d4321 * d4321)
mu_b = (d1343 + mu_a * d4321) / d4343
point_on_line_1 = p1 + mu_a * p21
point_on_line_2 = p3 + mu_b * p43
adjoining_line = point_on_line_2 - point_on_line_1
midpoint = adjoining_line / 2
triangulated_point = point_on_line_1 + midpoint
return triangulated_point, point_on_line_1, point_on_line_2
def get_stereo_setup_with_correspondences():
""" Returns all objects related to the stereo setup
presented at the end of notebook 1 for triangulating points.
"""
images, intrinsics, distortions, rotation_vectors, translation_vectors, object_points = calibrate_cameras()
camera_1_idx = 3
camera_2_idx = 0
image_1 = images[camera_1_idx].copy()
image_2 = images[camera_2_idx].copy()
# Initialize SIFT detector
sift = cv2.SIFT_create()
# Find the keypoints and descriptors with SIFT
keypoints_1, descriptors_1 = sift.detectAndCompute(image_1, None) # queryImage
keypoints_2, descriptors_2 = sift.detectAndCompute(image_2, None) # trainimage
# Match descriptors
bf = cv2.BFMatcher()
matches = bf.knnMatch(descriptors_1, descriptors_2, k=2)
# Apply ratio test
good = []
for best_match, second_best_match in matches:
if best_match.distance < 0.75 * second_best_match.distance:
good.append([best_match])
# Sort matches according to descriptor distance
dists = [g[0].distance for g in good]
good = list(sorted(zip(dists, good)))
good = [list(g) for g in zip(*good)][1]
# Select manually validated matches
hand_picked_matches = [2, 9, 15, 16, 18, 19, 22, 23, 24, 27, 28, 29, 31, 34, 40, 41, 42]
good = np.array(good, dtype=object)[hand_picked_matches]
match_coords_1 = list()
match_coords_2 = list()
for i in good:
i = i[0]
keypoint_1 = keypoints_1[i.queryIdx]
keypoint_2 = keypoints_2[i.trainIdx]
keypoint_1_center = np.array(keypoint_1.pt)
keypoint_2_center = np.array(keypoint_2.pt)
x1, y1 = keypoint_1_center
x2, y2 = keypoint_2_center
match_coords_1.append([x1, y1, 1, 1])
match_coords_2.append([x2, y2, 1, 1])
color = (np.random.rand(3) * 255).astype(int).clip(50, 255).tolist()
image1 = cv2.circle(image_1, keypoint_1_center.astype(int), 10, color, -1)
image2 = cv2.circle(image_2, keypoint_2_center.astype(int), 10, color, -1)
match_coords_1 = np.array(match_coords_1)
match_coords_2 = np.array(match_coords_2)
extrinsic_1 = extrinsics[camera_1_idx]
extrinsic_2 = extrinsics[camera_2_idx]
inv_extrinsic_1 = np.linalg.inv(extrinsic_1)
inv_extrinsic_2 = np.linalg.inv(extrinsic_2)
cam_center_1 = camera_centers[camera_1_idx]
cam_center_2 = camera_centers[camera_2_idx]
cam_x_1, cam_y_1, cam_z_1, _ = cam_center_1
cam_x_2, cam_y_2, cam_z_2, _ = cam_center_2
return [image_1, image_2], [extrinsic_1, extrinsic_2], [cam_center_1, cam_center_2], intrinsics, [match_coords_1, match_coords_2], object_points
def get_bunny():
"""Plots the Stanford bunny pointcloud and returns its points"""
bunny_coords = np.load(open('data/bunny_point_cloud.npy', 'rb')) * 2
b_xs, b_ys, b_zs = bunny_coords[:3]
b_xs -= b_xs.mean()
b_ys -= b_ys.mean()
b_zs -= b_zs.mean()
bunny_coords = np.array([b_xs, b_ys, b_zs])
return bunny_coords
def random_angle(max_angle):
""" Returns a random angle in radians. """
rad = np.radians(max_angle)
rand_factor = np.random.uniform(low=-1, high=1)
return rad * rand_factor
def random_rotation(max_angle=100):
""" Returns a matrix for random rotation around x, y, and z axis. """
t_x = random_angle(max_angle)
t_y = random_angle(max_angle)
t_z = random_angle(max_angle)
r_x = np.array([[1, 0, 0],
[0, np.cos(t_x), -np.sin(t_x)],
[0, np.sin(t_x), np.cos(t_x)]])
r_y = np.array([[np.cos(t_y), 0, np.sin(t_y)],
[0, 1, 0],
[-np.sin(t_y), 0, np.cos(t_y)]])
r_z = np.array([[np.cos(t_z), -np.sin(t_z), 0],
[np.sin(t_z), np.cos(t_z), 0],
[0, 0, 1]])
return r_x @ r_y @ r_z
def random_translation(max_offset=10):
""" Returns a random translation vector. """
return np.random.uniform(low=-max_offset, high=max_offset, size=3)
def distort_extrinsics(extrinsic, max_angle=100, max_trans=5):
""" Randomly distorts an extrinsic matrix such that
the pose it represents is rotated and moved.
"""
extrinsic = extrinsic.copy()
rot = extrinsic[:3, :3]
trans = extrinsic[3, :3]
rand_rot = random_rotation(max_angle)
rand_trans = random_translation(max_trans)
extrinsic[:3, :3] = rand_rot @ extrinsic[:3, :3]
extrinsic[:3, 3] = extrinsic[:3, 3] + rand_trans
return extrinsic
|
nilq/baby-python
|
python
|
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
class WebDelegate:
def __init__(self, parser_engine=BeautifulSoup):
#TO-DO: default parser engine은 BeautifulSoup. 필요시 추가.
self.__parser_engine=parser_engine
def get_web_data(self, addr):
req = Request(addr, headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req).read().decode('utf-8','replace')
data = self.__parser_engine(html, "html.parser")
return data
def check_url_alive(self, addr):
try:
req = Request(addr, headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req)
if html.status >= 300: # 3xx Redirection부터 에러 처리
return False
self.get_web_data(addr)
except Exception as e:
print("Exception access url : %s" % e)
print("We can not scrap %s, something wrong.\n" % addr)
return False
return True
|
nilq/baby-python
|
python
|
from solution import Solution
# MAIN TESTING PROGRAM:
m = 5
n = 5
sln = Solution()
result = sln.unique_paths(m,n)
print('Input:')
print(m)
print(n)
print('Output:')
print(result)
|
nilq/baby-python
|
python
|
'''
Facebook Hacker Cup 2017
(Qualification Round) problem 2 : https://www.facebook.com/hackercup/problem/169401886867367/
Lazy Loader
'''
import sys
DEBUG = 1
TESTCASE = 'input/lazy_loading.txt'
def maxTrips(a):
a = sorted(a)
trips = 0
while len(a) > 0:
w = a.pop()
k = w
while w <= 50 and len(a) > 0:
a.pop(0)
w += k
if w >= 50:
trips += 1
return trips
def parse():
if DEBUG:
with open(TESTCASE) as f:
lines = f.readlines()
else:
lines = sys.stdin.readlines()
length = int(lines.pop(0).strip('\n'))
result = []
for i in range(length):
tmp = []
l = int(lines.pop(0).strip('\n'))
for _ in range(l):
tmp.append(lines.pop(0).strip('\n'))
result.append(tmp)
return result
if __name__ == '__main__':
for case, a in enumerate(parse(), start=1):
res = maxTrips(map(int, a))
print "Case #%d: %d" % (case, res)
|
nilq/baby-python
|
python
|
# coding=utf-8
"""
Copyright 2013 LinkedIn Corp. 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 logging
logger = logging.getLogger('naarad.sla')
class SLA(object):
supported_sla_types = ('lt', '<', 'gt', '>', 'eq', '=')
def __init__(self, metric, sub_metric, stat_name, threshold, sla_type):
if sla_type not in self.supported_sla_types:
logger.error('Unsupported sla type passed : ' + sla_type)
return None
self.metric = metric
self.sub_metric = sub_metric
self.stat_name = stat_name
self.sla_type = sla_type
self.is_processed = False
self.threshold = None
self.display = None
if '%' in threshold:
self.threshold = float(threshold.translate(None, '%'))
self.display = '%'
else:
self.threshold = float(threshold)
self.display = ''
self.sla_passed = None
self.stat_value = None
def __str__(self):
return "{0} of {1}, threshold: {2}, sla_type: {3}, sla_passed: {4}, display: {5}".format(self.stat_name, self.sub_metric, self.threshold, self.sla_type,
self.sla_passed, self.display)
def get_csv_repr(self):
return "{0},{1},{2},{3},{4},{5}".format(self.sub_metric, self.stat_name, self.threshold, self.sla_type, self.stat_value, self.sla_passed)
def check_sla_passed(self, stat_value):
if self.sla_type in ('lt', '<'):
self.grade_lt(stat_value)
elif self.sla_type in ('gt', '>'):
self.grade_gt(stat_value)
elif self.sla_type in ('eq', '='):
self.grade_eq(stat_value)
else:
logger.error('sla type is unsupported')
self.stat_value = stat_value
return self.sla_passed
def grade_lt(self, stat_value):
self.is_processed = True
if stat_value >= self.threshold:
self.sla_passed = False
else:
self.sla_passed = True
def grade_gt(self, stat_value):
self.is_processed = True
if stat_value <= self.threshold:
self.sla_passed = False
else:
self.sla_passed = True
def grade_eq(self, stat_value):
self.is_processed = True
if stat_value == self.threshold:
self.sla_passed = True
else:
self.sla_passed = False
|
nilq/baby-python
|
python
|
"""
Json for config file. Added supports for comments and expandable keywords.
"""
import copy
import json
import os
RECURRENT_OBJECT_TYPES = (dict, list)
# Identifier key to import another json file,
# work as prefix, allowing "INCLUDE_KEY_1", "INCLUDE_KEY_2"...
INCLUDE_KEY = '_include_json'
# There may be performance suffer backtracking the last comma
def _remove_last_comma(str_list, before_index):
i = before_index - 1
while str_list[i].isspace() or not str_list[i]:
i -= 1
# This is the first none space char before before_index
if str_list[i] == ',':
str_list[i] = ''
def _dispose_comments(json_str: str):
"""Clear C/JS-style comments like // and /**/ in json_str.
Args:
json_str (str): json string to clean up comment
Returns:
str: json_str without comments
"""
result_str = list(json_str)
escaped = False
normal = True
sl_comment = False
ml_comment = False
quoted = False
a_step_from_comment = False
a_step_from_comment_away = False
former_index = None
for index, char in enumerate(json_str):
if escaped: # We have just met a '\'
escaped = False
continue
if a_step_from_comment: # We have just met a '/'
if char != '/' and char != '*':
a_step_from_comment = False
normal = True
continue
if a_step_from_comment_away: # We have just met a '*'
if char != '/':
a_step_from_comment_away = False
if char == '"':
if normal and not escaped:
# We are now in a string
quoted = True
normal = False
elif quoted and not escaped:
# We are now out of a string
quoted = False
normal = True
elif char == '\\':
# '\' should not take effect in comment
if normal or quoted:
escaped = True
elif char == '/':
if a_step_from_comment:
# Now we are in single line comment
a_step_from_comment = False
sl_comment = True
normal = False
former_index = index - 1
elif a_step_from_comment_away:
# Now we are out of comment
a_step_from_comment_away = False
normal = True
ml_comment = False
for i in range(former_index, index + 1):
result_str[i] = ""
elif normal:
# Now we are just one step away from comment
a_step_from_comment = True
normal = False
elif char == '*':
if a_step_from_comment:
# We are now in multi-line comment
a_step_from_comment = False
ml_comment = True
normal = False
former_index = index - 1
elif ml_comment:
a_step_from_comment_away = True
elif char == '\n':
if sl_comment:
sl_comment = False
normal = True
for i in range(former_index, index + 1):
result_str[i] = ""
elif char == ']' or char == '}':
if normal:
_remove_last_comma(result_str, index)
# To remove single line comment which is the last line of json
if sl_comment:
sl_comment = False
normal = True
for i in range(former_index, len(json_str)):
result_str[i] = ""
# Show respect to original input if we are in python2
return ("" if isinstance(json_str, str) else u"").join(result_str)
def _json_walker(json_obj, **kwargs):
"""Expand sub jsonc files in jsonc object
Args:
json_obj (dict or list): json object loaded from files
"""
# cache to update after walking finished
to_update = []
if isinstance(json_obj, dict):
for k, v in json_obj.items():
# support multiple include suffix, e.g. "include_key1", "include_key2"...
if k.startswith(INCLUDE_KEY):
base_path = kwargs.get('base_path', '')
if v.startswith('/'):
file_path = v
else:
file_path = os.path.join(base_path, v)
with open(file_path, 'r') as fp:
kwargs['base_path'] = os.path.dirname(fp.name)
to_update.append(
_json_include(
fp.read(),
inside_include=True,
**kwargs
)
)
elif isinstance(v, RECURRENT_OBJECT_TYPES):
_json_walker(v, **kwargs)
elif isinstance(json_obj, list):
for i in json_obj:
if isinstance(i, RECURRENT_OBJECT_TYPES):
_json_walker(i, **kwargs)
for i in to_update:
json_obj.update(i)
def _update_walker(d: dict, u: dict):
"""Similar to dict update in python, but apply recursively
TODO this is rather a quick implementation for keep_top_values, consider to optimize it @p2
Args:
d (dict): dict to be updated
u (dict): dict that apply to d
Returns:
dict: updated dict d
"""
if isinstance(u, dict):
assert isinstance(d, dict), 'Two dicts in _update should be the same type'
for k, v in u.items():
if isinstance(v, dict):
d[k] = _update_walker(d.get(k, {}), v)
elif isinstance(v, list):
d[k] = _update_walker(d.get(k, []), v)
else:
d[k] = v
if isinstance(u, list):
assert isinstance(d, list), 'Two objects in _update should be the same type'
for i, j in zip(d, u):
_update_walker(i, j)
return d
def _json_include(text: str, inside_include=False, keep_top_values=True, base_path='', **kwargs):
"""Build jsonc object from text
Args:
text (str): loaded text from jsonc file
inside_include (bool, optional): means this function is not top level _json_include call. Defaults to False.
keep_top_values (bool, optional): duplicated sub json key will be overwritten. Defaults to True.
base_path (str): base path for relative includes sub jsonc. Defaults to empty string.
Returns:
dict: loaded jsonc dict
"""
d = json.loads(_dispose_comments(text), **kwargs)
d_orignal = {}
if keep_top_values:
# cache the original file to prevent included file modifing original values
d_orignal = copy.deepcopy(d)
if inside_include:
assert isinstance(d, dict),\
'The JSON file being included should always be a dict rather than a list'
# update missing values from included files
_json_walker(d, base_path=base_path)
if keep_top_values:
# recover the original values from top files
_update_walker(d, d_orignal)
return d
def _remove_include_key(json_obj):
"""Remove the INCLUDE_KEY in the loaded json object
Args:
json_obj (dict or list): jsonc object to be modified
"""
to_del = []
if isinstance(json_obj, dict):
for k, v in json_obj.items():
if k.startswith(INCLUDE_KEY):
to_del.append(k)
elif isinstance(v, RECURRENT_OBJECT_TYPES):
_remove_include_key(v)
elif isinstance(json_obj, list):
for i in json_obj:
if isinstance(i, RECURRENT_OBJECT_TYPES):
_remove_include_key(i)
for i in to_del:
del json_obj[i]
# Below are just some wrapper function around the standard json module,
# note that not all original kwargs are tested.
def loads(text, remove_include_key=False, base_path='', **kwargs):
d = _json_include(text, keep_top_values=True, base_path=base_path, **kwargs)
if remove_include_key:
_remove_include_key(d)
return d
def load(fp, remove_include_key=False, **kwargs):
return loads(fp.read(), remove_include_key=remove_include_key, base_path=os.path.dirname(fp.name), **kwargs)
def dumps(obj, **kwargs):
return json.dumps(obj, **kwargs)
def dump(obj, fp, **kwargs):
json.dump(obj, fp, **kwargs)
|
nilq/baby-python
|
python
|
# Module name: user
# from package: smart_scheduler_tools
# Used Modules: basic_structures_definitions, code_plus_section_list_generator,
# code_plus_section_set_filter, subject_list_to_dictionary, dict_from_json
# Description: Its the most fundamental class of the application, it uses all the
# basic structures and it stores the user info, this info includes: Subribed subjects,
# name, posible schedules.
# Last Modified: 03/10/2019
# by: LFC & TAM
from smart_scheduler_tools.basic_structures.basic_structures_definitions import Schedule
from smart_scheduler_tools.basic_structures.basic_structures_definitions import Subject
from smart_scheduler_tools.user_subroutines.code_plus_section_list_generator import generate_code_plus_section_list
from smart_scheduler_tools.user_subroutines.code_plus_section_set_filter import *
from smart_scheduler_tools.user_subroutines.subject_list_to_dictionary import subject_list_to_dict
from smart_scheduler_tools.user_subroutines.dict_from_json import get_dict_from_json_subject_database
class User:
'User who contains info about wanted subjects.'
def __init__(self, name, subjects=[]):
self.name = name
self.subjects = subjects # subject's list
self.schedule_options = {} # the keys are the number of overlaps in the possible schedules
# In: self user, subject object, add/remove flag
# Out: the self.subjects list is updated
# by LFC & TAM
def change_subjects(self, subject, new_state):
length = len(self.subjects)
for i in range(length):
if self.subjects[i].code == subject.code:
if new_state == 0: # remove the subject
self.subjects.pop(i)
elif new_state == 1:
self.subjects[i] = subject
return
if new_state == 1: # add the subject
self.subjects.append(subject)
# In: self user, subject code (Ex: DEF101)
# Out: it stores in self.subjects list the subject taken from
# the subjects database
# by LFC & TAM
def load_subject_from_json(self, code):
subjects_database = get_dict_from_json_subject_database()
new_schedule_options = {}
if code in subjects_database:
# check if the subject is within the subject list to replace it
for i in range(len(self.subjects)):
if self.subjects[i].code == code:
self.subjects.pop(i)
# then we add the new subject
for section in subjects_database[code]:
new_schedule_options[int(section)] = Schedule(subjects_database[code][section])
else:
return
new_subject = Subject(code, new_schedule_options)
self.subjects.append(new_subject)
# In: self user
# Out: fills the self.schedule_options dictionary with possible schedules, organized
# by the amount of overlaps (the keys of the dictionary are the overlaps)
# by LFC & TAM
def compute_possible_schedules(self):
code_plus_section_list = generate_code_plus_section_list(self.subjects)
power_set = compute_power_set(code_plus_section_list)
filtered_set = filter_set(power_set, len(self.subjects))
subject_as_dict = subject_list_to_dict(self.subjects)
possible_schedule = Schedule()
for subject_combination in filtered_set:
for code_plus_section in subject_combination:
[code, section] = code_plus_section.split("_")
schedule_to_add = subject_as_dict[code][int(section)]
possible_schedule.add_schedule(schedule_to_add)
possible_schedule.compute_overlaps()
new_schedule = Schedule(possible_schedule.data)
if possible_schedule.overlaps in self.schedule_options:
self.schedule_options[possible_schedule.overlaps].append(new_schedule)
else:
self.schedule_options[possible_schedule.overlaps] = [new_schedule]
possible_schedule.clear()
|
nilq/baby-python
|
python
|
from preprocessor.Text_File import Text_File, Log_File
class Bibtex_File(Text_File):
"""
Examples:
>>> # Instantiation
>>> my_bibtex_file = Bibtex_File('example_data//vu_25_test.bib')
>>> # Invalid formatting of input directory
>>> try:
... # input directory path cannot contain single slashes
... my_bibtex_file = Bibtex_File('example_data/vu_25_test.bib')
... except Exception as error_message:
... print('Exception: ' + str(error_message))
Exception: Invalid path: Path contains "/" as directory separator, and should be replaced with "//".
"""
def __init__(self, input_file_path):
Text_File.__init__(self, input_file_path)
self.no_of_nonparsable_entries_due_to_unknown_reason = 0
self.no_of_unbalanced_entries_skipped = 0
def convert_to_ttl(self, desired_version_suffix, desired_source_bibliography_name, output_directory='',
show_progress_bar=True):
"""
Takes a bib file and outputs a .ttl file.
Args:
desired_version: Version name to be added ttl file that will be outputted
(e.g., my_bibliography.bib --> my_bibliography_2.1.ttl)
desired_source_label: The bibliography source information that be attached to each entry
(e.g., ex:my_article ==> ex:hasOriginBibliography ==> ex:opencitations)
Returns:
Nothing
Also see:
long_tests()
Examples:
>>> my_bibtex_file = Bibtex_File('example_data//vu_25_test.bib')
>>> my_bibtex_file.convert_to_ttl(desired_version_suffix='0.0.test', desired_source_bibliography_name='arbitrary_label',
... output_directory='example_data//example_output_dir',
... show_progress_bar=False)
Cleaning of "example_data//vu_25_test.bib" started
Cleaning of "example_data//vu_25_test.bib" finished
Parsing of example_data//vu_25_test_cleaned.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//vu_25_test_cleaned.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_author_labels': 2,
'b_authors': 2,
'b_document': 2,
'b_document_label': 2,
'b_note': 2,
'b_publication_year': 2,
'b_pure_bibliography_id': 2,
'b_type': 2}
<BLANKLINE>
<BLANKLINE>
Calculating the length of the Triples object
Writing of the triples to file "example_data//example_output_dir//vu_25_test_0.0.test.ttl" has started
Success: 53 triples were written to "example_data//example_output_dir//vu_25_test_0.0.test.ttl"
These items were skipped due to errors (0 items):
<BLANKLINE>
A log of the operation is kept in "log.txt"
>>> my_ttl_file = Text_File('example_data//example_output_dir//vu_25_test_0.0.test.ttl')
>>> my_ttl_file.preview(50)
<http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isAuthorOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasAuthor> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOn> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedBy> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnYear> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnMonth> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnDate> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasDOI> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasISSN> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasISBN> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasPureBibliographyID> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasOpenCitationsID> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isChapterOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/2000/01/rdf-schema#label> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasTopic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasAbstract> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasCited> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isCitedBy> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/2002/07/owl#equivalentClass> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasOriginBibliography> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#Topic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#arbitrary_label> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#arbitrary_label> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/resource#Bibliography> .
<http://clokman.com/kfir/ontology#JournalArticle> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#Book> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#BookChapter> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#Miscellaneous> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#arbitrary_label> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/2000/01/rdf-schema#label> "Geloof en rechtvaardiging"@en .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Agteresch_HJ> .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://www.w3.org/2000/01/rdf-schema#label> "Agteresch, HJ"@en .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2023" .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "a1f8850ca82a4fb89aab8db2a49f8fa1" .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#arbitrary_label> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/2000/01/rdf-schema#label> "Gereformeerde katholiciteit in de zeventiende eeuw"@en .
<http://clokman.com/kfir/resource#Hartevelt_LDA> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Hartevelt_LDA> .
<http://clokman.com/kfir/resource#Hartevelt_LDA> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
>>> # Invalid formatting of output directory
>>> my_bibtex_file = Bibtex_File('example_data//vu_25_test_0.0.test.bib')
>>> try:
... # output directory path cannot contain single slashes
... my_bibtex_file.convert_to_ttl(desired_version_suffix='0.0-test', desired_source_bibliography_name='vu',
... output_directory='example_data/example_output_dir')
... except Exception as error_message:
... print('Exception: ' + str(error_message))
Exception: Invalid path: Path contains "/" as directory separator, and should be replaced with "//".
>>> # a string with spaces entered as value for desired_source_bibliography_name parameter
>>> my_bibtex_file = Bibtex_File('example_data//vu_25_test.bib')
>>> my_bibtex_file.convert_to_ttl(desired_version_suffix='v0.0.test2',
... desired_source_bibliography_name='bib name with spaces',
... output_directory='example_data//example_output_dir',
... show_progress_bar=False)
Cleaning of "example_data//vu_25_test.bib" started
Cleaning of "example_data//vu_25_test.bib" finished
Parsing of example_data//vu_25_test_cleaned.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//vu_25_test_cleaned.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_author_labels': 2,
'b_authors': 2,
'b_document': 2,
'b_document_label': 2,
'b_note': 2,
'b_publication_year': 2,
'b_pure_bibliography_id': 2,
'b_type': 2}
<BLANKLINE>
<BLANKLINE>
Calculating the length of the Triples object
Writing of the triples to file "example_data//example_output_dir//vu_25_test_v0.0.test2.ttl" has started
Success: 53 triples were written to "example_data//example_output_dir//vu_25_test_v0.0.test2.ttl"
These items were skipped due to errors (0 items):
<BLANKLINE>
A log of the operation is kept in "log.txt"
>>> my_ttl_file = Text_File('example_data//example_output_dir//vu_25_test_v0.0.test2.ttl')
>>> my_ttl_file.print_lines(35)
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/2000/01/rdf-schema#label> "Geloof en rechtvaardiging"@en .
>>> # see bottom of this file for longer tests
"""
import os
from unidecode import unidecode
from preprocessor.string_tools import Parameter_Value, File_Path, String
from triplicator.rdfTools import Triples, RDF_File
from triplicator.bibTools import Bibliography # refers to own module, but not redundant—required by force_type
# method used in Triples.import_bibliography_object()
log_file = Log_File('log.txt')
log_file.clear_contents()
# Patterns to clean from bib files
pattern_replacements_dictionary = {
'<': '--',
'>': '--',
'\{"\}': "'", # to replace {"} with '
'\\\\': '--', # to remove '\' in expressions such as '\sqrt{s}' and rogue '\'s. unsure why '\\' does not work
'“': "'",
'”': "'",
'’': "'"
}
# Error handling for output_directory and desired_version_suffix parameters
Parameter_Value(output_directory).force_type(str)
File_Path(output_directory).raise_error_if_single_slash()
Parameter_Value(desired_version_suffix).force_type(str)
# Error handling and cleaning of 'desired_source_bibliography_name' parameter
Parameter_Value(desired_source_bibliography_name).force_type(str)
# TODO: The following parameter cleaning procedure should be extracted as a method of String class.
# the cleaning is done manually here as this item is injected by Triples() instance, and thus, is not
# cleaned with bibliography cleaning process beforehand. As a result, if a parameter with illegal characters
# is entered and had not been cleaned as below, this could lead to malformed URI's or unreadable ttl files.
desired_source_bibliography_name = String(desired_source_bibliography_name). \
clean_from_newline_characters(). \
replace_patterns(pattern_replacements_dictionary).\
replace_patterns({' ': "_"}) # spaces are not cleared by default for all bibliography entries such as labels,
# so it must be taken care of individually here
desired_source_bibliography_name.clean_from_non_ascii_characters()
desired_source_bibliography_name.clean_from_non_uri_safe_characters()
desired_source_bibliography_name = desired_source_bibliography_name.content # convert String to str
### Clean the bib file ###
self.clean_bibtex_file_and_write_output_to_another_file(patterns_to_replace=pattern_replacements_dictionary,
show_progress_bar=show_progress_bar)
### Parse the bib file ###
bibliography = Bibliography()
bibliography.importBibtex(self.cleaned_file_path, show_progress_bar=show_progress_bar)
### Convert to n3 format ###
triples = Triples()
triples.import_bibliography_object(bibliography,
desired_source_bibliography_name=desired_source_bibliography_name,
show_progress_bar=show_progress_bar)
### Write to .ttl file
if output_directory and (not os.path.exists(output_directory)):
os.makedirs(output_directory)
if output_directory:
output_directory_to_prepend = output_directory + '//'
else:
output_directory_to_prepend = ''
ttl_file_path = output_directory_to_prepend + self.input_file_name + '_' + desired_version_suffix + '.ttl'
ttl_file = RDF_File(ttl_file_path)
ttl_file.write_triples_to_file(triples, show_progress_bar=show_progress_bar)
def clean_bibtex_file_and_write_output_to_another_file(self, convert_to_ascii=True, patterns_to_replace={'': ''},
show_progress_bar=False):
"""
Examples:
### CLEANING ###############################################################################################
>>> # init and preview targets
>>> my_unclean_file = Bibtex_File('example_data//problematic_characters_test.bib')
>>> my_unclean_file.print_lines(46)
title = "Contribution to {"}Multimedia as bridges for language and literacy for young children{"}, SSSR:: Do multimedia in digital storybooks contribute to vocabulary development and which features are particularly supportive?",
>>> my_unclean_file.print_lines(32)
title = "Test of CP invariance in Z ---> mu+ mu- gamma decay",
>>> #remove unbalanced entries and clean specified patterns
>>> my_unclean_file.clean_bibtex_file_and_write_output_to_another_file(patterns_to_replace={'\{"\}': "'",
... '>': '',
... '<': ''})
Cleaning of "example_data//problematic_characters_test.bib" started
Cleaning of "example_data//problematic_characters_test.bib" finished
>>> # view results
>>> my_cleaned_file = Bibtex_File('example_data//problematic_characters_test_cleaned.bib')
>>> my_cleaned_file.print_lines(22) # line 46 is now line 22 because unbalanced entries excluded in output
title = "Contribution to 'Multimedia as bridges for language and literacy for young children', SSSR:: Do multimedia in digital storybooks contribute to vocabulary development and which features are particularly supportive?",
>>> # init and preview targets
>>> my_unclean_file = Bibtex_File('example_data//problematic_characters_test.bib')
>>> my_unclean_file.print_lines(46)
title = "Contribution to {"}Multimedia as bridges for language and literacy for young children{"}, SSSR:: Do multimedia in digital storybooks contribute to vocabulary development and which features are particularly supportive?",
>>> my_unclean_file.print_lines(32)
title = "Test of CP invariance in Z ---> mu+ mu- gamma decay",
>>> # This test disabled because currently all unbalanced entries are being cleaned
>>> ##do NOT remove unbalanced entries but clean specified patterns
>>> #my_unclean_file.clean_bibtex_file_and_write_output_to_another_file(remove_unbalanced_entries=False,
#... patterns_to_replace={'\{"\}': "'",
#... '>': '',
#... '<': ''})
>>> # view results
>>> #my_cleaned_file = Text_File('example_data//problematic_characters_test_cleaned.bib')
>>> #my_cleaned_file.print_lines(46) # line 46 is still in same place because unbalanced entries not excluded
title = "Contribution to 'Multimedia as bridges for language and literacy for young children', SSSR:: Do multimedia in digital storybooks contribute to vocabulary development and which features are particularly supportive?",
>>> #my_cleaned_file.print_lines(32) # line 32 is still in same plac because unbalanced entries not excluded
title = "Test of CP invariance in Z --- mu+ mu- gamma decay",
### BALANCING ##############################################################################################
>>> my_file = Bibtex_File('example_data//problematic_characters_test.bib')
>>> # unbalanced curly bracket in 'title' field
>>> my_file.print_lines(1,12)
% UNCLOSED CURLY BRACKET
% This entry will cause an EOF error due to the unclosed curly bracket in the title field values.
@book{a82caf00e1a143759c7f5543b6c84ea5,
title = "{Knowledge Representation for Health Care (AIME 2015 International Joint Workshop, KR4HC/ProHealth 2015)",
author = "D Riano and R. Lenz and S Miksch and M Peleg and M. Reichert and {ten Teije}, A.C.M.",
year = "2015",
doi = "10.1007/978-3-319-26585-8",
isbn = "9783319265841",
series = "LNAI",
publisher = "Springer",
number = "9485",
}
>>> # unbalanced > in 'title' field.
>>> my_file.print_lines(31,41)
@article{79948f66cc82409a8978d14c9131346a,
title = "Test of CP invariance in Z ---> mu+ mu- gamma decay",
author = "M. Acciarri and O. Adriani and M. Aguilar-Benitez and S.P. Ahlen and J. Alcaraz and G. Alemanni and J. Allaby and A. Aloisio and F.L. Linde",
year = "1998",
doi = "10.1016/S0370-2693(98)00965-4",
volume = "436",
pages = "428--436",
journal = "Physics Letters B",
issn = "0370-2693",
publisher = "Elsevier",
}
>>> # faulty entry is not included in the cleaned file. Now there is another entry in its place.
>>> my_file.clean_bibtex_file_and_write_output_to_another_file()
Cleaning of "example_data//problematic_characters_test.bib" started
Cleaning of "example_data//problematic_characters_test.bib" finished
>>> my_cleaned_file = Bibtex_File('example_data//problematic_characters_test_cleaned.bib')
>>> my_cleaned_file.print_lines(1,7)
@book{a350c3826d05484cb863e77166d6e17b,
title = "Proceedings of Console IX",
keywords = "international",
author = "C. Czinglar and K. K?hler and {van der Torre}, E.J. and K.E. Thrift and M. Zimmermann",
year = "2000",
publisher = "Kluwer",
}
>>> # faulty entry is not included in the cleaned file. Now there is another entry in its place.
>>> my_cleaned_file.print_lines(29,42)
@article{96d9add3e2f44e8abbf030170689bc30,
title = "When and where did the great recession erode the support of democracy?{"}",
abstract = "It is likely that ten years of economic crisis have eroded the support of democracy in Europe. But how much? The existing research is divided on this issue. Some claim that the degree of satisfaction with democracy has declined across the whole of Europe during the Great Recession. Other researchers have found no empirical evidence that the support of democracy as a core value has declined across Europe. They claim that merely the specific support has decreased in some countries. This article will use the data from the European Social Survey to verify both claims. It shows that the Great Recession did not lead to a legitimacy crisis of European democracies and that the diffuse support of democracy remains high in most regions. The degree to which the specific support of democracy has been weakened is moderated by the type of welfare regime. In countries where the economic crisis did strike hard and the welfare state is weakly developed, the support of democracy has dropped dramatically. This outcome takes a middle position between two extremes in the ongoing academic debate on the support of democracy. Both positions regarding the increase or decrease of support of and satisfaction with democracy are in need of more nuance by taking into account the impact of welfare regimes. Existing research often assumes a uniform European context that shows either increasing or decreasing levels of satisfaction with democracy. Our research has shown that the response of citizens to the Great Recession has been influenced by the welfare regime.",
keywords = "Democracy, Economic crisis, Europe, Welfare state, Survey data, Quantitative methods",
author = "P.J.M. Pennings",
year = "2017",
month = "3",
volume = "17",
pages = "81--103",
journal = "Zeitschrift fur Vergleichende Politikwissenschaft",
issn = "1865-2646",
publisher = "Springer Verlag",
number = "1",
}
### ASCII CONVERSION #######################################################################################
>>> my_file = Bibtex_File('example_data//problematic_characters_test.bib')
>>> # non-ascii characters in titles
>>> my_file.print_lines(125)
title = "Networks of · / G/ ∞ queues with shot-noise-driven arrival intensities",
>>> my_file.print_lines(142)
title = "Search for heavy resonances decaying to a $Z$ boson and a photon in $pp$ collisions at $\sqrt{s}=13$ TeV with the ATLAS detector",
>>> my_file.print_lines(156)
title = "In pursuit of lepton flavour violation: A search for the τ-> μγγ decay with atlas at √s=8 TeV",
>>> my_file.print_lines(166)
title = "Measurement of the CP-violating phase ϕsand the Bs0meson decay width difference with Bs0→ J/ψϕ decays in ATLAS",
>>> my_file.clean_bibtex_file_and_write_output_to_another_file(patterns_to_replace={'>': '', '<': ''})
Cleaning of "example_data//problematic_characters_test.bib" started
Cleaning of "example_data//problematic_characters_test.bib" finished
>>> my_cleaned_file = Bibtex_File('example_data//problematic_characters_test_cleaned.bib')
>>> my_cleaned_file.print_lines(95)
title = "Networks of * / G/ [?] queues with shot-noise-driven arrival intensities",
>>> my_cleaned_file.print_lines(111)
title = "Search for heavy resonances decaying to a $Z$ boson and a photon in $pp$ collisions at $\sqrt{s}=13$ TeV with the ATLAS detector",
>>> my_cleaned_file.print_lines(124)
title = "In pursuit of lepton flavour violation: A search for the t- mgg decay with atlas at [?]s=8 TeV",
"""
# This command likely cannot read some files in which certain unicode characters exist due to an encoding bug.
# See: http://www.i18nqa.com/debug/bug-double-conversion.html
from preprocessor.string_tools import String
from preprocessor.ListData import ListBuffer
from unidecode import unidecode
from meta.consoleOutput import ConsoleOutput
current_progress = 0
maximum_progress = self.get_no_of_lines_in_file()
console = ConsoleOutput(log_file_path='log.txt')
console.log_message(('Cleaning of "%s" started' % self.input_file_path),
add_timestamp_in_file=True)
with open(self.input_file_path, encoding="utf8") as input_file:
with open(self.cleaned_file_path, mode='w', encoding="utf8") as output_file:
buffer = ListBuffer()
for current_line in input_file:
current_line = String(current_line).\
clean_from_newline_characters().\
replace_patterns(patterns_to_replace)
if convert_to_ascii:
current_line.clean_from_non_ascii_characters()
# new entry line
if current_line.is_line_type('bibtex', 'start of entry'):
# this is the first entry ever (just append to buffer)
if buffer.is_empty:
buffer.append_row(current_line.content)
# this is NOT the first entry ever (write buffer to output if balanced, then re-initiate)
else:
if buffer.is_each_row_balanced(exclude_special_rows_of_syntax='bibtex'):
if buffer.is_parsable('bibtex'):
for each_buffer_line in buffer.dataset:
print(each_buffer_line, file=output_file)
else:
self.no_of_nonparsable_entries_due_to_unknown_reason += 1
else:
# currently, when an unbalanced row is detected, the entry it belongs to is simply
# not written to the output file. If a more precise procedure (e.g., an unbalanced
# character removal algorithm) is to be added, it should be added under this 'else'.
self.no_of_unbalanced_entries_skipped += 1
buffer.clear_all().\
append_row(current_line.content)
# regular line (just append to buffer)
elif not current_line.is_line_type('bibtex', 'start of entry') \
and not current_line.is_line_type('bibtex', 'comment'):
buffer.append_row(current_line.content)
# reporting
if show_progress_bar: # show_progress_bar is False by default to prevent overly long test outputs
console.print_current_progress(current_progress, maximum_progress,
'Cleaning %s' % self.input_file_path)
current_progress += 1
console.log_message(('Cleaning of "%s" finished' % self.input_file_path), add_timestamp_in_file=True)
class Bibliography:
"""
Creates a 'Bibliography' class instance.
Bibliography classs objects entail a collection of variables and functions that gives control over naming and
formatting of variables during bibliography to triple (i.e., RDF) conversion.
Returns:
A Bibliography class object.
Examples:
>>> # import class and instantiate a Bibliography object.
>>> from triplicator.bibTools import Bibliography
>>> my_bibliography = Bibliography()
>>> # add entries to the instance
>>> my_bibliography.setEntry('01', 'author', 'John Can Lokman')
>>> my_bibliography.setEntry('01', 'title', 'A title')
>>> my_bibliography.setEntry('02', 'title', 'Another title')
>>> my_bibliography.entries
{'01': {'author': 'John Can Lokman', 'title': 'A title'}, '02': {'title': 'Another title'}}
"""
_class_instance_registry = [] # will contain all instances created in this class
_class_id_registry = [] # will hold all instance ids created in this class
_class_field_values_registry = {} # will hold all field name-value pairs and entry ids associated
# ...with field values.
# ...this enables fast searching for field values such as author names, etc.
# ...across bibliographies.
def __init__(instance):
"""
Constructor for Bibliography Class Instance.
It creates an empty Bibliography object, which can later be populated by using e.g., .setEntry or .import
methods.
"""
# adds the instance to the list of all instances of the class
Bibliography._class_instance_registry.append(instance)
# local equivalent of _class_field_values_registry. Does the same thing for the bibliography instance.
instance._field_values_registry = {}
# hold all ids created within the bibliography instance. allows quick checking whether an id exists.
instance._id_registry = []
# dictionary for holding all field types and number of their occurrences
instance._field_type_registry = {}
# dictionary that holds all entries. this is where the bibliography data is held, including ids & field values.
instance.entries = {}
instance.no_of_existing_fields_enriched_in_last_operation = 0
instance.no_of_fields_added_in_last_operation = 0
instance.no_of_entries_enriched_in_last_operation = 0
instance.no_of_entries_added_in_last_operation = 0
instance.log_file_path = 'log.txt'
###################################################################################################################
############################################### IMPORT FUNCTIONS ##################################################
###################################################################################################################
def importBibtex(instance, path_of_file_to_import, conversion_arguments_list='bib_default', show_progress_bar=False):
"""
Parses a Bibliography class object from a .bib file. During parsing, field names in the bib file is converted
to names (i.e., strings) specified in conversation_conversion_arguments_list.
Args:
path_of_file_to_import(str): Location of the .bib file to be parsed
conversion_arguments_list(str or list):A list of lists that contains arguments to be passed to
bibliography_intance.setEntry(entry_id, TARGET_FIELD, FORMATTING ALGORITHM, NEW_FIELD_NAME) method.
because field names in .bib files is fixed, a custom arguments list will often will not be necessary;
the hardcoded conversion arguments list will likely be sufficient. However, in cases where
modifications may still be necessary, the format in the example sublist below should be followed:
['each_pybtex_entry.fields["title"]', 'pybtex_document_instance_name', 'b_document'],
Returns:
Nothing; modifies the object it is called from.
Examples:
>>> # Import a .bib object as Bibliography object
>>> my_bib = Bibliography()
>>> my_bib.importBibtex('example_data//test.bib')
Parsing of example_data//test.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//test.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_abstract': 2,
'b_author_labels': 4,
'b_authors': 4,
'b_document': 4,
'b_document_label': 4,
'b_doi': 2,
'b_issn': 3,
'b_issue_number': 1,
'b_journal': 3,
'b_journal_label': 3,
'b_pages': 2,
'b_publication_month': 4,
'b_publication_year': 4,
'b_publisher': 4,
'b_publisher_label': 4,
'b_pure_bibliography_id': 4,
'b_topic_labels': 2,
'b_topics': 2,
'b_type': 4,
'b_volume': 3}
<BLANKLINE>
<BLANKLINE>
"""
from builtins import KeyError
from pprint import pprint
from triplicator.pybtexImporter import Pybtex_import
from meta.consoleOutput import ConsoleOutput
from preprocessor.Text_File import Log_File
log_file = Log_File(instance.log_file_path)
console = ConsoleOutput(log_file_path='log.txt')
console.log_message('Parsing of %s started' % path_of_file_to_import, add_timestamp_in_file=True)
# import input data into pybtex_data variable
pybtex_import_instance = Pybtex_import(path_of_file_to_import)
pybtex_data = pybtex_import_instance.data
########################################################################
# Transfer items from pybtex parsed dictionary to output dictionary #
########################################################################
# In order to shorten the code, a list of arguments is given below, and then passed to the .setFormattedEntry method
# ... through a for loop. In the list, each line is a (sub-)list of three arguments to be passed.
# # Without the use of this shortening procedure, a function for each field should be written in try-except
# # blocks
# # ... as following:
# for each_pybtex_entry_id, each_pybtex_entry in pybtex_data.entries.items():
# # try-except blocks are necessary for use in for loops, as specified field may not always be present in an entry
# try:
# output_bibliography.setFormattedEntry(each_pybtex_entry_id, each_pybtex_entry.fields['title'],
# 'pybtex_document_instance_name', 'b_document')
# except:
# pass
if conversion_arguments_list == 'bib_default':
conversion_arguments_list = [
# CAUTION: If any changes are made to 'desired_field_name's, the same changes should be made to
# Bibliography.importCsv() > conversion_arguments_list > 'open citations' > 'desired_field_name' column
# [target_field_value in existing data, formatting_algorithm, desired_field_name in new object]
['each_pybtex_entry.type', 'capitalize_first_letter', 'b_type'],
['each_pybtex_entry_id', 'none', 'b_pure_bibliography_id'],
['each_pybtex_entry.fields["title"]', 'pybtex_document_instance_name', 'b_document'],
['each_pybtex_entry.fields["title"]', 'pybtex_document_label', 'b_document_label'],
['each_pybtex_entry.persons["author"]', 'pybtex_author_instance_name', 'b_authors'],
['each_pybtex_entry.persons["author"]', 'pybtex_author_label', 'b_author_labels'],
['each_pybtex_entry.fields["keywords"]', 'pybtex_topic_instance_name', 'b_topics'],
['each_pybtex_entry.fields["keywords"]', 'pybtex_topic_label', 'b_topic_labels'],
['each_pybtex_entry.fields["journal"]', 'pybtex_document_instance_name', 'b_journal'],
['each_pybtex_entry.fields["journal"]', 'pybtex_document_label', 'b_journal_label'],
['each_pybtex_entry.fields["booktitle"]', 'pybtex_document_instance_name', 'b_parent_book'],
['each_pybtex_entry.fields["booktitle"]', 'pybtex_document_label', 'b_parent_book_label'],
['each_pybtex_entry.fields["publisher"]', 'pybtex_document_instance_name', 'b_publisher'],
['each_pybtex_entry.fields["publisher"]', 'pybtex_document_label', 'b_publisher_label'],
['each_pybtex_entry.fields["year"]', 'none', 'b_publication_year'],
['each_pybtex_entry.fields["month"]', 'none', 'b_publication_month'],
['each_pybtex_entry.fields["number"]', 'none', 'b_issue_number'],
['each_pybtex_entry.fields["volume"]', 'none', 'b_volume'],
['each_pybtex_entry.fields["pages"]', 'none', 'b_pages'],
['each_pybtex_entry.fields["doi"]', 'none', 'b_doi'],
['each_pybtex_entry.fields["issn"]', 'none', 'b_issn'],
['each_pybtex_entry.fields["isbn"]', 'none', 'b_isbn'],
['each_pybtex_entry.fields["edition"]', 'none', 'b_edition'],
['each_pybtex_entry.fields["abstract"]', 'none', 'b_abstract'],
['each_pybtex_entry.fields["note"]', 'none', 'b_note']
]
# if conversion_arguments_list is provided, proceed without modifying the provided arguments list
elif type(conversion_arguments_list) is list:
pass
# if conversion_arguments_list is neither hardcoded nor provided, return error.
else:
raise ValueError("Conversion_arguments_list parameter should be either 'bib_default' or be a list that "
"contains at least one list of arguments.")
# variables for progress bar
current_progress = 0
console.log_message('Calculating file length...', add_timestamp_in_file=True)
maximum_progress = len(pybtex_data.entries.items())
# loop through individual reference entries in the parsed pybtex bib file
for each_pybtex_entry_id, each_pybtex_entry in pybtex_data.entries.items():
# loop through each line in the conversion_arguments_list
for each_argument_list in conversion_arguments_list:
# try using the elements of each sub-list in conversion_arguments_list as arguments of .setFormattedEntry method
# (try-except block is necessary, as each field may not exist for each entry)
try:
instance.setFormattedEntry(each_pybtex_entry_id, eval(each_argument_list[0]),
each_argument_list[1], each_argument_list[2])
except KeyError:
pass
if show_progress_bar: # default is false to prevent very long test outputs
console.print_current_progress(current_progress, maximum_progress,
'Parsing file "%s"' % path_of_file_to_import)
current_progress += 1
########################################################################
# SERIES_TITLE AND ID -- To be implemented if needed
# This has to be kept out of the main loop, as series is not a field, but a whole bibliography entry
# themselves.
# They are not nested within individual entries, and are rather parallel to them.
# Some older code from previous versions, which extracts and converts series title:
# try:
# # collection refers to a full reference entity, and this is why the title of the collection is nested quite
# # ...deeper than other elements parsed before in this script
# for series_id in pybtex_data.entries[each_pybtex_entry_id].collection.entries:
# print series_id, each_pybtex_entry_id
# #bibDictionary[each_pybtex_entry_id].append({"is_part_of_series_with_title":[each_pybtex_entry_id].fields["title"].encode("ascii",errors="ignore")
# bibDictionary[each_pybtex_entry_id].append({"is_part_of_series_with_id":series_id})
# #[each_pybtex_entry_id].fields["title"].encode("ascii",errors="ignore")
## field missing from bibliography
# except(KeyError):
# pass
########################
# OPERATION SUMMARY #
########################
# Print and log success message
import_complete_message = path_of_file_to_import + ' ' + 'parsed and imported as Bibliography object.'
print('\n\n-----------------------------------------------------------------------------------------------'
'----')
console.log_message(import_complete_message, add_timestamp_in_file=True)
# Print and log statistics about the import operation
# TODO: print total number of imported entries
console.log_message("\nFields added to the parsed the Bibliography object:")
instance.summarize()
for each_key, each_value in instance._field_type_registry.items():
log_file.append_line(str(each_key) + ': ' + str(each_value))
# Print and log a sample from parsed entries
console.log_message("\n")
instance.write_preview_to_log(number_of_entries_to_preview=3)
def importCsv(instance,
path_of_file_to_import,
csv_delimiter_character,
field_value_list_separator,
id_column_header,
conversion_arguments_list,
cleaning_algorithm=None,
show_progress_bar=False
):
"""
Parses a Bibliography class object from a .csv file.
Args:
path_of_file_to_import(str): Location of the .csv file to be parsed
csv_delimiter_character(str): One-character-long string that separates the columns of the CSV file.
field_value_list_separator:(str): One- or multi-character-long string that separates multiple
values in a cell.
id_column_header(str): Header of the id column
conversion_arguments_list(str or list): A list of lists that contains arguments to be passed to
bibliography_intance.setEntry(entry_id, TARGET_FIELD, FORMATTING ALGORITHM, NEW_FIELD_NAME) method.
a custom arguments list will look like this:
['each_entry_data["titles"]', 'pybtex_document_label', 'b_document_label']
cleaning_algorithm(str): CSV cleaning algorithm that will be executed in .cleanAndTokenizeCsv() method of
CSV_Bibliography class in csvImporter module.
Keyword Args:
"open citations" (conversion_arguments_list): Calls a list of lists that holds arguments for .setEntry
method. An example sub-list from conversion_arguments_list is:
['each_entry_data["titles"]', 'pybtex_document_instance_name', 'b_document']
"open citations" (cleaning_algorithm): Cleans commas that occur in entry field values using an algorithm
tuned for CSV files downloaded from Open Citatons.
"parse only" (cleaning_algorithm): Skips cleaning
Returns:
Nothing; modifies the object it is called from.
Examples:
>>> # import a csv file
>>> oc_bibliography = Bibliography()
>>> oc_bibliography.importCsv(path_of_file_to_import='example_data//oc_query_2.2_results_short_sample.csv',
... csv_delimiter_character=',',
... field_value_list_separator=' | ',
... id_column_header='journal_article',
... conversion_arguments_list='open citations',
... cleaning_algorithm='default')
Parsing of "example_data//oc_query_2.2_results_short_sample.csv" started
Conversion from ListData to Bibliography object started
Conversion completed. 3 out of 3 ListData rows converted to Bibliography object entries
<BLANKLINE>
Formatting of Bibliography entries started
"example_data//oc_query_2.2_results_short_sample.csv" parsed and imported into Bibliography object in memory
<BLANKLINE>
Number of fields in the parsed bibliography:
{'b_author_labels': 3,
'b_authors': 3,
'b_cited': 3,
'b_cited_by': 3,
'b_document': 3,
'b_document_label': 3,
'b_doi': 3,
'b_issue_number': 3,
'b_journal': 3,
'b_journal_label': 3,
'b_open_citations_id': 3,
'b_pages': 2,
'b_pmid': 3,
'b_publication_year': 3,
'b_publisher': 3,
'b_publisher_label': 3,
'b_type': 3,
'b_url': 3,
'b_volume': 3}
>>> oc_bibliography.preview(2)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('https://w3id.org/oc/corpus/br/362418',
{'b_author_labels': ['Zetterqvist, M'],
'b_authors': ['Zetterqvist_M'],
'b_cited': ['https://w3id.org/oc/corpus/br/37961',
'https://w3id.org/oc/corpus/br/38250',
'https://w3id.org/oc/corpus/br/135448',
'https://w3id.org/oc/corpus/br/135458',
'https://w3id.org/oc/corpus/br/177639',
'https://w3id.org/oc/corpus/br/177648',
'https://w3id.org/oc/corpus/br/177653',
'https://w3id.org/oc/corpus/br/177661',
'https://w3id.org/oc/corpus/br/177774',
'https://w3id.org/oc/corpus/br/362419',
'https://w3id.org/oc/corpus/br/362426',
'https://w3id.org/oc/corpus/br/362438',
'https://w3id.org/oc/corpus/br/607811',
'https://w3id.org/oc/corpus/br/1270766',
'https://w3id.org/oc/corpus/br/1560911',
'https://w3id.org/oc/corpus/br/1794850',
'https://w3id.org/oc/corpus/br/1881397',
'https://w3id.org/oc/corpus/br/2258672',
'https://w3id.org/oc/corpus/br/2907029',
'https://w3id.org/oc/corpus/br/2907034',
'https://w3id.org/oc/corpus/br/2907035',
'https://w3id.org/oc/corpus/br/2907042',
'https://w3id.org/oc/corpus/br/2907056',
'https://w3id.org/oc/corpus/br/3346205',
'https://w3id.org/oc/corpus/br/3567493',
'https://w3id.org/oc/corpus/br/3567495',
'https://w3id.org/oc/corpus/br/3949890',
'https://w3id.org/oc/corpus/br/5106137',
'https://w3id.org/oc/corpus/br/5441063',
'https://w3id.org/oc/corpus/br/5441066',
'https://w3id.org/oc/corpus/br/5441085',
'https://w3id.org/oc/corpus/br/5656230',
'https://w3id.org/oc/corpus/br/6060536',
'https://w3id.org/oc/corpus/br/6063037',
'https://w3id.org/oc/corpus/br/6449521',
'https://w3id.org/oc/corpus/br/6486152',
'https://w3id.org/oc/corpus/br/6486162',
'https://w3id.org/oc/corpus/br/6919305',
'https://w3id.org/oc/corpus/br/6919323',
'https://w3id.org/oc/corpus/br/7558746',
'https://w3id.org/oc/corpus/br/7560541',
'https://w3id.org/oc/corpus/br/7560644',
'https://w3id.org/oc/corpus/br/7560645',
'https://w3id.org/oc/corpus/br/7560646',
'https://w3id.org/oc/corpus/br/7560647',
'https://w3id.org/oc/corpus/br/7560648',
'https://w3id.org/oc/corpus/br/7560651',
'https://w3id.org/oc/corpus/br/7560652',
'https://w3id.org/oc/corpus/br/7560653',
'https://w3id.org/oc/corpus/br/7560654',
'https://w3id.org/oc/corpus/br/7560655',
'https://w3id.org/oc/corpus/br/7560656',
'https://w3id.org/oc/corpus/br/7560657',
'https://w3id.org/oc/corpus/br/7560658',
'https://w3id.org/oc/corpus/br/7560659',
'https://w3id.org/oc/corpus/br/7560660',
'https://w3id.org/oc/corpus/br/7560661',
'https://w3id.org/oc/corpus/br/7560662',
'https://w3id.org/oc/corpus/br/7560663',
'https://w3id.org/oc/corpus/br/7560664',
'https://w3id.org/oc/corpus/br/7560665',
'https://w3id.org/oc/corpus/br/7560666'],
'b_cited_by': 'https://w3id.org/oc/corpus/br/362415',
'b_document': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_document_label': 'The DSM-5 diagnosis of nonsuicidal self-injury '
'disorder-a review of the empirical literature',
'b_doi': '10.1186/s13034-015-0062-7',
'b_issue_number': '1',
'b_journal': 'Child_and_Adolescent_Psychiatry_and_Mental_Health-Child_Adolesc_Psychiatry_Ment_Health',
'b_journal_label': 'Child and Adolescent Psychiatry and Mental Health-Child '
'Adolesc Psychiatry Ment Health',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/362418',
'b_pages': ' ',
'b_pmid': '26417387',
'b_publication_year': '2015',
'b_publisher': 'Springer_Science_%2B_Business_Media',
'b_publisher_label': 'Springer Science + Business Media',
'b_type': 'Journal Article',
'b_url': 'http://dx.doi.org/10.1186/s13034-015-0062-7',
'b_volume': '9'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('https://w3id.org/oc/corpus/br/384',
{'b_author_labels': ['Creutzberg, CL', 'van_Putten, WLJ', 'Koper, PC',
'Lybeert, MLM', 'Jobsen, JJ', 'Warlam-Rodenhuis, CC',
'De_Winter, KAJ', 'Lutgens, LCHW', 'van_den_Bergh, ACM',
'van_der_Steen-Banasik, E', 'Beerman, H', 'van_Lent, M'],
'b_authors': ['Creutzberg_CL', 'van_Putten_WLJ', 'Koper_PC', 'Lybeert_MLM',
'Jobsen_JJ', 'Warlam-Rodenhuis_CC', 'De_Winter_KAJ',
'Lutgens_LCHW', 'van_den_Bergh_ACM', 'van_der_Steen-Banasik_E',
'Beerman_H', 'van_Lent_M'],
'b_cited': '',
'b_cited_by': ['https://w3id.org/oc/corpus/br/1',
'https://w3id.org/oc/corpus/br/1342763',
'https://w3id.org/oc/corpus/br/1772164'],
'b_document': 'Survival_after_relapse_in_patients_with_endometrial_cancer-results_from_a_randomized_trial',
'b_document_label': 'Survival after relapse in patients with endometrial '
'cancer-results from a randomized trial',
'b_doi': '10.1016/s0090-8258(03)00126-4',
'b_issue_number': '2',
'b_journal': 'Gynecologic_Oncology',
'b_journal_label': 'Gynecologic Oncology',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/384',
'b_pages': '201--209',
'b_pmid': '12713981',
'b_publication_year': '2003',
'b_publisher': 'Elsevier_BV',
'b_publisher_label': 'Elsevier BV',
'b_type': 'Journal Article',
'b_url': 'http://dx.doi.org/10.1016/s0090-8258%2803%2900126-4',
'b_volume': '89'})
<BLANKLINE>
>>> # create a Bibliography instance by using custom conversion_arguments_list
>>> custom_arguments_list = [
... ['each_entry_data["titles"]', 'pybtex_document_instance_name', 'x_document'],
... ['each_entry_data["titles"]', 'pybtex_document_label', 'x_document_label']
... ]
>>> my_custom_bibliography = Bibliography()
>>> my_custom_bibliography.importCsv(path_of_file_to_import='example_data//test.csv',
... conversion_arguments_list=custom_arguments_list,
... cleaning_algorithm="default",
... csv_delimiter_character=',',
... field_value_list_separator=' | ',
... id_column_header='referenceEntry')
Parsing of "example_data//test.csv" started
Conversion from ListData to Bibliography object started
Conversion completed. 7 out of 7 ListData rows converted to Bibliography object entries
<BLANKLINE>
Formatting of Bibliography entries started
"example_data//test.csv" parsed and imported into Bibliography object in memory
<BLANKLINE>
Number of fields in the parsed bibliography:
{'x_document': 7, 'x_document_label': 7}
>>> my_custom_bibliography.preview(1)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('https://w3id.org/oc/corpus/br/44493',
{'x_document': 'Midwife-led_continuity_models_versus_other_models_of_care_for_childbearing_women',
'x_document_label': 'Midwife-led continuity models versus other models of '
'care for childbearing women'})
<BLANKLINE>
>>>
>>> # import a csv file
>>> old_oc_conversion_arguments_list = [ # this is the old open citations conversion arguments list (was a keyword argument)
... # [target_field_value in existing data, formatting_algorithm, desired_field_name in new object]
... ['each_entry_data["titles"]', 'pybtex_document_instance_name', 'b_document'],
... ['each_entry_data["titles"]', 'pybtex_document_label', 'b_document_label'],
... ['each_entry_data["dois"]', 'oc_select_first_item_if_list', 'b_doi'],
... ['each_entry_data["authors"]', 'open_citations_author_instance_name', 'b_authors'],
... ['each_entry_data["authors"]', 'open_citations_author_label', 'b_author_labels'],
... ['each_entry_data["publications"]', 'pybtex_document_instance_name', 'b_publication'],
... ['each_entry_data["publications"]', 'pybtex_document_label', 'b_publication_label'],
... ['each_entry_data["publication_types"]', 'oc_select_last_item_if_list', 'b_publication_type'],
... ['each_entry_data["types"]', 'oc_select_last_item_if_list', 'b_type'],
... ['each_entry_data["years"]', 'oc_select_first_item_if_list', 'b_publication_year'],
... ['each_entry_data["publishers"]', 'pybtex_document_instance_name', 'b_publisher'],
... ['each_entry_data["publishers"]', 'pybtex_document_label', 'b_publisher_label']
... ]
>>> my_csv_bibliography = Bibliography()
>>> my_csv_bibliography.importCsv(path_of_file_to_import='example_data//test.csv',
... conversion_arguments_list=old_oc_conversion_arguments_list,
... cleaning_algorithm="default",
... csv_delimiter_character=',',
... field_value_list_separator=' | ',
... id_column_header='referenceEntry')
Parsing of "example_data//test.csv" started
Conversion from ListData to Bibliography object started
Conversion completed. 7 out of 7 ListData rows converted to Bibliography object entries
<BLANKLINE>
Formatting of Bibliography entries started
"example_data//test.csv" parsed and imported into Bibliography object in memory
<BLANKLINE>
Number of fields in the parsed bibliography:
{'b_author_labels': 7,
'b_authors': 7,
'b_document': 7,
'b_document_label': 7,
'b_doi': 7,
'b_publication': 7,
'b_publication_label': 7,
'b_publication_type': 7,
'b_publication_year': 7,
'b_publisher': 7,
'b_publisher_label': 7,
'b_type': 7}
>>> my_csv_bibliography.preview(1)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('https://w3id.org/oc/corpus/br/44493',
{'b_author_labels': ['Sandall, J', 'Soltani, H', 'Gates, S', 'Shennan, A',
'Devane, D'],
'b_authors': ['Sandall_J', 'Soltani_H', 'Gates_S', 'Shennan_A', 'Devane_D'],
'b_document': 'Midwife-led_continuity_models_versus_other_models_of_care_for_childbearing_women',
'b_document_label': 'Midwife-led continuity models versus other models of '
'care for childbearing women',
'b_doi': '10.1002/14651858.cd004667.pub3',
'b_publication': 'Cochrane_Database_of_Systematic_Reviews-Reviews',
'b_publication_label': 'Cochrane Database of Systematic Reviews-Reviews',
'b_publication_type': 'http://purl.org/spar/fabio/ExpressionCollection',
'b_publication_year': '2013',
'b_publisher': 'Wiley-Blackwell',
'b_publisher_label': 'Wiley-Blackwell',
'b_type': 'http://purl.org/spar/fabio/ReferenceEntry'})
<BLANKLINE>
"""
from triplicator.csvImporter import CSV_Bibliography
from meta.consoleOutput import ConsoleOutput
console = ConsoleOutput('log.txt')
console.log_message('Parsing of "%s" started' % path_of_file_to_import, add_timestamp_in_file=True)
# pass functions to CSV container and create an instance of CSV_Bibliography class
csv_bibliography = CSV_Bibliography(csv_file_path=path_of_file_to_import,
id_column_header=id_column_header,
field_value_list_separator=field_value_list_separator,
csv_delimiter_character=csv_delimiter_character,
cleaning_algorithm=cleaning_algorithm,
show_progress_bar=show_progress_bar
)
if conversion_arguments_list == 'open citations':
# "publication_type" , "journal_article" , "journal_issue_number" , "journal_volume_number" , "startEndPages" , "publisher_name" , "cited_by_article"
conversion_arguments_list = [
# CAUTION: If there would be any merge operation would be made with other bib files (e.g.,
# using 'enrich_with' method), the 'desired_field_name's should be the same with those in
# Bibliography.importBibtex() > conversion_arguments_list > 'bib_default' > 'desired_field_name' column
# If a field name differs from its counterpart in the bib conversion algorithm, then during the merge
# operation it will likely be added as a separate field under this differing name
# [target_field_value in existing data, formatting_algorithm, desired_field_name in new object]
['each_entry_data["publication_type"]', 'oc_select_last_item_if_list_and_capitalize_first_letter', 'b_type'],
# even though the field name below is "journal_article" and it refers to the column with the same header
# in the source csv file, this is the name of the column that contains OpenCitations IDs of documents
['each_entry_data["journal_article"]', 'oc_select_first_item_if_list', 'b_open_citations_id'],
['each_entry_data["title"]', 'pybtex_document_instance_name', 'b_document'],
['each_entry_data["title"]', 'pybtex_document_label', 'b_document_label'],
['each_entry_data["authors"]', 'open_citations_author_instance_name', 'b_authors'],
['each_entry_data["authors"]', 'open_citations_author_label', 'b_author_labels'],
['each_entry_data["journal_name"]', 'pybtex_document_instance_name', 'b_journal'],
['each_entry_data["journal_name"]', 'pybtex_document_label', 'b_journal_label'],
['each_entry_data["publisher_name"]', 'pybtex_document_instance_name', 'b_publisher'],
['each_entry_data["publisher_name"]', 'pybtex_document_label', 'b_publisher_label'],
['each_entry_data["publication_year"]', 'oc_select_first_item_if_list', 'b_publication_year'],
['each_entry_data["journal_issue_number"]', 'oc_select_first_item_if_list', 'b_issue_number'],
['each_entry_data["journal_volume_number"]', 'oc_select_first_item_if_list', 'b_volume'],
['each_entry_data["startEndPages"]', 'oc_select_first_item_if_list', 'b_pages'],
['each_entry_data["doi"]', 'oc_select_first_item_if_list', 'b_doi'],
['each_entry_data["pmid"]', 'oc_select_first_item_if_list', 'b_pmid'],
['each_entry_data["url"]', 'oc_select_first_item_if_list', 'b_url'],
['each_entry_data["cited_by_the_articles"]', 'none', 'b_cited_by'],
['each_entry_data["cited_the_articles"]', 'none', 'b_cited']
]
# if a custom conversion_arguments_list is provided, proceed without modifying the provided list
elif type(conversion_arguments_list) is list:
pass
else:
raise ValueError("Conversion_arguments_list parameter should be either left blank or be a list that "
"contains sublists of arguments.")
# For logging
console.log_message("\nFormatting of Bibliography entries started", add_timestamp_in_file=True)
maximum_progress = len(csv_bibliography.entries.keys())
failed_conversion_arguments = []
# loop through individual reference entries in the parsed pybtex bib file
for i, (each_entry_id, each_entry_data) in enumerate(csv_bibliography.entries.items()):
if show_progress_bar:
console.print_current_progress(i, maximum_progress, 'Formatting Bibliography object entries')
# loop through each line in the conversion_arguments_list
for each_argument_list in conversion_arguments_list:
# try using the elements of each sub-list in conversion_arguments_list as arguments of
# .setFormattedEntry method
# (try-except block is necessary, as each field may not exist for each entry)
try:
instance.setFormattedEntry(each_entry_id, eval(each_argument_list[0]),
each_argument_list[1], each_argument_list[2])
except:
# TODO: Restore this line (replaced it with a more forgiving except statement for now)
# except KeyError:
pass
###############################
# OVERALL OPERATION SUMMARY #
###############################
console.log_message('"%s" parsed and imported into Bibliography object in memory' % path_of_file_to_import,
add_timestamp_in_file=True)
console.log_message("\nNumber of fields in the parsed bibliography:", print_to_file=False) # because...
# ... '.summarize()' does not print to file
instance.summarize()
###################################################################################################################
############################################ MANIPULATION FUNCTIONS ###############################################
###################################################################################################################
def setEntry(instance, entry_id, field_name, field_value):
"""
Args:
entry_id (str): desired identifier for the entry to be created
field_name (str): name of the field to be created (e.g., 'author')
field_value (str): value of the field to be created (e.g., 'John Doe' or ['John Doe', 'Jane Doe'])
Returns:
Nothing, but it adds new entries to the Bibliography object instance.
Examples:
>>> # preparation: import class and instantiate a Bibliography object.
>>> from triplicator.bibTools import Bibliography
>>> my_bibliography = Bibliography()
>>> # add entries to the instance
>>> my_bibliography.setEntry("01", "author", "John Can Lokman")
>>> my_bibliography.setEntry("01", "title", "A title")
>>> my_bibliography.setEntry("02", "title", "Another title")
>>> my_bibliography.entries
{'01': {'author': 'John Can Lokman', 'title': 'A title'}, '02': {'title': 'Another title'}}
"""
# if the ID is a new entry
if entry_id not in instance._id_registry:
# add target id as key of a the output dictionary and a subdictionary to it as fields and values
instance.entries[entry_id] = {field_name: field_value}
# add an instance id to the instance._id_registry
instance._id_registry.append(entry_id)
# if the ID is NOT a new entry
else:
# call entry by id, and add value-key pair to it
instance.entries[entry_id][field_name] = field_value
instance.updateFieldTypesRegistry(entry_id, field_name, field_value)
instance.updateFieldValuesRegistry(entry_id, field_name, field_value)
def setFormattedEntry(instance, desired_entry_id, target_field_value, formatting_algorithm, desired_field_name):
"""
Extracts specified field values from a data source, formats it according to the specified algorithm, and adds
the formatted values to the specified Bibliography class object. Works by simply passing the arguments to
cleanAndFormatValues() function and .setEntry method. For modification of functionality, see that function or method
instead; .setFormattedEntry is merely proxy method built to serve as a shorthand. A fairly comprehensive
example that demonstrates usage of different formatting algorithms is also provided in the examples of the
curent method.
Args:
desired_entry_id: the identifier of the new entry to be created
target_field_value: the values in the existing source bibliography or data
formatting_algorithm: one of the formatting algorithms in cleanAndFormatValues function. See cleanAndFormatValues function
for a list of formatting algorithms.
desired_field_name: the name of the field to be created
Returns:
if target_field_value is 'author' and formatting_algorithm is 'pybtex_author...' : list of authors
if target_field_value is 'keywords' and formatting_algorithm is 'pybtex_author...': list of keywords
all other scenarios: formatted string
Examples:
>>> # import class and instantiate a Bibliography object.
>>> from triplicator.bibTools import Bibliography
>>> my_bibliography = Bibliography()
>>> # import a sample .bib file and assign it to a variable
>>> from triplicator.pybtexImporter import Pybtex_import
>>> pybtex_entries = Pybtex_import('example_data//test.bib').data.entries
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
>>> # extract fields and their values (from pybtex object), format them,
>>> # ... and add them to the Bibliography class object.
>>> for each_entry_id, each_entry in pybtex_entries.items():
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['title'], 'pybtex_document_instance_name', 'x_document')
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['title'], 'pybtex_document_label', 'x_document_label')
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.persons['author'], 'pybtex_author_instance_name', 'x_author')
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.persons['author'], 'pybtex_author_label', 'x_author_label')
... # some fields may exist for only some entries (for none in this sample .bib file)
... # this try-except block will finish with exception because there is no 'keyword' field in the sample
... # ... .bib file.
... try:
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['keyword'], 'pybtex_topic_instance_name', 'x_topics')
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['keyword'], 'pybtex_topic_label', 'x_topic_labels')
... except:
... pass
...
... try:
... # 'pybtex_document_instance_name' and 'pybtex_document_label' formatting algorithms are suitable
... # ... for use in other fields, like 'journal'
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['journal'], 'pybtex_document_instance_name', 'x_journal')
... my_bibliography.setFormattedEntry(each_entry_id, each_entry.fields['journal'], 'pybtex_document_label', 'x_journal_label')
... except KeyError:
... pass
>>> print(my_bibliography.entries)
{'56fafbf2574947cc9cbbfae578a0a36d': {'x_document': 'Book_with_one_author', 'x_document_label': 'Book with one author', 'x_author': ['Jaschke_AC'], 'x_author_label': ['Jaschke, AC']}, 'd79d00c790984ab08240e997d077c332': {'x_document': 'Article_with_5_authors_with_and_notation', 'x_document_label': "Article with 5 authors with 'and' notation", 'x_author': ['Lohr_A', 'Beunen_R', 'Savelli_H', 'Kalz_M', 'Ragas_A', 'Van_Belleghem_F'], 'x_author_label': ['Lohr, A', 'Beunen, R', 'Savelli, H', 'Kalz, M', 'Ragas, A', 'Van_Belleghem, F'], 'x_journal': 'Current_Opinion_in_Environmental_Sustainability', 'x_journal_label': 'Current Opinion in Environmental Sustainability'}, 'a8781aa0eae047d1826a658f3545ce3f': {'x_document': 'Article_with_3_authors_with_mixed_notation', 'x_document_label': 'Article with 3 authors with mixed notation', 'x_author': ['Mendoza_Rodriguez_JP', 'Wielhouwer_JL', 'Kirchler_ESMN'], 'x_author_label': ['Mendoza_Rodriguez, JP', 'Wielhouwer, JL', 'Kirchler, ESMN'], 'x_journal': 'Journal_of_Economic_Psychology', 'x_journal_label': 'Journal of Economic Psychology'}, '01b9c957875b4a96839c1bfd05ec6a31': {'x_document': 'Article_with_non-uri_safe_characters%3A%3C%3E%5B%5D_%40%25_to_WW_%E2%88%97%E2%86%92e%CE%BD%CE%BC%CE%BD_with_the_ATLAS_detector_at_%E2%88%9As%3D8_TeV', 'x_document_label': 'Article with non-uri safe characters:<>{}()[] @% to WW ∗→eνμν with the ATLAS detector at √s=8 TeV', 'x_author': ['%40uthor_%CE%BDbn', 'Aaboud_M', 'Bentvelsen_S', 'Berge_D', 'Colijn_AP', 'de_Jong_P', 'Koffeman_E', 'Sabato_G', 'Salek_D', 'van_Vulpen_I', 'Vermeulen_JC', 'Vreeswijk_M'], 'x_author_label': ['@uthor, νbn', 'Aaboud, M', 'Bentvelsen, S', 'Berge, D', 'Colijn, AP', 'de_Jong, P', 'Koffeman, E', 'Sabato, G', 'Salek, D', 'van_Vulpen, I', 'Vermeulen, JC', 'Vreeswijk, M'], 'x_journal': 'The_Journal_of_High_Energy_Physics', 'x_journal_label': 'The Journal of High Energy Physics'}}
"""
# if the current field exists for the current entry
# format the extracted value (which is a string or list [e.g., if it is the values from the 'author' field])
formatted_field_value = cleanAndFormatValues(target_field_value, formatting_algorithm)
# add the now-formatted name to Bibliography object
instance.setEntry(desired_entry_id, desired_field_name, formatted_field_value)
# if the current field does not exist for the current entry
def enrich_with(instance, target_bibliography_object, field_to_match_in_bibliographies, method='left join'):
"""
Left joins or merges two bibliographies.
Args:
target_bibliography_object(Bibliography): The target bibliography that will be used to enrich the current
bibliography.
field_to_match_in_bibliographies(str): The field name that will be used to match entries between bibliographies
(e.g., doi)
method(str): Method to use when combining bibliographies
Keyword Args:
'left join' (method): Add new fields and values from the target_bibliography_object only if the record they
belong to exists in the instance bibliography (i.e., to self)
'merge' (method): Left joins when possible, add if not, adds new entries from other_bibliography to the
instance bibliography (i.e., to self)
Returns:
Nothing
Examples:
>>> #=================================================
>>> # EXAMPLE: CREATE AND COMBINE BIBLIOGRAPHY OBJECTS
>>> #=================================================
>>> # initiaton
>>> bib_one = Bibliography()
>>> bib_one.setEntry(entry_id='01', field_name='doi', field_value='6226')
>>> bib_one.setEntry(entry_id='01', field_name='title', field_value='This is a title')
>>> bib_one.preview()
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('01', {'doi': '6226', 'title': 'This is a title'})
<BLANKLINE>
>>> bib_two = Bibliography()
>>> bib_two.setEntry(entry_id='05', field_name='doi', field_value='6226')
>>> bib_two.setEntry(entry_id='05', field_name='author', field_value='John Doe')
>>> bib_two.preview()
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('05', {'author': 'John Doe', 'doi': '6226'})
<BLANKLINE>
>>> # enrichment
>>> bib_one.enrich_with(target_bibliography_object=bib_two, field_to_match_in_bibliographies='doi')
<BLANKLINE>
Enrichment completed successfully.
Existing entries enriched: 1
Fields added to existing entries: 1
New entries added: 0
>>> bib_one.preview()
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('01', {'author': 'John Doe', 'doi': '6226', 'title': 'This is a title'})
<BLANKLINE>
>>> # no entries appended in 'left join' mode
>>> bib_two.setEntry(entry_id='100', field_name='doi', field_value='5000') # doi 500 not in bib_one
>>> bib_two.setEntry(entry_id='100', field_name='note', field_value='This is a note')
>>> bib_one.enrich_with(target_bibliography_object=bib_two, field_to_match_in_bibliographies='doi')
<BLANKLINE>
Enrichment completed successfully.
Existing entries enriched: 0
Fields added to existing entries: 0
New entries added: 0
>>> bib_one.preview(10)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('01', {'author': 'John Doe', 'doi': '6226', 'title': 'This is a title'})
<BLANKLINE>
>>> # entries enriched and appended in 'merge' mode
>>> bib_two.setEntry(entry_id='41124', field_name='doi', field_value='6226') # doi 6226 is in bib_one too
>>> bib_two.setEntry(entry_id='41124', field_name='publisher', field_value='Some publisher')
>>> bib_two.setEntry(entry_id='100', field_name='doi', field_value='5000') # doi 500 not in bib_one
>>> bib_two.setEntry(entry_id='100', field_name='note', field_value='This is a note')
>>> bib_one.enrich_with(target_bibliography_object=bib_two, field_to_match_in_bibliographies='doi'
... , method='merge')
<BLANKLINE>
Enrichment completed successfully.
Existing entries enriched: 1
Fields added to existing entries: 1
New entries added: 1
>>> bib_one.preview(10)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('01',
{'author': 'John Doe',
'doi': '6226',
'publisher': 'Some publisher',
'title': 'This is a title'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('100', {'doi': '5000', 'note': 'This is a note'})
<BLANKLINE>
>>> #=============================================
>>> # EXAMPLE: IMPORT AND COMBINE TWO BIBTEX FILES
>>> #=============================================
>>> # bib file import and merge
>>> bib_poor = Bibliography()
>>> bib_poor.importBibtex('example_data//merge_test_file_poor.bib')
Parsing of example_data//merge_test_file_poor.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//merge_test_file_poor.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_author_labels': 2,
'b_authors': 2,
'b_document': 2,
'b_document_label': 2,
'b_doi': 2,
'b_publication_month': 1,
'b_publication_year': 2,
'b_pure_bibliography_id': 2,
'b_type': 2}
<BLANKLINE>
<BLANKLINE>
>>> bib_poor.preview(100)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('b56e503067994b389d4eced98fae2206',
{'b_author_labels': ['Koning, R', 'Buraglio, N', 'de_Laat, CTAM', 'Grosso, P'],
'b_authors': ['Koning_R', 'Buraglio_N', 'de_Laat_CTAM', 'Grosso_P'],
'b_document': 'CoreFlow-Enriching_Bro_security_events_using_network_traffic_monitoring_data',
'b_document_label': 'CoreFlow-Enriching Bro security events using network '
'traffic monitoring data',
'b_doi': '10.1016--j.future.2017.04.017',
'b_publication_month': '2',
'b_publication_year': '2018',
'b_pure_bibliography_id': 'b56e503067994b389d4eced98fae2206',
'b_type': 'Article'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('d0e972a611e44a80b8014f1069bfad88',
{'b_author_labels': ['van_Spanje, J'],
'b_authors': ['van_Spanje_J'],
'b_document': 'Controlling_the_Electoral_Marketplace-How_Established_Parties_Ward_Off_Competition',
'b_document_label': 'Controlling the Electoral Marketplace-How Established '
'Parties Ward Off Competition',
'b_doi': '10.1007--978-3-319-58202-3',
'b_publication_year': '2018',
'b_pure_bibliography_id': 'd0e972a611e44a80b8014f1069bfad88',
'b_type': 'Book'})
<BLANKLINE>
>>> bib_rich = Bibliography()
>>> bib_rich.importBibtex('example_data//merge_test_file_rich.bib')
Parsing of example_data//merge_test_file_rich.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//merge_test_file_rich.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_abstract': 1,
'b_author_labels': 2,
'b_authors': 2,
'b_document': 2,
'b_document_label': 2,
'b_doi': 2,
'b_isbn': 1,
'b_issn': 1,
'b_issue_number': 1,
'b_journal': 1,
'b_journal_label': 1,
'b_pages': 1,
'b_publication_month': 1,
'b_publication_year': 2,
'b_publisher': 2,
'b_publisher_label': 2,
'b_pure_bibliography_id': 2,
'b_type': 2,
'b_volume': 1}
<BLANKLINE>
<BLANKLINE>
>>> bib_rich.preview(100)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('b56e503067994b389d4eced98fae2206',
{'b_abstract': 'Attacks against network infrastructures can be detected by '
'Intrusion Detection Systems (IDS). Still reaction to these '
'events are often limited by the lack of larger contextual '
'information in which they occurred. In this paper we present '
'CoreFlow, a framework for the correlation and enrichment of '
'IDS data with network flow information. CoreFlow ingests data '
'from the Bro IDS and augments this with flow data from the '
'devices in the network. By doing this the network providers '
'are able to reconstruct more precisely the route followed by '
'the malicious flows. This enables them to devise tailored '
'countermeasures, e.g. blocking close to the source of the '
'attack. We tested the initial CoreFlow prototype in the ESnet '
'network, using inputs from 3 Bro systems and more than 50 '
'routers.',
'b_author_labels': ['Koning, R', 'Buraglio, N', 'de_Laat, CTAM', 'Grosso, P'],
'b_authors': ['Koning_R', 'Buraglio_N', 'de_Laat_CTAM', 'Grosso_P'],
'b_document': 'CoreFlow-Enriching_Bro_security_events_using_network_traffic_monitoring_data',
'b_document_label': 'CoreFlow-Enriching Bro security events using network '
'traffic monitoring data',
'b_doi': '10.1016--j.future.2017.04.017',
'b_issn': '0167-739X',
'b_issue_number': '1',
'b_journal': 'Future_Generation_Computer_Systems',
'b_journal_label': 'Future Generation Computer Systems',
'b_pages': '235',
'b_publication_month': '2',
'b_publication_year': '2018',
'b_publisher': 'Elsevier',
'b_publisher_label': 'Elsevier',
'b_pure_bibliography_id': 'b56e503067994b389d4eced98fae2206',
'b_type': 'Article',
'b_volume': '79'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('d0e972a611e44a80b8014f1069bfad88',
{'b_author_labels': ['van_Spanje, J'],
'b_authors': ['van_Spanje_J'],
'b_document': 'Controlling_the_Electoral_Marketplace-How_Established_Parties_Ward_Off_Competition',
'b_document_label': 'Controlling the Electoral Marketplace-How Established '
'Parties Ward Off Competition',
'b_doi': '10.1007--978-3-319-58202-3',
'b_isbn': '9783319582016',
'b_publication_year': '2018',
'b_publisher': 'Palgrave_Macmillan',
'b_publisher_label': 'Palgrave Macmillan',
'b_pure_bibliography_id': 'd0e972a611e44a80b8014f1069bfad88',
'b_type': 'Book'})
<BLANKLINE>
>>> bib_poor.enrich_with(target_bibliography_object=bib_rich, field_to_match_in_bibliographies='b_doi')
<BLANKLINE>
Enrichment completed successfully.
Existing entries enriched: 2
Fields added to existing entries: 12
New entries added: 0
>>> bib_poor.preview(100)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('b56e503067994b389d4eced98fae2206',
{'b_abstract': 'Attacks against network infrastructures can be detected by '
'Intrusion Detection Systems (IDS). Still reaction to these '
'events are often limited by the lack of larger contextual '
'information in which they occurred. In this paper we present '
'CoreFlow, a framework for the correlation and enrichment of '
'IDS data with network flow information. CoreFlow ingests data '
'from the Bro IDS and augments this with flow data from the '
'devices in the network. By doing this the network providers '
'are able to reconstruct more precisely the route followed by '
'the malicious flows. This enables them to devise tailored '
'countermeasures, e.g. blocking close to the source of the '
'attack. We tested the initial CoreFlow prototype in the ESnet '
'network, using inputs from 3 Bro systems and more than 50 '
'routers.',
'b_author_labels': ['Koning, R', 'Buraglio, N', 'de_Laat, CTAM', 'Grosso, P'],
'b_authors': ['Koning_R', 'Buraglio_N', 'de_Laat_CTAM', 'Grosso_P'],
'b_document': 'CoreFlow-Enriching_Bro_security_events_using_network_traffic_monitoring_data',
'b_document_label': 'CoreFlow-Enriching Bro security events using network '
'traffic monitoring data',
'b_doi': '10.1016--j.future.2017.04.017',
'b_issn': '0167-739X',
'b_issue_number': '1',
'b_journal': 'Future_Generation_Computer_Systems',
'b_journal_label': 'Future Generation Computer Systems',
'b_pages': '235',
'b_publication_month': '2',
'b_publication_year': '2018',
'b_publisher': 'Elsevier',
'b_publisher_label': 'Elsevier',
'b_pure_bibliography_id': 'b56e503067994b389d4eced98fae2206',
'b_type': 'Article',
'b_volume': '79'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('d0e972a611e44a80b8014f1069bfad88',
{'b_author_labels': ['van_Spanje, J'],
'b_authors': ['van_Spanje_J'],
'b_document': 'Controlling_the_Electoral_Marketplace-How_Established_Parties_Ward_Off_Competition',
'b_document_label': 'Controlling the Electoral Marketplace-How Established '
'Parties Ward Off Competition',
'b_doi': '10.1007--978-3-319-58202-3',
'b_isbn': '9783319582016',
'b_publication_year': '2018',
'b_publisher': 'Palgrave_Macmillan',
'b_publisher_label': 'Palgrave Macmillan',
'b_pure_bibliography_id': 'd0e972a611e44a80b8014f1069bfad88',
'b_type': 'Book'})
<BLANKLINE>
>>> #===========================================
>>> # EXAMPLE: MERGE BIB AND CSV(Open Citations)
>>> #===========================================
>>> vu_bibliography = Bibliography()
>>> vu_bibliography.importBibtex('example_data//oc_query_complementary_bibtex_for_merging.bib')
Parsing of example_data//oc_query_complementary_bibtex_for_merging.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//oc_query_complementary_bibtex_for_merging.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_abstract': 1,
'b_author_labels': 2,
'b_authors': 2,
'b_document': 2,
'b_document_label': 2,
'b_doi': 2,
'b_publication_year': 1,
'b_publisher': 1,
'b_publisher_label': 1,
'b_pure_bibliography_id': 2,
'b_type': 2}
<BLANKLINE>
<BLANKLINE>
>>> oc_bibliography = Bibliography()
>>> oc_bibliography.importCsv(path_of_file_to_import='example_data/oc_query_2.2_results_short_sample_for_merging.csv',
... csv_delimiter_character=',',
... field_value_list_separator=' | ',
... id_column_header='journal_article',
... conversion_arguments_list='open citations',
... cleaning_algorithm='default')
Parsing of "example_data/oc_query_2.2_results_short_sample_for_merging.csv" started
Conversion from ListData to Bibliography object started
Conversion completed. 3 out of 3 ListData rows converted to Bibliography object entries
<BLANKLINE>
Formatting of Bibliography entries started
"example_data/oc_query_2.2_results_short_sample_for_merging.csv" parsed and imported into Bibliography object in memory
<BLANKLINE>
Number of fields in the parsed bibliography:
{'b_author_labels': 3,
'b_authors': 3,
'b_cited': 3,
'b_cited_by': 3,
'b_document': 3,
'b_document_label': 3,
'b_doi': 3,
'b_issue_number': 3,
'b_journal': 3,
'b_journal_label': 3,
'b_open_citations_id': 3,
'b_pages': 2,
'b_pmid': 3,
'b_publication_year': 3,
'b_publisher': 3,
'b_publisher_label': 3,
'b_type': 3,
'b_url': 3,
'b_volume': 3}
>>> # compare entries in two bibliographies
>>> from pprint import pprint
>>> # entry in the the poorer bibliography
>>> pprint(vu_bibliography.getEntriesByField('b_doi', '10.1186/s13034-015-0062-7'), compact=True)
[{'b_abstract': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed '
'do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua. Ut enim ad minim veniam, quis nostrud exercitation '
'ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis '
'aute irure dolor in reprehenderit in voluptate velit esse '
'cillum dolore eu fugiat nulla pariatur. Excepteur sint '
'occaecat cupidatat non proident, sunt in culpa qui officia '
'deserunt mollit anim id est laborum.',
'b_author_labels': ['Maria, Z'],
'b_authors': ['Maria_Z'],
'b_document': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_document_label': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_doi': '10.1186/s13034-015-0062-7',
'b_pure_bibliography_id': 'b466af64b57f4089b0596f133f4862d2',
'b_type': 'Article'}]
>>> # entry in the the richer bibliography
>>> pprint(oc_bibliography.getEntriesByField('b_doi', '10.1186/s13034-015-0062-7'), compact=True)
[{'b_author_labels': ['Zetterqvist, M'],
'b_authors': ['Zetterqvist_M'],
'b_cited': ['https://w3id.org/oc/corpus/br/37961',
'https://w3id.org/oc/corpus/br/38250',
'https://w3id.org/oc/corpus/br/135448',
'https://w3id.org/oc/corpus/br/135458',
'https://w3id.org/oc/corpus/br/177639',
'https://w3id.org/oc/corpus/br/177648',
'https://w3id.org/oc/corpus/br/177653',
'https://w3id.org/oc/corpus/br/177661',
'https://w3id.org/oc/corpus/br/177774',
'https://w3id.org/oc/corpus/br/362419',
'https://w3id.org/oc/corpus/br/362426',
'https://w3id.org/oc/corpus/br/362438',
'https://w3id.org/oc/corpus/br/607811',
'https://w3id.org/oc/corpus/br/1270766',
'https://w3id.org/oc/corpus/br/1560911',
'https://w3id.org/oc/corpus/br/1794850',
'https://w3id.org/oc/corpus/br/1881397',
'https://w3id.org/oc/corpus/br/2258672',
'https://w3id.org/oc/corpus/br/2907029',
'https://w3id.org/oc/corpus/br/2907034',
'https://w3id.org/oc/corpus/br/2907035',
'https://w3id.org/oc/corpus/br/2907042',
'https://w3id.org/oc/corpus/br/2907056',
'https://w3id.org/oc/corpus/br/3346205',
'https://w3id.org/oc/corpus/br/3567493',
'https://w3id.org/oc/corpus/br/3567495',
'https://w3id.org/oc/corpus/br/3949890',
'https://w3id.org/oc/corpus/br/5106137',
'https://w3id.org/oc/corpus/br/5441063',
'https://w3id.org/oc/corpus/br/5441066',
'https://w3id.org/oc/corpus/br/5441085',
'https://w3id.org/oc/corpus/br/5656230',
'https://w3id.org/oc/corpus/br/6060536',
'https://w3id.org/oc/corpus/br/6063037',
'https://w3id.org/oc/corpus/br/6449521',
'https://w3id.org/oc/corpus/br/6486152',
'https://w3id.org/oc/corpus/br/6486162',
'https://w3id.org/oc/corpus/br/6919305',
'https://w3id.org/oc/corpus/br/6919323',
'https://w3id.org/oc/corpus/br/7558746',
'https://w3id.org/oc/corpus/br/7560541',
'https://w3id.org/oc/corpus/br/7560644',
'https://w3id.org/oc/corpus/br/7560645',
'https://w3id.org/oc/corpus/br/7560646',
'https://w3id.org/oc/corpus/br/7560647',
'https://w3id.org/oc/corpus/br/7560648',
'https://w3id.org/oc/corpus/br/7560651',
'https://w3id.org/oc/corpus/br/7560652',
'https://w3id.org/oc/corpus/br/7560653',
'https://w3id.org/oc/corpus/br/7560654',
'https://w3id.org/oc/corpus/br/7560655',
'https://w3id.org/oc/corpus/br/7560656',
'https://w3id.org/oc/corpus/br/7560657',
'https://w3id.org/oc/corpus/br/7560658',
'https://w3id.org/oc/corpus/br/7560659',
'https://w3id.org/oc/corpus/br/7560660',
'https://w3id.org/oc/corpus/br/7560661',
'https://w3id.org/oc/corpus/br/7560662',
'https://w3id.org/oc/corpus/br/7560663',
'https://w3id.org/oc/corpus/br/7560664',
'https://w3id.org/oc/corpus/br/7560665',
'https://w3id.org/oc/corpus/br/7560666'],
'b_cited_by': 'https://w3id.org/oc/corpus/br/362415',
'b_document': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_document_label': 'The DSM-5 diagnosis of nonsuicidal self-injury '
'disorder-a review of the empirical literature',
'b_doi': '10.1186/s13034-015-0062-7',
'b_issue_number': '1',
'b_journal': 'Child_and_Adolescent_Psychiatry_and_Mental_Health-Child_Adolesc_Psychiatry_Ment_Health',
'b_journal_label': 'Child and Adolescent Psychiatry and Mental Health-Child '
'Adolesc Psychiatry Ment Health',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/362418',
'b_pages': ' ',
'b_pmid': '26417387',
'b_publication_year': '2015',
'b_publisher': 'Springer_Science_%2B_Business_Media',
'b_publisher_label': 'Springer Science + Business Media',
'b_type': 'Journal Article',
'b_url': 'http://dx.doi.org/10.1186/s13034-015-0062-7',
'b_volume': '9'}]
>>> # another entry in the the poorer bibliography
>>> pprint(vu_bibliography.getEntriesByField('b_doi', '10.1016/s0090-8258(03)00087-8'), compact=True)
[{'b_author_labels': ['Straughn, MJ', 'Huh, WK'],
'b_authors': ['Straughn_MJ', 'Huh_WK'],
'b_document': 'Stage_IC_adenocarcinoma_of_the_endometrium-survival_comparisons_of_surgically_staged_patients_with_and_without_adjuvant_radiation_therapy%C3%A2%C2%98%C2%86%C3%A2%C2%98%C2%86Presented_at_the_33rd_Annual_Meeting_of_Gynecologic_Oncologists_Miami_FL_March_2002',
'b_document_label': 'Stage IC adenocarcinoma of the endometrium-survival '
'comparisons of surgically staged patients with and '
'without adjuvant radiation '
'therapyâ\x98\x86â\x98\x86Presented at the 33rd Annual '
'Meeting of Gynecologic Oncologists, Miami, FL, March '
'2002.',
'b_doi': '10.1016/s0090-8258(03)00087-8',
'b_publication_year': '2003',
'b_publisher': 'Elsevier_BV',
'b_publisher_label': 'Elsevier B.V.',
'b_pure_bibliography_id': 'b3cd7336ed9a48bfaed37af3a2e593c6',
'b_type': 'Article'}]
>>> # another entry in the the richer bibliography
>>> pprint(oc_bibliography.getEntriesByField('b_doi', '10.1016/s0090-8258(03)00087-8'), compact=True)
[{'b_author_labels': ['Straughn, JM', 'Huh, WK', 'Orr, JW', 'Kelly, FJ',
'Roland, PY', 'Gold, MA', 'Powell, M', 'Mutch, DG',
'Partridge, EE', 'Kilgore, LC', 'Barnes, MN',
'Austin, JM', 'Alvarez, RD'],
'b_authors': ['Straughn_JM', 'Huh_WK', 'Orr_JW', 'Kelly_FJ', 'Roland_PY',
'Gold_MA', 'Powell_M', 'Mutch_DG', 'Partridge_EE', 'Kilgore_LC',
'Barnes_MN', 'Austin_JM', 'Alvarez_RD'],
'b_cited': '',
'b_cited_by': 'https://w3id.org/oc/corpus/br/1',
'b_document': 'Stage_IC_adenocarcinoma_of_the_endometrium-survival_comparisons_of_surgically_staged_patients_with_and_without_adjuvant_radiation_therapyaaPresented_at_the_33rd_Annual_Meeting_of_Gynecologic_Oncologists-Miami-FL-March_2002',
'b_document_label': 'Stage IC adenocarcinoma of the endometrium-survival '
'comparisons of surgically staged patients with and '
'without adjuvant radiation therapyaaPresented at the '
'33rd Annual Meeting of Gynecologic '
'Oncologists-Miami-FL-March 2002.',
'b_doi': '10.1016/s0090-8258(03)00087-8',
'b_issue_number': '2',
'b_journal': 'Gynecologic_Oncology',
'b_journal_label': 'Gynecologic Oncology',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/392',
'b_pages': '295--300',
'b_pmid': '12713994',
'b_publication_year': '2003',
'b_publisher': 'Elsevier_BV',
'b_publisher_label': 'Elsevier BV',
'b_type': 'Journal Article',
'b_url': 'http://dx.doi.org/10.1016/s0090-8258%2803%2900087-8',
'b_volume': '89'}]
>>> # merge poorer and richer bibliographies
>>> vu_bibliography.enrich_with(oc_bibliography, field_to_match_in_bibliographies='b_doi')
<BLANKLINE>
Enrichment completed successfully.
Existing entries enriched: 2
Fields added to existing entries: 23
New entries added: 0
>>> vu_bibliography.preview(100)
<BLANKLINE>
----------------------------------ENTRY 1----------------------------------
('b466af64b57f4089b0596f133f4862d2',
{'b_abstract': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed '
'do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua. Ut enim ad minim veniam, quis nostrud exercitation '
'ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis '
'aute irure dolor in reprehenderit in voluptate velit esse '
'cillum dolore eu fugiat nulla pariatur. Excepteur sint '
'occaecat cupidatat non proident, sunt in culpa qui officia '
'deserunt mollit anim id est laborum.',
'b_author_labels': ['Maria, Z'],
'b_authors': ['Maria_Z'],
'b_cited': ['https://w3id.org/oc/corpus/br/37961',
'https://w3id.org/oc/corpus/br/38250',
'https://w3id.org/oc/corpus/br/135448',
'https://w3id.org/oc/corpus/br/135458',
'https://w3id.org/oc/corpus/br/177639',
'https://w3id.org/oc/corpus/br/177648',
'https://w3id.org/oc/corpus/br/177653',
'https://w3id.org/oc/corpus/br/177661',
'https://w3id.org/oc/corpus/br/177774',
'https://w3id.org/oc/corpus/br/362419',
'https://w3id.org/oc/corpus/br/362426',
'https://w3id.org/oc/corpus/br/362438',
'https://w3id.org/oc/corpus/br/607811',
'https://w3id.org/oc/corpus/br/1270766',
'https://w3id.org/oc/corpus/br/1560911',
'https://w3id.org/oc/corpus/br/1794850',
'https://w3id.org/oc/corpus/br/1881397',
'https://w3id.org/oc/corpus/br/2258672',
'https://w3id.org/oc/corpus/br/2907029',
'https://w3id.org/oc/corpus/br/2907034',
'https://w3id.org/oc/corpus/br/2907035',
'https://w3id.org/oc/corpus/br/2907042',
'https://w3id.org/oc/corpus/br/2907056',
'https://w3id.org/oc/corpus/br/3346205',
'https://w3id.org/oc/corpus/br/3567493',
'https://w3id.org/oc/corpus/br/3567495',
'https://w3id.org/oc/corpus/br/3949890',
'https://w3id.org/oc/corpus/br/5106137',
'https://w3id.org/oc/corpus/br/5441063',
'https://w3id.org/oc/corpus/br/5441066',
'https://w3id.org/oc/corpus/br/5441085',
'https://w3id.org/oc/corpus/br/5656230',
'https://w3id.org/oc/corpus/br/6060536',
'https://w3id.org/oc/corpus/br/6063037',
'https://w3id.org/oc/corpus/br/6449521',
'https://w3id.org/oc/corpus/br/6486152',
'https://w3id.org/oc/corpus/br/6486162',
'https://w3id.org/oc/corpus/br/6919305',
'https://w3id.org/oc/corpus/br/6919323',
'https://w3id.org/oc/corpus/br/7558746',
'https://w3id.org/oc/corpus/br/7560541',
'https://w3id.org/oc/corpus/br/7560644',
'https://w3id.org/oc/corpus/br/7560645',
'https://w3id.org/oc/corpus/br/7560646',
'https://w3id.org/oc/corpus/br/7560647',
'https://w3id.org/oc/corpus/br/7560648',
'https://w3id.org/oc/corpus/br/7560651',
'https://w3id.org/oc/corpus/br/7560652',
'https://w3id.org/oc/corpus/br/7560653',
'https://w3id.org/oc/corpus/br/7560654',
'https://w3id.org/oc/corpus/br/7560655',
'https://w3id.org/oc/corpus/br/7560656',
'https://w3id.org/oc/corpus/br/7560657',
'https://w3id.org/oc/corpus/br/7560658',
'https://w3id.org/oc/corpus/br/7560659',
'https://w3id.org/oc/corpus/br/7560660',
'https://w3id.org/oc/corpus/br/7560661',
'https://w3id.org/oc/corpus/br/7560662',
'https://w3id.org/oc/corpus/br/7560663',
'https://w3id.org/oc/corpus/br/7560664',
'https://w3id.org/oc/corpus/br/7560665',
'https://w3id.org/oc/corpus/br/7560666'],
'b_cited_by': 'https://w3id.org/oc/corpus/br/362415',
'b_document': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_document_label': 'The_DSM-5_diagnosis_of_nonsuicidal_self-injury_disorder-a_review_of_the_empirical_literature',
'b_doi': '10.1186/s13034-015-0062-7',
'b_issue_number': '1',
'b_journal': 'Child_and_Adolescent_Psychiatry_and_Mental_Health-Child_Adolesc_Psychiatry_Ment_Health',
'b_journal_label': 'Child and Adolescent Psychiatry and Mental Health-Child '
'Adolesc Psychiatry Ment Health',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/362418',
'b_pages': ' ',
'b_pmid': '26417387',
'b_publication_year': '2015',
'b_publisher': 'Springer_Science_%2B_Business_Media',
'b_publisher_label': 'Springer Science + Business Media',
'b_pure_bibliography_id': 'b466af64b57f4089b0596f133f4862d2',
'b_type': 'Article',
'b_url': 'http://dx.doi.org/10.1186/s13034-015-0062-7',
'b_volume': '9'})
<BLANKLINE>
----------------------------------ENTRY 2----------------------------------
('b3cd7336ed9a48bfaed37af3a2e593c6',
{'b_author_labels': ['Straughn, MJ', 'Huh, WK'],
'b_authors': ['Straughn_MJ', 'Huh_WK'],
'b_cited': '',
'b_cited_by': 'https://w3id.org/oc/corpus/br/1',
'b_document': 'Stage_IC_adenocarcinoma_of_the_endometrium-survival_comparisons_of_surgically_staged_patients_with_and_without_adjuvant_radiation_therapy%C3%A2%C2%98%C2%86%C3%A2%C2%98%C2%86Presented_at_the_33rd_Annual_Meeting_of_Gynecologic_Oncologists_Miami_FL_March_2002',
'b_document_label': 'Stage IC adenocarcinoma of the endometrium-survival '
'comparisons of surgically staged patients with and '
'without adjuvant radiation '
'therapyâ\x98\x86â\x98\x86Presented at the 33rd Annual '
'Meeting of Gynecologic Oncologists, Miami, FL, March '
'2002.',
'b_doi': '10.1016/s0090-8258(03)00087-8',
'b_issue_number': '2',
'b_journal': 'Gynecologic_Oncology',
'b_journal_label': 'Gynecologic Oncology',
'b_open_citations_id': 'https://w3id.org/oc/corpus/br/392',
'b_pages': '295--300',
'b_pmid': '12713994',
'b_publication_year': '2003',
'b_publisher': 'Elsevier_BV',
'b_publisher_label': 'Elsevier B.V.',
'b_pure_bibliography_id': 'b3cd7336ed9a48bfaed37af3a2e593c6',
'b_type': 'Article',
'b_url': 'http://dx.doi.org/10.1016/s0090-8258%2803%2900087-8',
'b_volume': '89'})
<BLANKLINE>
"""
# reset instance counters (in case this is not the first merge operation on the instance, this is necessary)
instance.no_of_entries_enriched_in_last_operation = 0
instance.no_of_existing_fields_enriched_in_last_operation = 0
instance.no_of_entries_added_in_last_operation = 0
instance.no_of_fields_added_in_last_operation = 0
other_bibliography = target_bibliography_object
target_field_name = field_to_match_in_bibliographies
for each_entry_id_in_other_bibliography, each_entry_data_in_other_bibliography in other_bibliography.entries.items():
# for logging
last_entry_is_enriched = False
last_entry_is_added = False
# if a field name and value(e.g., doi) from other bibliography is found in the current one, enrich
# the corresponding entry in the current dataset with this field name and its value
# TODO: This try-except block should either be made more specific or replaced with an if-else block
try:
each_target_value_in_other_bibliography = each_entry_data_in_other_bibliography[target_field_name]
# Make sure that only one entry in self matches the target value (e.g., doi)
matching_entry_in_this_bibliography = instance.getEntriesByField(field_name=target_field_name, field_value=each_target_value_in_other_bibliography)
matching_entry_ids_in_this_bibliography = instance._field_values_registry[target_field_name][each_target_value_in_other_bibliography]
if len(matching_entry_ids_in_this_bibliography) > 1:
# TODO: If a DOI (or another target value) appears in multiple entries, only the first entry is
# TODO: ... enriched, and the other occurrences is simply left alone.
# TODO: ... A 'merge_duplicate_entries' function should be implemented and used during cleaning for
# TODO: ... cleaner behavior
# If multiple matches is the case (e.g., a DOI appears in multiple entries in self bibliography)
# only set the first occurrence for enrichment
matching_entry_id_in_this_bibliography = matching_entry_ids_in_this_bibliography[0]
# the old error in case there is more than one matching (e.g.) DOI:
#raise ValueError("More than one ID (%s) in the source bibliography returned with the field name '%s' and value '%s'."
# % (matching_entry_ids_in_this_bibliography, target_field_name, each_target_value_in_other_bibliography))
else:
matching_entry_id_in_this_bibliography = matching_entry_ids_in_this_bibliography[0]
# Enrich fields of matching entries
existing_field_names_in_matching_entry_of_this_bibliography = list(matching_entry_in_this_bibliography[0].keys())
for each_field_name_in_entry_from_other_bibliography, each_field_value_in_entry_from_other_bibliography in each_entry_data_in_other_bibliography.items():
if each_field_name_in_entry_from_other_bibliography not in existing_field_names_in_matching_entry_of_this_bibliography:
instance.entries[matching_entry_id_in_this_bibliography][each_field_name_in_entry_from_other_bibliography] \
= each_field_value_in_entry_from_other_bibliography
# Logging
instance.no_of_existing_fields_enriched_in_last_operation += 1
last_entry_is_enriched = True
else: # if field already exists in self bibliography, do nothing
pass
# TODO: Merge function is not thoroughly tested and it should be
except: # if the field name and value from the other bib is not found
if method == 'merge': # add field name and value to a new entry (if in merge mode)
for each_field_name_in_entry_from_other_bibliography, each_field_value_in_entry_from_other_bibliography in each_entry_data_in_other_bibliography.items():
instance.setEntry(each_entry_id_in_other_bibliography, each_field_name_in_entry_from_other_bibliography, each_field_value_in_entry_from_other_bibliography)
###### Logging ######################################################################################################
instance.no_of_fields_added_in_last_operation += 1
last_entry_is_added = True
else: # not in merge mode, do nothing
pass
if last_entry_is_enriched:
instance.no_of_entries_enriched_in_last_operation += 1
elif last_entry_is_added:
instance.no_of_entries_added_in_last_operation += 1
lines_of_console_message = [
'Existing entries enriched: %d' % instance.no_of_entries_enriched_in_last_operation,
'Fields added to existing entries: %d' % instance.no_of_existing_fields_enriched_in_last_operation,
'New entries added: %d' % instance.no_of_entries_added_in_last_operation
#'New fields added with new entries: %d' % instance.no_of_fields_added_in_last_operation # currently
# ... unnecessary to report
]
from meta.consoleOutput import ConsoleOutput
console = ConsoleOutput('log.txt')
console.log_list_with_caption('\nEnrichment completed successfully.', lines_of_console_message,
print_list_length_with_caption=False, add_timestamp_in_file=True)
###### Logging END #################################################################################################
###################################################################################################################
################################################# QUERY FUNCTIONS #################################################
###################################################################################################################
def getEntryById(instance, entry_id, field_name=""):
"""
Searches bibliography instance registry for given entry id or (optionally) entry_id-field_name combination
(e.g., '1234155125'-author), and returns a the matching entity.
Args:
entry_id (str): unique entry id of the bibliography entry
field_name (str): name of the bibliography field. e.g., author, title.
Returns:
If field_name is not specified: The entry that matches with the provided entry_id.
If field_name is specified: The value of the field of the entry which matches with the provided entry_id.
Examples:
>>> # Preparation:
>>> from triplicator.bibTools import Bibliography
>>> bibx = Bibliography()
>>> bibx.setEntry("01", "author", "John Can Lokman")
>>> bibx.setEntry("01", "title", "Test Book 1")
>>> # Example #1:
>>> bibx.getEntryById("01")
{'author': 'John Can Lokman', 'title': 'Test Book 1'}
>>> # Example #2:
>>> bibx.getEntryById("01", "title")
'Test Book 1'
"""
if field_name == "":
return instance.entries[entry_id]
else:
return instance.entries[entry_id][field_name]
def getEntriesByField(instance, field_name, field_value):
"""
Searches the bibliography instance registry for given field name-value combination (e.g., title-my_title), and returns a list of matching
bibliography entries.
Args:
field_name: Name of the field to be searched (e.g., "author").
field_value: Value of the field that is being searched (e.g., "John Lokman"
Returns:
A list of bibliography entries.
Examples:
# Example 1
>>> # preparation
>>> from triplicator.bibTools import Bibliography
>>> bibx = Bibliography()
>>> bibx.setEntry("01", "author", "John Can Lokman")
>>> bibx.setEntry("01", "title", "Test Book 1")
>>> bibx.setEntry("02", "author", "John Can Lokman")
>>> bibx.setEntry("02", "title", "Test Book 2")
>>> # method usage
>>> bibx.getEntriesByField("author", "John Can Lokman")
[{'author': 'John Can Lokman', 'title': 'Test Book 1'}, {'author': 'John Can Lokman', 'title': 'Test Book 2'}]
# Example 2
>>> # preparation:
>>> from triplicator.pybtexImporter import Pybtex_import
>>> pybtex_entries = Pybtex_import("example_data//test.bib").data.entries
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
>>> biby = Bibliography()
>>> for each_entry_id, each_entry in pybtex_entries.items():
... each_year = each_entry.fields["year"]
... biby.setEntry(each_entry_id, "b_year",each_year)
>>> # calling entries that has year 2017 using the method:
>>> biby.getEntriesByField("b_year", "2017")
[{'b_year': '2017'}, {'b_year': '2017'}, {'b_year': '2017'}]
>>> # a similar operation without using the method:
>>> for each_entry_id, each_entry_data in biby.entries.items():
... print(each_entry_data["b_year"])
2017
2017
2017
2016
"""
# Get matching ids from registry based on field name-value combination
matching_ids_list = instance._field_values_registry[field_name][field_value]
# Use matching ids that are returned to retrieve entities these ids correspond to
matching_entries_list = []
for each_id in matching_ids_list:
matching_entries_list.append(instance.getEntryById(each_id))
return matching_entries_list
def summarize(instance, print_header_text=False):
"""
Prints summary statistsics of the bibliograpghy.
Args:
print_header_text(bool): If true, prints a line before the start of summary, such as "Fields added
the parsed to bibliography:"
Returns:
Printed string on console
"""
from pprint import pprint
if print_header_text:
print('\n\n---------------------------------------------------------------------------------------------------')
print('Summary of fields in bibliography:')
pprint(instance._field_type_registry, compact=True)
def preview(instance, number_of_entries_to_preview=5, print_header_text=False):
"""
Prints a sample of entries from the bibliography.
Args:
number_of_entries_to_preview (int): The number of entities to be printed.
print_header_text: If true, prints a line before the start of summary, such as "Fields added
the parsed to bibliography:"
Returns:
Printed string on console
"""
from pprint import pprint
if print_header_text:
print('\n\n---------------------------------------------------------------------------------------------------')
print("\nPreview the parsed Bibliography object:")
for i, each_entry in enumerate(instance.entries.items()):
if i < number_of_entries_to_preview:
print('\n----------------------------------ENTRY ' + str(i+1) + '----------------------------------')
pprint(each_entry, compact=True)
else:
break
print('') # blank line (using an \n causes two blank lines)
def write_preview_to_log(instance, number_of_entries_to_preview=5, log_file_path='log.txt'):
"""
Prints a sample of entries from the bibliography to the specified log file.
"""
from preprocessor.Text_File import Log_File
log_file = Log_File(log_file_path)
log_file.append_line('Preview the parsed Bibliography object:')
for i, each_entry_id_entry_content_pair in enumerate(instance.entries.items()):
if i < number_of_entries_to_preview:
log_file.append_line('\n----------------------------------ENTRY ' + str(i+1) + '----------------------------------')
for each_key_value_pair in each_entry_id_entry_content_pair[1].items():
log_file.append_line(each_key_value_pair)
else:
break
###################################################################################################################
############################################ REGISTRY UPDATE FUNCTIONS ############################################
###################################################################################################################
def updateFieldValuesRegistry(instance, entry_id, field_name, field_value):
"""
Updates instance registry each time an entry is added to the bibliography instance. The registry allows
fast searching entries in the bibliography.
Args:
entry_id (str): id to be assigned to entry (e.g., '2341230u9078').
field_name(str): name of field (e.g., 'author')
field_value(str or list): value of the field (e.g., 'John Doe' )
Returns:
Nothing, but updates the instance._field_values_registry
Examples:
>>> # preparation
>>> from triplicator.bibTools import Bibliography
>>> bibx = Bibliography()
>>> # add first entry and see how instance registry is updated afterwards
>>> bibx.setEntry("01", "author", "John Can Lokman")
>>> bibx.setEntry("01", "title", "Test Book 1")
>>> print(bibx._field_values_registry)
{'author': {'John Can Lokman': ['01']}, 'title': {'Test Book 1': ['01']}}
>>> # add second entry and see how instance registry is updated afterwards
>>> bibx.setEntry("02", "title", "Test Book 2")
>>> bibx.setEntry("02", "author", "Stefan Schlobach")
>>> print(bibx._field_values_registry)
{'author': {'John Can Lokman': ['01'], 'Stefan Schlobach': ['02']}, 'title': {'Test Book 1': ['01'], 'Test Book 2': ['02']}}
TODO:
- Input should be treated as a search string rather than an exact string, so, for instance, a partial
author name can also be searched.
"""
# function must be able to accept a list of items, as this is sometimes the case (e.g., multiple authors
# ...for author field).
# Therefore, strings inputs are converted to lists to be compatible with the list processing facilities
field_value_list = []
if type(field_value) == str:
field_value_list = [field_value]
# Debugger
#print("input is this string:")
#print(field_value_list)
# Explicit statement. If the parameter is already a list, take it as it is
elif type(field_value) == list:
field_value_list = field_value
# Debugger
# print("input is this list:")
# print(field_value_list)
elif type(field_value) is None:
pass
#else:
# #raise Exception("'field_value' must be string or list. It is currently: " + str(field_value))
if field_value_list != []:
for each_field_value in field_value_list:
# if field_name (e.g., author) has never been added to the registry
if field_name not in instance._field_values_registry:
# Debugger
#print("SCENARIO 1")
#print("field_values_registry is currently:")
#print(instance._field_values_registry)
# Add dictionary entry for the field name-value pair and the entry id (e.g., {author:{"john x":[124515152])}
# NOTE: Below line can instead use instance._field_type_registry for more efficient search. This has to be tested
instance._field_values_registry[field_name] = {each_field_value: [entry_id]}
# Debugger
#print("field_name '" + str(field_name) + "' is not in registry")
#print("the current field value is: '" + each_field_value + "' (and it is not in registry).")
#print("field name and current field value is now added to registry with the following command:")
#print("instance._field_values_registry[field_name] = {each_field_value: [entry_id]}")
#print("the field_values_registry has now become:")
#print(instance._field_values_registry)
# if field name (e.g., 'author' field) is previously added to the registry...
elif field_name in instance._field_values_registry:
# Debugger
#print("SCENARIO 2")
#print("field_values_registry is currently:")
#print(instance._field_values_registry)
# ...but if field_value (e.g., author's name) has never been added to the registry
if each_field_value not in instance._field_values_registry[field_name]:
# add this field value (e.g., author) and set its value to a LIST that contains current entry_id
# so that this list can later be appended with other entry_ids.
# an example operation performed by the line below would be equivalent to:
# instance._field_values_registry[author] = {"John x": ["14578436002"]}
# which creates this dictionary entry:
# _field_values_registry:{ author:{ "John x": ["14578436002"] } }
instance._field_values_registry[field_name][each_field_value] = [entry_id]
# Debugger
#print("field_name '" + str(field_name) + "' has been found in the registry")
#print("current field value '" + each_field_value + "' has NOT been found in the registry")
#print("field name and current field value is now added to registry with the following command:")
#print("instance._field_values_registry[field_name] = {each_field_value: [entry_id]}")
#print("the field_values_registry has now become:")
#print(instance._field_values_registry)
# if field_value (e.g., author's name) is previously added to the registry
elif each_field_value in instance._field_values_registry[field_name]:
# Debugger
#print("SCENARIO 3")
#print("field_values_registry is currently:")
#print(instance._field_values_registry)
# append entry id to corresponding field value (e.g.,add entry_id to author name)
# an example operation performed by the line below would be equivalent to:
# instance._field_values_registry[author]["John x"].append["14578436002"]
# which creates this dictionary entry:
# _field_values_registry:{ author:{ "John x": ["some_previous_id", "14578436002"] } }
instance._field_values_registry[field_name][each_field_value].append(entry_id)
# Debugger
#print("field_name '" + str(field_name) + "' has been found in the registry")
#print("current field value '" + each_field_value + "' HAS been found in the registry")
#print("field name and current field value is now added to registry with the following command:")
#print("instance._field_values_registry[field_name] = {each_field_value: [entry_id]}")
#print("the field_values_registry has now become:")
#print(instance._field_values_registry)
# Debugger
#print("instance._field_values_registry is")
#print(instance._field_values_registry)
#print("")
def updateFieldTypesRegistry(instance, entry_id, field_name, field_value):
field_value_is_empty = False
if field_value == None or field_value == ' ' or field_value == '_':
field_value_is_empty = True
field_name_already_in_registry = False
if field_name in instance._field_type_registry:
field_name_already_in_registry = True
if not field_value_is_empty:
if not field_name_already_in_registry:
instance._field_type_registry[field_name] = 1
else:
instance._field_type_registry[field_name] += 1
###################################################################################################################
################################################ EXPORT FUNCTIONS #################################################
###################################################################################################################
def exportToCsv(instance, output_file_path, columns_to_ignore=None, new_header_names=None):
# TODO: This is a draft method, and it must be cleaned.
"""
Converts a Bibliography object file to CSV format with custom formatting options and writes a .csv file.
Args:
output_file_path(str): path of the .csv file to be written
columns_to_ignore(list): a list of strings that consists of column headers to be ignored during
export operation.
new_header_names(list): a list of strings that contains replacement column headers. Must be of same
length with the headers row in dataset.
- If 'columns_to_ignore' is provided, the length of the new_header_names list must be equal to the length of the headers row in the dataset.
- If 'columns_to_ignore' is not provided, the length of the new_header_names list must be equal to the length of the headers row in the dataset.
Returns:
New .csv file
"""
import csv
from preprocessor.ListData import ListData
list_data_bibliography = ListData()
list_data_bibliography.import_bibliography_object(instance)
#if columns_to_ignore != None:
list_data_bibliography.remove_columns(columns_to_ignore)
#if new_header_names != None:
list_data_bibliography.replace_headers(new_header_names)
#print(list_data_bibliography.headers_row)
#print(list_data_bibliography.data_rows)
#print(list_data_bibliography.dataset)
file = open(output_file_path, 'w', newline='', encoding='UTF-8')
writer = csv.writer(file, delimiter=';', quoting=csv.QUOTE_NONNUMERIC)
for row in list_data_bibliography.dataset:
writer.writerow(row)
print("\nBibliography is written to .csv file.")
file.close()
### CLEANING ###
import re
csv_file = open(output_file_path, 'r', encoding='UTF-8')
csv_string = csv_file.read()
csv_file.close()
csv_string_cleaned = re.sub('[\[\]\'\}{]', "", csv_string)
csv_file = open(output_file_path, 'w', encoding='UTF-8')
csv_file.write(csv_string_cleaned)
csv_file.close()
###################################################################################################################
############################################## FORMATTING FUNCTIONS ###############################################
###################################################################################################################
def replacePatternsAsPerDictionary(input_string, patterns_dictionary):
"""
Replaces patterns in an inputted string according to the key:value combinations in a dictionary. Useful for replacing
long name segments such as "_-_" with "-" in instance names during their creation.
Args:
input_string (str): String to be processed
patterns_dictionary(dict): A dictionary that contains the key:value combinations to be used in replacement.
Keys in the patterns dictionary should be target patterns, while values of the dictionary should be
the desired replacement patterns or characters.
Returns:
A string
Examples:
>>> patterns_dictionary = {"_-_" : "-",
... ": " : "-" }
>>> target_string = "Case_1_-_12:_Unknown"
>>> formatted_string = replacePatternsAsPerDictionary(target_string, patterns_dictionary)
>>> print(formatted_string)
Case_1-12:_Unknown
"""
import re
output_string = input_string
# replace undesired characters with the desired ones (e.g., ':' -> '-') using dictionary_of_patterns_to_replace
for each_target_pattern, each_replacement_pattern in patterns_dictionary.items():
output_string = re.sub(each_target_pattern, each_replacement_pattern, output_string)
return output_string
def standardizeCapitalization(input_string, algorithm):
"""
Converts title case words (e.g., ' The ') to lowercase e.g., ' the '). Allows conversion algorithms for multiple
scenarios (e.g., author names vs titles) and languages via keyword arguments of 'algorithm' parameter.
Args:
input_string (str): the string to be converted.
algorithm: capitalization algorithm to be used
Keyword Args:
"English title" (algorithm):
Returns:
The converted string
Examples:
>>> from triplicator.bibTools import standardizeCapitalization
>>> standardizeCapitalization("Phantom Of The Opera", "en_title")
'Phantom of the Opera'
"""
import re
formatted_string = input_string
# convert title case to lowercase (DBpedia format)
if algorithm is "en_title":
formatted_string = re.sub(" In ", " in ", formatted_string)
formatted_string = re.sub(" The ", " the ", formatted_string)
formatted_string = re.sub(" A ", " a ", formatted_string)
formatted_string = re.sub(" An ", " an ", formatted_string)
formatted_string = re.sub(" As ", " as ", formatted_string)
formatted_string = re.sub(" On ", " on ", formatted_string)
formatted_string = re.sub(" At ", " at ", formatted_string)
formatted_string = re.sub(" For ", " for ", formatted_string)
formatted_string = re.sub(" With ", " with ", formatted_string)
formatted_string = re.sub(" From ", " from ", formatted_string)
formatted_string = re.sub(" By ", " by ", formatted_string)
formatted_string = re.sub(" Of ", " of ", formatted_string)
formatted_string = re.sub(" Vs ", " vs ", formatted_string)
formatted_string = re.sub(" And ", " and ", formatted_string)
formatted_string = re.sub(" Be ", " be ", formatted_string)
formatted_string = re.sub(" Been ", " been ", formatted_string)
formatted_string = re.sub(" Not ", " not ", formatted_string)
formatted_string = re.sub(" Is ", " is ", formatted_string)
formatted_string = re.sub(" Isn\'t ", " isn\'t ", formatted_string)
formatted_string = re.sub(" Are ", " are ", formatted_string)
formatted_string = re.sub(" Aren\'t ", " aren\'t ", formatted_string)
formatted_string = re.sub(" Does ", " does ", formatted_string)
formatted_string = re.sub(" Doesn\'t ", " doesn\'t ", formatted_string)
formatted_string = re.sub(" Do ", " do ", formatted_string)
formatted_string = re.sub(" Don\'t ", " don\'t ", formatted_string)
formatted_string = re.sub(" Was ", " was ", formatted_string)
formatted_string = re.sub(" Wasn\'t ", " wasn\'t ", formatted_string)
formatted_string = re.sub(" Were ", " were ", formatted_string)
formatted_string = re.sub(" Weren\'t ", " weren\'t ", formatted_string)
formatted_string = re.sub(" Did ", " did ", formatted_string)
formatted_string = re.sub(" Didn\'t ", " didn\'t ", formatted_string)
# This list is not exhaustive
else:
raise Exception ('Unknown algorithm parameter: "' + algorithm + '". Please enter a valid capitalization algorithm such as "en_title".')
return formatted_string
def cleanAndFormatValues(target_field, algorithm):
"""
Formats the values in a given list or string according to the style specified by 'algorithm' parameter (e.g.,
'pybtex_author_instance_name'). All algorithms follow DBPedia naming conventions. For changing which characters
to omit, the internal variables 'pattern_of_characters_to_omit' and 'dictionary_of_patterns_to_replace' should be
modified.
Args:
target_field(str or list): string or list to be formatted.
For algorithm type 'pybtex_author_instance_name' the list contains 'person' objects.
algorithm (str): formatting style
Keyword Args:
"pybtex_author_instance_name" (algorithm): takes my_pybtex_instance.persons['author'] field and outputs in the format
['Lastname1_Lastname1_FN1', 'Lastname2_Lastname2_FN2'] (e.g.,
an input such as ["Van Belleghem, Frank", "Mendoza Rodriguez J.P."] would be formatted as
["Van_Belleghem_F", "Mendoza_Rodriguez_JP"])
"pybtex_author_label" (algorithm): takes my_pybtex_instance.persons['author'] field and outputs in the
format ['Lastname1_Lastname1, FN1', 'Lastname2_Lastname2, FN2'] (e.g.,
an input such as ["Van Belleghem, Frank", "Mendoza Rodriguez J.P."] would be formatted as
["Van Belleghem, F", "Mendoza Rodriguez, JP"])
"pybtex_document_instance_name" (algorithm): takes a given field's value
(e.g., my_pybtex_instance.fields["title"]) (which is a string), and returns a formatted string that is
suitable to be used as an instance name. During the operation, capitalization is standardized
(e.g., 'In' -> 'in'), undesired characters (e.g., [,],*,^) and punctuation are removed, and spaces are
replaced with underscores.
"pybtex_document_label" (algorithm): takes a given field's value
(e.g., my_pybtex_instance.fields["title"]) (which is a string), and returns a formatted string that is
suitable to be used as a label.During the operation, capitalization is standardized
(e.g., 'In' -> 'in').
"pybtex_topic_instance_name" (algorithm): takes my_pybtex_instance.fields["keywords"] field's value
(which is a string in the format of "keyword 1, keyword 2"), and returns a formatted string that is
suitable to be used as a list of 'topics' (in the format of ["keyword_1", "keyword_2"]).During the
operation, capitalization is standardized (e.g., 'In' -> 'in'), undesired characters (e.g., [,],*,^)
and punctuation are removed, and spaces are replaced with underscores.
"pybtex_topic_label" (algorithm): takes my_pybtex_instance.fields["keywords"] field's value
(which is a string in the format of "keyword 1, keyword 2"), and returns a formatted string that is
suitable to be used as a list of labels for 'topics' (in the format of ["keyword 1", "keyword 2"]).During the
operation, capitalization is standardized (e.g., 'In' -> 'in').
"oc_select_first_item_if_list" (algorithm): 'oc' prefix stands for 'OpenCitations' and these cleaning procedures
are made for OpenCitations data.
"oc_select_last_item_if_list_and_capitalize_first_letter" (algorithm)
"oc_select_last_item_if_list" (algorithm)
"capitalize_first_letter" (algorithm)
"none" (algorithm)
Raises:
Keyword Error: Keyword for 'algorithm' parameter does not exist.
Returns:
A version of the inputted values that is formatted according the specified algorithm.
Some algorithms and their corresponding outputs:
- "pybtex_author_instance_name": a list of strings
- "pybtex_author_label" : a list of strings
- "pybtex_document_instance_name" (algorithm): a string
- "pybtex_topic_instance_name" (algorithm): a list of strings in the format of
["topic_string_1", "topic_string_2"]. (note that the input was a string of keywords, in the format of
"topic string 1, topic string 2")
Examples:
>>> # preparation
>>> from triplicator.pybtexImporter import Pybtex_import
>>> from triplicator.bibTools import cleanAndFormatValues
>>> # import a bib file with pybtex and and extract entries (i.e., {entry_id:entries} pairs)
>>> pybtex_entries = Pybtex_import("example_data//test.bib").data.entries
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
>>> # AUTHOR FORMATTING
>>> # format all values (i.e., author names) in each entry's 'author' field
>>> for each_id, each_entry in pybtex_entries.items():
... cleanAndFormatValues(each_entry.persons["author"],"pybtex_author_instance_name")
... cleanAndFormatValues(each_entry.persons["author"],"pybtex_author_label")
['Jaschke_AC']
['Jaschke, AC']
['Lohr_A', 'Beunen_R', 'Savelli_H', 'Kalz_M', 'Ragas_A', 'Van_Belleghem_F']
['Lohr, A', 'Beunen, R', 'Savelli, H', 'Kalz, M', 'Ragas, A', 'Van_Belleghem, F']
['Mendoza_Rodriguez_JP', 'Wielhouwer_JL', 'Kirchler_ESMN']
['Mendoza_Rodriguez, JP', 'Wielhouwer, JL', 'Kirchler, ESMN']
['%40uthor_%CE%BDbn', 'Aaboud_M', 'Bentvelsen_S', 'Berge_D', 'Colijn_AP', 'de_Jong_P', 'Koffeman_E', 'Sabato_G', 'Salek_D', 'van_Vulpen_I', 'Vermeulen_JC', 'Vreeswijk_M']
['@uthor, νbn', 'Aaboud, M', 'Bentvelsen, S', 'Berge, D', 'Colijn, AP', 'de_Jong, P', 'Koffeman, E', 'Sabato, G', 'Salek, D', 'van_Vulpen, I', 'Vermeulen, JC', 'Vreeswijk, M']
>>> # AUTHOR FORMATTING + ADDING TO BIBLIOGRAPHY
>>> # format all values (i.e., author names) in each entry's 'author' field...
>>> # ...and this time add these formatted values to a Bibliography class instance
>>> my_bibliography = Bibliography()
>>> for each_id, each_entry in pybtex_entries.items():
... # create a shorter name for 'author' field
... each_author_field_values = each_entry.persons["author"]
...
... # format values in 'author' fields
... each_formatted_author_instance_names_list = cleanAndFormatValues(each_author_field_values, "pybtex_author_instance_name")
... each_formatted_author_labels_list = cleanAndFormatValues(each_entry.persons["author"],"pybtex_author_label")
...
... # add now-formatted entries to Bibliography object
... my_bibliography.setEntry(each_id, "b_author", each_formatted_author_instance_names_list)
... my_bibliography.setEntry(each_id, "b_author_labels", each_formatted_author_labels_list)
>>>
>>> # print the now-populated Bibliography object
>>> my_bibliography.entries
{'56fafbf2574947cc9cbbfae578a0a36d': {'b_author': ['Jaschke_AC'], 'b_author_labels': ['Jaschke, AC']}, 'd79d00c790984ab08240e997d077c332': {'b_author': ['Lohr_A', 'Beunen_R', 'Savelli_H', 'Kalz_M', 'Ragas_A', 'Van_Belleghem_F'], 'b_author_labels': ['Lohr, A', 'Beunen, R', 'Savelli, H', 'Kalz, M', 'Ragas, A', 'Van_Belleghem, F']}, 'a8781aa0eae047d1826a658f3545ce3f': {'b_author': ['Mendoza_Rodriguez_JP', 'Wielhouwer_JL', 'Kirchler_ESMN'], 'b_author_labels': ['Mendoza_Rodriguez, JP', 'Wielhouwer, JL', 'Kirchler, ESMN']}, '01b9c957875b4a96839c1bfd05ec6a31': {'b_author': ['%40uthor_%CE%BDbn', 'Aaboud_M', 'Bentvelsen_S', 'Berge_D', 'Colijn_AP', 'de_Jong_P', 'Koffeman_E', 'Sabato_G', 'Salek_D', 'van_Vulpen_I', 'Vermeulen_JC', 'Vreeswijk_M'], 'b_author_labels': ['@uthor, νbn', 'Aaboud, M', 'Bentvelsen, S', 'Berge, D', 'Colijn, AP', 'de_Jong, P', 'Koffeman, E', 'Sabato, G', 'Salek, D', 'van_Vulpen, I', 'Vermeulen, JC', 'Vreeswijk, M']}}
>>> # DOCUMENT INSTANCE NAME FORMATTING
>>> # Transform pybtex title string to document_instance_name:
>>> pybtex_entries = Pybtex_import("example_data//test.bib").data.entries
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
>>> my_bibliography = Bibliography()
>>>
>>> for each_entry_id, each_entry_data in pybtex_entries.items():
... each_document_instance_name = cleanAndFormatValues(each_entry_data.fields["title"], "pybtex_document_instance_name")
... my_bibliography.setEntry(each_entry_id, "document_instance_name", each_document_instance_name)
>>> my_bibliography.entries
{'56fafbf2574947cc9cbbfae578a0a36d': {'document_instance_name': 'Book_with_one_author'}, 'd79d00c790984ab08240e997d077c332': {'document_instance_name': 'Article_with_5_authors_with_and_notation'}, 'a8781aa0eae047d1826a658f3545ce3f': {'document_instance_name': 'Article_with_3_authors_with_mixed_notation'}, '01b9c957875b4a96839c1bfd05ec6a31': {'document_instance_name': 'Article_with_non-uri_safe_characters%3A%3C%3E%5B%5D_%40%25_to_WW_%E2%88%97%E2%86%92e%CE%BD%CE%BC%CE%BD_with_the_ATLAS_detector_at_%E2%88%9As%3D8_TeV'}}
>>> # TOPIC FORMATTING
>>> # transform pybtex keywords string to list of topics
>>> # (this example depends on imports made in previous examples)
>>> pybtex_entries = Pybtex_import("example_data//test.bib").data.entries
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
>>> # test diagnostic. necessary because try-except block would succeed even if the code does nothing
>>> no_of_keywords_processed = 0
>>>
>>> for each_pybtex_entry_id, each_pybtex_entry_data in pybtex_entries.items():
... # if pybtex entry contains keywords
... try:
... # extract keywords
... each_keyword_value_string = each_pybtex_entry_data.fields["keywords"]
... # format and tokenize keywords string in order to transform it into a list of topics
... each_formatted_keyword_list = cleanAndFormatValues(each_keyword_value_string, "pybtex_topic_instance_name")
... # add the newly generated topics to the Bibliography instance
... my_bibliography.setEntry(each_pybtex_entry_id, "b_topics", each_formatted_keyword_list)
...
... # test diagnostic
... no_of_keywords_processed = no_of_keywords_processed + 1
... except:
... pass
>>>
>>> # test diagnostic
>>> if no_of_keywords_processed == 0:
... raise Exception ("Test failed: No keywords processed inside the try-except block.")
"""
import re
from urllib.parse import quote
# special characters to omit from strings
# NOTE: currently, same characters are omitted for both labels and author names.
# ...for richer labels, this can be changed in a future revision.
# this variable is used by several cleaning/formatting algorithms within the current function
pattern_of_characters_to_omit = "[.,;\'\")(}{]"
# special character patterns to replace in strings
# in this dictionary, keys are target patterns and values are replacements
# note the spaces in the patterns (i.e., in keys of the dictionary)
# this dictionary is used by several cleaning/formatting algorithms within the current function
dictionary_of_patterns_to_replace = {
": ": "-",
" - ": "-",
# This pattern replacement is important for .bib files, as the pattern {"} is used to denote double quotation
# marks in .bib files. It should be used (at least) for replacing this pattern in titles and abstract strings.
# (otherwise, it leads to errors in .ttl files)
'\{"\}': "'"
}
# error handling for keywords of the 'algorithm' parameter is handled this way, as an 'else'
# statement at the end of elif block for keywords does not produce the same result ('else' too broad?)
# if the entered 'algorithm' parameter is unrecognized
algorithm_keywords_list = ["pybtex_author_instance_name",
"pybtex_author_label",
"pybtex_document_instance_name",
"pybtex_document_label",
"pybtex_topic_instance_name",
"pybtex_topic_label",
"open_citations_author_instance_name",
"open_citations_author_label",
"oc_select_first_item_if_list",
"oc_select_last_item_if_list_and_capitalize_first_letter",
"oc_select_last_item_if_list",
"capitalize_first_letter",
"none"]
# if algorithm not in algorithm_keywords_list:
# raise Exception ('Unknown algorithm parameter: "' + algorithm + '". Please enter a valid algorithm.')
#-------------------------------------------------------------------#
# FORMAT: AUTHOR INSTANCE NAME AND LABEL #
#-------------------------------------------------------------------#
# algorithm for pybtex author field
if algorithm is "pybtex_author_instance_name" or algorithm is "pybtex_author_label":
# TODO: Initial letter of last names and first name abbreviations should always be capitalized, even if this is not the case in input
# a more descriptive name for target_field
inputted_author_field_value_list = target_field
# two container variables for author (instance) names and author labels (which will later be needed by RDF format)
each_formatted_author_instance_list = []
each_formatted_author_label_list = []
each_abbreviated_first_names_string = ""
# for each "author" field value (which can hold multiple authors as a list) in the pybtex bib data
for each_author in inputted_author_field_value_list:
# extract and format each LAST NAME (if available)
try:
each_last_name = str(each_author.last()[0])
each_last_name_formatted = re.sub(pattern_of_characters_to_omit, "", each_last_name)
each_last_name_formatted = re.sub(" ", "_", each_last_name_formatted)
# extract and format each FIRST NAME (if available)
try:
# extract first name of a single author
each_first_names_string = str(each_author.first()[0])
# omit unwanted characters
each_first_names_string = re.sub(pattern_of_characters_to_omit, "", each_first_names_string)
# placeholder for output
each_abbreviated_first_names_string = ""
# for the first name's letters (e.g., "John", but "{John Some Middle Name}" is also possible)
for i, each_letter in enumerate(each_first_names_string):
# always add the first letter of a first name to abbreviated first name (i.e., the output)
if i == 0:
each_abbreviated_first_names_string = each_abbreviated_first_names_string + each_first_names_string[i]
# for the other letters that may be present in the first name string
else:
# if there are spaces or uppercase letter in the first name string
if " " in each_first_names_string or any(letter.isupper() for letter in each_first_names_string):
# add the character after space, or the capital letter, to the first name
if each_first_names_string[i-1] == " " or each_first_names_string[i].isupper() == True:
each_abbreviated_first_names_string = each_abbreviated_first_names_string + each_first_names_string[i]
# otherwise, don't do anything
else:
pass
# if there are no spaces of uppercase letters in the first name string, don't do anything additional
else:
pass
# if a first name is not available, don't do anything
except:
pass
except:
pass
# add extracted last and first names to the output variables (as author instance names or as labels, ...
# ...depending on the 'algorithm' parameter)
if algorithm is "pybtex_author_instance_name":
each_formatted_fullname = each_last_name_formatted + "_" + each_abbreviated_first_names_string
each_formatted_fullname = quote(each_formatted_fullname) # make safe to use as URI
each_formatted_author_instance_list.append(each_formatted_fullname)
elif algorithm is "pybtex_author_label":
each_formatted_fullname = each_last_name_formatted + ", " + each_abbreviated_first_names_string
each_formatted_author_label_list.append(each_formatted_fullname)
#each_formatted_author_label_list.append(each_last_name_formatted + ", " + each_first_name_formatted)
# return either author instance names or author labels depending on which 'algorithm' parameter is entered
if algorithm is "pybtex_author_instance_name":
return each_formatted_author_instance_list
elif algorithm is "pybtex_author_label":
return each_formatted_author_label_list
#-----------------------------------------------------------------------------------------------#
# FORMAT: OPEN CITATIONS AUTHOR INSTANCE NAME AND LABEL PREPROCESSOR #
#-----------------------------------------------------------------------------------------------#
if algorithm is "open_citations_author_instance_name" or algorithm is "open_citations_author_label":
from preprocessor.string_tools import Parameter_Value
target_field = Parameter_Value(target_field).convert_to_single_item_list_if_not_list()
authors_list = target_field
each_formatted_author_instance_list = []
each_formatted_author_label_list = []
each_last_name_formatted = ""
each_abbreviated_first_names_string = ""
for each_author_full_name in authors_list:
try:
each_author_split_names_list = each_author_full_name.split(' - ')
each_last_name = each_author_split_names_list[0]
each_first_names_string = each_author_split_names_list[1]
each_last_name_formatted = re.sub(pattern_of_characters_to_omit, "", each_last_name)
each_last_name_formatted = re.sub(" ", "_", each_last_name_formatted)
# extract and format each FIRST NAME (if available)
try:
# omit unwanted characters
each_first_names_string = re.sub(pattern_of_characters_to_omit, "", each_first_names_string)
# placeholder for output
each_abbreviated_first_names_string = ""
# for the first name's letters (e.g., "John", but "{John Some Middle Name}" is also possible)
for i, each_letter in enumerate(each_first_names_string):
# always add the first letter of a first name to abbreviated first name (i.e., the output)
if i == 0:
each_abbreviated_first_names_string = each_abbreviated_first_names_string + \
each_first_names_string[i]
# for the other letters that may be present in the first name string
else:
# if there are spaces or uppercase letter in the first name string
if " " in each_first_names_string or any(
letter.isupper() for letter in each_first_names_string):
# add the character after space, or the capital letter, to the first name
if each_first_names_string[i - 1] == " " \
or each_first_names_string[i].isupper() == True:
each_abbreviated_first_names_string = each_abbreviated_first_names_string + \
each_first_names_string[i]
# otherwise, don't do anything
else:
pass
# if there are no spaces of uppercase letters in the first name string, don't do anything additional
else:
pass
# if a first name is not available, don't do anything
except:
pass
except:
pass
# add extracted last and first names to the output variables (as author instance names or as labels, ...
# ...depending on the 'algorithm' parameter)
if algorithm is "open_citations_author_instance_name":
each_formatted_fullname = each_last_name_formatted + "_" + each_abbreviated_first_names_string
each_formatted_fullname = quote(each_formatted_fullname) # convert to uri-safe string
each_formatted_author_instance_list.append(each_formatted_fullname)
elif algorithm is "open_citations_author_label":
each_formatted_fullname = each_last_name_formatted + ", " + each_abbreviated_first_names_string
each_formatted_author_label_list.append(each_formatted_fullname)
# each_formatted_author_label_list.append(each_last_name_formatted + ", " + each_first_name_formatted)
# return either author instance names or author labels depending on which 'algorithm' parameter is entered
if algorithm is "open_citations_author_instance_name":
return each_formatted_author_instance_list
elif algorithm is "open_citations_author_label":
return each_formatted_author_label_list
# -------------------------------------------------------------------#
# FORMAT: DOCUMENT INSTANCE NAME AND LABEL #
# -------------------------------------------------------------------#
# if the task is title to document_instance_name conversion
elif algorithm is "pybtex_document_instance_name" or algorithm is "pybtex_document_label":
# in this case, the input (i.e., 'target_field') will be a string
# standardize capitalization in the string (e.g., ' At ' -> ' at ')
document_instance_name = standardizeCapitalization(target_field, "en_title")
# replace remaining undesired characters with the desired ones (e.g., ':' -> '-' or '{"}' with a double quote)
# using dictionary_of_patterns_to_replace
document_instance_name = replacePatternsAsPerDictionary(document_instance_name,
dictionary_of_patterns_to_replace)
if algorithm is "pybtex_document_instance_name":
# omit undesired characters from this string
document_instance_name = re.sub(pattern_of_characters_to_omit, "", document_instance_name)
document_instance_name = replacePatternsAsPerDictionary(document_instance_name,
dictionary_of_patterns_to_replace)
# replace spaces with underscores
document_instance_name = re.sub(" ", "_", document_instance_name)
# convert to uri-safe string
document_instance_name = quote(document_instance_name)
return document_instance_name
# ---------------------------------------------------------------------------#
# FORMAT: TOPIC INSTANCE NAME AND TOPIC LABEL #
# ---------------------------------------------------------------------------#
elif algorithm is "pybtex_topic_instance_name" or algorithm is "pybtex_topic_label":
# tokenize string input (which is in the format of "topic string 1, topic string 2")
# note that the split character is ", " and not ",". if space is not included, the first character of topics
# end up being a space e.g., " topic 1" instead of "topic 1" .
tokenized_topics_list = target_field.split(", ")
# placeholder for final output
formatted_topics_list = []
for each_topic_string in tokenized_topics_list:
# omit unwanted characters
each_formatted_topic = re.sub(pattern_of_characters_to_omit, "", each_topic_string)
# standardize capitalization in the string (e.g., ' At ' -> ' at ')
each_formatted_topic = standardizeCapitalization(each_formatted_topic, "en_title")
# if the task is to formatting the input as an instance name
if algorithm is "pybtex_topic_instance_name":
each_formatted_topic = each_formatted_topic.lower()
# replace remaining unwanted characters/patterns with the ones in dictionary_of_patterns_to_replace
each_formatted_topic = replacePatternsAsPerDictionary(each_formatted_topic,
dictionary_of_patterns_to_replace)
# replace spaces with underscores
each_formatted_topic = re.sub(" ", "_", each_formatted_topic)
# convert to uri-safe string
each_formatted_topic = quote(each_formatted_topic)
# if the task is to format as a topic label
elif algorithm is "pybtex_topic_label":
# keep the spaces (i.e., " " character) in topic strings
pass
# add the formatted topics list to the output variable
formatted_topics_list.append(each_formatted_topic)
return formatted_topics_list
# ---------------------------------------------------------------------------#
# MINIMIZE LISTS #
# ---------------------------------------------------------------------------#
# TODO: These list minimization/selection functions should be replaced with more graceful equivalents.
elif algorithm is "oc_select_first_item_if_list":
if type(target_field) is list:
inputted_list = target_field
return inputted_list[0]
else:
return target_field
elif algorithm is "oc_select_last_item_if_list_and_capitalize_first_letter":
from preprocessor.string_tools import String
if type(target_field) is list:
selected_element = target_field[-1]
else:
selected_element = target_field
formatted_element = String(selected_element).capitalize_first_letter().content
return formatted_element
elif algorithm is "oc_select_last_item_if_list":
from preprocessor.string_tools import String
if type(target_field) is list:
selected_element = target_field[-1]
else:
selected_element = target_field
return selected_element
elif algorithm is "capitalize_first_letter":
from preprocessor.string_tools import String
from preprocessor.string_tools import Parameter_Value
inputted_list = Parameter_Value(target_field).convert_to_single_item_list_if_not_list()
formatted_list = []
for each_element in inputted_list:
each_element = String(each_element).capitalize_first_letter().content
formatted_list.append(each_element)
return formatted_list[0]
# ---------------------------------------------------------------------------#
# NO FORMATTING: MINIMIZE LISTS (FOR NOW) #
# ---------------------------------------------------------------------------#
elif algorithm is "none":
# if no formatting is wanted, the target field values are returned as they are.
return target_field
def long_tests():
"""
Tests with long outputs are being run under this function
Additional tests for .convert_to_ttl():
>>> my_bibtex_file = Bibtex_File('example_data//vu_1k_test.bib')
>>> my_bibtex_file.convert_to_ttl(desired_version_suffix='0.0_test', desired_source_bibliography_name='vu')
Cleaning of "example_data//vu_1k_test.bib" started
[------------------------------------------------------------] 0% ...Cleaning example_data//vu_1k_test.bib
[=-----------------------------------------------------------] 0% ...Cleaning example_data//vu_1k_test.bib
[=-----------------------------------------------------------] 1% ...Cleaning example_data//vu_1k_test.bib
[=-----------------------------------------------------------] 2% ...Cleaning example_data//vu_1k_test.bib
[==----------------------------------------------------------] 2% ...Cleaning example_data//vu_1k_test.bib
[==----------------------------------------------------------] 3% ...Cleaning example_data//vu_1k_test.bib
[==----------------------------------------------------------] 4% ...Cleaning example_data//vu_1k_test.bib
[===---------------------------------------------------------] 4% ...Cleaning example_data//vu_1k_test.bib
[===---------------------------------------------------------] 5% ...Cleaning example_data//vu_1k_test.bib
[====--------------------------------------------------------] 5% ...Cleaning example_data//vu_1k_test.bib
[====--------------------------------------------------------] 6% ...Cleaning example_data//vu_1k_test.bib
[====--------------------------------------------------------] 7% ...Cleaning example_data//vu_1k_test.bib
[=====-------------------------------------------------------] 7% ...Cleaning example_data//vu_1k_test.bib
[=====-------------------------------------------------------] 8% ...Cleaning example_data//vu_1k_test.bib
[=====-------------------------------------------------------] 9% ...Cleaning example_data//vu_1k_test.bib
[======------------------------------------------------------] 9% ...Cleaning example_data//vu_1k_test.bib
[======------------------------------------------------------] 10% ...Cleaning example_data//vu_1k_test.bib
[=======-----------------------------------------------------] 10% ...Cleaning example_data//vu_1k_test.bib
[=======-----------------------------------------------------] 11% ...Cleaning example_data//vu_1k_test.bib
[=======-----------------------------------------------------] 12% ...Cleaning example_data//vu_1k_test.bib
[========----------------------------------------------------] 12% ...Cleaning example_data//vu_1k_test.bib
[========----------------------------------------------------] 13% ...Cleaning example_data//vu_1k_test.bib
[========----------------------------------------------------] 14% ...Cleaning example_data//vu_1k_test.bib
[=========---------------------------------------------------] 14% ...Cleaning example_data//vu_1k_test.bib
[=========---------------------------------------------------] 15% ...Cleaning example_data//vu_1k_test.bib
[==========--------------------------------------------------] 15% ...Cleaning example_data//vu_1k_test.bib
[==========--------------------------------------------------] 16% ...Cleaning example_data//vu_1k_test.bib
[==========--------------------------------------------------] 17% ...Cleaning example_data//vu_1k_test.bib
[===========-------------------------------------------------] 17% ...Cleaning example_data//vu_1k_test.bib
[===========-------------------------------------------------] 18% ...Cleaning example_data//vu_1k_test.bib
[===========-------------------------------------------------] 19% ...Cleaning example_data//vu_1k_test.bib
[============------------------------------------------------] 19% ...Cleaning example_data//vu_1k_test.bib
[============------------------------------------------------] 20% ...Cleaning example_data//vu_1k_test.bib
[=============-----------------------------------------------] 20% ...Cleaning example_data//vu_1k_test.bib
[=============-----------------------------------------------] 21% ...Cleaning example_data//vu_1k_test.bib
[=============-----------------------------------------------] 22% ...Cleaning example_data//vu_1k_test.bib
[==============----------------------------------------------] 22% ...Cleaning example_data//vu_1k_test.bib
[==============----------------------------------------------] 23% ...Cleaning example_data//vu_1k_test.bib
[==============----------------------------------------------] 24% ...Cleaning example_data//vu_1k_test.bib
[===============---------------------------------------------] 24% ...Cleaning example_data//vu_1k_test.bib
[===============---------------------------------------------] 25% ...Cleaning example_data//vu_1k_test.bib
[================--------------------------------------------] 25% ...Cleaning example_data//vu_1k_test.bib
[================--------------------------------------------] 26% ...Cleaning example_data//vu_1k_test.bib
[================--------------------------------------------] 27% ...Cleaning example_data//vu_1k_test.bib
[=================-------------------------------------------] 27% ...Cleaning example_data//vu_1k_test.bib
[=================-------------------------------------------] 28% ...Cleaning example_data//vu_1k_test.bib
[=================-------------------------------------------] 29% ...Cleaning example_data//vu_1k_test.bib
[==================------------------------------------------] 29% ...Cleaning example_data//vu_1k_test.bib
[==================------------------------------------------] 30% ...Cleaning example_data//vu_1k_test.bib
[===================-----------------------------------------] 30% ...Cleaning example_data//vu_1k_test.bib
[===================-----------------------------------------] 31% ...Cleaning example_data//vu_1k_test.bib
[===================-----------------------------------------] 32% ...Cleaning example_data//vu_1k_test.bib
[====================----------------------------------------] 32% ...Cleaning example_data//vu_1k_test.bib
[====================----------------------------------------] 33% ...Cleaning example_data//vu_1k_test.bib
[====================----------------------------------------] 34% ...Cleaning example_data//vu_1k_test.bib
[=====================---------------------------------------] 34% ...Cleaning example_data//vu_1k_test.bib
[=====================---------------------------------------] 35% ...Cleaning example_data//vu_1k_test.bib
[======================--------------------------------------] 35% ...Cleaning example_data//vu_1k_test.bib
[======================--------------------------------------] 36% ...Cleaning example_data//vu_1k_test.bib
[======================--------------------------------------] 37% ...Cleaning example_data//vu_1k_test.bib
[=======================-------------------------------------] 37% ...Cleaning example_data//vu_1k_test.bib
[=======================-------------------------------------] 38% ...Cleaning example_data//vu_1k_test.bib
[=======================-------------------------------------] 39% ...Cleaning example_data//vu_1k_test.bib
[========================------------------------------------] 39% ...Cleaning example_data//vu_1k_test.bib
[========================------------------------------------] 40% ...Cleaning example_data//vu_1k_test.bib
[=========================-----------------------------------] 40% ...Cleaning example_data//vu_1k_test.bib
[=========================-----------------------------------] 41% ...Cleaning example_data//vu_1k_test.bib
[=========================-----------------------------------] 42% ...Cleaning example_data//vu_1k_test.bib
[==========================----------------------------------] 42% ...Cleaning example_data//vu_1k_test.bib
[==========================----------------------------------] 43% ...Cleaning example_data//vu_1k_test.bib
[==========================----------------------------------] 44% ...Cleaning example_data//vu_1k_test.bib
[===========================---------------------------------] 44% ...Cleaning example_data//vu_1k_test.bib
[===========================---------------------------------] 45% ...Cleaning example_data//vu_1k_test.bib
[============================--------------------------------] 45% ...Cleaning example_data//vu_1k_test.bib
[============================--------------------------------] 46% ...Cleaning example_data//vu_1k_test.bib
[============================--------------------------------] 47% ...Cleaning example_data//vu_1k_test.bib
[=============================-------------------------------] 47% ...Cleaning example_data//vu_1k_test.bib
[=============================-------------------------------] 48% ...Cleaning example_data//vu_1k_test.bib
[=============================-------------------------------] 49% ...Cleaning example_data//vu_1k_test.bib
[==============================------------------------------] 49% ...Cleaning example_data//vu_1k_test.bib
[==============================------------------------------] 50% ...Cleaning example_data//vu_1k_test.bib
[===============================-----------------------------] 50% ...Cleaning example_data//vu_1k_test.bib
[===============================-----------------------------] 51% ...Cleaning example_data//vu_1k_test.bib
[===============================-----------------------------] 52% ...Cleaning example_data//vu_1k_test.bib
[================================----------------------------] 52% ...Cleaning example_data//vu_1k_test.bib
[================================----------------------------] 53% ...Cleaning example_data//vu_1k_test.bib
[================================----------------------------] 54% ...Cleaning example_data//vu_1k_test.bib
[=================================---------------------------] 54% ...Cleaning example_data//vu_1k_test.bib
[=================================---------------------------] 55% ...Cleaning example_data//vu_1k_test.bib
[==================================--------------------------] 55% ...Cleaning example_data//vu_1k_test.bib
[==================================--------------------------] 56% ...Cleaning example_data//vu_1k_test.bib
[==================================--------------------------] 57% ...Cleaning example_data//vu_1k_test.bib
[===================================-------------------------] 57% ...Cleaning example_data//vu_1k_test.bib
[===================================-------------------------] 58% ...Cleaning example_data//vu_1k_test.bib
[===================================-------------------------] 59% ...Cleaning example_data//vu_1k_test.bib
[====================================------------------------] 59% ...Cleaning example_data//vu_1k_test.bib
[====================================------------------------] 60% ...Cleaning example_data//vu_1k_test.bib
[=====================================-----------------------] 60% ...Cleaning example_data//vu_1k_test.bib
[=====================================-----------------------] 61% ...Cleaning example_data//vu_1k_test.bib
[=====================================-----------------------] 62% ...Cleaning example_data//vu_1k_test.bib
[======================================----------------------] 62% ...Cleaning example_data//vu_1k_test.bib
[======================================----------------------] 63% ...Cleaning example_data//vu_1k_test.bib
[======================================----------------------] 64% ...Cleaning example_data//vu_1k_test.bib
[=======================================---------------------] 64% ...Cleaning example_data//vu_1k_test.bib
[=======================================---------------------] 65% ...Cleaning example_data//vu_1k_test.bib
[========================================--------------------] 65% ...Cleaning example_data//vu_1k_test.bib
[========================================--------------------] 66% ...Cleaning example_data//vu_1k_test.bib
[========================================--------------------] 67% ...Cleaning example_data//vu_1k_test.bib
[=========================================-------------------] 67% ...Cleaning example_data//vu_1k_test.bib
[=========================================-------------------] 68% ...Cleaning example_data//vu_1k_test.bib
[=========================================-------------------] 69% ...Cleaning example_data//vu_1k_test.bib
[==========================================------------------] 69% ...Cleaning example_data//vu_1k_test.bib
[==========================================------------------] 70% ...Cleaning example_data//vu_1k_test.bib
[===========================================-----------------] 70% ...Cleaning example_data//vu_1k_test.bib
[===========================================-----------------] 71% ...Cleaning example_data//vu_1k_test.bib
[===========================================-----------------] 72% ...Cleaning example_data//vu_1k_test.bib
[============================================----------------] 72% ...Cleaning example_data//vu_1k_test.bib
[============================================----------------] 73% ...Cleaning example_data//vu_1k_test.bib
[============================================----------------] 74% ...Cleaning example_data//vu_1k_test.bib
[=============================================---------------] 74% ...Cleaning example_data//vu_1k_test.bib
[=============================================---------------] 75% ...Cleaning example_data//vu_1k_test.bib
[==============================================--------------] 75% ...Cleaning example_data//vu_1k_test.bib
[==============================================--------------] 76% ...Cleaning example_data//vu_1k_test.bib
[==============================================--------------] 77% ...Cleaning example_data//vu_1k_test.bib
[===============================================-------------] 77% ...Cleaning example_data//vu_1k_test.bib
[===============================================-------------] 78% ...Cleaning example_data//vu_1k_test.bib
[===============================================-------------] 79% ...Cleaning example_data//vu_1k_test.bib
[================================================------------] 79% ...Cleaning example_data//vu_1k_test.bib
[================================================------------] 80% ...Cleaning example_data//vu_1k_test.bib
[=================================================-----------] 80% ...Cleaning example_data//vu_1k_test.bib
[=================================================-----------] 81% ...Cleaning example_data//vu_1k_test.bib
[=================================================-----------] 82% ...Cleaning example_data//vu_1k_test.bib
[==================================================----------] 82% ...Cleaning example_data//vu_1k_test.bib
[==================================================----------] 83% ...Cleaning example_data//vu_1k_test.bib
[==================================================----------] 84% ...Cleaning example_data//vu_1k_test.bib
[===================================================---------] 84% ...Cleaning example_data//vu_1k_test.bib
[===================================================---------] 85% ...Cleaning example_data//vu_1k_test.bib
[====================================================--------] 85% ...Cleaning example_data//vu_1k_test.bib
[====================================================--------] 86% ...Cleaning example_data//vu_1k_test.bib
[====================================================--------] 87% ...Cleaning example_data//vu_1k_test.bib
[=====================================================-------] 87% ...Cleaning example_data//vu_1k_test.bib
[=====================================================-------] 88% ...Cleaning example_data//vu_1k_test.bib
[=====================================================-------] 89% ...Cleaning example_data//vu_1k_test.bib
[======================================================------] 89% ...Cleaning example_data//vu_1k_test.bib
[======================================================------] 90% ...Cleaning example_data//vu_1k_test.bib
[=======================================================-----] 90% ...Cleaning example_data//vu_1k_test.bib
[=======================================================-----] 91% ...Cleaning example_data//vu_1k_test.bib
[=======================================================-----] 92% ...Cleaning example_data//vu_1k_test.bib
[========================================================----] 92% ...Cleaning example_data//vu_1k_test.bib
[========================================================----] 93% ...Cleaning example_data//vu_1k_test.bib
[========================================================----] 94% ...Cleaning example_data//vu_1k_test.bib
[=========================================================---] 94% ...Cleaning example_data//vu_1k_test.bib
[=========================================================---] 95% ...Cleaning example_data//vu_1k_test.bib
[==========================================================--] 95% ...Cleaning example_data//vu_1k_test.bib
[==========================================================--] 96% ...Cleaning example_data//vu_1k_test.bib
[==========================================================--] 97% ...Cleaning example_data//vu_1k_test.bib
[===========================================================-] 97% ...Cleaning example_data//vu_1k_test.bib
[===========================================================-] 98% ...Cleaning example_data//vu_1k_test.bib
[===========================================================-] 99% ...Cleaning example_data//vu_1k_test.bib
[============================================================] 99% ...Cleaning example_data//vu_1k_test.bib
Cleaning of "example_data//vu_1k_test.bib" finished
Parsing of example_data//vu_1k_test_cleaned.bib started
pybtex package is parsing using bibtex.Parser()...
pybtex package finished parsing
Calculating file length...
[------------------------------------------------------------] 0% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=-----------------------------------------------------------] 1% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=-----------------------------------------------------------] 2% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==----------------------------------------------------------] 3% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===---------------------------------------------------------] 4% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===---------------------------------------------------------] 5% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====--------------------------------------------------------] 6% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====-------------------------------------------------------] 7% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====-------------------------------------------------------] 8% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======------------------------------------------------------] 9% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======-----------------------------------------------------] 11% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======-----------------------------------------------------] 12% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========----------------------------------------------------] 13% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========---------------------------------------------------] 14% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========---------------------------------------------------] 15% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========--------------------------------------------------] 16% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========-------------------------------------------------] 17% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========-------------------------------------------------] 18% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[============------------------------------------------------] 19% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=============-----------------------------------------------] 20% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=============-----------------------------------------------] 22% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==============----------------------------------------------] 23% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===============---------------------------------------------] 24% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===============---------------------------------------------] 25% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[================--------------------------------------------] 26% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[================--------------------------------------------] 27% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=================-------------------------------------------] 28% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==================------------------------------------------] 29% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==================------------------------------------------] 30% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===================-----------------------------------------] 31% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====================----------------------------------------] 33% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====================----------------------------------------] 34% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====================---------------------------------------] 35% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======================--------------------------------------] 36% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======================--------------------------------------] 37% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======================-------------------------------------] 38% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========================------------------------------------] 39% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========================------------------------------------] 40% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========================-----------------------------------] 41% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========================----------------------------------] 42% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========================----------------------------------] 44% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========================---------------------------------] 45% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[============================--------------------------------] 46% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[============================--------------------------------] 47% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=============================-------------------------------] 48% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==============================------------------------------] 49% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==============================------------------------------] 50% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===============================-----------------------------] 51% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[================================----------------------------] 52% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[================================----------------------------] 53% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=================================---------------------------] 54% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==================================--------------------------] 56% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==================================--------------------------] 57% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===================================-------------------------] 58% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====================================------------------------] 59% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====================================------------------------] 60% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====================================-----------------------] 61% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======================================----------------------] 62% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======================================----------------------] 63% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======================================---------------------] 64% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========================================--------------------] 65% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========================================--------------------] 67% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========================================-------------------] 68% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========================================------------------] 69% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========================================------------------] 70% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========================================-----------------] 71% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[============================================----------------] 72% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[============================================----------------] 73% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=============================================---------------] 74% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=============================================---------------] 75% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==============================================--------------] 76% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===============================================-------------] 78% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===============================================-------------] 79% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[================================================------------] 80% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=================================================-----------] 81% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=================================================-----------] 82% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==================================================----------] 83% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===================================================---------] 84% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===================================================---------] 85% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[====================================================--------] 86% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====================================================-------] 87% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=====================================================-------] 89% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[======================================================------] 90% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======================================================-----] 91% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=======================================================-----] 92% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[========================================================----] 93% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========================================================---] 94% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[=========================================================---] 95% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[==========================================================--] 96% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========================================================-] 97% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
[===========================================================-] 98% ...Parsing file "example_data//vu_1k_test_cleaned.bib"
<BLANKLINE>
<BLANKLINE>
---------------------------------------------------------------------------------------------------
example_data//vu_1k_test_cleaned.bib parsed and imported as Bibliography object.
<BLANKLINE>
Fields added to the parsed the Bibliography object:
{'b_abstract': 20,
'b_author_labels': 91,
'b_authors': 91,
'b_document': 91,
'b_document_label': 91,
'b_doi': 18,
'b_edition': 3,
'b_isbn': 7,
'b_issn': 20,
'b_issue_number': 5,
'b_journal': 21,
'b_journal_label': 21,
'b_note': 28,
'b_pages': 17,
'b_parent_book': 34,
'b_parent_book_label': 34,
'b_publication_month': 32,
'b_publication_year': 91,
'b_publisher': 56,
'b_publisher_label': 56,
'b_pure_bibliography_id': 91,
'b_topic_labels': 14,
'b_topics': 14,
'b_type': 91,
'b_volume': 18}
<BLANKLINE>
<BLANKLINE>
[------------------------------------------------------------] 0% ...Converting Bibliography object to Triples object.
[=-----------------------------------------------------------] 1% ...Converting Bibliography object to Triples object.
[=-----------------------------------------------------------] 2% ...Converting Bibliography object to Triples object.
[==----------------------------------------------------------] 3% ...Converting Bibliography object to Triples object.
[===---------------------------------------------------------] 4% ...Converting Bibliography object to Triples object.
[===---------------------------------------------------------] 5% ...Converting Bibliography object to Triples object.
[====--------------------------------------------------------] 6% ...Converting Bibliography object to Triples object.
[=====-------------------------------------------------------] 7% ...Converting Bibliography object to Triples object.
[=====-------------------------------------------------------] 8% ...Converting Bibliography object to Triples object.
[======------------------------------------------------------] 9% ...Converting Bibliography object to Triples object.
[=======-----------------------------------------------------] 11% ...Converting Bibliography object to Triples object.
[=======-----------------------------------------------------] 12% ...Converting Bibliography object to Triples object.
[========----------------------------------------------------] 13% ...Converting Bibliography object to Triples object.
[=========---------------------------------------------------] 14% ...Converting Bibliography object to Triples object.
[=========---------------------------------------------------] 15% ...Converting Bibliography object to Triples object.
[==========--------------------------------------------------] 16% ...Converting Bibliography object to Triples object.
[===========-------------------------------------------------] 17% ...Converting Bibliography object to Triples object.
[===========-------------------------------------------------] 18% ...Converting Bibliography object to Triples object.
[============------------------------------------------------] 19% ...Converting Bibliography object to Triples object.
[=============-----------------------------------------------] 20% ...Converting Bibliography object to Triples object.
[=============-----------------------------------------------] 22% ...Converting Bibliography object to Triples object.
[==============----------------------------------------------] 23% ...Converting Bibliography object to Triples object.
[===============---------------------------------------------] 24% ...Converting Bibliography object to Triples object.
[===============---------------------------------------------] 25% ...Converting Bibliography object to Triples object.
[================--------------------------------------------] 26% ...Converting Bibliography object to Triples object.
[================--------------------------------------------] 27% ...Converting Bibliography object to Triples object.
[=================-------------------------------------------] 28% ...Converting Bibliography object to Triples object.
[==================------------------------------------------] 29% ...Converting Bibliography object to Triples object.
[==================------------------------------------------] 30% ...Converting Bibliography object to Triples object.
[===================-----------------------------------------] 31% ...Converting Bibliography object to Triples object.
[====================----------------------------------------] 33% ...Converting Bibliography object to Triples object.
[====================----------------------------------------] 34% ...Converting Bibliography object to Triples object.
[=====================---------------------------------------] 35% ...Converting Bibliography object to Triples object.
[======================--------------------------------------] 36% ...Converting Bibliography object to Triples object.
[======================--------------------------------------] 37% ...Converting Bibliography object to Triples object.
[=======================-------------------------------------] 38% ...Converting Bibliography object to Triples object.
[========================------------------------------------] 39% ...Converting Bibliography object to Triples object.
[========================------------------------------------] 40% ...Converting Bibliography object to Triples object.
[=========================-----------------------------------] 41% ...Converting Bibliography object to Triples object.
[==========================----------------------------------] 42% ...Converting Bibliography object to Triples object.
[==========================----------------------------------] 44% ...Converting Bibliography object to Triples object.
[===========================---------------------------------] 45% ...Converting Bibliography object to Triples object.
[============================--------------------------------] 46% ...Converting Bibliography object to Triples object.
[============================--------------------------------] 47% ...Converting Bibliography object to Triples object.
[=============================-------------------------------] 48% ...Converting Bibliography object to Triples object.
[==============================------------------------------] 49% ...Converting Bibliography object to Triples object.
[==============================------------------------------] 50% ...Converting Bibliography object to Triples object.
[===============================-----------------------------] 51% ...Converting Bibliography object to Triples object.
[================================----------------------------] 52% ...Converting Bibliography object to Triples object.
[================================----------------------------] 53% ...Converting Bibliography object to Triples object.
[=================================---------------------------] 54% ...Converting Bibliography object to Triples object.
[==================================--------------------------] 56% ...Converting Bibliography object to Triples object.
[==================================--------------------------] 57% ...Converting Bibliography object to Triples object.
[===================================-------------------------] 58% ...Converting Bibliography object to Triples object.
[====================================------------------------] 59% ...Converting Bibliography object to Triples object.
[====================================------------------------] 60% ...Converting Bibliography object to Triples object.
[=====================================-----------------------] 61% ...Converting Bibliography object to Triples object.
[======================================----------------------] 62% ...Converting Bibliography object to Triples object.
[======================================----------------------] 63% ...Converting Bibliography object to Triples object.
[=======================================---------------------] 64% ...Converting Bibliography object to Triples object.
[========================================--------------------] 65% ...Converting Bibliography object to Triples object.
[========================================--------------------] 67% ...Converting Bibliography object to Triples object.
[=========================================-------------------] 68% ...Converting Bibliography object to Triples object.
[==========================================------------------] 69% ...Converting Bibliography object to Triples object.
[==========================================------------------] 70% ...Converting Bibliography object to Triples object.
[===========================================-----------------] 71% ...Converting Bibliography object to Triples object.
[============================================----------------] 72% ...Converting Bibliography object to Triples object.
[============================================----------------] 73% ...Converting Bibliography object to Triples object.
[=============================================---------------] 74% ...Converting Bibliography object to Triples object.
[=============================================---------------] 75% ...Converting Bibliography object to Triples object.
[==============================================--------------] 76% ...Converting Bibliography object to Triples object.
[===============================================-------------] 78% ...Converting Bibliography object to Triples object.
[===============================================-------------] 79% ...Converting Bibliography object to Triples object.
[================================================------------] 80% ...Converting Bibliography object to Triples object.
[=================================================-----------] 81% ...Converting Bibliography object to Triples object.
[=================================================-----------] 82% ...Converting Bibliography object to Triples object.
[==================================================----------] 83% ...Converting Bibliography object to Triples object.
[===================================================---------] 84% ...Converting Bibliography object to Triples object.
[===================================================---------] 85% ...Converting Bibliography object to Triples object.
[====================================================--------] 86% ...Converting Bibliography object to Triples object.
[=====================================================-------] 87% ...Converting Bibliography object to Triples object.
[=====================================================-------] 89% ...Converting Bibliography object to Triples object.
[======================================================------] 90% ...Converting Bibliography object to Triples object.
[=======================================================-----] 91% ...Converting Bibliography object to Triples object.
[=======================================================-----] 92% ...Converting Bibliography object to Triples object.
[========================================================----] 93% ...Converting Bibliography object to Triples object.
[=========================================================---] 94% ...Converting Bibliography object to Triples object.
[=========================================================---] 95% ...Converting Bibliography object to Triples object.
[==========================================================--] 96% ...Converting Bibliography object to Triples object.
[===========================================================-] 97% ...Converting Bibliography object to Triples object.
[===========================================================-] 98% ...Converting Bibliography object to Triples object.
Calculating the length of the Triples object
Writing of the triples to file "vu_1k_test_0.0_test.ttl" has started
[------------------------------------------------------------] 0% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=-----------------------------------------------------------] 0% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=-----------------------------------------------------------] 1% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=-----------------------------------------------------------] 2% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==----------------------------------------------------------] 2% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==----------------------------------------------------------] 3% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==----------------------------------------------------------] 4% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===---------------------------------------------------------] 4% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===---------------------------------------------------------] 5% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====--------------------------------------------------------] 5% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====--------------------------------------------------------] 6% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====--------------------------------------------------------] 7% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====-------------------------------------------------------] 7% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====-------------------------------------------------------] 8% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====-------------------------------------------------------] 9% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======------------------------------------------------------] 9% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======------------------------------------------------------] 10% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======-----------------------------------------------------] 10% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======-----------------------------------------------------] 11% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======-----------------------------------------------------] 12% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========----------------------------------------------------] 12% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========----------------------------------------------------] 13% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========----------------------------------------------------] 14% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========---------------------------------------------------] 14% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========---------------------------------------------------] 15% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========--------------------------------------------------] 15% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========--------------------------------------------------] 16% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========--------------------------------------------------] 17% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========-------------------------------------------------] 17% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========-------------------------------------------------] 18% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========-------------------------------------------------] 19% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============------------------------------------------------] 19% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============------------------------------------------------] 20% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============-----------------------------------------------] 20% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============-----------------------------------------------] 21% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============-----------------------------------------------] 22% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============----------------------------------------------] 22% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============----------------------------------------------] 23% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============----------------------------------------------] 24% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============---------------------------------------------] 24% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============---------------------------------------------] 25% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================--------------------------------------------] 25% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================--------------------------------------------] 26% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================--------------------------------------------] 27% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================-------------------------------------------] 27% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================-------------------------------------------] 28% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================-------------------------------------------] 29% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================------------------------------------------] 29% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================------------------------------------------] 30% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================-----------------------------------------] 30% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================-----------------------------------------] 31% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================-----------------------------------------] 32% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================----------------------------------------] 32% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================----------------------------------------] 33% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================----------------------------------------] 34% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================---------------------------------------] 34% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================---------------------------------------] 35% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================--------------------------------------] 35% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================--------------------------------------] 36% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================--------------------------------------] 37% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================-------------------------------------] 37% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================-------------------------------------] 38% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================-------------------------------------] 39% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================------------------------------------] 39% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================------------------------------------] 40% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================-----------------------------------] 40% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================-----------------------------------] 41% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================-----------------------------------] 42% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================----------------------------------] 42% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================----------------------------------] 43% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================----------------------------------] 44% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================---------------------------------] 44% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================---------------------------------] 45% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================--------------------------------] 45% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================--------------------------------] 46% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================--------------------------------] 47% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============================-------------------------------] 47% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============================-------------------------------] 48% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============================-------------------------------] 49% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============================------------------------------] 49% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============================------------------------------] 50% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================-----------------------------] 50% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================-----------------------------] 51% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================-----------------------------] 52% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================================----------------------------] 52% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================================----------------------------] 53% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================================----------------------------] 54% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================================---------------------------] 54% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================================---------------------------] 55% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================--------------------------] 55% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================--------------------------] 56% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================--------------------------] 57% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================================-------------------------] 57% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================================-------------------------] 58% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================================-------------------------] 59% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================================------------------------] 59% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================================------------------------] 60% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================-----------------------] 60% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================-----------------------] 61% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================-----------------------] 62% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================================----------------------] 62% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================================----------------------] 63% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================================----------------------] 64% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================================---------------------] 64% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================================---------------------] 65% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================--------------------] 65% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================--------------------] 66% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================--------------------] 67% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================================-------------------] 67% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================================-------------------] 68% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================================-------------------] 69% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================================------------------] 69% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================================------------------] 70% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================-----------------] 70% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================-----------------] 71% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================-----------------] 72% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================================----------------] 72% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================================----------------] 73% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================================----------------] 74% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============================================---------------] 74% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=============================================---------------] 75% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============================================--------------] 75% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============================================--------------] 76% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==============================================--------------] 77% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================================-------------] 77% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================================-------------] 78% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===============================================-------------] 79% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================================================------------] 79% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[================================================------------] 80% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================================================-----------] 80% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================================================-----------] 81% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=================================================-----------] 82% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================================----------] 82% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================================----------] 83% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==================================================----------] 84% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================================================---------] 84% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===================================================---------] 85% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================================================--------] 85% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================================================--------] 86% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[====================================================--------] 87% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================================-------] 87% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================================-------] 88% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=====================================================-------] 89% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================================================------] 89% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[======================================================------] 90% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================================================-----] 90% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================================================-----] 91% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=======================================================-----] 92% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================================----] 92% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================================----] 93% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[========================================================----] 94% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================================================---] 94% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[=========================================================---] 95% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================================================--] 95% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================================================--] 96% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[==========================================================--] 97% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================================-] 97% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================================-] 98% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[===========================================================-] 99% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================================================] 99% ...Writing triples to "vu_1k_test_0.0_test.ttl"
[============================================================] 100% ...Writing triples to "vu_1k_test_0.0_test.ttl"
Success: 2088 triples were written to "vu_1k_test_0.0_test.ttl"
These items were skipped due to errors (0 items):
<BLANKLINE>
A log of the operation is kept in "log.txt"
>>> from preprocessor.Text_File import Text_File
>>> my_ttl_file = Text_File('vu_1k_test_0.0_test.ttl')
>>> my_ttl_file.preview(250)
<http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isAuthorOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasAuthor> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOn> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedBy> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnYear> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnMonth> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isPublishedOnDate> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasDOI> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasISSN> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasISBN> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasPureBibliographyID> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasOpenCitationsID> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isChapterOf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/2000/01/rdf-schema#label> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasTopic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasAbstract> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasCited> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#isCitedBy> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.w3.org/2002/07/owl#equivalentClass> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#hasOriginBibliography> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://clokman.com/kfir/ontology#Topic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#vu> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#vu> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/resource#Bibliography> .
<http://clokman.com/kfir/ontology#JournalArticle> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#Book> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#BookChapter> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/ontology#Miscellaneous> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://www.w3.org/2000/01/rdf-schema#label> "Geloof en rechtvaardiging"@en .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Agteresch_HJ> .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Agteresch_HJ> <http://www.w3.org/2000/01/rdf-schema#label> "Agteresch, HJ"@en .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2023" .
<http://clokman.com/kfir/resource#a1f8850ca82a4fb89aab8db2a49f8fa1> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "a1f8850ca82a4fb89aab8db2a49f8fa1" .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://www.w3.org/2000/01/rdf-schema#label> "Gereformeerde katholiciteit in de zeventiende eeuw"@en .
<http://clokman.com/kfir/resource#Hartevelt_LDA> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Hartevelt_LDA> .
<http://clokman.com/kfir/resource#Hartevelt_LDA> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Hartevelt_LDA> <http://www.w3.org/2000/01/rdf-schema#label> "Hartevelt, LDA"@en .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2021" .
<http://clokman.com/kfir/resource#61d5cb748d514012b7ecba7bfd6dd745> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "61d5cb748d514012b7ecba7bfd6dd745" .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://www.w3.org/2000/01/rdf-schema#label> "Johann Friedrich Stapfer (1708-1775)-Theology, Orthodoxy and Polemics in the Late Orthodoxy Period"@en .
<http://clokman.com/kfir/resource#Blauw_C> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Blauw_C> .
<http://clokman.com/kfir/resource#Blauw_C> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Blauw_C> <http://www.w3.org/2000/01/rdf-schema#label> "Blauw, C"@en .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2021" .
<http://clokman.com/kfir/resource#5f2ab8884cf8455cac67c15632bbc6a0> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "5f2ab8884cf8455cac67c15632bbc6a0" .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://www.w3.org/2000/01/rdf-schema#label> "Liturgy John Owen"@en .
<http://clokman.com/kfir/resource#Hyde_D> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Hyde_D> .
<http://clokman.com/kfir/resource#Hyde_D> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Hyde_D> <http://www.w3.org/2000/01/rdf-schema#label> "Hyde, D"@en .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2021" .
<http://clokman.com/kfir/resource#351ffad9f38f44368f9808595d5537bf> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "351ffad9f38f44368f9808595d5537bf" .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://www.w3.org/2000/01/rdf-schema#label> "It Seems Good to the Holy Spirit and Us-How Reformed Churches in America May Move Past Binary and Beyond Opaque into Missional Decision Making"@en .
<http://clokman.com/kfir/resource#Wilson_M> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Wilson_M> .
<http://clokman.com/kfir/resource#Wilson_M> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Wilson_M> <http://www.w3.org/2000/01/rdf-schema#label> "Wilson, M"@en .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2020" .
<http://clokman.com/kfir/resource#f1a6e4c09d174631ba2bc5839ce037ea> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "f1a6e4c09d174631ba2bc5839ce037ea" .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://www.w3.org/2000/01/rdf-schema#label> "N.T. Wright and Missional Hermeneutics"@en .
<http://clokman.com/kfir/resource#Gonzalez_CJ> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Gonzalez_CJ> .
<http://clokman.com/kfir/resource#Gonzalez_CJ> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Gonzalez_CJ> <http://www.w3.org/2000/01/rdf-schema#label> "Gonzalez, CJ"@en .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2020" .
<http://clokman.com/kfir/resource#3bfe0162a51646b48679ebf8ecfa224a> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "3bfe0162a51646b48679ebf8ecfa224a" .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://www.w3.org/2000/01/rdf-schema#label> "The Mission of North American Church Planting in Europe-Motivations and effectiveness of North American church planting in continental Europe"@en .
<http://clokman.com/kfir/resource#Rossi_S> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Rossi_S> .
<http://clokman.com/kfir/resource#Rossi_S> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Rossi_S> <http://www.w3.org/2000/01/rdf-schema#label> "Rossi, S"@en .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2020" .
<http://clokman.com/kfir/resource#8c4e14adf9174238be2598a06a4c9525> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "8c4e14adf9174238be2598a06a4c9525" .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://www.w3.org/2000/01/rdf-schema#label> "Typologisch preken"@en .
<http://clokman.com/kfir/resource#vd_Weg_AAF> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#vd_Weg_AAF> .
<http://clokman.com/kfir/resource#vd_Weg_AAF> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#vd_Weg_AAF> <http://www.w3.org/2000/01/rdf-schema#label> "vd_Weg, AAF"@en .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2020" .
<http://clokman.com/kfir/resource#82971e5f9f2d40f0ab69296d2af28c21> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "82971e5f9f2d40f0ab69296d2af28c21" .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://www.w3.org/2000/01/rdf-schema#label> "Ministry and Mission-an investigation of the missional challenges for the Reformed Presbyterian theology of ministry in a post-Christendom age"@en .
<http://clokman.com/kfir/resource#den_Hertog_RG> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#den_Hertog_RG> .
<http://clokman.com/kfir/resource#den_Hertog_RG> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#den_Hertog_RG> <http://www.w3.org/2000/01/rdf-schema#label> "den_Hertog, RG"@en .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#f9f67ac5cf52469c92c8120aba31e1a4> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "f9f67ac5cf52469c92c8120aba31e1a4" .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://www.w3.org/2000/01/rdf-schema#label> "PhD begeleiding Sien de Groot, Universiteit Gent, promotor Marc de Groote"@en .
<http://clokman.com/kfir/resource#van_Opstall_EM> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#van_Opstall_EM> .
<http://clokman.com/kfir/resource#van_Opstall_EM> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#van_Opstall_EM> <http://www.w3.org/2000/01/rdf-schema#label> "van_Opstall, EM"@en .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#d0d9e402bf5d4c90b9f9aa7e31f236d2> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "d0d9e402bf5d4c90b9f9aa7e31f236d2" .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://www.w3.org/2000/01/rdf-schema#label> "Reasonableness and Pleasantness of christianity in Matthew Henry"@en .
<http://clokman.com/kfir/resource#Murray_DP> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Murray_DP> .
<http://clokman.com/kfir/resource#Murray_DP> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Murray_DP> <http://www.w3.org/2000/01/rdf-schema#label> "Murray, DP"@en .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#e883e940a6fc42109164e4fd60249f03> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "e883e940a6fc42109164e4fd60249f03" .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://www.w3.org/2000/01/rdf-schema#label> "Reward captures attention independent of the current focus of attention"@en .
<http://clokman.com/kfir/resource#Xue_X> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Xue_X> .
<http://clokman.com/kfir/resource#Xue_X> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Xue_X> <http://www.w3.org/2000/01/rdf-schema#label> "Xue, X"@en .
<http://clokman.com/kfir/resource#Li_S> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Li_S> .
<http://clokman.com/kfir/resource#Li_S> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Li_S> <http://www.w3.org/2000/01/rdf-schema#label> "Li, S"@en .
<http://clokman.com/kfir/resource#Theeuwes_JL> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Theeuwes_JL> .
<http://clokman.com/kfir/resource#Theeuwes_JL> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Theeuwes_JL> <http://www.w3.org/2000/01/rdf-schema#label> "Theeuwes, JL"@en .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasDOI> "10.1167/16.12.84" .
<http://clokman.com/kfir/resource#445d259ad2454906960165a6bbae883c> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "445d259ad2454906960165a6bbae883c" .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://www.w3.org/2000/01/rdf-schema#label> "Truth and Truth-Telling-Engaging South Africa's Post-Apartheid Public Religious Discourse"@en .
<http://clokman.com/kfir/resource#van_der_Riet_RL> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#van_der_Riet_RL> .
<http://clokman.com/kfir/resource#van_der_Riet_RL> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#van_der_Riet_RL> <http://www.w3.org/2000/01/rdf-schema#label> "van_der_Riet, RL"@en .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#8c769226a4fb44daa0cd2f8dc74136d3> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "8c769226a4fb44daa0cd2f8dc74136d3" .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://www.w3.org/2000/01/rdf-schema#label> "Una Sancta-Towards a Contextualised Ecclesiology for the Korean Churches"@en .
<http://clokman.com/kfir/resource#Park_S> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Park_S> .
<http://clokman.com/kfir/resource#Park_S> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Park_S> <http://www.w3.org/2000/01/rdf-schema#label> "Park, S"@en .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#f067bb9b678546da89b80ddc45b50291> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "f067bb9b678546da89b80ddc45b50291" .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Misc> .
<http://clokman.com/kfir/ontology#Misc> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://www.w3.org/2000/01/rdf-schema#label> "White anti-apartheid theologies as resource for a theological response to whiteness in post-apartheid South Africa"@en .
<http://clokman.com/kfir/resource#van_Wyngaard_GJ> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#van_Wyngaard_GJ> .
<http://clokman.com/kfir/resource#van_Wyngaard_GJ> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#van_Wyngaard_GJ> <http://www.w3.org/2000/01/rdf-schema#label> "van_Wyngaard, GJ"@en .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2019" .
<http://clokman.com/kfir/resource#cd51779243fb4c228d8313b89b8746e4> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "cd51779243fb4c228d8313b89b8746e4" .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Inbook> .
<http://clokman.com/kfir/ontology#Inbook> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://www.w3.org/2000/01/rdf-schema#label> "A Future for Cultural History of the Dutch Wadden Region-Challenges and Policies in a Maritime Agricultural Landscape"@en .
<http://clokman.com/kfir/resource#Egberts_LR> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Egberts_LR> .
<http://clokman.com/kfir/resource#Egberts_LR> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Egberts_LR> <http://www.w3.org/2000/01/rdf-schema#label> "Egberts, LR"@en .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#isPublishedBy> <http://clokman.com/kfir/resource#Amsterdam_University_Press> .
<http://clokman.com/kfir/resource#Amsterdam_University_Press> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#isPublishedOnYear> "2018" .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#isPublishedOnMonth> "10" .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#isPublishedOnDate> "2018.10" .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#hasPureBibliographyID> "1a2e1f3f51644da0929d49e8299f7532" .
<http://clokman.com/kfir/resource#1a2e1f3f51644da0929d49e8299f7532> <http://clokman.com/kfir/ontology#isChapterOf> <http://clokman.com/kfir/resource#Waddenland_Outstanding> .
<http://clokman.com/kfir/resource#Waddenland_Outstanding> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Book> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://clokman.com/kfir/ontology#Article> .
<http://clokman.com/kfir/ontology#Article> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://clokman.com/kfir/ontology#Document> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://clokman.com/kfir/ontology#hasOriginBibliography> <http://clokman.com/kfir/resource#vu> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://www.w3.org/2000/01/rdf-schema#label> "Climate, aggression, and violence (CLASH)-a cultural-evolutionary approach"@en .
<http://clokman.com/kfir/resource#Rinderu_MI> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Rinderu_MI> .
<http://clokman.com/kfir/resource#Rinderu_MI> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Rinderu_MI> <http://www.w3.org/2000/01/rdf-schema#label> "Rinderu, MI"@en .
<http://clokman.com/kfir/resource#Bushman_BJ> <http://clokman.com/kfir/ontology#isAuthorOf> <http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> .
<http://clokman.com/kfir/resource#fc77697ea6da4af396d009f8871dcea5> <http://clokman.com/kfir/ontology#hasAuthor> <http://clokman.com/kfir/resource#Bushman_BJ> .
<http://clokman.com/kfir/resource#Bushman_BJ> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#NamedIndividual> .
<http://clokman.com/kfir/resource#Bushman_BJ> <http://www.w3.org/2000/01/rdf-schema#label> "Bushman, BJ"@en .
>>> import os
>>> os.remove('vu_1k_test_0.0_test.ttl')
"""
|
nilq/baby-python
|
python
|
"""
Copyright 2020 The Johns Hopkins University Applied Physics Laboratory LLC
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.
"""
#Approved for public release, 20-563
import sys
sys.path.append("..")
from glob import glob
import os
import numpy as np
import gdal
import json
from keras import backend as K
from keras.applications import imagenet_utils
def no_nan_mse(y_true, y_pred, ignore_value=-10000):
mask_true = K.cast(K.not_equal(y_true, ignore_value), K.floatx())
masked_squared_error = K.square(mask_true * (y_true - y_pred))
masked_mse = K.sum(masked_squared_error, axis=-1) / K.maximum(K.sum(mask_true, axis=-1), 1)
return masked_mse
def get_checkpoint_dir(args):
height_str = "with_height" if args.add_height else "without_height"
aug_str = "with_aug" if args.augmentation else "without_aug"
checkpoint_sub_dir = height_str + "_" + aug_str
checkpoint_dir = os.path.join(args.checkpoint_dir, checkpoint_sub_dir)
if not os.path.isdir(checkpoint_dir):
os.makedirs(checkpoint_dir)
return checkpoint_dir,checkpoint_sub_dir
def load_vflow(vflow_path, agl):
vflow_data = json.load(open(vflow_path, "r"))
mag = agl * vflow_data["scale"]
xdir,ydir = np.sin(vflow_data["angle"]),np.cos(vflow_data["angle"])
vflow = np.zeros((agl.shape[0],agl.shape[1],2))
vflow[:,:,0] = mag * xdir
vflow[:,:,1] = mag * ydir
vflow_info = json.load(open(vflow_path, "r"))
return vflow,mag,xdir,ydir,vflow_data["angle"]
def get_data(args, is_train=True, rgb_paths_only=False):
split_dir = args.train_sub_dir if is_train else args.test_sub_dir
rgb_paths = glob(os.path.join(args.dataset_dir, split_dir, "*_RGB*.tif"))
if rgb_paths_only:
return rgb_paths
vflow_paths = [rgb_path.replace("_RGB", "_VFLOW").replace(".tif", ".json") for rgb_path in rgb_paths]
agl_paths = [rgb_path.replace("_RGB", "_AGL") for rgb_path in rgb_paths]
data = [(rgb_paths[i], vflow_paths[i], agl_paths[i]) for i in range(len(rgb_paths))]
return data
def load_image(image_path):
image = gdal.Open(image_path)
image = image.ReadAsArray()
if len(image.shape)==3:
image = np.transpose(image, [1,2,0])
return image
def save_image(image, out_path):
driver = gdal.GetDriverByName('GTiff')
if len(image.shape)==2:
out_channels = 1
else:
out_channels = image.shape[2]
dataset = driver.Create(out_path, image.shape[1], image.shape[0], out_channels, gdal.GDT_Float32)
if len(image.shape)==2:
dataset.GetRasterBand(1).WriteArray(image)
else:
for c in range(out_channels):
dataset.GetRasterBand(c+1).WriteArray(image[:,:,c])
dataset.FlushCache()
def image_preprocess(image_batch):
return imagenet_utils.preprocess_input(image_batch) / 255.0
def get_batch_inds(idx, batch_sz):
N = len(idx)
batch_inds = []
idx0 = 0
to_process = True
while to_process:
idx1 = idx0 + batch_sz
if idx1 > N:
idx1 = N
idx0 = idx1 - batch_sz
to_process = False
batch_inds.append(idx[idx0:idx1])
idx0 = idx1
return batch_inds
|
nilq/baby-python
|
python
|
# -*- coding: utf-8 -*-
r"""
Hugging Face BERT implementation.
==============
BERT model from hugging face transformers repo.
"""
import torch
from transformers import BertModel, BertForMaskedLM
from caption.models.encoders.encoder_base import Encoder
from caption.tokenizers import BERTTextEncoder
from test_tube import HyperOptArgumentParser
from torchnlp.utils import lengths_to_mask
class BERT(Encoder):
"""
BERT encoder.
:param tokenizer: BERT text encoder.
:param hparams: HyperOptArgumentParser obj.
:param lm_head: If true the language model head from the pretrain model is saved.
Check the available models here:
https://huggingface.co/transformers/pretrained_models.html
"""
def __init__(
self,
tokenizer: BERTTextEncoder,
hparams: HyperOptArgumentParser,
lm_head: bool = False,
) -> None:
super().__init__(768 if "base" in hparams.pretrained_model else 1024, tokenizer)
self._n_layers = 13 if "base" in hparams.pretrained_model else 25
self.padding_idx = self.tokenizer.padding_index
if not lm_head:
self.model = BertModel.from_pretrained(
hparams.pretrained_model, output_hidden_states=True
)
else:
mlm_model = BertForMaskedLM.from_pretrained(
hparams.pretrained_model, output_hidden_states=True
)
self.model = mlm_model.bert
self.lm_head = mlm_model.cls
@classmethod
def from_pretrained(cls, hparams: HyperOptArgumentParser, lm_head: bool = False):
""" Function that loads a pretrained BERT encoder.
:param hparams: HyperOptArgumentParser obj.
Returns:
- BERT Encoder model
"""
tokenizer = BERTTextEncoder(model=hparams.pretrained_model)
model = BERT(tokenizer=tokenizer, hparams=hparams, lm_head=lm_head)
return model
def forward(self, tokens: torch.tensor, lengths: torch.tensor, **kwargs) -> dict:
"""
Encodes a batch of sequences.
:param tokens: Torch tensor with the input sequences [batch_size x seq_len].
:param lengths: Torch tensor with the length of each sequence [seq_len].
Returns:
- 'sentemb': tensor [batch_size x 1024] with the sentence encoding.
- 'wordemb': tensor [batch_size x seq_len x 1024] with the word level embeddings.
- 'mask': torch.Tensor [seq_len x batch_size]
- 'all_layers': List with the word_embeddings returned by each layer.
- 'extra': tuple with the last_hidden_state [batch_size x seq_len x hidden_size],
the pooler_output representing the entire sentence and the word embeddings for
all BERT layers (list of tensors [batch_size x seq_len x hidden_size])
"""
mask = lengths_to_mask(lengths, device=tokens.device)
# Run BERT model.
last_hidden_states, pooler_output, all_layers = self.model(tokens, mask)
return {
"sentemb": pooler_output,
"wordemb": last_hidden_states,
"all_layers": all_layers,
"mask": mask,
"extra": (last_hidden_states, pooler_output, all_layers),
}
|
nilq/baby-python
|
python
|
from .. import db
Playlist_Songs = db.Table("play_songs", db.Column("playlist_id",
db.Integer, db.ForeignKey('playlist._Playlist__id')),
db.Column("song_id", db.Integer,
db.ForeignKey('song._Song__id'))
)
|
nilq/baby-python
|
python
|
import re
import numpy as np
def pad_sequences(sequences, maxlen=None, dtype='int32',
padding='pre', truncating='pre', value=0.):
"""Pads sequences to the same length.
# Arguments
sequences: List of lists, where each element is a sequence.
maxlen: Int, maximum length of all sequences.
dtype: Type of the output sequences.
padding: String, 'pre' or 'post':
pad either before or after each sequence.
truncating: String, 'pre' or 'post':
remove values from sequences larger than
`maxlen`, either at the beginning or at the end of the sequences.
value: Float, padding value.
# Returns
x: Numpy array with shape `(len(sequences), maxlen)`
# Raises
ValueError: In case of invalid values for `truncating` or `padding`,
or in case of invalid shape for a `sequences` entry.
"""
if not hasattr(sequences, '__len__'):
raise ValueError('`sequences` must be iterable.')
lengths = []
for x in sequences:
if not hasattr(x, '__len__'):
raise ValueError('`sequences` must be a list of iterables. '
'Found non-iterable: ' + str(x))
lengths.append(len(x))
num_samples = len(sequences)
if maxlen is None:
maxlen = np.max(lengths)
# take the sample shape from the first non empty sequence
# checking for consistency in the main loop below.
sample_shape = tuple()
for s in sequences:
if len(s) > 0:
sample_shape = np.asarray(s).shape[1:]
break
x = (np.ones((num_samples, maxlen) + sample_shape) * value).astype(dtype)
for idx, s in enumerate(sequences):
if not len(s):
continue # empty list/array was found
if truncating == 'pre':
trunc = s[-maxlen:]
elif truncating == 'post':
trunc = s[:maxlen]
else:
raise ValueError('Truncating type "%s" '
'not understood' % truncating)
# check `trunc` has expected shape
trunc = np.asarray(trunc, dtype=dtype)
if trunc.shape[1:] != sample_shape:
raise ValueError('Shape of sample %s of sequence at position %s '
'is different from expected shape %s' %
(trunc.shape[1:], idx, sample_shape))
if padding == 'post':
x[idx, :len(trunc)] = trunc
elif padding == 'pre':
x[idx, -len(trunc):] = trunc
else:
raise ValueError('Padding type "%s" not understood' % padding)
return x
class Tokenizer(object):
def __init__(self, token2idx={}, frozen=False, oov_idx=None):
"""
Converts tokens to numerical indices
Accepts:
token2idx (dict): A mapping from words/tokens to corresponding indices
frozen (bool): If set to True, new words will be converted to oov_idx
instead of being added to the vocabulary
oov_idx (int): If frozen==True, unknown words are converted to this index
Raises:
AssertionError: When frozen is set to True and oov_idx is None
"""
self.token2idx = token2idx
self.frozen = frozen
self.oov_idx = oov_idx
if self.frozen:
assert self.oov_idx is not None, "Assign a word index for out-of-vocabulary words"
self.idx = len(token2idx)
def tokenize(self, token):
"""Converts a single token to a numerical index.
Args:
token (str): A single token to be converted into a numerical index
"""
if token not in self.token2idx:
if self.frozen:
return self.oov_idx
else:
self.token2idx[token] = self.idx
self.idx += 1
return self.token2idx[token]
def split(self, sentence):
"""Method to split the sequence
Re implement this method for other tokenizers
"""
return sentence.split()
def tokenize_sentence(self, sentence, char_level=False):
"""
Splits and converts a sequence to a list
numerical indices
Accepts:
sentence: (list) Sentence to be converted
char_level: (bool) Whether to tokenize at char level
Returns:
A list of numerical indices
"""
if char_level:
return [[self.tokenize(char) for char in list(word)] for word in self.split(sentence)]
else:
return [self.tokenize(word) for word in self.split(sentence)]
def tokenize_list_of_sentences(self, sentence_list, char_level=False):
"""
Splits and converts a list of sequences to a list
numerical indices
Accepts:
sentence_list: List of sentences to be converted
char_level: (bool) Whether to tokenize at char level
Returns:
A list of list of numerical indices
"""
return [self.tokenize_sentence(sentence, char_level=char_level) for sentence in sentence_list]
class TreebankWordTokenizer(Tokenizer):
# starting quotes
STARTING_QUOTES = [
(re.compile(r'^\"'), r'``'),
(re.compile(r'(``)'), r' \1 '),
(re.compile(r"([ \(\[{<])(\"|\'{2})"), r'\1 `` '),
]
# punctuation
PUNCTUATION = [
(re.compile(r'([:,])([^\d])'), r' \1 \2'),
(re.compile(r'([:,])$'), r' \1 '),
(re.compile(r'\.\.\.'), r' ... '),
(re.compile(r'[;@#$%&]'), r' \g<0> '),
# Handles the final period.
(re.compile(r'([^\.])(\.)([\]\)}>"\']*)\s*$'), r'\1 \2\3 '),
(re.compile(r'[?!]'), r' \g<0> '),
(re.compile(r"([^'])' "), r"\1 ' "),
]
# Pads parentheses
PARENS_BRACKETS = (re.compile(r'[\]\[\(\)\{\}\<\>]'), r' \g<0> ')
# Optionally: Convert parentheses, brackets and converts them to PTB symbols.
CONVERT_PARENTHESES = [
(re.compile(r'\('), '-LRB-'), (re.compile(r'\)'), '-RRB-'),
(re.compile(r'\['), '-LSB-'), (re.compile(r'\]'), '-RSB-'),
(re.compile(r'\{'), '-LCB-'), (re.compile(r'\}'), '-RCB-')
]
DOUBLE_DASHES = (re.compile(r'--'), r' -- ')
# ending quotes
ENDING_QUOTES = [
(re.compile(r'"'), " '' "),
(re.compile(r'(\S)(\'\')'), r'\1 \2 '),
(re.compile(r"([^' ])('[sS]|'[mM]|'[dD]|') "), r"\1 \2 "),
(re.compile(r"([^' ])('ll|'LL|'re|'RE|'ve|'VE|n't|N'T) "), r"\1 \2 "),
]
# List of contractions adapted from Robert MacIntyre's tokenizer.
CONTRACTIONS2 = [r"(?i)\b(can)(?#X)(not)\b",
r"(?i)\b(d)(?#X)('ye)\b",
r"(?i)\b(gim)(?#X)(me)\b",
r"(?i)\b(gon)(?#X)(na)\b",
r"(?i)\b(got)(?#X)(ta)\b",
r"(?i)\b(lem)(?#X)(me)\b",
r"(?i)\b(mor)(?#X)('n)\b",
r"(?i)\b(wan)(?#X)(na)\s"]
CONTRACTIONS3 = [r"(?i) ('t)(?#X)(is)\b", r"(?i) ('t)(?#X)(was)\b"]
CONTRACTIONS2 = list(map(re.compile, CONTRACTIONS2))
CONTRACTIONS3 = list(map(re.compile, CONTRACTIONS3))
def split(self, text, convert_parentheses=False):
for regexp, substitution in self.STARTING_QUOTES:
text = regexp.sub(substitution, text)
for regexp, substitution in self.PUNCTUATION:
text = regexp.sub(substitution, text)
# Handles parentheses.
regexp, substitution = self.PARENS_BRACKETS
text = regexp.sub(substitution, text)
# Optionally convert parentheses
if convert_parentheses:
for regexp, substitution in self.CONVERT_PARENTHESES:
text = regexp.sub(substitution, text)
# Handles double dash.
regexp, substitution = self.DOUBLE_DASHES
text = regexp.sub(substitution, text)
# add extra space to make things easier
text = " " + text + " "
for regexp, substitution in self.ENDING_QUOTES:
text = regexp.sub(substitution, text)
for regexp in self.CONTRACTIONS2:
text = regexp.sub(r' \1 \2 ', text)
for regexp in self.CONTRACTIONS3:
text = regexp.sub(r' \1 \2 ', text)
return text.split()
|
nilq/baby-python
|
python
|
#!/usr/bin/env python
import argparse
import glob
import re
import os
import textract
import pyexcel as pe
import email.utils
import olefile as OleFile
from email.parser import Parser as EmailParser
EMAIL_REGEX = re.compile(r"(?i)([a-z0-9._-]{1,}@[a-z0-9-]{1,}\.[a-z]{2,})")
FLAT_FORMATS = ['txt', 'out', 'log', 'csv', 'ini']
BAD_FILES = ['exe', 'py', 'pyc', 'pyd', 'dll', 'js' 'css', 'ico']
def main():
parser = argparse.ArgumentParser(description='Search a directory containing documents for emails addresses')
parser.add_argument('directory', help='Directory containing documents')
parser.add_argument('-o', '--outfile', help='File to write found emails address to', default='emails_out.txt')
args = parser.parse_args()
found_emails = []
unprocessed_files = []
directories = get_files(args.directory)
for doc in directories:
try:
extension = (doc.split('.')[-1]).lower()
# Skip bad files
if extension in BAD_FILES:
continue
# Process xlsm documents
elif extension == 'xlsm':
emails = search_xlsm(doc)
# Process msg files
elif extension == 'msg':
emails = search_msg(doc)
# Process text documents
elif extension in FLAT_FORMATS:
emails = search_text(doc)
# Process all other documents
else:
emails = search_docs(doc)
# Unique emails
if len(emails) > 0:
print("{0} -> {1}".format(emails, doc))
for email in emails:
email = email.lower()
if email in found_emails:
continue
else:
found_emails.append(email)
except Exception as error:
print("[-] Unable to process: {0}".format(doc))
unprocessed_files.append(doc)
continue
# Write emails to file
if len(found_emails) > 0:
display_emails(found_emails, args.outfile, unprocessed_files)
else:
print("[-] No emails found in '{0}'".format(args.directory))
def get_files(directory):
directories = []
for root, dirs, filenames in os.walk(directory):
for filename in filenames:
directories.append(os.path.join(root, filename))
return directories
def search_msg(doc):
emails = []
outfile = "/tmp/{0}.txt".format(doc.split('/')[-1].split('.')[0])
msg = Message(doc)
msg.save(outfile)
emails = search_text(outfile)
return emails
def search_text(doc):
emails = []
text = open(doc, 'rb')
for line in text:
email = EMAIL_REGEX.search(line)
if email:
emails.append(email.group(0))
return emails
def search_xlsm(doc):
emails = []
doc_name = doc.split('/')[-1]
new_doc = "{0}.xls".format(doc_name.split('.')[0])
sheet = pe.get_book(file_name=doc)
sheet.save_as("/tmp/{0}".format(new_doc))
emails = search_docs("/tmp/{0}".format(new_doc))
return emails
def search_docs(doc):
emails = []
text = textract.process(doc)
emails = EMAIL_REGEX.findall(text)
return emails
def display_emails(emails, outfile, unprocessed):
f = open(outfile, 'a')
for email in emails:
f.write("{0}\n".format(email))
print(email)
f = open(outfile + '.unprocessed', 'a')
for u in unprocessed:
f.write("{0}\n".format(u))
class Message(OleFile.OleFileIO):
def __init__(self, filename):
OleFile.OleFileIO.__init__(self, filename)
def _getStream(self, filename):
if self.exists(filename):
stream = self.openstream(filename)
return stream.read()
else:
return None
def _getStringStream(self, filename, prefer='unicode'):
if isinstance(filename, list):
filename = "/".join(filename)
asciiVersion = self._getStream(filename + '001E')
unicodeVersion = windowsUnicode(self._getStream(filename + '001F'))
if asciiVersion is None:
return unicodeVersion
elif unicodeVersion is None:
return asciiVersion
else:
if prefer == 'unicode':
return unicodeVersion
else:
return asciiVersion
@property
def subject(self):
return self._getStringStream('__substg1.0_0037')
@property
def header(self):
try:
return self._header
except Exception:
headerText = self._getStringStream('__substg1.0_007D')
if headerText is not None:
self._header = EmailParser().parsestr(headerText)
else:
self._header = None
return self._header
@property
def sender(self):
try:
return self._sender
except Exception:
if self.header is not None:
headerResult = self.header["from"]
if headerResult is not None:
self._sender = headerResult
return headerResult
text = self._getStringStream('__substg1.0_0C1A')
email = self._getStringStream('__substg1.0_0C1F')
result = None
if text is None:
result = email
else:
result = text
if email is not None:
result = result + " <" + email + ">"
self._sender = result
return result
@property
def to(self):
try:
return self._to
except Exception:
if self.header is not None:
headerResult = self.header["to"]
if headerResult is not None:
self._to = headerResult
return headerResult
display = self._getStringStream('__substg1.0_0E04')
self._to = display
return display
@property
def cc(self):
try:
return self._cc
except Exception:
if self.header is not None:
headerResult = self.header["cc"]
if headerResult is not None:
self._cc = headerResult
return headerResult
display = self._getStringStream('__substg1.0_0E03')
self._cc = display
return display
@property
def body(self):
# Get the message body
return self._getStringStream('__substg1.0_1000')
def save(self, outfile):
def xstr(s):
return '' if s is None else str(s)
# Save the message body
f = open("{0}".format(outfile), "w")
f.write("From: " + xstr(self.sender) + "\n")
f.write("To: " + xstr(self.to) + "\n")
f.write("CC: " + xstr(self.cc) + "\n")
f.write("Subject: " + xstr(self.subject) + "\n")
f.write("-----------------\n\n")
f.write((self.body).encode('utf-8'))
f.close()
def windowsUnicode(string):
if string is None:
return None
return unicode(string, 'utf_16_le')
if __name__ == '__main__':
main()
|
nilq/baby-python
|
python
|
# Copyright 2019-2021 ETH Zurich and the DaCe authors. All rights reserved.
from typing import Sequence, Union, Callable, Any, Set
import warnings
import dace
from dace import Config
import dace.serialize
import dace.library
from dace.sdfg import SDFG, SDFGState
from dace.sdfg import graph, nodes
from dace.properties import Property, LambdaProperty, SymbolicProperty
from dace.frontend.operations import detect_reduction_type
from dace.memlet import Memlet
from dace.transformation.transformation import ExpandTransformation
from dace.frontend.common import op_repository as oprepo
from dace import dtypes, symbolic
from dace.libraries.nccl import environments, utils as nutil
from dace.frontend.python.replacements import _define_local_scalar
@dace.library.expansion
class ExpandReduceNCCL(ExpandTransformation):
environments = [environments.nccl.NCCL]
@staticmethod
def expansion(node: 'Reduce', state: SDFGState, sdfg: SDFG, **kwargs):
node.validate(sdfg, state)
for edge in state.in_edges(node):
if edge.dst_conn == '_inbuffer':
input_edge = edge
for edge in state.out_edges(node):
if edge.src_conn == '_outbuffer':
output_edge = edge
input_dims = input_edge.data.subset.size_exact()
output_dims = output_edge.data.subset.size_exact()
input_data = sdfg.arrays[input_edge.data.data]
output_data = sdfg.arrays[output_edge.data.data]
# Verify that data is on the GPU
if input_data.storage is not dtypes.StorageType.GPU_Global:
raise ValueError('Input of NCCL Send must reside '
' in global GPU memory.')
if output_data.storage is not dtypes.StorageType.GPU_Global:
raise ValueError('Output of NCCL Recv must reside '
' in global GPU memory.')
root = node.root
rootstr = str(root)
for fs in root.free_symbols:
if fs.name in sdfg.arrays:
sdfg.arrays[fs.name].lifetime = dtypes.AllocationLifetime.SDFG
if fs.name in sdfg.parent_sdfg.arrays:
sdfg.parent_sdfg.arrays[
fs.name].lifetime = dtypes.AllocationLifetime.SDFG
redtype = node.reduction_type
redtype = nutil.NCCL_SUPPORTED_OPERATIONS[redtype]
wcr_str = str(redtype)
wcr_str = wcr_str[wcr_str.find('.') + 1:] # Skip "NcclReductionType."
nccl_dtype_str = nutil.Nccl_dtypes(input_data.dtype.base_type)
count_str = "*".join(str(e) for e in input_dims)
if input_data.dtype.veclen > 1:
raise (NotImplementedError)
code = f"""ncclReduce(_inbuffer, _outbuffer, {count_str}, {nccl_dtype_str}, {wcr_str}, {rootstr}, __state->ncclCommunicators->at(__dace_cuda_device), __dace_current_stream)"""
if Config.get('compiler', 'build_type') == 'Debug':
code = '''DACE_NCCL_CHECK(''' + code + ''');\n'''
else:
code = code + ''';\n'''
if Config.get_bool('debugprint'):
code = (
f'''printf("{str(node)}: begin; dev,peer: %d, %d\\n", __dace_cuda_device, {rootstr});\n'''
+ code +
f'''printf("{str(node)}: end; dev,peer: %d, %d\\n\\n", __dace_cuda_device, {rootstr});\n'''
)
code += """\ncudaStreamSynchronize(__dace_current_stream);"""
tasklet = nodes.Tasklet(node.name + "_" + wcr_str,
node.in_connectors,
node.out_connectors,
code,
location=node.location,
language=dtypes.Language.CPP,
library_expansion_symbols=set(
map(str, root.free_symbols)))
return tasklet
@dace.library.node
class Reduce(nodes.LibraryNode):
# Global properties
implementations = {
"NCCL": ExpandReduceNCCL,
}
default_implementation = "NCCL"
# Object fields
wcr = LambdaProperty(default='lambda a, b: a + b')
root = SymbolicProperty(default=0,
allow_none=True,
desc="The gpu on which the receive buffer resides")
def __init__(self,
wcr="lambda a, b: a + b",
root: symbolic.SymbolicType = 0,
debuginfo=None,
*args,
**kwargs):
super().__init__(name='nccl_Reduce', *args, **kwargs)
self.wcr = wcr
self.root = root
self.schedule = dtypes.ScheduleType.GPU_Multidevice
self.debuginfo = debuginfo
@staticmethod
def from_json(json_obj, context=None):
ret = Reduce("lambda a, b: a + b", None)
dace.serialize.set_properties_from_json(ret, json_obj, context=context)
return ret
def __str__(self):
redtype = self.reduction_type
wcr_str = str(redtype)
wcr_str = wcr_str[wcr_str.find('.') + 1:] # Skip "ReductionType."
return f'nccl_Reduce({wcr_str})'
@property
def reduction_type(self):
# Autodetect reduction type
redtype = detect_reduction_type(self.wcr)
if redtype not in nutil.NCCL_SUPPORTED_OPERATIONS:
raise ValueError(
'NCCL only supports sum, product, min and max operations.')
return redtype
def validate(self, sdfg: SDFG, state: SDFGState):
redtype = self.reduction_type
in_edges = state.in_edges(self)
if len(in_edges) not in [1, 2]:
raise ValueError("NCCL Reduce must have one or two inputs.")
out_edges = state.out_edges(self)
if len(out_edges) not in [1, 2]:
raise ValueError("NCCL Reduce must have one or two outputs.")
@property
def free_symbols(self) -> Set[str]:
result = super().free_symbols
result.update(map(str, self.root.free_symbols))
return result
@oprepo.replaces('dace.comm.nccl.reduce')
@oprepo.replaces('dace.comm.nccl.Reduce')
def nccl_reduce(pv: 'ProgramVisitor',
sdfg: SDFG,
state: SDFGState,
redfunction: Callable[[Any, Any], Any],
in_buffer: str,
out_buffer: Union[str, None] = None,
root: str = None,
group_handle: str = None):
inputs = {"_inbuffer"}
outputs = {"_outbuffer"}
if isinstance(group_handle, str):
gh_start = False
if group_handle in sdfg.arrays.keys():
gh_name = group_handle
gh_out = state.add_access(gh_name)
gh_in = state.add_access(gh_name)
inputs.add("_group_handle")
else:
gh_start = True
gh_name = _define_local_scalar(pv, sdfg, state, dace.int32,
dtypes.StorageType.GPU_Global)
gh_out = state.add_access(gh_name)
outputs.add("_group_handle")
libnode = Reduce(inputs=inputs, outputs=outputs, wcr=redfunction, root=root)
if isinstance(group_handle, str):
gh_memlet = Memlet.simple(gh_name, '0')
if not gh_start:
state.add_edge(gh_in, None, libnode, "_group_handle", gh_memlet)
state.add_edge(libnode, "_group_handle", gh_out, None, gh_memlet)
# If out_buffer is not specified, the operation will be in-place.
if out_buffer is None:
out_buffer = in_buffer
# Add nodes
in_node = state.add_read(in_buffer)
out_node = state.add_write(out_buffer)
# Connect nodes
state.add_edge(in_node, None, libnode, '_inbuffer', Memlet(in_buffer))
state.add_edge(libnode, '_outbuffer', out_node, None, Memlet(out_buffer))
return []
|
nilq/baby-python
|
python
|
#
# 2018-01-15 by Toomas Mölder
# Some temporary logging (activity includes _tmp_ added for all steps to better understand, what steps take how long and what indexes to create
# TODO: add exception handling
#
from AnalyzerDatabaseManager import AnalyzerDatabaseManager
from models.AveragesByTimeperiodModel import AveragesByTimeperiodModel
import analyzer_conf
import settings
from logger_manager import LoggerManager
import os
import time
import datetime
from dateutil.relativedelta import relativedelta
import numpy as np
import pandas as pd
db_manager = AnalyzerDatabaseManager(settings, analyzer_conf)
logger_m = LoggerManager(settings.LOGGER_NAME, 'analyzer')
logger_m.log_info('_tmp_train_or_update_historic_averages_models_start',
"Process started ...")
# add first request timestamps for service calls that have appeared
logger_m.log_info('_tmp_train_or_update_historic_averages_models_1',
"Checking if completely new service calls have appeared ...")
logger_m.log_heartbeat("Checking if completely new service calls have appeared", settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
db_manager.add_first_request_timestamps_from_clean_data()
logger_m.log_info('_tmp_train_or_update_historic_averages_models_1',
"Checking if completely new service calls have appeared ... Done!")
#
logger_m.log_info('_tmp_train_or_update_historic_averages_models_2',
"Metric names ...")
metric_names = list(analyzer_conf.historic_averages_thresholds.keys())
logger_m.log_info('_tmp_train_or_update_historic_averages_models_2',
"Metric names ... Done!")
current_time = datetime.datetime.now()
max_incident_creation_timestamp = (current_time - datetime.timedelta(minutes=analyzer_conf.incident_expiration_time))
first_model_train_timestamp = (current_time - relativedelta(months=analyzer_conf.training_period_time))
max_request_timestamp = (current_time - datetime.timedelta(minutes=analyzer_conf.corrector_buffer_time))
# retrieve service calls according to stages
logger_m.log_info('_tmp_train_or_update_historic_averages_models_3',
"Determining service call stages ...")
logger_m.log_heartbeat("Determining service call stages", settings.HEARTBEAT_PATH, settings.HEARTBEAT_FILE)
sc_regular, sc_first_model, sc_second_model = db_manager.get_service_calls_for_train_stages(
time_first_model=first_model_train_timestamp,
time_second_model=max_incident_creation_timestamp)
logger_m.log_info('train_or_update_historic_averages_models',
"Number of service calls that have passed the training period (model will be trained for the first time): %s" % len(sc_first_model))
logger_m.log_info('train_or_update_historic_averages_models',
"Number of service calls that have passed the retraining period (model will be retrained for the first time): %s" % len(sc_second_model))
logger_m.log_info('train_or_update_historic_averages_models',
"Number of service calls that will be updated in regular mode: %s" % len(sc_regular))
logger_m.log_info('_tmp_train_or_update_historic_averages_models_3',
"Determining service call stages ... Done!")
# 4.3.5 - 4.3.9 Comparison with historic averages for:
# request count, response size, request size, response duration, request duration
for time_window, train_mode in analyzer_conf.historic_averages_time_windows:
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4',
"Comparison with historic averages (timeunit %s, mode %s) ..." % (str(time_window['timeunit_name']), train_mode))
last_fit_timestamp = db_manager.get_timestamp(ts_type="last_fit_timestamp", model_type=time_window['timeunit_name'])
last_fit_timestamp = last_fit_timestamp if train_mode != "retrain" else None
start = time.time()
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_1',
"Retrieving data according to service call stages (timeunit %s, mode %s) ..." % (str(time_window['timeunit_name']), train_mode))
logger_m.log_heartbeat("Retrieving data according to service call stages (%s model)" % time_window['timeunit_name'],
settings.HEARTBEAT_PATH, settings.HEARTBEAT_FILE)
data_regular, data_first_train, data_first_retrain = db_manager.get_data_for_train_stages(
sc_regular=sc_regular,
sc_first_model=sc_first_model,
sc_second_model=sc_second_model,
relevant_anomalous_metrics=metric_names,
max_incident_creation_timestamp=max_incident_creation_timestamp,
last_fit_timestamp=last_fit_timestamp,
agg_minutes=time_window["agg_window"]["agg_minutes"],
max_request_timestamp=max_request_timestamp)
data = pd.concat([data_regular, data_first_train, data_first_retrain])
logger_m.log_info('train_or_update_historic_averages_models', "Data (regular training) shape is: %s" % str(data_regular.shape))
logger_m.log_info('train_or_update_historic_averages_models', "Data (first-time training) shape is: %s" % str(data_first_train.shape))
logger_m.log_info('train_or_update_historic_averages_models', "Data (retraining) shape is: %s" % str(data_first_retrain.shape))
logger_m.log_info('train_or_update_historic_averages_models', "Aggregating the data took: %s%s" % (str(np.round(time.time() - start, 2)), " seconds."))
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_1',
"Retrieving data according to service call stages (timeunit %s, mode %s) ... Done!" % (time_window['timeunit_name'], train_mode))
if train_mode == "retrain" or last_fit_timestamp is None:
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Training the model %s, mode %s ..." % (time_window['timeunit_name'], train_mode))
logger_m.log_heartbeat("Training the %s model" % time_window['timeunit_name'], settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
if max_request_timestamp is not None:
logger_m.log_info('train_or_update_historic_averages_models', "Using data until %s." % (max_request_timestamp))
else:
logger_m.log_info('train_or_update_historic_averages_models', "Using all data.")
# Fit the model
start = time.time()
averages_by_time_period_model = AveragesByTimeperiodModel(time_window, analyzer_conf)
averages_by_time_period_model.fit(data)
logger_m.log_info('train_or_update_historic_averages_models',
"Averages by timeperiod model (%s) fitting time: %s%s" % (time_window['timeunit_name'],
np.round(time.time() - start, 2), " seconds."))
# Save the model
logger_m.log_heartbeat("Saving the %s model" % time_window['timeunit_name'], settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
db_manager.save_model(averages_by_time_period_model.dt_avgs.reset_index())
elif train_mode == "update":
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Updating the model %s, mode %s ..." % (time_window['timeunit_name'], train_mode))
logger_m.log_heartbeat("Updating the %s model" % time_window['timeunit_name'], settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
if max_request_timestamp is not None:
logger_m.log_info('train_or_update_historic_averages_models', "Using data between %s and %s." % (last_fit_timestamp, max_request_timestamp))
else:
logger_m.log_info('train_or_update_historic_averages_models', "Using data from %s until today." % last_fit_timestamp)
# Load the model
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Loading the existing %s model ..." % time_window['timeunit_name'])
logger_m.log_heartbeat("Loading the existing %s model" % time_window['timeunit_name'], settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
dt_model = db_manager.load_model(model_name=time_window['timeunit_name'], version=None)
model_version = dt_model.version.iloc[0]
model_creation_timestamp = dt_model.model_creation_timestamp.iloc[0]
# Discard from the model service calls that will be (re)trained
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Discard from the model service calls that will be (re)trained ...")
# dt_model = dt_model.merge(data_regular[analyzer_conf.service_call_fields])
dt_model.index = dt_model[analyzer_conf.service_call_fields]
if len(data_first_train) > 0:
data_first_train.index = data_first_train[analyzer_conf.service_call_fields]
dt_model = dt_model[~dt_model.index.isin(data_first_train.index)]
if len(data_first_retrain) > 0:
data_first_retrain.index = data_first_retrain[analyzer_conf.service_call_fields]
dt_model = dt_model[~dt_model.index.isin(data_first_retrain.index)]
# Generate the correct index for the model
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Generate the correct index for the model ...")
dt_model = dt_model.groupby(analyzer_conf.service_call_fields + ["similar_periods"]).first()
averages_by_time_period_model = AveragesByTimeperiodModel(time_window, analyzer_conf, dt_model,
version=model_version,
model_creation_timestamp=model_creation_timestamp)
# Update the model using new data
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Update the model using new data ...")
start = time.time()
averages_by_time_period_model.update_model(data)
logger_m.log_info('train_or_update_historic_averages_models',
"Updating the %s model took: %s%s" % (time_window['timeunit_name'], str(np.round(time.time() - start, 2)), " seconds."))
# Save the updated model
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Save the updated model ...")
logger_m.log_heartbeat("Saving the %s model" % time_window['timeunit_name'], settings.HEARTBEAT_PATH,
settings.HEARTBEAT_FILE, 'SUCCEEDED')
db_manager.save_model(averages_by_time_period_model.dt_avgs.reset_index())
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4_2',
"Save the updated model ... Done!")
else:
logger_m.log_error('train_or_update_historic_averages_models', "Unknown training mode.")
if len(data) > 0:
max_request_timestamp = data[analyzer_conf.timestamp_field].max()
logger_m.log_info('train_or_update_historic_averages_models',
"Maximum aggregated request timestamp used: %s" % max_request_timestamp)
logger_m.log_heartbeat("Updating last train timestamp (model %s)" % time_window['timeunit_name'],
settings.HEARTBEAT_PATH, settings.HEARTBEAT_FILE, 'SUCCEEDED')
db_manager.set_timestamp(ts_type="last_fit_timestamp", model_type=time_window['timeunit_name'],
value=max_request_timestamp)
logger_m.log_info('_tmp_train_or_update_historic_averages_models_4',
"Comparison with historic averages (timeunit %s, mode %s) ... Done!" % (str(time_window['timeunit_name']), train_mode))
# Update "first" timestamps for service calls that were trained or retrained
logger_m.log_info('_tmp_train_or_update_historic_averages_models_5',
"Updating timestamps ... Done!")
logger_m.log_heartbeat("Updating timestamps", settings.HEARTBEAT_PATH, settings.HEARTBEAT_FILE, 'SUCCEEDED')
db_manager.update_first_train_retrain_timestamps(sc_first_model, sc_second_model, current_time)
logger_m.log_info('_tmp_train_or_update_historic_averages_models_end',
"Process finished ... Done!")
logger_m.log_heartbeat("Finished training", settings.HEARTBEAT_PATH, settings.HEARTBEAT_FILE, 'SUCCEEDED')
|
nilq/baby-python
|
python
|
import os
import textwrap
from typing import List, Optional
import colorama # type: ignore
from spectacles.logger import GLOBAL_LOGGER as logger, log_sql_error, COLORS
LINE_WIDTH = 80
COLOR_CODE_LENGTH = len(colorama.Fore.RED) + len(colorama.Style.RESET_ALL)
def color(text: str, name: str) -> str:
if os.environ.get("NO_COLOR") or os.environ.get("TERM") == "dumb":
return str(text)
else:
return f"{COLORS[name]}{text}{COLORS['reset']}"
def bold(text: str) -> str:
return color(text, "bold")
def dim(text: str) -> str:
return color(text, "dim")
def red(text: str) -> str:
return color(text, "red")
def green(text: str) -> str:
return color(text, "green")
def yellow(text: str) -> str:
return color(text, "yellow")
def print_header(
text: str,
line_width: int = LINE_WIDTH,
char: str = "=",
leading_newline: bool = True,
) -> None:
header = f" {text} ".center(line_width, char)
if leading_newline:
header = "\n" + header
logger.info(f"{header}\n")
def print_content_error(
model: str,
explore: str,
message: str,
content_type: str,
tile_type: Optional[str],
tile_title: Optional[str],
space: str,
title: str,
url: str,
):
path = f"{title} [{space}]"
print_header(red(path), LINE_WIDTH + COLOR_CODE_LENGTH)
if content_type == "dashboard":
if tile_type == "dashboard_filter":
tile_type = "Filter"
else:
tile_type = "Tile"
line = f"{tile_type} '{tile_title}' failed validation."
wrapped = textwrap.fill(line, LINE_WIDTH)
logger.info(wrapped + "\n")
line = f"Error in {model}/{explore}: {message}"
wrapped = textwrap.fill(line, LINE_WIDTH)
logger.info(wrapped)
content_type = content_type.title()
logger.info("\n" + f"{content_type.title()}: {url}")
def print_data_test_error(
model: str, explore: str, test_name: str, message: str, lookml_url: str
) -> None:
path = f"{model}/{explore}/{test_name}"
print_header(red(path), LINE_WIDTH + COLOR_CODE_LENGTH)
wrapped = textwrap.fill(message, LINE_WIDTH)
logger.info(wrapped)
logger.info("\n" + f"LookML: {lookml_url}")
def print_lookml_error(
file_path: str, line_number: int, severity: str, message: str, lookml_url: str
) -> None:
if file_path is None:
file_path = "[File name not given by Looker]"
header_color = red if severity in ("fatal", "error") else yellow
print_header(
header_color(f"{file_path}:{line_number}"), LINE_WIDTH + COLOR_CODE_LENGTH
)
wrapped = textwrap.fill(f"[{severity.title()}] {message}", LINE_WIDTH)
logger.info(wrapped)
if lookml_url:
logger.info("\n" + f"LookML: {lookml_url}")
def print_lookml_success() -> None:
logger.info(green("✓ No LookML errors found."))
def print_sql_error(
model: str,
explore: str,
message: str,
sql: str,
log_dir: str,
dimension: Optional[str] = None,
lookml_url: Optional[str] = None,
) -> None:
path = model + "/"
if dimension:
path += dimension
else:
path += explore
print_header(red(path), LINE_WIDTH + COLOR_CODE_LENGTH)
wrapped = textwrap.fill(message, LINE_WIDTH)
logger.info(wrapped)
if lookml_url:
logger.info("\n" + f"LookML: {lookml_url}")
file_path = log_sql_error(model, explore, sql, log_dir, dimension)
logger.info("\n" + f"Test SQL: {file_path}")
def print_validation_result(status: str, source: str):
bullet = "✗" if status == "failed" else "✓"
if status == "passed":
message = green(source)
elif status == "failed":
message = red(source)
elif status == "skipped":
message = dim(source)
logger.info(f"{bullet} {message} {status}")
def mark_line(lines: List[str], line_number: int, char: str = "*") -> List[str]:
"""For a list of strings, mark a specified line with a prepended character."""
line_number -= 1 # Align with array indexing
marked = []
for i, line in enumerate(lines):
if i == line_number:
marked.append(char + " " + line)
else:
marked.append(dim("| " + line))
return marked
def extract_sql_context(sql: str, line_number: int, window_size: int = 2) -> str:
"""Extract a line of SQL with a specified amount of surrounding context."""
split = sql.split("\n")
line_number -= 1 # Align with array indexing
line_start = line_number - window_size
line_end = line_number + (window_size + 1)
line_start = line_start if line_start >= 0 else 0
line_end = line_end if line_end <= len(split) else len(split)
selected_lines = split[line_start:line_end]
marked = mark_line(selected_lines, line_number=line_number - line_start + 1)
context = "\n".join(marked)
return context
|
nilq/baby-python
|
python
|
# ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2015 - 2017 Pixar
#
# 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.
#
#
# ##### END MIT LICENSE BLOCK #####
import bpy
import _cycles
from bpy.app.handlers import persistent
import xml.etree.ElementTree as ET
import tempfile
import nodeitems_utils
import shutil
from bpy.props import *
from nodeitems_utils import NodeCategory, NodeItem
from .shader_parameters import class_generate_properties
from .shader_parameters import node_add_inputs
from .shader_parameters import node_add_outputs
from .shader_parameters import socket_map
from .shader_parameters import txmake_options, update_conditional_visops
from .util import args_files_in_path
from .util import get_path_list
from .util import rib
from .util import debug
from .util import user_path
from .util import get_real_path
from .util import readOSO
from .cycles_convert import *
from operator import attrgetter, itemgetter
import os.path
from time import sleep
import traceback
NODE_LAYOUT_SPLIT = 0.5
group_nodes = ['ShaderNodeGroup', 'NodeGroupInput', 'NodeGroupOutput']
# Default Types
# update node during ipr for a socket default_value
def update_func(self, context):
# check if this prop is set on an input
node = self.node if hasattr(self, 'node') else self
from . import engine
if engine.is_ipr_running():
engine.ipr.issue_shader_edits(node=node)
# socket name corresponds to the param on the node
class RendermanSocket:
ui_open: BoolProperty(name='UI Open', default=True)
def get_pretty_name(self, node):
if node.bl_idname in group_nodes:
return self.name
else:
return self.identifier
def get_value(self, node):
if node.bl_idname in group_nodes or not hasattr(node, self.name):
return self.default_value
else:
return getattr(node, self.name)
def draw_color(self, context, node):
return (0.25, 1.0, 0.25, 1.0)
def draw_value(self, context, layout, node):
layout.prop(node, self.identifier)
def draw(self, context, layout, node, text):
if self.is_linked or self.is_output or self.hide_value or not hasattr(self, 'default_value'):
layout.label(self.get_pretty_name(node))
elif node.bl_idname in group_nodes or node.bl_idname == "PxrOSLPatternNode":
layout.prop(self, 'default_value',
text=self.get_pretty_name(node), slider=True)
else:
layout.prop(node, self.name,
text=self.get_pretty_name(node), slider=True)
class RendermanSocketInterface:
def draw_color(self, context):
return (0.25, 1.0, 0.25, 1.0)
def draw(self, context, layout):
layout.label(self.name)
def from_socket(self, node, socket):
if hasattr(self, 'default_value'):
self.default_value = socket.get_value(node)
self.name = socket.name
def init_socket(self, node, socket, data_path):
sleep(.01)
socket.name = self.name
if hasattr(self, 'default_value'):
socket.default_value = self.default_value
# socket types (need this just for the ui_open)
class RendermanNodeSocketFloat(bpy.types.NodeSocketFloat, RendermanSocket):
'''RenderMan float input/output'''
bl_idname = 'RendermanNodeSocketFloat'
bl_label = 'RenderMan Float Socket'
default_value: FloatProperty(update=update_func)
renderman_type: StringProperty(default='float')
def draw_color(self, context, node):
return (0.5, 0.5, 0.5, 1.0)
class RendermanNodeSocketInterfaceFloat(bpy.types.NodeSocketInterfaceFloat, RendermanSocketInterface):
'''RenderMan float input/output'''
bl_idname = 'RendermanNodeSocketInterfaceFloat'
bl_label = 'RenderMan Float Socket'
bl_socket_idname = 'RendermanNodeSocketFloat'
default_value: FloatProperty()
def draw_color(self, context):
return (0.5, 0.5, 0.5, 1.0)
class RendermanNodeSocketInt(bpy.types.NodeSocketInt, RendermanSocket):
'''RenderMan int input/output'''
bl_idname = 'RendermanNodeSocketInt'
bl_label = 'RenderMan Int Socket'
default_value: IntProperty(update=update_func)
renderman_type: StringProperty(default='int')
def draw_color(self, context, node):
return (1.0, 1.0, 1.0, 1.0)
class RendermanNodeSocketInterfaceInt(bpy.types.NodeSocketInterfaceInt, RendermanSocketInterface):
'''RenderMan float input/output'''
bl_idname = 'RendermanNodeSocketInterfaceInt'
bl_label = 'RenderMan Int Socket'
bl_socket_idname = 'RendermanNodeSocketInt'
default_value: IntProperty()
def draw_color(self, context):
return (1.0, 1.0, 1.0, 1.0)
class RendermanNodeSocketString(bpy.types.NodeSocketString, RendermanSocket):
'''RenderMan string input/output'''
bl_idname = 'RendermanNodeSocketString'
bl_label = 'RenderMan String Socket'
default_value: StringProperty(update=update_func)
is_texture: BoolProperty(default=False)
renderman_type: StringProperty(default='string')
class RendermanNodeSocketStruct(bpy.types.NodeSocketString, RendermanSocket):
'''RenderMan struct input/output'''
bl_idname = 'RendermanNodeSocketStruct'
bl_label = 'RenderMan Struct Socket'
hide_value = True
renderman_type = 'string'
default_value = ''
class RendermanNodeSocketInterfaceStruct(bpy.types.NodeSocketInterfaceString, RendermanSocketInterface):
'''RenderMan struct input/output'''
bl_idname = 'RendermanNodeSocketInterfaceStruct'
bl_label = 'RenderMan Struct Socket'
bl_socket_idname = 'RendermanNodeSocketStruct'
hide_value = True
class RendermanNodeSocketColor(bpy.types.NodeSocketColor, RendermanSocket):
'''RenderMan color input/output'''
bl_idname = 'RendermanNodeSocketColor'
bl_label = 'RenderMan Color Socket'
default_value: FloatVectorProperty(size=3,
subtype="COLOR", update=update_func)
renderman_type: StringProperty(default='color')
def draw_color(self, context, node):
return (1.0, 1.0, .5, 1.0)
class RendermanNodeSocketInterfaceColor(bpy.types.NodeSocketInterfaceColor, RendermanSocketInterface):
'''RenderMan color input/output'''
bl_idname = 'RendermanNodeSocketInterfaceColor'
bl_label = 'RenderMan Color Socket'
bl_socket_idname = 'RendermanNodeSocketColor'
default_value: FloatVectorProperty(size=3,
subtype="COLOR")
def draw_color(self, context):
return (1.0, 1.0, .5, 1.0)
class RendermanNodeSocketVector(RendermanSocket, bpy.types.NodeSocketVector):
'''RenderMan vector input/output'''
bl_idname = 'RendermanNodeSocketVector'
bl_label = 'RenderMan Vector Socket'
hide_value = True
default_value: FloatVectorProperty(size=3,
subtype="EULER", update=update_func)
renderman_type: StringProperty(default='vector')
def draw_color(self, context, node):
return (.25, .25, .75, 1.0)
class RendermanNodeSocketInterfaceVector(bpy.types.NodeSocketInterfaceVector, RendermanSocketInterface):
'''RenderMan color input/output'''
bl_idname = 'RendermanNodeSocketInterfaceVector'
bl_label = 'RenderMan Vector Socket'
bl_socket_idname = 'RendermanNodeSocketVector'
hide_value = True
default_value: FloatVectorProperty(size=3,
subtype="EULER")
def draw_color(self, context):
return (.25, .25, .75, 1.0)
# Custom socket type for connecting shaders
class RendermanShaderSocket(bpy.types.NodeSocketShader, RendermanSocket):
'''RenderMan shader input/output'''
bl_idname = 'RendermanShaderSocket'
bl_label = 'RenderMan Shader Socket'
hide_value = True
# Custom socket type for connecting shaders
class RendermanShaderSocketInterface(bpy.types.NodeSocketInterfaceShader, RendermanSocketInterface):
'''RenderMan shader input/output'''
bl_idname = 'RendermanShaderInterfaceSocket'
bl_label = 'RenderMan Shader Socket'
bl_socket_idname = 'RendermanShaderSocket'
hide_value = True
# Base class for all custom nodes in this tree type.
# Defines a poll function to enable instantiation.
class RendermanShadingNode(bpy.types.ShaderNode):
bl_label = 'Output'
def update_mat(self, mat):
if self.renderman_node_type == 'bxdf' and self.outputs['Bxdf'].is_linked:
mat.specular_color = [1, 1, 1]
mat.diffuse_color = [1, 1, 1]
mat.use_transparency = False
mat.specular_intensity = 0
mat.diffuse_intensity = 1
if hasattr(self, "baseColor"):
mat.diffuse_color = self.baseColor
elif hasattr(self, "emitColor"):
mat.diffuse_color = self.emitColor
elif hasattr(self, "diffuseColor"):
mat.diffuse_color = self.diffuseColor
elif hasattr(self, "midColor"):
mat.diffuse_color = self.midColor
elif hasattr(self, "transmissionColor"):
mat.diffuse_color = self.transmissionColor
elif hasattr(self, "frontColor"):
mat.diffuse_color = self.frontColor
# specular intensity
if hasattr(self, "specular"):
mat.specular_intensity = self.specular
elif hasattr(self, "SpecularGainR"):
mat.specular_intensity = self.specularGainR
elif hasattr(self, "reflectionGain"):
mat.specular_intensity = self.reflectionGain
# specular color
if hasattr(self, "specularColor"):
mat.specular_color = self.specularColor
elif hasattr(self, "reflectionColor"):
mat.specular_color = self.reflectionColor
if self.bl_idname in ["PxrGlassBxdfNode", "PxrLMGlassBxdfNode"]:
mat.use_transparency = True
mat.alpha = .5
if self.bl_idname == "PxrLMMetalBxdfNode":
mat.diffuse_color = [0, 0, 0]
mat.specular_intensity = 1
mat.specular_color = self.specularColor
mat.mirror_color = [1, 1, 1]
elif self.bl_idname == "PxrLMPlasticBxdfNode":
mat.specular_intensity = 1
# all the properties of a shader will go here, also inputs/outputs
# on connectable props will have the same name
# node_props = None
def draw_buttons(self, context, layout):
self.draw_nonconnectable_props(context, layout, self.prop_names)
if self.bl_idname == "PxrOSLPatternNode":
layout.operator("node.refresh_osl_shader")
def draw_buttons_ext(self, context, layout):
self.draw_nonconnectable_props(context, layout, self.prop_names)
def draw_nonconnectable_props(self, context, layout, prop_names):
if self.bl_idname in ['PxrLayerPatternNode', 'PxrSurfaceBxdfNode']:
col = layout.column(align=True)
for prop_name in prop_names:
if prop_name not in self.inputs:
for name in getattr(self, prop_name):
if name.startswith('enable'):
col.prop(self, name, text=prop_name.split('.')[-1])
break
return
if self.bl_idname == "PxrOSLPatternNode" or self.bl_idname == "PxrSeExprPatternNode":
prop = getattr(self, "codetypeswitch")
layout.prop(self, "codetypeswitch")
if getattr(self, "codetypeswitch") == 'INT':
prop = getattr(self, "internalSearch")
layout.prop_search(
self, "internalSearch", bpy.data, "texts", text="")
elif getattr(self, "codetypeswitch") == 'EXT':
prop = getattr(self, "shadercode")
layout.prop(self, "shadercode")
elif getattr(self, "codetypeswitch") == 'NODE':
layout.prop(self, "expression")
else:
# temp until we can create ramps natively
if self.plugin_name == 'PxrRamp':
nt = bpy.data.node_groups[self.node_group]
if nt:
layout.template_color_ramp(
nt.nodes["ColorRamp"], 'color_ramp')
for prop_name in prop_names:
prop_meta = self.prop_meta[prop_name]
if 'widget' in prop_meta and prop_meta['widget'] == 'null' or \
'hidden' in prop_meta and prop_meta['hidden']:
continue
if prop_name not in self.inputs:
if prop_meta['renderman_type'] == 'page':
ui_prop = prop_name + "_uio"
ui_open = getattr(self, ui_prop)
icon = 'DISCLOSURE_TRI_DOWN' if ui_open \
else 'DISCLOSURE_TRI_RIGHT'
split = layout.split(NODE_LAYOUT_SPLIT)
row = split.row()
row.prop(self, ui_prop, icon=icon, text='',
icon_only=True, emboss=False, slider=True)
row.label(prop_name.split('.')[-1] + ':')
if ui_open:
prop = getattr(self, prop_name)
self.draw_nonconnectable_props(
context, layout, prop)
elif "Subset" in prop_name and prop_meta['type'] == 'string':
layout.prop_search(self, prop_name, bpy.data.scenes[0].renderman,
"object_groups")
else:
layout.prop(self, prop_name, slider=True)
def copy(self, node):
pass
# self.inputs.clear()
# self.outputs.clear()
def RefreshNodes(self, context, nodeOR=None, materialOverride=None):
# Compile shader. If the call was from socket draw get the node
# information anther way.
if hasattr(context, "node"):
node = context.node
else:
node = nodeOR
prefs = bpy.context.preferences.addons[__package__].preferences
out_path = user_path(prefs.env_vars.out)
compile_path = os.path.join(user_path(prefs.env_vars.out), "shaders")
if os.path.exists(out_path):
pass
else:
os.mkdir(out_path)
if os.path.exists(os.path.join(out_path, "shaders")):
pass
else:
os.mkdir(os.path.join(out_path, "shaders"))
if getattr(node, "codetypeswitch") == "EXT":
osl_path = user_path(getattr(node, 'shadercode'))
FileName = os.path.basename(osl_path)
FileNameNoEXT = os.path.splitext(FileName)[0]
FileNameOSO = FileNameNoEXT
FileNameOSO += ".oso"
export_path = os.path.join(
user_path(prefs.env_vars.out), "shaders", FileNameOSO)
if os.path.splitext(FileName)[1] == ".oso":
out_file = os.path.join(user_path(prefs.env_vars.out), "shaders", FileNameOSO)
if not os.path.exists(out_file) or not os.path.samefile(osl_path, out_file):
shutil.copy(osl_path, out_file)
# Assume that the user knows what they were doing when they
# compiled the osl file.
ok = True
else:
ok = node.compile_osl(osl_path, compile_path)
elif getattr(node, "codetypeswitch") == "INT" and node.internalSearch:
script = bpy.data.texts[node.internalSearch]
osl_path = bpy.path.abspath(
script.filepath, library=script.library)
if script.is_in_memory or script.is_dirty or \
script.is_modified or not os.path.exists(osl_path):
osl_file = tempfile.NamedTemporaryFile(
mode='w', suffix=".osl", delete=False)
osl_file.write(script.as_string())
osl_file.close()
FileNameNoEXT = os.path.splitext(script.name)[0]
FileNameOSO = FileNameNoEXT
FileNameOSO += ".oso"
node.plugin_name = FileNameNoEXT
ok = node.compile_osl(osl_file.name, compile_path, script.name)
export_path = os.path.join(
user_path(prefs.env_vars.out), "shaders", FileNameOSO)
os.remove(osl_file.name)
else:
ok = node.compile_osl(osl_path, compile_path)
FileName = os.path.basename(osl_path)
FileNameNoEXT = os.path.splitext(FileName)[0]
node.plugin_name = FileNameNoEXT
FileNameOSO = FileNameNoEXT
FileNameOSO += ".oso"
export_path = os.path.join(
user_path(prefs.env_vars.out), "shaders", FileNameOSO)
else:
ok = False
debug("osl", "Shader cannot be compiled. Shader name not specified")
# If Shader compiled successfully then update node.
if ok:
debug('osl', "Shader Compiled Successfully!")
# Reset the inputs and outputs
node.outputs.clear()
node.inputs.clear()
# Read in new properties
prop_names, shader_meta = readOSO(export_path)
debug('osl', prop_names, "MetaInfo: ", shader_meta)
# Set node name to shader name
node.label = shader_meta["shader"]
node.plugin_name = shader_meta["shader"]
# Generate new inputs and outputs
setattr(node, 'shader_meta', shader_meta)
node.setOslProps(prop_names, shader_meta)
else:
debug("osl", "NODE COMPILATION FAILED")
def compile_osl(self, inFile, outPath, nameOverride=""):
if not nameOverride:
FileName = os.path.basename(inFile)
FileNameNoEXT = os.path.splitext(FileName)[0]
out_file = os.path.join(outPath, FileNameNoEXT)
out_file += ".oso"
else:
FileNameNoEXT = os.path.splitext(nameOverride)[0]
out_file = os.path.join(outPath, FileNameNoEXT)
out_file += ".oso"
ok = _cycles.osl_compile(inFile, out_file)
return ok
def update(self):
debug("info", "UPDATING: ", self.name)
@classmethod
def poll(cls, ntree):
if hasattr(ntree, 'bl_idname'):
return ntree.bl_idname == 'ShaderNodeTree'
else:
return True
def setOslProps(self, prop_names, shader_meta):
for prop_name in prop_names:
prop_type = shader_meta[prop_name]["type"]
if shader_meta[prop_name]["IO"] == "out":
self.outputs.new(
socket_map[prop_type], prop_name)
else:
prop_default = shader_meta[prop_name]["default"]
if prop_type == "float":
prop_default = float(prop_default)
elif prop_type == "int":
prop_default = int(float(prop_default))
if prop_type == "matrix":
self.inputs.new(socket_map["struct"], prop_name, prop_name)
elif prop_type == "void":
pass
elif 'lockgeom' in shader_meta[prop_name] and shader_meta[prop_name]['lockgeom'] == 0:
pass
else:
input = self.inputs.new(socket_map[shader_meta[prop_name]["type"]],
prop_name, prop_name)
input.default_value = prop_default
if prop_type == 'struct' or prop_type == 'point':
input.hide_value = True
input.renderman_type = prop_type
debug('osl', "Shader: ", shader_meta["shader"], "Properties: ",
prop_names, "Shader meta data: ", shader_meta)
compileLocation = self.name + "Compile"
class RendermanOutputNode(RendermanShadingNode):
bl_label = 'RenderMan Material'
renderman_node_type = 'output'
bl_icon = 'MATERIAL'
node_tree = None
def init(self, context):
input = self.inputs.new('RendermanShaderSocket', 'Bxdf')
input.type = 'SHADER'
input.hide_value = True
input = self.inputs.new('RendermanShaderSocket', 'Light')
input.hide_value = True
input = self.inputs.new('RendermanShaderSocket', 'Displacement')
input.hide_value = True
def draw_buttons(self, context, layout):
return
def draw_buttons_ext(self, context, layout):
return
# when a connection is made or removed see if we're in IPR mode and issue
# updates
def update(self):
from . import engine
if engine.is_ipr_running():
engine.ipr.last_edit_mat = None
engine.ipr.issue_shader_edits(nt=self.id_data)
# Final output node, used as a dummy to find top level shaders
class RendermanBxdfNode(RendermanShadingNode):
bl_label = 'Bxdf'
renderman_node_type = 'bxdf'
shading_compatibility = {'NEW_SHADING'}
class RendermanDisplacementNode(RendermanShadingNode):
bl_label = 'Displacement'
renderman_node_type = 'displacement'
# Final output node, used as a dummy to find top level shaders
class RendermanPatternNode(RendermanShadingNode):
bl_label = 'Texture'
renderman_node_type = 'pattern'
bl_type = 'TEX_IMAGE'
bl_static_type = 'TEX_IMAGE'
class RendermanLightNode(RendermanShadingNode):
bl_label = 'Output'
renderman_node_type = 'light'
# Generate dynamic types
def generate_node_type(prefs, name, args):
''' Dynamically generate a node type from pattern '''
nodeType = args.find("shaderType/tag").attrib['value']
typename = '%s%sNode' % (name, nodeType.capitalize())
nodeDict = {'bxdf': RendermanBxdfNode,
'pattern': RendermanPatternNode,
'displacement': RendermanDisplacementNode,
'light': RendermanLightNode}
if nodeType not in nodeDict.keys():
return
ntype = type(typename, (nodeDict[nodeType],), {})
ntype.bl_label = name
ntype.typename = typename
inputs = [p for p in args.findall('./param')] + \
[p for p in args.findall('./page')]
outputs = [p for p in args.findall('.//output')]
def init(self, context):
if self.renderman_node_type == 'bxdf':
self.outputs.new('RendermanShaderSocket', "Bxdf").type = 'SHADER'
#socket_template = self.socket_templates.new(identifier='Bxdf', name='Bxdf', type='SHADER')
node_add_inputs(self, name, self.prop_names)
node_add_outputs(self)
# if this is PxrLayerSurface set the diffusegain to 0. The default
# of 1 is unintuitive
if self.plugin_name == 'PxrLayerSurface':
self.diffuseGain = 0
elif self.renderman_node_type == 'light':
# only make a few sockets connectable
node_add_inputs(self, name, self.prop_names)
self.outputs.new('RendermanShaderSocket', "Light")
elif self.renderman_node_type == 'displacement':
# only make the color connectable
self.outputs.new('RendermanShaderSocket', "Displacement")
node_add_inputs(self, name, self.prop_names)
# else pattern
elif name == "PxrOSL":
self.outputs.clear()
else:
node_add_inputs(self, name, self.prop_names)
node_add_outputs(self)
if name == "PxrRamp":
node_group = bpy.data.node_groups.new(
'PxrRamp_nodegroup', 'ShaderNodeTree')
node_group.nodes.new('ShaderNodeValToRGB')
node_group.use_fake_user = True
self.node_group = node_group.name
update_conditional_visops(self)
def free(self):
if name == "PxrRamp":
bpy.data.node_groups.remove(bpy.data.node_groups[self.node_group])
ntype.init = init
ntype.free = free
if name == 'PxrRamp':
ntype.node_group = StringProperty('color_ramp', default='')
ntype.plugin_name = StringProperty(name='Plugin Name',
default=name, options={'HIDDEN'})
# lights cant connect to a node tree in 20.0
class_generate_properties(ntype, name, inputs + outputs)
if nodeType == 'light':
ntype.light_shading_rate = FloatProperty(
name="Light Shading Rate",
description="Shading Rate for this light. \
Leave this high unless detail is missing",
default=100.0)
ntype.light_primary_visibility = BoolProperty(
name="Light Primary Visibility",
description="Camera visibility for this light",
default=True)
bpy.utils.register_class(ntype)
return typename, ntype
# UI
def find_node_input(node, name):
for input in node.inputs:
if input.name == name:
return input
return None
def find_node(material, nodetype):
if material and material.node_tree:
ntree = material.node_tree
active_output_node = None
for node in ntree.nodes:
if getattr(node, "bl_idname", None) == nodetype:
if getattr(node, "is_active_output", True):
return node
if not active_output_node:
active_output_node = node
return active_output_node
return None
def find_node_input(node, name):
for input in node.inputs:
if input.name == name:
return input
return None
def panel_node_draw(layout, context, id_data, output_type, input_name):
ntree = id_data.node_tree
node = find_node(id_data, output_type)
if not node:
layout.label(text="No output node")
else:
input = find_node_input(node, input_name)
#layout.template_node_view(ntree, node, input)
draw_nodes_properties_ui(layout, context, ntree)
return True
def is_renderman_nodetree(material):
return find_node(material, 'RendermanOutputNode')
def draw_nodes_properties_ui(layout, context, nt, input_name='Bxdf',
output_node_type="output"):
output_node = next((n for n in nt.nodes
if hasattr(n, 'renderman_node_type') and n.renderman_node_type == output_node_type), None)
if output_node is None:
return
socket = output_node.inputs[input_name]
node = socket_node_input(nt, socket)
layout.context_pointer_set("nodetree", nt)
layout.context_pointer_set("node", output_node)
layout.context_pointer_set("socket", socket)
split = layout.split(0.35)
split.label(socket.name + ':')
if socket.is_linked:
# for lights draw the shading rate ui.
split.operator_menu_enum("node.add_%s" % input_name.lower(),
"node_type", text=node.bl_label)
else:
split.operator_menu_enum("node.add_%s" % input_name.lower(),
"node_type", text='None')
if node is not None:
draw_node_properties_recursive(layout, context, nt, node)
def socket_node_input(nt, socket):
return next((l.from_node for l in nt.links if l.to_socket == socket), None)
def socket_socket_input(nt, socket):
return next((l.from_socket for l in nt.links if l.to_socket == socket and socket.is_linked),
None)
def linked_sockets(sockets):
if sockets is None:
return []
return [i for i in sockets if i.is_linked]
def draw_node_properties_recursive(layout, context, nt, node, level=0):
def indented_label(layout, label, level):
for i in range(level):
layout.label('', icon='BLANK1')
if label:
layout.label(label)
layout.context_pointer_set("node", node)
layout.context_pointer_set("nodetree", nt)
def draw_props(prop_names, layout, level):
for prop_name in prop_names:
# skip showing the shape for PxrStdAreaLight
if prop_name in ["lightGroup", "rman__Shape", "coneAngle", "penumbraAngle"]:
continue
if prop_name == "codetypeswitch":
row = layout.row()
if node.codetypeswitch == 'INT':
row.prop_search(node, "internalSearch",
bpy.data, "texts", text="")
elif node.codetypeswitch == 'EXT':
row.prop(node, "shadercode")
elif prop_name == "internalSearch" or prop_name == "shadercode" or prop_name == "expression":
pass
else:
prop_meta = node.prop_meta[prop_name]
prop = getattr(node, prop_name)
if 'widget' in prop_meta and prop_meta['widget'] == 'null' or \
'hidden' in prop_meta and prop_meta['hidden']:
continue
# else check if the socket with this name is connected
socket = node.inputs[prop_name] if prop_name in node.inputs \
else None
layout.context_pointer_set("socket", socket)
if socket and socket.is_linked:
input_node = socket_node_input(nt, socket)
icon = 'DISCLOSURE_TRI_DOWN' if socket.ui_open \
else 'DISCLOSURE_TRI_RIGHT'
split = layout.split(NODE_LAYOUT_SPLIT)
row = split.row()
indented_label(row, None, level)
row.prop(socket, "ui_open", icon=icon, text='',
icon_only=True, emboss=False)
label = prop_meta.get('label', prop_name)
row.label(label + ':')
if ('type' in prop_meta and prop_meta['type'] == 'vstruct') or prop_name == 'inputMaterial':
split.operator_menu_enum("node.add_layer", "node_type",
text=input_node.bl_label, icon="LAYER_USED")
elif prop_meta['renderman_type'] == 'struct':
split.operator_menu_enum("node.add_manifold", "node_type",
text=input_node.bl_label, icon="LAYER_USED")
elif prop_meta['renderman_type'] == 'normal':
split.operator_menu_enum("node.add_bump", "node_type",
text=input_node.bl_label, icon="LAYER_USED")
else:
split.operator_menu_enum("node.add_pattern", "node_type",
text=input_node.bl_label, icon="LAYER_USED")
if socket.ui_open:
draw_node_properties_recursive(layout, context, nt,
input_node, level=level + 1)
else:
row = layout.row(align=True)
if prop_meta['renderman_type'] == 'page':
ui_prop = prop_name + "_uio"
ui_open = getattr(node, ui_prop)
icon = 'DISCLOSURE_TRI_DOWN' if ui_open \
else 'DISCLOSURE_TRI_RIGHT'
split = layout.split(NODE_LAYOUT_SPLIT)
row = split.row()
for i in range(level):
row.label('', icon='BLANK1')
row.prop(node, ui_prop, icon=icon, text='',
icon_only=True, emboss=False)
sub_prop_names = list(prop)
if node.bl_idname in {"PxrSurfaceBxdfNode", "PxrLayerPatternNode"}:
for pn in sub_prop_names:
if pn.startswith('enable'):
row.prop(node, pn, text='')
sub_prop_names.remove(pn)
break
row.label(prop_name.split('.')[-1] + ':')
if ui_open:
draw_props(sub_prop_names, layout, level + 1)
else:
indented_label(row, None, level)
# indented_label(row, socket.name+':')
# don't draw prop for struct type
if "Subset" in prop_name and prop_meta['type'] == 'string':
row.prop_search(node, prop_name, bpy.data.scenes[0].renderman,
"object_groups")
else:
if prop_meta['renderman_type'] != 'struct':
row.prop(node, prop_name, slider=True)
else:
row.label(prop_meta['label'])
if prop_name in node.inputs:
if ('type' in prop_meta and prop_meta['type'] == 'vstruct') or prop_name == 'inputMaterial':
row.operator_menu_enum("node.add_layer", "node_type",
text='', icon="LAYER_USED")
elif prop_meta['renderman_type'] == 'struct':
row.operator_menu_enum("node.add_manifold", "node_type",
text='', icon="LAYER_USED")
elif prop_meta['renderman_type'] == 'normal':
row.operator_menu_enum("node.add_bump", "node_type",
text='', icon="LAYER_USED")
else:
row.operator_menu_enum("node.add_pattern", "node_type",
text='', icon="LAYER_USED")
# if this is a cycles node do something different
if not hasattr(node, 'plugin_name') or node.bl_idname == 'PxrOSLPatternNode':
node.draw_buttons(context, layout)
for input in node.inputs:
if input.is_linked:
input_node = socket_node_input(nt, input)
icon = 'DISCLOSURE_TRI_DOWN' if input.show_expanded \
else 'DISCLOSURE_TRI_RIGHT'
split = layout.split(NODE_LAYOUT_SPLIT)
row = split.row()
indented_label(row, None, level)
row.prop(input, "show_expanded", icon=icon, text='',
icon_only=True, emboss=False)
row.label(input.name + ':')
split.operator_menu_enum("node.add_pattern", "node_type",
text=input_node.bl_label, icon="LAYER_USED")
if input.show_expanded:
draw_node_properties_recursive(layout, context, nt,
input_node, level=level + 1)
else:
row = layout.row(align=True)
indented_label(row, None, level)
# indented_label(row, socket.name+':')
# don't draw prop for struct type
if input.hide_value:
row.label(input.name)
else:
row.prop(input, 'default_value',
slider=True, text=input.name)
row.operator_menu_enum("node.add_pattern", "node_type",
text='', icon="LAYER_USED")
else:
if node.plugin_name == 'PxrRamp':
dummy_nt = bpy.data.node_groups[node.node_group]
if dummy_nt:
layout.template_color_ramp(
dummy_nt.nodes['ColorRamp'], 'color_ramp')
draw_props(node.prop_names, layout, level)
layout.separator()
# Operators
# connect the pattern nodes in some sensible manner (color output to color input etc)
# TODO more robust
def link_node(nt, from_node, in_socket):
out_socket = None
# first look for resultF/resultRGB
if type(in_socket).__name__ in ['RendermanNodeSocketColor',
'RendermanNodeSocketVector']:
out_socket = from_node.outputs.get('resultRGB',
next((s for s in from_node.outputs
if type(s).__name__ == 'RendermanNodeSocketColor'), None))
elif type(in_socket).__name__ == 'RendermanNodeSocketStruct':
out_socket = from_node.outputs.get('pxrMaterialOut', None)
if not out_socket:
out_socket = from_node.outputs.get('result', None)
else:
out_socket = from_node.outputs.get('resultF',
next((s for s in from_node.outputs
if type(s).__name__ == 'RendermanNodeSocketFloat'), None))
if out_socket:
nt.links.new(out_socket, in_socket)
# bass class for operator to add a node
class Add_Node:
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
def get_type_items(self, context):
items = []
# if this is a pattern input do columns!
if self.input_type.lower() == 'pattern':
i = 0
for pattern_cat, patterns in pattern_categories.items():
if pattern_cat.lower() in ['layer', 'script', 'manifold', 'bump', 'displace']:
continue
items.append(('', pattern_cat, pattern_cat, '', 0))
for nodename in sorted(patterns):
nodetype = patterns[nodename]
items.append((nodetype.typename, nodetype.bl_label,
nodetype.bl_label, '', i))
i += 1
items.append(('', '', '', '', 0))
items.append(('REMOVE', 'Remove',
'Remove the node connected to this socket', '', i + 1))
items.append(('DISCONNECT', 'Disconnect',
'Disconnect the node connected to this socket', '', i + 2))
elif self.input_type.lower() in ['layer', 'manifold', 'bump']:
patterns = pattern_categories[self.input_type]
for nodename in sorted(patterns):
nodetype = patterns[nodename]
items.append((nodetype.typename, nodetype.bl_label,
nodetype.bl_label))
items.append(('REMOVE', 'Remove',
'Remove the node connected to this socket'))
items.append(('DISCONNECT', 'Disconnect',
'Disconnect the node connected to this socket'))
else:
for nodetype in nodetypes.values():
if self.input_type.lower() == 'light' and nodetype.renderman_node_type == 'light':
if nodetype.__name__ == 'PxrMeshLightLightNode':
items.append((nodetype.typename, nodetype.bl_label,
nodetype.bl_label))
elif nodetype.renderman_node_type == self.input_type.lower():
items.append((nodetype.typename, nodetype.bl_label,
nodetype.bl_label))
items = sorted(items, key=itemgetter(1))
items.append(('REMOVE', 'Remove',
'Remove the node connected to this socket'))
items.append(('DISCONNECT', 'Disconnect',
'Disconnect the node connected to this socket'))
return items
node_type: EnumProperty(name="Node Type",
description='Node type to add to this socket',
items=get_type_items)
def execute(self, context):
new_type = self.properties.node_type
if new_type == 'DEFAULT':
return {'CANCELLED'}
nt = context.nodetree
node = context.node
socket = context.socket
input_node = socket_node_input(nt, socket)
if new_type == 'REMOVE':
nt.nodes.remove(input_node)
return {'FINISHED'}
if new_type == 'DISCONNECT':
link = next((l for l in nt.links if l.to_socket == socket), None)
nt.links.remove(link)
return {'FINISHED'}
# add a new node to existing socket
if input_node is None:
newnode = nt.nodes.new(new_type)
newnode.location = node.location
newnode.location[0] -= 300
newnode.selected = False
if self.input_type in ['Pattern', 'Layer', 'Manifold', 'Bump']:
link_node(nt, newnode, socket)
else:
nt.links.new(newnode.outputs[self.input_type], socket)
# replace input node with a new one
else:
newnode = nt.nodes.new(new_type)
input = socket
old_node = input.links[0].from_node
if self.input_type == 'Pattern':
link_node(nt, newnode, socket)
else:
nt.links.new(newnode.outputs[self.input_type], socket)
newnode.location = old_node.location
active_material = context.active_object.active_material
newnode.update_mat(active_material)
nt.nodes.remove(old_node)
return {'FINISHED'}
class NODE_OT_add_bxdf(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new bxdfs,
connected to a given input socket.
'''
bl_idname = 'node.add_bxdf'
bl_label = 'Add Bxdf Node'
bl_description = 'Connect a Bxdf to this socket'
input_type: StringProperty(default='Bxdf')
class NODE_OT_add_displacement(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_displacement'
bl_label = 'Add Displacement Node'
bl_description = 'Connect a Displacement shader to this socket'
input_type: StringProperty(default='Displacement')
class NODE_OT_add_light(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_light'
bl_label = 'Add Light Node'
bl_description = 'Connect a Light shader to this socket'
input_type: StringProperty(default='Light')
class NODE_OT_add_pattern(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_pattern'
bl_label = 'Add Pattern Node'
bl_description = 'Connect a Pattern to this socket'
input_type: StringProperty(default='Pattern')
class NODE_OT_add_layer(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_layer'
bl_label = 'Add Layer Node'
bl_description = 'Connect a PxrLayer'
input_type: StringProperty(default='Layer')
class NODE_OT_add_manifold(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_manifold'
bl_label = 'Add Manifold Node'
bl_description = 'Connect a Manifold'
input_type: StringProperty(default='Manifold')
class NODE_OT_add_bump(bpy.types.Operator, Add_Node):
'''
For generating cycles-style ui menus to add new nodes,
connected to a given input socket.
'''
bl_idname = 'node.add_bump'
bl_label = 'Add Bump Node'
bl_description = 'Connect a bump node'
input_type: StringProperty(default='Bump')
# return if this param has a vstuct connection or linked independently
def is_vstruct_or_linked(node, param):
meta = node.prop_meta[param]
if 'vstructmember' not in meta.keys():
return node.inputs[param].is_linked
elif param in node.inputs and node.inputs[param].is_linked:
return True
else:
vstruct_name, vstruct_member = meta['vstructmember'].split('.')
if node.inputs[vstruct_name].is_linked:
from_socket = node.inputs[vstruct_name].links[0].from_socket
vstruct_from_param = "%s_%s" % (
from_socket.identifier, vstruct_member)
return vstruct_conditional(from_socket.node, vstruct_from_param)
else:
return False
# tells if this param has a vstuct connection that is linked and
# conditional met
def is_vstruct_and_linked(node, param):
meta = node.prop_meta[param]
if 'vstructmember' not in meta.keys():
return False
else:
vstruct_name, vstruct_member = meta['vstructmember'].split('.')
if node.inputs[vstruct_name].is_linked:
from_socket = node.inputs[vstruct_name].links[0].from_socket
# if coming from a shader group hookup across that
if from_socket.node.bl_idname == 'ShaderNodeGroup':
ng = from_socket.node.node_tree
group_output = next((n for n in ng.nodes if n.bl_idname == 'NodeGroupOutput'),
None)
if group_output is None:
return False
in_sock = group_output.inputs[from_socket.name]
if len(in_sock.links):
from_socket = in_sock.links[0].from_socket
vstruct_from_param = "%s_%s" % (
from_socket.identifier, vstruct_member)
return vstruct_conditional(from_socket.node, vstruct_from_param)
else:
return False
# gets the value for a node walking up the vstruct chain
def get_val_vstruct(node, param):
if param in node.inputs and node.inputs[param].is_linked:
from_socket = node.inputs[param].links[0].from_socket
return get_val_vstruct(from_socket.node, from_socket.identifier)
elif is_vstruct_and_linked(node, param):
return True
else:
return getattr(node, param)
# parse a vstruct conditional string and return true or false if should link
def vstruct_conditional(node, param):
if not hasattr(node, 'shader_meta') and not hasattr(node, 'output_meta'):
return False
meta = getattr(
node, 'shader_meta') if node.bl_idname == "PxrOSLPatternNode" else node.output_meta
if param not in meta:
return False
meta = meta[param]
if 'vstructConditionalExpr' not in meta.keys():
return True
expr = meta['vstructConditionalExpr']
expr = expr.replace('connect if ', '')
set_zero = False
if ' else set 0' in expr:
expr = expr.replace(' else set 0', '')
set_zero = True
tokens = expr.split()
new_tokens = []
i = 0
num_tokens = len(tokens)
while i < num_tokens:
token = tokens[i]
prepend, append = '', ''
while token[0] == '(':
token = token[1:]
prepend += '('
while token[-1] == ')':
token = token[:-1]
append += ')'
if token == 'set':
i += 1
continue
# is connected change this to node.inputs.is_linked
if i < num_tokens - 2 and tokens[i + 1] == 'is'\
and 'connected' in tokens[i + 2]:
token = "is_vstruct_or_linked(node, '%s')" % token
last_token = tokens[i + 2]
while last_token[-1] == ')':
last_token = last_token[:-1]
append += ')'
i += 3
else:
i += 1
if hasattr(node, token):
token = "get_val_vstruct(node, '%s')" % token
new_tokens.append(prepend + token + append)
if 'if' in new_tokens and 'else' not in new_tokens:
new_tokens.extend(['else', 'False'])
return eval(" ".join(new_tokens))
# Rib export
gains_to_enable = {
'diffuseGain': 'enableDiffuse',
'specularFaceColor': 'enablePrimarySpecular',
'specularEdgeColor': 'enablePrimarySpecular',
'roughSpecularFaceColor': 'enableRoughSpecular',
'roughSpecularEdgeColor': 'enableRoughSpecular',
'clearcoatFaceColor': 'enableClearCoat',
'clearcoatEdgeColor': 'enableClearCoat',
'iridescenceFaceGain': 'enableIridescence',
'iridescenceEdgeGain': 'enableIridescence',
'fuzzGain': 'enableFuzz',
'subsurfaceGain': 'enableSubsurface',
'singlescatterGain': 'enableSingleScatter',
'singlescatterDirectGain': 'enableSingleScatter',
'refractionGain': 'enableGlass',
'reflectionGain': 'enableGlass',
'glowGain': 'enableGlow',
}
# generate param list
def gen_params(ri, node, mat_name=None):
params = {}
# If node is OSL node get properties from dynamic location.
if node.bl_idname == "PxrOSLPatternNode":
if getattr(node, "codetypeswitch") == "EXT":
prefs = bpy.context.preferences.addons[__package__].preferences
osl_path = user_path(getattr(node, 'shadercode'))
FileName = os.path.basename(osl_path)
FileNameNoEXT,ext = os.path.splitext(FileName)
out_file = os.path.join(
user_path(prefs.env_vars.out), "shaders", FileName)
if ext == ".oso":
if not os.path.exists(out_file) or not os.path.samefile(osl_path, out_file):
if not os.path.exists(os.path.join(user_path(prefs.env_vars.out), "shaders")):
os.mkdir(os.path.join(user_path(prefs.env_vars.out), "shaders"))
shutil.copy(osl_path, out_file)
for input_name, input in node.inputs.items():
prop_type = input.renderman_type
if input.is_linked:
to_socket = input
from_socket = input.links[0].from_socket
params['reference %s %s' % (prop_type, input_name)] = \
[get_output_param_str(
from_socket.node, mat_name, from_socket, to_socket)]
elif type(input) != RendermanNodeSocketStruct:
params['%s %s' % (prop_type, input_name)] = \
rib(input.default_value,
type_hint=prop_type)
# Special case for SeExpr Nodes. Assume that the code will be in a file so
# that needs to be extracted.
elif node.bl_idname == "PxrSeExprPatternNode":
fileInputType = node.codetypeswitch
for prop_name, meta in node.prop_meta.items():
if prop_name in ["codetypeswitch", 'filename']:
pass
elif prop_name == "internalSearch" and fileInputType == 'INT':
if node.internalSearch != "":
script = bpy.data.texts[node.internalSearch]
params['%s %s' % ("string",
"expression")] = \
rib(script.as_string(),
type_hint=meta['renderman_type'])
elif prop_name == "shadercode" and fileInputType == "NODE":
params['%s %s' % ("string", "expression")] = node.expression
else:
prop = getattr(node, prop_name)
# if input socket is linked reference that
if prop_name in node.inputs and \
node.inputs[prop_name].is_linked:
to_socket = node.inputs[prop_name]
from_socket = to_socket.links[0].from_socket
params['reference %s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
[get_output_param_str(
from_socket.node, mat_name, from_socket, to_socket)]
# else output rib
else:
params['%s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
rib(prop, type_hint=meta['renderman_type'])
else:
for prop_name, meta in node.prop_meta.items():
if prop_name in txmake_options.index:
pass
elif node.plugin_name == 'PxrRamp' and prop_name in ['colors', 'positions']:
pass
elif(prop_name in ['sblur', 'tblur', 'notes']):
pass
else:
prop = getattr(node, prop_name)
# if property group recurse
if meta['renderman_type'] == 'page':
continue
elif prop_name == 'inputMaterial' or \
('type' in meta and meta['type'] == 'vstruct'):
continue
# if input socket is linked reference that
elif hasattr(node, 'inputs') and prop_name in node.inputs and \
node.inputs[prop_name].is_linked:
to_socket = node.inputs[prop_name]
from_socket = to_socket.links[0].from_socket
from_node = to_socket.links[0].from_node
if 'arraySize' in meta:
params['reference %s[1] %s' % (meta['renderman_type'],
meta['renderman_name'])] \
= [get_output_param_str(
from_node, mat_name, from_socket, to_socket)]
else:
params['reference %s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
[get_output_param_str(
from_node, mat_name, from_socket, to_socket)]
# see if vstruct linked
elif is_vstruct_and_linked(node, prop_name):
vstruct_name, vstruct_member = meta[
'vstructmember'].split('.')
from_socket = node.inputs[
vstruct_name].links[0].from_socket
temp_mat_name = mat_name
if from_socket.node.bl_idname == 'ShaderNodeGroup':
ng = from_socket.node.node_tree
group_output = next((n for n in ng.nodes if n.bl_idname == 'NodeGroupOutput'),
None)
if group_output is None:
return False
in_sock = group_output.inputs[from_socket.name]
if len(in_sock.links):
from_socket = in_sock.links[0].from_socket
temp_mat_name = mat_name + '.' + from_socket.node.name
vstruct_from_param = "%s_%s" % (
from_socket.identifier, vstruct_member)
if vstruct_from_param in from_socket.node.output_meta:
actual_socket = from_socket.node.output_meta[
vstruct_from_param]
params['reference %s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
[get_output_param_str(
from_socket.node, temp_mat_name, actual_socket)]
else:
print('Warning! %s not found on %s' %
(vstruct_from_param, from_socket.node.name))
# else output rib
else:
# if struct is not linked continue
if meta['renderman_type'] in ['struct', 'enum']:
continue
# if this is a gain on PxrSurface and the lobe isn't
# enabled
if node.bl_idname == 'PxrSurfaceBxdfNode' and \
prop_name in gains_to_enable and \
not getattr(node, gains_to_enable[prop_name]):
val = [0, 0, 0] if meta[
'renderman_type'] == 'color' else 0
params['%s %s' % (meta['renderman_type'],
meta['renderman_name'])] = val
elif 'options' in meta and meta['options'] == 'texture' \
and node.bl_idname != "PxrPtexturePatternNode" or \
('widget' in meta and meta['widget'] == 'assetIdInput' and prop_name != 'iesProfile'):
params['%s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
rib(get_tex_file_name(prop),
type_hint=meta['renderman_type'])
elif 'arraySize' in meta:
if type(prop) == int:
prop = [prop]
params['%s[%d] %s' % (meta['renderman_type'], len(prop),
meta['renderman_name'])] \
= rib(prop)
else:
params['%s %s' % (meta['renderman_type'],
meta['renderman_name'])] = \
rib(prop, type_hint=meta['renderman_type'])
if node.plugin_name == 'PxrRamp':
nt = bpy.data.node_groups[node.node_group]
if nt:
dummy_ramp = nt.nodes['ColorRamp']
colors = []
positions = []
# double the start and end points
positions.append(float(dummy_ramp.color_ramp.elements[0].position))
colors.extend(dummy_ramp.color_ramp.elements[0].color[:3])
for e in dummy_ramp.color_ramp.elements:
positions.append(float(e.position))
colors.extend(e.color[:3])
positions.append(
float(dummy_ramp.color_ramp.elements[-1].position))
colors.extend(dummy_ramp.color_ramp.elements[-1].color[:3])
params['color[%d] colors' % len(positions)] = colors
params['float[%d] positions' % len(positions)] = positions
return params
def create_rman_surface(nt, parent_node, input_index, node_type="PxrSurfaceBxdfNode"):
layer = nt.nodes.new(node_type)
nt.links.new(layer.outputs[0], parent_node.inputs[input_index])
setattr(layer, 'enableDiffuse', False)
layer.location = parent_node.location
layer.diffuseGain = 0
layer.location[0] -= 300
return layer
combine_nodes = ['ShaderNodeAddShader', 'ShaderNodeMixShader']
# rman_parent could be PxrSurface or PxrMixer
def convert_cycles_bsdf(nt, rman_parent, node, input_index):
# if mix or add pass both to parent
if node.bl_idname in combine_nodes:
i = 0 if node.bl_idname == 'ShaderNodeAddShader' else 1
node1 = node.inputs[
0 + i].links[0].from_node if node.inputs[0 + i].is_linked else None
node2 = node.inputs[
1 + i].links[0].from_node if node.inputs[1 + i].is_linked else None
if not node1 and not node2:
return
elif not node1:
convert_cycles_bsdf(nt, rman_parent, node2, input_index)
elif not node2:
convert_cycles_bsdf(nt, rman_parent, node1, input_index)
# if ones a combiner or they're of the same type and not glossy we need
# to make a mixer
elif node.bl_idname == 'ShaderNodeMixShader' or node1.bl_idname in combine_nodes \
or node2.bl_idname in combine_nodes or \
node1.bl_idname == 'ShaderNodeGroup' or node2.bl_idname == 'ShaderNodeGroup' \
or (bsdf_map[node1.bl_idname][0] == bsdf_map[node2.bl_idname][0]):
mixer = nt.nodes.new('PxrLayerMixerPatternNode')
# if parent is output make a pxr surface first
nt.links.new(mixer.outputs["pxrMaterialOut"],
rman_parent.inputs[input_index])
offset_node_location(rman_parent, mixer, node)
# set the layer masks
if node.bl_idname == 'ShaderNodeAddShader':
mixer.layer1Mask = .5
else:
convert_cycles_input(
nt, node.inputs['Fac'], mixer, 'layer1Mask')
# make a new node for each
convert_cycles_bsdf(nt, mixer, node1, 0)
convert_cycles_bsdf(nt, mixer, node2, 1)
# this is a heterogenous mix of add
else:
if rman_parent.plugin_name == 'PxrLayerMixer':
old_parent = rman_parent
rman_parent = create_rman_surface(nt, rman_parent, input_index,
'PxrLayerPatternNode')
offset_node_location(old_parent, rman_parent, node)
convert_cycles_bsdf(nt, rman_parent, node1, 0)
convert_cycles_bsdf(nt, rman_parent, node2, 1)
# else set lobe on parent
elif 'Bsdf' in node.bl_idname or node.bl_idname == 'ShaderNodeSubsurfaceScattering':
if rman_parent.plugin_name == 'PxrLayerMixer':
old_parent = rman_parent
rman_parent = create_rman_surface(nt, rman_parent, input_index,
'PxrLayerPatternNode')
offset_node_location(old_parent, rman_parent, node)
node_type = node.bl_idname
bsdf_map[node_type][1](nt, node, rman_parent)
# if we find an emission node, naively make it a meshlight
# note this will only make the last emission node the light
elif node.bl_idname == 'ShaderNodeEmission':
output = next((n for n in nt.nodes if hasattr(n, 'renderman_node_type') and
n.renderman_node_type == 'output'),
None)
meshlight = nt.nodes.new("PxrMeshLightLightNode")
nt.links.new(meshlight.outputs[0], output.inputs["Light"])
meshlight.location = output.location
meshlight.location[0] -= 300
convert_cycles_input(
nt, node.inputs['Strength'], meshlight, "intensity")
if node.inputs['Color'].is_linked:
convert_cycles_input(
nt, node.inputs['Color'], meshlight, "textureColor")
else:
setattr(meshlight, 'lightColor', node.inputs[
'Color'].default_value[:3])
else:
rman_node = convert_cycles_node(nt, node)
nt.links.new(rman_node.outputs[0], rman_parent.inputs[input_index])
def convert_cycles_displacement(nt, surface_node, displace_socket):
# for now just do bump
if displace_socket.is_linked:
bump = nt.nodes.new("PxrBumpPatternNode")
nt.links.new(bump.outputs[0], surface_node.inputs['bumpNormal'])
bump.location = surface_node.location
bump.location[0] -= 200
bump.location[1] -= 100
convert_cycles_input(nt, displace_socket, bump, "inputBump")
# return
# if displace_socket.is_linked:
# displace = nt.nodes.new("PxrDisplaceDisplacementNode")
# nt.links.new(displace.outputs[0], output_node.inputs['Displacement'])
# displace.location = output_node.location
# displace.location[0] -= 200
# displace.location[1] -= 100
# setattr(displace, 'dispAmount', .01)
# convert_cycles_input(nt, displace_socket, displace, "dispScalar")
# could make this more robust to shift the entire nodetree to below the
# bounds of the cycles nodetree
def set_ouput_node_location(nt, output_node, cycles_output):
output_node.location = cycles_output.location
output_node.location[1] -= 500
def offset_node_location(rman_parent, rman_node, cycles_node):
linked_socket = next((sock for sock in cycles_node.outputs if sock.is_linked),
None)
rman_node.location = rman_parent.location
if linked_socket:
rman_node.location += (cycles_node.location -
linked_socket.links[0].to_node.location)
def convert_cycles_nodetree(id, output_node, reporter):
# find base node
from . import cycles_convert
cycles_convert.converted_nodes = {}
nt = id.node_tree
reporter({'INFO'}, 'Converting material ' + id.name + ' to RenderMan')
cycles_output_node = find_node(id, 'ShaderNodeOutputMaterial')
if not cycles_output_node:
reporter({'WARNING'}, 'No Cycles output found ' + id.name)
return False
# if no bsdf return false
if not cycles_output_node.inputs[0].is_linked:
reporter({'WARNING'}, 'No Cycles bsdf found ' + id.name)
return False
# set the output node location
set_ouput_node_location(nt, output_node, cycles_output_node)
# walk tree
cycles_convert.report = reporter
begin_cycles_node = cycles_output_node.inputs[0].links[0].from_node
# if this is an emission use PxrLightEmission
if begin_cycles_node.bl_idname == "ShaderNodeEmission":
meshlight = nt.nodes.new("PxrMeshLightLightNode")
nt.links.new(meshlight.outputs[0], output_node.inputs["Light"])
offset_node_location(output_node, meshlight, begin_cycles_node)
convert_cycles_input(nt, begin_cycles_node.inputs[
'Strength'], meshlight, "intensity")
if begin_cycles_node.inputs['Color'].is_linked:
convert_cycles_input(nt, begin_cycles_node.inputs[
'Color'], meshlight, "textureColor")
else:
setattr(meshlight, 'lightColor', begin_cycles_node.inputs[
'Color'].default_value[:3])
bxdf = nt.nodes.new('PxrBlackBxdfNode')
nt.links.new(bxdf.outputs[0], output_node.inputs["Bxdf"])
else:
base_surface = create_rman_surface(nt, output_node, 0)
offset_node_location(output_node, base_surface, begin_cycles_node)
convert_cycles_bsdf(nt, base_surface, begin_cycles_node, 0)
convert_cycles_displacement(
nt, base_surface, cycles_output_node.inputs[2])
return True
cycles_node_map = {
'ShaderNodeAttribute': 'node_attribute',
'ShaderNodeBlackbody': 'node_checker_blackbody',
'ShaderNodeTexBrick': 'node_brick_texture',
'ShaderNodeBrightContrast': 'node_brightness',
'ShaderNodeTexChecker': 'node_checker_texture',
'ShaderNodeBump': 'node_bump',
'ShaderNodeCameraData': 'node_camera',
'ShaderNodeTexChecker': 'node_checker_texture',
'ShaderNodeCombineHSV': 'node_combine_hsv',
'ShaderNodeCombineRGB': 'node_combine_rgb',
'ShaderNodeCombineXYZ': 'node_combine_xyz',
'ShaderNodeTexEnvironment': 'node_environment_texture',
'ShaderNodeFresnel': 'node_fresnel',
'ShaderNodeGamma': 'node_gamma',
'ShaderNodeNewGeometry': 'node_geometry',
'ShaderNodeTexGradient': 'node_gradient_texture',
'ShaderNodeHairInfo': 'node_hair_info',
'ShaderNodeInvert': 'node_invert',
'ShaderNodeHueSaturation': 'node_hsv',
'ShaderNodeTexImage': 'node_image_texture',
'ShaderNodeHueSaturation': 'node_hsv',
'ShaderNodeLayerWeight': 'node_layer_weight',
'ShaderNodeLightFalloff': 'node_light_falloff',
'ShaderNodeLightPath': 'node_light_path',
'ShaderNodeTexMagic': 'node_magic_texture',
'ShaderNodeMapping': 'node_mapping',
'ShaderNodeMath': 'node_math',
'ShaderNodeMixRGB': 'node_mix',
'ShaderNodeTexMusgrave': 'node_musgrave_texture',
'ShaderNodeTexNoise': 'node_noise_texture',
'ShaderNodeNormal': 'node_normal',
'ShaderNodeNormalMap': 'node_normal_map',
'ShaderNodeObjectInfo': 'node_object_info',
'ShaderNodeParticleInfo': 'node_particle_info',
'ShaderNodeRGBCurve': 'node_rgb_curves',
'ShaderNodeValToRGB': 'node_rgb_ramp',
'ShaderNodeSeparateHSV': 'node_separate_hsv',
'ShaderNodeSeparateRGB': 'node_separate_rgb',
'ShaderNodeSeparateXYZ': 'node_separate_xyz',
'ShaderNodeTexSky': 'node_sky_texture',
'ShaderNodeTangent': 'node_tangent',
'ShaderNodeTexCoord': 'node_texture_coordinate',
'ShaderNodeUVMap': 'node_uv_map',
'ShaderNodeValue': 'node_value',
'ShaderNodeVectorCurves': 'node_vector_curves',
'ShaderNodeVectorMath': 'node_vector_math',
'ShaderNodeVectorTransform': 'node_vector_transform',
'ShaderNodeTexVoronoi': 'node_voronoi_texture',
'ShaderNodeTexWave': 'node_wave_texture',
'ShaderNodeWavelength': 'node_wavelength',
'ShaderNodeWireframe': 'node_wireframe',
}
def get_mat_name(mat_name):
return mat_name.replace(' ', '')
def get_node_name(node, mat_name):
return "%s.%s" % (mat_name, node.name.replace(' ', ''))
def get_socket_name(node, socket):
if type(socket) == dict:
return socket['name'].replace(' ', '')
# if this is a renderman node we can just use the socket name,
else:
if not hasattr('node', 'plugin_name'):
if socket.name in node.inputs and socket.name in node.outputs:
suffix = 'Out' if socket.is_output else 'In'
return socket.name.replace(' ', '') + suffix
return socket.identifier.replace(' ', '')
def get_socket_type(node, socket):
sock_type = socket.type.lower()
if sock_type == 'rgba':
return 'color'
elif sock_type == 'value':
return 'float'
elif sock_type == 'vector':
return 'point'
else:
return sock_type
# do we need to convert this socket?
def do_convert_socket(from_socket, to_socket):
if not to_socket:
return False
return (is_float_type(from_socket) and is_float3_type(to_socket)) or \
(is_float3_type(from_socket) and is_float_type(to_socket))
def build_output_param_str(mat_name, from_node, from_socket, convert_socket=False):
from_node_name = get_node_name(from_node, mat_name)
from_sock_name = get_socket_name(from_node, from_socket)
# replace with the convert node's output
if convert_socket:
if is_float_type(from_socket):
return "convert_%s.%s:resultRGB" % (from_node_name, from_sock_name)
else:
return "convert_%s.%s:resultF" % (from_node_name, from_sock_name)
else:
return "%s:%s" % (from_node_name, from_sock_name)
def get_output_param_str(node, mat_name, socket, to_socket=None):
# if this is a node group, hook it up to the input node inside!
if node.bl_idname == 'ShaderNodeGroup':
ng = node.node_tree
group_output = next((n for n in ng.nodes if n.bl_idname == 'NodeGroupOutput'),
None)
if group_output is None:
return "error:error"
in_sock = group_output.inputs[socket.name]
if len(in_sock.links):
link = in_sock.links[0]
return build_output_param_str(mat_name + '.' + node.name, link.from_node, link.from_socket, do_convert_socket(link.from_socket, to_socket))
else:
return "error:error"
if node.bl_idname == 'NodeGroupInput':
global current_group_node
if current_group_node is None:
return "error:error"
in_sock = current_group_node.inputs[socket.name]
if len(in_sock.links):
link = in_sock.links[0]
return build_output_param_str(mat_name, link.from_node, link.from_socket, do_convert_socket(link.from_socket, to_socket))
else:
return "error:error"
return build_output_param_str(mat_name, node, socket, do_convert_socket(socket, to_socket))
# hack!!!
current_group_node = None
def translate_node_group(ri, group_node, mat_name):
ng = group_node.node_tree
out = next((n for n in ng.nodes if n.bl_idname == 'NodeGroupOutput'),
None)
if out is None:
return
nodes_to_export = gather_nodes(out)
global current_group_node
current_group_node = group_node
for node in nodes_to_export:
shader_node_rib(ri, node, mat_name=(mat_name + '.' + group_node.name))
current_group_node = None
def translate_cycles_node(ri, node, mat_name):
if node.bl_idname == 'ShaderNodeGroup':
translate_node_group(ri, node, mat_name)
return
if node.bl_idname not in cycles_node_map.keys():
print('No translation for node of type %s named %s' %
(node.bl_idname, node.name))
return
mapping = cycles_node_map[node.bl_idname]
params = {}
for in_name, input in node.inputs.items():
param_name = "%s %s" % (get_socket_type(
node, input), get_socket_name(node, input))
if input.is_linked:
param_name = 'reference ' + param_name
link = input.links[0]
param_val = get_output_param_str(
link.from_node, mat_name, link.from_socket, input)
else:
param_val = rib(input.default_value,
type_hint=get_socket_type(node, input))
# skip if this is a vector set to 0 0 0
if input.type == 'VECTOR' and param_val == [0.0, 0.0, 0.0]:
continue
params[param_name] = param_val
ramp_size = 256
if node.bl_idname == 'ShaderNodeValToRGB':
colors = []
alphas = []
for i in range(ramp_size):
c = node.color_ramp.evaluate(float(i) / (ramp_size - 1.0))
colors.extend(c[:3])
alphas.append(c[3])
params['color[%d] ramp_color' % ramp_size] = colors
params['float[%d] ramp_alpha' % ramp_size] = alphas
elif node.bl_idname == 'ShaderNodeVectorCurve':
colors = []
node.mapping.initialize()
r = node.mapping.curves[0]
g = node.mapping.curves[1]
b = node.mapping.curves[2]
for i in range(ramp_size):
v = float(i) / (ramp_size - 1.0)
colors.extend([r.evaluate(v), g.evaluate(v), b.evaluate(v)])
params['color[%d] ramp' % ramp_size] = colors
elif node.bl_idname == 'ShaderNodeRGBCurve':
colors = []
node.mapping.initialize()
c = node.mapping.curves[0]
r = node.mapping.curves[1]
g = node.mapping.curves[2]
b = node.mapping.curves[3]
for i in range(ramp_size):
v = float(i) / (ramp_size - 1.0)
c_val = c.evaluate(v)
colors.extend([r.evaluate(v) * c_val, g.evaluate(v)
* c_val, b.evaluate(v) * c_val])
params['color[%d] ramp' % ramp_size] = colors
#print('doing %s %s' % (node.bl_idname, node.name))
# print(params)
ri.Pattern(mapping, get_node_name(node, mat_name), params)
# Export to rib
def shader_node_rib(ri, node, mat_name, disp_bound=0.0, portal=False):
# this is tuple telling us to convert
if type(node) == type(()):
shader, from_node, from_socket = node
input_type = 'float' if shader == 'PxrToFloat3' else 'color'
node_name = 'convert_%s.%s' % (get_node_name(
from_node, mat_name), get_socket_name(from_node, from_socket))
if from_node.bl_idname == 'ShaderNodeGroup':
node_name = 'convert_' + get_output_param_str(
from_node, mat_name, from_socket).replace(':', '.')
params = {"reference %s input" % input_type: get_output_param_str(
from_node, mat_name, from_socket)}
params['__instanceid'] = node_name
ri.Pattern(shader, node_name, params)
return
elif not hasattr(node, 'renderman_node_type'):
return translate_cycles_node(ri, node, mat_name)
params = gen_params(ri, node, mat_name)
instance = mat_name + '.' + node.name
params['__instanceid'] = instance
if 'string filename' in params:
params['string filename'] = bpy.path.abspath(params['string filename'])
if node.renderman_node_type == "pattern":
if node.bl_label == 'PxrOSL':
shader = node.plugin_name
if shader:
ri.Pattern(shader, instance, params)
else:
ri.Pattern(node.bl_label, instance, params)
elif node.renderman_node_type == "light":
light_group_name = ''
scene = bpy.context.scene
for lg in scene.renderman.light_groups:
if mat_name in lg.members.keys():
light_group_name = lg.name
break
params['string lightGroup'] = light_group_name
params['__instanceid'] = mat_name
light_name = node.bl_label
if light_name == 'PxrPortalLight':
if mat_name in bpy.data.lamps:
lamp = bpy.context.scene.objects.active
if lamp and lamp.parent and lamp.parent.type == 'LAMP' \
and lamp.parent.data.renderman.renderman_type == 'ENV':
from .export import property_group_to_params
parent_node = lamp.parent.data.renderman.get_light_node()
parent_params = property_group_to_params(parent_node)
params['string domeSpace'] = lamp.parent.name
params['string portalName'] = mat_name
params['string domeColorMap'] = parent_params['string lightColorMap']
params['float intensity'] = parent_params['float intensity'] * params['float intensityMult']
del params['float intensityMult']
params['float exposure'] = parent_params['float exposure']
params['color lightColor'] = [i*j for i,j in zip(parent_params['color lightColor'],params['color tint'])]
del params['color tint']
if not params['int enableTemperature']:
params['int enableTemperature'] = parent_params['int enableTemperature']
params['float temperature'] = parent_params['float temperature']
params['float specular'] *= parent_params['float specular']
params['float diffuse'] *= parent_params['float diffuse']
ri.Light(light_name, mat_name, params)
elif node.renderman_node_type == "lightfilter":
params['__instanceid'] = mat_name
light_name = node.bl_label
ri.LightFilter(light_name, mat_name, params)
elif node.renderman_node_type == "displacement":
ri.Attribute('displacementbound', {'sphere': disp_bound})
ri.Displace(node.bl_label, mat_name, params)
else:
ri.Bxdf(node.bl_label, instance, params)
def replace_frame_num(prop):
frame_num = bpy.data.scenes[0].frame_current
prop = prop.replace('$f4', str(frame_num).zfill(4))
prop = prop.replace('$F4', str(frame_num).zfill(4))
prop = prop.replace('$f3', str(frame_num).zfill(3))
prop = prop.replace('$F3', str(frame_num).zfill(3))
return prop
# return the output file name if this texture is to be txmade.
def get_tex_file_name(prop):
prop = replace_frame_num(prop)
prop = bpy.path.basename(prop)
part = prop.rpartition('.')
prop = part[0]
if prop != '' and part[2].lower() != 'tex':
_p_ = bpy.context.scene.renderman.path_texture_output
#
# just in case there is a leading path separator
#
_s_ = "" if _p_.endswith("/") or _p_.endswith("\\") else "/"
_f_ = "{}{}{}{}".format(_p_, _s_, prop, ".tex")
return user_path(_f_)
else:
return prop
def is_same_type(socket1, socket2):
return (type(socket1) == type(socket2)) or (is_float_type(socket1) and is_float_type(socket2)) or \
(is_float3_type(socket1) and is_float3_type(socket2))
def is_float_type(socket):
# this is a renderman node
if type(socket) == type({}):
return socket['renderman_type'] in ['int', 'float']
elif hasattr(socket.node, 'plugin_name'):
prop_meta = getattr(socket.node, 'output_meta', [
]) if socket.is_output else getattr(socket.node, 'prop_meta', [])
if socket.name in prop_meta:
return prop_meta[socket.name]['renderman_type'] in ['int', 'float']
else:
return socket.type in ['INT', 'VALUE']
def is_float3_type(socket):
# this is a renderman node
if type(socket) == type({}):
return socket['renderman_type'] in ['int', 'float']
elif hasattr(socket.node, 'plugin_name'):
prop_meta = getattr(socket.node, 'output_meta', [
]) if socket.is_output else getattr(socket.node, 'prop_meta', [])
if socket.name in prop_meta:
return prop_meta[socket.name]['renderman_type'] in ['color', 'vector', 'normal']
else:
return socket.type in ['RGBA', 'VECTOR']
# walk the tree for nodes to export
def gather_nodes(node):
nodes = []
for socket in node.inputs:
if socket.is_linked:
link = socket.links[0]
for sub_node in gather_nodes(socket.links[0].from_node):
if sub_node not in nodes:
nodes.append(sub_node)
# if this is a float -> color inset a tofloat3
if is_float_type(link.from_socket) and is_float3_type(socket):
convert_node = ('PxrToFloat3', link.from_node,
link.from_socket)
if convert_node not in nodes:
nodes.append(convert_node)
elif is_float3_type(link.from_socket) and is_float_type(socket):
convert_node = ('PxrToFloat', link.from_node, link.from_socket)
if convert_node not in nodes:
nodes.append(convert_node)
if hasattr(node, 'renderman_node_type') and node.renderman_node_type != 'output':
nodes.append(node)
elif not hasattr(node, 'renderman_node_type') and node.bl_idname not in ['ShaderNodeOutputMaterial', 'NodeGroupInput', 'NodeGroupOutput']:
nodes.append(node)
return nodes
# for an input node output all "nodes"
def export_shader_nodetree(ri, id, handle=None, disp_bound=0.0, iterate_instance=False):
if id and id.node_tree:
if is_renderman_nodetree(id):
portal = type(
id).__name__ == 'AreaLamp' and id.renderman.renderman_type == 'PORTAL'
# if id.renderman.nodetree not in bpy.data.node_groups:
# load_tree_from_lib(id)
nt = id.node_tree
if not handle:
handle = id.name
if type(id) == bpy.types.Material:
handle = get_mat_name(handle)
# if ipr we need to iterate instance num on nodes for edits
from . import engine
if engine.ipr and hasattr(id.renderman, 'instance_num'):
if iterate_instance:
id.renderman.instance_num += 1
if id.renderman.instance_num > 0:
handle += "_%d" % id.renderman.instance_num
out = next((n for n in nt.nodes if hasattr(n, 'renderman_node_type') and
n.renderman_node_type == 'output'),
None)
if out is None:
return
nodes_to_export = gather_nodes(out)
ri.ArchiveRecord('comment', "Shader Graph")
for node in nodes_to_export:
shader_node_rib(ri, node, mat_name=handle,
disp_bound=disp_bound, portal=portal)
elif find_node(id, 'ShaderNodeOutputMaterial'):
print("Error Material %s needs a RenderMan BXDF" % id.name)
def get_textures_for_node(node, matName=""):
textures = []
if hasattr(node, 'bl_idname'):
if node.bl_idname == "PxrPtexturePatternNode":
return textures
elif node.bl_idname == "PxrOSLPatternNode":
for input_name, input in node.inputs.items():
if hasattr(input, 'is_texture') and input.is_texture:
prop = input.default_value
out_file_name = get_tex_file_name(prop)
textures.append((replace_frame_num(prop), out_file_name,
['-smode', 'periodic', '-tmode',
'periodic']))
return textures
elif node.bl_idname == 'ShaderNodeGroup':
nt = node.node_tree
for node in nt.nodes:
textures.extend(get_textures_for_node(node, matName=""))
return textures
if hasattr(node, 'prop_meta'):
for prop_name, meta in node.prop_meta.items():
if prop_name in txmake_options.index:
pass
elif hasattr(node, prop_name):
prop = getattr(node, prop_name)
if meta['renderman_type'] == 'page':
continue
# else return a tuple of in name/outname
else:
if ('options' in meta and meta['options'] == 'texture') or \
(node.renderman_node_type == 'light' and
'widget' in meta and meta['widget'] == 'assetIdInput' and prop_name != 'iesProfile'):
out_file_name = get_tex_file_name(prop)
# if they don't match add this to the list
if out_file_name != prop:
if node.renderman_node_type == 'light' and \
"Dome" in node.bl_label:
# no options for now
textures.append(
(replace_frame_num(prop), out_file_name, ['-envlatl']))
else:
# Test and see if options like smode are on
# this node.
if hasattr(node, "smode"):
optionsList = []
for option in txmake_options.index:
partsOfOption = getattr(
txmake_options, option)
if partsOfOption["exportType"] == "name":
optionsList.append("-" + option)
# Float values need converting
# before they are passed to command
# line
if partsOfOption["type"] == "float":
optionsList.append(
str(getattr(node, option)))
else:
optionsList.append(
getattr(node, option))
else:
# Float values need converting
# before they are passed to command
# line
if partsOfOption["type"] == "float":
optionsList.append(
str(getattr(node, option)))
else:
optionsList.append(
"-" + getattr(node, option))
textures.append(
(replace_frame_num(prop), out_file_name, optionsList))
else:
# no options found add the bare minimum
# options for smooth export.
textures.append((replace_frame_num(prop), out_file_name,
['-smode', 'periodic',
'-tmode', 'periodic']))
return textures
def get_textures(id):
textures = []
if id is None or not id.node_tree:
return textures
nt = id.node_tree
for node in nt.nodes:
textures.extend(get_textures_for_node(node, id.name))
return textures
pattern_node_categories_map = {"texture": ["PxrFractal", "PxrBakeTexture", "PxrBakePointCloud", "PxrProjectionLayer", "PxrPtexture", "PxrTexture", "PxrVoronoise", "PxrWorley", "PxrFractalize", "PxrDirt", "PxrLayeredTexture", "PxrMultiTexture"],
"bump": ["PxrBump", "PxrNormalMap", "PxrFlakes", "aaOceanPrmanShader", 'PxrAdjustNormal'],
"color": ["PxrBlackBody", "PxrHairColor", "PxrBlend", "PxrLayeredBlend", "PxrClamp", "PxrExposure", "PxrGamma", "PxrHSL", "PxrInvert", "PxrMix", "PxrProjectionStack", "PxrRamp", "PxrRemap", "PxrThinFilm", "PxrThreshold", "PxrVary", "PxrChecker", "PxrColorCorrect"],
"manifold": ["PxrManifold2D", "PxrRandomTextureManifold", "PxrManifold3D", "PxrManifold3DN", "PxrProjector", "PxrRoundCube", "PxrBumpManifold2D", "PxrTileManifold"],
"geometry": ["PxrDot", "PxrCross", "PxrFacingRatio", "PxrTangentField"],
"script": ["PxrOSL", "PxrSeExpr"],
"utility": ["PxrAttribute", "PxrGeometricAOVs", "PxrMatteID", "PxrPrimvar", "PxrShadedSide", "PxrTee", "PxrToFloat", "PxrToFloat3", "PxrVariable"],
"displace": ["PxrDispScalarLayer", 'PxrDispTransform', 'PxrDispVectorLayer'],
"layer": ['PxrLayer', 'PxrLayerMixer']}
# Node Chatagorization List
def GetPatternCategory(name):
for cat_name, node_names in pattern_node_categories_map.items():
if name in node_names:
return cat_name
else:
return 'deprecated'
# our own base class with an appropriate poll function,
# so the categories only show up in our own tree type
class RendermanPatternNodeCategory(NodeCategory):
@classmethod
def poll(cls, context):
return context.space_data.tree_type == 'ShaderNodeTree'
classes = [
RendermanShaderSocket,
RendermanNodeSocketColor,
RendermanNodeSocketFloat,
RendermanNodeSocketInt,
RendermanNodeSocketString,
RendermanNodeSocketVector,
RendermanNodeSocketStruct,
]
nodetypes = {}
pattern_categories = {}
def register():
for cls in classes:
bpy.utils.register_class(cls)
preferences = bpy.context.preferences
prefs = preferences.addons[__package__].preferences
categories = {}
for name, arg_file in args_files_in_path(prefs, None).items():
try:
vals = generate_node_type(prefs, name, ET.parse(arg_file).getroot())
if vals:
typename, nodetype = vals
nodetypes[typename] = nodetype
except Exception:
print("Error parsing " + name)
traceback.print_exc()
node_cats = {
'bxdf': ('RenderMan Bxdfs', []),
'light': ('RenderMan Lights', []),
'patterns_texture': ('RenderMan Texture Patterns', []),
'patterns_bump': ('RenderMan Bump Patterns', []),
'patterns_color': ('RenderMan Color Patterns', []),
'patterns_manifold': ('RenderMan Manifold Patterns', []),
'patterns_geometry': ('RenderMan Geometry Patterns', []),
'patterns_utility': ('RenderMan Utility Patterns', []),
'patterns_script': ('RenderMan Script Patterns', []),
'patterns_displace': ('RenderMan Displacement Patterns', []),
'patterns_layer': ('RenderMan Layers', []),
'displacement': ('RenderMan Displacements', [])
}
for name, node_type in nodetypes.items():
node_item = NodeItem(name, label=node_type.bl_label)
if node_type.renderman_node_type == 'pattern':
# insert pxr layer in bxdf
pattern_cat = GetPatternCategory(node_type.bl_label)
if pattern_cat == 'deprecated':
continue
node_cat = 'patterns_' + pattern_cat
node_cats[node_cat][1].append(node_item)
pattern_cat = pattern_cat.capitalize()
if pattern_cat not in pattern_categories:
pattern_categories[pattern_cat] = {}
pattern_categories[pattern_cat][name] = node_type
elif 'LM' in name and node_type.renderman_node_type == 'bxdf':
# skip LM materials
continue
elif node_type.renderman_node_type == 'light' and 'PxrMeshLight' not in name:
# skip light nodes
continue
else:
node_cats[node_type.renderman_node_type][1].append(node_item)
# all categories in a list
node_categories = [
# identifier, label, items list
RendermanPatternNodeCategory("PRMan_output_nodes", "RenderMan Outputs",
items=[NodeItem('RendermanOutputNode', label=RendermanOutputNode.bl_label)]),
]
for name, (desc, items) in node_cats.items():
node_categories.append(RendermanPatternNodeCategory(name, desc,
items=sorted(items,
key=attrgetter('_label'))))
nodeitems_utils.register_node_categories("RENDERMANSHADERNODES",
node_categories)
def unregister():
nodeitems_utils.unregister_node_categories("RENDERMANSHADERNODES")
# bpy.utils.unregister_module(__name__)
for cls in classes:
bpy.utils.unregister_class(cls)
|
nilq/baby-python
|
python
|
import logging
import warnings
from analyzer import Analyzer
log = logging.getLogger(__name__)
class URLParsing:
def __init__(self, url_link):
self.url_link = url_link
self.tittle = None
self.keywords = None
log.info("set new url {}".format(self.url_link))
self.parsing()
def parsing(self):
"""
This method makes use of a parser to consult the
URL and in case of being able to access, obtain the
HTML text and access the configured information
"""
if Analyzer.check_url(self.url_link):
info = Analyzer.find_elements(self.url_link,['keywords'])
self.tittle = info.get("tittle",None)
self.keywords = info.get("keywords",None)
def __parsing_keywords(self, info):
"""
This method summarizes the keywords and
returns a dict that contains the keyword
and the amount for each one as a key.
Param: text wiht keyword
Return: {'keyword_n':'n'}
"""
keywords = {}
if info is not None:
for key in info.split(" "):
cls_key = key.replace(",","").lower()
if keywords.get(cls_key,None) is None:
keywords[cls_key] = 1
else:
temp_count = keywords.get(cls_key)
keywords[cls_key] = temp_count + 1
return keywords
def get_keywords(self):
return {"keywords":self.keywords,"url":self.url_link} if self.keywords is not None else None
def get_tittle(self):
return {"tittle":self.tittle,"url":self.url_link} if self.tittle is not None else None
def unique_kw(self):
kw = self.__parsing_keywords(self.keywords)
return {"keyword":[x.strip() for x in kw.keys()]}
def kw_in_tittle(self):
kw = [x.replace(",","").lower() for x in self.keywords.split(" ")]
kw_tittle = [x.lower() for x in self.__parsing_keywords(self.tittle).keys()]
total = [x for x in kw if x in kw_tittle]
return {"total_kewords":len(total),"keywords":total,"tittle": self.tittle}
def frequency_kw(self):
return self.__parsing_keywords(self.keywords)
|
nilq/baby-python
|
python
|
"""
Provides the Vault class for secure variable storage.
"""
import base64
import getpass
import json
import os
import subprocess
import tempfile
from simple_automation.vars import Vars
from simple_automation.exceptions import LogicError, MessageError
from simple_automation.utils import choice_yes
class Vault(Vars):
"""
A base-class for vaults.
Parameters
----------
manager : Manager
The manager to which this vault is registered.
file : str
The file which serves as the permanent storage.
"""
def __init__(self, manager, file: str):
super().__init__()
self.manager = manager
self.file = file
def decrypt_content(self, ciphertext: bytes) -> bytes:
"""
Decrypts the given ciphertext. Should be implemented by subclasses.
Parameters
----------
ciphertext : bytes
The bytes to decrypt.
Returns
-------
bytes
The plaintext
"""
raise NotImplementedError("Must be overwritten by subclass.")
def encrypt_content(self, plaintext: bytes) -> bytes:
"""
Encrypts the given plaintext. Should be implemented by subclasses.
Parameters
----------
plaintext : bytes
The bytes to encrypt.
Returns
-------
bytes
The ciphertext
"""
raise NotImplementedError("Must be overwritten by subclass.")
def decrypt(self):
"""
Decrypts the vault (using self.decrypt_content) and loads the content into our Vars.
"""
try:
with open(self.file, 'rb') as f:
self.vars = json.loads(self.decrypt_content(base64.decodebytes(f.read())))
except FileNotFoundError:
if self.manager.edit_vault is None:
print(f"[1;33mwarning:[m [1mLoaded nonexistent vault '{self.file}': [mTo create the file, use --edit-vault")
def encrypt(self) -> bytes:
"""
Encrypts the currently stored Vars (using self.encrypt_content) and overwrites the vault file.
"""
content = base64.encodebytes(self.encrypt_content(json.dumps(self.vars).encode('utf-8')))
with open(self.file, 'wb') as f:
f.write(content)
def edit(self):
"""
Opens an $EDITOR containing the loaded content as a pretty printed json,
and updates the internal representation as well as the original vault file,
if the content changed after the editor exists.
"""
editor = os.environ.get('EDITOR')
if editor is None:
raise RuntimeError("Cannot edit vault: $EDITOR is not set!")
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
# Set up temporary file
tf.write(json.dumps(self.vars, sort_keys=True, indent=4).encode('utf-8'))
tf.flush()
while True:
# Run editor
p = subprocess.run([editor, tf.name], check=False)
if p.returncode != 0:
raise RuntimeError(f"Aborting vault edit: $EDITOR exited with status {p.returncode}")
# Seek to beginning of file and load content
tf.seek(0)
try:
self.vars = json.load(tf)
break
except json.decoder.JSONDecodeError as e:
print(f"[1;31merror:[m {str(e)}")
if not choice_yes("Invalid json! Reopen editor?"):
# Abort without saving.
print("Editing aborted. Changes have been discarded.")
return
# Save vault
self.encrypt()
class SymmetricVault(Vault):
"""
A SymmetricVault is a Vault which saves its context symmetrically encrypted.
Content is encrypted with a salted key (+scrypt) using AES-256-GCM.
Initializes the vault from the given file and key/keyfile.
If neither key nor keyfile is provided, the key will be read via getpass().
The key may be given as str or bytes. If the key is given a a str,
it will automatically be converted to bytes (without encoding) before usage.
Parameters
----------
manager : Manager
The manager to which this vault is registered.
file : str
The file which serves as the permanent storage.
keyfile : str, optional
A file which contains the decryption key. Defaults to None.
key : str, optional
The decryption key. Defaults to None.
"""
def __init__(self, manager, file: str, keyfile=None, key=None):
super().__init__(manager, file)
self.keyfile = keyfile
self.key = key
def get_key(self):
"""
Loads the decryption key.
"""
# Get key from keyfile / ask for pass
if self.key is None:
if self.keyfile is None:
# Ask for key
self.key = getpass.getpass(f"Password for vault '{self.file}': ")
else:
with open(self.keyfile, 'rb') as f:
self.key = f.read()
# Change key to bytes, if it's a str
if isinstance(self.key, str):
# Latin1 is a str <-> bytes no-op (see https://stackoverflow.com/questions/42795042/how-to-cast-a-string-to-bytes-without-encoding)
self.key = self.key.encode('latin1')
def kdf(self, salt):
"""
Derives the actual aeskey from a given salt and the saved key.
"""
# pylint: disable=C0415
from Crypto.Protocol.KDF import scrypt
return scrypt(self.key, salt, key_len=32, N=2**17, r=8, p=1)
def decrypt_content(self, ciphertext: bytes) -> bytes:
"""
Decrypts the given ciphertext.
Parameters
----------
ciphertext : bytes
The bytes to decrypt.
Returns
-------
bytes
The plaintext
"""
self.get_key()
# pylint: disable=C0415
from Crypto.Cipher import AES
# Split ciphertext into raw input parts
salt = ciphertext[:32]
nonce = ciphertext[32:48]
aes_ciphertext = ciphertext[48:-16]
tag = ciphertext[-16:]
# Derive aeskey and decrypt ciphertext
aeskey = self.kdf(salt)
cipher = AES.new(aeskey, AES.MODE_GCM, nonce=nonce)
plaintext = cipher.decrypt(aes_ciphertext)
# Verify encrypted file was not tampered with
try:
cipher.verify(tag)
except ValueError as e:
# If we get a ValueError, there was an error when decrypting so delete the file we created
raise MessageError(f"Refusing decrypted data from '{self.file}', because content verification failed! Your file might have been tampered with!") from e
return plaintext
def encrypt_content(self, plaintext: bytes) -> bytes:
"""
Encrypts the given plaintext.
Parameters
----------
plaintext : bytes
The bytes to encrypt.
Returns
-------
bytes
The ciphertext
"""
# pylint: disable=C0415
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
salt = get_random_bytes(32)
# Derive aeskey and encrypt plaintext
aeskey = self.kdf(salt)
cipher = AES.new(aeskey, AES.MODE_GCM)
aes_ciphertext = cipher.encrypt(plaintext)
tag = cipher.digest()
# Return salt, nonce, AES ciphertext and verification tag
return salt + cipher.nonce + aes_ciphertext + tag
class GpgVault(Vault):
"""
A GpgVault is a Vault which saves its context encrypted with gpg.
This can be convenient if you e.g. use a YubiKey or similar hardware
to store your encryption keys.
Initializes the gpg encrypted vault from the given file and recipient.
Parameters
----------
manager : Manager
The manager to which this vault is registered.
file : str
The file which serves as the permanent storage.
recipient: str
Only needed for encryption (when editing). Reflects the gpg
command line parameter '--recipient'. If you don't plan on using
the editing feature, the recipient may be set to None.
"""
def __init__(self, manager, file: str, recipient: str):
super().__init__(manager, file)
self.recipient = recipient
def decrypt_content(self, ciphertext: bytes) -> bytes:
"""
Decrypts the given ciphertext.
Parameters
----------
ciphertext : bytes
The bytes to decrypt.
Returns
-------
bytes
The plaintext
"""
print(f"Decrypting gpg vault '{self.file}'")
return subprocess.run(["gpg", "--quiet", "--decrypt"], input=ciphertext, capture_output=True, check=True).stdout
def encrypt_content(self, plaintext: bytes) -> bytes:
"""
Encrypts the given plaintext.
Parameters
----------
plaintext : bytes
The bytes to encrypt.
Returns
-------
bytes
The ciphertext
"""
if self.recipient is None:
raise LogicError("GpgVault encryption requires a recipient")
return subprocess.run(["gpg", "--quiet", "--encrypt", "--recipient", self.recipient], input=plaintext, capture_output=True, check=True).stdout
|
nilq/baby-python
|
python
|
from .__version__ import __title__, __description__, __url__, __version__
from .__version__ import __author__, __author_email__, __license__
from .__version__ import __copyright__
from .config import Config
from .neuralnet import NeuralNetwork
from .layers import activation, affine, batch_norm, convolution, dropout
from .layers import layer, pooling
from .training import back_propagation, learning_curve, loss_function
from .training import optimizer, weight_initialization
from .utils import cnn_utils, nn_utils, numcupy
from .exception import DNNetIOError, DNNetRuntimeError
|
nilq/baby-python
|
python
|
try:
from ulab import numpy as np
except:
import numpy as np
dtypes = (np.uint8, np.int8, np.uint16, np.int16, np.float)
print(np.ones(3))
print(np.ones((3,3)))
print(np.eye(3))
print(np.eye(3, M=4))
print(np.eye(3, M=4, k=0))
print(np.eye(3, M=4, k=-1))
print(np.eye(3, M=4, k=-2))
print(np.eye(3, M=4, k=-3))
print(np.eye(3, M=4, k=1))
print(np.eye(3, M=4, k=2))
print(np.eye(3, M=4, k=3))
print(np.eye(4, M=4))
print(np.eye(4, M=3, k=0))
print(np.eye(4, M=3, k=-1))
print(np.eye(4, M=3, k=-2))
print(np.eye(4, M=3, k=-3))
print(np.eye(4, M=3, k=1))
print(np.eye(4, M=3, k=2))
print(np.eye(4, M=3, k=3))
for dtype in dtypes:
print(np.eye(3, dtype=dtype))
|
nilq/baby-python
|
python
|
from .imdb import AS, IMDb # NOQA
__version__ = "1.0.22"
|
nilq/baby-python
|
python
|
from AppKit import *
from PyObjCTools.TestSupport import *
class TestNSUserInterfaceValidationHelper (NSObject):
def action(self): return 1
def tag(self): return 1
def validateUserInterfaceItem_(self, a): return 1
class TestNSUserInterfaceValidation (TestCase):
def testProtocols(self):
self.assertResultHasType(TestNSUserInterfaceValidationHelper.action, objc._C_SEL)
self.assertResultHasType(TestNSUserInterfaceValidationHelper.tag, objc._C_NSInteger)
self.assertResultIsBOOL(TestNSUserInterfaceValidationHelper.validateUserInterfaceItem_)
if __name__ == "__main__":
main()
|
nilq/baby-python
|
python
|
import PyPDF2
import pytesseract
from pdf2image import convert_from_path
from src.plataform import data_dir_scan as dds
class pdf_extract:
@staticmethod
def get_text_pypdf2(filename):
pdf_reader = PyPDF2.PdfFileReader(pdf_extract.__path_from_filename(filename))
pdf_text = ''
for i in range(pdf_reader.numPages):
pdf_page = pdf_reader.getPage(i)
pdf_text += pdf_page.extractText()
return pdf_text
@staticmethod
def __path_from_filename(filename):
return dds.data_dir_scan.get_data_directory() + filename
@staticmethod
def get_text_pytesseract(filename, custom_config_extact_lib):
pages = convert_from_path(pdf_extract.__path_from_filename(filename), 500)
config_to_speed_up = '-c tessedit_do_invert=0'
config = config_to_speed_up + ' ' + custom_config_extact_lib
pdf_text = ''
for page in pages:
pdf_text = pdf_text + ' ' + pytesseract.image_to_string(page, lang='por', config=config)
return pdf_text
|
nilq/baby-python
|
python
|
"""
Simple water flow example using ANUGA: Water flowing down a channel.
It was called "steep_slope" in an old validation test.
"""
import sys
#------------------------------------------------------------------------------
# Import necessary modules
#------------------------------------------------------------------------------
import anuga
from anuga import rectangular_cross
from anuga import Inlet_operator
from anuga import Domain
from anuga import myid, finalize, distribute
Qin = 0.1
fluxin=Qin/100. #The momentum flux at the upstream boundary ( = discharge / width)
mann=0.03 # Manning's coef
bedslope=-0.1
uana= ( mann**(-2.)*abs(bedslope)*fluxin**(4./3.) )**(3./10.) # Velocity
dana= fluxin/uana # Depth
args = anuga.get_args()
alg = args.alg
verbose = args.verbose
#------------------------------------------------------------------------------
# Setup sequential computational domain
#------------------------------------------------------------------------------
if myid == 0:
points, vertices, boundary = rectangular_cross(40, 10, len1=400.0, len2=100.0)
domain = Domain(points, vertices, boundary) # Create domain
domain.set_name('channel') # Output name
domain.set_flow_algorithm(alg)
#------------------------------------------------------------------------------
# Setup initial conditions
#------------------------------------------------------------------------------
def topography(x, y):
return -x/10. # linear bed slope
def init_stage(x,y):
stg= -x/10.+0.004 # Constant depth: 10 cm.
return stg
#line0=[ [0.,0.], [0., 100.] ]
#Uin=[uana, 0.0]
#Inlet_operator(domain, line0, Q=Qin, velocity=Uin)
domain.set_quantity('elevation', topography) # Use function for elevation
domain.set_quantity('friction', mann) # Constant friction
domain.set_quantity('stage', init_stage)
domain.set_quantity('xmomentum', dana*uana)
else:
domain = None
#===========================================================================
# Create Parallel Domain
#===========================================================================
domain = distribute(domain)
#------------------------------------------------------------------------------
# Setup boundary conditions
#------------------------------------------------------------------------------
#
# This one can have outflow boundary issues -- a good approach is just to use a reflective
# boundary, and allow water to 'pool' at the bottom of the domain.
#
#Bt = anuga.Transmissive_boundary(domain)
#Bts = anuga.Transmissive_momentum_set_stage_boundary(domain, dana-160.0)
#Bts = anuga.Transmissive_n_momentum_zero_t_momentum_set_stage_boundary(domain, lambda t: dana-40.0)
##BdIN = anuga.Dirichlet_boundary([dana, fluxin, 0.0])
#BdOUT = anuga.Dirichlet_boundary([dana-40., dana*uana, 0.0])
print(dana-40.)
Br = anuga.Reflective_boundary(domain) # Solid reflective wall
domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br})
line1=[ [0.0, 0.], [0.0, 100.] ]
Qin=0.1
inlet = Inlet_operator(domain, line1, Q = Qin)
#if inlet: print inlet.statistics()
stage = domain.quantities['stage']
elev = domain.quantities['elevation']
print((stage-elev).get_integral())
#------------------------------------------------------------------------------
# Produce a documentation of parameters
#------------------------------------------------------------------------------
if myid == 0:
parameter_file=open('parameters.tex', 'w')
parameter_file.write('\\begin{verbatim}\n')
from pprint import pprint
pprint(domain.get_algorithm_parameters(),parameter_file,indent=4)
parameter_file.write('\\end{verbatim}\n')
parameter_file.close()
#------------------------------------------------------------------------------
# Evolve system through time
#------------------------------------------------------------------------------
for t in domain.evolve(yieldstep=10.0, finaltime=3000.0):
if myid == 0 and verbose: print(domain.timestepping_statistics())
#print (stage-elev).get_integral()
#print (domain.areas*(domain.quantities['stage'].centroid_values - domain.quantities['elevation'].centroid_values)).sum()
#s3 = domain.get_flow_through_cross_section([[30., 0.0], [30., 100.]])
#s4 = domain.get_flow_through_cross_section([[32., 0.0], [32., 100.]])
#s5 = domain.get_flow_through_cross_section([[34., 0.0], [34., 100.]])
#s2 = domain.get_flow_through_cross_section([[45., 0.0], [45., 100.]])
#s1 = domain.get_flow_through_cross_section([[53., 0.0], [53., 100.]])
#s0 = domain.get_flow_through_cross_section([[60., 0.0], [60., 100.]])
#print 'Xsectional flow:', s0, s1, s2, s3, s4, s5
domain.sww_merge(delete_old=True)
finalize()
|
nilq/baby-python
|
python
|
import numpy as np
def cubic_roots(a, b, c, d):
"""Compute the roots of the cubic polynomial :math:`ax^3 + bx^2 + cx + d`.
:param a: cubic coefficient
:param b: quadratic coefficient
:param c: linear coefficient
:param d: constant
:return: list of three complex roots
This function does not check if the found roots are real or complex.
"""
if (a != 0): # Case: ax^3 + bx^2 + cx + d = 0
delta_0 = b**2 - 3*a*c
delta_1 = 2*b**3 - 9*a*b*c + 27*a**2*d
C = ((delta_1 + np.sqrt(delta_1**2 - 4*delta_0**3 + 0j)) / 2)**(1/3)
zeta = -1/2 + 1j/2 * np.sqrt(3)
return [-1/(3*a) * (b + zeta**k * C + delta_0 / (zeta**k * C))
for k in [0, 1, 2]]
elif (b != 0): # Case: bx^2 + cx + d = 0
delta = c**2 - 4*b*d
return [(-c + k*np.sqrt(delta + 0j))/(2*b)
for k in [-1, 1]]
elif (c != 0): # Case: cx + d = 0
return -d/c
elif (d != 0): # Case: d != 0 (without solution)
return np.nan
else: # Case: d = 0 (with trivial solution)
return 0
|
nilq/baby-python
|
python
|
# Copyright 2017 Google Inc. 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.
"""Utility methods common to multiple commands."""
import json
import os
import subprocess
import sys
import tempfile
class InvalidInstanceException(Exception):
_MESSAGE = (
'The specified instance, {}, does not appear '
'to have been created by the `datalab` tool, and '
'so cannot be managed by it.')
def __init__(self, instance_name):
super(InvalidInstanceException, self).__init__(
InvalidInstanceException._MESSAGE.format(instance_name))
class NoSuchInstanceException(Exception):
_MESSAGE = (
'The specified instance, {}, does not exist in any zone.')
def __init__(self, instance_name):
super(NoSuchInstanceException, self).__init__(
NoSuchInstanceException._MESSAGE.format(instance_name))
def call_gcloud_quietly(args, gcloud_surface, cmd, report_errors=True):
"""Call `gcloud` and silence any output unless it fails.
Normally, the `gcloud` command line tool can output a lot of
messages that are relevant to users in general, but may not
be relevant to the way a Datalab instance is created.
For example, creating a persistent disk will result in a
message that the disk needs to be formatted before it can
be used. However, the instance we create formats the disk
if necessary, so that message is erroneous in our case.
These messages are output regardless of the `--quiet` flag.
This method allows us to avoid any confusion from those
messages by redirecting them to a temporary file.
In the case of an error in the `gcloud` invocation, we
still print the messages by reading from the temporary
file and printing its contents.
Args:
args: The Namespace returned by argparse
gcloud_surface: Function that can be used for invoking `gcloud <surface>`
cmd: The subcommand to run
report_errors: Whether or not to report errors to the user
Raises:
subprocess.CalledProcessError: If the `gcloud` command fails
"""
with tempfile.TemporaryFile() as stdout, \
tempfile.TemporaryFile() as stderr:
try:
cmd = ['--quiet'] + cmd
gcloud_surface(args, cmd, stdout=stdout, stderr=stderr)
except subprocess.CalledProcessError:
if report_errors:
stdout.seek(0)
stderr.seek(0)
print(stdout.read())
sys.stderr.write(stderr.read())
raise
stderr.seek(0)
gcloud_stderr = stderr.read()
if 'WARNING' in gcloud_stderr:
sys.stderr.write(gcloud_stderr)
return
def prompt_for_zone(args, gcloud_compute, instance=None):
"""Prompt the user to select a zone.
Args:
args: The Namespace instance returned by argparse
gcloud_compute: Function that can be used to invoke `gcloud compute`
Raises:
subprocess.CalledProcessError: If a nested `gcloud` calls fails
NoSuchInstanceException: If the user specified an instance that
does not exist in any zone.
"""
matching_zones = []
list_cmd = ['zones', '--quiet', 'list', '--format=value(name)']
if instance:
# list the zones for matching instances instea of all zones.
list_cmd = [
'instances', 'list', '--quiet', '--filter',
'name={}'.format(instance), '--format', 'value(zone)']
with tempfile.TemporaryFile() as stdout, \
open(os.devnull, 'w') as stderr:
gcloud_compute(args, list_cmd,
stdout=stdout, stderr=stderr)
stdout.seek(0)
matching_zones = stdout.read().strip().splitlines()
if len(matching_zones) == 1:
# There is only one possible zone, so just return it.
return matching_zones[0]
elif (instance and len(matching_zones) == 0):
raise NoSuchInstanceException(instance)
zone_number = 1
zone_map = {}
print('Please specify a zone from one of:')
for zone in matching_zones:
zone_map[zone_number] = zone
print(' [{}] {}'.format(zone_number, zone))
zone_number += 1
selected = raw_input('Your selected zone: ')
try:
zone_number = int(selected)
return zone_map[zone_number]
except:
if selected not in matching_zones:
print('Zone {} not recognized'.format(selected))
return prompt_for_zone(args, gcloud_compute, instance=instance)
return selected
def flatten_metadata(metadata):
"""Flatten the given API-style dictionary into a Python dictionary.
This takes a mapping of key-value pairs as returned by the Google
Compute Engine API, and converts it to a Python dictionary.
The `metadata` argument is an object that has an `items` field
containing a list of key->value mappings. Each key->value mapping
is an object with a `key` field and a `value` field.
Example:
Given the following input:
{ "items": [
{ "key": "a",
"value": 1
},
{ "key": "b",
"value": 2
},
],
"fingerprint": "<something>"
}
... this will return {"a": 1, "b": 2}
"""
items = metadata.get('items', [])
result = {}
for mapping in items:
result[mapping.get('key', '')] = mapping.get('value', '')
return result
def _check_datalab_tag(instance, tags):
"""Check that the given "tags" object contains `datalab`.
This is used to verify that a VM was created by the `datalab create`
command.
Args:
instance: The name of the instance to check
tags: An object with an 'items' field that is a list of tags.
Raises:
InvalidInstanceException: If the check fails.
"""
items = tags.get('items', [])
if 'datalab' not in items:
raise InvalidInstanceException(instance)
return
def describe_instance(args, gcloud_compute, instance):
"""Get the status and metadata of the given Google Compute Engine VM.
This will prompt the user to select a zone if necessary.
Args:
args: The Namespace instance returned by argparse
gcloud_compute: Function that can be used to invoke `gcloud compute`
instance: The name of the instance to check
Returns:
A tuple of the string describing the status of the instance
(e.g. 'RUNNING' or 'TERMINATED'), and the list of metadata items.
Raises:
subprocess.CalledProcessError: If the `gcloud` call fails
ValueError: If the result returned by gcloud is not valid JSON
InvalidInstanceException: If the instance was not created by
running `datalab create`.
NoSuchInstanceException: If the user specified an instance that
does not exist in any zone.
"""
get_cmd = ['instances', 'describe', '--quiet']
if args.zone:
get_cmd.extend(['--zone', args.zone])
get_cmd.extend(
['--format', 'json(status,tags.items,metadata.items)', instance])
with tempfile.TemporaryFile() as stdout, \
tempfile.TemporaryFile() as stderr:
try:
gcloud_compute(args, get_cmd, stdout=stdout, stderr=stderr)
stdout.seek(0)
json_result = stdout.read().strip()
status_tags_and_metadata = json.loads(json_result)
tags = status_tags_and_metadata.get('tags', {})
_check_datalab_tag(instance, tags)
status = status_tags_and_metadata.get('status', 'UNKNOWN')
metadata = status_tags_and_metadata.get('metadata', {})
return (status, flatten_metadata(metadata))
except subprocess.CalledProcessError:
if args.zone:
stderr.seek(0)
sys.stderr.write(stderr.read())
raise
else:
args.zone = prompt_for_zone(
args, gcloud_compute, instance=instance)
return describe_instance(
args, gcloud_compute, instance)
return ('UNKNOWN', [])
def maybe_prompt_for_zone(args, gcloud_compute, instance):
"""Prompt for the zone of the given VM if it is ambiguous.
This will update the args.zone flag to point to the selected zone.
Args:
args: The Namespace instance returned by argparse
gcloud_compute: Function that can be used to invoke `gcloud compute`
instance: The name of the instance to check
Raises:
subprocess.CalledProcessError: If the `gcloud` call fails
InvalidInstanceException: If the instance was not created by
running `datalab create`.
NoSuchInstanceException: If the user specified an instance that
does not exist in any zone.
"""
if not args.quiet:
describe_instance(args, gcloud_compute, instance)
return
def print_info_messages(args):
"""Return whether or not info messages should be printed.
Args:
args: The Namespace instance returned by argparse
Returns:
True iff the verbosity has been set to a level that includes
info messages.
"""
return args.verbosity in ['debug', 'info']
|
nilq/baby-python
|
python
|
from django.core.paginator import Paginator
from django.db.models import Q
from django.shortcuts import render
from django.utils import timezone
from pokefriend.forms import TrainerRegisterForms, TrainerSearchForms
from pokefriend.models import Trainer
def index(request):
register_form = TrainerRegisterForms()
search_form = TrainerSearchForms()
trainers = Trainer.objects.all()
if request.method == 'POST':
form = TrainerRegisterForms(request.POST)
if form.is_valid():
code = form.cleaned_data.get('code').replace(' ', '')
team = form.cleaned_data.get('team')
country = form.cleaned_data.get('country')
trainer = Trainer.objects.filter(code=code).first()
if trainer:
trainer.last_modified = timezone.now()
trainer.country = country
trainer.team = team
trainer.save()
else:
trainer = form.save(commit=False)
trainer.code = code
trainer.save()
trainers = Trainer.objects.all()
form = TrainerRegisterForms()
paginator = Paginator(trainers, 9)
trainer_page_object = paginator.get_page(1)
context = {
'register_form': form,
'search_form': search_form,
'trainers': trainer_page_object,
}
return render(request, 'index.html', context)
paginator = Paginator(trainers, 9)
page_number = request.GET.get('page') if request.GET.get('page') else 1
trainer_page_object = paginator.get_page(page_number)
context = {
'register_form': register_form,
'search_form': search_form,
'trainers': trainer_page_object,
}
return render(request, 'index.html', context)
def search(request):
register_form = TrainerRegisterForms()
search_form = TrainerSearchForms(request.GET)
trainers = None
query = Q()
if search_form.is_valid():
country = search_form.cleaned_data.get('country')
team = search_form.cleaned_data.get('team')
if country:
query &= Q(country=country)
if team:
if team != 'ANY':
query &= Q(team=team)
trainers = Trainer.objects.filter(query)
context = {
'register_form': register_form,
'search_form': search_form,
'trainers': trainers,
}
return render(request, 'index.html', context)
def faq(request):
return render(request, 'faq.html')
|
nilq/baby-python
|
python
|
class PBXList(list):
pass
|
nilq/baby-python
|
python
|
import collections
import logging
import random
from datetime import datetime
from typing import Iterable, Text, Dict, Any
import attr
import faust
from replay_output_experiment.app import app
from replay_output_experiment.page_views.models import BalanceUpdate, RequestTransfer, DATETIME_BASIC_FORMAT, \
balances_str
from simple_settings import settings
import asyncio
from faust.types import EventT
logger = logging.getLogger(__name__)
ACCOUNTS = ['a', 'b', 'c', 'd', 'e']
def random_requests(count=1) -> Iterable[RequestTransfer]:
while count > 0:
src = random.choice(ACCOUNTS)
dst = random.choice([a for a in ACCOUNTS if a != src])
yield RequestTransfer(src, dst, random.randint(1, 10))
count -= 1
transfers_topic = app.topic(settings.TRANSFERS_TOPIC, key_type=Text, value_type=RequestTransfer, partitions=1)
balances_topic = app.topic(settings.BALANCES_TOPIC, key_type=Text, value_type=BalanceUpdate,
# retention=, # With compaction are old messages also dropped after retention has passed?
compacting=True, partitions=1)
# Copied from tables/manager.py
replay_queue = app.FlowControlQueue(
maxsize=app.conf.stream_buffer_maxsize,
loop=asyncio.get_event_loop(),
clear_on_resume=False,
)
replay_channel = balances_topic.clone_using_queue(replay_queue)
class MyTable(faust.Table):
def update_balance(self, account: Text, quantity_delta: int, timestamp: Text) -> None:
key = f'balance/{account}'
if key in self:
new_balance = self[key].quantity + quantity_delta
else:
new_balance = quantity_delta
self[key] = BalanceUpdate(account=account, quantity=new_balance, timestamp_committed=timestamp)
#
# Overrides methods in tables/base.py:Collection
def _to_key(self, k: Any) -> Any:
print(f'Reading key {repr(k)} of type {type(k)}')
if isinstance(k, list):
assert False, 'Unexpected'
return k
def _to_value(self, v: Any) -> Any:
return v
balances_table = MyTable(app=app, name='balances', default=None, partitions=1, changelog_topic=balances_topic,
key_type=Text, value_type=BalanceUpdate)
app.tables.add(balances_table)
#@app.task()
#async def do_replay():
# print('seeking on replay channel')
# app.consumer.seek_wait({replay_channel.active_partitions, 0})
# print('Seek returned')
# async for event in replay_channel:
## event: EventT = await replay_queue.get()
# message = event.message
# print(f'Topic {message.topic} Offset {message.offset} {message.key}: {message.value}')
@attr.s(auto_attribs=True, slots=True)
class Executor:
async def execute_transfer(self, transfer: RequestTransfer):
timestamp_now = datetime.utcnow().isoformat()
balances_table.update_balance(
account=transfer.src_account,
quantity_delta=-transfer.quantity,
timestamp=timestamp_now)
balances_table.update_balance(
account=transfer.dst_account,
quantity_delta=transfer.quantity,
timestamp=timestamp_now)
print(f'Transfer of {transfer.quantity} from {transfer.src_account} to {transfer.dst_account} at {timestamp_now}')
print('Balance table: ' + balances_str(balances_table))
print(f'Transfer at {timestamp_now} finished.')
exec = Executor()
@app.timer(10)
async def producer():
for rand_req in random_requests():
await transfers_topic.send(key=datetime.utcnow().strftime(DATETIME_BASIC_FORMAT), value=rand_req)
@app.agent(transfers_topic)
async def process_transfers(transfers):
async for transfer in transfers:
await exec.execute_transfer(transfer)
|
nilq/baby-python
|
python
|
# print keypad combination
codes = [".;","abc","def","ghi","jkl","mno","pqrs","tu","vwx","yz"]
def gkpc(keypad):
if len(keypad) ==0:
return [""]
key = keypad[0]
rok = keypad[1:]
rres = gkpc(rok)
mres =[]
for word in rres:
for char in codes[int(key)]:
mres.append(char+word)
print(len(mres))
return mres
keypad = input()
print(gkpc(keypad))
|
nilq/baby-python
|
python
|
# encoding: utf8
# filename: completion.py
import logging
import transformers
from transformers import AutoConfig, AutoModel, AutoTokenizer, pipeline
from abc import ABC, abstractmethod
from functools import partial
from typing import List
from .corpus import Document
__all__ = ('AbstractCompletor', 'make_completor_loader')
def make_completor_loader(lm_opts):
"""Function make_completor_loader implements factory patter for constuction
a suitable completor builder (or loader) type.
"""
if (model_type := lm_opts.get('model_type')) is None:
logging.info('no model type is specified: assume `vocab` by default')
model_type = 'vocab'
# TODO: Use `match-case` syntax from Python 3.10.
if model_type in ('hf', 'huggingface'):
return HuggingFaceCompletorLoader(lm_opts['model_path'],
lm_opts['num_results'])
elif model_type == 'vocab':
return VocabCompletorLoader(lm_opts['vocab_path'])
else:
raise ValueError(f'Unknown language model type: {model_type}')
class AbstractCompletor(ABC):
"""Class AbstractCompletor defines interface for any comletion model used
in LSP implementation.
"""
@abstractmethod
def complete(self, doc: Document, line: int, char: int) -> List[str]:
pass
class DummyCompletor(AbstractCompletor):
"""Class DummyCompletor implements a completor model used for testing and
as a fallback interally.
"""
def complete(self, doc: Document, line: int, char: int) -> List[str]:
return []
class HuggingFaceCompletor(AbstractCompletor):
def __init__(self, model_path: str, num_results: int):
config = AutoConfig.from_pretrained(model_path)
model_class_name = config.architectures[0]
model_class = getattr(transformers, model_class_name, None)
if model_class is None:
logging.warning('failed to find model architecture %s: fallback',
model_class_name)
model_class = AutoModel
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
self.model = model_class.from_pretrained(model_path)
self.pipeline = pipeline('fill-mask',
model=self.model,
tokenizer=self.tokenizer)
self.apply = partial(self.pipeline, top_k=num_results)
def complete(self, doc: Document, line: int, char: int) -> List[str]:
prefix, suffix = doc.window(line, char)
text = ''.join([prefix, '<mask>', suffix])
suggest = [el['token_str'] for el in self.apply(text)]
return suggest
class HuggingFaceCompletorLoader:
def __init__(self, model_path: str, num_results: int):
self.completor: HuggingFaceCompletor
self.model_path = model_path
self.num_results = num_results
def load(self) -> HuggingFaceCompletor:
if not hasattr(self, 'completor'):
self.completor = HuggingFaceCompletor(self.model_path,
self.num_results)
return self.completor
class VocabCompletor(AbstractCompletor):
"""Class VocabCompletor implements completion logic based on predefined
vocabulary.
:param vocab: List of words.
"""
def __init__(self, vocab: List[str]):
self.vocab = vocab
def complete(self, doc: Document, line: int, char: int) -> List[str]:
return self.vocab
class VocabCompletorLoader:
"""Class VocabCompletorLoader is an loader object which loads from
filesystem and initialises completor. This loader class is a caching one.
:param vocab_path: Path to vocabulary file.
"""
def __init__(self, vocab_path):
self.completor: AbstractCompletor
self.vocab_path = vocab_path
def load(self) -> AbstractCompletor:
if not hasattr(self, 'completor'):
with open(self.vocab_path) as fin:
vocab = fin.read().splitlines()
self.completor = VocabCompletor(vocab)
return self.completor
|
nilq/baby-python
|
python
|
number_1 = float(input('Type a number: '))
number_2 = float(input('Type another one: '))
number_3 = float(input('Type the last one, please: '))
if number_1 > number_2 and number_1 > number_3:
if number_2 > number_3:
print(f'{number_1} + {number_2} = {number_1 + number_2}')
else:
print(f'{number_1} + {number_3} = {number_1 + number_3}')
elif number_2 > number_3 and number_2 > number_1:
if number_1 > number_3:
print(f'{number_2} + {number_1} = {number_2 + number_1}')
else:
print(f'{number_2} + {number_3} = {number_3 + number_2}')
else:
if number_1 > number_2:
print(f'{number_3} + {number_1} = {number_3 + number_1}')
else:
print(f'{number_3} + {number_2} = {number_2 + number_3}')
|
nilq/baby-python
|
python
|
# -*- encoding: utf-8 -*-
import os
import click
### set environment variable
# os.environ['FLASK_CONFIGURATION'] = "default" # "testing" / "production"
### change environment var to "production" for debugging
# os.environ['FLASK_CONFIGURATION'] = "production"
# from app import app
debug = True
@click.command()
@click.option('--mode', default="default", nargs=1, help="The <mode> you need to run the app : default, testing, preprod, production" )
@click.option('--host', default="localhost", nargs=1, help="The <host> name you want the app to run on : <IP_NUMBER> " )
@click.option('--port', default="8100", nargs=1, help="The <port> number you want the app to run on : <PORT_NUMBER>")
@click.option('--https', default="false", nargs=1, help="The <https> mode you want the app to run on : true | false")
def app_runner(mode, host, port, https) :
"""
app_runner
"""
print "= "*50
print "= = = RERUN FLASK APP FROM APP RUNNER = = ="
print "= "*50
### WARNING : CLIck will treat every input as string as defaults values are string too
print "\n=== CUSTOM CONFIG FROM CLI ===\n"
print "=== mode : ", mode
print "=== host : ", host
print "=== port : ", port
print "=== https : ", https
if https == "true" :
http_mode = "https"
else :
http_mode = "http"
### apply / overwrites host configuration
if mode != "default" :
print "=== mode : ", mode
os.environ["FLASK_CONFIGURATION"] = str(mode)
config_name = os.getenv('FLASK_CONFIGURATION', 'default') ### 'default' for local dev
print "=== config_name : ", config_name
### SET UP ENV VARS FROM CLI
os.environ["DOMAIN_ROOT"] = host
os.environ["DOMAIN_PORT"] = port
os.environ["SERVER_NAME"] = host + ":" + port
os.environ["DOMAIN_NAME"] = http_mode + "://" + host + ":" + port
### create app by importing app.__init__
from app import app
### simple flask runner
app.run( debug=debug, host=host, port=port, threaded=True )
if __name__ == '__main__':
"""
runner for the CIS front Flask app
- gets most of its variables at start from environment variables
-
in command line just type :
"python run_apiviz.py"
"""
app_runner()
|
nilq/baby-python
|
python
|
import sys
import unittest
import testing.postgresql
import psycopg2
import mock
from mock import patch
from eutils import Client
sys.path.append("..")
import crud
import dblib
def handler(postgresql):
with open('data/srss.sql', 'r') as myfile:
data = myfile.read().replace('\n', '')
conn = psycopg2.connect(**postgresql.dsn())
cursor = conn.cursor()
cursor.execute(data)
conn.commit()
for user in ['testuser_1', 'testuser_2']:
cursor.execute("INSERT INTO users(user_name, nickname) VALUES (%s,%s);", (user, user))
conn.commit()
cursor.execute("INSERT INTO USERS(user_name, nickname, id) VALUES (%s, %s, %s);",
('cochranebot', 'cochranebot', 17))
conn.commit()
conn.close()
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True,
on_initialized=handler)
class TestCrud(unittest.TestCase):
def mock_conn(self, VERBOSE):
return psycopg2.connect(**self.postgresql.dsn())
def setUp(self):
self.postgresql = Postgresql()
self.patcher1 = patch.object(dblib, 'create_con', self.mock_conn)
self.MockClass1 = self.patcher1.start()
def test_pubmedarticle_to_db(self):
ec = Client()
ids = [28616955,28800192,28797191]
for id in ids:
self.assertIsNone(crud.review_medtadata_db(id))
article = ec.efetch(db='pubmed',id=ids)
for i,a in enumerate(article):
crud.pubmedarticle_to_db(a,'systematic_reviews')
self.assertIsNotNone(crud.review_medtadata_db(ids[i]))
self.assertEqual(crud.review_medtadata_db(ids[i])['title'], a.title)
self.assertEqual(crud.review_medtadata_db(ids[i])['review_id'], int(a.pmid))
self.assertEqual(crud.review_medtadata_db(ids[i])['abstract'], a.abstract)
self.assertEqual(crud.review_medtadata_db(ids[i])['source'], a.jrnl)
self.assertEqual(crud.review_medtadata_db(ids[i])['doi'], a.doi)
self.assertEqual(crud.review_medtadata_db(ids[i])['publish_date'], int(a.year))
self.assertEqual(crud.review_medtadata_db(ids[i])['authors'], ', '.join(a.authors))
self.assertEqual(crud.review_medtadata_db(ids[i])['included_complete'], False)
self.assertEqual(crud.review_medtadata_db(ids[i])['verified_review'], None)
def test_review_lock_status(self):
ec = Client()
ids = [28616955,28800192,28797191]
for id in ids:
self.assertIsNone(crud.review_medtadata_db(id))
article = ec.efetch(db='pubmed', id=ids)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
self.assertEqual(crud.review_lock_status(ids[i]), False)
crud.complete_studies(ids[i],True)
self.assertEqual(crud.review_lock_status(ids[i]), True)
crud.complete_studies(ids[i],False)
self.assertEqual(crud.review_lock_status(ids[i]), False)
def test_get_locked(self):
ec = Client()
ids = [28569363,29202845,28933578]
for id in ids:
self.assertIsNone(crud.review_medtadata_db(id))
article = ec.efetch(db='pubmed', id=ids)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
self.assertIsNone(crud.get_locked())
crud.complete_studies(ids[0],True)
self.assertEqual(crud.get_locked(),[ids[0]])
crud.complete_studies(ids[1], True)
self.assertEqual(crud.get_locked(), [ids[0],ids[1]])
crud.complete_studies(ids[2], True)
self.assertEqual(crud.get_locked(), [ids[0],ids[1],ids[2]])
crud.complete_studies(ids[1], False)
self.assertEqual(crud.get_locked(), [ids[0],ids[2]])
def test_review_publication(self):
ec = Client()
trialpub_ids = [29871025,29859785,29866619]
review_ids= [28775712,28549125,29929949]
trialpubs = ec.efetch(db='pubmed', id=trialpub_ids)
reviews = ec.efetch(db='pubmed', id=review_ids)
for i, a in enumerate(trialpubs):
crud.pubmedarticle_to_db(a, 'trial_publications')
for i, a in enumerate(reviews):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_publication(a.pmid,trialpub_ids[i],1)
conn= self.mock_conn(True)
cur = conn.cursor()
cur.execute("SELECT trialpub_id from review_trialpubs where review_id = %s;",(a.pmid,))
trialpub = cur.fetchone()
self.assertEqual(trialpub[0], trialpub_ids[i])
conn.close()
def test_update_record(self):
nct_ids=['NCT02317328','NCT02317874','NCT02317887','NCT02330055']
for id in nct_ids:
xml = crud.get_trial_xml(id)
crud.update_record(xml)
conn = self.mock_conn(True)
cur = conn.cursor()
cur.execute("SELECT nct_id from tregistry_entries where nct_id in %s;",(tuple(nct_ids),))
res = cur.fetchall()
self.assertEqual(list(zip(*res)[0]), nct_ids)
conn.close()
def test_pulication_trial(self):
ec = Client()
trialpub_ids = [29871025, 29859785, 29866619]
nct_ids=['NCT02317328','NCT02317874','NCT02317887','NCT02330055']
trialpubs = ec.efetch(db='pubmed', id=trialpub_ids)
for i, a in enumerate(trialpubs):
crud.pubmedarticle_to_db(a, 'trial_publications')
self.assertIsNone(crud.linked_nctids(a.pmid))
for nct_id in nct_ids:
crud.publication_trial(a.pmid,nct_id,2)
self.assertEqual(crud.linked_nctids(a.pmid), nct_ids)
def test_add_trial_to_locked(self):
ec = Client()
ids = [28616955, 28800192, 28797191]
nct_ids=['NCT00195624','NCT00200889','NCT00207688']
test_nct = 'NCT00695409'
for id in ids:
self.assertIsNone(crud.review_medtadata_db(id))
article = ec.efetch(db='pubmed', id=ids)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_trial(ids[i],nct_ids[i],False, 'included','testuser_1',1,'up')
crud.complete_studies(ids[i],True)
crud.review_trial(ids[i],test_nct,False, 'included','testuser_1',1,'up')
self.assertIsNone(crud.check_existing_review_trial(ids[i],test_nct))
crud.complete_studies(ids[i],False)
crud.review_trial(ids[i],test_nct,False, 'included','testuser_1',1,'up')
self.assertIsNotNone(crud.check_existing_review_trial(ids[i],test_nct))
def test_review_trial(self):
ec = Client()
id = 28616955
nct_ids=['NCT00195624','NCT00200889','NCT00207688']
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
self.assertEqual(len(crud.get_review_trials_fast(id)['reg_trials']), 0)
# trial is inserted with correct values
crud.review_trial(id, nct_ids[0],False,'relevant','testuser_1',1)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[0]:
self.assertEqual(trials[i]['nct_id'], nct_ids[0])
self.assertEqual(trials[i]['upvotes'], 1)
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['voters'], 'testuser_1')
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'relevant')
# when the trial is added again by another user, it should recieve an upvote
crud.review_trial(id, nct_ids[0],False,'relevant','testuser_2',2)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[0]:
self.assertEqual(trials[i]['nct_id'], nct_ids[0])
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'relevant')
# adding an existing trial from the relevant column as included will move it
crud.review_trial(id, nct_ids[0], False, 'included', 'testuser_2', 2)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[0]:
self.assertEqual(trials[i]['nct_id'], nct_ids[0])
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'included')
# test included trial
crud.review_trial(id, nct_ids[1],False,'included','testuser_2',2)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[1]:
self.assertEqual(trials[i]['nct_id'], nct_ids[1])
self.assertEqual(trials[i]['upvotes'], 1)
self.assertEqual(trials[i]['voters'], 'testuser_2')
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'included')
# trying to insert a relevant trial when it's already included will give a vote but not move the trial
crud.review_trial(id, nct_ids[1],False,'relevant','testuser_1',1)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[1]:
self.assertEqual(trials[i]['nct_id'], nct_ids[1])
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'included')
# except for user_id 17 which can move included to relevant
crud.review_trial(id, nct_ids[1],False,'relevant','cochranebot',17, vote_type='down')
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[1]:
self.assertEqual(trials[i]['nct_id'], nct_ids[1])
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'cochranebot', 'testuser_1', 'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 1)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'relevant')
# if the review is locked and the trial is included, allow a vote
crud.review_trial(id, nct_ids[2],False,'included','testuser_1',1)
crud.complete_studies(id,True)
crud.review_trial(id, nct_ids[2],False,'included','testuser_2',2)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_ids[2]:
self.assertEqual(trials[i]['nct_id'], nct_ids[2])
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_1', 'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], True)
self.assertEqual(trials[i]['relationship'], 'included')
def test_change_relationship(self):
ec = Client()
id = 28934560
nct_id = 'NCT00678431'
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_trial(id, nct_id, False,'relevant','testuser_2',2)
link_id = crud.check_existing_review_trial(id,nct_id)
crud.change_relationship(link_id[0],'included')
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_id:
self.assertEqual(trials[i]['nct_id'], nct_id)
self.assertEqual(trials[i]['upvotes'], 1)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_2'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'included')
def test_check_existing_review_trial(self):
ec = Client()
id = 28934560
nct_id = 'NCT00678431'
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_trial(id, nct_id, False, 'relevant', 'testuser_2', 2)
link = crud.check_existing_review_trial(id, nct_id)
self.assertIsNotNone(link)
no_link = crud.check_existing_review_trial(5464824, 'NCT00000000')
self.assertIsNone(no_link)
def test_get_link_id(self):
ec = Client()
id = 28934560
nct_id = 'NCT00678431'
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_trial(id, nct_id, False, 'relevant', 'testuser_2', 2)
link_id = crud.get_link_id(nct_id,id)
self.assertIsNotNone(link_id)
no_link = crud.get_link_id('NCT02064179',28931939)
self.assertIsNone(no_link)
def test_vote(self):
ec = Client()
id = 28934560
nct_id = 'NCT00678431'
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
crud.review_trial(id, nct_id, False, 'relevant', 'testuser_2', 2)
link_id = crud.get_link_id(nct_id,id)
crud.vote(link_id,'up',1)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] == nct_id:
self.assertEqual(trials[i]['nct_id'], nct_id)
self.assertEqual(trials[i]['upvotes'], 2)
self.assertEqual(set(trials[i]['voters'].split(', ')), {'testuser_2','testuser_1'})
self.assertEqual(trials[i]['downvotes'], 0)
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'relevant')
def test_convert_id(self):
ec = Client()
id = 28795402
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
self.assertEqual(crud.convert_id(id,'doi'),'10.1002/ijc.30922')
self.assertEqual(crud.convert_id('10.1002/ijc.30922','pmid'),id)
article = ec.efetch(db='pubmed', id=24829965)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
self.assertEqual(crud.convert_id(24829965, 'doi'), None)
def test_complete_studies(self):
ec = Client()
id = 28795402
ncts = ['NCT00031265', 'NCT02199847', 'NCT00902980', 'NCT01266824', 'NCT03418909']
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
for n in ncts[:3]:
crud.review_trial(id, n, False, 'included', 'testuser_1', 1)
for n in ncts[3:]:
crud.review_trial(id, n, False, 'relevant', 'testuser_1', 1)
crud.complete_studies(id, True)
metadata = crud.review_medtadata_db(id)
self.assertEqual(metadata['included_complete'], True)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] in ncts[:3]:
self.assertEqual(trials[i]['verified'], True)
self.assertEqual(trials[i]['relationship'], 'included')
if t['nct_id'] in ncts[3:]:
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'relevant')
crud.complete_studies(id, False)
trials = crud.get_review_trials_fast(id)['reg_trials']
for i, t in enumerate(trials):
if t['nct_id'] in ncts[:3]:
self.assertEqual(trials[i]['verified'], False)
self.assertEqual(trials[i]['relationship'], 'included')
def test_get_review_trials_fast(self):
ec = Client()
id = 28795402
ncts = ['NCT00031265', 'NCT02199847', 'NCT00902980', 'NCT01266824', 'NCT03418909']
article = ec.efetch(db='pubmed', id=id)
for i, a in enumerate(article):
crud.pubmedarticle_to_db(a, 'systematic_reviews')
for n in ncts:
crud.review_trial(id, n, False, 'included', 'testuser_1', 1)
trials = crud.get_review_trials_fast(id)['reg_trials']
retrieved_ncts = [t['nct_id'] for t in trials]
for n in ncts:
self.assertTrue(n in retrieved_ncts)
def tearDown(self):
self.patcher1.stop()
self.postgresql.stop()
if __name__ == '__main__':
unittest.main()
|
nilq/baby-python
|
python
|
import datetime as dt
def getDatefromDate(date,delta,strfmt='%Y%m%d'):
if type(date)==str:
date=stringToDate(date,strfmt)
return (date + dt.timedelta(delta)).strftime(strfmt)
def getDateFromToday(delta,strfmt='%Y%m%d'):
""" Returns a string that represents a date n numbers of days from today.
Parameters:
-----------
delta : int
number of days
strfmt : string
format in which the date will be represented
"""
return (dt.date.today() + dt.timedelta(delta)).strftime(strfmt)
def stringToDate(stringDate,strfmt='%Y%m%d'):
""" Converts a string format date into datetime
Parameters:
-----------
stringDate : string
date in string format
strfmt : string
format in which the input date is represented
"""
return dt.datetime.strptime(stringDate,strfmt).date()
def intToDate(intDate):
""" Converts an int format date into datetime
Parameters:
-----------
intDate : int
date in int format
Example:
intDate(20151023)
"""
return stringToDate(str(intDate))
def dateToInt(date,strfmt='%Y%m%d'):
""" Converts a datetime date into int
Parameters:
-----------
date : datetime
date in datetime format
strfmt : string
format in which the int date will be generated
Example:
dateToInt(dt.date(2015,10,23),'%Y')
"""
return int(date.strftime(strfmt))
def dateToString(date,strfmt='%Y%m%d'):
return dt.datetime.strftime(date,strfmt)
def stringToString(date,from_strfmt='%d%b%y',to_strfmt='%Y%m%d'):
return dt.datetime.strftime(stringToDate(date,from_strfmt),to_strfmt)
|
nilq/baby-python
|
python
|
from M2Crypto.EVP import Cipher
from M2Crypto.Rand import rand_bytes
class TestRule3c:
def __init__(self):
self.g_encrypt = 1
self.g_decrypt = 0
self.g_key1 = b"12345678123456781234567812345678"
self.g_key2 = bytes("12345678123456781234567812345678", "utf8")
self.g_iv = b"0000000000000000"
def p_example1_hard_coded1(self, data):
cipher = Cipher("aes_256_ecb", b"12345678123456781234567812345678", self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example2_hard_coded2(self, data):
cipher = Cipher("aes_256_ecb", bytes("12345678123456781234567812345678", "utf8"),
self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example3_local_variable1(self, data):
key = b"12345678123456781234567812345678"
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example4_local_variable2(self, data):
key = bytes("12345678123456781234567812345678", "utf8")
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example5_nested_local_variable1(self, data):
key1 = b"12345678123456781234567812345678"
key2 = key1
key3 = key2
cipher = Cipher("aes_256_ecb", key3, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example6_nested_local_variable2(self, data):
key1 = bytes("12345678123456781234567812345678", "utf8")
key2 = key1
key3 = key2
cipher = Cipher("aes_256_ecb", key3, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example_method_call(self, key, data):
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example_nested_method_call(self, key, data):
return self.p_example_method_call(key, data)
def p_example7_direct_method_call1(self, data):
key = b"12345678123456781234567812345678"
return self.p_example_method_call(key, data)
def p_example8_direct_method_call2(self, data):
key = bytes("12345678123456781234567812345678", "utf8")
return self.p_example_method_call(key, data)
def p_example9_nested_method_call1(self, data):
key = b"12345678123456781234567812345678"
return self.p_example_nested_method_call(key, data)
def p_example10_nested_method_call2(self, data):
key = bytes("12345678123456781234567812345678", "utf8")
return self.p_example_nested_method_call(key, data)
def p_example11_direct_g_variable_access1(self, data):
cipher = Cipher("aes_256_ecb", self.g_key1, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example12_direct_g_variable_access2(self, data):
cipher = Cipher("aes_256_ecb", self.g_key2, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example13_indirect_g_variable_access1(self, data):
key = self.g_key1
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example14_indirect_g_variable_access2(self, data):
key = self.g_key2
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def p_example15_warning_parameter_not_resolvable(self, key, data):
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
def n_example1_random_key(self, data):
key = rand_bytes(32)
cipher = Cipher("aes_256_ecb", key, self.g_iv, self.g_encrypt)
cipher_text = cipher.update(data) + cipher.final()
return cipher_text
|
nilq/baby-python
|
python
|
"""General-purpose test script for image-to-image translation.
Once you have trained your model with train.py, you can use this script to test the model.
It will load a saved model from --checkpoints_dir and save the results to --results_dir.
It first creates model and dataset given the option. It will hard-code some parameters.
It then runs inference for --num_test images and save results to an HTML file.
Example (You need to train models first or download pre-trained models from our website):
Test a CycleGAN model (both sides):
python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
Test a CycleGAN model (one side only):
python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
The option '--model test' is used for generating CycleGAN results only for one side.
This option will automatically set '--dataset_mode single', which only loads the images from one set.
On the contrary, using '--model cycle_gan' requires loading and generating results in both directions,
which is sometimes unnecessary. The results will be saved at ./results/.
Use '--results_dir <directory_path_to_save_result>' to specify the results directory.
Test a pix2pix model:
python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
See options/base_options.py and options/test_options.py for more test options.
See training and test tips at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md
See frequently asked questions at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md
"""
import os
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from util.visualizer import save_images
from util import html
from external.genesis import multi_object_config
from torch.utils.tensorboard import SummaryWriter
import torch
from datetime import datetime
import os
import socket
from tqdm import tqdm
if __name__ == '__main__':
opt = TestOptions().parse() # get test options
# hard-code some parameters for test
opt.num_threads = 0 # test code only supports num_threads = 1
opt.batch_size = 2 # test code only supports batch_size = 1
opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed.
opt.no_flip = True # no flip; comment this line if results on flipped images are needed.
opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file.
train_dataloader, val_dataloader, test_dataloader = multi_object_config.load(opt)
#dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
model = create_model(opt) # create a model given opt.model and other options
model.setup(opt) # regular setup: load and print networks; create schedulers
current_time = datetime.now().strftime('%b%d_%H-%M-%S')
log_dir = os.path.join(
'/home/tberriel/Workspaces/representation_ws/MONet-pytorch/tests', current_time + '_' + socket.gethostname())
writer = SummaryWriter(log_dir=log_dir)
# test with eval mode. This only affects layers like batchnorm and dropout.
# For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode.
# For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout.
pbar = tqdm(enumerate(val_dataloader),
total=len(val_dataloader)
)
ari_fg_list = []
if opt.eval:
model.eval()
with torch.no_grad():
for counter, data in pbar:
model.set_input(data) # unpack data from data loader
model.test() # run inference
visuals = model.get_current_visuals() # get image results
#img_path = model.get_image_paths() # get image paths
writer.add_images("x_input/"+str(counter), model.x)
writer.add_images("x_tilde/"+str(counter),model.x_tilde)
for j in range(opt.batch_size):
writer.add_images("x_input_tilde/batch_"+str(counter)+"/"+str(j), torch.cat([model.x[j].expand(1,-1,-1,-1),model.x_tilde[j].expand(1,-1,-1,-1)]))
writer.add_images("x_masked/batch_"+str(counter)+"/"+str(j), model.x_masked[j])
writer.add_images("mask/batch_"+str(counter)+"/"+str(j), model.m[j].expand(1,-1,-1,-1).transpose(1,0))
#TODO evaluate ARI
#ari, _ = compute_ari(out["x_masked"], label, foreground_only=True)
#ari_fg_list.append(ari)
#writer.add_scalar('ARI', sum(ari_fg_list)/(len(ari_fg_list)), 0)
#writer.add_graph(net,img)
writer.close()
|
nilq/baby-python
|
python
|
from datetime import date
from time import sleep
print('\033[1:31m-=-\033[m' * 10)
print('\033[1m ...ALISTAMENTO MILITAR...\033[m')
print('\033[1:31m-=-\033[m' * 10)
ano = int(input('Em que ano você nasceu? '))
today = date.today().year
dif = today - ano
sleep(2)
print('''\033[1m...PROCESSANDO...AGUARDE...
''')
sleep(3)
if dif < 18:
status = -1
elif dif > 18:
status = 1
else:
status = 0
if status == -1:
print('Ano de nascimento do candidato: {}'.format(ano))
print('Ano do dia atual: {}'.format(today))
print('O candidato tem {} anos'.format(dif))
print('O candidato ainda não está apto a se alistar.')
print('Se alistará no ano {}'.format(today + (18-dif)))
if status == 0:
print('Ano de nascimento do candidato: {}'.format(ano))
print('Ano do dia atual: {}'.format(today))
print('O candidato tem {} anos'.format(dif))
print('O candida deve se alistar IMEDIATAMENTE')
if status == 1:
print('Ano de nascimento do candidato: {}'.format(ano))
print('Ano do dia atual: {}'.format(today))
print('O candidato tem {} anos'.format(dif))
print('O candidato já passou da hora de se alistar')
print('Se alistou no ano {}'.format(today - (dif-18)))
|
nilq/baby-python
|
python
|
import zPE.GUI.io_encap as io_encap
from zPE.GUI.zComp.zStrokeParser import KEY_BINDING_RULE_MKUP, parse_key_binding as zSP_PARSE_KEY_BINDING
import os, sys
import pygtk
pygtk.require('2.0')
import gtk
import copy # for deep copy
import pango # for parsing the font
import re # for parsing the string
# debug flags
__TRACE_KEY_SCAN = False
# import and update path
from zPE.util.global_config import CONFIG_PATH, HOME_PATH
CONFIG_PATH['gui_rc'] = os.path.join(HOME_PATH, '.zPE', 'gui.conf')
CONFIG_PATH['key_emacs'] = os.path.join(HOME_PATH, '.zPE', 'key.emacs')
#CONFIG_PATH['key_vi'] = os.path.join(HOME_PATH, '.zPE', 'key.vi')
CONFIG_PATH['key_other'] = os.path.join(HOME_PATH, '.zPE', 'key.other')
# constant that will be treated as false
STR_FALSE = [ '0', 'nil', 'false', 'False' ]
# get monospace font list
MONO_FONT = {
# 'font family name' : pango.FontFamily object
}
for font in gtk.gdk.pango_context_get().list_families():
if font.is_monospace():
MONO_FONT[font.get_name()] = font
# get html color mapping
COLOR_LIST = {
'black' : '#000000',
'blue' : '#0000FF',
'brown' : '#A52A2A',
'coral' : '#FF7F50',
'cyan' : '#00FFFF',
'fuchsia' : '#FF00FF',
'gold' : '#FFD700',
'gray' : '#808080',
'grey' : '#808080',
'green' : '#008000',
'lime' : '#00FF00',
'magenta' : '#FF00FF',
'maroon' : '#800000',
'olive' : '#808000',
'orange' : '#FFA500',
'orchid' : '#DA70D6',
'pink' : '#FFC0CB',
'purple' : '#800080',
'red' : '#FF0000',
'silver' : '#C0C0C0',
'violet' : '#EE82EE',
'wheat' : '#F5DEB3',
'white' : '#FFFFFF',
'yellow' : '#FFFF00',
}
INVERSE_COLOR_LIST = dict( [ (v, k) for (k, v) in COLOR_LIST.iteritems() ] )
# major editing mode map
MODE_MAP = {
# mode_string : mode_object
}
DEFAULT_BUFF_MODE_MAP = {
'scratch' : 'ASM Mode', # scratch file
'file' : 'ASM Mode', # regular file
'dir' : 'Text Mode', # directory
'disp' : 'Text Mode', # display panel
}
## Configurable Definition
DEFAULT = {
'MISC' : {
'KEY_BINDING' : 'other', # style ~ ( emacs, vi*, other )
# note: vi mode not implemented
'KILL_RING_SZ' : 16,
'TAB_ON' : 1,
'TAB_GROUPED' : 0,
'DEBUG_MODE' : 0,
},
'FONT' : {
'NAME' : 'Monospace',
'SIZE' : 12,
},
'COLOR_MAP' : {
'TEXT' : '#000000', # black
'TEXT_SELECTED' : '#000000', # black
'BASE' : '#FBEFCD', # wheat - mod
'BASE_SELECTED' : '#FFA500', # orenge
'STATUS' : '#808080', # gray
'STATUS_ACTIVE' : '#C0C0C0', # silver
# foreground only
'RESERVE' : '#0000FF', # blue
'COMMENT' : '#008000', # green
'LITERAL' : '#FF0000', # red
'SYMBOL' : '#800080', # purple
# background only
'INVALID' : '#FF0000', # red
},
'ENV' : {
'STARTING_PATH' : HOME_PATH,
},
}
DEFAULT_FUNC_KEY_BIND_KEY = [
'emacs',
# 'vi', # not implenemted yet
'other'
]
DEFAULT_FUNC_KEY_BIND = {
# need to be sync with "http://code.google.com/p/mainframe-env-simulator/wiki/GuiKeyBinding"
# split window manipulation; required by zSplitWindow
'window-split-horz' : {
'emacs' : 'C-x 3',
'vi' : '',
'other' : '',
},
'window-split-vert' : {
'emacs' : 'C-x 2',
'vi' : '',
'other' : '',
},
'window-delete' : {
'emacs' : 'C-x 0',
'vi' : '',
'other' : '',
},
'window-delete-other' : {
'emacs' : 'C-x 1',
'vi' : '',
'other' : '',
},
# buffer manipulation; required by zEdit
'buffer-open' : {
'emacs' : 'C-x C-f',
'vi' : '',
'other' : 'C-o',
},
'buffer-save' : {
'emacs' : 'C-x C-s',
'vi' : '',
'other' : 'C-s',
},
'buffer-save-as' : {
'emacs' : 'C-x C-w',
'vi' : '',
'other' : 'C-S',
},
'buffer-close' : {
'emacs' : 'C-x k',
'vi' : '',
'other' : 'F4',
},
'buffer-undo' : {
'emacs' : 'C-x u',
'vi' : '',
'other' : 'C-z',
},
'buffer-redo' : {
'emacs' : '',
'vi' : '',
'other' : 'C-y',
},
# tab manipulation; required by zEdit
'tabbar-mode' : {
'emacs' : 'F7',
'vi' : '',
'other' : 'F7',
},
'tabbar-prev' : {
'emacs' : 'C-Left',
'vi' : '',
'other' : 'C-Left',
},
'tabbar-next' : {
'emacs' : 'C-Right',
'vi' : '',
'other' : 'C-Right',
},
# editor related functions; required by zTextView and/or zEntry
'align-line' : {
'emacs' : '',
'vi' : '',
'other' : '',
},
'align-region' : {
'emacs' : 'C-M-\\',
'vi' : '',
'other' : '',
},
'align-or-complete' : {
'emacs' : 'TAB',
'vi' : '',
'other' : 'TAB',
},
'complete' : {
'emacs' : '',
'vi' : '',
'other' : '',
},
'complete-list' : {
'emacs' : 'M-/',
'vi' : '',
'other' : '',
},
'backward-char' : {
'emacs' : 'C-b',
'vi' : '',
'other' : '',
},
'backward-delete-char' : {
'emacs' : 'BackSpace',
'vi' : '',
'other' : 'BackSpace',
},
'forward-char' : {
'emacs' : 'C-f',
'vi' : '',
'other' : '',
},
'forward-delete-char' : {
'emacs' : 'Delete',
'vi' : '',
'other' : 'Delete',
},
'backward-word' : {
'emacs' : 'M-b',
'vi' : '',
'other' : '',
},
'backward-delete-word' : {
'emacs' : 'M-D',
'vi' : '',
'other' : '',
},
'forward-word' : {
'emacs' : 'M-f',
'vi' : '',
'other' : '',
},
'forward-delete-word' : {
'emacs' : 'M-d',
'vi' : '',
'other' : '',
},
'backward-line' : {
'emacs' : 'C-a',
'vi' : '',
'other' : '',
},
'backward-delete-line' : {
'emacs' : 'C-K',
'vi' : '',
'other' : '',
},
'forward-line' : {
'emacs' : 'C-e',
'vi' : '',
'other' : '',
},
'forward-delete-line' : {
'emacs' : 'C-k',
'vi' : '',
'other' : '',
},
'backward-para' : {
'emacs' : 'M-{',
'vi' : '',
'other' : '',
},
'backward-delete-para' : {
'emacs' : 'M-K',
'vi' : '',
'other' : '',
},
'forward-para' : {
'emacs' : 'M-}',
'vi' : '',
'other' : '',
},
'forward-delete-para' : {
'emacs' : 'M-k',
'vi' : '',
'other' : '',
},
'kill-region' : {
'emacs' : 'C-w',
'vi' : '',
'other' : 'C-x',
},
'kill-ring-save' : {
'emacs' : 'M-w',
'vi' : '',
'other' : 'C-c',
},
'kill-ring-yank' : {
'emacs' : 'C-y',
'vi' : '',
'other' : 'C-v',
},
'kill-ring-yank-pop' : {
'emacs' : 'M-y',
'vi' : '',
'other' : '',
},
'set-mark-command' : {
'emacs' : 'C-@',
'vi' : '',
'other' : '',
},
'set-mark-move-left' : {
'emacs' : 'S-Left',
'vi' : '',
'other' : 'S-Left',
},
'set-mark-move-right' : {
'emacs' : 'S-Right',
'vi' : '',
'other' : 'S-Right',
},
'set-mark-move-up' : {
'emacs' : 'S-Up',
'vi' : '',
'other' : 'S-Up',
},
'set-mark-move-down' : {
'emacs' : 'S-Down',
'vi' : '',
'other' : 'S-Down',
},
'set-mark-move-start' : {
'emacs' : 'S-Home',
'vi' : '',
'other' : 'S-Home',
},
'set-mark-move-end' : {
'emacs' : 'S-End',
'vi' : '',
'other' : 'S-End',
},
'set-mark-select-all' : {
'emacs' : 'C-x h',
'vi' : '',
'other' : 'C-a',
},
# functions that are not required by any z* module
# top-level functions
'prog-show-config' : {
'emacs' : 'C-c c',
'vi' : '',
'other' : 'C-p',
},
'prog-show-error' : {
'emacs' : 'C-c e',
'vi' : '',
'other' : 'C-J',
},
'prog-show-about' : {
'emacs' : '',
'vi' : '',
'other' : '',
},
'prog-quit' : {
'emacs' : 'C-x C-c',
'vi' : '',
'other' : 'C-q',
},
# zPE related functions
'zPE-submit' : {
'emacs' : 'F9',
'vi' : '',
'other' : 'F9',
},
'zPE-submit-with-JCL' : {
'emacs' : 'F8',
'vi' : '',
'other' : 'F8',
},
}
Config = {}
def init_rc_all():
init_rc()
init_key_binding()
def init_rc():
Config['MISC'] = {
'key_binding' : DEFAULT['MISC']['KEY_BINDING'],
'kill_ring_sz' : DEFAULT['MISC']['KILL_RING_SZ'],
'tab_on' : DEFAULT['MISC']['TAB_ON'],
'tab_grouped' : DEFAULT['MISC']['TAB_GROUPED'],
'debug_mode' : DEFAULT['MISC']['DEBUG_MODE'],
}
Config['FONT'] = {
'name' : DEFAULT['FONT']['NAME'],
'size' : DEFAULT['FONT']['SIZE'],
}
Config['COLOR_MAP'] = {
'text' : DEFAULT['COLOR_MAP']['TEXT'],
'text_selected' : DEFAULT['COLOR_MAP']['TEXT_SELECTED'],
'base' : DEFAULT['COLOR_MAP']['BASE'],
'base_selected' : DEFAULT['COLOR_MAP']['BASE_SELECTED'],
'status' : DEFAULT['COLOR_MAP']['STATUS'],
'status_active' : DEFAULT['COLOR_MAP']['STATUS_ACTIVE'],
'reserve' : DEFAULT['COLOR_MAP']['RESERVE'],
'comment' : DEFAULT['COLOR_MAP']['COMMENT'],
'literal' : DEFAULT['COLOR_MAP']['LITERAL'],
'symbol' : DEFAULT['COLOR_MAP']['SYMBOL'],
'invalid' : DEFAULT['COLOR_MAP']['INVALID'],
}
Config['ENV'] = {
'starting_path' : DEFAULT['ENV']['STARTING_PATH'],
}
def init_key_binding():
kb_style = Config['MISC']['key_binding']
Config['FUNC_BINDING'] = dict(
zip( DEFAULT_FUNC_KEY_BIND.keys(),
[ v[kb_style] for v in DEFAULT_FUNC_KEY_BIND.itervalues() ]
)
)
Config['KEY_BINDING'] = dict((v, k) for (k, v) in Config['FUNC_BINDING'].iteritems())
if '' in Config['KEY_BINDING']:
del Config['KEY_BINDING'][''] # remove empty binding
def read_rc_all():
read_rc()
read_key_binding()
def read_rc():
init_rc()
__CK_CONFIG()
label = None
for line in open(CONFIG_PATH['gui_rc'], 'r'):
if line.isspace(): # skip empty line
continue
line = line[:-1] # get rid of the '\n'
if line in [ '[MISC]', '[FONT]', '[COLOR_MAP]', '[ENV]' ]:
label = line[1:-1] # retrieve the top-level key
continue
if not label:
continue # no top-level key so far, skip the line
(k, v) = re.split('[ \t]*=[ \t]*', line, maxsplit=1)
if label == 'MISC':
if k in [ 'tab_on', 'tab_grouped', 'debug_mode', ]:
if v and v not in STR_FALSE:
Config[label][k] = 1
else:
Config[label][k] = 0
elif k == 'key_binding':
if v in DEFAULT_FUNC_KEY_BIND_KEY:
Config[label][k] = v
else:
Config[label][k] = DEFAULT['MISC']['KEY_BINDING']
sys.stderr.write('CONFIG WARNING: {0}: Invalid key binding style.\n'.format(v))
elif k == 'kill_ring_sz':
try:
v = int(v)
if v >= 1:
Config[label][k] = v
else:
sys.stderr.write('CONFIG WARNING: {0}: Kill-ring size must be at least 1.\n'.format(v))
except ValueError:
Config[label][k] = DEFAULT['MISC']['KILL_RING_SZ']
sys.stderr.write('CONFIG WARNING: {0}: Invalid kill-ring size.\n'.format(v))
elif label == 'FONT':
if k == 'name':
found = False
for font in MONO_FONT:
if font == v:
Config[label][k] = v
found = True
break
if not found:
sys.stderr.write('CONFIG WARNING: {0}: Invalid font name.\n'.format(v))
elif k == 'size':
try:
Config[label][k] = int(v)
except ValueError:
Config[label][k] = DEFAULT['FONT_SZ']
sys.stderr.write('CONFIG WARNING: {0}: Invalid font size.\n'.format(v))
elif label == 'COLOR_MAP':
if v.lower() in COLOR_LIST:
v_ty = 'name'
v = COLOR_LIST[v]
else:
v_ty = 'code'
v = v.upper() # convert hex color code to all upper case
if not re.match('#[0-9A-F]{6}', v):
sys.stderr.write('CONFIG WARNING: {0}: Invalid color {1}.\n'.format(v, v_ty))
continue
# valid color, check key
if k in Config[label]:
Config[label][k] = v
else:
sys.stderr.write('CONFIG WARNING: {0}: Invalid color mapping.\n'.format(k))
elif label == 'ENV':
if k == 'starting_path':
tmp_v = io_encap.norm_path(v)
if os.path.isdir(tmp_v):
Config[label][k] = tmp_v
else:
Config[label][k] = io_encap.norm_path( DEFAULT['ENV']['STARTING_PATH'] )
sys.stderr.write('CONFIG WARNING: {0}: Invalid starting path.\n'.format(v))
write_rc()
def read_key_binding():
__CK_KEY()
# retrieve all valid functions
Config['FUNC_BINDING'] = dict(zip(DEFAULT_FUNC_KEY_BIND.keys(), [''] * len(DEFAULT_FUNC_KEY_BIND)))
Config['KEY_BINDING'] = {}
# parse key binding file
for line in open(CONFIG_PATH[ 'key_{0}'.format(Config['MISC']['key_binding']) ], 'r'):
line = line[:-1] # get rid of the '\n'
(k, v) = re.split('[ \t]*=[ \t]*', line, maxsplit=1)
k = k.replace('_', '-') # normalize delimiter
if __TRACE_KEY_SCAN:
sys.stderr.write('\n== Style::{0} => {1}:\n'.format(Config['MISC']['key_binding'], line))
if not v:
continue
seq = parse_key_binding(v)
if not seq:
sys.stderr.write('CONFIG WARNING: {0}: Invalid key sequence.\n'.format(v))
continue
key_sequence_add(k, seq, force_override = True, force_rebind = True, warning = True)
if __TRACE_KEY_SCAN:
sys.stderr.write(' Func => Key:\n')
for (k, v) in Config['FUNC_BINDING'].iteritems():
sys.stderr.write(' {0} : {1}\n'.format(k, v))
sys.stderr.write(' Key => Func:\n')
for (k, v) in Config['KEY_BINDING'].iteritems():
sys.stderr.write(' {0} : {1}\n'.format(k, v))
write_key_binding()
def key_sequence_add(func, seq,
force_override = False, # redefine function with different stroke
force_rebind = False, # rebind stroke with different function
warning = True # print warning msg to stderr
):
stroke = ' '.join(seq) # normalize the key sequence
if func not in Config['FUNC_BINDING']:
# undefined function
if warning:
sys.stderr.write('CONFIG WARNING: {0}: Invalid key binding.\n'.format(func))
return None
else:
if Config['FUNC_BINDING'][func] == stroke:
# same binding as before
return False
# remove conflict stroke
if stroke in Config['KEY_BINDING']:
# key sequence defined for another function
msg = 'CONFIG WARNING: {0}: Key sequence already binded.\n'.format(stroke)
if not force_override:
raise ValueError('override', msg, Config['KEY_BINDING'][stroke], stroke)
if warning:
sys.stderr.write(msg)
old_func = Config['KEY_BINDING'][stroke] # will never be empty, unless in 'else' part
del Config['KEY_BINDING'][stroke] # remove stroke
else:
old_func = '' # old_func not found
old_stroke = Config['FUNC_BINDING'][func] # could be empty
if old_stroke:
# has previously defined stroke
msg = 'CONFIG WARNING: {0}: Redifing key binding for the function.\n'.format(func)
if not force_rebind:
raise ValueError('rebind', msg, func, old_stroke)
if warning:
sys.stderr.write(msg)
del Config['KEY_BINDING'][old_stroke] # remove old_stroke
# reset conflict func
Config['FUNC_BINDING'][func] = ''
if old_func:
Config['FUNC_BINDING'][old_func] = ''
# add new func and stroke
Config['FUNC_BINDING'][func] = stroke
Config['KEY_BINDING'][stroke] = func
return True
def func_binding_rm(func):
if func not in Config['FUNC_BINDING']:
raise KeyError('CONFIG WARNING: {0}: Not a valid function.\n'.format(func))
old_strock = Config['FUNC_BINDING'][func]
Config['FUNC_BINDING'][func] = ''
if old_strock:
del Config['KEY_BINDING'][old_strock]
def parse_key_binding(key_sequence):
return zSP_PARSE_KEY_BINDING(key_sequence, Config['MISC']['key_binding'])
def reset_key_binding():
for style in DEFAULT_FUNC_KEY_BIND_KEY:
func_binding = dict(
zip( DEFAULT_FUNC_KEY_BIND.keys(),
[ v[style] for v in DEFAULT_FUNC_KEY_BIND.itervalues() ]
)
)
__TOUCH_KEY(style, func_binding)
def write_rc_all():
write_rc()
write_key_binding()
def write_rc():
__TOUCH_RC()
def write_key_binding():
__TOUCH_KEY()
### Supporting Function
def __CK_CONFIG():
if not os.path.isfile(CONFIG_PATH['gui_rc']):
__TOUCH_RC()
def __CK_KEY():
style = Config['MISC']['key_binding']
style_path = 'key_{0}'.format(style)
if not os.path.isfile(CONFIG_PATH[style_path]):
init_key_binding()
__TOUCH_KEY()
def __TOUCH_RC():
fp = io_encap.open_file(CONFIG_PATH['gui_rc'], 'w')
for label in [ 'MISC', 'ENV', 'FONT' ]:
fp.write('[{0}]\n'.format(label))
for key in sorted(Config[label].iterkeys()):
fp.write('{0} = {1}\n'.format(key, Config[label][key]))
fp.write('\n')
label = 'COLOR_MAP'
fp.write('[{0}]\n'.format(label))
for key in sorted(Config[label].iterkeys()):
value = Config[label][key]
if value in INVERSE_COLOR_LIST:
value = INVERSE_COLOR_LIST[value]
fp.write('{0} = {1}\n'.format(key, value))
fp.write('\n')
fp.close()
def __TOUCH_KEY(style = None, func_binding = None):
if not style or not func_binding:
style = Config['MISC']['key_binding']
func_binding = copy.copy(Config['FUNC_BINDING'])
style_path = 'key_{0}'.format(style)
fp = io_encap.open_file(CONFIG_PATH[style_path], 'w')
for func in sorted(func_binding.iterkeys()):
fp.write('{0} = {1}\n'.format(func, func_binding[func]))
fp.close()
|
nilq/baby-python
|
python
|
# -*- coding: utf-8 -*-
import unittest
import os
from elfwrapper.elf_wrapper import ElfAddrObj
class TestApp(unittest.TestCase):
def setUp(self):
pass
def test_1(self):
elf = ElfAddrObj(os.path.join(os.getcwd(), r"example/Test.elf"))
with open(r'example\test_var.txtdatafile.txt') as f:
for line in f:
var, _, add = line.split(":")
if elf.get_var_addrs(var) == int(add.strip(),16):
print()
#self.assertEqual(, elf.get_var_addrs(""))
def suite():
suite = unittest.TestSuite()
suite.addTests(
unittest.TestLoader().loadTestsFromTestCase(TestApp)
)
return suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
|
nilq/baby-python
|
python
|
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""TPS65185: Single chip PMIC for E Ink (R) Vizplex (TM) Enabled Electronic Paper Display"""
__author__ = "ChISL"
__copyright__ = "TBD"
__credits__ = ["Texas Instruments"]
__license__ = "TBD"
__version__ = "0.1"
__maintainer__ = "https://chisl.io"
__email__ = "info@chisl.io"
__status__ = "Test"
from TPS65185_constants import *
# name: TPS65185
# description: Single chip PMIC for E Ink (R) Vizplex (TM) Enabled Electronic Paper Display
# manuf: Texas Instruments
# version: 0.1
# url: http://www.ti.com/lit/ds/symlink/tps65185.pdf
# date: 2016-08-01
# Derive from this class and implement read and write
class TPS65185_Base:
"""Single chip PMIC for E Ink (R) Vizplex (TM) Enabled Electronic Paper Display"""
# Register TMST_VALUE
# Thermistor value read by ADC signed int, in ˚C
# 1111 0110 – < -10°C
# 1111 0110 – -10°C
# 1111 0111 – -9°C
# ...
# 1111 1110 – -2°C
# 1111 1111 – -1°C
# 0000 0000 – 0°C
# 0000 0001 – 1°C
# 0000 0010 – 2°C
# ...
# 0001 1001 – 25°C
# ...
# 0101 0101 – 85°C
# 0101 0101 – > 85°C
def setTMST_VALUE(self, val):
"""Set register TMST_VALUE"""
self.write(REG.TMST_VALUE, val, 8)
def getTMST_VALUE(self):
"""Get register TMST_VALUE"""
return self.read(REG.TMST_VALUE, 8)
# Bits TEMP
# Register ENABLE
# Enable/disable bits for regulators
def setENABLE(self, val):
"""Set register ENABLE"""
self.write(REG.ENABLE, val, 8)
def getENABLE(self):
"""Get register ENABLE"""
return self.read(REG.ENABLE, 8)
# Bits ACTIVE
# 1 = Transition from STANDBY to ACTIVE mode. Rails power up as defined by UPSEQx
# registers.
# 0 = No effect.
# NOTE: After transition bit is cleared automatically
# Bits STANDBY
# Transition from STANDBY to ACTIVE mode. Rails power up as defined by DWNSEQx
# registers.
# NOTE: After transition bit is cleared automatically.
# STANDBY bit has priority over AVTIVE.
# Bits V3P3_EN
# VIN3P3 to V3P3 switch enable (1=ON)
# Bits VCOM_EN
# VCOM buffer enable (1 = enabled)
# Bits VDDH_EN
# VDDH charge pump enable (1 = enabled)
# Bits VPOS_EN
# VPOS LDO regulator enable. (1 = enabled)
# NOTE: VPOS cannot be enabled before VNEG is enabled.
# Bits VEE_EN
# VEE charge pump enable (1 = enabled)
# Bits VNEG_EN
# VNEG LDO regulator enable. (1 = enabled)
# NOTE: When VNEG is disabled VPOS will also be disabled.
# Register VADJ
# VPOS/VNEG voltage adjustment
def setVADJ(self, val):
"""Set register VADJ"""
self.write(REG.VADJ, val, 8)
def getVADJ(self):
"""Get register VADJ"""
return self.read(REG.VADJ, 8)
# Bits unused_0
# Bits VSET
# VPOS and VNEG voltage setting
# Register VCOM
# VCOM voltage setting, byteorder little
def setVCOM(self, val):
"""Set register VCOM"""
self.write(REG.VCOM, val, 16)
def getVCOM(self):
"""Get register VCOM"""
return self.read(REG.VCOM, 16)
# Bits ACQ
# Kick-back voltage acquisition bit.
# 1 - starts kick-back voltage measurement routine.
# NOTE: After measurement is complete bit is cleared automatically and measurement
# result is reflected in VCOM[8:0] bits.
# Bits PROG
# VCOM programming bit.
# 1 - VCOM[8:0] value is committed to nonvolatile memory and becomes new power-up
# default.
# NOTE: After programming bit is cleared automatically and TPS65185 will enter
# STANDBY mode.
# Bits HiZ
# VCOM HiZ bit.
# 1 - VCOM pin is placed into hi-impedance state to allow VCOM measurement
# 0 - VCOM amplifier is connected to VCOM pin
# Bits AVG
# Number of acquisitions that is averaged to a single kick-back voltage measurement
# NOTE: When the ACQ bit is set, the state machine repeat the A/D conversion of the
# kick-back voltage AVD[1:0] times and returns a single, averaged, value to VCOM[8:0]
# Bits unused_0
# Bits VCOM
# VCOM voltage adjustment
# VCOM = -10 mV * VCOM[8:0] in the range from 0 mV to -5.110 V
# 0x000h – 0 0000 0000 – –0 mV
# 0x001h – 0 0000 0001 – –10 mV
# 0x002h – 0 0000 0010 – –20 mV
# ...
# 0x07Dh - 0 0111 1101 – –1250 mV
# ...
# 0x1FEh – 1 1111 1110 – –5100 mV
# 0x1FFh – 1 1111 1111 – –5110 mV
#
# Register INT_EN1
# Interrupt enable group1
# 1 = enabled, 0 = disabled
def setINT_EN1(self, val):
"""Set register INT_EN1"""
self.write(REG.INT_EN1, val, 8)
def getINT_EN1(self):
"""Get register INT_EN1"""
return self.read(REG.INT_EN1, 8)
# Bits DTX_EN
# Panel temperature-change interrupt enable
# Bits TSD_EN
# Thermal shutdown interrupt enable
# Bits HOT_EN
# Thermal shutdown early warning enable
# Bits TMST_HOT_EN
# Thermistor hot interrupt enable
# Bits TMST_COLD_EN
# Thermistor cold interrupt enable
# Bits UVLO_EN
# VIN under voltage detect interrupt enable
# Bits ACQC_EN
# VCOM acquisition complete interrupt enable
# Bits PRGC_EN
# VCOM programming complete interrupt enable
# Register INT_EN2
# Interrupt enable group2
# 1 = enabled, 0 = disabled
def setINT_EN2(self, val):
"""Set register INT_EN2"""
self.write(REG.INT_EN2, val, 8)
def getINT_EN2(self):
"""Get register INT_EN2"""
return self.read(REG.INT_EN2, 8)
# Bits VBUVEN
# Positive boost converter under voltage detect interrupt enable
# Bits VDDHUVEN
# VDDH under voltage detect interrupt enable
# Bits VNUV_EN
# Inverting buck-boost converter under voltage detect interrupt enable
# Bits VPOSUVEN
# VPOS under voltage detect interrupt enable
# Bits VEEUVEN
# VEE under Voltage detect interrupt enable
# Bits VCOMFEN
# VCOM FAULT interrupt enable
# Bits VNEGUVEN
# VNEG under Voltage detect interrupt enable
# Bits EOCEN
# Temperature ADC end of conversion interrupt enable
# Register INT1
# Interrupt group1 DEFAULT '0xxxxx00
def setINT1(self, val):
"""Set register INT1"""
self.write(REG.INT1, val, 8)
def getINT1(self):
"""Get register INT1"""
return self.read(REG.INT1, 8)
# Bits DTX
# Panel temperature-change interrupt, 1 - temperature has changed by 3 deg or more
# over previous reading
# Bits TSD
# Thermal shutdown interrupt
# Bits HOT
# Thermal shutdown early warning
# Bits TMST_HOT
# Thermistor hot interrupt. 1 - thermistor temperature is equal or greater than
# TMST_HOT threshold
# Bits TMST_COLD
# Thermistor cold interrupt. 1 - thermistor temperature is equal or less than
# TMST_COLD threshold
# Bits UVLO
# VIN under voltage detect interrupt. 1 - input voltage is below UVLO threshold
# Bits ACQC
# VCOM acquisition complete
# Bits PRGC
# VCOM programming complete
# Register INT2
# Interrupt group2
def setINT2(self, val):
"""Set register INT2"""
self.write(REG.INT2, val, 8)
def getINT2(self):
"""Get register INT2"""
return self.read(REG.INT2, 8)
# Bits VB_UV
# Positive boost converter undervoltage detect interrupt 1 - under-voltage on
# DCDC1 detected
# Bits VDDH_UV
# VDDH under voltage detect interrupt on VDDH charge pump
# Bits VN_UV
# Inverting buck-boost converter under voltage detect interrupt 1 -
# undervoltage on DCDC2 detected
# Bits VPOS_UV
# VPOS undervoltage detect interrupt 1 - undervoltage on LDO1(VPOS) detected
# Bits VEE_UV
# VEE undervoltage detect interrupt 1 - undervoltage on VEE charge pump detected
# Bits VCOMF
# VCOM fault detection 1 - fault on VCOM detected
# (VCOM is outside normal operating range)
# Bits VNEG_UV
# VNEG undervoltage detect interrupt 1 - undervoltage on LDO2(VNEG) detected
# Bits EOC
# ADC end of conversion interrupt 1 - ADC conversion is complete
# (temperature acquisition is complete)
# Register UPSEQ0
# Power-up strobe assignment
def setUPSEQ0(self, val):
"""Set register UPSEQ0"""
self.write(REG.UPSEQ0, val, 8)
def getUPSEQ0(self):
"""Get register UPSEQ0"""
return self.read(REG.UPSEQ0, 8)
# Bits VDDH_UP
# VDDH power-up order
# Bits VPOS_UP
# VPOS power-up order
# Bits VEE_UP
# VEE power-up order
# Bits VNEG_UP
# VNEG power-up order
# Register UPSEQ1
# Power-up sequence delay times
def setUPSEQ1(self, val):
"""Set register UPSEQ1"""
self.write(REG.UPSEQ1, val, 8)
def getUPSEQ1(self):
"""Get register UPSEQ1"""
return self.read(REG.UPSEQ1, 8)
# Bits UDLY4
# DLY4 delay time set; defines the delay time from STROBE3 to STROBE4
# Bits UDLY3
# DLY3 delay time set; defines the delay time from STROBE2 to STROBE3
# Bits UDLY2
# DLY2 delay time set; defines the delay time from STROBE1 to STROBE2
# Bits UDLY
# DLY1 delay time set; defines the delay time from VN_PG high to STROBE1
# Register DWNSEQ0
# Power-down strobe assignment
def setDWNSEQ0(self, val):
"""Set register DWNSEQ0"""
self.write(REG.DWNSEQ0, val, 8)
def getDWNSEQ0(self):
"""Get register DWNSEQ0"""
return self.read(REG.DWNSEQ0, 8)
# Bits VDDH_DWN
# VDDH power-down order
# Bits VPOS_DWN
# VPOS power-down order
# Bits VEE_DWN
# VEE power-down order
# Bits VNEG_DWN
# VNEG power-down order
# Register DWNSEQ1
# Power-down sequence delay times
def setDWNSEQ1(self, val):
"""Set register DWNSEQ1"""
self.write(REG.DWNSEQ1, val, 8)
def getDWNSEQ1(self):
"""Get register DWNSEQ1"""
return self.read(REG.DWNSEQ1, 8)
# Bits DDLY4
# DLY4 delay time set; defines the delay time from STROBE3 to STROBE4
# Bits DDLY3
# DLY3 delay time set; defines the delay time from STROBE2 to STROBE3
# Bits DDLY2
# DLY2 delay time set; defines the delay time from STROBE1 to STROBE2
# Bits DDLY1
# DLY2 delay time set; defines the delay time from WAKEUP low to STROBE1
# Bits DFCTR
# At power-down delay time DLY2[1:0], DLY3[1:0], DLY4[1:0] are multiplied with DFCTR[1:0]
# Register TMST1
# Thermistor configuration
def setTMST1(self, val):
"""Set register TMST1"""
self.write(REG.TMST1, val, 8)
def getTMST1(self):
"""Get register TMST1"""
return self.read(REG.TMST1, 8)
# Bits READ_THERM
# Read thermistor value. 1 - initiates temperature acquisition
# NOTE: Bit is self-cleared after acquisition is completed
# Bits unused_0
# Bits CONV_END
# ADC conversion done flag
# Bits unused_1
# Bits unused_2
# Bits unused_3
# Bits DT
# Panel temperature-change interrupt threshold DTX interrupt is issued when
# difference between most recent temperature reading and baseline temperature
# is equal to or greater than threshold value. See Hot, Cold, and
# Temperature-Change Interrupts for details.
# Register TMST2
# Thermistor register 2: hot/cold temperature setting,
# default cold=0C, hot=50C
def setTMST2(self, val):
"""Set register TMST2"""
self.write(REG.TMST2, val, 8)
def getTMST2(self):
"""Get register TMST2"""
return self.read(REG.TMST2, 8)
# Bits TMST_COLD
# Thermistor COLD threshold
# NOTE: An interrupt is issued when thermistor temperature is equal or less
# than COLD threshold
# temp = -7C + TMST_COLD
# Bits TMST_HOT
# Thermistor HOT threshold
# NOTE: An interrupt is issued when thermistor temperature is equal or greater
# than HOT threshold
# temp = 42C + TMST_HOT
# Register PG
# Power good status each rails
def setPG(self, val):
"""Set register PG"""
self.write(REG.PG, val, 8)
def getPG(self):
"""Get register PG"""
return self.read(REG.PG, 8)
# Bits VB_PG
# Positive boost converter power good. 1 - DCDC1 is in regulation
# Bits VDDH_PG
# VDDH power good. 1 - VDDH charge pump is in regulation
# Bits VN_PG
# Inverting buck-boost power good. 1 - DCDC2 is in regulation
# Bits VPOS_PG
# VPOS power good. 1 - LDO1(VPOS) is in regulation
# Bits VEE_PG
# VEE power good. 1 - VEE charge pump is in regulation
# Bits unused_0
# Bits VNEG_PG
# VNEG power good. 1 - LDO2(VNEG) is in regulation
# Bits unused_1
# Register REVID
# Device revision ID information
def setREVID(self, val):
"""Set register REVID"""
self.write(REG.REVID, val, 8)
def getREVID(self):
"""Get register REVID"""
return self.read(REG.REVID, 8)
# Bits MJREV
# Bits MNREV
# Bits VERSION
|
nilq/baby-python
|
python
|
import matplotlib
matplotlib.use('Agg')
import os
import argparse
import torch
import numpy as np
import pickle
import sys
sys.path.append('./utils')
from torch import optim
from torch import nn
from torch import multiprocessing
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import DataLoader, ConcatDataset
from utils.builders import SingleViewDepthTripletBuilder, MultiViewDepthTripletBuilder, \
MultiViewTripletBuilder, SingleViewMultiFrameTripletBuilder, MultiViewMultiFrameTripletBuilder
from utils.builder_utils import distance, Logger, ensure_folder, collate_fn, time_stamped
from utils.vocabulary import Vocabulary
from mftcn import define_model
from ipdb import set_trace
from sklearn.preprocessing import OneHotEncoder
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
from torchvision import transforms
import torchvision.models as models
from torchvision import datasets
from tensorboardX import SummaryWriter
from utils.plot_utils import plot_mean
sys.path.append('/home/max/projects/gps-lfd')
sys.path.append('/home/msieb/projects/gps-lfd')
from config import Config_Isaac_Server as Config # Import approriate config
conf = Config()
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"]= "0, 1,2,3"
ITERATE_OVER_TRIPLETS = 3
IMAGE_SIZE = conf.IMAGE_SIZE
EXP_NAME = conf.EXP_NAME
#EXP_DIR = os.path.join('/home/msieb/data/tcn_data/experiments', EXP_NAME)
#EXP_DIR = os.path.join('/home/msieb/projects/data/tcn_data/experiments', EXP_NAME)
EXP_DIR = conf.EXP_DIR
MODEL_FOLDER = conf.MODEL_FOLDER
SAMPLE_SIZE = 100
builder = MultiViewMultiFrameTripletBuilder
logdir = os.path.join('runs', MODEL_FOLDER, time_stamped())
print("logging to {}".format(logdir))
writer = SummaryWriter(logdir)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--start-epoch', type=int, default=0)
parser.add_argument('--epochs', type=int, default=1000)
parser.add_argument('--save-every', type=int, default=5)
parser.add_argument('--model-folder', type=str, default=EXP_DIR + 'trained_models/' + MODEL_FOLDER, time_stamped())
parser.add_argument('--load-model', type=str, required=False)
# parser.add_argument('--train-directory', type=str, default='./data/multiview-pouring/train/')
# parser.add_argument('--validation-directory', type=str, default='./data/multiview-pouring/val/')
parser.add_argument('--train-directory', type=str, default=EXP_DIR + 'videos/train/')
parser.add_argument('--validation-directory', type=str, default=EXP_DIR + 'videos/valid/')
parser.add_argument('--minibatch-size', type=int, default=4)
parser.add_argument('--margin', type=float, default=4.0)
parser.add_argument('--model-name', type=str, default='tcn-no-labels-mv')
parser.add_argument('--log-file', type=str, default='./out.log')
parser.add_argument('--lr-start', type=float, default=0.001)
parser.add_argument('--triplets-from-videos', type=int, default=5)
parser.add_argument('--n-views', type=int, default=2)
parser.add_argument('--alpha', type=float, default=0.001, help='weighing factor of language loss to triplet loss')
# parser.add_argument('--model_path', type=str, default='models/' , help='path for saving trained models')
# parser.add_argument('--crop_size', type=int, default=224 , help='size for randomly cropping images')
# parser.add_argument('--vocab_path', type=str, default='data/vocab.pkl', help='path for vocabulary wrapper')
# parser.add_argument('--image_dir', type=str, default='data/resized2014', help='directory for resized images')
# parser.add_argument('--caption_path', type=str, default='data/annotations/captions_train2014.json', help='path for train annotation json file')
# parser.add_argument('--log_step', type=int , default=10, help='step size for prining log info')
# parser.add_argument('--save_step', type=int , default=1000, help='step size for saving trained models')
# Model parameters
parser.add_argument('--embed_size', type=int , default=32, help='dimension of word embedding vectors')
parser.add_argument('--hidden_size', type=int , default=256, help='dimension of lstm hidden states')
parser.add_argument('--num_layers', type=int , default=1, help='number of layers in lstm')
# parser.add_argument('--num_epochs', type=int, default=5)
# parser.add_argument('--batch_size', type=int, default=128)
# parser.add_argument('--num_workers', type=int, default=2)
# parser.add_argument('--learning_rate', type=float, default=0.001)
return parser.parse_args()
args = get_args()
print(args)
builder = SingleViewMultiFrameTripletBuilder
n_prev_frames = 3
logger = Logger(args.log_file)
def batch_size(epoch, max_size):
exponent = epoch // 100
return min(max(2 ** (exponent), 2), max_size)
validation_builder = builder(args.n_views, n_prev_frames, args.validation_directory, IMAGE_SIZE, args, sample_size=SAMPLE_SIZE)
validation_set = [validation_builder.build_set() for i in range(5)]
validation_set = ConcatDataset(validation_set)
del validation_builder
def loss_fn(tcn, minibatch):
if USE_CUDA:
anchor_frames = minibatch[0].cuda()
anchor_poses = minibatch[1].cuda()
anchor_output, unnormalized, anchor_pose_pred = tcn(anchor_frames)
print(anchor_pose_predprint(anchor_pose_pred))
loss = torch.nn.MSELoss()(anchor_pose_pred, anchor_poses)
return loss
def validate(tcn, use_cuda, args):
# Run model on validation data and log results
data_loader = DataLoader(
validation_set,
batch_size=4,
shuffle=False,
pin_memory=use_cuda,
)
losses = []
for minibatch in data_loader:
# frames = Variable(minibatch, require_grad=False)
loss = loss_fn(tcn, minibatch)
losses.append(loss.data.cpu().numpy())
loss = np.mean(losses)
logger.info('val loss: ',loss)
message = "Validation score correct with margin {with_margin}/{total} and without margin {without_margin}/{total}".format(
with_margin=correct_with_margin,
without_margin=correct_without_margin,
total=len(validation_set)
)
logger.info(message)
return correct_with_margin, correct_without_margin, loss
def model_filename(model_name, epoch):
return "epoch-{epoch}.pk".format(model_name=model_name, epoch=epoch)
def save_model(model, filename, model_folder):
ensure_folder(model_folder)
model_path = os.path.join(model_folder, filename)
torch.save(model.state_dict(), model_path)
def build_set(queue, triplet_builder, log):
while 1:
datasets = []
for i in range(5):
dataset = triplet_builder.build_set()
datasets.append(dataset)
dataset = ConcatDataset(datasets)
# log.info('Created {0} triplets'.format(len(dataset)))
queue.put(dataset)
def create_model(use_cuda):
tcn = define_model(use_cuda)
# tcn = PosNet()
if args.load_model:
model_path = os.path.join(
args.model_folder,
args.load_model
)
# map_location allows us to load models trained on cuda to cpu.
tcn.load_state_dict(torch.load(model_path, map_location=lambda storage, loc: storage))
if use_cuda:
tcn = tcn.cuda()
return tcn
def main():
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
use_cuda = torch.cuda.is_available()
print(use_cuda)
tcn = create_model(use_cuda)
tcn = torch.nn.DataParallel(tcn, device_ids=range(torch.cuda.device_count()))
triplet_builder = builder(args.n_views, n_prev_frames, \
args.train_directory, IMAGE_SIZE, args, sample_size=SAMPLE_SIZE)
queue = multiprocessing.Queue(1)
dataset_builder_process = multiprocessing.Process(target=build_set, args=(queue, triplet_builder, logger), daemon=True)
dataset_builder_process.start()
optimizer = optim.SGD(tcn.parameters(), lr=args.lr_start, momentum=0.9)
# This will diminish the learning rate at the milestones.
# 0.1, 0.01, 0.001
learning_rate_scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[30, 50, 100], gamma=0.5)
criterion = nn.CrossEntropyLoss()
trn_losses_ = []
val_losses_= []
val_acc_margin_ = []
val_acc_no_margin_ = []
for epoch in range(args.start_epoch, args.start_epoch + args.epochs):
print("=" * 20)
logger.info("Starting epoch: {0} learning rate: {1}".format(epoch,
learning_rate_scheduler.get_lr()))
learning_rate_scheduler.step()
dataset = queue.get()
data_loader = DataLoader(
dataset=dataset,
batch_size=args.minibatch_size, # batch_size(epoch, args.max_minibatch_size),
shuffle=True,
pin_memory=use_cuda,
)
for _ in range(0, ITERATE_OVER_TRIPLETS):
losses = []
set_trace()
for minibatch, _ in data_loader:
# frames = Variable(minibatch)
if use_cuda:
frames = minibatch.cuda()
anchor_frames = frames[:, :, 0, :, :, :]
positive_frames = frames[:, :, 1, :, :, :]
negative_frames = frames[:, :, 2, :, :, :]
anchor_output, unnormalized, _ = tcn(anchor_frames)
positive_output, _, _ = tcn(positive_frames)
negative_output, _, _ = tcn(negative_frames)
d_positive = distance(anchor_output, positive_output)
d_negative = distance(anchor_output, negative_output)
loss_triplet = torch.clamp(args.margin + d_positive - d_negative, min=0.0).mean()
loss = loss_triplet
print(loss)
losses.append(loss.data.cpu().numpy())
optimizer.zero_grad()
loss.backward()
optimizer.step()
trn_losses_.append(np.mean(losses))
logger.info('train loss: ', np.mean(losses))
if epoch % 1 == 0:
acc_margin, acc_no_margin, loss = validate(tcn, use_cuda, args)
val_losses_.append(loss)
val_acc_margin_.append(acc_margin)
val_acc_no_margin_.append(acc_no_margin)
if epoch % args.save_every == 0 and epoch != 0:
logger.info('Saving model.')
save_model(tcn, model_filename(args.model_name, epoch), args.model_folder)
plot_mean(trn_losses_, args.model_folder, 'train_loss')
plot_mean(val_losses_, args.model_folder, 'validation_loss')
# plot_mean(train_acc_, args.model_folder, 'train_acc')
plot_mean(val_acc_margin_, args.model_folder, 'validation_accuracy_margin')
plot_mean(val_acc_no_margin_, args.model_folder, 'validation_accuracy_no_margin')
if __name__ == '__main__':
main()
|
nilq/baby-python
|
python
|
from .duration import Duration
from .numeric import Numeric
from .rate import Rate
from .size import Size
|
nilq/baby-python
|
python
|
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
fams = UnwrapElement(IN[0])
ptypes = list()
for fam in fams:
if fam.GetType().ToString() == "Autodesk.Revit.DB.Family":
ptypes.append(fam.FamilyPlacementType)
else: ptypes.append(None)
OUT = ptypes
|
nilq/baby-python
|
python
|
from sys import stdin, stdout
from operator import itemgetter
cases = int(stdin.readline())
for c in range(cases):
text = stdin.readline().strip().lower()
text = [ch for ch in text if ch.isalpha()]
freq = {}
max_f = 0
for ch in text:
if not ch in freq:
freq[ch] = 1
else:
freq[ch] += 1
if freq[ch] > max_f:
max_f = freq[ch]
chars = []
for key, f in freq.items():
if f == max_f:
chars.append(key)
stdout.write("{}\n".format("".join(sorted(chars))))
|
nilq/baby-python
|
python
|
from temboo.Library.LinkedIn.PeopleAndConnections.GetMemberProfile import GetMemberProfile, GetMemberProfileInputSet, GetMemberProfileResultSet, GetMemberProfileChoreographyExecution
|
nilq/baby-python
|
python
|
import time
import asyncio
import concurrent.futures
from functools import partial
def a():
time.sleep(1)
return 'A'
async def b():
await asyncio.sleep(1)
return 'B'
async def c():
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, a)
def show_perf(func):
print('*' * 20)
start = time.perf_counter()
asyncio.run(func())
print(f'{func.__name__} Cost: {time.perf_counter() - start}')
async def c1():
loop = asyncio.get_running_loop()
await asyncio.gather(
loop.run_in_executor(None, a),
b()
)
async def c2():
await asyncio.gather(b(), c())
async def c3():
loop = asyncio.get_running_loop()
with concurrent.futures.ProcessPoolExecutor() as e:
print(await asyncio.gather(
loop.run_in_executor(e, a),
b()
))
if __name__ == '__main__':
for f in (c1, c2, c3):
show_perf(f)
|
nilq/baby-python
|
python
|
import logging
def _cancel_pending_orders(client, orders):
pending = [(order['variety'], order['order_id'])
for order in orders if 'OPEN' in order['status']]
# ToDo: if doesn't work in time, try to run it async.
for p in pending:
try:
order_id = client.cancel_order(*p)
logging.info('Order {} was canceled'.format(order_id))
except Exception as err:
logging.error(err)
def _handle_quantity_diff(client, quantity_diff):
if quantity_diff > 0:
try:
order_id = client.place_order(
variety=client.VARIETY_REGULAR,
tradingsymbol='SBIN',
exchange=client.EXCHANGE_NSE,
transaction_type=client.TRANSACTION_TYPE_SELL,
quantity=quantity_diff,
order_type=client.ORDER_TYPE_MARKET,
product=client.PRODUCT_MIS)
logging.info("Order placed. ID is: {}".format(order_id))
except Exception as err:
logging.error(err)
elif quantity_diff < 0:
try:
order_id = client.place_order(
variety=client.VARIETY_REGULAR,
tradingsymbol='SBIN',
exchange=client.EXCHANGE_NSE,
transaction_type=client.TRANSACTION_TYPE_BUY,
quantity=abs(quantity_diff),
order_type=client.ORDER_TYPE_MARKET,
product=client.PRODUCT_MIS)
logging.info("Order placed. ID is: {}".format(order_id))
except Exception as err:
logging.error(err)
def mis_mode(server):
client = server.kite_loginer.get_client()
kite_trader = server.kite
def tick():
orders = client.orders()
_cancel_pending_orders(client, orders)
positions = client.positions()
quantity_diff = positions['day'][0]['day_buy_quantity'] - positions['day'][0]['day_sell_quantity']
_handle_quantity_diff(client, quantity_diff)
kite_trader.reset()
# logging.info('Kite Agent balance and inventory were reset to {}, {}'.format(
# kite_trader.balance, kite_trader.inventory))
#104 for improvised def_reset() function in the kite_8.py
logging.info('Kite Agent balance and inventory were reset to {}, {}, {}, {}, {}, {}'.format(
kite_trader.balance, kite_trader.inventory, kite_trader._queue, kite_trader.buy_price_queue, kite_trader.sell_price_queue, kite_trader.actions_queue))
return tick
|
nilq/baby-python
|
python
|
import appdaemon.plugins.hass.hassapi as hass
import os
import glob
import random
#
# A helper app providing random template selection and rendering.
#
# This app could be used by Smart Assistants to provide some "randomness" in the assistant words.
#
# noinspection PyAttributeOutsideInit
class AssistantTemplate(hass.Hass):
def initialize(self):
self.add_namespace('assistant')
self.register_service('assistant/template', self.template_service, namespace='assistant')
self.language = self.args['language']
self.templates_path = self.args['templates_path']
self.log("Assistant Template support started for language %s", self.language, level='INFO')
def template_service(self, namespace, domain, service, data):
self.log("Service: %r", data, level='DEBUG')
tmpl_name = data['template']
tmpl_variables = data.get('variables')
tmpl_dir = self.template_dirname(tmpl_name)
if os.path.isdir(tmpl_dir):
tmpl_file = self.select_template(tmpl_dir)
else:
tmpl_file = self.template_filename(tmpl_name)
return self.render_template_file(tmpl_file, tmpl_variables)
# noinspection PyMethodMayBeStatic
def select_template(self, tmpl_dir):
return random.choice(glob.glob(os.path.join(tmpl_dir, '*.jinja2')))
def render_template_file(self, tmpl_file, variables):
with open(tmpl_file, mode='r', encoding='utf-8') as tmpl_fp:
tmpl_content = tmpl_fp.read()
self.log('Rendering template text: %r', tmpl_content)
return self.call_service('template/render',
template=tmpl_content,
variables=variables,
namespace='hass')
def template_dirname(self, name):
return os.path.join(self.templates_path, self.language, name)
def template_filename(self, name):
return os.path.join(self.templates_path, self.language, name + '.jinja2')
|
nilq/baby-python
|
python
|
#!/usr/bin/env python
################################################################
#
# osm.py - Obsidian Settings Manager
# Copyright 2021 Peter Kaminski. Licensed under MIT License.
# https://github.com/peterkaminski/obsidian-settings-manager
#
################################################################
VERSION = 'v0.3.0'
APPNAME = 'Obsidian Settings Manager'
import argparse
import datetime
import json
import shutil
import subprocess
import sys
import traceback
from pathlib import Path
# set up argparse
def init_argparse():
# TODO: make "action" flags (list, update, execute, etc.) mutually exclusive
parser = argparse.ArgumentParser(description='Manage Obsidian settings across multiple vaults.')
parser.add_argument('--list', '-l', action='store_true', help='list Obsidian vaults')
parser.add_argument('--update', '-u', help='update Obsidian vaults from UPDATE vault')
parser.add_argument('--rm', action='store_true', help='with --update, remove .obsidian and create again, rather than retain old .obsidian files')
parser.add_argument('--execute', '-x', help='run EXECUTE command within each vault (use caution!)')
parser.add_argument('--backup-list', action='store_true', help='list ISO 8601-formatted .obsidian backup files from all vaults')
parser.add_argument('--backup-remove', action='store_true', help='remove ISO 8601-formatted .obsidian backup files from all vaults')
parser.add_argument('--version', '-v', action='store_true', help='show version and exit')
return parser
# find all the vaults Obsidian is tracking
def get_vault_paths():
vault_paths = []
# read primary file
# location per https://help.obsidian.md/Advanced+topics/How+Obsidian+stores+data#System+directory
# (TODO: should be parameterized and support other OSes)
with open(Path.home() / 'Library/Application Support/obsidian/obsidian.json') as infile:
obsidian = json.load(infile)
vaults = obsidian['vaults']
for vault in vaults:
# skip Help or other system directory vaults
# TODO: support other OSes
if Path(vaults[vault]['path']).parent == Path.home() / 'Library/Application Support/obsidian':
continue
vault_paths.append(vaults[vault]['path'])
# sort paths (case-insensitive)
vault_paths.sort(key=str.lower)
# return paths
return vault_paths
# helper for `copy_settings()`
# does nothing if `src` does not exist
def copy_settings_file(datestring, src, dest, filename):
src_target = Path(src) / filename
dest_target = Path(dest) / filename
if src_target.exists():
if dest_target.exists():
dest_target.rename(str(dest_target)+datestring)
shutil.copy2(str(src_target), str(dest_target))
# helper for `copy_settings()`
# does nothing if `src` does not exist
def copy_settings_dir(datestring, src, dest, dirname):
src_target = Path(src) / dirname
dest_target = Path(dest) / dirname
if src_target.exists():
if dest_target.exists():
dest_target.rename(str(dest_target)+datestring)
shutil.copytree(str(src_target), dest_target)
# copy the usual settings files from `src` to `dest`
# `dest` is backed up to same filename with a ISO 8601-style
# date string ('2021-05-23T23:38:32.509386Z') in UTC appended,
# unless `--rm` is given
def copy_settings(src, dest, args):
# don't operate on self
if str(src) == str(dest):
return
print(f"Copying '{src}' configuration to '{dest}'")
# expand src and dest
src = Path(src) / '.obsidian'
dest = Path(dest) / '.obsidian'
# get current date/time
datestring = f"-{datetime.datetime.utcnow().isoformat()}Z"
# if --rm, remove and recreate .obsidian
if args.rm:
shutil.rmtree(str(dest), ignore_errors=True)
dest.mkdir()
# copy config
copy_settings_file(datestring, src, dest, 'config')
# copy starred.json
copy_settings_file(datestring, src, dest, 'starred.json')
# copy file used for vaults distributed to others via git
copy_settings_file(datestring, src, dest, 'README.md')
# copy plugins
copy_settings_dir(datestring, src, dest, 'plugins')
# copy snippets
copy_settings_dir(datestring, src, dest, 'snippets')
def backup_list_remove(vault_path, args):
dir_path = Path(vault_path) / '.obsidian'
iso_8601_glob = '*-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9]*Z'
for dest in dir_path.glob(iso_8601_glob):
if args.backup_list:
print(dest)
elif args.backup_remove:
if dest.is_file():
dest.unlink()
elif dest.is_dir():
shutil.rmtree(str(dest), ignore_errors=True)
def main():
# set up argparse
argparser = init_argparse();
args = argparser.parse_args();
# do stuff
try:
vault_paths = get_vault_paths()
# decide what to do
if args.version:
print(f'{APPNAME} {VERSION}')
elif args.list:
for vault_path in vault_paths:
print(Path(vault_path).relative_to(Path.home()))
elif args.update:
# TODO: check if given UPDATE vault is really an Obsidian vault
for vault_path in vault_paths:
copy_settings(Path.home() / args.update, vault_path, args)
elif args.backup_list or args.backup_remove:
for vault_path in vault_paths:
backup_list_remove(vault_path, args)
elif args.execute:
for vault_path in vault_paths:
print(f'\n# {vault_path}\n')
p = subprocess.Popen(args.execute, cwd=vault_path, shell=True)
p.wait()
else:
argparser.print_help(sys.stderr)
except Exception:
traceback.print_exc()
if __name__ == "__main__":
exit(main())
|
nilq/baby-python
|
python
|
import os
import argparse
from misc import date_str, get_dir
def model_args():
parser = argparse.ArgumentParser()
# Paths
parser.add_argument('--train_dir',
help='Directory of train data',
default='./data/poetryDB/txt/')
# parser.add_argument('--test_dir',
# help='Directory of test data',
# default='./data/bitmoji/test')
parser.add_argument('--save_dir',
help='Directory to save logs and model checkpoints',
default=os.path.join('.', 'save', date_str()))
parser.add_argument('--load_path',
help='Path of the model checkpoint to load')
parser.add_argument('--data_reader_path',
help='Path to save/load the DataReader object',
default=os.path.join('.', 'save', 'reader.pkl'))
# Model Architecture
parser.add_argument('--cell_size',
help='Minibatch size',
default=256,
type=int)
parser.add_argument('--num_layers',
help='Minibatch size',
default=3,
type=int)
# Hyperparams
parser.add_argument('--batch_size',
help='Minibatch size',
default=128,
type=int)
parser.add_argument('--seq_len',
help='Sequence length (the number of tokens in each element of the batch)',
default=20,
type=int)
parser.add_argument('--lr',
help='Learning rate',
default=1e-3,
type=float)
parser.add_argument('--lr_decay_steps',
help='The number of steps over which to decay by a multiple of lr_decay_rate',
default=200,
type=int)
parser.add_argument('--lr_decay_rate',
help='The multiple by which to decay the learning rate every lr_decay_steps steps',
default=0.9,
type=float)
parser.add_argument('--keep_prob',
help='The keep probability for dropout (always 1 for testing)',
default=0.5,
type=float)
# Training
parser.add_argument('--max_steps',
help='Max number of steps to train',
default=30000,
type=int)
parser.add_argument('--summary_freq',
help='Frequency (in steps) with which to write tensorboard summaries',
default=100,
type=int)
parser.add_argument('--model_save_freq',
help='Frequency (in steps) with which to save the model',
default=1000,
type=int)
parser.add_argument('--inference_freq',
help='Frequency (in steps) with which to perform inference',
default=100,
type=int)
# Inference
parser.add_argument('--inference',
help="Use the model to generate new text.",
action='store_true')
parser.add_argument('--argmax',
help="Use argmax to choose the next word, rather than sampling.",
action='store_true')
parser.add_argument('--max_gen_len',
help="The maximum number of words to generate.",
default=20,
type=int)
parser.add_argument('--primer',
help="The priming text to use for inference. Random if not supplied",
default=None)
# System
parser.add_argument('--gpu',
help='Comma separated list of GPU(s) to use.')
args = parser.parse_args()
if args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
get_dir(args.save_dir)
return args
def export_args():
parser = argparse.ArgumentParser()
parser.add_argument('--export_dir',
help='Directory to save the data',
default='save/serving/')
parser.add_argument('--load_path',
help='Path of the model checkpoint to load',
default='save/hal-3layer/model-9001')
parser.add_argument('--version',
help='Version of the model to save',
default=0,
type=int)
parser.add_argument('--data_reader_path',
help='Path to save/load the DataReader object',
default=os.path.join('.', 'save', 'reader.pkl'))
# Model Architecture
parser.add_argument('--cell_size',
help='Minibatch size',
default=256,
type=int)
parser.add_argument('--num_layers',
help='Minibatch size',
default=3,
type=int)
# Hyperparams
parser.add_argument('--batch_size',
help='Minibatch size',
default=128,
type=int)
parser.add_argument('--seq_len',
help='Sequence length (the number of tokens in each element of the batch)',
default=20,
type=int)
parser.add_argument('--keep_prob',
help='The keep probability for dropout (always 1 for testing)',
default=1,
type=float)
parser.add_argument('--lr',
help='Learning rate',
default=1e-3,
type=float)
parser.add_argument('--lr_decay_steps',
help='The number of steps over which to decay by a multiple of lr_decay_rate',
default=200,
type=int)
parser.add_argument('--lr_decay_rate',
help='The multiple by which to decay the learning rate every lr_decay_steps steps',
default=0.9,
type=float)
# Inference
parser.add_argument('--argmax',
help="Use argmax to choose the next word, rather than sampling.",
action='store_true')
parser.add_argument('--max_gen_len',
help="The maximum number of words to generate.",
default=20,
type=int)
args = parser.parse_args()
return args
|
nilq/baby-python
|
python
|
# Copyright 2020 Mark Dickinson. 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
#
# https://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.
"""
The CFGraph class provides the graph structure for the control-flow graph.
Conceptually, our graph is very similar to a DFA graph for a regular
expression. It consists of:
- a set of nodes
- for each node, a set of edge labels (strings)
- for each node and edge label, a target node
The set of operations that can mutate the graph is very limited:
- a new node can be added, together with edges to existing nodes
- an isolated node can be removed
- a node with no outgoing edges can be identified with another node
Parallel edges (with different labels) and self-loops are permitted.
Nodes can be any hashable object.
"""
from typing import Container, Dict, Mapping, Optional, Set, Tuple, TypeVar
#: Type of nodes. For now, require only that nodes are hashable.
NodeType = TypeVar("NodeType")
class CFGraph(Container[NodeType]):
"""
The directed graph underlying the control flow graph.
"""
#: The collection of nodes.
_nodes: Set[NodeType]
#: Mapping from source node and edge label to target node.
_edges: Dict[NodeType, Dict[str, NodeType]]
#: Mapping from target node to collection of (source node, edge) pairs.
_backedges: Dict[NodeType, Set[Tuple[NodeType, str]]]
def __init__(self) -> None:
self._nodes = set()
self._edges = {}
self._backedges = {}
# Functions that change the state of the graph.
def add_node(
self,
node: NodeType,
*,
edges: Optional[Mapping[str, NodeType]] = None,
) -> None:
"""
Add a new node, along with edges to existing nodes to the graph.
Parameters
----------
node
The node to be added to the graph.
edges
Edges from the given node, if any, provided as a mapping
from edge labels (strings) to target nodes. The target nodes
should already be in the graph.
Raises
------
ValueError
If the given node is already in the graph, or if any of the target
nodes for edges are not already in the graph.
"""
if node in self:
raise ValueError(f"node {node} is already present in the graph")
self._add_node(node)
if edges is not None:
for label, target in edges.items():
if target not in self or target == node:
raise ValueError(
f"target {target} for edge {label} is not in the graph"
)
self._add_edge(node, label, target)
def remove_node(self, node: NodeType) -> None:
"""
Remove an isolated node from the graph.
Fails if there are edges to or from that node: all edges must be removed
before it's possible to remove the node itself.
Parameters
----------
node: NodeType
The node to be removed.
Raises
------
ValueError
If the node is not in the graph, or if the node is not isolated.
"""
if node not in self._nodes:
raise ValueError(f"node {node} not present in the graph")
if self._edges[node]:
raise ValueError(f"node {node} is not isolated: it has forward edges")
if self._backedges[node]:
raise ValueError(f"node {node} is not isolated: it has back edges")
self._nodes.remove(node)
def collapse_node(self, dummy: NodeType, target: NodeType) -> None:
"""
Identify two nodes.
Identifies the *dummy* node with the *target* node, and removes the
*dummy* node from the graph. The dummy node should not have any outward
edges.
Note that this is the only mechanism for introducing cycles into the graph.
Parameters
----------
dummy
Node to be collapsed and removed
target
Node to identify *dummy* with
Raises
------
ValueError
If *dummy* has any outward edges, or if either of dummy or target is not
in the graph.
"""
if dummy not in self:
raise ValueError(f"node {dummy} is not in the graph")
if target not in self:
raise ValueError(f"node {target} is not in the graph")
if self._edges[dummy]:
raise ValueError(f"node {dummy} has outward edges")
if dummy == target:
raise ValueError(f"nodes {dummy} and {target} must be distinct")
edges_to_dummy = self.edges_to(dummy)
for source, label in edges_to_dummy.copy():
self._remove_edge(source, label)
self._add_edge(source, label, target)
self.remove_node(dummy)
# Functions for examining or traversing the graph.
def edge(self, source: NodeType, label: str) -> NodeType:
"""
Get the target of a given edge.
"""
return self._edges[source][label]
def edge_labels(self, source: NodeType) -> Set[str]:
"""
Get labels of all edges.
"""
return set(self._edges[source].keys())
def edges_to(self, target: NodeType) -> Set[Tuple[NodeType, str]]:
"""
Set of pairs (source, label) representing edges to this node.
"""
return self._backedges[target]
# Support for membership testing
def __contains__(self, node: object) -> bool:
"""
Determine whether a given node is contained in the graph.
"""
return node in self._nodes
# Low-level functions
def _add_node(self, node: NodeType) -> None:
"""
Add a node to the graph. Raises ValueError on an attempt to add a node that's
already in the graph.
"""
assert node not in self._nodes
self._nodes.add(node)
self._edges[node] = {}
self._backedges[node] = set()
def _add_edge(self, source: NodeType, label: str, target: NodeType) -> None:
"""
Add a labelled edge to the graph.
"""
assert label not in self._edges[source]
self._edges[source][label] = target
assert (source, label) not in self._backedges[target]
self._backedges[target].add((source, label))
def _remove_edge(self, source: NodeType, label: str) -> None:
"""
Remove a labelled edge from the graph.
"""
target = self._edges[source][label]
self._backedges[target].remove((source, label))
self._edges[source].pop(label)
|
nilq/baby-python
|
python
|
import logging
import os
import sys
import pandas as pd
import re
from collections import OrderedDict
import numpy as np
import argparse
import zipfile
import paramiko
import time
from sqlalchemy.exc import IntegrityError
from dataactcore.models.domainModels import DUNS
from dataactcore.interfaces.db import GlobalDB
from dataactcore.logging import configure_logging
from dataactvalidator.health_check import create_app
from dataactvalidator.scripts.loaderUtils import clean_data, insert_dataframe
from dataactcore.config import CONFIG_BROKER
logger = logging.getLogger(__name__)
REMOTE_SAM_DIR = '/current/SAM/2_FOUO/UTF-8/'
def get_config():
sam_config = CONFIG_BROKER.get('sam_duns')
if sam_config:
return sam_config.get('username'), sam_config.get('password'), sam_config.get('host'), \
sam_config.get('port')
return None, None, None, None, None
def get_relevant_models(data, benchmarks=False):
# Get a list of the duns we're gonna work off of to prevent multiple calls to the database
if benchmarks:
get_models = time.time()
logger.info("Getting relevant models")
duns_found = [duns.strip().zfill(9) for duns in list(data["awardee_or_recipient_uniqu"].unique())]
dun_objects_found = sess.query(DUNS).filter(DUNS.awardee_or_recipient_uniqu.in_(duns_found))
models = {duns.awardee_or_recipient_uniqu: duns for duns in dun_objects_found}
logger.info("Getting models with activation dates already set")
activated_models = {duns_num: duns for duns_num, duns in models.items() if duns.activation_date is not None}
if benchmarks:
logger.info("Getting models took {} seconds".format(time.time() - get_models))
return models, activated_models
def load_duns_by_row(data, sess, models, activated_models, benchmarks=False):
# data = activation_check(data, activated_models, benchmarks).where(pd.notnull(data), None)
update_duns(models, data, benchmarks=benchmarks)
sess.add_all(models.values())
# Removed this function when adding registration_date
# def activation_check(data, activated_models, benchmarks=False):
# # if activation_date's already set, keep it, otherwise update it (default)
# logger.info("going through activation check")
# if benchmarks:
# activation_check_start = time.time()
# lambda_func = (lambda duns_num: pd.Series([activated_models[duns_num].activation_date
# if duns_num in activated_models else np.nan]))
# data = data.assign(old_activation_date=data["awardee_or_recipient_uniqu"].apply(lambda_func))
# data.loc[pd.notnull(data["old_activation_date"]), "activation_date"] = data["old_activation_date"]
# del data["old_activation_date"]
# if benchmarks:
# logger.info("Activation check took {} seconds".format(time.time()-activation_check_start))
# return data
def update_duns(models, new_data, benchmarks=False):
"""Modify existing models or create new ones"""
logger.info("Updating duns")
if benchmarks:
update_duns_start = time.time()
for _, row in new_data.iterrows():
awardee_or_recipient_uniqu = row['awardee_or_recipient_uniqu']
if awardee_or_recipient_uniqu not in models:
models[awardee_or_recipient_uniqu] = DUNS()
for field, value in row.items():
setattr(models[awardee_or_recipient_uniqu], field, value)
if benchmarks:
logger.info("Updating duns took {} seconds".format(time.time() - update_duns_start))
def clean_sam_data(data):
return clean_data(data, DUNS, {
"awardee_or_recipient_uniqu": "awardee_or_recipient_uniqu",
"activation_date": "activation_date",
"deactivation_date": "deactivation_date",
"registration_date": "registration_date",
"expiration_date": "expiration_date",
"last_sam_mod_date": "last_sam_mod_date",
"sam_extract_code": "sam_extract_code",
"legal_business_name": "legal_business_name"
}, {})
def parse_sam_file(file_path, sess, monthly=False, benchmarks=False):
parse_start_time = time.time()
logger.info("Starting file " + str(file_path))
dat_file_name = os.path.splitext(os.path.basename(file_path))[0]+'.dat'
sam_file_type = "MONTHLY" if monthly else "DAILY"
dat_file_date = re.findall(".*{}_(.*).dat".format(sam_file_type), dat_file_name)[0]
with create_app().app_context():
column_header_mapping = {
"awardee_or_recipient_uniqu": 0,
"sam_extract_code": 4,
"registration_date": 6,
"expiration_date": 7,
"last_sam_mod_date": 8,
"activation_date": 9,
"legal_business_name": 10
}
column_header_mapping_ordered = OrderedDict(sorted(column_header_mapping.items(), key=lambda c: c[1]))
# Initial sweep of the file to see rows and possibly what DUNS we're updating
if benchmarks:
initial_sweep = time.time()
nrows = 0
with zipfile.ZipFile(file_path) as zip_file:
with zip_file.open(dat_file_name) as dat_file:
nrows = len(dat_file.readlines())
if benchmarks:
logger.info("Initial sweep took {} seconds".format(time.time() - initial_sweep))
block_size = 10000
batches = nrows//block_size
# skip the first line again if the last batch is also the first batch
skiplastrows = 2 if batches == 0 else 1
last_block_size = (nrows % block_size)-skiplastrows
batch = 0
added_rows = 0
while batch <= batches:
skiprows = 1 if batch == 0 else (batch*block_size)
nrows = (((batch+1)*block_size)-skiprows) if (batch < batches) else last_block_size
logger.info('Loading rows %s to %s', skiprows+1, nrows+skiprows)
with zipfile.ZipFile(file_path) as zip_file:
with zip_file.open(dat_file_name) as dat_file:
csv_data = pd.read_csv(dat_file, dtype=str, header=None, skiprows=skiprows, nrows=nrows, sep='|',
usecols=column_header_mapping_ordered.values(),
names=column_header_mapping_ordered.keys(), quoting=3)
# add deactivation_date column for delete records
lambda_func = (lambda sam_extract: pd.Series([dat_file_date if sam_extract == "1" else np.nan]))
csv_data = csv_data.assign(deactivation_date=pd.Series([np.nan], name='deactivation_date')
if monthly else csv_data["sam_extract_code"].apply(lambda_func))
# removing rows where DUNS number isn't even provided
csv_data = csv_data.where(csv_data["awardee_or_recipient_uniqu"].notnull())
# cleaning and replacing NaN/NaT with None's
csv_data = clean_sam_data(csv_data.where(pd.notnull(csv_data), None))
if monthly:
logger.info("Adding all monthly data with bulk load")
if benchmarks:
bulk_month_load = time.time()
del csv_data["sam_extract_code"]
insert_dataframe(csv_data, DUNS.__table__.name, sess.connection())
if benchmarks:
logger.info("Bulk month load took {} seconds".format(time.time()-bulk_month_load))
else:
add_data = csv_data[csv_data.sam_extract_code == '2']
update_delete_data = csv_data[(csv_data.sam_extract_code == '3') |
(csv_data.sam_extract_code == '1')]
for dataframe in [add_data, update_delete_data]:
del dataframe["sam_extract_code"]
if not add_data.empty:
try:
logger.info("Attempting to bulk load add data")
insert_dataframe(add_data, DUNS.__table__.name, sess.connection())
except IntegrityError:
logger.info("Bulk loading add data failed, loading add data by row")
sess.rollback()
models, activated_models = get_relevant_models(add_data, benchmarks=benchmarks)
logger.info("Loading add data ({} rows)".format(len(add_data.index)))
load_duns_by_row(add_data, sess, models, activated_models, benchmarks=benchmarks)
if not update_delete_data.empty:
models, activated_models = get_relevant_models(update_delete_data, benchmarks=benchmarks)
logger.info("Loading update_delete data ({} rows)".format(len(update_delete_data.index)))
load_duns_by_row(update_delete_data, sess, models, activated_models, benchmarks=benchmarks)
sess.commit()
added_rows += nrows
batch += 1
logger.info('%s DUNS records inserted', added_rows)
if benchmarks:
logger.info("Parsing {} took {} seconds with {} rows".format(dat_file_name, time.time()-parse_start_time,
added_rows))
def process_from_dir(root_dir, file_name, sess, local, monthly=False, benchmarks=False):
file_path = os.path.join(root_dir, file_name)
if not local:
logger.info("Pulling {}".format(file_name))
with open(file_path, "wb") as zip_file:
sftp.getfo(''.join([REMOTE_SAM_DIR, '/', file_name]), zip_file)
parse_sam_file(file_path, sess, monthly=monthly, benchmarks=benchmarks)
if not local:
os.remove(file_path)
def get_parser():
parser = argparse.ArgumentParser(description="Get the latest data from SAM and update duns table. By default, it "
"loads the latest daily file.")
parser.add_argument("--historic", "-i", action="store_true", help='load the oldest monthly zip and all the daily'
'files afterwards from the directory.')
parser.add_argument("--local", "-l", type=str, default=None, help='work from a local directory')
parser.add_argument("--monthly", "-m", type=str, default=None, help='load a local monthly file')
parser.add_argument("--daily", "-d", type=str, default=None, help='load a local daily file')
parser.add_argument("--benchmarks", "-b", action="store_true",
help='log times of operations for testing')
parser.add_argument("--update", "-u", action="store_true",
help='Run all daily files since latest last_sam_mod_date in table')
return parser
if __name__ == '__main__':
parser = get_parser()
args = parser.parse_args()
historic = args.historic
local = args.local
monthly = args.monthly
daily = args.daily
benchmarks = args.benchmarks
update = args.update
with create_app().app_context():
configure_logging()
sess = GlobalDB.db().session
if monthly and daily:
logger.error("For loading a single local file, you must provide either monthly or daily.")
sys.exit(1)
if historic and update:
logger.error("For multiple file loads you must choose either historic or update.")
sys.exit(1)
elif (monthly or daily) and local:
logger.error("Local directory specified with a local file.")
sys.exit(1)
elif monthly:
parse_sam_file(monthly, sess=sess, monthly=True, benchmarks=benchmarks)
elif daily:
parse_sam_file(daily, sess=sess, benchmarks=benchmarks)
else:
# dealing with a local or remote directory
if not local:
root_dir = CONFIG_BROKER["d_file_storage_path"]
username, password, host, port = get_config()
if None in (username, password):
logger.error("Missing config elements for connecting to SAM")
sys.exit(1)
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname=host,
username=username,
password=password
)
sftp = client.open_sftp()
# dirlist on remote host
dirlist = sftp.listdir(REMOTE_SAM_DIR)
else:
root_dir = local
dirlist = os.listdir(local)
# generate chronological list of daily and monthly files
sorted_monthly_file_names = sorted([monthly_file for monthly_file in dirlist
if re.match(".*MONTHLY_\d+\.ZIP", monthly_file.upper())])
sorted_daily_file_names = sorted([daily_file for daily_file in dirlist
if re.match(".*DAILY_\d+\.ZIP", daily_file.upper())])
if historic or update:
if historic:
if sorted_monthly_file_names:
process_from_dir(root_dir, sorted_monthly_file_names[0],
sess, local, monthly=True, benchmarks=benchmarks)
else:
logger.info("No monthly file found.")
if sorted_daily_file_names:
if historic:
if sorted_monthly_file_names:
earliest_daily_file = sorted_monthly_file_names[0].replace("MONTHLY", "DAILY")
else:
# Insert item into sorted file list with date of last sam mod
last_update = sess.query(DUNS.last_sam_mod_date).\
order_by(DUNS.last_sam_mod_date.desc()). \
filter(DUNS.last_sam_mod_date.isnot(None)). \
limit(1).one()[0].strftime("%Y%m%d")
earliest_daily_file = re.sub("_DAILY_[0-9]{8}\.ZIP", "_DAILY_" +
last_update + ".ZIP", sorted_daily_file_names[0])
if earliest_daily_file:
sorted_full_list = sorted(sorted_daily_file_names + [earliest_daily_file])
daily_files_after = sorted_full_list[sorted_full_list.index(earliest_daily_file) + 1:]
else:
daily_files_after = sorted_daily_file_names
if daily_files_after:
for daily_file in daily_files_after:
process_from_dir(root_dir, daily_file, sess, local, benchmarks=benchmarks)
else:
logger.info("No daily file found.")
else:
if sorted_daily_file_names:
process_from_dir(root_dir, sorted_daily_file_names[-1], sess, local, benchmarks=benchmarks)
else:
logger.info("No daily file found.")
sess.close()
|
nilq/baby-python
|
python
|
import os , csv
# relative path to find the csv file
os.chdir(os.path.abspath(os.path.dirname(__file__)))
path = os.getcwd()
my_path = os.path.join('.', 'Resources', 'budget_data.csv')
#defining our variables
totalMonths = 0
total = 0
averageChange = 0
greatestIncrease = 0
greatestDecrease = 0
#extra variables used during for loops
lastValue = 0
bigCheck = 0
smallCheck = 0
with open(my_path) as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
header = next(csvreader)
for row in csvreader:
#counts the number of rows
totalMonths += 1
#adds up total profit
total += int(row[1])
#if the difference between the current rows profit and that of the previous row is greater than the script has seen
#it will save the pertinent information to the greatestIncrease variable and update the bigCheck variable
if int(row[1]) - lastValue > bigCheck:
greatestIncrease = row[0] + ' ($' + str(int(row [1]) - lastValue) +')'
bigCheck = int(row[1]) - lastValue
#Same except for small differences
if int(row[1]) - lastValue < smallCheck:
greatestDecrease = row[0] + ' ($' + str(int(row [1]) - lastValue) + ')'
smallCheck = int(row[1]) - lastValue
lastValue = int(row[1])
#Takes the final value for profit/losses and subtracts the first value from it, then divides that by the total months to find the average change
with open(my_path) as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
header = next(csvreader)
test = list(csvreader)
averageChange = (int(test[85][1]) - int(test[0][1])) / totalMonths
#prints out the analysis
print(f'''
Financial Analysis
----------------------
Total Months: {totalMonths}
Total: ${total}
Average Change: ${averageChange}
Greatest Increase in Profits: {greatestIncrease}
Greatest Decrease in Profits: {greatestDecrease}
''')
#saves to a text document
file1 = open("./Analysis/analysis.txt", "w")
file1.write(f'''
Financial Analysis
----------------------
Total Months: {totalMonths}
Total: ${total}
Average Change: ${averageChange}
Greatest Increase in Profits: {greatestIncrease}
Greatest Decrease in Profits: {greatestDecrease}
''')
|
nilq/baby-python
|
python
|
import numpy as np
ip_list=[int(x) for x in input().split()]
ip_list=np.asfarray(ip_list)
def listmul(ip_list):
op_list=[]
for i in range(0,len(ip_list)):
temp=1
for j in range(0,len(ip_list)):
if i!=j:
temp=temp*ip_list[j]
op_list.append(temp)
return op_list
op_list = listmul(ip_list)
print(op_list)
|
nilq/baby-python
|
python
|
from time import time
from typing import Any
from flask import render_template
def login_render(auth_url: str) -> Any:
"""Return login page.
Arguments:
auth_url {str} -- Link to last.fm authorization page.
"""
return render_template("login.html", auth_url=auth_url, timestamp=time())
|
nilq/baby-python
|
python
|
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 7 18:10:42 2020
Small module to allow multiprocessing of the point in polygon problem
@author: Matthew Varnam - The University of Manchester
@email: matthew.varnam(-at-)manchester.ac.uk
"""
#Import numpy for mathematical calculations
import numpy as np
#Import shapely to create easily queryable objects
from shapely.geometry import Point, Polygon
from shapely.strtree import STRtree
#Create controlling worker to operate multiple procedures
def worker_pp (point_poly_list):
#Create global variables to be used by the procedures
global tree
global polygon_list
global px_points
global index_by_id
#Extract points and polygon lists from initialiser function argument
point_list,poly_list = point_poly_list
px_points = np.array(point_list)
sorted_polygons = np.array(poly_list)
#Convert to shapely class Points and Polygons
point_list = [Point(point) for point in px_points]
polygon_list = [Polygon(poly) for poly in sorted_polygons]
#Create STRtree to speed up checking of points and polygons
tree = STRtree(point_list)
#Create dictionary to index point list for faster querying
index_by_id = dict((id(pt), i) for i, pt in enumerate(point_list))
#Procedure function to be called multiple times to identify points in polygons
def proc_pp (*args):
#Choose the polygon matching the index provided by the multiprocessing Pool
k = args[0]
poly = polygon_list[k]
#Conduct two things - query the STRtree then confirm an intersection
valid_ids = [(index_by_id[id(pt)]) for pt in tree.query(poly)
if pt.intersects(poly)]
#Find the coordinates of the points that lie inside the polygon
valid_points = px_points[valid_ids]
returner = valid_points.tolist()
return k,returner
|
nilq/baby-python
|
python
|
import sys, os
import librosa
import torch
import numpy as np
from typing import Union, Tuple, List
from collections import defaultdict
import configparser
config = configparser.ConfigParser(allow_no_value=True)
config.read("config.ini")
from vectorizer.model import Model
from vectorizer.utils import chunk_data
from void.utils import Segmenter
from spectralcluster import SpectralClusterer
from scipy.optimize import linear_sum_assignment
from scipy.spatial import distance
class ToolBox(object):
def __init__(self, use_cpu:bool=False):
self.use_cpu = use_cpu
self._load()
def _load(self):
self.model = Model()
self.storage = 'cuda:0' if torch.cuda.is_available() else 'cpu'
self.storage = 'cpu' if self.use_cpu else self.storage
checkpoint = torch.load(config.get('VECTORIZER', 'trained_model'), map_location=self.storage)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.model = self.model.to(self.storage)
self.model.eval()
self.segmenter = Segmenter()
self.clusterer = SpectralClusterer(
min_clusters=2,
max_clusters=100,
p_percentile=0.95,
gaussian_blur_sigma=1.0)
def _check_audio(self, audio:Union[np.array, str], sr:int) -> Union[np.array, str]:
if isinstance(audio, str):
if not os.path.exists(audio):
raise FileNotFoundError(f"File not found at location: `{audio}`.")
try:
audio, _ = librosa.load(audio, sr=sr, mono=True)
except Exception as e:
raise ValueError(f"Exception: {e}\nCould not read audio at location: `{audio}`.")
elif not isinstance(audio, (np.ndarray, np.generic)):
raise TypeError(f"Invalid argument type: audio should be either str or np.array.")
audio = np.squeeze(audio)
if not len(audio.shape) == 1:
raise ValueError(f"Expected audio input to be 1 dimensional.")
return audio
def vectorize(self, audio:Union[np.array, str], sr:int=16000, frame_stride:float=None, hop_size:float=None) -> np.array:
"""
Parameters
----------
audio : np.array or str
1D numpy array or filepath to the audio file to vectorize.
sr : int, optional
Audio sample rate
frame_stride: float, optional
Chunk audio in frames of length frame_stride seconds
hop_size: float, optional
Chunk audio in frames of length frame_stride seconds with hop_size seconds
Returns
-------
np.array
A 2 Dimensional vector representation of the audio input.
"""
audio = self._check_audio(audio, sr)
frame_stride = config.getfloat("AUDIO", "frame_stride") if frame_stride is None else frame_stride
hop_size = config.getfloat("AUDIO", "hop_size") if hop_size is None else hop_size
frame_stride = int(sr*frame_stride)
hop_size = int(sr*hop_size)
audio = chunk_data(audio, frame_stride, max(0, (frame_stride-hop_size)))
audio = torch.from_numpy(np.array(audio).astype(np.float32)).to(self.storage)
with torch.no_grad():
features = self.model(audio)
return features.cpu().numpy()
def _diarize(self, audio:np.array, max_num_speakers:int) -> Tuple[List[Tuple[int, int]], np.array]:
segments = self.segmenter(audio)
audio_clips = [audio[s[0]:s[1]] for s in segments]
vectors = list(map(self.vectorize, audio_clips))
vectors = [item for sublist in vectors for item in sublist]
self.clusterer.max_clusters = max_num_speakers
labels = self.clusterer.predict(np.squeeze(np.array(vectors)))
return segments, labels
def diarize(self, audio:Union[np.array, str], sr:int=16000, max_num_speakers:int=30) -> List[str]:
"""
Parameters
----------
audio : np.array or str
1D numpy array or filepath to the audio file to vectorize.
sr : int, optional
Audio sample rate
max_num_speakers: int, optional
Maximum amount of expected speakers in the audio
Returns
-------
list
A list of strings. Each line is compatible with the RTTM format
"""
rttm = list()
audio = self._check_audio(audio, sr)
segments, labels = self._diarize(audio, max_num_speakers)
for idx, segment in enumerate(segments):
line = f"SPEAKER filename 1 {segment[0]/sr:.2f} {(segment[1]-segment[0])/sr:.2f} <NA> <NA> speaker{labels[idx]} <NA> <NA>\n"
rttm.append(line)
return rttm
def recognize(self, audio:Union[np.array, str], enrollments:list, sr:int=16000, max_num_speakers:int=30) -> List[str]:
"""
Parameters
----------
audio : np.array or str
1D numpy array or filepath to the audio file to vectorize.
enrollments: list
list of tuples: (audio:Union[np.array, str], label:str)
sr : int, optional
Audio sample rate
max_num_speakers: int, optional
Maximum amount of expected speakers in the audio
Returns
-------
list
A list of strings. Each line is compatible with the RTTM format
"""
rttm = list()
audio = self._check_audio(audio, sr)
enrollments = [(self._check_audio(audio, sr), label) for audio, label in enrollments]
enrollments = [(self.vectorize(audio), label) for audio, label in enrollments]
enrollment_vectors = list()
for vectors, l in enrollments:
for v in list(vectors):
enrollment_vectors.append((v, l))
# Compute representative vector for each label
enrollment_dict = defaultdict(list)
for vector, label in enrollment_vectors:
enrollment_dict[label].append(np.squeeze(vector))
enrollment_X, enrollment_y = zip(*[(np.mean(vectors, axis=0), label) for label, vectors in enrollment_dict.items()])
# Run diarization
segments, labels = self._diarize(audio, max_num_speakers)
# Compute representative vector for each label
segments_dict = defaultdict(list)
for idx, vector in enumerate(vectors):
segments_dict[labels[idx]].append(np.squeeze(vector))
segment_X, segment_y = zip(*[(np.mean(vectors, axis=0), label) for label, vectors in segments_dict.items()])
# Make sure we have the right shape
enrollment_X = np.squeeze(enrollment_X)
segment_X = np.squeeze(segment_X)
if len(enrollment_X.shape) == 1:
enrollment_X = enrollment_X[None, :]
if len(segment_X.shape) == 1:
segment_X = segment_X[None, :]
cost = distance.cdist(np.array(enrollment_X), np.array(segment_X), metric='cosine')
row_ind, col_ind = linear_sum_assignment(cost)
num_solutions = len(row_ind)
id2label = dict()
# Map between speaker ID and provided label (if it exists)
for sol in range(num_solutions):
id2label[list(segment_y)[col_ind[sol]]] = list(enrollment_y)[row_ind[sol]]
for idx, segment in enumerate(segments):
label = id2label.get(labels[idx])
if label is None:
label = f"speaker{labels[idx]}"
line = f"SPEAKER filename 1 {segment[0]/sr:.2f} {(segment[1]-segment[0])/sr:.2f} <NA> <NA> {label} <NA> <NA>\n"
rttm.append(line)
return rttm
def verify(self, audio:Union[np.array, str], enrollments:list, sr:int=16000 ) -> float:
"""
Parameters
----------
audio : np.array or str
1D numpy array or filepath to the audio file to vectorize.
enrollments: list
list of tuples: (audio:Union[np.array, str], label:str)
sr : int, optional
Audio sample rate
Returns
-------
float
Similarity score --> [0, 1]
"""
audio = self._check_audio(audio, sr)
enrollments = [(self._check_audio(audio, sr), label) for audio, label in enrollments]
enrollment_vector = [np.mean(self.vectorize(audio),axis=0) for audio, _ in enrollments]
segments = self.segmenter(audio)
audio_clips = [audio[s[0]:s[1]] for s in segments]
vectors = list(map(self.vectorize, audio_clips))
vectors = [item for sublist in vectors for item in sublist]
audio_vector = np.mean(vectors, axis=0)
similarity = max(0, np.mean(1-distance.cdist(audio_vector[None, :], np.array(enrollment_vector), 'cosine')))
return similarity
if __name__ == "__main__":
toolbox = ToolBox()
print(toolbox.vectorize.__doc__)
print(toolbox.diarize.__doc__)
print(toolbox.recognize.__doc__)
print(toolbox.verify.__doc__)
|
nilq/baby-python
|
python
|
#!/usr/bin/env python3
import re
import pysam
from .most_common import most_common
from .sequence_properties import repeat
cigar_ptn = re.compile(r"[0-9]+[MIDNSHPX=]")
def realn_softclips(
reads, pos, ins_or_del, idl_seq, idl_flanks, decompose_non_indel_read
):
template = make_indel_template(idl_seq, idl_flanks)
candidate_reads = [
classify_softclip_read(read, pos)
for read in reads
if classify_softclip_read(read, pos)
]
if not candidate_reads:
return []
fw_decomposed = [
forward_decomposition(
read, softclip_ptrn, pos, ins_or_del, idl_seq, decompose_non_indel_read
)
for read, softclip_ptrn in candidate_reads
]
rv_decomposed = [
reverse_decomposition(read, pos, ins_or_del, idl_seq)
for read, softclip_ptrn in candidate_reads
]
decomposed_candidates = fw_decomposed + rv_decomposed
compatible_softclip_reads = [
decom[0]
for decom in decomposed_candidates
if is_compatible(decom, template, ins_or_del)
]
return compatible_softclip_reads
def make_indel_template(idl_seq, idl_flanks):
"""Make consensus contig
"""
lt_flanks = [flank[0][::-1] for flank in idl_flanks if flank[0][-1] != "N"]
rt_flanks = [flank[1] for flank in idl_flanks if flank[1][0] != "N"]
lt_template = find_consensus_seq(lt_flanks)[::-1]
rt_template = find_consensus_seq(rt_flanks)
return lt_template, idl_seq, rt_template
def get_ith_char(seq, i):
try:
return seq[i]
except:
return None
def find_consensus_seq(seq_lst):
consensus = ""
if not seq_lst:
return consensus
for i in range(len(max(seq_lst, key=len))):
ith_chars = [get_ith_char(seq, i) for seq in seq_lst if get_ith_char(seq, i)]
if most_common(ith_chars) == "N":
break
else:
consensus += most_common(ith_chars)
return consensus.upper()
def classify_softclip_read(read, pos):
"""Check softclip pattern and the clipped alignment is in the exon of interest
"""
cigarstring = read.cigarstring
if not "S" in cigarstring:
return None
cigarlst = cigar_ptn.findall(read.cigarstring)
start_adjust = int(cigarlst[0][:-1]) if cigarlst[0].endswith("S") else 0
read_start = read.reference_start - start_adjust
end_adjust = int(cigarlst[-1][:-1]) if cigarlst[-1].endswith("S") else 0
read_end = read.reference_end + end_adjust
if "N" in cigarstring:
idx_at_splicesite = [
i for i, cigartoken in enumerate(cigarlst) if cigartoken.endswith("N")
]
exonic_cigarlst = split_lst_by_index(cigarlst, idx_at_splicesite)
# merge blocks separated by insertion/deletions
deletion_lengths = [
int(token[:-1]) for token in cigarlst if token.endswith("D")
]
d = max(deletion_lengths) if deletion_lengths else 0
blocks = merge_blocks(read.get_blocks(), d)
idx_at_this_exon = []
for i, block in enumerate(blocks):
if i == 0 and read_start <= pos <= block[1]:
idx_at_this_exon.append(i)
elif i == len(blocks) - 1 and block[0] <= pos <= read_end:
idx_at_this_exon.append(i)
elif block[0] <= pos <= block[1]:
idx_at_this_exon.append(i)
else:
pass
if idx_at_this_exon:
this_exon_cigarstring = exonic_cigarlst[idx_at_this_exon[0]]
else:
return None
else:
this_exon_cigarstring = cigarlst
first, last = this_exon_cigarstring[0][-1], this_exon_cigarstring[-1][-1]
if first == "S" and last != "S":
return (read, "leading")
elif first != "S" and last == "S":
return (read, "trailing")
elif first == "S" and last == "S":
# give up this pattern for now
return None
else:
return None
def merge_blocks(lst, d):
merged = []
for i, b in enumerate(lst):
if i <= len(lst) - 2:
if lst[i + 1][0] <= b[1] + d:
merged.append((b[0], lst[i + 1][1]))
del lst[i + 1]
else:
merged.append((b[0], b[1]))
else:
if lst[i - 1][1] < b[0]:
merged.append((b[0], b[1]))
else:
pass
if merged == lst or len(merged) == 1:
return merged
else:
return merge_blocks(merged, d)
def split_lst_by_index(lst, idx):
if idx:
idx = (0,) + tuple(data + 1 for data in idx) + (len(lst) + 1,)
my_lst = []
for start, end in zip(idx, idx[1:]):
my_lst.append(lst[start : end - 1])
return my_lst
def forward_decomposition(
read, softclip_ptrn, pos, ins_or_del, idl_seq, decompose_non_indel_read
):
"""Decompose softclipped read from 5'side
"""
decom = decompose_non_indel_read(read, pos, ins_or_del, idl_seq)
lt_flank, mid_seq, rt_flank = decom[2][0], decom[1], decom[2][1]
if ins_or_del == "I" and softclip_ptrn == "leading":
mid_seq = lt_flank[-len(idl_seq) :]
lt_flank = lt_flank[: -len(idl_seq)]
elif ins_or_del == "I" and softclip_ptrn == "trailing":
mid_seq = rt_flank[: len(idl_seq)]
rt_flank = rt_flank[len(idl_seq) :]
elif ins_or_del == "D" and softclip_ptrn == "leading":
lt_flank = lt_flank + mid_seq
mid_seq = idl_seq
else:
rt_flank = mid_seq + rt_flank
mid_seq = idl_seq
return (read, lt_flank, mid_seq, rt_flank)
def reverse_decomposition(read, pos, ins_or_del, idl_seq):
"""Decompose softclipped read from 3'side
"""
read_seq = read.query_sequence[::-1]
cigarstring = read.cigarstring
cigarlst = cigar_ptn.findall(read.cigarstring)[::-1]
adjust = int(cigarlst[0][:-1]) if cigarlst[0].endswith("S") else 0
read_pos = read.reference_end + adjust
idl_len = len(idl_seq)
read_idx = 0
pos = pos if ins_or_del == "I" else pos + idl_len
for token in cigarlst:
event, event_len = token[-1], int(token[:-1])
if pos < read_pos:
read_pos = read_pos if event == "I" else (read_pos - event_len)
read_idx = (
read_idx if event == "D" or event == "N" else read_idx + event_len
)
else:
break
diff = read_pos - pos
if ins_or_del == "D":
lt_flank, mid_seq, rt_flank = (
read_seq[read_idx + diff :],
"",
read_seq[: read_idx + diff],
)
else:
rt_flank, mid_seq, lt_flank = (
read_seq[: read_idx + diff],
read_seq[read_idx + diff : read_idx + diff + idl_len],
read_seq[read_idx + diff + idl_len :],
)
lt_flank, mid_seq, rt_flank = lt_flank[::-1], mid_seq[::-1], rt_flank[::-1]
return (read, lt_flank, mid_seq, rt_flank)
def is_compatible(read_tuple, template_tuple, ins_or_del):
read = read_tuple[0]
read_lt_flank, read_indel, read_rt_flank = (
read_tuple[1],
read_tuple[2],
read_tuple[3],
)
template_lt_flank, template_indel, template_rt_flank = (
template_tuple[0],
template_tuple[1],
template_tuple[2],
)
lt_len = min(len(read_lt_flank), len(template_lt_flank))
rt_len = min(len(read_rt_flank), len(template_rt_flank))
# count repeat in template
idl_type = 1 if ins_or_del == "I" else 0
if template_lt_flank and template_rt_flank:
template_repeat = repeat(
idl_type, template_lt_flank, template_indel, template_rt_flank
)
else:
return None
if template_repeat > 0:
if lt_len == 0 or rt_len == 0:
return False
else:
read_repeat = repeat(idl_type, read_lt_flank, template_indel, read_rt_flank)
if template_repeat != read_repeat:
return False
if ins_or_del == "D" and (lt_len == 0 or rt_len == 0):
return False
if lt_len > 0:
lt_read, lt_template = read_lt_flank[-lt_len:], template_lt_flank[-lt_len:]
else:
lt_read, lt_template = "", ""
rt_read, rt_template = read_rt_flank[:rt_len], template_rt_flank[:rt_len]
if not is_almost_same(lt_read[::-1], lt_template[::-1]) or not is_almost_same(
rt_read, rt_template
):
return False
if read_indel and ins_or_del == "I":
template_indel_len = len(template_indel)
read_indel_len = len(read_indel)
if template_indel_len < read_indel_len:
return False
elif read_indel == template_indel:
return True
elif 4 <= template_indel_len <= 5:
return identical_for_end_n_bases(read_indel, template_indel, 2)
elif 6 <= template_indel_len <= 7:
return identical_for_end_n_bases(read_indel, template_indel, 3)
else:
return identical_for_end_n_bases(read_indel, template_indel, 4)
elif ins_or_del == "D":
return True
else:
return False
def identical_for_end_n_bases(query_str, subject_str, n):
return (query_str[:n] == subject_str[:n]) or (query_str[-n:] == subject_str[-n:])
def is_almost_same(seq1, seq2, len_lim=10, mismatch_lim=1):
seq_len = len(seq1)
if seq_len > 0 and seq1[0] != seq2[0]:
return False
hamming = sum([seq1[i] != seq2[i] for i in range(seq_len)])
if seq_len >= len_lim:
return hamming <= mismatch_lim
else:
return hamming == 0
|
nilq/baby-python
|
python
|
"""Config namespace."""
from flask_restx import Namespace, Resource, fields # type: ignore
from jsonschema import ValidationError # type: ignore
from configmodel.logic.config import (
create_config,
delete_config,
get_config,
get_configs,
validate_config,
)
api = Namespace("config", description="Config operations")
config_model = api.model(
"Config",
{
"config_id": fields.Integer(
readonly=True, description="The configuration unique identifier"
),
"hostname": fields.String(required=True, description="Device hostname"),
"schema": fields.String(required=True, description="Configuration schema name"),
"config": fields.Raw(required=True, description="Configuration JSON object"),
},
)
@api.route("/")
class ConfigList(Resource):
"""Shows a list of all configs, and lets you POST to add new ones."""
@api.marshal_list_with(config_model)
def get(self):
"""List all configs."""
return get_configs()
@api.expect(config_model, validate=True)
@api.marshal_with(config_model, code=201, mask=None)
def post(self):
"""Create a new config."""
try:
validate_config(api.payload)
except ValidationError as e:
api.abort(400, e)
config = create_config(
hostname=api.payload["hostname"],
schema=api.payload["schema"],
config=api.payload["config"],
)
return config, 201
@api.route("/<int:config_id>")
@api.response(404, "config_id not found")
@api.param("config_id", "The config identifier")
class Config(Resource):
"""Show a single config item and lets you delete it."""
@api.marshal_with(config_model)
def get(self, config_id):
"""Fetch a given config."""
config = get_config(config_id)
if config is None:
api.abort(404)
return config
@api.response(204, "Config deleted")
def delete(self, config_id):
"""Delete a config given its identifier."""
result = delete_config(config_id)
if not result:
api.abort(404)
return "", 204
|
nilq/baby-python
|
python
|
# coding: utf-8
# In[1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# In[2]:
# 载入数据集
mnist = input_data.read_data_sets('./../../datas/mnist/', one_hot=True)
# 输入图片是28*28
n_inputs = 28 # 输入一行,一行有28个数据
max_time = 28 # 一共28行
lstm_size = 100 # 隐层单元
n_classes = 10 # 10个分类
batch_size = 50 # 每批次50个样本
n_batch = mnist.train.num_examples // batch_size # 计算一共有多少个批次
# 这里的none表示第一个维度可以是任意的长度
x = tf.placeholder(tf.float32, [None, 784])
# 正确的标签
y = tf.placeholder(tf.float32, [None, 10])
# 初始化权值
weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
# 初始化偏置值
biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))
# 定义RNN网络
def RNN(X, weights, biases):
# inputs=[batch_size, max_time, n_inputs]
inputs = tf.reshape(X, [-1, max_time, n_inputs])
# 定义LSTM基本CELL
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
# final_state[0]是cell state
# final_state[1]是hidden_state
outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, inputs, dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)
return results
# 计算RNN的返回结果
prediction = RNN(x, weights, biases)
# 损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
# 使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# 结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) # argmax返回一维张量中最大的值所在的位置
# 求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 把correct_prediction变为float32类型
# 初始化
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(6):
for batch in range(n_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
print("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))
# In[ ]:
|
nilq/baby-python
|
python
|
from framework.types import RequestT
from framework.types import ResponseT
from framework.utils import build_status
from framework.utils import read_static
def handle_image(_request: RequestT) -> ResponseT:
payload = read_static("image.jpg")
status = build_status(200)
headers = {"Content-type": "image/jpeg"}
return ResponseT(status, headers, payload)
|
nilq/baby-python
|
python
|
import pandas as pd
import googlemaps
import json
from shapely.geometry import shape, Point
with open('static/GEOJSON/USCounties_final.geojson') as f:
geojson1 = json.load(f)
county = geojson1["features"]
with open('static/GEOJSON/ID2.geojson') as f:
geojson = json.load(f)
district = geojson["features"]
project = pd.read_csv('static/Excel/CleanedProject.csv')
df = pd.read_csv('static/Excel/Community Partners.csv') #Get the Excel file from static/Excel
gmaps = googlemaps.Client(key='') #google geocoding API
collection = {'type': 'FeatureCollection', 'features': []}
df['fulladdress'] = df[['address_line1', 'city', 'state']].apply(lambda x: ' '.join(x.astype(str)), axis=1)
def feature_from_row(Community, Address, Mission, CommunityType, Website):
feature = {'type': 'Feature', 'properties': {'CommunityPartner': '', 'Address': '',
'Legislative District Number': '', 'Number of projects': '',
'Income': '', 'County': '', 'Mission Area': '',
'CommunityType': '', 'Campus Partner': '',
'Academic Year': '', 'Website': ''},
'geometry': {'type': 'Point', 'coordinates': []}
}
geocode_result = gmaps.geocode(Address) # get the coordinates
print(Address)
print(geocode_result)
if (geocode_result[0]):
latitude = geocode_result[0]['geometry']['location']['lat']
longitude = geocode_result[0]['geometry']['location']['lng']
feature['geometry']['coordinates'] = [longitude, latitude]
coord = Point([longitude, latitude])
for i in range(len(district)): # iterate through a list of district polygons
property = district[i]
polygon = shape(property['geometry']) # get the polygons
if polygon.contains(coord): # check if a partner is in a polygon
feature['properties']['Legislative District Number'] = property["properties"]["id"] # assign the district number to a partner
for m in range(len(county)): # iterate through the County Geojson
properties2 = county[m]
polygon = shape(properties2['geometry']) # get the polygon
if polygon.contains(coord): # check if the partner in question belongs to a polygon
feature['properties']['County'] = properties2['properties']['NAME']
feature['properties']['Income'] = properties2['properties']['Income']
# projectlist = 0
yearlist = []
campuslist = []
partners = project['community_partner']
years = project['academic_year']
campuses = project['campus_partner']
count = 0
for n in range(len(partners)):
if (partners[n] == Community):
if (years[n] not in yearlist):
yearlist.append(years[n])
if (campuses[n] not in campuslist):
campuslist.append(campuses[n])
count += 1
feature['properties']['Number of projects'] = count
feature['properties']['Campus Partner'] = campuslist
feature['properties']['Academic Year'] = yearlist
feature['properties']['CommunityPartner'] = Community
feature['properties']['CommunityType'] = CommunityType
feature['properties']['Website'] = Website
feature['properties']['Mission Area'] = Mission
collection['features'].append(feature)
return feature
geojson_series = df.apply(
lambda x: feature_from_row(x['name'], x['fulladdress'], x['mission_area'], x['community_type'], x['website_url']),
axis=1)
#
jsonstring = pd.io.json.dumps(collection)
output_filename = 'static/GEOJSON/Partner.geojson' #The file will be saved under static/GEOJSON
with open(output_filename, 'w') as output_file:
output_file.write(format(jsonstring))
|
nilq/baby-python
|
python
|
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.
"""Optimizers based on scalarization.
One of the simplest approaches to optimizing multi-loss problems is to scalarize
to a real objective by combining the individual losses. Depending on how the
scalarization is performed, different optimization algorithms arise.
"""
import gin
import tensorflow.compat.v1 as tf
from yoto.optimizers import base as optimizers_base
from yoto.optimizers import distributions
@gin.configurable("LinearlyScalarizedOptimizer")
class LinearlyScalarizedOptimizer(optimizers_base.MultiLossOptimizer):
r"""An optimizer that linearly scalarizes the losss.
Namely, if the losses are loss_1, ..., loss_n, then it minimizes
\sum_i loss_i * weight_i,
for fixed weights. The weights can be either randomly drawn from one of the
supported distributions, or fixed.
"""
def __init__(self, problem, weights,
batch_size=None, seed=17):
"""Initializes the optimizer.
Args:
problem: An instance of `problems.Problem`.
weights: Either `distributions.DistributionSpec` class or a
dictionary mapping the loss names to their corresponding
weights.
batch_size: Passed to the initializer of `MultiLossOptimizer`.
seed: random seed to be used for sampling the weights.
"""
super(LinearlyScalarizedOptimizer, self).__init__(
problem, batch_size=batch_size)
sampled_weights = distributions.get_samples_as_dicts(
weights, names=self._losses_names, seed=seed)[0]
self._check_weights_dict(sampled_weights)
self._weights = sampled_weights
def compute_train_loss_and_update_op(self, inputs, base_optimizer):
losses, metrics = self._problem.losses_and_metrics(inputs, training=True)
del metrics
linearized_loss = 0.
for loss_name, loss_value in losses.items():
linearized_loss += tf.reduce_mean(loss_value * self._weights[loss_name])
train_op = base_optimizer.minimize(
linearized_loss, global_step=tf.train.get_or_create_global_step())
self.normal_vars = tf.trainable_variables()
return linearized_loss, train_op
def compute_eval_loss(self, inputs):
losses, metrics = self._problem.losses_and_metrics(inputs, training=False)
del metrics
linearized_loss = 0.
for loss_name, loss_value in losses.items():
linearized_loss += tf.reduce_mean(loss_value * self._weights[loss_name])
return linearized_loss
|
nilq/baby-python
|
python
|
import sqlite3
from .utility import exception_info, enquote2
class SQLighter:
def __init__(self, db):
self.connection = sqlite3.connect(db)
self.cursor = self.connection.cursor()
def db_query(self, query, args=None):
with self.connection:
if args is None or args == ():
self.cursor.execute(query)
else:
self.cursor.execute(query, args)
rows = self.cursor.fetchall()
return rows
def db_query_rows(self, query, args=None):
rows = self.db_query(query, args)
if len(rows) == 0:
return None
else:
return rows
def db_query_row(self, query, args=None):
rows = self.db_query(query, args)
if len(rows) == 0:
return None
else:
return rows[0]
def db_query_single(self, query, args=None):
rows = self.db_query(query, args)
if len(rows) == 0:
return None
else:
return rows[0][0]
def db_query_commit(self, query, args):
try:
with self.connection:
self.cursor.execute(query, args)
self.connection.commit()
except Exception as ex:
print("#######" + exception_info(ex)) # TODO: ?
return None
else:
return self.cursor.lastrowid
def close(self):
self.connection.close()
@staticmethod
def gen_insert(table, **kwargs):
"""Generates DB insert statement"""
cols = []
vals = []
for col, val in kwargs.items():
cols.append(enquote2(col))
vals.append(enquote2(str(val)))
cols = ", ".join(cols)
vals = ", ".join(vals)
return "INSERT INTO '%s'(%s) VALUES(%s);" % (
table, cols, vals)
|
nilq/baby-python
|
python
|
from selfdrive.car import limit_steer_rate
from selfdrive.car.hyundai.hyundaican import create_lkas11, create_lkas12, \
create_1191, create_1156, \
learn_checksum, create_mdps12, create_clu11
from selfdrive.car.hyundai.values import Buttons
from selfdrive.can.packer import CANPacker
import zmq
from selfdrive.services import service_list
import selfdrive.messaging as messaging
from selfdrive.config import Conversions as CV
from common.params import Params
from selfdrive.swaglog import cloudlog
# Steer torque limits
class SteerLimitParams:
STEER_MAX = 255 # >255 results in frozen torque, >409 results in no torque
STEER_DELTA_UP = 3
STEER_DELTA_DOWN = 5
STEER_DRIVER_ALLOWANCE = 50
STEER_DRIVER_MULTIPLIER = 2
STEER_DRIVER_FACTOR = 1
DIVIDER = 2.0 # Must be > 1.0
class CarController(object):
def __init__(self, dbc_name, car_fingerprint):
self.apply_steer_last = 0
self.car_fingerprint = car_fingerprint
self.lkas11_cnt = 0
self.clu11_cnt = 0
self.mdps12_cnt = 0
self.cnt = 0
self.last_resume_cnt = 0
self.map_speed = 0
self.map_data_sock = messaging.sub_sock(service_list['liveMapData'].port)
self.params = Params()
self.speed_conv = 3.6
self.speed_offset = 1.03 # Multiplier for cruise speed vs speed limit TODO: Add to UI
self.speed_enable = True # Enable Auto Speed Set TODO: Add to UI
self.speed_adjusted = False
self.checksum = "NONE"
self.checksum_learn_cnt = 0
self.turning_signal_timer = 0
self.camera_disconnected = False
self.checksum_found = False
self.packer = CANPacker(dbc_name)
def update(self, enabled, CS, actuators, pcm_cancel_cmd, hud_alert):
### Error State Resets ###
disable_steer = False
can_sends = []
### Learn Checksum ###
if not self.checksum_found:
# Learn Checksum from the Camera
if self.checksum == "NONE":
self.checksum = learn_checksum(self.packer, CS.lkas11)
if self.checksum == "NONE" and self.checksum_learn_cnt < 50:
self.checksum_learn_cnt += 1
return
else:
cloudlog.info("Discovered Checksum %s" % self.checksum)
self.checksum_found = True
# If MDPS is faulted from bad checksum, then cycle through all Checksums until 1 works
if CS.steer_error == 1:
self.camera_disconnected = True
cloudlog.warning("Camera Not Detected: Brute Forcing Checksums")
if self.checksum_learn_cnt > 300:
self.checksum_learn_cnt = 50
if self.checksum == "NONE":
cloudlog.info("Testing 6B Checksum")
self.checksum = "6B"
elif self.checksum == "6B":
cloudlog.info("Testing 7B Checksum")
self.checksum = "7B"
elif self.checksum == "7B":
cloudlog.info("Testing CRC8 Checksum")
self.checksum = "crc8"
else:
self.checksum = "NONE"
return
else:
self.checksum_learn_cnt += 1
else:
cloudlog.info("Discovered Checksum %s" % self.checksum)
self.checksum_found = True
### Minimum Steer Speed ###
# Apply Usage of Minimum Steer Speed
if CS.low_speed_alert:
disable_steer = True
### Turning Indicators ###
if (CS.left_blinker_on == 1 or CS.right_blinker_on == 1):
self.turning_signal_timer = 100 # Disable for 1.0 Seconds after blinker turned off
if self.turning_signal_timer > 0:
disable_steer = True
self.turning_signal_timer -= 1
### Steering Torque ###
apply_steer = actuators.steer * SteerLimitParams.STEER_MAX
apply_steer = limit_steer_rate(apply_steer, self.apply_steer_last, CS.steer_torque_driver, SteerLimitParams)
if not enabled or disable_steer:
apply_steer = 0
steer_req = 0
else:
steer_req = 1
self.apply_steer_last = apply_steer
'''
### Auto Speed Limit ###
# Read Speed Limit and define if adjustment needed
if (self.cnt % 50) == 0 and self.speed_enable:
if not (enabled and CS.acc_active):
self.speed_adjusted = False
map_data = messaging.recv_one_or_none(self.map_data_sock)
if map_data is not None:
if bool(self.params.get("IsMetric")):
self.speed_conv = CV.MS_TO_KPH
else:
self.speed_conv = CV.MS_TO_MPH
if map_data.liveMapData.speedLimitValid:
last_speed = self.map_speed
v_speed = int(map_data.liveMapData.speedLimit * self.speed_offset)
self.map_speed = v_speed * self.speed_conv
if last_speed != self.map_speed:
self.speed_adjusted = False
else:
self.map_speed = 0
self.speed_adjusted = True
else:
self.map_speed = 0
self.speed_adjusted = True
# Spam buttons for Speed Adjustment
if CS.acc_active and not self.speed_adjusted and self.map_speed > (8.5 * self.speed_conv) and (self.cnt % 9 == 0 or self.cnt % 9 == 1):
if (CS.cruise_set_speed * self.speed_conv) > (self.map_speed * 1.005):
can_sends.append(create_clu11(self.packer, CS.clu11, Buttons.SET_DECEL, (1 if self.cnt % 9 == 1 else 0)))
elif (CS.cruise_set_speed * self.speed_conv) < (self.map_speed / 1.005):
can_sends.append(create_clu11(self.packer, CS.clu11, Buttons.RES_ACCEL, (1 if self.cnt % 9 == 1 else 0)))
else:
self.speed_adjusted = True
# Cancel Adjustment on Pedal
if CS.pedal_gas:
self.speed_adjusted = True
'''
### Generate CAN Messages ###
self.lkas11_cnt = self.cnt % 0x10
# self.clu11_cnt = self.cnt % 0x10
self.mdps12_cnt = self.cnt % 0x100
if self.camera_disconnected:
if (self.cnt % 10) == 0:
can_sends.append(create_lkas12())
if (self.cnt % 50) == 0:
can_sends.append(create_1191())
if (self.cnt % 7) == 0:
can_sends.append(create_1156())
can_sends.append(create_lkas11(self.packer, self.car_fingerprint, apply_steer, steer_req, self.lkas11_cnt,
enabled, CS.lkas11, hud_alert, (not self.camera_disconnected), self.checksum))
if not self.camera_disconnected:
can_sends.append(create_mdps12(self.packer, self.car_fingerprint, self.mdps12_cnt, CS.mdps12, CS.lkas11, \
self.checksum))
# if pcm_cancel_cmd:
# can_sends.append(create_clu11(self.packer, CS.clu11, Buttons.CANCEL, 0))
if CS.stopped and (self.cnt - self.last_resume_cnt) > 20:
if (self.cnt - self.last_resume_cnt) > 20:
self.last_resume_cnt = self.cnt
can_sends.append(create_clu11(self.packer, CS.clu11, Buttons.RES_ACCEL, self.clu11_cnt))
self.cnt += 1
return can_sends
|
nilq/baby-python
|
python
|
##==========================================================
## 2016.02.09 vsTAAmbk 0.4.1
## Ported from TAAmbk 0.7.0 by Evalyn
## Email: pov@mahou-shoujo.moe
## Thanks (author)kewenyu for help
##==========================================================
## Requirements:
## EEDI2
## nnedi3
## RemoveGrain/Repair
## fmtconv
## GenericFilters
## MSmoosh
## MVTools
## TemporalSoften
## sangnom
## HAvsFunc(and its requirements)
## VapourSynth R28 or newer
##
##==========================================================
##==========================================================
##
## #### Only YUV colorfmaily is supported !
## #### And input bitdepth must be 8 or 16 INT !
##
## Add lsb[bool] to control nnedi3 input bitdepth.
## "False" means input depth for nnedi3 is always 8bit.
## "thin" and "dark" are now removed.
## add "aatype = 7" using "pure" sangnom.
##
##==========================================================
##
## Output bitdepth is always 16bit INTEGER.
## AA precision is 16bit (8bit if necessary).
## Mask precision depend on the input.
## (if 8 then 8, if 16 then 16)
## Other parts are all 16bit.
##
##==========================================================
import vapoursynth as vs
import havsfunc as haf
def TAAmbk(input, aatype=1, lsb=False, preaa=0, sharp=0, postaa=None, mtype=None, mthr=32, src=None,
cycle=0, eedi3sclip=None, predown=False, repair=None, stabilize=0, p1=None, p2=None,
p3=None, p4=None, p5=None, p6=None, showmask=False, mtype2=0, mthr2=32, auxmthr=None):
core = vs.get_core()
#constant value
funcname = 'TAAmbk'
w = input.width
h = input.height
upw4 = (round(w*0.09375)*16) # mod16(w*1.5)
uph4 = (round(h*0.09375)*16) # mod16(h*1.5)
downw4 = (round(w*0.046875)*16) # mod16(w*0.75)
downh4 = (round(h*0.046875)*16) # mod16(h*0.75)
if input.format.num_planes == 1:
GRAY = True
else:
GRAY = False
# border to add for SangNomMod when aatype = 6 or 7
if aatype == 6 or aatype == 7:
# mod16 or not
if w % 16 == 0:
mod16w = True
else:
mod16w = False
borderW = (16 - w % 16)
if h % 16 == 0:
mod16h = True
else:
mod16h = False
borderH = (16 - h % 16)
#generate paramerters if None
if mtype == None:
if preaa == 0 and aatype == 0:
mtype = 0
else:
mtype = 1
if auxmthr == None:
if mtype == 1:
auxmthr = 1.2
else:
if mtype ==3:
auxmthr = 8
else:
auxmthr = 0.0
absSh = abs(sharp)
if postaa == None:
if absSh > 70 or (absSh > 0.4 and absSh < 1):
postaa = True
else:
postaa = False
if repair == None:
if (aatype != 1 and aatype != 2 and aatype != 3):
repair = 20
else:
repair = 0
if isinstance(mtype, vs.VideoNode):
rp = 20
else:
if mtype == 5:
rp = 0
else:
rp = 20
if eedi3sclip is None:
eedi3sclip = False
else:
if not isinstance(eedi3sclip, bool):
raise TypeError(funcname + ': \"eedi3sclip\" must be bool !')
# p1~p6 preset groups
pindex = aatype + 3
# aatype = -3 -2 -1 0 1 2 3 4 5 6 7
if p1 is None: p1 = [ 48, 48, 48, 0, 10, 0.5, 3, 48, 48, 48, 48][pindex]
if p2 is None: p2 = [ 3, 0.5, 10, 0, 20, 0.2, 1, 1, 0, rp, rp][pindex]
if p3 is None: p3 = [ 1, 0.2, 20, 0, 20, 20, 2, 3, 0, 0, 0][pindex]
if p4 is None: p4 = [ 2, 20, 20, 0, 24, 3, 0, 2, 0, 0, 0][pindex]
if p4 is None: p4 = [ 2, 20, 20, 0, 24, 3, 0, 2, 0, 0, 0][pindex]
if p5 is None: p5 = [ 0, 3, 24, 0, 50, 30, 0, 0, 0, 0, 0][pindex]
if p6 is None: p6 = [ 0, 30, 50, 0, 0, 0, 0, 0, 0, 0, 0][pindex]
#paramerters check
#input type check
if not isinstance(input, vs.VideoNode):
raise ValueError(funcname + ': \"input\" must be a clip !')
#YUV constant value
inputFormatid = input.format.id # A unique id identifying the format.
sColorFamily = input.format.color_family # Which group of colorspaces the format describes.
sbits_per_sample = int(input.format.bits_per_sample) # How many bits are used to store one sample in one plane.
sSType = input.format.sample_type # source sample type
#format check
if sColorFamily == vs.YUV or sColorFamily == vs.GRAY:
if sSType != vs.INTEGER:
raise TypeError(funcname + ': \"input\" must be INTEGER format !')
else:
if not (sbits_per_sample == 8 or sbits_per_sample == 16):
raise TypeError(funcname + ': \"input\" must be 8bit or 16bit INTEGER !')
else:
raise TypeError(funcname + ': Only YUV colorfmaily is supported !')
#aatype check
if not isinstance(aatype, int) or (aatype < -3 or aatype > 7):
raise ValueError(funcname + ': \"aatype\" (int: -3~7) invalid !')
#lsb check
if not isinstance(lsb, bool):
raise TypeError(funcname + ': \"lsb\" must be BOOL !')
#preaa check
if not isinstance(preaa, int) or (preaa < 0 or preaa > 1):
raise ValueError(funcname + ': \"preaa\" (int: 0~1) invalid !')
#mtype check
if not isinstance(mtype, int):
if not isinstance(mtype, vs.VideoNode):
raise TypeError(funcname + ': \"mtype\" is not a clip !')
else:
if mtype.format.id != inputFormatid :
raise TypeError(funcname + ': \"input\" and \"mclip(mtype)\" must be of the same format !')
else:
if mtype.width != w or mtype.height != h:
raise TypeError(funcname + ': resolution of \"input\" and your custome mask clip \"mtype\" must match !')
else:
if mtype < 0 or mtype > 6:
raise ValueError(funcname + ': \"mtype\" (int: 0~6) invalid !')
#mthr check
if not isinstance(mthr, int) or (mthr < 0 or mthr > 255):
raise ValueError(funcname + ': \"mthr\" (int: 0~255) invalid !')
#repair check
if not isinstance(repair, int) or (repair < -24 or repair > 24):
raise ValueError(funcname + ': \"repair\" (int: -24~24) invalid !')
#src clip check
if src is not None and isinstance(src, vs.VideoNode):
if src.format.id != inputFormatid :
raise TypeError(funcname + ': \"input\" and \"src\" must be of the same format !')
else:
if src.width != w or src.height != h:
raise TypeError(funcname + ': resolution of \"input\" and \"src\" must match !')
elif src is not None:
raise ValueError(funcname + ': \"src\" is not a clip !')
#cycle check
if not isinstance(cycle, int) or cycle < 0:
raise ValueError(funcname + ': \"cycle\" must be non-negative int !')
#stabilize check
if not isinstance(stabilize, int) or (stabilize < -3 or stabilize > 3):
raise ValueError(funcname + ': \"stabilize\" (int: -3~3) invalid !')
if showmask and mtype == 0:
raise ValueError(funcname + ': There is NO mask to show when \"mtype\" = 0 !')
###################################
### Small functions ##############
###################################
# average two clips of 3 yuv planes
def average(clipa, clipb):
return (core.std.Expr(clips=[clipa,clipb], expr=["x y + 2 /"]))
# bitdepth conversion from mvsfunc, mawen1250 Thanks!
def Depth(input, depth=None):
sbitPS = input.format.bits_per_sample
if sbitPS == depth:
return input
else:
return core.fmtc.bitdepth(input,bits=depth,flt=0,dmode=3)
# fast PointResize from mvsfunc
def PointPower(input, vpow=1):
for i in range(vpow):
clip = core.std.Interleave([input,input]).std.DoubleWeave(tff=True).std.SelectEvery(2,0)
return clip
###################################
# src clip issue
#======================
if src == None:
if predown:
if lsb:
src = core.nnedi3.nnedi3(core.fmtc.resample(input, w=downw4, h=downh4,kernel="spline36"),field=1,dh=True)
src = core.std.Transpose(core.fmtc.resample(src,w=downw4,h=h,sx=0,sy=[-0.5,-0.5*(1<<input.format.subsampling_h)],kernel="spline36"))
src = core.std.Transpose(core.fmtc.resample(core.nnedi3.nnedi3(src,field=1,dh=True),w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<input.format.subsampling_h)],kernel="spline36"))
else:
src = core.nnedi3.nnedi3(Depth(core.fmtc.resample(input, w=downw4, h=downh4,kernel="spline36"),8),field=1,dh=True)
src = core.std.Transpose(core.fmtc.resample(src,w=downw4,h=h,sx=0,sy=[-0.5,-0.5*(1<<input.format.subsampling_h)],kernel="spline36"))
src = core.std.Transpose(core.fmtc.resample(core.nnedi3.nnedi3(Depth(src,8),field=1,dh=True),w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<input.format.subsampling_h)],kernel="spline36"))
else:
src = input
#======================
#internal function
def TAAmbk_prepass(clip, predown=predown, downw4=downw4, downh4=downh4, thin=0, dark=0, preaa=preaa):
if predown:
pdclip = core.resize.Spline36(clip, downw4, downh4)
else:
pdclip = clip
if preaa == 1:
if lsb:
nn = core.nnedi3.nnedi3(pdclip, field=3)
nnt = core.std.Transpose(core.nnedi3.nnedi3(core.std.Transpose(pdclip), field=3))
else:
nn = core.nnedi3.nnedi3(Depth(pdclip,8), field=3)
nnt = core.std.Transpose(core.nnedi3.nnedi3(Depth(core.std.Transpose(pdclip),8), field=3))
#nnedi3 double rate start with top
clph = average(core.std.SelectEvery(nn, cycle=2, offsets=0), core.std.SelectEvery(nn, cycle=2, offsets=1))
clpv = average(core.std.SelectEvery(nnt, cycle=2, offsets=0), core.std.SelectEvery(nnt, cycle=2, offsets=1))
clp = average(clph, clpv)
preaaB = clp
else:
preaaB = pdclip
preaaC = preaaB
#filters unavailable
#=======================================
# if thin == 0 and dark == 0:
# preaaC = preaaB
# else:
# if dark == 0:
# preaaC = core.warp.AWarpSharp2(preaaB,depth=thin)
# elif thin == 0:
# preaaC = Toon(preaaB,dark) #?
# else:
# preaaC = Toon(core.warp.AWarpSharp2(preaaB,depth=thin),dark) #?
#=======================================
return preaaC
#internal functions
def TAAmbk_mainpass(preaaC, aatype=aatype, cycle=cycle, p1=p1, p2=p2, p3=p3, p4=p4, p5=p5, p6=p6, w=w, h=h,
uph4=uph4, upw4=upw4, eedi3sclip=eedi3sclip):
# generate eedi3 sclip using nnedi3 double height
if eedi3sclip is True:
if aatype == -2:
if lsb:
sclip = core.nnedi3.nnedi3(preaaC,field=1,dh=True)
sclip_r = core.resize.Spline36(sclip,w,uph4)
sclip_r = core.std.Transpose(sclip_r)
sclip_r = core.nnedi3.nnedi3(sclip_r,field=1,dh=True)
sclip = Depth(sclip,8)
sclip_r = Depth(sclip_r,8)
else:
sclip = core.nnedi3.nnedi3(Depth(preaaC,8),field=1,dh=True)
sclip_r = core.resize.Spline36(sclip,w,uph4)
sclip_r = core.std.Transpose(sclip_r)
sclip_r = core.nnedi3.nnedi3(sclip_r,field=1,dh=True)
elif aatype == 2:
if lsb:
sclip = core.nnedi3.nnedi3(preaaC,field=1,dh=True)
sclip_r = sclip_r = core.resize.Spline36(sclip,w,h)
sclip_r = core.std.Transpose(sclip_r)
sclip_r = core.nnedi3.nnedi3(sclip_r,field=1,dh=True)
sclip = Depth(sclip,8)
sclip_r = Depth(sclip_r,8)
else:
sclip = core.nnedi3.nnedi3(Depth(preaaC,8),field=1,dh=True)
sclip_r = sclip_r = core.resize.Spline36(sclip,w,h)
sclip_r = core.std.Transpose(sclip_r)
sclip_r = core.nnedi3.nnedi3(sclip_r,field=1,dh=True)
# generate aa_clip
##########################
# # # AAtype -3 or 4 # # #
##########################
if aatype == -3 or aatype == 4:
if lsb:
aa_clip = core.nnedi3.nnedi3(preaaC, dh=True, field=1, nsize=int(p2), nns=int(p3), qual=int(p4))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=w,h=uph4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(aa_clip, dh=True, field=1, nsize=int(p2), nns=int(p3), qual=int(p4)),w=uph4,h=upw4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = Depth(aa_clip,depth=8)
aa_clip = core.sangnom.SangNomMod(core.std.Transpose(core.sangnom.SangNomMod(aa_clip,aa=int(p1))),aa=int(p1))
aa_clip = core.fmtc.resample(aa_clip,w=w,h=h,kernel=["spline36","spline36"])
else:
aa_clip = core.nnedi3.nnedi3(Depth(preaaC,8), dh=True, field=1, nsize=int(p2), nns=int(p3), qual=int(p4))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=w,h=uph4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(Depth(aa_clip,8), dh=True, field=1, nsize=int(p2), nns=int(p3), qual=int(p4)),w=uph4,h=upw4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = Depth(aa_clip,depth=8)
aa_clip = core.sangnom.SangNomMod(core.std.Transpose(core.sangnom.SangNomMod(aa_clip,aa=int(p1))),aa=int(p1))
aa_clip = core.fmtc.resample(aa_clip,w=w,h=h,kernel=["spline36","spline36"])
######################
# # # AA type -2 # # #
######################
elif aatype == -2:
if eedi3sclip == False:
aa_clip = core.fmtc.resample(core.eedi3.eedi3(Depth(preaaC,8), dh=True, field=1, alpha=p2, beta=p3, gamma=p4, nrad=int(p5), mdis=int(p6)), w=w, h=uph4, sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = Depth(aa_clip,depth=8)
aa_clip = core.eedi3.eedi3(core.std.Transpose(aa_clip), dh=True, field=1, alpha=p2, beta=p3, gamma=p4, nrad=int(p5), mdis=int(p6))
aa_clip = core.sangnom.SangNomMod(Depth(core.fmtc.resample(aa_clip, w=uph4, h=upw4, sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"),depth=8),aa=int(p1))
aa_clip = core.sangnom.SangNomMod(core.std.Transpose(aa_clip),aa=int(p1))
aa_clip = core.fmtc.resample(aa_clip,w=w,h=h,kernel=["spline36","spline36"])
else:
# EEDI3 need w * h
aa_clip = core.fmtc.resample(core.eedi3.eedi3(Depth(preaaC,8), dh=True, field=1, alpha=p2, beta=p3, gamma=p4, nrad=int(p5), mdis=int(p6), sclip=sclip), w=w, h=uph4, sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
# output w * uph4
aa_clip = Depth(aa_clip,depth=8)
# EEDI3 need uph4 * w
aa_clip = core.eedi3.eedi3(core.std.Transpose(aa_clip), dh=True, field=1, alpha=p2, beta=p3, gamma=p4, nrad=int(p5), mdis=int(p6), sclip=sclip_r)
aa_clip = core.sangnom.SangNomMod(Depth(core.fmtc.resample(aa_clip, w=uph4, h=upw4, sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"),depth=8),aa=int(p1))
aa_clip = core.sangnom.SangNomMod(core.std.Transpose(aa_clip),aa=int(p1))
aa_clip = core.fmtc.resample(aa_clip,w=w,h=h,kernel=["spline36","spline36"])
######################
# # # AA type -1 # # #
######################
elif aatype == -1:
aa_clip = core.fmtc.resample(core.eedi2.EEDI2(preaaC, field=1, mthresh=int(p2), lthresh=int(p3), vthresh=int(p4), maxd=int(p5), nt=int(p6)),w=w,h=uph4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.eedi2.EEDI2(core.std.Transpose(aa_clip),field=1, mthresh=int(p2), lthresh=int(p3), vthresh=int(p4), maxd=int(p5), nt=int(p6))
aa_clip = core.sangnom.SangNomMod(Depth(core.fmtc.resample(aa_clip,w=uph4,h=upw4,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"),depth=8),aa=int(p1))
aa_clip = core.sangnom.SangNomMod(core.std.Transpose(aa_clip),aa=int(p1))
aa_clip = core.fmtc.resample(aa_clip,w=w,h=h,kernel=["spline36","spline36"])
######################
# # # AA type 1 # # #
######################
elif aatype == 1:
aa_clip = core.fmtc.resample(core.eedi2.EEDI2(preaaC,field=1,mthresh=int(p1), lthresh=int(p2), vthresh=int(p3), maxd=int(p4), nt=int(p5)),w=w,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.eedi2.EEDI2(core.std.Transpose(aa_clip),field=1,mthresh=int(p1), lthresh=int(p2), vthresh=int(p3), maxd=int(p4), nt=int(p5))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
######################
# # # AA type 2 # # #
######################
elif aatype == 2:
if eedi3sclip == False:
aa_clip = core.fmtc.resample(core.eedi3.eedi3(Depth(preaaC,8),dh=True, field=1, alpha=p1, beta=p2, gamma=p3, nrad=int(p4), mdis=int(p5)),w=w,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = Depth(core.std.Transpose(aa_clip),depth=8)
aa_clip = core.fmtc.resample(core.eedi3.eedi3(aa_clip,dh=True, field=1, alpha=p1, beta=p2, gamma=p3, nrad=int(p4), mdis=int(p5)),w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.std.Transpose(aa_clip)
else:
#EEDI3 need w * h
aa_clip = core.fmtc.resample(core.eedi3.eedi3(Depth(preaaC,8),dh=True, field=1, alpha=p1, beta=p2, gamma=p3, nrad=int(p4), mdis=int(p5), sclip=sclip),w=w,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
#output w * h
aa_clip = Depth(core.std.Transpose(aa_clip),depth=8)
#EEDI3 need h * w
aa_clip = core.fmtc.resample(core.eedi3.eedi3(aa_clip,dh=True, field=1, alpha=p1, beta=p2, gamma=p3, nrad=int(p4), mdis=int(p5), sclip=sclip_r),w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.std.Transpose(aa_clip)
######################
# # # AA type 3 # # #
######################
elif aatype == 3:
if lsb:
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(preaaC, dh=True, field=1, nsize=int(p1), nns=int(p2), qual=int(p3)),w=w,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.nnedi3.nnedi3(core.std.Transpose(aa_clip), dh=True, field=1, nsize=int(p1), nns=int(p2), qual=int(p3))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
else:
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(Depth(preaaC,8), dh=True, field=1, nsize=int(p1), nns=int(p2), qual=int(p3)),w=w,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.nnedi3.nnedi3(Depth(core.std.Transpose(aa_clip),8), dh=True, field=1, nsize=int(p1), nns=int(p2), qual=int(p3))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
######################
# # # AA type 5 # # #
######################
elif aatype == 5:
aa_clip = Depth(core.fmtc.resample(preaaC, w=upw4, h=uph4 ,kernel=["lanczos","bicubic"]),depth=8)
aa_clip = core.std.Transpose(core.sangnom.SangNomMod(aa_clip,aa=int(p1)))
aa_clip = core.fmtc.resample(core.sangnom.SangNomMod(aa_clip,aa=int(p1)),w=h,h=w,kernel="spline36")
aa_clip = core.std.Transpose(aa_clip)
######################
# # # AA type 6 # # #
######################
elif aatype == 6:
aa_clip = Depth(core.fmtc.resample(preaaC, w=w, h=uph4 ,kernel=["lanczos","bicubic"]),depth=8)
if mod16w is True:
aa_clip = core.fmtc.resample(core.sangnom.SangNomMod(aa_clip,aa=int(p1)),w=w,h=h,kernel="spline36")
else:
aa_clip = core.std.AddBorders(aa_clip,borderW)
aa_clip = core.fmtc.resample(core.sangnom.SangNomMod(aa_clip,aa=int(p1)),w=w,h=h,kernel="spline36")
aa_clip = core.std.CropRel(aa_clip,borderW)
aa_clip = core.fmtc.resample(core.std.Transpose(aa_clip),w=h,h=upw4,kernel=["lanczos","bicubic"])
if mod16h is True:
aa_clip = core.sangnom.SangNomMod(Depth(aa_clip,depth=8),aa=int(p1))
else:
aa_clip = core.std.AddBorders(aa_clip,borderH)
aa_clip = core.sangnom.SangNomMod(Depth(aa_clip,depth=8),aa=int(p1))
aa_clip = core.std.CropRel(aa_clip,borderH)
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,kernel="spline36"))
aa_clip = core.rgvs.Repair(aa_clip, core.fmtc.resample(preaaC,w=w,h=h,kernel="spline64"), mode=int(p2))
######################
# # # AA type 7 # # #
######################
elif aatype == 7:
aa_clip = PointPower(Depth(preaaC,8))
if mod16w and not predown:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.Transpose(aa_clip)
elif predown:
if aa_clip.width == downw4:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.Transpose(aa_clip)
elif mod16w:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.Transpose(aa_clip)
else:
aa_clip = core.std.AddBorders(aa_clip,borderW)
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.CropRel(aa_clip,borderW)
aa_clip = core.std.Transpose(aa_clip)
else:
aa_clip = core.std.AddBorders(aa_clip,borderW)
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.CropRel(aa_clip,borderW)
aa_clip = core.std.Transpose(aa_clip)
aa_clip = PointPower(aa_clip)
if mod16h and not predown:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
elif predown:
if aa_clip.width == downh4 * 2:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
elif mod16h:
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
else:
aa_clip = core.std.AddBorders(aa_clip,(16 - h * 2 % 16))
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.CropRel(aa_clip,(16 - h * 2 % 16))
else:
aa_clip = core.std.AddBorders(aa_clip,(16 - h * 2 % 16))
aa_clip = core.sangnom.SangNomMod(aa_clip,aa=int(p1))
aa_clip = core.std.CropRel(aa_clip,(16 - h * 2 % 16))
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,kernel="spline36"))
if predown:
aa_clip = core.rgvs.Repair(aa_clip, core.fmtc.resample(preaaC,w=w,h=h,kernel="spline64"), mode=int(p2))
else:
aa_clip = core.rgvs.Repair(aa_clip, Depth(preaaC,16), mode=int(p2))
# if predown and no aa, use nnedi3 to recover
else:
if predown:
if lsb:
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(preaaC,dh=True, field=1, nsize=1, nns=3, qual=2),w=preaaC.width,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.nnedi3.nnedi3(core.std.Transpose(aa_clip),dh=True, field=1, nsize=1, nns=3, qual=2)
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
else:
aa_clip = core.fmtc.resample(core.nnedi3.nnedi3(Depth(preaaC,8),dh=True, field=1, nsize=1, nns=3, qual=2),w=preaaC.width,h=h,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36")
aa_clip = core.nnedi3.nnedi3(Depth(core.std.Transpose(aa_clip),8),dh=True, field=1, nsize=1, nns=3, qual=2)
aa_clip = core.std.Transpose(core.fmtc.resample(aa_clip,w=h,h=w,sx=0,sy=[-0.5,-0.5*(1<<preaaC.format.subsampling_h)],kernel="spline36"))
return aa_clip if cycle == 0 else TAAmbk_mainpass(aa_clip, aatype=aatype ,cycle=cycle-1, p1=p1, p2=p2, p3=p3, p4=p4, p5=p5, p6=p6, w=w, h=h, uph4=uph4, upw4=upw4, eedi3sclip=eedi3sclip)
#Internal functions
def TAAmbk_mask(input, mtype=mtype, mthr=mthr, w=w, mtype2=mtype2, mthr2=mthr2, auxmthr=auxmthr):
bits = input.format.bits_per_sample
shift = bits - 8
neutral = 128 << shift
peak = (1 << bits) - 1
multiple = peak / 255
#generate edge_mask_1
if mtype == 1:
edge_mask_1 = core.tcanny.TCanny(input, sigma=auxmthr, mode=1, op=2, planes=0)
exprY = "x "+str(mthr*multiple)+" <= x 2 / x 2 * ?"
edge_mask_1 = core.std.Expr(edge_mask_1, [exprY] if GRAY else [exprY,""])
if w > 1100:
edge_mask_1 = core.rgvs.RemoveGrain(edge_mask_1, [20] if GRAY else [20,0])
else:
edge_mask_1 = core.rgvs.RemoveGrain(edge_mask_1, [11] if GRAY else [11,0])
edge_mask_1 = core.generic.Inflate(edge_mask_1, planes=0)
elif mtype == 3:
edge_mask_1 = core.generic.TEdge(input, min=auxmthr, planes=0)
exprY = "x "+str(mthr*multiple/5)+" <= x 2 / x 16 * ?"
edge_mask_1 = core.std.Expr(edge_mask_1, [exprY] if GRAY else [exprY,""])
edge_mask_1 = core.generic.Deflate(edge_mask_1, planes=0)
if w > 1100:
edge_mask_1 = core.rgvs.RemoveGrain(edge_mask_1, [20] if GRAY else [20,0])
else:
edge_mask_1 = core.rgvs.RemoveGrain(edge_mask_1, [11] if GRAY else [11,0])
elif mtype == 2:
edge_mask_1 = core.msmoosh.MSharpen(input, threshold=mthr//5, strength=0, mask=True, planes=0)
elif mtype == 4:
edge_mask_1 = core.generic.Sobel(input, min=5, max=7, planes=0)
edge_mask_1 = core.generic.Inflate(edge_mask_1, planes=0)
elif mtype == 5:
edge_mask_1 = core.std.Convolution(input,[0, 0, 0, 0, 2, -1, 0, -1, 0],planes=0)
edge_mask_1 = core.generic.Inflate(edge_mask_1, planes=0)
elif mtype == 6:
edgemask1 = core.std.Convolution(input,[1, 1, 0, 1, 0, -1, 0, -1, -1],divisor=1,saturate=False,planes=0)
edgemask2 = core.std.Convolution(input,[1, 1, 1, 0, 0, 0, -1, -1, -1],divisor=1,saturate=False,planes=0)
edgemask3 = core.std.Convolution(input,[1, 0, -1, 1, 0, -1, 1, 0, -1],divisor=1,saturate=False,planes=0)
edgemask4 = core.std.Convolution(input,[0, -1, -1, 1, 0, -1, 1, 1, 0],divisor=1,saturate=False,planes=0)
mt = "x y max z max a max"
edge_mask_1 = core.std.Expr([edgemask1,edgemask2,edgemask3,edgemask4],[mt] if GRAY else [mt,""])
exprY = "x "+str(mthr*multiple)+" <= x 2 / x 2.639015821545 * ?"
edge_mask_1 = core.std.Expr(edge_mask_1, [exprY] if GRAY else [exprY,""])
edge_mask_1 = core.rgvs.RemoveGrain(edge_mask_1, [4] if GRAY else [4,0])
edge_mask_1 = core.generic.Inflate(edge_mask_1, planes=0)
else:
edge_mask_1 == None
#generate edge_mask_2
if mtype2 == 0:
edge_mask_2 = None
elif mtype2 == 1:
edge_mask_2 = core.tcanny.TCanny(input, sigma=1.2, mode=1, op=0, planes=0)
exprY = "x "+str(mthr2*multiple)+" <= x 2 / x 2 * ?"
edge_mask_2 = core.std.Expr(edge_mask_2, [exprY] if GRAY else [exprY,""])
if w > 1100:
edge_mask_2 = core.rgvs.RemoveGrain(edge_mask_2, [20] if GRAY else [20,0])
else:
edge_mask_2 = core.rgvs.RemoveGrain(edge_mask_2, [11] if GRAY else [11,0])
edge_mask_1 = core.generic.Inflate(edge_mask_2, planes=0)
elif mtype2 == 3:
edge_mask_2 = core.generic.TEdge(input, planes=0)
exprY = "x "+str(mthr2*multiple/5)+" <= x 2 / x 16 * ?"
edge_mask_2 = core.std.Expr(edge_mask_2, [exprY] if GRAY else [exprY,""])
edge_mask_2 = core.generic.Deflate(edge_mask_2, planes=0)
if w > 1100:
edge_mask_2 = core.rgvs.RemoveGrain(edge_mask_2, [20] if GRAY else [20,0])
else:
edge_mask_2 = core.rgvs.RemoveGrain(edge_mask_2, [11] if GRAY else [11,0])
elif mtype2 == 2:
edge_mask_2 = core.msmoosh.MSharpen(input, threshold=mthr2//5, strength=0, mask=True, planes=0)
elif mtype2 == 4:
edge_mask_2 = core.generic.Sobel(input, min=5, max=7, planes=0)
edge_mask_2 = core.generic.Inflate(edge_mask_2, planes=0)
elif mtype2 == 5:
edge_mask_1 = core.std.Convolution(input,[0, 0, 0, 0, 2, -1, 0, -1, 0],planes=0)
edge_mask_2 = core.generic.Inflate(edge_mask_2, planes=0)
else:
edgemask1 = core.std.Convolution(input,[1, 1, 0, 1, 0, -1, 0, -1, -1],divisor=1,saturate=False,planes=0)
edgemask2 = core.std.Convolution(input,[1, 1, 1, 0, 0, 0, -1, -1, -1],divisor=1,saturate=False,planes=0)
edgemask3 = core.std.Convolution(input,[1, 0, -1, 1, 0, -1, 1, 0, -1],divisor=1,saturate=False,planes=0)
edgemask4 = core.std.Convolution(input,[0, -1, -1, 1, 0, -1, 1, 1, 0],divisor=1,saturate=False,planes=0)
mt = "x y max z max a max"
edge_mask_2 = core.std.Expr([edgemask1,edgemask2,edgemask3,edgemask4],[mt] if GRAY else [mt,""])
exprY = "x "+str(mthr2*multiple)+" <= x 2 / x 2.639015821545 * ?"
edge_mask_2 = core.std.Expr(edge_mask_2, [exprY] if GRAY else [exprY,""])
edge_mask_2 = core.rgvs.RemoveGrain(edge_mask_2, [4] if GRAY else [4,0])
edge_mask_2 = core.generic.Inflate(edge_mask_2, planes=0)
#generate final_mask
if mtype2 == 0:
final_mask = edge_mask_1
else:
final_mask = core.std.Expr([edge_mask_1,edge_mask_2], ["x y max"] if GRAY else ["x y max",""])
return final_mask
#temporal stabilizer of sharped clip
def Soothe(sharp, origin, keep=24):
bits = sharp.format.bits_per_sample
shift = bits - 8
neutral = 128 << shift
peak = (1 << bits) - 1
multiple = peak / 255
const = 100 * multiple
if keep > 100:
keep = 100
if keep < 0:
keep = 0
KP = keep*multiple
mt1 = 'x y - {neutral} +'.format(neutral=neutral)
diff = core.std.Expr(clips=[origin,sharp], expr=[mt1])
diff2 = core.focus.TemporalSoften(diff, radius=1, luma_threshold=255, chroma_threshold=255, scenechange=32, mode=2)
expr = 'x {neutral} - y {neutral} - * 0 < x {neutral} - {const} / {KP} * {neutral} + x {neutral} - abs y {neutral} - abs > x {KP} * y {const} {KP} - * + {const} / x ? ?'.format(neutral=neutral, const=const, KP=KP)
diff3 = core.std.Expr(clips=[diff,diff2], expr=[expr])
mt2 = 'x y {neutral} - -'.format(neutral=neutral)
return core.std.Expr(clips=[origin,diff3], expr=[mt2])
#internal functions
def TAAmbk_stabilize(input, aaedsharp, stabilize):
aadiff = core.std.MakeDiff(Depth(input,16), aaedsharp)
if(stabilize < 0):
aadiff_stab = core.rgvs.Repair(core.focus.TemporalSoften(aadiff,abs(stabilize), 255, 255, 254, 2),aadiff,4)
else:
inputsuper = core.mv.Super(input,pel=1)
diffsuper = core.mv.Super(aadiff,pel=1,levels=1)
if stabilize == 3:
fv3 = core.mv.Analyse(inputsuper,isb=False,delta=3,overlap=8,blksize=16)
bv3 = core.mv.Analyse(inputsuper,isb=True,delta=3,overlap=8,blksize=16)
if stabilize >= 2:
fv2 = core.mv.Analyse(inputsuper,isb=False,delta=2,overlap=8,blksize=16)
bv2 = core.mv.Analyse(inputsuper,isb=True,delta=2,overlap=8,blksize=16)
if stabilize >= 1:
fv1 = core.mv.Analyse(inputsuper,isb=False,delta=1,overlap=8,blksize=16)
bv1 = core.mv.Analyse(inputsuper,isb=True,delta=1,overlap=8,blksize=16)
if stabilize == 1:
stabilized_diff = core.mv.Degrain1(aadiff,diffsuper,bv1,fv1)
elif stabilize == 2:
stabilized_diff = core.mv.Degrain2(aadiff,diffsuper,bv1,fv1,bv2,fv2)
elif stabilize == 3:
stabilized_diff = core.mv.Degrain3(aadiff,diffsuper,bv1,fv1,bv2,fv2,bv3,fv3)
else:
stabilized_diff = None
bits = aadiff.format.bits_per_sample
shift = bits - 8
neutral = 128 << shift
peak = (1 << bits) - 1
multiple = peak / 255
mt = 'x {neutral} - abs y {neutral} - abs < x y ?'.format(neutral=neutral)
aadiff_stab = core.std.Expr(clips=[aadiff,stabilized_diff], expr=[mt])
aadiff_stab = core.std.Merge(aadiff_stab, stabilized_diff, [0.6] if GRAY else [0.6,0])
aaed_stab = core.std.MakeDiff(Depth(input,16), aadiff_stab)
return aaed_stab
#==============================
#main functions
#==============================
preaaC = TAAmbk_prepass(input, predown=predown, downw4=downw4, downh4=downh4, preaa=preaa)
aa_clip = TAAmbk_mainpass(preaaC,aatype=aatype, cycle=cycle, p1=p1, p2=p2, p3=p3, p4=p4, p5=p5, p6=p6, w=w, h=h, uph4=uph4, upw4=upw4, eedi3sclip=eedi3sclip)
#sharp
if sharp == 0:
aaedsp = aa_clip
elif sharp >= 1:
aaedsp = haf.LSFmod(aa_clip,strength=int(absSh), defaults="old", source=Depth(src,16))
elif sharp > 0:
per = int(40*absSh)
matrix = [-1, -2, -1, -2, 52-per , -2, -1, -2, -1]
aaedsp = core.generic.Convolution(aa_clip,matrix)
elif sharp > -1:
aaedsp = haf.LSFmod(aa_clip,strength=round(absSh*100), defaults="fast", source=Depth(src,16))
elif sharp == -1:
if w > 1100:
clipb = core.std.MakeDiff(aa_clip, core.rgvs.RemoveGrain(aa_clip, mode=20))
else:
clipb = core.std.MakeDiff(aa_clip, core.rgvs.RemoveGrain(aa_clip, mode=11))
clipb = core.rgvs.Repair(clipb, core.std.MakeDiff(Depth(src,16), aa_clip),mode=13)
aaedsp = core.std.MergeDiff(aa_clip, clipb)
else:
aaedsp = haf.LSFmod(aa_clip,strength=int(absSh), defaults="slow", source=Depth(src,16))
#postAA
if postaa:
aaedsp = Soothe(aaedsp,aa_clip,keep=48)
#stabilize
if stabilize != 0:
aaedstab = TAAmbk_stabilize(input, aaedsp, stabilize)
else:
aaedstab = aaedsp
#masked merge
if isinstance(mtype, vs.VideoNode):
edge_mask = mtype
aamerge = core.std.MaskedMerge(Depth(input,16),aaedstab,Depth(edge_mask,16),first_plane=True)
elif mtype != 0:
edge_mask = TAAmbk_mask(input, mtype=mtype, mthr=mthr, w=w, mtype2=mtype2, mthr2=mthr2, auxmthr=auxmthr)
aamerge = core.std.MaskedMerge(Depth(input,16),aaedstab,Depth(edge_mask,16),first_plane=True)
else:
aamerge = aaedstab
# output
if showmask:
return edge_mask
else:
if repair == 0 or aatype == 0:
return aamerge
elif(repair > 0):
return core.rgvs.Repair(aamerge, Depth(input,depth=16), mode=repair)
else:
return core.rgvs.Repair(Depth(input,depth=16), aamerge, mode=abs(repair))
|
nilq/baby-python
|
python
|
#!/usr/bin/env python3
from setuptools import setup
setup(
name='asyncpgsa',
version=__import__('asyncpgsa').__version__,
install_requires=[
'asyncpg~=0.9.0',
'sqlalchemy',
],
packages=['asyncpgsa', 'asyncpgsa.testing'],
url='https://github.com/canopytax/asyncpgsa',
license='Apache 2.0',
author='nhumrich',
author_email='nick.humrich@canopytax.com',
description='sqlalchemy support for asyncpg'
)
|
nilq/baby-python
|
python
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.