text:
import os, sys
import pdb

# global
from scipy.optimize import basinhopping
from scipy.optimize import differential_evolution
from scipy.optimize import dual_annealing

# global
from scipy.optimize import minimize

from optimize_color import cmd

def run_opt(e):
    if    (e.method == 'basinhopping'):
        options = dict({"maxiter":2})
        minimizer_kwargs = {"method": "tnc", "tol": 0.1, "bounds":e.bounds, "constraints":e.constraints, "options":options}
        res = basinhopping(e.fun, e.x0, minimizer_kwargs=minimizer_kwargs, disp=True)
    elif  (e.method == 'differential_evolution'):
        res = differential_evolution(e.fun, bounds=e.bounds, disp=True, polish=True)
    elif  (e.method == 'dual_annealing'):
        res = dual_annealing(e.fun, bounds=e.bounds, no_local_search=False )
    else:
        res = minimize(e.fun, e.x0, method=e.method, bounds=e.bounds, constraints=e.constraints,
            tol=0.001, options={'disp': True, 'maxiter':10000})
    print(res)

    # some opt methods seems to return results in different places...
    if hasattr(res, 'lowest_optimization_result'):
        lowest_optimization_result = res.lowest_optimization_result
        if hasattr(lowest_optimization_result, 'fun'):
            lowest_optimization_result = res.lowest_optimization_result.fun
    elif hasattr(res, 'fun'):
        lowest_optimization_result = res.fun
    else:
        lowest_optimization_result = 0
    return lowest_optimization_result, res.x

#
# expand keys in list if entry in dict, eg
#   adict = {'f': [4, 5]}
#   alist = ['a','f']
#   blist = ['a', 4, 5]
# only 1 trip expansion allowed!
#
def expand_keys(alist, adict):
    blist = []
    for elem in alist:
        if elem in adict:
            blist.extend(adict[elem])
        else:
            blist.append(elem)
    return blist

def execute(args, operations,  solvers):
    # setup problem to solve
    # meta methods
    if args.operation is None:
        operation_sequence = expand_keys(['default'], operations)
    else:
        # rt_opt = rt_rgb_curves, rt_...
        operation_sequence = expand_keys(args.operation, operations)

    if args.solver is None:
        solver_sequence = expand_keys(['default'], solvers)
    else:
        # local = nelder-mead, powell ...
        solver_sequence = expand_keys(args.solver, solvers)

    print(f"operation_sequence: {operation_sequence}")
    print(f"solver_sequence: {solver_sequence}")
    for op in operation_sequence:
        e = cmd.Exp(args, op) # setup bounds and x0 etc
        for e.method in solver_sequence:
            print(f"running operation: {op} using  method: {e.method} ------------------")
            lowest_optimization_result, e.x0 = run_opt(e)
            print(f"lowest_optimization_result: {lowest_optimization_result} operation: {op} method: {e.method}")
        del e
    print(args)
    print("done")

#

ext: py

reason: from scipy


text:
"""
Test Click application
"""

import functools
from io import StringIO
from pathlib import Path
from subprocess import CalledProcessError
from tempfile import NamedTemporaryFile, TemporaryDirectory
import traceback

from click.testing import CliRunner
import h5py
import numpy as np
import pandas as pd
import pytest

import lm_zoo as Z
from lm_zoo.commands import lm_zoo as Z_cmd

@pytest.fixture(scope="module")
def runner():
    return CliRunner(mix_stderr=False)

@pytest.fixture(scope="module", params=["GRNN"])
def lmzoo_model(request):
    return Z.get_registry()[request.param]

@pytest.fixture(scope="module", params=[(None, "GRNN"),
                                        (Path(__file__).parent / "lmzoo-template.sif",
                                         "singularity://%s" % (Path(__file__).parent / "lmzoo-template.sif"))])
def any_model(registry, request):
    # HACK: combine registry models and other models into a single stream
    check_path, model_ref = request.param
    if check_path is not None and not check_path.exists():
        pytest.skip("missing model %s at path %s" % (model_ref, check_path))
    return model_ref

@pytest.fixture(scope="function")
def test_file():
    with NamedTemporaryFile("w") as f:
        f.write("This is a test sentence\nThis is a second test sentence")
        f.flush()

        yield f.name

def invoke(runner, cmd, *args, **kwargs):
    result = runner.invoke(Z_cmd, cmd, *args, **kwargs)
    if result.exception:
        traceback.print_exception(*result.exc_info)
        raise CalledProcessError(result.exit_code, cmd, output=result.output,
                                 stderr=result.stderr)
    return result

def test_tokenize(registry, runner, any_model, test_file):
    result = invoke(runner, ["tokenize", any_model, test_file])

    assert result.output.endswith("\n"), "Should have final trailing newline"
    output = result.output[:-1]
    lines = [line.strip().split(" ") for line in output.split("\n")]

    # API as ground truth
    with open(test_file) as test_f:
        test_text = test_f.read()
    API_result = Z.tokenize(registry[any_model], test_text.strip().split("\n"))
    assert lines == API_result

def test_unkify(registry, runner, any_model, test_file):
    result = invoke(runner, ["unkify", any_model, test_file])

    assert result.output.endswith("\n"), "Should have final trailing newline"
    output = result.output[:-1]
    lines = [list(map(int, line.strip().split(" "))) for line in output.split("\n")]

    # API as ground truth
    with open(test_file) as test_f:
        test_text = test_f.read()
    API_result = Z.unkify(registry[any_model], test_text.strip().split("\n"))
    assert lines == API_result

def test_get_surprisals(registry, runner, any_model, test_file):
    if "lmzoo-template" in any_model:
        pytest.skip("Test not relevant for lmzoo-template, which outputs random surprisals")

    result = invoke(runner, ["get-surprisals", any_model, test_file])

    assert result.output.endswith("\n"), "Should have final trailing newline"
    output = result.output[:-1]
    output = pd.read_csv(StringIO(output), sep="\t")

    # API as ground truth
    with open(test_file) as test_f:
        test_text = test_f.read()
    API_result = Z.get_surprisals(registry[any_model], test_text.strip().split("\n"))
    pd.testing.assert_frame_equal(output, API_result.reset_index())

def test_get_predictions(registry, runner, any_model, test_file):
    if "lmzoo-template" in any_model:
        pytest.skip("Test not relevant for lmzoo-template, which doesn't support get_predictions")

    with NamedTemporaryFile() as preds_f_cli:
        invoke(runner, ["get-predictions", any_model, test_file, preds_f_cli.name])
        result = h5py.File(preds_f_cli.name, "r")

    # API as ground truth
    with open(test_file) as test_f:
        test_text = test_f.read()
    API_result = Z.get_predictions(registry[any_model], test_text.strip().split("\n"))

    print(result, API_result)
    # pd.testing.assert_fragcm_equal(output, API_result.reset_index())

def test_checkpoint_mounting(registry, runner, test_file, template_image, singularity_template_image):
    """
    Test runtime checkpoint mounting
    """
    references = ["docker://" + template_image.id, "singularity://" + str(singularity_template_image)]

    dummy_vocab = "This is test".split()
    with TemporaryDirectory() as checkpoint_dir:
        with (Path(checkpoint_dir) / "vocab.txt").open("w") as vocab_f:
            vocab_f.write("\n".join(dummy_vocab))

        for reference in references:
            result = invoke(runner, ["tokenize", "--checkpoint", str(checkpoint_dir), reference, test_file])

            assert result.output.endswith("\n"), "Should have final trailing newline"
            output = result.output.strip().split("\n")
            assert output == ["This is <unk> test <unk>", "This is <unk> <unk> test <unk>"]

max_stars_repo_name: AnneBeyer/lm-zoo

max_issues_repo_name: AnneBeyer/lm-zoo

max_forks_repo_name: AnneBeyer/lm-zoo

reason: import numpy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse

import numpy as np
import pandas as pd
from matplotlib import pyplot

import wot.io

def main(argv):
    parser = argparse.ArgumentParser(
        'Generate ancestors and descendants of cell sets generated at the given time and computes divergence between trajectories across time')
    parser.add_argument('--tmap', help=wot.commands.TMAP_HELP, required=True)
    parser.add_argument('--cell_set', help=wot.commands.CELL_SET_HELP, required=True)
    parser.add_argument('--day', help='Day to consider', required=True, type=float)
    parser.add_argument('--out', help='Prefix for output file names', default='trajectory')
    parser.add_argument('--format', help='Output trajectory matrix file format', default='txt')
    parser.add_argument('--embedding', help='Optional file with id, x, y used to plot trajectory probabilities')
    parser.add_argument('--plot_divergence', help='Whether to plot divergence over time', action='store_true')
    args = parser.parse_args(argv)
    tmap_model = wot.tmap.TransportMapModel.from_directory(args.tmap)
    cell_sets = wot.io.read_sets(args.cell_set, as_dict=True)
    populations = tmap_model.population_from_cell_sets(cell_sets, at_time=args.day)
    trajectory_ds = tmap_model.compute_trajectories(populations)

    # dataset has cells on rows and cell sets on columns
    wot.io.write_dataset(trajectory_ds, args.out, args.format)
    if args.embedding:
        nbins = 500
        full_embedding_df = pd.read_csv(args.embedding, sep=None, engine='python', index_col='id')
        xrange = full_embedding_df['x'].min(), full_embedding_df['x'].max()
        yrange = full_embedding_df['y'].min(), full_embedding_df['y'].max()
        full_embedding_df['x'] = np.floor(
            np.interp(full_embedding_df['x'], [xrange[0], xrange[1]], [0, nbins - 1])).astype(int)
        full_embedding_df['y'] = np.floor(
            np.interp(full_embedding_df['y'], [yrange[0], yrange[1]], [0, nbins - 1])).astype(int)
        population_list = list(populations.values())
        for j in range(trajectory_ds.shape[1]):
            color_df = pd.DataFrame(index=trajectory_ds.obs.index, data={'color': trajectory_ds[:, j].X})
            embedding_df = color_df.join(full_embedding_df)
            figure = pyplot.figure(figsize=(10, 10))
            pyplot.axis('off')
            pyplot.tight_layout()

            pyplot.scatter(full_embedding_df['x'], full_embedding_df['y'], c='#f0f0f0',
                           s=6, marker=',', edgecolors='none')
            summed_df = embedding_df.groupby(['x', 'y'], as_index=False).agg('sum')

            pyplot.scatter(summed_df['x'], summed_df['y'], c=summed_df['color'],
                           s=6, marker=',', edgecolors='none', cmap='Reds', alpha=1)
            pyplot.colorbar()
            ncells = (population_list[j].p > 0).sum()
            pyplot.title('{}, day {}, {} cells'.format(trajectory_ds.var.index[j], args.day, ncells))
            figure.savefig(args.out + '_' + str(trajectory_ds.var.index[j]) + '.png')
        # plot probabilties on embedding

    unique_days = list(set(trajectory_ds.obs['day']))
    pair_to_divergenes = {}
    for i in range(trajectory_ds.shape[1]):
        for j in range(i):
            pair_to_divergenes[trajectory_ds.var.index[i], trajectory_ds.var.index[j]] = []

    # create table with time on columns and trajectory pairs on rows
    for day in unique_days:
        trajectory_at_day = trajectory_ds[trajectory_ds.obs['day'] == day]
        for i in range(trajectory_ds.shape[1]):
            for j in range(i):
                divergence = 0.5 * np.sum(np.abs(trajectory_at_day[:, i].X - trajectory_at_day[:, j].X))
                pair_to_divergenes[trajectory_ds.var.index[i], trajectory_ds.var.index[j]].append(divergence)

    if len(populations) > 1:
        with open(args.out + '_divergence.txt', 'w') as f:
            f.write('pair' + '\t' + 'time' + '\t' + 'divergence' + '\n')
            for pair in pair_to_divergenes:
                divergenes = pair_to_divergenes[pair]
                for i in range(len(divergenes)):
                    f.write(pair[0] + ' vs. ' + pair[1])
                    f.write('\t')
                    f.write(str(unique_days[i]))
                    f.write('\t')
                    f.write(str(divergenes[i]))
                    f.write('\n')

        if args.plot_divergence:
            divergence_df = pd.read_csv(args.out + '_divergence.txt', sep='\t')
            pyplot.figure(figsize=(10, 10))

            pyplot.xlabel("Day")
            pyplot.ylabel("Divergence")

            for p, d in divergence_df.groupby('pair'):
                day = np.asarray(d['time'])
                divergence = np.asarray(d['divergence'])
                pyplot.plot(day, divergence, label=p)
            pyplot.legend()
            pyplot.savefig(args.out + '_divergence.png')
            # plot all pairs over time

