text:
# -*- coding: utf-8 -*-

"""
metrics.py
~~~~~~~~~~

A set of measurement tools to aid users designing time-aware experiments.

"""
from collections import defaultdict

import matplotlib.pyplot as plt
import numpy as np
from sklearn import metrics as skmetrics
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelEncoder

from tesseract import utils

def aut(results, metric=None):
    """Compute the AUT with respect to the given metric.

    Note that for results spanning a _single_ time period, AUT = 0 as this is
    not considered a time-aware evaluation.

    Args:
        results: The set of time-aware results to operate over.
        metric: The metric to operate with respect to.

    Returns:
        float: A measure of robustness for the applied model over the time
            spanning the results.

    """
    if isinstance(results, dict):
        results = results[metric]

    if len(results) <= 1:
        return 0

    return np.trapz(results) / (len(results) - 1)

def error_rate(results, metric='f1'):
    """Return the error rate formulation as it relates to the given metric.

    Args:
        results: The set of time-aware evaluation results to operate over.
        metric: The metric to operate with respect to (f1|precision|recall).

    Returns:
        float: The rate representing error for the given metric.

    """
    return {
        'f1': errors(results) / (np.sum(results['p']) + np.sum(results['n'])),
        'precision': np.sum(results['fn']) / (
                np.sum(results['tp']) + np.sum(results['fn'])),
        'recall': np.sum(results['fp']) / (
                np.sum(results['tn']) + np.sum(results['fp'])),
    }[metric]

def errors(results):
    """Return the total misclassifications in the results."""
    return np.sum(results['fn']) + np.sum(results['fp'])

def plot_alpha_assessment(alpha_assessment_results, outfile=None):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.boxplot((alpha_assessment_results['negative_predictions']['correct'],
                alpha_assessment_results['negative_predictions']['incorrect'],
                alpha_assessment_results['positive_predictions']['correct'],
                alpha_assessment_results['positive_predictions']['incorrect']))
    ax.set_xticklabels(('Neg C', 'Neg IC', 'Pos C', 'Pos IC'))

    if outfile:
        plt.savefig(outfile)
    else:
        plt.show()

    return fig, ax

# def plot_results(results, outfile=None, fields=None, title='Scores over time',
#                  quiet=False):
#     if not quiet:
#         logging.info(results)
#     if outfile:
#         results.to_csv(os.path.splitext(outfile)[0] + '.csv')
#
#     if fields is None:
#         fields = ['f1', 'precision', 'recall',
#                   'f1_n', 'precision_n', 'recall_n']
#
#     colors = ('#F2385A', '#F5A503', '#4AD9D9',
#               '#FF9999', '#FFDD99', '#AAEEEE')
#     ax = results[fields].plot(linestyle='--', marker='o', color=colors)
#
#     plt.title(title)
#     ax.set_xlabel('Testing round')
#     ax.set_ylabel('Score')
#     ax.set_ylim([0, 1])
#     ax.set_yticks(np.arange(0, 1.1, 0.1))
#     ax.set_xticks(results.index)
#     ax.grid('on', which='major', linestyle=':', axis='y')
#     plt.tight_layout()
#
#     if outfile:
#         plt.savefig(outfile)
#     else:
#         plt.show()
#
#     return ax

# def plot_by_time(y, t, granularity='month', type='line', outfile=None):
#     df = pd.DataFrame(y, columns=['positive'], index=t)
#     df['negative'] = [1 ^ x for x in df['positive']]
#
#     try:
#         offset_alias = {
#             'year': '1Y',
#             'quarter': '1Q',
#             'month': '1M',
#             'week': '1W',
#             'day': '1D'
#         }[granularity]
#     except KeyError:
#         # Allow a specific offset alias to be passed in
#         offset_alias = granularity
#
#     df = df.resample(offset_alias).sum()
#
#     colors = ('#cc0000', '#66b3ff')
#     plot_fn = df.plot.bar if type == 'bar' else df.plot
#     ax = plot_fn(color=colors, marker='o', linestyle='--')
#
#     plt.title('Frequency of class membership by {}'.format(granularity))
#     ax.set_xlabel('{}(s)'.format(granularity))
#     ax.set_ylabel('Frequency')
#     ax.grid('on', which='major', linestyle=':', axis='y')
#     plt.tight_layout()
#
#     if outfile:
#         plt.savefig(outfile)
#     else:
#         plt.show()
#
#     return ax

def summarize(y):
    positive = sum(y)
    negative = len(y) - positive
    print('Class counts:')
    print('-' * 20)
    print('negative: {}'.format(negative))
    print('positive: {}'.format(positive))
    print('\nTotal objects:')
    print('-' * 20)
    print('{} ({:.04}% positive)'.format(len(y), positive / len(y) * 100))

def get_train_info(X_train, y_train, t_train, existing=None):
    # Ensure results are a defaultdict(list)

    results = defaultdict(list, existing) if existing else defaultdict(list)

    # Ensure label array is a numpy array

    y_train = np.array(y_train)

    train_pos = np.sum(y_train)

    results['train_pos'].append(train_pos)
    results['train_neg'].append(len(y_train) - train_pos)
    results['train_tot'].append(len(y_train))

    return results

def calculate_metrics(y_true, y_pred, existing=None,
                      raw_scores=None, periods=1):
    periods = len(y_pred) if periods == -1 else periods

    if periods > 1:
        for y_t, y_p in zip(y_true, y_pred):
            existing = calculate_metrics(y_t, y_p, existing, raw_scores)
        return existing

    # Ensure results are a defaultdict(list)

    results = defaultdict(list, existing) if existing else defaultdict(list)

    # Ensure both label vectors are Numpy arrays

    y_true = np.array(y_true)
    y_pred = np.array(y_pred)

    # Heuristic to check if input are raw scores

    y_raw = None
    if (raw_scores or
            (raw_scores is None and
             utils.check_for_raw_scores(y_pred))):
        y_raw = y_pred

    # Convert output scores or categorical labels to integer labels

    y_pred = utils.resolve_categorical(y_pred)
    y_true = utils.resolve_categorical(y_true)

    # Ensure labels are encoded as integer labels

    if isinstance(y_pred[0], str):
        if isinstance(y_true[0], str):
            try:
                y_pred = np.array(y_pred, dtype='int32')
                y_true = np.array(y_true, dtype='int32')
            except ValueError:
                enc = LabelEncoder().fit(y_true)
                y_true = enc.transform(y_true)
                y_pred = enc.transform(y_pred)
        else:
            try:
                y_pred = np.array(y_pred, dtype='int32')
            except ValueError:
                y_pred = LabelEncoder().fit_transform(y_pred)

    assert len(set(y_true)) <= 2 and len(set(y_pred)) <= 2

    # Update total positive and negative predictions

    tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=(0, 1)).ravel()
    p = tp + fn
    n = tn + fp

    results['tp'].append(tp)
    results['fp'].append(fp)
    results['tn'].append(tn)
    results['fn'].append(fn)

    results['p'].append(p)
    results['n'].append(n)
    results['tot'].append(p + n)

    # Update cumulative totals

    results['tp_cumu'].append(np.sum(results['tp']))
    results['fp_cumu'].append(np.sum(results['fp']))
    results['tn_cumu'].append(np.sum(results['tn']))
    results['fn_cumu'].append(np.sum(results['fn']))

    results['p_cumu'].append(np.sum(results['p']))
    results['n_cumu'].append(np.sum(results['n']))
    results['tot_cumu'].append(np.sum(results['tot']))

    # Update true/false positive/negative rates

    if p == 0:
        results['tpr'].append(np.nan)
        results['fnr'].append(np.nan)
    else:
        results['tpr'].append(tp / p)
        results['fnr'].append(fn / p)

    if n == 0:
        results['fpr'].append(np.nan)
        results['tnr'].append(np.nan)
    else:
        results['fpr'].append(fp / n)
        results['tnr'].append(tn / n)

    # Calculate AUC-ROC if raw scores have been supplied

    if y_raw is not None:

        # Some classifiers output with a score/prob for each class, this
        # simply includes only the score/prob of the predicted class as
        # skmetrics.roc_auc_score expects both inputs to be the same shape
        if y_raw.shape != y_true.shape:
            y_scores = np.array([np.max(v) for v in y_raw])
        else:
            y_scores = y_raw

        try:
            results['auc_roc'].append(skmetrics.roc_auc_score(y_true, y_scores))
        except ValueError as e:
            print(e)
            results['auc_roc'].append(np.nan)

    # Calculate precision, recall and F1 wrt positive and negative classes

    results['precision'].append(
        skmetrics.precision_score(y_true, y_pred, pos_label=1))
    results['recall'].append(
        skmetrics.recall_score(y_true, y_pred, pos_label=1))
    results['f1'].append(skmetrics.f1_score(y_true, y_pred, pos_label=1))

    results['precision_n'].append(
        skmetrics.precision_score(y_true, y_pred, pos_label=0))
    results['recall_n'].append(
        skmetrics.recall_score(y_true, y_pred, pos_label=0))
    results['f1_n'].append(skmetrics.f1_score(y_true, y_pred, pos_label=0))

    return results

def print_metrics(results, keys=None, header=True):
    if keys is None:
        keys = [
            ('Actual pos', 'p'),
            ('Actual neg', 'n'),
            ('Total', 'tot'),
            ('hline', 'hline'),
            ('TPR', 'tpr'),
            ('FPR', 'fpr'),
            ('TNR', 'tnr'),
            ('FNR', 'fnr'),
            ('AUC ROC', 'auc_roc'),
            ('hline', 'hline'),
            ('Precision', 'precision'),
            ('Recall', 'recall'),
            ('F1', 'f1'),
            ('hline', 'hline')]
    else:
        if isinstance(keys[0], str):
            keys = [(k.title(), k) for k in keys]

    periods = max(len(v) for v in results.values())

    def print_hline():
        print(('-' * 12) + '+' + ('-' * 7 * periods))

    if header:
        header = '{:12}|  '.format('Test period')
        header += ''.join(['{:^7}'.format(i) for i in range(1, periods + 1)])
        print_hline()
        print(header)
        print_hline()

    for label, key in keys:
        if label == 'hline':
            print_hline()

        elif results[key]:
            row = '{:12}|'.format(label)
            for result in results[key]:
                if isinstance(result, float):
                    row += '{:>7.3f}'.format(result)
                else:
                    row += '{:>7}'.format(result)
            print(row)

        else:
            pass  # Silently skip missing keys

# def cumulative(results, metric):
#     if metric not in ('f1', 'precision', 'recall',
#                       'f1_n', 'precision_n', 'recall_n'):
#         return np.cumsum(results[metric])
#
#     tps = np.cumsum(results['tp'])
#     tns = np.cumsum(results['tn'])
#     fps = np.cumsum(results['fp'])
#     fns = np.cumsum(results['fn'])
#
#     precision = tps / (tps + fps)
#     recall = tps / (tps + fns)
#     f1 = 2 * precision * recall / (precision + recall)
#
#     if metric == 'f1':
#         return f1
#     if metric == 'precision':
#         return precision
#     if metric == 'recall':
#         return recall
#
#     precision_n = tns / (tns + fns)
#     recall_n = tns / (tns + fps)
#     f1_n = 2 * precision_n * recall_n / (precision_n + recall_n)
#
#     if metric == 'f1_n':
#         return f1_n
#     if metric == 'precision_n':
#         return precision_n
#     if metric == 'recall_n':
#         return recall_n


