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

Initialize empty lists with None instead of object #416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions dislib/cluster/dbscan/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def _arrange_data(x, g_shape, bins, dims, total_regions):
ind_lists = list()

for row in x._iterator(axis=0):
reg_list = [object() for _ in range(total_regions)]
ind_list = [object() for _ in range(total_regions)]
reg_list = [None] * total_regions
ind_list = [None] * total_regions

# after calling arrange_block, reg_list contains one nd-array per
# region with the corresponding samples, and ind_list contains
Expand Down Expand Up @@ -297,7 +297,7 @@ def _rearrange_labels(labels, indices, n_blocks):
blocks_list = list()

for i, arr in enumerate(labels):
blocks = [object() for _ in range(n_blocks)]
blocks = [None] * n_blocks

# blocks_list[i][j] contains the labels of region i that belong to
# row block j in the original arrangement of the data
Expand Down
24 changes: 12 additions & 12 deletions dislib/data/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def __sub__(self, other):
blocks = []

for hblock in self._iterator("rows"):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, other._blocks,
Array._subtract, out_blocks)
blocks.append(out_blocks)
Expand Down Expand Up @@ -254,7 +254,7 @@ def __isub__(self, other):

for hblock, others in zip(self._iterator("rows"),
other._iterator("rows")):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, others._blocks,
Array._subtract, out_blocks)
blocks.append(out_blocks)
Expand All @@ -266,7 +266,7 @@ def __isub__(self, other):
blocks = []

for hblock in self._iterator("rows"):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, other._blocks,
Array._subtract, out_blocks)
blocks.append(out_blocks)
Expand All @@ -285,7 +285,7 @@ def __add__(self, other):
blocks = []

for hblock in self._iterator("rows"):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, other._blocks,
Array._add, out_blocks)
blocks.append(out_blocks)
Expand Down Expand Up @@ -320,7 +320,7 @@ def __iadd__(self, other):

for hblock, others in zip(self._iterator("rows"),
other._iterator("rows")):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, others._blocks,
Array._add, out_blocks)
blocks.append(out_blocks)
Expand All @@ -332,7 +332,7 @@ def __iadd__(self, other):
blocks = []

for hblock in self._iterator("rows"):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, other._blocks,
Array._add, out_blocks)
blocks.append(out_blocks)
Expand All @@ -356,7 +356,7 @@ def __mul__(self, other):
blocks = []

