Import Error calling Python function from Matlab

1 view (last 30 days)
I need to call Modularity-Density Measure which is a Python function from Matlab to enable me determine the quality of network communities detected by an algorithm I designed. I tried so much without success to call the python function but each time, I end up with error messages even when I follow the examples on Mathworks's Python import and Matlab import. Let me state the examples and then show what I did.
import py.textwrap.wrap
S = wrap('This is a string');
The code above works fine but when I do same thing I get error message.
import py.modularitydensity.fine_tuned_modularity_density
%Error using import: Import argument 'py.modularitydensity.fine_tuned_modularity_density' cannot be found or cannot be imported.
I then tried the 'py.importlib.import_module' command.
py.importlib.import_module('modularitydensity.fine_tuned_modularity_density', 'modularity_density')
It returned the following results:
%Python module with properties:
%fiedler_vector: [1×1 py.function]
%np: [1×1 py.module]
%fine_tuned_clustering_qds: [1×1 py.function]
%merge_communities_qds: [1×1 py.function]
%split_communities_qds: [1×1 py.function]
%modularity_density: [1×1 py.function]
%not_implemented_for: [1×1 py.function]
%nx: [1×1 py.module]
Then I tried to implement the target function 'modularity_density' but I end up with error messages.
py.modularity_density(A)
%Undefined variable "py" or class "py.modularity_density".
I tried these other commands:
!E:\python\python.exe modularity_density.py A
%E:\python\python.exe: can't open file 'modularity_density.py': [Errno 2] No such file or directory
!E:\python\python.exe fine_tuned_modularity_density.py A
%Traceback (most recent call last):
%File "fine_tuned_modularity_density.py", line 17, in <module>
%from metrics import modularity_density
%File "E:\matlab\bin\from github\similarity\modularity-density-master\modularity-density-master\src\modularitydensity\metrics.py", line 10, in <module>
%from scipy.sparse import identity
%ImportError: No module named scipy.sparse
None of the commands give results and I don't understand what I have been doing wrong. Any advice and assistance to help me get this code working is highly appreciated.
Thanks.
link to the modularity density Python package: Modularity_Density Python Package

Answers (1)

Luis J Gilarranz
Luis J Gilarranz on 6 Apr 2020

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!