Exemplo n.º 1
0
  /**
   * Permet de calculer les données de la simulation
   *
   * @param file Fichier texte dans lequel lire les données utiles
   */
  public static void executResult(String file) {
    // Récupération des donnée contenu dans le fichier
    SimulationData sd = IOFile.read(file);

    // Calcule de données
    long[] dCall = DataConverter.toDuration(sd.callStart, sd.callGoal);
    float moyenneCall = MathOperation.avarage(dCall);
    float ecartCall = MathOperation.standardDeviation(dCall);

    long[] dTravel = DataConverter.toDuration(sd.travelStart, sd.travelGoal);
    float moyenneTravel = MathOperation.avarage(dTravel);
    float ecartTravel = MathOperation.standardDeviation(dTravel);

    long[] dGlobalUserTravel = DataConverter.toDuration(sd.callStart, sd.travelGoal);
    float moyenneGlobalUserTravel = MathOperation.avarage(dGlobalUserTravel);
    float ecartGlobalUserTravel = MathOperation.standardDeviation(dGlobalUserTravel);

    // Ecriture des données calculé
    String toWrite =
        "Temps moyen d'arriver de l'ascenseur après appel: " + Float.toString(moyenneCall);
    toWrite +=
        "Ecart type du temps d'arriver de l'ascenseur après appel: " + Float.toString(ecartCall);
    toWrite += "Temps moyendu trajet de l'ascenseur: " + Float.toString(moyenneTravel);
    toWrite += "Ecart type du tempe de trajet de l'ascenseur: " + Float.toString(ecartTravel);
    toWrite +=
        "Temps moyendu trajet total de l'utilisateur (en milliseconde) : "
            + Float.toString(moyenneGlobalUserTravel);
    toWrite +=
        "Ecart type du tempe de trajet total de l'utilisateur: "
            + Float.toString(ecartGlobalUserTravel);
    IOFile.write(toWrite);
  }
Exemplo n.º 2
0
  /**
   * Loads the data from a file
   *
   * @param fs file system
   * @param fpath data file path
   * @throws IOException if any problem is encountered
   */
  public static Data loadData(Dataset dataset, FileSystem fs, Path fpath) throws IOException {
    FSDataInputStream input = fs.open(fpath);
    Scanner scanner = new Scanner(input);

    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      if (line.isEmpty()) {
        log.warn("{}: empty string", instances.size());
        continue;
      }

      Instance instance = converter.convert(instances.size(), line);
      if (instance == null) {
        // missing values found
        log.warn("{}: missing values", instances.size());
        continue;
      }

      instances.add(instance);
    }

    scanner.close();

    return new Data(dataset, instances);
  }
Exemplo n.º 3
0
  public static double Mahalanobis(double[] data, WormGene g) {
    // Compute Mahalanobis distance from worm gene data

    assert data.length == g.dim;
    int dim = g.dim;

    Vector<double[]> eigvec = DataConverter.mat2Jvector(g.eigenvectors);
    double[] eigval = DataConverter.colMat2Double(g.eigenvalues);

    for (int i = 0; i < dim; i++) eigval[i] += Configs.MDIS_EPSILON;

    for (int i = 0; i < dim; i++) assert eigval[i] != 0;

    RealVector v1 = MatrixUtils.createRealVector(data);
    RealVector[] vec = new RealVector[dim];
    for (int i = 0; i < dim; i++) {
      vec[i] = MatrixUtils.createRealVector(eigvec.elementAt(i));
    }
    double[] d = new double[dim];
    for (int i = 0; i < dim; i++) {
      d[i] = v1.dotProduct(vec[i]) / Math.sqrt(eigval[i]);
    }

    RealVector dis = MatrixUtils.createRealVector(d);

    return vectorLength(dis);
  }
