private void loadData(boolean drawLast) { ChartCollection collection = fromDevice ? fileManager.getCollection() : fileManager.getPcaCollection(); if (collection != null) { int size = collection.size(); if (size > 0) { List<Entry> entries = new ArrayList<>(); List<String> xVals = new ArrayList<>(); ChartData data = collection.getCharData(size - 1); Map<Double, Double> values = data.getData(); List<Double> keyList = new ArrayList<>(); keyList.addAll(values.keySet()); Collections.sort(keyList); for (Double x : keyList) { Number numX = x; int intX = numX.intValue(); xVals.add(String.valueOf(x)); double value = values.get(x); Number num = value; float val = num.floatValue(); Entry entry = new Entry(val, intX); entries.add(entry); } LineDataSet dataSet = new LineDataSet(entries, "Dados"); dataSet.setDrawCircleHole(false); dataSet.setDrawCircles(false); LineData chartData = new LineData(xVals, dataSet); chart.setData(chartData); totalGraphs = size; if (drawLast) { currentGraph = totalGraphs; } else { if (currentGraph < 1 || currentGraph > size) { currentGraph = totalGraphs; } } mainHandler.post( new Runnable() { @Override public void run() { chart.notifyDataSetChanged(); chart.invalidate(); txtTotal.setText(String.valueOf(totalGraphs)); txtCurrent.setText(String.valueOf(currentGraph)); } }); } } }
public PcaAsyncTask(Context context, List<String> files, int numComponents) { super(context); mFiles = files; mFileManager = FileManager.getSingleton(context); mNumComponents = numComponents; mShowComponentsMsg = false; }
private void fillFilesListView() { File defaultPath = fileManager.getDefaultFolder(); String[] filesInPath = defaultPath.list(); ListAdapter filesAdapter = new ArrayAdapter<>( getContext(), android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, filesInPath); listFiles.setAdapter(filesAdapter); listFiles.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); fileManager = FileManager.getSingleton(getContext()); DeviceConnectionListener listener = null; Bundle args = getArguments(); if (args != null) { fromDevice = args.getBoolean(ARG_FROM_DEVICE); if (fromDevice) { listener = this; } } usbConn = UsbConnection.getSingleton(getContext(), listener); mainHandler = new Handler(getContext().getMainLooper()); }
@Override public Map<String, ChartCollection> loadInBackground() { ChartCollection collection = new ChartCollection("PCA"); File defaultFolder = mFileManager.getDefaultFolder(); Uri uriDefaultFolder = Uri.parse("file://" + defaultFolder.getAbsolutePath()); for (String filename : mFiles) { Uri uriFile = uriDefaultFolder.buildUpon().appendPath(filename).build(); ChartCollection fileCollection = mFileManager.readFileForPca(uriFile); if (fileCollection != null) { for (ChartData chartData : fileCollection) { collection.addChartData(chartData); } } } if (mNumComponents > collection.size()) { mShowComponentsMsg = true; mNumComponents = collection.size(); } double[][] array = new double[collection.size()][2048]; for (int i = 0; i < collection.size(); i++) { for (int j = 0; j < 2048; j++) { array[i][j] = collection.getCharData(i).getData().get((double) j); } } Matrix matrix = new Matrix(array); // SingularValueDecomposition svd = matrix.svd(); // SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Pca pca = new Pca(matrix, mNumComponents); StringWriter strWriter = new StringWriter(); PrintWriter writer = new PrintWriter(strWriter); matrix.print(writer, 10, 0); Log.d("Visio", "Matrix = " + strWriter.toString()); Map<String, ChartCollection> pcaCollections = new HashMap<>(2); ChartCollection lCollection = new ChartCollection("PCA - L"); Matrix lMatrix = pca.getL(); double[][] lArray = lMatrix.getArray(); for (int i = 0; i < lMatrix.getRowDimension(); i++) { Map<Double, Double> data = new HashMap<>(); for (int j = 0; j < lMatrix.getColumnDimension(); j++) { data.put((double) j, lArray[i][j]); } ChartData chartData = new ChartData("PCA", "PCA", new Date().getTime(), data); lCollection.addChartData(chartData); } pcaCollections.put("L", lCollection); ChartCollection tCollection = new ChartCollection("PCA - T"); Matrix tMatrix = pca.getT(); double[][] tArray = tMatrix.getArray(); for (int i = 0; i < tMatrix.getRowDimension(); i++) { Map<Double, Double> data = new HashMap<>(); for (int j = 0; j < tMatrix.getColumnDimension(); j++) { data.put((double) j, tArray[i][j]); } ChartData chartData = new ChartData("PCA", "PCA", new Date().getTime(), data); tCollection.addChartData(chartData); } pcaCollections.put("T", tCollection); return pcaCollections; }
private void createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment view = inflater.inflate(R.layout.fragment_pca_tab, container, false); layoutProgress = view.findViewById(R.id.layoutProgress); layoutProgress.setVisibility(View.GONE); layoutPCAGraphOptions = view.findViewById(R.id.layoutPCAGraphOptions); layoutPCAGraphOptions.setVisibility(View.GONE); listFiles = (ListView) view.findViewById(R.id.listArquivos); txtComponentes = (TextView) view.findViewById(R.id.txtComponentes); txtComponentes.setText("20"); seekComponentes = (SeekBar) view.findViewById(R.id.seekComponentes); seekComponentes.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (progress == 0) { seekBar.setProgress(1); progress = 1; } txtComponentes.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO numComponentes = seekBar.getProgress(); } }); seekComponentes.setProgress(20); numComponentes = 20; btnPCA = (Button) view.findViewById(R.id.btnPCA); btnPCA.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // TODO: btnPCA.setEnabled(false); ArrayList<String> files = new ArrayList<>(); SparseBooleanArray checkedPositions = listFiles.getCheckedItemPositions(); for (int i = 0; i < listFiles.getCount(); i++) { if (checkedPositions.get(i)) { String strFile = (String) listFiles.getItemAtPosition(i); if (!TextUtils.isEmpty(strFile)) { files.add(strFile); } else { // TODO: } } } if (files.isEmpty()) { // TODO: show alert, need to select at least one file return; } Bundle args = new Bundle(); args.putStringArrayList(ARG_FILENAMES, files); getLoaderManager().restartLoader(LOADER_PCA, args, PcaTabFragment.this).forceLoad(); } }); radioGroupMatrizes = (RadioGroup) view.findViewById(R.id.radioGroupMatrizes); radioMatrizesL = (RadioButton) view.findViewById(R.id.radioMatrizesL); radioMatrizesL.setChecked(true); spinX = (Spinner) view.findViewById(R.id.spinX); spinY = (Spinner) view.findViewById(R.id.spinY); btnPlotPCA = (Button) view.findViewById(R.id.btnPlotPCA); btnPlotPCA.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { ChartCollection collection = null; switch (radioGroupMatrizes.getCheckedRadioButtonId()) { case R.id.radioMatrizesL: collection = chartCollections.get("L"); break; case R.id.radioMatrizesT: collection = chartCollections.get("T"); break; } if (collection != null) { int x = spinX.getSelectedItemPosition(); int y = spinY.getSelectedItemPosition(); ChartCollection finalCollection = new ChartCollection(String.format("PCA - (%d, %d)", x, y)); Map<Double, Double> data = new HashMap<>(); for (ChartData chartData : collection) { // TODO: Number numX = chartData.getData().get((double) x); data.put(numX.doubleValue(), chartData.getData().get((double) y)); } ChartData chartData = new ChartData("PCA", "PCA", new Date().getTime(), data); finalCollection.addChartData(chartData); listener.onPlotChart(finalCollection); } } }); fileManager = FileManager.getSingleton(getContext()); if (fileManager.askForStoragePermission( getActivity(), REQUEST_FILE_PERMISSION, Manifest.permission.READ_EXTERNAL_STORAGE)) { fillFilesListView(); } mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime); }