text:
import argparse
import sys
import os
import data_utils_LA
import numpy as np
from torch import Tensor
from torch.utils.data import DataLoader
from torchvision import transforms
import yaml
import torch
from torch import nn
from model import RawNet
from tensorboardX import SummaryWriter

def keras_lr_decay(step, decay = 0.0001):
	return 1./(1.+decay*step)

def pad(x, max_len=64600):

    x_len = x.shape[0]
    if x_len >= max_len:
        return x[:max_len]
    # need to pad
    num_repeats = int(max_len / x_len)+1
    padded_x = np.tile(x, (1, num_repeats))[:, :max_len][0]

    return padded_x

def init_weights(m):
    #print(m)
    if isinstance(m, nn.Linear):
        torch.nn.init.xavier_uniform(m.weight)
        m.bias.data.fill_(0.0001)
    elif isinstance(m, nn.BatchNorm1d):
        pass
    else:
        if hasattr(m, 'weight'):
            torch.nn.init.kaiming_normal_(m.weight, a=0.01)
        else:
            print('no weight',m)

def evaluate_accuracy(data_loader, model, device):
    num_correct = 0.0
    num_total = 0.0
    model.eval()
    for batch_x, batch_y, batch_meta in data_loader:

        batch_size = batch_x.size(0)
        num_total += batch_size
        batch_x = batch_x.to(device)
        batch_y = batch_y.view(-1).type(torch.int64).to(device)
        batch_out = model(batch_x,batch_y)
        _, batch_pred = batch_out.max(dim=1)
        num_correct += (batch_pred == batch_y).sum(dim=0).item()
    return 100 * (num_correct / num_total)

def produce_evaluation_file(dataset, model, device, save_path):
    data_loader = DataLoader(dataset, batch_size=8, shuffle=False)
    num_correct = 0.0
    num_total = 0.0
    model.eval()
    true_y = []
    fname_list = []
    key_list = []
    sys_id_list = []
    key_list = []
    score_list = []
    for batch_x, batch_y, batch_meta in data_loader:
        batch_size = batch_x.size(0)
        num_total += batch_size
        batch_x = batch_x.to(device)
        batch_y = batch_y.view(-1).type(torch.int64).to(device)
        batch_out = model(batch_x,batch_y,is_test=True)
        batch_score = (batch_out[:, 1]
                       ).data.cpu().numpy().ravel()

        # add outputs
        fname_list.extend(list(batch_meta[1]))
        key_list.extend(
          ['bonafide' if key == 1 else 'spoof' for key in list(batch_meta[4])])
        sys_id_list.extend([dataset.sysid_dict_inv[s.item()]
                            for s in list(batch_meta[3])])
        score_list.extend(batch_score.tolist())

    with open(save_path, 'w') as fh:
        for f, s, k, cm in zip(fname_list, sys_id_list, key_list, score_list):
            if dataset.is_eval:
                fh.write('{} {} {} {}\n'.format(f, s, k, cm))
            else:
                fh.write('{} {}\n'.format(f, cm))
    print('Result saved to {}'.format(save_path))

def train_epoch(data_loader, model, lr,optim, device):
    running_loss = 0
    num_correct = 0.0
    num_total = 0.0
    ii = 0
    model.train()
    weight = torch.FloatTensor([1.0, 9.0]).to(device)
    criterion = nn.CrossEntropyLoss(weight=weight)

    for batch_x, batch_y, batch_meta in train_loader:

        batch_size = batch_x.size(0)
        num_total += batch_size
        ii += 1
        batch_x = batch_x.to(device)
        batch_y = batch_y.view(-1).type(torch.int64).to(device)
        batch_out = model(batch_x,batch_y)
        batch_loss = criterion(batch_out, batch_y)
        _, batch_pred = batch_out.max(dim=1)
        num_correct += (batch_pred == batch_y).sum(dim=0).item()
        running_loss += (batch_loss.item() * batch_size)
        if ii % 10 == 0:
            sys.stdout.write('\r \t {:.2f}'.format(
                (num_correct/num_total)*100))
        optim.zero_grad()
        batch_loss.backward()
        optim.step()

    running_loss /= num_total
    train_accuracy = (num_correct/num_total)*100
    return running_loss, train_accuracy

if __name__ == '__main__':
    parser = argparse.ArgumentParser('ASVSpoof2019  model')
    parser.add_argument('--eval', action='store_true', default=False,
                        help='eval mode')
    parser.add_argument('--model_path', type=str,
                        default=None, help='Model checkpoint')

    parser.add_argument('--database_path', type=str, default='/your/path/to/data/ASVspoof_database/', help='change this to user\'s full directory address of LA database')
    parser.add_argument('--protocols_path', type=str, default='database/ASVspoof2019_LA_cm_protocols/', help='Change with path to user\'s LA database protocols directory address')
    parser.add_argument('--eval_output', type=str, default=None,
                        help='Path to save the evaluation result')
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--num_epochs', type=int, default=100)
    parser.add_argument('--lr', type=float, default=0.0001)
    parser.add_argument('--weight_decay', type=float, default=0.0001)
    parser.add_argument('--comment', type=str, default=None,
                        help='Comment to describe the saved mdoel')
    parser.add_argument('--track', type=str, default='logical')
    parser.add_argument('--features', type=str, default='Raw_audio')
    parser.add_argument('--is_eval', action='store_true', default=False)
    parser.add_argument('--eval_part', type=int, default=0)
    parser.add_argument('--loss', type=str, default='weighted_CCE')

    dir_yaml = os.path.splitext('model_config_RawNet2')[0] + '.yaml'

    with open(dir_yaml, 'r') as f_yaml:
            parser1 = yaml.load(f_yaml)

    np.random.seed(parser1['seed'])
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    if not os.path.exists('models'):
        os.mkdir('models')
    args = parser.parse_args()

    # LA and PA
    track = args.track

    #Creat Model
    model_tag = 'model_{}_{}_{}_{}_{}'.format(
        track, args.loss, args.num_epochs, args.batch_size, args.lr)
    if args.comment:
        model_tag = model_tag + '_{}'.format(args.comment)
    model_save_path = os.path.join('models', model_tag)

    is_logical = (track == 'logical')
    if not os.path.exists(model_save_path):
        os.mkdir(model_save_path)

    transforms = transforms.Compose([

        lambda x: pad(x),
        lambda x: Tensor(x)

    ])

    # GPU device
    device = 'cuda' if torch.cuda.is_available() else 'cpu'                  # cuda-0

    # Dataloader
    dev_set = data_utils_LA.ASVDataset(database_path=args.database_path,protocols_path=args.protocols_path,is_train=False, is_logical=is_logical,
                                    transform=transforms,
                                    feature_name=args.features, is_eval=args.is_eval, eval_part=args.eval_part)
    dev_loader = DataLoader(dev_set, batch_size=args.batch_size, shuffle=True)

    #torch.backends.cudnn.enabled = False

    # Model Initialization
    if bool(parser1['mg']):
            model_1gpu = RawNet(parser1['model'], device)
            nb_params = sum([param.view(-1).size()[0] for param in model_1gpu.parameters()])
            model =(model_1gpu).to(device)
    else:
        model = RawNet(parser1['model'], device).to(device)
        nb_params = sum([param.view(-1).size()[0] for param in model.parameters()])

    # Adam optimizer
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr,weight_decay=args.weight_decay)

    if args.model_path:
        model.load_state_dict(torch.load(args.model_path,map_location=device))
        print('Model loaded : {}'.format(args.model_path))

    if args.eval:

        produce_evaluation_file(dev_set, model, device, args.eval_output)
        sys.exit(0)

    # Dataloader
    train_set = data_utils_LA.ASVDataset(database_path=args.database_path,protocols_path=args.protocols_path,is_train=True, is_logical=is_logical, transform=transforms,
                                      feature_name=args.features)
    train_loader = DataLoader(
        train_set, batch_size=args.batch_size, shuffle=True)

    # Training and validation
    num_epochs = args.num_epochs
    writer = SummaryWriter('logs/{}'.format(model_tag))
    best_acc = 99
    for epoch in range(num_epochs):
        running_loss, train_accuracy = train_epoch(train_loader,model, args.lr,optimizer, device)
        valid_accuracy = evaluate_accuracy(dev_loader, model, device)
        writer.add_scalar('train_accuracy', train_accuracy, epoch)
        writer.add_scalar('valid_accuracy', valid_accuracy, epoch)
        writer.add_scalar('loss', running_loss, epoch)
        print('\n{} - {} - {:.2f} - {:.2f}'.format(epoch,
                                                   running_loss, train_accuracy, valid_accuracy))
        print('*'*50)
        print('dev_acc %f', valid_accuracy)
        if valid_accuracy > best_acc:
            print('best model found at epoch', epoch)
        print('*'*50)
        best_acc = max(valid_accuracy, best_acc)
        torch.save(model.state_dict(), os.path.join(model_save_path, 'epoch_{}.pth'.format(epoch)))


text:
# Made by Fabian 13.12.2019
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random
import math
import statistics
import time
from tqdm import trange
from tqdm import tqdm
from os import system as s

data = pd.read_csv("hotels.csv")

if len(data['quadratmeter']) != len(data['preis']):
    print('Ups es ist was schief gelaufen daten stimmen nicht überein!!')
    exit()

print('Hier sind die Daten veranschaulicht')
print('Einfach das Bild schliessen um weiter zu machen')
plt.scatter(data["preis"], data["quadratmeter"], color='red')
plt.show()

print('Jetzt werden die Anfangspunkte ausgerechnet...')
time.sleep(1)
# y = m * x + b     m, b herausfinden (model.fit)
# Init the function:
# find the nearest x, y coordinates to 0 and the fartest

nearest = None
fartest = None
d_max = None
d_min = None

for i in range(len(data['quadratmeter'])):
    x = data['preis'][i]
    y = data['quadratmeter'][i]
    # distance to 0 with pythagoras:
    # a^2 + b^2 = c^2  ==>  d^2 = x^2 + y^2
    d = math.sqrt(x**2+y**2)
    if nearest == None:
        nearest = (x, y)
        fartest = (x, y)
        d_min = d
        d_max = d
    elif d_min > d:
        nearest = (x, y)
        d_min = d
    elif d_max < d:
        fartest = (x, y)
        d_max = d
    else:
        pass # in between

x1 = nearest[0]
y1 = nearest[1]
x2 = fartest[0]
y2 = fartest[1]

print('Nähester Punkt:', nearest)
print('Entferntester Punkt:', fartest)
time.sleep(.5)
print('Hier sind die nähesten/ entferntesten Punkte markiert.')
print('Einfach das Bild schliessen um weiter zu machen')
plt.scatter(data["preis"], data["quadratmeter"], color='red')
plt.scatter(x1, y1, color='green')
plt.scatter(x2, y2, color='green')
plt.show()