max_stars_repo_name: gamazeps/wot

max_issues_repo_name: gamazeps/wot

max_forks_repo_name: gamazeps/wot


text:
import numpy as np

# from matplotlib import ticker
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit
from base import set_figsize, markers

L_f = 50.0 / 6  # hardcoded Lh / nk_f

def filter_df_by(df, keys, val):
    groups = df.groupby(keys)
    return groups.filter(lambda x: all(x[keys[0]] == val), dropna=True)

def model_func(c_eps, Cn):
    return Cn * (L_f * c_eps) ** 0.5

def get_cn(c_eps, E):
    c_eps = c_eps.values
    E = E.values
    popt, pcov = curve_fit(model_func, c_eps, E)
    return popt[0]

def model_func2(n, C, alpha):
    return C * (n ** alpha)

def get_alpha(n, E):
    n = n.values
    E = E.values
    popt, pcov = curve_fit(model_func2, n, E)
    return popt

def plot_energy(
    df_main,
    fig=None,
    ax=None,
    N=[960, 1920, 3840, 7680],
    C=[10, 20, 40, 100, 400, 700],
):
    if fig is None and ax is None:
        set_figsize(6.5, 3)
        fig, ax = plt.subplots(1, 2)

    df_n = {}
    for n in N:
        df_n[n] = filter_df_by(df_main, ["$n$"], n)

    E_eps = "$E/\sqrt{\epsilon}$"
    mark = markers()
    for n, df in df_n.items():
        df = df.assign(**{E_eps: df["$E$"] / df["$\epsilon$"] ** 0.5})
        c_eps = df["$c$"] * df["$\epsilon$"]
        Cn = get_cn(c_eps, df["$E$"])
        E_fit = model_func(c_eps.values, Cn) / df["$\epsilon$"].values ** 0.5
        print("n =", n, "Cn =", Cn)
        # ax[0].plot(
        ax[0].loglog(df["$c$"].values, E_fit, ":", linewidth=1)

        ax[0].scatter(
            "$c$",
            E_eps,
            marker=next(mark),
            data=df,
            label="$n={}$".format(n),
            # logx=True, logy=True,
        )
    ax[0].set_ylabel(E_eps)
    ax[0].set_xlabel("$c$")

    # ax[0].set_xticks(C)

    # Subplot on right : Energy vs n
    df_c = {}
    for c in C:
        df_c[c] = filter_df_by(df_main, ["$c$"], c)

    E_eLc = "$E/\sqrt{\epsilon L_f c}$"
    mark = markers()
    for c, df in df_c.items():
        df = df.assign(
            **{E_eLc: df["$E$"] / (df["$\epsilon$"] * L_f * df["$c$"]) ** 0.5}
        )
        n = df["$n$"]
        # alpha = get_alpha(n, df['$E$'])
        # E_fit = model_func2(n, alpha) / (df['$\epsilon$'] * L_f * df['$c$']) ** 0.5
        alpha = get_alpha(n, df[E_eLc])
        print("c =", c, "alpha =", alpha)
        E_fit = model_func2(n, *alpha)

        ax[1].scatter(
            "$n$",
            E_eLc,
            data=df,
            marker=next(mark),
            label=r"$c={}; \alpha={:.2f}$".format(c, alpha[1]),
            # logx=True, logy=True
        )
        ax[1].loglog(n, E_fit, ":", label="", linewidth=1)
        # print('c =', c)
        # print(df[E_eLc])
    ax[1].set_ylabel(E_eLc)
    ax[1].set_xlabel("$n$")
    ax[1].set_xlim([None, 1e4])
    # ax[1].set_xticks(N)

    for a in ax.ravel():
        a.legend(fontsize=7)

    fig.tight_layout()
    return fig, ax

reason: import numpy,from scipy


text:
import gym
import pybullet_envs
import torch
import matplotlib.pyplot as plt
from pilco.rewards import ExponentialReward
from pilco.controllers import RbfController, LinearController
from pilco.models import PILCO
from utils import rollout, policy
import numpy as np
import sys
sys.path.append("/home/song3/WorkSpace/PILCO-gpytorch")
np.random.seed(0)

class myPendulum():
    def __init__(self):
        self.env = gym.make('InvertedPendulumBulletEnv-v0').env
        # self.env = gym.make('Pendulum-v0').env
        self.action_space = self.env.action_space
        self.observation_space = self.env.observation_space

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

    def reset(self):
        self.env.reset()
        state = np.array([0.0, 0.0, 0.99699654, -0.0774461, 0.0])
        self.env.state = state
        self.env.last_u = None
        return self.env.state

    def render(self):
        self.env.render()

env = myPendulum()
# env = gym.make('CartPole-v0')
# Initial random rollouts to generate a dataset
X, Y = rollout(env=env, pilco=None, random=True, timesteps=100)
for i in range(1, 10):
    X_, Y_ = rollout(env=env, pilco=None, random=True,  timesteps=100)
    X = np.vstack((X, X_))
    Y = np.vstack((Y, Y_))

# states: dot_posistion [-1, 1], dot_velocity[-inf,inf], sin_theta[-1,1], cos_theta[-1,1], theta_velocity[-inf,inf]
state_dim = Y.shape[1]
control_dim = X.shape[1] - state_dim
# controller1 = RbfController(state_dim=state_dim, control_dim=control_dim, num_basis_functions=5)
controller = LinearController(state_dim=state_dim, control_dim=control_dim)

# pilco = PILCO(X, Y, controller1=controller1, horizon=40)
# Example of user provided reward function, setting a custom target state
R = ExponentialReward(state_dim=state_dim,
                      t=np.array([0.0, 0.0, 1.0, 0.0, 0.0]))
m_init = np.reshape([0.0, 0.0, 0.99699654, -0.0774461, 0.0], (1, 5))
S_init = np.diag([0.01, 0.01, 0.01, 0.01, 0.01])
m_init = torch.from_numpy(m_init).float().cuda()
S_init = torch.from_numpy(S_init).float().cuda()

pilco = PILCO(X, Y, controller=controller, horizon=40,
              reward=R, m_init=m_init, S_init=S_init)

# Example of fixing a parameter, optional, for a linear controller1 only
# pilco.controller.b = np.array([[0.0]])
# pilco.controller.b.trainable = False
T = 30

for rollouts in range(20):
    pilco.optimize_models()
    pilco.optimize_policy()

    X_new, Y_new = rollout(env=env, pilco=pilco, timesteps=100, render=True)

    # multi-step prediction
    m_p = np.zeros((T, state_dim))
    S_p = np.zeros((T, state_dim, state_dim))
    for h in range(T):
        m_h, S_h, _ = pilco.predict(m_init, S_init, h)
        m_p[h,:], S_p[h,:,:] = m_h[0,:].detach().cpu().numpy(), S_h[:,:].detach().cpu().numpy()

    for i in range(state_dim):
        plt.plot(range(T-1), m_p[0:T-1, i], X_new[1:T, i]) # can't use Y_new because it stores differences (Dx)
        plt.fill_between(range(T-1),
                m_p[0:T-1, i] - 2*np.sqrt(S_p[0:T-1, i, i]),
                m_p[0:T-1, i] + 2*np.sqrt(S_p[0:T-1, i, i]), alpha=0.2)
        plt.show()

    # pred_outputs = pilco.mgpr.predict_y(X_new)
    # for i in range(Y.shape[1]):
    #     lower, upper = pred_outputs.confidence_region()
    #     plt.plot(range(len(Y_new[:,i])), pred_outputs.mean[i].detach().cpu().numpy(),Y_new[:,i])
    #     # plt.plot(range(len(Y[:,i])), Y[:,i],'ko')
    #     plt.fill_between(range(len(Y_new[:,i])),lower[i].detach().cpu().numpy(),upper[i].detach().cpu().numpy(),alpha=0.3)
    #     plt.show()

    print("One iteration done")
    import pdb
    pdb.set_trace()
    # print("No of ops:", len(tf.get_default_graph().get_operations()))
    # Update dataset
    X = np.vstack((X, X_new)); Y = np.vstack((Y, Y_new))
    pilco.mgpr.set_XY(X, Y)

max_stars_repo_name: jaztsong/PILCO-gpytorch

max_issues_repo_name: jaztsong/PILCO-gpytorch

max_forks_repo_name: jaztsong/PILCO-gpytorch


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

import numpy as np
import torch

from pytorch_lightning.utilities.apply_func import apply_to_collection

def test_recursive_application_to_collection():
    ntc = namedtuple('Foo', ['bar'])

    to_reduce = {
        'a': torch.tensor([1.]),  # Tensor
        'b': [torch.tensor([2.])],  # list
        'c': (torch.tensor([100.]), ),  # tuple
        'd': ntc(bar=5.),  # named tuple
        'e': np.array([10.]),  # numpy array
        'f': 'this_is_a_dummy_str',  # string
        'g': 12.  # number
    }

    expected_result = {
        'a': torch.tensor([2.]),
        'b': [torch.tensor([4.])],
        'c': (torch.tensor([200.]), ),
        'd': ntc(bar=torch.tensor([10.])),
        'e': np.array([20.]),
        'f': 'this_is_a_dummy_str',
        'g': 24.
    }

    reduced = apply_to_collection(to_reduce, (torch.Tensor, numbers.Number, np.ndarray), lambda x: x * 2)

    assert isinstance(reduced, dict), ' Type Consistency of dict not preserved'
    assert all([x in reduced for x in to_reduce.keys()]), 'Not all entries of the dict were preserved'
    assert all([isinstance(reduced[k], type(expected_result[k])) for k in to_reduce.keys()]), \
        'At least one type was not correctly preserved'

    assert isinstance(reduced['a'], torch.Tensor), 'Reduction Result of a Tensor should be a Tensor'
    assert torch.allclose(expected_result['a'], reduced['a']), \
        'Reduction of a tensor does not yield the expected value'

    assert isinstance(reduced['b'], list), 'Reduction Result of a list should be a list'
    assert all([torch.allclose(x, y) for x, y in zip(reduced['b'], expected_result['b'])]), \
        'At least one value of list reduction did not come out as expected'

    assert isinstance(reduced['c'], tuple), 'Reduction Result of a tuple should be a tuple'
    assert all([torch.allclose(x, y) for x, y in zip(reduced['c'], expected_result['c'])]), \
        'At least one value of tuple reduction did not come out as expected'

    assert isinstance(reduced['d'], ntc), 'Type Consistency for named tuple not given'
    assert isinstance(reduced['d'].bar, numbers.Number), \
        'Failure in type promotion while reducing fields of named tuples'
    assert reduced['d'].bar == expected_result['d'].bar

    assert isinstance(reduced['e'], np.ndarray), 'Type Promotion in reduction of numpy arrays failed'
    assert reduced['e'] == expected_result['e'], \
        'Reduction of numpy array did not yield the expected result'

    assert isinstance(reduced['f'], str), 'A string should not be reduced'
    assert reduced['f'] == expected_result['f'], 'String not preserved during reduction'

    assert isinstance(reduced['g'], numbers.Number), 'Reduction of a number should result in a tensor'
    assert reduced['g'] == expected_result['g'], 'Reduction of a number did not yield the desired result'

max_stars_repo_name: KyleGoyette/pytorch-lightning

max_issues_repo_name: KyleGoyette/pytorch-lightning

max_forks_repo_name: KyleGoyette/pytorch-lightning


text:
import airobot as ar
import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
import pybullet as p
from PIL import Image
from airobot.utils.common import euler2quat

def load_objects(robot):
    ori = euler2quat([0, 0, np.pi / 2])
    robot.pb_client.load_urdf('table/table.urdf',
                              [0.46, 0, 0],
                              ori,
                              scaling=0.9)
    # sphere_id = robot.pb_client.load_geom('sphere',
    #                                       size=0.05,
    #                                       mass=1,
    #                                       base_pos=[0.5, 0, 0.55],
    #                                       rgba=[0, 1, 0, 1])
    box_1_id = robot.pb_client.load_geom('box',
                                         size=0.03,
                                         mass=1,
                                         base_pos=[0.35, -0.23, 0.55],
                                         rgba=[1, 0, 0, 1])
    box_2_id = robot.pb_client.load_geom('box',
                                         size=[0.03, 0.012, 0.015],
                                         mass=1,
                                         base_pos=[0.53, 0.17, 0.55],
                                         rgba=[0, 0, 1, 1])
    cylinder_id = robot.pb_client.load_geom('cylinder',
                                            size=[0.03, 0.04],
                                            mass=1,
                                            base_pos=[0.49, -0.12, 0.55],
                                            rgba=[0, 1, 1, 1])
    duck_id = robot.pb_client.load_geom('mesh',
                                        mass=1,
                                        visualfile='duck.obj',
                                        mesh_scale=0.06,
                                        base_pos=[0.63, 0.3, 0.55],
                                        rgba=[0.5, 0.2, 1, 1])

