Example #1
0
  /**
   * Adds new row element to this data set
   *
   * @param row data set row to add
   */
  public void addRow(DataSetRow row) throws VectorSizeMismatchException {

    if (row == null) {
      throw new NeurophException("Training dta row cannot be null!");
    }

    // check input vector size if it is predefined
    if ((this.inputSize != 0) && (row.getInput().length != this.inputSize)) {
      throw new VectorSizeMismatchException(
          "Input vector size does not match data set input size!");
    }

    if ((this.outputSize != 0) && (row.getDesiredOutput().length != this.outputSize)) {
      throw new VectorSizeMismatchException(
          "Output vector size does not match data set output size!");
    }

    // if everything went ok add training element
    this.rows.add(row);
  }
Example #2
0
  @Test
  public void should_iterate_row_with_metadata() throws IOException {
    // given
    String[] columnNames =
        new String[] {
          "id",
          "firstname",
          "lastname",
          "state",
          "registration",
          "city",
          "birth",
          "nbCommands",
          "avgAmount"
        };

    final InputStream input = this.getClass().getResourceAsStream("dataSetRowMetadata.json");
    final ObjectMapper mapper = builder.build();
    try (JsonParser parser = mapper.getFactory().createParser(input)) {
      final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
      final Iterator<DataSetRow> iterator = dataSet.getRecords().iterator();

      List<ColumnMetadata> actualColumns = new ArrayList<>();
      int recordCount = 0;
      while (iterator.hasNext()) {
        final DataSetRow next = iterator.next();
        actualColumns = next.getRowMetadata().getColumns();
        assertThat(actualColumns, not(empty()));
        recordCount++;
      }

      // then
      assertEquals(10, recordCount);
      for (int i = 0; i < actualColumns.size(); i++) {
        final ColumnMetadata column = actualColumns.get(i);
        assertEquals(columnNames[i], column.getId());
      }
    } catch (Exception e) {
      throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e);
    }
  }
Example #3
0
  public void saveAsTxt(String filePath, String delimiter) {
    if ((delimiter == null) || delimiter.equals("")) {
      delimiter = " ";
    }

    PrintWriter out = null;

    try {
      out = new PrintWriter(new FileWriter(new File(filePath)));

      for (DataSetRow element : this.rows) {
        double[] input = element.getInput();
        for (int i = 0; i < input.length; i++) {
          out.print(input[i] + delimiter);
        }

        if (element instanceof DataSetRow) {
          double[] output = ((DataSetRow) element).getDesiredOutput();
          for (int j = 0; j < output.length; j++) {
            out.print(output[j] + delimiter);
          }
        }
        out.println();
      }

      out.flush();

    } catch (Exception e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }