text:
from matplotlib import rc
import matplotlib.pyplot as plt
from scipy.stats import ks_2samp
from matplotlib import pyplot as plt
import glob
import os
import numpy as np
import time
import cv2
import sys
from statsmodels.distributions.empirical_distribution import ECDF
from noise_model import *

# See https://en.wikipedia.org/wiki/Confusion_matrix for more info
# Example confusion matrix
CONFUSION_MATRIX = np.array(
        [[0.85714286,0.,0.14285714,0.,0.,0.,0.],
        [0.06666667,0.8,0.06666667,0.,0.,0.06666667,0.],
        [0.,0.,1.,0.,0.,0.,0.],
        [0.03636364,0.,0.01818182,0.94545455,0.,0.,0.],
        [0.,0.,0.,0.,1.,0.,0.],
        [0.,0.,0.,0.,0.,1.,0.],
        [0.,0.,0.,0.01408451,0.,0.,0.98591549]]).T

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')

GENERATE_NEW_BBS = True
GENERATE_IMGS = False

CLASSES = ['mountainbike',
           'car',
           'truck',
           'dog',
           'horse',
           'sheep',
           'giraffe']

def get_overlap_fraction(bb1, bb2, getAreaDiff=False):
    """Returns the fraction of overlap (as a fraction of
    the smallest of the BBs) between two BBs.

    Parameters
    ----------
    bb1 : tuple(int, int, int, int)
        BB extent defined by min and max x-coordinates and
        min and max y-coordinates.
    bb2 : tuple(int, int, int, int)
        BB extent defined by min and max x-coordinates and
        min and max y-coordinates.
    getAreaDiff : bool, optional
        if true, will return the difference in area, by default False

    Returns
    -------
    float
        The overlap fraction, or area difference.
    """

    xmin1, xmax1, ymin1, ymax1, cls1 = bb1
    xmin2, xmax2, ymin2, ymax2, cls2 = bb2

    xmin1 = int(xmin1)
    xmax1 = int(xmax1)
    ymin1 = int(ymin1)
    ymax1 = int(ymax1)
    xmin2 = int(xmin2)
    xmax2 = int(xmax2)
    ymin2 = int(ymin2)
    ymax2 = int(ymax2)

    dx = np.min((xmax1, xmax2)) - np.max((xmin1, xmin2))
    dy = np.min((ymax1, ymax2)) - np.max((ymin1, ymin2))

    dx = float(dx)
    dy = float(dy)

    if (dx>=0) and (dy>=0):
        overlapping = dx*dy
        area1 = (xmax1 - xmin1) * (ymax1 - ymin1)
        area2 = (xmax2 - xmin2) * (ymax2 - ymin2)
        not_overlapping = area1 + area2 - 2 * overlapping
        if getAreaDiff: return float(np.abs(area1 - area2))/area1 if area1 != 0 else 0
        if overlapping == 0: return 0
        return overlapping /(not_overlapping+overlapping)
    else:
        return 0

def dist(p1, p2):
    return np.linalg.norm(p2-p1)

def center(bb):
    xmin, xmax, ymin, ymax, label = bb
    xmin = int(xmin)
    xmax = int(xmax)
    ymin = int(ymin)
    ymax = int(ymax)

    x = xmax - xmin
    x /= 2
    y = ymax - ymin
    y /= 2

    return np.array([x,y])

def area(bb):
    xmin, xmax, ymin, ymax, label = bb
    xmin = int(xmin)
    xmax = int(xmax)
    ymin = int(ymin)
    ymax = int(ymax)
    return (xmax - xmin) * (ymax - ymin)

def get_min_dim(bb):
    xmin, xmax, ymin, ymax, label = bb
    xmin = int(xmin)
    xmax = int(xmax)
    ymin = int(ymin)
    ymax = int(ymax)
    return np.min((xmax-xmin, ymax-ymin))

def get_max_dim(bb):
    xmin, xmax, ymin, ymax, label = bb
    xmin = int(xmin)
    xmax = int(xmax)
    ymin = int(ymin)
    ymax = int(ymax)
    return np.max((xmax-xmin, ymax-ymin))

