Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

region_plot raises weird IndexError when y_min > y_max #35032

Closed
2 tasks done
seblabbe opened this issue Feb 8, 2023 · 5 comments
Closed
2 tasks done

region_plot raises weird IndexError when y_min > y_max #35032

seblabbe opened this issue Feb 8, 2023 · 5 comments

Comments

@seblabbe
Copy link
Contributor

seblabbe commented Feb 8, 2023

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.

Did you read the documentation and troubleshoot guide?

  • I have read the documentation and troubleshoot guide

Environment

- **OS**: Ubuntu 20.04
- **Sage Version**: SageMath version 9.8.beta1, Release Date: 2022-09-29

Steps To Reproduce

sage: region_plot(x+y>0, (x,-20,20), (y,20,-20))

Expected Behavior

A ValueError saying that y_min and y_max should satisfy y_min < y_max.

Actual Behavior

IndexError                                
Traceback (most recent call last)
Input In [168], in <cell line: 1>()
----> 1 region_plot(x+y>Integer(0), (x,-Integer(20),Integer(20)), (y, Integer(20),-Integer(20)))

File ~/GitBox/sage/src/sage/misc/decorators.py:491, in options.__call__.<locals>.wrapper(*args, **kwds)
    489     options['__original_opts'] = kwds
    490 options.update(kwds)
--> 491 return func(*args, **options)

File ~/GitBox/sage/src/sage/plot/contour_plot.py:1727, in region_plot(f, xrange, yrange, plot_points, incol, outcol, bordercol, borderstyle, borderwidth, alpha, **options)
   1723 g._set_extra_kwds(Graphics._extract_kwds_for_show(options,
   1724                                                   ignore=['xmin', 'xmax']))
   1726 if neqs == 0:
-> 1727     g.add_primitive(ContourPlot(xy_data_array, xrange, yrange,
   1728                                 dict(contours=[-1e-20, 0, 1e-20],
   1729                                      cmap=cmap,
   1730                                      fill=True, **options)))
   1731 else:
   1732     mask = numpy.asarray([[elt > 0 for elt in rows]
   1733                           for rows in xy_data_array],
   1734                          dtype=bool)

File ~/GitBox/sage/src/sage/plot/contour_plot.py:82, in ContourPlot.__init__(self, xy_data_array, xrange, yrange, options)
     80 self.xy_data_array = xy_data_array
     81 self.xy_array_row = len(xy_data_array)
---> 82 self.xy_array_col = len(xy_data_array[0])
     83 GraphicPrimitive.__init__(self, options)

IndexError: index 0 is out of bounds for axis 0 with size 0

Additional Information

The current error does not help the user to find the real problem.

@seblabbe seblabbe added the t: bug label Feb 8, 2023
@seblabbe seblabbe changed the title <title> region_plot raises weird IndexError when y_min > y_max Feb 8, 2023
@jnash10
Copy link
Contributor

jnash10 commented Feb 10, 2023

Hi @seblabbe,

I've modified the code to solve the issue in PR #35065.

This is the change in the 'setup_for_eval_on_grid' function in misc.py which Contour_plot uses to prepare for plotting and preprocesses the ranges including checking for errors like correct range format.

#to check if range is correctly defined by user. gives error is xmin>xmax or ymin>ymax
    if (ranges[0][-2]>ranges[0][-1]):
        raise ValueError("Range not correctly defined. xmin>xmax")

    if (ranges[1][-2]>ranges[1][-1]):
        raise ValueError("Range not correctly defined. ymin>ymax")

@jnash10
Copy link
Contributor

jnash10 commented Feb 10, 2023

The new input-output looks like this:

sage: x,y = var('x,y')
sage: region_plot(x+y>0, (-20,20), (20,-20))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 region_plot(x+y>Integer(0), (-Integer(20),Integer(20)), (Integer(20),-Integer(20)))

File ~/projects/sage/src/sage/misc/decorators.py:496, in options.__call__.<locals>.wrapper(*args, **kwds)
    494     options['__original_opts'] = kwds
    495 options.update(kwds)
--> 496 return func(*args, **options)

File ~/projects/sage/src/sage/plot/contour_plot.py:1688, in region_plot(f, xrange, yrange, plot_points, incol, outcol, bordercol, borderstyle, borderwidth, alpha, **options)
   1682 if not f:
   1683     return implicit_plot(feqs[0], xrange, yrange, plot_points=plot_points,
   1684                          fill=False, linewidth=borderwidth,
   1685                          linestyle=borderstyle, color=bordercol, **options)
-> 1688 f_all, ranges = setup_for_eval_on_grid(feqs + f,
   1689                                        [xrange, yrange],
   1690                                        plot_points)
   1691 xrange, yrange = [r[:2] for r in ranges]
   1693 xy_data_arrays = numpy.asarray([[[func(x, y)
   1694                                   for x in xsrange(*ranges[0],
   1695                                                    include_endpoint=True)]
   1696                                  for y in xsrange(*ranges[1],
   1697                                                   include_endpoint=True)]
   1698                                 for func in f_all[neqs::]], dtype=float)

File ~/projects/sage/src/sage/plot/misc.py:145, in setup_for_eval_on_grid(funcs, ranges, plot_points, return_vars, imaginary_tolerance)
    142     raise ValueError("Range not correctly defined. xmin>xmax")
    144 if (ranges[1][-2]>ranges[1][-1]):
--> 145     raise ValueError("Range not correctly defined. ymin>ymax")
    147 else:
    148     vars, free_vars = unify_arguments(funcs)

ValueError: Range not correctly defined. ymin>ymax

@jnash10
Copy link
Contributor

jnash10 commented Feb 11, 2023

Please check the latest PR #35077 as a fix to the issue.

This PR has a clean commit history.

Thank you, looking forward to contribute to SageMath regularly.

@jnash10
Copy link
Contributor

jnash10 commented Feb 13, 2023

Last PR was interfering with old if/else case.

Updated in new PR #35113

Also merged base/develop into the branch. So there are 2 commits in total. (fix + merge)
Hopefully that does not create an issue.

Hopefully everything is fine this time.

@kwankyu
Copy link
Collaborator

kwankyu commented Feb 22, 2023

Fixed by #35113

@kwankyu kwankyu closed this as completed Feb 22, 2023
vbraun pushed a commit that referenced this issue Mar 26, 2023
    
### 📚 Description

Solves #35032

Add if condition in misc.py which contour_plot uses to check for invalid
range entered by user(xmin > xmax or ymin>ymax)

### 📝 Checklist


- [x ] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [ x] I have linked an issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation accordingly.
    
URL: #35113
Reported by: Agamdeep Singh
Reviewer(s): Agamdeep Singh, Kwankyu Lee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment