コード例 #1
0
ファイル: features.py プロジェクト: brsaylor/atn-data
def get_simulation_data_atn_csv_format(file_object):
    """
    Given an ATN CSV file,
    return a tuple (node_config, node_config_attributes, biomass_data).

    node_config_attributes is a dictionary with the node config parameters (as
    returned by nodeConfigToParams()).

    biomass_data is a DataFrame whose columns are node IDs and whose index is the timeteps of the simulation.
    """

    node_config_attributes = None
    biomass_data = pd.DataFrame()

    reader = csv.reader(file_object)
    reader.__next__()  # Skip the header row
    for row in reader:
        if len(row) == 0 or row[0] == '':
            # Blank line: end of biomass data
            break
        node_id = int(row[0].split('.')[1])
        biomass_data[node_id] = [float(x) for x in row[1:]]

    # The next row should have the node config
    row = reader.__next__()
    node_config_str = row[0].split(': ')[1]
    node_config = parse_node_config(node_config_str)
    node_config_attributes = node_config_to_params(node_config)

    return node_config, node_config_attributes, biomass_data
コード例 #2
0
ファイル: features.py プロジェクト: brsaylor/atn-data
def get_simulation_data_hdf5(filename):
    """ Read ATN simulation data from an HDF5 file produced by WoB Server.

    Parameters
    ----------
    filename : str
        The name of the HDF5 file

    Returns
    -------
    (node_config : str, node_config_attributes : dict, biomass_data : pd.DataFrame)
        - node_config is the node configuration string
        - node_config_attributes is the corresponding output of node_config_to_params()
        - DataFrame columns are node IDs and whose index is the timeteps of the simulation.
    """

    with h5py.File(filename, 'r') as f:
        biomass_data = pd.DataFrame(f['biomass'][:, :], columns=list(f['node_ids']))
        node_config_str = f.attrs['node_config'].decode('utf-8')
        node_config = parse_node_config(node_config_str)
        node_config_attributes = node_config_to_params(node_config)

        return node_config, node_config_attributes, biomass_data
コード例 #3
0
if not os.path.isdir(args.set_directory):
    print("Error: set directory {} does not exist".format(args.set_directory), file=sys.stderr)
    sys.exit(1)
if not os.path.isdir(args.data_directory):
    print("Error: data directory {} does not exist".format(args.data_directory), file=sys.stderr)
    sys.exit(1)
set_directory = os.path.normpath(args.set_directory)
data_directory = os.path.normpath(args.data_directory)

# Read the first nodeconfig from the nodeconfig file in the set directory
nodeconfig_filename = os.path.join(set_directory, 'nodeconfigs.{}.txt'.format(os.path.basename(set_directory)))
try:
    nodeconfig_file = open(nodeconfig_filename)
except IOError:
    print("Error: could not read node config file {}".format(nodeconfig_filename), file=sys.stderr)
    sys.exit(1)
nodeconfig_str = nodeconfig_file.readline()
nodeconfig_file.close()
nodeconfig = parse_node_config(nodeconfig_str)

# Determine the number of species and food web ID
node_ids = [node['nodeId'] for node in nodeconfig]
node_ids.sort()
num_species = len(node_ids)
food_web_id = '-'.join(map(str, node_ids))

# Do the move
new_set_directory = os.path.join(data_directory, '{}-species'.format(num_species), food_web_id, set_directory)
print("Moving {} to {}".format(set_directory, new_set_directory))
os.renames(set_directory, new_set_directory)
コード例 #4
0
def node_ids_from_node_config(node_config):
    nodes = nodeconfigs.parse_node_config(node_config)
    node_ids = [node['nodeId'] for node in nodes]
    node_ids.sort()
    return node_ids
コード例 #5
0
set_directory = os.path.normpath(args.set_directory)
data_directory = os.path.normpath(args.data_directory)

# Read the first nodeconfig from the nodeconfig file in the set directory
nodeconfig_filename = os.path.join(
    set_directory,
    'nodeconfigs.{}.txt'.format(os.path.basename(set_directory)))
try:
    nodeconfig_file = open(nodeconfig_filename)
except IOError:
    print("Error: could not read node config file {}".format(
        nodeconfig_filename),
          file=sys.stderr)
    sys.exit(1)
nodeconfig_str = nodeconfig_file.readline()
nodeconfig_file.close()
nodeconfig = parse_node_config(nodeconfig_str)

# Determine the number of species and food web ID
node_ids = [node['nodeId'] for node in nodeconfig]
node_ids.sort()
num_species = len(node_ids)
food_web_id = '-'.join(map(str, node_ids))

# Do the move
new_set_directory = os.path.join(data_directory,
                                 '{}-species'.format(num_species), food_web_id,
                                 set_directory)
print("Moving {} to {}".format(set_directory, new_set_directory))
os.renames(set_directory, new_set_directory)