|
| 1 | +from cortex import Cortex |
| 2 | + |
| 3 | +class Records(): |
| 4 | + """ |
| 5 | + A class to query records, request to download undownloaded records, and export local records |
| 6 | +
|
| 7 | + Attributes |
| 8 | + ---------- |
| 9 | + c : Cortex |
| 10 | + Cortex communicate with Emotiv Cortex Service |
| 11 | +
|
| 12 | + Methods |
| 13 | + ------- |
| 14 | + start(): |
| 15 | + start authorize process. |
| 16 | + query_records(): |
| 17 | + To query records owned by the current user |
| 18 | + request_download_records() |
| 19 | + To request to download records if they are not at local machine |
| 20 | + export_record() |
| 21 | + To export records to CSV/ EDF files |
| 22 | + """ |
| 23 | + def __init__(self, app_client_id, app_client_secret, **kwargs): |
| 24 | + """ |
| 25 | + Constructs cortex client and bind a function to query records, request to download and export records |
| 26 | + If you do not want to log request and response message , set debug_mode = False. The default is True |
| 27 | + """ |
| 28 | + print("Query Records __init__") |
| 29 | + self.c = Cortex(app_client_id, app_client_secret, debug_mode=True, **kwargs) |
| 30 | + self.c.bind(authorize_done=self.on_authorize_done) |
| 31 | + self.c.bind(query_records_done=self.on_query_records_done) |
| 32 | + self.c.bind(export_record_done=self.on_export_record_done) |
| 33 | + self.c.bind(request_download_records_done=self.on_request_download_records_done) |
| 34 | + self.c.bind(inform_error=self.on_inform_error) |
| 35 | + |
| 36 | + def start(self): |
| 37 | + """ |
| 38 | + To open websocket and process authorizing step. |
| 39 | + After get authorize_done The program will query records |
| 40 | + Returns |
| 41 | + ------- |
| 42 | + None |
| 43 | + """ |
| 44 | + self.c.open() |
| 45 | + |
| 46 | + def query_records(self, license_id, application_id): |
| 47 | + """ |
| 48 | + To query records |
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + orderBy : array of object, required : Specify how to sort the records. |
| 52 | + query: object, required: An object to filter the records. |
| 53 | + If you set an empty object, it will return all records created by your application |
| 54 | + If you want get records created by other application, you need set licenseId and applicationId as parameter of query object |
| 55 | + More detail at https://emotiv.gitbook.io/cortex-api/records/queryrecords |
| 56 | + Returns |
| 57 | + ------- |
| 58 | + """ |
| 59 | + |
| 60 | + query_obj = {} |
| 61 | + if license_id != '': |
| 62 | + query_obj["licenseId"] = license_id |
| 63 | + if application_id != '': |
| 64 | + query_obj["applicationId"] = application_id |
| 65 | + |
| 66 | + query_params = { |
| 67 | + "orderBy": [{ "startDatetime": "DESC" }], |
| 68 | + "query": query_obj, |
| 69 | + "includeSyncStatusInfo":True |
| 70 | + } |
| 71 | + self.c.query_records(query_params) |
| 72 | + |
| 73 | + def request_download_records(self, record_ids): |
| 74 | + """ |
| 75 | + To request to download records |
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + record_ids : list, required: list of wanted record ids |
| 79 | + More detail at https://emotiv.gitbook.io/cortex-api/records/requesttodownloadrecorddata |
| 80 | + Returns |
| 81 | + ------- |
| 82 | + None |
| 83 | + """ |
| 84 | + self.c.request_download_records(record_ids) |
| 85 | + |
| 86 | + def export_record(self, record_ids, license_ids): |
| 87 | + """ |
| 88 | + To export records |
| 89 | + By default, you can only export the records that were created by your application. |
| 90 | + If you want to export a record that was created by another applications |
| 91 | + then you must provide the license ids of those applications in the parameter licenseIds. |
| 92 | + Parameters |
| 93 | + record_ids: list, required: list of wanted export record ids |
| 94 | + license_ids: list, no required: list of license id of other applications |
| 95 | + ---------- |
| 96 | + More detail at https://emotiv.gitbook.io/cortex-api/records/exportrecord |
| 97 | + Returns |
| 98 | + ------- |
| 99 | + None |
| 100 | + """ |
| 101 | + folder = '' # your place to export, you should have write permission, for example: 'C:\\Users\\NTT\\Desktop' |
| 102 | + stream_types = ['EEG', 'MOTION', 'PM', 'BP'] |
| 103 | + export_format = 'CSV' # support 'CSV' or 'EDF' |
| 104 | + version = 'V2' |
| 105 | + self.c.export_record(folder, stream_types, export_format, record_ids, version, licenseIds=license_ids) |
| 106 | + |
| 107 | + |
| 108 | + # callbacks functions |
| 109 | + def on_authorize_done(self, *args, **kwargs): |
| 110 | + print('on_authorize_done') |
| 111 | + # query records |
| 112 | + self.query_records(self.license_id, self.application_id) |
| 113 | + |
| 114 | + # callbacks functions |
| 115 | + def on_query_records_done(self, *args, **kwargs): |
| 116 | + data = kwargs.get('data') |
| 117 | + count = kwargs.get('count') |
| 118 | + print('on_query_records_done: total records are {0}'.format(count)) |
| 119 | + # print(data) |
| 120 | + not_downloaded_record_Ids = [] |
| 121 | + export_record_Ids = [] |
| 122 | + license_ids = [] |
| 123 | + for item in data: |
| 124 | + uuid = item['uuid'] |
| 125 | + sync_status = item["syncStatus"]["status"] |
| 126 | + application_id = item["applicationId"] |
| 127 | + license_id = item["licenseId"] |
| 128 | + print("recordId {0}, applicationId {1}, sync status {2}".format(uuid, application_id, sync_status)) |
| 129 | + if (sync_status == "notDownloaded") : |
| 130 | + not_downloaded_record_Ids.append(uuid) |
| 131 | + elif (sync_status == "neverUploaded") or (sync_status == "downloaded"): |
| 132 | + export_record_Ids.append(uuid) |
| 133 | + if license_id not in license_ids: |
| 134 | + license_ids.append(license_id) |
| 135 | + |
| 136 | + # download records has not downloaded to local machine |
| 137 | + if len(not_downloaded_record_Ids) > 0: |
| 138 | + self.request_download_records(not_downloaded_record_Ids) |
| 139 | + |
| 140 | + # Open comment below to export records |
| 141 | + # if len(export_record_Ids) > 0: # or export records are in local machine |
| 142 | + # self.export_record(export_record_Ids, license_ids) |
| 143 | + |
| 144 | + def on_export_record_done(self, *args, **kwargs): |
| 145 | + print('on_export_record_done: the successful record exporting as below:') |
| 146 | + data = kwargs.get('data') |
| 147 | + print(data) |
| 148 | + self.c.close() |
| 149 | + |
| 150 | + def on_request_download_records_done(self, *args, **kwargs): |
| 151 | + data = kwargs.get('data') |
| 152 | + success_records = [] |
| 153 | + for item in data['success']: |
| 154 | + record_Id = item['recordId'] |
| 155 | + print('The record '+ record_Id + ' is downloaded successfully.') |
| 156 | + success_records.append(record_Id) |
| 157 | + |
| 158 | + for item in data['failure']: |
| 159 | + record_Id = item['recordId'] |
| 160 | + failed_msg = item['message'] |
| 161 | + print('The record '+ record_Id + ' is downloaded unsuccessfully. Because: ' + failed_msg) |
| 162 | + |
| 163 | + self.c.close() |
| 164 | + |
| 165 | + def on_inform_error(self, *args, **kwargs): |
| 166 | + error_data = kwargs.get('error_data') |
| 167 | + print(error_data) |
| 168 | + |
| 169 | +# ----------------------------------------------------------- |
| 170 | +# |
| 171 | +# GETTING STARTED |
| 172 | +# - Please reference to https://emotiv.gitbook.io/cortex-api/ first. |
| 173 | +# - Please make sure the your_app_client_id and your_app_client_secret are set before starting running. |
| 174 | +# RESULT |
| 175 | +# - on_query_records_done: will show all records filtered by query condition in query_record |
| 176 | +# - on_export_record_done: will show the successful exported record |
| 177 | +# - on_request_download_records_done: will show all success and failure download case |
| 178 | +# |
| 179 | +# ----------------------------------------------------------- |
| 180 | + |
| 181 | +def main(): |
| 182 | + |
| 183 | + # Enter your application Client ID and Client Secret below. |
| 184 | + # You can obtain these credentials after registering your App ID with the Cortex SDK for development. |
| 185 | + # For instructions, visit: https://emotiv.gitbook.io/cortex-api#create-a-cortex-app |
| 186 | + your_app_client_id = 'put_your_app_client_id_here' |
| 187 | + your_app_client_secret = 'put_your_app_client_secret_here' |
| 188 | + |
| 189 | + # Don't need to create session in this case |
| 190 | + r = Records(your_app_client_id, your_app_client_secret, auto_create_session= False) |
| 191 | + |
| 192 | + # As default, the Program will query records of your application. |
| 193 | + # In the case, you want to query records created from other application (such as EmotivPRO). |
| 194 | + # You need set license_id and application_id of the application |
| 195 | + # If set license_id without application_id. It will return records created from all applications use the license_id |
| 196 | + # If set both license_id and application_id. It will return records from the application has the application_id |
| 197 | + r.license_id = '' |
| 198 | + r.application_id = '' |
| 199 | + |
| 200 | + r.start() |
| 201 | + |
| 202 | +if __name__ =='__main__': |
| 203 | + main() |
| 204 | + |
| 205 | +# ----------------------------------------------------------- |
0 commit comments