def extract_subnetworks(<EOL>partition_file,<EOL>network_file,<EOL>output_dir,<EOL>max_cores=DEFAULT_MAX_CORES,<EOL>max_size_matrix=DEFAULT_MAX_SIZE_MATRIX,<EOL>saturation_threshold=DEFAULT_SATURATION_THRESHOLD,<EOL>):
logger.info("<STR_LIT>")<EOL>data_chunks = np.loadtxt(partition_file, usecols=(<NUM_LIT:1>,), dtype=np.int32)<EOL>logger.info("<STR_LIT>")<EOL>network = np.loadtxt(network_file, dtype=np.int32)<EOL>cores = data_chunks<EOL>core_network = np.copy(network)<EOL>core_network[:, <NUM_LIT:0>] = cores[network[:, <NUM_LIT:0>]]<EOL>core_network[:, <NUM_LIT:1>] = cores[network[:, <NUM_LIT:1>]]<EOL>n = np.amax(cores) + <NUM_LIT:1><EOL>def extract(network_to_keep, filename):<EOL><INDENT>subnetwork = np.copy(network[network_to_keep])<EOL>subnetwork[:, <NUM_LIT:0>] -= <NUM_LIT:1><EOL>subnetwork[:, <NUM_LIT:1>] -= <NUM_LIT:1><EOL>np.savetxt(filename, subnetwork, fmt="<STR_LIT>")<EOL>return subnetwork<EOL><DEDENT>def draw(subnetwork, filename):<EOL><INDENT>try:<EOL><INDENT>row = subnetwork[:, <NUM_LIT:0>]<EOL>col = subnetwork[:, <NUM_LIT:1>]<EOL>data = subnetwork[:, <NUM_LIT:2>]<EOL><DEDENT>except TypeError:<EOL><INDENT>row = subnetwork.row<EOL>col = subnetwork.col<EOL>data = subnetwork.data<EOL><DEDENT>row_indices = stats.rankdata(<EOL>np.concatenate((row, col)), method="<STR_LIT>"<EOL>)<EOL>col_indices = stats.rankdata(<EOL>np.concatenate((col, row)), method="<STR_LIT>"<EOL>)<EOL>data = np.concatenate((data, data))<EOL>unique_row = np.unique(row)<EOL>unique_col = np.unique(col)<EOL>size = len(np.unique(np.concatenate((unique_row, unique_col)))) + <NUM_LIT:1><EOL>try:<EOL><INDENT>sparse_subnet = sparse.coo_matrix(<EOL>(data, (row_indices, col_indices)), shape=(size, size)<EOL>)<EOL>binning_factor = (size // max_size_matrix) + <NUM_LIT:1><EOL>binned_subnet = hcs.bin_sparse(<EOL>sparse_subnet, subsampling_factor=binning_factor<EOL>)<EOL>dense_subnet = binned_subnet.todense()<EOL>diagonal = np.diag(np.diag(dense_subnet))<EOL>normed_subnet = hcs.normalize_dense(dense_subnet - diagonal)<EOL>vmax = np.percentile(normed_subnet, saturation_threshold)<EOL>spaceless_pdf_plot_maker(normed_subnet, filename, vmax=vmax)<EOL><DEDENT>except MemoryError:<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT><DEDENT>def extract_and_draw(network_to_keep, filename_text, filename_image):<EOL><INDENT>subnetwork = extract(network_to_keep, filename=filename_text)<EOL>draw(subnetwork, filename=filename_image)<EOL><DEDENT>global_network_indices_list = []<EOL>for i in range(<NUM_LIT:1>, n):<EOL><INDENT>if i > max_cores:<EOL><INDENT>break<EOL><DEDENT>network_to_keep_1 = core_network[:, <NUM_LIT:0>] == i<EOL>network_to_keep_2 = core_network[:, <NUM_LIT:1>] == i<EOL>network_to_keep = network_to_keep_1 * network_to_keep_2<EOL>nonzero_indices, = np.nonzero(network_to_keep)<EOL>global_network_indices_list += nonzero_indices.tolist()<EOL>subnetwork_file = os.path.join(<EOL>output_dir, "<STR_LIT>".format(i)<EOL>)<EOL>image_name = os.path.join(output_dir, "<STR_LIT>".format(i))<EOL>extract_and_draw(<EOL>network_to_keep=network_to_keep,<EOL>filename_text=subnetwork_file,<EOL>filename_image=image_name,<EOL>)<EOL><DEDENT>
Extract bin subnetworks from the main network

    Identify bins, extract subnets, draws the adjacency matrices,
    saves it all in a specified output directory.

    Parameters
    ----------
    partition_file : file, str or pathlib.Path
        The file containing, for each chunk, the communities it was
        assigned to at each iteration.
    network_file : file, str or pathlib.Path
        The file containing the network in sparse (edge list) format
    output_dir : str or pathlib.Path
        The output directory to write the subnetworks into.
    max_cores : int, optional
        The maximum number of bins to extract. Default is 100.
    max_size_matrix : int, optional
        When rendering contact maps for each bin, the maximum size for the
        matrix. Default is 2000.
    saturation_threshold : float, optional
        When rendering contact maps for each bin, the percentile value over
        which the color map should be saturated. Default is 80.