nodes: 0, module; 1, function_definition; 2, function_name:gp_datdir; 3, parameters; 4, block; 5, identifier:initial; 6, identifier:topN; 7, expression_statement; 8, comment:# prepare input/output directories; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, if_statement; 13, comment:# prepare data; 14, expression_statement; 15, for_statement; 16, expression_statement; 17, comment:# shown if --log flag given on command line; 18, comment:# sort countries according to mean population (highest -> lowest); 19, expression_statement; 20, comment:# "pop" (select) N most populated countries; 21, expression_statement; 22, comment:# generate plot using ccsgp.make_plot; 23, expression_statement; 24, expression_statement; 25, return_statement; 26, comment:"""example for plotting from a text file via numpy.loadtxt
  
    1. prepare input/output directories
    2. load the data into an OrderedDict() [adjust axes units]
    3. sort countries from highest to lowest population
    4. select the <topN> most populated countries
    5. call ccsgp.make_plot with data from 4
  
    Below is an output image for country initial T and the 4 most populated
    countries for this initial (click to enlarge). Also see::
  
      $ python -m ccsgp_get_started.examples.gp_datdir -h
  
    for help on the command line options.
  
    .. image:: pics/T.png
       :width: 450 px
  
    .. image:: pics/U.png
       :width: 450 px
  
    :param initial: country initial
    :type initial: str
    :param topN: number of most populated countries to plot
    :type topN: int
    :ivar inDir: input directory according to package structure and initial
    :ivar outDir: output directory according to package structure
    :ivar data: OrderedDict with datasets to plot as separate keys
    :ivar file: data input file for specific country, format: [x y] OR [x y dx dy]
    :ivar country: country, filename stem of input file
    :ivar file_url: absolute url to input file
    :ivar nSets: number of datasets
    """; 27, assignment; 28, assignment; 29, assignment; 30, not_operator; 31, comment:# catch missing initial; 32, block; 33, assignment; 34, identifier:file; 35, call; 36, block; 37, call; 38, assignment; 39, assignment; 40, assignment; 41, call; 42, string; 43, pattern_list; 44, call; 45, identifier:initial; 46, call; 47, identifier:inDir; 48, call; 49, call; 50, return_statement; 51, identifier:data; 52, call; 53, attribute; 54, argument_list; 55, expression_statement; 56, expression_statement; 57, expression_statement; 58, comment:# load data; 59, comment:# set y-axis unit to 1M; 60, expression_statement; 61, if_statement; 62, attribute; 63, argument_list; 64, identifier:sorted_data; 65, call; 66, identifier:top_data; 67, call; 68, identifier:nSets; 69, call; 70, identifier:make_plot; 71, argument_list; 72, string_content:done; 73, identifier:inDir; 74, identifier:outDir; 75, identifier:getWorkDirs; 76, argument_list; 77, attribute; 78, argument_list; 79, attribute; 80, argument_list; 81, attribute; 82, argument_list; 83, binary_operator:"initial %s doesn't exist" % initial; 84, identifier:OrderedDict; 85, argument_list; 86, identifier:os; 87, identifier:listdir; 88, identifier:inDir; 89, assignment; 90, assignment; 91, assignment; 92, augmented_assignment; 93, comparison_operator:data[country].shape[1] > 2; 94, block; 95, identifier:logging; 96, identifier:debug; 97, identifier:data; 98, identifier:OrderedDict; 99, argument_list; 100, identifier:OrderedDict; 101, generator_expression; 102, identifier:len; 103, argument_list; 104, keyword_argument; 105, keyword_argument; 106, keyword_argument; 107, comment:# use data keys as legend titles; 108, keyword_argument; 109, keyword_argument; 110, keyword_argument; 111, keyword_argument; 112, keyword_argument; 113, keyword_argument; 114, keyword_argument; 115, identifier:initial; 116, identifier:capitalize; 117, attribute; 118, identifier:join; 119, identifier:inDir; 120, identifier:initial; 121, attribute; 122, identifier:exists; 123, identifier:inDir; 124, string:"initial %s doesn't exist"; 125, identifier:initial; 126, identifier:country; 127, subscript; 128, identifier:file_url; 129, call; 130, subscript; 131, call; 132, subscript; 133, float:1e6; 134, subscript; 135, integer:2; 136, expression_statement; 137, call; 138, call; 139, for_in_clause; 140, if_clause; 141, identifier:top_data; 142, identifier:data; 143, call; 144, identifier:properties; 145, list_comprehension; 146, identifier:titles; 147, call; 148, identifier:name; 149, call; 150, identifier:key; 151, list; 152, identifier:ylabel; 153, string; 154, identifier:xlabel; 155, string; 156, identifier:rmargin; 157, float:0.99; 158, identifier:tmargin; 159, float:0.85; 160, identifier:size; 161, string; 162, identifier:os; 163, identifier:path; 164, identifier:os; 165, identifier:path; 166, call; 167, integer:0; 168, attribute; 169, argument_list; 170, identifier:data; 171, identifier:country; 172, attribute; 173, argument_list; 174, subscript; 175, slice; 176, integer:1; 177, attribute; 178, integer:1; 179, augmented_assignment; 180, identifier:sorted; 181, argument_list; 182, attribute; 183, argument_list; 184, identifier:i; 185, call; 186, identifier:sorted_data; 187, attribute; 188, argument_list; 189, call; 190, for_in_clause; 191, attribute; 192, argument_list; 193, attribute; 194, argument_list; 195, string; 196, string; 197, string_content; 198, string_content:year; 199, string_content:8.5in,8in; 200, attribute; 201, argument_list; 202, attribute; 203, identifier:join; 204, identifier:inDir; 205, identifier:file; 206, identifier:np; 207, identifier:loadtxt; 208, call; 209, identifier:data; 210, identifier:country; 211, subscript; 212, identifier:shape; 213, subscript; 214, float:1e6; 215, call; 216, keyword_argument; 217, keyword_argument; 218, identifier:sorted_data; 219, identifier:popitem; 220, keyword_argument; 221, identifier:xrange; 222, argument_list; 223, identifier:top_data; 224, identifier:values; 225, identifier:getOpts; 226, argument_list; 227, identifier:i; 228, call; 229, identifier:top_data; 230, identifier:keys; 231, attribute; 232, identifier:join; 233, identifier:outDir; 234, identifier:initial; 235, string_content:at graph 1., 1.2; 236, string_content:maxrows 2; 237, escape_sequence:\664; 238, attribute; 239, identifier:splitext; 240, identifier:file; 241, identifier:os; 242, identifier:path; 243, identifier:open; 244, argument_list; 245, identifier:data; 246, identifier:country; 247, subscript; 248, slice; 249, slice; 250, attribute; 251, argument_list; 252, identifier:key; 253, lambda; 254, identifier:reverse; 255, True; 256, identifier:last; 257, False; 258, identifier:topN; 259, identifier:i; 260, identifier:xrange; 261, argument_list; 262, identifier:os; 263, identifier:path; 264, identifier:os; 265, identifier:path; 266, identifier:file_url; 267, string; 268, identifier:data; 269, identifier:country; 270, integer:3; 271, identifier:data; 272, identifier:items; 273, lambda_parameters; 274, call; 275, identifier:nSets; 276, string_content:rb; 277, identifier:t; 278, attribute; 279, argument_list; 280, identifier:np; 281, identifier:mean; 282, subscript; 283, subscript; 284, slice; 285, integer:1; 286, identifier:t; 287, integer:1
code: def gp_datdir(initial, topN):
    """example for plotting from a text file via numpy.loadtxt
  
    1. prepare input/output directories
    2. load the data into an OrderedDict() [adjust axes units]
    3. sort countries from highest to lowest population
    4. select the <topN> most populated countries
    5. call ccsgp.make_plot with data from 4
  
    Below is an output image for country initial T and the 4 most populated
    countries for this initial (click to enlarge). Also see::
  
      $ python -m ccsgp_get_started.examples.gp_datdir -h
  
    for help on the command line options.
  
    .. image:: pics/T.png
       :width: 450 px
  
    .. image:: pics/U.png
       :width: 450 px
  
    :param initial: country initial
    :type initial: str
    :param topN: number of most populated countries to plot
    :type topN: int
    :ivar inDir: input directory according to package structure and initial
    :ivar outDir: output directory according to package structure
    :ivar data: OrderedDict with datasets to plot as separate keys
    :ivar file: data input file for specific country, format: [x y] OR [x y dx dy]
    :ivar country: country, filename stem of input file
    :ivar file_url: absolute url to input file
    :ivar nSets: number of datasets
    """
    # prepare input/output directories
    inDir, outDir = getWorkDirs()
    initial = initial.capitalize()
    inDir = os.path.join(inDir, initial)
    if not os.path.exists(inDir): # catch missing initial
      return "initial %s doesn't exist" % initial
    # prepare data
    data = OrderedDict()
    for file in os.listdir(inDir):
      country = os.path.splitext(file)[0]
      file_url = os.path.join(inDir, file)
      data[country] = np.loadtxt(open(file_url, 'rb')) # load data
      # set y-axis unit to 1M
      data[country][:, 1] /= 1e6
      if data[country].shape[1] > 2: data[country][:, 3:] /= 1e6
    logging.debug(data) # shown if --log flag given on command line
    # sort countries according to mean population (highest -> lowest)
    sorted_data = OrderedDict(sorted(
      data.items(), key = lambda t: np.mean(t[1][:,1]), reverse = True
    ))
    # "pop" (select) N most populated countries
    top_data = OrderedDict(
      sorted_data.popitem(last = False) for i in xrange(topN)
      if sorted_data
    )
    # generate plot using ccsgp.make_plot
    nSets = len(top_data)
    make_plot(
      data = top_data.values(),
      properties = [ getOpts(i) for i in xrange(nSets) ],
      titles = top_data.keys(), # use data keys as legend titles
      name = os.path.join(outDir, initial),
      key = [ 'at graph 1., 1.2', 'maxrows 2' ],
      ylabel = 'total population ({/Symbol \664} 10^{6})',
      xlabel = 'year', rmargin = 0.99, tmargin = 0.85, size='8.5in,8in'
    )
    return 'done'