Skip to content

MAINT Use np.full when possible #12379

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

Merged
merged 6 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,7 @@ def dopatch(xs, ys, **kwargs):
# maybe draw the fliers
if showfliers:
# fliers coords
flier_x = np.ones(len(stats['fliers'])) * pos
flier_x = np.full(len(stats['fliers']), pos, dtype=np.float64)
flier_y = stats['fliers']

fliers.extend(doplot(
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def __init__(self, hatch, density):
self.num_rows = (hatch.count('*')) * density
path = Path.unit_regular_star(5)
self.shape_vertices = path.vertices
self.shape_codes = np.ones(len(self.shape_vertices)) * Path.LINETO
self.shape_codes = np.full(len(self.shape_vertices), Path.LINETO,
dtype=Path.code_type)
self.shape_codes[0] = Path.MOVETO
Shapes.__init__(self, hatch, density)

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def create_artists(self, legend, orig_handle,
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
width, height, fontsize)

ydata = ((height - ydescent) / 2.) * np.ones(xdata.shape, float)
ydata = np.full_like(xdata, ((height - ydescent) / 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify dtype?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying xdata's dtype (which full_like does) seems normal.

legline = Line2D(xdata, ydata)

self.update_prop(legline, orig_handle, legend)
Expand Down
11 changes: 6 additions & 5 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def make_compound_path_from_polys(cls, XY):
stride = numsides + 1
nverts = numpolys * stride
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * cls.LINETO
codes = np.full(nverts, cls.LINETO, dtype=cls.code_type)
codes[0::stride] = cls.MOVETO
codes[numsides::stride] = cls.CLOSEPOLY
for i in range(numsides):
Expand Down Expand Up @@ -552,7 +552,8 @@ def interpolated(self, steps):
vertices = simple_linear_interpolation(self.vertices, steps)
codes = self.codes
if codes is not None:
new_codes = Path.LINETO * np.ones(((len(codes) - 1) * steps + 1, ))
new_codes = np.full((len(codes) - 1) * steps + 1, Path.LINETO,
dtype=self.code_type)
new_codes[0::steps] = codes
else:
new_codes = None
Expand Down Expand Up @@ -802,7 +803,7 @@ def unit_circle_righthalf(cls):

float)

codes = cls.CURVE4 * np.ones(14)
codes = np.full(14, cls.CURVE4, dtype=cls.code_type)
codes[0] = cls.MOVETO
codes[-1] = cls.CLOSEPOLY

Expand Down Expand Up @@ -864,7 +865,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
if is_wedge:
length = n * 3 + 4
vertices = np.zeros((length, 2), float)
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
codes = np.full(length, cls.CURVE4, dtype=cls.code_type)
vertices[1] = [xA[0], yA[0]]
codes[0:2] = [cls.MOVETO, cls.LINETO]
codes[-2:] = [cls.LINETO, cls.CLOSEPOLY]
Expand All @@ -873,7 +874,7 @@ def arc(cls, theta1, theta2, n=None, is_wedge=False):
else:
length = n * 3 + 1
vertices = np.empty((length, 2), float)
codes = cls.CURVE4 * np.ones((length, ), cls.code_type)
codes = np.full(length, cls.CURVE4, dtype=cls.code_type)
vertices[0] = [xA[0], yA[0]]
codes[0] = cls.MOVETO
vertex_offset = 1
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tri/triinterpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ def get_Kff_and_Ff(self, J, ecc, triangles, Uc):
"""
ntri = np.size(ecc, 0)
vec_range = np.arange(ntri, dtype=np.int32)
c_indices = -np.ones(ntri, dtype=np.int32) # for unused dofs, -1
c_indices = np.full(ntri, -1, dtype=np.int32) # for unused dofs, -1
f_dof = [1, 2, 4, 5, 7, 8]
c_dof = [0, 3, 6]

Expand Down
10 changes: 3 additions & 7 deletions lib/matplotlib/tri/trirefine.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def refine_triangulation(self, return_tri_index=False, subdiv=3):
# We have to initialize found_index with -1 because some nodes
# may very well belong to no triangle at all, e.g., in case of
# Delaunay Triangulation with DuplicatePointWarning.
found_index = - np.ones(refi_npts, dtype=np.int32)
found_index = np.full(refi_npts, -1, dtype=np.int32)
tri_mask = self._triangulation.mask
if tri_mask is None:
found_index[refi_triangles] = np.repeat(ancestors,
Expand Down Expand Up @@ -238,12 +238,8 @@ def _refine_triangulation_once(triangulation, ancestors=None):
# (can be -1 if border edge)
# For slave and master we will identify the apex pointing to the edge
# start
edge_elems = np.ravel(np.vstack([np.arange(ntri, dtype=np.int32),
np.arange(ntri, dtype=np.int32),
np.arange(ntri, dtype=np.int32)]))
edge_apexes = np.ravel(np.vstack([np.zeros(ntri, dtype=np.int32),
np.ones(ntri, dtype=np.int32),
np.ones(ntri, dtype=np.int32)*2]))
edge_elems = np.tile(np.arange(ntri, dtype=np.int32), 3)
edge_apexes = np.repeat(np.arange(3, dtype=np.int32), ntri)
edge_neighbors = neighbors[edge_elems, edge_apexes]
mask_masters = (edge_elems > edge_neighbors)

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tri/tritools.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _total_to_compress_renum(mask, n=None):
if n is None:
n = np.size(mask)
if mask is not None:
renum = -np.ones(n, dtype=np.int32) # Default num is -1
renum = np.full(n, -1, dtype=np.int32) # Default num is -1
valid = np.arange(n, dtype=np.int32).compress(~mask, axis=0)
renum[valid] = np.arange(np.size(valid, 0), dtype=np.int32)
return renum
Expand Down