content
stringlengths
0
894k
type
stringclasses
2 values
import pytest import numpy.testing as npt from xbout.load import _auto_open_mfboutdataset class TestAccuracyAgainstOldCollect: @pytest.mark.skip def test_single_file(self): from boutdata import collect var = 'n' expected = collect(var, path='./tests/data/dump_files/single', prefix='equilibrium', xguards=False) ds, metadata = _auto_open_mfboutdataset('./tests/data/dump_files/single/equilibrium.nc') print(ds) actual = ds[var].values assert expected.shape == actual.shape npt.assert_equal(actual, expected) @pytest.mark.skip def test_multiple_files_along_x(self): from boutdata import collect var = 'n' expected = collect(var, path='./tests/data/dump_files/', prefix='BOUT.dmp', xguards=False) ds, metadata = _auto_open_mfboutdataset('./tests/data/dump_files/BOUT.dmp.*.nc') actual = ds[var].values assert expected.shape == actual.shape npt.assert_equal(actual, expected) @pytest.mark.skip def test_multiple_files_along_x(self): ... @pytest.mark.skip def test_metadata(self): ... @pytest.mark.skip class test_speed_against_old_collect: ...
python
import numpy as np import logging logger = logging.getLogger(__name__) def create_model(args, initial_mean_value, overal_maxlen, vocab): import keras.backend as K from keras.layers.embeddings import Embedding from keras.models import Sequential, Model from keras.layers.core import Dense, Dropout, Activation from nea.my_layers import Attention, MeanOverTime, Conv1DWithMasking ############################################################################################################################### ## Recurrence unit type # if args.recurrent_unit == 'lstm': from keras.layers.recurrent import LSTM as RNN elif args.recurrent_unit == 'gru': from keras.layers.recurrent import GRU as RNN elif args.recurrent_unit == 'simple': from keras.layers.recurrent import SimpleRNN as RNN ############################################################################################################################### ## Create Model # dropout_W = 0.5 # default=0.5 dropout_U = 0.1 # default=0.1 cnn_border_mode='same' if initial_mean_value.ndim == 0: initial_mean_value = np.expand_dims(initial_mean_value, axis=1) num_outputs = len(initial_mean_value) if args.model_type == 'cls': raise NotImplementedError elif args.model_type == 'reg': logger.info('Building a REGRESSION model') model = Sequential() model.add(Embedding(args.vocab_size, args.emb_dim, mask_zero=True)) if args.cnn_dim > 0: model.add(Conv1DWithMasking(nb_filter=args.cnn_dim, filter_length=args.cnn_window_size, border_mode=cnn_border_mode, subsample_length=1)) if args.rnn_dim > 0: model.add(RNN(args.rnn_dim, return_sequences=False, dropout_W=dropout_W, dropout_U=dropout_U)) if args.dropout_prob > 0: model.add(Dropout(args.dropout_prob)) model.add(Dense(num_outputs)) if not args.skip_init_bias: bias_value = (np.log(initial_mean_value) - np.log(1 - initial_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) model.add(Activation('sigmoid')) model.emb_index = 0 elif args.model_type == 'regp': logger.info('Building a REGRESSION model with POOLING') model = Sequential() model.add(Embedding(args.vocab_size, args.emb_dim, mask_zero=True)) if args.cnn_dim > 0: model.add(Conv1DWithMasking(nb_filter=args.cnn_dim, filter_length=args.cnn_window_size, border_mode=cnn_border_mode, subsample_length=1)) if args.rnn_dim > 0: model.add(RNN(args.rnn_dim, return_sequences=True, dropout_W=dropout_W, dropout_U=dropout_U)) if args.dropout_prob > 0: model.add(Dropout(args.dropout_prob)) if args.aggregation == 'mot': model.add(MeanOverTime(mask_zero=True)) elif args.aggregation.startswith('att'): model.add(Attention(op=args.aggregation, activation='tanh', init_stdev=0.01)) model.add(Dense(num_outputs)) if not args.skip_init_bias: bias_value = (np.log(initial_mean_value) - np.log(1 - initial_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) model.add(Activation('sigmoid')) model.emb_index = 0 elif args.model_type == 'breg': logger.info('Building a BIDIRECTIONAL REGRESSION model') from keras.layers import Dense, Dropout, Embedding, LSTM, Input, merge model = Sequential() sequence = Input(shape=(overal_maxlen,), dtype='int32') output = Embedding(args.vocab_size, args.emb_dim, mask_zero=True)(sequence) if args.cnn_dim > 0: output = Conv1DWithMasking(nb_filter=args.cnn_dim, filter_length=args.cnn_window_size, border_mode=cnn_border_mode, subsample_length=1)(output) if args.rnn_dim > 0: forwards = RNN(args.rnn_dim, return_sequences=False, dropout_W=dropout_W, dropout_U=dropout_U)(output) backwards = RNN(args.rnn_dim, return_sequences=False, dropout_W=dropout_W, dropout_U=dropout_U, go_backwards=True)(output) if args.dropout_prob > 0: forwards = Dropout(args.dropout_prob)(forwards) backwards = Dropout(args.dropout_prob)(backwards) merged = merge([forwards, backwards], mode='concat', concat_axis=-1) densed = Dense(num_outputs)(merged) if not args.skip_init_bias: raise NotImplementedError score = Activation('sigmoid')(densed) model = Model(input=sequence, output=score) model.emb_index = 1 elif args.model_type == 'bregp': logger.info('Building a BIDIRECTIONAL REGRESSION model with POOLING') from keras.layers import Dense, Dropout, Embedding, LSTM, Input, merge model = Sequential() sequence = Input(shape=(overal_maxlen,), dtype='int32') output = Embedding(args.vocab_size, args.emb_dim, mask_zero=True)(sequence) if args.cnn_dim > 0: output = Conv1DWithMasking(nb_filter=args.cnn_dim, filter_length=args.cnn_window_size, border_mode=cnn_border_mode, subsample_length=1)(output) if args.rnn_dim > 0: forwards = RNN(args.rnn_dim, return_sequences=True, dropout_W=dropout_W, dropout_U=dropout_U)(output) backwards = RNN(args.rnn_dim, return_sequences=True, dropout_W=dropout_W, dropout_U=dropout_U, go_backwards=True)(output) if args.dropout_prob > 0: forwards = Dropout(args.dropout_prob)(forwards) backwards = Dropout(args.dropout_prob)(backwards) forwards_mean = MeanOverTime(mask_zero=True)(forwards) backwards_mean = MeanOverTime(mask_zero=True)(backwards) merged = merge([forwards_mean, backwards_mean], mode='concat', concat_axis=-1) densed = Dense(num_outputs)(merged) if not args.skip_init_bias: raise NotImplementedError score = Activation('sigmoid')(densed) model = Model(input=sequence, output=score) model.emb_index = 1 logger.info(' Done') ############################################################################################################################### ## Initialize embeddings if requested # if args.emb_path: from w2vEmbReader import W2VEmbReader as EmbReader logger.info('Initializing lookup table') emb_reader = EmbReader(args.emb_path, emb_dim=args.emb_dim) model.layers[model.emb_index].W.set_value(emb_reader.get_emb_matrix_given_vocab(vocab, model.layers[model.emb_index].W.get_value())) logger.info(' Done') return model
python
from plugins import * # Importing all the plugins from plugins/ folder from settings_base import BaseSettings # Importing base settings class BotSettings(BaseSettings): # See README.md for details! USERS = ( ("user", "ТУТ ТОКЕН ПОЛЬЗОВАТЕЛЯ",), ) # Default settings for plugins DEFAULTS["PREFIXES"] = DEFAULT_PREFIXES = ("/",) DEFAULTS["ADMINS"] = DEFAULT_ADMINS = (87641997, ) # You can setup plugins any way you like. See plugins's classes and README.md. # All available plugins can be found in folder `plugins` or in file `PLUGINS.md`. # Bot will use all plugins inside PLUGINS variable. help_plugin = HelpPlugin("помощь", "команды", "?", prefixes=DEFAULT_PREFIXES) # List of active plugins PLUGINS = ( StoragePlugin(in_memory=True, save_to_file=True), StaffControlPlugin(prefixes=DEFAULT_PREFIXES, admins=DEFAULT_ADMINS, set_admins=True), ChatMetaPlugin(), UserMetaPlugin(), StatisticsPlugin(), VoterPlugin(prefixes=DEFAULT_PREFIXES), FacePlugin("сделай", prefixes=DEFAULT_PREFIXES), SmileWritePlugin(), JokePlugin(), GraffitiPlugin(), QuoteDoerPlugin(), WikiPlugin(), AnagramsPlugin(), MembersPlugin(), PairPlugin(), WhoIsPlugin(), YandexNewsPlugin(), AboutPlugin(), BirthdayPlugin(), TimePlugin(), MemeDoerPlugin(), QRCodePlugin(), ChatKickerPlugin(admins_only=True), RandomPostPlugin({"kitties": -145935681, "random": -111759315, "savehouse": -96322217, "octavia": -36007583}), CalculatorPlugin(), VideoPlugin(), DispatchPlugin(), NamerPlugin(), help_plugin, # Needs tokens (see plugin's codes, some have defaults): SayerPlugin(), # Plugins for bot's control AntifloodPlugin(), NoQueuePlugin(), CommandAttacherPlugin(), ForwardedCheckerPlugin(), ) help_plugin.add_plugins(PLUGINS)
python
# -*- coding: utf-8 -*- # !/usr/bin/env python from abc import ABC from settings import config from peewee import SqliteDatabase, MySQLDatabase, Model class SqliteFKDatabase(SqliteDatabase, ABC): def initialize_connection(self, conn): self.execute_sql('PRAGMA foreign_keys=ON;') db = MySQLDatabase(host=config.DB_HOST, user=config.DB_USER, passwd=config.DB_PASSWORD, database=config.DB_NAME, charset='utf8') class BaseModel(Model): class Meta: database = db
python
################################################################################ # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ################################################################################ import json import logging import sys from pyflink.table import (EnvironmentSettings, TableEnvironment, DataTypes, TableDescriptor, Schema) from pyflink.table.udf import udf def process_json_data_with_udf(): t_env = TableEnvironment.create(EnvironmentSettings.in_streaming_mode()) # define the source table = t_env.from_elements( elements=[ (1, '{"name": "Flink", "tel": 123, "addr": {"country": "Germany", "city": "Berlin"}}'), (2, '{"name": "hello", "tel": 135, "addr": {"country": "China", "city": "Shanghai"}}'), (3, '{"name": "world", "tel": 124, "addr": {"country": "USA", "city": "NewYork"}}'), (4, '{"name": "PyFlink", "tel": 32, "addr": {"country": "China", "city": "Hangzhou"}}') ], schema=['id', 'data']) # define the sink t_env.create_temporary_table( 'sink', TableDescriptor.for_connector('print') .schema(Schema.new_builder() .column('id', DataTypes.BIGINT()) .column('data', DataTypes.STRING()) .build()) .build()) # update json columns @udf(result_type=DataTypes.STRING()) def update_tel(data): json_data = json.loads(data) json_data['tel'] += 1 return json.dumps(json_data) table = table.select(table.id, update_tel(table.data)) # execute table.execute_insert('sink') \ .wait() # remove .wait if submitting to a remote cluster, refer to # https://nightlies.apache.org/flink/flink-docs-stable/docs/dev/python/faq/#wait-for-jobs-to-finish-when-executing-jobs-in-mini-cluster # for more details if __name__ == '__main__': logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s") process_json_data_with_udf()
python
from astroquery import alfa # Test Case: A Seyfert 1 galaxy RA = '0h8m05.63s' DEC = '14d50m23.3s' def test_alfa_catalog(): cat = alfa.get_catalog() def test_alfa_spectrum(): sp = alfa.get_spectrum(ra=RA, dec=DEC, counterpart=True) if __name__ == '__main__': test_alfa_catalog() test_alfa_spectrum()
python
from PyQt5.QtWidgets import QMainWindow,QMessageBox from PyQt5.QtGui import QImage,QPixmap from istanbul_city_surveillance_cameras_Gui_python import Ui_MainWindow from src.camera_list import selected_camera from src.yolov4_pred import YOLOv4 import os import time import cv2 class istanbul_city_surveillance_cameras(QMainWindow): def __init__(self): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.pushButton.clicked.connect(self.start_predict) self.ui.comboBox_2.currentIndexChanged[int].connect(self.select_camera) self.ui.pushButton_2.clicked.connect(self.page_menu) self.ui.stackedWidget.setCurrentIndex(0) def page_menu(self): self.ui.stackedWidget.setCurrentIndex(0) self.stop = False #==================== Tespit etmek istediğiniz kamera bölgesi seçiliyor ========================== def select_camera(self,index): if index != 0: self.camera_index = index self.camera_name = self.ui.comboBox_2.itemText(index) self.url_cam = selected_camera(self.camera_index) print('url adresi',self.url_cam) #=========== işlenmiş görüntüyü göster ================================ def show_images_area(self,img): geo = self.ui.label_mobese.geometry() w,h = geo.getRect()[2:] image = cv2.resize(img,(w,h)) frame = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) image = QImage(frame,frame.shape[1],frame.shape[0],frame.strides[0],QImage.Format_RGB888) self.ui.label_mobese.setPixmap(QPixmap.fromImage(image)) #=================== Seçilen mobese üzerinden predict yapmaya başlar ============================ def start_predict(self): self.ui.stackedWidget.setCurrentIndex(1) self.stop = True #======= Yolov4 ağırlıkları yüklenmektedir ======================== weightsPath = "yolo_weight/yolov4-obj.weights" configPath = "yolo_weight/yolov4-obj.cfg" net = cv2.dnn.readNet(weightsPath, configPath) net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16) cnt=0 frames_to_count=20 st=0 vid = cv2.VideoCapture(self.url_cam) if vid.isOpened() == False: QMessageBox.about(self,'Error','Can not connect camera. Turn back menu page') #============ kamera açıldıysa görüntüyü aktar =========== while(vid.isOpened()): ret,frame = vid.read() predicted_img,car_count,people_count = YOLOv4(net,frame) if car_count>20 : car_crowd = 'Crowded' else: car_crowd = 'Normal' if people_count>20 : people_crowd = 'Crowded' else: people_crowd = 'Normal' text = f'Predictions on {self.camera_name} camera People count: {people_count} -- {people_crowd} , Car count: {car_count} -- {car_crowd} ' self.ui.label.setText(text) if cnt == frames_to_count: try: print(frames_to_count/(time.time()-st),'FPS') fps = round(frames_to_count/(time.time()-st)) st = time.time() cnt=0 except: pass cnt+=1 self.show_images_area(predicted_img) '''cv2.namedWindow('predict_video',cv2.WINDOW_NORMAL) cv2.resizeWindow('predict_video',1400,750) cv2.imshow("predict_video",frame)''' if cv2.waitKey(1) & self.stop == False: self.ui.label_mobese.clear() break vid.release() cv2.destroyAllWindows()
python
# Copyright 2018 The trfl Authors. 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. # ============================================================================ """Unit tests for discrete-action Policy Gradient functions.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function # Dependency imports from absl.testing import parameterized import numpy as np from six.moves import xrange # pylint: disable=redefined-builtin import tensorflow.compat.v1 as tf import tree as nest from trfl import discrete_policy_gradient_ops as pg_ops class EntropyCostTest(parameterized.TestCase, tf.test.TestCase): """Tests for discrete_policy_entropy op.""" @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) def testEntropy(self, is_multi_actions): with self.test_session() as sess: # Large values check numerical stability through the logs policy_logits_np = np.array([[0, 1], [1, 2], [0, 2], [1, 1], [0, -1000], [0, 1000]]) if is_multi_actions: num_action_components = 3 policy_logits_nest = [tf.constant(policy_logits_np, dtype=tf.float32) for _ in xrange(num_action_components)] else: num_action_components = 1 policy_logits_nest = tf.constant(policy_logits_np, dtype=tf.float32) entropy_op = pg_ops.discrete_policy_entropy_loss(policy_logits_nest) entropy = entropy_op.extra.entropy self.assertEqual(entropy.get_shape(), tf.TensorShape(6)) # Get these reference values in Torch with: # c = nnd.EntropyCriterion() # s = nn.LogSoftMax() # result = c:forward(s:forward(logits)) expected_entropy = num_action_components * np.array( [0.58220309, 0.58220309, 0.36533386, 0.69314718, 0, 0]) self.assertAllClose(sess.run(entropy), expected_entropy, atol=1e-4) @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) def testGradient(self, is_multi_actions): with self.test_session() as sess: policy_logits_np = np.array([[0, 1], [1, 2], [0, 2], [1, 1], [0, -1000], [0, 1000]]) if is_multi_actions: num_action_components = 3 policy_logits_nest = [tf.constant(policy_logits_np, dtype=tf.float32) for _ in xrange(num_action_components)] else: num_action_components = 1 policy_logits_nest = tf.constant(policy_logits_np, dtype=tf.float32) entropy_op = pg_ops.discrete_policy_entropy_loss(policy_logits_nest) entropy = entropy_op.extra.entropy # Counterintuitively, the gradient->0 as policy->deterministic, that's why # the gradients for the large logit cases are `[0, 0]`. They should # strictly be >0, but they get truncated when we run out of precision. expected_gradients = np.array([[0.1966119, -0.1966119], [0.1966119, -0.1966119], [0.2099872, -0.2099872], [0, 0], [0, 0], [0, 0]]) for policy_logits in nest.flatten(policy_logits_nest): gradients = tf.gradients(entropy, policy_logits) grad_policy_logits = sess.run(gradients[0]) self.assertAllClose(grad_policy_logits, expected_gradients, atol=1e-4) @parameterized.named_parameters(('TwoActions', 2), ('FiveActions', 5), ('TenActions', 10), ('MixedMultiActions', [2, 5, 10])) def testNormalisation(self, num_actions): with self.test_session() as sess: if isinstance(num_actions, list): policy_logits = [tf.constant([[1.0] * n], dtype=tf.float32) for n in num_actions] else: policy_logits = tf.constant( [[1.0] * num_actions], dtype=tf.float32) entropy_op = pg_ops.discrete_policy_entropy_loss( policy_logits, normalise=True) self.assertAllClose(sess.run(entropy_op.loss), [-1.0]) @parameterized.named_parameters( ('Fixed', 5, 4, 3, False), ('DynamicLength', None, 4, 3, False), ('DynamicBatch', 5, None, 3, False), ('DynamicBatchAndLength', None, None, 3, False), ('DynamicAll', None, None, None, False), ('NormFixed', 5, 4, 3, True), ('NormDynamicLength', None, 4, 3, True), ('NormDynamicBatch', 5, None, 3, True), ('NormDynamicBatchAndLength', None, None, 3, True), ('NormDynamicAll', None, None, None, True)) def testShapeInference3D(self, sequence_length, batch_size, num_actions, normalise): T, B, A = sequence_length, batch_size, num_actions # pylint: disable=invalid-name op = pg_ops.discrete_policy_entropy_loss( policy_logits=tf.placeholder(tf.float32, shape=[T, B, A]), normalise=normalise) op.extra.entropy.get_shape().assert_is_compatible_with([T, B]) op.loss.get_shape().assert_is_compatible_with([T, B]) @parameterized.named_parameters( ('Fixed2D', 4, 3, False), ('DynamicBatch2D', None, 3, False), ('DynamicAll2D', None, None, False), ('NormFixed2D', 4, 3, True), ('NormDynamicBatch2D', None, 3, True), ('NormDynamicAll2D', None, None, True)) def testShapeInference2D(self, batch_size, num_actions, normalise): policy_logits = tf.placeholder(tf.float32, shape=[batch_size, num_actions]) op = pg_ops.discrete_policy_entropy_loss(policy_logits, normalise=normalise) op.extra.entropy.get_shape().assert_is_compatible_with([batch_size]) op.loss.get_shape().assert_is_compatible_with([batch_size]) @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) class DiscretePolicyGradientLossTest(parameterized.TestCase, tf.test.TestCase): """Tests for discrete_policy_gradient_loss op.""" def _setUpLoss(self, is_multi_actions): policy_logits_np = np.array([[[0, 1], [0, 1]], [[1, 1], [0, 100]]]) actions_np = np.array([[0, 0], [1, 1]], dtype=np.int32) if is_multi_actions: self._num_action_components = 3 self._policy_logits_nest = [ tf.constant(policy_logits_np, dtype=tf.float32) for _ in xrange(self._num_action_components)] self._actions_nest = [tf.constant(actions_np, dtype=tf.int32) for _ in xrange(self._num_action_components)] else: self._num_action_components = 1 self._policy_logits_nest = tf.constant(policy_logits_np, dtype=tf.float32) self._actions_nest = tf.constant(actions_np, dtype=tf.int32) self._action_values = tf.constant([[0, 1], [2, 1]], dtype=tf.float32) self._loss = pg_ops.discrete_policy_gradient_loss( self._policy_logits_nest, self._actions_nest, self._action_values) def testLoss(self, is_multi_actions): self._setUpLoss(is_multi_actions) with self.test_session() as sess: self.assertEqual(self._loss.get_shape(), tf.TensorShape(2)) # [B] self.assertAllClose( sess.run(self._loss), # computed by summing expected losses from DiscretePolicyGradientTest # over the two sequences of length two which I've split the batch # into: self._num_action_components * np.array([1.386294, 1.313262])) def testGradients(self, is_multi_actions): self._setUpLoss(is_multi_actions) with self.test_session() as sess: total_loss = tf.reduce_sum(self._loss) gradients = tf.gradients( [total_loss], nest.flatten(self._policy_logits_nest)) grad_policy_logits_nest = sess.run(gradients) for grad_policy_logits in grad_policy_logits_nest: self.assertAllClose(grad_policy_logits, [[[0, 0], [-0.731, 0.731]], [[1, -1], [0, 0]]], atol=1e-4) dead_grads = tf.gradients( [total_loss], nest.flatten(self._actions_nest) + [self._action_values]) for grad in dead_grads: self.assertIsNone(grad) class DiscretePolicyGradientTest(tf.test.TestCase): """Tests for discrete_policy_gradient op.""" def testLoss(self): with self.test_session() as sess: policy_logits = tf.constant([[0, 1], [0, 1], [1, 1], [0, 100]], dtype=tf.float32) action_values = tf.constant([0, 1, 2, 1], dtype=tf.float32) actions = tf.constant([0, 0, 1, 1], dtype=tf.int32) loss = pg_ops.discrete_policy_gradient(policy_logits, actions, action_values) self.assertEqual(loss.get_shape(), tf.TensorShape(4)) # Calculate the targets with: # loss = action_value*(-logits[action] + log(sum_a(exp(logits[a])))) # The final case (with large logits), runs out of precision and gets # truncated to 0, but isn't `nan`. self.assertAllClose(sess.run(loss), [0, 1.313262, 1.386294, 0]) def testGradients(self): with self.test_session() as sess: policy_logits = tf.constant([[0, 1], [0, 1], [1, 1], [0, 100]], dtype=tf.float32) action_values = tf.constant([0, 1, 2, 1], dtype=tf.float32) actions = tf.constant([0, 0, 1, 1], dtype=tf.int32) loss = pg_ops.discrete_policy_gradient(policy_logits, actions, action_values) total_loss = tf.reduce_sum(loss) gradients = tf.gradients([total_loss], [policy_logits]) grad_policy_logits = sess.run(gradients[0]) # The final case (with large logits), runs out of precision and gets # truncated to 0, but isn't `nan`. self.assertAllClose(grad_policy_logits, [[0, 0], [-0.731, 0.731], [1, -1], [0, 0]], atol=1e-4) self.assertAllEqual(tf.gradients([total_loss], [actions, action_values]), [None, None]) def testDynamicBatchSize(self): policy_logits = tf.placeholder(tf.float32, shape=[None, 3]) action_values = tf.placeholder(tf.float32, shape=[None]) actions = tf.placeholder(tf.int32, shape=[None]) loss = pg_ops.discrete_policy_gradient(policy_logits, actions, action_values) self.assertEqual(loss.get_shape().as_list(), [None]) gradients = tf.gradients(tf.reduce_sum(loss), [policy_logits]) self.assertAllEqual(gradients[0].get_shape().as_list(), [None, 3]) class SequenceAdvantageActorCriticLossTest(parameterized.TestCase, tf.test.TestCase): @parameterized.named_parameters( ('SingleActionEntropyNormalise', False, True), ('SingleActionNoEntropyNormalise', False, False), ('MultiActionsEntropyNormalise', True, True), ('MultiActionsNoEntropyNormalise', True, False), ) def testLossSequence(self, is_multi_actions, normalise_entropy): # A sequence of length 2, batch size 1, 3 possible actions. num_actions = 3 policy_logits = [[[0., 0., 1.]], [[0., 1., 0.]]] actions = [[0], [1]] baseline_values = [[0.2], [0.3]] rewards = [[0.4], [0.5]] pcontinues = [[0.9], [0.8]] bootstrap_value = [0.1] baseline_cost = 0.15 entropy_cost = 0.25 if is_multi_actions: num_action_components = 3 policy_logits_nest = [tf.constant(policy_logits, dtype=tf.float32) for _ in xrange(num_action_components)] actions_nest = [tf.constant(actions, dtype=tf.int32) for _ in xrange(num_action_components)] else: num_action_components = 1 policy_logits_nest = tf.constant(policy_logits, dtype=tf.float32) actions_nest = tf.constant(actions, dtype=tf.int32) loss, extra = pg_ops.sequence_advantage_actor_critic_loss( policy_logits_nest, tf.constant(baseline_values, dtype=tf.float32), actions_nest, tf.constant(rewards, dtype=tf.float32), tf.constant(pcontinues, dtype=tf.float32), tf.constant(bootstrap_value, dtype=tf.float32), baseline_cost=baseline_cost, entropy_cost=entropy_cost, normalise_entropy=normalise_entropy) # Manually calculate the discounted returns. return1 = 0.5 + 0.8 * 0.1 return0 = 0.4 + 0.9 * return1 with self.test_session() as sess: # Discounted returns self.assertAllClose(sess.run(extra.discounted_returns), [[return0], [return1]]) # Advantages advantages = [return0 - baseline_values[0][0], return1 - baseline_values[1][0]] self.assertAllClose(sess.run(extra.advantages), [[adv] for adv in advantages]) # Baseline expected_baseline_loss = baseline_cost*sum([0.5 * adv**2 for adv in advantages]) self.assertAllClose( sess.run(extra.baseline_loss), [expected_baseline_loss]) # Policy Gradient loss # loss = sum_t(action_value*(-logits[action] + # log(sum_a(exp(logits[a]))))) # # The below takes advantage of there only being one minibatch dim. normalise = lambda logits: np.log(np.exp(logits).sum()) batch = 0 expected_policy_gradient_loss = num_action_components * sum([ advantages[0]*(-(policy_logits[0][batch][actions[0][batch]]) + normalise(policy_logits[0])), advantages[1]*(-(policy_logits[1][batch][actions[1][batch]]) + normalise(policy_logits[1])), ]) self.assertAllClose(sess.run(extra.policy_gradient_loss), [expected_policy_gradient_loss]) # Entropy, calculated as per discrete_policy_entropy tests. expected_entropy = num_action_components*0.97533*2 expected_entropy_loss = -entropy_cost*expected_entropy if normalise_entropy: expected_entropy_loss /= (num_action_components * np.log(num_actions)) self.assertAllClose(sess.run(extra.entropy), [expected_entropy], atol=1e-4) self.assertAllClose(sess.run(extra.entropy_loss), [expected_entropy_loss], atol=1e-4) # Total loss expected_loss = [expected_entropy_loss + expected_policy_gradient_loss + expected_baseline_loss] self.assertAllClose(sess.run(loss), expected_loss, atol=1e-4) @parameterized.named_parameters(('Fixed', 5, 4, 3), ('DynamicLength', None, 4, 3), ('DynamicBatch', 5, None, 3), ('DynamicBatchAndLength', None, None, 3), ('DynamicAll', None, None, None)) def testShapeInference(self, sequence_length, batch_size, num_actions): T, B, A = sequence_length, batch_size, num_actions # pylint: disable=invalid-name loss, extra = pg_ops.sequence_advantage_actor_critic_loss( policy_logits=tf.placeholder(tf.float32, shape=[T, B, A]), baseline_values=tf.placeholder(tf.float32, shape=[T, B]), actions=tf.placeholder(tf.int32, shape=[T, B]), rewards=tf.placeholder(tf.float32, shape=[T, B]), pcontinues=tf.placeholder(tf.float32, shape=[T, B]), bootstrap_value=tf.placeholder(tf.float32, shape=[B]), entropy_cost=1) extra.discounted_returns.get_shape().assert_is_compatible_with([T, B]) extra.advantages.get_shape().assert_is_compatible_with([T, B]) extra.baseline_loss.get_shape().assert_is_compatible_with([B]) extra.policy_gradient_loss.get_shape().assert_is_compatible_with([B]) extra.entropy.get_shape().assert_is_compatible_with([B]) extra.entropy_loss.get_shape().assert_is_compatible_with([B]) loss.get_shape().assert_is_compatible_with([B]) @parameterized.named_parameters(('Fixed', 5, 4, 3), ('DynamicLength', None, 4, 3), ('DynamicBatch', 5, None, 3), ('DynamicBatchAndLength', None, None, 3), ('DynamicAll', None, None, None)) def testShapeInferenceGAE(self, sequence_length, batch_size, num_actions): T, B, A = sequence_length, batch_size, num_actions # pylint: disable=invalid-name loss, extra = pg_ops.sequence_advantage_actor_critic_loss( policy_logits=tf.placeholder(tf.float32, shape=[T, B, A]), baseline_values=tf.placeholder(tf.float32, shape=[T, B]), actions=tf.placeholder(tf.int32, shape=[T, B]), rewards=tf.placeholder(tf.float32, shape=[T, B]), pcontinues=tf.placeholder(tf.float32, shape=[T, B]), bootstrap_value=tf.placeholder(tf.float32, shape=[B]), lambda_=0.9, entropy_cost=1) extra.discounted_returns.get_shape().assert_is_compatible_with([T, B]) extra.advantages.get_shape().assert_is_compatible_with([T, B]) extra.baseline_loss.get_shape().assert_is_compatible_with([B]) extra.policy_gradient_loss.get_shape().assert_is_compatible_with([B]) extra.entropy.get_shape().assert_is_compatible_with([B]) extra.entropy_loss.get_shape().assert_is_compatible_with([B]) loss.get_shape().assert_is_compatible_with([B]) class SequenceAdvantageActorCriticLossGradientTest(parameterized.TestCase, tf.test.TestCase): def setUp(self): super(SequenceAdvantageActorCriticLossGradientTest, self).setUp() self.num_actions = 3 self.num_action_components = 5 policy_logits_np = np.array([[[0., 0., 1.]], [[0., 1., 0.]]]) self.policy_logits = tf.constant(policy_logits_np, dtype=tf.float32) self.multi_policy_logits = [tf.constant(policy_logits_np, dtype=tf.float32) for _ in xrange(self.num_action_components)] self.baseline_values = tf.constant([[0.2], [0.3]]) actions_np = np.array([[0], [1]]) actions = tf.constant(actions_np) multi_actions = [tf.constant(actions_np) for _ in xrange(self.num_action_components)] rewards = tf.constant([[0.4], [0.5]]) pcontinues = tf.constant([[0.9], [0.8]]) bootstrap_value = tf.constant([0.1]) baseline_cost = 0.15 entropy_cost = 0.25 self.op = pg_ops.sequence_advantage_actor_critic_loss( self.policy_logits, self.baseline_values, actions, rewards, pcontinues, bootstrap_value, baseline_cost=baseline_cost, entropy_cost=entropy_cost) self.multi_op = pg_ops.sequence_advantage_actor_critic_loss( self.multi_policy_logits, self.baseline_values, multi_actions, rewards, pcontinues, bootstrap_value, baseline_cost=baseline_cost, entropy_cost=entropy_cost) self.invalid_grad_inputs = [actions, rewards, pcontinues, bootstrap_value] self.invalid_grad_outputs = [None]*len(self.invalid_grad_inputs) @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) def testPolicyGradients(self, is_multi_actions): if is_multi_actions: loss = self.multi_op.extra.policy_gradient_loss policy_logits_nest = self.multi_policy_logits else: loss = self.op.extra.policy_gradient_loss policy_logits_nest = self.policy_logits grad_policy_list = [ tf.gradients(loss, policy_logits)[0] * self.num_actions for policy_logits in nest.flatten(policy_logits_nest)] for grad_policy in grad_policy_list: self.assertEqual(grad_policy.get_shape(), tf.TensorShape([2, 1, 3])) self.assertAllEqual(tf.gradients(loss, self.baseline_values), [None]) self.assertAllEqual(tf.gradients(loss, self.invalid_grad_inputs), self.invalid_grad_outputs) def testNonDifferentiableDiscountedReturns(self): self.assertAllEqual(tf.gradients(self.op.extra.discounted_returns, self.invalid_grad_inputs), self.invalid_grad_outputs) @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) def testEntropyGradients(self, is_multi_actions): if is_multi_actions: loss = self.multi_op.extra.entropy_loss policy_logits_nest = self.multi_policy_logits else: loss = self.op.extra.entropy_loss policy_logits_nest = self.policy_logits grad_policy_list = [ tf.gradients(loss, policy_logits)[0] * self.num_actions for policy_logits in nest.flatten(policy_logits_nest)] for grad_policy in grad_policy_list: self.assertEqual(grad_policy.get_shape(), tf.TensorShape([2, 1, 3])) self.assertAllEqual(tf.gradients(loss, self.baseline_values), [None]) self.assertAllEqual(tf.gradients(loss, self.invalid_grad_inputs), self.invalid_grad_outputs) def testBaselineGradients(self): loss = self.op.extra.baseline_loss grad_baseline = tf.gradients(loss, self.baseline_values)[0] self.assertEqual(grad_baseline.get_shape(), tf.TensorShape([2, 1])) self.assertAllEqual(tf.gradients(loss, self.policy_logits), [None]) self.assertAllEqual(tf.gradients(loss, self.invalid_grad_inputs), self.invalid_grad_outputs) @parameterized.named_parameters(('SingleAction', False), ('MultiActions', True)) def testTotalLossGradients(self, is_multi_actions): with self.test_session() as sess: if is_multi_actions: total_loss = tf.reduce_sum(self.multi_op.loss) policy_logits_nest = self.multi_policy_logits else: total_loss = tf.reduce_sum(self.op.loss) policy_logits_nest = self.policy_logits grad_policy_list = [ tf.gradients(total_loss, policy_logits)[0] for policy_logits in nest.flatten(policy_logits_nest)] grad_baseline = tf.gradients(total_loss, self.baseline_values)[0] for grad_policy in grad_policy_list: self.assertEqual(grad_policy.get_shape(), tf.TensorShape([2, 1, 3])) # These values were just generated once and hard-coded here to check for # regressions. Calculating by hand would be too time-consuming, # error-prone and unreadable. self.assertAllClose(sess.run(grad_policy), [[[-0.5995, 0.1224, 0.4770]], [[0.0288, -0.0576, 0.0288]]], atol=1e-4) self.assertEqual(grad_baseline.get_shape(), tf.TensorShape([2, 1])) self.assertAllClose(sess.run(grad_baseline), [[-0.1083], [-0.0420]], atol=1e-4) self.assertAllEqual(tf.gradients(total_loss, self.invalid_grad_inputs), self.invalid_grad_outputs) if __name__ == '__main__': tf.test.main()
python
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from msrest.serialization import Model class CacheExpirationActionParameters(Model): """Defines the parameters for the cache expiration action. Variables are only populated by the server, and will be ignored when sending a request. All required parameters must be populated in order to send to Azure. :ivar odatatype: Required. Default value: "Microsoft.Azure.Cdn.Models.DeliveryRuleCacheExpirationActionParameters" . :vartype odatatype: str :param cache_behavior: Required. Caching behavior for the requests that include query strings. Possible values include: 'BypassCache', 'Override', 'SetIfMissing' :type cache_behavior: str or ~azure.mgmt.cdn.models.enum :ivar cache_type: Required. The level at which the content needs to be cached. Default value: "All" . :vartype cache_type: str :param cache_duration: The duration for which the the content needs to be cached. Allowed format is [d.]hh:mm:ss :type cache_duration: str """ _validation = { 'odatatype': {'required': True, 'constant': True}, 'cache_behavior': {'required': True}, 'cache_type': {'required': True, 'constant': True}, } _attribute_map = { 'odatatype': {'key': '@odata\\.type', 'type': 'str'}, 'cache_behavior': {'key': 'cacheBehavior', 'type': 'str'}, 'cache_type': {'key': 'cacheType', 'type': 'str'}, 'cache_duration': {'key': 'cacheDuration', 'type': 'str'}, } odatatype = "Microsoft.Azure.Cdn.Models.DeliveryRuleCacheExpirationActionParameters" cache_type = "All" def __init__(self, *, cache_behavior, cache_duration: str=None, **kwargs) -> None: super(CacheExpirationActionParameters, self).__init__(**kwargs) self.cache_behavior = cache_behavior self.cache_duration = cache_duration
python
#!/usr/bin/env python import os from pathlib import Path import flash import matplotlib.pyplot as plt import numpy as np import pandas as pd import scipy as sp datadir = Path("/kaggle/input") from flash.core.utilities.imports import _ICEVISION_AVAILABLE from flash.image.data import IMG_EXTENSIONS, NP_EXTENSIONS, image_loader if _ICEVISION_AVAILABLE: from icevision.core.record import BaseRecord from icevision.core.record_components import ClassMapRecordComponent, FilepathRecordComponent, tasks from icevision.data.data_splitter import SingleSplitSplitter from icevision.parsers.parser import Parser else: assert 0, "with ice please" # In[108]: from pathlib import Path from icevision.all import * from icevision.core.record_defaults import KeypointsRecord class CustomBIWIKeypointsMetadata(KeypointsMetadata): labels = ["center"] # , "apex", "root"] class BiwiPNG: # TODO cache calibration for each subject (avoid loading for every frame) def load_keypoints(self, impath): name = str(impath)[:-8] pose = np.loadtxt(name + "_pose.txt") R = pose[:3, :3] # pose rotation from standard pose to this centre_biwi = pose[3, :] cal_rgb = os.path.join(os.path.split(name)[0], "rgb.cal") cal_rgb_P = np.eye(4) cal_rgb_P[:3, :3] = np.genfromtxt(cal_rgb, skip_header=5, skip_footer=2) cal_rgb_P[:3, 3] = np.genfromtxt(cal_rgb, skip_header=9, skip_footer=1) cal_rgb = np.genfromtxt(cal_rgb, skip_footer=6) def biwi2img(vec, camera_cal=True): if camera_cal: # RGB camera calibration x, y, z = cal_rgb_P[:3, :3] @ vec + cal_rgb_P[:3, 3] else: x, y, z = vec # BIWI world to image conversion # x <--> v # y <--> u # z == d v = x * cal_rgb[0, 0] / z + cal_rgb[0, 2] u = y * cal_rgb[1, 1] / z + cal_rgb[1, 2] return u, v centre = biwi2img(centre_biwi) # assuming the standard orientation of the nose is frontal upright, apex and root distance and directions are guesses dist = 50.0 apex = biwi2img(centre_biwi + dist * R @ np.array([0, 0, -1.0])) root = biwi2img( centre_biwi + dist / np.sqrt(2) * R @ np.array([0, -1.0, -1.0]) ) # guessed 45 degree angle towards root return {"center": centre, "apex": apex, "root": root} class CustomParser(Parser): def __init__(self, img_dir: Union[str, Path], imgID_annotations: Dict, idmap=None): super().__init__(template_record=self.template_record(), idmap=idmap) self.img_dir = Path(img_dir) self.class_map = ClassMap(CustomBIWIKeypointsMetadata().labels) self.annotations_dict = imgID_annotations def __iter__(self): yield from self.annotations_dict.items() def __len__(self): return len(self.annotations_dict) def template_record(self) -> BaseRecord: return KeypointsRecord() def record_id(self, o): return o[0] def filepath(self, o): return self.img_dir / o[0] def keypoints(self, o): return [ KeyPoints.from_xyv([x, y, 1], CustomBIWIKeypointsMetadata) for y, x in o[1] ] # TODO check coordinate flip def image_width_height(self, o) -> Tuple[int, int]: return get_img_size(self.filepath(o)) def labels(self, o) -> List[Hashable]: return list(range(1, len(CustomBIWIKeypointsMetadata().labels) + 1)) def bboxes(self, o) -> List[BBox]: w, h = get_img_size(self.filepath(o)) return [BBox.from_xywh(0, 0, w, h)] * (len(CustomBIWIKeypointsMetadata().labels)) def parse_fields(self, o, record, is_new): if is_new: record.set_filepath(self.filepath(o)) record.set_img_size(self.image_width_height(o)) record.detection.set_class_map(self.class_map) record.detection.add_labels_by_id(self.labels(o)) record.detection.add_bboxes(self.bboxes(o)) record.detection.add_keypoints(self.keypoints(o)) def parser(data_dir: Path): images = sorted(Path(data_dir).glob("??/frame_*_rgb.png"))[:100] # TODO remove truncation imgID_annotations = {} biwi = BiwiPNG() for im in images: keypoints = biwi.load_keypoints(im) imgID_annotations[str(im.relative_to(data_dir))] = [keypoints["center"]] # TODO add other keypoints return CustomParser(img_dir=data_dir, imgID_annotations=imgID_annotations) if True: p = parser(datadir) p.parse() for s in p: break r = KeypointsRecord() p.parse_fields(s, r, True) for kp in p.keypoints(s): print(kp.xyv) print(s, r) # In[109]: from flash.image import KeypointDetectionData, KeypointDetector datamodule = KeypointDetectionData.from_icedata(parser=parser, train_folder=datadir, batch_size=8) model = KeypointDetector( head="keypoint_rcnn", backbone="resnet18_fpn", num_keypoints=3, num_classes=3, ) trainer = flash.Trainer(max_epochs=2, gpus=1) trainer.finetune(model, datamodule=datamodule, strategy="freeze") # In[110]: sample = datamodule.train_dataset[0] sample # In[111]: from flash.core.data.io.input import DataKeys plt.imshow(sample[DataKeys.INPUT]) plt.scatter( sample[DataKeys.TARGET]["keypoints"][0][0]["x"], sample[DataKeys.TARGET]["keypoints"][0][0]["y"], marker="+" ) sample # In[ ]: # In[ ]: # In[ ]:
python
# -*- coding: utf-8 -*- ''' Das eigentliche starten der app wird über run erledigt ''' __author__ = "R. Bauer" __copyright__ = "MedPhyDO - Machbarkeitsstudien des Instituts für Medizinische Strahlenphysik und Strahlenschutz am Klinikum Dortmund im Rahmen von Bachelor und Masterarbeiten an der TU-Dortmund / FH-Dortmund" __credits__ = ["R.Bauer", "K.Loot"] __license__ = "MIT" __version__ = "0.1.5" __status__ = "Prototype" import logging from isp.config import ispConfig from app.db import gqadb from app.api import gqa from isp.webapp import ispBaseWebApp from isp.safrs import db, system class system( system ): @classmethod def _extendedSystemCheck(self): """filled Stub Function for api_list (Systeminformationen) Returns ------- dict, string """ import os import json def checkPath( path, info ): html = "" if os.path.exists( path ): info_class = "success" info_text = "{} ist vorhanden.".format( info ) else: info_class = "danger" info_text = "{} ist nicht vorhanden.".format( info ) html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) if os.path.isdir( path ): info_class = "success" info_text = "{} ist ein Verzeichnis.".format( info ) else: info_class = "danger" info_text = "{} ist kein Verzeichnis.".format( info ) html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) if os.access(path, os.W_OK): info_class = "success" info_text = "{} ist beschreibbar.".format( info ) else: info_class = "danger" info_text = "{} ist nicht beschreibbar.".format( info ) html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) return html config = ispConfig() html = "<h4>System Check</h4>" from isp.config import dict_merge # --------------- Aria Datenbank from app.aria import ariaClass _database_key = config.get( "database.servername", "" ) connect = None aria = None loaded_config = config.get( ["database", _database_key] ) db_config = { "dbname":"notset", "host":"notset/unused", "user":"notset", "password":"notset", "engine":"notset/unused", "dsn":"notset/unused" } if loaded_config: db_config = dict_merge( db_config, loaded_config.toDict() ) aria = ariaClass( _database_key, config ) connect = aria.openDatabase( _database_key ) else: html += '<div class="alert alert-danger">Kein passender Eintrag für <b>server.database.servername</b></div>' html += '<div class="alert alert-dark" >Prüfe Datenbankzugriff <span class="badge badge-info">database.servername</span>: <b>{}</b> - Konfiguration:'.format( _database_key ) db_config_copy = db_config.copy() db_config_copy["password"] = "******" html += '</br> <pre>{}</pre>'.format( json.dumps( db_config_copy, indent=2 ) ) info_text = "Der Zugriff auf die Datenbank dbname:<b>{dbname}</b>, user:<b>{user}</b>".format( **db_config ) if not connect: info_class = "danger" info_text = "{} ist nicht möglich.".format( info_text ) else: info_class = "success" info_text = "{} ist möglich.".format( info_text ) db_version = aria.getDbVersion( ) info_text += "<br>{}".format( db_version.replace( "\\n", "<br>").replace('\\t', '') ) html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) if connect: html += '<div class="alert alert-dark" >Prüfe Patienten für <span class="badge badge-info">units</span> - Konfiguration:' html += '</br> <pre>{}</pre>'.format( json.dumps( config.get( "units" ).toDict(), indent=2 ) ) for name, unit in config.get( "units" ).items(): sql = "SELECT PatientSer, PatientId, FirstName, LastName FROM [{dbname}].[dbo].[Patient] [Patient]" sql = sql + " WHERE [PatientId] = '{}' ".format( name ) result = aria.execute( sql ) html += aria.lastExecuteSql info_text = "PatientId: <b>{}</b>".format( name ) if len( result ) > 0: info_text = "{} ist vorhanden.".format( info_text ) info_text += '</br> <pre>{}</pre>'.format( json.dumps( result[0], indent=2 ) ) info_class = "success" else: info_text = "{} ist nicht vorhanden.".format( info_text ) info_class = "danger" html += '<div class="alert alert-{} ">{}'.format( info_class, info_text ) if len( result ) > 0: html += "<br>Prüfe Tags im Datenbankfeld '[Radiation].[Comment]' für PatientId: <b>{}</b> ".format( name ) tags = aria.getTags( name ) if tags and len( tags ) > 0: info_text = "<b>{}</b> Tags sind vorhanden.".format( len( tags ) ) info_class = "success" else: info_text = "Keine Tags vorhanden." info_class = "danger" html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) html += "</div>" html += "</div>" html += "</div>" # --------------- DICOM from app.ariadicom import ariaDicomClass _dicom_key = config.get( "dicom.servername", "" ) adc = ariaDicomClass( _database_key, _dicom_key, config ) loaded_config = config.get( ["dicom", _dicom_key] ) dicom_config = { "aec" : "notset", "server_ip": "notset", "server_port": "notset", "aet": "notset", "listen_port": "notset" } if loaded_config: dicom_config = dict_merge(dicom_config, loaded_config.toDict() ) else: html += '<div class="alert alert-danger">Kein passender Eintrag für <b>server.dicom.servername</b></div>' html += '<div class="alert alert-dark" >Prüfe Dicom <span class="badge badge-info">dicom.servername</span>: <b>{}</b> - Konfiguration:'.format( _dicom_key ) html += '<pre>{}</pre>'.format( json.dumps( dicom_config, indent=2 ) ) html += '<br>Server Settings - AE Title (aec): <b>{aec}</b> - IP (server_ip): <b>{server_ip}</b> - Port (server_port): <b>{server_port}</b><br>'.format( **dicom_config ) html += '<br>Application Entity Map Entry - AE Title (aet): <b>{aet}</b> - Port (listen_port): <b>{listen_port}</b>'.format( **dicom_config ) html += '<div class="alert alert-dark" >Prüfe Verzeichnis: <span class="badge badge-info">dicom.{}.local_dir</span>'.format( _dicom_key ) html += checkPath( dicom_config.get("local_dir", "notset" ) , '<span class="badge badge-info">dicom.{}.local_dir</span>'.format(_dicom_key)) html += "</div>" status = adc.initAE() dicom_info = adc.getInfo() adc.closeAE() if status == 0x0000: info_class = "success" info_text = "Dicom Zugriff ist möglich. Associations: " for association in dicom_info["associations"]: association["ae_title"] = association["ae_title"].decode().strip() info_text += '</br> <pre>{}</pre>'.format( json.dumps( association, indent=2 ) ) else: info_class = "danger" info_text = "Dicom Zugriff ist nicht möglich. ErrorCode: 0x{0:04x}.".format( status ) html += '<div class="alert alert-{} ">{}</div>'.format( info_class, info_text ) html += "</div>" # --------------- resultsPath resultsPath = adc.initResultsPath() html += '<div class="alert alert-dark" >Prüfe <span class="badge badge-info">resultsPath</span>: <b>{}</b>'.format( resultsPath ) html += checkPath(resultsPath, '<span class="badge badge-info">resultsPath</span>') html += "</div>" # --------------- MQTT mqtt_config = config.get( "server.mqtt" ) mqtt_config_copy = mqtt_config.copy() mqtt_config_copy.password = "********" if mqtt_config_copy.get("host", "") == "": html += '<div class="alert alert-info" >MQTT deaktiviert' else: html += '<div class="alert alert-dark" >Prüfe <span class="badge badge-info">server.mqtt</span> - Konfiguration:' html += '<pre>{}</pre>'.format( json.dumps( mqtt_config_copy.toDict(), indent=2 ) ) mqtt = config.mqttGetHandler() if not mqtt: info_class = "danger" info_text = "MQTT Zugriff ist nicht möglich." else: info_class = "info" info_text = 'MQTT Zugriff ist eingerichtet. <button type="button" class="btn btn-primary" onClick="mqttTest( this )">Prüfen</button>' html += '<div id="MQTT-checkline" class="alert alert-{} ">{}<div id="MQTT-results" class"alert"></div></div>'.format( info_class, info_text ) html += "</div>" html += ''' <script> var box = document.querySelector("#MQTT-checkline"); var result_box = document.querySelector("#MQTT-results"); if ( typeof app.clientMqtt === "object" ) { app.clientMqtt.subscribe( "MQTT/test", function( msg ) { box.className = "alert alert-success"; result_box.className = "alert alert-success"; result_box.innerHTML = "MQTT Test erfolgreich"; } ); } function mqttTest( btn ){ box.className = "alert alert-info"; result_box.className = ""; if ( typeof app.clientMqtt === "object" ) { result_box.className = "alert alert-danger"; result_box.innerHTML = "MQTT Test nicht erfolgreich."; app.clientMqtt.publish( "MQTT/test", { "test":"MQTT" } ); } else { result_box.className = "alert alert-warning"; result_box.innerHTML = "kein clientMqtt vorhanden"; } } </script> ''' return {}, html # ----------------------------------------------------------------------------- def run( overlay:dict={}, load_tests_db:bool=False ): ''' Startet ispBaseWebApp mit zusätzlichen config Angaben Parameters ---------- overlay : dict, optional Overlay Angaben für config. The default is {}. load_tests_db: bool, optional load also testdb Returns ------- webApp : ispBaseWebApp Die gestartete WebApplication ''' # Konfiguration öffnen _config = ispConfig( mqttlevel=logging.WARNING ) _apiConfig = { "models": [ gqa, gqadb, system ], } if load_tests_db: # pragma: no cover import tests.testdb as testdb _apiConfig["models"].append( testdb.dbtests ) # Webserver starten webApp = ispBaseWebApp( _config, db, apiconfig=_apiConfig, overlay=overlay ) # mqtt in config schließen _config.mqttCleanup( ) return webApp
python
import open3d as o3d import numpy as np import random import copy from aux import * import aux.aux_ekf as a_ekf from aux.aux_octree import * from aux.qhull_2d import * from aux.min_bounding_rect import * from aux.aux_voxel_grid import * import matplotlib.pyplot as plt import pickle from timeit import default_timer as timer import settings class Plane: def __init__(self): self.inliers = [] self.inliersId = [] self.equation = [] self.color = [] self.nPoints = 0 self.centroid = [] self.store_point_bucket = settings.get_setting('save_point_cloud') self.store_octree_model = settings.get_setting('save_octree') self.store_voxel_grid_model = settings.get_setting('save_voxel_grid') self.bucket_octree = [] self.bucket_voxel_grid = [] self.inlier_bucket = o3d.geometry.PointCloud() self.inlier_bucket.points = o3d.utility.Vector3dVector([]) self.bucket = o3d.geometry.PointCloud() self.bucket.points = o3d.utility.Vector3dVector([]) self.bucket_pos = o3d.geometry.PointCloud() self.bucket_pos.points = o3d.utility.Vector3dVector([]) self.bucket_odom = o3d.geometry.PointCloud() self.bucket_odom.points = o3d.utility.Vector3dVector([]) self.t__bucket =0 self.t__bucket_debug =0 def findPlane(self, pts, thresh=0.05, minPoints=3, maxIteration=1000): n_points = np.asarray(pts.points).shape[0] self.nPoints = n_points #print(n_points) best_eq = [] best_inliers = [] valid = False pcd = pts plane_model, inliers = pcd.segment_plane(distance_threshold=thresh,ransac_n=3,num_iterations=maxIteration) [a, b, c, d] = plane_model best_eq = [a, b, c, d] print(f"Plane equation: {a:.2f}x + {b:.2f}y + {c:.2f}z + {d:.2f} = 0") if self.store_point_bucket or self.store_octree_model or self.store_voxel_grid_model: self.inlier_bucket = pcd.select_by_index(inliers) self.inliers = np.asarray(pcd.select_by_index(inliers).points) self.inliersId = np.asarray(inliers) self.equation = [a, b, c, d] self.centroid = np.mean(self.inliers, axis=0) #print("Plano tem esse número de pontos como inliers: ", self.inliers.shape[0]) if(int(self.inliers.shape[0]) > 2000): # pcd = o3d.geometry.PointCloud() # pcd.points = o3d.utility.Vector3dVector(self.inliers) # with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm: # labels = np.array(pcd.cluster_dbscan(eps=0.5, min_points=int(self.inliers.shape[0]/400), print_progress=False)) # max_label = labels.max() # colors = plt.get_cmap("tab20")(labels / (max_label if max_label > 0 else 1)) # colors[labels < 0] = 0 # pcd.colors = o3d.utility.Vector3dVector(colors[:, :3]) # o3d.visualization.draw_geometries([pcd]) # if(max_label > 1): # self.equation = [] # self.best_inliers = [] pcd = pcd.select_by_index(inliers) #o3d.visualization.draw_geometries([pcd]) pcd = pcd.voxel_down_sample(voxel_size=settings.get_setting('plane_density_voxel_filter')) cl, ind = pcd.remove_statistical_outlier(nb_neighbors=int(50*0.1/settings.get_setting('plane_density_voxel_filter')), std_ratio=0.1) # pcd = pcd.voxel_down_sample(voxel_size=0.1) # cl, ind = pcd.remove_statistical_outlier(nb_neighbors=50, std_ratio=0.1) pcd = pcd.select_by_index(ind) #o3d.visualization.draw_geometries([pcd]) #aux.display_inlier_outlier(pcd, ind) #aux.display_inlier_outlier(pcd, ind) if self.store_octree_model or self.store_point_bucket or self.store_voxel_grid_model: self.inlier_bucket = pcd self.inliers = np.asarray(pcd.points) #self.inliersId = ind self.equation = best_eq self.centroid = np.mean(self.inliers, axis=0) if(self.equation): if self.equation[3] < 0: self.equation[0] = -self.equation[0] self.equation[1] = -self.equation[1] self.equation[2] = -self.equation[2] self.equation[3] = -self.equation[3] # # Simplificação em plano xy ou plano z # print("eq: ", self.equation) # vec_eq = [self.equation[0], self.equation[1], self.equation[2]] # imin = vec_eq.index(min(vec_eq)) # vec_eq[imin] = 0 # vec_eq = vec_eq / np.linalg.norm(vec_eq) # self.equation[0], self.equation[1], self.equation[2] = vec_eq[0], vec_eq[1], vec_eq[2] # print("nova eeq: ", self.equation) centroid_pontos = np.mean(self.inliers, axis=0) center_point, rot_angle, width, height, inliers_plano_desrotacionado = self.update_geometry(self.inliers) centroid_retangulo = np.mean(inliers_plano_desrotacionado, axis=0) dimin = np.amin([width, height]) # pcd = o3d.geometry.PointCloud() # pcd.points = o3d.utility.Vector3dVector(self.inliers) # mesh_frame1 = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5, origin=[0, 0, 0]).translate(centroid_pontos) # mesh_frame2 = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5, origin=[0, 0, 0]).translate(centroid_retangulo) # o3d.visualization.draw_geometries([mesh_frame1, mesh_frame2, pcd]) if(np.linalg.norm(centroid_pontos-centroid_retangulo)<dimin*0.3): print("GATE DE VALIDAÇÃO DE SIMETRIA OK") self.center2d = center_point self.rot_angle = rot_angle self.width = width self.height = height self.points_main = inliers_plano_desrotacionado self.centroid = np.mean(self.points_main, axis=0) valid = True min_area_plane = settings.get_setting('min_area_plane') # GATE DE VALIDAÇÃO DE ÁREA (Apenas se não for chão) if np.abs(self.equation[2]) < 0.90: if self.width * self.height < min_area_plane: valid = False print("PLANO NÃO PASSOU NA GATE DE VALIDAÇÃO DE ÁREA: ", self.width * self.height, " m2") else: print("GATE DE VALIDAÇÃO DE ÁREA OK") # # GATE de validação de densidade # densidade = len(self.inliersId)/(self.width*self.height) # if not densidade > settings.get_setting('min_density'): # valid = False # print("PLANO NÃO PASSOU NA GATE DE VALIDAÇÃO DE DENSIDADE: ", densidade, " pontos por m2") # else: # print("GATE DE VALIDAÇÃO DE DENSIDADE OK ", densidade, "") else: print("PLANO NÃO PASSOU NA GATE DE VALIDAÇÃO DE CENTROIDE GEOMÉTRICA") valid = False if valid: print("Saiu do plano: ", self.equation) return self.equation, self.inliersId, valid def move(self, ekf): ekf = copy.deepcopy(ekf) atual_loc = [ekf.x_m[0,0], ekf.x_m[1,0], 0] atual_angulo = [0, 0, ekf.x_m[2,0]] rotMatrix = aux.get_rotation_matrix_bti(atual_angulo) tranlation = atual_loc # inlin = np.dot(self.inliers, rotMatrix.T) + tranlation # pmain = np.dot(self.points_main, rotMatrix.T) + tranlation # cent = np.mean(inlin, axis=0) # vec = np.dot(rotMatrix, [self.equation[0], self.equation[1], self.equation[2]]) # d = -np.sum(np.multiply(vec, cent)) # eqcerta = [vec[0], vec[1],vec[2], d] # print("EQUAÇÃO CERTAAAAAAA: ", eqcerta) # uv = d*np.asarray([[vec[0]], [vec[1]],[vec[2]]]) #for point in self.points_main: # print("USANDO G: ",a_ekf.apply_g_point(ekf.x_m, np.asarray([point]).T).T) self.inliers = np.dot(self.inliers, rotMatrix.T) + tranlation if self.store_octree_model or self.store_point_bucket or self.store_voxel_grid_model: self.inlier_bucket.points = o3d.utility.Vector3dVector(np.asarray(self.inliers)) if self.store_voxel_grid_model: self.bucket_voxel_grid = pcd_to_voxel_grid(copy.deepcopy(self.inlier_bucket), 0.2) if self.store_octree_model: self.bucket_octree = pcd_to_octree(copy.deepcopy(self.inlier_bucket), 0.2) if self.store_point_bucket: t__start = timer() self.bucket = copy.deepcopy(self.inlier_bucket) self.t__bucket = timer() - t__start t__start = timer() self.bucket_pos = copy.deepcopy(self.inlier_bucket) inliers_local = np.dot(self.inliers- tranlation, rotMatrix) ekf_odom_x = copy.deepcopy(ekf.x_errado) atual_loc_odom = [ekf_odom_x[0,0], ekf_odom_x[1,0], 0] atual_angulo_odom = [0, 0, ekf_odom_x[2,0]] rotMatrix_odom = aux.get_rotation_matrix_bti(atual_angulo_odom) tranlation_odom = atual_loc_odom inlier_move_odom = np.dot(np.asarray(inliers_local), rotMatrix_odom.T) + tranlation_odom self.bucket_odom.points = o3d.utility.Vector3dVector(np.asarray(inlier_move_odom)) self.bucket_odom.colors = self.inlier_bucket.colors self.t__bucket_debug = timer() - t__start self.points_main = np.dot(self.points_main, rotMatrix.T) #print('points_main antes: ', self.points_main) self.points_main = self.points_main + tranlation #print('points_main depois: ', self.points_main) self.centroid = np.mean(self.inliers, axis=0) Z = np.asarray([[self.equation[0]],[self.equation[1]],[self.equation[2]], [self.equation[3]]]) N = a_ekf.apply_g_plane(ekf.x_m, Z) # Z2 = a_ekf.apply_h_plane(ekf.x_m, N) # N2 = a_ekf.apply_g_plane(ekf.x_m, Z2) # Z3 = a_ekf.apply_h_plane(ekf.x_m, N2) # print("Z1: ", Z.T) # print("Z2: ", Z2.T) # print("Z3: ", Z3.T) #print("USANDO GGGGGGGG: ", N.T) self.equation = [N[0,0], N[1,0], N[2,0], N[3,0]]#[eqcerta[0],eqcerta[1],eqcerta[2],eqcerta[3]] # # # if self.equation[3] < 0: # self.equation[0] = self.equation[0]*-1 # self.equation[1] = self.equation[1]*-1 # self.equation[2] = self.equation[2]*-1 # self.equation[3] = self.equation[3]*-1 #print("EQUAÇÃO USAAAAAADAAAAA: ", self.equation) center_point, rot_angle, width, height, inliers_plano_desrotacionado = self.update_geometry(self.points_main) self.center2d = center_point self.rot_angle = rot_angle self.width = width self.height = height #self.points_main = inliers_plano_desrotacionado self.centroid = np.mean(self.points_main, axis=0) def getProrieties(self): return {"equation": self.equation,"nPoints":self.inliers.shape[0], "color": self.color, "centroid":self.centroid, "height": self.height, "width": self.width, "center2d": self.center2d, "rot_angle":self.rot_angle} def get_height(self, ground_normal): pts_Z = aux.rodrigues_rot(self.points_main, ground_normal, [0,0,1]) center_Z = aux.rodrigues_rot(self.points_main[4], ground_normal, [0,0,1])[0] centered_pts_Z = pts_Z[:, 2] - center_Z[2] height = np.max(centered_pts_Z) - np.min(centered_pts_Z) return height def get_geometry(self): center_point = np.asarray([self.center2d[0], self.center2d[1], 0]) dep = 0.1 mesh_box = o3d.geometry.TriangleMesh.create_box(width=self.width, height=self.height, depth=dep) mesh_box = mesh_box.translate(np.asarray([-self.width/2, -self.height/2, -dep/2])) mesh_box = mesh_box.rotate(aux.get_rotation_matrix_bti([0, 0, self.rot_angle]), center=np.asarray([0, 0, 0])) mesh_box.compute_vertex_normals() mesh_box.paint_uniform_color(self.color) # center the box on the frame # move to the plane location mesh_box = mesh_box.translate(np.asarray(center_point)) mesh_box = mesh_box.translate(np.asarray([0, 0, -self.equation[3]])) #mesh_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1, origin=[0, 0, 0]) #o3d.visualization.draw_geometries([mesh_frame, mesh_box]) mesh_box = mesh_box.rotate(aux.get_rotationMatrix_from_vectors([0, 0, 1], [self.equation[0], self.equation[1], self.equation[2]]), center=np.asarray([0, 0, 0])) #pcd = o3d.geometry.PointCloud() #pcd.points = o3d.utility.Vector3dVector(inliers_plano_desrotacionado) # pcd.voxel_down_sample(voxel_size=0.1) #pcd.paint_uniform_color(self.color) #obb = pcd.get_oriented_bounding_box() #obb.color = (self.color[0], self.color[1], self.color[2]) # estimate radius for rolling ball #o3d.visualization.draw_geometries([pcd, mesh_box]) pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(self.points_main) return [mesh_box, pcd] def get_octree(self, depth=5, expantion=0): octree = o3d.geometry.Octree(max_depth=depth) octree.convert_from_point_cloud(copy.deepcopy(self.bucket), size_expand=expantion) return octree def getVoxelStructure(self, voxel_size=0.2): return o3d.geometry.VoxelGrid.create_from_point_cloud(copy.deepcopy(self.bucket), voxel_size=voxel_size) def append_plane(self, plano, neweq = [], nvezes=0, is_cobaia = False): #print("Shape antes de append: "+str(self.inliers.shape[0])) # #print("Shape depois de append: "+str(self.inliers.shape[0])) # centroid_pontos = np.mean(points, axis=0) # center_point, rot_angle, width, height, inliers_plano_desrotacionado = self.update_geometry(points) # centroid_retangulo = np.mean(inliers_plano_desrotacionado, axis=0) # dimin = np.amin([width, height]) # if(np.linalg.norm(centroid_pontos-centroid_retangulo)<dimin*0.1): plano = copy.deepcopy(plano) neweq = copy.deepcopy(neweq) usa_media = False points = plano.feat.points_main if is_cobaia == False: if self.store_voxel_grid_model: pcd_voxel_grid = voxel_grid_to_pcd(copy.deepcopy(self.bucket_voxel_grid), 10) corrected_points = aux.projected_point_into_plane(np.append(pcd_voxel_grid.points, plano.feat.inlier_bucket.points, axis=0), self.equation) pcd_voxel_grid.points = o3d.utility.Vector3dVector(corrected_points) pcd_voxel_grid.colors = o3d.utility.Vector3dVector(np.append(pcd_voxel_grid.colors, plano.feat.inlier_bucket.colors, axis=0)) self.bucket_voxel_grid = pcd_to_voxel_grid(pcd_voxel_grid, 0.2) if self.store_octree_model: pcd_octree = octree_to_pcd(copy.deepcopy(self.bucket_octree), 3) corrected_points = aux.projected_point_into_plane(np.append(pcd_octree.points, plano.feat.inlier_bucket.points, axis=0), self.equation) pcd_octree.points = o3d.utility.Vector3dVector(corrected_points) pcd_octree.colors = o3d.utility.Vector3dVector(np.append(pcd_octree.colors, plano.feat.inlier_bucket.colors, axis=0)) self.bucket_octree = pcd_to_octree(pcd_octree, 0.2) # Add points to point bucket if self.store_point_bucket: t__start = timer() self.bucket_pos.points = o3d.utility.Vector3dVector(np.append(self.bucket_pos.points, plano.feat.inlier_bucket.points, axis=0)) self.bucket_pos.colors = o3d.utility.Vector3dVector(np.append(self.bucket_pos.colors, plano.feat.inlier_bucket.colors, axis=0)) self.bucket_pos = self.bucket_pos.voxel_down_sample(voxel_size=settings.get_setting('plane_density_voxel_filter')) self.t__bucket_debug = timer() - t__start t__start = timer() corrected_points = aux.projected_point_into_plane(np.append(self.bucket.points, plano.feat.inlier_bucket.points, axis=0), self.equation) self.bucket.points = o3d.utility.Vector3dVector(corrected_points) self.bucket.colors = o3d.utility.Vector3dVector(np.append(self.bucket.colors, plano.feat.inlier_bucket.colors, axis=0)) self.bucket = self.bucket.voxel_down_sample(voxel_size=settings.get_setting('plane_density_voxel_filter')) self.t__bucket = timer() - t__start t__start = timer() self.bucket_odom.points = o3d.utility.Vector3dVector(np.append(self.bucket_odom.points, plano.feat.bucket_odom.points, axis=0)) self.bucket_odom.colors = o3d.utility.Vector3dVector(np.append(self.bucket_odom.colors, plano.feat.bucket_odom.colors, axis=0)) self.bucket_odom = self.bucket_odom.voxel_down_sample(voxel_size=settings.get_setting('plane_density_voxel_filter')) self.t__bucket_debug = timer() - t__start + self.t__bucket_debug if(usa_media): eqplano2 = plano.feat.equation nvezes_plano2 = plano.running_geo["total"] eqplano1 = copy.deepcopy(self.equation) # nova equação do plano: # Média ponderada entre o o número de vezes já detectado e da área de cada plano # print('eqplano1: ', eqplano1, ' nvezes: ', nvezes+1) # print('eqplano2: ', eqplano2, 'nvezes_plano2: ', nvezes_plano2) area1 = self.width*self.height area2 = plano.feat.width*plano.feat.height self.equation = (np.asarray(eqplano1)*nvezes*area1 + np.asarray(eqplano2)*nvezes_plano2*area2)/((nvezes*area1+nvezes_plano2*area2)) #print("JUNTANDO AS EQUAÇÃO TUDO: ",self.equation) # Muda os dois planos para essa orientação e posição: #self.points_main = aux.rodrigues_rot(self.points_main, [eqplano1[0], eqplano1[1], eqplano1[2]], [self.equation[0], self.equation[1], self.equation[2]]) #points = aux.rodrigues_rot(points, [eqplano2[0], eqplano2[1], eqplano2[2]], [self.equation[0], self.equation[1], self.equation[2]]) else: self.equation = neweq provisorio = copy.deepcopy(np.append(self.points_main, points, axis=0)) center_point, rot_angle, width, height, inliers_plano_desrotacionado = self.update_geometry(provisorio) self.center2d = center_point self.rot_angle = rot_angle self.width = width self.height = height self.points_main = inliers_plano_desrotacionado centroidantes = self.centroid self.centroid = np.mean(self.points_main, axis=0) centroiddepois = self.centroid #print("DIFERENÇA DE CENTROIDES: ", np.linalg.norm(centroidantes-centroiddepois)) discentnormal = np.dot((centroidantes-centroiddepois),np.asarray([self.equation[0], self.equation[1], self.equation[2]])) # O que me interessa mesmo aqui é mudança da centroide mas em direção a normal do plano. Não tem problema a centroide mudar na direção da superfície do plano #print("DIFERENÇA DE CENTROIDES na direção do plano: ",discentnormal) if(np.abs(discentnormal) > 0.8): self.color = (1, 0, 0) return False return True # else: # return False def update_geometry(self, points): # Encontra parâmetros do semi-plano inlier_planez = points # Encontra representação 2d da projeção na normal do plano inliers_plano = aux.rodrigues_rot(copy.deepcopy(inlier_planez), [self.equation[0], self.equation[1], self.equation[2]], [0, 0, 1])- np.asarray([0, 0, -self.equation[3]]) dd_plano = np.delete(inliers_plano, 2, 1) # Fita retângulo de menor área # print('dd_plano: ',dd_plano.shape) # filename = 'pontos.pckl' # outfile = open(filename,'wb') # pickle.dump(dd_plano,outfile) # outfile.close() hull_points = qhull2D(dd_plano) hull_points = hull_points[::-1] (rot_angle, area, width, height, center_point, corner_points) = minBoundingRect(hull_points) # Volta pro espaço 3D p = np.vstack((np.asarray(corner_points), np.asarray(center_point))) ddd_plano= np.c_[ p, np.zeros(p.shape[0]) ] + np.asarray([0, 0, -self.equation[3]]) inliers_plano_desrotacionado = aux.rodrigues_rot(ddd_plano, [0, 0, 1], [self.equation[0], self.equation[1], self.equation[2]]) return center_point, rot_angle, width, height, inliers_plano_desrotacionado # # Load saved point cloud and visualize it # pcd_load = o3d.io.read_point_cloud("caixa.ply") # #o3d.visualization.draw_geometries([pcd_load]) # points = np.asarray(pcd_load.points) # plano1 = Plane() # best_eq, best_inliers = plano1.findPlane(points, 0.01) # plane = pcd_load.select_by_index(best_inliers).paint_uniform_color([1, 0, 0]) # obb = plane.get_oriented_bounding_box() # obb2 = plane.get_axis_aligned_bounding_box() # obb.color = [0, 0, 1] # obb2.color = [0, 1, 0] # not_plane = pcd_load.select_by_index(best_inliers, invert=True) # #mesh = o3d.geometry.TriangleMesh.create_coordinate_frame(origin=[0, 0, 0]) # o3d.visualization.draw_geometries([not_plane, plane, obb, obb2])
python
#! /usr/bin/env python # -*- coding: utf-8 -*- """ Module that contains definitions for tags in Artella """ from __future__ import print_function, division, absolute_import __author__ = "Tomas Poveda" __license__ = "MIT" __maintainer__ = "Tomas Poveda" __email__ = "[email protected]" import ast import string import tpDcc as tp import artellapipe.register class ArtellaTagNode(object): def __init__(self, project, node, tag_info=None): super(ArtellaTagNode, self).__init__() self._project = project self._node = node self._tag_info_dict = tag_info if tag_info: self._tag_info_dict = ast.literal_eval(tag_info) short_node = tp.Dcc.node_short_name(node) if short_node in self._tag_info_dict.keys(): self._tag_info_dict = self._tag_info_dict[short_node] else: short_node_strip = short_node.rstrip(string.digits) if short_node_strip in self._tag_info_dict.keys(): self._tag_info_dict = self._tag_info_dict[short_node_strip] @property def node(self): """ Returns linked to the tag node :return: str """ return self._node @property def tag_info(self): """ Returns tag info data stored in this node :return: dict """ return self._tag_info_dict def get_clean_node(self): """ Returns current node with the short name and with ids removed :return: str """ return tp.Dcc.node_short_name(self._node).rstrip(string.digits) def get_asset_node(self): """ Returns asset node linked to this tag node :return: ArtellaAssetNode """ if not self._node or not tp.Dcc.object_exists(self._node): return None if self._tag_info_dict: return artellapipe.AssetsMgr().get_asset_node_in_scene(node_id=self._node) else: if not tp.Dcc.attribute_exists( node=self._node, attribute_name=artellapipe.TagsMgr().TagDefinitions.NODE_ATTRIBUTE_NAME): return None connections = tp.Dcc.list_connections( node=self._node, attribute_name=artellapipe.TagsMgr().TagDefinitions.NODE_ATTRIBUTE_NAME) if connections: node = connections[0] return artellapipe.AssetsMgr().get_asset_node_in_scene(node_id=node) return None def get_tag_type(self): """ Returns the type of the tag :return: str """ return self._get_attribute(attribute_name=artellapipe.TagsMgr().TagDefinitions.TAG_TYPE_ATTRIBUTE_NAME) def _get_attribute(self, attribute_name): """ Internal function that retrieves attribute from wrapped TagData node :param attribute_name: str, attribute name to retrieve from TagData node :return: variant """ if self._tag_info_dict: return self._tag_info_dict.get(attribute_name) else: if not self._node or not tp.Dcc.object_exists(self._node): return None if not tp.Dcc.attribute_exists(node=self._node, attribute_name=attribute_name): return None return tp.Dcc.get_attribute_value(node=self._node, attribute_name=attribute_name) artellapipe.register.register_class('TagNode', ArtellaTagNode)
python
#!/bin/env python # # Copyright (C) 2014 eNovance SAS <[email protected]> # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import config from utils import Base from utils import ManageSfUtils from utils import skipIfIssueTrackerMissing from pysflib.sfgerrit import GerritUtils from requests.auth import HTTPBasicAuth from requests.exceptions import HTTPError import requests class TestGateway(Base): def _auth_required(self, url): resp = requests.get(url, allow_redirects=False) self.assertEqual(resp.status_code, 307) self.assertTrue("/auth/login" in resp.headers['Location']) @skipIfIssueTrackerMissing() def test_redmine_root_url_for_404(self): """ Test if redmine yield RoutingError """ url = "%s/redmine/" % config.GATEWAY_URL for i in xrange(11): resp = requests.get(url) self.assertNotEquals(resp.status_code, 404) def _url_is_not_world_readable(self, url): """Utility function to make sure a url is not accessible""" resp = requests.get(url) self.assertTrue(resp.status_code > 399, resp.status_code) def test_managesf_is_secure(self): """Test if managesf config.py file is not world readable""" url = "%s/managesf/config.py" % config.GATEWAY_URL self._url_is_not_world_readable(url) def test_cauth_is_secure(self): """Test if managesf config.py file is not world readable""" url = "%s/cauth/config.py" % config.GATEWAY_URL self._url_is_not_world_readable(url) @skipIfIssueTrackerMissing() # TODO(XXX) this is not up to date and can change with config def test_topmenu_links_shown(self): """ Test if all service links are shown in topmenu """ subpaths = ["/r/", "/jenkins/", "/redmine/", "/zuul/", "/etherpad/", "/paste/", "/docs/"] url = config.GATEWAY_URL + "/topmenu.html" resp = requests.get(url) self.assertEqual(resp.status_code, 200) for subpath in subpaths: self.assertTrue(('href="%s"' % subpath) in resp.text) def test_gerrit_accessible(self): """ Test if Gerrit is accessible on gateway hosts """ # Unauthenticated calls urls = [config.GATEWAY_URL + "/r/", config.GATEWAY_URL + "/r/#/"] for url in urls: resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Gerrit Code Review</title>' in resp.text) # URL that requires login - shows login page url = config.GATEWAY_URL + "/r/a/projects/?" resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue('form-signin' in resp.text) # Authenticated URL that requires login url = config.GATEWAY_URL + "/r/a/projects/?" self._auth_required(url) resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) # /r/a/projects returns JSON list of projects self.assertTrue('All-Users' in resp.text) def test_gerrit_projectnames(self): """ Test if projectnames similar to LocationMatch settings work """ # Unauthenticated calls, unknown projects. Must return 404, not 30x urls = [config.GATEWAY_URL + "/r/dashboard", config.GATEWAY_URL + "/r/grafana", config.GATEWAY_URL + "/r/jenkinslogs"] for url in urls: resp = requests.get(url, allow_redirects=False) self.assertEqual(resp.status_code, 404) def test_gerrit_api_accessible(self): """ Test if Gerrit API is accessible on gateway hosts """ m = ManageSfUtils(config.GATEWAY_URL) url = config.GATEWAY_URL + "/api/" a = GerritUtils(url) a.g.url = "%s/" % a.g.url.rstrip('a/') self.assertRaises(HTTPError, a.get_account, config.USER_1) api_passwd = m.create_gerrit_api_password(config.USER_1) auth = HTTPBasicAuth(config.USER_1, api_passwd) a = GerritUtils(url, auth=auth) self.assertTrue(a.get_account(config.USER_1)) m.delete_gerrit_api_password(config.USER_1) a = GerritUtils(url, auth=auth) self.assertRaises(HTTPError, a.get_account, config.USER_1) a = GerritUtils(url) a.g.url = "%s/" % a.g.url.rstrip('a/') self.assertRaises(HTTPError, a.get_account, 'john') def test_jenkins_accessible(self): """ Test if Jenkins is accessible on gateway host """ url = config.GATEWAY_URL + "/jenkins/" # Without SSO cookie. Note that auth is no longer enforced resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Dashboard [Jenkins]</title>' in resp.text) # With SSO cookie resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Dashboard [Jenkins]</title>' in resp.text) # User should be known in Jenkins if logged in with SSO self.assertTrue(config.USER_1 in resp.text) def test_zuul_accessible(self): """ Test if Zuul is accessible on gateway host """ url = config.GATEWAY_URL + "/zuul/" resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Zuul Status</title>' in resp.text) @skipIfIssueTrackerMissing() def test_redmine_accessible(self): """ Test if Redmine is accessible on gateway host """ url = config.GATEWAY_URL + "/redmine/" # Without SSO cookie. Note that auth is no longer enforced resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Redmine</title>' in resp.text) # With SSO cookie resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>Redmine</title>' in resp.text) # User should be known in Redmine if logged in with SSO self.assertTrue(config.USER_1 in resp.text) # Check one of the CSS files to ensure static files are accessible css_file = "plugin_assets/redmine_backlogs/stylesheets/global.css" url = config.GATEWAY_URL + "/redmine/%s" % css_file resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('GLOBAL' in resp.text) def test_etherpad_accessible(self): """ Test if Etherpad is accessible on gateway host """ url = config.GATEWAY_URL + "/etherpad/" resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>SF - Etherpad</title>' in resp.text) def test_paste_accessible(self): """ Test if Paste is accessible on gateway host """ url = config.GATEWAY_URL + "/paste/" resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<title>New Paste | LodgeIt!</title>' in resp.text) def test_css_js_for_topmenu_accessible(self): """ Test if css/js for topmenu are accessible on gateway host """ url = config.GATEWAY_URL + "/static/js/jquery.min.js" resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue("jQuery v2.1.1" in resp.content) paths = ('js/bootstrap.min.js', 'css/bootstrap.min.css') for p in paths: url = config.GATEWAY_URL + "/static/bootstrap/%s" % p resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue("Bootstrap v3.2.0" in resp.content) def test_static_dir_for_paste_accessible(self): """ Test if static dir for paste is accessible on gateway host """ url = config.GATEWAY_URL + "/static/lodgeit/jquery.js" resp = requests.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue("jQuery 1.2.6" in resp.content) def test_docs_accessible(self): """ Test if Sphinx docs are accessible on gateway host """ url = config.GATEWAY_URL + "/docs/index.html" resp = requests.get(url) self.assertEqual(resp.status_code, 200) def test_dashboard_accessible(self): """ Test if Dashboard is accessible on gateway host """ url = config.GATEWAY_URL + "/dashboard/" self._auth_required(url) resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) self.assertTrue('<body ng-controller="mainController">' in resp.text) def test_jenkinslogs_accessible(self): """ Test if Jenkins logs are accessible on gateway host """ url = "http://%s/jenkinslogs/127.0.0.1/dashboard/" % ( config.GATEWAY_HOST) resp = requests.get(url, allow_redirects=False) self.assertEqual(resp.status_code, 307) self._auth_required(url) resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 200) url = "http://%s/jenkinslogs/127.0.0.2/dashboard/" % ( config.GATEWAY_HOST) resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertEqual(resp.status_code, 404)
python
import argparse from transcribeUtils import * from webvttUtils import * import requests from videoUtils import * from audioUtils import * # Get the command line arguments and parse them parser = argparse.ArgumentParser( prog="testWebVTT.py", description="Process a video found in the input file, process it, and write tit out to the output file", ) parser.add_argument( "-region", required=True, help="The AWS region containing the S3 buckets" ) parser.add_argument( "-inbucket", required=True, help="The S3 bucket containing the input file" ) parser.add_argument("-infile", required=True, help="The input file to process") parser.add_argument( "-outbucket", required=True, help="The S3 bucket containing the input file" ) parser.add_argument( "-outfilename", required=True, help="The file name without the extension" ) parser.add_argument( "-outfiletype", required=True, help="The output file type. E.g. mp4, mov" ) parser.add_argument( "-outlang", required=True, nargs="+", help="The language codes for the desired output. E.g. en = English, de = German", ) parser.add_argument( "-TranscriptJob", required=True, help="The URI resulting from the transcript job", ) args = parser.parse_args() job = getTranscriptionJobStatus(args.TranscriptJob) # print( job ) # Now get the transcript JSON from AWS Transcribe transcript = getTranscript( str(job["TranscriptionJob"]["Transcript"]["TranscriptFileUri"]) ) # print( "\n==> Transcript: \n" + transcript) # Create the WebVTT File for the original transcript and write it out. writeTranscriptToWebVTT(transcript, "en", "subtitles-en.vtt") # createVideo( args.infile, "subtitles-en.vtt", args.outfilename + "-en." + args.outfiletype, "audio-en.mp3", True) # Now write out the translation to the transcript for each of the target languages for lang in args.outlang: writeTranslationToWebVTT( transcript, "en", lang, "subtitles-" + lang + ".vtt" ) # Now that we have the subtitle files, let's create the audio track # createAudioTrackFromTranslation( args.region, transcript, 'en', lang, "audio-" + lang + ".mp3" ) # Finally, create the composited video # createVideo( args.infile, "subtitles-" + lang + ".WebVTT", args.outfilename + "-" + lang + "." + args.outfiletype, "audio-" + lang + ".mp3", False)
python
from basis.setting import PERIODS from basis.assistant import getID import progressbar ALL_PERIODS = [] for i in range(len(PERIODS)-1): ALL_PERIODS.append({"from":{"hour":PERIODS[i][0],"minute":PERIODS[i][1]},"to":{"hour":PERIODS[i+1][0],"minute":PERIODS[i+1][1]}}) def getperiodsIndex(all_periods): time_dics = {} for i,period in enumerate(all_periods): for hour in range(period["from"]["hour"],period["to"]["hour"]+1): min_minute,max_minute = 0,60 if hour == period["from"]["hour"]: min_minute = period["from"]["minute"] if hour == period["to"]["hour"]: max_minute = period["to"]["minute"] for minute in range(min_minute,max_minute): time_dics[getID(hour,minute)] = i return time_dics def getMinutes(all_periods): for i,period in enumerate(all_periods): period["minutes"] = (period["to"]["hour"]-period["from"]["hour"])*60 + period["to"]["minute"] - period["from"]["minute"] return all_periods ALL_PERIODS = getMinutes(ALL_PERIODS) TIME_DICS = getperiodsIndex(ALL_PERIODS)
python
import logging logging.basicConfig() logger = logging.getLogger('led_detection') logger.setLevel(logging.DEBUG) from .api import * from .unit_tests import * from .algorithms import *
python
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 def transform(logdata): headers = logdata['httpRequest']['headers'] if len(headers) > 0: logdata['httpRequest']['header'] = {} for header in headers: key = header['name'].lower().replace('-', '_') logdata['httpRequest']['header'][key] = header['value'] if key == 'host': logdata['url']['domain'] = header['value'] elif key == 'user_agent': logdata['user_agent'] = {} logdata['user_agent']['original'] = header['value'] elif key == 'referer': logdata['http']['request']['referrer'] = header['value'] elif key == 'authorization': del logdata['httpRequest']['header'][key] try: # WAFv2 logdata['rule']['ruleset'] = logdata['webaclId'].split('/')[2] region_type = logdata['webaclId'].split(':')[5].split('/')[0] if region_type == 'global': logdata['cloud']['region'] = 'global' else: logdata['cloud']['region'] = logdata['webaclId'].split(':')[3] logdata['cloud']['account'] = {'id': logdata['webaclId'].split(':')[4]} except IndexError: # WAFv1 logdata['rule']['ruleset'] = logdata['webaclId'] logdata['http']['version'] = (logdata['httpRequest'] ['httpVersion'].split('/')[1]) # action = logdata.get('action') # if 'ALLOW' in action: # logdata['event']['outcome'] = 'success' # elif 'BLOCK' in action: # logdata['event']['outcome'] = 'failure' # else: # logdata['event']['outcome'] = 'unknown' return logdata
python
from django.contrib import admin from app.models import UserProfileInfo admin.site.register(UserProfileInfo)
python
# -*- coding: utf-8 -*- from south.utils import datetime_utils as datetime from south.db import db from south.v2 import SchemaMigration from django.db import models class Migration(SchemaMigration): def forwards(self, orm): # Adding model 'Tag' db.create_table(u'tagging_tag', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('title', self.gf('django.db.models.fields.CharField')(max_length=32, db_index=True)), ('description', self.gf('django.db.models.fields.TextField')(null=True, blank=True)), ('site', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['sites.Site'], null=True, blank=True)), )) db.send_create_signal(u'tagging', ['Tag']) # Adding unique constraint on 'Tag', fields ['title', 'site'] db.create_unique(u'tagging_tag', ['title', 'site_id']) # Adding model 'ContentObjectTag' db.create_table(u'tagging_contentobjecttag', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(related_name='content_type_set_for_contentobjecttag', to=orm['contenttypes.ContentType'])), ('object_pk', self.gf('django.db.models.fields.PositiveIntegerField')()), ('site', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['sites.Site'])), ('tag', self.gf('django.db.models.fields.related.ForeignKey')(related_name='content_object_tags', to=orm['tagging.Tag'])), )) db.send_create_signal(u'tagging', ['ContentObjectTag']) def backwards(self, orm): # Removing unique constraint on 'Tag', fields ['title', 'site'] db.delete_unique(u'tagging_tag', ['title', 'site_id']) # Deleting model 'Tag' db.delete_table(u'tagging_tag') # Deleting model 'ContentObjectTag' db.delete_table(u'tagging_contentobjecttag') models = { u'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) }, u'sites.site': { 'Meta': {'ordering': "(u'domain',)", 'object_name': 'Site', 'db_table': "u'django_site'"}, 'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, u'tagging.contentobjecttag': { 'Meta': {'object_name': 'ContentObjectTag'}, 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'content_type_set_for_contentobjecttag'", 'to': u"orm['contenttypes.ContentType']"}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'object_pk': ('django.db.models.fields.PositiveIntegerField', [], {}), 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['sites.Site']"}), 'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'content_object_tags'", 'to': u"orm['tagging.Tag']"}) }, u'tagging.tag': { 'Meta': {'unique_together': "[('title', 'site')]", 'object_name': 'Tag'}, 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'site': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['sites.Site']", 'null': 'True', 'blank': 'True'}), 'title': ('django.db.models.fields.CharField', [], {'max_length': '32', 'db_index': 'True'}) } } complete_apps = ['tagging']
python
#! /usr/bin/env python # This file is part of khmer, https://github.com/dib-lab/khmer/, and is # Copyright (C) 2011-2015, Michigan State University. # Copyright (C) 2015, The Regents of the University of California. # # 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 the Michigan State University 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 THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Contact: [email protected] import sys import khmer import os try: import matplotlib matplotlib.use('Agg') from pylab import * except ImportError: pass def main(): hashfile = sys.argv[1] filename = sys.argv[2] figure = sys.argv[3] ht = khmer.load_countgraph(hashfile) outabund = open(os.path.basename(filename) + '.counts', 'w') counts = [] d = {} for sequence in open(sys.argv[2]): sequence = sequence.strip() count = ht.get(sequence) counts.append(count) d[count] = d.get(count, 0) + 1 if count > 1000: print(sequence, count, file=outabund) outfp = open(figure + '.countshist', 'w') sofar = 0 sofar_cumu = 0 for k in sorted(d.keys()): sofar += d[k] sofar_cumu += k * d[k] print(k, d[k], sofar, sofar_cumu, file=outfp) hist(counts, normed=True, cumulative=True, bins=100, range=(1, 1000)) savefig(figure) if __name__ == '__main__': main()
python
import re from isic_archive.models.dataset_helpers import matchFilenameRegex def assertMatch(originalFilename, csvFilename): """Assert that the filename in the CSV matches the original filename.""" regex = matchFilenameRegex(csvFilename) assert re.match(regex, originalFilename) is not None def assertNotMatch(originalFilename, csvFilename): """Assert that the filename in the CSV doesn't match the original filename.""" regex = matchFilenameRegex(csvFilename) assert re.match(regex, originalFilename) is None def testMatchFilenameRegex(): """ Test matchFilenameRegex. The matchFilenameRegex function generates a regular expression to match image filenames in a metadata CSV file to original image filenames in the database. """ originalFilename = 'ABC-6D.JPG' assertMatch(originalFilename, 'ABC-6D') assertMatch(originalFilename, 'ABC-6D.JPG') assertMatch(originalFilename, 'ABC-6D.jpg') assertMatch(originalFilename, 'abc-6D.jpg') assertMatch(originalFilename, 'abc-6d.jpg') assertNotMatch(originalFilename, 'ABC-6D.png') assertNotMatch(originalFilename, 'ABC-6D.PNG') originalFilename = '20010425124238356.jpg' assertMatch(originalFilename, '20010425124238356') assertMatch(originalFilename, '20010425124238356.jpg') assertMatch(originalFilename, '20010425124238356.JPG') assertNotMatch(originalFilename, '20010425124238356.png') assertNotMatch(originalFilename, '20010425124238356.PNG') originalFilename = 'AbcDef00598.jpg' assertMatch(originalFilename, 'AbcDef00598') assertMatch(originalFilename, 'AbcDef00598.jpg') assertMatch(originalFilename, 'AbcDef00598.JPG') assertMatch(originalFilename, 'abcdef00598.JPG') assertNotMatch(originalFilename, 'AbcDef00598.png') assertNotMatch(originalFilename, 'AbcDef00598.PNG') originalFilename = 'test-20010425124238356.jpg' assertMatch(originalFilename, 'test-20010425124238356') assertMatch(originalFilename, 'test-20010425124238356.jpg') assertMatch(originalFilename, 'TEST-20010425124238356.jpg') assertMatch(originalFilename, 'TEST-20010425124238356.JPG') assertNotMatch(originalFilename, 'TEST-20010425124238356.png') assertNotMatch(originalFilename, 'TEST-20010425124238356.PNG') originalFilename = 'AEOU3014, (20020901020318037) 20010425124238356.jpg' assertMatch(originalFilename, 'AEOU3014, (20020901020318037) 20010425124238356') assertMatch(originalFilename, 'AEOU3014, (20020901020318037) 20010425124238356.jpg') assertMatch(originalFilename, 'AEOU3014, (20020901020318037) 20010425124238356.JPG') assertMatch(originalFilename, 'aeou3014, (20020901020318037) 20010425124238356.JPG') assertMatch(originalFilename, 'aeou3014, (20020901020318037) 20010425124238356.jpg') assertNotMatch(originalFilename, 'AEOU3014, (20020901020318037) 20010425124238356.png') assertNotMatch(originalFilename, 'AEOU3014, (20020901020318037) 20010425124238356.PNG') originalFilename = '20020901020318037_30445187_2002-0901_Null_ 001.jpg' assertMatch(originalFilename, '20020901020318037_30445187_2002-0901_Null_ 001') assertMatch(originalFilename, '20020901020318037_30445187_2002-0901_Null_ 001.jpg') assertMatch(originalFilename, '20020901020318037_30445187_2002-0901_Null_ 001.JPG') assertMatch(originalFilename, '20020901020318037_30445187_2002-0901_NULL_ 001.jpg') assertMatch(originalFilename, '20020901020318037_30445187_2002-0901_NULL_ 001.JPG') assertNotMatch(originalFilename, '20020901020318037_30445187_2002-0901_NULL_ 001.png') assertNotMatch(originalFilename, '20020901020318037_30445187_2002-0901_NULL_ 001.PNG') # Filename that contains a period originalFilename = 'test.315704d.jpg' assertMatch(originalFilename, 'test.315704d') assertMatch(originalFilename, 'test.315704d.jpg') assertNotMatch(originalFilename, 'test.315704d.PNG') # Filename that contains multiple periods originalFilename = 'test.315704d.4e95e3d.png' assertMatch(originalFilename, 'test.315704d.4e95e3d') assertMatch(originalFilename, 'test.315704d.4e95e3d.png') assertNotMatch(originalFilename, 'test.315704d') assertNotMatch(originalFilename, 'test.315704d.4e95e3d.') assertNotMatch(originalFilename, 'test.315704d.4e95e3d.jpg')
python
# ------------------------------------------------------------------------------ # pose.pytorch # Copyright (c) 2018-present Microsoft # Licensed under The Apache-2.0 License [see LICENSE for details] # Written by Bin Xiao ([email protected]) # ------------------------------------------------------------------------------ from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import os import pprint import torch import torch.nn.parallel import torch.backends.cudnn as cudnn import torch.optim import torch.utils.data import torch.utils.data.distributed import torchvision.transforms as transforms import torchvision import _init_paths from config import cfg from config import update_config from core.loss import JointsMSELoss from core.function import validate from utils.utils import create_logger from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog from models.pose_hrnet import get_pose_net from dataset.coco_realtime import COCODataset from utils.vis import save_batch_heatmaps import cv2 import glob import os import time def parse_args(): parser = argparse.ArgumentParser(description='Train keypoints network') # general parser.add_argument('--cfg', help='experiment configure file name', default='experiments/coco/hrnet/w32_256x192_adam_lr1e-3.yaml', required=True, type=str) parser.add_argument('opts', help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER) parser.add_argument('--modelDir', help='model directory', type=str, default='models/pytorch/pose_coco') parser.add_argument('--logDir', help='log directory', type=str, default='') parser.add_argument('--dataDir', help='data directory', type=str, default='') parser.add_argument('--prevModelDir', help='prev Model directory', type=str, default='') args = parser.parse_args() return args def detectron_validate(config, val_dataset, model, criterion, output_dir, tb_log_dir, writer_dict=None): # switch to evaluate mode model.eval() with torch.no_grad(): outputs = model(val_dataset.cuda()) def detectron_save_image(crop, model, criterion, final_output_dir): model.eval() with torch.no_grad(): outputs = model(crop.cuda()) grid_img = torchvision.utils.make_grid(crop, padding=0) prefix = '{}_{:05d}'.format( os.path.join(final_output_dir, 'val'), criterion ) torchvision.utils.save_image(grid_img, prefix + '_im.jpg', normalize=True) save_batch_heatmaps(crop, outputs, prefix + '_heat.jpg') def main(): args = parse_args() update_config(cfg, args) logger, final_output_dir, tb_log_dir = create_logger( cfg, args.cfg, 'valid') logger.info(pprint.pformat(args)) logger.info(cfg) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED # Create config detection_cfg = get_cfg() detection_cfg.DATASETS.TRAIN = (os.getcwd() + "/data/coco/images/train2017",) detection_cfg.DATASETS.TEST = (os.getcwd() + "../data/coco/images/val2017",) detection_cfg.merge_from_file("../detectron2/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml") detection_cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model detection_cfg.MODEL.WEIGHTS = "detectron2://COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl" # Create predictor predictor = DefaultPredictor(detection_cfg) # Create detector model = get_pose_net(cfg, is_train=False) ''' model = eval('models.' + cfg.MODEL.NAME + '.get_pose_net')( cfg, is_train=False ) ''' # print(model) if cfg.TEST.MODEL_FILE: logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=False) else: model_state_file = os.path.join( final_output_dir, 'final_state.pth' ) logger.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file)) model = torch.nn.DataParallel(model, device_ids=cfg.GPUS).cuda() # define loss function (criterion) and optimizer criterion = JointsMSELoss( use_target_weight=cfg.LOSS.USE_TARGET_WEIGHT ).cuda() test_list = glob.glob("{}/{}/*".format(os.getcwd(), '/data/coco/images/val2017')) tic = time.time() total_image = len(test_list) total_person = 0 detect_time = 0 estimate_time = 0 for i in range(len(test_list)): inputs = cv2.imread(test_list[i]) det_start = time.time() outputs = predictor(inputs) detect_time = detect_time + time.time() - det_start human_boxes = outputs['instances'].pred_boxes[outputs['instances'].pred_classes == 0] # human_boxes = [i for i in human_boxes if abs(int(boxes[i, 1])-int(boxes[i, 3])) * abs(int(boxes[i, 0])-int(boxes[i, 2])) >= 32*32] boxes = human_boxes.tensor total_person = total_person + boxes.shape[0] if boxes.shape[0] > 0: for j in range(boxes.shape[0]): cropped_img = cv2.resize(inputs[int(boxes[j, 1]): int(boxes[j, 3]), int(boxes[j, 0]): int(boxes[j, 2])], dsize=(192, 256)) if j is 0: crop = torch.unsqueeze(torch.from_numpy(cropped_img), 0) else: crop = torch.cat((crop, torch.unsqueeze(torch.from_numpy(cropped_img), 0)), 0) crop = torch.transpose(torch.transpose(crop, -1, -2), -2, -3).float() # NCHW crop = ((crop/255.) - torch.tensor([[[[0.485]],[[0.456]],[[0.406]]]]))/torch.tensor([[[[0.229]],[[0.224]],[[0.225]]]]) est_start = time.time() detectron_validate(cfg, crop, model, criterion, final_output_dir, tb_log_dir) estimate_time = estimate_time + time.time() - est_start detectron_save_image(crop, model, i, final_output_dir) else: total_image -= 1 total_time = time.time()-tic print('-[only detection]-') print('[*] Total elapsed time: {}'.format(detect_time)) print('[*] image per second: {}'.format(total_image / detect_time)) print('[*] person per second: {}'.format(total_person / detect_time)) print('--[only estimation]-') print('[*] Total elapsed time: {}'.format(estimate_time)) print('[*] image per second: {}'.format(total_image / estimate_time)) print('[*] person per second: {}'.format(total_person / estimate_time)) print('--[detection+estimation]-') print('[*] Total elapsed time: {}'.format(total_time)) print('[*] image per second: {}'.format(total_image/total_time)) print('[*] person per second: {}'.format(total_person / total_time)) if __name__ == '__main__': main()
python
from django.urls import path from .views import ( FollowAPIView, FollowersListAPIView, FollowingListAPIView, UserListAPIView, UserProfileAPIView, UserRetrieveUpdateDeleteAPIView, UserRegisterAPIView, UserLoginAPIView, confirm_email, password_reset_request, password_reset_confirm, NotificationListAPIView, set_notifications_as_read, set_notifications_as_unread, ) urlpatterns = [ path("", UserListAPIView.as_view()), path("<str:username>/", UserRetrieveUpdateDeleteAPIView.as_view()), path("<str:username>/profile/", UserProfileAPIView.as_view()), path("<str:username>/follow/", FollowAPIView.as_view()), path("<str:username>/followers/", FollowersListAPIView.as_view()), path("<str:username>/following/", FollowingListAPIView.as_view()), path("<str:username>/notifications/", NotificationListAPIView.as_view()), path("<str:username>/notifications/<int:pk>/read/", set_notifications_as_read), path("<str:username>/notifications/<int:pk>/unread/", set_notifications_as_unread), path("auth/register/", UserRegisterAPIView.as_view()), path("auth/login/", UserLoginAPIView.as_view()), path("auth/email/confirm/<str:token>/", confirm_email), path("auth/password/reset/", password_reset_request), path("auth/password/reset/confirm/<str:token>/", password_reset_confirm), ]
python
import ast import sys from pyflakes import checker from pyflakes.test.harness import TestCase, skipIf class TypeableVisitorTests(TestCase): """ Tests of L{_TypeableVisitor} """ @staticmethod def _run_visitor(s): """ Run L{_TypeableVisitor} on the parsed source and return the visitor. """ tree = ast.parse(s) visitor = checker._TypeableVisitor() visitor.visit(tree) return visitor def test_node_types(self): """ Test that the typeable node types are collected """ visitor = self._run_visitor( """\ x = 1 # assignment for x in range(1): pass # for loop def f(): pass # function definition with a as b: pass # with statement """ ) self.assertEqual(visitor.typeable_lines, [1, 2, 3, 4]) self.assertIsInstance(visitor.typeable_nodes[1], ast.Assign) self.assertIsInstance(visitor.typeable_nodes[2], ast.For) self.assertIsInstance(visitor.typeable_nodes[3], ast.FunctionDef) self.assertIsInstance(visitor.typeable_nodes[4], ast.With) def test_visitor_recurses(self): """ Test the common pitfall of missing `generic_visit` in visitors by ensuring that nested nodes are reported """ visitor = self._run_visitor( """\ def f(): x = 1 """ ) self.assertEqual(visitor.typeable_lines, [1, 2]) self.assertIsInstance(visitor.typeable_nodes[1], ast.FunctionDef) self.assertIsInstance(visitor.typeable_nodes[2], ast.Assign) @skipIf(sys.version_info < (3, 5), 'async syntax introduced in py35') def test_py35_node_types(self): """ Test that the PEP 492 node types are collected """ visitor = self._run_visitor( """\ async def f(): # async def async for x in y: pass # async for async with a as b: pass # async with """ ) self.assertEqual(visitor.typeable_lines, [1, 2, 3]) self.assertIsInstance(visitor.typeable_nodes[1], ast.AsyncFunctionDef) self.assertIsInstance(visitor.typeable_nodes[2], ast.AsyncFor) self.assertIsInstance(visitor.typeable_nodes[3], ast.AsyncWith) def test_last_node_wins(self): """ Test that when two typeable nodes are present on a line, the last typeable one wins. """ visitor = self._run_visitor('x = 1; y = 1') # detected both assignable nodes self.assertEqual(visitor.typeable_lines, [1, 1]) # but the assignment to `y` wins self.assertEqual(visitor.typeable_nodes[1].targets[0].id, 'y') class CollectTypeCommentsTests(TestCase): """ Tests of L{_collect_type_comments} """ @staticmethod def _collect(s): """ Run L{_collect_type_comments} on the parsed source and return the mapping from nodes to comments. The return value is converted to a set: {(node_type, tuple of comments), ...} """ tree = ast.parse(s) tokens = checker.make_tokens(s) ret = checker._collect_type_comments(tree, tokens) return {(type(k), tuple(s for _, s in v)) for k, v in ret.items()} def test_bytes(self): """ Test that the function works for binary source """ ret = self._collect(b'x = 1 # type: int') self.assertSetEqual(ret, {(ast.Assign, ('# type: int',))}) def test_text(self): """ Test that the function works for text source """ ret = self._collect(u'x = 1 # type: int') self.assertEqual(ret, {(ast.Assign, ('# type: int',))}) def test_non_type_comment_ignored(self): """ Test that a non-type comment is ignored """ ret = self._collect('x = 1 # noqa') self.assertSetEqual(ret, set()) def test_type_comment_before_typeable(self): """ Test that a type comment before something typeable is ignored. """ ret = self._collect('# type: int\nx = 1') self.assertSetEqual(ret, set()) def test_type_ignore_comment_ignored(self): """ Test that `# type: ignore` comments are not collected. """ ret = self._collect('x = 1 # type: ignore') self.assertSetEqual(ret, set()) def test_type_ignore_with_other_things_ignored(self): """ Test that `# type: ignore` comments with more content are also not collected. """ ret = self._collect('x = 1 # type: ignore # noqa') self.assertSetEqual(ret, set()) ret = self._collect('x = 1 #type:ignore#noqa') self.assertSetEqual(ret, set()) def test_type_comment_with_extra_still_collected(self): ret = self._collect('x = 1 # type: int # noqa') self.assertSetEqual(ret, {(ast.Assign, ('# type: int # noqa',))}) def test_type_comment_without_whitespace(self): ret = self._collect('x = 1 #type:int') self.assertSetEqual(ret, {(ast.Assign, ('#type:int',))}) def test_type_comment_starts_with_word_ignore(self): ret = self._collect('x = 1 # type: ignore[T]') self.assertSetEqual(ret, {(ast.Assign, ('# type: ignore[T]',))}) def test_last_node_wins(self): """ Test that when two typeable nodes are present on a line, the last typeable one wins. """ ret = self._collect('def f(): x = 1 # type: int') self.assertSetEqual(ret, {(ast.Assign, ('# type: int',))}) def test_function_def_assigned_comments(self): """ Test that type comments for function arguments are all attributed to the function definition. """ ret = self._collect( """\ def f( a, # type: int b, # type: str ): # type: (...) -> None pass """ ) expected = {( ast.FunctionDef, ('# type: int', '# type: str', '# type: (...) -> None'), )} self.assertSetEqual(ret, expected)
python
from dotenv import load_dotenv import os load_dotenv() client = os.getenv("CLIENT_ID") secret = os.getenv("CLIENT_SECRET") def printenvironment(): print(f'The client id is: {client}.') print(f'The secret id is: {secret}.') if __name__ == "__main__": printenvironment()
python
import re from fsrtools.simulation_tools._manager_utils import integer_filter def product_combination_generator(iterate_dict): total_length = 1 length_dict = {} combination_list = [] if len(iterate_dict.keys()): for key in iterate_dict.keys(): length_dict[key] = len(iterate_dict[key]) total_length = total_length * len(iterate_dict[key]) combination_list = [{} for x in range(total_length)] repeat_length = total_length previous_length = total_length for key, length in sorted(length_dict.items(), key=lambda x: -x[1]): repeat_length //= length for i in range(total_length): combination_list[i][key] = iterate_dict[key][ (i % previous_length) // repeat_length ] previous_length = repeat_length return combination_list def set_total_combinations(simulate_params,logger): simulate_params_temp = simulate_params.copy() iterate_dict = {} for key in simulate_params.keys(): if isinstance(simulate_params[key], list): iterate_dict[key] = simulate_params[key] logger('[list input : {0} : {1}]'.format(key, simulate_params[key])) elif isinstance(simulate_params[key], str): counter = 0 local_variables = {} for key_t in simulate_params.keys(): if key_t != key and \ re.search( r'\b' + key_t+ r'\b',simulate_params[key]): counter += 1 if not isinstance(simulate_params[key_t], list) and \ not isinstance(simulate_params[key_t],str): local_variables[key_t] = simulate_params[key_t] if len(local_variables) == counter: try: calculated_value = \ eval(simulate_params[key],globals(),local_variables) simulate_params_temp[key] = \ integer_filter(calculated_value) except NameError as err: logger('[{} as paremter : "{}" is input as "{}"]' .format(err,key,simulate_params[key])) simulate_params_temp[key] = simulate_params[key] logger('[{0} : {1}]'.format(key, simulate_params_temp[key])) else: for key_t in local_variables.keys(): logger('{0} is as command: depend on changing {1}' .format(key,key_t)) total_combinations = product_combination_generator(iterate_dict) return simulate_params_temp, total_combinations
python
############### usha/ssd_distplot_seaborn.ipynb import csv import os.path my_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '..')) path = os.path.join(my_path, 'documents/Leadss.csv') fpath = os.path.join(my_path, 'static/images/distplot') import pandas as pd import numpy as np import seaborn as sns import math import matplotlib import matplotlib.pyplot as plt matplotlib.use('Agg') data = path #'C:\Users\Usha\Downloads\Ecommerce Purchases.csv' def histo(data): print(data) sep = ',' header = 'None' df = pd.read_csv(data,header,sep) datatype= df.dtypes #get only the numeric values of dataframe pp=df._get_numeric_data() #convert the pp to 2d array df1=pp.values print(pp.head(5)) #get the first columns array first_col = list(pp) print(first_col) np_2d = np.array(df1) #get the number of rows in a file n = np_2d.shape[0] #path_name = os.path.expanduser('~\Downloads') #press shift + tab for backing all selected by 1 tab and press tab only for selected things to move forward a = 1 d = 0 for num in first_col: bins = round(2*n**(1/3)) print(bins) distplot=sns.distplot(pp[num], ) print(distplot) fig=distplot.get_figure() #plt.xlabel('bins '+str(a)) print(a) #plt.ylabel(num) #plt.legend() #fig.show() a = a+1 #fig.savefig(num+'.png') fig.savefig(fpath + '\\' + 'image' + str(d) + '.png') d = d+1 plt.close() #fig.savefig(num+'.png') #histo(data)
python
{ "includes": [ "../common.gypi" ], "targets": [ { "target_name": "libgdal_jpeg_frmt", "type": "static_library", "sources": [ "../gdal/frmts/jpeg/jpgdataset.cpp", "../gdal/frmts/jpeg/libjpeg/jcapimin.c", "../gdal/frmts/jpeg/libjpeg/jcapistd.c", "../gdal/frmts/jpeg/libjpeg/jccoefct.c", "../gdal/frmts/jpeg/libjpeg/jccolor.c", "../gdal/frmts/jpeg/libjpeg/jcdctmgr.c", "../gdal/frmts/jpeg/libjpeg/jchuff.c", "../gdal/frmts/jpeg/libjpeg/jcinit.c", "../gdal/frmts/jpeg/libjpeg/jcmainct.c", "../gdal/frmts/jpeg/libjpeg/jcmarker.c", "../gdal/frmts/jpeg/libjpeg/jcmaster.c", "../gdal/frmts/jpeg/libjpeg/jcomapi.c", "../gdal/frmts/jpeg/libjpeg/jcparam.c", "../gdal/frmts/jpeg/libjpeg/jcphuff.c", "../gdal/frmts/jpeg/libjpeg/jcprepct.c", "../gdal/frmts/jpeg/libjpeg/jcsample.c", "../gdal/frmts/jpeg/libjpeg/jctrans.c", "../gdal/frmts/jpeg/libjpeg/jdapimin.c", "../gdal/frmts/jpeg/libjpeg/jdapistd.c", "../gdal/frmts/jpeg/libjpeg/jdatadst.c", "../gdal/frmts/jpeg/libjpeg/jdatasrc.c", "../gdal/frmts/jpeg/libjpeg/jdcoefct.c", "../gdal/frmts/jpeg/libjpeg/jdcolor.c", "../gdal/frmts/jpeg/libjpeg/jddctmgr.c", "../gdal/frmts/jpeg/libjpeg/jdhuff.c", "../gdal/frmts/jpeg/libjpeg/jdinput.c", "../gdal/frmts/jpeg/libjpeg/jdmainct.c", "../gdal/frmts/jpeg/libjpeg/jdmarker.c", "../gdal/frmts/jpeg/libjpeg/jdmaster.c", "../gdal/frmts/jpeg/libjpeg/jdmerge.c", "../gdal/frmts/jpeg/libjpeg/jdphuff.c", "../gdal/frmts/jpeg/libjpeg/jdpostct.c", "../gdal/frmts/jpeg/libjpeg/jdsample.c", "../gdal/frmts/jpeg/libjpeg/jdtrans.c", "../gdal/frmts/jpeg/libjpeg/jerror.c", "../gdal/frmts/jpeg/libjpeg/jfdctflt.c", "../gdal/frmts/jpeg/libjpeg/jfdctfst.c", "../gdal/frmts/jpeg/libjpeg/jfdctint.c", "../gdal/frmts/jpeg/libjpeg/jidctflt.c", "../gdal/frmts/jpeg/libjpeg/jidctfst.c", "../gdal/frmts/jpeg/libjpeg/jidctint.c", "../gdal/frmts/jpeg/libjpeg/jidctred.c", "../gdal/frmts/jpeg/libjpeg/jmemansi.c", "../gdal/frmts/jpeg/libjpeg/jmemmgr.c", "../gdal/frmts/jpeg/libjpeg/jquant1.c", "../gdal/frmts/jpeg/libjpeg/jquant2.c", "../gdal/frmts/jpeg/libjpeg/jutils.c", "../gdal/frmts/jpeg/vsidataio.cpp" ], "include_dirs": [ "../gdal/frmts/jpeg", "../gdal/frmts/jpeg/libjpeg" ] } ] }
python
import datetime import numpy as np import cv2 import pickle import face_recognition # ------------------------------------------------------------------- # Parameters # ------------------------------------------------------------------- CONF_THRESHOLD = 0.5 NMS_THRESHOLD = 0.4 IMG_WIDTH = 416 IMG_HEIGHT = 416 # Default colors COLOR_BLUE = (255, 0, 0) COLOR_GREEN = (0, 255, 0) COLOR_RED = (0, 0, 255) COLOR_WHITE = (255, 255, 255) COLOR_YELLOW = (0, 255, 255) #face encoding data data=None # ------------------------------------------------------------------- # Help functions # ------------------------------------------------------------------- # Get the names of the output layers def get_outputs_names(net): # Get the names of all the layers in the network layers_names = net.getLayerNames() # Get the names of the output layers, i.e. the layers with unconnected # outputs return [layers_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # Draw the predicted bounding box def draw_predict(frame, conf, left, top, right, bottom): # Draw a bounding box. cv2.rectangle(frame, (left, top), (right, bottom), COLOR_YELLOW, 2) text = '{:.2f}'.format(conf) # Display the label at the top of the bounding box label_size, base_line = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1) top = max(top, label_size[1]) cv2.putText(frame, text, (left, top - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.4,COLOR_WHITE, 1) def post_process(frame, outs, conf_threshold, nms_threshold): frame_height = frame.shape[0] frame_width = frame.shape[1] # Scan through all the bounding boxes output from the network and keep only # the ones with high confidence scores. Assign the box's class label as the # class with the highest score. confidences = [] boxes = [] final_boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > conf_threshold: center_x = int(detection[0] * frame_width) center_y = int(detection[1] * frame_height) width = int(detection[2] * frame_width) height = int(detection[3] * frame_height) left = int(center_x - width / 2) top = int(center_y - height / 2) confidences.append(float(confidence)) boxes.append([left, top, width, height]) # Perform non maximum suppression to eliminate redundant # overlapping boxes with lower confidences. indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold) for i in indices: i = i[0] box = boxes[i] left = box[0] top = box[1] width = box[2] height = box[3] final_boxes.append(box) #left, top, right, bottom = refined_box(left, top, width, height) # draw_predict(frame, confidences[i], left, top, left + width, # top + height) #draw_predict(frame, confidences[i], left, top, right, bottom) return final_boxes class FPS: def __init__(self): # store the start time, end time, and total number of frames # that were examined between the start and end intervals self._start = None self._end = None self._num_frames = 0 def start(self): self._start = datetime.datetime.now() return self def stop(self): self._end = datetime.datetime.now() def update(self): # increment the total number of frames examined during the # start and end intervals self._num_frames += 1 def elapsed(self): # return the total number of seconds between the start and # end interval return (self._end - self._start).total_seconds() def fps(self): # compute the (approximate) frames per second return self._num_frames / self.elapsed() def refined_box(left, top, width, height): right = left + width bottom = top + height original_vert_height = bottom - top top = int(top + original_vert_height * 0.15) bottom = int(bottom - original_vert_height * 0.05) margin = ((bottom - top) - (right - left)) // 2 left = left - margin if (bottom - top - right + left) % 2 == 0 else left - margin - 1 right = right + margin return left, top, right, bottom #dlib face recognition def load_encodings(fname): global data data = pickle.loads(open(fname, "rb").read()) def recognize_face(frame,boxes): #Converting boxes according to face_recognition reboxes=[] for j in boxes: reboxes.append([j[1],j[0]+j[2],j[1]+j[3],j[0]]) #Convert BGR to RGB rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # compute the facial embeddings for each face bounding box encodings = face_recognition.face_encodings(rgb, reboxes) names = [] # loop over the facial embeddings for encoding in encodings: # attempt to match each face in the input image to our known # encodings matches = face_recognition.compare_faces(data["encodings"], encoding,0.6) name = "Unknown" # check to see if we have found a match if True in matches: # find the indexes of all matched faces then initialize a # dictionary to count the total number of times each face # was matched matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number # of votes (note: in the event of an unlikely tie Python # will select first entry in the dictionary) name = max(counts, key=counts.get) # update the list of names names.append(name) # loop over the recognized faces for ((x, y, w, h), name) in zip(boxes, names): # draw the predicted face name on the image cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) s = y - 15 if y - 15 > 15 else y + 15 cv2.putText(frame, name, (x, s), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) print("Faces recognized:",names)
python
#!/usr/bin/python3 # encoding: utf-8 """ @author: m1n9yu3 @license: (C) Copyright 2021-2023, Node Supply Chain Manager Corporation Limited. @file: web_server.py @time: 2021/4/27 13:41 @desc: """ from flask import * from huluxiaThirdflood_api import get_random_imageurl import conf app = Flask(__name__) @app.route('/') def hello_world(): # 爬取 12 个帖子 image_list = get_random_imageurl(conf.display_num) # print(image_list) display_image = [] for i in range(0, len(image_list), 3): try: display_image.append([["#imageModal%d" % i, image_list[i]], ["#imageModal%d" % (i+1), image_list[i+1]], ["#imageModal%d" % (i+2), image_list[i+2]]]) except Exception as e: # 报错说明, 爬取到的图片不足3 的倍数 pass large_image = [] for image in display_image: large_image += [[i[0].replace('#', ""), i[1]] for i in image] # print(large_image) return render_template('index.html', imagelist=display_image, large_image=large_image) @app.route('/favicon.ico') def favicon(): return app.send_static_file('images/favicon.ico') @app.route('/<num>') def displayImage(num): # 这里数量达到 100 时,会发生 index out of range 异常, 正在想办法整改 num = int(num) image_list = get_random_imageurl(num) # print(image_list) display_image = [] for i in range(0, len(image_list), 3): try: display_image.append([["#imageModal%d" % i, image_list[i]], ["#imageModal%d" % (i+1), image_list[i+1]], ["#imageModal%d" % (i+2), image_list[i+2]]]) except Exception as e: # 报错说明, 爬取到的图片不足3 的倍数 pass large_image = [] for image in display_image: large_image += [[i[0].replace('#', ""), i[1]] for i in image] # print(large_image) return render_template('index.html', imagelist=display_image, large_image=large_image) if __name__ == '__main__': app.run(debug=True, host="0.0.0.0", port=8999)
python
import UDPComms import numpy as np import time from pupper.Config import Configuration from src.State import BehaviorState, State, ArmState from src.Command import Command from src.Utilities import deadband, clipped_first_order_filter class JoystickInterface: def __init__( self, config: Configuration, udp_port=8830, udp_publisher_port=8840, ): self.config = config self.previous_gait_toggle = 0 self.previous_state = BehaviorState.REST self.previous_hop_toggle = 0 self.previous_activate_toggle = 0 self.previous_record_toggle = 0 self.previous_arm_toggle = 0 self.previous_grab_toggle = 0 self.message_rate = 50 self.udp_handle = UDPComms.Subscriber(udp_port, timeout=0.3) self.udp_publisher = UDPComms.Publisher(udp_publisher_port) def get_command(self, state, do_print=False): try: msg = self.udp_handle.get() command = Command() ####### Handle discrete commands ######## # Check if requesting a state transition to trotting, or from trotting to resting arm_toggle = msg["R2"] command.arm_event = arm_toggle > 0.99 and self.previous_arm_toggle <= 0.99 # Check if requesting recording record_toggle = msg["L2"] command.record_event = record_toggle == 1 and self.previous_record_toggle == 0 if state.arm_state is ArmState.DEACTIVATED: activate_toggle = msg["L1"] command.activate_event = activate_toggle == 1 and self.previous_activate_toggle == 0 gait_toggle = msg["R1"] command.trot_event = gait_toggle == 1 and self.previous_gait_toggle == 0 # Check if requesting a state transition to hopping, from trotting or resting hop_toggle = msg["x"] command.hop_event = hop_toggle == 1 and self.previous_hop_toggle == 0 grab_toggle = 0 else: gait_toggle = 0 activate_toggle = 0 hop_toggle = 0 grab_toggle = msg["x"] command.gripper_event = grab_toggle == 1 and self.previous_grab_toggle == 0 # Update previous values for toggles and state self.previous_arm_toggle = arm_toggle self.previous_gait_toggle = gait_toggle self.previous_hop_toggle = hop_toggle self.previous_grab_toggle = grab_toggle self.previous_activate_toggle = activate_toggle if state.arm_state is ArmState.DEACTIVATED: ####### Handle continuous commands ######## x_vel = msg["ly"] * self.config.max_x_velocity y_vel = msg["lx"] * -self.config.max_y_velocity command.horizontal_velocity = np.array([x_vel, y_vel]) command.yaw_rate = msg["rx"] * -self.config.max_yaw_rate else: command.arm_x_diff = msg["lx"] * self.config.arm_x_factor command.arm_y_diff = msg["ly"] * self.config.arm_y_factor command.arm_z_diff += msg["R1"] * self.config.arm_z_factor command.arm_z_diff -= msg["L1"] * self.config.arm_z_factor message_rate = msg["message_rate"] message_dt = 1.0 / message_rate pitch = msg["ry"] * self.config.max_pitch deadbanded_pitch = deadband(pitch, self.config.pitch_deadband) pitch_rate = clipped_first_order_filter( state.pitch, deadbanded_pitch, self.config.max_pitch_rate, self.config.pitch_time_constant, ) command.pitch = state.pitch + message_dt * pitch_rate height_movement = msg["dpady"] command.height = state.height - message_dt * self.config.z_speed * height_movement roll_movement = -msg["dpadx"] command.roll = state.roll + message_dt * self.config.roll_speed * roll_movement return command except UDPComms.timeout: if do_print: print("UDP Timed out") return Command() def set_color(self, color): joystick_msg = {"ps4_color": color} self.udp_publisher.send(joystick_msg)
python
import cv2 from je_open_cv import template_detection image_data_array = template_detection.find_object("../test.png", "../test_template.png", detect_threshold=0.9, draw_image=True) print(image_data_array) if image_data_array[0] is True: height = image_data_array[1][2] - image_data_array[1][0] width = image_data_array[1][3] - image_data_array[1][1] center = [int(height / 2), int(width / 2)] print(center) cv2.imshow("test", image_data_array[2]) cv2.waitKey(0) cv2.destroyAllWindows()
python
from __future__ import division from itertools import izip, count import matplotlib.pyplot as plt from numpy import linspace, loadtxt, ones, convolve import numpy as np import pandas as pd import collections from random import randint from matplotlib import style style.use('fivethirtyeight') #tab csv data = loadtxt("dataset/sunspots.txt", float) # 2. View the data as a table data_as_frame = pd.DataFrame(data, columns=['Months', 'SunSpots']) data_as_frame.head()
python
# -*- coding: utf-8 -*- from __future__ import unicode_literals import mock import pytest from h.interfaces import IGroupService from h.services.annotation_json_presentation import AnnotationJSONPresentationService from h.services.annotation_json_presentation import annotation_json_presentation_service_factory @pytest.mark.usefixtures('presenters') class TestAnnotationJSONPresentationService(object): def test_initializes_flag_formatter(self, formatters): AnnotationJSONPresentationService(session=mock.sentinel.session, user=mock.sentinel.user, group_svc=mock.sentinel.group_svc, links_svc=mock.sentinel.links_svc, flag_svc=mock.sentinel.flag_svc) formatters.AnnotationFlagFormatter.assert_called_once_with(mock.sentinel.flag_svc, mock.sentinel.user) def test_it_configures_flag_formatter(self, formatters): svc = AnnotationJSONPresentationService(session=mock.sentinel.session, user=mock.sentinel.user, group_svc=mock.sentinel.group_svc, links_svc=mock.sentinel.links_svc, flag_svc=mock.sentinel.flag_svc) assert formatters.AnnotationFlagFormatter.return_value in svc.formatters def test_present_inits_presenter(self, svc, presenters, annotation_resource): svc.present(annotation_resource) presenters.AnnotationJSONPresenter.assert_called_once_with(annotation_resource) def test_present_adds_formatters(self, svc, annotation_resource, presenters): formatters = [mock.Mock(), mock.Mock()] svc.formatters = formatters presenter = presenters.AnnotationJSONPresenter.return_value svc.present(annotation_resource) assert presenter.add_formatter.mock_calls == [mock.call(f) for f in formatters] def test_present_returns_presenter_dict(self, svc, presenters): presenter = presenters.AnnotationJSONPresenter.return_value result = svc.present(mock.Mock()) assert result == presenter.asdict.return_value def test_present_all_loads_annotations_from_db(self, svc, storage): svc.present_all(['id-1', 'id-2']) storage.fetch_ordered_annotations.assert_called_once_with( svc.session, ['id-1', 'id-2'], query_processor=mock.ANY) def test_present_all_initialises_annotation_resources(self, svc, storage, resources): ann = mock.Mock() storage.fetch_ordered_annotations.return_value = [ann] svc.present_all(['ann-1']) resources.AnnotationResource.assert_called_once_with(ann, svc.group_svc, svc.links_svc) def test_present_all_presents_annotation_resources(self, svc, storage, resources, present): storage.fetch_ordered_annotations.return_value = [mock.Mock()] resource = resources.AnnotationResource.return_value svc.present_all(['ann-1']) present.assert_called_once_with(svc, resource) def test_present_all_preloads_formatters(self, svc, storage): formatter = mock.Mock(spec_set=['preload']) svc.formatters = [formatter] svc.present_all(['ann-1', 'ann-2']) formatter.preload.assert_called_once_with(['ann-1', 'ann-2']) def test_returns_presented_annotations(self, svc, storage, present): storage.fetch_ordered_annotations.return_value = [mock.Mock()] result = svc.present_all(['ann-1']) assert result == [present.return_value] @pytest.fixture def svc(self, db_session): group_svc = mock.Mock() links_svc = mock.Mock() flag_svc = mock.Mock() return AnnotationJSONPresentationService(session=db_session, user=None, group_svc=group_svc, links_svc=links_svc, flag_svc=flag_svc) @pytest.fixture def annotation_resource(self): return mock.Mock(spec_set=['annotation'], annotation=mock.Mock()) @pytest.fixture def presenters(self, patch): return patch('h.services.annotation_json_presentation.presenters') @pytest.fixture def storage(self, patch): return patch('h.services.annotation_json_presentation.storage') @pytest.fixture def resources(self, patch): return patch('h.services.annotation_json_presentation.resources') @pytest.fixture def present(self, patch): return patch('h.services.annotation_json_presentation.AnnotationJSONPresentationService.present') @pytest.fixture def formatters(self, patch): return patch('h.services.annotation_json_presentation.formatters') @pytest.mark.usefixtures('group_svc', 'links_svc', 'flag_svc') class TestAnnotationJSONPresentationServiceFactory(object): def test_returns_service(self, pyramid_request): svc = annotation_json_presentation_service_factory(None, pyramid_request) assert isinstance(svc, AnnotationJSONPresentationService) def test_provides_session(self, pyramid_request, service_class): annotation_json_presentation_service_factory(None, pyramid_request) _, kwargs = service_class.call_args assert kwargs['session'] == pyramid_request.db def test_provides_user(self, pyramid_request, service_class): annotation_json_presentation_service_factory(None, pyramid_request) _, kwargs = service_class.call_args assert kwargs['user'] == pyramid_request.user def test_provides_group_service(self, pyramid_request, service_class, group_svc): annotation_json_presentation_service_factory(None, pyramid_request) _, kwargs = service_class.call_args assert kwargs['group_svc'] == group_svc def test_provides_links_service(self, pyramid_request, service_class, links_svc): annotation_json_presentation_service_factory(None, pyramid_request) _, kwargs = service_class.call_args assert kwargs['links_svc'] == links_svc def test_provides_flag_service(self, pyramid_request, service_class, flag_svc): annotation_json_presentation_service_factory(None, pyramid_request) _, kwargs = service_class.call_args assert kwargs['flag_svc'] == flag_svc @pytest.fixture def service_class(self, patch): return patch('h.services.annotation_json_presentation.AnnotationJSONPresentationService') @pytest.fixture def group_svc(self, pyramid_config): svc = mock.Mock() pyramid_config.register_service(svc, iface=IGroupService) return svc @pytest.fixture def links_svc(self, pyramid_config): svc = mock.Mock() pyramid_config.register_service(svc, name='links') return svc @pytest.fixture def flag_svc(self, pyramid_config): svc = mock.Mock() pyramid_config.register_service(svc, name='flag') return svc @pytest.fixture def pyramid_request(self, pyramid_request): pyramid_request.user = mock.Mock() return pyramid_request
python
from flask import Flask from flask_restful import Resource, Api from flask_cors import CORS from api.db_utils import * from api.Culprit_api import * app = Flask(__name__) #create Flask instance CORS(app) api = Api(app) #api router api.add_resource(CaseSetup,'/case-setup') api.add_resource(Session, '/key') api.add_resource(PlayerName, '/name') api.add_resource(Tokens, '/token') api.add_resource(TokenRemove, '/token-remove') api.add_resource(Poll, '/poll') api.add_resource(PollExclude, '/poll-exclude') api.add_resource(Accuse, '/accuse') api.add_resource(CheckEndGame, '/check-end') api.add_resource(EndGameData, '/end') api.add_resource(LoadGame, '/load') if __name__ == '__main__': print("Building Database") build_tables() print("Loading Data") load_game_data() print("Starting Flask") app.run(debug=True), #starts Flask
python
from ._sw import Controller
python
# -*- coding: utf-8 -*- from __future__ import ( absolute_import, division, print_function, unicode_literals) import json try: from urllib import parse as urlparse except ImportError: import urlparse from operator import itemgetter import yaml from flask import jsonify, request, Blueprint from builtins import * # noqa # pylint: disable=unused-import SWAGGER_TYPES = { 'bool': 'bool', 'int': 'integer', 'dec': 'number', 'float': 'float', 'str': 'string', 'date': 'date', 'datetime': 'date-time', } JSON_TYPES = { 'bool': 'boolean', 'float': 'number', 'binary': 'string', 'date': 'string', 'date-time': 'string', } def get_column_defn(column): stype = SWAGGER_TYPES[column['type']] if stype in JSON_TYPES: column_defn = {'type': JSON_TYPES[stype], 'format': stype} else: column_defn = {'type': stype} return column_defn class Swaggerify(object): swagger = { 'swagger': '2.0', 'info': {}, 'tags': [], 'schemes': ['https', 'http'], 'basePath': '/', 'consumes': ['application/json'], 'produces': ['application/json'], 'paths': {}, 'definitions': {} } def __init__(self, app=None, **kwargs): self.app = None if app is not None: self.init_app(app, **kwargs) def to_json(self, **kwargs): return json.dumps(self.swagger, **kwargs) def to_yaml(self, **kwargs): return yaml.dump(self.swagger, **kwargs) def __str__(self): return self.to_json(indent=4) @property def tags(self): return set(tag['name'] for tag in self.swagger['tags']) @tags.setter def tags(self, value): self.swagger['tags'] = value @property def version(self): if 'version' in self.swagger['info']: return self.swagger['info']['version'] return None @version.setter def version(self, value): self.swagger['info']['version'] = value @property def title(self): if 'title' in self.swagger['info']: return self.swagger['info']['title'] return None @title.setter def title(self, value): self.swagger['info']['title'] = value @property def description(self): if 'description' in self.swagger['info']: return self.swagger['info']['description'] return None @description.setter def description(self, value): self.swagger['info']['description'] = value def add_path(self, table, **kwargs): path = '{0}/{name}'.format(kwargs.get('url_prefix', ''), **table) parameters = [] for column in table['columns']: if column['kind'] in {'param', 'type'}: param = {'in': 'path', 'required': True} path = '{0}/{{{name}}}'.format(path, **column) elif column['kind'] in {'keyword', 'kwtype'}: param = {'in': 'query'} if column['kind'] in {'param', 'keyword', 'type', 'kwtype'}: param.update( {'name': column['name'], 'description': column['desc']}) param.update(get_column_defn(column)) parameters.append(param) self.swagger['paths'][path] = {} ref = '#/definitions/{rtype}'.format(**table) if table.get('desc'): self.swagger['paths'][path]['description'] = table['desc'] if table.get('list'): _schema = {'type': 'array', 'items': {'$ref': ref}} schema = {'type': 'object', 'properties': {'objects': _schema}} else: schema = {'$ref': ref} self.swagger['paths'][path]['get'] = { 'summary': table.get('desc', 'get {name}'.format(**table)), 'tags': [table['tag']] if table.get('tag') else [], 'parameters': parameters, 'responses': { 200: { 'description': '{name} result'.format(**table), 'schema': schema}}} if table.get('tag') and table['tag'] not in self.tags: tag = { 'name': table['tag'], 'description': '{tag} operations'.format(**table)} self.swagger['tags'].append(tag) def add_defn(self, table): def_value = {'type': 'object', 'properties': {}} for column in sorted(table['columns'], key=itemgetter('name')): excluded = column['name'] in self.exclude_columns if excluded or column['name'] == 'id': continue column_defn = get_column_defn(column) if column.get('desc'): column_defn['description'] = column['desc'] def_value['properties'][column['name']] = column_defn self.swagger['definitions'][table['name']] = def_value def init_app(self, app, **kwargs): self.app = app swagger = Blueprint('swagger', __name__) if kwargs.get('name'): self.title = kwargs['name'] if kwargs.get('version'): self.version = kwargs['version'] if kwargs.get('description'): self.description = kwargs['description'] @swagger.route('/swagger.json') def swagger_json(): # Must have a request context self.swagger['host'] = urlparse.urlparse(request.url_root).netloc return jsonify(self.swagger) app.register_blueprint(swagger) def create_docs(self, table, **kwargs): self.exclude_columns = set(kwargs.get('exclude_columns', [])) if not kwargs.get('skip_defn'): self.add_defn(table) if not kwargs.get('skip_path'): self.add_path(table, **kwargs)
python
import yaml import rclpy import std_msgs.msg as std import geometry_msgs.msg as geom from msg_printer.std_yaml import YamlHeader class YamlAccel(yaml.YAMLObject): yaml_tag = u"!Accel" def __init__(self, a: geom.Accel): self._dict = { "linear": YamlVector3(a.linear).dict, "angular": YamlVector3(a.angular).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlAccelStamped(yaml.YAMLObject): yaml_tag = u"!AccelStamped" def __init__(self, a: geom.AccelStamped): self._dict = { "header": YamlHeader(a.header).dict, "accel": YamlAccel(a.accel).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlAccelWithCovariance(yaml.YAMLObject): yaml_tag = u"!AccelWithCovariance" def __init__(self, a: geom.AccelWithCovariance): self._dict = { "accel": YamlAccel(a.accel).dict, "covariance": [float(elem) for elem in a.covariance], } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlAccelWithCovarianceStamped(yaml.YAMLObject): yaml_tag = u"!AccelWithCovarianceStamped" def __init__(self, a: geom.AccelWithCovarianceStamped): self._dict = { "header": YamlHeader(a.header).dict, "accel": YamlAccelWithCovariance(a.accel).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlInertia(yaml.YAMLObject): yaml_tag = u"!Inertia" def __init__(self, i: geom.Inertia): self._dict = { "m": i.m, "com": YamlVector3(i.com).dict, "ixx": i.ixx, "ixy": i.ixy, "ixz": i.ixz, "iyy": i.iyy, "iyz": i.iyz, "izz": i.izz, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlInertiaStamped(yaml.YAMLObject): yaml_tag = u"!InertiaStamped" def __init__(self, i: geom.InertiaStamped): self._dict = { "header": YamlHeader(i.header).dict, "inertia": YamlInertia(i.inertia).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoint(yaml.YAMLObject): yaml_tag = u"!Point" def __init__(self, p: geom.Point): self._dict = {"x": p.x, "y": p.y, "z": p.z} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPointStamped(yaml.YAMLObject): yaml_tag = u"!PointStamped" def __init__(self, p: geom.PointStamped): self._dict = { "header": YamlHeader(p.header).dict, "point": YamlPoint(p.point).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoint32(yaml.YAMLObject): yaml_tag = u"!Point32" def __init__(self, p: geom.Point32): self._dict = {"x": p.x, "y": p.y, "z": p.z} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPolygon(yaml.YAMLObject): yaml_tag = u"!Polygon" def __init__(self, p: geom.Polygon): self._dict = {"points": [YamlPoint32(point).dict for point in p.points]} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPolygonStamped(yaml.YAMLObject): yaml_tag = u"!PolygonStamped" def __init__(self, p: geom.PolygonStamped): self._dict = { "header": YamlHeader(p.header).dict, "polygon": YamlPolygon(p.polygon).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPose(yaml.YAMLObject): yaml_tag = u"!Pose" def __init__(self, p: geom.Pose): self._dict = { "position": YamlPoint(p.position).dict, "orientation": YamlQuaternion(p.orientation).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPose2D(yaml.YAMLObject): yaml_tag = u"!Pose2D" def __init__(self, p: geom.Pose2D): self._dict = {"x": p.x, "y": p.y, "theta": p.theta} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoseArray(yaml.YAMLObject): yaml_tag = u"!PoseArray" def __init__(self, p: geom.PoseArray): self._dict = { "header": YamlHeader(p.header).dict, "poses": [YamlPose(pose).dict for pose in p.poses], } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoseStamped(yaml.YAMLObject): yaml_tag = u"!PoseStamped" def __init__(self, p: geom.PoseStamped): self._dict = { "header": YamlHeader(p.header).dict, "pose": YamlPose(p.pose).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoseWithCovariance(yaml.YAMLObject): yaml_tag = u"!PoseWithCovariance" def __init__(self, p: geom.PoseWithCovariance): self._dict = { "pose": YamlPose(p.pose).dict, "covariance": [float(elem) for elem in p.covariance], } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlPoseWithCovarianceStamped(yaml.YAMLObject): yaml_tag = u"!PoseWithCovarianceStamped" def __init__(self, p: geom.PoseWithCovarianceStamped): self._dict = { "header": YamlHeader(p.header).dict, "pose": YamlPoseWithCovariance(p.pose).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlQuaternion(yaml.YAMLObject): yaml_tag = u"!Quaternion" def __init__(self, q: geom.Quaternion): self._dict = {"x": q.x, "y": q.y, "z": q.z, "w": q.w} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlQuaternionStamped(yaml.YAMLObject): yaml_tag = u"!QuaternionStamped" def __init__(self, q: geom.QuaternionStamped): self._dict = { "header": YamlHeader(q.header).dict, "quaternion": YamlQuaternion(q.quaternion).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTransform(yaml.YAMLObject): yaml_tag = u"!Transform" def __init__(self, t: geom.Transform): self._dict = { "translation": YamlVector3(t.translation).dict, "rotation": YamlQuaternion(t.rotation).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTransformStamped(yaml.YAMLObject): yaml_tag = u"!TransformStamped" def __init__(self, t: geom.TransformStamped): self._dict = { "header": YamlHeader(t.header).dict, "child_frame_id": t.child_frame_id, "transform": YamlTransform(t.transform).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTwist(yaml.YAMLObject): yaml_tag = u"!Twist" def __init__(self, t: geom.Twist): self._dict = { "linear": YamlVector3(t.linear).dict, "angular": YamlVector3(t.angular).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTwistStamped(yaml.YAMLObject): yaml_tag = u"!TwistStamped" def __init__(self, t: geom.TwistStamped): self._dict = { "header": YamlHeader(t.header).dict, "twist": YamlTwist(t.twist).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTwistWithCovariance(yaml.YAMLObject): yaml_tag = u"!TwistWithCovariace" def __init__(self, t: geom.TwistWithCovariance): self._dict = { "twist": YamlTwist(t.twist).dict, "covariance": [float(elem) for elem in t.covariance], } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlTwistWithCovarianceStamped(yaml.YAMLObject): yaml_tag = u"!TwistWithCovarianceStamped" def __init__(self, t: geom.TwistWithCovarianceStamped): self._dict = { "header": YamlHeader(t.header).dict, "twist": YamlTwistWithCovariance(t.twist).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlVector3(yaml.YAMLObject): yaml_tag = u"!Vector3" def __init__(self, v: geom.Vector3): self._dict = {"x": v.x, "y": v.y, "z": v.z} @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlVector3Stamped(yaml.YAMLObject): yaml_tag = u"!Vector3Stamped" def __init__(self, v: geom.Vector3Stamped): self._dict = { "header": YamlHeader(v.header).dict, "vector": YamlVector3(v.vector).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlWrench(yaml.YAMLObject): yaml_tag = u"!Wrench" def __init__(self, w: geom.Wrench): self._dict = { "force": YamlVector3(w.force).dict, "torque": YamlVector3(w.torque).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) class YamlWrenchStamped(yaml.YAMLObject): yaml_tag = u"!WrenchStamped" def __init__(self, w: geom.WrenchStamped): self._dict = { "header": YamlHeader(w.header).dict, "wrench": YamlWrench(w.wrench).dict, } @property def dict(self): return self._dict @classmethod def to_yaml(cls, dumper, data): return dumper.represent_mapping(cls.yaml_tag, data.dict) def main(args=None): rclpy.init(args=args) node = rclpy.create_node("test") test_msg = geom.WrenchStamped() node.get_logger().info(yaml.dump(YamlWrenchStamped(test_msg), sort_keys=False)) node.destroy_node() rclpy.shutdown() if __name__ == "__main__": main()
python
import os import sys import time import argparse from naoqi import ALProxy import robot_behavior_pb2 from os.path import dirname from os.path import abspath def register_motions(name,parameterServerAddress,motions): behaviorModule = robot_behavior_pb2.RobotBehaviorModule() behaviorModule.name = name for motion in motions: desc = behaviorModule.behaviors.add() desc.name = motion desc.type = robot_behavior_pb2.BehaviorDescription.Blocking desc.state = robot_behavior_pb2.BehaviorDescription.Idle print behaviorModule register_motions("hello","",["move","sit","stand"]) currdir = dirname(__file__) parent = abspath(os.path.join(currdir,os.pardir)) i = 0 bin = os.environ["DEV_SDK_ROOT"] print bin parent = currdir while i<4: #parent = abspath(os.path.join(parent,os.pardir)) if not "scripts" in parent: i=i+1 continue else: break i=i+1 print currdir print parent for dirname, dirnames, filenames in os.walk(parent): # print path to all subdirectories first. for subdirname in dirnames: print os.path.join(dirname, subdirname) sys.path.append(currdir) sys.path.append(parent)
python
#!/usr/bin/env python domain_name = os.environ['DOMAIN_NAME'] admin_server_listen_address = os.environ['ADMIN_SERVER_LISTEN_ADDRESS'] admin_server_listen_port = os.environ['ADMIN_SERVER_LISTEN_PORT'] admin_username = os.environ['ADMIN_USERNAME'] admin_password = os.environ['ADMIN_PASSWORD'] ###################################################################### def set_domain_web_app(_domain_name): cd('/WebAppContainer/' + _domain_name) # cmo.setReloginEnabled(false) # cmo.setAllowAllRoles(false) # cmo.setFilterDispatchedRequestsEnabled(false) # cmo.setOverloadProtectionEnabled(false) cmo.setXPoweredByHeaderLevel('NONE') # cmo.setMimeMappingFile('./config/mimemappings.properties') # cmo.setOptimisticSerialization(false) # cmo.setRtexprvalueJspParamName(false) # cmo.setClientCertProxyEnabled(false) # cmo.setHttpTraceSupportEnabled(false) # cmo.setWeblogicPluginEnabled(false) # cmo.setAuthCookieEnabled(true) # cmo.setChangeSessionIDOnAuthentication(true) # cmo.setWAPEnabled(false) # cmo.setPostTimeoutSecs(30) # cmo.setMaxPostTimeSecs(-1) # cmo.setMaxPostSize(-1) # cmo.setWorkContextPropagationEnabled(true) # cmo.setP3PHeaderValue('') # cmo.setJSPCompilerBackwardsCompatible(false) # cmo.setShowArchivedRealPathEnabled(false) ###################################################################### admin_server_url = 't3://' + admin_server_listen_address + ':' + admin_server_listen_port connect(admin_username, admin_password, admin_server_url) edit() startEdit() set_domain_web_app(domain_name) save() activate() exit()
python
from django.db import models import json # Create your models here. class weather(models.Model): ''' 溫度、最高溫、最低溫、露點溫度、相對濕度、最小相對濕度、降雨量、最大十分鐘降雨量、最大六十分鐘降雨量 ''' date = models.DateTimeField() temperature = models.FloatField() relativeHumidity = models.FloatField() rainfall = models.FloatField() maxTenMinuteRainFall = models.FloatField() maxSixtyMinuteRainFall = models.FloatField() class hole(models.Model): ''' 區、經度、緯度、時間、原因、住址、淹水潛勢 ''' town = models.CharField(max_length=5) positionLon = models.FloatField() positionLat = models.FloatField() occurTime = models.DateTimeField() reason = models.CharField(max_length=50,blank=True) address = models.CharField(max_length=100,blank=True) flood = models.IntegerField() class examination(models.Model): ''' 巡視結果 ''' positionLon = models.FloatField() positionLat = models.FloatField() examinationTime = models.DateTimeField(auto_now=True) photoURL = models.CharField(max_length=100,blank=True) class modelResult(models.Model): ''' 儲存model, Astar預測結果 暫時忽略polyList ''' date = models.DateTimeField() route = models.CharField(max_length=200) def set_route(self,data): self.route = json.dumps(data) def get_route(self): return json.loads(self.route)
python
class Solution: #c1 is always opening type def counter_part(self, c1: str, c2:str)->bool: if c1 == '(' and c2 == ')': return True if c1 == '{' and c2 == '}': return True if c1 == '[' and c2 == ']': return True return False def isValid(self, s: str) -> bool: stack=[] opening_type=['(', '{', '['] closing_type=[')', '}', ']'] for x in s: if x in opening_type: print("here") stack.append(x) if x in closing_type: try: c=stack.pop() if self.counter_part(c, x): #we good, continue with next char in i/p string print("here2", c, x) continue else: #we dont have a matched print("here3",x, c) return False except IndexError:#we found a closing type, but nothing left in stack return False try: stack.pop() return False except IndexError: return True
python
from __future__ import unicode_literals __VERSION__ = '0.1.1'
python
import os ############################################################################### def create_dir(path): if not os.path.isdir(path): os.makedirs(path) ############################################################################### # SAVE A DICTIONARY dict_ = {} import json with open(os.path.join('name.json'), 'w') as fp: json.dump(dict_, fp) fp.close() ############################################################################### # GET LINES OF A TEXT FILE def readText(file_path): with open(file_path) as f: lines = f.readlines() f.close() return lines
python
# KicadModTree is free software: you can redistribute it and/or # modify it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # KicadModTree is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with kicad-footprint-generator. If not, see < http://www.gnu.org/licenses/ >. # # (C) 2016-2018 by Thomas Pointhuber, <[email protected]> # (C) 2018 by Rene Poeschl, github @poeschlr import warnings from KicadModTree.Vector import Vector2D from KicadModTree.nodes.Node import Node class PolygonPoints(object): r"""Representation of multiple points for creating polygons :Keyword Arguments: * *nodes* (``list(Point)``) -- 2D points describing the "polygon" * *polygone* (``list(Point)``) -- alternative naming for the nodes parameter for backwards compatibility. * *x_mirror* (``[int, float](mirror offset)``) -- mirror x direction around offset "point" * *y_mirror* (``[int, float](mirror offset)``) -- mirror y direction around offset "point" :Example: >>> from KicadModTree import * >>> PolyPoint([(0, 0),(1, 0)]) >>> PolyPoint([{'x': 0, 'y':0}, {'x': 1, 'y':0}]) """ def __init__(self, **kwargs): self._initMirror(**kwargs) self._initNodes(**kwargs) def _initNodes(self, **kwargs): self.nodes = [] if 'nodes' in kwargs: for n in kwargs['nodes']: self.nodes.append(Vector2D(n)) if 'polygone' in kwargs: raise KeyError('Use of "nodes" and "polygone" parameter at the same time is not supported.') elif 'polygone' in kwargs: warnings.warn( "polygone argument is deprecated, use nodes instead", DeprecationWarning ) for n in kwargs['polygone']: self.nodes.append(Vector2D(n)) else: raise KeyError('Either "nodes" or "polygone" parameter is required for creating a PolyPoint instance.') for point in self.nodes: if self.mirror[0] is not None: point.x = 2 * self.mirror[0] - point.x if self.mirror[1] is not None: point.y = 2 * self.mirror[1] - point.y def _initMirror(self, **kwargs): self.mirror = [None, None] if 'x_mirror' in kwargs and type(kwargs['x_mirror']) in [float, int]: self.mirror[0] = kwargs['x_mirror'] if 'y_mirror' in kwargs and type(kwargs['y_mirror']) in [float, int]: self.mirror[1] = kwargs['y_mirror'] def calculateBoundingBox(self): min = max = self.getRealPosition(self.nodes[0]) for n in self.nodes: min.x = min([min.x, n.x]) min.y = min([min.y, n.y]) max.x = max([max.x, n.x]) max.y = max([max.y, n.y]) return Node.calculateBoundingBox({'min': min, 'max': max}) def findNearestPoints(self, other): r""" Find the nearest points for two polygons Find the two points for both polygons that are nearest to each other. :param other: the polygon points of the other polygon :return: a tuble with the indexes of the two points (pint in self, point in other) """ min_distance = self[0].distance_to(other[0]) pi = 0 pj = 0 for i in range(len(self)): for j in range(len(other)): d = self[i].distance_to(other[j]) if d < min_distance: pi = i pj = j min_distance = d return (pi, pj) def getPoints(self): r""" get the points contained within self :return: the array of points contained within this instance """ return self.nodes def cut(self, other): r""" Cut other polygon points from self As kicad has no native support for cuting one polygon from the other, the cut is done by connecting the nearest points of the two polygons with two lines on top of each other. This function assumes that the other polygon is fully within this one. It also assumes that connecting the two nearest points creates a valid polygon. (There are no geometry checks) :param other: the polygon points that are cut from this polygon """ warnings.warn( "No geometry checks are implement for cutting polygons.\n" "Make sure the second polygon is fully inside the main polygon\n" "Check resulting polygon carefully.", Warning ) idx_self, idx_other = self.findNearestPoints(other) self.nodes.insert(idx_self+1, self[idx_self]) for i in range(len(other)): self.nodes.insert(idx_self+1, other[(i+idx_other) % len(other)]) self.nodes.insert(idx_self+1, other[idx_other]) def rotate(self, angle, origin=(0, 0), use_degrees=True): r""" Rotate points around given origin :params: * *angle* (``float``) rotation angle * *origin* (``Vector2D``) origin point for the rotation. default: (0, 0) * *use_degrees* (``boolean``) rotation angle is given in degrees. default:True """ for p in self.nodes: p.rotate(angle=angle, origin=origin, use_degrees=use_degrees) return self def translate(self, distance_vector): r""" Translate points :params: * *distance_vector* (``Vector2D``) 2D vector defining by how much and in what direction to translate. """ for p in self.nodes: p += distance_vector return self def __copy__(self): return PolygonPoints(nodes=self.nodes) def __iter__(self): for n in self.nodes: yield n def __getitem__(self, idx): return self.nodes[idx] def __len__(self): return len(self.nodes)
python
import sys from antlr4 import * import graphGenerator from MyVisitor import MyVisitor from PythonLexer import PythonLexer from PythonParser import PythonParser import os def testefunc(graph, code, function): file = open("testfile.txt", "w") file.write(code) file.close() input=FileStream("testfile.txt") if os.path.isfile("textfile.txt"): os.remove("testfile.txt") lexer = PythonLexer(input) stream = CommonTokenStream(lexer) parser = PythonParser(stream) tree = parser.root() visitor = MyVisitor() visitor.visit(tree) src = "" if graph == "FCG": src = graphGenerator.callGraph(visitor.getCall()) elif graph == "CFG": src = graphGenerator.controlGraph(visitor.getControl(), function) elif graph == "DFG": src = graphGenerator.dataGraph(visitor.getData(), function) return src def getFunctions(code): file = open("testfile.txt", "w") file.write(code) file.close() input=FileStream("testfile.txt") os.remove("testfile.txt") lexer = PythonLexer(input) stream = CommonTokenStream(lexer) parser = PythonParser(stream) tree = parser.root() visitor = MyVisitor() visitor.visit(tree) return visitor.getListFunctions()
python
#!/usr/bin/python3 # Modifies the assembly output of compilation for control_mem_dtlb_store # to provide necessary pattern for successful TLB/store attack on gem5 # See parse_tlb_logs.py for more details # generated with gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) # command: gcc src/control_mem_dtlb_store -S -o bin/control_mem_dtlb_store_pre_mod.s pre_transmit = ['movl %eax, %eax\n', '\tcmpq %rax, -2104(%rbp)\n', ] pre_transmit_mod = ['movl %eax, %eax\n', '\tcmpq %rax, -2104(%rbp)\n', '\tmovq -2104(%rbp), %rax\n', ] post_transmit = ['leaq array1(%rip), %rdx\n', '\tmovq -2104(%rbp), %rax\n', ] post_transmit_mod = ['leaq array1(%rip), %rdx\n',] with open('bin/control_mem_dtlb_store_pre_mod.s', 'r') as asm: data = asm.read().replace(''.join(pre_transmit), ''.join(pre_transmit_mod)).replace( ''.join(post_transmit), ''.join(post_transmit_mod) ) asm.close() with open('bin/control_mem_dtlb_store_post_mod.s', 'w') as asm: asm.write(data) asm.close()
python
# coding=utf-8 # -------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. # # Code generated by Microsoft (R) AutoRest Code Generator. # Changes may cause incorrect behavior and will be lost if the code is # regenerated. # -------------------------------------------------------------------------- from .subscriptions_operations import SubscriptionsOperations from .tenants_operations import TenantsOperations from .subscription_operations import SubscriptionOperations from .operations import Operations from .alias_operations import AliasOperations from .subscription_policy_operations import SubscriptionPolicyOperations from .billing_account_operations import BillingAccountOperations __all__ = [ 'SubscriptionsOperations', 'TenantsOperations', 'SubscriptionOperations', 'Operations', 'AliasOperations', 'SubscriptionPolicyOperations', 'BillingAccountOperations', ]
python
#-*- encoding: utf-8 -*- import redis r=redis.Redis(host='localhost',db=0) p=r.pubsub() p.subscribe('test') for message in p.listen(): print(message)
python
# Holds permission data for a private race room def get_permission_info(server, race_private_info): permission_info = PermissionInfo() for admin_name in race_private_info.admin_names: for role in server.roles: if role.name.lower() == admin_name.lower(): permission_info.admin_roles.append(role) for member in server.members: if member.name.lower() == admin_name.lower(): permission_info.admins.append(member) for racer_name in race_private_info.racer_names: for member in server.members: if member.name.lower() == racer_name.lower(): permission_info.racers.append(member) return permission_info class PermissionInfo(object): def __init__(self): self.admins = [] self.admin_roles = [] self.racers = [] def is_admin(self, member): for role in member.roles: if role in self.admin_roles: return True return member in self.admins
python
""" loader module provides actual implementation of the file savers. .. warning:: This is an internal implementation. API may change without notice in the future, so you should use :class:`word_embedding_loader.word_embedding.WordEmbedding` """ __all__ = ["glove", "word2vec_bin", "word2vec_text"] from word_embedding_loader.saver import glove, word2vec_bin, word2vec_text
python
# Stimulator class # Imports #from StimulationSignal import StimulationSignal import crccheck.checksum import numpy as np import serial import time import struct # channel_stim: list of active channels # freq: main stimulation frequency in Hz (NOTE: this overrides ts1) # ts1: main stimulation period in ms (1-1024.5 ms in 0.5 steps) # ts2: inter-pulse time in ms (1.5-17 ms in 0.5 steps) # Notes: # - Revoir les principes d'orienté objet (encapsulation) # - Indentation : 4 espaces class Stimulator: # Class variables VERSION = 0x01 INIT_REPETITION_TIME = 0.5 START_BYTE = 0xF0 STOP_BYTE = 0x0F STUFFING_BYTE = 0x81 STUFFING_KEY = 0x55 MAX_PACKET_BYTES = 69 BAUD_RATE = 460800 TYPES = {'Init': 0x01, 'InitAck': 0x02, 'UnknownCommand': 0x03, 'Watchdog': 0x04, 'GetStimulationMode': 0x0A, 'GetStimulationModeAck': 0x0B, 'InitChannelListMode': 0x1E, 'InitChannelListModeAck': 0x1F, 'StartChannelListMode': 0x20, 'StartChannelListModeAck': 0x21, 'StopChannelListMode': 0x22, 'StopChannelListModeAck': 0x23, 'SinglePulse': 0x24, 'SinglePulseAck': 0x25, 'StimulationError': 0x26} # Constuctor def __init__(self, StimulationSignal, port_path): #Changer ts1 pour 1/StimulationSignal.frequency # ---- StimulationSignal = Contient les infos d'amplitude, de fréquence, de durée d'impulsion et le nom du muscle pour chaque électrode ---- # # ---- ts1 = Main stimulation interval ---- # # ---- ts2 = Inter pulse interval (use only if use duplet or triplet) ---- # # ---- Mode = Single pulse, duplet or triplet ---- # # ---- port = open port from port_path ---- # # ---- packet_count = initialise the packet count ---- # self.matrice = StimulationSignal ''' self.electrode_number = 0 idx = [] print(StimulationSignal) for i in range(0,8): if StimulationSignal[0][i]==0: idx.append(i) else: self.electrode_number += (2)**(i) StimulationSignal = np.delete(StimulationSignal, idx, 1) self.idx = idx ''' ''' self.amplitude = [] self.ts1 = [] self.frequency = [] self.pulse_width = [] self.muscle = [] self.ts2 = [] for i in range (8-len(idx)): self.amplitude.append(StimulationSignal[0][i]) self.ts1.append(int((1000/StimulationSignal[1][i] - 1)/0.5)) #à vérifier si bon indice pour fréquence self.frequency.append(StimulationSignal[1][i]) self.pulse_width.append(StimulationSignal[2][i]) #à vérifier si bon indice self.muscle.append(StimulationSignal[3][i]) self.ts2 = ts2 ''' # self.set_StimulationSignal(StimulationSignal) self.port = serial.Serial(port_path, self.BAUD_RATE, bytesize=serial.EIGHTBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=0.1) self.packet_count = 0 #self.initialise_connection() #self.stimulation_220_10() """ while True: received_packet= self.read_packets() self.init_ACK(received_packet) time.sleep(self.INIT_REPETITION_TIME) return""" def initialise_connection(self): while (1): if (self.port.in_waiting>0): self.calling_ACK() break def stimulation_220_10(self): self.set_stim_biceps_DeltPost() self.set_StimulationSignal(self.StimulationSignal) #starttime = time.time() #timer = 0 self.send_packet('InitChannelListMode', self.packet_count) #À MODIFIER POUR AVOIR ANGLES À LA PLACE ### while (1) ### self.send_packet('StartChannelListMode', self.packet_count) ### AJOUTER BREAK DANS ERGOCYCLE #while timer < 5.00: #timer = round(time.time()-starttime,2) self.send_packet('StartChannelListMode', self.packet_count) #time.sleep(1/self.frequency[0]) #if timer >=(5.00-(1/self.frequency[0])): # break def stimulation_20_180(self): self.set_stim_triceps_DeltAnt() self.set_StimulationSignal(self.StimulationSignal) starttime = time.time() timer = 0 self.send_packet('InitChannelListMode', self.packet_count) #À MODIFIER POUR AVOIR ANGLES À LA PLACE ### while (1) ### self.send_packet('StartChannelListMode', self.packet_count) ### AJOUTER BREAK DANS ERGOCYCLE self.send_packet('StartChannelListMode', self.packet_count) def set_matrice(self, Signal): self.matrice = Signal # Function to modify the stimulation's parameters def set_StimulationSignal(self,StimulationSignal): self.amplitude = [] self.ts1 = [] self.frequency = [] self.pulse_width = [] self.muscle = [] for i in range (8-len(self.idx)): self.amplitude.append(StimulationSignal[0][i]) self.ts1.append(int((1000/StimulationSignal[1][i] - 1)/0.5)) #à vérifier si bon indice pour fréquence self.frequency.append(StimulationSignal[1][i]) self.pulse_width.append(StimulationSignal[2][i]) #à vérifier si bon indice self.muscle.append(StimulationSignal[3][i]) def set_stim_biceps_DeltPost(self): idx = [] self.electrode_number = 0 biceps_DeltPost = np.copy(self.matrice) for j in range(np.shape(self.matrice)[1]): if(self.matrice[3][j] == 2 or self.matrice[3][j]== 4): biceps_DeltPost[:,j]=0 for i in range(0,8): if biceps_DeltPost[0][i]==0: idx.append(i) else: self.electrode_number += (2)**(i) biceps_DeltPost = np.delete(biceps_DeltPost, idx, 1) print(self.electrode_number) self.StimulationSignal = biceps_DeltPost self.idx = idx def set_stim_triceps_DeltAnt(self): idx = [] triceps_DeltAnt = np.copy(self.matrice) self.electrode_number = 0 for j in range(np.shape(self.matrice)[1]): if(self.matrice[3][j] == 1 or self.matrice[3][j]== 3): triceps_DeltAnt[:,j]=0 for i in range(0,8): if triceps_DeltAnt[0][i]==0: idx.append(i) else: self.electrode_number += (2)**(i) triceps_DeltAnt = np.delete(triceps_DeltAnt, idx, 1) self.StimulationSignal = triceps_DeltAnt self.idx = idx # Function to modify the time between pulses if doublet or triplet are chose def set_t2(self,t2): self.t2 = t2 # "byte stuffing", i.e, xoring with STUFFING_KEY def stuff_byte(self,byte): return ((byte & ~Stimulator.STUFFING_KEY) | (~byte & Stimulator.STUFFING_KEY)) #return bytes(a ^ b for (a, b) in zip(byte, bitarray(self.STUFFING_KEY))) # Construction of each packet def packet_construction(self,packet_count, packet_type, *packet_data): start_byte = self.START_BYTE stop_byte = self.STOP_BYTE self.packet_type = packet_type packet_command = self.TYPES[packet_type] packet_payload = [packet_count, packet_command] data_length = 0 if packet_data!= None: packet_data = list(packet_data) for i in range (0, len(packet_data)): if packet_data[i] == 240 or packet_data[i] == 15: packet_data[i] = self.stuff_byte(packet_data[i]) packet_payload += packet_data checksum = crccheck.crc.Crc8.calc(packet_payload) checksum = self.stuff_byte(checksum) data_length = self.stuff_byte(len(packet_payload)) packet_lead = [start_byte, self.STUFFING_BYTE, checksum, self.STUFFING_BYTE, data_length] packet_end = [stop_byte] packet = packet_lead + packet_payload + packet_end return b''.join([byte.to_bytes(1, 'little') for byte in packet]) # Closes port def close_port(self): self.port.close() # Send packets def send_packet(self, cmd, packet_number): if cmd == 'InitAck': self.port.write(self.init_ACK(packet_number)) elif cmd == 'Watchdog': self.port.write(self.watchdog()) elif cmd == 'GetStimulationMode': self.port.write(self.getMode()) elif cmd == 'InitChannelListMode': self.port.write(self.init_stimulation()) #quoi faire avec channel_execution elif cmd == 'StartChannelListMode': self.port.write(self.start_stimulation()) elif cmd == 'StopChannelListMode': self.port.write(self.stop_stimulation()) # Update packet count self.packet_count = (self.packet_count + 1) % 256 # Receives packet # Read the received packet def read_packets(self): # Read port stream packet = self.port.readline() # If it is a start byte, collect packet if packet[0] == self.START_BYTE: # Collect header bytes ''' for i in range(4): packet += self.port.read() # Collect data bytes datalength = packet[-1] for i in range(datalength): packet += self.port.read() # Collect stop byte packet += self.port.read() # Call the right ACK function ''' return packet else: # Return empty string to avoid hanging return b'' # Creates packet for every command part of dictionary TYPES def calling_ACK(self): #Call the Ack function packet = self.read_packets() if (len(packet)>1): if(int(packet[6]) == Stimulator.TYPES['Init'] and int(packet[7]) == self.VERSION): return Stimulator.send_packet(self, 'InitAck', int(packet[5])) elif(str(packet[6]) == Stimulator.TYPES['UnknownCommand']): return Stimulator.unknown_cmd() elif(str(packet[6]) == Stimulator.TYPES['GetStimulationModeAck']): return Stimulator.getmodeACK(packet) elif(str(packet[6]) == Stimulator.TYPES['InitChannelListModeAck']): return Stimulator.init_stimulation_ACK(packet) elif(str(packet[6]) == Stimulator.TYPES['StartChannelListMode']): return Stimulator.start_stimulation_ACK(packet) elif(str(packet[6]) == Stimulator.TYPES['StopChannelListModeAck']): return Stimulator.stop_stimulation_ACK(packet) elif(str(packet[6]) == Stimulator.TYPES['StartChannelListModeAck']): return Stimulator.error_stimulation_ACK(packet) # Establishes connexion acknowlege def init(self, packet_count): packet = self.packet_construction(packet_count,'Init', self.VERSION ) return packet # Establishes connexion acknowlege def init_ACK(self, packet_count): packet = self.packet_construction(packet_count, 'InitAck', 0) return packet # Sends message for unknown command def unknown_cmd(self, packet): return str(packet[6]) # Error signal (inactivity ends connexion) VERIFY IF IT HAVE TO BE SEND EVERY <1200MS OR SEND IF ONLY NOTHING SEND AFTER 120MS def watchdog(self): packet = self.packet_construction(self.packet_count,'Watchdog') return packet # Asking to know which mode has been chosen def getMode(self): packet = self.packet_construction(self.packet_count, 'GetStimulationMode') return packet # Sent by RehaStim2 in response to getMode def getModeACK(self, packet): if(str(packet[6] == '0')): if(str(packet[7]) == '0'): return 'Start Mode' elif(str(packet[7]) == '1'): return 'Stimulation initialized' elif(str(packet[7]) == '2'): return 'Stimulation started' elif(str(packet[6]) == '-1'): return 'Transfer error' elif(str(packet[6]) == '-8'): return 'Busy error' #add a timer # Initialises stimulation def init_stimulation(self): MSB, LSB = self.MSB_LSB_main_stim() packet = self.packet_construction(self.packet_count,'InitChannelListMode', 0, self.electrode_number, 0, 2, MSB, LSB, 0 ) # Channel est 1,2,4,8,16,32,64,128 pour chaque et l'addition donne l'activation de plusieurs channels return packet # Sent by RehaStim2 in response to init_stimulation def init_stimulation_ACK(self, packet): if(str(packet[6]) == '0'): return 'Stimulation initialized' elif(str(packet[6]) == '-1'): return 'Transfer error' elif(str(packet[6]) == '-2'): return 'Parameter error' #Change for please change parameters? elif(str(packet[6]) == '-3'): return 'Wrong mode error' elif(str(packet[6]) == '-8'): return 'Busy error' # Add a timer? # Starts stimulation and modifies it def start_stimulation(self): #VA PROBABLEMENT CHANGER PULSE_WIDTH ET AMPLITUDE SELON COMMENT RÉCUPÈRE DONNÉES #if len(self.pulse_width) == 1: MSB_matrix =[] LSB_matrix =[] for i in range (len(self.amplitude)): MSB, LSB = self.MSB_LSB_pulse_stim(self.pulse_width[i]) MSB_matrix.append(MSB) LSB_matrix.append(LSB) if (len(self.amplitude)) ==1: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0])) if len(self.amplitude) == 2: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1])) elif len(self.amplitude) == 3: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2])) elif len(self.amplitude) == 4: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2]), 0, int(MSB_matrix[3]), int(LSB_matrix[3]), int(self.amplitude[3])) elif len(self.amplitude) == 5: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2]), 0, int(MSB_matrix[3]), int(LSB_matrix[3]), int(self.amplitude[3]), 0, int(MSB_matrix[4]), int(LSB_matrix[4]), int(self.amplitude[4])) elif len(self.amplitude) == 6: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2]), 0, int(MSB_matrix[3]), int(LSB_matrix[3]), int(self.amplitude[3]), 0, int(MSB_matrix[4]), int(LSB_matrix[4]), int(self.amplitude[4]), 0, int(MSB_matrix[5]), int(LSB_matrix[5]), int(self.amplitude[5])) elif len(self.amplitude) == 7: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2]), 0, int(MSB_matrix[3]), int(LSB_matrix[3]), int(self.amplitude[3]), 0, int(MSB_matrix[4]), int(LSB_matrix[4]), int(self.amplitude[4]), 0, int(MSB_matrix[5]), int(LSB_matrix[5]), int(self.amplitude[5]), 0, int(MSB_matrix[6]), int(LSB_matrix[6]), int(self.amplitude[6])) elif len(self.amplitude) == 8: packet = self.packet_construction(self.packet_count,'StartChannelListMode', 0, int(MSB_matrix[0]), int(LSB_matrix[0]), int(self.amplitude[0]), 0, int(MSB_matrix[1]), int(LSB_matrix[1]), int(self.amplitude[1]), 0, int(MSB_matrix[2]), int(LSB_matrix[2]), int(self.amplitude[2]), 0, int(MSB_matrix[3]), int(LSB_matrix[3]), int(self.amplitude[3]), 0, int(MSB_matrix[4]), int(LSB_matrix[4]), int(self.amplitude[4]), 0, int(MSB_matrix[5]), int(LSB_matrix[5]), int(self.amplitude[5]), 0, int(MSB_matrix[6]), int(LSB_matrix[6]), int(self.amplitude[6]), 0, int(MSB_matrix[7]), int(LSB_matrix[7]), int(self.amplitude[7])) return packet # Sent by RehaStim2 in response to start_stimulation def start_stimulation_ACK(self, packet): if(str(packet[6]) == '0'): return ' Stimulation started' if(str(packet[6]) == '-1'): return ' Transfer error' if(str(packet[6]) == '-2'): return ' Parameter error' if(str(packet[6]) == '-3'): return ' Wrong mode error' if(str(packet[6]) == '-8'): return ' Busy error' # Stops stimulation def stop_stimulation(self): packet = self.packet_construction(self.packet_count,'StopChannelListMode') return packet # Sent by RehaStim2 in response to stop_stimulation def stop_stimulation_ACK(self, packet): if(str(packet[6]) == '0'): return ' Stimulation stopped' elif(str(packet[6]) == '-1'): return ' Transfer error' def stimulation_error(self, packet): if(str(packet[6]) == '-1'): return ' Emergency switch activated/not connected' #mettre fonction qui affiche message sur interface elif(str(packet[6]) == '-2'): return ' Electrode error' elif(str(packet[6]) == '-3'): return 'Stimulation module error' # Function to command the stimulator with pre-defined commands def throw_command(self, command): print("(Stimulator) TODO : call the '" + command + "' command") #if command type == hexadécimal of certain command, throw associated function. #fonction qui lit le paquet reçu par rehastim et qui l'associe à une commande. #command = {'Init':0x01} def MSB_LSB_main_stim (self): if self.ts1[0] <= 255: LSB = self.ts1[0] MSB = 0; elif 256 <= self.ts1[0] <= 511: LSB = self.ts1[0]-256 MSB = 1; elif 512 <= self.ts1[0] <= 767: LSB = self.ts1[0]-512 MSB = 2; elif 768 <= self.ts1[0] <= 1023: LSB = self.ts1[0]-768 MSB = 3; elif 1024 <= self.ts1[0] <= 1279: LSB = self.ts1[0]-1024 MSB = 4; elif 1280 <= self.ts1[0] <= 1535: LSB = self.ts1[0]-1280 MSB = 5; elif 1536 <= self.ts1[0] <= 1791: LSB = self.ts1[0]-1536 MSB = 6; elif 1792 <= self.ts1[0] <= 2047: LSB = self.ts1[0]-1792 MSB = 7; elif self.ts1[0] == 2048: LSB = 0 MSB = 8; return MSB, int(LSB) def MSB_LSB_pulse_stim (self, pulse_width): if pulse_width <= 255: LSB = pulse_width MSB = 0; elif 256 <= pulse_width <= 500: LSB = pulse_width-256 MSB = 1; return MSB,LSB
python
class Order(object): def __init__(self, name, address, comments): self.name = name self.address = address self.comments = comments
python
"""Helper functions for all Solar Forecast Arbiter /sites/* endpoints. """ import time from flask import current_app as app from requests.exceptions import ChunkedEncodingError, ConnectionError from sentry_sdk import capture_exception from sfa_dash import oauth_request_session from sfa_dash.api_interface.util import handle_response from sfa_dash.errors import DataRequestException def get_request(path, **kwargs): """Make a get request to a path at SFA api. Parameters ---------- path: str The api endpoint to query including leading slash. Returns ------- requests.Response The api response. """ # may need to handle errors if oauth_request_session does not exist somehow # definitely need to handle errors here retries = kwargs.pop('failure_retries', 2) errors = None try: req = oauth_request_session.get( f'{app.config["SFA_API_URL"]}{path}', **kwargs) except ChunkedEncodingError as e: errors = e except ConnectionError as e: errors = e if errors is not None: if retries > 0: kwargs['failure_retries'] = retries - 1 time.sleep((3 - retries) * 0.1) return get_request(path, **kwargs) else: # API timed out or dropped the connection, send the error to # sentry for tracking and return a message to the user. capture_exception(errors) raise DataRequestException(503, { 'Error': 'API connection failed. Please try again.' }) else: return handle_response(req) def post_request(path, payload, json=True): """Post payload to a path at the SFA api. Parameters ---------- path: str The api endpoint to post to including leading slash. payload: str or dict Payload to send to the api either a string or JSON dict. json: boolean A flag for setting the content type of the request, if True, posts json to the api, otherwise sends the payload as text/csv. Returns ------- requests.Response The api response. """ if json: kwargs = {'json': payload} else: kwargs = {'headers': {'Content-type': 'text/csv'}, 'data': payload} return handle_response(oauth_request_session.post( f'{app.config["SFA_API_URL"]}{path}', **kwargs)) def delete_request(path, **kwargs): """Make a delete request. Parameters ---------- path: str The api endpoint to post to including leading slash. Returns ------- requests.Response The api response. """ return handle_response(oauth_request_session.delete( f'{app.config["SFA_API_URL"]}{path}', **kwargs))
python
from django.urls import path from app.pages.views import ( display_customers_data_page, display_customer_by_id_page, display_home_page, ) urlpatterns = [ path('', display_home_page), path('customers/', display_customers_data_page, name="customers"), path('customers_by_id/', display_customer_by_id_page, name="customers_by_id"), ]
python
import pytest from orders.models import Order pytestmark = [pytest.mark.django_db] def test_tinkoff_bank_is_called_by_default(call_purchase, tinkoff_bank, tinkoff_credit): call_purchase() tinkoff_bank.assert_called_once() tinkoff_credit.assert_not_called() def test_tinkoff_bank(call_purchase, tinkoff_bank, tinkoff_credit): call_purchase(desired_bank='tinkoff_bank') tinkoff_bank.assert_called_once() tinkoff_credit.assert_not_called() def test_tinkoff_credit(call_purchase, tinkoff_bank, tinkoff_credit): call_purchase(desired_bank='tinkoff_credit') tinkoff_bank.assert_not_called() tinkoff_credit.assert_called_once() def test_desired_bank_is_saved(call_purchase): call_purchase(desired_bank='tinkoff_credit') order = Order.objects.last() assert order.desired_bank == 'tinkoff_credit' def test_by_default_desired_bank_is_empty_string(call_purchase): call_purchase() order = Order.objects.last() assert order.desired_bank == '' def test_desired_bank_is_stored_during_gift(api, default_gift_data): api.post( '/api/v2/courses/ruloning-oboev/gift/', { **default_gift_data, 'desired_bank': 'tinkoff_credit', }, format='multipart', expected_status_code=302) order = Order.objects.last() assert order.desired_bank == 'tinkoff_credit'
python
""" This file contains the core methods for the Batch-command- and Batch-code-processors respectively. In short, these are two different ways to build a game world using a normal text-editor without having to do so 'on the fly' in-game. They also serve as an automatic backup so you can quickly recreate a world also after a server reset. The functions in this module is meant to form the backbone of a system called and accessed through game commands. The Batch-command processor is the simplest. It simply runs a list of in-game commands in sequence by reading them from a text file. The advantage of this is that the builder only need to remember the normal in-game commands. They are also executing with full permission checks etc, making it relatively safe for builders to use. The drawback is that in-game there is really a builder-character walking around building things, and it can be important to create rooms and objects in the right order, so the character can move between them. Also objects that affects players (such as mobs, dark rooms etc) will affect the building character too, requiring extra care to turn off/on. The Batch-code processor is a more advanced system that accepts full Python code, executing in chunks. The advantage of this is much more power; practically anything imaginable can be coded and handled using the batch-code processor. There is no in-game character that moves and that can be affected by what is being built - the database is populated on the fly. The drawback is safety and entry threshold - the code is executed as would any server code, without mud-specific permission checks and you have full access to modifying objects etc. You also need to know Python and Evennia's API. Hence it's recommended that the batch-code processor is limited only to superusers or highly trusted staff. ======================================================================= Batch-command processor file syntax The batch-command processor accepts 'batchcommand files' e.g 'batch.ev', containing a sequence of valid evennia commands in a simple format. The engine runs each command in sequence, as if they had been run at the game prompt. Each evennia command must be delimited by a line comment to mark its end. #INSERT path.batchcmdfile - this as the first entry on a line will import and run a batch.ev file in this position, as if it was written in this file. This way entire game worlds can be created and planned offline; it is especially useful in order to create long room descriptions where a real offline text editor is often much better than any online text editor or prompt. Example of batch.ev file: ---------------------------- # batch file # all lines starting with # are comments; they also indicate # that a command definition is over. @create box # this comment ends the @create command. @set box/desc = A large box. Inside are some scattered piles of clothing. It seems the bottom of the box is a bit loose. # Again, this comment indicates the @set command is over. Note how # the description could be freely added. Excess whitespace on a line # is ignored. An empty line in the command definition is parsed as a \n # (so two empty lines becomes a new paragraph). @teleport #221 # (Assuming #221 is a warehouse or something.) # (remember, this comment ends the @teleport command! Don'f forget it) # Example of importing another file at this point. #IMPORT examples.batch @drop box # Done, the box is in the warehouse! (this last comment is not necessary to # close the @drop command since it's the end of the file) ------------------------- An example batch file is game/gamesrc/commands/examples/batch_example.ev. ========================================================================== Batch-code processor file syntax The Batch-code processor accepts full python modules (e.g. "batch.py") that looks identical to normal Python files with a few exceptions that allows them to the executed in blocks. This way of working assures a sequential execution of the file and allows for features like stepping from block to block (without executing those coming before), as well as automatic deletion of created objects etc. You can however also run a batch-code python file directly using Python (and can also be de). Code blocks are separated by python comments starting with special code words. #HEADER - this denotes commands global to the entire file, such as import statements and global variables. They will automatically be pasted at the top of all code blocks. Observe that changes to these variables made in one block is not preserved between blocks! #CODE #CODE (info) #CODE (info) objname1, objname1, ... - This designates a code block that will be executed like a stand-alone piece of code together with any #HEADER defined. (info) text is used by the interactive mode to display info about the node to run. <objname>s mark the (variable-)names of objects created in the code, and which may be auto-deleted by the processor if desired (such as when debugging the script). E.g., if the code contains the command myobj = create.create_object(...), you could put 'myobj' in the #CODE header regardless of what the created object is actually called in-game. #INSERT path.filename - This imports another batch_code.py file and runs it in the given position. paths are given as python path. The inserted file will retain its own HEADERs which will not be mixed with the HEADERs of the file importing this file. The following variables are automatically made available for the script: caller - the object executing the script Example batch.py file ----------------------------------- #HEADER import traceback from django.config import settings from src.utils import create from game.gamesrc.typeclasses import basetypes GOLD = 10 #CODE obj, obj2 obj = create.create_object(basetypes.Object) obj2 = create.create_object(basetypes.Object) obj.location = caller.location obj.db.gold = GOLD caller.msg("The object was created!") #INSERT another_batch_file #CODE script = create.create_script() """ import re import codecs import traceback, sys from traceback import format_exc from django.conf import settings from django.core.management import setup_environ from src.utils import logger from src.utils import utils from game import settings as settings_module ENCODINGS = settings.ENCODINGS CODE_INFO_HEADER = re.compile(r"\(.*?\)") #------------------------------------------------------------ # Helper function #------------------------------------------------------------ def read_batchfile(pythonpath, file_ending='.py'): """ This reads the contents of a batch-file. Filename is considered to be the name of the batch file relative the directory specified in settings.py. file_ending specify which batchfile ending should be assumed (.ev or .py). """ # open the file if pythonpath and not (pythonpath.startswith('src.') or pythonpath.startswith('game.') or pythonpath.startswith('contrib.')): abspaths = [] for basepath in settings.BASE_BATCHPROCESS_PATHS: abspaths.append(utils.pypath_to_realpath("%s.%s" % (basepath, pythonpath), file_ending)) else: abspaths = [utils.pypath_to_realpath(pythonpath, file_ending)] fobj, lines, err = None, [], None for file_encoding in ENCODINGS: # try different encodings, in order load_errors = [] for abspath in abspaths: # try different paths, until we get a match try: # we read the file directly into unicode. fobj = codecs.open(abspath, 'r', encoding=file_encoding) except IOError: load_errors.append("Could not open batchfile '%s'." % abspath) continue break if not fobj: continue load_errors = [] err =None # We have successfully found and opened the file. Now actually # try to decode it using the given protocol. try: lines = fobj.readlines() except UnicodeDecodeError: # give the line of failure fobj.seek(0) try: lnum = 0 for lnum, line in enumerate(fobj): pass except UnicodeDecodeError, err: # lnum starts from 0, so we add +1 line, # besides the faulty line is never read # so we add another 1 (thus +2) to get # the actual line number seen in an editor. err.linenum = lnum + 2 fobj.close() # possibly try another encoding continue # if we get here, the encoding worked. Stop iteration. break if load_errors: logger.log_errmsg("\n".join(load_errors)) if err: return err else: return lines #------------------------------------------------------------ # # Batch-command processor # #------------------------------------------------------------ class BatchCommandProcessor(object): """ This class implements a batch-command processor. """ def parse_file(self, pythonpath): """ This parses the lines of a batchfile according to the following rules: 1) # at the beginning of a line marks the end of the command before it. It is also a comment and any number of # can exist on subsequent lines (but not inside comments). 2) #INSERT at the beginning of a line imports another batch-cmd file file and pastes it into the batch file as if it was written there. 3) Commands are placed alone at the beginning of a line and their arguments are considered to be everything following (on any number of lines) until the next comment line beginning with #. 4) Newlines are ignored in command definitions 5) A completely empty line in a command line definition is condered a newline (so two empty lines is a paragraph). 6) Excess spaces and indents inside arguments are stripped. """ #helper function def identify_line(line): """ Identifies the line type (comment, commanddef or empty) """ try: if line.strip().startswith("#INSERT"): return "insert" elif line.strip()[0] == '#': return "comment" else: return "commanddef" except IndexError: return "empty" #read the indata, if possible. lines = read_batchfile(pythonpath, file_ending='.ev') #line = utils.to_unicode(line) if not lines: return None commands = [] curr_cmd = "" #purge all superfluous whitespace and newlines from lines reg1 = re.compile(r"\s+") lines = [reg1.sub(" ", l) for l in lines] #parse all command definitions into a list. for line in lines: typ = identify_line(line) if typ == "commanddef": curr_cmd += line elif typ == "empty" and curr_cmd: curr_cmd += "\r\n" elif typ == "insert": # note that we are not safeguarding for # cyclic imports here! if curr_cmd: commands.append(curr_cmd.strip()) curr_cmd = "" filename = line.lstrip("#INSERT").strip() insert_commands = self.parse_file(filename) if insert_commands == None: insert_commands = ["{rINSERT ERROR: %s{n" % filename] commands.extend(insert_commands) else: #comment if curr_cmd: commands.append(curr_cmd.strip()) curr_cmd = "" if curr_cmd: commands.append(curr_cmd.strip()) #second round to clean up now merged line edges etc. reg2 = re.compile(r"[ \t\f\v]+") commands = [reg2.sub(" ", c) for c in commands] #remove eventual newline at the end of commands commands = [c.strip('\r\n') for c in commands] return commands #------------------------------------------------------------ # # Batch-code processor # #------------------------------------------------------------ def tb_filename(tb): "Helper to get filename from traceback" return tb.tb_frame.f_code.co_filename def tb_iter(tb): while tb is not None: yield tb tb = tb.tb_next class BatchCodeProcessor(object): """ This implements a batch-code processor """ def parse_file(self, pythonpath): """ This parses the lines of a batchfile according to the following rules: 1) Lines starting with #HEADER starts a header block (ends other blocks) 2) Lines starting with #CODE begins a code block (ends other blocks) 3) #CODE headers may be of the following form: #CODE (info) objname, objname2, ... 4) Lines starting with #INSERT are on form #INSERT filename. 3) All lines outside blocks are stripped. 4) All excess whitespace beginning/ending a block is stripped. """ # helper function def parse_line(line): """ Identifies the line type: block command, comment, empty or normal code. """ parseline = line.strip() if parseline.startswith("#HEADER"): return ("header", "", "") if parseline.startswith("#INSERT"): filename = line.lstrip("#INSERT").strip() if filename: return ('insert', "", filename) else: return ('comment', "", "{r#INSERT <None>{n") elif parseline.startswith("#CODE"): # parse code command line = line.lstrip("#CODE").strip() info = CODE_INFO_HEADER.findall(line) or "" if info: info = info[0] line = line.replace(info, "") objs = [o.strip() for o in line.split(",") if o.strip()] return ("codeheader", info, objs) elif parseline.startswith('#'): return ('comment', "", "%s" % line) else: #normal line - return it with a line break. return ('line', "", "%s" % line) # read indata lines = read_batchfile(pythonpath, file_ending='.py') if not lines: return None # parse file into blocks header = "" codes = [] in_header = False in_code = False for line in lines: # parse line mode, info, line = parse_line(line) # try: # print "::", in_header, in_code, mode, line.strip() # except: # print "::", in_header, in_code, mode, line if mode == 'insert': # recursive load of inserted code files - note that we # are not checking for cyclic imports! in_header = False in_code = False inserted_codes = self.parse_file(line) or [{'objs':"", 'info':line, 'code':""}] for codedict in inserted_codes: codedict["inserted"] = True codes.extend(inserted_codes) elif mode == 'header': in_header = True in_code = False elif mode == 'codeheader': in_header = False in_code = True # the line is a list of object variable names # (or an empty list) at this point. codedict = {'objs':line, 'info':info, 'code':""} codes.append(codedict) elif mode == 'comment' and in_header: continue else: # another type of line (empty, comment or code) if line and in_header: header += line elif line and in_code: codes[-1]['code'] += line else: # not in a block (e.g. first in file). Ignore. continue # last, we merge the headers with all codes. for codedict in codes: #print "codedict:", codedict if codedict and "inserted" in codedict: # we don't need to merge code+header in this case # since that was already added in the recursion. We # just check for errors. if not codedict['code']: codedict['code'] = "{r#INSERT ERROR: %s{n" % codedict['info'] else: objs = ", ".join(codedict["objs"]) if objs: objs = "[%s]" % objs codedict["code"] = "#CODE %s %s \n%s\n\n%s" % (codedict['info'], objs, header.strip(), codedict["code"].strip()) return codes def code_exec(self, codedict, extra_environ=None, debug=False): """ Execute a single code block, including imports and appending global vars extra_environ - dict with environment variables """ # define the execution environment environ = "setup_environ(settings_module)" environdict = {"setup_environ":setup_environ, "settings_module":settings_module} if extra_environ: for key, value in extra_environ.items(): environdict[key] = value # merge all into one block code = "%s # auto-added by Evennia\n%s" % (environ, codedict['code']) if debug: # try to delete marked objects for obj in codedict['objs']: code += "\ntry: %s.delete()\nexcept: pass" % obj # execute the block try: exec(code, environdict) except Exception: etype, value, tb = sys.exc_info() fname = tb_filename(tb) for tb in tb_iter(tb): if fname != tb_filename(tb): break lineno = tb.tb_lineno - 1 err = "" for iline, line in enumerate(code.split("\n")): if iline == lineno: err += "\n{w%02i{n: %s" % (iline + 1, line) elif lineno - 5 < iline < lineno + 5: err += "\n%02i: %s" % (iline + 1, line) err += "\n".join(traceback.format_exception(etype, value, tb)) #errlist = format_exc().split('\n') #if len(errlist) > 4: # errlist = errlist[4:] #err = "\n".join(" %s" % line for line in errlist if line) if debug: # try to delete objects again. try: for obj in codedict['objs']: eval("%s.delete()" % obj, environdict) except Exception: pass return err return None BATCHCMD = BatchCommandProcessor() BATCHCODE = BatchCodeProcessor()
python
from typing import Optional, Union from .set_config import _get_config class _Engine(object): """Indicates the database engine that is currently in use.""" ENGINE = 0 MYSQL = 1 SQLITE = 3 _created = False # Indicates whether the connection to the database has been created. @classmethod def set_engine(cls, this_engine: str): cls.ENGINE = getattr(cls, this_engine.upper()) @classmethod def is_mysql(cls): return cls.ENGINE == cls.MYSQL @classmethod def is_sqlite(cls): return cls.ENGINE == cls.SQLITE try: _engine = _get_config().pop('engine').lower() except KeyError: _engine = 'sqlite' except ModuleNotFoundError: _engine = '' if _engine == 'mysql': _Engine.set_engine('mysql') from .engine.mysql import __create_connection, _close_db_connection, _select, _execute, _Connection, _Transaction elif _engine == 'sqlite': _Engine.set_engine('sqlite') from .engine.sqlite import __create_connection, _close_db_connection, _select, _execute, _Connection, _Transaction def _set_engine(new_engine: str): """Set a engine, import related modules, use in setconf.set_config.""" global __create_connection, _close_db_connection, _select, _execute, _Connection, _Transaction engine = new_engine.lower() if engine == 'mysql': _Engine.set_engine('mysql') from .engine.mysql import __create_connection, _close_db_connection, _select, _execute, _Connection, \ _Transaction elif engine == 'sqlite': _Engine.set_engine('sqlite') from .engine.sqlite import __create_connection, _close_db_connection, _select, _execute, _Connection, \ _Transaction def _create_connection(echo: bool = False, debug: bool = False, **kwargs): """ Create a connection to databases. Args: See setconf's __doc__ . """ return __create_connection(echo=echo, debug=debug, **kwargs) async def close_db_connection(): """Close connection with database.You may sometime need it.""" return await _close_db_connection() def Connection(): """ A async context manager to run a custom sql statement. Creates new connection.Returns a Connection instance. You can also use this connection in ORM by specifying the conn parameter. If you have not set autocommit=True, you should commit manual by use ``conn.commit()``. """ return _Connection() def Transaction(): """ Get a connection to do atomic transaction. This is a subclass of Connection and they have the same usage, and on exit, this connection will automatically commit or roll back on error. You can also use this connection in ORM by specifying the conn parameter. Example:: async whit connection.Transaction() as conn: await Table(tl1='abc',tl2=123).save(conn=conn) """ return _Transaction() def select(sql: str, args: Optional[Union[list, tuple]] = (), conn: Optional[Connection] = None) -> list: """ Execute a select query, and return a list of result.You can use this method when you encounter a query that ORM cannot complete Args: sql(str): a sql statement, use ? as placeholder. args(list or tuple): argument in placeholder. conn: use this parameter to specify a custom connection. Return: (list) a list of result. """ return _select(sql, args, conn) def execute(sql: str, args: Optional[Union[list, tuple]] = (), conn: Optional[Connection] = None) -> int: """ Execute a insert,update or delete query, and return the number of affected rows.You can use this method when you encounter a query that ORM cannot complete. Args: sql(str): a sql statement, use ? as placeholder. args(list or tuple): argument in placeholder. conn: use this parameter to specify a custom connection. Return: (int) affected rows. """ return _execute(sql, args, conn)
python
import argparse import confluent.client as cli import sys import time c = cli.Command() nodes = [] ap = argparse.ArgumentParser(description='Snake identify light through nodes') ap.add_argument('noderange', help='Noderange to iterate through') ap.add_argument('-d', '--duration', type=float, help='How long to have each system illuminated') args = ap.parse_args() def runit(itera): for rsp in itera: if 'error' in rsp: sys.stderr.write('{0}\n'.format(repr(rsp))) for ret in c.read('/noderange/{0}/nodes/'.format(args.noderange)): node = ret.get('item', {}).get('href', None) if node: node = node.replace('/', '') nodes.append(node) else: print(repr(ret)) if not nodes: sys.exit(1) lastnode = None interval = args.duration if interval: interval = interval / 2 else: interval = 0.25 while True: for node in nodes: print('Lighting {0}'.format(node)) runit(c.update('/nodes/{0}/identify'.format(node), {'identify': 'on'})) time.sleep(interval) if lastnode: runit(c.update('/nodes/{0}/identify'.format(lastnode), {'identify': 'off'})) lastnode = node time.sleep(interval)
python
# Generated by Django 3.1.7 on 2021-03-12 13:53 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('core', '0010_genre_desc'), ] operations = [ migrations.AlterField( model_name='genre', name='desc', field=models.TextField(blank=True, max_length=255, null=True), ), ]
python
import os import sys from pathlib import Path from typing import List import nox nox.options.sessions = ["lint", "test-dist"] PYTHON_ALL_VERSIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"] RUNNING_CI = "TRAVIS" in os.environ or "GITHUB_ACTIONS" in os.environ @nox.session(python=["3.6"], reuse_venv=True) def lint(session: nox.Session) -> None: """ Run linters on the codebase. """ session.install("pre-commit") session.run("pre-commit", "run", "--all-files") @nox.session() def coverage(session: nox.Session) -> None: """ Run coverage using unit tests. """ session.install(".[coverage]") session.run( "python", "-m", "pytest", "tests/unit", "--cov=auditwheel", "--cov-report=term-missing", ) def _docker_images(session: nox.Session) -> List[str]: tmp_dir = Path(session.create_tmp()) script = tmp_dir / "list_images.py" images_file = tmp_dir / "images.lst" script.write_text( fr""" import sys from pathlib import Path sys.path.append("./tests/integration") from test_manylinux import MANYLINUX_IMAGES images = "\n".join(MANYLINUX_IMAGES.values()) Path(r"{images_file}").write_text(images) """ ) session.run("python", str(script), silent=True) return images_file.read_text().splitlines() @nox.session(python=PYTHON_ALL_VERSIONS) def tests(session: nox.Session) -> None: """ Run tests. """ posargs = session.posargs extras = "coverage" if RUNNING_CI else "test" session.install("-e", f".[{extras}]") if RUNNING_CI: session.install("codecov") posargs.extend(["--cov", "auditwheel", "--cov-branch"]) # pull manylinux images that will be used. # this helps passing tests which would otherwise timeout. for image in _docker_images(session): session.run("docker", "pull", image, external=True) session.run("pytest", "-s", *posargs) if RUNNING_CI: session.run("auditwheel", "lddtree", sys.executable) try: session.run("codecov") except nox.command.CommandFailed: pass # Ignore failures from codecov tool def _build(session: nox.Session, dist: Path) -> None: session.install("build") tmp_dir = Path(session.create_tmp()) / "build-output" session.run("python", "-m", "build", "--outdir", str(tmp_dir)) (wheel_path,) = tmp_dir.glob("*.whl") (sdist_path,) = tmp_dir.glob("*.tar.gz") dist.mkdir(exist_ok=True) wheel_path.rename(dist / wheel_path.name) sdist_path.rename(dist / sdist_path.name) @nox.session(name="test-dist") def test_dist(session: nox.Session) -> None: """ Builds SDist & Wheels then run unit tests on those. """ tmp_dir = Path(session.create_tmp()) dist = tmp_dir / "dist" _build(session, dist) python_versions = session.posargs or PYTHON_ALL_VERSIONS for version in python_versions: session.notify(f"_test_sdist-{version}", [str(dist)]) session.notify(f"_test_wheel-{version}", [str(dist)]) def _test_dist(session: nox.Session, path: str, pattern: str) -> None: (dist_path,) = Path(path).glob(pattern) session.install(f"{str(dist_path)}[test]") session.run("pytest", "tests/unit") @nox.session(python=PYTHON_ALL_VERSIONS) def _test_sdist(session: nox.Session) -> None: """ Do not run explicitly. """ _test_dist(session, session.posargs[0], "*.tar.gz") @nox.session(python=PYTHON_ALL_VERSIONS) def _test_wheel(session: nox.Session) -> None: """ Do not run explicitly. """ _test_dist(session, session.posargs[0], "*.whl") @nox.session def build(session: nox.Session) -> None: """ Make an SDist and a wheel. """ _build(session, Path("dist")) @nox.session(python=PYTHON_ALL_VERSIONS, reuse_venv=True) def develop(session: nox.Session) -> None: session.run("python", "-m", "pip", "install", "--upgrade", "pip", "setuptools") session.install("-e", ".[develop]")
python
import pandas as pd #given a word, search all cases and find caseNames which contain the word #return as a dataframe def search_scdb_cases_by_name(word,all_scdb_case_data): return all_scdb_case_data[all_scdb_case_data['caseName'].str.contains(word,na=False)] #try to convert case names from SCDB 'caseName' field to CL opinion scraped #casenames. cl_opin case_names are from urls, so lowercase smashed w/ hyphens #vs,versus -> v def format_case_name(case_name): return str(case_name).lower().lstrip().rstrip().replace(',','').replace("'",'').replace('versus','v').replace('vs.','v.').replace('.','').replace(' ','-') #find case in SCDB database given CourtListener opinion_id #CL ids are in form "U.S. citation|lower-case-v-name" def find_scdb_case(cl_opin_id,all_scdb_case_data): us_citation = cl_opin_id.split('|')[0] #get df of all cases with given U.S. citation "xxx U.S. xxx" scdb_cases = all_scdb_case_data[all_scdb_case_data['usCite']==us_citation] if not scdb_cases.empty: return scdb_cases #if that doesn't work, try lower-case-v-name case_name_from_id = cl_opin_id.split('|')[1] for caseName in all_scdb_case_data['caseName']: if format_case_name(case_name_from_id) == format_case_name(caseName): return all_scdb_case_data[all_scdb_case_data['caseName']==caseName] #if no match, return the empty DataFrame return pd.DataFrame()
python
# MIT License # # Copyright (c) 2020 University of Oxford # # 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. """ Command line interface for tsdate. """ import argparse import logging import sys import tskit import tsdate logger = logging.getLogger(__name__) log_format = '%(asctime)s %(levelname)s %(message)s' def exit(message): """ Exit with the specified error message, setting error status. """ sys.exit("{}: {}".format(sys.argv[0], message)) def setup_logging(args): log_level = "WARN" if args.verbosity > 0: log_level = "INFO" if args.verbosity > 1: log_level = "DEBUG" logging.basicConfig(level=log_level, format=log_format) def tsdate_cli_parser(): parser = argparse.ArgumentParser( description="Set up base data, generate inferred datasets,\ and process datasets.") parser.add_argument( "-V", "--version", action='version', version='%(prog)s {}'.format(tsdate.__version__)) parser.add_argument('ts', help="The path and name of the input tree sequence from which \ we estimate node ages.") parser.add_argument('output', help="The path and name of output file where the dated tree \ sequence will saved.") parser.add_argument('Ne', type=float, help="estimated effective (diploid) population size.") parser.add_argument('-m', '--mutation-rate', type=float, default=None, help="The estimated mutation rate per unit of genome per \ generation. If provided, the dating algorithm will use a \ mutation rate clock to help estimate node dates.") parser.add_argument('-r', '--recombination-rate', type=float, default=None, help="The estimated recombination rate per unit \ of genome per generation. If provided, the dating algorithm \ will use a recombination rate clock to help estimate node \ dates.") parser.add_argument('-e', '--epsilon', type=float, default=1e-6, help="Specify minimum distance separating time points. Also \ specifies the error factor in time difference calculations.") parser.add_argument('-t', '--num-threads', type=int, default=None, help="The number of threads to use. A simpler unthreaded \ algorithm is used unless this is >= 1 (default: None).") parser.add_argument('--probability-space', type=str, default='logarithmic', help="Should the internal algorithm save probabilities in \ 'logarithmic' (slower, less liable to to overflow) or 'linear' \ space (faster, may overflow).") parser.add_argument('--method', type=str, default='inside_outside', help="Specify which estimation method to use: can be \ 'inside_outside' (empirically better, theoretically \ problematic) or 'maximization' (worse empirically, especially \ with a gamma approximated prior, but theoretically robust). \ Default: 'inside_outside.'") parser.add_argument('-p', '--progress', action='store_true', help="Show progress bar.") parser.add_argument('-v', '--verbosity', type=int, default=0, help="How much verbosity to output.") return parser def run_date(args): try: ts = tskit.load(args.ts) except tskit.FileFormatError as ffe: exit("Error loading '{}: {}".format(args.ts, ffe)) dated_ts = tsdate.date( ts, args.Ne, mutation_rate=args.mutation_rate, recombination_rate=args.recombination_rate, probability_space=args.probability_space, method=args.method, eps=args.epsilon, num_threads=args.num_threads, progress=args.progress) dated_ts.dump(args.output) def tsdate_main(arg_list=None): parser = tsdate_cli_parser() args = parser.parse_args(arg_list) setup_logging(args) run_date(args)
python
""" [summary] [extended_summary] """ # region [Imports] # * Standard Library Imports ----------------------------------------------------------------------------> import os import traceback from datetime import datetime from typing import Tuple import re # * Third Party Imports ---------------------------------------------------------------------------------> from discord import Embed, ChannelType from fuzzywuzzy import fuzz from fuzzywuzzy import process as fuzzprocess from discord.ext import commands import discord # * Gid Imports -----------------------------------------------------------------------------------------> import gidlogger as glog # * Local Imports ---------------------------------------------------------------------------------------> from antipetros_discordbot.utility.misc import async_seconds_to_pretty_normal, async_split_camel_case_string from antipetros_discordbot.utility.exceptions import MissingAttachmentError, NotNecessaryRole, IsNotTextChannelError, NotNecessaryDmId, NotAllowedChannelError, NotNecessaryRole from antipetros_discordbot.utility.gidtools_functions import loadjson from antipetros_discordbot.abstracts.subsupport_abstract import SubSupportBase from antipetros_discordbot.init_userdata.user_data_setup import ParaStorageKeeper from antipetros_discordbot.utility.discord_markdown_helper.special_characters import ZERO_WIDTH from antipetros_discordbot.bot_support.sub_support.sub_support_helper.cooldown_dict import CoolDownDict # endregion[Imports] # region [TODO] # TODO: rebuild whole error handling system # TODO: make it so that creating the embed also sends it, with more optional args # endregion [TODO] # region [AppUserData] # endregion [AppUserData] # region [Logging] log = glog.aux_logger(__name__) # endregion[Logging] # region [Constants] APPDATA = ParaStorageKeeper.get_appdata() BASE_CONFIG = ParaStorageKeeper.get_config('base_config') THIS_FILE_DIR = os.path.abspath(os.path.dirname(__file__)) EMBED_SYMBOLS = loadjson(APPDATA["embed_symbols.json"]) # endregion[Constants] class ErrorHandler(SubSupportBase): char_to_replace = "'" config_name = 'error_handling' error_thumbnail = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/Dialog-error-round.svg/1200px-Dialog-error-round.svg.png" def __init__(self, bot, support): self.bot = bot self.support = support self.loop = self.bot.loop self.is_debug = self.bot.is_debug self.emphasis_regex = re.compile(r"'.*?'") self.error_handle_table = {commands.MaxConcurrencyReached: self._handle_max_concurrency, commands.CommandOnCooldown: self._handle_command_on_cooldown, commands.errors.BadArgument: self._handle_bad_argument, MissingAttachmentError: self._handle_missing_attachment, commands.CheckFailure: self._handle_check_failure, IsNotTextChannelError: self._handle_not_text_channel, NotNecessaryDmId: self._handle_not_necessary_dm_id, NotAllowedChannelError: self._handle_not_allowed_channel, NotNecessaryRole: self._handle_not_necessary_role} self.cooldown_data = CoolDownDict() glog.class_init_notification(log, self) @property def delete_invoking_messages(self): return BASE_CONFIG.retrieve(self.config_name, 'delete_invoking_messages', typus=bool, direct_fallback=False) @property def delete_reply_after(self): _out = BASE_CONFIG.retrieve(self.config_name, 'delete_reply_after', typus=int, direct_fallback=120) if _out == 0 or _out <= 0: return None return _out @property def emphasis_chars(self): format_lut = {'bold': '**', 'underlined': '__', 'italic': '*', 'strikethrough': '~'} format_keywords = BASE_CONFIG.retrieve(self.config_name, 'msg_keyword_format', typus=Tuple[str], direct_fallback=[], mod_func=lambda x: x.casefold()) return (''.join(map(lambda x: format_lut.get(x, ''), format_keywords)), ''.join(map(lambda x: format_lut.get(x, ''), reversed(format_keywords)))) async def transform_error_msg(self, error_msg): before_emphasis, after_emphasis = self.emphasis_chars _msg = error_msg for orig_word in self.emphasis_regex.findall(error_msg): cleaned_word = orig_word.strip("'").strip() mod_word = f"{before_emphasis}{cleaned_word.upper()}{after_emphasis}" _msg = _msg.replace(orig_word, mod_word) return _msg async def handle_errors(self, ctx, error): error_traceback = '\n'.join(traceback.format_exception(error, value=error, tb=None)) await self.error_handle_table.get(type(error), self._default_handle_error)(ctx, error, error_traceback) if ctx.channel.type is ChannelType.text: log.error("Error '%s' was caused by '%s' on the command '%s' with args '%s' and traceback --> %s", error.__class__.__name__, ctx.author.name, ctx.command.name, ctx.args, error_traceback) if self.delete_invoking_messages is True: await ctx.message.delete() async def _default_handle_error(self, ctx: commands.Context, error, error_traceback): log.error('Ignoring exception in command {}:'.format(ctx.command)) log.exception(error, exc_info=True, stack_info=False) if ctx.channel.type is ChannelType.text: await ctx.reply(f'The command had an unspecified __**ERROR**__\n please send {self.bot.creator.member_object.mention} a DM of what exactly you did when the error occured.', delete_after=120, allowed_mentions=discord.AllowedMentions.none()) await self.bot.message_creator(embed=await self.error_reply_embed(ctx, error, 'Error With No Special Handling Occured', msg=str(error), error_traceback=error_traceback)) async def _handle_not_necessary_role(self, ctx, error, error_traceback): embed_data = await self.bot.make_generic_embed(footer='default_footer', title='Missing Role', thumbnail=self.error_thumbnail, description=await self.transform_error_msg(error.msg), field=[self.bot.field_item(name='Your Roles:', value='\n'.join(role.name for role in ctx.author.roles))]) await ctx.reply(delete_after=self.delete_reply_after, **embed_data) async def _handle_not_allowed_channel(self, ctx, error, error_traceback): embed_data = await self.bot.make_generic_embed(footer='default_footer', title='Wrong Channel', thumbnail=self.error_thumbnail, description=await self.transform_error_msg(error.msg)) await ctx.reply(delete_after=self.delete_reply_after, **embed_data) async def _handle_not_necessary_dm_id(self, ctx, error, error_traceback): embed_data = await self.bot.make_generic_embed(footer='default_footer', title='Missing Permission', thumbnail=self.error_thumbnail, description=await self.transform_error_msg(error.msg)) await ctx.reply(**embed_data) async def _handle_not_text_channel(self, ctx, error, error_traceback): embed_data = await self.bot.make_generic_embed(footer='default_footer', title='Only allowed in Text Channels', thumbnail=self.error_thumbnail, description=await self.transform_error_msg(error.msg)) await ctx.reply(**embed_data) async def _handle_check_failure(self, ctx, error, error_traceback): if self.bot.is_blacklisted(ctx.author) is False: await ctx.channel.send(delete_after=self.delete_reply_after, embed=await self.error_reply_embed(ctx, error, 'Missing Permission', f'{ctx.author.mention}\n{ZERO_WIDTH}\n **You dont_have Permission to call this Command**\n{ZERO_WIDTH}')) async def _handle_missing_attachment(self, ctx, error, error_traceback): await ctx.channel.send(delete_after=self.delete_reply_after, embed=await self.error_reply_embed(ctx, error, 'Missing Attachments', f'{ctx.author.mention}\n{ZERO_WIDTH}\n **{str(error)}**\n{ZERO_WIDTH}')) async def _handle_bad_argument(self, ctx, error, error_traceback): await ctx.channel.send(delete_after=self.delete_reply_after, embed=await self.error_reply_embed(ctx, error, 'Wrong Argument', f'{ctx.author.mention}\n{ZERO_WIDTH}\n **You tried to invoke `{ctx.command.name}` with an wrong argument**\n{ZERO_WIDTH}\n```shell\n{ctx.command.name} {ctx.command.signature}\n```', error_traceback=None)) async def _handle_max_concurrency(self, ctx, error, error_traceback): await ctx.channel.send(embed=await self.error_reply_embed(ctx, error, 'STOP SPAMMING!', f'{ctx.author.mention}\n{ZERO_WIDTH}\n **Your mother was a hamster and your father smelled of elderberries!**', error_traceback=error_traceback), delete_after=self.delete_reply_after) await ctx.message.delete() async def _handle_command_on_cooldown(self, ctx, error, error_traceback): # TODO: get normal sentence from BucketType, with dynamical stuff (user_name, channel_name,...) msg = await self.transform_error_msg(f"Command '{ctx.command.name}' is on cooldown for '{error.cooldown.type.name.upper()}'. \n{ZERO_WIDTH}\nYou can try again in '{await async_seconds_to_pretty_normal(int(round(error.retry_after, 0)))}'\n{ZERO_WIDTH}") if self.cooldown_data.in_data(ctx, error) is True: await ctx.message.delete() await ctx.author.send(msg) return await self.cooldown_data.add(ctx, error) embed_data = await self.bot.make_generic_embed(title=f'Command is on Cooldown for the scope of {error.cooldown.type.name.upper()}', thumbnail="https://www.seekpng.com/png/full/896-8968896_cooldown-cooldown-car-air-conditioning-icon.png", description=msg) await ctx.reply(**embed_data, delete_after=error.retry_after) await ctx.message.delete() async def error_reply_embed(self, ctx, error, title, msg, error_traceback=None): embed = Embed(title=title, description=f"{ZERO_WIDTH}\n{msg}\n{ZERO_WIDTH}", color=self.support.color('red').int, timestamp=datetime.utcnow()) embed.set_thumbnail(url=EMBED_SYMBOLS.get('warning')) embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) if error_traceback is not None: embed.add_field(name='Traceback', value=str(error_traceback)[0:500]) if ctx.command is not None: embed.set_footer(text=f"Command: `{ctx.command.name}`\n{ZERO_WIDTH}\n By User: `{ctx.author.name}`\n{ZERO_WIDTH}\n Error: `{await async_split_camel_case_string(error.__class__.__name__)}`\n{ZERO_WIDTH}\n{ZERO_WIDTH}") else: embed.set_footer(text=f"text: {ctx.message.content}\n{ZERO_WIDTH}\n By User: `{ctx.author.name}`\n{ZERO_WIDTH}\n Error: `{await async_split_camel_case_string(error.__class__.__name__)}`\n{ZERO_WIDTH}\n{ZERO_WIDTH}") return embed async def error_message_embed(self, ctx, error, msg=ZERO_WIDTH): embed = Embed(title='ERROR', color=self.support.color('orange').int, timestamp=datetime.utcnow(), description=ZERO_WIDTH + '\n' + msg + '\n' + ZERO_WIDTH) embed.set_thumbnail(url=EMBED_SYMBOLS.get('warning')) try: embed.add_field(name=await async_split_camel_case_string(error.__class__.__name__), value=f"error occured with command: {ctx.command.name} and arguments: {str(ctx.args)}") except AttributeError: embed.add_field(name=await async_split_camel_case_string(error.__class__.__name__), value="command not found\n" + ZERO_WIDTH + '\n', inline=False) corrections = fuzzprocess.extract(ctx.message.content.split(' ')[1], [command.name for command in self.bot.commands], scorer=fuzz.token_set_ratio, limit=3) if corrections is not None: embed.add_field(name='did you mean:', value=ZERO_WIDTH + '\n' + f'\n{ZERO_WIDTH}\n'.join(correction[0] for correction in corrections), inline=False) embed.set_footer(text=f'to get a list of all commands use:\n@AntiPetros {self.bot.help_invocation}\n{ZERO_WIDTH}\n{ZERO_WIDTH}') return embed async def if_ready(self): log.debug("'%s' sub_support is READY", str(self)) async def update(self, typus): return log.debug("'%s' sub_support was UPDATED", str(self)) def retire(self): log.debug("'%s' sub_support was RETIRED", str(self)) def get_class(): return ErrorHandler # region[Main_Exec] if __name__ == '__main__': pass # endregion[Main_Exec]
python
# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """GKE master version available for new clusters. The GKE master version should be a version that is available for new clusters. If a version is not available it could mean that it is deprecated, or possibly retired due to issues with it. """ from gcpdiag import lint, models from gcpdiag.queries import gke def run_rule(context: models.Context, report: lint.LintReportRuleInterface): clusters = gke.get_clusters(context) if not clusters: report.add_skipped(None, 'no clusters found') for _, c in sorted(clusters.items()): if c.release_channel: report.add_skipped(c, 'release channel: ' + c.release_channel) continue valid_master_versions = gke.get_valid_master_versions( c.project_id, c.location) if c.master_version not in valid_master_versions: report.add_failed(c, 'valid versions: ' + ', '.join(valid_master_versions), c.master_version) else: report.add_ok(c, c.master_version)
python
# Copyright (c) 2012-2018 SoftBank Robotics. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the COPYING file. import os def test_simple(qidoc_action): world_proj = qidoc_action.add_test_project("world") build_dir = os.path.join(world_proj.path, "build-doc") assert not os.path.exists(build_dir) qidoc_action("build", "world") assert os.path.exists(build_dir) qidoc_action("clean", "world") assert os.path.exists(build_dir) qidoc_action("clean", "world", "--force") assert not os.path.exists(build_dir)
python
from io import BytesIO from .utils import Tools, Status, Network from .config import highQuality from .config import RESOURCES_BASE_PATH from PIL import Image, ImageDraw, ImageFilter, ImageFont class User(): def __init__(self, nickname, favorability, days, hitokoto): self._userNickname = nickname self._userFavorability = favorability self._userSignInDays = days self._userHitokoto = hitokoto self._userInfo = '签 到 成 功' self._userInfoIntegration = f'签到天数 {self._userSignInDays} 好感度 {self._userFavorability}' class SignIn(User): FONT_REEJI = 'REEJI-HonghuangLiGB-SemiBold.ttf' FONT_ZHANKU = 'zhanku.ttf' def __init__(self, userQQ, nickname, favorability, days, hitokoto, avatarUrl, basemapSize = 640, avatarSize = 256): super().__init__(nickname, favorability, days, hitokoto) self._userQQ = userQQ self._basemapSize = basemapSize self._avatarSize = avatarSize self._avatarUrl = avatarUrl self._img = Status.FAILURE self._roundImg = Status.FAILURE self._canvas = Status.FAILURE self._magicCircle = Status.FAILURE self._textBaseMap = Status.FAILURE self._magicCirclePlus = 30 self._avatarVerticalOffset = 50 self._textBaseMapSize = (540, 160) self._topPositionOfTextBaseMap = 425 self._textBaseMapLeftPosition = int((self._basemapSize - self._textBaseMapSize[0]) / 2) self._fontAttenuation = 2 self._minimumFontLimit = 10 self._infoCoordinatesY = Tools.dictToObj({ 'nickname': self._topPositionOfTextBaseMap + 26, 'info': self._topPositionOfTextBaseMap + 64, 'integration': self._topPositionOfTextBaseMap + 102, 'hitokoto': self._topPositionOfTextBaseMap + 137 }) self._infoFontSize = Tools.dictToObj({ 'nickname': 28, 'info': 28, 'integration': 25, 'hitokoto': 25 }) self._infoFontName = Tools.dictToObj({ 'nickname': self.FONT_REEJI, 'info': self.FONT_REEJI, 'integration': self.FONT_REEJI, 'hitokoto': self.FONT_ZHANKU }) @staticmethod async def getPictures(url): img = await Network.getBytes(url) return img async def createAvatar(self): size = self._basemapSize avatarImgUrl = self._avatarUrl res = await self.getPictures(avatarImgUrl) self._img = self.resize(Image.open(BytesIO(res)).convert('RGBA'), (size, size)) return self @staticmethod def resize(img, size): return img.copy().resize(size, Image.ANTIALIAS) @staticmethod def gaussianBlur(img, radius = 7): return img.copy().filter(ImageFilter.GaussianBlur(radius = radius)) @staticmethod def imageRadiusProcessing(img, centralA, radius = 30): """处理图片四个圆角。 :centralA: 中央区域的 A 通道值,当指定为 255 时全透,四角将使用 0 全不透 """ circle = Image.new('L', (radius * 2, radius * 2), 0) draw = ImageDraw.Draw(circle) draw.ellipse((0, 0, radius * 2, radius * 2), fill = centralA) w, h = img.size alpha = Image.new('L', img.size, centralA) upperLeft, lowerLeft = circle.crop((0, 0, radius, radius)), circle.crop((0, radius, radius, radius * 2)) upperRight, lowerRight = circle.crop((radius, 0, radius * 2, radius)), circle.crop((radius, radius, radius * 2, radius * 2)) alpha.paste(upperLeft, (0, 0)) alpha.paste(upperRight, (w - radius, 0)) alpha.paste(lowerRight, (w - radius, h - radius)) alpha.paste(lowerLeft, (0, h - radius)) img.putalpha(alpha) return img def createRoundImg(self): img = self._img size = self._avatarSize mask = Image.new('L', (size, size), 0) draw = ImageDraw.Draw(mask) draw.ellipse((0, 0, size, size), fill = 255) self._roundImg = self.resize(img, (size, size)) self._roundImg.putalpha(mask) return self def createCanvas(self): size = self._basemapSize self._canvas = Image.new('RGBA', (size, size), (0, 0, 0, 0)) self._canvas.paste(self.gaussianBlur(self._img)) return self def createAMagicCircle(self): size = self._magicCirclePlus + self._avatarSize magicCircle = Image.open(f'{RESOURCES_BASE_PATH}/magic-circle.png').convert('L') magicCircle = self.resize(magicCircle, (size, size)) self._magicCircle = Image.new('RGBA', (size, size), (0, 0, 0, 0)) self._magicCircle.putalpha(magicCircle) return self def createTextBasemap(self, transparency = 190): self._textBaseMap = Image.new('RGBA', self._textBaseMapSize, (0, 0, 0, transparency)) self._textBaseMap = self.imageRadiusProcessing(self._textBaseMap, transparency) return self def additionalMagicCircle(self): magicCircle = self._magicCircle x = int((self._basemapSize - self._avatarSize - self._magicCirclePlus) / 2) y = x - self._avatarVerticalOffset self._canvas.paste(magicCircle, (x, y), magicCircle) return self def additionalAvatar(self): avatar = self._roundImg x = int((self._basemapSize - self._avatarSize) / 2) y = x - self._avatarVerticalOffset self._canvas.paste(avatar, (x, y), avatar) return self def additionalTextBaseMap(self): textBaseMap = self._textBaseMap x = int((self._basemapSize - self._textBaseMapSize[0]) / 2) y = self._topPositionOfTextBaseMap self._canvas.paste(textBaseMap, (x, y), textBaseMap) return self def writePicture(self, img, text, position, fontName, fontSize, color = (255, 255, 255)): font = ImageFont.truetype(f'{RESOURCES_BASE_PATH}/font/{fontName}', fontSize) draw = ImageDraw.Draw(img) textSize = font.getsize(text) attenuation = self._fontAttenuation x = int(position[0] - textSize[0] / 2) limit = self._minimumFontLimit while x <= self._textBaseMapLeftPosition: fontSize -= attenuation if fontSize <= limit: return Status.FAILURE font = ImageFont.truetype(f'{RESOURCES_BASE_PATH}/font/{fontName}', fontSize) textSize = font.getsize(text) x = int(position[0] - textSize[0] / 2) y = int(position[1] - textSize[1] / 2) draw.text((x, y), text, color, font = font) return Status.SUCCESS def additionalSignInInformation(self): fontSize = self._infoFontSize coordinateY = self._infoCoordinatesY font = self._infoFontName x = int(self._basemapSize / 2) # Add user nickname result = self.writePicture(img = self._canvas, text = self._userNickname, position = (x, coordinateY.nickname), fontName = font.nickname, fontSize = fontSize.nickname) if result == Status.FAILURE: return Status.FAILURE # Add success message result = self.writePicture(img = self._canvas, text = self._userInfo, position = (x, coordinateY.info), fontName = font.info, fontSize = fontSize.info) if result == Status.FAILURE: return Status.FAILURE # Add integration information result = self.writePicture(img = self._canvas, text = self._userInfoIntegration, position = (x, coordinateY.integration), fontName = font.integration, fontSize = fontSize.integration) if result == Status.FAILURE: return Status.FAILURE # Addition hitokoto result = self.writePicture(img = self._canvas, text = self._userHitokoto, position = (x, coordinateY.hitokoto), fontName = font.hitokoto, fontSize = fontSize.hitokoto) if result == Status.FAILURE: return Status.FAILURE return self def save(self): dir = f'{RESOURCES_BASE_PATH}/cache' Tools.checkFolder(dir) if highQuality: path = f'{RESOURCES_BASE_PATH}/cache/{self._userQQ}.png' self._canvas.save(path) else: path = f'{RESOURCES_BASE_PATH}/cache/{self._userQQ}.jpg' self._canvas.convert('RGB').save(path) async def drawing(self): # Start generating result = await self.createAvatar() result = (result.createRoundImg() .createCanvas() .createAMagicCircle() .createTextBasemap() # Start processing .additionalMagicCircle() .additionalAvatar() .additionalTextBaseMap() # Must be the last step .additionalSignInInformation()) if result == Status.FAILURE: return result # Save result.save() return Status.SUCCESS
python
import time import argparse import numpy as np import matplotlib.pyplot as plt import torch import torchvision import torchvision.transforms as transforms import n3ml.model import n3ml.encoder import n3ml.optimizer np.set_printoptions(threshold=np.inf, linewidth=np.nan) class Plot: def __init__(self): plt.ion() self.fig, self.ax = plt.subplots(figsize=(10, 10)) self.ax2 = self.ax.twinx() plt.title('BP-STDP') def update(self, y1, y2): x = torch.arange(y1.shape[0]) * 30 ax1 = self.ax ax2 = self.ax2 ax1.plot(x, y1, 'g') ax2.plot(x, y2, 'b') ax1.set_xlabel('number of images') ax1.set_ylabel('accuracy', color='g') ax2.set_ylabel('loss', color='b') self.fig.canvas.draw() self.fig.canvas.flush_events() def accuracy(r: torch.Tensor, label: int) -> torch.Tensor: """ :param r: (time interval, # classes) the spike trains of output neurons in T ms :param label: :return: """ return (torch.argmax(torch.sum(r, dim=0)) == label).float() def mse(r: torch.Tensor, z: torch.Tensor, label: int, epsilon: int = 4) -> torch.Tensor: """ :param r: (time interval, # classes) the spike trains of output neurons in T ms :param z: (time interval, # classes) the desired spike trains in T ms :return: """ e = torch.zeros_like(r) for t in range(e.size(0)): if z[t, label] > 0.5: tt = t-epsilon if t-epsilon > 0 else 0 for i in range(e.size(1)): if i == label: if torch.sum(r[tt:t, i]) < 0.5: e[t, i] = 1 else: if torch.sum(r[tt:t, i]) > 0.5: e[t, i] = -1 T = r.size(0) return (torch.sum(e, dim=[0, 1])/T)**2 def label_encoder(label, beta, num_classes, time_interval): """ 1초 동안에 생성될 수 있는 spikes의 수는 얼마나 될까? 이는 continuous time domain인지 discrete time domain인지에 따라 달라질 수 있다. 예를 들어, continuous의 경우는 최대로 생성될 수 있는 스파이크 수는 무한대가 된다. 반면에 discrete의 경우는 최대로 생성될 수 있는 스파이크 수는 time step에 영향을 받는다. 현재 구현에서는 time step이 1ms라고 가정을 하고 spike train을 생성한다. 향후에 입력된 time step에 따라 적절한 spike train을 생성하는 방법을 구현할 필요가 있다. """ r = torch.zeros((time_interval, num_classes)) r[:, label] = torch.rand(time_interval) <= (beta/1000) return r def validate(loader, model, encoder, criterion, opt): num_images = 0 total_loss = 0.0 num_corrects = 0 for image, label in loader: image = image.squeeze(dim=0).cuda() label = label.squeeze().cuda() spiked_image = encoder(image) spiked_image = spiked_image.view(spiked_image.size(0), -1) spiked_label = label_encoder(label, opt.beta, opt.num_classes, opt.time_interval) loss_buffer = [] for t in range(opt.time_interval): model(spiked_image[t]) loss_buffer.append(model.fc2.o.clone()) model.reset_variables(w=False) num_images += 1 num_corrects += accuracy(r=torch.stack(loss_buffer), label=label) total_loss += criterion(r=torch.stack(loss_buffer), z=spiked_label, label=label, epsilon=opt.epsilon) return total_loss/num_images, float(num_corrects)/num_images def train(loader, model, encoder, optimizer, criterion, opt) -> None: plotter = Plot() num_images = 0 total_loss = 0.0 num_corrects = 0 list_loss = [] list_acc = [] for image, label in loader: # Squeeze batch dimension # Now, batch processing isn't supported image = image.squeeze(dim=0) label = label.squeeze() spiked_image = encoder(image) spiked_image = spiked_image.view(spiked_image.size(0), -1) spiked_label = label_encoder(label, opt.beta, opt.num_classes, opt.time_interval) # print(label) # print(spiked_label) # exit(0) # np_spiked_image = spiked_image.numpy() spike_buffer = { 'inp': [], 'fc1': [], 'fc2': [] } loss_buffer = [] print() print("label: {}".format(label)) for t in range(opt.time_interval): # print(np_spiked_image[t]) model(spiked_image[t]) spike_buffer['inp'].append(spiked_image[t].clone()) spike_buffer['fc1'].append(model.fc1.o.clone()) spike_buffer['fc2'].append(model.fc2.o.clone()) loss_buffer.append(model.fc2.o.clone()) for l in spike_buffer.values(): if len(l) > 5: # TODO: 5를 epsilon을 사용해서 표현해야 함 l.pop(0) # print(model.fc1.u.numpy()) # print(model.fc1.o.numpy()) # print(model.fc2.u.numpy()) print(model.fc2.o.numpy()) # time.sleep(1) optimizer.step(spike_buffer, spiked_label[t], label) model.reset_variables(w=False) num_images += 1 num_corrects += accuracy(r=torch.stack(loss_buffer), label=label) total_loss += criterion(r=torch.stack(loss_buffer), z=spiked_label, label=label, epsilon=opt.epsilon) if num_images > 0 and num_images % 30 == 0: list_loss.append(total_loss / num_images) list_acc.append(float(num_corrects) / num_images) plotter.update(y1=np.array(list_acc), y2=np.array(list_loss)) # print("loss: {} - accuracy: {}".format(total_loss/num_images, float(num_corrects)/num_images)) # return total_loss/num_images, float(num_corrects)/num_images def app(opt): print(opt) # Load MNIST train_loader = torch.utils.data.DataLoader( torchvision.datasets.MNIST( opt.data, train=True, transform=torchvision.transforms.Compose([transforms.ToTensor()])), batch_size=opt.batch_size, shuffle=True) val_loader = torch.utils.data.DataLoader( torchvision.datasets.MNIST( opt.data, train=False, transform=torchvision.transforms.Compose([transforms.ToTensor()])), batch_size=opt.batch_size, shuffle=False) # Make a model model = n3ml.model.TravanaeiAndMaida2017(opt.num_classes, hidden_neurons=opt.hidden_neurons) model.reset_variables() # Make an encoder encoder = n3ml.encoder.Simple(time_interval=opt.time_interval) # Make an optimizer optimizer = n3ml.optimizer.TavanaeiAndMaida(model, lr=opt.lr) # Define a loss criterion = mse for epoch in range(opt.num_epochs): # loss, acc = train(train_loader, model, encoder, optimizer, criterion, opt) # print("epoch: {} - loss: {} - accuracy: {}".format(epoch, loss, acc)) train(train_loader, model, encoder, optimizer, criterion, opt) loss, acc = validate(val_loader, model, encoder, criterion, opt) print("In test, loss: {} - accuracy: {}".format(loss, acc)) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data', default='data') parser.add_argument('--num_classes', default=10, type=int) parser.add_argument('--num_epochs', default=120, type=int) parser.add_argument('--batch_size', default=1, type=int) parser.add_argument('--time_interval', default=20, type=int) parser.add_argument('--beta', default=250, type=float) # 250 Hz parser.add_argument('--lr', default=0.0005, type=float) parser.add_argument('--hidden_neurons', default=500, type=int) parser.add_argument('--epsilon', default=4, type=int) app(parser.parse_args())
python
import numpy as np from c4.tables import rev_segments, all_segments PLAYER1 = 1 PLAYER2 = 2 DRAW = 0 COMPUTE = -1 class WrongMoveError(Exception): pass class Board(object): def __init__(self, pos=None, stm=PLAYER1, end=COMPUTE, cols=7, rows=6): if pos is None: pos = np.zeros((cols, rows), dtype=int) self._pos = pos self._stm = stm if end == COMPUTE: self._end = self._check_end(pos) else: self._end = end @property def end(self): return self._end @property def stm(self): return self._stm @property def other(self): return PLAYER1 if self._stm != PLAYER1 else PLAYER2 @classmethod def _check_end(cls, pos): for seg in cls.segments(pos): c = np.bincount(seg) if c[0]: continue if c[PLAYER1] == 4: return PLAYER1 elif c[PLAYER2] == 4: return PLAYER2 if pos.all(): return DRAW else: return None @classmethod def _check_end_around(cls, pos, r, c, side): if (cls.segments_around(pos, r, c) == side).all(1).any(): return side if pos.all(): return DRAW else: return None @classmethod def segments(cls, pos): if isinstance(pos, Board): return cls.segments(pos._pos) else: pos = pos.flatten() return pos[all_segments] @classmethod def segments_around(cls, pos, r, c): if isinstance(pos, Board): return cls.segments_around(pos._pos, r, c) else: idx = c * pos.shape[1] + r pos = pos.flatten() return pos[rev_segments[idx]] def __str__(self): disc = { 0: ' ', 1: 'X', 2: 'O' } s = [] for row in reversed(self._pos.transpose()): s.append(' | '.join(disc[x] for x in row)) s.append(' | '.join('-'*7)) s.append(' | '.join(map(str, range(1, 8)))) s = ['| ' + x + ' |' for x in s] s = [i + ' ' + x for i, x in zip('ABCDEFG ', s)] s = '\n'.join(s) if self._end is DRAW: s += '\n<<< Game over: draw' % [self._end] elif self._end is not None: s += '\n<<< Game over: %s win' % disc[self._end] else: s += '\n<<< Move to %s' % disc[self._stm] return s def move(self, m): if not (0 <= m < 7): raise ValueError(m) pos = self._pos.copy() r = pos[m].argmin() if pos[m][r] != 0: raise WrongMoveError('Full Column') pos[m][r] = self._stm end = self._check_end_around(pos, r, m, self._stm) stm = self.other return Board(pos, stm, end) def freerow(self, m): r = self._pos[m].argmin() if self._pos[m][r] != 0: return None return r def moves(self): return np.flatnonzero(self._pos[:, -1] == 0) def hashkey(self): """Generates an hashkey Returns a tuple (key, flip) flip is True if it returned the key of the symmetric Board. """ k1 = 0 k2 = 0 for x in self._pos.flat: k1 *= 3 k1 += int(x) assert k1 >= 0 for x in self._pos[::-1].flat: k2 *= 3 k2 += int(x) assert k2 >= 0 if k2 < k1: return k2, True else: return k1, False
python
from math import sqrt __author__ = "Samvid Mistry" import time from MAnimations.MAnimate import MAnimate from PySide.QtGui import QApplication, QPainterPath from PySide.QtCore import QPoint, QPointF class MCircularReveal(MAnimate): """ Can be used to perform circular reveal or circular hide animation on an MShape object. Requires self.target to be either 'show[_circle]' or 'hide[_circle]' """ def __init__(self): MAnimate.__init__(self) self.__clip = QPainterPath() def animate(self, shapes): self.start_signal.emit() time.sleep(self.start_delay) self.running = True self.ended = False target_radius = [] original_clips = [] centers = [] animating_radius = [] rate_of_change = [] for s in shapes: if self.target.startswith("show"): # Setting max of width or height as radius, ergo "circular" reveal, # not "oval" reveal side = max(s.width, s.height) side_square = side * side # Applying pythagoras theorem target = sqrt(side_square + side_square) # Starting from the zero reaching the max animating_radius.append(0) rate_of_change.append((target / self.fps) * (1000 / self.duration)) elif self.target.startswith("hide"): # You know why... target = 0 # Starting from the max reaching the 0 animating_radius.append(max(s.width, s.height)) rate_of_change.append(((target - max(s.width, s.height)) / self.fps) * (1000 / self.duration)) else: raise ValueError("Target should be either 'reveal' or 'hide'") target_radius.append(target) # Getting the original masks; Used in case of cancellation original_clips.append(s.clip) # Center of the shape, considering margin centers.append(QPoint((s.width / 2) + s.margin_left, (s.height / 2) + s.margin_top)) # Calculating the increase rate using the good ol' formula while self.running or self.paused: if self.canceled and not self.paused: for i, s in enumerate(shapes): s.clip = original_clips[i] self.cancel_signal.emit() return elif self.ended: self.end_signal.emit() return elif self.paused: # Handling the pause self.pause_signal.emit() while self.paused: # If you want the current state, pause the # animation and then cancel it if self.canceled: self.ended = True self.started = False self.cancel_signal.emit() return self.resume_signal.emit() else: # Setting FPS from the animator time.sleep(1 / self.fps) completed = False for i, s in enumerate(shapes): if rate_of_change[i] > 0: if not animating_radius[i] < target_radius[i]: completed = True else: # TODO: leaves 1 pixel visible in hiding the check, # added 1 to overall radius checking for now, look into this issue if not animating_radius[i] > target_radius[i] + 1: completed = True if not completed: animating_radius[i] += rate_of_change[i] path = QPainterPath() if self.target.endswith("circle"): path.addEllipse( QPointF((s.width / 2) + s.margin_left, (s.height / 2) + s.margin_top), animating_radius[i] / 2, animating_radius[i] / 2 ) else: path.addEllipse( QPointF((s.width / 2) + s.margin_left, (s.height / 2) + s.margin_top), animating_radius[i], animating_radius[i] ) s.clip = path s.update() QApplication.processEvents() # No need to check on every iteration, duration is same so # all objects are gonna end at the same time if completed: self.end_signal.emit() self.started = False self.ended = True return
python
import tensorflow as tf import numpy as np import sys, os import getopt import random import datetime import traceback import pandas as pd import cfr.cfr_net as cfr from cfr.util import * ''' Define parameter flags ''' FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string('loss', 'l2', """Which loss function to use (l1/l2/log)""") tf.app.flags.DEFINE_integer('n_in', 2, """Number of representation layers. """) tf.app.flags.DEFINE_integer('n_out', 2, """Number of regression layers. """) tf.app.flags.DEFINE_float('p_alpha', 1e-4, """Imbalance regularization param. """) tf.app.flags.DEFINE_float('p_lambda', 0.0, """Weight decay regularization parameter. """) tf.app.flags.DEFINE_integer('rep_weight_decay', 1, """Whether to penalize representation layers with weight decay""") tf.app.flags.DEFINE_float('dropout_in', 0.9, """Input layers dropout keep rate. """) tf.app.flags.DEFINE_float('dropout_out', 0.9, """Output layers dropout keep rate. """) tf.app.flags.DEFINE_string('nonlin', 'relu', """Kind of non-linearity. Default relu. """) tf.app.flags.DEFINE_float('lrate', 0.05, """Learning rate. """) tf.app.flags.DEFINE_float('decay', 0.5, """RMSProp decay. """) tf.app.flags.DEFINE_integer('batch_size', 100, """Batch size. """) tf.app.flags.DEFINE_integer('dim_in', 100, """Pre-representation layer dimensions. """) tf.app.flags.DEFINE_integer('dim_out', 100, """Post-representation layer dimensions. """) tf.app.flags.DEFINE_integer('batch_norm', 0, """Whether to use batch normalization. """) tf.app.flags.DEFINE_string('normalization', 'none', """How to normalize representation (after batch norm). none/bn_fixed/divide/project """) tf.app.flags.DEFINE_float('rbf_sigma', 0.1, """RBF MMD sigma """) tf.app.flags.DEFINE_integer('experiments', 1, """Number of experiments. """) tf.app.flags.DEFINE_integer('iterations', 2000, """Number of iterations. """) tf.app.flags.DEFINE_float('weight_init', 0.01, """Weight initialization scale. """) tf.app.flags.DEFINE_float('lrate_decay', 0.95, """Decay of learning rate every 100 iterations """) tf.app.flags.DEFINE_integer('wass_iterations', 20, """Number of iterations in Wasserstein computation. """) tf.app.flags.DEFINE_float('wass_lambda', 1, """Wasserstein lambda. """) tf.app.flags.DEFINE_integer('wass_bpt', 0, """Backprop through T matrix? """) tf.app.flags.DEFINE_integer('varsel', 0, """Whether the first layer performs variable selection. """) tf.app.flags.DEFINE_string('outdir', '../results/tfnet_topic/alpha_sweep_22_d100/', """Output directory. """) tf.app.flags.DEFINE_string('datadir', '../data/topic/csv/', """Data directory. """) tf.app.flags.DEFINE_string('dataform', 'topic_dmean_seed_%d.csv', """Training data filename form. """) tf.app.flags.DEFINE_string('data_test', '', """Test data filename form. """) tf.app.flags.DEFINE_integer('sparse', 0, """Whether data is stored in sparse format (.x, .y). """) tf.app.flags.DEFINE_integer('seed', 1, """Seed. """) tf.app.flags.DEFINE_integer('repetitions', 1, """Repetitions with different seed.""") tf.app.flags.DEFINE_integer('use_p_correction', 1, """Whether to use population size p(t) in mmd/disc/wass.""") tf.app.flags.DEFINE_string('optimizer', 'RMSProp', """Which optimizer to use. (RMSProp/Adagrad/GradientDescent/Adam)""") tf.app.flags.DEFINE_string('imb_fun', 'mmd_lin', """Which imbalance penalty to use (mmd_lin/mmd_rbf/mmd2_lin/mmd2_rbf/lindisc/wass). """) tf.app.flags.DEFINE_integer('output_csv',0,"""Whether to save a CSV file with the results""") tf.app.flags.DEFINE_integer('output_delay', 100, """Number of iterations between log/loss outputs. """) tf.app.flags.DEFINE_integer('pred_output_delay', -1, """Number of iterations between prediction outputs. (-1 gives no intermediate output). """) tf.app.flags.DEFINE_integer('debug', 0, """Debug mode. """) tf.app.flags.DEFINE_integer('save_rep', 0, """Save representations after training. """) tf.app.flags.DEFINE_float('val_part', 0, """Validation part. """) tf.app.flags.DEFINE_boolean('split_output', 0, """Whether to split output layers between treated and control. """) tf.app.flags.DEFINE_boolean('reweight_sample', 1, """Whether to reweight sample for prediction loss with average treatment probability. """) tf.app.flags.DEFINE_boolean('residual_block', 1, """Whether to use residual block for the output layers. """) tf.app.flags.DEFINE_boolean('embeddings', 0, """Whether to use embeddings as student features. """) tf.app.flags.DEFINE_string('rname', '../LSTM-autoencoder/result.pkl', """The file contains student representations. """) tf.app.flags.DEFINE_boolean('rnn', 0, """Whether to use rnn to extract features from student logs. """) tf.app.flags.DEFINE_string('ps', '', """The problem set id""") tf.app.flags.DEFINE_integer('hidden_num', 50, """The size of hidden layer in rnn""") tf.app.flags.DEFINE_boolean('trainable_embed', 0, """when rnn = 1, whether to use embeddings to represent problem sets""") FLAGS.dim_out = FLAGS.dim_in if FLAGS.sparse: import scipy.sparse as sparse NUM_ITERATIONS_PER_DECAY = 100 __DEBUG__ = False if FLAGS.debug: __DEBUG__ = True def train(CFR, sess, train_step, D, I_valid, D_test, logfile, i_exp, user_ids=None, test_user_ids=None, x_dict=None, len_dict=None, p_input=None, seq_len=None, ps_dict=None, sq_embed_idx=None): """ Trains a CFR model on supplied data """ ''' Train/validation split ''' n = D['x'].shape[0] I = range(n) I_train = list(set(I)-set(I_valid)) n_train = len(I_train) ''' Compute treatment probability''' p_treated = np.mean(D['t'][I_train,:]) ''' Set up loss feed_dicts''' if FLAGS.rnn: # load all data l = [] train_all_len = [] train_all_embed = [] for ite in user_ids: l.append(x_dict[ite]) train_all_len.append(len_dict[ite]) if FLAGS.trainable_embed: train_all_embed.append(ps_dict[ite]) train_all_x = np.stack(l, axis=0) if FLAGS.trainable_embed: train_all_embed = np.stack(train_all_embed, axis=0) l = [] test_all_len = [] test_all_embed = [] for ite in test_user_ids: l.append(x_dict[ite]) test_all_len.append(len_dict[ite]) if FLAGS.trainable_embed: test_all_embed.append(ps_dict[ite]) test_all_x = np.stack(l, axis=0) if FLAGS.trainable_embed: test_all_embed = np.stack(test_all_embed, axis=0) l = [] train_len = [] train_embed = [] for ite in user_ids[I_train]: l.append(x_dict[ite]) train_len.append(len_dict[ite]) if FLAGS.trainable_embed: train_embed.append(ps_dict[ite]) train_x = np.stack(l, axis=0) if FLAGS.trainable_embed: train_embed = np.stack(train_embed, axis=0) if FLAGS.trainable_embed: dict_factual = {p_input: train_x, seq_len: train_len, sq_embed_idx:train_embed, CFR.t: D['t'][I_train,:], CFR.y_: D['yf'][I_train,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} else: dict_factual = {p_input: train_x, seq_len: train_len, CFR.t: D['t'][I_train,:], CFR.y_: D['yf'][I_train,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} else: dict_factual = {CFR.x: D['x'][I_train,:], CFR.t: D['t'][I_train,:], CFR.y_: D['yf'][I_train,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} if FLAGS.val_part > 0: if FLAGS.rnn: l = [] valid_len = [] valid_embed = [] for ite in user_ids[I_valid]: l.append(x_dict[ite]) valid_len.append(len_dict[ite]) if FLAGS.trainable_embed: valid_embed.append(ps_dict[ite]) valid_x = np.stack(l, axis=0) if FLAGS.trainable_embed: dict_valid = {p_input: valid_x, seq_len: valid_len, sq_embed_idx: valid_embed, CFR.t: D['t'][I_valid,:], CFR.y_: D['yf'][I_valid,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} else: dict_valid = {p_input: valid_x, seq_len: valid_len, CFR.t: D['t'][I_valid,:], CFR.y_: D['yf'][I_valid,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} else: dict_valid = {CFR.x: D['x'][I_valid,:], CFR.t: D['t'][I_valid,:], CFR.y_: D['yf'][I_valid,:], CFR.do_in: 1.0, CFR.do_out: 1.0, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated} ''' Initialize TensorFlow variables ''' sess.run(tf.global_variables_initializer()) ''' Set up for storing predictions ''' preds_train = [] preds_test = [] ''' Compute losses ''' losses = [] obj_loss, f_error, imb_err = sess.run([CFR.tot_loss, CFR.pred_loss, CFR.imb_dist], feed_dict=dict_factual) cf_error = np.nan if D['HAVE_TRUTH']: cf_error = sess.run(CFR.pred_loss, feed_dict=dict_cfactual) valid_obj = np.nan; valid_imb = np.nan; valid_f_error = np.nan; if FLAGS.val_part > 0: valid_obj, valid_f_error, valid_imb = sess.run([CFR.tot_loss, CFR.pred_loss, CFR.imb_dist], feed_dict=dict_valid) losses.append([obj_loss, f_error, cf_error, imb_err, valid_f_error, valid_imb, valid_obj]) objnan = False reps = [] reps_test = [] ''' Train for multiple iterations ''' for i in range(FLAGS.iterations): ''' Fetch sample ''' I = random.sample(range(0, n_train), FLAGS.batch_size) x_batch = D['x'][I_train,:][I,:] t_batch = D['t'][I_train,:][I] y_batch = D['yf'][I_train,:][I] if FLAGS.rnn: user_batch = user_ids[I_train][I] l = [] batch_len = [] batch_embed = [] for ite in user_batch: l.append(x_dict[ite]) batch_len.append(len_dict[ite]) if FLAGS.trainable_embed: batch_embed.append(ps_dict[ite]) x_batch = np.stack(l, axis=0) ''' Do one step of gradient descent ''' if not objnan: if FLAGS.rnn: if FLAGS.trainable_embed: sess.run(train_step, feed_dict={p_input: x_batch, seq_len: batch_len, sq_embed_idx: batch_embed, CFR.t: t_batch, CFR.y_: y_batch, CFR.do_in: FLAGS.dropout_in, CFR.do_out: FLAGS.dropout_out, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated}) else: sess.run(train_step, feed_dict={p_input: x_batch, seq_len: batch_len, CFR.t: t_batch, CFR.y_: y_batch, CFR.do_in: FLAGS.dropout_in, CFR.do_out: FLAGS.dropout_out, CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated}) else: sess.run(train_step, feed_dict={CFR.x: x_batch, CFR.t: t_batch, \ CFR.y_: y_batch, CFR.do_in: FLAGS.dropout_in, CFR.do_out: FLAGS.dropout_out, \ CFR.r_alpha: FLAGS.p_alpha, CFR.r_lambda: FLAGS.p_lambda, CFR.p_t: p_treated}) ''' Project variable selection weights ''' if FLAGS.varsel: wip = simplex_project(sess.run(CFR.weights_in[0]), 1) sess.run(CFR.projection, feed_dict={CFR.w_proj: wip}) ''' Compute loss every N iterations ''' if i % FLAGS.output_delay == 0 or i==FLAGS.iterations-1: obj_loss,f_error,imb_err = sess.run([CFR.tot_loss, CFR.pred_loss, CFR.imb_dist], feed_dict=dict_factual) #rep = sess.run(CFR.h_rep_norm, feed_dict={CFR.x: D['x'], CFR.do_in: 1.0}) #rep_norm = np.mean(np.sqrt(np.sum(np.square(rep), 1))) cf_error = np.nan if D['HAVE_TRUTH']: cf_error = sess.run(CFR.pred_loss, feed_dict=dict_cfactual) valid_obj = np.nan; valid_imb = np.nan; valid_f_error = np.nan; if FLAGS.val_part > 0: valid_obj, valid_f_error, valid_imb = sess.run([CFR.tot_loss, CFR.pred_loss, CFR.imb_dist], feed_dict=dict_valid) losses.append([obj_loss, f_error, cf_error, imb_err, valid_f_error, valid_imb, valid_obj]) loss_str = str(i) + '\tObj: %.3f,\tF: %.3f,\tCf: %.3f,\tImb: %.2g,\tVal: %.3f,\tValImb: %.2g,\tValObj: %.2f' \ % (obj_loss, f_error, cf_error, imb_err, valid_f_error, valid_imb, valid_obj) if FLAGS.loss == 'log': if FLAGS.rnn: if FLAGS.trainable_embed: y_pred = sess.run(CFR.output, feed_dict={p_input: x_batch, seq_len: batch_len, sq_embed_idx: batch_embed, CFR.t: t_batch, CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred = sess.run(CFR.output, feed_dict={p_input: x_batch, seq_len: batch_len, CFR.t: t_batch, CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred = sess.run(CFR.output, feed_dict={CFR.x: x_batch, CFR.t: t_batch, CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred = 1.0*(y_pred > 0.5) acc = 100*(1 - np.mean(np.abs(y_batch - y_pred))) loss_str += ',\tAcc: %.2f%%' % acc log(logfile, loss_str) if np.isnan(obj_loss): log(logfile,'Experiment %d: Objective is NaN. Skipping.' % i_exp) objnan = True ''' Compute predictions every M iterations ''' if (FLAGS.pred_output_delay > 0 and i % FLAGS.pred_output_delay == 0) or i==FLAGS.iterations-1: if FLAGS.rnn: if FLAGS.trainable_embed: y_pred_f = sess.run(CFR.output, feed_dict={p_input: train_all_x, seq_len: train_all_len,sq_embed_idx: train_all_embed, CFR.t: D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf = sess.run(CFR.output, feed_dict={p_input: train_all_x, seq_len: train_all_len,sq_embed_idx: train_all_embed, CFR.t: 1-D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred_f = sess.run(CFR.output, feed_dict={p_input: train_all_x, seq_len: train_all_len, CFR.t: D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf = sess.run(CFR.output, feed_dict={p_input: train_all_x, seq_len: train_all_len, CFR.t: 1-D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred_f = sess.run(CFR.output, feed_dict={CFR.x: D['x'], \ CFR.t: D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf = sess.run(CFR.output, feed_dict={CFR.x: D['x'], \ CFR.t: 1-D['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) preds_train.append(np.concatenate((y_pred_f, y_pred_cf),axis=1)) if D_test is not None: if FLAGS.rnn: if FLAGS.trainable_embed: y_pred_f_test = sess.run(CFR.output, feed_dict={p_input: test_all_x, seq_len: test_all_len, sq_embed_idx: test_all_embed, CFR.t: D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf_test = sess.run(CFR.output, feed_dict={p_input: test_all_x, seq_len: test_all_len, sq_embed_idx: test_all_embed, CFR.t: 1-D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred_f_test = sess.run(CFR.output, feed_dict={p_input: test_all_x, seq_len: test_all_len, CFR.t: D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf_test = sess.run(CFR.output, feed_dict={p_input: test_all_x, seq_len: test_all_len, CFR.t: 1-D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) else: y_pred_f_test = sess.run(CFR.output, feed_dict={CFR.x: D_test['x'], \ CFR.t: D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) y_pred_cf_test = sess.run(CFR.output, feed_dict={CFR.x: D_test['x'], \ CFR.t: 1-D_test['t'], CFR.do_in: 1.0, CFR.do_out: 1.0}) preds_test.append(np.concatenate((y_pred_f_test, y_pred_cf_test),axis=1)) return losses, preds_train, preds_test, reps, reps_test def run(outdir): """ Runs an experiment and stores result in outdir """ ''' Set up paths and start log ''' npzfile = outdir+'result' npzfile_test = outdir+'result.test' repfile = outdir+'reps' repfile_test = outdir+'reps.test' outform = outdir+'y_pred' outform_test = outdir+'y_pred.test' lossform = outdir+'loss' logfile = outdir+'log.txt' f = open(logfile,'w') f.close() dataform = FLAGS.datadir + FLAGS.dataform has_test = False if not FLAGS.data_test == '': # if test set supplied has_test = True dataform_test = FLAGS.datadir + FLAGS.data_test ''' Set random seeds ''' random.seed(FLAGS.seed) tf.set_random_seed(FLAGS.seed) np.random.seed(FLAGS.seed) ''' Save parameters ''' save_config(outdir+'config.txt') log(logfile, 'Training with hyperparameters: alpha=%.2g, lambda=%.2g' % (FLAGS.p_alpha,FLAGS.p_lambda)) ''' Load Data ''' npz_input = False if dataform[-3:] == 'npz': npz_input = True if npz_input: datapath = dataform if has_test: datapath_test = dataform_test else: datapath = dataform if has_test: datapath_test = dataform_test log(logfile, 'Training data: ' + datapath) if has_test: log(logfile, 'Test data: ' + datapath_test) #D = load_data(datapath) D = load_assistments_data(datapath, rname=FLAGS.rname, embeddings=FLAGS.embeddings) D_test = None if has_test: D_test = load_assistments_data(datapath_test, rname=FLAGS.rname, embeddings=FLAGS.embeddings) log(logfile, 'Loaded data with shape [%d,%d]' % (D['n'], D['dim'])) ''' Start Session ''' sess = tf.Session() ''' Parameter placeholders ''' r_alpha = tf.placeholder("float", name='r_alpha') r_lambda = tf.placeholder("float", name='r_lambda') do_in = tf.placeholder("float", name='dropout_in') do_out = tf.placeholder("float", name='dropout_out') p = tf.placeholder("float", name='p_treated') ''' Initialize input placeholders ''' if FLAGS.rnn: problem_set = FLAGS.ps trainable_embed = FLAGS.trainable_embed if trainable_embed: file_path = '../lstm-autoencoder/'+str(problem_set)+'_sq_train_data.csv' else: file_path = '../lstm-autoencoder/'+str(problem_set)+'_pl_train_data.csv' hidden_num = FLAGS.hidden_num pl_df = pd.read_csv(file_path) # the number of features d_num = 3 if trainable_embed else 2 elem_num = len(pl_df.columns)-d_num # group by students pl_df.set_index('id', inplace=True) pl_g = pl_df.groupby('user_id') cnt_list = [] for name,group in pl_g: cnt = len(group) cnt_list.append(cnt) max_len = max(cnt_list) avg_len = sum(cnt_list)/len(cnt_list) max_max_len = int(np.percentile(cnt_list, 70)) print 'max len {}'.format(max_len) print 'avg len {}'.format(avg_len) print 'max max len {}'.format(max_max_len) max_len = min(max_len, max_max_len) if trainable_embed: # load ps list if FLAGS.rnn: ps_file = '../lstm-autoencoder/'+str(problem_set)+'_ps_index' else: ps_file = '../lstm-autoencoder/2016_ps_index' ps_list = [] with open(ps_file) as f: for line in f: ps_list.append(int(line)) sq_embed_idx = tf.placeholder(tf.int32, [None, max_len]) #max_len = 1000 for i in range(len(cnt_list)): if cnt_list[i] > max_len: cnt_list[i] = max_len # get user id list user_list = pl_df['user_id'].unique().tolist() x_dict = {} len_dict = {} if trainable_embed: ps_dict = {} for ite in user_list: m = pl_g.get_group(ite).iloc[:, :-1*(d_num-1)].as_matrix() if trainable_embed: seq_ids = pl_g.get_group(ite)['sequence_id'].tolist() embed_ids = [] for seq_id in seq_ids: if seq_id in ps_list: tmp_idx = ps_list.index(seq_id) embed_ids.append(tmp_idx) else: embed_ids.append(len(ps_list)) if max_len >= m.shape[0]: len_dict[ite] = m.shape[0] diff = max_len - m.shape[0] x_dict[ite] = np.pad(m, ((0,diff), (0,0)), mode='constant', constant_values=0) if trainable_embed: ps_dict[ite] = embed_ids + [0]*diff else: len_dict[ite] = max_len x_dict[ite] = m[-1*max_len:, :] if trainable_embed: ps_dict[ite] = embed_ids[-1*max_len:] # load user ids from exp data data = np.loadtxt(open(dataform,"rb"),delimiter=",") user_ids = data[:, 1] test_data = np.loadtxt(open(dataform_test,"rb"),delimiter=",") test_user_ids = test_data[:, 1] p_input = tf.placeholder(tf.float32, [None, max_len, elem_num]) if FLAGS.trainable_embed: embedding_size = 10 # look up embeddings W = tf.get_variable('W', shape=[len(ps_list)+1, embedding_size], initializer=tf.contrib.layers.xavier_initializer()) sq_embed = tf.nn.embedding_lookup(W, sq_embed_idx) cell_input = tf.reshape(tf.expand_dims(sq_embed, -2) * tf.expand_dims(p_input, -1), [-1, max_len, embedding_size*elem_num]) else: cell_input = p_input cell = tf.nn.rnn_cell.GRUCell(hidden_num) cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=do_in) seq_len = tf.placeholder(tf.int32, [None]) z_codes, enc_state = tf.nn.dynamic_rnn(cell, cell_input, seq_len, dtype=tf.float32) x = enc_state dims = [hidden_num, FLAGS.dim_in, FLAGS.dim_out] else: x = tf.placeholder("float", shape=[None, D['dim']], name='x') # Features dims = [D['dim'], FLAGS.dim_in, FLAGS.dim_out] t = tf.placeholder("float", shape=[None, 1], name='t') # Treatent y_ = tf.placeholder("float", shape=[None, 1], name='y_') # Outcome ''' Define model graph ''' log(logfile, 'Defining graph...\n') CFR = cfr.cfr_net(x, t, y_, p, FLAGS, r_alpha, r_lambda, do_in, do_out, dims) ''' Set up optimizer ''' global_step = tf.Variable(0, trainable=False) lr = tf.train.exponential_decay(FLAGS.lrate, global_step, \ NUM_ITERATIONS_PER_DECAY, FLAGS.lrate_decay, staircase=True) opt = None if FLAGS.optimizer == 'Adagrad': opt = tf.train.AdagradOptimizer(lr) elif FLAGS.optimizer == 'GradientDescent': opt = tf.train.GradientDescentOptimizer(lr) elif FLAGS.optimizer == 'Adam': opt = tf.train.AdamOptimizer(lr) else: opt = tf.train.RMSPropOptimizer(lr, FLAGS.decay) ''' Unused gradient clipping ''' #gvs = opt.compute_gradients(CFR.tot_loss) #capped_gvs = [(tf.clip_by_value(grad, -1.0, 1.0), var) for grad, var in gvs] #train_step = opt.apply_gradients(capped_gvs, global_step=global_step) train_step = opt.minimize(CFR.tot_loss,global_step=global_step) ''' Set up for saving variables ''' all_losses = [] all_preds_train = [] all_preds_test = [] all_valid = [] if FLAGS.varsel: all_weights = None all_beta = None all_preds_test = [] ''' Handle repetitions ''' n_experiments = FLAGS.experiments if FLAGS.repetitions>1: if FLAGS.experiments>1: log(logfile, 'ERROR: Use of both repetitions and multiple experiments is currently not supported.') sys.exit(1) n_experiments = FLAGS.repetitions ''' Run for all repeated experiments ''' for i_exp in range(1,n_experiments+1): if FLAGS.repetitions>1: log(logfile, 'Training on repeated initialization %d/%d...' % (i_exp, FLAGS.repetitions)) else: log(logfile, 'Training on experiment %d/%d...' % (i_exp, n_experiments)) ''' Load Data (if multiple repetitions, reuse first set)''' if i_exp==1 or FLAGS.experiments>1: D_exp_test = None if npz_input: D_exp = {} D_exp['x'] = D['x'][:,:,i_exp-1] D_exp['t'] = D['t'][:,i_exp-1:i_exp] D_exp['yf'] = D['yf'][:,i_exp-1:i_exp] if D['HAVE_TRUTH']: D_exp['ycf'] = D['ycf'][:,i_exp-1:i_exp] else: D_exp['ycf'] = None if has_test: D_exp_test = {} D_exp_test['x'] = D_test['x'][:,:,i_exp-1] D_exp_test['t'] = D_test['t'][:,i_exp-1:i_exp] D_exp_test['yf'] = D_test['yf'][:,i_exp-1:i_exp] if D_test['HAVE_TRUTH']: D_exp_test['ycf'] = D_test['ycf'][:,i_exp-1:i_exp] else: D_exp_test['ycf'] = None else: datapath = dataform D_exp = load_assistments_data(datapath, rname=FLAGS.rname, embeddings=FLAGS.embeddings) if has_test: datapath_test = dataform_test D_exp_test = load_assistments_data(datapath_test, rname=FLAGS.rname, embeddings=FLAGS.embeddings) D_exp['HAVE_TRUTH'] = D['HAVE_TRUTH'] if has_test: D_exp_test['HAVE_TRUTH'] = D_test['HAVE_TRUTH'] ''' Split into training and validation sets ''' I_train, I_valid = validation_split(D_exp, FLAGS.val_part) ''' Run training loop ''' # pass more parameters: p_input, seq_len, rnn if FLAGS.rnn: if FLAGS.trainable_embed: losses, preds_train, preds_test, reps, reps_test = train(CFR, sess, train_step, D_exp, I_valid, D_exp_test, logfile, i_exp, user_ids, test_user_ids, x_dict, len_dict, p_input, seq_len, ps_dict, sq_embed_idx) else: losses, preds_train, preds_test, reps, reps_test = train(CFR, sess, train_step, D_exp, I_valid, D_exp_test, logfile, i_exp, user_ids, test_user_ids, x_dict, len_dict, p_input, seq_len) else: losses, preds_train, preds_test, reps, reps_test = train(CFR, sess, train_step, D_exp, I_valid, D_exp_test, logfile, i_exp) ''' Collect all reps ''' all_preds_train.append(preds_train) all_preds_test.append(preds_test) all_losses.append(losses) ''' Fix shape for output (n_units, dim, n_reps, n_outputs) ''' out_preds_train = np.swapaxes(np.swapaxes(all_preds_train,1,3),0,2) if has_test: out_preds_test = np.swapaxes(np.swapaxes(all_preds_test,1,3),0,2) out_losses = np.swapaxes(np.swapaxes(all_losses,0,2),0,1) ''' Store predictions ''' log(logfile, 'Saving result to %s...\n' % outdir) if FLAGS.output_csv: np.savetxt('%s_%d.csv' % (outform,i_exp), preds_train[-1], delimiter=',') np.savetxt('%s_%d.csv' % (outform_test,i_exp), preds_test[-1], delimiter=',') np.savetxt('%s_%d.csv' % (lossform,i_exp), losses, delimiter=',') ''' Compute weights if doing variable selection ''' if FLAGS.varsel: if i_exp == 1: all_weights = sess.run(CFR.weights_in[0]) all_beta = sess.run(CFR.weights_pred) else: all_weights = np.dstack((all_weights, sess.run(CFR.weights_in[0]))) all_beta = np.dstack((all_beta, sess.run(CFR.weights_pred))) ''' Save results and predictions ''' all_valid.append(I_valid) if FLAGS.varsel: np.savez(npzfile, pred=out_preds_train, loss=out_losses, w=all_weights, beta=all_beta, val=np.array(all_valid)) else: np.savez(npzfile, pred=out_preds_train, loss=out_losses, val=np.array(all_valid)) if has_test: np.savez(npzfile_test, pred=out_preds_test) ''' Save representations ''' if FLAGS.save_rep and i_exp == 1: np.savez(repfile, rep=reps) if has_test: np.savez(repfile_test, rep=reps_test) def main(argv=None): # pylint: disable=unused-argument """ Main entry point """ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S-%f") outdir = FLAGS.outdir+'/results_'+timestamp+'/' os.mkdir(outdir) try: run(outdir) except Exception as e: with open(outdir+'error.txt','w') as errfile: errfile.write(''.join(traceback.format_exception(*sys.exc_info()))) raise if __name__ == '__main__': tf.app.run()
python
from compecon.tools import Options_Container, qzordered import numpy as np import pandas as pd from compecon.tools import jacobian, hessian, gridmake, indices __author__ = 'Randall' class LQlabels(Options_Container): """ Container for labels of the LQmodel variables Attributes: s labels for continuous state variables x labels for continuous action variables """ description = "Labels for LQmodel variables" def __init__(self, s, x): self.s = list(s) self.x = list(x) @property def snext(self): return [k + '_next' for k in self.s] @property def p(self): return ['value_' + k for k in self.s] class LQmodel(object): """ A Linear-Quadratic dynamic programming model class Solves discrete time linear quadratic control model using Ricatti equation methods Uses QZ decomposition to solve the Ricatti equation of a deterministic stationary infinite-horizon linear-quadratic dynamic optimization model max_x f0 + fs*s + fx*x + 0.5*s'*fss*s + s'*fsx*x +0.5*x'*fxx*x s.t. s' = g0 + gs*s + gx*x The optimal policy function is x(s) = xstar + X*(s-sstar) The shadow price function is p(s) = pstar + P*(s-sstar) The value function is V(s) = vstar + pstar*(s-sstar) + 0.5*(s-sstar)'*P*(s-sstar) The controlled state process is snext = sstar + G*(s-sstar) """ # TODO: write the docstring def __init__(self, f0,fs,fx,fss,fsx,fxx,g0,gs,gx,delta, slabels=None, xlabels=None): """ Args: f0: 1.1 objective function parameter fs: 1.ds objective function parameter fx: 1.dx objective function parameter fss: ds.ds objective function parameter fsx: ds.dx objective function parameter fxx: dx.dx objective function parameter g0: ds.1 state transition function parameter gs: ds.ds state transition function parameter gx: ds.dx state transition function parameter delta: discount factor """ fs, fx, fss, fsx, fxx, g0, gs, gx = np.atleast_2d(fs,fx,fss,fsx,fxx,g0,gs,gx) # Determine dimensions of state and action variables ds = fs.size dx = fx.size fs.shape = 1, ds fx.shape = 1, dx # Check conformability assert fss.shape == (ds, ds), f'error in LQmodel: fss must be a {ds} by {ds} matrix' assert fsx.shape == (ds, dx), f'error in LQmodel: fsx must be a {ds} by {dx} matrix' assert fxx.shape == (dx, dx), f'error in LQmodel: fxx must be a {dx} by {dx} matrix' assert g0.shape == (ds, 1), f'error in LQmodel: g0 must be a {ds} by 1 matrix' assert gs.shape == (ds, ds), f'error in LQmodel: gs must be a {ds} by {ds} matrix' assert gx.shape == (ds, dx), f'error in LQmodel: gx must be a {ds} by {dx} matrix' self.f0 = f0 self.fs = fs self.fx = fx self.fss = fss self.fsx = fsx self.fxx = fxx self.g0 = g0 self.gs = gs self.gx = gx self.delta = delta self.dims ={'ds': ds, 'dx': dx} self.X = np.nan self.P = np.nan self.G = np.nan '''MAKE THE LABELS''' if slabels is None: slabels = ['s'] if ds == 1 else [f's{i}' for i in range(ds)] if xlabels is None: xlabels = ['x'] if dx == 1 else [f'x{i}' for i in range(dx)] self.labels = LQlabels(slabels, xlabels) '''SOLVE THE MODEL''' self.solve() ''' <<<<<<<<<<<<<<<<<<< END OF CONSTRUCTOR >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>''' def __repr__(self): pass txt = 'A continuous state, ' + ('continuous' if self.dims.dx > 0 else 'discrete') + ' action dynamic model.\n' txt = txt.upper() txt += '\n\t* Continuous states:\n' n, a, b = self.Value.n, self.Value.a, self.Value.b for k in range(self.Value.d): txt += "\t\t{:<2d}: {:<s} --> {:d} nodes in [{:.2f}, {:.2f}]\n".format(k, self.labels.s[k], n[k], a[k], b[k]) if self.dims.dx > 0: txt += '\n\t* Continuous actions\n' for v, vlab in enumerate(self.labels.x): txt += '\t\t{:<2d}: {:s}\n'.format(v, vlab) if self.dims.ni > 1: txt += '\n\t* Discrete states\n' for v, vlab in enumerate(self.labels.i): txt += '\t\t{:<2d}: {:s}\n'.format(v, vlab) if self.dims.nj > 1: txt += '\n\t* Discrete choices:\n' for v, vlab in enumerate(self.labels.j): txt += '\t\t{:<2d}: {:s}\n'.format(v, vlab) return txt @property def steady_state(self): return self.steady['s'], self.steady['x'], self.steady['p'], self.steady['v'] def Value(self, ss): ''' Args: ss: state evaluation points, ds.ns Returns: value function at ss Note: The value function is V(s) = vstar + pstar' * (s-sstar) + 0.5*(s-sstar)'*P*(s-sstar) ''' sstar, xstar, pstar, vstar = self.steady_state ss0 = ss-sstar # ds.ns Pss0 = self.P @ ss0 ss0Pss0 = [k.dot(h) for k, h in zip(ss0.T, Pss0.T)] return vstar + pstar.T @ ss0 + 0.5 * np.array(ss0Pss0) def Policy(self, ss): ''' Args: ss: state evaluation points Returns: policy function at ss Notes: The optimal policy function is x(s) = xstar + X * (s - sstar) ''' sstar, xstar, pstar, vstar = self.steady_state return xstar + self.X @ (ss - sstar) def Shadow(self, ss): ''' Args: ss: state evaluation points Returns: shadow price function at ss Notes: The shadow price function is p(s) = pstar + P * (s - sstar) ''' sstar, xstar, pstar, vstar = self.steady_state return pstar + self.P @ (ss - sstar) def Next(self, ss): ''' Args: ss: state evaluation points Returns: controlled state process at ss Notes: The controlled state process is snext(s) = sstar + G * (s - sstar) ''' sstar, xstar, pstar, vstar = self.steady_state return sstar + self.G @ (ss - sstar) def solution(self, ss): """ Computes solution over a refined grid s: -- array >> compute solution over provided values """ ds, dx = self.dims['ds'], self.dims['dx'] '''GET THE DATA''' ss = np.atleast_2d(ss) assert ss.shape[0] == ds, 'provided s grid must have {} rows'.format(ds) xr = self.Policy(ss) vr = self.Value(ss) pr = self.Shadow(ss) snext = self.Next(ss) ''' MAKE DATABASE''' DATA = pd.DataFrame(np.r_[ss, xr, vr, pr, snext].T, columns=self.labels.s + self.labels.x + ['value'] + self.labels.p + self.labels.snext) '''SET INDEX FOR DATA''' if ds == 1: DATA.index = DATA[self.labels.s[0]] return DATA def simulate(self, nper, sinit, seed=None): # Simulate the model # # S = self.simulate(nper, sinit, iinit) # # nper = number of periods to simulate (scalar) # sinit = initial continuos state (nrep x ds), where nrep is number of repetitions # # S = simulation results (table), with variables: # r, repetion number # t, time period # s, continuous state # x, optimal continuous action # ****** 1: Preparation*********************************************************** #TODO: ADD THE STOCHASTIC COMPONENT ds, dx, = self.dims['ds'], self.dims['dx'] if seed: np.random.seed(seed) # Determine number of replications nrep and periods nper to be simulated. # nper cannot exceed time.horizon. sinit = np.atleast_2d(sinit).astype(float) ds2, nrep = sinit.shape assert ds==ds2, 'initial continous state must have {} rows'.format(ds) ### Allocate memory to output arrays ssim = np.empty((nper+1, ds, nrep)) xsim = np.empty((nper, dx, nrep)) ### Set initial states ssim[0] = sinit # ***** *2: Simulate the model *************************************************** for t in range(nper): xsim[t] = self.Policy(ssim[t]) ssim[t+1] = self.Next(ssim[t]) ### Trim the last observation ssim = ssim[:nper] # ****** 3: Make a table with the simulated data ********************************* '''MAKE THE LABELS''' slabels = ['s'] if ds == 1 else [f's{i}' for i in range(ds)] xlabels = ['x'] if dx == 1 else [f'x{i}' for i in range(dx)] '''MAKE DATA MATRICES''' sdata = ssim.swapaxes(0, 1).reshape((ds, -1)) xdata = xsim.swapaxes(0, 1).reshape((dx, -1)) ### Add variables rsim and tsim to identify the repetition number and the time # period of each observation tsim, rsim = gridmake(np.arange(nper), np.arange(nrep)) # Make the table. DATA = pd.DataFrame() DATA['time'] = tsim if nrep > 1: DATA['_rep'] = rsim for k, slab in enumerate(slabels): DATA[slab] = sdata[k] for k, xlab in enumerate(xlabels): DATA[xlab] = xdata[k] return DATA def solve(self): # Unpak data ds = self.dims['ds'] dx = self.dims['dx'] delta = self.delta f0, fx, fxx = self.f0, self.fx, self.fxx g0, gx = self.g0, self.gx fs, fsx, fss, gs = self.fs, self.fsx, self.fss, self.gs ''' Solve Riccati equation using QZ decomposition ''' dx2ds = dx + 2 * ds A = np.zeros((dx2ds, dx2ds)) A[:ds, :ds] = np.identity(ds) A[ds:-ds, -ds:] = -delta * gx.T A[-ds:, -ds:] = delta * gs.T B = np.zeros_like(A) B[:ds, :-ds] = np.c_[gs, gx] B[ds: -ds, :-ds] = np.c_[fsx.T, fxx] B[-ds:] = np.c_[-fss, -fsx, np.identity(ds)] S, T, Q, Z = qzordered(A, B) C = np.real(np.linalg.solve(Z[:ds, :ds].T, Z[ds:, :ds].T)).T X = C[:dx] P = C[dx:, :] G = gs + gx @ X self.X = X self.P = P self.G = G ''' Compute steady-state state, action, and shadow price''' t0 = np.r_[np.c_[fsx.T, fxx, delta * gx.T], np.c_[fss, fsx, delta*gs.T - np.eye(ds)], np.c_[gs - np.eye(ds), gx, np.zeros((ds, ds))]] t1 = np.r_[-fx.T, -fs.T, -g0] t = np.linalg.solve(t0, t1) sstar, xstar, pstar = np.split(t, [ds, ds + dx]) vstar = (f0 + fs @ sstar + fx @ xstar + 0.5 * sstar.T @ fss @ sstar + sstar.T @ fsx @ xstar + 0.5 * xstar.T @ fxx @ xstar) / (1 - delta) self.steady = {'s':sstar, 'x': xstar, 'p': pstar, 'v':vstar}
python
h1, m1, h2, m2 = map(int, input().split()) minutos = m2 - m1 horas = h2 - h1 if horas <= 0: horas += 24 if minutos < 0: horas -= 1 minutos = 60 + minutos print("O JOGO DUROU {} HORA(S) E {} MINUTO(S)".format(horas, minutos))
python
# grad.py """ Created on Fri May 25 19:10:00 2018 @author: Wentao Huang """ from torch.autograd import Function class Grad(Function): r"""Records operation history and defines formulas for differentiating ops. Each function object is meant to be used only once (in the forward pass). Attributes: requires_grad: Boolean indicating whether the :func:`backward` will ever need to be called. """ @staticmethod def forward(ctx, input, C, bias=None, beta=1.0, isorth=True, eps=1e-6, *args, **kwargs): r"""Performs the operation. This function is to be overridden by all subclasses. It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types). The context can be used to store tensors that can be then retrieved during the backward pass. """ return NotImplementedError @staticmethod def backward(ctx, grad_output=None): r"""Defines a formula for differentiating the operation. It must accept a context ctx as the first argument, followed by as many outputs did :func:`forward` return, and it should return as many tensors, as there were inputs to :func:`forward`. Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. The context can be used to retrieve tensors saved during the forward pass. """ # d_input=d_C=d_b=d_beta=d_isorth=d_eps=None dC, db, argnum = ctx.saved_variables output = [None]*int(argnum) if ctx.needs_input_grad[1]: output[1] = dC #d_C = dC if ctx.needs_input_grad[2]: output[2] = db #d_b = db if grad_output is not None: output[1] = grad_output * output[1] if output[2] is not None: output[2] = grad_output * output[2] return tuple(output) # d_input, d_C, d_b, d_beta, d_isorth, d_eps
python
# !/usr/bin/env python # -*- coding: utf-8 -*- """ @author: Wang Zhiyi @file: logger.py @time: 6/20/2021 @version: 1.0 """ import os from pyspark.ml.pipeline import PipelineModel from spark_manager import get_spark_session _spark_session = get_spark_session() _spark_context = _spark_session.sparkContext def load_model_from_file(path): """ Reconstruct a models from a file persisted with models.dump. Args: path (string): The source path stores a models. Returns: ModelObject: Model object. """ # model = RandomForestClassificationModel.load(path=path) # model = RandomForestClassifier.load(path=path) # model = PipelineModel.load(path) model = PipelineModel.load(path) os.system('echo -e "\033[31m\033[1m{}\033[0m"'.format(str(model))) return model def load_model_to_file(model, path): """ Persist an models object into one file. Args: model (ModelObject): The model object. path (string): The target path stores a models. Returns: NoneType: None """ # dump(model, path) model.write().overwrite().save(path=path) return None
python
import tensorflow as tf import numpy as np from PIL import Image import os def maybe_download(directory, filename, url): print('Try to dwnloaded', url) if not tf.gfile.Exists(directory): tf.gfile.MakeDirs(directory) filepath = os.path.join(directory, filename) if not tf.gfile.Exists(filepath): filepath, _ = urllib.request.urlretrieve(url, filepath) with tf.gfile.GFile(filepath) as f: size = f.size() print('Successfully downloaded', filename, size, 'bytes.') return filepath def load_pretrained(filepath): return np.load(filepath, encoding='bytes').item() def get_epoch(): epoch_step = tf.Variable(0, name='epoch_step', trainable=False) epoch_update = epoch_step.assign(epoch_step + 1) return epoch_step, epoch_update def load_imgs(train_img_dir, filelist): def load_img(path): _img = Image.open(path) img = np.array(_img) _img.close() return img _imgs = [os.path.join(train_img_dir, filename + ".png") for filename in filelist] imgs = [load_img(_img) for _img in _imgs] return imgs def load_annots(train_annot_dir, filelist): def load_annot(path): #print(path) annot = np.load(path, encoding='bytes') #print("original dims: {}x{}".format(annot[0,0], annot[0,1])) return annot _annots = [os.path.join(train_annot_dir, filename + ".npy") for filename in filelist] annots = [load_annot(_annot) for _annot in _annots] return annots def tf_Print(on, x, summarize=50, message=""): if on: x = tf.Print(x, [x, tf.shape(x)], summarize=summarize, message=message) return x def debug_print(on, *x): if on: print(x) return x
python
# -*- coding: utf-8 -*- import scrapy import json from locations.items import GeojsonPointItem from locations.hours import OpeningHours class SprintSpider(scrapy.Spider): name = "sprint" allowed_domains = ["sprint.com"] start_urls = ( 'https://www.sprint.com/locations/', ) def parse_hours(self, store_hours): opening_hours = OpeningHours() for store_day in store_hours: day, open_close = store_day.split(' ') open_time, close_time = open_close.split('-') opening_hours.add_range(day=day, open_time=open_time, close_time=close_time, time_format='%H:%M' ) return opening_hours.as_opening_hours() def parse(self, response): state_urls = response.xpath('//a[@class="lm-homepage__state"]/@href').extract() for state_url in state_urls: yield scrapy.Request(response.urljoin(state_url), callback=self.parse_state) def parse_state(self, response): city_urls = response.xpath('//a[@class="lm-state__store"]/@href').extract() for city_url in city_urls: yield scrapy.Request(response.urljoin(city_url), callback=self.parse_store) def parse_store(self, response): data = json.loads(response.xpath( '//script[@type="application/ld+json" and contains(text(), "streetAddress")]/text()').extract_first()) properties = { 'name': data["name"], 'ref': data["branchCode"], 'addr_full': data["address"]["streetAddress"], 'city': data["address"]["addressLocality"], 'state': data["address"]["addressRegion"], 'postcode': data["address"]["postalCode"], 'country': data["address"]["addressCountry"], 'phone': data.get("telephone"), 'website': data.get("url") or response.url, 'lat': float(data["geo"]["latitude"]), 'lon': float(data["geo"]["longitude"]), } hours = self.parse_hours(data.get("openingHoursSpecification", [])) if hours: properties["opening_hours"] = hours yield GeojsonPointItem(**properties)
python
# -*- coding: utf-8 -*- # # Copyright (C) 2019 John J. Rofrano <[email protected]> # All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ ZenHub Workspace Based on ZenHub API @ https://github.com/ZenHubIO/API """ import json from .board import Board class Workspace: """ ZenHub Workspace for a repository Example JSON: [ { "name": null, "description": null, "id": "57e2f42c86e6ae285942419d", "repositories": [ 68837948 ] } ] """ def __init__(self, data, repo): self.data = data self.repo = repo def __repr__(self): return '<%s %r>' % (type(self).__name__, self.repo.id) def __str__(self): return '<%s %r>\n' % (type(self).__name__, self.repo.id) + json.dumps(self.data, indent=4) @property def name(self): """ :type: string """ try: return self.data['name'] except KeyError: return None @property def description(self): """ :type: string """ try: return self.data['description'] except KeyError: return None @property def id(self): """ :type: int """ try: return self.data['id'] except KeyError: return None @property def repositories(self): """ :type: list """ try: return self.data['repositories'] except KeyError: return [] def board(self): """ Get ZenHub Board data for a repository (repo_id) within the Workspace (workspace_id) :calls: `GET /p2/workspaces/:workspace_id/repositories/:repo_id/board <https://github.com/ZenHubIO/API#get-a-zenhub-board-for-a-repository>`_ :return: :class:`Board <Board>` object :rtype: :class:`zenhub.Board` """ data = self.repo.zenhub.get(f'/p2/workspaces/{self.id}/repositories/{self.repo.id}/board') if data: return Board(data, self.repo) return None
python
#!/usr/bin/python ''' Created on Apr 19, 2016 @author: Rohan Achar ''' import sys import argparse from spacetime.store_server import FrameServer import cmd import shlex from flask import request from threading import Thread as Parallel class SpacetimeConsole(cmd.Cmd): prompt = 'Spacetime> ' """Command console interpreter for frame.""" def do_quit(self, line): """ quit Exits all applications by calling their shutdown methods. """ shutdown() def do_exit(self, line): """ exit Exits all applications by calling their shutdown methods. """ shutdown() def do_findobjs(self, line): """ findobjs Looks for objects where a given dimension matches a given value for a given set. """ tokens = shlex.split(line) if len(tokens) == 3: type_text = tokens[0] dim = tokens[1] val = tokens[2] if type_text in fs.name2class: tp = fs.name2class[type_text] if hasattr(tp, dim): objs = fs.Store.get(tp) for obj in objs: try: v = getattr(obj, dim) except Exception: continue if str(v) == val: for d in obj: print "%s: %s" % (d, obj[d]) else: print "type %s does not have dimension %s" % (type_text, dim) else: print "could not find type %s" % type_text else: print "usage: findobjs <type> <dimension> <value>" def do_descobj(self, line): """ descobj <type> <id> Given a type and an id, prints all the dimensions and values. Has auto-complete. """ tokens = shlex.split(line) if len(tokens) == 2: type_text = tokens[0] oid = tokens[1] if type_text in fs.name2class: obj = {} try: obj = fs.Store.get_object_state(fs.name2class[type_text], oid) except: print "could not find object with id %s" % oid for dim in obj: print "%s: %s" % (dim, obj[dim]) else: print "could not find type %s" % type_text def complete_descobj(self, text, line, begidx, endidx): tokens = shlex.split(line) if len(tokens) == 1: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES] elif len(tokens) == 2 and text: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES if t.__realname__.startswith(text)] else: if tokens[1] in fs.name2class: if len(tokens) == 2 and not text: completions = [oid for oid in fs.Store.get_ids(fs.name2class[tokens[1]])] elif len(tokens) == 3 and text: completions = [oid for oid in fs.Store.get_ids(fs.name2class[tokens[1]]) if oid.startswith(text)] else: print "\n%s is not a valid type." % tokens[1] return completions def do_objsin(self, type_text): """ objsin <type> Prints the primary key of all objects of a type (accepts auto-complete) """ if type_text in fs.name2class: objs = fs.Store.get(fs.name2class[type_text]) if objs: print "{0:20s}".format("ids") print "=============" for oid in objs: print "{0:20s}".format(oid) print "" else: print "could not find type %s" % type_text def complete_objsin(self, text, line, begidx, endidx): if not text: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES] else: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES if t.__realname__.startswith(text)] return completions def do_countobjsin(self, type_text): """ objsin <type> Prints the primary key of all objects of a type (accepts auto-complete) """ if type_text in fs.name2class: objs = fs.Store.get(fs.name2class[type_text]) if objs: print "=============" print "Number of objects in %s is %d" % (type_text, len(objs)) print "" else: print "could not find type %s" % type_text def complete_countobjsin(self, text, line, begidx, endidx): if not text: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES] else: completions = [t.__realname__ for t in fs.DATAMODEL_TYPES if t.__realname__.startswith(text)] return completions def complete_list(self, text, line, begidx, endidx): return ['sets','apps'] def do_list(self, line): """ list ['sets','apps'] list accepts one of two arguments: * 'sets' prints all pcc sets tracked by the server * 'apps' prints the name of all applications registered with the server """ if line == "sets": for t in fs.DATAMODEL_TYPES: print "{0:60s}{1:s}".format(t.__realname__, t.__module__) elif line == "apps": all_apps = fs.Store.get_app_list() for app in all_apps: print app else: print line def do_clear(self, type_text): """ clear [<type>, '!all'] Deletes all objects of the type passed. If '!all' is passed, all objects of all types are cleared. """ if type_text: if type_text == "!all": fs.Store.clear() print "cleared all objects in store..." else: try: fs.Store.clear(fs.name2class[type_text]) print "cleared all objects of type %s" % type_text except: print "could not clear objects of type %s" % type_text def emptyline(self): pass def do_EOF(self, line): shutdown() # TODO: do_pause. Will require telling the applications to pause, to avoid # issues. def shutdown(): print "Shutting down ..." global fs fs.shutdown() sys.exit(0) if __name__== "__main__": parser = argparse.ArgumentParser() parser.add_argument('-p', '--port', type=int, default=10000, help='Port where the server will listen (default: 12000)') parser.add_argument('-P', '--profile', action='store_true', help='Enable profiling on store server.') parser.add_argument('-d', '--debug', action='store_true', help='Debug on') parser.add_argument('-e', '--external', action='store_true', help='Make this server externally accessible') parser.add_argument('-w', '--watchdog', action='store_true', help='Starts the server with thes slack/github watchdog') parser.add_argument('-t', '--timeout', type=int, default=0, help='Timeout in seconds for the server to consider a client disconnected.') parser.add_argument('-c', '--clearempty', action='store_true', default=False, help='Clears the dataframes when all simulations leave.') args = parser.parse_args() global fs fs = FrameServer(args.port, args.debug, args.external, args.timeout, args.clearempty) p = Parallel(target = fs.run, args = (args.profile,)) p.daemon = True p.start() if args.watchdog: try: from slack_watchdog import start_watchdog start_watchdog(fs) except: print "error starting watchdog." raise SpacetimeConsole().cmdloop()
python
import dydra ## # Represents a Dydra.com RDF repository. # # @see http://docs.dydra.com/sdk/python class Repository(dydra.Resource): """Represents a Dydra.com RDF repository.""" ## # (Attribute) The repository name. name = None ## # @param name A valid repository name. def __init__(self, name, **kwargs): self.name = str(name) super(Repository, self).__init__(self.name, **kwargs) ## # @return A string representation of this object. def __repr__(self): return "dydra.Repository('%s')" % (self.name) ## # @return The number of statements in this repository. def __len__(self): return self.count() ## # Creates this repository on Dydra.com. # # @return A pending operation. def create(self): """Creates this repository on Dydra.com.""" return dydra.Operation(self.client.call('repository.create', self.name), client=self.client) ## # Destroys this repository from Dydra.com. # # @return A pending operation. def destroy(self): """Destroys this repository from Dydra.com.""" return dydra.Operation(self.client.call('repository.destroy', self.name), client=self.client) ## # Returns the number of RDF statements in this repository. # # @return A positive integer. def count(self): """Returns the number of RDF statements in this repository.""" return self.client.call('repository.count', self.name) ## # Deletes all data in this repository. # # @return A pending operation. def clear(self): """Deletes all data in this repository.""" return dydra.Operation(self.client.call('repository.clear', self.name), client=self.client) ## # Imports data from the given URL into this repository. # # @param url A valid URL string. # @return A pending operation. def import_from_url(self, url, **kwargs): """Imports data from the given URL into this repository.""" url, context, base_uri = str(url), '', '' if kwargs.has_key('context') and kwargs['context']: context = str(kwargs['context']) if kwargs.has_key('base_uri') and kwargs['base_uri']: base_uri = str(kwargs['base_uri']) return dydra.Operation(self.client.call('repository.import', self.name, url, context, base_uri), client=self.client)
python
import cv2 import numpy as np src = cv2.imread('data/src/lena.jpg') mask = np.zeros_like(src) print(mask.shape) # (225, 400, 3) print(mask.dtype) # uint8 cv2.rectangle(mask, (50, 50), (100, 200), (255, 255, 255), thickness=-1) cv2.circle(mask, (200, 100), 50, (255, 255, 255), thickness=-1) cv2.fillConvexPoly(mask, np.array([[330, 50], [300, 200], [360, 150]]), (255, 255, 255)) cv2.imwrite('data/dst/opencv_draw_mask.jpg', mask) # True # ![](data/dst/opencv_draw_mask.jpg) mask_blur = cv2.GaussianBlur(mask, (51, 51), 0) cv2.imwrite('data/dst/opencv_draw_mask_blur.jpg', mask_blur) # True # ![](data/dst/opencv_draw_mask_blur.jpg) dst = src * (mask_blur / 255) cv2.imwrite('data/dst/opencv_draw_mask_blur_result.jpg', dst) # True # ![](data/dst/opencv_draw_mask_blur_result.jpg)
python
"""Base test class for DNS authenticators.""" import configobj import josepy as jose import mock import six from acme import challenges from certbot import achallenges from certbot.compat import filesystem from certbot.tests import acme_util from certbot.tests import util as test_util DOMAIN = 'example.com' KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) class BaseAuthenticatorTest(object): """ A base test class to reduce duplication between test code for DNS Authenticator Plugins. Assumes: * That subclasses also subclass unittest.TestCase * That the authenticator is stored as self.auth """ achall = achallenges.KeyAuthorizationAnnotatedChallenge( challb=acme_util.DNS01, domain=DOMAIN, account_key=KEY) def test_more_info(self): self.assertTrue(isinstance(self.auth.more_info(), six.string_types)) # pylint: disable=no-member def test_get_chall_pref(self): self.assertEqual(self.auth.get_chall_pref(None), [challenges.DNS01]) # pylint: disable=no-member def test_parser_arguments(self): m = mock.MagicMock() self.auth.add_parser_arguments(m) # pylint: disable=no-member m.assert_any_call('propagation-seconds', type=int, default=mock.ANY, help=mock.ANY) def write(values, path): """Write the specified values to a config file. :param dict values: A map of values to write. :param str path: Where to write the values. """ config = configobj.ConfigObj() for key in values: config[key] = values[key] with open(path, "wb") as f: config.write(outfile=f) filesystem.chmod(path, 0o600)
python
import os os.environ["CUDA_VISIBLE_DEVICES"] = "0" from torch import optim from torch.utils.data import DataLoader import torch from dataloader import * from model import * from metric import * import soundfile as sf from semetrics import * test_clean = "data/test_clean/" test_noisy = "data/test_noisy/" data_augment = 'Specmix' if __name__ == "__main__" : testset = SignalDataset(test_clean, test_noisy, training=False) testloader = DataLoader(testset,batch_size=1, shuffle=False, num_workers=0, collate_fn=collate_fn) model = Network() model.to('cuda') criterion = nn.MSELoss().to('cuda') state = torch.load("model_{}/model.pth".format(data_augment)) model.load_state_dict(state['model']) epoch_loss = 0. epoch_pesq = 0. epoch_csig = 0. epoch_cbak = 0. epoch_covl = 0. epoch_ssnr = 0. epoch_pesq_noisy = 0. print("Evaluate start") model.eval() idx = 0 with torch.no_grad() : for iter, (clean, noisy, clean_spec, noisy_spec, length) in enumerate(testloader) : mask, output = model(noisy_spec) #plot_train(clean_spec[0], output[0,:,:,:], noisy_spec[0]) clean = clean_spec.permute(0, 2, 3, 1) output = output.permute(0, 2, 3, 1) noisy = noisy_spec.permute(0, 2, 3, 1) gt = get_wav(clean.squeeze(0).cpu().numpy(), length=length[0])[np.newaxis, :] pred = get_wav(output.squeeze(0).cpu().numpy(), length=length[0])[np.newaxis, :] noisy_gt = get_wav(noisy.squeeze(0).cpu().numpy(), length=length[0])[np.newaxis, :] if not os.path.isdir("eval/test_{}/clean/".format(data_augment)) : os.makedirs("eval/test_{}/clean/".format(data_augment)) if not os.path.isdir("eval/test_{}/estimated/".format(data_augment)) : os.makedirs("eval/test_{}/estimated/".format(data_augment)) if not os.path.isdir("eval/test_{}/noisy/".format(data_augment)): os.makedirs("eval/test_{}/noisy/".format(data_augment)) for i in range(len(gt)) : gt[i] = np.clip(gt[i], -1, 1) pred[i] = np.clip(pred[i], -1, 1) noisy_gt[i] = np.clip(noisy_gt[i], -1, 1) sf.write("eval/test_{}/clean/{}.wav".format(data_augment, idx), gt[i], 16000) sf.write("eval/test_{}/estimated/{}.wav".format(data_augment, idx), pred[i], 16000) sf.write("eval/test_{}/noisy/{}.wav".format(data_augment, idx), noisy_gt[i], 16000) pesq, csig, cbak, covl, ssnr = composite("eval/test_{}/clean/{}.wav".format(data_augment,idx), "eval/test_{}/estimated/{}.wav".format(data_augment,idx)) #pesq_noisy, csig_noisy, cbak_noisy, covl_noisy, ssnr_noisy = composite("eval/clean/{}.wav".format(idx), # "eval/noisy/{}.wav".format(idx)) print(idx) print('estimated : ', pesq, csig, cbak, covl, ssnr) #print('noisy : ',pesq_noisy, csig_noisy, cbak_noisy, covl_noisy, ssnr_noisy) epoch_pesq += pesq epoch_csig += csig epoch_cbak += cbak epoch_covl += covl epoch_ssnr += ssnr idx += 1 #plot_data(clean[i], mask[i], noisy[i]) epoch_pesq /= idx epoch_csig /= idx epoch_cbak /= idx epoch_covl /= idx epoch_ssnr /= idx print("test epoch pesq : %f csig : %f cbak : %f covl : %f ssnr : %f"%(epoch_pesq, epoch_csig, epoch_cbak,epoch_covl, epoch_ssnr))
python
#!venv/bin/python3 import logging from logging.handlers import RotatingFileHandler import sync_mongo from sync_mongo import SyncMongo __level = logging.DEBUG logger = logging.getLogger(__name__) uri = "repl1/localhost:27017,localhost:27018,localhost:27019" def setup_logging(): FORMAT='%(asctime)s %(levelname)s:%(message)s' logging.basicConfig(format=FORMAT, level=__level) logger = logging.getLogger() handler = RotatingFileHandler('main.log', maxBytes=2*1024*1024, backupCount=4) handler.setLevel(__level) handler.setFormatter(logging.Formatter(FORMAT)) logger.addHandler(handler) setup_logging() logger.info("Starting") SyncMongo(uri, "superheroesdb", "superheroes", "superclone" ).sync() logger.info("completed")
python
""" Bilby ===== Bilby: a user-friendly Bayesian inference library. The aim of bilby is to provide a user-friendly interface to perform parameter estimation. It is primarily designed and built for inference of compact binary coalescence events in interferometric data, but it can also be used for more general problems. The code, and many examples are hosted at https://git.ligo.org/lscsoft/bilby. For installation instructions see https://lscsoft.docs.ligo.org/bilby/installation.html. """ import sys from . import core, gw, hyper from .core import utils, likelihood, prior, result, sampler from .core.sampler import run_sampler from .core.likelihood import Likelihood __version__ = utils.get_version_information() if sys.version_info < (3,): raise ImportError( """You are running bilby >= 0.6.4 on Python 2 Bilby 0.6.4 and above are no longer compatible with Python 2, and you still ended up with this version installed. That's unfortunate; sorry about that. It should not have happened. Make sure you have pip >= 9.0 to avoid this kind of issue, as well as setuptools >= 24.2: $ pip install pip setuptools --upgrade Your choices: - Upgrade to Python 3. - Install an older version of bilby: $ pip install 'bilby<0.6.4' """)
python
from discord import Color, Embed, Member, Object from discord.ext import commands from discord.ext.commands import Context as CommandContext PREMIUM_RULESBOT = 488367350274326528 ACTIVE_PATREON = 488774886043680769 class PremiumCog(object): def __init__(self, bot: commands.Bot): self.bot: commands.Bot = bot async def on_member_update(self, before: Member, after: Member): if after.guild.id != 385848724628439062: return if any(role.id == ACTIVE_PATREON for role in after.roles) and not any( role.id == PREMIUM_RULESBOT for role in after.roles): await after.add_roles(Object(id=PREMIUM_RULESBOT), reason="Patreon") @commands.command() async def premium(self, ctx: CommandContext): await ctx.send( embed=Embed( color=Color.green(), description="Hey, cool that you think about to go premium! If you go premium you would support the " "developer and the moderators. Also you would help us to cover our costs ;) But what " "would you get?\n__**What will you get?**__\n• change footer text\n• change embed " "color\n• you can sign up to an beta from the bot to test new features\n• faster " "support\n• exclusive textchannels\n__**Where to buy?**__\nYou could buy it on Patreon [" "here](https://www.patreon.com/TheBotDev), but other then normally with patreon this is " "an **one time payment** so you dont need to pay monthly for staying premium!")) def setup(bot: commands.Bot): bot.add_cog(PremiumCog(bot))
python
"""Downloads the Forge MDK""" import os import zipfile import shutil import requests from globals import * import logger import modfile def download(): """Downloads and extracts MDK""" prefix = 'https://files.minecraftforge.net/maven/net/minecraftforge/forge/' version = modfile.get('minecraft') + '-' + modfile.get('forge') url = prefix + version + '/forge-' + version + '-mdk.zip' zip_output = 'output.zip' failmsg = f'Failed to download Forge MDK version "{version}". ' # Download MDK req = None logger.log(f'Downloading Forge MDK version {version}...') try: req = requests.get(url, allow_redirects=True) except ConnectionError as err: logger.error(err, suppress=True) logger.log(failmsg + 'Please try again.') if b"404 Not Found" in req.content: error = failmsg + 'This is most likely due to invalid configuration. Please try again.' logger.error(error, suppress=True) raise FileNotFoundError(error) with open(zip_output, 'wb') as file: file.write(req.content) # Extract MDK logger.log('Extracting downloaded MDK') shutil.rmtree(OUTPUT_FOLDER, ignore_errors=True) with zipfile.ZipFile(zip_output, 'r') as file: file.extractall(OUTPUT_FOLDER) os.remove(zip_output) try: cleanup() except: print("Folder cleanup failed.") def cleanup(): """Cleans up output folder""" # Configure gitfiles # with open(OUTPUT_FOLDER + '.gitattributes', 'w') as file: # file.write('src/generated/**/*.json text eol=lf') os.remove(OUTPUT_FOLDER + '.gitattributes') with open(OUTPUT_FOLDER + '.gitignore', 'w') as file: file.write('.gradle/\nbuild/\nlogs/\nmdk_info/\n') # Move Forge metainfo into folder mdk_info = ['changelog.txt', 'CREDITS.txt', 'README.txt', 'LICENSE.txt'] shutil.rmtree(OUTPUT_FOLDER + 'mdk_info', ignore_errors=True) os.mkdir(OUTPUT_FOLDER + 'mdk_info') for file in mdk_info: os.rename(OUTPUT_FOLDER + file, OUTPUT_FOLDER + 'mdk_info/' + file) os.rename(OUTPUT_FOLDER + 'src/main/java/com/example/examplemod/ExampleMod.java', OUTPUT_FOLDER + 'mdk_info/ExampleMod.java') shutil.rmtree(OUTPUT_FOLDER + 'src/main/java/com') # make setup if __name__ == '__main__': modfile.read() download()
python
# -*- coding: utf-8 -*- from django.db import migrations, models import api.models class Migration(migrations.Migration): dependencies = [ ('api', '0001_initial'), ] operations = [ migrations.AlterField( model_name='app', name='id', field=models.SlugField(max_length=24, unique=True, null=True, validators=[api.models.validate_app_id, api.models.validate_reserved_names]), ), migrations.AlterField( model_name='app', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='build', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='config', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='container', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='key', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='push', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), migrations.AlterField( model_name='release', name='uuid', field=models.UUIDField(serialize=False, verbose_name='UUID', primary_key=True), ), ]
python
''' Date: 2021-08-12 12:24:42 LastEditors: Liuliang LastEditTime: 2021-08-12 18:28:35 Description: ''' from typing import Optional class Node(): def __init__(self, data) -> None: self.data = data self._next = None # class LinkList(): # def __init__(self) -> None: # self._head = None # link = LinkList() # link._head = Node(1) # link._head.next = Node(2) # print(link._head.next.val) n_1 = Node(1) n_2 = Node(2) n_3 = Node(3) n_4 = Node(4) n_5 = Node(5) n_1._next = n_2 n_2._next = n_3 n_3._next = n_4 n_4._next = n_5 def reverse(head: Node) -> Optional[Node]: reversed_head = None current = head while current: # current, reversed_head, reversed_head._next, = current._next, current, reversed_head # current, reversed_head._next, reversed_head, = current._next, reversed_head, current #这个不行 reversed_head, reversed_head._next, current = current, reversed_head,current._next return reversed_head def reverse_cur(head:Node): if head == None or head._next == None: return head else: newhead = reverse_cur(head._next) head._next._next = head head._next = None return newhead def test(head:Node): slow, fast = head, head while fast and fast._next: slow = slow._next fast = fast._next._next if slow == fast: return True return False def merge(l1:Node,l2:Node): if l1 and l2: p1, p2 = l1, l2 fake_head = Node(None) current = fake_head while p1 and p2: if p1.data <= p2.data: current._next = p1 p1 = p1._next else: current._next = p2 p2 = p2._next current = current._next current._next = p1 if p1 else p2 return fake_head._next return l1 or l2 def del_n(head:Node, n:int): current = head count = 0 while current is not None: count += 1 current = current._next count -= n+1 current = head while count>0: count -= 1 current = current._next current._next = current._next._next return head #nums = count - n def del_n_2(head:Node, n:int): fast = head count = 0 while fast and count < n: fast = fast._next count += 1 if not fast and count < n: return head if not fast and count == n: return head._next slow = head while fast._next: fast, slow = fast._next, slow._next slow._next = slow._next._next return head return 0 def print_all(head:Node): nums = [] current = head while current: nums.append(current.data) current = current._next print('->'.join(str(num) for num in nums)) # def find_mid(head:Node): print_all(n_1) m = reverse(n_1) print_all(m) print(test(m)) print(test(n_1)) nums = del_n_2(m,3) print_all(m) # print(n_1.data) # print(n_1._next.data)
python
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ducktape.utils.util import wait_until class JmxMixin(object): """This mixin helps existing service subclasses start JmxTool on their worker nodes and collect jmx stats. A couple things worth noting: - this is not a service in its own right. - we assume the service using JmxMixin also uses KafkaPathResolverMixin """ def __init__(self, num_nodes, jmx_object_names=None, jmx_attributes=None): self.jmx_object_names = jmx_object_names self.jmx_attributes = jmx_attributes or [] self.jmx_port = 9192 self.started = [False] * num_nodes self.jmx_stats = [{} for x in range(num_nodes)] self.maximum_jmx_value = {} # map from object_attribute_name to maximum value observed over time self.average_jmx_value = {} # map from object_attribute_name to average value observed over time def clean_node(self, node): node.account.kill_process("jmx", clean_shutdown=False, allow_fail=True) node.account.ssh("rm -rf /mnt/jmx_tool.log", allow_fail=False) def start_jmx_tool(self, idx, node): if self.started[idx-1] or self.jmx_object_names is None: return # JmxTool is not particularly robust to slow-starting processes. In order to ensure JmxTool doesn't fail if the # process we're trying to monitor takes awhile before listening on the JMX port, wait until we can see that port # listening before even launching JmxTool def check_jmx_port_listening(): return 0 == node.account.ssh("nc -z 127.0.0.1 %d" % self.jmx_port, allow_fail=True) wait_until(check_jmx_port_listening, timeout_sec=30, backoff_sec=.1, err_msg="%s: Never saw JMX port for %s start listening" % (node.account, self)) cmd = "%s kafka.tools.JmxTool " % self.path.script("kafka-run-class.sh", node) cmd += "--reporting-interval 1000 --jmx-url service:jmx:rmi:///jndi/rmi://127.0.0.1:%d/jmxrmi" % self.jmx_port for jmx_object_name in self.jmx_object_names: cmd += " --object-name %s" % jmx_object_name for jmx_attribute in self.jmx_attributes: cmd += " --attributes %s" % jmx_attribute cmd += " | tee -a /mnt/jmx_tool.log" self.logger.debug("Start JmxTool %d command: %s", idx, cmd) jmx_output = node.account.ssh_capture(cmd, allow_fail=False) jmx_output.next() self.started[idx-1] = True def read_jmx_output(self, idx, node): if self.started[idx-1] == False: return object_attribute_names = [] cmd = "cat /mnt/jmx_tool.log" self.logger.debug("Read jmx output %d command: %s", idx, cmd) for line in node.account.ssh_capture(cmd, allow_fail=False): if "time" in line: object_attribute_names = line.strip()[1:-1].split("\",\"")[1:] continue stats = [float(field) for field in line.split(',')] time_sec = int(stats[0]/1000) self.jmx_stats[idx-1][time_sec] = {name : stats[i+1] for i, name in enumerate(object_attribute_names)} # do not calculate average and maximum of jmx stats until we have read output from all nodes if any(len(time_to_stats) == 0 for time_to_stats in self.jmx_stats): return start_time_sec = min([min(time_to_stats.keys()) for time_to_stats in self.jmx_stats]) end_time_sec = max([max(time_to_stats.keys()) for time_to_stats in self.jmx_stats]) for name in object_attribute_names: aggregates_per_time = [] for time_sec in xrange(start_time_sec, end_time_sec + 1): # assume that value is 0 if it is not read by jmx tool at the given time. This is appropriate for metrics such as bandwidth values_per_node = [time_to_stats.get(time_sec, {}).get(name, 0) for time_to_stats in self.jmx_stats] # assume that value is aggregated across nodes by sum. This is appropriate for metrics such as bandwidth aggregates_per_time.append(sum(values_per_node)) self.average_jmx_value[name] = sum(aggregates_per_time) / len(aggregates_per_time) self.maximum_jmx_value[name] = max(aggregates_per_time) def read_jmx_output_all_nodes(self): for node in self.nodes: self.read_jmx_output(self.idx(node), node)
python
class Solution: """ @param arr: an array of integers @return: the length of the shortest possible subsequence of integers that are unordered """ def shortestUnorderedArray(self, arr): # write your code here inc = dec = True for i in range(len(arr) - 1): if arr[i] > arr[i + 1]: inc = False break for i in range(len(arr) - 1): if arr[i] < arr[i + 1]: dec = False break return 0 if inc or dec else 3
python
# -*- coding: utf-8 -*- # Copyright (c) 2018-2020 Niko Sandschneider """Module implementing Signals for communicating with the GUI.""" from functools import wraps import logging from PyQt5.QtCore import QObject, pyqtSignal class Signals(QObject): """Class for signal communication between worker classes and GUI.""" update_progress_bar = pyqtSignal() add_progress_text = pyqtSignal(str, bool) abort_signal = pyqtSignal() get_credentials = pyqtSignal(str) send_credentials = pyqtSignal(str, str) def __init__(self): super().__init__() self.abort = False self.abort_signal.connect(self.abort_evaluation) self.connected = False self.logger = logging.getLogger('easyp2p.p2p_signals.Signals') self.logger.debug('Created Signals instance.') def update_progress(self, func): """Decorator for updating progress text and progress bar.""" @wraps(func) def wrapper(*args, **kwargs): try: if self.abort: raise RuntimeError('Abort by user') result = func(*args, **kwargs) except RuntimeError as err: self.logger.exception('RuntimeError in update_progress') self.add_progress_text.emit(str(err), True) raise PlatformFailedError from err except RuntimeWarning as err: self.logger.warning( 'RuntimeWarning in update_progress', exc_info=True) self.add_progress_text.emit(str(err), True) result = None finally: self.update_progress_bar.emit() return result return wrapper def watch_errors(self, func): """Decorator for emitting error messages to the progress window.""" @wraps(func) def wrapper(*args, **kwargs): try: result = func(*args, **kwargs) except RuntimeError as err: self.logger.exception('RuntimeError in watch_errors.') self.add_progress_text.emit(str(err), True) raise PlatformFailedError from err except RuntimeWarning as err: self.logger.warning(str(err)) self.add_progress_text.emit(str(err), True) result = None return result return wrapper def connect_signals(self, other: 'Signals') -> None: """ Helper method for connecting signals of different classes. Args: other: Signals instance of another class. """ self.logger.debug('Connecting signals.') self.update_progress_bar.connect(other.update_progress_bar) self.add_progress_text.connect(other.add_progress_text) self.get_credentials.connect(other.get_credentials) other.send_credentials.connect(self.send_credentials) self.connected = True self.logger.debug('Connecting signals successful.') def disconnect_signals(self) -> None: """ Disconnect signals. Ignore error if they were not connected or if disconnecting fails. """ if not self.connected: return self.logger.debug('Disconnecting signals.') for signal in [ self.add_progress_text, self.get_credentials, self.update_progress_bar]: try: signal.disconnect() except TypeError: self.logger.exception( 'Disconnecting signal %s failed.', str(signal)) else: self.logger.debug('Signal %s disconnected.', str(signal)) self.connected = False def abort_evaluation(self): """Set the abort flag to True.""" self.logger.debug('Aborting evaluation.') self.abort = True class PlatformFailedError(Exception): """Will be raised if evaluation of a P2P platform fails."""
python
from django.contrib import admin from main.models import StudentRegisterationForm from django.http import HttpResponse import csv def show_email(obj): email = obj.email return '<a href="mailto:%s" target="_blank">%s</a>' % (email, email) show_email.allow_tags = True show_email.short_description = 'Email' def show_github_url(obj): github_url = obj.github_url return '<a href="%s" target="_blank">%s</a>' % (github_url, github_url) show_github_url.allow_tags = True show_github_url.short_description = 'GitHub URL' class registerAdmin(admin.ModelAdmin): list_display = ('name', show_email, 'branch', 'year', show_github_url) actions = ['export_csv'] class Meta: model = StudentRegisterationForm def export_csv(modeladmin, request, queryset): # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="css_export.csv"' writer = csv.writer(response) for obj in queryset: writer.writerow([getattr(obj, f) for f in modeladmin.model._meta.fields]) return response export_csv.short_description = "Export to CSV" admin.site.register(StudentRegisterationForm, registerAdmin)
python
############################################################################ # Copyright ESIEE Paris (2018) # # # # Contributor(s) : Benjamin Perret # # # # Distributed under the terms of the CECILL-B License. # # # # The full license is in the file LICENSE, distributed with this software. # ############################################################################ import higra as hg import numpy as np @hg.argument_helper(hg.CptGridGraph) def graph_4_adjacency_2_khalimsky(graph, edge_weights, shape, add_extra_border=False): """ Create a contour image in the Khalimsky grid from a 4 adjacency edge-weighted graph. :param graph: must be a 4 adjacency 2d graph (Concept :class:`~higra.CptGridGraph`) :param edge_weights: edge weights of the graph :param shape: shape of the graph (deduced from :class:`~higra.CptGridGraph`) :param add_extra_border: if False result size is 2 * shape - 1 and 2 * shape + 1 otherwise :return: a 2d array """ shape = hg.normalize_shape(shape) return hg.cpp._graph_4_adjacency_2_khalimsky(graph, shape, edge_weights, add_extra_border) def khalimsky_2_graph_4_adjacency(khalimsky, extra_border=False): """ Create a 4 adjacency edge-weighted graph from a contour image in the Khalimsky grid. :param khalimsky: a 2d array :param extra_border: if False the shape of the Khalimsky image is 2 * shape - 1 and 2 * shape + 1 otherwise, where shape is the shape of the resulting grid graph :return: a graph (Concept :class:`~higra.CptGridGraph`) and its edge weights """ graph, embedding, edge_weights = hg.cpp._khalimsky_2_graph_4_adjacency(khalimsky, extra_border) hg.CptGridGraph.link(graph, hg.normalize_shape(embedding.shape())) hg.set_attribute(graph, "no_border_vertex_out_degree", 4) return graph, edge_weights def get_4_adjacency_graph(shape): """ Create an explicit undirected 4 adjacency graph of the given shape. :param shape: a pair (height, width) :return: a graph (Concept :class:`~higra.CptGridGraph`) """ shape = hg.normalize_shape(shape) graph = hg.cpp._get_4_adjacency_graph(shape) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", 4) return graph def get_8_adjacency_graph(shape): """ Create an explicit undirected 8 adjacency graph of the given shape. :param shape: a pair (height, width) :return: a graph (Concept :class:`~higra.CptGridGraph`) """ shape = hg.normalize_shape(shape) graph = hg.cpp._get_8_adjacency_graph(shape) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", 8) return graph def get_4_adjacency_implicit_graph(shape): """ Create an implicit undirected 4 adjacency graph of the given shape (edges are not stored). :param shape: a pair (height, width) :return: a graph (Concept :class:`~higra.CptGridGraph`) """ shape = hg.normalize_shape(shape) graph = hg.cpp._get_4_adjacency_implicit_graph(shape) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", 4) return graph def get_8_adjacency_implicit_graph(shape): """ Create an implicit undirected 8 adjacency graph of the given shape (edges are not stored). :param shape: a pair (height, width) :return: a graph (Concept :class:`~higra.CptGridGraph`) """ shape = hg.normalize_shape(shape) graph = hg.cpp._get_8_adjacency_implicit_graph(shape) hg.CptGridGraph.link(graph, shape) hg.set_attribute(graph, "no_border_vertex_out_degree", 8) return graph
python
from collections import defaultdict, namedtuple from numba import njit, jitclass from numba import types import numba import matplotlib.pyplot as plt import matplotlib.patches as patches import numpy as np from matplotlib.ticker import (AutoMinorLocator, MultipleLocator) import argparse parser = argparse.ArgumentParser() parser.add_argument("--no-random", action='store_true', help="Chose 30 points at random instead of manual input") args = parser.parse_args() Line = namedtuple('line', ['slope', 'intercept']) spec = [ ('points', numba.float32[:, :]), ('next_point', numba.int32), ] @jitclass(spec) class Queue(object): def __init__(self, points): self.points = points self.next_point = 0 @property def length(self): points_left = len(self.points) - self.next_point # TODO intersection queue intersections_left = 0 return points_left + intersections_left @property def is_next_point(self): #TODO check if an intersection is closer return True def pop_point(self): current_point = self.points[self.next_point] self.next_point += 1 return current_point def gather_points_manually(): f = plt.figure() points = plt.ginput(-1) plt.close(f) return np.array(points) def gather_points_random(): return np.random.uniform(-1, 1, size=(30, 2)) def dist(points): if points.shape[0] == 1: return np.inf return np.sqrt(((points[0] - points[1])**2).sum()) def sort_points_numpy(points): return points[np.lexsort(points.T)] @njit def solve(queue): while queue.length > 0: if queue.is_next_point: point = queue.pop_point() print(point) if not args.no_random: points = gather_points_random() else: points = gather_points_manually() print(points.shape) points = np.array([[5, 1], [0, 1], [-1, 1] ,[0, 0], [5, 0],[-1, 0]]) points = sort_points_numpy(points) queue = Queue(points.astype('float32')) solve(queue)
python
""" Quickly load ROOT symbols without triggering PyROOT's finalSetup(). The main principle is that appropriate dictionaries first need to be loaded. """ from __future__ import absolute_import import ROOT from .. import log; log = log[__name__] from .module_facade import Facade __all__ = [] root_module = ROOT.module._root if hasattr(root_module, 'LookupCppEntity'): # pragma: no cover lookup_func = 'LookupCppEntity' else: # pragma: no cover lookup_func = 'LookupRootEntity' # Quick's __name__ needs to be the ROOT module for this to be transparent. # The below is one way of obtaining such a function # First determine the ROOT version without triggering PyROOT's finalSetup() Quick = eval('lambda symbol: module._root.{0}(symbol)'.format(lookup_func), ROOT.__dict__) _gSystem = Quick("gSystem") Load = _gSystem.Load # It is not vital to list _all_ symbols in here, just enough that a library # will be loaded by the time it is needed. SYMBOLS = dict( Hist='TH1 TGraph TGraphAsymmErrors', Tree='TCut TTree', Gui='TPad TCanvas', Graf='TLegend TLine TEllipse', Physics='TVector2 TVector3 TLorentzVector TRotation TLorentzRotation', Matrix='TMatrixT', RooStats='RooStats RooMsgService', RooFit='RooFit RooWorkspace', ) # Mapping of symbols to libraries which need to be loaded SYMBOLS_TO_LIB = dict( (sym, lib) for lib, syms in SYMBOLS.items() for sym in syms.split()) # If you encounter problems with particular symbols, add them to this set. SLOW = set("".split()) @Facade(__name__, expose_internal=False) class QuickROOT(object): def __getattr__(self, symbol): if symbol in SLOW: # pragma: no cover log.warning( "Tried to quickly load {0} which is always slow".format(symbol)) lib = SYMBOLS_TO_LIB.get(symbol, None) if lib: # Load() doesn't cost anything if the library is already loaded libname = "lib{0}".format(lib) if libname not in _gSystem.GetLibraries(): regex = "^duplicate entry .* for level 0; ignored$" with log["/ROOT.TEnvRec.ChangeValue"].ignore(regex): if Load(libname) == 0: log.debug("Loaded {0} (required by {1})".format( libname, symbol)) elif lib == 'Gui': # Possibly no X11 forwarding log.debug("Unable to load {0} (required by {1}). " "Putting ROOT in batch mode.".format( libname, symbol)) ROOT.gROOT.SetBatch(True) else: # pragma: no cover raise RuntimeError( "Unable to load {0} (required by {1})".format( libname, symbol)) try: thing = Quick(symbol) except NameError: # pragma: no cover # NameError: global name 'module' is not defined # Python must be exiting... return None if isinstance(thing, root_module.PropertyProxy): # descriptor setattr(self.__class__, symbol, thing) return getattr(self, symbol) # normal member return thing
python
from contextlib import contextmanager from flask_login import UserMixin import markdown from markdown.extensions.toc import TocExtension from sqlalchemy import create_engine from sqlalchemy import Column, Integer, String, DateTime from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base _Base = declarative_base() _engine = create_engine("sqlite:///blog.db") _Session = sessionmaker(bind=_engine, expire_on_commit=False) class Post(_Base): __tablename__ = "posts" rowid = Column(Integer, primary_key=True) title = Column(String) text = Column(String) published = Column(DateTime) last_modified = Column(DateTime) @property def html(self): md_converter = markdown.Markdown(extensions=[ TocExtension(baselevel=2, marker=""), "markdown.extensions.fenced_code", "markdown.extensions.codehilite" ]) return md_converter.convert(self.text) @property def toc(self): md_converter = markdown.Markdown(extensions=[ TocExtension(baselevel=2, marker=""), "markdown.extensions.fenced_code", "markdown.extensions.codehilite" ]) md_converter.convert(self.text) return md_converter.toc def __repr__(self): return "<Post(title='{0}', published='{1}', ...)>".format( self.title, self.published) class Tag(_Base): __tablename__ = "tags" rowid = Column(Integer, primary_key=True) tag = Column(String) def __repr__(self): return "<Tag(tag='{0}')>".format(self.tag) class Post2Tag(_Base): __tablename__ = "posts2tags" rowid = Column(Integer, primary_key=True) post_id = Column(Integer) tag_id = Column(Integer) def __repr__(self): return "<Post2Tag(post_id='{0}', tag_id='{1}')>".format( self.post_id, self.tag_id) class User(_Base, UserMixin): __tablename__ = "users" rowid = Column(Integer, primary_key=True) username = Column(String) pw_hash = Column(String) is_admin = Column(Integer) fullname = Column(String) @property def id(self): return str(self.rowid) def __repr__(self): return "<User(username='{0}', is_admin='{1}')>".format( self.username, self.is_admin) @contextmanager def session_context(): session = _Session() try: yield session session.commit() except: session.rollback() raise finally: session.close()
python
__author__ = 'DafniAntotsiou' import numpy as np from gym import spaces # universal prefix for the different networks def get_il_prefix(): return 'il_' def get_semi_prefix(): return 'semi_' # add auxiliary actions to env observation space for semi network def semi_ob_space(env, semi_size): if semi_size > 0: semi_obs_dim = env.observation_space.shape[0] + semi_size semi_high = np.inf * np.ones(semi_obs_dim) semi_low = -semi_high return spaces.Box(semi_low, semi_high, dtype=np.float64) else: return env.observation_space def reset_envs(semi_dataset, envs, traj_id=-1, random_init=True, add_noise=False): """ resets the environment to a frame in the semi-supervised dataset :param semi_dataset: the dataset :param env: List of environments to be reset. :param traj_id: the id number of the trajectory to initialise the environment to. Is random if < 0. :param random_init: Initialise at the beginning of the trajectory if False, random trajectory frame if True. :param add_noise: add noise to the dataset trajectories during reset :return: the (full_ob of the semi-supervised network, the environment ob, the environment) tuple """ # reset the first env in list and then copy that to the rest of the envs full_ob, ob, set_env = reset_env(semi_dataset, envs[0], traj_id=traj_id, random_init=random_init, add_noise=add_noise) qpos = set_env.env.env.sim.get_state().qpos.copy() qvel = set_env.env.env.sim.get_state().qvel.copy() if hasattr(set_env.env.env, "gs"): semi_dict = set_env.env.env.gs() for env in envs: if env == set_env: continue env.reset() if hasattr(env.env.env, "ss"): if add_noise: env.env.env.ss(semi_dict, add_noise=add_noise) # written like this for debug. TODO: refactor else: env.env.env.ss(semi_dict) elif hasattr(env.env.env, "reset_model_pos"): env.env.env.reset_model_pos(qpos=qpos, qvel=qvel) elif hasattr(env.env.env, "set_state"): env.env.env.set_state(qpos=qpos, qvel=qvel) else: print("Incompatible environment for semi supervision...") exit(1) return full_ob, ob, envs def reset_env(semi_dataset, env, traj_id=-1, random_init=True, add_noise=False): """ resets the environment to a frame in the semi-supervised dataset :param semi_dataset: the dataset :param env: the environment to be reset :param traj_id: the id number of the trajectory to initialise the environment to. Is random if < 0. :param random_init: Initialise at the beginning of the trajectory if False, random trajectory frame if True. :param add_noise: add noise to the semi_dataset trajectories during reset :return: the (full_ob of the semi-supervised network, the environment ob, the environment) tuple """ ob = env.reset() if semi_dataset: # is the retargeting network - reset the env with semi-labels semi_dict = semi_dataset.init_traj_labels(traj_id=traj_id, random_init=random_init) # random initialisation if not semi_dict: print("No available expert semi-labels fam!") exit(1) # reset the environment with the observations from the dataset if hasattr(env.env.env, "ss"): if add_noise: env.env.env.ss(semi_dict, add_noise=add_noise) # written like this for debug. TODO: refactor else: env.env.env.ss(semi_dict) elif hasattr(env.env.env, "reset_model_pos"): env.env.env.reset_model_pos(qpos=semi_dict['qpos'], qvel=semi_dict['qvel']) elif hasattr(env.env.env, "set_state"): env.env.env.set_state(qpos=semi_dict['qpos'], qvel=semi_dict['qvel']) else: print("Incompatible environment for retargeting...") exit(1) full_ob = semi_dict['full_ob'] ob = semi_dict['ob'] else: full_ob = ob return full_ob, ob, env def semi_loss_func(ac, full_ob, semi_dataset, is_relative_actions=False): """ get the L2 loss between generated actions and semi supervised actions :param ac: the semi supervised actions :param full_ob: the full observations of the semi supervised dataset :param semi_dataset: the semi supervised dataset :return: the L2 loss if semi_dataset exists, 0 otherwise """ diff = ac - semi_dataset.full_ob_2_acs(full_ob) if not is_relative_actions else ac return (diff ** 2).mean() if semi_dataset is not None else 0 def relative_2_absolute_action(ac, full_ob, semi_dataset, ac_space): """ get absolute action by adding the relative action to the original from the semi dataset, given environmental action bounds :param ac: the relative actions from the policy :param full_ob: the full set of observations from semi_dataset that produced ac :param semi_dataset: the semi dataset that produced the full_ob :param ac_space: the action space of the environment, to set the action bounds :return: the absolute value of the actions to apply to the environment """ orig_ac = semi_dataset.full_ob_2_acs(full_ob) sigma_ratio = 0.4 # ratio of sigma ac can move orig_ac in both directions sigma = (ac_space.high - ac_space.low) * sigma_ratio ac = np.clip(ac, -sigma, sigma) return np.clip(ac + orig_ac, ac_space.low, ac_space.high)
python
# # Copyright (c) 2018 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import torch import torch.nn as nn class EltwiseAdd(nn.Module): def __init__(self, inplace=False): """Element-wise addition""" super().__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res += t else: for t in input[1:]: res = res + t return res class EltwiseSub(nn.Module): def __init__(self, inplace=False): """Element-wise subtraction""" super().__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res -= t else: for t in input[1:]: res = res - t return res class EltwiseMult(nn.Module): def __init__(self, inplace=False): """Element-wise multiplication""" super().__init__() self.inplace = inplace def forward(self, *input): res = input[0] if self.inplace: for t in input[1:]: res *= t else: for t in input[1:]: res = res * t return res class EltwiseDiv(nn.Module): def __init__(self, inplace=False): """Element-wise division""" super().__init__() self.inplace = inplace def forward(self, x: torch.Tensor, y): if self.inplace: return x.div_(y) return x.div(y)
python