As-wandb's workspace
Runs
850
Name
68 visualized
1-20
of 68runs.summary["swebench_output_table"]
- 6 of 5440
astropy__astropy-12907
unresolved
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
Modelingの`separability_matrix`は、入れ子のCompoundModelsに対して分離可能性を正しく計算しません。
次のモデルを考えます:
```python
from astropy.modeling import models as m
from astropy.modeling.separable import separability_matrix
cm = m.Linear1D(10) & m.Linear1D(5)
```
予想通り、分離可能性行列は対角行列になります:
```python
>>> separability_matrix(cm)
array([[ True, False],
[False, True]])
```
モデルをさらに複雑にするとします:
```python
>>> separability_matrix(m.Pix2Sky_TAN() & m.Linear1D(10) & m.Linear1D(5))
array([[ True, True, False, False],
[ True, True, False, False],
[False, False, True, False],
[False, False, False, True]])
```
出力行列は再び予想通りで、線形モデルの入出力は互いに分離可能かつ独立しています。
しかし、これらの複合モデルを入れ子にした場合:
```python
>>> separability_matrix(m.Pix2Sky_TAN() & cm)
array([[ True, True, False, False],
[ True, True, False, False],
[False, False, True, True],
[False, False, True, True]])
```
突然、入力と出力が分離不能になってしまいました?
これはバグのように感じますが、何か見落としているのでしょうか?
</issue>
<code>
[start of README.rst]
1 =======
2 Astropy
3 =======
4
5 |Actions Status| |CircleCI Status| |Azure Status| |Coverage Status| |PyPI Status| |Documentation Status| |Zenodo|
6
7 The Astropy Project (http://astropy.org/) is a community effort to develop a
8 single core package for Astronomy in Python and foster interoperability between
9 Python astronomy packages. This repository contains the core package which is
10 intended to contain much of the core functionality and some common tools needed
11 for performing astronomy and astrophysics with Python.
12
13 Releases are `registered on PyPI <https://pypi.org/project/astropy>`_,
14 and development is occurring at the
15 `project's GitHub page <http://github.com/astropy/astropy>`_.
16
17 For installation instructions, see the `online documentation <https://docs.astropy.org/>`_
18 or `docs/install.rst <docs/install.rst>`_ in this source distribution.
19
20 Contributing Code, Documentation, or Feedback
21 ---------------------------------------------
22
23 The Astropy Project is made both by and for its users, so we welcome and
24 encourage contributions of many kinds. Our goal is to keep this a positive,
25 inclusive, successful, and growing community by abiding with the
26 `Astropy Community Code of Conduct <http://www.astropy.org/about.html#codeofconduct>`_.
27
28 More detailed information on contributing to the project or submitting feedback
29 can be found on the `contributions <http://www.astropy.org/contribute.html>`_
30 page. A `summary of contribution guidelines <CONTRIBUTING.md>`_ can also be
31 used as a quick reference when you are ready to start writing or validating
32 code for submission.
33
34 Supporting the Project
35 ----------------------
36
37 |NumFOCUS| |Donate|
38
39 The Astropy Project is sponsored by NumFOCUS, a 501(c)(3) nonprofit in the
40 United States. You can donate to the project by using the link above, and this
41 donation will support our mission to promote sustainable, high-level code base
42 for the astronomy community, open code development, educational materials, and
43 reproducible scientific research.
44
45 License
46 -------
47
48 Astropy is licensed under a 3-clause BSD style license - see the
49 `LICENSE.rst <LICENSE.rst>`_ file.
50
51 .. |Actions Status| image:: https://github.com/astropy/astropy/workflows/CI/badge.svg
52 :target: https://github.com/astropy/astropy/actions
53 :alt: Astropy's GitHub Actions CI Status
54
55 .. |CircleCI Status| image:: https://img.shields.io/circleci/build/github/astropy/astropy/main?logo=circleci&label=CircleCI
56 :target: https://circleci.com/gh/astropy/astropy
57 :alt: Astropy's CircleCI Status
58
59 .. |Azure Status| image:: https://dev.azure.com/astropy-project/astropy/_apis/build/status/astropy.astropy?repoName=astropy%2Fastropy&branchName=main
60 :target: https://dev.azure.com/astropy-project/astropy
61 :alt: Astropy's Azure Pipelines Status
62
63 .. |Coverage Status| image:: https://codecov.io/gh/astropy/astropy/branch/main/graph/badge.svg
64 :target: https://codecov.io/gh/astropy/astropy
65 :alt: Astropy's Coverage Status
66
67 .. |PyPI Status| image:: https://img.shields.io/pypi/v/astropy.svg
68 :target: https://pypi.org/project/astropy
69 :alt: Astropy's PyPI Status
70
71 .. |Zenodo| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.4670728.svg
72 :target: https://doi.org/10.5281/zenodo.4670728
73 :alt: Zenodo DOI
74
75 .. |Documentation Status| image:: https://img.shields.io/readthedocs/astropy/latest.svg?logo=read%20the%20docs&logoColor=white&label=Docs&version=stable
76 :target: https://docs.astropy.org/en/stable/?badge=stable
77 :alt: Documentation Status
78
79 .. |NumFOCUS| image:: https://img.shields.io/badge/powered%20by-NumFOCUS-orange.svg?style=flat&colorA=E1523D&colorB=007D8A
80 :target: http://numfocus.org
81 :alt: Powered by NumFOCUS
82
83 .. |Donate| image:: https://img.shields.io/badge/Donate-to%20Astropy-brightgreen.svg
84 :target: https://numfocus.salsalabs.org/donate-to-astropy/index.html
85
86
87 If you locally cloned this repo before 7 Apr 2021
88 -------------------------------------------------
89
90 The primary branch for this repo has been transitioned from ``master`` to
91 ``main``. If you have a local clone of this repository and want to keep your
92 local branch in sync with this repo, you'll need to do the following in your
93 local clone from your terminal::
94
95 git fetch --all --prune
96 # you can stop here if you don't use your local "master"/"main" branch
97 git branch -m master main
98 git branch -u origin/main main
99
100 If you are using a GUI to manage your repos you'll have to find the equivalent
101 commands as it's different for different programs. Alternatively, you can just
102 delete your local clone and re-clone!
103
[end of README.rst]
[start of astropy/modeling/separable.py]
1 # Licensed under a 3-clause BSD style license - see LICENSE.rst
2
3 """
4 Functions to determine if a model is separable, i.e.
5 if the model outputs are independent.
6
7 It analyzes ``n_inputs``, ``n_outputs`` and the operators
8 in a compound model by stepping through the transforms
9 and creating a ``coord_matrix`` of shape (``n_outputs``, ``n_inputs``).
10
11
12 Each modeling operator is represented by a function which
13 takes two simple models (or two ``coord_matrix`` arrays) and
14 returns an array of shape (``n_outputs``, ``n_inputs``).
15
16 """
17
18 import numpy as np
19
20 from .core import Model, ModelDefinitionError, CompoundModel
21 from .mappings import Mapping
22
23
24 __all__ = ["is_separable", "separability_matrix"]
25
26
27 def is_separable(transform):
28 """
29 A separability test for the outputs of a transform.
30
31 Parameters
32 ----------
33 transform : `~astropy.modeling.core.Model`
34 A (compound) model.
35
36 Returns
37 -------
38 is_separable : ndarray
39 A boolean array with size ``transform.n_outputs`` where
40 each element indicates whether the output is independent
41 and the result of a separable transform.
42
43 Examples
44 --------
45 >>> from astropy.modeling.models import Shift, Scale, Rotation2D, Polynomial2D
46 >>> is_separable(Shift(1) & Shift(2) | Scale(1) & Scale(2))
47 array([ True, True]...)
48 >>> is_separable(Shift(1) & Shift(2) | Rotation2D(2))
49 array([False, False]...)
50 >>> is_separable(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]) | \
51 Polynomial2D(1) & Polynomial2D(2))
52 array([False, False]...)
53 >>> is_separable(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]))
54 array([ True, True, True, True]...)
55
56 """
57 if transform.n_inputs == 1 and transform.n_outputs > 1:
58 is_separable = np.array([False] * transform.n_outputs).T
59 return is_separable
60 separable_matrix = _separable(transform)
61 is_separable = separable_matrix.sum(1)
62 is_separable = np.where(is_separable != 1, False, True)
63 return is_separable
64
65
66 def separability_matrix(transform):
67 """
68 Compute the correlation between outputs and inputs.
69
70 Parameters
71 ----------
72 transform : `~astropy.modeling.core.Model`
73 A (compound) model.
74
75 Returns
76 -------
77 separable_matrix : ndarray
78 A boolean correlation matrix of shape (n_outputs, n_inputs).
79 Indicates the dependence of outputs on inputs. For completely
80 independent outputs, the diagonal elements are True and
81 off-diagonal elements are False.
82
83 Examples
84 --------
85 >>> from astropy.modeling.models import Shift, Scale, Rotation2D, Polynomial2D
86 >>> separability_matrix(Shift(1) & Shift(2) | Scale(1) & Scale(2))
87 array([[ True, False], [False, True]]...)
88 >>> separability_matrix(Shift(1) & Shift(2) | Rotation2D(2))
89 array([[ True, True], [ True, True]]...)
90 >>> separability_matrix(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]) | \
91 Polynomial2D(1) & Polynomial2D(2))
92 array([[ True, True], [ True, True]]...)
93 >>> separability_matrix(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]))
94 array([[ True, False], [False, True], [ True, False], [False, True]]...)
95
96 """
97 if transform.n_inputs == 1 and transform.n_outputs > 1:
98 return np.ones((transform.n_outputs, transform.n_inputs),
99 dtype=np.bool_)
100 separable_matrix = _separable(transform)
101 separable_matrix = np.where(separable_matrix != 0, True, False)
102 return separable_matrix
103
104
105 def _compute_n_outputs(left, right):
106 """
107 Compute the number of outputs of two models.
108
109 The two models are the left and right model to an operation in
110 the expression tree of a compound model.
111
112 Parameters
113 ----------
114 left, right : `astropy.modeling.Model` or ndarray
115 If input is of an array, it is the output of `coord_matrix`.
116
117 """
118 if isinstance(left, Model):
119 lnout = left.n_outputs
120 else:
121 lnout = left.shape[0]
122 if isinstance(right, Model):
123 rnout = right.n_outputs
124 else:
125 rnout = right.shape[0]
126 noutp = lnout + rnout
127 return noutp
128
129
130 def _arith_oper(left, right):
131 """
132 Function corresponding to one of the arithmetic operators
133 ['+', '-'. '*', '/', '**'].
134
135 This always returns a nonseparable output.
136
137
138 Parameters
139 ----------
140 left, right : `astropy.modeling.Model` or ndarray
141 If input is of an array, it is the output of `coord_matrix`.
142
143 Returns
144 -------
145 result : ndarray
146 Result from this operation.
147 """
148 # models have the same number of inputs and outputs
149 def _n_inputs_outputs(input):
150 if isinstance(input, Model):
151 n_outputs, n_inputs = input.n_outputs, input.n_inputs
152 else:
153 n_outputs, n_inputs = input.shape
154 return n_inputs, n_outputs
155
156 left_inputs, left_outputs = _n_inputs_outputs(left)
157 right_inputs, right_outputs = _n_inputs_outputs(right)
158
159 if left_inputs != right_inputs or left_outputs != right_outputs:
160 raise ModelDefinitionError(
161 "Unsupported operands for arithmetic operator: left (n_inputs={}, "
162 "n_outputs={}) and right (n_inputs={}, n_outputs={}); "
163 "models must have the same n_inputs and the same "
164 "n_outputs for this operator.".format(
165 left_inputs, left_outputs, right_inputs, right_outputs))
166
167 result = np.ones((left_outputs, left_inputs))
168 return result
169
170
171 def _coord_matrix(model, pos, noutp):
172 """
173 Create an array representing inputs and outputs of a simple model.
174
175 The array has a shape (noutp, model.n_inputs).
176
177 Parameters
178 ----------
179 model : `astropy.modeling.Model`
180 model
181 pos : str
182 Position of this model in the expression tree.
183 One of ['left', 'right'].
184 noutp : int
185 Number of outputs of the compound model of which the input model
186 is a left or right child.
187
188 """
189 if isinstance(model, Mapping):
190 axes = []
191 for i in model.mapping:
192 axis = np.zeros((model.n_inputs,))
193 axis[i] = 1
194 axes.append(axis)
195 m = np.vstack(axes)
196 mat = np.zeros((noutp, model.n_inputs))
197 if pos == 'left':
198 mat[: model.n_outputs, :model.n_inputs] = m
199 else:
200 mat[-model.n_outputs:, -model.n_inputs:] = m
201 return mat
202 if not model.separable:
203 # this does not work for more than 2 coordinates
204 mat = np.zeros((noutp, model.n_inputs))
205 if pos == 'left':
206 mat[:model.n_outputs, : model.n_inputs] = 1
207 else:
208 mat[-model.n_outputs:, -model.n_inputs:] = 1
209 else:
210 mat = np.zeros((noutp, model.n_inputs))
211
212 for i in range(model.n_inputs):
213 mat[i, i] = 1
214 if pos == 'right':
215 mat = np.roll(mat, (noutp - model.n_outputs))
216 return mat
217
218
219 def _cstack(left, right):
220 """
221 Function corresponding to '&' operation.
222
223 Parameters
224 ----------
225 left, right : `astropy.modeling.Model` or ndarray
226 If input is of an array, it is the output of `coord_matrix`.
227
228 Returns
229 -------
230 result : ndarray
231 Result from this operation.
232
233 """
234 noutp = _compute_n_outputs(left, right)
235
236 if isinstance(left, Model):
237 cleft = _coord_matrix(left, 'left', noutp)
238 else:
239 cleft = np.zeros((noutp, left.shape[1]))
240 cleft[: left.shape[0], : left.shape[1]] = left
241 if isinstance(right, Model):
242 cright = _coord_matrix(right, 'right', noutp)
243 else:
244 cright = np.zeros((noutp, right.shape[1]))
245 cright[-right.shape[0]:, -right.shape[1]:] = 1
246
247 return np.hstack([cleft, cright])
248
249
250 def _cdot(left, right):
251 """
252 Function corresponding to "|" operation.
253
254 Parameters
255 ----------
256 left, right : `astropy.modeling.Model` or ndarray
257 If input is of an array, it is the output of `coord_matrix`.
258
259 Returns
260 -------
261 result : ndarray
262 Result from this operation.
263 """
264
265 left, right = right, left
266
267 def _n_inputs_outputs(input, position):
268 """
269 Return ``n_inputs``, ``n_outputs`` for a model or coord_matrix.
270 """
271 if isinstance(input, Model):
272 coords = _coord_matrix(input, position, input.n_outputs)
273 else:
274 coords = input
275 return coords
276
277 cleft = _n_inputs_outputs(left, 'left')
278 cright = _n_inputs_outputs(right, 'right')
279
280 try:
281 result = np.dot(cleft, cright)
282 except ValueError:
283 raise ModelDefinitionError(
284 'Models cannot be combined with the "|" operator; '
285 'left coord_matrix is {}, right coord_matrix is {}'.format(
286 cright, cleft))
287 return result
288
289
290 def _separable(transform):
291 """
292 Calculate the separability of outputs.
293
294 Parameters
295 ----------
296 transform : `astropy.modeling.Model`
297 A transform (usually a compound model).
298
299 Returns :
300 is_separable : ndarray of dtype np.bool
301 An array of shape (transform.n_outputs,) of boolean type
302 Each element represents the separablity of the corresponding output.
303 """
304 if (transform_matrix := transform._calculate_separability_matrix()) is not NotImplemented:
305 return transform_matrix
306 elif isinstance(transform, CompoundModel):
307 sepleft = _separable(transform.left)
308 sepright = _separable(transform.right)
309 return _operators[transform.op](sepleft, sepright)
310 elif isinstance(transform, Model):
311 return _coord_matrix(transform, 'left', transform.n_outputs)
312
313
314 # Maps modeling operators to a function computing and represents the
315 # relationship of axes as an array of 0-es and 1-s
316 _operators = {'&': _cstack, '|': _cdot, '+': _arith_oper, '-': _arith_oper,
317 '*': _arith_oper, '/': _arith_oper, '**': _arith_oper}
318
[end of astropy/modeling/separable.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/astropy/modeling/separable.py
+++ b/astropy/modeling/separable.py
@@ -202,11 +202,11 @@
- else:
- mat[-model.n_outputs:, -model.n_inputs:] = m
+ else:
+ mat[-model.n_outputs:, :model.n_inputs] = m
return mat
if not model.separable:
# this does not work for more than 2 coordinates
mat = np.zeros((noutp, model.n_inputs))
if pos == 'left':
- mat[:model.n_outputs, : model.n_inputs] = 1
+ mat[:model.n_outputs, : model.n_inputs] = np.eye(model.n_inputs)
else:
- mat[-model.n_outputs:, -model.n_inputs:] = 1
+ mat[-model.n_outputs:, : model.n_inputs] = np.eye(model.n_inputs)
else:
True
swebench/sweb.eval.x86_64.astropy_1776_astropy-12907:latest
astropy__astropy-13033
unresolved
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
TimeSeries: 必須列チェックに失敗したときの誤解を招く例外
<!-- This comments are hidden when you submit the issue,
so you do not need to remove them! -->
<!-- Please be sure to check out our contributing guidelines,
https://github.com/astropy/astropy/blob/main/CONTRIBUTING.md .
Please be sure to check out our code of conduct,
https://github.com/astropy/astropy/blob/main/CODE_OF_CONDUCT.md . -->
<!-- Please have a search on our GitHub repository to see if a similar
issue has already been posted.
If a similar issue is closed, have a quick look to see if you are satisfied
by the resolution.
If not please go ahead and open an issue! -->
<!-- Please check that the development version still produces the same bug.
You can install development version with
pip install git+https://github.com/astropy/astropy
command. -->
### 説明
<!-- Provide a general description of the bug. -->
`TimeSeries`オブジェクトに追加の必須列(`time`に加えて)が存在する場合に、コードが誤って必須列を削除しようとした際に発生する例外メッセージが誤解を招く内容になっている。
### 期待される動作
<!-- What did you expect to happen. -->
必須列が不足していることをユーザーに通知する例外が発生すること。
### 実際の動作
実際の例外メッセージは以下のように分かりづらい:
`ValueError: TimeSeries object is invalid - expected 'time' as the first columns but found 'time'`
### 再現手順
<!-- Ideally a code example could be provided so we can run it ourselves. -->
<!-- If you are pasting code, use triple backticks (```) around
your code snippet. -->
<!-- If necessary, sanitize your screen output to be pasted so you do not
reveal secrets like tokens and passwords. -->
```python
from astropy.time import Time
from astropy.timeseries import TimeSeries
time=Time(np.arange(100000, 100003), format='jd')
ts = TimeSeries(time=time, data = {"flux": [99.9, 99.8, 99.7]})
ts._required_columns = ["time", "flux"]
ts.remove_column("flux")
```
### システム情報
<!-- Even if you do not think this is necessary, it is useful information for the maintainers.
Please run the following snippet and paste the output below:
import platform; print(platform.platform())
import sys; print("Python", sys.version)
import numpy; print("Numpy", numpy.__version__)
import erfa; print("pyerfa", erfa.__version__)
import astropy; print("astropy", astropy.__version__)
import scipy; print("Scipy", scipy.__version__)
import matplotlib; print("Matplotlib", matplotlib.__version__)
-->
```text
Windows-10-10.0.22000-SP0
Python 3.9.10 | packaged by conda-forge | (main, Feb 1 2022, 21:21:54) [MSC v.1929 64 bit (AMD64)]
Numpy 1.22.3
pyerfa 2.0.0.1
astropy 5.0.3
Scipy 1.8.0
Matplotlib 3.5.1
```
</issue>
<code>
[start of README.rst]
1 =======
2 Astropy
3 =======
4
5 |Actions Status| |CircleCI Status| |Azure Status| |Coverage Status| |PyPI Status| |Documentation Status| |Zenodo|
6
7 The Astropy Project (http://astropy.org/) is a community effort to develop a
8 single core package for Astronomy in Python and foster interoperability between
9 Python astronomy packages. This repository contains the core package which is
10 intended to contain much of the core functionality and some common tools needed
11 for performing astronomy and astrophysics with Python.
12
13 Releases are `registered on PyPI <https://pypi.org/project/astropy>`_,
14 and development is occurring at the
15 `project's GitHub page <http://github.com/astropy/astropy>`_.
16
17 For installation instructions, see the `online documentation <https://docs.astropy.org/>`_
18 or `docs/install.rst <docs/install.rst>`_ in this source distribution.
19
20 Contributing Code, Documentation, or Feedback
21 ---------------------------------------------
22
23 The Astropy Project is made both by and for its users, so we welcome and
24 encourage contributions of many kinds. Our goal is to keep this a positive,
25 inclusive, successful, and growing community by abiding with the
26 `Astropy Community Code of Conduct <http://www.astropy.org/about.html#codeofconduct>`_.
27
28 More detailed information on contributing to the project or submitting feedback
29 can be found on the `contributions <http://www.astropy.org/contribute.html>`_
30 page. A `summary of contribution guidelines <CONTRIBUTING.md>`_ can also be
31 used as a quick reference when you are ready to start writing or validating
32 code for submission.
33
34 Supporting the Project
35 ----------------------
36
37 |NumFOCUS| |Donate|
38
39 The Astropy Project is sponsored by NumFOCUS, a 501(c)(3) nonprofit in the
40 United States. You can donate to the project by using the link above, and this
41 donation will support our mission to promote sustainable, high-level code base
42 for the astronomy community, open code development, educational materials, and
43 reproducible scientific research.
44
45 License
46 -------
47
48 Astropy is licensed under a 3-clause BSD style license - see the
49 `LICENSE.rst <LICENSE.rst>`_ file.
50
51 .. |Actions Status| image:: https://github.com/astropy/astropy/workflows/CI/badge.svg
52 :target: https://github.com/astropy/astropy/actions
53 :alt: Astropy's GitHub Actions CI Status
54
55 .. |CircleCI Status| image:: https://img.shields.io/circleci/build/github/astropy/astropy/main?logo=circleci&label=CircleCI
56 :target: https://circleci.com/gh/astropy/astropy
57 :alt: Astropy's CircleCI Status
58
59 .. |Azure Status| image:: https://dev.azure.com/astropy-project/astropy/_apis/build/status/astropy.astropy?repoName=astropy%2Fastropy&branchName=main
60 :target: https://dev.azure.com/astropy-project/astropy
61 :alt: Astropy's Azure Pipelines Status
62
63 .. |Coverage Status| image:: https://codecov.io/gh/astropy/astropy/branch/main/graph/badge.svg
64 :target: https://codecov.io/gh/astropy/astropy
65 :alt: Astropy's Coverage Status
66
67 .. |PyPI Status| image:: https://img.shields.io/pypi/v/astropy.svg
68 :target: https://pypi.org/project/astropy
69 :alt: Astropy's PyPI Status
70
71 .. |Zenodo| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.4670728.svg
72 :target: https://doi.org/10.5281/zenodo.4670728
73 :alt: Zenodo DOI
74
75 .. |Documentation Status| image:: https://img.shields.io/readthedocs/astropy/latest.svg?logo=read%20the%20docs&logoColor=white&label=Docs&version=stable
76 :target: https://docs.astropy.org/en/stable/?badge=stable
77 :alt: Documentation Status
78
79 .. |NumFOCUS| image:: https://img.shields.io/badge/powered%20by-NumFOCUS-orange.svg?style=flat&colorA=E1523D&colorB=007D8A
80 :target: http://numfocus.org
81 :alt: Powered by NumFOCUS
82
83 .. |Donate| image:: https://img.shields.io/badge/Donate-to%20Astropy-brightgreen.svg
84 :target: https://numfocus.salsalabs.org/donate-to-astropy/index.html
85
86
87 If you locally cloned this repo before 7 Apr 2021
88 -------------------------------------------------
89
90 The primary branch for this repo has been transitioned from ``master`` to
91 ``main``. If you have a local clone of this repository and want to keep your
92 local branch in sync with this repo, you'll need to do the following in your
93 local clone from your terminal::
94
95 git fetch --all --prune
96 # you can stop here if you don't use your local "master"/"main" branch
97 git branch -m master main
98 git branch -u origin/main main
99
100 If you are using a GUI to manage your repos you'll have to find the equivalent
101 commands as it's different for different programs. Alternatively, you can just
102 delete your local clone and re-clone!
103
[end of README.rst]
[start of astropy/timeseries/core.py]
1 # Licensed under a 3-clause BSD style license - see LICENSE.rst
2
3 from types import FunctionType
4 from contextlib import contextmanager
5 from functools import wraps
6
7 from astropy.table import QTable
8
9 __all__ = ['BaseTimeSeries', 'autocheck_required_columns']
10
11 COLUMN_RELATED_METHODS = ['add_column',
12 'add_columns',
13 'keep_columns',
14 'remove_column',
15 'remove_columns',
16 'rename_column']
17
18
19 def autocheck_required_columns(cls):
20 """
21 This is a decorator that ensures that the table contains specific
22 methods indicated by the _required_columns attribute. The aim is to
23 decorate all methods that might affect the columns in the table and check
24 for consistency after the methods have been run.
25 """
26
27 def decorator_method(method):
28
29 @wraps(method)
30 def wrapper(self, *args, **kwargs):
31 result = method(self, *args, **kwargs)
32 self._check_required_columns()
33 return result
34
35 return wrapper
36
37 for name in COLUMN_RELATED_METHODS:
38 if (not hasattr(cls, name) or
39 not isinstance(getattr(cls, name), FunctionType)):
40 raise ValueError(f"{name} is not a valid method")
41 setattr(cls, name, decorator_method(getattr(cls, name)))
42
43 return cls
44
45
46 class BaseTimeSeries(QTable):
47
48 _required_columns = None
49 _required_columns_enabled = True
50
51 # If _required_column_relax is True, we don't require the columns to be
52 # present but we do require them to be the correct ones IF present. Note
53 # that this is a temporary state - as soon as the required columns
54 # are all present, we toggle this to False
55 _required_columns_relax = False
56
57 def _check_required_columns(self):
58
59 if not self._required_columns_enabled:
60 return
61
62 if self._required_columns is not None:
63
64 if self._required_columns_relax:
65 required_columns = self._required_columns[:len(self.colnames)]
66 else:
67 required_columns = self._required_columns
68
69 plural = 's' if len(required_columns) > 1 else ''
70
71 if not self._required_columns_relax and len(self.colnames) == 0:
72
73 raise ValueError("{} object is invalid - expected '{}' "
74 "as the first column{} but time series has no columns"
75 .format(self.__class__.__name__, required_columns[0], plural))
76
77 elif self.colnames[:len(required_columns)] != required_columns:
78
79 raise ValueError("{} object is invalid - expected '{}' "
80 "as the first column{} but found '{}'"
81 .format(self.__class__.__name__, required_columns[0], plural, self.colnames[0]))
82
83 if (self._required_columns_relax
84 and self._required_columns == self.colnames[:len(self._required_columns)]):
85 self._required_columns_relax = False
86
87 @contextmanager
88 def _delay_required_column_checks(self):
89 self._required_columns_enabled = False
90 yield
91 self._required_columns_enabled = True
92 self._check_required_columns()
93
[end of astropy/timeseries/core.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/astropy/timeseries/core.py
+++ b/astropy/timeseries/core.py
@@ -74,1 +74,1 @@
- if self._required_columns is not None:
+ if self._required_columns is not None and len(self._required_columns) > 0:
@@ -80,2 +80,2 @@
-
+
if not self._required_columns_relax and len(self.colnames) == 0:
@@ -87,3 +87,6 @@
- raise ValueError("{} object is invalid - expected '{}' "
- "as the first column{} but found '{}'"
- .format(self.__class__.__name__, required_columns[0], plural, self.colnames[0]))
+ if missing_columns:
+ raise ValueError("{} object is invalid - missing required column{} '{}'"
+ .format(self.__class__.__name__, plural, "', '".join(missing_columns)))
+ else:
+ raise ValueError("{} object is invalid - expected columns '{}' in order but found '{}'"
+ .format(self.__class__.__name__, "', '".join(required_columns), "', '".join(self.colnames)))
True
swebench/sweb.eval.x86_64.astropy_1776_astropy-13033:latest
astropy__astropy-14182
error
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
RestructuredText出力でのヘッダ行のサポートを依頼
### 説明
以下の処理が可能になると非常に便利です:
```Python
>>> from astropy.table import QTable
>>> import astropy.units as u
>>> import sys
>>> tbl = QTable({'wave': [350,950]*u.nm, 'response': [0.7, 1.2]*u.count})
>>> tbl.write(sys.stdout, format="ascii.rst")
===== ========
wave response
===== ========
350.0 0.7
950.0 1.2
===== ========
>>> tbl.write(sys.stdout, format="ascii.fixed_width", header_rows=["name", "unit"])
| wave | response |
| nm | ct |
| 350.0 | 0.7 |
| 950.0 | 1.2 |
>>> tbl.write(sys.stdout, format="ascii.rst", header_rows=["name", "unit"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/astropy/table/connect.py", line 129, in __call__
self.registry.write(instance, *args, **kwargs)
File "/usr/lib/python3/dist-packages/astropy/io/registry/core.py", line 369, in write
return writer(data, *args, **kwargs)
File "/usr/lib/python3/dist-packages/astropy/io/ascii/connect.py", line 26, in io_write
return write(table, filename, **kwargs)
File "/usr/lib/python3/dist-packages/astropy/io/ascii/ui.py", line 856, in write
writer = get_writer(Writer=Writer, fast_writer=fast_writer, **kwargs)
File "/usr/lib/python3/dist-packages/astropy/io/ascii/ui.py", line 800, in get_writer
writer = core._get_writer(Writer, fast_writer, **kwargs)
File "/usr/lib/python3/dist-packages/astropy/io/ascii/core.py", line 1719, in _get_writer
writer = Writer(**writer_kwargs)
TypeError: RST.__init__() got an unexpected keyword argument 'header_rows'
```
### 追加コンテキスト
RestructuredText出力は自動生成されたドキュメントにコンテンツを埋め込むのに最適な方法です。このような柔軟性があると作業が非常に楽になります `:-)`
</issue>
<code>
[start of README.rst]
1 =======
2 Astropy
3 =======
4
5 .. container::
6
7 |Actions Status| |CircleCI Status| |Coverage Status| |PyPI Status| |Documentation Status| |Pre-Commit| |isort Status| |Zenodo|
8
9 The Astropy Project (http://astropy.org/) is a community effort to develop a
10 single core package for Astronomy in Python and foster interoperability between
11 Python astronomy packages. This repository contains the core package which is
12 intended to contain much of the core functionality and some common tools needed
13 for performing astronomy and astrophysics with Python.
14
15 Releases are `registered on PyPI <https://pypi.org/project/astropy>`_,
16 and development is occurring at the
17 `project's GitHub page <http://github.com/astropy/astropy>`_.
18
19 For installation instructions, see the `online documentation <https://docs.astropy.org/>`_
20 or `docs/install.rst <docs/install.rst>`_ in this source distribution.
21
22 Contributing Code, Documentation, or Feedback
23 ---------------------------------------------
24
25 The Astropy Project is made both by and for its users, so we welcome and
26 encourage contributions of many kinds. Our goal is to keep this a positive,
27 inclusive, successful, and growing community by abiding with the
28 `Astropy Community Code of Conduct <http://www.astropy.org/about.html#codeofconduct>`_.
29
30 More detailed information on contributing to the project or submitting feedback
31 can be found on the `contributions <http://www.astropy.org/contribute.html>`_
32 page. A `summary of contribution guidelines <CONTRIBUTING.md>`_ can also be
33 used as a quick reference when you are ready to start writing or validating
34 code for submission.
35
36 Supporting the Project
37 ----------------------
38
39 |NumFOCUS| |Donate|
40
41 The Astropy Project is sponsored by NumFOCUS, a 501(c)(3) nonprofit in the
42 United States. You can donate to the project by using the link above, and this
43 donation will support our mission to promote sustainable, high-level code base
44 for the astronomy community, open code development, educational materials, and
45 reproducible scientific research.
46
47 License
48 -------
49
50 Astropy is licensed under a 3-clause BSD style license - see the
51 `LICENSE.rst <LICENSE.rst>`_ file.
52
53 .. |Actions Status| image:: https://github.com/astropy/astropy/workflows/CI/badge.svg
54 :target: https://github.com/astropy/astropy/actions
55 :alt: Astropy's GitHub Actions CI Status
56
57 .. |CircleCI Status| image:: https://img.shields.io/circleci/build/github/astropy/astropy/main?logo=circleci&label=CircleCI
58 :target: https://circleci.com/gh/astropy/astropy
59 :alt: Astropy's CircleCI Status
60
61 .. |Coverage Status| image:: https://codecov.io/gh/astropy/astropy/branch/main/graph/badge.svg
62 :target: https://codecov.io/gh/astropy/astropy
63 :alt: Astropy's Coverage Status
64
65 .. |PyPI Status| image:: https://img.shields.io/pypi/v/astropy.svg
66 :target: https://pypi.org/project/astropy
67 :alt: Astropy's PyPI Status
68
69 .. |Zenodo| image:: https://zenodo.org/badge/DOI/10.5281/zenodo.4670728.svg
70 :target: https://doi.org/10.5281/zenodo.4670728
71 :alt: Zenodo DOI
72
73 .. |Documentation Status| image:: https://img.shields.io/readthedocs/astropy/latest.svg?logo=read%20the%20docs&logoColor=white&label=Docs&version=stable
74 :target: https://docs.astropy.org/en/stable/?badge=stable
75 :alt: Documentation Status
76
77 .. |Pre-Commit| image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
78 :target: https://github.com/pre-commit/pre-commit
79 :alt: pre-commit
80
81 .. |isort Status| image:: https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336
82 :target: https://pycqa.github.io/isort/
83 :alt: isort Status
84
85 .. |NumFOCUS| image:: https://img.shields.io/badge/powered%20by-NumFOCUS-orange.svg?style=flat&colorA=E1523D&colorB=007D8A
86 :target: http://numfocus.org
87 :alt: Powered by NumFOCUS
88
89 .. |Donate| image:: https://img.shields.io/badge/Donate-to%20Astropy-brightgreen.svg
90 :target: https://numfocus.salsalabs.org/donate-to-astropy/index.html
91
92
93 If you locally cloned this repo before 7 Apr 2021
94 -------------------------------------------------
95
96 The primary branch for this repo has been transitioned from ``master`` to
97 ``main``. If you have a local clone of this repository and want to keep your
98 local branch in sync with this repo, you'll need to do the following in your
99 local clone from your terminal::
100
101 git fetch --all --prune
102 # you can stop here if you don't use your local "master"/"main" branch
103 git branch -m master main
104 git branch -u origin/main main
105
106 If you are using a GUI to manage your repos you'll have to find the equivalent
107 commands as it's different for different programs. Alternatively, you can just
108 delete your local clone and re-clone!
109
[end of README.rst]
[start of astropy/io/ascii/rst.py]
1 # Licensed under a 3-clause BSD style license
2 """
3 :Author: Simon Gibbons (simongibbons@gmail.com)
4 """
5
6
7 from .core import DefaultSplitter
8 from .fixedwidth import (
9 FixedWidth,
10 FixedWidthData,
11 FixedWidthHeader,
12 FixedWidthTwoLineDataSplitter,
13 )
14
15
16 class SimpleRSTHeader(FixedWidthHeader):
17 position_line = 0
18 start_line = 1
19 splitter_class = DefaultSplitter
20 position_char = "="
21
22 def get_fixedwidth_params(self, line):
23 vals, starts, ends = super().get_fixedwidth_params(line)
24 # The right hand column can be unbounded
25 ends[-1] = None
26 return vals, starts, ends
27
28
29 class SimpleRSTData(FixedWidthData):
30 start_line = 3
31 end_line = -1
32 splitter_class = FixedWidthTwoLineDataSplitter
33
34
35 class RST(FixedWidth):
36 """reStructuredText simple format table.
37
38 See: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#simple-tables
39
40 Example::
41
42 ==== ===== ======
43 Col1 Col2 Col3
44 ==== ===== ======
45 1 2.3 Hello
46 2 4.5 Worlds
47 ==== ===== ======
48
49 Currently there is no support for reading tables which utilize continuation lines,
50 or for ones which define column spans through the use of an additional
51 line of dashes in the header.
52
53 """
54
55 _format_name = "rst"
56 _description = "reStructuredText simple table"
57 data_class = SimpleRSTData
58 header_class = SimpleRSTHeader
59
60 def __init__(self):
61 super().__init__(delimiter_pad=None, bookend=False)
62
63 def write(self, lines):
64 lines = super().write(lines)
65 lines = [lines[1]] + lines + [lines[1]]
66 return lines
67
[end of astropy/io/ascii/rst.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/astropy/io/ascii/rst.py
+++ b/astropy/io/ascii/rst.py
@@ -7,49 +7,50 @@
-from .core import DefaultSplitter
+from .core import DefaultSplitter, Header
+from .fixedwidth import FixedWidth, FixedWidthData, FixedWidthHeader, FixedWidthTwoLineDataSplitter
class SimpleRSTHeader(FixedWidthHeader):
position_line = 0
start_line = 1
splitter_class = DefaultSplitter
position_char = "="
def get_fixedwidth_params(self, line):
vals, starts, ends = super().get_fixedwidth_params(line)
# The right hand column can be unbounded
ends[-1] = None
return vals, starts, ends
class SimpleRSTData(FixedWidthData):
start_line = 3
end_line = -1
splitter_class = FixedWidthTwoLineDataSplitter
class RST(FixedWidth):
"""reStructuredText simple format table.
See: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#simple-tables
Example::
==== ===== ======
Col1 Col2 Col3
==== ===== ======
1 2.3 Hello
2 4.5 Worlds
==== ===== ======
Currently there is no support for reading tables which utilize continuation lines,
or for ones which define column spans through the use of an additional
line of dashes in the header.
"""
_format_name = "rst"
_description = "reStructuredText simple table"
data_class = SimpleRSTData
header_class = SimpleRSTHeader
- def __init__(self):
+ def __init__(self, header_rows=None):
super().__init__(delimiter_pad=None, bookend=False, header_rows=header_rows)
True
swebench/sweb.eval.x86_64.astropy_1776_astropy-14182:latest
astropy__astropy-7336
resolved
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
`units.quantity_input` デコレータが型ヒント付きの戻り値 `None` のコンストラクタで失敗
### 概要
`units.quantity_input` デコレータをコンストラクタの型ヒント付きで使用していますが、コンストラクタの正しい戻り値 (`None`) を指定すると例外が発生します。これは `None` に `'to'` 属性が存在しないためです。
### 再現手順
以下のファイルで問題を再現できます:
``` Python
import astropy.units as u
class PoC(object):
@u.quantity_input
def __init__(self, voltage: u.V) -> None:
pass
if __name__ == '__main__':
poc = PoC(1.*u.V)
```
実行結果:
```
$ python3 poc.py
Traceback (most recent call last):
File "poc.py", line 12, in <module>
poc = PoC(1.*u.V)
File "/usr/lib64/python3.6/site-packages/astropy/utils/decorators.py", line 868, in __init__
func = make_function_with_signature(func, name=name, **wrapped_args)
File "/usr/lib64/python3.6/site-packages/astropy/units/decorators.py", line 225, in wrapper
return return_.to(wrapped_signature.return_annotation)
AttributeError: 'NoneType' object has no attribute 'to'
```
Fedora 27環境(Python 3.6.3, astropy 2.0.2, numpy 1.13.3)でテスト済みです。
### 回避策
戻り値の型ヒントを省略することで問題を回避できます。ただし、静的型チェックツールは関数が何も返さないことを推論できなくなります。
### 修正案
デコレータが明示的に `None` が返された場合に単位チェックをスキップするように動作を変更する方法が考えられます。
</issue>
<code>
[start of README.rst]
1 =======
2 Astropy
3 =======
4
5 .. image:: https://img.shields.io/pypi/v/astropy.svg
6 :target: https://pypi.python.org/pypi/astropy
7
8 Astropy (http://www.astropy.org) is a package intended to contain much of
9 the core functionality and some common tools needed for performing
10 astronomy and astrophysics with Python.
11
12 Releases are `registered on PyPI <http://pypi.python.org/pypi/astropy>`_,
13 and development is occurring at the
14 `project's github page <http://github.com/astropy/astropy>`_.
15
16 For installation instructions, see the `online documentation <http://docs.astropy.org/>`_
17 or ``docs/install.rst`` in this source distribution.
18
19 For system packagers: Please install Astropy with the command::
20
21 $ python setup.py --offline install
22
23 This will prevent the astropy_helpers bootstrap script from attempting to
24 reach out to PyPI.
25
26 Project Status
27 --------------
28
29 .. image:: https://travis-ci.org/astropy/astropy.svg
30 :target: https://travis-ci.org/astropy/astropy
31 :alt: Astropy's Travis CI Status
32
33 .. image:: https://coveralls.io/repos/astropy/astropy/badge.svg
34 :target: https://coveralls.io/r/astropy/astropy
35 :alt: Astropy's Coveralls Status
36
37 .. image:: https://ci.appveyor.com/api/projects/status/ym7lxajcs5qwm31e/branch/master?svg=true
38 :target: https://ci.appveyor.com/project/Astropy/astropy/branch/master
39 :alt: Astropy's Appveyor Status
40
41 For an overview of the testing and build status of all packages associated
42 with the Astropy Project, see http://dashboard.astropy.org.
43
44 .. image:: https://img.shields.io/badge/powered%20by-NumFOCUS-orange.svg?style=flat&colorA=E1523D&colorB=007D8A
45 :target: http://numfocus.org
46 :alt: Powered by NumFOCUS
47
48
49 Contributing Code, Documentation, or Feedback
50 ---------------------------------------------
51 The Astropy project is made both by and for its users, so we welcome and encourage
52 contributions of many kinds. Our goal is to keep this a positive, inclusive,
53 successful, and growing community, by abiding with the
54 `Astropy Community Code of Conduct <http://www.astropy.org/about.html#codeofconduct>`_.
55
56 More detailed information on contributing to the project or submitting feedback
57 can be found on the `contributions <http://www.astropy.org/contribute.html>`_ page.
58
59 A `summary of contribution guidelines <CONTRIBUTING.md>`_ can also be used as a quick
60 reference when you're ready to start writing or validating code for submission.
61
62 License
63 -------
64 Astropy is licensed under a 3-clause BSD style license - see the
65 ``LICENSE.rst`` file.
66
[end of README.rst]
[start of astropy/units/decorators.py]
1 # -*- coding: utf-8 -*-
2 # Licensed under a 3-clause BSD style license - see LICENSE.rst
3
4 __all__ = ['quantity_input']
5
6 import inspect
7 from ..utils.decorators import wraps
8 from ..utils.misc import isiterable
9
10 from .core import Unit, UnitsError, add_enabled_equivalencies
11 from .physical import _unit_physical_mapping
12
13
14 def _get_allowed_units(targets):
15 """
16 From a list of target units (either as strings or unit objects) and physical
17 types, return a list of Unit objects.
18 """
19
20 allowed_units = []
21 for target in targets:
22
23 try: # unit passed in as a string
24 target_unit = Unit(target)
25
26 except ValueError:
27
28 try: # See if the function writer specified a physical type
29 physical_type_id = _unit_physical_mapping[target]
30
31 except KeyError: # Function argument target is invalid
32 raise ValueError("Invalid unit or physical type '{0}'."
33 .format(target))
34
35 # get unit directly from physical type id
36 target_unit = Unit._from_physical_type_id(physical_type_id)
37
38 allowed_units.append(target_unit)
39
40 return allowed_units
41
42
43 def _validate_arg_value(param_name, func_name, arg, targets, equivalencies):
44 """
45 Validates the object passed in to the wrapped function, ``arg``, with target
46 unit or physical type, ``target``.
47 """
48
49 allowed_units = _get_allowed_units(targets)
50
51 for allowed_unit in allowed_units:
52 try:
53 is_equivalent = arg.unit.is_equivalent(allowed_unit,
54 equivalencies=equivalencies)
55
56 if is_equivalent:
57 break
58
59 except AttributeError: # Either there is no .unit or no .is_equivalent
60 if hasattr(arg, "unit"):
61 error_msg = "a 'unit' attribute without an 'is_equivalent' method"
62 else:
63 error_msg = "no 'unit' attribute"
64
65 raise TypeError("Argument '{0}' to function '{1}' has {2}. "
66 "You may want to pass in an astropy Quantity instead."
67 .format(param_name, func_name, error_msg))
68
69 else:
70 if len(targets) > 1:
71 raise UnitsError("Argument '{0}' to function '{1}' must be in units"
72 " convertible to one of: {2}."
73 .format(param_name, func_name,
74 [str(targ) for targ in targets]))
75 else:
76 raise UnitsError("Argument '{0}' to function '{1}' must be in units"
77 " convertible to '{2}'."
78 .format(param_name, func_name,
79 str(targets[0])))
80
81
82 class QuantityInput:
83
84 @classmethod
85 def as_decorator(cls, func=None, **kwargs):
86 r"""
87 A decorator for validating the units of arguments to functions.
88
89 Unit specifications can be provided as keyword arguments to the decorator,
90 or by using function annotation syntax. Arguments to the decorator
91 take precedence over any function annotations present.
92
93 A `~astropy.units.UnitsError` will be raised if the unit attribute of
94 the argument is not equivalent to the unit specified to the decorator
95 or in the annotation.
96 If the argument has no unit attribute, i.e. it is not a Quantity object, a
97 `ValueError` will be raised.
98
99 Where an equivalency is specified in the decorator, the function will be
100 executed with that equivalency in force.
101
102 Notes
103 -----
104
105 The checking of arguments inside variable arguments to a function is not
106 supported (i.e. \*arg or \**kwargs).
107
108 Examples
109 --------
110
111 .. code-block:: python
112
113 import astropy.units as u
114 @u.quantity_input(myangle=u.arcsec)
115 def myfunction(myangle):
116 return myangle**2
117
118
119 .. code-block:: python
120
121 import astropy.units as u
122 @u.quantity_input
123 def myfunction(myangle: u.arcsec):
124 return myangle**2
125
126 Also you can specify a return value annotation, which will
127 cause the function to always return a `~astropy.units.Quantity` in that
128 unit.
129
130 .. code-block:: python
131
132 import astropy.units as u
133 @u.quantity_input
134 def myfunction(myangle: u.arcsec) -> u.deg**2:
135 return myangle**2
136
137 Using equivalencies::
138
139 import astropy.units as u
140 @u.quantity_input(myenergy=u.eV, equivalencies=u.mass_energy())
141 def myfunction(myenergy):
142 return myenergy**2
143
144 """
145 self = cls(**kwargs)
146 if func is not None and not kwargs:
147 return self(func)
148 else:
149 return self
150
151 def __init__(self, func=None, **kwargs):
152 self.equivalencies = kwargs.pop('equivalencies', [])
153 self.decorator_kwargs = kwargs
154
155 def __call__(self, wrapped_function):
156
157 # Extract the function signature for the function we are wrapping.
158 wrapped_signature = inspect.signature(wrapped_function)
159
160 # Define a new function to return in place of the wrapped one
161 @wraps(wrapped_function)
162 def wrapper(*func_args, **func_kwargs):
163 # Bind the arguments to our new function to the signature of the original.
164 bound_args = wrapped_signature.bind(*func_args, **func_kwargs)
165
166 # Iterate through the parameters of the original signature
167 for param in wrapped_signature.parameters.values():
168 # We do not support variable arguments (*args, **kwargs)
169 if param.kind in (inspect.Parameter.VAR_KEYWORD,
170 inspect.Parameter.VAR_POSITIONAL):
171 continue
172
173 # Catch the (never triggered) case where bind relied on a default value.
174 if param.name not in bound_args.arguments and param.default is not param.empty:
175 bound_args.arguments[param.name] = param.default
176
177 # Get the value of this parameter (argument to new function)
178 arg = bound_args.arguments[param.name]
179
180 # Get target unit or physical type, either from decorator kwargs
181 # or annotations
182 if param.name in self.decorator_kwargs:
183 targets = self.decorator_kwargs[param.name]
184 else:
185 targets = param.annotation
186
187 # If the targets is empty, then no target units or physical
188 # types were specified so we can continue to the next arg
189 if targets is inspect.Parameter.empty:
190 continue
191
192 # If the argument value is None, and the default value is None,
193 # pass through the None even if there is a target unit
194 if arg is None and param.default is None:
195 continue
196
197 # Here, we check whether multiple target unit/physical type's
198 # were specified in the decorator/annotation, or whether a
199 # single string (unit or physical type) or a Unit object was
200 # specified
201 if isinstance(targets, str) or not isiterable(targets):
202 valid_targets = [targets]
203
204 # Check for None in the supplied list of allowed units and, if
205 # present and the passed value is also None, ignore.
206 elif None in targets:
207 if arg is None:
208 continue
209 else:
210 valid_targets = [t for t in targets if t is not None]
211
212 else:
213 valid_targets = targets
214
215 # Now we loop over the allowed units/physical types and validate
216 # the value of the argument:
217 _validate_arg_value(param.name, wrapped_function.__name__,
218 arg, valid_targets, self.equivalencies)
219
220 # Call the original function with any equivalencies in force.
221 with add_enabled_equivalencies(self.equivalencies):
222 return_ = wrapped_function(*func_args, **func_kwargs)
223 if wrapped_signature.return_annotation is not inspect.Signature.empty:
224 return return_.to(wrapped_signature.return_annotation)
225 else:
226 return return_
227
228 return wrapper
229
230
231 quantity_input = QuantityInput.as_decorator
232
[end of astropy/units/decorators.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/astropy/units/decorators.py +++ b/astropy/units/decorators.py @@ -223,4 +223,8 @@ - if wrapped_signature.return_annotation is not inspect.Signature.empty: - return return_.to(wrapped_signature.return_annotation) - else: - return return_ + # Skip unit conversion if return annotation is None + if wrapped_signature.return_annotation is not inspect.Signature.empty: + if wrapped_signature.return_annotation is not None: + return return_.to(wrapped_signature.return_annotation) + else: + return return_ + else: + return return_
True
swebench/sweb.eval.x86_64.astropy_1776_astropy-7336:latest
astropy__astropy-7671
unresolved
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
minversionの失敗
PR #7647 で導入された変更により、`minversion` が特定のケースで失敗するようになりました。例えば以下の例が該当します:
```
>>> from astropy.utils import minversion
>>> minversion('numpy', '1.14dev')
TypeError Traceback (most recent call last)
<ipython-input-1-760e6b1c375e> in <module>()
1 from astropy.utils import minversion
----> 2 minversion('numpy', '1.14dev')
~/dev/astropy/astropy/utils/introspection.py in minversion(module, version, inclusive, version_path)
144
145 if inclusive:
--> 146 return LooseVersion(have_version) >= LooseVersion(version)
147 else:
148 return LooseVersion(have_version) > LooseVersion(version)
~/local/conda/envs/photutils-dev/lib/python3.6/distutils/version.py in __ge__(self, other)
68
69 def __ge__(self, other):
---> 70 c = self._cmp(other)
71 if c is NotImplemented:
72 return c
~/local/conda/envs/photutils-dev/lib/python3.6/distutils/version.py in _cmp(self, other)
335 if self.version == other.version:
336 return 0
--> 337 if self.version < other.version:
338 return -1
339 if self.version > other.version:
TypeError: '<' not supported between instances of 'int' and 'str'
```
これはどうやらLooseVersionのバグ(https://bugs.python.org/issue30272)が原因のようです:
```
>>> from distutils.version import LooseVersion
>>> LooseVersion('1.14.3') >= LooseVersion('1.14dev')
...
TypeError: '<' not supported between instances of 'int' and 'str'
```
".3"がない場合は失敗しないことも確認してください:
```
>>> LooseVersion('1.14') >= LooseVersion('1.14dev')
False
```
また、以前使用されていた`pkg_resources.parse_version`(現存しない)では正常に動作します:
```
>>> from pkg_resources import parse_version
>>> parse_version('1.14.3') >= parse_version('1.14dev')
True
```
CC: @mhvk
</issue>
<code>
[start of README.rst]
1 =======
2 Astropy
3 =======
4
5 .. image:: https://img.shields.io/pypi/v/astropy.svg
6 :target: https://pypi.python.org/pypi/astropy
7
8 Astropy (http://www.astropy.org) is a package intended to contain much of
9 the core functionality and some common tools needed for performing
10 astronomy and astrophysics with Python.
11
12 Releases are `registered on PyPI <http://pypi.python.org/pypi/astropy>`_,
13 and development is occurring at the
14 `project's github page <http://github.com/astropy/astropy>`_.
15
16 For installation instructions, see the `online documentation <http://docs.astropy.org/>`_
17 or ``docs/install.rst`` in this source distribution.
18
19 For system packagers: Please install Astropy with the command::
20
21 $ python setup.py --offline install
22
23 This will prevent the astropy_helpers bootstrap script from attempting to
24 reach out to PyPI.
25
26 Project Status
27 --------------
28
29 .. image:: https://travis-ci.org/astropy/astropy.svg
30 :target: https://travis-ci.org/astropy/astropy
31 :alt: Astropy's Travis CI Status
32
33 .. image:: https://coveralls.io/repos/astropy/astropy/badge.svg
34 :target: https://coveralls.io/r/astropy/astropy
35 :alt: Astropy's Coveralls Status
36
37 .. image:: https://ci.appveyor.com/api/projects/status/ym7lxajcs5qwm31e/branch/master?svg=true
38 :target: https://ci.appveyor.com/project/Astropy/astropy/branch/master
39 :alt: Astropy's Appveyor Status
40
41 For an overview of the testing and build status of all packages associated
42 with the Astropy Project, see http://dashboard.astropy.org.
43
44 .. image:: https://img.shields.io/badge/powered%20by-NumFOCUS-orange.svg?style=flat&colorA=E1523D&colorB=007D8A
45 :target: http://numfocus.org
46 :alt: Powered by NumFOCUS
47
48
49 Contributing Code, Documentation, or Feedback
50 ---------------------------------------------
51 The Astropy project is made both by and for its users, so we welcome and encourage
52 contributions of many kinds. Our goal is to keep this a positive, inclusive,
53 successful, and growing community, by abiding with the
54 `Astropy Community Code of Conduct <http://www.astropy.org/about.html#codeofconduct>`_.
55
56 More detailed information on contributing to the project or submitting feedback
57 can be found on the `contributions <http://www.astropy.org/contribute.html>`_ page.
58
59 A `summary of contribution guidelines <CONTRIBUTING.md>`_ can also be used as a quick
60 reference when you're ready to start writing or validating code for submission.
61
62 License
63 -------
64 Astropy is licensed under a 3-clause BSD style license - see the
65 ``LICENSE.rst`` file.
66
[end of README.rst]
[start of astropy/utils/introspection.py]
1 # -*- coding: utf-8 -*-
2 # Licensed under a 3-clause BSD style license - see LICENSE.rst
3 """Functions related to Python runtime introspection."""
4
5
6 import inspect
7 import types
8 import importlib
9 from distutils.version import LooseVersion
10
11
12 __all__ = ['resolve_name', 'minversion', 'find_current_module',
13 'isinstancemethod']
14
15
16 __doctest_skip__ = ['find_current_module']
17
18
19 def resolve_name(name, *additional_parts):
20 """Resolve a name like ``module.object`` to an object and return it.
21
22 This ends up working like ``from module import object`` but is easier
23 to deal with than the `__import__` builtin and supports digging into
24 submodules.
25
26 Parameters
27 ----------
28
29 name : `str`
30 A dotted path to a Python object--that is, the name of a function,
31 class, or other object in a module with the full path to that module,
32 including parent modules, separated by dots. Also known as the fully
33 qualified name of the object.
34
35 additional_parts : iterable, optional
36 If more than one positional arguments are given, those arguments are
37 automatically dotted together with ``name``.
38
39 Examples
40 --------
41
42 >>> resolve_name('astropy.utils.introspection.resolve_name')
43 <function resolve_name at 0x...>
44 >>> resolve_name('astropy', 'utils', 'introspection', 'resolve_name')
45 <function resolve_name at 0x...>
46
47 Raises
48 ------
49 `ImportError`
50 If the module or named object is not found.
51 """
52
53 additional_parts = '.'.join(additional_parts)
54
55 if additional_parts:
56 name = name + '.' + additional_parts
57
58 parts = name.split('.')
59
60 if len(parts) == 1:
61 # No dots in the name--just a straight up module import
62 cursor = 1
63 fromlist = []
64 else:
65 cursor = len(parts) - 1
66 fromlist = [parts[-1]]
67
68 module_name = parts[:cursor]
69
70 while cursor > 0:
71 try:
72 ret = __import__(str('.'.join(module_name)), fromlist=fromlist)
73 break
74 except ImportError:
75 if cursor == 0:
76 raise
77 cursor -= 1
78 module_name = parts[:cursor]
79 fromlist = [parts[cursor]]
80 ret = ''
81
82 for part in parts[cursor:]:
83 try:
84 ret = getattr(ret, part)
85 except AttributeError:
86 raise ImportError(name)
87
88 return ret
89
90
91 def minversion(module, version, inclusive=True, version_path='__version__'):
92 """
93 Returns `True` if the specified Python module satisfies a minimum version
94 requirement, and `False` if not.
95
96 Parameters
97 ----------
98
99 module : module or `str`
100 An imported module of which to check the version, or the name of
101 that module (in which case an import of that module is attempted--
102 if this fails `False` is returned).
103
104 version : `str`
105 The version as a string that this module must have at a minimum (e.g.
106 ``'0.12'``).
107
108 inclusive : `bool`
109 The specified version meets the requirement inclusively (i.e. ``>=``)
110 as opposed to strictly greater than (default: `True`).
111
112 version_path : `str`
113 A dotted attribute path to follow in the module for the version.
114 Defaults to just ``'__version__'``, which should work for most Python
115 modules.
116
117 Examples
118 --------
119
120 >>> import astropy
121 >>> minversion(astropy, '0.4.4')
122 True
123 """
124 if isinstance(module, types.ModuleType):
125 module_name = module.__name__
126 elif isinstance(module, str):
127 module_name = module
128 try:
129 module = resolve_name(module_name)
130 except ImportError:
131 return False
132 else:
133 raise ValueError('module argument must be an actual imported '
134 'module, or the import name of the module; '
135 'got {0!r}'.format(module))
136
137 if '.' not in version_path:
138 have_version = getattr(module, version_path)
139 else:
140 have_version = resolve_name(module.__name__, version_path)
141
142 if inclusive:
143 return LooseVersion(have_version) >= LooseVersion(version)
144 else:
145 return LooseVersion(have_version) > LooseVersion(version)
146
147
148 def find_current_module(depth=1, finddiff=False):
149 """
150 Determines the module/package from which this function is called.
151
152 This function has two modes, determined by the ``finddiff`` option. it
153 will either simply go the requested number of frames up the call
154 stack (if ``finddiff`` is False), or it will go up the call stack until
155 it reaches a module that is *not* in a specified set.
156
157 Parameters
158 ----------
159 depth : int
160 Specifies how far back to go in the call stack (0-indexed, so that
161 passing in 0 gives back `astropy.utils.misc`).
162 finddiff : bool or list
163 If False, the returned ``mod`` will just be ``depth`` frames up from
164 the current frame. Otherwise, the function will start at a frame
165 ``depth`` up from current, and continue up the call stack to the
166 first module that is *different* from those in the provided list.
167 In this case, ``finddiff`` can be a list of modules or modules
168 names. Alternatively, it can be True, which will use the module
169 ``depth`` call stack frames up as the module the returned module
170 most be different from.
171
172 Returns
173 -------
174 mod : module or None
175 The module object or None if the package cannot be found. The name of
176 the module is available as the ``__name__`` attribute of the returned
177 object (if it isn't None).
178
179 Raises
180 ------
181 ValueError
182 If ``finddiff`` is a list with an invalid entry.
183
184 Examples
185 --------
186 The examples below assume that there are two modules in a package named
187 ``pkg``. ``mod1.py``::
188
189 def find1():
190 from astropy.utils import find_current_module
191 print find_current_module(1).__name__
192 def find2():
193 from astropy.utils import find_current_module
194 cmod = find_current_module(2)
195 if cmod is None:
196 print 'None'
197 else:
198 print cmod.__name__
199 def find_diff():
200 from astropy.utils import find_current_module
201 print find_current_module(0,True).__name__
202
203 ``mod2.py``::
204
205 def find():
206 from .mod1 import find2
207 find2()
208
209 With these modules in place, the following occurs::
210
211 >>> from pkg import mod1, mod2
212 >>> from astropy.utils import find_current_module
213 >>> mod1.find1()
214 pkg.mod1
215 >>> mod1.find2()
216 None
217 >>> mod2.find()
218 pkg.mod2
219 >>> find_current_module(0)
220 <module 'astropy.utils.misc' from 'astropy/utils/misc.py'>
221 >>> mod1.find_diff()
222 pkg.mod1
223
224 """
225
226 frm = inspect.currentframe()
227 for i in range(depth):
228 frm = frm.f_back
229 if frm is None:
230 return None
231
232 if finddiff:
233 currmod = inspect.getmodule(frm)
234 if finddiff is True:
235 diffmods = [currmod]
236 else:
237 diffmods = []
238 for fd in finddiff:
239 if inspect.ismodule(fd):
240 diffmods.append(fd)
241 elif isinstance(fd, str):
242 diffmods.append(importlib.import_module(fd))
243 elif fd is True:
244 diffmods.append(currmod)
245 else:
246 raise ValueError('invalid entry in finddiff')
247
248 while frm:
249 frmb = frm.f_back
250 modb = inspect.getmodule(frmb)
251 if modb not in diffmods:
252 return modb
253 frm = frmb
254 else:
255 return inspect.getmodule(frm)
256
257
258 def find_mod_objs(modname, onlylocals=False):
259 """ Returns all the public attributes of a module referenced by name.
260
261 .. note::
262 The returned list *not* include subpackages or modules of
263 ``modname``, nor does it include private attributes (those that
264 begin with '_' or are not in `__all__`).
265
266 Parameters
267 ----------
268 modname : str
269 The name of the module to search.
270 onlylocals : bool or list of str
271 If `True`, only attributes that are either members of ``modname`` OR
272 one of its modules or subpackages will be included. If it is a list
273 of strings, those specify the possible packages that will be
274 considered "local".
275
276 Returns
277 -------
278 localnames : list of str
279 A list of the names of the attributes as they are named in the
280 module ``modname`` .
281 fqnames : list of str
282 A list of the full qualified names of the attributes (e.g.,
283 ``astropy.utils.introspection.find_mod_objs``). For attributes that are
284 simple variables, this is based on the local name, but for functions or
285 classes it can be different if they are actually defined elsewhere and
286 just referenced in ``modname``.
287 objs : list of objects
288 A list of the actual attributes themselves (in the same order as
289 the other arguments)
290
291 """
292
293 mod = resolve_name(modname)
294
295 if hasattr(mod, '__all__'):
296 pkgitems = [(k, mod.__dict__[k]) for k in mod.__all__]
297 else:
298 pkgitems = [(k, mod.__dict__[k]) for k in dir(mod) if k[0] != '_']
299
300 # filter out modules and pull the names and objs out
301 ismodule = inspect.ismodule
302 localnames = [k for k, v in pkgitems if not ismodule(v)]
303 objs = [v for k, v in pkgitems if not ismodule(v)]
304
305 # fully qualified names can be determined from the object's module
306 fqnames = []
307 for obj, lnm in zip(objs, localnames):
308 if hasattr(obj, '__module__') and hasattr(obj, '__name__'):
309 fqnames.append(obj.__module__ + '.' + obj.__name__)
310 else:
311 fqnames.append(modname + '.' + lnm)
312
313 if onlylocals:
314 if onlylocals is True:
315 onlylocals = [modname]
316 valids = [any(fqn.startswith(nm) for nm in onlylocals) for fqn in fqnames]
317 localnames = [e for i, e in enumerate(localnames) if valids[i]]
318 fqnames = [e for i, e in enumerate(fqnames) if valids[i]]
319 objs = [e for i, e in enumerate(objs) if valids[i]]
320
321 return localnames, fqnames, objs
322
323
324 # Note: I would have preferred call this is_instancemethod, but this naming is
325 # for consistency with other functions in the `inspect` module
326 def isinstancemethod(cls, obj):
327 """
328 Returns `True` if the given object is an instance method of the class
329 it is defined on (as opposed to a `staticmethod` or a `classmethod`).
330
331 This requires both the class the object is a member of as well as the
332 object itself in order to make this determination.
333
334 Parameters
335 ----------
336 cls : `type`
337 The class on which this method was defined.
338 obj : `object`
339 A member of the provided class (the membership is not checked directly,
340 but this function will always return `False` if the given object is not
341 a member of the given class).
342
343 Examples
344 --------
345 >>> class MetaClass(type):
346 ... def a_classmethod(cls): pass
347 ...
348 >>> class MyClass(metaclass=MetaClass):
349 ... def an_instancemethod(self): pass
350 ...
351 ... @classmethod
352 ... def another_classmethod(cls): pass
353 ...
354 ... @staticmethod
355 ... def a_staticmethod(): pass
356 ...
357 >>> isinstancemethod(MyClass, MyClass.a_classmethod)
358 False
359 >>> isinstancemethod(MyClass, MyClass.another_classmethod)
360 False
361 >>> isinstancemethod(MyClass, MyClass.a_staticmethod)
362 False
363 >>> isinstancemethod(MyClass, MyClass.an_instancemethod)
364 True
365 """
366
367 return _isinstancemethod(cls, obj)
368
369
370 def _isinstancemethod(cls, obj):
371 if not isinstance(obj, types.FunctionType):
372 return False
373
374 # Unfortunately it seems the easiest way to get to the original
375 # staticmethod object is to look in the class's __dict__, though we
376 # also need to look up the MRO in case the method is not in the given
377 # class's dict
378 name = obj.__name__
379 for basecls in cls.mro(): # This includes cls
380 if name in basecls.__dict__:
381 return not isinstance(basecls.__dict__[name], staticmethod)
382
383 # This shouldn't happen, though this is the most sensible response if
384 # it does.
385 raise AttributeError(name)
386
[end of astropy/utils/introspection.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/astropy/utils/introspection.py
+++ b/astropy/utils/introspection.py
@@ -11,1 +11,1 @@
-from distutils.version import LooseVersion
+from packaging.version import parse as parse_version
@@ -143,3 +143,3 @@
- return LooseVersion(have_version) >= LooseVersion(version)
+ return parse_version(have_version) >= parse_version(version)
else:
- return LooseVersion(have_version) > LooseVersion(version)
+ return parse_version(have_version) > parse_version(version)
True
swebench/sweb.eval.x86_64.astropy_1776_astropy-7671:latest
django__django-10880
unresolved
You will be provided with a partial code base and an issue statement explaining a problem to resolve.
<issue>
# 条件とdistinctの組み合わせによるクエリ構文エラー
## 説明
`Count`注釈に`Case`条件と`distinct=True`パラメータを同時に含めると、Django 2.2でクエリエラーが発生します(DBバックエンドに関わらず)。少なくともスペースが不足しています(... COUNT(DISTINCTCASE WHEN ...))。
</issue>
<code>
[start of README.rst]
1 Django is a high-level Python Web framework that encourages rapid development
2 and clean, pragmatic design. Thanks for checking it out.
3
4 All documentation is in the "``docs``" directory and online at
5 https://docs.djangoproject.com/en/stable/. If you're just getting started,
6 here's how we recommend you read the docs:
7
8 * First, read ``docs/intro/install.txt`` for instructions on installing Django.
9
10 * Next, work through the tutorials in order (``docs/intro/tutorial01.txt``,
11 ``docs/intro/tutorial02.txt``, etc.).
12
13 * If you want to set up an actual deployment server, read
14 ``docs/howto/deployment/index.txt`` for instructions.
15
16 * You'll probably want to read through the topical guides (in ``docs/topics``)
17 next; from there you can jump to the HOWTOs (in ``docs/howto``) for specific
18 problems, and check out the reference (``docs/ref``) for gory details.
19
20 * See ``docs/README`` for instructions on building an HTML version of the docs.
21
22 Docs are updated rigorously. If you find any problems in the docs, or think
23 they should be clarified in any way, please take 30 seconds to fill out a
24 ticket here: https://code.djangoproject.com/newticket
25
26 To get more help:
27
28 * Join the ``#django`` channel on irc.freenode.net. Lots of helpful people hang
29 out there. See https://en.wikipedia.org/wiki/Wikipedia:IRC/Tutorial if you're
30 new to IRC.
31
32 * Join the django-users mailing list, or read the archives, at
33 https://groups.google.com/group/django-users.
34
35 To contribute to Django:
36
37 * Check out https://docs.djangoproject.com/en/dev/internals/contributing/ for
38 information about getting involved.
39
40 To run Django's test suite:
41
42 * Follow the instructions in the "Unit tests" section of
43 ``docs/internals/contributing/writing-code/unit-tests.txt``, published online at
44 https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/#running-the-unit-tests
45
[end of README.rst]
[start of django/db/models/aggregates.py]
1 """
2 Classes to represent the definitions of aggregate functions.
3 """
4 from django.core.exceptions import FieldError
5 from django.db.models.expressions import Case, Func, Star, When
6 from django.db.models.fields import IntegerField
7 from django.db.models.functions.mixins import (
8 FixDurationInputMixin, NumericOutputFieldMixin,
9 )
10
11 __all__ = [
12 'Aggregate', 'Avg', 'Count', 'Max', 'Min', 'StdDev', 'Sum', 'Variance',
13 ]
14
15
16 class Aggregate(Func):
17 template = '%(function)s(%(distinct)s%(expressions)s)'
18 contains_aggregate = True
19 name = None
20 filter_template = '%s FILTER (WHERE %%(filter)s)'
21 window_compatible = True
22 allow_distinct = False
23
24 def __init__(self, *expressions, distinct=False, filter=None, **extra):
25 if distinct and not self.allow_distinct:
26 raise TypeError("%s does not allow distinct." % self.__class__.__name__)
27 self.distinct = distinct
28 self.filter = filter
29 super().__init__(*expressions, **extra)
30
31 def get_source_fields(self):
32 # Don't return the filter expression since it's not a source field.
33 return [e._output_field_or_none for e in super().get_source_expressions()]
34
35 def get_source_expressions(self):
36 source_expressions = super().get_source_expressions()
37 if self.filter:
38 return source_expressions + [self.filter]
39 return source_expressions
40
41 def set_source_expressions(self, exprs):
42 self.filter = self.filter and exprs.pop()
43 return super().set_source_expressions(exprs)
44
45 def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
46 # Aggregates are not allowed in UPDATE queries, so ignore for_save
47 c = super().resolve_expression(query, allow_joins, reuse, summarize)
48 c.filter = c.filter and c.filter.resolve_expression(query, allow_joins, reuse, summarize)
49 if not summarize:
50 # Call Aggregate.get_source_expressions() to avoid
51 # returning self.filter and including that in this loop.
52 expressions = super(Aggregate, c).get_source_expressions()
53 for index, expr in enumerate(expressions):
54 if expr.contains_aggregate:
55 before_resolved = self.get_source_expressions()[index]
56 name = before_resolved.name if hasattr(before_resolved, 'name') else repr(before_resolved)
57 raise FieldError("Cannot compute %s('%s'): '%s' is an aggregate" % (c.name, name, name))
58 return c
59
60 @property
61 def default_alias(self):
62 expressions = self.get_source_expressions()
63 if len(expressions) == 1 and hasattr(expressions[0], 'name'):
64 return '%s__%s' % (expressions[0].name, self.name.lower())
65 raise TypeError("Complex expressions require an alias")
66
67 def get_group_by_cols(self):
68 return []
69
70 def as_sql(self, compiler, connection, **extra_context):
71 extra_context['distinct'] = 'DISTINCT' if self.distinct else ''
72 if self.filter:
73 if connection.features.supports_aggregate_filter_clause:
74 filter_sql, filter_params = self.filter.as_sql(compiler, connection)
75 template = self.filter_template % extra_context.get('template', self.template)
76 sql, params = super().as_sql(
77 compiler, connection, template=template, filter=filter_sql,
78 **extra_context
79 )
80 return sql, params + filter_params
81 else:
82 copy = self.copy()
83 copy.filter = None
84 source_expressions = copy.get_source_expressions()
85 condition = When(self.filter, then=source_expressions[0])
86 copy.set_source_expressions([Case(condition)] + source_expressions[1:])
87 return super(Aggregate, copy).as_sql(compiler, connection, **extra_context)
88 return super().as_sql(compiler, connection, **extra_context)
89
90 def _get_repr_options(self):
91 options = super()._get_repr_options()
92 if self.distinct:
93 options['distinct'] = self.distinct
94 if self.filter:
95 options['filter'] = self.filter
96 return options
97
98
99 class Avg(FixDurationInputMixin, NumericOutputFieldMixin, Aggregate):
100 function = 'AVG'
101 name = 'Avg'
102
103
104 class Count(Aggregate):
105 function = 'COUNT'
106 name = 'Count'
107 output_field = IntegerField()
108 allow_distinct = True
109
110 def __init__(self, expression, filter=None, **extra):
111 if expression == '*':
112 expression = Star()
113 if isinstance(expression, Star) and filter is not None:
114 raise ValueError('Star cannot be used with filter. Please specify a field.')
115 super().__init__(expression, filter=filter, **extra)
116
117 def convert_value(self, value, expression, connection):
118 return 0 if value is None else value
119
120
121 class Max(Aggregate):
122 function = 'MAX'
123 name = 'Max'
124
125
126 class Min(Aggregate):
127 function = 'MIN'
128 name = 'Min'
129
130
131 class StdDev(NumericOutputFieldMixin, Aggregate):
132 name = 'StdDev'
133
134 def __init__(self, expression, sample=False, **extra):
135 self.function = 'STDDEV_SAMP' if sample else 'STDDEV_POP'
136 super().__init__(expression, **extra)
137
138 def _get_repr_options(self):
139 return {**super()._get_repr_options(), 'sample': self.function == 'STDDEV_SAMP'}
140
141
142 class Sum(FixDurationInputMixin, Aggregate):
143 function = 'SUM'
144 name = 'Sum'
145
146
147 class Variance(NumericOutputFieldMixin, Aggregate):
148 name = 'Variance'
149
150 def __init__(self, expression, sample=False, **extra):
151 self.function = 'VAR_SAMP' if sample else 'VAR_POP'
152 super().__init__(expression, **extra)
153
154 def _get_repr_options(self):
155 return {**super()._get_repr_options(), 'sample': self.function == 'VAR_SAMP'}
156
[end of django/db/models/aggregates.py]
</code>
Here is an example of a patch file. It consists of changes to the code base. It specifies the file names, the line numbers of each change, and the removed and added lines. A single patch file can contain changes to multiple files.
<patch>
--- a/file.py
+++ b/file.py
@@ -1,27 +1,35 @@
def euclidean(a, b):
- while b:
- a, b = b, a % b
- return a
+ if b == 0:
+ return a
+ return euclidean(b, a % b)
def bresenham(x0, y0, x1, y1):
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
- sx = 1 if x0 < x1 else -1
- sy = 1 if y0 < y1 else -1
- err = dx - dy
+ x, y = x0, y0
+ sx = -1 if x0 > x1 else 1
+ sy = -1 if y0 > y1 else 1
- while True:
- points.append((x0, y0))
- if x0 == x1 and y0 == y1:
- break
- e2 = 2 * err
- if e2 > -dy:
+ if dx > dy:
+ err = dx / 2.0
+ while x != x1:
+ points.append((x, y))
err -= dy
- x0 += sx
- if e2 < dx:
- err += dx
- y0 += sy
+ if err < 0:
+ y += sy
+ err += dx
+ x += sx
+ else:
+ err = dy / 2.0
+ while y != y1:
+ points.append((x, y))
+ err -= dx
+ if err < 0:
+ x += sx
+ err += dy
+ y += sy
+ points.append((x, y))
return points
</patch>
CRITICAL: When generating the patch, you MUST include proper line numbers in the @@ hunk headers. Each hunk header must follow the format @@ -start,count +start,count @@ where start is the line number and count is the number of lines. Do NOT use @@ @@ without line numbers.
I need you to solve the provided issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the format shown above.
Respond below:
--- a/django/db/models/aggregates.py
+++ b/django/db/models/aggregates.py
@@ -19,2 +19,2 @@
- template = '%(function)s(%(distinct)s%(expressions)s)'
+ template = '%(function)s(%(distinct)s %(expressions)s)'
contains_aggregate = True
True
swebench/sweb.eval.x86_64.django_1776_django-10880:latest
instance_id
status
input
patch
image_available
image_name
1
2
3
4
5
6