text:
"""
Original code from John Schulman for CS294 Deep Reinforcement Learning Spring 2017
Adapted for CS294-112 Fall 2017 by Abhishek Gupta and Joshua Achiam
Adapted for CS294-112 Fall 2018 by Soroush Nasiriany, Sid Reddy, and Greg Kahn
"""

import inspect
import os
import time
from itertools import count

import gym
import numpy as np
import torch
from torch import nn
from torch.multiprocessing import Process
from torch.nn import functional as F

import logz

class Net(nn.Module):
    def __init__(self, obs_dim, act_dim):
        super(Net, self).__init__()
        self.fc0 = nn.Linear(obs_dim, 128)
        self.fc1 = nn.Linear(128, act_dim)

    def forward(self, x):
        x = x.type_as(self.fc0.bias)
        x = F.relu(self.fc0(x))
        x = self.fc1(x)
        return x

class GaussianPolicy(nn.Module):
    def __init__(self, obs_dim, act_dim):
        super(GaussianPolicy, self).__init__()
        self.mean = Net(obs_dim, act_dim)
        self.std = nn.Parameter(torch.ones(1, act_dim))

    def forward(self, obs):
        mean = torch.tanh(self.mean(obs))
        dist = torch.distributions.Normal(mean, self.std)
        action = dist.sample()
        log_prob = dist.log_prob(action).sum(dim=1, keepdim=True)
        return action, log_prob, torch.zeros((log_prob.size(0), 1))

class CategoricalPolicy(nn.Module):
    def __init__(self, obs_dim, act_dim):
        super(CategoricalPolicy, self).__init__()
        self.network = Net(obs_dim, act_dim)

    def forward(self, obs):
        logit = self.network(obs)
        dist = torch.distributions.Categorical(logits=logit)
        action = dist.sample()
        log_prob = dist.log_prob(action).unsqueeze(-1)
        return action, log_prob, dist.entropy().unsqueeze(-1)

def normalize(array):  # Normalize Numpy array or PyTorch tensor to a standard normal distribution.
    return (array - array.mean()) / (array.std() + 1e-7)

def setup_logger(logdir, locals_):
    logz.configure_output_dir(logdir)
    # Log experimental parameters
    args = inspect.getfullargspec(train_AC)[0]
    params = {k: locals_[k] if k in locals_ else None for k in args}
    logz.save_params(params)

def quick_log(agent, rewards, iteration, start_time, timesteps_this_batch, total_timesteps):
    returns = [re.sum() for re in rewards]
    ep_lengths = [len(re) for re in rewards]
    logz.log_dict({"Time": time.time() - start_time,
                   "Iteration": iteration,
                   "AverageReturn": np.mean(returns),
                   "StdReturn": np.std(returns),
                   "MaxReturn": np.max(returns),
                   "MinReturn": np.min(returns),
                   "EpLenMean": np.mean(ep_lengths),
                   "EpLenStd": np.std(ep_lengths),
                   "TimestepsThisBatch": timesteps_this_batch,
                   "TimestepsSoFar": total_timesteps})
    logz.dump_tabular()
    logz.save_agent(agent)

def make_agent_args(args, env):
    discrete = isinstance(env.action_space, gym.spaces.Discrete)
    return {'n_layers': args['n_layers'],
            'ob_dim': env.observation_space.shape[0],
            'ac_dim': env.action_space.n if discrete else env.action_space.shape[0],
            'discrete': discrete,
            'size': args['size'],
            'learning_rate': args['learning_rate'],
            'num_target_updates': args['num_target_updates'],
            'num_grad_steps_per_target_update': args['num_grad_steps_per_target_update'],
            'device': torch.device("cuda" if torch.cuda.is_available() else "cpu"),
            'animate': args['render'],
            'max_path_length': args['ep_len'] if args['ep_len'] > 0 else env.spec.max_episode_steps,
            'min_timesteps_per_batch': args['batch_size'],
            'gamma': args['discount'],
            'normalize_advantages': not args['dont_normalize_advantages']}

# ============================================================================================#
# Actor Critic
# ============================================================================================#

class Agent(object):
    def __init__(self, args):
        super(Agent, self).__init__()
        self.n_layers = args['n_layers']
        self.ob_dim = args['ob_dim']
        self.ac_dim = args['ac_dim']
        self.discrete = args['discrete']
        self.size = args['size']
        self.learning_rate = args['learning_rate']
        self.num_target_updates = args['num_target_updates']
        self.num_grad_steps_per_target_update = args['num_grad_steps_per_target_update']
        self.device = args['device']
        self.animate = args['animate']
        self.max_path_length = args['max_path_length']
        self.min_timesteps_per_batch = args['min_timesteps_per_batch']
        self.gamma = args['gamma']
        self.normalize_advantages = args['normalize_advantages']

        self.actor = None  # The loss function is computed at self.update_parameters.
        if self.discrete:
            self.actor = CategoricalPolicy(self.ob_dim, self.ac_dim).to(self.device)
        else:
            self.actor = GaussianPolicy(self.ob_dim, self.ac_dim).to(self.device)

        self.optimizer = torch.optim.Adam(self.actor.parameters(), lr=self.learning_rate)

        self.critic_prediction = Net(self.ob_dim, 1).to(self.device)
        self.critic_loss = nn.MSELoss()
        self.critic_optimizer = torch.optim.Adam(self.critic_prediction.parameters(), lr=self.learning_rate)

    def sample_trajectories(self, itr, env):
        # Collect trajectories until we have enough timesteps
        timesteps_this_batch = 0
        obs, acs, next_obs, log_probs, res, terminals = [], [], [], [], [], []
        while timesteps_this_batch <= self.min_timesteps_per_batch:
            animate_this_episode = (len(res) == 0 and (itr % 10 == 0) and self.animate)
            ob, ac, next_ob, log_prob, re, terminal = self.sample_trajectory(env, animate_this_episode)
            obs.append(ob)
            acs.append(ac)
            next_obs.append(next_ob)
            log_probs.append(log_prob)
            res.append(re)
            terminals.append(terminal)
            timesteps_this_batch += len(re)
        return np.concatenate(obs), acs, np.concatenate(next_obs), torch.cat(log_probs).squeeze(1), res, np.concatenate(
            terminals), \
               timesteps_this_batch

    def sample_trajectory(self, env, animate_this_episode):
        ob = env.reset()
        obs, acs, next_obs, log_probs, rewards, terminals = [], [], [], [], [], []
        for steps in count():
            if animate_this_episode:
                env.render()
                time.sleep(0.1)

            ac, log_prob, _ = self.actor(torch.from_numpy(ob).to(self.device).unsqueeze(0))
            if self.discrete:
                ac = ac[0].item()
            else:
                ac = ac[0].detach().cpu().numpy()
            obs.append(ob)
            acs.append(ac)
            log_probs.append(log_prob)
            next_ob, rew, done, _ = env.step(ac)
            next_obs.append(next_ob)
            rewards.append(rew)
            ob = next_ob
            # If the episode ended, the corresponding terminal value is 1
            # otherwise, it is 0
            if done or steps > self.max_path_length:
                terminals.append(1)
                break
            else:
                terminals.append(0)

        return np.array(obs, dtype=np.float32), \
               np.array(acs, dtype=np.float32), \
               np.array(next_obs, dtype=np.float32), \
               torch.cat(log_probs), \
               np.array(rewards, dtype=np.float32), \
               np.array(terminals, dtype=np.float32)

    def estimate_advantage(self, ob_no, next_ob_no, re_n, terminal_n):
        re_n = torch.from_numpy(np.concatenate(re_n)).to(self.device)
        ob_no = torch.from_numpy(ob_no).to(self.device)
        next_ob_no = torch.from_numpy(next_ob_no).to(self.device)
        mask = torch.from_numpy(1 - terminal_n).to(self.device)

        v_prime_n = self.critic_prediction(next_ob_no).reshape(re_n.shape)
        q_n = re_n + self.gamma * v_prime_n * mask
        v_n = self.critic_prediction(ob_no).reshape(re_n.shape)
        adv_n = q_n - v_n

        if self.normalize_advantages:
            adv_n = normalize(adv_n)
        return adv_n.detach()

    def update_critic(self, ob_no, next_ob_no, re_n, terminal_n):
        """
            Update the parameters of the critic.

            let sum_of_path_lengths be the sum of the lengths of the paths sampled from
                Agent.sample_trajectories
            let num_paths be the number of paths sampled from Agent.sample_trajectories

            arguments:
                ob_no: shape: (sum_of_path_lengths, ob_dim)
                next_ob_no: shape: (sum_of_path_lengths, ob_dim). The observation after taking one step forward
                re_n: length: sum_of_path_lengths. Each element in re_n is a scalar containing
                    the reward for each timestep
                terminal_n: length: sum_of_path_lengths. Each element in terminal_n is either 1 if the episode ended
                    at that timestep of 0 if the episode did not end

            returns:
                nothing
        """
        # Use a bootstrapped target values to update the critic
        # Compute the target values r(s, a) + gamma*V(s') by calling the critic to compute V(s')
        # In total, take n=self.num_grad_steps_per_target_update*self.num_target_updates gradient update steps
        # Every self.num_grad_steps_per_target_update steps, recompute the target values
        # by evaluating V(s') on the updated critic

        ob_no = torch.from_numpy(ob_no).to(self.device).unsqueeze(0)
        next_ob_no = torch.from_numpy(next_ob_no).to(self.device).unsqueeze(0)
        re_n = torch.from_numpy(np.concatenate(re_n)).to(self.device)
        mask = torch.from_numpy(1 - terminal_n).to(self.device)

        for _ in range(self.num_target_updates):
            self.critic_prediction.eval()

            v_prime_n = self.critic_prediction(next_ob_no).reshape(re_n.shape)
            target = re_n + self.gamma * v_prime_n * mask
            target = target.detach()

            self.critic_prediction.train()

            for _ in range(self.num_grad_steps_per_target_update):
                prediction = self.critic_prediction(ob_no).reshape(re_n.shape)
                loss = self.critic_loss(input=prediction, target=target)
                self.critic_optimizer.zero_grad()
                loss.backward()
                self.critic_optimizer.step()
        self.critic_prediction.eval()

    def update_actor(self, ob_no, log_prob_na, adv_n):
        """
            Update the parameters of the policy.

            arguments:
                ob_no: shape: (sum_of_path_lengths, ob_dim)
                ac_na: shape: (sum_of_path_lengths).
                adv_n: shape: (sum_of_path_lengths). A single vector for the estimated
                    advantages whose length is the sum of the lengths of the paths

            returns:
                nothing

        """
        self.actor.train()
        self.optimizer.zero_grad()
        loss = -torch.mean(log_prob_na * adv_n)
        loss.backward()
        self.optimizer.step()
        self.actor.eval()

