Add value to DataPoint by user - android-graphview

How to add values to DataPoint which to be plotted in case I do not know number of possible entries?
DataPoint[] db = new DataPoint[]{
new DataPoint(5,4),
new DataPoint(2,4),
new DataPoint(4,4),
new DataPoint(9,0),
new DataPoint(0,3)} ;
PointsGraphSeries<DataPoint> series = new PointsGraphSeries<DataPoint>(db) ;

Make a call to this method before you add DataPoint array to LineGraphSeries.
private void generateDataForGraph() {
int size = Amount.size();
values = new DataPoint[size];
for (int i=0; i<size; i++) {
Integer xi = Integer.parseInt(Dates.get(i));
Integer yi = Integer.parseInt(Amount.get(i));
DataPoint v = new DataPoint(xi, yi);
values[i] = v;
}
series = new LineGraphSeries<DataPoint>(values);
graph2.addSeries(series);
}

Related

Hundred thousands of datatable records to PDF in web API

I'm trying to create PDF from the DataTable in web api using ADO.Net. Unfortunately based on filters some times I may get very less records & able to download without any problem. Sometimes may be very huge like 200 thousand of records. When I'm checking in local my system its getting hang while converting the dt to PDF. My code is like below:
private FileContentResult ExportPDF(DataTable dataTable)
{
string Name = "Logs";
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
byte[] content = null;
try
{
string[] columnNames = (from dc in dataTable.Columns.Cast<DataColumn>() select dc.ColumnName).ToArray();
int count = columnNames.Length;
object[] array = new object[count];
dataTable.Rows.Add(array);
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
HeaderFooter header = new HeaderFooter(new Phrase(Name), false);
// Remove the border that is set by default
header.Border = iTextSharp.text.Rectangle.TITLE;
// Align the text: 0 is left, 1 center and 2 right.
header.Alignment = Element.ALIGN_CENTER;
pdfDoc.Header = header;
// Header.
pdfDoc.Open();
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1; pdfTable.Width = 100;
pdfTable.Padding = 1; pdfTable.Spacing = 4;
//creating table headers
for (int i = 0; i < cols; i++)
{
Cell cellCols = new Cell();
Chunk chunkCols = new Chunk();
iTextSharp.text.Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.Black);
chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);
}
//creating table data (actual result)
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++)
{
Cell cellRows = new Cell();
iTextSharp.text.Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
content = mStream.ToArray();
return File(content, "application/pdf", "LogReports.pdf");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

DL4J linear regression

I am new in neural networks. I am trying to implement and train simple neural network with DL4j. My function:
y = x * 2 + 300
My vision
My result
Parameters:
public final int seed = 12345;
public final int iterations = 1;
public final int nEpochs = 1;
public final int batchSize = 1000;
public final double learningRate = 0.01;
public final Random rng = new Random(seed);
public final int numInputs = 2;
public final int numOutputs = 1;
public final double maxX = 100;//xmax = 100; ymax=500.
public final double scale = 500;//for scale out x and y.
Network configuration:
public MultiLayerConfiguration createConf() {
return new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.learningRate(learningRate)
.weightInit(WeightInit.XAVIER)
.updater(new Nesterovs(0.9))
.list()
.layer(0, new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
.activation(Activation.IDENTITY)
.nIn(numInputs).nOut(numOutputs).build())
.pretrain(false).backprop(true).build();
}
Training data:
public DataSetIterator generateTrainingData() {
List<DataSet> list = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
double x = rng.nextDouble() * maxX * (rng.nextBoolean() ? 1 : -1);
double y = y(x);
list.add(
new DataSet(
Nd4j.create(new double[]{x / scale, 1}),
Nd4j.create(new double[]{y / scale})
)
);
}
return new ListDataSetIterator(list, batchSize);
}
Testing:
public void test() {
final MultiLayerNetwork net = new MultiLayerNetwork(createConf());
net.init();
net.setListeners(new ScoreIterationListener(1));
for (int i = 0; i < nEpochs; i++) {
net.fit(generateTrainingData());
}
int idx = 0;
double x[] = new double[19];
double y[] = new double[19];
double p[] = new double[19];
for (double i = -90; i < 100; i += 10) {
x[idx] = i;
y[idx] = y(i);
p[idx] = scale * net.output(Nd4j.create(new double[]{i / scale, 1})).getDouble(0, 0);
idx++;
}
plot(x, y, p);
}
Please tell me what i am doing wrong or if i have incorrect vision...
Thank you in advance,
Regards,
Minas
Take a look at this example:
https://github.com/deeplearning4j/dl4j-examples/tree/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/feedforward/regression
Few tips:
Use our built in normalization tools. Don't do this yourself.
Our normalization tools allow you to normalize labels as well.
Turn minibatch off (set minibatch(false) on the neural net config near the top)
Ultimately you still aren't actually doing "minibatch learning"
Also, you're regenerating the dataset each time. There's no need to do that. Just create it once and pass it in to fit.
For visualization purposes, use the restore mechanism I mentioned earlier (This is in the example, you can pick 1 of any of the normalizers like MinMaxScalar, NormalizeStandardize,.. etc)
Your iterations are also wrong. Just keep that value at 1 and keep your for loop. Otherwise you're just overfitting and spending way more of your training time then you need to. An "iteration" is actually the number of updates you want to run per fit call on the same dataset. Next release we are getting rid of that option anyways.

