text:
# MIT License
#
# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2020
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import logging

import numpy as np
import pytest

from art.attacks.evasion import DPatch
from art.estimators.estimator import BaseEstimator, LossGradientsMixin
from art.estimators.object_detection.object_detector import ObjectDetectorMixin
from tests.attacks.utils import backend_test_classifier_type_check_fail
from tests.utils import master_seed
from tests.utils import ARTTestException

logger = logging.getLogger(__name__)

@pytest.fixture()
def fix_get_mnist_subset(get_mnist_dataset):
    (x_train_mnist, y_train_mnist), (x_test_mnist, y_test_mnist) = get_mnist_dataset
    n_train = 100
    n_test = 11
    yield x_train_mnist[:n_train], y_train_mnist[:n_train], x_test_mnist[:n_test], y_test_mnist[:n_test]

@pytest.mark.parametrize("random_location", [True, False])
@pytest.mark.parametrize("image_format", ["NHWC", "NCHW"])
@pytest.mark.framework_agnostic
def test_augment_images_with_patch(art_warning, random_location, image_format, fix_get_mnist_subset):
    try:
        (x_train_mnist, y_train_mnist, x_test_mnist, y_test_mnist) = fix_get_mnist_subset

        # TODO this master_seed should be removed as it is already set in conftest.py. expected values will
        # need to be updated accordingly
        master_seed()

        if image_format == "NHWC":
            patch = np.ones(shape=(4, 4, 1)) * 0.5
            x = x_train_mnist[0:3]
            channels_first = False
        elif image_format == "NCHW":
            patch = np.ones(shape=(1, 4, 4)) * 0.5
            x = np.transpose(x_train_mnist[0:3], (0, 3, 1, 2))
            channels_first = True

        patched_images, transformations = DPatch._augment_images_with_patch(
            x=x, patch=patch, random_location=random_location, channels_first=channels_first
        )

        if random_location:
            transformation_expected = {"i_x_1": 0, "i_y_1": 2, "i_x_2": 4, "i_y_2": 6}
            patched_images_column = [
                0.0,
                0.0,
                0.5,
                0.5,
                0.5,
                0.5,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ]
        else:
            transformation_expected = {"i_x_1": 0, "i_y_1": 0, "i_x_2": 4, "i_y_2": 4}
            patched_images_column = [
                0.5,
                0.5,
                0.5,
                0.5,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
            ]

        assert transformations[1] == transformation_expected

        if image_format == "NCHW":
            patched_images = np.transpose(patched_images, (0, 2, 3, 1))

        np.testing.assert_array_equal(patched_images[1, 2, :, 0], patched_images_column)
    except ARTTestException as e:
        art_warning(e)

@pytest.mark.framework_agnostic
def test_classifier_type_check_fail(art_warning):
    try:
        backend_test_classifier_type_check_fail(DPatch, [BaseEstimator, LossGradientsMixin, ObjectDetectorMixin])
    except ARTTestException as e:
        art_warning(e)


text:
from logger import logger
from api import api
from twitter import twitter
from datetime import datetime
import urllib

import pandas as pd
import numpy as np

import sys

import reviews.walmart as walmart

def compress(query):
    # COMPRESS SENTIMENT
    tweets = api.tweet_data().search(query)

    # handle currently no data
    if len(tweets) < 1:
        return

    tweets = pd.DataFrame(tweets).dropna()
    tweets.index = tweets['tweet_date'].apply(lambda x: datetime.strptime(x,"%Y-%m-%dT%H:%M:%S.%fZ"))

    def weighted_mean(arr):
        score = [float(score) for score in arr['tweet_score']]
        sentiment = [float(sentiment) for sentiment in arr['tweet_sentiment']]

        return ( np.sum([ i*j for i,j in zip(score, sentiment) ]) / np.sum(score) )

    print(tweets)

    tweets = tweets.resample('D', how=weighted_mean)['tweet_sentiment']

    for date, sentiment in tweets.items():
        sentiment = 1000*round(sentiment,3)
        if(sentiment != 0):
            api.tweet_sentiment().delete(query, str(date))
            api.tweet_sentiment().create(query, str(date), str(sentiment))
        else:
            print("Could not add empty Sentiment")

def get_tweets():
    while 1:
        # Get Old Tweets
        track = api.tracker().next()
        track['keyword'] = urllib.parse.quote_plus(track['keyword'])
        print("> " + str(track['keyword']))
        twitter.twitter(track['keyword'], track['id'], track['min_id'], track['max_id'], 5).loopTweets()

        # Get Recent Tweets
        track = api.tracker().next()
        print("> " + str(track['keyword']))
        twitter.twitter(track['keyword'], 0, 0, 5).loopTweets()

def get_sentiment():
    while 1:
        tracker = api.tracker().get()

        for track in tracker:
            print("> " + track['keyword'])
            compress(track['keyword'])

def add_keyword(keyword):
    api.tracker().create(keyword)

def remove_keyword(keyword):
    api.tracker().delete(keyword)

def get_reviews():
    for i in range(1, 1000000):
        rt = walmart.walmart().data
        print(rt)

        if len(rt) > 0:
            api.review().create(rt)

# Option Selection
opt = sys.argv[1]

if opt == '--tweets':
    get_tweets()
elif opt == '--sentiment':
    get_sentiment()
elif opt == '--keyword_add':
    add_keyword(sys.argv[2])
elif opt == '--keyword_remove':
    remove_keyword(sys.argv[2])
elif opt == '--reviews':
    get_reviews()
else:
    print('Please select an option')

ext: py

max_stars_repo_name: BraunPhilipp/sentiment-analyzer

max_issues_repo_name: BraunPhilipp/sentiment-analyzer

max_forks_repo_name: BraunPhilipp/sentiment-analyzer

reason: import numpy


text:
def run_prophet(tickers,ticker,market_dfset,modelset,forecastset):
    from fbprophet import Prophet
    import numpy as np
    import yfinance as yf

    market_df = yf.Ticker(ticker).history(period="max",auto_adjust=True,prepost=True)
    if len(market_df) == 0:
        tickers.remove(ticker)
        #return
    #market_df = market_df[~market_df.index.duplicated()]
    df = market_df.reset_index().rename(columns={'Date':'ds', 'Close':'y'})
    df['y'] = np.log(df['y'])
    model = Prophet(weekly_seasonality=False,)
    model.fit(df)
    future = model.make_future_dataframe(periods=365) #forecasting for 1 year from now.
    forecast = model.predict(future)
    modelset[ticker] = model
    market_dfset[ticker] = market_df
    forecastset[ticker] = forecast

def run_prophet_daily(tickers,ticker,market_dfset,modelset,forecastset):
    from fbprophet import Prophet
    import pandas as pd
    import numpy as np
    import yfinance as yf
    from datetime import date,timedelta

    market_df = yf.download(tickers=ticker,start=(date.today()+timedelta(days=-29)).strftime("%Y-%m-%d"),end=(date.today()+timedelta(days=-29+6)).strftime("%Y-%m-%d"),interval="1m").tz_localize(None)
    for i in range(1,5): market_df = market_df.append(yf.download(tickers=ticker,start=(date.today()+timedelta(days=-29+6*i)).strftime("%Y-%m-%d"),end=(date.today()+timedelta(days=-29+6+6*i)).strftime("%Y-%m-%d"),interval="1m").tz_localize(None))
    if len(market_df) == 0:
        tickers.remove(ticker)
        #return
    #market_df = market_df[~market_df.index.duplicated()]
    df = market_df.reset_index().rename(columns={'Datetime':'ds', 'Close':'y'})
    df['y'] = np.log(df['y'])
    model = Prophet(changepoint_prior_scale=0.01,daily_seasonality=True,weekly_seasonality=False,yearly_seasonality=False)
    model.fit(df);
    future = model.make_future_dataframe(periods=1*24*60,freq="1min") #forecasting for 1 day from now.
    future2 = future.copy()
    future2 = pd.merge(future2[future2['ds'].dt.hour > 9 ],future2[future2['ds'].dt.hour < 16],how='inner')
    future3 = future.copy()
    future3 = pd.merge(future3[future3['ds'].dt.hour >= 9 ],future3[future3['ds'].dt.hour < 10],how='inner')
    future3 = future3[future3['ds'].dt.minute >=30]
    future = future3.append(future2)
    forecast = model.predict(future)
    modelset[ticker] = model
    market_dfset[ticker] = market_df
    forecastset[ticker] = forecast

def run_neuralprophet(tickers,ticker,market_dfset,modelset,forecastset):
    from neuralprophet import NeuralProphet
    import yfinance as yf

    market_df = yf.Ticker(ticker).history(period="max",auto_adjust=True,prepost=True)[['Close']]
    if len(market_df) == 0:
        tickers.remove(ticker)
        #return
    #market_df = market_df[~market_df.index.duplicated()]
    df = market_df.reset_index().rename(columns={'Date':'ds', 'Close':'y'})
    future_periods = 30
    model = NeuralProphet(n_forecasts=future_periods,
    n_lags= future_periods * 2 - 1,
    #n_lags=len(df),
    n_changepoints=100,
    #yearly_seasonality=True,
    #weekly_seasonality=False,
    #daily_seasonality=False,
    batch_size=64,
    epochs=100,
    learning_rate=1.0)
    #model = NeuralProphet(    n_forecasts=future_periods,
    #n_lags=future_periods * 2 - 1,
    ##n_lags = len(df),
    ##n_lags=5,
    #yearly_seasonality=True,
    #weekly_seasonality=False,
    #daily_seasonality=False,)
    metrics = model.fit(df, validate_each_epoch=True,
                    valid_p=0.2, freq='D',
                    plot_live_loss=True,
                    epochs=100)
    #metrics = model.fit(df, validate_each_epoch=True, valid_p=0.2, freq="D")
    #future = model.make_future_dataframe(df, periods=future_periods, n_historic_predictions=len(df))
    future = model.make_future_dataframe(df, periods=future_periods, n_historic_predictions=True)
    forecast = model.predict(future)
    model.highlight_nth_step_ahead_of_each_forecast(step_number=model.n_forecasts)
    modelset[ticker] = model
    market_dfset[ticker] = market_df
    forecastset[ticker] = forecast

def plot_forecast(model, data, periods, historic_pred=True, highlight_steps_ahead=None):

    """ plot_forecast function - generates and plots the forecasts for a NeuralProphet model
    - model -> a trained NeuralProphet model
    - data -> the dataframe used for training
    - periods -> the number of periods to forecast
    - historic_pred -> a flag indicating whether or not to plot the model's predictions on historic data
    - highlight_steps_ahead -> the number of steps ahead of the forecast line to highlight, used for autoregressive models only"""

    future = model.make_future_dataframe(data,
                                         periods=periods,
                                         n_historic_predictions=historic_pred)
    forecast = model.predict(future)

    if highlight_steps_ahead is not None:
        model = model.highlight_nth_step_ahead_of_each_forecast(highlight_steps_ahead)
        model.plot_last_forecast(forecast)
    else:
        model.plot(forecast)

max_stars_repo_name: alvin-chang/frankensteinprophet

max_issues_repo_name: alvin-chang/frankensteinprophet

max_forks_repo_name: alvin-chang/frankensteinprophet


text:
import sys
import jsonl
from fragments import Fragments
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
from goodSummaries import createDictionary

def get_colors():
    all_colors = list(mcolors.BASE_COLORS)
    all_colors.extend(mcolors.TABLEAU_COLORS)
    all_colors.extend(mcolors.CSS4_COLORS)

    bad_colors = ['w', 'whitesmoke', 'white', 'snow', 'mistyrose', 'seashell',
        'linen', 'oldlace', 'floralwhite', 'cornsilk', 'lemonchiffon', 'ivory',
        'beige', 'lightyellow', 'lightgoldenyellow', 'honeydew', 'mintcream', 'azure',
        'lightcyan', 'aliceblue', 'ghostwhite', 'lavenderbush']

    good_colors = []
    for color in all_colors:
        if not color in bad_colors:
            good_colors.append(color)
    return good_colors

