Merge pull request #361 from MilosKozak/oref-insulincurves

Oref Insulincurves
This commit is contained in:
Milos Kozak 2017-08-15 10:13:50 +02:00 committed by GitHub
commit 4c490c32f7
14 changed files with 533 additions and 1 deletions

View file

@ -32,6 +32,9 @@ import info.nightscout.androidaps.plugins.ConstraintsObjectives.ObjectivesFragme
import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin; import info.nightscout.androidaps.plugins.ConstraintsSafety.SafetyPlugin;
import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment; import info.nightscout.androidaps.plugins.InsulinFastacting.InsulinFastactingFragment;
import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment; import info.nightscout.androidaps.plugins.InsulinFastactingProlonged.InsulinFastactingProlongedFragment;
import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakFragment;
import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefRapidActingFragment;
import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefUltraRapidActingFragment;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.Loop.LoopFragment; import info.nightscout.androidaps.plugins.Loop.LoopFragment;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientInternalFragment;
@ -113,6 +116,9 @@ public class MainApp extends Application {
if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin()); if (Config.ACTION) pluginsList.add(ActionsFragment.getPlugin());
pluginsList.add(InsulinFastactingFragment.getPlugin()); pluginsList.add(InsulinFastactingFragment.getPlugin());
pluginsList.add(InsulinFastactingProlongedFragment.getPlugin()); pluginsList.add(InsulinFastactingProlongedFragment.getPlugin());
pluginsList.add(InsulinOrefRapidActingFragment.getPlugin());
pluginsList.add(InsulinOrefUltraRapidActingFragment.getPlugin());
pluginsList.add(InsulinOrefFreePeakFragment.getPlugin());
pluginsList.add(SensitivityOref0Plugin.getPlugin()); pluginsList.add(SensitivityOref0Plugin.getPlugin());
pluginsList.add(SensitivityAAPSPlugin.getPlugin()); pluginsList.add(SensitivityAAPSPlugin.getPlugin());
pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin()); pluginsList.add(SensitivityWeightedAveragePlugin.getPlugin());

View file

