phyddle 0.3.1__tar.gz → 0.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  [bumpversion]
2
- current_version = 0.3.1
2
+ current_version = 0.3.2
3
3
 
4
4
  [bumpversion:file:pyproject.toml]
5
5
 
@@ -90,3 +90,51 @@ docs/source/tables/glossary_raw.csv
90
90
  docs/source/tables/make_phyddle_settings_table.sh
91
91
  workspace/levy_r/config_modeltest.py
92
92
  workspace/levy_r/plot_tree.R
93
+
94
+ tests/workspace/valid/simulate/out.*.stdout.log
95
+ tests/workspace/valid/simulate/out.*.stderr.log
96
+
97
+
98
+ docs/example.zip
99
+ docs/out.network_architecture.png
100
+ docs/source/images/favicon2.svg
101
+ fig1_phyddle_pipeline.pptx
102
+ tests/workspace.bak/
103
+ uv.lock
104
+ workspace/albert/
105
+ workspace/bisse_r/bisse_r.tar.gz
106
+ workspace/bisse_r/empirical_outlier/
107
+ workspace/bisse_r/format_undertrain/
108
+ workspace/bisse_r/history.txt
109
+ workspace/bisse_r/plot_outlier/
110
+ workspace/bisse_r/plot_undertrain/
111
+ workspace/bisse_r/plot_within/
112
+ workspace/bisse_r/train_overtrain/
113
+ workspace/bisse_r/train_undertrain/
114
+ workspace/bisse_r_acp/
115
+ workspace/bisse_r_basic/empirical.bisse/
116
+ workspace/bisse_r_basic/empirical.sir/
117
+ workspace/bisse_r_basic/new_empirical/
118
+ workspace/bisse_r_nolabel/
119
+ workspace/bisse_validate_r/
120
+ workspace/chromo.zip
121
+ workspace/chromo/
122
+ workspace/chromosizesse/
123
+ workspace/ddgeosse_jl/
124
+ workspace/levy_1tree/
125
+ workspace/levy_r/compare.conf_mtx.csv
126
+ workspace/levy_r/phyddle.conf_mtx.csv
127
+ workspace/levy_r/plot copy/
128
+ workspace/levy_r/plot_conf_mtx.R
129
+ workspace/levy_r/pulsr.conf_mtx.csv
130
+ workspace/levy_r/validate_levy.pdf
131
+ workspace/mol_revbayes/history.txt
132
+ workspace/sim_chromosse_test.py
133
+ workspace/sir_master/test/
134
+ workspace/sirm_all_phases_master/SIRM_postexp.phyddle_proj.tar.gz
135
+ workspace/sirm_all_phases_master/post_exponential_sim_files/
136
+ workspace/sirm_all_phases_master/postexp_bayesian_analysis/
137
+ workspace/sirm_all_phases_master/postexp_cnn_est/
138
+ workspace/sirm_exp_phase_master/SIRM_exp.phyddle_proj.tar.gz
139
+ workspace/sirm_exp_phase_master/sysbio_2024_bayes_estimates/
140
+ workspace/sirm_exp_phase_master/sysbio_phylocomp_files/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: phyddle
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Software for phylogenetic model exploration with deep learning
5
5
  Project-URL: Homepage, https://github.com/mlandis/phyddle
6
6
  Project-URL: Bug Reports, https://github.com/mlandis/phyddle/issues
@@ -34,7 +34,7 @@ Requires-Dist: torchview>=0.2.6
34
34
  Requires-Dist: tqdm>=4.66.1
35
35
  Description-Content-Type: text/markdown
36
36
 
37
- # phyddle 0.3.1
37
+ # phyddle 0.3.2
38
38
 
39
39
  Software for exploring phylogenetic models with deep learning [[manuscript](https://doi.org/10.1093/sysbio/syaf036)]
40
40
 
@@ -1,4 +1,4 @@
1
- # phyddle 0.3.1
1
+ # phyddle 0.3.2
2
2
 
3
3
  Software for exploring phylogenetic models with deep learning [[manuscript](https://doi.org/10.1093/sysbio/syaf036)]
4
4
 
@@ -1,3 +1,11 @@
1
+ **phyddle v0.3.2** -- 26.11.19
2
+
3
+ * Test/validation/calibration datasets are now processed in batches, allowing larger datasets to be processed
4
+ * Plot now skips PCA plots for datasets that contain no variation (e.g. if all training examples use the same fixed tree)
5
+ * Corrected calibration symmetric adjustment factor for conformalized quantile prediction (introduced in v0.3.1)
6
+ * Updated remaining code to be compatible with pandas v3.0.0+
7
+ * Minor updates to documentation
8
+
1
9
  **phyddle v0.3.1** -- 26.11.11
2
10
 
3
11
  * Add support for ancestral state estimation (https://doi.org/10.64898/2026.03.19.712918); read the paper, documentation, and tutorial to use
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "phyddle"
7
- version = "0.3.1"
7
+ version = "0.3.2"
8
8
  authors = [
9
9
  { name="Michael Landis", email="michael.landis@wustl.edu" },
10
10
  { name="Ammon Thompson", email="ammonthompson@gmail.com" },
@@ -5,7 +5,7 @@ __init__
5
5
  Initializes phyddle package and discovers all modules.
6
6
 
7
7
  Authors: Michael Landis, Ammon Thompson
8
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
8
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
9
9
  License: MIT
10
10
  """
11
11
 
@@ -14,9 +14,9 @@ License: MIT
14
14
  from multiprocessing import set_start_method
15
15
 
16
16
  __project__ = 'phyddle'
17
- __version__ = '0.3.1'
17
+ __version__ = '0.3.2'
18
18
  __author__ = 'Michael Landis and Ammon Thompson'
19
- __copyright__ = '(c) 2022-2025, Michael Landis and Ammon Thompson'
19
+ __copyright__ = '(c) 2022-2026, Michael Landis and Ammon Thompson'
20
20
  __citation__ = 'MJ Landis, A Thompson. 2024. phyddle: software for phylogenetic model exploration with deep learning. bioRxiv 2024.08.06.606717.'
21
21
 
22
22
  # DEFAULT
@@ -7,7 +7,7 @@ network and uses it to generate new estimates, e.g. estimate model parameters
7
7
  for a new empirical dataset.
8
8
 
9
9
  Authors: Michael Landis and Ammon Thompson
10
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
10
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
11
11
  License: MIT
12
12
  """
13
13
 
@@ -93,6 +93,7 @@ class Estimator:
93
93
  self.param_est = dict(args['param_est'])
94
94
  self.log_offset = float(args['log_offset'])
95
95
  self.use_cuda = bool(args['use_cuda'])
96
+ self.trn_batch_size = int(args['trn_batch_size'])
96
97
 
97
98
  # anc states
98
99
  self.asr_est = bool(args['asr_est'])
@@ -328,19 +329,19 @@ class Estimator:
328
329
 
329
330
  # denormalization factors for new aux data
330
331
  train_aux_data_norm = pd.read_csv(train_norm_aux_data_fn, sep=',', index_col=False)
331
- train_aux_data_means = train_aux_data_norm['mean'].T.to_numpy().flatten()
332
- train_aux_data_sd = train_aux_data_norm['sd'].T.to_numpy().flatten()
332
+ train_aux_data_means = train_aux_data_norm['mean'].T.to_numpy(copy=True).flatten()
333
+ train_aux_data_sd = train_aux_data_norm['sd'].T.to_numpy(copy=True).flatten()
333
334
  self.train_aux_data_mean_sd = (train_aux_data_means, train_aux_data_sd)
334
335
 
335
336
  if self.has_label_num:
336
337
  # denormalization factors for labels
337
338
  train_norm_labels_num = pd.read_csv(train_norm_labels_num_fn, sep=',', index_col=False)
338
- train_num_labels_mean = train_norm_labels_num['mean'].T.to_numpy().flatten()
339
- train_num_labels_sd = train_norm_labels_num['sd'].T.to_numpy().flatten()
339
+ train_num_labels_mean = train_norm_labels_num['mean'].T.to_numpy(copy=True).flatten()
340
+ train_num_labels_sd = train_norm_labels_num['sd'].T.to_numpy(copy=True).flatten()
340
341
  self.train_labels_num_mean_sd = (train_num_labels_mean, train_num_labels_sd)
341
342
 
342
343
  # read in CQR interval adjustments
343
- self.cpi_adjustments = pd.read_csv(model_cpi_fn, sep=',', index_col=False).to_numpy()
344
+ self.cpi_adjustments = pd.read_csv(model_cpi_fn, sep=',', index_col=False).to_numpy(copy=True)
344
345
 
345
346
  # done
346
347
  return
@@ -379,14 +380,14 @@ class Estimator:
379
380
  label_names = None
380
381
  if self.tensor_format == 'csv':
381
382
  phy_data = pd.read_csv(phy_data_fn, header=None,
382
- on_bad_lines='skip').to_numpy()
383
+ on_bad_lines='skip').to_numpy(copy=True)
383
384
  aux_data = pd.read_csv(aux_data_fn, header=None,
384
- on_bad_lines='skip').to_numpy()
385
+ on_bad_lines='skip').to_numpy(copy=True)
385
386
  idx_data = pd.read_csv(idx_data_fn,
386
387
  on_bad_lines='skip')
387
388
  if mode == 'sim':
388
389
  labels = pd.read_csv(labels_fn, header=None,
389
- on_bad_lines='skip').to_numpy()
390
+ on_bad_lines='skip').to_numpy(copy=True)
390
391
  label_names = labels[0,:]
391
392
  labels = labels[1:,:].astype('float64')
392
393
  aux_data = aux_data[1:,:].astype('float64')
@@ -394,12 +395,12 @@ class Estimator:
394
395
 
395
396
  elif self.tensor_format == 'hdf5':
396
397
  hdf5_file = h5py.File(hdf5_fn, 'r')
397
- phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy()
398
- aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy()
398
+ phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy(copy=True)
399
+ aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy(copy=True)
399
400
  idx_data = pd.DataFrame(hdf5_file['idx'], columns=['idx'])
400
401
  # idx_data = idx_data[:,:].astype('int')
401
402
  if mode == 'sim':
402
- labels = pd.DataFrame(hdf5_file['labels']).to_numpy()
403
+ labels = pd.DataFrame(hdf5_file['labels']).to_numpy(copy=True)
403
404
  label_names = [ s.decode() for s in hdf5_file['label_names'][0,:] ]
404
405
  aux_data_names = [ s.decode() for s in hdf5_file['aux_data_names'][0,:] ]
405
406
  hdf5_file.close()
@@ -471,18 +472,48 @@ class Estimator:
471
472
  out_true_labels_num_fn = f'{path_prefix}_true.labels_num.csv'
472
473
  out_est_labels_cat_fn = f'{path_prefix}_est.labels_cat.csv'
473
474
  out_true_labels_cat_fn = f'{path_prefix}_true.labels_cat.csv'
474
-
475
+
476
+ # prepare batches
477
+ num_est = self.phy_data.shape[0]
478
+ batch_size = 100 # self.trn_batch_size
479
+ est_num_batches = []
480
+ est_cat_batches = {}
481
+
475
482
  # load model
476
483
  self.mymodel = torch.load(model_arch_fn, map_location=self.TORCH_DEVICE, weights_only=False)
477
484
  self.mymodel.to(self.TORCH_DEVICE)
478
485
 
479
- # get estimates
480
- label_est = self.mymodel(torch.Tensor(self.phy_data).to(self.TORCH_DEVICE),
481
- torch.Tensor(self.aux_data).to(self.TORCH_DEVICE))
482
-
483
- # real vs. cat estimates
484
- labels_est_num = label_est[0:3]
485
- labels_est_cat = label_est[3]
486
+ # evaluation mode
487
+ self.mymodel.eval()
488
+
489
+ # do not update gradients
490
+ with torch.no_grad():
491
+
492
+ # move data to device
493
+ phy_dat = torch.Tensor(self.phy_data).to(self.TORCH_DEVICE)
494
+ aux_dat = torch.Tensor(self.aux_data).to(self.TORCH_DEVICE)
495
+
496
+ # process batches
497
+ for j in range(0, num_est, batch_size):
498
+
499
+ # get batch indices
500
+ k = min(num_est, j+batch_size)
501
+
502
+ # get estimates
503
+ lbls_hat = self.mymodel(phy_dat[j:k], aux_dat[j:k])
504
+
505
+ # collect numerical results
506
+ est_num_batches.append(torch.stack(lbls_hat[0:3]).cpu()) #.numpy())
507
+
508
+ # collect categorical results
509
+ for key in lbls_hat[3].keys():
510
+ if key not in est_cat_batches:
511
+ est_cat_batches[key] = []
512
+ est_cat_batches[key].append(lbls_hat[3][key].cpu()) #.numpy())
513
+
514
+ # build up estimates
515
+ labels_est_num = torch.cat(est_num_batches, dim=1)
516
+ labels_est_cat = {k: torch.cat(v, dim=0) for k,v in est_cat_batches.items()}
486
517
 
487
518
  # force categorical dimensionality (had problems for categ)
488
519
  for k,v in labels_est_cat.items():
@@ -493,10 +524,11 @@ class Estimator:
493
524
  if self.has_label_num:
494
525
 
495
526
  # move Tensor from device to numpy
496
- labels_est_num = torch.stack(labels_est_num).cpu().detach().numpy()
527
+ #labels_est_num = torch.stack(labels_est_num).cpu().detach().numpy()
497
528
 
498
529
  if labels_est_num.ndim == 2:
499
- labels_est_num.shape = (labels_est_num.shape[0], 1, labels_est_num.shape[1])
530
+ #labels_est_num.shape = (labels_est_num.shape[0], 1, labels_est_num.shape[1])
531
+ labels_est_num = labels_est_num.reshape(labels_est_num.shape[0], 1, labels_est_num.shape[1])
500
532
 
501
533
  if self.cpi_method == 'acp':
502
534
  dx = np.sqrt(np.exp(labels_est_num[1,:,:])) * self.cpi_adjustments[0,:]
@@ -6,7 +6,7 @@ Defines classes and methods for the Format step. This step converts raw data
6
6
  into tensor data that can be used by the Train step.
7
7
 
8
8
  Authors: Michael Landis and Ammon Thompson
9
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
9
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
10
10
  License: MIT
11
11
  """
12
12
  # standard imports
@@ -39,7 +39,7 @@ try:
39
39
  except RuntimeError:
40
40
  pass
41
41
 
42
- ##################################################
42
+ ####.to_numpydsad(#.to_numpy(#############################################
43
43
 
44
44
 
45
45
  def load(args):
@@ -52,7 +52,7 @@ def load(args):
52
52
  args (dict): Contains phyddle settings.
53
53
 
54
54
  Returns:
55
- Simulator: A new Simulator object.
55
+ Formatter: A new Formatter object.
56
56
  """
57
57
  # settings
58
58
  sys.setrecursionlimit(10000)
@@ -628,7 +628,7 @@ class Formatter:
628
628
  if is_first:
629
629
  s += ','.join(aux_data_names) + '\n'
630
630
  is_first = False
631
- s += util.ndarray_to_flat_str(x.to_numpy()) + '\n'
631
+ s += util.ndarray_to_flat_str(x.to_numpy(copy=True)) + '\n'
632
632
  outfile.write(s)
633
633
 
634
634
  # labels tensor
@@ -640,7 +640,7 @@ class Formatter:
640
640
  if is_first:
641
641
  s += ','.join(par_est_names) + '\n'
642
642
  is_first = False
643
- s += util.ndarray_to_flat_str(x.to_numpy()) + '\n'
643
+ s += util.ndarray_to_flat_str(x.to_numpy(copy=True)) + '\n'
644
644
  outfile.write(s)
645
645
 
646
646
  # replicate index
@@ -1316,23 +1316,23 @@ class Formatter:
1316
1316
  # read data
1317
1317
  if self.tensor_format == 'csv':
1318
1318
  phy_data = pd.read_csv(phy_data_fn, header=None,
1319
- on_bad_lines='skip').to_numpy()
1319
+ on_bad_lines='skip').to_numpy(copy=True)
1320
1320
  aux_data = pd.read_csv(aux_data_fn, header=None,
1321
- on_bad_lines='skip').to_numpy()
1321
+ on_bad_lines='skip').to_numpy(copy=True)
1322
1322
  idx_data = pd.read_csv(idx_data_fn,
1323
1323
  on_bad_lines='skip')
1324
1324
  labels = pd.read_csv(labels_fn, header=None,
1325
- on_bad_lines='skip').to_numpy()
1325
+ on_bad_lines='skip').to_numpy(copy=True)
1326
1326
  label_names = labels[0,:]