Exemplo n.º 4
0
  @NonNull
  public Observable<PageResult<T>> load(
      @NonNull DataConverter<T> dataConverter,
      @NonNull DatabaseQuery databaseQuery,
      @NonNull Observable<Page> pageObservable) {
    final SparseArrayCompat<T> allItems = new SparseArrayCompat<>();
    final Observable<Cursor> cursorObservable =
        database.load(databaseQuery).doOnNext(cursor -> allItems.clear());

    return Observable.combineLatest(
        pageObservable,
        cursorObservable,
        (page, cursor) -> {
          final List<T> pageItems = new ArrayList<>();
          for (int i = page.getStart(),
                  size = Math.min(cursor.getCount(), page.getStart() + page.getSize());
              i < size;
              i++) {
            T item = allItems.get(i);
            if (item == null) {
              cursor.moveToPosition(i);
              item = dataConverter.from(cursor);
              allItems.put(i, item);
            }
            pageItems.add(item);
          }

          return new PageResult<>(cursor, allItems, pageItems, page);
        });
  }
  public void write(Collection<SinkRecord> records) {
    for (SinkRecord sinkRecord : records) {
      final String indexOverride = topicToIndexMap.get(sinkRecord.topic());
      final String index = indexOverride != null ? indexOverride : sinkRecord.topic();
      final boolean ignoreKey = ignoreKeyTopics.contains(sinkRecord.topic()) || this.ignoreKey;
      final boolean ignoreSchema =
          ignoreSchemaTopics.contains(sinkRecord.topic()) || this.ignoreSchema;

      if (!ignoreSchema && !existingMappings.contains(index)) {
        try {
          if (Mapping.getMapping(client, index, type) == null) {
            Mapping.createMapping(client, index, type, sinkRecord.valueSchema());
          }
        } catch (IOException e) {
          // FIXME: concurrent tasks could attempt to create the mapping and one of the requests may
          // fail
          throw new ConnectException("Failed to initialize mapping for index: " + index, e);
        }
        existingMappings.add(index);
      }

      final IndexableRecord indexableRecord =
          DataConverter.convertRecord(sinkRecord, index, type, ignoreKey, ignoreSchema);

      bulkProcessor.add(indexableRecord, flushTimeoutMs);
    }
  }
Exemplo n.º 6
0
  public static void draw() {
    //		Vector<Point> p = new Vector<Point>();
    //		Point p1 = new Point(1,0);
    //		p1.setLocation(2, 7);
    //		p.addElement(p1);
    //		p1.setLocation(5, 7);
    //		p.addElement(p1);
    //		p1.setLocation(1, 3);
    //		p.addElement(p1);
    //		p1.setLocation(2, 4);
    //		p.addElement(p1);
    //		p1.setLocation(5, 1);
    //		p.addElement(p1);
    //
    Vector<double[]> points = new Vector<double[]>();
    double[] p1 = {0.25, 0.82};
    double[] p2 = {0.73, 0.16};
    double[] p3 = {0.12, 0.21};
    double[] p4 = {0.92, 0.80};
    double[] p5 = {0.16, 0.46};
    points.addElement(p1);
    points.addElement(p2);
    points.addElement(p3);
    points.addElement(p4);
    points.addElement(p5);

    Mat mp = DataConverter.jvector2Mat(points);
    //		System.out.println(mp.dump());

    //		new Visu(points);
  }
Exemplo n.º 7
0
 public static void V2M() {
   Vector<double[]> v = new Vector<double[]>();
   double[] temp = {1.0, 2.0, 3.0, 4.0};
   v.add(temp);
   v.add(temp);
   v.add(temp);
   Mat m = DataConverter.jvector2Mat(v);
   System.out.println(m.dump());
 }
Exemplo n.º 8
0
  /**
   * Registers FFI ruby classes/modules
   *
   * @param module the module to register the classes under
   */
  public void init(Ruby runtime, RubyModule ffi) {
    synchronized (ffi) {
      if (ffi.getClass("Type") == null) {
        Type.createTypeClass(runtime, ffi);
      }
      DataConverter.createDataConverterModule(runtime, ffi);

      if (ffi.getClass(AbstractMemory.ABSTRACT_MEMORY_RUBY_CLASS) == null) {
        AbstractMemory.createAbstractMemoryClass(runtime, ffi);
      }
      if (ffi.getClass("Buffer") == null) {
        Buffer.createBufferClass(runtime, ffi);
      }
      if (ffi.getClass("Pointer") == null) {
        Pointer.createPointerClass(runtime, ffi);
      }
      if (ffi.getClass("AutoPointer") == null) {
        AutoPointer.createAutoPointerClass(runtime, ffi);
      }
      if (ffi.getClass("MemoryPointer") == null) {
        MemoryPointer.createMemoryPointerClass(runtime, ffi);
      }
      if (ffi.getClass("Struct") == null) {
        Struct.createStructClass(runtime, ffi);
      }
      if (ffi.getClass(StructLayout.CLASS_NAME) == null) {
        StructLayout.createStructLayoutClass(runtime, ffi);
      }
      if (ffi.getClass("StructByValue") == null) {
        StructByValue.createStructByValueClass(runtime, ffi);
      }
      if (ffi.getClass(AbstractInvoker.CLASS_NAME) == null) {
        AbstractInvoker.createAbstractInvokerClass(runtime, ffi);
      }
      if (ffi.getClass(CallbackInfo.CLASS_NAME) == null) {
        CallbackInfo.createCallbackInfoClass(runtime, ffi);
      }
      if (ffi.getClass("Enum") == null) {
        Enum.createEnumClass(runtime, ffi);
      }
      if (ffi.getClass("Type").getClass("Mapped") == null) {
        MappedType.createConverterTypeClass(runtime, ffi);
      }
      if (ffi.getClass(FileDescriptorIO.CLASS_NAME) == null) {
        FileDescriptorIO.createFileDescriptorIOClass(runtime, ffi);
      }

      ffi.setConstant("TypeDefs", RubyHash.newHash(runtime));

      Platform.createPlatformModule(runtime, ffi);
      IOModule.createIOModule(runtime, ffi);

      StructByReference.createStructByReferenceClass(runtime, ffi);
    }
  }