@ -14,6 +14,7 @@ import android.preference.PreferenceManager;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.events.EventRefreshGui; import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.InsulinOrefCurves.InsulinOrefFreePeakPlugin;
import info.nightscout.androidaps.plugins.PumpDanaR.BluetoothDevicePreference; import info.nightscout.androidaps.plugins.PumpDanaR.BluetoothDevicePreference;
import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin; import info.nightscout.androidaps.plugins.PumpDanaR.DanaRPlugin;
import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin; import info.nightscout.androidaps.plugins.PumpDanaRKorean.DanaRKoreanPlugin;
@ -139,6 +140,11 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) { if (virtualPumpPlugin != null && virtualPumpPlugin.isEnabled(PluginBase.PUMP)) {
addPreferencesFromResource(R.xml.pref_virtualpump); addPreferencesFromResource(R.xml.pref_virtualpump);
} }
InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin = (InsulinOrefFreePeakPlugin) MainApp.getSpecificPlugin(InsulinOrefFreePeakPlugin.class);
if(insulinOrefFreePeakPlugin.isEnabled(PluginBase.INSULIN)){
addPreferencesFromResource(R.xml.pref_insulinoreffreepeak);
}
NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class); NSClientInternalPlugin nsClientInternalPlugin = (NSClientInternalPlugin) MainApp.getSpecificPlugin(NSClientInternalPlugin.class);
if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) { if (nsClientInternalPlugin != null && nsClientInternalPlugin.isEnabled(PluginBase.GENERAL)) {
addPreferencesFromResource(R.xml.pref_nsclientinternal); addPreferencesFromResource(R.xml.pref_nsclientinternal);

View file

@ -12,6 +12,10 @@ import info.nightscout.androidaps.db.Treatment;
public interface InsulinInterface { public interface InsulinInterface {
final int FASTACTINGINSULIN = 0; final int FASTACTINGINSULIN = 0;
final int FASTACTINGINSULINPROLONGED = 1; final int FASTACTINGINSULINPROLONGED = 1;
final int OREF_RAPID_ACTING = 2;
final int OREF_ULTRA_RAPID_ACTING = 3;
final int OREF_FREE_PEAK = 4;
int getId(); int getId();
String getFriendlyName(); String getFriendlyName();

View file

@ -21,7 +21,6 @@ import info.nightscout.androidaps.interfaces.InsulinInterface;
*/ */
public class ActivityGraph extends GraphView { public class ActivityGraph extends GraphView {
Context context; Context context;
public ActivityGraph(Context context) { public ActivityGraph(Context context) {
@ -35,6 +34,8 @@ public class ActivityGraph extends GraphView {
} }
public void show(InsulinInterface insulin) { public void show(InsulinInterface insulin) {
removeAllSeries();
mSecondScale = null;
double dia = insulin.getDia(); double dia = insulin.getDia();
int hours = (int) Math.floor(dia + 1); int hours = (int) Math.floor(dia + 1);

View file

@ -0,0 +1,108 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
/**
* Created by adrian on 13.08.2017.
*/
public abstract class InsulinOrefBasePlugin implements PluginBase, InsulinInterface {
public static double MIN_DIA = 5;
long lastWarned = 0;
@Override
public int getType() {
return INSULIN;
}
@Override
public String getNameShort() {
return MainApp.sResources.getString(R.string.insulin_shortname);
}
@Override
public boolean canBeHidden(int type) {
return true;
}
@Override
public boolean hasFragment() {
return true;
}
@Override
public boolean showInList(int type) {
return true;
}
@Override
public double getDia() {
double dia = getUserDefinedDia();
if(dia >= MIN_DIA){
return dia;
} else {
if((System.currentTimeMillis() - lastWarned) > 60*1000) {
lastWarned = System.currentTimeMillis();
Notification notification = new Notification(Notification.SHORT_DIA, String.format(MainApp.sResources.getString(R.string.dia_too_short), dia, MIN_DIA), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
}
return MIN_DIA;
}
}
public double getUserDefinedDia() {
return MainApp.getConfigBuilder().getProfile() != null ? MainApp.getConfigBuilder().getProfile().getDia() : Constants.defaultDIA;
}
@Override
public Iob iobCalcForTreatment(Treatment treatment, long time, Double dia) {
Iob result = new Iob();
int peak = getPeak();
if (treatment.insulin != 0d) {
long bolusTime = treatment.date;
double t = (time - bolusTime) / 1000d / 60d;
double td = getDia()*60; //getDIA() always > 5
double tp = peak;
// force the IOB to 0 if over DIA hours have passed
if (t < td) {
double tau = tp * (1 - tp / td) / (1 - 2 * tp / td);
double a = 2 * tau / td;
double S = 1 / (1 - a + (1 + a) * Math.exp(-td / tau));
result.activityContrib = treatment.insulin * (S / Math.pow(tau, 2)) * t * (1 - t / td) * Math.exp(-t / tau);
result.iobContrib = treatment.insulin * (1 - S * (1 - a) * ((Math.pow(t, 2) / (tau * td * (1 - a)) - t / tau - 1) * Math.exp(-t / tau) + 1));
}
}
return result;
}
@Override
public String getComment() {
String comment = commentStandardText();
double userDia = getUserDefinedDia();
if(userDia < MIN_DIA){
comment += "\n" + String.format(MainApp.sResources.getString(R.string.dia_too_short), userDia, MIN_DIA);
}
return comment;
}
abstract int getPeak();
abstract String commentStandardText();
}

View file

@ -0,0 +1,58 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefFreePeakFragment extends Fragment {
static InsulinOrefFreePeakPlugin insulinPlugin = new InsulinOrefFreePeakPlugin();
static public InsulinOrefFreePeakPlugin getPlugin() {
return insulinPlugin;
}
TextView insulinName;
TextView insulinComment;
TextView insulinDia;
ActivityGraph insulinGraph;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.insulin_fragment, container, false);
insulinName = (TextView) view.findViewById(R.id.insulin_name);
insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
updateGUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateGUI();
}
private void updateGUI() {
insulinName.setText(insulinPlugin.getFriendlyName());
insulinComment.setText(insulinPlugin.getComment());
insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h");
insulinGraph.show(insulinPlugin);
}
}

View file

@ -0,0 +1,67 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.utils.SP;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefFreePeakPlugin extends InsulinOrefBasePlugin {
private static boolean fragmentEnabled = false;
private static boolean fragmentVisible = false;
public static final int DEFAULT_PEAK = 75;
@Override
public int getId() {
return OREF_FREE_PEAK;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.free_peak_oref);
}
@Override
public String getFragmentClass() {
return InsulinOrefFreePeakFragment.class.getName();
}
@Override
public String getFriendlyName() {
return MainApp.sResources.getString(R.string.free_peak_oref);
}
@Override
public String commentStandardText() {
return MainApp.sResources.getString(R.string.insulin_peak_time) + ": " + getPeak();
}
@Override
public boolean isEnabled(int type) {
return type == INSULIN && fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return type == INSULIN && fragmentVisible;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == INSULIN) this.fragmentVisible = fragmentVisible;
}
@Override
int getPeak() {
return SP.getInt(R.string.key_insulin_oref_peak, DEFAULT_PEAK);
}
}

View file

@ -0,0 +1,57 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefRapidActingFragment extends Fragment {
static InsulinOrefRapidActingPlugin insulinPlugin = new InsulinOrefRapidActingPlugin();
static public InsulinOrefRapidActingPlugin getPlugin() {
return insulinPlugin;
}
TextView insulinName;
TextView insulinComment;
TextView insulinDia;
ActivityGraph insulinGraph;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.insulin_fragment, container, false);
insulinName = (TextView) view.findViewById(R.id.insulin_name);
insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
updateGUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateGUI();
}
private void updateGUI() {
insulinName.setText(insulinPlugin.getFriendlyName());
insulinComment.setText(insulinPlugin.getComment());
insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h");
insulinGraph.show(insulinPlugin);
}
}

View file

@ -0,0 +1,66 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefRapidActingPlugin extends InsulinOrefBasePlugin {
private static boolean fragmentEnabled = false;
private static boolean fragmentVisible = false;
public static final int PEAK = 75;
@Override
public int getId() {
return OREF_RAPID_ACTING;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.rapid_acting_oref);
}
@Override
public String getFragmentClass() {
return InsulinOrefRapidActingFragment.class.getName();
}
@Override
public String getFriendlyName() {
return MainApp.sResources.getString(R.string.rapid_acting_oref);
}
@Override
public String commentStandardText() {
return MainApp.sResources.getString(R.string.fastactinginsulincomment);
}
@Override
public boolean isEnabled(int type) {
return type == INSULIN && fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return type == INSULIN && fragmentVisible;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == INSULIN) this.fragmentVisible = fragmentVisible;
}
@Override
int getPeak() {
return PEAK;
}
}

View file

@ -0,0 +1,58 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.InsulinFastacting.ActivityGraph;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefUltraRapidActingFragment extends Fragment {
static InsulinOrefUltraRapidActingPlugin insulinPlugin = new InsulinOrefUltraRapidActingPlugin();
static public InsulinOrefUltraRapidActingPlugin getPlugin() {
return insulinPlugin;
}
TextView insulinName;
TextView insulinComment;
TextView insulinDia;
ActivityGraph insulinGraph;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.insulin_fragment, container, false);
insulinName = (TextView) view.findViewById(R.id.insulin_name);
insulinComment = (TextView) view.findViewById(R.id.insulin_comment);
insulinDia = (TextView) view.findViewById(R.id.insulin_dia);
insulinGraph = (ActivityGraph) view.findViewById(R.id.insuling_graph);
updateGUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateGUI();
}
private void updateGUI() {
insulinName.setText(insulinPlugin.getFriendlyName());
insulinComment.setText(insulinPlugin.getComment());
insulinDia.setText(MainApp.sResources.getText(R.string.dia) + " " + new Double(insulinPlugin.getDia()).toString() + "h");
insulinGraph.show(insulinPlugin);
}
}

