Skip to content

Commit

Permalink
moving machines'
Browse files Browse the repository at this point in the history
  • Loading branch information
mesargent committed Oct 28, 2015
1 parent bb8e01e commit b19bf2f
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.detroitteatime.datagatherer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.example.datagatherer.R;

public class CategoryDialog extends Activity {
private Button enter;
private TextView name, category;
private RadioGroup methodGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_dialog);
methodGroup = (RadioGroup)findViewById(R.id.method_group);
name = (TextView)findViewById(R.id.name);
category = (TextView)findViewById(R.id.category);
enter = (Button)findViewById(R.id.enter_category);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = methodGroup.getCheckedRadioButtonId();
RadioButton selected = (RadioButton)findViewById(selectedId);
String method = selected.getText().toString();

switch(method){
case "Logistic Regression":
method = Constants.LOGISIC_REGRESSION;
break;
case "Support Vector Machine":
method = Constants.SVM;
break;
case "Neural Net":
method = Constants.NEURAL_NET;
break;
}

DataBaseHelper helper = new DataBaseHelper(CategoryDialog.this);
helper.insertModel(name.getText().toString(),category.getText().toString(), method, null, null);
helper.close();

Intent intent = new Intent(CategoryDialog.this, ModelList.class);
startActivity(intent);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public final class Constants {
public static final String DATA =
"com.detroitteatime.datagatherer.DATA";

public static final String LOGISIC_REGRESSION = "log_reg";

public static final String SVM = "svm";

public static final String NEURAL_NET = "neural_net";

}
111 changes: 85 additions & 26 deletions app/src/main/java/com/detroitteatime/datagatherer/DataBaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.util.List;


public class DataBaseHelper extends SQLiteOpenHelper{

private SQLiteDatabase ourDatabase;
Expand All @@ -29,6 +28,7 @@ public class DataBaseHelper extends SQLiteOpenHelper{
public static final String NAME = "name";
public static final String CLASS = "class";
public static final String PARAMETERS = "parameters";
public static final String METHOD = "method";

public static final String SPEED_GPS = "speed_GPS";
public static final String SPEED_ACCEL = "speed_accel";
Expand Down Expand Up @@ -57,11 +57,11 @@ public class DataBaseHelper extends SQLiteOpenHelper{
public static final String TEMPERATURE = "temperature";
public static final String POSITIVE = "in_category";
public static final String DB_NAME = "data";
public static final String ID = "_id";


public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);

}

@Override
Expand Down Expand Up @@ -101,20 +101,19 @@ public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + MODEL_TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT, " +
CLASS + " TEXT " +
MODEL + " TEXT" +
PARAMETERS + "TEXT);");
CLASS + " TEXT, " +
MODEL + " TEXT, " +
METHOD + " TEXT, " +
PARAMETERS + " TEXT);");



}catch(SQLiteException e){
Log.e("My Code", "DataBaseHelper, db.execSQL(): CREATE TABLE Speed Data " + e.getMessage());
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Add some sql later

}
public void open(int i){
Expand All @@ -124,7 +123,6 @@ public void open(int i){
}else{
ourDatabase = getReadableDatabase();
}

}

