text:
# -----------------------------------------------------------------------------
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------

# Scripts to generate all the basic plots
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(0.4, 0.4))
mpl.rcParams['axes.linewidth'] = 0.5
mpl.rcParams['xtick.major.size'] = 0.0
mpl.rcParams['ytick.major.size'] = 0.0
d = 0.01

# Basic line plot (color)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
X = np.linspace(0, 10, 100)
Y = 4+2*np.sin(2*X)
ax.plot(X, Y, color="black", linewidth=0.75)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
ax.grid(linewidth=0.125)
plt.savefig("../figures/plot-color.pdf")
fig.clear()

# Basic line plot (linestyle)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
X = np.linspace(0, 10, 100)
Y = 4+2*np.sin(2*X)
ax.plot(X, Y, color="C1", linewidth=0.75, linestyle="--")
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
ax.grid(linewidth=0.125)
plt.savefig("../figures/plot-linestyle.pdf")
fig.clear()

# Basic line plot (linewidth)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
X = np.linspace(0, 10, 100)
Y = 4+2*np.sin(2*X)
ax.plot(X, Y, color="C1", linewidth=1.5)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
ax.grid(linewidth=0.125)
plt.savefig("../figures/plot-linewidth.pdf")
fig.clear()

# Basic line plot (marker)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
X = np.linspace(0, 10, 100)
Y = 4+2*np.sin(2*X)
ax.plot(X, Y, color="C1", linewidth=0.75, marker="o", markevery=5, markersize=2)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
ax.grid(linewidth=0.125)
plt.savefig("../figures/plot-marker.pdf")
fig.clear()

# Basic line plot (multi)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
X = np.linspace(0, 10, 100)
Y1 = 4+2*np.sin(2*X)
Y2 = 4+2*np.cos(2*X)
ax.plot(X, Y1, color="C1", linewidth=0.75)
ax.plot(X, Y2, color="C0", linewidth=0.75)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
ax.grid(linewidth=0.125)
plt.savefig("../figures/plot-multi.pdf")
fig.clear()

# Basic line plot (vsplit)
# -----------------------------------------------------------------------------
ax1 = fig.add_axes([d, d, 1-2*d, .45-d])
ax2 = fig.add_axes([d, .55-d, 1-2*d, .45-d])

X = np.linspace(0, 10, 100)
Y1 = 2+1*np.sin(2*X)
ax1.plot(X, Y1, color="C1", linewidth=0.75)
ax1.set_xlim(0, 8), ax1.set_xticks(np.arange(1, 8)), ax1.set_xticklabels([])
ax1.set_ylim(0, 4), ax1.set_yticks(np.arange(1, 4)), ax1.set_yticklabels([])
ax1.grid(linewidth=0.125)
ax1.tick_params(axis=u'both', which=u'both', length=0)

Y2 = 2+1*np.cos(2*X)
ax2.plot(X, Y2, color="C0", linewidth=0.75)
ax2.set_xlim(0, 8), ax2.set_xticks(np.arange(1, 8)), ax2.set_xticklabels([])
ax2.set_ylim(0, 4), ax2.set_yticks(np.arange(1, 4)), ax2.set_yticklabels([])
ax2.grid(linewidth=0.125)
ax2.tick_params(axis=u'both', which=u'both', length=0)
plt.savefig("../figures/plot-vsplit.pdf")
fig.clear()

# Basic line plot (hsplit)
# -----------------------------------------------------------------------------
ax1 = fig.add_axes([d, d, .45-d, 1-2*d])
ax2 = fig.add_axes([.55-d, d, .45-d, 1-2*d])

X = np.linspace(0, 10, 100)
Y1 = 2+1*np.sin(2*X)
ax1.plot(Y1, X, color="C1", linewidth=0.75)
ax1.set_xlim(0, 4), ax1.set_xticks(np.arange(1, 4)), ax1.set_xticklabels([])
ax1.set_ylim(0, 8), ax1.set_yticks(np.arange(1, 8)), ax1.set_yticklabels([])
ax1.grid(linewidth=0.125)
ax1.tick_params(axis=u'both', which=u'both', length=0)

Y2 = 2+1*np.cos(2*X)
ax2.plot(Y2, X, color="C0", linewidth=0.75)
ax2.set_xlim(0, 4), ax2.set_xticks(np.arange(1, 4)), ax2.set_xticklabels([])
ax2.set_ylim(0, 8), ax2.set_yticks(np.arange(1, 8)), ax2.set_yticklabels([])
ax2.grid(linewidth=0.125)
ax2.tick_params(axis=u'both', which=u'both', length=0)
plt.savefig("../figures/plot-hsplit.pdf")
fig.clear()

# Basic line plot (title)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, d, 1-2*d, .8-2*d])
X = np.linspace(0, 10, 100)
Y = 3+2*np.sin(2*X)
ax.plot(X, Y, color="C1", linewidth=0.75)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
ax.set_ylim(0, 6), ax.set_yticks(np.arange(1, 6))
ax.grid(linewidth=0.125)
ax.text(4, 6.75, "A Sine wave", transform=ax.transData, clip_on=False,
        weight="bold", size=4, ha="center", va="center")
plt.savefig("../figures/plot-title.pdf")
fig.clear()

# Basic line plot (xlabel)
# -----------------------------------------------------------------------------
ax = fig.add_axes([d, .2-d, 1-2*d, .8-2*d])
X = np.linspace(0, 10, 100)
Y = 3+2*np.sin(2*X)
ax.plot(X, Y, color="C1", linewidth=0.75)
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)), ax.set_xticklabels([])
ax.set_ylim(0, 6), ax.set_yticks(np.arange(1, 6)), ax.set_yticklabels([])
ax.grid(linewidth=0.125)
ax.text(4, -1, "Time", transform=ax.transData, clip_on=False,
        size=3.5, ha="center", va="center")
# plt.show()
plt.savefig("../figures/plot-xlabel.pdf")
fig.clear()

max_stars_repo_name: oscargus/cheatsheets

max_issues_repo_name: oscargus/cheatsheets


text:
import numpy as np
from scipy import sparse
from scipy.sparse import linalg

from dolfin.cpp.la import SLEPcEigenSolver

class MySLEPcEigenSolver(SLEPcEigenSolver):

    def __init__(self, A, B, solver='krylov-schur',
                 spectrum='smallest magnitude', problem_type='gen_hermitian'):

        super().__init__(A, B)
        self.parameters['solver'] = solver
        self.parameters['spectrum'] = spectrum
        self.parameters['problem_type'] = problem_type

    def solve(self, n_eig=5):
        super().solve(n_eig)

        return collect_SLEPc_eigenpairs(self)

class ScipySparseEigenSolver:
    '''
    Solves generalized eigenproblem for symmetric matrices.

    Notes:
        Looks to emulate behavior of SLEPcEigenSolver (goal is to use both \
    indistinctively).
    '''

    def __init__(self, A, B, spectrum='smallest magnitude'):
        self.A = self._get_scipy_sparse(A)
        self.B = self._get_scipy_sparse(B)
        self.params = {'spectrum': spectrum}

    def _map_spectrum(self):
        map_dict = {'smallest magnitude': 'SM',
                    'largest magnitude': 'LM', }

        return {'which': map_dict[self.params['spectrum']]}

    def _get_scipy_sparse(self, M):
        return sparse.csr_matrix(M.mat().getValuesCSR()[::-1])

    def solve(self, n_eig=5):
        return linalg.eigsh(self.A, M=self.B, k=n_eig,
                            **self._map_spectrum())

def collect_SLEPc_eigenpairs(solver):
    '''Returns only real part.
    '''

    w, v = [], []
    for i in range(solver.get_number_converged()):
        r, _, rv, _ = solver.get_eigenpair(i)
        w.append(r)
        v.append(rv)

    return np.array(w), np.array(v).T

ext: py

reason: import numpy,from scipy


text:
''' This file is YingLi's request
Input:  two image (not-aligned)
Output: similar score
=========requirement ============
1: face detection & alignment
'''

#====================================== Dependent file ===================================
import os,sys
import dlib
import cv2
import numpy as np
import model_define as models
import torchvision.transforms as transforms
import torch.nn.functional as F
import torch
# model: detection & align
detector = dlib.get_frontal_face_detector()                         # traditional detector
cnn_face_detector = dlib.cnn_face_detection_model_v1('./model/mmod_human_face_detector.dat')   # cnn detector
sp = dlib.shape_predictor('./model/shape_predictor_5_face_landmarks.dat')                           #

# model: feature extraction
resnet18 = './model/resnet18.tar'
#========================================= Function =======================================
def face_detection_alignmen(original_image):
    # Load the image using OpenCV
    bgr_img = cv2.imread(original_image)

    # Convert to RGB since dlib uses RGB images
    img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)

    # first traditional detector for efficient
    dets = detector(img, 1)
    num_faces = len(dets)

    # If there is no face detected in the image
    if num_faces == 0:
        # second use cnn detector
        cnn_dets = cnn_face_detector(img, 1)
        dets = dlib.rectangles()
        dets.extend([d.rect for d in cnn_dets])
        num_faces = len(dets)
        if num_faces == 0:
            print('There is no face in the image')

    # Find the 5 face landmarks we need to do the alignment.
    faces = dlib.full_object_detections()
    for detection in dets:
        faces.append(sp(img, detection))

    image = dlib.get_face_chip(img, faces[0], size=224, padding=0.3)
    cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    # cv2.imshow('annotShow', cv_bgr_img)
    # cv2.waitKey(0)

    return cv_bgr_img

def load_resnet18():

    _structure = models.resnet18(num_classes=8)
    pretrained_state_dict = torch.load(resnet18)['state_dict']
    model_state_dict = _structure.state_dict()

    for key in pretrained_state_dict:
        if '.num_batches_tracked' in key:
            pass
        else:
            model_state_dict[key.replace('module.', '')] = pretrained_state_dict[key]

    _structure.load_state_dict(model_state_dict)
    model = torch.nn.DataParallel(_structure).cuda()

    return model

def expression_feature_extraction(aligned_face):

    # Load model
    model = load_resnet18()
    # numpy2tensor
    transform = transforms.ToTensor()
    tensor_face = transform(aligned_face).unsqueeze(0)
    # feature
    feature, softmax_score = model(tensor_face)

    return feature, softmax_score

def measure_similiar(feature1, feature2):
    feature1 = F.normalize(feature1)
    feature2 = F.normalize(feature2)
    distance = feature1.mm(feature2.t())

    return distance

#========================================== Main Function ===================================
def main(cartoon_dir, realman_dir):

    # image preprocess
    aligned_cartoon = face_detection_alignmen(cartoon_dir)
    aligned_realman = face_detection_alignmen(realman_dir)

    # feature extraction
    feature_cartoon, score_cartoon = expression_feature_extraction(aligned_cartoon)
    feature_realman, score_realman = expression_feature_extraction(aligned_realman)

    # similiar score

    feature_similiar = measure_similiar(feature_cartoon, feature_realman)
    score_similiar   = measure_similiar(score_cartoon, score_realman)
    return feature_similiar, score_similiar

if __name__ == 'main':
    cartoon_dir = '/home/dbmeng/LibMeng/Database/Emotion/EmotiW19/FrameVal/Fear/000405847.avi/0000009.png'
    realman_dir = '/home/dbmeng/LibMeng/Database/Emotion/EmotiW19/FrameVal/Happy/000403327.avi/0000010.png'
    feature_similiar, score_similiar = main(cartoon_dir, realman_dir)

max_stars_repo_name: Open-Debin/Emotion-Similiarity

max_issues_repo_name: Open-Debin/Emotion-Similiarity

max_forks_repo_name: Open-Debin/Emotion-Similiarity

reason: import numpy


text:
# coding=utf-8
# Copyright 2022 The Uncertainty Baselines Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the segmenter ViT model."""
from absl.testing import absltest
from absl.testing import parameterized
import jax
import jax.numpy as jnp
import ml_collections
import uncertainty_baselines as ub

