class Examples: def __init__(self): """ load/set example params/data. This example perform following operations: - create sample phone battery data stream - perform windowing operation on the stream - store windowed data asa new stream Args: example_name: windowing Notes: this example generatess ome random gps coordinates of Memphis, TN """ self.setup_example() self.gps_clustering() def setup_example(self): """ setup required params for the example: - create cerebralcortex-kernel object - generate sample phone-battery data/metadata - create a DataStream object - save sample stream using cerebralcortex-kernel. """ # create cerebralcortex object self.CC = Kernel("../../conf/") # sample data params self.stream_name = "LOCATION--org.md2k.phonesensor--PHONE" self.user_id = "00000000-afb8-476e-9872-6472b4e66b68" # generate sample phone-battery data/metadata ds = gen_location_datastream(user_id=self.user_id, stream_name=self.stream_name) # save sample data using cerebralcortex-kernal. # now we have some sample data stored in CerebralCortex format to play with!!! self.CC.save_stream(ds) def gps_clustering(self): # create CC Kernel object # get stream data gps_stream = self.CC.get_stream( "LOCATION--org.md2k.phonesensor--PHONE") # apply GPS clustering algorithm centroids = gps_stream.groupby("user").compute(gps_clusters) # print the centroids print("*" * 10, " CLUSTER CENTROIDS COORDINATES ", "*" * 10) centroids.show(truncate=False)
class Examples: def __init__(self, example_name): """ load/set example params/data. This example perform following operations: - create sample phone battery data stream - perform windowing operation on the stream - store windowed data asa new stream Args: example_name: windowing """ self.setup_example() if example_name == "window": self.window_example() def setup_example(self): """ setup required params for the example: - create cerebralcortex-kernel object - generate sample phone-battery data/metadata - create a DataStream object - save sample stream using cerebralcortex-kernel. """ # create cerebralcortex object self.CC = Kernel("../../conf/") # sample data params self.stream_name = "BATTERY--org.md2k.phonesensor--PHONE" self.user_id = "00000000-afb8-476e-9872-6472b4e66b68" # generate sample phone-battery data/metadata data = gen_phone_battery_data(user_id=self.user_id) metadata = gen_phone_battery_metadata(stream_name=self.stream_name) # create a DataStream object ds = DataStream(data, metadata) # save sample data using cerebralcortex-kernal. # now we have some sample data stored in CerebralCortex format to play with!!! self.CC.save_stream(ds) def window_example(self): """ This example will window phone battery stream into 1 minutes chunks and take the average of battery level """ # get sample stream data ds = self.CC.get_stream(self.stream_name) new_ds = ds.window(windowDuration=60) new_ds.show(5) # save newly create data as a new stream in cerebralcortex new_stream_name = "BATTERY--org.md2k.phonesensor--PHONE-windowed-data" new_ds.metadata.set_name(new_stream_name).set_description("1 minute windowed data of phone battery with average battery levels of each window.") \ .add_dataDescriptor( DataDescriptor().set_attribute("description", "start/end time of a window")) \ .add_dataDescriptor( DataDescriptor().set_attribute("description", "average battery values of a window")) \ .add_module( ModuleMetadata().set_name("cerebralcortex.examples.main").set_version("0.1.2").set_attribute("description", "CerebralCortex-kernel example code to window phone battery data").set_author( "test_user", "test_user@test_email.com")) if self.CC.save_stream(new_ds): print(new_stream_name, "has been stored.\n\n")
class Examples: def __init__(self): """ load/set example params/data. This example perform following operations: - create sample phone battery data stream - perform windowing operation on the stream - store windowed data asa new stream Args: example_name: windowing """ self.setup_example() self.basic_datastream_operations() def setup_example(self): """ setup required params for the example: - create cerebralcortex-kernel object - generate sample phone-battery data/metadata - create a DataStream object - save sample stream using cerebralcortex-kernel. """ # create cerebralcortex object self.CC = Kernel("../../conf/") # sample data params self.stream_name="BATTERY--org.md2k.phonesensor--PHONE" self.user_id = "00000000-afb8-476e-9872-6472b4e66b68" # generate sample phone-battery data/metadata data = gen_phone_battery_data(user_id=self.user_id) metadata = gen_phone_battery_metadata(stream_name=self.stream_name) # create a DataStream object ds = DataStream(data, metadata) # save sample data using cerebralcortex-kernal. # now we have some sample data stored in CerebralCortex format to play with!!! self.CC.save_stream(ds) def basic_datastream_operations(self): """ This example will window phone battery stream into 1 minutes chunks and take the average of battery level """ streams = self.CC.list_streams() print(streams) # [{'name': 'BATTERY--org.md2k.phonesensor--PHONE', 'version': 1, 'description': [''], 'metadata_hash':.......] stream_names = self.CC.search_stream("battery") print(stream_names) #['BATTERY--org.md2k.phonesensor--PHONE', 'BATTERY--org.md2k.phonesensor--PHONE-windowed-data'] # get sample stream data ds = self.CC.get_stream(self.stream_name) metadata = ds.get_metadata(version=1) print(metadata) # {'name': 'BATTERY--org.md2k.phonesensor--PHONE', 'version': 1, 'description': [''], 'metadata_ha......... ds.show(3,False) # +-------------------+--------+-------------+---+-------+------------------------------------+ # |timestamp |offset |battery_level|ver|version|user | # +-------------------+--------+-------------+---+-------+------------------------------------+ # |2019-01-09 11:49:28|21600000|92 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:49:29|21600000|92 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:49:30|21600000|92 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # +-------------------+--------+-------------+---+-------+------------------------------------+ ds.filter("battery_level", ">", 97) ds.show(3,False) # +-------------------+--------+-------------+---+-------+------------------------------------+ # |timestamp |offset |battery_level|ver|version|user | # +-------------------+--------+-------------+---+-------+------------------------------------+ # |2019-01-09 11:39:08|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:09|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:10|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # +-------------------+--------+-------------+---+-------+------------------------------------+ ds.filter_user("00000000-afb8-476e-9872-6472b4e66b68") ds.show(3,False) # +-------------------+--------+-------------+---+-------+------------------------------------+ # |timestamp |offset |battery_level|ver|version|user | # +-------------------+--------+-------------+---+-------+------------------------------------+ # |2019-01-09 11:39:08|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:09|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:10|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # +-------------------+--------+-------------+---+-------+------------------------------------+ ds.filter_version("1") ds.show(3,False) # +-------------------+--------+-------------+---+-------+------------------------------------+ # |timestamp |offset |battery_level|ver|version|user | # +-------------------+--------+-------------+---+-------+------------------------------------+ # |2019-01-09 11:39:08|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:09|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # |2019-01-09 11:39:10|21600000|98 |1 |1 |00000000-afb8-476e-9872-6472b4e66b68| # +-------------------+--------+-------------+---+-------+------------------------------------+ ds.window(windowDuration=60) ds.show(3, False) # +------------------------------------+------------------------------------------+--------------------------------- # |user |window |collect_list(battery_level) # +------------------------------------+------------------------------------------+--------------------------------- # |00000000-afb8-476e-9872-6472b4e66b68|[2019-01-09 11:35:00, 2019-01-09 11:36:00]|[100, 100, 100, 100, 100......... # |00000000-afb8-476e-9872-6472b4e66b68|[2019-01-09 11:38:00, 2019-01-09 11:39:00]|[99, 99, 99, 99, 99, 99, 99,..... # |00000000-afb8-476e-9872-6472b4e66b68|[2019-01-09 11:39:00, 2019-01-09 11:40:00]|[98, 98, 98, 98, 98, 98, 98,..... # +------------------------------------+------------------------------------------+--------------------------------- ds.to_pandas() print(ds.data)