View file

@ -0,0 +1,66 @@
package info.nightscout.androidaps.plugins.InsulinOrefCurves;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by adrian on 14/08/17.
*/
public class InsulinOrefUltraRapidActingPlugin extends InsulinOrefBasePlugin {
private static boolean fragmentEnabled = false;
private static boolean fragmentVisible = false;
public static final int PEAK = 55;
@Override
public int getId() {
return OREF_ULTRA_RAPID_ACTING;
}
@Override
public String getName() {
return MainApp.sResources.getString(R.string.ultrarapid_oref);
}
@Override
public String getFragmentClass() {
return InsulinOrefUltraRapidActingFragment.class.getName();
}
@Override
public String getFriendlyName() {
return MainApp.sResources.getString(R.string.ultrarapid_oref);
}
@Override
public String commentStandardText() {
return MainApp.sResources.getString(R.string.ultrafastactinginsulincomment);
}
@Override
public boolean isEnabled(int type) {
return type == INSULIN && fragmentEnabled;
}
@Override
public boolean isVisibleInTabs(int type) {
return type == INSULIN && fragmentVisible;
}
@Override
public void setFragmentEnabled(int type, boolean fragmentEnabled) {
if (type == INSULIN) this.fragmentEnabled = fragmentEnabled;
}
@Override
public void setFragmentVisible(int type, boolean fragmentVisible) {
if (type == INSULIN) this.fragmentVisible = fragmentVisible;
}
@Override
int getPeak() {
return PEAK;
}
}