COLORS = get_colors()

def create_matrix(cluster):
    articles = []
    summaries = []
    for article in cluster:
        articles.append(article)
        summaries.append(article[1])

    matrix = []
    num = 0
    for article in articles:
        text = article[0]
        title = article[2]
        entries = []
        for index in range(len(summaries)):
            summary = summaries[index]
            fragments = Fragments(summary, text)
            obj = ASData(article, summary, title, True, fragments.coverage(),
                fragments.density(), fragments.compression())
            entries.append(obj)
        matrix.append(entries)
        num += 1
    return matrix

def cdplot(cluster):
    """Generates a scatter plot showing the relationship between coverage and
    density for the inputted matrix"""

    matrix = create_matrix(cluster)
    fig = plt.figure(figsize=(13,8))
    fig.add_subplot(223)
    plt.xlabel('coverage')
    plt.ylabel('density')
    for x in range(len(matrix)):
        coverages = []
        densities = []
        title = matrix[x][0].getTitle()[:20]
        for obj in matrix[x]:
            coverages.append(obj.getCoverage())
            densities.append(obj.getDensity())
        plt.scatter(coverages, densities, marker = 'o', c=COLORS[x], label=title, alpha=0.6)
    plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
    return plt

def complot(cluster):
    """Generates a dot plot for the compression for the inputted matrix"""

    matrix = create_matrix(cluster)
    fig = plt.figure(figsize=(13,8))
    fig.add_subplot(223)
    plt.xlabel('article')
    plt.ylabel('compression')
    for x in range(len(matrix)):
        compressions = []
        title = matrix[x][0].getTitle()[:20]
        for obj in matrix[x]:
            compressions.append(obj.getCompression())
        plt.scatter([x]*len(compressions), compressions, marker='o', c=COLORS[x], label=title, alpha=0.6)
    plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
    return plt

def article_cdplot(article, summary_list):
    fig = plt.figure(figsize=(13,8))
    fig.add_subplot(223)
    plt.xlabel('coverage')
    plt.ylabel('density')
    text = dict[article][1]
    for i, s in enumerate(summary_list):
        summary = dict[s][0]
        fragments = Fragments(summary, text)
        coverage = [fragments.coverage()]
        density = [fragments.density()]
        plt.scatter(coverage, density, marker = 'o', c=COLORS[i], label=s, alpha=0.6)
    plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
    return plt

def article_complot(article, summary_list):
    fig = plt.figure(figsize=(13,8))
    fig.add_subplot(223)
    plt.xlabel('summary')
    plt.ylabel('compression')
    text = dict[article][1]
    for i, s in enumerate(summary_list):
        summary = dict[s][0]
        fragments = Fragments(summary, text)
        compression = [fragments.compression()]
        plt.scatter(i, compression, marker='o', c=COLORS[i], label=s, alpha=0.6)
        plt.legend(bbox_to_anchor=(1.05,1), loc=2, borderaxespad=0.)
    return plt

class ASData(object):
    """A class representing data for an article-summary analysis

    INSTANCE ATTRIBUTES:
        _match [bool]: whether or not the summary is a match for the article
        _data [str]: name of jsonl file with articles
        _coverage [float]: the coverage for the article-summary pair
        _density [float]: the density for the article-summary pair
        _compression [float]: the compression for the article-summary pair
        _title [str]: the title of the article
        """

    def __init__(self, article, summary, title, match, coverage = None, density = None, compression = None):
        self._match = match
        self._article = article
        self._summary = summary
        self._title = title
        if match == True:
            self._coverage = coverage
            self._density = density
            self._compression = compression

    def getMatch(self):
        return self._match

    def getTitle(self):
        return self._title

    def getCoverage(self):
        return self._coverage

    def getDensity(self):
        return self._density

    def getCompression(self):
        return self._compression

    def __repr__(self):
        if self._match == True:
            return "Coverage: "+str(self._coverage)+", Density: "+str(self._density)+", Compression: "+str(self._compression)+'\n'
        else:
            return "Not a match\n"

if __name__ == '__main__':
    colors()


text:
from collections import deque
import numpy as np
from IPython import get_ipython
ipython = get_ipython()

# FIFO with a deque
# Using a deque is most effecicent when you only need access to
# the first and last values in a container (such as a FIFO)
print("Deque FIFO")
x = deque(range(5), 5)
print(x)
x.append(5)
print(x)

print("\n\n")

# FIFO with a List
# need to append and pop. Appending to a list is efficient
# as is pop() from the end (as done for a stack) but pop(0)
# from the start of a list is not because all elements must
# be copied and moved
print("List FIFO")
x = list(range(5))
print(x)
x.append(5)
print(x)
x.pop(0)
print(x)

print("\n\n")

# FIFO with NumPy
print("NumPy FIFO")
x = np.array(range(5))
print(x)
# shift
x[:-1] = x[1:]
print(x)
# add new value
x[-1] = 5
print(x)


text:
"""
A Python adapter module for Roomba Open Interface

This module is based on the document: iRobot® Roomba 500 Open Interface (OI) Specification
- Link https://www.irobot.lv/uploaded_files/File/iRobot_Roomba_500_Open_Interface_Spec.pdf

"""
import os
import math
import sys
from time import sleep

import json
import serial  # pyserial

