KeyError: 'latitude' - csv

I am trying to create a streamlit app to map coordinates from a csv with the ability to zoom to each point where a popup message appears with the attributes of that specific point. However I am getting this KeyError: latitude message even though I have no gaps in my latitude columns thus I am a bit perplexed how to solve this.
import folium
from folium import Map
import pandas as pd
import streamlit as st
def display_map(file):
data = pd.read_csv(file)
m = Map(location=[data['latitude'].mean(), data['longitude'].mean()], zoom_start=13)
for i, row in data.itterrows():
folium.Marker(
location=[row['latitude'], row['longitude']],
popup=row['APPLICATION '],
icon=folium.Icon(color='red')
).add_to(m)
st.write(m)
def main():
file = st.file_uploader("Upload a CSV File", type=["csv"])
if file is not None:
display_map(file)
if __name__ == '__main__':
main()

Related

How to download a trained model as a pickle file using Streamlit Download Button?

How do I download a trained model as a pickle file using Streamlit Download Button?
You can use io.BytesIO to store the pickled data inside bytes in RAM. Then, give these bytes as data argument in the st.download_button function.
import io
import pickle
import streamlit as st
def create_model():
"""Create an sklearn model so that we will have
something interesting to pickle.
Example taken from here:
https://machinelearningmastery.com/save-load-machine-learning-models-python-scikit-learn/
"""
import pandas as pd
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
names = ["preg", "plas", "pres", "skin", "test", "mass", "pedi", "age", "class"]
dataframe = pd.read_csv(url, names=names)
array = dataframe.values
X = array[:,0:8]
Y = array[:,8]
test_size = 0.33
seed = 7
X_train, _, Y_train, _ = model_selection.train_test_split(X, Y, test_size=test_size, random_state=seed)
# Fit the model on training set
model = LogisticRegression()
model.fit(X_train, Y_train)
return model
def pickle_model(model):
"""Pickle the model inside bytes. In our case, it is the "same" as
storing a file, but in RAM.
"""
f = io.BytesIO()
pickle.dump(model, f)
return f
st.title("My .pkl downloader")
model = create_model()
data = pickle_model(model)
st.download_button("Download .pkl file", data=data, file_name="my-pickled-model.pkl")

Pandas parallel URL downloads with pd.read_html

I know I can download a csv file from a web page by doing:
import pandas as pd
import numpy as np
from io import StringIO
URL = "http://www.something.com"
data = pd.read_html(URL)[0].to_csv(index=False, header=True)
file = pd.read_csv(StringIO(data), sep=',')
Now I would like to do the above for more URLs at the same time, like when you open different tabs in your browser. In other words, a way to parallelize this when you have different URLs, instead of looping through or doing it one at a time. So, I thought of having a series of URLs inside a dataframe, and then create a new column which contains the strings 'data', one for each URL.
list_URL = ["http://www.something.com", "http://www.something2.com",
"http://www.something3.com"]
df = pd.DataFrame(list_URL, columns =['URL'])
df['data'] = pd.read_html(df['URL'])[0].to_csv(index=False, header=True)
But it gives me error: cannot parse from 'Series'
Is there a better syntax, or does this mean I cannot do this in parallel for more than one URL?
You could try like this:
import pandas as pd
URLS = [
"https://en.wikipedia.org/wiki/Periodic_table#Presentation_forms",
"https://en.wikipedia.org/wiki/Planet#Planetary_attributes",
]
df = pd.DataFrame(URLS, columns=["URL"])
df["data"] = df["URL"].map(
lambda x: pd.read_html(x)[0].to_csv(index=False, header=True)
)
print(df)
# Output
URL data
0 https://en.wikipedia.org/wiki/Periodic_t... 0\r\nPart of a series on the\r\nPeriodic...
1 https://en.wikipedia.org/wiki/Planet#Pla... 0\r\n"The eight known planets of the Sol...

How to download buildings data from OSM with a polygon (shapefile) as the bounding box?

