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

# In[1]:

from os import path
import keras
import matplotlib.pyplot as plt
import cv2
import numpy as np
import pandas as pd

# In[2]:

import os
import skimage.io as io
import skimage.transform as trans
import numpy as np
import tensorflow as tf
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as keras
from keras import backend as K
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# In[3]:

skip_ids=[21, 25, 37, 38, 40, 59, 60, 72, 92, 98,101, 118, 120, 128, 130, 182, 206, 210, 226, 245, 271]

# In[5]:

trainDamagedAP_path = '/kaggle/input/spinedataset/Training/Damaged/ID ({})/AP/AP.jpg'
trainDamagedAP=[]
# shape should be divisible by 16 to make downsampling and upsampling consistent in U-Net
for i in range(1,329):
    imagePath=trainDamagedAP_path.format(i)
    if path.exists(imagePath):
        trainDamagedAP.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/spinedataset/Training/Damaged/ID ({})/AP/ap.jpg'.format(i)):
            trainDamagedAP.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Damaged/ID ({})/AP/ap.jpg'.format(i),0),(224,224)))
        else:
            trainDamagedAP.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Damaged/ID ({})/AP.jpg'.format(i),0),(224,224)))
trainDamagedAP=np.array(trainDamagedAP)

# In[6]:

trainNormalAP_path = '/kaggle/input/spinedataset/Training/Normal/ID ({})/AP/AP.jpg'
trainNormalAP=[]
for i in range(1,351):
    imagePath=trainNormalAP_path.format(i)
    if path.exists(imagePath):
        trainNormalAP.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/spinedataset/Training/Normal/ID ({})/AP/ap.jpg'.format(i)):
            trainNormalAP.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Normal/ID ({})/AP/ap.jpg'.format(i),0),(224,224)))
        else:
            trainNormalAP.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Normal/ID ({})/AP.jpg'.format(i),0),(224,224)))
trainNormalAP=np.array(trainNormalAP)

# In[7]:

trainAP=np.concatenate((trainNormalAP,trainDamagedAP))
del trainNormalAP
del trainDamagedAP
trainAP = np.expand_dims(trainAP, axis=3)

# In[8]:

testAP_path = '/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/AP/AP.jpg'
testAP=[]
for i in range(1,302):
    if i in skip_ids:
        continue
    imagePath=testAP_path.format(i)
    if path.exists(imagePath):
        testAP.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/AP/ap.jpg'.format(i)):
            testAP.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/AP/ap.jpg'.format(i),0),(224,224)))
        else:
            if(i==287):
                testAP.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test (287)/AP/SERIES_001_IMG_0000.jpg',0),(224,224)))
            else:
                testAP.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/AP.jpg'.format(i),0),(224,224)))
testAP=np.array(testAP)
testAP = np.expand_dims(testAP, axis=3)

# In[9]:

smooth = 1.

def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)

def dice_coef_loss(y_true, y_pred):
    return -dice_coef(y_true, y_pred)

# In[10]:

def jaccard_distance_loss(y_true, y_pred, smooth=1):
    """
    Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|)
            = sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|))
    """
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    sum_ = (K.sum(y_true_f) + K.sum(y_pred_f))
    jac = (intersection*intersection*intersection + smooth) / (sum_ - intersection + smooth)
    return (1 - jac) * smooth

# In[11]:

from keras.models import Model, Input, load_model
from keras.layers import Input
from keras.layers.core import Dropout, Lambda
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import MaxPooling2D
from keras.layers.merge import concatenate
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint

# In[12]:

def dice_score(y_true, y_pred):
    m = y_true.shape[0]
    total=0
    for i in range(m):
        true=y_true[i,:,:].flatten()
        pred=y_pred[i,:,:].flatten()
        intersection = np.sum(np.multiply(true, pred))
        sum_ = np.sum(true)+np.sum(pred)
        total+= (2*intersection+1)/(sum_+1)
    return total/m

# In[67]:

def save_result(y_pred,name, Results):
#     print(y_pred.shape)
    t=0
    for i in range(y_pred.shape[0]):
        if (i+1+t) in skip_ids:
            t=t+1
        if name=='Ap_Pedicle':
            Results[i+1+t]={}
            Results[i+1+t][name]=np.array(y_pred[i,:,:,0],dtype=bool)
        else:
            Results[i+1+t][name]=np.array(y_pred[i,:,:,0],dtype=bool)
    return Results

# In[60]:

History=[]
Dice_Scores=[]
Results={}
def train_result(x_train,y_train,x_test,y_test,name, Results):
    inputs = Input((224, 224, 1))

    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
    conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
    conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2)
    conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3)
    conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4)
    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)

    up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
    conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up6)
    conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6)

    up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
    conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up7)
    conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7)

    up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
    conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(up8)
    conv8 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv8)

    up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
    conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(up9)
    conv9 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv9)

    conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)

    model = Model(inputs=[inputs], outputs=[conv10])
    model.compile(optimizer=Adam(lr = 1e-5), loss=jaccard_distance_loss, metrics=[dice_coef])
    history = model.fit(x_train,y_train,batch_size=8,validation_data=(x_test,y_test),shuffle=True,epochs=140)
    History.append(history)
    y_pred=model.predict(x_test)
    print(y_pred.shape)
    score=dice_score(y_test,y_pred)
    print("dice score: ",score)
    Dice_Scores.append((name,score))
    for i in range(y_pred.shape[0]):
        y_pred[i,:,:,:][y_pred[i,:,:,:]>0.5] = 1
        y_pred[i,:,:,:][y_pred[i,:,:,:]<=0.5] = 0
    Results = save_result(y_pred,name,Results)
    return Results

# In[20]:

def load_dataAP(name):
    trainNormal_path = '/kaggle/input/spinedataset/Training/Normal/ID ({})/AP/{}.png'
    trainNormal=[]
    for i in range(1,351):
        imagePath=trainNormal_path.format(i,name)
        trainNormal.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    trainNormal=np.array(trainNormal)
    trainNormal = np.expand_dims(trainNormal, axis=3)

    trainDamaged_path = '/kaggle/input/spinedataset/Training/Damaged/ID ({})/AP/{}.png'
    trainDamaged=[]
    for i in range(1,329):
        imagePath=trainDamaged_path.format(i,name)
        trainDamaged.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    trainDamaged=np.array(trainDamaged)
    trainDamaged = np.expand_dims(trainDamaged, axis=3)
    Y_train=np.concatenate((trainNormal,trainDamaged))
    del trainNormal
    del trainDamaged
    Y_test_path = '/kaggle/input/testmasks/TestResizedMasks/Test ({})/AP/{}.png'
    Y_test=[]
    for i in range(1,302):
        if i in skip_ids:
            continue
        imagePath=Y_test_path.format(i,name)
        Y_test.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    Y_test=np.array(Y_test)
    Y_test = np.expand_dims(Y_test, axis=3)
    for i in range(Y_train.shape[0]):
        Y_train[i,:,:,:]=Y_train[i,:,:,:]/255
        Y_train[i,:,:,:][Y_train[i,:,:,:]>0.5] = 1
        Y_train[i,:,:,:][Y_train[i,:,:,:]<=0.5] = 0
    for i in range(Y_test.shape[0]):
        Y_test[i,:,:,:]=Y_test[i,:,:,:]/255
        Y_test[i,:,:,:][Y_test[i,:,:,:]>0.5] = 1
        Y_test[i,:,:,:][Y_test[i,:,:,:]<=0.5] = 0
    return Y_train, Y_test

# In[21]:

def load_dataLAT(name):
    trainNormal_path = '/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/{}.png'
    trainNormal=[]
    for i in range(1,351):
        imagePath=trainNormal_path.format(i,name)
        if path.exists(imagePath):
            trainNormal.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
        else:
            imagePath='/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/{}.png'.format(i,'LAT_Spinous_Process')
            trainNormal.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))

    trainNormal=np.array(trainNormal)
    trainNormal = np.expand_dims(trainNormal, axis=3)

    trainDamaged_path = '/kaggle/input/spinedataset/Training/Damaged/ID ({})/LAT/{}.png'
    trainDamaged=[]
    for i in range(1,329):
        imagePath=trainDamaged_path.format(i,name)
        if(i==233):
            if(name=='Lat_Posterior_Vertebral_Line'):
                imagePath=trainDamaged_path.format(i-1,name)
                trainDamaged.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
                continue
        if(i==169):
            if(name=='Lat_Spinous_Process'):
                imagePath=trainDamaged_path.format(i-1,name)
                trainDamaged.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
                continue
        trainDamaged.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    trainDamaged=np.array(trainDamaged)
    trainDamaged = np.expand_dims(trainDamaged, axis=3)
    Y_train=np.concatenate((trainNormal,trainDamaged))
    del trainNormal
    del trainDamaged
    Y_test_path = '/kaggle/input/testmasks/TestResizedMasks/Test ({})/LAT/{}.png'
    Y_test=[]
    for i in range(1,302):
        if i in skip_ids:
            continue
        imagePath=Y_test_path.format(i,name)
        Y_test.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    Y_test=np.array(Y_test)
    Y_test = np.expand_dims(Y_test, axis=3)
    for i in range(Y_train.shape[0]):
        Y_train[i,:,:,:]=Y_train[i,:,:,:]/255
        Y_train[i,:,:,:][Y_train[i,:,:,:]>0.5] = 1
        Y_train[i,:,:,:][Y_train[i,:,:,:]<=0.5] = 0
    for i in range(Y_test.shape[0]):
        Y_test[i,:,:,:]=Y_test[i,:,:,:]/255
        Y_test[i,:,:,:][Y_test[i,:,:,:]>0.5] = 1
        Y_test[i,:,:,:][Y_test[i,:,:,:]<=0.5] = 0
    return Y_train, Y_test