class SegVitBETest(parameterized.TestCase):

  @parameterized.parameters(
      (2, 16, 224, 224, 'vit', 'linear', 1),
      (2, 16, 224, 224, 'vit_be', 'linear', 3),
      (2, 16, 224, 224, 'vit_be', 'linear_be', 3),
  )
  def test_segmenter_be_transformer(self, num_classes, hidden_size, img_h,
                                    img_w, encoder_type, decoder_type,
                                    ens_size):
    # VisionTransformer.
    config = ml_collections.ConfigDict()

    config.num_classes = num_classes

    config.patches = ml_collections.ConfigDict()
    config.patches.size = [4, 4]

    config.backbone_configs = ml_collections.ConfigDict()
    config.backbone_configs.type = encoder_type
    config.backbone_configs.hidden_size = hidden_size
    config.backbone_configs.attention_dropout_rate = 0.
    config.backbone_configs.dropout_rate = 0.
    config.backbone_configs.mlp_dim = 2
    config.backbone_configs.num_heads = 1
    config.backbone_configs.num_layers = 1
    config.backbone_configs.classifier = 'gap'

    config.decoder_configs = ml_collections.ConfigDict()
    config.decoder_configs.type = decoder_type

    # BE params
    config.backbone_configs.ens_size = ens_size
    config.backbone_configs.random_sign_init = -0.5
    config.backbone_configs.be_layers = (0,)

    num_examples = 2
    inputs = jnp.ones([num_examples, img_h, img_w, 3], jnp.float32)
    model = ub.models.SegVitBE(**config)
    key = jax.random.PRNGKey(0)
    variables = model.init(key, inputs, train=False)

    logits, outputs = model.apply(variables, inputs, train=False)

    self.assertEqual(logits.shape,
                     (num_examples * ens_size, img_h, img_w, num_classes))

    self.assertEqual(
        set(outputs.keys()), set(('stem', 'transformed', 'logits')))

if __name__ == '__main__':
  absltest.main()

max_stars_repo_name: dvdzhang/uncertainty-baselines

max_issues_repo_name: dvdzhang/uncertainty-baselines

max_forks_repo_name: dvdzhang/uncertainty-baselines

reason: import jax


text:
"""
 Copyright (c) 2019 Intel Corporation

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
"""

import time
from multiprocessing import Process, Queue, Value

import cv2
import numpy as np

class Visualizer:
    """Class that allows to play video sources with different speed"""

    def __init__(self, trg_fps=60):
        """Constructor"""

        self._trg_time_step = 1. / float(trg_fps)
        self._last_key = Value('i', -1, lock=True)
        self._need_stop = Value('i', False, lock=False)
        self._worker_process = None
        self._tasks = dict()

    def register_window(self, name):
        """Allocates resources for the new window"""

        if self._worker_process is not None and self._worker_process.is_alive():
            raise RuntimeError('Cannot add the window for running visualizer')

        self._tasks[name] = Queue(1)

    def get_key(self):
        """Returns the value of pressed key"""

        with self._last_key.get_lock():
            out_key = self._last_key.value
            self._last_key.value = -1

        return out_key

    def get_queue(self, name):
        if name not in self._tasks:
            raise ValueError('Unknown name of queue: {}'.format(name))

        return self._tasks[name]

    def put_queue(self, frame, name):
        """Adds frame in the queue of the specified window"""

        if name not in self._tasks.keys():
            raise ValueError('Cannot show unregistered window: {}'.format(name))

        self._tasks[name].put(np.copy(frame), True)

    def start(self):
        """Starts internal threads"""

        if self._worker_process is not None and self._worker_process.is_alive():
            return

        if len(self._tasks) == 0:
            raise RuntimeError('Cannot start without registered windows')

        self._need_stop.value = False

        self._worker_process = Process(target=self._worker,
                                       args=(self._tasks, self._last_key,
                                             self._trg_time_step, self._need_stop))
        self._worker_process.daemon = True
        self._worker_process.start()

    def release(self):
        """Stops playing and releases internal storages"""

        if self._worker_process is not None:
            self._need_stop.value = True
            self._worker_process.join()

    @staticmethod
    def _worker(tasks, last_key, trg_time_step, need_stop):
        """Shows new frames in appropriate screens"""

        while not need_stop.value:
            start_time = time.perf_counter()

            for name, frame_queue in tasks.items():
                if not frame_queue.empty():
                    frame = frame_queue.get(True)

                    cv2.imshow(name, frame)

            key = cv2.waitKey(1)
            if key != -1:
                last_key.value = key

            end_time = time.perf_counter()
            elapsed_time = end_time - start_time
            rest_time = trg_time_step - elapsed_time
            if rest_time > 0.0:
                time.sleep(rest_time)


text:
# coding: utf-8
import sys

import scipy as sp
import scipy.sparse

counter = 0
prev = 0

with open('/home/ubuntu/com-friendster.ungraph.txt', 'r') as graph:
    for i, line in enumerate(graph):
        try:
            if line.startswith('#'):
                continue
            src = int(line.split()[0])
            if src != prev:
                prev = src
                counter += 1
                if counter % 100000 == 0:
                    sys.stdout.write('.')
                    sys.stdout.flush()
                if counter == 10000000:
                    print(src)
                    break
        except:
            print('Error occurred when processing line {}.\n{}'.format(i, line))
            raise

reason: import scipy


text:
import multiprocessing
import time
from multiprocessing import Process, Queue

import gym
import numpy
from aivle_gym.agent_env import AgentEnv
from aivle_gym.env_serializer import EnvSerializer
from aivle_gym.judge_env import JudgeEnv

from aivle_grader.abc.agent import Agent
from aivle_grader.evaluator import StepCountEvaluator
from aivle_grader.test_case import ReinforcementLearningTestCase
from aivle_grader.test_suite import TestSuite

class CartPoleEnvSerializer(EnvSerializer):
    def action_to_json(self, action):
        return action

    def json_to_action(self, action_json):
        return action_json

    def observation_to_json(self, obs):
        return obs.tolist()

    def json_to_observation(self, obs_json):
        return numpy.array(obs_json)

    def info_to_json(self, info):
        return info

    def json_to_info(self, info_json):
        return info_json

class CartPoleJudgeEnv(JudgeEnv):
    def __init__(self):
        self.env = gym.make("CartPole-v0")
        super().__init__(
            CartPoleEnvSerializer(),
            self.env.action_space,
            self.env.observation_space,
            self.env.reward_range,
        )

    def step(self, action):
        return self.env.step(action)

    def reset(self):
        return self.env.reset()

    def render(self, mode="human"):
        return self.env.render(mode=mode)

    def close(self):
        self.env.close()

    def seed(self, seed=None):
        self.env.seed(seed)

class CartPoleAgentEnv(AgentEnv):
    def __init__(self, port: int):
        base_env = gym.make("CartPole-v0")
        super().__init__(
            CartPoleEnvSerializer(),
            base_env.action_space,
            base_env.observation_space,
            base_env.reward_range,
            uid=0,
            port=port
        )  # uid can be any int for single-agent agent env

class CartPoleAgent(Agent):
    def step(self, state):
        return gym.spaces.Discrete(2).sample()

    def reset(self):
        pass

def create_agent(**kwargs):
    return CartPoleAgent()

def run_judge(return_queue: Queue):
    judge_env = CartPoleJudgeEnv()
    return_queue.put(judge_env.bind())
    judge_env.start()

def main():
    manager = multiprocessing.Manager()
    return_queue = manager.Queue()
    judge_proc = Process(target=run_judge, args=(return_queue,))
    judge_proc.start()
    port = None
    for _ in range(10):  # wait for up to 10 seconds
        time.sleep(1)
        if not return_queue.empty():
            port = return_queue.get()
            break
    if not isinstance(port, int):
        raise Exception("judge process not properly initialized")
    try:
        n_runs = 10
        env = CartPoleAgentEnv(port=port)
        evaluator = StepCountEvaluator()
        seeds = [2333 for _ in range(n_runs)]
        test_case = ReinforcementLearningTestCase(
            t_max=10000,
            env=env,
            evaluator=evaluator,
            agent_init={},
            seeds=seeds,
            case_id=0,
            time_limit=3600,
            n_runs=n_runs,
        )
        test_suite = TestSuite(suite_id="cart_pole_test", cases=[test_case])
        res = test_suite.run(create_agent)
        print(res)
    finally:
        judge_proc.terminate()

if __name__ == "__main__":
    main()

max_stars_repo_name: edu-ai/aivle-grader

max_issues_repo_name: edu-ai/aivle-grader

max_forks_repo_name: edu-ai/aivle-grader


text:
import numpy as np
from brainlit.napari_viterbrain.viterbrain_plugin import (
    napari_get_reader,
    comp_trace,
)
from brainlit.algorithms.connect_fragments.tests.test_viterbrain import create_vb
import pickle

vb = create_vb()
vb.compute_all_costs_dist(vb.frag_frag_dist, vb.frag_soma_dist)
vb.compute_all_costs_int()

def test_reader(tmp_path):
    my_test_file = str(tmp_path / "viterbrain.pickle")
    with open(my_test_file, "wb") as handle:
        pickle.dump(vb, handle)

    # try to read it back in
    reader = napari_get_reader(my_test_file)
    assert callable(reader)

    # make sure we're delivering the right format
    layer_data_list = reader(my_test_file)
    assert isinstance(layer_data_list, list) and len(layer_data_list) > 0
    layer_data_tuple = layer_data_list[0]
    assert isinstance(layer_data_tuple, tuple) and len(layer_data_tuple) > 0

    reader = napari_get_reader("fake.file")
    assert reader is None

# def test_example_magic_widget(make_napari_viewer, tmp_path):
#     my_test_file = str(tmp_path / "viterbrain.pickle")
#     with open(my_test_file, "wb") as handle:
#         pickle.dump(vb, handle)

#     viewer = make_napari_viewer()

#     start_layernum = len(list(viewer.layers))

#     my_widget = comp_trace()
#     my_widget(v=viewer, start_comp=1, end_comp=5, filename=my_test_file)

#     # test that a layer was added
#     assert len(list(viewer.layers)) == start_layernum + 1

max_stars_repo_name: neurodata/mouselight

max_issues_repo_name: neurodata/mouselight

max_forks_repo_name: neurodata/mouselight


text:
# -*- coding: utf-8 -*-
import os
import cv2
from scipy import misc
from PIL import Image
import numpy as np

# path changed for testing
# dataset_dir = 'datasets'
dataset_dir = 'datasets_'
dataset = 'data_crop_512_jpg'
train_dir = 'train'
test_dir = 'val_test'
val_dir = 'val'
resize_to = 256
scale = 4

if not os.path.exists(os.path.join(dataset_dir, train_dir)):
    os.mkdir(os.path.join(dataset_dir, train_dir))
if not os.path.exists(os.path.join(dataset_dir, test_dir)):
    os.mkdir(os.path.join(dataset_dir, test_dir))
if not os.path.exists(os.path.join(dataset_dir, val_dir)):
    os.mkdir(os.path.join(dataset_dir, val_dir))

fileList = os.listdir(os.path.join(dataset_dir, dataset))
for index, file in enumerate(fileList):
    imgPath = os.path.join(dataset_dir, dataset, file)
    if os.path.isdir(imgPath):
        continue
    print("procesing " + file + " " + str(index+1) + '/' + str(len(fileList)))
    img = cv2.cvtColor(cv2.imread(imgPath), cv2.COLOR_BGR2RGB)
    # img = misc.imresize(img, (resize_to, resize_to), interp='bilinear')
    img = np.array(Image.fromarray(img).resize((resize_to, resize_to), resample=Image.BILINEAR))
    size = int(img.shape[0] / scale)
    # resizeImg = misc.imresize(img, (size, size), interp='bilinear')
    resizeImg = np.array(Image.fromarray(img).resize((size, size), resample=Image.BILINEAR))
    # finalImg = misc.imresize(resizeImg, (img.shape[0], img.shape[0]), interp='bilinear')
    finalImg = np.array(Image.fromarray(resizeImg).resize((img.shape[0], img.shape[0]), resample=Image.BILINEAR))
    combineImg = Image.new('RGB', (img.shape[0]*2, img.shape[0]))
    combineImg.paste(Image.fromarray(finalImg), (0,0))
    combineImg.paste(Image.fromarray(img), (img.shape[0]+1,0))
    savePath = ""
    # sample ratio of train:test:val is 6:2:2
    if index % 10 < 6:
        savePath = os.path.join(dataset_dir, train_dir, file)
    elif index % 10 < 8:
        savePath = os.path.join(dataset_dir, test_dir, file)
    else:
        savePath = os.path.join(dataset_dir, val_dir, file)
    #misc.imsave(savePath, combineImg)
    combineImg.save(savePath)

