/**
   * Create code for the work function of a built-in filter.
   *
   * @param filter The filter that the work function is being built for
   * @param selfID A unique id assigned in FlatIRToC
   * @param p A printer to output the generated code
   */
  static void predefinedFilterWork(SIRPredefinedFilter filter, int selfID, CodegenPrintWriter p) {

    Tape inputTape = RegisterStreams.getFilterInStream(filter);
    Tape outputTape = RegisterStreams.getFilterOutStream(filter);

    // Caller has printed function name and an iteration parameter ____n
    // Generate loop to execute body of this function ____n times,
    // In loop generate specialized code for function.
    p.print("void " + ClusterUtils.getWorkName(filter, selfID) + "(int ____n) {");
    p.indent();
    p.newLine();
    p.indent();
    //        p.println("// predefinedFilterWork " + filter.getName());

    // SIRFileReader
    if (filter instanceof SIRFileReader) {
      if (!(KjcOptions.mencoder || KjcOptions.blender)) {
        genFileReaderWork((SIRFileReader) filter, outputTape, selfID, p);
      }
      // SIRFileWriter
    } else if (filter instanceof SIRFileWriter) {
      if (!(KjcOptions.mencoder || KjcOptions.blender)) {
        genFileWriterWork((SIRFileWriter) filter, inputTape, selfID, p);
      }
      // SIRIdentity
    } else if (filter instanceof SIRIdentity) {
      assert false : "should not process SIRIdentity here";
      p.println("for (; 0 < ____n; ____n--) {");
      p.indent();
      p.println("  " + outputTape.getPushName() + "(" + inputTape.getPopName() + "());");
      p.outdent();
      p.print("}"); // end of for loop.
    } else if (filter instanceof SIRDummySink || filter instanceof SIRDummySource) {
      // DummySource and SummySink do not appear in any of our
      // application code.  Are they part of the language?
      // TODO: get right exception for unimplemented.
      throw new Error("Unsupported predefined filter " + filter.getName());
    } else {
      // TODO: get right unchecked exception for unextended code...
      throw new Error("Unknown predefined filter " + filter.getName());
    }
    p.newLine();
    p.outdent(); // end of method body
    p.outdent(); // end of method definition
    p.print("}");
    p.newLine();
    p.newLine();
  }
  /*
   * When generating (no) code for init routine, also generate File*
   * and close() for file.
   */
  private static void genFileReaderInit(
      SIRFileReader fr,
      CType return_type,
      String function_name,
      int selfID,
      List /*String*/ cleanupCode,
      CodegenPrintWriter p) {

    int id = NodeEnumerator.getSIROperatorId(fr);
    String theType = "" + fr.getOutputType();
    // dispatch to special routine for bit type
    if (theType.equals("bit")) {
      p.print("FILE* " + fpName(fr) + ";");
      p.newLine();
      p.newLine();
    }

    startParameterlessFunction(CommonUtils.CTypeToStringA(return_type, true), function_name, p);

    if (theType.equals("bit")) {
      p.print(fpName(fr) + " = fopen(\"" + fr.getFileName() + "\", \"r\");");
      p.newLine();
      p.print("assert (" + fpName(fr) + ");");
      p.newLine();
    } else {
      p.print("__file_descr__" + id + " = FileReader_open(\"" + fr.getFileName() + "\");");
      p.newLine();
      p.print("assert (__file_descr__" + id + ");");
      p.newLine();
    }

    endFunction(p);

    String closeName = ClusterUtils.getWorkName(fr, selfID) + "__close";

    startParameterlessFunction("void", closeName, p);

    if (theType.equals("bit")) {
      p.println("fclose(" + fpName(fr) + ");");
    } else {
      p.println("FileReader_close(__file_descr__" + id + ");");
    }

    endFunction(p);

    cleanupCode.add(closeName + "();\n");
  }
