Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ambiguity_solver_network.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ambiguity_solver_network.py
1 import pandas as pd
2 import numpy as np
3 
4 import torch.nn as nn
5 import torch.nn.functional as F
6 import torch.utils
7 
8 import ast
9 
10 
11 def prepareDataSet(data: pd.DataFrame) -> pd.DataFrame:
12  """Format the dataset that have been written from the Csv file"""
13  """
14  @param[in] data: input DataFrame containing 1 event
15  @return: Formatted DataFrame
16  """
17  data = data
18  # Remove tracks with less than 7 measurements
19  data = data[data["nMeasurements"] > 6]
20  data = data.sort_values("good/duplicate/fake", ascending=False)
21  # Remove pure duplicate (tracks purely identical) keep the ones good one if among them.
22  data = data.drop_duplicates(
23  subset=[
24  "particleId",
25  "Hits_ID",
26  "nOutliers",
27  "nHoles",
28  "nSharedHits",
29  "chi2",
30  ],
31  keep="first",
32  )
33  data = data.sort_values("particleId")
34  # Set truth particle ID as index
35  data = data.set_index("particleId")
36  # Transform the hit list from a string to an actual list
37  hitsIds = []
38  mergedIds = []
39  for list in data["Hits_ID"].values:
40  hitsIds.append(ast.literal_eval(list))
41  data["Hits_ID"] = hitsIds
42  # Combine dataset
43  return data
44 
45 
46 class DuplicateClassifier(nn.Module):
47  """MLP model used to separate good tracks from duplicate tracks. Return one score per track the higher one correspond to the good track."""
48 
49  def __init__(self, input_dim, n_layers):
50  """Three layer MLP, 20% dropout, sigmoid activation for the last layer."""
51  super(DuplicateClassifier, self).__init__()
52  self.linear1 = nn.Linear(input_dim, n_layers[0])
53  self.linear2 = nn.Linear(n_layers[0], n_layers[1])
54  self.linear3 = nn.Linear(n_layers[1], n_layers[2])
55  self.output = nn.Linear(n_layers[2], 1)
56  self.sigmoid = nn.Sigmoid()
57 
58  def forward(self, z):
59  z = F.relu(self.linear1(z))
60  z = F.relu(self.linear2(z))
61  z = F.relu(self.linear3(z))
62  return self.sigmoid(self.output(z))
63 
64 
65 class Normalise(nn.Module):
66  """Normalisation of the input before the MLP model."""
67 
68  def __init__(self, mean, std):
69  super(Normalise, self).__init__()
70  self.mean = torch.tensor(mean, dtype=torch.float32)
71  self.std = torch.tensor(std, dtype=torch.float32)
72 
73  def forward(self, z):
74  z = z - self.mean
75  z = z / self.std
76  return z