text:
#!/usr/bin/env python3

import math

import numpy as np
import numpy.linalg as LA
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d

def gold_spiral_sampling_patch(v, alpha, num_pts):
    v1 = orth(v)
    v2 = np.cross(v, v1)
    v, v1, v2 = v[:, None], v1[:, None], v2[:, None]
    indices = np.arange(num_pts) + 0.66
    phi = np.arccos(1 + (math.cos(alpha) - 1) * indices / num_pts)
    theta = np.pi * (1 + 5 ** 0.5) * indices
    r = np.sin(phi)
    return (v * np.cos(phi) + r * (v1 * np.cos(theta) + v2 * np.sin(theta))).T

def orth(v):
    x, y, z = v
    o = np.array([0.0, -z, y] if abs(x) < abs(y) else [-z, 0.0, x])
    o /= LA.norm(o)
    return o

arc = math.pi / 2
for level in range(7):
    result = 0
    pts = gold_spiral_sampling_patch(np.array([0, 0, 1]), arc, 64)
    for p in gold_spiral_sampling_patch(np.array([0, 0, 1]), arc, 200000):
        theta = pts @ p
        result = max(result, min(np.arccos(np.abs(theta))))

    print("level:", level)
    print("  acc:", result)
    print("  rad:", result * 1.2)
    print("  deg:", result / 2 / np.pi * 360)
    arc = result * 1.2

ax = plt.figure(figsize=(15, 12)).add_subplot(111, projection="3d")
cb = ax.scatter(pts[:, 0], pts[:, 1], pts[:, 2], c=pts[:, 2])
ax.scatter(0, 0, 0.5, c="red", marker="^")
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
plt.colorbar(cb)
plt.show()

max_stars_repo_name: keatonkraiger/neurvps


text:
#!/usr/bin/env python3

import os
import glob
import torch
import random
import numpy as np
import pytorchrl as prl
from pytorchrl.agent.env import VecEnv
from pytorchrl.envs.animal_olympics.animal_olympics_env_factory import animal_train_env_factory
from code_examples.train_animalai.ppod.train import get_args

def enjoy():

    args = get_args()

    # Define single copy of the environment
    arena_file = os.path.dirname(os.path.abspath(__file__)) + "/arenas/"
    env, action_space, obs_space = VecEnv.create_factory(
        env_fn=animal_train_env_factory,
        env_kwargs={
            "arenas_dir": arena_file,
            "frame_skip": args.frame_skip,
            "frame_stack": args.frame_stack,
            "inference": True,
        }, vec_env_size=1)

    # Define agent device and agent
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # Start recording
    env = env()
    obs = env.reset()

    demos_list = glob.glob(args.demos_dir + '/*.npz')
    demo_name = random.choice(demos_list)
    demo = np.load(demo_name)
    done, episode_reward, step = False, 0, 0
    length_demo = demo[prl.ACT].shape[0]
    print("LOADING DEMO: {}, LENGTH {}".format(demo_name, length_demo))

    # Execute episodes
    while not done:

        obs, reward, done, info = env.step(torch.Tensor(demo[prl.ACT][step]).view(1, -1).to(device))
        episode_reward += reward
        step += 1
        print(step)

        if step == length_demo:
            done = True

        if done:
            print("EPISODE: reward: {}".format(episode_reward.item()), flush=True)
            done, episode_reward, step = False, 0, 0
            obs = env.reset()
            demo_name = random.choice(demos_list)
            demo = np.load(demo_name)
            length_demo = demo[prl.ACT].shape[0]
            print("LOADING DEMO: {}, LENGTH {}".format(demo_name, length_demo))

if __name__ == "__main__":
    enjoy()

ext: py

max_stars_repo_name: PyTorchRL/pytorchrl

max_issues_repo_name: PyTorchRL/pytorchrl

max_forks_repo_name: PyTorchRL/pytorchrl

reason: import numpy


text:
import os
import sys
from multiprocessing import Pool
from shutil import get_terminal_size
import time
import argparse

import numpy as np
import cv2

from utilgan import img_list, basename

try:  # progress bar for notebooks
    get_ipython().__class__.__name__
    from progress_bar import ProgressIPy as ProgressBar
except:  # normal console
    from progress_bar import ProgressBar

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--in_dir', help='Input directory')
parser.add_argument('-o', '--out_dir', help='Output directory')
parser.add_argument('-s', '--size', type=int, default=512, help='Output directory')
parser.add_argument('--step', type=int, default=None, help='Step')
parser.add_argument('--workers', type=int, default=8, help='number of workers (8, as of cpu#)')
parser.add_argument('--png_compression', type=int, default=1, help='png compression (0 to 9; 0 = uncompressed, fast)')
parser.add_argument('--jpg_quality', type=int, default=95, help='jpeg quality (0 to 100; 95 = max reasonable)')
a = parser.parse_args()

# https://pillow.readthedocs.io/en/3.0.x/handbook/image-file-formats.html#jpeg
# image quality = from 1 (worst) to 95 (best); default 75. Values above 95 should be avoided;
# 100 disables portions of the JPEG compression algorithm => results in large files with hardly any gain in image quality.

# CV_IMWRITE_PNG_COMPRESSION from 0 to 9. A higher value means a smaller size and longer
# compression time. If read raw images during training, use 0 for faster IO speed.

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def worker(path, save_folder, crop_size, step, min_step):
    img_name = basename(path)
    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)

    # convert monochrome to RGB if needed
    if len(img.shape) == 2:
        img = img[:, :, np.newaxis]
    if img.shape[2] == 1:
        img = img[:, :, (0, 0, 0)]
    h, w, c = img.shape

    ext = 'png' if img.shape[2] == 4 else 'jpg'

    min_size = min(h, w)
    if min_size < crop_size:
        h = int(h * crop_size / min_size)
        w = int(w * crop_size / min_size)
        img = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)

    h_space = np.arange(0, h - crop_size + 1, step)
    if h - (h_space[-1] + crop_size) > min_step:
        h_space = np.append(h_space, h - crop_size)
    w_space = np.arange(0, w - crop_size + 1, step)
    if w - (w_space[-1] + crop_size) > min_step:
        w_space = np.append(w_space, w - crop_size)

    index = 0
    for x in h_space:
        for y in w_space:
            index += 1
            crop_img = img[x:x + crop_size, y:y + crop_size, :]
            crop_img = np.ascontiguousarray(crop_img)
            if ext == 'png':
                cv2.imwrite(os.path.join(save_folder, '%s-s%03d.%s' % (img_name, index, ext)), crop_img,
                            [cv2.IMWRITE_PNG_COMPRESSION, a.png_compression])
            else:
                cv2.imwrite(os.path.join(save_folder, '%s-s%03d.%s' % (img_name, index, ext)), crop_img,
                            [cv2.IMWRITE_JPEG_QUALITY, a.jpg_quality])
    return 'Processing {:s} ...'.format(img_name)

def main():
    """A multi-thread tool to crop sub images."""
    input_folder = a.in_dir
    save_folder = a.out_dir
    n_thread = a.workers
    crop_size = a.size
    step = a.size // 2 if a.step is None else a.step
    min_step = a.size // 8

    os.makedirs(save_folder, exist_ok=True)

    images = img_list(input_folder, subdir=True)

    def update(arg):
        pbar.upd(arg)

    pbar = ProgressBar(len(images))

    pool = Pool(n_thread)
    for path in images:
        pool.apply_async(worker,
                         args=(path, save_folder, crop_size, step, min_step),
                         callback=update)
    pool.close()
    pool.join()
    print('All subprocesses done.')

if __name__ == '__main__':
    # workaround for multithreading in jupyter console
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"
    main()


text:
from PIL import Image
import os
import os.path
import numpy as np
import pickle
import scipy

from .vision import VisionDataset
from .utils import  download_and_extract_archive

def padWithZeros(X, margin=2):

    ## From: https://github.com/gokriznastic/HybridSN/blob/master/Hybrid-Spectral-Net.ipynb
    newX = np.zeros((X.shape[0] + 2 * margin, X.shape[1] + 2* margin, X.shape[2]))
    x_offset = margin
    y_offset = margin
    newX[x_offset:X.shape[0] + x_offset, y_offset:X.shape[1] + y_offset, :] = X
    return newX

def createImageCubes(X, y, windowSize=5, removeZeroLabels = True):

     ## From: https://github.com/gokriznastic/HybridSN/blob/master/Hybrid-Spectral-Net.ipynb
    margin = int((windowSize - 1) / 2)
    zeroPaddedX = padWithZeros(X, margin=margin)
    # split patches
    patchesData = np.zeros((X.shape[0] * X.shape[1], windowSize, windowSize, X.shape[2]), dtype=np.uint8)
    patchesLabels = np.zeros((X.shape[0] * X.shape[1]), dtype=np.uint8)
    patchIndex = 0
    for r in range(margin, zeroPaddedX.shape[0] - margin):
        for c in range(margin, zeroPaddedX.shape[1] - margin):
            patch = zeroPaddedX[r - margin:r + margin + 1, c - margin:c + margin + 1]
            patchesData[patchIndex, :, :, :] = patch
            patchesLabels[patchIndex] = y[r-margin, c-margin]
            patchIndex = patchIndex + 1
    if removeZeroLabels:
        patchesData = patchesData[patchesLabels>0,:,:,:]
        patchesLabels = patchesLabels[patchesLabels>0]
        patchesLabels -= 1
    return patchesData, patchesLabels

class IndianPines(VisionDataset):

    data_url = "http://www.ehu.eus/ccwintco/uploads/6/67/Indian_pines_corrected.mat"
    label_url = "http://www.ehu.eus/ccwintco/uploads/c/c4/Indian_pines_gt.mat"
    file_data = "Indian_pines_corrected.mat"
    file_tgt = "Indian_pines_gt.mat"
    def __init__(self, root, train=True, transform=None, target_transform=None,
                 download=False,split=0.95):

        super(IndianPines, self).__init__(root, transform=transform, target_transform=target_transform)

        self.train = train  # training set or test set

        self.data = np.array(scipy.io.loadmat(root+data_url.split('/')[-1])[data_url.split('/')[-1].split('.')[0].lower()])
        self.targets = np.array(scipy.io.loadmat(root+label_url.split('/')[-1])[label_url.split('/')[-1].split('.')[0].lower()])
        self.data, self.targets = createImageCubes(self.data,self.targets, windowSize=7)
        self.data = self.data.transpose((0,3,1,2))

        length = self.data.shape[0]
        if self.train:
            self.data,self.targets = self.data[:int(np.ceil(split*length)),:,:,:],self.targets[:int(np.ceil(split*length))]

        else:
            self.data,self.targets = self.data[:int(-np.ceil(split*length)),:,:,:],
                                                    self.targets[:int(-np.ceil(split*length))]

    def __getitem__(self, idx):

      return self.data[idx,:,:,:] , self.targets[idx]

    def __len__(self):
        return self.data.shape[0]

    def download(self):
        download_and_extract_archive(self.data_url, self.root, filename=self.file_data)
        download_and_extract_archive(self.label_url, self.root, filename=self.file_tgt)

    def extra_repr(self):
        return "Split: {}".format("Train" if self.train is True else "Test")

max_stars_repo_name: IEM-Computer-Vision/vision

max_issues_repo_name: IEM-Computer-Vision/vision

max_forks_repo_name: IEM-Computer-Vision/vision

reason: import numpy,import scipy


text:
import time

import numpy as np
import pandas as pd
from pandas import testing as tm
import pytest
import requests

from pandas_datareader._testing import skip_on_exception
from pandas_datareader._utils import RemoteDataError
from pandas_datareader.wb import (
    WorldBankReader,
    download,
    get_countries,
    get_indicators,
    search,
)

pytestmark = pytest.mark.stable

class TestWB(object):
    def test_wdi_search(self):

        # Test that a name column exists, and that some results were returned
        # ...without being too strict about what the actual contents of the
        # results actually are.  The fact that there are some, is good enough.

        result = search("gdp.*capita.*constant")
        assert result.name.str.contains("GDP").any()

        # check cache returns the results within 0.5 sec
        current_time = time.time()
        result = search("gdp.*capita.*constant")
        assert result.name.str.contains("GDP").any()
        assert time.time() - current_time < 0.5

        result2 = WorldBankReader().search("gdp.*capita.*constant")
        session = requests.Session()

        result3 = search("gdp.*capita.*constant", session=session)
        result4 = WorldBankReader(session=session).search("gdp.*capita.*constant")

        for result in [result2, result3, result4]:
            assert result.name.str.contains("GDP").any()

    def test_wdi_download(self):

        # Test a bad indicator with double (US), triple (USA),
        # standard (CA, MX), non standard (KSV),
        # duplicated (US, US, USA), and unknown (BLA) country codes

        # ...but NOT a crash inducing country code (World Bank strips pandas
        #    users of the luxury of laziness because they create their
        #    own exceptions, and don't clean up legacy country codes.
        # ...but NOT a retired indicator (user should want it to error).

        cntry_codes = ["CA", "MX", "USA", "US", "US", "KSV", "BLA"]
        inds = ["NY.GDP.PCAP.CD", "BAD.INDICATOR"]

        # These are the expected results, rounded (robust against
        # data revisions in the future).
        expected = {
            "NY.GDP.PCAP.CD": {
                ("Canada", "2004"): 32000.0,
                ("Canada", "2003"): 28000.0,
                ("Kosovo", "2004"): 2000.0,
                ("Kosovo", "2003"): 2000.0,
                ("Mexico", "2004"): 7000.0,
                ("Mexico", "2003"): 7000.0,
                ("United States", "2004"): 42000.0,
                ("United States", "2003"): 39000.0,
            }
        }
        expected = pd.DataFrame(expected)
        expected = expected.sort_index()

        result = download(
            country=cntry_codes, indicator=inds, start=2003, end=2004, errors="ignore"
        )
        result = result.sort_index()

        # Round, to ignore revisions to data.
        result = np.round(result, decimals=-3)

        expected.index.names = ["country", "year"]
        tm.assert_frame_equal(result, expected)

        # pass start and end as string
        result = download(
            country=cntry_codes,
            indicator=inds,
            start="2003",
            end="2004",
            errors="ignore",
        )
        result = result.sort_index()

        # Round, to ignore revisions to data.
        result = np.round(result, decimals=-3)
        tm.assert_frame_equal(result, expected)

    def test_wdi_download_str(self):

        # These are the expected results, rounded (robust against
        # data revisions in the future).
        expected = {
            "NY.GDP.PCAP.CD": {
                ("Japan", "2004"): 38000.0,
                ("Japan", "2003"): 35000.0,
                ("Japan", "2002"): 32000.0,
                ("Japan", "2001"): 34000.0,
                ("Japan", "2000"): 39000.0,
            }
        }
        expected = pd.DataFrame(expected)
        expected = expected.sort_index()

        cntry_codes = "JP"
        inds = "NY.GDP.PCAP.CD"
        result = download(
            country=cntry_codes, indicator=inds, start=2000, end=2004, errors="ignore"
        )
        result = result.sort_index()
        result = np.round(result, decimals=-3)

        expected.index.names = ["country", "year"]
        tm.assert_frame_equal(result, expected)

        result = WorldBankReader(
            inds, countries=cntry_codes, start=2000, end=2004, errors="ignore"
        ).read()
        result = result.sort_index()
        result = np.round(result, decimals=-3)
        tm.assert_frame_equal(result, expected)

    def test_wdi_download_error_handling(self):
        cntry_codes = ["USA", "XX"]
        inds = "NY.GDP.PCAP.CD"

        msg = "Invalid Country Code\\(s\\): XX"
        with pytest.raises(ValueError, match=msg):
            download(
                country=cntry_codes,
                indicator=inds,
                start=2003,
                end=2004,
                errors="raise",
            )

        with pytest.warns(Warning):
            result = download(
                country=cntry_codes, indicator=inds, start=2003, end=2004, errors="warn"
            )
            assert isinstance(result, pd.DataFrame)
            assert len(result), 2

        cntry_codes = ["USA"]
        inds = ["NY.GDP.PCAP.CD", "BAD_INDICATOR"]

        msg = "The provided parameter value is not valid\\. " "Indicator: BAD_INDICATOR"
        with pytest.raises(ValueError, match=msg):
            download(
                country=cntry_codes,
                indicator=inds,
                start=2003,
                end=2004,
                errors="raise",
            )

        with pytest.warns(Warning):
            result = download(
                country=cntry_codes, indicator=inds, start=2003, end=2004, errors="warn"
            )
            assert isinstance(result, pd.DataFrame)
            assert len(result) == 2

    def test_wdi_download_w_retired_indicator(self):

        cntry_codes = ["CA", "MX", "US"]
        # Despite showing up in the search feature, and being listed online,
        # the api calls to GDPPCKD don't work in their own query builder, nor
        # pandas module.  GDPPCKD used to be a common symbol.
        #
        # This test is written to ensure that error messages to pandas users
        # continue to make sense, rather than a user getting some missing
        # key error, cause their JSON message format changed.  If
        #
        # World bank ever finishes the deprecation of this symbol,
        # this test should still pass.

        inds = ["GDPPCKD"]

        with pytest.raises(ValueError):
            result = download(
                country=cntry_codes,
                indicator=inds,
                start=2003,
                end=2004,
                errors="ignore",
            )

            # If it ever gets here, it means WB unretired the indicator.
            # even if they dropped it completely, it would still
            # get caught above
            # or the WB API changed somehow in a really
            # unexpected way.
            if len(result) > 0:  # pragma: no cover
                pytest.skip("Invalid results")

    def test_wdi_download_w_crash_inducing_countrycode(self):

        cntry_codes = ["CA", "MX", "US", "XXX"]
        inds = ["NY.GDP.PCAP.CD"]

        with pytest.raises(ValueError):
            result = download(
                country=cntry_codes,
                indicator=inds,
                start=2003,
                end=2004,
                errors="ignore",
            )

            # If it ever gets here, it means the country code XXX
            # got used by WB
            # or the WB API changed somehow in a really
            # unexpected way.
            if len(result) > 0:  # pragma: no cover
                pytest.skip("Invalid results")

    def test_wdi_get_countries(self):
        result1 = get_countries()
        result2 = WorldBankReader().get_countries()

        session = requests.Session()
        result3 = get_countries(session=session)
        result4 = WorldBankReader(session=session).get_countries()

        for result in [result1, result2, result3, result4]:
            assert "Zimbabwe" in list(result["name"])
            assert len(result) > 100
            assert pd.notnull(result.latitude.mean())
            assert pd.notnull(result.longitude.mean())

    def test_wdi_get_indicators(self):
        result1 = get_indicators()
        result2 = WorldBankReader().get_indicators()

        session = requests.Session()
        result3 = get_indicators(session=session)
        result4 = WorldBankReader(session=session).get_indicators()

        for result in [result1, result2, result3, result4]:
            exp_col = pd.Index(
                [
                    "id",
                    "name",
                    "unit",
                    "source",
                    "sourceNote",
                    "sourceOrganization",
                    "topics",
                ]
            )
            # Column order is version dependent, so check columns are present
            assert sorted(result.columns) == sorted(exp_col)
            assert len(result) > 10000

    @skip_on_exception(RemoteDataError)
    def test_wdi_download_monthly(self):
        expected = {
            "COPPER": {
                ("World", "2012M01"): 8040.47,
                ("World", "2011M12"): 7565.48,
                ("World", "2011M11"): 7581.02,
                ("World", "2011M10"): 7394.19,
                ("World", "2011M09"): 8300.14,
                ("World", "2011M08"): 9000.76,
                ("World", "2011M07"): 9650.46,
                ("World", "2011M06"): 9066.85,
                ("World", "2011M05"): 8959.90,
                ("World", "2011M04"): 9492.79,
                ("World", "2011M03"): 9503.36,
                ("World", "2011M02"): 9867.60,
                ("World", "2011M01"): 9555.70,
            }
        }
        expected = pd.DataFrame(expected)
        # Round, to ignore revisions to data.
        expected = np.round(expected, decimals=-3)
        expected = expected.sort_index()
        cntry_codes = "ALL"
        inds = "COPPER"
        result = download(
            country=cntry_codes,
            indicator=inds,
            start=2011,
            end=2012,
            freq="M",
            errors="ignore",
        )
        result = result.sort_index()
        result = np.round(result, decimals=-3)

        expected.index.names = ["country", "year"]
        tm.assert_frame_equal(result, expected)

        result = WorldBankReader(
            inds, countries=cntry_codes, start=2011, end=2012, freq="M", errors="ignore"
        ).read()
        result = result.sort_index()
        result = np.round(result, decimals=-3)
        tm.assert_frame_equal(result, expected)

    def test_wdi_download_quarterly(self):
        code = "DT.DOD.PUBS.CD.US"
        expected = {
            code: {
                ("Albania", "2012Q1"): 3240539817.18,
                ("Albania", "2011Q4"): 3213979715.15,
                ("Albania", "2011Q3"): 3187681048.95,
                ("Albania", "2011Q2"): 3248041513.86,
                ("Albania", "2011Q1"): 3137210567.92,
            }
        }
        expected = pd.DataFrame(expected)
        # Round, to ignore revisions to data.
        expected = np.round(expected, decimals=-3)
        expected = expected.sort_index()
        cntry_codes = "ALB"
        inds = "DT.DOD.PUBS.CD.US"
        result = download(
            country=cntry_codes,
            indicator=inds,
            start=2011,
            end=2012,
            freq="Q",
            errors="ignore",
        )
        result = result.sort_index()
        result = np.round(result, decimals=-3)

        expected.index.names = ["country", "year"]
        tm.assert_frame_equal(result, expected)

        result = WorldBankReader(
            inds, countries=cntry_codes, start=2011, end=2012, freq="Q", errors="ignore"
        ).read()
        result = result.sort_index()
        result = np.round(result, decimals=-1)
        tm.assert_frame_equal(result, expected)


text:
# This class is written with the reference, and modified with the author's environment.
# Original from "https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html"

# If you want to input multiple data (multi_input_model), adjust the output of "def generate"...

import numpy as np

class DataGenerator(object):
    '''Generates data for Keras'''

    def __init__(self, vert_size = 192, hori_size = 256, color_chn = 3, batch_size = 32, shuffle = True):
        '''Initialization'''
        self.vert_size = vert_size
        self.hori_size = hori_size
        self.color_chn = color_chn
        self.batch_size = batch_size
        self.shuffle = shuffle

    def generate(self, labels, data_dict, list_IDs):
        '''Generates batches of samples'''

        # Infinite loop
        while 1:
            # Generate order of exploration of dataset
            indexes = self.__get_exploration_order(list_IDs)
            # Generate batches
            imax = int(len(indexes)/self.batch_size)

            for i in range(imax):
                # Find list of IDs
                list_IDs_temp = [list_IDs[k] for k in indexes[i*self.batch_size:(i+1)*self.batch_size]]

                # Generate data
                X, y = self.__data_generation(labels, data_dict, list_IDs_temp)

                yield X, y

    def __get_exploration_order(self, list_IDs):
        '''Generates order of exploration'''
        # Find exploration order

        indexes = np.arange(len(list_IDs))

        if self.shuffle == True:
            np.random.shuffle(indexes)

        return indexes

    def __data_generation(self, labels, data_dict, list_IDs_temp):
        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
        # Initialization
        X = np.empty((self.batch_size, self.vert_size, self.hori_size, self.color_chn))
        y = np.empty((self.batch_size), dtype=int)

        # Generate data
        for i, ID in enumerate(list_IDs_temp):
            # Store sample
            X[i, :, :, :] = np.load(data_dict[ID])

            # Store class
            y[i] = labels[ID]

        return X, y


text:
# next is to add accel and see the difference
# add stiffness too
import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
from all_functions import *
import pickle
from warnings import simplefilter

simplefilter(action='ignore', category=FutureWarning)

experiment_ID = "experiment_3_tmp"

mc_run_number = 50
babbling_time = 3
number_of_refinements = 0
stiffness_versions = 9
# errors_all_A_A = np.zeros([2, number_of_refinements+1, mc_run_number])
# errors_all_A_B = np.zeros([2, number_of_refinements+1, mc_run_number])
# errors_all_B_B = np.zeros([2, number_of_refinements+1, mc_run_number])

rewards_all = np.zeros([mc_run_number, stiffness_versions])
energies_all = np.zeros([mc_run_number, stiffness_versions])
exploration_run_no_all = np.zeros([mc_run_number, stiffness_versions])

