Marsilea-viz / marsilea

Declarative creation of composable visualization for Python (Complex heatmap, Upset plot, Oncoprint and more~)
https://marsilea.rtfd.io/
MIT License
166 stars 6 forks source link

Provide pre-calculated linkage for the split heatmap #17

Closed liuzj039 closed 10 months ago

liuzj039 commented 10 months ago

fix #16 .

Hi @Mr-Milk , I apologize for not being able to complete this task until the weekend. I could not locate a section in the current code to save the order, hence I noted them in the _split_{axis}_group_order attribute of the clusterboard and deform, respectively. I apologise if I have overlooked anything. I hope this code would be helpful.

liuzj039 commented 10 months ago

And the use case:

ar_data = np.array([[4.4879415 , 4.10098041, 3.51843572, 3.49319912, 3.70876169,
        3.53841469, 3.62779429, 3.53420859, 3.56575433, 3.51107504,
        3.5899394 , 3.49950827, 3.73610133, 3.62148514, 3.65513393,
        3.71927694, 3.67616442, 3.50792047],
       [3.88862879, 4.4879415 , 3.49756349, 3.49206521, 3.590347  ,
        3.55873188, 3.5298659 , 3.51612019, 3.50787277, 3.52093119,
        3.51337105, 3.49481435, 3.58759786, 3.58072501, 3.63776968,
        3.80615456, 3.55941916, 3.5016872 ],
       [3.73580475, 3.60759962, 4.4879415 , 3.67597569, 3.75289876,
        3.57341159, 3.69306971, 3.56486458, 3.62469364, 3.51358253,
        3.6845227 , 3.50503552, 3.77853979, 3.60759962, 3.65033466,
        3.5905056 , 3.72725774, 3.49648851],
       [3.61952045, 3.64583624, 4.06688887, 4.4879415 , 3.54057308,
        3.51425729, 3.56688887, 3.61952045, 3.51425729, 3.61952045,
        3.51425729, 3.4879415 , 3.59320466, 3.72478361, 3.64583624,
        3.56688887, 3.51425729, 3.59320466],
       [3.78371615, 3.69780065, 3.53160347, 3.4907584 , 4.4879415 ,
        3.60202601, 3.5851246 , 3.50484291, 3.62456122, 3.53442037,
        3.59780065, 3.4907584 , 3.85273023, 3.62456122, 3.60343446,
        3.5823077 , 3.81611051, 3.50484291],
       [3.73283946, 4.0134517 , 3.53896191, 3.49304354, 3.90120681,
        4.4879415 , 3.56447211, 3.59508436, 3.56957415, 3.63590068,
        3.54406395, 3.49304354, 3.79406395, 3.7379415 , 3.67671701,
        3.7634517 , 3.68181905, 3.54406395],
       [3.98982829, 3.71813018, 3.57850754, 3.49926225, 3.74831886,
        3.54454527, 4.4879415 , 3.66907358, 3.72945093, 3.5369981 ,
        3.80114905, 3.52567735, 3.7369981 , 3.64643207, 3.58228112,
        3.60492263, 3.85020565, 3.52945093],
       [3.71358253, 3.69819791, 3.53409535, 3.51358253, 3.54947996,
        3.59563381, 3.73409535, 4.4879415 , 3.52896714, 3.70332612,
        3.62640304, 3.58024919, 3.56486458, 3.73409535, 3.61101842,
        3.66742868, 3.66230047, 3.56486458],
       [3.72137999, 3.57942415, 3.53841469, 3.49109607, 3.79393519,
        3.53841469, 3.68983424, 3.51317809, 4.4879415 , 3.51633267,
        3.59835159, 3.49109607, 3.81601721, 3.5857333 , 3.59835159,
        3.53210554, 3.66144308, 3.51002352],
       [3.64287108, 3.82596967, 3.50906826, 3.52315277, 3.72033587,
        3.69216685, 3.5794908 , 3.78371615, 3.55132178, 4.4879415 ,
        3.53019502, 3.52315277, 3.58653305, 3.82596967, 3.63582882,
        3.63582882, 3.64287108, 3.60765981],
       [4.06877982, 3.70949839, 3.62566605, 3.49392952, 3.95500737,
        3.55380976, 3.98494749, 3.64961815, 3.69752234, 3.52386964,
        4.4879415 , 3.54782174, 4.04482773, 3.70949839, 3.69752234,
        3.70351036, 4.11069599, 3.54183372],
       [3.9279415 , 3.8879415 , 3.5679415 , 3.4879415 , 3.5679415 ,
        3.5279415 , 3.8879415 , 4.2079415 , 3.5279415 , 3.6879415 ,
        3.8879415 , 4.4879415 , 3.6879415 , 4.0079415 , 3.6079415 ,
        3.6079415 , 3.9679415 , 3.6879415 ],
       [3.76723144, 3.65953913, 3.52817819, 3.49267523, 3.79445038,
        3.55894742, 3.56604801, 3.50569298, 3.61101842, 3.50450955,
        3.59800067, 3.49385866, 4.4879415 , 3.70805984, 3.79800067,
        3.80155097, 3.71042671, 3.50805984],
       [3.76223956, 3.77951817, 3.51817908, 3.50737994, 3.69744474,
        3.59377303, 3.57865424, 3.59161321, 3.55489614, 3.59161321,
        3.56785511, 3.51601925, 3.88966936, 4.4879415 , 3.74280111,
        3.77519852, 3.61537131, 3.59161321],
       [3.57011204, 3.600603  , 3.49776062, 3.49104228, 3.53031876,
        3.50706295, 3.5008614 , 3.5003446 , 3.50602936, 3.49879421,
        3.50602936, 3.48949189, 3.62334202, 3.54892341, 4.4879415 ,
        4.17166243, 3.51946605, 3.4998278 ],
       [3.56307811, 3.64606992, 3.49203986, 3.48896609, 3.51082401,
        3.50638412, 3.49852893, 3.49989505, 3.49272292, 3.49511363,
        3.50023658, 3.48896609, 3.57844696, 3.533365  , 3.93978576,
        4.4879415 , 3.50809177, 3.49408904],
       [3.85250362, 3.69975413, 3.54496798, 3.48997816, 3.96248325,
        3.56533458, 3.68346085, 3.55718794, 3.59995779, 3.53274802,
        3.69975413, 3.51238142, 3.87083356, 3.60810443, 3.61217775,
        3.60810443, 4.4879415 , 3.538858  ],
       [3.71685716, 3.72890536, 3.49998969, 3.53613427, 3.63251981,
        3.62047162, 3.62047162, 3.66866439, 3.57227885, 3.69276078,
        3.59637523, 3.54818246, 3.69276078, 4.06625475, 3.76504993,
        3.70480897, 3.78914632, 4.4879415 ]])

