setting an array element with a sequence error - python

2nd Edit
This is how a "stokes_line" is generated:
os.system('pdv -t %s > temp.txt ' % epoch_name)
stokes_line = np.genfromtxt('temp.txt', usecols=3, dtype=[('stokesI','float')], skip_header=1)
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i])
stokes_list[b] = stokes_line
"pdv" is a command from another programme which generated some numbers used in the "stokes_line".
stokes_list just gets a new stokes_line added too each in a loop each time.
EDIT:
A stokes_line is created with a numpy.genfromtxt command (and so extracts from a txt file) and is made up of floats and they look like this:
[(0.00126596,) (0.000621272,) (0.00058694,) ..., (0.0015368,)
(0.000926448,) (7.95851e-05,)]
A bunch of them together make up a stokes_list, and each stokes_line is made up of the same number of floats.
Is the problem because the genfromtxt is not creating a list which would be all square brackets, but something else (is it called a sequence)??
I wrote a python code which worked find on my laptop (python version 2.7.3), but it doesn't seem to run on my university desktop (python version 2.5.2).
I don't think that showing lots of the code here will be useful, but the error message is on the second of these two lines:
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i])
stokes_list[b] = stokes_line
Where "vap" is a command from another programme which returns some values. Anyway, the crux of the matter is that I get the following error message ONLY when I run on the older version of Python:
ValueError: setting an array element with a sequence.
Now I did a little research, and people say that the problem is due to incompatible lengths of lists of some such - but if that was the case, why does it work fine when I run it on my newer version of Python.
Any help would be great, thank you!

.format, the Formatter class was added in 2.6.
os.system('vap -nc "mjd" %s >> mjd.txt' % new_y[i]) should work.

When you're using np.genfromtxt(…, usecols=3, dtype=[("stokesI","float")]), you're constructing a structured array with only one field ("stokesI"). Of course, when you print the array, this structure appears:
[(0.00126596,) (0.000621272,) (0.00058694,) ..., (0.0015368,) (0.000926448,) (7.95851e-05,)]
Each item is a np.void consisting of a named float field.
Looks a bit wasteful, you could probably simplify your expression as :
np.genfromtxt(…,usecols=3, dtype=float)
You then get:
[ 1.26596000e-03 6.21272000e-04 5.86940000e-04 ... 1.53680000e-03
9.26448000e-04 7.95851000e-05]
The probable reason why it apparently fails on an older version of Python is that you're using an older version of NumPy, like 1.4 or 1.5. There have been some improvements on the construction of ndarrays since (better handling of generators, automatic conversion from strings…)

Related

Updated Python Starred Expressions Error in Numpy Indexing

I have some code that I have been running in Python 3.6, but when I move to Python 3.9 I recieve the below error:
SyntaxError: can't use starred expression here
I understand some syntax related to expressions of the form (*something) was implemented in 3.9 that is not backwards compatible (see, for example, here).
Here is a minimal working example of what my code tries to do:
# Get some data
y = np.random.randn(100,100,100)
# Indexes stored as a tuple
x = (1,2)
# Result I'm after
result = y[...,(*x)]
In the above example, I am trying to essentially return y[:,1,2], but in practice, my tuple may have more values, and my array may be larger.
The above code works fine in Python 3.6 but doesn't work in Python 3.9. I cannot work out what the equivalent piece of code would be in Python 3.9 and above. I don't want to assume the number of dimensions in Y (e.g. I want to keep the ...), but I want to retain the behaviour I have above. How can I do this?
You were almost there:
result = y[(..., *x)]
Intermediate:
(..., *x)
# (Ellipsis, 1, 2)

Placeholders in Python: placing multiple names in an array

I am a beginner in python and learning "Learn Python: The Complete Python Programming Course" using Udemy. I downloaded Python 3.8. All things were good until I reached placeholders:
for i in arr.
Print(sen%(“i”))
I just want to use the function of placeholders in python and complete the task by placing multiple names in an array. I cannot type any of this. May I know why?
I am stuck and can't move ahead in the learning of the course.
Try:
for i in arr:
print(sen%(i))
That is, replace arr. to arr:
It is a bit finicky and will throw an error if the statement isn't lined up correctly, if you hit return in the wrong spot, or put a space where there shouldn't be one.
As #Joshua Varghese suggested, first change arr. to arr:
Make sure there is no space in arr:print(sen%(i))
Put your curser just before print(sen%(i)), and hit return. (This will put your print statement on the next line with the correct spacing).
To have your program execute and iterate through the array of names, you need to have your curser on the next line, the line following print(sen%(i)), then hit return.
Note: using python 3.9.2 on Mac OS