for stiffness_version_A in range(stiffness_versions):

	MuJoCo_model_name_A="nmi_leg_w_chassis_air_v{}.xml".format(stiffness_version_A)
	MuJoCo_model_name_A_walk="nmi_leg_w_chassis_air_v{}_walk.xml".format(stiffness_version_A)

	random_seed = -1

	for mc_counter in range(mc_run_number):
		random_seed+=1
		# train model_A
		np.random.seed(random_seed) # change the seed for different initial conditions
		tf.random.set_random_seed(random_seed)
		[babbling_kinematics, babbling_activations] =\
			babbling_fcn(
				MuJoCo_model_name=MuJoCo_model_name_A,
				simulation_minutes=babbling_time,
				kinematics_activations_show=False)
		model_A_babble = inverse_mapping_fcn(
			kinematics=babbling_kinematics,
			activations=babbling_activations,
			log_address="./logs/{}/scalars/stiffness_version{}/babble_A_mc_run{}/".format(experiment_ID, stiffness_version_A, mc_counter),
			early_stopping=False)
		cum_kinematics_A_babble = babbling_kinematics
		cum_activations_A_babble = babbling_activations
		#A_A test
		np.random.seed(random_seed) # change the seed for different initial conditions
		tf.random.set_random_seed(random_seed)
		[ best_reward_so_far, all_rewards, best_features_so_far, real_attempt_activations, exploration_run_no ]=\
		learn_to_move_2_fcn(
			MuJoCo_model_name=MuJoCo_model_name_A_walk,
			model=model_A_babble,
			cum_kinematics=cum_kinematics_A_babble,
			cum_activations=cum_activations_A_babble,
			reward_thresh=7,
			refinement=False,
			Mj_render=False)
		[rewardA_A, _, _, _, real_attempt_activations]=\
		feat_to_run_attempt_fcn(
			MuJoCo_model_name=MuJoCo_model_name_A_walk,
			features=best_features_so_far,
			model=model_A_babble,
			feat_show=False,
			Mj_render=False)
		total_energyA_A = np.square(real_attempt_activations).sum(0).sum(0)

		print("traveled distance: ", rewardA_A)
		print("consumed energy: ", total_energyA_A)
		exploration_run_no_all[mc_counter, stiffness_version_A] = exploration_run_no
		rewards_all[mc_counter, stiffness_version_A] = rewardA_A
		energies_all[mc_counter, stiffness_version_A] = total_energyA_A

os.makedirs("./results/{}".format(experiment_ID), exist_ok=True)
np.save("./results/{}/exploration_run_no_all".format(experiment_ID),exploration_run_no_all)
np.save("./results/{}/rewards_all".format(experiment_ID),rewards_all)
np.save("./results/{}/energies_all".format(experiment_ID),energies_all)
#print("best_reward_so_far: ", best_reward_so_far)
## printing the results
# print("errors_mean: ",errors_all_A_A.mean(1))
# print("errors_std: ",errors_all_A_A.std(1))
# print("errors_mean: ",errors_all_A_B.mean(1))
# print("errors_std: ",errors_all_A_B.std(1))

# import pdb; pdb.set_trace()


text:
import numpy as np
import torch
import torch.nn.functional as F
import random
import math
import time
import diffdart as dart
from typing import Dict
from diffdart import DartTorchLossFn, DartTorchTrajectoryRollout, GUITrajectoryTrainer, DartGUI

def main():
  world = dart.simulation.World()
  world.setGravity([0, -9.81, 0])

  # Set up the 2D cartpole

  jumpworm = dart.dynamics.Skeleton()

  rootJoint, root = jumpworm.createTranslationalJoint2DAndBodyNodePair()
  rootJoint.setXYPlane()
  rootShape = root.createShapeNode(dart.dynamics.BoxShape([.1, .1, .1]))
  rootVisual = rootShape.createVisualAspect()
  rootShape.createCollisionAspect()
  rootVisual.setColor([0.7, 0.7, 0.7])
  rootJoint.setControlForceUpperLimit(0, 0)
  rootJoint.setControlForceLowerLimit(0, 0)
  rootJoint.setControlForceUpperLimit(1, 0)
  rootJoint.setControlForceLowerLimit(1, 0)
  rootJoint.setVelocityUpperLimit(0, 1000.0)
  rootJoint.setVelocityLowerLimit(0, -1000.0)
  rootJoint.setVelocityUpperLimit(1, 1000.0)
  rootJoint.setVelocityLowerLimit(1, -1000.0)

  def createTailSegment(parent, color):
    poleJoint, pole = jumpworm.createRevoluteJointAndBodyNodePair(parent)
    poleJoint.setAxis([0, 0, 1])
    poleShape = pole.createShapeNode(dart.dynamics.BoxShape([.05, 0.25, .05]))
    poleVisual = poleShape.createVisualAspect()
    poleVisual.setColor(color)
    poleJoint.setControlForceUpperLimit(0, 100.0)
    poleJoint.setControlForceLowerLimit(0, -100.0)
    poleJoint.setVelocityUpperLimit(0, 10000.0)
    poleJoint.setVelocityLowerLimit(0, -10000.0)

    poleOffset = dart.math.Isometry3()
    poleOffset.set_translation([0, -0.125, 0])
    poleJoint.setTransformFromChildBodyNode(poleOffset)

    poleJoint.setPosition(0, 90 * 3.1415 / 180)
    poleJoint.setPositionUpperLimit(0, 180 * 3.1415 / 180)
    poleJoint.setPositionLowerLimit(0, 0 * 3.1415 / 180)

    poleShape.createCollisionAspect()

    if parent != root:
      childOffset = dart.math.Isometry3()
      childOffset.set_translation([0, 0.125, 0])
      poleJoint.setTransformFromParentBodyNode(childOffset)
    return pole

  tail1 = createTailSegment(root, [182.0/255, 223.0/255, 144.0/255])
  tail2 = createTailSegment(tail1, [223.0/255, 228.0/255, 163.0/255])
  tail3 = createTailSegment(tail2, [221.0/255, 193.0/255, 121.0/255])
  # tail4 = createTailSegment(tail3, [226.0/255, 137.0/255, 79.0/255])

  jumpworm.setPositions(np.array([0, 0, 90, 90, 45]) * 3.1415 / 180)

  world.addSkeleton(jumpworm)

  # Floor

  floor = dart.dynamics.Skeleton()
  floorJoint, floorBody = floor.createWeldJointAndBodyNodePair()
  floorOffset = dart.math.Isometry3()
  floorOffset.set_translation([0, -0.7, 0])
  floorJoint.setTransformFromParentBodyNode(floorOffset)
  floorShape = floorBody.createShapeNode(dart.dynamics.BoxShape([2.5, 0.25, .5]))
  floorVisual = floorShape.createVisualAspect()
  floorVisual.setColor([0.5, 0.5, 0.5])
  floorShape.createCollisionAspect()
  # floorBody.setFrictionCoeff(0)

  world.addSkeleton(floor)

  # Do a benchmark

  jumpworm.setPosition(1, -0.14)
  world.step()

  snapshot: dart.neural.BackpropSnapshot = dart.neural.forwardPass(world)
  snapshot.benchmarkJacobians(world, 100)

  """
  gui = DartGUI()
  gui.stateMachine().renderWorld(world)
  gui.serve(8080)
  gui.stateMachine().blockWhileServing()
  """

if __name__ == "__main__":
  main()

max_stars_repo_name: dylanturpin/nimblephysics


text:
import numpy as np
import tensorflow as tf
from random import randint
from tf_agents.environments.py_environment import PyEnvironment
from tf_agents.environments.tf_py_environment import TFPyEnvironment
from tf_agents.specs import array_spec, TensorSpec
from tf_agents.trajectories import time_step as ts
from tf_agents.trajectories.time_step import TimeStep
from tf_agents.agents import DqnAgent
from typing import Callable
from tf_agents.networks.q_network import QNetwork
from tf_agents.replay_buffers.tf_uniform_replay_buffer import TFUniformReplayBuffer
from tensorflow.python.framework.tensor_spec import BoundedTensorSpec
from tf_agents.trajectories import trajectory
from statistics import mean
import pickle

def set_up_grid():
    # creates 5x5 grid with random goal
    np_grid = np.zeros((5, 5), dtype=np.int32)
    goal = (randint(0, 4), randint(0, 4))
    while goal == (0, 0) or goal == (4, 4):
        goal = (randint(0, 4), randint(0, 4))
    print('goal= ', goal)
    np_grid[goal] = 3
    np_grid[0][0] = 1
    np_grid[4][4] = 2
    return np_grid.flatten()

def print_grid(np_grid):
    # prints formatted grid
    np_grid = np_grid.reshape(5, 5)
    grid_str = '''
    |{}|{}|{}|{}|{}|
    |{}|{}|{}|{}|{}|
    |{}|{}|{}|{}|{}|
    |{}|{}|{}|{}|{}|
    |{}|{}|{}|{}|{}|
    '''.format(*tuple(np_grid.flatten()))
    grid_str = grid_str.replace('1', 'S').replace('0', ' ').replace('2', 'A').replace('3', 'X')
    print(grid_str)

def move_players(np_grid, senior_move, analyst_move):
    # moves players, checks for result
    np_grid = np_grid.reshape(5, 5)
    senior_pos = np.where(np_grid == 1)
    senior_pos = senior_pos[0][0], senior_pos[1][0]
    analyst_pos = np.where(np_grid == 2)
    analyst_pos = analyst_pos[0][0], analyst_pos[1][0]

    if senior_move == 1:
        new_senior_pos = senior_pos[0] - 1, senior_pos[1]
    elif senior_move == 2:
        new_senior_pos = senior_pos[0], senior_pos[1] + 1
    elif senior_move == 3:
        new_senior_pos = senior_pos[0] + 1, senior_pos[1]
    elif senior_move == 4:
        new_senior_pos = senior_pos[0], senior_pos[1] - 1
    else:
        new_senior_pos = senior_pos

    # check move doesn't take player off the grid
    if any(x < 0 for x in new_senior_pos) or any(x > 4 for x in new_senior_pos):
        new_senior_pos = senior_pos

    if analyst_move == 1:
        new_analyst_pos = analyst_pos[0] - 1, analyst_pos[1]
    elif analyst_move == 2:
        new_analyst_pos = analyst_pos[0], analyst_pos[1] + 1
    elif analyst_move == 3:
        new_analyst_pos = analyst_pos[0] + 1, analyst_pos[1]
    elif analyst_move == 4:
        new_analyst_pos = analyst_pos[0], analyst_pos[1] - 1
    else:
        new_analyst_pos = analyst_pos

    if any(x < 0 for x in new_analyst_pos) or any(x > 4 for x in new_analyst_pos):
        new_analyst_pos = analyst_pos
    # check if both players reached target this move
    if np_grid[new_senior_pos] == np_grid[new_analyst_pos] == 3:
        result = 'draw'

    # check if players are trying to occupy same square
    elif new_analyst_pos == new_senior_pos:
        result = 'continue'

    else:
        if np_grid[new_analyst_pos] == 3:
            result = 'analyst'
            np_grid[analyst_pos] = 0
            np_grid[new_analyst_pos] = 2
        elif np_grid[new_senior_pos] == 3:
            result = 'senior'
            np_grid[senior_pos] = 0
            np_grid[new_senior_pos] = 1
        else:
            result = 'continue'
            np_grid[senior_pos] = 0
            np_grid[new_senior_pos] = 1
            np_grid[analyst_pos] = 0 if np_grid[analyst_pos] != 1 else 1
            np_grid[new_analyst_pos] = 2

    return result, np_grid.flatten()

class GridEnvironment(PyEnvironment):

    def __init__(self):

        super().__init__()
        self._action_spec = array_spec.BoundedArraySpec((2,), np.int32, minimum=1, maximum=5)
        self._observation_spec = array_spec.BoundedArraySpec((25,), np.int32, minimum=0, maximum=4)
        self._episode_ended = False
        self._state = set_up_grid()
        self.steps = 0

    def action_spec(self):
        return self._action_spec

    def observation_spec(self):
        return self._observation_spec

    def _reset(self):
        print(f'===\n- resetting -\n===')
        self._state = set_up_grid()
        self._episode_ended = False
        self.steps = 0
        return ts.restart(np.array(self._state))

    def _step(self, action):
        self.steps += 1
        if self.steps >= 20000:
            return ts.termination(np.array(self._state), -0.5)

        result = move_players(self._state, action[0], action[1])
        self._state = result[1]

        if result[0] == 'draw':
            return ts.transition(np.array(result[1]), 0.5)

        if result[0] == 'continue':
            return ts.transition(np.array(result[1]), 0)

        if result[0] == 'senior':
            self._episode_ended = True
            print('senior wins')
            return ts.termination(np.array(result[1]), 1)

        if result[0] == 'analyst':
            self._episode_ended = True
            print('analyst wins')
            return ts.termination(np.array(result[1]), -1)

