pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

How to apply the `GroupLasso` in pyvips

Open SikangSHU opened this issue 3 years ago • 0 comments

Dear expert: Hello! How can I apply the GroupLasso in pyvips so that it can be done for each pixel of the image and w_hat[0],w_hat[3] can be known to judge whether they are zero. The meaning of my problem is as the following program shows while it can't be run successfully.

import numpy as np
import matplotlib.pyplot as plt
from skimage import data, io
from numpy import double, linalg
import pyvips
from celer.homotopy import _alpha_max_grp
from celer import GroupLasso
import gc

image = pyvips.Image.tiffload('E:\\testpicture_jupyter\\4colour_ceshitu\\unc9.ome.tif')

image /= 255

y = image.log()

stain_vectors_1 = [
    [0.571, 0.095, 0.767],
    [0.584, 0.258, 0.576],
    [0.577, 0.961, 0.284]
]
stain_inverse_1 = linalg.inv(stain_vectors_1).tolist()
stain_space_1 = y.recomb(stain_inverse_1)
y1 = stain_space_1.recomb(stain_vectors_1)

stain_vectors_2 = [
    [0.095, 0.105, 0.767],
    [0.258, 0.758, 0.576],
    [0.961, 0.644, 0.284]
]
stain_inverse_2 = linalg.inv(stain_vectors_2).tolist()
stain_space_2 = y.recomb(stain_inverse_2)
y2 = stain_space_2.recomb(stain_vectors_2)

stain_vectors_3 = [
    [0.571, 0.767, -0.48],
    [0.584, 0.576, 0.808],
    [0.577, 0.284, -0.343]
]
stain_inverse_3 = linalg.inv(stain_vectors_3).tolist()
stain_space_3 = y.recomb(stain_inverse_3)
y3 = stain_space_3.recomb(stain_vectors_3)

stain_vectors_4 = [
    [0.095, 0.767, -0.553],
    [0.258, 0.576, 0.817],
    [0.961, 0.284, -0.165]
]
stain_inverse_4 = linalg.inv(stain_vectors_4).tolist()
stain_space_4 = y.recomb(stain_inverse_4)
y4 = stain_space_4.recomb(stain_vectors_4)

stain_vectors_5 = [
    [0.105, 0.767, -0.218],
    [0.758, 0.576, 0.649],
    [0.644, 0.284, -0.729]
]
stain_inverse_5 = linalg.inv(stain_vectors_5).tolist()
stain_space_5 = y.recomb(stain_inverse_5)
y5 = stain_space_5.recomb(stain_vectors_5)

stain_space_1_exp = stain_space_1.exp()
stain_space_2_exp = stain_space_2.exp()
stain_space_3_exp = stain_space_3.exp()
stain_space_4_exp = stain_space_4.exp()
stain_space_5_exp = stain_space_5.exp()

X = np.array([[0.571, 0.095, 0.767, 0.095, 0.105, 0.767, 0.571, 0.767, 0.095, 0.767, 0.105, 0.767],
              [0.584, 0.258, 0.576, 0.258, 0.758, 0.576, 0.584, 0.576, 0.258, 0.576, 0.758, 0.576],
              [0.577, 0.961, 0.284, 0.961, 0.644, 0.284, 0.577, 0.284, 0.961, 0.284, 0.644, 0.284]])
X = np.asfortranarray(X)

groups_celer = [[0, 1, 2], [3, 4, 5], [6, 7], [8, 9], [10, 11]]

alpha_max = _alpha_max_grp(X, y, groups_celer, center=True)
alpha = alpha_max
gl = GroupLasso(
    groups=groups_celer,
    alpha=alpha,
    max_iter=10,
    tol=1e-3,
    verbose=1,
)
gl.fit(X, y)
w_hat = gl.coef_

gray_CD8 = []
gray_PanCK = []
gray_PDL1 = []
gray_Hema = []

gray_PanCK = (w_hat[3] != 0).ifthenelse(stain_space_2_exp[0], 1)
gray_PanCK = (w_hat[0] != 0).ifthenelse(stain_space_1_exp[1], 1)
gray_PanCK = (w_hat[8] != 0).ifthenelse(stain_space_4_exp[0], 1)

gray_CD8 = (w_hat[0] != 0).ifthenelse(stain_space_1_exp[0], 1)
gray_CD8 = (w_hat[6] != 0).ifthenelse(stain_space_3_exp[0], 1)

gray_PDL1 = (w_hat[3] != 0).ifthenelse(stain_space_2_exp[1], 1)
gray_PDL1 = (w_hat[10] != 0).ifthenelse(stain_space_5_exp[0], 1)

gray_Hema = (w_hat[3] != 0).ifthenelse(stain_space_2_exp[2], stain_space_5_exp[1])
gray_Hema = (w_hat[0] != 0).ifthenelse(stain_space_1_exp[2], stain_space_5_exp[1])
gray_Hema = (w_hat[6] != 0).ifthenelse(stain_space_3_exp[1], stain_space_5_exp[1])
gray_Hema = (w_hat[8] != 0).ifthenelse(stain_space_4_exp[1], stain_space_5_exp[1])

SikangSHU avatar Feb 21 '22 11:02 SikangSHU