text:
import pandas as pd
from scripts.python.routines.manifest import get_manifest
import numpy as np
import os
import pickle
from scripts.python.pheno.datasets.filter import filter_pheno, get_passed_fields
from sklearn.metrics import mean_squared_error, mean_absolute_error
from scipy.stats import spearmanr
import matplotlib.pyplot as plt
from scripts.python.pheno.datasets.features import get_column_name, get_status_dict, get_sex_dict
from scripts.python.routines.plot.scatter import add_scatter_trace
from matplotlib import colors
from scipy.stats import mannwhitneyu
import plotly.graph_objects as go
from scripts.python.routines.plot.save import save_figure
from scripts.python.routines.plot.violin import add_violin_trace
from scripts.python.routines.plot.box import add_box_trace
from scripts.python.routines.plot.layout import add_layout
import pathlib
import seaborn as sns
from sklearn.preprocessing import StandardScaler
import plotly.express as px
from functools import reduce
import plotly
from sklearn.decomposition import PCA
import statsmodels.formula.api as smf
from scripts.python.routines.manifest import get_manifest
from scripts.python.routines.plot.save import save_figure
from scripts.python.routines.plot.histogram import add_histogram_trace
from scripts.python.routines.plot.layout import add_layout
from scripts.python.routines.plot.p_value import add_p_value_annotation

thld_abs_diff = 16

dataset = "GSEUNN"

path = f"E:/YandexDisk/Work/pydnameth/datasets"
datasets_info = pd.read_excel(f"{path}/datasets.xlsx", index_col='dataset')
platform = datasets_info.loc[dataset, 'platform']
manifest = get_manifest(platform)

status_col = get_column_name(dataset, 'Status').replace(' ','_')
age_col = get_column_name(dataset, 'Age').replace(' ','_')
sex_col = get_column_name(dataset, 'Sex').replace(' ','_')
status_dict = get_status_dict(dataset)
status_passed_fields = status_dict['Control'] + status_dict['Case']
sex_dict = get_sex_dict(dataset)

with open(f'{path}/{platform}/{dataset}/features/immuno.txt') as f:
    immuno_features = f.read().splitlines()

path_save = f"{path}/{platform}/{dataset}/special/011_immuno_part3_and_part4_check_clocks"
pathlib.Path(f"{path_save}/figs/{thld_abs_diff}").mkdir(parents=True, exist_ok=True)

continuous_vars = {}
categorical_vars = {status_col: [x.column for x in status_passed_fields], sex_col: list(sex_dict.values())}
pheno = pd.read_pickle(f"{path}/{platform}/{dataset}/pheno_xtd.pkl")
pheno = filter_pheno(dataset, pheno, continuous_vars, categorical_vars)
ctrl = pheno.loc[pheno['Group'] == 'Control']
esrd = pheno.loc[pheno['Group'] == 'ESRD']

formula = f"ImmunoAge ~ Age"
model_linear = smf.ols(formula=formula, data=ctrl).fit()
y_pred = model_linear.predict(pheno)
pheno['ImmunoAgeAcc'] = pheno['ImmunoAge'] - y_pred
ImmunoAgeAcc_check = abs(pheno['ImmunoAgeAcc'] - pheno['ImmunoAgeAA'])
print(f"Linear model checking: {max(abs(ImmunoAgeAcc_check.values))}")

df = pd.read_excel(f"{path}/{platform}/{dataset}/data/immuno/part3_part4_with_age_sex.xlsx", index_col='ID')

model_df = pd.read_excel(f"{path}/{platform}/{dataset}/special/011_immuno_part3_and_part4_check_clocks/legacy/Control/v22/clock.xlsx")

features = model_df['feature'].to_list()
coefs = model_df['coef'].to_list()

# Checking of model
immuno_check = np.full(pheno.shape[0], coefs[0])
for feat_id in range(1, len(features)):
    immuno_check += pheno.loc[:, features[feat_id]].values * coefs[feat_id]
check = immuno_check - pheno.loc[:, 'ImmunoAge'].values
print(f"Clock model checking: {max(abs(check))}")

predicted = np.full(df.shape[0], coefs[0])
for feat_id in range(1, len(features)):
    predicted += df.loc[:, features[feat_id]].values * coefs[feat_id]

df[f'ImmunoAge'] = predicted
df.to_excel(f"{path}/{platform}/{dataset}/data/immuno/part3_part4_with_age_sex.xlsx", index=True)

df[f'ImmunoAgeAbsDiff'] = df[f'ImmunoAge'] - df[f'Age']
df[f'Name'] = df.index.values
y_pred = model_linear.predict(df)
df['ImmunoAgeAcc'] = df['ImmunoAge'] - y_pred
df = df[~df.index.str.startswith(('Q', 'H'))]

fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x=df.loc[abs(df[f'ImmunoAgeAbsDiff']) <= thld_abs_diff, 'Age'].values,
        y=df.loc[abs(df[f'ImmunoAgeAbsDiff']) <= thld_abs_diff, 'ImmunoAge'].values,
        showlegend=True,
        name="Controls",
        mode="markers",
        marker=dict(
            size=9,
            opacity=0.65,
            line=dict(
                width=0.5
            )
        )
    )
)
fig.add_trace(
    go.Scatter(
        x=df.loc[abs(df[f'ImmunoAgeAbsDiff']) > thld_abs_diff, 'Age'].values,
        y=df.loc[abs(df[f'ImmunoAgeAbsDiff']) > thld_abs_diff, 'ImmunoAge'].values,
        showlegend=False,
        mode="markers+text",
        text=df.loc[abs(df[f'ImmunoAgeAbsDiff']) > thld_abs_diff, 'Name'].values,
        textposition="middle right",
        marker=dict(
            size=10,
            opacity=0.65,
            line=dict(
                width=1
            )
        )
    )
)
add_layout(fig, 'Age', 'ipAGE', f"")
fig.update_layout({'colorway': ["blue", "blue", "red"]})
fig.update_layout(
    margin=go.layout.Margin(
        l=80,
        r=20,
        b=80,
        t=50,
        pad=0
    )
)
save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/x(Age)_y(ImmunoAge)")

df = df[abs(df[f'ImmunoAgeAbsDiff']) <= thld_abs_diff]
print(f"Shape: {df.shape}")
df.to_excel(f"{path_save}/part3_part4_filtered_with_age_sex_{thld_abs_diff}.xlsx", index=True)
rmse = np.sqrt(mean_squared_error(df.loc[:, 'Age'].values, df.loc[:, 'ImmunoAge'].values))
mae = mean_absolute_error(df.loc[:, 'Age'].values, df.loc[:, 'ImmunoAge'].values)
print(f"RMSE in test controls: {rmse}")
print(f"MAE in test controls: {mae}")

fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x=df.loc[:, 'Age'].values,
        y=df.loc[:, 'ImmunoAge'].values,
        showlegend=True,
        name="Controls",
        mode="markers",
        marker=dict(
            size=9,
            opacity=0.65,
            line=dict(
                width=0.5
            )
        )
    )
)
add_layout(fig, 'Age', 'ipAGE', f"")
fig.update_layout({'colorway': ["blue", "blue", "red"]})
fig.update_layout(
    margin=go.layout.Margin(
        l=80,
        r=20,
        b=80,
        t=50,
        pad=0
    )
)
save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/x(Age)_y(ImmunoAge)_filtered")

ctrl_color = 'lime'
ctrl_test_color = 'cyan'
esrd_color = 'fuchsia'
dist_num_bins = 25
ctrl = pheno.loc[pheno['Group'] == 'Control']
esrd = pheno.loc[pheno['Group'] == 'ESRD']
ctrl_test = df

values_ctrl = ctrl.loc[:, 'ImmunoAgeAA'].values
values_ctrl_test = ctrl_test.loc[:, 'ImmunoAgeAcc'].values
values_esrd = esrd.loc[:, 'ImmunoAgeAA'].values

stat_01, pval_01 = mannwhitneyu(values_ctrl, values_ctrl_test, alternative='two-sided')
stat_02, pval_02 = mannwhitneyu(values_ctrl, values_esrd, alternative='two-sided')
stat_12, pval_12 = mannwhitneyu(values_ctrl_test, values_esrd, alternative='two-sided')

fig = go.Figure()
add_box_trace(fig, pheno.loc[pheno['Group'] == 'Control', 'ImmunoAgeAcc'].values, f"Control (model building)")
add_box_trace(fig, df.loc[:, 'ImmunoAgeAcc'].values, f"Control (test)")
add_box_trace(fig, pheno.loc[pheno['Group'] == 'ESRD', 'ImmunoAgeAcc'].values, f"ESRD")
add_layout(fig, "", "Age acceleration", f"")
fig.update_layout(
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.25,
        xanchor="center",
        x=0.5
    )
)
fig.update_layout({'colorway': ['blue', 'cyan', 'red']})
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(autorange=False)
fig.update_layout(yaxis_range=[-50, 250])
fig = add_p_value_annotation(fig, {(0,1): pval_01, (1, 2) : pval_12, (0,2): pval_02})
fig.update_layout(
    margin=go.layout.Margin(
        l=80,
        r=20,
        b=30,
        t=120,
        pad=0
    )
)

save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/box_age_acceleration")

fig = go.Figure()
fig.add_trace(
    go.Violin(
        y=values_ctrl,
        name=f"Control",
        box_visible=True,
        meanline_visible=True,
        showlegend=True,
        line_color='black',
        fillcolor=ctrl_color,
        marker=dict(color=ctrl_color, line=dict(color='black', width=0.3), opacity=0.8),
        points='all',
        bandwidth=np.ptp(values_ctrl) / dist_num_bins,
        opacity=0.8
    )
)
fig.add_trace(
    go.Violin(
        y=values_ctrl_test,
        name=f"Control (test)",
        box_visible=True,
        meanline_visible=True,
        showlegend=True,
        line_color='black',
        fillcolor=ctrl_test_color,
        marker=dict(color=ctrl_test_color, line=dict(color='black', width=0.3), opacity=0.8),
        points='all',
        bandwidth=np.ptp(values_ctrl_test) / dist_num_bins,
        opacity=0.8
    )
)
fig.add_trace(
    go.Violin(
        y=values_esrd,
        name=f"ESRD",
        box_visible=True,
        meanline_visible=True,
        showlegend=True,
        line_color='black',
        fillcolor=esrd_color,
        marker=dict(color=esrd_color, line=dict(color='black', width=0.3), opacity=0.8),
        points='all',
        bandwidth=np.ptp(values_esrd) / 50,
        opacity=0.8
    )
)

add_layout(fig, "", "ipAGE acceleration", f"")
fig.update_layout({'colorway': ['lime', 'cyan', 'fuchsia']})
fig = add_p_value_annotation(fig, {(0,1): pval_01, (1, 2) : pval_12, (0,2): pval_02})
fig.update_yaxes(autorange=False)
fig.update_layout(yaxis_range=[-50, 200])
fig.update_layout(title_xref='paper')
fig.update_layout(legend_font_size=20)
fig.update_layout(
    margin=go.layout.Margin(
        l=110,
        r=20,
        b=50,
        t=90,
        pad=0
    )
)
fig.update_layout(
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.25,
        xanchor="center",
        x=0.5
    )
)
fig.add_annotation(dict(font=dict(color='black', size=45),
                        x=-0.18,
                        y=1.4,
                        showarrow=False,
                        text=f"(b)",
                        textangle=0,
                        yanchor='top',
                        xanchor='left',
                        xref="paper",
                        yref="paper"))
save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/vio_age_acceleration")

fig = go.Figure()
add_histogram_trace(fig, df.loc[df['Sex'] == 'M', 'Age'].values, f"Males ({df.loc[df['Sex'] == 'M', :].shape[0]})", 5.0)
add_histogram_trace(fig, df.loc[df['Sex'] == 'F', 'Age'].values, f"Females ({df.loc[df['Sex'] == 'F', :].shape[0]})", 5.0)
add_layout(fig, "Age", "Count", "")
fig.update_layout(colorway=['blue', 'red'], barmode='overlay')
fig.update_layout(
    margin=go.layout.Margin(
        l=50,
        r=10,
        b=60,
        t=40,
        pad=0
    )
)
save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/histogram_Age")