max_stars_repo_name: fialhocoelho/projetoic

max_issues_repo_name: fialhocoelho/projetoic

max_forks_repo_name: fialhocoelho/projetoic


text:
""" All the transformations that can be applied on a circuit graph. """

import networkx as nx

def retiming_transform(G, r):
  """ Apply retiming by inserting a lag value to each vertex.

  Args:
    G(nx.MultiDiGraph)
    r(dict): mapping for vertexes to retiming labels
  Returns:
    A transformed graph.
  """
  assert isinstance(G, nx.MultiDiGraph)
  assert isinstance(r, dict)

  G_ = G.copy()
  for u, v, i in G.edges(keys=True):
    if u in r:
      G_[u][v][i]['weight'] -= r[u]
    if v in r:
      G_[u][v][i]['weight'] += r[v]

    # validation
    if not isinstance(G_[u][v][i]['weight'], int):
      raise TypeError('Values in retiming {} involve non-integers'.format(r))
    if G_[u][v][i]['weight'] < 0:
      raise ValueError(
          'Retiming "{r}" turns the weight of an edge ({u}, {v}, {i}) into a negative value: {w}.'
          .format(r=r, u=u, v=v, i=i, w=G_[u][v][i]['weight']))

  return G_

max_stars_repo_name: kumasento/retiming

max_issues_repo_name: kumasento/retiming

max_forks_repo_name: kumasento/retiming

reason: import networkx


text:
import numpy as np
from basis.function import Function

class Gaussian(Function):
    """
    Class to store information about a single gaussian basis function

    TODO: Add the functions necessary functions to evaluate gaussian to make a cube file

    Attributes
    ----------
    """
    def __init__(self, exps, contract_coeff, ang_mom, pos, on_center, center_Z, center_symb):
        Function.__init__(self, exps, contract_coeff, ang_mom, pos, on_center, center_Z, center_symb)

max_stars_repo_name: amandadumi/semiempy

max_issues_repo_name: amandadumi/semiempy

max_forks_repo_name: amandadumi/semiempy


text:
from __future__ import print_function

import os
import sys
import logging
import h5py

import pandas as pd
import numpy as np
from tensorflow.keras.utils import to_categorical

file_path = os.path.dirname(os.path.realpath(__file__))
lib_path2 = os.path.abspath(os.path.join(file_path, '..', '..', 'common'))
sys.path.append(lib_path2)

import candle

logger = logging.getLogger(__name__)
candle.set_parallelism_threads()

additional_definitions = [
    {'name': 'latent_dim',
     'action': 'store',
     'type': int,
     'help': 'latent dimensions'},
    {'name': 'residual',
     'type': candle.str2bool,
     'default': False,
     'help': 'add skip connections to the layers'},
    {'name': 'reduce_lr',
     'type': candle.str2bool,
     'default': False,
     'help': 'reduce learning rate on plateau'},
    {'name': 'warmup_lr',
     'type': candle.str2bool,
     'default': False,
     'help': 'gradually increase learning rate on start'},
    {'name': 'base_lr',
     'type': float,
     'help': 'base learning rate'},
    {'name': 'epsilon_std',
     'type': float,
     'help': 'epsilon std for sampling latent noise'},
    {'name': 'use_cp',
     'type': candle.str2bool,
     'default': False,
     'help': 'checkpoint models with best val_loss'},
    {'name': 'use_tb',
     'type': candle.str2bool,
     'default': False,
     'help': 'use tensorboard'},
    {'name': 'tsne',
     'type': candle.str2bool,
     'default': False,
     'help': 'generate tsne plot of the latent representation'}
]

required = [
    'activation',
    'batch_size',
    'dense',
    'dropout',
    'epochs',
    'initialization',
    'learning_rate',
    'loss',
    'optimizer',
    'rng_seed',
    'scaling',
    'val_split',
    'latent_dim',
    'batch_normalization',
    'epsilon_std',
    'timeout'
]

class BenchmarkAttn(candle.Benchmark):

    def set_locals(self):
        """Functionality to set variables specific for the benchmark
        - required: set of required parameters for the benchmark.
        - additional_definitions: list of dictionaries describing the additional parameters for the
        benchmark.
        """

        if required is not None:
            self.required = set(required)
        if additional_definitions is not None:
            self.additional_definitions = additional_definitions

def extension_from_parameters(params, framework=''):
    """Construct string for saving model with annotation of parameters"""
    ext = framework
    for i, n in enumerate(params['dense']):
        if n:
            ext += '.D{}={}'.format(i + 1, n)
    ext += '.A={}'.format(params['activation'][0])
    ext += '.B={}'.format(params['batch_size'])
    ext += '.E={}'.format(params['epochs'])
    ext += '.L={}'.format(params['latent_dim'])
    ext += '.LR={}'.format(params['learning_rate'])
    ext += '.S={}'.format(params['scaling'])

    if params['epsilon_std'] != 1.0:
        ext += '.EPS={}'.format(params['epsilon_std'])
    if params['dropout']:
        ext += '.DR={}'.format(params['dropout'])
    if params['batch_normalization']:
        ext += '.BN'
    if params['warmup_lr']:
        ext += '.WU_LR'
    if params['reduce_lr']:
        ext += '.Re_LR'
    if params['residual']:
        ext += '.Res'

    return ext

def save_cache(cache_file, x_train, y_train, x_val, y_val, x_test, y_test):
    hf = h5py.File(cache_file, 'w')
    hf.create_dataset("x_train", data=x_train)
    hf.create_dataset("y_train", data=y_train)
    hf.create_dataset("x_val", data=x_val)
    hf.create_dataset("y_val", data=y_val)
    hf.create_dataset("x_test", data=x_test)
    hf.create_dataset("y_test", data=y_test)
    hf.close()

def load_cache(cache_file, perc):
    with h5py.File(cache_file, 'r') as hf:
        x_train = hf['x_train'][:]
        y_train = hf['y_train'][:]
        x_val = hf['x_val'][:]
        y_val = hf['y_val'][:]
        x_test = hf['x_test'][:]
        y_test = hf['y_test'][:]
    return x_train[:int(len(x_train)*perc), :], y_train[:int(len(y_train)*perc), :], x_val[:int(len(x_val)*perc), :], y_val[:int(len(y_val)*perc), :], x_test[:int(len(x_test)*perc), :], y_test[:int(len(y_test)*perc), :]

def download_and_preprocess_data(train_data='top_21_1fold_001.h5', data_url='http://ftp.mcs.anl.gov/pub/candle/public/benchmarks/Pilot1/uno/'):

    # start change #
    if train_data.endswith('h5') or train_data.endswith('hdf5'):
        print('processing h5 in file {}'.format(train_data))

        url = data_url
        file_train = train_data
        train_file = "../../Data/Pilot1/top_21_1fold_001.h5"
        # train_file = candle.get_file(file_train, url + file_train, cache_subdir='Pilot1')

        df_x_train_0 = pd.read_hdf(train_file, 'x_train_0').astype(np.float32)
        df_x_train_1 = pd.read_hdf(train_file, 'x_train_1').astype(np.float32)
        X_train = pd.concat([df_x_train_0, df_x_train_1], axis=1, sort=False)
        del df_x_train_0, df_x_train_1

        df_x_test_0 = pd.read_hdf(train_file, 'x_test_0').astype(np.float32)
        df_x_test_1 = pd.read_hdf(train_file, 'x_test_1').astype(np.float32)
        X_test = pd.concat([df_x_test_0, df_x_test_1], axis=1, sort=False)
        del df_x_test_0, df_x_test_1

        df_x_val_0 = pd.read_hdf(train_file, 'x_val_0').astype(np.float32)
        df_x_val_1 = pd.read_hdf(train_file, 'x_val_1').astype(np.float32)
        X_val = pd.concat([df_x_val_0, df_x_val_1], axis=1, sort=False)
        del df_x_val_0, df_x_val_1

        Y_train = pd.read_hdf(train_file, 'y_train')
        Y_test = pd.read_hdf(train_file, 'y_test')
        Y_val = pd.read_hdf(train_file, 'y_val')

        # assumes AUC is in the third column at index 2
        # df_y = df['AUC'].astype('int')
        # df_x = df.iloc[:,3:].astype(np.float32)

        # assumes dataframe has already been scaled
        # scaler = StandardScaler()
        # df_x = scaler.fit_transform(df_x)
    else:
        print('expecting in file file suffix h5')
        sys.exit()

    # preprocessing
    Y_train = Y_train['AUC']
    Y_test = Y_test['AUC']
    Y_val = Y_val['AUC']

    Y_train_neg, Y_train_pos = np.bincount(Y_train)
    Y_test_neg, Y_test_pos = np.bincount(Y_test)
    Y_val_neg, Y_val_pos = np.bincount(Y_val)

    Y_train_total = Y_train_neg + Y_train_pos
    Y_test_total = Y_test_neg + Y_test_pos
    Y_val_total = Y_val_neg + Y_val_pos

    total = Y_train_total + Y_test_total + Y_val_total
    # neg = Y_train_neg + Y_test_neg + Y_val_neg
    pos = Y_train_pos + Y_test_pos + Y_val_pos

    print('Examples:\n    Total: {}\n    Positive: {} ({:.2f}% of total)\n'.format(
        total, pos, 100 * pos / total))

    nb_classes = 2

    Y_train = to_categorical(Y_train, nb_classes)
    Y_test = to_categorical(Y_test, nb_classes)
    Y_val = to_categorical(Y_val, nb_classes)

    print('X_train shape:', X_train.shape)
    print('X_test shape:', X_test.shape)

    print('Y_train shape:', Y_train.shape)
    print('Y_test shape:', Y_test.shape)

    # saving
    save_cache(file_train, X_train, Y_train, X_val, Y_val, X_test, Y_test)

    return X_train, Y_train, X_val, Y_val, X_test, Y_test

    # start change #
    if train_file.endswith('h5') or train_file.endswith('hdf5'):
        print('processing h5 in file {}'.format(train_file))

        df_x_train_0 = pd.read_hdf(train_file, 'x_train_0').astype(np.float32)
        df_x_train_1 = pd.read_hdf(train_file, 'x_train_1').astype(np.float32)
        X_train = pd.concat([df_x_train_0, df_x_train_1], axis=1, sort=False)
        del df_x_train_0, df_x_train_1

        df_x_test_0 = pd.read_hdf(train_file, 'x_test_0').astype(np.float32)
        df_x_test_1 = pd.read_hdf(train_file, 'x_test_1').astype(np.float32)
        X_test = pd.concat([df_x_test_0, df_x_test_1], axis=1, sort=False)
        del df_x_test_0, df_x_test_1

        df_x_val_0 = pd.read_hdf(train_file, 'x_val_0').astype(np.float32)
        df_x_val_1 = pd.read_hdf(train_file, 'x_val_1').astype(np.float32)
        X_val = pd.concat([df_x_val_0, df_x_val_1], axis=1, sort=False)
        del df_x_val_0, df_x_val_1

        Y_train = pd.read_hdf(train_file, 'y_train')
        Y_test = pd.read_hdf(train_file, 'y_test')
        Y_val = pd.read_hdf(train_file, 'y_val')

        # assumes AUC is in the third column at index 2
        # df_y = df['AUC'].astype('int')
        # df_x = df.iloc[:,3:].astype(np.float32)

        # assumes dataframe has already been scaled
        # scaler = StandardScaler()
        # df_x = scaler.fit_transform(df_x)
    else:
        print('expecting in file file suffix h5')
        sys.exit()

    print('x_train shape:', X_train.shape)
    print('x_test shape:', X_test.shape)

    return X_train, Y_train, X_val, Y_val, X_test, Y_test

max_stars_repo_name: minesweeter/Benchmarks

max_issues_repo_name: minesweeter/Benchmarks

max_forks_repo_name: minesweeter/Benchmarks


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

