# Path: apgl/io/MultiGraphCsvReader.py
# class MultiGraphCsvReader(CsvReader):
#     def __init__(self, idIndex, featureIndices, converters, nanProcessor=None):
#         self.idIndex = idIndex
#         self.featureIndices = featureIndices
#         self.converters = converters
#         featureIndices.insert(0, idIndex)
#         self.vertexIndices = tuple(featureIndices)
#         self.nanProcessor = nanProcessor 
# 
#     def readGraph(self, vertexFileName, edgeFileNames, undirected=True, delimiter=None):
#         """
#         Read a MultiGraph from at least 2 files: one is the information about
#         vertices and the other(s) are lists of edges. For the list of vertices
#         the first column must be the ID of the vertex. 
#         """
#         
#         X = numpy.loadtxt(vertexFileName, skiprows=1, converters=self.converters, usecols=self.vertexIndices, delimiter=delimiter)
# 
#         numVertices = X.shape[0]
#         numFeatures = X.shape[1]-1 
# 
#         vertexIds = X[:, 0]
#         vertexIdsDict = {}
# 
#         for i in range(0, numVertices):
#             vertexIdsDict[vertexIds[i]] = i
# 
#         if self.nanProcessor != None:
#             X[:, 1:numFeatures+1] = self.nanProcessor(X[:, 1:numFeatures+1])
# 
#         vertexList = VertexList(numVertices, numFeatures)
#         vertexList.setVertices(X[:, 1:numFeatures+1])
#         
#         maxEdgeTypes = len(edgeFileNames)
#         sparseMultiGraph = SparseMultiGraph(vertexList, maxEdgeTypes, undirected)
# 
#         for i in range(0, maxEdgeTypes):
#             self.__readEdgeFile(vertexIdsDict, edgeFileNames[i], sparseMultiGraph, i)
# 
#         logging.info("MultiGraph read with " + str(sparseMultiGraph.getNumVertices()) + " vertices and " + str(sparseMultiGraph.getNumEdges()) + " edges")
# 
#         return sparseMultiGraph
# 
#     def __readEdgeFile(self, vertexIdsDict, edgeFileName, sparseMultiGraph, edgeType):
#         """
#         Each edge file contains a list of edges with possible weights.
#         """
#         edges = numpy.loadtxt(edgeFileName)
#         numEdges = edges.shape[0]
# 
#         if edges.shape[1] == 2:
#             for i in range(0, numEdges):
#                 sparseMultiGraph.addEdge(vertexIdsDict[edges[i, 0]], vertexIdsDict[edges[i, 1]], edgeType)
#         elif edges.shape[1] == 3:
#             for i in range(0, numEdges):
#                 sparseMultiGraph.addEdge(vertexIdsDict[edges[i, 0]], vertexIdsDict[edges[i, 1]], edgeType, edges[i, 2])
#         else:
#             raise ValueError("Bad edge file")
# 
#     idIndex = None
#     featureIndices = None
#     converters = None
# 
# Path: apgl/util/PathDefaults.py
# class PathDefaults(object):
#     """
#     This class stores some useful global default paths. 
#     """
#     @staticmethod 
#     def readField(field):
#         configFileName = expanduser("~") + os.sep + ".apglrc"
# 
#         if not os.path.exists(configFileName): 
#             print("Creating missing config file: " + configFileName)
#             defaultConfig = "[paths]\n" 
#             defaultConfig += "data = " + expanduser("~") + os.sep + "data" + os.sep + "\n"
#             defaultConfig += "output = " + expanduser("~") + os.sep + "output" + os.sep + "\n"
#             configFile = open(configFileName, "w")
#             configFile.write(defaultConfig)
#             configFile.close()
#             
#         parser = SafeConfigParser()
#         parser.read(configFileName)
#         return parser.get('paths', field)
# 
#     @staticmethod
#     def getSourceDir():
#         """
#         Root directory of source code for APGL. 
#         """
#         dir = os.path.abspath( __file__ )
#         dir, head = os.path.split(dir)
#         dir, head = os.path.split(dir)
#         return dir 
#         
#     @staticmethod
#     def getDataDir():
#         """
#         Location of data files. 
#         """
#         return PathDefaults.readField("data")
# 
#     @staticmethod
#     def getOutputDir():
#         """
#         Location of output files. 
#         """
#         return PathDefaults.readField("output")
#         
#     @staticmethod
#     def getTempDir():
#         return tempfile.gettempdir() + os.sep


import unittest
import os
import numpy 
from apgl.io.MultiGraphCsvReader import MultiGraphCsvReader
from apgl.util.PathDefaults import PathDefaults 
class MultiGraphCsvReaderTest(unittest.TestCase):
    def setUp(self):
        pass

    def testReadGraph(self):

        dir = PathDefaults.getDataDir()
        vertexFileName = dir + "test/deggraf10.csv"
        edgeFileNames = [dir + "test/testEdges1.csv", dir + "test/testEdges2.csv"]

        def genderConv(x):
            genderDict = {'"M"': 0, '"F"': 1}
            return genderDict[x]

        def orientConv(x):
            orientDict = {'"HT"': 0, '"HB"': 1}
            return orientDict[x]

        def fteConv(x):
            fteDict = {'"INTER"': 0, '"CONTA"': 1}
            return fteDict[x]

        def provConv(x):
            provDict = {'"CH"': 0, '"SC"': 1, '"SS"': 2, '"LH"' : 3, '"GM"' : 4}
            return provDict[x]

        converters = {3: genderConv, 4: orientConv, 5:fteConv, 6:provConv}

        idIndex = 0
        featureIndices = list(range(1,11))