class MARLAgent(DqnAgent):

    def __init__(self,
                 env: PyEnvironment,
                 action_spec: array_spec.BoundedArraySpec = None,
                 reward_fn: Callable = lambda time_step: time_step.reward,
                 replay_buffer_max_length: int = 10000,
                 fc_layer_params=(200,),
                 **dqn_kwargs):

        self._env = env
        self._reward_fn = reward_fn
        self._observation_spec = self._env.observation_spec()
        self._action_spec = action_spec or self._env.action_spec()
        self.fc_layer_params = fc_layer_params
        self.q_net = self._build_q_net()
        env_ts_spec = self._env.time_step_spec()

        time_step_spec = TimeStep(
            step_type=env_ts_spec.step_type,
            reward=env_ts_spec.reward,
            discount=env_ts_spec.discount,
            observation=self.q_net.input_tensor_spec
        )

        optimiser = tf.keras.optimizers.Adam()
        super().__init__(time_step_spec,
                         self._action_spec,
                         self.q_net,
                         optimiser,
                         **dqn_kwargs)

        self._policy_state = self.policy.get_initial_state(
            batch_size=self._env.batch_size)

        self._replay_buffer = TFUniformReplayBuffer(
            data_spec=self.collect_data_spec,
            batch_size=self._env.batch_size,
            max_length=replay_buffer_max_length)

    def _build_q_net(self):
        fc_layer_params = self.fc_layer_params

        q_net = QNetwork(
            self._observation_spec,
            self._action_spec,
            fc_layer_params=fc_layer_params)

        q_net.create_variables()
        q_net.summary()

        return q_net

    def reset(self):
        self._policy_state = self.policy.get_initial_state(
            batch_size=self._env.batch_size
        )

    def _augment_time_step(self, time_step: TimeStep) -> TimeStep:
        reward = self._reward_fn(time_step)
        reward = tf.convert_to_tensor(reward, dtype=tf.float32)
        if reward.shape != time_step.reward.shape:
            reward = tf.reshape(reward, time_step.reward.shape)

        return TimeStep(
            step_type=time_step.step_type,
            reward=reward,
            discount=time_step.discount,
            observation=time_step.observation
        )

# custom rewards for analyst agent
def analyst_reward_fn(ts: TimeStep):
    return ts.reward * -1 if (ts.reward != 0.5 and ts.reward != -0.5) else ts.reward

grid_env = GridEnvironment()
grid_env = TFPyEnvironment(grid_env)
actions = BoundedTensorSpec.from_spec(array_spec.BoundedArraySpec(shape=(), dtype=np.int32, minimum=0, maximum=4))
senior = MARLAgent(grid_env, action_spec=actions, fc_layer_params=(50, 100, 200))
analyst = MARLAgent(grid_env, action_spec=actions, reward_fn=analyst_reward_fn)

# code to load a pretrain model:
# sc_model = tf.train.Checkpoint(q_net=senior.q_net)
# an_model = tf.train.Checkpoint(q_net=analyst.q_net)
# sc_model.restore('models/sc_checkfin-1')
# an_model.restore('models/an_checkfin-1')

from PIL import Image
import imageio

def image_grid(imgs, rows, cols):
    assert len(imgs) == rows * cols

    w, h = imgs[0].size
    grid = Image.new('RGB', size=(cols * w, rows * h))
    grid_w, grid_h = grid.size

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    return grid

def create_gif(grids, file_name: str):
    gif_images = []
    im_dict = {
        0: 'images/black.png',
        1: 'images/SC.png',
        2: 'images/A.png',
        3: 'images/shake.png'
    }
    for grid in grids:
        imgs = [Image.open(im_dict[x]) for x in grid.flatten()]
        new_image = image_grid(imgs, rows=5, cols=5)
        gif_images.append(new_image)

    return imageio.mimsave('gifs/' + file_name + '.gif', gif_images, duration=0.2)

# play 100 games 500 times
games_per_it = 100
total_its = 500
total_game_count = 0
iteration_move_avg = []

for iteration in range(total_its):
    print('iteration: ', iteration + 1)
    all_move_counts = []

    for game in range(games_per_it):
        if total_game_count % 1 == 0:
            make_gif = True
            gif_grids = []
        else:
            make_gif = False
        total_game_count += 1

        grid_env.reset()
        senior.reset()
        analyst.reset()
        print(f'game number {game + 1}')
        timeStep = grid_env.current_time_step()
        moves = 0
        while not timeStep.is_last():

            if moves % 500 == 0:
                print('move:', moves)
            if make_gif:
                gif_grids.append(timeStep.observation.numpy().reshape(5, 5))
            moves += 1

            s_action = senior.collect_policy.action(timeStep, senior._policy_state)
            a_action = analyst.collect_policy.action(timeStep, analyst._policy_state)
            combined_action = np.array([s_action[0].numpy(), a_action[0].numpy()]).reshape((1, 2))
            combined_action = s_action.replace(action=combined_action)
            nextStep = grid_env.step(combined_action)
            s_data = trajectory.from_transition(timeStep,
                                                s_action,
                                                nextStep)
            a_data = trajectory.from_transition(analyst._augment_time_step(timeStep),
                                                a_action,
                                                analyst._augment_time_step(nextStep))
            senior._replay_buffer.add_batch(s_data)
            analyst._replay_buffer.add_batch(a_data)
            timeStep = grid_env.current_time_step()

        print(f'game complete, moves = {moves}')
        gif_grids.append(timeStep.observation.numpy().reshape(5, 5))
        if make_gif:
            create_gif(gif_grids, str(total_game_count)) if moves <= 200 else create_gif(gif_grids[-200:],
                                                                                str(total_game_count))
        print('training...')
        for x in range(moves):
            senior_experience, info = senior._replay_buffer.get_next(sample_batch_size=10, num_steps=2)
            senior_experience = senior.train(senior_experience)
            analyst_experience, info = analyst._replay_buffer.get_next(sample_batch_size=10, num_steps=2)
            analyst_experience = analyst.train(analyst_experience)

        print('training complete')
        all_move_counts.append(moves)

    iteration_move_avg.append(mean(all_move_counts))

# saving model for further training
sc_checkpoint = tf.train.Checkpoint(q_net=senior.q_net)
sc_checkpoint.save('models/sc_check')
an_checkpoint = tf.train.Checkpoint(q_net=analyst.q_net)
an_checkpoint.save('models/an_check')

# save move averages for analysis
with open('average_moves.pk', 'wb') as f:
    pickle.dump(iteration_move_avg, f)


text:
import numpy as np
import pickle
from videotime.layout import Layout

class TimeDetector:
    '''Detects superimposed timestamps in videos.

    The detector performs a trivial template match to determine
    the digit class for each digit position in a given Layout.

    Since overlay digits are usually rendered pixel exact, the
    detector considers only a single image location per digit.

    Extracting the time of a single still image usually takes around
    80usecs, or equivalently 12500 images can be processed per second.
    The detector is quite robust against Gaussian noise but cannot handle
    images shifts very well. However, this usually does not pose a problem
    as overlays are rendered exactly.
    '''

    def __init__(self, layout, weights):
        '''Create detector from time layout and weights.'''
        self.layout = layout
        self.weights = weights

    def detect(self, img, **kwargs):
        '''Detect and parse time overlay.

        Params
        ------
        img : HxW, HxWxC
            Image to be processed

        Kwargs
        ------
        verbose: bool, optional
            Wheter or not to sho detection result.

        Returns
        -------
        time : datetime object
            Detected time
        probability: scalar
            Probability of detection [0..1]
        '''

        verbose = kwargs.pop('verbose', False)

        img = np.asarray(img)
        if img.ndim == 3:
            img = img[..., 0]

        # Extract letters from HH:MM:SS.TT
        rois = np.array([img[ys, xs] for xs, ys in zip(self.layout.slicesx, self.layout.slicesy)])

        # Scale/shift to [-1, 1]
        srois = (rois / 255. - 0.5)*2

        # Inner product of digit positions and weights to yield scores
        scores = np.tensordot(srois, self.weights, axes=([1,2], [1,2]))

        # Probabilities for each roi according to the alphabet (softmax)
        def softmax(x):
            # e ^ (x - max(x)) / sum(e^(x - max(x))
            xn = x - x.max(axis=1, keepdims=True)
            ex = np.exp(xn)
            return  ex / ex.sum(axis=1, keepdims=True)

        # Use max probs for each digit position as detection result
        probs = softmax(scores)
        dtime = np.argmax(probs, axis=1)
        dprobs = probs[np.arange(probs.shape[0]), dtime]

        try:
            dtime = self.layout.build_time(dtime)
        except ValueError:
            dtime = None

        if verbose and dtime is not None:
            from datetime import datetime
            import matplotlib.pyplot as plt
            fig, axs = plt.subplots(1, rois.shape[0])
            fig.suptitle('Detected time {} - {}%'.format(datetime.strftime(dtime, '%H:%M:%S.%f'), dprobs.min()*100))
            [ax.axis('off') for ax in axs]
            [ax.imshow(l, origin='upper') for ax,l in zip(axs, rois)]
            plt.show()

        return dtime, dprobs.min()

    def save(self, fname):
        '''Save detector'''
        with open(fname, 'wb') as handle:
            pickle.dump(self, handle, protocol=pickle.HIGHEST_PROTOCOL)

    @staticmethod
    def load(fname):
        '''Load detector'''
        with open(fname, 'rb') as handle:
            return pickle.load(handle)

    @staticmethod
    def train(imgs, **kwargs):
        '''Train detector from images.

        This function builds a model for each digit (0-9). It assumes
        10 images to be handed. A layout is assumed that describes the
        positioning of the time overlay in images. Each image is assumed
        to contain a single unique digit in a specific position according
        to the layout. The position can be controlled via the `digitid`
        parameter. If the order of the images is not in digit-order,
        `digitorder` can be used to correct the order.

        Params
        ------
        imgs : 10xHxW, 10xHxWxC
            10 images containing all digits of 0-9. Each image
            contributes one unique digit. The digit location is controlled
            via `digitid`.

        Kwargs
        ------
        digitorder: array-like, optional
            Defines which image contains which digit. By default it is
            assumed that the images contain digits in order. So first
            image contributes 0 digit, second image provides digit 1, etc.
        verbose: bool, optional
            Whether or not to show extracted digits for debugging.
        xshifts: int
            Specifies the amount of +/- shifting in x-direction of images,
            to compensate for potential image shifts. Usually not necessary
            as overlays are rendered at exact positions.
        yshifts: int
            Specifies the amount of +/- shifting of images in y-direction
            to compensate for potential image shifts. Usually not necessary
            as overlays are rendered at exact positions.

        Returns
        -------
        detector : TimeDetector object
            The trained detector.
        '''

        digitorder = kwargs.pop('digitorder', list(range(10)))
        layout = kwargs.pop('layout', Layout())
        digitid = kwargs.pop('digitid', 7) # For Axis cameras this is the minor second digit.
        verbose = kwargs.pop('verbose', False)
        xshifts = kwargs.pop('xshifts', 0)
        yshifts = kwargs.pop('yshifts', 0)

        so = np.argsort(digitorder)
        imgs = np.asarray(imgs)[so]

        if imgs.ndim == 4:
            imgs = imgs[..., 0]

        imgs = (imgs / 255. - 0.5)*2

        idx = layout.digits.index(digitid)

        rois = []
        for xs in range(-xshifts,xshifts+1):
            for ys in range(-yshifts,yshifts+1):
                rimgs = np.roll(imgs, xs, axis=2)
                rimgs = np.roll(rimgs, ys, axis=1)
                rois.append(rimgs[:, layout.slicesy[idx], layout.slicesx[idx]])
        nshifts = (2*xshifts+1) * (2*yshifts+1)
        rois = np.concatenate(rois).reshape(nshifts, 10, layout.digit_shape[0], layout.digit_shape[1])
        rois = np.mean(rois, axis=0)

        weights = rois

        if verbose:
            import matplotlib.pyplot as plt
            fig, axs = plt.subplots(1, 10)
            [ax.axis('off') for ax in axs]
            [ax.imshow(r, origin='upper') for ax,r in zip(axs, rois)]
            plt.show()
            # You should see digits 0..9 in order.

        return TimeDetector(layout, weights)

max_stars_repo_name: cheind/py-videotime

max_issues_repo_name: cheind/py-videotime

max_forks_repo_name: cheind/py-videotime