def train_AC(args, logdir, seed):
    start = time.time()
    setup_logger(logdir, locals())

    # Make the gym environment
    env = gym.make(args['env_name'])
    discrete = isinstance(env.action_space, gym.spaces.Discrete)

    env.seed(args['seed'])
    torch.manual_seed(args['seed'])
    np.random.seed(args['seed'])

    agent_args = make_agent_args(args, env)
    agent = Agent(agent_args)  # estimate_return_args

    total_timesteps = 0
    for itr in range(args['n_iter']):
        print("********** Iteration %i ************" % itr)

        ob_no, _, next_ob_no, log_prob_na, re_n, terminal_n, timesteps_this_batch = agent.sample_trajectories(itr, env)

        # (1) update the critic, by calling agent.update_critic
        # (2) use the updated critic to compute the advantage by, calling agent.estimate_advantage
        # (3) use the estimated advantage values to update the actor, by calling agent.update_actor

        agent.update_critic(ob_no, next_ob_no, re_n, terminal_n)
        adv_n = agent.estimate_advantage(ob_no, next_ob_no, re_n, terminal_n)
        agent.update_actor(ob_no, log_prob_na, adv_n)

        # Log diagnostics
        total_timesteps += timesteps_this_batch
        quick_log(agent=agent, rewards=re_n, iteration=itr, start_time=start, timesteps_this_batch=timesteps_this_batch,
                  total_timesteps=total_timesteps)

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('env_name', type=str)
    parser.add_argument('--exp_name', type=str, default='vac')
    parser.add_argument('--render', action='store_true')
    parser.add_argument('--discount', type=float, default=1.0)
    parser.add_argument('--n_iter', '-n', type=int, default=100)
    parser.add_argument('--batch_size', '-b', type=int, default=1000)
    parser.add_argument('--ep_len', '-ep', type=float, default=-1.)
    parser.add_argument('--learning_rate', '-lr', type=float, default=5e-3)
    parser.add_argument('--dont_normalize_advantages', '-dna', action='store_true')
    parser.add_argument('--num_target_updates', '-ntu', type=int, default=10)
    parser.add_argument('--num_grad_steps_per_target_update', '-ngsptu', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--n_experiments', '-e', type=int, default=1)
    parser.add_argument('--n_layers', '-l', type=int, default=2)
    parser.add_argument('--size', '-s', type=int, default=64)
    args = parser.parse_args()

    data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
    args.exp_name = 'ac_' + args.exp_name

    if not (os.path.exists(data_path)):
        os.makedirs(data_path)
    logdir = args.exp_name + '_' + args.env_name + '_' + time.strftime("%d-%m-%Y_%H-%M-%S")
    logdir = os.path.join(data_path, logdir)
    if not (os.path.exists(logdir)):
        os.makedirs(logdir)

    processes = []
    for e in range(args.n_experiments):
        seed = args.seed + 10 * e
        print('Running experiment with seed %d' % seed)
        p = Process(target=train_AC, args=(vars(args), os.path.join(logdir, '%d' % seed), seed))
        p.start()
        processes.append(p)
    for p in processes:
        p.join()

if __name__ == "__main__":
    main()


text:
'''

Generate HTML code for word cloud

'''

import pandas as pd
import logging
import numpy as np
import random
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer

class WordCloud:

    def __init__(self, stopwords=[], use_tfidf=False):

        self.use_tfidf = use_tfidf
        self.data = []
        self.color_choices = ['#b82c2c',
                              '#a55571',
                              '#bc72d0',
                              '#8000FF',
                              '#3498DB',
                              '#FF5733',
                              '#223AE6',
                              '#2ECC71',
                              '#5F6A6A',
                              '#6C22E6',
                              '#CE22E6',
                              '#ACB02E',
                              '#B18904',
                              '#848484',
                              '#04B404',
                              '#5882FA',
                              '#FF0080',
                              '#0489B1',
                              '#FA5858',
                              '#DBA901',
                              '#00b4ff',
                              '#008080',
                              '#003366',
                              '#725394'
                              ]
        self.color_choices = ['#b82c2c',
                              '#a55571',
                              '#bc72d0',
                              '#8000FF',
                              '#3498DB',
                              '#FF5733',
                              '#223AE6',
                              '#2ECC71',
                              '#5F6A6A',
                              '#6C22E6',
                              '#CE22E6',
                              '#ACB02E',
                              '#B18904',
                              '#848484',
                              '#04B404',
                              '#5882FA',
                              '#FF0080',
                              '#0489B1',
                              '#FA5858',
                              '#DBA901',
                              '#00b4ff',
                              '#008080',
                              '#003366',
                              '#725394'
                              ]

        # load a set of stop words
        self.stopwords = stopwords

    def get_color_code(self, score):
        """Get the appropriate color codes."""

        step = 0.05
        current_incremented_score = 0
        idx = 0

        while current_incremented_score < 1:
            if score <= current_incremented_score:
                return self.color_choices[idx]
            idx += 1
            current_incremented_score = current_incremented_score + step

        return self.color_choices[0]

    def get_font_size(self, score: float):
        """Increment scale until score almost equals current_incremented_score."""

        # font size start and increment
        scale = 0.5
        max_scale = 2.5
        scale_step = 0.15

        # score increment
        score_step = 0.05
        current_incremented_score = 0

        while current_incremented_score < 1:

            # increment scale until score almost equals current_incremented_score
            # the larger the score, the more the scale increment
            if score <= current_incremented_score:
                return scale

            current_incremented_score = current_incremented_score + score_step
            scale += scale_step

            #if scale > max_scale:
             #   scale = max_scale

        return scale

    def get_embed_code(self, text_scores: pd.DataFrame = None, text: list = [], topn=100, random_color=True):

        if text_scores is None and len(text) > 0:
            items = self.extract_topn_from_vector(text, topn=topn)
            text_df = pd.DataFrame(items, columns=['words', 'score'])
        elif text_scores is not None:
            text_df = text_scores
            text_df.columns = ['words', 'score']
        else:
            logging.error(
                "There is a problem with your input text. Did you provide any?")
            return

        if random_color:
            random.shuffle(self.color_choices)

        word_cloud_items = []

        html = [
            "

"]
        for idx, row in text_df.iterrows():
            word = row.words.replace(" ", "-")
            scale = self.get_font_size(row.score)
            color_code = self.get_color_code(row.score)
            word_cloud_items.append(
                " {2} ".format(
                    color_code, scale, word))

        random.shuffle(word_cloud_items)
        random.shuffle(word_cloud_items)

        html.extend(word_cloud_items)
        html.append("

")
        return ''.join(html)

    def sort_coo(self, coo_matrix):
        tuples = zip(coo_matrix.col, coo_matrix.data)
        return sorted(tuples, key=lambda x: (x[1], x[0]), reverse=True)

    def get_ranks(self,word_vector):
        """Get normalized tf."""

        max = np.max(word_vector)

        # normalize raw counts
        word_count_vector = np.multiply(word_vector, 1/(max))

        return word_count_vector

    def get_normalized_tf(self, cv: CountVectorizer, text: list):
        """Get normalized tf."""

        big_text = ' '.join(text)
        word_count_vector = cv.fit_transform([big_text])
        max = np.max(word_count_vector)

        # normalize raw counts
        word_count_vector = np.multiply(word_count_vector, 1/(max))

        return word_count_vector

    def get_tfidf_scores(self, cv: CountVectorizer, text: list):
        """Get tfidf values."""

        word_count_vector = cv.fit_transform(text)

        big_text = ' '.join(text)

        # compute word scores
        tfidf_transformer = TfidfTransformer(
            smooth_idf=False, use_idf=True, norm='l2')
        tfidf_transformer.fit(word_count_vector)
        tf_idf_vector = tfidf_transformer.transform(cv.transform([big_text]))

        return tf_idf_vector

    def extract_topn_from_vector(self, text: list, topn=10):
        """Extract keywords based on tf-idf score."""

        # get word count
        cv = CountVectorizer(stop_words=self.stopwords)

        word_scores_vector = None
        if self.use_tfidf:
            word_scores_vector = self.get_tfidf_scores(cv, text)
        else:
            word_scores_vector = self.get_normalized_tf(cv, text)

        #word_scores_vector=self.get_ranks(word_scores_vector)

        # sort the tf-idf vectors by descending order of scores
        sorted_items = self.sort_coo(word_scores_vector.tocoo())
        sorted_items = sorted_items[:topn]

        final_items = []

        # word index and corresponding tf-idf score
        for idx, score in sorted_items:
            final_items.append([cv.get_feature_names()[idx], score])

        return final_items

ext: py

reason: import numpy


text:
#! /home/drcl_yang/anaconda3/envs/py36/bin/python

from pathlib import Path
import sys
import os

FILE = Path(__file__).absolute()
sys.path.append(FILE.parents[0].as_posix())  # add code to path

import numpy as np
import time
import cv2

windowName = 'image'

def ball_tracking(image):

        ball_cand_box = []

        image_ori = image.copy()

        blur = cv2.GaussianBlur(image_ori, (13, 13), 0)

        fgmask = fgbg.apply(blur, None, 0.1)

        fgmask_erode = cv2.erode(fgmask, kernel_erosion_1, iterations = 1) #오픈 연산이아니라 침식으로 바꾸자

        fgmask_dila = cv2.dilate(fgmask_erode,kernel_dilation_2,iterations = 1)

        nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(fgmask_dila, connectivity = 8)

        for i in range(len(stats)):
            x, y, w, h, area = stats[i]

            if area > 3000 : # or area < 500 or aspect > 1.2 or aspect < 0.97 :
                continue
            cv2.rectangle(image_ori, (x, y), (x + w, y + h), (255,0,0), 3)

            x0, y0, x1, y1 = x, y, x+w, y+h

            ball_cand_box.append([x0, y0, x1, y1])

        MOG2_img = cv2.hconcat([fgmask,fgmask_erode,fgmask_dila])

        cv2.imshow("MOG2_img",MOG2_img)

        #cv2.imshow("fgmask",fgmask)
        #cv2.imshow("fgmask_erode",fgmask_erode)
        #cv2.imshow("fgmask_dila",fgmask_dila)

        return image_ori, ball_cand_box

def main(input_video):

    check_id = 0
    ball_id = 0

    cap_main = cv2.VideoCapture(input_video)

    fps = int(cap_main.get(cv2.CAP_PROP_FPS))

    while cap_main.isOpened():
        t1 = time.time()

        ball_list = []

        ret, frame_main = cap_main.read()

        """frame = frame_main[:,320:960,:]
        frame_left = frame_main[0:360,:590,:]
        frame_right = frame_main[360:,50:,:]"""

        frame = cv2.resize(frame_main, dsize=(720, 408 * 2), interpolation=cv2.INTER_LINEAR)
        frame_left = frame_main[0:408,:,:]
        frame_right = frame_main[408:,:,:]

        frame_main = cv2.vconcat([frame_left,frame_right])

        frame_mog2 = frame_main.copy()

        ball_detect_img, ball_cand_box_list = ball_tracking(frame_mog2)  #get ball cand bbox list

        t2 = time.time()

        #cv2.imshow('frame_recode',frame_recode)
        #cv2.imshow('person_tracking_img',person_tracking_img)
        # cv2.imshow('ball_detect_img',ball_detect_img)
        # cv2.imshow('point_image',point_image)

        cv2.imshow('frame_main',frame_main)

        print("FPS : " , 1/(t2-t1))
        key = cv2.waitKey(1)

        if key == 27 :
            cv2.destroyAllWindows()
            break

if __name__ == "__main__":

    video_path = "videos/tennis_video_1.mov"
    main(video_path)

max_stars_repo_name: diddytpq/Tennisball-Tracking-in-Video

max_issues_repo_name: diddytpq/Tennisball-Tracking-in-Video

max_forks_repo_name: diddytpq/Tennisball-Tracking-in-Video


text:
from collections import Counter
import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import json
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def make_cloud(text):
    wordcloud = WordCloud(max_font_size=100, width=1520, height=535).generate(text)
    plt.figure(figsize=(16, 9))
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()

objects = []
performance = []
limit = 15
count = 0
for line in open('master_reducer', 'r'):
    data = line.split(",")
    objects.append(data[0])
    performance.append(int(data[1]))
    count += 1
    if count == limit:
        break

make_cloud(" ".join(objects))

print("Depois >>")

y_pos = np.arange(limit)

plt.bar(y_pos, performance, align='center', alpha=0.2)
plt.xticks(y_pos, objects)
plt.ylabel('Quantidade')
plt.title('Palavras')
plt.show()

max_stars_repo_path: .map-reduce model/plot.py

max_issues_repo_path: .map-reduce model/plot.py

max_forks_repo_path: .map-reduce model/plot.py


text:
import autoarray as aa
import numpy as np
import pytest

def test__sets_up_scaled_alone_grid():

    grid_slim = aa.util.grid_1d.grid_1d_slim_via_shape_slim_from(
        shape_slim=(3,), pixel_scales=(1.0,), sub_size=1
    )

    assert (grid_slim == np.array([-1.0, 0.0, 1.0])).all()

    grid_slim = aa.util.grid_1d.grid_1d_slim_via_shape_slim_from(
        shape_slim=(3,), pixel_scales=(1.0,), sub_size=2, origin=(1.0,)
    )

    assert (grid_slim == np.array([-0.25, 0.25, 0.75, 1.25, 1.75, 2.25])).all()

def test__grid_1d_is_actual_via_via_mask_from():

    mask = np.array([False, True, False, False])

    grid_slim = aa.util.grid_1d.grid_1d_slim_via_mask_from(
        mask_1d=mask, pixel_scales=(3.0,), sub_size=1
    )

    assert (grid_slim == np.array([-4.5, 1.5, 4.5])).all()

    mask = np.array([True, False, True, False])

    grid_slim = aa.util.grid_1d.grid_1d_slim_via_mask_from(
        mask_1d=mask, pixel_scales=(2.0,), sub_size=2, origin=(1.0,)
    )

    assert (grid_slim == np.array([-0.5, 0.5, 3.5, 4.5])).all()

def test__grid_1d_slim_from():

    mask_1d = np.array([False, False, False, False])

    grid_1d_native = np.array([1.0, 2.0, 3.0, 4.0])

    grid_1d_slim = aa.util.grid_1d.grid_1d_slim_from(
        grid_1d_native=grid_1d_native, mask_1d=mask_1d, sub_size=1
    )

    assert (grid_1d_slim == np.array([1.0, 2.0, 3.0, 4.0])).all()

    mask_1d = np.array([True, False, False, True, False, False])

    grid_1d_native = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])

    grid_1d_slim = aa.util.grid_1d.grid_1d_slim_from(
        grid_1d_native=grid_1d_native, mask_1d=mask_1d, sub_size=1
    )

    assert (grid_1d_slim == np.array([2.0, 3.0, 5.0, 6.0])).all()

    mask_1d = np.array([True, True, False, True, False, False])

    grid_1d_native = np.array(
        [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
    )

    grid_1d_slim = aa.util.grid_1d.grid_1d_slim_from(
        grid_1d_native=grid_1d_native, mask_1d=mask_1d, sub_size=2
    )

    assert (grid_1d_slim == np.array([5.0, 6.0, 9.0, 10.0, 11.0, 12.0])).all()

def test__grid_1d_native_from():

    mask_1d = np.array([False, False, False, False])

    grid_1d_slim = np.array([1.0, 2.0, 3.0, 4.0])

    grid_1d_native = aa.util.grid_1d.grid_1d_native_from(
        grid_1d_slim=grid_1d_slim, mask_1d=mask_1d, sub_size=1
    )

    assert (grid_1d_native == np.array([1.0, 2.0, 3.0, 4.0])).all()

    mask_1d = np.array([False, False, True, True, False, False])

    grid_1d_slim = np.array([1.0, 2.0, 3.0, 4.0])

    grid_1d_native = aa.util.grid_1d.grid_1d_native_from(
        grid_1d_slim=grid_1d_slim, mask_1d=mask_1d, sub_size=1
    )

    assert (grid_1d_native == np.array([1.0, 2.0, 0.0, 0.0, 3.0, 4.0])).all()

    mask_1d = np.array([True, False, False, True, False, False])

    grid_1d_slim = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])

    grid_1d_native = aa.util.grid_1d.grid_1d_native_from(
        grid_1d_slim=grid_1d_slim, mask_1d=mask_1d, sub_size=2
    )

    assert (
        grid_1d_native
        == np.array([0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 5.0, 6.0, 7.0, 8.0])
    ).all()


