protected void compareDatasets(Instances data1, Instances data2)
    throws Exception {
    
    if (data1.numAttributes() != data2.numAttributes())
      throw new Exception("number of attributes has changed");
    
    if (!(data2.numInstances() == data1.numInstances()))
      throw new Exception("number of instances has changed");

    for (int i = 0; i < data2.numInstances(); i++) {
      Instance orig = data1.instance(i);
      Instance copy = data2.instance(i);
      for (int j = 0; j < orig.numAttributes(); j++) {
        if (orig.isMissing(j)) {
          if (!copy.isMissing(j))
            throw new Exception("instances have changed");
        }
        else if (!orig.toString(j).equals(copy.toString(j))) {
          throw new Exception("instances have changed");
        }
        
        if (orig.weight() != copy.weight())
          throw new Exception("instance weights have changed");
      }
    }
  }