Reading Python CDispatch object only gives the first line

first question coming up...have just started using Python 3.6. I am creating an XML format document of tabulated data. The document object itself has a collection called CellValues. Using Dimensions (aka Unicom Intelligence) I can read this collection as a record set and loop round it with .movenext() etc.
However when I read it in Python with:
rs=tomdoc.tables["T0"].cellvalues()
for val in rs:
print(val)
I only see the first line. In contrast, when I connect to a SqL database, the returned object is a SQLrows type and prints the whole thing, but this one says it's CDispatch.
How can I get it to either loop round or show me the whole recordset?
Apologies for my ignorance and thanks in advance :)
Thanks to a colleague, I do now have a working process.
In fact the collection needs to be indexed this way:
rs = tomdoc.Tables("T0").CellValues
Then it can be read as one normally would read a SQL-type record set:
rs.MoveFirst()
while not rs.EOF:
rowStr = ""
for f in rs.Fields:
rowStr += f.value + "\t"
print(rowStr)
rs.MoveNext()
I'm not sure why the ["T0"] gave me the first line though - that threw me somewhat and made it look closer than it actually was (one of those jolly things one encounters when mixing objects) so I didn't investigate alternatives for that part of the script :(

error with gdalbuildvrt, in Python

I am new to python/GDAL and am running into perhaps a trivial issue. This may stem from the fact that I don't really understand how to use GDAL properly in python, or something careless, but even though I think I am following the help doc, I keep getting a syntax error when trying to use "gdalbuildvrt".
What I want to do is take several (amount varies for each set, call it N) geotagged 1-band binary rasters [all values are either 0 or 1] of different sizes (each raster in the set overlaps for the most part though), and "stack" them on top of each other so that they are aligned properly according to their coordinate information. I want this "stack" simply so I can sum the values and produce a 'total' tiff that has an extent to match the exclusive extent (meaning not just the overlap region) of all the original rasters. The resulting tiff would have values ranging from 0 to N, to represent the total number of "hits" the pixel in that location received over the course of the N rasters.
I was led to gdalbuildvrt [http://www.gdal.org/gdalbuildvrt.html] and after reading about it, it seemed that by using the keyword -separate, I would be able to achieve what I need. However, each time I try to run my program, I get a syntax error. The following shows two of the several different ways I tried calling gdalbuildvrt:
gdalbuildvrt -separate -input_file_list stack.vrt inputlist.txt
gdalbuildvrt -separate stack.vrt inclassfiles
Where inputlist.txt is a text file with a path to the tif on every line, just like the help doc specifies. And inclassfiles is a python list of the pathnames. Every single time, no matter which way I call it, I get a syntax error on the first word after the keywords (i.e. 'inputlist' in inputlist.txt, or 'stack' in stack.vrt).
Could someone please shed some light on what I might be doing wrong? Alternatively, does anyone know how else I could use python to get what I need?
Thanks so much.
gdalbuildvrt is a GDAL command line utility. From your example its a bit unclear how you actually run it, but when running from within Python you should execute it as a subprocess.
And in your first line you have the .vrt and the .txt in the wrong order. The textfile containing the files should follow directly after the -input_file_list.
From within Python you can call gdalbuildvrt like:
import os
os.system('gdalbuildvrt -separate -input_file_list inputlist.txt stack.vrt')
Note that the command is provided as a string. Using a Python list with the files can be done with something like:
os.system('gdalbuildvrt -separate stack.vrt %s') % ' '.join(data)
The ' '.join(data) part converts the list to a string with a space between the items.
Depending on how your GDAL is build, its sometimes possible to use wildcards as well:
os.system('gdalbuildvrt -separate stack.vrt *.tif')

Python "for" inside "any" syntax error?

I have a piece of code that broke the other day and I can't find the problem. I need to do something if I find a coincidence between a user input and the first value of any element of a list of lists.
I had this code running in another computer, but somehow I can't make it run anymore:
if any(orderinput == x[0] for x in order):
orderinput is the user input and order is the list of lists. This worked once and should be working based on what I've read here on stackoverflow, but it throws a syntax error at the r in for.
I tried moving it between lines or adding spaces, but the error follows the r.
I'm working in Python 2.2. I don't remember the version in the machine I made the code.
Generator expressions are available since python 2.4. Try changing to a list comprehension:
if any([orderinput == x[0] for x in order]):
Python 2.2 is twelve years old. A lot of things were different.

Categories

Resources