File size: 1,692 Bytes
d957db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import argparse
from pathlib import Path
import scipy as sp

import matplotlib.pyplot as plt

def get_markersize(matrix_size: int, nnz: int) -> float:
    """
    Calculate the marker size for the spy plot based on the matrix size.
    """
    density = nnz / (matrix_size * matrix_size)

    print(f"    - Density of the matrix: {density}")
    print(f"    - Number of non-zero elements: {nnz}")
    print(f"    - Size of the matrix: {matrix_size} x {matrix_size}")

    if matrix_size < 1000:
        fixed_ratio = 0.01 / 10
    else:
        fixed_ratio = 0.01 / 10000

    markersize = fixed_ratio * matrix_size
    return markersize

def spy(matrix: sp.sparse.coo_matrix, file_name: str) -> None:
    """
    Visualize the sparsity pattern of a sparse matrix using matplotlib.
    """
    markersize = get_markersize(matrix.shape[0], matrix.nnz)

    plt.figure(figsize=(10, 10))
    plt.spy(matrix, markersize=markersize, color="teal")
    # plt.spy(matrix, markersize=1)
    plt.title(f"spyplot: {file_name}")
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Matrix Market utilities")
    parser.add_argument("command", choices=["spy"], help="Command to execute")
    parser.add_argument("input_file", help="Input file path")

    args = parser.parse_args()

    file_name = os.path.splitext(os.path.basename(args.input_file))[0]
    print(f"file_name: {file_name}")

    if args.command == "spy":
        print("Visualizing sparsity pattern:")
        matrix_coo = sp.sparse.load_npz(args.input_file).tocoo()
        print(f"    - Loaded NPZ file from: {args.input_file}")
        spy(matrix_coo, file_name)