print('Jetzt werden m und b für die Anfangslinie ausgerechnet...')
time.sleep(1)

# find out what m and b is
m = (y2-y1) / (x2-x1)
# y = mx + b umformen und für x, y die nähesten zwei punkte zu nullstelle einsetzen
b = -(m * x1 - y1)

print('Die Anfangsgerade lautet: y = {}x + {}'.format(m, b))

print('Hier ist die Gerade zu denn Daten gezeichnet.')
print('Einfach das Bild schliessen um weiter zu machen')
plt.scatter(data["preis"], data["quadratmeter"], color='r')
plt.scatter(x1, y1, color='green')
plt.scatter(x2, y2, color='green')
plt.plot(data['preis'], data['preis']*m + b, color='b', label='InitLine')
plt.show()

# try to minimize the error
# numpy linearRegression: slope, intercept = np.polyfit(X, Y, 1)

xmean = statistics.mean(data['preis'])
ymean = statistics.mean(data['quadratmeter'])

m1 = 0
m2 = 0

print('Jetzt wird die gerade angepasst...')

for i, x in enumerate(tqdm(list(data['preis']))):
    m1 += (x - xmean) * (data['quadratmeter'][i] - ymean)
    m2 += (x - xmean)**2

    m = m1 / m2

    b = ymean - m * xmean

    plt.scatter(data["preis"], data["quadratmeter"], color='r')
    plt.plot(data['preis'], data['preis']*m + b, color='b')
    plt.show(block=False)
    plt.pause(.05)
    plt.cla()

print('Die perfekte Linie wurde ausgerechenet!')
print('Die perfekte Linie lautet: f(x) = {}x + {}'.format(m, b))
print('Oder auch y = {}x + {}'.format(m, b))
print('Die Finale Gerade wurde gefunden!')
print('Einfach das Bild schliessen um weiter zu machen')
plt.scatter(data["preis"], data["quadratmeter"], color='r')
plt.plot(data['preis'], data['preis']*m + b, color='b', label='FittetLine')
plt.show()

'''
Init the function:
m = (y2-y1) / (x2-x1)
y - y1 = m * (x - x1)  # m * x1 = b
minimize loss:
m = (sum((x-xmean)*(y-ymean)))/sum(x-xmean)
b = ymean - m * xmean

mean = durschnitt von datenpunkten
'''
# Predict the a value:
# f(x) = mx + b => einsetzen
# print(m*20 + b)

print('Jetzt kann man das Program nutzen um vorraussagen zu treffen...')
while True:
    try:
        frage = int(input('Geben sie eine Grösse eines Hotels ein um denn vorrausgesagten Preis zu bekommen: '))
        print('Die KI hat denn preis = {} Euro vorrausgesagt.'.format(m * frage + b))
    except:
        print('Bitte nur Zahlen eingeben, ohne "m" oder änliches!', e)

ext: py

reason: import numpy


text:
from Model.Pathfinder import PathFinder
import time
import numpy as np
import tkinter
import ast
from PIL import Image, ImageTk

class tool:
    def __init__(self):
        self.tk = tkinter.Tk()
        self.tk.geometry('{}x{}'.format(500, 500))
        self.tk.title('Pathfinder tool')
        self.frame = tkinter.Frame(self.tk, width=500, height=500)
        self.frame.place(x=0, y=0, anchor='nw')

        self.start_lbl = tkinter.Label(self.frame, text='Start')
        self.start_lbl.place(relx=0.20, rely=0.015, anchor='center')
        self.start_coords_lbl = tkinter.Label(self.frame, text='Coords')
        self.start_coords_lbl.place(relx=0.10, rely=0.05, anchor='center')
        self.start_cell_lbl = tkinter.Label(self.frame, text='Cell')
        self.start_cell_lbl.place(relx=0.30, rely=0.05, anchor='center')
        self.start_coords_entry = tkinter.Entry(width=10)
        self.start_coords_entry.place(relx=0.1, rely=0.1, anchor='center')
        self.start_cell_entry = tkinter.Entry(width=3)
        self.start_cell_entry.place(relx=0.30, rely=0.1, anchor='center')

        self.end_lbl = tkinter.Label(self.frame, text='End')
        self.end_lbl.place(relx=0.80, rely=0.015, anchor='center')
        self.end_coords_lbl = tkinter.Label(self.frame, text='Coords')
        self.end_coords_lbl.place(relx=0.7, rely=0.05, anchor='center')
        self.end_cell_lbl = tkinter.Label(self.frame, text='Cell')
        self.end_cell_lbl.place(relx=0.9, rely=0.05, anchor='center')
        self.end_coords_entry = tkinter.Entry(width=10)
        self.end_coords_entry.place(relx=0.7, rely=0.1, anchor='center')
        self.end_cell_entry = tkinter.Entry(width=3)
        self.end_cell_entry.place(relx=0.9, rely=0.1, anchor='center')

        self.path_button = tkinter.Button(command=self.generate_path, text='Get path')
        self.path_button.place(relx=0.33, rely=0.15, anchor='center')

        self.maps_button = tkinter.Button(command=self.generate_map_collage, text='Get maps')
        self.maps_button.place(relx=0.66, rely=0.15, anchor='center')

        self.tk.mainloop()

    def eval_entry(self):
        start_coords = ast.literal_eval(self.start_coords_entry.get())
        start_cell = self.start_cell_entry.get()
        start_cell = int(start_cell) if start_cell else None
        end_coords = ast.literal_eval(self.end_coords_entry.get())
        end_cell = self.end_cell_entry.get()
        end_cell = int(end_cell) if end_cell else None
        return start_coords, start_cell, end_coords, end_cell

    def generate_path(self):
        start_coords, start_cell, end_coords, end_cell = self.eval_entry()
        pf = PathFinder(start_coords, end_coords, start_cell, end_cell, worldmap=1)

        pf.get_path()
        pf.add_path_to_adapted_maps()
        pf.add_map_change_coords_to_adapted_maps()
        pf.map_to_image(pf.adapted_maps, 1)
        lel = np.divide(np.array(Image.open('Out.png')), 255)
        image = Image.fromarray(lel)
        image = image.resize((350, 350))
        photo = ImageTk.PhotoImage(image)
        image_lbl = tkinter.Label(self.tk, image=photo)
        image_lbl.image = photo
        image_lbl.place(relx=0.5, rely=0.6, anchor='center')
        map_change_cells = pf.get_map_change_cells()
        print(pf.path_cells)
        print(map_change_cells)
        return pf.path_cells, map_change_cells

    def generate_map_collage(self):
        start_coords, start_cell, end_coords, end_cell = self.eval_entry()
        pf = PathFinder(start_coords, end_coords, start_cell, end_cell, worldmap=1)

        maps_coords = pf.get_maps_coords()
        maps = []
        shape = (abs(end_coords[1] - start_coords[1]) + 1, abs(end_coords[0] - start_coords[0]) + 1)
        counter = 0
        for coord in maps_coords:
            map_infos = pf.llf.coord_fetch_map(coord, pf.worldmap)
            counter += 1
            print('{}/{}'.format(counter, shape[0] * shape[1]))
            if map_infos is not None and np.array(map_infos).shape == (40, 14):
                maps.append(map_infos)
            elif map_infos is not None and np.array(map_infos).shape != (40, 14):
                maps.append([[5] * 14] * 40)
            else:
                maps.append([[1] * 14] * 40)
        glued = pf.glue_maps(maps, shape)
        # print(glued)
        # print(pf.adapted_maps)
        pf.map_to_image(pf.adapt_shape_maps(glued), 1)
        lel = np.divide(np.array(Image.open('Out.png')), 255)
        image = Image.fromarray(lel)
        image = image.resize((350, 350))
        photo = ImageTk.PhotoImage(image)
        image_lbl = tkinter.Label(self.tk, image=photo)
        image_lbl.image = photo
        image_lbl.place(relx=0.5, rely=0.6, anchor='center')

tool()
"(7, -6), cell 546 to map [6, -6], cell 391, worldmap : 1"
"(9, 25) cell 321 to [8, 25] cell 335"

max_stars_repo_name: ProjectBlackFalcon/DatBot

max_issues_repo_name: ProjectBlackFalcon/DatBot

max_forks_repo_name: ProjectBlackFalcon/DatBot


text:
import numpy as np
import tensorflow as tf

def main(argv=None):
  anotherstuff()
  input_tensor = tf.constant("1.png", dtype=tf.string)
  keys = tf.constant(np.array(["1.png", "2.png","3.png"]), dtype=tf.string)
  #values = tf.constant(np.array([4, 1], [5, 1], [6, 1]), dtype=tf.int64)

  value1 = tf.constant([1,2], dtype=tf.int64)
  #values = tf.constant(np.array([value1,value1,value1]), dtype=tf.int64) -> doens't work (work just with (np.array[1,2,3])..
  #we need a list.. ,and adds elementt ot the list:
  a = np.array([0])
  b = np.array([1])
  c = np.array([1])
  #c2 = np.array([2, 3, 4, 5, 6])
  d = np.concatenate((a,b,c,np.array([2, 3, 4, 5, 6])))
  e = np.concatenate((d,[2]))

  #values = np.array([0,1,2])
  values = np.array(["provaaa","try","try2"])
  #aggiungere i valori ok, ora.. ci serve.. che..
  #values = np.array([[2,3,4],[5,6,7],[7,8,9]])

  #https: // stackoverflow.com / questions / 50315932 / tensorflow - hashtable - lookup -with-arrays
  #purtroppo lavora solo con degli scalari.. quindi??!

  #table = tf.contrib.lookup.HashTable(
  #  tf.contrib.lookup.KeyValueTensorInitializer(keys, values),
  #  -999)
#  #[{'label': '3', 'top': '7', 'left': '52', 'width': '21', 'height': '46'}, {'label': '1', 'top': '10', 'left': '74', 'width': '15', 'height': '46'}]
#Quindi potremmo mettere queste informazioni in stringa.. e poi trattare le stringhe splittando!..

  table = tf.contrib.lookup.HashTable(
   tf.contrib.lookup.KeyValueTensorInitializer(keys, values),
    "default")

  out = table.lookup(input_tensor)

  with tf.Session() as sess:
    table.init.run()
    print(out.eval())