# In[68]:

# namesAP=['Ap_Pedicle','Ap_Spinous_Process','Ap_Vertebra']
namesAP=['Ap_Pedicle']

namesLAT=['Lat_Anterior_Vertebral_Line','Lat_Disk_Height','Lat_Posterior_Vertebral_Line','Lat_Spinous_Process','Lat_Vertebra']
namesLAT=['Lat_Anterior_Vertebral_Line','Lat_Disk_Height','Lat_Posterior_Vertebral_Line','Lat_Spinous_Process','Lat_Vertebra']

# In[23]:

for name in namesAP:
    print(name)
    y_train, y_test = load_dataAP(name)
    Results=train_result(trainAP,y_train,testAP,y_test,name, Results)
    del y_train
    del y_test
del trainAP
del testAP

# In[62]:

trainDamagedLAT_path = '/kaggle/input/spinedataset/Training/Damaged/ID ({})/LAT/LAT.jpg'
trainDamagedLAT=[]
# shLATe should be divisible by 16 to make downsampling and upsampling consistent in U-Net
for i in range(1,329):
    imagePath=trainDamagedLAT_path.format(i)
    if path.exists(imagePath):
        trainDamagedLAT.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/spinedataset/Training/Damaged/ID ({})/LAT/lat.jpg'.format(i)):
            trainDamagedLAT.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Damaged/ID ({})/LAT/lat.jpg'.format(i),0),(224,224)))
        else:
            trainDamagedLAT.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Damaged/ID ({})/LAT.jpg'.format(i),0),(224,224)))
trainDamagedLAT=np.array(trainDamagedLAT)

trainNormalLAT_path = '/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/LAT.jpg'
trainNormalLAT=[]
for i in range(1,351):
    imagePath=trainNormalLAT_path.format(i)
    if path.exists(imagePath):
        trainNormalLAT.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/lat.jpg'.format(i)):
            trainNormalLAT.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/lat.jpg'.format(i),0),(224,224)))
        elif path.exists('/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT.jpg'.format(i)):
            trainNormalLAT.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT.jpg'.format(i),0),(224,224)))
        else:
            trainNormalLAT.append(cv2.resize(cv2.imread('/kaggle/input/spinedataset/Training/Normal/ID ({})/LAT/LAT .jpg'.format(i),0),(224,224)))
trainNormalLAT=np.array(trainNormalLAT)

trainLAT=np.concatenate((trainNormalLAT,trainDamagedLAT))
del trainNormalLAT
del trainDamagedLAT
trainLAT = np.expand_dims(trainLAT, axis=3)

# In[57]:

testLAT_path = '/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/LAT/LAT.jpg'
testLAT=[]
for i in range(1,302):
    if (i) in skip_ids:
        continue
    imagePath=testLAT_path.format(i)
    if path.exists(imagePath):
        testLAT.append(cv2.resize(cv2.imread(imagePath,0),(224,224)))
    else:
        if path.exists('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/LAT/lat.jpg'.format(i)):
            testLAT.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/LAT/lat.jpg'.format(i),0),(224,224)))
        else:
            if(i==287):
                testLAT.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test (287)/LAT/SERIES_001_IMG_0001.jpg',0),(224,224)))
            else:
                testLAT.append(cv2.resize(cv2.imread('/kaggle/input/testdataxrays/TestData(XraysOnly)/Test ({})/Lat.jpg'.format(i),0),(224,224)))
testLAT=np.array(testLAT)
testLAT = np.expand_dims(testLAT, axis=3)

# In[69]:

for name in namesLAT:
    print(name)
    y_train, y_test = load_dataLAT(name)
    Results=train_result(trainLAT,y_train,testLAT,y_test,name, Results)

    del y_train
    del y_test
del trainLAT
del testLAT

# In[29]:

import pickle
with open('Segmentation.pickle', 'wb') as handle:
    pickle.dump(Results, handle, protocol=pickle.HIGHEST_PROTOCOL)

# In[30]:

i=0
for history in History:
    file_name='history_{}'.format(i)
    with open(filename, 'wb') as file_pi:
            pickle.dump(history.history, file_pi)

# In[31]:

print(Dice_Scores)

#

# In[50]:

history=History[7]
from matplotlib import pyplot as plt
# summarize history for accuracy
plt.plot(history.history['dice_coef'])
plt.plot(history.history['val_dice_coef'])
# plt.title('model accuracy - batch_size:' + str(batch_size))
plt.ylabel('dice_coef')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')

# In[51]:

history=History[7]
from matplotlib import pyplot as plt
# summarize history for accuracy
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
# plt.title('model accuracy - batch_size:' + str(batch_size))
plt.ylabel('jaccard_loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')

max_stars_repo_name: ashish-gpta/Spine-XRay

max_issues_repo_name: ashish-gpta/Spine-XRay

max_forks_repo_name: ashish-gpta/Spine-XRay


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

import numpy as np
import matplotlib.pyplot as plt
import math

x = np.asarray(range(1, 102))
y1 = np.random.randn(101)
y2 = np.linspace(-1, 1, 101)
# sigmoid的导数 y=sigmoid(x)，则y`=y(1-y)
y3 = map(lambda y:y*(1-y), x)
sigmoid = map(lambda x:1/(1+math.e**(-x)), x)

plt.plot(x, y1, 'ro', label='randn')
plt.plot(x, y2, label='linspace')
#plt.plot(x, y3, label='simoid`')
#plt.plot(x, sigmoid, label='sigmoid')
plt.legend()
plt.show()

ext: py

max_stars_repo_name: funkygao/tfshow

max_issues_repo_name: funkygao/tfshow

max_forks_repo_name: funkygao/tfshow

reason: import numpy


text:
import os
import weakref
import platform
from collections import OrderedDict

import numpy as np
from matplotlib.colors import ColorConverter

from qtpy import QtWidgets
from glue.core.message import SettingsChangeMessage
from glue.utils.qt import load_ui, ColorProperty, get_qapp
from glue.utils.qt.widget_properties import (CurrentComboTextProperty,
                                             ValueProperty, ButtonProperty)
from glue._settings_helpers import save_settings

__all__ = ["PreferencesDialog"]

rgb = ColorConverter().to_rgb

AUTOLINK_OPTIONS = OrderedDict([
    ('always_show', 'Always show suggestions'),
    ('always_accept', 'Always accept suggestions'),
    ('always_ignore', 'Always ignore suggestions')
])