text:
import urllib.parse as url
import http.server
import os.path as path
import ujson as json
import numpy as np
import scipy.sparse

import time

thisdir = path.dirname(path.realpath(__file__))

class npzToObj:
    def __init__(self, npz):
        """
        npz are lazyloaded, this preloads it loads
        """
        for name in npz.files:
            setattr(self, name, npz[name])

class GraphServer:
    def __init__(self, clusters, distance, connectivity, nodes, verbose=False):
        self._verbose = verbose
        if (self._verbose): print("Initializing graph server")

        self._clusters = npzToObj(clusters)
        self._distance = scipy.sparse.csr_matrix(distance)
        self._connectivity = scipy.sparse.csr_matrix(connectivity)
        self._raw_nodes = nodes
        self._nodes = [[node['title'], node['website'], node['id']] for node in nodes]

        # Create a http server
        if (self._verbose): print("\tCreating http server")
        self._server = http.server.HTTPServer(('127.0.0.1', 8000), GraphServer.Responder)
        self._server._owner = self

    def listen(self):
        if (self._verbose): print("Server listening on http://127.0.0.1:8000")
        self._server.serve_forever()

    def fetch_article(self, id):
        return self._raw_nodes[id]

    def fetch_compare(self, a, b):
        return {
            "connecitivity": bool(self._connectivity[min(a, b), max(a, b)]),
            "distance": float(self._distance[min(a, b), max(a, b)])
        }

    def _groups_from_title(self, search):
        if (self._verbose): print("\tSearching for \"%s\"" % (search))
        words = search.split()

        # Construct and execute SQL search query
        match = np.fromiter([
            np.all([(word in node[0]) for word in words])
            for node in self._nodes
        ], dtype='bool')

        # Fetch groups
        groups = set(int(group) for group in self._clusters.node_to_group[match])
        if (self._verbose): print("\tSearch complete, found %d groups" % len(groups))

        return groups

    def _fetch_single_group(self, group_id):
        if (self._verbose): print("\tFetching group %d" % group_id)

        # Create node info object
        nodes = self._clusters.group[group_id, 0:self._clusters.group_size[group_id]]
        node_info = [self._nodes[id] for id in nodes]

        # Create link info object
        if (self._verbose): print("\tBuilding link object")
        mask = np.any(self._clusters.connects_row[:, np.newaxis] == nodes, axis=1)
        if (np.sum(mask) == 0):
            link_info = []
        else:
            info = (
                self._clusters.connects_row[mask],
                self._clusters.connects_col[mask]
            )

            link_info = [
                [int(row), int(col), float(data)]
                for (row, col, data)
                in zip(info[0], info[1], self._distance[info].A1)
            ]

        # Send group info
        return (node_info, link_info)

    def fetch_graph(self, groups):
        if (self._verbose): print("Fetching groups")

        # Validate groups
        max_group_size = int(self._clusters.group.shape[0])
        for group in groups:
            if (group >= max_group_size):
                if (self._verbose): print("\tGroup with id %d do not exists" % group)
                return None

        # Initialize info array
        info = []

        # Fetch group info
        for group in groups:
            (node_info, link_info) = self._fetch_single_group(group)
            info.append({
                "group": group,
                "nodes": node_info,
                "links": link_info
            })

        # Done return result
        return info

    class Responder(http.server.BaseHTTPRequestHandler):
        def __init__(self, *args, **kwargs):
            self._owner = args[2]._owner
            self._verbose = self._owner._verbose
            http.server.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)

        def do_GET(self):
            if self.path         == '/'             : self.index_page()
            elif self.path       == '/details'      : self.details_page()
            elif self.path       == '/d3.js'        : self.d3_script()
            elif self.path       == '/view.js'      : self.view_script()
            elif self.path       == '/style.css'    : self.style_script()
            elif self.path[0:11] == '/graph.json'   : self.graph_data()
            elif self.path[0:13] == '/article.json' : self.article_data()
            elif self.path[0:13] == '/compare.json' : self.compare_data()
            else                                    : self.otherwise()

        def index_page(self):
            self.send_response(200)
            self.send_header('Content-Type', 'text/html; charset=UTF-8')
            self.end_headers()

            f = open(path.join(thisdir, 'public', 'index.html'), 'rb')
            self.wfile.write(f.read())
            f.close()

        def details_page(self):
            self.send_response(200)
            self.send_header('Content-Type', 'text/html; charset=UTF-8')
            self.end_headers()

            f = open(path.join(thisdir, 'public', 'details.html'), 'rb')
            self.wfile.write(f.read())
            f.close()

        def d3_script(self):
            self.send_response(200)
            self.send_header('Content-Type', 'application/javascript; charset=UTF-8')
            self.end_headers()

            f = open(path.join(thisdir, 'public', 'd3.js'), 'rb')
            self.wfile.write(f.read())
            f.close()

        def view_script(self):
            self.send_response(200)
            self.send_header('Content-Type', 'application/javascript; charset=UTF-8')
            self.end_headers()

            f = open(path.join(thisdir, 'public', 'view.js'), 'rb')
            self.wfile.write(f.read())
            f.close()

        def style_script(self):
            self.send_response(200)
            self.send_header('Content-Type', 'text/css; charset=UTF-8')
            self.end_headers()

            f = open(path.join(thisdir, 'public', 'style.css'), 'rb')
            self.wfile.write(f.read())
            f.close()

        def article_data(self):
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=UTF-8')
            self.end_headers()

            query = url.parse_qs(url.urlparse(self.path).query)
            data = self._owner.fetch_article(int(query['id'][0]))
            self.wfile.write(bytes(json.dumps(data), 'ASCII'))

        def compare_data(self):
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=UTF-8')
            self.end_headers()

            query = url.parse_qs(url.urlparse(self.path).query)
            data = self._owner.fetch_compare(int(query['a'][0]), int(query['b'][0]))
            self.wfile.write(bytes(json.dumps(data), 'ASCII'))

        def graph_data(self):
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=UTF-8')
            self.end_headers()

            query = url.parse_qs(url.urlparse(self.path).query)

            # Convert input to group list
            if ('title' in query):
                groups = self._owner._groups_from_title(query['title'][0])
            elif ('groups' in query):
                groups = set(int(group) for group in query['groups'][0].split(","))
            else:
                groups = None

            # Fetch nodes and links
            if (groups is None):
                data = None
            else:
                data = self._owner.fetch_graph(groups)

            # Send data
            if (data is None):
                if (self._verbose): print("\tBad query input")
                self.wfile.write(bytes('null', 'ASCII'))
            else:
                if (self._verbose): print("\tSending result")

                self.wfile.write(bytes('[', 'ASCII'))
                for index, group in enumerate(data):
                    self.wfile.write(bytes('{"group":', 'ASCII'))
                    self.wfile.write(bytes(json.dumps(group['group']), 'ASCII'))

                    self.wfile.write(bytes(', "nodes":', 'ASCII'))
                    self.wfile.write(bytes(json.dumps(group['nodes']), 'ASCII'))

                    self.wfile.write(bytes(', "links":', 'ASCII'))
                    self.wfile.write(bytes(json.dumps(group['links']), 'ASCII'))

                    # Write } if last item otherwise write },\n
                    if (index == (len(data) - 1)):
                        self.wfile.write(bytes('}', 'ASCII'))
                    else:
                        self.wfile.write(bytes('},\n', 'ASCII'))
                self.wfile.write(bytes(']', 'ASCII'))

                if (self._verbose): print("\tResult send")

        def otherwise(self):
            self.send_response(404)
            self.send_header('Content-Type', 'text/html; charset=UTF-8')
            self.end_headers()

            self.wfile.write(bytes('
Sorry invalid path (404)
', 'UTF-8'))

max_stars_repo_name: AndreasMadsen/bachelor-code

max_issues_repo_name: AndreasMadsen/bachelor-code

max_forks_repo_name: AndreasMadsen/bachelor-code

reason: import numpy,import scipy


text:
import numpy as np
import os
from PIL import Image
import shutil

from geneticnet.network import Network
from tests.utilities import get_testfiles_path

def test_create_network():
    input_vector = [1, 2, 3]
    network = Network(input_size=3, output_classes=['even', 'odd'])
    result = network.run(input_vector)
    print(result)

max_stars_repo_name: brandonschabell/geneticnet

max_issues_repo_name: brandonschabell/geneticnet

max_forks_repo_name: brandonschabell/geneticnet


text:
''' Shane Demskie CSCI 2961 Lab 7'''

"""
SGBWords() returns an undirected graph over the 5757 5-letter
words in the datafile words_dat.txt.  Two words are connected by an edge
if they differ in one letter, resulting in 14,135 edges. This example
is described in Section 1.1 in Knuth's book [1,2].
References.
----------
[1] Donald E. Knuth,
    "The Stanford GraphBase: A Platform for Combinatorial Computing",
    ACM Press, New York, 1993.
[2] http://www-cs-faculty.stanford.edu/~knuth/sgb.html
"""

__author__ = """Brendt Wohlberg\nAric Hagberg (hagberg@lanl.gov)"""
__date__ = "$Date: 2005-04-01 07:56:04 -0700 (Fri, 01 Apr 2005) $"
__credits__ = """"""
__revision__ = ""

from networkx import *
import re
import sys

__author__ = """"""
__date__ = ""
__credits__ = """"""
__version__ = ""
# $Header$

def _notComment(line):
    return not(line.startswith('*'))

def _wdist(a,b):
    d=abs(len(a)-len(b))

    for k in range(0,len(a)):
        found = False
        if (len(b)) > 0:
            for n in range(0,len(b)):
                if a[k] == b[n]:
                    found = True
                    b = b.replace(b[n], "")
                    break
            if found == False:
                d = d+1
    return d

def words_graph():
    """ Return example graph from the Stanford GraphBase"""

    import sys
    # open file words_dat.txt.gz
    try:
        try:
            import gzip
            fh=gzip.open('words_dat.txt.gz','r')
        except:
            fh=open("words_dat.txt","r")
    except IOError:
        raise "File words_dat.txt not found."

    G = Graph(name="words")
    sys.stderr.write("Loading words_dat.txt: ")
    for line in fh.readlines():
        if line.startswith("*"):
            continue
        w=line[0:5]
        G.add_node(w)
    nwords=number_of_nodes(G)
    words=G.nodes()
    for k in xrange(0,nwords):
        if (k%100==0):
            sys.stderr.softspace=0
            sys.stderr.write(".")
        for l in xrange(k+1,nwords):
            if _wdist(words[k],words[l]) == 1:
                if words[k] == "chaos" and words[l] == "shoes":
                    print "THEIS"
                    break
                G.add_edge(words[k],words[l])
    return G

if __name__ == '__main__':
    from networkx import *
    G=words_graph()

    print "Loaded words_dat.txt containing 5757 five-letter English words."
    print "Two words are connected if they differ in one letter."
    print "graph has %d nodes with %d edges"\
          %(number_of_nodes(G),number_of_edges(G))

    sp=shortest_path(G, 'chaos', 'order')
    print "shortest path between 'chaos' and 'order' is:\n", sp

    print
    sp=shortest_path(G, 'nodes', 'graph')
    print "shortest path between 'nodes' and 'graph' is:\n", sp

    print
    sp=shortest_path(G, 'moron', 'smart')
    print "shortest path between 'moron' and 'smart' is:\n", sp

    print
    sp=shortest_path(G, 'pound', 'marks')
    print "shortest path between 'pound' and 'marks' is:\n", sp

reason: from networkx


text:
import egltest
import numpy
from OpenGL.GLES2 import *
from OpenGL.GLES2 import shaders

@egltest.egltest( api='es2' )
def test_gl( ):
    glClearColor( 1,0,0, 0 )
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT )

    print 'pre-compile error', glGetError()
    shader = shaders.compileProgram(
        shaders.compileShader( '''#version 130
    attribute vec3 position;
    void main() {
        gl_Position = vec4( position, 0 );
    }''', GL_VERTEX_SHADER),
        shaders.compileShader( '''#version 130
    void main() {
        gl_FragColor = vec4( 1,0,0,0 );
    }''', GL_FRAGMENT_SHADER),
    )
    vbo = VBO( array([
        (0,1,0),
        (1,-1,0),
        (-1,-1,0),
    ],dtype='f'))
    position_location = glGetAttribLocation(
        self.shader, 'position'
    )
    stride = 3*4
    with vbo:
        with shader:
            glEnableVertexAttribArray( position_location )
            stride = 3*4
            glVertexAttribPointer(
                position_location,
                3, GL_FLOAT,False, stride, self.vbo
            )
            glDrawArrays( GL_TRIANGLES, 0, 3 )

if __name__ == "__main__":
    test_gl()


text:
import numpy as np
from math import log

# sigmoid activation as per example
def sigmoid_activation(x):
    return 1.0 / (1.0+np.exp(-x))

# inverted sigmoid activation
def inv_sigmoid_activation(x):
    return -1.0 * np.log((1.0-x)/x) if x > 0.0 else 0.0

# corresponding derivative
def sigmoid_prime(x):
    return np.multiply(sigmoid_activation(x), (1-sigmoid_activation(x)))

# loss function
def dot_loss(p, b):
    e = p - b
    esq = e.dot(e.T)

    return np.sum(np.array(esq)), e

# vector of partial derivatives for the output activations
def cost_derivative(out_act, y):
    return out_act-y

# softmax
def softmax(x):
    return np.exp(x) / np.sum(np.exp(x))

# Sample generator
def next_batch(x, y, batchsize):
    for i in np.arange(0, x.shape[0], batchsize):
        yield (x[i:i + batchsize], y[i:i + batchsize])

max_issues_repo_name: konqr/HEPDrone

max_forks_repo_name: konqr/HEPDrone


text:
"""
MIT License

Copyright (c) 2019 TUMCREATE <https://tum-create.edu.sg/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from __future__ import division
import numpy as np
import pandas as pd
import datetime

__author__ = "Sebastian Troitzsch"
__copyright__ = "Copyright 2019, Architecture and Building Systems - ETH Zurich"
__credits__ = ["Sebastian Troitzsch", "Sreepathi Bhargava Krishna"]
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Daren Thomas"
__email__ = "thomas@arch.ethz.ch"
__status__ = "Production"

def main(
        date_and_time_prediction,
        time_step, set_temperature_goal,
        constant_temperature,
        buildings_names,
        system_controls_df,
        occupancy_per_building_cardinal,
        occupancy_per_building_list,
        occupancy_probability_df,
        indoor_comfort_df,
        T_int_cea_dic
):

    # Determine the parameters linked to time
    prediction_horizon = int((date_and_time_prediction[-1] - date_and_time_prediction[0]) / time_step) + 1

    # Define the set temperatures
    (
        center_interval_temperatures_dic,
        set_setback_temperatures_dic,
        setback_boolean_dic,
        heating_boolean,
        cooling_boolean
    ) = get_set_temperatures(
        date_and_time_prediction,
        prediction_horizon,
        buildings_names,
        system_controls_df,
        occupancy_per_building_cardinal,
        occupancy_per_building_list,
        occupancy_probability_df,
        indoor_comfort_df
    )

    if set_temperature_goal == 'follow_cea':
        set_temperatures_dic = {}
        for building in buildings_names:
            set_temperatures_df = pd.DataFrame(
                np.zeros((occupancy_per_building_cardinal[building], prediction_horizon)),
                occupancy_per_building_list[building],
                date_and_time_prediction
            )
            for time in date_and_time_prediction:
                for occupancy in occupancy_per_building_list[building]:
                    string_object_time = datetime.datetime.strftime(time, '%Y-%m-%d %H:%M:%S')
                    set_temperatures_df.loc[occupancy][time] = T_int_cea_dic[building].loc[string_object_time]
            set_temperatures_dic[building] = set_temperatures_df

    elif set_temperature_goal == 'constant_temperature':
        set_temperatures_dic = {}
        for building in buildings_names:
            set_temperatures_df = pd.DataFrame(
                np.zeros((occupancy_per_building_cardinal[building], prediction_horizon)),
                occupancy_per_building_list[building],
                date_and_time_prediction
            )
            for time in date_and_time_prediction:
                for occupancy in occupancy_per_building_list[building]:
                    set_temperatures_df.loc[occupancy][time] = constant_temperature
            set_temperatures_dic[building] = set_temperatures_df

    elif set_temperature_goal == 'set_setback_temperature':
        set_temperatures_dic = set_setback_temperatures_dic

    else:
        raise ValueError('No valid set_temperature_goal defined')

    return (
        prediction_horizon,
        center_interval_temperatures_dic,
        set_setback_temperatures_dic,
        setback_boolean_dic,
        heating_boolean,
        cooling_boolean,
        set_temperatures_dic
    )

def get_set_temperatures(
        date_and_time_prediction,
        prediction_horizon,
        buildings_names,
        system_controls_df,
        occupancy_per_building_cardinal,
        occupancy_per_building_list,
        occupancy_probability_df,
        indoor_comfort_df
):
    center_interval_temperatures_dic = {}
    set_setback_temperatures_dic = {}
    setback_boolean_dic = {}
    type_season = seasons_type(system_controls_df)
    (
        heating_boolean,
        cooling_boolean
    ) = season_all(
        date_and_time_prediction,
        type_season,
        system_controls_df
    )
    for building in buildings_names:
        (center_interval_temperatures_dic[building],
         set_setback_temperatures_dic[building],
         setback_boolean_dic[building]
         ) = set_outputs_one_building(
            building,
            prediction_horizon,
            date_and_time_prediction,
            heating_boolean,
            cooling_boolean,
            occupancy_per_building_cardinal,
            occupancy_per_building_list,
            occupancy_probability_df,
            indoor_comfort_df
        )

    return (
        center_interval_temperatures_dic,
        set_setback_temperatures_dic,
        setback_boolean_dic,
        heating_boolean,
        cooling_boolean
    )

def seasons_type(system_controls_df):
    if (system_controls_df.loc[0]['has_heating_season'] == 1 or str(
            system_controls_df.loc[0]['has_heating_season']).upper() == 'TRUE') and (
            system_controls_df.loc[0]['has_cooling_season'] == 0 or str(
            system_controls_df.loc[0]['has_cooling_season']).upper() == 'FALSE'):
        # Only has a heating season
        type_season = 1
    elif (system_controls_df.loc[0]['has_heating_season'] == 0 or str(
            system_controls_df.loc[0]['has_heating_season']).upper() == 'FALSE') and (
            system_controls_df.loc[0]['has_cooling_season'] == 1 or str(
            system_controls_df.loc[0]['has_cooling_season']).upper() == 'TRUE'):
        # Only has a cooling season
        type_season = 2
    elif (system_controls_df.loc[0]['has_heating_season'] == 1 or str(
            system_controls_df.loc[0]['has_heating_season']).upper() == 'TRUE') \
            and (system_controls_df.loc[0]['has_cooling_season'] == 1 or str(
            system_controls_df.loc[0]['has_cooling_season']).upper() == 'TRUE'):
        # Has both a heating and a cooling season
        if system_controls_df.loc[0]['heating_season_end_month'] < system_controls_df.loc[0][
                'cooling_season_end_month']:  # Comparing end of heating season month and end of cooling season month
            type_season = 3  # North hemisphere type
        elif system_controls_df.loc[0]['heating_season_end_month'] == system_controls_df.loc[0][
                'cooling_season_end_month'] and system_controls_df.loc[0][
                ' heating_season_end_day'] < system_controls_df.loc[0][
                'cooling_season_end_day']:  # Comparing end of heating season day and end of cooling season day
            type_season = 3
        else:
            type_season = 4  # South hemisphere type
    else:
        return "The code is not in line with system_controls.xlsx"
    return type_season

def date_season(time, type_season, system_controls_df):
    # For a given time step, returns the season which it corresponds to
    if type_season == 1:
        season_t = 'heating'
    elif type_season == 2:
        season_t = 'cooling'
    else:
        year_t = time.year
        heating_season_end = datetime.datetime(
            year_t,
            system_controls_df['heating_season_end_month'],
            system_controls_df['heating_season_end_day'],
            23,
            59,
            59,
            999999
        )
        cooling_season_end = datetime.datetime(
            year_t,
            system_controls_df['cooling_season_end_month'],
            system_controls_df['cooling_season_end_day'],
            23,
            59,
            59,
            999999
        )
        if type_season == 3:
            if time <= heating_season_end:
                season_t = 'heating'
            elif time <= cooling_season_end:
                season_t = 'cooling'
            else:
                season_t = 'heating'
        elif type_season == 4:
            if time <= cooling_season_end:
                season_t = 'cooling'
            elif time <= heating_season_end:
                season_t = 'heating'
            else:
                season_t = 'cooling'
        else:
            raise ValueError('Season not defined to "heating" nor "cooling"')

    return season_t

def season_all(date_and_time_prediction, type_season, system_controls_df):
    heating_boolean = {}
    cooling_boolean = {}
    for time in date_and_time_prediction:
        season_t = date_season(time, type_season, system_controls_df)
        if season_t == 'cooling':
            heating_boolean[time] = 0
            cooling_boolean[time] = 1
        elif season_t == 'heating':
            heating_boolean[time] = 1
            cooling_boolean[time] = 0
        else:
            raise ValueError('Season not defined to "heating" nor "cooling"')
    return heating_boolean, cooling_boolean

def set_outputs_one_building(
        building,
        prediction_horizon,
        date_and_time_prediction,
        heating_boolean,
        cooling_boolean,
        occupancy_per_building_cardinal,
        occupancy_per_building_list,
        occupancy_probability_df,
        indoor_comfort_df
):

    center_interval_temperatures_df = pd.DataFrame(
        np.zeros((occupancy_per_building_cardinal[building], prediction_horizon)),
        occupancy_per_building_list[building],
        date_and_time_prediction
    )
    set_setback_temperatures_df = pd.DataFrame(
        np.zeros((occupancy_per_building_cardinal[building], prediction_horizon)),
        occupancy_per_building_list[building],
        date_and_time_prediction
    )
    setback_boolean_df = pd.DataFrame(
        np.zeros((occupancy_per_building_cardinal[building], prediction_horizon)),
        occupancy_per_building_list[building],
        date_and_time_prediction
    )

    for occupancy in occupancy_per_building_list[building]:
        for time in date_and_time_prediction:
            if occupancy_probability_df.loc[occupancy][time] > 0:
                setback_boolean_df.loc[occupancy][time] = 0
                if cooling_boolean[time] == 1:
                    center_interval_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Tcs_set_C']
                    set_setback_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Tcs_set_C']
                elif heating_boolean[time] == 1:
                    center_interval_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Ths_set_C']
                    set_setback_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Ths_set_C']
                else:
                    raise ValueError('Season not defined to "heating" nor "cooling"')
            if occupancy_probability_df.loc[occupancy][time] == 0:
                setback_boolean_df.loc[occupancy][time] = 1
                if cooling_boolean[time] == 1:
                    center_interval_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Tcs_set_C']
                    set_setback_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Tcs_setb_C']
                elif heating_boolean[time] == 1:
                    center_interval_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Ths_set_C']
                    set_setback_temperatures_df.loc[occupancy][time] = indoor_comfort_df.loc[occupancy]['Ths_setb_C']
                else:
                    raise ValueError('Season not defined to "heating" nor "cooling"')

    return (
        center_interval_temperatures_df,
        set_setback_temperatures_df,
        setback_boolean_df
    )

max_stars_repo_name: AlexJew/CityEnergyAnalyst

max_issues_repo_name: AlexJew/CityEnergyAnalyst

max_forks_repo_name: AlexJew/CityEnergyAnalyst


text:
import numpy as np
from aerofiles.util.geo import haversine

class Analyser:
    def __init__(self):
        pass
    # TODO: Add zero division handling everywhere

    def analyse(self, data, path):
        self.legs = []
        self.contest = {}

        # Do we really need to do this?
        time = data['time']
        lon = data['lon']
        lat = data['lat']
        time = data['time']
        alt = data['alt']
        thermals = data['thermals']
        glides = data['glides']
        raw_time = data['raw_time']
        distance = data['distance']
        track_distance = data['track_distance']
        # cumulative distance is calculated over the whole path, not only
        # from scoring start, not a problem because all calculation involve
        # subtraction of cumulative sums, so it equals off
        cum_track_distance = np.cumsum(track_distance)

        # iterate all legs
        for start, stop in zip(path, path[1:]):
            leg = {}
            leg['raw_time'] = raw_time[stop] - raw_time[start]
            # only thermals fully or partly in the current leg are considered
            # and clipped of at the leg start
            leg_thermals = thermals[(thermals[:,1]>start) & (thermals[:,0]<stop)]
            leg_thermals = np.clip(leg_thermals, start, stop)

            leg['thermal_count'] = len(leg_thermals)
            leg['thermal_gain'] = np.sum(
                alt[leg_thermals[:,1]]-alt[leg_thermals[:,0]]
            )
            leg['thermal_time'] = np.sum(
                raw_time[leg_thermals[:,1]]-raw_time[leg_thermals[:,0]]
            )
            leg['thermal_avg'] = leg['thermal_gain'] / leg['thermal_time']
            leg['thermal_percentage'] = leg['thermal_time'] / leg['raw_time']

            # only glides fully or partly in the current leg are considered
            # and clipped of at the leg start
            leg_glides = glides[(glides[:,1]>start) & (glides[:,0]<stop)]
            leg_glides = np.clip(leg_glides, start, stop)

            leg['glide_count'] = len(leg_glides)
            leg['glide_gain'] = np.sum(
                alt[leg_glides[:,1]] -
                alt[leg_glides[:,0]]
            )
            leg['glide_time'] = np.sum(
                raw_time[leg_glides[:,1]] -
                raw_time[leg_glides[:,0]]
            )

            # glide_track is in KM
            leg['glide_distance'] = np.sum(
                cum_track_distance[leg_glides[:,1]] -
                cum_track_distance[leg_glides[:,0]]
            )
            # per definition of L/D, we need the negative sign
            leg['glide_ratio'] = -(leg['glide_track']/leg['glide_gain']) * 1000
            leg['glide_percentage'] = leg['glide_time']/leg['raw_time']

            assert(leg['glide_time']+leg['thermal_time']==leg['raw_time'])
            assert(leg['glide_gain']+leg['thermal_gain']==alt[stop]-alt[start])

            leg['start'] = start
            leg['end'] = stop

            leg['start_time'] = time[start]
            leg['end_time'] = time[stop]

            leg['start_alt'] = alt[start]
            leg['end_alt'] = alt[stop]

            leg['start_point'] = (lon[start], lat[start])
            leg['end_point'] = (lon[stop], lat[stop])

            leg['distance'] = haversine(
                lon[start], lat[start],
                lon[stop], lat[stop],
            )
            leg['speed'] = 3600 * leg['distance'] / leg['raw_time']
            self.legs.append(leg)

        self.contest['distance'] = sum([leg['distance'] for leg in self.legs])
        self.contest['glide_distance'] = sum(
            [leg['glide_track'] for leg in self.legs]
        )
        self.contest['start_time'] = time[path[0]]
        self.contest['end_time'] = time[path[-1]]

        self.contest['raw_time'] = raw_time[path[-1]] - raw_time[path[0]]
        self.contest['speed'] = (
            3600*self.contest['distance'] /
            self.contest['raw_time']
        )
        # We can not sum up over the legs because clipped glides would count twice
        self.contest['glide_count'] = len(
            glides[(glides[:,1]>path[0]) & (glides[:,0]<path[-1])]
        )
        self.contest['glide_gain'] = sum(
            [leg['glide_gain'] for leg in self.legs]
        )
        self.contest['glide_time'] = sum(
            [leg['glide_time'] for leg in self.legs]
        )
        self.contest['glide_track'] = sum(
            [leg['glide_track'] for leg in self.legs]
        )
        self.contest['glide_ratio'] = -(
            self.contest['glide_track'] / self.contest['glide_gain']
        ) * 1000
        self.contest['glide_percentage'] = (
            self.contest['glide_time'] / self.contest['raw_time']
        )
        # We can not sum up over the legs because clipped thermals would count twice
        self.contest['thermal_count'] = len(
            thermals[(thermals[:,1]>path[0]) & (thermals[:,0]<path[-1])]
        )
        self.contest['thermal_gain'] = sum(
            [leg['thermal_gain'] for leg in self.legs]
        )
        self.contest['thermal_time'] = sum(
            [leg['thermal_time'] for leg in self.legs]
        )
        self.contest['thermal_avg'] = (
            self.contest['thermal_gain'] / self.contest['thermal_time']
        )
        self.contest['thermal_percentage'] = (
            self.contest['thermal_time'] / self.contest['raw_time']
        )

        return self.contest, self.legs

    def compute_qnh(self):
        if not (self.fixes and self.flight.takeoff_fix):
            # TODO: AGL information can be used here to infere qnh
            self.flight.qnh = None
            return
        # TODO: Which fixes to take for qnh guessing
        # TOTHINK: More logic needed here, maybe an exta class
        sum = 0
        for fix in self.fixes[0:self.flight.takeoff_fix.index+1]:
            known_alt = fix.ground_alt if fix.ground_alt else fix.gnss_alt
            sum += pr.find_qnh(fix.pressure_alt, known_alt)

        qnh = round(sum / len(self.fixes[0:self.flight.takeoff_fix.index+1]))
        self.flight.qnh = qnh
        for fix in self.fixes:
            fix.qnh_alt = pr.pressure_alt_to_qnh_alt(fix.pressure_alt, qnh)

max_stars_repo_name: Gliderhub/aerofiles

max_issues_repo_name: Gliderhub/aerofiles

max_forks_repo_name: Gliderhub/aerofiles


text:
if __name__ == '__main__':
    if __package__ is None:
        import sys
        from os import path
        sys.path.append(path.dirname(path.dirname(path.dirname(path.abspath(__file__)))))

from Stream import Stream
from Operators import stream_func
from ML import Stream_LearntnuW,
import numpy as np
from sklearn import linear_model

# Parameters
num_features = 1
num_points = 1000

def train_function(x, y, model, window_state):
    regr = linear_model.LinearRegression()
    regr.fit(x, y)
    return regr

def predict_function(x, y, model):
    y_predict = model.predict(x)
    return (x[0], y_predict.flatten().tolist()[0])

def print_stream(y):
    print y

if __name__ == "__main__":

    i = 0
    x = np.zeros((100, 2))

    for i in range(0, 100):
        x[i, 0] = i
        x[i, 1] = 2 * i

    predict_stream = Stream('predict')

    model = Stream_Learn(data_train=x, data_out=predict_stream,
                         train_func=train_function,
                         predict_func=predict_function,
                         min_window_size=2,
                         max_window_size=2, step_size=1,
                         num_features=num_features)
    y = model.run()
    stream_func(inputs=y, f=print_stream, f_type='element', num_outputs=0)

    while i < num_points:
        x_value = np.random.rand(1, num_features) * 2 - 1
        x_b = np.hstack((np.ones((1, 1)), x_value)).transpose()
        values = x_value.tolist()[0]
        predict_stream.extend([tuple(values)])

        i += 1

max_stars_repo_name: AnomalyInc/StreamPy

max_issues_repo_name: StreamPy/StreamPy

max_forks_repo_name: StreamPy/StreamPy


text:
import numpy as np

import bayem

np.random.seed(6174)
xs = np.linspace(0.01, 0.1, 100)

param_true = [7.0, 10.0]
noise_sd = 0.1
data = param_true[1] + xs * param_true[0] + np.random.normal(0, noise_sd, size=xs.shape)

# [1] Define the problem in terms of numpy arrays
def f(x):
    m, c = x
    return c + xs * m - data

def jac(x):
    d_dm = xs
    d_dc = np.ones_like(xs)
    return np.vstack([d_dm, d_dc]).T

n0 = bayem.Gamma.FromSDQuantiles(0.5 * noise_sd, 1.5 * noise_sd)

# [2] Define the problem as list of numpy arrays

def f_list(x):
    return [f(x)]

def jac_list(x):
    return [jac(x)]

n0_list = [n0]

# [3] Define the problem as dict of numpy arrays

def f_dict(x):
    return {"noise": f(x)}

def jac_dict(x):
    return {"noise": jac(x)}

n0_dict = {"noise": n0}

# [4] Define f to return both k and jac
def f_jac(x):
    return f(x), jac(x)

# [5] Define f to return both k and jac as dict
def f_jac_dict(x):
    return f_dict(x), jac_dict(x)

x0 = bayem.MVN([0, 11], [[1 / 7 ** 2, 0], [0, 1 / 3 ** 2]])

def test_provide_everything():
    assert bayem.vba(f, x0, n0, jac).success
    assert bayem.vba(f_list, x0, n0_list, jac_list).success
    assert bayem.vba(f_dict, x0, n0_dict, jac_dict).success
    assert bayem.vba(f_jac, x0, n0, jac=True).success
    assert bayem.vba(f_jac_dict, x0, n0_dict, jac=True).success

def test_no_noise():
    assert bayem.vba(f, x0, jac=jac).success
    assert bayem.vba(f_list, x0, jac=jac_list).success
    assert bayem.vba(f_dict, x0, jac=jac_dict).success
    assert bayem.vba(f_jac, x0, jac=True).success

def test_no_jacobian():
    assert bayem.vba(f, x0, n0).success
    assert bayem.vba(f_list, x0, n0_list).success
    assert bayem.vba(f_dict, x0, n0_dict).success

def test_minimal():
    assert bayem.vba(f, x0).success
    assert bayem.vba(f_list, x0).success
    assert bayem.vba(f_dict, x0).success

def test_returned_noise_type():
    assert isinstance(bayem.vba(f, x0).noise, bayem.Gamma)
    assert isinstance(bayem.vba(f_list, x0).noise, list)
    assert isinstance(bayem.vba(f_dict, x0).noise, dict)

max_stars_repo_name: BAMresearch/bayem

max_issues_repo_name: BAMresearch/bayem

max_forks_repo_name: BAMresearch/bayem


text:
import csv
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

from common.cli import args
from common.filedata import file_data
from common.serialdata import serial_data

matplotlib.use("tkAgg")

# Columns in the serial data
TIME = 0
YAW = 1
VA = 2
VB = 3

time_data = np.array([], np.float32)
yaw_data = np.array([], np.float32)
va_data = np.array([], np.float32)
vb_data = np.array([], np.float32)

plt.ion()
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.set_ylabel('Yaw (°)')
ax1.set_ybound(-30, 30)
ax2.set_xlabel('Time (s)')
ax2.set_ylabel('Speed')
ax2.set_ybound(-300, 300)
yaw, = ax1.plot(time_data, yaw_data, scaley=True)
va, = ax2.plot(time_data, va_data, scaley=True)
vb, = ax2.plot(time_data, vb_data, scaley=True)

def update_line(line, data):
    line.set_xdata(time_data)
    line.set_ydata(data)

def redraw():
    update_line(yaw, yaw_data)
    update_line(va, va_data)
    update_line(vb, vb_data)
    for ax in fig.get_axes():
        ax.relim()
        ax.autoscale_view(scaley=True)
    fig.canvas.draw()
    fig.canvas.flush_events()

def process(data):
    global time_data, yaw_data, va_data, vb_data
    time_data = np.append(time_data, data[TIME] / 1000)
    yaw_data = np.append(yaw_data, data[YAW])
    va_data = np.append(va_data, data[VA])
    vb_data = np.append(vb_data, data[VB])

    if args.output_file:
        with open(args.output_file, "a") as f:
            writer = csv.writer(f)
            writer.writerow(data)

if args.file:
    # Load data from file
    with file_data(args.file) as file:
        for data in file.read():
            process(data)
    redraw()
else:
    # Read data continuously from serial port
    with serial_data() as serial:
        for data in serial.read():
            process(data)
            redraw()

plt.show(block=True)

max_stars_repo_name: rimesc/arduino-serial-logger

max_issues_repo_name: rimesc/arduino-serial-logger

max_forks_repo_name: rimesc/arduino-serial-logger