I am struggling to complete this task. I am trying OSMnx which can be used to download data from OSM above however I am getting an error when using its from_polygon feature when trying to download data. Also I am not sure if this data will include Buildings data.
I load my shapefile into geopandas and can then view and interact with it
This is the code
Building_data = ox.graph_from_polygon(my_shapefile, network_type='all')
ox.plot_graph(Building_data)
However I am getting this error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
EDIT: So I tried to use the OSMnx library instead:
import osmnx as ox
import shapefile
import geopandas
from shapely.geometry import shape
shp = shapefile.Reader('shapefile.shp')
feature = shp.shapeRecords()[0]
first = feature.shape.__geo_interface__
#convert to shapely shape
shp_geom = shape(first)
fprints = ox.geometries.geometries_from_polygon(shp_geom, {'buildings':True})
fprints.to_file('footprints.shp', driver='ESRI Shapefile')
However even though I am using a shapefile with OSMnx I am still getting the error:
CRSError: Invalid projection: +proj=utm +zone=80957 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +type=crs: (Internal Proj Error: proj_create: Error -35 (invalid UTM zone number))
Any ideas?
I wasn't able to download buildings data from OSM with a polygon (shapefile) as the bounding box however I was able to using distance from a point with the following code:
import osmnx as ox
import ast
point = 'point coordinates'
dist = 'distance in m'
buildings = ox.geometries.geometries_from_point(point, {'building': True}, dist=dist)
And convert to a geodataframe:
buildings_save = buildings.applymap(lambda x: str(x) if isinstance(x, list) else x)
Then I clip the buildings data to a boundary using geopandas:
import geopandas as gpd
boundary = gpd.read_file('C:/boundary.shp')
buildings_final = gpd.clip(buildings_save, boundary)
Plot the data to check:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize = (15,12))
buildings_final.plot(ax = ax, color = 'red', edgecolor = 'black',)
plt.title('Buildings Data')
plt.show()

Live plotting from CSV file with matplotlib.animation - Data drawn to first entry error

I am attempting to plot data from a sensor that is continuously being written to a CSV file. While successful in creating a live plot, each new data entry creates an additional line that extends to the first data entry. See Below:
The Python 3.4 Script:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import datetime as dt
import csv
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
x=[] ; y1=[]; y2=[]; y3=[]
def animate(i):
with open( 'data_log.csv', 'r') as csvfile:
alphasensefile = csv.reader(csvfile, delimiter = ',')
next(alphasensefile, None)
next(alphasensefile, None)
next(alphasensefile, None)
for column in alphasensefile:
a = dt.datetime.strptime((column[0]), '%H:%M:%S')
x.append((a))
y1.append(column[1])
y2.append(column[2])
y3.append(column[3])
ax1.clear()
ax1.plot(x,y1)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
Running this script collects sensor data and records it to a CSV file. Every data entry recorded live from that start draws an additional line that goes to the first data entry point. Like so:
if I open the file while the sensor is not recording, only the last data entry is linked to the first point like so:
Data is recorded to the CSV file like so:
PM Data Recorded: 23 03 2018
Time, PM 1, PM 2.5, PM 10
16:12:10, 0.1173, 0.1802, 3.2022
Any thoughts to why this is occurring?
FIX:
After a while playing around with the code, i changed the code to:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.dates as matdate
from matplotlib import style
import time
import datetime as dt
import csv
style.use('bmh')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
def animate(i):
csvfile = open( 'data_log.csv', 'r+')
alphasensefile = csv.reader(csvfile, delimiter = ',')
next(alphasensefile, None)
next(alphasensefile, None)
next(alphasensefile, None)
x=[] ; y1=[]; y2=[]; y3=[]
for column in alphasensefile:
a = dt.datetime.strptime((column[0]), '%H:%M:%S')
x.append((a))
y1.append(column[1])
y2.append(column[2])
y3.append(column[3])
ax.clear()
ax.plot(x, y1, label = 'PM 1 ', linewidth=0.7)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
This plots the data live and smooth without issues.

Imbed matplotlib figure into iPython HTML

