# Databricks notebook source
# MAGIC %md
# MAGIC # Feature Store taxi example notebook
# MAGIC
# MAGIC This notebook illustrates the use of Feature Store to create a model that predicts NYC Yellow Taxi fares. It includes these steps:
# MAGIC
# MAGIC - Compute and write features.
# MAGIC - Train a model using these features to predict fares.
# MAGIC - Evaluate that model on a new batch of data using existing features, saved to Feature Store.
# MAGIC
# MAGIC ## Requirements
# MAGIC - Databricks Runtime for Machine Learning 8.3 or above.
# MAGIC
# MAGIC **Note:** This notebook is written to run with Databricks Runtime for Machine Learning 10.2 or above. If you are using Databricks Runtime for Machine Learning 10.1 or below, delete or comment out Cmd 19 and uncomment Cmd 20.

# COMMAND ----------

# MAGIC %md
# MAGIC

# COMMAND ----------

# MAGIC %md ## Compute features

# COMMAND ----------

# MAGIC %md #### Load the raw data used to compute features
# MAGIC
# MAGIC Load the `nyc-taxi-tiny` dataset.  This was generated from the full [NYC Taxi Data](https://www1.nyc.gov/site/tlc/about/tlc-trip-record-data.page) which can be found at `dbfs:/databricks-datasets/nyctaxi` by applying the following transformations:
# MAGIC
# MAGIC 1. Apply a UDF to convert latitude and longitude coordinates into ZIP codes, and add a ZIP code column to the DataFrame.
# MAGIC 1. Subsample the dataset into a smaller dataset based on a date range query using the `.sample()` method of the Spark `DataFrame` API.
# MAGIC 1. Rename certain columns and drop unnecessary columns.
# MAGIC
# MAGIC If you want to create this dataset from the raw data yourself, follow these steps:
# MAGIC 1. Run the Feature Store taxi example dataset notebook ([AWS](https://docs.databricks.com/_static/notebooks/machine-learning/feature-store-taxi-example-dataset.html)|[Azure](https://docs.microsoft.com/azure/databricks/_static/notebooks/machine-learning/feature-store-taxi-example-dataset.html)|[GCP](https://docs.gcp.databricks.com/_static/notebooks/machine-learning/feature-store-taxi-example-dataset.html)) to generate the Delta table.
# MAGIC 1. In this notebook, replace the following `spark.read.format("delta").load("/databricks-datasets/nyctaxi-with-zipcodes/subsampled")` with: `spark.read.table("feature_store_taxi_example.nyc_yellow_taxi_with_zips")`

# COMMAND ----------

raw_data = spark.read.format("delta").load("/databricks-datasets/nyctaxi-with-zipcodes/subsampled")
display(raw_data)

# COMMAND ----------

# MAGIC %md
# MAGIC
# MAGIC From the taxi fares transactional data, we will compute two groups of features based on trip pickup and drop off zip codes.
# MAGIC
# MAGIC #### Pickup features
# MAGIC 1. Count of trips (time window = 1 hour, sliding window = 15 minutes)
# MAGIC 1. Mean fare amount (time window = 1 hour, sliding window = 15 minutes)
# MAGIC
# MAGIC #### Drop off features
# MAGIC 1. Count of trips (time window = 30 minutes)
# MAGIC 1. Does trip end on the weekend (custom feature using python code)
# MAGIC
# MAGIC

# COMMAND ----------

# MAGIC %md ### Helper functions

# COMMAND ----------

from databricks import feature_store
from pyspark.sql.functions import *
from pyspark.sql.types import FloatType, IntegerType, StringType
from pytz import timezone

@udf(returnType=IntegerType())
def is_weekend(dt):
    tz = "America/New_York"
    return int(dt.astimezone(timezone(tz)).weekday() >= 5)  # 5 = Saturday, 6 = Sunday

@udf(returnType=StringType())
def partition_id(dt):
    # datetime -> "YYYY-MM"
    return f"{dt.year:04d}-{dt.month:02d}"

def filter_df_by_ts(df, ts_column, start_date, end_date):
    if ts_column and start_date:
        df = df.filter(col(ts_column) >= start_date)
    if ts_column and end_date:
        df = df.filter(col(ts_column) < end_date)
    return df

# COMMAND ----------

# MAGIC %md ### Data scientist's custom code to compute features

# COMMAND ----------

def pickup_features_fn(df, ts_column, start_date, end_date):
    """
    Computes the pickup_features feature group.
    To restrict features to a time range, pass in ts_column, start_date, and/or end_date as kwargs.
    """
    df = filter_df_by_ts(
        df, ts_column, start_date, end_date
    )
    pickupzip_features = (
        df.groupBy(
            "pickup_zip", window("tpep_pickup_datetime", "1 hour", "15 minutes")
        )  # 1 hour window, sliding every 15 minutes
        .agg(
            mean("fare_amount").alias("mean_fare_window_1h_pickup_zip"),
            count("*").alias("count_trips_window_1h_pickup_zip"),
        )
        .select(
            col("pickup_zip").alias("zip"),
            unix_timestamp(col("window.end")).alias("ts").cast(IntegerType()),
            partition_id(to_timestamp(col("window.end"))).alias("yyyy_mm"),
            col("mean_fare_window_1h_pickup_zip").cast(FloatType()),
            col("count_trips_window_1h_pickup_zip").cast(IntegerType()),
        )
    )
    return pickupzip_features

def dropoff_features_fn(df, ts_column, start_date, end_date):
    """
    Computes the dropoff_features feature group.
    To restrict features to a time range, pass in ts_column, start_date, and/or end_date as kwargs.
    """
    df = filter_df_by_ts(
        df,  ts_column, start_date, end_date
    )
    dropoffzip_features = (
        df.groupBy("dropoff_zip", window("tpep_dropoff_datetime", "30 minute"))
        .agg(count("*").alias("count_trips_window_30m_dropoff_zip"))
        .select(
            col("dropoff_zip").alias("zip"),
            unix_timestamp(col("window.end")).alias("ts").cast(IntegerType()),
            partition_id(to_timestamp(col("window.end"))).alias("yyyy_mm"),
            col("count_trips_window_30m_dropoff_zip").cast(IntegerType()),
            is_weekend(col("window.end")).alias("dropoff_is_weekend"),
        )
    )
    return dropoffzip_features

# COMMAND ----------

from datetime import datetime

pickup_features = pickup_features_fn(
    raw_data, ts_column="tpep_pickup_datetime", start_date=datetime(2016, 1, 1), end_date=datetime(2016, 1, 31)
)
dropoff_features = dropoff_features_fn(
    raw_data, ts_column="tpep_dropoff_datetime", start_date=datetime(2016, 1, 1), end_date=datetime(2016, 1, 31)
)

# COMMAND ----------

display(pickup_features)

# COMMAND ----------

# MAGIC %md ### Use Feature Store library to create new feature tables

# COMMAND ----------

# MAGIC %md First, create the database where the feature tables will be stored.

# COMMAND ----------

dbutils.widgets.text("fs_db_name","")

# COMMAND ----------

fs_db_name = dbutils.widgets.get("fs_db_name")

# COMMAND ----------

# MAGIC %sql
# MAGIC CREATE DATABASE IF NOT EXISTS $fs_db_name;

# COMMAND ----------

# MAGIC %md Next, create an instance of the Feature Store client.

# COMMAND ----------

fs = feature_store.FeatureStoreClient()

# COMMAND ----------

# MAGIC %md
# MAGIC Use either the `create_table` API (Databricks Runtime 10.2 ML or above) or the `create_feature_table` API (Databricks Runtime 10.1 ML or below) to define schema and unique ID keys. If the optional argument `df` (Databricks Runtime 10.2 ML or above) or `features_df` (Databricks Runtime 10.1 ML or below) is passed, the API also writes the data to Feature Store.

# COMMAND ----------

# This cell uses an API introduced with Databricks Runtime 10.2 ML.
# If your cluster is running Databricks Runtime 10.1 ML or below, skip or comment out this cell and uncomment and run Cmd 22.
'''
spark.conf.set("spark.sql.shuffle.partitions", "5")

fs.create_table(
    name=f"{fs_db_name}.trip_pickup_features",
    primary_keys=["zip", "ts"],
    df=pickup_features,
    partition_columns="yyyy_mm",
    description="Taxi Fares. Pickup Features",
)
fs.create_table(
    name=f"{fs_db_name}.trip_dropoff_features",
    primary_keys=["zip", "ts"],
    df=dropoff_features,
    partition_columns="yyyy_mm",
    description="Taxi Fares. Dropoff Features",
)
'''

# COMMAND ----------

# To run this notebook with Databricks Runtime 10.1 ML or below, uncomment this cell.

spark.conf.set("spark.sql.shuffle.partitions", "5")

fs.create_feature_table(
    name=f"{fs_db_name}.trip_pickup_features",
    keys=["zip", "ts"],
    features_df=pickup_features,
    partition_columns="yyyy_mm",
    description="Taxi Fares. Pickup Features",
)

fs.create_feature_table(
    name=f"{fs_db_name}.trip_dropoff_features",
    keys=["zip", "ts"],
    features_df=dropoff_features,
    partition_columns="yyyy_mm",
    description="Taxi Fares. Dropoff Features",
)

# COMMAND ----------

# MAGIC %md ## Update features
# MAGIC
# MAGIC Use the `write_table` function to update the feature table values.
# MAGIC
# MAGIC

# COMMAND ----------

display(raw_data)

# COMMAND ----------

# Compute the pickup_features feature group.
pickup_features_df = pickup_features_fn(
  df=raw_data,
  ts_column="tpep_pickup_datetime",
  start_date=datetime(2016, 2, 1),
  end_date=datetime(2016, 2, 29),
)

# Write the pickup features DataFrame to the feature store table
fs.write_table(
  name=f"{fs_db_name}.trip_pickup_features",
  df=pickup_features_df,
  mode="merge",
)

# Compute the dropoff_features feature group.
dropoff_features_df = dropoff_features_fn(
  df=raw_data,
  ts_column="tpep_dropoff_datetime",
  start_date=datetime(2016, 2, 1),
  end_date=datetime(2016, 2, 29),
)

# Write the dropoff features DataFrame to the feature store table
fs.write_table(
  name=f"{fs_db_name}.trip_dropoff_features",
  df=dropoff_features_df,
  mode="merge",
)

# COMMAND ----------

# MAGIC %md When writing, both `merge` and `overwrite` modes are supported.
# MAGIC
# MAGIC     fs.write_table(
# MAGIC       name="feature_store_taxi_example.trip_pickup_features",
# MAGIC       df=pickup_features_df,
# MAGIC       mode="overwrite",
# MAGIC     )
# MAGIC
# MAGIC Data can also be streamed into Feature Store by passing a dataframe where `df.isStreaming` is set to `True`:
# MAGIC
# MAGIC     fs.write_table(
# MAGIC       name="streaming_example.streaming_features",
# MAGIC       df=streaming_df,
# MAGIC       mode="merge",
# MAGIC     )
# MAGIC
# MAGIC You can schedule a notebook to periodically update features using Databricks Jobs ([AWS](https://docs.databricks.com/jobs.html)|[Azure](https://docs.microsoft.com/azure/databricks/jobs)|[GCP](https://docs.gcp.databricks.com/jobs.html)).

# COMMAND ----------

# MAGIC %md Analysts can interact with Feature Store using SQL, for example:

# COMMAND ----------

# MAGIC %sql
# MAGIC SELECT SUM(count_trips_window_30m_dropoff_zip) AS num_rides,
# MAGIC        dropoff_is_weekend
# MAGIC FROM   $fs_db_name.trip_dropoff_features
# MAGIC WHERE  dropoff_is_weekend IS NOT NULL
# MAGIC GROUP  BY dropoff_is_weekend;

# COMMAND ----------

# MAGIC %md ## Feature Search and Discovery

# COMMAND ----------

# MAGIC %md
# MAGIC You can now discover your feature tables in the Feature Store UI.
# MAGIC
# MAGIC
# MAGIC
# MAGIC Search by "trip_pickup_features" or "trip_dropoff_features" to view details such as table schema, metadata, data sources, producers, and online stores.
# MAGIC
# MAGIC You can also edit the description for the feature table, or configure permissions for a feature table using the dropdown icon next to the feature table name.
# MAGIC
# MAGIC Check the [Use the Feature Store UI
# MAGIC ](https://docs.databricks.com/applications/machine-learning/feature-store.html#use-the-feature-store-ui) documentation for more details.