h = ma.Heatmap(ar_data, cmap='Reds')
h.hsplit(labels = ['a', 'b'] * 9, order=['a', 'b'])
h.vsplit(labels = ['a', 'b'] * 9, order=['a', 'b'])
h.add_dendrogram('top')

h.render()
plt.close()

h.add_dendrogram('left', linkage=h.get_col_linkage())
h.render()

image

Mr-Milk commented 10 months ago

Please don't feel pressured on this, there is no need to be hurry. The purpose of open-source is welcoming any communication and contributions then improve from that. I'll start reviewing soon. Thank you for your contributions! This is the first PR for Marsilea!

Mr-Milk commented 10 months ago

I don't know how to edit the pull request, I checkout to your branch but cannot commit to it. The git just created a new branch instead. So I just opened a new PR #18.

I made a few modifications, I placed get_linkage() method in Deformation, the ClusterBoard is for handling the rendering process. You are right about adding a split_order attribute, i merge it with the set_split_{axis} method to keep things simple. I added an extra key attribute for Dendrogram, so tracking of linkage is easier.

I will merge all the code for now. But I will have to close this and merge the #18. If you know how to edit PR directly instead of creating a new one, please let me know.

Thanks for your contributions again! Great job!

liuzj039 commented 10 months ago

Oh, I forgot to tick the box to allow maintainers to edit. If there's a next time, I'll be sure to tick it.