Skip to content

Commit

Permalink
update : logger and gen_using made dynamic.
Browse files Browse the repository at this point in the history
  • Loading branch information
sadrasabouri committed Mar 12, 2024
1 parent 5f8d021 commit 4568221
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 116 deletions.
55 changes: 15 additions & 40 deletions pyrgg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
16: dot_maker,
}

ENGINE_GENERATOR = {
1: pyrgg_engine.gen_using,
2: erg_engine.gen_using,
ENGINE_MAPPER = {
1: pyrgg_engine,
2: erg_engine,
}


Expand All @@ -47,31 +47,12 @@ def gen_graph(input_dict, file_name):
:return: None
"""
first_time = time.perf_counter()
min_weight = input_dict["min_weight"]
max_weight = input_dict["max_weight"]
vertices_number = input_dict["vertices"]
min_edge = input_dict["min_edge"]
max_edge = input_dict["max_edge"]
sign = input_dict["sign"]
direct = input_dict["direct"]
self_loop = input_dict["self_loop"]
multigraph = input_dict["multigraph"]
output_format = input_dict["output_format"]
probability = input_dict["probability"]
engine = input_dict["engine"]
edge_number = ENGINE_GENERATOR[engine](
input_dict["edge_number"] = ENGINE_MAPPER[engine].gen_using(
GENERATOR_MENU[output_format],
file_name=file_name,
min_weight=min_weight,
max_weight=max_weight,
vertices_number=vertices_number,
min_edge=min_edge,
max_edge=max_edge,
sign=sign,
direct=direct,
self_loop=self_loop,
multigraph=multigraph,
probability=probability)
file_name,
input_dict)
if output_format == 4:
json_to_yaml(file_name)
if output_format == 7:
Expand All @@ -80,23 +61,17 @@ def gen_graph(input_dict, file_name):
second_time = time.perf_counter()
elapsed_time = second_time - first_time
elapsed_time_format = time_convert(elapsed_time)
print("Total Number of Edges : " + str(edge_number))
print("Total Number of Edges : " + str(input_dict["edge_number"]))
print("Graph Generated in " + elapsed_time_format)
print("Where --> " + SOURCE_DIR)
logger(
file_name + SUFFIX_MENU[output_format],
vertices_number,
edge_number,
max_edge,
min_edge,
direct,
sign,
multigraph,
self_loop,
max_weight,
min_weight,
engine,
elapsed_time_format)
try:
with open("logfile.log", "a") as file:
ENGINE_MAPPER[engine].logger(file,
file_name,
elapsed_time_format,
input_dict)
except Exception:
print(PYRGG_LOGGER_ERROR_MESSAGE)


def run(input_dict=None):
Expand Down
55 changes: 47 additions & 8 deletions pyrgg/engines/erdos_reyni_gilbert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
# -*- coding: utf-8 -*-
"""Erdős-Rényi-Gilbert Engine module."""
import datetime
from random import random
from pyrgg.params import ENGINE_MENU, SUFFIX_MENU

LOGGER_TEMPLATE = """{0}
Filename : {1}
Probability : {2}
Vertices : {3}
Total Edges : {4}
Engine : {5} ({6})
Elapsed Time : {7}
-------------------------------
"""

def logger(file, file_name, elapsed_time, input_dict):
"""
Save generated graph logs for Erdős-Rényi-Gilbert engine.
:param file: file to write log into
:type file: file object
:param file_name: file name
:type file_name: str
:param elapsed_time: elapsed time
:type elapsed_time: str
:param input_dict: input data
:type input_dict: dict
:return: None
"""
file.write(LOGGER_TEMPLATE.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
file_name + SUFFIX_MENU[input_dict['output_format']],
str(input_dict["probability"]),
str(input_dict["vertices"]),
str(input_dict["edge_number"]),
input_dict["engine"],
ENGINE_MENU[input_dict["engine"]],
elapsed_time))


def edge_gen(n, p):
"""
Expand All @@ -25,28 +61,31 @@ def edge_gen(n, p):

def gen_using(
gen_function,
**kwargs):
file_name,
input_dict):
"""
Generate graph using given function based on Erdos Renyi Gilbert model.
Refer to (https://en.wikipedia.org/wiki/Erd%C5%91s%E2%80%93R%C3%A9nyi_model)
:param gen_function: generation function
:type gen_function: function object
:param kwargs: input data as keyword arguments
:type kwargs: dict
:param file_name: file name
:type file_name: str
:param input_dict: input data
:type input_dict: dict
:return: number of edges as int
"""
edge_dic, edge_number = edge_gen(
kwargs['vertices_number'],
kwargs['probability'])
weight_dic = {key: [1] * edge_number for key in range(1, kwargs['vertices_number'] + 1)}
input_dict['vertices'],
input_dict['probability'])
weight_dic = {key: [1] * edge_number for key in range(1, input_dict['vertices'] + 1)}
gen_function(
edge_dic,
weight_dic,
{
"file_name": kwargs['file_name'],
"vertices_number": kwargs['vertices_number'],
"file_name": file_name,
"vertices_number": input_dict['vertices'],
"max_weight": 1,
"min_weight": 1,
"min_edge": edge_number,
Expand Down
103 changes: 79 additions & 24 deletions pyrgg/engines/pyrgg.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
# -*- coding: utf-8 -*-
"""PyRGG Engine module."""
import datetime
from random import randint, uniform, choice
from pyrgg.functions import _threshold_calc, get_precision
from pyrgg.params import PYRGG_TEST_MODE
from pyrgg.functions import _threshold_calc, get_precision, is_weighted
from pyrgg.params import PYRGG_TEST_MODE, ENGINE_MENU, SUFFIX_MENU

LOGGER_TEMPLATE = """{0}
Filename : {1}
Vertices : {2}
Total Edges : {3}
Max Edge : {4}
Min Edge : {5}
Directed : {6}
Signed : {7}
Multigraph : {8}
Self Loop : {9}
Weighted : {10}
Max Weight : {11}
Min Weight : {12}
Engine : {13} ({14})
Elapsed Time : {15}
-------------------------------
"""

def logger(file, file_name, elapsed_time, input_dict):
"""
Save generated graph logs for PyRGG engine.
:param file: file to write log into
:type file: file object
:param file_name: file name
:type file_name: str
:param elapsed_time: elapsed time
:type elapsed_time: str
:param input_dict: input data
:type input_dict: dict
:return: None
"""
file.write(LOGGER_TEMPLATE.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
file_name + SUFFIX_MENU[input_dict['output_format']],
str(input_dict["vertices"]),
str(input_dict["edge_number"]),
str(input_dict["max_edge"]),
str(input_dict["min_edge"]),
str(bool(input_dict["direct"])),
str(bool(input_dict["sign"])),
str(bool(input_dict["multigraph"])),
str(bool(input_dict["self_loop"])),
str(is_weighted(input_dict["max_edge"],
input_dict["min_edge"],
bool(input_dict["sign"]))),
str(input_dict["max_edge"]),
str(input_dict["min_edge"]),
input_dict["engine"],
ENGINE_MENU[input_dict["engine"]],
elapsed_time))


def branch_gen(
Expand Down Expand Up @@ -188,40 +240,43 @@ def edge_gen(

def gen_using(
gen_function,
**kwargs):
file_name,
input_dict):
"""
Generate graph using given function based on PyRGG model.
:param gen_function: generation function
:type gen_function: function object
:param kwargs: input data as keyword arguments
:type kwargs: dict
:param file_name: file name
:type file_name: str
:param input_dict: input data
:type input_dict: dict
:return: number of edges as int
"""
edge_dic, weight_dic, edge_number = edge_gen(
kwargs['vertices_number'],
kwargs['min_weight'],
kwargs['max_weight'],
kwargs['min_edge'],
kwargs['max_edge'],
kwargs['sign'],
kwargs['direct'],
kwargs['self_loop'],
kwargs['multigraph'])
input_dict['vertices'],
input_dict['min_weight'],
input_dict['max_weight'],
input_dict['min_edge'],
input_dict['max_edge'],
input_dict['sign'],
input_dict['direct'],
input_dict['self_loop'],
input_dict['multigraph'])
gen_function(
edge_dic,
weight_dic,
{
"file_name": kwargs['file_name'],
"min_weight": kwargs['min_weight'],
"max_weight": kwargs['max_weight'],
"vertices_number": kwargs['vertices_number'],
"min_edge": kwargs['min_edge'],
"max_edge": kwargs['max_edge'],
"sign": kwargs['sign'],
"direct": kwargs['direct'],
"self_loop": kwargs['self_loop'],
"multigraph": kwargs['multigraph'],
"file_name": file_name,
"min_weight": input_dict['min_weight'],
"max_weight": input_dict['max_weight'],
"vertices_number": input_dict['vertices'],
"min_edge": input_dict['min_edge'],
"max_edge": input_dict['max_edge'],
"sign": input_dict['sign'],
"direct": input_dict['direct'],
"self_loop": input_dict['self_loop'],
"multigraph": input_dict['multigraph'],
"edge_number": edge_number,
})
return edge_number
1 change: 0 additions & 1 deletion pyrgg/functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Pyrgg functions module."""
import datetime
from json import loads as json_loads
from json import dump as json_dump
import os
Expand Down
18 changes: 0 additions & 18 deletions pyrgg/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,6 @@

PYRGG_CONFIG_SAVE_ERROR_MESSAGE = "[Error] Failed to save config file!"

PYRGG_LOGGER_TEMPLATE = """{0}
Filename : {1}
Vertices : {2}
Total Edges : {3}
Max Edge : {4}
Min Edge : {5}
Directed : {6}
Signed : {7}
Multigraph : {8}
Self Loop : {9}
Weighted : {10}
Max Weight : {11}
Min Weight : {12}
Engine : {13} ({14})
Elapsed Time : {15}
-------------------------------
"""

DIMACS_FIX = dedent(
"""\
c FILE :{0}.gr
Expand Down
Loading

0 comments on commit 4568221

Please sign in to comment.