def harmonic(x0,v0,T,dt=0.01,k=1.0,m=1.0):
    E0  = 0.5*k*x0**2 + 0.5*m*v0**2
    Time = [i*dt for i in range(int(T/dt)+1)]
    X = [x0]
    V = [v0]
    Energy = [E0]
    x = x0
    v = v0

    for i in range(int(T/dt)):
        f0x = v
        f0v = -k/m*x
        x1 = x + f0x*dt/2
        v1 = v + f0v*dt/2

        f1x = v1
        f1v = -k/m*x1
        x2 = x + f1x*dt/2
        v2 = v + f1v*dt/2

        f2x = v2
        f2v = -k/m*x2
        x3 = x + f2v*dt
        v3 = v + f2v*dt

        f3x = v3
        f3v = -k/m*x3

        x = x + (f0x + 2*f1x +2*f2x + f3x)/6*dt
        v = v + (f0v + 2*f1v +2*f2v + f3v)/6*dt
        E = 0.5*k*x**2 + 0.5*m*v**2
        X.append(x)
        V.append(v)
        Energy.append(E)
    return Energy,X,V,Time

energy, distance, velocity, time = harmonic(0,0.1,20)
plt.plot(time,energy)
plt.plot(time,distance)
plt.plot(time,velocity)
plt.show()

max_stars_repo_name: samantamrityunjay/computational

max_issues_repo_name: samantamrityunjay/computational

max_forks_repo_name: samantamrityunjay/computational


text:
from __future__ import division

# pylint: disable-msg=W0402

from datetime import datetime
import random
import string
import sys

from contextlib import contextmanager  # contextlib is available since 2.5

from distutils.version import LooseVersion

from numpy.random import randn
import numpy as np

from pandas.core.common import isnull
import pandas.core.index as index
import pandas.core.series as series
import pandas.core.frame as frame
import pandas.core.panel as panel

from pandas import bdate_range
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.period import PeriodIndex

Index = index.Index
Series = series.Series
DataFrame = frame.DataFrame
Panel = panel.Panel

N = 30
K = 4

def rands(n):
    choices = string.ascii_letters + string.digits
    return ''.join([random.choice(choices) for _ in xrange(n)])

def randu(n):
    choices = u"".join(map(unichr, range(1488, 1488 + 26))) + string.digits
    return ''.join([random.choice(choices) for _ in xrange(n)])

#------------------------------------------------------------------------------
# Console debugging tools

def debug(f, *args, **kwargs):
    from pdb import Pdb as OldPdb
    try:
        from IPython.core.debugger import Pdb
        kw = dict(color_scheme='Linux')
    except ImportError:
        Pdb = OldPdb
        kw = {}
    pdb = Pdb(**kw)
    return pdb.runcall(f, *args, **kwargs)

def pudebug(f, *args, **kwargs):
    import pudb
    return pudb.runcall(f, *args, **kwargs)

def set_trace():
    from IPython.core.debugger import Pdb
    try:
        Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
    except:
        from pdb import Pdb as OldPdb
        OldPdb().set_trace(sys._getframe().f_back)

#------------------------------------------------------------------------------
# Comparators

def equalContents(arr1, arr2):
    """Checks if the set of unique elements of arr1 and arr2 are equivalent.
    """
    return frozenset(arr1) == frozenset(arr2)

def isiterable(obj):
    return hasattr(obj, '__iter__')

def assert_almost_equal(a, b):
    if isinstance(a, dict) or isinstance(b, dict):
        return assert_dict_equal(a, b)

    if isinstance(a, basestring):
        assert a == b, (a, b)
        return True

    if isiterable(a):
        np.testing.assert_(isiterable(b))
        assert(len(a) == len(b))
        if np.array_equal(a, b):
            return True
        else:
            for i in xrange(len(a)):
                assert_almost_equal(a[i], b[i])
        return True

    err_msg = lambda a, b: 'expected %.5f but got %.5f' % (a, b)

    if isnull(a):
        np.testing.assert_(isnull(b))
        return

    if isinstance(a, (bool, float, int)):
        # case for zero
        if abs(a) < 1e-5:
            np.testing.assert_almost_equal(
                a, b, decimal=5, err_msg=err_msg(a, b), verbose=False)
        else:
            np.testing.assert_almost_equal(
                1, a / b, decimal=5, err_msg=err_msg(a, b), verbose=False)
    else:
        assert(a == b)

def is_sorted(seq):
    return assert_almost_equal(seq, np.sort(np.array(seq)))

def assert_dict_equal(a, b, compare_keys=True):
    a_keys = frozenset(a.keys())
    b_keys = frozenset(b.keys())

    if compare_keys:
        assert(a_keys == b_keys)

    for k in a_keys:
        assert_almost_equal(a[k], b[k])

def assert_series_equal(left, right, check_dtype=True,
                        check_index_type=False,
                        check_index_freq=False,
                        check_series_type=False):
    if check_series_type:
        assert(type(left) == type(right))
    assert_almost_equal(left.values, right.values)
    if check_dtype:
        assert(left.dtype == right.dtype)
    assert(left.index.equals(right.index))
    if check_index_type:
        assert(type(left.index) == type(right.index))
        assert(left.index.dtype == right.index.dtype)
        assert(left.index.inferred_type == right.index.inferred_type)
    if check_index_freq:
        assert(getattr(left, 'freqstr', None) ==
               getattr(right, 'freqstr', None))

def assert_frame_equal(left, right, check_index_type=False,
                       check_column_type=False,
                       check_frame_type=False):
    if check_frame_type:
        assert(type(left) == type(right))
    assert(isinstance(left, DataFrame))
    assert(isinstance(right, DataFrame))

    assert(left.columns.equals(right.columns))
    assert(left.index.equals(right.index))

    for i, col in enumerate(left.columns):
        assert(col in right)
        lcol = left.icol(i)
        rcol = right.icol(i)
        assert_series_equal(lcol, rcol)

    if check_index_type:
        assert(type(left.index) == type(right.index))
        assert(left.index.dtype == right.index.dtype)
        assert(left.index.inferred_type == right.index.inferred_type)
    if check_column_type:
        assert(type(left.columns) == type(right.columns))
        assert(left.columns.dtype == right.columns.dtype)
        assert(left.columns.inferred_type == right.columns.inferred_type)

def assert_panel_equal(left, right, check_panel_type=False):
    if check_panel_type:
        assert(type(left) == type(right))

    assert(left.items.equals(right.items))
    assert(left.major_axis.equals(right.major_axis))
    assert(left.minor_axis.equals(right.minor_axis))

    for col, series in left.iterkv():
        assert(col in right)
        assert_frame_equal(series, right[col])

    for col in right:
        assert(col in left)

def assert_contains_all(iterable, dic):
    for k in iterable:
        assert(k in dic)

def getCols(k):
    return string.ascii_uppercase[:k]

def makeStringIndex(k):
    return Index([rands(10) for _ in xrange(k)])

def makeUnicodeIndex(k):
    return Index([randu(10) for _ in xrange(k)])

def makeIntIndex(k):
    return Index(range(k))

def makeFloatIndex(k):
    values = sorted(np.random.random_sample(k)) - np.random.random_sample(1)
    return Index(values * (10 ** np.random.randint(0, 9)))

def makeFloatSeries():
    index = makeStringIndex(N)
    return Series(randn(N), index=index)

def makeStringSeries():
    index = makeStringIndex(N)
    return Series(randn(N), index=index)

def makeObjectSeries():
    dateIndex = makeDateIndex(N)
    dateIndex = Index(dateIndex, dtype=object)
    index = makeStringIndex(N)
    return Series(dateIndex, index=index)

def getSeriesData():
    index = makeStringIndex(N)
    return dict((c, Series(randn(N), index=index)) for c in getCols(K))

def makeDataFrame():
    data = getSeriesData()
    return DataFrame(data)

def getArangeMat():
    return np.arange(N * K).reshape((N, K))

def getMixedTypeDict():
    index = Index(['a', 'b', 'c', 'd', 'e'])

    data = {
        'A': [0., 1., 2., 3., 4.],
        'B': [0., 1., 0., 1., 0.],
        'C': ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'],
        'D': bdate_range('1/1/2009', periods=5)
    }

    return index, data

def makeDateIndex(k):
    dt = datetime(2000, 1, 1)
    dr = bdate_range(dt, periods=k)
    return DatetimeIndex(dr)

def makePeriodIndex(k):
    dt = datetime(2000, 1, 1)
    dr = PeriodIndex(start=dt, periods=k, freq='B')
    return dr

def makeTimeSeries(nper=None):
    if nper is None:
        nper = N
    return Series(randn(nper), index=makeDateIndex(nper))

def makePeriodSeries(nper=None):
    if nper is None:
        nper = N
    return Series(randn(nper), index=makePeriodIndex(nper))

def getTimeSeriesData():
    return dict((c, makeTimeSeries()) for c in getCols(K))

def makeTimeDataFrame():
    data = getTimeSeriesData()
    return DataFrame(data)

def getPeriodData():
    return dict((c, makePeriodSeries()) for c in getCols(K))

def makePeriodFrame():
    data = getPeriodData()
    return DataFrame(data)

def makePanel():
    cols = ['Item' + c for c in string.ascii_uppercase[:K - 1]]
    data = dict((c, makeTimeDataFrame()) for c in cols)
    return Panel.fromDict(data)

def add_nans(panel):
    I, J, N = panel.shape
    for i, item in enumerate(panel.items):
        dm = panel[item]
        for j, col in enumerate(dm.columns):
            dm[col][:i + j] = np.NaN

class TestSubDict(dict):
    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)

# Dependency checks.  Copied this from Nipy/Nipype (Copyright of
# respective developers, license: BSD-3)
def package_check(pkg_name, version=None, app='pandas', checker=LooseVersion,
                  exc_failed_import=ImportError,
                  exc_failed_check=RuntimeError):
    """Check that the minimal version of the required package is installed.

    Parameters
    ----------
    pkg_name : string
        Name of the required package.
    version : string, optional
        Minimal version number for required package.
    app : string, optional
        Application that is performing the check.  For instance, the
        name of the tutorial being executed that depends on specific
        packages.
    checker : object, optional
        The class that will perform the version checking.  Default is
        distutils.version.LooseVersion.
    exc_failed_import : Exception, optional
        Class of the exception to be thrown if import failed.
    exc_failed_check : Exception, optional
        Class of the exception to be thrown if version check failed.

    Examples
    --------
    package_check('numpy', '1.3')
    package_check('networkx', '1.0', 'tutorial1')

    """

    if app:
        msg = '%s requires %s' % (app, pkg_name)
    else:
        msg = 'module requires %s' % pkg_name
    if version:
        msg += ' with version >= %s' % (version,)
    try:
        mod = __import__(pkg_name)
    except ImportError:
        raise exc_failed_import(msg)
    if not version:
        return
    try:
        have_version = mod.__version__
    except AttributeError:
        raise exc_failed_check('Cannot find version for %s' % pkg_name)
    if checker(have_version) < checker(version):
        raise exc_failed_check(msg)

def skip_if_no_package(*args, **kwargs):
    """Raise SkipTest if package_check fails

    Parameters
    ----------
    *args Positional parameters passed to `package_check`
    *kwargs Keyword parameters passed to `package_check`
    """
    from nose import SkipTest
    package_check(exc_failed_import=SkipTest,
                  exc_failed_check=SkipTest,
                  *args, **kwargs)

#
# Additional tags decorators for nose
#

def network(t):
    """
    Label a test as requiring network connection.

    In some cases it is not possible to assume network presence (e.g. Debian
    build hosts).

    Parameters
    ----------
    t : callable
        The test requiring network connectivity.

    Returns
    -------
    t : callable
        The decorated test `t`.

    Examples
    --------
    A test can be decorated as requiring network like this::

      from pandas.util.testing import *

      @network
      def test_network(self):
          print 'Fetch the stars from http://'

    And use ``nosetests -a '!network'`` to exclude running tests requiring
    network connectivity.
    """

    t.network = True
    return t

