Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This guide serves as a starting point for your C3 journey, but does not expose most of C3's API or potential.

Connecting to a C3 Cluster

There are a couple options for connecting to the C3 Cluster:

Fetching Instances of Types

All data in C3 is represented by a 'Type'. Data for a specific type can be 'fetched' from C3 using the 'fetch' API.
In each language, each Type has a 'fetch' function to which a FetchSpec Type can be passed. This function then retrieves
the data in a FetchResult Type which can be opened and used for data analysis.

...

  • filter: Defines an expression to evaluate for each type. When the expression evaluates as true, that type is fetched.
  • limit: Fetch only 'limit' results. Can be useful to debug a fetch which might grab a lot of results.
  • include: Define specific properties of the Type to include in the fetch result. If not defined, all properties will be grabbed.
  • order: An expression which defines the order to return the results in.

Examples of Fetch operations

Consider the DTI housing example located here: https://github.com/c3aidti/HouseCoverageExample

...

  1. FetchSpec Documentation: https://developer.c3.ai/docs/7.12.0/type/FetchSpec
  2. Fetching in Python: https://developer.c3.ai/docs/7.12.0/topic/ds-jupyter-notebooks

Converting Fetch results to usable forms in Jupyter Notebook

For most data analysis situations, FetchResults need to be massaged a little bit to be useful.
Here, we show the typical ways FetchResults can be used.

Python

In python, generally, you get the 'objs' property from the FetchResults object, then call the toJson() function.
This function returns an array of dictionaries each with keys equal to the requested properties of the fetched
type. This works well with the pandas DataFrame constructor which accepts such an array. The returned
DataFrame object can now be analyzed very easily. We show an example below.

...

Users can then use the resulting dataframe as they normally would.

ExpressionEngineFunctions

Several spots in the C3 API allow for the use of so-called 'ExpressionEngineFunctions'. These functions take a variety of
arguments and perform a variety of processing tasks. For example, the function 'contains' takes two strings and checks
whether the first argument contains the second argument. The function 'lowerCase' takes an input string, and returns
that string with all uppercase letters replaced with lowercase ones. In addition to those string processing functions,
many math functions exist as well such as 'log' which operates on a variety of input data types.

...

Official C3 documentation for ExpressionEngineFunctions: https://developer.c3.ai/docs/7.12.0/type/ExpressionEngineFunction

Evaluating Metrics on Time series data

C3 can store timeseries data using many different types, however knowing the exact type of timeseries data isn't
super important when it comes to evaluating so-called 'Metrics' on that data.

Normalization Process

Usually, timeseries data goes through a 'normalization' process, the purpose of which is to take non-uniform, and possibly multiple datasets and produce
a single uniform timeseries which can be analyzed a little more easily in most cases. We copy here the list of normalization steps that are currently
performed within the C3 platform, these are available from C3's official documentation here: https://developer.c3.ai/docs/7.12.0/guide/guide-c3aisuite-basic/ts-normalization-engine

...

Once the normalization process is complete, a single time series sampled at a uniform interval is given.

SimpleMetrics

Simple metrics form the 'base' of the Metrics system. They are defined on a specific Type and reference timeseries data stored within.
Essentially, the Simple metric defines:

...

For more detail, see the C3 documentation on SimpleMetrics here: https://developer.c3.ai/docs/7.12.0/guide/guide-c3aisuite-basic/metrics-simple-metrics

CompoundMetrics

Compound metrics are generally easier to define and use as they operate on already defined metrics either Simple or Compound.
They essentially just consist of and id/name, and an expression defining the metric in terms of constants and already defined metrics.
If you try and execute a CompoundMetric on a type for which some necessary SimpleMetric is not defined, you'll get an error.

...

An example CompoundMetric is:

Code Block
languagepy
sample_compound_met = c3.SimpleMetric.make({
	'id': 'CompoundMetric',
	'name': 'CompoundMetric',
	'expression': 'window("AVG", SimpleMetric, 0, 7)',
})

...

For more detail, see the C3 documentation on CompoundMetrics here: https://developer.c3.ai/docs/7.12.0/guide/guide-c3aisuite-basic/metrics-compound-metrics

Evaluating Metrics

Not all SimpleMetrics are defined on all types. Types on which you can evaluate a metric mixin the Type 'MetricEvaluatable' (C3 Docs here: https://developer.c3.ai/docs/7.12.0/type/MetricEvaluatable)

Finding Metrics

Not all SimpleMetrics are defined on all types.
This bestows the function 'listMetrics' (among others) to that type, so if you're unsure what kind of metrics are available for a given type, execute that function to get a list, for example:

Javascript:

Code Block
languagejs
var metrics = SmartBulb.listMetrics()
c3Grid(metrics)

Python:

Code Block
languagepy
pd.DataFrame(c3.SmartBulb.listMetrics().toJson())

Once you have the metric you want to evaluate in mind, you can evaluate it.

Evaluating Metrics

With a metric in mind, you can use the 'evalMetrics' API function which is brought in with the MetricEvaluatable type to actually evaluate the metric.
The evalMetrics function takes an 'EvalMetricsSpec' type which contains the following:

  1. list of Type ids you want the metrics to be evaluated on
  2. A list of metrics you want to be evaluated
  3. A start date (in ISO 8601 format)
  4. An end date (in ISO 8601 format)
  5. An evaluation interval

Such an evaluation in Python might look like this:

Code Block
languagepy
spec = c3.EvalMetricsSpec({
	'ids': [ 'A', 'B', 'C' ],
	'expressions': [ 'SampleMetric', 'SampleMetric2' ],
	'start': '2019-01-01',
	'end': '2019-05-01',
	'interval': 'DAY',
})

results = c3.SampleType.evalMetrics(spec=spec)

The results are in the form of a 'EvalMetricsResult'. By itself, this type isn't easily usable, however C3 offers the type 'Dataset' which is better suited for data analysis.
We can then convert the EvalMetricsResult to a Dataset using a convenient helper function and then in the case of Python to a pandas DataFrame using another
helper function.

Code Block
languagepy
ds = c3.Dataset.fromEvalMetricsResult(result=results)
df = c3.Dataset.toPandas(dataset=ds)

If you're in the browser using Javascript, you can utilize the 'c3Viz' console function to display the result of eval metrics. The whole evaluation might look like this:

Code Block
languagejs
var spec = EvalMetricsSpec(
	ids= ['A', 'B', 'C' ],
	expressions= [ 'SampleMetric', 'SampleMetric2' ],
	start= '2019-01-01',
	end= '2019-05-01',
	interval= 'DAY')

var results = SampleType.evalMetrics(spec)
c3Viz(results)

Bonus: An additional function is available as well: evalMetric, Have a look at the MetricEvaluatable documentation to see how it differs from evalMetrics: https://developer.c3.ai/docs/7.12.0/type/MetricEvaluatable

Conclusion

To get started quickly, focus on 'CompoundMetrics'. They're the easiest to use, and for most cases, the 'AVG' treatment works well.

...

Review and Next Steps

Principals from this Primer areFor most data exploration, you'll find yourself 'Fetching' and running 'evalMetrics'. This guide provides a good foundation for these activities.
This first set of activities might be described as 'Read-Only'. Here you're using the C3 AI Suite as simply a readable database and API.
The next set of things to learn would be 'Write' type operations. How do you define new types? How do you 'persist' new instances of a type?
How do you clean the databases in your tag up? And so on. These will be the subject of a planned DTI Guide.