fig = go.Figure()
x = df.loc[:, 'Age'].values,
y = df.loc[:, 'ImmunoAge'].values,
add_scatter_trace(fig, ctrl.loc[:, 'Age'].values, ctrl.loc[:, 'ImmunoAge'].values, f"Control (original)")
add_scatter_trace(fig, ctrl.loc[:, 'Age'].values, model_linear.fittedvalues.values, "", "lines")
add_scatter_trace(fig, df.loc[:, 'Age'].values, df.loc[:, 'ImmunoAge'].values, f"Control (test)")
add_scatter_trace(fig, esrd.loc[:, 'Age'].values, esrd.loc[:, 'ImmunoAge'].values, f"ESRD")
add_layout(fig, f"Age", 'ipAGE', f"")
fig.update_layout({'colorway': ['blue', 'blue', 'cyan', 'red']})
fig.update_layout(
    margin=go.layout.Margin(
        l=80,
        r=20,
        b=80,
        t=50,
        pad=0
    )
)
fig.update_yaxes(autorange=False)
fig.update_xaxes(autorange=False)
fig.update_layout(yaxis_range=[10, 100])
fig.update_layout(xaxis_range=[10, 100])
save_figure(fig, f"{path_save}/figs/{thld_abs_diff}/scatter_Age_ipAGE")

max_stars_repo_name: AaronBlare/dnam

max_issues_repo_name: AaronBlare/dnam

max_forks_repo_name: AaronBlare/dnam

reason: import numpy,from scipy,import statsmodels


text:
import cv2
import numpy as np

img = cv2.imread('../images/victoria.jpg',0)

# Does a global histogram equalization causing a global contrast of the img
equal = cv2.equalizeHist(img)

# Stack the images horizontally side-by-side
res = np.hstack((img, equal))

cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

ext: py

max_stars_repo_name: vibinash/vision

max_issues_repo_name: vibinash/vision

max_forks_repo_name: vibinash/vision

reason: import numpy


text:
#!/usr/bin/env python3
"""Deep learning from scratch."""
import csv
import itertools
import json
import numpy as np
from typing import Tuple
from scipy.sparse import coo_matrix
import sys

def label_binarize(y: np.ndarray, classes: list) -> np.ndarray:
    """Convert the array of labels to an array of one hot vectors."""
    return np.hstack([y == c for c in classes])

def softmax(X: np.ndarray, axis: int) -> np.ndarray:
    """Normalize a vector of weights to probabilities."""
    e = np.exp(X)
    return e / e.sum(axis=axis)[np.newaxis].T