def anotherstuff():
  #what does it do?
  lookup = tf.placeholder(shape=(2,), dtype=tf.int64)
  default_value = tf.constant([1, 1], dtype=tf.int64)
  input_tensor = tf.constant([1, 1], dtype=tf.int64)
  keys = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.int64)
  values = tf.constant([[4, 1], [5, 1], [6, 1]], dtype=tf.int64)
  val0 = values[:, 0] #(4,5,6)
  val1 = values[:, 1] #(1,1,1]

  st0 = tf.SparseTensor(keys, val0, dense_shape=(7, 7)) #print(sess.run(st0))
  '''SparseTensorValue(indices=array([[1, 2],
                                   [3, 4],
                                   [5, 6]], dtype=int64), values=array([4, 5, 6], dtype=int64),
                    dense_shape=array([7, 7], dtype=int64))'''

  st1 = tf.SparseTensor(keys, val1, dense_shape=(7, 7))
  '''SparseTensorValue(indices=array([[1, 2],
                                   [3, 4],
                                   [5, 6]], dtype=int64), values=array([1, 1, 1], dtype=int64),
                    dense_shape=array([7, 7], dtype=int64))'''

  x0 = tf.sparse_slice(st0, lookup, [1, 1])
  y0 = tf.reshape(tf.sparse_tensor_to_dense(x0, default_value=default_value[0]), ())
  x1 = tf.sparse_slice(st1, lookup, [1, 1])
  y1 = tf.reshape(tf.sparse_tensor_to_dense(x1, default_value=default_value[1]), ())
  #NOTA: la separazione delle immagine da fare con tool open cv (preparare in un altra data set).

  y = tf.stack([y0, y1], axis=0)
  #mi sembra di capire che ha messo in stack gli indici con il valore del primo, più gli indici con il secondo valore.. hmm
  #y0-> è st0 con il lookup  più il valore di default, così anche y1, se unisci... hmm

  with tf.Session() as sess:
    #st0.eval()?..
    #print(sess.run(st0))
    #print(sess.run(st1))

    print(sess.run(y, feed_dict={lookup: [1, 2]}))
    print(sess.run(y, feed_dict={lookup: [1, 1]}))
    print(sess.run(y, feed_dict={lookup: [3, 4]}))
    print(sess.run(y, feed_dict={lookup: [5, 6]}))
  #questo funziona... ma comunque non vogliamo il feeddict, penso proprio che non sia un problema...
  #vogliamo che il grafo, la computazione sia a stabilre il lookup, non un placeholder..
  #da capire  questo pezzo di codice..

if __name__ == '__main__':
  tf.app.run() #main()


text:
"""
=============================
Noise estimation using PIESNO
=============================

Often, one is interested in estimating the noise in the diffusion signal. One
of the methods to do this is the Probabilistic Identification and Estimation of
Noise (PIESNO) framework [Koay2009]_. Using this method, one can detect the
standard deviation of the noise from diffusion-weighted imaging (DWI). PIESNO
also works with multiple channel DWI datasets that are acquired from N array
coils for both SENSE and GRAPPA reconstructions.

The PIESNO method works in two steps:

1) First, it finds voxels that are most likely background voxels. Intuitively,
these voxels have very similar diffusion-weighted intensities (up to some noise)
in the fourth dimension of the DWI dataset. White matter, gray matter or CSF
voxels have diffusion intensities that vary quite a lot across different
directions.

2) From these estimated background voxels and the input number of coils N,
PIESNO finds what sigma each Gaussian from each of the N coils would have
generated the observed Rician (N=1) or non-central Chi (N>1) distributed noise
profile in the DWI datasets.

PIESNO makes an important assumption: the Gaussian noise standard deviation is
assumed to be uniform. The noise is uniform across multiple slice locations or
across multiple images of the same location.

For the full details, please refer to the original paper.

In this example, we will demonstrate the use of PIESNO with a 3-shell data-set.

We start by importing necessary modules and functions and loading the data:
"""

import nibabel as nib
import numpy as np
from dipy.denoise.noise_estimate import piesno
from dipy.data import fetch_sherbrooke_3shell, read_sherbrooke_3shell

fetch_sherbrooke_3shell()
img, gtab = read_sherbrooke_3shell()
data = img.get_data()

"""
Now that we have fetched a dataset, we must call PIESNO with the right number
of coils used to acquire this dataset. It is also important to know what
was the parallel reconstruction algorithm used. Here, the data comes from a
GRAPPA reconstruction, was acquired with a 12-elements head coil available on
the Tim Trio Siemens, for which the 12 coil elements are combined into 4 groups
of 3 coil elements each. The signal is therefore received through 4 distinct
groups of receiver channels, yielding N = 4. Had we used a GE acquisition, we
would have used N=1 even if multiple channel coils are used because GE uses a
SENSE reconstruction, which has a Rician noise nature and thus N is always 1.
"""

sigma, mask = piesno(data, N=4, return_mask=True)

