text:
# Required Libraries
import os
import sys
import re
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import csv
import time
from datetime import datetime
from zipfile import ZipFile
from pyspark import SparkConf, SparkContext
from textblob import TextBlob
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from statistics import mean
from collections import Counter
from wordcloud import WordCloud, STOPWORDS
from PIL import Image

'''Input'''

print("Welcome to Feedback Analyzer!")

print("Instructions:")
print("------------------------------------------------------------------------------------------")
print("1. Ensure your input file is a CSV FIle.")
print("2. Ensure your column having feedbacks should be named 'Feedback'.")
print("3. Ensure your input file exists in the same directory where you'll be executing the code.")
print("------------------------------------------------------------------------------------------")
input_file = sys.argv[1]
print(input_file,"been used.")

# Check if Input is correct
try :
    data = pd.read_csv("sample.csv", index_col ="Feedback" )
except ValueError :
    print("'Feedback' column not found!")
    print("Process Terminated. Please go through Instructions.")
    sys.exit()
else :
    print("Input Check Completed.")
    print("------------------------------------------------------")

'''Word Count'''

print("Word Count Started")

# Clean Feedbacks
def normalizeWords(text):
    return re.compile(r'\W+', re.UNICODE).split(text.lower())

# Spark Instance
conf = SparkConf().setMaster("local").setAppName("WordCount")
sc = SparkContext(conf = conf)

# Files for counting words
csv_file = 'filtered_feedback.csv'
txt_file = 'result.txt'

# Extract Feedback column
data = pd.read_csv(input_file, index_col ="Feedback" )
data = data.loc[:, data.columns.intersection(['Feedback'])]
data.to_csv('filtered_feedback.csv', header=False)

# Encoding Feedback
with open(txt_file, "w") as my_output_file:
    with open(csv_file, "r", encoding='cp1252',  errors="ignore") as my_input_file:
        [ my_output_file.write(" ".join(row)+'\n') for row in csv.reader(my_input_file)]
    my_output_file.close()

# Spark Counting Words
input = sc.textFile("file:///Feedback_Analyzer/result.txt")
words = input.flatMap(normalizeWords)

wordCounts = words.map(lambda x: (x, 1)).reduceByKey(lambda x, y: x + y)
wordCountsSorted = wordCounts.map(lambda x: (x[1], x[0])).sortByKey()
results = wordCountsSorted.collect()

word_count_result = {}
for result in results:
    count = str(result[0])
    word = result[1].encode('ascii', 'ignore')
    if (word):
        word_count_result[str(word.decode())] = [int(count)]
        '''print(word.decode() + ":\t\t" + count)'''

# CSV file for result of word count
dataframe = pd.DataFrame.from_dict(word_count_result, orient='index')
dataframe.to_csv('word_count_result.csv',header=False)

print("Word Count Completed")
print("------------------------------------------------------")

'''Sentiment Analysis'''

print("Sentiment Analysis Started")
print("Sit back and relax!! This might take several minutes.")

# Remove Emojis from feedback
def remove_emoji(string):
    emoji_pattern = re.compile("["
                           u"\U0001F600-\U0001F64F"  # emoticons
                           u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                           u"\U0001F680-\U0001F6FF"  # transport & map symbols
                           u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                           u"\U00002702-\U000027B0"
                           u"\U000024C2-\U0001F251"
                           "]+", flags=re.UNICODE)
    return emoji_pattern.sub(r'', string)

# Textblob Evaluation
def textblob_evaluation(polarity):
    if polarity >= 0.05: return 1
    elif polarity <= -0.05: return -1
    else: return 0

# Vader Evaluation
def vader_evaluation(polarity):
    if polarity >= 0.1: return 1
    elif polarity <= -0.4: return -1
    else: return 0

# Algorithm for Sentiment Analysis
def sentiment_evaluation(polarities):
    polarity_counter = Counter(polarities)
    pos,neu,neg = polarity_counter[1],polarity_counter[0],polarity_counter[-1]
    polarity_list = { "-1" : neg, "0" : neu, "1" : pos }
    # print(polarity_list)
    if pos == neu == neg == 0: return 0
    if neg > 1 and (neg >= pos-1 or neg >= neu-1): return -1
    if pos >= neu and pos > neg: return 1
    return max(polarity_list, key= lambda x: polarity_list[x])

# Create Clean Text
def clean_text(line):
    f = open("clean_text.txt", "a")
    f.write(line+"\n")
    f.close()

# Analyze Sentiment for each Feedback
def analyze_sentiment(line):
    polarities = []
    sid_obj = SentimentIntensityAnalyzer()
    line = remove_emoji(str(line))
    line = (str(line).encode('ascii', 'ignore')).decode()
    clean_text(line)
    for sentence in line.split('.'):
        if len(sentence)>0:
            # print(sentence)
            textblob_result,vader_result = TextBlob(str(sentence)).sentiment.polarity, sid_obj.polarity_scores(sentence)['compound']
            textblob_eval, vader_eval = textblob_evaluation(textblob_result), vader_evaluation(vader_result)
            # print(textblob_result,vader_result)
            if(textblob_eval == -1 or vader_eval == -1): polarities.append(-1)
            elif(textblob_eval == 0 and vader_eval == 0): polarities.append(0)
            elif(textblob_eval == 0 or vader_eval == 0):
                if(vader_eval != 0) : polarities.append(vader_eval)
                else: polarities.append(0)
            elif(textblob_eval == 1 and vader_eval == 1): polarities.append(1)
    result = int(sentiment_evaluation(polarities))
    if result>0: return "Positive"
    if result==0: return "Neutral"
    if result<0: return "Negative"

# Generate file after Sentiment analysis
df = pd.read_csv(input_file)

sentiment = np.array([])
for feedback in df["Feedback"]:
    sentiment = np.append(sentiment, np.array([analyze_sentiment(feedback)]))
df["Sentiment"] = sentiment
df = df.loc[:, df.columns.intersection(['Feedback','Sentiment'])]
df.index += 1
df.to_csv('sentiment_analysis.csv', header=False)

# Generate WordCloud
text = open('clean_text.txt', 'r').read()
stopwords = STOPWORDS
mask = np.array(Image.open("cloud.png"))
wc = WordCloud(stopwords = stopwords, mask=mask, background_color="white")
wc.generate(text)
wc.to_file('wordcloud.png')

print("Sentiment Analysis Completed")
print("------------------------------------------------------")

'''Zip File Generation'''

print("Zipping Files")

# Current Timestamp
dt_object = datetime.fromtimestamp(time.time())

# Process details
f = open("details.txt", "w")
details = "Timestamp: "+str(dt_object)+"\nInput File: "+str(input_file)
f.write(str(details))
f.close()

# Zipping Files
filename = "fa_"+str(round(time.time()))+".zip"
zipObj = ZipFile(str(filename), "w")
zipObj.write('details.txt')
zipObj.write('sentiment_analysis.csv')
zipObj.write('word_count_result.csv')
zipObj.write('wordcloud.png')
zipObj.close()

# Remove Files
scrap_files = [
    "details.txt",
    "filtered_feedback.csv",
    "result.txt",
    "sentiment_analysis.csv",
    "word_count_result.csv",
    "clean_text.txt",
    "wordcloud.png"
]
for scrap_file in scrap_files:
    try:
        os.remove(scrap_file)
        # print("% s removed successfully" % scrap_file)
    except OSError as error:
        print(error)
        print("File path %s can not be removed" % scrap_file)

print("Your required ZIP file "+str(filename)+" is ready.")
print("------------------------------------------------------")


text:
from collections import defaultdict
from tqdm import tqdm
import numpy as np
import os
import imageio
import matplotlib.pyplot as plt
import glob
import tensorflow as tf
import cv2
import cmapy
import skimage
import pathlib
from tensorboard.backend.event_processing import event_accumulator
from moviepy.editor import VideoFileClip, clips_array, vfx, ImageSequenceClip, CompositeVideoClip, concatenate_videoclips, VideoClip, TextClip
from matplotlib import animation

def extract_images_from_summary(events_path, tag_names_to_look_for, suffix='', img_outdir=None, colormap=None):
    print("Extracting data from tensorboard summary...")
    event_acc = event_accumulator.EventAccumulator(events_path, size_guidance={'images': 0})
    event_acc.Reload()

    # a suffix to append to the name if we save in outdir
    strsuffix = suffix

    if img_outdir is not None:
        outdir = pathlib.Path(img_outdir)
        outdir.mkdir(exist_ok=True, parents=True)

    # We are looking at all the images ...
    image_dict = defaultdict(list)
    for tag in event_acc.Tags()['images']:
        print("processing tag %s"%tag)
        events = event_acc.Images(tag)
        tag_name = tag.replace('/', '_')
        # ... that have the tag name: "tag_name_to_look_for"
        if tag_name in tag_names_to_look_for:
            tag_name = tag_name + strsuffix

            if img_outdir is not None:
                dirpath = outdir / tag_name
                dirpath.mkdir(exist_ok=True, parents=True)

            for index, event in enumerate(events):
                s = np.frombuffer(event.encoded_image_string, dtype=np.uint8)
                image = cv2.imdecode(s, cv2.IMREAD_COLOR)

                if colormap is not None:
                    image = cv2.applyColorMap(image[..., 0], cmapy.cmap(colormap))

                if img_outdir is not None:
                    outpath = dirpath / '{:04}.png'.format(index)
                    cv2.imwrite(outpath.as_posix(), image)

                image_dict[tag].append(image)
    return image_dict

def extract_from_summary(path, value_tag):
    ''' Extracts values and wall times from tensorboard summaries
    '''
    if os.path.isdir(path):
        path = glob.glob(os.path.join(path, "*"))[0]

    origin_wall_time = None
    wall_times = []
    values = []

    for event in tf.compat.v1.train.summary_iterator(path):
        if not origin_wall_time:
            origin_wall_time = event.wall_time
        for value in event.summary.value:
            if value.tag == value_tag:
                wall_times.append(event.wall_time - origin_wall_time)
                values.append(value.simple_value)
    return wall_times, values

def save_video(video_clip, filepath):
    video_clip.resize(width=1080)

    height, width = video_clip.h, video_clip.w

    if height % 2:
        height += 1

    video_clip.resize(width=1080, height=height).write_videofile(filepath, fps=25,
                                                                 audio_codec='libfdk_aac', audio=False)

def make_video_grid_from_filepaths(num_rows, num_cols, video_list, trgt_name,
                                   margin_color=(255,255,255), margin_width=0,
                                   column_wise=True):

    clip_array = [[] for _ in range(num_rows)]
    for col in range(num_cols):
        for row in range(num_rows):
            if column_wise:
                idx = col * num_rows + row
            else:
                idx = row * num_cols + col

            video_clip = VideoFileClip(video_list[idx]).margin(margin_width, color=margin_color)
            if margin_width > 0:
                video_clip = video_clip.margin(margin_width, color=margin_color)

            clip_array[row].append(video_clip)

    final_clip = clips_array(clip_array)
    save_video(final_clip, trgt_name)

def animated_line_plot(x_axis, data, trgt_path, legend_loc='lower right', plot_type=None):
    fig, ax = plt.subplots()
    fontdict = {'size': 16}
    ax.tick_params(axis='both', which='major', direction='in', labelsize=11)
    ax.set_ylabel("PSNR", fontdict=fontdict)
    ax.set_xlabel("Iterations", fontdict=fontdict)
    if plot_type == 'image':
        ax.set_xticks([5000, 10000, 15000])
        ax.set_xticklabels(['5,000', '10,000', '15,000'])
        ax.set_xlim(0, 15000)
        ax.set_yticks([10, 20, 30, 40, 50, 60])
        ax.set_ylim(0, 60)
    elif plot_type == 'poisson':
        ax.set_xticks([1000, 2000, 3000, 4000])
        ax.set_xticklabels(['1,000', '2,000', '3,000', '4,000'])
        ax.set_xlim(0, 4000)
        ax.set_yticks([5, 10, 15, 20, 25, 30, 35])
        ax.set_ylim(0, 35)
    ax.grid()

    lines = []
    for key, y in data.items():
        lobj = ax.plot(x_axis, y, label=key)[0]
        lines.append(lobj)

    ax.legend(loc=legend_loc, bbox_to_anchor=(0.95, 0.035))

    def update(num, x, data, lines):
        for idx, (_, y) in enumerate(data.items()):
            lines[idx].set_data(x[:num], y[:num])
        return lines

    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=26, bitrate=1800)
    anim = animation.FuncAnimation(fig, update, fargs=[x_axis, data, lines],
                                   frames=len(x_axis), interval=1, blit=True)
    anim.save(trgt_path, writer=writer)

