56 |
56 |
* @author sloiseau
|
57 |
57 |
*/
|
58 |
58 |
public class MatrixImpl extends QuantitativeDataStructureImpl implements Matrix {
|
59 |
|
|
|
59 |
|
60 |
60 |
public static int[][] asIntMatrix(REXP r) throws REXPMismatchException {
|
61 |
|
|
|
61 |
|
62 |
62 |
int[] ct = r.asIntegers();
|
63 |
63 |
if (ct == null) return null;
|
64 |
64 |
REXP dim = r.getAttribute("dim"); //$NON-NLS-1$
|
65 |
65 |
int[] ds = dim.asIntegers();
|
66 |
66 |
if (ds == null || ds.length != 2)
|
67 |
67 |
return null; // matrix must be 2-dimensional
|
68 |
|
|
|
68 |
|
69 |
69 |
int m = ds[0], n = ds[1];
|
70 |
70 |
int[][] rez = new int[m][n];
|
71 |
|
|
|
71 |
|
72 |
72 |
// R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
|
73 |
73 |
// we need to copy everything, since we create 2d array from 1d array
|
74 |
74 |
int i = 0, k = 0;
|
... | ... | |
81 |
81 |
}
|
82 |
82 |
return rez;
|
83 |
83 |
}
|
84 |
|
|
85 |
|
// /** The ncol. */
|
86 |
|
// protected int ncol;
|
87 |
|
//
|
88 |
|
// /** The nrow. */
|
89 |
|
// protected int nrow;
|
90 |
|
|
|
84 |
|
|
85 |
// /** The ncol. */
|
|
86 |
// protected int ncol;
|
|
87 |
//
|
|
88 |
// /** The nrow. */
|
|
89 |
// protected int nrow;
|
|
90 |
|
91 |
91 |
/**
|
92 |
92 |
* Instantiates a new matrix impl.
|
93 |
93 |
*
|
... | ... | |
97 |
97 |
public MatrixImpl(double[][] matrix) throws RWorkspaceException {
|
98 |
98 |
super();
|
99 |
99 |
rw.addMatrixToWorkspace(symbol, matrix);
|
100 |
|
// this.nrow = matrix.length;
|
101 |
|
// this.ncol = matrix[0].length;
|
102 |
|
//this.rows = null;
|
103 |
|
//this.cols = null;
|
|
100 |
// this.nrow = matrix.length;
|
|
101 |
// this.ncol = matrix[0].length;
|
|
102 |
// this.rows = null;
|
|
103 |
// this.cols = null;
|
104 |
104 |
}
|
105 |
105 |
|
106 |
106 |
/**
|
... | ... | |
112 |
112 |
public MatrixImpl(double[][] matrix, int ncol, int nrow) throws RWorkspaceException {
|
113 |
113 |
super();
|
114 |
114 |
rw.addMatrixToWorkspace(symbol, matrix, ncol, nrow);
|
115 |
|
// this.nrow = matrix.length;
|
116 |
|
// this.ncol = matrix[0].length;
|
117 |
|
//this.rows = null;
|
118 |
|
//this.cols = null;
|
|
115 |
// this.nrow = matrix.length;
|
|
116 |
// this.ncol = matrix[0].length;
|
|
117 |
// this.rows = null;
|
|
118 |
// this.cols = null;
|
119 |
119 |
}
|
120 |
|
|
|
120 |
|
121 |
121 |
/**
|
122 |
122 |
* Instantiates a new matrix impl.
|
123 |
123 |
*
|
... | ... | |
127 |
127 |
public MatrixImpl(DoubleMatrix2D matrix) throws RWorkspaceException {
|
128 |
128 |
super();
|
129 |
129 |
rw.addMatrixToWorkspace(symbol, matrix);
|
130 |
|
// this.nrow = matrix.rows();
|
131 |
|
// this.ncol = matrix.columns();
|
132 |
|
//this.rows = null;
|
133 |
|
//this.cols = null;
|
|
130 |
// this.nrow = matrix.rows();
|
|
131 |
// this.ncol = matrix.columns();
|
|
132 |
// this.rows = null;
|
|
133 |
// this.cols = null;
|
134 |
134 |
}
|
135 |
|
|
|
135 |
|
136 |
136 |
/**
|
137 |
137 |
* Create a new matrix given a {@link DoubleMatrix2D}, with row and col
|
138 |
138 |
* names.
|
... | ... | |
145 |
145 |
public MatrixImpl(DoubleMatrix2D matrix, String[] rowNames,
|
146 |
146 |
String[] columnNames) throws RWorkspaceException {
|
147 |
147 |
this(matrix);
|
148 |
|
|
|
148 |
|
149 |
149 |
if (rowNames == null) {
|
150 |
150 |
throw new IllegalArgumentException(StatsEngineCoreMessages.rowNamesOfAContingencyTableCannotBeNull);
|
151 |
151 |
}
|
152 |
|
// if (rowNames.length != nrow) {
|
153 |
|
// throw new IllegalArgumentException(Messages.MatrixImpl_5);
|
154 |
|
// }
|
155 |
|
//this.rows = new VectorImpl(rowNames);
|
|
152 |
// if (rowNames.length != nrow) {
|
|
153 |
// throw new IllegalArgumentException(Messages.MatrixImpl_5);
|
|
154 |
// }
|
|
155 |
// this.rows = new VectorImpl(rowNames);
|
156 |
156 |
rw.assignRowNamesToMatrix(symbol, new VectorImpl(rowNames).getSymbol());
|
157 |
|
|
|
157 |
|
158 |
158 |
if (columnNames == null) {
|
159 |
159 |
throw new IllegalArgumentException(StatsEngineCoreMessages.columnNamesOfAContingencyTableCannotBeNull);
|
160 |
160 |
}
|
161 |
|
// if (columnNames.length != ncol) {
|
162 |
|
// throw new IllegalArgumentException(Messages.MatrixImpl_7
|
163 |
|
// + columnNames.length + ", " + ncol + ")."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
164 |
|
// }
|
165 |
|
//this.cols = new VectorImpl(columnNames);
|
|
161 |
// if (columnNames.length != ncol) {
|
|
162 |
// throw new IllegalArgumentException(Messages.MatrixImpl_7
|
|
163 |
// + columnNames.length + ", " + ncol + ")."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
164 |
// }
|
|
165 |
// this.cols = new VectorImpl(columnNames);
|
166 |
166 |
rw.assignColNamesToMatrix(symbol, new VectorImpl(columnNames).getSymbol());
|
167 |
|
|
|
167 |
|
168 |
168 |
}
|
169 |
|
|
|
169 |
|
170 |
170 |
/**
|
171 |
171 |
* Instantiates a new matrix impl.
|
172 |
172 |
*
|
... | ... | |
176 |
176 |
public MatrixImpl(int[][] matrix, int ncol, int nrow) throws RWorkspaceException {
|
177 |
177 |
super();
|
178 |
178 |
rw.addMatrixToWorkspace(symbol, matrix, ncol, nrow);
|
179 |
|
// this.nrow = matrix.length;
|
180 |
|
// if (matrix.length > 0)
|
181 |
|
// this.ncol = matrix[0].length;
|
182 |
|
// else
|
183 |
|
// this.ncol = 0;
|
184 |
|
//this.rows = null;
|
185 |
|
//this.cols = null;
|
|
179 |
// this.nrow = matrix.length;
|
|
180 |
// if (matrix.length > 0)
|
|
181 |
// this.ncol = matrix[0].length;
|
|
182 |
// else
|
|
183 |
// this.ncol = 0;
|
|
184 |
// this.rows = null;
|
|
185 |
// this.cols = null;
|
186 |
186 |
}
|
187 |
187 |
|
188 |
188 |
/**
|
... | ... | |
194 |
194 |
public MatrixImpl(int[][] matrix) throws RWorkspaceException {
|
195 |
195 |
super();
|
196 |
196 |
rw.addMatrixToWorkspace(symbol, matrix);
|
197 |
|
// this.nrow = matrix.length;
|
198 |
|
// if (matrix.length > 0)
|
199 |
|
// this.ncol = matrix[0].length;
|
200 |
|
// else
|
201 |
|
// this.ncol = 0;
|
202 |
|
//this.rows = null;
|
203 |
|
//this.cols = null;
|
|
197 |
// this.nrow = matrix.length;
|
|
198 |
// if (matrix.length > 0)
|
|
199 |
// this.ncol = matrix[0].length;
|
|
200 |
// else
|
|
201 |
// this.ncol = 0;
|
|
202 |
// this.rows = null;
|
|
203 |
// this.cols = null;
|
204 |
204 |
}
|
205 |
|
|
|
205 |
|
|
206 |
@Override
|
206 |
207 |
public void exchangeColumns(int c1, int c2) {
|
207 |
208 |
try {
|
208 |
209 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance();
|
209 |
|
rw.eval("tmp <- "+symbol+"[,"+c1+"]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
210 |
|
rw.eval(symbol+"[,"+c1+"] <- "+symbol+"[,"+c2+"]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
211 |
|
rw.eval(symbol+"[,"+c2+"] <- tmp"); //$NON-NLS-1$ //$NON-NLS-2$
|
212 |
|
rw.eval("tmp <- colnames("+symbol+")["+c1+"]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
213 |
|
rw.eval("colnames("+symbol+")["+c1+"] <- colnames("+symbol+")["+c2+"]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
214 |
|
rw.eval("colnames("+symbol+")["+c2+"] <- tmp"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
215 |
|
} catch (RWorkspaceException e) {
|
|
210 |
rw.eval("tmp <- " + symbol + "[," + c1 + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
211 |
rw.eval(symbol + "[," + c1 + "] <- " + symbol + "[," + c2 + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
|
212 |
rw.eval(symbol + "[," + c2 + "] <- tmp"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
213 |
rw.eval("tmp <- colnames(" + symbol + ")[" + c1 + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
214 |
rw.eval("colnames(" + symbol + ")[" + c1 + "] <- colnames(" + symbol + ")[" + c2 + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
|
215 |
rw.eval("colnames(" + symbol + ")[" + c2 + "] <- tmp"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
216 |
}
|
|
217 |
catch (RWorkspaceException e) {
|
216 |
218 |
// TODO Auto-generated catch block
|
217 |
219 |
org.txm.utils.logger.Log.printStackTrace(e);
|
218 |
220 |
}
|
219 |
221 |
}
|
220 |
|
|
|
222 |
|
221 |
223 |
/**
|
222 |
224 |
* Instantiates a new matrix impl.
|
223 |
225 |
*
|
... | ... | |
227 |
229 |
* @throws RWorkspaceException the r workspace exception
|
228 |
230 |
*/
|
229 |
231 |
public MatrixImpl(int[][] matrix, String[] lineNames, String[] partNames) throws RWorkspaceException {
|
230 |
|
this(matrix, partNames!=null?partNames.length:0, lineNames!=null?lineNames.length:0);
|
231 |
|
|
|
232 |
this(matrix, partNames != null ? partNames.length : 0, lineNames != null ? lineNames.length : 0);
|
|
233 |
|
232 |
234 |
if (lineNames == null) {
|
233 |
235 |
throw new IllegalArgumentException(StatsEngineCoreMessages.rowNamesOfAContingencyTableCannotBeNull);
|
234 |
236 |
}
|
... | ... | |
237 |
239 |
throw new IllegalArgumentException(StatsEngineCoreMessages.columnNamesOfAContingencyTableCannotBeNull);
|
238 |
240 |
}
|
239 |
241 |
|
240 |
|
// if (lineNames.length != nrow) {
|
241 |
|
// throw new IllegalArgumentException(Messages.MatrixImpl_5);
|
242 |
|
// }
|
243 |
|
//this.rows = ;
|
|
242 |
// if (lineNames.length != nrow) {
|
|
243 |
// throw new IllegalArgumentException(Messages.MatrixImpl_5);
|
|
244 |
// }
|
|
245 |
// this.rows = ;
|
244 |
246 |
rw.assignRowNamesToMatrix(symbol, new VectorImpl(lineNames).getSymbol());
|
245 |
|
|
246 |
247 |
|
247 |
|
// if (partNames.length != ncol) {
|
248 |
|
// throw new IllegalArgumentException(Messages.MatrixImpl_7
|
249 |
|
// + partNames.length + ", " + ncol + ")."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
250 |
|
// }
|
251 |
|
//this.cols = new VectorImpl(partNames);
|
|
248 |
|
|
249 |
// if (partNames.length != ncol) {
|
|
250 |
// throw new IllegalArgumentException(Messages.MatrixImpl_7
|
|
251 |
// + partNames.length + ", " + ncol + ")."); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
|
|
252 |
// }
|
|
253 |
// this.cols = new VectorImpl(partNames);
|
252 |
254 |
rw.assignColNamesToMatrix(symbol, new VectorImpl(partNames).getSymbol());
|
253 |
|
|
|
255 |
|
254 |
256 |
}
|
255 |
|
|
|
257 |
|
256 |
258 |
/**
|
257 |
259 |
* Create a matrix wrapping data already existing into R with the given
|
258 |
260 |
* symbol.
|
... | ... | |
266 |
268 |
protected MatrixImpl(String symbol) throws RWorkspaceException {
|
267 |
269 |
super(symbol);
|
268 |
270 |
}
|
269 |
|
|
|
271 |
|
270 |
272 |
/**
|
271 |
273 |
* Removes the row with a negative index.
|
|
274 |
*
|
272 |
275 |
* @param row the row [1..N]
|
273 |
276 |
*/
|
274 |
277 |
public void _removeRow(int row) {
|
... | ... | |
276 |
279 |
try {
|
277 |
280 |
REXP r = rw.eval(symbol
|
278 |
281 |
+ " <- " + symbol + "[ " + row + ",];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
279 |
|
//nrow--;
|
280 |
|
} catch (RWorkspaceException e) {
|
|
282 |
// nrow--;
|
|
283 |
}
|
|
284 |
catch (RWorkspaceException e) {
|
281 |
285 |
// TODO Auto-generated catch block
|
282 |
286 |
org.txm.utils.logger.Log.printStackTrace(e);
|
283 |
287 |
}
|
284 |
288 |
// de la liste rows
|
285 |
289 |
}
|
286 |
|
|
287 |
|
|
288 |
|
|
|
290 |
|
|
291 |
|
|
292 |
|
289 |
293 |
/**
|
290 |
294 |
* Export data.
|
291 |
295 |
*
|
... | ... | |
297 |
301 |
public void exportData(File f, String colseparator, String txtseparator) {
|
298 |
302 |
try {
|
299 |
303 |
org.txm.statsengine.r.core.RWorkspace
|
300 |
|
.getRWorkspaceInstance().safeEval(
|
301 |
|
"write.table(" + symbol + ", file=\"" + f.getAbsolutePath().replace("\\", "\\\\") + "\", fileEncoding=\"UTF-8\");"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
302 |
|
} catch (Exception e1) {
|
|
304 |
.getRWorkspaceInstance().safeEval(
|
|
305 |
"write.table(" + symbol + ", file=\"" + f.getAbsolutePath().replace("\\", "\\\\") + "\", fileEncoding=\"UTF-8\");"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
|
306 |
}
|
|
307 |
catch (Exception e1) {
|
303 |
308 |
org.txm.utils.logger.Log.printStackTrace(e1);
|
304 |
309 |
RWorkspace.printLastSafeEvalExpr();
|
305 |
310 |
}
|
306 |
311 |
}
|
307 |
|
|
|
312 |
|
308 |
313 |
/**
|
309 |
314 |
* Filter.
|
310 |
315 |
*
|
311 |
316 |
* @param freqs the freqs
|
312 |
317 |
* @param nlines the nlines
|
313 |
318 |
* @param fmin the fmin
|
314 |
|
* @throws REXPMismatchException
|
315 |
|
* @throws RWorkspaceException
|
316 |
|
* @throws StatException
|
|
319 |
* @throws REXPMismatchException
|
|
320 |
* @throws RWorkspaceException
|
|
321 |
* @throws StatException
|
317 |
322 |
*/
|
318 |
|
public void filter(int nlines, int fmin) throws RWorkspaceException, REXPMismatchException {
|
|
323 |
public void filter(int nlines, int fmin, int fmax) throws RWorkspaceException, REXPMismatchException {
|
319 |
324 |
RWorkspace rw = RWorkspace.getRWorkspaceInstance();
|
320 |
|
|
|
325 |
|
321 |
326 |
// first sort rows
|
322 |
|
rw.eval("freqs <- rowSums("+symbol+")"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
327 |
rw.eval("freqs <- rowSums(" + symbol + ")"); //$NON-NLS-1$ //$NON-NLS-2$
|
323 |
328 |
int[] freqs = rw.eval("freqs").asIntegers(); //$NON-NLS-1$
|
324 |
329 |
rw.eval("order <- sort(freqs, decreasing= TRUE, index.return= TRUE)"); //$NON-NLS-1$
|
325 |
|
//rw.eval("print(order)");
|
|
330 |
// rw.eval("print(order)");
|
|
331 |
|
|
332 |
// get table sort indexes
|
326 |
333 |
int[] order = rw.eval("order$ix").asIntegers(); //$NON-NLS-1$
|
|
334 |
|
|
335 |
// sort the table
|
327 |
336 |
rw.eval(symbol + "<- " + symbol + "[order$ix, ];"); //$NON-NLS-1$ //$NON-NLS-2$
|
328 |
|
|
|
337 |
|
329 |
338 |
// find first fmin line
|
330 |
339 |
int ifmin = 999999999;
|
331 |
|
for (int i = 0 ; i < order.length ; i++) {
|
332 |
|
//System.out.print("\t"+i+" freqs["+order[i]+"]"+freqs[order[i]-1]);
|
333 |
|
if (freqs[order[i]-1] < fmin) { // minus 1 because of R indexes
|
|
340 |
int ifmax = 0;
|
|
341 |
for (int i = 0; i < order.length; i++) {
|
|
342 |
// System.out.print("\t"+i+" freqs["+order[i]+"]"+freqs[order[i]-1]);
|
|
343 |
if (freqs[order[i] - 1] < fmin) { // minus 1 because of R indexes
|
334 |
344 |
ifmin = i; // the ith line
|
335 |
345 |
break;
|
336 |
346 |
}
|
337 |
347 |
}
|
338 |
|
|
339 |
|
//cut
|
340 |
|
cut(Math.min(nlines, ifmin));
|
|
348 |
// ifmin = Math.min(order.length - 1, ifmin);
|
|
349 |
|
|
350 |
for (int i = order.length - 1; i >= 0; i--) {
|
|
351 |
// System.out.print("\t"+i+" freqs["+order[i]+"]"+freqs[order[i]-1]);
|
|
352 |
// System.out.println("i=" + i + " o=" + order[i] + " f=" + freqs[order[i] - 1]);
|
|
353 |
if (freqs[order[i] - 1] > fmax) { // minus 1 because of R indexes
|
|
354 |
ifmax = i + 1 + 1; // the previous ith line + fix index for R
|
|
355 |
break;
|
|
356 |
}
|
|
357 |
}
|
|
358 |
|
|
359 |
// cut
|
|
360 |
cut(Math.max(1, ifmax), Math.min(nlines, ifmin));
|
341 |
361 |
}
|
342 |
|
|
|
362 |
|
343 |
363 |
/**
|
344 |
364 |
* Cut.
|
345 |
365 |
*
|
346 |
366 |
* @param nRowToCut the nlines
|
347 |
|
* @throws StatException
|
|
367 |
* @throws StatException
|
348 |
368 |
*/
|
349 |
|
public void cut(int nRowToCut){
|
350 |
|
if (nRowToCut > this.getNRows())
|
|
369 |
public void cut(int before, int after) {
|
|
370 |
if (after > this.getNRows())
|
351 |
371 |
return;
|
352 |
|
|
|
372 |
|
|
373 |
if (before < 1) before = 1;
|
|
374 |
|
353 |
375 |
try {
|
354 |
376 |
// cut nlines
|
355 |
|
REXP r = rw.eval(symbol + "<- " + symbol + "[1:" + (nRowToCut) + ", ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
356 |
|
getRowNames().cut(nRowToCut);
|
357 |
|
//this.nrow = nlines;
|
358 |
|
} catch (RWorkspaceException e) {
|
359 |
|
// TODO Auto-generated catch block
|
|
377 |
REXP r = rw.eval(symbol + "<- " + symbol + "[" + before + ":" + (after) + ", ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
378 |
getRowNames().cut(before, after);
|
|
379 |
// this.nrow = nlines;
|
|
380 |
}
|
|
381 |
catch (RWorkspaceException e) {
|
360 |
382 |
org.txm.utils.logger.Log.printStackTrace(e);
|
361 |
383 |
}
|
362 |
384 |
}
|
363 |
385 |
|
364 |
386 |
public void sortByFreqs(boolean reverse) throws RWorkspaceException, REXPMismatchException {
|
365 |
|
rw.eval("freqs <- rowSums("+symbol+")"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
387 |
rw.eval("freqs <- rowSums(" + symbol + ")"); //$NON-NLS-1$ //$NON-NLS-2$
|
366 |
388 |
String DECREASING = "FALSE"; //$NON-NLS-1$
|
367 |
389 |
if (reverse) DECREASING = "TRUE"; //$NON-NLS-1$
|
368 |
|
rw.eval("order <- sort(freqs, decreasing= "+DECREASING+", index.return= TRUE)"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
390 |
rw.eval("order <- sort(freqs, decreasing= " + DECREASING + ", index.return= TRUE)"); //$NON-NLS-1$ //$NON-NLS-2$
|
369 |
391 |
rw.eval(symbol + "<- " + symbol + "[order$ix, ];"); //$NON-NLS-1$ //$NON-NLS-2$
|
370 |
392 |
}
|
371 |
|
|
|
393 |
|
372 |
394 |
/**
|
373 |
395 |
* Fusion rows.
|
374 |
396 |
*
|
375 |
397 |
* @param row the row
|
376 |
|
* @throws RWorkspaceException
|
|
398 |
* @throws RWorkspaceException
|
377 |
399 |
*/
|
378 |
400 |
public boolean mergeRows(int[] rows) throws RWorkspaceException {
|
379 |
401 |
if (rows.length < 2) return false;
|
380 |
402 |
Arrays.sort(rows);
|
381 |
|
//System.out.println("fusion rows: "+Arrays.toString(rows)); //$NON-NLS-1$
|
|
403 |
// System.out.println("fusion rows: "+Arrays.toString(rows)); //$NON-NLS-1$
|
382 |
404 |
int rowToChange = rows[0] + 1;
|
383 |
405 |
rw.addVectorToWorkspace("rowsToMerge", ArrayIndex.zeroToOneBasedIndex(rows, rows.length)); //$NON-NLS-1$
|
384 |
|
RWorkspace.getRWorkspaceInstance().eval("newrow <- colSums("+symbol+"[rowsToMerge,])"); //$NON-NLS-1$ //$NON-NLS-2$
|
385 |
|
RWorkspace.getRWorkspaceInstance().eval(symbol+"["+rowToChange+",] <- newrow"); //$NON-NLS-1$ //$NON-NLS-2$
|
386 |
|
|
|
406 |
RWorkspace.getRWorkspaceInstance().eval("newrow <- colSums(" + symbol + "[rowsToMerge,])"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
407 |
RWorkspace.getRWorkspaceInstance().eval(symbol + "[" + rowToChange + ",] <- newrow"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
408 |
|
387 |
409 |
this.removeRows(Arrays.copyOfRange(rows, 1, rows.length)); // remove rows minus the first row to remove
|
388 |
|
|
|
410 |
|
389 |
411 |
return true;
|
390 |
412 |
}
|
391 |
|
|
|
413 |
|
392 |
414 |
/**
|
393 |
415 |
* Fusion rows.
|
394 |
416 |
*
|
395 |
417 |
* @param row the row
|
396 |
|
* @throws RWorkspaceException
|
|
418 |
* @throws RWorkspaceException
|
397 |
419 |
*/
|
398 |
420 |
public boolean mergeCols(int[] cols) throws RWorkspaceException {
|
399 |
421 |
if (cols.length < 2) return false;
|
400 |
422 |
Arrays.sort(cols);
|
401 |
|
//System.out.println("fusion rows: "+Arrays.toString(rows)); //$NON-NLS-1$
|
|
423 |
// System.out.println("fusion rows: "+Arrays.toString(rows)); //$NON-NLS-1$
|
402 |
424 |
int colToChange = cols[0] + 1;
|
403 |
425 |
rw.addVectorToWorkspace("colsToMerge", ArrayIndex.zeroToOneBasedIndex(cols, cols.length)); //$NON-NLS-1$
|
404 |
|
RWorkspace.getRWorkspaceInstance().eval("newcol <- rowSums("+symbol+"[,colsToMerge])"); //$NON-NLS-1$ //$NON-NLS-2$
|
405 |
|
RWorkspace.getRWorkspaceInstance().eval(symbol+"[,"+colToChange+"] <- newcol"); //$NON-NLS-1$ //$NON-NLS-2$
|
406 |
|
|
|
426 |
RWorkspace.getRWorkspaceInstance().eval("newcol <- rowSums(" + symbol + "[,colsToMerge])"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
427 |
RWorkspace.getRWorkspaceInstance().eval(symbol + "[," + colToChange + "] <- newcol"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
428 |
|
407 |
429 |
this.removeCols(Arrays.copyOfRange(cols, 1, cols.length)); // remove rows minus the first row to remove
|
408 |
|
|
|
430 |
|
409 |
431 |
return true;
|
410 |
432 |
}
|
411 |
|
|
412 |
|
/* (non-Javadoc)
|
|
433 |
|
|
434 |
/*
|
|
435 |
* (non-Javadoc)
|
413 |
436 |
* @see org.txm.stat.data.Matrix#get(int, int)
|
414 |
437 |
*/
|
415 |
438 |
@Override
|
416 |
439 |
public double get(int row, int col) throws RException, RWorkspaceException {
|
417 |
440 |
int ncol = this.getNColumns();
|
418 |
|
int nrow = this.getNRows();
|
419 |
|
|
|
441 |
int nrow = this.getNRows();
|
|
442 |
|
420 |
443 |
if (row < 0 || row >= nrow) {
|
421 |
|
//throw new IllegalArgumentException(Messages.MatrixImpl_10 + row + Messages.MatrixImpl_11 + nrow + ")"); //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-1$
|
|
444 |
// throw new IllegalArgumentException(Messages.MatrixImpl_10 + row + Messages.MatrixImpl_11 + nrow + ")"); //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-1$
|
422 |
445 |
}
|
423 |
446 |
if (col < 0 || col >= ncol) {
|
424 |
|
//throw new IllegalArgumentException(Messages.MatrixImpl_13 + col + Messages.MatrixImpl_14 + ncol + ")"); //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-1$
|
|
447 |
// throw new IllegalArgumentException(Messages.MatrixImpl_13 + col + Messages.MatrixImpl_14 + ncol + ")"); //$NON-NLS-3$ //$NON-NLS-1$ //$NON-NLS-1$
|
425 |
448 |
}
|
426 |
449 |
REXP r = rw.eval(symbol
|
427 |
450 |
+ "[" + ArrayIndex.zeroToOneBasedIndex(row) + ", " + ArrayIndex.zeroToOneBasedIndex(col) + " ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
428 |
451 |
if (r.isInteger()) {
|
429 |
452 |
try {
|
430 |
453 |
return r.asInteger();
|
431 |
|
} catch (REXPMismatchException e) {
|
|
454 |
}
|
|
455 |
catch (REXPMismatchException e) {
|
432 |
456 |
throw new RWorkspaceException(e);
|
433 |
457 |
}
|
434 |
|
} else if (r.isComplex()) {
|
|
458 |
}
|
|
459 |
else if (r.isComplex()) {
|
435 |
460 |
try {
|
436 |
461 |
return r.asDouble();
|
437 |
|
} catch (REXPMismatchException e) {
|
|
462 |
}
|
|
463 |
catch (REXPMismatchException e) {
|
438 |
464 |
throw new RWorkspaceException(e);
|
439 |
465 |
}
|
440 |
|
} else if (r.isNumeric()) {
|
|
466 |
}
|
|
467 |
else if (r.isNumeric()) {
|
441 |
468 |
try {
|
442 |
469 |
return r.asDouble();
|
443 |
|
} catch (REXPMismatchException e) {
|
|
470 |
}
|
|
471 |
catch (REXPMismatchException e) {
|
444 |
472 |
throw new RWorkspaceException(e);
|
445 |
473 |
}
|
446 |
|
} else if (r.isNull()) {
|
|
474 |
}
|
|
475 |
else if (r.isNull()) {
|
447 |
476 |
throw new IllegalArgumentException(StatsEngineCoreMessages.rObjectEvaluatedToNull);
|
448 |
|
} else {
|
|
477 |
}
|
|
478 |
else {
|
449 |
479 |
// System.out.println("row: " + row);
|
450 |
480 |
// System.out.println("col: " + col);
|
451 |
481 |
// System.out.println("nrow: " + nrow);
|
... | ... | |
454 |
484 |
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.unknownTypeColon, r.getAttribute(StatsEngineCoreMessages.mode)));
|
455 |
485 |
}
|
456 |
486 |
}
|
457 |
|
|
458 |
|
/* (non-Javadoc)
|
|
487 |
|
|
488 |
/*
|
|
489 |
* (non-Javadoc)
|
459 |
490 |
* @see org.txm.stat.data.Matrix#getCol(int)
|
460 |
491 |
*/
|
461 |
492 |
@Override
|
462 |
493 |
public Vector getCol(int index) throws RException, RWorkspaceException {
|
463 |
494 |
int ncol = this.getNColumns();
|
464 |
|
|
|
495 |
|
465 |
496 |
if (index < 0 || index >= ncol) {
|
466 |
|
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.columnIndex, index, ncol));
|
|
497 |
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.columnIndex, index, ncol));
|
467 |
498 |
}
|
468 |
499 |
String colName = createSymbole(Vector.class);
|
469 |
500 |
rw.voidEval(colName + "<- " + symbol + "[ ," + (index + 1) + "];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
470 |
501 |
return new VectorImpl(colName);
|
471 |
502 |
}
|
472 |
|
|
473 |
|
/* (non-Javadoc)
|
|
503 |
|
|
504 |
/*
|
|
505 |
* (non-Javadoc)
|
474 |
506 |
* @see org.txm.stat.data.Matrix#getCol(java.lang.String)
|
475 |
507 |
*/
|
476 |
508 |
@Override
|
... | ... | |
479 |
511 |
rw.voidEval(colName + "<- " + symbol + "[ ,\"" + column + "\"];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
480 |
512 |
return new VectorImpl(colName);
|
481 |
513 |
}
|
482 |
|
|
483 |
|
/* (non-Javadoc)
|
|
514 |
|
|
515 |
/*
|
|
516 |
* (non-Javadoc)
|
484 |
517 |
* @see org.txm.stat.data.Matrix#getColNames()
|
485 |
518 |
*/
|
486 |
519 |
@Override
|
487 |
520 |
public Vector getColNames() {
|
488 |
521 |
try {
|
489 |
|
return new VectorImpl("colnames("+this.getSymbol()+")"); //$NON-NLS-1$ //$NON-NLS-2$
|
490 |
|
} catch (RWorkspaceException e) {
|
|
522 |
return new VectorImpl("colnames(" + this.getSymbol() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
523 |
}
|
|
524 |
catch (RWorkspaceException e) {
|
491 |
525 |
org.txm.utils.logger.Log.printStackTrace(e);
|
492 |
526 |
return null;
|
493 |
527 |
}
|
494 |
528 |
}
|
495 |
|
|
|
529 |
|
496 |
530 |
public int[][] getIntData() {
|
497 |
531 |
try {
|
498 |
532 |
REXP rexp = RWorkspace.getRWorkspaceInstance().eval(this.symbol);
|
499 |
533 |
return asIntMatrix(rexp);
|
500 |
|
} catch (RWorkspaceException e) {
|
|
534 |
}
|
|
535 |
catch (RWorkspaceException e) {
|
501 |
536 |
org.txm.utils.logger.Log.printStackTrace(e);
|
502 |
|
} catch (REXPMismatchException e) {
|
|
537 |
}
|
|
538 |
catch (REXPMismatchException e) {
|
503 |
539 |
org.txm.utils.logger.Log.printStackTrace(e);
|
504 |
540 |
}
|
505 |
541 |
return null;
|
506 |
542 |
}
|
507 |
|
|
508 |
|
/* (non-Javadoc)
|
|
543 |
|
|
544 |
/*
|
|
545 |
* (non-Javadoc)
|
509 |
546 |
* @see org.txm.stat.data.Matrix#getNColumns()
|
510 |
547 |
*/
|
511 |
548 |
@Override
|
512 |
549 |
public int getNColumns() {
|
513 |
550 |
try {
|
514 |
551 |
return RWorkspace.getRWorkspaceInstance().eval("dim(" + symbol + ")[2]").asInteger(); //$NON-NLS-1$ //$NON-NLS-2$
|
515 |
|
} catch (Exception e) {
|
|
552 |
}
|
|
553 |
catch (Exception e) {
|
516 |
554 |
Log.severe(StatsEngineCoreMessages.bind("Matrix: failed to get nrow: {0}.", e));
|
517 |
555 |
}
|
518 |
556 |
return 0;
|
519 |
557 |
}
|
520 |
|
|
521 |
|
/* (non-Javadoc)
|
|
558 |
|
|
559 |
/*
|
|
560 |
* (non-Javadoc)
|
522 |
561 |
* @see org.txm.stat.data.Matrix#getNRows()
|
523 |
562 |
*/
|
524 |
563 |
@Override
|
525 |
564 |
public int getNRows() {
|
526 |
565 |
try {
|
527 |
566 |
return RWorkspace.getRWorkspaceInstance().eval("dim(" + symbol + ")[1]").asInteger(); //$NON-NLS-1$ //$NON-NLS-2$
|
528 |
|
} catch (Exception e) {
|
|
567 |
}
|
|
568 |
catch (Exception e) {
|
529 |
569 |
Log.severe(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.matrixColonFailedToGetNrowColon, e));
|
530 |
570 |
}
|
531 |
571 |
return 0;
|
532 |
572 |
}
|
533 |
|
|
534 |
|
/* (non-Javadoc)
|
|
573 |
|
|
574 |
/*
|
|
575 |
* (non-Javadoc)
|
535 |
576 |
* @see org.txm.stat.data.Matrix#getRow(int)
|
536 |
577 |
*/
|
537 |
578 |
@Override
|
538 |
579 |
public Vector getRow(int index) throws RException, RWorkspaceException {
|
539 |
580 |
int nrow = getNRows();
|
540 |
581 |
if (index < 0 || index >= nrow) {
|
541 |
|
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.rowIndex, index, nrow));
|
|
582 |
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.rowIndex, index, nrow));
|
542 |
583 |
}
|
543 |
584 |
String rowName = createSymbole(Vector.class);
|
544 |
585 |
rw.voidEval(rowName + "<- " + symbol + "[" + (index + 1) + ", ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
545 |
586 |
return new VectorImpl(rowName);
|
546 |
587 |
}
|
547 |
|
|
548 |
|
/* (non-Javadoc)
|
|
588 |
|
|
589 |
/*
|
|
590 |
* (non-Javadoc)
|
549 |
591 |
* @see org.txm.stat.data.Matrix#getRow(java.lang.String)
|
550 |
592 |
*/
|
551 |
593 |
@Override
|
... | ... | |
554 |
596 |
rw.voidEval(rowName + "<- " + symbol + "[ \"" + row + "\" , ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
555 |
597 |
return new VectorImpl(rowName);
|
556 |
598 |
}
|
557 |
|
|
558 |
|
/* (non-Javadoc)
|
|
599 |
|
|
600 |
/*
|
|
601 |
* (non-Javadoc)
|
559 |
602 |
* @see org.txm.stat.data.Matrix#getRowNames()
|
560 |
603 |
*/
|
561 |
604 |
@Override
|
562 |
605 |
public Vector getRowNames() {
|
563 |
606 |
try {
|
564 |
|
return new VectorImpl("rownames("+this.getSymbol()+")"); //$NON-NLS-1$ //$NON-NLS-2$
|
565 |
|
} catch (RWorkspaceException e) {
|
|
607 |
return new VectorImpl("rownames(" + this.getSymbol() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
608 |
}
|
|
609 |
catch (RWorkspaceException e) {
|
566 |
610 |
org.txm.utils.logger.Log.printStackTrace(e);
|
567 |
611 |
return null;
|
568 |
612 |
}
|
569 |
613 |
}
|
570 |
|
|
571 |
|
|
572 |
|
/* (non-Javadoc)
|
|
614 |
|
|
615 |
|
|
616 |
/*
|
|
617 |
* (non-Javadoc)
|
573 |
618 |
* @see org.txm.stat.data.ContingencyTable#getRowMargin()
|
574 |
619 |
*/
|
575 |
620 |
public int[] getRowMargins() throws StatException {
|
576 |
621 |
try {
|
577 |
622 |
return rw.eval("rowSums(" + this.symbol + ");").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$
|
578 |
|
} catch (REXPMismatchException e) {
|
|
623 |
}
|
|
624 |
catch (REXPMismatchException e) {
|
579 |
625 |
throw new StatException(e);
|
580 |
|
}
|
|
626 |
}
|
581 |
627 |
}
|
582 |
|
|
583 |
|
/* (non-Javadoc)
|
|
628 |
|
|
629 |
/*
|
|
630 |
* (non-Javadoc)
|
584 |
631 |
* @see org.txm.stat.data.ContingencyTable#getRowMargin()
|
585 |
632 |
*/
|
586 |
633 |
public int[] getColMargins() throws StatException {
|
587 |
634 |
try {
|
588 |
635 |
return rw.eval("colSums(" + this.symbol + ");").asIntegers(); //$NON-NLS-1$ //$NON-NLS-2$
|
589 |
|
} catch (Exception e) {
|
|
636 |
}
|
|
637 |
catch (Exception e) {
|
590 |
638 |
throw new StatException(e);
|
591 |
639 |
}
|
592 |
640 |
}
|
593 |
|
|
|
641 |
|
594 |
642 |
/**
|
595 |
643 |
* Import data.
|
596 |
644 |
*
|
597 |
645 |
* @param f the f
|
598 |
|
* @throws RWorkspaceException
|
599 |
|
* @throws REXPMismatchException
|
|
646 |
* @throws RWorkspaceException
|
|
647 |
* @throws REXPMismatchException
|
600 |
648 |
*/
|
601 |
649 |
public boolean importData(File f, String encoding, String colseparator, String txtseparator) throws RWorkspaceException, REXPMismatchException {
|
602 |
|
|
|
650 |
|
603 |
651 |
String path = f.getAbsolutePath();
|
604 |
|
|
|
652 |
|
605 |
653 |
if (OSDetector.isFamilyWindows()) {
|
606 |
654 |
path = path.replace("\\", "\\\\"); //$NON-NLS-1$ //$NON-NLS-2$
|
607 |
655 |
}
|
608 |
656 |
|
609 |
|
/*
|
610 |
|
t2 <- as.matrix(read.table(file = "/home/mdecorde/TEMP/export (copie).csv", row.names=1, skip=1));
|
611 |
|
header <-scan(file = "/home/mdecorde/TEMP/export (copie).csv", nlines=1, sep="\t", fileEncoding="UTF-8", what=character())
|
612 |
|
header <- header[2:length(header)];
|
613 |
|
t3 <- t2[1:nrow(t2),];
|
614 |
|
colnames(t3) <- header;
|
615 |
|
*/
|
616 |
|
rw.voidEval("tmpmat <- as.matrix(read.table(file=\"" + path + "\", sep=\""+colseparator+"\", row.names=1, skip=1, fileEncoding=\""+encoding+"\"));"); //$NON-NLS-1$ //$NON-NLS-2$
|
617 |
|
rw.voidEval("tmpheader <- scan(file=\"" + path + "\", what = character(), nlines=1, sep=\""+colseparator+"\", fileEncoding=\""+encoding+"\");"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
657 |
/*
|
|
658 |
* t2 <- as.matrix(read.table(file = "/home/mdecorde/TEMP/export (copie).csv", row.names=1, skip=1));
|
|
659 |
* header <-scan(file = "/home/mdecorde/TEMP/export (copie).csv", nlines=1, sep="\t", fileEncoding="UTF-8", what=character())
|
|
660 |
* header <- header[2:length(header)];
|
|
661 |
* t3 <- t2[1:nrow(t2),];
|
|
662 |
* colnames(t3) <- header;
|
|
663 |
*/
|
|
664 |
rw.voidEval("tmpmat <- as.matrix(read.table(file=\"" + path + "\", sep=\"" + colseparator + "\", row.names=1, skip=1, fileEncoding=\"" + encoding + "\"));"); //$NON-NLS-1$ //$NON-NLS-2$
|
|
665 |
rw.voidEval("tmpheader <- scan(file=\"" + path + "\", what = character(), nlines=1, sep=\"" + colseparator + "\", fileEncoding=\"" + encoding + "\");"); //$NON-NLS-1$ //$NON-NLS-2$
|
618 |
666 |
rw.voidEval("tmpheader <- tmpheader[2:length(tmpheader)];"); //$NON-NLS-1$
|
619 |
667 |
rw.voidEval("tmpmat <- tmpmat[1:nrow(tmpmat),];"); //$NON-NLS-1$
|
620 |
668 |
rw.voidEval("colnames(tmpmat) <- tmpheader"); //$NON-NLS-1$
|
621 |
|
|
622 |
|
rw.eval(symbol +"<- tmpmat"); //$NON-NLS-1$
|
623 |
|
|
|
669 |
|
|
670 |
rw.eval(symbol + "<- tmpmat"); //$NON-NLS-1$
|
|
671 |
|
624 |
672 |
return true;
|
625 |
673 |
}
|
626 |
|
|
|
674 |
|
627 |
675 |
public void print() {
|
628 |
|
|
|
676 |
|
629 |
677 |
try {
|
630 |
678 |
String[] rowNames = this.getRowNames().asStringsArray();
|
631 |
679 |
String[] colNames = this.getColNames().asStringsArray();
|
632 |
680 |
int[][] data = getIntData();
|
633 |
|
|
|
681 |
|
634 |
682 |
// col names
|
635 |
683 |
for (String col : colNames)
|
636 |
|
System.out.print("\t"+col); //$NON-NLS-1$
|
637 |
|
System.out.println();
|
638 |
|
for (int i = 0 ; i < rowNames.length ; i++) {
|
|
684 |
System.out.print("\t" + col); //$NON-NLS-1$
|
|
685 |
System.out.println();
|
|
686 |
for (int i = 0; i < rowNames.length; i++) {
|
639 |
687 |
System.out.print(rowNames[i]);
|
640 |
|
for (int j = 0 ; j < colNames.length ; j++) {
|
641 |
|
System.out.print("\t"+data[i][j]); //$NON-NLS-1$
|
|
688 |
for (int j = 0; j < colNames.length; j++) {
|
|
689 |
System.out.print("\t" + data[i][j]); //$NON-NLS-1$
|
642 |
690 |
}
|
643 |
691 |
System.out.println();
|
644 |
692 |
}
|
645 |
|
} catch (StatException e) {
|
|
693 |
}
|
|
694 |
catch (StatException e) {
|
646 |
695 |
// TODO Auto-generated catch block
|
647 |
696 |
org.txm.utils.logger.Log.printStackTrace(e);
|
648 |
697 |
}
|
649 |
698 |
}
|
650 |
|
|
|
699 |
|
651 |
700 |
/**
|
652 |
701 |
* Removes the col.
|
653 |
702 |
*
|
... | ... | |
655 |
704 |
* @param checkEmptyLines the check empty lines
|
656 |
705 |
*/
|
657 |
706 |
public void removeCol(int col) {
|
658 |
|
//System.out.println("rmv col " + col);
|
|
707 |
// System.out.println("rmv col " + col);
|
659 |
708 |
try {
|
660 |
709 |
REXP r = rw.eval(symbol + " <- " + symbol + "[,-" + ArrayIndex.zeroToOneBasedIndex(col) + "];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
661 |
|
} catch (RWorkspaceException e) {
|
|
710 |
}
|
|
711 |
catch (RWorkspaceException e) {
|
662 |
712 |
// TODO Auto-generated catch block
|
663 |
713 |
org.txm.utils.logger.Log.printStackTrace(e);
|
664 |
714 |
}
|
665 |
715 |
}
|
666 |
|
|
|
716 |
|
667 |
717 |
/**
|
668 |
718 |
* Removes the col and delete empty rows.
|
669 |
719 |
*
|
... | ... | |
671 |
721 |
* @param checkEmptyLines the check empty lines
|
672 |
722 |
*/
|
673 |
723 |
public void removeCol(int col, boolean checkEmptyLines) {
|
674 |
|
//System.out.println("rmv col " + col);
|
|
724 |
// System.out.println("rmv col " + col);
|
675 |
725 |
try {
|
676 |
726 |
removeCol(col);
|
677 |
|
|
678 |
|
//we remove the empty lineslines
|
|
727 |
|
|
728 |
// we remove the empty lineslines
|
679 |
729 |
if (checkEmptyLines) {
|
680 |
730 |
removeEmptyRows();
|
681 |
731 |
}
|
682 |
|
} catch (RWorkspaceException e) {
|
|
732 |
}
|
|
733 |
catch (RWorkspaceException e) {
|
683 |
734 |
// TODO Auto-generated catch block
|
684 |
735 |
org.txm.utils.logger.Log.printStackTrace(e);
|
685 |
736 |
}
|
686 |
737 |
}
|
687 |
|
|
|
738 |
|
688 |
739 |
/**
|
689 |
740 |
* Removes the cols.
|
690 |
741 |
*
|
... | ... | |
698 |
749 |
removeCol(index, false);
|
699 |
750 |
}
|
700 |
751 |
}
|
701 |
|
|
|
752 |
|
702 |
753 |
public void removeCols(int[] cols, boolean removeEmptyLines) throws RWorkspaceException {
|
703 |
754 |
removeCols(cols);
|
704 |
755 |
if (removeEmptyLines) removeEmptyRows();
|
705 |
756 |
}
|
706 |
|
|
|
757 |
|
707 |
758 |
/**
|
708 |
759 |
* Removes the cols.
|
709 |
760 |
*
|
... | ... | |
717 |
768 |
removeCol(index, false);
|
718 |
769 |
}
|
719 |
770 |
}
|
720 |
|
|
|
771 |
|
721 |
772 |
public void removeCols(List<Integer> cols, boolean removeEmptyLines) throws RWorkspaceException {
|
722 |
773 |
removeCols(cols);
|
723 |
774 |
if (removeEmptyLines) {
|
724 |
775 |
removeEmptyRows();
|
725 |
776 |
}
|
726 |
777 |
}
|
727 |
|
|
|
778 |
|
728 |
779 |
private void removeEmptyCols() throws RWorkspaceException {
|
729 |
780 |
try {
|
730 |
|
int[] colsums = rw.eval("apply("+symbol+",2,sum)").asIntegers();//get a vector of line sums //$NON-NLS-1$ //$NON-NLS-2$
|
731 |
|
ArrayList<Integer> removecols = new ArrayList<Integer>();
|
|
781 |
int[] colsums = rw.eval("apply(" + symbol + ",2,sum)").asIntegers();// get a vector of line sums //$NON-NLS-1$ //$NON-NLS-2$
|
|
782 |
ArrayList<Integer> removecols = new ArrayList<>();
|
732 |
783 |
// get the empty line index
|
733 |
|
for (int i = 0 ; i < colsums.length ; i++) {
|
|
784 |
for (int i = 0; i < colsums.length; i++) {
|
734 |
785 |
if (colsums[i] == 0)
|
735 |
786 |
removecols.add(i);
|
736 |
787 |
}
|
737 |
|
|
|
788 |
|
738 |
789 |
if (removecols.size() > 0) {
|
739 |
790 |
removeCols(removecols);
|
740 |
791 |
}
|
741 |
|
} catch (REXPMismatchException e) {
|
|
792 |
}
|
|
793 |
catch (REXPMismatchException e) {
|
742 |
794 |
// TODO Auto-generated catch block
|
743 |
795 |
org.txm.utils.logger.Log.printStackTrace(e);
|
744 |
796 |
}
|
745 |
797 |
}
|
746 |
|
|
|
798 |
|
747 |
799 |
/**
|
748 |
800 |
* Removes the empty lines.
|
749 |
801 |
*
|
750 |
802 |
* @throws RWorkspaceException the r workspace exception
|
751 |
803 |
*/
|
752 |
|
public void removeEmptyRows() throws RWorkspaceException
|
753 |
|
{
|
|
804 |
public void removeEmptyRows() throws RWorkspaceException {
|
754 |
805 |
try {
|
755 |
|
int[] rowsums = rw.eval("apply("+symbol+",1,sum)").asIntegers();//get a vector of line sums //$NON-NLS-1$ //$NON-NLS-2$
|
756 |
|
ArrayList<Integer> rowIndexes = new ArrayList<Integer>();
|
|
806 |
int[] rowsums = rw.eval("apply(" + symbol + ",1,sum)").asIntegers();// get a vector of line sums //$NON-NLS-1$ //$NON-NLS-2$
|
|
807 |
ArrayList<Integer> rowIndexes = new ArrayList<>();
|
757 |
808 |
// get the empty line index
|
758 |
|
for (int i = 0 ; i < rowsums.length ; i++) {
|
|
809 |
for (int i = 0; i < rowsums.length; i++) {
|
759 |
810 |
if (rowsums[i] == 0)
|
760 |
811 |
rowIndexes.add(i);
|
761 |
812 |
}
|
762 |
|
|
|
813 |
|
763 |
814 |
if (rowIndexes.size() > 0) {
|
764 |
815 |
removeRows(rowIndexes);
|
765 |
816 |
}
|
766 |
|
} catch (REXPMismatchException e) {
|
|
817 |
}
|
|
818 |
catch (REXPMismatchException e) {
|
767 |
819 |
// TODO Auto-generated catch block
|
768 |
820 |
org.txm.utils.logger.Log.printStackTrace(e);
|
769 |
821 |
}
|
770 |
822 |
}
|
771 |
|
|
|
823 |
|
772 |
824 |
/**
|
773 |
825 |
* Removes the row.
|
774 |
826 |
*
|
... | ... | |
779 |
831 |
try {
|
780 |
832 |
REXP r = rw.eval(symbol
|
781 |
833 |
+ " <- " + symbol + "[ -" + ArrayIndex.zeroToOneBasedIndex(row) + ",];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
782 |
|
} catch (RWorkspaceException e) {
|
|
834 |
}
|
|
835 |
catch (RWorkspaceException e) {
|
783 |
836 |
// TODO Auto-generated catch block
|
784 |
837 |
org.txm.utils.logger.Log.printStackTrace(e);
|
785 |
838 |
}
|
786 |
839 |
// de la liste rows
|
787 |
840 |
}
|
788 |
|
|
|
841 |
|
789 |
842 |
/**
|
790 |
843 |
* Removes the rows.
|
791 |
844 |
*
|
... | ... | |
800 |
853 |
}
|
801 |
854 |
}
|
802 |
855 |
}
|
803 |
|
|
|
856 |
|
804 |
857 |
/**
|
805 |
858 |
* Removes the rows indexes.
|
806 |
859 |
*
|
... | ... | |
811 |
864 |
int nrow = this.getNRows();
|
812 |
865 |
try {
|
813 |
866 |
Arrays.sort(rows);
|
814 |
|
for (int i = 0 ; i < rows.length ; i++) {
|
815 |
|
rows[i] = - (rows[i]+1);
|
|
867 |
for (int i = 0; i < rows.length; i++) {
|
|
868 |
rows[i] = -(rows[i] + 1);
|
816 |
869 |
}
|
817 |
870 |
int idx = rows[0];
|
818 |
871 |
if (rows.length > 1) {
|
819 |
|
int[] subrows = new int[rows.length-1];
|
|
872 |
int[] subrows = new int[rows.length - 1];
|
820 |
873 |
System.arraycopy(rows, 1, subrows, 0, rows.length - 1);
|
821 |
874 |
rw.addVectorToWorkspace("linesToDelete", subrows); //$NON-NLS-1$
|
822 |
|
//System.out.println("all lines to delete: "+Arrays.toString(rows));
|
823 |
|
//System.out.println("lines to delete: "+Arrays.toString(subrows));
|
|
875 |
// System.out.println("all lines to delete: "+Arrays.toString(rows));
|
|
876 |
// System.out.println("lines to delete: "+Arrays.toString(subrows));
|
824 |
877 |
REXP r = rw.eval(symbol + " <- " + symbol + "[ linesToDelete,];"); //$NON-NLS-1$ //$NON-NLS-2$
|
825 |
878 |
nrow -= rows.length - 1;
|
826 |
879 |
}
|
827 |
|
|
|
880 |
|
828 |
881 |
if (nrow == 2) {
|
829 |
882 |
// 2 lines left
|
830 |
883 |
int last = 1;
|
... | ... | |
832 |
885 |
String[] rowNames;
|
833 |
886 |
try {
|
834 |
887 |
rowNames = getRowNames().asStringsArray();
|
835 |
|
} catch (StatException e) {
|
|
888 |
}
|
|
889 |
catch (StatException e) {
|
836 |
890 |
System.out.println(StatsEngineCoreMessages.failedToGetRowNames);
|
837 |
891 |
org.txm.utils.logger.Log.printStackTrace(e);
|
838 |
892 |
return;
|
839 |
893 |
}
|
840 |
|
String lastRowName = rowNames[last-1];
|
841 |
|
REXP r = rw.eval(this.symbol+"<-matrix("+this.symbol+"["+idx+",], nrow=1, dimnames = list(rownames=\""+lastRowName+"\", colnames("+this.symbol+")))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
842 |
|
//_removeRow(idx);
|
|
894 |
String lastRowName = rowNames[last - 1];
|
|
895 |
REXP r = rw.eval(this.symbol + "<-matrix(" + this.symbol + "[" + idx + ",], nrow=1, dimnames = list(rownames=\"" + lastRowName + "\", colnames(" + this.symbol + ")))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
|
|
896 |
// _removeRow(idx);
|
843 |
897 |
nrow = 1;
|
844 |
|
} else {
|
845 |
|
//System.out.println("remove last row: "+idx);
|
|
898 |
}
|
|
899 |
else {
|
|
900 |
// System.out.println("remove last row: "+idx);
|
846 |
901 |
_removeRow(idx);
|
847 |
902 |
}
|
848 |
|
} catch (RWorkspaceException e) {
|
|
903 |
}
|
|
904 |
catch (RWorkspaceException e) {
|
849 |
905 |
// TODO Auto-generated catch block
|
850 |
906 |
org.txm.utils.logger.Log.printStackTrace(e);
|
851 |
907 |
}
|
852 |
908 |
}
|
853 |
|
|
|
909 |
|
854 |
910 |
public void removeRows(int[] rows, boolean removeEmptyCols) throws RWorkspaceException {
|
855 |
911 |
removeRows(rows);
|
856 |
912 |
if (removeEmptyCols) removeEmptyCols();
|
857 |
913 |
}
|
858 |
|
|
|
914 |
|
859 |
915 |
/**
|
860 |
916 |
* Removes the rows.
|
861 |
917 |
*
|
862 |
918 |
* @param rows the row
|
863 |
919 |
*/
|
864 |
920 |
public void removeRows(List<Integer> rows) {
|
865 |
|
|
|
921 |
|
866 |
922 |
Collections.sort(rows);
|
867 |
|
//System.out.println("del rows " + row);
|
|
923 |
// System.out.println("del rows " + row);
|
868 |
924 |
int index = 0;
|
869 |
925 |
int nrow = this.getNRows();
|
870 |
926 |
int diff = Math.abs(rows.size() - nrow);
|
... | ... | |
873 |
929 |
index = rows.get(i);
|
874 |
930 |
removeRow(index);
|
875 |
931 |
}
|
876 |
|
} else if (diff == 1) {
|
|
932 |
}
|
|
933 |
else if (diff == 1) {
|
877 |
934 |
for (int i = rows.size() - 2; i >= 0; i--) {
|
878 |
935 |
index = rows.get(i);
|
879 |
936 |
removeRow(index);
|
... | ... | |
886 |
943 |
String[] rowNames;
|
887 |
944 |
try {
|
888 |
945 |
rowNames = getRowNames().asStringsArray();
|
889 |
|
} catch (StatException e) {
|
|
946 |
}
|
|
947 |
catch (StatException e) {
|
890 |
948 |
System.out.println(StatsEngineCoreMessages.failedToGetRowNames);
|
891 |
949 |
org.txm.utils.logger.Log.printStackTrace(e);
|
892 |
950 |
return;
|
893 |
951 |
}
|
894 |
952 |
String lastRowName = rowNames[last];
|
895 |
|
System.out.println("table<-matrix(table[c(-"+idx+"),],nrow=1,dimnames = list(rownames="+lastRowName+",colnames(table)))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
896 |
|
} else {
|
|
953 |
System.out.println("table<-matrix(table[c(-" + idx + "),],nrow=1,dimnames = list(rownames=" + lastRowName + ",colnames(table)))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
954 |
}
|
|
955 |
else {
|
897 |
956 |
System.out.println(StatsEngineCoreMessages.cannotDeleteAllLines);
|
898 |
957 |
}
|
899 |
958 |
}
|
900 |
|
|
|
959 |
|
901 |
960 |
public void removeRows(List<Integer> rows, boolean removeEmptyCols) throws RWorkspaceException {
|
902 |
961 |
this.removeRows(rows);
|
903 |
962 |
if (removeEmptyCols) {
|
904 |
963 |
removeEmptyCols();
|
905 |
964 |
}
|
906 |
965 |
}
|
907 |
|
|
|
966 |
|
908 |
967 |
/**
|
909 |
968 |
* Sets the.
|
910 |
969 |
*
|
... | ... | |
916 |
975 |
int nrow = this.getNRows();
|
917 |
976 |
int ncol = this.getNColumns();
|
918 |
977 |
if (row < 0 || row >= nrow) {
|
919 |
|
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.rowIndex, row, nrow));
|
|
978 |
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.rowIndex, row, nrow));
|
920 |
979 |
}
|
921 |
980 |
if (col < 0 || col >= ncol) {
|
922 |
|
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.colIndex, col, ncol));
|
|
981 |
throw new IllegalArgumentException(StatsEngineCoreMessages.bind(StatsEngineCoreMessages.colIndex, col, ncol));
|
923 |
982 |
}
|
924 |
983 |
// appel à R
|
925 |
984 |
try {
|
926 |
985 |
REXP r = rw.eval(symbol
|
927 |
986 |
+ "[" + ArrayIndex.zeroToOneBasedIndex(row) + ", " + ArrayIndex.zeroToOneBasedIndex(col) + " ] <- " + value + ";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
928 |
|
} catch (RWorkspaceException e) {
|
|
987 |
}
|
|
988 |
catch (RWorkspaceException e) {
|
929 |
989 |
// TODO Auto-generated catch block
|
930 |
990 |
org.txm.utils.logger.Log.printStackTrace(e);
|
931 |
991 |
}
|
932 |
992 |
}
|
933 |
|
|
|
993 |
|
934 |
994 |
/**
|
935 |
995 |
* Sets the order.
|
936 |
996 |
*
|
... | ... | |
939 |
999 |
*/
|
940 |
1000 |
public void setOrder(List<Integer> neworder, Boolean reverse) {
|
941 |
1001 |
int nrow = this.getNRows();
|
942 |
|
if (neworder.size() != nrow)
|
943 |
|
{
|
944 |
|
//System.out.println("MatrixImpl.setOrder: error new order size "+neworder.size()+" != nrow "+nrow);
|
|
1002 |
if (neworder.size() != nrow) {
|
|
1003 |
// System.out.println("MatrixImpl.setOrder: error new order size "+neworder.size()+" != nrow "+nrow);
|
945 |
1004 |
return;
|
946 |
1005 |
}
|
947 |
1006 |
String order = "c("; //$NON-NLS-1$
|
948 |
1007 |
if (reverse) {
|
949 |
1008 |
for (int i = neworder.size() - 1; i >= 0; i--)
|
950 |
1009 |
order += "" + (neworder.get(i) + 1) + ", "; //$NON-NLS-1$ //$NON-NLS-2$
|
951 |
|
} else {
|
|
1010 |
}
|
|
1011 |
else {
|
952 |
1012 |
for (Integer i : neworder)
|
953 |
1013 |
order += "" + (i + 1) + ", "; //$NON-NLS-1$ //$NON-NLS-2$
|
954 |
1014 |
}
|
... | ... | |
957 |
1017 |
try {
|
958 |
1018 |
// System.out.println("R sort rownames Reverse"+reverse);
|
959 |
1019 |
REXP r = rw.eval(symbol + "<- " + symbol + "[" + order + ", ];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
960 |
|
//REXP r2 = rw.eval("rownames("+symbol+") <- rownames(" + symbol + ")[" + order + "];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
961 |
|
} catch (RWorkspaceException e) {
|
|
1020 |
// REXP r2 = rw.eval("rownames("+symbol+") <- rownames(" + symbol + ")[" + order + "];"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
|
1021 |
}
|
|
1022 |
catch (RWorkspaceException e) {
|