# COMMAND ----------

# MAGIC %md ## Train a model
# MAGIC
# MAGIC This section illustrates how to train a model using the pickup and dropoff features stored in Feature Store. It trains a LightGBM model to predict taxi fare.

# COMMAND ----------

# MAGIC %md ### Helper functions

# COMMAND ----------

from pyspark.sql import *
from pyspark.sql.functions import current_timestamp
from pyspark.sql.types import IntegerType
import math
from datetime import timedelta
import mlflow.pyfunc

def rounded_unix_timestamp(dt, num_minutes=15):
    """
    Ceilings datetime dt to interval num_minutes, then returns the unix timestamp.
    """
    nsecs = dt.minute * 60 + dt.second + dt.microsecond * 1e-6
    delta = math.ceil(nsecs / (60 * num_minutes)) * (60 * num_minutes) - nsecs
    return int((dt + timedelta(seconds=delta)).timestamp())

rounded_unix_timestamp_udf = udf(rounded_unix_timestamp, IntegerType())

def rounded_taxi_data(taxi_data_df):
    # Round the taxi data timestamp to 15 and 30 minute intervals so we can join with the pickup and dropoff features
    # respectively.
    taxi_data_df = (
        taxi_data_df.withColumn(
            "rounded_pickup_datetime",
            rounded_unix_timestamp_udf(taxi_data_df["tpep_pickup_datetime"], lit(15)),
        )
        .withColumn(
            "rounded_dropoff_datetime",
            rounded_unix_timestamp_udf(taxi_data_df["tpep_dropoff_datetime"], lit(30)),
        )
        .drop("tpep_pickup_datetime")
        .drop("tpep_dropoff_datetime")
    )
    taxi_data_df.createOrReplaceTempView("taxi_data")
    return taxi_data_df

def get_latest_model_version(model_name):
  latest_version = 1
  mlflow_client = MlflowClient()
  for mv in mlflow_client.search_model_versions(f"name='{model_name}'"):
    version_int = int(mv.version)
    if version_int > latest_version:
      latest_version = version_int
  return latest_version

# COMMAND ----------

# MAGIC %md ### Read taxi data for training

# COMMAND ----------

taxi_data = rounded_taxi_data(raw_data)

# COMMAND ----------

# MAGIC %md ### Understanding how a training dataset is created
# MAGIC
# MAGIC In order to train a model, you need to create a training dataset that is used to train the model.  The training dataset is comprised of:
# MAGIC
# MAGIC 1. Raw input data
# MAGIC 1. Features from the feature store
# MAGIC
# MAGIC The raw input data is needed because it contains:
# MAGIC
# MAGIC 1. Primary keys used to join with features.
# MAGIC 1. Raw features like `trip_distance` that are not in the feature store.
# MAGIC 1. Prediction targets like `fare` that are required for model training.
# MAGIC
# MAGIC Here's a visual overview that shows the raw input data being combined with the features in the Feature Store to produce the training dataset:
# MAGIC
# MAGIC
# MAGIC
# MAGIC These concepts are described further in the Creating a Training Dataset documentation ([AWS](https://docs.databricks.com/applications/machine-learning/feature-store.html#create-a-training-dataset)|[Azure](https://docs.microsoft.com/en-us/azure/databricks/applications/machine-learning/feature-store#create-a-training-dataset)|[GCP](https://docs.gcp.databricks.com/applications/machine-learning/feature-store.html#create-a-training-dataset)).
# MAGIC
# MAGIC The next cell loads features from Feature Store for model training by creating a `FeatureLookup` for each needed feature.

# COMMAND ----------

# If you are running Databricks Runtime for Machine Learning 9.1 or below, you can uncomment the code in this cell and use it instead of the code in Cmd 34.

from databricks.feature_store import FeatureLookup
import mlflow

pickup_features_table = f"{fs_db_name}.trip_pickup_features"
dropoff_features_table = f"{fs_db_name}.trip_dropoff_features"

pickup_feature_lookups = [
    FeatureLookup(
      table_name = pickup_features_table,
      feature_names = ["mean_fare_window_1h_pickup_zip", "count_trips_window_1h_pickup_zip"],
      lookup_key = ["pickup_zip", "rounded_pickup_datetime"],
    ),
]

dropoff_feature_lookups = [
    FeatureLookup(
      table_name = dropoff_features_table,
      feature_names = ["count_trips_window_30m_dropoff_zip", "dropoff_is_weekend"],
      lookup_key = ["dropoff_zip", "rounded_dropoff_datetime"],
    ),
]

# COMMAND ----------

# MAGIC %md ### Create a Training Dataset
# MAGIC
# MAGIC When `fs.create_training_set(..)` is invoked below, the following steps will happen:
# MAGIC
# MAGIC 1. A `TrainingSet` object will be created, which will select specific features from Feature Store to use in training your model. Each feature is specified by the `FeatureLookup`'s created above.
# MAGIC
# MAGIC 1. Features are joined with the raw input data according to each `FeatureLookup`'s `lookup_key`.
# MAGIC
# MAGIC The `TrainingSet` is then transformed into a DataFrame to train on. This DataFrame includes the columns of taxi_data, as well as the features specified in the `FeatureLookups`.

# COMMAND ----------

# End any existing runs (in the case this notebook is being run for a second time)
mlflow.end_run()

# Start an mlflow run, which is needed for the feature store to log the model
mlflow.start_run()

# Since the rounded timestamp columns would likely cause the model to overfit the data
# unless additional feature engineering was performed, exclude them to avoid training on them.
exclude_columns = ["rounded_pickup_datetime", "rounded_dropoff_datetime"]

# Create the training set that includes the raw input data merged with corresponding features from both feature tables
training_set = fs.create_training_set(
  taxi_data,
  feature_lookups = pickup_feature_lookups + dropoff_feature_lookups,
  label = "fare_amount",
  exclude_columns = exclude_columns
)

# Load the TrainingSet into a dataframe which can be passed into sklearn for training a model
training_df = training_set.load_df()

# COMMAND ----------

# Display the training dataframe, and note that it contains both the raw input data and the features from the Feature Store, like `dropoff_is_weekend`
display(training_df)

# COMMAND ----------

# MAGIC %md ## Next steps
# MAGIC
# MAGIC 1. Explore the feature tables created in this example in the Feature Store UI.
# MAGIC 1. Adapt this notebook to your own data and create your own feature tables.


class FoodItem:
    def __init__(self,id1,name,category,price):
        self.item_id = id1
        self.item_name = name
        self.item_category = category
        self.item_price = price
    def provideDiscount(self,number):
            self.item_price = self.item_price - ((self.item_price *number)/100)
class Restaurant:
    def __init__(self,name,flist):
        self.restaurant_name = name
        self.fooditem_list = flist
    def retrieveUpdatedPrice(self,itemid,percent):
        for f in self.fooditem_list:
            if f.item_id == itemid:
                f.provideDiscount(percent)
                return f

if __name__ =="__main__":
    flist =[]
    n = int(input())
    for i in range(n):
        id1 = int(input())
        name = input()
        category = input()
        price = int(input())
        flist.append(FoodItem(id1,name,category,price))
    itemid = int(input())
    percent = int(input())
    res = Restaurant("TCS",flist)
    ans = res.retrieveUpdatedPrice(itemid,percent)
    if ans==None:
        print("No Food item exists which matches the search criteria")
    else:
        print(ans.item_name,end=' ')
        print(ans.item_price,end=' ')


from tkinter import *

class Box():
	''' Represent BOX '''

	def __init__(self, name):
		""" Imitialize the Box"""
		self.name = name

	def Set(self):
		print(self.name + " ชุดใหญ่")

	def BUTTON(self):
		GUI = Tk()
		GUI.title('การใช้ class')
		GUI.geometry('300x500')

		self.LB1 = Label(GUI, text='ปุ่มกด')
		self.LB1.pack()

		self.BT1 = Button(GUI, text='เปิดไฟ-1')
		self.BT1.pack(ipady=20,ipadx=80)

		GUI = mainloop()

my_box = Box('ของรางวัล')

print("นี่คิือ : " + my_box.name)
my_box.Set()

print('----------------------------')

# ( Inheritance)การสืบทอดคุณสมบัติ
class AAbox(Box):

	def __init__(self, name):
		super().__init__(name)

	def search(self):
		print(self.name + " สิ่งที่ค้นหาอยู่")

my_box = AAbox('กล่อง')

print(my_box.name + "เครื่องมือ")
my_box.Set()
my_box.search()

my_box.BUTTON()


#!/bin/python3

import os

#
# Complete the 'quickSort' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def quickSort(arr):
    # Write your code here
    pivot = arr[0]
    left = []
    right = []
    for element in arr:
        if element < pivot:
            left.append(element)
        else:
            right.append(element)
    return left + right

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    arr = list(map(int, input().rstrip().split()))

    result = quickSort(arr)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()

Algorithms/04 - Sorting/Quicksort 1 - Partition.py


import socket
import threading
import time
import random

HOST = "192.168.161.251"
PORT = 5555
TYPE = "utf-8"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((HOST, PORT))

server.listen()

print(f"[SERVER LISTENING ON PORT {PORT}]")

allClients = []
playClients = []

sentChar = False
character = None

def sendToAllOtherClients(message, conn, printing):
    for client in playClients:
        currentConn = client[0]
        if currentConn != conn:
            if printing:
                print(f"Sending message: {message}")
            currentConn.send(message.encode(TYPE))
            break

def handleClient(conn, addr):
    character = None
    while True:
        try:
            message = conn.recv(4128)
            message = message.decode(TYPE)

            if message.split(":")[0] == "char":
                character = message.split(":")[1]

                enoughPlayers = len(playClients) < 1

                if enoughPlayers:
                    conn.send("not ready".encode(TYPE))
                    playClients.append([conn, message.split(":")[1]])

                else:
                    playClients.append([conn, message.split(":")[1]])

                    for clients in playClients:
                        currentConn = clients[0]
                        currentConn.send("ready".encode(TYPE))

            elif message == "quit":
                allClients.remove(conn)
                for client in playClients:
                    if conn in client:
                        playClients.remove(client)
                        break
                conn.close()
                print(f"removed client - length of clients: {len(playClients)}")
                break

            if message == "getChar":
                for client in playClients:
                    currentConn = client[0]

                    if currentConn != conn:
                        currentConn.send(f"{character}".encode(TYPE))
                        break

            if message.split(":")[0] == "velo":
                sendToAllOtherClients(message, conn, False)

            if message == "jump\n":
                sendToAllOtherClients(message, conn, True)

            if message.split(":")[0] == "dmg" or message.split(":")[0] == "knockback" or message.split(":")[0] == "attack":
                sendToAllOtherClients(message, conn, True)

        except Exception as e:
            print(f"Error in handling client {addr}: {e}")
            allClients.remove(conn)

            for client in playClients:
                if conn in client:
                    playClients.remove(client)
                    break

            conn.close()
            break

def getClients():
    while True:
        conn, addr = server.accept()
        allClients.append(conn)
        print(allClients)
        print(f"CONNECTED TO SERVER AT ADRESS - {addr}")
        newThread = threading.Thread(target=handleClient, args=(conn, addr))
        newThread.start()

getClients()


from blog.app import app
from blog.models.database import db

if __name__ == "__main__":
    app.run(
        host="0.0.0.0",
        debug=True,
    )

@app.cli.command("create-tags")
def create_tags():
    """
    Run in your terminal:
    flask create-tags
    > done! created tags: <flask> <django> <python> <gb> <sqlite>
    """
    from blog.models import Tag

    tags = ('flask', 'django', 'python', 'gb', 'sqlite')
    for item in tags:
        db.session.add(Tag(name=item))
    db.session.commit()
    print(f"done! created tags: {', '.join(tags)}")


# AI 2018

# Importing the libraries
import os
import numpy as np
import gym
from gym import wrappers
import pybullet_envs

# Setting the Hyper Parameters

class Hp():

    def __init__(self):
        self.nb_steps = 1000
        self.episode_length = 1000
        self.learning_rate = 0.02
        self.nb_directions = 16
        self.nb_best_directions = 16
        assert self.nb_best_directions <= self.nb_directions
        self.noise = 0.03
        self.seed = 1
        self.env_name = 'HalfCheetahBulletEnv-v0'

# Normalizing the states