def make_video_from_tensorboard_summaries(summary_paths, trgt_path, image_extraction_dir, pred_tag_list,
                                          num_rows, num_cols, gt_tag_list=None, overwrite=False, colormap=None):
    for key in summary_paths.keys():
        if os.path.isdir(summary_paths[key]):
            summary_paths[key] = glob.glob(os.path.join(summary_paths[key], "*"))[0]

    video_filepaths = []

    def extract_images_make_videoclips(summary_path, root_dir, tag_list):
        if not os.path.exists(root_dir) or overwrite:
            extract_images_from_summary(summary_path,
                                        tag_names_to_look_for=tag_list,
                                        img_outdir=root_dir,
                                        colormap=colormap)

        for tag in tag_list:
            dir = os.path.join(root_dir, tag)
            video_path = os.path.join(dir, 'video.mp4')
            if not os.path.exists(video_path):
                print("Making video for %s" % dir)
                img_clip = ImageSequenceClip(dir, 26)
                save_video(img_clip, video_path)
            video_filepaths.append(video_path)

    # Extract ground truth
    gt_dir = os.path.join(image_extraction_dir, 'gt')
    summary_path = summary_paths[next(iter(summary_paths))]
    extract_images_make_videoclips(summary_path, gt_dir, gt_tag_list)

    # Extract all model predictions
    for key, summary_path in summary_paths.items():
        subdir = os.path.join(image_extraction_dir, key)
        summary_path = summary_path
        extract_images_make_videoclips(summary_path, subdir, pred_tag_list)

    # Now make joint video...
    if os.path.exists(trgt_path):
        val = input("The video %s exists. Overwrite? (y/n)" % trgt_path)
        if val == 'y':
            os.remove(trgt_path)

    make_video_grid_from_filepaths(num_rows, num_cols, video_list=video_filepaths,
                                   trgt_name=trgt_path, margin_width=0)

def glob_all_imgs(trgt_dir):
    '''Returns list of all images in trgt_dir
    '''
    all_imgs = []
    for ending in ['*.png', '*.tiff', '*.tif', '*.jpeg', '*.JPEG', '*.jpg', '*.bmp']:
        all_imgs.extend(glob.glob(os.path.join(trgt_dir, ending)))

    return all_imgs

def make_convergence_plot(gt_dir, img_dirs, trgt_path, animate=False, iters_info=None, plot_type=None):
    '''
    Args:
        img_dirs: dictionary with method name as key and path to the directory with the respective images as item
    '''
    if gt_dir is not None:
        gt_images = sorted(glob_all_imgs(gt_dir))

    psnrs = defaultdict(list)
    for key, path in tqdm(img_dirs.items()):
        psnrs_path = os.path.join(path, 'psnrs.npy')
        if os.path.exists(psnrs_path):
            psnrs[key] = np.load(psnrs_path).tolist()
            continue

        pred_images = sorted(glob_all_imgs(path))
        for gt_path, pred_path in tqdm(zip(gt_images, pred_images)):
            gt_img = imageio.imread(gt_path)
            pred_img = imageio.imread(pred_path)

            psnr = skimage.measure.compare_psnr(gt_img, pred_img)
            psnrs[key].append(psnr)

        np.save(psnrs_path, np.array(psnrs[key]))

    # Now make a line plot
    if gt_dir is not None:
        iterations = np.arange(len(gt_images))
    else:
        iterations = np.arange(0, iters_info['num_iters'], iters_info['step'])

    if animate:
        assert 'mp4' in trgt_path, "Filepath needs to be mp4"
        animated_line_plot(iterations, psnrs, trgt_path, plot_type=plot_type)
    else:
        fig, ax = plt.subplots()
        ax.tick_params(axis='both', which='major', direction='in', labelsize=8)
        ax.set_ylabel("PSNR")
        ax.set_xlabel("Iterations")
        ax.grid()

        ax.plot(iterations, psnrs[next(iter(psnrs))])
        fig.savefig(trgt_path, bbox='tight', bbox_inches='tight', pad_inches=0.)

def image_convergence_video():
    # Point each of the keys to the directory where you logged tensorboard summaries for this experiment.
    summary_paths = {"ReLU": "",
                     "Tanh": "",
                     "ReLU P.E.": "",
                     "RBF-ReLU": "",
                     "SIREN": ""}

    # This is the directory where all the images from the summaries will be extracted to.
    image_extraction_dir = './data/image_summaries'
    os.makedirs(image_extraction_dir, exist_ok=True)

    gt_tag_list = ['train_gt_img', 'train_gt_grad', 'train_gt_lapl']
    pred_tag_list = ['train_pred_img', 'train_pred_grad', 'train_pred_lapl']
    trgt_path = 'image_convergence.mp4'
    make_video_from_tensorboard_summaries(summary_paths, trgt_path, image_extraction_dir=image_extraction_dir,
                                          pred_tag_list=pred_tag_list, num_rows=3, num_cols=len(summary_paths)+1,
                                          gt_tag_list=gt_tag_list, overwrite=True)

def image_convergence_plot():
    animated = True

    # Point each of the keys to the directory where you logged tensorboard summaries for this experiment.
    summary_paths = {"ReLU": "",
                     "Tanh": "",
                     "ReLU P.E.": "",
                     "RBF-ReLU": "",
                     "SIREN": ""}

    # make convergence plot
    filename = 'image_psnr_convergence' + '.mp4' if animated else '.pdf'

    make_convergence_plot(None, img_dirs=summary_paths, animate=animated,
                          trgt_path=filename, iters_info={'num_iters':15001, 'step':5}, plot_type='image')

def extract_image_psnrs(summary_paths):
    for key, item in summary_paths.items():
        summary_file = os.listdir(item)[0]
        summary_file = os.path.join(item, summary_file)

        wall_times, values = extract_from_summary(summary_file, 'train_img_psnr')
        psnrs = [values for _, values in sorted(zip(wall_times, values))]

        np.save(os.path.join(item, 'psnrs.npy'), psnrs)

if __name__ == '__main__':
    image_convergence_video()
    image_convergence_plot()

ext: py

max_stars_repo_name: yusufraji/siren

max_issues_repo_name: yusufraji/siren

max_forks_repo_name: yusufraji/siren

reason: import numpy


text:
#!/usr/bin/env python

import rospy
import tf
import numpy as np

from geometry_msgs.msg import PointStamped
from geometry_msgs.msg import Point
from std_msgs.msg import Header
from sensor_msgs.msg import CameraInfo

import tf2_msgs.msg

global location_publisher
global inverse_camera_projection_matrix
inverse_camera_projection_matrix = None
global tf_listener

def process_camera_info(camera_info_msg):
    global inverse_camera_projection_matrix
    if inverse_camera_projection_matrix is None:
        rospy.loginfo("process_camera_info")
        inverse_camera_projection_matrix = np.array(camera_info_msg.P + (0., 0., 0., 1.))
        rospy.loginfo("camera_projection_matrix:\n %s", inverse_camera_projection_matrix)
        inverse_camera_projection_matrix.shape = (4, 4)
        inverse_camera_projection_matrix = np.linalg.inv(inverse_camera_projection_matrix)
        rospy.loginfo("inverse_camera_projection_matrix:\n %s", inverse_camera_projection_matrix)

def name():
    return "image_to_target"

def process_location(point_stamped):

    rospy.logdebug("process_location:\n%s", point_stamped)

    # Normalized image coordinates, homogenous coordinates
    image_target = np.array([point_stamped.point.x, point_stamped.point.y, 1., 1.])
    # rospy.loginfo("target 2D normalized:%s", image_target)

    # 2D to 3D normalized
    target = np.dot(inverse_camera_projection_matrix, image_target)
    # rospy.loginfo("target 3D normalized:%s", target)

    # Change to vehicle coordinate system
    target = [target[2], -target[0], -target[1], 1]
    # rospy.loginfo("target 3D vehicle coordinates:%s", target)

    # We also need to camera center point
    camera = np.array([0., 0., 0., 1.])

    # Get the frame translation to local_origin
    source_frame = 'gopro'
    target_frame = 'local_origin'

    if tf_listener.canTransform(source_frame, target_frame, point_stamped.header.stamp):
        rospy.logdebug("Transforming")

        target = to_point_stamped(target, source_frame, point_stamped.header.seq, point_stamped.header.stamp)
        camera = to_point_stamped(camera, source_frame, point_stamped.header.seq, point_stamped.header.stamp)

        #rospy.loginfo("camera in nikon:\n%s", camera)
        #rospy.loginfo("target in nikon:\n%s", target)

        #rospy.loginfo("Transform nikon -> fcu: %s", tf_listener.lookupTransform('fcu', 'nikon', point_stamped.header.stamp))
        target = tf_listener.transformPoint('fcu', target)
        camera = tf_listener.transformPoint('fcu', camera)

        #rospy.loginfo("camera in fcu:\n%s", camera)
        #rospy.loginfo("target in fcu:\n%s", target)

        #rospy.loginfo("Transform fcu -> local: %s", tf_listener.lookupTransform('local_origin', 'fcu', point_stamped.header.stamp))
        target = tf_listener.transformPoint(target_frame, target)
        camera = tf_listener.transformPoint(target_frame, camera)

        #rospy.loginfo("camera in local_origin:\n%s", camera)
        #rospy.loginfo("target in local_origin:\n%s", target)

        # project to intersection with ground plane
        scale = - camera.point.z / (target.point.z - camera.point.z)
        # rospy.loginfo("scale:%s", scale)

        point = Point(x=scale * target.point.x + (1. - scale) * camera.point.x, y=scale * target.point.y + (1. - scale) * camera.point.y, z=scale * target.point.z + (1. - scale) * camera.point.z)
        header = Header(frame_id=target_frame, seq=point_stamped.header.seq, stamp=point_stamped.header.stamp)
        joe = PointStamped(point=point, header=header)
        location_publisher.publish(joe)

    else:
        rospy.logdebug("Not transforming")

def to_point_stamped(np_point, frame, seq, stamp):
    point = Point(x=np_point[0], y=np_point[1], z=np_point[2])
    header = Header(frame_id=frame, seq=seq, stamp=stamp)
    point_stamped = PointStamped(point=point, header=header)
    return point_stamped

def process_tf(tfm):
    global last_tf
    global latch_tf

    for transform in tfm.transforms:
        frame = transform.header.frame_id
        child = transform.child_frame_id
        name = "%s to %s" % (frame, child)
        latch_tf[name] = transform.transform

    now = rospy.Time.now()
    if now - last_tf > rospy.Duration(3):
        info = ""
        for name in sorted(latch_tf.keys()):
            frame, child = name.split(" to ")
            transform = latch_tf[name]
            t = transform.translation
            r = transform.rotation
            info += "\n  %-12s  %-7s  translation: [%2.2f,%2.2f,%2.2f]  rotation: [%2.2f,%2.2f,%2.2f,%2.2f]" % \
                    (frame, child, t.x, t.y, t.z, r.x, r.y, r.z, r.w)
        rospy.loginfo("%s", info)
        last_tf = now

def image_to_target():
    global location_publisher
    global tf_listener
    rospy.init_node(name())
    rospy.loginfo("%s starting" % name())

    # Start listening for frame information
    tf_listener = tf.TransformListener()

    # publish 3D locations in local_origin frame
    location_publisher = rospy.Publisher("target_position_local", PointStamped, queue_size = 10)

    # subscribe to get camera calibration
    rospy.Subscriber("camera_info", CameraInfo, process_camera_info)

    global last_tf
    global latch_tf
    last_tf = rospy.Time.now()
    latch_tf = {}
    rospy.Subscriber("/tf", tf2_msgs.msg.TFMessage, process_tf)

    # subscribe to 2D image locations
    rospy.Subscriber("image_location", PointStamped, process_location)
    rospy.spin()

if __name__ == "__main__":
    image_to_target()


text:
#!/usr/bin/env python
"""
Natural Parameters Helper for Mean + Variance Exp Families
"""

# Import Modules
import numpy as np
from ep_clustering._utils import fix_docs
from ._exponential_family import ExponentialFamily
from scipy.special import digamma, multigammaln
from scipy.stats import invwishart, wishart

# Author Information
__author__ = "Christopher Aicher"

