-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_file_to_mtx.py
59 lines (46 loc) · 1.57 KB
/
input_file_to_mtx.py
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
54
55
56
57
58
59
import numpy as np
import pandas as pd
import mtx_file_check as file_check
import sys
import json
# Input and output file paths
input_file = sys.argv[1]
output_file = sys.argv[1]
result=file_check.validate_mtx_file(input_file)
if "Validation Error" in result:
data = [result, 8, 5]
json_data = json.dumps(data)
print(json_data)
sys.exit()
# Read the file and separate metadata and edges
with open(input_file, 'r') as file:
lines = file.readlines()
# Extract metadata (first row)
metadata = lines[0].strip().split()
num_nodes = int(metadata[0])
num_edges = int(metadata[1])
# Extract edges
edges = []
for line in lines[1:]:
edges.append(list(map(int, line.strip().split())))
data = np.array(edges)
# Interchange the first and second columns
data[:, [0, 1]] = data[:, [1, 0]]
# Change the third column (weights) to negative if it exists
#if data.shape[1] == 3:
# data[:, 2] = data[:, 2] * -1
# Sort rows by column 2, then by column 1
data = data[np.lexsort((data[:, 0], data[:, 1]))]
# Write the Matrix Market file
with open(output_file, 'w') as f:
# Write the Matrix Market header
f.write('%MatrixMarket matrix coordinate integer symmetric\n')
f.write('% Graph adjacency matrix generated from edge list sorted by column 2 with negative weights\n')
# Write the size of the matrix and the number of non-zero entries (edges)
f.write(f'{num_nodes} {num_nodes} {num_edges}\n')
# Write the edges to the file
for edge in data:
f.write(f'{edge[0]} {edge[1]} {edge[2]}\n')
data = [result, 8, 5]
json_data = json.dumps(data)
print(json_data)