Convert Points Datatype of vtkUnstructuredGrid - python

I have an unstructured grid where the points are given as doubles. I'd like to convert them to floats.
What's the easiest way to do this?
So far I've been resorting to the hacky method of writing the grid to a legacy vtk ascii file and simply exchanging double for float in the header...

You could just recreate a dataset manually, or use a vtkArrayCalculator Filter for this task, with the following parameters :
Coordinate Results : ON
Function : "coords"
Result Array Type : Float
If you used ParaView, it would look like this :

Related

Replace quotes in a python array or arrays

I have a dict object in python:
'polygon': {'coordinates': [[['51.89600366973369,5.604579537426986'], ['51.88589816283178,5.598937488274174'], ['51.882074887477046,5.620585782912315'], ['51.891197214447004,5.622057621577596']]]}
I want to replace the ' from the coordinates with nothing so:
'51.89600366973369,5.604579537426986' becomes 51.89600366973369,5.604579537426986
How can I achieve that in python? To be honest, I don't now where to start.
Yes now it's indeed an array with a string with geographic coordinates, but elasticsearch want the according the geojson format (https://www.rfc-editor.org/rfc/rfc7946) which is not a string. The python function needs tranform the data and then stream it to elasticsearch.

change the type of numpyndarray float element to string

I have an arff file as input. I read the arff file and put the element values in a numpy ndarray.Now my arff file contains some '?' as some of the elements. Basically these are property values of matrices calculated by anamod. Whichever values anamod cannot calculate it plugs in a '?' character for those. I want to do a Naive baiyes, Random Forest etc prediction for my data. So to handle the '?' I want to use an imputer which is like :
Imputer(missing_values='NaN', strategy='mean', axis=0)
The missing_values above is of type string of course. My question is that how to change the type of a few numpy ndarray elements to string from float. I used my_numpy_ndarray.astype('str') == 'NaN' to check for NaN values and I could do it successfully but I am not sure how to change the type of numpyndarray float element to string.
You can't change the type of parts of an ordinary ndarray. An ndarray requires all elements in the array to have the same numpy type (the dtype), so that mathematical operations can be done efficiently. The only way to do this is to change the dtype to object, which allows you to store arbitrary types in each element. However, this will drastically reduce the speed of most operations, and make some operations impossible or unreliable (such as adding two arrays).

numpy array values to be converted from string to float?

I have a dataset like the one shown below
http://i.stack.imgur.com/1uxCK.png
I am able to read them into an numpy array but the datatype is of type string when it has read from the CSV file. I am unable to convert the same into float since without that i would not be able to proceed further.Mind you there are blank spaces between the two data columns shown in the first screenshot.
The numpy array structure when printed looks like in the screenshot given below:
http://i.stack.imgur.com/JFfzw.png
Note: (Observe the Single Quotation Marks between the start and end of each data line in the screenshot which is a proof that numpy has stored the data as a string rather than float)
Any help would be appreciated in helping me convert the data from string to float type?????? have Tried many things but yet all in vain!!!!!!!!
numpy.loadtxt(filename) should work out of the box: it yields numbers.

Import matrix from text file using python

I have two text files that have matrices written in them(not numpy matrices, so its a list of lists). These matrices are written in string format, so the text file looks like this :
[[1,2,3],[3,4,5],[6,7,8]],[[3,3,3],[5,6,7],.....
I want to read this matrix back from the text file using python. I can't read using numpy as it gives ValueError: could not convert string to float
Is there anyway to do this? Would it be easier if I just wrote the matrix as a numpy matrix in the first place(I need to change code of a previous program for that, and was just wondering if there was a python way of loading matrices when it was stored as a string in a text file)?
You could make use of the ast module:
import ast
strArray = "[[1,2,3],[3,4,5],[6,7,8]]"
# evaluates the array in string format and converts it to a python array object
array = ast.literal_eval(strArray)
note:
For multiple nested arrays like you have, literal_eval will most likely convert the string into a tuple with nested arrays as elements. Just keep that in mind as you use this module.

python data types

I wrote a script to take files of data that is in columns and plot it depending on which column the user wants to view. Well, I noticed that the plots look crazy, and have all the wrong numbers because python is ignoring the exponential.
My numbers are in the format: 1.000000E+1 OR 1.000000E-1
What dtype is that? I am using numpy.genfromtxt to import with a dtype = float. I know there are all sorts of dtypes you can enter, but I cannot find a comprehensive list of the options, and examples.
Thanks.
Here is an example of my input (those spaces are tabs):
Time StampT1_ModBtT2_90BendT3_InPET5_Stg2Rfrg
5:22 AM2.115800E+21.400000E+01.400000E+03.035100E+1
5:23 AM2.094300E+21.400000E+01.400000E+03.034800E+1
5:24 AM2.079300E+21.400000E+01.400000E+03.031300E+1
5:25 AM2.069500E+21.400000E+01.400000E+03.031400E+1
5:26 AM2.052600E+21.400000E+01.400000E+03.030400E+1
5:27 AM2.040700E+21.400000E+01.400000E+03.029100E+1
Update
I figured out at least part of the reason why what I am doing does not work. Still do not know how to define dtypes the way I want to.
import numpy as np
file = np.genfromtxt('myfile.txt', usecols = (0,1), dtype = (str, float), delimiter = '\t')
That returns an array of strings for each column. How do I tell it I want column 0 to be a str, and all the rest of the columns to be float?
In [55]: type(1.000000E+1)
Out[55]: <type 'float'>
What does your input data look like, it's fair possible that it's in the wrong input format but it's also sure that it's fairly easy to convert it to the right format.
Numbers in the form 1.0000E+1 can be parsed by float(), so I'm not sure what the problem is:
>>> float('1.000E+1')
10.0
I think you'll want to get a text parser to parse the format into a native python data type.
like 1.00000E+1 turns into 1.0^1, which could be expressed as a float.

Categories