# Helper function for Inverse Wishart
def multidigamma(a, d):
    """ Returns psi_d(a), where psi_d is the multivariate digamma of d

    Calculation exploits the recursion:
        psi_d(a) = \sum_{i=1}^d psi_1(a + (1-i)/2)
    where psi_1 is the digamma function
    """
    if np.isscalar(a):
        return np.sum(digamma(a - 0.5*np.arange(d)),
            axis=-1)
    else:
        return np.sum(digamma(a[...,None] - 0.5*np.arange(d)),
            axis=-1)

@fix_docs
class NormalInverseWishartFamily(ExponentialFamily):
    """ Normal Inverse Wishart Family Site Approximation Class

    Natural Parameters:
        lambduh (double):
        mean_lambduh (num_dim ndarray):
        nu (double):
        psi_plus (num_dim by num_dim ndarray):

    Helper Functions:
        get_mean(): return expected mean
        get_mean_precision(): return expected mean-precision
        get_variance(): return expected variance
        get_precision(): return expected precision
        get_log_variance(): return expected log variance
    """
    def __init__(self, num_dim, log_scaling_coef=0.0,
            lambduh=0.0, mean_lambduh=None,
            nu=0.0, psi_plus=None):
        # Set Default mean_precision and precision
        if mean_lambduh is None:
            mean_lambduh = np.zeros(num_dim)
        if psi_plus is None:
            psi_plus = np.zeros((num_dim, num_dim))

        # Check Dimensions
        if np.size(mean_lambduh) != num_dim:
            raise ValueError("mean_lambduh must be size num_dim")
        if np.shape(psi_plus) != (num_dim, num_dim):
            if np.size(psi_plus) == num_dim:
                psi_plus = np.diag(psi_plus)
            else:
                raise ValueError("psi_plus must be size num_dim by num_dim")

        super(NormalInverseWishartFamily, self).__init__(
                num_dim=num_dim,
                log_scaling_coef=log_scaling_coef,
                lambduh=lambduh,
                mean_lambduh=mean_lambduh,
                nu=nu,
                psi_plus=psi_plus,
                )
        return

    def get_mean(self):
        """ Return the expected mean """
        if not self.is_valid_density():
            return np.NaN
        mean = self.natural_parameters['mean_lambduh'] / \
                self.natural_parameters['lambduh']
        return mean

    def get_mean_precision(self):
        """ Return the expected mean precision """
        if not self.is_valid_density():
            return np.NaN
        mean_precision = self.natural_parameters['nu'] * np.linalg.solve(
                self._get_psi(), self.get_mean())
        return mean_precision

    def get_variance(self):
        """ Return the expected variance (returns NaN if variance DNE)"""
        if not self.is_valid_density():
            return np.NaN
        nu = self.natural_parameters['nu']
        if nu - self.num_dim - 1 <= 0:
            variance = np.NaN
        else:
            variance = self._get_psi() / (nu - self.num_dim - 1)
        return variance

    def get_precision(self):
        """ Return the expected precision """
        if not self.is_valid_density():
            return np.NaN
        precision = self.natural_parameters['nu'] * \
                np.linalg.inv(self._get_psi())
        return precision

    def get_log_variance(self):
        """ Return the expected log det of variance """
        if not self.is_valid_density():
            return np.NaN
        log_det_psi = np.linalg.slogdet(self._get_psi())[1]
        log_det_var = log_det_psi - self.num_dim * np.log(2) - \
            multidigamma(a=self.natural_parameters['nu']/2.0, p=self.num_dim)
        return log_det_var

    def _get_psi(self):
        psi = self.natural_parameters['psi_plus'] - \
                np.outer(self.natural_parameters['mean_lambduh'],
                self.natural_parameters['mean_lambduh']) / \
                self.natural_parameters['lambduh']
        return psi

    def is_valid_density(self):
        if self.natural_parameters['lambduh'] < 1e-12:
            return False
        if self.natural_parameters['nu'] < self.num_dim - 1 + 1e-12:
            return False
        eigvals = np.linalg.eigvals(self._get_psi())
        if np.any(eigvals < 1e-12):
            return False
        return True

    def sample(self):
        self._check_is_valid_density()
        variance = invwishart(df=self.natural_parameters['nu'],
                scale=self._get_psi()).rvs()
        mean = np.random.multivariate_normal(mean=self.get_mean(),
                cov=variance/self.natural_parameters['lambduh'])
        return dict(mean=mean, variance=variance)

@fix_docs
class NormalWishartFamily(ExponentialFamily):
    """ Normal Wishart Family Site Approximation Class

    Canonical Parameters:
        mu (num_dim ndarray):
        psi (num_dim by num_dim ndarray):
        kappa (double): pseudo count on mu observations
        nu (double): pseudo count on psi observations

    Natural Parameters:
        kappa (double): kappa
        nu_minus (double): nu - num_dim
        eta (num_dim ndarray): kappa * mu
        zeta (num_dim by num_dim ndarray): psi + kappa * mu * mu.T

    Helper Functions:
        get_mean(): return expected mean
        get_mean_precision(): return expected mean-precision
        get_variance(): return expected variance
        get_precision(): return expected precision
        get_log_variance(): return expected log variance
    """
    def __init__(self, num_dim, log_scaling_coef=0.0,
            kappa=0.0, nu_minus=0.0, eta=None, zeta=None):
        # Set Default mean_precision and precision
        if eta is None:
            eta = np.zeros(num_dim)
        if zeta is None:
            zeta = np.zeros((num_dim, num_dim))

        # Check Dimensions
        if np.size(eta) != num_dim:
            raise ValueError("mean_lambduh must be size num_dim")
        if np.shape(zeta) != (num_dim, num_dim):
            if np.size(zeta) == num_dim:
                zeta = np.diag(zeta)
            else:
                raise ValueError("zeta must be size num_dim by num_dim")

        super(NormalWishartFamily, self).__init__(
                num_dim=num_dim,
                log_scaling_coef=log_scaling_coef,
                kappa=kappa,
                nu_minus=nu_minus,
                eta=eta,
                zeta=zeta,
                )
        return

    @staticmethod
    def from_mu_kappa_nu_psi(mu, kappa, nu, psi):
        """ Helper function for constructing from canonical parameters """
        num_dim = np.size(mu)
        if np.shape(psi) != (num_dim, num_dim):
            raise ValueError("psi must be size num_dim by num_dim")
        eta = kappa * mu
        zeta = psi + kappa * np.outer(mu, mu)
        return NormalWishartFamily(
                num_dim=num_dim,
                kappa=kappa,
                nu_minus=nu - num_dim,
                eta=eta,
                zeta=zeta,
                )

    def get_mean(self):
        """ Return the expected mean """
        if not self.is_valid_density():
            return np.NaN
        mean = self.natural_parameters['eta'] / \
                self.natural_parameters['kappa']
        return mean

    def get_mean_precision(self):
        """ Return the expected mean precision """
        if not self.is_valid_density():
            return np.NaN
        mean_precision = self._get_nu() * np.linalg.solve(
                self._get_psi(), self.get_mean())
        return mean_precision

    def get_variance(self):
        """ Return the expected variance (returns NaN if variance DNE)"""
        if not self.is_valid_density():
            return np.NaN
        nu = self._get_nu()
        if nu - self.num_dim - 1 <= 0:
            variance = np.NaN
        else:
            variance = self._get_psi() / (nu - self.num_dim - 1)
        return variance

    def get_precision(self):
        """ Return the expected precision """
        if not self.is_valid_density():
            return np.NaN
        precision = self._get_nu() * \
                np.linalg.inv(self._get_psi())
        return precision

    def get_log_precision(self):
        """ Return the expected log det of precision """
        if not self.is_valid_density():
            return np.NaN
        log_det_psi = np.linalg.slogdet(self._get_psi())[1]
        log_det_precision = -log_det_psi + self.num_dim * np.log(2) + \
            multidigamma(a=self._get_nu()/2.0, p=self.num_dim)
        return log_det_precision

    def get_log_variance(self):
        """ Return the expected log det of variance """
        if not self.is_valid_density():
            return np.NaN
        log_det_var = -1.0 * self.get_log_variance()
        return log_det_var

    def get_kappa_nu_mu_psi(self):
        """ Return Canonical parametrization """
        kappa = self.natural_parameters['kappa']
        mu = self.natural_parameters['eta'] / \
                self.natural_parameters['kappa']
        nu = self._get_nu()
        psi = self._get_psi()
        return [kappa, nu, mu, psi]

    def _get_psi(self):
        psi = self.natural_parameters['zeta'] - \
                np.outer(self.natural_parameters['eta'],
                self.natural_parameters['eta']) / \
                self.natural_parameters['kappa']
        return psi

    def _get_nu(self):
        return self.natural_parameters['nu_minus'] + self.num_dim

    def is_valid_density(self):
        if self.natural_parameters['kappa'] < 1e-12:
            return False
        if self.natural_parameters['nu_minus'] < 1e-12:
            return False
        eigvals = np.linalg.eigvals(self._get_psi())
        if np.any(eigvals < 1e-12):
            return False
        return True

    def logpartition(self):
        self._check_is_valid_density()
        nu = self._get_nu()
        psi = self._get_psi()
        kappa = self.natural_parameters['kappa']
        log_partition = 0.5 * self.num_dim * np.log(2*np.pi)
        log_partition += 0.5 * self.num_dim * nu * np.log(2)
        log_partition -= 0.5 * nu * np.linalg.slogdet(psi)[1]
        log_partition -= 0.5 * self.num_dim * np.log(kappa)
        log_partition += multigammaln(nu/2.0, self.num_dim)
        return log_partition

    def sample(self):
        self._check_is_valid_density()
        precision = wishart(df=self._get_nu(),
                            scale=np.linalg.inv(self._get_psi())).rvs()
        L = np.linalg.cholesky(precision) * \
                np.sqrt(self.natural_parameters['kappa'])
        mean = self.get_mean() + \
                np.linalg.solve(L.T, np.random.normal(size=self.num_dim))
        return dict(mean=mean, precision=precision)

reason: import numpy,from scipy


text:
## Largely Based on MSCN - Will separate out ot make sure we use MSCN directly

import csv
import torch
from torch.utils.data import dataset
from torch.utils.data import DataLoader
import argparse
import time
import os
#from models. import *
import pandas as pd
import numpy as np
import json
from mscn.util import *
import sqlparse
import re
#from util import *
from model.model import *
import matplotlib.pyplot as plt

def unnormalize_torch(vals, min_val, max_val):
    vals = (vals * (max_val - min_val)) + min_val
    return torch.exp(vals)

def qerror_loss(preds, targets, min_val, max_val):
    qerror = []
    predsn = preds.detach().numpy()
    targetsn = targets.detach().numpy()
    preds = unnormalize_torch(preds, min_val, max_val)
    targets = unnormalize_torch(targets, min_val, max_val)
    plt.figure(figsize=(6,6))
    plt.scatter(targetsn,predsn,alpha=0.05)
    plt.xlabel('Actual (normalized) cardinality')
    plt.ylabel('Predicted (normalized) cardinality')
    plt.plot([0,1],[0,1],color='black')
    plt.grid()
    plt.savefig('mscn_synth.png')
    for i in range(len(targets)):
        if (preds[i] > targets[i]).cpu().data.numpy()[0]:
            qerror.append(preds[i] / targets[i])
        else:
            qerror.append(targets[i] / preds[i])
    return torch.mean(torch.cat(qerror))

def predict(model, data_loader, cuda):
    preds = []
    t_total = 0.

    model.eval()
    for batch_idx, data_batch in enumerate(data_loader):

        samples, predicates, joins, targets, sample_masks, predicate_masks, join_masks = data_batch

        #if cuda:
        #    samples, predicates, joins, targets = samples.cuda(), predicates.cuda(), joins.cuda(), targets.cuda()
        #    sample_masks, predicate_masks, join_masks = sample_masks.cuda(), predicate_masks.cuda(), join_masks.cuda()
        #samples, predicates, joins, targets = Variable(samples), Variable(predicates), Variable(joins), Variable(
        #    targets)
        #sample_masks, predicate_masks, join_masks = Variable(sample_masks), Variable(predicate_masks), Variable(
        #    join_masks)

        t = time.time()
        outputs = model(samples, predicates, joins, sample_masks, predicate_masks, join_masks)
        t_total += time.time() - t

        for i in range(outputs.data.shape[0]):
            preds.append(outputs.data[i])

    return preds, t_total

