<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Daniel Liu's Blog</title>
    <description>Programming and Research</description>
    <link>https://blog.liudaniel.com</link>
    <atom:link href="https://blog.liudaniel.com/feed.xml" rel="self" type="application/rss+xml" />
    <author>
      <name>Daniel Liu</name>
      <email>daniel.liu02@gmail.com</email>
      <uri>https://liudaniel.com/</uri>
    </author>
    
      <item>
        <title>Funding Open Source Bioinformatics Software</title>
        <description>&lt;p&gt;We need sustainable ways to fund quality open source bioinformatics software.&lt;/p&gt;

&lt;p&gt;I’m not really sure what’s the best solution (funding model), so I’ll try to review
some of what’s currently happening in the field.
I will mainly focus on genomics and biological sequence analysis software
since I’m most familiar with this area.&lt;/p&gt;

&lt;p&gt;First, let’s motivate &lt;em&gt;why&lt;/em&gt; open source bioinformatics is important.&lt;/p&gt;

&lt;h2 id=&quot;why-open-source-software&quot;&gt;Why open source software?&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Exists for a long time even if the original developers move on or go bankrupt.&lt;/li&gt;
  &lt;li&gt;Benefits both academia and industry: people are not starting from zero for new projects,
which leads to faster iteration speed for the entire field.&lt;/li&gt;
  &lt;li&gt;Modifiable to fit new use cases. Every lab or company seems to have their own custom data,
so there’s rarely one-size-fits-all solutions.&lt;/li&gt;
  &lt;li&gt;Aids the development of new methods by making it easier to build off of, experiment, and
compare against previous tools.&lt;/li&gt;
  &lt;li&gt;Easy to use off-the-shelf software for now, then invest in tuning or customizing later.&lt;/li&gt;
  &lt;li&gt;For employers: more likely to find people with existing experience.&lt;/li&gt;
  &lt;li&gt;For employees: allows work to be “transferable” across employers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why-bioinformatics&quot;&gt;Why bioinformatics?&lt;/h2&gt;
&lt;p&gt;Most recent major scientific discoveries are tool-based &lt;em&gt;(TODO: find the source for this)&lt;/em&gt;.
Bioinformatics software tools are needed to analyze the large amounts of data coming out of
genomic sequencers.
These tools have applications across metagenomics, pandemic monitoring, cancer biology, immunology,
verifying CRISPR edits, etc.
I also don’t thinking we are going to get “foundational models” and “AI agents” in biology without foundational
bioinformatics tooling first.&lt;/p&gt;

&lt;h2 id=&quot;status-quo&quot;&gt;Status quo&lt;/h2&gt;
&lt;p&gt;What is the state of open source bioinformatics now? Who contributes time and money?
Here’s what I’ve seen:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Scientists and PhD students in their free time, mainly as byproduct of their government grant funded
research projects.&lt;/li&gt;
  &lt;li&gt;Chan-Zuckerberg Institute (CZI) gives out grants specifically for developing and maintain open source software.
This seems to go towards well-known academic labs.&lt;/li&gt;
  &lt;li&gt;Hardware and instrument companies that want to demonstrate their devices:
    &lt;ul&gt;
      &lt;li&gt;Intel, which develops mm2-fast and bwa-mem2&lt;/li&gt;
      &lt;li&gt;Nvidia, which develops Clara Parabricks&lt;/li&gt;
      &lt;li&gt;Sequencing companies, like Illumina, Oxford Nanopore, PacBio&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of these companies seems to have developed more user-facing tools instead of libraries.
Libraries seem to be maintained by people (across industry and academia) on their own time.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TODO: Seqera Labs, St. Jude’s genomics infrastructure team&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I think academic labs do not provide the optimal environment to develop and maintain open source tools:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;PhD students need to publish&lt;/li&gt;
  &lt;li&gt;Hard to hire good software engineers (maybe exception: Aaron Quinlan’s lab)&lt;/li&gt;
  &lt;li&gt;Prominent tool builders do not have enough bandwidth to maintain tools (Heng Li’s bwa-mem)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I think simply throwing more money at academic labs may not work.&lt;/p&gt;

&lt;p&gt;Which type of funding model could work?&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Grant funding specifically for open source software (government funding or CZI)
    &lt;ul&gt;
      &lt;li&gt;Possible to provide relatively stable job and career development for one or more software engineers?&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Open core startup
    &lt;ul&gt;
      &lt;li&gt;VCs need to make their money back&lt;/li&gt;
      &lt;li&gt;Unclear if existing open core startups in other domains (like software dev tools) will work well
  in the long run with this model.&lt;/li&gt;
      &lt;li&gt;Nonzero interest rates is brutal&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Consulting or bootstrapped startup
    &lt;ul&gt;
      &lt;li&gt;Bioinformatics consulting: Fulcrum Genomics&lt;/li&gt;
      &lt;li&gt;Possible to just sell a (hosted) version without splitting attention and providing consulting services?&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Get big biotech companies to pay employees that focus on open source&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sat, 20 Jan 2024 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//open-source-bioinformatics</link>
        <link href="https://blog.liudaniel.com/open-source-bioinformatics"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/open-source-bioinformatics</guid>
      </item>
    
      <item>
        <title>One Weird Trick for Getting into CS Research Roles!</title>
        <description>&lt;p&gt;I’ve found the following method to somewhat work when applying to CS research teams.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Try to get in touch with someone on the team. Very often, just applying online is not enough. Use prior connections for this. It’s the easiest if you have worked in a similar area as the team, so you know which teams and people to contact. You will also have some common ground to chat about and some credibilty in the area.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Reimplement an algorithm that they use or is related to what they work on. Read some papers and code up something quickly. This helps you learn in detail about the problem they are trying to solve and the algorithms they use. I find this fun to do anyways, since its an opportunity to quickly learn and apply that knowledge by coding something. Often times, gaps in your understanding or important practical details reveal themselves when you try to implement something. This also gives useful knowledge if you get the position. Show them what you have done and the issues you faced or other open questions. This should show initiative, insight, and interest in the team’s work.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;???&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Profit???&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Doing steps 1 and 2 in the opposite order should also work. Pick an area, read some papers in that area, try to reimplement their algorithms, then you can get in contact with people to collaborate on research.&lt;/p&gt;
