def ward_cluster(df_all, feature_names, max_cluster_num, output_dir, snapshots_dir= None, RemoveOutliers = 0, datasetType='ivscc'):
  print("\n\n\n  ***************  ward computation, max_cluster = %d  *************:" % max_cluster_num)

  if not os.path.exists(output_dir):
    os.mkdir(output_dir)
  else:
    os.system("rm -r  " + output_dir + '/*')

  #### similarity plots
  # df_simMatrix = distance_matrix(df_all, feature_names, output_dir + "/morph_features_similarity_matrix.csv", 1)
  # # visualize heatmap using ward on similarity matrix
  # out = heatmap_plot_distancematrix(df_simMatrix, df_all, output_dir, "Similarity")
  # linkage = out.dendrogram_row.calculated_linkage


  ##### zscores  featuer plots
  df_zscores, df_all_outlier_removed, df_outliers = get_zscore_features(df_all, feature_names,
      output_dir + '/zscore.csv', RemoveOutliers)
  if (df_outliers.shape[0] > 0 ):
    output_single_cluster_results(df_outliers, output_dir, "outliers", snapshots_dir)

  if datasetType =='ivscc':
      linkage = heatmap_plot_zscore_ivscc(df_zscores, df_all_outlier_removed, output_dir, "feature zscores")
  if datasetType =='bbp':
      linkage = heatmap_plot_zscore_bbp(df_zscores, df_all_outlier_removed, output_dir, "feature zscores")

  assignments = hierarchy.fcluster(linkage, max_cluster_num, criterion="maxclust")
  #hierarchy.dendrogram(linkage)

  ## put assignments into ano files and csv files
  clusters_list = output_clusters(assignments, df_zscores, df_all_outlier_removed, feature_names, output_dir, snapshots_dir)
  dunn_index = dunn(clusters_list)
  print("dunn index is %f" % dunn_index)
  return linkage,df_zscores