Skip to content

Commit

Permalink
change : input model changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
sadrasabouri committed Dec 14, 2024
1 parent 3eb9a46 commit 5872946
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
46 changes: 42 additions & 4 deletions pyrgg/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ def handle_pos_int(input_number):
return val


def handle_natural_number(input_number):
"""
Handle input number and raise ValueError if it is not a natural number.
:param input_number: input number
:type input_number: float or int or str
:return: result as int
"""
val = int(input_number)
if val < 1:
raise ValueError
return val

def handle_str_to_number(string):
"""
Convert string to float or int.
Expand Down Expand Up @@ -267,8 +280,9 @@ def handle_prob_matrix(string):
"multigraph": handle_str_to_bool,
"config": handle_str_to_bool,
"probability": handle_str_prob,
"block_sizes": handle_pos_list,
"probability_matrix": handle_prob_matrix,
"blocks": handle_natural_number,
"inter_probability": handle_str_prob,
"intra_probability": handle_str_prob,
}


Expand Down Expand Up @@ -411,13 +425,15 @@ def get_input(input_func=input):
"multigraph": False,
"config": False,
"probability": 0.5,
"block_sizes": [],
"probability_matrix": [],
"blocks": 1,
"inter_probability": 0.75,
"intra_probability": 0.25,
}

result_dict = _update_using_menu(result_dict, input_func)
result_dict = _update_with_engine_params(
result_dict, input_func, pyrgg.params.ENGINE_PARAM_MAP[result_dict['engine']])
result_dict = _post_input_update(result_dict)
return input_filter(result_dict)


Expand Down Expand Up @@ -457,6 +473,8 @@ def _update_with_engine_params(result_dict, input_func, engine_params):
:type engine_params: dict
:return: result_dict as dict
"""
if result_dict['engine'] == 4:
print(pyrgg.params.PYRGG_SBM_WARNING_MESSAGE)
for index in sorted(engine_params):
item1, item2 = engine_params[index]
if not result_dict["weight"] and item1 in ["max_weight", "min_weight"]:
Expand All @@ -473,6 +491,26 @@ def _update_with_engine_params(result_dict, input_func, engine_params):
return result_dict


def _post_input_update(result_dict):
"""
Update result_dict after getting user input.
:param result_dict: result data
:type result_dict: dict
:return: result_dict as dict
"""
result_dict["block_sizes"] = [result_dict["vertices"] // result_dict["blocks"]] * (result_dict["blocks"])
if result_dict["vertices"] % result_dict["blocks"] != 0:
result_dict["block_sizes"][-1] += result_dict["vertices"] % result_dict["blocks"]
print(pyrgg.params.PYRGG_UNDIVISIBLE_WARNING_MESSAGE)
result_dict["probability_matrix"] = [
[result_dict["intra_probability"] if i == j else result_dict["inter_probability"]
for j in range(result_dict["blocks"])]
for i in range(result_dict["blocks"])
]
return result_dict


def json_to_yaml(filename):
"""
Convert json file to yaml file.
Expand Down
13 changes: 9 additions & 4 deletions pyrgg/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@

SBM_ENGINE_PARAMS = {
1: ["vertices", "- Vertices Number (n >= 0) : "],
2: ["block_sizes", "- Block Sizes (|Ci|>=0) - |C1| |C2| ... |Ck| : "],
3: ["probability_matrix", "- Probability Matrix (0 <= Pij <= 1) - P11 P12 ... P1k, ..., Pk1 Pk2 ... Pkk : "],
4: ["direct", "- Undirected[0] or Directed[1]"],
5: ["self_loop", "- No Self Loop[0] or Self Loop[1]"],
2: ["blocks", "- Blocks Number (k >= 0) : "],
3: ["intra_probability", "- Intra Block Probability (0 <= p_intra <= 1): "],
4: ["inter_probability", "- Inter Block Probability (0 <= p_inter <= 1): "],
5: ["direct", "- Undirected[0] or Directed[1]"],
6: ["self_loop", "- No Self Loop[0] or Self Loop[1]"],
}

ENGINE_PARAM_MAP = {
Expand Down Expand Up @@ -153,6 +154,10 @@

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

PYRGG_UNDIVISIBLE_WARNING_MESSAGE = "Warning : Vertices are not divisible by blocks. The last block will have the remaining vertices."

PYRGG_SBM_WARNING_MESSAGE = "[Warning] - In CLI mode, Stochastic Block Model gets the number of blocks and inter/intra probabilities. To get more detailed configuration, please save and edit the config file."

PYRGG_CONFIG_LIST_MESSAGE = "Config files detected in the current directory are listed below:"

PYRGG_CONFIG_LOAD_MESSAGE = "Press the config index to load or any other keys to start with the new one:"
Expand Down

0 comments on commit 5872946

Please sign in to comment.