class SimpleMock(object):
    """
    Poor man's mocking object

    Note: only works for new-style classes, assumes  __getattribute__ exists.

    >>> a = type("Duck",(),{})
    >>> a.attr1,a.attr2 ="fizz","buzz"
    >>> b = SimpleMock(a,"attr1","bar")
    >>> b.attr1 == "bar" and b.attr2 == "buzz"
    True
    >>> a.attr1 == "fizz" and a.attr2 == "buzz"
    True
    """
    def __init__(self, obj, *args, **kwds):
        assert(len(args) % 2 == 0)
        attrs = kwds.get("attrs", {})
        for k, v in zip(args[::2], args[1::2]):
            # dict comprehensions break 2.6
            attrs[k] = v
        self.attrs = attrs
        self.obj = obj

    def __getattribute__(self, name):
        attrs = object.__getattribute__(self, "attrs")
        obj = object.__getattribute__(self, "obj")
        return attrs.get(name, type(obj).__getattribute__(obj, name))

@contextmanager
def stdin_encoding(encoding=None):
    """
    Context manager for running bits of code while emulating an arbitrary
    stdin encoding.

    >>> import sys
    >>> _encoding = sys.stdin.encoding
    >>> with stdin_encoding('AES'): sys.stdin.encoding
    'AES'
    >>> sys.stdin.encoding==_encoding
    True

    """
    import sys
    _stdin = sys.stdin
    sys.stdin = SimpleMock(sys.stdin, "encoding", encoding)
    yield
    sys.stdin = _stdin

max_stars_repo_name: flexlee/pandas

max_issues_repo_name: flexlee/pandas

max_forks_repo_name: flexlee/pandas

reason: import numpy,from numpy


text:
from scipy.integrate import solve_dde
from scipy.special import factorial
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import CubicHermiteSpline
from jitcdde import jitcdde
from jitcdde import y as y_jit
from jitcdde import t as t_jit

"""
The so-called diverging problem with jumps, seen in Example 4 of
Willé & Baker 1992 DELSOL - a numerical code for the solution of systems of
delay–differential equations.

    - Equation : $y'(t) = y(t-1)$
    - History :  $h(t<0) = (-1)^[-5t]$ with [s] = integer part of s.

Tested features:
    - initial discontinuities and jumps in histories

Comparison to analytical solution in t [0,5]
"""

def ana(t, tau):
    """
    Solution of $y'(t>=0)=\mu y(t-tau) $
    et $y(t<=0)= -1^{[-5t]}$
    """
    if t < 0.0:
        return (-1)**(np.floor(-5*t))
    elif t < 0.2:
        return t + 1.
    elif t < 0.4:
        return -t + 1.4
    elif t < 0.6:
        return t + 0.6
    elif t < 0.8:
        return -t + 1.8
    elif t < 1.0:
        return t + 0.2
    elif t < 1.2:
        return t**2 * 0.5 + 0.7
    elif t < 1.4:
        return -t**2/2 + 12*t/5 - 0.74
    elif t < 1.6:
        return t**2/2 - 2*t/5 + 1.22
    elif t < 1.8:
        return -t**2/2 + 14*t/5 - 1.34
    elif t < 2.0:
        return t**2/2 - 4*t/5 + 1.9
    elif t < 2.2:
        return t**3/6 - t**2/2 + 6*t/5 + 0.566666666666667
    elif t <2.4:
        return -t**3/6 + 17*t**2/10 - 91*t/25 + 4.116
    elif t < 2.6:
        return t**3/6 - 7*t**2/10 + 53*t/25 - 0.492
    elif t < 2.8:
        return -t**3/6 + 19*t**2/10 - 116*t/25 + 5.36666666666666
    elif t < 3.0:
        return t**3/6 - 9*t**2/10 + 16*t/5 - 1.95066666666668
    elif t < 3.2:
        return t**4/24 - t**3/3 + 27*t**2/20 - 13*t/10 + 1.42433333333332
    elif t < 3.4:
        return -t**4/24 + 11*t**3/15 - 377*t**2/100 + 7217*t/750 - 7.31379999999998
    elif t < 3.6:
        return t**4/24 - 2*t**3/5 + 201*t**2/100 - 2609*t/750 + 3.82233333333336
    elif t < 3.8:
        return -t**4/24 + 4*t**3/5 - 447*t**2/100 + 362200000000001*t/30000000000000 - 10.1744666666667
    elif t < 4.0:
        return t**4/24 - 7*t**3/15 + 11*t**2/4 - 466300000000001*t/75000000000000 + 7.20166666666679
    elif t < 4.2:
        return t**5/120 - t**4/8 + 13*t**3/15 - 31*t**2/12 + 111233333333333*t/25000000000000 - 1.33166666666654
    elif t < 4.4:
        return -t**5/120 + 9*t**4/40 - 311*t**3/150 + 14647*t**2/1500 - 107407333333333*t/5000000000000 + 20.4502053333331
    elif t < 4.6:
        return t**5/120 - 17*t**4/120 + 173*t**3/150 - 6649*t**2/1500 + 2925800000000009*t/300000000000000 - 7.03583200000007
    elif t < 4.8:
        return -t**5/120 + 29*t**4/120 - 178*t**3/75 + 707400000000001*t**2/60000000000000 - 826784000000003*t/30000000000000 + 27.2913306666669
    elif t <= 5.0:
        return t**5/120 - 19*t**4/120 + 22*t**3/15 - 996300000000001*t**2/150000000000000 + 500320000000003*t/30000000000000 - 15.1759973333335

def fun(t,y,Z):
    y_tau = Z[:,0]
    return [ y_tau ]

tau = 1
y0 = [1.]
t0 = 0.0
tf = 5
atol = 1e-10
rtol = 1e-5
tspan = [t0, tf]
delays = [tau]

def h(t):
    return [(-1)**(np.floor(-5*t))]

jumps = sorted([-k*(1/5) for k in range(1,6)])
tspan = [t0, tf]
sol = solve_dde(fun, tspan, delays, y0, h, method='RK23', jumps=jumps, atol=atol, rtol=rtol)
sol45 = solve_dde(fun, tspan, delays, y0, h, method='RK45', jumps=jumps, atol=atol, rtol=rtol)

t = sol.t
y = sol.y[0,:]
yp = sol.yp[0,:]
t45 = sol45.t
y45 = sol45.y[0,:]
yp45 = sol45.yp[0,:]

#jit cdde
f = [ y_jit(0,t_jit-tau)]

DDE = jitcdde(f,
		max_delay=20 # for plotting; lest history is forgotten
	)
DDE.set_integration_parameters(atol=atol,rtol=rtol)
DDE.past_from_function(h,times_of_interest=np.linspace(-1.1,-1e-8,10))

DDE.adjust_diff()
data = []
dt_jit = []
times = np.linspace(DDE.t+0.01, tf, 101)
for time in times:
    data.append(DDE.integrate(time))
    dt_jit.append(DDE.dt)
y_jit = np.asarray(data).T[0,:]

# sol matlab
import scipy.io as spio
path_matlab = 'data_dde23/solDivJumps.mat'
mat = spio.loadmat(path_matlab, squeeze_me=True)

t_mat = mat['t']
y_mat = mat['y']
yp_mat = mat['yp']

# sol with rtol=1e-10 atol=1e-12
f90 = np.loadtxt('data_dde_solver_fortran/divJ.dat')
t_f90 = f90[:,0]
y_f90 = f90[:,1]
yp_f90 = f90[:,1]

ana_spdev = np.array([ana(t[i], tau) for i in range(len(t))])
ana_spdev45 = np.array([ana(t45[i], tau) for i in range(len(t45))])
ana_mat = np.array([ana(t_mat[i], tau) for i in range(len(t_mat))])
ana_jit = np.array([ana(times[i],tau) for i in range(len(times))])
ana_f90 = np.array([ana(t_f90[i],tau) for i in range(len(t_f90))])

err_f90 = np.abs(ana_f90 - y_f90) / ana_f90
err_jit = np.abs(ana_jit - y_jit) / ana_jit
err_scipy = np.abs(ana_spdev - y) / ana_spdev
err_scipy45 = np.abs(ana_spdev45 - y45) / ana_spdev45
err_mat = np.abs(ana_mat - y_mat) / ana_mat

plt.figure()
plt.plot(t, ana_spdev, 'o-', label='ana scipy-dev y(t)')
plt.plot(t, y, 'o-', label='scipy-dev y(t)')
plt.plot(times, y_jit, 'o-', label='jit y(t)')
plt.plot(t_mat, y_mat, 'o-', label='matlab y(t)')
plt.plot(t_f90, y_f90, '-', label='f90')
plt.xlabel(r'$t$')
plt.xlabel(r'$y$')
plt.legend()
plt.savefig('figures/solDiv/jumps_y')

plt.figure()
plt.plot(times, err_jit,'-o',label='err jit')
plt.plot(t, err_scipy,'-s',label='err scipy RK23')
plt.plot(t45, err_scipy45,'-o',label='err scipy RK45')
plt.plot(t_mat, err_mat, '-o', label='err mat')
plt.plot(t_f90, err_f90, '-o', label='err f90')
plt.yscale('log')
plt.legend()
plt.ylabel(r'$err$')
plt.xlabel(r'$t$')
plt.savefig('figures/solDiv/jumps_err')

plt.figure()
plt.plot(t[:-1],np.diff(t),'-o',label='dt RK23')
plt.plot(t45[:-1],np.diff(t45),'-o',label='dt RK45')
# plt.plot(t_f90[:-1],np.diff(t_f90),'-o',label='dt f90')
plt.plot(times, dt_jit,'-o',label='dt jit')
plt.plot(t_mat[:-1],np.diff(t_mat),'-o',label='dt matlab solver')
plt.yscale('log')
plt.legend()
plt.ylabel(r'$\Delta t$')
plt.xlabel(r'$t$')
plt.savefig('figures/solDiv/jumps_dt')

plt.show()

max_stars_repo_name: jrmejansen/scipy

max_issues_repo_name: jrmejansen/scipy

max_forks_repo_name: jrmejansen/scipy

reason: import numpy,import scipy,from scipy


text:
from pyamps.plot_utils import Polarsubplot
from pyamps import AMPS
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text',usetex=True)

d2r = np.pi/180

v = 350
B = 4.
tilts = [-25, 0, 25]
F107 = 80.
EQRES = 10
model = AMPS(v, B, B, 0, F107, minlat = 60) # CHANGE COEFFICIENT FILE IN THIS LINE

psilevels = np.hstack((np.r_[-300:0:EQRES], np.r_[0:300:EQRES]))
faclevels = np.linspace(-.95, .95, 20)/2
mlat, mlt = map(lambda x:np.split(x, 2)[0], model.scalargrid)

withFAC = True

# dictionary of where to put the different clock angle bins
ca = {(0, 1): 0, (0, 0):-45, (1, 0):-90,(2,0):-135,(2,1):180,(0,2):45,(1,2):90,(2,2):135}