def print_qerror(preds_unnorm, labels_unnorm):
    qerror = []
    for i in range(len(preds_unnorm)):
        if preds_unnorm[i] > float(labels_unnorm[i]):
            qerror.append(preds_unnorm[i] / float(labels_unnorm[i]))
        else:
            qerror.append(float(labels_unnorm[i]) / float(preds_unnorm[i]))

    print("Median: {}".format(np.median(qerror)))
    print("90th percentile: {}".format(np.percentile(qerror, 90)))
    print("95th percentile: {}".format(np.percentile(qerror, 95)))
    print("99th percentile: {}".format(np.percentile(qerror, 99)))
    print("Max: {}".format(np.max(qerror)))
    print("Mean: {}".format(np.mean(qerror)))

def train_and_predict_marc(filename, num_queries, hid_units):
    num_materialized_samples = 1000
    dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data = get_train_datasets(
        filename, num_queries, num_materialized_samples)
    table2vec, column2vec, op2vec, join2vec = dicts
    #print(table2vec)
    #print(column2vec)
    #print(op2vec)
    #print(join2vec)

def train_and_predict(filename, num_queries, num_epochs, batch_size, hid_units, cuda):
    # Load training and validation data
    num_materialized_samples = 1000
    dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data = get_train_datasets(
        filename, num_queries, num_materialized_samples)
    table2vec, column2vec, op2vec, join2vec = dicts

    # Train model
    sample_feats = len(table2vec) # + num_materialized_samples
    predicate_feats = len(column2vec) + len(op2vec) + 1
    join_feats = len(join2vec)

    model = SetConv(sample_feats, predicate_feats, join_feats, hid_units)

    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

    if cuda:
        model.cuda()

    train_data_loader = DataLoader(train_data, batch_size=batch_size)
    test_data_loader = DataLoader(test_data, batch_size=batch_size)

    t = time.time()
    model.train()
    for epoch in range(num_epochs):
        loss_total = 0.

        for batch_idx, data_batch in enumerate(train_data_loader):

            samples, predicates, joins, targets, sample_masks, predicate_masks, join_masks = data_batch

            optimizer.zero_grad()
            outputs = model(samples, predicates, joins, sample_masks, predicate_masks, join_masks)
            loss = qerror_loss(outputs, targets.float(), min_val, max_val)
            loss_total += loss.item()
            loss.backward()
            optimizer.step()

        print("Epoch {}, loss: {}".format(epoch, loss_total / len(train_data_loader)))

    train_time = time.time() - t
    print("Training time per query: {}".format(train_time / num_queries ))
    # Get final training and validation set predictions
    preds_train, t_total = predict(model, train_data_loader, cuda)
    print("Prediction time per training sample: {}".format(t_total / len(labels_train) * 1000))

    preds_test, t_total = predict(model, test_data_loader, cuda)
    print("Prediction time per validation sample: {}".format(t_total / len(labels_test) * 1000))

    # Unnormalize
    preds_train_unnorm = unnormalize_labels(preds_train, min_val, max_val)
    labels_train_unnorm = unnormalize_labels(labels_train, min_val, max_val)

    preds_test_unnorm = unnormalize_labels(preds_test, min_val, max_val)
    labels_test_unnorm = unnormalize_labels(labels_test, min_val, max_val)

    # Print metrics
    print("\nQ-Error training set:")
    print_qerror(preds_train_unnorm, labels_train_unnorm)

    print("\nQ-Error validation set:")
    print_qerror(preds_test_unnorm, labels_test_unnorm)
    print("")

    # Load test data
    #file_name = "workloads/" + workload_name
    #joins, predicates, tables, samples, label = load_data(file_name, num_materialized_samples)

    ## Get feature encoding and proper normalization
    #samples_test = encode_samples(tables, samples, table2vec)
    #predicates_test, joins_test = encode_data(predicates, joins, column_min_max_vals, column2vec, op2vec, join2vec)
    #labels_test, _, _ = normalize_labels(label, min_val, max_val)

    #print("Number of test samples: {}".format(len(labels_test)))

    #max_num_predicates = max([len(p) for p in predicates_test])
    #max_num_joins = max([len(j) for j in joins_test])

    ## Get test set predictions
    #test_data = make_dataset(samples_test, predicates_test, joins_test, labels_test, max_num_joins, max_num_predicates)
    #test_data_loader = DataLoader(test_data, batch_size=batch_size)

    #preds_test, t_total = predict(model, test_data_loader, cuda)
    #print("Prediction time per test sample: {}".format(t_total / len(labels_test) * 1000))

    ## Unnormalize
    #preds_test_unnorm = unnormalize_labels(preds_test, min_val, max_val)

    ## Print metrics
    #print("\nQ-Error " + workload_name + ":")
    #print_qerror(preds_test_unnorm, label)

    ## Write predictions
    #file_name = "results/predictions_" + workload_name + ".csv"
    #os.makedirs(os.path.dirname(file_name), exist_ok=True)
    #with open(file_name, "w") as f:
    #    for i in range(len(preds_test_unnorm)):
    #        f.write(str(preds_test_unnorm[i]) + "," + label[i] + "\n")

def conv_to_mscn_format(file_name):
    label = []
    queries = []
    joins = []
    predicates = []
    tables = []
    fw = open(file_name+".csv", 'w')
    with open(file_name, 'r') as f:
        data_raw = list(list(rec) for rec in csv.reader(f, delimiter=';'))
        for row in data_raw:
            queries.append(row[0])
            label.append(row[1])
            parsed = sqlparse.parse(row[0])
            i=0
            rowtables=[]
            rowjoins=[]
            rowpreds=[]
            #print("Query:", str(row[0]))
            for token in parsed[0].tokens:
                #print(token)
                if i==6: #table names
                    #print(token)
                    for t in str(token).split(','):
                        rowtables.append(t)
                    #fw.write(str(token) + '#')
                if i==8: #joins, predicates
                    #print(token)
                    conds = str(token).split()
                    for cond in conds:
                        if cond == 'WHERE' or cond == 'AND':
                            continue
                        condsplit = re.split('<|>|=',cond)
                        op = re.search('[<=>]',cond)
                        if(condsplit[1].isnumeric()):
                            rowpreds.append([condsplit[0], op[0], condsplit[1]])
                        else:
                            rowjoins.append(cond)
                i=i+1
            tables.append(rowtables)
            predicates.append(rowpreds)
            joins.append(rowjoins)
    return tables, joins, predicates, label

def load_data(file_name, num_materialized_samples):
    joins = []
    predicates = []
    tables = []
    samples = []
    label = []
    queries = []
    # Load queries
    with open(file_name + ".sql", 'rU') as f:
        data_raw = list(list(rec) for rec in csv.reader(f, delimiter=';'))
        for row in data_raw:
            queries.append(row[0])
            label.append(row[1])
    print("Loaded queries")
    return queries, label

def load_and_encode_train_data(file_name, num_queries, num_materialized_samples):
    file_name_column_min_max_vals = "column_min_max_vals.csv"

    tables, joins, predicates, label = conv_to_mscn_format(file_name)

    # Get column name dict
    column_names = get_all_column_names(predicates)
    #print("Column Names", column_names)
    column2vec, idx2column = get_set_encoding(column_names)

    # Get table name dict
    table_names = get_all_table_names(tables)
    table2vec, idx2table = get_set_encoding(table_names)

    # Get operator name dict
    operators = get_all_operators(predicates)
    op2vec, idx2op = get_set_encoding(operators)

    # Get join name dict
    join_set = get_all_joins(joins)
    join2vec, idx2join = get_set_encoding(join_set)

    # Get min and max values for each column
    with open(file_name_column_min_max_vals, 'r') as f:
        data_raw = list(list(rec) for rec in csv.reader(f, delimiter=','))
        column_min_max_vals = {}
        for i, row in enumerate(data_raw):
            if i == 0:
                continue
            column_min_max_vals[row[0]] = [float(row[1]), float(row[2])]
    tables_enc = encode_tables(tables, table2vec)
    predicates_enc, joins_enc = encode_data(predicates, joins, column_min_max_vals, column2vec, op2vec, join2vec)
    label_norm, min_val, max_val = normalize_labels(label)

    num_train = int(num_queries * 0.9)
    num_test = num_queries - num_train

    tables_train = tables_enc[:num_train]
    predicates_train = predicates_enc[:num_train]
    joins_train = joins_enc[:num_train]
    labels_train = label_norm[:num_train]

    tables_test = tables_enc[num_train:num_train + num_test]
    predicates_test = predicates_enc[num_train:num_train + num_test]
    joins_test = joins_enc[num_train:num_train + num_test]
    labels_test = label_norm[num_train:num_train + num_test]

    print("Number of training samples: {}".format(len(labels_train)))
    print("Number of validation samples: {}".format(len(labels_test)))

    max_num_joins = max(max([len(j) for j in joins_train]), max([len(j) for j in joins_test]))
    max_num_predicates = max(max([len(p) for p in predicates_train]), max([len(p) for p in predicates_test]))

    dicts = [table2vec, column2vec, op2vec, join2vec]
    train_data = [tables_train, predicates_train, joins_train]
    test_data = [tables_test, predicates_test, joins_test]
    return dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data

def make_dataset(samples, predicates, joins, labels, max_num_joins, max_num_predicates):
    """Add zero-padding and wrap as tensor dataset."""

    sample_masks = []
    sample_tensors = []
    for sample in samples:
        sample_tensor = np.vstack(sample)
        num_pad = max_num_joins + 1 - sample_tensor.shape[0]
        sample_mask = np.ones_like(sample_tensor).mean(1, keepdims=True)
        sample_tensor = np.pad(sample_tensor, ((0, num_pad), (0, 0)), 'constant')
        sample_mask = np.pad(sample_mask, ((0, num_pad), (0, 0)), 'constant')
        sample_tensors.append(np.expand_dims(sample_tensor, 0))
        sample_masks.append(np.expand_dims(sample_mask, 0))
    sample_tensors = np.vstack(sample_tensors)

    #print("Sample Tensor,:", str(sample_tensors))
    sample_tensors = torch.FloatTensor(sample_tensors)
    sample_masks = np.vstack(sample_masks)
    #print("Sample Masks,:", str(sample_masks))
    sample_masks = torch.FloatTensor(sample_masks)

    predicate_masks = []
    predicate_tensors = []
    for predicate in predicates:
        predicate_tensor = np.vstack(predicate)
        num_pad = max_num_predicates - predicate_tensor.shape[0]
        predicate_mask = np.ones_like(predicate_tensor).mean(1, keepdims=True)
        predicate_tensor = np.pad(predicate_tensor, ((0, num_pad), (0, 0)), 'constant')
        predicate_mask = np.pad(predicate_mask, ((0, num_pad), (0, 0)), 'constant')
        predicate_tensors.append(np.expand_dims(predicate_tensor, 0))
        predicate_masks.append(np.expand_dims(predicate_mask, 0))
    predicate_tensors = np.vstack(predicate_tensors)
    #print("Predicate Tensor,:", str(predicate_tensors))
    predicate_tensors = torch.FloatTensor(predicate_tensors)
    predicate_masks = np.vstack(predicate_masks)
    #print("Predicate Masks,:", str(predicate_masks))
    predicate_masks = torch.FloatTensor(predicate_masks)

    join_masks = []
    join_tensors = []
    for join in joins:
        join_tensor = np.vstack(join)
        num_pad = max_num_joins - join_tensor.shape[0]
        join_mask = np.ones_like(join_tensor).mean(1, keepdims=True)
        join_tensor = np.pad(join_tensor, ((0, num_pad), (0, 0)), 'constant')
        join_mask = np.pad(join_mask, ((0, num_pad), (0, 0)), 'constant')
        join_tensors.append(np.expand_dims(join_tensor, 0))
        join_masks.append(np.expand_dims(join_mask, 0))
    join_tensors = np.vstack(join_tensors)
    #print("Join Tensor,:", str(join_tensors))
    join_tensors = torch.FloatTensor(join_tensors)
    join_masks = np.vstack(join_masks)
    #print("Join Masks,:", str(join_masks))
    join_masks = torch.FloatTensor(join_masks)

    target_tensor = torch.FloatTensor(labels)

    return dataset.TensorDataset(sample_tensors, predicate_tensors, join_tensors, target_tensor, sample_masks,
                                 predicate_masks, join_masks)