</description>
        <pubDate>Wed, 07 Sep 2022 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//one-weird-trick</link>
        <link href="https://blog.liudaniel.com/one-weird-trick"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/one-weird-trick</guid>
      </item>
    
      <item>
        <title>So you want to do some scientific research as a high school student?</title>
        <description>&lt;p&gt;It is difficult to get started doing research as a high school student without any existing connections with companies or university labs. Therefore, I have compiled a list of notable activities, programs, and awards that I have heard of. Obviously, I have not included every possible opportunity, but these should get interested and motivated students started with doing STEM research.&lt;/p&gt;

&lt;object style=&quot;margin: auto&quot; data=&quot;../assets/research_list_general.pdf&quot; type=&quot;application/pdf&quot; width=&quot;100%&quot; height=&quot;750px&quot;&gt;
Link to the list: &lt;a href=&quot;../assets/research_list_general.pdf&quot;&gt;here&lt;/a&gt;.
&lt;/object&gt;

&lt;p&gt;Have fun doing research!&lt;/p&gt;
</description>
        <pubDate>Tue, 25 Feb 2020 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//high-school-research-opportunities</link>
        <link href="https://blog.liudaniel.com/high-school-research-opportunities"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/high-school-research-opportunities</guid>
      </item>
    
      <item>
        <title>The birth of a new sub-sub-field: adversarial attacks on 3D point sets</title>
        <description>&lt;p&gt;An interesting phenomenon with neural networks is that it is incredibly easy to perturb or change the input by a little amount, and cause the network to make a completely different prediction. In general, this field of adversarial machine learning contains two main problems that everyone is slowly chipping away at:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;How can we attack neural networks with a bounded amount of change/perturbation to the input, under a certain metric?&lt;/li&gt;
  &lt;li&gt;How can we (empirically or provably) defend neural networks against adversarial attacks?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently, this is a perpetual tug of war between the attacker and the defender. There is a lot of work on both attacking and defending, particularly because coming up with an efficient and provably robust defensive method against all attacks is very difficult.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/cropped_schematics.png&quot; alt=&quot;&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Even in the rapidly expanding field of machine learning, there are stones left unturned. In the short timespan of one year, I was fortunate enough to contribute to the birth of a new subsubfield examining the adversarial robustness of neural networks that learn from 3D point sets. This was incredibly exciting—I got to witness the development and maturation of ideas in a field with many opportunities by participating &lt;em&gt;first-hand&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;In this post, I will highlight some interesting ideas born through the intersection of 3D machine learning and adversarial machine learning. This will mostly cover the two papers (&lt;a href=&quot;https://arxiv.org/abs/1901.03006&quot;&gt;first&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1908.06062&quot;&gt;second&lt;/a&gt; papers) I contributed to the field along with my mentors Ronald Yu and Hao Su, and also some work by groups all over the world. I will also provide evaluations of the plausibility and effectiveness of different ideas, especially for defending against adversarial attacks, which is prone to unintentional errors.&lt;/p&gt;

&lt;h1 id=&quot;name-of-the-game&quot;&gt;Name of the game&lt;/h1&gt;

&lt;p&gt;Before we dive into the ideas for attacking and defending 3D neural networks, let us examine what kind of ideas we want to extract in this field. Since we are examining 3D point clouds and neural networks built especially for them, we want to create attacks and defenses that are native to 3D point clouds. We can create attacks and defenses that are universal to all types of inputs, but then we would have to “compete” with previously proposed ideas. If we merely apply ideas that were used for 2D images, which are studied more thoroughly, to 3D space, then we are not bringing major contributions to the table. Therefore, we want to exploit the special properties of both 3D point clouds and the point cloud neural networks in our journey of attacking and defending.&lt;/p&gt;

&lt;p&gt;As neural networks are quite easy to attack in general, we want to ensure that our attacks satisfy desirable constraints, like imperceptibility. In 3D space, there are a lot of new constraints to explore that are not present in other domains.&lt;/p&gt;

&lt;p&gt;Now, let us go over a little background on 3D point clouds and their neural networks.&lt;/p&gt;

&lt;h2 id=&quot;point-clouds&quot;&gt;Point clouds&lt;/h2&gt;

&lt;p&gt;A 3D point cloud provides an approximation of a 3D object’s shape. This shape is only the boundary of a 3D object, so it is hollow. If we represent the true shape as an infinite set of points \(S\), then we have \(x \subset S\) for a point cloud \(x\). For simplicity, we will assume that \(x\) is evenly sampled from the true shape. When point clouds are obtained through scanning objects in the real world (LiDAR and RGB-D scans, or photogrammetry), they are usually uneven and partially occluded.&lt;/p&gt;

&lt;p&gt;3D point clouds differ from 2D images in a few ways:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The order of points does not matter.&lt;/li&gt;
  &lt;li&gt;Shape, instead of color, is represented. Along with the assumed even sampling of points, this means that there is exploitable structure in 3D point clouds.&lt;/li&gt;
  &lt;li&gt;Randomly moving the points by a small amount, dropping a few points, and adding a few points on the true shape should still represents the same object.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;learning-in-3d-space&quot;&gt;Learning in 3D space&lt;/h2&gt;

&lt;p&gt;The challenge of learning in 3D space is to deal with the order and density invariance of point clouds. The general idea of the popular &lt;a href=&quot;https://arxiv.org/abs/1612.00593&quot;&gt;PointNet&lt;/a&gt; architecture is to apply the classical multi-layer perceptron (multiplication by weight matrix) for each point &lt;em&gt;separately&lt;/em&gt;. From the three color channels of each point, we use multiple layers of matrix multiplication to obtain 1024 features. Then, max pooling is applied elementwise across the 1024-dimensional feature vectors of each point. Since the max operation is symmetrical, this is invariant to the order of points in a point set. Many other architectures basically just extend this idea with subsampling and other ideas.&lt;/p&gt;

&lt;p&gt;For density invariance, the PointNet architecture only selects a set of “critical” points from the entire input point set with the max pooling operation. This set of critical points form the skeleton of the point cloud, and the rest of the points are basically ignored by the network.&lt;/p&gt;

&lt;h1 id=&quot;basic-attacks-in-3d&quot;&gt;Basic attacks in 3D&lt;/h1&gt;

&lt;p&gt;The most straightforward attack is by using gradient descent to directly perturb the position of each point. We are essentially solving the following optimization problem for a point cloud \(x\):&lt;/p&gt;

\[\begin{align}
&amp;amp;\text{maximize}&amp;amp; &amp;amp;J(f_\theta(x + \delta), y)\\
&amp;amp;\text{subject to}&amp;amp; &amp;amp;||\delta||_p \leq \epsilon
\end{align}\]

&lt;p&gt;\(J\) represents the loss function, \(y\) is the label class, and \(f_\theta\) is a neural network parameterized by \(\theta\). Notice that the perturbation \(\delta\) is bounded by \(\epsilon\) under the \(L_p\) norm, and we do not clip \(x + \delta\) because unlike 2D image color channels, the positions of 3D points are unbounded. The \(L_p\) norm also provides a way for us to measure the perceptibility of adversarial perturbations.&lt;/p&gt;

&lt;p&gt;Just like with 2D images, this method works very well for generating adversarial attacks. Here are visualizations of adversarial perturbations (bounded with the \(L_2\) norm) on a car and a person from the ModelNet40 dataset:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/iter_car.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/iter_person.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The perturbed points are orange.&lt;/p&gt;

&lt;p&gt;A simple extension to this is to ensure that each point is perturbed by the same amount by normalizing the perturbation vector for each point. Also, it is possible to add a few new points and perturb them instead of perturbing the original points in the point cloud (for example, &lt;a href=&quot;https://arxiv.org/abs/1809.07016&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1902.10899&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;h1 id=&quot;defense-removing-outliers&quot;&gt;Defense: removing outliers&lt;/h1&gt;

&lt;p&gt;Right away, we notice that constraining the perturbations using the \(L_2\) norm results in some points being perturbed more than others, and those points become outliers in the point set. Therefore, a simple defense would be to just remove those outlier points. A common method for identifying outliers is based on each point and its \(k\)-nearest neighbors. Finding points that are outliers is done by examining the distribution of distances between each point and its nearest neighbors. Afterwards, the outlier points that are too far away from its \(k\)-nearest neigbors are removed from the point set.&lt;/p&gt;

&lt;p&gt;Removing outliers is actually very effective as a defense—it performs much better than adversarial training, a classical defense that involves teaching a neural network the correct labels for adversarial examples. In practice, removing outliers works well even if we constrain the amount of perturbation to each &lt;em&gt;point&lt;/em&gt; by an \(\epsilon\) so that large perturbations are not possible. The perturbation of each point can even contrained to the average distance between each point and its nearest neighbor in the clean point cloud, and outlier removal would still work.&lt;/p&gt;

&lt;p&gt;Interestingly, this method was proposed in parallel in many different papers on defending against adversarial attacks on point clouds. I guess everyone noticed the outliers generated by adversarial perturbations. The simpler method of randomly removing points was also proposed as a defense.&lt;/p&gt;

&lt;h1 id=&quot;defense-removing-salient-points&quot;&gt;Defense: removing salient points&lt;/h1&gt;

&lt;p&gt;Since we perturb points by their gradients, it makes sense to remove adversarial points by examining their gradients to hopefully restore the point cloud of an object. The idea is to first calculate the saliency of each point (at index \(i\) in \(x\)) through&lt;/p&gt;

\[s[i] = \max_j ||(\nabla_{x^\ast} f_\theta(x^\ast)[j])[i]||_2\]

&lt;p&gt;where \(x^\ast = x + \delta\).&lt;/p&gt;

&lt;p&gt;Then, we can sort the points by their saliencies and remove points with high saliencies. In other words, points that have large saliencies, which are the magnitudes of the gradient of each output class with respect to each point, are removed. In practice, this works well as a defense, and it performs better than adversarial training. This defense avoids the issue of being unable to identify adversarial points if there are no outliers, but we are making the assumption that points with large magnitudes of gradients are the perturbed points.&lt;/p&gt;

&lt;h1 id=&quot;defense-limitations&quot;&gt;Defense: limitations&lt;/h1&gt;

&lt;p&gt;The reason why removing points performs so well is actually due them relying on gradient masking. The max pooling operation in PointNet ignores a set of points, which causes them to not get any gradient flow. These points cannot be perturbed with gradient-based methods, so they can accurately represent the unperturbed shape of a point cloud once the perturbed points are removed. However, relying on masked gradients does not lead to truly robust models, as we will see in the shape attacks that are proposed later.&lt;/p&gt;

&lt;h1 id=&quot;minimizing-the-perceptibility-of-perturbations&quot;&gt;Minimizing the perceptibility of perturbations&lt;/h1&gt;

&lt;p&gt;So far, when we perturb a point cloud, we do not really take into account the intrinsic shape that it represents with the \(L_p\) norms. Therefore, it may prove fruitful to examine some other metrics for measuring the perturbation on 3D point clouds. One such metric is the Hausdorff distance between two sets:&lt;/p&gt;

\[\mathcal{H}(A, B) = \max_{a \in A} \min_{b \in B} ||b - a||_2\]

&lt;p&gt;This is actually not technically a metric, but it enables us to measure the distance between a perturbed point set \(A\) and a clean point set \(B\). In words, the Hausdorff distance is defined as the the maximum distance between each point in \(A\) and its closest point in \(B\). For an adversarial point set \(x^\ast\), we have two ways of using the Hausdorff distance:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;\(\mathcal{H}(x^\ast, x)\): comparing the adversarial point cloud to the clean point cloud. The advantage of this over the \(L_p\) norms is that it allows the positions of two points to be swapped, making it a much more natural metric for 3D point clouds that are order-invariant. This was proposed in &lt;a href=&quot;https://arxiv.org/abs/1809.07016&quot;&gt;this paper&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;\(\mathcal{H}(x^\ast, S)\): comparing the adversarial point cloud to the true shape of the point cloud \(x\). This takes into the account the shape of the point cloud, and is point density invariant. This was proposed in my papers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To maximize the loss of the neural network while ensuring that \(\mathcal{H}(x^\ast, S) \leq \epsilon\), we can use projected gradient descent to project the perturbation of each point onto the 3D object that the point clouds were sampled from. If the 3D object is unavailable, we can use a triangulation algorithm, like the alpha shapes algorithm, to infer the object shape. For faster projection speed, we can build some metric tree, like the VP-tree, on the triangular mesh, by representing each triangle as a point. I call this method the “distributional attack”, since it changes the distribution of points near the shape of an 3D object. The great thing about this method is that we can generate perturbations with \(\mathcal{H}(x^\ast, S) = 0\), which means that we only move the points around on the shape \(S\). If the 3D object is available, this creates &lt;em&gt;very&lt;/em&gt; imperceptible perturbations, and it reaches around 25% success rate on PointNet/PointNet++. Even with a slightly higher Hausdorff distance and an approximated surface \(S\), the perturbations remain imperceptible, while the success rate of the attack becomes much higher (&amp;gt;80%).&lt;/p&gt;

&lt;p&gt;Here is a visualization of the distributional attack on an approximated shape, with a small Hausdorff distance:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/dist_lamp.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here is a visualization with the true shape and a Hausdorff distance of exactly zero, with perturbed points in orange:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/grad_proj_car.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Although the perturbation of each point is very small and kept on the shape of the object, removing points is effective as a defense against this type of attack.&lt;/p&gt;

&lt;h1 id=&quot;shape-attacks&quot;&gt;Shape attacks&lt;/h1&gt;

&lt;p&gt;In addition to constraining the Hausdorff distance, we can add another constraint to ensure an uniform density distribution of points after perturbing points. Therefore, the perturbations must change the overall shape of an point cloud, since small changes to the point distribution are not allowed. This is more realistic than having a few points suspended in mid-air, far away from the main 3D object. Also, it is easier to control the shape than the distribution of points obtained through a scanner. Shape attacks are also effective against point removal defenses, as they modify or destroy the density information that outlier removal relies on, and removing a few points from a perturbed shape does not make it clean again.&lt;/p&gt;

&lt;h2 id=&quot;perturbation-resampling&quot;&gt;Perturbation Resampling&lt;/h2&gt;

&lt;p&gt;We can express the idea of having the points \(x^\ast\) evenly distributed on a shape \(S^\ast\) as an optimization problem maximizing the distance between points:&lt;/p&gt;

\[\begin{align}
&amp;amp;\text{maximize}&amp;amp; &amp;amp;\min_{i \in \{1 \ldots N\}} \min_{j \in \{1 \ldots N\}\setminus\{i\}} ||x^\ast[j] - x^\ast[i]||_2\\
&amp;amp;\text{subject to}&amp;amp; &amp;amp;x^\ast \subset S^\ast
\end{align}\]

&lt;p&gt;However, we do not need to solve this problem exactly; a greedy approximation using farthest point sampling works fine. The perturbation resampling attack is simple: just perturb points using gradient descent, but resample a portion of the points on the estimated adversarial shape that is determined by the perturbed points.&lt;/p&gt;

&lt;p&gt;The final result is&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/resample_lamp.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that sometimes, the perturbation of a point is so large that the triangulation algorithm cannot include it in the triangulation, so it becomes an outlier.&lt;/p&gt;

&lt;h2 id=&quot;adversarial-sticks&quot;&gt;Adversarial Sticks&lt;/h2&gt;

&lt;p&gt;We can also add new adversarial features to the shape of the point cloud. In &lt;a href=&quot;https://arxiv.org/abs/1809.07016&quot;&gt;this paper&lt;/a&gt;, they propose adding new clusters of points, or even smaller versions of other point clouds that float in mid-air, near a clean point cloud. We take a simpler and more realistic route by adding a few sticks, or line segments, that are attached to the shape of the clean point cloud. Then, the adversarial object will look like a porcupine. Conceptually, we need to figure out where to place the sticks on an object, and the length/direction of each stick. Formally, we are solving the following optimization problem:&lt;/p&gt;

\[\begin{align}
&amp;amp;\text{maximize}_{\alpha, \beta}&amp;amp; &amp;amp;J(f_\theta(x \cup \mathcal{S}_\kappa(\alpha, \beta)), y)\\
&amp;amp;\text{subject to}&amp;amp; &amp;amp;||\beta||_2 \leq \epsilon,\\
&amp;amp;&amp;amp;&amp;amp;\alpha \subset S
\end{align}\]

&lt;p&gt;\(\alpha\) is a set of points representing where the sticks are attached to the shape \(S\), \(\beta\) is a set of vectors representing the orientation of each stick, and \(\mathcal{S}_\kappa\) returns a set of \(\kappa\) points sampled on the sticks.&lt;/p&gt;

&lt;p&gt;Like perturbation resampling, we can just approximate this by first perturbing &lt;em&gt;points&lt;/em&gt; using gradient descent, and then connecting those points to the closest point on the surface of a 3D object at the end. Finally, we need to sample points on the adversarial sticks. A visualization of the result is:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/sticks_lamp.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;adversarial-sinks&quot;&gt;Adversarial Sinks&lt;/h2&gt;

&lt;p&gt;With our previous techniques, we need to resample points during gradient descent. This works, but it feels like a weird hack. So is there a fully differentiable way to perturb the shape of a point cloud?&lt;/p&gt;

&lt;p&gt;Let us assume that we have a few guide points (\(s_f\)) for this perturbation process. We also have \(s_0 \subset x\), which are the starting point positions for \(s_f\). Then, we can perturb points on the shape by attracting them to the sink points \(s_f\). This attraction falls off over distance according to the Gaussian radial basis function:&lt;/p&gt;

\[\phi_{\mu'} = e^{-(\frac{r}{\mu'})^2}\]

