001 package org.expasy.jpl.commons.collection.stat;
002
003
004 import java.io.BufferedWriter;
005 import java.io.FileWriter;
006 import java.io.IOException;
007
008
009 /**
010 * This object exports {@code HistogramDataSet}s into files or convert into
011 * other objects (for rendering for example).
012 *
013 * @author nikitin
014 *
015 * @version 1.0
016 *
017 */
018 public final class HistogramDataSetExporter {
019
020 /** true if export bins instead of data */
021 private boolean isBinExport;
022
023 // TODO: bin option
024 // center or lower bound ???
025 private boolean isLoadCenterBin;
026
027 private HistogramDataSetExporter() {
028 isLoadCenterBin = true;
029 }
030
031 public static HistogramDataSetExporter newInstance() {
032 return new HistogramDataSetExporter();
033 }
034
035 public void exportBinEnabled() {
036 isBinExport = true;
037 }
038
039 public void exportBinDisabled() {
040 isBinExport = false;
041 }
042
043 public void exportBinStatus(boolean status) {
044 isBinExport = status;
045 }
046
047 public void loadBinCenter() {
048 isLoadCenterBin = true;
049 }
050
051 public void loadBinLowerBound() {
052 isLoadCenterBin = false;
053 }
054
055 public void export(double[] values, String filename) throws IOException {
056
057 try {
058 FileWriter fstream = new FileWriter(filename);
059
060 BufferedWriter out = new BufferedWriter(fstream);
061
062 out.write("data");
063 out.write("\n");
064
065 for (int i = 0; i < values.length; i++) {
066
067 out.write(String.valueOf(values[i]));
068 out.write("\n");
069 }
070 out.close();
071 } catch (IOException e) {
072 e.printStackTrace();
073 throw new IOException("Can't create file " + filename);
074 }
075 }
076
077 public void export(HistogramDataSet histo, String filename)
078 throws IOException {
079
080 try {
081 FileWriter fstream = new FileWriter(filename);
082
083 BufferedWriter out = new BufferedWriter(fstream);
084
085 double[] data = null;
086 double[] freqs = null;
087
088 if (isBinExport) {
089 out.write("bin\tfrequency\n");
090 data = histo.getBins();
091 } else {
092 out.write("data");
093 data = histo.getValues();
094 freqs = histo.getWeights();
095
096 if (freqs != null && freqs.length > 0) {
097 out.write("\tfrequency");
098 }
099 out.write("\n");
100 }
101
102 if (data == null || data.length == 0) {
103 isBinExport = true;
104 out.write("bin\tfrequency\n");
105 data = histo.getBins();
106 }
107
108 for (int i = 0; i < data.length; i++) {
109
110 if (isBinExport) {
111 StatisticalCategory catego = histo.toCategory(i);
112 if (data[i] > 0) {
113 if (isLoadCenterBin) {
114 out.write(String.valueOf(catego.getCenter()));
115 } else {
116 out.write(String.valueOf(catego.getInterval()
117 .getLowerBound()));
118 }
119 out.write("\t" + data[i] + "\n");
120 }
121 } else {
122 out.write(String.valueOf(data[i]));
123
124 if (freqs != null && i < freqs.length) {
125 out.write("\t" + freqs[i]);
126 }
127 out.write("\n");
128 }
129 }
130 out.close();
131 } catch (IOException e) {
132 e.printStackTrace();
133 throw new IOException("Can't create file " + filename);
134 }
135
136 }
137
138 /**
139 * @return the isBinExport
140 */
141 public boolean isBinExport() {
142 return isBinExport;
143 }
144
145 /**
146 * @param isBinExport the isBinExport to set
147 */
148 public void setBinExport(boolean isBinExport) {
149 this.isBinExport = isBinExport;
150 }
151
152 /**
153 * @return the isLoadCenterBin
154 */
155 public boolean isLoadCenterBin() {
156 return isLoadCenterBin;
157 }
158 }