Skip to content

Commit

Permalink
Merge pull request #25 from zilliztech/ivf_blog
Browse files Browse the repository at this point in the history
Ivf blog
  • Loading branch information
shanghaikid committed May 17, 2022
2 parents f233a4b + 119ebdd commit 22880ca
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 140 deletions.
21 changes: 18 additions & 3 deletions federjs/Feder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default class Feder {
core = null,
filePath = '',
source = '',
projectParams = {},
domSelector,
viewParams = {},
}) {
Expand All @@ -15,7 +14,7 @@ export default class Feder {
this.initCorePromise = fetch(filePath)
.then((res) => res.arrayBuffer())
.then((data) => {
core = new FederCore({ data, source, projectParams });
core = new FederCore({ data, source, params: viewParams });
this.core = core;
const indexType = core.indexType;
const indexMeta = core.indexMeta;
Expand Down Expand Up @@ -53,9 +52,25 @@ export default class Feder {
this.federView.search({ searchRes, targetMediaUrl });
}
}
async searchById(testId = null) {
this.initCorePromise && (await this.initCorePromise);
if (!(testId in this.core.id2vector)) {
console.log('Invalid Id');
} else {
const testVec = this.core.id2vector[testId];
const targetMediaUrl =
this.viewParams && this.viewParams.mediaCallback
? this.viewParams.mediaCallback(testId)
: null;
this.search(testVec, targetMediaUrl);
}
}
async searchRandTestVec() {
this.initCorePromise && (await this.initCorePromise);
const [testId, testVec] = await this.core.getTestIdAndVec();
let [testId, testVec] = await this.core.getTestIdAndVec();
while (isNaN(testId)) {
[testId, testVec] = await this.core.getTestIdAndVec();
}
console.log('random test vector:', testId, testVec);
const targetMediaUrl =
this.viewParams && this.viewParams.mediaCallback
Expand Down
4 changes: 3 additions & 1 deletion federjs/FederCore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export default class FederCore {
constructor({
data, // arraybuffer
source,
params,
}) {
try {
this.params = params;
const index = this.parserIndex(data, source);
this.index = index;
this.indexType = index.indexType;
Expand Down Expand Up @@ -56,7 +58,7 @@ export default class FederCore {
fineSearchWithProjection = false,
projectMethod,
projectParams,
} = this.searchParams;
} = this.params;
if (fineSearchWithProjection) {
const ids = searchRes.fine.map((item) => item.id);
this.initProjector({
Expand Down
2 changes: 1 addition & 1 deletion federjs/FederView/HnswView/layout/overviewShortestPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function getOverviewShortestPathData({
SPNodesLevels[preLevel].push(preNode);
}
}
preNodeInternalId =
const preNodeInternalId =
keyNode.path.length > 0 ? keyNode.path[keyNode.path.length - 1] : null;
reachableLevel = keyLevel;
reachableNodes = keyNode.linksLevels[keyLevel]
Expand Down
5 changes: 2 additions & 3 deletions federjs/FederView/IvfflatView/InfoPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export default class InfoPanel {
targetMediaUrl,
listSizes,
} = federView;
console.log('!!!!');
const items = [
{
title: 'IVF_Flat - Search',
Expand Down Expand Up @@ -177,7 +176,7 @@ export default class InfoPanel {
}

updateSearchViewFineProjectOverviewInfo(federView) {
const { k, nprobe, searchRes, targetMediaUrl, mediaCallback } = federView;
const { k, nprobe, searchRes, targetMediaUrl, mediaCallback, viewParams } = federView;
const fineAllVectorsCount = searchRes.fine.length;
const showImages = searchRes.fsResIds
.map((id) => mediaCallback(id))
Expand Down Expand Up @@ -209,7 +208,7 @@ export default class InfoPanel {
callback: () => federView.switchSearchView('project'),
},
{
text: `Projection of all ${fineAllVectorsCount} vectors in the ${nprobe} (nprobe=${nprobe}) clusters using UMAP.`,
text: `Projection of all ${fineAllVectorsCount} vectors in the ${nprobe} (nprobe=${nprobe}) clusters using ${viewParams.projectMethod || 'random'}.`,
},
{
images: showImages,
Expand Down
1 change: 1 addition & 0 deletions federjs/FederView/IvfflatView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const defaultIvfflatViewParams = {
projectPadding: [20, 20, 20, 260],
axisTickCount: 5,
polarAxisStrokeWidth: 1,
polarAxisOpacity: 0.4,
polarOriginBias: 0.15,
ease: d3.easeCubic,
animateExitTime: 1500,
Expand Down
3 changes: 2 additions & 1 deletion federjs/FederView/IvfflatView/render/renderPolarAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function renderPolarAxis({
polarMaxR,
polarAxisStrokeWidth,
canvasScale,
polarAxisOpacity,
}) {
const circles = d3
.range(axisTickCount)
Expand All @@ -17,6 +18,6 @@ export default function renderPolarAxis({
circles,
hasStroke: true,
lineWidth: polarAxisStrokeWidth * canvasScale,
strokeStyle: hexWithOpacity(ZBlue, 0.3),
strokeStyle: hexWithOpacity(ZBlue, polarAxisOpacity),
});
}
2 changes: 1 addition & 1 deletion federpy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="federpy",
version="0.3.0",
version="0.4.0",
author="min.tian@zilliz",
author_email="min.tian@zilliz.com",
description="Feder for python",
Expand Down
9 changes: 9 additions & 0 deletions federpy/src/federpy/federpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from IPython.display import display, HTML
import random


class FederPy:
def __init__(self, indexFile, indexSource, width=1000, height=620, mediaType="", mediaUrls=[]):
self.indexFile = indexFile
Expand Down Expand Up @@ -49,6 +50,14 @@ def overview(self, isDisplay=True):
else:
return self.getHtml()

def searchById(self, targetId, isDisplay=True):
self.actionJs = "feder.setSearchParams(%s)\nfeder.searchById(%s)" % (
self.searchParams, targetId)
if isDisplay:
self.showHtml()
else:
return self.getHtml()

def searchRandTestVec(self, isDisplay=True):
self.actionJs = "feder.setSearchParams(%s)\nfeder.searchRandTestVec()" % self.searchParams
if isDisplay:
Expand Down
Loading

0 comments on commit 22880ca

Please sign in to comment.