You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_path_parser.py 5.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Test the path_parser module."""
  16. from datetime import datetime
  17. from unittest import TestCase, mock
  18. from mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
  19. from mindinsight.lineagemgr.common.path_parser import SummaryPathParser
  20. MOCK_SUMMARY_DIRS = [
  21. {
  22. 'relative_path': './relative_path0'
  23. },
  24. {
  25. 'relative_path': './'
  26. },
  27. {
  28. 'relative_path': './relative_path1'
  29. }
  30. ]
  31. MOCK_SUMMARIES = [
  32. {
  33. 'file_name': 'file0',
  34. 'create_time': datetime.fromtimestamp(1582031970)
  35. },
  36. {
  37. 'file_name': 'file0_lineage',
  38. 'create_time': datetime.fromtimestamp(1582031970)
  39. },
  40. {
  41. 'file_name': 'file1',
  42. 'create_time': datetime.fromtimestamp(1582031971)
  43. },
  44. {
  45. 'file_name': 'file1_lineage',
  46. 'create_time': datetime.fromtimestamp(1582031971)
  47. }
  48. ]
  49. class TestSummaryPathParser(TestCase):
  50. """Test the class of SummaryPathParser."""
  51. @mock.patch.object(SummaryWatcher, 'list_summary_directories')
  52. def test_get_summary_dirs(self, *args):
  53. """Test the function of get_summary_dirs."""
  54. args[0].return_value = MOCK_SUMMARY_DIRS
  55. expected_result = [
  56. '/path/to/base/relative_path0',
  57. '/path/to/base',
  58. '/path/to/base/relative_path1'
  59. ]
  60. base_dir = '/path/to/base'
  61. result = SummaryPathParser.get_summary_dirs(base_dir)
  62. self.assertListEqual(expected_result, result)
  63. args[0].return_value = []
  64. result = SummaryPathParser.get_summary_dirs(base_dir)
  65. self.assertListEqual([], result)
  66. @mock.patch.object(SummaryWatcher, 'list_summaries')
  67. def test_get_latest_lineage_summary(self, *args):
  68. """Test the function of get_latest_lineage_summary."""
  69. args[0].return_value = MOCK_SUMMARIES
  70. summary_dir = '/path/to/summary_dir'
  71. result = SummaryPathParser.get_latest_lineage_summary(summary_dir)
  72. self.assertEqual('/path/to/summary_dir/file1_lineage', result)
  73. args[0].return_value = [
  74. {
  75. 'file_name': 'file0',
  76. 'create_time': datetime.fromtimestamp(1582031970)
  77. }
  78. ]
  79. result = SummaryPathParser.get_latest_lineage_summary(summary_dir)
  80. self.assertEqual(None, result)
  81. args[0].return_value = [
  82. {
  83. 'file_name': 'file0_lineage',
  84. 'create_time': datetime.fromtimestamp(1582031970)
  85. }
  86. ]
  87. result = SummaryPathParser.get_latest_lineage_summary(summary_dir)
  88. self.assertEqual(None, result)
  89. args[0].return_value = [
  90. {
  91. 'file_name': 'file0_lineage',
  92. 'create_time': datetime.fromtimestamp(1582031970)
  93. },
  94. {
  95. 'file_name': 'file0_lineage_lineage',
  96. 'create_time': datetime.fromtimestamp(1582031970)
  97. },
  98. {
  99. 'file_name': 'file1_lineage',
  100. 'create_time': datetime.fromtimestamp(1582031971)
  101. },
  102. {
  103. 'file_name': 'file1_lineage_lineage',
  104. 'create_time': datetime.fromtimestamp(1582031971)
  105. }
  106. ]
  107. result = SummaryPathParser.get_latest_lineage_summary(summary_dir)
  108. self.assertEqual('/path/to/summary_dir/file1_lineage_lineage', result)
  109. @mock.patch.object(SummaryWatcher, 'list_summaries')
  110. @mock.patch.object(SummaryWatcher, 'list_summary_directories')
  111. def test_get_latest_lineage_summaries(self, *args):
  112. """Test the function of get_latest_lineage_summaries."""
  113. args[0].return_value = MOCK_SUMMARY_DIRS
  114. args[1].return_value = MOCK_SUMMARIES
  115. expected_result = [
  116. '/path/to/base/relative_path0/file1_lineage',
  117. '/path/to/base/file1_lineage',
  118. '/path/to/base/relative_path1/file1_lineage'
  119. ]
  120. base_dir = '/path/to/base'
  121. result = SummaryPathParser.get_latest_lineage_summaries(base_dir)
  122. self.assertListEqual(expected_result, result)
  123. args[1].return_value = [
  124. {
  125. 'file_name': 'file0_lineage',
  126. 'create_time': datetime.fromtimestamp(1582031970)
  127. }
  128. ]
  129. result = SummaryPathParser.get_latest_lineage_summaries(base_dir)
  130. self.assertListEqual([], result)

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。