Related
I am using Azure ML Notebook to run both python and R code in notebook cells. I have installed python packages pandas 1.1.5 and rpy2 3.5.4 but when I run the following lines of code:
%load_ext rpy2.ipython
print("ankit")
I get the following error message:
AttributeError: module 'pandas' has no attribute 'Float64Dtype'
Can anyone please help to resolve this error ? Any help would be appreciated.
Complete error is as following:
AttributeError Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 get_ipython().run_line_magic('load_ext', 'rpy2.ipython')
2 print("ankit")
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/interactiveshell.py:2305, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
2303 kwargs['local_ns'] = self.get_local_scope(stack_depth)
2304 with self.builtin_trap:
-> 2305 result = fn(*args, **kwargs)
2306 return result
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/magics/extension.py:33, in ExtensionMagics.load_ext(self, module_str)
31 if not module_str:
32 raise UsageError('Missing module name.')
---> 33 res = self.shell.extension_manager.load_extension(module_str)
35 if res == 'already loaded':
36 print("The %s extension is already loaded. To reload it, use:" % module_str)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/extensions.py:76, in ExtensionManager.load_extension(self, module_str)
69 """Load an IPython extension by its module name.
70
71 Returns the string "already loaded" if the extension is already loaded,
72 "no load function" if the module doesn't have a load_ipython_extension
73 function, or None if it succeeded.
74 """
75 try:
---> 76 return self._load_extension(module_str)
77 except ModuleNotFoundError:
78 if module_str in BUILTINS_EXTS:
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/IPython/core/extensions.py:92, in ExtensionManager._load_extension(self, module_str)
90 if module_str not in sys.modules:
91 with prepended_to_syspath(self.ipython_extension_dir):
---> 92 mod = import_module(module_str)
93 if mod.__file__.startswith(self.ipython_extension_dir):
94 print(("Loading extensions from {dir} is deprecated. "
95 "We recommend managing extensions like any "
96 "other Python packages, in site-packages.").format(
97 dir=compress_user(self.ipython_extension_dir)))
File /anaconda/envs/azureml_py38/lib/python3.8/importlib/__init__.py:127, in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
File <frozen importlib._bootstrap>:1014, in _gcd_import(name, package, level)
File <frozen importlib._bootstrap>:991, in _find_and_load(name, import_)
File <frozen importlib._bootstrap>:975, in _find_and_load_unlocked(name, import_)
File <frozen importlib._bootstrap>:671, in _load_unlocked(spec)
File <frozen importlib._bootstrap_external>:783, in exec_module(self, module)
File <frozen importlib._bootstrap>:219, in _call_with_frames_removed(f, *args, **kwds)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/__init__.py:1, in <module>
----> 1 from . import rmagic
3 load_ipython_extension = rmagic.load_ipython_extension
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/rmagic.py:121, in <module>
116 def _get_converter(template_converter=template_converter):
117 return Converter('ipython conversion',
118 template=template_converter)
--> 121 ipy_template_converter = _get_ipython_template_converter(template_converter,
122 numpy=numpy,
123 pandas=pandas)
124 converter = _get_converter(template_converter=ipy_template_converter)
127 def CELL_DISPLAY_DEFAULT(res, args):
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/ipython/rmagic.py:111, in _get_ipython_template_converter(template_converter, numpy, pandas)
109 template_converter += numpy2ri.converter
110 if pandas:
--> 111 from rpy2.robjects import pandas2ri
112 template_converter += pandas2ri.converter
113 return template_converter
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/rpy2/robjects/pandas2ri.py:144, in <module>
125 v = math.nan
126 set_elt(r_vector, i, cast_value(v))
129 _PANDASTYPE2RPY2 = {
130 datetime.date: DateVector,
131 int: functools.partial(
132 IntVector.from_iterable,
133 populate_func=_int_populate_r_vector
134 ),
135 pandas.BooleanDtype: functools.partial(
136 BoolVector.from_iterable,
137 populate_func=_bool_populate_r_vector
138 ),
139 None: BoolVector,
140 str: functools.partial(
141 StrVector.from_iterable,
142 populate_func=_str_populate_r_vector
143 ),
--> 144 pandas.Float64Dtype: functools.partial(
145 FloatVector.from_iterable,
146 populate_func=_float_populate_r_vector
147 ),
148 bytes: (numpy2ri.converter.py2rpy.registry[
149 numpy.ndarray
150 ])
151 }
154 #py2rpy.register(pandas.core.series.Series)
155 def py2rpy_pandasseries(obj):
156 if obj.dtype.name == 'O':
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/pandas/__init__.py:258, in __getattr__(name)
254 from pandas.core.arrays.sparse import SparseArray as _SparseArray
256 return _SparseArray
--> 258 raise AttributeError(f"module 'pandas' has no attribute '{name}'")
AttributeError: module 'pandas' has no attribute 'Float64Dtype'
I replicate your situation on my system and it works.
You can use this set of library versions:
R version: 4.2.1
rpy2 version: 3.4.5
Pandas version: 1.3.5
ValueError Traceback (most recent call
last) /tmp/ipykernel_2113989/1063976035.py in
11
12 # we should specify shape of the input tensor
---> 13 k_model = pytorch_to_keras(model, input_var, [(3, 224, 224,)], verbose=True)
14 #k_model = pytorch_to_keras(model, input_var, [(3, None, None,)], verbose=True)
15
~/anaconda3/envs/torch/lib/python3.7/site-packages/pytorch2keras/converter.py
in pytorch_to_keras(model, args, input_shapes, change_ordering,
verbose, name_policy, use_optimizer, do_constant_folding)
82 k_model = onnx_to_keras(onnx_model=onnx_model, input_names=input_names,
83 input_shapes=input_shapes, name_policy=name_policy,
---> 84 verbose=verbose, change_ordering=change_ordering)
85
86 return k_model
~/anaconda3/envs/torch/lib/python3.7/site-packages/onnx2keras/converter.py
in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy,
verbose, change_ordering)
179 lambda_funcs,
180 node_name,
--> 181 keras_names
182 )
183 if isinstance(keras_names, list):
~/anaconda3/envs/torch/lib/python3.7/site-packages/onnx2keras/operation_layers.py
in convert_clip(node, params, layers, lambda_func, node_name,
keras_name)
39 lambda_func[keras_name] = target_layer
40
---> 41 layers[node_name] = layer(input_0)
42
43
~/anaconda3/envs/torch/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py
in call(self, inputs, *args, **kwargs)
819 self.name)
820 graph = backend.get_graph()
--> 821 with graph.as_default(), backend.name_scope(self._name_scope()):
822 # Build layer if applicable (if the build method has been
823 # overridden).
~/anaconda3/envs/torch/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py
in enter(self) 6442 else: 6443 scope =
get_default_graph().name_scope(self._name)
-> 6444 scope_name = scope.enter() 6445 self._exit_fns.append(scope.exit) 6446 return scope_name
~/anaconda3/envs/torch/lib/python3.7/contextlib.py in enter(self)
110 del self.args, self.kwds, self.func
111 try:
--> 112 return next(self.gen)
113 except StopIteration:
114 raise RuntimeError("generator didn't yield") from None
~/anaconda3/envs/torch/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py
in name_scope(self, name) 4127 # op name regex, which
constrains the initial character. 4128 if not
_VALID_OP_NAME_REGEX.match(name):
-> 4129 raise ValueError("'%s' is not a valid scope name" % name) 4130 old_stack = self._name_stack 4131 if not
name: # Both for name=None and name="" we re-set to empty scope.
ValueError: 'onnx::Conv_369' is not a valid scope name
I am trying to convert pytorch pretrained model to keras via pytorch2keras (https://github.com/gmalivenko/pytorch2keras).
I had tried to install older version of Onnx (1.8.1) and tried to install Onnx from main branch (https://github.com/onnx/onnx).
However, I still face the error. May I know how to solve it?
Try setting name_policy='renumerate' or name_policy='short' for onnx_2_keras.
Something is wrong with my pandas module. I tried to read in an excel file using the following code, which works on my classmate's computer, but it's giving me an error on my computer:
FFT1=pd.read_excel('FFT1.xlsx', sheet_name='sheet1')
The file named 'FFT1.xlsx' is in the same directory as my jupyter notebook. The error message says:
XLRDError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_7436/2793485739.py in <module>
----> 1 FFT1=pd.read_excel('FFT1.xlsx', sheet_name='sheet1')
D:\Softwares\Anaconda\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
302
303 if not isinstance(io, ExcelFile):
--> 304 io = ExcelFile(io, engine=engine)
305 elif engine and engine != io.engine:
306 raise ValueError(
D:\Softwares\Anaconda\lib\site-packages\pandas\io\excel\_base.py in __init__(self, io, engine)
819 self._io = stringify_path(io)
820
--> 821 self._reader = self._engines[engine](self._io)
822
823 def __fspath__(self):
D:\Softwares\Anaconda\lib\site-packages\pandas\io\excel\_xlrd.py in __init__(self, filepath_or_buffer)
19 err_msg = "Install xlrd >= 1.0.0 for Excel support"
20 import_optional_dependency("xlrd", extra=err_msg)
---> 21 super().__init__(filepath_or_buffer)
22
23 #property
D:\Softwares\Anaconda\lib\site-packages\pandas\io\excel\_base.py in __init__(self, filepath_or_buffer)
351 self.book = self.load_workbook(filepath_or_buffer)
352 elif isinstance(filepath_or_buffer, str):
--> 353 self.book = self.load_workbook(filepath_or_buffer)
354 elif isinstance(filepath_or_buffer, bytes):
355 self.book = self.load_workbook(BytesIO(filepath_or_buffer))
D:\Softwares\Anaconda\lib\site-packages\pandas\io\excel\_xlrd.py in load_workbook(self, filepath_or_buffer)
34 return open_workbook(file_contents=data)
35 else:
---> 36 return open_workbook(filepath_or_buffer)
37
38 #property
D:\Softwares\Anaconda\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows, ignore_workbook_corruption)
168 # files that xlrd can parse don't start with the expected signature.
169 if file_format and file_format != 'xls':
--> 170 raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
171
172 bk = open_workbook_xls(
XLRDError: Excel xlsx file; not supported
How should I fix this?
Make sure that you already install openpyxl, if you don't try
pip install openpyxl
Change your code to
FFT1=pd.read_excel('FFT1.xlsx', sheet_name='sheet1',engine='openpyxl')
I'm trying to run a Jupyter Notebook that uses "Pool" From the Multiprocessing package and getting the "[Errno 13] Permission denied" error
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<timed exec> in <module>
<ipython-input-9-9d32e1938b9e> in get_ray_tracing(rays, population_element)
7 # Necessario para o windows
8 if __name__ == '__main__':
----> 9 p=Pool(processes = len(rays))
10 # O jupyter notebook necessita que a funcao esteja em um arquivo diference
11 # O programa ficara travado nessa linha ate que todos os processos terminem
/snap/jupyter/6/lib/python3.7/multiprocessing/context.py in Pool(self, processes, initializer, initargs, maxtasksperchild)
117 from .pool import Pool
118 return Pool(processes, initializer, initargs, maxtasksperchild,
--> 119 context=self.get_context())
120
121 def RawValue(self, typecode_or_type, *args):
/snap/jupyter/6/lib/python3.7/multiprocessing/pool.py in __init__(self, processes, initializer, initargs, maxtasksperchild, context)
156 maxtasksperchild=None, context=None):
157 self._ctx = context or get_context()
--> 158 self._setup_queues()
159 self._taskqueue = queue.SimpleQueue()
160 self._cache = {}
/snap/jupyter/6/lib/python3.7/multiprocessing/pool.py in _setup_queues(self)
249
250 def _setup_queues(self):
--> 251 self._inqueue = self._ctx.SimpleQueue()
252 self._outqueue = self._ctx.SimpleQueue()
253 self._quick_put = self._inqueue._writer.send
/snap/jupyter/6/lib/python3.7/multiprocessing/context.py in SimpleQueue(self)
110 '''Returns a queue object'''
111 from .queues import SimpleQueue
--> 112 return SimpleQueue(ctx=self.get_context())
113
114 def Pool(self, processes=None, initializer=None, initargs=(),
/snap/jupyter/6/lib/python3.7/multiprocessing/queues.py in __init__(self, ctx)
330 def __init__(self, *, ctx):
331 self._reader, self._writer = connection.Pipe(duplex=False)
--> 332 self._rlock = ctx.Lock()
333 self._poll = self._reader.poll
334 if sys.platform == 'win32':
/snap/jupyter/6/lib/python3.7/multiprocessing/context.py in Lock(self)
65 '''Returns a non-recursive lock object'''
66 from .synchronize import Lock
---> 67 return Lock(ctx=self.get_context())
68
69 def RLock(self):
/snap/jupyter/6/lib/python3.7/multiprocessing/synchronize.py in __init__(self, ctx)
160
161 def __init__(self, *, ctx):
--> 162 SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)
163
164 def __repr__(self):
/snap/jupyter/6/lib/python3.7/multiprocessing/synchronize.py in __init__(self, kind, value, maxvalue, ctx)
57 sl = self._semlock = _multiprocessing.SemLock(
58 kind, value, maxvalue, self._make_name(),
---> 59 unlink_now)
60 except FileExistsError:
61 pass
PermissionError: [Errno 13] Permission denied
I tried, without success, to apply the suggestions of the question Python multiprocessing: Permission denied - Stack Overflow
What else could i try?
I'm trying to do a choropleth using folium which offers a great link between GeoJSON, Pandas and leaflet.
GeoJSON format is like below :
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":
{
"type":"Polygon",
"coordinates":[[[-1.6704591323124895,49.62681486270549], .....
{
"insee":"50173",
"nom":"Équeurdreville-Hainneville",
"wikipedia":"fr:Équeurdreville-Hainneville",
"surf_m2":12940306}},
Pandas DataFrame :
postal_count.head(5)
Out[98]:
Code_commune_INSEE CP_count
0 75120 723
1 75115 698
2 75112 671
3 75118 627
4 75111 622
"Code_communes_INSEE" corresponds to the attribute "insee" in the GeoJSON. I'd like to do a choropleth using the variable "CP_count" in the above DataFrame.
Here is my code (snippet from this notebook)
map_france = folium.Map(location=[47.000000, 2.000000], zoom_start=6)
map_france.choropleth(
geo_str=open(geo_path + 'simplified_communes100m.json').read(),
data=postal_count,
columns=['Code_commune_INSEE', 'CP_count'],
key_on='feature.geometry.properties.insee',
fill_color='YlGn',
)
map_france.save(table_path + 'choro_test1.html')
I'm still getting this error again and again :
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-83-ea0fd2c1c207> in <module>()
8 fill_color='YlGn',
9 )
---> 10 map_france.save('/media/flo/Stockage/Data/MesAides/map/choro_test1.html')
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in save(self, outfile, close_file, **kwargs)
151
152 root = self.get_root()
--> 153 html = root.render(**kwargs)
154 fid.write(html.encode('utf8'))
155 if close_file:
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
357 """Renders the HTML representation of the element."""
358 for name, child in self._children.items():
--> 359 child.render(**kwargs)
360 return self._template.render(this=self, kwargs=kwargs)
361
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
665
666 for name, element in self._children.items():
--> 667 element.render(**kwargs)
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/element.py in render(self, **kwargs)
661 script = self._template.module.__dict__.get('script', None)
662 if script is not None:
--> 663 figure.script.add_children(Element(script(self, kwargs)),
664 name=self.get_name())
665
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/jinja2/runtime.py in __call__(self, *args, **kwargs)
434 raise TypeError('macro %r takes not more than %d argument(s)' %
435 (self.name, len(self.arguments)))
--> 436 return self._func(*arguments)
437
438 def __repr__(self):
<template> in macro(l_this, l_kwargs)
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/jinja2/runtime.py in call(_Context__self, _Context__obj, *args, **kwargs)
194 args = (__self.environment,) + args
195 try:
--> 196 return __obj(*args, **kwargs)
197 except StopIteration:
198 return __self.environment.undefined('value was undefined because '
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/features.py in style_data(self)
352
353 for feature in self.data['features']:
--> 354 feature.setdefault('properties', {}).setdefault('style', {}).update(self.style_function(feature)) # noqa
355 return json.dumps(self.data, sort_keys=True)
356
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in style_function(x)
671 "color": line_color,
672 "fillOpacity": fill_opacity,
--> 673 "fillColor": color_scale_fun(x)
674 }
675
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in color_scale_fun(x)
659 def color_scale_fun(x):
660 return color_range[len(
--> 661 [u for u in color_domain if
662 u <= color_data[get_by_key(x, key_on)]])]
663 else:
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in <listcomp>(.0)
660 return color_range[len(
661 [u for u in color_domain if
--> 662 u <= color_data[get_by_key(x, key_on)]])]
663 else:
664 def color_scale_fun(x):
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
655 return (obj.get(key, None) if len(key.split('.')) <= 1 else
656 get_by_key(obj.get(key.split('.')[0], None),
--> 657 '.'.join(key.split('.')[1:])))
658
659 def color_scale_fun(x):
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
655 return (obj.get(key, None) if len(key.split('.')) <= 1 else
656 get_by_key(obj.get(key.split('.')[0], None),
--> 657 '.'.join(key.split('.')[1:])))
658
659 def color_scale_fun(x):
/home/flo/.virtualenvs/mesaides/lib/python3.4/site-packages/folium/folium.py in get_by_key(obj, key)
653
654 def get_by_key(obj, key):
--> 655 return (obj.get(key, None) if len(key.split('.')) <= 1 else
656 get_by_key(obj.get(key.split('.')[0], None),
657 '.'.join(key.split('.')[1:])))
AttributeError: 'NoneType' object has no attribute 'get'
I tried playing with key_on='feature.geometry.properties.insee' without any success.
There were 2 problems :
1 - The correct access to 'insee' parameters is : key_on='feature.properties.insee'
The best way to find the right key_on is to play with the geoJSON dict to make sure you are calling the right properties.
2- Once you have the right key_on parameters, you need to make sure that all the available keys in the geoJSON are contained in your Pandas DataFrame (otherwise it will raise a KeyError)
In this case, I used the following command line to get all the insee keys contained by my geoJSON:
ogrinfo -ro -al communes-20150101-100m.shp -geom=NO | grep insee > list_code_insee.txt
If you are experiencing the same issue, this should solve your problem.
I had the same problem on JupyterLab (on labs.cognitiveclass.ai) using Folium 0.5.0. Then I copied my code and ran it in PyCharm, and it worked! I don't understand why, perhaps there is some backend issue (?)
If you want to display a folium map outside of a Jupyter notebook, you have to save the map to html:
map_france.save('map_france.html')
and open the html in your browser.