The example illustrates the various metrics that can be obtained about the device detection process, for example, the degree of certainty about the result. Running the example outputs those properties and values.
41 from itertools
import groupby
42 from pathlib
import Path
44 from fiftyone_devicedetection.devicedetection_pipelinebuilder
import DeviceDetectionPipelineBuilder
45 from fiftyone_pipeline_core.logger
import Logger
46 from fiftyone_pipeline_core.basiclist_evidence_keyfilter
import BasicListEvidenceKeyFilter
47 from ..example_utils
import ExampleUtils
48 from fiftyone_devicedetection_shared.example_constants
import EVIDENCE_VALUES
49 from fiftyone_devicedetection_shared.example_constants
import LITE_DATAFILE_NAME
51 class MatchMetricsConsole():
53 def run(self, data_file, show_descs, logger, output):
56 pipeline = DeviceDetectionPipelineBuilder(
57 data_file_path = data_file,
63 performance_profile =
"LowMemory",
67 usage_sharing =
False,
75 restricted_properties=[
"ismobile",
"hardwarename"],
86 use_predictive_graph =
True,
87 use_performance_graph =
False,
90 update_matched_useragent =
True).add_logger(logger).build()
92 ExampleUtils.check_data_file(pipeline, logger)
94 data = pipeline.create_flowdata()
99 data.evidence.add_from_dict(self.Evidence)
104 output(
"--- Compare evidence with what was matched ---\n")
107 for entry
in sorted(self.Evidence.items(), key=
lambda item: len(item[1]), reverse=
True):
108 output(f
" {entry[0]}: {entry[1]}")
112 for entry
in sorted(device.useragents.value(), key=
lambda item: len(item)):
113 output(f
" Matched User-Agent: {entry}")
118 output(
"--- Listing all available properties, by component, by property name ---")
119 output(
"For a discussion of what the match properties mean, see: https://51degrees.com/documentation/_device_detection__hash.html#DeviceDetection_Hash_DataSetProduction_Performance\n")
124 hashEngineElementKey = pipeline.get_element(
"device").datakey
133 availableProperties = dict(pipeline.get_properties()[hashEngineElementKey])
137 def get_component(property):
138 return property[1][
"component"]
140 availableProperties = dict(sorted(availableProperties.items(),key=
lambda item: get_component(item)))
141 categoryMap = groupby(availableProperties.items(), get_component)
143 for component, properties
in categoryMap:
145 for propertyKey, property
in properties:
146 propertyName = property[
"name"]
147 propertyDescription = property[
"description"]
151 value = device[propertyKey]
155 if value.has_value()
and isinstance(value.value(), list):
156 output(f
" {propertyName}: {len(value.value())} Values")
157 for item
in value.value():
161 output(f
" {propertyName}: {value.value()}")
163 if (show_descs ==
True):
164 output(f
" {propertyDescription}")
167 logger.log(
"info",
"Finished Match Metrics Example")
171 Evidence = EVIDENCE_VALUES[2]
182 data_file = argv[0]
if len(argv) > 0
else ExampleUtils.find_file(LITE_DATAFILE_NAME)
185 logger = Logger(min_level=
"info")
187 if (data_file !=
None):
188 MatchMetricsConsole().run(data_file,
False, logger,
print)
191 "Failed to find a device detection data file. Make sure the " +
192 "device-detection-data submodule has been updated by running " +
193 "`git submodule update --recursive`.")
195 if __name__ ==
"__main__":