1327
1327
  labels = labels[1:,:].astype('float64')
1328
1328
  aux_data = aux_data[1:,:].astype('float64')
1329
1329
  aux_data_names = aux_data[0,:]
1330
1330
  elif self.tensor_format == 'hdf5':
1331
1331
  hdf5_file = h5py.File(hdf5_fn, 'r')
1332
- phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy()
1333
- aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy()
1332
+ phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy(copy=True)
1333
+ aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy(copy=True)
1334
1334
  idx_data = pd.DataFrame(hdf5_file['idx'], columns=['idx'])
1335
- labels = pd.DataFrame(hdf5_file['labels']).to_numpy()
1335
+ labels = pd.DataFrame(hdf5_file['labels']).to_numpy(copy=True)
1336
1336
  label_names = [ s.decode() for s in hdf5_file['label_names'][0,:] ]
1337
1337
  aux_data_names = [ s.decode() for s in hdf5_file['aux_data_names'][0,:] ]
1338
1338
  hdf5_file.close()
@@ -6,7 +6,7 @@ Defines classes for neural networks, loss functions, and datasets using
6
6
  PyTorch.
7
7
 
8
8
  Authors: Michael Landis and Ammon Thompson
9
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
9
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
10
10
  License: MIT
11
11
  """
12
12
 
@@ -7,7 +7,7 @@ from the Simulate, Train, and Estimate steps. Generates figures for previous
7
7
  pipeline steps, then compiles them into a standard pdf report.
8
8
 
9
9
  Authors: Michael Landis and Ammon Thompson
10
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
10
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
11
11
  License: MIT
12
12
  """