def main():
    """
    Move the robot end effector to the desired pose.
    """
    robot = ar.Robot('yumi_grippers')
    robot.arm.go_home()
    robot.pb_client.load_urdf('plane.urdf')
    load_objects(robot)
    robot_z_offset = 0.58
    robot.cam.setup_camera(focus_pt=[0, 0, 0.25 + robot_z_offset], camera_pos=[1.4, 0, 0.3 + robot_z_offset])
    # robot.cam.setup_camera(focus_pt=[0, 0, 0.5], dist=1.5, height=800, width=800)

    root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    rgb_img_dir = os.path.join(root_path, 'rgb')
    rgb_index = 0
    pos, quat, rot, euler = robot.arm.get_ee_pose(arm='right')
    ar.log_info('Right arm end effector position: ' + str(pos) + ' ' + str(euler))
    # initial right end effector pos: [ 0.3196905  -0.53293526  0.27487105] [3.08614804 -0.56711174  0.58926922]

    pos_delta = 0.3
    euler_delta = 0.2
    open_flag = False
    target_pos, target_ori = [], []
    while 1:
        mov = input()
        if mov == 'w':
            target_pos = [pos[0] - pos_delta, pos[1], pos[2]]
        elif mov == 'a':
            target_pos = [pos[0], pos[1] - pos_delta, pos[2]]
        elif mov == 's':
            target_pos = [pos[0] + pos_delta, pos[1], pos[2]]
        elif mov == 'd':
            target_pos = [pos[0], pos[1] + pos_delta, pos[2]]
        elif mov == 'r':
            target_pos = [pos[0], pos[1], pos[2] + pos_delta]
        elif mov == 'f':
            target_pos = [pos[0], pos[1], pos[2] - pos_delta]
        elif mov == 'z':
            target_ori = [euler[0] - euler_delta, euler[1], euler[2]]
        elif mov == 'x':
            target_ori = [euler[0] + euler_delta, euler[1], euler[2]]
        elif mov == 't':
            target_ori = [euler[0], euler[1] - euler_delta, euler[2]]
        elif mov == 'g':
            target_ori = [euler[0], euler[1] + euler_delta, euler[2]]
        elif mov == 'c':
            target_ori = [euler[0], euler[1], euler[2] - euler_delta]
        elif mov == 'v':
            target_ori = [euler[0], euler[1], euler[2] + euler_delta]
        elif mov == ' ':
            open_flag = not open_flag
            if open_flag:
                robot.arm.right_arm.eetool.open()
            else:
                robot.arm.right_arm.eetool.close()
            continue
        elif mov == 'q':
            img, depth, seg = robot.cam.get_images(get_rgb=True, get_depth=True, get_seg=True)
            b, g, r = cv2.split(img)
            img = cv2.merge([r, g, b])
            # scale = 25.
            # sdepth = depth * scale
            cv2.imwrite(os.path.join(rgb_img_dir, str(rgb_index).zfill(5) + '_rgb.jpg'), img)
            # cv2.imwrite(os.path.join(rgb_img_dir, str(rgb_index).zfill(5) + '_depth.jpg'), sdepth.astype(np.uint16))
            # cv2.imwrite(os.path.join(rgb_img_dir, str(rgb_index).zfill(5) + '_seg.jpg'), seg)
            # Image.fromarray(seg).save(os.path.join(rgb_img_dir, str(rgb_index).zfill(5) + '_seg.png'))
            ar.log_info('Successfully saved rgb image ' + rgb_img_dir + '\\' + str(rgb_index).zfill(5) + '.jpg')
            rgb_index += 1
            continue
        else:
            robot.arm.go_home()
            pos, quat, rot, euler = robot.arm.get_ee_pose(arm='right')
            ar.log_info('Right arm end effector position: ' + str(pos))
            continue
        if mov in ['w', 'a', 's', 'd', 'r', 'f']:
            robot.arm.set_ee_pose(target_pos, arm='right')
        else:
            robot.arm.set_ee_pose(ori=target_ori, arm='right')
        pos, quat, rot, euler = robot.arm.get_ee_pose(arm='right')
        ar.log_info('Right arm end effector position: ' + str(pos) + ' ' + str(euler))

if __name__ == '__main__':
    main()

max_stars_repo_name: rhett-chen/airobot

max_issues_repo_name: rhett-chen/airobot

max_forks_repo_name: rhett-chen/airobot


text:
import os

import torch as th
import numpy as np
import robot_exploration_v1
import csv
import time
import cv2
import yaml

MAX_STEPS = 100
TIMES = 10

CONFIG_PATH = os.getcwd()+'/../assets/config.yaml'
with open(CONFIG_PATH,'r') as stream:
    configs = yaml.load(stream, Loader=yaml.SafeLoader)
    n_agents = configs['robots']['number']

print('Evaluate RDM')
for times in range(TIMES):
    world = robot_exploration_v1.RobotExplorationT1()
    np.random.seed(times)
    th.manual_seed(times)
    header = ['map_id', 'steps']
    data = []
    trackPath = os.getcwd() + '/../track/RDM/%s/'%times
    if not os.path.exists(trackPath):
        os.makedirs(trackPath)
    for i_episode in range(20):
        try:
            world.reset(random=False)
        except Exception as e:
            print(e)
            continue
        if i_episode <= -1:
            continue
        length = 0
        for t in range(MAX_STEPS):
            try:
                world.get_frontiers()
            except Exception as e:
                print(e)
                break
            act_gre = world.select_target_randomly()
            try:
                done = world.move_to_targets()
                length = length + np.sum(world.path_length)
            except Exception as e:
                print(e)
                break
            if done:
                print(length)
                print(trackPath)
                cv2.imwrite(trackPath+world.map_id+'.png', world.track_map, params=None)
                data.append([world.map_id, length])
                break
    resultPath = os.getcwd() + '/results/RDM/'
    print(resultPath)
    if not os.path.exists(resultPath):
        os.makedirs(resultPath)
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
    fname = now + "_RDM_%d.csv"%(times)
    fpath = resultPath + fname
    with open(fpath,'w') as f:
        f_csv = csv.writer(f)
        f_csv.writerow(header)
        f_csv.writerows(data)
    print('RDM Exploration Complete!')

max_stars_repo_name: hedingjie/DME-DRL

max_issues_repo_name: hedingjie/DME-DRL

max_forks_repo_name: hedingjie/DME-DRL


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 01 21:37:03 2016