# Path: apgl/io/MultiGraphCsvReader.py
# class MultiGraphCsvReader(CsvReader):
#     def __init__(self, idIndex, featureIndices, converters, nanProcessor=None):
#         self.idIndex = idIndex
#         self.featureIndices = featureIndices
#         self.converters = converters
#         featureIndices.insert(0, idIndex)
#         self.vertexIndices = tuple(featureIndices)
#         self.nanProcessor = nanProcessor 
# 
#     def readGraph(self, vertexFileName, edgeFileNames, undirected=True, delimiter=None):
#         """
#         Read a MultiGraph from at least 2 files: one is the information about
#         vertices and the other(s) are lists of edges. For the list of vertices
#         the first column must be the ID of the vertex. 
#         """
#         
#         X = numpy.loadtxt(vertexFileName, skiprows=1, converters=self.converters, usecols=self.vertexIndices, delimiter=delimiter)
# 
#         numVertices = X.shape[0]
#         numFeatures = X.shape[1]-1 
# 
#         vertexIds = X[:, 0]
#         vertexIdsDict = {}
# 
#         for i in range(0, numVertices):
#             vertexIdsDict[vertexIds[i]] = i
# 
#         if self.nanProcessor != None:
#             X[:, 1:numFeatures+1] = self.nanProcessor(X[:, 1:numFeatures+1])
# 
#         vertexList = VertexList(numVertices, numFeatures)
#         vertexList.setVertices(X[:, 1:numFeatures+1])
#         
#         maxEdgeTypes = len(edgeFileNames)
#         sparseMultiGraph = SparseMultiGraph(vertexList, maxEdgeTypes, undirected)
# 
#         for i in range(0, maxEdgeTypes):
#             self.__readEdgeFile(vertexIdsDict, edgeFileNames[i], sparseMultiGraph, i)
# 
#         logging.info("MultiGraph read with " + str(sparseMultiGraph.getNumVertices()) + " vertices and " + str(sparseMultiGraph.getNumEdges()) + " edges")
# 
#         return sparseMultiGraph
# 
#     def __readEdgeFile(self, vertexIdsDict, edgeFileName, sparseMultiGraph, edgeType):
#         """
#         Each edge file contains a list of edges with possible weights.
#         """
#         edges = numpy.loadtxt(edgeFileName)
#         numEdges = edges.shape[0]
# 
#         if edges.shape[1] == 2:
#             for i in range(0, numEdges):
#                 sparseMultiGraph.addEdge(vertexIdsDict[edges[i, 0]], vertexIdsDict[edges[i, 1]], edgeType)
#         elif edges.shape[1] == 3:
#             for i in range(0, numEdges):
#                 sparseMultiGraph.addEdge(vertexIdsDict[edges[i, 0]], vertexIdsDict[edges[i, 1]], edgeType, edges[i, 2])
#         else:
#             raise ValueError("Bad edge file")
# 
#     idIndex = None
#     featureIndices = None
#     converters = None
# 
# Path: apgl/util/PathDefaults.py
# class PathDefaults(object):
#     """
#     This class stores some useful global default paths. 
#     """
#     @staticmethod 
#     def readField(field):
#         configFileName = expanduser("~") + os.sep + ".apglrc"
# 
#         if not os.path.exists(configFileName): 
#             print("Creating missing config file: " + configFileName)
#             defaultConfig = "[paths]\n" 
#             defaultConfig += "data = " + expanduser("~") + os.sep + "data" + os.sep + "\n"
#             defaultConfig += "output = " + expanduser("~") + os.sep + "output" + os.sep + "\n"
#             configFile = open(configFileName, "w")
#             configFile.write(defaultConfig)
#             configFile.close()
#             
#         parser = SafeConfigParser()
#         parser.read(configFileName)
#         return parser.get('paths', field)
# 
#     @staticmethod
#     def getSourceDir():
#         """
#         Root directory of source code for APGL. 
#         """
#         dir = os.path.abspath( __file__ )
#         dir, head = os.path.split(dir)
#         dir, head = os.path.split(dir)
#         return dir 
#         
#     @staticmethod
#     def getDataDir():
#         """
#         Location of data files. 
#         """
#         return PathDefaults.readField("data")
# 
#     @staticmethod
#     def getOutputDir():
#         """
#         Location of output files. 
#         """
#         return PathDefaults.readField("output")
#         
#     @staticmethod
#     def getTempDir():
#         return tempfile.gettempdir() + os.sep


# Path: apgl/io/test/MultiGraphCsvReaderTest.py
import unittest
import os
import numpy 
from apgl.io.MultiGraphCsvReader import MultiGraphCsvReader
from apgl.util.PathDefaults import PathDefaults 

class MultiGraphCsvReaderTest(unittest.TestCase):
    def setUp(self):
        pass

    def testReadGraph(self):

        dir = PathDefaults.getDataDir()
        vertexFileName = dir + "test/deggraf10.csv"
        edgeFileNames = [dir + "test/testEdges1.csv", dir + "test/testEdges2.csv"]

        def genderConv(x):
            genderDict = {'"M"': 0, '"F"': 1}
            return genderDict[x]

        def orientConv(x):
            orientDict = {'"HT"': 0, '"HB"': 1}
            return orientDict[x]

        def fteConv(x):
            fteDict = {'"INTER"': 0, '"CONTA"': 1}
            return fteDict[x]

        def provConv(x):
            provDict = {'"CH"': 0, '"SC"': 1, '"SS"': 2, '"LH"' : 3, '"GM"' : 4}
            return provDict[x]

        converters = {3: genderConv, 4: orientConv, 5:fteConv, 6:provConv}

        idIndex = 0
        featureIndices = list(range(1,11))

        multiGraphCsvReader = MultiGraphCsvReader(idIndex, featureIndices, converters)