OGR GetX returns always zero - gdal

I am trying to get the first x-coordinates of a polygon. However I only get zero in return. What could cause that?
import os, sys, gdal, ogr
from gdalconst import *
driver = ogr.GetDriverByName('ESRI Shapefile')
testarea = driver.Open('testarea.shp', 0)
testarealyr = testarea.GetLayer()
testareafeature = testarealyr.GetNextFeature()
# get the x,y coordinates for the point
testareageom = testareafeature.GetGeometryRef()
print testareageom
x = testareageom.GetX()
print x
>>>
POLYGON ((-124.1963006330602 43.006410659375554,-124.1861022086067 43.006647759060762,-124.1858958821004 43.00274627515271,-124.19612378176909 43.002422936639086,-124.19612378176909 43.002422936639086,-124.1963006330602 43.006410659375554))
0.0
>>>

Related

Matplotlib output too small to read

In adjusting the domain of a function to find certain parameters in a matplotlib plot, I found that when I try to isolate the part I need, the output becomes so small that details are impossible to see. I've tried refreshing the kernel with no change and plt.rcParams['figure.figsize'] hasn't been effective either.
This is my current code, with unused options in the function removed.
import numpy as np
import matplotlib.pyplot as plt
def P_cubic(V,T,Tc,Pc,ParamSet,omega=0):
R = 8.31446261815324 #J mol^-1 K^-1
Tr = T/Tc
if ParamSet == 'vdW':
elif ParamSet == 'RK':
elif ParamSet == 'SRK':
elif ParamSet == 'PR':
alpha = (1+(0.37464+1.54226*omega-0.26992*omega**2)*
(1-Tr**(1/2)))**2
sigma = 1+np.sqrt(2)
epsilon = 1-np.sqrt(2)
Omega = 0.07780
Psi = 0.45724
Zc = 0.30740
a = Psi*alpha*R**2*Tc**2/Pc
b = Omega*T*Tc/Pc #m3 mol-1
P = R*T/(V-b)-a/((V+epsilon*b)*(V+sigma*b))
return P
Tc = 512.5 #K
Pc = 8.0840E6 #Pa
omega = 0.565831
T = 473 #K
b = 0.07780*T*Tc/Pc #m3 mol-1
V = np.arange(0,1,0.001)
Vrange = b*V #m3 mol-1
PPa = np.empty(len(Vrange))
for i in range(len(Vrange)):
PPa[i]=P_cubic(Vrange[i],T,Tc,Pc,'PR',omega) #Pa
Pbar = PPa*1.0E-5 #bar
plt.rcParams['figure.figsize']=(1,0.8)
plt.plot(V,Pbar)
plt.xlabel('V/b')
plt.ylabel('P /bar')
plt.xlim(0,np.max(V))
plt.ylim(np.min(Pbar),np.max(Pbar))
plt.title('Variance of Pressure with Volume of Pure Methanol at 473 K')
plt.text(15,-6,f'b = {b:.2E} m^3/mol');
Below are screenshots with the output at varying figsize parameters to show that plt.rcParams['figure.figsize'] is not helping.
How do I fix this so that I can see the details of the plot?
There are two reasons for this. First, the size unit of the graph is inches, so the specified number itself is small, resulting in a smaller graph. Secondly, the default coordinates of the annotations are based on the data, so the x-value is 15, which is far from the graph, so the figure is automatically smaller. So, I think you need to set the graph size and fix the x-value of the annotations.
fig, ax = plt.subplots()
plt.rcParams['figure.figsize']=(8,4)
ax.plot(V,Pbar)
plt.xlabel('V/b')
plt.ylabel('P /bar')
plt.xlim(0,np.max(V))
plt.ylim(np.min(Pbar),np.max(Pbar))
plt.title('Variance of Pressure with Volume of Pure Methanol at 473 K')
plt.text(1.1,-6,f'b = {b:.2E} m^3/mol')
#plt.text(1.1,-6,f'b = {b:.2E} m^3/mol', transform=ax.transData)
plt.show()

Operands Could not be Broadcast with Shapes (19,)(0,) -- KNN