text:
#  Copyright (c) 2021, Greg Michael
#  Licensed under BSD 3-Clause License. See LICENSE.txt for details.

import numpy as np

from .sigfigs import sigfigs

def scientific_notation(n,sf=3,force=False):
    '''
    Convert number to latex-style scientific notation string

    values >0.01 and <10000 are shown without multiplier unless force=True

    :param n: number to convert
    :param sf: number of significant figures
    :param force: force notation even for low-value exponents
    :return: latex-style formatted string
    '''

    if -2 < np.log10(abs(n)) < 4 and not force:
            return sigfigs(n, sf)

    s0 = ("{:." + str(sf-1) + "e}").format(n)
    s = s0.split('e')
    return s[0]+'×10^{'+str(int(s[1]))+'}'

ext: py

max_stars_repo_name: AndrewAnnex/craterstats

max_issues_repo_name: AndrewAnnex/craterstats

max_forks_repo_name: AndrewAnnex/craterstats

reason: import numpy


text:
import discord
import glob
from discord.ext import commands,tasks
import gspread
import random  # おみくじで使用
from oauth2client.service_account import ServiceAccountCredentials
import numpy as np
import pandas as pd
import datetime
import os
import urllib.request, urllib.error
import requests
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
from datetime import timedelta
import cv2
import io
from PIL import Image

bot_token = os.environ['DISCORD_BOT_TOKEN']
client = discord.Client()  # 接続に使用するオブジェクト

def uranai(url):
	html = urllib.request.urlopen(url)
	soup = BeautifulSoup(html)
	df2 = pd.DataFrame(soup.find_all("a"))
	bbbb = str(df2[0][17])
	bbb = bbbb.split("「")
	bbb = bbb[1].split("」")
	bbb = bbb[0]

	df = pd.DataFrame(soup.find_all("td"))
	test = str(df[0][1])
	n = test.split("=")

	df4 = pd.DataFrame(soup.find_all("p"))
	test2 = str(df4[0][4])
	mm = test2.split("=")
	mmm = mm[0].split(">")[1].split("<")[0].split("。")

	ccc = str(soup.find_all("meta")[7]).split("=")
	ddd = ccc[1][1:-10]

	list = []
	list.append(n[3].split(" ")[0][1:-1])
	list.append(n[6].split(" ")[0][1:-1])
	list.append(n[9].split(" ")[0][1:-1])
	list.append(n[12].split(" ")[0][1:-1])
	list.append(bbb)
	return list,ddd

@client.event
async def on_ready():
	"""起動時に通知してくれる処理"""
	print('ログインしました')
	print(client.user.name)  # ボットの名前
	print(client.user.id)  # ボットのID
	print(discord.__version__)  # discord.pyのバージョン
	print('------')

@client.event
async def on_message(message):
	"""メッセージを処理"""
	if message.author.bot:  # ボットのメッセージをハネる
		return

	elif message.attachments:
		headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",}
		request = urllib.request.Request(url=str(message.attachments[0].url),headers=headers)
		f = io.BytesIO(urllib.request.urlopen(request).read())

		validation_img = Image.open("horntale_necklace.png")
		validation_grayimg = validation_img.convert('L')
		validation_array = np.asarray(validation_grayimg)

		img = Image.open(f)
		grayimg = img.convert('L')
		input_array = np.asarray(grayimg)
		custom_cascade = cv2.CascadeClassifier('cascade.xml')
		custom_rect = custom_cascade.detectMultiScale(grayimg, scaleFactor=1.07, minNeighbors=2, minSize=(1, 1))

		#match_result = cv2.matchTemplate(input_array,validation_array,cv2.TM_CCOEFF_NORMED)
		#threshold = 0.5
		#loc=np.where(match_result >= threshold)

		if len(custom_rect) == 0:
			return
		else: await message.channel.send(f"あるよ")

	elif message.content == "('o')ｷｬｧｧｧｧｧｧｧｧｧｧｧｧｧｧｧｧｧｧwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww":
		await message.channel.send(f"うるせえぞタピオカ")

	elif message.content == "!やるじゃん":
		await message.channel.send(f"ありがとう")

	elif message.content == "!えっち":
		await message.channel.send(f'きゃー！{message.author.mention}さんのえっち！！', file=discord.File("4ba65a1c.jpg"))

	elif message.content == "!ハンバーグ":
		await message.channel.send(f"ハンバアアアアアアアアアアアアアアアアアアアアアアアアアアアグ！！！！！！")

	elif message.content == "!やってないじゃん":
		await message.channel.send(f"ごめんなさい")

	elif message.content == "!あるよ":
		await message.channel.send(f'あるよ', file=discord.File("aruyo.jpg"))

	elif message.content == "!ゆきやこんこ":
		await message.channel.send(f"⛄雪や⛄\n\n❄❅❆❄❅❆❄❅❆❄\n▉▉▉ ◥◣　　 ▉▉▉ \n　　▉ 　　◢◤ 　　▉ \n▉▉▉ ◢▉◤　 ▉▉▉ \n❄❅❆❄❅❆❄❅❆❄\n\n🚽ケツから🚽\n\n💩💩💩💩💩💩💩💩\n　▉\n▉▉▉▉◥◣　　▉▉▉\n▉　◢◤　　◢◤　　▉\n　◢◤　◢▉◤　▉▉▉\n💩💩💩💩💩💩💩💩")

	elif message.content == "juruli":
		await message.channel.send(f"そのキャラはキャラデリしました")

	elif message.content == "!おみくじ":
		# Embedを使ったメッセージ送信 と ランダムで要素を選択
		embed = discord.Embed(title="おみくじ", description=f"{message.author.mention}さんの今日の運勢は！",color=0x2ECC69)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="[運勢] ", value=random.choice(('大吉', '吉', '凶', '大凶')), inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!ダイス":
		embed = discord.Embed(title="ダイス", description=f"{message.author.mention}さんの結果",color=0x2ECC69)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="[結果] ", value=random.randint(0,100), inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!ダイレクトメッセージ":
		# ダイレクトメッセージ送信
		dm = await message.author.create_dm()
		await dm.send(f"{message.author.mention}さんにダイレクトメッセージ")

	elif message.content == "!おひつじ座":
		url = "https://fortune.yahoo.co.jp/12astro/aries"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!おうし座":
		url = "https://fortune.yahoo.co.jp/12astro/taurus"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!ふたご座":
		url = "https://fortune.yahoo.co.jp/12astro/gemini"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!かに座":
		url = "https://fortune.yahoo.co.jp/12astro/cancer"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!しし座":
		url = "https://fortune.yahoo.co.jp/12astro/leo"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!おとめ座":
		url = "https://fortune.yahoo.co.jp/12astro/virgo"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!てんびん座":
		url = "https://fortune.yahoo.co.jp/12astro/libra"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!さそり座":
		url = "https://fortune.yahoo.co.jp/12astro/scorpio"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!いて座":
		url = "https://fortune.yahoo.co.jp/12astro/sagittarius"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!やぎ座":
		url = "https://fortune.yahoo.co.jp/12astro/capricorn"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!みずがめ座":
		url = "https://fortune.yahoo.co.jp/12astro/aquarius"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

	elif message.content == "!うお座":
		url = "https://fortune.yahoo.co.jp/12astro/pisces"
		kekka,ddd = uranai(url)
		embed = discord.Embed(title="星座占い", description=f"{message.author.mention}さんの今日の運勢は！",color=0x00FF00)
		embed.set_thumbnail(url=message.author.avatar_url)
		embed.add_field(name="総合運",value=kekka[0],inline=False)
		embed.add_field(name="恋愛運",value=kekka[1],inline=False)
		embed.add_field(name="金運",value=kekka[2],inline=False)
		embed.add_field(name="仕事運",value=kekka[3],inline=False)
		embed.add_field(name="コメント",value=kekka[4],inline=False)
		embed.add_field(name="====",value=ddd,inline=False)
		await message.channel.send(embed=embed)

client.run(bot_token)

max_stars_repo_name: himopantsu/monostripe

max_issues_repo_name: himopantsu/monostripe

max_forks_repo_name: himopantsu/monostripe


text:
# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.

"""Data loaders."""

from typing import Generator, Tuple

import jax
import numpy as onp
import tensorflow as tf
import tensorflow_datasets as tfds

SupervisedDataset = Tuple[onp.ndarray, onp.ndarray]
SupervisedDatasetGen = Generator[SupervisedDataset, None, None]

# Format: (img_mean, img_std)
_ALL_DS_STATS = {
    "cifar10": ((0.49, 0.48, 0.44), (0.2, 0.2, 0.2))
}

def load_dataset(
    split,
    batch_size,
    name = "cifar10"
):
  """Loads the dataset as a generator of batches."""
  # Do no data augmentation.
  name = name.lower()
  ds, dataset_info = tfds.load(name, split=split, as_supervised=True,
                               with_info=True)
  num_classes = dataset_info.features["label"].num_classes
  num_examples = dataset_info.splits[split].num_examples

  def img_to_float32(image, label):
    return tf.image.convert_image_dtype(image, tf.float32), label

  ds = ds.map(img_to_float32).cache()
  ds_stats = _ALL_DS_STATS[name]

  def img_normalize(image, label):
    """Normalize the image to zero mean and unit variance."""
    mean, std = ds_stats
    image -= tf.constant(mean, shape=[1, 1, 3], dtype=image.dtype)
    image /= tf.constant(std, shape=[1, 1, 3], dtype=image.dtype)
    return image, label

  ds = ds.map(img_normalize)
  if batch_size == -1:
    batch_size = num_examples
  ds = ds.batch(batch_size)
  return tfds.as_numpy(ds), num_classes, num_examples

def batch_split_axis(batch,
                     n_split):
  """Reshapes batch to have first axes size equal n_split."""
  x, y = batch
  n = x.shape[0]
  n_new = n / n_split
  assert n_new == int(n_new), (
      "First axis cannot be split: batch dimension was {} when "
      "n_split was {}.".format(x.shape[0], n_split))
  n_new = int(n_new)
  return tuple(arr.reshape([n_split, n_new, *arr.shape[1:]]) for arr in (x, y))

def make_ds_pmap_fullbatch(
    name = "cifar10",
    n_devices = None):
  """Make train and test sets sharded over batch dim."""

  train_set, n_classes, _ = load_dataset("train", -1, name)
  train_set = next(iter(train_set))

  test_set, _, _ = load_dataset("test", -1, name)
  test_set = next(iter(test_set))

  n_devices = n_devices or len(jax.local_devices())

  def pmap_ds(ds):
    return jax.pmap(lambda x: x)(batch_split_axis(ds, n_devices))
  train_set, test_set = tuple(pmap_ds(ds) for ds in (train_set, test_set))
  return train_set, test_set, n_classes

max_stars_repo_name: egonrian/google-research

max_issues_repo_name: Alfaxad/google-research

max_forks_repo_name: Alfaxad/google-research

reason: import numpy,import jax


text:
"""
Definitions for different labels present in Vicar files.

This is a collection for their values and types collected into enums.
The reader will try to convert all the SystemLabels into other label types where applicable.
"""
from enum import Enum
from typing import Type, Dict, List, Tuple, Optional, Set

