Coverage for /home/deng/Projects/metatree_drawer/treeprofiler_algo/pastml/pastml/__init__.py: 37%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
« prev ^ index » next coverage.py v7.2.7, created at 2024-03-21 09:19 +0100
1from datetime import datetime
3METHOD = 'method'
4STATES = 'states'
5CHARACTER = 'character'
7NUM_SCENARIOS = 'num_scenarios'
8NUM_UNRESOLVED_NODES = 'num_unresolved_nodes'
9NUM_STATES_PER_NODE = 'num_states_per_node_avg'
10PERC_UNRESOLVED = 'percentage_of_unresolved_nodes'
11NUM_NODES = 'num_nodes'
12NUM_TIPS = 'num_tips'
15def datetime2numeric(d):
16 """
17 Converts a datetime date to numeric format.
18 For example: 2016-12-31 -> 2016.9972677595629; 2016-1-1 -> 2016.0
19 :param d: a date to be converted
20 :type d: np.datetime
21 :return: numeric representation of the date
22 :rtype: float
23 """
24 first_jan_this_year = datetime(year=d.year, month=1, day=1)
25 day_of_this_year = d - first_jan_this_year
26 first_jan_next_year = datetime(year=d.year + 1, month=1, day=1)
27 days_in_this_year = first_jan_next_year - first_jan_this_year
28 return d.year + day_of_this_year / days_in_this_year
31def numeric2datetime(d):
32 """
33 Converts a numeric date to datetime format.
34 For example: 2016.9972677595629 -> 2016-12-31; 2016.0 -> 2016-1-1
35 :param d: numeric representation of a date to be converted
36 :type d: float
37 :return: the converted date
38 :rtype: np.datetime
39 """
40 year = int(d)
41 first_jan_this_year = datetime(year=year, month=1, day=1)
42 first_jan_next_year = datetime(year=year + 1, month=1, day=1)
43 days_in_this_year = first_jan_next_year - first_jan_this_year
44 day_of_this_year = int(round(days_in_this_year.days * (d % 1), 6)) + 1
45 for m in range(1, 13):
46 days_in_m = (datetime(year=year if m < 12 else (year + 1), month=m % 12 + 1, day=1)
47 - datetime(year=year, month=m, day=1)).days
48 if days_in_m >= day_of_this_year:
49 return datetime(year=year, month=m, day=day_of_this_year)
50 day_of_this_year -= days_in_m
53def col_name2cat(column):
54 """
55 Reformats the column string to make sure it contains only numerical, letter characters or underscore.
57 :param column: column name to be reformatted
58 :type column: str
59 :return: column name with illegal characters removed
60 :rtype: str
61 """
62 column_string = ''.join(s for s in column.replace(' ', '_') if s.isalnum() or '_' == s)
63 return column_string
66def get_personalized_feature_name(character, feature):
67 """
68 Precedes the feature name by the character name
69 (useful when likelihoods for different characters are calculated in parallel).
71 :param character: str, character name
72 :param feature: str, feature to be personalized
73 :return: str, the personalized feature
74 """
75 return '{}_{}'.format(character, feature)
78def value2list(n, value, default_value):
79 # create a variable for n columns
80 # specifying the default value if nothing was chosen
81 if value is None:
82 value = default_value
83 # and propagating the chosen value to all columns
84 if not isinstance(value, list):
85 value = [value] * n
86 elif len(value) == 1:
87 value = value * n
88 # or making sure that the default value is chosen for the columns for which the value was not specified
89 else:
90 value += [default_value] * (n - len(value))
91 return value