def get_train_datasets(file_name, num_queries, num_materialized_samples):
    dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data = load_and_encode_train_data(file_name, num_queries, num_materialized_samples)
    train_dataset = make_dataset(*train_data, labels=labels_train, max_num_joins=max_num_joins,
                                 max_num_predicates=max_num_predicates)
    print("Created TensorDataset for training data")
    test_dataset = make_dataset(*test_data, labels=labels_test, max_num_joins=max_num_joins,
                                max_num_predicates=max_num_predicates)
    print("Created TensorDataset for validation data")
    return dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_dataset, test_dataset

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--sql", help=".sql file with queries, cardinalities", type=str)
    parser.add_argument("--epochs", help="number of epochs (default: 10)", type=int, default=100)
    parser.add_argument("--queries", help="number of queries (default: 1000)", type=int, default=100)
    parser.add_argument("--batch", help="batch size (default: 1024)", type=int, default=1024)
    parser.add_argument("--hid", help="number of hidden units (default: 256)", type=int, default=256)
    args = parser.parse_args()
    tables=[]
    labels=[]
    predicates=[]
    joins=[]
    #tables, joins, predicates, labels = conv_to_mscn_format(args.sql)
    #queries, label = load_data(args.sql, 10)
    num_queries=args.queries
    #dicts, column_min_max_vals, min_val, max_val, labels_train, labels_test, max_num_joins, max_num_predicates, train_data, test_data = get_train_datasets(args.sql, num_queries, 1000)
    print("Number of queries:", num_queries)
    train_and_predict(args.sql, num_queries, args.epochs, args.batch, args.hid, False)

if __name__ == "__main__":
    main()


text:
#
# In this example, I use the official data from the Iris flower classification:
# Https://en.wikipedia.org/wiki/Iris_(plant)
# This version is tensorflow solution which provides an official source of data that should be integrated with Wikipedia.
# This sample will first list the "features" and the "labels" that will be classified as "classification",
# which is the iris of what species it belongs to.
#
import config
from tensorflow.contrib import learn
from tensorflow.contrib import layers
import numpy as np
from numpy import delete

train_steps = 1500

# Get data for training.
iris_dataset = learn.datasets.load_iris()

# Prepare train-data
train_x = iris_dataset.data
train_y = iris_dataset.target
feature_count = train_x.shape[1]  # Four features.
feature_columns = [layers.real_valued_column("", dimension=feature_count)]

positions_of_collection_to_delete = config.positions_of_collection_to_delete
# Remove rows on iris_dataset.data and copy for training data. 0 -> rows.
train_x = delete(iris_dataset.data, positions_of_collection_to_delete, 0)
# axis = None for remove elements in 1-D array.
train_y = delete(iris_dataset.target, positions_of_collection_to_delete)

# Prepare test-data
test_x = iris_dataset.data[positions_of_collection_to_delete]
test_y = iris_dataset.target[positions_of_collection_to_delete]

# Train model
dnn_classifier = learn.DNNClassifier(feature_columns=feature_columns,
                                     hidden_units=[10, 10, 10],
                                     n_classes=3)
dnn_classifier.fit(train_x, train_y, steps=train_steps)

# Accuracy
evl = dnn_classifier.evaluate(test_x, test_y)
print("\n👍 evaluate:\nloss: {:>5.2f}\naccuracy: {:>5.2f}\nglobal_step: {:>5.2f}\n\n".format(
    evl["loss"], evl["accuracy"], evl["global_step"]))

# Predication
try_data = np.array(
    [[5.1, 2.5, 3., 1.1], [6.1, 3., 4.6, 1.4], [5.6, 3., 4.1, 1.3]])
print("\n👍 predict:\n{}\n\nPredict:\n{}\n\n".format(
    try_data,
    list(dnn_classifier.predict(try_data))
))

max_stars_repo_name: XinyueZ/some-python-codes

max_issues_repo_name: XinyueZ/some-python-codes

max_forks_repo_name: XinyueZ/some-python-codes

reason: import numpy,from numpy