for tilt in tilts:

    fig = plt.figure(figsize = (9, 9))
    axes = [[Polarsubplot(plt.subplot2grid((91, 3), (j*30, i), rowspan = 30),
                          linestyle = ':', color = 'grey', minlat = 60, linewidth = .9)
             for i in range(3) if (i, j) != (1, 1)]
             for j in range(3)]
    axdial = plt.subplot2grid((91, 3), (30, 1), rowspan = 30)
    axdial.set_aspect('equal')
    axdial.set_axis_off()
    if withFAC:
        axcbar = plt.subplot2grid((91, 3), (90, 1))
    axinfo = plt.subplot2grid((91, 3), (90, 0))
    axinfo.set_axis_off()

    for i in range(3):
        for j in range(3):
            if i == j == 1:
                continue # skip
            By, Bz = np.sin(ca[(i,j)] * d2r) * B, np.cos(ca[(i,j)] * d2r) * B
            axdial.text(np.sin(ca[i, j] * d2r), np.cos(ca[i, j] * d2r), str(ca[i, j]) + r'$^\circ$', rotation = -ca[i, j], ha = 'center', va = 'center')
            axdial.plot([.75*np.sin(ca[i, j] * d2r), .85*np.sin(ca[i, j] * d2r)], [.75*np.cos(ca[i, j] * d2r), .85*np.cos(ca[i, j] * d2r)], color = 'lightgrey')

            if (i, j) == (1, 2):
                j = 1
            model.update_model(v, By, Bz, tilt, F107)

            ju      = model.get_upward_current(mlat, mlt)
            psi     = model.get_divergence_free_current_function(mlat, mlt)
            j_up, j_down, _, __ = model.get_integrated_upward_current()
            if withFAC:
                axes[i][j].contourf(mlat, mlt, ju, levels = faclevels, cmap = plt.cm.bwr, extend = 'both')
                axes[i][j].write(60, 21, '$\parallel$ {:.1f}'.format(j_up) + ' MA\n$\perp$ {0:.0f} kA'.format(psi.max() - psi.min()), ha = 'right', va = 'top', size = 9, multialignment='right')
                axes[i][j].write(60, 10, '$\Delta+' + '{:.2f}'.format(ju.max()) + '\mu$A/m$^2$\n $\\nabla' + '{:.2f}'.format(ju.min()) + ' \mu$A/m$^2$', ha = 'left', va = 'bottom', size = 9, multialignment='left')
                axes[i][j].scatter(mlat[ju.argmax()], mlt[ju.argmax()], marker = '^', s = 40, c = 'black', zorder = 100)
                axes[i][j].scatter(mlat[ju.argmax()], mlt[ju.argmax()], marker = '^', s = 20, c = 'red', linewidth = .5, zorder = 101)
                axes[i][j].scatter(mlat[ju.argmin()], mlt[ju.argmin()], marker = 'v', s = 40, c = 'black', zorder = 100)
                axes[i][j].scatter(mlat[ju.argmin()], mlt[ju.argmin()], marker = 'v', s = 20, c = 'blue', linewidth = .5, zorder = 101)

            axes[i][j].contour( mlat, mlt, psi, levels = psilevels, colors = 'black', linewidths = .8)

    axdial.text(0, 0, u'DIVERGENCE-\nFREE AND\nBIRKELAND\nCURRENT', ha = 'center', va = 'center', size = 14)

    plt.subplots_adjust(hspace = .01, wspace = .01, left = .01, right = .99, bottom = .05, top = .99)

    axdial.set_xlim(-1.2, 1.2)
    axdial.set_ylim(-1.2, 1.2)

    if withFAC:
        axcbar.contourf(faclevels, [0, 1], np.vstack((faclevels, faclevels)), levels = faclevels, cmap = plt.cm.bwr)
        axcbar.set_yticks([])
        axcbar.set_xlabel('$\mu$A/m$^2$')

    axinfo.text(axinfo.get_xlim()[0], axinfo.get_ylim()[0], '$|B|$ = %s nT, $v$ = %s km/s,\n F$_{10.7}$ = %s, TILT $= %s^\circ$' % (B, v, F107, tilt), ha = 'left', va = 'top', size = 9)

    plt.savefig('amps_fac_and_equivalent_current_' + str(tilt) + '_deg_tilt.png', dpi = 250)
    plt.savefig('amps_fac_and_equivalent_current_' + str(tilt) + '_deg_tilt.pdf')
    plt.savefig('amps_fac_and_equivalent_current_' + str(tilt) + '_deg_tilt.svg')

plt.show()

max_stars_repo_name: Dartspacephysiker/pySHEIC

max_issues_repo_name: Dartspacephysiker/pySHEIC

max_forks_repo_name: Dartspacephysiker/pySHEIC


text:
import pytest
import numpy as np

from vis.backend import modify_model_backprop
from vis.utils.test_utils import skip_backends

import tensorflow.keras
from tensorflow.keras.models import Model, Input, Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.initializers import Constant
from tensorflow.keras import backend as K
from tensorflow.keras.activations import get
from tensorflow.keras.layers import advanced_activations, Activation

def _compute_grads(model, input_array):
    grads_fn = K.gradients(model.output, model.input)[0]
    compute_fn = K.function([model.input, K.learning_phase()], [grads_fn])
    return compute_fn([np.array([input_array]), 0])[0][0]