View file

@ -47,6 +47,8 @@ public class Notification {
public static final int NSANNOUNCEMENT = 18; public static final int NSANNOUNCEMENT = 18;
public static final int NSALARM = 19; public static final int NSALARM = 19;
public static final int NSURGENTALARM = 20; public static final int NSURGENTALARM = 20;
public static final int SHORT_DIA = 21;
public int id; public int id;
public Date date; public Date date;

View file

@ -581,6 +581,7 @@
<string name="configbuilder_insulin">Insulin</string> <string name="configbuilder_insulin">Insulin</string>
<string name="fastactinginsulin">Fast Acting Insulin</string> <string name="fastactinginsulin">Fast Acting Insulin</string>
<string name="fastactinginsulincomment">Novorapid, Novolog, Humalog</string> <string name="fastactinginsulincomment">Novorapid, Novolog, Humalog</string>
<string name="ultrafastactinginsulincomment">Fiasp</string>
<string name="insulin_shortname">INS</string> <string name="insulin_shortname">INS</string>
<string name="fastactinginsulinprolonged">Fast Acting Insulin Prolonged</string> <string name="fastactinginsulinprolonged">Fast Acting Insulin Prolonged</string>
<string name="key_usesuperbolus" translatable="false">key_usersuperbolus</string> <string name="key_usesuperbolus" translatable="false">key_usersuperbolus</string>
@ -690,5 +691,16 @@
<string name="careportal_pump_label">PUMP</string> <string name="careportal_pump_label">PUMP</string>
<string name="overview_newtempbasal_basalabsolute">Basal value [U/h]</string> <string name="overview_newtempbasal_basalabsolute">Basal value [U/h]</string>
<string name="careportal_newnstreatment_duration_min_label">Duration [min]</string> <string name="careportal_newnstreatment_duration_min_label">Duration [min]</string>
<string name="key_insulin_oref_peak" translatable="false">insulin_oref_peak</string>
<string name="insulin_oref_peak">IOB Curve Peak Time</string>
<string name="insulin_peak_time">Peak Time [min]</string>
<string name="free_peak_oref">Free-Peak Oref</string>
<string name="rapid_acting_oref">Rapid-Acting Oref</string>
<string name="ultrarapid_oref">Ultra-Rapid Oref</string>
<string name="dia_too_short" formatted="false">"DIA of %s too short - using %s instead!"</string>
</resources> </resources>

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:validate="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="InsulinOrefFreePeak"
android:title="@string/insulin_oref_peak">
<com.andreabaccega.widget.ValidatingEditTextPreference
validate:testType="numericRange"
validate:minNumber="35"
validate:maxNumber="120"
android:digits="0123456789"
android:defaultValue="75"
android:selectAllOnFocus="true"
android:inputType="number"
android:maxLines="20"
android:title="@string/insulin_peak_time"
android:key="@string/key_insulin_oref_peak" />
</PreferenceCategory>
</PreferenceScreen>