def draw_sparse_matrix( array_filename, output_image, vmax=DEFAULT_SATURATION_THRESHOLD, max_size_matrix=DEFAULT_MAX_SIZE_MATRIX, ): """Draw a quick preview of a sparse matrix with automated binning and normalization. """ matrix = np.loadtxt(array_filename, dtype=np.int32, skiprows=1) try: row, col, data = matrix.T except ValueError: row, col, data = matrix size = max(np.amax(row), np.amax(col)) + 1 S = sparse.coo_matrix((data, (row, col)), shape=(size, size)) if max_size_matrix <= 0: binning = 1 else: binning = (size // max_size_matrix) + 1 binned_S = hcs.bin_sparse(S, subsampling_factor=binning) dense_S = binned_S.todense() dense_S = dense_S + dense_S.T - np.diag(np.diag(dense_S)) normed_S = hcs.normalize_dense(dense_S) spaceless_pdf_plot_maker(normed_S, output_image, vmax=vmax)
def draw(subnetwork, filename): try: # Numpy array format row = subnetwork[:, 0] col = subnetwork[:, 1] data = subnetwork[:, 2] except TypeError: # Scipy sparse format row = subnetwork.row col = subnetwork.col data = subnetwork.data row_indices = stats.rankdata( np.concatenate((row, col)), method="dense" ) col_indices = stats.rankdata( np.concatenate((col, row)), method="dense" ) data = np.concatenate((data, data)) # print("Row length: {}, col length: {}, data length: {}" # "".format(len(row_indices), len(col_indices), len(data))) unique_row = np.unique(row) unique_col = np.unique(col) # print("Network shape: {},{}".format(len(unique_row), # len(unique_col))) size = len(np.unique(np.concatenate((unique_row, unique_col)))) + 1 # print("Size of matrix to draw: {}".format(size)) try: sparse_subnet = sparse.coo_matrix( (data, (row_indices, col_indices)), shape=(size, size) ) binning_factor = (size // max_size_matrix) + 1 binned_subnet = hcs.bin_sparse( sparse_subnet, subsampling_factor=binning_factor ) dense_subnet = binned_subnet.todense() diagonal = np.diag(np.diag(dense_subnet)) normed_subnet = hcs.normalize_dense(dense_subnet - diagonal) vmax = np.percentile(normed_subnet, saturation_threshold) spaceless_pdf_plot_maker(normed_subnet, filename, vmax=vmax) except MemoryError: logger.warning( "Warning, couldn't save matrix due to memory issues" )