Why subscriptions to IObservables taken from a Collection don't work (and what to do about it)

My goal is to create a bunch of observables from a source observable, so that I can subscribe to them individually.
When I do that manually (that is, creating each subSource manually), things work as expected: the values added to the original source propagate adequately to the subSources.
But when I create them in a loop, adding them to a List<IObservable<T>>, the subscriptions to elements taken from that list don't seem to be working:
class Program
{
static void Main(string[] args)
{
// using Subject for the sake of example
var source = new Subject<int>();
// manually creating each subSource
var source0 = source.Where((t, i) => i % 3 == 0);
var source1 = source.Where((t, i) => i % 3 == 1);
var source2 = source.Where((t, i) => i % 3 == 2);
// creating a List of subsources
List<IObservable<int>> sources = new List<IObservable<int>>();
int count = 3;
for (int i = 0; i < count; i++)
{
sources.Add(source.Where((v, ix) => ix % 3 == i));
}
// subscribing to one subSource from each group
source0.Subscribe(Console.WriteLine); // this works
sources[1].Subscribe(Console.WriteLine); // this doesn't
// feeding data
Observable.Range(0, 20).Subscribe(source);
Console.ReadKey();
}
}
The predicate of your Where clause references the loop variable i.
However, the predicate is tested when a value is published from source - not when the loop is iterated. By the that time, i has reached it's final value 3.
To fix this, create a new variable inside the loop to store the current value of i, and reference that in your Where predicate:
for (int i = 0; i < count; i++)
{
var j = i;
sources.Add(source.Where((v, ix) => ix % 3 == j)); // note use of j here
}
for (int i = 0; i < count; i++)
{
**var j = i;**
sources.Add(source.Where((v, ix) => ix % 3 == j));
}
Closures.

Add Object To Other Object Scripts that require an Object