import numpy as np

LABEL_ENCODING = 'ASCII'
LABEL_SEPARATOR = '='

class VicarEnum(Enum):
    """
    Some stuff for cross linking this stuff
    """

    @classmethod
    def has_value(cls, o) -> bool:
        return o in cls._value2member_map_

    @classmethod
    def map2member(cls, val: str):
        if val in cls._value2member_map_:
            return cls._value2member_map_[val]
        else:
            return None

    def __repr__(self):
        return f'{self.value}'

    def __str__(self):
        return self.__repr__()

class VicarMultiEnum(VicarEnum):
    """
    Continuing the abuse of enums
    """

    @classmethod
    def __fill_lookup(cls):
        if not hasattr(cls, '__lookup_filled__'):
            cls.__lookup_filled__ = True
            cls.__member_lookup__ = dict()
            for v, m in cls._value2member_map_.items():
                cls.__member_lookup__[v[0]] = m
        cls.__lookup_ok = True

    @classmethod
    def has_value(cls, value) -> bool:
        cls.__fill_lookup()
        return value in cls.__member_lookup__

    @classmethod
    def map2member(cls, value: str):
        cls.__fill_lookup()
        return cls.__member_lookup__[value]

    def __repr__(self):
        return f'{self.value[0]}'

class NumberFormat(VicarMultiEnum):
    """
    Vicar file number formats

    Values:
        0: Name
        1: Byte width
        2: Python struct unpack type
        3: Numpy datatype
    """
    BYTE = 'BYTE', 1, 'B', np.dtype('uint8')
    HALF = 'HALF', 2, 'h', np.dtype('int16')
    FULL = 'FULL', 4, 'i', np.dtype('int32')
    REAL = 'REAL', 4, 'f', np.dtype('single')
    DOUB = 'DOUB', 8, 'd', np.dtype('double')
    COMP = 'COMP', 8, 'f', np.dtype('complex64')  # .       SPECIAL re(f) + im(f)
    WORD = 'WORD', 2, 'h', np.dtype('int16')  # .           HALF Obsolete
    LONG = 'LONG', 4, 'i', np.dtype('int32')  # .           FULL Obsolete
    COMPLEX = 'COMPLEX', 8, 'f', np.dtype('complex64')  # . COMP, SPECIAL re(f) + im(f) Obsolete

INT_FORMATS: Set[NumberFormat] = {
    NumberFormat.BYTE,
    NumberFormat.HALF,
    NumberFormat.FULL,
    NumberFormat.WORD,
    NumberFormat.LONG
}

FLOAT_FORMATS: Set[NumberFormat] = {
    NumberFormat.REAL,
    NumberFormat.DOUB,
    NumberFormat.COMP,
    NumberFormat.COMPLEX
}

def isIntFormat(fmt: NumberFormat) -> bool:
    """
    Checks whether the provided NumberFormat is an IntFormat
    """
    return fmt in INT_FORMATS

def isRealFormat(fmt: NumberFormat) -> bool:
    """
    Checks whether the provided NumberFormat is a RealFormat
    """
    return fmt in FLOAT_FORMATS

def isFloatFormat(fmt: NumberFormat) -> bool:
    """
    Alias for isRealFormat
    """
    return fmt in FLOAT_FORMATS

class DataType(VicarEnum):
    """
    Vicar file data types
    """
    IMAGE = 'IMAGE'
    PARAMS = 'PARAMS'  # .                      Very old
    PARM = 'PARM'  # .                          Old
    PARAM = 'PARAM'  # .                        Parameter file
    GRAPH1 = 'GRAPH1'  # .                      IBIS Graphics
    GRAPH2 = 'GRAPH2'
    GRAPH3 = 'GRAPH3'
    TABULAR = 'TABULAR'

class DataOrg(VicarEnum):
    """
    Data format inside the file

    Notes:
        N1 - Varies fastest, N3 - Slowest

    Different representations:
        BSQ - Band SeQuential (samples, lines, bands)
        BIL - Band Interleaved by Line (samples, bands, lines)
        BIP - Band Interleaved by Pixel (bands, samples, lines)

    Image(s):
        - B: bands (images)
        - L: lines
        - S: sample

    Provide data order and representation (B, L, S):
        BSQ - Image samples (S) for lines (L) in blocks, blocks are bands (images)
            => (0,0,0) -> (0,0,1) -> (0,0,2) --> (1,0,0)
        BIL - Image samples (S) for bands in blocks, blocks are lines (L)
            => (0,0,0) -> (0,0,1) -> (0,0,2) --> (0,1,0)
        BIP - Image bands for samples (S) in blocks, blocks are lines (L)
            => (0,0,0) -> (1,0,0) -> (2,0,0) --> (0,1,0)

    Numpy data array (B, L, S)
    """
    BSQ = 'BSQ'  # .                            N1 samples, N2 lines, N3 bands
    BIL = 'BIL'  # .                            N1 samples, N2 bands, N3 lines
    BIP = 'BIP'  # .                            N1 bands, N2 samples, N3 lines

class HostType(VicarEnum):
    """
    Host types, not really useful, prefer strings over these as not all are known
    """
    ALLIANT = 'ALLIANT'
    CRAY = 'CRAY'  # .                          Unimplemented standard
    DECSTATN = 'DECSTATN'
    HP700 = 'HP-700'
    MACAUX = 'MAC-AUX'
    MACMPW = 'MAC-MPW'
    SGI = 'SGI'
    SUN3 = 'SUN-3'
    SUN4 = 'SUN-4'
    TEK = 'TEK'
    VAS = 'VAX-VMS'
    PC = 'PC_X86_64'

class IntFormat(VicarMultiEnum):
    """
    Integer Endian settings in data
    """
    HIGH = 'HIGH', '>'  # .                     high first Big-Endian
    LOW = 'LOW', '<'  # .                       low first Little-Endian

class RealFormat(VicarMultiEnum):
    """
    Float bit order in data

    VAX (and VAX-VMS) is unimplemented
    """
    IEEE = 'IEEE', '>'  # .                     IEEE 754 high first
    RIEEE = 'RIEEE', '<'  # .                   Reverse, documentation says only on DECSTATN
    VAX = 'VAX', ''  # .                        VAX format, double VAX D, only VAX-VMS NOT IMPLEMENTED

class SystemLabel(VicarMultiEnum):
    """
    System labels that define Vicar file properties
    """
    LBLSIZE = 'LBLSIZE', int  # .               Multiple of RECSIZE                 MANDATORY
    FORMAT = 'FORMAT', NumberFormat  # .        Format of data                      MANDATORY
    TYPE = 'TYPE', DataType  # .                Data type                           default IMAGE
    BUFSIZ = 'BUFSIZ', int  # .                 Buffer size              (obsolete) default RECSIZE
    DIM = 'DIM', int  # .                       Data dimensions  (2 in old, but =3) SET 3
    EOL = 'EOL', int  # .                       0, 1 End of file labels             default 0
    RECSIZE = 'RECSIZE', int  # .               Record size     (NBB + N1 * FORMAT) MANDATORY
    ORG = 'ORG', DataOrg  # .                   Data organization                   default BSQ
    NL = 'NL', int  # .                         Number of lines                     MANDATORY
    NS = 'NS', int  # .                         Number of samples                   MANDATORY
    NB = 'NB', int  # .                         Number of bands                     MANDATORY
    N1 = 'N1', int  # .                         Fastest dim                         default max(NS,NB)
    N2 = 'N2', int  # .                         Mid dim                             default mid(NL,NS,NB)
    N3 = 'N3', int  # .                         Slowest dim                         default min(NL,NB)
    N4 = 'N4', int  # .                         Not implemented in standard         SET 0
    NBB = 'NBB', int  # .                       Binary prefix                       default 0
    NLB = 'NLB', int  # .                       Binary header                       default 0
    HOST = 'HOST', HostType  # .                Host                         (doc)  default VAX-VMS
    INTFMT = 'INTFMT', IntFormat  # .           Integer format                      default LOW
    REALFMT = 'REALFMT', RealFormat  # .        Float format                        default VAX
    BHOST = 'BHOST', HostType  # .              Binary host                  (doc)  default VAX-VMS
    BINTFMT = 'BINTFMT', IntFormat  # .         Binary Int format                   default LOW
    BREALFMT = 'BREALFMT', RealFormat  # .      Binary Float format                 default VAX
    BLTYPE = 'BLTYPE', str  # .                 Binary label/prefix host     (doc)  default \x00

    @staticmethod
    def fill_dims(labels: Dict):
        """
        Fills alternate dimension according to standard
        """
        org: Optional[DataOrg] = None
        try:
            org = labels[SystemLabel.ORG]
        except KeyError:
            labels[SystemLabel.ORG] = DataOrg.BSQ
        defaults: Tuple[SystemLabel, SystemLabel, SystemLabel]
        if org == DataOrg.BIL:
            defaults = (SystemLabel.NS, SystemLabel.NB, SystemLabel.NL)
        elif org == DataOrg.BIP:
            defaults = (SystemLabel.NB, SystemLabel.NS, SystemLabel.NL)
        else:
            defaults = (SystemLabel.NS, SystemLabel.NL, SystemLabel.NB)
        for i, m in enumerate((SystemLabel.N1, SystemLabel.N2, SystemLabel.N3)):
            if m not in labels:
                labels[m] = defaults[i]
        if SystemLabel.N4 not in labels:
            labels[SystemLabel.N4] = 0
        if SystemLabel.NBB not in labels:
            labels[SystemLabel.NBB] = 0
        if SystemLabel.NLB not in labels:
            labels[SystemLabel.NLB] = 0

class SpecialLabel(VicarEnum):
    """
    Markers for special sections in file labels
    """
    PROPERTY = 'PROPERTY'
    TASK = 'TASK'

SYSTEM_CLASS_LIST: List[Type[VicarEnum]] = [
    NumberFormat,
    DataType,
    DataOrg,
    IntFormat,
    RealFormat,
    SystemLabel
]

def getSystemTypes() -> List[Type[VicarEnum]]:
    return SYSTEM_CLASS_LIST

max_stars_repo_name: joniumGit/moons

max_issues_repo_name: joniumGit/moons

max_forks_repo_name: joniumGit/moons


text:
from .functions import *
from .functions import chain as C, flat as F, mapper as M

import tsfresh.feature_extraction.feature_calculators as fc
import numpy as np

