def adjacency_rcm_ordered(data_matrix, threshold, countries, file_prefix, ordered=True): ''' r = [1 2 3 4 5 6 7 8 9 10] allowedR = [1 2 3 4 5 6] orderedVector = {'Argentina', 'Brazil', 'Fm USSR', 'Peru', 'Libya', 'Israel', 'UK', 'USA', 'Denmark', 'Portugal', 'Norway', 'Sweden'}; filteredLabels = {}; filteredR = [] r_size = size(r) r_size = r_size(2) filteredRIndex = 1 for i=1:r_size if ismember(r(i), allowedR) filteredR = [filteredR r(i)] filteredLabels{filteredRIndex} = char(orderedVector(r(i))); filteredRIndex = filteredRIndex + 1 end end ''' assert len(data_matrix) == len(data_matrix[0]) all_countries = countries[0] allowed_countries = countries[1] lines = [] lines.append(matrix_py_matlab_with_name('c0', transform_pn_to_01(data_matrix, threshold))) lines.append(matrix_py_matlab_with_name('datamatrix', data_matrix)) lines.append("r = symrcm(c0);") lines.append( "allowedR = %s;" % (str([all_countries.index(country) + 1 for country in allowed_countries]).replace(",", ""))) lines.append("countriesVectorRow={%s};" % (str(all_countries)[1:-1])) lines.append("filteredLabels = {};") lines.append("filteredR = []") lines.append("r_size = size(r)") lines.append("r_size = r_size(2)") lines.append("filteredRIndex = 1") lines.append("for i=1:r_size") lines.append("\tif ismember(r(i), allowedR)") lines.append("\t\tfilteredR = [filteredR r(i)]") lines.append("\t\tfilteredLabels{filteredRIndex} = char(countriesVectorRow(r(i)));") lines.append("\t\tfilteredRIndex = filteredRIndex + 1") lines.append("\tend") lines.append("end") lines.append("x=redgreencmap(200);") vectorName = 'filteredLabels' if ordered else 'countriesVectorRow' lines.append( "hm = HeatMap(datamatrix%s,'RowLabels',%s,'ColumnLabels',%s, 'Colormap', horzcat(horzcat(x(:,2),x(:,1)),x(:,3)));" % ('(filteredR,filteredR)' if ordered else '', vectorName, vectorName) ) lines.append("plot(hm);") lines.append( "saveas(gcf,'%s-%d-%s','png');" % (file_prefix, threshold * 100, 'ordered' if ordered else 'unordered')) lines.append("close all force;") return lines
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)
def matlab_code_for_rcm_ordered_corr_coef_for_export_volume_matrix(data, normalize_row_or_column): f = open(OUT_DIR.RCM_MATRIC + 'iraniraqexport.m', 'w') all_countries = DEFAULT_COUNTRIES_LIST allowed_countries = iran_iraq_countries # all_countries = ['USA', 'UK', 'Australia', 'Greece'] # allowed_countries = ['Australia', 'Greece'] for year in data.all_years: pn_matrix = export_volume_matrix(data, year, all_countries, normalize_row_or_column) f.write(matrix_py_matlab_with_name('exportvolume', pn_matrix)) data_matrix = corrcoef(pn_matrix) for threshold in [0]: for line in adjacency_rcm_ordered(data_matrix, threshold, [all_countries, allowed_countries], '%s' % (year)): f.write("%s\n" % line) f.close()
def matlab_code_for_rcm_ordered_corr_coef_for_sliding_window_degree_matrix(data, definition, def_args, normalize_row_or_column): f = open(OUT_DIR.RCM_MATRIC + 'iraniraqpn.m', 'w') all_countries = DEFAULT_COUNTRIES_LIST allowed_countries = iran_iraq_countries # all_countries = ['USA', 'UK', 'Australia', 'Greece'] # allowed_countries = ['Australia', 'Greece'] window_size = 5 for window_start_year in data.all_years: sliding_window = range(window_start_year, window_start_year + window_size) window_end_year = sliding_window[-1:][0] if window_end_year > 2000: break pn_matrix = positives_and_negatives_matrix(data, definition, def_args, sliding_window, all_countries, normalize_row_or_column) f.write(matrix_py_matlab_with_name('pnmatrix', pn_matrix)) data_matrix = corrcoef(pn_matrix) for threshold in [0]: for line in adjacency_rcm_ordered(data_matrix, threshold, [all_countries, allowed_countries], '%s-%s' % (window_start_year, window_end_year)): f.write("%s\n" % line) f.close()