class Normalizer():

    def __init__(self, nb_inputs):
        self.n = np.zeros(nb_inputs)
        self.mean = np.zeros(nb_inputs)
        self.mean_diff = np.zeros(nb_inputs)
        self.var = np.zeros(nb_inputs)

    def observe(self, x):
        self.n += 1.
        last_mean = self.mean.copy()
        self.mean += (x - self.mean) / self.n
        self.mean_diff += (x - last_mean) * (x - self.mean)
        self.var = (self.mean_diff / self.n).clip(min = 1e-2)

    def normalize(self, inputs):
        obs_mean = self.mean
        obs_std = np.sqrt(self.var)
        return (inputs - obs_mean) / obs_std

# Building the AI

class Policy():

    def __init__(self, input_size, output_size):
        self.theta = np.zeros((output_size, input_size))
        print("self.theta=",self.theta)
    def evaluate(self, input, delta = None, direction = None):
        if direction is None:
            return np.clip(self.theta.dot(input), -1.0, 1.0)
        elif direction == "positive":
            return np.clip((self.theta + hp.noise*delta).dot(input), -1.0, 1.0)
        else:
            return np.clip((self.theta - hp.noise*delta).dot(input), -1.0, 1.0)

    def sample_deltas(self):
        return [np.random.randn(*self.theta.shape) for _ in range(hp.nb_directions)]

    def update(self, rollouts, sigma_r):
        step = np.zeros(self.theta.shape)
        for r_pos, r_neg, d in rollouts:
            step += (r_pos - r_neg) * d
        self.theta += hp.learning_rate / (hp.nb_best_directions * sigma_r) * step

# Exploring the policy on one specific direction and over one episode

def explore(env, normalizer, policy, direction = None, delta = None):
    state = env.reset()
    done = False
    num_plays = 0.
    sum_rewards = 0
    while not done and num_plays < hp.episode_length:
        normalizer.observe(state)
        state = normalizer.normalize(state)
        action = policy.evaluate(state, delta, direction)
        state, reward, done, _ = env.step(action)
        reward = max(min(reward, 1), -1)
        sum_rewards += reward
        num_plays += 1
    return sum_rewards

# Training the AI

def train(env, policy, normalizer, hp):

    for step in range(hp.nb_steps):

        # Initializing the perturbations deltas and the positive/negative rewards
        deltas = policy.sample_deltas()
        positive_rewards = [0] * hp.nb_directions
        negative_rewards = [0] * hp.nb_directions

        # Getting the positive rewards in the positive directions
        for k in range(hp.nb_directions):
            positive_rewards[k] = explore(env, normalizer, policy, direction = "positive", delta = deltas[k])

        # Getting the negative rewards in the negative/opposite directions
        for k in range(hp.nb_directions):
            negative_rewards[k] = explore(env, normalizer, policy, direction = "negative", delta = deltas[k])

        # Gathering all the positive/negative rewards to compute the standard deviation of these rewards
        all_rewards = np.array(positive_rewards + negative_rewards)
        sigma_r = all_rewards.std()

        # Sorting the rollouts by the max(r_pos, r_neg) and selecting the best directions
        scores = {k:max(r_pos, r_neg) for k,(r_pos,r_neg) in enumerate(zip(positive_rewards, negative_rewards))}
        order = sorted(scores.keys(), key = lambda x:scores[x])[:hp.nb_best_directions]
        rollouts = [(positive_rewards[k], negative_rewards[k], deltas[k]) for k in order]

        # Updating our policy
        policy.update(rollouts, sigma_r)

        # Printing the final reward of the policy after the update
        reward_evaluation = explore(env, normalizer, policy)
        print('Step:', step, 'Reward:', reward_evaluation)

# Running the main code

def mkdir(base, name):
    path = os.path.join(base, name)
    if not os.path.exists(path):
        os.makedirs(path)
    return path
work_dir = mkdir('exp', 'brs')
monitor_dir = mkdir(work_dir, 'monitor')

hp = Hp()
np.random.seed(hp.seed)
env = gym.make(hp.env_name)
# env.render(mode = "human")
#env = wrappers.Monitor(env, monitor_dir, force = True)
nb_inputs = env.observation_space.shape[0]
nb_outputs = env.action_space.shape[0]
policy = Policy(nb_inputs, nb_outputs)
normalizer = Normalizer(nb_inputs)
train(env, policy, normalizer, hp)


c_list = {'mid-red'      : '#e61e1e',
          'mid-green'    : '#5cbf00',
          'orange'       : '#FF8856',
          'mid-blue'     : '#1e5ae6',
          'gray'         : '#505050'}