I am working on how to use KNN to predict a rating for a movie. I use a video and a book to teach myself how to go about it
I tried to run the code I found in the book but it gave me error message. I googled the error message so as to understand it and fix my problem but I don't think I know how to adapt the solutions to my problem.
import numpy as np
import pandas as pd
r_cols = ['user_id', 'movie_id', 'rating']
ratings = pd.read_csv('C:/Users/dell/Downloads/DataScience/DataScience-Python3/ml-100k/u.data', sep='\t', engine='python', names=r_cols, usecols=range(3)) # please enter your file path here. The file is u.data
print(ratings.head())
movieProperties = ratings.groupby('movie_id').agg({'rating': [np.size, np.mean]})
print(movieProperties.head())
movieNumRatings = pd.DataFrame(movieProperties['rating']['size'])
movieNormalizedNumRatings = movieNumRatings.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))
print(movieNormalizedNumRatings.head())
movieDict = {}
with open('C:/Users/dell/Downloads/DataScience/DataScience-Python3/ml-100k/u.item') as f: # The file is u.item
temp = ''
for line in f:
fields = line.rstrip('\n').split('|')
movieID = int(fields[0])
name = fields[1]
genres = fields[5:25]
genres = map(int, genres)
movieDict[movieID] = (name, genres, movieNormalizedNumRatings.loc[movieID].get('size'), movieProperties.loc[movieID].rating.get('mean'))
print(movieDict[1])
from scipy import spatial
def ComputeDistance(a, b):
genresA = np.array(list(a[1]))
genresB = np.array(list(b[1]))
genreDistance = spatial.distance.cosine(genresA, genresB)
popularityA = np.array(a[2])
popularityB = np.array(b[2])
popularityDistance = abs(popularityA - popularityB)
return genreDistance + popularityDistance
print(ComputeDistance(movieDict[2], movieDict[4]))
import operator
def getNeighbors(movieID, K):
distances = []
for movie in movieDict:
if (movie != movieID):
dist = ComputeDistance(movieDict[movieID], movieDict[movie])
distances.append((movie, dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(K):
neighbors.append(distance[x][0])
return neighbors
K = 10
avgRating = 0
neighbors = getNeighbors(1, K)
I got this error message from PowerShell:
Traceback(most recent call last):
neighbors = getNeighbors(1, K)
dist = ComputeDistance(movieDict[movieID], movieDict[movie])
genreDistance = spatial.distance.cosine(genresA, genresB)
return correlation(u, v, w=w, centered=False)
uv = np.average(u*v, weights=w)
ValueError: operands could not be broadcast together with shape (19,)(0,)
I got this error message when I tried to debug the problem from ipython terminal:
c:\programdata\anaconda3\lib\site-packages\scipy\spatial\distance.py(695)correlation()
693 u = u - umu
694 v = v - vmu
---> 695 uv = np.average(u*v, weights=w)
696 uu = np.average(np.square(u), weights=w)
697 vv = np.average(np.square(v), weights=w)
**Note**: The code ran fine and produced results up until *print(Cprint(ComputeDistance(movieDict[2], movieDict[4]))*
My guess is the problem is with this part of the code:
import operator
def getNeighbors(movieID, K):
distances = []
for movie in movieDict:
if (movie != movieID):
dist = ComputeDistance(movieDict[movieID], movieDict[movie])
distances.append((movie, dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(K):
neighbors.append(distance[x][0])
return neighbors
K = 10
avgRating = 0
neighbors = getNeighbors(1, K)
The code can be found in this link: https://hendra-herviawan.github.io/Movie-Recommendation-based-on-KNN-K-Nearest-Neighbors.html
The error of "operands could not be broadcast together with shape (x,)(y,)" usually raises when you are trying to perform an operation between two arrays that must have the same shape but they don't. In your case you are trying to take an weighted average between two arrays u and v. The arrays u and v don't have the length.
I saw that you parsing a movies list by splitting the lines with the "|" character and then storing these results in a dictionary. Probably this file or its division with "|" are returning different results.
The error log shows that the second array doesn't have any element, this could be generated by an empty line on the movies files.

traitsui Range() fatal error with slider entry box

Below is some sample code straight from the MayaVI website on using sliders. Try putting in a number outside of the slider range for a fatal error:
from numpy import arange, pi, cos, sin
from traits.api import HasTraits, Range, Instance, \
on_trait_change
from traitsui.api import View, Item, Group
from mayavi.core.api import PipelineBase
from mayavi.core.ui.api import MayaviScene, SceneEditor, \
MlabSceneModel
dphi = pi/1000.
phi = arange(0.0, 2*pi + 0.5*dphi, dphi, 'd')
def curve(n_mer, n_long):
mu = phi*n_mer
x = cos(mu) * (1 + cos(n_long * mu/n_mer)*0.5)
y = sin(mu) * (1 + cos(n_long * mu/n_mer)*0.5)
z = 0.5 * sin(n_long*mu/n_mer)
t = sin(mu)
return x, y, z, t
class MyModel(HasTraits):
n_meridional = Range(0, 30, 6, )#mode='spinner')
n_longitudinal = Range(0, 30, 11, )#mode='spinner')
scene = Instance(MlabSceneModel, ())
plot = Instance(PipelineBase)
# When the scene is activated, or when the parameters are changed, we
# update the plot.
#on_trait_change('n_meridional,n_longitudinal,scene.activated')
def update_plot(self):
x, y, z, t = curve(self.n_meridional, self.n_longitudinal)
if self.plot is None:
self.plot = self.scene.mlab.plot3d(x, y, z, t,
tube_radius=0.025, colormap='Spectral')
else:
self.plot.mlab_source.set(x=x, y=y, z=z, scalars=t)
# The layout of the dialog created
view = View(Item('scene', editor=SceneEditor(scene_class=MayaviScene),
height=250, width=300, show_label=False),
Group(
'_', 'n_meridional', 'n_longitudinal',
),
resizable=True,
)
my_model = MyModel()
my_model.configure_traits()
How can I improve this code to disallow users from triggering this fatal error? I think a line that could deactivate the entry box (such as setDisabled(True)) could work, or remove it entirely - but I'm not sure how to implement it within the traitsui methods.
After lots of trial and error, this appears to be a bug in the default Range() mode of Traitsui, at least in the case for Mac OS X (I'm running High Sierra, 10.13.3).
The solution is to alter the default mode to one that looks and acts identical, minus crashing the program:
n_meridional = Range(0, 30, 6, mode='slider')

PyOpenCL reduction Kernel on each pixel of image as array instead of each byte (RGB mode, 24 bits )

I'm trying to calculate the average Luminance of an RGB image. To do this, I find the luminance of each pixel i.e.
L(r,g,b) = X*r + Y*g + Z*b (some linear combination).
And then find the average by summing up luminance of all pixels and dividing by width*height.
To speed this up, I'm using pyopencl.reduction.ReductionKernel
The array I pass to it is a Single Dimension Numpy Array so it works just like the example given.
import Image
import numpy as np
im = Image.open('image_00000001.bmp')
data = np.asarray(im).reshape(-1) # so data is a single dimension list
# data.dtype is uint8, data.shape is (w*h*3, )
I want to incorporate the following code from the example into it . i.e. I would make changes to datatype and the type of arrays I'm passing. This is the example:
a = pyopencl.array.arange(queue, 400, dtype=numpy.float32)
b = pyopencl.array.arange(queue, 400, dtype=numpy.float32)
krnl = ReductionKernel(ctx, numpy.float32, neutral="0",
reduce_expr="a+b", map_expr="x[i]*y[i]",
arguments="__global float *x, __global float *y")
my_dot_prod = krnl(a, b).get()
Except, my map_expr will work on each pixel and convert each pixel to its luminance value.
And reduce expr remains the same.
The problem is, it works on each element in the array, and I need it to work on each pixel which is 3 consecutive elements at a time (RGB ).
One solution is to have three different arrays, one for R, one for G and one for B ,which would work, but is there another way ?
Edit: I changed the program to illustrate the char4 usage instead of float4:
import numpy as np
import pyopencl as cl
import pyopencl.array as cl_array
deviceID = 0
platformID = 0
workGroup=(1,1)
N = 10
testData = np.zeros(N, dtype=cl_array.vec.char4)
dev = cl.get_platforms()[platformID].get_devices()[deviceID]
ctx = cl.Context([dev])
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
Data_In = cl.Buffer(ctx, mf.READ_WRITE, testData.nbytes)
prg = cl.Program(ctx, """
__kernel void Pack_Cmplx( __global char4* Data_In, int N)
{
int gid = get_global_id(0);
//Data_In[gid] = 1; // This would change all components to one
Data_In[gid].x = 1; // changing single component
Data_In[gid].y = 2;
Data_In[gid].z = 3;
Data_In[gid].w = 4;
}
""").build()
prg.Pack_Cmplx(queue, (N,1), workGroup, Data_In, np.int32(N))
cl.enqueue_copy(queue, testData, Data_In)
print testData
I hope it helps.

Numpy.ma polyfit function for masked arrays crashes on integer input

The numpy polynomial fit function for masked arrays, ma.polyfit, crashes on integer iput:
import numpy.ma as ma
x = ma.arange(2)
y = ma.arange(2)
p1 = ma.polyfit(np.float32(x), y, deg=1)
p2 = ma.polyfit( x , y, deg=1)
The last line results in an error:
ValueError: data type <type 'numpy.int64'> not inexact
Why can't I fit data with integer x-values (it's no problem with the normal numpy.polyfit function), is this a (known) bug?
It is indeed a bug from numpy.ma : the rcond (a parameter to exclude some values ) takes len(x)*np.finfo(x.dtypes).eps as a default value, and np.int32 does not have any epsfield (because an int does not have a relative precision).
import numpy.ma as ma
eps = np.finfo(np.float32).eps
x = ma.arange(2)
y = ma.arange(2)
p1 = ma.polyfit(np.float32(x), y, deg=1, rcond = len(x)*eps)
p2 = ma.polyfit( x , y, deg=1, rcond = len(x)*eps)
I've looked quickly into numpy's issues, and this bug does not seems to figured there. It might be a good idea to raise a new issue : New Issue