for hblock in self._iterator("rows"):
out_blocks = [object() for _ in range(hblock._n_blocks[1])]
out_blocks = [None] * hblock._n_blocks[1]
_combine_blocks(hblock._blocks, other._blocks,
operator.mul, out_blocks)
blocks.append(out_blocks)
Expand Down Expand Up @@ -450,7 +450,7 @@ def _get_out_blocks(n_blocks):
Helper function that builds empty lists of lists to be filled as
parameter of type COLLECTION_OUT
"""
return [[object() for _ in range(n_blocks[1])]
return [[None] * n_blocks[1]
for _ in range(n_blocks[0])]

@staticmethod
Expand Down Expand Up @@ -881,7 +881,7 @@ def _get_by_lst_rows(self, rows):
# enough rows to merge into a row_block
if n_rows >= self._reg_shape[0]:
n_blocks = ceil(self.shape[1] / self._reg_shape[1])
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_merge_rows(to_merge, out_blocks, self._reg_shape, skip)
final_blocks.append(out_blocks)

Expand All @@ -898,7 +898,7 @@ def _get_by_lst_rows(self, rows):

if n_rows > 0:
n_blocks = ceil(self.shape[1] / self._reg_shape[1])
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_merge_rows(to_merge, out_blocks, self._reg_shape, skip)
final_blocks.append(out_blocks)

Expand Down Expand Up @@ -948,7 +948,7 @@ def _get_by_lst_cols(self, cols):
# enough cols to merge into a col_block
if n_cols >= self._reg_shape[1]:
n_blocks = ceil(self.shape[0] / self._reg_shape[0])
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_merge_cols([to_merge], out_blocks, self._reg_shape, skip)
final_blocks.append(out_blocks)

Expand All @@ -965,7 +965,7 @@ def _get_by_lst_cols(self, cols):

if n_cols > 0:
n_blocks = ceil(self.shape[0] / self._reg_shape[0])
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_merge_cols([to_merge], out_blocks, self._reg_shape, skip)
final_blocks.append(out_blocks)

Expand Down
10 changes: 5 additions & 5 deletions dislib/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def load_txt_file(path, block_size, discard_first_row=False,
lines.append(line.encode())

if len(lines) == block_size[0]:
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_read_lines(lines, block_size[1], delimiter, out_blocks,
col_of_index=col_of_index)
blocks.append(out_blocks)
lines = []

if lines:
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_read_lines(lines, block_size[1], delimiter, out_blocks,
col_of_index=col_of_index)
blocks.append(out_blocks)
Expand Down Expand Up @@ -170,7 +170,7 @@ def load_npy_file(path, block_size):
read_count = min(block_size[0], shape[0] - i)
read_size = int(read_count * shape[1] * dtype.itemsize)
data = fid.read(read_size)
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_read_from_buffer(data, dtype, shape[1], block_size[1], out_blocks)
blocks.append(out_blocks)

Expand Down Expand Up @@ -507,7 +507,7 @@ def _load_mdcrd_copy(path, block_size, n_cols, n_hblocks, bytes_per_snap,
blocks = []

for i in range(0, file_size, bytes_per_block):
out_blocks = [object() for _ in range(n_hblocks)]
out_blocks = [None] * n_hblocks
_read_crd_file(path, i, bytes_per_block, block_size[1], n_cols,
out_blocks)
blocks.append(out_blocks)
Expand All @@ -530,7 +530,7 @@ def _load_mdcrd(path, block_size, n_cols, n_blocks, bytes_per_snap,

for _ in range(0, file_size, bytes_per_block):
data = fid.read(bytes_per_block)
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_read_crd_bytes(data, block_size[1], n_cols, out_blocks)
blocks.append(out_blocks)
finally:
Expand Down
2 changes: 1 addition & 1 deletion dislib/decomposition/pca/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _transform_eig(self, x):
n_col_blocks = div + (1 if mod else 0)

for rows in x._iterator('rows'):
out_blocks = [object() for _ in range(n_col_blocks)]
out_blocks = [None] * n_col_blocks
_subset_transform(rows._blocks, self.mean_._blocks,
self.components_._blocks, reg_shape, out_blocks)
new_blocks.append(out_blocks)
Expand Down
8 changes: 4 additions & 4 deletions dislib/math/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _compute_u(a):
u_blocks = [[] for _ in range(a._n_blocks[0])]

for vblock in a._iterator("columns"):
u_block = [object() for _ in range(vblock._n_blocks[0])]
u_block = [None] * vblock._n_blocks[0]
_compute_u_block(vblock._blocks, u_block)

for i in range(len(u_block)):
Expand All @@ -232,7 +232,7 @@ def _compute_u_sorted(a, sorting):
hbsize = a._reg_shape[1]

for i, vblock in enumerate(a._iterator("columns")):
u_block = [object() for _ in range(a._n_blocks[1])]
u_block = [None] * a._n_blocks[1]
_compute_u_block_sorted(vblock._blocks, i, hbsize, sorting, u_block)

for j in range(len(u_block)):
Expand All @@ -242,7 +242,7 @@ def _compute_u_sorted(a, sorting):
final_blocks = Array._get_out_blocks(a._n_blocks)

for i, u_block in enumerate(u_blocks):
new_block = [object() for _ in range(a._n_blocks[0])]
new_block = [None] * a._n_blocks[0]
_merge_svd_block(u_block, i, hbsize, vbsize, sorting, new_block)

for j in range(len(new_block)):
Expand Down Expand Up @@ -270,7 +270,7 @@ def _sort_v(v, sorting):
final_blocks = Array._get_out_blocks(v._n_blocks)

for i, v_block in enumerate(v_blocks):
new_block = [object() for _ in range(v._n_blocks[0])]
new_block = [None] * v._n_blocks[0]
_merge_svd_block(v_block, i, hbsize, vbsize, sorting, new_block)

for j in range(len(new_block)):
Expand Down
2 changes: 1 addition & 1 deletion dislib/model_selection/_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def merge_slices(s1, s2):
for a in g:
for row_block in a._blocks:
blocks.append(row_block)
group_blocks = [object() for _ in range(s1._n_blocks[1])]
group_blocks = [None] * s1._n_blocks[1]
_merge_rows_keeping_cols(blocks, group_blocks)
all_blocks.append(group_blocks)

Expand Down
8 changes: 4 additions & 4 deletions dislib/optimization/admm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def fit(self, x, y):
if self.verbose:
print("Iteration ", self.n_iter_)

z_blocks = [object() for _ in range(x_reg._n_blocks[1])]
z_blocks = [None] * x_reg._n_blocks[1]
_split_z(self._z, x._reg_shape[1], z_blocks)
self.z_ = Array([z_blocks], (1, x._reg_shape[1]), (1, x._reg_shape[1]),
(1, x.shape[1]), False)
Expand Down Expand Up @@ -156,7 +156,7 @@ def _compute_primal_res(self, z_old):
blocks = []

for w_hblock in self._w._iterator():
out_blocks = [object() for _ in range(self._w._n_blocks[1])]
out_blocks = [None] * self._w._n_blocks[1]
_substract(w_hblock._blocks, z_old, out_blocks)
blocks.append(out_blocks)

Expand All @@ -172,7 +172,7 @@ def _u_step(self):

for u_hblock, w_hblock in zip(self._u._iterator(),
self._w._iterator()):
out_blocks = [object() for _ in range(self._u._n_blocks[1])]
out_blocks = [None] * self._u._n_blocks[1]
_update_u(self._z, u_hblock._blocks, w_hblock._blocks, out_blocks)
u_blocks.append(out_blocks)

Expand All @@ -191,7 +191,7 @@ def _w_step(self, x, y):
for xy_hblock, u_hblock in zip(_paired_partition(x, y),
self._u._iterator()):
x_hblock, y_hblock = xy_hblock
w_hblock = [object() for _ in range(x._n_blocks[1])]
w_hblock = [None] * x._n_blocks[1]
x_blocks = x_hblock._blocks
y_blocks = y_hblock._blocks
u_blocks = u_hblock._blocks
Expand Down
4 changes: 2 additions & 2 deletions dislib/preprocessing/minmax_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def transform(self, x):
max_blocks = self.data_max_._blocks

for row in x._iterator(axis=0):
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_transform(row._blocks, min_blocks, max_blocks, out_blocks,
self._feature_range[0], self._feature_range[1])
blocks.append(out_blocks)
Expand Down Expand Up @@ -111,7 +111,7 @@ def inverse_transform(self, x):
max_blocks = self.data_max_._blocks

for row in x._iterator(axis=0):
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_inverse_transform(row._blocks, min_blocks, max_blocks, out_blocks,
self._feature_range[0], self._feature_range[1])
blocks.append(out_blocks)
Expand Down
4 changes: 2 additions & 2 deletions dislib/preprocessing/standard_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def transform(self, x):
v_blocks = self.var_._blocks

for row in x._iterator(axis=0):
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_transform(row._blocks, m_blocks, v_blocks, out_blocks)
blocks.append(out_blocks)

Expand Down Expand Up @@ -119,7 +119,7 @@ def inverse_transform(self, x):
v_blocks = self.var_._blocks

for row in x._iterator(axis=0):
out_blocks = [object() for _ in range(n_blocks)]
out_blocks = [None] * n_blocks
_inverse_transform(row._blocks, m_blocks, v_blocks, out_blocks)
blocks.append(out_blocks)

Expand Down
18 changes: 9 additions & 9 deletions dislib/utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ def shuffle(x, y=None, random_state=None):
in mapped_subsamples]
seed = np.random.randint(np.iinfo(np.int32).max)
# TODO: change object() for None when COMPSs version support it
part_out_x_blocks = [object() for _ in range(x._n_blocks[1])]
part_out_x_blocks = [None] * x._n_blocks[1]
if y is None:
_merge_shuffle_x(seed, part_out_subsamples, part_out_x_blocks,
x._reg_shape[1])
else:
part_out_y_blocks = [object() for _ in range(y._n_blocks[1])]
part_out_y_blocks = [None] * y._n_blocks[1]
_merge_shuffle_xy(seed, part_out_subsamples, part_out_x_blocks,
part_out_y_blocks, x._reg_shape[1],
y._reg_shape[1])
Expand Down Expand Up @@ -176,7 +176,7 @@ def _partition_arrays(part_in, sizes_out):
subsample_sizes[j] = n_selected
n_rows -= n_selected

subsamples = [object() for _ in range(n_parts_out)]
subsamples = [None] * n_parts_out
seed = np.random.randint(np.iinfo(np.int32).max)
if y is None:
_choose_and_assign_rows_x(x._blocks, subsample_sizes, subsamples, seed)
Expand All @@ -194,10 +194,10 @@ def _make_splits(x, y=None, test_size=None, train_size=None,
train_y_blocks_split = []
test_y_blocks_split = []
for index, blocks_x in enumerate(zip(x._blocks, y._blocks)):
blocks_train_x = [object() for _ in range(x._n_blocks[1])]
blocks_test_x = [object() for _ in range(x._n_blocks[1])]
blocks_train_y = [object() for _ in range(y._n_blocks[1])]
blocks_test_y = [object() for _ in range(y._n_blocks[1])]
blocks_train_x = [None] * x._n_blocks[1]
blocks_test_x = [None] * x._n_blocks[1]
blocks_train_y = [None] * y._n_blocks[1]
blocks_test_y = [None] * y._n_blocks[1]
if index <= len(x._blocks) - 2:
_compute_splits_x_y(blocks_x[0], blocks_x[1],
blocks_train_x, blocks_test_x,
Expand Down Expand Up @@ -267,8 +267,8 @@ def _make_splits(x, y=None, test_size=None, train_size=None,
train_x_blocks_split = []
test_x_blocks_split = []
for index, blocks_x in enumerate(x._blocks):
blocks_train_x = [object() for _ in range(x._n_blocks[1])]
blocks_test_x = [object() for _ in range(x._n_blocks[1])]
blocks_train_x = [None] * x._n_blocks[1]
blocks_test_x = [None] * x._n_blocks[1]
if index <= len(x._blocks) - 2:
_compute_splits_x(blocks_x, blocks_train_x,
blocks_test_x, test_size=test_size,
Expand Down