axial = data[:, :, data.shape[2] // 2, 0].T
axial_piesno = mask[:, :, data.shape[2] // 2].T

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
ax[0].imshow(axial, cmap='gray', origin='lower')
ax[0].set_title('Axial slice of the b=0 data')
ax[1].imshow(axial_piesno, cmap='gray', origin='lower')
ax[1].set_title('Background voxels from the data')
for a in ax:
    a.set_axis_off()

plt.savefig('piesno.png', bbox_inches='tight')

"""
.. figure:: piesno.png
   :align: center

   **Showing the mid axial slice of the b=0 image (left) and estimated
   background voxels (right) used to estimate the noise standard deviation**.
"""

nib.save(nib.Nifti1Image(mask, img.affine, img.header),
         'mask_piesno.nii.gz')

print('The noise standard deviation is sigma= ', sigma)
print('The std of the background is =', np.std(data[mask[...,None].astype(np.bool)]))

"""

Here, we obtained a noise standard deviation of 7.26. For comparison, a simple
standard deviation of all voxels in the estimated mask (as done in the previous
example :ref:`example_snr_in_cc`) gives a value of 6.1.

"""

"""

.. [Koay2009] Koay C.G., E. Ozarslan, C. Pierpaoli. Probabilistic
              Identification and Estimation of Noise (PIESNO): A
              self-consistent approach and its applications in MRI.
              JMR, 199(1):94-103, 2009.

.. include:: ../links_names.inc

"""

max_stars_repo_name: nasimanousheh/dipy


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

import torch

from torch import nn
from torch.nn import functional as F
from torchvision.models import resnet
from torch.nn.modules.batchnorm import _BatchNorm

from det3d.torchie.cnn import constant_init, kaiming_init, xavier_init
from det3d.torchie.trainer import load_checkpoint
from det3d.models.utils import Empty, GroupNorm, Sequential
from det3d.models.utils import change_default_args

from .. import builder
from ..registry import NECKS
from ..utils import build_norm_layer

# Spatial-Semantic Feature Aggregation (SSFA) Module
@NECKS.register_module
class SSFA(nn.Module):
    def __init__(self, layer_nums, ds_layer_strides, ds_num_filters, us_layer_strides, us_num_filters,
                 num_input_features, norm_cfg=None, name="rpn", logger=None, **kwargs):
        super(SSFA, self).__init__()
        self._layer_strides = ds_layer_strides  # [1,]
        self._num_filters = ds_num_filters      # [128,]
        self._layer_nums = layer_nums           # [5,]
        self._upsample_strides = us_layer_strides      # [1,]
        self._num_upsample_filters = us_num_filters    # [128,]
        self._num_input_features = num_input_features  # 128

        if norm_cfg is None:  # True
            norm_cfg = dict(type="BN", eps=1e-3, momentum=0.01)
        self._norm_cfg = norm_cfg

        self.bottom_up_block_0 = Sequential(
            nn.ZeroPad2d(1),
            nn.Conv2d(128, 128, 3, stride=1, bias=False),
            build_norm_layer(self._norm_cfg, 128)[1],
            nn.ReLU(),

            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),

            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.bottom_up_block_1 = Sequential(
            # [200, 176] -> [100, 88]
            nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=2, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 256, )[1],
            nn.ReLU(),

            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 256, )[1],
            nn.ReLU(),

            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 256, )[1],
            nn.ReLU(),

        )

        self.trans_0 = Sequential(
            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=1, stride=1, padding=0, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.trans_1 = Sequential(
            nn.Conv2d(in_channels=256, out_channels=256, kernel_size=1, stride=1, padding=0, bias=False, ),
            build_norm_layer(self._norm_cfg, 256, )[1],
            nn.ReLU(),
        )

        self.deconv_block_0 = Sequential(
            nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=3, stride=2, padding=1, output_padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.deconv_block_1 = Sequential(
            nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=3, stride=2, padding=1, output_padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.conv_0 = Sequential(
            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.w_0 = Sequential(
            nn.Conv2d(in_channels=128, out_channels=1, kernel_size=1, stride=1, padding=0, bias=False, ),
            build_norm_layer(self._norm_cfg, 1, )[1],
        )

        self.conv_1 = Sequential(
            nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1, bias=False, ),
            build_norm_layer(self._norm_cfg, 128, )[1],
            nn.ReLU(),
        )

        self.w_1 = Sequential(
            nn.Conv2d(in_channels=128, out_channels=1, kernel_size=1, stride=1, padding=0, bias=False, ),
            build_norm_layer(self._norm_cfg, 1, )[1],
        )

        logger.info("Finish RPN Initialization")

    # default init_weights for conv(msra) and norm in ConvModule
    def init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                xavier_init(m, distribution="uniform")

    def forward(self, x):
        x_0 = self.bottom_up_block_0(x)
        x_1 = self.bottom_up_block_1(x_0)
        x_trans_0 = self.trans_0(x_0)
        x_trans_1 = self.trans_1(x_1)
        x_middle_0 = self.deconv_block_0(x_trans_1) + x_trans_0
        x_middle_1 = self.deconv_block_1(x_trans_1)
        x_output_0 = self.conv_0(x_middle_0)
        x_output_1 = self.conv_1(x_middle_1)

        x_weight_0 = self.w_0(x_output_0)
        x_weight_1 = self.w_1(x_output_1)
        x_weight = torch.softmax(torch.cat([x_weight_0, x_weight_1], dim=1), dim=1)
        x_output = x_output_0 * x_weight[:, 0:1, :, :] + x_output_1 * x_weight[:, 1:, :, :]

        return x_output.contiguous()

max_stars_repo_name: reinforcementdriving/CIA-SSD

max_issues_repo_name: reinforcementdriving/CIA-SSD

max_forks_repo_name: reinforcementdriving/CIA-SSD


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) IBM Corporation 2018
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from datetime import datetime
import numpy as np
import logging

import matplotlib

from matplotlib.backends.qt_compat import QtWidgets

if matplotlib.backends.qt_compat.is_pyqt5():
    from matplotlib.backends.backend_qt5agg import (
        FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
    from matplotlib.backends.backend_qt4agg import (
        FigureCanvas, NavigationToolbar2QT as NavigationToolbar)

class TimePlot(QtWidgets.QMainWindow):
    """
    DOCUMENTATION!

    """
    def __init__(self):
        self.qapp = QtWidgets.QApplication(sys.argv)
        super(TimePlot, self).__init__()
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main)
        self.layout = QtWidgets.QGridLayout(self._main)

        # Empty objects that will be used during visualization.
        self.fig = None
        self.frames = None

        # Slider stuff
        hbox_timeline = QtWidgets.QHBoxLayout()
        self.slider = QtWidgets.QSlider(matplotlib.backends.qt_compat.QtCore.Qt.Horizontal)
        self.slider.setMinimum(0)
        self.slider.valueChanged.connect(self.slider_valuechanged)
        hbox_timeline.addWidget(self.slider)
        self.layout.addLayout(hbox_timeline, 1, 0)

        # Bottom buttons
        hbox_buttons = QtWidgets.QHBoxLayout()
        hbox_buttons.addStretch(1)
        # Button for saving movies.
        save_movie_btn = QtWidgets.QPushButton(
            "Save as &movie")  # Shortcut is Alt+M
        save_movie_btn.clicked.connect(self._save_movie_clicked)
        # Button for next episode.
        next_btn = QtWidgets.QPushButton("&Proceed")  # Shortcut is Alt+P
        next_btn.clicked.connect(self._next_clicked)
        # Quit button.
        stop_btn = QtWidgets.QPushButton("&Stop experiment")  # Shortcut is Alt+S
        stop_btn.clicked.connect(self.closeEvent)
        # Add buttons to widget.
        hbox_buttons.addWidget(save_movie_btn)
        hbox_buttons.addWidget(next_btn)
        hbox_buttons.addWidget(stop_btn)
        self.layout.addLayout(hbox_buttons, 2, 0)
        self.terminate = False

    def _save_movie_clicked(self):
        logger = logging.getLogger('Model')
        logger.info("Saving movie - please wait...")
        # Generate filename.
        time_str = '{0:%Y%m%d_%H%M%S}'.format(datetime.now())
        name_str = 'experiment_run_' + time_str + '.mp4'

        # Save the animation.
        ani = matplotlib.animation.ArtistAnimation(
            self.fig, self.frames, blit=False, interval=1.0)
        Writer = matplotlib.animation.writers['ffmpeg']
        # writer = Writer(fps=1, extra_args=['-r', '25', '-probesize', '10M'])
        # ani.save(name_str, writer=writer, dpi=200)
        writer = Writer(fps=1, bitrate=5000)
        ani.save(name_str, writer=writer)
        logger.info("Saved movie to file '{}'".format(name_str))

    def _next_clicked(self):
        self.qapp.quit()

    def closeEvent(self, _):
        # Set flag to True, so we could break the external loop.
        self.terminate = True
        self.qapp.quit()

    def update(self, fig, frames):
        # "Save" figure and frame objects.
        self.fig = fig
        self.frames = frames

        # Create the widget.
        w = QtWidgets.QWidget()
        layout = QtWidgets.QVBoxLayout(w)
        # Cast to figure canvas.
        fc = FigureCanvas(fig)
        # Add "navigation" (save) on top.
        layout.addWidget(NavigationToolbar(fc, self))
        layout.addWidget(fc)
        w.setLayout(layout)
        # Add widget at position (0,0)!
        self.layout.addWidget(w, 0, 0)
        # Reset current frame.
        self.current_frame_number = 0

        # Set slider properties...
        self.slider.setMaximum(len(frames) - 1)
        # ... And set slider to end, which will cause showing last frame.
        self.slider.setValue(len(frames) - 1)
        # But call it anyway - for the case when user pressed "next" button.
        self.show_frame(len(frames) - 1)

        # Show.
        self.show()
        self.qapp.exec_()
        # Resume event loop - throw SystemExit if one pressed "stop".
        if self.terminate:
            raise SystemExit("the user pressed Stop!")

    def slider_valuechanged(self):
        """
        Event handler attached to the slider.
        """
        val = self.slider.value()
        self.show_frame(val)

    def show_frame(self, frame_number):
        """
        Shows given frame.
        """
        # Make all the artists from the current frame visible
        for frame in self.frames:
            for artist in frame:  # self.frames[self.current_frame_number]:
                artist.set_visible(False)

        # Make all the artists from the current frame visible
        for artist in self.frames[frame_number]:
            artist.set_visible(True)

        # Redraw the figure canvas.
        self.fig.canvas.draw_idle()

        # Remember current frame.
        self.current_frame_number = frame_number

if __name__ == '__main__':
    # Set logging level.
    logging.basicConfig(level=logging.DEBUG)

    # Test code
    plot = TimePlot()

    # Create a single "figure canvas".
    fig = matplotlib.pyplot.figure()
    axes = fig.subplots(1, 3, sharey=True)
    axes[0].set_title("input")
    axes[1].set_title("output")
    axes[2].set_title("target")

    def fun(x, y):
        return np.sin(x) + np.cos(y)

    try:
        tmp = 0
        while True:
            print("Inside of while")
            x = np.linspace(tmp, 2 * np.pi, 120)
            y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
            # frames is a list of lists, each row is a list of artists to draw in a
            # given frame.
            frames = []
            for i in range(60):
                x += np.pi / 15.
                y += np.pi / 20.
                # Get axes
                artists = [None] * len(fig.axes)
                artists[0] = axes[0].imshow(fun(x, y))
                artists[1] = axes[1].imshow(fun(x, y))
                artists[2] = axes[2].imshow(fun(x, y))
                # Make them invisible.
                for artist in artists:
                    artist.set_visible(False)
                # Add "frame".
                frames.append(artists)
            # Plot.
            plot.update(fig, frames)
            tmp = tmp + 1
    except SystemExit as e:
        print("Visualization stopped")

max_stars_repo_name: vincentalbouy/mi-prometheus

max_issues_repo_name: vincentalbouy/mi-prometheus

max_forks_repo_name: vincentalbouy/mi-prometheus


text:
#From https://gist.github.com/EndingCredits/b5f35e84df10d46cfa716178d9c862a3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.framework import ops
from tensorflow.python.training import optimizer
import tensorflow as tf
import hyperchamber as hc
import numpy as np
import inspect
from operator import itemgetter
from hypergan.train_hooks.base_train_hook import BaseTrainHook

class AdversarialRobustTrainHook(BaseTrainHook):
  "AR from https://openreview.net/pdf?id=HJE6X305Fm"
  def __init__(self, gan=None, config=None, trainer=None, name="GradientPenaltyTrainHook"):
    super().__init__(config=config, gan=gan, trainer=trainer, name=name)
    if hasattr(self.gan, 'x0'):
        gan_inputs = self.gan.x0
    else:
        gan_inputs = self.gan.inputs.x
    config = hc.Config(config)

    if 'lambda' in config:
        self._lambda = self.gan.configurable_param(config['lambda'])
    else:
        self._lambda = 1.0
    self._vlambda = self.gan.configurable_param(config.vlambda or 1.0)

    self.trainablex=tf.Variable(tf.zeros_like(self.gan.inputs.x))
    self.trainableg=tf.Variable(tf.zeros_like(self.gan.inputs.x))
    clearx=tf.assign(self.trainablex, tf.zeros_like(self.gan.inputs.x))
    clearg=tf.assign(self.trainableg, tf.zeros_like(self.gan.inputs.x))
    with tf.get_default_graph().control_dependencies([clearx, clearg]):
        self.adversarial_discriminator = gan.create_component(gan.config.discriminator, name="discriminator", input=tf.concat([gan.inputs.x+self.trainablex,gan.generator.sample+self.trainableg],axis=0), features=[gan.features], reuse=True)
        self.v = tf.gradients(self.adversarial_discriminator.sample, [self.trainablex, self.trainableg])
        self.v = [self._vlambda*v/tf.norm(v, ord=2) for v in self.v]

        self.robustness_discriminator = gan.create_component(gan.config.discriminator, name='discriminator', input=tf.concat([gan.inputs.x+self.v[0], gan.generator.sample+self.v[1]], axis=0), features=[gan.features], reuse=True)

        if config.loss_type == 'gan':
            self.loss = gan.create_component(gan.config.loss, self.robustness_discriminator).sample
        else:
            self.loss = tf.square(self.robustness_discriminator.sample - gan.discriminator.sample)

        self.loss = [self._lambda * self.loss[0], self._lambda * self.loss[1]]

        #self.loss = gan.create_component(gan.config.loss, self.robustness_discriminator)
        self.gan.add_metric('adl', self.loss[0])
        self.gan.add_metric('agl', self.loss[1])
        self.gan.add_metric('vx', tf.reduce_sum(self.v[0]))
        self.gan.add_metric('vg', tf.reduce_sum(self.v[1]))

  def losses(self):
    return self.loss

max_issues_repo_name: KonradLinkowski/HyperGAN

max_forks_repo_name: KonradLinkowski/HyperGAN


text:
# coding: utf-8

""" Astropy coordinate class for the Sagittarius coordinate system """

from __future__ import division, print_function

from astropy.coordinates import frame_transform_graph
import astropy.coordinates as coord
import astropy.units as u
from astropy.coordinates.matrix_utilities import rotation_matrix, matrix_product, matrix_transpose

__all__ = ["Orphan"]

class Orphan(coord.BaseCoordinateFrame):
    """
    A Heliocentric spherical coordinate system defined by the orbit
    of the Orphan stream, as described in
    Newberg et al. 2010 (see: `<http://arxiv.org/abs/1001.0576>`_).

    Note: to be consistent with other stream classes, we refer to the longitude
    and latitude as ``phi1`` and ``phi2`` instead of ``Lambda`` and ``Beta``.

    For more information about this class, see the Astropy documentation
    on coordinate frames in :mod:`~astropy.coordinates`.

    Parameters
    ----------
    representation : :class:`~astropy.coordinates.BaseRepresentation` or None
        A representation object or None to have no data (or use the other keywords)

    phi1 : angle_like, optional, must be keyword
        The longitude-like angle corresponding to Orphan's orbit.
    phi2 : angle_like, optional, must be keyword
        The latitude-like angle corresponding to Orphan's orbit.
    distance : :class:`~astropy.units.Quantity`, optional, must be keyword
        The Distance for this object along the line-of-sight.

    pm_phi1_cosphi2 : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in the longitude-like direction corresponding to
        the Orphan stream's orbit.
    pm_phi2 : :class:`~astropy.units.Quantity`, optional, must be keyword
        The proper motion in the latitude-like direction perpendicular to the
        Orphan stream's orbit.
    radial_velocity : :class:`~astropy.units.Quantity`, optional, must be keyword
        The Distance for this object along the line-of-sight.

    """
    default_representation = coord.SphericalRepresentation
    default_differential = coord.SphericalCosLatDifferential

    frame_specific_representation_info = {
        coord.SphericalRepresentation: [
            coord.RepresentationMapping('lon', 'phi1'),
            coord.RepresentationMapping('lat', 'phi2'),
            coord.RepresentationMapping('distance', 'distance')]
    }

    _default_wrap_angle = 180*u.deg

    def __init__(self, *args, **kwargs):
        wrap = kwargs.pop('wrap_longitude', True)
        super().__init__(*args, **kwargs)
        if wrap and isinstance(self._data, (coord.UnitSphericalRepresentation,
                                            coord.SphericalRepresentation)):
            self._data.lon.wrap_angle = self._default_wrap_angle

# Define the Euler angles
phi = 128.79 * u.degree
theta = 54.39 * u.degree
psi = 90.70 * u.degree

# Generate the rotation matrix using the x-convention (see Goldstein)
D = rotation_matrix(phi, "z")
C = rotation_matrix(theta, "x")
B = rotation_matrix(psi, "z")
R = matrix_product(B, C, D)

@frame_transform_graph.transform(coord.StaticMatrixTransform, coord.Galactic, Orphan)
def galactic_to_orp():
    """ Compute the transformation from Galactic spherical to
        heliocentric Orphan coordinates.
    """
    return R

# Oph to Galactic coordinates
@frame_transform_graph.transform(coord.StaticMatrixTransform, Orphan, coord.Galactic)
def oph_to_galactic():
    """ Compute the transformation from heliocentric Orphan coordinates to
        spherical Galactic.
    """
    return matrix_transpose(galactic_to_orp())

max_stars_repo_name: ltlancas/gala

max_issues_repo_name: ltlancas/gala

max_forks_repo_name: ltlancas/gala

reason: import astropy,from astropy


text:
import subprocess
import sys, os
import json
import cv2
import numpy as np
from imutils.object_detection import non_max_suppression

class AntiTrash():
    def __init__(self):
        # Initialize variables
        self.initiate_command = "./darknet"
        self.cfg = "cfg/yolov3-tiny.cfg"
        self.weights = "yolov3-tiny.weights"
        self.arguments = "-ext_output -dont_show"
        self.output_text = "detections.txt"
        self.input_image = "captured_image.jpg"
        self.visualize = False
        self.threshold = 0.25
        self.objects= {}
        self.validate=  False
        self.argument_parsing()

    def argument_parsing(self):
        """
       Parse all the arguments introduced by command line when executing the detection
       :return: arguments initialization
       """

        # HANDLING ARGUMENTS
        import argparse

        parser = argparse.ArgumentParser(description='Process an image and classify human detection.')
        parser.add_argument('-i', '--input', type=str, default="captured_image.jpg", help='Path to input image file')
        parser.add_argument('--resultsFile', type=str, default='detections.txt', help='File with all the information of the image detection')
        parser.add_argument('--model', type=str, default="yolov3-tiny", help='Model to input')
        parser.add_argument('--visualize', type=bool, default=False, help='Display the image or not')
        parser.add_argument('--threshold', type=float, default=0.25, help='Threshold for the accuract of the detection--> default=0.25')
        parser.add_argument('--directory', type=bool, default=False, help='Introduce True if needs recursive detection. Remember to use input with directory')
        parser.add_argument('--validate', type=bool, default=False, help='Introduce True if wants double check through HOG Detector')
        parser.add_argument('--faceDetector', type=str, default="DataFace/haarcascade_profileface.xml", help='Introduce the xml file for the detection (in directory DataFace)')

        args = parser.parse_args()
        self.cfg = "cfg/" + str(args.model) + ".cfg"
        self.weights = str(args.model) + ".weights"
        self.output_text = args.resultsFile
        self.input_image = args.input
        self.visualize = args.visualize
        self.threshold = args.threshold
        self.input_path = args.directory
        self.validate = args.validate
        self.classifier = args.faceDetector

    def execute_command(self):
        self.command =self.initiate_command+" detect " + self.cfg + " " + self.weights + " " + self.input_image + " " + self.arguments
        print("Executing command: " +str(self.command))
        results = subprocess.run(str(self.command).split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        return results.stdout.decode('utf-8')

    def read_detections(self, output_text = None):
        """
        Import all the results from the File self.output_text
        """
        if output_text is not None:
            self.output_text = output_text
        try:
            with open(self.output_text) as f:
                return str(f.read())
        except Exception as ex:
            print("There was an error while detecting: " + str(output_text))
            print(str(ex))
        return []

    def transformResults(self, text):
        try:
            start_position = str(text).index(str(self.input_image))
            interesting = str(text)[start_position:-1]
            self.findTime(str(interesting))
            lines = interesting.split("\n")

        except Exception as ex:
            print("Error: Could not parse the results file")
            print(str(ex))
        for line in lines[1:]:
            self.toObject(line)

    def toObject(self, object):
        """
        Format is --> "category" : "accuracy"%\t(left_x:  "left"   top_y:  "top"   width:  "width"   height:  "height")
        """
        results = object.split(" ")
        category = results[0].replace(":","")
        accuracy = results[1].split("%")[0]
        left, top, width, height = self.getBoxes(object)
        id = len(self.objects)
        new_obj = ObjectDetected(id, category, accuracy, left, top, width, height)
        self.objects[id] = new_obj

    def findTime(self, text):
        self.time = self.find_between(text, "Predicted in ", " milli-seconds.")

    def getBoxes(self, text):
        left = self.find_between(text, "left_x:", "top_y").replace(" ", "")
        top = self.find_between(text, "top_y:", "width").replace(" ", "")
        width = self.find_between(text, "width:", "height").replace(" ", "")
        height = self.find_between(text, "height:", ")").replace(" ", "")
        return left, top, width, height

    def find_between(self, s, first, last):
        try:
            start = s.index(first) + len(first)
            end = s.index(last, start)
            return s[start:end]
        except ValueError:
            return ""

    def process_image(self):
        print("Processing image : " + str(self.input_image))
        result = self.execute_command()
        self.transformResults(result)
        self.visualize_image()
        return str(self.objects)

    def visualize_image(self):
        if self.visualize:
            image = cv2.imread("predictions.jpg")

            cv2.imshow("Detected image", image)
            k = cv2.waitKey(0)
            if k == 0xFF:
                pass

    def checkPeople(self):
        ids_ = []
        for id, object in self.objects.items():
            if str(object.category) != 'person':
                ids_.append(id)
            elif float(object.accuracy) < self.threshold:
                ids_.append(id)
        for id in ids_:
            del self.objects[id]

    def classify(self):
        self.checkPeople()
        length_yolo = len(self.objects)
        if self.validate:
            length = self.hog_persondetector(self.input_image)
            print("HOG detector has validated "+ str(length)+" person in the image")
            length_yolo += length
        if length_yolo>0:
            print("We have detected "+ str(len(self.objects))+" person in the image "+ str(self.input_image))
            return 1
        else:
            print("No person has been detected in the image "+ str(self.input_image))
            return 0

    def process_directory(self):
        """
        Main script if making recursive processing of videos for whole directory
        :return:
        """
        import glob
        path = self.input_image
        if not str(self.input_image).endswith("/"):
            path = self.input_image + "/"
        # List all .jpg and .png images in the input directory path and its subdirectories
        files = [f for f in glob.glob(path + "**/*.png", recursive=True)]
        files.extend([f for f in glob.glob(path + "**/*.jpg", recursive=True)])

        num_max = len(files)
        if num_max == 0:
            print("Error: directory introduced did not have any .png or .jpf image in it")
            sys.exit()
        else:
            print("Images we will process:" + str(files).replace('[', '\n\t-').replace(']', '\n').replace(',','\n\t-').replace(" ", ""))
            filename = self.write_file(files)
            if filename is not None:
                self.command = self.initiate_command + " detector test cfg/coco.data " + self.cfg + " " + self.weights + " " + self.arguments + " -out result.json"
                myfile = open(str(filename))
                print("We have started processing the images. Process will last around: " +str(num_max*30)+" seconds")
                results = subprocess.run(str(self.command).split(" "), stdin=myfile, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                print("All image processing has been executed: now we will classify all images")
                result = self.transformJson()
                self.remove_file(filename)
                return result

    def transformJson(self):
        self.books = (json.loads(self.read_detections("result.json")))
        result = {}
        for image in self.books:
            self.input_image = image["filename"]
            i=0
            self.objects = {}
            for obj in image["objects"]:
                self.objects[i] = ObjectDetected(i,  obj["name"], obj["confidence"], obj["relative_coordinates"]["center_x"], obj["relative_coordinates"]["center_y"], obj["relative_coordinates"]["width"], obj["relative_coordinates"]["height"])
                i+=1
            result[self.input_image] = self.classify()
        print("Final results for all the images :" + str(result).replace('[', '\n\t-').replace(']', '\n').replace(',','\n\t-').replace(" ", ""))
        print("For more information, use the 'result.json' file")
        return result

    def write_file(self, file_list, filename="File.txt"):
        """
        Writes down all files into a .txt --> create the output file that will be read by darknet
        :param directory: name of the directory where document will be placed in
        :param configurations: text to write down
        :return:
        """
        configurations = str(file_list).replace('[', '').replace(']', '').replace(',','\n').replace("'","").replace(" ","")
        try:
            f = open(filename, "x+")
            f.write(configurations)
            return filename
        except:
            print("There was an error while creating file to write configurations of the detection\nError automatically resolved!")
            self.remove_file(filename)
            return self.write_file(file_list, filename)

    def remove_file(self, file_name):
        if os.path.exists(file_name):
            os.remove(file_name)
        else:
            print("The file does not exist")

    def getRecursive(self):
        return self.input_path

    def getAccuracy(self):
        print("accuracy")

    def hog_persondetector(self,filename):
        self.hog = FaceDetectionCV(classifier=self.classifier, visualize=self.visualize)
        # loading image
        image = cv2.imread(filename)
        # resizing for faster detection
        #frame = cv2.resize(image, (640, 480))
        length = self.hog.process_image(image)
        return length

class FaceDetectionCV:
    def __init__(self, classifier="DataFace/haarcascade_frontalface_default.xml", visualize=False):
        print("Starting validation using "+str(classifier)+ "...")
        self.visualize=visualize
        self.haar_cascade_face = cv2.CascadeClassifier(str(classifier))

    def process_image(self, image):
        # call the function to detect faces
        print("Detecting faces...")
        num_faces, faces = self.detect_faces(self.haar_cascade_face, image)
        print("Faces detected: "+str(num_faces))

        if self.visualize:
            # convert to RGB and display image
            cv2.imshow("Face Detector", self.convertToRGB(faces))
            key = cv2.waitKey(0)
            if key == ord('q'):
                pass
        return len(num_faces)

    def convertToRGB(self,image):
        return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    def detect_faces(self, cascade, test_image, scaleFactor=1.2):
        # convert the test image to gray scale as opencv face detector expects gray images
        gray_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)

        # Applying the haar classifier to detect faces
        faces_rect = cascade.detectMultiScale(gray_image, scaleFactor=scaleFactor, minNeighbors=5)
        for (x, y, w, h) in faces_rect:
            cv2.rectangle(test_image, (x, y), (x + w, y + h), (0, 255, 0), 15)

        return faces_rect, test_image

class ObjectDetected():
    def __init__(self, id,  category, accuracy, left_x, top_y, width, height):
        # Initialize variables
        self.id = id
        self.category=category
        self.accuracy = accuracy
        self.rect = (int(int(left_x)-int(width)/2), int(int(top_y)+int(height)/2),int(int(left_x)+int(width)/2),  int(int(top_y)-int(height)/2))# (left, bottom, right, top)

    def __str__(self):
        return "ID "+ str(self.id)+ ": "+ str(self.category) + " with accuracy " + str(self.accuracy) + " and position " + str(
            self.rect) + "\n"

    def __repr__(self):
        return "ID "+ str(self.id)+ ": "+ str(self.category) + " with accuracy " + str(self.accuracy) + " and position " + str(
            self.rect) + "\n"

if __name__ == "__main__":
    antitrash = AntiTrash()
    # Check if we are actually asked for recursive Detection or just One vid detection

    if antitrash.getRecursive():
        antitrash.process_directory()
    else:
        print(str(antitrash.process_image()))
        antitrash.classify()


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

# In[ ]:

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

def describeDf(df):
    print("Quantitative Data:")
    print(df.describe())
    print("\n")
    print("Qualitative Data:")
    print(df.describe(exclude=[np.number]))
    print("\n")
    print("Smoker/Non-Smoker Count:")
    print(df.Smoker.value_counts())

def EDAplots(df):
    #3 Lineplots for Chargs vs. Age/BMI/# of Children
    #1 Violin Plot for Smokers Charges vs. Non-Smokers Charges
    plt.figure()
    fig2=sns.lineplot(data=df, x="BMI", y="Charges")
    fig2.set_title("Total Charges vs. BMI")

    plt.figure()
    fig3=sns.lineplot(data=df, x="Age", y="Charges")
    fig3.set_title("Total Charges vs. Age")

    plt.figure()
    fig3=sns.lineplot(data=df, x="Children", y="Charges")
    fig3.set_title("Total Charges vs. Number of Children")

    plt.figure()
    fig=sns.violinplot(x='Charges', y="Smoker", data=df)
    fig.set_title("Distribution of Charges for Smokers and Non Smokers")

def load_and_process(url_or_path_to_csv_file):

    df= (

        pd.read_csv(url_or_path_to_csv_file).rename(columns={"age": "Age","sex":"Sex","bmi":"BMI","children":"Children","smoker" : "Smoker", "region":"Region", "charges" : "Charges"})
        .drop(['Sex'], axis=1)
        .dropna()
        .sort_values(by=['Age'])
        .reset_index()
        .drop(['index'], axis=1)
        .assign(BMI_Class=lambda x: np.select([
        (x.BMI <=25),
        (x.BMI >25) & (x.BMI <= 30),
        (x.BMI >30) & (x.BMI <= 40),
        (x.BMI > 40)
        ], ['Healthy', 'OverWeight', 'Obese', 'Extremely Obese']))

    )
    return df

##All Analysis Functions
def smokerVPlot(df):
    #Violin plot of Smoker/Non-Smoker charges
    sns.set_theme()
    plt.figure()
    fig=sns.violinplot(x='Charges', y="Smoker", data=df)
    fig.set_title("Distribution of Charges for Smokers and Non Smokers")
     ###Average Smoker/NonSmoker Charges
    print("Average Smoker Charges:")
    print(df[(df.Smoker=='yes')].Charges.mean())
    print("Average Non-Smoker Charges:")
    print(df[(df.Smoker=='no')].Charges.mean())

def boxPlotAge(df):
    #Box plot of charges per age group
    dfYoungOld=df
    dfYoungOld["Age Group"]=["Age<41" if x<41 else "Age>=41" for x in df['Age']]
    plt.figure()
    fig=sns.boxplot(x='Charges', y="Age Group", data=dfYoungOld, linewidth=2.5, palette='Set2')
    fig = sns.stripplot(x='Charges', y='Age Group', data=dfYoungOld, color="0.3", size=3)
    fig.set_title("Distribution of Charges for Ages Less than and Greater than or Equal to the Median Age")

def boxPlotBMIClass(df):
    #Box plot of charges per BMI class
    plt.figure()
    fig=sns.boxplot(x='Charges', y="BMI_Class", data=df, linewidth=2.5, palette='Set2', order=[ "Healthy", "OverWeight", "Obese", "Extremely Obese"])
    fig = sns.stripplot(x='Charges', y='BMI_Class', data=df, color="0.3", size=3, order=[ "Healthy", "OverWeight", "Obese", "Extremely Obese"])
    fig.set_title("Distribution of Charges for Each BMI Classification")

def boxPlotSmoker(df):
    #Box plot of smoker charges
    plt.figure()
    fig=sns.boxplot(x='Charges', y="Smoker", data=df, linewidth=2.5, palette='Set2')
    fig = sns.stripplot(x='Charges', y='Smoker', data=df, color="0.3", size=3)
    fig.set_title("Distribution of Charges for Smokers and Non-Smokers")

def boxPlotSmokerAge(df):
    ##Two boxplots with scatter plots overlain of Smokers Charges in two age groups
    plt.figure()
    fig=sns.boxplot(x='Charges', y="Smoker", data=df.loc[df["Age"]<41], linewidth=2.5, palette='Set2')
    fig = sns.stripplot(x='Charges', y='Smoker', data=df.loc[df["Age"]<41], color="0.3", size=3)
    fig.set_title("Distribution of Charges for Smokers and Non Smokers Younger than 41")

    plt.figure()
    fig=sns.boxplot(x='Charges', y="Smoker", data=df.loc[df["Age"]>=41], linewidth=2.5, palette='Set2')
    fig = sns.stripplot(x='Charges', y='Smoker', data=df.loc[df["Age"]>=41], color="0.3", size=3)
    fig.set_title("Distribution of Charges for Smokers and Non Smokers Older than 40")

def pieChartSmoker(df):
    ## Pie Chart of Smokers and Non-Smokers Contribution to Charges
    plt.figure()
    dfSmoker=df.loc[df["Smoker"]=="yes"]
    dfNonSmoker=df.loc[df["Smoker"]=="no"]
    labels = 'Smokers', 'Non-Smokers'
    sizes = [dfSmoker["Charges"].sum(),dfNonSmoker["Charges"].sum()]
    explode = (0.1,0)
    fig1, ax1 = plt.subplots()
    ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90)
    ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
    plt.title("Smokers and Non-Smokers Contribution to Total Charges")
    plt.show()

def barSmokerBMI(df):
    #Bar plot of smokers and non-smokers in each BMI class
    plt.figure()
    fig = sns.countplot(x="BMI_Class", hue="Smoker", data=df, order=[ "Healthy", "OverWeight", "Obese", "Extremely Obese"])
    fig.set_title("Smokers and Non Smokers in Each BMI Class")

def barAgeBMI(df):
    #Bar plot of age groups in each BMI class
    plt.figure()
    dfYoungOld=df
    dfYoungOld["Age Group"]=["18<=age<30" if x<30 else "30<=Age<41" if x>=30 and x<41 else "41<=Age<52" if x>=41 and x<52 else "Age>52" for x in df['Age']]
    fig = sns.countplot(x="BMI_Class", hue="Age Group", data=df, order=[ "Healthy", "OverWeight", "Obese", "Extremely Obese"])
    fig.set_title("Age Groups in Each BMI Class")


text:
import pandas as pd
import jsonlines
import src.utils.io as io
from itertools import chain
from src.interface.corpus import Corpus
import logging as log
import numpy

class InputOutputHandler:
    """Interface between the provided training data and other modules.
    When initialized author information for each doc are fetched from the database via the
    provided Corpus object."""

    def __init__(self,
                corpus,
                fsequence = "./training/training-sequence.tsv",
                fquery = "./training/fair-TREC-training-sample.json",
                fgroup = "./training/fair-TREC-sample-author-groups.csv"):

        self.corpus = corpus

        queries = io.read_jsonlines(fquery, handler = self.__unnest_query)
        queries = list(chain.from_iterable(queries))
        self.seq = pd.read_csv(fsequence, names=["sid", "q_num", "qid"], sep='\.|,', engine='python')
        self.queries = pd.DataFrame(queries)
        self.groups = pd.read_csv(fgroup, dtype={"author_id":str, "gid":str})
        self.authors = None

    def get_queries(self):
            return self.queries.drop_duplicates()

    def get_query_seq(self):
        seq = pd.merge(self.seq, self.queries, on = "qid", how = 'left')
        return seq.drop_duplicates()

    def get_authors(self):
        if self.authors is None:
            ids = self.queries.doc_id.drop_duplicates().tolist()
            self.authors = self.corpus.get_authors(ids)
            self.authors = pd.merge(self.authors, self.groups, on="author_id", how='left')
        return self.authors

    def __unnest_query(self, query):
        ret = []
        for rank, doc in enumerate(query.get("documents"), start = 1):
            ret.append({
                "doc_id":doc.get("doc_id"),
                "rank": rank,
                "relevance": doc.get("relevance"),
                "frequency": query.get("frequency"),
                "qid": query.get("qid"),
                "query" : query.get("query")
            })
        return ret

    def write_submission(self, model, outfile = "./training/submission.json"):
        """
        accepts a model and writes a jsonlines submission file.
        """
        model.predictions.sort_values(['sid', 'q_num', 'rank'], axis = 0, inplace = True)
        submission = model.predictions.groupby(['sid', 'q_num', 'qid']).apply(lambda df: pd.Series({'ranking': df['doc_id']}))
        submission.reset_index(inplace = True)
        q_num = [str(submission['sid'][i]) + "." + str(submission['q_num'][i]) for i in range(len(submission))]
        submission['q_num'] =  q_num
        submission.drop('sid', axis = 1, inplace = True)
        submission.to_json(outfile, orient = 'records', lines = True)

max_stars_repo_name: irgroup/fair-trec

max_issues_repo_name: irgroup/fair-trec

max_forks_repo_name: irgroup/fair-trec


text:
import os
from tqdm import tqdm
import numpy as np
import tensorflow as tf

from dataset import DatasetFactory
from network_hrnet import HRNet
from config import parse_args
from ocnn import octree_property

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

FLAGS = parse_args()

## graph
octree, label = DatasetFactory(FLAGS.DATA.test)()
hrnet = HRNet(FLAGS.MODEL)
tensors = hrnet.network(octree, training=False)
with tf.variable_scope('signal'):
  child = octree_property(octree, property_name='child', dtype=tf.int32,
                          depth=FLAGS.DATA.test.depth, channel=1)
  child = tf.reshape(child, [-1])

## output
FLAGSS = FLAGS.SOLVER
test_iter = FLAGSS.test_iter
output_prefix = FLAGSS.logdir + '/' + os.path.basename(FLAGS.DATA.test.location)

# classification features
fc1 = tensors['fc1']
channel1 = int(fc1.shape[1])
fc1 = tf.reshape(fc1, [channel1])
features1 = np.zeros((test_iter, channel1), dtype=np.float32)

fc2 = tensors['fc2']
fc2 = tf.nn.l2_normalize(fc2, axis=1)
channel2 = int(fc2.shape[1])
fc2 = tf.reshape(fc2, [channel2])
features2 = np.zeros((test_iter, channel2), dtype=np.float32)

# segmentation features
seg = tensors['logit_seg'] # N x C
seg = tf.nn.l2_normalize(seg, axis=1)

# label
labels = np.zeros((test_iter))

## run
def cls_features(sess):
  print('Classification features...')
  for i in tqdm(range(0, test_iter), ncols=80):
    f1, f2, l = sess.run([fc1, fc2, label])
    features1[i, :] = f1
    features2[i, :] = f2
    labels[i] = l

  np.save(output_prefix + '_fc1', features1)
  np.save(output_prefix + '_fc2', features2)
  np.save(output_prefix + '_label', labels)

def seg_features(sess):
  print('Segmentation features...')
  for i in tqdm(range(0, test_iter), ncols=80):
    s, o, c, f = sess.run([seg, octree, child, fc2])

    o.tofile(output_prefix + '_%03d.octree' % i)
    np.save(output_prefix + '_%03d.seg.npy' % i, s)
    np.save(output_prefix + '_%03d.child.npy' % i, c)
    np.save(output_prefix + '_%03d.fc2.npy' % i, f)

assert(FLAGSS.ckpt)
tf_saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
  tf.summary.FileWriter(FLAGSS.logdir, sess.graph)
  print('Restore from checkpoint: ', FLAGSS.ckpt)
  tf_saver.restore(sess, FLAGSS.ckpt)

  if FLAGSS.run == 'cls':
    cls_features(sess)
  elif FLAGSS.run == 'seg':
    seg_features(sess)
  else:
    print('Error, unsupported SOLVER.run: ' + FLAGSS.run)

max_stars_repo_name: enpelonio/O-CNN

max_issues_repo_name: enpelonio/O-CNN

max_forks_repo_name: enpelonio/O-CNN


text:
import inspect
import sys
import tempfile
from io import StringIO
from unittest import mock

import numpy as np
from numpy.testing import (
    assert_, assert_equal, assert_raises, assert_raises_regex)
from numpy.core.overrides import (
    _get_implementing_args, array_function_dispatch,
    verify_matching_signatures, ARRAY_FUNCTION_ENABLED)
from numpy.compat import pickle
import pytest

requires_array_function = pytest.mark.skipif(
    not ARRAY_FUNCTION_ENABLED,
    reason="__array_function__ dispatch not enabled.")

def _return_not_implemented(self, *args, **kwargs):
    return NotImplemented

# need to define this at the top level to test pickling
@array_function_dispatch(lambda array: (array,))
def dispatched_one_arg(array):
    """Docstring."""
    return 'original'

@array_function_dispatch(lambda array1, array2: (array1, array2))
def dispatched_two_arg(array1, array2):
    """Docstring."""
    return 'original'

class TestGetImplementingArgs:

    def test_ndarray(self):
        array = np.array(1)

        args = _get_implementing_args([array])
        assert_equal(list(args), [array])

        args = _get_implementing_args([array, array])
        assert_equal(list(args), [array])

        args = _get_implementing_args([array, 1])
        assert_equal(list(args), [array])

        args = _get_implementing_args([1, array])
        assert_equal(list(args), [array])

    def test_ndarray_subclasses(self):

        class OverrideSub(np.ndarray):
            __array_function__ = _return_not_implemented

        class NoOverrideSub(np.ndarray):
            pass

        array = np.array(1).view(np.ndarray)
        override_sub = np.array(1).view(OverrideSub)
        no_override_sub = np.array(1).view(NoOverrideSub)

        args = _get_implementing_args([array, override_sub])
        assert_equal(list(args), [override_sub, array])

        args = _get_implementing_args([array, no_override_sub])
        assert_equal(list(args), [no_override_sub, array])

        args = _get_implementing_args(
            [override_sub, no_override_sub])
        assert_equal(list(args), [override_sub, no_override_sub])

    def test_ndarray_and_duck_array(self):

        class Other:
            __array_function__ = _return_not_implemented

        array = np.array(1)
        other = Other()

        args = _get_implementing_args([other, array])
        assert_equal(list(args), [other, array])

        args = _get_implementing_args([array, other])
        assert_equal(list(args), [array, other])

    def test_ndarray_subclass_and_duck_array(self):

        class OverrideSub(np.ndarray):
            __array_function__ = _return_not_implemented

        class Other:
            __array_function__ = _return_not_implemented

        array = np.array(1)
        subarray = np.array(1).view(OverrideSub)
        other = Other()

        assert_equal(_get_implementing_args([array, subarray, other]),
                     [subarray, array, other])
        assert_equal(_get_implementing_args([array, other, subarray]),
                     [subarray, array, other])

    def test_many_duck_arrays(self):

        class A:
            __array_function__ = _return_not_implemented

        class B(A):
            __array_function__ = _return_not_implemented

        class C(A):
            __array_function__ = _return_not_implemented

        class D:
            __array_function__ = _return_not_implemented

        a = A()
        b = B()
        c = C()
        d = D()

        assert_equal(_get_implementing_args([1]), [])
        assert_equal(_get_implementing_args([a]), [a])
        assert_equal(_get_implementing_args([a, 1]), [a])
        assert_equal(_get_implementing_args([a, a, a]), [a])
        assert_equal(_get_implementing_args([a, d, a]), [a, d])
        assert_equal(_get_implementing_args([a, b]), [b, a])
        assert_equal(_get_implementing_args([b, a]), [b, a])
        assert_equal(_get_implementing_args([a, b, c]), [b, c, a])
        assert_equal(_get_implementing_args([a, c, b]), [c, b, a])

    def test_too_many_duck_arrays(self):
        namespace = dict(__array_function__=_return_not_implemented)
        types = [type('A' + str(i), (object,), namespace) for i in range(33)]
        relevant_args = [t() for t in types]

        actual = _get_implementing_args(relevant_args[:32])
        assert_equal(actual, relevant_args[:32])

        with assert_raises_regex(TypeError, 'distinct argument types'):
            _get_implementing_args(relevant_args)

class TestNDArrayArrayFunction:

    @requires_array_function
    def test_method(self):

        class Other:
            __array_function__ = _return_not_implemented

        class NoOverrideSub(np.ndarray):
            pass

        class OverrideSub(np.ndarray):
            __array_function__ = _return_not_implemented

        array = np.array([1])
        other = Other()
        no_override_sub = array.view(NoOverrideSub)
        override_sub = array.view(OverrideSub)

        result = array.__array_function__(func=dispatched_two_arg,
                                          types=(np.ndarray,),
                                          args=(array, 1.), kwargs={})
        assert_equal(result, 'original')

        result = array.__array_function__(func=dispatched_two_arg,
                                          types=(np.ndarray, Other),
                                          args=(array, other), kwargs={})
        assert_(result is NotImplemented)

        result = array.__array_function__(func=dispatched_two_arg,
                                          types=(np.ndarray, NoOverrideSub),
                                          args=(array, no_override_sub),
                                          kwargs={})
        assert_equal(result, 'original')

        result = array.__array_function__(func=dispatched_two_arg,
                                          types=(np.ndarray, OverrideSub),
                                          args=(array, override_sub),
                                          kwargs={})
        assert_equal(result, 'original')

        with assert_raises_regex(TypeError, 'no implementation found'):
            np.concatenate((array, other))

        expected = np.concatenate((array, array))
        result = np.concatenate((array, no_override_sub))
        assert_equal(result, expected.view(NoOverrideSub))
        result = np.concatenate((array, override_sub))
        assert_equal(result, expected.view(OverrideSub))

    def test_no_wrapper(self):
        # This shouldn't happen unless a user intentionally calls
        # __array_function__ with invalid arguments, but check that we raise
        # an appropriate error all the same.
        array = np.array(1)
        func = lambda x: x
        with assert_raises_regex(AttributeError, '_implementation'):
            array.__array_function__(func=func, types=(np.ndarray,),
                                     args=(array,), kwargs={})

@requires_array_function
class TestArrayFunctionDispatch:

    def test_pickle(self):
        for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
            roundtripped = pickle.loads(
                    pickle.dumps(dispatched_one_arg, protocol=proto))
            assert_(roundtripped is dispatched_one_arg)

    def test_name_and_docstring(self):
        assert_equal(dispatched_one_arg.__name__, 'dispatched_one_arg')
        if sys.flags.optimize < 2:
            assert_equal(dispatched_one_arg.__doc__, 'Docstring.')

    def test_interface(self):

        class MyArray:
            def __array_function__(self, func, types, args, kwargs):
                return (self, func, types, args, kwargs)

        original = MyArray()
        (obj, func, types, args, kwargs) = dispatched_one_arg(original)
        assert_(obj is original)
        assert_(func is dispatched_one_arg)
        assert_equal(set(types), {MyArray})
        # assert_equal uses the overloaded np.iscomplexobj() internally
        assert_(args == (original,))
        assert_equal(kwargs, {})

    def test_not_implemented(self):

        class MyArray:
            def __array_function__(self, func, types, args, kwargs):
                return NotImplemented

        array = MyArray()
        with assert_raises_regex(TypeError, 'no implementation found'):
            dispatched_one_arg(array)

@requires_array_function
class TestVerifyMatchingSignatures:

    def test_verify_matching_signatures(self):

        verify_matching_signatures(lambda x: 0, lambda x: 0)
        verify_matching_signatures(lambda x=None: 0, lambda x=None: 0)
        verify_matching_signatures(lambda x=1: 0, lambda x=None: 0)

        with assert_raises(RuntimeError):
            verify_matching_signatures(lambda a: 0, lambda b: 0)
        with assert_raises(RuntimeError):
            verify_matching_signatures(lambda x: 0, lambda x=None: 0)
        with assert_raises(RuntimeError):
            verify_matching_signatures(lambda x=None: 0, lambda y=None: 0)
        with assert_raises(RuntimeError):
            verify_matching_signatures(lambda x=1: 0, lambda y=1: 0)

    def test_array_function_dispatch(self):

        with assert_raises(RuntimeError):
            @array_function_dispatch(lambda x: (x,))
            def f(y):
                pass

        # should not raise
        @array_function_dispatch(lambda x: (x,), verify=False)
        def f(y):
            pass

def _new_duck_type_and_implements():
    """Create a duck array type and implements functions."""
    HANDLED_FUNCTIONS = {}

    class MyArray:
        def __array_function__(self, func, types, args, kwargs):
            if func not in HANDLED_FUNCTIONS:
                return NotImplemented
            if not all(issubclass(t, MyArray) for t in types):
                return NotImplemented
            return HANDLED_FUNCTIONS[func](*args, **kwargs)

    def implements(numpy_function):
        """Register an __array_function__ implementations."""
        def decorator(func):
            HANDLED_FUNCTIONS[numpy_function] = func
            return func
        return decorator

    return (MyArray, implements)

@requires_array_function
class TestArrayFunctionImplementation:

    def test_one_arg(self):
        MyArray, implements = _new_duck_type_and_implements()

        @implements(dispatched_one_arg)
        def _(array):
            return 'myarray'

        assert_equal(dispatched_one_arg(1), 'original')
        assert_equal(dispatched_one_arg(MyArray()), 'myarray')

    def test_optional_args(self):
        MyArray, implements = _new_duck_type_and_implements()

        @array_function_dispatch(lambda array, option=None: (array,))
        def func_with_option(array, option='default'):
            return option

        @implements(func_with_option)
        def my_array_func_with_option(array, new_option='myarray'):
            return new_option

        # we don't need to implement every option on __array_function__
        # implementations
        assert_equal(func_with_option(1), 'default')
        assert_equal(func_with_option(1, option='extra'), 'extra')
        assert_equal(func_with_option(MyArray()), 'myarray')
        with assert_raises(TypeError):
            func_with_option(MyArray(), option='extra')

        # but new options on implementations can't be used
        result = my_array_func_with_option(MyArray(), new_option='yes')
        assert_equal(result, 'yes')
        with assert_raises(TypeError):
            func_with_option(MyArray(), new_option='no')

    def test_not_implemented(self):
        MyArray, implements = _new_duck_type_and_implements()

        @array_function_dispatch(lambda array: (array,), module='my')
        def func(array):
            return array

        array = np.array(1)
        assert_(func(array) is array)
        assert_equal(func.__module__, 'my')

        with assert_raises_regex(
                TypeError, "no implementation found for 'my.func'"):
            func(MyArray())

class TestNDArrayMethods:

    def test_repr(self):
        # gh-12162: should still be defined even if __array_function__ doesn't
        # implement np.array_repr()

        class MyArray(np.ndarray):
            def __array_function__(*args, **kwargs):
                return NotImplemented

        array = np.array(1).view(MyArray)
        assert_equal(repr(array), 'MyArray(1)')
        assert_equal(str(array), '1')

class TestNumPyFunctions:

    def test_set_module(self):
        assert_equal(np.sum.__module__, 'numpy')
        assert_equal(np.char.equal.__module__, 'numpy.char')
        assert_equal(np.fft.fft.__module__, 'numpy.fft')
        assert_equal(np.linalg.solve.__module__, 'numpy.linalg')

    def test_inspect_sum(self):
        signature = inspect.signature(np.sum)
        assert_('axis' in signature.parameters)

    @requires_array_function
    def test_override_sum(self):
        MyArray, implements = _new_duck_type_and_implements()

        @implements(np.sum)
        def _(array):
            return 'yes'

        assert_equal(np.sum(MyArray()), 'yes')

    @requires_array_function
    def test_sum_on_mock_array(self):

        # We need a proxy for mocks because __array_function__ is only looked
        # up in the class dict
        class ArrayProxy:
            def __init__(self, value):
                self.value = value
            def __array_function__(self, *args, **kwargs):
                return self.value.__array_function__(*args, **kwargs)
            def __array__(self, *args, **kwargs):
                return self.value.__array__(*args, **kwargs)

        proxy = ArrayProxy(mock.Mock(spec=ArrayProxy))
        proxy.value.__array_function__.return_value = 1
        result = np.sum(proxy)
        assert_equal(result, 1)
        proxy.value.__array_function__.assert_called_once_with(
            np.sum, (ArrayProxy,), (proxy,), {})
        proxy.value.__array__.assert_not_called()

    @requires_array_function
    def test_sum_forwarding_implementation(self):

        class MyArray(np.ndarray):

            def sum(self, axis, out):
                return 'summed'

            def __array_function__(self, func, types, args, kwargs):
                return super().__array_function__(func, types, args, kwargs)

        # note: the internal implementation of np.sum() calls the .sum() method
        array = np.array(1).view(MyArray)
        assert_equal(np.sum(array), 'summed')

class TestArrayLike:

    class MyArray():

        def __init__(self, function=None):
            self.function = function

        def __array_function__(self, func, types, args, kwargs):
            try:
                my_func = getattr(TestArrayLike.MyArray, func.__name__)
            except AttributeError:
                return NotImplemented
            return my_func(*args, **kwargs)

    class MyNoArrayFunctionArray():

        def __init__(self, function=None):
            self.function = function

    def add_method(name, arr_class, enable_value_error=False):
        def _definition(*args, **kwargs):
            # Check that `like=` isn't propagated downstream
            assert 'like' not in kwargs

            if enable_value_error and 'value_error' in kwargs:
                raise ValueError

            return arr_class(getattr(arr_class, name))
        setattr(arr_class, name, _definition)

    def func_args(*args, **kwargs):
        return args, kwargs

    @requires_array_function
    def test_array_like_not_implemented(self):
        TestArrayLike.add_method('array', TestArrayLike.MyArray)

        ref = TestArrayLike.MyArray.array()

        with assert_raises_regex(TypeError, 'no implementation found'):
            array_like = np.asarray(1, like=ref)

    _array_tests = [
        ('array', *func_args((1,))),
        ('asarray', *func_args((1,))),
        ('asanyarray', *func_args((1,))),
        ('ascontiguousarray', *func_args((2, 3))),
        ('asfortranarray', *func_args((2, 3))),
        ('require', *func_args((np.arange(6).reshape(2, 3),),
                               requirements=['A', 'F'])),
        ('empty', *func_args((1,))),
        ('full', *func_args((1,), 2)),
        ('ones', *func_args((1,))),
        ('zeros', *func_args((1,))),
        ('arange', *func_args(3)),
        ('frombuffer', *func_args(b'\x00' * 8, dtype=int)),
        ('fromiter', *func_args(range(3), dtype=int)),
        ('fromstring', *func_args('1,2', dtype=int, sep=',')),
        ('loadtxt', *func_args(lambda: StringIO('0 1\n2 3'))),
        ('genfromtxt', *func_args(lambda: StringIO(u'1,2.1'),
                                  dtype=[('int', 'i8'), ('float', 'f8')],
                                  delimiter=',')),
    ]

    @pytest.mark.parametrize('function, args, kwargs', _array_tests)
    @pytest.mark.parametrize('numpy_ref', [True, False])
    @requires_array_function
    def test_array_like(self, function, args, kwargs, numpy_ref):
        TestArrayLike.add_method('array', TestArrayLike.MyArray)
        TestArrayLike.add_method(function, TestArrayLike.MyArray)
        np_func = getattr(np, function)
        my_func = getattr(TestArrayLike.MyArray, function)

        if numpy_ref is True:
            ref = np.array(1)
        else:
            ref = TestArrayLike.MyArray.array()

        like_args = tuple(a() if callable(a) else a for a in args)
        array_like = np_func(*like_args, **kwargs, like=ref)

        if numpy_ref is True:
            assert type(array_like) is np.ndarray

            np_args = tuple(a() if callable(a) else a for a in args)
            np_arr = np_func(*np_args, **kwargs)

            # Special-case np.empty to ensure values match
            if function == "empty":
                np_arr.fill(1)
                array_like.fill(1)

            assert_equal(array_like, np_arr)
        else:
            assert type(array_like) is TestArrayLike.MyArray
            assert array_like.function is my_func

    @pytest.mark.parametrize('function, args, kwargs', _array_tests)
    @pytest.mark.parametrize('numpy_ref', [True, False])
    @requires_array_function
    def test_no_array_function_like(self, function, args, kwargs, numpy_ref):
        TestArrayLike.add_method('array', TestArrayLike.MyNoArrayFunctionArray)
        TestArrayLike.add_method(function, TestArrayLike.MyNoArrayFunctionArray)
        np_func = getattr(np, function)
        my_func = getattr(TestArrayLike.MyNoArrayFunctionArray, function)

        if numpy_ref is True:
            ref = np.array(1)
        else:
            ref = TestArrayLike.MyNoArrayFunctionArray.array()

        like_args = tuple(a() if callable(a) else a for a in args)
        array_like = np_func(*like_args, **kwargs, like=ref)

        assert type(array_like) is np.ndarray
        if numpy_ref is True:
            np_args = tuple(a() if callable(a) else a for a in args)
            np_arr = np_func(*np_args, **kwargs)

            # Special-case np.empty to ensure values match
            if function == "empty":
                np_arr.fill(1)
                array_like.fill(1)

            assert_equal(array_like, np_arr)

    @pytest.mark.parametrize('numpy_ref', [True, False])
    def test_array_like_fromfile(self, numpy_ref):
        TestArrayLike.add_method('array', TestArrayLike.MyArray)
        TestArrayLike.add_method("fromfile", TestArrayLike.MyArray)

        if numpy_ref is True:
            ref = np.array(1)
        else:
            ref = TestArrayLike.MyArray.array()

        data = np.random.random(5)

        fname = tempfile.mkstemp()[1]
        data.tofile(fname)

        array_like = np.fromfile(fname, like=ref)
        if numpy_ref is True:
            assert type(array_like) is np.ndarray
            np_res = np.fromfile(fname, like=ref)
            assert_equal(np_res, data)
            assert_equal(array_like, np_res)
        else:
            assert type(array_like) is TestArrayLike.MyArray
            assert array_like.function is TestArrayLike.MyArray.fromfile

    @requires_array_function
    def test_exception_handling(self):
        TestArrayLike.add_method(
            'array',
            TestArrayLike.MyArray,
            enable_value_error=True,
        )

        ref = TestArrayLike.MyArray.array()

        with assert_raises(ValueError):
            np.array(1, value_error=True, like=ref)

max_stars_repo_name: kurtamohler/numpy

max_issues_repo_name: kurtamohler/numpy

max_forks_repo_name: kurtamohler/numpy

reason: import numpy,from numpy