content
stringlengths
1
1.05M
input_ids
listlengths
1
883k
ratio_char_token
float64
1
22.9
token_count
int64
1
883k
import os import base64 import botocore import boto3 import json import urllib from chalice import BadRequestError from chalice import ChaliceViewError from chalice import Chalice app = Chalice(app_name='a3x') app.debug = True REGION = 'us-east-1' BUCKET = 'freko-001' S3 = boto3.resource('s3') REKOGNITION = boto3.client('rekognition')
[ 11748, 28686, 198, 11748, 2779, 2414, 198, 11748, 10214, 420, 382, 198, 11748, 275, 2069, 18, 198, 11748, 33918, 198, 11748, 2956, 297, 571, 198, 6738, 442, 282, 501, 1330, 7772, 18453, 12331, 198, 6738, 442, 282, 501, 1330, 30529, 501,...
2.685039
127
# -------------------------------------------------------- # Written by Yufei Ye (https://github.com/JudyYe) # -------------------------------------------------------- from __future__ import print_function from __future__ import absolute_import from __future__ import division # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved. # customize laplacian argument import torch def mesh_laplacian_smoothing(meshes, verts_packed=None, method: str = "uniform"): r""" Computes the laplacian smoothing objective for a batch of meshes. This function supports three variants of Laplacian smoothing, namely with uniform weights("uniform"), with cotangent weights ("cot"), and cotangent cuvature ("cotcurv").For more details read [1, 2]. Args: meshes: Meshes object with a batch of meshes. method: str specifying the method for the laplacian. Returns: loss: Average laplacian smoothing loss across the batch. Returns 0 if meshes contains no meshes or all empty meshes. Consider a mesh M = (V, F), with verts of shape Nx3 and faces of shape Mx3. The Laplacian matrix L is a NxN tensor such that LV gives a tensor of vectors: for a uniform Laplacian, LuV[i] points to the centroid of its neighboring vertices, a cotangent Laplacian LcV[i] is known to be an approximation of the surface normal, while the curvature variant LckV[i] scales the normals by the discrete mean curvature. For vertex i, assume S[i] is the set of neighboring vertices to i, a_ij and b_ij are the "outside" angles in the two triangles connecting vertex v_i and its neighboring vertex v_j for j in S[i], as seen in the diagram below. .. code-block:: python a_ij /\ / \ / \ / \ v_i /________\ v_j \ / \ / \ / \ / \/ b_ij The definition of the Laplacian is LV[i] = sum_j w_ij (v_j - v_i) For the uniform variant, w_ij = 1 / |S[i]| For the cotangent variant, w_ij = (cot a_ij + cot b_ij) / (sum_k cot a_ik + cot b_ik) For the cotangent curvature, w_ij = (cot a_ij + cot b_ij) / (4 A[i]) where A[i] is the sum of the areas of all triangles containing vertex v_i. There is a nice trigonometry identity to compute cotangents. Consider a triangle with side lengths A, B, C and angles a, b, c. .. code-block:: python c /|\ / | \ / | \ B / H| \ A / | \ / | \ /a_____|_____b\ C Then cot a = (B^2 + C^2 - A^2) / 4 * area We know that area = CH/2, and by the law of cosines we have A^2 = B^2 + C^2 - 2BC cos a => B^2 + C^2 - A^2 = 2BC cos a Putting these together, we get: B^2 + C^2 - A^2 2BC cos a _______________ = _________ = (B/H) cos a = cos a / sin a = cot a 4 * area 2CH [1] Desbrun et al, "Implicit fairing of irregular meshes using diffusion and curvature flow", SIGGRAPH 1999. [2] Nealan et al, "Laplacian Mesh Optimization", Graphite 2006. """ if meshes.isempty(): return torch.tensor( [0.0], dtype=torch.float32, device=meshes.device, requires_grad=True ) N = len(meshes) if verts_packed is None: verts_packed = meshes.verts_packed() # (sum(V_n), 3) num_verts_per_mesh = meshes.num_verts_per_mesh() # (N,) verts_packed_idx = meshes.verts_packed_to_mesh_idx() # (sum(V_n),) weights = num_verts_per_mesh.gather(0, verts_packed_idx) # (sum(V_n),) weights = 1.0 / weights.float() # We don't want to backprop through the computation of the Laplacian; # just treat it as a magic constant matrix that is used to transform # verts into normals with torch.no_grad(): if method == "uniform": L = meshes.laplacian_packed() elif method in ["cot", "cotcurv"]: L, inv_areas = laplacian_cot(meshes) if method == "cot": norm_w = torch.sparse.sum(L, dim=1).to_dense().view(-1, 1) idx = norm_w > 0 norm_w[idx] = 1.0 / norm_w[idx] else: norm_w = 0.25 * inv_areas else: raise ValueError("Method should be one of {uniform, cot, cotcurv}") if method == "uniform": loss = L.mm(verts_packed) elif method == "cot": loss = L.mm(verts_packed) * norm_w - verts_packed elif method == "cotcurv": loss = (L.mm(verts_packed) - verts_packed) * norm_w loss = loss.norm(dim=1) loss = loss * weights return loss.sum() / N def laplacian_cot(meshes): """ Returns the Laplacian matrix with cotangent weights and the inverse of the face areas. Args: meshes: Meshes object with a batch of meshes. Returns: 2-element tuple containing - **L**: FloatTensor of shape (V,V) for the Laplacian matrix (V = sum(V_n)) Here, L[i, j] = cot a_ij + cot b_ij iff (i, j) is an edge in meshes. See the description above for more clarity. - **inv_areas**: FloatTensor of shape (V,) containing the inverse of sum of face areas containing each vertex """ verts_packed = meshes.verts_packed() # (sum(V_n), 3) faces_packed = meshes.faces_packed() # (sum(F_n), 3) # V = sum(V_n), F = sum(F_n) V, F = verts_packed.shape[0], faces_packed.shape[0] face_verts = verts_packed[faces_packed] v0, v1, v2 = face_verts[:, 0], face_verts[:, 1], face_verts[:, 2] # Side lengths of each triangle, of shape (sum(F_n),) # A is the side opposite v1, B is opposite v2, and C is opposite v3 A = (v1 - v2).norm(dim=1) B = (v0 - v2).norm(dim=1) C = (v0 - v1).norm(dim=1) # Area of each triangle (with Heron's formula); shape is (sum(F_n),) s = 0.5 * (A + B + C) # note that the area can be negative (close to 0) causing nans after sqrt() # we clip it to a small positive value area = (s * (s - A) * (s - B) * (s - C)).clamp_(min=1e-12).sqrt() # Compute cotangents of angles, of shape (sum(F_n), 3) A2, B2, C2 = A * A, B * B, C * C cota = (B2 + C2 - A2) / area cotb = (A2 + C2 - B2) / area cotc = (A2 + B2 - C2) / area cot = torch.stack([cota, cotb, cotc], dim=1) cot /= 4.0 # Construct a sparse matrix by basically doing: # L[v1, v2] = cota # L[v2, v0] = cotb # L[v0, v1] = cotc ii = faces_packed[:, [1, 2, 0]] jj = faces_packed[:, [2, 0, 1]] idx = torch.stack([ii, jj], dim=0).view(2, F * 3) L = torch.sparse.FloatTensor(idx, cot.view(-1), (V, V)) # Make it symmetric; this means we are also setting # L[v2, v1] = cota # L[v0, v2] = cotb # L[v1, v0] = cotc L += L.t() # For each vertex, compute the sum of areas for triangles containing it. idx = faces_packed.view(-1) inv_areas = torch.zeros(V, dtype=torch.float32, device=meshes.device) val = torch.stack([area] * 3, dim=1).view(-1) inv_areas.scatter_add_(0, idx, val) idx = inv_areas > 0 inv_areas[idx] = 1.0 / inv_areas[idx] inv_areas = inv_areas.view(-1, 1) return L, inv_areas
[ 2, 20368, 22369, 198, 2, 22503, 416, 575, 3046, 20295, 11609, 357, 5450, 1378, 12567, 13, 785, 14, 26141, 88, 35543, 8, 198, 2, 20368, 22369, 198, 6738, 11593, 37443, 834, 1330, 3601, 62, 8818, 198, 6738, 11593, 37443, 834, 1330, 4112...
2.214329
3,336
import shutil import traceback from pathlib import Path from pprint import pprint from typing import List, Optional, Union from marshmallow import ValidationError from bioimageio.spec import export_resource_package, load_raw_resource_description from bioimageio.spec.shared.raw_nodes import URI from bioimageio.spec.shared.utils import resolve_uri def package( rdf_source: Union[Path, str, URI, dict], path: Path = Path() / "{src_name}-package.zip", update_format: bool = False, weights_priority_order: Optional[List[str]] = None, verbose: bool = False, ) -> int: """Package a BioImage.IO resource described by a BioImage.IO Resource Description File (RDF).""" code = validate(rdf_source, update_format=update_format, update_format_inner=update_format, verbose=verbose) source_name = rdf_source.get("name") if isinstance(rdf_source, dict) else rdf_source if code: print(f"Cannot export invalid BioImage.IO RDF {source_name}") return code try: tmp_package_path = export_resource_package( rdf_source, update_to_current_format=update_format, weights_priority_order=weights_priority_order ) except Exception as e: print(f"Failed to package {source_name} due to: {e}") if verbose: traceback.print_exc() return 1 try: rdf_local_source = resolve_uri(rdf_source) path = path.with_name(path.name.format(src_name=rdf_local_source.stem)) shutil.move(tmp_package_path, path) except Exception as e: print(f"Failed to move package from {tmp_package_path} to {path} due to: {e}") if verbose: traceback.print_exc() return 1 print(f"exported bioimageio package from {source_name} to {path}") return 0 def validate( rdf_source: Union[Path, str, URI, dict], update_format: bool = False, update_format_inner: bool = None, verbose: bool = False, ) -> int: """Validate a BioImage.IO Resource Description File (RDF).""" if update_format_inner is None: update_format_inner = update_format source_name = rdf_source.get("name") if isinstance(rdf_source, dict) else rdf_source try: raw_rd = load_raw_resource_description(rdf_source, update_to_current_format=update_format) except ValidationError as e: print(f"Invalid {source_name}:") pprint(e.normalized_messages()) return 1 except Exception as e: print(f"Could not validate {source_name}:") pprint(e) if verbose: traceback.print_exc() return 1 code = 0 if raw_rd.type == "collection": for inner_category in ["application", "collection", "dataset", "model", "notebook"]: for inner in getattr(raw_rd, inner_category) or []: try: inner_source = inner.source except Exception as e: pprint(e) code += 1 else: code += validate(inner_source, update_format_inner, update_format_inner, verbose) if code: print(f"Found invalid RDFs in collection {source_name}.") if not code: print(f"successfully verified {raw_rd.type} {source_name}") return code
[ 11748, 4423, 346, 198, 11748, 12854, 1891, 198, 6738, 3108, 8019, 1330, 10644, 198, 6738, 279, 4798, 1330, 279, 4798, 198, 6738, 19720, 1330, 7343, 11, 32233, 11, 4479, 198, 198, 6738, 22397, 42725, 1330, 3254, 24765, 12331, 198, 198, 6...
2.430996
1,355
import numpy as np
[ 11748, 299, 32152, 355, 45941, 628 ]
3.333333
6
''' MIT License Copyright (c) 2020 Autonomous Vision Group (AVG), Max Planck Institute for Intelligent Systems Tbingen 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. ''' # Copyright (c) 2020,21 NVIDIA CORPORATION & AFFILIATES.. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # 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. ''' The functions in file is mostly borrowed from https://github.com/autonomousvision/differentiable_volumetric_rendering/blob/11542ed5ac4e7e4c19c5c74eba7929c1333f3896/im2mesh/dvr/models/__init__.py with some modifications. Codes released under MIT license ''' import torch import torch.nn as nn import torch.nn.functional as F from .decoder import Decoder from .conv import Resnet18 import numpy as np ########################################################
[ 7061, 6, 198, 36393, 13789, 198, 198, 15269, 357, 66, 8, 12131, 5231, 38175, 19009, 4912, 357, 10116, 38, 828, 220, 5436, 5224, 694, 5136, 329, 49452, 11998, 309, 4623, 268, 198, 198, 5990, 3411, 318, 29376, 7520, 11, 1479, 286, 3877,...
3.706954
604
""" Slixmpp: The Slick XMPP Library Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout This file is part of Slixmpp. See the file LICENSE for copying permission. """ import logging from slixmpp.xmlstream import register_stanza_plugin from slixmpp.plugins.base import BasePlugin, register_plugin log = logging.getLogger(__name__) register_plugin(XEP_0223)
[ 37811, 198, 220, 220, 220, 3454, 844, 76, 381, 25, 383, 3454, 624, 1395, 7378, 47, 10074, 198, 220, 220, 220, 15069, 357, 34, 8, 2321, 32607, 2271, 417, 327, 13, 45954, 11, 18990, 449, 13, 51, 13, 40275, 198, 220, 220, 220, 770, ...
2.865672
134
#! python3 # Combines all the pafs in the current working directory into a single pdf import PyPDF2, os, sys, logging
[ 2, 0, 21015, 18, 198, 2, 14336, 1127, 477, 262, 279, 1878, 82, 287, 262, 1459, 1762, 8619, 656, 257, 2060, 37124, 198, 198, 11748, 9485, 20456, 17, 11, 28686, 11, 25064, 11, 18931, 628, 628, 198 ]
3.324324
37
# -*- coding: utf-8 -*- import traceback from JobBrowserBFF.TestBase import TestBase from biokbase.Errors import ServiceError import unittest import re UPSTREAM_SERVICE = 'mock' ENV = 'mock' JOB_ID_WITH_LOGS = '59820c93e4b06f68bf751eeb' # non-admin JOB_ID_NO_LOGS = '5cf1522aaa5a4d298c5dc2ff' # non-admin JOB_ID_NOT_FOUND = '5cf1522aaa5a4d298c5dc2fe' # non-admin JOB_ID_NO_PERMISSION = '57ec06aee4b0b05cf8996b89' # access it as non-admin user TIMEOUT_MS = 10000
[ 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 11748, 12854, 1891, 198, 6738, 15768, 46532, 33, 5777, 13, 14402, 14881, 1330, 6208, 14881, 198, 6738, 3182, 482, 8692, 13, 9139, 5965, 1330, 4809, 12331, 198, 11748, 555,...
2.186916
214
"""Index related entity names Revision ID: 323f8d77567b Revises: 82b34e2777a4 Create Date: 2016-11-16 13:00:25.782487 """ # revision identifiers, used by Alembic. revision = '323f8d77567b' down_revision = '82b34e2777a4' from alembic import op import sqlalchemy as sa
[ 37811, 15732, 3519, 9312, 3891, 198, 198, 18009, 1166, 4522, 25, 38446, 69, 23, 67, 34483, 3134, 65, 198, 18009, 2696, 25, 9415, 65, 2682, 68, 1983, 3324, 64, 19, 198, 16447, 7536, 25, 1584, 12, 1157, 12, 1433, 1511, 25, 405, 25, ...
2.504587
109
import random high_score = 0 dice_game()
[ 11748, 4738, 198, 198, 8929, 62, 26675, 796, 657, 628, 198, 198, 67, 501, 62, 6057, 3419, 198 ]
2.5
18
"""Copyright [2020] [Jiatong Shi]. 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. """ # !/usr/bin/env python3 import copy import librosa from librosa.display import specshow import matplotlib.pyplot as plt import numpy as np import os from scipy import signal import soundfile as sf from SVS.model.layers.global_mvn import GlobalMVN import SVS.utils.metrics as Metrics import time import torch # from SVS.model.layers.utterance_mvn import UtteranceMVN # from pathlib import Path device = torch.device("cuda" if torch.cuda.is_available() else "cpu") def collect_stats(train_loader, args): """collect_stats.""" print("get in collect stats", flush=True) count, sum, sum_square = 0, 0, 0 count_mel, sum_mel, sum_square_mel = 0, 0, 0 for ( step, ( phone, beat, pitch, spec, real, imag, length, chars, char_len_list, mel, ), ) in enumerate(train_loader, 1): # print(f"spec.shape: {spec.shape},length.shape: # {length.shape}, mel.shape: {mel.shape}") for i, seq in enumerate(spec.cpu().numpy()): # print(f"seq.shape: {seq.shape}") seq_length = torch.max(length[i]) # print(seq_length) seq = seq[:seq_length] sum += seq.sum(0) sum_square += (seq ** 2).sum(0) count += len(seq) for i, seq in enumerate(mel.cpu().numpy()): seq_length = torch.max(length[i]) seq = seq[:seq_length] sum_mel += seq.sum(0) sum_square_mel += (seq ** 2).sum(0) count_mel += len(seq) assert count_mel == count dirnames = [ os.path.dirname(args.stats_file), os.path.dirname(args.stats_mel_file), ] for name in dirnames: if not os.path.exists(name): os.makedirs(name) np.savez( args.stats_file, count=count, sum=sum, sum_square=sum_square, ) np.savez( args.stats_mel_file, count=count_mel, sum=sum_mel, sum_square=sum_square_mel, ) def train_one_epoch( train_loader, model, device, optimizer, criterion, perceptual_entropy, epoch, args, ): """train_one_epoch.""" losses = AverageMeter() spec_losses = AverageMeter() if args.perceptual_loss > 0: pe_losses = AverageMeter() if args.n_mels > 0: mel_losses = AverageMeter() # mcd_metric = AverageMeter() # f0_distortion_metric, vuv_error_metric = # AverageMeter(), AverageMeter() if args.double_mel_loss: double_mel_losses = AverageMeter() model.train() log_save_dir = os.path.join( args.model_save_dir, "epoch{}/log_train_figure".format(epoch) ) if not os.path.exists(log_save_dir): os.makedirs(log_save_dir) start = time.time() # f0_ground_truth_all = np.reshape(np.array([]), (-1, 1)) # f0_synthesis_all = np.reshape(np.array([]), (-1, 1)) for ( step, ( phone, beat, pitch, spec, real, imag, length, chars, char_len_list, mel, ), ) in enumerate(train_loader, 1): phone = phone.to(device) beat = beat.to(device) pitch = pitch.to(device).float() spec = spec.to(device).float() if mel is not None: mel = mel.to(device).float() real = real.to(device).float() imag = imag.to(device).float() length_mask = length.unsqueeze(2) if mel is not None: length_mel_mask = length_mask.repeat(1, 1, mel.shape[2]).float() length_mel_mask = length_mel_mask.to(device) length_mask = length_mask.repeat(1, 1, spec.shape[2]).float() length_mask = length_mask.to(device) length = length.to(device) char_len_list = char_len_list.to(device) if not args.use_asr_post: chars = chars.to(device) char_len_list = char_len_list.to(device) else: phone = phone.float() # output = [batch size, num frames, feat_dim] # output_mel = [batch size, num frames, n_mels dimension] if args.model_type == "GLU_Transformer": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "LSTM": output, hidden, output_mel, output_mel2 = model(phone, pitch, beat) att = None elif args.model_type == "GRU_gs": output, att, output_mel = model(spec, phone, pitch, beat, length, args) att = None elif args.model_type == "PureTransformer": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "Conformer": # print(f"chars: {np.shape(chars)}, phone: # {np.shape(phone)}, length: {np.shape(length)}") output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "Comformer_full": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "USTC_DAR": output_mel = model( phone, pitch, beat, length, args ) # mel loss written in spec loss att = None spec_origin = spec.clone() mel_origin = mel.clone() if args.normalize: sepc_normalizer = GlobalMVN(args.stats_file) mel_normalizer = GlobalMVN(args.stats_mel_file) spec, _ = sepc_normalizer(spec, length) mel, _ = mel_normalizer(mel, length) if args.model_type == "USTC_DAR": spec_loss = 0 else: spec_loss = criterion(output, spec, length_mask) if args.n_mels > 0: mel_loss = criterion(output_mel, mel, length_mel_mask) if args.double_mel_loss: double_mel_loss = criterion(output_mel2, mel, length_mel_mask) else: double_mel_loss = 0 else: mel_loss = 0 double_mel_loss = 0 train_loss = mel_loss + double_mel_loss + spec_loss if args.perceptual_loss > 0: pe_loss = perceptual_entropy(output, real, imag) final_loss = ( args.perceptual_loss * pe_loss + (1 - args.perceptual_loss) * train_loss ) else: final_loss = train_loss final_loss = final_loss / args.accumulation_steps final_loss.backward() if args.gradclip > 0: torch.nn.utils.clip_grad_norm_(model.parameters(), args.gradclip) if (epoch + 1) % args.accumulation_steps == 0: if args.optimizer == "noam": optimizer.step_and_update_lr() else: optimizer.step() # optimizer.zero_grad() losses.update(final_loss.item(), phone.size(0)) if args.model_type != "USTC_DAR": spec_losses.update(spec_loss.item(), phone.size(0)) if args.perceptual_loss > 0: pe_losses.update(pe_loss.item(), phone.size(0)) if args.n_mels > 0: mel_losses.update(mel_loss.item(), phone.size(0)) if args.double_mel_loss: double_mel_losses.update(double_mel_loss.item(), phone.size(0)) if step % args.train_step_log == 0: end = time.time() if args.model_type == "USTC_DAR": # normalize inverse inferlogwav,mcd if args.normalize and args.stats_file: output_mel, _ = mel_normalizer.inverse(output_mel, length) log_figure_mel( step, output_mel, mel_origin, att, length, log_save_dir, args, ) out_log = "step {}: train_loss {:.4f}; spec_loss {:.4f};".format( step, losses.avg, spec_losses.avg ) else: # normalize inverse inferlogwav,mcd if args.normalize and args.stats_file: output, _ = sepc_normalizer.inverse(output, length) log_figure(step, output, spec_origin, att, length, log_save_dir, args) out_log = "step {}: train_loss {:.4f}; spec_loss {:.4f};".format( step, losses.avg, spec_losses.avg ) if args.perceptual_loss > 0: out_log += "pe_loss {:.4f}; ".format(pe_losses.avg) if args.n_mels > 0: out_log += "mel_loss {:.4f}; ".format(mel_losses.avg) if args.double_mel_loss: out_log += "dmel_loss {:.4f}; ".format(double_mel_losses.avg) print("{} -- sum_time: {:.2f}s".format(out_log, (end - start))) info = {"loss": losses.avg, "spec_loss": spec_losses.avg} if args.perceptual_loss > 0: info["pe_loss"] = pe_losses.avg if args.n_mels > 0: info["mel_loss"] = mel_losses.avg return info def validate(dev_loader, model, device, criterion, perceptual_entropy, epoch, args): """validate.""" losses = AverageMeter() spec_losses = AverageMeter() if args.perceptual_loss > 0: pe_losses = AverageMeter() if args.n_mels > 0: mel_losses = AverageMeter() mcd_metric = AverageMeter() if args.double_mel_loss: double_mel_losses = AverageMeter() model.eval() log_save_dir = os.path.join( args.model_save_dir, "epoch{}/log_val_figure".format(epoch) ) if not os.path.exists(log_save_dir): os.makedirs(log_save_dir) start = time.time() with torch.no_grad(): for ( step, ( phone, beat, pitch, spec, real, imag, length, chars, char_len_list, mel, ), ) in enumerate(dev_loader, 1): phone = phone.to(device) beat = beat.to(device) pitch = pitch.to(device).float() spec = spec.to(device).float() if mel is not None: mel = mel.to(device).float() real = real.to(device).float() imag = imag.to(device).float() length_mask = length.unsqueeze(2) if mel is not None: length_mel_mask = length_mask.repeat(1, 1, mel.shape[2]).float() length_mel_mask = length_mel_mask.to(device) length_mask = length_mask.repeat(1, 1, spec.shape[2]).float() length_mask = length_mask.to(device) length = length.to(device) char_len_list = char_len_list.to(device) if not args.use_asr_post: chars = chars.to(device) char_len_list = char_len_list.to(device) else: phone = phone.float() if args.model_type == "GLU_Transformer": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "LSTM": output, hidden, output_mel, output_mel2 = model(phone, pitch, beat) att = None elif args.model_type == "GRU_gs": output, att, output_mel = model(spec, phone, pitch, beat, length, args) att = None elif args.model_type == "PureTransformer": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "Conformer": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "Comformer_full": output, att, output_mel, output_mel2 = model( chars, phone, pitch, beat, pos_char=char_len_list, pos_spec=length, ) elif args.model_type == "USTC_DAR": output_mel = model(phone, pitch, beat, length, args) att = None spec_origin = spec.clone() mel_origin = mel.clone() if args.normalize: sepc_normalizer = GlobalMVN(args.stats_file) mel_normalizer = GlobalMVN(args.stats_mel_file) spec, _ = sepc_normalizer(spec, length) mel, _ = mel_normalizer(mel, length) if args.model_type == "USTC_DAR": spec_loss = 0 else: spec_loss = criterion(output, spec, length_mask) if args.n_mels > 0: mel_loss = criterion(output_mel, mel, length_mel_mask) if args.double_mel_loss: double_mel_loss = criterion(output_mel2, mel, length_mel_mask) else: double_mel_loss = 0 else: mel_loss = 0 double_mel_loss = 0 dev_loss = mel_loss + double_mel_loss + spec_loss if args.perceptual_loss > 0: pe_loss = perceptual_entropy(output, real, imag) final_loss = ( args.perceptual_loss * pe_loss + (1 - args.perceptual_loss) * dev_loss ) else: final_loss = dev_loss losses.update(final_loss.item(), phone.size(0)) if args.model_type != "USTC_DAR": spec_losses.update(spec_loss.item(), phone.size(0)) if args.perceptual_loss > 0: # pe_loss = perceptual_entropy(output, real, imag) pe_losses.update(pe_loss.item(), phone.size(0)) if args.n_mels > 0: mel_losses.update(mel_loss.item(), phone.size(0)) if args.double_mel_loss: double_mel_losses.update(double_mel_loss.item(), phone.size(0)) if args.model_type == "USTC_DAR": # normalize inverse stage if args.normalize and args.stats_file: output_mel, _ = mel_normalizer.inverse(output_mel, length) mcd_value, length_sum = ( 0, 1, ) # FIX ME! Calculate_melcd_fromMelSpectrum else: # normalize inverse stage if args.normalize and args.stats_file: output, _ = sepc_normalizer.inverse(output, length) (mcd_value, length_sum,) = Metrics.Calculate_melcd_fromLinearSpectrum( output, spec_origin, length, args ) mcd_metric.update(mcd_value, length_sum) if step % args.dev_step_log == 0: if args.model_type == "USTC_DAR": log_figure_mel( step, output_mel, mel_origin, att, length, log_save_dir, args, ) else: log_figure( step, output, spec_origin, att, length, log_save_dir, args, ) out_log = ( "step {}: train_loss {:.4f}; " "spec_loss {:.4f}; mcd_value {:.4f};".format( step, losses.avg, spec_losses.avg, mcd_metric.avg ) ) if args.perceptual_loss > 0: out_log += "pe_loss {:.4f}; ".format(pe_losses.avg) if args.n_mels > 0: out_log += "mel_loss {:.4f}; ".format(mel_losses.avg) if args.double_mel_loss: out_log += "dmel_loss {:.4f}; ".format(double_mel_losses.avg) end = time.time() print("{} -- sum_time: {}s".format(out_log, (end - start))) info = { "loss": losses.avg, "spec_loss": spec_losses.avg, "mcd_value": mcd_metric.avg, } if args.perceptual_loss > 0: info["pe_loss"] = pe_losses.avg if args.n_mels > 0: info["mel_loss"] = mel_losses.avg return info def save_checkpoint(state, model_filename): """save_checkpoint.""" torch.save(state, model_filename) return 0 def save_model( args, epoch, model, optimizer, train_info, dev_info, logger, save_loss_select, ): """save_model.""" if args.optimizer == "noam": save_checkpoint( { "epoch": epoch, "state_dict": model.state_dict(), "optimizer": optimizer._optimizer.state_dict(), }, "{}/epoch_{}_{}.pth.tar".format( args.model_save_dir, save_loss_select, epoch ), ) else: save_checkpoint( { "epoch": epoch, "state_dict": model.state_dict(), }, "{}/epoch_{}_{}.pth.tar".format( args.model_save_dir, save_loss_select, epoch ), ) # record training and validation information if args.use_tfboard: record_info(train_info, dev_info, epoch, logger) def record_info(train_info, dev_info, epoch, logger): """record_info.""" loss_info = { "train_loss": train_info["loss"], "dev_loss": dev_info["loss"], } logger.add_scalars("losses", loss_info, epoch) return 0 def invert_spectrogram(spectrogram, win_length, hop_length): """Invert_spectrogram. applies inverse fft. Args: spectrogram: [1+n_fft//2, t] """ return librosa.istft(spectrogram, hop_length, win_length=win_length, window="hann") def griffin_lim(spectrogram, iter_vocoder, n_fft, hop_length, win_length): """griffin_lim.""" X_best = copy.deepcopy(spectrogram) for i in range(iter_vocoder): X_t = invert_spectrogram(X_best, win_length, hop_length) est = librosa.stft(X_t, n_fft, hop_length, win_length=win_length) phase = est / np.maximum(1e-8, np.abs(est)) X_best = spectrogram * phase X_t = invert_spectrogram(X_best, win_length, hop_length) y = np.real(X_t) return y def spectrogram2wav( mag, max_db, ref_db, preemphasis, power, sr, hop_length, win_length, n_fft ): """Generate wave file from linear magnitude spectrogram. Args: mag: A numpy array of (T, 1+n_fft//2) Returns: wav: A 1-D numpy array. """ hop_length = int(hop_length * sr) win_length = int(win_length * sr) n_fft = n_fft # transpose mag = mag.T # de-noramlize mag = (np.clip(mag, 0, 1) * max_db) - max_db + ref_db # to amplitude mag = np.power(10.0, mag * 0.05) # wav reconstruction wav = griffin_lim(mag ** power, 100, n_fft, hop_length, win_length) # de-preemphasis wav = signal.lfilter([1], [1, -preemphasis], wav) # trim wav, _ = librosa.effects.trim(wav) return wav.astype(np.float32) def log_figure_mel(step, output, spec, att, length, save_dir, args): """log_figure_mel.""" # only get one sample from a batch # save wav and plot spectrogram output = output.cpu().detach().numpy()[0] out_spec = spec.cpu().detach().numpy()[0] length = np.max(length.cpu().detach().numpy()[0]) output = output[:length] out_spec = out_spec[:length] # FIX ME! Need WaveRNN to produce wav from mel-spec # wav = spectrogram2wav(output, args.max_db, args.ref_db, # args.preemphasis, args.power, args.sampling_rate, # args.frame_shift, args.frame_length, args.nfft) # wav_true = spectrogram2wav(out_spec, args.max_db, # args.ref_db, args.preemphasis, args.power, args.sampling_rate, # args.frame_shift, args.frame_length, args.nfft) # if librosa.__version__ < '0.8.0': # librosa.output.write_wav(os.path.join(save_dir, # '{}.wav'.format(step)), wav, args.sampling_rate) # librosa.output.write_wav(os.path.join(save_dir, # '{}_true.wav'.format(step)), wav_true, args.sampling_rate) # else: # # librosa > 0.8 remove librosa.output.write_wav module # sf.write(os.path.join(save_dir, '{}.wav'.format(step)), # wav, args.sampling_rate,format='wav', subtype='PCM_24') # sf.write(os.path.join(save_dir, '{}_true.wav'.format(step)), # wav, args.sampling_rate,format='wav', subtype='PCM_24') plt.subplot(1, 2, 1) specshow(output.T) plt.title("prediction") plt.subplot(1, 2, 2) specshow(out_spec.T) plt.title("ground_truth") plt.savefig(os.path.join(save_dir, "{}.png".format(step))) if att is not None: att = att.cpu().detach().numpy()[0] att = att[:, :length, :length] plt.subplot(1, 4, 1) specshow(att[0]) plt.subplot(1, 4, 2) specshow(att[1]) plt.subplot(1, 4, 3) specshow(att[2]) plt.subplot(1, 4, 4) specshow(att[3]) plt.savefig(os.path.join(save_dir, "{}_att.png".format(step))) def log_figure(step, output, spec, att, length, save_dir, args): """log_figure.""" # only get one sample from a batch # save wav and plot spectrogram output = output.cpu().detach().numpy()[0] out_spec = spec.cpu().detach().numpy()[0] length = np.max(length.cpu().detach().numpy()[0]) output = output[:length] out_spec = out_spec[:length] wav = spectrogram2wav( output, args.max_db, args.ref_db, args.preemphasis, args.power, args.sampling_rate, args.frame_shift, args.frame_length, args.nfft, ) wav_true = spectrogram2wav( out_spec, args.max_db, args.ref_db, args.preemphasis, args.power, args.sampling_rate, args.frame_shift, args.frame_length, args.nfft, ) if librosa.__version__ < "0.8.0": librosa.output.write_wav( os.path.join(save_dir, "{}.wav".format(step)), wav, args.sampling_rate, ) librosa.output.write_wav( os.path.join(save_dir, "{}_true.wav".format(step)), wav_true, args.sampling_rate, ) else: # librosa > 0.8 remove librosa.output.write_wav module sf.write( os.path.join(save_dir, "{}.wav".format(step)), wav, args.sampling_rate, format="wav", subtype="PCM_24", ) sf.write( os.path.join(save_dir, "{}_true.wav".format(step)), wav_true, args.sampling_rate, format="wav", subtype="PCM_24", ) plt.subplot(1, 2, 1) specshow(output.T) plt.title("prediction") plt.subplot(1, 2, 2) specshow(out_spec.T) plt.title("ground_truth") plt.savefig(os.path.join(save_dir, "{}.png".format(step))) if att is not None: att = att.cpu().detach().numpy()[0] att = att[:, :length, :length] plt.subplot(1, 4, 1) specshow(att[0]) plt.subplot(1, 4, 2) specshow(att[1]) plt.subplot(1, 4, 3) specshow(att[2]) plt.subplot(1, 4, 4) specshow(att[3]) plt.savefig(os.path.join(save_dir, "{}_att.png".format(step))) def log_mel(step, output_mel, spec, att, length, save_dir, args, voc_model): """log_mel.""" # only get one sample from a batch # save wav and plot spectrogram output_mel = output_mel.cpu().detach().numpy()[0] out_spec = spec.cpu().detach().numpy()[0] length = np.max(length.cpu().detach().numpy()[0]) output_mel = output_mel[:length] out_spec = out_spec[:length] wav = voc_model.generate(output_mel) wav_true = spectrogram2wav( out_spec, args.max_db, args.ref_db, args.preemphasis, args.power, args.sampling_rate, args.frame_shift, args.frame_length, args.nfft, ) if librosa.__version__ < "0.8.0": librosa.output.write_wav( os.path.join(save_dir, "{}.wav".format(step)), wav, args.sampling_rate ) librosa.output.write_wav( os.path.join(save_dir, "{}_true.wav".format(step)), wav_true, args.sampling_rate, ) else: # librosa > 0.8 remove librosa.output.write_wav module sf.write( os.path.join(save_dir, "{}.wav".format(step)), wav, args.sampling_rate, format="wav", subtype="PCM_24", ) sf.write( os.path.join(save_dir, "{}_true.wav".format(step)), wav_true, args.sampling_rate, format="wav", subtype="PCM_24", ) plt.subplot(1, 2, 1) specshow(output_mel.T) plt.title("prediction") plt.subplot(1, 2, 2) specshow(out_spec.T) plt.title("ground_truth") plt.savefig(os.path.join(save_dir, "{}.png".format(step))) if att is not None: att = att.cpu().detach().numpy()[0] att = att[:, :length, :length] plt.subplot(1, 4, 1) specshow(att[0]) plt.subplot(1, 4, 2) specshow(att[1]) plt.subplot(1, 4, 3) specshow(att[2]) plt.subplot(1, 4, 4) specshow(att[3]) plt.savefig(os.path.join(save_dir, "{}_att.png".format(step))) def Calculate_time(elapsed_time): """Calculate_time.""" elapsed_hours = int(elapsed_time / 3600) elapsed_mins = int((elapsed_time - (elapsed_hours * 3600)) / 60) elapsed_secs = int(elapsed_time - (elapsed_hours * 3600) - (elapsed_mins * 60)) return elapsed_hours, elapsed_mins, elapsed_secs def Calculate_time_path(path): """Calculate_time_path.""" num_list = os.listdir(path) total_time = 0 for number in num_list: # print(number) number_path = os.path.join(path, number) # print(number_path) wav_name_list = os.listdir(number_path) for wav_name in wav_name_list: wav_path = os.path.join(number_path, wav_name) print(wav_path) time = librosa.get_duration(filename=wav_path) print(time) total_time += time return total_time def Calculate_dataset_duration(dataset_path): """Calculate_dataset_duration.""" train_path = os.path.join(dataset_path, "train") dev_path = os.path.join(dataset_path, "dev") test_path = os.path.join(dataset_path, "test") total_time = ( Calculate_time_path(train_path) + Calculate_time_path(dev_path) + Calculate_time_path(test_path) ) hours, mins, secs = Calculate_time(total_time) print(f"Time: {hours}h {mins}m {secs}s'") if __name__ == "__main__": # path = "/data5/jiatong/SVS_system/SVS/data/ # public_dataset/kiritan_data/wav_info" path = "/data5/jiatong/SVS_system/SVS/data/public_dataset/hts_data/wav_info" Calculate_dataset_duration(path)
[ 37811, 15269, 685, 42334, 60, 685, 41, 5375, 506, 16380, 4083, 198, 198, 26656, 15385, 739, 262, 24843, 13789, 11, 10628, 362, 13, 15, 357, 1169, 366, 34156, 15341, 198, 5832, 743, 407, 779, 428, 2393, 2845, 287, 11846, 351, 262, 1378...
1.871314
15,635
# Author Toshihiko Aoki # # 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. """BertClassifier.""" from mptb import BertClassifier if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description='BERT classification.', usage='%(prog)s [options]') parser.add_argument('--config_path', help='JSON file path for defines networks.', nargs='?', type=str, default='config/bert_base.json') parser.add_argument('--train_dataset_path', help='Training Dataset file (TSV file) path for classification.', nargs='?', type=str, default=None) parser.add_argument('--eval_dataset_path', help='Evaluate Dataset file (TSV file) path for classification.', nargs='?', type=str, default=None) parser.add_argument('--pretrain_path', help='Pre-training PyTorch model path.', nargs='?', type=str, default=None) parser.add_argument('--tf_pretrain_path', help='Pre-training TensorFlow(Google) model path.', nargs='?', type=str, default=None) parser.add_argument('--model_path', help='Classifier PyTorch model path.', nargs='?', type=str, default=None) parser.add_argument('--vocab_path', help='Vocabulary file path for BERT to pre-training.', nargs='?', required=True, type=str) parser.add_argument('--sp_model_path', help='Trained SentencePiece model path.', nargs='?', type=str, default=None) parser.add_argument('--save_dir', help='Classification model saving directory path.', nargs='?', type=str, default='classifier/') parser.add_argument('--log_dir', help='Logging file path.', nargs='?', type=str, default=None) parser.add_argument('--batch_size', help='Batch size', nargs='?', type=int, default=4) parser.add_argument('--max_pos', help='The maximum sequence length for BERT (slow as big).', nargs='?', type=int, default=512) parser.add_argument('--lr', help='Learning rate', nargs='?', type=float, default=2e-5) parser.add_argument('--warmup_steps', help='Warm-up steps proportion.', nargs='?', type=float, default=0.1) parser.add_argument('--epochs', help='Epochs', nargs='?', type=int, default=10) parser.add_argument('--per_save_epochs', help= 'Saving training model timing is the number divided by the epoch number', nargs='?', type=int, default=1) parser.add_argument('--mode', help='train or eval', nargs='?', type=str, default='train') parser.add_argument('--label_num', help='labels number', nargs='?', type=int, default=-1) parser.add_argument('--balance_weight', action='store_true', help='Use automatically adjust weights') parser.add_argument('--balance_sample', action='store_true', help='Use automatically adjust samples(random)') parser.add_argument('--under_sampling', action='store_true', help='Use automatically adjust under samples') parser.add_argument('--under_sampling_cycle', action='store_true', help='Use automatically adjust under samples cycle peer') parser.add_argument('--tokenizer', nargs='?', type=str, default='google', help= 'Select from the following name groups tokenizer that uses only vocabulary files.(mecab, juman)' ) parser.add_argument('--read_head', action='store_true', help='Use not include header TSV file') parser.add_argument('--fp16', action='store_true', help='Use nVidia fp16 (require apex module)') parser.add_argument('--task', nargs='?', type=str, default='class', help='Target Task (class or choice)') parser.add_argument('--device', nargs='?', type=str, default=None, help='Target Runing device name.') parser.add_argument('--quantize', action='store_true', help='Use quantized bert (testing),') parser.add_argument('--model_name', nargs='?', type=str, default='bert', help= 'Select from the following name groups model. (bert, proj, albert)' ) parser.add_argument('--optimizer', nargs='?', type=str, default='bert', help= 'Select from the following name groups optimizer. (bert, adamw, lamb)' ) parser.add_argument('--encoder_json_path', help='GPT2 encoder JSON file path.', nargs='?', type=str) parser.add_argument('--vocab_bpe_path', help='GPT2 encoder bpe file path.', nargs='?', type=str) parser.add_argument('--sw_log_dir', help='TensorBoard lgo_dir path.', nargs='?', type=str, default='runs') args = parser.parse_args() classification( config_path=args.config_path, train_dataset_path=args.train_dataset_path, eval_dataset_path=args.eval_dataset_path, pretrain_path= args.pretrain_path, tf_pretrain_path=args.tf_pretrain_path, model_path=args.model_path, vocab_path=args.vocab_path, sp_model_path=args.sp_model_path, save_dir=args.save_dir, log_dir=args.log_dir, batch_size=args.batch_size, max_pos=args.max_pos, lr=args.lr, warmup_proportion=args.warmup_steps, epochs=args.epochs, per_save_epochs=args.per_save_epochs, mode=args.mode, label_num=args.label_num, balance_weight=args.balance_weight, balance_sample=args.balance_sample, under_sampling=args.under_sampling, under_sampling_cycle=args.under_sampling_cycle, tokenizer_name=args.tokenizer, read_head=args.read_head, fp16=args.fp16, task=args.task, device=args.device, quantize=args.quantize, model_name=args.model_name, optimizer=args.optimizer, encoder_json_path=args.encoder_json_path, vocab_bpe_path=args.vocab_bpe_path, sw_log_dir=args.sw_log_dir )
[ 2, 6434, 47922, 4449, 12125, 317, 18228, 198, 2, 198, 2, 49962, 739, 262, 24843, 13789, 11, 10628, 362, 13, 15, 357, 1169, 366, 34156, 15341, 198, 2, 345, 743, 407, 779, 428, 2393, 2845, 287, 11846, 351, 262, 13789, 13, 198, 2, 92...
2.263402
3,022
from django.urls import path from dev.views import FindMyIp,FindMyGps app_name = 'dev' urlpatterns = [ # path('', Main.as_view(), name = 'index'), path('findmyip', FindMyIp.as_view(), name = 'findmyip'), path('findmygps', FindMyGps.as_view(), name = 'findmygps'), ]
[ 6738, 42625, 14208, 13, 6371, 82, 1330, 3108, 198, 198, 6738, 1614, 13, 33571, 1330, 9938, 3666, 40, 79, 11, 16742, 3666, 38, 862, 198, 198, 1324, 62, 3672, 796, 705, 7959, 6, 198, 6371, 33279, 82, 796, 685, 198, 220, 220, 220, 13...
2.447368
114
#!/usr/bin/env python # -*- encoding: utf-8 -*- """ Topic: htmlxml Desc : """ import html if __name__ == '__main__': html_xml()
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 2, 532, 9, 12, 21004, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 37811, 198, 33221, 25, 27711, 19875, 198, 24564, 1058, 220, 198, 37811, 198, 11748, 27711, 628, 198, 361, 11593, 3672, ...
2.305085
59
__all__ = ['db_utils', 'RfamDB', 'parse_taxbrowser']
[ 834, 439, 834, 796, 37250, 9945, 62, 26791, 3256, 705, 49, 44769, 11012, 3256, 705, 29572, 62, 19290, 40259, 20520, 198 ]
2.52381
21
__copyright__ = "Copyright (c) 2020 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Optional from . import FlatRecursiveMixin, BaseExecutableDriver, DocsExtractUpdateMixin
[ 834, 22163, 4766, 834, 796, 366, 15269, 357, 66, 8, 12131, 449, 1437, 9552, 15302, 13, 1439, 2489, 10395, 526, 198, 834, 43085, 834, 796, 366, 25189, 4891, 12, 17, 13, 15, 1, 198, 198, 6738, 19720, 1330, 32233, 198, 198, 6738, 764, ...
3.365079
63
# 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. from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import argparse import collections import json import os import six import sys from tensorflow.python.lib.io import file_io SCHEMA_FILE = 'schema.json' NUMERICAL_ANALYSIS_FILE = 'stats.json' CATEGORICAL_ANALYSIS_FILE = 'vocab_%s.csv' def parse_arguments(argv): """Parse command line arguments. Args: argv: list of command line arguments, includeing programe name. Returns: An argparse Namespace object. """ parser = argparse.ArgumentParser( description='Runs Preprocessing on structured CSV data.') parser.add_argument('--input-file-pattern', type=str, required=True, help='Input CSV file names. May contain a file pattern') parser.add_argument('--output-dir', type=str, required=True, help='Google Cloud Storage which to place outputs.') parser.add_argument('--schema-file', type=str, required=True, help=('BigQuery json schema file')) args = parser.parse_args(args=argv[1:]) # Make sure the output folder exists if local folder. file_io.recursive_create_dir(args.output_dir) return args def run_numerical_categorical_analysis(args, schema_list): """Makes the numerical and categorical analysis files. Args: args: the command line args schema_list: python object of the schema json file. Raises: ValueError: if schema contains unknown column types. """ header = [column['name'] for column in schema_list] input_files = file_io.get_matching_files(args.input_file_pattern) # Check the schema is valid for col_schema in schema_list: col_type = col_schema['type'].lower() if col_type != 'string' and col_type != 'integer' and col_type != 'float': raise ValueError('Schema contains an unsupported type %s.' % col_type) # initialize the results numerical_results = collections.defaultdict(_init_numerical_results) categorical_results = collections.defaultdict(set) # for each file, update the numerical stats from that file, and update the set # of unique labels. for input_file in input_files: with file_io.FileIO(input_file, 'r') as f: for line in f: parsed_line = dict(zip(header, line.strip().split(','))) for col_schema in schema_list: col_name = col_schema['name'] col_type = col_schema['type'] if col_type.lower() == 'string': categorical_results[col_name].update([parsed_line[col_name]]) else: # numerical column. # if empty, skip if not parsed_line[col_name].strip(): continue numerical_results[col_name]['min'] = ( min(numerical_results[col_name]['min'], float(parsed_line[col_name]))) numerical_results[col_name]['max'] = ( max(numerical_results[col_name]['max'], float(parsed_line[col_name]))) numerical_results[col_name]['count'] += 1 numerical_results[col_name]['sum'] += float(parsed_line[col_name]) # Update numerical_results to just have min/min/mean for col_schema in schema_list: if col_schema['type'].lower() != 'string': col_name = col_schema['name'] mean = numerical_results[col_name]['sum'] / numerical_results[col_name]['count'] del numerical_results[col_name]['sum'] del numerical_results[col_name]['count'] numerical_results[col_name]['mean'] = mean # Write the numerical_results to a json file. file_io.write_string_to_file( os.path.join(args.output_dir, NUMERICAL_ANALYSIS_FILE), json.dumps(numerical_results, indent=2, separators=(',', ': '))) # Write the vocab files. Each label is on its own line. for name, unique_labels in six.iteritems(categorical_results): labels = '\n'.join(list(unique_labels)) file_io.write_string_to_file( os.path.join(args.output_dir, CATEGORICAL_ANALYSIS_FILE % name), labels) def run_analysis(args): """Builds an analysis files for training.""" # Read the schema and input feature types schema_list = json.loads( file_io.read_file_to_string(args.schema_file)) run_numerical_categorical_analysis(args, schema_list) # Also save a copy of the schema in the output folder. file_io.copy(args.schema_file, os.path.join(args.output_dir, SCHEMA_FILE), overwrite=True) if __name__ == '__main__': main()
[ 2, 15069, 2177, 3012, 3457, 13, 1439, 6923, 33876, 13, 198, 2, 198, 2, 49962, 739, 262, 24843, 13789, 11, 10628, 362, 13, 15, 357, 1169, 366, 34156, 15341, 198, 2, 345, 743, 407, 779, 428, 2393, 2845, 287, 11846, 351, 262, 13789, ...
2.586022
2,046
import datetime import json from ariadne import MutationType, QueryType, convert_kwargs_to_snake_case from ariadne_token_auth.decorators import login_required from django.contrib.auth import get_user_model from habits import models as habit_models from utils.general import get_user from utils.handlers.errors import ErrorContainer CUSTOM_USER_MODEL = get_user_model() query = QueryType() mutation = MutationType()
[ 11748, 4818, 8079, 198, 11748, 33918, 198, 198, 6738, 257, 21244, 710, 1330, 337, 7094, 6030, 11, 43301, 6030, 11, 10385, 62, 46265, 22046, 62, 1462, 62, 16184, 539, 62, 7442, 198, 6738, 257, 21244, 710, 62, 30001, 62, 18439, 13, 1250...
3.269231
130
import csv, pylab as pl, re DB = dict(); BD = dict(); whales_ = []; classes = []; line_num = 0; with open('data/train.csv', 'rb') as train_class_data: data = csv.reader(train_class_data, delimiter=','); for line in data: if (line_num == 0): line_num += 1; continue; keys = DB.keys(); syek = BD.keys(); pic_name = line[0]; class_name = line[1]; whales_.append(int(re.sub('w_','',re.sub('.jpg','',pic_name)))); if (class_name not in keys): DB[class_name] = [pic_name]; classes.append(int(re.sub('whale_','',class_name))); else: DB[class_name].append(pic_name); BD[pic_name] = class_name; keys = DB.keys(); N = len(keys); frequency_table = [0 for i in xrange(N)]; for i in xrange(N): frequency_table[i] = len(DB[keys[i]]); pl.plot(frequency_table); M = len(whales_); match_table = [[0 for j in xrange(N+1)] for i in xrange(M+1)]; for j in xrange(N): match_table[0][j+1] = classes[j]; for i in xrange(M): match_table[i+1][0] = whales_[i]; for i in xrange(N): for j in xrange(M): strWhale = 'w_'+str(whales_[j])+'.jpg'; num_zero = 0; if (classes[i] < 10): num_zero += 4; elif (classes[i] < 100): num_zero += 3; elif (classes[i] < 1000): num_zero += 2; elif (classes[i] < 10000): num_zero += 1; zeros = num_zero*'0'; strClass = 'whale_'+zeros+str(classes[i]); if (strWhale in DB[strClass]): match_table[j+1][i+1] = 1; match_table = pl.array(match_table); pl.savetxt('data/match_table.csv', match_table, delimiter=','); target_matrix = pl.array([[0 for j in xrange(M)] for j in xrange(M)]); i = 0; for pic_name_i in whales_: j = 0; for pic_name_j in whales_: class_of_i = BD['w_'+str(pic_name_i)+'.jpg']; class_of_j = BD['w_'+str(pic_name_j)+'.jpg']; if (class_of_i == class_of_j): target_matrix[i,j] = 1; j += 1; target_matrix[i,i] = 1; i += 1; new_train_numerical = pl.array([[0 for it1 in xrange(2)] for it2 in xrange(M)]); for i in xrange(M): whale = whales_[i]; new_train_numerical[i,0] = whale; class_ = class_of_i = BD['w_'+str(whale)+'.jpg']; new_train_numerical[i,1] = int(re.sub('whale_','',class_)); pl.savetxt('data/target_matrix.csv', target_matrix, delimiter=','); pl.savetxt('data/train_numer.csv', new_train_numerical, delimiter=',');
[ 11748, 269, 21370, 11, 279, 2645, 397, 355, 458, 11, 302, 198, 198, 11012, 796, 8633, 9783, 198, 14529, 796, 8633, 9783, 198, 198, 1929, 2040, 62, 796, 25787, 198, 37724, 796, 25787, 198, 1370, 62, 22510, 796, 657, 26, 198, 4480, 12...
1.975136
1,287
from typing import Union from datetime import datetime import os import tempfile from contextlib import contextmanager import logging from collections import Counter import yaml logger = logging.getLogger(__name__) if __name__ == "__main__": fspath = "./config/download_log.yaml" import loggers from yammler import Yammler loggers.standard_config() logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) with DownloadLog.context(fspath) as f: # print(f) # f.known_files = "test" # print(f.known_files) f.add("test1") print(f) y = Yammler("./config/operators.yaml") s2 = [{x.pop("operator"): x} for x in s] from stringprocessor import StringProcessor as sp for s in s2: for k, v in s.items(): x = s.pop(k) x["alias"] = sp.normalize(x.pop("alias"), lower=True) x["method"] = sp.normalize(x.pop("method"), lower=True) s[sp.normalize(k, lower=True)] = x for x in s2: for key, value in x.items(): try: value["created"] = value["created"].isoformat() value["updated"] = value["updated"].isoformat() except: pass finally: y[key] = value f = DownloadLog(fspath) # f.known_files # f.add("test1") # f.dump() # f.remove("test1")
[ 6738, 19720, 1330, 4479, 198, 6738, 4818, 8079, 1330, 4818, 8079, 198, 11748, 28686, 198, 11748, 20218, 7753, 198, 6738, 4732, 8019, 1330, 4732, 37153, 198, 11748, 18931, 198, 6738, 17268, 1330, 15034, 198, 198, 11748, 331, 43695, 198, 19...
2.168196
654
# Generated by Django 4.0.2 on 2022-03-25 11:59 from django.db import migrations, models
[ 2, 2980, 515, 416, 37770, 604, 13, 15, 13, 17, 319, 33160, 12, 3070, 12, 1495, 1367, 25, 3270, 198, 198, 6738, 42625, 14208, 13, 9945, 1330, 15720, 602, 11, 4981, 628 ]
2.84375
32
# -*- coding: utf-8 -*- import os # Module with fdf-aware dictionary from tkdict import FDFDict from aiida.orm.calculation.job import JobCalculation from aiida.common.exceptions import InputValidationError from aiida.common.datastructures import CalcInfo from aiida.common.utils import classproperty from aiida.common.datastructures import CodeInfo from aiida.orm.data.parameter import ParameterData from aiida.orm.data.remote import RemoteData __copyright__ = u"Copyright (c), 2015, ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE (Theory and Simulation of Materials (THEOS) and National Centre for Computational Design and Discovery of Novel Materials (NCCR MARVEL)), Switzerland and ROBERT BOSCH LLC, USA. All rights reserved." __license__ = "MIT license, see LICENSE.txt file" __version__ = "0.9.10" __contributors__ = "Victor M. Garcia-Suarez, Alberto Garcia" def get_input_data_text(key,val, mapping=None): """ Given a key and a value, return a string (possibly multiline for arrays) with the text to be added to the input file. :param key: the flag name :param val: the flag value. If it is an array, a line for each element is produced, with variable indexing starting from 1. Each value is formatted using the conv_to_fortran function. :param mapping: Optional parameter, must be provided if val is a dictionary. It maps each key of the 'val' dictionary to the corresponding list index. For instance, if ``key='magn'``, ``val = {'Fe': 0.1, 'O': 0.2}`` and ``mapping = {'Fe': 2, 'O': 1}``, this function will return the two lines ``magn(1) = 0.2`` and ``magn(2) = 0.1``. This parameter is ignored if 'val' is not a dictionary. """ from aiida.common.utils import conv_to_fortran # I check first the dictionary, because it would also match # hasattr(__iter__) if isinstance(val, dict): if mapping is None: raise ValueError("If 'val' is a dictionary, you must provide also " "the 'mapping' parameter") list_of_strings = [] for elemk, itemval in val.iteritems(): try: idx = mapping[elemk] except KeyError: raise ValueError("Unable to find the key '{}' in the mapping " "dictionary".format(elemk)) list_of_strings.append((idx," {0}({2}) = {1}\n".format( key, conv_to_fortran(itemval), idx))) # I first have to resort, then to remove the index from the first # column, finally to join the strings list_of_strings = zip(*sorted(list_of_strings))[1] return "".join(list_of_strings) elif hasattr(val,'__iter__'): # a list/array/tuple of values list_of_strings = [ "{0}({2}) {1}\n".format(key, conv_to_fortran(itemval), idx+1) for idx, itemval in enumerate(val)] return "".join(list_of_strings) else: # single value if key[:6] == '%block': bname = key.split()[1] b1 = "{0} {1}".format(key, my_conv_to_fortran(val)) return b1 + "\n%endblock " + bname + "\n" else: return "{0} {1}\n".format(key, my_conv_to_fortran(val)) def my_conv_to_fortran(val): """ Special version to avoid surrounding strings with extra ' '. Otherwise the fdf tokenizer will not split values and units, for example. :param val: the value to be read and converted to a Fortran-friendly string. """ # Note that bool should come before integer, because a boolean matches also # isinstance(...,int) if (isinstance(val, bool)): if val: val_str = '.true.' else: val_str = '.false.' elif (isinstance(val, (int, long))): val_str = "{:d}".format(val) elif (isinstance(val, float)): val_str = ("{:18.10e}".format(val)).replace('e', 'd') elif (isinstance(val, basestring)): val_str = "{!s}".format(val) else: raise ValueError("Invalid value passed, accepts only bools, ints, " "floats and strings") return val_str
[ 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 11748, 28686, 198, 198, 2, 19937, 351, 277, 7568, 12, 9685, 22155, 198, 6738, 256, 74, 11600, 1330, 376, 8068, 35, 713, 198, 198, 6738, 257, 72, 3755, 13, 579, 13, 9...
2.341744
1,823
import uuid import os from datetime import datetime from django.db import transaction from api.management.data_script import OperationalDataScript from api.models.CompliancePeriod import CompliancePeriod from api.models.Organization import Organization from api.models.OrganizationActionsType import OrganizationActionsType from api.models.OrganizationBalance import OrganizationBalance from api.models.OrganizationStatus import OrganizationStatus from api.models.OrganizationType import OrganizationType from api.models.Role import Role from api.models.User import User from api.models.UserRole import UserRole script_class = LoadFTData
[ 11748, 334, 27112, 198, 11748, 28686, 198, 198, 6738, 4818, 8079, 1330, 4818, 8079, 198, 198, 6738, 42625, 14208, 13, 9945, 1330, 8611, 198, 198, 6738, 40391, 13, 27604, 13, 7890, 62, 12048, 1330, 6564, 864, 6601, 7391, 198, 6738, 40391...
4.202614
153
from challenge.agoda_cancellation_estimator import AgodaCancellationEstimator from IMLearn.utils import split_train_test from IMLearn.base import BaseEstimator import numpy as np import pandas as pd def load_data(filename: str): """ Load Agoda booking cancellation dataset Parameters ---------- filename: str Path to house prices dataset Returns ------- Design matrix and response vector in either of the following formats: 1) Single dataframe with last column representing the response 2) Tuple of pandas.DataFrame and Series 3) Tuple of ndarray of shape (n_samples, n_features) and ndarray of shape (n_samples,) """ full_data = pd.read_csv(filename).drop_duplicates() full_data["cancellation_datetime"] = full_data["cancellation_datetime"].fillna(0) full_data = full_data.dropna() # full_data = full_data.fillna("Unknown") full_data[["booking_datetime", "checkin_date", "checkout_date", "hotel_live_date", "cancellation_datetime"]] = \ full_data[ ["booking_datetime", "checkin_date", "checkout_date", "hotel_live_date", "cancellation_datetime"]].apply( pd.to_datetime) full_data["cancellation_datetime"] = full_data["cancellation_datetime"].apply(lambda x: x.value // 10**9) full_data["booking_date"], full_data["booking_time"] = full_data["booking_datetime"].dt.date, full_data[ "booking_datetime"].dt.time features_to_dummify = ["hotel_id", "hotel_country_code", "accommadation_type_name", "charge_option", "customer_nationality", "guest_nationality_country_name", "origin_country_code", "language", "original_payment_method", "original_payment_currency", "hotel_area_code", "hotel_city_code"] for feature in features_to_dummify: feature_dummies = pd.get_dummies(full_data[feature]).add_prefix(f"{feature}") full_data = full_data.join(feature_dummies) full_data = full_data.drop(["h_booking_id", "h_customer_id", "booking_datetime"] + features_to_dummify, axis=1) labels = full_data.pop("cancellation_datetime") return full_data, labels def evaluate_and_export(estimator: BaseEstimator, X: np.ndarray, filename: str): """ Export to specified file the prediction results of given estimator on given test set. File saved is in csv format with a single column named 'predicted_values' and n_samples rows containing predicted values. Parameters ---------- estimator: BaseEstimator or any object implementing predict() method as in BaseEstimator (for example sklearn) Fitted estimator to use for prediction X: ndarray of shape (n_samples, n_features) Test design matrix to predict its responses filename: path to store file at """ pd.DataFrame(estimator.predict(X), columns=["predicted_values"]).to_csv(filename, index=False) if __name__ == '__main__': np.random.seed(0) # Load data df, cancellation_labels = load_data("agoda_cancellation_train.csv") train_X, train_y, test_X, test_y = split_train_test(df, cancellation_labels) # Fit model over data from time import time start = time() estimator = AgodaCancellationEstimator().fit(train_X, train_y) pred = estimator.predict(test_X) print(time() - start) # Store model predictions over test set evaluate_and_export(estimator, test_X, "id1_id2_id3.csv")
[ 6738, 4427, 13, 363, 11329, 62, 66, 590, 297, 341, 62, 395, 320, 1352, 1330, 2449, 11329, 34, 590, 297, 341, 22362, 320, 1352, 198, 6738, 314, 5805, 451, 77, 13, 26791, 1330, 6626, 62, 27432, 62, 9288, 198, 6738, 314, 5805, 451, 7...
2.638168
1,310
#!/usr/bin/env python3 import scripthelper scripthelper.add_argument("-n", "--name", help="Name to greet") logger, args = scripthelper.bootstrap_args() if args.name: logger.debug("Name was provided") logger.info(f"Hello {args.name}") else: logger.warning("Name was not provided")
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 11748, 4226, 2978, 525, 198, 198, 12048, 2978, 525, 13, 2860, 62, 49140, 7203, 12, 77, 1600, 366, 438, 3672, 1600, 1037, 2625, 5376, 284, 12589, 4943, 198, 6404, 1362, 11, 26498, ...
2.8
105
#backslash and new line ignored print("one\ two\ three")
[ 2, 1891, 6649, 1077, 290, 649, 1627, 9514, 198, 4798, 7203, 505, 59, 198, 220, 220, 220, 220, 220, 220, 734, 59, 198, 220, 220, 220, 220, 220, 220, 1115, 4943, 198 ]
2.21875
32
#!/usr/bin/env python r""" Front-facing script to find drifting, narrowband events in a set of generalized cadences of ON-OFF radio SETI observations. The main function contained in this file is :func:`find_event_pipeline` calls find_events from find_events.py to read a list of turboSETI .dat files. It then finds events within this group of files. """ #required packages and programs import os from operator import attrgetter import logging logger_name = 'find_event_pipeline' logger = logging.getLogger(logger_name) logger.setLevel(logging.INFO) import pandas as pd import numpy as np from blimpy import Waterfall from blimpy.utils import change_the_ext from turbo_seti.find_event.find_event import find_events RTOL_DIFF = 0.01 # 1% def get_file_header(filepath_h5): r''' Extract and return the target's source name from the DAT file path. Parameters ---------- dat_path : str Full or relative path name of the DAT file Returns ------- header : Waterfall header object ''' wf = Waterfall(filepath_h5, load_data=False) return wf.container.header def close_enough(x, y): r"""Make sure that x and y are close enough to be considered roughly equal.""" if np.isclose(float(x), float(y), rtol=RTOL_DIFF): return True return False def find_event_pipeline(dat_file_list_str,h5_file_list_str=None, SNR_cut=10, check_zero_drift=False, filter_threshold=3, on_off_first='ON', number_in_cadence=6, on_source_complex_cadence=False, saving=True, csv_name=None, user_validation=False, sortby_tstart=True): """ Find event pipeline. Parameters ---------- dat_file_list_str : str The string name of a plaintext file ending in .lst that contains the filenames of .dat files, each on a new line, that were created with seti_event.py. The .lst should contain a set of cadences (ON observations alternating with OFF observations). The cadence can be of any length, given that the ON source is every other file. This includes Breakthrough Listen standard ABACAD as well as OFF first cadences like BACADA. Minimum cadence length is 2, maximum cadence length is unspecified (currently tested up to 6). Example: ABACAD|ABACAD|ABACAD h5_file_list_str : str | None The string name of a plaintext file ending in .lst that contains the filenames of .h5 files, each on a new line, that were created with seti_event.py. The .lst should contain a set of cadences (ON observations alternating with OFF observations). The cadence can be of any length, given that the ON source is every other file. This includes Breakthrough Listen standard ABACAD as well as OFF first cadences like BACADA. Minimum cadence length is 2, maximum cadence length is unspecified (currently tested up to 6). SNR_cut : int The threshold SNR below which hits in the ON source will be disregarded. For the least strict thresholding, set this parameter equal to the minimum-searched SNR that you used to create the .dat files from seti_event.py. Recommendation (and default) is 10. check_zero_drift : bool A True/False flag that tells the program whether to include hits that have a drift rate of 0 Hz/s. Earth- based RFI tends to have no drift rate, while signals from the sky are expected to have non-zero drift rates. filter_threshold : int Specification for how strict the hit filtering will be. There are 3 different levels of filtering, specified by the integers 1, 2, and 3. Filter_threshold = 1 returns hits above an SNR cut, taking into account the check_zero_drift parameter, but without an ON-OFF check. Filter_threshold = 2 returns hits that passed level 1 AND that are in at least one ON but no OFFs. Filter_threshold = 3 returns events that passed level 2 AND that are present in *ALL* ONs. on_off_first : str {'ON', 'OFF'} Tells the code whether the .dat sequence starts with the ON or the OFF observation. Valid entries are 'ON' and 'OFF' only. Default is 'ON'. number_in_cadence : int The number of files in a single ON-OFF cadence. Default is 6 for ABACAD. on_source_complex_cadence : bool If using a complex cadence (i.e. ons and offs not alternating), this variable should be the string target name used in the .dat filenames. The code will then determine which files in your dat_file_list_str cadence are ons and which are offs. saving : bool A True/False flag that tells the program whether to save the output array as a .csv. user_validation : bool A True/False flag that, when set to True, asks if the user wishes to continue with their input parameters (and requires a 'y' or 'n' typed as confirmation) before beginning to run the program. Recommended when first learning the program, not recommended for automated scripts. sortby_tstart : bool If True, the input file list is sorted by header.tstart. Returns ------- Either: * a Pandas dataframe with all the events that were found. * None, if no events were found. Notes ----- The HDF5 file is ASSUMED(!!) to have the same name as .dat files. Examples -------- >>> import find_event_pipeline; >>> find_event_pipeline.find_event_pipeline(dat_file_list_str, ... SNR_cut=10, ... check_zero_drift=False, ... filter_threshold=3, ... on_off_first='ON', ... number_in_cadence=6, ... on_source_complex_cadence=False, ... saving=True, ... user_validation=False) """ print() print("************ BEGINNING FIND_EVENT PIPELINE **************") print() if on_source_complex_cadence: print("Assuming a complex cadence for the following on source: {}" .format(on_source_complex_cadence)) else: # not on_source_complex_cadence: print("Assuming the first observation is an " + on_off_first) complex_cadence = on_source_complex_cadence # Get a list of the DAT files. # Get source names and build path_record list. source_name_list = [] path_record = [] # Get a list of the DAT/h5 files. n_files, dat_file_list = list_of_files(dat_file_list_str) if h5_file_list_str is None: h5_file_list = dat_file_list for hf in h5_file_list: header = get_file_header(change_the_ext(hf, 'dat', 'h5')) source_name = header["source_name"] tstart = header["tstart"] path_record.append(PathRecord(hf, tstart, source_name, header["fch1"], header["foff"], header["nchans"])) source_name_list.append(source_name) else: hn_files, h5_file_list = list_of_files(h5_file_list_str) for hf in h5_file_list: header = get_file_header(hf) for dat in dat_file_list: # O(n^2) TODO: create tests in pytest if os.path.basename(dat).replace('.dat','.h5')==os.path.basename(hf): source_name = header["source_name"] tstart = header["tstart"] path_record.append(PathRecord(dat, tstart, source_name, header["fch1"], header["foff"], header["nchans"])) source_name_list.append(source_name) # If sorting by header.tstart, then rewrite the dat_file_list in header.tstart order. if sortby_tstart: path_record = sorted(path_record, key=attrgetter('tstart')) dat_file_list = [] for obj in path_record: dat_file_list.append(obj.path_dat) # Set up the frequency range matcher record. # If a complex cadence, the source name is used to select the matcher; # Otherwise, just use the first record. if on_source_complex_cadence: flag_terminate = True for obj in path_record: # Look for 1st occurence of source_name. if obj.source_name == on_source_complex_cadence: matcher = obj flag_terminate = False break if flag_terminate: logger.error("find_event_pipeline: Source '{}' is not in this complex cadence!" .format(on_source_complex_cadence)) for obj in path_record: logger.info("find_event_pipeline: file={}, tstart={}, source_name={}, fch1={}, foff={}, nchans={}" .format(os.path.basename(obj.path_dat), obj.tstart, obj.source_name, obj.fch1, obj.foff, obj.nchans)) return None else: matcher = path_record[0] # Display path_record rows. flag_terminate = False for obj in path_record: logger.info("find_event_pipeline: file={}, tstart={}, source_name={}, fch1={}, foff={}, nchans={}" .format(os.path.basename(obj.path_dat), obj.tstart, obj.source_name, obj.fch1, obj.foff, obj.nchans)) if on_source_complex_cadence: # Complex cadence? # If not a part of the complex cadence, then skip it. if on_source_complex_cadence != obj.source_name: continue # Part of the cadence, complex or not. # Make sure that the frequency range makes sense. if not close_enough(obj.fch1, matcher.fch1) \ or not close_enough(obj.foff, matcher.foff) \ or obj.nchans != matcher.nchans: logger.error("find_event_pipeline: Inconsistent frequency range! This does not look like a cadence of related files.") flag_terminate = True if flag_terminate: return None # If this is a complex cadence, # * construct a complex_cadence list of 1s and 0s. # * compute count_cadence = number of matches on on_source_complex_cadence. if on_source_complex_cadence: complex_cadence = [] count_cadence = 0 for i in range(0, len(source_name_list)): source = source_name_list[i] if source == on_source_complex_cadence: complex_cadence.append(1) count_cadence += 1 else: complex_cadence.append(0) if count_cadence > 0: print("The derived complex cadence is: " + str(complex_cadence)) else: print("\n*** find_event_pipeline [complex cadence]: Sorry, no potential candidates with your given on_source_complex_cadence={} :(" .format(on_source_complex_cadence)) return None num_of_sets = int(n_files / number_in_cadence) print("There are " + str(len(dat_file_list)) + " total files in the filelist " + dat_file_list_str) print("therefore, looking for events in " + str(num_of_sets) + " on-off set(s)") print("with a minimum SNR of " + str(SNR_cut)) if filter_threshold == 1: print("Present in an ON source only, above SNR_cut") if filter_threshold == 2: print("Present in at least one ON source with RFI rejection from the OFF sources") if filter_threshold == 3: print("Present in all ON sources with RFI rejection from the OFF sources") if not check_zero_drift: print("not including signals with zero drift") else: print("including signals with zero drift") if not saving: print("not saving the output files") else: print("saving the output files") if user_validation: question = "Do you wish to proceed with these settings?" while "the answer is invalid": reply = str(input(question+' (y/n): ')).lower().strip() if reply == '': return None if reply[0] == 'y': break if reply[0] == 'n': return None #Looping over number_in_cadence chunks. candidate_list = [] for ii in range(num_of_sets): sublist_low = number_in_cadence * ii sublist_high = sublist_low + number_in_cadence file_sublist = dat_file_list[sublist_low : sublist_high] if not complex_cadence: if on_off_first == 'ON': filename = os.path.basename(file_sublist[0]) else: # on_off_first == 'OFF' filename = os.path.basename(file_sublist[1]) else: # complex_cadence filename = os.path.basename(file_sublist[complex_cadence.index(1)]) print() print("*** First DAT file in set: " + filename + " ***") print() cand = find_events(file_sublist, SNR_cut=SNR_cut, check_zero_drift=check_zero_drift, filter_threshold=filter_threshold, on_off_first=on_off_first, complex_cadence=complex_cadence) cand_len = 1 if cand is None: cand_len = 0 if cand_len != 0: candidate_list.append(cand) if len(candidate_list) > 0: find_event_output_dataframe = pd.concat(candidate_list) else: print("\n*** find_event_pipeline: Sorry, no potential candidates with your given parameters :(") return None print("*** find_event_output_dataframe is complete ***") if saving: if csv_name is None: prefix = os.path.dirname(dat_file_list[0]) + '/' + source_name_list[0] if check_zero_drift: filestring = prefix + '_f' + str(filter_threshold) + '_snr' \ + str(SNR_cut) + '_zero' + '.csv' else: filestring = prefix + '_f' + str(filter_threshold) + '_snr' \ + str(SNR_cut) + '.csv' else: filestring = csv_name if not isinstance(find_event_output_dataframe, list): find_event_output_dataframe.to_csv(filestring) print("find_event_pipeline: Saved CSV file to {}".format(filestring)) else: print("\n*** find_event_pipeline: Sorry, no events to save :(") return None return find_event_output_dataframe
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 198, 81, 37811, 198, 25886, 12, 29532, 4226, 284, 1064, 38193, 11, 7135, 3903, 2995, 287, 257, 900, 286, 38284, 198, 66, 324, 3007, 286, 6177, 12, 27977, 5243, 25823, 40, 13050, 13, ...
2.243198
6,616
import numpy as np import matplotlib.pyplot as plt #Dahlquist test #sol1ex = lambda t: np.exp(-t) #sol2ex = lambda t: np.exp(-2*t) #oscillator 1 sol1ex = lambda t: np.cos(t**2/2) sol2ex = lambda t: np.sin(t**2/2) #oscillator 2 #sol1ex = lambda t: np.exp(np.sin(t**2)) #sol2ex = lambda t: np.exp(np.cos(t**2)) name = 'Osc1' t = np.fromfile('../out/%s_snap_t' % name) nsnap = len(t) sol1 = np.zeros((nsnap,)) sol2 = sol1.copy() for i in range(nsnap): s = np.fromfile('../out/%s_snap_%d' % (name,i)) sol1[i] = s[0] sol2[i] = s[1] fig, axs = plt.subplots(2, 3, figsize=(10,5)) axs = [item for sublist in axs for item in sublist] tdense = np.linspace(min(t), max(t), 2500) axs[0].plot(tdense, sol1ex(tdense), 'k', linewidth=0.5, label='$y_1$ exact') axs[0].plot(t, sol1, 'C0.', label='$y_1$ numerical') axs[0].set_title('Solutions') axs[0].set_ylabel('$y_1$') axs[0].legend() axs[3].plot(tdense, sol2ex(tdense), 'k', linewidth=0.5, label='$y_2$ exact') axs[3].plot(t, sol2, 'C1.', label='$y_2$ numerical') axs[3].set_ylabel('$y_2$') axs[3].legend() axs[1].semilogy(t, np.abs(sol1 - sol1ex(t)), 'C0.', label='$y_1$ abs err') axs[4].semilogy(t, np.abs(sol2 - sol2ex(t)), 'C1.', label='$y_2$ abs err') axs[1].set_title('Absolute Error') axs[2].semilogy(t, np.abs((sol1 - sol1ex(t))/sol1ex(t)), 'C0.', label='$y_1$ rel err') axs[5].semilogy(t, np.abs((sol2 - sol2ex(t))/sol1ex(t)), 'C1.', label='$y_2$ rel err') axs[2].set_title('Relative Error') axs[3].set_xlabel('t') axs[4].set_xlabel('t') axs[5].set_xlabel('t') plt.tight_layout() plt.show()
[ 11748, 299, 32152, 355, 45941, 198, 11748, 2603, 29487, 8019, 13, 9078, 29487, 355, 458, 83, 198, 198, 2, 35, 15668, 30062, 1332, 198, 2, 34453, 16, 1069, 796, 37456, 256, 25, 45941, 13, 11201, 32590, 83, 8, 198, 2, 34453, 17, 1069,...
1.977157
788
"""MNE visual_92_categories dataset.""" from .kiloword import data_path, get_version
[ 37811, 44, 12161, 5874, 62, 5892, 62, 66, 26129, 27039, 526, 15931, 198, 198, 6738, 764, 34553, 322, 585, 1330, 1366, 62, 6978, 11, 651, 62, 9641, 198 ]
3.071429
28
# Dmitry Kisler 2020-present # www.dkisler.com from typing import List, Dict from sklearn.metrics import f1_score, accuracy_score def model_performance(y_true: List[List[str]], y_pred: List[List[str]]) -> Dict[str, float]: """Accuracy calculation function Args: y_true: List of true labels of the tokenized sentese. y_pred: List of predicted labels of the tokenized sentese. Returns: Dict of metrics: { "accuracy": float, "f1_micro": float, "f1_macro": float, "f1_weighted": float, } Raises: ValueError: Exception occurred when input lists' length don't match. """ if len(y_true) == 0: return None if len(y_true) != len(y_pred): raise ValueError("Lengths of input lists don't match.") def _list_flattener(inpt: List[List[str]]) -> List[str]: """Flattener for list of lists into a single list.""" output = [] for i in inpt: output.extend(i) return output y_true = _list_flattener(y_true) y_pred = _list_flattener(y_pred) if len(y_true) != len(y_pred): raise ValueError("Numper of tokens don't match between y_true and y_pred.") try: metrics = { "accuracy": accuracy_score(y_true, y_pred), "f1_micro": f1_score(y_true, y_pred, average='micro'), "f1_macro": f1_score(y_true, y_pred, average='macro'), "f1_weighted": f1_score(y_true, y_pred, average='weighted'), } except Exception as ex: raise Exception(f"Metrics calculation error: {ex}") return metrics
[ 2, 45181, 30519, 1754, 220, 12131, 12, 25579, 198, 2, 7324, 13, 34388, 271, 1754, 13, 785, 198, 198, 6738, 19720, 1330, 7343, 11, 360, 713, 198, 6738, 1341, 35720, 13, 4164, 10466, 1330, 277, 16, 62, 26675, 11, 9922, 62, 26675, 628,...
2.162821
780
#!/usr/bin/env python3 """Interpret an L-String as a set of 3D Turtle commands and record the turtle's path. Multiple lines of input will be treated as a continuation of a single L-String. Default commandset: F,G - Step forward while drawing f,g - Step forward without drawing -,+ - Yaw around the normal axis v,^ - Pitch around the transverse axis <,> - Roll around the longitudinal axis | - Flip orientation 180 degrees d,D - Turn drawing on, off [,] - Push, pop position and orientation onto a stack """ import argparse import logging import pathlib import sys root = pathlib.Path(__file__).resolve().parent.parent sys.path.insert(0, str(root)) from generative.lsystem.interpreter import LSystemInterpeter from generative.wkio import serialize_geometries LOG_LEVELS = { "CRITICAL": logging.CRITICAL, "ERROR": logging.ERROR, "WARNING": logging.WARNING, "INFO": logging.INFO, "DEBUG": logging.DEBUG, } DEFAULT_LEVEL = "WARNING" if __name__ == "__main__": args = parse_args() logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=LOG_LEVELS.get(args.log_level), stream=sys.stderr, ) logger = logging.getLogger(name=__file__) main(args)
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 37811, 9492, 5310, 281, 406, 12, 10100, 355, 257, 900, 286, 513, 35, 33137, 9729, 290, 1700, 262, 28699, 338, 3108, 13, 198, 198, 31217, 3951, 286, 5128, 481, 307, 5716, 355, 257...
2.760776
464
""" @author: yuboya """ ### pins position to be sent to robot ## from TransformationCalculation: import numpy as np import math
[ 201, 198, 37811, 201, 198, 201, 198, 31, 9800, 25, 331, 549, 23790, 201, 198, 37811, 201, 198, 201, 198, 21017, 20567, 2292, 284, 307, 1908, 284, 9379, 201, 198, 201, 198, 2235, 422, 49127, 9771, 14902, 25, 201, 198, 201, 198, 201, ...
2.559322
59
import requests import json from json import JSONDecodeError base_uri = "https://api.github.com/" licenses = ['afl-3.0', 'apache-2.0', 'artistic-2.0', 'bsl-1.0', 'bsd-2-clause', 'license bsd-3-clause', 'bsd-3-clause-clear', 'cc', 'cc0-1.0', 'cc-by-4.0', 'cc-by-sa-4.0', 'wtfpl', 'ecl-2.0', 'epl-1.0', 'epl-2.0', 'eupl-1.1', 'agpl-3.0', 'gpl', 'gpl-2.0', 'gpl-3.0', 'lgpl', 'lgpl-2.1', 'lgpl-3.0', 'isc', 'lppl-1.3c', 'ms-pl', 'mit', 'mpl-2.0', 'osl-3.0', 'postgresql', 'ofl-1.1', 'ncsa', 'unlicense', 'zlib']
[ 11748, 7007, 198, 11748, 33918, 198, 6738, 33918, 1330, 19449, 10707, 1098, 12331, 198, 198, 8692, 62, 9900, 796, 366, 5450, 1378, 15042, 13, 12567, 13, 785, 30487, 198, 198, 677, 4541, 796, 37250, 1878, 75, 12, 18, 13, 15, 3256, 705,...
1.836299
281
import os import tempfile import transaction from onegov.core import log from onegov.core.utils import safe_move
[ 11748, 28686, 198, 11748, 20218, 7753, 198, 11748, 8611, 198, 198, 6738, 530, 9567, 13, 7295, 1330, 2604, 198, 6738, 530, 9567, 13, 7295, 13, 26791, 1330, 3338, 62, 21084, 628, 198 ]
3.625
32
from django.conf import settings from django.db import models from django.dispatch import receiver from django.urls import Resolver404, resolve from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ from cms import operations from cms.models import CMSPlugin, Placeholder from cms.models.fields import PlaceholderField from cms.signals import pre_placeholder_operation from cms.utils.plugins import get_bound_plugins
[ 6738, 42625, 14208, 13, 10414, 1330, 6460, 198, 6738, 42625, 14208, 13, 9945, 1330, 4981, 198, 6738, 42625, 14208, 13, 6381, 17147, 1330, 9733, 198, 6738, 42625, 14208, 13, 6371, 82, 1330, 1874, 14375, 26429, 11, 10568, 198, 6738, 42625, ...
3.740157
127
#!/usr/bin/env python #coding=utf-8 import json from lib.sqs import zhihufav_sqs from lib.tasks import add_note if __name__=="__main__": for i in range(5): get_sqs_queue()
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 2, 66, 7656, 28, 40477, 12, 23, 198, 198, 11748, 33918, 198, 6738, 9195, 13, 31166, 82, 1330, 1976, 71, 4449, 3046, 615, 62, 31166, 82, 198, 6738, 9195, 13, 83, 6791, 1330, 751, 62...
2.235294
85
from subprocess import Popen, PIPE cmd = "echo hello world" p = Popen(cmd, shell=True, stdout=PIPE) ret, err = p.communicate()
[ 6738, 850, 14681, 1330, 8099, 268, 11, 350, 4061, 36, 198, 198, 28758, 796, 366, 30328, 23748, 995, 1, 198, 79, 796, 8099, 268, 7, 28758, 11, 7582, 28, 17821, 11, 14367, 448, 28, 47, 4061, 36, 8, 198, 1186, 11, 11454, 796, 279, ...
2.645833
48
# encoding=utf8 # pylint: disable=mixed-indentation, trailing-whitespace, multiple-statements, attribute-defined-outside-init, logging-not-lazy import logging from numpy import apply_along_axis, argmin from NiaPy.algorithms.algorithm import Algorithm logging.basicConfig() logger = logging.getLogger('NiaPy.algorithms.basic') logger.setLevel('INFO') __all__ = ['BareBonesFireworksAlgorithm'] # vim: tabstop=3 noexpandtab shiftwidth=3 softtabstop=3
[ 2, 21004, 28, 40477, 23, 198, 2, 279, 2645, 600, 25, 15560, 28, 76, 2966, 12, 521, 298, 341, 11, 25462, 12, 1929, 2737, 10223, 11, 3294, 12, 14269, 3196, 11, 11688, 12, 23211, 12, 43435, 12, 15003, 11, 18931, 12, 1662, 12, 75, 1...
2.967105
152
from ixnetwork_restpy.base import Base from ixnetwork_restpy.files import Files
[ 6738, 220, 844, 27349, 62, 2118, 9078, 13, 8692, 1330, 7308, 198, 6738, 220, 844, 27349, 62, 2118, 9078, 13, 16624, 1330, 13283, 628 ]
3.375
24
# Generated by Django 2.1.3 on 2018-11-18 02:34 from django.db import migrations, models
[ 2, 2980, 515, 416, 37770, 362, 13, 16, 13, 18, 319, 2864, 12, 1157, 12, 1507, 7816, 25, 2682, 198, 198, 6738, 42625, 14208, 13, 9945, 1330, 15720, 602, 11, 4981, 628 ]
2.84375
32
import random
[ 11748, 4738, 628 ]
5
3
jogador = dict() partidas = list() jogador['nome'] = str(input('Nome do jogador: ')) tot = int(input(f'Quantas partidas {jogador["nome"]} jogou? ')) for c in range(0, tot): partidas.append(int(input(f' Quantos gols na partida {c}? '))) jogador['gols'] = partidas[:] jogador['total'] = sum(partidas) print(30*'-=') print(jogador) print(30*'-=') for k, v in jogador.items(): print(f'O campo {k} tem o valor {v}') print(30*'-=') print(f'O jogador {jogador["nome"]} jogou {len(jogador["gols"])} partidas.') for i, v in enumerate(jogador["gols"]): print(f' => Na partida {i}, fez {v} gols.') print(f'Foi um total de {jogador["total"]} gols.') # Ou # jogador = dict() # partidas = list() # p = tot = 0 # jogador['nome'] = str(input('Nome do Jogador: ')) # quant = int(input(f'Quantas partidas {jogador["nome"]} jogou? ')) # while p < quant: # jogos = int(input(f' Quantos gols na partida {p}? ')) # partidas.append(jogos) # tot += jogos # p += 1 # jogador['gols'] = partidas # jogador['total'] = tot # print(30*'-=') # print(jogador) # print(30*'-=') # for k, v in jogador.items(): # print(f'O campo {k} tem o valor {v}') # print(30*'-=') # print(f'O jogador {jogador["nome"]} jogou {quant} partidas.') # for c, g in enumerate(partidas): # print(f' => Na partida {c}, fez {g} gols.') # print(f'Foi um total de {jogador["total"]} gols.')
[ 73, 519, 7079, 796, 8633, 3419, 198, 3911, 24496, 796, 1351, 3419, 198, 73, 519, 7079, 17816, 77, 462, 20520, 796, 965, 7, 15414, 10786, 45, 462, 466, 48342, 7079, 25, 705, 4008, 198, 83, 313, 796, 493, 7, 15414, 7, 69, 6, 24915, ...
2.224556
619
#!/usr/bin/env python import asyncio from abc import abstractmethod, ABC from enum import Enum import logging from typing import ( Optional, List, Deque ) from hummingbot.logger import HummingbotLogger from hummingbot.core.data_type.kline_stream_tracker_data_source import \ KlineStreamTrackerDataSource from hummingbot.core.data_type.kline import Kline import numpy as np import talib from collections import deque def calc_tech_indicators(self): array = [float(kline.close_price) for kline in self._klines] # self.logger().info(f"HAHA array is {array}") np_closes = np.array(array) ema_short = talib.EMA(np_closes, timeperiod=7) ema_long = talib.EMA(np_closes, timeperiod=20) macd = talib.MACD(np_closes, fastperiod=7, slowperiod=20, signalperiod=9) self._ema_short = ema_short[-1] self._ema_long = ema_long[-1] # MACD output 3 lists. We only need last list(histogram). We only # copy the last 10 histograms. self._macd_histograms = macd[-1][-10:] self.logger().info( f"(Classic) EMA_7 is {self._ema_short}, EMA_20 is {self._ema_long}, MACD(7, 20, 9) Histogram is {macd[-1][-1]} Histogram list is {self._macd_histograms}")
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 198, 11748, 30351, 952, 198, 6738, 450, 66, 1330, 12531, 24396, 11, 9738, 198, 6738, 33829, 1330, 2039, 388, 198, 11748, 18931, 198, 6738, 19720, 1330, 357, 198, 220, 220, 220, 32233, ...
2.359633
545
import discord from discord.ext import commands import asyncio from datetime import datetime, timedelta import psutil # Bot related commands
[ 11748, 36446, 201, 198, 6738, 36446, 13, 2302, 1330, 9729, 201, 198, 11748, 30351, 952, 201, 198, 6738, 4818, 8079, 1330, 4818, 8079, 11, 28805, 12514, 201, 198, 11748, 26692, 22602, 201, 198, 201, 198, 2, 18579, 3519, 9729, 201 ]
3.7
40
from Stack import Stack from random import randint
[ 6738, 23881, 1330, 23881, 198, 6738, 4738, 1330, 43720, 600, 198 ]
4.636364
11
import requests from bs4 import BeautifulSoup import json if __name__ == '__main__': loadMasterStock()
[ 11748, 7007, 198, 6738, 275, 82, 19, 1330, 23762, 50, 10486, 198, 11748, 33918, 198, 198, 361, 11593, 3672, 834, 6624, 705, 834, 12417, 834, 10354, 198, 197, 2220, 18254, 26207, 3419, 198 ]
3.181818
33
import unittest from selenium import webdriver from tests import Base if __name__ == "__main__": unittest.main()
[ 11748, 555, 715, 395, 198, 6738, 384, 11925, 1505, 1330, 3992, 26230, 198, 6738, 5254, 1330, 7308, 628, 198, 198, 361, 11593, 3672, 834, 6624, 366, 834, 12417, 834, 1298, 198, 220, 220, 220, 555, 715, 395, 13, 12417, 3419, 198 ]
2.926829
41
#!/usr/bin/env python # # Given a VOC dataset of TENNIS videos dumped at 1920x1080 resolution, this script creates a # scaled and cropped dataset. Even though the cropped zone size is static (1280x720/640x360) # crop scale), the zones themselves are dynamically selected based on the objects locations # (by reading the annotations). # The zone size 1280x720 is selected for multiple reasons: [Other size is 640x360] # a. This size (2/3 of full scale) gives grid boxes of 1/3rd the full scale. This grid size # is the minimum overlap between the diagonal zones. Horizontal and vertically aligned # zones have the overlap that is double the height or width of this grid size. The # minimum grid size is large enough to include a trail of tennis ball across three frames # even at fast speeds. This allows us to fully utilize motion information during training. # b. When images are cropped at 1280x720, and then finally scaled by 1/2, we get 640x360 # as the final image size. This works perfectly with either 533x300 or 300x300 of final # training size while still allowing for random crop for training time data augmentation. # # Alternative to 1280x720 cropping is direct cropping at 640x360. Of course, this imposes # stricter tracking requirement at inference time. # # Since we want this to work well for motion dataset for at least three frames of motion, the # algorithm reads three frames at a time to decide how to crop the images. The three frames of # motion also adds inherent hysteresis to the zone selection, making it stable. # # The algorithm is as follows: # 1. Read three sequential frames -- current, prev1, prev2 # 2. Read annotations. Use 'ball' and 'racket' objects annotations for zones selection. # 3. Create a union of bboxes for each object across three frames. Let's call this uboxes. # 4. Select zones to crop: The zone selection is based on how centered a ubox is inside a zone. # Since zones have significant overlap with each other, multiple zones may contain an # object. We compute the distance of each ubox center from the center of the zone. # For each object, the zone where this distance is the smallest is selected. # 5. Crop out the selected zone/s to create output image/s. # # Note that here the emphasis is NOT to center the objects within the cropped output. If we did # that, the network will incorrectly learn to expect the objects at the center of the image. # Since we can't provide the network with such images at the inference time, this type of # training will be useless. # Instead, we use fixed, four zone locations within the image, and select the zones purely on # the basis of how *close* an object is to a zone center. This method guarantees to create # output images where the objects will be found in various locations within the image which # adds a good amount of regularization to the training and avoid overfitting. # # For the real-time inference, the application must make an initial guess about which region # to crop for the input to the network, and may require multiple tries in the beginning. # However, once the ball is detected, the one can implement rudimentary tracking for the next # crop. Since ball detection (and not the racket detection) is the most important part of # detection, decision making is trivial. # # Just to be clear, it is not necessary to use the same zones during inference; any region # within the image will be fine as long as it contains the ball. When the ball nears the # player, the racket will automatically get into the view. Note that at the time of training, # we utilize all available samples of racket images, not just the images where both ball and # racket are visible at the same time. from __future__ import print_function import os import sys import cv2 as cv from lxml import etree from glob import glob import re import argparse sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import tennis_common as tc ## INPUT IMAGE DIMENSIONS (scaled to these dimensions if required) WIDTH = 1920 HEIGHT = 1080 ## MOTION DB setting: '3FRAMES' or 'FRAMESDIFF' MOTION_TYPE = 'FRAMESDIFF' ## Change this to view images SHOW_IMAGES = False ## Verbosity DEBUG = 0 tc.DEBUG = DEBUG ##-##################################################################################### args = parseArgs() ## Main variables IN_VOCDIR = os.path.abspath(args.invoc) IN_IMGDIR = os.path.join(IN_VOCDIR, "{}", "JPEGImages") # Template IN_ANNDIR = os.path.join(IN_VOCDIR, "{}", "Annotations") # Template OUT_VOCDIR = os.path.abspath(args.outvoc) OUT_IMGDIR = os.path.join(OUT_VOCDIR, "{}", "JPEGImages") # Template OUT_ANNDIR = os.path.join(OUT_VOCDIR, "{}", "Annotations")# Template cropsize = (int(args.height*16./9.), args.height) if args.height != 720 and args.height != 360: print("Crop height of {} is not supported (use 720 or 360).".format(args.height)) sys.exit(1) ## Find base datasets containing annotations output = tc.runSystemCmd(r"find {}/ -mindepth 3 -name '*.xml' | sed -e 's#/Annotations/.*.xml##g' | sort | uniq".format(IN_VOCDIR)) vocbases = [os.path.basename(d) for d in output] #print(vocbases) print("There are {} datasets to process".format(len(vocbases))) cnt = 0 dbcnt = 0 for vocbase in vocbases: dbcnt += 1 print("\n{}/{}. VOC Base: {}".format(dbcnt, len(vocbases), vocbase)) print("-------------------------------------------------") i_imgdir = IN_IMGDIR.format(vocbase) i_anndir = IN_ANNDIR.format(vocbase) if not os.path.isdir(i_imgdir): print("Input image dir {} is not accessible".format(i_imgdir)) if not os.path.isdir(i_anndir): print("Input annotations dir {} is not accessible".format(i_anndir)) o_imgdir = OUT_IMGDIR.format(vocbase) o_anndir = OUT_ANNDIR.format(vocbase) for idir in [o_imgdir, o_anndir]: if not os.path.isdir(idir): os.makedirs(idir) else: print("Dir {} already exists".format(idir)) ## Create image list to process imgs = glob("{}/*.jpg".format(i_imgdir)) imgs = [os.path.basename(i) for i in imgs] imgs.sort() # Sort images to pick frames in order. It is assumed the images are named likewise (fprefix, ntemplate) = tc.getNumberingScheme(imgs[0]) if cropsize[1] == 720: ## Define the grid points ## 0/3 1/3 2/3 3/3 gy = [0, int(HEIGHT/3.), int(HEIGHT*2.0/3.0), HEIGHT] gx = [0, int( WIDTH/3.), int( WIDTH*2.0/3.0), WIDTH] ## Create zones based on the grid zones = tc.BoundingBoxes('zones') # ymin xmin ymax xmax zones.addBBox([gy[0], gx[0], gy[2], gx[2]]) # Top-left zone zones.addBBox([gy[0], gx[1], gy[2], gx[3]]) # Top-right zone zones.addBBox([gy[1], gx[0], gy[3], gx[2]]) # Bottom-left zone zones.addBBox([gy[1], gx[1], gy[3], gx[3]]) # Bottom-right zone else: # cropsize[1] == 360: ## Define the grid points ## 0/6 1/6 2/6 3/6 4/6 5/6 6/6 gy = [0, int(HEIGHT/6.), int(HEIGHT/3.), int(HEIGHT/2.), int(HEIGHT*2.0/3.0), int(HEIGHT*5.0/6.0), HEIGHT] gx = [0, int( WIDTH/6.), int( WIDTH/3.), int( WIDTH/2.), int( WIDTH*2.0/3.0), int( WIDTH*5.0/6.0), WIDTH] ## Create zones based on the grid zones = tc.BoundingBoxes('zones') for y in range(len(gy)-2): for x in range(len(gx)-2): zones.addBBox([gy[y], gx[x], gy[y+2], gx[x+2]]) annnames = glob("{}/*.xml".format(i_anndir)) annnames = [os.path.basename(i) for i in annnames] annnames.sort() # Sort files to pick frames in order. It is assumed that xml/images are named likewise if len(annnames) < 3: print("This VOC Base has less than 3 annotations. Skipping.") continue kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE,(4,4)) i = 2 ## Index for annfile in annnames[2:]: annName_i = annnames[i] annName_p1 = annnames[i-1] annName_p2 = annnames[i-2] i += 1 fnum = int(re.sub(r'.*[-_](\d+).xml', r'\1', annName_i)) eannName_i = fprefix + ntemplate.format(fnum) + '.xml' eannName_p1 = fprefix + ntemplate.format(fnum-1) + '.xml' eannName_p2 = fprefix + ntemplate.format(fnum-2) + '.xml' if annName_i != eannName_i or annName_p1 != eannName_p1 or annName_p2 != eannName_p2: # Not a continuous series of three frames including previous two, we skip this frame if 1: #DEBUG>=1: print("Skipping. Frame sequence not found for {}. ".format(annName_i)) continue # Get next image/ann else: if DEBUG>=1: print("Processing {}".format(annName_i)) ## Now that we got a three sequential frames, let's read annotations and get uboxes ## uboxes = union of bboxes for each of the 'ball' or 'racket' bbox in all three images ## We are assuming only one 'ball' annotation per image. However, it is easy to handle ## multiple balls per image too. Not needed for our dataset. annfiles = [fprefix + ntemplate.format(fn) + '.xml' for fn in [fnum, fnum-1, fnum-2]] anns = [tc.getAnnotations(os.path.join(i_anndir, annfile)) for annfile in annfiles] seq = True for ann_ in anns: objs = ann_.findall('.//object/name') if 'ball' not in objs: seq = False break # don't check other anns if not seq: if 1: # DEBUG>=1: print("\tSkipping. 3 ball labels sequence not found for {}".format(annName_i)) continue # Get next image/ann ballUBox, _ = tc.getUBoxes(anns[1:]) # Find union bbox for ball label from two previous frames assert(ballUBox is not None),"Error! Cannot find union of previous two balls bounding boxes" ## Add this as a new label. We call this label 'pballs' for 'previous balls' tc.addAnnotation(anns[0], 'pballs', ballUBox) w = anns[0].size.width ## Scale input to WIDTHxHEIGHT fixed dimensions if input size is different if w != WIDTH: scale = float(WIDTH) / float(w) ## Scale annotations anns = [tc.scaleAnnotations(ann, scale) for ann in anns] else: scale = 1.0 ballUBox, racketUBox = tc.getUBoxes(anns) ## Find best enclosing zone for ball and racket UBoxes zid_b = zones.findEnclosing(ballUBox) zid_r = zones.findEnclosing(racketUBox) crop_zids = [] if zid_b == zid_r: ## Both ball and racket are in the same zone if zid_b is not None: crop_zids.append(zid_b) else: for zid in [zid_b, zid_r]: if zid is not None: crop_zids.append(zid) if DEBUG>=1: print("Crop Zones: {}".format(crop_zids)) #assert(len(crop_zids) != 0), "No zones found for cropping. This means that the frame doesn't have ball or racket" if len(crop_zids) == 0: print("No zones found for cropping. This means that the frame doesn't have ball or racket. Skipped") continue ## load images as grayscale img_i, img_p1, img_p2 = [fprefix + ntemplate.format(fn) + '.jpg' for fn in [fnum, fnum-1, fnum-2]] _cvimg_c = cv.imread(os.path.join(i_imgdir, img_i), cv.IMREAD_COLOR) _cvimg = cv.cvtColor(_cvimg_c, cv.COLOR_BGR2GRAY) _cvimg1 = cv.imread(os.path.join(i_imgdir, img_p1), cv.IMREAD_GRAYSCALE) _cvimg2 = cv.imread(os.path.join(i_imgdir, img_p2), cv.IMREAD_GRAYSCALE) if w != WIDTH: ## Resize if scale is different cvimg_c = cv.resize(_cvimg_c, (WIDTH, HEIGHT), interpolation = cv.INTER_CUBIC) cvimg = cv.resize(_cvimg, (WIDTH, HEIGHT), interpolation = cv.INTER_CUBIC) cvimg1 = cv.resize(_cvimg1, (WIDTH, HEIGHT), interpolation = cv.INTER_CUBIC) cvimg2 = cv.resize(_cvimg2, (WIDTH, HEIGHT), interpolation = cv.INTER_CUBIC) else: cvimg_c = _cvimg_c cvimg = _cvimg cvimg1 = _cvimg1 cvimg2 = _cvimg2 if MOTION_TYPE == '3FRAMES': # Merge (merge 3 grascale motion frames into BGR channels) cvimg_n = cv.merge([cvimg, cvimg1, cvimg2]) elif MOTION_TYPE == 'FRAMESDIFF': ## Create frame-diff based background subtracted image with a trail of three balls ## We are doing this (keeping the trail) on purpse. This to provide the network ## with some referene in the case when the ball is not visible in the current frame ## but it was visible in previous frames. diff_p1p2 = cv.absdiff(cvimg1, cvimg2) diff_cp1 = cv.absdiff(cvimg, cvimg1) image_b = cv.bitwise_or(diff_p1p2, diff_cp1) ## This will create the trail of three objects #bring back? =>#image_diff= cv.dilate(image_b, kernel) ## enlarge the blobs # Replace blue channel with frame diff. Blue channel is less important in tennis for us # since the ball is greenish yellow -- most information in red and green channel. cvimg_n = cvimg_c.copy() cvimg_n[:,:,0] = image_b #image_diff else: print("Unsupported motion type {}".format(MOTION_TYPE)) sys.exit(1) ## Crop images and annotations as per selected zones imgfilenames = [] annfilenames = [] outimgs = [] outanns = [] for zid in crop_zids: imgbase = fprefix + ntemplate.format(fnum) + '-z{:02d}'.format(zid) imgname = imgbase + '.jpg' annname = imgbase + '.xml' imgfilenames.append(imgname) annfilenames.append(annname) roi = zones.getBBox(zid) outann = tc.cropAnnotations(anns[0], roi, imgname, 6) outimg = zones.getImgRoI(zid, cvimg_n).copy() outanns.append(outann) outimgs.append(outimg) if DEBUG>=3 and len(crop_zids) > 1: obj_xml = etree.tostring(outann, pretty_print=True, xml_declaration=False) print("Annotation {}\n{}".format(annname, obj_xml)) ###################################################################################### ## Write output files ###################################################################################### for index in range(len(outimgs)): ## Write annotation files tc.cleanUpAnnotations(outanns[index], ['ball', 'racket', 'pballs']) tc.writeAnnotation(outanns[index], os.path.join(o_anndir, annfilenames[index])) ## Write cropped motion images imgfile = os.path.join(o_imgdir, imgfilenames[index]) if DEBUG>=2: print("Writing {}".format(imgfile)) cv.imwrite(imgfile, outimgs[index]) if SHOW_IMAGES: for zid in crop_zids: cvimg_n = drawZone(cvimg_n, zones, zid, cropsize) for index in range(len(outimgs)): img = outimgs[index] for obj in outanns[index].iter('object'): bbox = [obj.bndbox.ymin, obj.bndbox.xmin, obj.bndbox.ymax, obj.bndbox.xmax] outimgs[index] = tc.drawBoundingBox(outimgs[index], bbox, tc.LBL_IDS[obj.name]) ## Draw bounding boxes if ballUBox is not None: cvimg_n = tc.drawBoundingBox(cvimg_n, ballUBox, 1) if racketUBox is not None: cvimg_n = tc.drawBoundingBox(cvimg_n, racketUBox, 2) show_imgs(cvimg_c, cvimg_n, outimgs) #if (cnt >= 50): # assert(False), "Temp forced exit to check work. Remove later." cnt += 1 cv.destroyAllWindows() print("Done. Motion Dataset created with {} annotations and images".format(cnt))
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 2, 198, 2, 11259, 257, 569, 4503, 27039, 286, 309, 34571, 1797, 5861, 24105, 379, 14062, 87, 24045, 6323, 11, 428, 4226, 8075, 257, 198, 2, 27464, 290, 48998, 27039, 13, 3412, 996, 2...
2.378251
6,805
"""Tests for core.config.""" import json import yaml from pathlib import Path import pytest import fromconfig def test_core_config_no_jsonnet(tmpdir, monkeypatch): """Test jsonnet missing handling.""" monkeypatch.setattr(fromconfig.core.config, "_jsonnet", None) # No issue to dump even if missing config = {"x": 2} fromconfig.dump(config, str(tmpdir.join("config.jsonnet"))) fromconfig.dump(config, str(tmpdir.join("config.json"))) fromconfig.dump(config, str(tmpdir.join("config.yaml"))) fromconfig.dump(config, str(tmpdir.join("config.yml"))) # No issue to load non-jsonnet files assert fromconfig.load(str(tmpdir.join("config.json"))) == config assert fromconfig.load(str(tmpdir.join("config.yaml"))) == config assert fromconfig.load(str(tmpdir.join("config.yml"))) == config # Raise import error if reloading from jsonnet with pytest.raises(ImportError): fromconfig.load(str(tmpdir.join("config.jsonnet"))) def test_core_config(): """Test Config.""" config = fromconfig.Config(x=1) assert config["x"] == 1 assert list(config) == ["x"] config["x"] = 2 assert config["x"] == 2 def test_core_config_is_json_serializable(): """Test that Config is json serializable.""" config = fromconfig.Config(x=1) assert json.dumps(config) == '{"x": 1}'
[ 37811, 51, 3558, 329, 4755, 13, 11250, 526, 15931, 198, 198, 11748, 33918, 198, 11748, 331, 43695, 198, 6738, 3108, 8019, 1330, 10644, 198, 198, 11748, 12972, 9288, 198, 198, 11748, 422, 11250, 628, 198, 4299, 1332, 62, 7295, 62, 11250,...
2.748988
494
#!/usr/bin/env python # # Release script for beholder # import hashlib import urllib from collections import OrderedDict try: from hostage import * #pylint: disable=unused-wildcard-import,wildcard-import except ImportError: print "!! Release library unavailable." print "!! Use `pip install hostage` to fix." print "!! You will also need an API token in .github.token," print "!! a .hubrrc config, or `brew install hub` configured." print "!! A $GITHUB_TOKEN env variable will also work." exit(1) # # Globals # notes = File(".last-release-notes") latestTag = git.Tag.latest() def buildLabeled(labelsToTitles): """Given a set of (label, title) tuples, produces an OrderedDict whose keys are `label`, and whose values are dictionaries containing 'title' -> `title`, and 'content' -> string. The iteration order of the dictionary will preserve the ordering of the provided tuples """ result = OrderedDict() for k, v in labelsToTitles: result[k] = {'title': v, 'content': ''} return result # # Verify # verify(Grep("stopship", inDir="src").foundAny(silent=False)) \ .then(echoAndDie("I don't think so")) version = verify(File("src/beholder.go") .filtersTo(RegexFilter('const Version = "(.*)"')) ).valueElse(echoAndDie("No version!?")) versionTag = git.Tag(version) verify(versionTag.exists())\ .then(echoAndDie("Version `%s` already exists!" % version)) # # Make sure all the tests pass # # this syntax recursively checks all subpackages for tests verify(Execute("go test ./... -v")).succeeds(silent=False).orElse(die()) # # Build the release notes # initialNotes = verify(notes.contents()).valueElse(buildDefaultNotes) notes.delete() verify(Edit(notes, withContent=initialNotes).didCreate())\ .orElse(echoAndDie("Aborted due to empty message")) releaseNotes = notes.contents() # # Compile # versions = [ # (label, os, arch) tuples ("macOS", "darwin", "amd64"), ("windows-x64", "windows", "amd64"), ] compiled = [] for (buildLabel, os, arch) in versions: f = 'bin/beholder-%s-%s' % (version, buildLabel) if os == "windows": f += ".exe" print "Compiling:", f cmd = 'env GOOS=%s GOARCH=%s go build -v -o %s' % (os, arch, f) verify(Execute(cmd)).succeeds(silent=False) compiled.append(f) # # Upload to github # print "Uploading to Github..." verify(versionTag).create() verify(versionTag).push("origin") gitRelease = github.Release(version) verify(gitRelease).create(body=releaseNotes) for f in compiled: print "Uploading", f verify(gitRelease).uploadFile(f, 'application/octet-stream') # # Update homebrew repo # print "Updating homebrew..." tarUrl = 'https://github.com/dhleong/beholder/archive/%s.tar.gz' % version tarSha = sha256(tarUrl) homebrewConfig = github.Config("dhleong/homebrew-tap") formulaFile = github.RepoFile("/Formula/beholder.rb", config=homebrewConfig) oldContents = formulaFile.read() newContents = oldContents newContents = re.sub('url "[^"]+"', 'url "%s"' % tarUrl, newContents) newContents = re.sub('sha256 "[^"]+"', 'sha256 "%s"' % tarSha, newContents) print " url <-", tarUrl print " sha256 <-", tarSha commit = 'Update for v%s' % version verify(formulaFile).write(newContents, commitMessage=commit) # # Success! Now, just cleanup and we're done! # notes.delete() print "Done! Published %s" % version
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 2, 198, 2, 13868, 4226, 329, 1372, 19892, 198, 2, 198, 198, 11748, 12234, 8019, 198, 11748, 2956, 297, 571, 198, 6738, 17268, 1330, 14230, 1068, 35, 713, 198, 198, 28311, 25, 198, 22...
2.73328
1,256
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations import jsonfield.fields
[ 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 6738, 11593, 37443, 834, 1330, 28000, 1098, 62, 17201, 874, 198, 198, 6738, 42625, 14208, 13, 9945, 1330, 4981, 11, 15720, 602, 198, 11748, 33918, 3245, 13, 25747, 628 ]
3.046512
43
from API.models import GoesWellWith, Menu
[ 6738, 7824, 13, 27530, 1330, 31914, 5779, 3152, 11, 21860, 198 ]
3.818182
11
from typing import Optional from algorithms.basic_testing import BasicTesting from simulator.controllers.main_controller import MainController from simulator.controllers.map.map_controller import MapController from simulator.controllers.gui.gui_controller import GuiController from simulator.models.main_model import MainModel from simulator.models.map_model import MapModel from simulator.services.debug import DebugLevel from simulator.services.services import Services from simulator.services.event_manager.events.event import Event from simulator.services.event_manager.events.reinit_event import ReinitEvent from simulator.views.main_view import MainView from simulator.views.map.map_view import MapView from simulator.views.gui.gui_view import GuiView from structures import Size """ Implementation is done after https://github.com/wesleywerner/mvc-game-design """
[ 6738, 19720, 1330, 32233, 198, 198, 6738, 16113, 13, 35487, 62, 33407, 1330, 14392, 44154, 198, 6738, 35375, 13, 3642, 36667, 13, 12417, 62, 36500, 1330, 8774, 22130, 198, 6738, 35375, 13, 3642, 36667, 13, 8899, 13, 8899, 62, 36500, 133...
4.107981
213
# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import api, fields, models, _ from odoo.addons import decimal_precision as dp
[ 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 2, 2142, 286, 10529, 2238, 13, 4091, 38559, 24290, 2393, 329, 1336, 6634, 290, 15665, 3307, 13, 198, 198, 6738, 16298, 2238, 1330, 40391, 11, 7032, 11, 4981, 11, 4808, ...
3.20339
59
from django.urls import path from .views import ContactCreateView urlpatterns = [ path('',ContactCreateView.as_view()), ]
[ 6738, 42625, 14208, 13, 6371, 82, 1330, 3108, 198, 6738, 764, 33571, 1330, 14039, 16447, 7680, 198, 198, 6371, 33279, 82, 796, 685, 198, 220, 220, 220, 3108, 10786, 3256, 17829, 16447, 7680, 13, 292, 62, 1177, 3419, 828, 198, 60 ]
3.073171
41
# modelo anterior - Enquanto cont at 10 for verdade, ser repetido cont = 1 while cont <= 10: print(cont, ' ...', end='') cont += 1 print('FIM') # Usando o Enquanto VERDADE ele vai repetir para sempre, temos que colocar uma condio PARA=BREAK n = s = 0 while True: n = int(input('Digite um nmero: [Digite 999 para PARAR] ')) if n == 999: break s += n print(f'A soma vale {s}')
[ 2, 2746, 78, 32700, 532, 2039, 421, 14723, 542, 379, 838, 329, 3326, 67, 671, 11, 1055, 46152, 17305, 198, 3642, 796, 352, 220, 198, 4514, 542, 19841, 838, 25, 198, 220, 220, 220, 3601, 7, 3642, 11, 705, 2644, 3256, 886, 28, 7061,...
2.368421
171
import settings import handlers.base_handler import csv
[ 11748, 6460, 198, 11748, 32847, 13, 8692, 62, 30281, 198, 11748, 269, 21370, 198 ]
4
14
# -*- coding: utf-8 -*- import os import urllib.request from urllib.request import urlopen # url from urllib.parse import quote_plus # from bs4 import BeautifulSoup from selenium import webdriver # webdriver import time # from time import sleep import warnings # from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.webdriver.chrome.options import Options from MakeExcel import MakeFollowerExcel from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC warnings.filterwarnings(action='ignore') # # url baseUrl = "https://www.instagram.com/" SCROLL_PAUSE_TIME = 1.0
[ 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 220, 198, 198, 11748, 28686, 198, 11748, 2956, 297, 571, 13, 25927, 198, 6738, 2956, 297, 571, 13, 25927, 1330, 19016, 9654, 220, 1303, 220, 19016, 220, 220, 198, 6738, 2956,...
2.968992
258
#!/usr/bin/env python # -*- coding: utf-8 -*- import click if __name__ == '__main__': main()
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 198, 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 198, 11748, 3904, 628, 628, 198, 361, 11593, 3672, 834, 6624, 705, 834, 12417, 834, 10354, 198, 220, 220, 220, 1388, ...
2.217391
46
from .bot import ItsnpBot
[ 6738, 764, 13645, 1330, 6363, 37659, 20630, 201, 198 ]
3
9
# Copyright 2014 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. import mojo_lexer import unittest # Try to load the ply module, if not, then assume it is in the third_party # directory. try: # Disable lint check which fails to find the ply module. # pylint: disable=F0401 from ply import lex except ImportError: # This assumes this file is in src/mojo/public/tools/bindings/pylib/parse/. module_path, module_name = os.path.split(__file__) third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir, os.pardir, os.pardir, 'third_party') sys.path.append(third_party) # pylint: disable=F0401 from ply import lex # This (monkey-patching LexToken to make comparison value-based) is evil, but # we'll do it anyway. (I'm pretty sure ply's lexer never cares about comparing # for object identity.) setattr(lex.LexToken, '__eq__', _LexTokenEq) def _MakeLexToken(type, value, lineno=1, lexpos=0): """Makes a LexToken with the given parameters. (Note that lineno is 1-based, but lexpos is 0-based.)""" rv = lex.LexToken() rv.type, rv.value, rv.lineno, rv.lexpos = type, value, lineno, lexpos return rv def _MakeLexTokenForKeyword(keyword, **kwargs): """Makes a LexToken for the given keyword.""" return _MakeLexToken(keyword.upper(), keyword.lower(), **kwargs) if __name__ == "__main__": unittest.main()
[ 2, 15069, 1946, 383, 18255, 1505, 46665, 13, 1439, 2489, 10395, 13, 198, 2, 5765, 286, 428, 2723, 2438, 318, 21825, 416, 257, 347, 10305, 12, 7635, 5964, 326, 460, 307, 198, 2, 1043, 287, 262, 38559, 24290, 2393, 13, 198, 198, 11748...
2.83619
525
from django.contrib import admin from glue.models import *
[ 6738, 42625, 14208, 13, 3642, 822, 1330, 13169, 198, 6738, 22749, 13, 27530, 1330, 1635, 198 ]
3.6875
16
from typing import Any, Dict, List from qmt.infrastructure import WithParts
[ 6738, 19720, 1330, 4377, 11, 360, 713, 11, 7343, 198, 6738, 10662, 16762, 13, 10745, 6410, 1330, 2080, 42670, 628 ]
3.85
20
#!/usr/bin/python # # Copyright (c) 2015, Arista Networks, Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # Neither the name of Arista Networks nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # DOCUMENTATION = """ --- module: eos_routemap short_description: Manage EOS routemap resources description: - This module will manage routemap entries on EOS nodes version_added: 1.2.0 category: Route Policy author: Arista EOS+ requirements: - Arista EOS 4.13.7M or later with command API enabled - Python Client for eAPI 0.4.0 or later notes: - All configuration is idempotent unless otherwise specified - Supports eos metaparameters for using the eAPI transport - Supports stateful resource configuration. options: name: description: - The name of the routemap to manage. required: true default: null choices: [] aliases: [] version_added: 1.2.0 action: description: - The action associated with the routemap name. required: true default: 'permit' choices: ['permit','deny'] aliases: [] version_added: 1.2.0 seqno: description: - The sequence number of the rule that this entry corresponds to. required: true default: null choices: [] aliases: [] version_added: 1.2.0 description: description: - The description for this routemap entry. required: false default: null choices: [] aliases: [] version_added: 1.2.0 match: description: - The list of match statements that define the routemap entry. The match statements should be a comma separated list of match statements without the word match at the beginning of the string. See the example below for more information. required: false default: null choices: [] aliases: [] version_added: 1.2.0 set: description: - The list of set statements that define the routemap entry. The set statements should be a comma separated list of set statements without the word set at the beginning of the string. See the example below for more information. required: false default: null choices: [] aliases: [] version_added: 1.2.0 continue: description: - The statement defines the next routemap clause to evaluate. required: false default: null choices: [] aliases: [] version_added: 1.2.0 """ EXAMPLES = """ - eos_routemap: name=rm1 action=permit seqno=10 description='this is a great routemap' match='as 50,interface Ethernet2' set='tag 100,weight 1000' continue=20 """ #<<EOS_COMMON_MODULE_START>> import syslog import collections from ansible.module_utils.basic import * try: import pyeapi PYEAPI_AVAILABLE = True except ImportError: PYEAPI_AVAILABLE = False DEFAULT_SYSLOG_PRIORITY = syslog.LOG_NOTICE DEFAULT_CONNECTION = 'localhost' TRANSPORTS = ['socket', 'http', 'https', 'http_local'] def map_argument_spec(self): """map_argument_spec maps only the module argument spec to attrs This method will map the argumentspec minus the meta_args to attrs and return the attrs. This returns a dict object that includes only the original argspec plus the stateful_args (if self._stateful=True) Returns: dict: Returns a dict object that includes the original argument_spec plus stateful_args with values minus meta_args """ keys = set(self.params).difference(self.meta_args) attrs = dict() attrs = dict([(k, self.params[k]) for k in self.params if k in keys]) if 'CHECKMODE' in attrs: del attrs['CHECKMODE'] return attrs def validate(self): for key, value in self.attributes.iteritems(): func = self.func('validate_%s' % key) if func: self.attributes[key] = func(value) #<<EOS_COMMON_MODULE_END>> def instance(module): """ Returns an instance of Routemaps based on name, action and sequence number. """ name = module.attributes['name'] action = module.attributes['action'] seqno = int(module.attributes['seqno']) _instance = dict(name=name, action=action, seqno=seqno, state='absent') try: result = module.api('routemaps').get(name)[action][seqno] except: result = None if result: _instance['state'] = 'present' _instance['seqno'] = str(seqno) _instance['set'] = ','.join(result['set']) desc = result['description'] _instance['description'] = desc if desc else '' _instance['match'] = ','.join(result['match']) cont = result['continue'] _instance['continue'] = str(cont) if cont else '' return _instance def set_description(module): """ Configures the description for the routemap """ name = module.attributes['name'] action = module.attributes['action'] seqno = int(module.attributes['seqno']) value = module.attributes['description'] module.log('Invoked set_description with %s for eos_routemap[%s %s %s]' % (value, name, action, seqno)) if value == '': module.node.api('routemaps').set_description(name, action, seqno, disable=True) else: module.node.api('routemaps').set_description(name, action, seqno, value) def set_continue(module): """ Configures the continue value for the routemap """ name = module.attributes['name'] action = module.attributes['action'] seqno = int(module.attributes['seqno']) try: value = int(module.attributes['continue']) except: value = None module.log('Invoked set_continue for eos_routemap[%s %s %s]' % (name, action, seqno)) if value is None: module.node.api('routemaps').set_continue(name, action, seqno, disable=True) else: module.node.api('routemaps').set_continue(name, action, seqno, value) def set_match(module): """ Configures the match statements for the routemap """ name = module.attributes['name'] action = module.attributes['action'] seqno = int(module.attributes['seqno']) statements = module.attributes['match'].split(',') module.log('Invoked set_match for eos_routemap[%s %s %s]' % (name, action, seqno)) module.node.api('routemaps').set_match_statements(name, action, seqno, statements) def set_set(module): """ Configures the set statements for the routemap """ name = module.attributes['name'] action = module.attributes['action'] seqno = int(module.attributes['seqno']) statements = module.attributes['set'].split(',') module.log('Invoked set_set for eos_routemap[%s %s %s]' % (name, action, seqno)) module.node.api('routemaps').set_set_statements(name, action, seqno, statements) def main(): """ The main module routine called when the module is run by Ansible """ argument_spec = dict( name=dict(required=True), action=dict(default='permit', choices=['permit', 'deny']), seqno=dict(required=True), description=dict(), match=dict(), set=dict() ) argument_spec['continue'] = dict() module = EosAnsibleModule(argument_spec=argument_spec, supports_check_mode=True) module.flush(True) main()
[ 2, 48443, 14629, 14, 8800, 14, 29412, 198, 2, 198, 2, 15069, 357, 66, 8, 1853, 11, 943, 12523, 27862, 11, 3457, 13, 198, 2, 1439, 2489, 10395, 13, 198, 2, 198, 2, 2297, 396, 3890, 290, 779, 287, 2723, 290, 13934, 5107, 11, 351, ...
2.596476
3,462
"""Setup configuration.""" import setuptools from furystoolbox import __version__ with open("README.md", "r") as fh: LONG = fh.read() REQUIRES = ['click>=7.0', 'requests>=2.21.0', 'PyGithub>=1.43.4'] setuptools.setup( name="furystoolbox", version=__version__, author="Joakim Sorensen", author_email="ludeeus@gmail.com", description="A collection of tools.", long_description=LONG, long_description_content_type="text/markdown", url="https://github.com/ludeeus/furystoolbox", install_requires=REQUIRES, packages=setuptools.find_packages(), classifiers=( "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ), entry_points={ 'console_scripts': [ 'fury = furystoolbox.cli.cli:CLI' ] } )
[ 37811, 40786, 8398, 526, 15931, 198, 11748, 900, 37623, 10141, 198, 6738, 26206, 301, 970, 3524, 1330, 11593, 9641, 834, 198, 198, 4480, 1280, 7203, 15675, 11682, 13, 9132, 1600, 366, 81, 4943, 355, 277, 71, 25, 198, 220, 220, 220, 44...
2.276215
391
#skip.runas import Image; im = Image.open("Scribus.gif"); image_list = list(im.getdata()); cols, rows = im.size; res = range(len(image_list)); sobelFilter(image_list, res, cols, rows) #runas cols = 100; rows = 100 ;image_list=[x%10+y%20 for x in xrange(cols) for y in xrange(rows)]; sobelFilter(image_list, cols, rows) #bench cols = 1000; rows = 500 ;image_list=[x%10+y%20 for x in xrange(cols) for y in xrange(rows)]; sobelFilter(image_list, cols, rows) #pythran export sobelFilter(int list, int, int)
[ 2, 48267, 13, 5143, 292, 1330, 7412, 26, 545, 796, 7412, 13, 9654, 7203, 3351, 822, 385, 13, 27908, 15341, 2939, 62, 4868, 796, 1351, 7, 320, 13, 1136, 7890, 35430, 951, 82, 11, 15274, 796, 545, 13, 7857, 26, 581, 796, 2837, 7, ...
2.619792
192
import numpy as np import torch from spconv.pytorch.utils import PointToVoxel from scipy.spatial.transform import Rotation from pcdet.datasets import DatasetTemplate
[ 11748, 299, 32152, 355, 45941, 198, 11748, 28034, 198, 198, 6738, 599, 42946, 13, 9078, 13165, 354, 13, 26791, 1330, 6252, 2514, 53, 1140, 417, 198, 6738, 629, 541, 88, 13, 2777, 34961, 13, 35636, 1330, 371, 14221, 198, 198, 6738, 279...
3.090909
55
""" This is the script containing the calibration module, basically calculating homography matrix. This code and data is released under the Creative Commons Attribution-NonCommercial 4.0 International license (CC BY-NC.) In a nutshell: # The license is only for non-commercial use (commercial licenses can be obtained from Stanford). # The material is provided as-is, with no warranties whatsoever. # If you publish any code, data, or scientific work based on this, please cite our work. Technical Paper: Y. Peng, S. Choi, N. Padmanaban, G. Wetzstein. Neural Holography with Camera-in-the-loop Training. ACM TOG (SIGGRAPH Asia), 2020. """ import cv2 import matplotlib.pyplot as plt import numpy as np def circle_detect(captured_img, num_circles, spacing, pad_pixels=(0., 0.), show_preview=True): """ Detects the circle of a circle board pattern :param captured_img: captured image :param num_circles: a tuple of integers, (num_circle_x, num_circle_y) :param spacing: a tuple of integers, in pixels, (space between circles in x, space btw circs in y direction) :param show_preview: boolean, default True :param pad_pixels: coordinate of the left top corner of warped image. Assuming pad this amount of pixels on the other side. :return: a tuple, (found_dots, H) found_dots: boolean, indicating success of calibration H: a 3x3 homography matrix (numpy) """ # Binarization # org_copy = org.copy() # Otherwise, we write on the original image! img = (captured_img.copy() * 255).astype(np.uint8) if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(img, 15) img_gray = img.copy() img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 121, 0) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)) img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) img = 255 - img # Blob detection params = cv2.SimpleBlobDetector_Params() # Change thresholds params.filterByColor = True params.minThreshold = 128 # Filter by Area. params.filterByArea = True params.minArea = 50 # Filter by Circularity params.filterByCircularity = True params.minCircularity = 0.785 # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.87 # Filter by Inertia params.filterByInertia = False params.minInertiaRatio = 0.01 detector = cv2.SimpleBlobDetector_create(params) # Detecting keypoints # this is redundant for what comes next, but gives us access to the detected dots for debug keypoints = detector.detect(img) found_dots, centers = cv2.findCirclesGrid(img, num_circles, blobDetector=detector, flags=cv2.CALIB_CB_SYMMETRIC_GRID) # Drawing the keypoints cv2.drawChessboardCorners(captured_img, num_circles, centers, found_dots) img_gray = cv2.drawKeypoints(img_gray, keypoints, np.array([]), (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Find transformation H = np.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=np.float32) if found_dots: # Generate reference points to compute the homography ref_pts = np.zeros((num_circles[0] * num_circles[1], 1, 2), np.float32) pos = 0 for i in range(0, num_circles[1]): for j in range(0, num_circles[0]): ref_pts[pos, 0, :] = spacing * np.array([j, i]) + np.array(pad_pixels) pos += 1 H, mask = cv2.findHomography(centers, ref_pts, cv2.RANSAC, 1) if show_preview: dsize = [int((num_circs - 1) * space + 2 * pad_pixs) for num_circs, space, pad_pixs in zip(num_circles, spacing, pad_pixels)] captured_img_warp = cv2.warpPerspective(captured_img, H, tuple(dsize)) if show_preview: fig = plt.figure() ax = fig.add_subplot(223) ax.imshow(img_gray, cmap='gray') ax2 = fig.add_subplot(221) ax2.imshow(img, cmap='gray') ax3 = fig.add_subplot(222) ax3.imshow(captured_img, cmap='gray') if found_dots: ax4 = fig.add_subplot(224) ax4.imshow(captured_img_warp, cmap='gray') plt.show() return found_dots, H
[ 37811, 198, 1212, 318, 262, 4226, 7268, 262, 36537, 8265, 11, 6209, 26019, 3488, 4867, 17593, 13, 198, 198, 1212, 2438, 290, 1366, 318, 2716, 739, 262, 17404, 13815, 45336, 12, 15419, 48401, 604, 13, 15, 4037, 5964, 357, 4093, 11050, ...
2.306977
1,935
from django.apps import AppConfig from django.contrib.admin.apps import AdminConfig
[ 6738, 42625, 14208, 13, 18211, 1330, 2034, 16934, 198, 6738, 42625, 14208, 13, 3642, 822, 13, 28482, 13, 18211, 1330, 32053, 16934, 198 ]
3.652174
23
from researchutils.task import plotter
[ 6738, 2267, 26791, 13, 35943, 1330, 7110, 353 ]
4.75
8
''' # Amazon Kinesis Construct Library <!--BEGIN STABILITY BANNER-->--- ![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge) ![cdk-constructs: Stable](https://img.shields.io/badge/cdk--constructs-stable-success.svg?style=for-the-badge) --- <!--END STABILITY BANNER--> [Amazon Kinesis](https://docs.aws.amazon.com/streams/latest/dev/introduction.html) provides collection and processing of large [streams](https://aws.amazon.com/streaming-data/) of data records in real time. Kinesis data streams can be used for rapid and continuous data intake and aggregation. ## Table Of Contents * [Streams](#streams) * [Encryption](#encryption) * [Import](#import) * [Permission Grants](#permission-grants) * [Read Permissions](#read-permissions) * [Write Permissions](#write-permissions) * [Custom Permissions](#custom-permissions) * [Metrics](#metrics) ## Streams Amazon Kinesis Data Streams ingests a large amount of data in real time, durably stores the data, and makes the data available for consumption. Using the CDK, a new Kinesis stream can be created as part of the stack using the construct's constructor. You may specify the `streamName` to give your own identifier to the stream. If not, CloudFormation will generate a name. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Stream(self, "MyFirstStream", stream_name="my-awesome-stream" ) ``` You can also specify properties such as `shardCount` to indicate how many shards the stream should choose and a `retentionPeriod` to specify how long the data in the shards should remain accessible. Read more at [Creating and Managing Streams](https://docs.aws.amazon.com/streams/latest/dev/working-with-streams.html) ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Stream(self, "MyFirstStream", stream_name="my-awesome-stream", shard_count=3, retention_period=Duration.hours(48) ) ``` ### Encryption [Stream encryption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-kinesis-stream-streamencryption.html) enables server-side encryption using an AWS KMS key for a specified stream. Encryption is enabled by default on your stream with the master key owned by Kinesis Data Streams in regions where it is supported. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Stream(self, "MyEncryptedStream") ``` You can enable encryption on your stream with a user-managed key by specifying the `encryption` property. A KMS key will be created for you and associated with the stream. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Stream(self, "MyEncryptedStream", encryption=StreamEncryption.KMS ) ``` You can also supply your own external KMS key to use for stream encryption by specifying the `encryptionKey` property. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 import aws_cdk.aws_kms as kms key = kms.Key(self, "MyKey") Stream(self, "MyEncryptedStream", encryption=StreamEncryption.KMS, encryption_key=key ) ``` ### Import Any Kinesis stream that has been created outside the stack can be imported into your CDK app. Streams can be imported by their ARN via the `Stream.fromStreamArn()` API ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 stack = Stack(app, "MyStack") imported_stream = Stream.from_stream_arn(stack, "ImportedStream", "arn:aws:kinesis:us-east-2:123456789012:stream/f3j09j2230j") ``` Encrypted Streams can also be imported by their attributes via the `Stream.fromStreamAttributes()` API ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 from aws_cdk.aws_kms import Key stack = Stack(app, "MyStack") imported_stream = Stream.from_stream_attributes(stack, "ImportedEncryptedStream", stream_arn="arn:aws:kinesis:us-east-2:123456789012:stream/f3j09j2230j", encryption_key=kms.Key.from_key_arn("arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012") ) ``` ### Permission Grants IAM roles, users or groups which need to be able to work with Amazon Kinesis streams at runtime should be granted IAM permissions. Any object that implements the `IGrantable` interface (has an associated principal) can be granted permissions by calling: * `grantRead(principal)` - grants the principal read access * `grantWrite(principal)` - grants the principal write permissions to a Stream * `grantReadWrite(principal)` - grants principal read and write permissions #### Read Permissions Grant `read` access to a stream by calling the `grantRead()` API. If the stream has an encryption key, read permissions will also be granted to the key. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 lambda_role = iam.Role(self, "Role", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"), description="Example role..." ) stream = Stream(self, "MyEncryptedStream", encryption=StreamEncryption.KMS ) # give lambda permissions to read stream stream.grant_read(lambda_role) ``` The following read permissions are provided to a service principal by the `grantRead()` API: * `kinesis:DescribeStreamSummary` * `kinesis:GetRecords` * `kinesis:GetShardIterator` * `kinesis:ListShards` * `kinesis:SubscribeToShard` #### Write Permissions Grant `write` permissions to a stream is provided by calling the `grantWrite()` API. If the stream has an encryption key, write permissions will also be granted to the key. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 lambda_role = iam.Role(self, "Role", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"), description="Example role..." ) stream = Stream(self, "MyEncryptedStream", encryption=StreamEncryption.KMS ) # give lambda permissions to write to stream stream.grant_write(lambda_role) ``` The following write permissions are provided to a service principal by the `grantWrite()` API: * `kinesis:ListShards` * `kinesis:PutRecord` * `kinesis:PutRecords` #### Custom Permissions You can add any set of permissions to a stream by calling the `grant()` API. ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 user = iam.User(stack, "MyUser") stream = Stream(stack, "MyStream") # give my user permissions to list shards stream.grant(user, "kinesis:ListShards") ``` ### Metrics You can use common metrics from your stream to create alarms and/or dashboards. The `stream.metric('MetricName')` method creates a metric with the stream namespace and dimension. You can also use pre-define methods like `stream.metricGetRecordsSuccess()`. To find out more about Kinesis metrics check [Monitoring the Amazon Kinesis Data Streams Service with Amazon CloudWatch](https://docs.aws.amazon.com/streams/latest/dev/monitoring-with-cloudwatch.html). ```python # Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 stream = Stream(stack, "MyStream") # Using base metric method passing the metric name stream.metric("GetRecords.Success") # using pre-defined metric method stream.metric_get_records_success() # using pre-defined and overriding the statistic stream.metric_get_records_success(statistic="Maximum") ``` ''' import abc import builtins import datetime import enum import typing import jsii import publication import typing_extensions from ._jsii import * import aws_cdk.aws_cloudwatch import aws_cdk.aws_iam import aws_cdk.aws_kms import aws_cdk.core import constructs class _IStreamProxy( jsii.proxy_for(aws_cdk.core.IResource) # type: ignore[misc] ): '''A Kinesis Stream.''' __jsii_type__: typing.ClassVar[str] = "@aws-cdk/aws-kinesis.IStream" # Adding a "__jsii_proxy_class__(): typing.Type" function to the interface typing.cast(typing.Any, IStream).__jsii_proxy_class__ = lambda : _IStreamProxy __all__ = [ "CfnStream", "CfnStreamConsumer", "CfnStreamConsumerProps", "CfnStreamProps", "IStream", "Stream", "StreamAttributes", "StreamEncryption", "StreamProps", ] publication.publish()
[ 7061, 6, 198, 2, 6186, 509, 1127, 271, 28407, 10074, 198, 198, 27, 28112, 33, 43312, 47550, 25382, 347, 1565, 21479, 46904, 6329, 628, 198, 0, 58, 12993, 77, 12, 37540, 25, 520, 540, 16151, 5450, 1378, 9600, 13, 26662, 82, 13, 952, ...
3.211778
2,649
import torch import torch.nn as nn import torchvision.models as models from torch.nn.utils.rnn import pack_padded_sequence
[ 11748, 28034, 198, 11748, 28034, 13, 20471, 355, 299, 77, 198, 11748, 28034, 10178, 13, 27530, 355, 4981, 198, 6738, 28034, 13, 20471, 13, 26791, 13, 81, 20471, 1330, 2353, 62, 79, 29373, 62, 43167, 198, 220, 220, 220, 220, 198 ]
3.121951
41
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jul 16 14:36:46 2019 @author: Tawanda """ import sys import argparse from selenium import webdriver from selenium.common.exceptions import NoSuchElementException if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--driver", help="path to chrome driver") args = parser.parse_args() if not args.driver: print("Please enter a valid path to the chrome driver ( --driver argument )") sys.exit(1) browser = webdriver.Chrome(executable_path=args.driver) browser.implicitly_wait(10) browser.maximize_window() try: browser.get('https://www.oursky.com/') button = browser.find_element_by_class_name('btn-header') button.click() print('=======Button Click test was successful=======') except NoSuchElementException as ex: print(f'Error :: No such element : {ex}')
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 2, 532, 9, 12, 19617, 25, 3384, 69, 12, 23, 532, 9, 12, 198, 37811, 198, 41972, 319, 30030, 5979, 1467, 1478, 25, 2623, 25, 3510, 13130, 198, 198, 31, 9800, 25, 309, 707, 528...
2.615176
369
from .base_reader import BaseReader import pandas as pd
[ 6738, 764, 8692, 62, 46862, 1330, 7308, 33634, 198, 198, 11748, 19798, 292, 355, 279, 67, 628 ]
3.411765
17
"""Learn ideal points with the text-based ideal point model (TBIP). Let y_{dv} denote the counts of word v in document d. Let x_d refer to the ideal point of the author of document d. Then we model: theta, beta ~ Gamma(alpha, alpha) x, eta ~ N(0, 1) y_{dv} ~ Pois(sum_k theta_dk beta_kv exp(x_d * eta_kv). We perform variational inference to provide estimates for the posterior distribution of each latent variable. We take reparameterization gradients, using a lognormal variational family for the positive variables (theta, beta) and a normal variational family for the real variables (x, eta). The directory `data/{data_name}/clean/` should have the following four files: 1. `counts.npz`: a [num_documents, num_words] sparse matrix containing the word counts for each document. 2. `author_indices.npy`: a [num_documents] vector where each entry is an integer in the set {0, 1, ..., num_authors - 1}, indicating the author of the corresponding document in `counts.npz`. 3. `vocabulary.txt`: a [num_words]-length file where each line is a string denoting the corresponding word in the vocabulary. 4. `author_map.txt`: a [num_authors]-length file where each line is a string denoting the name of an author in the corpus. We provide more details in our paper [1]. #### References [1]: Keyon Vafa, Suresh Naidu, David Blei. Text-Based Ideal Points. In _Conference of the Association for Computational Linguistics_, 2020. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import functools import os import time from absl import flags import numpy as np import scipy.sparse as sparse import tensorflow as tf import tensorflow_probability as tfp flags.DEFINE_float("learning_rate", default=0.01, help="Adam learning rate.") flags.DEFINE_integer("max_steps", default=1000000, help="Number of training steps to run.") flags.DEFINE_integer("num_topics", default=50, help="Number of topics.") flags.DEFINE_integer("batch_size", default=1024, help="Batch size.") flags.DEFINE_integer("num_samples", default=1, help="Number of samples to use for ELBO approximation.") flags.DEFINE_enum("counts_transformation", default="nothing", enum_values=["nothing", "binary", "sqrt", "log"], help="Transformation used on counts data.") flags.DEFINE_boolean("pre_initialize_parameters", default=True, help="Whether to use pre-initialized document and topic " "intensities (with Poisson factorization).") flags.DEFINE_string("data", default="senate-speeches-114", help="Data source being used.") flags.DEFINE_integer("senate_session", default=113, help="Senate session (used only when data is " "'senate-speech-comparisons'.") flags.DEFINE_integer("print_steps", default=500, help="Number of steps to print and save results.") flags.DEFINE_integer("seed", default=123, help="Random seed to be used.") FLAGS = flags.FLAGS def build_input_pipeline(data_dir, batch_size, random_state, counts_transformation="nothing"): """Load data and build iterator for minibatches. Args: data_dir: The directory where the data is located. There must be four files inside the rep: `counts.npz`, `author_indices.npy`, `author_map.txt`, and `vocabulary.txt`. batch_size: The batch size to use for training. random_state: A NumPy `RandomState` object, used to shuffle the data. counts_transformation: A string indicating how to transform the counts. One of "nothing", "binary", "log", or "sqrt". """ counts = sparse.load_npz(os.path.join(data_dir, "counts.npz")) num_documents, num_words = counts.shape author_indices = np.load( os.path.join(data_dir, "author_indices.npy")).astype(np.int32) num_authors = np.max(author_indices + 1) author_map = np.loadtxt(os.path.join(data_dir, "author_map.txt"), dtype=str, delimiter="\n", encoding='latin-1') # Shuffle data. documents = random_state.permutation(num_documents) shuffled_author_indices = author_indices[documents] shuffled_counts = counts[documents] # Apply counts transformation. if counts_transformation == "nothing": count_values = shuffled_counts.data elif counts_transformation == "binary": count_values = np.int32(shuffled_counts.data > 0) elif counts_transformation == "log": count_values = np.round(np.log(1 + shuffled_counts.data)) elif counts_transformation == "sqrt": count_values = np.round(np.sqrt(shuffled_counts.data)) else: raise ValueError("Unrecognized counts transformation.") # Store counts as sparse tensor so it occupies less memory. shuffled_counts = tf.SparseTensor( indices=np.array(shuffled_counts.nonzero()).T, values=count_values, dense_shape=shuffled_counts.shape) dataset = tf.data.Dataset.from_tensor_slices( (documents, shuffled_counts, shuffled_author_indices)) batches = dataset.repeat().batch(batch_size).prefetch(batch_size) iterator = batches.make_one_shot_iterator() vocabulary = np.loadtxt(os.path.join(data_dir, "vocabulary.txt"), dtype=str, delimiter="\n", comments="<!-") total_counts_per_author = np.bincount( author_indices, weights=np.array(np.sum(counts, axis=1)).flatten()) counts_per_document_per_author = ( total_counts_per_author / np.bincount(author_indices)) # Author weights is how much lengthy each author's opinion over average is. author_weights = (counts_per_document_per_author / np.mean(np.sum(counts, axis=1))).astype(np.float32) return (iterator, author_weights, vocabulary, author_map, num_documents, num_words, num_authors) def build_lognormal_variational_parameters(initial_document_loc, initial_objective_topic_loc, num_documents, num_words, num_topics): """ Build document and objective topic lognormal variational parameters. Args: initial_document_loc: A [num_documents, num_topics] NumPy array containing the initial document intensity means. initial_objective_topic_loc: A [num_topics, num_words] NumPy array containing the initial objective topic means. num_documents: Number of documents in the data set. num_words: Number of words in the data set. num_topics: Number of topics. Returns: document_loc: A Variable object with shape [num_documents, num_topics]. document_scale: A positive Variable object with shape [num_documents, num_topics]. objective_topic_loc: A Variable object with shape [num_topics, num_words]. objective_topic_scale: A positive Variable object with shape [num_topics, num_words]. """ document_loc = tf.get_variable( "document_loc", initializer=tf.constant(np.log(initial_document_loc))) objective_topic_loc = tf.get_variable( "objective_topic_loc", initializer=tf.constant(np.log(initial_objective_topic_loc))) document_scale_logit = tf.get_variable( "document_scale_logit", shape=[num_documents, num_topics], initializer=tf.initializers.random_normal(mean=0, stddev=1.), dtype=tf.float32) objective_topic_scale_logit = tf.get_variable( "objective_topic_scale_logit", shape=[num_topics, num_words], initializer=tf.initializers.random_normal(mean=0, stddev=1.), dtype=tf.float32) document_scale = tf.nn.softplus(document_scale_logit) objective_topic_scale = tf.nn.softplus(objective_topic_scale_logit) tf.summary.histogram("params/document_loc", document_loc) tf.summary.histogram("params/objective_topic_loc", objective_topic_loc) tf.summary.histogram("params/document_scale", document_scale) tf.summary.histogram("params/objective_topic_scale", objective_topic_scale) return (document_loc, document_scale, objective_topic_loc, objective_topic_scale) def print_topics(neutral_mean, negative_mean, positive_mean, vocabulary): """Get neutral and ideological topics to be used for Tensorboard. Args: neutral_mean: The mean of the neutral topics, a NumPy matrix with shape [num_topics, num_words]. negative_mean: The mean of the negative topics, a NumPy matrix with shape [num_topics, num_words]. positive_mean: The mean of the positive topics, a NumPy matrix with shape [num_topics, num_words]. vocabulary: A list of the vocabulary with shape [num_words]. Returns: topic_strings: A list of the negative, neutral, and positive topics. """ num_topics, num_words = neutral_mean.shape words_per_topic = 10 top_neutral_words = np.argsort(-neutral_mean, axis=1) top_negative_words = np.argsort(-negative_mean, axis=1) top_positive_words = np.argsort(-positive_mean, axis=1) topic_strings = [] for topic_idx in range(num_topics): neutral_start_string = "Neutral {}:".format(topic_idx) neutral_row = [vocabulary[word] for word in top_neutral_words[topic_idx, :words_per_topic]] neutral_row_string = ", ".join(neutral_row) neutral_string = " ".join([neutral_start_string, neutral_row_string]) positive_start_string = "Positive {}:".format(topic_idx) positive_row = [vocabulary[word] for word in top_positive_words[topic_idx, :words_per_topic]] positive_row_string = ", ".join(positive_row) positive_string = " ".join([positive_start_string, positive_row_string]) negative_start_string = "Negative {}:".format(topic_idx) negative_row = [vocabulary[word] for word in top_negative_words[topic_idx, :words_per_topic]] negative_row_string = ", ".join(negative_row) negative_string = " ".join([negative_start_string, negative_row_string]) topic_strings.append(" \n".join( [negative_string, neutral_string, positive_string])) return np.array(topic_strings) def print_ideal_points(ideal_point_loc, author_map): """Print ideal point ordering for Tensorboard.""" return ", ".join(author_map[np.argsort(ideal_point_loc)]) def get_log_prior(samples, prior): """Return log prior of sampled Gaussians. Args: samples: A `Tensor` with shape `[num_samples, :, :]`. prior: String representing prior distribution. Returns: log_prior: A `Tensor` with shape `[num_samples]`, with the log priors summed across latent dimensions. """ if prior == 'normal': prior_distribution = tfp.distributions.Normal(loc=0., scale=1.) elif prior == 'gamma': prior_distribution = tfp.distributions.Gamma(concentration=0.3, rate=0.3) log_prior = tf.reduce_sum(prior_distribution.log_prob(samples), axis=[1, 2]) return log_prior def get_elbo(counts, document_indices, author_indices, author_weights, document_distribution, objective_topic_distribution, ideological_topic_distribution, ideal_point_distribution, num_documents, batch_size, num_samples=1): """Approximate variational Lognormal ELBO using reparameterization. Args: counts: A matrix with shape `[batch_size, num_words]`. document_indices: An int-vector with shape `[batch_size]`. author_indices: An int-vector with shape `[batch_size]`. author_weights: A vector with shape `[num_authors]`, constituting how lengthy the opinion is above average. document_distribution: A positive `Distribution` object with parameter shape `[num_documents, num_topics]`. objective_topic_distribution: A positive `Distribution` object with parameter shape `[num_topics, num_words]`. ideological_topic_distribution: A positive `Distribution` object with parameter shape `[num_topics, num_words]`. ideal_point_distribution: A `Distribution` object over [0, 1] with parameter_shape `[num_authors]`. num_documents: The number of documents in the total data set (used to calculate log-likelihood scale). batch_size: Batch size (used to calculate log-likelihood scale). num_samples: Number of Monte-Carlo samples. Returns: elbo: A scalar representing a Monte-Carlo sample of the ELBO. This value is averaged across samples and summed across batches. """ document_samples = document_distribution.sample(num_samples) objective_topic_samples = objective_topic_distribution.sample(num_samples) ideological_topic_samples = ideological_topic_distribution.sample( num_samples) ideal_point_samples = ideal_point_distribution.sample(num_samples) _, num_topics, _ = objective_topic_samples.get_shape().as_list() ideal_point_log_prior = tfp.distributions.Normal( loc=0., scale=1.) ideal_point_log_prior = tf.reduce_sum( ideal_point_log_prior.log_prob(ideal_point_samples), axis=[1,2]) document_log_prior = get_log_prior(document_samples, 'gamma') objective_topic_log_prior = get_log_prior(objective_topic_samples, 'gamma') ideological_topic_log_prior = get_log_prior(ideological_topic_samples, 'normal') log_prior = (document_log_prior + objective_topic_log_prior + ideological_topic_log_prior + ideal_point_log_prior) selected_document_samples = tf.gather(document_samples, document_indices, axis=1) selected_ideal_points = tf.gather(ideal_point_samples, author_indices, axis=1) selected_ideological_topic_samples = tf.exp( # replace by a column selected_ideal_points[:, :, :, tf.newaxis] * ideological_topic_samples[:, tf.newaxis, :, :]) # Normalize by how lengthy the author's opinion is. selected_author_weights = tf.gather(author_weights, author_indices) selected_ideological_topic_samples = ( selected_author_weights[tf.newaxis, :, tf.newaxis, tf.newaxis] * selected_ideological_topic_samples) document_entropy = -tf.reduce_sum( document_distribution.log_prob(document_samples), axis=[1, 2]) objective_topic_entropy = -tf.reduce_sum( objective_topic_distribution.log_prob(objective_topic_samples), axis=[1, 2]) ideological_topic_entropy = -tf.reduce_sum( ideological_topic_distribution.log_prob(ideological_topic_samples), axis=[1, 2]) ideal_point_entropy = -tf.reduce_sum( ideal_point_distribution.log_prob(ideal_point_samples), axis=1) entropy = (document_entropy + objective_topic_entropy + ideological_topic_entropy + ideal_point_entropy) rate = tf.reduce_sum( selected_document_samples[:, :, :, tf.newaxis] * objective_topic_samples[:, tf.newaxis, :, :] * selected_ideological_topic_samples[:, :, :, :], axis=2) count_distribution = tfp.distributions.Poisson(rate=rate) # Need to un-sparsify the counts to evaluate log-likelihood. count_log_likelihood = count_distribution.log_prob( tf.sparse.to_dense(counts)) count_log_likelihood = tf.reduce_sum(count_log_likelihood, axis=[1, 2]) # Adjust for the fact that we're only using a minibatch. count_log_likelihood = count_log_likelihood * (num_documents / batch_size) elbo = log_prior + count_log_likelihood + entropy elbo = tf.reduce_mean(elbo) tf.summary.scalar("elbo/elbo", elbo) tf.summary.scalar("elbo/log_prior", tf.reduce_mean(log_prior)) tf.summary.scalar("elbo/count_log_likelihood", tf.reduce_mean(count_log_likelihood)) tf.summary.scalar("elbo/entropy", tf.reduce_mean(entropy)) return elbo if __name__ == "__main__": tf.app.run()
[ 37811, 20238, 7306, 2173, 351, 262, 2420, 12, 3106, 7306, 966, 2746, 357, 51, 47772, 737, 198, 198, 5756, 331, 23330, 67, 85, 92, 42685, 262, 9853, 286, 1573, 410, 287, 3188, 288, 13, 3914, 2124, 62, 67, 3522, 284, 262, 220, 198, ...
2.44067
6,801
from tkinter import * from tax_profiler import TaxProfile from tkinter import messagebox as mb if __name__ == '__main__': main()
[ 6738, 256, 74, 3849, 1330, 1635, 198, 6738, 1687, 62, 5577, 5329, 1330, 9241, 37046, 198, 6738, 256, 74, 3849, 1330, 3275, 3524, 355, 285, 65, 628, 628, 198, 361, 11593, 3672, 834, 6624, 705, 834, 12417, 834, 10354, 198, 220, 220, 2...
2.978261
46
from django.contrib import admin from lms_app.models import Professor admin.site.register(Professor) # Register your models here.
[ 6738, 42625, 14208, 13, 3642, 822, 1330, 13169, 201, 198, 6738, 300, 907, 62, 1324, 13, 27530, 1330, 8129, 201, 198, 28482, 13, 15654, 13, 30238, 7, 25031, 8, 201, 198, 201, 198, 2, 17296, 534, 4981, 994, 13, 201, 198 ]
3.317073
41
#!/usr/bin/env python3 from .document import Document from intervaltree import Interval,IntervalTree # from .annotationGraph import AnnotationGraph import logging logger = logging.getLogger(__name__)
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 198, 6738, 764, 22897, 1330, 16854, 198, 6738, 16654, 21048, 1330, 4225, 2100, 11, 9492, 2100, 27660, 198, 2, 422, 764, 1236, 14221, 37065, 1330, 1052, 38983, 37065, 198, 11748, 1893...
3.607143
56
from django.apps import AppConfig
[ 6738, 42625, 14208, 13, 18211, 1330, 2034, 16934, 628 ]
3.888889
9
#Ex004b algo = (input('\033[34m''Digite algo: ''\033[m')) print('So letras ou palavras?: \033[33m{}\033[m'.format(algo.isalpha())) print('Est em maisculo?: \033[34m{}\033[m'.format(algo.isupper())) print('Est em minsculo?: \033[35m{}\033[m'.format(algo.islower())) print('Est captalizada?: \033[36m{}\033[m'.format(algo.istitle())) print('S tem espao?: \033[31m{}\033[m'.format(algo.isspace())) print(' numrico?: \033[32m{}\033[m'.format(algo.isnumeric())) print('xD')
[ 2, 3109, 22914, 65, 198, 282, 2188, 796, 357, 15414, 10786, 59, 44427, 58, 2682, 76, 7061, 19511, 578, 435, 2188, 25, 10148, 59, 44427, 58, 76, 6, 4008, 198, 4798, 10786, 2396, 1309, 8847, 267, 84, 6340, 615, 8847, 27514, 3467, 4442...
2.222749
211
import os import sonic_platform # Constants ==================================================================== PDDF_SUPPORT_FILE = '/usr/share/sonic/platform/pddf_support' # Helper classs
[ 198, 11748, 28686, 198, 11748, 36220, 62, 24254, 198, 198, 2, 4757, 1187, 38093, 18604, 198, 5760, 8068, 62, 40331, 15490, 62, 25664, 796, 31051, 14629, 14, 20077, 14, 1559, 291, 14, 24254, 14, 79, 1860, 69, 62, 11284, 6, 198, 198, ...
4.041667
48
from django.test import TestCase, RequestFactory from .mixins import TwoUserMixin, ProposalGroupMixin, ViewMixin from django.utils import timezone from consensus_engine.views import CreateProposalView from consensus_engine.forms import ProposalForm from consensus_engine.models import Proposal from django.core.exceptions import PermissionDenied
[ 6738, 42625, 14208, 13, 9288, 1330, 6208, 20448, 11, 19390, 22810, 198, 6738, 764, 19816, 1040, 1330, 4930, 12982, 35608, 259, 11, 1041, 40007, 13247, 35608, 259, 11, 3582, 35608, 259, 198, 6738, 42625, 14208, 13, 26791, 1330, 640, 11340,...
3.910112
89
from JDI.web.selenium.elements.composite.web_site import WebSite from tests.jdi_uitests_webtests.main.page_objects.w3c_site.frame_page import FramePage
[ 6738, 449, 17931, 13, 12384, 13, 741, 47477, 13, 68, 3639, 13, 785, 1930, 578, 13, 12384, 62, 15654, 1330, 5313, 29123, 198, 6738, 5254, 13, 73, 10989, 62, 5013, 3558, 62, 12384, 41989, 13, 12417, 13, 7700, 62, 48205, 13, 86, 18, ...
2.833333
54
''' @author: oluiscabral ''' import unittest from creationals.scraper_factory import ScraperFactory from helpers.webdriver_factory import WebdriverFactory from actioners.login_control import LoginControl from ui.login_ui import LoginUI from data_structure.data_ref import DataRef if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()
[ 7061, 6, 198, 31, 9800, 25, 267, 2290, 2304, 397, 1373, 198, 7061, 6, 198, 11748, 555, 715, 395, 198, 6738, 1126, 864, 82, 13, 1416, 38545, 62, 69, 9548, 1330, 1446, 38545, 22810, 198, 6738, 49385, 13, 12384, 26230, 62, 69, 9548, ...
3.008
125
import copy import logging import numpy as np import six import tensorflow as tf from functools import wraps from contextlib import contextmanager from .backend_base import BackendBase, FunctionBase, DeviceDecorator try: from tensorflow.contrib.distributions import fill_triangular except: print("Cannot find fill_triangular")
[ 11748, 4866, 198, 11748, 18931, 198, 11748, 299, 32152, 355, 45941, 198, 11748, 2237, 198, 11748, 11192, 273, 11125, 355, 48700, 198, 6738, 1257, 310, 10141, 1330, 27521, 198, 6738, 4732, 8019, 1330, 4732, 37153, 198, 198, 6738, 764, 1891...
3.5
96
from __future__ import absolute_import, division, print_function import cv2 import pandas as pd import numpy as np import six import ubelt as ub from six.moves import zip_longest from os.path import join, dirname import warnings def multi_plot(xdata=None, ydata=[], **kwargs): r""" plots multiple lines, bars, etc... This is the big function that implements almost all of the heavy lifting in this file. Any function not using this should probably find a way to use it. It is pretty general and relatively clean. Args: xdata (ndarray): can also be a list of arrays ydata (list or dict of ndarrays): can also be a single array **kwargs: Misc: fnum, pnum, use_legend, legend_loc Labels: xlabel, ylabel, title, figtitle ticksize, titlesize, legendsize, labelsize Grid: gridlinewidth, gridlinestyle Ticks: num_xticks, num_yticks, tickwidth, ticklength, ticksize Data: xmin, xmax, ymin, ymax, spread_list # can append _list to any of these # these can be dictionaries if ydata was also a dict plot_kw_keys = ['label', 'color', 'marker', 'markersize', 'markeredgewidth', 'linewidth', 'linestyle'] any plot_kw key can be a scalar (corresponding to all ydatas), a list if ydata was specified as a list, or a dict if ydata was specified as a dict. kind = ['bar', 'plot', ...] if kind='plot': spread if kind='bar': stacked, width References: matplotlib.org/examples/api/barchart_demo.html CommandLine: python -m netharn.util.mplutil multi_plot:0 --show python -m netharn.util.mplutil multi_plot:1 --show Example: >>> autompl() >>> xdata = [1, 2, 3, 4, 5] >>> ydata_list = [[1, 2, 3, 4, 5], [3, 3, 3, 3, 3], [5, 4, np.nan, 2, 1], [4, 3, np.nan, 1, 0]] >>> kwargs = {'label': ['spam', 'eggs', 'jam', 'pram'], 'linestyle': '-'} >>> #fig = multi_plot(xdata, ydata_list, title='$\phi_1(\\vec{x})$', xlabel='\nfds', **kwargs) >>> fig = multi_plot(xdata, ydata_list, title='', xlabel='\nfds', **kwargs) >>> show_if_requested() Example: >>> autompl() >>> fig1 = multi_plot([1, 2, 3], [4, 5, 6]) >>> fig2 = multi_plot([1, 2, 3], [4, 5, 6], fnum=4) >>> show_if_requested() """ import matplotlib as mpl from matplotlib import pyplot as plt ydata_list = ydata if isinstance(ydata_list, dict): # Special case where ydata is a dictionary if isinstance(xdata, six.string_types): # Special-er case where xdata is specified in ydata xkey = xdata ykeys = set(ydata_list.keys()) - {xkey} xdata = ydata_list[xkey] else: ykeys = list(ydata_list.keys()) # Normalize input ydata_list = list(ub.take(ydata_list, ykeys)) kwargs['label_list'] = kwargs.get('label_list', ykeys) else: ykeys = None # allow ydata_list to be passed without a container if is_list_of_scalars(ydata_list): ydata_list = [np.array(ydata_list)] if xdata is None: xdata = list(range(len(ydata_list[0]))) num_lines = len(ydata_list) # Transform xdata into xdata_list if is_list_of_lists(xdata): xdata_list = [np.array(xd, copy=True) for xd in xdata] else: xdata_list = [np.array(xdata, copy=True)] * num_lines fnum = ensure_fnum(kwargs.get('fnum', None)) pnum = kwargs.get('pnum', None) kind = kwargs.get('kind', 'plot') transpose = kwargs.get('transpose', False) def parsekw_list(key, kwargs, num_lines=num_lines, ykeys=ykeys): """ copies relevant plot commands into plot_list_kw """ if key in kwargs: val_list = kwargs[key] elif key + '_list' in kwargs: warnings.warn('*_list is depricated, just use kwarg {}'.format(key)) val_list = kwargs[key + '_list'] elif key + 's' in kwargs: # hack, multiple ways to do something warnings.warn('*s depricated, just use kwarg {}'.format(key)) val_list = kwargs[key + 's'] else: val_list = None if val_list is not None: if isinstance(val_list, dict): if ykeys is None: raise ValueError('ydata is not a dict, but a property was.') else: val_list = [val_list[key] for key in ykeys] if not isinstance(val_list, list): val_list = [val_list] * num_lines return val_list # Parse out arguments to ax.plot plot_kw_keys = ['label', 'color', 'marker', 'markersize', 'markeredgewidth', 'linewidth', 'linestyle', 'alpha'] # hackish / extra args that dont go to plot, but help extra_plot_kw_keys = ['spread_alpha', 'autolabel', 'edgecolor', 'fill'] plot_kw_keys += extra_plot_kw_keys plot_ks_vals = [parsekw_list(key, kwargs) for key in plot_kw_keys] plot_list_kw = dict([ (key, vals) for key, vals in zip(plot_kw_keys, plot_ks_vals) if vals is not None ]) if 'color' not in plot_list_kw: plot_list_kw['color'] = distinct_colors(num_lines) if kind == 'plot': if 'marker' not in plot_list_kw: plot_list_kw['marker'] = distinct_markers(num_lines) if 'spread_alpha' not in plot_list_kw: plot_list_kw['spread_alpha'] = [.2] * num_lines if kind == 'bar': # Remove non-bar kwargs for key in ['markeredgewidth', 'linewidth', 'marker', 'markersize', 'linestyle']: plot_list_kw.pop(key, None) stacked = kwargs.get('stacked', False) width_key = 'height' if transpose else 'width' if 'width_list' in kwargs: plot_list_kw[width_key] = kwargs['width_list'] else: width = kwargs.get('width', .9) # if width is None: # # HACK: need variable width # # width = np.mean(np.diff(xdata_list[0])) # width = .9 if not stacked: width /= num_lines #plot_list_kw['orientation'] = ['horizontal'] * num_lines plot_list_kw[width_key] = [width] * num_lines spread_list = kwargs.get('spread_list', None) if spread_list is None: pass # nest into a list of dicts for each line in the multiplot valid_keys = list(set(plot_list_kw.keys()) - set(extra_plot_kw_keys)) valid_vals = list(ub.dict_take(plot_list_kw, valid_keys)) plot_kw_list = [dict(zip(valid_keys, vals)) for vals in zip(*valid_vals)] extra_kw_keys = [key for key in extra_plot_kw_keys if key in plot_list_kw] extra_kw_vals = list(ub.dict_take(plot_list_kw, extra_kw_keys)) extra_kw_list = [dict(zip(extra_kw_keys, vals)) for vals in zip(*extra_kw_vals)] # Get passed in axes or setup a new figure ax = kwargs.get('ax', None) if ax is None: doclf = kwargs.get('doclf', False) fig = figure(fnum=fnum, pnum=pnum, docla=False, doclf=doclf) ax = plt.gca() else: plt.sca(ax) fig = ax.figure # +--------------- # Draw plot lines ydata_list = np.array(ydata_list) if transpose: if kind == 'bar': plot_func = ax.barh elif kind == 'plot': else: plot_func = getattr(ax, kind) # usually ax.plot assert len(ydata_list) > 0, 'no ydata' #assert len(extra_kw_list) == len(plot_kw_list), 'bad length' #assert len(extra_kw_list) == len(ydata_list), 'bad length' _iter = enumerate(zip_longest(xdata_list, ydata_list, plot_kw_list, extra_kw_list)) for count, (_xdata, _ydata, plot_kw, extra_kw) in _iter: ymask = np.isfinite(_ydata) ydata_ = _ydata.compress(ymask) xdata_ = _xdata.compress(ymask) if kind == 'bar': if stacked: # Plot bars on top of each other xdata_ = xdata_ else: # Plot bars side by side baseoffset = (width * num_lines) / 2 lineoffset = (width * count) offset = baseoffset - lineoffset # Fixeme for more histogram bars xdata_ = xdata_ - offset # width_key = 'height' if transpose else 'width' # plot_kw[width_key] = np.diff(xdata) objs = plot_func(xdata_, ydata_, **plot_kw) if kind == 'bar': if extra_kw is not None and 'edgecolor' in extra_kw: for rect in objs: rect.set_edgecolor(extra_kw['edgecolor']) if extra_kw is not None and extra_kw.get('autolabel', False): # FIXME: probably a more cannonical way to include bar # autolabeling with tranpose support, but this is a hack that # works for now for rect in objs: if transpose: numlbl = width = rect.get_width() xpos = width + ((_xdata.max() - _xdata.min()) * .005) ypos = rect.get_y() + rect.get_height() / 2. ha, va = 'left', 'center' else: numlbl = height = rect.get_height() xpos = rect.get_x() + rect.get_width() / 2. ypos = 1.05 * height ha, va = 'center', 'bottom' barlbl = '%.3f' % (numlbl,) ax.text(xpos, ypos, barlbl, ha=ha, va=va) # print('extra_kw = %r' % (extra_kw,)) if kind == 'plot' and extra_kw.get('fill', False): ax.fill_between(_xdata, ydata_, alpha=plot_kw.get('alpha', 1.0), color=plot_kw.get('color', None)) # , zorder=0) if spread_list is not None: # Plots a spread around plot lines usually indicating standard # deviation _xdata = np.array(_xdata) spread = spread_list[count] ydata_ave = np.array(ydata_) y_data_dev = np.array(spread) y_data_max = ydata_ave + y_data_dev y_data_min = ydata_ave - y_data_dev ax = plt.gca() spread_alpha = extra_kw['spread_alpha'] ax.fill_between(_xdata, y_data_min, y_data_max, alpha=spread_alpha, color=plot_kw.get('color', None)) # , zorder=0) # L________________ #max_y = max(np.max(y_data), max_y) #min_y = np.min(y_data) if min_y is None else min(np.min(y_data), min_y) ydata = _ydata # HACK xdata = _xdata # HACK if transpose: #xdata_list = ydata_list ydata = xdata # Hack / Fix any transpose issues kwargs = {transpose_key(key): val for key, val in kwargs.items()} # Setup axes labeling title = kwargs.get('title', None) xlabel = kwargs.get('xlabel', '') ylabel = kwargs.get('ylabel', '') xlabel = none_or_unicode(xlabel) ylabel = none_or_unicode(ylabel) title = none_or_unicode(title) # Initial integration with mpl rcParams standards mplrc = mpl.rcParams.copy() mplrc.update({ # 'legend.fontsize': custom_figure.LEGEND_SIZE, # 'axes.titlesize': custom_figure.TITLE_SIZE, # 'axes.labelsize': custom_figure.LABEL_SIZE, # 'legend.facecolor': 'w', # 'font.family': 'sans-serif', # 'xtick.labelsize': custom_figure.TICK_SIZE, # 'ytick.labelsize': custom_figure.TICK_SIZE, }) mplrc.update(kwargs.get('rcParams', {})) titlesize = kwargs.get('titlesize', mplrc['axes.titlesize']) labelsize = kwargs.get('labelsize', mplrc['axes.labelsize']) legendsize = kwargs.get('legendsize', mplrc['legend.fontsize']) xticksize = kwargs.get('ticksize', mplrc['xtick.labelsize']) yticksize = kwargs.get('ticksize', mplrc['ytick.labelsize']) family = kwargs.get('fontfamily', mplrc['font.family']) tickformat = kwargs.get('tickformat', None) ytickformat = kwargs.get('ytickformat', tickformat) xtickformat = kwargs.get('xtickformat', tickformat) # 'DejaVu Sans','Verdana', 'Arial' weight = kwargs.get('fontweight', None) if weight is None: weight = 'normal' labelkw = { 'fontproperties': mpl.font_manager.FontProperties( weight=weight, family=family, size=labelsize) } ax.set_xlabel(xlabel, **labelkw) ax.set_ylabel(ylabel, **labelkw) tick_fontprop = mpl.font_manager.FontProperties(family=family, weight=weight) if tick_fontprop is not None: for ticklabel in ax.get_xticklabels(): ticklabel.set_fontproperties(tick_fontprop) for ticklabel in ax.get_yticklabels(): ticklabel.set_fontproperties(tick_fontprop) if xticksize is not None: for ticklabel in ax.get_xticklabels(): ticklabel.set_fontsize(xticksize) if yticksize is not None: for ticklabel in ax.get_yticklabels(): ticklabel.set_fontsize(yticksize) if xtickformat is not None: # mpl.ticker.StrMethodFormatter # newstyle # mpl.ticker.FormatStrFormatter # oldstyle ax.xaxis.set_major_formatter(mpl.ticker.FormatStrFormatter(xtickformat)) if ytickformat is not None: ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter(ytickformat)) xtick_kw = ytick_kw = { 'width': kwargs.get('tickwidth', None), 'length': kwargs.get('ticklength', None), } xtick_kw = {k: v for k, v in xtick_kw.items() if v is not None} ytick_kw = {k: v for k, v in ytick_kw.items() if v is not None} ax.xaxis.set_tick_params(**xtick_kw) ax.yaxis.set_tick_params(**ytick_kw) #ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%d')) # Setup axes limits if 'xlim' in kwargs: xlim = kwargs['xlim'] if xlim is not None: if 'xmin' not in kwargs and 'xmax' not in kwargs: kwargs['xmin'] = xlim[0] kwargs['xmax'] = xlim[1] else: raise ValueError('use xmax, xmin instead of xlim') if 'ylim' in kwargs: ylim = kwargs['ylim'] if ylim is not None: if 'ymin' not in kwargs and 'ymax' not in kwargs: kwargs['ymin'] = ylim[0] kwargs['ymax'] = ylim[1] else: raise ValueError('use ymax, ymin instead of ylim') xmin = kwargs.get('xmin', ax.get_xlim()[0]) xmax = kwargs.get('xmax', ax.get_xlim()[1]) ymin = kwargs.get('ymin', ax.get_ylim()[0]) ymax = kwargs.get('ymax', ax.get_ylim()[1]) text_type = six.text_type if text_type(xmax) == 'data': xmax = max([xd.max() for xd in xdata_list]) if text_type(xmin) == 'data': xmin = min([xd.min() for xd in xdata_list]) # Setup axes ticks num_xticks = kwargs.get('num_xticks', None) num_yticks = kwargs.get('num_yticks', None) if num_xticks is not None: # TODO check if xdata is integral if xdata.dtype.kind == 'i': xticks = np.linspace(np.ceil(xmin), np.floor(xmax), num_xticks).astype(np.int32) else: xticks = np.linspace((xmin), (xmax), num_xticks) ax.set_xticks(xticks) if num_yticks is not None: if ydata.dtype.kind == 'i': yticks = np.linspace(np.ceil(ymin), np.floor(ymax), num_yticks).astype(np.int32) else: yticks = np.linspace((ymin), (ymax), num_yticks) ax.set_yticks(yticks) force_xticks = kwargs.get('force_xticks', None) if force_xticks is not None: xticks = np.array(sorted(ax.get_xticks().tolist() + force_xticks)) ax.set_xticks(xticks) yticklabels = kwargs.get('yticklabels', None) if yticklabels is not None: # Hack ONLY WORKS WHEN TRANSPOSE = True # Overrides num_yticks ax.set_yticks(ydata) ax.set_yticklabels(yticklabels) xticklabels = kwargs.get('xticklabels', None) if xticklabels is not None: # Overrides num_xticks ax.set_xticks(xdata) ax.set_xticklabels(xticklabels) xtick_rotation = kwargs.get('xtick_rotation', None) if xtick_rotation is not None: [lbl.set_rotation(xtick_rotation) for lbl in ax.get_xticklabels()] ytick_rotation = kwargs.get('ytick_rotation', None) if ytick_rotation is not None: [lbl.set_rotation(ytick_rotation) for lbl in ax.get_yticklabels()] # Axis padding xpad = kwargs.get('xpad', None) ypad = kwargs.get('ypad', None) xpad_factor = kwargs.get('xpad_factor', None) ypad_factor = kwargs.get('ypad_factor', None) if xpad is None and xpad_factor is not None: xpad = (xmax - xmin) * xpad_factor if ypad is None and ypad_factor is not None: ypad = (ymax - ymin) * ypad_factor xpad = 0 if xpad is None else xpad ypad = 0 if ypad is None else ypad ypad_high = kwargs.get('ypad_high', ypad) ypad_low = kwargs.get('ypad_low', ypad) xpad_high = kwargs.get('xpad_high', xpad) xpad_low = kwargs.get('xpad_low', xpad) xmin, xmax = (xmin - xpad_low), (xmax + xpad_high) ymin, ymax = (ymin - ypad_low), (ymax + ypad_high) ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) xscale = kwargs.get('xscale', None) yscale = kwargs.get('yscale', None) if yscale is not None: ax.set_yscale(yscale) if xscale is not None: ax.set_xscale(xscale) gridlinestyle = kwargs.get('gridlinestyle', None) gridlinewidth = kwargs.get('gridlinewidth', None) gridlines = ax.get_xgridlines() + ax.get_ygridlines() if gridlinestyle: for line in gridlines: line.set_linestyle(gridlinestyle) if gridlinewidth: for line in gridlines: line.set_linewidth(gridlinewidth) # Setup title if title is not None: titlekw = { 'fontproperties': mpl.font_manager.FontProperties( family=family, weight=weight, size=titlesize) } ax.set_title(title, **titlekw) use_legend = kwargs.get('use_legend', 'label' in valid_keys) legend_loc = kwargs.get('legend_loc', 'best') legend_alpha = kwargs.get('legend_alpha', 1.0) if use_legend: legendkw = { 'alpha': legend_alpha, 'fontproperties': mpl.font_manager.FontProperties( family=family, weight=weight, size=legendsize) } legend(loc=legend_loc, ax=ax, **legendkw) figtitle = kwargs.get('figtitle', None) if figtitle is not None: set_figtitle(figtitle, fontfamily=family, fontweight=weight, size=kwargs.get('figtitlesize')) use_darkbackground = kwargs.get('use_darkbackground', None) lightbg = kwargs.get('lightbg', None) if lightbg is None: lightbg = True if use_darkbackground is None: use_darkbackground = not lightbg if use_darkbackground: _dark_background(force=use_darkbackground is True) # TODO: return better info return fig def figure(fnum=None, pnum=(1, 1, 1), title=None, figtitle=None, doclf=False, docla=False, projection=None, **kwargs): """ http://matplotlib.org/users/gridspec.html Args: fnum (int): fignum = figure number pnum (int, str, or tuple(int, int, int)): plotnum = plot tuple title (str): (default = None) figtitle (None): (default = None) docla (bool): (default = False) doclf (bool): (default = False) Returns: mpl.Figure: fig CommandLine: python -m netharn.util.mplutil figure:0 --show Example: >>> autompl() >>> import matplotlib.pyplot as plt >>> fnum = 1 >>> fig = figure(fnum, (2, 2, 1)) >>> plt.gca().text(0.5, 0.5, "ax1", va="center", ha="center") >>> fig = figure(fnum, (2, 2, 2)) >>> plt.gca().text(0.5, 0.5, "ax2", va="center", ha="center") >>> show_if_requested() Example: >>> autompl() >>> import matplotlib.pyplot as plt >>> fnum = 1 >>> fig = figure(fnum, (2, 2, 1)) >>> plt.gca().text(0.5, 0.5, "ax1", va="center", ha="center") >>> fig = figure(fnum, (2, 2, 2)) >>> plt.gca().text(0.5, 0.5, "ax2", va="center", ha="center") >>> fig = figure(fnum, (2, 4, (1, slice(1, None)))) >>> plt.gca().text(0.5, 0.5, "ax3", va="center", ha="center") >>> show_if_requested() """ import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = ensure_fig(fnum) if doclf: fig.clf() if pnum is not None: _setup_subfigure(pnum) # Set the title / figtitle if title is not None: ax = plt.gca() ax.set_title(title) if figtitle is not None: fig.suptitle(figtitle) return fig def adjust_subplots(left=None, right=None, bottom=None, top=None, wspace=None, hspace=None, fig=None): """ Kwargs: left (float): left side of the subplots of the figure right (float): right side of the subplots of the figure bottom (float): bottom of the subplots of the figure top (float): top of the subplots of the figure wspace (float): width reserved for blank space between subplots hspace (float): height reserved for blank space between subplots """ from matplotlib import pyplot as plt kwargs = dict(left=left, right=right, bottom=bottom, top=top, wspace=wspace, hspace=hspace) kwargs = {k: v for k, v in kwargs.items() if v is not None} if fig is None: fig = plt.gcf() subplotpars = fig.subplotpars adjust_dict = subplotpars.__dict__.copy() del adjust_dict['validate'] adjust_dict.update(kwargs) fig.subplots_adjust(**adjust_dict) def savefig2(fig, fpath, **kwargs): """ Does a tight layout and saves the figure with transparency """ import matplotlib as mpl if 'transparent' not in kwargs: kwargs['transparent'] = True if 'extent' not in kwargs: axes_extents = extract_axes_extents(fig) extent = mpl.transforms.Bbox.union(axes_extents) kwargs['extent'] = extent fig.savefig(fpath, **kwargs) def copy_figure_to_clipboard(fig): """ References: https://stackoverflow.com/questions/17676373/python-matplotlib-pyqt-copy-image-to-clipboard """ print('Copying figure %d to the clipboard' % fig.number) import matplotlib as mpl app = mpl.backends.backend_qt5.qApp QtGui = mpl.backends.backend_qt5.QtGui im_bgra = render_figure_to_image(fig, transparent=True) im_rgba = cv2.cvtColor(im_bgra, cv2.COLOR_BGRA2RGBA) im = im_rgba QImage = QtGui.QImage qim = QImage(im.data, im.shape[1], im.shape[0], im.strides[0], QImage.Format_RGBA8888) clipboard = app.clipboard() clipboard.setImage(qim) # size = fig.canvas.size() # width, height = size.width(), size.height() # qim = QtGui.QImage(fig.canvas.buffer_rgba(), width, height, QtGui.QImage.Format_ARGB32) # QtWidgets = mpl.backends.backend_qt5.QtWidgets # pixmap = QtWidgets.QWidget.grab(fig.canvas) # clipboard.setPixmap(pixmap) def dict_intersection(dict1, dict2): r""" Args: dict1 (dict): dict2 (dict): Returns: dict: mergedict_ CommandLine: python -m utool.util_dict --exec-dict_intersection Example: >>> # ENABLE_DOCTEST >>> dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> dict2 = {'b': 2, 'c': 3, 'd': 5, 'e': 21, 'f': 42} >>> mergedict_ = dict_intersection(dict1, dict2) >>> print(ub.repr2(mergedict_, nl=0)) {'b': 2, 'c': 3} """ isect_keys = set(dict1.keys()).intersection(set(dict2.keys())) # maintain order if possible if isinstance(dict1, ub.odict): isect_keys_ = [k for k in dict1.keys() if k in isect_keys] _dict_cls = ub.odict else: isect_keys_ = isect_keys _dict_cls = dict dict_isect = _dict_cls( (k, dict1[k]) for k in isect_keys_ if dict1[k] == dict2[k] ) return dict_isect def _dark_background(ax=None, doubleit=False, force=False): r""" Args: ax (None): (default = None) doubleit (bool): (default = False) CommandLine: python -m .draw_func2 --exec-_dark_background --show Example: >>> # ENABLE_DOCTEST >>> autompl() >>> fig = figure() >>> _dark_background() >>> show_if_requested() """ import matplotlib as mpl from matplotlib import pyplot as plt if force: from mpl_toolkits.mplot3d import Axes3D BLACK = np.array(( 0, 0, 0, 255)) / 255.0 # Should use mpl style dark background instead bgcolor = BLACK * .9 if ax is None: ax = plt.gca() if isinstance(ax, Axes3D): ax.set_axis_bgcolor(bgcolor) ax.tick_params(colors='white') return xy, width, height = _get_axis_xy_width_height(ax) if doubleit: halfw = (doubleit) * (width / 2) halfh = (doubleit) * (height / 2) xy = (xy[0] - halfw, xy[1] - halfh) width *= (doubleit + 1) height *= (doubleit + 1) rect = mpl.patches.Rectangle(xy, width, height, lw=0, zorder=0) rect.set_clip_on(True) rect.set_fill(True) rect.set_color(bgcolor) rect.set_zorder(-99999999999) rect = ax.add_patch(rect) def _get_axis_xy_width_height(ax=None, xaug=0, yaug=0, waug=0, haug=0): """ gets geometry of a subplot """ from matplotlib import pyplot as plt if ax is None: ax = plt.gca() autoAxis = ax.axis() xy = (autoAxis[0] + xaug, autoAxis[2] + yaug) width = (autoAxis[1] - autoAxis[0]) + waug height = (autoAxis[3] - autoAxis[2]) + haug return xy, width, height _LEGEND_LOCATION = { 'upper right': 1, 'upper left': 2, 'lower left': 3, 'lower right': 4, 'right': 5, 'center left': 6, 'center right': 7, 'lower center': 8, 'upper center': 9, 'center': 10, } def set_figtitle(figtitle, subtitle='', forcefignum=True, incanvas=True, size=None, fontfamily=None, fontweight=None, fig=None): r""" Args: figtitle (?): subtitle (str): (default = '') forcefignum (bool): (default = True) incanvas (bool): (default = True) fontfamily (None): (default = None) fontweight (None): (default = None) size (None): (default = None) fig (None): (default = None) CommandLine: python -m .custom_figure set_figtitle --show Example: >>> # DISABLE_DOCTEST >>> autompl() >>> fig = figure(fnum=1, doclf=True) >>> result = set_figtitle(figtitle='figtitle', fig=fig) >>> # xdoc: +REQUIRES(--show) >>> show_if_requested() """ from matplotlib import pyplot as plt if figtitle is None: figtitle = '' if fig is None: fig = plt.gcf() figtitle = ub.ensure_unicode(figtitle) subtitle = ub.ensure_unicode(subtitle) if incanvas: if subtitle != '': subtitle = '\n' + subtitle prop = { 'family': fontfamily, 'weight': fontweight, 'size': size, } prop = {k: v for k, v in prop.items() if v is not None} sup = fig.suptitle(figtitle + subtitle) if prop: fontproperties = sup.get_fontproperties().copy() for key, val in prop.items(): getattr(fontproperties, 'set_' + key)(val) sup.set_fontproperties(fontproperties) # fontproperties = mpl.font_manager.FontProperties(**prop) else: fig.suptitle('') # Set title in the window window_figtitle = ('fig(%d) ' % fig.number) + figtitle window_figtitle = window_figtitle.replace('\n', ' ') fig.canvas.set_window_title(window_figtitle) def legend(loc='best', fontproperties=None, size=None, fc='w', alpha=1, ax=None, handles=None): r""" Args: loc (str): (default = 'best') fontproperties (None): (default = None) size (None): (default = None) Ignore: >>> # ENABLE_DOCTEST >>> autompl() >>> loc = 'best' >>> xdata = np.linspace(-6, 6) >>> ydata = np.sin(xdata) >>> plt.plot(xdata, ydata, label='sin') >>> fontproperties = None >>> size = None >>> result = legend(loc, fontproperties, size) >>> print(result) >>> show_if_requested() """ from matplotlib import pyplot as plt assert loc in _LEGEND_LOCATION or loc == 'best', ( 'invalid loc. try one of %r' % (_LEGEND_LOCATION,)) if ax is None: ax = plt.gca() if fontproperties is None: prop = {} if size is not None: prop['size'] = size # prop['weight'] = 'normal' # prop['family'] = 'sans-serif' else: prop = fontproperties legendkw = dict(loc=loc) if prop: legendkw['prop'] = prop if handles is not None: legendkw['handles'] = handles legend = ax.legend(**legendkw) if legend: legend.get_frame().set_fc(fc) legend.get_frame().set_alpha(alpha) def distinct_colors(N, brightness=.878, randomize=True, hue_range=(0.0, 1.0), cmap_seed=None): r""" Args: N (int): brightness (float): Returns: list: RGB_tuples CommandLine: python -m color_funcs --test-distinct_colors --N 2 --show --hue-range=0.05,.95 python -m color_funcs --test-distinct_colors --N 3 --show --hue-range=0.05,.95 python -m color_funcs --test-distinct_colors --N 4 --show --hue-range=0.05,.95 python -m .color_funcs --test-distinct_colors --N 3 --show --no-randomize python -m .color_funcs --test-distinct_colors --N 4 --show --no-randomize python -m .color_funcs --test-distinct_colors --N 6 --show --no-randomize python -m .color_funcs --test-distinct_colors --N 20 --show References: http://blog.jianhuashao.com/2011/09/generate-n-distinct-colors.html CommandLine: python -m .color_funcs --exec-distinct_colors --show python -m .color_funcs --exec-distinct_colors --show --no-randomize --N 50 python -m .color_funcs --exec-distinct_colors --show --cmap_seed=foobar Ignore: >>> # build test data >>> autompl() >>> N = ub.smartcast(ub.get_argval('--N', default=2), int) # FIXME >>> randomize = not ub.argflag('--no-randomize') >>> brightness = 0.878 >>> # execute function >>> cmap_seed = ub.get_argval('--cmap_seed', default=None) >>> hue_range = ub.smartcast(ub.get_argval('--hue-range', default=(0.00, 1.0)), list) #FIXME >>> RGB_tuples = distinct_colors(N, brightness, randomize, hue_range, cmap_seed=cmap_seed) >>> # verify results >>> assert len(RGB_tuples) == N >>> result = str(RGB_tuples) >>> print(result) >>> # xdoctest: +REQUIRES(--show) >>> color_list = RGB_tuples >>> testshow_colors(color_list) >>> show_if_requested() """ # TODO: Add sin wave modulation to the sat and value # HACK for white figures from matplotlib import pyplot as plt import colorsys remove_yellow = True use_jet = False if use_jet: cmap = plt.cm.jet RGB_tuples = list(map(tuple, cmap(np.linspace(0, 1, N)))) elif cmap_seed is not None: # Randomized map based on a seed #cmap_ = 'Set1' #cmap_ = 'Dark2' choices = [ #'Set1', 'Dark2', 'jet', #'gist_rainbow', #'rainbow', #'gnuplot', #'Accent' ] cmap_hack = ub.argval('--cmap-hack', default=None) ncolor_hack = ub.argval('--ncolor-hack', default=None) if cmap_hack is not None: choices = [cmap_hack] if ncolor_hack is not None: N = int(ncolor_hack) N_ = N seed = sum(list(map(ord, ub.hash_data(cmap_seed)))) rng = np.random.RandomState(seed + 48930) cmap_str = rng.choice(choices, 1)[0] #print('cmap_str = %r' % (cmap_str,)) cmap = plt.cm.get_cmap(cmap_str) #.hashstr27(cmap_seed) #cmap_seed = 0 #pass jitter = (rng.randn(N) / (rng.randn(100).max() / 2)).clip(-1, 1) * ((1 / (N ** 2))) range_ = np.linspace(0, 1, N, endpoint=False) #print('range_ = %r' % (range_,)) range_ = range_ + jitter #print('range_ = %r' % (range_,)) while not (np.all(range_ >= 0) and np.all(range_ <= 1)): range_[range_ < 0] = np.abs(range_[range_ < 0] ) range_[range_ > 1] = 2 - range_[range_ > 1] #print('range_ = %r' % (range_,)) shift = rng.rand() range_ = (range_ + shift) % 1 #print('jitter = %r' % (jitter,)) #print('shift = %r' % (shift,)) #print('range_ = %r' % (range_,)) if ncolor_hack is not None: range_ = range_[0:N_] RGB_tuples = list(map(tuple, cmap(range_))) else: sat = brightness val = brightness hmin, hmax = hue_range if remove_yellow: hue_skips = [(.13, .24)] else: hue_skips = [] hue_skip_ranges = [_[1] - _[0] for _ in hue_skips] total_skip = sum(hue_skip_ranges) hmax_ = hmax - total_skip hue_list = np.linspace(hmin, hmax_, N, endpoint=False, dtype=np.float) # Remove colors (like hard to see yellows) in specified ranges for skip, range_ in zip(hue_skips, hue_skip_ranges): hue_list = [hue if hue <= skip[0] else hue + range_ for hue in hue_list] HSV_tuples = [(hue, sat, val) for hue in hue_list] RGB_tuples = [colorsys.hsv_to_rgb(*x) for x in HSV_tuples] if randomize: deterministic_shuffle(RGB_tuples) return RGB_tuples def distinct_markers(num, style='astrisk', total=None, offset=0): r""" Args: num (?): CommandLine: python -m .draw_func2 --exec-distinct_markers --show python -m .draw_func2 --exec-distinct_markers --style=star --show python -m .draw_func2 --exec-distinct_markers --style=polygon --show Ignore: >>> autompl() >>> style = ub.get_argval('--style', type_=str, default='astrisk') >>> marker_list = distinct_markers(10, style) >>> x_data = np.arange(0, 3) >>> for count, (marker) in enumerate(marker_list): >>> plt.plot(x_data, [count] * len(x_data), marker=marker, markersize=10, linestyle='', label=str(marker)) >>> legend() >>> show_if_requested() """ num_sides = 3 style_num = { 'astrisk': 2, 'star': 1, 'polygon': 0, 'circle': 3 }[style] if total is None: total = num total_degrees = 360 / num_sides marker_list = [ (num_sides, style_num, total_degrees * (count + offset) / total) for count in range(num) ] return marker_list def deterministic_shuffle(list_, rng=0): r""" Args: list_ (list): seed (int): Returns: list: list_ Example: >>> list_ = [1, 2, 3, 4, 5, 6] >>> seed = 1 >>> list_ = deterministic_shuffle(list_, seed) >>> result = str(list_) >>> print(result) [3, 2, 5, 1, 4, 6] """ from netharn import util rng = util.ensure_rng(rng) rng.shuffle(list_) return list_ _BASE_FNUM = 9001 def show_if_requested(N=1): """ Used at the end of tests. Handles command line arguments for saving figures Referencse: http://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib """ import matplotlib.pyplot as plt # Process figures adjustments from command line before a show or a save # udpate_adjust_subplots() # if use_argv: # # hack to take args from commandline # adjust_dict = ut.parse_dict_from_argv(adjust_dict) # adjust_subplots(use_argv=True) # def update_figsize(): # """ updates figsize based on command line """ # figsize = ub.argval('--figsize', type_=list, default=None) # if figsize is not None: # # Enforce inches and DPI # fig = plt.gcf() # figsize = [eval(term) if isinstance(term, str) else term # for term in figsize] # figw, figh = figsize[0], figsize[1] # print('get_size_inches = %r' % (fig.get_size_inches(),)) # print('fig w,h (inches) = %r, %r' % (figw, figh)) # fig.set_size_inches(figw, figh) # #print('get_size_inches = %r' % (fig.get_size_inches(),)) # update_figsize() save_parts = ub.argflag('--saveparts') fpath_ = ub.argval('--save', default=None) if fpath_ is None: fpath_ = ub.argval('--saveparts', default=None) save_parts = True if fpath_ is not None: _save_requested(fpath_, save_parts) # elif ub.argflag('--cmd'): # pass if ub.argflag('--show'): # if ub.argflag('--tile'): # if ut.get_computer_name().lower() in ['hyrule']: # fig_presenter.all_figures_tile(percent_w=.5, monitor_num=0) # else: # fig_presenter.all_figures_tile() # if ub.argflag('--present'): # fig_presenter.present() # for fig in fig_presenter.get_all_figures(): # fig.set_dpi(80) plt.show() def save_parts(fig, fpath, grouped_axes=None, dpi=None): """ FIXME: this works in mpl 2.0.0, but not 2.0.2 Args: fig (?): fpath (str): file path string dpi (None): (default = None) Returns: list: subpaths CommandLine: python -m draw_func2 save_parts Ignore: >>> # DISABLE_DOCTEST >>> autompl() >>> import matplotlib as mpl >>> import matplotlib.pyplot as plt >>> def testimg(fname): >>> return plt.imread(mpl.cbook.get_sample_data(fname)) >>> fnames = ['grace_hopper.png', 'ada.png'] * 4 >>> fig = plt.figure(1) >>> for c, fname in enumerate(fnames, start=1): >>> ax = fig.add_subplot(3, 4, c) >>> ax.imshow(testimg(fname)) >>> ax.set_title(fname[0:3] + str(c)) >>> ax.set_xticks([]) >>> ax.set_yticks([]) >>> ax = fig.add_subplot(3, 1, 3) >>> ax.plot(np.sin(np.linspace(0, np.pi * 2))) >>> ax.set_xlabel('xlabel') >>> ax.set_ylabel('ylabel') >>> ax.set_title('title') >>> fpath = 'test_save_parts.png' >>> adjust_subplots(fig=fig, wspace=.3, hspace=.3, top=.9) >>> subpaths = save_parts(fig, fpath, dpi=300) >>> fig.savefig(fpath) >>> ub.startfile(subpaths[0]) >>> ub.startfile(fpath) """ if dpi: # Need to set figure dpi before we draw fig.dpi = dpi # We need to draw the figure before calling get_window_extent # (or we can figure out how to set the renderer object) # if getattr(fig.canvas, 'renderer', None) is None: fig.canvas.draw() # Group axes that belong together if grouped_axes is None: grouped_axes = [] for ax in fig.axes: grouped_axes.append([ax]) subpaths = [] _iter = enumerate(grouped_axes, start=0) _iter = ub.ProgIter(list(_iter), label='save subfig') for count, axs in _iter: subpath = ub.augpath(fpath, suffix=chr(count + 65)) extent = axes_extent(axs).transformed(fig.dpi_scale_trans.inverted()) savekw = {} savekw['transparent'] = ub.argflag('--alpha') if dpi is not None: savekw['dpi'] = dpi savekw['edgecolor'] = 'none' fig.savefig(subpath, bbox_inches=extent, **savekw) subpaths.append(subpath) return subpaths _qtensured = False def _current_ipython_session(): """ Returns a reference to the current IPython session, if one is running """ try: __IPYTHON__ except NameError: return None else: import IPython ipython = IPython.get_ipython() # if ipython is None we must have exited ipython at some point return ipython def qtensure(): """ If you are in an IPython session, ensures that your backend is Qt. """ global _qtensured if not _qtensured: ipython = _current_ipython_session() if ipython: import sys if 'PyQt4' in sys.modules: ipython.magic('pylab qt4 --no-import-all') _qtensured = True else: ipython.magic('pylab qt5 --no-import-all') _qtensured = True def aggensure(): """ Ensures that you are in agg mode as long as IPython is not running This might help prevent errors in tmux like: qt.qpa.screen: QXcbConnection: Could not connect to display localhost:10.0 Could not connect to any X display. """ import matplotlib as mpl current_backend = mpl.get_backend() if current_backend != 'agg': ipython = _current_ipython_session() if not ipython: set_mpl_backend('agg') def set_mpl_backend(backend): """ Args: backend (str): name of backend to use (e.g. Agg, PyQt) """ import sys import matplotlib as mpl if backend.lower().startswith('qt'): # handle interactive qt case qtensure() if backend != mpl.get_backend(): # If we have already imported pyplot, then we need to use experimental # behavior. Otherwise, we can just set the backend. if 'matplotlib.pyplot' in sys.modules: from matplotlib import pyplot as plt plt.switch_backend(backend) else: mpl.use(backend) def autompl(): """ Uses platform heuristics to automatically set the mpl backend. If no display is available it will be set to agg, otherwise we will try to use the cross-platform Qt5Agg backend. """ import os import sys if sys.platform.startswith('win32'): # TODO: something reasonable pass else: DISPLAY = os.environ.get('DISPLAY', '') if not DISPLAY: set_mpl_backend('agg') else: set_mpl_backend('Qt5Agg') def imshow(img, fnum=None, title=None, figtitle=None, pnum=None, interpolation='nearest', cmap=None, heatmap=False, data_colorbar=False, xlabel=None, redraw_image=True, colorspace='bgr', ax=None, alpha=None, norm=None, **kwargs): r""" Args: img (ndarray): image data fnum (int): figure number colorspace (str): if the data is 3-4 channels, this indicates the colorspace 1 channel data is assumed grayscale. 4 channels assumes alpha. title (str): figtitle (None): pnum (tuple): plot number interpolation (str): other interpolations = nearest, bicubic, bilinear cmap (None): heatmap (bool): data_colorbar (bool): darken (None): redraw_image (bool): used when calling imshow over and over. if false doesnt do the image part. Returns: tuple: (fig, ax) Kwargs: docla, doclf, projection Returns: tuple: (fig, ax) Ignore: >>> autompl() >>> img_fpath = ut.grab_test_imgpath('carl.jpg') >>> img = util.imread(img_fpath) >>> (fig, ax) = imshow(img) >>> result = ('(fig, ax) = %s' % (str((fig, ax)),)) >>> print(result) >>> ut.show_if_requested() """ import matplotlib as mpl import matplotlib.pyplot as plt if ax is not None: fig = ax.figure nospecial = True else: fig = figure(fnum=fnum, pnum=pnum, title=title, figtitle=figtitle, **kwargs) ax = plt.gca() nospecial = False #ax.set_xticks([]) #ax.set_yticks([]) #return fig, ax if not redraw_image: return fig, ax if isinstance(img, six.string_types): # Allow for path to image to be specified from netharn import util img_fpath = img img = util.imread(img_fpath) plt_imshow_kwargs = { 'interpolation': interpolation, #'cmap': plt.get_cmap('gray'), } if alpha is not None: plt_imshow_kwargs['alpha'] = alpha if norm is not None: if norm is True: norm = mpl.colors.Normalize() plt_imshow_kwargs['norm'] = norm else: if cmap is None and not heatmap and not nospecial: plt_imshow_kwargs['vmin'] = 0 plt_imshow_kwargs['vmax'] = 255 if heatmap: cmap = 'hot' # Handle tensor chw format in most cases if img.ndim == 3: if img.shape[0] == 3 or img.shape[0] == 1: if img.shape[2] > 4: # probably in chw format img = img.transpose(1, 2, 0) try: if len(img.shape) == 3 and (img.shape[2] == 3 or img.shape[2] == 4): # img is in a color format from netharn import util dst_space = 'rgb' if img.shape[2] == 4: colorspace += 'a' dst_space += 'a' imgRGB = util.convert_colorspace(img, dst_space=dst_space, src_space=colorspace) if imgRGB.dtype.kind == 'f': maxval = imgRGB.max() if maxval > 1.01 and maxval < 256: imgRGB = np.array(imgRGB, dtype=np.uint8) ax.imshow(imgRGB, **plt_imshow_kwargs) elif len(img.shape) == 2 or (len(img.shape) == 3 and img.shape[2] == 1): # img is in grayscale if len(img.shape) == 3: imgGRAY = img.reshape(img.shape[0:2]) else: imgGRAY = img if cmap is None: cmap = plt.get_cmap('gray') if isinstance(cmap, six.string_types): cmap = plt.get_cmap(cmap) # for some reason gray floats aren't working right if imgGRAY.max() <= 1.01 and imgGRAY.min() >= -1E-9: imgGRAY = (imgGRAY * 255).astype(np.uint8) ax.imshow(imgGRAY, cmap=cmap, **plt_imshow_kwargs) else: raise AssertionError( 'unknown image format. img.dtype=%r, img.shape=%r' % (img.dtype, img.shape)) except TypeError as te: print('[df2] imshow ERROR %r' % (te,)) raise except Exception as ex: print('!!!!!!!!!!!!!!WARNING!!!!!!!!!!!') print('[df2] type(img) = %r' % type(img)) if not isinstance(img, np.ndarray): print('!!!!!!!!!!!!!!ERRROR!!!!!!!!!!!') pass #print('img = %r' % (img,)) print('[df2] img.dtype = %r' % (img.dtype,)) print('[df2] type(img) = %r' % (type(img),)) print('[df2] img.shape = %r' % (img.shape,)) print('[df2] imshow ERROR %r' % ex) raise #plt.set_cmap('gray') ax.set_xticks([]) ax.set_yticks([]) if data_colorbar is True: scores = np.unique(img.flatten()) if cmap is None: cmap = 'hot' colors = scores_to_color(scores, cmap) colorbar(scores, colors) if xlabel is not None: ax.set_xlabel(xlabel) if figtitle is not None: set_figtitle(figtitle) return fig, ax def colorbar(scalars, colors, custom=False, lbl=None, ticklabels=None, float_format='%.2f', **kwargs): """ adds a color bar next to the axes based on specific scalars Args: scalars (ndarray): colors (ndarray): custom (bool): use custom ticks Kwargs: See plt.colorbar Returns: cb : matplotlib colorbar object Ignore: >>> autompl() >>> scalars = np.array([-1, -2, 1, 1, 2, 7, 10]) >>> cmap_ = 'plasma' >>> logscale = False >>> custom = True >>> reverse_cmap = True >>> val2_customcolor = { ... -1: UNKNOWN_PURP, ... -2: LIGHT_BLUE, ... } >>> colors = scores_to_color(scalars, cmap_=cmap_, logscale=logscale, reverse_cmap=reverse_cmap, val2_customcolor=val2_customcolor) >>> colorbar(scalars, colors, custom=custom) >>> df2.present() >>> show_if_requested() Ignore: >>> # ENABLE_DOCTEST >>> scalars = np.linspace(0, 1, 100) >>> cmap_ = 'plasma' >>> logscale = False >>> custom = False >>> reverse_cmap = False >>> colors = scores_to_color(scalars, cmap_=cmap_, logscale=logscale, >>> reverse_cmap=reverse_cmap) >>> colors = [lighten_rgb(c, .3) for c in colors] >>> colorbar(scalars, colors, custom=custom) >>> df2.present() >>> show_if_requested() """ import matplotlib as mpl import matplotlib.pyplot as plt assert len(scalars) == len(colors), 'scalars and colors must be corresponding' if len(scalars) == 0: return None # Parameters ax = plt.gca() divider = _ensure_divider(ax) cax = divider.append_axes('right', size='5%', pad=0.05) xy, width, height = _get_axis_xy_width_height(ax) #orientation = ['vertical', 'horizontal'][0] TICK_FONTSIZE = 8 # # Create scalar mappable with cmap if custom: # FIXME: clean this code up and change the name custom # to be meaningful. It is more like: display unique colors unique_scalars, unique_idx = np.unique(scalars, return_index=True) unique_colors = np.array(colors)[unique_idx] #max_, min_ = unique_scalars.max(), unique_scalars.min() #extent_ = max_ - min_ #bounds = np.linspace(min_, max_ + 1, extent_ + 2) listed_cmap = mpl.colors.ListedColormap(unique_colors) #norm = mpl.colors.BoundaryNorm(bounds, listed_cmap.N) #sm = mpl.cm.ScalarMappable(cmap=listed_cmap, norm=norm) sm = mpl.cm.ScalarMappable(cmap=listed_cmap) sm.set_array(np.linspace(0, 1, len(unique_scalars) + 1)) else: sorted_scalars = sorted(scalars) listed_cmap = scores_to_cmap(scalars, colors) sm = plt.cm.ScalarMappable(cmap=listed_cmap) sm.set_array(sorted_scalars) # Use mapable object to create the colorbar #COLORBAR_SHRINK = .42 # 1 #COLORBAR_PAD = .01 # 1 #COLORBAR_ASPECT = np.abs(20 * height / (width)) # 1 cb = plt.colorbar(sm, cax=cax, **kwargs) ## Add the colorbar to the correct label #axis = cb.ax.yaxis # if orientation == 'horizontal' else cb.ax.yaxis #position = 'bottom' if orientation == 'horizontal' else 'right' #axis.set_ticks_position(position) # This line alone removes data # axis.set_ticks([0, .5, 1]) if custom: ticks = np.linspace(0, 1, len(unique_scalars) + 1) if len(ticks) < 2: ticks += .5 else: # SO HACKY ticks += (ticks[1] - ticks[0]) / 2 if isinstance(unique_scalars, np.ndarray) and unique_scalars.dtype.kind == 'f': ticklabels = [float_format % scalar for scalar in unique_scalars] else: ticklabels = unique_scalars cb.set_ticks(ticks) # tick locations cb.set_ticklabels(ticklabels) # tick labels elif ticklabels is not None: ticks_ = cb.ax.get_yticks() mx = ticks_.max() mn = ticks_.min() ticks = np.linspace(mn, mx, len(ticklabels)) cb.set_ticks(ticks) # tick locations cb.set_ticklabels(ticklabels) #cb.ax.get_yticks() #cb.set_ticks(ticks) # tick locations #cb.set_ticklabels(ticklabels) # tick labels # _set_plotdat(cb.ax, 'viztype', 'colorbar-%s' % (lbl,)) # _set_plotdat(cb.ax, 'sm', sm) # FIXME: Figure out how to make a maximum number of ticks # and to enforce them to be inside the data bounds cb.ax.tick_params(labelsize=TICK_FONTSIZE) # Sets current axis plt.sca(ax) if lbl is not None: cb.set_label(lbl) return cb _DF2_DIVIDER_KEY = '_df2_divider' def _get_plotdat(ax, key, default=None): """ returns internal property from a matplotlib axis """ _plotdat = _get_plotdat_dict(ax) val = _plotdat.get(key, default) return val def _set_plotdat(ax, key, val): """ sets internal property to a matplotlib axis """ _plotdat = _get_plotdat_dict(ax) _plotdat[key] = val def _del_plotdat(ax, key): """ sets internal property to a matplotlib axis """ _plotdat = _get_plotdat_dict(ax) if key in _plotdat: del _plotdat[key] def _get_plotdat_dict(ax): """ sets internal property to a matplotlib axis """ if '_plotdat' not in ax.__dict__: ax.__dict__['_plotdat'] = {} plotdat_dict = ax.__dict__['_plotdat'] return plotdat_dict def _ensure_divider(ax): """ Returns previously constructed divider or creates one """ from mpl_toolkits.axes_grid1 import make_axes_locatable divider = _get_plotdat(ax, _DF2_DIVIDER_KEY, None) if divider is None: divider = make_axes_locatable(ax) _set_plotdat(ax, _DF2_DIVIDER_KEY, divider) orig_append_axes = divider.append_axes def df2_append_axes(divider, position, size, pad=None, add_to_figure=True, **kwargs): """ override divider add axes to register the divided axes """ div_axes = _get_plotdat(ax, 'df2_div_axes', []) new_ax = orig_append_axes(position, size, pad=pad, add_to_figure=add_to_figure, **kwargs) div_axes.append(new_ax) _set_plotdat(ax, 'df2_div_axes', div_axes) return new_ax new_method = df2_append_axes.__get__(divider, divider.__class__) setattr(divider, 'append_axes', new_method) # ut.inject_func_as_method(divider, df2_append_axes, 'append_axes', allow_override=True) return divider def scores_to_color(score_list, cmap_='hot', logscale=False, reverse_cmap=False, custom=False, val2_customcolor=None, score_range=None, cmap_range=(.1, .9)): """ Other good colormaps are 'spectral', 'gist_rainbow', 'gist_ncar', 'Set1', 'Set2', 'Accent' # TODO: plasma Args: score_list (list): cmap_ (str): defaults to hot logscale (bool): cmap_range (tuple): restricts to only a portion of the cmap to avoid extremes Returns: <class '_ast.ListComp'> Ignore: >>> ut.exec_funckw(scores_to_color, globals()) >>> score_list = np.array([-1, -2, 1, 1, 2, 10]) >>> # score_list = np.array([0, .1, .11, .12, .13, .8]) >>> # score_list = np.linspace(0, 1, 100) >>> cmap_ = 'plasma' >>> colors = scores_to_color(score_list, cmap_) >>> imgRGB = util.atleast_nd(np.array(colors)[:, 0:3], 3, tofront=True) >>> imgRGB = imgRGB.astype(np.float32) >>> imgBGR = util.convert_colorspace(imgRGB, 'BGR', 'RGB') >>> imshow(imgBGR) >>> show_if_requested() Ignore: >>> score_list = np.array([-1, -2, 1, 1, 2, 10]) >>> cmap_ = 'hot' >>> logscale = False >>> reverse_cmap = True >>> custom = True >>> val2_customcolor = { ... -1: UNKNOWN_PURP, ... -2: LIGHT_BLUE, ... } """ import matplotlib.pyplot as plt assert len(score_list.shape) == 1, 'score must be 1d' if len(score_list) == 0: return [] if logscale: # Hack score_list = apply_logscale(score_list) #if loglogscale #score_list = np.log2(np.log2(score_list + 2) + 1) #if isinstance(cmap_, six.string_types): cmap = plt.get_cmap(cmap_) #else: # cmap = cmap_ if reverse_cmap: cmap = reverse_colormap(cmap) #if custom: # base_colormap = cmap # data = score_list # cmap = customize_colormap(score_list, base_colormap) if score_range is None: min_ = score_list.min() max_ = score_list.max() else: min_ = score_range[0] max_ = score_range[1] if logscale: min_, max_ = apply_logscale([min_, max_]) if cmap_range is None: cmap_scale_min, cmap_scale_max = 0., 1. else: cmap_scale_min, cmap_scale_max = cmap_range extent_ = max_ - min_ if extent_ == 0: colors = [cmap(.5) for fx in range(len(score_list))] else: if False and logscale: # hack score_list = np.array(score_list) #rank_multiplier = score_list.argsort() / len(score_list) #normscore = np.array(list(map(score2_01, score_list))) * rank_multiplier normscore = np.array(list(map(score2_01, score_list))) colors = list(map(cmap, normscore)) else: colors = [cmap(score2_01(score)) for score in score_list] if val2_customcolor is not None: colors = [ np.array(val2_customcolor.get(score, color)) for color, score in zip(colors, score_list)] return colors def reverse_colormap(cmap): """ References: http://nbviewer.ipython.org/github/kwinkunks/notebooks/blob/master/Matteo_colourmaps.ipynb """ import matplotlib as mpl if isinstance(cmap, mpl.colors.ListedColormap): return mpl.colors.ListedColormap(cmap.colors[::-1]) else: reverse = [] k = [] for key, channel in six.iteritems(cmap._segmentdata): data = [] for t in channel: data.append((1 - t[0], t[1], t[2])) k.append(key) reverse.append(sorted(data)) cmap_reversed = mpl.colors.LinearSegmentedColormap( cmap.name + '_reversed', dict(zip(k, reverse))) return cmap_reversed def draw_border(ax, color, lw=2, offset=None, adjust=True): 'draws rectangle border around a subplot' if adjust: xy, width, height = _get_axis_xy_width_height(ax, -.7, -.2, 1, .4) else: xy, width, height = _get_axis_xy_width_height(ax) if offset is not None: xoff, yoff = offset xy = [xoff, yoff] height = - height - yoff width = width - xoff import matplotlib as mpl rect = mpl.patches.Rectangle(xy, width, height, lw=lw) rect = ax.add_patch(rect) rect.set_clip_on(False) rect.set_fill(False) rect.set_edgecolor(color) return rect def draw_boxes(boxes, box_format='xywh', color='blue', labels=None, textkw=None, ax=None): """ Args: boxes (list): list of coordindates in xywh, tlbr, or cxywh format box_format (str): specify how boxes are formated xywh is the top left x and y pixel width and height cxywh is the center xy pixel width and height tlbr is the top left xy and the bottom right xy color (str): edge color of the boxes labels (list): if specified, plots a text annotation on each box Example: >>> from netharn.util.mplutil import * >>> autompl() >>> bboxes = [[.1, .1, .6, .3], [.3, .5, .5, .6]] >>> col = draw_boxes(bboxes) """ import matplotlib as mpl from matplotlib import pyplot as plt if ax is None: ax = plt.gca() from netharn import util if isinstance(boxes, util.Boxes): box_format = boxes.format boxes = boxes.data if not len(boxes): return boxes = np.asarray(boxes) if box_format == 'xywh': xywh = boxes elif box_format == 'cxywh': cx, cy, w, h = boxes.T[0:4] x1 = cx - (w / 2) y1 = cy - (h / 2) xywh = np.vstack([x1, y1, w, h]).T elif box_format == 'tlbr': x1, y1 = boxes.T[0:2] w, h = boxes.T[2:4] - boxes.T[0:2] xywh = np.vstack([x1, y1, w, h]).T else: raise KeyError(box_format) edgecolor = Color(color).as01('rgba') facecolor = Color((0, 0, 0, 0)).as01('rgba') rectkw = dict(ec=edgecolor, fc=facecolor, lw=2, linestyle='solid') patches = [mpl.patches.Rectangle((x, y), w, h, **rectkw) for x, y, w, h in xywh] col = mpl.collections.PatchCollection(patches, match_original=True) ax.add_collection(col) if labels: texts = [] default_textkw = { 'horizontalalignment': 'left', 'verticalalignment': 'top', 'backgroundcolor': (0, 0, 0, .3), 'color': 'white', 'fontproperties': mpl.font_manager.FontProperties( size=6, family='monospace'), } tkw = default_textkw.copy() if textkw is not None: tkw.update(textkw) for (x1, y1, w, h), label in zip(xywh, labels): texts.append((x1, y1, label, tkw)) for (x1, y1, catname, tkw) in texts: ax.text(x1, y1, catname, **tkw) return col def draw_line_segments(pts1, pts2, ax=None, **kwargs): """ draws `N` line segments between `N` pairs of points Args: pts1 (ndarray): Nx2 pts2 (ndarray): Nx2 ax (None): (default = None) **kwargs: lw, alpha, colors CommandLine: python -m netharn.util.mplutil draw_line_segments --show Example: >>> pts1 = np.array([(.1, .8), (.6, .8)]) >>> pts2 = np.array([(.6, .7), (.4, .1)]) >>> figure(fnum=None) >>> draw_line_segments(pts1, pts2) >>> # xdoc: +REQUIRES(--show) >>> import matplotlib.pyplot as plt >>> ax = plt.gca() >>> ax.set_xlim(0, 1) >>> ax.set_ylim(0, 1) >>> show_if_requested() """ import matplotlib.pyplot as plt import matplotlib as mpl if ax is None: ax = plt.gca() assert len(pts1) == len(pts2), 'unaligned' segments = [(xy1, xy2) for xy1, xy2 in zip(pts1, pts2)] linewidth = kwargs.pop('lw', kwargs.pop('linewidth', 1.0)) alpha = kwargs.pop('alpha', 1.0) if 'color' in kwargs: kwargs['colors'] = kwargs['color'] # mpl.colors.ColorConverter().to_rgb(kwargs['color']) line_group = mpl.collections.LineCollection(segments, linewidths=linewidth, alpha=alpha, **kwargs) ax.add_collection(line_group) def make_heatmask(probs, cmap='plasma', with_alpha=True): """ Colorizes a single-channel intensity mask (with an alpha channel) """ import matplotlib as mpl from netharn.util import imutil assert len(probs.shape) == 2 cmap_ = mpl.cm.get_cmap(cmap) probs = imutil.ensure_float01(probs) heatmask = cmap_(probs) if with_alpha: heatmask[:, :, 0:3] = heatmask[:, :, 0:3][:, :, ::-1] heatmask[:, :, 3] = probs return heatmask def colorbar_image(domain, cmap='plasma', dpi=96, shape=(200, 20), transparent=False): """ Notes: shape is approximate Ignore: domain = np.linspace(-30, 200) cmap='plasma' dpi = 80 dsize = (20, 200) util.imwrite('foo.png', util.colorbar_image(np.arange(0, 1)), shape=(400, 80)) import plottool as pt pt.qtensure() import matplotlib as mpl mpl.style.use('ggplot') util.imwrite('foo.png', util.colorbar_image(np.linspace(0, 1, 100), dpi=200, shape=(1000, 40), transparent=1)) ub.startfile('foo.png') """ import matplotlib as mpl mpl.use('agg', force=False, warn=False) from matplotlib import pyplot as plt fig = plt.figure(dpi=dpi) w, h = shape[1] / dpi, shape[0] / dpi # w, h = 1, 10 fig.set_size_inches(w, h) ax = fig.add_subplot('111') sm = plt.cm.ScalarMappable(cmap=plt.get_cmap(cmap)) sm.set_array(domain) plt.colorbar(sm, cax=ax) cb_img = render_figure_to_image(fig, dpi=dpi, transparent=transparent) plt.close(fig) return cb_img if __name__ == '__main__': r""" CommandLine: python -m netharn.util.mplutil """ import xdoctest xdoctest.doctest_module(__file__)
[ 6738, 11593, 37443, 834, 1330, 4112, 62, 11748, 11, 7297, 11, 3601, 62, 8818, 198, 11748, 269, 85, 17, 198, 11748, 19798, 292, 355, 279, 67, 198, 11748, 299, 32152, 355, 45941, 198, 11748, 2237, 198, 11748, 20967, 2120, 355, 20967, 19...
2.062957
31,577
# Copyright (c) OpenMMLab. All rights reserved. import torch from mmdeploy.core import FUNCTION_REWRITER
[ 2, 15069, 357, 66, 8, 4946, 44, 5805, 397, 13, 1439, 2489, 10395, 13, 198, 11748, 28034, 198, 198, 6738, 8085, 2934, 1420, 13, 7295, 1330, 29397, 4177, 2849, 62, 2200, 18564, 2043, 1137, 628, 198 ]
3
36
#!/usr/bin/env python3 """This script demonstrates setting properties on a page manually. The script accepts a single command line option, which is a page ID. It will then display information about the properties and update a few of them. Note that this script assumes the database has already been created with required fields. The caller must set `NOTION_AUTH_TOKEN` to a valid integration token. """ import logging import os import sys logging.basicConfig(level=logging.INFO) import notional from notional import types page_id = sys.argv[1] auth_token = os.getenv("NOTION_AUTH_TOKEN") notion = notional.connect(auth=auth_token) # get an existing page... page = notion.pages.retrieve(page_id) print(f"{page.Title} => {page.url}") # print all current properties on the page... for name, prop in page.properties.items(): print(f"{name} => {prop}") # update a property on the page... page["Complete"] = types.Checkbox.from_value(True) # FIXME this feature is broken - https://github.com/jheddings/notional/issues/9 # notion.pages.update(page)
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 198, 37811, 1212, 4226, 15687, 4634, 6608, 319, 257, 2443, 14500, 13, 198, 198, 464, 4226, 18178, 257, 2060, 3141, 1627, 3038, 11, 543, 318, 257, 2443, 4522, 13, 220, 632, 481, 7...
3.261538
325
#!/usr/bin/env python3 """Use this file to setup a build environment.""" import os import argparse from support.linux.log import Log from support.docker_wrapper.retroroot import RetrorootDocker CWD = os.getcwd() def parse_args(args): """Parse arguments. :return: The argument object. """ parser = argparse.ArgumentParser() parser.add_argument("-b", "--build", default=False, action="store_true", help="Build") parser.add_argument("-s", "--setup", default=False, action="store_true", help="setup") parser.add_argument("--verbose", default=False, action="store_true", help="Prepare verbosely") return parser.parse_args(args) if __name__ == '__main__': main()
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 37811, 11041, 428, 2393, 284, 9058, 257, 1382, 2858, 526, 15931, 198, 11748, 28686, 198, 11748, 1822, 29572, 198, 6738, 1104, 13, 23289, 13, 6404, 1330, 5972, 198, 6738, 1104, 13, ...
2.024229
454
import coalpy.gpu as g import numpy as np import math import functools from . import prefix_sum as gpu_prefix_sum if __name__ == "__main__": run_test("test prefix sum inclusive", test_cluster_gen_inclusive) run_test("test prefix sum exclusive", test_cluster_gen_exclusive)
[ 11748, 5655, 9078, 13, 46999, 355, 308, 198, 11748, 299, 32152, 355, 45941, 198, 11748, 10688, 198, 11748, 1257, 310, 10141, 198, 6738, 764, 1330, 21231, 62, 16345, 355, 308, 19944, 62, 40290, 62, 16345, 198, 198, 361, 11593, 3672, 834,...
3.043011
93
from torchvision.transforms import transforms from torch.utils.data import DataLoader from torchvision.datasets import ImageFolder import torch as T import torch.optim as optim from model import Generator, Discriminator from loss_fn import GeneratorLoss, TVLoss from utils import show_progress, save import datetime import gc import os device = 'cuda' if T.cuda.is_available() else 'cpu' BATCH_SIZE = 16 SIZE_HR = 256 SIZE_LR = 64 num_workers = 2 rootpath = '../data' transform_hr = transforms.Compose([ transforms.Resize((SIZE_HR, SIZE_HR)), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) data_hr = ImageFolder(rootpath, transform=transform_hr) transform_lr = transforms.Compose([ transforms.Resize((SIZE_LR, SIZE_LR)), transforms.ToTensor(), transforms.GaussianBlur(kernel_size=25), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) data_lr = ImageFolder(rootpath, transform=transform_lr) full_data = ConcatDataset(data_lr, data_hr) loader = DataLoader(full_data, BATCH_SIZE, num_workers=num_workers) generator = Generator(3, 64).to(device) discriminator = Discriminator(3, 64).to(device) lr = 1e-1000 gen_optimizer = optim.Adam(generator.parameters(), lr=lr) disc_optimizer = optim.Adam(discriminator.parameters(), lr=lr) generator_criterion = GeneratorLoss().to(device) g_losses = [] d_losses = [] EPOCHS = 1000 if 'models' not in os.listdir(): os.mkdir('models') save_path = '../models/' # <----- TRAINING LOOP -----> for epoch in range(1, EPOCHS): generator.train() discriminator.train() print(f'EPOCH [{epoch}/{EPOCHS}]') sum_d_loss = 0 sum_g_loss = 0 gc.collect() T.cuda.empty_cache() start = datetime.datetime.now() for idx, (item, target) in enumerate(loader): item = item[0].to(device) target = target[0].to(device) fake_image = generator(item) discriminator.zero_grad() real_out = discriminator(target).mean() fake_out = discriminator(fake_image).mean() d_loss = 1 - real_out + fake_out d_loss.backward(retain_graph=True) generator.zero_grad() g_loss = generator_criterion(fake_out, fake_image, target) g_loss.backward() fake_img = generator(item) fake_out = discriminator(fake_img).mean() if idx % 100 == 0: print( f'Batch {idx}/{loader.__len__()} \nLoss (Generator) {g_loss.detach().cpu()}\nLoss (Discriminator) {d_loss.detach().cpu()}' ) pred = fake_img[0].detach().cpu() save(generator, discriminator, save_path) show_progress([item.detach().cpu()[0], pred, target.detach().cpu()[0]], save=True, show=False) gen_optimizer.step() sum_d_loss += d_loss.detach().cpu() sum_g_loss += g_loss.detach().cpu() print(f'Time per epoch = {start - datetime.datetime.now()}') g_losses.append(sum_g_loss / loader.__len__()) d_losses.append(sum_d_loss / loader.__len__()) print(f'D_loss {sum_d_loss}') print(f'G_loss {sum_g_loss}')
[ 6738, 28034, 10178, 13, 7645, 23914, 1330, 31408, 198, 6738, 28034, 13, 26791, 13, 7890, 1330, 6060, 17401, 198, 6738, 28034, 10178, 13, 19608, 292, 1039, 1330, 7412, 41092, 198, 11748, 28034, 355, 309, 198, 11748, 28034, 13, 40085, 355, ...
2.13038
1,580
import re ''' Function for Formatting n-grams. @Parameters: Tuple: n-gram to be formatted. @Return: String: formatted gram. ''' ''' Function for Formatting sentences. @Parameters: Sentence: unformatted sentence. @Return: String: formatted sentence. '''
[ 11748, 302, 198, 198, 7061, 6, 15553, 329, 18980, 889, 299, 12, 4546, 82, 13, 198, 220, 220, 220, 2488, 48944, 25, 309, 29291, 25, 299, 12, 4546, 284, 307, 39559, 13, 198, 220, 220, 220, 2488, 13615, 25, 10903, 25, 39559, 14599, 1...
3.079545
88
from datetime import timedelta import commonware.log from celeryutils import task from django.utils.encoding import force_text from tower import ugettext_lazy as _ from mkt.account.utils import fxa_preverify_url from mkt.site.mail import send_html_mail_jinja from mkt.users.models import UserProfile fxa_email_subjects = { 'customers-before': _('Firefox Accounts is coming'), 'customers-during': _('Activate your Firefox Account'), 'customers-after': _('Activate your Firefox Account'), 'developers-before': _('Firefox Accounts is coming'), 'developers-during': _('Activate your Firefox Account'), 'developers-after': _('Activate your Firefox Account') } fxa_email_types = fxa_email_subjects.keys() log = commonware.log.getLogger('z.users')
[ 6738, 4818, 8079, 1330, 28805, 12514, 198, 198, 11748, 2219, 1574, 13, 6404, 198, 6738, 18725, 1924, 26791, 1330, 4876, 198, 198, 6738, 42625, 14208, 13, 26791, 13, 12685, 7656, 1330, 2700, 62, 5239, 198, 198, 6738, 10580, 1330, 334, 11...
3.088
250
import sys sys.path.insert(0, '/srv/jpmorgan') from app import app as application
[ 11748, 25064, 198, 17597, 13, 6978, 13, 28463, 7, 15, 11, 31051, 27891, 85, 14, 73, 4426, 9971, 11537, 198, 198, 6738, 598, 1330, 598, 355, 3586 ]
3.037037
27
""" Voronoi Plot ============ """ import numpy as np from sklearn.cluster import KMeans import msmexplorer as msme # Create a random dataset across several variables rs = np.random.RandomState(42) n, p = 1000, 2 d = rs.normal(0, 2, (n, p)) d += np.log(np.arange(1, p + 1)) * -5 + 10 # Cluster data using KMeans kmeans = KMeans(random_state=rs) kmeans.fit(d) # Plot Voronoi Diagram msme.plot_voronoi(kmeans, color_palette=msme.palettes.msme_rgb)
[ 37811, 198, 53, 273, 261, 23013, 28114, 198, 25609, 198, 37811, 198, 11748, 299, 32152, 355, 45941, 198, 6738, 1341, 35720, 13, 565, 5819, 1330, 509, 5308, 504, 198, 198, 11748, 285, 5796, 20676, 11934, 355, 13845, 1326, 198, 198, 2, ...
2.480663
181
"""This algorithm implements the Wang Generalization algotithm with constraint checking This algorithm simplifies lines. It detects for each line the bends. It analyze the bend and remove the bends that are below a certain diameter. The point and lines that do not need to be simplified are still used to enforce topology integrity between those feature that need to be simplified Limits and constraints Always works better when the line to process meet the OGC simple line. """ import math, sys from shapely.geometry import Point, LineString, LinearRing, Polygon from shapely.prepared import prep from shapely import affinity from lib_geosim import GenUtil, PointSc, LineStringSc, SpatialContainer, GeoSimException # Internal constant ===> Should be modify with care... _AREA_CMP_INDEX = .75 # Compactness index factor applied to the adjusted area #Internal key word constants _BURNED = "Burned" _DIAMETER = "diameter" _SIMPLIFIED = 'Simplified' _NOT_SIMPLIFIED = 'NotSimplified' _UNSIMPLIFIABLE = 'Unsimplifiable' def _rotate_start_bend(self): """Rotate a closed line string so the start of the line is also the start of a clockwise bend To be done on closed line only Parameters ---------- None Returns ------- None """ rotate = None max_v = len(self.vertex_orientation) for i in range(max_v): j = (i+1) % max_v if self.vertex_orientation[i] == GenUtil.CLOCKWISE and \ self.vertex_orientation[j] == GenUtil.ANTI_CLOCKWISE: rotate = i break # Rotate the frist last vertex to the position of the biggest bend if rotate is None: # All the bend are clockwise. Nothing to do pass elif rotate == 0: # The line string does not to be rotated pass else: lst_coord = self.coords[rotate:] + self.coords[1:rotate+1] self.coords = lst_coord # Update the LineString coordinate def _extract_coords(self, i,j): """Extract the coordinate between index [i,j] If j is lower than i act like a circular array and avoid duplication of first/last vertice Parameters ---------- i,j : int Index used to extract a sub list Returns ------- List list of (x,y) coordinates """ if i <= j: lst_coords = self.coords[i:j+1] else: lst_coords = self.coords[i:] + self.coords[0:j+1] return lst_coords def _change_inflexion(self, i): """Flag if there is an inflexion between at the specified vertices. There is inflexion when a change of orientation occurs from clock wise to anti clocwise or vice cersa Parameters ---------- i : int Index of for vertex orientation Returns ------- bool Flag indicating if an inflexion occurs or not """ max_v = len(self.vertex_orientation) if (self.vertex_orientation[i] == GenUtil.ANTI_CLOCKWISE and self.vertex_orientation[(i+1) % max_v] == GenUtil.CLOCKWISE) or \ (self.vertex_orientation[i] == GenUtil.CLOCKWISE and self.vertex_orientation[(i+1) % max_v] == GenUtil.ANTI_CLOCKWISE): inflexion = True else: inflexion = False return inflexion def _add_bends(self, inflexions): """Add Bend to the line from the inflexion list Parameters ---------- inflexions : List List of the inflexions in the list Returns ------- None """ for k in range(len(inflexions) - 1): i = inflexions[k][0] j = inflexions[k + 1][1] self.sb_bends.append(Bend(i, j, self._extract_coords(i, j))) def _create_bends(self): """Create the bends in the line Parameters ---------- None Returns ------- None """ # Delete any actual bend information self.sb_bends = [] # Remove the colinear vertice in order to facilitate bend detection (moreover colinaer vertice are useless) self._remove_colinear_vertex() inflexions = [] max = len(self.vertex_orientation) if self.is_closed: # Rotate the line to position at the start of a bend self._rotate_start_bend() # The vertex_oriention list is considered a circular list for i in range(max): j = (i + 1) % max if self._change_inflexion(i): inflexions.append((i, j)) # Create the bend from the inflexion point if inflexions: if len(inflexions) >= 3: # If there is more than 23 inflexions we add another circular inflexion i = inflexions[-1][0] j = inflexions[0][1] inflexions.append((i, j)) # Transform the inflexion into bends self._add_bends(inflexions) else: # The vertex_oriention list is not considered a circular list if max == 3: # Special case there is only one bend to simplify j = len(self.coords)-1 self.sb_bends.append(Bend(0, j, self._extract_coords(0, j))) elif max >= 4: for i in range(1, max-2): if self._change_inflexion(i): inflexions.append((i, i+1)) # Add inflexion to add the first and last bend inflexions = [(0, None)] + inflexions + [(None, max-1)] # Transform inflexion into bends self._add_bends(inflexions) return def _sort_bends(self): """Sort the bends by order of ascending min_adj_are Parameters ---------- None Returns ------- None """ lst_bends = [] for i, bend in enumerate(self.sb_bends): if bend.adj_area <= self.sb_min_adj_area: # Only select the bend below the minimum adjusted area lst_bends.append((i, bend.adj_area)) # Sort based of the adj_area from smallest to biggest lst_bends.sort(key=lambda tup: tup[1]) # sorts in place return lst_bends def _offset_bend_ij(self, i, j): """"Offset the value of the different bend i,j because one or more vertice of the line were removed Handle circular list when j < i Parameters ---------- i,j : int Index in the line where the vertice were removed Returns ------- None """ if i < j: offset = j-i-1 else: offset = j for bend in self.sb_bends: if bend.status == _NOT_SIMPLIFIED: if bend.i < bend.j: if bend.i >= j: bend.i -= offset bend.j -= offset else: if bend.i >= j: bend.i -= offset def _make_line_ccw(self): """Make sure the line is counter clockwise. Only apply to closed line Parameters ---------- None Returns ------- None """ if self.sb_is_closed: tmp_ring = LinearRing(self.coords) if not tmp_ring.is_ccw: # The linear ring is clockwise. Reverse the coordinates to make it ccw self.coords = list(reversed(self.coords)) def simplify(self, diameter, s_constraints=None): """Simplify the line by reducing each bend Parameters ---------- None Returns ------- None """ nbr_bend_simplified = 0 # Make sure the line is counter clockwise # self._make_line_ccw() # Create the bend in the line self._create_bends() max_bends = len(self.sb_bends) sorted_bends = self._sort_bends() if len(sorted_bends) == 0: # No more bend to simplify. Line is at its simplest form self.sb_is_simplest = True elif len(sorted_bends) >= 2: # Make the biggest bend (last one) unsimplifiable ind_last = sorted_bends[-1][0] self.sb_bends[ind_last].status = _UNSIMPLIFIABLE # Loop over each bend to simplify them for sorted_bend in sorted_bends: ind = sorted_bend[0] if self.sb_bends[ind].status == _NOT_SIMPLIFIED: ind_before = None ind_after = None if self.sb_is_closed: if max_bends >= 2: ind_before = (ind-1) % max_bends ind_after = (ind+1) % max_bends else: if ind > 0: ind_before = ind-1 if ind < max_bends-1: ind_after = ind+1 # Validate the spatial constraints i = self.sb_bends[ind].i j = self.sb_bends[ind].j if i < j: lst_coords = self.coords[0:i+1] + self.coords[j:] else: # Manage circular list lst_coords = self.coords[j:i+1] + self.coords[j:j+1] if self.is_closed: if len(lst_coords) >= 4: if s_constraints is not None: in_conflict = s_constraints.check_constraints(self, self.sb_bends[ind]) else: in_conflict = False else: # A closed line cannot have less than 4 vertices in_conflict = True else: if len(lst_coords) >= 2: if s_constraints is not None: in_conflict = s_constraints.check_constraints(self, self.sb_bends[ind]) else: in_conflict = False else: # An open line cannot have less than 3 vertices in_conflict = True if not in_conflict: # Update the coordinates self.coords = lst_coords # Bend before and after must no be simplified in this pass maybe a next pass if ind_before is not None: self.sb_bends[ind_before].status = _UNSIMPLIFIABLE if ind_after is not None: self.sb_bends[ind_after].status = _UNSIMPLIFIABLE self.sb_bends[ind].status = _SIMPLIFIED nbr_bend_simplified += 1 self._offset_bend_ij(i, j) return nbr_bend_simplified class PointSb(PointSc): """ A class to represent a Point used by the SherBend algorithm Attributes ---------- coords : tuple A tuple (x,y) representing one coordinate properties : dict The dictionary of the properties (attributes of the features) fast_access : Boolean A flag to indicate if we keep a copy od the coordinate in order to accelrate the access becase the access to the C function is slow """ class AlgoSherbend(object): """Main class for the Sherbend algorithm Attributes: - None """ def __init__(self, command, geo_content): """Constructor of the class Parameters ---------- command : DataClass Contains all the commands for the Sherbend line simplification algorithm geo_content: DataClass Contains the geo information needed for the the sherbend line reduction algorithm Returns ------- None """ self.command = command self.geo_content = geo_content self.nbr_bend_simplified = 0 def calculate_min_adj_area(self, diameter): """Calculates the minimum adjusted area of a band Parameters ---------- diameter : float diameter used to calculate the minimum adjusted area Returns ------- float Minimum adjusted area """ return (_AREA_CMP_INDEX * math.pi * (diameter/2.0)**2.0) def _calculate_adj_area(self, coords): """Calculates the adjusted area of a polygon Parameters ---------- coords : list List of x,y coordinates defining a polygon Returns ------- float Minimum adjusted area """ pol = Polygon(coords) cmp_index = GenUtil.calculate_compactness_index(pol.area, pol.length) adj_area = GenUtil.calculate_adjusted_area(pol.area, cmp_index) return adj_area def load_features(self, geo_content, command): """Load the points, line strings and polygons in the spatial container. The Polygons are deconstructued into a list LineString with clockwise orientation and extra added information needed for the reconstruction of the original Polygon Parameters ---------- geo_content : DataClass Contains all the input#output geo spatial information command :ParserArgument Contains the parameters of the command line interface Returns ------- None """ features = [] # List of features to pass to the spatial container # Load all the features in the spatial container for feature in geo_content.in_features: diameter = command.dlayer_dict[feature.sb_layer_name] min_adj_area = self.calculate_min_adj_area(diameter) if feature.geom_type == GenUtil.POINT: out_feature = PointSb(feature.coords, feature.sb_layer_name, feature.sb_properties) # Add the feature features.append(out_feature) elif feature.geom_type == GenUtil.LINE_STRING: out_feature = out_feature = LineStringSb(feature.coords, GenUtil.LINE_STRING, min_adj_area, feature.sb_layer_name, feature.sb_properties) # Add the feature features.append(out_feature) elif feature.geom_type == GenUtil.POLYGON: adj_area = self._calculate_adj_area(feature.exterior.coords) # Only keep the polygon over the minimum adjusted area if not command.exclude_polygon or adj_area > min_adj_area: # Deconstruct the Polygon into a list of LineString with supplementary information # needed to reconstruct the original Polygon ext_feature = LineStringSb(feature.exterior.coords, GenUtil.POLYGON_EXTERIOR, min_adj_area, feature.sb_layer_name, feature.sb_properties) interiors = feature.interiors int_features = [] # Extract the interiors as LineString for interior in interiors: adj_area = self._calculate_adj_area(interior.coords) # Only keep the interior (hole) over the minimal adjusted area if not command.exclude_hole or adj_area > min_adj_area: interior = LineStringSb(interior.coords, GenUtil.POLYGON_INTERIOR, min_adj_area, None, None) int_features.append(interior) else: geo_content.nbr_del_holes += len(feature.interiors) #Add interior features needed for Polygon reconstruction ext_feature.sb_interiors = int_features # Add the exterior and the interior independently features.append(ext_feature) # Add the exterior features += int_features # Add the interiors else: # Do not add the feature (exterior and interiors ) in the spatial container # Update some stats geo_content.nbr_del_polygons += 1 geo_content.nbr_del_holes += len(feature.interiors) else: raise GeoSimException ("Invalid geometry type: {}".format(feature.geometry)) # Create the spatial container that will receive all the spatial features self.s_container = SpatialContainer() self.s_container.add_features(features) # Load all the features return def _manage_lines_simplification (self, s_constraints): """Main routine to simplify the lines For each line to simplify For each valid bend to simplify check the consraints if the constraint are violated check alternative bends (only if the number of bend to simplify is one. One of the costly operation specially for very long line string (like contour) is to rewrite the coordinates into the Shapely structure. This is why we updtade the shapely structure at the end when the last bend of the line is processed Parameters ---------- s_constraints : SpatialContraints Spatal constraints to validate Returns ------- int Total number of bend simplified """ iter_nbr = 0 total_nbr_bend_simplified = 0 # Iterate until all the line are simplified or there are no more line have to be simplified while (True): iter_nbr_bend_simplified = 0 print('Iteration # {}'.format(iter_nbr)) # Build line iterator lines = (feature for feature in self.s_container.get_features() if(not feature.sb_is_simplest and feature.sb_geom_type==GenUtil.LINE_STRING )) for line in lines: nbr_bend_simplified = line.simplify(self.command.diameter, s_constraints) iter_nbr_bend_simplified += nbr_bend_simplified total_nbr_bend_simplified += nbr_bend_simplified print('Number of bend simplified {}'.format(iter_nbr_bend_simplified)) print('----------') iter_nbr += 1 if iter_nbr_bend_simplified == 0: break print('Total number of bend simplified: {}'.format(total_nbr_bend_simplified)) print('Total number of simplicity error: {}'.format(s_constraints.nbr_err_simplicity)) print('Total number of crossing error: {}'.format(s_constraints.nbr_err_crossing)) print('Total number of sidedness error: {}'.format(s_constraints.nbr_err_sidedness)) return total_nbr_bend_simplified def process(self): """Main routine for the Sherbend algorithm The algorithm will simplify the lines using the Sherbend algorithm. It will iterate over the lines until there are no more bends to simplify. Parameters ---------- None Returns ------- geo_content : DataClass Contains the output information """ # Load the features into the spatial container self.load_features(self.geo_content, self.command) s_constraints = SpatialConstraints(s_container=self.s_container) self._manage_lines_simplification(s_constraints) for feature in self.s_container.get_features(): if feature.sb_geom_type == GenUtil.POINT: self.geo_content.out_features.append(feature) elif feature.sb_geom_type == GenUtil.LINE_STRING: if feature.sb_original_type == GenUtil.LINE_STRING: self.geo_content.out_features.append(feature) else: if feature.sb_original_type == GenUtil.POLYGON_EXTERIOR: # The LineString was an exterior Polygon so reconstruct the originalPolygon interiors = [list(interior.coords) for interior in feature.sb_interiors] polygon = Polygon(feature.coords, interiors) polygon.sb_layer_name = feature.sb_layer_name polygon.sb_properties = feature.sb_properties self.geo_content.out_features.append(polygon) else: pass # Nothing to do with the holes here return
[ 37811, 1212, 11862, 23986, 262, 15233, 3611, 1634, 435, 23442, 342, 76, 351, 32315, 10627, 628, 220, 220, 220, 770, 11862, 7106, 6945, 3951, 13, 220, 632, 39382, 329, 1123, 1627, 262, 44370, 13, 220, 632, 16602, 262, 19396, 290, 198, ...
2.076764
10,148
#!/usr/bin/env python3 import socket port = 12345 MAX_SIZE = 65535 target_address = '127.0.0.1' s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((target_address,port)) s.listen(2) conn, addr = s.accept() # conn: socket is the client socket. print(addr, "Now Connected") text = "Thank you for connecting from TCP Server." data = text.encode('ascii') conn.send(data) conn.close()
[ 2, 48443, 14629, 14, 8800, 14, 24330, 21015, 18, 198, 198, 11748, 17802, 198, 198, 634, 796, 17031, 2231, 198, 22921, 62, 33489, 796, 45021, 2327, 198, 16793, 62, 21975, 796, 705, 16799, 13, 15, 13, 15, 13, 16, 6, 198, 198, 82, 79...
2.655405
148
from empyric.adapters import * from empyric.collection.instrument import *
[ 6738, 795, 9078, 1173, 13, 324, 12126, 1330, 1635, 198, 6738, 795, 9078, 1173, 13, 43681, 13, 259, 43872, 1330, 1635, 628, 628 ]
3.391304
23
# APACHE LICENSE # Copyright 2020 Stuart Paterson # # 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. # External Packages import os import discord from dotenv import load_dotenv # Local Files import utils # Create the bot load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') client = discord.Client() def get_channel_by_name(client, guild, name): """Returns a channel by name from a specific guild""" for server in client.guilds: if server == guild: for channel in server.text_channels: if channel.name == name: return channel # Run the bot client.run(TOKEN)
[ 2, 3486, 2246, 13909, 38559, 24290, 198, 2, 15069, 12131, 22559, 3208, 882, 198, 2, 198, 2, 49962, 739, 262, 24843, 13789, 11, 10628, 362, 13, 15, 357, 1169, 366, 34156, 15341, 198, 2, 345, 743, 407, 779, 428, 2393, 2845, 287, 11846...
3.10221
362
import argparse import json import requests import pathlib from urllib.parse import urlparse from auth import AWSSignatureV4 if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('api_endpoint', help='HTTPS endpoint for REST API') parser.add_argument('api_key') parser.add_argument('access_key_id') parser.add_argument('secret_access_key') parser.add_argument('--with_s3_kms', action='store_true') subparsers = parser.add_subparsers() invoice_prediction_parser = subparsers.add_parser('invoice_prediction') invoice_prediction_parser.add_argument('document_path', help='Path to document to make predictions on') invoice_prediction_parser.add_argument('content_type', choices={'image/jpeg', 'application/pdf'}, help='Content-Type of document to make predictions on') invoice_prediction_parser.add_argument('--consent_id', default='1234', help='Consent ID is typically a mapping from end user to a unique identifier') invoice_prediction_parser.set_defaults(cmd=invoice_prediction) receipt_prediction_parser = subparsers.add_parser('receipt_prediction') receipt_prediction_parser.add_argument('document_path', help='Path to document to make predictions on') receipt_prediction_parser.add_argument('content_type', choices={'image/jpeg', 'application/pdf'}, help='Content-Type of document to make predictions on') receipt_prediction_parser.add_argument('--consent_id', default='1234', help='Consent ID is typically a mapping from end user to a unique identifier') receipt_prediction_parser.set_defaults(cmd=receipt_prediction) document_split_parser = subparsers.add_parser('document_split') document_split_parser.add_argument('document_path', help='Path to document to split') document_split_parser.add_argument('content_type', choices={'application/pdf'}, help='Content-Type of document to split') document_split_parser.add_argument('--consent_id', default='1234', help='Consent ID is typically a mapping from end user to a unique identifier') document_split_parser.set_defaults(cmd=document_split) args = parser.parse_args() args.cmd()
[ 11748, 1822, 29572, 198, 11748, 33918, 198, 11748, 7007, 198, 11748, 3108, 8019, 198, 198, 6738, 2956, 297, 571, 13, 29572, 1330, 19016, 29572, 198, 6738, 6284, 1330, 14356, 5432, 570, 1300, 53, 19, 628, 628, 628, 628, 628, 198, 198, ...
2.609914
928