def plot_best_cluster_latent(exp_results, 
                     out_filenames):

    sample_d = pickle.load(open(exp_results))
    chains = sample_d['chains']
    
    exp = sample_d['exp']
    data_filename = exp['data_filename']
    data = pickle.load(open(data_filename))
    data_basename, _ = os.path.splitext(data_filename)
    meta = pickle.load(open(data_basename + ".meta"))

    meta_infile = meta['infile']
    print "meta_infile=", meta_infile

    d = pickle.load(open(meta_infile, 'r'))
    conn = d['dist_matrix']['link']
    cell_id_permutation = d['cell_id_permutation']

    dist_matrix = d['dist_matrix']
    orig_data = pickle.load(open(d['infile']))
    cell_types = d['types'][:len(conn)]

    type_metadata_df = pickle.load(open("type_metadata.pickle", 'r'))['type_metadata']
    type_color_map = {'gc' : 'r', 
                      'ac' : 'b', 
                      'bc' : 'g', 
                      'other' : 'k'}

    TYPE_N = np.max(cell_types) + 1

    type_colors = []
    for i in range(TYPE_N):
        if (i < 70):
            d = type_metadata_df.loc[i+1]['desig']
        else:
            d = "  "
        type_colors.append(type_color_map.get(d[:2], 'k'))

    print type_colors 
    

    chains = [c for c in chains if type(c['scores']) != int]
    CHAINN = len(chains)

    chains_sorted_order = np.argsort([d['scores'][-1] for d in chains])[::-1]

    soma_positions = pickle.load(open('soma.positions.pickle', 'r'))
    synapses = pickle.load(open('synapses.pickle', 'r'))['synapsedf']
    # only take the first 950
    synapses = synapses[(synapses['from_id'] < len(cell_id_permutation) )  & (synapses['to_id']<len(cell_id_permutation))]

    reorder_synapses = util.reorder_synapse_ids(synapses, cell_id_permutation)

    pos_vec = soma_positions['pos_vec'][cell_id_permutation]
    model = data['relations']['R1']['model']
    print dist_matrix.dtype, model
    if "istance" not in model:
        dist_matrix = dist_matrix['link']

    for chain_pos, (cluster_fname, latent_fname) in enumerate(out_filenames):
        best_chain_i = chains_sorted_order[chain_pos]
        best_chain = chains[best_chain_i]
        sample_latent = best_chain['state']
        cell_assignment = sample_latent['domains']['d1']['assignment']

        a = irm.util.canonicalize_assignment(cell_assignment)

        util.plot_cluster_properties(a, cell_types, 
                                     pos_vec, reorder_synapses, 
                                     cluster_fname, class_colors=type_colors)

            
        print "model=", model, dist_matrix.dtype
        util.plot_latent(sample_latent, dist_matrix, latent_fname, 
                         model = model, 
                         PLOT_MAX_DIST=150.0, MAX_CLASSES=20)
        ax_purity.axvline(v)
    ax_purity.set_ylabel('true cell id')
    ax_purity.set_xlim(0, len(z_ord))

    f.tight_layout()

    f.savefig(plot_latent_filename)
    soma_positions = pickle.load(open('soma.positions.pickle', 'r'))
    synapses = pickle.load(open('synapses.pickle', 'r'))['synapsedf']
    # only take the first 950
    synapses = synapses[(synapses['from_id'] < len(cell_id_permutation) )  & (synapses['to_id']<len(cell_id_permutation))]

    reorder_synapses = util.reorder_synapse_ids(synapses, cell_id_permutation)

    pos_vec = soma_positions['pos_vec'][cell_id_permutation]
    util.plot_cluster_properties(purity, cell_types, pos_vec, reorder_synapses, 
                                 plot_truth_filename)


@transform(get_results, suffix(".samples"), 
           [(".%d.clusters.pdf" % d, ".%d.latent.pdf" % d )  for d in range(2)])
def plot_best_cluster_latent(exp_results, 
                     out_filenames):

    sample_d = pickle.load(open(exp_results))
    chains = sample_d['chains']
    
    exp = sample_d['exp']
    data_filename = exp['data_filename']
    data = pickle.load(open(data_filename))
    data_basename, _ = os.path.splitext(data_filename)
    meta = pickle.load(open(data_basename + ".meta"))