Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ipd_extended/util.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
145 lines (113 sloc)
4.64 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import numpy as np | |
import os | |
import constants as cst | |
import re | |
import datetime | |
import random | |
import json | |
def get_file_name(file): | |
return re.match(cst.FILE_NAME_PATTERN, file).group(1) | |
def now(): | |
return datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
def get_escaped_name(problem): | |
return problem.replace("-", "_").replace(".", "") | |
def parse_relevant_features(data_file_name): | |
# search = re.search('\D+_(\d+)', data_file_name) | |
search = re.search('.*_r(\d+)_', data_file_name) | |
if not search: | |
return None | |
dims_count = int(search.group(1)) | |
return dims_count | |
# | |
# | |
# def try_parse_relevant_features(data_file_name): | |
# search = re.search('.+_(\d+)', data_file_name) | |
# if not search: | |
# raise ValueError("wrong file format: " + data_file_name) | |
# dims_count = int(search.group(1)) | |
# return dims_count | |
# todo | |
def datasets_iterator(f): | |
params = [] | |
def store(param): | |
if not param: | |
return | |
print('collected param:', param) | |
if type(param) == list: | |
params.extend(param) | |
else: | |
params.append(param) | |
if cst.REAL_DATASETS is not None: | |
for dataset_name in cst.REAL_DATASETS: | |
store(f(dataset_name, None, None, None, None, None, None)) | |
else: | |
for rows in cst.ROWS_RANGE_LIST: | |
for interaction_type in cst.INTERACTION_TYPE_RANGE_LIST: | |
dataset_name = interaction_type.name.lower() + '_n' + str(rows) | |
if interaction_type == cst.InteractionType.UNIFORM: | |
store(f(dataset_name, interaction_type, rows, None, None, None, None)) | |
continue | |
relevant_features_range_list = cst.XOR_RELEVANT_FEATURES_RANGE_LIST if interaction_type.name.startswith("XOR") else cst.CUBES_RELEVANT_FEATURES_RANGE_LIST | |
for rf in relevant_features_range_list: | |
# dataset_name += '_r' + str(rf) | |
if interaction_type.name.startswith("XOR"): | |
for offset in cst.XOR_OFFSETS: | |
store(f(dataset_name + '_r' + str(rf) + '_off' + str(offset), interaction_type, rows, rf, None, None, (0, offset))) | |
continue | |
continue | |
# CUBES, BLOBS | |
for i in cst.INTERACTION_NUMBER_RANGE_LIST: | |
for c in cst.CUBES_NUMBER_RANGE_LIST: | |
store(f(dataset_name+ '_r' + str(rf) + '_i' + str(i) + '_c' + str(c), interaction_type, rows, rf, i, c, None)) | |
return params | |
def construct_dataset_name(interaction_type, rows, rf=None, i=None, t=None, c=None, xoroffset=None): | |
dataset_name = interaction_type.name.lower() + '_n' + '{0:02d}'.format(rows) | |
if interaction_type.name.startswith("UNIFORM"): | |
return dataset_name | |
dataset_name += '_r' + '{0:02d}'.format(rf) | |
if interaction_type.name.startswith("XOR"): | |
return dataset_name + '_off' + str(xoroffset) | |
assert rf and i and t and c | |
if t == 'c': | |
# for example, returns cubes_7_3_c.csv where 3 is a number of cubes | |
return interaction_type.name.lower() + '_' + '{0:02d}'.format(rf) + '_' \ | |
+ '{0:02d}'.format(c) + '_' \ | |
+ t | |
if c == 1: | |
# for example, returns cubes_7_3_i.csv where 3 is a number of interactions with 1 cube in each | |
return interaction_type.name.lower() + '_' + '{0:02d}'.format(rf) + '_' \ | |
+ '{0:02d}'.format(i) + '_' \ | |
+ t | |
# for example, returns cubes_7_3_2_i.csv where 3 is a number of interactions with 2 cube in each | |
return interaction_type.name.lower() + '_' + '{0:02d}'.format(rf) + '_' \ | |
+ '{0:02d}'.format(i) + '_' \ | |
+ '{0:02d}'.format(c) + '_' \ | |
+ t | |
def chunks(l, n): | |
"""Yield successive n-sized chunks from l.""" | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
def parse_cubes_number(data_file_name): | |
match = re.match(".*_c(\d+)", data_file_name) | |
if match is not None: | |
cubes_number = int(match.group(1)) | |
else: | |
raise ValueError("cubes number is not known!") | |
return cubes_number | |
def parse_dataset_name(experiment_name): | |
search = re.search("(.*?)__", experiment_name) | |
if not search: | |
return None | |
return search.group(1) | |
def read_csv(csv_file): | |
if not os.path.exists(csv_file): | |
return None | |
with open(csv_file, "r") as f: | |
reader = csv.reader(f) | |
experiment_names = set() | |
for l in list(reader): | |
experiment_names.add(l[0]) | |
if len(experiment_names) == 0: | |
return None | |
return experiment_names |