text:
"""
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import unittest
import os
import numpy as np

import oneflow as flow
import oneflow.unittest

x = np.array(
    [
        [
            0.21490018,
            0.22043167,
            0.1605895,
            0.25424683,
            0.12975895,
            0.49967155,
            0.04753795,
            0.7518577,
            0.38964537,
            0.01955934,
        ],
        [
            0.16392729,
            0.41410774,
            0.05424517,
            0.7668146,
            0.08050849,
            0.5763975,
            0.42364502,
            0.4950619,
            0.9608427,
            0.11889187,
        ],
    ]
)

y = np.array(
    [
        [
            0.9903706,
            0.11213686,
            0.29525927,
            0.79380244,
            0.70357895,
            0.6950597,
            0.52552456,
            0.32304054,
            0.6997739,
            0.15671141,
        ],
        [
            0.76867193,
            0.59983397,
            0.07774717,
            0.07815815,
            0.30385414,
            0.7366552,
            0.4607681,
            0.40554753,
            0.8290172,
            0.8405671,
        ],
        [
            0.8900324,
            0.5274955,
            0.80989295,
            0.71331054,
            0.8076364,
            0.94833183,
            0.04778554,
            0.23992656,
            0.57683426,
            0.81757474,
        ],
    ]
)

class MyModule1(flow.nn.Module):
    def __init__(self, weight):
        assert isinstance(weight, flow._oneflow_internal.Tensor)
        super().__init__()
        self.weight = flow.nn.Parameter(weight)
        self.activation = flow.nn.ReLU()

    def forward(self, x):
        # print(f"x shape: {x.shape}, placement: {x.placement}, sbp: {x.sbp}")
        # print(
        #     f"weight shape: {self.weight.shape}, placement: {self.weight.placement}, sbp: {self.weight.sbp}"
        # )
        y = flow._C.matmul(x, self.weight, transpose_b=True)
        # print(f"y shape: {y.shape}, placement: {y.placement}, sbp: {y.sbp}")
        if y.is_global:
            y = y.to_global(sbp=flow.sbp.broadcast)
            # print(f"post y shape: {y.shape}, placement: {y.placement}, sbp: {y.sbp}")
        return self.activation(y)

class MyModule2(flow.nn.Module):
    def __init__(self, weight):
        assert isinstance(weight, flow._oneflow_internal.Tensor)
        super().__init__()
        self.weight = flow.nn.Parameter(weight)
        self.activation = flow.nn.ReLU()

    def forward(self, x):
        # print(f"weight shape: {self.weight.shape}, placement: {self.weight.placement}, sbp: {self.weight.sbp}")
        if self.weight.is_global:
            y = self.weight.to_global(grad_sbp=flow.sbp.broadcast)
        z = flow._C.matmul(y, x, transpose_b=True)
        out = self.activation(z).sum()
        if self.weight.is_global:
            out = out.to_global(sbp=flow.sbp.broadcast)
        return out

class MyModule3(flow.nn.Module):
    def __init__(self, transpose_a=False, transpose_b=False):
        super().__init__()
        self.activation = flow.nn.ReLU()
        self.transpose_a = transpose_a
        self.transpose_b = transpose_b

    def forward(self, x, y):
        z = flow._C.matmul(x, y, self.transpose_a, self.transpose_b)
        if z.is_global:
            z = z.to_global(sbp=flow.sbp.broadcast)
        return self.activation(z)

class ConsistentToModule(flow.nn.Module):
    def __init__(self, device="cuda"):
        super().__init__()
        self.device = device

    def forward(self, x):
        return x.to(self.device)

class FreeTensorModule(flow.nn.Module):
    def __init__(self, shape, placement, sbp):
        super().__init__()
        self.shape = shape
        self.placement = placement
        self.sbp = sbp

    def forward(self, x):
        y = flow.ones(
            self.shape, dtype=flow.float32, placement=self.placement, sbp=self.sbp
        )
        return flow._C.matmul(x, y, transpose_b=True)

class ToPlacementModule(flow.nn.Module):
    def __init__(self, placement):
        super().__init__()
        self.placement = placement

    def forward(self, x):
        return x.to_global(placement=self.placement)

class MyGraph(flow.nn.Graph):
    def __init__(self, module, optimizer=None):
        super().__init__()
        self.module = module
        if optimizer is not None:
            self.add_optimizer(optimizer)

    def build(self, *arg):
        y = self.module(*arg)
        if self.config.training:
            y.backward()
        return y

@unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test cpu cases")
@flow.unittest.skip_unless_1n2d()
class ToGlobalGraphTestCase(oneflow.unittest.TestCase):
    # @unittest.skipIf(True, "")
    def test_fwd_P2B(test_case):
        """ compare eager fwd and lazy bwd
        """
        rank = flow.env.get_rank()
        # pid = os.getpid()
        # print(f"[{pid}][{rank}] ToGlobalGraphTestCase.test_fwd_P2B")

        local_x = flow.tensor(x, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))
        local_y = flow.tensor(y, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))

        z = flow._C.matmul(
            flow.cat([local_x, local_x], dim=1),
            flow.cat([local_y, local_y], dim=1),
            transpose_b=True,
        )
        z = flow._C.relu(z)
        # print(f"z shape: {z.shape}, device: {z.device}")
        # print(z.numpy())

        placement = flow.placement("cuda", ranks=[0, 1])
        sbp = flow.sbp.split(1)
        c_x = local_x.to_global(placement=placement, sbp=sbp)
        c_y = local_y.to_global(placement=placement, sbp=sbp)

        # print(f"c_x shape: {c_x.shape}, placement: {c_x.placement}, sbp: {c_x.sbp}")
        # print(f"c_y shape: {c_y.shape}, placement: {c_y.placement}, sbp: {c_y.sbp}")

        m = MyModule1(c_y)
        g = MyGraph(m)

        g_z = g(c_x)
        # print(f"g_z shape: {g_z.shape}, placement: {g_z.placement}, sbp: {g_z.sbp}")
        # print(g_z.to_local().numpy())
        test_case.assertTrue(np.allclose(z.numpy(), g_z.to_local().numpy()))

    # @unittest.skipIf(True, "")
    def test_bwd_P2B(test_case):
        """ compare eager bwd and lazy bwd
        """
        rank = flow.env.get_rank()
        # pid = os.getpid()
        # print(f"[{pid}][{rank}] ToGlobalGraphTestCase.test_bwd_P2B")

        local_x = flow.tensor(x, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))
        local_y = flow.tensor(y, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))

        z = flow._C.matmul(
            local_y, flow.cat([local_x, local_x], dim=0), transpose_b=True,
        )
        z = flow._C.relu(z)
        z = z.sum()

        placement = flow.placement("cuda", ranks=[0, 1])
        c_x = local_x.to_global(placement=placement, sbp=flow.sbp.split(0))
        c_y = local_y.to_global(placement=placement, sbp=flow.sbp.broadcast)

        m = MyModule2(c_y)
        optimizer = flow.optim.SGD(m.parameters(), lr=1.0)
        g = MyGraph(m, optimizer)

        g_z = g(c_x)
        # print(f"g_z shape: {g_z.shape}, placement: {g_z.placement}, sbp: {g_z.sbp}")
        test_case.assertTrue(g_z.is_global)
        test_case.assertTrue(g_z.sbp[0] == flow.sbp.broadcast)
        # S(1) -> B not supported yet
        # c_z = g_z.to_global(sbp=flow.sbp.broadcast)
        # print(f"c_z shape: {c_z.shape}, placement: {c_z.placement}, sbp: {c_z.sbp}")
        test_case.assertTrue(np.allclose(z.numpy(), g_z.to_local().numpy()))

        e_y = c_y.detach()
        # print(f"e_y shape: {e_y.shape}, placement: {e_y.placement}, sbp: {e_y.sbp}")
        e_m = MyModule2(e_y)
        e_z = e_m(c_x)
        # print(f"e_z shape: {e_z.shape}, placement: {e_z.placement}, sbp: {e_z.sbp}")
        e_z.backward()

        test_case.assertTrue(
            np.allclose(c_y.to_local().numpy(), e_y.to_local().numpy())
        )

    # @unittest.skipIf(True, "")
    def test_multi_graph(test_case):
        """ compare two lazy fwd
        """
        rank = flow.env.get_rank()
        # pid = os.getpid()
        # print(f"[{pid}][{rank}] ToGlobalGraphTestCase.test_multi_graph")

        local_x = flow.tensor(x, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))
        local_y = flow.tensor(y, dtype=flow.float32, device=flow.device(f"cuda:{rank}"))

        placement = flow.placement("cuda", ranks=[0, 1])
        x1 = local_x.to_global(placement=placement, sbp=flow.sbp.broadcast)
        y1 = local_y.to_global(placement=placement, sbp=flow.sbp.broadcast)
        # B * B -> B -> B
        m1 = MyModule3(transpose_b=True)
        g1 = MyGraph(m1)

        slice_obj = slice(
            int(rank * local_x.shape[0] / 2), int((rank + 1) * local_x.shape[0] / 2)
        )
        x2 = local_x[slice_obj, :]
        x2 = x2.to_global(placement=placement, sbp=flow.sbp.split(0))
        y2 = local_y.to_global(placement=placement, sbp=flow.sbp.broadcast)
        # S(0) * B -> S(0) -> B
        m2 = MyModule3(transpose_b=True)
        g2 = MyGraph(m2)

        x3 = local_x[
            :, int(rank * local_x.shape[1] / 2) : int((rank + 1) * local_x.shape[1] / 2)
        ]
        x3 = x3.to_global(placement=placement, sbp=flow.sbp.split(1))
        y3 = local_y[
            :, int(rank * local_y.shape[1] / 2) : int((rank + 1) * local_y.shape[1] / 2)
        ]
        y3 = y3.to_global(placement=placement, sbp=flow.sbp.split(1))
        # S(1) * S(0) -> P -> B
        m3 = MyModule3(transpose_b=True)
        g3 = MyGraph(m3)

        z1 = g1(x1, y1)
        # print(f"z1 shape: {z1.shape}, placement: {z1.placement}, sbp: {z1.sbp}")
        # print(z1.to_local().numpy())
        z2 = g2(x2, y2)
        # print(f"z2 shape: {z2.shape}, placement: {z2.placement}, sbp: {z2.sbp}")
        # print(z2.to_local().numpy())
        z3 = g3(x3, y3)
        # print(f"z3 shape: {z3.shape}, placement: {z3.placement}, sbp: {z3.sbp}")
        # print(z3.to_local().numpy())

        test_case.assertTrue(np.allclose(z1.to_local().numpy(), z2.to_local().numpy()))
        test_case.assertTrue(np.allclose(z1.to_local().numpy(), z3.to_local().numpy()))

    # @unittest.skipIf(True, "")
    def test_global_to(test_case):
        c_x = flow.ones(
            (4, 3), placement=flow.placement("cpu", ranks=[0, 1]), sbp=flow.sbp.split(0)
        )

        global_to = ConsistentToModule("cuda")
        g_global_to = MyGraph(global_to)

        e = global_to(c_x)
        test_case.assertTrue(e.is_cuda)
        test_case.assertTrue(e.is_global)
        test_case.assertTrue(e.sbp[0] == flow.sbp.split(0))

        g = g_global_to(c_x)
        test_case.assertTrue(g.is_cuda)
        test_case.assertTrue(g.is_global)
        test_case.assertTrue(g.sbp[0] == flow.sbp.split(0))

        test_case.assertTrue(np.allclose(e.to_local().numpy(), g.to_local().numpy()))

    # @unittest.skipIf(True, "")
    def test_free_tensor_to_global(test_case):
        local_x = flow.tensor(x, dtype=flow.float32, device="cpu")
        placement = flow.placement("cuda", ranks=[0, 1])
        c_x = local_x.to_global(placement, flow.sbp.split(0))

        m = FreeTensorModule((3, 10), placement, flow.sbp.broadcast)
        g = MyGraph(m)

        eager_out = m(c_x)
        test_case.assertTrue(eager_out.is_cuda)
        test_case.assertTrue(eager_out.is_global)
        test_case.assertTrue(eager_out.sbp[0] == flow.sbp.split(0))

        graph_out = g(c_x)
        test_case.assertTrue(graph_out.is_cuda)
        test_case.assertTrue(graph_out.is_global)
        test_case.assertTrue(graph_out.sbp[0] == flow.sbp.split(0))

        test_case.assertTrue(
            np.allclose(eager_out.to_local().numpy(), graph_out.to_local().numpy())
        )

    # @unittest.skipIf(True, "")
    def test_to_placement(test_case):
        rank = flow.env.get_rank()
        # pid = os.getpid()
        # print(f"[{pid}][{rank}] ToGlobalGraphTestCase.test_to_placement")

        if rank == 0:
            x = flow.ones((2, 3), dtype=flow.float32)
        elif rank == 1:
            x = flow.empty(tuple())
        else:
            raise ValueError

        c_x = x.to_global(
            placement=flow.placement("cpu", ranks=[0]), sbp=flow.sbp.broadcast
        )
        # print(f"c_x shape: {c_x.shape}, placement: {c_x.placement}, sbp: {c_x.sbp}")

        p1 = flow.placement("cpu", ranks=[0, 1])
        m1 = ToPlacementModule(p1)
        g1 = MyGraph(m1)
        y1 = g1(c_x)

        # print(f"y1 shape: {y1.shape}, placement: {y1.placement}, sbp: {y1.sbp}")
        test_case.assertTrue(y1.placement == p1)
        test_case.assertTrue(y1.sbp[0] == flow.sbp.broadcast)
        test_case.assertTrue(y1.to_local().numpy().mean() == 1.0)

        p2 = flow.placement("cuda", ranks=[0, 1])
        m2 = ToPlacementModule(p2)
        g2 = MyGraph(m2)
        y2 = g2(y1)

        # print(f"y2 shape: {y2.shape}, placement: {y2.placement}, sbp: {y2.sbp}")
        test_case.assertTrue(y2.placement == p2)
        test_case.assertTrue(y2.sbp[0] == flow.sbp.broadcast)
        test_case.assertTrue(y2.to_local().numpy().mean() == 1.0)

    # @unittest.skipIf(True, "")
    def test_to_dtype(test_case):
        x = flow.ones((2, 3), dtype=flow.int32, device="cpu")

        placement = flow.placement("cpu", ranks=[0, 1])
        c_x = flow.ones(
            (2, 3), dtype=flow.int32, placement=placement, sbp=flow.sbp.broadcast
        )

        class CastModule(flow.nn.Module):
            def __init__(self, dtype):
                super().__init__()
                self.dtype = dtype

            def forward(self, x):
                return x.to(dtype=self.dtype)

        m = CastModule(flow.float32)
        g = MyGraph(m)

        e_x = m(x)
        e_c_x = m(c_x)
        # NOTE(chengcheng):
        #   There are two BUG in this test script:
        #   1. first call and second call input tensor meta is NOT same
        #   2. nn.Graph NOT support local input with multi-rank yet.
        # g_x = g(x)
        g_c_x = g(c_x)

        test_case.assertTrue(e_x.dtype == flow.float32)
        # test_case.assertTrue(g_x.dtype == flow.float32)
        test_case.assertTrue(e_c_x.dtype == flow.float32)
        test_case.assertTrue(g_c_x.dtype == flow.float32)

class MyModule5(flow.nn.Module):
    def __init__(self, transpose_a=False, transpose_b=False, sbp=[]):
        super().__init__()
        self.transpose_a = transpose_a
        self.transpose_b = transpose_b
        self.sbp = sbp

    def forward(self, x, y):
        z = flow._C.matmul(x, y, self.transpose_a, self.transpose_b)
        assert z.is_global
        assert len(z.sbp) == len(self.sbp)
        return z.to_global(sbp=self.sbp)

@unittest.skipIf(True, "")
@unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test cpu cases")
@flow.unittest.skip_unless_1n4d()
class ToGlobal2DGraphTestCase(oneflow.unittest.TestCase):
    def test_matmul(test_case):
        placement = flow.placement("cuda", ranks=[[0, 1], [2, 3]])
        x = flow.ones(
            (4, 6), placement=placement, sbp=[flow.sbp.split(0), flow.sbp.split(1)]
        )
        y = flow.ones(
            (4, 6), placement=placement, sbp=[flow.sbp.broadcast, flow.sbp.split(1)]
        )
        z = flow._C.matmul(x, y, transpose_b=True)
        print(f"z shape: {z.shape}, placement: {z.placement}, sbp: {z.sbp}")

        # m = MyModule5(transpose_b=True, sbp=[flow.sbp.split(0), flow.sbp.broadcast])
        # z = m(x, y)
        # print(f"z shape: {z.shape}, placement: {z.placement}, sbp: {z.sbp}")

@flow.unittest.skip_unless_1n4d()
@unittest.skipIf(os.getenv("ONEFLOW_TEST_CPU_ONLY"), "only test cpu cases")
class TestLazy1dTo2dConsistent(flow.unittest.TestCase):
    def test_lazy_1d_to_2d_sbp(test_case):
        P_1d = flow.placement(
            device_type="cuda", device_ids={0: range(4)}, hierarchy=(4,)
        )
        P_2d = flow.placement(
            device_type="cuda", device_ids={0: range(4)}, hierarchy=(2, 2)
        )
        B = flow.sbp.broadcast

        class Test1dTo2dModule(flow.nn.Module):
            def forward(self, x):
                return x.to_global(placement=P_2d, sbp=[B, B])

        class Test1dTo2dGraph(flow.nn.Graph):
            def __init__(self, model):
                super().__init__()
                self.model = model

            def build(self, x):
                return self.model(x)

        class Test2dTo1dModule(flow.nn.Module):
            def forward(self, x):
                return x.to_global(placement=P_1d, sbp=[B])

        class Test2dTo1dGraph(flow.nn.Graph):
            def __init__(self, model):
                super().__init__()
                self.model = model

            def build(self, x):
                return self.model(x)

        model_1d_to_2d = Test1dTo2dModule()
        graph_1d_to_2d = Test1dTo2dGraph(model_1d_to_2d)

        x = flow.zeros(4, 4, 4, 4, sbp=[B, B], placement=P_2d)
        x = x.to_global(placement=P_1d, sbp=[B])
        test_case.assertTrue(x.sbp == (B,))
        test_case.assertTrue(x.placement == P_1d)
        y = graph_1d_to_2d(x)
        test_case.assertTrue(y.sbp == (B, B))
        test_case.assertTrue(y.placement == P_2d)

        model_2d_to_1d = Test2dTo1dModule()
        graph_2d_to_1d = Test2dTo1dGraph(model_2d_to_1d)
        z = graph_2d_to_1d(y)
        test_case.assertTrue(z.sbp == x.sbp)
        test_case.assertTrue(z.placement == x.placement)

if __name__ == "__main__":
    unittest.main()


text:
# import the pandas library
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(2, 2),
                  index=['1', '3'],
                  columns=['key_1', 'val_1']
                  )

df = df.reindex(['1', '2', '3'])

print(df)
# todo how to handle NaN?

max_stars_repo_path: Section 5/MissingValue.py

max_issues_repo_path: Section 5/MissingValue.py

max_forks_repo_path: Section 5/MissingValue.py


text:
import numpy as np
import matplotlib.pyplot as plt
import cvxopt

N = 100

positive = []
negative = []

mean1 = [-2, 2]
mean2 = [2, -2]
cov = [[1.0, 0.3], [0.3, 1.0]]

positive.extend(np.random.multivariate_normal(mean1, cov, N//2))
negative.extend(np.random.multivariate_normal(mean2, cov, N//2))
X = np.vstack((positive, negative))

y = []
for i in range(N//2):
    y.append(1.0)
for i in range(N//2):
    y.append(-1.0)
y = np.array(y)

_Q = np.zeros((N, N))
for i in range(N):
    for j in range(N):
        _Q[i, j] = y[i]*y[j]*np.dot(X[i], X[j])

Q = cvxopt.matrix(_Q)
p = cvxopt.matrix(-np.ones(N))
G = cvxopt.matrix(-np.eye(N))
h = cvxopt.matrix(np.zeros(N))
A = cvxopt.matrix(y.reshape((1, N)))
b = cvxopt.matrix(0.0)

solution = cvxopt.solvers.qp(Q, p, G, h, A, b)
alpha = np.array(solution['x']).flatten()

top2_sv_indices = alpha.argsort()[-2:]
sv_indices = alpha > 1e-6
W = np.dot(alpha[sv_indices] * y.flatten()[sv_indices], X[sv_indices])
bias = -np.dot(X[top2_sv_indices], W).mean()

xs = np.array([X.min(), X.max()])
ys = np.array([(-W[0]*xs[0]-bias)/W[1], (-W[0]*xs[1]-bias)/W[1]])

plt.scatter(X[:, 0][y == 1], X[:, 1][y == 1], label="positive")
plt.scatter(X[:, 0][y == -1], X[:, 1][y == -1], label="negative")
for coordinate in X[sv_indices]:
    plt.annotate('sv', coordinate)
plt.plot(xs, ys, color = "sienna")
plt.legend()
plt.grid()
plt.show()

max_stars_repo_name: tomohiro-inaba/svm

max_issues_repo_name: tomohiro-inaba/svm

max_forks_repo_name: tomohiro-inaba/svm


text:
import random
import pandas as pd
import numpy as np
from scipy.stats import bernoulli
import matplotlib.pyplot as plt

p=0.2
N=1000
X1=bernoulli.rvs(p,size=N)
X1=np.array(X1).reshape(N,1)
X2=bernoulli.rvs(p,size=N)
X2=np.array(X2).reshape(N,1)
X3=bernoulli.rvs(p,size=N)
X3=np.array(X3).reshape(N,1)
#print("The shape of r is ",r.shape)
dataset=np.concatenate((X1,X2,X3), axis=1)

indices1 = np.random.permutation(dataset.shape[0])
training_idx, test_idx = indices1[:800], indices1[800:]
training_dataset, test_dataset = dataset[training_idx,:], dataset[test_idx,:]
label_train,label_test=dataset[training_idx,:], dataset[test_idx,:]

W11,W12,W13=0.01,0.03,0.05
W21,W22,W23=0.08,0.03,0.1
W0=0.01
W00=0.20

B11,B12=0.001,0.001
B21,B22=0.009,0.001
B31,B32=0.008,0.007
B0=0.001
B00=0.003
B000=0.004

#Initial Learning Rate
lr=0.1

def sigmoidF(X):
    return (1/(1+np.exp(-X)))
def sigmoidP(X):
    return (sigmoidF(X)*(1-sigmoidF(X)))

DW11=DW12=DW13=DW21=DW22=DW23=DW0=DW00=DB11=DB12=DB21=DB22=DB31=DB32=DB0=DB00=DB000=0.00

train_loss_array=[]
test_loss_array=[]

for epoch in range(200):
    y1=[]
    y2=[]
    y3=[]
    for i in range(training_dataset.shape[0]):
        X1=training_dataset[i][0]
        X2=training_dataset[i][1]
        X3=training_dataset[i][2]
        a1=(X1*W11)+(X2*W12)+(X3*W13)+W0
        Z1=sigmoidF(a1)
        a2=(X1*W21)+(X2*W22)+(X3*W23)+W00
        Z2=sigmoidF(a2)
        a3=(Z1*B11)+(Z2*B12)+B0
        a4=(Z1*B21)+(Z2*B22)+B00
        a5=(Z1*B31)+(Z2*B32)+B000
        Z3=sigmoidF(a3)
        Z4=sigmoidF(a4)
        Z5=sigmoidF(a5)
        y1.append(Z3)
        y2.append(Z4)
        y3.append(Z5)

        DW11=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B11*sigmoidP(a1)*X1)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B21*sigmoidP(a1)*X1)+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B31*sigmoidP(a1)*X1)

        DW12=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B11*sigmoidP(a1)*X2)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B21*sigmoidP(a1)*X2)+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B31*sigmoidP(a1)*X2)

        DW13=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B11*sigmoidP(a1)*X3)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B21*sigmoidP(a1)*X3)+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B31*sigmoidP(a1)*X3)

        DW21=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B12*sigmoidP(a2)*X1)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B22*sigmoidP(a2)*X1)+ (-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B32*sigmoidP(a2)*X1)

        DW22=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B12*sigmoidP(a2)*X2)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B22*sigmoidP(a2)*X2)+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B32*sigmoidP(a2)*X2)

        DW23=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B12*sigmoidP(a2)*X3)+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B22*sigmoidP(a2)*X3)+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B32*sigmoidP(a2)*X3)

        DW0=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B11*sigmoidP(a1))+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B21*sigmoidP(a1))+(-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B31*sigmoidP(a1))
        DW00=(-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*B12*sigmoidP(a2))+(-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*B22*sigmoidP(a2))+ (-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*B32*sigmoidP(a2))

        DB11=-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*Z1
        DB12=-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))*Z2

        DB21=-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*Z1
        DB22=-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))*Z2

        DB31=-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*Z1
        DB32=-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))*Z2

        DB0=-1*(X1*(1-sigmoidF(a3))+(1-X1)*-1*sigmoidF(a3))
        DB00=-1*(X2*(1-sigmoidF(a4))+(1-X2)*-1*sigmoidF(a4))
        DB000=-1*(X3*(1-sigmoidF(a5))+(1-X3)*-1*sigmoidF(a5))

        W11=W11-(lr*DW11)
        W12=W12-(lr*DW12)
        W13=W13-(lr*DW13)
        W21=W21-(lr*DW21)
        W22=W22-(lr*DW22)
        W23=W23-(lr*DW23)
        W0=W0-(lr*DW0)
        W00=W00-(lr*DW00)

        B11=B11-(lr*DB11)
        B12=B12-(lr*DB12)
        B21=B21-(lr*DB21)
        B22=B22-(lr*DB22)
        B31=B31-(lr*DB31)
        B32=B32-(lr*DB32)

        B0=B0-(lr*DB0)
        B00=B00-(lr*DB00)
        B000=B000-(lr*DB000)
    diff1=np.square(training_dataset[:,0]-y1)
    diff2=np.square(training_dataset[:,1]-y2)
    diff3=np.square(training_dataset[:,2]-y3)
    diff=(diff1+diff2+diff3)
    difftrain=diff.reshape(800,1)
    msetrain=np.mean(difftrain)
    y11=[]
    y22=[]
    y33=[]
    for i in range(test_dataset.shape[0]):
        X1_test=test_dataset[i][0]
        X2_test=test_dataset[i][1]
        X3_test=test_dataset[i][2]
        a1=(X1_test*W11)+(X2_test*W12)+(X3_test*W13)+W0
        Z1=sigmoidF(a1)
        a2=(X1_test*W21)+(X2_test*W22)+(X3_test*W23)+W00
        Z2=sigmoidF(a2)
        a3=(Z1*B11)+(Z2*B12)+B0
        a4=(Z1*B21)+(Z2*B22)+B00
        a5=(Z1*B31)+(Z2*B32)+B000
        Z3=sigmoidF(a3)
        Z4=sigmoidF(a4)
        Z5=sigmoidF(a5)
        y11.append(Z3)
        y22.append(Z4)
        y33.append(Z5)
    diff1=np.square(test_dataset[:,0]-y11)
    diff2=np.square(test_dataset[:,1]-y22)
    diff3=np.square(test_dataset[:,2]-y33)
    diff=(diff1+diff2+diff3)
    difftest=diff.reshape(200,1)
    msetest=np.mean(difftest)
    print("Train Loss after Epoch" , epoch,"is : ",msetrain,"Test Loss after Epoch" , epoch,"is : ",msetest)
    train_loss_array.append(msetrain)
    test_loss_array.append(msetest)
    #lr=1/(epoch+1)

y11=[]
y22=[]
y33=[]
for i in range(test_dataset.shape[0]):
        X1=test_dataset[i][0]
        X2=test_dataset[i][1]
        X3=test_dataset[i][2]
        a1=(X1*W11)+(X2*W12)+(X3*W13)+W0
        Z1=sigmoidF(a1)
        a2=(X1*W21)+(X2*W22)+(X3*W23)+W00
        Z2=sigmoidF(a2)
        a3=(Z1*B11)+(Z2*B12)+B0
        a4=(Z1*B21)+(Z2*B22)+B00
        a5=(Z1*B31)+(Z2*B32)+B000
        Z3=sigmoidF(a3)
        Z4=sigmoidF(a4)
        Z5=sigmoidF(a5)
        y11.append(Z3)
        y22.append(Z4)
        y33.append(Z5)

diff1=(test_dataset[:,0]-y11)
diff2=(test_dataset[:,1]-y22)
diff3=(test_dataset[:,2]-y33)
diff=np.square(diff1+diff2+diff3)
diff=diff.reshape(200,1)

bce=np.mean(diff)
print("Loss on test set ",bce)

plt.title("Train and Test Loss each Epoch for binary data without using Autograd")
plt.xlabel("Epochs")
plt.ylabel("Train and Test Loss without autograd")
for i in range(198):
    plt.plot([i,i+2],train_loss_array[i:i+2],linestyle='-',linewidth=1,color='red')
    plt.plot([i,i+2],test_loss_array[i:i+2],linestyle='-',linewidth=1,color='blue')
plt.legend(["train_loss_array", "test_loss_array"], loc ="upper right")
plt.savefig("Q2_WA.png")
plt.clf()

max_stars_repo_name: dipankarsk/Statistical-Machine-Learning

max_issues_repo_name: dipankarsk/Statistical-Machine-Learning

max_forks_repo_name: dipankarsk/Statistical-Machine-Learning


text:
# Original Version: Taehoon Kim (http://carpedm20.github.io)
# Source: https://raw.githubusercontent.com/carpedm20/DCGAN-tensorflow/master/model.py
# Modifications for image inpainting: Zack Soliman
import os
import time
import math
from glob import glob
import tensorflow as tf
import numpy as np

from ops import *
from utils import *

image_summary = tf.summary.image
scalar_summary = tf.summary.scalar
histogram_summary = tf.summary.histogram
merge_summary = tf.summary.merge
SummaryWriter = tf.summary.FileWriter

class DCGAN(object):
    def __init__(self, sess, image_size=64, batch_size=64, sample_size = 64, z_dim=100,
            gf_dim=64, df_dim=64, gfc_dim=1024, dfc_dim=1024, c_dim=3,
            dataset_name='default', z_dist="gaussian", checkpoint_dir=None, sample_dir=None):
        """
        Args:
            sess: TensorFlow session
            batch_size: The size of batch. Should be specified before training.
            y_dim: (optional) Dimension of dim for y. [None]
            z_dim: (optional) Dimension of dim for Z. [100]
            gf_dim: (optional) Dimension of gen filters in first conv layer. [64]
            df_dim: (optional) Dimension of discrim filters in first conv layer. [64]
            gfc_dim: (optional) Dimension of gen units for for fully connected layer. [1024]
            dfc_dim: (optional) Dimension of discrim units for fully connected layer. [1024]
            c_dim: (optional) Dimension of image color. For grayscale input, set to 1. [3]
        """
        self.sess = sess
        self.model_name = "DCGAN.model"
        self.z_dist = z_dist
        self.batch_size = batch_size
        self.sample_size = sample_size
        self.image_size = image_size
        self.z_dim = z_dim
        self.gf_dim = gf_dim
        self.df_dim = df_dim
        self.gfc_dim = gfc_dim
        self.dfc_dim = dfc_dim
        self.c_dim = c_dim

        # batch normalization : deals with poor initialization helps gradient flow
        self.d_bn1 = batch_norm(name='d_bn1')
        self.d_bn2 = batch_norm(name='d_bn2')

        if not self.y_dim:
            self.d_bn3 = batch_norm(name='d_bn3')

        self.g_bn0 = batch_norm(name='g_bn0')
        self.g_bn1 = batch_norm(name='g_bn1')
        self.g_bn2 = batch_norm(name='g_bn2')

        if not self.y_dim:
            self.g_bn3 = batch_norm(name='g_bn3')

        self.dataset_name = dataset_name
        self.checkpoint_dir = checkpoint_dir
        self.build_model()

    def build_model(self):
        image_dims = [self.input_height, self.input_width, self.c_dim]

        self.inputs = tf.placeholder(
            tf.float32, [self.batch_size] + image_dims, name='real_images')
        self.sample_inputs = tf.placeholder(
            tf.float32, [self.sample_size] + image_dims, name='sample_inputs')

        inputs = self.inputs
        sample_inputs = self.sample_inputs

        self.z = tf.placeholder(
            tf.float32, [None, self.z_dim], name='z')
        self.z_sum = histogram_summary("z", self.z)

        if self.y_dim:
            self.G = self.generator(self.z, self.y)
            self.D, self.D_logits = self.discriminator(inputs, self.y, reuse=False)

            self.sampler = self.sampler(self.z, self.y)
            self.D_, self.D_logits_ = self.discriminator(self.G, self.y, reuse=True)
        else:
            self.G = self.generator(self.z)
            self.D, self.D_logits = self.discriminator(inputs)

            self.sampler = self.sampler(self.z)
            self.D_, self.D_logits_ = self.discriminator(self.G, reuse=True)

        self.d_sum = histogram_summary("d", self.D)
        self.d__sum = histogram_summary("d_", self.D_)
        self.G_sum = image_summary("G", self.G, max_outputs=30)

        def sigmoid_cross_entropy_with_logits(x, y):
            try:
                return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y)
            except:
                return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, targets=y)

        self.d_loss_real = tf.reduce_mean(
            sigmoid_cross_entropy_with_logits(self.D_logits, tf.ones_like(self.D)))
        self.d_loss_fake = tf.reduce_mean(
            sigmoid_cross_entropy_with_logits(self.D_logits_, tf.zeros_like(self.D_)))
        self.g_loss = tf.reduce_mean(
            sigmoid_cross_entropy_with_logits(self.D_logits_, tf.ones_like(self.D_)))

        self.d_loss_real_sum = scalar_summary("d_loss_real", self.d_loss_real)
        self.d_loss_fake_sum = scalar_summary("d_loss_fake", self.d_loss_fake)

        self.d_loss = self.d_loss_real + self.d_loss_fake

        self.g_loss_sum = scalar_summary("g_loss", self.g_loss)
        self.d_loss_sum = scalar_summary("d_loss", self.d_loss)

        t_vars = tf.trainable_variables()

        self.d_vars = [var for var in t_vars if 'd_' in var.name]
        self.g_vars = [var for var in t_vars if 'g_' in var.name]

        self.saver = tf.train.Saver()

    def train(self, config):
        print("Loading data from:")
        print(os.path.join(config.dataset, "*.jpg"))
        data = glob(os.path.join(config.dataset, "*.jpg"))
        #np.random.shuffle(data)
        assert(len(data) > 0)

        d_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1) \
                            .minimize(self.d_loss, var_list=self.d_vars)
        g_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1) \
                            .minimize(self.g_loss, var_list=self.g_vars)
        tf.global_variables_initializer().run()

        self.g_sum = merge_summary(
            [self.z_sum, self.d__sum, self.G_sum, self.d_loss_fake_sum, self.g_loss_sum])
        self.d_sum = merge_summary(
            [self.z_sum, self.d_sum, self.d_loss_real_sum, self.d_loss_sum])
        self.writer = SummaryWriter("./logs", self.sess.graph)

        if self.z_dist == "uniform":
            sample_z = np.random.uniform(-1, 1, size=(self.sample_size , self.z_dim))
        elif self.z_dist == "gaussian":
            sample_z = np.random.normal(size=(self.sample_size, self.z_dim))

        sample_files = data[0:self.sample_size]
        sample = load_images(sample_files)
        sample_images = np.array(sample).astype(np.float32)

        counter = 1
        start_time = time.time()

        if self.load(self.checkpoint_dir):
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")

        for epoch in range(config.epoch):
            data = glob(os.path.join(config.dataset, "*.jpg"))
            batch_idxs = min(len(data), config.train_size) // self.batch_size

            for idx in range(0, batch_idxs):
                batch_files = data[idx*config.batch_size:(idx+1)*config.batch_size]
                batch = load_images(batch_files)

                if self.z_dist == "uniform":
                    print("Sampling z from uniform")
                    batch_z = np.random.uniform(-1, 1, [config.batch_size, self.z_dim]) \
                                .astype(np.float32)
                elif self.z_dist == "gaussian":
                    print("Sampling z from gaussian")
                    batch_z = np.random.normal(size=(config.batch_size, self.z_dim))

                # Update D network
                _, summary_str = self.sess.run([d_optim, self.d_sum],
                    feed_dict={ self.inputs: batch, self.z: batch_z })
                self.writer.add_summary(summary_str, counter)

                # Update G network
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                    feed_dict={ self.z: batch_z })
                self.writer.add_summary(summary_str, counter)

                # Run g_optim twice to make sure that d_loss does not go to zero (different from paper)
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                    feed_dict={ self.z: batch_z })
                self.writer.add_summary(summary_str, counter)

                errD_fake = self.d_loss_fake.eval({self.z: batch_z})
                errD_real = self.d_loss_real.eval({self.inputs: batch})
                errG = self.g_loss.eval({self.z: batch_z})

                counter += 1
                print("Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f" \
                    % (epoch, idx, batch_idxs,
                        time.time() - start_time, errD_fake+errD_real, errG))

                if np.mod(counter, 100) == 1:
                    samples, d_loss, g_loss = self.sess.run(
                        [self.sampler, self.d_loss, self.g_loss],
                        feed_dict={self.z: sample_z, self.inputs: sample_images}
                    )
                    save_images(samples, [8, 8],
                                './samples/train_{:02d}_{:04d}.png'.format(epoch, idx))
                    print("[Sample] d_loss: %.8f, g_loss: %.8f" % (d_loss, g_loss))

                if np.mod(counter, 500) == 2:
                    self.save(config.checkpoint_dir, counter)

    def encoder(input_imgs):
        pass

    def discriminator(self, image, y=None, reuse=False):
        with tf.variable_scope("discriminator") as scope:
            if reuse:
                scope.reuse_variables()

            if not self.y_dim:
                h0 = lrelu(conv2d(image, self.df_dim, name='d_h0_conv'))
                h1 = lrelu(self.d_bn1(conv2d(h0, self.df_dim*2, name='d_h1_conv')))
                h2 = lrelu(self.d_bn2(conv2d(h1, self.df_dim*4, name='d_h2_conv')))
                h3 = lrelu(self.d_bn3(conv2d(h2, self.df_dim*8, name='d_h3_conv')))
                h4 = linear(tf.reshape(h3, [self.batch_size, -1]), 1, 'd_h3_lin')

                return tf.nn.sigmoid(h4), h4
            else:
                yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim])
                x = conv_cond_concat(image, yb)

                h0 = lrelu(conv2d(x, self.c_dim + self.y_dim, name='d_h0_conv'))
                h0 = conv_cond_concat(h0, yb)

                h1 = lrelu(self.d_bn1(conv2d(h0, self.df_dim + self.y_dim, name='d_h1_conv')))
                h1 = tf.reshape(h1, [self.batch_size, -1])
                h1 = concat([h1, y], 1)

                h2 = lrelu(self.d_bn2(linear(h1, self.dfc_dim, 'd_h2_lin')))
                h2 = concat([h2, y], 1)

                h3 = linear(h2, 1, 'd_h3_lin')

                return tf.nn.sigmoid(h3), h3

    def generator(self, z, y=None):
        with tf.variable_scope("generator") as scope:
            if not self.y_dim:
                self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*8*4*4, 'g_h0_lin', with_w=True)

                self.h0 = tf.reshape(self.z_, [-1, 4, 4, self.gf_dim * 8])
                h0 = tf.nn.relu(self.g_bn0(self.h0))

                self.h1, self.h1_w, self.h1_b = conv2d_transpose(h0,
                [self.batch_size, 8, 8, self.gf_dim*4], name='g_h1', with_w=True)
                h1 = tf.nn.relu(self.g_bn1(self.h1))

                h2, self.h2_w, self.h2_b = conv2d_transpose(h1,
                [self.batch_size, 16, 16, self.gf_dim*2], name='g_h2', with_w=True)
                h2 = tf.nn.relu(self.g_bn2(h2))

                h3, self.h3_w, self.h3_b = conv2d_transpose(h2,
                [self.batch_size, 32, 32, self.gf_dim*1], name='g_h3', with_w=True)
                h3 = tf.nn.relu(self.g_bn3(h3))

                h4, self.h4_w, self.h4_b = conv2d_transpose(h3,
                [self.batch_size, 64, 64, 3], name='g_h4', with_w=True)

                return tf.nn.tanh(h4)

            else:
                s_h, s_w = self.output_height, self.output_width
                s_h2, s_h4 = int(s_h/2), int(s_h/4)
                s_w2, s_w4 = int(s_w/2), int(s_w/4)

                # yb = tf.expand_dims(tf.expand_dims(y, 1),2)
                yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim])
                z = concat([z, y], 1)

                h0 = tf.nn.relu(
                    self.g_bn0(linear(z, self.gfc_dim, 'g_h0_lin')))
                h0 = concat([h0, y], 1)

                h1 = tf.nn.relu(self.g_bn1(
                    linear(h0, self.gf_dim*2*s_h4*s_w4, 'g_h1_lin')))
                h1 = tf.reshape(h1, [self.batch_size, s_h4, s_w4, self.gf_dim * 2])

                h1 = conv_cond_concat(h1, yb)

                h2 = tf.nn.relu(self.g_bn2(conv2d_transpose(h1,
                    [self.batch_size, s_h2, s_w2, self.gf_dim * 2], name='g_h2')))
                h2 = conv_cond_concat(h2, yb)

                return tf.nn.sigmoid(
                    conv2d_transpose(h2, [self.batch_size, s_h, s_w, self.c_dim], name='g_h3'))

    def sampler(self, z, y=None):
        with tf.variable_scope("generator") as scope:
            scope.reuse_variables()

            if not self.y_dim:
                h0 = tf.reshape(linear(z, self.gf_dim*8*4*4, 'g_h0_lin'),
                                [-1, 4, 4, self.gf_dim * 8])
                h0 = tf.nn.relu(self.g_bn0(h0, train=False))

                h1 = conv2d_transpose(h0, [self.batch_size, 8, 8, self.gf_dim*4], name='g_h1')
                h1 = tf.nn.relu(self.g_bn1(h1, train=False))

                h2 = conv2d_transpose(h1, [self.batch_size, 16, 16, self.gf_dim*2], name='g_h2')
                h2 = tf.nn.relu(self.g_bn2(h2, train=False))

                h3 = conv2d_transpose(h2, [self.batch_size, 32, 32, self.gf_dim*1], name='g_h3')
                h3 = tf.nn.relu(self.g_bn3(h3, train=False))

                h4 = conv2d_transpose(h3, [self.batch_size, 64, 64, 3], name='g_h4')

                return tf.nn.tanh(h4)
            else:
                s_h, s_w = self.output_height, self.output_width
                s_h2, s_h4 = int(s_h/2), int(s_h/4)
                s_w2, s_w4 = int(s_w/2), int(s_w/4)

                # yb = tf.reshape(y, [-1, 1, 1, self.y_dim])
                yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim])
                z = concat([z, y], 1)

                h0 = tf.nn.relu(self.g_bn0(linear(z, self.gfc_dim, 'g_h0_lin'), train=False))
                h0 = concat([h0, y], 1)

                h1 = tf.nn.relu(self.g_bn1(
                    linear(h0, self.gf_dim*2*s_h4*s_w4, 'g_h1_lin'), train=False))
                h1 = tf.reshape(h1, [self.batch_size, s_h4, s_w4, self.gf_dim * 2])
                h1 = conv_cond_concat(h1, yb)

                h2 = tf.nn.relu(self.g_bn2(
                    conv2d_transpose(h1, [self.batch_size, s_h2, s_w2, self.gf_dim * 2], name='g_h2'), train=False))
                h2 = conv_cond_concat(h2, yb)

                return tf.nn.sigmoid(conv2d_transpose(h2, [self.batch_size, s_h, s_w, self.c_dim], name='g_h3'))

    def save(self, checkpoint_dir, step):
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)

        self.saver.save(self.sess,
                        os.path.join(checkpoint_dir, self.model_name),
                        global_step=step)

    def load(self, checkpoint_dir):
        print(" [*] Reading checkpoints...")

        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            self.saver.restore(self.sess, ckpt.model_checkpoint_path)
            return True
        else:
            return False


text:
"""
1:1:1 competition binding system solved using Lagrange multiplier approach

