def matlab_code_for_rcm_ordered_corr_coef_for_adjacency_matrix(data, definition, def_args):
    f = open(OUT_DIR.RCM_MATRIC + 'code1.m', 'w')
    countries_list = data.top_countries_by_export_all_year(50)
    for year in data.all_years:
        adj_matrix = adjacency_matrix(data, definition, def_args, year, countries_list)
        for line in adjacency_rcm_ordered(corrcoef(adj_matrix), 0, [countries_list, countries_list], '%s' % year, True):
            f.write("%s\n" % line)
    f.close()
def write_all_correlation_files(data, definition, def_args):
    window_size = 4
    for year in data.all_years:
        write_correlation_list('adj-y%d.txt' % year, adjacency_matrix(data, definition, def_args, year), 0)
        if 1963 + window_size <= year:
            write_correlation_list('pn-y%d-to-y%d.txt' % (year - window_size + 1, year),
                                   positives_and_negatives_matrix(data, definition, def_args,
                                                                  range(year - window_size + 1, year + 1)), 0.5)
def write_matlab_code_for_rcm(data, definition, def_args, countries=DEFAULT_COUNTRIES_LIST):
    f = open(OUT_DIR.RCM_MATRIC + 'code.m', 'w')
    for year in [1965, 1970, 1975, 1980, 1985, 1990, 1995, 1999, 2000]:
        for coeff in range(0, 11):
            c = coeff * .05
            adj_matrix = adjacency_matrix(data, definition, def_args, year, countries)
            for line in adjacency_rcm_ordered(corrcoef(adj_matrix), c, [countries, countries]):
                f.write(line + "\n")
    f.close()
def matlab_code_for_rcm_ordered_corr_coef_for_adjacency_matrix(data, definition, def_args):
    f = open(OUT_DIR.RCM_MATRIC + 'iraniraqadj.m', 'w')
    all_countries = DEFAULT_COUNTRIES_LIST
    allowed_countries = iran_iraq_countries
    for year in data.all_years:
        data_matrix = adjacency_matrix(data, definition, def_args, year, all_countries)
        for line in adjacency_rcm_ordered(data_matrix, 0, [all_countries, allowed_countries], '%s' % year):
            f.write("%s\n" % line)
    f.close()
def write_matlab_code_for_corrmatrix(data, years, definition, def_args, file_prefix,
                                     countries=DEFAULT_COUNTRIES_LIST,
                                     country_study=False):
    for year in years:
        corrcoef_mat = corrcoef(adjacency_matrix(data, definition, def_args, year, countries))
        print matrix_py_matlab_with_name('corrmatrix%d' % year, corrcoef_mat, country_study)
    print "corrmatrix=[%s]" % (';'.join(['corrmatrix%d' % year for year in years]))
    print list_as_matlab_vector('countriesVectorColumn', countries)
    if country_study:
        print "countriesVectorRow={%s};" % (str(concat_countries([countries[0]], years))[1:-1])
    else:
        print "countriesVectorRow={%s};" % (str(concat_countries(countries, years))[1:-1])
    print "x=redgreencmap(200);"
    print "HeatMap(corrmatrix,'RowLabels',countriesVectorRow,'ColumnLabels',countriesVectorColumn, 'Colormap', horzcat(horzcat(x(:,2),x(:,1)),x(:,3)));"
    print "saveas(gcf,'%s','png');" % (file_prefix)