Skip to content

Commit fdb3ce1

Browse files
author
Anton Eriksson
committed
feat(python): Add write method
1 parent 2b9e5ca commit fdb3ce1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

interfaces/python/infomap.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import namedtuple
22
from contextlib import contextmanager
3+
import os
34

45
try:
56
from math import log2
@@ -1879,6 +1880,29 @@ def meta_entropy(self):
18791880
# Write Results
18801881
# ----------------------------------------
18811882

1883+
def write(self, filename, *args, **kwargs):
1884+
"""Write results to file.
1885+
1886+
Raises
1887+
------
1888+
NotImplementedError
1889+
If the file format is not supported.
1890+
1891+
Parameters
1892+
----------
1893+
filename : str
1894+
The filename.
1895+
"""
1896+
_, ext = os.path.splitext(filename)
1897+
1898+
# remove the dot
1899+
writer = "write_{}".format(ext[1:])
1900+
1901+
if hasattr(self, writer):
1902+
return getattr(self, writer)(filename, *args, **kwargs)
1903+
1904+
raise NotImplementedError("No method found for writing {} files".format(ext))
1905+
18821906
def write_clu(self, filename, states=False, depth_level=1):
18831907
"""Write result to a clu file.
18841908
@@ -1977,6 +2001,10 @@ def write_csv(self, filename, states=False):
19772001
"""
19782002
return self.writeCsvTree(filename, states)
19792003

2004+
# for the method "write"
2005+
write_ftree = write_flow_tree
2006+
write_nwk = write_newick
2007+
19802008

19812009
def main():
19822010
import sys

0 commit comments

Comments
 (0)