13
13
 
@@ -447,9 +447,9 @@ class Plotter:
447
447
  """
448
448
  # Densities for aux. data and labels
449
449
  if self.has_train_fmt:
450
- self.make_plot_stat_density('train', 'aux_data')
450
+ self.make_plot_stat_density('train', 'aux_data')
451
451
  if self.has_train_num and self.has_train_fmt:
452
- self.make_plot_stat_density('train', 'labels')
452
+ self.make_plot_stat_density('train', 'labels')
453
453
 
454
454
  # PCA hex bins for aux. data and labels
455
455
  if self.has_train_fmt:
@@ -460,23 +460,23 @@ class Plotter:
460
460
 
461
461
  # scatter accuracy
462
462
  if self.has_train_num:
463
- self.make_plot_scatter_accuracy('train')
463
+ self.make_plot_scatter_accuracy('train')
464
464
  if self.has_test_num:
465
- self.make_plot_scatter_accuracy('test')
465
+ self.make_plot_scatter_accuracy('test')
466
466
 
467
467
  # confusion matrix
468
468
  if self.has_train_cat:
469
- self.make_plot_confusion_matrix('train')
469
+ self.make_plot_confusion_matrix('train')
470
470
  if self.has_test_cat:
471
- self.make_plot_confusion_matrix('test')
471
+ self.make_plot_confusion_matrix('test')
472
472
 
473
473
  # point estimates and CPIs in empirical dataset
474
474
  if self.has_emp_num:
475
- self.make_plot_emp_ci()
475
+ self.make_plot_emp_ci()
476
476
 
477
477
  # bar plot for categorical in empirical dataset
478
478
  if self.has_emp_cat:
479
- self.make_plot_emp_cat()
479
+ self.make_plot_emp_cat()
480
480
 
481
481
  # training history stats
482
482
  self.make_plot_train_history()
@@ -928,6 +928,12 @@ class Plotter:
928
928
  # dist_values = np.log(dist_values + self.log_offset)
929
929
  scaler = StandardScaler()
930
930
  x = scaler.fit_transform(dist_values)
931
+
932
+ # skip dataset if only contains 0.0
933
+ if np.all(x == 0.0):
934
+ util.print_warn(f'{title} contains only 0.0 values, so no PCA plot generated')
935
+ return None
936
+
931
937
  if self.plot_pca_noise != 0.0:
932
938
  x = x + sp.stats.norm.rvs(size=x.shape, loc=0,
933
939
  scale=self.plot_pca_noise)
@@ -1083,10 +1089,10 @@ class Plotter:
1083
1089
  if len(zero_true_idx) > 0:
1084
1090
  tiny_val = np.max(lbl_true - np.min(lbl_true)) * 1E-6
1085
1091
  lbl_true[zero_true_idx] = tiny_val
1086
- lbl_est = ests[f'{p}_value'][:].to_numpy()
1087
- lbl_lower = ests[f'{p}_lower'][:].to_numpy()
1088
- lbl_upper = ests[f'{p}_upper'][:].to_numpy()
1089
- lbl_true = labels[p][:].to_numpy()
1092
+ lbl_est = ests[f'{p}_value'][:].to_numpy(copy=True)
1093
+ lbl_lower = ests[f'{p}_lower'][:].to_numpy(copy=True)
1094
+ lbl_upper = ests[f'{p}_upper'][:].to_numpy(copy=True)
1095
+ lbl_true = labels[p][:].to_numpy(copy=True)
1090
1096
 
1091
1097
  # accuracy stats
1092
1098
  stat_mae = np.mean(np.abs(lbl_est - lbl_true))
@@ -7,7 +7,7 @@ of simulated datasets (in parallel, if desired) that are later formatted and
7
7
  used to train the neural network.
8
8
 
9
9
  Authors: Michael Landis and Ammon Thompson
10
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
10
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
11
11
  License: MIT
12
12
  """
13
13
 
@@ -6,7 +6,7 @@ Defines classes and methods for the Training step, which builds and trains a
6
6
  network using the tensor data from the Formatting step.
7
7
 
8
8
  Authors: Michael Landis and Ammon Thompson
9
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
9
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
10
10
  License: MIT
11
11
  """
12
12
 
@@ -391,13 +391,13 @@ class CnnTrainer(Trainer):
391
391
  full_labels = None
392
392
  if self.tensor_format == 'csv':
393
393
  full_phy_data = pd.read_csv(input_phy_data_fn, header=None,
394
- on_bad_lines='skip').to_numpy()
394
+ on_bad_lines='skip').to_numpy(copy=True)
395
395
  full_aux_data = pd.read_csv(input_aux_data_fn, header=None,
396
- on_bad_lines='skip').to_numpy()
396
+ on_bad_lines='skip').to_numpy(copy=True)
397
397
  full_labels = pd.read_csv(input_labels_fn, header=None,
398
- on_bad_lines='skip').to_numpy()
398
+ on_bad_lines='skip').to_numpy(copy=True)
399
399
  full_idx_data = pd.read_csv(input_idx_data_fn,
400
- on_bad_lines='skip').to_numpy()
400
+ on_bad_lines='skip').to_numpy(copy=True)
401
401
  # extract headers
402
402
  self.aux_data_names = full_aux_data[0,:].tolist()
403
403
  self.label_names = full_labels[0,:].tolist()
@@ -408,10 +408,10 @@ class CnnTrainer(Trainer):
408
408
  hdf5_file = h5py.File(input_hdf5_fn, 'r')
409
409
  self.aux_data_names = [ s.decode() for s in hdf5_file['aux_data_names'][0,:] ]
410
410
  self.label_names = [ s.decode() for s in hdf5_file['label_names'][0,:] ]
411
- full_phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy()
412
- full_aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy()
413
- full_idx_data = pd.DataFrame(hdf5_file['idx']).to_numpy()
414
- full_labels = pd.DataFrame(hdf5_file['labels']).to_numpy()
411
+ full_phy_data = pd.DataFrame(hdf5_file['phy_data']).to_numpy(copy=True)
412
+ full_aux_data = pd.DataFrame(hdf5_file['aux_data']).to_numpy(copy=True)
413
+ full_idx_data = pd.DataFrame(hdf5_file['idx']).to_numpy(copy=True)
414
+ full_labels = pd.DataFrame(hdf5_file['labels']).to_numpy(copy=True)
415
415
  hdf5_file.close()
416
416
 
417
417
 
@@ -602,14 +602,20 @@ class CnnTrainer(Trainer):
602
602
  # training dataset
603
603
  train_loader = torch.utils.data.DataLoader(dataset=self.train_dataset,
604
604
  batch_size=self.trn_batch_size)
605
- num_batches = int(np.ceil(self.train_dataset.phy_data.shape[0] / self.trn_batch_size))
606
-
607
- # validation dataset
608
- val_phy_dat = torch.Tensor(self.val_dataset.phy_data).to(self.TORCH_DEVICE)
609
- val_aux_dat = torch.Tensor(self.val_dataset.aux_data).to(self.TORCH_DEVICE)
610
- val_idx_dat = torch.Tensor(self.val_dataset.idx_data).to(self.TORCH_DEVICE)
611
- val_lbl_num = torch.Tensor(self.val_dataset.labels_num).to(self.TORCH_DEVICE)
612
- val_lbl_cat = torch.LongTensor(self.val_dataset.labels_cat).to(self.TORCH_DEVICE)
605
+ num_trn_batches = int(np.ceil(self.train_dataset.phy_data.shape[0] / self.trn_batch_size))
606
+ num_trn_total = len(train_loader.dataset)
607
+
608
+ # validation dataset (use same batch size as train)
609
+ val_loader = torch.utils.data.DataLoader(dataset=self.val_dataset,
610
+ batch_size=self.trn_batch_size)
611
+ num_val_batches = int(np.ceil(self.val_dataset.phy_data.shape[0] / self.trn_batch_size))
612
+ num_val_total = len(val_loader.dataset)
613
+
614
+ # val_phy_dat = torch.Tensor(self.val_dataset.phy_data).to(self.TORCH_DEVICE)
615
+ # val_aux_dat = torch.Tensor(self.val_dataset.aux_data).to(self.TORCH_DEVICE)
616
+ # val_idx_dat = torch.Tensor(self.val_dataset.idx_data).to(self.TORCH_DEVICE)
617
+ # val_lbl_num = torch.Tensor(self.val_dataset.labels_num).to(self.TORCH_DEVICE)
618
+ # val_lbl_cat = torch.LongTensor(self.val_dataset.labels_cat).to(self.TORCH_DEVICE)
613
619
  val_bad_count = 0
614
620
 
615
621
  # model device
@@ -703,7 +709,7 @@ class CnnTrainer(Trainer):
703
709
 
704
710
  train_msg = f'Training epoch {i+1} of {self.num_epochs}'
705
711
  for j, (phy_dat, aux_dat, idx_dat, lbl_num, lbl_cat) in tqdm(enumerate(train_loader),
706
- total=num_batches,
712
+ total=num_trn_batches,
707
713
  desc=train_msg,
708
714
  smoothing=0):
709
715
 
@@ -714,7 +720,7 @@ class CnnTrainer(Trainer):
714
720
  # send labels to device
715
721
  phy_dat = phy_dat.to(self.TORCH_DEVICE)
716
722
  aux_dat = aux_dat.to(self.TORCH_DEVICE)
717
- idx_dat = idx_dat.to(self.TORCH_DEVICE)
723
+ #idx_dat = idx_dat.to(self.TORCH_DEVICE)
718
724
  lbl_num = lbl_num.to(self.TORCH_DEVICE)
719
725
  lbl_cat = lbl_cat.to(self.TORCH_DEVICE)
720
726
 
@@ -752,14 +758,15 @@ class CnnTrainer(Trainer):
752
758
  loss_combined = uncertainty_weight(torch.stack(loss_list))
753
759
 
754
760
  # collect history stats
761
+ f_trn_prop = phy_dat.size(0) / num_trn_total
755
762
  if self.has_label_num:
756
- trn_loss_value += loss_value.item() / num_batches
757
- trn_loss_lower += loss_lower.item() / num_batches
758
- trn_loss_upper += loss_upper.item() / num_batches
759
- trn_mse_value += (torch.mean((lbl_num - lbls_hat[0])**2)).item() / num_batches
760
- trn_mae_value += (torch.mean(torch.abs(lbl_num - lbls_hat[0]))).item() / num_batches
761
- trn_mape_value += 100. * (torch.median(torch.abs((lbl_num - lbls_hat[0])/lbl_num))).item() / num_batches
762
- trn_loss_combined += loss_combined.item() / num_batches
763
+ trn_loss_value += loss_value.item() * f_trn_prop
764
+ trn_loss_lower += loss_lower.item() * f_trn_prop
765
+ trn_loss_upper += loss_upper.item() * f_trn_prop
766
+ trn_mse_value += (torch.mean((lbl_num - lbls_hat[0])**2)).item() * f_trn_prop
767
+ trn_mae_value += (torch.mean(torch.abs(lbl_num - lbls_hat[0]))).item() * f_trn_prop
768
+ trn_mape_value += 100. * (torch.median(torch.abs((lbl_num - lbls_hat[0])/lbl_num))).item() * f_trn_prop
769
+ trn_loss_combined += loss_combined.item() * f_trn_prop
763
770
 
764
771
  # backward pass to update gradients
765
772
  loss_combined.backward()
@@ -772,60 +779,76 @@ class CnnTrainer(Trainer):
772
779
  trn_loss_combined, trn_mse_value,
773
780
  trn_mae_value, trn_mape_value ]
774
781
 
775
- # forward pass of validation to estimate labels
776
- val_lbls_hat = self.model(val_phy_dat, val_aux_dat)
777
-
778
- # collect validation metrics
779
- val_loss_list = list()
782
+ # VALIDATION
783
+ # metrics
780
784
  val_loss_value = 0.
781
785
  val_loss_lower = 0.
782
786
  val_loss_upper = 0.
783
787
  val_loss_combined = 0.
784
- if self.has_label_num:
785
- if self.cpi_method == 'cqr':
786
- val_loss_value = loss_value_func(val_lbls_hat[0], val_lbl_num).item()
787
- val_loss_lower = loss_lower_func(val_lbls_hat[1], val_lbl_num).item()
788
- val_loss_upper = loss_upper_func(val_lbls_hat[2], val_lbl_num).item()
789
- val_loss_list += [ val_loss_value, val_loss_lower, val_loss_upper ]
790
- elif self.cpi_method == 'acp':
791
- val_loss_value = loss_scale_func(val_lbls_hat[0], val_lbls_hat[1], val_lbl_num ).item()
792
- val_loss_lower = val_loss_value
793
- val_loss_upper = val_loss_value
794
- val_loss_list += [ val_loss_value ]
795
- # val_loss_combined = val_loss_value + val_loss_lower + val_loss_upper
796
- if self.has_label_cat:
797
- val_loss_categ = loss_categ_func(val_lbls_hat[3], val_lbl_cat).item()
798
- val_loss_list += [ val_loss_categ ]
799
-
800
- # val_loss_combined = sum(val_loss_list)
801
- if self.loss_aggregation == 'sum':
802
- # val_loss_combined = torch.stack(val_loss_list).sum()
803
- val_loss_combined = np.sum(val_loss_list)
804
- elif self.loss_aggregation == 'geometric':
805
- val_loss_combined = np.exp(np.mean(np.log(val_loss_list)))
806
- # val_loss_combined = torch.exp(torch.mean(torch.log(torch.stack(val_loss_list))))
807
- elif self.loss_aggregation == 'median':
808
- # val_loss_combined = torch.median(torch.stack(val_loss_list))
809
- val_loss_combined = np.median(val_loss_list)
810
- elif self.loss_aggregation == 'weighted_sum':
811
- #if self.device = 'cud
812
- #val_loss_combined = uncertainty_weight(val_loss_list).detach().numpy()
813
- val_loss_combined = uncertainty_weight(val_loss_list).cpu().detach().numpy()
814
-
815
-
816
788
  val_mse_value = 0.
817
- val_mae_value = 0.
818
789
  val_mape_value = 0.
819
- if self.has_label_num:
820
- val_mse_value = (torch.mean((val_lbl_num - val_lbls_hat[0])**2)).item()
821
- val_mae_value = (torch.mean(torch.abs(val_lbl_num - val_lbls_hat[0]))).item()
822
- val_mape_value = 100. * (torch.median(torch.abs((val_lbl_num - val_lbls_hat[0]) / val_lbl_num))).item()
823
-
824
- val_metric_vals = [ val_loss_value, val_loss_lower, val_loss_upper,
825
- val_loss_combined, val_mse_value, val_mae_value,
826
- val_mape_value ]
827
-
828
-
790
+ val_mae_value = 0.
791
+ # evaluation mode
792
+ self.model.eval()
793
+ # do not update gradients
794
+ with torch.no_grad():
795
+ for j, (phy_dat, aux_dat, idx_dat, lbl_num, lbl_cat) in enumerate(val_loader):
796
+
797
+ # send labels to device
798
+ phy_dat = phy_dat.to(self.TORCH_DEVICE)
799
+ aux_dat = aux_dat.to(self.TORCH_DEVICE)
800
+ #idx_dat = idx_dat.to(self.TORCH_DEVICE)
801
+ lbl_num = lbl_num.to(self.TORCH_DEVICE)
802
+ lbl_cat = lbl_cat.to(self.TORCH_DEVICE)
803
+
804
+ # forward pass of training data to estimate labels
805
+ lbls_hat = self.model(phy_dat, aux_dat)
806
+
807
+ # calculating the loss between original and predicted data points
808
+ loss_list = list()
809
+ if self.has_label_num:
810
+ if self.cpi_method == 'cqr':
811
+ loss_value = loss_value_func(lbls_hat[0], lbl_num)
812
+ loss_lower = loss_lower_func(lbls_hat[1], lbl_num)
813
+ loss_upper = loss_upper_func(lbls_hat[2], lbl_num)
814
+ loss_list += [ loss_value, loss_lower, loss_upper ]
815
+ elif self.cpi_method == 'acp':
816
+ loss_value = loss_scale_func(lbls_hat[0], lbls_hat[1], lbl_num )
817
+ loss_lower = loss_value
818
+ loss_upper = loss_value
819
+ loss_list += [ loss_value ]
820
+ if self.has_label_cat:
821
+ loss_categ = loss_categ_func(lbls_hat[3], lbl_cat)
822
+ loss_list += [ loss_categ ]
823
+ # loss_combined = torch.stack(loss_list).sum()
824
+
825
+ if self.loss_aggregation == 'sum':
826
+ loss_combined = torch.stack(loss_list).sum()
827
+ elif self.loss_aggregation == 'geometric':
828
+ loss_combined = torch.exp(torch.mean(torch.log(torch.stack(loss_list))))
829
+ elif self.loss_aggregation == 'median':
830
+ loss_combined = torch.median(torch.stack(loss_list))
831
+ elif self.loss_aggregation == 'weighted_sum':
832
+ loss_combined = uncertainty_weight(torch.stack(loss_list))
833
+
834
+ # collect history stats
835
+ f_val_prop = phy_dat.size(0) / num_val_total
836
+ if self.has_label_num:
837
+ val_loss_value += loss_value.item() * f_val_prop
838
+ val_loss_lower += loss_lower.item() * f_val_prop
839
+ val_loss_upper += loss_upper.item() * f_val_prop
840
+ val_mse_value += (torch.mean((lbl_num - lbls_hat[0])**2)).item() * f_val_prop
841
+ val_mae_value += (torch.mean(torch.abs(lbl_num - lbls_hat[0]))).item() * f_val_prop
842
+ val_mape_value += 100. * (torch.median(torch.abs((lbl_num - lbls_hat[0])/lbl_num))).item() * f_val_prop
843
+ val_loss_combined += loss_combined.item() * f_val_prop
844
+
845
+ # re-enable training
846
+ self.model.train()
847
+
848
+ val_metric_vals = [ val_loss_lower, val_loss_upper, val_loss_value,
849
+ val_loss_combined, val_mse_value,
850
+ val_mae_value, val_mape_value ]
851
+
829
852
  # update best loss scores
830
853
  if trn_loss_combined < best_trn_loss_combined:
831
854
  best_trn_loss_combined = trn_loss_combined
@@ -952,23 +975,41 @@ class CnnTrainer(Trainer):
952
975
 
953
976
  # temporarily switch to CPU
954
977
  # self.model.to('cpu')
955
-
956
- # make initial CPI estimates
957
- num_calib_examples = self.calib_phy_data_tensor.shape[0]
978
+
979
+ # calibration dataset (use same batch size as train)
958
980
  calib_loader = torch.utils.data.DataLoader(dataset=self.calib_dataset,
959
- batch_size=num_calib_examples)
960
- calib_batch = next(iter(calib_loader))
961
- calib_phy_dat, calib_aux_dat = calib_batch[0], calib_batch[1]
962
- # calib_phy_dat = calib_phy_dat.to('cpu')
963
- # calib_aux_dat = calib_aux_dat.to('cpu')
981
+ batch_size=self.trn_batch_size)
982
+ #num_calib_batches = int(np.ceil(self.calib_dataset.phy_data.shape[0] / self.trn_batch_size))
983
+ #num_calib_total = len(calib_loader.dataset)
964
984
 
965
- # get calib estimates
966
- calib_label_est = self.model(calib_phy_dat, calib_aux_dat)
985
+ # collect datasets
986
+ calib_batches = []
987
+ # evaluation mode
988
+ self.model.eval()
989
+ # do not update gradients
990
+ with torch.no_grad():
991
+ for j, (phy_dat, aux_dat, idx_dat, lbl_num, lbl_cat) in enumerate(calib_loader):
967
992
 
968
- # make CPI adjustments
969
- norm_calib_label_num_est = torch.stack(calib_label_est[0:3]).cpu().detach().numpy()
970
-
971
- #print(np.mean(norm_calib_num_est_quantiles[
993
+ # send labels to device
994
+ phy_dat = phy_dat.to(self.TORCH_DEVICE)
995
+ aux_dat = aux_dat.to(self.TORCH_DEVICE)
996
+
997
+ # forward pass of training data to estimate labels
998
+ lbls_hat = self.model(phy_dat, aux_dat)
999
+
1000
+ # detach from device
1001
+ calib_batch_one = torch.stack(lbls_hat[0:3]).cpu().detach().numpy()
1002
+
1003
+ # collect results
1004
+ calib_batches.append(calib_batch_one)
1005
+
1006
+ # build up dataset
1007
+ norm_calib_label_num_est = np.concatenate(calib_batches, axis=1)
1008
+ #norm_calib_label_num_est = torch.stack(calib_label_est[0:3]).cpu().detach().numpy()
1009
+
1010
+ # restore train mode
1011
+ self.model.train()
1012
+
972
1013
 
973
1014
  self.cpi_adjustments = None
974
1015
  if self.cpi_method == "acp":
@@ -1367,9 +1408,8 @@ class CnnTrainer(Trainer):
1367
1408
  # Symmetric non-comformity score
1368
1409
  s = np.amax(np.array((ests[0][:,i]-true[:,i], true[:,i]-ests[1][:,i])), axis=0)
1369
1410
  # get adjustment constant: 1 - alpha/2's quantile of non-comformity scores
1370
- symm_p = (1 + inner_quantile)/2 * (1 + 1/ests.shape[1])
1371
1411
  # MJL: Note to self, should be this?
1372
- # symm_p = inner_quantile * (1 + 1/ests.shape[0])
1412
+ symm_p = inner_quantile * (1 + 1/ests.shape[1])
1373
1413
 
1374
1414
  if symm_p < 0.:
1375
1415
  self.logger.write_log('trn',
@@ -7,7 +7,7 @@ Functions include argument parsing and checking, file conversion, managing
7
7
  log files, and printing to screen.
8
8
 
9
9
  Authors: Michael Landis and Ammon Thompson
10
- Copyright: (c) 2022-2025, Michael Landis and Ammon Thompson
10
+ Copyright: (c) 2022-2026, Michael Landis and Ammon Thompson
11
11
  License: MIT
12
12
  """
13
13
 
@@ -175,7 +175,7 @@ def settings_registry():
175
175
  'char_format': {'step': 'FTE', 'type': str, 'section': 'Format', 'default': 'nexus', 'help': 'File format for character data', 'choices': ['csv', 'nexus']},
176
176
  'tensor_format': {'step': 'FTEP', 'type': str, 'section': 'Format', 'default': 'hdf5', 'help': 'File format for training example tensors', 'choices': ['csv', 'hdf5']},
177
177
  'save_phyenc_csv': {'step': 'F', 'type': str, 'section': 'Format', 'default': 'F', 'help': 'Save encoded phylogenetic tensor encoding to csv?', 'bool': True},
178
- 'shuffle_test': {'step': 'F', 'type': str, 'section': 'Format', 'default': 'T', 'help': 'Shuffle which phylogeneties are in the test vs training dataset', 'bool': True},
178
+ 'shuffle_test': {'step': 'F', 'type': str, 'section': 'Format', 'default': 'T', 'help': 'Shuffle which phylogenies are in the test vs. training dataset', 'bool': True},
179
179
 
180
180
  # training options
181
181
  'num_epochs': {'step': 'T', 'type': int, 'section': 'Train', 'default': 200, 'help': 'Number of training epochs'},
@@ -185,10 +185,9 @@ def settings_registry():
185
185
  'prop_test': {'step': 'FT', 'type': float, 'section': 'Train', 'default': 0.10, 'help': 'Proportion of data used as test examples (assess trained network performance)'},
