Coverage for nexusLIMS/extractors/plugins/profiles/jeol_jem_642.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2026-03-24 05:23 +0000

1"""Instrument profile for JEOL JEM TEM (642 Stroboscope).""" 

2 

3from __future__ import annotations 

4 

5import logging 

6from typing import TYPE_CHECKING, Any 

7 

8from nexusLIMS.extractors.base import InstrumentProfile 

9from nexusLIMS.extractors.profiles import get_profile_registry 

10 

11if TYPE_CHECKING: 

12 from nexusLIMS.extractors.base import ExtractionContext 

13 

14_logger = logging.getLogger(__name__) 

15 

16 

17def detect_diffraction_from_filename( 

18 metadata: dict[str, Any], 

19 context: ExtractionContext, 

20) -> dict[str, Any]: 

21 """ 

22 Detect diffraction patterns using filename heuristics. 

23 

24 The JEOL Stroboscope doesn't add metadata indicating diffraction mode, 

25 so we use common filename patterns (Diff, SAED, DP) to detect it. 

26 

27 This is not perfect but better than nothing. 

28 

29 Parameters 

30 ---------- 

31 metadata 

32 Metadata dictionary with 'nx_meta' key 

33 context 

34 Extraction context containing file path 

35 

36 Returns 

37 ------- 

38 dict 

39 Modified metadata dictionary with updated dataset type if applicable 

40 """ 

41 filename = str(context.file_path) 

42 

43 # Check for common diffraction pattern naming conventions 

44 for pattern in ["Diff", "SAED", "DP"]: 

45 if ( 

46 pattern.lower() in filename 

47 or pattern.upper() in filename 

48 or pattern in filename 

49 ): 

50 _logger.info( 

51 'Detected file as Diffraction type based on "%s" in the filename', 

52 pattern, 

53 ) 

54 metadata["nx_meta"]["DatasetType"] = "Diffraction" 

55 metadata["nx_meta"]["Data Type"] = "TEM_Diffraction" 

56 break 

57 

58 # Add warnings since detection is unreliable 

59 warnings = metadata["nx_meta"].get("warnings", []) 

60 warnings.append(["DatasetType"]) 

61 warnings.append(["Data Type"]) 

62 metadata["nx_meta"]["warnings"] = warnings 

63 

64 return metadata 

65 

66 

67# Register the profile 

68jeol_jem_642_profile = InstrumentProfile( 

69 instrument_id="JEOL-JEM-TEM", 

70 parsers={ 

71 "diffraction_detection": detect_diffraction_from_filename, 

72 }, 

73 transformations={}, 

74 extension_fields={}, 

75) 

76"""An instrument profile for the JEOL Stroboscope""" 

77 

78get_profile_registry().register(jeol_jem_642_profile) 

79 

80_logger.debug("Registered JEOL JEM TEM (642) instrument profile")