def make_figure_5a(ax):
    #import matplotlib
    #matplotlib.rcParams['text.usetex'] = True
    import matplotlib.pyplot as plt  #to implement plots
    import csv                       #to read and process CSV files
    import numpy as np               #to format numbers
    from matplotlib.ticker import (MultipleLocator,FormatStrFormatter,AutoMinorLocator)

    mols = [] #x-axis data
    nm   = [] #y-axis data 1
    rom  = [] #y-axis data 2
    nul  = [] #y-axis data 3
    qst  = [] #y-axis data 4
    ee   = [] #x-axis data 5

    #open CSV file
    with open('../fig5a_s0.csv') as csvDataFile:
        csvReader = csv.reader(csvDataFile)    #file handler
        for row in csvReader:                  #loop through file handler
            mols.append(row[0])                #load up second column element
            nm.append(row[1])                  #load up third column element
            rom.append(row[2])
            nul.append(row[3])
            qst.append(row[4])

    nmFloat=list(map(float, nm))
    romFloat=list(map(float, rom))
    qstFloat=list(map(float, qst))

    #fig, ax = plt.subplots()
    x = np.arange(len(mols))                   #label locations
    width = 0.1                               #width of the bars
    #plt.rcParams['font.size'] = '16'

    rects1=ax.bar(x - 0.15,nmFloat, width, label="VQE (nm)",   color = c_list['gray'])
    rects2=ax.bar(x - 0.05,romFloat, width, label="VQE (rom)", color = c_list['mid-red'])
    rects3=ax.bar(x + 0.05,nul, width)
    rects4=ax.bar(x + 0.15,qstFloat, width, label="VQE (qst)", color = c_list['mid-blue'])

    def autolabel(rects, xpos='center',jr=0):
        ha = {'center': 'center', 'right': 'left', 'left': 'right'}
        offset = {'center': 0, 'right': 1, 'left': -1}

        for rect in rects:
            y_value = rect.get_height()
            x_value = rect.get_x() + rect.get_width() / 2
            space =  5
            va = 'bottom'
            if y_value < 0:
                space *= -1
                va = 'top'
            label = "{:.1f}".format(y_value)
            ax.annotate(
                label,                      # Use `label` as label
                (x_value,17-4*(jr//3)),     # Place label at end of the bar
                xytext=(0, space),          # Vertically shift label by `space`
                textcoords="offset points", # Interpret `xytext` as offset in points
                ha='center',                # Horizontally center label
                va=va,fontsize=20)          # Vertically align label differently for
                                            # positive and negative values.
            ax.plot([x_value,x_value],[17-4*(jr//3)+0.5,y_value+0.5],linestyle='--',color='black',linewidth=0.8)
            jr += 1
        return jr

    jr = 0
    jr = autolabel(rects1, "center", jr)
    jr = autolabel(rects2, "center", jr)
    jr = autolabel(rects4, "center", jr)

    ax.set_xticks(x)            #axis setting of X ticks
    ax.set_xticklabels(mols)    #axis setting of X tick labels
    ax.set_title('(a) $S_0$ states', fontsize=24,pad=10)
    ax.set_ylabel(r' $ \Delta \Delta E_{ST}$ [eV]', fontsize=18)
    ax.get_yaxis().set_major_formatter(FormatStrFormatter('% 1.1f'))


'''

-Hard-
*Prefix Sum*

Given an integer array nums, return the sum of floor(nums[i] / nums[j]) for all pairs of indices 0 <= i, j < nums.length in the array. Since the answer may be too large, return it modulo 109 + 7.

The floor() function returns the integer part of the division.

Example 1:

Input: nums = [2,5,9]
Output: 10
Explanation:
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
floor(5 / 2) = 2
floor(9 / 2) = 4
floor(9 / 5) = 1
We calculate the floor of the division for every pair of indices in the array then sum them up.
Example 2:

Input: nums = [7,7,7,7,7,7,7]
Output: 49

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 105

'''

from typing import List
from collections import Counter
from itertools import accumulate

class Solution:
    def sumOfFlooredPairs(self, nums: List[int]) -> int:
        incs, counter=[0]*(max(nums)+1), Counter(nums)            # To store all the quotients increases; counter
        for num in counter:                                       # Loop over all the divisors
            for j in range(num, len(incs), num):                  # Loop over all the possible dividends where the quotient increases
                incs[j] += counter[num]                           # Increment the increases in quotients
        quots=list(accumulate(incs))                              # Accumulate the increases to get the sum of quotients
        print(incs, quots)
        return sum([quots[num] for num in nums]) % 1_000_000_007  # Sum up all the quotients for all the numbers in the list.

if __name__ == "__main__":
    print(Solution().sumOfFlooredPairs(nums = [2,5,9]))


# A website domain like "discuss.leetcode.com" consists of various subdomains. A
# t the top level, we have "com", at the next level, we have "leetcode.com", and a
# t the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss
# .leetcode.com", we will also visit the parent domains "leetcode.com" and "com" i
# mplicitly.
#
#  Now, call a "count-paired domain" to be a count (representing the number of v
# isits this domain received), followed by a space, followed by the address. An ex
# ample of a count-paired domain might be "9001 discuss.leetcode.com".
#
#  We are given a list cpdomains of count-paired domains. We would like a list o
# f count-paired domains, (in the same format as the input, and in any order), tha
# t explicitly counts the number of visits to each subdomain.
#
#
# Example 1:
# Input:
# ["9001 discuss.leetcode.com"]
# Output:
# ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
# Explanation:
# We only have one website domain: "discuss.leetcode.com". As discussed above, t
# he subdomain "leetcode.com" and "com" will also be visited. So they will all be
# visited 9001 times.
#
#
#
#
# Example 2:
# Input:
# ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
# Output:
# ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 i
# ntel.mail.com","951 com"]
# Explanation:
# We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.c
# om" once and "wiki.org" 5 times. For the subdomains, we will visit "mail.com" 90
# 0 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
#
#
#
#  Notes:
#
#
#  The length of cpdomains will not exceed 100.
#  The length of each domain name will not exceed 100.
#  Each address will have either 1 or 2 "." characters.
#  The input count in any count-paired domain will not exceed 10000.
#  The answer output can be returned in any order.
#
#  Related Topics Hash Table
#  👍 608 👎 766

# region time
# 2021-01-06 22:32:33
# endregion

# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def subdomainVisits(self, cpdomains):
        links = {}
        for i in cpdomains:
            num, link = i.split(' ')
            link = link.split('.')
            for j in range(len(link)):
                web = '.'.join(link[j:len(link)])
                if web in links:
                    links[web] += int(num)
                else:
                    links[web] = int(num)
        return [str(links[key]) + ' ' + key for key in links]

# leetcode submit region end(Prohibit modification and deletion)

if __name__ == '__main__':
    n = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
    print(Solution().subdomainVisits(n))


from r_steenrod_algebra import *
from r_polynomial import *
from r_monomial import *
from wrapper import *
from E2_page import *
import time

options = Options(
    PickleStorage("real_comm_db.pickle"),
    PickleStorage("real_adem_db.pickle"),
    "t", "p", "t*", "p*",
    { "t" : (0,1), "p" : (1,1), "u" : (1, 1), "t*" : (0, -1),
      "p*" : (-1, -1), "u*" : (-1, -1) }, "Real", (20, 10),
    "real_log.log", logging.WARNING,
    "real_resolution.pickle",
    "real_resolution_no_mat.pickle",
    "real_dual_resolution.pickle",
    "real_a_star",
    8)

myss = MASS(options)
myss.set_degree_bounds((30, 15))
#res = myss.get_resolution()
#myss.make_no_mat_resolution()
#myss.make_dual_resolution()

#res.map_printout("real_map_printout.txt", myss.get_options())
#myss.get_E_2_page().compute_product_structure(myss.get_options())
#myss.get_E_2_page_no_mat().product_printout("real_prod_printout.txt", myss.get_options())
#myss.cohomology_printout("real_cohom_printout.txt")

#myss.make_charts_with_mat()
#fixed_weight_plot(myss.get_E_2_page_no_mat(), -6, myss.get_options())
#myss.get_printout("real_printout.txt")

myss.make_isaksen_chart()


def get_bucket(self, bucket_name, validate=True, headers=None, force=None):
        """Return a bucket from MimicDB if it exists. Return a
        S3ResponseError if the bucket does not exist and validate is passed.

        :param boolean force: If true, API call is forced to S3
        """
        if force:
            bucket = super(S3Connection, self).get_bucket(bucket_name, validate, headers)
            mimicdb.backend.sadd(tpl.connection, bucket.name)
            return bucket

        if mimicdb.backend.sismember(tpl.connection, bucket_name):
            return Bucket(self, bucket_name)
        else:
            if validate:
                raise S3ResponseError(404, 'NoSuchBucket')
            else:
                return Bucket(self, bucket_name)


from logging import exception
import mysql.connector as c
conn = c.connect(host='localhost',user='root',passwd='',database='Python_Database')
cursor = conn.cursor()
createQuery ="""
create table student(
    name varchar(10),
    roll integer(3),
    id integer primary key
);
"""
try:
    cursor.execute(createQuery)
    print("Table Created Sucessfully...")
except Exception as ex:
    print(ex)
finally:
    if cursor:
        cursor.close()
    if conn:
        conn.close()


import collections

class Solution(object):
    def maxDistance(self, grid):
        """
        :type grid: List[Lis
        t[int]]
        :rtype: int

         [[1,0,0],
          [0,0,0],
          [0,0,0]]

        Step 0. Store all the maximum distances in a variable

        Step 1. Iterate through the grid
            Step 1.1 If point is water, BFS and find the closest water cell
            Step 1.2 If that curr distance is greater than max_distance, update max_distance

        return max_distance

         [0,0,1,1,1],
         [0,1,1,0,0],
         [0,0,1,1,0],
         [1,0,0,0,0],
         [1,1,0,0,1]

        """
        m,n = len(grid), len(grid[0])
        q = collections.deque([(i,j) for i in range(m) for j in range(n) if grid[i][j] == 1])
        if len(q) == m * n or len(q) == 0: return -1
        level = 0
        moves = [(0,1), (0,-1), (1,0), (-1,0)]
        while q:
            size = len(q)
            for _ in range(size):
                curr_row, curr_col = q.popleft()
                for move in moves:
                    new_row, new_col = curr_row + move[0], curr_col + move[1]
                    if not self.is_invalid(grid, new_row, new_col) and grid[new_row][new_col] == 0:
                        q.append((new_row, new_col))
                        grid[new_row][new_col] = 1

            for row in grid:
                print(row)
            print('*'*30)
            level += 1

        return level - 1

    def is_invalid(self, grid, row, col):
        if row < 0 or col < 0 or row == len(grid) or col == len(grid[0]):
            return True
        return False

grid = [[1,0,1],[0,0,0],[1,0,1]]
s = Solution()
s.maxDistance(grid)

Algorithms/Leetcode/1162 - As Far from Land as Possible.py


# 버블정렬 -> 이웃한 두 원소의 대소관계를 비교하여 필요에 따라 교환하는 알고리즘
# 이웃한 두 원소로만 교환을 하기 때문에 안정성이 좋음
# 일련의 교환.과정을 "패스" 라고 칭함.

# 버블 정렬 (배열이 정해져 있을 때)
def bubble_sort(a):
  n = len(a)
  for i in range(n-1):
    for j in range(n-1,i,-1):
      if a[j-1] > a[j]:
        a[j-1], a[j] = a[j], a[j-1]

a = [3,1,475,623,472,57,45,73,568,346,24362,47,46,345]
bubble_sort(a)
print(a)

# 버블 정렬 (배열이 정해져 있지 않을 때)
def bubble_sort(a):
  n = len(a)
  for i in range(n-1):
    exchange = 0
    for j in range(n-1,i,-1):
      if a[j-1] > a[j]:
        a[j-1], a[j] = a[j], a[j-1]
        exchange += 1
    if exchange == 0:     # 패스 값이 0이라면, 이미 배열이 완료되어 있다는 뜻이므로 break
      break
n = int(input("원소 수를 입력해주세요: "))
x = [None] * n

for i in range(n):
  x[i] = int(input(f"x[{i}] = "))

bubble_sort(x)

for i in range(n):
  print(f"x[{i}] = {x[i]}", end="   ")

# 버블 정렬 알고리즘 개선
def bubble_sort(a):
  n = len(a)
  k = 0
  while k < n-1:
    last = n - 1
    for j in range(n - 1, k, -1):
      if a[j-1] > a[j]:
        a[j-1], a[j] = a[j], a[j-1]
        last = j          # 마지막으로 바꾼 값이 j번째 이므로, 그 앞은 이미 정렬이 되어 있기 때문에
    k = last              # 다음 패스를 시작할 때 k = last(k를 마지막으로 바꾼 곳)로 설정하고 n-1부터 k 까지 -1씩 가는 것.

n = int(input("원소 수를 입력해주세요: "))
x = [None] * n

for i in range(n):
  x[i] = int(input(f"x[{i}] = "))

bubble_sort(x)

for i in range(n):
  print(f"x[{i}] = {x[i]}")

정렬함수/1.버블 정렬.py


from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
from keras.preprocessing import image
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input
from .forms import RecordForm
import numpy as np
from .apps import MalariadiseaseConfig
from keras import backend as K
from django.contrib import messages
from django.core.files.storage import FileSystemStorage
# Create your views here.

img_height, img_width=224,224

def form_handler(request):
	form = RecordForm()
	context = {}

	if request.method == 'POST':
		form = RecordForm(request.POST,request.FILES)

		if form.is_valid():

			img = form.cleaned_data.get("image")
			image = load_img(img,target_size=(224,224))

			fileObj=request.FILES['image']
			fs=FileSystemStorage()
			filePathName=fs.save(fileObj.name,fileObj)
			context['image'] = fs.url(filePathName)

			arr = img_to_array(image)
			arr = preprocess_input(arr)
			arr = np.expand_dims(arr,axis=0)

			with MalariadiseaseConfig.model_graph.as_default():
				with MalariadiseaseConfig.tf_session.as_default():
					y_pred = MalariadiseaseConfig.mdl.predict(arr)
			K.clear_session()
			y_pred = np.argmax(y_pred)

			for key in MalariadiseaseConfig.classes:
				if MalariadiseaseConfig.classes[key] == y_pred:
					answer = key
			messages.success(request,'Application Status: {}'.format(answer))

	context['form'] = form

	return render(request, 'malariadisease/myform.html',context)


# APERTIF PARSET GENERATOR ATDB VERSION 1.5 (atdb_parsets.py) - now with ARTS
# Input: source text file
# V.A. Moss 19/12/2018 (vmoss.astro@gmail.com)
__author__ = "V.A. Moss"
__date__ = "$07-jun-2019 17:00:00$"
__version__ = "1.5.1"

import os
import sys
from modules.beamcalc import *
from astropy.io import ascii
import numpy as np
from argparse import ArgumentParser, RawTextHelpFormatter
from modules.functions import *
from modules.drift import calc_drift
from datetime import datetime,timedelta
import time
import getpass

def main():
	"""
	The main program to be run.
	:return:
	"""

	# Time the total process length
	start = time.time()

	# Parse the relevant arguments
	parser = ArgumentParser(formatter_class=RawTextHelpFormatter)
	parser.add_argument('-f', '--filename',
		default='input/Emily Petroff - sc4_20191118.csv',
		help='Specify the input file location (default: %(default)s)')
	parser.add_argument('-m', '--mode',
		default='sc4',
		type = str.lower,
		help='Specify whether mode is imaging/sc1/sc4 (default: %(default)s)')
	parser.add_argument('-t', '--telescopes',
		default='23456789',
		help='Specify which telescopes to include (default: %(default)s)')
	parser.add_argument('-u', '--upload',
		default=False,
		action='store_true',
		help='Specify whether to automatically upload to wcudata1 (default: %(default)s)')
	parser.add_argument('-p', '--parset_only',
		default=False,
		action='store_true',
		help='Specify whether to only make a parset and not submit it (default: %(default)s)')
	parser.add_argument('-v', '--verification',
		default=False,
		action='store_true',
		help='Specify whether to send a verification/test observation for specified mode (default: %(default)s)')
	parser.add_argument('-a', '--artsmode',
		default='TAB',
		help='Specify which mode to record for ARTS SC4, either incoherent IAB or tied-array TAB (default: %(default)s)')
	parser.add_argument('-n', '--numofobs',
		default=4,
		type = int,
		help='Specify how many verification observations to do, in imaging mode (default: %(default)s)')
	parser.add_argument('-s', '--selobs',
		default=None,
		type = int,
		help='Specify which verification observation to do, in imaging mode (default: %(default)s)')
	parser.add_argument('-c', '--centfreq',
		default=1370,
		type = int,
		help='Specify which central frequency, in imaging mode (default: %(default)s)')
	parser.add_argument('-b', '--beams',
		default='[0..39]',
		type = str,
		help='Specify which CBs to record in ARTS SC4  mode (default: %(default)s)')
	parser.add_argument('-i', '--skipingest',
		default=False,
		action='store_true',
		help='Specify whether to skip auto ingest(default: %(default)s)')
	parser.add_argument('-r', '--refbeam',
		default='0',
		type = str,
		help='Specify which reference beam to use (default: %(default)s)')
	parser.add_argument('-d', '--duration',
		default=None,
		type = int,
		help='Specify non-standard duration of imaging verification observations (default: %(default)s)')

	# Parse the arguments above
	args = parser.parse_args()

	# Weight pattern dictionary
	weightdict = {'compound': 'square_39p1',
				  'XXelement': 'central_element_beams_x',
				  'YYelement': 'central_element_beams_y',
				  'XXelement40': 'central_element_beams_x',
				  'YYelement40': 'central_element_beams_y',
				  'hybrid': 'hybridXX_20180928_8bit',
				  'compound_element_x_subset' : 'compound_element_x_subset'}

	# Expected telescopes
	scopesdict = {'imaging':'23456789ABCD',
				  'sc1': '23456789',
				  'sc4': '23456789'}

	# Check if default doesn't match and warn user
	expected_scopes = scopesdict[args.mode]
	if args.telescopes != expected_scopes:
		print('WARNING!!!\nExpected telescopes: %s\nSpecified telescopes: %s\n' % (expected_scopes,args.telescopes))

		try:
			# Python 3
			proceed = input('Do you want to proceed? (y/n) ')
		except:
			# Python 2
			proceed = raw_input('Do you want to proceed? (y/n) ')

		if proceed != 'y':
			print('... exiting!')
			sys.exit()
		else:
			print('Okay, I will continue with the telescopes you specified...')

	# Initialise the class to store variables
	obs = Observation()
	obs.telescopes = args.telescopes

	# This determines whether to use the system offset for calculating offset beams or not
	system_offset = False # default should be True when we completely trust the system

	# specify the filename
	fname = args.filename

	# offset (if local time is specified in the parset)
	# This should depreciate when we trust the input specification
	offset = 0 # hours

	# parsetonly string
	if args.parset_only:
		#parsetonly = '--parset_only'
		obs.parsetonly = '--parset_only '
	else:
		#parsetonly = ''
		obs.parsetonly = ''

	# Decide about whether to skip ingest or not
	if args.skipingest == True:
		obs.skipingest = '--skip_auto_ingest '
	else:
		obs.skipingest = ''

	# Consider also ARTS mode and beams
	obs.artsmode = args.artsmode
	obs.beams = args.beams

	# Verification observation
	if args.verification:
		obs.numofobs = args.numofobs
		obs.selobs = args.selobs
		obs.centfreq = args.centfreq
		obs.refbeam = args.refbeam
		obs.duration = args.duration
		out,outname = make_verification(obs,args.mode)
		obs.out = out
		obs.outname = outname

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

	else:
		# Read file (either tab or comma separated)
		try:
			d = ascii.read(fname,delimiter=',',guess=False)
		except:
			d = ascii.read(fname,delimiter='\t',guess=True)

		print(list(d.keys()))

		# Start the file
		outname = '%s_%s.sh' % (fname.split('.csv')[0],args.mode)
		out = open(outname,'w')
		out.write(make_header())
		out.flush()

		# Add to the class definition
		obs.out = out
		obs.outname = outname

		# Loop through sources
		for i in range(0,len(d)):

			# Check for delay offset
			if 'delayoffset' in d.keys():
				obs.delayoffset = '--delay_center_offset=%s ' % d['delayoffset'][i]
			else:
				obs.delayoffset = ''

			# Check for template specification
			if 'template' in d.keys():
				obs.template = '--parset_location=%s ' % d['template'][i]
			else:
				obs.template = ''

			# Get the common parameters for all
			#src = d['source'][i]
			obs.src = d['source'][i]

			# Get the pieces of date time
			stime = d['time1'][i]
			sdate = d['date1'][i]

			# Fix times if they aren't the right length
			if len(stime.split(':')[0]) < 2:
				stime = '0'+stime

			# Form the datetime object
			try:
				sdate_dt = datetime.strptime(sdate+stime,'%Y-%m-%d%H:%M:%S')
			except ValueError:
				sdate_dt = datetime.strptime(sdate+stime,'%Y-%m-%d%H:%M')
			sdate_dt = sdate_dt + timedelta(hours=offset)

			# Endtime or duration
			if 'time2' in d.keys():
				etime = d['time2'][i]
				edate = d['date2'][i]

				# Fix times if they aren't the right length
				if len(etime.split(':')[0]) < 2:
					etime = '0'+etime

				# Form the datetime object
				try:
					edate_dt = datetime.strptime(edate+etime,'%Y-%m-%d%H:%M:%S')
				except ValueError:
					edate_dt = datetime.strptime(edate+etime,'%Y-%m-%d%H:%M')
				edate_dt = edate_dt + timedelta(hours=offset)

				# Check for mistaken date
				if edate_dt <= sdate_dt:
					print('End date is further in the past than start date... adding a day!')
					edate_dt = edate_dt + timedelta(days=1)

				# Added by LO
				duration = int((edate_dt - sdate_dt).total_seconds())

				# nasty duration hack to avoid crazy values (LO)
				# Note from VM: I think this depreciates with proper datetime objects
				# while duration > 86400:
				# 	duration -= 86400
				# if duration < 0:
				# 	duration = 86400 + duration

			elif 'duration' in d.keys() and d['duration'][i] != '-':
				edate_dt = sdate_dt + timedelta(seconds=float(d['duration'][i]))
				etime = str(edate_dt.time())
				duration = d['duration'][i]
			else:
				edate_dt = None
				duration = None

			# Assign the results to the class
			obs.sdate = sdate_dt
			obs.edate = edate_dt
			obs.duration = duration

			# Check the duration, and print a warning if <= 0
			if obs.duration <= 0:
				print('############\nWARNING: Negative duration for %s (row %i): %s seconds!' % (obs.src,i+1,obs.duration))
				print('It is recommended that you check the input and contact science teams if necessary...\n############')
				try:
					ack = raw_input('Please type "y" to acknowledge this warning and continue anyway: ')
				except:
					ack = input('Please type "y" to acknowledge this warning and continue anyway: ')

				if ack != 'y':
					print('You did not type "y" so I am exiting out of concern...')
					sys.exit()

			# Define the obs type (not needed really?)
			src_obstype = obs.obstype

			# Define the process type
			if 'process_type' in d.keys():
				obs.processtype = '--process_type=%s ' % d['process_type'][i]
			else:
				obs.processtype = '--process_type=science '

			# Observing mode
			if args.mode == 'sc4':
				#observing_mode = 'arts_sc4_survey'
				#start_beam = d['sbeam'][i]
				#end_beam = d['ebeam'][i]
				#pulsar = d['pulsar'][i]

				# Class replacements
				obs.obsmode = 'arts_sc4_survey'
				obs.sbeam = d['sbeam'][i]
				obs.ebeam = d['ebeam'][i]
				obs.pulsar = d['pulsar'][i]

			elif args.mode == 'sc1':

				if d['mode'][i] == 'timing':
					#observing_mode = 'arts_sc1_timing'
					obs.obsmode = 'arts_sc1_timing'
				elif d['mode'][i] == 'baseband':
					#observing_mode = 'arts_sc1_baseband'
					obs.obsmode = 'arts_sc1_baseband'

					# for arts_sc1_baseband no dataproducts are created for ALTA
					# add the --skip_auto_ingest flag to let baseband observations end in a 'completed' state in ATDB.
					obs.skipingest = '--skip_auto_ingest '

			#	sband = d['sband'][i]
		#		eband = d['eband'][i]
	#			parfile = d['par'][i]

				# Class replacements
				obs.sband = d['sband'][i]
				obs.eband = d['eband'][i]
				obs.parfile = d['par'][i]

			else:
				#observing_mode = 'imaging'

				# Class replacements
				obs.obsmode = 'imaging'

			# Get ref beam
			try:
				#refbeam = d['beam'][i]

				# Class replacements
				obs.refbeam = d['beam'][i]
			except:
				#refbeam = '0'

				# Class replacements
				obs.refbeam = args.refbeam

			# Determine the integration factor in seconds
			try:
				#ints = d['int'][i]
				obs.intfac = d['int'][i]
			except:
				if args.mode == 'sc4':
					#ints = 30
					obs.intfac = 30
				elif args.mode == 'sc1':
					#ints = 20
					obs.intfac = 20

			# Add the SC1 template no matter what
			if args.mode == 'sc1':
				obs.template = '--parset_location=/opt/apertif/share/parsets/parset_start_observation_atdb_arts_sc1.template '

			# Define weight pattern
			try:
				#weightpatt = weightdict[d['weight'][i]]
				obs.weightpatt = weightdict[d['weight'][i]]
			except:
				#weightpatt = 'square_39p1'
				obs.weightpatt = 'square_39p1'

			# Try to find central frequency
			if 'centfreq' in d.keys():
				#centfreq = int(d['centfreq'][i])
				obs.centfreq = int(d['centfreq'][i])
			else:
				#centfreq = 1400
				obs.centfreq = 1370 # new default

			# Parse the Position coordinates (accounting now for ha)
			# note that HA is stored as RA in the Obs class, even if it is HA
			#hadec = ''
			obs.hadec = ''

			try:
				if 'ha' in d.keys() and d['ha'][i] != '-':

					print('Detecting an HADEC observation!')
					ra = float(d['ha'][i])
					dec = float(d['dec'][i])

					obs.ratype = 'field_ha'

				else:
					ra = float(d['ra'][i])
					dec = float(d['dec'][i])
					obs.ratype = 'field_ra'
			except:
				if 'ha' in d.keys() and d['ha'][i] != '-':
					print('Detecting an HADEC observation!')
					try:
						ra = float(d['ha'][i])
						dec = float(d['dec'][i])
					except:
						ra = float(ra2dec(d['ha'][i]))
						dec = float(dec2dec(d['dec'][i]))
					#hadec = '--parset_location=/opt/apertif/share/parsets/parset_start_observation_driftscan_atdb.template '
					#obs.hadec = '--parset_location=/opt/apertif/share/parsets/parset_start_observation_driftscan_atdb.template '

					obs.ratype = 'field_ha'

				elif d['ra'][i] == '-':
					print('No coordinates specified... maybe a pointing observation?')
					ra = d['ra'][i]
					dec = d['dec'][i]

				elif 'deg' in d['ra'][i]:
					ra = float(d['ra'][i].split('deg')[0])
					dec = float(d['dec'][i].split('deg')[0])
					obs.ratype = 'field_ra'

				# With :
				elif ':' in d['ra'][i]:
					ra = ra2dec(d['ra'][i])
					dec = dec2dec(d['dec'][i])
					obs.ratype = 'field_ra'

				# With HMS
				elif 'h' in d['ra'][i]:
					ra = ra2dec(d['ra'][i].replace('h',':').replace('m',':').replace('s',''))
					dec = dec2dec(d['dec'][i].replace('d',':').replace('m',':').replace('s',''))
					obs.ratype = 'field_ra'

				else:
					print('Error parsing coordinates!')
					sys.exit()

			# Assign these to the class
			obs.ra = ra
			obs.dec = dec
			obs.extra = ''

			# Imaging specific things
			if args.mode == 'imaging':
				#src_obstype = d['type'][i]
				obs.obstype = d['type'][i]

				if 'freqmode' in d.keys():
					if d['freqmode'][i] == 300:
						#extra = '--end_band=24'
						obs.extra = '--end_band=24 '
					elif d['freqmode'][i] == 200:
						#extra = ''
						obs.extra = ''
				else:
					#extra = '--end_band=24'
					obs.extra = '--end_band=24 '

				# Go into pointing mode
				if obs.obstype == 'P':

					print('Pointing observation identified!')

					# Send the relevant data to the pointing function
					#observing_mode = 'imaging_pointing'
					obs.obsmode = 'imaging_pointing'
					obs.processtype = '--process_type=pointing '

					#make_pointing(sdate_dt,edate_dt,ints,weightpatt,out,args.telescopes,observing_mode,parsetonly,hadec)
					make_pointing(obs)

					# We don't want to proceed with the code once the pointing is done!
					break

				elif obs.obstype == 'O':
					print('Operations tests mode identified!')

					# Determine if offset beam is chosen or random
					if str(obs.refbeam) != '0':
						offbeam = int(obs.refbeam)
					else:
						offbeam = randint(1,40)

					beamname = 'B0%.2d' % offbeam
					beams = [0,offbeam]#,0]
					ra_new1,dec_new1 = calc_pos_compound(obs.ra,obs.dec,beamname)
					ra_new2,dec_new2 = calc_pos(ra,dec,beamname)
					ras = [obs.ra,obs.ra,[ra_new1,ra_new2]]
					decs = [obs.dec,obs.dec,[dec_new1,dec_new2]]
					names = [obs.src,obs.src + '_%i' % offbeam,obs.src + '_%i' % offbeam]
					patterns = [weightdict['compound'],weightdict['XXelement']]#,weightdict['XXelement']]#, weightdict['YYelement']]
					generate_tests(names,ras,decs,patterns,beams,obs)

					break

				elif obs.obstype == 'D' or obs.obstype == 'D*':
					print('Drift mode identified!')

					# Single drift through a row
					if obs.obstype == 'D':
						try:
							n_drift = d['n_drift'][i]
						except:
							n_drift = 1

						ha,duration = calc_drift((obs.ra,obs.dec),obs.sdate,n_drift)
						print(ra2dec(ha),duration)

						# Deal with ref beam
						beamname = 'B0%.2d' % obs.refbeam
						ra_new1,dec_new1 = calc_pos_compound(obs.ra,obs.dec,beamname)
						offset = '%+.2d' % ((obs.dec - dec_new1)*60.)

						# Change the variables
						obs.ratype='field_ha'
						obs.ra = ra2dec(ha) # Note: it intentionally goes to ra!
						obs.duration = duration
						obs.edate = sdate_dt + timedelta(seconds=int(duration))
						obs.src = obs.src+'drift'+offset

						# Write to the outfile
						writesource_imaging(obs)

					elif obs.obstype == 'D*':
						refbeams = [0,7,14,20,26,32,39]
						nbeams = [1,7,7,6,6,6,7]
						currdate_dt = obs.sdate
						truename = obs.src
						truera = obs.ra
						truedec = obs.dec

						for ii in range(0,len(refbeams)):

							obs.refbeam = refbeams[ii]
							n_drift = nbeams[ii]
							obs.ra = truera
							obs.dec = truedec

							#print(obs.ra,obs.dec)
							ha,duration = calc_drift((truera,truedec),currdate_dt,n_drift)
							print(ra2dec(ha),duration)

							# Change the variables
							obs.ratype='field_ha'
							obs.ra = ra2dec(ha) # Note: it intentionally goes to ra!
							obs.duration = duration
							obs.sdate = currdate_dt
							obs.edate = currdate_dt + timedelta(seconds=int(duration))

							# Deal with ref beam
							beamname = 'B0%.2d' % obs.refbeam
							ra_new1,dec_new1 = calc_pos_compound(obs.ra,obs.dec,beamname)
							offset = '%+.2d' % ((obs.dec - dec_new1)*60.)
							obs.src = truename+'drift'+offset

							# Write to the outfile
							writesource_imaging(obs)

							# update the time
							currdate_dt = obs.edate + timedelta(seconds=120)

					continue

				# System offset stuff
				if d['switch_type'][i] == 'system':
					#system_offset = True
					obs.systemoffset = True

				elif d['switch_type'][i] == 'manual':
					#system_offset = False
					obs.systemoffset = False

					if 'S' not in src_obstype:
						beamname = 'B0%.2d' % refbeam
						if d['weight'][i] == 'XXelement' or d['weight'][i] == 'YYelement':
							ra_new,dec_new = calc_pos(obs.ra,obs.dec,beamname)
						elif d['weight'][i] == 'compound':
							ra_new,dec_new = calc_pos_compound(obs.ra,obs.dec,beamname)
						else:
							print (weightpatt)
						#print(beamname,ra_new,dec_new,ra,dec)
						obs.ra,obs.dec = ra_new,dec_new
						obs.refbeam = '0'

				elif d['switch_type'][i] == '-' or d['switch_type'][i] == -1.0:
					print('No switching!')
				else:
					print('Switch type error!')
					sys.exit()

			# Account for beam switching (imaging only)
			if obs.obstype and 'S' in obs.obstype:
				make_beamswitch(obs)

			# Standard observation otherwise
			else:

				# Write sources to file
				if args.mode == 'imaging':
					writesource_imaging(obs)

				elif args.mode == 'sc4':

					# # Reset the tid if needed
					# if str(old_edate) != str(date) and old_edate != None:
					# 	start_tid = 1
					# 	start_tnum = 0

					writesource_sc4(obs)

				elif args.mode == 'sc1':
					writesource_sc1(obs)

	# Close the outfile
	out.close()

	# Make the resultting file executables
	os.system('chmod oug+x "%s"' % obs.outname)

	if args.upload:

		# Upload the file automatically to wcudata1
		# Note: this assumes you have ssh key forwarding activated for apertif user account
		cmd = "rsync -avzP %s apertif@wcudata1.apertif:~/atdb_client/scripts/" % obs.outname
		os.system(cmd)

		#if args.mode == 'SC4':

			# Also do the same for SC4 cluster
			# Note: this assumes you have ssh key forwarding activated for arts user account
			#cmd = 'rsync -avzP %s arts@arts041.apertif:~/observations/scripts/' % outname2
			#os.system(cmd)

if __name__ == '__main__':
	main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
#django library
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect
from django.views.generic import (
    CreateView,
    UpdateView,
    DetailView,
    DeleteView,
    ListView,
    TemplateView,
    View,
)
from django.views.generic.edit import FormView

#from app itinerario
from applications.itinerario.models import Itinerary
from applications.blog.models import Blog
from applications.home.models import Home
#
from .models import Service
from .forms import SearchServiceForm

class ClasicToursView(ListView):
    """ vista que lista tours clasicos """

    context_object_name = 'servicios'
    template_name = 'servicios/tour_clasicos.html'

    def get_queryset(self):
        #recuperamos el valor por GET
        queryset = Service.objects.service_by_tipe('0','')
        return queryset

class AlternativeWalksView(ListView):
    """ vista que lista camintas alternativas """

    context_object_name = 'servicios'
    template_name = 'servicios/caminatas_alternativas.html'

    def get_context_data(self, **kwargs):
        context = super(AlternativeWalksView, self).get_context_data(**kwargs)
        #contexto principal
        context['form'] = SearchServiceForm
        return context

    def get_queryset(self):
        #recuperamos el valor por GET
        q = self.request.GET.get("kword", '')
        #consultas
        queryset = Service.objects.service_by_tipe('2','')
        proyectos = Blog.objects.list_by_category('','3').order_by('-created')[:4]
        return {'queryset':queryset, 'proyectos':proyectos}

class CaminoIncaView(ListView):
    """ vista que lista servicios camino inca"""

    context_object_name = 'servicios'
    template_name = 'servicios/camino_inka.html'

    def get_context_data(self, **kwargs):
        context = super(CaminoIncaView, self).get_context_data(**kwargs)
        #contexto principal
        context['form'] = SearchServiceForm
        return context

    def get_queryset(self):
        #recuperamos el valor por GET
        q = self.request.GET.get("kword", '')
        #consultas
        queryset = Service.objects.service_by_tipe('1',q)
        proyectos = Blog.objects.list_by_category('','3').order_by('-created')[:4]
        return {'queryset':queryset, 'proyectos':proyectos}

class ServicesListView(ListView):
    """ vista que lista todos los servicios"""

    context_object_name = 'servicios'
    template_name = 'servicios/list.html'

    def get_context_data(self, **kwargs):
        context = super(ServicesListView, self).get_context_data(**kwargs)
        #contexto principal
        context['form'] = SearchServiceForm
        context['home'] = Home.objects.order_by('-created')[0]
        return context

    def get_queryset(self):
        #recuperamos el valor por GET
        q = self.request.GET.get("kword", '')
        #consultas
        queryset = Service.objects.service_by_tipe('3',q)
        return queryset

class ServiceDetailView(DetailView):
    model = Service
    template_name = 'servicios/detail.html'

    def get_context_data(self, **kwargs):
        context = super(ServiceDetailView, self).get_context_data(**kwargs)
        #contexto principal
        itinerarios = Itinerary.objects.filter(
            service=self.get_object(),
        ).order_by('denomination')
        #
        context['itinerario'] = itinerarios
        context['proyectos'] = Blog.objects.filter(
            category__model='3',
            published=True,
        ).order_by('-created')[:4]
        return context


import numpy as np

class ReplayExperiences:

    def __init__(self, capacity=1000000, batch_size=32):
        self.capacity = capacity
        self.batch_size = batch_size
        self.buffer_idx = 0
        self.num_experiences = 0

        self.actions = np.empty(self.capacity, dtype=np.int32)
        self.states = np.empty(self.capacity, dtype=np.ndarray)
        self.next_states = np.empty(self.capacity, dtype=np.ndarray)
        self.rewards = np.empty(self.capacity, dtype=np.float32)
        self.episode_status = np.empty(self.capacity, dtype=np.float32)

    def add(self, action, state, next_state, reward, done):
        self.actions[self.buffer_idx] = action
        self.states[self.buffer_idx] = state
        self.next_states[self.buffer_idx] = next_state
        self.rewards[self.buffer_idx] = reward
        self.episode_status[self.buffer_idx] = done
        self.buffer_idx = (self.buffer_idx + 1) % self.capacity
        self.num_experiences = min(self.capacity, self.num_experiences + 1)

    def sample(self):
        indices = np.random.choice(range(self.num_experiences), size=self.batch_size)

        actions = np.array([self.actions[i] for i in indices])
        states = np.array([self.states[i] for i in indices])
        next_states = np.array([self.next_states[i] for i in indices])
        rewards = np.array([self.rewards[i] for i in indices])
        dones = np.array([self.episode_status[i] for i in indices])

        return actions, states, next_states, rewards, dones


#!/usr/bin/env python

# SYSTEM IMPORTS
import unittest
import rospy
import rostest
import sys
import os

currentDir = os.path.dirname(os.path.realpath(__file__))
scriptsDir = os.path.abspath(os.path.join(currentDir, "..", ".."))
sys.path.extend([currentDir, scriptsDir])

# PYTHON PROJECT IMPORTS
import NaiveBayesClassifier
import TestFunctors

class NaiveBayesClassifierTest(unittest.TestCase):

    def test_constructor(self):
        testClassifier = NaiveBayesClassifier.NaiveBayesClassifier("lion", TestFunctors.BooleanFunctor(),
            {"has-fur?": TestFunctors.BooleanFunctor(),
            "long-teeth?": TestFunctors.BooleanFunctor(),
            "scary?": TestFunctors.BooleanFunctor()})

        self.assertEqual("lion", testClassifier.classificationName)
        self.assertEqual(TestFunctors.BooleanFunctor(), testClassifier.classificationStateFunctor)
        self.assertEqual(4, len(testClassifier.nodes))
        for featureName in ["has-fur?", "long-teeth?", "scary?"]:
            self.assertTrue(featureName in testClassifier.featureNames)

    def test_train(self):
        testClassifier = NaiveBayesClassifier.NaiveBayesClassifier("lion", TestFunctors.BooleanFunctor(),
            {"has-fur?": TestFunctors.BooleanFunctor(),
            "long-teeth?": TestFunctors.BooleanFunctor(),
            "scary?": TestFunctors.BooleanFunctor()})

        sampleTrainingData = [
            {"has-fur?":True, "long-teeth?":False, "scary?":False, "lion": False},
            {"has-fur?":False, "long-teeth?":True, "scary?":True, "lion": False},
            {"has-fur?":True, "long-teeth?":True, "scary?":True, "lion": True}
        ]
        testClassifier.train(sampleTrainingData)

        lionNode = testClassifier.nodes["lion"]
        hasFurNode = testClassifier.nodes["has-fur?"]
        longTeethNode = testClassifier.nodes["long-teeth?"]
        scaryNode = testClassifier.nodes["scary?"]
        conditionalsTable = [[0.5, 0.5], [0, 1]]
        self.assertEqual([0.67, 0.33], [round(x, 2) for x in lionNode.table[0]])
        self.assertEqual(conditionalsTable, [[round(y, 2) for y in x] for x in hasFurNode.table])
        self.assertEqual(conditionalsTable, [[round(y, 2) for y in x] for x in longTeethNode.table])
        self.assertEqual(conditionalsTable, [[round(y, 2) for y in x] for x in scaryNode.table])

    def test_prOfState(self):
        testClassifier = NaiveBayesClassifier.NaiveBayesClassifier("lion", TestFunctors.BooleanFunctor(),
            {"has-fur?": TestFunctors.BooleanFunctor(),
            "long-teeth?": TestFunctors.BooleanFunctor(),
            "scary?": TestFunctors.BooleanFunctor()})
        testClassifier.nodes["lion"].table = [(0.9, 0.1)]
        testClassifier.nodes["has-fur?"].table = [(0.9, 0.1),
                                                  (0.5, 0.5)]
        testClassifier.nodes["long-teeth?"].table = [(0.5, 0.5),
                                                     (0.1, 0.9)]
        testClassifier.nodes["scary?"].table = [(0.5, 0.5),
                                                (0.2, 0.8)]

        featureVector = {"has-fur?": True, "long-teeth?": False, "scary?": False}
        self.assertEqual(0.001, round(testClassifier.prOfState(featureVector, True), 3))
        self.assertEqual(0.0225, round(testClassifier.prOfState(featureVector, False), 4))

    def test_classify(self):
        testClassifier = NaiveBayesClassifier.NaiveBayesClassifier("lion", TestFunctors.BooleanFunctor(),
            {"has-fur?": TestFunctors.BooleanFunctor(),
            "long-teeth?": TestFunctors.BooleanFunctor(),
            "scary?": TestFunctors.BooleanFunctor()})
        testClassifier.nodes["lion"].table = [(0.9, 0.1)]
        testClassifier.nodes["has-fur?"].table = [(0.1, 0.9),
                                                  (0.5, 0.5)]
        testClassifier.nodes["long-teeth?"].table = [(0.5, 0.5),
                                                     (0.1, 0.9)]
        testClassifier.nodes["scary?"].table = [(0.5, 0.5),
                                                (0.2, 0.8)]

        self.assertEqual([False], testClassifier.classify(
            {"has-fur?": True, "long-teeth?": False, "scary?": False}))

if __name__ == "__main__":
    rostest.rosrun("pyclassifiers", "NaiveBayesClassifierTest", NaiveBayesClassifierTest)


from data_access.iris import load_iris_data
from neurons.adaline import Adaline
from plots.learning_results import plot_2d_decision_boundary, plot_error_history
from utils.data_transformation import standardize, binarize_class_labels

iris_df = load_iris_data()

versicolor_virginica = ["Iris-versicolor", "Iris-virginica"]
predictor_columns = ["sepal length", "petal length"]

ver_vir_df = iris_df[iris_df["class"].isin(versicolor_virginica)].copy()
ver_vir_df = binarize_class_labels(ver_vir_df)
ver_vir_df_s = standardize(ver_vir_df, columns=predictor_columns)

train_X = ver_vir_df_s[predictor_columns].values
train_y = ver_vir_df_s["binary class"].values

adaline = Adaline(n_iter=15, learning_rate=0.01)
adaline.fit(train_X, train_y)

# plot_error_history(adaline.cost_history_)
plot_2d_decision_boundary(ver_vir_df_s, adaline, support_vectors=True)


'''
/**
 * @author [Jai Miles]
 * @email [jaimiles23@gmail.com]
 * @create date 2020-01-29 10:38:19
 * @modify date 2020-01-29 10:38:19
 * @desc [
Per definition, a happy number is derived through brute force calculuations. These calculations only stop when the number is caught in an infinite loop,
i.e., the numbers are repeating each other.  This is an iterative brute force solution to determine the number

Results:
Runtime: 28 ms, faster than 87.55% of Python3 online submissions for Happy Number.
Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Happy Number.

Complexity analysis:
Time complexity:
O(N**2) - for loop embedded in a while loop. NOTE: per definition, I don't think you can be faster than O(N**2)
Space complexity:
O(N) - Store O(N) elements in set searched_nums, and O(1) elements for num, next_num, and searching flag.
 ]
 */
'''

class IterativeSolution:
    def isHappy(self, n: int) -> bool:
        # instantiate vars and flag
        num, next_num = n, 0
        searched_nums = {n}
        searching_happy = True

        while searching_happy:

            # Construct next_num
            for char in str(num):
                next_num += int(char) ** 2

            # Check Happy
            if str(next_num) == '1':
                return True

            # Check if already checked
            elif next_num in searched_nums:
                searching_happy = False

            # Reset vars for next check
            else:
                num, next_num = next_num, 0
                searched_nums.add(num)

        return False


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

# Imporar a base de dados
import pandas as pd

tabela = pd.read_csv("telecom_users.csv")

# Vizualizar a base de dados (Entender as informações que tenho disponíveis, descobrir as falhas da base de dados)
tabela  = tabela.drop("Unnamed: 0", axis=1)
display(tabela)

# Tratamento de dados (resolver as falhas da base de dados)
tabela["TotalGasto"] = pd.to_numeric(tabela["TotalGasto"], errors="coerce")

# Tratar as informações vazias
tabela = tabela.dropna(how="all", axis=1)

# Linhas com alguma (any) informação vazia
tabela = tabela.dropna(how="any", axis=0)

print(tabela.info())

# Análise inicial dos dados
print(tabela["Churn"].value_counts())
print(tabela["Churn"].value_counts(normalize=True).map("{:.1%}".format))

# Descobrir os motivos do cancelamento
#!pip install plotly
# Importando biblioteca para gráficos
import plotly.express as px
#Criando um gráfico pra cada coluna (IDClientes, Genero, Aposentado...)
for coluna in tabela.columns:
    # Etapa 1: Cria o gráfico: bar, line, histogram...
    grafico = px.histogram(tabela,x=coluna, color="Churn")

    # Etapa 2: exibe o gráfico
    grafico.show()


number =[1,3,5,7,9,3,8]
#first solution
num = sorted(number)[-1]
print(num)

def max(a):
    res = a[0]
    for i in a:
        if res < i:
            res = i
    return res

array = (1, 2, 3, 4, 5, 6)
print(max(array))


import dotenv
import json
import re
import os

from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import RegexpTokenizer
from pyspark import SparkContext
from pyspark.mllib.fpm import FPGrowth
from pyspark.streaming import StreamingContext

# load environment variables
dotenv.load_dotenv(dotenv_path='./.env', verbose=True)
HOST = os.getenv("HOST")
PORT = int(os.getenv("PORT"))

stopwords = set(stopwords.words("english"))
stemmer = PorterStemmer()
tokenizer = RegexpTokenizer(r'\w+')

filter_words = ["trade", "wars", "wars"]

# sampling function
def sampling(rdd):
    return rdd.sample(False, 0.5, 1)

# remove @, #, link, unicode char from tweet
def get_clean_tweet(tweet):
    json_tweet = json.loads(tweet)
    tweet_data = ''

    if ("extended_tweet" in json_tweet):
        tweet_data = json_tweet["extended_tweet"]["full_text"]
    else:
        tweet_data = json_tweet["text"]

    tweet_data = re.sub(r"#(\w+)", "", tweet_data)
    tweet_data = re.sub(r"@(\w+)", "", tweet_data)
    tweet_data = re.sub(r"https?:\/\/.*[\r\n]*", "", tweet_data)
    tweet_data = re.sub(" +", " ", tweet_data)
    tweet_data = tweet_data.encode('ascii', 'ignore')

    if ("retweet_count" in json_tweet):
        if tweet_data[:2] == "RT":
            tweet_data = tweet_data[5:]

    return tweet_data

# stem words in tweet
def get_stemmed_tweet(tweet):
    words = []

    tweet = tweet.lower()
    tokens = tokenizer.tokenize(tweet)
    for word in tokens:
        if word not in stopwords and word != '':
            words.append(stemmer.stem(word))

    return " ".join(words)

# get distinct words in tweet and remove filter words
# this function is used for itemset counting
def get_distinct_words(tweet):
    words = []

    for word in tweet.split(" "):
        if word not in words and word not in filter_words:
            words.append(word)

    return words

if __name__ == "__main__":
    # create Spark context
    spark_context = SparkContext("local[2]", "TwitterWords")
    spark_context.setLogLevel("ERROR")

    # create streaming object
    streaming_context = StreamingContext(spark_context, 15)
    stream_object = streaming_context.socketTextStream(HOST, PORT)

    # clean tweets
    lines = stream_object.map(lambda line: get_clean_tweet(line))
    lines.pprint(5)

    # stem tweets
    stemmed_lines = lines.map(lambda line: get_stemmed_tweet(line))
    stemmed_lines.pprint(5)

    # store to file for itemset counting
    stemmed_lines.repartition(1).saveAsTextFiles("./tweet_words/result")

    # create words from tweets
    words = stemmed_lines.flatMap(lambda line: line.split(" "))
    words.pprint(5)

    # count distict words
    pairs = words.map(lambda word: (word, 1))
    wordCounts = pairs.reduceByKey(lambda x, y: x + y)
    wordCounts = wordCounts.transform(lambda rdd: rdd.sortBy(lambda y: -y[1]))
    wordCounts.pprint(5)

    # start stream processing
    streaming_context.start()

    # wait stream processing then stop
    streaming_context.awaitTermination(65)
    streaming_context.stop(stopSparkContext=False)

    # read stored tweets
    data = spark_context.textFile("tweet_words/result-*/part-*")
    transactions = data.map(lambda line: get_distinct_words(line))

    # count itemsets
    model = FPGrowth.train(transactions, minSupport=0.05, numPartitions=10)
    result = model.freqItemsets().collect()
    for fi in result:
        print(fi)


import sys

sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline

n = int(input())
parent = [i for i in range(n + 1)]

def union(a, b):
    a = find(a)
    b = find(b)

    if a != b:
        parent[a] = b

def find(x):
    if parent[x] == x:
        return x
    parent[x] = find(parent[x])
    return parent[x]

if __name__ == '__main__':
    m = int(input())
    for i in range(n):
        city = list(map(int, input().split()))
        for j in range(n):
            if i == j:
                continue

            if city[j] == 1:
                union(i + 1, j + 1)

    route = list(map(int, input().split()))
    standard = find(route[0])
    flag = True
    for elem in route:
        if find(elem) != standard:
            flag = False
            break

    if flag:
        print("YES")
    else:
        print("NO")


# coding=UTF-8
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import csv, os, sys

if len(sys.argv) < 2:
    print("[Usage] python crawl_gpa.py [semester]")
    exit(1)

semester = sys.argv[1]
DATA_DIR = 'data'
BASE_URL = 'https://ntusweety.herokuapp.com'
gpa = [0.0, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0, 4.3]

with open( os.path.join(DATA_DIR , semester + '.csv'), newline='', encoding = "UTF-8") as csvfile:
    coursereader = csv.reader(csvfile, delimiter=';')
    with open(semester + '_gpa.csv', 'a', newline='', encoding = "UTF-8") as gpafile:
        courseriter = csv.writer(gpafile, delimiter=',')
        for cidx, c in enumerate(coursereader):
            print(cidx)
            if len(c) != 16:
                print(c)
            gpa_url = c[-1]
            page = urlopen(BASE_URL + gpa_url)
            soup = BeautifulSoup(page,"html.parser")
            table = soup.find('table',{'id': 'history-table'})

            # no gpa record
            if not table:
                continue

            trs = table.findAll('tr', {'class': 'item'})
            gpa_record = [0] * 10
            for tr in trs:
                tds = tr.findAll('td')
                for idx, td in enumerate(tds):
                    if idx < 2:
                        continue
                    gpa_record[idx-2] += int(td.getText())

            weighted_sum = 0
            for idx, gpa_score in enumerate(gpa_record):
                weighted_sum += gpa_record[idx] * gpa[idx]
            avg_gpa = 0
            if sum(gpa_record) > 0:
                avg_gpa = round(weighted_sum / sum(gpa_record), 3)
            result = [ c[5], c[2]] + gpa_record + [avg_gpa]
            courseriter.writerow(result)


from twitter_api.types.v2_search_query.operators.has_links_operator import (
    HasLinksOperator,
)
from twitter_api.types.v2_search_query.operators.operator import IncompleteOperator
from twitter_api.types.v2_search_query.search_query import SearchQuery, build

class TestHasLinksOperator:
    def test_has_links_operator(self):
        assert str(HasLinksOperator()) == "has:links"

    def test_query_incomplete(self):
        assert isinstance(
            build(lambda q: q.has_links()),
            IncompleteOperator,
        )

    def test_query_build(self):
        assert (
            str(SearchQuery.build(lambda q: q.mention("twitterdev") & q.has_links()))
            == "@twitterdev has:links"
        )


# -*- coding:utf-8 -*-
# 导入依赖
# 导入乳腺癌数据集
from sklearn.datasets import load_breast_cancer
# 导入BP模型
from sklearn.neural_network import MLPClassifier
# 导入训练集分割方法
from sklearn.model_selection import train_test_split
# 导入预测指标计算函数和混淆矩阵计算函数
from sklearn.metrics import classification_report
# 导入绘图包
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D

# 读取数据
cancer = load_breast_cancer()
print(cancer)

# 数据探索
# 查看数据集信息
print('breast_cancer数据集的长度为：', len(cancer))
print('breast_cancer数据集的类型为：', type(cancer))
# 分割数据为训练集和测试集
cancer_data = cancer['data']
print('cancer_data数据维度为：', cancer_data.shape)
print('cancer_data数据类型为：', type(cancer_data))
cancer_target = cancer['target']
print('cancer_target标签维度为：', cancer_target.shape)
print('cancer_target标签类型为：', type(cancer_target))
feature_name = cancer['feature_names']
cancer_feature = pd.DataFrame(cancer_data, columns=feature_name)
x_train, x_test, y_train, y_test = train_test_split(
    cancer_feature, cancer_target, test_size=0.2, random_state=2020)

# 特征工程
# 模型训练
# 建立 BP 模型, 采用Adam优化器，relu非线性映射函数
BP = MLPClassifier(
    solver='adam',
    activation='relu',
    max_iter=1000,
    alpha=1e-3,
    hidden_layer_sizes=(64, 32, 32),
    random_state=1)
# 进行模型训练
BP.fit(x_train, y_train)
# 模型评估
# 进行模型预测
y_predict = BP.predict(x_test)
# 可视化真实数据
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20)
ax.scatter(x_train.values[:, 0], x_train.values[:, 1],
           x_train.values[:, 2], marker='o', c=y_train)
plt.title('True Label Map')

# 可视化预测数据
fig = plt.figure()
ax = Axes3D(fig, rect=[0, 0, 1, 1], elev=20, azim=20)
ax.scatter(x_test.values[:, 0], x_test.values[:, 1],
           x_test.values[:, 2], marker='o', c=y_test)
plt.title('Cancer with BP Model')

# 进行测试集数据的类别预测
print("测试集的真实标签:\n", y_test)
print("测试集的预测标签:\n", y_predict)
print(classification_report(y_test, y_predict))

plt.show()


import asyncio
import json
import numpy as np
import matplotlib.pyplot as plt
import aiohttp

from sklearn import preprocessing

from understat import Understat

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
    return x * (1 - x)

def results(team, kolejka, fixtures):
    wins = 0
    draws = 0
    losses = 0
    start = kolejka - 7
    if start < 0:
        start = 0
    matches = list()

    for game in fixtures:
        for side in ('h', 'a'):
            if game[side]['title'] == team:
                matches.append(game)

    for i in range(start, kolejka):
        game = matches[i]
        for side in ('h', 'a'):
            if game[side]['title'] == team:
                if game['goals']['h'] == game['goals']['a']:
                    draws += 1
                elif game['goals']['h'] > game['goals']['a']:
                    if side == 'h':
                        wins += 1
                    else:
                        losses += 1
                else:
                    if side == 'a':
                        wins += 1
                    else:
                        losses += 1
    return wins, draws, losses

def wynik(team, kolejka, fixtures):
    matches = list()
    for game in fixtures:
        for side in ('h', 'a'):
            if game[side]['title'] == team:
                matches.append(game)
    game = matches[kolejka]
    for side in ('h', 'a'):
        if game[side]['title'] == team:
            if side == 'h':
                stadion = 1
                przeciwnik = game['a']['title']
            else:
                stadion = 0
                przeciwnik = game['h']['title']
            if game['goals']['h'] == game['goals']['a']:
                return 0.5, przeciwnik, stadion
            elif game['goals']['h'] > game['goals']['a']:
                if side == 'h':
                    return 1, przeciwnik, stadion
                else:
                    return 0, przeciwnik, stadion
            else:
                if side == 'a':
                    return 1, przeciwnik, stadion
                else:
                    return 0, przeciwnik, stadion

def zbieranie_danych(teams_list, fixtures, start_k, stop_k):
    training_inputs = list()
    training_outputs = list()
    druzyny = list()
    liczba_meczy = 0
    for team in teams_list:
        for i in range(start_k, stop_k):
            liczba_meczy += 1
            score, opponent, strona = wynik(team, i, fixtures)
            wins, draws, losses = results(team, i, fixtures)
            wins_op, draws_op, losses_op = results(opponent, i, fixtures)
            druzyny.append([team, opponent])
            training_inputs.append([wins, draws, losses, wins_op, draws_op, losses_op, strona])
            training_outputs.append(score)

    training_inputs = np.array(training_inputs)
    training_inputs = preprocessing.normalize(training_inputs)  # normalizujemy inputy
    training_outputs = np.array(training_outputs)
    training_outputs = np.reshape(training_outputs, (1, liczba_meczy))
    training_outputs = training_outputs.T
    return training_inputs, training_outputs, liczba_meczy, druzyny

def testing(fixtures, learning_rate, prog, data):
    teams_list = list()
    for game in fixtures:
        for side in ('h', 'a'):
            if game[side]['title'] not in teams_list:
                teams_list.append(game[side]['title'])

    training_inputs, training_outputs, liczba_meczy, druzyny1 = zbieranie_danych(teams_list, fixtures, 8, 25)
    test_inputs, test_outputs, test_meczy, druzyny2 = zbieranie_danych(teams_list, fixtures, 28, 38)

    synaptic_weights = 2 * np.random.random((7, 1)) - 1
    # print(synaptic_weights)
    # print(training_outputs)
    bledy = []
    for iteration in range(data):
        input_layer = training_inputs
        outputs = sigmoid(np.dot(input_layer, synaptic_weights))  # mnożenie skalarne macierzy
        error = training_outputs - outputs
        sum_error = sum(error)
        for i in sum_error:
            bledy.append(abs(i))

        adjustments = learning_rate * error * sigmoid_derivative(outputs)
        synaptic_weights += np.dot(input_layer.T, adjustments)

    print(bledy)
    plt.plot(bledy)
    plt.show()
    # print(synaptic_weights)
    # print('Outputs after training: ')
    predict_outputs = sigmoid(np.dot(test_inputs, synaptic_weights))
    licznik = 0
    for i in range(1, test_meczy):
        if prog >= predict_outputs[i] >= 1 - prog:
            prediction = 0.5
        elif predict_outputs[i] < 1 - prog:
            prediction = 0
        else:
            prediction = 1
        print(druzyny2[i - 1][0], "vs", druzyny2[i - 1][1])
        print("Faktyczny wynik:", test_outputs[i], "Przewidywany wynik:", predict_outputs[i], "\n") # raw
        #print("Faktyczny wynik:", test_outputs[i], "Przewidywany wynik:", prediction, "\n")
        if test_outputs[i] == prediction:
            licznik += 1

    return licznik / test_meczy

async def main():
    async with aiohttp.ClientSession() as session:
        year = 2019  # year = int(input("Podaj rok: "))

        understat = Understat(session)
        fixtures = await understat.get_league_results(
            "epl",
            year,

        )
        maks = 0
        lr = 0
        progi = 0
        suma = 0
        data = 0
        """for j in np.arange(0.05, 1, step=0.05):
            print("learning rate:", j, "\n")
            for m in np.arange(0.6, 0.9, step=0.05):
                print("prog:", m, "\n")
                for g in range(100, 1050, 100):
                    for i in range(1, 10):
                        suma += testing(fixtures, j, m, g)

                    x = suma / 9
                    if x > maks:
                        maks = x
                        lr = j
                        progi = m
                        data = g
                    suma = 0"""

        """print(data)
        print(lr)
        print(progi)"""
        """do_scattera = []
        for i in range(1, 100):
            wynik_testu = testing(fixtures, 0.6, 0.6, 400)
            suma += wynik_testu
            do_scattera.append(wynik_testu)

        plt.scatter(range(1, 100), do_scattera)
        plt.show()
        x = suma/99"""
        x = testing(fixtures, 0.6, 0.6, 400)
        print("Skuteczność:", round(x * 100, 2), "%")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())


idades = 0
mid = 0
idade = 0
nome = ''
for g in range(1, 5):
    print('='*10, F'{g}° PESSOA', '='*10)
    n1 = str(input('Digite o nome: ')).strip().upper()
    n2 = int(input('Digite a idade: '))
    n3 = str(input('Digite o sexo M/F: ')).strip()
    idades += n2
    if n3 in 'FffemininoFEMININO' and n2 < 20:
        mid += 1
    if n3 in 'MmasculinoMASCULINO' and g == 1:
        nome = n1
        idade += n2
    if n3 in 'mMmasculinoMASCULINO' and idade < n2:
        idade = n2
        nome = n1
    if nome == '':
        nome = 'Não há homens'
print(f'A média de idades das pessoas é de {idades/4}\n'
      f'O Homem mais velho é: {nome} e sua idade é {idade}\n'
      f'E existem {mid} mulher com menos de 20 anos')


import numpy as np
from logging import Logger

class NumberLogger(Logger):
	def __init__(self, file):
		self.file = file
	def log_nb(self, nb):
		self.log("INFO", np.format_float_scientific(nb))

if __name__ == "__main__":
	lg = NumberLogger()
	lg.log_nb(1.245)


import scipy as sp
from scipy.sparse import csr_matrix, lil_matrix, spdiags
from scipy.sparse.linalg import spsolve, inv, norm

def make_a(n):
    """
    Creates a matrix that can be used to solve Euler-Bernoulli beam equations EIy''''=f(x)

    A:

    |16        -9        8/3       -1/4                       |
    |-4        6         -4        1                          |
    |1         -4        6         -4        1                |
    |    ...       ...       ...       ...       ...          |
    |          1         -4        6         -4        1      |
    |                    16/17     -60/17    72/17     -28/17 |
    |                    -12/17    96/17     -156/17   72/17  |

    :param n: the length of the matrix
    :return: a matrix with the necessary values to solve Euler-Bernoulli beam equations
    """
    e = sp.ones(n)
    A = spdiags([e, -4 * e, 6 * e, -4 * e, e], [-2, -1, 0, 1, 2], n, n)
    A = lil_matrix(A)
    B = csr_matrix([[16, -9, 8 / 3, -1 / 4],
                    [16 / 17, -60 / 17, 72 / 17, -28 / 17],
                    [-12 / 17, 96 / 17, -156 / 17, 72 / 17]])
    A[0, 0:4] = B[0, :]
    A[n - 2, n - 4:n] = B[1, :]
    A[n - 1, n - 4:n] = B[2, :]
    return A.tocsc()

def cond(A):
    """
    Finds the condition number of a sparse matrix

    :param A: the matrix to find the condition number of
    :return: the condition number of A
    """
    return norm(A) * norm(inv(A))

def displacement(n: int, L: float, E: float, I: float, f: staticmethod) -> list:
    """
    A method for calculating the displacement of a beam

    :param n: number of steps
    :param L: length
    :param E: Young modulus
    :param f: weight function with weight of beam included
    :return: the displacement at each step
    """
    A = make_a(n)

    h = L / n

    # x_i = h * i
    b = [f(h * i) * h ** 4 / (E * I) for i in range(1, n + 1)]

    y = spsolve(A, b)

    return y


from secrets import token_urlsafe
import configparser

config = configparser.ConfigParser()
config['DEFAULT'] = {'AUTOCONNECT': str(False),
                     'PUBLIC_ACC': str(False)}

config['USER'] = {}
config['USER']['USERNAME'] = 'username'
config['USER']['IS_PAID'] = str(False)

config['CONNECTION'] = {}
config['CONNECTION']['IP'] = '127.0.0.1'

with open('setup.ini', 'w') as config_file:
    config.write(config_file)


import os
import helper_functions as hf
from tqdm import tqdm

def main():

    ####################################### USER-INPUT ##################################################

    FOLDER = ('/home/ubuntu/PycharmProjects/DeepImagePrior/BME590_DeepImagePrior'
              '/results/UNET_MSE/5')
    IMAGE_NAME = '5'
    START_ITER = 0
    END_ITER = 50000

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

    # Create save folders:
    SAVE_FOLDER = os.path.join(FOLDER, 'selection_metrics')
    if not os.path.exists(SAVE_FOLDER):
        os.mkdir(SAVE_FOLDER)

    # Initialise write file:
    WRITE_FILENAME = os.path.join(SAVE_FOLDER, 'metrics.txt')
    with open(WRITE_FILENAME, 'a') as wf:
        wf.write('Iteration\tCNR\tSSIM')

    # Load original noisy image:
    noisy_filename = os.path.join(FOLDER, 'input_image.tif')
    noisy_image = hf.get_training_image(noisy_filename)

    # Load ground truth image:
    truth_filename = os.path.join(FOLDER, 'ground_truth.tif')
    truth_image = hf.get_training_image(truth_filename)

    # Calculate initial metrics:
    cnr = hf.calculate_metrics(truth_image, noisy_image, 'cnr', IMAGE_NAME)
    ssim = hf.calculate_metrics(truth_image, noisy_image, 'ssim', IMAGE_NAME)

    # Write to file:
    with open(WRITE_FILENAME, 'a') as wf:
        wf.write('\ninput_image\t{}\t{}'.format(cnr, ssim))

    # Keep track:
    track_iter = []
    track_cnr = []
    track_ssim = []

    print('Calculating metrics...')

    for i in tqdm(range(START_ITER, END_ITER+1, 1000)):

        # Load reconstructed image:
        denoised_filename = os.path.join(FOLDER, 'iteration_{}.tif'.format(i))
        denoised_image = hf.get_training_image(denoised_filename)

        # Calculate metrics:
        cnr = hf.calculate_metrics(truth_image, denoised_image, 'cnr', IMAGE_NAME)
        ssim = hf.calculate_metrics(truth_image, denoised_image, 'ssim', IMAGE_NAME)

        # Write to file:
        with open(WRITE_FILENAME, 'a') as wf:
            wf.write('\n{}\t{}\t{}'.format(i, cnr, ssim))

        # Track:
        track_iter.append(i)
        track_cnr.append(cnr)
        track_ssim.append(ssim)

    # Plot:
    plot_filename = os.path.join(SAVE_FOLDER, 'cnr.tif')
    hf.plot_metrics(x=track_iter,
                    y=track_cnr,
                    title='cnr',
                    save_filename=plot_filename,
                    calculate_max=True,
                    calculate_min=False)

    plot_filename = os.path.join(SAVE_FOLDER, 'ssim.tif')
    hf.plot_metrics(x=track_iter,
                    y=track_ssim,
                    title='ssim',
                    save_filename=plot_filename,
                    calculate_max=True,
                    calculate_min=False)

    print('Completed.')

if __name__ == "__main__":
    main()


from ConfigSpace import ConfigurationSpace
from ConfigSpace.hyperparameters import \
    CategoricalHyperparameter, UniformFloatHyperparameter, UniformIntegerHyperparameter

from xbbo.search_algorithm.bo_optimizer import BO

def custom_black_box_func(config):
    '''
    define black box function:
    y = x^2
    '''
    return config['x'] ** 2

def custom_search_space():
    '''
    define search space
    '''
    configuration_space = ConfigurationSpace()
    configuration_space.add_hyperparameter(UniformFloatHyperparameter('x', -10, 10, default_value=-3))
    return configuration_space

if __name__ == "__main__":
    MAX_CALL = 30

    cs = custom_search_space()

    # specify black box optimizer
    hpopt = BO(space=cs, suggest_limit=MAX_CALL)
    # ---- Begin BO-loop ----
    for i in range(MAX_CALL):
        # suggest
        trial_list = hpopt.suggest() # defalut suggest one trial
        # evaluate
        obs = custom_black_box_func(trial_list[0].config_dict)
        # observe
        trial_list[0].add_observe_value(obs)
        hpopt.observe(trial_list=trial_list)

        print(obs)

    print('find best (value, config):{}'.format(hpopt.trials.get_best()))