class PyRoombaAdapter:
    """
    Adapter class for Roomba Open Interface

    The constructor connects serial port and change the mode to safe mode

    :param string port: Serial port path

    :param int bau_rate: bau rate of serial connection (default=115200)

    :param float time_out_sec: read time out of serial connection [sec] (default=1.0)

    :param float wheel_span_mm: wheel span of Roomba [mm]  (default=235.0)

    Examples:
        >>> PORT = "/dev/ttyUSB0"
        >>> adapter = PyRoombaAdapter(PORT)

    """
    CMD = {"Start": 128,
           "Baud": 129,
           "Safe": 131,
           "Full": 132,
           "Power": 133,
           "Spot": 134,
           "Clean": 135,
           "Max": 136,
           "Drive": 137,
           "Moters": 138,
           "Song": 140,
           "Play": 141,
           "Sensors": 142,
           "Seek Dock": 143,
           "PWM Moters": 144,
           "Drive Direct": 145,
           "Drive PWM": 146,
           "Query List": 149,
           "Stream": 148,
           "Buttons": 165,
           }

    PARAMS = {
        "STRAIGHT_RADIUS": 32768,
        "MIN_RADIUS": -2000,
        "MAX_RADIUS": 2000,
        "MIN_VELOCITY": -500,
        "MAX_VELOCITY": 500,
    }

    def __init__(self, port, bau_rate=115200, time_out_sec=1., wheel_span_mm=235.0):
        self.WHEEL_SPAN = wheel_span_mm
        try:
            self.serial_con = self._connect_serial(port, bau_rate, time_out_sec)
        except SerialException:
            print("Cannot find serial port. Plase reconnect it.")
            sys.exit(1)

        with open(os.path.join(os.path.dirname(__file__), "sensor_packet.json")) as f:
            self.sensor_packet = json.load(f)
        self.change_mode_to_safe()  # default mode is safe mode
        sleep(1.0)

    def __del__(self):
        """
        Destructor of PyRoombaAdapter class

        The Destructor make Roomba move to passive mode and close serial connection
        """
        # disconnect sequence
        self.change_mode_to_passive()  # move to passive mode
        sleep(0.1)
        self.serial_con.close()

    def start_cleaning(self):
        """
        Start the default cleaning

        - Available in modes: Passive, Safe, or Full
        - Changes mode to: Passive

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.start_cleaning()
        """
        self._send_cmd(self.CMD["Clean"])

    def start_max_cleaning(self):
        """
        Start the max cleaning

        - Available in modes: Passive, Safe, or Full
        - Changes mode to: Passive

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.start_max_cleaning()
        """
        self._send_cmd(self.CMD["Max"])

    def start_spot_cleaning(self):
        """
        Start spot cleaning

        - Available in modes: Passive, Safe, or Full
        - Changes mode to: Passive

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.start_spot_cleaning()
        """
        self._send_cmd(self.CMD["Spot"])

    def start_seek_dock(self):
        """
        Start seek dock

        - Available in modes: Passive, Safe, or Full
        - Changes mode to: Passive

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.start_seek_dock()
        """
        self._send_cmd(self.CMD["Seek Dock"])

    def change_mode_to_passive(self):
        """
        Change mode to passive mode

        Roomba beeps once to acknowledge it is starting from “off” mode.

        - Available in modes: Passive, Safe, or Full
        """
        self._send_cmd(self.CMD["Start"])
        sleep(0.1)

        # check mode
        if self.request_data(["OI Mode"])[0] == 1:
            print("change mode to passive")
        else:
            print("cannot change mode to passive")
            sys.exit(1)

    def change_mode_to_safe(self):
        """
        Change mode to safe mode

        Safe mode turns off all LEDs.
        If a safety condition occurs, Roomba reverts automatically to Passive mode.

        - Available in modes: Passive, Safe, or Full
        """
        # send command
        self._send_cmd(self.CMD["Safe"])
        sleep(0.1)

        # check mode
        if self.request_data(["OI Mode"])[0] == 2:
            print("change mode to safe")
        else:
            print("cannot change mode to safe")
            sys.exit(1)

    def change_mode_to_full(self):
        """
        Change mode to full mode

        Full mode turns off the cliff, wheel-drop and internal charger safety features.
        In Full mode, Roomba executes any command that you send it, even if the internal charger is plugged in,
        or command triggers a cliff or wheel drop condition.

        - Available in modes: Passive, Safe, or Full
        """
        # send command
        self._send_cmd(self.CMD["Full"])
        sleep(0.1)

        # check mode
        if self.request_data(["OI Mode"])[0] == 3:
            print("change mode to full")
        else:
            print("cannot change mode to full")
            sys.exit(1)

    def turn_off_power(self):
        """
        Turn off power of Roomba

        The mode change to passive mode.

        - Available in modes: Passive, Safe, or Full

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.turn_off_power()
        """
        # send command
        self._send_cmd(self.CMD["Power"])

    def request_data(self, request_data_list):
        data_list = []
        for request_data in request_data_list:
            self._send_cmd(self.CMD["Sensors"])
            self._send_cmd(self.sensor_packet[request_data_list[0]]["ID"])
            byte = self.sensor_packet[request_data_list[0]]["bytes"]
            data = self.serial_con.read(byte)
            data_list.append(self._get_value_from_bytes(data))
        return data_list

    def move(self, velocity, yaw_rate):
        """
        control roomba at the velocity and the rotational speed (yaw rate)

        Note:
            The Roomba keep a control command until receiving next command

        - Available in modes: Safe or Full
        - Changes mode to: No Change

        - Special cases:
            - Straight = 32768 or 32767 = hex 8000 or 7FFF
            - Turn in place clockwise = -1
            - Turn in place counter-clockwise = 1

        :param float velocity: velocity (m/sec)

        :param float yaw_rate: yaw rate (rad/sec)

        Examples:
            >>> import numpy as np
            >>> import time
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.move(0, np.rad2deg(-10)) # rotate to right side
            >>> time.sleep(1.0) # keep rotate
            >>> adapter.move(0.1, 0.0) # move straight with 10cm/sec
        """
        if velocity == 0:  # rotation
            vel_mm_sec = math.fabs(yaw_rate) * (self.WHEEL_SPAN / 2.0)
            if yaw_rate >= 0:
                radius_mm = 1
            else:  # default is 'CCW' (turning left)
                radius_mm = -1
        elif yaw_rate == 0:
            vel_mm_sec = velocity * 1000.0  # m/s -> mm/s
            radius_mm = self.PARAMS["STRAIGHT_RADIUS"]
        else:
            vel_mm_sec = velocity * 1000.0  # m/s -> mm/s
            radius_mm = vel_mm_sec / yaw_rate

        self.send_drive_cmd(vel_mm_sec, radius_mm)

    def send_drive_cmd(self, roomba_mm_sec, roomba_radius_mm):
        """
        send drive command

        This command controls Roomba’s drive wheels.
        The radius is measured from the center of the turning circle to the center of Roomba.
        A Drive command with a positive velocity and a positive radius makes Roomba drive forward while turning left.
        A negative radius makes Roomba turn toward the right.
        Special cases for the radius make Roomba turn in place or drive straight.
        A negative velocity makes Roomba drive backward.

        :param float roomba_mm_sec: the average velocity of the drive wheels in millimeters per second (-500 – 500 mm/s)

        :param float roomba_radius_mm: the radius in millimeters at which Roomba will turn. (-2000 – 2000 mm)

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.send_drive_cmd(-100, -1000) # back to the right side
            >>> sleep(2.0) # keep 2 sec
        """
        # print(roomba_mm_sec, roomba_radius_mm, turn_dir)

        roomba_mm_sec = self._adjust_min_max(roomba_mm_sec, self.PARAMS["MIN_VELOCITY"], self.PARAMS["MAX_VELOCITY"])
        velHighVal, velLowVal = self._get_2_bytes(roomba_mm_sec)

        roomba_radius_mm = self._adjust_min_max(roomba_radius_mm, self.PARAMS["MIN_RADIUS"], self.PARAMS["MAX_RADIUS"])
        radiusHighVal, radiusLowVal = self._get_2_bytes(roomba_radius_mm)

        # send these bytes and set the stored velocities
        self._send_cmd([self.CMD["Drive"], velHighVal, velLowVal, radiusHighVal, radiusLowVal])

    def send_drive_direct(self, right_mm_sec, left_mm_sec):
        """
        send drive direct command

        This command lets you control the forward and backward motion of Roomba’s drive wheels independently.
        A positive velocity makes that wheel drive forward, while a negative velocity makes it drive backward.

        - Available in modes: Safe or Full

        :param float right_mm_sec: the velocity of the right drive wheels in millimeters per second (-500 – 500 mm/s)

        :param float left_mm_sec: the velocity of the left drive wheels in millimeters per second (-500 – 500 mm/s)

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.send_drive_direct(10, 100) # move right forward
            >>> sleep(2.0) # keep 2 sec
        """
        right_mm_sec = self._adjust_min_max(right_mm_sec, self.PARAMS["MIN_VELOCITY"], self.PARAMS["MAX_VELOCITY"])
        right_high, right_low = byte_tool.get_2_bytes(right_mm_sec)

        left_mm_sec = self._adjust_min_max(left_mm_sec, self.PARAMS["MIN_VELOCITY"], self.PARAMS["MAX_VELOCITY"])
        left_high, left_low = byte_tool.get_2_bytes(left_mm_sec)

        # send these bytes and set the stored velocities
        self._send_cmd([self.CMD["Drive Direct"], right_high, right_low, left_high, left_low])

    def send_drive_pwm(self, right_pwm, left_pwm):
        """
        send drive pwm command

        This command lets you control the forward and backward motion of Roomba’s drive wheels independently.
        It takes four data bytes, which are interpreted as two 16-bit signed values using two’s complement.
        A positive PWM makes that wheel drive forward, while a negative PWM makes it drive backward.

        - Available in modes: Safe or Full

        :param int right_pwm: Right wheel PWM (-255 – 255)

        :param int left_pwm: Left wheel PWM (-255 - 255)

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.send_drive_pwm(-200, -200) # move backward
            >>> sleep(2.0) # keep 2 sec
        """
        right_pwm = self._adjust_min_max(right_pwm, -255, 255)
        right_high, right_low = byte_tool.get_2_bytes(right_pwm)

        left_pwm = self._adjust_min_max(left_pwm, -255, 255)
        left_high, left_low = byte_tool.get_2_bytes(left_pwm)

        # send these bytes and set the stored velocities
        self._send_cmd([self.CMD["Drive PWM"], right_high, right_low, left_high, left_low])

    def send_moters_cmd(self, main_brush_on, main_brush_direction_is_ccw,
                        side_brush_on, side_brush_direction_is_inward,
                        vacuum_on
                        ):
        """
        send moters command

        This command controls the motion of Roomba’s main brush, side brush, and vacuum independently.
        Motor velocity cannot be controlled with this command, all motors will run at maximum speed when enabled.
        The main brush and side brush can be run in either direction. The vacuum only runs forward.

        :param bool main_brush_on: main brush on or off

        :param bool main_brush_direction_is_ccw: main brush direction, clockwise or counter-clockwise(default)

        :param bool side_brush_on: side brush on or off

        :param bool side_brush_direction_is_inward: side brush direction, inward(default) or outward

        :param bool side_brush_on: side brush on or off

        :param bool vacuum_on: vacuum on or off

        Examples:
            >>> PORT = "/dev/ttyUSB0"
            >>> adapter = PyRoombaAdapter(PORT)
            >>> adapter.send_moters_cmd(False, True, True, True, False) # side brush is on, and it rotates inward
            >>> sleep(2.0) # keep 2 sec
        """
        cmd = 0  # All initial bit is 0
        if side_brush_on:
            cmd |= 0b00000001
        if vacuum_on:
            cmd |= 0b00000010
        if main_brush_on:
            cmd |= 0b00000100
        if not side_brush_direction_is_inward:
            cmd |= 0b00001000
        if not main_brush_direction_is_ccw:
            cmd |= 0b00010000

        self._send_cmd([self.CMD["Moters"], cmd])

    def send_pwm_moters(self, main_brush_pwm, side_brush_pwm, vacuum_pwm):
        """
        send pwm moters

        This command control the speed of Roomba’s main brush, side brush, and vacuum independently.
        With each data byte, you specify the duty cycle for the low side driver (max 128).
        For example, if you want to control a motor with 25% of battery voltage, choose a duty cycle of 128 * 25% = 32.
        The main brush and side brush can be run in either direction.
        The vacuum only runs forward. Positive speeds turn the motor in its default (cleaning) direction.
        Default direction for the side brush is counterclockwise.
        Default direction for the main brush/flapper is inward.

        - Available in modes: Safe or Full

        :param int main_brush_pwm: main brush PWM (-127 - 127)

        :param int side_brush_pwm: side brush PWM (-127 - 127)

        :param int vacuum_pwm: vacuum duty cycle (0 - 127)

        Examples:
            >>> adapter = PyRoombaAdapter("/dev/ttyUSB0")
            >>> adapter.send_pwm_moters(-55, 0, 0) # main brush is 55% PWM to opposite direction
            >>> sleep(2.0) # keep 2 sec
        """
        main_brush_pwm = self._get_1_bytes(self._adjust_min_max(main_brush_pwm, -127, 127))
        side_brush_pwm = self._get_1_bytes(self._adjust_min_max(side_brush_pwm, -127, 127))
        vacuum_pwm = self._adjust_min_max(vacuum_pwm, 0, 127)
        self._send_cmd([self.CMD["PWM Moters"], main_brush_pwm, side_brush_pwm, vacuum_pwm])

    def send_buttons_cmd(self, clean=False, spot=False, dock=False,
                         minute=False, hour=False, day=False, schedule=False, clock=False):
        """
        send buttons command

        This command lets you push Roomba’s buttons. The buttons will automatically release after 1/6th of a second.

        Note:
            - This API doesn't work on Roomba 690 Model

        :param bool clean: clean button on
        :param bool spot: spot button on
        :param bool dock: dock button on
        :param bool minute: minute button on
        :param bool hour: hour button on
        :param bool day: day button on
        :param bool schedule: schedule button on
        :param bool clock: clock button on
        """
        buttons = 0  # All initial bit is 0
        if clean:
            buttons |= 0b00000001
        if spot:
            buttons |= 0b00000010
        if dock:
            buttons |= 0b00000100
        if minute:
            buttons |= 0b00001000
        if hour:
            buttons |= 0b00010000
        if day:
            buttons |= 0b00100000
        if schedule:
            buttons |= 0b01000000
        if clock:
            buttons |= 0b10000000

        self._send_cmd([self.CMD["Buttons"], buttons])

    def send_song_cmd(self, song_number, song_length, note_number_list, note_duration_list):
        """
        Send song command

        This command lets you specify up to four songs to the OI that you can play at a later time.
        Each song is associated with a song number.
        The Play command uses the song number to identify your song selection.
        Each song can contain up to sixteen notes.
        Each note is associated with a note number that uses MIDI note definitions and a duration
        that is specified in fractions of a second.
        The number of data bytes varies, depending on the length of the song specified.
        A one note song is specified by four data bytes.
        For each additional note within a song, add two data bytes.

        - Available in modes: Passive, Safe, or Full

        :param int song_number: (0-4) The song number associated with the specific song.
                        If you send a second Song command, using the same song number, the old song is overwritten.

        :param int song_length: (1-16) The length of the song, according to the number of musical notes within the song.

        :param list note_number_list: Note Number (31 – 127) The pitch of the musical note Roomba will play,
                                      according to the MIDI note numbering scheme.
                                      The lowest musical note that Roomba will play is Note #31.
                                      Roomba considers all musical notes outside the range of 31 – 127 as rest notes,
                                      and will make no sound during the duration of those notes.

        :param list note_duration_list: Note Duration (0 – 255) The duration of a musical note,
                                        in increments of 1/64th of a second. Example: a half-second long
                                        musical note has a duration value of 32.

        Examples:
            >>> adapter = PyRoombaAdapter("/dev/ttyUSB0")
            >>> # note names
            >>> f4 = 65
            >>> a4 = 69
            >>> c5 = 72
            >>> # note lengths
            >>> MEASURE = 160
            >>> HALF = int(MEASURE / 2)
            >>> Q = int(MEASURE / 4)
            >>> Ed = int(MEASURE * 3 / 16)
            >>> S = int(MEASURE / 16)
            >>> adapter.send_song_cmd(0, 9,
            >>>             [a4, a4, a4, f4, c5, a4, f4, c5, a4],
            >>>             [Q, Q, Q, Ed, S, Q, Ed, S, HALF]) # set song
            >>> adapter.send_play_cmd(0) # play song
            >>> sleep(10.0) # keep playing
        """
        cmd = [self.CMD["Song"], song_number, song_length]
        for (note_number, note_duration) in zip(note_number_list, note_duration_list):
            cmd.append(note_number)
            cmd.append(note_duration)
        self._send_cmd(cmd)

    def send_play_cmd(self, song_number):
        """
        send play command

        This command lets you select a song to play from the songs added to Roomba using the Song command.
        You must add one or more songs to Roomba using the Song command in order for the Play command to work.

        :param int song_number: (0-4)

        Examples:
            >>> adapter = PyRoombaAdapter("/dev/ttyUSB0")
            >>> # note names
            >>> f4 = 65
            >>> a4 = 69
            >>> c5 = 72
            >>> # note lengths
            >>> MEASURE = 160
            >>> HALF = int(MEASURE / 2)
            >>> Q = int(MEASURE / 4)
            >>> Ed = int(MEASURE * 3 / 16)
            >>> S = int(MEASURE / 16)
            >>> adapter.send_song_cmd(0, 9,
            >>>             [a4, a4, a4, f4, c5, a4, f4, c5, a4],
            >>>             [Q, Q, Q, Ed, S, Q, Ed, S, HALF]) # set song
            >>> adapter.send_play_cmd(0) # play song
            >>> sleep(10.0) # keep playing
        """
        self._send_cmd([self.CMD["Play"], song_number])

    def get_encoder_counts(self):
        enc_count = self.request_data(['Left Encoder Counts', 'Right Encoder Counts'])
        return enc_count

    @staticmethod
    def _connect_serial(port, bau_rate, time_out):
        serial_con = serial.Serial(port, baudrate=bau_rate, timeout=time_out)
        if serial_con.isOpen():
            print('Serial port is open, presumably to a roomba...')
        else:
            print('Serial port did NOT open')
        return serial_con

    @staticmethod
    def _adjust_min_max(val, min_val, max_val):

        # integer cast
        if type(val) != int:
            val = int(val)

        if val < min_val:
            val = min_val
        elif val > max_val:
            val = max_val

        return val

    @staticmethod
    def _get_2_bytes(value):
        """ returns two bytes (ints) in high, low order
        whose bits form the input value when interpreted in
        two's complement
        """
        # if positive or zero, it's OK
        if value >= 0:
            eqBitVal = value
        # if it's negative, I think it is this
        else:
            eqBitVal = (1 << 16) + value

        return (eqBitVal >> 8) & 0xFF, eqBitVal & 0xFF

    @staticmethod
    def _get_1_bytes(value):
        """ returns one bytes (int)
        """
        # if positive or zero, it's OK
        if value >= 0:
            eqBitVal = value
        # if it's negative, I think it is this
        else:
            eqBitVal = (1 << 8) + value
        return eqBitVal & 0xFF

    @staticmethod
    def _get_value_from_bytes(byte):
        value = int.from_bytes(byte, "big")
        return value

    def _send_cmd(self, cmd):
        if type(cmd) == list:  # command list
            self.serial_con.write(bytes(cmd))
        else:  # one command
            self.serial_con.write(bytes([cmd]))

