Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ public DataFrame<R,C> sma(double windowSize) {
@Override
public DataFrame<R,C> ema(double halfLife) {
try {
if (!inPlace) {
if (halfLife < 0) {
throw new IllegalArgumentException("Half-life for smoothing must be >= 0, " + halfLife + " is illegal");
} else if (halfLife == 0d) {
return frame;
} else if (!inPlace) {
return frame.copy().smooth(true).ema(halfLife);
} else {
final int rowCount = frame.rows().count();
if (rowCount > 0) {
final int colCount = frame.cols().count();
for (int colIndex=0; colIndex<colCount; ++colIndex) {
for (int colIndex = 0; colIndex < colCount; ++colIndex) {
final double value = frame.data().getDouble(0, colIndex);
frame.data().setDouble(0, colIndex, value);
}
Expand All @@ -68,7 +72,7 @@ public DataFrame<R,C> ema(double halfLife) {
if (rowOrdinal > 0) {
row.forEachValue(v -> {
final double rawValue = v.getDouble();
final double emaPrior = v.col().getDouble(rowOrdinal-1);
final double emaPrior = v.col().getDouble(rowOrdinal - 1);
final double emaValue = rawValue * alpha + (1d - alpha) * emaPrior;
v.setDouble(emaValue);
});
Expand All @@ -77,7 +81,7 @@ public DataFrame<R,C> ema(double halfLife) {
}
return frame;
}
} catch (DataFrameException ex) {
} catch (IllegalArgumentException ex) {
throw ex;
} catch (Exception ex) {
throw new DataFrameException("Failed to apply EWMA smoothing to DataFrame", ex);
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/com/zavtech/morpheus/util/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;

import com.zavtech.morpheus.util.IO;
import com.zavtech.morpheus.util.Initialiser;
Expand Down Expand Up @@ -217,14 +218,36 @@ private static class DefaultResponse implements HttpResponse {
});
}

/**
* Returns the content encoding for response
* @return the content encoding
*/
private Optional<String> getContentEncoding() {
for (HttpHeader header : headers) {
if (header.getKey().equals("Content-Encoding")) {
return Optional.ofNullable(header.getValue());
}
}
return Optional.empty();
}

@Override
public HttpStatus getStatus() {
return status;
}

@Override
public InputStream getStream() {
return stream;
try {
final String encoding = getContentEncoding().orElse("default").toLowerCase();
if (encoding.equalsIgnoreCase("gzip")) {
return new GZIPInputStream(stream);
} else {
return stream;
}
} catch (Exception ex) {
throw new RuntimeException("Failed to read from input stream", ex);
}
}

@Override
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/com/zavtech/morpheus/util/text/SmartFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public class SmartFormat extends java.text.Format {
private Matcher timeMatcher1 = Pattern.compile(TIME_REGEX_1).matcher("");
private Matcher timeMatcher2 = Pattern.compile(TIME_REGEX_2).matcher("");

private Format decimalFormat1 = new DecimalFormat("0.0000####;-0.0000####");
private Format decimalFormat2 = new DecimalFormat("0.0000####;-0.0000####");
private Format decimalFormat6 = new DecimalFormat("0.0000####;-0.0000####");
private Format billionsFormat = new DecimalFormat("0.0000'B';-0.0000'B'");
private Format millionsFormat = new DecimalFormat("0.0000'M';-0.0000'M'");
private Format thousandFormat = new DecimalFormat("0.0000'K';-0.0000'K'");
private Format decimalFormat = new DecimalFormat("###,###,##0.0000####;-###,###,##0.0000####");

private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
private DateFormat dateFormat2 = new SimpleDateFormat("dd-MM-yyyy");
private DateFormat dateFormat3 = new SimpleDateFormat("dd/MM/yyyy");
Expand Down Expand Up @@ -105,11 +107,17 @@ public StringBuffer format(Object value, StringBuffer buffer, FieldPosition posi
return buffer;
} else if (value instanceof Double) {
final Double doubleValue = (Double)value;
if (Double.isNaN(doubleValue)) buffer.append("NaN");
else if (doubleValue < 0.001d) buffer.append(decimalFormat1.format(value));
else if (doubleValue < 0d) buffer.append(decimalFormat6.format(value));
else if (doubleValue > 1000000d) buffer.append(decimalFormat1.format(value));
else buffer.append(decimalFormat2.format(value));
if (Double.isNaN(doubleValue)) {
buffer.append("NaN");
} else if (Math.abs(doubleValue) > 1000000000d) {
buffer.append(billionsFormat.format(doubleValue / 1000000000d));
} else if (Math.abs(doubleValue) > 1000000d) {
buffer.append(millionsFormat.format(doubleValue / 1000000d));
} else if (Math.abs(doubleValue) > 1000d) {
buffer.append(thousandFormat.format(doubleValue / 1000d));
} else {
buffer.append(decimalFormat.format(doubleValue));
}
return buffer;
} else if (value instanceof Boolean) {
buffer.append(value.toString());
Expand Down