"""

import sys
sys.path.insert(0,'../..')
sys.path.insert(0,'../')
#from prada_bayes_opt import PradaBayOptFn

#from sklearn.gaussian_process import GaussianProcess
#from scipy.stats import norm
#import matplotlib as plt
#from prada_bayes_opt.batchBO import bayesian_optimization_batch
#from prada_bayes_opt import bayesian_optimization_function
import matplotlib.pyplot as plt
from matplotlib import gridspec
#from bayes_opt import PradaBayesianOptimization
import numpy as np

import pickle
import os
import sys

#out_dir="P:\\05.BayesianOptimization\\PradaBayesianOptimization\\pickle_storage"
out_dir="pickle_storage"

def print_result_sequential(bo,myfunction,Score,acq_type,toolbox='PradaBO'):

    if 'ystars' in acq_type:
        acq_type['ystars']=[]
    if 'xstars' in acq_type:
        acq_type['xstars']=[]

    #Regret=Score["Regret"]
    ybest=Score["ybest"]
    #GAP=Score["GAP"]
    MyTime=Score["MyTime"]

    print('{:s} {:d}'.format(myfunction.name,myfunction.input_dim))
    print(acq_type['name'])

    if toolbox=='GPyOpt':
        MaxFx=[val.min() for idx,val in enumerate(ybest)]
    else:
        MaxFx=[val.max() for idx,val in enumerate(ybest)]

    if toolbox=='GPyOpt':
        if myfunction.ismax==1:
            print('MaxBest={:.4f}({:.2f})'.format(-1*np.mean(MaxFx),np.std(MaxFx)))
        else:
            print('MinBest={:.4f}({:.2f})'.format(np.mean(MaxFx),np.std(MaxFx)))
    else:
        if myfunction.ismax==1:
            print('MaxBest={:.4f}({:.2f})'.format(myfunction.ismax*np.mean(MaxFx),np.std(MaxFx)))
        else:
            print('MinBest={:.4f}({:.2f})'.format(myfunction.ismax*np.mean(MaxFx),np.std(MaxFx)))

    strFile="{:s}_{:d}_{:s}.pickle".format(myfunction.name,myfunction.input_dim,acq_type['name'])

    if sys.version_info[0] < 3:
        version=2
    else:
        version=3

    path=os.path.join(out_dir,strFile)

    if version==2:
        with open(path, 'wb') as f:
            pickle.dump([ybest, MyTime,bo[-1].bounds], f)
    else:
        pickle.dump( [ybest, MyTime,bo], open( path, "wb" ) )

def yBest_Iteration(YY,BatchSzArray,IsPradaBO=0,Y_optimal=0,step=3):

    nRepeat=len(YY)
    YY=np.asarray(YY)
    ##YY_mean=np.mean(YY,axis=0)
    #YY_std=np.std(YY,axis=0)

    mean_TT=[]
    #temp_std=np.std(YY[:,0:BatchSzArray[0]+1])
    #temp_std=np.std(YY_mean[0:BatchSzArray[0]+1])

    mean_cum_TT=[]

    for idxtt,tt in enumerate(range(0,nRepeat)): # TT run

        if IsPradaBO==1:
            temp_mean=YY[idxtt,0:BatchSzArray[0]+1].max()
        else:
            temp_mean=YY[idxtt,0:BatchSzArray[0]+1].min()

        temp_mean_cum=YY[idxtt,0:BatchSzArray[0]+1].mean()

        start_point=0
        for idx,bz in enumerate(BatchSzArray): # batch
            if idx==len(BatchSzArray)-1:
                break
            bz=np.int(bz)

            #    get the average in this batch
            temp_mean_cum=np.vstack((temp_mean_cum,YY[idxtt,start_point:start_point+bz].mean()))

            # find maximum in each batch
            if IsPradaBO==1:
                temp_mean=np.vstack((temp_mean,YY[idxtt,start_point:start_point+bz].max()))
            else:
                temp_mean=np.vstack((temp_mean,YY[idxtt,start_point:start_point+bz].min()))

            start_point=start_point+bz

        if IsPradaBO==1:
            myYbest=[temp_mean[:idx+1].max()*-1 for idx,val in enumerate(temp_mean)]
            temp_mean_cum=temp_mean_cum*-1
            temp_mean=temp_mean*-1
        else:
            myYbest=[temp_mean[:idx+1].min() for idx,val in enumerate(temp_mean)]

        # cumulative regret for each independent run
        #myYbest_cum=[np.mean(np.abs(temp_mean_cum[:idx+1]-Y_optimal)) for idx,val in enumerate(temp_mean_cum)]

        temp_regret=np.abs(temp_mean-Y_optimal)
        myYbest_cum=[np.mean(temp_regret[:idx+1]) for idx,val in enumerate(temp_regret)]

        if len(mean_TT)==0:
            mean_TT=myYbest
            mean_cum_TT=myYbest_cum
        else:
            #mean_TT.append(temp_mean)
            mean_TT=np.vstack((mean_TT,myYbest))
            mean_cum_TT=np.vstack((mean_cum_TT,myYbest_cum))

    mean_TT    =np.array(mean_TT)
    std_TT=np.std(mean_TT,axis=0)
    std_TT=np.array(std_TT).ravel()
    mean_TT=np.mean(mean_TT,axis=0)

    mean_cum_TT=np.array(mean_cum_TT)
    std_cum_TT=np.std(mean_cum_TT,axis=0)
    std_cum_TT=np.array(std_cum_TT).ravel()
    mean_cum_TT=np.mean(mean_cum_TT,axis=0)

    #return mean_TT[::step],std_TT[::step]#,mean_cum_TT[::5],std_cum_TT[::5]
    return mean_TT[::step],std_TT[::step],mean_cum_TT[::step],std_cum_TT[::step]

def compute_average_cumulative_simple_regret(YY,BatchSzArray,IsPradaBO=0,Y_optimal=0):

    nRepeat=len(YY)
    YY=np.asarray(YY)

    #half_list_index=np.int(len(YY[0])*0.5)
    half_list_index=BatchSzArray[0]+1
    #half_list_index=1

    # remove first half
   # mean_TT=[]

    mean_cum_simple_regret_TT=[]

    for idxtt,tt in enumerate(range(0,nRepeat)): # TT run

        if IsPradaBO==1:
            temp_simple_regret=YY[idxtt,0:BatchSzArray[0]+1].max()
        else:
            temp_simple_regret=YY[idxtt,0:BatchSzArray[0]+1].min()

        start_point=0
        for idx,bz in enumerate(BatchSzArray): # batch
            if idx==0:
                continue
            if idx==len(BatchSzArray)-1:
                break
            bz=np.int(bz)

            # find maximum in each batch
            if IsPradaBO==1:
                temp_simple_regret=np.vstack((temp_simple_regret,YY[idxtt,start_point:start_point+bz].max()))
            else:
                temp_simple_regret=np.vstack((temp_simple_regret,YY[idxtt,start_point:start_point+bz].min()))

            start_point=start_point+bz

        if IsPradaBO==1:
            # ignore the first element of initialization
            myYbest=[temp_simple_regret[:idx+1].max()*-1 for idx,val in enumerate(temp_simple_regret)]
            temp_simple_regret=temp_simple_regret*-1
        else:
            myYbest=[temp_simple_regret[:idx+1].min() for idx,val in enumerate(temp_simple_regret)]

        # cumulative regret for each independent run
        #myYbest_cum=[np.mean(np.abs(temp_mean_cum[:idx+1]-Y_optimal)) for idx,val in enumerate(temp_mean_cum)]

        temp_regret=np.abs(np.asarray(myYbest)-Y_optimal)
        temp_regret=temp_regret[half_list_index:]
        myYbest_cum=[np.mean(temp_regret[:idx+1]) for idx,val in enumerate(temp_regret)]

        if len(mean_cum_simple_regret_TT)==0:
            #mean_TT=myYbest
            mean_cum_simple_regret_TT=myYbest_cum
        else:
            #mean_TT.append(temp_mean)
            #mean_TT=np.vstack((mean_TT,myYbest))
            mean_cum_simple_regret_TT=np.vstack((mean_cum_simple_regret_TT,myYbest_cum))

    mean_cum_simple_regret_TT=np.array(mean_cum_simple_regret_TT)
    std_cum_TT=np.std(mean_cum_simple_regret_TT,axis=0)
    std_cum_TT=np.array(std_cum_TT).ravel()
    mean_cum_simple_regret_TT=np.mean(mean_cum_simple_regret_TT,axis=0)

    #return mean_TT[::step],std_TT[::step]#,mean_cum_TT[::5],std_cum_TT[::5]
    #return mean_TT,std_TT,np.mean(mean_cum_simple_regret_TT),np.mea(std_cum_TT)

    #half_list_index=np.int(len(mean_cum_simple_regret_TT)*0.5)
    #return np.mean(mean_cum_simple_regret_TT[half_list_index:]),np.mean(std_cum_TT[half_list_index:])
    return np.mean(mean_cum_simple_regret_TT),np.mean(std_cum_TT)


text:
import myutil as mu
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

################################################################################
# 선형 회귀(Linear Regression)

# 데이터
x_train = torch.FloatTensor([[1], [2], [3]])
y_train = torch.FloatTensor([[2], [4], [6]])
# 모델 초기화
W = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# optimizer 설정
optimizer = optim.SGD([W, b], lr=0.01)

nb_epochs = 2000  # 원하는만큼 경사 하강법을 반복

mu.plt_init()

for epoch in range(nb_epochs + 1):

    # H(x) 계산
    hypothesis = x_train * W + b

    # cost 계산
    cost = torch.mean((hypothesis - y_train) ** 2)

    # accuracy 계산
    accuracy = mu.get_regression_accuracy(hypothesis, y_train)

    # cost로 H(x) 개선
    optimizer.zero_grad()
    cost.backward()
    optimizer.step()

    # 100번마다 로그 출력
    if epoch % 100 == 0:
        mu.log_epoch(epoch, nb_epochs, cost, accuracy)

mu.plt_show()
mu.log("W", W)
mu.log("b", b)

################################################################################
# optimizer.zero_grad()가 필요한 이유

import torch

w = torch.tensor(2.0, requires_grad=True)

nb_epochs = 20
for epoch in range(nb_epochs + 1):
    z = 2 * w

    z.backward()
    print('수식을 w로 미분한 값 : {}'.format(w.grad))

################################################################################
# torch.manual_seed()를 하는 이유

import torch

torch.manual_seed(3)
print('랜덤 시드가 3일 때')
for i in range(1, 3):
    print(torch.rand(1))

torch.manual_seed(5)
print('랜덤 시드가 5일 때')
for i in range(1, 3):
    print(torch.rand(1))

torch.manual_seed(3)
print('랜덤 시드가 다시 3일 때')
for i in range(1, 3):
    print(torch.rand(1))

max_stars_repo_name: HyundongHwang/PyTorchDeepLearningStart

max_issues_repo_name: HyundongHwang/PyTorchDeepLearningStart

max_forks_repo_name: HyundongHwang/PyTorchDeepLearningStart


text:
from typing import Union, Optional

import numpy as np
import torch
from torch import nn
from torch.distributions.normal import Normal

from utils.util_data import cartesian_2d_to_complex, integers_to_symbols

class Modulator():
    def __init__(self, *,
                 model,
                 bits_per_symbol: Union[float, int],
                 optimizer: Optional[str] = 'adam',
                 stepsize_mu: float = 0.0,
                 stepsize_sigma: float = 0.0,
                 initial_std: float = 0.4,
                 # initial_std: float = 0.1,
                 min_std: float = 1e-5,
                 max_std: float = 1e2,
                 lambda_baseline: float = 0.0,  # used in update method
                 lambda_center: float = 0.0,  # used in update method
                 lambda_l1: float = 0.0,  # used in update method
                 lambda_l2: float = 0.0,  # used in update method
                 **kwargs):

        self.model = model(bits_per_symbol=bits_per_symbol, **kwargs)
        self.name = self.model.name
        self.bits_per_symbol = bits_per_symbol
        self.log_std_min = np.log(min_std) * torch.ones(2)
        self.log_std_max = np.log(max_std) * torch.ones(2)
        self.lambda_l1 = torch.tensor(lambda_l1).float()
        self.lambda_l2 = torch.tensor(lambda_l2).float()
        self.lambda_center = torch.tensor(lambda_center).float()
        self.lambda_baseline = torch.tensor(lambda_baseline).float()
        self.all_symbols = integers_to_symbols(np.arange(
            0, 2 ** bits_per_symbol), bits_per_symbol)
        self.log_std = nn.Parameter(np.log(initial_std) * torch.ones(2))
        # self.std.register_hook(lambda grad: print(grad))

        optimizers = {
            'adam': torch.optim.Adam,
            'sgd': torch.optim.SGD,
        }

        if optimizer and hasattr(self.model, "mu_parameters") and not hasattr(self.model, "update"):
            assert optimizer.lower() in optimizers.keys(), "modulator optimizer=%s not supported" % optimizer
            optimizer = optimizers[optimizer.lower()]
            if kwargs['verbose']:
                print("Modulator %s initialized with %s optimizer." % (self.model.name, optimizer.__name__))
            self.param_dicts = [ \
                {'params': self.model.mu_parameters(), 'lr': stepsize_mu},
                {'params': self.log_std, 'lr': stepsize_sigma}]
            self.optimizer = optimizer(self.param_dicts)
        else:
            if kwargs['verbose']:
                print("Modulator %s initialized WITHOUT an optimizer" % (self.model.name))
            self.optimizer = None
            if hasattr(self.model, "mu_parameters"):
                self.param_dicts = [ \
                    {'params': self.model.mu_parameters(), 'lr': stepsize_mu},
                    {'params': self.log_std, 'lr': stepsize_sigma}]
            else:
                self.param_dicts = []

    # input bit symbols
    # returns np cartesian or np complex
    def modulate(self, symbols: np.ndarray, dtype: str = 'complex', mode: str = 'exploit') -> np.ndarray:
        symbols = torch.from_numpy(symbols).float()
        if mode == 'explore' and not self.model.name.lower() == 'classic':
            means = self.model(symbols)
            # log_std = torch.min(torch.max(self.log_std_min, self.log_std), self.log_std_max)
            self.policy = Normal(means, self.log_std.exp())
            cartesian_points = self.policy.sample()
        elif self.model.name.lower() == 'classic' or mode == 'exploit':
            cartesian_points = self.model(symbols)
        else:
            raise Exception("modulator.modulate mode=%s not recognized; accepted inputs: 'explore', 'exploit'" % mode)
        if dtype == 'cartesian':
            return cartesian_points.detach().numpy()
        elif dtype == 'complex':
            return cartesian_2d_to_complex(cartesian_points.detach().numpy().astype(np.complex64))
        else:
            raise Exception(
                "modulator.modulate dtype=%s not recognized; accepted inputs: 'cartesian', 'complex'" % dtype)

    # input torch.Tensor of bit symbols
    # returns torch.Tensor of cartesian points
    def modulate_tensor(self, symbols: torch.Tensor, mode: str = 'exploit') -> torch.Tensor:
        if mode == 'explore' and not self.model.name.lower() == 'classic':
            means = self.model(symbols)
            # log_std = torch.min(torch.max(self.log_std_min, self.log_std), self.log_std_max)
            self.policy = Normal(means, self.log_std.exp())
            cartesian_points = self.policy.sample()
        elif self.model.name.lower() == 'classic' or mode == 'exploit':
            cartesian_points = self.model(symbols)
        else:
            raise Exception(
                "modulator.modulate_tensor mode=%s not recognized accepted inputs: 'explore', 'exploit'" % mode)
        return cartesian_points

    # input bit symbols, cartesian/complex actions, bit symbols, regenerate Gaussian means
    # Vanilla PG, fine for simulation
    def update(self, symbols: np.ndarray, actions: np.ndarray, received_symbols: np.ndarray,
               rebuild_policy: bool = False, **kwargs):
        if hasattr(self.model, "update"):
            kwargs['symbols'] = symbols
            kwargs['actions'] = actions
            kwargs['received_symbols'] = received_symbols
            self.model.update(**kwargs)
            return
        else:
            assert self.optimizer, "Modulator is not initialized with an optimizer"
            if len(actions.shape) == 2:
                cartesian_actions = torch.from_numpy(actions).float()
            elif len(actions.shape) == 1:
                cartesian_actions = torch.from_numpy(
                    np.stack((actions.real.astype(np.float32), actions.imag.astype(np.float32)), axis=-1))
            reward = torch.from_numpy(
                -np.sum(symbols ^ received_symbols, axis=1)).float()  # correct bits = 0, incorrect bits = -1 #TESTED
            # reward =torch.from_numpy(np.sum(1 - 2 * (symbols ^ received_symbols), axis=1)).float() #correct bits = 1, incorrect bits = -1 #NOT TESTED
            if rebuild_policy:
                # Rebuild the policy here for the case where we overlap echo rounds
                tensor_symbols = torch.from_numpy(symbols).float()
                self.policy = Normal(self.model(tensor_symbols), self.log_std.exp())
            log_probs = self.policy.log_prob(cartesian_actions).sum(dim=1)
            baseline = torch.mean(reward)
            loss = -torch.mean(log_probs * (reward - self.lambda_baseline * baseline))
            if self.lambda_center > 0:
                loss += self.lambda_center * self.model.location_loss()
            if self.lambda_l1 > 0:
                loss += self.lambda_l1 * self.model.l1_loss()
            if self.lambda_l2 > 0:
                loss += self.lambda_l2 * self.model.l2_loss()

            self.optimizer.zero_grad()
            loss.backward(retain_graph=True)
            self.optimizer.step()
            # self.log_std = nn.functional.relu(self.log_std-self.log_std_min)+self.log_std_min
            # self.log_std = self.log_std_max-nn.functional.relu(-(self.log_std-self.log_std_max))
            return -np.average(reward)

    # input bit symbols, cartesian/complex actions, bit symbols
    # PPO instead of Vanilla Policy Gradient, Why? for efficiency when we are over the air... we can train more on the
    # single preamble we get. So sending a large preamble and training mini-batches over it.
    # lots of overhead to sending over air!
    def updatePPO(self, symbols: np.ndarray,
                  actions: np.ndarray,
                  received_symbols: np.ndarray,
                  clip_ratio=0.2,
                  epochs=5,
                  rebuild_policy=False, **kwargs):
        if hasattr(self.model, "update"):
            kwargs['symbols'] = symbols
            kwargs['actions'] = actions
            kwargs['received_symbols'] = received_symbols
            self.model.update(**kwargs)
            return
        else:
            assert self.optimizer, "Modulator is not initialized with an optimizer"
            if len(actions.shape) == 2:
                cartesian_actions = torch.from_numpy(actions).float()
            elif len(actions.shape) == 1:
                cartesian_actions = torch.from_numpy(
                    np.stack((actions.real.astype(np.float32), actions.imag.astype(np.float32)), axis=-1))
        if rebuild_policy:
            # Rebuild the policy here for the case where we overlap echo rounds
            tensor_symbols = torch.from_numpy(symbols).float()
            self.policy = Normal(self.model(tensor_symbols), self.log_std.exp())
        prev_log_prob = self.policy.log_prob(cartesian_actions).sum(dim=1).detach()
        reward = torch.from_numpy(-np.sum(symbols ^ received_symbols,
                                          axis=1)).float()  # correct bits = 0, incorrect bits = -1 #TESTED
        baseline = torch.mean(reward)
        adv = reward - self.lambda_baseline * baseline
        for i in range(epochs):
            log_prob = self.policy.log_prob(cartesian_actions).sum(dim=1)
            ratio = (log_prob - prev_log_prob).exp()
            min_adv = torch.where(adv > 0, (1 + clip_ratio) * adv,
                                  (1 - clip_ratio) * adv)
            loss = -(torch.min(ratio * adv, min_adv)).mean()
            if self.lambda_center > 0:
                loss += self.lambda_center * self.model.location_loss()
            if self.lambda_l1 > 0:
                loss += self.lambda_l1 * self.model.l1_loss()
            if self.lambda_l2 > 0:
                loss += self.lambda_l2 * self.model.l2_loss()

            self.optimizer.zero_grad()
            loss.backward(retain_graph=True)
            self.optimizer.step()

        # self.log_std = nn.functional.relu(self.log_std-self.log_std_min)+self.log_std_min
        # self.log_std = self.log_std_max-nn.functional.relu(-(self.log_std-self.log_std_max))
        return -np.average(reward)

    def get_std(self):
        if hasattr(self.model, 'mu_parameters'):
            return self.log_std.exp().data.detach().numpy()
        else:
            return [0.0, 0.0]

    def get_constellation(self):
        all_unique_symbols = self.all_symbols
        cartesian_constellation = self.modulate(all_unique_symbols, dtype='cartesian')
        return cartesian_constellation

    def get_param_dicts(self):
        return self.param_dicts

    def get_regularization_loss(self):
        r_loss = torch.tensor(0.0).float()
        if not hasattr(self.model, "update"):
            if self.lambda_center > 0:
                r_loss += self.lambda_center * self.model.location_loss()
            if self.lambda_l1 > 0:
                r_loss += self.lambda_l1 * self.model.l1_loss()
            if self.lambda_l2 > 0:
                r_loss += self.lambda_l2 * self.model.l2_loss()
        return r_loss


text:
import math
from typing import List

import numpy as np
import torch
from sklearn.metrics import mean_squared_error
from torch import nn
from tqdm import trange

from solvation_predictor.data.scaler import Scaler
from solvation_predictor.data.data import DatapointList

def evaluate(model: nn.Module,
             data: DatapointList,
             metric_func: str,
             scaler: Scaler = None
             ):
    """
    Evaluates an ensemble of models on a dataset.

    :param model: A model.
    :param data: A MoleculeDataset.
    :param num_tasks: Number of tasks.
    :param metric_func: Metric function which takes in a list of targets and a list of predictions.
    :param batch_size: Batch size.
    :param dataset_type: Dataset type.
    :param scaler: A StandardScaler object fit on the training targets.
    :param logger: Logger.
    :return: A list with the score for each task based on `metric_func`.
    """
    preds = predict(
        model=model,
        data=data,
        scaler=scaler
    )

    targets = [d.targets for d in data.get_data()]

    results = evaluate_predictions(
        preds=preds,
        targets=targets,
        metric_func=metric_func
    )

    return results

def evaluate_predictions(preds: List[List[float]],
                         targets: List[List[float]],
                         metric_func: str
                         ) -> List[float]:
    """
    Evaluates predictions using a metric function and filtering out invalid targets.

    :param preds: A list of lists of shape (data_size, num_tasks) with model predictions.
    :param targets: A list of lists of shape (data_size, num_tasks) with targets.
    :param num_tasks: Number of tasks.
    :param metric_func: Metric function which takes in a list of targets and a list of predictions.
    :param dataset_type: Dataset type.
    :param logger: Logger.
    :return: A list with the score for each task based on `metric_func`.
    """
    num_tasks = len(targets[0])
    if len(preds) == 0:
        return [float('nan')] * num_tasks

    # Filter out empty targets
    # valid_preds and valid_targets have shape (num_tasks, data_size)
    valid_preds = [[] for _ in range(num_tasks)]
    valid_targets = [[] for _ in range(num_tasks)]
    for i in range(num_tasks):
        for j in range(len(preds)):
            if not np.isnan(targets[j][i]):  # Skip those without targets
                valid_preds[i].append(preds[j][i])
                valid_targets[i].append(targets[j][i])

    # Compute metric
    results = []
    for i in range(num_tasks):
        if metric_func == "rmse":
            results.append(rmse(valid_targets[i], valid_preds[i]))
        elif metric_func == "mse":
            results.append(mse(valid_targets[i], valid_preds[i]))

    return results

def predict(model: nn.Module,
            data: DatapointList,
            scaler: Scaler = None):
    """
    Makes predictions on a dataset using an ensemble of models.

    :param model: A model.
    :param data: A MoleculeDataset.
    :param batch_size: Batch size.
    :param scaler: A StandardScaler object fit on the training targets.
    :return: A list of lists of predictions. The outer list is examples
    while the inner list is tasks.
    """
    model.eval()
    preds = []

    batch_size = 1
    num_iters = len(data.get_data()) // batch_size * batch_size
    iter_size = batch_size

    for i in trange(0, num_iters, iter_size):
        if (i + iter_size) > len(data.get_data()):
            break
        batch = DatapointList(data.get_data()[i:i+batch_size])
        with torch.no_grad():
            pred = model(batch)
        pred = pred.data.cpu().numpy().tolist()
        preds.extend(pred)

    if num_iters == 0:
        pred = []

    for i in range(0, len(preds)):
        data.get_data()[i].scaled_predictions = preds[i]

    if scaler is not None:
        preds = scaler.inverse_transform(preds)
    for i in range(0, len(preds)):
        data.get_data()[i].predictions = preds[i]

    preds = preds.tolist()

    return preds

def rmse(targets: List[float], preds: List[float]) -> float:
    """
    Computes the root mean squared error.

    :param targets: A list of targets.
    :param preds: A list of predictions.
    :return: The computed rmse.
    """
    return math.sqrt(mean_squared_error(targets, preds))

def mse(targets: List[float], preds: List[float]) -> float:
    """
    Computes the mean squared error.

    :param targets: A list of targets.
    :param preds: A list of predictions.
    :return: The computed mse.
    """
    return mean_squared_error(targets, preds)


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from glob import glob

import tensorflow as tf
import numpy as np

slim = tf.contrib.slim

def get_queue(mode_str, cfg):
    """
    Defining the loading ops for data management
    """
    batch_size = cfg.batch_size
    num_worker = cfg.num_worker
    min_after_dequeue = 5000
    capacity = min_after_dequeue + 3 * batch_size

    if cfg.dataset == "helium" or cfg.dataset == "cxidb":
        img, lbl = get_dataset(mode_str, cfg)
        if not cfg.use_log_act:
            img = tf.image.convert_image_dtype(img, tf.float32)

        if mode_str == "predict":
            [img_data, lbl_data] = tf.train.batch([img, lbl],
                                                  batch_size=batch_size,
                                                  num_threads=1,
                                                  capacity=capacity,
                                                  name="input_data")
        else:
            [img_data, lbl_data
             ] = tf.train.shuffle_batch([img, lbl],
                                        batch_size=batch_size,
                                        num_threads=num_worker,
                                        capacity=capacity,
                                        min_after_dequeue=min_after_dequeue,
                                        name="input_data")

        if cfg.ori_depth == 1 and cfg.target_depth == 3:
            img_data = tf.image.grayscale_to_rgb(img_data)

        # Crop the image so that is has 2^x dimensions
        crop_height = np.power(2, np.floor(np.log2(cfg.ori_height)))
        crop_width = np.power(2, np.floor(np.log2(cfg.ori_width)))
        img_data = tf.image.resize_image_with_crop_or_pad(
            img_data, crop_height.astype(int), crop_width.astype(int))
        new_interp_size = tf.convert_to_tensor(
            [int(cfg.target_height),
             int(cfg.target_width)], dtype=tf.int32)
        img_data = tf.image.resize_images(img_data, new_interp_size)

        if mode_str == "train":
            img_data = augment_data(img_data)
        elif mode_str == "predict" and cfg.dataset == "helium":
            # if predicting and using the helium dataset
            # the "labels" field is is filled with the unique
            # bunchID of the image.
            labels = tf.reshape(tf.to_int32(lbl_data), [batch_size, 1])
            return {"imgs": img_data, "lbl": labels}
        elif mode_str == "predict" and cfg.dataset == "cxidb":
            # if predicting and using the cxidb dataset
            # the "labels" field is is filled with the unique
            # bunchID of the image.
            labels = tf.reshape(
                tf.argmax(lbl_data, axis=-1, output_type=tf.int32),
                [batch_size, 1])
            return {"imgs": img_data, "lbl": labels}

        labels = tf.reshape(tf.to_int32(lbl_data),
                            [batch_size, cfg.num_classes])
        return img_data, labels
    else:
        print("Currently --datasets has to be: \"helium\", \"cxidb\"")
        return None, None

def get_dataset(mode_str, cfg):
    """
    Gets a dataset tuple with instructions for reading airynet.
    Args:
        cfg: the config class
    Returns:
        Single img and lbl
    """
    if mode_str == "predict":
        num_epochs = 1
    else:
        num_epochs = None

    if not cfg.fraction_mode or mode_str == "eval":
        filenames = glob(cfg.data_dir + cfg.dataset + "/*" + mode_str +
                         ".tfrecord")
    else:
        filenames = glob(cfg.data_dir + cfg.dataset + "/*" + mode_str +
                         "*{}".format(cfg.fraction).replace(".", "_") +
                         ".tfrecord")

    if cfg.dataset == "cxidb" and mode_str == "predict":
        filenames = glob(cfg.data_dir + cfg.dataset + "/*train.tfrecord")

    filename_queue = tf.train.string_input_producer(filenames,
                                                    num_epochs=num_epochs)
    print(filenames)
    # Allowing None in the signature so that
    # dataset_factory can use the default.
    reader = tf.TFRecordReader()

    _, serialized_example = reader.read(filename_queue)
    img_string = "image/"
    if cfg.use_ccf:
        img_string += "ccf"
    else:
        img_string += "encoded"

    if cfg.add_noise == 0 and cfg.use_ccf:
        img_string += "_noise_"
    elif cfg.add_noise == 1:
        img_string += "_noise_mean"
    elif cfg.add_noise == 2:
        img_string += "_noise_mean_std"
    elif cfg.add_noise == 3:
        img_string += "_noise_max"
    print(img_string)
    features = tf.parse_single_example(
        serialized_example,
        # Defaults are not specified since both keys are required.
        features={
            img_string: tf.FixedLenFeature([], tf.string),
            "image/format": tf.FixedLenFeature([], tf.string),
            "image/class/label": tf.FixedLenFeature([], tf.string),
            "image/height": tf.FixedLenFeature([], tf.int64),
            "image/width": tf.FixedLenFeature([], tf.int64)
        })
    # Convert from a scalar string tensor (whose single string has
    # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape
    # [mnist.IMAGE_PIXELS].
    image_data = tf.decode_raw(features[img_string], tf.uint16)
    if mode_str == "train" or mode_str == "eval":
        lbl_data = tf.decode_raw(features["image/class/label"], tf.int16)
        lbl_data.set_shape([cfg.num_classes])
    elif mode_str == "predict":
        if cfg.dataset == "helium":
            lbl_data = tf.decode_raw(features["image/class/label"], tf.int32)
            lbl_data.set_shape([1])
        elif cfg.dataset == "cxidb":
            lbl_data = tf.decode_raw(features["image/class/label"], tf.int16)
            lbl_data.set_shape([cfg.num_classes])

    height = tf.cast(features["image/height"], tf.int32)
    width = tf.cast(features["image/width"], tf.int32)

    image_data_shape = tf.stack([height, width, 1])

    image_data = tf.reshape(image_data, image_data_shape)
    image_data.set_shape([cfg.ori_width, cfg.ori_height, cfg.ori_depth])

    return image_data, lbl_data

def augment_data(img_data):
    # img_data = tf.image.random_contrast(img_data, .25, 1.5)
    # img_data = tf.image.random_brightness(img_data, 1.5)
    img_data = tf.map_fn(lambda img: tf.image.random_flip_left_right(img),
                         img_data)
    img_data = tf.map_fn(lambda img: tf.image.random_flip_up_down(img),
                         img_data)
    return img_data

max_stars_repo_name: julian-carpenter/airynet

max_issues_repo_name: julian-carpenter/airynet

max_forks_repo_name: julian-carpenter/airynet


text:
# Front-End
from re import template
from numpy.core.numeric import _full_like_dispatcher
import streamlit as st
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
from prophet import Prophet
import plotly.graph_objects as go
import plotly.io as pio
from pathlib import Path
import base64
from iso3166 import countries as codes
import hydralit_components as hc
from streamlit import caching

# defaults
plt.style.use('seaborn-whitegrid')

# GLOBALS
mapping = {"Côte D'Ivoire": "Côte d'Ivoire",
 'Syria': 'Syrian Arab Republic',
 'United States': 'United States of America',
 'Tanzania': 'Tanzania, United Republic of',
 'Vietnam': 'Viet Nam',
 'Congo (Democratic Republic Of The)': 'Congo',
 'United Kingdom': 'United Kingdom of Great Britain and Northern Ireland',
 'Iran': 'Iran, Islamic Republic of',
 'Russia': 'Russian Federation',
 'Burma': 'Myanmar',
 'South Korea': 'Korea, Republic of',
 'Taiwan': 'Taiwan, Province of China'}

# FONTS
font_url0 = ''' <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Kaisei+HarunoUmi:wght@700&display=swap" rel="stylesheet">
'''
font_url1 = '''<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,700;1,800&display=swap" rel="stylesheet">
'''

fontname0 = "Kaisei HarunoUmi"
fontname1 = "Playfair Display"

def img_to_bytes(img_path):
    img_bytes = Path(img_path).read_bytes()
    encoded = base64.b64encode(img_bytes).decode()
    return encoded

@st.cache(persist=True, allow_output_mutation=True)
def load_data(about=True):

    if not about:
        dmajcity = pd.read_csv("./data/AvgTempMajorCity.csv").drop('Unnamed: 0',axis=1)

        return dmajcity
    else:
        coords = pd.read_csv('./data/choropleth-dat.csv').drop('Unnamed: 0',axis=1)
        return coords

@st.cache(persist=True, allow_output_mutation=True)
def load_provinces():

    # dcity = pd.read_csv("./data/unis-country.csv").drop('Unnamed: 0',axis=1)
    india = pd.read_csv("./data/india.csv").drop('Unnamed: 0',axis=1)
    us = pd.read_csv("./data/USA.csv").drop('Unnamed: 0',axis=1)
    uk = pd.read_csv("./data/UK.csv").drop('Unnamed: 0',axis=1)

    dcity = pd.concat([india,us,uk])
    unis = pd.read_csv("./data/unis.csv").drop('Unnamed: 0',axis=1)

    return dcity, unis
    # return unis

@st.cache(persist=True)
def median_agg(data, sequence='Month',loc='Country',countryname=None,returnlists=False):

    # '''
    # Median Aggregation of AverageTemperatures by Country
    # '''
    if loc == 'Country':
        df = pd.DataFrame(data.groupby([loc, sequence])[['AverageTemperature','AverageTemperatureUncertainty']].median())
        if countryname !=None:
            try:
                return df.loc[countryname, :]
            except:
                print('Invalid Country Name')
        else:
            return pd.DataFrame(data.groupby([loc, sequence])[['AverageTemperature','AverageTemperatureUncertainty']].median())
    elif loc == 'City':
        if countryname != None:
            df = pd.DataFrame(data.groupby(['Country',loc, sequence])[['AverageTemperature','AverageTemperatureUncertainty']].median())
            try:
                return df.loc[countryname, :]
            except:
                print('Invalid Country Name')

        else:
            return pd.DataFrame(data.groupby([loc, sequence])[['AverageTemperature','AverageTemperatureUncertainty']].median())

    else:
        return None

def order_monthidx(df):
    cats = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sept', 'Oct', 'Nov', 'Dec']
    df.index = pd.CategoricalIndex(df.index, categories=cats, ordered=True)
    df = df.sort_index()
    return df

# Seasonal Pattern
def fetch_seasonal_pattern(ex):

    cats = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sept', 'Oct', 'Nov', 'Dec']
    ex = ex.reset_index()
    ex['Month'] =  pd.CategoricalIndex(ex['Month'], categories=cats, ordered=True)
    seasons = list(ex.sort_values(by='Month')['Seasons'].unique())
    ex = ex.sort_values(by='Month')
    return seasons, ex

@st.cache(persist=True)
def city_synopsis(data, city):

    # Dictionary of everything - CITY : COMPARATIVE SUMMARY
    package = {}

    df = data[data['City']==city]

    # Lowest Ever
    least_series = df.iloc[df['AverageTemperature'].argmin(),:]

    min_avgT = least_series['AverageTemperature']
    min_season = least_series['Seasons']
    min_month = least_series['Month']
    min_year = least_series['Year']
    min_deltaT = least_series['AverageTemperatureUncertainty']

    # Highest Ever
    high_series = df.iloc[df['AverageTemperature'].argmax(),:]
    max_avgT = high_series['AverageTemperature']
    max_season = high_series['Seasons']
    max_month = high_series['Month']
    max_year = high_series['Year']
    max_deltaT = high_series['AverageTemperatureUncertainty']

    maxdata = {'avgT':max_avgT, 'season':max_season, 'deltaT':max_deltaT,
                  'year':max_year, 'month':max_month}
    mindata = {'avgT':min_avgT, 'season':min_season, 'deltaT':min_deltaT,
                  'year':min_year, 'month':min_month}

    package['Least'] = mindata
    package['Highest'] = maxdata

    summarydf = df.groupby(['Month','Seasons'])[['AverageTemperature','AverageTemperatureUncertainty']].mean()

    # hemisphere
    hemisphere = df['Hemisphere'].values[0]
    package['hemisphere'] = hemisphere

    #seasons
    seasons, summary_df = fetch_seasonal_pattern(summarydf)

    seasonal_avg = {}
    for season in seasons:
        temp = summary_df[summary_df['Seasons']==season][['AverageTemperature','AverageTemperatureUncertainty']].mean()
        seasonal_avg[season] = list(temp.values)

    package['seasons'] = seasonal_avg

    return package

def summary_markdown(city, data, diff, dcity, country):

    st.markdown(f'
{city}
', unsafe_allow_html=True)

    st.markdown('')
    st.markdown('')

    seasons = list(data['seasons'].keys())
    st.subheader("**Seasonal pattern**")
    st.markdown("***{}***".format("→".join(seasons)),unsafe_allow_html=True)

    st.subheader('**Seasonal Average Temperatures**')
    for season, templist in zip(data['seasons'].keys(),data['seasons'].values()):

        avgT, deltaT = templist
        st.metric(label=season, value=str("%.2f" % avgT)+' °C', delta='ΔT '+str("%.2f" % deltaT)+' °C',delta_color='inverse')

    if diff:
        for x in range(6):
            st.markdown('')

    # seperator
    st.markdown('***')

    st.subheader('**Extreme temperatures ever recorded**')
    st.markdown('')
    st.markdown('')

    least = data['Least']
    highest = data['Highest']

    # least
    st.markdown(f" → In the Year - {least['year']}, {least['month']} ({least['season']})",unsafe_allow_html=True)
    st.metric(label='Lowest Ever', value=str("%.2f" % least['avgT'])+' °C', delta='ΔT '+str("%.2f" % least['deltaT'])+' °C',delta_color='inverse')

    # highest
    st.markdown(f" → In the Year - {highest['year']}, {highest['month']} ({highest['season']})",unsafe_allow_html=True)
    st.metric(label='Highest Ever', value=str("%.2f" % highest['avgT'])+' °C', delta='ΔT '+str("%.2f" % highest['deltaT'])+' °C',delta_color='inverse')

    # seperator
    st.markdown('***')

    # Box plot
    st.subheader('**Year Average Temperature Distribution**')
    monthly = median_agg(dcity,loc='City',countryname=country).loc[city,:]

    x_vals = monthly['AverageTemperature']

    fig = go.Figure()
    fig.add_trace(go.Box(y=x_vals, name=city, marker_color = 'dodgerblue'))

    fig.update_layout(
        yaxis_title="Temperature",
        font=dict(
            family="sora",
            size=12,
        )
    )

    st.plotly_chart(fig,use_container_width=True)

def plottimeseries(data,cityname, attr='AverageTemperature',loc='City',multiple=None):

    ## Prompt
    if attr == 'AverageTemperature':
        prompt = 'Average Temperature'
    else:
        prompt = 'Average Temperature Uncertainty (Δ T)'

    if loc=='City':

        if 'Month' in data.reset_index().columns:
            plt.figure(figsize=(15,8))

            if multiple == None:
                data = data.xs(cityname, level=0, drop_level=True)
                data = order_monthidx(data)
                x = data.index
                y = data[attr].values
                if attr == 'AverageTemperature':
                    plots = sns.barplot(x=x,y=y,palette='Blues_r',label=cityname,edgecolor='black')

                    for bar in plots.patches:

                        plots.annotate(format(bar.get_height(), '.2f'),
                                    (bar.get_x() + bar.get_width() / 2,
                                        bar.get_height()), ha='center', va='center',
                                    size=18, xytext=(0, 8),
                                    textcoords='offset points')

                    plt.title('Monthly {} Recorded in {}'.format(prompt,cityname),size=25)
                    plt.ylabel('Temperatures °C',size=23)
                    plt.xlabel('Months',size=23)
                    plt.xticks(size=15)
                    plt.yticks(size=15)
                else:
                    sns.lineplot(x=x,y=y,color='royalblue',marker='o')
                    plt.title('Monthly {} Recorded in {}'.format(prompt,cityname),size=25)
                    plt.ylabel('Temperatures °C',size=23)
                    plt.xlabel('Months',size=23)
                    plt.xticks(size=15)
                    plt.yticks(size=15)
                # plt.legend(bbox_to_anchor=(1.13, 1.05))

            else:
                coords = []
                if len(multiple) <=3 and len(multiple) <=5:
                    for cityname in multiple:
                        df = data.xs(cityname, level=0, drop_level=True)
                        df = order_monthidx(df)
                        x = df.index
                        y = df[attr].values
                        coords.append((x,y))

                # setting plot attributes
                plotattrs = {}
                colors = ['crimson','magenta','darkorange','darkslateblue']
                for cityname in multiple[1:]:
                    plotattrs[cityname] = {'marker':'o', 'color':colors.pop(0)}

                count = 0
                for coord,cityname in zip(coords,multiple):
                    x,y = coord

                    if count < 1:
                        sns.barplot(x=x,y=y,palette='Blues_r',label=cityname, edgecolor='black')
                        count +=1
                    else:
                        pltattrs = plotattrs[cityname]
                        sns.lineplot(x=x,y=y,color=pltattrs['color'],marker=pltattrs['marker'],label=cityname)
                    plt.title('Monthly {} Recorded in {}'.format(prompt,cityname),size=25)
                    plt.ylabel('Temperatures °C',size=23)
                    plt.xlabel('Months',size=23)
                    plt.xticks(size=15)
                    plt.yticks(size=15)
                    plt.legend(bbox_to_anchor=(1.13, 1.05))

        else:
            data = data.xs(cityname, level=0, drop_level=True)
            plt.figure(figsize=(15,8))
            x = data.index
            y = data[attr].values
            sns.lineplot(x=x,y=y,label=cityname,marker='P',color='royalblue',palette='inferno')
            plt.title('Yearly {} Recorded in {}'.format(prompt,cityname),size=25)
            plt.ylabel('Temperatures °C',size=23)
            plt.xlabel('Years',size=23)
            plt.xticks(size=15)
            plt.yticks(size=15)
            # plt.legend(bbox_to_anchor=(1.13, 1.05))

    else:
        pass

def plotlychart(df,attr='AverageTemperature',cityname=None,sequence='Monthly'):

    # pio.templates.default = 'plotly_white'
        ## Prompt
    if attr == 'AverageTemperature':
        prompt = 'Average Temperature'
    else:
        prompt = 'Average Temperature Uncertainty (Δ T)'

    # Template
    pio.templates.default = 'plotly_white'

    if sequence == 'Monthly':

        if not attr == 'AverageTemperatureUncertainty':

            x = list(df.index)
            y0 = np.round(df[attr].values,3)

            fig = go.Figure()
            fig.add_trace(go.Bar(x=x, y=y0, text=y0, textposition='outside',
                                marker={'color': y0, 'colorscale': 'Blues'},name='Average'))
            # fig.add_trace(go.Bar(x=x, y=y1,
            #                 marker_color='crimson',name='deltaT'))

            fig.update_layout(font=dict(size=11,family='sora'),template='ggplot2+seaborn' ,title='{} {} Recorded in {}'.format(sequence,prompt,cityname),
                            xaxis_title='Months',
                            yaxis_title='Temperature C',
                            title_x=0.5
                            )

        else:

            x = list(df.index)
            y0 = np.round(df[attr].values,3)

            fig = go.Figure()
            fig.add_trace(go.Scatter(x=x, y=y0,name='Average',mode='lines+markers',
                                    line=dict(color='dodgerblue',width=2)))
            # fig.add_trace(go.Bar(x=x, y=y1,
            #                 marker_color='crimson',name='deltaT'))

            fig.update_layout(font=dict(size=11,family='sora'), template='ggplot2+plotly_dark', title='{} {} Recorded in {}'.format(sequence,prompt,cityname),
                            xaxis_title='Months',
                            yaxis_title='Temperature C',
                            title_x=0.5
                            )

    elif sequence == 'Yearly':
        x = list(df.index)
        y0 = df[attr]

        fig = go.Figure()
        fig.add_trace(go.Scatter(x=x, y=y0,name='Average',mode='lines',
                                line=dict(color='royalblue',width=2)))
        # fig.add_trace(go.Bar(x=x, y=y1,
        #                 marker_color='crimson',name='deltaT'))

        fig.update_layout(font=dict(size=11,family='sora'), template='ggplot2+plotly_dark', title='{} {} Recorded in {}'.format(sequence,prompt,cityname),
                          xaxis_title='Months',
                          yaxis_title='Temperature C',
                          title_x=0.5
                         )

    return fig

# For Dash Board Section -- HTML unordered list generator
def sections_cs(section, subsection=False):

    if not subsection:
        id = '-'.join(section.lower().split())
        space= ''
        html_string = f'* {section.capitalize()}
{space}'
        return html_string, id

    else:
        pattern = []

        id = '-'.join(section.lower().split())
        # html_string = f'* {section.capitalize()}
{space}',
        if isinstance(subsection, list):
            for s in subsection:
                pattern.append('''

{}

'''.format(s))

            pattern = ''.join(pattern)
            html_string = '''* {}

{}
'''.format(id,fontname0,section.capitalize(),pattern)

            return html_string, id

        return None

def about_cs():

    df = load_data(about=True)

    fig = go.Figure(data=go.Choropleth(
        locations = df['CODE'],
        z = df['AvgTemp'],
        text = df['COUNTRY'],
        colorscale = 'RdBu_r',
        autocolorscale=False,
        marker_line_color='black',
        marker_line_width=0.9,
        colorbar_ticksuffix = '°C',
        colorbar_title = 'Temperature °C',
    ))

    fig.update_layout( geo=dict( showframe=True, showcoastlines=True, projection_type='natural earth'),font=dict(size=15,family='sora'),template='plotly_white' ,height=600,width=900,dragmode=False)
    fig.update_geos(lataxis_showgrid=True, lonaxis_showgrid=True)

    about_info = '''
    Forecasting the Long-term global warming rate is significant in a variety of fields, including climate research, agriculture, energy, medicine, and many more. This is project show cases detailed analysis of the change in temperatures for different countries. Challenge dwells on creating a reliable, efficient statistically reliable model on large data set and accurately capture relationship between average annual temperature. The data is predicted and forecasted by multiple machine learning algorithms which are specific towards TimeSeries data. The primary objective of this project is to put-forward a robust application which has the ability to predict average temperatures and the apparent temperature change based on the yearly trend. Out of all the methodologies we have empirically studied, Facebook’s Prophet showed prominent results. Having a robust system in place to constantly analyse and forecast on large amounts of data ensures the quantification of the rate of change of surface temperatures. Obtaining this analysed and forecasted data ahead of time allows the use of long-term preventive measures.
    '''

    st.markdown(f"""