&lt;p&gt;The idea is similar to perturbing a few points separately through basic gradient descent, but since the sink points have attraction, they modify the overall shape of the point cloud. Each point is affected by the sum of the attractions of the \(\sigma\) sink points:&lt;/p&gt;

\[x^\ast[i] = x[i] + \tanh\big(\sum_{j = 1}^\sigma (s_f[j] - x[i]) \phi_{\mu'} (||s_0[j] - x[i]||_2)\big), \quad\forall i \in \{1 \ldots N\}\]

&lt;p&gt;where \(\mu'\) is&lt;/p&gt;

\[\mu' = \frac{\mu}{N} \sum_{i = 1}^N \min_{j \in \{1 \ldots N\}\setminus\{i\}} ||x[j] - x[i]||_2\]

&lt;p&gt;to ensure that the tunable attraction falloff relies of the distribution of points. Note that the \(\tanh\) is used to clip the perturbations. Since the entire perturbation expression is differentiable, we can use something like Adam to maximize the loss while minimizing the perturbation. A visualization of the result:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/sinks_lamp.png&quot; alt=&quot;&quot; width=&quot;250px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This method is my favorite out of all the shape attacks because it is fully differentiable, so it feels “clean” and not as hacky as resampling points. It was inspired by black holes due to my interest in strange physics stuff like quantum mechanics and spacetime.&lt;/p&gt;

&lt;p&gt;For a quick run down of all the attacks proposed in my second paper, just look at this graphic:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/attacks_2d.png&quot; alt=&quot;&quot; width=&quot;800px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The shape attacks all perform very well against point removal defenses, and they perform much better than the iterative gradient \(L_2\) attack, which represents a naive pointwise attack:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/removing_points.png&quot; alt=&quot;&quot; width=&quot;700px&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;removing-points-as-an-attack&quot;&gt;Removing points as an attack&lt;/h1&gt;

&lt;p&gt;Another avenue for attacking point clouds is through &lt;em&gt;removing&lt;/em&gt; points (&lt;a href=&quot;https://arxiv.org/abs/1812.01687&quot;&gt;this&lt;/a&gt; and &lt;a href=&quot;https://arxiv.org/abs/1902.10899&quot;&gt;this&lt;/a&gt; papers). This is done by dropping points that are part of the critical point set that contribute to decreasing the loss between the model output and the correct class. The saliency (gradient) is used to find these critical points. Note that this idea is very similar to the defense method of removing salient points.&lt;/p&gt;

&lt;p&gt;Obviously, removing more points as a defense will not help point clouds that are attacked through point removal. However, this attack is not realistic, as it is difficult to control the lost points from point clouds that are directly scanned from 3D objects.&lt;/p&gt;

&lt;p&gt;As a side note, I did independently come up with a similar method to this a long time ago (it is still in my code), but I did not test multiple iterations of this attack, so it did not perform very well. This caused me to (unfortunately) scrap the idea. The idea of removing points is quite popular, and similar attacks has been proposed independently in multiple papers.&lt;/p&gt;

&lt;p&gt;An upsampling network (&lt;a href=&quot;https://arxiv.org/abs/1812.11017&quot;&gt;this paper&lt;/a&gt;) can be used to defend against adversarially removed points, but it is quite easy to also attack the upsampling network, since it is fully differentiable anyways.&lt;/p&gt;

&lt;h1 id=&quot;beyond-3d-point-sets&quot;&gt;Beyond 3D point sets&lt;/h1&gt;

&lt;p&gt;Point clouds are easy to perturb because changing the numerical values in the input point set directly changes the point locations. For attacking other formats like voxels or meshes, it may be easier to first convert them to point clouds before perturbing them.&lt;/p&gt;

&lt;h1 id=&quot;conclusion-and-future-directions&quot;&gt;Conclusion and future directions&lt;/h1&gt;

&lt;p&gt;So far, creating truly robust defenses by using information exclusive to 3D point clouds, like point density, is still an open problem. In the beginning, we would think that 3D space is somehow more easily defensible than 2D space, but my later work showed that this is not true. 3D space and 2D space have different strengths and vulnerabilities, and there are attacks and defenses that are exclusive to one domain but not the other. As with many work on defenses in 2D, creating defenses that are robust in 3D space is not an easy task—it is quite easy to attack the assumptions that are made in certain defense techniques. From my short journey through adversarial machine learning, I envision the truly robust defense methods to be mathematically provable and domain-agnostic, so they do not need to make use of domain-specific properties (like the distribution of points) that may be easily circumvented.&lt;/p&gt;
</description>
        <pubDate>Thu, 22 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//birth-of-a-new-sub-sub-field</link>
        <link href="https://blog.liudaniel.com/birth-of-a-new-sub-sub-field"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/birth-of-a-new-sub-sub-field</guid>
      </item>
    
      <item>
        <title>Coming up with new research ideas</title>
        <description>&lt;p&gt;This post contains some notes on methods for obtaining research ideas, so I can remember to apply them when I am stuck.&lt;/p&gt;

&lt;p&gt;I believe that working efficiently is more important than blindly throwing time at a project. For my research work, I spend a lot more time &lt;em&gt;thinking&lt;/em&gt; instead of &lt;em&gt;implementing&lt;/em&gt; (at least for projects that are not focused on reimplementing existing ideas). Also, I believe it is important to have solid programming and problem solving (competitive math/programming) fundamentals, so the bottleneck in the research process is in coming up with new ideas, not implementing the ideas. In terms of actually coming up with ideas, here are a few methods:&lt;/p&gt;

&lt;h2 id=&quot;1-working-forwards&quot;&gt;1. Working forwards&lt;/h2&gt;

&lt;p&gt;Credit for this idea goes to &lt;a href=&quot;https://twitter.com/k3yavi&quot;&gt;Avi Srivastava&lt;/a&gt; for pointing this out to me.&lt;/p&gt;

&lt;p&gt;The idea is to first read existing research papers to closely examine previous work. Then, attempt to find details that they miss, or “future work” that they have not done yet. There should always be &lt;em&gt;something&lt;/em&gt; that the previous work misses, because researchers have limited time to put into their work.&lt;/p&gt;

&lt;p&gt;The UMICollapse tool (&lt;a href=&quot;https://www.biorxiv.org/content/10.1101/648683v2&quot;&gt;paper&lt;/a&gt;) I created was based off of this idea. The insight was to speed up the slow pairwise UMI comparison step that was used in previous tools. This step is a bottleneck in the speed many UMI collapsing algorithms for grouping UMIs, but many previous works only focused on how to accurately group UMIs, not how it can be done efficiently. To ensure that the algorithms I introduce are novel compared to previous algorithms, I adapted the previous well-known algorithms to the UMI deduplication task specifically, by using different tricks based on the UMI grouping algorithms. This is another example of the idea of working forwards from previous ideas by adapting them for a specific domain. For more information on UMI collapsing tricks, read my &lt;a href=&quot;https://blog.liudaniel.com/n-grams-BK-trees&quot;&gt;blog post&lt;/a&gt; on it.&lt;/p&gt;

&lt;h2 id=&quot;2-working-backwards&quot;&gt;2. Working backwards&lt;/h2&gt;

&lt;p&gt;The idea is to examine the main research problem from a completely different angle that is not yet examined in a previous work. Usually, this involves defining a new metric. For example, if the previous work claims to achieve a high score under a certain metric, then come up with a different meaningful metric and attempt to get better results under this new metric.&lt;/p&gt;

&lt;p&gt;My work on adversarial attacks in 3D space (&lt;a href=&quot;https://arxiv.org/abs/1908.06062&quot;&gt;paper&lt;/a&gt;) is an example of defining new metrics. In addition to creating effective attacks against 3D neural networks, I examined alternative goals like the perceptibility of the adversarial perturbations to humans, the ease of construction of the adversarial examples in real-life, and the effectiveness of the attacks against defensive techniques. In this case, I came up with new ideas by asking myself, “what other desirable qualities of adversarial examples do we want?”&lt;/p&gt;

&lt;h2 id=&quot;3-diversity&quot;&gt;3. Diversity&lt;/h2&gt;

&lt;p&gt;When working on a research problem and its subproblems, especially through the working forwards method, try to find diverse, marginally related ideas and attempt to connect them to the research problem. Sometimes, this connection may be novel. Even if this connection proves fruitless, it may reveal an alternative approach to the problem. The marginally related ideas can be found through learning. In fact, when learning about new topics, I often think about connecting those new topics to each of the research problems I have tackled before. Instead of looking for an overarching idea, this method involves looking at the tools, and then asking what those tools can build if they are used together.&lt;/p&gt;

&lt;p&gt;To learn different ideas, I do the following in my spare time:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Read relevant research subreddits on Reddit.&lt;/li&gt;
  &lt;li&gt;Read Hacker News.&lt;/li&gt;
  &lt;li&gt;Read tweets about blog posts and threads about new papers on Twitter.&lt;/li&gt;
  &lt;li&gt;Watch videos that summarize recent research, like &lt;a href=&quot;https://www.youtube.com/channel/UCbfYPyITQ-7l4upoX8nvctg&quot;&gt;Two Minute Papers&lt;/a&gt; on YouTube.&lt;/li&gt;
  &lt;li&gt;Skim a paper and then google things in the paper that I do not know, but seem promising. This usually involves reading Wikipedia to learn more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sometimes I come up with a random idea not exactly related to what I am working on, and then I google it to find if it has been done. Often times, someone else has already examined this idea. In this case, I can still learn a new concept that I can use in future work. Other times, if I am lucky, then I get a new connection between two different ideas that are related to my current research, and I can get a new algorithm or analysis technique out of it.&lt;/p&gt;

&lt;p&gt;An example of this diversity method in action is how I came up with the adversarial sinks attack for 3D points (&lt;a href=&quot;https://arxiv.org/abs/1908.06062&quot;&gt;paper&lt;/a&gt;). The general premise of the attack is to pull points on a 3D object towards sink points. This idea was inspired by black holes and how they attract other objects, but it was ultimately named after the source/sinks in network flow problems. I also came up with the idea of actually attempting to approximate the true shape of a 3D point set through alpha shapes, which stems from Delaunay triangulations that I considered for nearest neighbor searches in my bioinformatics work on finding similar DNA sequences.&lt;/p&gt;

&lt;p&gt;Another example of this method is how I came up with using a domain-specific language for describing patterns while preprocessing DNA sequences (&lt;a href=&quot;https://peerj.com/articles/7170/&quot;&gt;paper&lt;/a&gt;). I knew that the string matching ideas for finding patterns were related to matching regular expressions, so I thought, “why not go one step further and make a full &lt;em&gt;language&lt;/em&gt;?” Sometimes, generalizing further works well.&lt;/p&gt;

&lt;h2 id=&quot;4-collaborations&quot;&gt;4. Collaborations&lt;/h2&gt;

&lt;p&gt;It is often very difficult to cover all your bases (eg., analyzing data, paper writing, etc.) when working on a research project alone. Collaborating with others help make this easier. This also allows more ideas to be generated through conversations with someone with a slightly different point of view. For an added benefit, collaborating with others reduces the chance of them “competing” with you to solve a certain research problem.&lt;/p&gt;
</description>
        <pubDate>Mon, 19 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//new-research-ideas</link>
        <link href="https://blog.liudaniel.com/new-research-ideas"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/new-research-ideas</guid>
      </item>
    
      <item>
        <title>n-grams + BK-trees: tricks for collapsing UMIs faster</title>
        <description>&lt;p&gt;Creating a new algorithm or data structure is often not just one major insight, but a collection tricks that work together to produce better results. With that said, what kind of “better results” are we looking for? For computer scientists, this question often results in three answers: higher accuracy, faster speed, or lower memory footprint. In a recent &lt;a href=&quot;https://www.biorxiv.org/content/10.1101/648683v2&quot;&gt;paper&lt;/a&gt; (&lt;a href=&quot;https://github.com/Daniel-Liu-c0deb0t/UMICollapse&quot;&gt;code&lt;/a&gt;), I utilized this answer to explore the problem of efficiently deduplicating sequenced DNA reads with Unique Molecular Identifiers (UMIs) from a computer science perspective.&lt;/p&gt;

&lt;h1 id=&quot;a-bit-of-background&quot;&gt;A bit of background&lt;/h1&gt;

&lt;p&gt;Each UMI is a short, unique random sequence that corresponds to a certain DNA fragment. When PCR amplification is applied to DNA fragments before sequencing, each DNA fragment is duplicated multiple times. UMIs allow us to figure out which sequenced reads are duplicates by grouping reads through their UMIs. The reason why we do not group using the DNA fragments themselves is because there may be multiple copies of the same DNA fragment before PCR amplification, and we want to be able to accurately count the duplicate DNA fragments. UMIs are applied in many experiments, including single-cell RNA sequencing.&lt;/p&gt;

&lt;p&gt;As with many bioinformatics tasks that involve processing sequenced reads, the difficulty lies in handling sequencing and PCR amplification errors. Therefore, the deduplication task is just somehow grouping similar UMIs together, where similarity is defined by counting the number of mismatches between two UMI sequences. Since UMIs are used very often, this task has been thoroughly explored. Most notably, the “directional adjacency” method from &lt;a href=&quot;https://genome.cshlp.org/content/early/2017/01/18/gr.209601.116.abstract&quot;&gt;UMI-tools&lt;/a&gt; involves first obtaining the frequency of each unique UMI, and then grouping low frequency UMIs with high frequency UMIs. The main intuition behind this is idea is that UMIs that appear frequently have a very high chance of being correctly amplified and sequenced, while UMIs that appear less frequently are most likely wrong. This algorithm is discussed in more detail in &lt;a href=&quot;https://cgatoxford.wordpress.com/2015/08/14/unique-molecular-identifiers-the-problem-the-solution-and-the-proof/&quot;&gt;this&lt;/a&gt; blog post. An image comparison of different grouping algorithms, from the blog post:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/UMI_tools_grouping_methods.png&quot; alt=&quot;&quot; width=&quot;700px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Consider what happens for each UMI (which we will can the “queried” UMI): we need to quickly find UMIs that are similar to the queried UMI that also have a UMI frequency lower than our threshold. Let us call this step a single “query”. After multiple queries, we build the full UMI graph as shown above by connecting the similar UMIs. Each connection is directional, so we connect higher frequency UMIs to lower frequency UMIs. Then, we need to group lower frequency UMIs together with the high frequency UMI using the graph, and we assume that the corresponding sequenced reads for the grouped UMIs all originate from the same DNA fragment.&lt;/p&gt;

&lt;h1 id=&quot;improvements&quot;&gt;Improvements?&lt;/h1&gt;

&lt;p&gt;Applying the three things that computer scientists care about, we know that we can basically make three improvements to this process: make it more accurate, speed it up, or lower the memory footprint. We can attempt to improve the accuracy with a better algorithm than directional adjacency, but it is difficult without biological insights and actual experience with UMI data. Therefore, we are forced to settle on attempting to improve the other two areas while ensuring that we do not make any compromises on the accuracy of the directional adjacency algorithm. Directly attempting to lower the memory footprint is kind of pointless since we have to store all of the sequenced reads in memory no matter what, and most computers have a ton of memory anyways. Thus, the only option is to improve the speed of deduplication process while ensuring that the memory footprint does increase significantly.&lt;/p&gt;

&lt;p&gt;The first step is identifying the bottleneck. There are two main time-consuming procedures in the directional adjacency algorithm: building the graph of UMIs and extracting groups of UMIs. Since each UMI can only belong to one group, then it is easy to see that extracting groups of UMIs from the graph only takes time linear to the number of unique UMIs. Building the graph naively requires comparisons between each pair of unique UMIs, which scales &lt;em&gt;quadratically&lt;/em&gt; with the number of UMIs. It turns out that many tools made for grouping UMIs use the naive method for building the graph, which leaves a lot of room for improvement.&lt;/p&gt;

&lt;h1 id=&quot;n-grams-bk-trees-a-medley-of-tricks&quot;&gt;n-grams BK-trees: a medley of tricks&lt;/h1&gt;

&lt;p&gt;As a recap, our goal is to find a list of candidate UMIs that are similar to a queried UMI, and then narrow down the list to only UMIs that are low frequency. The exact frequency value of a “low frequency” UMI depends on the frequency of other UMIs, so in general we will narrow down the list of similar UMIs to only the low frequency UMIs with a fixed frequency threshold that represents the upper bound frequency. Now, we can figure out how to speed this up with different techniques.&lt;/p&gt;

&lt;h2 id=&quot;trick-1-n-grams&quot;&gt;Trick 1: n-grams&lt;/h2&gt;

&lt;p&gt;The first trick we can apply is a simple and intuitive one. The idea is to decompose each UMI into multiple “fingerprints”, and build a mapping from each fingerprint to a list of all UMIs that have that fingerprint. This fingerprint is simply a contiguous segment of the UMI, called a n-gram, and its location in the UMI. The only difficult part of this trick is how we select the segments to allow errors (mismatches) in the UMIs. It is not hard to see that if we want to allow up to \(k\) errors, then we only need to split the UMIs into \(k + 1\) n-grams. Therefore, for each queried UMI, we only need to search other UMIs that share at least one n-gram with the queried UMI. This allows us to prune UMIs by the number of errors we allow. The interesting part of this trick is that as the UMI length increases, each n-gram becomes longer, and thus rarer, and more UMIs can be pruned through this method.&lt;/p&gt;

&lt;h2 id=&quot;trick-2-bk-trees&quot;&gt;Trick 2: BK-trees&lt;/h2&gt;

&lt;p&gt;BK-trees are metric trees that partition the (in our case) space of all UMIs into shells. In other words, we pick a UMI and partition the UMI space into multiple shells of different radii that are centered around that picked UMI. The tree structure is constructed by repeatedly picking parent UMIs to partition the UMI space, and connecting each parent UMI to children UMIs that lie in each of the shells of different radii. For a more detailed walkthrough of how BK-trees work, read &lt;a href=&quot;https://signal-to-noise.xyz/post/bk-tree/&quot;&gt;this&lt;/a&gt; blog post, which contains an excellent introduction.&lt;/p&gt;

&lt;p&gt;Here is an example of a BK-tree:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/bktree.png&quot; alt=&quot;&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The main problem in the n-grams algorithm is that we have to check every single UMI that shares at least one n-gram with our queried UMI. Many of these UMIs may not be similar to our queried UMI, so it is definately a good idea to transform the list used in the n-grams method into something else, like a BK-tree. Then, each unique n-gram maps to a BK-tree that contains all of the UMIs associated with that n-gram, and we can prune even more UMIs from our search space. Here is an example of the n-grams BK-trees algorithm:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/ngrams_bktrees.png&quot; alt=&quot;&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The main advantage of the n-grams BK-trees data structure over other “brute force” type algorithms that go through all possible errors that could occur in a UMI string is that it does not directly scale exponentially as the number of errors we allow or the UMI length increases.&lt;/p&gt;

&lt;h2 id=&quot;trick-3-prune-by-frequency&quot;&gt;Trick 3: prune by frequency&lt;/h2&gt;

&lt;p&gt;A seemingly obvious property of trees is that once we know that each node in a subtree does not satisfy our criteria, we can just skip that entire subtree. If we compute the minimum frequency of all UMIs in each subtree in a BK-tree and save those values, then we can easily figure out whether a subtree contains at least one UMI with a frequency that is less than our fixed frequency threshold. With this, we can skip subtrees that only contains UMIs with frequencies greater than our threshold.&lt;/p&gt;

&lt;p&gt;This property important because it hints that we should prune UMIs by frequency while searching for similar UMIs to build the UMI graph. We can avoid visiting UMIs in the BK-trees that are similar to our queried UMI, but have a UMI frequency higher than our frequency threshold, by keeping track of the minimum UMI frequency of each subtree in each BK-tree.&lt;/p&gt;

&lt;h2 id=&quot;unused-trick-sorting-by-frequency&quot;&gt;Unused trick: sorting by frequency&lt;/h2&gt;

&lt;p&gt;Since we are pruning by frequencies, why not go one step further and also sort the UMIs by frequency before adding them one-by-one into the BK-trees? This allows lower frequency UMIs to be added closer to the root of the tree and higher frequency UMIs to be added near the leaves of the tree. As subtrees often mostly include UMIs that are far away from the root, it is more likely for a subtree to contain UMIs with frequencies higher than the threshold the farther away we get from the root. That means that more subtrees are pruned overall.&lt;/p&gt;

&lt;p&gt;In the end, we will not use this when initializing the n-grams BK-trees data structure because it requires sorting the UMIs by frequency, which actually slows down the initialization step in practice.&lt;/p&gt;

&lt;h2 id=&quot;trick-4-literally-extract-umis&quot;&gt;Trick 4: literally extract UMIs&lt;/h2&gt;

&lt;p&gt;So far, we are able to obtain the lower frequency UMIs that are similar to a queried higher frequency UMI. After querying with each UMI as the higher frequency UMI, we can build the directed graph of UMIs and group UMIs through the directional adjacency algorithm. However, notice that in the end, each UMI can only belong to one single group. Therefore, we are actually wasting time explicitly building the UMI graph, because after adding an UMI to a group in the directional adjacency algorithm, we do not ever need to revisit that UMI again. All of the extra edges leading to that UMI, which we painstakingly calculated through multiple queries, are essentially useless.&lt;/p&gt;

&lt;p&gt;So why do even build the graph in the first place if we do not use most of it? Why not just make the UMI graph &lt;em&gt;implicit&lt;/em&gt;, so we compute the edges we need? This actually works, and it basically means that we remove UMIs from our BK-trees after each query. Therefore, UMIs that are added to a group in a previous query are marked as removed and skipped in later queries. We can keep track of subtrees in each BK-tree where all of the UMIs are removed, and skip entire subtrees to save time. Since the UMI graph is not explicitly constructed, we need to merge the graph construction and the UMI extraction/grouping steps of the directional adjacency algorithm together. This means that we essentially interleave grouping the UMIs and modifying the BK-trees that represent the implicit UMI graph.&lt;/p&gt;

&lt;h2 id=&quot;trick-5-special-encoding&quot;&gt;Trick 5: special encoding&lt;/h2&gt;

&lt;p&gt;With two strings of nucleotides (A, T, C, or G) of length \(M\), we can compute the Hamming distance (how we measure similarity) in exactly \(O(M)\) time. At first it seems unlikely, but &lt;em&gt;can we do better&lt;/em&gt;? The answer is actually yes!&lt;/p&gt;

&lt;p&gt;Usually, the encoding method for nucleotides is mapping them to binary: 00, 01, 10, and 11. This is the best we can do if we optimize for size. The problem with this encoding is that we cannot easily find the Hamming distance for two arbitrary strings encoded with them. If we optimize for the speed of computing Hamming distance, we can actually get a different set of encodings: 011, 110, 101, 000. The special property of this set of encodings is that the bitwise Hamming distance between each pair of encodings is exactly 2 (try it; count the number of different bits between each pair). This means that we can easily infer the Hamming distance between two strings of nucleotides when they are encoded with that encoding by calculating the bitwise Hamming distance.&lt;/p&gt;

&lt;p&gt;The reason why we can get faster than \(O(M)\) Hamming distance computation is because we can pack a bunch of nucleotides into a single 64-bit computer word. In fact, since computing the bitwise Hamming distance is constant-time (XOR + POPCOUNT operations), we can compute the Hamming distance between two 21 nucleotide strings in constant-time! Note that encoding a nucleotide string still takes \(O(M)\) time so this is only beneficial if multiple Hamming comparisons are made. By packing a bunch of nucleotides into one computer word, we can also make hashing and other comparison operations constant-time.&lt;/p&gt;

&lt;h2 id=&quot;implementation-trick-reducing-copies&quot;&gt;Implementation trick: reducing copies&lt;/h2&gt;

&lt;p&gt;When we split UMIs into n-grams, we can avoid copying the pieces of the UMI multiple times by using views on the UMIs. A view basically represents a contiguous segment of an UMI string with only the start and end locations of the segment, which is backed by a reference to the original UMI. We can also cache the hash for each view so we do not need to recalculate it.&lt;/p&gt;

&lt;h1 id=&quot;performance-in-practice&quot;&gt;Performance in practice&lt;/h1&gt;

&lt;p&gt;It is important to remember that the tricks for speeding up UMI deduplication may &lt;em&gt;degrade&lt;/em&gt; performance compared to other algorithms on very small datasets. This is completely expected, since there are overheads associated with using those tricks.&lt;/p&gt;

&lt;p&gt;First, let us see how fast the n-grams BK-trees method performs runs compared to other methods as the number of unique UMIs increases:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/umi_increase_run_time.png&quot; alt=&quot;&quot; width=&quot;700px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The n-grams BK-trees method is able to make use of the benefits of both the n-grams and the BK-tree methods (individually), as it performs better.&lt;/p&gt;

&lt;p&gt;If we increase the length of the UMIs, then we see that it ties with the n-grams method:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/umi_length_increase_run_time.png&quot; alt=&quot;&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If we increase the number of errors allowed, then the n-grams BK-trees method scales much more favorably compared to other methods:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/umi_edits_increase_run_time.png&quot; alt=&quot;&quot; width=&quot;500px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that in all three experiments, we use simulated (randomly generated) datasets. In practice, when there are less UMIs at a single alignment coordinate, the n-grams BK-trees method does not result in such a dramatic gains in performance.&lt;/p&gt;

&lt;p&gt;For a little more information about the n-grams BK-tree data structure, we can look at some statistics about the n-grams with more than 160,000 UMIs:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/ngrams_stats.png&quot; alt=&quot;&quot; width=&quot;300px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We can see that the n-grams method is able to prune a significant portion of the UMIs. The largest BK-tree is only built on around 140 UMIs.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The n-grams BK-trees method is essentially a bag of all sorts of tricks for speeding up the UMI deduplication task. However, in general, finding similar strings to a queried string is very useful in a variety of applications. It is vital in natural language processing and bioinformatics tasks that involve clustering and grouping similar strings. Perhaps the insights and tricks behind the n-grams BK-trees method can be applied to accurately finding similar strings under the Hamming distance metric for other tasks.&lt;/p&gt;
</description>
        <pubDate>Thu, 08 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://blog.liudaniel.com//n-grams-BK-trees</link>
        <link href="https://blog.liudaniel.com/n-grams-BK-trees"/>
        <guid isPermaLink="true">https://blog.liudaniel.com/n-grams-BK-trees</guid>
      </item>
    
  </channel>
</rss>