@skip_backends('theano')
def test_guided_grad_modifier():
    # Create a simple 2 dense layer model.
    simple_model = Sequential([
        Dense(2, activation='relu', use_bias=False, kernel_initializer=Constant([[-1., 1.], [-1., 1.]]), input_shape=(2,)),
        Dense(1, activation='linear', use_bias=False, kernel_initializer=Constant([-1., 1.]))
    ])
    simple_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam())

    # Create a simple 2 dense layer model using Activation.
    simple_model_with_activation = Sequential([
        Dense(2, activation='linear', use_bias=False, kernel_initializer=Constant([[-1., 1.], [-1., 1.]]), input_shape=(2,)),
        Activation('relu'),
        Dense(1, activation='linear', use_bias=False, kernel_initializer=Constant([-1., 1.]))
    ])
    simple_model_with_activation.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam())

    for i, model in enumerate([simple_model, simple_model_with_activation]):
        # Create guided backprop model
        modified_model = modify_model_backprop(model, 'guided')

        # Gradients are zeros.
        input_array = [0., 0.]
        assert np.array_equal(_compute_grads(model, input_array), [0., 0.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [0., 0.])

        # Below 3 cases, GuidedBackprop gradients is the same as Original gradients.
        input_array = [1., 0.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [1., 1.])

        input_array = [0., 1.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [1., 1.])

        input_array = [1., 1.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [1., 1.])

        # If inputs contains negative values,
        # GuidedBackprop gradients is not the same as Original gradients.
        input_array = [-1., 0.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [0., 0.])

        input_array = [0., -1.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [0., 0.])

        input_array = [-1., -1.]
        assert np.array_equal(_compute_grads(model, input_array), [1., 1.])
        assert np.array_equal(_compute_grads(modified_model, input_array), [0., 0.])

        # Activation is not changed.
        if i == 0:  # modified first model
            modified_model.layers[0].activation == keras.activations.relu
            modified_model.layers[1].activation == keras.activations.linear
        if i == 1:  # modified second model
            modified_model.layers[0].activation == keras.activations.linear
            modified_model.layers[1].activation == keras.activations.relu
            modified_model.layers[2].activation == keras.activations.linear

# Currently, the modify_model_backprop function doesn't support advanced activation.
# Therefore, this test case will temporarily comment out.
#
# @skip_backends('theano')
# def test_advanced_activations():
#     """ Tests that various ways of specifying activations in keras models are handled when replaced with Relu
#     """
#     inp = Input(shape=(2, ))
#     x = Dense(5, activation='elu')(inp)
#     x = advanced_activations.LeakyReLU()(x)
#     x = Activation('elu')(x)
#     model = Model(inp, x)
#
#     # Ensure that layer.activation, Activation and advanced activations are replaced with relu
#     modified_model = modify_model_backprop(model, 'guided')
#     assert modified_model.layers[1].activation == get('relu')
#     assert modified_model.layers[2].activation == get('relu')
#     assert modified_model.layers[3].activation == get('relu')
#
#     # Ensure that original model is unchanged.
#     assert model.layers[1].activation == get('elu')
#     assert isinstance(model.layers[2], advanced_activations.LeakyReLU)
#     assert model.layers[3].activation == get('elu')

# @skip_backends('theano')
# def test_rectified_grad_modifier():
#     # Only test tensorflow implementation for now.
#     if K.backend() == 'theano':
#         return
#
#     # Create a simple linear sequence x -> linear(w.x) with weights w1 = -1, w2 = 1.
#     inp = Input(shape=(2, ))
#     out = Dense(1, activation='linear', use_bias=False, kernel_initializer=Constant([-1., 1.]))(inp)
#     model = Model(inp, out)
#
#     # Original model gradient should be [w1, w2]
#     assert np.array_equal(_compute_grads(model, [1., -1.]), [-1., 1.])
#
#     # Original gradient is [-1, 1] but new gradient should be [0, 1]
#     # First one is clipped because of negative gradient.
#     modified_model = modify_model_backprop(model, 'rectified')
#
#     # TODO: Interestingly this does not work for some reason.
#     # It is failing at tf.cast(grad > 0., dtype)
#     assert np.array_equal(_compute_grads(modified_model, [1., -1.]), [0., 1.])
#
#     # Ensure that the original model reference remains unchanged.
#     assert model.layers[1].activation == get('linear')
#     assert modified_model.layers[1].activation == get('relu')

if __name__ == '__main__':
    pytest.main([__file__])

max_stars_repo_name: chck/keras-vis

max_issues_repo_name: chck/keras-vis

max_forks_repo_name: chck/keras-vis


text:
# OSM Audit  - Lancet series
import time
from sqlalchemy import create_engine
import geopandas as gpd
from osgeo import gdal
import rasterio
from rasterio.mask import mask
import psycopg2
from geoalchemy2 import Geometry, WKTElement
import numpy as np
import json
from shapely.geometry import Polygon, MultiPolygon
from rasterstats import zonal_stats

from script_running_log import script_running_log

# Import custom variables for National Liveability indicator process
from _project_setup import *

def main():
    # simple timer for log file
    start = time.time()
    script = os.path.basename(sys.argv[0])
    task = 'Summarise OSM destination resources for city'

    date_yyyymmdd = time.strftime("%d%m%Y")
    # population raster set up
    population_folder = '../data/GHS'
    # projections = {'Mollwiede':54009,'WGS84':4326}
    projections = {'WGS84':4326}
    ghs_year = 2015
    region_sql = '''
    SELECT "Study region" study_region,
           geom
    FROM urban_study_region
    '''
    count_sql = '''
    DROP TABLE IF EXISTS urban_dest_summary;
    CREATE TABLE IF NOT EXISTS urban_dest_summary AS
    SELECT a.study_region,
           t.dest_name_full,
           t.count,
           a.urban_pop_est,
           a.area_sqkm,
           a.pop_per_sqkm,
           t.count/a.area_sqkm AS dest_per_sqkm,
           t.count/a.area_sqkm/(urban_pop_est/10000) AS dest_per_sqkm_per_10kpop
    FROM urban_study_region_pop a,
         (SELECT d.dest_name_full,
                 COUNT(d.*) count
            FROM osm_destinations d,
                 urban_study_region_pop c
        WHERE ST_Intersects(d.geom, c.geom)
        GROUP BY dest_name_full ) t
    ;
    SELECT * FROM urban_dest_summary;
    '''

    if locale != 'vic':
        print(locale)
        xls = pandas.ExcelFile(os.path.join(cwd,'_project_configuration.xlsx'))
        df_local = pandas.read_excel(xls, 'region_settings',index_col=0)
        df_local[locale] = df_local[locale].fillna('')
        for var in [x for x in  df_local.index.values]:
            globals()[var] = df_local.loc[var][locale]
        study_region = '{}_{}_{}'.format(locale,region,year).lower()
        # region specific output locations
        locale_dir = os.path.join(folder_path,'study_region','{}'.format(study_region))
        buffered_study_region = '{}_{}{}'.format(study_region,study_buffer,units)
        engine = create_engine(f"postgresql://{db_user}:{db_pwd}@{db_host}/{db}")
        clipping_boundary = gpd.GeoDataFrame.from_postgis('''SELECT geom FROM {table}'''.format(table = buffered_study_region), engine, geom_col='geom' )
        for p in projections:
            # construct virtual raster table
            vrt = os.path.join(population_folder,f'ghs_{p}.vrt')
            population_raster_clipped   = os.path.join(locale_dir,f'ghs_{ghs_year}_{p}_{locale}.tif')
            population_raster_projected = os.path.join(locale_dir,f'ghs_{ghs_year}_{p}_{locale}_{srid}.tif')
            pop_feature = f'pop_ghs_{ghs_year}'
            print("Global population dataset..."),
            if not os.path.isfile(vrt):
                tif_folder = f'../data/GHS/{p}'
                tif_files = [os.path.join(tif_folder,file) for file in os.listdir(tif_folder) if os.path.splitext(file)[-1] == '.tif']
                gdal.BuildVRT(vrt, tif_files)
                print(f"  has now been indexed ({vrt}).")
            else:
                print(f"  has already been indexed ({vrt}).")
            print("Population data clipped to region..."),
            if not os.path.isfile(population_raster_clipped):
                # extract study region boundary in projection of tiles
                clipping = clipping_boundary.to_crs({'init':'epsg:{}'.format(projections[p])})
                # get clipping boundary values in required order for gdal translate
                bbox = list(clipping.bounds[['minx','maxy','maxx','miny']].values[0])
                # bbox = list(clipping.bounds.values[0])
                ds = gdal.Translate(population_raster_clipped, vrt, projWin = bbox)
                ds = None
                print(f"  has now been created ({population_raster_clipped}).")
            else:
                print(f"  has already been created ({population_raster_clipped}).")
            print("Population data projected for region..."),
            if not os.path.isfile(population_raster_projected):
                # reproject and save the re-projected clipped raster
                # (see config file for reprojection function)
                reproject_raster(inpath = population_raster_clipped,
                              outpath = population_raster_projected,
                              new_crs = 'EPSG:{}'.format(srid))
                print(f"  has now been created ({population_raster_projected}).")
            else:
                print(f"  has already been created ({population_raster_projected}).")
            print("Interpolation of population data to urban area...")
            analysis_area = gpd.GeoDataFrame.from_postgis(region_sql,
                                                            engine,
                                                            geom_col='geom')
            print("  - processing population zonal statistics...")
            pop_feature = "urban_study_region_pop"
            if not engine.has_table(pop_feature):
                result = zonal_stats(analysis_area,population_raster_projected,stats="sum", all_touched=True,geojson_out=True, nodata=-200)
                print("  - creating additional required fields...")
                urban_pop = gpd.GeoDataFrame.from_features(result)
                urban_pop.rename(columns={'sum':'urban_pop_est'},inplace=True)
                urban_pop['area_sqkm'] = urban_pop['geometry'].area/10**6
                urban_pop['pop_per_sqkm'] = urban_pop['urban_pop_est'] / urban_pop['area_sqkm']
                # Create WKT geometry (postgis won't read shapely geometry)
                urban_pop["geometry"] = [MultiPolygon([feature]) if type(feature) == Polygon else feature for feature in urban_pop["geometry"]]
                urban_pop['geom'] = urban_pop['geometry'].apply(lambda x: WKTElement(x.wkt, srid=srid))
                urban_pop.drop('geometry', 1, inplace=True)
                # urban_pop.drop('sum', 1, inplace=True)
                # Ensure all geometries are multipolygons (specifically - can't be mixed type; complicates things)
                print(f"  - copying to postgis ({pop_feature})...")
                # Copy to project Postgis database
                urban_pop.to_sql(pop_feature, engine, if_exists='replace', index=True, dtype={'geom': Geometry('MULTIPOLYGON', srid=srid)})
            else:
                print(f"    - hex grid with population zonal statistics has already been procesed ({pop_feature}).")
    else:
        print(locale)
        xls = pandas.ExcelFile(os.path.join(cwd,'_project_configuration.xlsx'))
        df_local = pandas.read_excel(xls, 'region_settings',index_col=0)
        df_local[locale] = df_local[locale].fillna('')
        for var in [x for x in  df_local.index.values]:
            globals()[var] = df_local.loc[var][locale]
        study_region = '{}_{}_{}'.format(locale,region,year).lower()
        vic_region_sql = f'''
        SELECT "Study region" study_region,
               geom
        FROM {study_region}
        '''
        # region specific output locations
        locale_dir = os.path.join(folder_path,'study_region','{}'.format(study_region))
        buffered_study_region = '{}_{}{}'.format(study_region,study_buffer,units)
        engine = create_engine(f"postgresql://{db_user}:{db_pwd}@{db_host}/{db}")
        clipping_boundary = gpd.GeoDataFrame.from_postgis('''SELECT geom FROM {table}'''.format(table = buffered_study_region), engine, geom_col='geom' )
        for p in projections:
            # construct virtual raster table
            vrt = os.path.join(population_folder,f'ghs_{p}.vrt')
            population_raster_clipped   = os.path.join(locale_dir,f'ghs_{ghs_year}_{p}_{locale}.tif')
            population_raster_projected = os.path.join(locale_dir,f'ghs_{ghs_year}_{p}_{locale}_{srid}.tif')
            pop_feature = f'pop_ghs_{ghs_year}'
            print("Global population dataset..."),
            if not os.path.isfile(vrt):
                tif_folder = f'../data/GHS/{p}'
                tif_files = [os.path.join(tif_folder,file) for file in os.listdir(tif_folder) if os.path.splitext(file)[-1] == '.tif']
                gdal.BuildVRT(vrt, tif_files)
                print(f"  has now been indexed ({vrt}).")
            else:
                print(f"  has already been indexed ({vrt}).")
            print("Population data clipped to region..."),
            if not os.path.isfile(population_raster_clipped):
                # extract study region boundary in projection of tiles
                clipping = clipping_boundary.to_crs({'init':'epsg:{}'.format(projections[p])})
                # get clipping boundary values in required order for gdal translate
                bbox = list(clipping.bounds[['minx','maxy','maxx','miny']].values[0])
                # bbox = list(clipping.bounds.values[0])
                ds = gdal.Translate(population_raster_clipped, vrt, projWin = bbox)
                ds = None
                print(f"  has now been created ({population_raster_clipped}).")
            else:
                print(f"  has already been created ({population_raster_clipped}).")
            print("Population data projected for region..."),
            if not os.path.isfile(population_raster_projected):
                # reproject and save the re-projected clipped raster
                # (see config file for reprojection function)
                reproject_raster(inpath = population_raster_clipped,
                              outpath = population_raster_projected,
                              new_crs = 'EPSG:{}'.format(srid))
                print(f"  has now been created ({population_raster_projected}).")
            else:
                print(f"  has already been created ({population_raster_projected}).")
            print("Interpolation of population data to urban area...")
            analysis_area = gpd.GeoDataFrame.from_postgis(vic_region_sql,
                                                            engine,
                                                            geom_col='geom')
            print("  - processing population zonal statistics...")
            pop_feature = "urban_study_region_pop"
            if not engine.has_table(pop_feature):
                result = zonal_stats(analysis_area,population_raster_projected,stats="sum", all_touched=True,geojson_out=True, nodata=-200)
                print("  - creating additional required fields...")
                urban_pop = gpd.GeoDataFrame.from_features(result)
                urban_pop.rename(columns={'sum':'urban_pop_est'},inplace=True)
                urban_pop['area_sqkm'] = urban_pop['geometry'].area/10**6
                urban_pop['pop_per_sqkm'] = urban_pop['urban_pop_est'] / urban_pop['area_sqkm']
                # Create WKT geometry (postgis won't read shapely geometry)
                urban_pop["geometry"] = [MultiPolygon([feature]) if type(feature) == Polygon else feature for feature in urban_pop["geometry"]]
                urban_pop['geom'] = urban_pop['geometry'].apply(lambda x: WKTElement(x.wkt, srid=srid))
                urban_pop.drop('geometry', 1, inplace=True)
                # urban_pop.drop('sum', 1, inplace=True)
                # Ensure all geometries are multipolygons (specifically - can't be mixed type; complicates things)
                print(f"  - copying to postgis ({pop_feature})...")
                # Copy to project Postgis database
                urban_pop.to_sql(pop_feature, engine, if_exists='replace', index=True, dtype={'geom': Geometry('MULTIPOLYGON', srid=srid)})
            else:
                print(f"    - hex grid with population zonal statistics has already been procesed ({pop_feature}).")

    # grant access to the tables just created
    engine.execute(grant_query)
    df = pandas.read_sql(count_sql, con=engine, index_col=None)
    df.to_csv(f'./../data/study_region/{study_region}/osm_audit_{locale}_{date_yyyymmdd}.csv')

    # output to completion log
    script_running_log(script, task, start, locale)
    engine.dispose()

if __name__ == '__main__':
    main()

max_stars_repo_name: shiqin-liu/global-indicators

max_issues_repo_name: nicholas-david/global-indicators

max_forks_repo_name: nicholas-david/global-indicators


text:
#!/usr/bin/env python
# license removed for brevity
import rospy
# because we need the command-line arguments
import sys

from std_msgs.msg import String
from rospy.numpy_msg import numpy_msg

from sensor_msgs.msg import Imu
from turtlesim.msg import Pose
from geometry_msgs.msg import TwistWithCovarianceStamped
# Now we've got our own message type.
from spine_controller.msg import InvkinControlCommand

class SerialTxControlTopic:

  #contador de secuencia por sensor
  seq = 0
  seq2 = 0
  seq3 = 0

  # invkin_ref_state =[device, count])
    # devices:
      # no device:  0
      # laser scan: 1
      # imu:        2
      # wheels:     3

  def invkin_laser_publisher_callback(self, message):
    rate = rospy.Rate(2) # ROS Rate at 5Hz
    SerialTxControlTopic.seq += 1
    hello_str = InvkinControlCommand(invkin_control = [message.x, message.y, message.theta, message.linear_velocity, message.angular_velocity], \
          invkin_ref_state =[1, SerialTxControlTopic.seq]) #laser scan = 1
    #hello_str = "invkin_control: 0\n %s" % rospy.get_time()
    #rospy.loginfo(hello_str)
    self.pub.publish(hello_str)
    rate.sleep()

  def invkin_imu_publisher_callback(self, message):
    SerialTxControlTopic.seq2 += 1
    hello_str = InvkinControlCommand(invkin_control = [message.orientation.z, message.orientation.w, message.angular_velocity.z], \
          invkin_ref_state =[2, SerialTxControlTopic.seq2]) #imu = 2
    #hello_str = "invkin_control: 0\n %s" % rospy.get_time()
    #rospy.loginfo(hello_str)
    self.pub.publish(hello_str)

  def invkin_wheel_publisher_callback(self, message):
    SerialTxControlTopic.seq3 += 1
    hello_str = InvkinControlCommand(invkin_control = [message.twist.twist.linear.x, message.twist.twist.angular.z], \
          invkin_ref_state =[3, SerialTxControlTopic.seq3]) #wheel odom = 3
    #hello_str = "invkin_control: 0\n %s" % rospy.get_time()
    #rospy.loginfo(hello_str)
    self.pub.publish(hello_str)

  def invkin_publisher(self, topic_name):
    #inicio el nodo publicador
    rospy.init_node('invkin_publisher', anonymous=True)
    #declaracion del topico donde se va a publicar
    pub = rospy.Publisher('invkin_tx_commands', InvkinControlCommand, queue_size=10)
    #declaracion del topico que se va a escuchar para recibir el movimiento de Wheels encoders
    sub = rospy.Subscriber('/turtle1/pose', Pose, self.invkin_laser_publisher_callback)
    #declaracion del topico que se va a escuchar para recibir el IMU
    sub2 = rospy.Subscriber('/turtle1/sensors/imu', Imu, self.invkin_imu_publisher_callback)
    #declaracion del topico que se va a escuchar para recibir el movimiento de Wheels encoders
    sub3 = rospy.Subscriber('/turtle1/sensors/twist', TwistWithCovarianceStamped, self.invkin_wheel_publisher_callback)

        # finishing setup.

    print("Running invkin_publisher node in invkin_tx_commands topic")

    # and return the publisher so that the callback can use it.
    # also needs to have the serial port available to the callback.
    return pub

  # The constructor calls a helper to initialize everything, and stores the
  # resulting publisher and serial port object that's created.
  def __init__(self, topic_name):
    self.pub = self.invkin_publisher(topic_name)
    # and that's all.

if __name__ == '__main__':
        #invkin_publisher()
        s_tx = SerialTxControlTopic(sys.argv[1])
        #rospy.spin()
        try:
            rospy.spin()
        except KeyboardInterrupt:
            pass
        except rospy.ROSInterruptException:
            pass


text:
from .commen_preprocess import *
from string import punctuation
from sklearn import *
from sklearn.feature_extraction.text import TfidfVectorizer
from string import punctuation
from gensim.parsing.preprocessing import STOPWORDS
from nltk.stem.porter import *
ps = PorterStemmer()
from scipy.sparse import vstack, hstack

### stopwords and punctuations are not removed but text is cleaned and stemmed
def glove_tokenize_norem(text):
    #text = tokenizer(text)
    text=clean(text, remove_stopwords=False, remove_punctuations=False)
    words = text.split()
    words =[ps.stem(word) for word in words]
    return words

####stopwords and punctuations are removed along with that text is cleaned ans stemmed
def glove_tokenize(text):
    #text = tokenizer(text)
    text=clean(text, remove_stopwords=False, remove_punctuations=False)
    text = ''.join([c for c in text if c not in punctuation])
    words = text.split()
    words = [word for word in words if word not in STOPWORDS]
    words =[ps.stem(word) for word in words]
    return words

### this is the glove tokenize for embedding
def glove_tokenize_embed(text):
    #text = tokenizer(text)
    text=clean(text, remove_stopwords=False, remove_punctuations=False)
    text = ''.join([c for c in text if c not in punctuation])
    words = text.split()
    words = [word for word in words if word not in STOPWORDS]
    return words

reason: from scipy


text:
import numpy as np

import utils
from models import MeanFieldVINN, VanillaNN

def run_vcl(hidden_size, num_epochs, data_generator, coreset_method, coreset_size=0, batch_size=None, single_head=True):
    """It runs the variational continual learning algorithm presented in "Variational Continual Learning" (2018) by
    Cuong V. Nguyen et al.

    :param hidden_size:
    :param num_epochs:
    :param data_generator:
    :param coreset_method:
    :param coreset_size:
    :param batch_size:
    :param single_head:
    :return:
    """
    in_dim, out_dim = data_generator.get_dims()

    # TODO: what is difference between coresets and testsets? Maybe coresets are training sets?
    x_coresets, y_coresets = [], []

    x_testsets, y_testsets = [], []

    all_acc = np.array([])

    # max_iter corresponds to the number of tasks (?)
    for task_id in range(data_generator.max_iter):
        x_train, y_train, x_test, y_test = data_generator.next_task()

        x_testsets.append(x_test)
        y_testsets.append(y_test)

        # Set the readout head to train
        head = 0 if single_head else task_id

        bsize = x_train.shape[0] if (batch_size is None) else batch_size

        # Train network with maximum likelihood to initialize first model
        if task_id == 0:
            ml_model = VanillaNN(in_dim, hidden_size, out_dim, x_train.shape[0])

            ml_model.train(x_train, y_train, task_id, num_epochs, bsize)

            mf_weights = ml_model.get_weights()

            mf_variances = None

            ml_model.close_session()

        # Select coreset if needed
        if coreset_size > 0:
            x_coresets, y_coresets, x_train, y_train = coreset_method(x_coresets, y_coresets, x_train, y_train,
                                                                      coreset_size)

        # Train on non-coreset data
        mf_model = MeanFieldVINN(in_dim, hidden_size, out_dim, x_train.shape[0], prev_means=mf_weights,
                                 prev_log_variances=mf_variances)
        mf_model.train(x_train, y_train, head, num_epochs, bsize)
        mf_weights, mf_variances = mf_model.get_weights()

        # Incorporate coreset data and make prediction
        acc = utils.get_scores(mf_model, x_testsets, y_testsets, x_coresets, y_coresets, hidden_size, num_epochs,
                               single_head, batch_size)

        all_acc = utils.concatenate_results(acc, all_acc)

        mf_model.close_session()

    return all_acc

max_stars_repo_name: nbro/variational-continual-learning

max_issues_repo_name: nbro/variational-continual-learning

max_forks_repo_name: nbro/variational-continual-learning


text:
import numpy as np
from numpy import einsum
from .MaterialBase import Material
from Florence.Tensor import trace, Voigt

class MooneyRivlin_1(Material):
    """ Polyconvex compressible MooneyRivlin material model based on the energy:

            W = alpha*C:I+beta*G:I+lambda/2*(J-1)**2-4*beta*J-2*alpha*lnJ - (3*alpha-beta)

        where at the origin (alpha + beta) = mu/2
        """

    def __init__(self, ndim, **kwargs):
        mtype = type(self).__name__
        super(MooneyRivlin_1, self).__init__(mtype,ndim,**kwargs)

        self.alpha = self.mu/4.0
        self.beta = self.mu/4.0

        self.is_transversely_isotropic = False
        self.energy_type = "internal_energy"
        self.nature = "nonlinear"
        self.fields = "mechanics"

        if self.ndim==3:
            self.H_VoigtSize = 6
        elif self.ndim==2:
            self.H_VoigtSize = 3

        # LOW LEVEL DISPATCHER
        self.has_low_level_dispatcher = False

    def Hessian(self,StrainTensors,ElectricFieldx=0,elem=0,gcounter=0):

        lamb = self.lamb

        I = StrainTensors['I']
        J = StrainTensors['J'][gcounter]
        b = StrainTensors['b'][gcounter]

        # Actual version
        # d = np.einsum
        # H_Voigt = Voigt( 4.0*beta/J*d('ij,kl',b,b) - 2.0*beta/J*( d('ik,jl',b,b) + d('il,jk',b,b) ) +\
        #   (lamb+4.0*beta+4.0*alpha/J)*d('ij,kl',I,I) + 2.0*(lamb*(J-1.0) -4.0*beta -2.0*alpha/J)*d('ij,kl',I,I) -\
        #   1.0*(lamb*(J-1.0) -4.0*beta -2.0*alpha/J)*(d('ik,jl',I,I)+d('il,jk',I,I)) ,1)

        # # Simplified version
        # H_Voigt = 2.0*beta/J*( 2.0*einsum('ij,kl',b,b) - einsum('ik,jl',b,b) - einsum('il,jk',b,b) ) + \
        #   (lamb*(2.0*J-1.0) -4.0*beta)*einsum('ij,kl',I,I) - \
        #   (lamb*(J-1.0) -4.0*beta -2.0*alpha/J)*( einsum('ik,jl',I,I) + einsum('il,jk',I,I) )

        # Further simplified version
        H_Voigt = 2.0*self.beta/J*( 2.0*einsum('ij,kl',b,b) - einsum('ik,jl',b,b) - einsum('il,jk',b,b) ) + \
            (lamb*(2.0*J-1.0) -4.0*self.beta)*self.Iijkl - \
            (lamb*(J-1.0) -4.0*self.beta -2.0*self.alpha/J)*self.Iikjl

        H_Voigt = Voigt(H_Voigt,1)

        self.H_VoigtSize = H_Voigt.shape[0]

        return H_Voigt

    def CauchyStress(self,StrainTensors,ElectricFieldx,elem=0,gcounter=0):

        lamb = self.lamb

        I = StrainTensors['I']
        J = StrainTensors['J'][gcounter]
        b = StrainTensors['b'][gcounter]

        if self.ndim == 3:
            trb = trace(b)
        elif self.ndim == 2:
            trb = trace(b) + 1

        # stress = 2.0*alpha/J*b+2.0*beta/J*(trace(b)*b - np.dot(b,b)) + (lamb*(J-1.0)-4.0*beta-2.0*alpha/J)*I
        stress = 2.0*self.alpha/J*b+2.0*self.beta/J*(trb*b - np.dot(b,b)) + (lamb*(J-1.0)-4.0*self.beta-2.0*self.alpha/J)*I
        # print stress
        return stress

    def ElectricDisplacementx(self,StrainTensors,ElectricFieldx):
        ndim = StrainTensors['I'].shape[0]
        return np.zeros((ndim,1))

max_stars_repo_name: jdlaubrie/florence

max_issues_repo_name: jdlaubrie/florence

max_forks_repo_name: jdlaubrie/florence


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Jul  7 17:35:52 2016

@author: avigan
"""

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fft

from . import aperture

def single_power(dim, Dpup, error, power=-2., fmin=None, fmax=None):
    '''
    Produces a phase screen for a power law PSD

    Parameters
    ----------
    dim : int
        Dimension of the array, in pixel

    Dpup : int
        Diameter of the pupil, in pixel

    error : float
        Expected standard deviation of the phase scree, in nm

    power : float, optional
        Power of the law. Default is -2, typically corresponding to good quality
        polished optics

    fmin : float, optional
        Minimum frequency range, in cycle/pupil. The PSD values below that minimum
        frequency will be set to 0. Default value is 1.

    fmax : float, optional
        Maximum frequency range, in cycle/pupil. The PSD values below that maximum
        frequency will be set to 0. Default value is None

    Returns
    -------
    wf : array
        Phase screen expressed in the same unit as error, and with a standard
        deviation equal to error
    '''
    x = np.arange(dim, dtype=np.float64) - dim//2
    y = np.arange(dim, dtype=np.float64) - dim//2
    xx, yy = np.meshgrid(x, y)
    rr = np.sqrt(xx**2 + yy**2)

    # mask out null frequency
    rr[dim//2, dim//2] = rr[dim//2, dim//2+1]

    # frequencies and PSD
    freq = rr / dim * Dpup
    psd  = freq**power

    # filter out some frequencies
    if (fmin is None):
        fmin = freq.min()

    if (fmax is None) or (fmax > Dpup/2):
        fmax = Dpup/2

    psd[(freq < fmin) | (freq > fmax)] = 1e-20
    filt = np.sqrt(psd)

    np.random.seed(12345)
    rand = np.empty((dim, dim), dtype=np.complex)
    rand.real = np.random.normal(size=(dim, dim)) * filt
    rand.imag = np.random.normal(size=(dim, dim)) * filt

    rand   = fft.fftshift(rand)
    screen = fft.ifft2(rand)
    screen = fft.fftshift(screen)

    wf = screen.real
    wf *= error / wf.std()
    wf -= wf.mean()

    return wf

if __name__ == '__main__':
    Dpup   = 700
    dim    = 2048
    error  = 10.

    wf = single_power(dim, Dpup, error, fmin=1, fmax=300)

    print(wf.std())
    plt.imshow(wf)

max_stars_repo_name: avigan/Python-utils

max_issues_repo_name: avigan/Python-utils

max_forks_repo_name: avigan/Python-utils

reason: import numpy,import scipy


text:
#! /usr/bin/env python3
import numpy as np
import tkinter as tk

def coords2xbm(coords, returnOffset=False, joinstr=', '):
    """
    Draw an XBM-formatted image from coordinates.

    :param coords: The coordinates to be set as image foreground
    :type coords: (N,2)-shaped numpy array with x-values in first and y-values in second column
    :param returnOffset: Flag whether to return the offset or not
    :type returnOffset: bool
    :param joinstr: The string to be used for joining the byte values; defaults to ", "
    :type joinstr: str
    :return: if ``returnOffset``, a tuple of a tuple of the x- and y-offset and the image string, else only the image string
    """
    # Assess coordinate range
    x_min = coords[:,0].min()
    x_max = coords[:,0].max()
    y_min = coords[:,1].min()
    y_max = coords[:,1].max()

    n_cols = np.ceil(x_max - x_min + 1).astype(np.uint)
    n_bytes = np.ceil(n_cols / 8).astype(np.uint)
    n_rows = np.ceil(y_max - y_min + 1).astype(np.uint)

    # Normalize coordinates (eliminate offset)
    coords = np.round(coords - np.array([[x_min, y_min]])).astype(np.uint)

    # Write pixel data
    bm = np.zeros([n_rows, n_bytes], dtype=np.uint8)
    for x, y in coords:
        byte = np.floor(x / 8).astype(np.uint)
        bit = (x % 8).astype(np.uint8)
        bm[y,byte] |= 1 << bit

    # Convert pixel data to XBM format
    bmx = joinstr.join("{:#x}".format(b) for b in bm.flat)
    xbm = "#define im_width {:d}\n#define im_height {:d}\nstatic char im_bits[] = {{\n{}\n}};".format(n_cols, n_rows, bmx)

    if returnOffset:
        return (x_min, y_min), xbm
    return xbm

if __name__ == "__main__":
    coords = np.array([[0,0], [2,0], [4,0], [6,0], [1,2], [2,2], [4,2], [5,2], [1,3], [2,3], [4,3], [5,3], [3,5], [3,6], [1,7], [5,7], [2,8], [4,8], [3,8]], dtype=np.uint)
    xbm = coords2xbm(coords)
    print(xbm)

    with open("out.xbm", "w") as f:
        f.write(xbm)

    root = tk.Tk()
    canvas = tk.Canvas(root, highlightthickness=0)
    canvas.pack()
    bitmap = tk.BitmapImage(data=xbm, master=root)
    canvas.bm = bitmap
    canvas.create_image(0, 0, image=bitmap, anchor=tk.NW)
    root.mainloop()

max_stars_repo_name: BeastyBlacksmith/pyama

max_issues_repo_name: BeastyBlacksmith/pyama

max_forks_repo_name: BeastyBlacksmith/pyama