def plot_confusion_matrix(cm,
                          normalize=True,
                          title=None,
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    plt.show() must be run to view the plot, this is not done
    by this function.

    Parameters
    ----------
    cm : np.ndarray
        the confusion matrix to plot
    normalize : bool, optional
        whether to normalise the matrix, by default True
    title : string, optional
        title of the plot, otherwise otherwise a sensible title is generated. By default None
    cmap : matplotlib.colormap, optional
        matplotlib colormap, by default plt.cm.Blues

    Returns
    -------
    matplotlib.Axes
        axes object of the plot generated
    """
    if not title:
        title = 'Normalized confusion matrix' if normalize else 'Confusion matrix, without normalization'

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    fig, ax = plt.subplots(figsize=(4,2))
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=CLASSES, yticklabels=CLASSES,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()

    return ax

def analyse(get_ps = True, lerr=0.8, cerr=0.8, plot=False):
    # Make a sensible function which shows the
    # distribution of errors given an array of BBs
    raise NotImplementedError("Currently in development")

if __name__ == '__main__':
    lerr = 0.175
    cerr = 0.7
    analyse(True, lerr, cerr, True)

max_stars_repo_name: matthewlemaitre/airsim-bbs

max_issues_repo_name: matthewlemaitre/airsim-bbs

max_forks_repo_name: matthewlemaitre/airsim-bbs

reason: import numpy,from scipy,from statsmodels


text:
from utilities.nn.neuralnetwork import NeuralNetwork
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn import datasets
import numpy as np

#load the MNIST dataset and apply min/max scaling to scale the pixel intensity values to the range[0,1] from[0,255](each image is represented by a 8*8 dim feature vector)

print("[INFO] loading MNIST (sample) dataset...")
digits = datasets.load_digits()
data = digits.data.astype("float")
data = (data - data.min())/(data.max() - data.min())

print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1]))

#Split the data
(trainX, testX, trainY,  testY) = train_test_split(data, digits.target, test_size=0.5)
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)

#train the network
print("[INFO] training network...")
nn= NeuralNetwork([trainX.shape[1], 32, 16, 10])
print("[INFO] {}".format(nn))
nn.fit(trainX, trainY, epochs = 1000)

#evaluate the network
print("[INFO] evaluating network...")
predictions = nn.predict(testX)

predictions = predictions.argmax(axis=1)
print(classification_report(testY.argmax(axis=1), predictions))

ext: py

max_stars_repo_name: Blowoffvalve/OpenCv

max_issues_repo_name: Blowoffvalve/OpenCv

max_forks_repo_name: Blowoffvalve/OpenCv

reason: import numpy


text:
# Import necessary standard libraries
import cv2
import numpy as np
from random import randint
# Import necessary constants
from utils.constants import *

class Map:
    def __init__(self):
        """
        Initialize map class
        """
        # Various class parameters
        self.height = SCALING_FACTOR * MAP_SIZE[0]
        self.width = SCALING_FACTOR * MAP_SIZE[1]
        self.black = (0, 0, 0)
        # Define radius of circular obstacles
        # Radius is same for all circles
        self.circle_radius = int(SCALING_FACTOR * 0.2)
        # Define centers of all circles
        self.circle_centers = np.array([(8, 1),
                                        (6, 8.4),
                                        (3, 5)],
                                       dtype=np.int32)
        self.scaled_centers = SCALING_FACTOR * self.circle_centers
        # Define empty world and add obstacles to it
        self.map_img = self.generate_map()
        # Get image to search for obstacles
        self.animation_img = self.draw_obstacles()
        # save obstacle trajectories
        self.obstacles = {'o1':[], 'o2':[], 'o3':[]}

    def update_scaled_centers(self):
        """
        Method to update scaled centers
        Only to be called if obstacle centers have been changed
        :return: nothing
        """
        self.scaled_centers = SCALING_FACTOR * self.circle_centers

    def draw_circle(self, img, thresh=0):
        """
        Draw the 4 circular obstacles on the map-image
        :return: nothing
        """
        for center in self.scaled_centers:
            # Draw the circle
            cv2.circle(img, (int(center[0]), int(center[1])), self.circle_radius + thresh, self.black, -1)

    def generate_map(self):
        """
        Get eroded image to check for obstacles considering the robot radius and clearance
        :return: image with obstacle space expanded to distance threshold between robot and obstacle
        """
        # Get map with obstacles
        # Initialize empty grid
        check_img = np.full((MAP_SIZE[0], MAP_SIZE[1]), fill_value=FREE_SPACE_VALUE, dtype=np.int8)
        # Mark obstacle locations
        for center in self.circle_centers:
            check_img[center[1]][center[0]] = OBSTACLE_LOC_VALUE
        return check_img

    def draw_obstacles(self):
        """
        Draw map using half-plane equations
        :return: map-image with all obstacles
        """
        self.animation_img = cv2.imread('images/map.png')
        if self.animation_img is None:
            self.animation_img = np.zeros((self.height, self.width, 3), dtype=np.uint8)
            # Fill map-image with white color
            self.animation_img.fill(255)
            # Draw various obstacles on the map
            self.draw_circle(self.animation_img)
            cv2.imwrite('images/map.png', self.animation_img)

        return self.animation_img

    def update_obstacle_space(self):
        """
        Method to move obstacles around on the map
        :return: map-image with updated positions of the obstacles
        """
        for i in range(len(self.circle_centers)):
            center = self.circle_centers[i][0], self.circle_centers[i][1]
            self.map_img[center[1]][center[0]] = FREE_SPACE_VALUE
            action = randint(0, TOTAL_ACTIONS)
            new_center, new_action = self.take_action(action, center)
            if new_center not in self.circle_centers:
                self.circle_centers[i] = new_center
                self.obstacles['o'+str(i+1)].append([new_center, new_action])
            else:
                self.obstacles['o'+str(i+1)].append([new_center, None])
            self.map_img[self.circle_centers[i][1]][self.circle_centers[i][0]] = OBSTACLE_LOC_VALUE
        self.update_scaled_centers()
        self.animation_img.fill(255)
        self.draw_circle(self.animation_img)
        return self.map_img

    @staticmethod
    def take_action(action, center_obstacle):
        """
        Method to change centers of the obstacles
        :param action: an integer indicating the action
        :param center_obstacle: a tuple containing center of the obstacle
        :return: a tuple containing new centers of the obstacle
        """
        center_new = center_obstacle
        # Take required and get new center coordinates of the obstacles
        if action == ACTION_SPACE['up']:
            center_new = center_obstacle[0], center_obstacle[1] - VELOCITY_OBSTACLE
        elif action == ACTION_SPACE['down']:
            center_new = center_obstacle[0], center_obstacle[1] + VELOCITY_OBSTACLE
        elif action == ACTION_SPACE['left']:
            center_new = center_obstacle[0] - VELOCITY_OBSTACLE, center_obstacle[1]
        elif action == ACTION_SPACE['right']:
            center_new = center_obstacle[0] + VELOCITY_OBSTACLE, center_obstacle[1]
        elif action == ACTION_SPACE['top_left']:
            center_new = center_obstacle[0] - VELOCITY_OBSTACLE, center_obstacle[1] - VELOCITY_OBSTACLE
        elif action == ACTION_SPACE['top_right']:
            center_new = center_obstacle[0] + VELOCITY_OBSTACLE, center_obstacle[1] - VELOCITY_OBSTACLE
        elif action == ACTION_SPACE['bottom_left']:
            center_new = center_obstacle[0] - VELOCITY_OBSTACLE, center_obstacle[1] + VELOCITY_OBSTACLE
        elif action == ACTION_SPACE['bottom_right']:
            center_new = center_obstacle[0] + VELOCITY_OBSTACLE, center_obstacle[1] + VELOCITY_OBSTACLE
        # Check for out of bounds condition
        if ((center_new[0] <= 0 or center_new[0] > MAP_SIZE[0] - 1) or
                (center_new[1] <= 0 or center_new[1] > MAP_SIZE[1] - 1)):
            center_new = center_obstacle
            action = None
        if center_new == (9, 6):
            center_new = center_obstacle
            action = None
        return center_new, action

max_issues_repo_name: bigmpc/dynamic-path-planner

max_forks_repo_name: bigmpc/dynamic-path-planner


text:
#%%
import fnmatch, os, re
import glob
import math
import cv2
import numpy as np
import operator
from matplotlib import pyplot as plt

point = '.'
extension = 'jpg'
point_extension = '.'+ extension
tag = '_blurring'

#%%
def preview(image):
    plt.imshow(image, cmap='gray'), plt.xticks([]), plt.yticks([])
    plt.show()
#%%
def insensitive_glob(pattern):
    def either(c):
        return '[%s%s]' % (c.lower(), c.upper()) if c.isalpha() else c
    return glob.glob(''.join(map(either, pattern)))

def main():
    for filename in insensitive_glob(os.path.join('data','*','*.{}').format(extension)):
        if '.DS_Store' in filename or '_' in filename:
            continue
        image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
        image_blurry = cv2.blur(image, (5,5))
        # preview(image_blurry)
        (name, ext) = filename.split('.')
        status = cv2.imwrite(name+tag+point_extension,image_blurry)

#%%
if __name__ == '__main__':
    main()
    print('Blurring photos sucessful!!!')

#%%


text:
# -*- coding: utf-8 -*-
import networkx.algorithms.isomorphism as iso
import networkx,sys,codecs
from scipy.spatial.distance import cosine

from sklearn.neighbors import NearestNeighbors

from sklearn.preprocessing import normalize

import numpy as np

import operator, time
reload(sys)
sys.setdefaultencoding('utf8')

# Pruning parameter
FREQ = 10000

def load_word_vectors(file_destination):
    """
    This method loads the word vectors from the supplied file destination.
    It loads the dictionary of word vectors and prints its size and the vector
    dimensionality.
    """
    print >> sys.stderr, "Loading vectors from", file_destination
    input_dic = {}

    with open(file_destination, "r") as in_file:
        lines = in_file.readlines()

    in_file.close()

    words = []
    vectors = []
    for line in lines[1:]:
        item = line.strip().split()
        dkey = item.pop(0)
        words.append(dkey)
        vector = np.array(item, dtype='float32')
        vectors.append(vector)
        #print np.mean(vector)

    npvectors = np.vstack(vectors)

    # Our words are stored in the list words and...
    # ...our vectors are stored in the 2D array npvectors

    # 1. Length normalize
    npvectors = normalize(npvectors, axis=1, norm='l2')

    # 2. Mean centering dimesionwise
    #npvectors = npvectors - npvectors.mean(0)

    # 3. Length normalize again
    #npvectors = normalize(npvectors, axis=1, norm='l2')

    # Create the final dictionary
    for i in xrange(len(words)):
        word = words[i]
        vector = npvectors[i]
        input_dic[word] = vector

    print >> sys.stderr, len(input_dic), "vectors loaded from", file_destination
    return words, input_dic

### This function selects the correct k
def select_k(spectrum, minimum_energy = 0.9):
    running_total = 0.0
    total = sum(spectrum)
    if total == 0.0:
        return len(spectrum)
    for i in range(len(spectrum)):
        running_total += spectrum[i]
        if running_total / total >= minimum_energy:
            return i + 1
    return len(spectrum)

def main():
    # Get vectors first and words sorted by frequency
    en_freq, en_vec = load_word_vectors(sys.argv[1])
    de_freq, de_vec = load_word_vectors(sys.argv[2])

    # Initialise neighborhood graphs
    en_G=networkx.Graph()
    de_G=networkx.Graph()

    # Prepare data for nearest neighbour retrieval
    en_keys = []
    en_pruned = []
    de_keys = []
    de_pruned = []
    for word in en_freq[:FREQ]:
        en_keys.append(word)
        en_pruned.append(en_vec[word])

    for word in de_freq[:FREQ]:
        de_keys.append(word)
        de_pruned.append(de_vec[word])

    # Get nearest neighbours
    nbrs_en = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(en_pruned)
    distances_en, indices_en = nbrs_en.kneighbors(en_pruned)

    nbrs_de = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(de_pruned)
    distances_de, indices_de = nbrs_de.kneighbors(de_pruned)

    for index in indices_en:
        en_G.add_edge(en_keys[index[0]], en_keys[index[1]])

    for index in indices_de:
        de_G.add_edge(de_keys[index[0]], de_keys[index[1]])

    laplacian1 = networkx.spectrum.laplacian_spectrum(en_G)
    laplacian2 = networkx.spectrum.laplacian_spectrum(de_G)

    k1 = select_k(laplacian1)
    k2 = select_k(laplacian2)
    k = min(k1, k2)

    similarity = sum((laplacian1[:k] - laplacian2[:k])**2)

    print "Laplacian: ", similarity

# The code starts here
if __name__=='__main__':
    main()

reason: import numpy,from scipy,import networkx


text:
# # کدهای پیاده سازی پروژه پایان ترم بیگ دیتا  - امین زایراومالی

# # وارد کردن کتابخانه های مورد نیاز پایتون

# first neural network with keras tutorial with letter Dataset
# Dataset Link : https://archive.ics.uci.edu/ml/datasets/Letter+Recognition
# Powered By Amin Zayeromali  - amin.Zayeroamli@gmail.com
from keras.callbacks import ModelCheckpoint
from keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
from numpy import loadtxt
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model
import tensorflow as tf
from keras.models import Model, load_model
from sklearn import preprocessing
import math

# ## بارگزاری دیتاست و توضیحات مشخصه آن
# DataSet Information
# Author: David J. Slate
# Source: [UCI](https://archive.ics.uci.edu/ml/datasets/Letter+Recognition) - 01-01-1991
# Please cite: P. W. Frey and D. J. Slate. "Letter Recognition Using Holland-style Adaptive Classifiers". Machine Learning 6(2), 1991

# TITLE:  Letter Image Recognition Data
#
#    The objective is to identify each of a large number of black-and-white
#    rectangular pixel displays as one of the 26 capital letters in the English
#    alphabet.  The character images were based on 20 different fonts and each
#    letter within these 20 fonts was randomly distorted to produce a file of
#    20,000 unique stimuli.  Each stimulus was converted into 16 primitive
#    numerical attributes (statistical moments and edge counts) which were then
#    scaled to fit into a range of integer values from 0 through 15.  We
#    typically train on the first 16000 items and then use the resulting model
#    to predict the letter category for the remaining 4000.  See the article
#    cited above for more details.

# load the dataset
dataset = loadtxt('dataset_6_letter.txt', dtype=str, delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:10000, 0:16]  # انتخاب ۱۰۰۰۰ هزار رکورد از داده ها برای ترین
X = X.astype(float)
y = dataset[:10000, 16]
le = preprocessing.LabelEncoder()
y = le.fit_transform(y).astype(int)
print("Number Of Unique Label on Target :", len(
    set(y)))  # تعداد کلاسهای یونیک مشخص می شود
y = tf.keras.utils.to_categorical(y, num_classes=len(
    set(y)))  # تبدیل داده های اینتیجر لیبل به باینری

# نمایش تعداد رکوردهای دیتاست ( فیچرها و لیبلهای تارگت )
print(X.shape)
print(y.shape)
print(X)

print(y)  # لیبل بندی کلاسهای خروجی براساس تعداد اپوچ های خروجی با فرمت باینری

# # ساخت مدل برای دیتاست مذکور که به صورت چند کلاسه
# ##  مرحله اول فقط دو لایه پنهان با ورودی ۱۶ تایی و خروجی ۲۶ تایی ساخته و فیت می کنیم
# param_number = output_channel_number * (input_channel_number + 1)

# define the keras model
model = Sequential()
# تعداد نورون های ورودی برابر با فیچر ها ۱۶ تاست
model.add(Dense(32, input_dim=16, activation='relu'))
#model.add(Dense(64, activation='relu'))
# تعداد نورون های خروجی نیز برابر با تعداد لیبلهای غیر تکراری کلاس تارگت گرفتیم
model.add(Dense(26, activation='sigmoid'))
# compile the keras model
#from keral.optimizer import adam
model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=[
              'accuracy'])  # categorical_crossentropy or #binary_crossentropy
print(model.summary())
plot_model(model, show_shapes=True, to_file='mymodel.png')

# ## مدل را برای مقادیر epochs=50, batch_size=10 ساخته و فیت می کنیم

# fit the keras model on the dataset
history = model.fit(X, y, epochs=50, batch_size=10)

plt.figure()
plt.plot(history.history['loss'])

plt.figure()
plt.plot(history.history['accuracy'])

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

predictions = model.predict(X)
# summarize the first 5 cases
for i in range(10):
    print("\n------------------------------- Case ", i+1,
          " ---------------------------------------------------\n")
    print("Data is :\n", X[i].tolist(), "\nPredicted Value is :\n",
          predictions[i], "\nReal Value is :\n", y[i])

# ## مدل را برای مقادیر epochs=100, batch_size=50 ساخته و فیت می کنیم

# define the keras model
model = Sequential()
# تعداد نورون های ورودی برابر با فیچر ها ۱۶ تاست
model.add(Dense(32, input_dim=16, activation='relu'))
#model.add(Dense(64, activation='relu'))
# تعداد نورون های خروجی نیز برابر با تعداد لیبلهای غیر تکراری کلاس تارگت گرفتیم
model.add(Dense(26, activation='sigmoid'))
# compile the keras model
#from keral.optimizer import adam
model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=[
              'accuracy'])  # categorical_crossentropy or #binary_crossentropy
print(model.summary())
plot_model(model, show_shapes=True, to_file='mymodel.png')

# fit the keras model on the dataset
history = model.fit(X, y, epochs=100, batch_size=50)

plt.figure()
plt.plot(history.history['loss'])

plt.figure()
plt.plot(history.history['accuracy'])

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

predictions = model.predict(X)
# summarize the first 5 cases
for i in range(10):
    print("\n------------------------------- Case ", i+1,
          " ---------------------------------------------------\n")
    print("Data is :\n", X[i].tolist(), "\nPredicted Value is :\n",
          predictions[i], "\nReal Value is :\n", y[i])

# #   مدل را سه لایه پنهان  و برای مقادیر توینیگ شده و بهینه فیت و ترین می کنیم
# define the keras model
model = Sequential()
# تعداد نورون های ورودی برابر با فیچر ها ۱۶ تاست
model.add(Dense(32, input_dim=16, activation='relu'))
model.add(Dense(64, activation='relu'))
# تعداد نورون های خروجی نیز برابر با تعداد لیبلهای غیر تکراری کلاس تارگت گرفتیم
model.add(Dense(26, activation='sigmoid'))
# compile the keras model
#from keral.optimizer import adam
model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=[
              'accuracy'])  # categorical_crossentropy or #binary_crossentropy
print(model.summary())
plot_model(model, show_shapes=True, to_file='mymodel.png')

# fit the keras model on the dataset
history = model.fit(X, y, epochs=100, batch_size=50)

plt.figure()
plt.plot(history.history['loss'])

plt.figure()
plt.plot(history.history['accuracy'])

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

predictions = model.predict(X)
# summarize the first 5 cases
for i in range(10):
    print("\n------------------------------- Case ", i+1,
          " ---------------------------------------------------\n")
    print("Data is :\n", X[i].tolist(), "\nPredicted Value is :\n",
          predictions[i], "\nReal Value is :\n", y[i])

# # این بار مدل را برای اپوچ ۲۵۰ تایی اجرا می کنیم
# define the keras model
model = Sequential()
# تعداد نورون های ورودی برابر با فیچر ها ۱۶ تاست
model.add(Dense(32, input_dim=16, activation='relu'))
model.add(Dense(64, activation='relu'))
# تعداد نورون های خروجی نیز برابر با تعداد لیبلهای غیر تکراری کلاس تارگت گرفتیم
model.add(Dense(26, activation='sigmoid'))
# compile the keras model
#from keral.optimizer import adam
model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=[
              'accuracy'])  # categorical_crossentropy or #binary_crossentropy
print(model.summary())
plot_model(model, show_shapes=True, to_file='mymodel.png')

# fit the keras model on the dataset
history = model.fit(X, y, epochs=250, batch_size=50)

plt.figure()
plt.plot(history.history['loss'])

plt.figure()
plt.plot(history.history['accuracy'])

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

predictions = model.predict(X)
# summarize the first 5 cases
for i in range(10):
    print("\n------------------------------- Case ", i+1,
          " ---------------------------------------------------\n")
    print("Data is :\n", X[i].tolist(), "\nPredicted Value is :\n",
          predictions[i], "\nReal Value is :\n", y[i])

# # ساخت مدل با ولیدیشن و استاپ لاست برای مدل بهینه تر

es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20)
mc = ModelCheckpoint('best_model.h5', monitor='val_accuracy',
                     mode='max', verbose=1, save_best_only=True)
history = model.fit(X, y, epochs=250, batch_size=10, verbose=1, validation_split=0.2, callbacks=[
                    mc, es])  # validation_data=[test_x, test_y]
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['loss', 'val_loss'], loc='upper right')
plt.figure()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['acc', 'val_acc'], loc='upper right')

# # با استفاده از مدل بهینه تعداد ۵ نمونه را تست میگیریم و نتایج را مشخص می کنیم
model = load_model('best_model.h5')
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
predictions = model.predict(X)
# summarize the first 5 cases
for i in range(5):
    print("\n------------------------------- Case ", i+1,
          " ---------------------------------------------------\n")
    print("Data is :\n", X[i].tolist(), "\nPredicted Value is :\n",
          predictions[i], "\nReal Value is :\n", y[i])

max_stars_repo_name: aminzayer/Deep-Learning

max_issues_repo_name: aminzayer/Deep-Learning

max_forks_repo_name: aminzayer/Deep-Learning

reason: import numpy,from numpy


text:
import sympy as sp
import numpy as np
from typing import Any, Dict, Iterator, List, Optional, Tuple, TYPE_CHECKING, Union
from pyomo.environ import (
    ConcreteModel, Constraint, Set, Var, Param,
)
from sympy import Matrix as Mat
from . import utils, visual
from .system import System3D
from .links import Link3D

if TYPE_CHECKING:
    from .variable_list import VariableList

class _TorqueSpeedLimit:
    def __init__(self, name: str, torque_bounds: Tuple[float, float], no_load_speed: float) -> None:
        """
        Limit the input torque to:
            $$ τ = τ_s - ω / ω_n * τ_s $$

        where
            τ = input torque
            ω = rotational velocity about the same axis
            ω_n = no load speed (x-axis intercept on graphs you may see)
            τ_s = stall torque (y-axis intercept on graphs you may see)

        From http://lancet.mit.edu/motors/motors3.html
        """
        assert torque_bounds[0] < torque_bounds[1]

        self.name = name
        self.torque_bounds = torque_bounds
        self.no_load_speed = no_load_speed

        self.relative_angle_velocities: List = []  # sympy equations
        self.axes: List[str] = []

    def add_rel_vel(self, rel_velocity, axis: str):
        self.relative_angle_velocities.append(rel_velocity)
        self.axes.append(axis)

    def add_equations_to_pyomo_model(self,
                                     sp_variables: List[sp.Symbol],
                                     pyo_variables: 'VariableList',
                                     collocation: str,
                                     Tc, Tc_set):
        m = Tc.model()
        self.Tc = Tc
        self.Tc_set = Tc_set
        self.pyo_variables = pyo_variables

        self.rel_angle_vels_f = utils.lambdify_EOM(
            self.relative_angle_velocities, sp_variables
        )

        τ_sn, τ_sp = self.torque_bounds
        ω_n = self.no_load_speed

        def torque_speed_limit_constr(m, fe, idx, posneg):
            ω = self.rel_angle_vels_f[idx](pyo_variables[fe, 1])
            τ = Tc[fe, idx]

            # return inequality(- τ_s, τ * (1 - ω / ω_n), τ_s)
            # return inequality(- τ_s, τ * ω / ω_n, τ_s)
            if posneg == '+':
                return τ <= τ_sp * (1 - ω / ω_n)
            elif posneg == '-':
                return τ >= τ_sn * (1 + ω / ω_n)
            else:
                raise ValueError(f'posneg should be "+" or "-". Got: {posneg}')

        constraintname = self.name + '_torque_speed_limit'
        assert not hasattr(m, constraintname), \
            f'The model already has a constraint with the name {constraintname}'

        setattr(m, constraintname,
                Constraint(m.fe, Tc_set, ('+', '-'), rule=torque_speed_limit_constr))

    def plot(self, _ax=None, markersize: float = 5.):
        m = self.Tc.model()
        # ncp = len(m.cp)
        data: List[List[float]] = [
            [v.value for v in self.pyo_variables[fe, 1]]
            for fe in m.fe
        ]

        τ = utils.get_vals(self.Tc, (self.Tc_set,))
        ω = np.zeros(τ.shape)
        for idx, ω_f in enumerate(self.rel_angle_vels_f):
            for fe in m.fe:
                ω[fe-1, idx] = ω_f(data[fe-1])

        import matplotlib.pyplot as plt
        if _ax is None:
            _, ax = plt.subplots()
        else:
            ax = _ax

        # plot the torque-speed curve line
        τ_sn, τ_sp = self.torque_bounds
        ω_n = self.no_load_speed
        # plt.plot([0,  2*ω_n], [τ_sp, 0], '--', color='black')
        # plt.plot([-2*ω_n, 0], [0, τ_sn], '--', color='black')
        ax.plot([0,  2*ω_n], [τ_sp, τ_sn], '--', color='black')
        ax.plot([-2*ω_n, 0], [τ_sp, τ_sn], '--', color='black')

        # torque limit line
        ax.plot([-2*ω_n, 0], [τ_sp, τ_sp], '--', color='black')
        ax.plot([0,  2*ω_n], [τ_sn, τ_sn], '--', color='black')

        # plot torque vs speed
        for i in range(τ.shape[1]):
            ax.scatter(ω[:, i], τ[:, i], s=markersize, label=f'$\\tau_{i}$')

        if _ax is not None:
            return ax

        plt.title(f'Torque speed curve in {self.name}')
        plt.xlabel('Angular velocity $\\omega$ [rad/s]')
        plt.ylabel('Input torque $\\tau$ [Nm/body_weight]')
        plt.legend()
        plt.grid(True)
        plt.show()

        return ax

    def __repr__(self) -> str:
        return f'_TorqueSpeedLimit(torque_bounds={self.torque_bounds}, no_load_speed={self.no_load_speed}, axes={self.axes})'

class Motor3D:
    def __init__(self, name: str, Rb_I: Mat,
                 *,
                 torque_bounds: Tuple[float, float],
                 no_load_speed: Optional[float] = None):
        self.name = name
        self.Rb_I = Rb_I
        self.torque_bounds = torque_bounds

        if no_load_speed is not None:
            self.torque_speed_limit = _TorqueSpeedLimit(
                name, torque_bounds, no_load_speed)

        self.input_torques: List[sp.Symbol] = []
        self.torques_on_body = Mat([0., 0., 0.])

        # a list of tuples of [torques_on_body, rotation, about] on other bodies
        self.other_bodies: List[Tuple[Mat, Mat, str]] = []

    def add_input_torques_at(self, otherlink, about: str):
        """ Add input torques between two links, about axis `about` of `self`
        >>> link_body.add_input_torques_at(link_UFL, about='xy')
        TODO: THINK MORE ABOUT WHAT THIS REALLY MEANS! The second torque should really be rotated
        """
        assert all(axis in 'xyz' for axis in about)

        τ = Mat([
            *sp.symbols(r'\tau_{%s/%s/:%s}' % (self.name, otherlink.name, len(about)))
        ])
        self.input_torques.extend(τ)

        torques_on_other_body = Mat([0, 0, 0])

        for idx, axis in enumerate(about):
            constraint_axis = Mat([0, 0, 0])
            constraint_axis['xyz'.index(axis)] = 1
            torque = τ[idx] * self.Rb_I @ Mat(constraint_axis)
            self.torques_on_body += torque
            torques_on_other_body -= torque

        self.other_bodies.append(
            (torques_on_other_body, otherlink.Rb_I, about))

    def calc_eom(self, q, dq, ddq) -> Mat:
        ang_vel_body = utils.skew_symmetric(self.Rb_I, q, dq)
        dW = Mat([self.torques_on_body.dot(ang_vel_body)])
        Q = dW.jacobian(dq).T

        for (torques_on_body, Rb_I, about) in self.other_bodies:
            ang_vel = utils.skew_symmetric(Rb_I, q, dq)
            dW = Mat([torques_on_body.dot(ang_vel)])
            Q += dW.jacobian(dq).T

            if hasattr(self, 'torque_speed_limit'):
                for ax in about:
                    self.torque_speed_limit.add_rel_vel(
                        (ang_vel_body - ang_vel)['xyz'.index(ax)], ax,
                    )

        return Q

    def add_vars_to_pyomo_model(self, m: ConcreteModel) -> None:
        Tc_set = Set(initialize=range(len(self.input_torques)),
                     name='Tc_set', ordered=True)
        Tc = Var(m.fe, Tc_set, name='Tc', bounds=self.torque_bounds)

        self.pyomo_params: Dict[str, Param] = {}
        self.pyomo_sets: Dict[str, Set] = {'Tc_set': Tc_set}
        self.pyomo_vars: Dict[str, Var] = {'Tc': Tc}

        utils.add_to_pyomo_model(m, self.name, [
            self.pyomo_params.values(),
            self.pyomo_sets.values(),
            self.pyomo_vars.values(),
        ])

    def __getitem__(self, varname: str) -> Var:
        return self.pyomo_vars[varname]

    def get_pyomo_vars(self, fe: int, cp: int) -> List:
        return [
            *self.pyomo_vars['Tc'][fe, :],
        ]

    def get_sympy_vars(self) -> List[sp.Symbol]:
        return [
            *self.input_torques,
        ]

    def add_equations_to_pyomo_model(self,
                                     sp_variables: List[sp.Symbol],
                                     pyo_variables: 'VariableList',
                                     collocation: str):
        if hasattr(self, 'torque_speed_limit'):
            self.torque_speed_limit.add_equations_to_pyomo_model(
                sp_variables, pyo_variables, collocation,
                self.pyomo_vars['Tc'], self.pyomo_sets['Tc_set'])

    def save_data_to_dict(self) -> Dict[str, Any]:
        Tc_set = self.pyomo_sets['Tc_set']

        return {
            'name': self.name,
            'Tc': utils.get_vals(self.pyomo_vars['Tc'], (Tc_set,)) if len(Tc_set) > 0 else [],
        }

    def init_from_dict_one_point(self,
                                 data: Dict[str, Any],
                                 fed: int, cpd: int,
                                 fes: Optional[int] = None, cps: Optional[int] = None,
                                 **kwargs
                                 ) -> None:
        if fes is None:
            fes = fed - 1

        if cps is None:
            cps = cpd - 1

        # assert self.name == data['name']  # TODO: uncomment!

        if len(data['Tc']) > 0:
            for idx, T in enumerate(self.pyomo_sets['Tc_set']):
                utils.maybe_set_var(
                    self.pyomo_vars['Tc'][fed, T], data['Tc'][fes, idx], **kwargs)

    def torque_squared_cost(self):
        # TODO: add an option to scale by time and body weight?
        # previous tests slowed things down quite a bit, so I'm not sure
        # it's worth it, especially since we're not actually doing global
        # optimization in any case
        return sum([Tc**2 for Tc in self.pyomo_vars['Tc'][:, :]])

    def animation_setup(self, fig, ax, data: List[List[float]]):
        pass

    def animation_update(self, fig, ax,
                         fe: Optional[int] = None,
                         t: Optional[float] = None,
                         t_arr: Optional[np.ndarray] = None,
                         track: bool = False):
        pass

    def cleanup_animation(self, fig, ax):
        pass

    def plot(self) -> None:
        import matplotlib.pyplot as plt
        try:
            Tc_set = self.pyomo_sets['Tc_set']
            Tc = utils.get_vals(self.pyomo_vars['Tc'], (Tc_set,))
            plt.plot(Tc)
            plt.legend(Tc_set)
            plt.grid(True)
            plt.title('Input torques in link ' + self.name)
            plt.xlabel('Finite element')
            plt.ylabel('$T$ [Nm/body_weight]')
            plt.show()
        except StopIteration:
            pass

        if hasattr(self, 'torque_speed_limit'):
            self.torque_speed_limit.plot()

    def __repr__(self) -> str:
        if hasattr(self, 'torque_speed_limit'):
            tsl = f'\n{" "*6}{self.torque_speed_limit}'
        else:
            tsl = ''
        return f'Motor3D(name="{self.name}", torque_bounds={self.torque_bounds}{tsl})'

def add_torque(link, otherlink, about: str, name: Optional[str] = None, **kwargs):
    if name is None:
        name = f'{link.name}_{otherlink.name}_torque'

    assert name not in link.nodes,\
        f'This link already has a node with the name {name}'

    motor = Motor3D(str(name), link.Rb_I, **kwargs)
    link.nodes[name] = motor
    motor.add_input_torques_at(otherlink, about=about)
    return motor

def torques(robot_or_link: Union[System3D, Link3D]) -> List[Motor3D]:
    from typing import cast

    if isinstance(robot_or_link, System3D):
        robot = robot_or_link
        return [cast(Motor3D, node)
                for link in robot.links
                for node in link.nodes.values()
                if isinstance(node, Motor3D)]
    else:
        link = robot_or_link
        return [cast(Motor3D, node)
                for node in link.nodes.values()
                if isinstance(node, Motor3D)]

def torque_squared_penalty(robot: 'System3D'):
    return sum(
        torque.torque_squared_cost()
        for torque in torques(robot)
    )

# TODO: this outputs an Iterator with `len(m.fe) * len(Tc_set)` elements
# perhaps an Iterator of Iterators would make more sense?
def power(motor: Motor3D, pyo_variables, fe: Optional[int] = None) -> Iterator:
    if hasattr(motor, 'torque_speed_limit'):
        rel_angle_vels_f = motor.torque_speed_limit.rel_angle_vels_f

        Tc = motor.pyomo_vars['Tc']
        Tc_set = motor.pyomo_sets['Tc_set']
        m = Tc.model()
        v = pyo_variables

        # ω * τ
        if fe is not None:
            return (
                rel_angle_vels_f[idx](v[fe, 1]) * Tc[fe, idx]
                for idx in Tc_set
            )
        else:
            return (
                rel_angle_vels_f[idx](v[fe, 1]) * Tc[fe, idx]
                for fe in m.fe
                for idx in Tc_set
            )
    else:
        raise RuntimeError(
            'Current impementation requires a TorqueSpeedLimit :/')

def work_penalty(robot: 'System3D'):
    return sum(
        sum(power(motor, robot.pyo_variables))
        for motor in torques(robot)
    )

def work_squared_penalty(robot: 'System3D', with_time: bool):
    if with_time:
        hm = robot.m.hm
        hm0 = robot.m.hm0.value
        nfe = len(robot.m.fe)

        # enumerate returns 0-based, pyomo expects 1-based
        return sum(
            sum(hm[(fe % nfe)+1]*hm0*P**2
                for (fe, P) in enumerate(power(motor, robot.pyo_variables)))
            for motor in torques(robot)
        )
    else:
        return sum(
            sum(P**2 for P in power(motor, robot.pyo_variables))
            for motor in torques(robot)
        )

def max_power_constraint(robot: 'System3D', maxpower: float):
    def powerlimit_f(m, fe: int):
        P = sum(
            sum(power(motor, robot.pyo_variables, fe))
            for motor in torques(robot)
        )
        return P**2 <= maxpower**2

    constraintname = f'{robot.name}_maxpower'
    assert not hasattr(robot.m, constraintname), \
        f'The model already has a constraint with the name {constraintname}'

    setattr(robot.m, constraintname,
            Constraint(robot.m.fe, rule=powerlimit_f))

reason: import numpy,import sympy,from sympy,from pyomo


text:
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
from abc import ABCMeta, abstractmethod

import mmcv
import numpy as np
from PIL import Image

class BaseDataSource(object, metaclass=ABCMeta):
    """Datasource base class to load dataset information.

    Args:
        data_prefix (str): the prefix of data path.
        classes (str | Sequence[str], optional): Specify classes to load.
        ann_file (str | None): the annotation file. When ann_file is str,
            the subclass is expected to read from the ann_file. When ann_file
            is None, the subclass is expected to read according to data_prefix.
        test_mode (bool): in train mode or test mode. Defaults to False.
        color_type (str): The flag argument for :func:`mmcv.imfrombytes()`.
            Defaults to color.
        file_client_args (dict): Arguments to instantiate a FileClient.
            See :class:`mmcv.fileio.FileClient` for details.
            Defaults to dict(backend='disk').
    """

    CLASSES = None

    def __init__(self,
                 data_prefix,
                 classes=None,
                 ann_file=None,
                 test_mode=False,
                 color_type='color',
                 file_client_args=dict(backend='disk')):
        self.data_prefix = data_prefix
        self.ann_file = ann_file
        self.test_mode = test_mode
        self.color_type = color_type
        self.file_client_args = file_client_args
        self.file_client = None
        self.CLASSES = self.get_classes(classes)
        self.data_infos = self.load_annotations()

    def __len__(self):
        return len(self.data_infos)

    @abstractmethod
    def load_annotations(self):
        pass

    def get_cat_ids(self, idx):
        """Get category id by index.

        Args:
            idx (int): Index of data.

        Returns:
            int: Image category of specified index.
        """

        return self.data_infos[idx]['gt_label'].astype(np.int)

    def get_gt_labels(self):
        """Get all ground-truth labels (categories).

        Returns:
            list[int]: categories for all images.
        """

        gt_labels = np.array([data['gt_label'] for data in self.data_infos])
        return gt_labels

    def get_img(self, idx):
        """Get image by index.

        Args:
            idx (int): Index of data.

        Returns:
            Image: PIL Image format.
        """
        if self.file_client is None:
            self.file_client = mmcv.FileClient(**self.file_client_args)

        if self.data_infos[idx].get('img_prefix', None) is not None:
            if self.data_infos[idx]['img_prefix'] is not None:
                filename = osp.join(
                    self.data_infos[idx]['img_prefix'],
                    self.data_infos[idx]['img_info']['filename'])
            else:
                filename = self.data_infos[idx]['img_info']['filename']
            img_bytes = self.file_client.get(filename)
            img = mmcv.imfrombytes(img_bytes, flag=self.color_type)
        else:
            img = self.data_infos[idx]['img']

        img = img.astype(np.uint8)
        return Image.fromarray(img)

    @classmethod
    def get_classes(cls, classes=None):
        """Get class names of current dataset.

        Args:
            classes (Sequence[str] | str | None): If classes is None, use
                default CLASSES defined by builtin dataset. If classes is a
                string, take it as a file name. The file contains the name of
                classes where each line contains one class name. If classes is
                a tuple or list, override the CLASSES defined by the dataset.

        Returns:
            tuple[str] or list[str]: Names of categories of the dataset.
        """
        if classes is None:
            return cls.CLASSES

        if isinstance(classes, str):
            # take it as a file path
            class_names = mmcv.list_from_file(classes)
        elif isinstance(classes, (tuple, list)):
            class_names = classes
        else:
            raise ValueError(f'Unsupported type {type(classes)} of classes.')

        return class_names

max_stars_repo_name: XingzhiZhou/mmselfsup

max_issues_repo_name: XingzhiZhou/mmselfsup

max_forks_repo_name: XingzhiZhou/mmselfsup


text:
import typing as t

import numpy as np
import pandas as pd
from pandas.core.generic import NDFrame

ArrType = t.TypeVar("ArrType", np.ndarray, NDFrame)
ArrCallable = t.Callable[[ArrType], ArrType]
ColRef = t.TypeVar("ColRef", str, pd.Series)

def tabulate_one_way(
        x: pd.Series,
        values: t.Optional[pd.Series] = None,
        aggfunc: t.Optional[t.Union[ArrCallable, t.Iterable[ArrCallable]]] = None,
        **kwargs
) -> pd.DataFrame:
    if values is None and aggfunc is not None:
        raise ValueError("aggfunc cannot be used without values.")

    elif aggfunc is None and values is not None:
        raise ValueError("values cannot be used without an aggfunc.")

    elif values is None and aggfunc is None:
        values = pd.Series(1, index=x.index, name="_values")

        def percent(v):
            return np.round(np.size(v) / len(x) * 100, 2)

        aggfunc = [np.size, percent]

    if not isinstance(aggfunc, t.Iterable):
        aggfunc = [aggfunc]

    df = pd.concat([x, values], axis=1)
    return df.groupby(x.name, **kwargs)[values.name].agg(aggfunc)

def tabulate(
        x: t.Union[pd.Series, t.Iterable[pd.Series]],
        y: t.Optional[t.Union[pd.Series, t.Iterable[pd.Series]]] = None,
        values: t.Optional[pd.Series] = None,
        aggfunc: t.Optional[t.Union[ArrCallable, t.Iterable[ArrCallable]]] = None,
        **kwargs
) -> pd.DataFrame:
    if y is None:
        return tabulate_one_way(x, values=values, aggfunc=aggfunc, **kwargs)
    else:
        return pd.crosstab(x, y, values=values, aggfunc=aggfunc, **kwargs)

class BaseTabAccessor(object):

    def __init__(self, pandas_obj: pd.DataFrame):
        self._obj = pandas_obj

    def _lookup(
            self,
            c: t.Optional[t.Union[ColRef, t.Iterable[ColRef]]]
    ) -> t.Optional[t.Union[pd.Series, t.List[pd.Series]]]:
        if c is None or isinstance(c, pd.Series):
            return c
        else:
            if isinstance(c, str):
                return self._obj[c]
            elif isinstance(c, t.Iterable):
                return list(map(self._lookup, c))
            else:
                raise self._obj[c]

@pd.api.extensions.register_dataframe_accessor("tab")
class TabDataFrameAccessor(BaseTabAccessor):

    def __call__(
            self,
            x: t.Union[ColRef],
            y: t.Optional[t.Union[ColRef, t.Iterable[ColRef]]] = None,
            values: t.Optional[pd.Series] = None,
            aggfunc: t.Optional[t.Union[ArrCallable, t.Iterable[ArrCallable]]] = None,
            **kwargs
    ) -> pd.DataFrame:
        x = self._lookup(x)
        y = self._lookup(y)
        values = self._lookup(values)
        return tabulate(x, y, values=values, aggfunc=aggfunc, **kwargs)

@pd.api.extensions.register_series_accessor("tab")
class TabSeriesAccessor(BaseTabAccessor):

    def __call__(
            self,
            x: t.Union[ColRef],
            y: t.Optional[ColRef] = None,
            values: t.Optional[pd.Series] = None,
            aggfunc: t.Optional[t.Union[ArrCallable, t.Iterable[ArrCallable]]] = None,
            **kwargs
    ) -> pd.DataFrame:
        x = self._lookup(x)
        values = self._lookup(values)
        return tabulate_one_way(x, values=values, aggfunc=aggfunc, **kwargs)


text:
#!/usr/bin/env python
# tribe-admin
# Administrative utility for working with Tribe data
#
# Author:   Benjamin Bengfort <benjamin@bengfort.com>
# Created:  Thu Nov 13 14:08:12 2014 -0500
#
# Copyright (C) 2014 Bengfort.com
# For license information, see LICENSE.txt
#
# ID: tribe-admin.py [5232e54] benjamin@bengfort.com $

"""
Administrative utility for working with Tribe data
"""

##########################################################################
## Imports
##########################################################################

import sys
import json
import tribe
import argparse
import networkx as nx

from tribe.utils import timeit
from tribe.utils import humanizedelta
from tribe.viz import draw_social_network
from tribe.extract import ConsoleMBoxReader as MBoxReader

##########################################################################
## Command Variables
##########################################################################

DESCRIPTION = "An administrative utility for the Tribe Social Network Analysis"
EPILOG      = "If there are any bugs or concerns, submit an issue on Github"
VERSION     = "tribe v{}".format(tribe.__version__)

##########################################################################
## Commands
##########################################################################

def header_analysis(args):
    """
    Perform a header analysis on an MBox
    """

    @timeit
    def timed_inner(path):
        reader  = MBoxReader(path)
        return reader.header_analysis()

    headers, seconds = timed_inner(args.mbox[0])

    if args.show:
        headers.plot()

    json.dump(headers, args.write)
    return "Analysis complete in {}".format(humanizedelta(seconds=seconds))

def count_emails(args):
    """
    Count the number of emails in an MBox.
    """

    @timeit
    def timed_inner(path):
        reader = MBoxReader(path)
        return reader.count()

    count, seconds = timed_inner(args.mbox[0])
    elapsed = humanizedelta(seconds=seconds)
    return "Found {:,} emails in {}".format(count, elapsed)

def extract(args):
    """
    Extract a GraphML file from an MBox
    """

    @timeit
    def timed_inner(path, outpath):
        reader = MBoxReader(path)
        G = reader.extract_graph()
        nx.write_graphml(G, outpath)
        return reader.errors

    print("Starting Graph extraction, a long running process")
    errors, seconds = timed_inner(args.mbox[0], args.write)
    print("GraphML written out to {}".format(args.write.name))

    if errors:
        print("\nThe following errors were encountered:")
        for err, num in errors.most_common():
            print("    {}: {}".format(num, err))
        print("\n")
    else:
        print("\nNo errors encountered in processing\n")

    return "Graph extraction took {}".format(humanizedelta(seconds=seconds))

def info(args):
    """
    Print information about a GraphML file
    """
    for idx, path in enumerate(args.graphml):
        G = nx.read_graphml(path)
        print(nx.info(G))

        if idx < len(args.graphml) - 1:
            print("----")

    return ""

def draw(args):
    """
    Draw a GraphML with the tribe draw method.
    """
    G = nx.read_graphml(args.graphml[0])
    draw_social_network(G, args.write)
    return ""

##########################################################################
## Main Function and Methodology
##########################################################################

def main(*args):

    # Construct the argument parser
    parser = argparse.ArgumentParser(description=DESCRIPTION, epilog=EPILOG)
    subparsers = parser.add_subparsers(title='commands', description='Administrative commands for Tribe')

    # Version Command
    parser.add_argument('-v', '--version', action='version', version=VERSION)

    # Headers Analysis Command
    headers_parser = subparsers.add_parser('headers', help='Perform an analysis of the email headers in an MBox')
    headers_parser.add_argument('-w', '--write', type=argparse.FileType('w'), default=sys.stdout, help='Location to write data to')
    headers_parser.add_argument('-s', '--show', action='store_true', default=False, help='Show graph of key distribution')
    headers_parser.add_argument('mbox', type=str, nargs=1, help='Path or location to MBox for analysis')
    headers_parser.set_defaults(func=header_analysis)

    # Count Emails Command
    count_parser = subparsers.add_parser('count', help='Count the number of emails in an MBox.')
    count_parser.add_argument('mbox', type=str, nargs=1, help='Path or location to MBox for analysis')
    count_parser.set_defaults(func=count_emails)

    # Extract Command
    extract_parser = subparsers.add_parser('extract', help='Extract a GraphML file from an MBox')
    extract_parser.add_argument('-w', '--write', type=argparse.FileType('wb'), default=sys.stdout, help='Location to write data to')
    extract_parser.add_argument('mbox', type=str, nargs=1, help='Path or location to MBox for analysis')
    extract_parser.set_defaults(func=extract)

    # Graph Info Command
    info_parser = subparsers.add_parser('info', help='Print information about a GraphML file')
    info_parser.add_argument('graphml', nargs="+", type=argparse.FileType('r'), help='Location of MBox(es) to get info for')
    info_parser.set_defaults(func=info)

    # Draw Command
    draw_parser = subparsers.add_parser('draw', help='Draw a GraphML using the tribe draw method')
    draw_parser.add_argument('-w', '--write', type=str, default=None, help='Location to draw to')
    draw_parser.add_argument('graphml', nargs=1, type=argparse.FileType('r'), help='Location of GraphML to draw')
    draw_parser.set_defaults(func=draw)

    # Handle input from the command line
    args = parser.parse_args()            # Parse the arguments
    try:
        msg = args.func(args)             # Call the default function
        parser.exit(0, msg+"\n")               # Exit cleanly with message
    except Exception as e:
        parser.error(str(e))              # Exit with error

if __name__ == '__main__':
    main(*sys.argv[1:])

max_stars_repo_name: looselycoupled/tribe

max_issues_repo_name: rotationalio/tribe

max_forks_repo_name: rotationalio/tribe

reason: import networkx


text:
"""
 _____ _              _
/  ___| |            | |
\ `--.| |_ ___  _ __ | |
 `--. \ __/ _ \| '_ \| |
/\__/ / || (_) | |_) |_|
\____/ \__\___/| .__/(_)
               | |
               |_|

 _   _                         _    _ _              _            _      _                   _        __                 _      _
