def test_get_vertices_values(self): """Tests get_vertices_values.""" test1 = io.StringIO('1\t2\t0.1\n2\t3\t0.2\n1\t3\t0.3') expected1 = [('1', '2', 0.1), ('2', '3', 0.2), ('1', '3', 0.3)] actual1 = list(edge_analysis.get_vertices_values(test1)) self.assertEqual(actual1, expected1) test2 = io.StringIO('') expected2 = [] actual2 = list(edge_analysis.get_vertices_values(test2)) self.assertEqual(actual2, expected2)
def index(field, weightedness): """Renders chord diagram for a specific field. Args: field (string): A field, e.g. "major1" weightedness (string): Either weighted, unweighted, or weighted_norm """ network_filename = '../data/reduced/network_{}_{}.tsv'.format( field, weightedness) try: with open(network_filename, 'r') as network_file: full_network = list(edge_analysis.get_vertices_values(network_file)) except FileNotFoundError: bottle.abort(404, 'Couldn\'t find correct data file!') rendered_network = render_network_to_json(full_network, field) return bottle.template('chord', field=field, data=rendered_network)
def index(field, weightedness): """Renders chord diagram for a specific field. Args: field (string): A field, e.g. "major1" weightedness (string): Either weighted, unweighted, or weighted_norm """ network_filename = '../data/reduced/network_{}_{}.tsv'.format( field, weightedness) try: with open(network_filename, 'r') as network_file: full_network = list( edge_analysis.get_vertices_values(network_file)) except FileNotFoundError: bottle.abort(404, 'Couldn\'t find correct data file!') rendered_network = render_network_to_json(full_network, field) return bottle.template('chord', field=field, data=rendered_network)
help='Either "weighted" or "unweighted".') parser.add_argument( 'file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='File to segment (default standard input).') parser.add_argument( '--student-archive-path', dest='student_archive_path', help='Path of the student archive file.', required=True) parser.add_argument( '--swig-module-path', dest='swig_module_path', help='Directory containing the swig modules.', action=ReadableDirectory, required=True) args = parser.parse_args() # read edges from file; these correspond to actual interaction values edge_lines = list(edge_analysis.get_vertices_values(args.file)) # get expected interaction values students_wrapper = StudentContainerWrapper( args.swig_module_path, args.student_archive_path) segment_counts = segment.get_segment_counts(students_wrapper, args.field) # compare actual edges with expected edges, put into matrix segments = sorted(list(segment_counts.keys())) population = sum(segment_counts.values()) heatmatrix = numpy.zeros((len(segments), len(segments))) for vertex1, vertex2, actual_weight in edge_lines: vertex1_weight = segment_counts[vertex1] vertex2_weight = segment_counts[vertex2] # store vertex expected weight
import numpy import edge_analysis import simpson_diversity if __name__ == '__main__': parser = argparse.ArgumentParser( description='Calculate simpson diversity index for every vertex in ' 'a network.') parser.add_argument( 'file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='File to segment (default standard input).') args = parser.parse_args() edge_lines = edge_analysis.get_vertices_values(args.file) edge_values = edge_analysis.reduce_to_vertices(edge_lines) for vertex, values in edge_values.items(): # make every value in values an array because calculation functions # expect multiple arrays grouped by segment values_sum = numpy.sum(values) single_value_arrays = (numpy.array(value) for value in values) # We use weighted regardless of the input file because we the value to # be taken into account. There is only one value, so looking at the # number of values is useless. diversity_index = ( simpson_diversity.calculate_weighted_simpson_diversity_index( single_value_arrays, values_sum))