FEATURES = {
    'fuse_belt_accel': {
        'inputs': [
            'sens_belt_accel_amb',
            'sens_belt_accel_egw',
            'sens_belt_accel_app'
        ],
        'function': flat_fuse,
        'window': '',
    },

    'feat_belt_accel_magnitude': {
        'inputs': [
            'fuse_belt_accel',
        ],
       'function': magnitude,
       'window': ''
    },

    'feat_mobility_label_mode': {
        'inputs': [
            'feat_mobility_label'
        ],
        'function': mode,
        'window': '5s',
        'default': 'NO_LABEL'
    },

    'feat_belt_accel_magnitude_approx_entropy_2_0.5': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.approximate_entropy, 2, 0.5)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_change_quantiles__f_agg_mean__isabs_True__qh_0.8__ql_0.2': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.change_quantiles, f_agg="mean", isabs=True, qh=0.8, ql=0.2)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_0_abs_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"abs", "coeff":0}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_6_abs_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"abs", "coeff":6}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_11_abs_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"abs", "coeff":11}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_0_real_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"real", "coeff":0}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_12_abs_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"abs", "coeff":12}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_fft_1_imag_attr': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.fft_coefficient, [{"attr":"imag", "coeff":1}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_False__qh_1.0__ql_0.8': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.change_quantiles, f_agg="var", isabs=False, qh=1.0, ql=0.8)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_True__qh_0.8__ql_0.6': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.change_quantiles, f_agg="var", isabs=True, qh=0.8, ql=0.6)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_change_quantiles_f_agg_mean__isabs_True__qh_0.8__ql_0.2': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.change_quantiles, f_agg="mean", isabs=True, qh=0.8, ql=0.2)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_True__qh_0.6__ql_0.2': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.change_quantiles, f_agg="var", isabs=True, qh=0.6, ql=0.2)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_maximum': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.maximum)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_approx_entropy_2_0.7': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.approximate_entropy, 2, 0.7)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_approx_entropy_2_0.3': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.approximate_entropy, 2, 0.3)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_approx_entropy_2_0.9': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.approximate_entropy, 2, 0.9)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_longest_strike_below_mean': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.longest_strike_below_mean)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_autocorrelation__lag_1': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.autocorrelation, 1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_10__attr_stderr': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.agg_linear_trend, [{'attr': 'stderr', 'chunk_len': 10, 'f_agg': 'max'}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_spkt_welch_density__coeff_2': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.spkt_welch_density, [{"coeff":2}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_partial_autocorrelation__lag_1': {
        'inputs': [
            'feat_belt_accel_magnitude'
        ],
        'function': C(slicer('5s'), M(fc.partial_autocorrelation, [{"lag":1}], default=((0, 0))), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_agg_linear_trend__f_agg_mean__chunk_len_50__attr_stderr': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.agg_linear_trend, [{'attr': 'stderr', 'chunk_len': 50, 'f_agg': 'mean'}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_5__attr_stderr': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.agg_linear_trend, [{'attr': 'stderr', 'chunk_len': 5, 'f_agg': 'max'}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_skewness': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.skewness)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_50__attr_stderr': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.agg_linear_trend, [{'attr': 'stderr', 'chunk_len': 50, 'f_agg': 'max'}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_number_crossing_m__m_1': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.number_crossing_m, 1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_number_peaks__n_1': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.number_peaks, 1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__c3__lag_2': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.c3, 2)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__quantile__q_0.3': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.quantile, 0.3)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__range_count__max_1__min_-1': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.range_count, -1, 1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__sum_values': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(sum)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__linear_trend__attr_intercept': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.linear_trend, [{"attr":"intercept"}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__abs_energy': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.abs_energy)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__quantile__q_0.6': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.quantile, 0.6)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__median': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.median)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__quantile__q_0.1': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.quantile, 0.1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__quantile__q_0.2': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.quantile, 0.2)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__quantile__q_0.4': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.quantile, 0.4)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__mean': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.mean)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__c3__lag_3': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.c3, 3)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude__c3__lag_1': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(fc.c3, 1)),
        'window': '',
        'default': 0
    },

    'feat_belt_accel_magnitude_agg_linear_trend__f_agg_mean__chunk_len_50__attr_intercept': {
        'inputs': [
            'feat_belt_accel_magnitude',
        ],
        'function': C(slicer('5s'), M(np.array), M(fc.agg_linear_trend, [{'attr': 'intercept', 'chunk_len': 50, 'f_agg': 'mean'}]), extract_component(0), extract_component(1)),
        'window': '',
        'default': 0
    },

    'feat_mobility_activity': {
        'inputs': [
            'feat_belt_accel_magnitude__abs_energy',
            'feat_belt_accel_magnitude__c3__lag_1',
            'feat_belt_accel_magnitude__c3__lag_2',
            'feat_belt_accel_magnitude__c3__lag_3',
            'feat_belt_accel_magnitude__linear_trend__attr_intercept',
            'feat_belt_accel_magnitude__mean',
            'feat_belt_accel_magnitude__median',
            'feat_belt_accel_magnitude__quantile__q_0.1',
            'feat_belt_accel_magnitude__quantile__q_0.2',
            'feat_belt_accel_magnitude__quantile__q_0.3',
            'feat_belt_accel_magnitude__quantile__q_0.4',
            'feat_belt_accel_magnitude__quantile__q_0.6',
            'feat_belt_accel_magnitude__range_count__max_1__min_-1',
            'feat_belt_accel_magnitude__sum_values',
            'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_10__attr_stderr',
            'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_50__attr_stderr',
            'feat_belt_accel_magnitude_agg_linear_trend__f_agg_max__chunk_len_5__attr_stderr',
            'feat_belt_accel_magnitude_agg_linear_trend__f_agg_mean__chunk_len_50__attr_intercept',
            'feat_belt_accel_magnitude_agg_linear_trend__f_agg_mean__chunk_len_50__attr_stderr',
            'feat_belt_accel_magnitude_approx_entropy_2_0.3',
            'feat_belt_accel_magnitude_approx_entropy_2_0.5',
            'feat_belt_accel_magnitude_approx_entropy_2_0.7',
            'feat_belt_accel_magnitude_approx_entropy_2_0.9',
            'feat_belt_accel_magnitude_autocorrelation__lag_1',
            'feat_belt_accel_magnitude_change_quantiles__f_agg_mean__isabs_True__qh_0.8__ql_0.2',
            'feat_belt_accel_magnitude_change_quantiles_f_agg_mean__isabs_True__qh_0.8__ql_0.2',
            'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_False__qh_1.0__ql_0.8',
            'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_True__qh_0.6__ql_0.2',
            'feat_belt_accel_magnitude_change_quantiles_f_agg_var__isabs_True__qh_0.8__ql_0.6',
            'feat_belt_accel_magnitude_fft_0_abs_attr',
            'feat_belt_accel_magnitude_fft_0_real_attr',
            'feat_belt_accel_magnitude_fft_11_abs_attr',
            'feat_belt_accel_magnitude_fft_12_abs_attr',
            'feat_belt_accel_magnitude_fft_1_imag_attr',
            'feat_belt_accel_magnitude_fft_6_abs_attr',
            'feat_belt_accel_magnitude_longest_strike_below_mean',
            'feat_belt_accel_magnitude_maximum',
            'feat_belt_accel_magnitude_number_crossing_m__m_1',
            'feat_belt_accel_magnitude_number_peaks__n_1',
            'feat_belt_accel_magnitude_partial_autocorrelation__lag_1',
            'feat_belt_accel_magnitude_skewness',
            'feat_belt_accel_magnitude_spkt_welch_density__coeff_2',
        ],
         'function': run_ml_model('test_clf_walk-still'),
         'window': '',
         'store': True
    },

    'feat_mobility_activity_current': {
        'inputs': [
            'feat_mobility_activity'
        ],
        'window': '',
        'function': alias
    },

    # mobility_stand_up pipeline
    'feat_mobility_stand_up_count': {
        'inputs': [
        ],
        'function': constant('medium'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_stand_up_mode': {
        'inputs': [
        ],
        'function': constant('no_hands'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_stand_up_time': {
        'inputs': [
        ],
        'function': constant('fast'),
        'window': '24h',
        'store': True
    },

    'situ_mobility_stand_up_predicted': {
        'inputs': [
        ],
        'function': constant('high'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_stand_up_count_weekly': {
        'inputs': [
        ],
        'function': constant(0),
        'window': '24h',
        'store': True
    },

    # mobility_standing pipeline
    'feat_mobility_standing_episode_length': {
        'inputs': [
        ],
        'function': constant('long'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_standing_mode': {
        'inputs': [
        ],
        'function': constant('no_aids'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_standing_daily_duration': {
        'inputs': [
        ],
        'function': constant('high'),
        'window': '24h',
        'store': True
    },

    'situ_mobility_standing_predicted': {
        'inputs': [
        ],
        'function': constant('high'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_standing_duration_weekly': {
        'inputs': [
        ],
        'function': constant(0),
        'window': '24h',
        'store': True
    },

    # mobility_walking pipeline
    'feat_mobility_walking_longest_episode': {
        'inputs': [
            'feat_mobility_activity'
        ],
        'function': longest_walking_episode,
        'window': '24h',
        'store': True
    },

    'feat_mobility_walking_episode_distance_qualitative': {
        'inputs': [
            'feat_mobility_walking_longest_episode'
        ],
        'function': walking_episode_qualitative,
        'window': '24h',
        'default': 'normal_or_high',
        'store': True
    },

    'feat_mobility_walking_daily_distance_qualitative': {
        'inputs': [
            'feat_mobility_walking_duration_qualitative'
        ],
        'function': alias,
        'window': '24h',
        'default': 'medium',
        'store': True
    },

    'feat_mobility_walking_duration': {
        'inputs': [
            'feat_mobility_activity'
        ],
        'function': cumulative_walking_time,
        'window': '24h',
        'store': True
    },

    'feat_mobility_walking_duration_qualitative': {
        'inputs': [
            'feat_mobility_walking_duration'
        ],
        'function': cumulative_walking_time_qualitative,
        'window': '24h',
        'default': 'medium',
        'store': True
    },

    'feat_mobility_walking_mode': {
        'inputs': [
        ],
        'function': constant('mostly_unaided'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_walking_instabilities_count': {
        'inputs': [
        ],
        'function': constant('some'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_walking_falls_count': {
        'inputs': [
        ],
        'function': constant('some'),
        'window': '24h',
        'store': True
    },

    'situ_mobility_walking_predicted': {
        'inputs': [
            ('situ_mobility_walking', '4d')
        ],
        'function': mode,
        'window': '24h',
        'default': 'medium',
        'store': True
    },

    'feat_mobility_walking_step_count_weekly': {
        'inputs': [
        ],
        'function': constant(0),
        'window': '24h',
        'store': True
    },

    'feat_mobility_walking_distance_weekly': {
        'inputs': [
        ],
        'function': constant(0),
        'window': '24h',
        'store': True
    },

    # mobility_use_stairs pipeline
    'feat_mobility_use_stairs_mode': {
        'inputs': [
        ],
        'function': constant('mostly_unaided'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_use_stairs_count': {
        'inputs': [
        ],
        'function': constant('many'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_use_stairs_time_per': {
        'inputs': [
        ],
        'function': constant('short'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_use_stairs_rest_count': {
        'inputs': [
        ],
        'function': constant('few'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_use_stairs_rest_duration': {
        'inputs': [
        ],
        'function': constant('short'),
        'window': '24h',
        'store': True
    },

    'situ_mobility_use_stairs_predicted': {
        'inputs': [
        ],
        'function': constant('high'),
        'window': '24h',
        'store': True
    },

    'feat_mobility_use_stairs_count_weekly': {
        'inputs': [
        ],
        'function': constant(0),
        'window': '24h',
        'store': True
    },

    # mobility_instructed_walking
    'feat_walking_longest_episode_length': {
        'inputs': [
            ('feat_mobility_activity', '24h')
        ],
        'function': C(midnight_filter, longest_walking_episode),
        'window': '4h',
        'default': 0,
        'store': True
    },

    'feat_walking_longest_episode_exceeded': {
        'inputs': [
            'feat_walking_longest_episode_length',
            'feat_mobility_activity_current'
        ],
        'function': longest_walking_episode_vs_prescribed,
        'window': '4h',
        'store': True
    },

    'feat_walking_time_cumulative': {
        'inputs': [
            ('feat_mobility_activity', '24h')
        ],
        'function': C(midnight_filter, cumulative_walking_time),
        'window': '4h',
        'default': 0,
        'store': True
    },

    'feat_walking_time_threshold_reached': {
        'inputs': [
            'feat_walking_time_cumulative',
            'feat_mobility_activity_current'
        ],
        'function': cumulative_walking_time_vs_prescribed,
        'window': '4h',
        'store': True
    }
}


text:
#!/usr/bin/env python

VERSION = '0.9'

import os
import sys
from glob import glob

import numpy as np

def configuration(parent_package='', top_path=None):
    if os.path.exists('MANIFEST'): os.remove('MANIFEST')

    from numpy.distutils.misc_util import Configuration
    config = Configuration(None, parent_package, top_path)

    config.set_options(
            ignore_setup_xxx_py=True,
            assume_default_configuration=True,
            delegate_options_to_subpackages=True,
            quiet=True)

    config.add_subpackage('supreme')

    return config

if __name__ == '__main__':
    from numpy.distutils.core import setup

    setup(
        name = 'supreme',
        version = VERSION,
        configuration = configuration,

        package_data = {
            '': ['*.txt', '*.png', '*.jpg', '*.pgm'],
        },

        author = "Stefan van der Walt",
        author_email = "<stefan.no-spam(at)mentat.za.net>",
        description = "SUper REsolution MEthods",
        url = "http://mentat.za.net",
        license = "BSD3",
    )

max_stars_repo_name: KirillDZR/supreme

max_issues_repo_name: KirillDZR/supreme

max_forks_repo_name: KirillDZR/supreme

reason: import numpy,from numpy


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

from __future__ import print_function
import community_ext
import networkx as nx
import scipy

# optimization routine
def opt_par(G,method,par):
    def opt_fn(work_par):
        work_par = work_par[0]
        if method in ('ilfrs','ilfr'):
            partition = community_ext.best_partition(G,model=method,pars={'mu':work_par})
            loglike =   community_ext.model_log_likelihood(G,partition,model=method,pars={'mu':work_par})
        else:
            partition = community_ext.best_partition(G,model=method,pars={'gamma':work_par})
            loglike =   community_ext.model_log_likelihood(G,partition,model=method,pars={'gamma':work_par})
        return -loglike

    # find the optimal parameter value
    best_par = float(scipy.optimize.fmin_powell(opt_fn, par, full_output = False,disp = False))

    # calculate the partition with the optimal parameter value
    if method in ('ilfrs','ilfr'):
        partition = community_ext.best_partition(G,model=method,pars={'mu':best_par})
        loglike =   community_ext.model_log_likelihood(G,partition,model=method,pars={'mu':best_par})
    else:
        partition = community_ext.best_partition(G,model=method,pars={'gamma':best_par})
        loglike =   community_ext.model_log_likelihood(G,partition,model=method,pars={'gamma':best_par})
    return partition, loglike, best_par

fn1 = "datasets/polblogs/polblogs.edges"
fn2 = fn1.replace(".edges",".clusters")
print("DATASET:",fn1)

# load graph
G = nx.Graph()
for line in open(fn1):
    from_node, to_node = map(int, line.rstrip().split("\t"))
    if from_node not in G or to_node not in G[from_node]:
        G.add_edge(from_node,to_node)

# load the ground-truth partition
groundtruth_partition = dict()
for line in open(fn2):
    node, cluster = map(int, line.rstrip().split("\t"))
    if node not in G.nodes(): continue
    groundtruth_partition[node] = cluster

# print some general info
gt_mu = community_ext.estimate_mu(G,groundtruth_partition)
print("ground truth mu\t",gt_mu)
print("ground truth clusters\t",len(set(groundtruth_partition.values())))
print("ground truth modularity\t", community_ext.modularity(groundtruth_partition,G))

# now cycle thru the methods and optimize with each one
methods = ('ppm','dcppm','ilfrs','ilfr')

for method in methods:
    print('\nMethod', method)
    # a starting parameter value depends on the method
    if method in ('ilfrs','ilfr'):
        work_par = 0.5
    else:
        work_par = 1.

    partition, loglike, best_par = opt_par(G,method,work_par)
    # calculate and print the scores of resulting partition
    part_scores = community_ext.compare_partitions(groundtruth_partition,partition)
    print('best par',best_par)
    community_ext.print_scores(part_scores, partition, loglike)

reason: import scipy,import networkx


text:
import unittest

import numpy as np

import dimod

import dwave.embedding

class TestBrokenChains(unittest.TestCase):
    def test_broken_chains_typical(self):
        S = np.array([[-1, 1, -1, 1],
                      [1, 1, -1, -1],
                      [-1, 1, -1, -1]])
        chains = [[0, 1], [2, 3]]

        broken = dwave.embedding.broken_chains(S, chains)

        np.testing.assert_array_equal([[1, 1], [0, 0], [1, 0]], broken)

    def test_broken_chains_chains_length_0(self):
        S = np.array([[-1, 1, -1, 1],
                      [1, 1, -1, -1],
                      [-1, 1, -1, -1]])
        chains = [[0, 1], [], [2, 3]]

        broken = dwave.embedding.broken_chains(S, chains)

        np.testing.assert_array_equal([[1, 0, 1], [0, 0, 0], [1, 0, 0]], broken)

    def test_broken_chains_single_sample(self):
        S = [-1, 1, 1, 1]
        chains = [[0, 1], [2, 3]]
        with self.assertRaises(ValueError):
            dwave.embedding.broken_chains(S, chains)

    def test_matrix(self):
        samples_matrix = np.array([[-1, +1, -1, +1],
                                   [+1, +1, +1, +1],
                                   [-1, -1, +1, -1],
                                   [-1, -1, +1, +1]], dtype='int8')
        chain_list = [(0, 1), (2, 3)]

        broken = dwave.embedding.broken_chains(samples_matrix, chain_list)

class TestDiscard(unittest.TestCase):
    def test_discard_no_breaks_all_ones_identity_embedding(self):

        samples_matrix = np.array(np.ones((100, 50)), dtype='int8')
        chain_list = [[idx] for idx in range(50)]

        new_matrix, idxs = dwave.embedding.discard(samples_matrix, chain_list)

        np.testing.assert_equal(new_matrix, samples_matrix)

    def test_discard_no_breaks_all_ones_one_var_embedding(self):

        samples_matrix = np.array(np.ones((100, 50)), dtype='int8')
        chain_list = [[idx for idx in range(50)]]

        new_matrix, idxs = dwave.embedding.discard(samples_matrix, chain_list)

        self.assertEqual(new_matrix.shape, (100, 1))

    def test_discard_typical(self):

        samples_matrix = np.array([[-1, +1, -1, +1],
                                   [+1, +1, +1, +1],
                                   [-1, -1, +1, -1],
                                   [-1, -1, +1, +1]], dtype='int8')
        chain_list = [(0, 1), (2, 3)]

        new_matrix, idxs = dwave.embedding.discard(samples_matrix, chain_list)

        np.testing.assert_equal(new_matrix, [[+1, +1],
                                             [-1, +1]])

    def test_mixed_chain_types(self):
        chains = [(0, 1), [2, 3], {4, 5}]
        samples = [[1, 1, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1]]
        unembedded, idx = dwave.embedding.discard(samples, chains)

        np.testing.assert_array_equal(unembedded, [[1, 1, 1]])
        np.testing.assert_array_equal(idx, [0])

class TestMajorityVote(unittest.TestCase):
    def test_typical_spin(self):
        S = np.array([[-1, +1, -1, +1],
                      [+1, +1, -1, +1],
                      [-1, +1, -1, -1]])
        chains = [[0, 1, 2], [3]]

        samples, idx = dwave.embedding.majority_vote(S, chains)

        np.testing.assert_equal(samples, [[-1, +1],
                                          [+1, +1],
                                          [-1, -1]])

    def test_typical_binary(self):
        S = np.array([[0, 1, 0, 1],
                      [1, 1, 0, 1],
                      [0, 1, 0, 0]])
        chains = [[0, 1, 2], [3]]

        samples, idx = dwave.embedding.majority_vote(S, chains)

        np.testing.assert_equal(samples, [[0, 1],
                                          [1, 1],
                                          [0, 0]])

    def test_four_chains(self):
        S = [[-1, -1, -1, -1],
             [+1, -1, -1, -1],
             [+1, +1, -1, -1],
             [-1, +1, -1, -1],
             [-1, +1, +1, -1],
             [+1, +1, +1, -1],
             [+1, -1, +1, -1],
             [-1, -1, +1, -1],
             [-1, -1, +1, +1],
             [+1, -1, +1, +1],
             [+1, +1, +1, +1],
             [-1, +1, +1, +1],
             [-1, +1, -1, +1],
             [+1, +1, -1, +1],
             [+1, -1, -1, +1],
             [-1, -1, -1, +1]]
        chains = [[0], [1], [2, 3]]

        samples, idx = dwave.embedding.majority_vote(S, chains)

        self.assertEqual(samples.shape, (16, 3))
        self.assertEqual(set().union(*samples), {-1, 1})  # should be spin-valued

class TestMinimizeEnergy(unittest.TestCase):
    def test_minimize_energy(self):
        embedding = {0: (0, 5), 1: (1, 6), 2: (2, 7), 3: (3, 8), 4: (4, 10)}
        h = []
        j = {(0, 1): -1, (0, 2): 2, (0, 3): 2, (0, 4): -1,
             (2, 1): -1, (1, 3): 2, (3, 1): -1, (1, 4): -1,
             (2, 3): 1, (4, 2): -1, (2, 4): -1, (3, 4): 1}

        bqm = dimod.BinaryQuadraticModel.from_ising(h, j)

        solutions = [
            [-1, -1, -1, -1, -1, -1, +1, +1, +1, 3, +1],
            [+1, +1, +1, +1, +1, -1, +1, -1, -1, 3, -1],
            [+1, +1, -1, +1, -1, -1, -1, -1, -1, 3, -1]
        ]
        expected = [
            [-1, -1, +1, +1, -1],
            [+1, +1, +1, -1, +1],
            [-1, -1, -1, +1, -1]
        ]

        cbm = dwave.embedding.MinimizeEnergy(bqm, embedding)

        unembedded, idx = cbm(solutions, [embedding[v] for v in range(5)])

        np.testing.assert_array_equal(expected, unembedded)

    def test_minimize_energy_non_clique(self):
        embedding = {0: (0, 5), 1: (1, 6), 2: (2, 7), 3: (3, 8), 4: (4, 10)}
        h = []
        j = {(0, 1): -1, (0, 2): 2, (0, 3): 2, (0, 4): -1,
             (1, 3): 2, (3, 1): -1, (1, 4): -1,
             (2, 3): 1, (4, 2): -1, (2, 4): -1, (3, 4): 1}

        bqm = dimod.BinaryQuadraticModel.from_ising(h, j)

        solutions = [
            [-1, -1, -1, -1, -1, -1, +1, +1, +1, 3, +1],
            [+1, +1, +1, +1, +1, -1, +1, -1, -1, 3, -1],
            [+1, +1, -1, +1, -1, -1, -1, -1, -1, 3, -1]
        ]

        expected = [
            [-1, -1, +1, +1, -1],
            [+1, +1, +1, -1, +1],
            [-1, -1, -1, +1, -1]
        ]

        cbm = dwave.embedding.MinimizeEnergy(bqm, embedding)
        unembedded, idx = cbm(solutions, [embedding[v] for v in range(5)])

        np.testing.assert_array_equal(expected, unembedded)

    def test_minimize_energy_easy(self):
        chains = ({0, 1}, [2], (4, 5, 6))
        embedding = {v: chain for v, chain in enumerate(chains)}
        h = [-1, 0, 0]
        j = {}
        bqm = dimod.BinaryQuadraticModel.from_ising(h, j)
        solutions = [
            [-1, -1, +1, 3, -1, -1, -1],
            [-1, +1, -1, 3, +1, +1, +1]
        ]
        expected = [
            [-1, +1, -1],
            [+1, -1, +1]
        ]
        cbm = dwave.embedding.MinimizeEnergy(bqm, embedding)

        unembedded, idx = cbm(solutions, chains)

        np.testing.assert_array_equal(expected, unembedded)

    def test_empty_matrix(self):
        chains = []
        bqm = dimod.BinaryQuadraticModel.empty(dimod.BINARY)
        solutions = [[]]
        embedding = {}
        cbm = dwave.embedding.MinimizeEnergy(bqm, embedding)
        unembedded, idx = cbm(solutions, chains)

        np.testing.assert_array_equal([[]], unembedded)
        np.testing.assert_array_equal(idx, [0])

    def test_empty_chains(self):
        embedding = {}
        h = []
        j = {(0, 1): -1, (0, 2): 2, (0, 3): 2, (0, 4): -1,
             (2, 1): -1, (1, 3): 2, (3, 1): -1, (1, 4): -1,
             (2, 3): 1, (4, 2): -1, (2, 4): -1, (3, 4): 1}

        bqm = dimod.BinaryQuadraticModel.from_ising(h, j)

        solutions = [
            [-1, -1, -1, -1, -1, -1, +1, +1, +1, 3, +1],
            [+1, +1, +1, +1, +1, -1, +1, -1, -1, 3, -1],
            [+1, +1, -1, +1, -1, -1, -1, -1, -1, 3, -1]
        ]
        expected = [
            [-1, -1, +1, +1, -1],
            [+1, +1, +1, -1, +1],
            [-1, -1, -1, +1, -1]
        ]

        cbm = dwave.embedding.MinimizeEnergy(bqm, embedding)

        unembedded, idx = cbm(solutions, [])

        np.testing.assert_array_equal(unembedded, [[], [], []])

max_stars_repo_name: joseppinilla/dwave-system

max_issues_repo_name: davage/dwave-system

max_forks_repo_name: davage/dwave-system


text:
'''
Singer Voice Separator RNN

Lei Mao
University of Chicago
'''

import tensorflow as tf
import numpy as np
import os
import shutil
from datetime import datetime

class SVSRNN(object):

    def __init__(self, num_features, num_rnn_layer = 3, num_hidden_units = [256, 256, 256], tensorboard_directory = 'graphs/svsrnn', clear_tensorboard = True):

        assert len(num_hidden_units) == num_rnn_layer

        self.num_features = num_features
        self.num_rnn_layer = num_rnn_layer
        self.num_hidden_units = num_hidden_units

        self.gstep = tf.Variable(0, dtype = tf.int32, trainable = False, name = 'global_step')
        self.learning_rate = tf.placeholder(tf.float32, shape = [], name = 'learning_rate')
        # The shape of x_mixed, y_src1, y_src2 are [batch_size, n_frames (time), n_frequencies]
        self.x_mixed = tf.placeholder(tf.float32, shape = [None, None, num_features], name = 'x_mixed')
        self.y_src1 = tf.placeholder(tf.float32, shape = [None, None, num_features], name = 'y_src1')
        self.y_src2 = tf.placeholder(tf.float32, shape = [None, None, num_features], name = 'y_src2')

        self.y_pred_src1, self.y_pred_src2 = self.network_initializer()

        # Loss balancing parameter
        self.gamma = 0.001
        self.loss = self.loss_initializer()
        self.optimizer = self.optimizer_initializer()

        self.saver = tf.train.Saver()
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        # Tensorboard summary
        if clear_tensorboard:
            shutil.rmtree(tensorboard_directory, ignore_errors = True)
            logdir = tensorboard_directory
        else:
            now = datetime.now()
            logdir = os.path.join(tensorboard_directory, now.strftime('%Y%m%d-%H%M%S'))
        self.writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
        self.summary_op = self.summary()

    def network(self):

        rnn_layer = [tf.nn.rnn_cell.GRUCell(size) for size in self.num_hidden_units]
        multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layer)
        outputs, state = tf.nn.dynamic_rnn(cell = multi_rnn_cell, inputs = self.x_mixed, dtype = tf.float32)
        y_hat_src1 = tf.layers.dense(
            inputs = outputs,
            units = self.num_features,
            activation = tf.nn.relu,
            name = 'y_hat_src1')
        y_hat_src2 = tf.layers.dense(
            inputs = outputs,
            units = self.num_features,
            activation = tf.nn.relu,
            name = 'y_hat_src2')
        # Time-frequency masking layer
        # np.finfo(float).eps: the smallest representable positive number such that 1.0 + eps != 1.0
        # Absolute value? In principle y_srcs could only be positive in spectrogram
        y_tilde_src1 = y_hat_src1 / (y_hat_src1 + y_hat_src2 + np.finfo(float).eps) * self.x_mixed
        y_tilde_src2 = y_hat_src2 / (y_hat_src1 + y_hat_src2 + np.finfo(float).eps) * self.x_mixed
        # Mask with Abs
        #y_tilde_src1 = tf.abs(y_hat_src1) / (tf.abs(y_hat_src1) + tf.abs(y_hat_src2) + np.finfo(float).eps) * self.x_mixed
        #y_tilde_src2 = tf.abs(y_hat_src2) / (tf.abs(y_hat_src1) + tf.abs(y_hat_src2) + np.finfo(float).eps) * self.x_mixed

        return y_tilde_src1, y_tilde_src2
        #return y_hat_src1, y_hat_src2

    def network_initializer(self):

        with tf.variable_scope('rnn_network') as scope:
            y_pred_src1, y_pred_src2 = self.network()

        return y_pred_src1, y_pred_src2

    def generalized_kl_divergence(self, y, y_hat):

        return tf.reduce_mean(y * tf.log(y / y_hat) - y + y_hat)

    def loss_initializer(self):

        with tf.variable_scope('loss') as scope:

            # Mean Squared Error Loss
            loss = tf.reduce_mean(tf.square(self.y_src1 - self.y_pred_src1) + tf.square(self.y_src2 - self.y_pred_src2), name = 'loss')

            '''
            # Generalized KL Divergence Loss
            loss = tf.add(
                x = self.generalized_kl_divergence(y = self.y_src1, y_hat = self.y_pred_src1),
                y = self.generalized_kl_divergence(y = self.y_src2, y_hat = self.y_pred_src2),
                name = 'GKL_loss')

            # Mean Squared Error + Signal to Inference Ratio Loss
            loss = tf.reduce_mean(tf.square(self.y_src1 - self.y_pred_src1) + tf.square(self.y_src2 - self.y_pred_src2) - self.gamma * (tf.square(self.y_src1 - self.y_pred_src2) + tf.square(self.y_src2 - self.y_pred_src1)), name = 'MSE_SIR_loss')

            # Generalized KL Divergence + Signal to Inference Ratio Loss
            loss = tf.subtract(
                x = (self.generalized_kl_divergence(y = self.y_src1, y_hat = self.y_pred_src1) + self.generalized_kl_divergence(y = self.y_src2, y_hat = self.y_pred_src2)),
                y = self.gamma * (self.generalized_kl_divergence(y = self.y_src1, y_hat = self.y_pred_src2) + self.generalized_kl_divergence(y = self.y_src2, y_hat = self.y_pred_src1)),
                name = 'GKL_SIR_loss')
            '''

        return loss

    def optimizer_initializer(self):

        optimizer = tf.train.AdamOptimizer(learning_rate = self.learning_rate).minimize(self.loss, global_step = self.gstep)

        return optimizer

    def train(self, x, y1, y2, learning_rate):

        #step = self.gstep.eval()

        step = self.sess.run(self.gstep)

        _, train_loss, summaries = self.sess.run([self.optimizer, self.loss, self.summary_op],
            feed_dict = {self.x_mixed: x, self.y_src1: y1, self.y_src2: y2, self.learning_rate: learning_rate})
        self.writer.add_summary(summaries, global_step = step)
        return train_loss

    def validate(self, x, y1, y2):

        y1_pred, y2_pred, validate_loss = self.sess.run([self.y_pred_src1, self.y_pred_src2, self.loss],
            feed_dict = {self.x_mixed: x, self.y_src1: y1, self.y_src2: y2})
        return y1_pred, y2_pred, validate_loss

    def test(self, x):

        y1_pred, y2_pred = self.sess.run([self.y_pred_src1, self.y_pred_src2], feed_dict = {self.x_mixed: x})

        return y1_pred, y2_pred

    def save(self, directory, filename):

        if not os.path.exists(directory):
            os.makedirs(directory)
        self.saver.save(self.sess, os.path.join(directory, filename))
        return os.path.join(directory, filename)

    def load(self, filepath):

        self.saver.restore(self.sess, filepath)

    def summary(self):
        '''
        Create summaries to write on TensorBoard
        '''
        with tf.name_scope('summaries'):
            tf.summary.scalar('loss', self.loss)
            tf.summary.histogram('x_mixed', self.x_mixed)
            tf.summary.histogram('y_src1', self.y_src1)
            tf.summary.histogram('y_src2', self.y_src2)
            summary_op = tf.summary.merge_all()

        return summary_op


text:
import sys
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
from datetime import datetime

class AdamTrainer:

    def __init__(self, rng=np.random, batchsize=16, epochs=100, alpha=0.001, beta1=0.9, beta2=0.999, eps=1e-8, cost='mse'):
        self.alpha = alpha
        self.beta1 = beta1
        self.beta2 = beta2
        self.eps = eps
        self.rng = rng
        self.theano_rng = RandomStreams(rng.randint(2 ** 30))
        self.epochs = epochs
        self.batchsize = batchsize
        if   cost == 'mse':
            self.cost = lambda network, x, y: T.mean((network(x) - y)**2)
        elif cost == 'cross_entropy':
            self.cost = lambda network, x, y: T.nnet.binary_crossentropy(network(x), y).mean()
        else:
            self.cost = cost
        self.params = None
        self.total_epochs = np.array([0])
        self.train_losses = np.array([0])

    def get_cost_updates(self, network, input, output):

        cost = self.cost(network, input, output) + network.cost(input)
        #cost = self.cost(network, input, output)

        gparams = T.grad(cost, self.params)
        m0params = [self.beta1 * m0p + (1-self.beta1) *  gp     for m0p, gp in zip(self.m0params, gparams)]
        m1params = [self.beta2 * m1p + (1-self.beta2) * (gp*gp) for m1p, gp in zip(self.m1params, gparams)]
        params = [p - self.alpha *
                  ((m0p/(1-(self.beta1**self.t[0]))) /
            (T.sqrt(m1p/(1-(self.beta2**self.t[0]))) + self.eps))
            for p, m0p, m1p in zip(self.params, m0params, m1params)]

        updates = ([( p,  pn) for  p,  pn in zip(self.params, params)] +
                   [(m0, m0n) for m0, m0n in zip(self.m0params, m0params)] +
                   [(m1, m1n) for m1, m1n in zip(self.m1params, m1params)] +
                   [(self.t, self.t+1)])

        return (cost, updates)

    def save(self, network, filename):
        database = {}
        network.save(database, '')
        np.savez_compressed(filename, **database)

    def train(self, network, input_data, output_data, filename=None, restart=True, shuffle=True, silent=False):

        input = input_data.type()
        output = output_data.type()
        index = T.lscalar()

        if restart or self.params is None:
            self.params = network.params
            self.m0params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
            self.m1params = [theano.shared(np.zeros(p.shape.eval(), dtype=theano.config.floatX), borrow=True) for p in self.params]
            self.t = theano.shared(np.array([1], dtype=theano.config.floatX))

        cost, updates = self.get_cost_updates(network, input, output)
        train_func = theano.function([index], cost, updates=updates, givens={
            input:input_data[index*self.batchsize:(index+1)*self.batchsize],
            output:output_data[index*self.batchsize:(index+1)*self.batchsize],
        }, allow_input_downcast=True)

        last_mean = 0
        for epoch in range(self.epochs):

            batchinds = np.arange(input_data.shape.eval()[0] // self.batchsize)

            if shuffle:
                self.rng.shuffle(batchinds)

            sys.stdout.write('\n')

            c = np.zeros((len(batchinds),))
            for bii, bi in enumerate(batchinds):
                c[bii] = train_func(bi)
                if np.isnan(c[bii]): return
                if not silent and bii % (int(len(batchinds) / 1000) + 1) == 0:
                #if True:
                    sys.stdout.write('\r[Epoch %3i] % 3.1f%% mean %03.5f' % (epoch, 100 * float(bii)/len(batchinds), c[:bii+1].mean()))
                    sys.stdout.flush()

            curr_mean = c.mean()
            self.total_epochs = np.append(self.total_epochs, self.total_epochs[-1]+1)
            self.train_losses = np.append(self.train_losses, c.mean())
            diff_mean, last_mean = curr_mean-last_mean, curr_mean
            sys.stdout.write('\r[Epoch %3i] 100.0%% mean %03.5f diff % .5f %s' %
                (epoch, curr_mean, diff_mean, str(datetime.now())[11:19]))
            sys.stdout.flush()

            if filename: self.save(network, filename)

reason: import numpy,import theano,from theano


text:
# xyz
# Jan 2018
import os, sys
import numpy as np
from plyfile import PlyData, PlyElement

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(BASE_DIR)
sys.path.append(ROOT_DIR)
from datasets.all_datasets_meta.datasets_meta import DatasetsMeta

def gen_box_and_pl( ply_fn, box_vertexes, pl_xyz=None, extra='' ):
    '''
    Generate box and points together in the same file. box_vertexes and pl_xyz
      independently. The 8 vertexs are included automatically in the box.
      pl_xyz is used to provide other points.
    box_vertexes:[num_box,8,3]
    pl_xyz:  [num_point,3]
    '''
    assert box_vertexes.ndim == 3
    assert box_vertexes.shape[1] == 8
    assert box_vertexes.shape[-1] == 3

    box_vertexes = np.reshape( box_vertexes, (-1,3) )
    num_box = box_vertexes.shape[0] // 8
    if type(pl_xyz) != type(None):
        assert pl_xyz.shape[-1] == 3
        pl_xyz = np.reshape( pl_xyz, (-1,3) )
        num_vertex = box_vertexes.shape[0] + pl_xyz.shape[0]
    else:
        num_vertex = box_vertexes.shape[0]
    vertex = np.zeros( shape=(num_vertex) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8')])
    for i in range(box_vertexes.shape[0]):
        vertex[i] = ( box_vertexes[i,0],box_vertexes[i,1],box_vertexes[i,2] )

    if type(pl_xyz) != type(None):
        for i in range(pl_xyz.shape[0]):
            vertex[i+box_vertexes.shape[0]] = ( pl_xyz[i,0],pl_xyz[i,1],pl_xyz[i,2] )

    el_vertex = PlyElement.describe(vertex,'vertex')

    # define the order of the 8 vertexs for a box
    edge_basic = np.array([ (0, 1, 255, 0, 0),
                            (1, 2, 255, 0, 0),
                            (2, 3, 255, 0, 0),
                            (3, 0, 255, 0, 0),
                            (4, 5, 255, 0, 0),
                            (5, 6, 255, 0, 0),
                            (6, 7, 255, 0, 0),
                            (7, 4, 255, 0, 0),
                            (0, 4, 255, 0, 0),
                            (1, 5, 255, 0, 0),
                            (2, 6, 255, 0, 0),
                            (3, 7, 255, 0, 0)] )
    if extra=='random_color_between_boxes':
      color = np.random.randint(0,256,3)
      color[2] = 255 - color[0] - color[1]
    edge_basic[:,2:5] = color

    edge_val = np.concatenate( [edge_basic]*num_box,0 )
    for i in range(num_box):
        edge_val[i*12:(i+1)*12,0:2] += (8*i)
    edge = np.zeros( shape=(edge_val.shape[0]) ).astype(
                    dtype=[('vertex1', 'i4'), ('vertex2','i4'),
                           ('red', 'u1'), ('green', 'u1'),('blue', 'u1')])
    for i in range(edge_val.shape[0]):
        edge[i] = ( edge_val[i,0], edge_val[i,1], edge_val[i,2], edge_val[i,3], edge_val[i,4] )
    el_edge = PlyElement.describe(edge,'edge')

    dirname = os.path.dirname(ply_fn)
    if not os.path.exists(dirname):
      os.makedirs(dirname)
    PlyData([el_vertex, el_edge],text=True).write(ply_fn)
    print('write %s ok'%(ply_fn))

def gen_box_8vertexs( bxyz_min, bxyz_max ):
    '''
    no rotation!!
    from block xyz_min and xyz_max, generate eight vertexs in order
    bxyz_min: [n_box,3]
    bxyz_max" [n_box,3]
    '''
    if bxyz_min.ndim == 1:
        bxyz_max = np.expand_dims( bxyz_max,0 )
        bxyz_min = np.expand_dims( bxyz_min,0 )

    dxyz = bxyz_max - bxyz_min
    dx = dxyz[:,0]
    dy = dxyz[:,1]
    dz = dxyz[:,2]
    if bxyz_min.ndim==1:
        bxyz_min = np.expand_dims( bxyz_min,0 )

    box_vertexes = np.expand_dims( bxyz_min,1 )
    box_vertexes = np.tile( box_vertexes,[1,8,1] )
    box_vertexes[:,1,1] += dy
    box_vertexes[:,2,0] += dx
    box_vertexes[:,2,1] += dy
    box_vertexes[:,3,0] += dx

    box_vertexes[:,4:8,:] = box_vertexes[:,0:4,:] + 0
    box_vertexes[:,4:8,2] += np.expand_dims( dz,-1 )
    return box_vertexes

def gen_box_norotation( ply_fn, bxyz_min, bxyz_max ):
    box_vertexes = gen_box_8vertexs( bxyz_min, bxyz_max )
    gen_box_and_pl( ply_fn, box_vertexes )

def test_box( pl_xyz=None ):
    box_vertex0_a = np.array( [(0,0,0),(0,1,0),(1,1,0),(1,0,0)] )
    box_vertex0_b = box_vertex0_a + np.array([0,0,1])
    box_vertex0 = np.concatenate([box_vertex0_a,box_vertex0_b],0)
    box_vertex0 = np.expand_dims( box_vertex0,0 )
    box_vertex1 = box_vertex0 + np.array([[0.3,0.3,0]])
    box_vertexes = np.concatenate( [box_vertex0,box_vertex1],0 )

    bxyz_min = np.array([[0,0,0], [0,3,4]])
    bxyz_max = np.array([[1,2,1], [1,4,6]])
    box_vertexes = gen_box_8vertexs( bxyz_min, bxyz_max )
    pl_xyz = box_vertexes + np.array([-0.2,-0.3,0.2])
    pl_xyz = None

    gen_box_and_pl( '/tmp/box_pl.ply',box_vertexes, pl_xyz )

def cut_xyz( xyz, cut_threshold = [1,1,1] ):
    xyz = np.reshape( xyz,[-1,xyz.shape[-1]] )
    if np.sum(cut_threshold) == 3:
        is_keep = np.array( [True]*xyz.shape[0] )
        return xyz, is_keep
    cut_threshold = np.array( cut_threshold )
    xyz_min = np.amin( xyz[:,0:3], 0 )
    xyz_max = np.amax( xyz[:,0:3], 0 )
    xyz_scope = xyz_max - xyz_min
    xyz_cut = xyz_min + xyz_scope * cut_threshold
    tmp = xyz[:,0:3] <= xyz_cut
    is_keep = np.array( [True]*xyz.shape[0] )
    for i in range(xyz.shape[0]):
        is_keep[i] = tmp[i].all()
    xyz_new = xyz[is_keep,:]
    return xyz_new, is_keep

def create_ply( xyz0, ply_fn, label=None, label2color=None, force_color=None, box=None, cut_threshold=[1,1,1] ):
    '''
    xyz0: (num_block,num_point,3) or (num_point,3) or (num_block,num_point,6) or (num_point,6)
    label: (num_block,num_point) or (num_point)
    force_color: (3,)

    (1) color in xyz0: xyz0.shape[-]==6,  label=None, label2color=None
    (2) no color: xyz0.shape[-1]==3, label=None, label2color=None
    (3) color by label: xyz0.shape[-1]==3,label.shape= (num_block,num_point) or (num_point)
    (4) set consistent color: xyz0.shape[-1]==3, label=None, label2color=None, force_color=[255,0,0]
  '''
    print(ply_fn)
    folder = os.path.dirname(ply_fn)
    if not os.path.exists(folder):
        os.makedirs(folder)
    new_xyz_ls = []
    is_keep_ls = []
    is_cut = xyz0.ndim >= 3
    if is_cut:
        for i in range( xyz0.shape[0] ):
            new_xyz_i, is_keep_i = cut_xyz( xyz0[i], cut_threshold )
            new_xyz_ls.append( new_xyz_i )
            is_keep_ls.append( is_keep_i )
        xyz = np.concatenate( new_xyz_ls, 0 )
        is_keep = np.concatenate( is_keep_ls, 0 )
    else:
        xyz = xyz0

    if xyz.shape[-1] == 3:
        if type(label) != type(None) and label2color!=None:
            #(3) color by label: xyz0.shape[-1]==3,label.shape= (num_block,num_point) or (num_point)
            label2color_ls = []
            for i in range(len(label2color)):
                label2color_ls.append( np.reshape(np.array(label2color[i]),(1,3)) )
            label2colors = np.concatenate( label2color_ls,0 )
            color = np.take( label2colors,label,axis=0 )
            color = np.reshape( color,(-1,3) )
            if is_cut:
                color = color[is_keep,:]
            xyz = np.concatenate([xyz,color],-1)
        elif type(force_color)!=type(None):
            #(4) set consistent color: xyz0.shape[-1]==3, label=None, label2color=None, force_color=[255,0,0]
            color = np.reshape( np.array( force_color ),[1,3] )
            color = np.tile( color,[xyz.shape[0],1] )
            xyz = np.concatenate([xyz,color],-1)
    if xyz.shape[-1] == 3:
        #(2) no color: xyz0.shape[-1]==3, label=None, label2color=None
        vertex = np.zeros( shape=(xyz.shape[0]) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8')])
        for i in range(xyz.shape[0]):
            vertex[i] = ( xyz[i,0],xyz[i,1],xyz[i,2] )
    elif xyz.shape[-1] == 6:
        vertex = np.zeros( shape=(xyz.shape[0]) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8'),('red','u1'),('green','u1'),('blue','u1')])
        for i in range(xyz.shape[0]):
            vertex[i] = ( xyz[i,0],xyz[i,1],xyz[i,2],xyz[i,3],xyz[i,4],xyz[i,5] )
    else:
      raise NotImplementedError

    el_vertex = PlyElement.describe(vertex,'vertex')
    PlyData([el_vertex],text=True).write(ply_fn)

    print('save ply file: %s'%(ply_fn))

def create_ply_dset( dataset_name, xyz, ply_fn, label=None, cut_threshold=[1,1,1], extra='' ):
    if extra == 'random_same_color' and type(label)==type(None):
      label = np.ones(xyz.shape[0:-1]) * np.random.randint(
        len(DatasetsMeta.g_label2class[dataset_name]))
      label = label.astype(np.int32)
    create_ply( xyz, ply_fn, label = label, label2color = DatasetsMeta.g_label2color[dataset_name], cut_threshold=cut_threshold )

def draw_points_and_edges(ply_fn, xyz, edge_indices):
  '''
  xyz: (num_point, 3)
  edge_indices:(num_edge,2)
  '''
  #
  vertex = np.zeros( shape=(xyz.shape[0]) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8')])
  for i in range(xyz.shape[0]):
      vertex[i] = ( xyz[i,0],xyz[i,1],xyz[i,2] )

  #
  num_edge = edge_indices.shape[0]
  edge_val = np.zeros(shape=(num_edge, 5))
  edge_val[:,0:2] = edge_indices
  edge_val[:,2:5] = np.array([0,0,255])

  edge = np.zeros( shape=(edge_val.shape[0]) ).astype(
                  dtype=[('vertex1', 'i4'), ('vertex2','i4'),
                          ('red', 'u1'), ('green', 'u1'),('blue', 'u1')])
  for i in range(edge_val.shape[0]):
      edge[i] = ( edge_val[i,0], edge_val[i,1], edge_val[i,2], edge_val[i,3], edge_val[i,4] )

  el_vertex = PlyElement.describe(vertex,'vertex')
  el_edge = PlyElement.describe(edge,'edge')
  PlyData([el_vertex, el_edge],text=True).write(ply_fn)
  print('write %s ok'%(ply_fn))

def draw_points_and_voxel_indices(ply_fn, xyz, voxel_indices):
  '''
  xyz:(num_point,3)
  voxel_indices:(num_point,3)
  '''
  # create edges
  num_point = xyz.shape[0]
  voxel_w = voxel_indices.max() + 1
  edge_indices = [ ]
  for i in range(num_point):
    for j in range(num_point):
      x_neib = (voxel_indices[j] == voxel_indices[i] + np.array([1,0,0])).all()
      y_neib = (voxel_indices[j] == voxel_indices[i] + np.array([0,1,0])).all()
      z_neib = (voxel_indices[j] == voxel_indices[i] + np.array([0,0,1])).all()
      if x_neib or y_neib or z_neib:
        edge_indices.append(np.array([[i,j]]))
  edge_indices = np.concatenate(edge_indices, 0)

  draw_points_and_edges(ply_fn, xyz, edge_indices)

def draw_blocks_by_bot_cen_top(ply_fn, bot_cen_top, random_crop=0):
  '''
    bot_cen_top: (num_blocks, 9)
  '''
  shape0 = bot_cen_top.shape
  assert shape0[-1] == 9
  bot_cen_top = np.reshape(bot_cen_top, [-1,9])
  if random_crop>0:
    N = bot_cen_top.shape[0]
    choices = np.random.choice(N, N-int(N*random_crop))
    bot_cen_top = np.take(bot_cen_top, choices, axis=0)
  block_bottom = bot_cen_top[:,0:3]
  block_center = bot_cen_top[:,3:6]
  block_top = 2*(block_center - block_bottom) + block_bottom
  box_vertexes = gen_box_8vertexs(block_bottom, block_top)
  gen_box_and_pl(ply_fn, box_vertexes, extra='random_color_between_boxes')

import  color_dic
def gen_mesh_ply(ply_fn, vertices0, vertex_idx, face_label=None,
                 vertex_label=None, vertex_color=None,
                 extra='label_color_default'):
    '''
    '''
    assert (int(face_label is None) + int(vertex_label is None) + int(vertex_color is None)) >=2, \
      "choose one color method from three: {}, {}, {}".format(face_label, vertex_label, vertex_color)
    assert vertices0.shape[-1] ==  3
    vnpf = vertex_idx.shape[-1]
    assert np.min(vertex_idx)>=0, "negative vertex_idx"

    vertices0 = np.reshape( vertices0, (-1,3) )
    vertex_idx = np.reshape(vertex_idx, (-1,vnpf))
    if vertex_label is not None:
      vertex_label = np.reshape(vertex_label, (-1))
    if vertex_color is not None:
      vertex_color = np.reshape(vertex_color, (-1, 3))

    num_vertex = vertices0.shape[0]
    assert np.max(vertex_idx) < num_vertex

    is_vertex_color = (vertex_label is not None) or (vertex_color is not None)
    if not is_vertex_color:
      vertex = np.zeros( shape=(num_vertex) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8')])
    else:
      if vertex_label is not None:
        vertex_color = np.take(color_dic.rgb_order, vertex_label, 0)

      assert vertex_color.shape[0] == num_vertex
      vertex = np.zeros( shape=(num_vertex) ).astype([('x', 'f8'), ('y', 'f8'),('z', 'f8'),
                                            ('red', 'u1'), ('green', 'u1'),('blue', 'u1')])
    for i in range(vertices0.shape[0]):
      if not is_vertex_color:
        vertex[i] = ( vertices0[i,0],vertices0[i,1],vertices0[i,2] )
      else:
        vertex[i] = ( vertices0[i,0],vertices0[i,1],vertices0[i,2], \
                      vertex_color[i,0], vertex_color[i,1], vertex_color[i,2])

    el_vertex = PlyElement.describe(vertex,'vertex')
    if vnpf >2:
      el_face = get_face_ele(vertex_idx, face_label, extra)
    else:
      el_face = get_edge_ele(vertex_idx, face_label, extra)

    dirname = os.path.dirname(ply_fn)
    if not os.path.exists(dirname):
      os.makedirs(dirname)
    PlyData([el_vertex, el_face],text=True).write(ply_fn)
    print('write %s ok'%(ply_fn))

def get_face_ele(vidx_per_face, face_label, extra):
    # face
    num_face = vidx_per_face.shape[0]
    vnpf = vidx_per_face.shape[1]
    assert vnpf > 2
    is_face_color = face_label is not None
    if is_face_color:
      if face_label is not None:
        if extra=='label_color_default':
          face_label = np.squeeze(face_label)
          face_color = np.take(color_dic.rgb_order, face_label, 0)

      face = np.zeros( shape=(num_face) ).astype(
                    dtype=[('vertex_indices', 'i4', (vnpf,)),
                           ('red', 'u1'), ('green', 'u1'),('blue', 'u1')])
    else:
      face = np.zeros( shape=(num_face) ).astype(
                    dtype=[('vertex_indices', 'i4', (vnpf,))])

    for i in range(num_face):
      if is_face_color:
        face[i] = ( vidx_per_face[i], face_color[i,0], face_color[i,1], face_color[i,2] )
      else:
        face[i] = (vidx_per_face[i],)

    el_face = PlyElement.describe(face, 'face')
    return el_face

def get_edge_ele(vidx_per_edge, edge_label, extra):
    # edge
    num_edge = vidx_per_edge.shape[0]
    vnpf = vidx_per_edge.shape[1]
    assert vnpf == 2
    is_edge_color = edge_label is not None
    if is_edge_color:
      if edge_label is not None:
        if extra=='label_color_default':
          edge_label = np.squeeze(edge_label)
          edge_color = np.take(color_dic.rgb_order, edge_label, 0)

      edge = np.zeros( shape=(num_edge) ).astype(
                    dtype=[('vertex1', 'i4'), ('vertex2', 'i4'),
                           ('red', 'u1'), ('green', 'u1'),('blue', 'u1')])
    else:
      edge = np.zeros( shape=(num_edge) ).astype(
                    dtype=[('vertex1', 'i4'), ('vertex2', 'i4')])

    for i in range(num_edge):
      if is_edge_color:
        edge[i] = ( vidx_per_edge[i][0], vidx_per_edge[i][1], edge_color[i,0], edge_color[i,1], edge_color[i,2] )
      else:
        edge[i] = ( vidx_per_edge[i][0], vidx_per_edge[i][1])

    el_edge = PlyElement.describe(edge, 'edge')
    return el_edge

def closed_path_to_edges(closed_vidx, valid_vn):
  vn0 = closed_vidx.shape[-1]
  closed_vidx = np.reshape(closed_vidx, [-1, vn0])
  valid_vn = np.reshape(valid_vn, [-1])
  en0 = closed_vidx.shape[0]

  edges = []
  for i in range(en0):
    vn_i = max(valid_vn[i], vn0)
    for j in range(vn_i):
      if j==vn_i-1:
        next_j = 0
        continue
      else:
        next_j = j+1
      if closed_vidx[i,j] == closed_vidx[i,next_j]:
        continue
      edges.append([[closed_vidx[i,j], closed_vidx[i,next_j]]])
  edges = np.concatenate(edges, 0)
  return edges

def test_closed_path_ply():
  ply_fn = '/home/z/Desktop/plys/close.ply'
  xyz = np.array([[0,0,0], [1,0,0], [2,0,0], [2,1,0], [2,2,0], [0,2,0]])
  closed_vidx = np.array([range(6)+[0]])
  valid_num = np.array([7])
  edges = closed_path_to_edges(closed_vidx, valid_num)
  gen_mesh_ply(ply_fn, xyz, edges)

if __name__ == '__main__':
    #test_box()
    test_closed_path_ply()

    #sg_bidxmap_i0 = np.arange( sg_bidxmap_i1.shape[0] ).reshape([-1,1,1,1])
    #sg_bidxmap_i0 = np.tile( sg_bidxmap_i0, [0,sg_bidxmap_i1.shape[1], sg_bidxmap_i1.shape[2],1] )

max_stars_repo_name: xuyongzhi/SparseVoxelNet

max_issues_repo_name: xuyongzhi/SparseVoxelNet

max_forks_repo_name: xuyongzhi/SparseVoxelNet