Exemplo n.º 9
0
  /** Loads the data from a String array */
  public static Data loadData(Dataset dataset, String[] data) {
    List<Instance> instances = new ArrayList<>();

    DataConverter converter = new DataConverter(dataset);

    for (String line : data) {
      if (!line.isEmpty()) {
        Instance instance = converter.convert(line);
        if (instance != null) {
          instances.add(instance);
        } else {
          // missing values found
          log.warn("{}: missing values", instances.size());
        }
      } else {
        log.warn("{}: empty string", instances.size());
      }
    }

    return new Data(dataset, instances);
  }
Exemplo n.º 10
0
  /** Loads the data from a String array */
  public static Data loadData(Dataset dataset, String[] data) {
    List<Instance> instances = Lists.newArrayList();

    DataConverter converter = new DataConverter(dataset);

    for (String line : data) {
      if (line.isEmpty()) {
        log.warn("{}: empty string", instances.size());
        continue;
      }

      Instance instance = converter.convert(instances.size(), line);
      if (instance == null) {
        // missing values found
        log.warn("{}: missing values", instances.size());
        continue;
      }

      instances.add(instance);
    }

    return new Data(dataset, instances);
  }
Exemplo n.º 11
0
  public static void M2V() {

    Mat data =
        new Mat(3, 4, CvType.CV_32F) {
          {
            put(0, 0, 1, 2, 2, 4);
            put(1, 0, 2, 4, 4, 8);
            put(2, 0, 3, 6, 6, 12);
          }
        };

    Vector<double[]> v = DataConverter.mat2Jvector(data);
    //		VecOperation.printVectorArray(v);

  }
  @Override
  public List<SourceRecord> poll() throws InterruptedException {
    ArrayList<SourceRecord> records = new ArrayList<>();

    MaxwellAbstractRowsEvent event;
    try {
      event = replicator.getEvent();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return records;
    }
    try {
      this.maxwellContext.ensurePositionThread();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return records;
    }

    if (event == null) {
      return records;
    }

    if (event.getTable().getDatabase().getName().equals("maxwell")) {
      return records;
    }

    String databaseName = event.getDatabase().getName();
    String tableName = event.getTable().getName();

    String topicName = databaseName + "." + tableName;

    Table table = event.getTable();

    List<Row> rows = event.filteredRows();
    // databaseName.tableName
    // create schema for primary key
    Schema pkSchema = DataConverter.convertPrimaryKeySchema(table);

    List<Struct> primaryKeys = new ArrayList<Struct>();

    for (Row row : rows) {
      // make primary key schema
      Struct pkStruct = DataConverter.convertPrimaryKeyData(pkSchema, table, row);
      primaryKeys.add(pkStruct);
    }

    Iterator<Row> rowIter = rows.iterator();
    Iterator<Struct> pkIter = primaryKeys.iterator();

    while (rowIter.hasNext() && pkIter.hasNext()) {
      Row row = rowIter.next();

      // create schema
      Schema rowSchema = DataConverter.convertRowSchema(table);

      Struct rowStruct = DataConverter.convertRowData(rowSchema, table, row);

      Struct key = pkIter.next();

      System.out.print("got a maxwell event!");
      System.out.println(row);
      SourceRecord rec =
          new SourceRecord(
              sourcePartition(),
              sourceOffset(event),
              topicName,
              null, // partition
              pkSchema,
              key,
              rowSchema,
              rowStruct);
      records.add(rec);
    }
    return records;
  }
  @Override
  public List<SourceRecord> poll() throws InterruptedException {

    try {
      MaxwellAbstractRowsEvent event = replicator.getEvent();
      this.maxwellContext.ensurePositionThread();

      if (event == null) {
        return null;
      }

      if (event.getTable().getDatabase().getName().equals("maxwell")) {
        return null;
      }

      String databaseName = event.getDatabase().getName();
      String tableName = event.getTable().getName();

      String topicName = databaseName + "." + tableName;

      ArrayList<SourceRecord> records = new ArrayList<>();

      Table table = event.getTable();

      List<Row> rows = event.filteredRows();
      // databaseName.tableName
      // create schema for primary key
      Schema pkSchema = DataConverter.convertPrimaryKeySchema(table);

      List<Struct> primaryKeys = new ArrayList<Struct>();

      for (Row row : rows) {
        // make primary key schema
        Struct pkStruct = new Struct(pkSchema);

        for (String pk : table.getPKList()) {
          int idx = table.findColumnIndex(pk);

          Column column = row.getColumns().get(idx);
          ColumnDef def = table.getColumnList().get(idx);

          switch (def.getType()) {
            case "int":
              IntColumnDef intDef = (IntColumnDef) def;
              Long l = intDef.toLong(column.getValue());
              pkStruct.put(pk, l.intValue());
              break;
            default:
              throw new RuntimeException("unsupported type");
          }
        }
        primaryKeys.add(pkStruct);
      }

      Iterator<String> jsonIter = event.toJSONStrings().iterator();
      //            Iterator<String> keysIter = event.getPKStrings().iterator();

      Iterator<Struct> pkIter = primaryKeys.iterator();

      while (jsonIter.hasNext() && pkIter.hasNext()) {
        String json = jsonIter.next();
        Struct key = pkIter.next();

        System.out.print("got a maxwell event!");
        System.out.println(json);
        SourceRecord rec =
            new SourceRecord(
                sourcePartition(),
                sourceOffset(event),
                topicName,
                null, // partition
                pkSchema,
                key,
                VALUE_SCHEMA,
                json);
        records.add(rec);
      }
      return records;

      // return records;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }
Exemplo n.º 14
0
 /**
  * Load the Data for the given class or method. This method will try to find {@link DataLoader} on
  * either the class level or the method level. In case the annotation is found, this method will
  * load the data using the specified loader class and then save it in the DataContext for further
  * use by the system. We also create another copy of the input test data that we store in the
  * {@link DataDrivenTestRunner#writableData} field. This is done in order to facilitate the
  * writing of the data that might be returned by the test method.
  *
  * @param testClass the class object, if any.
  * @param method current executing method, if any.
  * @param currentTestClass the currently executing test class. this is used to append in front of
  *     the method name to get unique method names as there could be methods in different classes
  *     with the same name and thus we want to avoid conflicts.
  * @param writableData The writable data that is used internally for reporting purposes
  */
 public static void loadData(
     Class<?> testClass,
     FrameworkMethod method,
     TestClass currentTestClass,
     Map<String, List<Map<String, Object>>> writableData) {
   if (testClass == null && method == null) {
     Assert.fail(
         "The framework should provide either the testClass parameter or the method parameter in order to load the test data.");
   }
   // We give priority to Class Loading and then to method loading
   DataLoader testData = null;
   if (testClass != null) {
     testData = testClass.getAnnotation(DataLoader.class);
   } else {
     testData = method.getAnnotation(DataLoader.class);
   }
   if (testData != null) {
     TestInfo testInfo = DataLoaderUtil.determineLoader(testData, currentTestClass);
     Loader dataLoader = testInfo.getDataLoader();
     if (testInfo.getDataLoader() == null) {
       Assert.fail(
           "The framework currently does not support the specified Loader type. "
               + "You can provide the custom Loader by choosing LoaderType.CUSTOM in TestData "
               + "annotation and providing your custom loader using DataLoader annotation.");
     } else {
       if (testInfo.getFilePaths() == null || testInfo.getFilePaths().length == 0) {
         // implies that there exists a CUSTOM loader that loads the data using Java classes
         Map<String, List<Map<String, Object>>> data = dataLoader.loadData(new EmptyResource());
         // We also maintain the copy of the actual data for our write functionality.
         writableData.putAll(data);
         DataContext.setData(DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
         DataContext.setConvertedData(
             DataConverter.convert(data, currentTestClass.getJavaClass()));
       } else {
         ResourceLoader resourceLoader =
             new ResourceLoaderStrategy(currentTestClass.getJavaClass());
         for (String filePath : testInfo.getFilePaths()) {
           Resource resource = resourceLoader.getResource(filePath);
           try {
             if (resource.exists()) {
               Map<String, List<Map<String, Object>>> data = dataLoader.loadData(resource);
               // We also maintain the copy of the actual data for our write functionality.
               writableData.putAll(data);
               DataContext.setData(
                   DataConverter.appendClassName(data, currentTestClass.getJavaClass()));
               DataContext.setConvertedData(
                   DataConverter.convert(data, currentTestClass.getJavaClass()));
             } else {
               LOG.warn(
                   "Resource {} does not exists in the specified path. If it is a classpath resource, use 'classpath:' "
                       + "before the path name, else check the path.",
                   resource);
             }
           } catch (Exception e) {
             LOG.error(
                 "Exception occured while trying to load the data for resource {}", resource, e);
             throw new RuntimeException(e);
           }
         }
       }
     }
   }
 }