text:
from nmrespy import freqfilter, sig

import matplotlib as mpl
mpl.use("pgf")
import matplotlib.pyplot as plt
plt.style.use("./lato.mplstyle")
from matplotlib import patches

import numpy as np

fig = plt.figure(figsize=(4,6))
bottom = 0
top = 1
left = 0.05
right = 0.95

total_height = top - bottom
total_width = right - left

axes = []
for i in range(2, -1, -1):
    axes.append(fig.add_axes(
        [left, bottom + (i * total_height / 3), total_width, total_height/3]
    ))
    axes[-1].set_xticks([])
    axes[-1].set_yticks([])
    for orient in ["top", "bottom", "left", "right"]:
        axes[-1].spines[orient].set_visible(False)

n = [4096]
sw = [100.]
para = np.array([
    [1, 0, 40, 0.7],
    [2, 0, 41, 0.7],
    [2, 0, 42, 0.7],
    [1, 0, 43, 0.7],
    [0.7, 0, 29, 0.7],
    [1.4, 0, 30, 0.7],
    [0.7, 0, 31, 0.7],
    [1.5, 0, 9, 0.7],
    [3, 0, 8, 0.7],
    [1.5, 0, 7, 0.7],
    [1.5, 0, 6, 0.7],
    [3, 0, 5, 0.7],
    [1.5, 0, 4, 0.7],
    [1.8, 0, -10, 0.7],
    [1.8, 0, -11, 0.7],
    [1.8, 0, -13, 0.7],
    [1.8, 0, -14, 0.7],
    [0.9, 0, -28, 0.7],
    [1.8, 0, -29.5, 0.7],
    [0.9, 0, -31, 0.7],
])

spectrum = np.real(sig.ft(sig.make_fid(para, n, sw)[0]))
spectrum = spectrum - np.mean(spectrum[0:10])

region = [[1600, 1950]]
sg, c, bw = freqfilter.super_gaussian(region, n, p=40.0)
c = c[0]
bw = bw[0]

filter_spectrum = spectrum * sg

cut_ratio = 3
cut_region = [int(np.floor(c - (bw * cut_ratio / 2))),
              int(np.ceil(c + (bw * cut_ratio / 2)))]

axes[0].plot(spectrum)
axes[0].plot(sg * 1.1 * np.amax(spectrum))
filter_span = patches.ConnectionPatch(
    xyA=(region[0][0], -30), xyB=(region[0][1], -30),
    coordsA=axes[0].transData, coordsB=axes[0].transData,
    arrowstyle="|-|,widthA=0.2,widthB=0.2", axesA=axes[0], axesB=axes[0],
)
axes[0].add_artist(filter_span)
axes[0].text(
    c, -70, "Filter width",
    horizontalalignment="center", verticalalignment="center",
)
axes[0].set_ylim(-100, 1.2 * np.amax(spectrum))

axes[1].plot(filter_spectrum)
axes[1].set_ylim(-140, 1.2 * np.amax(spectrum))
filter_span = patches.ConnectionPatch(
    xyA=(cut_region[0], -30), xyB=(cut_region[1], -30),
    coordsA=axes[1].transData, coordsB=axes[1].transData,
    arrowstyle="|-|,widthA=0.2,widthB=0.2", axesA=axes[1], axesB=axes[1],
)
axes[1].add_artist(filter_span)
axes[1].text(
    c, -70, "Cut ratio $\\times$ Filter width ",
    horizontalalignment="center", verticalalignment="center",
)
axes[1].set_ylim(axes[0].get_ylim())

axes[2].plot(
    np.arange(n[0])[cut_region[0]:cut_region[1]],
    filter_spectrum[cut_region[0]:cut_region[1]],
)
axes[2].set_xlim(axes[0].get_xlim())
axes[2].set_ylim(axes[0].get_ylim())

filter_arrow = patches.ConnectionPatch(
    xyA=(0.6, 0.7), xyB=(0.6, 0.6),
    connectionstyle="arc3,rad=-0.5",
    coordsA="figure fraction", coordsB="figure fraction",
    arrowstyle="->"
)
fig.add_artist(filter_arrow)
fig.text(
    0.69, 0.65, "Filter",
    horizontalalignment="center", verticalalignment="center",
)

cut_arrow = patches.ConnectionPatch(
    xyA=(0.6, 0.32), xyB=(0.6, 0.22),
    connectionstyle="arc3,rad=-0.5",
    coordsA="figure fraction", coordsB="figure fraction",
    arrowstyle="->"
)
fig.add_artist(cut_arrow)
fig.text(
    0.69, 0.27, "Cut",
    horizontalalignment="center", verticalalignment="center",
)

to_td_arrow = patches.ConnectionPatch(
    xyA=(0.62, 0.1), xyB=(0.9, 0.1),
    coordsA="figure fraction", coordsB="figure fraction",
    arrowstyle="->"
)
fig.add_artist(to_td_arrow)
fig.text(
    0.62, 0.14, "Generate time-domain\nsignal...",
    horizontalalignment="left", verticalalignment="center",
)

fig.savefig("filter_cut.png")

max_stars_repo_name: foroozandehgroup/NMR-EsPy

max_issues_repo_name: foroozandehgroup/NMR-EsPy

max_forks_repo_name: foroozandehgroup/NMR-EsPy


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

def crea_matriz_modelo_polinomial(x, poly_deg=2):
    n_points = len(x)
    A = np.ones((n_points, poly_deg+1))
    for i in range(poly_deg+1):
        A[:,i] = x**i
    return A

def poly_model(x, c):
    n = len(c)
    y = np.zeros(len(x))
    for i in range(n):
        y += c[i] * x**i
    return y

# Ejercicio 1
data = np.loadtxt("numeros_6.txt")
x = data[:,0]
y = data[:,1]

# Resuelve el sistema lineal
A = crea_matriz_modelo_polinomial(x, poly_deg=len(x)-1)
A_inv = np.linalg.inv(A) # saco la inversa de A
c = A_inv@y

# datos del modelo
x_model = np.linspace(x.min(), x.max(), 100)
y_model = poly_model(x_model, c)

# grafica
plt.figure()
plt.plot(x_model, y_model, label='fit')
plt.scatter(x, y, label='data')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.savefig("fit_parte_1.png")

# Ejercicio 2
data = np.loadtxt("numeros_20.txt")
x = data[:,0]
y = data[:,1]

# Resuelve el sistema lineal, guarda los coeficients en un diccionario
# para los ajustes de polinomios de orden 0, 1, 2.
c = {}
for n in [0,1,2,3]:
    A = crea_matriz_modelo_polinomial(x, poly_deg=n)
    A_inv = np.linalg.pinv(A) # esta ves uso la pseudo inversa
    c[n] = A_inv@y

# datos del modelo
x_model = np.linspace(x.min(), x.max(), 100)

# grafica
plt.figure()
for n in c.keys():
    y_model = poly_model(x_model, c[n])
    plt.plot(x_model, y_model, label='fit orden {}'.format(n))

plt.scatter(x, y, label='data', color='black')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.savefig("fit_parte_2.png")


text:
# Open3selfopen3d.org
# The MIT License (MIT)
# See license file or visit www.open3d.org for details

# examples/Python/Basic/mesh_properties.py

import numpy as np
import open3d as o3d
import time

import meshes

def mesh_generator(edge_cases=True):
    yield 'box', o3d.geometry.TriangleMesh.create_box()
    yield 'sphere', o3d.geometry.TriangleMesh.create_sphere()
    yield 'cone', o3d.geometry.TriangleMesh.create_cone()
    yield 'torus', o3d.geometry.TriangleMesh.create_torus(radial_resolution=30,
                                                          tubular_resolution=20)
    yield 'moebius (twists=1)', o3d.geometry.TriangleMesh.create_moebius(
        twists=1)
    yield 'moebius (twists=2)', o3d.geometry.TriangleMesh.create_moebius(
        twists=2)
    yield 'moebius (twists=3)', o3d.geometry.TriangleMesh.create_moebius(
        twists=3)

    yield 'knot', meshes.knot()

    if edge_cases:
        yield 'non-manifold edge', meshes.non_manifold_edge()
        yield 'non-manifold vertex', meshes.non_manifold_vertex()
        yield 'open box', meshes.open_box()
        yield 'boxes', meshes.intersecting_boxes()

def check_properties(name, mesh):

    def fmt_bool(b):
        return 'yes' if b else 'no'

    print(name)
    edge_manifold = mesh.is_edge_manifold(allow_boundary_edges=True)
    print('  edge_manifold:          %s' % fmt_bool(edge_manifold))
    edge_manifold_boundary = mesh.is_edge_manifold(allow_boundary_edges=False)
    print('  edge_manifold_boundary: %s' % fmt_bool(edge_manifold_boundary))
    vertex_manifold = mesh.is_vertex_manifold()
    print('  vertex_manifold:        %s' % fmt_bool(vertex_manifold))
    self_intersecting = mesh.is_self_intersecting()
    print('  self_intersecting:      %s' % fmt_bool(self_intersecting))
    watertight = mesh.is_watertight()
    print('  watertight:             %s' % fmt_bool(watertight))
    orientable = mesh.is_orientable()
    print('  orientable:             %s' % fmt_bool(orientable))

    mesh.compute_vertex_normals()

    if not edge_manifold:
        edges = mesh.get_non_manifold_edges(allow_boundary_edges=True)
        print('  # visualize non-manifold edges (allow_boundary_edges=True)')
        o3d.visualization.draw_geometries(
            [mesh, meshes.edges_to_lineset(mesh, edges, (1, 0, 0))])
    if not edge_manifold_boundary:
        edges = mesh.get_non_manifold_edges(allow_boundary_edges=False)
        print('  # visualize non-manifold edges (allow_boundary_edges=False)')
        o3d.visualization.draw_geometries(
            [mesh, meshes.edges_to_lineset(mesh, edges, (0, 1, 0))])
    if not vertex_manifold:
        verts = np.asarray(mesh.get_non_manifold_vertices())
        print('  # visualize non-manifold vertices')
        pcl = o3d.geometry.PointCloud()
        pcl.points = o3d.utility.Vector3dVector(
            np.asarray(mesh.vertices)[verts])
        pcl.paint_uniform_color((0, 0, 1))
        o3d.visualization.draw_geometries([mesh, pcl])
    if self_intersecting:
        intersecting_triangles = np.asarray(
            mesh.get_self_intersecting_triangles())
        intersecting_triangles = intersecting_triangles[0:1]
        intersecting_triangles = np.unique(intersecting_triangles)
        print('  # visualize self-intersecting triangles')
        triangles = np.asarray(mesh.triangles)[intersecting_triangles]
        edges = [
            np.vstack((triangles[:, i], triangles[:, j]))
            for i, j in [(0, 1), (1, 2), (2, 0)]
        ]
        edges = np.hstack(edges).T
        edges = o3d.utility.Vector2iVector(edges)
        o3d.visualization.draw_geometries(
            [mesh, meshes.edges_to_lineset(mesh, edges, (1, 1, 0))])
    if watertight:
        print('  # visualize watertight mesh')
        o3d.visualization.draw_geometries([mesh])

    if not edge_manifold:
        print('  # Remove non-manifold edges')
        mesh.remove_non_manifold_edges()
        print('  # Is mesh now edge-manifold: {}'.format(
            fmt_bool(mesh.is_edge_manifold())))
        o3d.visualization.draw_geometries([mesh])

if __name__ == "__main__":
    # test mesh properties
    print('#' * 80)
    print('Test mesh properties')
    print('#' * 80)
    for name, mesh in mesh_generator(edge_cases=True):
        check_properties(name, mesh)

    # fix triangle orientation
    print('#' * 80)
    print('Fix triangle orientation')
    print('#' * 80)
    for name, mesh in mesh_generator(edge_cases=False):
        mesh.compute_vertex_normals()
        triangles = np.asarray(mesh.triangles)
        rnd_idx = np.random.rand(*triangles.shape).argsort(axis=1)
        rnd_idx[0] = (0, 1, 2)
        triangles = np.take_along_axis(triangles, rnd_idx, axis=1)
        mesh.triangles = o3d.utility.Vector3iVector(triangles)
        o3d.visualization.draw_geometries([mesh])
        sucess = mesh.orient_triangles()
        print('%s orientated: %s' % (name, 'yes' if sucess else 'no'))
        o3d.visualization.draw_geometries([mesh])

    # intersection tests
    print('#' * 80)
    print('Intersection tests')
    print('#' * 80)
    np.random.seed(30)
    bbox = o3d.geometry.TriangleMesh.create_box(20, 20, 20).translate(
        (-10, -10, -10))
    meshes = [o3d.geometry.TriangleMesh.create_box() for _ in range(20)]
    meshes.append(o3d.geometry.TriangleMesh.create_sphere())
    meshes.append(o3d.geometry.TriangleMesh.create_cone())
    meshes.append(o3d.geometry.TriangleMesh.create_torus())
    dirs = [np.random.uniform(-0.1, 0.1, size=(3,)) for _ in meshes]
    for mesh in meshes:
        mesh.compute_vertex_normals()
        mesh.paint_uniform_color((0.5, 0.5, 0.5))
        mesh.translate(np.random.uniform(-7.5, 7.5, size=(3,)))
    vis = o3d.visualization.Visualizer()
    vis.create_window()
    for mesh in meshes:
        vis.add_geometry(mesh)
    for iter in range(1000):
        for mesh, dir in zip(meshes, dirs):
            mesh.paint_uniform_color((0.5, 0.5, 0.5))
            mesh.translate(dir)
        for idx0, mesh0 in enumerate(meshes):
            collision = False
            collision = collision or mesh0.is_intersecting(bbox)
            for idx1, mesh1 in enumerate(meshes):
                if collision: break
                if idx0 == idx1: continue
                collision = collision or mesh0.is_intersecting(mesh1)
            if collision:
                mesh0.paint_uniform_color((1, 0, 0))
                dirs[idx0] *= -1
        vis.update_geometry()
        vis.poll_events()
        vis.update_renderer()
        time.sleep(0.05)
    vis.destroy_window()


text:
"""
Mask R-CNN
Train on the toy bottle dataset and implement color splash effect.

Copyright (c) 2018 Matterport, Inc.
Licensed under the MIT License (see LICENSE for details)
Written by Waleed Abdulla

------------------------------------------------------------

Usage: import the module (see Jupyter notebooks for examples), or run from
	   the command line as such:

	# Train a new model starting from pre-trained COCO weights
	python3 bottle.py train --dataset=/home/datascience/Workspace/maskRcnn/Mask_RCNN-master/samples/bottle/dataset --weights=coco

	# Resume training a model that you had trained earlier
	python3 bottle.py train --dataset=/path/to/bottle/dataset --weights=last

	# Train a new model starting from ImageNet weights
	python3 bottle.py train --dataset=/path/to/bottle/dataset --weights=imagenet

	# Apply color splash to an image
	python3 bottle.py splash --weights=/path/to/weights/file.h5 --image=<URL or path to file>

	# Apply color splash to video using the last weights you trained
	python3 bottle.py splash --weights=last --video=<URL or path to file>
"""
# import libs
import os
import sys
import json
import datetime
import numpy as np
import skimage.draw
import cv2
from mrcnn.visualize import display_instances
import matplotlib.pyplot as plt
from pycocotools.coco import COCO
import skimage.io as io
import pylab
import random
from tqdm import tqdm
import time

from imgaug import augmenters as iaa
from mrcnn import visualize

# Root directory of the project
ROOT_DIR = os.path.abspath("./")
print("ROOT_DIR: ", ROOT_DIR)
# Import Mask RCNN
from mrcnn.config import Config
from mrcnn import model as modellib, utils

# Path to trained weights file
COCO_WEIGHTS_PATH = os.path.join('./weights', "mask_rcnn_coco.h5")

# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")
print("LOG: ", DEFAULT_LOGS_DIR)
############################################################
#  Configurations
############################################################

category = 'medical'
class_names = ['BG', 'head']

class CustomConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = category

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 2

    # Number of classes (including background)
    NUM_CLASSES = 2  # 1 + len(class_names)  # Background + toy

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 150
    VALIDATION_STEPS = 50

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9
    DETECTION_NMS_THRESHOLD = 0.7

    WEIGHT_DECAY = 0.0005

    # Backbone network architecture
    # Supported values are: resnet50, resnet101
    BACKBONE = "resnet101"

    IMAGE_RESIZE_MODE = "square"
    IMAGE_MIN_DIM = 540
    IMAGE_MAX_DIM = 1024

    # Length of square anchor side in pixels
    RPN_ANCHOR_SCALES = (16, 32, 64, 128, 256)

    # Non-max suppression threshold to filter RPN proposals.
    # You can increase this during training to generate more propsals.
    RPN_NMS_THRESHOLD = 0.9

    # How many anchors per image to use for RPN training
    RPN_TRAIN_ANCHORS_PER_IMAGE = 64

    # If enabled, resizes instance masks to a smaller size to reduce
    # memory load. Recommended when using high-resolution images.
    USE_MINI_MASK = True
    MINI_MASK_SHAPE = (56, 56)  # (height, width) of the mini-mask

    # Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE = 400

    # Maximum number of ground truth instances to use in one image
    MAX_GT_INSTANCES = 200

    # Max number of final detections per image
    DETECTION_MAX_INSTANCES = 5

############################################################
#  Dataset
############################################################

class CustomDataset(utils.Dataset):

    def load_custom(self, dataset_dir, subset):
        """Load a subset of the bottle dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes. We have only one class to add.
        for i in range(1, len(class_names) + 1):
            self.add_class(category, i, class_names[i - 1] + 1)

        # Train or validation dataset?
        assert subset in ["train", "val"]
        dataset_dir = os.path.join(dataset_dir, subset)

        # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region

        coco = COCO(os.path.join(dataset_dir, 'annotations.json'))

        category_ids = coco.loadCats(coco.getCatIds())
        cat_ids = [obj['id'] for obj in category_ids]

        for catIndex in tqdm(cat_ids):
            image_ids = coco.getImgIds(catIds=catIndex)
            for i in range(len(image_ids)):
                polygons = []
                class_ids = []

                tmp = coco.loadImgs([image_ids[i]])
                w, h = tmp[0]['width'], tmp[0]['height']
                a_image_id = image_ids[i]
                img = coco.loadImgs(a_image_id)[0]  # here fetching it
                annotation_ids = coco.getAnnIds(imgIds=img['id'])
                annotations = coco.loadAnns(annotation_ids)
                image_path = os.path.join(dataset_dir, 'images/' + img['file_name'])

                # we are now inputting the polygons
                for j in range(len(annotations)):
                    all_points_x = []
                    all_points_y = []

                    for n in range(0, len(annotations[j]['segmentation'][0]), 2):
                        all_points_x.append(annotations[j]['segmentation'][0][n])
                        all_points_y.append(annotations[j]['segmentation'][0][n + 1])
                    polygons.append({'name': 'polygon', 'all_points_x': all_points_x, 'all_points_y': all_points_y})
                    idx = cat_ids.index(annotations[j]['category_id'])
                    class_ids.append(class_names.index(category_ids[idx]['name']) + 1)

                self.add_image(
                    category,  ## for a single class just add the name here
                    image_id=img['file_name'],  # use file name as a unique image id
                    path=image_path,
                    width=w, height=h,
                    polygons=polygons,
                    class_ids=class_ids)

    # TOMODIFY FROM HERE
    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a bottle dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != category:
            return super(self.__class__, self).load_mask(image_id)

        class_ids = image_info['class_ids']
        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            # rr, cc = skimage.draw.ellipse(p['cy'], p['cx'], p['ry'], p['rx'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance.
        class_ids = np.array(class_ids, dtype=np.int32)
        return mask.astype(np.bool), class_ids

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == category:
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)

#######################################################################################################################

def detect_and_color_splash(model, image=None):
    import cv2
    # Run model detection and generate the color splash effect
    #print("Running on {}".format(args.image))
    rgb_img = image.copy()

    # Detect objects
    r = model.detect([image], verbose=1)[0]

    # Color splash
    # display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
    mask = r["masks"]

    for j in range(image.shape[2]):
        image[:, :, j] = image[:, :, j] * mask[:, :, 0]

    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = np.where(image > 0, 255, image)

    # Combine surrounding noise with ROI
    kernel = np.ones((6, 6), np.uint8)
    image = cv2.dilate(image, kernel, iterations=3)

    # filename = "crop_mask.jpg"
    # cv2.imwrite(filename, image)

    return image, rgb_img

############################################################
#  Inference
############################################################
class InferenceConfig(CustomConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

def load_mrcnn_model():
    ### Define weights path
    weights_path = './/mask_rcnn//weights//mask_rcnn_head_final.h5'
    # weights_path = os.path.join(ROOT_DIR, "mask_rcnn\weights\mask_rcnn_head_final.h5")
    print("Weights path: ", weights_path)

    ### Define config
    config = InferenceConfig()
    #config.display()

    # Define Mask RCNN model
    model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir='./logs')

    # Load weights
    print("Loading weights ", weights_path)
    model.load_weights("E:\\Deep Learning\\HC-18-API\\Fetal-Head-Circumferences-HC18\\mask_rcnn\\weights\\mask_rcnn_head_final.h5", by_name=True)

    return model

# global model
# model = load_mrcnn_model()
#
# detect_and_color_splash(model)


text:
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import time
import os
import torchvision
import matplotlib.pyplot as plt
from torchvision import datasets, models, transforms
from torch.nn import functional as F
from model import DCGAN
import cv2 as cv
import shutil

# =================================== Checkpoint ======================================
def get_model_name(name, batch_size, learning_rate, epoch):
    """ Generate a name for the model consisting of all the hyperparameter values

    Args:
        config: Configuration object containing the hyperparameters
    Returns:
        path: A string with the hyperparameter name and value concatenated
    """
    path = "./model_{0}_bs{1}_lr{2}_epoch{3}".format(name,batch_size,learning_rate, epoch)

    return path

# ================================ Load Model ===================================
def load_model(num_channel=3, num_epoch=899):
    # example parameters we used to train our model
    cloud_computing = True
    filter_size = 64
    ngpu = 1
    batch_size = 64
    learning_rate = 1e-4
    device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
    gan = DCGAN(device, filter_size, num_channel, ngpu, cloud_computing)

    model_path = get_model_name(gan.name, batch_size, learning_rate, num_epoch)
    state = torch.load(model_path, map_location=lambda storage, loc: storage)
    gan.load_state_dict(state)
    return gan

# ============================== Generate Image =================================
def generate_image(model=None, num_channel=3):

    if os.path.exists('output'):
        shutil.rmtree('output')

    os.makedirs('output')

    if os.path.exists('comparison'):
        shutil.rmtree('comparison')

    os.makedirs('comparison')

    # load test image under "test" folder
    test_dir = 'test'
    assert(num_channel==3)

    # if num_channel == 1:
    #     transform = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
    # else:
    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

    # test image
    test_edge = torchvision.datasets.ImageFolder(root=test_dir, transform=transform)
    test_loader = torch.utils.data.DataLoader(test_edge, batch_size=1, num_workers=1)

    # traversing the test folder to generate a new image for each sketch
    for i, image in enumerate(test_loader, 0):
        test_data = image[0]
        output_img = model.netG(test_data)
        # if num_channel == 1:
        #     output_img = output_img.detach().cpu().numpy().squeeze()
        # else:
        output_img = np.transpose(output_img.detach().numpy().squeeze(), [1, 2, 0])

        output_img = (output_img / 2 + 0.5)
        plt.imsave("./output/" + str(i) + ".jpg", output_img)

        temp_test_data = np.transpose(test_data.detach().cpu().numpy().squeeze(), [1, 2, 0])
        temp_test_data = (temp_test_data / 2 + 0.5)
        comparison = np.hstack((temp_test_data, output_img))

        plt.imsave("./comparison/" + str(i) + ".jpg", comparison)

# =================================== Main ======================================
if __name__ == '__main__':
    # we only support rgb
    num_channel = 3
    model = load_model(num_channel, 899)
    generate_image(model, num_channel)
    print("Done generating images, please check 'output' and 'comparison' directory")


text:
import abc
import numpy as np
from collections import Counter
from typing import List, Tuple, Dict
from sklearn.feature_extraction.text import CountVectorizer

class DataInterface(metaclass=abc.ABCMeta):

    def __init__(self, entities, relations):
        try:
            assert isinstance(entities, list)
        except AssertionError:
            assert isinstance(
                entities, tuple), "Expected list or tuple, but is {type(entities)}"
        try:
            assert isinstance(relations, list)
        except AssertionError:
            assert isinstance(
                relations, tuple), "Expected list or tuple, but is {type(relations)}"
        self.ents = tuple(sorted(entities))
        self.rels = tuple(sorted(relations))
        self.vect = CountVectorizer(vocabulary=self.build_vocab(), binary=True)
        self.out_map = {i: n for n, i in enumerate(self.ents+self.rels)}

    @abc.abstractmethod
    def sample_entitie_name(self, name: str, n: int):
        raise NotImplementedError()

    def sample_entities_name(self, name: List[str], n: int):
        assert len(name) != 0
        samples_type = np.random.randint(len(name), size=n)
        samples_type = Counter(samples_type)
        data = None
        res = None
        for i, j in samples_type.items():
            if res is None:
                data, res = self.sample_entitie_name(name[i], j)
            else:
                dd, rr = self.sample_entitie_name(name[i], j)
                data = np.vstack([data, dd])
                res = np.vstack([res, rr])
        return data, res

    def sample_entities(self, n: int):
        return self.sample_entities_name(self.ents, n)

    @abc.abstractmethod
    def sample_relation_name(self, name: str, n: int):
        raise NotImplementedError()

    def sample_relations_name(self, name: List[str], n: int):
        assert len(name) != 0
        samples_type = np.random.randint(len(name), size=n)
        samples_type = Counter(samples_type)
        data = None
        res = None
        for i, j in samples_type.items():
            if res is None:
                data, res = self.sample_relation_name(name[i], j)
            else:
                dd, rr = self.sample_relation_name(name[i], j)
                data = np.vstack([data, dd])
                res = np.vstack([res, rr])
        return data, res

    def sample_relations(self, n: int):
        return self.sample_relations_name(self.rels, n)

    def build_vocab(self):
        tt = self._build_vocab()
        assert isinstance(tt, dict) or isinstance(tt, set)
        return tt

    @abc.abstractmethod
    def _build_vocab(self):
        raise NotImplementedError()

max_stars_repo_name: fsadannn/snn

max_issues_repo_name: fsadannn/snn

max_forks_repo_name: fsadannn/snn


text:
# coding=utf-8
import transformers
import torch
import os
import json
import random
import numpy as np
import argparse
# from torch.utils.tensorboard import SummaryWriter
from datetime import datetime
from tqdm import tqdm
from torch.nn import DataParallel
import logging
from transformers import GPT2TokenizerFast, GPT2LMHeadModel, GPT2Config
from transformers import BertTokenizerFast
# from transformers import BertTokenizer
from os.path import join, exists
from itertools import zip_longest, chain
# from chatbot.model import DialogueGPT2Model
from dataset import MyDataset
from torch.utils.data import Dataset, DataLoader
from torch.nn import CrossEntropyLoss
# from sklearn.model_selection import train_test_split
import torch.nn.functional as F

PAD = '[PAD]'
pad_id = 0

def set_args():
    """
    Sets up the arguments.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('--device', default='0', type=str, required=False, help='生成设备')
    parser.add_argument('--temperature', default=1, type=float, required=False, help='生成的temperature')
    parser.add_argument('--topk', default=8, type=int, required=False, help='最高k选1')
    parser.add_argument('--topp', default=0, type=float, required=False, help='最高积累概率')
    # parser.add_argument('--model_config', default='config/model_config_dialogue_small.json', type=str, required=False,
    #                     help='模型参数')
    parser.add_argument('--log_path', default='data/interact.log', type=str, required=False, help='interact日志存放位置')
    parser.add_argument('--vocab_path', default='vocab/vocab.txt', type=str, required=False, help='选择词库')
    parser.add_argument('--model_path', default='model/epoch40', type=str, required=False, help='对话模型路径')
    parser.add_argument('--save_samples_path', default="sample/", type=str, required=False, help="保存聊天记录的文件路径")
    parser.add_argument('--repetition_penalty', default=1.0, type=float, required=False,
                        help="重复惩罚参数，若生成的对话重复性较高，可适当提高该参数")
    # parser.add_argument('--seed', type=int, default=None, help='设置种子用于生成随机数，以使得训练的结果是确定的')
    parser.add_argument('--max_len', type=int, default=25, help='每个utterance的最大长度,超过指定长度则进行截断')
    parser.add_argument('--max_history_len', type=int, default=3, help="dialogue history的最大长度")
    parser.add_argument('--no_cuda', action='store_true', help='不使用GPU进行预测')
    return parser.parse_args()