def main():
    PORT = "/dev/ttyUSB0"
    adapter = PyRoombaAdapter(PORT)
    # adapter.send_drive_cmd(-100, -1000)
    # adapter.send_drive_direct(-100, 100)
    import numpy as np
    # adapter.move(0.1, np.deg2rad(-10))
    # adapter.send_drive_pwm(80, 80)
    # adapter.send_drive_pwm(-200, -200)
    # adapter.send_moters_cmd(False, True, True, True, False)
    # adapter.send_pwm_moters(-55, -25, 25)
    # adapter.send_buttons_cmd(dock=True)
    # sleep(1.0)

    adapter.move(0.2, np.deg2rad(0.0))
    sleep(1.0)
    adapter.move(0, np.deg2rad(-20))
    sleep(6.0)
    adapter.move(0.2, np.deg2rad(0.0))
    sleep(1.0)
    adapter.move(0, np.deg2rad(20))
    sleep(6.0)

    # adapter.move(-0.1, 0)
    # sleep(1.0)

if __name__ == '__main__':
    main()

max_stars_repo_name: Jumpei-Arima/PyRoombaAdapter

max_issues_repo_name: Jumpei-Arima/PyRoombaAdapter

max_forks_repo_name: Jumpei-Arima/PyRoombaAdapter


text:
import os, sys
import numpy as np
import random
import copy
import torch
import torch.autograd as autograd
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data.sampler import SubsetRandomSampler

from config import *
from models import *
from utils import *

from ops import exp_mov_avg

IMG_DIM = 784
NUM_CLASSES = 10
CLIP_BOUND = 1.
SENSITIVITY = 2.
DATA_ROOT = './../data'

##########################################################
### hook functions
##########################################################
def master_hook_adder(module, grad_input, grad_output):
    '''
    global hook

    :param module:
    :param grad_input:
    :param grad_output:
    :return:
    '''
    global dynamic_hook_function
    return dynamic_hook_function(module, grad_input, grad_output)

def dummy_hook(module, grad_input, grad_output):
    '''
    dummy hook

    :param module:
    :param grad_input:
    :param grad_output:
    :return:
    '''
    pass

def modify_gradnorm_conv_hook(module, grad_input, grad_output):
    '''
    gradient modification hook

    :param module:
    :param grad_input:
    :param grad_output:
    :return:
    '''
    ### get grad wrt. input (image)
    grad_wrt_image = grad_input[0]
    grad_input_shape = grad_wrt_image.size()
    batchsize = grad_input_shape[0]
    clip_bound_ = CLIP_BOUND / batchsize  # account for the 'sum' operation in GP

    grad_wrt_image = grad_wrt_image.view(batchsize, -1)
    grad_input_norm = torch.norm(grad_wrt_image, p=2, dim=1)

    ### clip
    clip_coef = clip_bound_ / (grad_input_norm + 1e-10)
    clip_coef = clip_coef.unsqueeze(-1)
    grad_wrt_image = clip_coef * grad_wrt_image
    grad_input_new = [grad_wrt_image.view(grad_input_shape)]
    for i in range(len(grad_input)-1):
        grad_input_new.append(grad_input[i+1])
    return tuple(grad_input_new)

def dp_conv_hook(module, grad_input, grad_output):
    '''
    gradient modification + noise hook

    :param module:
    :param grad_input:
    :param grad_output:
    :return:
    '''
    global noise_multiplier
    ### get grad wrt. input (image)
    grad_wrt_image = grad_input[0]
    grad_input_shape = grad_wrt_image.size()
    batchsize = grad_input_shape[0]
    clip_bound_ = CLIP_BOUND / batchsize

    grad_wrt_image = grad_wrt_image.view(batchsize, -1)
    grad_input_norm = torch.norm(grad_wrt_image, p=2, dim=1)

    ### clip
    clip_coef = clip_bound_ / (grad_input_norm + 1e-10)
    clip_coef = torch.min(clip_coef, torch.ones_like(clip_coef))
    clip_coef = clip_coef.unsqueeze(-1)
    grad_wrt_image = clip_coef * grad_wrt_image

    ### add noise
    noise = clip_bound_ * noise_multiplier * SENSITIVITY * torch.randn_like(grad_wrt_image)
    grad_wrt_image = grad_wrt_image + noise
    grad_input_new = [grad_wrt_image.view(grad_input_shape)]
    for i in range(len(grad_input)-1):
        grad_input_new.append(grad_input[i+1])
    return tuple(grad_input_new)

##########################################################
### main
##########################################################
def main(args):
    ### config
    global noise_multiplier
    dataset = args.dataset
    num_discriminators = args.num_discriminators
    noise_multiplier = args.noise_multiplier
    z_dim = args.z_dim
    model_dim = args.model_dim
    batchsize = args.batchsize
    L_gp = args.L_gp
    L_epsilon = args.L_epsilon
    critic_iters = args.critic_iters
    latent_type = args.latent_type
    load_dir = args.load_dir
    save_dir = args.save_dir
    if_dp = (noise_multiplier > 0.)
    gen_arch = args.gen_arch
    num_gpus = args.num_gpus

    ### CUDA
    use_cuda = torch.cuda.is_available()
    devices = [torch.device("cuda:%d" % i if use_cuda else "cpu") for i in range(num_gpus)]
    device0 = devices[0]
    if use_cuda:
        torch.set_default_tensor_type('torch.cuda.FloatTensor')

    ### Random seed
    random.seed(args.random_seed)
    np.random.seed(args.random_seed)
    torch.manual_seed(args.random_seed)

    ### Fix noise for visualization
    if latent_type == 'normal':
        fix_noise = torch.randn(10, z_dim)
    elif latent_type == 'bernoulli':
        p = 0.5
        bernoulli = torch.distributions.Bernoulli(torch.tensor([p]))
        fix_noise = bernoulli.sample((10, z_dim)).view(10, z_dim)
    else:
        raise NotImplementedError

    ### Set up models
    if gen_arch == 'DCGAN':
        netG = GeneratorDCGAN(z_dim=z_dim, model_dim=model_dim, num_classes=10)
    elif gen_arch == 'ResNet':
        netG = GeneratorResNet(z_dim=z_dim, model_dim=model_dim, num_classes=10)
    else:
        raise ValueError

    netGS = copy.deepcopy(netG)
    netD_list = []
    for i in range(num_discriminators):
        netD = DiscriminatorDCGAN()
        netD_list.append(netD)

    ### Load pre-trained discriminators
    if load_dir is not None:
        for netD_id in range(num_discriminators):
            print('Load NetD ', str(netD_id))
            network_path = os.path.join(load_dir, 'netD_%d' % netD_id, 'netD.pth')
            netD = netD_list[netD_id]
            netD.load_state_dict(torch.load(network_path))

    netG = netG.to(device0)
    for netD_id, netD in enumerate(netD_list):
        device = devices[get_device_id(netD_id, num_discriminators, num_gpus)]
        netD.to(device)

    ### Set up optimizers
    optimizerD_list = []
    for i in range(num_discriminators):
        netD = netD_list[i]
        optimizerD = optim.Adam(netD.parameters(), lr=1e-4, betas=(0.5, 0.9))
        optimizerD_list.append(optimizerD)
    optimizerG = optim.Adam(netG.parameters(), lr=1e-4, betas=(0.5, 0.9))

    ### Data loaders
    transform_train = transforms.ToTensor()
    if dataset == 'mnist':
        dataloader = datasets.MNIST
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'MNIST'), train=True, download=True,
                              transform=transform_train)
    elif dataset == 'fashionmnist':
        dataloader = datasets.FashionMNIST
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'FashionMNIST'), train=True, download=True,
                              transform=transform_train)
    else:
        raise NotImplementedError

    if load_dir is not None:
        assert os.path.exists(os.path.join(load_dir, 'indices.npy'))
        print('load indices from disk')
        indices_full = np.load(os.path.join(load_dir, 'indices.npy'), allow_pickle=True)
    else:
        print('creat indices file')
        indices_full = np.arange(len(trainset))
        np.random.shuffle(indices_full)
        indices_full.dump(os.path.join(save_dir, 'indices.npy'))
    trainset_size = int(len(trainset) / num_discriminators)
    print('Size of the dataset: ', trainset_size)

    input_pipelines = []
    for i in range(num_discriminators):
        start = i * trainset_size
        end = (i + 1) * trainset_size
        indices = indices_full[start:end]
        trainloader = data.DataLoader(trainset, batch_size=args.batchsize, drop_last=False,
                                      num_workers=args.num_workers, sampler=SubsetRandomSampler(indices))
        input_data = inf_train_gen(trainloader)
        input_pipelines.append(input_data)

    ### Register hook
    global dynamic_hook_function
    for netD in netD_list:
        netD.conv1.register_backward_hook(master_hook_adder)

    for iter in range(args.iterations + 1):
        #########################
        ### Update D network
        #########################
        netD_id = np.random.randint(num_discriminators, size=1)[0]
        device = devices[get_device_id(netD_id, num_discriminators, num_gpus)]
        netD = netD_list[netD_id]
        optimizerD = optimizerD_list[netD_id]
        input_data = input_pipelines[netD_id]

        for p in netD.parameters():
            p.requires_grad = True

        for iter_d in range(critic_iters):
            real_data, real_y = next(input_data)
            real_data = real_data.view(-1, IMG_DIM)
            real_data = real_data.to(device)
            real_y = real_y.to(device)
            real_data_v = autograd.Variable(real_data)

            ### train with real
            dynamic_hook_function = dummy_hook
            netD.zero_grad()
            D_real_score = netD(real_data_v, real_y)
            D_real = -D_real_score.mean()

            ### train with fake
            batchsize = real_data.shape[0]
            if latent_type == 'normal':
                noise = torch.randn(batchsize, z_dim).to(device0)
            elif latent_type == 'bernoulli':
                noise = bernoulli.sample((batchsize, z_dim)).view(batchsize, z_dim).to(device0)
            else:
                raise NotImplementedError
            noisev = autograd.Variable(noise)
            fake = autograd.Variable(netG(noisev, real_y.to(device0)).data)
            inputv = fake.to(device)
            D_fake = netD(inputv, real_y.to(device))
            D_fake = D_fake.mean()

            ### train with gradient penalty
            gradient_penalty = netD.calc_gradient_penalty(real_data_v.data, fake.data, real_y, L_gp, device)
            D_cost = D_fake + D_real + gradient_penalty

            ### train with epsilon penalty
            logit_cost = L_epsilon * torch.pow(D_real_score, 2).mean()
            D_cost += logit_cost

            ### update
            D_cost.backward()
            Wasserstein_D = -D_real - D_fake
            optimizerD.step()

        del real_data, real_y, fake, noise, inputv, D_real, D_fake, logit_cost, gradient_penalty
        torch.cuda.empty_cache()

        ############################
        # Update G network
        ###########################
        if if_dp:
            ### Sanitize the gradients passed to the Generator
            dynamic_hook_function = dp_conv_hook
        else:
            ### Only modify the gradient norm, without adding noise
            dynamic_hook_function = modify_gradnorm_conv_hook

        for p in netD.parameters():
            p.requires_grad = False
        netG.zero_grad()

        ### train with sanitized discriminator output
        if latent_type == 'normal':
            noise = torch.randn(batchsize, z_dim).to(device0)
        elif latent_type == 'bernoulli':
            noise = bernoulli.sample((batchsize, z_dim)).view(batchsize, z_dim).to(device0)
        else:
            raise NotImplementedError
        label = torch.randint(0, NUM_CLASSES, [batchsize]).to(device0)
        noisev = autograd.Variable(noise)
        fake = netG(noisev, label)
        fake = fake.to(device)
        label = label.to(device)
        G = netD(fake, label)
        G = - G.mean()

        ### update
        G.backward()
        G_cost = G
        optimizerG.step()

        ### update the exponential moving average
        exp_mov_avg(netGS, netG, alpha=0.999, global_step=iter)

        ############################
        ### Results visualization
        ############################
        if iter < 5 or iter % args.print_step == 0:
            print('G_cost:{}, D_cost:{}, Wasserstein:{}'.format(G_cost.cpu().data,
                                                                D_cost.cpu().data,
                                                                Wasserstein_D.cpu().data
                                                                ))

        if iter % args.vis_step == 0:
            generate_image(iter, netGS, fix_noise, save_dir, device0, num_classes=10)

        if iter % args.save_step == 0:
            ### save model
            torch.save(netGS.state_dict(), os.path.join(save_dir, 'netGS_%d.pth' % iter))

        del label, fake, noisev, noise, G, G_cost, D_cost
        torch.cuda.empty_cache()

    ### save model
    torch.save(netG.state_dict(), os.path.join(save_dir, 'netG.pth'))
    torch.save(netGS.state_dict(), os.path.join(save_dir, 'netGS.pth'))

