def read_legacy_complexities(complexities, timestep_directory, tdata):
    def __path(type):
        return os.path.join(timestep_directory, 'complexity_' + type + '.txt')

    complexities_read = []

    for type in complexities:
        path = __path(type)
        if os.path.isfile(path):
            data = common_complexity.parse_legacy_complexities(path)
            tdata[type] = common_complexity.normalize_complexities(data)
            complexities_read.append(type)

    return complexities_read
def read_legacy_complexities(complexities,
			     timestep_directory,
			     tdata):
	def __path(type):
		return os.path.join(timestep_directory, 'complexity_' + type + '.txt')

	complexities_read = []
	
	for type in complexities:
		path = __path(type)
		if os.path.isfile(path):
			data = common_complexity.parse_legacy_complexities(path)
			tdata[type] = common_complexity.normalize_complexities(data)
			complexities_read.append(type)

	return complexities_read
def compute_complexities(complexities,
			 timestep_directory,
			 tdata):
	def __path(type):
		return os.path.join(timestep_directory, 'complexity_' + type + '.plt')
	
	# --- Read in any complexities computed on a previous invocation of this script
	complexities_read = []
	for type in complexities:
		path = __path(type)

		if os.path.isfile(path):
			try:
				table = datalib.parse(path)[type]
				data = table.getColumn('Complexity').data
				tdata[type] = common_complexity.normalize_complexities(data)
	
				complexities_read.append(type)
			except datalib.InvalidFileError, e:
				# file must have been incomplete
				print "Failed reading ", path, "(", e, ") ... regenerating"
def compute_complexities(complexities, timestep, timestep_directory, tdata):
    def __path(type):
        return os.path.join(timestep_directory, 'complexity_' + type + '.plt')

    # --- Read in any complexities computed on a previous invocation of this script
    complexities_read = []
    if not OverwriteEpochComplexities:
        for type in complexities:
            path = __path(type)

            if os.path.isfile(path):
                try:
                    table = datalib.parse(path)[type]
                    data = table.getColumn('Complexity').data
                    tdata[type] = common_complexity.normalize_complexities(
                        data)

                    complexities_read.append(type)
                except datalib.InvalidFileError, e:
                    # file must have been incomplete
                    print "Failed reading ", path, "(", e, ") ... regenerating"
				table = tables[type]
				row = table.createRow()
	
				row.set('AgentNumber', agent_number)
				row.set('Complexity', fields.pop(0))
	
		# --- Write to file and normalize data (eg sort and remove 0's)
		for type in complexities_remaining:
			table = tables[type]
	
			datalib.write(__path(type),
				      table)
	
			data = table.getColumn('Complexity').data
	
			tdata[type] = common_complexity.normalize_complexities(data)

	return complexities

####################################################################################
###
### FUNCTION make_percents()
###
####################################################################################
def make_percents(tables, totals):
	for table in tables.values():
		for row in table.rows():
			t = row.get('Timestep')
			total = totals[t][table.name]

			for bin in range(NUMBINS):
            for type in complexities_remaining:
                table = tables[type]
                row = table.createRow()

                row.set('AgentNumber', agent_number)
                row.set('Complexity', fields.pop(0))

        # --- Write to file and normalize data (eg sort and remove 0's)
        for type in complexities_remaining:
            table = tables[type]

            datalib.write(__path(type), table)

            data = table.getColumn('Complexity').data

            tdata[type] = common_complexity.normalize_complexities(data)

    return complexities


####################################################################################
###
### FUNCTION divide(numerator, denominator)
###
####################################################################################
def divide(numerator, denominator):
    if denominator == 0:
        return 0.0
    else:
        return numerator / denominator