class AutolinkPreferencesPane(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(AutolinkPreferencesPane, self).__init__(parent=parent)

        from glue.config import settings, autolinker  # noqa

        layout = QtWidgets.QGridLayout()

        self.combos = {}

        if len(autolinker) > 0:

            for i, (label, _) in enumerate(autolinker):
                combo = QtWidgets.QComboBox()
                for short, display in AUTOLINK_OPTIONS.items():
                    combo.addItem(display, userData=short)
                if label in settings.AUTOLINK:
                    index = list(AUTOLINK_OPTIONS.keys()).index(settings.AUTOLINK[label])
                else:
                    index = 0
                combo.setCurrentIndex(index)
                layout.addWidget(QtWidgets.QLabel(label), i, 0)
                layout.addWidget(combo, i, 1)
                self.combos[label] = combo

            layout.addWidget(QtWidgets.QWidget(), i + 1, 0)

        self.setLayout(layout)

    def finalize(self):
        from glue.config import settings
        for label, combo in self.combos.items():
            settings.AUTOLINK[label] = combo.currentData()

class PreferencesDialog(QtWidgets.QDialog):

    theme = CurrentComboTextProperty('ui.combo_theme')
    background = ColorProperty('ui.color_background')
    foreground = ColorProperty('ui.color_foreground')
    data_color = ColorProperty('ui.color_default_data')
    data_alpha = ValueProperty('ui.slider_alpha', value_range=(0, 1))
    data_apply = ButtonProperty('ui.checkbox_apply')
    save_to_disk = ButtonProperty('ui.checkbox_save')
    font_size = ValueProperty('ui.spinner_font_size')

    def __init__(self, application, parent=None):

        super(PreferencesDialog, self).__init__(parent=parent)

        self._app = weakref.ref(application)

        self.ui = load_ui('preferences.ui', self,
                          directory=os.path.dirname(__file__))

        self.ui.cancel.clicked.connect(self.reject)
        self.ui.ok.clicked.connect(self.accept)

        self.ui.combo_theme.currentIndexChanged.connect(self._update_colors_from_theme)

        self.ui.button_reset_dialogs.clicked.connect(self._reset_dialogs)

        # The following is needed because of a bug in Qt which means that
        # tab titles don't get scaled right.
        if platform.system() == 'Darwin':
            app = get_qapp()
            app_font = app.font()
            self.ui.tab_widget.setStyleSheet('font-size: {0}px'.format(app_font.pointSize()))

        from glue.config import settings
        self.background = settings.BACKGROUND_COLOR
        self.foreground = settings.FOREGROUND_COLOR
        self.data_color = settings.DATA_COLOR
        self.data_alpha = settings.DATA_ALPHA
        self.font_size = settings.FONT_SIZE

        self._update_theme_from_colors()

        self._autolink_pane = AutolinkPreferencesPane()
        self.ui.tab_widget.addTab(self._autolink_pane, 'Autolinking')

        self.panes = []

        from glue.config import preference_panes
        for label, widget_cls in sorted(preference_panes):
            pane = widget_cls()
            self.ui.tab_widget.addTab(pane, label)
            self.panes.append(pane)

    def _update_theme_from_colors(self, *args):
        if (rgb(self.background) == (1, 1, 1) and rgb(self.foreground) == (0, 0, 0) and
                rgb(self.data_color) == (0.35, 0.35, 0.35) and np.allclose(self.data_alpha, 0.8)):
            self.theme = 'Black on White'
        elif (rgb(self.background) == (0, 0, 0) and rgb(self.foreground) == (1, 1, 1) and
                rgb(self.data_color) == (0.75, 0.75, 0.75) and np.allclose(self.data_alpha, 0.8)):
            self.theme = 'White on Black'
        else:
            self.theme = 'Custom'

    def _update_colors_from_theme(self, *args):
        if self.theme == 'Black on White':
            self.foreground = 'black'
            self.background = 'white'
            self.data_color = '0.35'
            self.data_alpha = 0.8
        elif self.theme == 'White on Black':
            self.foreground = 'white'
            self.background = 'black'
            self.data_color = '0.75'
            self.data_alpha = 0.8
        elif self.theme != 'Custom':
            raise ValueError("Unknown theme: {0}".format(self.theme))

    def _reset_dialogs(self, *args):
        from glue.config import settings
        for key, _, _ in settings:
            if key.lower().startswith(('show_info', 'show_warn', 'show_large')):
                setattr(settings, key, True)

    def accept(self):

        # Update default settings

        from glue.config import settings
        settings.FOREGROUND_COLOR = self.foreground
        settings.BACKGROUND_COLOR = self.background
        settings.DATA_COLOR = self.data_color
        settings.DATA_ALPHA = self.data_alpha
        settings.FONT_SIZE = self.font_size

        self._autolink_pane.finalize()

        for pane in self.panes:
            pane.finalize()

        # Save to disk if requested
        if self.save_to_disk:
            save_settings()
        else:
            settings._save_to_disk = True

        # Trigger viewers to update defaults

        app = self._app()

        if app is not None:
            app._hub.broadcast(SettingsChangeMessage(self, ('FOREGROUND_COLOR', 'BACKGROUND_COLOR', 'FONT_SIZE')))
            if self.data_apply:  # If requested, trigger data to update color
                app.set_data_color(settings.DATA_COLOR, settings.DATA_ALPHA)

        super(PreferencesDialog, self).accept()

if __name__ == "__main__":

    app = get_qapp()
    widget = PreferencesDialog()
    widget.show()
    widget.raise_()
    app.exec_()

max_stars_repo_name: HPLegion/glue

max_issues_repo_name: HPLegion/glue

max_forks_repo_name: HPLegion/glue


text:
import os
import sys
import numpy as np
import skimage.io
import random
import cv2

# Root directory of the project
ROOT_DIR = os.path.abspath("/../../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
from mrcnn import model as modellib

# Path to trained weights file
COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "./mask_rcnn_coco.h5")

# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

############################################################
#  Configurations
############################################################

class WBCConfig(Config):
    """Configuration for training on the WBC dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "WBC"

    # Running on CPU
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 5  # Background + 5 types of WBC

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 60
    VALIDATION_STEPS = 2

    # Backbone network architecture
    # Supported values are: resnet50, resnet101
    BACKBONE = 'resnet50'

    # Input image resizing
    # In 'square' resizing mode, images are scaled
    # up such that the small side is = IMAGE_MIN_DIM, but ensuring that the
    # scaling doesn't make the long side > IMAGE_MAX_DIM. Then the image is
    # padded with zeros to make it a square so multiple images can be put
    # in one batch.
    IMAGE_RESIZE_MODE = "square"
    IMAGE_MIN_DIM = 768
    IMAGE_MAX_DIM = 768

    # Minimum probability value to accept a detected instance
    # ROIs below this threshold are skipped
    DETECTION_MIN_CONFIDENCE = 0.8

############################################################
#  Dataset
############################################################

class WBCDataset(utils.Dataset):

    # function to load the images
    def load_dataset(self, dataset_dir, subset):
        # Adding the 5 classes
        self.add_class("WBC", 1, "Basophil")
        self.add_class("WBC", 2, "Eosinophil")
        self.add_class("WBC", 3, "Lymphocyte")
        self.add_class("WBC", 4, "Monocyte")
        self.add_class("WBC", 5, "Neutrophil")

        # Train, validation or test/detect set?
        assert subset in ["train", "val", 'detect']
        self.dataset_dir = os.path.join(dataset_dir, subset)

        if subset == 'detect':
            for test in os.listdir(os.path.join(self.dataset_dir, 'image')):
                img_path = os.path.join(self.dataset_dir, 'image', test)
                self.add_image('WBC', image_id=test, path=img_path)

        else:
            for filename in os.listdir(os.path.join(self.dataset_dir, 'image')):
                # To get the class ids of the images.
                if filename == "Basophil":
                    ids = 1
                if filename == "Eosinophil":
                    ids = 2
                if filename == "Lymphocyte":
                    ids = 3
                if filename == "Monocyte":
                    ids = 4
                if filename == "Neutrophil":
                    ids = 5

                # Loop over all the images of 1 type of WBC
                for img in os.listdir(os.path.join(self.dataset_dir, 'image', filename)):
                    img_path = os.path.join(self.dataset_dir, 'image', filename, img)
                    image = cv2.imread(img_path)
                    height, width = image.shape[:2]

                    # Reading the mask of the image to get the number of instances in the image
                    mask_path = os.path.join(self.dataset_dir, 'mask', filename, img)
                    mask = cv2.imread(mask_path)
                    gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
                    thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)[1]
                    ret, labels = cv2.connectedComponents(thresh)

                    # labels.max() gives the number of instances in the image
                    class_ids=np.zeros([labels.max()]).astype(int)
                    for i in range(labels.max()):
                        class_ids[i]=ids

                    self.add_image('WBC', image_id=img, path=img_path, width=width, height=height,
                                   class_ids=np.array(class_ids))

    # function to load the masks
    def load_mask(self, image_id):
        info = self.image_info[image_id]
        mask_dir = os.path.join(self.dataset_dir, 'mask')

        for filename in os.listdir(mask_dir):
            for mask in os.listdir(os.path.join(mask_dir, filename)):
                mname,png = os.path.splitext(mask)
                iname,jpg = os.path.splitext(info['id'])
                # checking if mask is that of the input image
                if mname == iname:
                    mask_r = cv2.imread(os.path.join(mask_dir,filename, mask), cv2.IMREAD_GRAYSCALE)
                    # dimensions of image and mask are not the same. So we pad the masks with black pixels
                    mask_r = cv2.copyMakeBorder(mask_r,4,4,4,4,cv2.BORDER_CONSTANT,value=[0,0,0])
                    ret, labels = cv2.connectedComponents(mask_r)
                    mask_merge=[]
                    # Creating a boolean mask for each WBC instance in the mask
                    for label in range(1,ret):
                        mask = np.array(labels, dtype=np.uint8)
                        mask[labels == label]=True
                        mask[labels != label]=False
                        mask_merge.append(mask)

                    # Stacking the masks along the z-axis
                    fin_mask = np.dstack(mask_merge)

        return fin_mask, info['class_ids']

    def image_reference(self, image_id):
        info = self.image_info[image_id]
        return info['path']

############################################################
#  Training
############################################################

def train(model):
    # Training dataset.
    dataset_train = WBCDataset()
    dataset_train.load_dataset('path/to/dataset/','train')
    dataset_train.prepare()

    # Validation Dataset
    dataset_val = WBCDataset()
    dataset_val.load_dataset('path/to/dataset/','val')
    dataset_val.prepare()

    print("TRAINING LAYERS...")
    model.train(dataset_train, dataset_val, learning_rate=config.LEARNING_RATE, epochs=75, layers='3+')

def color_splash(image, mask):
    """Apply color splash effect.
    image: RGB image [height, width, 3]
    mask: instance segmentation mask [height, width, instance count]

    Returns result image.
    """
    # Make a grayscale copy of the image. The grayscale copy still
    # has 3 RGB channels, though.
    gray = skimage.color.gray2rgb(skimage.color.rgb2gray(image)) * 255
    # Copy color pixels from the original color image where mask is set
    if mask.shape[-1] > 0:
        # We're treating all instances as one, so collapse the mask into one layer
        mask = (np.sum(mask, -1, keepdims=True) >= 1)
        splash = np.where(mask, image, gray).astype(np.uint8)
    else:
        splash = gray.astype(np.uint8)
    return splash

############################################################
#  Command Line
############################################################

if __name__ == '__main__':
    import argparse
    # Parse command line arguments
    parser = argparse.ArgumentParser(
        description='Train Mask R-CNN to detect custom class.')
    parser.add_argument("command",
                        metavar="<command>",
                        help="'train' or 'mask'")
    parser.add_argument('--dataset', required=False,
                        metavar="/path/to/custom/dataset/",
                        help='Directory of the custom dataset')
    parser.add_argument('--weights', required=True,
                        metavar="/path/to/weights.h5",
                        help="Path to weights .h5 file or 'coco'")
    parser.add_argument('--logs', required=False,
                        default=DEFAULT_LOGS_DIR,
                        metavar="/path/to/logs/",
                        help='Logs and checkpoints directory (default=logs/)')
    parser.add_argument('--image', required=False,
                        metavar="path or URL to image",
                        help='Image to apply the color mask effect on')
    args = parser.parse_args()

    # Validate arguments
    if args.command == "train":
        assert args.dataset, "Argument --dataset is required for training"

    print("Weights: ", args.weights)
    print("Dataset: ", args.dataset)
    print("Logs: ", args.logs)

    # Configurations
    if args.command == "train":
        config = WBCConfig()
    else:
        class InferenceConfig(WBCConfig):
            # Set batch size to 1 since we'll be running inference on
            # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
            GPU_COUNT = 1
            IMAGES_PER_GPU = 1
        config = InferenceConfig()
    config.display()

    # Create model
    if args.command == "train":
        model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
    else:
        model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

    # Select weights file to load
    if args.weights.lower() == "coco":
        weights_path = COCO_WEIGHTS_PATH
        # Download weights file
        if not os.path.exists(weights_path):
            utils.download_trained_weights(weights_path)
    elif args.weights.lower() == "last":
        # Find last trained weights
        weights_path = model.find_last()[1]
    elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
        weights_path = model.get_imagenet_weights()
    else:
        weights_path = args.weights

    # Load weights
    print("Loading weights ", weights_path)
    if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes

        model.load_weights(weights_path, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
            "mrcnn_bbox", "mrcnn_mask"])
    else:
        model.load_weights(weights_path, by_name=True)

    # Train
    if args.command == "train":
        train(model)
    else:
        print("'{}' is not recognized. "
              "Use 'train'".format(args.command))


text:
import random
from typing import Any, List, Optional

import numpy as np
import numpy.typing as npt
import pytorch_lightning as pl
import torch
import torch.utils.data
from nuplan.planning.training.modeling.types import FeaturesType, TargetsType, move_features_type_to_device
from nuplan.planning.training.preprocessing.feature_collate import FeatureCollate
from nuplan.planning.training.preprocessing.features.raster import Raster
from nuplan.planning.training.preprocessing.features.trajectory import Trajectory
from nuplan.planning.training.visualization.raster_visualization import get_raster_with_trajectories_as_rgb

class RasterVisualizationCallback(pl.Callback):
    """
    Callbacks that visualizes model input raster and logs them in Tensorboard.
    """

    def __init__(
            self,
            images_per_tile: int,
            num_train_tiles: int,
            num_val_tiles: int,
            pixel_size: float,
    ):
        """
        Initializes the class.

        :param images_per_tile: number of images per tiles to visualize
        :param num_train_tiles: number of tiles from the training set
        :param num_val_tiles: number of tiles from the validation set
        :param pixel_size: [m] size of pixel in meters
        """
        super().__init__()

        self.custom_batch_size = images_per_tile
        self.num_train_images = num_train_tiles * images_per_tile
        self.num_val_images = num_val_tiles * images_per_tile
        self.pixel_size = pixel_size

        self.train_dataloader: Optional[torch.utils.data.DataLoader] = None
        self.val_dataloader: Optional[torch.utils.data.DataLoader] = None

    def _initialize_dataloaders(self, datamodule: pl.LightningDataModule) -> None:
        """
        Initializes the dataloaders. This makes sure that the same examples are sampled
        every time for comparison during visualization.

        :param datamodule: lightning datamodule
        """
        train_set = datamodule.train_dataloader().dataset  # type: ignore
        val_set = datamodule.val_dataloader().dataset  # type: ignore

        self.train_dataloader = self._create_dataloader(train_set, self.num_train_images)
        self.val_dataloader = self._create_dataloader(val_set, self.num_val_images)

    def _create_dataloader(self, dataset: torch.utils.data.Dataset, num_samples: int) -> torch.utils.data.DataLoader:
        dataset_size = len(dataset)
        num_keep = min(dataset_size, num_samples)
        sampled_idxs = random.sample(range(dataset_size), num_keep)
        subset = torch.utils.data.Subset(dataset=dataset, indices=sampled_idxs)
        return torch.utils.data.DataLoader(dataset=subset, batch_size=self.custom_batch_size,
                                           collate_fn=FeatureCollate())

    def _log_from_dataloader(
            self,
            pl_module: pl.LightningModule,
            dataloader: torch.utils.data.DataLoader,
            loggers: List[Any],
            training_step: int,
            prefix: str,
    ) -> None:
        """
        Visualizes and logs all examples from the input dataloader.

        :param pl_module: lightning module used for inference
        :param dataloader: torch dataloader
        :param loggers: list of loggers from the trainer
        :param training_step: global step in training
        :param prefix: prefix to add to the log tag
        """
        for batch_idx, batch in enumerate(dataloader):
            features: FeaturesType = batch[0]
            targets: TargetsType = batch[1]
            predictions = self._infer_model(pl_module, move_features_type_to_device(features, pl_module.device))

            self._log_batch(loggers, features, targets, predictions, batch_idx, training_step, prefix)

    def _log_batch(
            self,
            loggers: List[Any],
            features: FeaturesType,
            targets: TargetsType,
            predictions: TargetsType,
            batch_idx: int,
            training_step: int,
            prefix: str,
    ) -> None:
        """
        Visualizes and logs a batch of data (features, targets, predictions) from the model.

        :param loggers: list of loggers from the trainer
        :param features: tensor of model features
        :param targets: tensor of model targets
        :param predictions: tensor of model predictions
        :param batch_idx: index of total batches to visualize
        :param training_step: global trainign step
        :param prefix: prefix to add to the log tag
        """
        if 'trajectory' not in targets and 'trajectory' not in predictions:
            return

        if 'raster' in features:
            image_batch = self._get_raster_images_from_batch(
                features['raster'], targets['trajectory'], predictions['trajectory'])
        else:
            return

        tag = f'{prefix}_visualization_{batch_idx}'

        for logger in loggers:
            if isinstance(logger, torch.utils.tensorboard.writer.SummaryWriter):
                logger.add_images(
                    tag=tag,
                    img_tensor=torch.from_numpy(image_batch),
                    global_step=training_step,
                    dataformats='NHWC',
                )

    def _get_raster_images_from_batch(self, features: Raster, targets: Trajectory, predictions: Trajectory) \
            -> npt.NDArray[np.float32]:
        """
        Creates a list of RGB raster images from a batch of model data.

        :param features: tensor of model features
        :param targets: tensor of model targets
        :param predictions: tensor of model predictions
        :return: list of raster images
        """
        images = list()

        for feature, target, prediction in zip(features.data, targets.data, predictions.data):
            raster = Raster.from_feature_tensor(feature)
            target_trajectory = Trajectory(target)
            predicted_trajectory = Trajectory(prediction)

            image = get_raster_with_trajectories_as_rgb(
                self.pixel_size,
                raster,
                target_trajectory,
                predicted_trajectory,
            )

            images.append(image)

        return np.asarray(images)

    def _infer_model(self, pl_module: pl.LightningModule, features: FeaturesType) -> TargetsType:
        """
        Makes an inference of the input batch features given a model.

        :param pl_module: lightning model
        :param features: model inputs
        :return: model predictions
        """
        with torch.no_grad():
            pl_module.eval()
            predictions = move_features_type_to_device(pl_module(features), torch.device('cpu'))
            pl_module.train()

        return predictions

    def on_train_epoch_end(
            self,
            trainer: pl.Trainer,
            pl_module: pl.LightningModule,
            unused: Optional = None,  # type: ignore
    ) -> None:
        """
        Visualizes and logs training examples at the end of the epoch.

        :param trainer: lightning trainer
        :param pl_module: lightning module
        """
        assert hasattr(trainer, 'datamodule'), "Trainer missing datamodule attribute"
        assert hasattr(trainer, 'global_step'), "Trainer missing global_step attribute"

        if self.train_dataloader is None:
            self._initialize_dataloaders(trainer.datamodule)  # type: ignore

        self._log_from_dataloader(
            pl_module,
            self.train_dataloader,
            trainer.logger.experiment,
            trainer.global_step,  # type: ignore
            'train',
        )

    def on_validation_epoch_end(
            self,
            trainer: pl.Trainer,
            pl_module: pl.LightningModule,
            unused: Optional = None,  # type: ignore
    ) -> None:
        """
        Visualizes and logs validation examples at the end of the epoch.

        :param trainer: lightning trainer
        :param pl_module: lightning module
        """
        assert hasattr(trainer, 'datamodule'), "Trainer missing datamodule attribute"
        assert hasattr(trainer, 'global_step'), "Trainer missing global_step attribute"

        if self.val_dataloader is None:
            self._initialize_dataloaders(trainer.datamodule)  # type: ignore

        self._log_from_dataloader(
            pl_module,
            self.val_dataloader,
            trainer.logger.experiment,
            trainer.global_step,  # type: ignore
            'val',
        )

max_stars_repo_name: MCZhi/nuplan-devkit

max_issues_repo_name: MCZhi/nuplan-devkit

max_forks_repo_name: MCZhi/nuplan-devkit


text:
import os

import numpy as np

import I3Tray
import icecube.icetray
import icecube.dataclasses
import icecube.clsim
import icecube.simclasses

source_directory = os.environ['I3_SRC']
file = open(source_directory + "/ppc/resources/ice/dx.dat")
contents = []
for line in file:
    contents += [line.split()]
DOMs = []
for key in contents:
    # i contains string , OM_number , orientation of cable , and error on orientation
    DOMs.append( [icecube.icetray.OMKey( int(key[0]) , int(key[1]) ) , float(key[2])] )

class AddCylinder(icecube.icetray.I3Module):

    def __init__(self,context):
        icecube.icetray.I3Module.__init__(self,context)
        self.AddParameter( "TopCylinder" , "Position of top of cylinder" , icecube.dataclasses.I3Position( 0 , 0 , 500 ))
        self.AddParameter( "BottomCylinder" , "Position of the bottom of cylinder" , icecube.dataclasses.I3Position( 0 , 0 , -500 ))
        self.AddParameter( "Radius" , "Radius of Cylinder" , 10)
        self.AddParameter( "CableMapName" , "Frame key of cable map" , "CableMap")

    def Configure(self):
        self.top = self.GetParameter('TopCylinder')
        self.bottom = self.GetParameter('BottomCylinder')
        self.radius = self.GetParameter('Radius')
        self.cable_map_name = self.GetParameter('CableMapName')

    def Geometry(self,frame):
        frame['Cable'] = icecube.simclasses.I3ExtraGeometryItemCylinder(self.top , self.bottom , self.radius)
        self.PushFrame(frame)

class AddCylinders(icecube.icetray.I3Module):
    '''
    For each DOM in the geometry a cylinder is added right next to each DOM.
    '''
    def __init__(self,context):
        icecube.icetray.I3Module.__init__(self,context)
        self.AddParameter( "CylinderLength", "Length of the cable" , 1.0 )
        self.AddParameter( "CylinderRadius" , "Radius of the cable" , 0.023 ) #The radius of the cables is 23 mm
        self.AddParameter( "CableMapName" , "Frame key of cable map" , "CableMap")
        self.AddParameter( "DOMRadius" , "Radius of the DOM" , 0.1651*I3Tray.I3Units.m)

    def Configure(self):
        self.height = self.GetParameter("CylinderLength")
        self.radius = self.GetParameter("CylinderRadius")
        self.cable_map_name = self.GetParameter('CableMapName')
        self.dom_radius = self.GetParameter("DOMRadius")

    def Geometry(self,frame):
        geometry = frame["I3Geometry"]
        cable_map = icecube.simclasses.I3CylinderMap()
        for i in DOMs:
            om_key = i[0]
            orientation = i[1] * (np.pi/180.0)
            position_x = geometry.omgeo[om_key].position.x + (self.dom_radius + self.radius) * np.cos( orientation )
            position_y = geometry.omgeo[om_key].position.y + (self.dom_radius + self.radius) * np.sin( orientation )
            position_z = geometry.omgeo[om_key].position.z
            top = icecube.dataclasses.I3Position( position_x , position_y , position_z + self.height/2.0)
            bottom = icecube.dataclasses.I3Position( position_x , position_y , position_z - self.height/2.0)
            cable_map[om_key] = icecube.simclasses.I3ExtraGeometryItemCylinder(top, bottom, self.radius)

        frame[self.cable_map_name] = cable_map
        self.PushFrame(frame)

class AverageShadowFraction(icecube.icetray.I3Module):
    def __init__(self, context):
        icecube.icetray.I3Module.__init__(self, context)

        self.AddParameter("PhotonMapName", "Map name of unshadowed photons", "")
        self.AddParameter("ShadowedPhotonMapName", "Map name of shadowed photons", "")

        self.measured_shadow_fractions = list()

    def Configure(self):
        self.shadowed_photons = self.GetParameter("ShadowedPhotonMapName")
        self.photons = self.GetParameter("PhotonMapName")

    def DAQ(self, frame):
        '''
        Calculate the ratio of shadowed to unshadowed photons.
        '''
        # the first element should be the number of shadowed photons
        # the second element should be the number of unshadowed photons.
        photon_counts = list()
        for frame_key in [self.shadowed_photons, self.photons]:
            photons = frame[frame_key]
            photon_counts.append(sum([len(i) for i in photons.values()]))

        shadow_fraction = float(photon_counts[0])/float(photon_counts[1])
        self.measured_shadow_fractions.append(shadow_fraction)
        self.PushFrame(frame)

    def Finish(self):
        average_shadow_fraction = sum(self.measured_shadow_fractions)/len(self.measured_shadow_fractions)
        print("Average Shadow Fraction = %.2f" % average_shadow_fraction)


text:
import logging

import h5py
import numpy as np

from westpa.tools import (WESTParallelTool, ProgressIndicatorComponent)
from westpa.fasthist import histnd, normhistnd
from westpa.core import h5io

log = logging.getLogger('w_eddist')

class DurationDataset:
    '''A facade for the 'dsspec' dataclass that incorporates the mask into get_iter_data method'''

    def __init__(self, dataset, mask, iter_start=1):
        self.dataset = dataset
        self.mask = mask
        self.dtype = dataset.dtype
        self.iter_start = iter_start

    def get_iter_data(self, n_iter):
        try:
            assert n_iter >= self.iter_start
            dset = self.dataset[n_iter-1][self.mask[n_iter-self.iter_start]]
        except(AssertionError, IndexError):
            raise ValueError("Iteration {} is not within the iteration range".format(n_iter))
        nsegs = dset.shape[0]
        if nsegs == 0:
            return None
        else:
            return dset.reshape(nsegs, 1, 1)

def isiterable(x):
    try:
        iter(x)
    except TypeError:
        return False
    else:
        return True

def _remote_min_max(ndim, dset_dtype, n_iter, dsspec):
    try:
        minval = np.finfo(dset_dtype).min
        maxval = np.finfo(dset_dtype).max
    except ValueError:
        minval = np.iinfo(dset_dtype).min
        maxval = np.iinfo(dset_dtype).max

    data_range = [(maxval,minval) for _i in range(ndim)]

    dset = dsspec.get_iter_data(n_iter)

    if dset == None:
        return data_range

    for idim in range(ndim):
        dimdata = dset[:,:,idim]
        current_min, current_max = data_range[idim]
        current_min = min(current_min, dimdata.min())
        current_max = max(current_max, dimdata.max())
        data_range[idim] = (current_min, current_max)
        del dimdata
    del dset
    return data_range

def _remote_bin_iter(iiter, n_iter, dsspec, wt_dsspec, initpoint, binbounds, ignore_out_of_range):

    iter_hist_shape = tuple(len(bounds)-1 for bounds in binbounds)
    iter_hist = np.zeros(iter_hist_shape, dtype=np.float64)

    dset = dsspec.get_iter_data(n_iter)
    if dset is None:
        return iiter, n_iter, iter_hist
    else:
        npts = dset.shape[1]
    weights = wt_dsspec.get_iter_data(n_iter)[:,0,0]

    #dset = dset[:,initpoint:,:]
    for ipt in range(npts-initpoint):
        histnd(dset[:,ipt,:], binbounds, weights, out=iter_hist, binbound_check = False, ignore_out_of_range=ignore_out_of_range)

    del weights, dset

    # normalize histogram
    normhistnd(iter_hist,binbounds)
    return iiter, n_iter, iter_hist

class WEDDist(WESTParallelTool):
    prog = 'w_eddist'
    description = '''\
Calculate time-resolved transition-event duration distribution from kinetics results

-----------------------------------------------------------------------------
Source data
-----------------------------------------------------------------------------

Source data is collected from the results of 'w_kinetics trace' (see w_kinetics trace --help for
more information on generating this dataset).

-----------------------------------------------------------------------------
Histogram binning
-----------------------------------------------------------------------------

By default, histograms are constructed with 100 bins in each dimension. This
can be overridden by specifying -b/--bins, which accepts a number of different
kinds of arguments:

  a single integer N
    N uniformly spaced bins will be used in each dimension.

  a sequence of integers N1,N2,... (comma-separated)
    N1 uniformly spaced bins will be used for the first dimension, N2 for the
    second, and so on.

  a list of lists [[B11, B12, B13, ...], [B21, B22, B23, ...], ...]
    The bin boundaries B11, B12, B13, ... will be used for the first dimension,
    B21, B22, B23, ... for the second dimension, and so on. These bin
    boundaries need not be uniformly spaced. These expressions will be
    evaluated with Python's ``eval`` construct, with ``np`` available for
    use [e.g. to specify bins using np.arange()].

The first two forms (integer, list of integers) will trigger a scan of all
data in each dimension in order to determine the minimum and maximum values,
which may be very expensive for large datasets. This can be avoided by
explicitly providing bin boundaries using the list-of-lists form.

Note that these bins are *NOT* at all related to the bins used to drive WE
sampling.

-----------------------------------------------------------------------------
Output format
-----------------------------------------------------------------------------

The output file produced (specified by -o/--output, defaulting to "pdist.h5")
may be fed to plothist to generate plots (or appropriately processed text or
HDF5 files) from this data. In short, the following datasets are created:

  ``histograms``
    Normalized histograms. The first axis corresponds to iteration, and
    remaining axes correspond to dimensions of the input dataset.

  ``/binbounds_0``
    Vector of bin boundaries for the first (index 0) dimension. Additional
    datasets similarly named (/binbounds_1, /binbounds_2, ...) are created
    for additional dimensions.

  ``/midpoints_0``
    Vector of bin midpoints for the first (index 0) dimension. Additional
    datasets similarly named are created for additional dimensions.

  ``n_iter``
    Vector of iteration numbers corresponding to the stored histograms (i.e.
    the first axis of the ``histograms`` dataset).

-----------------------------------------------------------------------------
Subsequent processing
-----------------------------------------------------------------------------

The output generated by this program (-o/--output, default "pdist.h5") may be
plotted by the ``plothist`` program. See ``plothist --help`` for more
information.

-----------------------------------------------------------------------------
Parallelization
-----------------------------------------------------------------------------

This tool supports parallelized binning, including reading of input data.
Parallel processing is the default. For simple cases (reading pre-computed
input data, modest numbers of segments), serial processing (--serial) may be
more efficient.

-----------------------------------------------------------------------------
Command-line options
-----------------------------------------------------------------------------

'''

    def __init__(self):
        super(WEDDist,self).__init__()

        # Parallel processing by default (this is not actually necessary, but it is
        # informative!)
        self.wm_env.default_work_manager = self.wm_env.default_parallel_work_manager

        # These are used throughout
        self.progress = ProgressIndicatorComponent()
        self.default_kinetics_file = 'kintrace.h5'
        self.kinetics_filename = None
        self.kinetics_file = None  #Kinavg file
        self.istate = None
        self.fstate = None
        ##Duration and weight dsspecs
        self.duration_dsspec = None
        self.wt_dsspec = None
        self.binspec = None
        self.output_filename = None
        self.output_file = None

        # These are used during histogram generation only
        self.iter_start = None
        self.iter_stop = None
        self.ndim = None
        #self.ntimepoints = None
        self.dset_dtype = None
        self.binbounds = None  # bin boundaries for each dimension
        self.midpoints = None  # bin midpoints for each dimension
        self.data_range = None # data range for each dimension, as the pairs (min,max)
        self.ignore_out_of_range = False
        self.compress_output = False

    def add_args(self, parser):

        parser.add_argument('-b', '--bins', dest='bins', metavar='BINEXPR', default='100',
                            help='''Use BINEXPR for bins. This may be an integer, which will be used for each
                            dimension of the progress coordinate; a list of integers (formatted as [n1,n2,...])
                            which will use n1 bins for the first dimension, n2 for the second dimension, and so on;
                            or a list of lists of boundaries (formatted as [[a1, a2, ...], [b1, b2, ...], ... ]), which
                            will use [a1, a2, ...] as bin boundaries for the first dimension, [b1, b2, ...] as bin boundaries
                            for the second dimension, and so on. (Default: 100 bins in each dimension.)''')

        parser.add_argument('-C', '--compress', action='store_true',
                            help='''Compress histograms. May make storage of higher-dimensional histograms
                            more tractable, at the (possible extreme) expense of increased analysis time.
                            (Default: no compression.)''')

        parser.add_argument('--loose', dest='ignore_out_of_range', action='store_true',
                            help='''Ignore values that do not fall within bins. (Risky, as this can make buggy bin
                            boundaries appear as reasonable data. Only use if you are
                            sure of your bin boundary specification.)''')

        parser.add_argument('--istate', type=int, required=True, dest='istate',
                            help='''Initial state defining transition event''')

        parser.add_argument('--fstate', type=int, required=True, dest='fstate',
                            help='''Final state defining transition event''')

        itergroup = parser.add_argument_group('iteration range options')

        itergroup.add_argument('--first-iter', default=1, dest='iter_start', type=int,
                               help='''Iteration to begin analysis (default: 1)''')

        itergroup.add_argument('--last-iter', dest='iter_stop', type=int,
                               help='''Iteration to end analysis''')

        iogroup = parser.add_argument_group('input/output options')

        # self.default_kinetics_file will be picked up as a class attribute from the appropriate subclass
        iogroup.add_argument('-k', '--kinetics', default=self.default_kinetics_file,
                            help='''Populations and transition rates (including evolution) are stored in KINETICS
                            (default: %(default)s).''')
        iogroup.add_argument('-o', '--output', dest='output', default='eddist.h5',
                            help='''Store results in OUTPUT (default: %(default)s).''')

        self.progress.add_args(parser)

    def process_args(self, args):
        self.progress.process_args(args)
        self.kinetics_filename = args.kinetics
        self.istate = args.istate
        self.fstate = args.fstate
        self.kinetics_file = h5io.WESTPAH5File(self.kinetics_filename, 'r')

        self.iter_start = args.iter_start
        if args.iter_stop is None:
            self.iter_stop = self.kinetics_file.attrs['iter_stop']
        else:
            self.iter_stop = args.iter_stop + 1

        self.binspec = args.bins
        self.output_filename = args.output
        self.ignore_out_of_range = bool(args.ignore_out_of_range)
        self.compress_output = args.compress or False

    def go(self):

        pi = self.progress.indicator
        pi.operation = 'Initializing'
        with pi:
            self.duration = self.kinetics_file['durations'][self.iter_start-1:self.iter_stop-1]

            ##Only select transition events from specified istate to fstate
            mask = (self.duration['istate'] == self.istate) & (self.duration['fstate'] == self.fstate)

            self.duration_dsspec = DurationDataset(self.kinetics_file['durations']['duration'], mask, self.iter_start)
            self.wt_dsspec = DurationDataset(self.kinetics_file['durations']['weight'], mask, self.iter_start)

            self.output_file = h5py.File(self.output_filename, 'w')
            h5io.stamp_creator_data(self.output_file)

            # Construct bin boundaries
            self.construct_bins(self.parse_binspec(self.binspec))
            for idim, (binbounds, midpoints) in enumerate(zip(self.binbounds, self.midpoints)):
                self.output_file['binbounds_{}'.format(idim)] = binbounds
                self.output_file['midpoints_{}'.format(idim)] = midpoints

            # construct histogram
            self.construct_histogram()

            # Record iteration range
            iter_range = np.arange(self.iter_start, self.iter_stop, 1, dtype=(np.min_scalar_type(self.iter_stop)))
            self.output_file['n_iter'] = iter_range
            self.output_file['histograms'].attrs['iter_start'] = self.iter_start
            self.output_file['histograms'].attrs['iter_stop'] = self.iter_stop

            self.output_file.close()

    @staticmethod
    def parse_binspec(binspec):

        namespace = {'numpy': np,
                     'np': np,
                     'inf': float('inf')}

        try:
            binspec_compiled = eval(binspec,namespace)
        except Exception as e:
            raise ValueError('invalid bin specification: {!r}'.format(e))
        else:
            if log.isEnabledFor(logging.DEBUG):
                log.debug('bin specs: {!r}'.format(binspec_compiled))
        return binspec_compiled

    def construct_bins(self, bins):
        '''
        Construct bins according to ``bins``, which may be:

          1) A scalar integer (for that number of bins in each dimension)
          2) A sequence of integers (specifying number of bins for each dimension)
          3) A sequence of sequences of bin boundaries (specifying boundaries for each dimension)

        Sets ``self.binbounds`` to a list of arrays of bin boundaries appropriate for passing to
        fasthist.histnd, along with ``self.midpoints`` to the midpoints of the bins.
        '''

        if not isiterable(bins):
            self._construct_bins_from_scalar(bins)
        elif not isiterable(bins[0]):
            self._construct_bins_from_int_seq(bins)
        else:
            self._construct_bins_from_bound_seqs(bins)

        if log.isEnabledFor(logging.DEBUG):
            log.debug('binbounds: {!r}'.format(self.binbounds))

    def scan_data_shape(self):
        if self.ndim is None:
            dset = self.duration_dsspec
            #self.ntimepoints = dset.shape[1]
            #self.ndim = dset.shape[2]
            self.ndim = 1
            self.dset_dtype = dset.dtype

    def scan_data_range(self):
        '''Scan input data for range in each dimension. The number of dimensions is determined
        from the shape of the progress coordinate as of self.iter_start.'''

        self.progress.indicator.new_operation('Scanning for data range', self.iter_stop-self.iter_start)
        self.scan_data_shape()

        dset_dtype = self.dset_dtype
        ndim = self.ndim
        dsspec = self.duration_dsspec

        try:
            minval = np.finfo(dset_dtype).min
            maxval = np.finfo(dset_dtype).max
        except ValueError:
            minval = np.iinfo(dset_dtype).min
            maxval = np.iinfo(dset_dtype).max

        data_range = self.data_range = [(maxval,minval) for _i in range(self.ndim)]

        #futures = []
        #for n_iter in xrange(self.iter_start, self.iter_stop):
            #_remote_min_max(ndim, dset_dtype, n_iter, dsspec)
        #    futures.append(self.work_manager.submit(_remote_min_max, args=(ndim, dset_dtype, n_iter, dsspec)))

        #for future in self.work_manager.as_completed(futures):
        for future in self.work_manager.submit_as_completed(((_remote_min_max, (ndim, dset_dtype, n_iter, dsspec), {})
                                                             for n_iter in range(self.iter_start, self.iter_stop)),
                                                            self.max_queue_len):
            bounds = future.get_result(discard=True)
            for idim in range(ndim):
                current_min, current_max = data_range[idim]
                current_min = min(current_min, bounds[idim][0])
                current_max = max(current_max, bounds[idim][1])
                data_range[idim] = (current_min, current_max)
            self.progress.indicator.progress += 1

    def _construct_bins_from_scalar(self, bins):
        if self.data_range is None:
            self.scan_data_range()

        #print(self.data_range)

        self.binbounds = []
        self.midpoints = []
        for idim in range(self.ndim):
            lb, ub = self.data_range[idim]
            # Advance just beyond the upper bound of the range, so that we catch
            # the maximum in the histogram
            ub *= 1.01

            #lb -= 0.01

            boundset = np.linspace(lb,ub,bins+1)
            midpoints = (boundset[:-1] + boundset[1:]) / 2.0
            self.binbounds.append(boundset)
            self.midpoints.append(midpoints)

    def _construct_bins_from_int_seq(self, bins):
        if self.data_range is None:
            self.scan_data_range()

        self.binbounds = []
        self.midpoints = []
        for idim in range(self.ndim):
            lb, ub = self.data_range[idim]
            # Advance just beyond the upper bound of the range, so that we catch
            # the maximum in the histogram
            ub *= 1.01

            boundset = np.linspace(lb,ub,bins[idim]+1)
            midpoints = (boundset[:-1] + boundset[1:]) / 2.0
            self.binbounds.append(boundset)
            self.midpoints.append(midpoints)

    def _construct_bins_from_bound_seqs(self, bins):
        self.binbounds = []
        self.midpoints = []
        for boundset in bins:
            boundset = np.asarray(boundset)
            if (np.diff(boundset) <= 0).any():
                raise ValueError('boundary set {!r} is not strictly monotonically increasing'.format(boundset))
            self.binbounds.append(boundset)
            self.midpoints.append((boundset[:-1]+boundset[1:])/2.0)

    def construct_histogram(self):
        '''Construct a histogram using bins previously constructed with ``construct_bins()``.
        The time series of histogram values is stored in ``histograms``.
        Each histogram in the time series is normalized.'''

        self.scan_data_shape()

        iter_count = self.iter_stop - self.iter_start
        histograms_ds = self.output_file.create_dataset('histograms', dtype=np.float64,
                                                        shape=((iter_count,) + tuple(len(bounds)-1 for bounds in self.binbounds)),
                                                        compression=9 if self.compress_output else None)
        binbounds = [np.require(boundset, self.dset_dtype, 'C') for boundset in self.binbounds]

        self.progress.indicator.new_operation('Constructing histograms',self.iter_stop-self.iter_start)
        task_gen = ((_remote_bin_iter, (iiter, n_iter, self.duration_dsspec, self.wt_dsspec, 0, binbounds,
                                        self.ignore_out_of_range), {})
                    for (iiter,n_iter) in enumerate(range(self.iter_start, self.iter_stop)))
        #futures = set()
        #for iiter, n_iter in enumerate(xrange(self.iter_start, self.iter_stop)):
        #    initpoint = 1 if iiter > 0 else 0
        #    futures.add(self.work_manager.submit(_remote_bin_iter,
        #                                            args=(iiter, n_iter, self.dsspec, self.wt_dsspec, initpoint, binbounds)))

        #for future in self.work_manager.as_completed(futures):
            #future = self.work_manager.wait_any(futures)
        #for future in self.work_manager.submit_as_completed(task_gen, self.queue_size):
        log.debug('max queue length: {!r}'.format(self.max_queue_len))
        for future in self.work_manager.submit_as_completed(task_gen, self.max_queue_len):
            iiter, n_iter, iter_hist = future.get_result(discard=True)
            self.progress.indicator.progress += 1

            # store histogram
            histograms_ds[iiter] = iter_hist
            del iter_hist, future

def entry_point():
    WEDDist().main()

if __name__ == '__main__':
    entry_point()


text:
# coding=utf-8
# Copyright (C) 2020 NumS Development Team.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# coding=utf-8
# Copyright (C) 2020 NumS Development Team.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import numpy as np

from nums.core.array.application import ArrayApplication

def test_stats(app_inst: ArrayApplication):
    np_x = np.arange(100)
    ba_x = app_inst.array(np_x, block_shape=np_x.shape)
    assert np.allclose(np.mean(np_x), app_inst.mean(ba_x).get())
    assert np.allclose(np.std(np_x), app_inst.std(ba_x).get())

def test_uops(app_inst: ArrayApplication):
    np_x = np.arange(100)
    ba_x = app_inst.array(np_x, block_shape=np_x.shape)
    assert np.allclose(np.abs(np_x), app_inst.abs(ba_x).get())
    assert np.allclose(np.linalg.norm(np_x), app_inst.norm(ba_x).get())

def test_bops(app_inst: ArrayApplication):
    pairs = [(1, 2),
             (2.0, 3.0),
             (2, 3.0),
             (2.0, 3)]
    for a, b in pairs:
        np_a, np_b = np.array(a), np.array(b)
        ba_a, ba_b = app_inst.scalar(a), app_inst.scalar(b)
        assert np.allclose(np_a + np_b, (ba_a + ba_b).get())
        assert np.allclose(np_a - np_b, (ba_a - ba_b).get())
        assert np.allclose(np_a * np_b, (ba_a * ba_b).get())
        assert np.allclose(np_a / np_b, (ba_a / ba_b).get())
        assert np.allclose(np_a ** np_b, (ba_a ** ba_b).get())

if __name__ == "__main__":
    # pylint: disable=import-error
    from tests import conftest

    app_inst = conftest.get_app("serial")
    test_stats(app_inst)
    test_uops(app_inst)
    test_bops(app_inst)


text:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
from skimage import color

from time import time
from IPython.display import HTML

#from __future__ import print_function

#matplotlib inline
plt.rcParams['figure.figsize'] = (15.0, 12.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

from skimage import io, util

# Load image
img = io.imread('imgs/broadway_tower.jpg')
img = util.img_as_float(img)

plt.title('Original Image')
plt.imshow(img)
plt.show()

from seam_carving import energy_function

test_img = np.array([[1.0, 2.0, 1.5],
                     [3.0, 1.0, 2.0],
                     [4.0, 0.5, 3.0]])
test_img = np.stack([test_img] * 3, axis=2)
assert test_img.shape == (3, 3, 3)

# Compute energy function
test_energy = energy_function(test_img)

solution_energy = np.array([[3.0, 1.25,  1.0],
                            [3.5, 1.25, 1.75],
                            [4.5,  1.0,  3.5]])

print("Image (channel 0):")
print(test_img[:, :, 0])

print("Energy:")
print(test_energy)
print("Solution energy:")
print(solution_energy)

assert np.allclose(test_energy, solution_energy)

# Compute energy function
start = time()
energy = energy_function(img)
end = time()

print("Computing energy function: %f seconds." % (end - start))

plt.title('Energy')
plt.axis('off')
plt.imshow(energy)
plt.show()

from seam_carving import compute_cost

# Let's first test with a small example

test_energy = np.array([[1.0, 2.0, 1.5],
                        [3.0, 1.0, 2.0],
                        [4.0, 0.5, 3.0]])

solution_cost = np.array([[1.0, 2.0, 1.5],
                          [4.0, 2.0, 3.5],
                          [6.0, 2.5, 5.0]])

solution_paths = np.array([[ 0,  0,  0],
                           [ 0, -1,  0],
                           [ 1,  0, -1]])

# Vertical Cost Map
_=test_img
vcost, vpaths = compute_cost(_, test_energy, axis=1)  # don't need the first argument for compute_cost

print("Energy:")
print(test_energy)

print("Cost:")
print(vcost)
print("Solution cost:")
print(solution_cost)

print("Paths:")
print(vpaths)
print("Solution paths:")
print(solution_paths)

# Vertical Cost Map
start = time()
vcost, _ = compute_cost(_, energy, axis=1)  # don't need the first argument for compute_cost
end = time()

print("Computing vertical cost map: %f seconds." % (end - start))

plt.title('Vertical Cost Map')
plt.axis('off')
plt.imshow(vcost, cmap='inferno')
plt.show()

# Horizontal Cost Map
start = time()
hcost, _ = compute_cost(_, energy, axis=0)
end = time()

print("Computing horizontal cost map: %f seconds." % (end - start))

plt.title('Horizontal Cost Map')
plt.axis('off')
plt.imshow(hcost, cmap='inferno')
plt.show()

from seam_carving import backtrack_seam

# Let's first test with a small example
cost = np.array([[1.0, 2.0, 1.5],
                 [4.0, 2.0, 3.5],
                 [6.0, 2.5, 5.0]])

paths = np.array([[ 0,  0,  0],
                  [ 0, -1,  0],
                  [ 1,  0, -1]])

# Vertical Backtracking

end = np.argmin(cost[-1])
seam_energy = cost[-1, end]
seam = backtrack_seam(vpaths, end)

print('Seam Energy:', seam_energy)
print('Seam:', seam)

assert seam_energy == 2.5
assert np.allclose(seam, [0, 1, 1])

vcost, vpaths = compute_cost(img, energy)

# Vertical Backtracking
start = time()
end = np.argmin(vcost[-1])
seam_energy = vcost[-1, end]
seam = backtrack_seam(vpaths, end)
end = time()

print("Backtracking optimal seam: %f seconds." % (end - start))
print('Seam Energy:', seam_energy)

# Visualize seam
vseam = np.copy(img)
for row in range(vseam.shape[0]):
    vseam[row, seam[row], :] = np.array([1.0, 0, 0])

plt.title('Vertical Seam')
plt.axis('off')
plt.imshow(vseam)
plt.show()

from seam_carving import reduce

# Let's first test with a small example
test_img = np.arange(9, dtype=np.float64).reshape((3, 3))
test_img = np.stack([test_img, test_img, test_img], axis=2)
assert test_img.shape == (3, 3, 3)

cost = np.array([[1.0, 2.0, 1.5],
                 [4.0, 2.0, 3.5],
                 [6.0, 2.5, 5.0]])

paths = np.array([[ 0,  0,  0],
                  [ 0, -1,  0],
                  [ 1,  0, -1]])

# Reduce image width
W_new = 2

# We force the cost and paths to our values
out = reduce(test_img, W_new, cfunc=lambda x, y: (cost, paths))

print("Original image (channel 0):")
print(test_img[:, :, 0])
print("Reduced image (channel 0): we see that seam [0, 4, 7] is removed")
print(out[:, :, 0])

assert np.allclose(out[:, :, 0], np.array([[1, 2], [3, 5], [6, 8]]))

# Reduce image width

H, W, _ = img.shape

W_new = 400

start = time()
out = reduce(img, W_new)
end = time()

print("Reducing width from %d to %d: %f seconds." % (W, W_new, end - start))

plt.subplot(2, 1, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(2, 1, 2)
plt.title('Resized')
plt.imshow(out)

plt.show()

# Reduce image height
H, W, _ = img.shape
H_new = 300

start = time()
out = reduce(img, H_new, axis=0)
end = time()

print("Reducing height from %d to %d: %f seconds." % (H, H_new, end - start))

plt.subplot(1, 2, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.title('Resized')
plt.imshow(out)

plt.show()

from seam_carving import enlarge_naive

# Let's first test with a small example
test_img = np.arange(9, dtype=np.float64).reshape((3, 3))
test_img = np.stack([test_img, test_img, test_img], axis=2)
assert test_img.shape == (3, 3, 3)

cost = np.array([[1.0, 2.0, 1.5],
                 [4.0, 2.0, 3.5],
                 [6.0, 2.5, 5.0]])

paths = np.array([[ 0,  0,  0],
                  [ 0, -1,  0],
                  [ 1,  0, -1]])

# Increase image width
W_new = 4

# We force the cost and paths to our values
out = enlarge_naive(test_img, W_new, cfunc=lambda x, y: (cost, paths))

print("Original image (channel 0):")
print(test_img[:, :, 0])
print("Enlarged image (channel 0): we see that seam [0, 4, 7] is duplicated")
print(out[:, :, 0])

assert np.allclose(out[:, :, 0], np.array([[0, 0, 1, 2], [3, 4, 4, 5], [6, 7, 7, 8]]))

W_new = 800

# This is a naive implementation of image enlarging
# which iteratively computes energy function, finds optimal seam
# and duplicates it.
# This process will a stretching artifact by choosing the same seam
start = time()
enlarged = enlarge_naive(img, W_new)
end = time()

# Can take around 20 seconds
print("Enlarging(naive) height from %d to %d: %f seconds." \
      % (W, W_new, end - start))

plt.imshow(enlarged)
plt.show()

from seam_carving import find_seams

# Alternatively, find k seams for removal and duplicate them.
H, W, _ = img.shape
W_new = 800
start = time()
seams = find_seams(img, W_new - W)
end = time()

# Can take around 10 seconds
print("Finding %d seams: %f seconds." % (W_new - W, end - start))

plt.imshow(seams, cmap='viridis')
plt.show()

from seam_carving import enlarge

# Let's first test with a small example
test_img = np.array([[0.0, 1.0, 3.0],
                     [0.0, 1.0, 3.0],
                     [0.0, 1.0, 3.0]])
#test_img = np.arange(9, dtype=np.float64).reshape((3, 3))
test_img = np.stack([test_img, test_img, test_img], axis=2)
assert test_img.shape == (3, 3, 3)

# Increase image width
W_new = 5
H, W, _ = img.shape
out_naive = enlarge_naive(test_img, W_new)
out = enlarge(test_img, W_new)

print("Original image (channel 0):")
print(test_img[:, :, 0])
print("Enlarged naive image (channel 0): first seam is duplicated twice.")
print(out_naive[:, :, 0])
print("Enlarged image (channel 0): first and second seam are each duplicated once.")
print(out[:, :, 0])

assert np.allclose(out[:, :, 0], np.array([[0, 0, 1, 1, 3], [0, 0, 1, 1, 3], [0, 0, 1, 1, 3]]))

W_new = 800

start = time()
out = enlarge(img, W_new)
end = time()

# Can take around 20 seconds
print("Enlarging width from %d to %d: %f seconds." \
      % (W, W_new, end - start))

plt.subplot(2, 1, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(2, 1, 2)
plt.title('Resized')
plt.imshow(out)

plt.show()

# Map of the seams for horizontal seams.
start = time()
seams = find_seams(img, W_new - W, axis=0)
end = time()

# Can take around 15 seconds
print("Finding %d seams: %f seconds." % (W_new - W, end - start))

plt.imshow(seams, cmap='viridis')
plt.show()

H_new = 600

start = time()
out = enlarge(img, H_new, axis=0)
end = time()

# Can take around 20 seconds
print("Enlarging height from %d to %d: %f seconds." \
      % (H, H_new, end - start))

plt.subplot(1, 2, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(1, 2, 2)
plt.title('Resized')
plt.imshow(out)

plt.show()

# Reduce image width
W_new = 200

start = time()
out = reduce(img, W_new)
end = time()

print("Reducing width from %d to %d: %f seconds." % (W, W_new, end - start))

plt.subplot(2, 1, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(2, 1, 2)
plt.title('Resized')
plt.imshow(out)

plt.show()

from seam_carving import reduce_fast

# Reduce image width
H, W, _ = img.shape
W_new = 400

start = time()
out = reduce(img, W_new)
end = time()

print("Normal reduce width from %d to %d: %f seconds." % (W, W_new, end - start))

start = time()
out_fast = reduce_fast(img, W_new)
end = time()

print("Faster reduce width from %d to %d: %f seconds." % (W, W_new, end - start))

assert np.allclose(out, out_fast), "Outputs don't match."

plt.subplot(3, 1, 1)
plt.title('Original')
plt.imshow(img)

plt.subplot(3, 1, 2)
plt.title('Resized')
plt.imshow(out)

plt.subplot(3, 1, 3)
plt.title('Faster resized')
plt.imshow(out)

plt.show()

# Load image
img2 = io.imread('imgs/wave.jpg')
img2 = util.img_as_float(img2)

plt.title('Original Image')
plt.imshow(img2)
plt.show()

out = reduce(img2, 300)
plt.imshow(out)
plt.show()

out = enlarge(img2, 800)
plt.imshow(out)
plt.show()

# Load image
img_yolo = io.imread('imgs/yolo.jpg')
img_yolo = util.img_as_float(img_yolo)

plt.title('Original Image')
plt.imshow(img_yolo)
plt.show()

from seam_carving import compute_forward_cost

# Let's first test with a small example
img_test = np.array([[1.0, 1.0, 2.0],
                     [0.5, 0.0, 0.0],
                     [1.0, 0.5, 2.0]])
img_test = np.stack([img_test]*3, axis=2)
assert img_test.shape == (3, 3, 3)

energy = energy_function(img_test)

solution_cost = np.array([[0.5, 2.5, 3.0],
                          [1.0, 2.0, 3.0],
                          [2.0, 4.0, 6.0]])

solution_paths = np.array([[ 0,  0,  0],
                           [ 0, -1, -1],
                           [ 0, -1, -1]])

# Vertical Cost Map
vcost, vpaths = compute_forward_cost(img_test, energy)  # don't need the first argument for compute_cost

print("Image:")
print(color.rgb2grey(img_test))

print("Energy:")
print(energy)

print("Cost:")
print(vcost)
print("Solution cost:")
print(solution_cost)

print("Paths:")
print(vpaths)
print("Solution paths:")
print(solution_paths)

assert np.allclose(solution_cost, vcost)
assert np.allclose(solution_paths, vpaths)

from seam_carving import compute_forward_cost

energy = energy_function(img_yolo)

out, _ = compute_cost(img_yolo, energy)
plt.subplot(1, 2, 1)
plt.imshow(out, cmap='inferno')
plt.title("Normal cost function")

out, _ = compute_forward_cost(img_yolo, energy)
plt.subplot(1, 2, 2)
plt.imshow(out, cmap='inferno')
plt.title("Forward cost function")

plt.show()

from seam_carving import reduce
out = reduce(img_yolo, 200, axis=0)
plt.imshow(out)
plt.show()

# This step can take a very long time depending on your implementation.
out = reduce(img_yolo, 200, axis=0, cfunc=compute_forward_cost)
plt.imshow(out)
plt.show()

# Load image
image = io.imread('imgs/wyeth.jpg')
image = util.img_as_float(image)

mask = io.imread('imgs/wyeth_mask.jpg', as_grey=True)
mask = util.img_as_bool(mask)

plt.subplot(1, 2, 1)
plt.title('Original Image')
plt.imshow(image)

plt.subplot(1, 2, 2)
plt.title('Mask of the object to remove')
plt.imshow(mask)

plt.show()

# Use your function to remove the object
out = remove_object(image, mask)

plt.subplot(2, 2, 1)
plt.title('Original Image')
plt.imshow(image)

plt.subplot(2, 2, 2)
plt.title('Mask of the object to remove')
plt.imshow(mask)

plt.subplot(2, 2, 3)
plt.title('Image with object removed')
plt.imshow(out)

plt.show()

max_stars_repo_path: Computer Vision/CS131/HW4/test.py

max_stars_repo_name: bayeslabs/AiGym

max_issues_repo_path: Computer Vision/CS131/HW4/test.py

max_issues_repo_name: bayeslabs/AiGym

max_forks_repo_path: Computer Vision/CS131/HW4/test.py

max_forks_repo_name: bayeslabs/AiGym