if __name__ == '__main__':
    args = parse_arguments()
    save_config(args)
    main(args)

max_stars_repo_name: Dong-Jie-Chen/GS-WGAN

max_issues_repo_name: Dong-Jie-Chen/GS-WGAN

max_forks_repo_name: Dong-Jie-Chen/GS-WGAN


text:
import os
import sys
import scipy
import logging
import numpy
import tempfile
import shutil
import numpy
import subprocess
import random

from scipy.io import savemat, loadmat
from PIL import Image

from aletheialib import utils
from aletheialib.octave_interface import _embed

import multiprocessing
from multiprocessing.dummy import Pool as ThreadPool
from multiprocessing import cpu_count

from imageio import imread, imwrite

# {{{ embed_message()
def embed_message(embed_fn, path, payload, output_dir,
                  embed_fn_saving=False):

    path=utils.absolute_path(path)

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    output_dir=utils.absolute_path(output_dir)

    # Read filenames
    files=[]
    if os.path.isdir(path):
        for dirpath,_,filenames in os.walk(path):
            for f in filenames:
                path=os.path.abspath(os.path.join(dirpath, f))
                if not utils.is_valid_image(path):
                    print("Warning, please provide a valid image: ", f)
                else:
                    files.append(path)
    else:
        files=[path]

    # remove fileas already generated in a previous execution
    filtered_files = []
    for f in files:
        basename=os.path.basename(f)
        dst_path=os.path.join(output_dir, basename)
        if os.path.exists(dst_path):
            print("Warning! file already exists, ignored:", dst_path)
            continue
        filtered_files.append(f)
    files = filtered_files
    del filtered_files

    def embed(path):
        basename=os.path.basename(path)
        dst_path=os.path.join(output_dir, basename)

        if embed_fn_saving:
            embed_fn(path, payload, dst_path)
        else:
            X=embed_fn(path, payload)
            try:
                imwrite(dst_path, X.astype('uint8'))
            except Exception as e:
                print(str(e))

    # Process thread pool in batches
    batch=1000
    for i in range(0, len(files), batch):
        files_batch = files[i:i+batch]
        n_core=cpu_count()
        print("Using", n_core, "threads")
        pool = ThreadPool(n_core)
        results = pool.map(embed, files_batch)
        pool.close()
        pool.terminate()
        pool.join()

# }}}

def wow(path, payload):
    return _embed('wow', path, payload)

def s_uniward(path, payload):
    return _embed('s_uniward', path, payload)

def j_uniward(path, payload, dst_path):
    return _embed('j_uniward', path, payload, dst_path)

def j_uniward_color(path, payload, dst_path):
    return _embed('j_uniward_color', path, payload, dst_path)

def hugo(path, payload):
    return _embed('hugo', path, payload)

def hill(path, payload):
    return _embed('hill', path, payload)

def ebs(path, payload, dst_path):
    return _embed('ebs', path, payload, dst_path)

def ebs_color(path, payload, dst_path):
    return _embed('ebs_color', path, payload, dst_path)

def ued(path, payload, dst_path):
    return _embed('ued', path, payload, dst_path)

def ued_color(path, payload, dst_path):
    return _embed('ued_color', path, payload, dst_path)

def nsf5(path, payload, dst_path):
    return _embed('nsf5', path, payload, dst_path)

def nsf5_color(path, payload, dst_path):
    return _embed('nsf5_color', path, payload, dst_path)

def experimental(path, payload):
    return _embed('experimental', path, payload)

def custom(path, command, dst_path):
    bn = os.path.basename(path)
    shutil.copyfile(path, dst_path)
    cmd = command.replace("<IMAGE>", dst_path)
    FNUL = open(os.devnull, 'w')
    p=subprocess.Popen(cmd, stdout=FNUL, stderr=FNUL, shell=True)
    #output, err = p.communicate()
    status = p.wait()

# {{{ lsbm()
def lsbm(path, payload):
    X = imread(path)
    sign=[1, -1]
    for j in range(X.shape[0]):
        for i in range(X.shape[1]):
            if random.randint(0,99)>int(float(payload)*100):
                continue

            if len(X.shape)==2:
                k=sign[random.randint(0, 1)]
                if X[i, j]==0: k=1
                if X[i, j]==255: k=-1
                if X[i, j]%2!=random.randint(0,1): # message
                    X[i, j]+=k
            else:
                kr=sign[random.randint(0, 1)]
                kg=sign[random.randint(0, 1)]
                kb=sign[random.randint(0, 1)]
                if X[i, j][0]==0: kr=1
                if X[i, j][1]==0: kg=1
                if X[i, j][2]==0: kb=1
                if X[i, j][0]==255: kr=-1
                if X[i, j][1]==255: kg=-1
                if X[i, j][2]==255: kb=-1
                # message
                if X[i, j][0]%2==random.randint(0,1): kr=0
                if X[i, j][1]%2==random.randint(0,1): kg=0
                if X[i, j][2]%2==random.randint(0,1): kb=0
                X[i, j]=(X[i,j][0]+kr, X[i,j][1]+kg, X[i,j][2]+kb)
    return X
# }}}

# {{{ lsbr()
def lsbr(path, payload):
    X = imread(path)
    sign=[1, -1]
    for j in range(X.shape[0]):
        for i in range(X.shape[1]):
            if random.randint(0,99)>int(float(payload)*100):
                continue

            if len(X.shape)==2:
                k=sign[random.randint(0, 1)]
                if X[i, j]==0: k=1
                if X[i, j]==255: k=-1
                if X[i, j]%2!=random.randint(0,1): # message
                    if X[i, j]%2==0: X[i, j]+=1
                    else: X[i, j]-=1
            else:
                # message
                kr=0; Kg=0; kb=0

                if X[i, j][0]%2==0: kr=1
                else: kr=-1

                if X[i, j][1]%2==0: kg=1
                else: kg=-1

                if X[i, j][2]%2==0: kb=1
                else: kb=-1

                if X[i, j][0]%2==random.randint(0,1): kr=0
                if X[i, j][1]%2==random.randint(0,1): kg=0
                if X[i, j][2]%2==random.randint(0,1): kb=0
                X[i, j]=(X[i,j][0]+kr, X[i,j][1]+kg, X[i,j][2]+kb)
    return X
# }}}

# {{{ embedding_fn()
def embedding_fn(name):
    if name=="lsbm-sim":
        return lsbm
    if name=="lsbr-sim":
        return lsbr
    if name=="hugo-sim":
        return hugo
    if name=="wow-sim":
        return wow
    if name=="s-uniward-sim":
        return s_uniward
    if name=="j-uniward-sim":
        return j_uniward
    if name=="j-uniward-color-sim":
        return j_uniward_color
    if name=="hill-sim":
        return hill
    if name=="nsf5-sim":
        return nsf5
    if name=="nsf5-color-sim":
        return nsf5_color
    if name=="ebs-sim":
        return ebs
    if name=="ebs-color-sim":
        return ebs_color
    if name=="ued-sim":
        return ued
    if name=="ueb-color-sim":
        return ued_color

    print("Unknown simulator:", name)
    sys.exit(0)
# }}}

max_stars_repo_name: deepsteg/deepsteg

max_issues_repo_name: deepsteg/deepsteg

max_forks_repo_name: deepsteg/deepsteg

reason: import numpy,import scipy,from scipy


text:
import numpy as np
import json
import os

def build_imdb(image_set):
    print('building imdb %s' % image_set)
    question_file = '../clevr_dataset/questions/CLEVR_%s_questions.json'
    with open(question_file % image_set) as f:
        questions = json.load(f)['questions']
    imdb = []
    for n_q, q in enumerate(questions):
        if (n_q+1) % 10000 == 0:
            print('processing %d / %d' % (n_q+1, len(questions)))
        questionId = '%s_%s' % (image_set, q['question_index'])
        imageId = '%s_%s' % (image_set, q['image_index'])
        question = q['question']
        image_name = q['image_filename']
        iminfo = dict(questionId=questionId,
                      imageId=imageId,
                      question=question,
                      image_name=image_name)
        if 'answer' in q:
            iminfo['answer'] = q['answer']

        imdb.append(iminfo)
    return imdb

imdb_trn = build_imdb('train')
imdb_val = build_imdb('val')
imdb_tst = build_imdb('test')

os.makedirs('./imdb', exist_ok=True)
np.save('./imdb/imdb_train.npy', np.array(imdb_trn))
np.save('./imdb/imdb_val.npy', np.array(imdb_val))
np.save('./imdb/imdb_test.npy', np.array(imdb_tst))

max_stars_repo_name: geetickachauhan/lcgn

max_issues_repo_name: geetickachauhan/lcgn

max_forks_repo_name: geetickachauhan/lcgn


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 28 21:45:11 2016

