Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error importing matrix #13

Closed
aspitaleri opened this issue Mar 22, 2023 · 3 comments
Closed

Error importing matrix #13

aspitaleri opened this issue Mar 22, 2023 · 3 comments
Labels
question Further information is requested

Comments

@aspitaleri
Copy link

aspitaleri commented Mar 22, 2023

Hi
I am importing the matrix in attach using the example in https://github.com/moshi4/pyCirclize/blob/main/docs/plot_tips.ipynb

matrix_df=pd.read_csv('t.csv',sep='\t')

circos = Circos.initialize_from_matrix(

       matrix_df,
    r_lim=(95, 100),
    space=5,
    cmap="tab10",
    link_kws=dict(ec="black", lw=0.5, direction=1),
)
fig = circos.plotfig()

but I get error:
ZeroDivisionError Traceback (most recent call last)
Cell In [1604], line 1
----> 1 circos = Circos.initialize_from_matrix(
2
3 a.replace(0,1),
4 r_lim=(95, 100),
5 space=5,
6 cmap="tab10",
7 link_kws=dict(ec="black", lw=0.5, direction=1),
8 )
9 fig = circos.plotfig()

File /opt/homebrew/lib/python3.10/site-packages/pycirclize/circos.py:224, in Circos.initialize_from_matrix(matrix, start, end, space, endspace, r_lim, cmap, link_cmap, ticks_interval, order, label_kws, ticks_kws, link_kws)
221 circos = Circos(matrix.to_sectors(), start, end, space=space, endspace=endspace)
222 for sector in circos.sectors:
223 # Plot label, outer track axis & xticks
--> 224 sector.text(sector.name, **label_kws)
225 outer_track = sector.add_track(r_lim)
226 color = name2color[sector.name]

File /opt/homebrew/lib/python3.10/site-packages/pycirclize/sector.py:249, in Sector.text(self, text, x, r, orientation, **kwargs)
246 if x is None:
247 # Get sector center radian position
248 center_x = (self.start + self.end) / 2
--> 249 rad = self.x_to_rad(center_x)
250 else:
251 rad = self.x_to_rad(x)

File /opt/homebrew/lib/python3.10/site-packages/pycirclize/sector.py:193, in Sector.x_to_rad(self, x, ignore_range_error)
191 err_msg = f"{x=} is invalid range of '{self.name}' sector.\n{self}"
192 raise ValueError(err_msg)
--> 193 size_ratio = self.rad_size / self.size
194 x_from_start = x - self.start
195 rad_from_start = x_from_start * size_ratio
ZeroDivisionError: float division by zero

I tried doing matrix_df.replace(0,10) but still same error. Any help?

t.csv

@moshi4
Copy link
Owner

moshi4 commented Mar 22, 2023

Hi, @aspitaleri

The error is probably due to the incorrect format of your matrix file (t.csv). The matrix file must have explicit row names.

Below is an example of a Chord Diagram plot using a matrix file.

from pycirclize import Circos
import pandas as pd

# Create 3 x 6 matrix file
row_names = ["F1", "F2", "F3"]
col_names = ["T1", "T2", "T3", "T4", "T5", "T6"]
matrix_data = [
    [4, 14, 13, 17, 5, 2],
    [7, 1, 6, 8, 12, 15],
    [9, 10, 3, 16, 11, 18],
]
matrix_df = pd.DataFrame(matrix_data, index=row_names, columns=col_names)
matrix_df.to_csv("matrix.tsv", sep="\t")

# Plot chord diagram (Use matrix file)
circos = Circos.initialize_from_matrix("matrix.tsv", space=5, cmap="tab10")
circos.savefig("chord_diagram.png")

matrix.tsv

	T1	T2	T3	T4	T5	T6
F1	4	14	13	17	5	2
F2	7	1	6	8	12	15
F3	9	10	3	16	11	18

chord_diagram.png

chord_diagram

@aspitaleri
Copy link
Author

I see - I will find out what it is the problem. Thanks. Anyway, if a column has all 0, you get similar error. So I will replace 0 with -1 value, should it work?

@moshi4
Copy link
Owner

moshi4 commented Mar 23, 2023

if a column has all 0, you get a similar error. so i will replace 0 with -1 value, should it work?

If a row or column has all 0 values, it means that the row or column has no data to be plotted in the Chord Diagram. Therefore, values with all 0 rows or columns should be preliminarily removed from the matrix.

The following is an example of plotting a Chord Diagram after removing only 0 values from the matrix.

from pycirclize import Circos
import pandas as pd

# Create 3 x 6 matrix file (Only 0 row-column exist)
row_names = ["F1", "F2", "F3"]
col_names = ["T1", "T2", "T3", "T4", "T5", "T6"]
matrix_data = [
    [0, 0, 0, 0, 0, 0],
    [0, 1, 6, 8, 12, 15],
    [0, 10, 3, 16, 11, 18],
]
matrix_df = pd.DataFrame(matrix_data, index=row_names, columns=col_names)

# Remove only zero column matrix data
matrix_df = matrix_df.loc[:, ~(matrix_df <= 0).all(axis=0)]
# Remove only zero row matrix data
matrix_df = matrix_df.loc[~(matrix_df <= 0).all(axis=1)]

# Plot chord diagram (Use matrix file)
circos = Circos.initialize_from_matrix(matrix_df, space=5, cmap="tab10")
circos.savefig("chord_diagram.png")

chord_diagram.png
chord_diagram

@moshi4 moshi4 closed this as completed Mar 25, 2023
@moshi4 moshi4 added the question Further information is requested label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants