Using rpy2 with streamlit - python

I am trying to build an app by using python and rpy2. I read a .csv file (see below, table_mean_plain) and I would like to plot an histogram of its percentage of explained variances. When I use the code in Jupyter notebook it is working fine. But when I try through streamlit the code is loading forever and I receive the message:
2022-10-25 17:08:05.231 Uncaught app exception
Traceback (most recent call last):
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 562, in _run_script
exec(code, module.__dict__)
File "/Users/XX/Documents/XX/pages/4_Visualisations.py", line 69, in <module>
res_pca = FactoMineR.PCA(my_data)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/robjects/functions.py", line 201, in __call__
return (super(SignatureTranslatedFunction, self)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/robjects/functions.py", line 124, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/rinterface_lib/conversion.py", line 45, in _
cdata = function(*args, **kwargs)
File "/opt/anaconda3/envs/XX/lib/python3.9/site-packages/rpy2/rinterface.py", line 810, in __call__
raise embedded.RRuntimeError(_rinterface._geterrmessage())
rpy2.rinterface_lib.embedded.RRuntimeError: Erreur dans (function (title, width, height, pointsize, family, antialias, :
impossible de créer le périphérique cible quartz(), le type fourni n'est peut-être pas supporté
Assertion failed: (NSViewIsCurrentlyBuildingLayerTreeForDisplay() != currentlyBuildingLayerTree), function NSViewSetCurrentlyBuildingLayerTreeForDisplay, file NSView.m, line 13477.
[9] 59974 illegal hardware instruction streamlit run 1_Workbench.py
Here is my code
from rpy2.robjects.packages import importr, data
from rpy2.robjects.vectors import DataFrame
from rpy2.ipython.ggplot import image_png
import streamlit as st
import matplotlib.pyplot as plt
from rpy2 import robjects
import matplotlib.image as mpimg
utils = importr('utils')
corrplot = importr('corrplot')
FactoMineR = importr('FactoMineR')
factoextra = importr('factoextra')
my_datafile = 'table/plain/table_mean_plain.csv'
#my_data = utils.read_csv(my_datafile)
my_data = DataFrame.from_csvfile(my_datafile, row_names="X")
#rownames(my_data = my_data$X
#my_dataX = NULL
# Do PCA
# --------
res_pca = FactoMineR.PCA(my_data)
# Eigen values / Variance
# --------
eig_val = factoextra.get_eigenvalue(res_pca)
st.pyplot(display(image_png(factoextra.fviz_eig(res_pca, addlabels = True))))
The corresponding .csv file is
,logistic-regression,svm-linear,svm-rbf,Gnb,decision-tree,random-forest,XGBoost,MLP,Ensemble,GEV,iForest,DevNet
news,0.827,0.566,0.724,0.681,0.687,0.852,0.865,0.859,0.866,0.87,0.497,0.666
telE,0.761,0.552,0.623,0.756,0.86,0.944,0.957,0.915,0.95,0.941,0.399,0.717
bank,0.832,0.62,0.772,0.817,0.687,0.848,0.841,0.848,0.855,0.873,0.625,0.793
member,0.648,0.488,0.514,0.625,0.574,0.696,0.695,0.669,0.707,0.667,0.545,0.598
dsn,0.735,0.708,0.785,0.707,0.761,0.884,0.897,0.754,0.884,0.766,0.568,0.661
mobile,0.889,0.432,0.393,0.842,0.776,0.9,0.906,0.9,0.908,0.907,0.7,0.852
campaign,0.907,0.543,0.668,0.809,0.709,0.928,0.933,0.923,0.935,0.924,0.65,0.832
HR,0.85,0.766,0.841,0.762,0.615,0.809,0.797,0.825,0.838,0.827,0.589,0.72
sato,0.794,0.747,0.8,0.729,0.651,0.808,0.809,0.799,0.825,0.82,0.5,0.739
uci,0.854,0.586,0.897,0.859,0.818,0.901,0.905,0.847,0.913,0.91,0.663,0.793
TelC,0.844,0.632,0.797,0.814,0.658,0.819,0.825,0.842,0.841,0.85,0.295,0.784
median_AUC,0.832,0.586,0.772,0.762,0.687,0.852,0.866,0.847,0.866,0.87,0.568,0.739
Rank,5.67,10.67,7.5,8.08,9.0,4.0,3.25,5.0,1.83,2.67,11.42,8.92

Related

Python "checknull() takes no keyword arguments" error

I have tried to research this error and I cannot seem to find an answer. I've never seen this and this code worked fine last month and now I receive this error with the same code. Please advise and thank you. I've included my code and the error message below. The program is designed to read data from a URL and ingest that data into a new dataframe. I do not understand the meaning of "checknull()".
import os
os.environ["PROJ_LIB"] = 'C:\\Users\\Yury\\anaconda3\\Library\\share'
from sys import exit
import netCDF4 as nc4
from netCDF4 import Dataset
import numpy as np
import matplotlib as m
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap, cm
import datetime
from datetime import datetime
import pandas as pd
import xarray as xr
import cartopy.crs as ccrs
import math
#import cdstoolbox as ct
import bottleneck as bn
from mpl_toolkits.basemap import Basemap
import cdsapi
from matplotlib.pyplot import figure
import cartopy.feature as cfeature
import time
import calendar
# -----------------------------------------------------------------------------------------------------------
#
# -----------------------------------------------------------------------------------------------------------
#setx ECCODES_DEFINITION_PATH "C:\\Users\\U321103\\Anaconda3\\envs\\Maps2\\Library\\share\\eccodes\\definitions"
# copy setx... in command prompt in C:\\Users\\U321103
#!/usr/bin/env python3
c = cdsapi.Client()
url = c.retrieve(
'reanalysis-era5-single-levels-monthly-means',
{
'product_type': 'monthly_averaged_reanalysis',
'format': 'grib',
'variable': ['Mean sea level pressure'],
'year': ['1992','1993','1994','1995','1996','1997','1998','1999','2000','2001','2002','2003','2004','2005','2006',
'2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021','2022'],
'month': ['01','02','03','04','05','06','07','08','09','10','11','12'],
# 'month': '12',
# 'day': '01',
'time': ['00:00'],
'grid': [0.25, 0.25],
# GRID = 0.5 TO MATCH VORTEX ?
'area': [65.00, -140.00, 15.00, -53.00],
},
"C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure")
path = "C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure"
ds = xr.load_dataset(path, engine='cfgrib')
exit()
And the error --- >
runfile('//porfiler03.ar.local/gtdshare/GOALS_2022/WeatherTypes/ERA5_Get_MSLP_USA_WORKING_MONTHLY.py', wdir='//porfiler03.ar.local/gtdshare/GOALS_2022/WeatherTypes')
2023-01-16 18:09:51,830 INFO Welcome to the CDS
2023-01-16 18:09:51,831 INFO Sending request to https://cds.climate.copernicus.eu/api/v2/resources/reanalysis-era5-single-levels-monthly-means
2023-01-16 18:09:52,149 INFO Request is completed
2023-01-16 18:09:52,150 INFO Downloading https://download-0001-clone.copernicus-climate.eu/cache-compute-0001/cache/data0/adaptor.mars.internal-1673912965.1298344-26515-5-eb9a349c-be6c-4e0c-b3fb-79e0d436a411.grib to C:\Users\U321103\.spyder-py3\ERA5_MAPPING\mean_sea_level_pressure (49.9M)
2023-01-16 18:09:59,400 INFO Download rate 6.9M/s
2023-01-16 18:09:59,401 WARNING Ignoring index file 'C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\mean_sea_level_pressure.923a8.idx' older than GRIB file
Traceback (most recent call last):
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 523, in decode_cf_variables
new_vars[k] = decode_cf_variable(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 364, in decode_cf_variable
var = coder.decode(var, name=name)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\coding\variables.py", line 189, in decode
encoded_fill_values = {
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\coding\variables.py", line 193, in <setcomp>
if not pd.isnull(fv)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\pandas\core\dtypes\missing.py", line 185, in isna
return _isna(obj)
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\pandas\core\dtypes\missing.py", line 208, in _isna
return libmissing.checknull(obj, inf_as_na=inf_as_na)
TypeError: checknull() takes no keyword arguments
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "\\porfiler03.ar.local\gtdshare\goals_2022\weathertypes\era5_get_mslp_usa_working_monthly.py", line 58, in <module>
ds = xr.load_dataset(path, engine='cfgrib')
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 258, in load_dataset
object.
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 545, in open_dataset
ds = _dataset_from_backend_dataset(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\backends\api.py", line 451, in maybe_decode_store
specified). If None (default), attempt to decode times to
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 659, in decode_cf
vars, attrs, coord_names = decode_cf_variables(
File "C:\Users\U321103\Anaconda3\envs\Maps2\lib\site-packages\xarray\conventions.py", line 534, in decode_cf_variables
raise type(e)(f"Failed to decode variable {k!r}: {e}")
TypeError: Failed to decode variable 'number': checknull() takes no keyword arguments

Error in .exe of python script having Barcode from Reportlab.graphics

I have a python script for generating barcodes (code128) which works fine in IDE.
But when converted into exe, it encounters below error:
Traceback (most recent call last):
File "amazonbarcode-dubai.py", line 6, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "reportlab\graphics\barcode\__init__.py", line 76, in <module>
File "reportlab\graphics\barcode\__init__.py", line 42, in _reset
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "reportlab\graphics\barcode\widgets.py", line 166, in <module>
File "reportlab\graphics\barcode\widgets.py", line 95, in _BCW
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'reportlab.graphics.barcode.code128'
I tried import code128 from reportlab.graphics.barcode that also works fine with IDE but fails in exe.
Below is my python script:
import os
import pandas as pd
from reportlab.pdfgen import canvas
from reportlab.graphics import barcode
from reportlab.lib.units import mm
cwd = os.getcwd()
df1 = pd.read_csv(cwd+'\\exportedTable.csv')
df2 = pd.read_excel(cwd+'\\AMAZON BARCODE.xlsx',dtype=str)
df3 = pd.merge(df1,df2,left_on='ASIN',right_on='amazonasin',how='left')
df_missing = df3[df3.item.isnull()]
if df_missing.size>0:
print(df_missing.to_string())
print(df_missing.columns)
df_missing = df_missing[['Model number','UPC/EAN/ISBN','ASIN']]
df_missing.to_excel(cwd+'\\amazonmissingbarcode.xlsx',index=False)
df3 = df3.sort_values('model').reset_index()
df3['ctn'] = df3.index+1
df3 = df3[['ctn','model','barcode','amazonasin','Package tracking ID','PO number']]
df3.columns = ['ctn','model','ean','asin','accn','pono']
print(df3.to_string())
f = df3.pono.unique()[0]
cb = canvas.Canvas(f+'-big.pdf')
cb.setPageSize((100*mm,75*mm))
barcode = barcode.code128.Code128
for i,row in df3.iterrows():
barcode(row['asin'],barHeight=20*mm,barWidth=0.5*mm).drawOn(cb,9*mm,53*mm)
cb.setFont("Helvetica",10)
cb.drawCentredString(54*mm,50*mm,'ASIN : ' + row['asin'])
barcode(row['pono'],barHeight=15*mm,barWidth=0.5*mm).drawOn(cb,15*mm,32*mm)
cb.setFont("Helvetica",10)
cb.drawCentredString(54*mm,29*mm,'PO NO. : '+row['pono'])
barcode(row['accn'],barHeight=15*mm,barWidth=0.4*mm).drawOn(cb,7*mm,12*mm)
cb.setFont("Helvetica",10)
cb.drawCentredString(54*mm,9*mm,row['accn'])
cb.setFont("Helvetica-Bold",12)
cb.drawString(5*mm,4*mm,'MODEL: '+row['model'])
cb.drawRightString(95*mm,4*mm,'CTN NO.: '+str(row['ctn']))
cb.showPage()
cb.save()

How to name a figure following its histogram name in matplotlib pyplot using savefig

I use the script
import matplotlib.pyplot as plt
import numpy as np
import yoda
def readScatter2D(histname, filename):
histos = yoda.core.read(filename+".yoda")
x = []
y = []
h = histos[histname]
for b in h:
x.append(b.x())
y.append(b.y())
plt.plot([x],[y], 'bo')
plt.savefig(str(histname)+".pdf")
readScatter2D("totalCh", "properties_file")
Compiling this with python3 gives me the error:
Traceback (most recent call last):
File "enhancement.py", line 18, in <module>
readScatter2D("totalCh", "properties_file")
File "enhancement.py", line 16, in readScatter2D
plt.savefig(str(histname)+".pdf")
File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 697, in savefig
res = fig.savefig(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1573, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 2252, in print_figure
**kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2519, in print_pdf
file = PdfFile(filename)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 422, in __init__
fh = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: 'totalCh.pdf'
I do not understand why matplotlib cannot name the file while saving, by following the histogram names. What is the solution to save the figure following the original histogram? I am using matplotlib version 2.0.2

Start various dynamically processes in Python

I'd like to generate a dynamically threads or processes in Python to consume each own queue.
My code: main.py
import cv2
import numpy as np
from classes import roi_process
import time
import os
import copy
import queue
import multiprocessing
roi_list = eval("[(0,0,639,720,1),(640,0,1280,720,2)]")
for _ROI in roi_list:
print("################# " + str(_ROI[4]))
vars()["FILA_"+str(_ROI[4])] = queue.Queue(maxsize=4)
vars()["T_"+str(_ROI[4])] = multiprocessing.Process(target = roi_process.RoiProcess, args = ( eval("FILA_"+str(_ROI[4])) , str(_ROI[4])), daemon=True)
for _ROI in roi_list:
eval("T_"+str(_ROI[4])).start()
classes/roi_process.py
import cv2
import queue
import numpy as np
import imutils
import time
import os
class RoiProcess:
def __init__(self, queue_pool = None, id_roi = 0):
self.id_roi = id_roi
self.queue_pool = queue_pool
print("Iniciou em thread o id: " + self.id_roi)
self.run()
def run(self):
i = 0
while True:
print(str(self.id_roi) + ": " + str(i))
i = i + 1
time.sleep(1)
This is generating the following error:
(tensorflow) C:\projects\car detector\semparar\AI_CARANDPLATE>python main.py
################# 1
################# 2
Traceback (most recent call last):
File "main.py", line 64, in <module>
eval("T_"+str(_ROI[4])).start()
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\context.py", line 326, in _Popen
return Popen(process_obj)
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
reduction.dump(process_obj, to_child)
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.lock' object
(tensorflow) C:\projects\car detector\semparar\AI_CARANDPLATE>Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\spawn.py", line 107, in spawn_main
new_handle = reduction.duplicate(pipe_handle,
File "C:\Users\MasterRoot\Anaconda3\envs\tensorflow\lib\multiprocessing\reduction.py", line 79, in duplicate
return _winapi.DuplicateHandle(
PermissionError: [WinError 5] Acesso negado
I really need to start dynamic threads or processes to consume every each poll that will be fed by a while True: in main.py
I will make a opencv frame reader and slice the main frame into many pieces.
After that I will feed a dynamic queue object with this information and each thread will process a predictor as I need.
I changed my code to:
FILA={}
T={}
#cria fila para cada ROI e instancia uma thread de obj para ler esta fila continuamente.
for _ROI in roi_list:
print("################# " + str(_ROI[4]))
FILA[_ROI[4]] = queue.Queue(maxsize=4)
T[_ROI[4]] = multiprocessing.Process(target = roi_process.RoiProcess, args = ( FILA[_ROI[4]] , str(_ROI[4])), daemon=True).start()
but its keepeing going to do the same error for threads.
Don't use standard Queue with multiprocessing, use:
from multiprocessing import Queue

Key error & Pandas

I wrote a Python script (below) which load data from a text file (using pandas) and checks the values in the columns.
import sys
import pandas as pd
import numpy as np
from numpy import ndarray
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from skimage import data
from skimage.feature import match_template
if __name__ == '__main__':
data = pd.read_csv('Fe_PSI_spt_refined.txt', sep=" ", header = None)
data.columns = ["Angle_number", "Omega", "Intensity", "X", "Y", "Address", "ID"]#, "flag"]
Number_of_projections = 181
Number_of_lines_in_txt = 3493
numrows = len(data)
counter_array = []
correlation_threshold_value = 0.7
a = np.zeros(Number_of_lines_in_txt)
output_file = ("output.txt")
for i in range(2, (Number_of_projections + 1)):
filename_cutouts_combined = ("cutouts_combined_%03i.txt" % (i))
filename_cutouts_combined_tag = ("cutouts_combined_tag_%03i.txt" % (i))
image = np.loadtxt(filename_cutouts_combined)
image_tagged = np.loadtxt(filename_cutouts_combined_tag)
for j in range(0, Number_of_lines_in_txt - 1):
print data.Angle_number[j], i
After one iteration of j I get the error below. Do you spot any error I should fix? Thanks
`Traceback (most recent call last):
File "Hyperbola_search.py", line 46, in <module>
print data.Angle_number[j], i
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 491, in __getitem__
result = self.index.get_value(self, key)
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1032, in get_value
return self._engine.get_value(s, k)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2661)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2476)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3215)
File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6450)
File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6394)
KeyError: 3491`
You load files into image and image_tagged, while a remains unused.
I don't know what data.Angle_number and numrows are, but they appear to be from libraries, not related to your files.

Categories

Resources