Abstract:{about_info}
""", unsafe_allow_html=True)
    st.markdown('---')
    st.markdown(f'
Choropleth Projection of Average Temperatures Y.1743-2013
', unsafe_allow_html=True)
    cols = st.columns([1,6,1])
    cols[1].plotly_chart(fig)

    st.markdown('---')

def traveller_cs(data, mig):

    # info = {"Migrating": 'Migration: for students who are moving out of their home country for education & For people starting their professional endeavours in abroad.',
    #         "Visiting": "Visiting: for people who are travelling to a Country and its major cities."}
    info = {"Migrating": 'Migration: for students who are moving out of their home country for education & For people starting their professional endeavours in abroad.',}

    # instantiation
    hcity, mcity = None, None

    clist = list(np.sort(data['Country'].unique()))
    countries = ['Select Country', *clist]

    # data Access
    dcity, unis = mig

    scols = st.columns([6,3])
    category = scols[0].selectbox('What type are you?', ['Migrating','Visiting'],key='category')
    st.markdown(f""">
{info[category]}
""", unsafe_allow_html=True)
    st.markdown('***')

    if category == 'Migrating':

        st.markdown(f'
{category}

', unsafe_allow_html=True)
        st.markdown('')

        # A predefined list of purpose of migration
        purpose = ['Masters/PhD', 'Employment & Others']
        motive = scols[1].radio('Motive',purpose,key='purpose')

        if motive == 'Employment & Others':

            frm_cols = st.columns(2)

            with frm_cols[0]:

                hcountry = st.selectbox('Home Country',countries,key='hcountries')
                cltemp = data[data['Country']==hcountry]['City'].unique()
                hcitylist = ['Select City', *cltemp]

                mcountry = st.selectbox('Migrating to?',countries,key='mcountries')
                cltemp = data[data['Country']==mcountry]['City'].unique()
                mcitylist = ['Select City', *cltemp]

                st.markdown('')

            with frm_cols[1]:

                with st.form(key='emp'):
                    hcity = st.selectbox('Home City',hcitylist,key='hcities')
                    mcity = st.selectbox('City',mcitylist,key='mcities')

                    submit_button = st.form_submit_button(label='Analyse 🧐')

        elif motive == 'Masters/PhD':

            clist = np.sort(dcity['Country'].unique())
            countries = ['Select Country', *clist]

            ulist = unis['uni-country'].unique()
            universities = ['Select University', 'Other', *ulist]

            hcol = st.columns([6,3])

            hcountry = hcol[0].selectbox('Home Country', countries, key='masters-hc')
            uni = hcol[0].selectbox('University?',universities,key='unis')

            hcol[0].markdown("*Please be aware of the city/branch of the university you are applying to, if your city doesn't exist in the list, try with the cities nearby for general intuition.*")

            if not hcountry == 'Select Country' and not uni == 'Select University':
                tempcity = dcity[dcity['Country']==hcountry]['City'].unique()
                try:
                    uni,code = uni.split(',')
                except:
                    uni,_,code = uni.split(',')
                mcountry = unis[unis['codes'] == code]['Country'].unique()[0]
                ucities = dcity[dcity['Country']==mcountry]['City'].unique()

                hcities = ['Select City', *tempcity]
                with hcol[1].form(key='Cities'):
                    hcity = st.selectbox('Home City', hcities, key='masters-hcity')
                    mcity = st.selectbox('Branch/City', ucities, key='uni-branches')

                    submit_button = st.form_submit_button(label='Analyse 🧐')

    if not hcity == None and not mcity == None:

        if not hcity == 'Select City' and  not mcity == 'Select City':
            # checks
            if hcity == mcity:
                st.error("*Home City and the city you're Migrating to should'nt be the same, if it is then this analysis doesn't make sense.*")

            else:
                st.markdown('---')

                # Fetch Dictionaries from the pipeline

                homedata = city_synopsis(dcity, hcity)
                awaydata = city_synopsis(dcity, mcity)

                home, space, away = st.columns([6,0.5,6])

                y0 = np.random.randn(50)

                fig = go.Figure()
                fig.add_trace(go.Box(x=y0, name='Sample A',
                                marker_color = 'indianred'))

                with home:

                    # For Spacing & organisation after seasonal averages
                    if len(homedata['seasons'].keys()) < len(awaydata['seasons'].keys()):
                        diff = True
                    else:
                        diff = False

                    summary_markdown(hcity, homedata, diff, dcity, hcountry )

                with space:
                    for x in range(40):
                        st.markdown('|')

                with away:

                    # For Spacing & organisation after seasonal averages
                    if len(awaydata['seasons'].keys()) < len(homedata['seasons'].keys()):
                        diff = True
                    else:
                        diff = False

                    summary_markdown(mcity, awaydata, diff, dcity, mcountry)

                st.markdown('***')

def analytics_cs(data):

    clist = list(np.sort(data['Country'].unique()))

    countries = ['Select Country', *clist]

    slcols = st.columns([3,2])

    countryname = slcols[0].selectbox('Country',countries,key='country')
    emslcols = slcols[1].empty()

    st.markdown('***')

    if not mapping.get(countryname) == None:
        countryname = mapping.get(countryname)

    if not countryname == 'Select Country':

        monthex = median_agg(data,sequence='Month',loc='City',countryname=countryname)
        yearex = median_agg(data,sequence='Year',loc='City',countryname=countryname)

        try:
            if monthex == None:
                orgc = list(mapping.keys())[list(mapping.values()).index(countryname)]
                monthex = median_agg(data,sequence='Month',loc='City',countryname=orgc)
                yearex = median_agg(data,sequence='Year',loc='City',countryname=orgc)

        except:
            pass

        # st.write(orgc)
        cityname = emslcols.selectbox('Major Cities',pd.Series(dict(monthex.index)).index,key='cities')

        cols = st.columns(2)
        try:

            cols[0].markdown(f'''
{countryname}
{cityname}

''', unsafe_allow_html=True)
            countryname = orgc
        except:

            pass
            # cols[0].markdown(f'
{orgc}
{cityname}

', unsafe_allow_html=True)
            # countryname = orgc

        # Alignment
        st.markdown('')
        st.markdown('')
        # cols[1].markdown('')

        # Sections

        secs = cols[1].expander('Dashboard Sections',True)

        htmlstr0, ts_id = sections_cs('Time Series',['Selective Time-Series'])
        secs.markdown(htmlstr0, unsafe_allow_html=True)

        htmlstr1, qs_id = sections_cs('Quantified Summary')
        secs.markdown(htmlstr1, unsafe_allow_html=True)

        htmlstr2, ca_id = sections_cs('Comparative Analysis')
        secs.markdown(htmlstr2, unsafe_allow_html=True)

        htmlstr3, cs_id = sections_cs('Contrast & Summary')
        secs.markdown(htmlstr3, unsafe_allow_html=True)

        # Ids
        # st.write([ts_id, qs_id, ca_id, cs_id])

        st.markdown("",unsafe_allow_html=True)
        st.markdown(f'
Time Series
', unsafe_allow_html=True)
        st.markdown('')
        st.markdown('')

        # CONTRFOL
        if not countryname=='Select Country':
                # # Key Notes can Include the final machine learning quantified Delta T with st.metric and arrow to indicate an increase or a decrease
                # expcol = cols[1].expander(f'key-notes of {countryname}',False)
                # expcol.code(list(dict(monthex.index).keys()))

            # Plots
            pltcols = st.columns(2)

            fig1 = plotlychart(monthex.loc[cityname,:], cityname=cityname, sequence='Monthly', attr='AverageTemperatureUncertainty')
            pltcols[0].markdown('')
            pltcols[0].markdown('')
            # Montly Average Bar Chart
            pltcols[0].pyplot(plottimeseries(monthex,cityname))
            # Monthly Average Delta T
            pltcols[1].plotly_chart(fig1)

            # line break
            st.markdown('')

            tscols = st.columns([5,0.2,5])
            ts_avgtemp = plotlychart(yearex.loc[cityname,:], cityname=cityname, sequence='Yearly', attr='AverageTemperature')
            ts_avgun = plotlychart(yearex.loc[cityname,:], cityname=cityname, sequence='Yearly', attr='AverageTemperatureUncertainty')

            tscols[0].plotly_chart(ts_avgtemp)
            tscols[2].plotly_chart(ts_avgun)

        st.markdown("",unsafe_allow_html=True)
        st.markdown(f'
Quantified Summary
', unsafe_allow_html=True)
        st.markdown('')
        st.markdown('')

        # Dont Delete -- Redirection
        st.markdown(f'Jump to top of page',unsafe_allow_html=True)

@st.cache(persist=True)
def prophetize(dmajcity, province):

    df = dmajcity[dmajcity['City']==province]

    # initialise model
    model = Prophet(yearly_seasonality=True)

    dt_prophet = df[['dt','AverageTemperature']]
    dt_prophet.columns = ['ds','y']

    # fit model
    model.fit(dt_prophet)

    future_dates=model.make_future_dataframe(periods=2920)
    prediction=model.predict(future_dates)
    pred_df = prediction
    pred_df['year'] = pred_df['ds'].dt.year
    pred_df['day'] = pred_df['ds'].dt.day
    pred_df['month'] = pred_df['ds'].dt.strftime("%b")

    # prune dates
    pred_df = pred_df[pred_df['year']>2013]
    pred_df = pred_df[pred_df['day']==1]
    pred_df = pred_df.drop('day',axis=1)

    return pred_df

def project_yhat(data,city,year,mode='compound'):

    # read data

    hydorg = pd.read_csv('./data/HydAvg18-20Org.csv').drop('Unnamed: 0',axis=1)
    tororg = pd.read_csv('./data/TorontoAvg18-20Org.csv').drop('Unnamed: 0',axis=1)

    if mode == 'compound':
        data = data[(data['year']>=2018)&(data['year']<2021)]
        prompt = 'Prophet Forecast for Average Temperatures in {}'
        if city == 'Hyderabad':
            original = hydorg['Average']
        else:
            original = tororg['Avg.Temp']
    elif mode == 'individual':
        data = data[data['year']==year]
        prompt = 'Prophet Forecast for Average Temperatures in {}, {}'

        if city == 'Hyderabad':
            original = hydorg[hydorg['year ']==year]['Average']
        else:
            original = tororg[tororg['year ']==year]['Avg.Temp']

    x = data['ds']
    y0 = data.yhat
    y1 = data.yhat_lower
    y2 = data.yhat_upper

    # Create traces
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=x, y=y0,
                        mode='lines+markers',
                        name='yhat', line=dict(color='crimson')))
    fig.add_trace(go.Scatter(x=x, y=original,
                        mode='lines+markers',
                        name='original', line=dict(color='dodgerblue')))
    fig.add_trace(go.Scatter(x=x, y=y1,
                        name='yhat_lower', line=dict(color='green', width=2,
                              dash='dash')))
    fig.add_trace(go.Scatter(x=x, y=y2,

                        name='yhat_upper', line=dict(color='orange', width=2,
                              dash='dot')))

    fig.update_traces(marker=dict(
                              line=dict(width=2,
                                        color='DarkSlateGrey')),
                  selector=dict(mode='markers'))

    fig.update_layout(
        title=prompt.format(city,year),
        title_x=0.5,
        xaxis_title="Months",
        yaxis_title="Average Temperature",
        legend_title="Legend",)

    return fig

@st.cache(persist=True)
def gen_seasonal_dfs(dmajcity,city):

    datapackage = {}
    seasondfs = []

    # slice by CITY
    data = dmajcity[dmajcity['City']==city]

    # get seasons
    seasons = data['Seasons'].unique()
    datapackage['Seasons'] = seasons

    # hemisphere
    datapackage['Hemisphere'] = data['Hemisphere'].unique()[0]

    for season in seasons:
        seasondfs.append(data[data['Seasons']==season])

    datapackage['dfs'] = seasondfs

    return datapackage

def seasonality_prophet(dflist,seasons):
    models = {}
    for df in dflist:
        season = df['Seasons'].unique()[0]

        # initialise model
        print(f'{season.upper()}')

        model = Prophet(yearly_seasonality=True)

        dt_prophet = df[['dt','AverageTemperature']]
        dt_prophet.columns = ['ds','y']

        # fit
        model.fit(dt_prophet)

        # caching
        models[season] = model
        models[season+'-months'] = df['Month'].unique()

    return models

@st.cache(allow_output_mutation=True,suppress_st_warning=True)
def quantify_future_seasonality(year, model_dict,seasons):

    quantified = {}

    season_models = model_dict
    future_dates=season_models['Winter'].make_future_dataframe(periods=2920)

    # my_bar = st.progress(0)

    for season in seasons:
        prediction=season_models[season].predict(future_dates)
        df = prediction
        df['year'] = df['ds'].dt.year
        df['day'] = df['ds'].dt.day
        df['month'] = df['ds'].dt.strftime("%b")

        # prune dates
        df = df[df['year']>2013]
        df = df[df['day']==1]
        df = df.drop('day',axis=1)
        df = df[df['month'].isin(season_models[f'{season}-months'])]

        quantified[season] = df[df['year']==year]
        # my_bar.progress(season)

    return quantified

def prophet_cs(dmajcity):

    mode = st.selectbox('Modes',['TimeSeries-Prophet', 'Seasonal-Prophet'],key='prophet-Modes')

    st.markdown(f'
{mode}

', unsafe_allow_html=True)

    # Coverting dt to datetime obj
    dmajcity['dt'] = pd.to_datetime(dmajcity['dt'])

    if mode == 'TimeSeries-Prophet':
        cities = ['Toronto','Hyderabad']

        cols = st.columns(2)
        city = cols[1].selectbox('City',cities,key='casestudy')
        cols[0].markdown(f'

{city}

', unsafe_allow_html=True)

        pred_df = prophetize(dmajcity, province=city)

        fig = project_yhat(pred_df,city=city,year=2020,mode='compound')

        st.plotly_chart(fig,use_container_width=True)

        year = st.slider('Year', min_value=2018,max_value=2020, key='yearslider')

        fig = project_yhat(pred_df,city=city,year=year,mode='individual')
        st.plotly_chart(fig,use_container_width=True)

    else:

        countries = list(dmajcity['Country'].unique())
        countries.insert(0, 'Select Country')

        cols = st.columns([3,2])

        country = cols[0].selectbox('Country',countries, key='select-country')

        if not country == 'Select Country':
            cities = dmajcity[dmajcity['Country']==country]['City'].unique()

            with cols[1].form(key='season'):

                city = st.selectbox('City', cities, key='season-sets')
                year = st.slider('Year',min_value=2018, max_value=2020)

                submit_button = st.form_submit_button(label='Forecast 🌡')

            if submit_button:
                datapackage = gen_seasonal_dfs(dmajcity, city)
                season_models = seasonality_prophet(datapackage['dfs'], datapackage['Seasons'])

                quantified = quantify_future_seasonality(year, model_dict=season_models, seasons=datapackage['Seasons'])

                st.markdown('***')
                st.markdown(f'***Year - {year}***')

                cols = st.columns(len(quantified.keys()))

                for i, (key, value) in enumerate(zip(quantified.keys(),quantified.values()),0):

                        season = key
                        value = value['yhat'].mean()

                        cols[i].metric(label=season, value=str("%.2f" % value)+' °C',delta_color='inverse')

                # caching.clear_cache()

def cs_main():

    st.set_option('deprecation.showPyplotGlobalUse', False)
    st.set_page_config(page_title="Temperature Dashboard",layout="wide",initial_sidebar_state="collapsed")
    st.markdown(font_url0,unsafe_allow_html=True)
    st.markdown(font_url1,unsafe_allow_html=True)

    menu_data = [

        {'icon': "far fa-chart-bar", 'label':"Analytics", "ttip":"Ton of Visualisations"},
        {'icon': "fa fa-cogs", 'label':"Prophet", "ttip":"Here lies the power of Machine Learning"},
        {'icon': "fa fa-blind", 'label':"Traveller?",'ttip':"The Traveller's Acquaintance"},
        # {'icon': "far fa-copy", 'label':"Right End"},
    ]

    # we can override any part of the primary colors of the menu
    #over_theme = {'txc_inactive': '#FFFFFF','menu_background':'red','txc_active':'yellow','option_active':'blue'}
    over_theme = {'txc_inactive': 'black','menu_background':'#F5F5F5','txc_active':'#058DFC'}
    menu_id = hc.nav_bar(menu_definition=menu_data,home_name='About',override_theme=over_theme)

    st.markdown(f"
Global Temperature Variation Analysis & Modelling
| {menu_id}

",unsafe_allow_html=True)
    st.markdown("",unsafe_allow_html=True)
    st.markdown('***')

     # data
    data = load_data(about=False)
    dcity, unis = load_provinces()

    # unis = load_provinces()

    if menu_id == 'Analytics':
        analytics_cs(data)

    elif menu_id == 'About':
        about_cs()

    elif menu_id == 'Prophet':
        prophet_cs(data)

    elif menu_id == 'Traveller?':
        traveller_cs(data, (dcity,unis))

if __name__ == '__main__':
    cs_main()

reason: import numpy,from numpy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 6 11:46:41 2020

@author: alfredocu
"""