186
186
  'prop_val': {'step': 'T', 'type': float, 'section': 'Train', 'default': 0.10, 'help': 'Proportion of data used as validation examples (diagnose network overtraining)'},
187
187
  'prop_cal': {'step': 'T', 'type': float, 'section': 'Train', 'default': 0.20, 'help': 'Proportion of data used as calibration examples (calibrate CPIs)'},
188
- 'cpi_method': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'cqr', 'help': 'Method for computing conformalized prediction intervals (CPIs)', 'choices': ['cqr', 'acp']},
189
- 'cpi_coverage': {'step': 'T', 'type': float, 'section': 'Train', 'default': 0.95, 'help': 'Expected coverage percent for CPIs'},
190
- 'cpi_asymmetric': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'T', 'help': 'Use asymmetric (True) or symmetric (False) adjustments for CPIs?', 'bool': True},
191
188
  'cpi_method': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'cqr', 'help': 'What method for calibrated prediction intervals (CPIs)?', 'choices':['cqr','acp']},
189
+ 'cpi_coverage': {'step': 'T', 'type': float, 'section': 'Train', 'default': 0.95, 'help': 'Expected coverage percent for CPIs'},
190
+ 'cpi_asymmetric': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'T', 'help': 'CQR only: Use asymmetric (True) or symmetric (False) adjustments for CPIs?', 'bool': True},
192
191
  'loss_numerical': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'mse', 'help': 'Loss function for real value estimates', 'choices': ['mse', 'mae']},
