spm-image icon indicating copy to clipboard operation
spm-image copied to clipboard

Apply ADMM to the program

Open SikangSHU opened this issue 3 years ago • 0 comments

Hello! I'd like to apply ADMM to my program so that the speed of the algorithm can be improved. How can I do that? Any suggestion will be OK. Thank you!


from numpy import double, linalg
import pyvips
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()

mse_result1 = ((y1 - y) ** 2).bandmean()
mse_result2 = ((y2 - y) ** 2).bandmean()
mse_result3 = ((y3 - y) ** 2).bandmean()
mse_result4 = ((y4 - y) ** 2).bandmean()
mse_result5 = ((y5 - y) ** 2).bandmean()

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

gray_PanCK = (mse_result2 < mse_result1 & mse_result2 < mse_result3 & mse_result2 < mse_result4 & mse_result2 < mse_result5).ifthenelse(stain_space_2_exp[0], 1)
gray_PanCK = (mse_result1 < mse_result2 & mse_result1 < mse_result3 & mse_result1 < mse_result4 & mse_result1 < mse_result5).ifthenelse(stain_space_1_exp[1], 1)
gray_PanCK = (mse_result4 < mse_result1 & mse_result4 < mse_result2 & mse_result4 < mse_result3 & mse_result4 < mse_result5).ifthenelse(stain_space_4_exp[0], 1)

gray_CD8 = (mse_result1 < mse_result2 & mse_result1 < mse_result3 & mse_result1 < mse_result4 & mse_result1 < mse_result5).ifthenelse(stain_space_1_exp[0], 1)
gray_CD8 = (mse_result3 < mse_result1 & mse_result3 < mse_result2 & mse_result3 < mse_result4 & mse_result3 < mse_result5).ifthenelse(stain_space_3_exp[0], 1)

gray_PDL1 = (mse_result2 < mse_result1 & mse_result2 < mse_result3 & mse_result2 < mse_result4 & mse_result2 < mse_result5).ifthenelse(stain_space_2_exp[1], 1)
gray_PDL1 = (mse_result5 < mse_result1 & mse_result5 < mse_result2 & mse_result5 < mse_result3 & mse_result5 < mse_result4).ifthenelse(stain_space_5_exp[0], 1)

gray_Hema = (mse_result2 < mse_result1 & mse_result2 < mse_result3 & mse_result2 < mse_result4 & mse_result2 < mse_result5).ifthenelse(stain_space_2_exp[2], stain_space_5_exp[1])
gray_Hema = (mse_result1 < mse_result2 & mse_result1 < mse_result3 & mse_result1 < mse_result4 & mse_result1 < mse_result5).ifthenelse(stain_space_1_exp[2], stain_space_5_exp[1])
gray_Hema = (mse_result3 < mse_result1 & mse_result3 < mse_result2 & mse_result3 < mse_result4 & mse_result3 < mse_result5).ifthenelse(stain_space_3_exp[1], stain_space_5_exp[1])
gray_Hema = (mse_result4 < mse_result1 & mse_result4 < mse_result2 & mse_result4 < mse_result3 & mse_result4 < mse_result5).ifthenelse(stain_space_4_exp[1], stain_space_5_exp[1])

filename = f"CD8.tif[tile,pyramid,subifd]"
print(f"writing {filename} ...")
CD8 = (255 - gray_CD8 * 255).cast("int").colourspace("grey16")
CD8.set_type(pyvips.GValue.gstr_type, "image-description",
f"""
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    Creator="OME Bio-Formats 6.7.0"
    xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06
        http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
    <Image ID="Image:0">
        <!-- Minimum required fields about image dimensions -->
        <Pixels DimensionOrder="XYCZT"
                ID="Pixels:0"
                Interleaved="false"
                SizeC="1"
                SizeT="1"
                SizeX="61659"
                SizeY="65001"
                SizeZ="1"
                Type="uint16">
        <Channel Color="-16718848" ID="Channel:0:0" Name="Channel 1" SamplesPerPixel="1"><LightPath/></Channel><TiffData FirstC="0" FirstT="0" FirstZ="0" IFD="0" PlaneCount="1">
        </TiffData>
        </Pixels>
    </Image>
</OME>
""")
CD8.tiffsave('CD8.tif', tile=True, pyramid=True, subifd=True)

SikangSHU avatar Jan 13 '22 01:01 SikangSHU