Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cs.collections error
#1
Hi
I have this error from the python 2.7 code below. Can someone help please? Much appreciated.

Error:
Traceback (most recent call last): File "C:/Users/David/PycharmProjects/pythonProject2/venv/init_collapse.py", line 107, in <module> csp=cs.collections[0].get_paths()[2]# path IndexError: list index out of range
################################################## collapse bathymetry data
collapse_steps=2 # can be changed by USER
collapse_starttime=0.0 # in second; can be changed by USER
collapse_times=linspace(0,collapse_duration,collapse_steps)
cs=contour(kxx,kyy,-kelevation,[collapse_depth])
csp=cs.collections[0].get_paths()[2]# path
csv=csp.vertices # vertices
csv_x=csv[:,0]
csv_y=csv[:,1]
if (not plot_debugfig):
    close()
Reply
#2
There error seems pretty self-explanatory: you're trying to access an element of a list that doesn't exist. There are two indices in that line, so the first question I'd ask is: which is it that's causing the problem?

Also, why are you using Python 2 in the first place?
Reply
#3
This is a script written in the past by someone now lost and I am trying to debug it. I'm a user of the output and have no real programming skills (I'm working blind here). I tried to shift it over to Python3 but it was causing me more headaches.

Can you please describe the code I need to find out which indices is causing the problem.
Reply
#4
Assign the parts that use the different indices to different variables. Those assignments will be on separate lines then and the error will point you to the line that's failing.

Then, work backwards from there - either print out your variables at different points to see whether they have the values you expect, or use a debugger like pdb in the standard library.

I don't know what your situation is, but perhaps learning some of the basics in a more structured way (rather than while trying to fix this broken program) would serve you well?
Reply
#5
please supply full script (including imports, and any external data required).
The script will require upgrade to python 3 as python 2.7 was obsolete January 1, 2020.

In order to do so, need full script.
Reply
#6
I agree I need to learn more but this is a rush to meet a thesis deadline.

It would help if I new what 'csp=cs.collections[0].get_paths()[2]' is trying to do. it seems to take the collected values from the variables 'cs' and interrogate this for something? Could you explain please?
Reply
#7
This is the script up to the problem line. All the variables look good up to the error point. I'm assuming cs.collections is a function of Matplotlib, would it be that that function is missing from my environment?

from scipy import *
from pylab import *
from numpy import *
import sys
from degrees2utm import latlng2utm
from copy import deepcopy
from matplotlib.patches import PathPatch

#close('all')

# =================================================================================
#                       INPUT SECTION BEGIN
# =================================================================================

meshfile='Caldera_Before.xyz' # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
collapse_duration = 60.0 # in seconds
collapse_depth = -480    # depth of the filling cup

meshfile_start='Caldera_Output.xyz' # file with output
file_ls = "collapse.xyt" # file with displacement


# =================================================================================
#                       INPUT SECTION END
# =================================================================================
#utm_grid = True
print('reading mesh file data; might take some time')
data=genfromtxt(meshfile)

nx = 88 # dimensions of the mesh file
ny = 84

elevation=data[:, 2]
xx=data[:, 0]
yy=data[:, 1]

elevation=elevation.reshape((ny, nx)) # bathymetry/topography
xx=xx.reshape((ny, nx))
yy=yy.reshape((ny, nx))


cmax=500 # color scale
lmin=0 # cutting from east 
lmax=88 # cutting from west
rmin=0 # north of santorini
rmax=84 # south of santorini

###### kolumbo data
kxx=xx[rmin:rmax, lmin:lmax]
kyy=yy[rmin:rmax, lmin:lmax]
kelevation=elevation[rmin:rmax, lmin:lmax]

################################################## collapse bathymetry data
collapse_steps = 2 # can be changed by USER
collapse_starttime = 0.0 # in second; can be changed by USER
collapse_times = linspace(0, collapse_duration, collapse_steps)
cs = contour(kxx, kyy, -kelevation, [collapse_depth])
csp = cs.collections[0].get_paths()[2]  # path
csv = csp.vertices # vertices
csv_x = csv[:, 0]
csv_y = csv[:, 1]

if (not plot_debugfig):
    close()
Reply
#8
This is a good example of why '*' should not be used when importing modules.

I'm pretty sure the countour from cs = contour(kxx, kyy, -kelevation, [collapse_depth])
refers to https://matplotlib.org/stable/api/_as_ge...ntour.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,216 Aug-10-2021, 08:10 AM
Last Post: snippsat
  AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts Kristenl2784 4 7,367 Jul-17-2020, 01:50 AM
Last Post: palladium
  collections.OrderedDict Bert123456 2 1,790 Jul-09-2020, 08:51 PM
Last Post: Bert123456
  AttributeError: module 'collections' has no attribute 'namedtuple' epgs1975 2 10,335 May-04-2020, 08:10 PM
Last Post: epgs1975
  Problem with importing and using collections module pythomdummy 3 5,828 Nov-14-2019, 08:48 AM
Last Post: Gribouillis
  Please, advise collections for my task AlekseyPython 1 2,115 Sep-11-2019, 12:44 PM
Last Post: perfringo
  What's the use case of collections.counter? magic 2 2,602 Sep-21-2018, 02:04 AM
Last Post: Mekire
  Drills/Exercises for collections, itertools, and common built-in data structures? Cranberry 2 3,370 Apr-26-2018, 05:43 PM
Last Post: Cranberry

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020