public void close(){
Expand All @@ -138,28 +136,36 @@ public void insertData(DataSet data){
ContentValues cv = setValues(data);

try{

ourDatabase = getWritableDatabase();
ourDatabase.insertOrThrow(SENSOR_TABLE_NAME, "nullColumnHack", cv);

}catch(Exception e){
if(e.getMessage()!= null) Log.e("My Code", e.getMessage());
}
}

public void insertDataArray(List<DataSet> dataList){
ourDatabase = getWritableDatabase();
for(DataSet d: dataList){
insertData(d);
ContentValues cv = setValues(d);

try{
ourDatabase.insertOrThrow(SENSOR_TABLE_NAME, "nullColumnHack", cv);
ourDatabase.close();
}catch(Exception e){
if(e.getMessage()!= null) Log.e("My Code", e.getMessage());
}
}

}

public Cursor getData(){
ourDatabase = getReadableDatabase();
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(SENSOR_TABLE_NAME);


Cursor cursor = builder.query(ourDatabase, null, null, null,
null, null, TIME, null);

return cursor;

}
Expand Down Expand Up @@ -192,46 +198,99 @@ public ContentValues setValues(DataSet data){
cv.put(POSITIVE, data.isPositive());

return cv;

}

//// Model methods /////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////

public ContentValues setModelCV(String name, String model, String c){
public ContentValues setModelCV(String name, String c, String method, String params, String model){

ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(MODEL, model);
cv.put(CLASS, c);
cv.put(METHOD, method);
cv.put(PARAMETERS, params);

return cv;
}

public void insertModel(String c, String name, String model){
ContentValues cv = setModelCV(name, model, c);

public void insertModel(String name, String c, String method, String params, String model){
ourDatabase = getWritableDatabase();
ContentValues cv = setModelCV(name, c, method, params, model);
try{

ourDatabase.insertOrThrow(MODEL_TABLE_NAME, "nullColumnHack", cv);

}catch(Exception e){
if(e.getMessage()!= null) Log.e("My Code", e.getMessage());
}
}

public void editModel(long id, String name, String c, String method, String params, String model){
ourDatabase = getWritableDatabase();
ContentValues cv = setModelCV(name, model, c, method, params);
String[] ids = {String.valueOf(id)};
ourDatabase.update(MODEL_TABLE_NAME, cv, "_id=?", ids);

public Cursor getModelByName(String name){
}

public Cursor getModelById(long id){
ourDatabase = getReadableDatabase();
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(SENSOR_TABLE_NAME);
builder.setTables(MODEL_TABLE_NAME);
String[] ids = {String.valueOf(id)};

Cursor cursor = builder.query(ourDatabase, null, null, null,
null, null, TIME, null);
Cursor cursor = builder.query(ourDatabase, null, "_id=?", ids,
null, null, null, null);

return cursor;

}

public Cursor getModelData(){
ourDatabase = getReadableDatabase();
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(MODEL_TABLE_NAME);

Cursor cursor = builder.query(ourDatabase, null, null, null,
null, null, null, null);

return cursor;
}

public Predictor getPredictorById(long id){
ourDatabase = getReadableDatabase();
Cursor c = getModelById(id);
c.moveToFirst();
int idx = c.getColumnIndex(METHOD);
Predictor p = null;
String method = c.getString(idx);
String name = c.getString(c.getColumnIndex(NAME));
String model = c.getString(c.getColumnIndex(MODEL));
String category = c.getString(c.getColumnIndex(CLASS));
String parameters = c.getString(c.getColumnIndex(PARAMETERS));

switch(method){

case Constants.LOGISIC_REGRESSION:
p = new LogisticPredictor();
p.setCategory(category);
p.setModel(model);
p.setName(name);
p.setParameterString(parameters);
p.setMethod(method);
break;

case Constants.SVM:
break;
case Constants.NEURAL_NET:
break;
}
c.close();

return p;
}

public void persistPredictor(Predictor p){
insertModel(p.getName(), p.getCategory(), p.getMethod(), p.getParameterString(), p.getModel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/
public class LogisticPredictor extends Predictor{

private double yIntercept;

@Override
public double predictProb(int[] parameters, int[] features, int yIntercept) {
public double predictProb(double[] features) {
if(parameters.length != features.length) throw new IllegalArgumentException("Feature and parameter arrays must be the same length.");
double pTX = 0;

Expand All @@ -18,13 +20,22 @@ public double predictProb(int[] parameters, int[] features, int yIntercept) {
}


public boolean predict(int[] parameters, int[] features, int yIntercept){
return predictProb(parameters, features, yIntercept) > 0.5;
@Override
public boolean predict(double[] features) {
return predictProb(features) > 0.5;
}

public double sigmoid(double pTransposeF) throws IllegalArgumentException{

return 1 / (1 + Math.exp(- pTransposeF));
return 1 / (1 + Math.exp(- pTransposeF));

}

public double getyIntercept() {
return yIntercept;
}

public void setyIntercept(double yIntercept) {
this.yIntercept = yIntercept;
}
}
Loading

0 comments on commit b19bf2f

Please sign in to comment.