# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # explore dataset

#
# This set of notebooks is based upon 
# 1. NASA bearing dataset: http://data-acoustics.com/measurements/bearing-faults/bearing-4/
#    Set number 2: 4 accelerometers one on each bearing
# 2. Tutorial: https://towardsdatascience.com/machine-learning-for-anomaly-detection-and-condition-monitoring-d4614e7de770
#
# Acknowledgement is made for the measurements used in this work provided through data-acoustics.com database and the python code from a tutorial by <NAME>.

# +
import configparser
import logging
from ocs_sample_library_preview import *
import json
import pandas as pd
import pprint

logger = logging.getLogger()
logger.setLevel(logging.CRITICAL)

config = configparser.ConfigParser()
config.read('config.ini')

ocsClient = OCSClient(config.get('Access', 'ApiVersion'), config.get('Access', 'Tenant'), config.get('Access', 'Resource'), 
                        config.get('Credentials', 'ClientId'), config.get('Credentials', 'ClientSecret'))

namespace_id = config.get('Configurations', 'Namespace')
print(namespace_id)
# -

# determine the dataset start-end dates
# they should all be the same, so we'll use the signal variables in later cells
import pprint as pprint
for i in range(1,5):
    stream = f'Nasa.bearing{i}.agg'
    signal_starttime = ocsClient.Streams.getFirstValue(namespace_id,stream,None)['timestamp']
    signal_endtime = ocsClient.Streams.getLastValue(namespace_id,stream,None)['timestamp']
    print(f'{stream},{signal_starttime},{signal_endtime}')

# get the first 10 values from each of the aggregate streams to explore what the data looks like
import pprint as pprint
for i in range(1,5):
    signal_starttime = ocsClient.Streams.getFirstValue(namespace_id,f'Nasa.bearing{i}.agg',None)['timestamp']
    pprint.pprint(ocsClient.Streams.getRangeValues(namespace_id,f'Nasa.bearing{i}.agg',start=signal_starttime,count=10,value_class=None,skip=0,reverse=False,boundary_type=SdsBoundaryType.Inside))
    # retrieve summary information


# +
# define a function to format the getSummaries output into a DataFrame based upon a stream query to help understand the data
def sds_summaries_format(query,start=None,end=None,property=None):
    
    if start == None or end == None:
        print("not implemented, specify start and end parameters")
        return None
    
    df_summaries = None
    df_summaries = pd.DataFrame(columns=['Count', 'Minimum', 'Maximum', 'Range', 'Total', 'Mean', 'StandardDeviation', 'PopulationStandardDeviation', 
                               'WeightedMean', 'WeightedStandardDeviation', 'WeightedPopulationStandardDeviation', 'Skewness', 'Kurtosis'])
    for stream in ocsClient.Streams.getStreams(namespace_id,query=query):
        try:
            summary = ocsClient.Streams.getSummaries(namespace_id,stream.Id,start=start,end=end,count=1,
                                                                value_class=None)
            #pprint.pprint(summary)
            for key,value in summary[0]['Summaries'].items():
                df_summaries.loc[stream.Name,key] = value[f'{property}']
        except Exception as e:
            print(f'getSummaries error: {str(e)}')
    
    return(df_summaries)

df = sds_summaries_format("nasa.bearing*.agg",start= signal_starttime,end= signal_endtime,property="channel")
df.sort_index(inplace=True)
df

# +
# create a dataframe of the aggregate dataset that could be used for later analysis work

ocsClient.acceptverbosity=True
dfagg = pd.DataFrame()
for bearing in range(1,5):
    values = ocsClient.Streams.getRangeValues(namespace_id,f'nasa.bearing{bearing}.agg',start= signal_starttime,skip=0,count=985,value_class=None,reverse=False,boundary_type=SdsBoundaryType.Exact)
    df_temp = pd.DataFrame.from_dict(values).set_index('timestamp')
#    df_temp = df_temp.set_index('timestamp')
    df_temp.rename(columns={'channel':f'bearing {bearing}'},inplace=True)
    if dfagg.empty:
        dfagg = dfagg.append(df_temp)
    else:
        dfagg = dfagg.merge(df_temp,on='timestamp')
# -

dfagg.head()

StarcoderdataJupyterScriptsDedupFiltered