def create_logger(args):
    """
    将日志输出到日志文件和控制台
    """
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    formatter = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(message)s')

    # 创建一个handler，用于写入日志文件
    file_handler = logging.FileHandler(
        filename=args.log_path)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    logger.addHandler(file_handler)

    # 创建一个handler，用于将日志输出到控制台
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    console.setFormatter(formatter)
    logger.addHandler(console)

    return logger

def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
    """ Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
        Args:
            logits: logits distribution shape (vocab size)
            top_k > 0: keep only top k tokens with highest probability (top-k filtering).
            top_p > 0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
                Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
        From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
    """
    assert logits.dim() == 1  # batch size 1 for now - could be updated for more but the code would be less clear
    top_k = min(top_k, logits.size(-1))  # Safety check
    if top_k > 0:
        # Remove all tokens with a probability less than the last token of the top-k
        # torch.topk()返回最后一维最大的top_k个元素，返回值为二维(values,indices)
        # ...表示其他维度由计算机自行推断
        indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
        logits[indices_to_remove] = filter_value  # 对于topk之外的其他元素的logits值设为负无穷

    if top_p > 0.0:
        sorted_logits, sorted_indices = torch.sort(logits, descending=True)  # 对logits进行递减排序
        cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)

        # Remove tokens with cumulative probability above the threshold
        sorted_indices_to_remove = cumulative_probs > top_p
        # Shift the indices to the right to keep also the first token above the threshold
        sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
        sorted_indices_to_remove[..., 0] = 0

        indices_to_remove = sorted_indices[sorted_indices_to_remove]
        logits[indices_to_remove] = filter_value
    return logits

def main():
    args = set_args()
    logger = create_logger(args)
    # 当用户使用GPU,并且GPU可用时
    args.cuda = torch.cuda.is_available() and not args.no_cuda
    device = 'cuda' if args.cuda else 'cpu'
    logger.info('using device:{}'.format(device))
    os.environ["CUDA_VISIBLE_DEVICES"] = args.device
    tokenizer = BertTokenizerFast(vocab_file=args.vocab_path, sep_token="[SEP]", pad_token="[PAD]", cls_token="[CLS]")
    # tokenizer = BertTokenizer(vocab_file=args.voca_path)
    model = GPT2LMHeadModel.from_pretrained(args.model_path)
    model = model.to(device)
    model.eval()
    if args.save_samples_path:
        if not os.path.exists(args.save_samples_path):
            os.makedirs(args.save_samples_path)
        samples_file = open(args.save_samples_path + '/samples.txt', 'a', encoding='utf8')
        samples_file.write("聊天记录{}:\n".format(datetime.now()))
    # 存储聊天记录，每个utterance以token的id的形式进行存储
    history = []
    print('开始和chatbot聊天，输入CTRL + Z以退出')

    while True:
        try:
            text = input("user:")
            # text = "你好"
            if args.save_samples_path:
                samples_file.write("user:{}\n".format(text))
            text_ids = tokenizer.encode(text, add_special_tokens=False)
            history.append(text_ids)
            input_ids = [tokenizer.cls_token_id]  # 每个input以[CLS]为开头

            for history_id, history_utr in enumerate(history[-args.max_history_len:]):
                input_ids.extend(history_utr)
                input_ids.append(tokenizer.sep_token_id)
            input_ids = torch.tensor(input_ids).long().to(device)
            input_ids = input_ids.unsqueeze(0)
            response = []  # 根据context，生成的response
            # 最多生成max_len个token
            for _ in range(args.max_len):
                outputs = model(input_ids=input_ids)
                logits = outputs.logits
                next_token_logits = logits[0, -1, :]
                # 对于已生成的结果generated中的每个token添加一个重复惩罚项，降低其生成概率
                for id in set(response):
                    next_token_logits[id] /= args.repetition_penalty
                next_token_logits = next_token_logits / args.temperature
                # 对于[UNK]的概率设为无穷小，也就是说模型的预测结果不可能是[UNK]这个token
                next_token_logits[tokenizer.convert_tokens_to_ids('[UNK]')] = -float('Inf')
                filtered_logits = top_k_top_p_filtering(next_token_logits, top_k=args.topk, top_p=args.topp)
                # torch.multinomial表示从候选集合中无放回地进行抽取num_samples个元素，权重越高，抽到的几率越高，返回元素的下标
                next_token = torch.multinomial(F.softmax(filtered_logits, dim=-1), num_samples=1)
                if next_token == tokenizer.sep_token_id:  # 遇到[SEP]则表明response生成结束
                    break
                response.append(next_token.item())
                input_ids = torch.cat((input_ids, next_token.unsqueeze(0)), dim=1)
                # his_text = tokenizer.convert_ids_to_tokens(curr_input_tensor.tolist())
                # print("his_text:{}".format(his_text))
            history.append(response)
            text = tokenizer.convert_ids_to_tokens(response)
            print("chatbot:" + "".join(text))
            if args.save_samples_path:
                samples_file.write("chatbot:{}\n".format("".join(text)))
        except KeyboardInterrupt:
            if args.save_samples_path:
                samples_file.close()
            break

if __name__ == '__main__':
    main()


text:
import copy
import matplotlib.pyplot as plt
import networkx as nx
from networkx.algorithms import bipartite