| | | |                       | |  (_) |            | |          | |    (_)                 | |      / _|               | |    | |
| | | |___  ___     __ _ _ __ | | ___| |_           | |_ ___  ___| |_    _ _ __  _   _ _ __ | |__   | |_ ___  _ __    __| | ___| |__  _   _  __ _
| | | / __|/ _ \   / _` | '_ \| |/ / | __|          | __/ _ \/ __| __|  | | '_ \| | | | '_ \| '_ \  |  _/ _ \| '__|  / _` |/ _ \ '_ \| | | |/ _` |
| |_| \__ \  __/  | (_| | |_) |   <| | |_           | ||  __/\__ \ |_ _ | | |_) | |_| | | | | |_) | | || (_) | |    | (_| |  __/ |_) | |_| | (_| |
 \___/|___/\___|   \__, | .__/|_|\_\_|\__|           \__\___||___/\__(_)|_| .__/ \__, |_| |_|_.__/  |_| \___/|_|     \__,_|\___|_.__/ \__,_|\__, |
                    __/ | |                 ______                      | |     __/ |                                                      __/ |
                   |___/|_|                |______|                     |_|    |___/                                                      |___/

=========================================================

  _____           __      _______
 |  __ \         /\ \    / /  __ \
 | |__) |   _   /  \ \  / /| |  | |
 |  ___/ | | | / /\ \ \/ / | |  | |
 | |   | |_| |/ ____ \  /  | |__| |
 |_|    \__, /_/    \_\/   |_____/           |
         __/ |                               |
        |___/                                |
                                           .-'-.
                                          ' ___ '
                                ---------'  .-.  '---------
                _________________________'  '-'  '_________________________
                 ''''''-|---|--/    \==][^',_m_,'^][==/    \--|---|-''''''
                               \    /  ||/   H   \||  \    /
                                '--'   OO   O|O   OO   '--'

PyAVD - A Python package to pretty much -solve- aircraft conceptual design!

AVD Group 11, Department of Aeronautics, Imperial College London.

"""

import gpkit
import streamlit as st
from streamlit import session_state as sesh

from Models import *
from gpkit import Model, ureg
from gpkit.constraints.bounded import Bounded

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Set up the page config
st.set_page_config(page_title="PyAVD",
                    page_icon="https://ichef.bbci.co.uk/news/976/cpsprodpb/117D1/production/_98633617_mediaitem98633616.jpg",
                    layout="centered")

## For dodgy situations with signomial inequalities...
# with gpkit.SignomialsEnabled():

#     AC = Aircraft()
#     # print(AC)

#     MISSION = Mission(AC)
#     # print(MISSION)

#     M = Model(AC.M_0 * AC.T0_W0, [Bounded(MISSION), Bounded(AC)])
#     print(M)

#     print(M.variables_byname('AR'))

#     sol = M.localsolve(verbosity=1)
#     print(sol.table())

#     print(M.program.results)

AC = Aircraft()
# print(AC)

MISSION = Mission(AC)
# print(MISSION)

# M = Model(AC.M_0 * AC.T0_W0, [Bounded(MISSION), Bounded(AC)])
M = Model(AC.M_0 * AC.T0_W0, [MISSION, AC])
print(M)

print(M.variables_byname('AR'))

sol = M.solve(verbosity=1)
print(sol.table())


text:
import logging
import os

import numpy as np

from LMmodel import punc_transformer
from LMmodel.tf2_trm import Transformer, tf
from utils.text_featurizers import TextFeaturizer

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

class LM():
    def __init__(self, config, punc_config=None):
        self.config = config
        self.am_featurizer = TextFeaturizer(config['am_token'])
        self.lm_featurizer = TextFeaturizer(config['lm_token'])
        self.model_config = self.config['model_config']
        self.model_config.update(
            {'input_vocab_size': self.am_featurizer.num_classes, 'target_vocab_size': self.lm_featurizer.num_classes})
        self.punc_config = punc_config
        if punc_config:
            self.punc_vocab_featurizer = TextFeaturizer(punc_config['punc_vocab'])
            self.punc_bd_featurizer = TextFeaturizer(punc_config['punc_biaodian'])
            self.punc_model_config = self.punc_config['model_config']
            self.punc_model_config.update({'input_vocab_size': self.punc_vocab_featurizer.num_classes,
                                           'bd_vocab_size': self.punc_bd_featurizer.num_classes})

    def load_model(self, training=True):
        self.model = Transformer(**self.model_config)
        if self.punc_config is not None:
            self.punc_model = punc_transformer.Transformer(**self.punc_model_config)
        if not training:
            self.model._build()

            if self.punc_config is not None:
                self.punc_model._build()
            self.load_checkpoint()

        self.model.start_id = self.lm_featurizer.start
        self.model.end_id = self.lm_featurizer.stop

    def convert_to_pb(self, export_path):
        import tensorflow as tf
        self.model.inference(np.ones([1, 10], 'int32'))

        concrete_func = self.model.inference.get_concrete_function()
        tf.saved_model.save(self.model, export_path, signatures=concrete_func)

    def load_checkpoint(self, ):
        """Load checkpoint."""

        self.checkpoint_dir = os.path.join(self.config['running_config']["outdir"], "checkpoints")
        files = os.listdir(self.checkpoint_dir)
        files.sort(key=lambda x: int(x.split('_')[-1].replace('.h5', '')))
        self.model.load_weights(os.path.join(self.checkpoint_dir, files[-1]))
        if self.punc_config is not None:
            self.checkpoint_dir = os.path.join(self.punc_config['running_config']["outdir"], "checkpoints")
            files = os.listdir(self.checkpoint_dir)
            files.sort(key=lambda x: int(x.split('_')[-1].replace('.h5', '')))
            self.punc_model.load_weights(os.path.join(self.checkpoint_dir, files[-1]))

    def encode(self, word, token):
        x = [token.start]
        for i in word:
            x.append(token.token_to_index[i])
        x.append(token.stop)
        return np.array(x)[np.newaxis, :]

    def decode(self, out, token):
        de = []
        for i in out[1:]:
            de.append(token.index_to_token[i])
        return de

    def predict(self, pins):
        x = self.encode(pins, self.am_featurizer)
        result = self.model.inference(x)
        return result

    def creat_mask(self, seq):
        seq_pad = tf.cast(tf.equal(seq, 0), tf.float32)
        return seq_pad[:, tf.newaxis, tf.newaxis, :]  # (batch_size, 1, 1, seq_len)

    def only_chinese(self, word):
        n = ''
        for ch in word:
            if '\u4e00' <= ch <= '\u9fff':
                n += ch
        return n

    def punc_predict(self, txt):
        chinese = self.only_chinese(txt)
        x=np.array(self.encode(chinese, self.punc_vocab_featurizer),'int32')
        mask = self.creat_mask(x)
        result = self.punc_model.inference(x, mask)
        decoded = self.punc_decoded(chinese,result[0].numpy())
        value = self.iextract(decoded, txt)
        return value

    def iextract(self, decoded, input_strs):

        idx = 0
        inp = list(input_strs)
        for n in decoded:
            idx_ = inp.index(n[0], idx)
            inp[idx_] = ''.join(n)
            idx = idx_ + 1

        return inp

    def punc_decoded(self,chinese,bd_out):
        de = []
        for i in range(1, len(chinese) + 1):
            now = [chinese[i - 1]]

            if bd_out[i].argmax(-1) > 1 and bd_out[i].max() >= 0.8:
                result = bd_out[i].argmax(-1)
                now.append(self.punc_bd_featurizer.vocab_array[result])
            de.append(now)
        return de


text:
from scipy import linalg
import numpy as np

from system_functions import apply_bc_by_reduction, recuperate_bc_by_extension

def eigenvalue_solve(k,m, n_el=3, dof_labels=['y','g']):

    n_nodes = n_el + 1

    eig_values_raw, eigen_modes_raw = linalg.eigh(apply_bc_by_reduction(k),
                                                  apply_bc_by_reduction(m))

    eig_values = np.sqrt(np.real(eig_values_raw))

    eigenfrequencies = eig_values / 2. / np.pi #rad/s
    eig_periods = 1 / eigenfrequencies

    gen_mass = np.matmul(np.matmul(np.transpose(eigen_modes_raw), apply_bc_by_reduction(m)), eigen_modes_raw)

    is_identiy = np.allclose(gen_mass, np.eye(gen_mass.shape[0]))
    if not is_identiy:
        raise Exception('generalized mass is not identiy')

    eigenmodes = {}
    for dof in dof_labels:
        eigenmodes[dof] = []

    for i in range(len(eigenfrequencies)):
        for j, dof in enumerate(dof_labels):
            eigenmodes[dof].append(np.zeros(n_nodes))
            eigenmodes[dof][i][1:] = eigen_modes_raw[j:,i][::len(dof_labels)]

    return eigenmodes, eigenfrequencies

def static_solve(k, force_vector=np.array([750.0, 25.0, 1250.0, 35.0, 1500.0, 45.0]), n_el=3, dof_labels=['y','g']):
    n_nodes = n_el + 1

    static_result = np.linalg.solve(apply_bc_by_reduction(k), force_vector)

    static_deformation = {}
    for dof in dof_labels:
        static_deformation[dof] = np.zeros(n_nodes)

    for i, label in enumerate(dof_labels):
        static_deformation[label][1:] = static_result[i::len(dof_labels)]

    return static_deformation

if __name__ == '__main__':

    import matplotlib.pyplot as plt
    from system_functions import build_system_matrix

    k_full, m_full = build_system_matrix([1.0,1.0,1.0])

    x = [0.0, 1.0, 2.0, 3.0]

    eigenmodes, eigenfrequencies = eigenvalue_solve(k_full, m_full)
    print('Eigenvalue solve results')
    for idx, freq in enumerate(eigenfrequencies):
        print('   Mode: ', str(idx+1))
        print('      Eigenfeq: ', str(freq))
        print('      y-entries: ', eigenmodes['y'][idx])
        print('      g-entries: ', eigenmodes['g'][idx])

        if idx == 0:
            fig, axs = plt.subplots(2)
            fig.suptitle('Eigenvalue solve')
            axs[0].plot(x, eigenmodes['y'][idx])
            axs[1].plot(x, eigenmodes['g'][idx])
            plt.show()

    static_deformation = static_solve(k_full)
    print('Static solve results')
    print('   y-entries: ', static_deformation['y'])
    print('   g-entries: ', static_deformation['g'])

    fig, axs = plt.subplots(2)
    fig.suptitle('Static solve')
    axs[0].plot(x, static_deformation['y'])
    axs[1].plot(x, static_deformation['g'])
    plt.show()

max_stars_repo_name: JoZimmer/Beam-Models

max_issues_repo_name: JoZimmer/Beam-Models

max_forks_repo_name: JoZimmer/Beam-Models

reason: import numpy,from scipy


text:
# Code for the ex-post analysis (segmentation, market shares, etc.)

# General
import sys
import time
import copy
import matplotlib.pyplot as plt
import numpy as np

# Data
import Data_LinSibdari_MNL as data_file

def printOutputAnalysis(data):

    # Print prices, subsidies, taxes, market shares for all alternatives
    print('\nALTERNATIVES:              ', end=" ")
    for i in range(data['I_tot']):
        print('   {:2d}    '.format(i), end=" ")
    print('\n\nPRICES                     ', end =" ")
    for i in range(data['I_tot']):
        print(' {:8.4f}'.format(data['p_fixed'][i]), end=" ")
    print('\nTOTAL DEMAND               ', end =" ")
    for i in range(data['I_tot']):
        print(' {:8.3f}'.format(data['output']['demand'][i]), end=" ")
    print('\nTOTAL MARKET SHARE         ', end =" ")
    for i in range(data['I_tot']):
        print(' {:8.4f}'.format(data['output']['market_share'][i]), end=" ")

    # Print profits for all suppliers
    print('\n\n\nSUPPLIERS:             ', end=" ")
    for k in range(data['K']+1):
        print('   {:2d}    '.format(k), end=" ")
    print('\nPROFITS               ', end =" ")
    for k in range(data['K']+1):
        print(' {:8.4f}'.format(data['output']['profit'][k]), end=" ")

    # Print utilities for all customers
    print('\n\nCUSTOMERS:             ', end=" ")
    for n in range(data['N']):
        print('   {:2d}    '.format(n), end=" ")
    print('\nSIZE SEGMENT           ', end=" ")
    for n in range(data['N']):
        print(' {:4.0f}    '.format(data['popN'][n]), end=" ")
    print('\nUTILITIES             ', end =" ")
    for n in range(data['N']):
        print(' {:8.2f}'.format(data['output']['EMU'][n]), end=" ")
    print('\nPROB 0                ', end=" ")
    for n in range(data['N']):
        print(' {:8.2f}'.format(data['output']['P'][0,n]), end=" ")
    print('\nPROB 1                ', end =" ")
    for n in range(data['N']):
        print(' {:8.2f}'.format(data['output']['P'][1,n]), end=" ")
    print('\nPROB 2                ', end =" ")
    for n in range(data['N']):
        print(' {:8.2f}'.format(data['output']['P'][2,n]), end=" ")
    print()

def calculation(data):

    price = copy.deepcopy(data['p_fixed'])

    data['output'] = {}
    data['output']['U'] = np.zeros([data['I_tot'], data['N'], data['R']])
    data['output']['EMU'] = np.zeros([data['N']])
    data['output']['w'] = np.zeros([data['I_tot'], data['N'], data['R']])
    data['output']['P'] = np.zeros([data['I_tot'], data['N']])
    data['output']['profit'] = np.zeros([data['K']+1])
    data['output']['demand'] = np.zeros([data['I_tot']])
    data['output']['market_share'] = np.zeros([data['I_tot']])

    # Calculate utilities and choices
    for n in range(data['N']):
        for r in range(data['R']):
            if data['DCM'] == 'MixedLogit':
                data['output']['U'][0,n,r] = data['xi'][0, n, r]
                data['output']['U'][1,n,r] = data['endo_coef'][1,n,r] * price[1] + data['exo_utility'][1,n,r] + data['xi'][1, n, r]
                data['output']['U'][2,n,r] = data['endo_coef'][2,n,r] * price[2] + data['exo_utility'][2,n,r] + data['xi'][2, n, r]
            else:
                data['output']['U'][0,n,r] = data['xi'][0, n, r]
                data['output']['U'][1,n,r] = data['beta'] * price[1] + data['exo_utility'][1,n] + data['xi'][1, n, r]
                data['output']['U'][2,n,r] = data['beta'] * price[2] + data['exo_utility'][1,n] + data['xi'][2, n, r]
            i_UMax = np.argmax(data['output']['U'][:,n,r])
            data['output']['EMU'][n] += data['output']['U'][i_UMax, n, r]/data['R']
            data['output']['w'][i_UMax,n,r] = 1
            data['output']['P'][i_UMax, n] += 1/data['R']

    # Calculate demand
    for i in range(data['I_tot']):
        for n in range(data['N']):
            data['output']['demand'][i] += data['output']['P'][i,n] * data['popN'][n]

    # Calculate market shares
    data['output']['market_share'] = data['output']['demand'] / data['Pop']

    # Calculate profits
    for k in range(data['K'] + 1):
        for i in data['list_alt_supplier'][k]:
            data['output']['profit'][k] += data['output']['demand'][i] * price[i]

def linearProfitCurve():

        data = data_file.getData()
        data_file.printCustomers(data)

        price = copy.deepcopy(data['p_fixed'])

        opt = data['optimizer']
        price[opt] = data['lb_p'][opt]

        while price[opt] <= data['ub_p'][opt]:

            data['output'] = {}
            data['output']['U'] = np.zeros([data['I_tot'], data['N'], data['R']])
            data['output']['EMU'] = np.zeros([data['N']])
            data['output']['w'] = np.zeros([data['I_tot'], data['N'], data['R']])
            data['output']['P'] = np.zeros([data['I_tot'], data['N']])
            data['output']['profit'] = np.zeros([data['K']+1])
            data['output']['demand'] = np.zeros([data['I_tot']])
            data['output']['market_share'] = np.zeros([data['I_tot']])

            # Calculate utilities and choices
            for n in range(data['N']):
                for r in range(data['R']):
                    if data['DCM'] == 'MixedLogit':
                        data['output']['U'][0,n,r] = data['xi'][0, n, r]
                        data['output']['U'][1,n,r] = data['endo_coef'][i,n,r] * price[1] + data['exo_utility'][1,n,r] + data['xi'][1, n, r]
                        data['output']['U'][2,n,r] = data['endo_coef'][i,n,r] * price[2] + data['exo_utility'][2,n,r] + data['xi'][2, n, r]
                    else:
                        data['output']['U'][0,n,r] = data['xi'][0, n, r]
                        data['output']['U'][1,n,r] = data['beta'] * price[1] + data['exo_utility'][1,n] + data['xi'][1, n, r]
                        data['output']['U'][2,n,r] = data['beta'] * price[2] + data['exo_utility'][2,n] + data['xi'][2, n, r]
                    i_UMax = np.argmax(data['output']['U'][:,n,r])
                    data['output']['EMU'][n] += data['output']['U'][i_UMax, n, r]/data['R']
                    data['output']['w'][i_UMax,n,r] = 1
                    data['output']['P'][i_UMax, n] += 1/data['R']

            # Calculate demand
            for i in range(data['I_tot']):
                for n in range(data['N']):
                    data['output']['demand'][i] += data['output']['P'][i,n] * data['popN'][n]

            # Calculate market shares
            data['output']['market_share'] = data['output']['demand'] / data['Pop']

            # Calculate profits
            for k in range(data['K'] + 1):
                for i in data['list_alt_supplier'][k]:
                    data['output']['profit'][k] += data['output']['demand'][i] * price[i]

            data['p_fixed'] = price

            printOutputAnalysis(data)

            price[opt] += 1

def mainSimulation():

        replications = 100

        demand = np.zeros((replications,3))
        marketshares = np.zeros((replications,3))

        for seed in range(replications):

            # Set random seed
            np.random.seed(seed)

            # Read instance
            data = data_file.getData()

            data_file.printCustomers(data)

            calculation(data)
            printOutputAnalysis(data)

            demand[seed] = data['output']['demand']
            marketshares[seed] = data['output']['market_share']

        for seed in range(replications):
            print('\n{:4d}  '.format(seed+1), end =" ")
            for i in range(3):
                print(' {:6.4f}'.format(marketshares[seed][i]), end=" ")

if __name__ == '__main__':

    #mainSimulation()
    linearProfitCurve()


text:
# Copyright 2018-2020 Ivan Alles. See also the LICENSE file.

import os
import shutil

import cv2
import numpy as np

def make_clean_directory(path):
    """
    Creates an empty directory.
    If it exists, delete its content.
    :param path: path to the directory.
    """
    need_create = True
    try:
        if os.path.isdir(path):
            for file in os.listdir(path):
                file_path = os.path.join(path, file)
                if os.path.isfile(file_path):
                    os.remove(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
            need_create = False
        elif os.path.isfile(path):
            os.remove(path)
    except PermissionError:
        # This can be caused by Windows Explorer indexing images, so just ignore it.
        pass
    if need_create:
        os.makedirs(path)

def make_transform2(scale, angle=0, tx=0, ty=0):
    """
    Rotate, scale (about origin) and then translate 2d points.
    """
    ca = np.cos(angle) * scale
    sa = np.sin(angle) * scale
    t = np.array([ca, -sa, tx, sa, ca, ty, 0, 0, 1]).reshape(3, 3)
    return t

def save_tensor_as_images(tensor, dst_dir, prefix='', suffix='',
                          delete_old_files=False, mode=None,
                          file_name_format='{0}{1:0>3}{2}-{3:1d}.png'):
    """
    Save tensor as images.

    :param tensor: an array of shape (batch, c, h, w) or (c, h, w) (will be reshaped to (1, c, h, w)).
    :param prefix a prefix for image name. Can be a string or an array-like of strings for each batch element.
    :param suffix a suffix for image name. Can be a string or an array-like of strings for each batch element.
    :param mode: one of
        None: default
        'rgb': save as rgb if it has 3 channels.
        'rg': save negative values as red, positive as green.
    :param file_name_format string for the file name. Arguments are
        0: prefix
        1: index in the 0th axis of the tensor
        2: suffix.
        3: channel (index in the 1st axis of the tensor).
    """
    if tensor.ndim == 3:
        tensor.reshape((1,) + tensor.shape)
    elif tensor.ndim != 4:
        raise ValueError("Unsupported tensor shape")

    os.makedirs(dst_dir, exist_ok=True)
    if delete_old_files:
        for f in os.listdir(dst_dir):
            file_path = os.path.join(dst_dir, f)
            if os.path.isfile(file_path):
                os.remove(file_path)

    def save_image(i, image, channel):
        if image.ndim == 2 and mode == 'rg':
            image = red_green(image)

        file_prefix = prefix if type(prefix) == str else prefix[i]
        file_suffix = suffix if type(suffix) == str else suffix[i]

        file_name = file_name_format.format(file_prefix, i, file_suffix, channel)
        cv2.imwrite(os.path.join(dst_dir, file_name), image.astype(np.uint8))

    for i in range(0, tensor.shape[0]):
        if tensor[i].shape[2] == 3 and mode == 'rgb':
            save_image(i, tensor[i], 0)
        elif tensor[i].shape[2] == 1:
            save_image(i, tensor[i][:, :, 0], 0)
        else:
            for ci in range(tensor[i].shape[2]):
                save_image(i, tensor[i][:, :, ci], ci)

def red_green(image):
    """
    Convert a 1 channel image of positive and negative numbers into a color image.
    Red corresponds to negative numbers, green to positive ones.
    :param image: one channel image (H, W) or (H, W, 1).
    :return: a color image.
    """
    if np.ndim == 2:
        image = np.squeeze(image, axis=2)
    pos = (image >= 0) * image
    neg = (image <= 0) * image
    blue = np.zeros(image.shape, dtype=image.dtype)
    image = np.stack([blue, pos, -neg], axis=-1)
    return image

def save_batch_as_images(batch, dst_dir, fmt, prefix=''):
    make_clean_directory(dst_dir)

    maxcol = 230

    if hasattr(batch, 'input'):
        save_tensor_as_images(batch.input * maxcol, dst_dir, prefix=prefix, suffix='_i',
                              delete_old_files=False,
                              mode='rgb',
                              file_name_format=fmt)

    if hasattr(batch, 'output_on_image'):
        for cat in range(batch.output_on_image.shape[1]):
            save_tensor_as_images(batch.output_on_image[:, cat, 0] * maxcol, dst_dir,
                                  prefix=prefix, suffix=f'_{cat}_oyoi', mode='rgb', file_name_format=fmt)
            save_tensor_as_images(batch.output_on_image[:, cat, 1] * maxcol, dst_dir,
                                  prefix=prefix, suffix=f'_{cat}_oxoi', mode='rgb', file_name_format=fmt)

    def save_variable(batch, name, suffix, channel_names, factors, mode='rg'):
        if not hasattr(batch, name):
            return
        tensor = getattr(batch, name)
        if tensor is None:
            return
        tensor = np.array(tensor)
        for cat in range(tensor.shape[1]):
            for ch in range(tensor.shape[-1]):
                save_tensor_as_images(tensor[:, cat, :, :, ch: ch + 1] * factors[ch], dst_dir,
                                      prefix=prefix,
                                      suffix=f'_{cat:d}_{suffix}{channel_names[ch]}',
                                      mode=mode,
                                      file_name_format=fmt)

    pos_factor = maxcol * 0.05
    # Training only
    save_variable(batch, 'output_window', 'ow', ['y', 'x', 's', 'c'], [maxcol, maxcol, maxcol, maxcol])
    save_variable(batch, 'target_window', 'tw', ['y', 'x', 's', 'c'], [maxcol, maxcol, maxcol, maxcol])
    save_variable(batch, 'weight', 'w', [''], [maxcol])
    # Training & prediction
    save_variable(batch, 'output', 'o', ['y', 'x', 's', 'c'], [pos_factor, pos_factor, maxcol, maxcol])
    # Prediction only
    save_variable(batch, 'output_window_pos', 'ow', ['y', 'x'], [maxcol, maxcol])
    save_variable(batch, 'match_pos', 'm', [''], [maxcol])
    save_variable(batch, 'average_pos', 'a', ['y', 'x'], [maxcol, maxcol])
    save_variable(batch, 'average_angle', 'a', ['s', 'c'], [maxcol, maxcol])

def make_xy_tensor(shape):
    """
    Compute a tensor with homogeneous pixel coordinates.
    :param shape: tensor shape.
    :return: a tensor of shape: shape + (2,)
    """
    xy = np.ones((3,) + shape, dtype=np.int32)
    xy[0] = np.arange(0, shape[1])  # x
    xy[1] = np.arange(0, shape[0]).T.reshape(-1, 1)  # y
    return np.moveaxis(xy, 0, 2)

def draw_objects(image, objects, axis_length=10, thickness=1, scale=1, category_colors=None):
    """
    Draw poses on an image.
    :param image: the image.
    :param objects: ground truth or predicted objects.
    :param axis_length: a number or a tuple (lx, ly) of axis lengths.
    :param thickness: line thickness.
    :param scale: scale factor.
    :param category_colors: an array of colors for categories.
    :return:
    """
    maxcol = 1
    if image.dtype == np.uint8:
        maxcol = 255

    axis_length = np.broadcast_to(axis_length, (2,))

    if category_colors is None:
        category_colors = [[0, 0, 1],
                           [0, 1, 0],
                           [1, 0, 0],
                           [1, 0, 1],
                           [1, 1, 0],
                           [1, 0, 1],
                           [1, 1, 1]
                           ]

    category_colors = np.array(category_colors, dtype=np.float32) * maxcol

    for obj in objects:
        conf = obj.confidence if hasattr(obj, 'confidence') else 1
        conf = conf * 0.5 + 0.5
        color = category_colors[obj.category % len(category_colors)] * conf
        color = tuple([float(c) for c in color])
        t = make_transform2(1, obj.angle, obj.origin[0] * scale, obj.origin[1] * scale)
        points = np.array([
            [0, 0, 1],
            [axis_length[0], 0, 1],
            [0, 0, 1],
            [0, axis_length[1], 1]
        ], dtype=np.float32)

        points = np.dot(points, t.T)[:, :2].astype(int)

        cv2.line(image, tuple(points[0]), tuple(points[1]), color, thickness)
        cv2.line(image, tuple(points[2]), tuple(points[3]), color, thickness)


text:
from __future__ import (
    division,
    absolute_import,
    with_statement,
    print_function,
    unicode_literals,
)
import torch
import torch.optim as optim
import torch.optim.lr_scheduler as lr_sched
import torch.nn as nn
from torch.utils.data import DataLoader
import sys

sys.path.append("/home/tbarton/Pointnet2_PyTorch/")
import etw2.etw_pytorch_utils as pt_utils
import pprint
import os.path as osp
import os
import argparse
import numpy as np
import h5py

torch.manual_seed(0)
from generate_fake_data import run
from pointnet2.models import Pointnet2SemMSG as Pointnet
from pointnet2.models.pointnet2_msg_sem import model_fn_decorator
from pointnet2.data import Indoor3DSemSeg

parser = argparse.ArgumentParser(description="Arg parser")
parser.add_argument(
    "-batch_size", type=int, default=2, help="Batch size [default: 32]"
)
parser.add_argument(
    "-num_points",
    type=int,
    default=4096,
    help="Number of points to train with [default: 4096]",
)
parser.add_argument(
    "-v",
    type=str,
    default="test",
    help="Number of points to train with [default: 4096]",
)
# parser.add_argument(
#     "-file_train",
#     type = str,
#     default = "/home/theresa/datav1balancedtrain.h5",
#     help = ""
#     )
# parser.add_argument(
#     "-file_test",
#     type = str,
#     default = "/home/theresa/datav1balancedtest.h5",
#     help = ""
#     )
parser.add_argument(
    "-weight_decay",
    type=float,
    default=0,
    help="L2 regularization coeff [default: 0.0]",
)
parser.add_argument(
    "-lr", type=float, default=1e-4, help="Initial learning rate [default: 1e-2]"
)
parser.add_argument(
    "-lr_decay",
    type=float,
    default=0.5,
    help="Learning rate decay gamma [default: 0.5]",
)
parser.add_argument(
    "-decay_step",
    type=float,
    default=2e5,
    help="Learning rate decay step [default: 20]",
)
parser.add_argument(
    "-bn_momentum",
    type=float,
    default=0.9,
    help="Initial batch norm momentum [default: 0.9]",
)
parser.add_argument(
    "-bn_decay",
    type=float,
    default=0.5,
    help="Batch norm momentum decay gamma [default: 0.5]",
)
parser.add_argument(
    "-checkpoint", type=bool, default=True, help="Checkpoint to start from"
)
parser.add_argument(
    "-epochs", type=int, default=1000, help="Number of epochs to train for"
)
parser.add_argument(
    "-run_name",
    type=str,
    default="sem_seg_run_1",
    help="Name for run in tensorboard_logger",
)
parser.add_argument("--visdom-port", type=int, default=8097)
parser.add_argument("--visdom", action="store_true")

lr_clip = 1e-5
bnm_clip = 1e-2

if __name__ == "__main__":
    args = parser.parse_args()
    c = args.v.split("/")
    c = c[-2] + c[-1]
    file_test = f"/home/theresa/p/{c}test.h5"
    file_train = f"/home/theresa/p/{c}train.h5"
    f = h5py.File(file_train)
    median_data = f["labels"][:]
    medians = []
    for data_file in median_data:
        medians.append(np.median(data_file))
    thresh = np.median(medians)
    test_set = Indoor3DSemSeg(args.num_points, file_test, train=False, thresh=thresh)
    test_loader = DataLoader(
        test_set,
        batch_size=args.batch_size,
        shuffle=True,
        pin_memory=True,
        num_workers=2,
    )

    train_set = Indoor3DSemSeg(args.num_points, file_train)
    train_loader = DataLoader(
        train_set,
        batch_size=args.batch_size,
        pin_memory=True,
        num_workers=2,
        shuffle=True,
    )

    model = Pointnet(num_classes=2, input_channels=0, use_xyz=True)
    model.cuda()
    optimizer = optim.Adam(
        model.parameters(), lr=args.lr, weight_decay=args.weight_decay
    )

    lr_lbmd = lambda it: max(
        args.lr_decay ** (int(it * args.batch_size / args.decay_step)),
        lr_clip / args.lr,
    )
    bnm_lmbd = lambda it: max(
        args.bn_momentum
        * args.bn_decay ** (int(it * args.batch_size / args.decay_step)),
        bnm_clip,
    )

    # default value
    it = -1  # for the initialize value of `LambdaLR` and `BNMomentumScheduler`
    best_loss = 1e10
    start_epoch = 1

    # load status from checkpoint
    checkpoint_name = f"checkpoints/pointnet2_{c}"
    best_name = f"checkpoints/pointnet2_{c}_best"
    # checkpoint_name = "checkpoints/" + "pointnet2_semseg.pth.tar"
    # checkpoint_name = "checkpoints/" + "pointnet2_semseg.pth.tar"

    if args.checkpoint:
        checkpoint_status = pt_utils.load_checkpoint(
            model, optimizer, filename=checkpoint_name.split(".")[0]
        )
        if checkpoint_status is not None:
            it, start_epoch, best_loss = checkpoint_status

    lr_scheduler = lr_sched.LambdaLR(optimizer, lr_lambda=lr_lbmd, last_epoch=it)
    bnm_scheduler = pt_utils.BNMomentumScheduler(
        model, bn_lambda=bnm_lmbd, last_epoch=it
    )

    it = max(it, 0)  # for the initialize value of `trainer.train`
    weight = torch.tensor([.58, .42])
    model_fn = model_fn_decorator(nn.CrossEntropyLoss(weight=weight.cuda()))

    if args.visdom:
        viz = pt_utils.VisdomViz(port=args.visdom_port)
    else:
        viz = pt_utils.CmdLineViz()

    viz.text(pprint.pformat(vars(args)))

    evaluator = pt_utils.Eval(
        model,
        model_fn,results_folder=c
    )
    print(len(train_loader), len(test_loader))
    _ = evaluator.eval_epoch(train_loader, "train_guesses")
    _ = evaluator.eval_epoch(test_loader, "test_guesses")


text:
import threading
import logging
import time

from numpy import abs

from DriveComputer.src.drive_control.modules.accessory_ctrl import Accessory_Controller
from DriveComputer.src.drive_control.modules.direction_ctrl import Direction_Controller
from DriveComputer.src.drive_control.modules.drive_ctrl import Drive_Controller

from DriveComputer.src.drive_control.computer_components.can_adapter import CAN_Adapter
from DriveComputer.src.drive_control.computer_components.computer_lcd import LCD
from DriveComputer.src.drive_control.computer_components.mpu import MPU

import DriveComputer.src.drive_control.computer_components.can_adapter as can_util

# Drive Computer Core Library
# Cart Control
#
# Class to control the cart's drive hardware
#
# Part of the GSSM Autonomous Golf Cart
# Written by Joseph Telaak, class of 2022

class MyCart:

    def __init__(self):
        # Setup the message logging
        self.logger = logging.getLogger("hardware_manager")
        file_handler = logging.FileHandler("logs/hardware_manager.log")
        file_handler.setFormatter(logging.Formatter("%(asctime)s - %(threadName)s - %(message)s"))
        self.logger.addHandler(file_handler)

        # Assign the Modules
        self.logger.info("Preparing to Initialize Hardware Manager")
        self.direction_controller = Direction_Controller(can_address = "1")
        self.accessory_controller = Accessory_Controller(can_address = "2")
        self.drive_controller = Drive_Controller(can_address = "3")

        # Internal Hardware
        self.can = CAN_Adapter(serial_port='/dev/ttyUSB0')
        self.lcd = LCD(serial_port='/dev/ttyUSB1')
        self.mpu = MPU(serial_port='/dev/ttyUSB2')

        # Sub-Threads
        self.listener = threading.Thread(target=self.listen, name="message_listener", daemon=True)   # Start Message RX Processing

        self.vars = {
            "accel_pos": 0,
            "accel_enable": 0,
            "direction": 0, # 0 Forwards, 1 Reverse
            "accel_pedal_sw": 0,
            "accel_pedal_pos": 0,
            "right_signal": 0,
            "left_signal": 0,
            "head_light": 0,
            "tail_light": 0,
            "horn": 0,
            "buzzer": 0,
            "steering_motor_en": 0,
            "steering_wheel": 0, # 1 No Change, 2 Left, 3 Right
            "steering_pos": 0,
            "brake_motor_en": 0,
            "brake_pos": 0

        }

        # Init Message
        self.logger.info("Hardware Manager Initialization Preparation Complete")

    def intialize(self):
        # Init Message
        self.logger.info("Initializing Hardware Manager")

        # Starting listener thread
        self.listener.start()

        # Init Message
        self.logger.info("Hardware Manager Initialization Complete")

    # ----------------------------
    # Threads
    # ----------------------------

    # Listen for messages
    def listen(self):
        self.logger.info("CAN Listener Thread Starting")

        # Main loop
        while True:
            message = self.can.read()

            if message != "":
                message_id = can_util.getID()
                message = can_util.removeID(message=message).split(" ")

                if message[0] == "10" and message[1] == "10": # Is a data response message
                    if message_id == "3": # Message from drive controller
                        if message[2] == "10":
                            if message[3] == "10":
                                self.vars["accel_pos"] = can_util.eighttosixteen(int(message[6]), int(message[7]))
                            elif message[3] == "13":
                                self.vars["accel_pedal_pos"] = can_util.eighttosixteen(int(message[6]), int(message[7]))
                            elif message[3] == "15":
                                self.vars["accel_enable"] = can_util.canbool(int(message[7]))
                        elif message[2] == "13":
                            self.vars["direction"] = can_util.canbool(int(message[7]))
                        elif message[2] == "15":
                            self.vars["accel_pedal_sw"] = can_util.canbool(int(message[7]))

                    elif message_id == "2":
                        if message[2] == "10":
                            if message[3] == "1":
                                self.vars["right_signal"] = can_util.canbool(int(message[7]))
                            elif message[3] == "2":
                                self.vars["left_signal"] = can_util.canbool(int(message[7]))
                            elif message[3] == "3":
                                self.vars["head_light"] = can_util.canbool(int(message[7]))
                            elif message[3] == "4":
                                self.vars["tail_light"] = can_util.canbool(int(message[7]))
                            elif message[3] == "5":
                                self.vars["horn"] = can_util.canbool(int(message[7]))
                            elif message[3] == "6":
                                self.vars["buzzer"] = can_util.canbool(int(message[7]))

                    elif message_id == "1":
                        if message[2] == "1":
                            if message[3] == "10":
                                self.vars["steering_motor_en"] = can_util.canbool(int(message[7]))
                            elif message[3] == "15":
                                self.vars["steering_wheel"] = int(message[7])
                            elif message[3] == "16":
                                self.vars["steering_pos"] = can_util.eighttosixteen(int(message[6]), int(message[7]))
                        elif message[2] == "2":
                            if message[3] == "10":
                                self.vars["brake_motor_en"] = can_util.canbool(int(message[7]))
                            elif message[3] == "16":
                                self.vars["brake_pos"] = can_util.eighttosixteen(int(message[6]), int(message[7]))

    # ----------------------------
    # Wheel
    # ----------------------------

    # Turn left
    def turnLeft(self, power = 128):
        if self.vars["steering_motor_en"] != 1:
            self.can.write(self.direction_controller.steering_motor.enable())

        self.can.write(self.direction_controller.steering_motor.left(power))

    # Turn right
    def turnRight(self, power = 128):
        if self.vars["steering_motor_en"] != 1:
            self.can.write(self.direction_controller.steering_motor.enable())

        self.can.write(self.direction_controller.steering_motor.right(power))

    def stopTurn(self):
        self.can.write(self.direction_controller.steering_motor.disable())

    # Run to positon
    def turnToPos(self, position, power = 128):
        if abs(self.vars["steering_pos"] - position) > 500 and not (position < 600 and position > 400):
            if (self.vars["steering_pos"] - position < 0):
                self.leftSignal()
            else:
                self.rightSignal()

        if self.vars["steering_motor_en"] != 1:
            self.can.write(self.direction_controller.steering_motor.enable())

        self.can.write(self.direction_controller.steering_motor.goTo(position, power))

    # ----------------------------
    # Accel
    # ----------------------------

    # Enagage Brakes NOTE: Not recommended, use completestop instead
    def brake(self):
        # Disable the accelerator
        self.setSpeed(0)
        self.can.write(self.drive_controller.digital_accelerator.disable)

        # Brake
        self.can.write(self.direction_controller.brake_motor.pull())
        self.can.write(self.direction_controller.brake_motor.enable())

    # Disengages brakes
    def disengageBrakes(self):
        self.can.write(self.direction_controller.brake_motor.push())
        self.can.write(self.direction_controller.brake_motor.enable())

    # Come to a complete stop
    def completeStop(self):
        if self.vars["brake_pos"] <= 10:
            return
        else:
            self.brake()

            while self.vars["brake_pos"] > 10:
                time.sleep(.1)

            time.sleep(5)

    # Set the accelerator speed
    def setSpeed(self, speed: int):
        if (speed < 0):
                speed = 0
        elif (speed > 255):
                speed = 255

        self.can.write(self.drive_controller.digital_accelerator.setPotPos(speed))

    def enableAccelerator(self):
        self.can.write(self.drive_controller.digital_accelerator.enable())

    def disableAccelerator(self):
        self.can.write(self.drive_controller.digital_accelerator.disable())

    # ----------------------------
    # Direction
    # ----------------------------

    # Set the direction to forwards
    def forwards(self):
        if (self.vars["direction"] == 0):
            return

        # Come to a complete stop for hardware protection
        self.completeStop()

        # Change mode
        self.can.write(self.drive_controller.direction_controller.forwards())

        # Disengage brake pull
        self.disengageBrakes()

    # Set the direction to reverse
    def reverse(self):
        if (self.vars["direction"] == 1):
            return

        # Come to a complete stop for hardware protection
        self.completeStop()

        # Change mode
        self.can.write(self.drive_controller.direction_controller.reverse())

        # Disengage brake pull
        self.disengageBrakes()

    # ----------------------------
    # Turn Signals
    # ----------------------------

    # Blink the right signal
    def rightSignal(self):
        self.can.write(self.accessory_controller.right_signal.blink())

    # Blink the left signal
    def leftSignal(self):
        self.can.write(self.accessory_controller.left_signal.blink())

    # Stop signalling
    def stopSignal(self):
        self.can.write(self.accessory_controller.left_signal.off())
        self.can.write(self.accessory_controller.right_signal.off())

    # Hazards
    def hazards(self):
        self.can.write(self.accessory_controller.tail_light.blink())

    # Stop hazards
    def stopHazards(self):
        self.can.write(self.accessory_controller.tail_light.off())

    # ----------------------------
    # Horn
    # ----------------------------

    def honk(self):
        self.can.write(self.accessory_controller.horn.honk())

max_stars_repo_name: GSSM-AutoGolfCart/Drive-Computer

max_issues_repo_name: GSSM-AutoGolfCart/Drive-Computer

max_forks_repo_name: GSSM-AutoGolfCart/Drive-Computer

reason: from numpy