import numpy as np
import matplotlib.pyplot as plt

class  LinearNeuron:
    def __init__(self, n_inputs, learning_rate = 0.1):
        self.w = -1 + 2 * np.random.rand(n_inputs)
        self.b = -1 + 2 * np.random.rand()
        self.eta = learning_rate # Factor de aprendizaje.

    # Predicción.
    def predict(self, X):
        Y_est = np.dot(self.w, X) + self.b # Propagación.
        return Y_est

    # Entrenamiento.
    def train(self, X, Y, epochs = 50, solver ="SGD"):
        p = X.shape[1] # Obtener la cantidad de patrones.
        if solver == "SGD":
            for _ in range(epochs):
                for i in range(p):
                    y_est = self.predict(X[:, i])
                    self.w += self.eta * (Y[:, i] - y_est) * X[:, i]
                    self.b += self.eta * (Y[:, i] - y_est)

        elif solver == "BDG":
              for _ in range(epochs):
                    y_est = np.dot(self.w, X) + self.b
                    self.w += (self.eta / p) * np.dot((Y - y_est), X.T).ravel()
                    self.b += (self.eta / p) * np.sum(Y - y_est)

        else: # Direct
            X_hat = np.concatenate((np.ones((1,p)), X), axis = 0)
            w_hat = np.dot(Y.reshape(1, -1), np.linalg.pinv(X_hat))
            self.b = w_hat[0, 0]
            self.w = w_hat[0, 1:]

# Ejemplo.
p=500 # p = 200
x = -1 + 2 * np.random.rand(p).reshape(1, -1)
y = -18 * x + 6 + 30 * np.random.rand(p)
plt.plot(x, y, '.b')

neuron = LinearNeuron(1, 0.1)
neuron.train(x, y, epochs=200, solver="Direct")

# Dibujar linea.
xn = np.array([[-1, 1]])
plt.plot(xn.ravel(), neuron.predict(xn), "--k")
plt.savefig("Direct.eps", format="eps")

max_stars_repo_name: AlfredoCU/LInear-Neuron

max_issues_repo_name: AlfredoCU/LInear-Neuron

max_forks_repo_name: AlfredoCU/LInear-Neuron