Skip to content

Data Loading

Yue Wu edited this page May 6, 2021 · 8 revisions

Server Data

Every time the user puts their basic information or changes the lifestyle, the app will communicate with a Bodylogical demo server (managed by PwC) to fetch the health data.

We use Json.NET to parse Archetype and and Lifestyle data into JSON, and from JSON back to Health. You can find the code in NetworkUtils.

Since the server is only for demo purposes, it is sometimes unstable, and will return 500 errors in such cases. When calling methods from the utils class, make sure you use a while loop to keep sending requests if you encounter a server error. See ShowInfo in AppStateManager for details.

There are two methods:

  • UserMatch() will query the server and match the user's basic information (gender, age, weight, height) against an existing archetype in the database (hence we don't need lifestyle data). After getting the id for the archetype, it will then request for a "baseline" data: what would happen in the future if the user doesn't change their lifestyle?

  • Forecast() will take the archetype's id (which is stored in the basic information object) and a lifestyle and request for the future health.

Unit conversions

PwC has plans to use this app in Japan, which uses the SI unit (cm for height, kg for weight) instead of ft/inch and lb. Therefore, we need a front-end solution to convert the user inputs between two units (the back-end/server will always use standard units). We will use UnitManager to achieve that. It will convert height/weight values between the two metrics, and will convert the weight value on the data panels accordingly.

Local Data files

Assets/Resources holds all local resources that need to be dynamically loaded. Specifically, they include:

  • Archetype data

    • Lifestyle.csv specifies the "presets". In addition to the avatar that the user is controlling, there will be two "presets" -- one with a better lifestyle, one with a worse one -- on the side, for comparison.

    • Ranges.csv specifies the range of each biomarker. Each range includes a min/max value, as well as the bounds for warning/danger zones. It also has a gender field, since the criteria for different genders might be different.

  • Localization files

    • Filename is formatted as "locale-[language].xml". LocalizationManager will look for localization files of this format and load them into the app when prompted (either when the app starts or when the user requests a language change).

Loading and Parsing Archetype Data

Scripts on data loading are stored in Scripts/Data and the DataLoader script. This section only covers archetype data. For information on parsing localization files, see Localization System.

We use the method Resources.Load() to load assets to resources. For usage, see https://docs.unity3d.com/ScriptReference/Resources.Load.html. Notice that when you load a file, you do not need to include its extension.

According to https://learn.unity.com/tutorial/assets-resources-and-assetbundles Section 3, the best practice of using Resources is to not use it. The tutorial also covers AssetBundles as an alternative to Resources.

The CSV Parser

The data is stored as csv files. Unity (or C#) has no native support for csv, so I wrote one by myself.

Take a look at the LoadCsv() method. The usage is

CSVParser.LoadCsv<T>(string str);

where str is the csv string (NOT filename). For the generic <T>, pass in the type you want it to convert. For example, in LoadLifestyles(), we write

TextAsset textAsset = Resources.Load<TextAsset>("Data/Lifestyles");
List<Lifestyle> archetypes = CSVParser.LoadCsV<Lifestyle>(textAsset.text);

Calculation

The Health Range script includes a method for calculating a 0-100 score from a given biomarker. It does so by considering 60-100 as healthy (so min-warning), 30-60 as moderate (warning-upper), and 0-30 as unhealthy (upper-max).

The Health Util Script includes a method for converting a score to a status based on breakpoints. You can decide what breakpoints are ideal for the score, to maximize the "dramatic" effects in visualization.

The Long Term Health is a collection of health data for an archetype. It contains a method CalculateHealth() that calculates an overall health score at a specific time point. This data is used in Stats visualization.

Clone this wiki locally