Exemple #3
0
  public void test1() {
    for (int r = 0; r < 1; r++) {
      Graph mim = DataGraphUtils.randomSingleFactorModel(5, 5, 6, 0, 0, 0);

      Graph mimStructure = structure(mim);

      SemImInitializationParams params = new SemImInitializationParams();
      params.setCoefRange(.5, 1.5);

      SemPm pm = new SemPm(mim);
      SemIm im = new SemIm(pm, params);
      DataSet data = im.simulateData(300, false);

      String algorithm = "FOFC";
      Graph searchGraph;
      List<List<Node>> partition;

      if (algorithm.equals("FOFC")) {
        FindOneFactorClusters fofc =
            new FindOneFactorClusters(data, TestType.TETRAD_WISHART, 0.001);
        searchGraph = fofc.search();
        partition = fofc.getClusters();
      } else if (algorithm.equals("BPC")) {
        TestType testType = TestType.TETRAD_WISHART;
        TestType purifyType = TestType.TETRAD_BASED2;

        BuildPureClusters bpc = new BuildPureClusters(data, 0.001, testType, purifyType);
        searchGraph = bpc.search();

        partition = MimUtils.convertToClusters2(searchGraph);
      } else {
        throw new IllegalStateException();
      }

      List<String> latentVarList = reidentifyVariables(mim, data, partition, 2);

      System.out.println(partition);
      System.out.println(latentVarList);

      System.out.println("True\n" + mimStructure);

      Graph mimbuildStructure;

      for (int mimbuildMethod : new int[] {3, 4}) {
        if (mimbuildMethod == 1) {
          System.out.println("Mimbuild 1\n");
          Clusters measurements = ClusterUtils.mimClusters(searchGraph);
          IndTestMimBuild test = new IndTestMimBuild(data, 0.001, measurements);
          MimBuild mimbuild = new MimBuild(test, new Knowledge2());
          Graph full = mimbuild.search();
          full = changeLatentNames(full, measurements, latentVarList);
          mimbuildStructure = structure(full);
          System.out.println(
              "SHD = "
                  + SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure));
          System.out.println("Estimated\n" + mimbuildStructure);
          System.out.println();
        }
        //                else if (mimbuildMethod == 2) {
        //                    System.out.println("Mimbuild 2\n");
        //                    Mimbuild2 mimbuild = new Mimbuild2();
        //                    mimbuild.setAlpha(0.001);
        //                    mimbuildStructure = mimbuild.search(partition, latentVarList, data);
        //                    TetradMatrix latentcov = mimbuild.getLatentsCov();
        //                    List<String> latentnames = mimbuild.getLatentNames();
        //                    System.out.println("\nCovariance over the latents");
        //                    System.out.println(MatrixUtils.toStringSquare(latentcov.toArray(),
        // latentnames));
        //                    System.out.println("Estimated\n" + mimbuildStructure);
        //                    System.out.println("SHD = " +
        // SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure));
        //                    System.out.println();
        //                }
        else if (mimbuildMethod == 3) {
          System.out.println("Mimbuild 3\n");
          Mimbuild2 mimbuild = new Mimbuild2();
          mimbuild.setAlpha(0.001);
          mimbuild.setMinClusterSize(3);
          mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
          ICovarianceMatrix latentcov = mimbuild.getLatentsCov();
          System.out.println("\nCovariance over the latents");
          System.out.println(latentcov);
          System.out.println("Estimated\n" + mimbuildStructure);
          System.out.println(
              "SHD = "
                  + SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure));
          System.out.println();
        } else if (mimbuildMethod == 4) {
          System.out.println("Mimbuild Trek\n");
          MimbuildTrek mimbuild = new MimbuildTrek();
          mimbuild.setAlpha(0.1);
          mimbuild.setMinClusterSize(3);
          mimbuildStructure = mimbuild.search(partition, latentVarList, new CovarianceMatrix(data));
          ICovarianceMatrix latentcov = mimbuild.getLatentsCov();
          System.out.println("\nCovariance over the latents");
          System.out.println(latentcov);
          System.out.println("Estimated\n" + mimbuildStructure);
          System.out.println(
              "SHD = "
                  + SearchGraphUtils.structuralHammingDistance(mimStructure, mimbuildStructure));
          System.out.println();
        } else {
          throw new IllegalStateException();
        }
      }
    }
  }
  /*
   * When generating (no) code for init routine, also generate File*
   * and close() for file.
   */
  private static void genFileWriterInit(
      SIRFileWriter fw,
      CType return_type,
      String function_name,
      int selfID,
      List /*String*/ cleanupCode,
      CodegenPrintWriter p) {

    int id = NodeEnumerator.getSIROperatorId(fw);

    String theType = "" + fw.getInputType();
    String bits_to_go = bitsToGoName(fw);
    String the_bits = theBitsName(fw);

    if (theType.equals("bit")) {
      p.println("FILE* " + fpName(fw) + ";");
    }

    if (theType.equals("bit")) {
      // the bit type is special since you can not just read or
      // write a bit.  It requires buffering in some larger
      // integer type.

      p.println(bits_type + " " + the_bits + " = 0;");
      p.println("int " + bits_to_go + " = 8 * sizeof(" + the_bits + ");");
    }
    p.newLine();

    startParameterlessFunction(CommonUtils.CTypeToStringA(return_type, true), function_name, p);

    if (theType.equals("bit")) {
      p.println(fpName(fw) + " = fopen(\"" + fw.getFileName() + "\", \"w\");");
      p.println("assert (" + fpName(fw) + ");");
    } else {
      p.print("__file_descr__" + id + " = FileWriter_open(\"" + fw.getFileName() + "\");");
      p.newLine();
      p.print("assert (__file_descr__" + id + ");");
      p.newLine();
    }

    endFunction(p);

    String closeName = ClusterUtils.getWorkName(fw, selfID) + "__close";

    startParameterlessFunction("void", closeName, p);
    if (theType.equals("bit")) {
      p.println("if (" + bits_to_go + " != 8 * sizeof(" + the_bits + ")) {");
      p.indent();
      p.println(the_bits + " = " + the_bits + " << " + bits_to_go + ";");
      p.println(
          "fwrite("
              + "&"
              + the_bits
              + ", "
              + "sizeof("
              + the_bits
              + "), "
              + "1, "
              + fpName(fw)
              + ");");
      p.outdent();
      p.println("}");
    }

    if (theType.equals("bit")) {
      p.println("fclose(" + fpName(fw) + ");");
    } else {
      p.println("FileWriter_flush(__file_descr__" + id + ");");
      p.println("FileWriter_close(__file_descr__" + id + ");");
    }

    endFunction(p);

    cleanupCode.add(closeName + "();\n");
  }