def read(report_type: str, report_id: str, results_path: str = None) -> dict: """ :param report_type: :param report_id: :param results_path: :return: """ first_character = report_type[0].lower() if first_character == 't': report_type = 'trial' elif first_character == 'g': report_type = 'group' if not results_path: results_path = paths.results() path = os.path.join( paths.clean(results_path), 'reports', report_type, report_id, '{}.json'.format(report_id) ) if not os.path.exists(path): return None out = read_path(path) out['id'] = report_id return out
def listings( report_type: str, results_path: str = None, matching_glob: str = None ) -> dict: """ Returns a dictionary that contains the report ids and absolute paths to the corresponding data files. :param report_type: :param results_path: :param matching_glob: :return: """ report_type = report_type.lower()[0] report_type = 'group' if report_type == 'g' else 'trial' if not results_path: results_path = paths.results() else: results_path = paths.clean(results_path) directory = os.path.join(results_path, 'reports', report_type) out = dict() if matching_glob: matching_glob = os.path.join( directory, matching_glob.strip(os.sep) ) for item_path in glob.iglob(matching_glob): item_path = item_path.rstrip(os.sep) item = os.path.basename(item_path) out[item] = os.path.join(item_path, '{}.json'.format(item)) return out for item in os.listdir(directory): item_path = os.path.join(directory, item, '{}.json'.format(item)) out[item] = item_path return out
def fetch_trial_list(settings: dict) -> list: """ :param settings: :return: """ trials = settings.get('trials', []) if isinstance(trials, (list, tuple)) and len(trials) > 0: return trials if not isinstance(trials, str): trials = '' if not trials: directory = settings['path'] else: directory = paths.clean(os.path.join(settings['path'], trials)) if not os.path.exists(directory): return [] out = [] for item in os.listdir(directory): if not item.endswith('.json'): continue item_path = os.path.join(directory, item) try: with open(item_path, 'r+') as f: contents = json.load(f) except Exception: continue if 'trials' in contents: continue if 'name' in contents: out.append(item) return out
def set_key(configs: dict, key: str, value: typing.List[str]): """ Removes the specified key from the tracksim configs if the key exists :param configs: The tracksim configs object to modify :param key: The key in the tracksim configs object to remove :param value: """ if key.startswith('path.'): for index in range(len(value)): value[index] = paths.clean(value[index]) if len(value) == 1: value = value[0] configs[key] = value system.save_configs(configs) system.log('[SET]: "{}" to "{}"'.format(key, value))