AttributeError for selfloop_edges() - networkx

When executing the following:
import networkx as nx
import matplotlib.pyplot as plt
import csv
with open("nutrients.csv") as file:
reader = csv.reader(file)
G = nx.Graph(reader) #initialize Graph
print(G.nodes()) #this part works fine
print(repr(G.edges))
G.selfloop_edges()#attribute of question
It's coming back with
AttributeError:"Graph" object has no attribute 'selfloop_edge'
Does anyone know what could be the issue?

You getting an error because this method has been moved from the base graph class into the main namespace, see Migration guide from 1.X to 2.0. So either you're looking at the docs of 1.X or using code from previous releases.
You need to call this method as:
nx.selfloop_edges(G, data=True)

Related

How to get clickable links to functions and classes on VS code for Mac

Sorry if the question is not entirely clear. Will add details if unclear.
PrintPlugin is a class from another python file I have imported. I used to get clickable links on this on VS code. Meaning, if I cmd+click on PrintPlugin it would go to that class on the source Python file.
My bottom bar used to look like this when it was working.
Now it looks like this
The difference is that I was having the Python 3.9.5 64-bit ('base':conda) earlier.
Everything else works fine. I am on my virtual environment too. I am not sure what else could be causing this. Will add any details required.
Edit:
I have added some reproducible code. This differs from the original image because the original was work related and I can't post it here. But I'm still running this in the same folder and in the same virtual environment, so it has the same problem (that I cannot cmd+click on Multiply() in process.py and go to the class on plugin.py)
File process.py
import numpy as np
from plugin import Multiply
a = np.array([[2,3],
[3,4]])
b = np.array([[5,6],
[7,8]])
result = Multiply()
print(result.multiply(a,b))
File plugin.py
import numpy as np
class Multiply():
def __init__(
self,
a = np.array([[1,1],
[1,1]]),
b = np.array([[1,1],
[1,1]])
):
return
def multiply(self,a,b):
c = a # b
return c
Output when I do python process.py
>>> [[31 36]
[43 50]]

Python Bokeh FileInput Widget for DLIS file

I am trying a web app in bokeh where i import a dlis file through the widget fileinput.
I am using the following libraries
from dlisio import dlis
from pybase64 import b64decode
import io
def upload_file_dlis(attr, old, new):
print('upload_file_dlis')
decoded = b64decode(new)
f = io.BytesIO(decoded)
file_input_dlis.on_change('value', upload_file_dlis)
I have the following issue:
OSError: '<_io.BytesIO object at 0x000001CE6507CF40>' is not an existing regular file
I guess it is because dlis files are not handled by Io library.
What is the best way to work on this?
Do you have any suggestions?

Obsolete/erroneous code in convolutional_mlp.py at DeepLearningTutorials?

This code contains the following tidbit:
from theano.tensor.nnet import conv2d
...
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
input_shape=image_shape
)
which raises an exception due to 'input_shape' not being found, despite being mentioned in the documentation where it says that:
"image_shape ... – Deprecated alias for input_shape"
Looking at conv.py both locally and in the source I found:
def conv2d(input, filters, image_shape=None, filter_shape=None,
border_mode='valid', subsample=(1, 1), **kargs):
Needless to say, there is no trace of input_shape.
If one modifies the code above as follows
# convolve input feature maps with filters
conv_out = conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape
)
, the exception disappears and the code runs fine.
What am I missing? If image_shape is deprecated, how come it works while input_shape does not?
Is the theano version at the repository obsolete?
PS: I would have liked to ask directly the folks at http://deeplearning.net, but I could not find how.
Are you sure you have the latest version installed?
conv.py contains the deprecated implementation of conv2d. The new implementation can be found in __init__.py
Make sure you are using the import statement
from theano.tensor.nnet import conv2d
and not
from theano.tensor.nnet.conv import conv2d
since the second one is going to import the deprecated implementation

python: Imported class does not change, when edited, saved and reimported

I'm new to Python and trying to understand classes. Not sure the following error is coming from the use of my IDE, which is Spyder, or if it is intended behaviour.
I define a class message in the file C:\mydir\class_def.py. Here is what the file contains:
class message:
def __init__(self,msg1,msg2):
self.msg1 = msg1
self.msg2 = msg2
I have another script were I want to execute code, called execute.py. In this script I import the class and make an instance of the class object. Here is the code from the script execute.py:
import os
os.chdir('C:\mydir')
from class_def import message
message_obj = message('Hello','world')
So far no problems!
Then I edit class_def.py to the following:
class message:
def __init__(self,msg1):
self.msg1 = msg1
and edit execute.py to match the new class, so removing one input tomessage:
import os
os.chdir('C:\mydir')
from class_def import message
message_obj = message('Hello')
and I get the following error:
TypeError: __init__() takes exactly 3 arguments (2 given)
It seems like Python keeps the old version of class_def.py and does not import the new one, even though it is saved.
Is this normal behaviour or is Spyder doing something funny?
If you have a .pyc file such as class_def.pyc, delete it.
I'd remove all .pyc files in your working directory and then try again. If that doesn't work, maybe you're not using the module you think you are? To be certain try something like:
import myModule
print myModule.__file__ #This will give you the path to the .pyc file your program loaded
#or
import myModule
import os
print os.path.dirname(myModule.__file__)
try those out so you can be certain that you're actually using the file you're modifying. Hope that helps!

Import {} from location is not found in VS Code using TypeScript and Angular 2

I am trying out the new Angular 2 Forms. My import statements are as follows:
import {bootstrap, onChange, NgIf, Component, Directive, View, Ancestor} from 'angular2/angular2';
import {formDirectives, NgControl, Validators, NgForm} from 'angular2/forms';
import {RegExpWrapper, print, isPresent} from 'angular2/src/facade/lang';
import {reflector} from 'angular2/src/reflection/reflection';
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
The 'angular2/angular2' resolves fine, but none of the other "from" locations resolve. The error is:
Cannot find module 'angular2/forms'.
All of these components are currently in my node_modules directory. If I put in the full path:
import {formDirectives, ControlDirective, Validators, TemplateDrivenFormDirective} from 'C:/Users/Deb/node_modules/angular2/forms';
then it works. However, I should not need to use the full path. Am I missing something when I set up the tsconfig or is there something else wrong?
Problem was that the example application did not match with the version of Angular 2 currently available for download.
If anyone is interested, I now have a working example of Angular2 forms with TypeScript and Visual Studio Code here:
https://github.com/DeborahK/AngularU2015-Angular2Forms
Hope this helps anyone else standing on the "bleeding edge".