Ok , I have problem with adding object let's first see what is this about.
I Have Map when i click at map i create Sphere on map.
then i serialized the point, msg and name .....
void Start () {
Ser = new Serial();
Ser.read();
for (int i =0 ; i < Ser.list.Count; i++)
{
Data d = new Data();
d = Ser.list[i];
GameObject a = GameObject.CreatePrimitive(PrimitiveType.Sphere);
a.name = d.Id;
a.transform.position = new Vector3(d.x,d.y,0);
a.transform.localScale =new Vector3(0.5f , 0.5f , 0.5f);
Follow putTarget = new Follow();
Data Pos_In_File = new Data();
Pos_In_File = Ser.list[i];
GUIText GText = new GUIText();
Vector3 SP = new Vector3();
GameObject TextObj = new GameObject("GUIText" + Convert.ToString(i));
GText = (GUIText)TextObj.AddComponent(typeof(GUIText));
GText.text = Pos_In_File.msg;
GText.material.color = new Color(0 , 0 , 1);
GText.fontSize = 20;
GText.fontStyle = FontStyle.Bold;
SP = Camera.main.WorldToViewportPoint(new Vector3(Pos_In_File.x , Pos_In_File.y , Pos_In_File.z));
TextObj.transform.position = SP;
TextObj.AddComponent(typeof(Follow));
GameObject OBJ = GameObject.Find(Pos_In_File.Id);
putTarget =GetComponent<Follow>();
putTarget.target = OBJ.transform;
}
}
now in last three lines i want to add OBJ to TextOBJ->Follow(Script)->target
What i am messing up ?
i asked this quastion long ago my software have been finnished a 10 month
and the i get through this problem is
void Start () {
Resources.UnloadUnusedAssets();
Ser = new Serial();
Screen.fullScreen = true;
Ser.read();
for (int i =0 ; i < Ser.list.Count; i++)
{
Data d = new Data();
d = Ser.list[i];
GameObject a = GameObject.CreatePrimitive(PrimitiveType.Sphere);
a.name = d.Id;
a.transform.position = new Vector3(d.x,d.y,0);
a.renderer.material.color = Color.green;
Data Pos_In_File = new Data();
Pos_In_File = Ser.list[i];
GUIText GText = new GUIText();
Vector3 SP = new Vector3();
GameObject TextObj = new GameObject(Pos_In_File.Id+"h");
GText = (GUIText)TextObj.AddComponent(typeof(GUIText));
GText.text = Pos_In_File.msg;
GText.material.color = new Color(0 , 0 , 1);
GText.fontSize = 20 + (int)Camera.main.transform.position.z;
GText.fontStyle = FontStyle.Bold;
SP = Camera.main.WorldToViewportPoint(new Vector3(Pos_In_File.x , Pos_In_File.y , Pos_In_File.z));
TextObj.transform.position = SP;
TextObj.AddComponent(typeof(Follow));
}

How to clear the Overlay using GWT

I have a class which I used to overlay rectangles on maps. But I am not able to figure out how to remove the previous overlay's to draw new rectangles if a new set of results are provided to my displayOnMap method.
To provide more insight on the draw method. It takes in PlotSetOutput as an argument which contains centers and each center contains a set of lat/long co-ordinates. Hence the logic for looping over it and creating lat/long bounds and assigning it to rectangle objects.
public class displayOnMap extends Composite {
private final VerticalPanel pWidget;
private MapWidget mapWidget;
private static Rectangle rectangle;
private RectangleOptions rectOpts;
private static final LatLng USCENTER = LatLng.newInstance(33.68,-116.17);
public displayOnMap(PlotSetOutput result) {
pWidget = new VerticalPanel();
initWidget(pWidget);
draw(result);
}
private void draw(PlotSetOutput result) {
MapOptions mapOpts = MapOptions.newInstance();
mapOpts.setZoom(4);
mapOpts.setCenter(USCENTER);
mapOpts.setMapTypeId(MapTypeId.TERRAIN);
mapWidget = new MapWidget(mapOpts);
pWidget.add(mapWidget);
mapWidget.setSize("800px", "800px");
ArrayList<Centers> listOfCenters = new ArrayList<Centers>();
List<ResultClusterPlots> finalCluster = result.getFinalcluster();
int totalNumberOfClusters = result.getTotalNumberOfClusters();
for (int i = 0; i < totalNumberOfClusters; i++) {
listOfCenters.add(i, new Centers());
}
for (int j = 0; j < finalCluster.size(); j++) {
Centers p = listOfCenters.get(finalCluster.get(j).getClusterID()-1);
LatLng ne = LatLng.newInstance(finalCluster.get(j).getLatitude()
.get(0), finalCluster.get(j).getLongitude().get(0));
LatLng sw = LatLng.newInstance(finalCluster.get(j).getLatitude()
.get(1), finalCluster.get(j).getLongitude().get(1));
p.setLatLongArr(LatLngBounds.newInstance(ne,sw));
}
for (int k = 0; k < listOfCenters.size(); k++) {
ArrayList<LatLngBounds> ltlgBound = listOfCenters.get(k).getLatLongArr();
String color = getRandomColor();
for (int l = 0; l < ltlgBound.size(); l++) {
rectOpts = RectangleOptions.newInstance();
rectOpts.setStrokeColor("#FF0000");
rectOpts.setStrokeOpacity(0.3);
rectOpts.setStrokeWeight(2);
rectOpts.setFillColor(color);
rectOpts.setFillOpacity(0.35);
rectOpts.setMap(mapWidget);
rectOpts.setBounds(ltlgBound.get(l));
rectangle = Rectangle.newInstance(rectOpts);
rectangle.setMap(mapWidget);
}
}
}
}
Output when the method (displayOnMap) is invoked for the first time. Everything works fine.
Output when the displayOnMap method is called with a second query.
I tried to do rectangle.setMap(null); pWidget.removeFromParent(); but I kept getting the same result.
I have had the same problem, the only way I could hide / delete / show the overlays was through the OverlayCompleteMapEvent.
As I understand it isn't possible to get a hook to an overlay before it is completed, once it is completed the only way to get a hook on it is handling "OverlayCompleteMapEvent" event. I used a list to store the Overlays and then I could hide/show/delete them.