Modified Factory example utilising Lagrane multiplier to solve PL complex
concentration in a 1:1: protein:ligand and protein:inhibitor binding system
"""

from scipy.optimize import fsolve
import autograd.numpy as np
from autograd import grad
def lagrange_competition(p0,l0, i0,kdpl, kdpi):
    def F(X): # Augmented Lagrange function
        response=(X[0]*X[1])/kdpl
        constraint1=p0-(response+X[0]+(X[0]*X[2]/kdpi))
        constraint2=l0-(response+X[1])
        constraint3=i0-(X[2]+(X[0]*X[2]/kdpi))
        return response - X[3] *constraint1 - X[4]*constraint2 -X[5]*constraint3
    dfdL = grad(F, 0) # Gradients of the Lagrange function
    pf, lf, inhf, lam1, lam2,lam3 = fsolve(dfdL, [p0, l0, i0, 1.0,1.0,1.0])
    return {'pf':pf,'lf':lf,'pl':(pf*lf)/kdpl}

if __name__ == "__main__":
    concentrations=lagrange_competition(10,12,13,7.5,3.2)
    print(f"The answer is at {concentrations['pl']}")
    print(concentrations)

max_stars_repo_name: stevenshave/lagrange-binding-systems

max_issues_repo_name: stevenshave/lagrange-binding-systems

max_forks_repo_name: stevenshave/lagrange-binding-systems

reason: from scipy