repos = {'khughitt': ['pandas-dev/pandas', 'conda-forge/staged-recipes'], 'skyline75489': ['pytorch/tutorials', 'pallets/werkzeug'], 'jart': ['django/django', 'django-extensions/django-extensions', 'tensorflow/models', 'tensorflow/tensorboard'], 'rodrigoberriel': ['keras-team/keras'], 'allenwang28': ['tensorflow/models', 'tensorflow/tensorboard'], 'mdmueller': ['astropy/astropy', 'pandas-dev/pandas', 'scikit-image/scikit-image', 'sympy/sympy'], 'kyleabeauchamp': ['pandas-dev/pandas', 'pymc-devs/pymc', 'scipy/scipy', 'conda-forge/staged-recipes', 'statsmodels/statsmodels'], 'ksunden': ['psf/black', 'matplotlib/matplotlib'], 'DatenBiene': ['scikit-learn/scikit-learn'], 'jona-sassenhagen': ['mne-tools/mne-python', 'scikit-learn/scikit-learn', 'scipy/scipy'], 'bhack': ['isocpp/CppCoreGuidelines', 'keras-team/keras', 'tensorflow/models', 'tensorflow/tensorboard'], 'kmh4321': ['tensorflow/datasets'], 'anushkrishnav': ['matplotlib/matplotlib', 'TheAlgorithms/Python'], 'kbrose': ['matplotlib/matplotlib', 'python/typeshed'], 'superbobry': ['python/cpython', 'cython/cython', 'google/jax', 'PyCQA/pylint', 'Theano/Theano'], 'tomMoral': ['python/cpython', 'dask/distributed', 'scikit-learn/scikit-learn'], 'benzyx': ['openai/gym'], 'xinan-jiang': ['tensorflow/tensor2tensor'], 'albertvillanova': ['huggingface/datasets', 'nltk/nltk', 'pandas-dev/pandas', 'pytest-dev/pytest', 'huggingface/transformers', 'scikit-learn/scikit-learn', 'tensorflow/datasets', 'tensorflow/models'], 'ashraf-bhuiyan': ['tensorflow/models'], 'teng-li': ['pytorch/tutorials'], 'fgb': ['wbond/package_control_channel'], 'franslarsson': ['scikit-learn/scikit-learn'], 'zblz': ['matplotlib/matplotlib', 'mlflow/mlflow', 'scipy/scipy'], 'wdirons': ['tensorflow/tensorboard'], 'tguillemot': ['jupyterhub/jupyterhub', 'scikit-image/scikit-image', 'scikit-learn/scikit-learn'], 'shashvatshahi1998': ['django/django', 'tensorflow/models', 'scipy/scipy', 'tensorflow/tensorboard'], 'jeromefv': ['matplotlib/matplotlib'], 'ppwwyyxx': ['open-mmlab/mmdetection', 'sphinx-doc/sphinx'], 'f0k': ['matplotlib/matplotlib', 'scikit-learn/scikit-learn', 'Theano/Theano'], 'charliec443': ['apache/airflow'], 'forrestm': ['matplotlib/matplotlib'], 'varunkumar-dev': ['networkx/networkx', 'pandas-dev/pandas', 'scikit-learn/scikit-learn'], 'genvalen': ['scikit-learn/scikit-learn'], 'kastnerkyle': ['dask/dask', 'pytorch/fairseq', 'getpelican/pelican-plugins', 'pymc-devs/pymc', 'Theano/Theano'], 'ciyongch': ['keras-team/keras', 'intel/Theano'], 'r1chardj0n3s': ['pallets/flask', 'openstack/horizon', 'kivy/kivy', 'pypa/pip', 'psf/requests', 'tox-dev/tox', 'pypa/warehouse', 'pallets/werkzeug'], 'cmarmo': ['astropy/astropy', 'numpy/numpy', 'scikit-learn/scikit-learn'], 'jianyuh': ['apache/tvm'], 'MartinPerez': ['mne-tools/mne-python'], 'soumith': ['pytorch/pytorch', 'conda-forge/staged-recipes'], 'chebee7i': ['ipython/ipython', 'matplotlib/matplotlib', 'networkx/networkx', 'pandas-dev/pandas', 'pymc-devs/pymc', 'getsentry/raven-python', 'readthedocs/readthedocs.org', 'sagemath/sage', 'scrapy/scrapy', 'statsmodels/statsmodels'], 'HH-MWB': ['pandas-dev/pandas'], 'xmunoz': ['python/peps', 'matrix-org/synapse', 'pypa/warehouse'], 'rjpower': ['conda/conda', 'google/jax', 'keras-team/keras', 'tensorflow/tensor2tensor'], 'jvdillon': ['tensorflow/probability'], 'k-w-w': ['nkconnor/models', 'tensorflow/tensorboard'], 'HastingsGreer': ['matplotlib/matplotlib'], 'heathhenley': ['python/cpython', 'keras-team/keras', 'matplotlib/matplotlib', 'scikit-learn/scikit-learn'], 'gvalkov': ['ansible/ansible', 'buildbot/buildbot', 'gitpython-developers/GitPython', 'home-assistant/core', 'pypa/pip', 'pypa/pipenv', 'qtile/qtile', 'tornadoweb/tornado'], 'mzeitlin11': ['pandas-dev/pandas'], 'benoit-pierre': ['python-pillow/Pillow', 'pypa/pip', 'pyinstaller/pyinstaller', 'pypa/setuptools', 'tox-dev/tox'], 'cjc7373': ['pypa/pip'], 'bmzhao': ['tensorflow/models'], 'cimarronm': ['matplotlib/matplotlib', 'numpy/numpy', 'saltstack/salt', 'scipy/scipy'], 'qinhanmin2014': ['scikit-learn/scikit-learn', 'huggingface/transformers'], 'realead': ['cython/cython', 'pandas-dev/pandas', 'scikit-learn/scikit-learn'], 'emcastillo': ['chainer/chainer', 'cupy/cupy'], 'sohero': ['matplotlib/matplotlib'], 'ebetica': ['pytorch/fairseq', 'ray-project/ray'], 'ryanbelt': ['matplotlib/matplotlib'], 'julianser': ['Theano/Theano'], 'mrocklin': ['cupy/cupy', 'dask/dask', 'dask/distributed', 'networkx/networkx', 'numpy/numpy', 'pandas-dev/pandas', 'scikit-image/scikit-image', 'zzzeek/sqlalchemy', 'conda-forge/staged-recipes', 'sympy/sympy', 'Theano/Theano', 'tornadoweb/tornado', 'pydata/xarray'], 'jucor': ['tensorflow/models', 'scikit-learn/scikit-learn', 'pallets/werkzeug'], 'sdegryze': ['pandas-dev/pandas', 'scikit-learn/scikit-learn'], 'garymm': ['pytorch/fairseq', 'pytorch/tutorials', 'pytorch/vision'], 'Licht-T': ['python/cpython', 'Guake/guake', 'apache/airflow', 'pandas-dev/pandas', 'pyinstaller/pyinstaller', 'scikit-learn/scikit-learn', 'scipy/scipy'], 'AlexKirko': ['pandas-dev/pandas'], 'bstriner': ['python/cpython', 'openai/gym', 'keras-team/keras', 'mlflow/mlflow', 'Theano/Theano'], 'topper-123': ['python/cpython', 'pandas-dev/pandas'], 'baam25simo': ['scikit-learn/scikit-learn'], 'behzadnouri': ['pandas-dev/pandas', 'scipy/scipy'], 'unutbu': ['ipython/ipython', 'matplotlib/matplotlib', 'pandas-dev/pandas', 'scipy/scipy'], 'amroamroamro': ['scikit-image/scikit-image'], 'nschloe': ['pandas-dev/pandas', 'scipy/scipy'], 'zaxtax': ['nltk/nltk', 'pymc-devs/pymc', 'scikit-learn/scikit-learn', 'sympy/sympy'], 'notoraptor': ['apache/tvm', 'Theano/Theano'], 'aitikgupta': ['coala/coala-bears'], 'wonghang': ['cupy/cupy', 'Theano/Theano'], 'meeseeksmachine': ['ipython/ipython', 'matplotlib/matplotlib', 'pandas-dev/pandas', 'scikit-image/scikit-image'], 'vaibhavhrt': ['pandas-dev/pandas'], 'kynan': ['pandas-dev/pandas', 'pypa/pip', 'OpenMined/PySyft', 'conda-forge/staged-recipes'], 'yui-knk': ['pandas-dev/pandas'], 'rafaelcaricio': ['encode/django-rest-framework', 'python/mypy', 'pypa/pip', 'saltstack/salt', 'tox-dev/tox', 'pallets/werkzeug'], 'hameerabbasi': ['numba/numba', 'scipy/scipy', 'conda-forge/staged-recipes'], 'lc0': ['chainer/chainer', 'tensorflow/datasets', 'tensorflow/models', 'OpenMined/PySyft'], 'kemaleren': ['scikit-image/scikit-image'], 'glemaitre': ['dask/dask', 'pandas-dev/pandas', 'pytorch/pytorch', 'scikit-image/scikit-image', 'scikit-learn/scikit-learn', 'scipy/scipy', 'conda-forge/staged-recipes', 'statsmodels/statsmodels'], 'botev': ['openai/gym', 'google/jax', 'Sarasra/models', 'Theano/Theano'], 'imaginary-person': ['apache/tvm'], 'simpkins': ['tornadoweb/tornado', 'python/typeshed'], 'shackenberg': ['celery/celery', 'keras-team/keras', 'Theano/Theano'], 'royxue': ['keras-team/keras', 'tensorflow/models', 'Theano/Theano'], 'niedbalski': ['ansible/ansible', 'buildbot/buildbot', 'benoitc/gunicorn', 'openstack/kolla', 'openstack/kolla-ansible', 'pypa/pip', 'tornadoweb/tornado'], 'wxs': ['keras-team/keras'], 'jnmclarty': ['vinta/awesome-python', 'python/cpython', 'dask/dask', 'python/mypy', 'pandas-dev/pandas'], 'vincentqb': ['pytorch/fairseq'], 'bburan-galenea': ['mne-tools/mne-python', 'pandas-dev/pandas'], 'tonyyli': ['matplotlib/matplotlib', 'pandas-dev/pandas', 'scipy/scipy'], 'tusharsadhwani': ['python/mypy'], 'adamklein': ['cython/cython', 'pandas-dev/pandas'], 'gdesjardins': ['Theano/Theano'], 'magnunor': ['dask/distributed', 'matplotlib/matplotlib', 'conda-forge/staged-recipes'], 'yifeif': ['keras-team/keras'], 'neggert': ['keras-team/keras', 'matplotlib/matplotlib', 'PyTorchLightning/pytorch-lightning'], 'NNemec': ['matplotlib/matplotlib', 'mesonbuild/meson'], 'frankchn': ['jupyterhub/jupyterhub', 'tensorflow/models'], 'pradyunsg': ['psf/black', 'python/cpython', 'PyCQA/isort', 'python/peps', 'pypa/pip', 'pypa/pipenv', 'pypa/packaging.python.org', 'psf/requests', 'RustPython/RustPython', 'pypa/setuptools', 'python/typeshed'], 'tonysyu': ['ipython/ipython', 'matplotlib/matplotlib', 'scikit-image/scikit-image'], 'carljm': ['ansible/ansible-modules-extras', 'psf/black', 'python/cpython', 'django/django', 'benoitc/gunicorn', 'PyCQA/isort', 'python/mypy', 'pypa/pip', 'getsentry/raven-python', 'python/typeshed'], 'alex': ['ansible/ansible', 'boto/boto', 'cookiecutter/cookiecutter', 'python/cpython', 'pyca/cryptography', 'django/django', 'jazzband/django-debug-toolbar', 'benoitc/gunicorn', 'HypothesisWorks/hypothesis', 'certbot/josepy', 'celery/kombu', 'certbot/certbot', 'apache/libcloud', 'MongoEngine/mongoengine', 'pypa/pip', 'Pylons/pyramid', 'pytest-dev/pytest', 'realpython/python-guide', 'getsentry/raven-python', 'readthedocs/readthedocs.org', 'psf/requests', 'getsentry/sentry', 'pypa/setuptools', 'sphinx-doc/sphinx', 'zzzeek/sqlalchemy', 'urllib3/urllib3', 'pypa/warehouse', 'pallets/werkzeug', 'zulip/zulip'], 'ionelmc': ['vinta/awesome-python', 'borgbackup/borg', 'celery/celery', 'pallets/click', 'cookiecutter/cookiecutter', 'django-cms/django-cms', 'jazzband/django-debug-toolbar', 'PyCQA/isort', 'celery/kombu', 'getpelican/pelican', 'getpelican/pelican-plugins', 'pypa/pip', 'pytest-dev/pytest', 'pypa/packaging.python.org', 'psf/requests', 'sphinx-doc/sphinx', 'tox-dev/tox'], 'smessmer': ['python/typeshed'], 'mingfeima': ['pytorch/fairseq', 'huggingface/transformers'], 'danFromTelAviv': ['keras-team/keras'], 'ashahba': ['kubeflow/pipelines', 'pytorch/pytorch', 'tensorflow/models'], 'edgarriba': ['pytorch/pytorch'], 'mynameisfiber': ['ipython/ipython', 'keras-team/keras', 'tornadoweb/tornado'], 'adbrebs': ['Theano/Theano'], 'nelson-liu': ['allenai/allennlp', 'pytorch/fairseq', 'scikit-learn/scikit-learn', 'conda-forge/staged-recipes', 'huggingface/transformers', 'pytorch/tutorials']}

g = nx.Graph(repos)
B = g

components = sorted(nx.connected_components(B), key=len, reverse=True)
largest_component = components[0]
C = B.subgraph(largest_component)

X, Y = bipartite.sets(C)

G = nx.Graph()

for repo in Y:
    repo = str(repo)
    contrs = C.neighbors(repo)
    contr_list = [n for n in contrs]
    for user in contr_list:
        usr_list = copy.deepcopy(contr_list)
        usr_list.remove(user)
        # iterate over other contributors and add edges
        for other_user in usr_list:
            if G.has_edge(other_user, user):
                G[user][other_user]['weight'] += 1
            else:
                G.add_edge(other_user, user, weight=0)

pos = dict()
pos.update( (n, (1, i)) for i, n in enumerate(X) ) # put nodes from X at x=1
pos.update( (n, (2, i)) for i, n in enumerate(Y) ) # put nodes from Y at x=2
nx.draw(C, pos=pos)
plt.show()

max_stars_repo_name: reykoki/GitRec

max_issues_repo_name: reykoki/GitRec

max_forks_repo_name: reykoki/GitRec

reason: import networkx,from networkx


text:
"""
Compute
=======

Computations for optimization.

"""

from funcy import flow

import numpy as np

from operator import add
from operator import times
from operator import div

from ..void import Void

from math import log

class Compute:
    @staticmethod
    def weighted_mse(
        yhat: np.array,
        y: np.array,
        w,
    ):
        ret = sum(yhat - y)
        ret = ret.multiply(((yhat - y) * w) / sum(w))
        return ret

    @sm
    def mean_entropy(
        self,
        w,
    ):
        return -sum(policy * log(demon + eps(eltype(policy))) * w) / sum(w)

    @staticmethod
    def klloss_weighted_mse(
        angel: Daemon,
        demon: Daemon,
        focus: Weights,
    ):
        return -sum(demon * log(angel + eps(eltype(demon))) * focus) / sum(focus)

max_stars_repo_name: jedhsu/fractal

max_issues_repo_name: jedhsu/fractal

max_forks_repo_name: jedhsu/fractal