def log_loss(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Multiclass cross entropy cost."""
    return -(y_true * np.log(y_pred)).sum(axis=1).mean()

def acc(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Top-1 accuracy."""
    return (y_true.argmax(axis=1) == y_pred.argmax(axis=1)).mean()

def f1_macro(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Harmonic mean of precision and recall."""
    y_true, y_pred = y_true.argmax(axis=1), y_pred.argmax(axis=1)
    c = (np.ones_like(y_true), (y_true, y_pred))
    c = coo_matrix(c, shape=(CLASSES, CLASSES)).toarray()
    d = np.diag(c)
    precision, recall = d / c.sum(axis=1), d / c.sum(axis=0)
    return 2 * (precision * recall / (precision + recall)).mean()

def binary_roc_auc(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """One vs rest ROC AUC."""
    ix = np.argsort(y_pred)[::-1]
    y_true, y_pred = y_true[ix], y_pred[ix]
    distinct_value_indices = np.where(np.diff(y_pred))[0]
    threshold_idxs = np.r_[distinct_value_indices, y_true.size - 1]
    tps = y_true.cumsum()[threshold_idxs]
    fps = 1 + threshold_idxs - tps
    thresholds = y_pred[threshold_idxs]
    c = np.logical_or(np.diff(fps, 2), np.diff(tps, 2))
    optimal_idxs = np.where(np.r_[True, c, True])[0]
    fps, tps = fps[optimal_idxs], tps[optimal_idxs]
    fps, tps = np.r_[0, fps], np.r_[0, tps]
    fpr, tpr = fps / fps[-1], tps / tps[-1]
    return np.trapz(tpr, fpr)

def roc_auc_macro(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    """Area Under the Receiver Operating Characteristic Curve."""
    res = [binary_roc_auc(y_true[:, c], y_pred[:, c]) for c in classes]
    return np.mean(res)

class Dense:
    """A fully connected layer."""
    def __init__(self,
                 n_in: int,
                 n_out: int,
                 activation: str = '',
                 l2: float = 0,
                 dropout: float = 0):
        """Randomly initialize weights."""
        bound = np.sqrt(6 / (n_in + n_out))
        self.W = np.random.uniform(low=-bound, high=bound, size=(n_in, n_out))
        self.b = np.zeros(n_out)
        self._activation, self._l2, self._dropout = activation, l2, dropout

    def __call__(self, X: np.ndarray, train: bool) -> np.ndarray:
        """Apply hidden layer and any additional transformations."""
        self._X = X
        ret = X @ self.W + self.b
        if self._activation == 'relu':
            ret[ret < 0] = 0
            self._next_X = ret
        if train:
            self._D = (np.random.rand(*ret.shape) >=
                       self._dropout) / (1 - self._dropout)
            ret *= self._D
        return ret

    def backward(self, delta: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """Calculate gradient for linear, activation and dropout."""
        delta *= self._D
        if self._activation == 'relu':
            delta *= self._next_X > 0
        grad_W = (self._X.T @ delta) / len(delta) + self._l2 * self.W
        return delta @ self.W.T, grad_W

class BatchNorm:
    """Batch normalization layer."""
    def __init__(self, n: int, momentum: float, epsilon: float):
        """Initialize weights."""
        self.W, self.b = np.ones(n), np.zeros(n)
        self._momentum, self._epsilon = momentum, epsilon
        self._running_mean, self._running_var = np.zeros(n), np.ones(n)

    def __call__(self, X: np.ndarray, train: bool) -> np.ndarray:
        """Center and scale input data."""
        mean, var = self._running_mean, self._running_var
        if train:
            mean, var = np.mean(X, axis=0), np.var(X, axis=0)
            self._running_mean *= self._momentum
            self._running_mean += (1 - self._momentum) * mean
            self._running_var *= self._momentum
            self._running_var += (1 - self._momentum) * var
        self._std_inv = 1 / np.sqrt(var + self._epsilon)
        self._X = (X - mean) * self._std_inv
        return self.W * self._X + self.b

    def backward(self, delta: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """Calculate gradient for batch normalization."""
        dx = delta * self.W
        grad_W = (delta * self._X).mean(axis=0)
        return self._std_inv * (dx - dx.mean(axis=0) - self._X *
                                (dx * self._X).mean(axis=0)), grad_W

class Classifier:
    """Multiclass classifier implemented as a multi layer perceptron."""
    def __init__(self, layers: list):
        """Construct neural network."""
        self._layers = layers

    def fit(self,
            X: np.ndarray,
            y: np.ndarray,
            X_test: np.ndarray,
            y_test: np.ndarray,
            batch_size: int,
            epochs: int,
            lr: float,
            momentum: float = 0) -> None:
        """Train the network with early stopping on training loss."""
        v_W = [np.zeros_like(layer.W) for layer in self._layers]
        v_b = [np.zeros_like(layer.b) for layer in self._layers]
        data = np.hstack((X, y))
        progress = {
            'batch_size': batch_size,
            'epochs': epochs,
            'lr': lr,
            'momentum': momentum
        }
        for epoch in range(epochs):
            np.random.shuffle(data)
            loss = 0
            for batch in np.array_split(data, len(data) // batch_size):
                y_pred = self.predict_proba(batch[:, :-y.shape[1]], True)
                y_true = batch[:, -y.shape[1]:]
                cost, delta = log_loss(y_true, y_pred), y_pred - y_true
                for i, layer in reversed(list(enumerate(self._layers))):
                    grad_b = delta.mean(axis=0)
                    delta, grad_W = layer.backward(delta)
                    v_W[i] *= momentum
                    v_W[i] += lr * grad_W
                    v_b[i] *= momentum
                    v_b[i] += lr * grad_b
                    layer.W -= v_W[i]
                    layer.b -= v_b[i]
                loss += len(batch) * cost / len(data)
            progress['epoch'] = epoch + 1
            progress['logloss'] = loss
            for (t, X_val, y_val) in ('train', X, y), ('test', X_test, y_test):
                y_pred = self.predict_proba(X_val)
                for m in metrics:
                    progress[f'{t}_{m.__name__}'] = m(y_val, y_pred)
            print(json.dumps(progress), file=sys.stderr)

    def predict_proba(self, X: np.ndarray, train: bool = False) -> np.ndarray:
        """Run the network and apply softmax to get probabilities."""
        for layer in self._layers:
            X = layer(X, train)
        return softmax(X, axis=1)

[X, y, X_test, y_test] = [
    np.load(f'data/{k}.npy')
    for k in ('train_data', 'train_label', 'test_data', 'test_label')
]
CLASSES = 10
classes = list(range(CLASSES))
y = label_binarize(y, classes=classes)
y_test = label_binarize(y_test, classes=classes)
combinations = itertools.product([0, 0.2], [0, 0.01], [0, 0.9])
metrics = [log_loss, acc, f1_macro, roc_auc_macro]
fields = ['Dropout', 'L2', 'Momentum'] + [
    f'{t} {m.__name__}{s}' for t in ['Train', 'Test'] for m in metrics
    for s in ['', ' std']
]
w = csv.DictWriter(sys.stdout, fieldnames=fields)
w.writeheader()
sys.stdout.flush()
trials = 5
train = np.zeros((trials, len(metrics)))
test = np.zeros((trials, len(metrics)))
for dropout, l2, momentum in combinations:
    row = {'Dropout': dropout, 'L2': l2, 'Momentum': momentum}
    for i in range(trials):
        model = Classifier([
            BatchNorm(X.shape[1], 0.99, 0.001),
            Dense(X.shape[1], 128, activation='relu', dropout=dropout, l2=l2),
            BatchNorm(128, 0.99, 0.001),
            Dense(128, 64, activation='relu', dropout=dropout, l2=l2),
            BatchNorm(64, 0.99, 0.001),
            Dense(64, y.shape[1]),
        ])
        model.fit(X, y, X_test, y_test, 1000, 50, 0.1, momentum=momentum)
        y_pred = model.predict_proba(X)
        y_test_pred = model.predict_proba(X_test)
        for k, m in enumerate(metrics):
            train[i][k] = m(y, y_pred)
            test[i][k] = m(y_test, y_test_pred)
    for t, a in ('Train', train), ('Test', test):
        mean, std = a.mean(axis=0), a.std(axis=0)
        for n, m, s in zip(metrics, mean, std):
            row[f'{t} {n.__name__}'] = m
            row[f'{t} {n.__name__} std'] = s
    w.writerow(row)
    sys.stdout.flush()

reason: import numpy,from scipy


text:
import h5py
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier

# 3 models for testing
def neuralNetwork(Xtrain, Ytrain, Xtest):
    # Neural network model
    clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(10,10),max_iter=3500)
    clf.fit(Xtrain, Ytrain)
    # calculate predictions
    predictions = clf.predict(Xtest)

    return predictions

def randomForest(Xtrain, Ytrain, Xtest):
    clf = RandomForestClassifier(n_estimators=14)
    clf.fit(Xtrain, Ytrain)
    prediction = clf.predict(Xtest)

    return prediction

def knn(Xtrain, Ytrain, Xtest):
    neigh = KNeighborsClassifier(n_neighbors=15)
    neigh.fit(Xtrain, Ytrain)
    prediction = neigh.predict(Xtest)

    return prediction

# Metrics
def accuracy(Ytest, prediction):
    accuracy = accuracy_score(Ytest, prediction)

    return accuracy

def precision(Ytest, prediction):
    precision = precision_score(Ytest, prediction,average='weighted')

    return precision

def recall(Ytest, prediction):
    recall = recall_score(Ytest, prediction,average='weighted')

    return recall

def Fmeasure(Ytest, prediction):
    Fmeasure = f1_score(Ytest, prediction,average='weighted')

    return Fmeasure

def print_metrics(X_train, Y_train, X_test, Y_test, prediction):
    accuracy_score = accuracy(Y_test, prediction)
    precision_score = precision(Y_test, prediction)
    recall_score = recall(Y_test, prediction)
    Fmeasure_score = Fmeasure(Y_test, prediction)

    print('The accuracy of the classifier is:', accuracy_score * 100, '%')
    print('The precision of the classifier is:', precision_score * 100, '%')
    print('The recall of the classifier is:', recall_score * 100, '%')
    print('The F-measure of the classifier is:', Fmeasure_score * 100, '%')

#Reading datasets saved in h5 format
print("Reading features and targets...")

h5f_features = h5py.File('data.h5','r')
h5f_targets = h5py.File('labels.h5','r')

gl_feat_dataset = h5f_features['dataset_1']
gl_targ_dataset = h5f_targets['dataset_1']

global_features = np.array(gl_feat_dataset)
global_targets = np.array(gl_targ_dataset)

h5f_features.close()
h5f_targets.close()

#Train/Test/Split
print("Splitting data into training and testing...")

X_train, X_test, y_train, y_test = train_test_split(global_features, global_targets, test_size=0.2)

# Random Forest
print('')
print('********** Random Forest classifier **********')
randomF_pred = randomForest(X_train, y_train, X_test)
print_metrics(X_train, y_train, X_test, y_test, randomF_pred)

# Neural Network
print('')
print('********** Neural Network classifier **********')
neural_pred = neuralNetwork(X_train, y_train, X_test)
print_metrics(X_train, y_train, X_test, y_test, neural_pred)

# kNN
print('')
print('********** kNN classifier **********')
knn_pred = knn(X_train, y_train, X_test)
print_metrics(X_train, y_train, X_test, y_test, knn_pred)

max_stars_repo_name: Mamxlam/legumeRecognition

max_issues_repo_name: Mamxlam/legumeRecognition

max_forks_repo_name: Mamxlam/legumeRecognition


text:
# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py:percent
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.3'
#       jupytext_version: 1.13.0
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# %%
import pickle
from os.path import join

import random
import itertools
import numpy as np
from sklearn.utils import shuffle
import matplotlib.pyplot as plt

# Tensorflow, Keras stuff
import tensorflow as tf
from tensorflow import keras

# Pre-processing
from tensorflow.data import AUTOTUNE
from tensorflow.image import stateless_random_flip_left_right, stateless_random_flip_up_down,                             stateless_random_brightness, stateless_random_contrast,                             stateless_random_crop, pad_to_bounding_box, central_crop,                             rot90

from tensorflow.keras.preprocessing.image import apply_affine_transform, random_shift, random_rotation, random_zoom, smart_resize
from zca import ZCA    #https://github.com/mwv/zca

# Create model
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, SpatialDropout2D, Dropout
from tensorflow.keras.optimizers import Adam, Nadam, SGD
from tensorflow.keras.regularizers import l1, l2, l1_l2
from tensorflow.keras.callbacks import ModelCheckpoint

# Reproducibility
from tensorflow.python.framework import ops
ops.reset_default_graph()   #tf.reset_default_graph()
tf.random.set_seed(0)
random.seed(0)
np.random.seed(0)

# %% [markdown]
# # Model

# %%
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3)))
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(SpatialDropout2D(0.3))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(SpatialDropout2D(0.3))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu', kernel_initializer='he_uniform'))   # , kernel_regularizer=l2(1e-4)
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))

model.summary()

# %%
""" from google.colab import drive
drive.mount('/gdrive') """

# %%
def unpickle(file):
    with open(file, 'rb') as f:
        dict = pickle.load(f, encoding="latin1")
    return dict

def one_hot(label):
    one_hot_matrix = np.zeros((len(label), 10))
    one_hot_matrix[range(len(label)),label] = 1
    return one_hot_matrix

def data_augmentation(img, label):
    seed = (0,1)
    seed2 = (0,2)
    augmented_img = [img,
                     stateless_random_flip_left_right(img, seed=seed).numpy(),
                     stateless_random_flip_up_down(img, seed=seed).numpy(),
                     random_zoom(img, zoom_range=(0.7, 0.7), row_axis=0, col_axis=1, channel_axis=2),
                     smart_resize(stateless_random_crop(img, size=(28, 28, 3), seed=seed2), size=(32,32), interpolation="bicubic").numpy(),
                     smart_resize(stateless_random_crop(img, size=(24, 24, 3), seed=seed), size=(32,32), interpolation="bicubic").numpy(),
                     smart_resize(stateless_random_crop(img, size=(20, 20, 3), seed=seed2), size=(32,32), interpolation="bicubic").numpy(),
                     rot90(img).numpy(),
                     rot90(img, k=2).numpy(),
                     apply_affine_transform(img, shear=np.random.randint(10, 20), zx=0.8, zy=0.8, row_axis=0, col_axis=1, channel_axis=2, fill_mode='reflect', order=3),
                     random_zoom(rot90(img, k=3).numpy(), zoom_range=(0.7, 0.7), row_axis=0, col_axis=1, channel_axis=2),
                     #random_shift(img, wrg=0.15, hrg=0.15, row_axis=0, col_axis=1, channel_axis=2),
                     #central_crop(img, 0.125),
                     #stateless_random_brightness(img, max_delta=0.2, seed=seed).numpy(), bug atm need to divide by 255 first
                     #stateless_random_contrast(img, lower=0.2, upper=0.5, seed=seed).numpy(), bug atm need to divide by 255 first
                     #random_rotation(img, rg=90, row_axis=0, col_axis=1, channel_axis=2),
                    ]

    augmented_label = [label]*len(augmented_img)

    return augmented_img, augmented_label

# %% [markdown]
# # Data processing

# %%
DATADIR = '../data/cifar-10-batches-py/' # '../data/cifar-10-batches-py/' ; '/gdrive/MyDrive/cifar-10-batches-py/'

# %% [markdown]
# ## Test data and label name

# %%
test_data = unpickle(join(DATADIR, 'test_batch'))
x_test = test_data["data"].reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float64")
y_test = np.array(test_data["labels"])

labeldict = unpickle(join(DATADIR, 'batches.meta'))
label_names = labeldict["label_names"]
label_names

# %% [markdown]
# ## Label
#
# **Transportation**:
# - airplane
# - ship
# - automobile, truck
#
# **Animal**:
# - bird
# - dog
# - frog
# - horse, deer
# %% [markdown]
# ## Augmentation experiment

# %%
""" # Test data
seed=(0,1)
test_data = unpickle(join(DATADIR, 'test_batch'))
X = test_data["data"].reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("int32")
Y = np.array(test_data["labels"])

i = 2000
plt.style.use('default')
plt.figure(1)
img = X[i]#/255
plt.imshow(img)
new_img=img
#new_img = random_shift(img, wrg=0.2, hrg=0.2, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest')
#new_img = random_zoom(img, zoom_range=(0.8, 0.8), row_axis=0, col_axis=1, channel_axis=2)
#new_img = random_rotation(img, rg=360, row_axis=0, col_axis=1, channel_axis=2, fill_mode='constant', cval=220)
new_img = apply_affine_transform(img, theta=0,
                                       tx=0,#np.random.uniform(0, 0.25),
                                       ty=0,#np.random.uniform(0, 0.25),
                                       shear=np.random.randint(-20, 20),#np.random.uniform(0, 0.25),
                                       zx=0.8,#np.random.uniform(0.75, 1),
                                       zy=0.8,#np.random.uniform(0.75, 1),
                                       row_axis=0, col_axis=1, channel_axis=2,
                                       fill_mode='reflect', order=3)
#new_img = stateless_random_flip_up_down(img, seed=seed).numpy()
#new_img=[smart_resize(stateless_random_crop(img, size=(28, 28, 3), seed=seed), size=(32,32), interpolation="bicubic").numpy()/255,
#         smart_resize(stateless_random_crop(img, size=(28, 28, 3), seed=(0,3)), size=(32,32), interpolation="bicubic").numpy()/255]
#plt.hist(new_img.ravel(), bins=500)
#plt.show()
plt.figure(2)
plt.imshow(new_img)
plt.title(f"Image {i} label={label_names[Y[i]]} (num {Y[i]})") """

# %% [markdown]
# ## Train data

# %%
""" val_counter = {c:0 for c in range(10)}
val_data = []
val_labels = [] """

amount_batch_to_use = 5
if 5 >= amount_batch_to_use > 0:
    trdata = []
    trlabels = []
    for batch_id in range(1, amount_batch_to_use+1):
        train_data = unpickle(join(DATADIR, f'data_batch_{batch_id}'))
        data = train_data["data"].reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float64")
        label = train_data["labels"]
        for i in range(len(data)):
            aug_images, labels = data_augmentation(data[i], label[i])
            trdata.extend(aug_images) # aug_images, [data[i]]
            trlabels.extend(labels) # labels, [label[i]]

            ''' if val_counter[c] < 500:
                val_data.append(data[c])
                val_labels.append(c)
                val_counter[c] += 1
            else:
                trdata.extend([data[c]] + [data_augmentation(data[c]) for _ in range(3)])  # 4 times amount of training data
                trlabels.extend([c]*4) '''

    x_train, y_train = shuffle(np.array(trdata), np.array(trlabels))
    # x_val, y_val = np.array(val_data), np.array(val_labels)

else:
    raise RuntimeError(f"Expect amount_batch_to_use from 1 to 5. Current amount_batch_to_use: {amount_batch_to_use}")

# %% [markdown]
# ## Pre-processing

# %%
# Normalization
x_train_mean  = np.mean(x_train,axis=0)
x_train_std = np.std(x_train,axis=0)
print("mean shape", x_train_mean.shape)
#print(x_train_std)
x_train_process = (x_train-x_train_mean)/(x_train_std+1e-5)
x_test_process = (x_test-x_train_mean)/(x_train_std+1e-5)

# ZCA whitening
""" x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
trf = ZCA().fit(x_train)
x_train = trf.transform(x_train).reshape(x_train.shape[0], 32, 32, 3)
x_test = trf.transform(x_test).reshape(x_test.shape[0], 32, 32, 3) """

print("min", np.amin(x_train_process), "max", np.amax(x_train_process))

# One-hot encoding label vector
y_test_onehot = one_hot(y_test)
y_train_onehot = one_hot(y_train)
#y_val_onehot = one_hot(y_val)

# %%
""" train_ds  = tf.data.Dataset.from_tensor_slices((x_train, y_train_onehot))
train_ds
aug_ds = train_ds.map(lambda x, y: (data_augmentation(x), y),
                      num_parallel_calls=AUTOTUNE)
                 .shuffle(0)
                 .batch(batch_size)
                 .prefetch(buffer_size=AUTOTUNE) """

# %% [markdown]
# # Training

# %%
batch_size = 64
max_epoch = 300
optimizer = SGD(learning_rate=0.001, nesterov=True, momentum=0.9)   #Adam(learning_rate=3e-4)

model.compile(
    optimizer=optimizer,
    loss=keras.losses.CategoricalCrossentropy(),
    metrics=[keras.metrics.CategoricalAccuracy()],
)

model_checkpoint = ModelCheckpoint('model_checkpoints/CIFAR10_epoch{epoch:02d}.h5', save_freq=25*(1+50000*11//64), save_weights_only=False)
history = model.fit(
    x_train_process,
    y_train_onehot,
    batch_size=batch_size,
    epochs=max_epoch,
    validation_data=(x_test_process, y_test_onehot),
    callbacks=[model_checkpoint],
    #shuffle=False
)

# %% [markdown]
# # Evaluation

# %%
# Save model
#model = tf.keras.models.save_model('model_checkpoints/CIFAR10_final.h5', save_weights_only=False)

# Load model
#model = tf.keras.models.load_model('model_checkpoints/CIFAR10_final.h5')

results = model.evaluate(x_test_process, y_test_onehot, batch_size=512)
print("test loss, test acc:", results)

# %%
start_epoch = 5 # start from 1
start_epoch_id = start_epoch - 1
xticks = range(start_epoch, max_epoch + 1)

plt.rc('font', size=14)
plt.rc('axes', titlesize=15)
plt.rc('axes', labelsize=12)
plt.rc('legend', fontsize=9)
plt.rcParams['figure.dpi'] = 90
plt.style.use('ggplot')

fig, (ax_loss, ax_acc) = plt.subplots(nrows=2, ncols=1, figsize=(14, 9), sharex=True)
ax_loss.set_title(f'Tess loss and accuracy from epoch 5 (final {results[1]*100:.2f}%)\n')

# Plot loss
ax_loss.plot(xticks, history.history['loss'][start_epoch_id:], label='training loss')
val_loss = history.history['val_loss']
ax_loss.plot(xticks, val_loss[start_epoch_id:], label='validation loss')
ax_loss.set_ylabel('Loss')
ax_loss.grid(True)
ax_loss.legend(loc="upper right")

# Annotate highest loss and its epoch (and accuracy)
best_loss_id = np.argmin(np.array(val_loss))
best_loss = round(val_loss[best_loss_id], 2)
ax_loss.plot(best_loss_id+1, best_loss, 'ko', markersize=5)
x_shift, y_shift = 1, 0.03
ax_loss.annotate(f"epochs {best_loss_id+1}, {best_loss}", xy=(best_loss_id, best_loss), xytext=(best_loss_id+x_shift, best_loss+y_shift), fontsize="small")

# Plot accuracy
ax_acc.plot(xticks, np.array(history.history['categorical_accuracy'][start_epoch_id:])*100, label='training accuracy')
val_acc = np.around(np.array(history.history['val_categorical_accuracy'])* 100, decimals=4)
ax_acc.plot(xticks, val_acc[start_epoch_id:], label='validation accuracy')
ax_acc.set_xlabel('Epochs')
ax_acc.set_ylabel('Accuracy (%)')
ax_acc.grid(True)
ax_acc.legend(loc='lower right')

# Plot current best
current_best_acc = 85.00
ax_acc.hlines(y=current_best_acc, xmin=5, xmax=max_epoch, linewidth=1, linestyles="dashed", color="#000000", alpha=0.4)
ax_acc.annotate("Current best", xy=(5, current_best_acc+0.5), fontsize="medium")

# Annotate highest accuracy and its epoch (and loss)
best_acc_id = np.argmax(np.array(val_acc))
best_acc = val_acc[best_acc_id]
ax_acc.plot(best_acc_id+1, best_acc, 'ko', markersize=5)
x_shift, y_shift = 1, -2
ax_acc.annotate(f"epochs {best_acc_id+1}, {best_acc}%", xy=(best_acc_id, best_acc), xytext=(best_acc_id+x_shift, best_acc+y_shift), fontsize="small")

# Fixing xlabel ticks
epochs_list = range(0, max_epoch + 1, 25)
for i, num in enumerate(epochs_list):
    if num > start_epoch:
        xticks_plot = [start_epoch] + list(epochs_list[i:])
        break
ax_acc.xaxis.set_ticks(xticks_plot)
""" ax_acc.xaxis.set_major_locator(mticker.MaxNLocator(5))
ticks_loc = ax_acc.get_xticks().tolist()
ax_acc.xaxis.set_major_locator(mticker.FixedLocator(ticks_loc))
ax_acc.set_xticklabels([x for x in ticks_loc]) """

print(f"Best loss at epoch {best_loss_id+1} {best_loss}, and accuracy {val_acc[best_loss_id]}%")
print(f"Best accuracy at epoch {best_acc_id+1} {best_acc}%, and loss {round(val_loss[best_acc_id], 2)}")

fig.tight_layout()
plt.show()

# %%
# https://www.kaggle.com/grfiv4/plot-a-confusion-matrix/notebook
def plot_confusion_matrix(cm,
                          target_names,
                          title='Confusion matrix',
                          cmap=None,
                          normalize=True):

    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(8, 6))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()

    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names, rotation=45)
        plt.yticks(tick_marks, target_names)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    plt.grid(False)
    plt.show()

onehot_prediction = model(x_test_process)
prediction = tf.math.argmax(onehot_prediction, axis=1)
confusion_matrix = tf.math.confusion_matrix(y_test, prediction, num_classes=10).numpy()

plot_confusion_matrix(cm=confusion_matrix,
                      normalize=False,
                      target_names=label_names)

# %%
# Pick cat with highest probability of dog
test_data = unpickle(join(DATADIR, 'test_batch'))
X = test_data["data"].reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("int32")

cat_onehot_list = onehot_prediction[y_test==3].numpy()
sort_id_big_cat = np.argsort(cat_onehot_list[:,5])

# %%
top = 10
misclassified_dog_id = sort_id_big_cat[-top]
cat_img = x_test[y_test==3]     # _process

plt.figure(figsize=(1.5,1.5))
plt.imshow(np.squeeze(cat_img[misclassified_dog_id]).astype('int'))
plt.grid(False)
plt.title(f"Cat predicted as dog (top {top})", fontsize=8)
plt.xticks([])
plt.yticks([])

plt.figure(figsize=(6,3.5))
plt.bar(range(10), cat_onehot_list[misclassified_dog_id])
plt.yscale('log')
plt.xticks(range(10), label_names, rotation=60, fontsize=7)
plt.gca().yaxis.grid(True)
plt.show()

# %% [markdown]
# # Compare to other machine learning methods

# %%
xaxis = range(3)
accuracy_list = [35.39, 43.09, best_acc]
fig, ax = plt.subplots(figsize=(8, 7.5))
ax.set_title("Comparison between 3 machine learning methods")
ax.bar(xaxis, accuracy_list, width=0.5, tick_label=["1-nn", "bayes 16x16", "VGG16-based neural network"])
ax.set_ylabel("Accuracy (%)")

for i, acc in enumerate(accuracy_list):
    ax.annotate(f"{acc:.2f}", xy=(i-0.1, acc+1), fontsize=12)

plt.tight_layout()
plt.grid(False)
plt.show()


text:
import numpy as np
import pandas as pd

from IMLearn.learners.classifiers import Perceptron, LDA, GaussianNaiveBayes
from typing import Tuple
from utils import *
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from math import atan2, pi

def load_dataset(filename: str) -> Tuple[np.ndarray, np.ndarray]:
    """
    Load dataset for comparing the Gaussian Naive Bayes and LDA classifiers. File is assumed to be an
    ndarray of shape (n_samples, 3) where the first 2 columns represent features and the third column the class

    Parameters
    ----------
    filename: str
        Path to .npy data file

    Returns
    -------
    X: ndarray of shape (n_samples, 2)
        Design matrix to be used

    y: ndarray of shape (n_samples,)
        Class vector specifying for each sample its class

    """
    data = np.load(filename)
    return data[:, :2], data[:, 2].astype(int)

def run_perceptron():
    """
    Fit and plot fit progression of the Perceptron algorithm over both the linearly separable and inseparable datasets

    Create a line plot that shows the perceptron algorithm's training loss values (y-axis)
    as a function of the training iterations (x-axis).
    """
    def create_callback(losses: list, X: np.ndarray, y: np.ndarray):
        def callback(p: Perceptron, sample: np.ndarray, classification: int):
            losses.append(p.loss(X,y))
        return callback

    for n, f in [("Linearly Separable", "linearly_separable.npy"), ("Linearly Inseparable", "linearly_inseparable.npy")]:
        # Load dataset
        title = n
        X, y = load_dataset("../datasets/"+f)

        # Fit Perceptron and record loss in each fit iteration
        losses = []
        perceptron = Perceptron(callback=create_callback(losses, X, y))
        perceptron.fit(X,y)

        # Plot figure of loss as function of fitting iteration
        fig = go.Figure([go.Scatter(x=np.arange(1,len(losses)+1,1, dtype=int), y=np.array(losses))])
        fig.update_layout(title="Loss of Perceptron per iteration with "+title+" data")
        fig.update_layout(xaxis_title="Iterations")
        fig.update_layout(yaxis_title="Loss")
        fig.show()

def get_ellipse(mu: np.ndarray, cov: np.ndarray):
    """
    Draw an ellipse centered at given location and according to specified covariance matrix

    Parameters
    ----------
    mu : ndarray of shape (2,)
        Center of ellipse

    cov: ndarray of shape (2,2)
        Covariance of Gaussian

    Returns
    -------
        scatter: A plotly trace object of the ellipse
    """
    l1, l2 = tuple(np.linalg.eigvalsh(cov)[::-1])
    theta = atan2(l1 - cov[0, 0], cov[0, 1]) if cov[0, 1] != 0 else (np.pi / 2 if cov[0, 0] < cov[1, 1] else 0)
    t = np.linspace(0, 2 * pi, 100)
    xs = (l1 * np.cos(theta) * np.cos(t)) - (l2 * np.sin(theta) * np.sin(t))
    ys = (l1 * np.sin(theta) * np.cos(t)) + (l2 * np.cos(theta) * np.sin(t))

    return go.Scatter(x=mu[0] + xs, y=mu[1] + ys, mode="lines", marker_color="black")

def compare_gaussian_classifiers():
    """
    Fit both Gaussian Naive Bayes and LDA classifiers on both gaussians1 and gaussians2 datasets
    """
    for f in ["gaussian1.npy", "gaussian2.npy"]:
        # Load dataset
        X, y = load_dataset("../datasets/" + f)

        # Fit models and predict over training set
        lda = LDA()
        lda.fit(X,y)

        bayes = GaussianNaiveBayes()
        bayes.fit(X,y)

        # Plot a figure with two suplots, showing the Gaussian Naive Bayes predictions on the left and LDA predictions
        # on the right. Plot title should specify dataset used and subplot titles should specify algorithm and accuracy
        # Create subplots
        from IMLearn.metrics import accuracy
        fig = make_subplots(rows=1, cols=2)

        lda_pred = lda.predict(X)
        bayes_pred = bayes.predict(X)

        lda_acc = accuracy(y, lda_pred)
        bayes_acc = accuracy(y, bayes_pred)

        fig = make_subplots(rows=1, cols=2, subplot_titles=[
            "LDA model (accuracy: " + str(lda_acc)+")",
            "Naive Gaussian model (accuracy: " + str(bayes_acc)+")"])
        fig.update_layout(title_text=f)

        fig.add_trace(
            go.Scatter(x=X[:, 0], y=X[:, 1], mode="markers",
                       marker=dict(color=lda_pred, symbol=y),
                       showlegend=False),
            row=1, col=1
        )

        fig.add_trace(
            go.Scatter(x=X[:, 0], y=X[:, 1], mode="markers",
                       marker=dict(color=bayes_pred, symbol=y),
                       showlegend=False),
            row=1, col=2
        )

        fig.add_trace(
            go.Scatter(x=lda.mu_[:,0], y=lda.mu_[:,1], mode="markers", marker=dict(color="black", symbol="x"),showlegend=False),row=1, col=1)
        fig.add_trace(
            go.Scatter(x=bayes.mu_[:, 0], y=bayes.mu_[:, 1], mode="markers", marker=dict(color="black", symbol="x"),
                       showlegend=False), row=1, col=2)

        fig.add_traces([get_ellipse(mu, lda.cov_) for mu in lda.mu_], rows=1, cols=1)
        fig.add_traces([get_ellipse(bayes.mu_[k], bayes.cov_[k]) for k in range(len(bayes.classes_))], rows=1, cols=2)

        fig.update_layout(height=600, width=1400)
        fig.show()

if __name__ == '__main__':
    np.random.seed(0)
    run_perceptron()
    compare_gaussian_classifiers()

    # X = np.array([[1,1],[1,2],[2,3],[2,4],[3,3],[3,4]])
    # y = np.array([0,0,1,1,1,1])
    # gauss = GaussianNaiveBayes()
    # gauss.fit(X,y)
    # print(gauss.mu_)
    # print(gauss.vars_)


text:
# Library for customized functions
import numpy as np
import os, os.path
import cv2
import imutils

# fileName = "/home/hduser/Documents/experiment/vg_text/001.tif"

def checkValidExtension ( fileName ):
    '''
        =======================================
        Check for the valid file extension in
        given file name or string passed
        =======================================

        Arguments:
            fileName: file name or filename string
    '''

    valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff", "jpg", "jpeg", "png", "tif",
                              "tiff"]  # specify your valid extensions here
    valid_image_extensions = [item.lower () for item in valid_image_extensions]

    if fileName.__contains__ ('/'):
        extension = fileName.lower ().split ("/")[-1].split ('.')[-1]
    else:
        extension = fileName.lower ().split ('.')[-1]

    # print extension
    if valid_image_extensions.__contains__ (extension):
        # print "Contains " + str (extension)
        return True
    else:
        return False

# checkValidExtension(fileName)

def pyramid ( image, scale=1.5, minSize=(15, 15) ):
    '''
        ========================
        Returns an image pyramid
        ========================

        Arguments:
            image: Image file or object
            scale: Decreasing with a specified ratio
            minSize: Minimum image size

        Return:
            image: Sliced image
    '''
    # yield the original image
    yield image

    # keep looping over the pyramid
    while True:
        # compute the new dimensions of the image and resize it
        w = int (image.shape[1] / scale)
        image = imutils.resize (image, width=w)

        # if the resized image does not meet the supplied minimum
        # size, then stop constructing the pyramid
        if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
            break

        # yield the next image in the pyramid
        yield image

def sliding_window ( image, stepSize, windowSize ):
    '''
        Splits the image based on the window size and stride length

        Arguments:
            image: Pass image object or the numpy array
            stepSize: Stride length for frame
            windowSize: window size for the patch from image to extract
        Returns:
            x, y: Corrdinate of the main image with
            image: Cropped image of dimensions with respect to image size
                    and given window size and stride length
    '''
    # slide a window across the image
    for y in xrange (0, image.shape[0], stepSize):
        for x in xrange (0, image.shape[1], stepSize):
            # yield the current window
            yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])

def createDirectory ( path ):
    '''
        ================================
        Create directory for a give path
        ================================

        Arguments:
            path: Directory name or path of the directory

        Returns:
            No return values
    '''
    if os.path.exists (path):
        print "Directory exists"
    elif not os.path.exists (path):
        os.mkdir (path)
        print "Directory created successfully: " + str (path)
    else:
        print "Could not create Directory"

def resizeToMainWindowSize ( image, winSize ):
    '''
        ==================================================
        Resize the window size for larger than given image
        ==================================================

        Arguments:
            image: Image you want to resize
            winSize: Window size of the image
        Returns:
            Resize image of given window size
    '''
    if type (winSize) == int:
        return cv2.resize (image, (winSize, winSize), interpolation=cv2.INTER_CUBIC)
    elif type (winSize) == []:
        return cv2.resize (image, (winSize[0], winSize[1]), interpolation=cv2.INTER_CUBIC)
    elif type (winSize) == ():
        return cv2.resize (image, (winSize), interpolation=cv2.INTER_CUBIC)

def drawRectangleOnImage ( image, x, y, winSize ):
    clone = image.copy ()
    if type (winSize) == int:
        cv2.rectangle (clone, (x, y), (x + winSize[0], y + winSize[1]), (0, 255, 0), 2)
        return clone

def drawROI ( img, x, y, alp, winsz, color ):
    '''
        It will draw the ROI as transparent window with border over image

        Args:
            img   : Image which you want to draw the ROI
            x, y  : x and y starting coordinates for the ROI
            alp   : Alpha or transparency value between 0.0 and 1.0
            winsz : Windows size of the ROI winsz x winsz single value
            color : Same color for the border and the filled color in square

        Returns:
            Returns the processed image

            image = drawROI('image.jpg',30,50,,0.6,20,(0,255,0))

    '''
    ovly = img.copy ()
    out = img.copy ()

    # Draw filled rectangle
    cv2.rectangle (ovly, (x, y), (x + winsz, y + winsz), (color), -1)
        # Draw line or border for rectangle
    cv2.rectangle (ovly, (x, y), (x + winsz, y + winsz), (color), 2)
    cv2.addWeighted (ovly, alp, out, 1 - alp, 0, out)

    return out

def dispOpticalFlow (Image, Flow, Divisor, name ):
    """

    Display image with a visualisation of a flow over the top.
    A divisor controls the density of the quiver plot.

    Arguments:
        Image:      Image on which to display flow lines
        Flow :      Flow vectors x and y
        Divisor:    Spacing between the arrow nodes
        name:       Name of the window
    """
    PictureShape = np.shape (Image)
    # determine number of quiver points there will be
    Imax = int (PictureShape[0] / Divisor)
    Jmax = int (PictureShape[1] / Divisor)
    # create a blank mask,   on which lines will be drawn.
    mask = np.zeros_like (Image)
    panel = np.zeros_like (Image)

    for i in range (1, Imax):
        for j in range (1, Jmax):
            X1 = (i) * Divisor
            Y1 = (j) * Divisor

            X2 = int (X1 + Flow[X1, Y1, 1])
            Y2 = int (Y1 + Flow[X1, Y1, 0])
            X2 = np.clip (X2, 0, PictureShape[0])
            Y2 = np.clip (Y2, 0, PictureShape[1])
            # add all the lines to the mask

            mask = cv2.arrowedLine (mask, (Y1, X1), (Y2, X2), [255, 255, 255], 1)
            # To show only arrows in the image
            # cv2.namedWindow("Panel", 0)
            # panel = panel+mask
            # cv2.imshow("Panel", panel)
    # superpose lines onto image

    img = cv2.add (Image, mask)
    # print image
    cv2.startWindowThread ()
    cv2.namedWindow (name, 0)
    cv2.imshow (name, img)

    return []

def exitScript():
    ky = cv2.waitKey (1) & 0xff
    if ky == 27:
        print "Exiting the program"
        os._exit(0)
    elif ky == ord ('q'):
        print "Exiting the program"
        os._exit(0)
    elif ky == ord ('e'):
        print "Exiting the program"
        os._exit (0)

max_stars_repo_name: nabulago/anomalous-event-detection

max_issues_repo_name: Tiacy/anomaly-event-detection

max_forks_repo_name: Tiacy/anomaly-event-detection


text:
import argparse
import numpy as np

import cv2

parser = argparse.ArgumentParser(description="Processes an input for header")
parser.add_argument("input", type=str, help="Name of input image.")
parser.add_argument("--output", type=str, help="Filename of output image.")

def main():
    args = parser.parse_args()
    image = cv2.imread(args.input)
    image = cv2.GaussianBlur(image, (7,7), 0)
    image = np.ndarray.astype(image, np.int32)
    image[:, :, :] = np.maximum(0, image[:, :, :] - 0.5 * image[:, :, :] - 15)  # This is HSV, not RGB!
    image = np.ndarray.astype(image, np.uint8)
    cv2.imwrite(args.output, image)

if __name__ == "__main__":
    main()

max_stars_repo_name: ICRS/website-staging

max_issues_repo_name: ICRS/website-staging

max_forks_repo_name: ICRS/website-staging


text:
import subprocess
lib_list = ['numpy','ymmsl','sobol_seq','csv','seaborn','zenodo_get']
for lib_name in lib_list:
	try:
		import lib_name
	except ImportError:
		if lib_name == 'csv':
			print(lib_name,' Module not installed')
			subprocess.run(['pip','install','python-csv'])
		else:
			print(lib_name,' Module not installed')
			subprocess.run(['pip','install','%s'%lib_name])

import numpy as np
import ymmsl
import sobol_seq
import csv
import os
import seaborn as sns
import zenodo_get

# Transform the normalized sample matrix to ranges of uncertain parameters
def dim_transform(sobol_vector,uncertain_list):

    dim = len(uncertain_list)
    for num_dim in range(dim):
        para_max = uncertain_list[num_dim].get('max')
        para_min = uncertain_list[num_dim].get('min')
        sobol_vector[:,num_dim] = para_min + (para_max-para_min)*sobol_vector[:,num_dim]

    return sobol_vector

####################################################################################
##### Sample generation and UQ campaign creation (including instances folder)#######
####################################################################################
# Note:
#  This is used to generate UQ samples for only four biological parameters:
#	1) Endothelium endpoint 2)smc max stran 3)balloon extension 4) Fenestration probability

# Naming of Folder and files for samples
# Level 0:    UQtest                                    (UQ campaign name)
# Level 1:    UQtest/A                                  (sample matrix of sobol sequence)
# Level 2:    UQtest/A/A_X where X vary from 1 -> N     (N: number of samples)
# Level 3:    UQtest/A/A_X/input.ymmsl

### Main function
# Number of samples for UQ
# Note that ISR3D is a computationally intensive application.
# Running 128 instances would need some cluster resources
# You can start with a small number, 16 for instances.
NumSample = 128

# Template path to the ymmsl file (relative path from ISR3D/Result/UQtest/ to ISR3D/UQ/template/input_stage4.ymmsl)
input_path = '../../UQ/template/'
input_ymmsl_filename = 'input_stage4.ymmsl'

# Output directory for UQ campagin folder and name
output_path = './'
experiment_name = 'UQtest'

# Read in the data of template ymmsl file
with open(input_path+input_ymmsl_filename,'r') as f:
    ymmsl_data = ymmsl.load(f)

# Take out the unchanged model part and need-for-change settings part for ymmsl model
model = ymmsl_data.model
settings = ymmsl_data.settings

# Set uncertain parameters and its ranges as a list
ymmsl_uncertain_parameters = [
        {
            'name': 'smc.endo_endpoint',
            'min': 10.0,
            'max': 20.0
        },
        {
            'name': 'smc.balloon_extension',
            'min': 0.5,
            'max': 1.5
        },
        {
            'name': 'smc.smc_max_strain',
            'min': 1.2,
            'max': 1.8
        },
        {
            'name': 'smc.fenestration_probability',
            'min': 0.0,# Calculate the lumen volume from (lumen_area_of_each_slice*depth_of_slice)
            'max': 0.1
        }]

# Count the total uncertain input dimensions (here 4 parameters)
num_uncer_para = len(ymmsl_uncertain_parameters)
print('Number of uncertain parameter: '+str(num_uncer_para))

# Generate sobel sequence range (0,1), save the file and transform to (min,max)
A = sobol_seq.i4_sobol_generate(num_uncer_para,NumSample)
A = dim_transform(A,ymmsl_uncertain_parameters)
np.savetxt("A.csv",A)

# Create corresponding directory and folders
try:
    os.mkdir(output_path+experiment_name)
except OSError:
    print ("Creation of the directory %s failed" % output_path+experiment_name)
else:
    print ("Successfully created the directory %s" % output_path+experiment_name)

# A: Replace the corresponding value within the dict and output the file
os.mkdir(output_path+experiment_name+'/A')
checklist = ['A']

for n in range(NumSample):
    sample_path = output_path+experiment_name+'/A'+'/A_'+str(n)
    os.mkdir(sample_path)

    # Generate file for ymmsl
    num_para = 0
    for para in ymmsl_uncertain_parameters:
        settings[para.get('name')] = float(A[n,num_para])
        num_para = num_para + 1

    config = ymmsl.Configuration(model, settings, None, None)
    with open(sample_path+'/input_stage4.ymmsl', 'w') as f:
        ymmsl.save(config, f)

print('ymmsl input for each UQ instance has been generated')

####################################################################################
##### Run shell script to broadcast other input files to each sample folder#########
####################################################################################
import subprocess
# Download Other input files from Zenodo
print('Start to download other input files for ISR3D from Zenodo')
subprocess.run(['wget https://zenodo.org/record/4603912/files/stage3.test_vessel.dat'],shell = True)
subprocess.run(['wget https://zenodo.org/record/4603912/files/stage3.test_vessel_nb.dat'],shell = True)
subprocess.run(['wget https://zenodo.org/record/4603912/files/test_vessel_centerline.csv'],shell = True)
print('Start to broadcast the input to each UQ instance directory')
# Template path to the ymmsl file (relative path from ISR3D/Result/UQtest/ to ISR3D/UQ/function/BCastStage3.sh)
pass_arg = str(NumSample)
subprocess.run(['bash','../../UQ/function/BCastStage3.sh', '%s'%pass_arg])
print('Sample generation done')


text:
"""
The MIT License (MIT)

@author: Stephen J. Maher
"""
import numpy as np

STOCH_RHS   = "rhs"
STOCH_COEF  = "coef"
STOCH_OBJ   = "obj"
STOCH_TYPES = [STOCH_RHS, STOCH_COEF, STOCH_OBJ]

RHS     = "RHS"
ROWS    = "ROWS"
COLUMNS = "COLUMNS"
PERIODS = "PERIODS"

class Instance:
   def __init__(self, corfile = None, timfile = None, stofile = None):
      self.corfile = corfile
      self.timfile = timfile
      self.stofile = stofile;
      self.constraints = []
      self.variables = []
      self.coeffs = {}
      self.rhs = {}
      self.periods = []

   def readInstance(self, readCor = False, readTim = False, readSto = False):
      '''
      reads the specified files for the instance in SMPS format
      '''
      if readCor:
         self.readCorFile()

      if readTim:
         self.readTimFile()

      if readSto:
         self.readStoFile()

   def readCorFile(self):
      '''
      reads a COR file for an SMPS instance
      '''
      assert self.corfile is not None
      with open(self.corfile, "r") as infile:
         while True:
            line = infile.readline()
            if not line: break

            # if the line does not start with a space, then we are in a new
            # section
            if not line.startswith(" "):
               section = None

            # reading each line and storing the important information
            if line.startswith(ROWS):
               section = ROWS
            if line.startswith(COLUMNS):
               section = COLUMNS
            if line.startswith(RHS):
               section = RHS

            if line.startswith(" "):
               if section == ROWS:
                  self.storeConstraints(line)
               if section == COLUMNS:
                  self.storeVariables(line)
               if section == RHS:
                  self.storeConsRhs(line)

   def readTimFile(self):
      '''
      reads a TIM file for an SMPS instance
      '''
      assert self.timfile is not None
      with open(self.timfile, "r") as infile:
         while True:
            line = infile.readline()
            if not line: break

            # if the line does not start with a space, then we are in a new
            # section
            if not line.startswith(" "):
               section = None

            # reading each line and storing the important information
            if line.startswith(PERIODS):
               section = PERIODS

            if line.startswith(" "):
               if section == PERIODS:
                  self.storePeriods(line)

   def readStoFile(self):
      '''
      reads a STO file for an SMPS instance
      '''
      pass

   def writeTimFile(self):
      '''
      writes the TIM file of the SMPS format
      '''
      assert self.timfile is not None
      with open(self.timfile, 'w') as outfile:
         outfile.write("TIME\n")
         outfile.write("PERIODS   LP\n")

         self.writeStageFile(outfile)

         outfile.write("ENDATA")

   def writeStoFile(self, nscenarios, stochtype = STOCH_RHS):
      '''
      writes an STO file
      '''
      assert self.stofile is not None
      assert stochtype in STOCH_TYPES

      np.random.seed(nscenarios)
      with open(self.stofile, 'w') as outfile:
         # writing the header of the STO file
         outfile.write("STOCH\n")
         outfile.write("SCENARIOS     DISCRETE\n")

         if stochtype == STOCH_RHS:
            self.writeRhsStochasticFile(outfile, nscenarios)
         elif stochtype == STOCH_COEF:
            self.writeCoefStochasticFile(outfile, nscenarios)
         elif stochtype == STOCH_OBJ:
            self.writeObjStochasticFile(outfile, nscenarios)

         outfile.write("ENDATA")

   def writeSmpsFile(self):
      '''
      writes the SMPS file for the generated problem
      '''
      smpsfile = "%s.smps"%(self.stofile.split('.')[0])
      with open(smpsfile, 'w') as outfile:
         outfile.write("%s\n"%self.corfile)
         outfile.write("%s\n"%self.timfile)
         outfile.write("%s\n"%self.stofile)

   def storeConstraints(self, line):
      '''
      stores the constraints of the problem
      '''
      linelist = line.split()
      if linelist[0] != 'N':
         # storing the constraint name as the key of the dictionary. The
         # constraints are initials given an rhs of 0
         self.constraints.append(linelist[1])

   def storeVariables(self, line):
      '''
      stores the variables of the problem
      '''
      # if the MARKER keyword is found, then we exit the function
      if "MARKER" in line:
         return

      linelist = line.split()
      # if the variables array is empty or the new variable name differs from
      # the last stored name, then we store the variable
      if len(self.variables) == 0 or linelist[0] != self.variables[-1]:
         self.variables.append(linelist[0])

      self.coeffs[linelist[0], linelist[1]] = float(linelist[2])

      if len(linelist) == 5:
         self.coeffs[linelist[0], linelist[3]] = float(linelist[4])

   def storeConsRhs(self, line):
      '''
      stores the RHS for constraints given by the line
      '''
      linelist = line.split()

      self.rhs[linelist[1]] = float(linelist[2])

      # the line list will be of length 5 if there are two RHS
      if len(linelist) == 5:
         self.rhs[linelist[3]] = float(linelist[4])

   def storePeriods(self, line):
      '''
      stores the periods for the variables and constraints
      '''
      linelist = line.split()

      self.periods.append([linelist[2], linelist[0], linelist[1]])

   def writeRhsStochasticFile(self, outfile, nscenarios):
      '''
      writes the scenarios with RHS stochasticity
      '''
      print("The write RHS stochastic file has not been implemented")

   def writeCoefStochasticFile(self, outfile, nscenarios):
      '''
      writes the scenarios with coefficient stochasticity
      '''
      print("The write coefficient stochastic file has not been implemented")

   def writeObjStochasticFile(self, outfile, nscenarios):
      '''
      writes the scenarios with objective stochasticity
      '''
      print("The write objective stochastic file has not been implemented")

   def writeStageFile(self, outfile):
      '''
      writes the stages (.tim) file for a given core file
      '''
      print("The write stages (.tim) file has not been implemented")

max_stars_repo_name: stephenjmaher/GenStoch

max_issues_repo_name: stephenjmaher/GenStoch

max_forks_repo_name: stephenjmaher/GenStoch


text:
"""
2017/05/18 At the moment the functions here are not being revealed
    to the users.  I need to check if any of these functions are being
    called from other functions in pytheos.
"""
import scipy.constants as constants
import numpy as np
from scipy.integrate import odeint
import uncertainties as uct
from uncertainties import unumpy as unp
from .eqn_therm_constq import constq_pth
from .eqn_hugoniot import hugoniot_p, hugoniot_t
from .conversion import vol_uc2mol
from .eqn_debye import debye_E
from .etc import isuncertainties

"""
def _get_rho(self, v):
    v_mol = v / self.z * constants.Avogadro / 1.e24
    rho = self.mass / v_mol  # g/cm^3
    return rho

def _get_v(self, rho):
    v_mol = self.mass / rho
    v = v_mol * self.z / constants.Avogadro * 1.e24
    return v
"""

def jamieson_pst(v, v0, c0, s, gamma0, q, theta0, n, z, mass, c_v,
                 three_r=3. * constants.R, t_ref=300.):
    """
    calculate static pressure at 300 K from Hugoniot data using the constq
    formulation

    :param v: unit-cell volume in A^3
    :param v0: unit-cell volume in A^3 at 1 bar
    :param c0: velocity at 1 bar in km/s
    :param s: slope of the velocity change
    :param gamma0: Gruneisen parameter at 1 bar
    :param q: logarithmic derivative of Gruneisen parameter
    :param theta0: Debye temperature in K
    :param n: number of elements in a chemical formula
    :param z: number of formula unit in a unit cell
    :param mass: molar mass in gram
    :param c_v: heat capacity
    :param three_r: 3 times gas constant.
        Jamieson modified this value to compensate for mismatches
    :param t_ref: reference temperature, 300 K
    :return: static pressure in GPa
    :note: 2017/05/18 I am unsure if this is actually being used in pytheos
    """
    rho = mass / vol_uc2mol(v, z) * 1.e-6
    rho0 = mass / vol_uc2mol(v0, z) * 1.e-6
    p_h = hugoniot_p(rho, rho0, c0, s)
    p_th_h = jamieson_pth(v, v0, c0, s, gamma0, q, theta0, n, z, mass, c_v,
                          three_r=three_r, t_ref=t_ref)
    p_st = p_h - p_th_h
    return p_st

def jamieson_pth(v, v0, c0, s, gamma0, q, theta0, n, z, mass, c_v,
                 three_r=3. * constants.R, t_ref=300.):
    """
    calculate thermal pressure from Hugoniot data using the constq
    formulation

    :param v: unit-cell volume in A^3
    :param v0: unit-cell volume in A^3 at 1 bar
    :param c0: velocity at 1 bar in km/s
    :param s: slope of the velocity change
    :param gamma0: Gruneisen parameter at 1 bar
    :param q: logarithmic derivative of Gruneisen parameter
    :param theta0: Debye temperature in K
    :param n: number of elements in a chemical formula
    :param z: number of formula unit in a unit cell
    :param mass: molar mass in gram
    :param c_v: heat capacity
    :param three_r: 3 times gas constant.
        Jamieson modified this value to compensate for mismatches
    :param t_ref: reference temperature, 300 K
    :return: static pressure in GPa
    :note: 2017/05/18 I am unsure if this is actually being used in pytheos
    """
    rho = mass / vol_uc2mol(v, z) * 1.e-6
    rho0 = mass / vol_uc2mol(v0, z) * 1.e-6
    temp = hugoniot_t(rho, rho0, c0, s, gamma0, q, theta0, n, mass,
                      three_r=three_r, t_ref=t_ref, c_v=c_v)
    pth = constq_pth(v, temp, v0, gamma0, q, theta0, n, z, t_ref=t_ref,
                     three_r=three_r)
    return pth

def hugoniot_p_nlin(rho, rho0, a, b, c):
    """
    calculate pressure along a Hugoniot throug nonlinear equations
    presented in Jameison 1982

    :param rho: density in g/cm^3
    :param rho0: density at 1 bar in g/cm^3
    :param a: prefactor for nonlinear fit of Hugoniot data
    :param b: prefactor for nonlinear fit of Hugoniot data
    :param c: prefactor for nonlinear fit of Hugoniot data
    :return: pressure along Hugoniot in GPa
    """
    eta = 1. - (rho0 / rho)
    Up = np.zeros_like(eta)
    if isuncertainties([rho, rho0, a, b, c]):
        Up[eta != 0.] = ((b * eta - 1.) + unp.sqrt(
            np.power((1. - b * eta), 2.) - 4. * np.power(eta, 2.) * a * c)) /\
            (-2. * eta * c)
    else:
        Up[eta != 0.] = ((b * eta - 1.) + np.sqrt(
            np.power((1. - b * eta), 2.) - 4. * np.power(eta, 2.) * a * c)) /\
            (-2. * eta * c)
    Us = a + Up * b + Up * Up * c
    Ph = rho0 * Up * Us
    return Ph

"""
The lines below do not work.  The problem is in dUp_delta.  But without this
approach, I can still use the linear case to make similar value as Jamieson.
In fact, in mdaap this part is there but never been called because of Internal
bug.  At the moment, I do not want to struggle with this anymore as the value
I got is decent.
def _dT_h_delta_nlin(T_in_kK, eta, k, threenk, c_v):

    rho0 = k[0]  # g/m^3
    a = k[1]
    b = k[2]
    c = k[3]
    gamma0 = k[4]  # no unit
    q = k[5]  # no unit
    theta0_in_kK = k[6]  # K, see Jamieson 1983 for detail
    rho = rho0 / (1. - eta)
    Ph = hugoniot_p_nlin(rho, rho0, a, b, c)  # in [GPa]
    Up = ((b * eta - 1.) + np.sqrt(
        np.power((1. - b * eta), 2.) - 4. * np.power(eta, 2.) * a * c)) / \
        (-2. * eta * c)
    Us = Up * Up * c + Up * b + a
    dUp_delta = Us / (1. - b * eta - 2. * c * eta * Up)
    dUsdUp = b + 2. * c * Up
    dPhdelta_H = rho0 * Us * dUp_delta + rho0 * Up * dUsdUp * dUp_delta

    # calculate Cv
    gamma = gamma0 * np.power((1. - eta), q)
    theta_in_kK = theta0_in_kK * np.exp((gamma0 - gamma) / q)
    x = theta_in_kK / T_in_kK
    debye3 = debye_E(x)
    if c_v == 0.:
        c_v = threenk * (4. * debye3 - 3. * x / (np.exp(x) - 1.))  # [J/g/K]
    # calculate dYdX
    dYdX = (gamma / (1. - eta) * T_in_kK) + (dPhdelta_H * eta - Ph) / \
        (2. * c_v * rho0)
    return dYdX

def hugoniot_t_nlin_single(rho, rho0, a, b, c, gamma0, q, theta0, n, mass,
                           three_r=3. * constants.R, t_ref=300., c_v=0.):
    eta = 1. - rho0 / rho
    if abs(eta) <= 1.e-3:
        return 300.
    threenk = three_r / mass * n  # [J/mol/K] / [g/mol] = [J/g/K]
    k = [rho0, a, b, c, gamma0, q, theta0 / 1.e3]
    t_h = odeint(_dT_h_delta_nlin, t_ref / 1.e3, [0., eta],
                 args=(k, threenk, c_v), full_output=1)
    temp_h = np.squeeze(t_h[0][1])
    return temp_h * 1.e3

def hugoniot_t_nlin(rho, rho0, a, b, c, gamma0, q, theta0, n, mass,
                    three_r=3. * constants.R, t_ref=300., c_v=0.):
    if isuncertainties([rho, rho0, a, b, c, gamma0, q, theta0]):
        print('uncertainties case')
        f_v = np.vectorize(uct.wrap(hugoniot_t_nlin_single),
                           excluded=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
    else:
        f_v = np.vectorize(hugoniot_t_nlin_single, excluded=[1, 2, 3, 4, 5, 6,
                                                             7, 8, 9, 10, 11,
                                                             12])
    return f_v(rho, rho0, a, b, c, gamma0, q, theta0, n, mass,
               three_r=three_r, t_ref=t_ref, c_v=c_v)
#    return np.squeeze(T_h[0]) * 1.e3
"""

max_stars_repo_name: SHDShim/pytheos

max_issues_repo_name: SHDShim/pytheos

max_forks_repo_name: SHDShim/pytheos

reason: import numpy,import scipy,from scipy


text:
from flask import Flask, request, jsonify, render_template
import numpy as np
import pandas as pd
import requests

# Initialize Flask app
def create_app():
    """create and configures an instance of a flask app"""
    app = Flask(__name__)

    @app.route('/')
    def root():
        return "App Hollow Home"

    # @app.route('/data', methods=['POST', 'GET'])
    # def responses():
    #     response = requests.get(
    #         'https://raw.githubusercontent.com/labs15-best-places/backend/master/data-seeding/1-cities/data.js')
    #     return str(response.text)

    return app

# if __name__ == "__main__":
#     app.run(debug=True)


text:
import argparse
from cloudvolume import CloudVolume, Storage
from cloudvolume.lib import mkdir, touch, Vec
# from pathos.multiprocessing import ProcessingPool as Pool
import h5py
import numpy as np
import os

def ingest(args):
    """
    Ingest an HDF file to a CloudVolume bucket
    """
    if args.local_hdf_path:
        hdf_file = h5py.File(args.local_hdf_path, "r")
    else:
        with Storage(args.cloud_src_path) as storage:
            hdf_file = h5py.File(storage.get_file(args.cloud_hdf_filename), "r")
    cur_hdf_group = hdf_file
    for group_name in args.hdf_keys_to_dataset:
        cur_hdf_group = cur_hdf_group[group_name]
    hdf_dataset = cur_hdf_group
    if args.zyx:
        dataset_shape = np.array(
            [hdf_dataset.shape[2], hdf_dataset.shape[1], hdf_dataset.shape[0]]
        )
    else:
        dataset_shape = np.array([*hdf_dataset.shape])
    if args.layer_type == "image":
        data_type = "uint8"
    else:
        data_type = "uint64"
    voxel_offset = args.voxel_offset
    info = CloudVolume.create_new_info(
        num_channels=1,
        layer_type=args.layer_type,
        data_type=data_type,
        encoding="raw",
        resolution=args.resolution,
        voxel_offset=voxel_offset,
        chunk_size=args.chunk_size,
        volume_size=dataset_shape,
    )
    provenance = {
        "description": args.provenance_description,
        "owners": [args.owner]
    }
    vol = CloudVolume(args.dst_path, info=info, provenance=provenance)
    vol.commit_info()
    vol.commit_provenance()

    all_files = set()
    for x in np.arange(
        voxel_offset[0], voxel_offset[0] + dataset_shape[0], args.chunk_size[0]
    ):
        for y in np.arange(
            voxel_offset[1], voxel_offset[1] + dataset_shape[1], args.chunk_size[1]
        ):
            for z in np.arange(
                voxel_offset[2], voxel_offset[2] + dataset_shape[2], args.chunk_size[2]
            ):
                all_files.add(tuple((x, y, z)))

    progress_dir = mkdir("progress/")  # unlike os.mkdir doesn't crash on prexisting
    done_files = set()
    for done_file in os.listdir(progress_dir):
        done_files.add(tuple(done_file.split(",")))
    to_upload = all_files.difference(done_files)

    for chunk_start_tuple in to_upload:
        chunk_start = np.array(list(chunk_start_tuple))
        end_of_dataset = np.array(voxel_offset) + dataset_shape
        chunk_end = chunk_start + np.array(args.chunk_size)
        chunk_end = Vec(*chunk_end)
        chunk_end = Vec.clamp(chunk_end, Vec(0, 0, 0), end_of_dataset)
        chunk_hdf_start = chunk_start - voxel_offset
        chunk_hdf_end = chunk_end - voxel_offset
        if args.zyx:
            chunk = hdf_dataset[
                chunk_hdf_start[2] : chunk_hdf_end[2],
                chunk_hdf_start[1] : chunk_hdf_end[1],
                chunk_hdf_start[0] : chunk_hdf_end[0],
            ]
            chunk = chunk.T
        else:
            chunk = hdf_dataset[
                chunk_hdf_start[0] : chunk_hdf_end[0],
                chunk_hdf_start[1] : chunk_hdf_end[1],
                chunk_hdf_start[2] : chunk_hdf_end[2],
            ]
        print("Processing ", chunk_start_tuple)
        array = np.array(chunk, dtype=np.dtype(data_type), order="F")
        vol[
            chunk_start[0] : chunk_end[0],
            chunk_start[1] : chunk_end[1],
            chunk_start[2] : chunk_end[2],
        ] = array
        touch(os.path.join(progress_dir, str(chunk_start_tuple)))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--local_hdf_path", type=str, help="Local path to hdf file", required=False
    )
    # Either supply a local hdf path, OR supply a cloud hdf path and
    # a cloud hdf filename
    parser.add_argument(
        "--cloud_src_path",
        type=str,
        help="CloudVolume path to the bucket containing the hdf file",
        required=False,
    )
    parser.add_argument(
        "--cloud_hdf_filename",
        type=str,
        help="HDF filename on the cloud",
        required=False,
    )
    parser.add_argument(
        "--hdf_keys_to_dataset",
        type=str,
        nargs="+",
        help="Sequence of keys to retrieve the dataset from the hdf file. E.g. if hdf_file['volumes']['raw'] contains the image data, then you should pass 'volumes raw' (without quotes) as the argument",
    )
    parser.add_argument(
        "--zyx",
        action="store_true",
        default=False,
        help="Pass this argument if the data is stored in zyx order.",
    )
    parser.add_argument(
        "--dst_path", type=str, help="CloudVolume path to store the segmentation"
    )
    parser.add_argument("--layer_type", type=str, help="image or segmentation")
    parser.add_argument(
        "--chunk_size",
        type=int,
        nargs=3,
        help="cloudvolume chunk size. int list of length 3 space delimited (e.g. 128 128 256)",
    )
    parser.add_argument(
        "--resolution",
        type=int,
        nargs=3,
        help="voxel dimensions (nm). int list of length 3 space delimited (e.g. 4 4 40)",
    )
    parser.add_argument(
        "--voxel_offset",
        type=int,
        nargs=3,
        default=np.array([0,0,0])
    )
    parser.add_argument("--provenance_description", type=str, required=False)
    parser.add_argument(
        "--parallel",
        type=int,
        default=1,
        help="int number of processes to use for parallel ops",
    )
    parser.add_argument(
        "--owner", type=str, help="email address for cloudvolume provenance"
    )
    args = parser.parse_args()
    if args.layer_type != "image" and args.layer_type != "segmentation":
        raise ValueError("Invalid layer type (must be image or segmentation)")
    if not args.local_hdf_path and not (
        args.cloud_src_path and args.cloud_hdf_filename
    ):
        raise ValueError(
            "Must specify either a local_hdf_path or both a cloud path and cloud filename"
        )
    ingest(args)

# Example
# python hdf5_ingest.py --local_hdf_path groundtruth.hdf
# --hdf_keys_to_dataset volumes labels neuron_ids
# --dst_path gs://example/cloud/path/
# --layer_type segmentation --chunk_size 512 512 32 --resolution 16 16 40 --owner johndoe@gmail.com

max_stars_repo_name: manuel-castro/reco-suave

max_issues_repo_name: manuel-castro/reco-suave

max_forks_repo_name: manuel-castro/reco-suave


text:
import os, sys
import numpy as np
import shutil
from random import shuffle
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.metrics import confusion_matrix

import torch
from torch.multiprocessing import Pool
from torch.autograd import Variable
from torch.optim import lr_scheduler

class LambdaLR():
    def __init__(self, n_epochs, offset, decay_start_epoch):
        assert ((n_epochs - decay_start_epoch) > 0), "Decay must start before the training session ends!"
        self.n_epochs = n_epochs
        self.offset = offset
        self.decay_start_epoch = decay_start_epoch

    def step(self, epoch):
        return 1.0 - max(0, epoch + self.offset - self.decay_start_epoch)/(self.n_epochs - self.decay_start_epoch)

def train_cls(dataloader, val_dataloader, model_root, net, args):
    net.train()

    start_epoch = 1
    optimizer = torch.optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum,
                                nesterov=True, weight_decay=args.weight_decay)
    lr_scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,
        lr_lambda=LambdaLR(args.maxepoch, start_epoch, args.decay_epoch).step)
    train_loss, step_cnt, batch_count = 0.0, 0, 0
    best_acc = 0.50
    for epoc_num in np.arange(start_epoch, args.maxepoch+1):
        for batch_idx, (batch_data, gt_classes, true_num, bboxes) in enumerate(dataloader):
            im_data   = batch_data.cuda().float()
            im_label  = gt_classes.cuda().long()
            num_data  = true_num.cuda().long()

            im_label = im_label.view(-1, 1)
            train_pred, assignments = net(im_data, im_label, true_num=num_data)

            vecloss = net.loss
            loss = torch.mean(vecloss)
            n_data = im_data.size()[0]
            num_sample  = im_data.size()[0]
            train_loss_val = loss.data.cpu().item()
            train_loss  += train_loss_val
            # backward
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            step_cnt += 1
            batch_count += 1

            train_loss /= step_cnt
        print((' epoch {}, loss: {}, learning rate: {:.5f}'. \
                format(epoc_num, train_loss, optimizer.param_groups[0]['lr'])))

        net.eval()
        total_pred, total_gt = [], []
        for val_data, val_label, val_num, val_boxes in val_dataloader:
            val_data  =  val_data.cuda().float()
            val_num   =  val_num.cuda().long()
            val_pred_pro, assignments = net(val_data, true_num = val_num)
            val_pred_pro = val_pred_pro.cpu()
            _, cls_labels  = torch.topk(val_pred_pro, 1, dim=1)
            cls_labels    =  cls_labels.data.cpu().numpy()[:,0]

            total_pred.extend(cls_labels.tolist())
            total_gt.extend(val_label.tolist())
        precision, recall, fscore, support = score(total_gt, total_pred)
        con_mat = confusion_matrix(total_gt, total_pred)
        # print(' p:  {}\n r:  {}\n f1: {} \n'.format(precision, recall, fscore))
        # print('confusion matrix:')
        # print(con_mat)
        cls_acc = np.trace(con_mat) * 1.0 / np.sum(con_mat)
        print("\n Current classification accuracy is: {:.4f}".format(cls_acc))
        train_loss, step_cnt = 0, 0
        net.train()

        lr_scheduler.step()
        if epoc_num % args.save_freq == 0 and cls_acc >= best_acc and epoc_num >= args.maxepoch - 10:
            save_model_name = 'epoch-{}-acc-{:.3f}.pth'.format(str(epoc_num).zfill(3), cls_acc)
            torch.save(net.state_dict(), os.path.join(model_root, save_model_name))
            print('Model saved as {}'.format(save_model_name))
            best_acc = cls_acc

max_stars_repo_name: PingjunChen/ThyroidGeneralWSI

max_issues_repo_name: PingjunChen/frozen-thyroid-cls

max_forks_repo_name: PingjunChen/frozen-thyroid-cls


text:
#!/usr/bin/python
#coding = utf-8
import numpy as np
from RiskQuantLib.Set.Security.Derivative.Future.future import setFuture

class setBondFuture(setFuture):
    def __nullFunction__(self):
        pass

    # build module, contents below will be automatically built and replaced, self-defined functions shouldn't be written here
    #-<Begin>
    #-<End>

max_stars_repo_name: SyuyaMurakami/RiskQuantLib-Doc

max_issues_repo_name: SyuyaMurakami/RiskQuantLib-Doc

max_forks_repo_name: SyuyaMurakami/RiskQuantLib-Doc


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : JaeZheng
# @Time    : 2019/11/2 17:51
# @File    : load_patch.py

import numpy as np
import cv2
import copy
from scipy.special import comb
import random
import matplotlib.pyplot as plt
from keras.models import *
# from Genesis_Thyroid_US import generate_pair

def bernstein_poly(i, n, t):
    """
     The Bernstein polynomial of n, i as a function of t
    """

    return comb(n, i) * ( t**(n-i) ) * (1 - t)**i

def bezier_curve(points, nTimes=1000):
    """
       Given a set of control points, return the
       bezier curve defined by the control points.

       Control points should be a list of lists, or list of tuples
       such as [ [1,1],
                 [2,3],
                 [4,5], ..[Xn, Yn] ]
        nTimes is the number of time steps, defaults to 1000

        See http://processingjs.nihongoresources.com/bezierinfo/
    """

    nPoints = len(points)
    xPoints = np.array([p[0] for p in points])
    yPoints = np.array([p[1] for p in points])

    t = np.linspace(0.0, 1.0, nTimes)

    polynomial_array = np.array([bernstein_poly(i, nPoints - 1, t) for i in range(0, nPoints)])

    xvals = np.dot(xPoints, polynomial_array)
    yvals = np.dot(yPoints, polynomial_array)

    return xvals, yvals

def data_augmentation(x, y, prob=0.5):
    # augmentation by flipping
    cnt = 3
    while random.random() < prob and cnt > 0:
        degree = random.choice([0, 1, 2])
        x = np.flip(x, axis=degree)
        y = np.flip(y, axis=degree)
        cnt = cnt - 1

    return x, y

def nonlinear_transformation(x, prob=0.5):
    if random.random() >= prob:
        return x
    points = [[0, 0], [random.random(), random.random()], [random.random(), random.random()], [1, 1]]
    xpoints = [p[0] for p in points]
    ypoints = [p[1] for p in points]
    xvals, yvals = bezier_curve(points, nTimes=100000)
    if random.random() < 0.5:
        # Half change to get flip
        xvals = np.sort(xvals)
    else:
        xvals, yvals = np.sort(xvals), np.sort(yvals)
    nonlinear_x = np.interp(x, xvals, yvals)
    return nonlinear_x

def local_pixel_shuffling(x, prob=0.5):
    if random.random() >= prob:
        return x
    image_temp = copy.deepcopy(x)
    orig_image = copy.deepcopy(x)
    img_rows, img_cols = x.shape
    num_block = 500
    for _ in range(num_block):
        block_noise_size_x = random.randint(1, img_rows//10)
        block_noise_size_y = random.randint(1, img_cols//10)
        noise_x = random.randint(0, img_rows-block_noise_size_x)
        noise_y = random.randint(0, img_cols-block_noise_size_y)
        window = orig_image[noise_x:noise_x+block_noise_size_x,
                            noise_y:noise_y+block_noise_size_y]
        window = window.flatten()
        np.random.shuffle(window)
        window = window.reshape((block_noise_size_x,
                                 block_noise_size_y))
        image_temp[noise_x:noise_x+block_noise_size_x,
                   noise_y:noise_y+block_noise_size_y] = window
    local_shuffling_x = image_temp

    return local_shuffling_x

def image_in_painting(x, prob=0.9):
    in_painting_x = copy.deepcopy(x)
    img_rows, img_cols = x.shape
    num_painting = 5
    for _ in range(num_painting):
        if random.random() >= prob:
            continue
        block_noise_size_x = random.randint(10, 20)
        block_noise_size_y = random.randint(10, 20)
        noise_x = random.randint(3, img_rows-block_noise_size_x-3)
        noise_y = random.randint(3, img_cols-block_noise_size_y-3)
        in_painting_x[noise_x:noise_x+block_noise_size_x,
          noise_y:noise_y+block_noise_size_y] = random.random()
    return in_painting_x

def image_out_painting(x):
    out_painting_x = copy.deepcopy(x)
    out_painting_x[:, :] = random.random()
    img_rows, img_cols = x.shape
    block_noise_size_x = img_rows - random.randint(20, 30)
    block_noise_size_y = img_cols - random.randint(20, 30)
    noise_x = random.randint(3, img_rows-block_noise_size_x-3)
    noise_y = random.randint(3, img_cols-block_noise_size_y-3)
    image_temp = copy.deepcopy(x)
    out_painting_x[noise_x:noise_x+block_noise_size_x,
                   noise_y:noise_y+block_noise_size_y] = image_temp[noise_x:noise_x+block_noise_size_x,
                                                                        noise_y:noise_y+block_noise_size_y]
    return out_painting_x

def load_and_write_patch():
    npy = np.load('datasets/thyroid/generated_patch/bat_32_64x64_2.npy')
    print(npy.shape)

    count = 0
    for patch in npy:
        # cv2.imwrite('datasets/tmp/'+str(count)+'.jpg', patch)
        plt.imsave('datasets/tmp/'+str(count)+'.jpg', patch, cmap='gray')
        count += 1

def transform_demo():
    npy = np.load('datasets/thyroid/generated_patch/bat_32_64x64_2.npy')
    print(npy.shape)
    # 原图
    example = npy[99]
    # 非线性变换
    non = nonlinear_transformation(example)
    # 局部像素打乱
    local_shuff = local_pixel_shuffling(example)
    # 向内补丁
    in_painting = image_in_painting(example)
    # 向外补丁
    out_painting = image_out_painting(example)
    print(np.mean(example))
    print(np.mean(non))
    print(np.mean(local_shuff))
    print(np.mean(in_painting))
    print(np.mean(out_painting))
    plt.figure(figsize=(10, 5))  # 设置窗口大小
    plt.suptitle('Multi_Image')  # 图片名称
    plt.subplot(2, 3, 1), plt.title('original')
    plt.imshow(example, cmap='gray'), plt.axis('off')
    plt.subplot(2, 3, 2), plt.title('nonlinear')
    plt.imshow(non, cmap='gray'), plt.axis('off')
    plt.subplot(2, 3, 3), plt.title('local_shuff')
    plt.imshow(local_shuff, cmap='gray'), plt.axis('off')
    plt.subplot(2, 3, 4), plt.title('in_painting')
    plt.imshow(in_painting, cmap='gray'), plt.axis('off')
    plt.subplot(2, 3, 5), plt.title('out_painting')
    plt.imshow(out_painting, cmap='gray'), plt.axis('off')
    plt.show()

def visualize_model_genesis():
    # model = load_model('pretrained_weights/Vnet-genesis_thyroid_us.h5')
    npy = np.load('datasets/thyroid/generated_patch/bat_32_128x128_2.npy')
    # 原图
    example = npy[99]
    print(example.shape)
    # 非线性变换
    non = nonlinear_transformation(example)
    # 局部像素打乱
    local_shuff = local_pixel_shuffling(example)
    # 向内补丁
    in_painting = image_in_painting(example)
    # 向外补丁
    out_painting = image_out_painting(example)
    # 还原
    # tmp = copy.deepcopy(non)
    # tmp = np.expand_dims(tmp, axis=0)
    # tmp = np.expand_dims(tmp, axis=-1)
    # result_non = model.predict(tmp)
    # tmp = copy.deepcopy(local_shuff)
    # tmp = np.expand_dims(tmp, axis=0)
    # tmp = np.expand_dims(tmp, axis=-1)
    # result_local_shuff = model.predict(tmp)
    # tmp = copy.deepcopy(in_painting)
    # tmp = np.expand_dims(tmp, axis=0)
    # tmp = np.expand_dims(tmp, axis=-1)
    # result_in_painting = model.predict(tmp)
    # tmp = copy.deepcopy(out_painting)
    # tmp = np.expand_dims(tmp, axis=0)
    # tmp = np.expand_dims(tmp, axis=-1)
    # result_out_painting = model.predict(tmp)
    print(np.mean(example))
    print(np.mean(non))
    print(np.mean(local_shuff))
    print(np.mean(in_painting))
    print(np.mean(out_painting))
    # print(np.mean(result_non))
    # print(np.mean(result_local_shuff))
    # print(np.mean(result_in_painting))
    # print(np.mean(result_out_painting))
    plt.figure(figsize=(10, 5))  # 设置窗口大小
    plt.suptitle('Multi_Image')  # 图片名称
    plt.subplot(3, 3, 1), plt.title('original')
    plt.imshow(example, cmap='gray'), plt.axis('off')
    plt.subplot(3, 3, 2), plt.title('nonlinear')
    plt.imshow(non, cmap='gray'), plt.axis('off')
    plt.subplot(3, 3, 3), plt.title('local_shuff')
    plt.imshow(local_shuff, cmap='gray'), plt.axis('off')
    plt.subplot(3, 3, 4), plt.title('in_painting')
    plt.imshow(in_painting, cmap='gray'), plt.axis('off')
    plt.subplot(3, 3, 5), plt.title('out_painting')
    plt.imshow(out_painting, cmap='gray'), plt.axis('off')
    # plt.subplot(3, 3, 6), plt.title('result_non')
    # plt.imshow(result_non[0,:,:,0], cmap='gray'), plt.axis('off')
    # plt.subplot(3, 3, 7), plt.title('result_local_shuff')
    # plt.imshow(result_local_shuff[0, :, :, 0], cmap='gray'), plt.axis('off')
    # plt.subplot(3, 3, 8), plt.title('result_in_painting')
    # plt.imshow(result_in_painting[0, :, :, 0], cmap='gray'), plt.axis('off')
    # plt.subplot(3, 3, 9), plt.title('result_out_painting')
    # plt.imshow(result_out_painting[0, :, :, 0], cmap='gray'), plt.axis('off')
    plt.show()

if __name__ == '__main__':
    # load_and_write_patch()
    # transform_demo()
    visualize_model_genesis()

max_stars_repo_name: JaeZheng/ModelsGenesis

max_issues_repo_name: JaeZheng/ModelsGenesis

max_forks_repo_name: JaeZheng/ModelsGenesis