Skip to content

Commit dd4ab92

Browse files
committed
Cleanup matrix
1 parent 733890a commit dd4ab92

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

src/diffupy/matrix.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464
self.validate_labels()
6565

6666
def __str__(self):
67+
"""Return string version of the matrix class."""
6768
s = f" {self.cols_labels}"
6869

6970
for i, row_label in enumerate(self.rows_labels):
@@ -72,6 +73,7 @@ def __str__(self):
7273
return f"\nmatrix {self.name} \n {s} \n "
7374

7475
def __iter__(self, **kargs):
76+
"""Helper method for the matrix class."""
7577
self.i = -1
7678
self.j = 0
7779

@@ -83,6 +85,7 @@ def __iter__(self, **kargs):
8385
return self
8486

8587
def __next__(self):
88+
"""Helper method for the matrix class."""
8689
if self.i >= len(self.rows_labels) - 1 and self.j >= len(self.cols_labels) - 1:
8790
self.get_labels = True
8891
self.get_indices = False
@@ -152,14 +155,14 @@ def validate_labels_and_update_ix_mappings(self):
152155
@property
153156
def cols_labels(self):
154157
"""Return a copy of Matrix Object."""
155-
156158
if self.quadratic:
157159
return self.rows_labels
158160

159161
return self._cols_labels
160162

161163
@cols_labels.setter
162164
def cols_labels(self, cols_labels):
165+
"""Set column labels."""
163166
if self.quadratic:
164167
self.rows_labels = list(cols_labels)
165168
else:
@@ -168,6 +171,7 @@ def cols_labels(self, cols_labels):
168171
# Rows ix mapping
169172
@property
170173
def rows_labels_ix_mapping(self):
174+
"""Set row labels to ix."""
171175
if hasattr(self, '_rows_labels_ix_mapping'):
172176
return self._rows_labels_ix_mapping
173177

@@ -176,11 +180,13 @@ def rows_labels_ix_mapping(self):
176180

177181
@rows_labels_ix_mapping.setter
178182
def rows_labels_ix_mapping(self, rows_labels_ix_mapping):
183+
"""Set labels labels to ix."""
179184
self._rows_labels_ix_mapping = rows_labels_ix_mapping
180185

181186
# Columns ix mapping
182187
@property
183188
def cols_labels_ix_mapping(self):
189+
"""Set column labels to ix."""
184190
if self.quadratic:
185191
return self.rows_labels_ix_mapping
186192

@@ -192,6 +198,7 @@ def cols_labels_ix_mapping(self):
192198

193199
@cols_labels_ix_mapping.setter
194200
def cols_labels_ix_mapping(self, cols_labels_ix_mapping):
201+
"""Set mapping labels to ix."""
195202
if self.quadratic:
196203
self._rows_labels_ix_mapping = cols_labels_ix_mapping
197204

@@ -200,6 +207,7 @@ def cols_labels_ix_mapping(self, cols_labels_ix_mapping):
200207
# Rows scores mapping
201208
@property
202209
def rows_idx_scores_mapping(self):
210+
"""Set mapping indexes to scores."""
203211
if hasattr(self, '_rows_idx_scores_mapping'):
204212
return self._rows_idx_scores_mapping
205213

@@ -209,11 +217,13 @@ def rows_idx_scores_mapping(self):
209217

210218
@rows_idx_scores_mapping.setter
211219
def rows_idx_scores_mapping(self, rows_idx_scores_mapping):
220+
"""Set mapping rows to ids."""
212221
self._rows_idx_scores_mapping = rows_idx_scores_mapping
213222

214223
# Columns scores mapping
215224
@property
216225
def cols_idx_scores_mapping(self):
226+
"""Set mapping indexes to scores."""
217227
if hasattr(self, '_cols_idx_scores_mapping'):
218228
return self._cols_idx_scores_mapping
219229

@@ -230,32 +240,35 @@ def cols_idx_scores_mapping(self, cols_idx_scores_mapping):
230240
"""Getters from labels"""
231241

232242
def set_row_from_label(self, label, x):
243+
"""Set row from label."""
233244
self.mat[self.rows_labels_ix_mapping[label]] = x
234245

235246
def get_row_from_label(self, label):
247+
"""Get row from labels."""
236248
return self.mat[self.rows_labels_ix_mapping[label]]
237249