I want to dynamically write and display HTML with a code cell in Jupyter Notebook. The objective is to generate the HTML to display table, div, img tags in some way I choose. I want to capture img data and place it where I want in this auto generated HTML.
So far I've figured out that I can do the following:
from IPython.core.display import HTML
HTML("<h1>Hello</h1>")
and get:
Hello
That's great. However, I want to be able to do this:
HTML("<h1>Hello</h1><hr/><img src='somestring'/>")
and get something similar to a Hello with a horizontal line and an image below it, where the image is the same one as below.
import pandas as pd
import numpy as np
np.random.seed(314)
df = pd.DataFrame(np.random.randn(1000, 2), columns=['x', 'y'])
df.plot.scatter(0, 1)
The result should look like this:
Question
What do I replace 'something' with in order to implement this? And more to the point, how do I get it via python?
I would have imagined there was an attribute on a figure object that would hold an serialized version of the image but I can't find it.
After some digging around. Credit to Dmitry B. for pointing me in the right direction.
Solution
from IPython.core.display import HTML
import binascii
from StringIO import StringIO
import matplotlib.pyplot as plt
# open IO object
sio = StringIO()
# generate random DataFrame
np.random.seed(314)
df = pd.DataFrame(np.random.randn(1000, 2), columns=['x', 'y'])
# initialize figure and axis
fig, ax = plt.subplots(1, 1)
# plot DataFrame
ax.scatter(df.iloc[:, 0], df.iloc[:, 1]);
# print raw canvas data to IO object
fig.canvas.print_png(sio)
# convert raw binary data to base64
# I use this to embed in an img tag
img_data = binascii.b2a_base64(sio.getvalue())
# keep img tag outter html in its own variable
img_html = '<img src="data:image/png;base64,{}
">'.format(img_data)
HTML("<h1>Hello</h1><hr/>"+img_html)
I end up with:
from IPython.core.display import Image
import io
s = io.BytesIO()
# make your figure here
plt.savefig(s, format='png', bbox_inches="tight")
plt.close()
Image(s.getvalue())
Let say you have base64 encoded image data:
img_data =
"iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAb2ElEQVR42u1dB3wU5bY/m+xuOklIARIgdKQqeunk2kClSRNsKD9UVFR4ei8PBFTKu1f8Xd8PeCpeBCPlonRBmggiXaogYBIJJQkppPdNts68cybZzZaZrbNJNsyByexO3++c73/Kd843MpZlQaJ7l+RiXUiGRMK0ZMkSWXJysqy5NVSvXr1MPWXRokUs/lzTPtaHe5FMpGeXTZkyxQ8byb+8vNwfya+6uloWGxsLtPaVxggODjY1RkFBgcX20NBQNjc3F+Li4pji4mJWo9Ew+Jnt2bMnu337dgshMQqILwiGGAIgw15PjFcEBAQEMgwThEuAVquVI/kkEqAAE4O5dd0mRqfTsfjd4OfnZ8Dfp8ffZkDS48IEBQWxuI2hz6WlpWyHDh0YOgeRkDUKxeLFi9mmiBYeCwAy3w9XysrKylC9Xh+Fkh+NbRGODRWIDYIrP18TAmoTP2Q2g7+Fwd/E4HcGf4ce9+nwsxY/a3GfBn8nrXUkFLhdT4JB3/FcHQlHRESEHlGDwY5hMCIGCUZTEghPBYDr/QiJwfg5BnvC4926dZtHKoA6Ut31fUoAUGUFIJq1IEYRM3GtwaUCEaAE9+Wo1eo0ZG4B7lPh9hr8rRqjYNCxKAzVtB2PUdN3hUKhxc9aPJ8ERxcVFaXH9uIEAtGCIYRoTJXhsQCg7ld06dIlDH9QW2yMyTNnzlyAEGja72vwj8yCsrIyqKqqAm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AgNDVXgAylQSv3QOJF6v3dQgMGORnpfhypAh71ft2jRIoM7alcmxoicUQh69uwp+/nnn/1wOFOGQiAx34tExjd5XsnJyey2bdsYd20umZhDsrK66oXm9nrVJqwOTHaBu9f4fyVgzJGpmA/3AAAAAElFTkSuQmCC"
then in have it rendered inside of an iPython cell you simply do:
from IPython.core.display import Image
Image(data=img_data)
I'm going to build on what was answered by others (piRSquared) because it didn't work for me with Jupyter and Python 3. I wrote the following function, which will take any plot function I define and call it, and capture the outputs without displaying them in Jupyter. I personally use this in to build custom HTML machine learning reports based on many model iterations I execute using Livy and Spark.
from IPython.core.display import HTML
import binascii
from io import BytesIO
import matplotlib.pyplot as plt
import numpy as np
import base64
def capturePlotHTML(plotFunction):
# open IO object
sio3 = BytesIO()
plotFunction()
plt.savefig(sio3)
sio3.seek(0)
data_uri = base64.b64encode(sio3.read()).decode('ascii')
html_out = '<html><head></head><body>'
html_out += '<img src="data:image/png;base64,{0}" align="left">'.format(data_uri)
html_out += '</body></html>'
#prevents plot from showing in output
plt.close()
return (HTML(html_out))
# Plot Wrappers
# Advanced Wrapper for more complex visualizations (seaborn, etc)
class plotRegline:
def __init__(self):
#// could also pass in name as arg like this #def __init__(self, name):
reg_line_prepped_pdf = pandas_input_pdf
sns.lmplot(x='predicted',y='actual',data=reg_line_prepped_pdf,fit_reg=True, height=3, aspect=2).fig.suptitle("Regression Line")
# Basic Wrapper for simple matplotlib visualizations
def plotTsPred():
ts_plot_prepped_pdf = pandas_input_pdf
ts_plot_prepped_pdf.index = pd.to_datetime(ts_plot_prepped_pdf.DAYDATECOLUMN)
ts_plot_prepped_pdf = ts_plot_prepped_pdf.drop(columns=["DAYDATECOLUMN"])
ts_plot_prepped_pdf.plot(title="Predicted Vs Actual -- Timeseries Plot -- Days", figsize=(25,6))
#building the plots and capturing the outputs
regline_html = capturePlotHTML(plotRegline)
ts_plot_day_html = capturePlotHTML(plotTsPred)
# could be any list number of html objects
html_plots = [regline_html, ts_plot_day_html]
combined_html_plots = display_html(*html_plots)
# the following can be run in this code block or another display the results
combined_html_plotes
The answer by piRSquared no longer works with Python 3. I had to change it to:
from IPython.core.display import HTML
import binascii
from io import BytesIO
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# open IO object
bio = BytesIO()
# generate random DataFrame
np.random.seed(314)
df = pd.DataFrame(np.random.randn(1000, 2), columns=['x', 'y'])
# initialize figure and axis
fig, ax = plt.subplots(1, 1);
# plot DataFrame
ax.scatter(df.iloc[:, 0], df.iloc[:, 1]);
# print raw canvas data to IO object
fig.canvas.print_png(bio)
plt.close(fig)
# convert raw binary data to base64
# I use this to embed in an img tag
img_data = binascii.b2a_base64(bio.getvalue()).decode()
# keep img tag outter html in its own variable
img_html = '<img src="data:image/png;base64,{}
">'.format(img_data)
HTML("<h1>Hello</h1><hr/>"+img_html)
Specifically, I import from io, not StringIO, and I use BytesIO rather than StringIO. I needed to decode the bytes into a string for inserting into the HTML. I also added the required imports of numpy and pandas for the example plot to work, and added plt.close(fig) so that you don't end up with two figures in the output.
If you want to show the results of DataFrame.plot in an iPython cell, try this:
import pandas as pd
import numpy as np
%matplotlib inline
np.random.seed(314)
df = pd.DataFrame(np.random.randn(1000, 2), columns=['x', 'y'])
df.plot.scatter(0, 1)