# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + deletable=true editable=true
import numpy as np
import os

# pwd = %pwd

# source directory where we search for METIS network files that end in .txt
DATA_DIR = os.path.join(pwd, "data", "predition_model_tests", "network")
# output directory for network METIS files where we add randomly generated edge weights
OUT_DATA_DIR = os.path.join(pwd, "data", "predition_model_tests", "network", "rand_edge_weights")

# minimum & maximum values for randomly generated edge weights
minEdgeWeight = 1
maxEdgeWeight = 20

if not os.path.exists(OUT_DATA_DIR):
    os.makedirs(OUT_DATA_DIR)

# + deletable=true editable=true
network_files = [f for f in os.listdir(DATA_DIR) if os.path.isfile(os.path.join(DATA_DIR, f))]

randnbr = np.random.randint(minEdgeWeight, maxEdgeWeight + 1, 1000)
edgeCount = 0


for file in network_files:
    newFileLines = []
    if file.endswith(".txt"):
        with open(os.path.join(DATA_DIR, file), 'r+') as f:
            isFirst = True
            for line in f:
                line = line.strip()
                if line.startswith("%"):
                    continue
                if isFirst:
                    isFirst = False
                    
                    parts = line.split(" ")
                    if(len(parts) == 3):
                        # DON'T proceed for this file as it already has edge weights
                        break
                    else:
                        s = ''
                        for part in parts:
                            if len(s):
                                s = s + " "
                            s = s + str(part)
                        s = s + " 1"
                        newFileLines.append(s)
                else:
                    edges = line.split(" ")
                    edgeLine = ''
                    for edge in edges:
                        if len(edge) == 0:
                            continue
                        if len(edgeLine):
                            edgeLine = edgeLine + " "
                        if edgeCount == len(randnbr):
                            randnbr = np.random.randint(minEdgeWeight, maxEdgeWeight + 1, 1000)
                            edgeCount = 0
                        edgeLine = edgeLine + edge + " " + str(randnbr[edgeCount])
                        edgeCount += 1
                    newFileLines.append(edgeLine)
                
    if len(newFileLines):
        outFilePath = os.path.join(OUT_DATA_DIR, file)
        print('Saving network file', file, 'with randomly generated edges to:')
        print('   ', outFilePath)
        with open(outFilePath, 'w+') as of:
            for line in newFileLines:
                of.write(line + '\n')


# + deletable=true editable=true


StarcoderdataJupyterScriptsDedupFiltered