238250
def set_col_from_label(self, label, x):
251+
"""Set col from label."""
239252
self.mat[:, self.cols_labels_ix_mapping[label]] = x
240253

241254
def get_col_from_label(self, label):
255+
"""Get col from labels."""
242256
return self.mat[:, self.cols_labels_ix_mapping[label]]
243257

244258
def set_cell_from_labels(self, row_label, col_label, x):
259+
"""Set cell from labels."""
245260
self.mat[self.rows_labels_ix_mapping[row_label], self.cols_labels_ix_mapping[col_label]] = x
246261

247262
def get_cell_from_labels(self, row_label, col_label):
263+
"""Get cell from labels."""
248264
return self.mat[self.rows_labels_ix_mapping[row_label], self.cols_labels_ix_mapping[col_label]]
249265

250-
# TODO: este nombre es un poco confuso no?
251-
252266
"""Methods"""
253267

254268
"""Binds"""
255269

256270
def row_bind(self, rows=None, rows_labels=None, matrix=None):
257271
"""Return a copy of Matrix Object."""
258-
259272
if matrix:
260273
rows = matrix.mat
261274
rows_labels = matrix.rows_labels
@@ -269,7 +282,6 @@ def row_bind(self, rows=None, rows_labels=None, matrix=None):
269282

270283
def col_bind(self, cols=None, cols_labels=None, matrix=None):
271284
"""Return a copy of Matrix Object."""
272-
273285
if matrix:
274286
cols = matrix.mat
275287
cols_labels = matrix.cols_labels
@@ -285,7 +297,6 @@ def col_bind(self, cols=None, cols_labels=None, matrix=None):
285297

286298
def match_rows(self, reference_matrix):
287299
"""Match method to set rows labels as reference matrix."""
288-
289300
if self.quadratic:
290301
log.warning('Changing rows of a symmetric Matrix implies changing also columns.')
291302
return self.match_mat(reference_matrix, True)
@@ -302,7 +313,6 @@ def match_rows(self, reference_matrix):
302313

303314
def match_cols(self, reference_matrix):
304315
"""Match method to set cols labels as reference matrix."""
305-
306316
if reference_matrix.cols_labels == reference_matrix.cols_labels:
307317
return self
308318

@@ -322,7 +332,6 @@ def match_cols(self, reference_matrix):
322332

323333
def match_mat(self, reference_matrix, match_quadratic=None):
324334
"""Match method to set axis labels as reference matrix."""
325-
326335
if reference_matrix.cols_labels == self.cols_labels and reference_matrix.rows_labels == self.rows_labels:
327336
return self
328337

@@ -348,7 +357,6 @@ def match_mat(self, reference_matrix, match_quadratic=None):
348357

349358
def match_missing_rows(self, reference_labels, missing_fill):
350359
"""Match method to set missing rows labels from reference labels with the missing_fill value."""
351-
352360
if reference_labels == self.rows_labels:
353361
return self
354362

@@ -368,7 +376,6 @@ def match_missing_rows(self, reference_labels, missing_fill):
368376

369377
def match_missing_cols(self, reference_labels, missing_fill):
370378
"""Match method to set missing cols labels from reference labels with the missing_fill value."""
371-
372379
if reference_labels == self.cols_labels:
373380
return self
374381

@@ -391,7 +398,6 @@ def match_missing_cols(self, reference_labels, missing_fill):
391398

392399
def order_rows(self, reverse=True, col_ref_idx=None):
393400
"""Order matrix rows by cell values."""
394-
395401
# Get the row index-cell value mapping.
396402
mapping = self.rows_idx_scores_mapping
397403

@@ -419,7 +425,6 @@ def order_rows(self, reverse=True, col_ref_idx=None):
419425

420426
def from_csv(csv_path):
421427
"""Import matrix from csv file using the headers as a Matrix class."""
422-
423428
m = np.genfromtxt(csv_path, dtype=None, delimiter=',')
424429
return Matrix(
425430
mat=np.array(
@@ -434,8 +439,9 @@ def from_csv(csv_path):
434439
)
435440

436441

437-
# TODO: Poner que es matriz simetrica
438442
class LaplacianMatrix(Matrix):
443+
"""Laplacian matrix class"""
444+
439445
def __init__(self, graph, normalized=False, name=''):
440446
l_mat = get_laplacian(graph, normalized)
441447

0 commit comments

Comments
 (0)