@author: kevin
"""
#%%

from __future__ import division
from numpy import *
from sympy import *
import matplotlib
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)

expr = ( x + y ) ** 3
expr
expr.expand()

#%%

import os

print os.getcwd()
print os.listdir('data')

#%%
def average(a, b):
    """
    Given two numbers a and b, return their average value.

    Parameters
    ----------
    a : number
      A number
    b : number
      Another number

    Returns
    -------
    res : number
      The average of a and b, computed using 0.5*(a + b)

    Example
    -------
    >>> average(5, 10)
    7.5

    """

    return (a + b) * 0.5

#%%
import pylab
pylab.plot(range(10), 'o')

max_stars_repo_name: kevyin/sitta

max_issues_repo_name: kevyin/sitta

max_forks_repo_name: kevyin/sitta

reason: from numpy,from sympy


text:
#!/usr/bin/python2.7
import re
import traceback
import numpy as np

def textParser(text):
    """
    对SMS预处理，去除空字符串，并统一小写
    :param text:
    :return:
    """
    regEx = re.compile(r'[^a-zA-Z]|\d')  # 匹配非字母或者数字，即去掉非字母非数字，只留下单词
    words = regEx.split(text)
    # 去除空字符串，并统一小写
    words = [word.lower() for word in words if len(word) > 0]
    return words

def loadSMSData(fileName):
    """
    加载SMS数据
    :param fileName:
    :return:
    """
    f = open(fileName)
    classCategory = []  # 类别标签，1表示是垃圾SMS，0表示正常SMS
    moodWords = []
    for line in f.readlines():
        linedatas = line.strip().split('\t')
        if linedatas[0] == 'ham':
            classCategory.append(0)
        elif linedatas[0] == 'Pos':
            classCategory.append(1)
        # 切分文本
        words = textParser(linedatas[1])
        moodWords.append(words)
    return moodWords, classCategory

def createVocabularyList(moodWords):
    """
    创建语料库
    :param moodWords:
    :return:
    """
    vocabularySet = set([])
    for words in moodWords:
        vocabularySet = vocabularySet | set(words)
    vocabularyList = list(vocabularySet)
    return vocabularyList

def getVocabularyList(fileName):
    """
    从词汇列表文件中获取语料库
    :param fileName:
    :return:
    """
    fr = open(fileName)
    vocabularyList = fr.readline().strip().split('\t')
    fr.close()
    return vocabularyList

def setOfWordsToVecTor(vocabularyList, moodWords):
    """
    SMS内容匹配预料库，标记预料库的词汇出现的次数
    :param vocabularyList:
    :param moodWords:
    :return:
    """
    vocabMarked = [0] * len(vocabularyList)
    for smsWord in moodWords:
        if smsWord in vocabularyList:
            vocabMarked[vocabularyList.index(smsWord)] += 1
    return np.array(vocabMarked)

def setOfWordsListToVecTor(vocabularyList, moodWordsList):
    """
    将文本数据的二维数组标记
    :param vocabularyList:
    :param moodWordsList:
    :return:
    """
    vocabMarkedList = []
    for i in range(len(moodWordsList)):
        vocabMarked = setOfWordsToVecTor(vocabularyList, moodWordsList[i])
        vocabMarkedList.append(vocabMarked)
    return vocabMarkedList

def trainingNaiveBayes(trainMarkedWords, trainCategory):
    """
    训练数据集中获取语料库中词汇的
    Pos：P（Wi|Pos总词数）
    Neg：P（Wi|Neg总词数）
    Neutral：P（Wi|Neutral总词数）
    """
    numTrainDoc = len(trainMarkedWords)
    numWords = len(trainMarkedWords[0])
    # 是积极的先验概率P(S)
    pPos, pNeg, pNeutral = 0.0, 0.0, 0.0
    for i in trainCategory:
        if i == 1:
            pPos = pPos + 1
        elif i == 2:
            pNeg = pNeg + 1
        else:
            pNeutral = pNeutral + 1
    pPos = pPos / float(numTrainDoc)
    pNeg = pNeg / float(numTrainDoc)
    pNeutral = pNeutral / float(numTrainDoc)
    wordsInPosNum = np.ones(numWords)
    wordsInNegNum = np.ones(numWords)
    wordsInNeutralNum = np.ones(numWords)
    PosWordsNum = 2.0
    NegWordsNum = 2.0
    NeutralWordsNum = 2.0
    for i in range(0, numTrainDoc):
        try:
            if trainCategory[i] == 1:  # 如果是垃圾SMS或邮件
                wordsInPosNum += trainMarkedWords[i]
                PosWordsNum += sum(trainMarkedWords[i])  # 统计Pos中语料库中词汇出现的总次数
            elif trainCategory[i] == 2:
                wordsInNegNum += trainMarkedWords[i]
                NegWordsNum += sum(trainMarkedWords[i])
            else:
                wordsInNeutralNum += trainMarkedWords[i]
                NeutralWordsNum += sum(trainMarkedWords[i])
        except Exception as e:
            traceback.print_exc(e)
    pWordsPosicity = np.log(wordsInPosNum / PosWordsNum)
    pWordsNegy = np.log(wordsInNegNum / NegWordsNum)
    pWordsNeutral = np.log(wordsInNeutralNum / NeutralWordsNum)
    return pWordsPosicity, pWordsNegy, pWordsNeutral, pPos, pNeg, pNeutral

def getTrainedModelInfo():
    """
    获取训练的模型信息
    :return:
    """
    # 加载训练获取的语料库信息
    vocabularyList = getVocabularyList('vocabularyList.txt')
    pWordsNegy = np.loadtxt('pWordsNegy.txt', delimiter='\t')
    pWordsPosicity = np.loadtxt('pWordsPosicity.txt', delimiter='\t')
    fr = open('pPos.txt')
    pPos = float(fr.readline().strip())
    fr.close()

    return vocabularyList, pWordsPosicity, pWordsNegy, pPos

def classify(pWordsPosicity, pWordsNegy, pWordsNeutral, DS, pPos, pNeg, pNeutral, testWordsMarkedArray, DS_neg,
             DS_neutral):
    """
    计算联合概率进行分类
    :param DS:  adaboost算法额外增加的权重系数
    np.log(x)返回x的自然对数    即：ln(x)
    """
    # 计算P(Ci|W)，W为向量。P(Ci|W)只需计算P(W|Ci)P(Ci)
    pP = sum(testWordsMarkedArray * pWordsPosicity * DS) + np.log(pPos)
    pN = sum(testWordsMarkedArray * pWordsNegy * DS_neg) + np.log(pNeg)
    pNeu = sum(testWordsMarkedArray * pWordsNeutral * DS_neutral) + np.log(pNeutral)

    if pP > pN > pNeu or pP > pNeu > pN:
        return pP, pN, pNeu, 1
    elif pN > pP > pNeu or pN > pNeu > pP:
        return pP, pN, pNeu, 2
    else:
        return pP, pN, pNeu, 3

def classify_two(pWordsSpamicity, pWordsHealthy, DS, pSpam, testWordsMarkedArray):
    ps = sum(testWordsMarkedArray * pWordsSpamicity * DS) + np.log(pSpam)
    ph = sum(testWordsMarkedArray * pWordsHealthy) + np.log(1 - pSpam)
    if ps > ph:
        return ps, ph, 1
    else:
        return ps, ph, 0


text:
import gym
import random
import numpy as np
from .utils import rgb2gray, imresize

class Environment(object):
  def __init__(self, config):
    self.env = gym.make(config.env_name)

    screen_width, screen_height, self.action_repeat, self.random_start = \
        config.screen_width, config.screen_height, config.action_repeat, config.random_start

    self.display = config.display
    self.dims = (screen_height, screen_width)

    self._screen = None
    self._last_screen = None
    self._reward = [0]
    self._terminal = True

  def new_game(self, from_random_game=False):
    self._screen = self.env.reset()
    while not self._screen or self._screen[0] is None:
        ret = self._step(0)
        if ret:
            self._screen, _, _ = ret

    self._step(0)
    self.render()
    return self.screen, 0, 0, self.terminal

  def new_random_game(self):
    self.new_game(True)
    for _ in range(random.randint(0, self.random_start - 1)):
      self._step(0)
    self.render()
    return self.screen, 0, 0, self.terminal

  def _step(self, action):
    self._screen, self._reward, self._terminal, _ = self.env.step(action)
  def _random_step(self):
    action = self.env.action_space.sample()
    self._step(action)

  @property
  def screen(self):
    if not self._screen or self._screen[0] is None:
      self._screen = self._last_screen
    screen = self._screen[0]['vision']
    return imresize(rgb2gray(screen)/255., self.dims)
    #return cv2.resize(cv2.cvtColor(self._screen, cv2.COLOR_BGR2YCR_CB)/255., self.dims)[:,:,0]
  @property
  def action_size(self):
    return self.env.action_space.n

  @property
  def terminal(self):
    return self._terminal[0]

  @property
  def reward(self):
    return self._reward[0]
  @property
  def lives(self):
    return self.env.ale.lives()

  @property
  def state(self):
    return self.screen, self.reward, self.terminal

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

  def after_act(self, action):
    self.render()

class GymEnvironment(Environment):
  def __init__(self, config):
    super(GymEnvironment, self).__init__(config)

  def act(self, action, is_training=True):
    cumulated_reward = 0
    start_lives = self.lives

    for _ in range(self.action_repeat):
      self._step(action)
      cumulated_reward = cumulated_reward + self.reward

      if is_training and start_lives > self.lives:
        cumulated_reward -= 1
        self.terminal = True

      if self.terminal:
        break

    self.reward = cumulated_reward

    self.after_act(action)
    return self.state

class SimpleGymEnvironment(Environment):
  def __init__(self, config):
    super(SimpleGymEnvironment, self).__init__(config)

  def act(self, action, is_training=True):
    self._step(action)

    self.after_act(action)
    return self.state

class UniverseEnvironment(Environment):
  def __init__(self, config):
    super(UniverseEnvironment, self).__init__(config)
#    self.env.configure(remotes='vnc://localhost:5900+15900')
    self.env.configure(remotes=1)
  @property
  def action_size(self):
    return 5

  def _step(self, action):
    act_dict = {
      0 : [('KeyEvent', 'ArrowLeft', True),
           ('KeyEvent', 'ArrowRight', False),
           ('KeyEvent', 'space', False)],
      1 : [('KeyEvent', 'ArrowRight', True),
           ('KeyEvent', 'ArrowLeft', False),
           ('KeyEvent', 'space', False)],
      2: [('KeyEvent', 'space', False),
          ('KeyEvent', 'ArrowLeft', False),
          ('KeyEvent', 'ArrowRight', False)],
      3 : [('KeyEvent', 'ArrowLeft', True),
           ('KeyEvent', 'ArrowRight', False),
           ('KeyEvent', 'space', False)],
      4 : [('KeyEvent', 'ArrowRight', True),
           ('KeyEvent', 'ArrowLeft', False),
           ('KeyEvent', 'space', False)],
    }
    self._last_screen = self._screen
    self._screen, self._reward, self._terminal, _ = self.env.step([act_dict[action]])
   # if not self._screen or self._screen[0] is None:
   #   self._terminal = [True]
   #   self._screen = [{'vision': None}]

  def _random_step(self):

    x = np.random.randint(5) + 1
    self._step(x)

  def act(self, action, is_training=True):
    self._step(action)
    return self.state

  def render(*args, **kwargs):
    pass

  @property
  def screen(self):
    if not self._screen or self._screen[0] is None:
      self._screen = self._last_screen
    screen = self._screen[0]['vision'][21:512,83:384]
    return imresize(rgb2gray(screen)/255., self.dims)

  @property
  def lives(self):
    return 1


text:
import numpy as np
from tetris import TetrisApp
from scipy.misc import imresize, imsave
import cv2
import random
from environment import BaseEnvironment, FramePool, ObservationPool
import logging

IMG_SIZE_X = 84
IMG_SIZE_Y = 84
NR_IMAGES = 4
ACTION_REPEAT = 1
MAX_START_WAIT = 30
FRAMES_IN_POOL = 1

class TetrisEmulator(BaseEnvironment):
    def __init__(self, actor_id, args):
        self.tetris = TetrisApp(emulator = True)

        self.legal_actions = [0,1,2,3,4]
        self.screen_width, self.screen_height = 288, 396
        self.lives = 1

        self.random_start = args.random_start
        self.single_life_episodes = args.single_life_episodes
        self.call_on_new_frame = args.visualize
        self.global_step = 0

        self.compteur = 0

        # Processed historcal frames that will be fed in to the network
        # (i.e., four 84x84 images)
        self.rgb = args.rgb
        self.depth = 1
        if self.rgb : self.depth = 3
        self.rgb_screen = np.zeros((self.screen_height, self.screen_width, 3), dtype=np.uint8)
        self.gray_screen = np.zeros((self.screen_height, self.screen_width,1), dtype=np.uint8)
        self.frame_pool = FramePool(np.empty((2, self.screen_height,self.screen_width, self.depth), dtype=np.uint8),
                                        self.__process_frame_pool)
        self.observation_pool = ObservationPool(np.zeros((IMG_SIZE_X, IMG_SIZE_Y, self.depth, NR_IMAGES), dtype=np.uint8), self.rgb)

    def get_legal_actions(self):
        return self.legal_actions

    def __get_screen_image(self):
        """ Get the current frame luminance. Return: the current frame """
        self.gray_screen = self.tetris.getScreen(rgb=False)
        if self.rgb :
            self.rgb_screen = self.tetris.getScreen()
        if self.call_on_new_frame:
            self.rgb_screen = self.tetris.getScreen()
            self.on_new_frame(self.rgb_screen)
        self.compteur += 1
        if self.rgb :
            return self.rgb_screen
        return self.gray_screen

    def on_new_frame(self, frame):
        pass

    def __new_game(self):
        """ Restart game """
        self.tetris.init_game()
        self.lives = 1
        if self.random_start:
            wait = random.randint(0, MAX_START_WAIT)
            for _ in range(wait):
                self.tetris.act(0)

    def __process_frame_pool(self, frame_pool):
        """ Preprocess frame pool """
        img = np.amax(frame_pool, axis=0)
        if not self.rgb :
            img = np.reshape(img, (self.screen_height, self.screen_width))
        img = imresize(img, (84, 84), interp='nearest')
        img = img.astype(np.uint8)
        if not self.rgb :
            img = np.reshape(img, (84, 84, 1))
        return img

    def __action_repeat(self, a, times=ACTION_REPEAT):
        """ Repeat action and grab screen into frame pool """
        reward = 0
        for i in range(times - FRAMES_IN_POOL):
            reward += self.tetris.act(a)
        # Only need to add the last FRAMES_IN_POOL frames to the frame pool
        for i in range(FRAMES_IN_POOL):
            reward += self.tetris.act(a)
            img = self.__get_screen_image()
            if not self.rgb :
                img = np.reshape(img, (self.screen_height, self.screen_width, 1))
            self.frame_pool.new_frame(img)
        return reward

    def get_initial_state(self):
        """ Get the initial state """
        self.__new_game()
        for step in range(NR_IMAGES):
            _ = self.__action_repeat(0)
            self.observation_pool.new_observation(self.frame_pool.get_processed_frame())
        if self.__is_terminal():
            raise Exception('This should never happen.')
        return self.observation_pool.get_pooled_observations()

    def next(self, action):
        """ Get the next state, reward, and game over signal """
        reward = self.__action_repeat(action)
        self.observation_pool.new_observation(self.frame_pool.get_processed_frame())
        terminal = self.__is_terminal()
        self.lives = 0 if terminal else 1
        observation = self.observation_pool.get_pooled_observations()
        self.global_step += 1
        return observation, reward, terminal

    def __is_terminal(self):
        return self.tetris.gameover

    def __is_over(self):
        return self.tetris.gameover

    def get_noop(self):
        return [1.0, 0.0]

max_stars_repo_name: jolibrain/manette

max_forks_repo_name: jolibrain/manette

reason: import numpy,from scipy


text:
"""
This generates a picture with 100 squares of random size, angle,
color and position.
"""

import gizeh as gz
import numpy as np

L = 200 # <- dimensions of the final picture
surface = gz.Surface(L, L, bg_color=(1,1,1))

# We generate 1000 random values of size, angle, color, position.
# 'rand' is a function that generates numbers between 0 and 1
n_squares = 1000 # number of squares
angles = 2*np.pi* np.random.rand(n_squares) # n_squares angles between 0 and 2pi
sizes = 20 + 20 * np.random.rand(n_squares) # all sizes between 20 and 40
positions = L * np.random.rand(n_squares, 2) # [ [x1, y1] [x2 y2] [x3 y3]]...
colors = np.random.rand(n_squares, 3)

for angle, size, position, color in zip(angles, sizes, positions, colors):
    square = gz.square(size, xy=position, angle=angle, fill=color,
                       stroke_width=size/20) # stroke is black by default.
    square.draw(surface)

surface.write_to_png("random_squares.png")


text:
# -*- coding: utf-8 -*-
"""
Created on Tue Nov  9 14:34:46 2021