193
192
  'loss_aggregation': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'sum', 'help': 'Loss function for real value estimates', 'choices': ['sum', 'weighted_sum']},
194
193
  'optimizer': {'step': 'T', 'type': str, 'section': 'Train', 'default': 'adam', 'help': 'Method used for optimizing neural network', 'choices': ['adam', 'adadelta', 'adagrad', 'adamw', 'rmsprop', 'sgd']},
@@ -1743,8 +1742,9 @@ def encode_cdvs(phy, dat, dat_asr, node_name, asr_est, asr_rotate, tree_width, t
1743
1742
  reorder = 1
1744
1743
  nd.annotations.add_new(name="reordered", value=reorder)
1745
1744
 
1746
- name= prefix + ".form.tre"
1747
- phy.write_to_path(name, schema="newick")
1745
+ if len(asr_rotate) > 0 and asr_est == True:
1746
+ form_name = prefix + '.form.tre'
1747
+ phy.write_to_path(form_name, schema='newick')
1748
1748
 
1749
1749
  # inorder traversal to fill matrix
1750
1750
  phy.seed_node.edge.length = 0
@@ -1872,8 +1872,9 @@ def encode_cblvs(phy, dat, dat_asr, node_name, asr_est, asr_rotate, tree_width,
1872
1872
  reorder = 1
1873
1873
  nd.annotations.add_new(name="reordered", value=reorder)
1874
1874
 
1875
- name= prefix + ".form.tre"
1876
- phy.write_to_path(name, schema="newick")
1875
+ if len(asr_rotate) > 0 and asr_est == True:
1876
+ form_name = prefix + '.form.tre'
1877
+ phy.write_to_path(form_name, schema='newick')
1877
1878
 
1878
1879
  # inorder traversal to fill matrix
1879
1880
  last_int_node = phy.seed_node
Binary file
File without changes
File without changes
File without changes
File without changes