@author: W-H
"""

import matplotlib.pyplot as plt
import numpy as np
rssi = [-63.08, -66.88, -69.10, -70.68, -71.91,
        -72.91, -73.75, -74.48, -75.13, -75.71,
        -76.23, -76.71, -77.15, -77.55, -77.93,
        -78.27]

dist = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5,
        4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0]
plt.plot(dist, rssi, color='green', marker='o',
         linestyle='dashed',  linewidth=2, markersize=12)
plt.show()

x = np.linspace(0, 2, 100)

# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
fig, ax = plt.subplots()  # Create a figure and an axes.
ax.plot(x, x, label='linear')  # Plot some data on the axes.
ax.plot(x, x**2, label='quadratic')  # Plot more data on the axes...
ax.plot(x, x**3, label='cubic')  # ... and some more.
ax.set_xlabel('x label')  # Add an x-label to the axes.
ax.set_ylabel('y label')  # Add a y-label to the axes.
ax.set_title("Simple Plot")  # Add a title to the axes.
ax.legend()  # Add a legend.


text:
from pylab import *
from numpy import *
from numpy.linalg import solve
from scipy.integrate import odeint
from scipy.stats import norm, uniform, beta
from scipy.special import jacobi

a = 0.0
b = 3.0
theta=1.0
sigma=sqrt(theta/(2*(a+b+2)))

tscale = 0.05

invariant_distribution = poly1d( [-1 for x in range(int(a))], True)*poly1d( [1 for x in range(int(b))], True)

def eigenvalue(n):
    return theta*n*(n+a+b+1)/(a+b+2)

gaussian_var = norm()
def dW(dt):
    return norm.rvs() / sqrt(dt)

def random_walk(y0, tmax, dt, times = None):
    dt = dt * tscale
    def rhs(y,t):
        return -theta*(y-(a-b)/(a+b+2)) + sqrt(2*theta*(1-y*y)/(a+b+2))*dW(dt/tscale)
    if (times is None):
        times = arange(0,tmax,dt)
    y = zeros(shape=times.shape, dtype=float)
    y[0] = y0
    for i in range(1,y.shape[0]):
        y[i] = y[i-1] + rhs(y[i-1], times[i])*dt
        if abs(y[i]) > 1:
            y[i] = y[i] / abs(y[i])
    return (times, y)

def beta_prior(s, f):
    return poly1d(ones(shape=(s,)), True)*poly1d(-1*ones(shape=(f,)), True)

def poly_to_jacobi(x):
    """x is a poly1d object"""
    xc = x.coeffs
    N = x.order+1
    matrix = zeros(shape=(N,N), dtype=float)
    for i in range(N):
        matrix[N-i-1:N, i] = jacobi(i,a,b).coeffs
    return solve(matrix, xc)

def jacobi_to_poly(x):
    result = poly1d([0])
    for i in range(x.shape[0]):
        result = result + (jacobi(i,a,b)*invariant_distribution)*x[i]
    return result

def jacobi_to_poly_no_invariant(x):
    result = poly1d([0])
    for i in range(x.shape[0]):
        result = result + jacobi(i,a,b)*x[i]
    return result

def propagate_jacobi(pc, t):
    """Takes jacobi coefficients and propagates them"""
    n = arange(pc.shape[0], dtype=float)
    l = theta*n*(n+a+b+1.0)/(a+b+2.0)*tscale
    return exp(-l*t)*pc

def truncate_unnecessary_jacobi(p):
    p_normalized = p / (abs(p).sum())
    cs = cumsum(abs(p_normalized[::-1]))[::-1]
    return p_normalized[where(abs(cs) > 1e-4)]

def pde_solve(prior, t):
    result = zeros(shape=(t.shape[0], prior.shape[0]), dtype=float)
    result[0,:] = prior
    for i in range(1,t.shape[0]):
        result[i,:] = propagate_jacobi(result[i-1,:], t[i]-t[i-1])
    return result

def transform_to_x(pdf, x):
    result = zeros(shape=(pdf.shape[0], x.shape[0]), dtype=float)
    for i in range(0, pdf.shape[0]):
        p = jacobi_to_poly(pdf[i,:])
        result[i,:] = p(x)
        result[i,:] /= result[i,:].sum()
    return result

tmax = 4
prior = beta_prior(40, 20)
prior_in_jacobi = poly_to_jacobi(prior)

dt = 0.1
times = arange(0,tmax,dt)
x = arange(-1,1,0.01)

rw_dt = 0.01
t, y = random_walk(0.35*2-1, tmax, rw_dt)

solution_as_x = zeros(shape=(times.size, x.size), dtype=float)
solution_as_jacobi = None
empirical_ctr = zeros(shape=(4,), dtype=float)
for i in range(0,4):
    nt = int(1.0/dt)
    prior = prior_in_jacobi
    rnd = uniform(0,1)
    if (i > 0):
        nsamples = 40
        r = rnd.rvs(nsamples)
        ctr = (y[i/rw_dt]+1)/2.0
        print "CTR: " + str(ctr)
        success = (r < ctr).sum()
        print "Empirical: " + str(success / float(nsamples))
        evidence = beta_prior( nsamples - success, success)
        prior = None
        j = truncate_unnecessary_jacobi(solution_as_jacobi[int(1/dt)-1])
        prior = poly_to_jacobi(evidence * jacobi_to_poly_no_invariant(j))
        empirical_ctr[i] = success / float(nsamples)

    solution_as_jacobi = pde_solve(prior, times[i*nt:(i+1)*nt])

    solution_as_x[i*nt:(i+1)*nt] = transform_to_x(solution_as_jacobi, x)

plot(arange(0,4), empirical_ctr, 'go')
plot(t, (y+1)/2.0, 'k')

imshow(solution_as_x.transpose(), origin='lower', extent=[0,tmax,0,1])
xlabel("time")
ylabel("CTR")
title("Bayesian Estimate of CTR")
colorbar()

show()

max_stars_repo_name: jjhenkel/dockerizeme

max_issues_repo_name: jjhenkel/dockerizeme

max_forks_repo_name: jjhenkel/dockerizeme

reason: from numpy,from scipy


text:
# Copyright 2018-2021 Xanadu Quantum Technologies Inc.

# 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.
"""
Unit tests for the batch params transform.
"""
import functools
import pytest

import pennylane as qml
from pennylane import numpy as np

def test_simple_circuit(mocker):
    """Test that batching works for a simple circuit"""
    dev = qml.device("default.qubit", wires=3)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(data, x, weights):
        qml.templates.AmplitudeEmbedding(data, wires=[0, 1, 2], normalize=True)
        qml.RX(x, wires=0)
        qml.RY(0.2, wires=1)
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2])
        return qml.probs(wires=[0, 2])

    batch_size = 5
    data = np.random.random((batch_size, 8))
    x = np.linspace(0.1, 0.5, batch_size, requires_grad=True)
    weights = np.ones((batch_size, 10, 3, 3), requires_grad=True)

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(data, x, weights)
    assert res.shape == (batch_size, 4)
    assert len(spy.call_args[0][0]) == batch_size

def test_basic_entangler_layers(mocker):
    """Test that batching works for BasicEngtanglerLayers"""
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(weights):
        qml.templates.BasicEntanglerLayers(weights, wires=[0, 1])
        qml.RY(0.2, wires=1)
        return qml.probs(wires=[0, 1])

    batch_size = 5
    weights = np.random.random((batch_size, 2, 2))

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(weights)
    assert res.shape == (batch_size, 4)
    assert len(spy.call_args[0][0]) == batch_size

def test_angle_embedding(mocker):
    """Test that batching works for AngleEmbedding"""
    dev = qml.device("default.qubit", wires=3)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(data):
        qml.templates.AngleEmbedding(data, wires=[0, 1, 2])
        qml.RY(0.2, wires=1)
        return qml.probs(wires=[0, 2])

    batch_size = 5
    data = np.random.random((batch_size, 3))

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(data)
    assert res.shape == (batch_size, 4)
    assert len(spy.call_args[0][0]) == batch_size

def test_mottonenstate_preparation(mocker):
    """Test that batching works for MottonenStatePreparation"""
    dev = qml.device("default.qubit", wires=3)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(data, weights):
        qml.templates.MottonenStatePreparation(data, wires=[0, 1, 2])
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2])
        return qml.probs(wires=[0, 1, 2])

    batch_size = 3

    # create a batched input statevector
    data = np.random.random((batch_size, 2**3))
    data /= np.linalg.norm(data, axis=1).reshape(-1, 1)  # normalize
    weights = np.random.random((batch_size, 10, 3, 3))

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(data, weights)
    assert res.shape == (batch_size, 2**3)
    assert len(spy.call_args[0][0]) == batch_size

    # check the results against individually executed circuits (no batching)
    @qml.qnode(dev)
    def circuit2(data, weights):
        qml.templates.MottonenStatePreparation(data, wires=[0, 1, 2])
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2])
        return qml.probs(wires=[0, 1, 2])

    indiv_res = []
    for state, weight in zip(data, weights):
        indiv_res.append(circuit2(state, weight))
    assert np.allclose(res, indiv_res)

def test_basis_state_preparation(mocker):
    """Test that batching works for BasisStatePreparation"""
    dev = qml.device("default.qubit", wires=4)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(data, weights):
        qml.templates.BasisStatePreparation(data, wires=[0, 1, 2, 3])
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2, 3])
        return qml.probs(wires=[0, 1, 2, 3])

    batch_size = 5

    # create random batched basis states
    data = np.random.randint(2, size=(batch_size, 4))
    weights = np.random.random((batch_size, 10, 4, 3))

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(data, weights)
    assert res.shape == (batch_size, 2**4)
    assert len(spy.call_args[0][0]) == batch_size

    # check the results against individually executed circuits (no batching)
    @qml.qnode(dev)
    def circuit2(data, weights):
        qml.templates.BasisStatePreparation(data, wires=[0, 1, 2, 3])
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2, 3])
        return qml.probs(wires=[0, 1, 2, 3])

    indiv_res = []
    for state, weight in zip(data, weights):
        indiv_res.append(circuit2(state, weight))
    assert np.allclose(res, indiv_res)

@pytest.mark.parametrize("diff_method", ["backprop", "adjoint", "parameter-shift"])
def test_autograd(diff_method, tol):
    """Test derivatives when using autograd"""
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, diff_method=diff_method)
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    def cost(x):
        return np.sum(circuit(x))

    batch_size = 3
    x = np.linspace(0.1, 0.5, batch_size, requires_grad=True)

    res = qml.grad(cost)(x)
    expected = -np.sin(0.1) * np.sin(x)
    assert np.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.parametrize("diff_method", ["backprop", "adjoint", "parameter-shift"])
def test_jax(diff_method, tol):
    """Test derivatives when using JAX."""
    jax = pytest.importorskip("jax")
    jnp = jax.numpy
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, interface="jax", diff_method=diff_method)
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    def cost(x):
        return jnp.sum(circuit(x))

    batch_size = 3
    x = jnp.linspace(0.1, 0.5, batch_size)

    res = jax.grad(cost)(x)
    expected = -np.sin(0.1) * np.sin(x)
    assert np.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.parametrize("diff_method", ["adjoint", "parameter-shift"])
@pytest.mark.parametrize("interface", ["jax", "jax-jit"])
def test_jax_jit(diff_method, interface, tol):
    """Test derivatives when using JAX and JIT."""
    jax = pytest.importorskip("jax")
    jnp = jax.numpy
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, interface=interface, diff_method=diff_method)
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    @jax.jit
    def cost(x):
        return jnp.sum(circuit(x))

    batch_size = 3
    x = jnp.linspace(0.1, 0.5, batch_size)

    res = jax.grad(cost)(x)
    expected = -np.sin(0.1) * np.sin(x)
    assert np.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.parametrize("diff_method", ["backprop", "adjoint", "parameter-shift"])
def test_torch(diff_method, tol):
    """Test derivatives when using Torch"""
    torch = pytest.importorskip("torch")
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, interface="torch", diff_method=diff_method)
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    def cost(x):
        return torch.sum(circuit(x))

    batch_size = 3
    x = torch.linspace(0.1, 0.5, batch_size, requires_grad=True)

    loss = cost(x)
    loss.backward()

    res = x.grad
    expected = -np.sin(0.1) * torch.sin(x)
    assert torch.allclose(res, expected, atol=tol, rtol=0)

@pytest.mark.parametrize("diff_method", ["backprop", "adjoint", "parameter-shift"])
def test_tf(diff_method, tol):
    """Test derivatives when using TF"""
    tf = pytest.importorskip("tensorflow")
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, interface="tf", diff_method=diff_method)
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    def cost(x):
        return tf.reduce_sum(circuit(x))

    batch_size = 3
    x = tf.Variable(tf.linspace(0.1, 0.5, batch_size))

    with tf.GradientTape() as tape:
        loss = cost(x)

    res = tape.gradient(loss, x)
    expected = -np.sin(0.1) * tf.sin(x)
    assert np.allclose(res, expected, atol=tol, rtol=0)

def test_tf_autograph(tol):
    """Test derivatives when using TF and autograph"""
    tf = pytest.importorskip("tensorflow")
    dev = qml.device("default.qubit", wires=2)

    @qml.batch_params
    @qml.qnode(dev, interface="tf", diff_method="backprop")
    def circuit(x):
        qml.RX(x, wires=0)
        qml.RY(0.1, wires=1)
        qml.CNOT(wires=[0, 1])
        return qml.expval(qml.PauliZ(0) @ qml.PauliX(1))

    @tf.function
    def cost(x):
        return tf.reduce_sum(circuit(x))

    batch_size = 3
    x = tf.Variable(tf.linspace(0.1, 0.5, batch_size))

    with tf.GradientTape() as tape:
        loss = cost(x)

    res = tape.gradient(loss, x)
    expected = -np.sin(0.1) * tf.sin(x)
    assert np.allclose(res, expected, atol=tol, rtol=0)

def test_all_operations(mocker):
    """Test that a batch dimension can be added to all operations"""
    dev = qml.device("default.qubit", wires=3)

    @functools.partial(qml.batch_params, all_operations=True)
    @qml.qnode(dev)
    def circuit(x, weights):
        qml.RX(x, wires=0)
        qml.RY([0.2, 0.3, 0.3], wires=1)
        qml.templates.StronglyEntanglingLayers(weights, wires=[0, 1, 2])
        return qml.probs(wires=[0, 2])

    batch_size = 3
    x = np.linspace(0.1, 0.5, batch_size, requires_grad=True)
    weights = np.ones((batch_size, 10, 3, 3), requires_grad=False)

    spy = mocker.spy(circuit.device, "batch_execute")
    res = circuit(x, weights)
    assert res.shape == (batch_size, 4)
    assert len(spy.call_args[0][0]) == batch_size

def test_unbatched_parameter():
    """Test that an exception is raised if a parameter
    is not batched"""

    dev = qml.device("default.qubit", wires=1)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(x, y):
        qml.RY(x, wires=[0])
        qml.RX(y, wires=[0])
        return qml.expval(qml.PauliZ(0))

    x = np.array([0.3, 0.4, 0.5])
    y = np.array(0.2)

    with pytest.raises(ValueError, match="0.2 has incorrect batch dimension"):
        circuit(x, y)

def test_initial_unbatched_parameter():
    """Test that an exception is raised if an initial parameter
    is not batched"""

    dev = qml.device("default.qubit", wires=1)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(x, y):
        qml.RY(x, wires=[0])
        qml.RX(y, wires=[0])
        return qml.expval(qml.PauliZ(0))

    x = np.array(0.2)
    y = np.array([0.3, 0.4, 0.5])

    with pytest.raises(ValueError, match="Parameter 0.2 does not contain a batch"):
        circuit(x, y)

def test_no_batch_param_error():
    """Test that the right error is thrown when there is nothing to batch"""
    dev = qml.device("default.qubit", wires=1)

    @qml.batch_params
    @qml.qnode(dev)
    def circuit(x):
        qml.RY(x, wires=0)
        return qml.expval(qml.PauliZ(0))

    x = [0.2, 0.6, 3]
    with pytest.raises(ValueError, match="There are no operations to transform"):
        circuit(x)

max_stars_repo_name: therooler/pennylane

max_issues_repo_name: therooler/pennylane

max_forks_repo_name: therooler/pennylane


text:
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
from lib.dataset_kmeans import KMeansPreprocessingDataset

class KmeansClustering(nn.Module):
    def __init__(self, num_clusters, num_features, adjustment_rate=1e-3, n_fft=2048, centroids=None):
        super(KmeansClustering, self).__init__()

        self.adjustment_rate = adjustment_rate
        self.num_clusters = num_clusters
        self.num_features = num_features
        self.centroids = None
        self.energy = torch.inf
        self.max_bin = n_fft // 2
        self.centroids = torch.randn(num_clusters, num_features) if centroids is None else centroids

    def initialize(self, dataloader: KMeansPreprocessingDataset, device=None, num_init_samples=8):
        with torch.no_grad():
            centroids = None

            for i, B in enumerate(tqdm(dataloader)):
                B = B.to(device)
                B = B[:, :, :, :, :self.max_bin]
                N,T,W,C,H = B.shape
                B = B.reshape(N*T,W*C*H)

                centroids = B if centroids is None else centroids + B

                if i == num_init_samples-1:
                    break

            centroids = centroids / num_init_samples
            self.centroids = centroids.clone()

    def __call__(self, x):
        if self.centroids.device != x.device:
            self.centroids = self.centroids.to(x.device)

        with torch.no_grad():
            sum_delta = 0
            delta_k = 0
            dist = torch.sum(torch.square(torch.sub(x.unsqueeze(0), self.centroids.unsqueeze(1))), dim=2)
            nearest = torch.argmin(dist, dim=0)
            avg_dist = torch.sum(dist[:, nearest]) / x.shape[0]
            global_centroid = torch.sum(x, dim=0) / x.shape[0]

            miss_count = 0
            for K in range(self.num_clusters):
                idx, = torch.where(nearest == K)

                if len(idx) > 0:
                    delta_k += 1
                    new_center = self.adjustment_rate * (torch.sum(x[idx, :], dim=0) / len(idx)) + (1 - self.adjustment_rate) * self.centroids[K]
                    delta = torch.sum(torch.square(torch.sub(new_center, self.centroids[K])))
                    sum_delta = sum_delta + delta
                    self.centroids[K] = new_center
                else:
                    self.centroids[K] = self.adjustment_rate * global_centroid + (1 - self.adjustment_rate) * self.centroids[K]
                    miss_count += 1

            return sum_delta / delta_k, avg_dist, miss_count

    def get_clusters(self, x):
        if self.centroids.device != x.device:
            self.centroids = self.centroids.to(x.device)

        return torch.argmin(torch.sum(torch.square(torch.sub(x.unsqueeze(0), self.centroids.unsqueeze(1))), dim=2), dim=0)